2018-06-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / match.pd
blobf70a88c362ef7d00450995cff893526d2ed59c8f
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  */
1468 (for cmp (eq ne)
1469      ocmp (lt ge)
1470  (simplify
1471   (cmp (trunc_div @0 @1) integer_zerop)
1472   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1473        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1474    (ocmp @0 @1))))
1476 /* X == C - X can never be true if C is odd.  */
1477 (for cmp (eq ne)
1478  (simplify
1479   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1480   (if (TREE_INT_CST_LOW (@1) & 1)
1481    { constant_boolean_node (cmp == NE_EXPR, type); })))
1483 /* Arguments on which one can call get_nonzero_bits to get the bits
1484    possibly set.  */
1485 (match with_possible_nonzero_bits
1486  INTEGER_CST@0)
1487 (match with_possible_nonzero_bits
1488  SSA_NAME@0
1489  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1490 /* Slightly extended version, do not make it recursive to keep it cheap.  */
1491 (match (with_possible_nonzero_bits2 @0)
1492  with_possible_nonzero_bits@0)
1493 (match (with_possible_nonzero_bits2 @0)
1494  (bit_and:c with_possible_nonzero_bits@0 @2))
1496 /* Same for bits that are known to be set, but we do not have
1497    an equivalent to get_nonzero_bits yet.  */
1498 (match (with_certain_nonzero_bits2 @0)
1499  INTEGER_CST@0)
1500 (match (with_certain_nonzero_bits2 @0)
1501  (bit_ior @1 INTEGER_CST@0))
1503 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
1504 (for cmp (eq ne)
1505  (simplify
1506   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1507   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1508    { constant_boolean_node (cmp == NE_EXPR, type); })))
1510 /* ((X inner_op C0) outer_op C1)
1511    With X being a tree where value_range has reasoned certain bits to always be
1512    zero throughout its computed value range,
1513    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1514    where zero_mask has 1's for all bits that are sure to be 0 in
1515    and 0's otherwise.
1516    if (inner_op == '^') C0 &= ~C1;
1517    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1518    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1520 (for inner_op (bit_ior bit_xor)
1521      outer_op (bit_xor bit_ior)
1522 (simplify
1523  (outer_op
1524   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1525  (with
1526   {
1527     bool fail = false;
1528     wide_int zero_mask_not;
1529     wide_int C0;
1530     wide_int cst_emit;
1532     if (TREE_CODE (@2) == SSA_NAME)
1533       zero_mask_not = get_nonzero_bits (@2);
1534     else
1535       fail = true;
1537     if (inner_op == BIT_XOR_EXPR)
1538       {
1539         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1540         cst_emit = C0 | wi::to_wide (@1);
1541       }
1542     else
1543       {
1544         C0 = wi::to_wide (@0);
1545         cst_emit = C0 ^ wi::to_wide (@1);
1546       }
1547   }
1548   (if (!fail && (C0 & zero_mask_not) == 0)
1549    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1550    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1551     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1553 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
1554 (simplify
1555   (pointer_plus (pointer_plus:s @0 @1) @3)
1556   (pointer_plus @0 (plus @1 @3)))
1558 /* Pattern match
1559      tem1 = (long) ptr1;
1560      tem2 = (long) ptr2;
1561      tem3 = tem2 - tem1;
1562      tem4 = (unsigned long) tem3;
1563      tem5 = ptr1 + tem4;
1564    and produce
1565      tem5 = ptr2;  */
1566 (simplify
1567   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1568   /* Conditionally look through a sign-changing conversion.  */
1569   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1570        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1571             || (GENERIC && type == TREE_TYPE (@1))))
1572    @1))
1573 (simplify
1574   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1575   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1576    (convert @1)))
1578 /* Pattern match
1579      tem = (sizetype) ptr;
1580      tem = tem & algn;
1581      tem = -tem;
1582      ... = ptr p+ tem;
1583    and produce the simpler and easier to analyze with respect to alignment
1584      ... = ptr & ~algn;  */
1585 (simplify
1586   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1587   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1588    (bit_and @0 { algn; })))
1590 /* Try folding difference of addresses.  */
1591 (simplify
1592  (minus (convert ADDR_EXPR@0) (convert @1))
1593  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1594   (with { poly_int64 diff; }
1595    (if (ptr_difference_const (@0, @1, &diff))
1596     { build_int_cst_type (type, diff); }))))
1597 (simplify
1598  (minus (convert @0) (convert ADDR_EXPR@1))
1599  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1600   (with { poly_int64 diff; }
1601    (if (ptr_difference_const (@0, @1, &diff))
1602     { build_int_cst_type (type, diff); }))))
1603 (simplify
1604  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1605  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1606       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1607   (with { poly_int64 diff; }
1608    (if (ptr_difference_const (@0, @1, &diff))
1609     { build_int_cst_type (type, diff); }))))
1610 (simplify
1611  (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1612  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1613       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1614   (with { poly_int64 diff; }
1615    (if (ptr_difference_const (@0, @1, &diff))
1616     { build_int_cst_type (type, diff); }))))
1618 /* If arg0 is derived from the address of an object or function, we may
1619    be able to fold this expression using the object or function's
1620    alignment.  */
1621 (simplify
1622  (bit_and (convert? @0) INTEGER_CST@1)
1623  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1624       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1625   (with
1626    {
1627      unsigned int align;
1628      unsigned HOST_WIDE_INT bitpos;
1629      get_pointer_alignment_1 (@0, &align, &bitpos);
1630    }
1631    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1632     { wide_int_to_tree (type, (wi::to_wide (@1)
1633                                & (bitpos / BITS_PER_UNIT))); }))))
1636 /* We can't reassociate at all for saturating types.  */
1637 (if (!TYPE_SATURATING (type))
1639  /* Contract negates.  */
1640  /* A + (-B) -> A - B */
1641  (simplify
1642   (plus:c @0 (convert? (negate @1)))
1643   /* Apply STRIP_NOPS on the negate.  */
1644   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1645        && !TYPE_OVERFLOW_SANITIZED (type))
1646    (with
1647     {
1648      tree t1 = type;
1649      if (INTEGRAL_TYPE_P (type)
1650          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1651        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1652     }
1653     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1654  /* A - (-B) -> A + B */
1655  (simplify
1656   (minus @0 (convert? (negate @1)))
1657   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1658        && !TYPE_OVERFLOW_SANITIZED (type))
1659    (with
1660     {
1661      tree t1 = type;
1662      if (INTEGRAL_TYPE_P (type)
1663          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1664        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1665     }
1666     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1667  /* -(T)(-A) -> (T)A
1668     Sign-extension is ok except for INT_MIN, which thankfully cannot
1669     happen without overflow.  */
1670  (simplify
1671   (negate (convert (negate @1)))
1672   (if (INTEGRAL_TYPE_P (type)
1673        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1674            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1675                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1676        && !TYPE_OVERFLOW_SANITIZED (type)
1677        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1678    (convert @1)))
1679  (simplify
1680   (negate (convert negate_expr_p@1))
1681   (if (SCALAR_FLOAT_TYPE_P (type)
1682        && ((DECIMAL_FLOAT_TYPE_P (type)
1683             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1684             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1685            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1686    (convert (negate @1))))
1687  (simplify
1688   (negate (nop_convert (negate @1)))
1689   (if (!TYPE_OVERFLOW_SANITIZED (type)
1690        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1691    (view_convert @1)))
1693  /* We can't reassociate floating-point unless -fassociative-math
1694     or fixed-point plus or minus because of saturation to +-Inf.  */
1695  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1696       && !FIXED_POINT_TYPE_P (type))
1698   /* Match patterns that allow contracting a plus-minus pair
1699      irrespective of overflow issues.  */
1700   /* (A +- B) - A       ->  +- B */
1701   /* (A +- B) -+ B      ->  A */
1702   /* A - (A +- B)       -> -+ B */
1703   /* A +- (B -+ A)      ->  +- B */
1704   (simplify
1705     (minus (plus:c @0 @1) @0)
1706     @1)
1707   (simplify
1708     (minus (minus @0 @1) @0)
1709     (negate @1))
1710   (simplify
1711     (plus:c (minus @0 @1) @1)
1712     @0)
1713   (simplify
1714    (minus @0 (plus:c @0 @1))
1715    (negate @1))
1716   (simplify
1717    (minus @0 (minus @0 @1))
1718    @1)
1719   /* (A +- B) + (C - A)   -> C +- B */
1720   /* (A +  B) - (A - C)   -> B + C */
1721   /* More cases are handled with comparisons.  */
1722   (simplify
1723    (plus:c (plus:c @0 @1) (minus @2 @0))
1724    (plus @2 @1))
1725   (simplify
1726    (plus:c (minus @0 @1) (minus @2 @0))
1727    (minus @2 @1))
1728   (simplify
1729    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1730    (if (TYPE_OVERFLOW_UNDEFINED (type)
1731         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1732     (pointer_diff @2 @1)))
1733   (simplify
1734    (minus (plus:c @0 @1) (minus @0 @2))
1735    (plus @1 @2))
1737   /* (A +- CST1) +- CST2 -> A + CST3
1738      Use view_convert because it is safe for vectors and equivalent for
1739      scalars.  */
1740   (for outer_op (plus minus)
1741    (for inner_op (plus minus)
1742         neg_inner_op (minus plus)
1743     (simplify
1744      (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1745                CONSTANT_CLASS_P@2)
1746      /* If one of the types wraps, use that one.  */
1747      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1748       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1749          forever if something doesn't simplify into a constant.  */
1750       (if (!CONSTANT_CLASS_P (@0))
1751        (if (outer_op == PLUS_EXPR)
1752         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1753         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1754       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1755            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1756        (if (outer_op == PLUS_EXPR)
1757         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1758         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1759        /* If the constant operation overflows we cannot do the transform
1760           directly as we would introduce undefined overflow, for example
1761           with (a - 1) + INT_MIN.  */
1762        (if (types_match (type, @0))
1763         (with { tree cst = const_binop (outer_op == inner_op
1764                                         ? PLUS_EXPR : MINUS_EXPR,
1765                                         type, @1, @2); }
1766          (if (cst && !TREE_OVERFLOW (cst))
1767           (inner_op @0 { cst; } )
1768           /* X+INT_MAX+1 is X-INT_MIN.  */
1769           (if (INTEGRAL_TYPE_P (type) && cst
1770                && wi::to_wide (cst) == wi::min_value (type))
1771            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1772            /* Last resort, use some unsigned type.  */
1773            (with { tree utype = unsigned_type_for (type); }
1774             (if (utype)
1775              (view_convert (inner_op
1776                             (view_convert:utype @0)
1777                             (view_convert:utype
1778                              { drop_tree_overflow (cst); }))))))))))))))
1780   /* (CST1 - A) +- CST2 -> CST3 - A  */
1781   (for outer_op (plus minus)
1782    (simplify
1783     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1784     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1785      (if (cst && !TREE_OVERFLOW (cst))
1786       (minus { cst; } @0)))))
1788   /* CST1 - (CST2 - A) -> CST3 + A  */
1789   (simplify
1790    (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1791    (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1792     (if (cst && !TREE_OVERFLOW (cst))
1793      (plus { cst; } @0))))
1795   /* ~A + A -> -1 */
1796   (simplify
1797    (plus:c (bit_not @0) @0)
1798    (if (!TYPE_OVERFLOW_TRAPS (type))
1799     { build_all_ones_cst (type); }))
1801   /* ~A + 1 -> -A */
1802   (simplify
1803    (plus (convert? (bit_not @0)) integer_each_onep)
1804    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1805     (negate (convert @0))))
1807   /* -A - 1 -> ~A */
1808   (simplify
1809    (minus (convert? (negate @0)) integer_each_onep)
1810    (if (!TYPE_OVERFLOW_TRAPS (type)
1811         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1812     (bit_not (convert @0))))
1814   /* -1 - A -> ~A */
1815   (simplify
1816    (minus integer_all_onesp @0)
1817    (bit_not @0))
1819   /* (T)(P + A) - (T)P -> (T) A */
1820   (simplify
1821    (minus (convert (plus:c @@0 @1))
1822     (convert? @0))
1823    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1824         /* For integer types, if A has a smaller type
1825            than T the result depends on the possible
1826            overflow in P + A.
1827            E.g. T=size_t, A=(unsigned)429497295, P>0.
1828            However, if an overflow in P + A would cause
1829            undefined behavior, we can assume that there
1830            is no overflow.  */
1831         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1832             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1833     (convert @1)))
1834   (simplify
1835    (minus (convert (pointer_plus @@0 @1))
1836     (convert @0))
1837    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1838         /* For pointer types, if the conversion of A to the
1839            final type requires a sign- or zero-extension,
1840            then we have to punt - it is not defined which
1841            one is correct.  */
1842         || (POINTER_TYPE_P (TREE_TYPE (@0))
1843             && TREE_CODE (@1) == INTEGER_CST
1844             && tree_int_cst_sign_bit (@1) == 0))
1845     (convert @1)))
1846    (simplify
1847     (pointer_diff (pointer_plus @@0 @1) @0)
1848     /* The second argument of pointer_plus must be interpreted as signed, and
1849        thus sign-extended if necessary.  */
1850     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1851      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1852         second arg is unsigned even when we need to consider it as signed,
1853         we don't want to diagnose overflow here.  */
1854      (convert (view_convert:stype @1))))
1856   /* (T)P - (T)(P + A) -> -(T) A */
1857   (simplify
1858    (minus (convert? @0)
1859     (convert (plus:c @@0 @1)))
1860    (if (INTEGRAL_TYPE_P (type)
1861         && TYPE_OVERFLOW_UNDEFINED (type)
1862         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1863     (with { tree utype = unsigned_type_for (type); }
1864      (convert (negate (convert:utype @1))))
1865     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1866          /* For integer types, if A has a smaller type
1867             than T the result depends on the possible
1868             overflow in P + A.
1869             E.g. T=size_t, A=(unsigned)429497295, P>0.
1870             However, if an overflow in P + A would cause
1871             undefined behavior, we can assume that there
1872             is no overflow.  */
1873          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1874              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1875      (negate (convert @1)))))
1876   (simplify
1877    (minus (convert @0)
1878     (convert (pointer_plus @@0 @1)))
1879    (if (INTEGRAL_TYPE_P (type)
1880         && TYPE_OVERFLOW_UNDEFINED (type)
1881         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1882     (with { tree utype = unsigned_type_for (type); }
1883      (convert (negate (convert:utype @1))))
1884     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1885          /* For pointer types, if the conversion of A to the
1886             final type requires a sign- or zero-extension,
1887             then we have to punt - it is not defined which
1888             one is correct.  */
1889          || (POINTER_TYPE_P (TREE_TYPE (@0))
1890              && TREE_CODE (@1) == INTEGER_CST
1891              && tree_int_cst_sign_bit (@1) == 0))
1892      (negate (convert @1)))))
1893    (simplify
1894     (pointer_diff @0 (pointer_plus @@0 @1))
1895     /* The second argument of pointer_plus must be interpreted as signed, and
1896        thus sign-extended if necessary.  */
1897     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1898      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1899         second arg is unsigned even when we need to consider it as signed,
1900         we don't want to diagnose overflow here.  */
1901      (negate (convert (view_convert:stype @1)))))
1903   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1904   (simplify
1905    (minus (convert (plus:c @@0 @1))
1906     (convert (plus:c @0 @2)))
1907    (if (INTEGRAL_TYPE_P (type)
1908         && TYPE_OVERFLOW_UNDEFINED (type)
1909         && element_precision (type) <= element_precision (TREE_TYPE (@1))
1910         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
1911     (with { tree utype = unsigned_type_for (type); }
1912      (convert (minus (convert:utype @1) (convert:utype @2))))
1913     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1914           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1915          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1916              /* For integer types, if A has a smaller type
1917                 than T the result depends on the possible
1918                 overflow in P + A.
1919                 E.g. T=size_t, A=(unsigned)429497295, P>0.
1920                 However, if an overflow in P + A would cause
1921                 undefined behavior, we can assume that there
1922                 is no overflow.  */
1923              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1924                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1925                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1926                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
1927      (minus (convert @1) (convert @2)))))
1928   (simplify
1929    (minus (convert (pointer_plus @@0 @1))
1930     (convert (pointer_plus @0 @2)))
1931    (if (INTEGRAL_TYPE_P (type)
1932         && TYPE_OVERFLOW_UNDEFINED (type)
1933         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1934     (with { tree utype = unsigned_type_for (type); }
1935      (convert (minus (convert:utype @1) (convert:utype @2))))
1936     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1937          /* For pointer types, if the conversion of A to the
1938             final type requires a sign- or zero-extension,
1939             then we have to punt - it is not defined which
1940             one is correct.  */
1941          || (POINTER_TYPE_P (TREE_TYPE (@0))
1942              && TREE_CODE (@1) == INTEGER_CST
1943              && tree_int_cst_sign_bit (@1) == 0
1944              && TREE_CODE (@2) == INTEGER_CST
1945              && tree_int_cst_sign_bit (@2) == 0))
1946      (minus (convert @1) (convert @2)))))
1947    (simplify
1948     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
1949     /* The second argument of pointer_plus must be interpreted as signed, and
1950        thus sign-extended if necessary.  */
1951     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1952      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1953         second arg is unsigned even when we need to consider it as signed,
1954         we don't want to diagnose overflow here.  */
1955      (minus (convert (view_convert:stype @1))
1956             (convert (view_convert:stype @2)))))))
1958 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
1959     Modeled after fold_plusminus_mult_expr.  */
1960 (if (!TYPE_SATURATING (type)
1961      && (!FLOAT_TYPE_P (type) || flag_associative_math))
1962  (for plusminus (plus minus)
1963   (simplify
1964    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
1965    (if ((!ANY_INTEGRAL_TYPE_P (type)
1966          || TYPE_OVERFLOW_WRAPS (type)
1967          || (INTEGRAL_TYPE_P (type)
1968              && tree_expr_nonzero_p (@0)
1969              && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1970         /* If @1 +- @2 is constant require a hard single-use on either
1971            original operand (but not on both).  */
1972         && (single_use (@3) || single_use (@4)))
1973     (mult (plusminus @1 @2) @0)))
1974   /* We cannot generate constant 1 for fract.  */
1975   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
1976    (simplify
1977     (plusminus @0 (mult:c@3 @0 @2))
1978     (if ((!ANY_INTEGRAL_TYPE_P (type)
1979           || TYPE_OVERFLOW_WRAPS (type)
1980           || (INTEGRAL_TYPE_P (type)
1981               && tree_expr_nonzero_p (@0)
1982               && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1983          && single_use (@3))
1984      (mult (plusminus { build_one_cst (type); } @2) @0)))
1985    (simplify
1986     (plusminus (mult:c@3 @0 @2) @0)
1987     (if ((!ANY_INTEGRAL_TYPE_P (type)
1988           || TYPE_OVERFLOW_WRAPS (type)
1989           || (INTEGRAL_TYPE_P (type)
1990               && tree_expr_nonzero_p (@0)
1991               && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1992          && single_use (@3))
1993      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
1995 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
1997 (for minmax (min max FMIN_ALL FMAX_ALL)
1998  (simplify
1999   (minmax @0 @0)
2000   @0))
2001 /* min(max(x,y),y) -> y.  */
2002 (simplify
2003  (min:c (max:c @0 @1) @1)
2004  @1)
2005 /* max(min(x,y),y) -> y.  */
2006 (simplify
2007  (max:c (min:c @0 @1) @1)
2008  @1)
2009 /* max(a,-a) -> abs(a).  */
2010 (simplify
2011  (max:c @0 (negate @0))
2012  (if (TREE_CODE (type) != COMPLEX_TYPE
2013       && (! ANY_INTEGRAL_TYPE_P (type)
2014           || TYPE_OVERFLOW_UNDEFINED (type)))
2015   (abs @0)))
2016 /* min(a,-a) -> -abs(a).  */
2017 (simplify
2018  (min:c @0 (negate @0))
2019  (if (TREE_CODE (type) != COMPLEX_TYPE
2020       && (! ANY_INTEGRAL_TYPE_P (type)
2021           || TYPE_OVERFLOW_UNDEFINED (type)))
2022   (negate (abs @0))))
2023 (simplify
2024  (min @0 @1)
2025  (switch
2026   (if (INTEGRAL_TYPE_P (type)
2027        && TYPE_MIN_VALUE (type)
2028        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2029    @1)
2030   (if (INTEGRAL_TYPE_P (type)
2031        && TYPE_MAX_VALUE (type)
2032        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2033    @0)))
2034 (simplify
2035  (max @0 @1)
2036  (switch
2037   (if (INTEGRAL_TYPE_P (type)
2038        && TYPE_MAX_VALUE (type)
2039        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2040    @1)
2041   (if (INTEGRAL_TYPE_P (type)
2042        && TYPE_MIN_VALUE (type)
2043        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2044    @0)))
2046 /* max (a, a + CST) -> a + CST where CST is positive.  */
2047 /* max (a, a + CST) -> a where CST is negative.  */
2048 (simplify
2049  (max:c @0 (plus@2 @0 INTEGER_CST@1))
2050   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2051    (if (tree_int_cst_sgn (@1) > 0)
2052     @2
2053     @0)))
2055 /* min (a, a + CST) -> a where CST is positive.  */
2056 /* min (a, a + CST) -> a + CST where CST is negative. */
2057 (simplify
2058  (min:c @0 (plus@2 @0 INTEGER_CST@1))
2059   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2060    (if (tree_int_cst_sgn (@1) > 0)
2061     @0
2062     @2)))
2064 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2065    and the outer convert demotes the expression back to x's type.  */
2066 (for minmax (min max)
2067  (simplify
2068   (convert (minmax@0 (convert @1) INTEGER_CST@2))
2069   (if (INTEGRAL_TYPE_P (type)
2070        && types_match (@1, type) && int_fits_type_p (@2, type)
2071        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2072        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2073    (minmax @1 (convert @2)))))
2075 (for minmax (FMIN_ALL FMAX_ALL)
2076  /* If either argument is NaN, return the other one.  Avoid the
2077     transformation if we get (and honor) a signalling NaN.  */
2078  (simplify
2079   (minmax:c @0 REAL_CST@1)
2080   (if (real_isnan (TREE_REAL_CST_PTR (@1))
2081        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2082    @0)))
2083 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
2084    functions to return the numeric arg if the other one is NaN.
2085    MIN and MAX don't honor that, so only transform if -ffinite-math-only
2086    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
2087    worry about it either.  */
2088 (if (flag_finite_math_only)
2089  (simplify
2090   (FMIN_ALL @0 @1)
2091   (min @0 @1))
2092  (simplify
2093   (FMAX_ALL @0 @1)
2094   (max @0 @1)))
2095 /* min (-A, -B) -> -max (A, B)  */
2096 (for minmax (min max FMIN_ALL FMAX_ALL)
2097      maxmin (max min FMAX_ALL FMIN_ALL)
2098  (simplify
2099   (minmax (negate:s@2 @0) (negate:s@3 @1))
2100   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2101        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2102            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2103    (negate (maxmin @0 @1)))))
2104 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2105    MAX (~X, ~Y) -> ~MIN (X, Y)  */
2106 (for minmax (min max)
2107  maxmin (max min)
2108  (simplify
2109   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2110   (bit_not (maxmin @0 @1))))
2112 /* MIN (X, Y) == X -> X <= Y  */
2113 (for minmax (min min max max)
2114      cmp    (eq  ne  eq  ne )
2115      out    (le  gt  ge  lt )
2116  (simplify
2117   (cmp:c (minmax:c @0 @1) @0)
2118   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2119    (out @0 @1))))
2120 /* MIN (X, 5) == 0 -> X == 0
2121    MIN (X, 5) == 7 -> false  */
2122 (for cmp (eq ne)
2123  (simplify
2124   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2125   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2126                  TYPE_SIGN (TREE_TYPE (@0))))
2127    { constant_boolean_node (cmp == NE_EXPR, type); }
2128    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2129                   TYPE_SIGN (TREE_TYPE (@0))))
2130     (cmp @0 @2)))))
2131 (for cmp (eq ne)
2132  (simplify
2133   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2134   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2135                  TYPE_SIGN (TREE_TYPE (@0))))
2136    { constant_boolean_node (cmp == NE_EXPR, type); }
2137    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2138                   TYPE_SIGN (TREE_TYPE (@0))))
2139     (cmp @0 @2)))))
2140 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
2141 (for minmax (min     min     max     max     min     min     max     max    )
2142      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
2143      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2144  (simplify
2145   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2146   (comb (cmp @0 @2) (cmp @1 @2))))
2148 /* Simplifications of shift and rotates.  */
2150 (for rotate (lrotate rrotate)
2151  (simplify
2152   (rotate integer_all_onesp@0 @1)
2153   @0))
2155 /* Optimize -1 >> x for arithmetic right shifts.  */
2156 (simplify
2157  (rshift integer_all_onesp@0 @1)
2158  (if (!TYPE_UNSIGNED (type)
2159       && tree_expr_nonnegative_p (@1))
2160   @0))
2162 /* Optimize (x >> c) << c into x & (-1<<c).  */
2163 (simplify
2164  (lshift (rshift @0 INTEGER_CST@1) @1)
2165  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2166   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2168 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2169    types.  */
2170 (simplify
2171  (rshift (lshift @0 INTEGER_CST@1) @1)
2172  (if (TYPE_UNSIGNED (type)
2173       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2174   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2176 (for shiftrotate (lrotate rrotate lshift rshift)
2177  (simplify
2178   (shiftrotate @0 integer_zerop)
2179   (non_lvalue @0))
2180  (simplify
2181   (shiftrotate integer_zerop@0 @1)
2182   @0)
2183  /* Prefer vector1 << scalar to vector1 << vector2
2184     if vector2 is uniform.  */
2185  (for vec (VECTOR_CST CONSTRUCTOR)
2186   (simplify
2187    (shiftrotate @0 vec@1)
2188    (with { tree tem = uniform_vector_p (@1); }
2189     (if (tem)
2190      (shiftrotate @0 { tem; }))))))
2192 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2193    Y is 0.  Similarly for X >> Y.  */
2194 #if GIMPLE
2195 (for shift (lshift rshift)
2196  (simplify
2197   (shift @0 SSA_NAME@1)
2198    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2199     (with {
2200       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2201       int prec = TYPE_PRECISION (TREE_TYPE (@1));
2202      }
2203      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2204       @0)))))
2205 #endif
2207 /* Rewrite an LROTATE_EXPR by a constant into an
2208    RROTATE_EXPR by a new constant.  */
2209 (simplify
2210  (lrotate @0 INTEGER_CST@1)
2211  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2212                             build_int_cst (TREE_TYPE (@1),
2213                                            element_precision (type)), @1); }))
2215 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
2216 (for op (lrotate rrotate rshift lshift)
2217  (simplify
2218   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2219   (with { unsigned int prec = element_precision (type); }
2220    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2221         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2222         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2223         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2224     (with { unsigned int low = (tree_to_uhwi (@1)
2225                                 + tree_to_uhwi (@2)); }
2226      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2227         being well defined.  */
2228      (if (low >= prec)
2229       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2230        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2231        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2232         { build_zero_cst (type); }
2233         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2234       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2237 /* ((1 << A) & 1) != 0 -> A == 0
2238    ((1 << A) & 1) == 0 -> A != 0 */
2239 (for cmp (ne eq)
2240      icmp (eq ne)
2241  (simplify
2242   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2243   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2245 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2246    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2247    if CST2 != 0.  */
2248 (for cmp (ne eq)
2249  (simplify
2250   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2251   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2252    (if (cand < 0
2253         || (!integer_zerop (@2)
2254             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2255     { constant_boolean_node (cmp == NE_EXPR, type); }
2256     (if (!integer_zerop (@2)
2257          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2258      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2260 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2261         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2262    if the new mask might be further optimized.  */
2263 (for shift (lshift rshift)
2264  (simplify
2265   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2266            INTEGER_CST@2)
2267    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2268         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2269         && tree_fits_uhwi_p (@1)
2270         && tree_to_uhwi (@1) > 0
2271         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2272     (with
2273      {
2274        unsigned int shiftc = tree_to_uhwi (@1);
2275        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2276        unsigned HOST_WIDE_INT newmask, zerobits = 0;
2277        tree shift_type = TREE_TYPE (@3);
2278        unsigned int prec;
2280        if (shift == LSHIFT_EXPR)
2281          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2282        else if (shift == RSHIFT_EXPR
2283                 && type_has_mode_precision_p (shift_type))
2284          {
2285            prec = TYPE_PRECISION (TREE_TYPE (@3));
2286            tree arg00 = @0;
2287            /* See if more bits can be proven as zero because of
2288               zero extension.  */
2289            if (@3 != @0
2290                && TYPE_UNSIGNED (TREE_TYPE (@0)))
2291              {
2292                tree inner_type = TREE_TYPE (@0);
2293                if (type_has_mode_precision_p (inner_type)
2294                    && TYPE_PRECISION (inner_type) < prec)
2295                  {
2296                    prec = TYPE_PRECISION (inner_type);
2297                    /* See if we can shorten the right shift.  */
2298                    if (shiftc < prec)
2299                      shift_type = inner_type;
2300                    /* Otherwise X >> C1 is all zeros, so we'll optimize
2301                       it into (X, 0) later on by making sure zerobits
2302                       is all ones.  */
2303                  }
2304              }
2305            zerobits = HOST_WIDE_INT_M1U;
2306            if (shiftc < prec)
2307              {
2308                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2309                zerobits <<= prec - shiftc;
2310              }
2311            /* For arithmetic shift if sign bit could be set, zerobits
2312               can contain actually sign bits, so no transformation is
2313               possible, unless MASK masks them all away.  In that
2314               case the shift needs to be converted into logical shift.  */
2315            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2316                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2317              {
2318                if ((mask & zerobits) == 0)
2319                  shift_type = unsigned_type_for (TREE_TYPE (@3));
2320                else
2321                  zerobits = 0;
2322              }
2323          }
2324      }
2325      /* ((X << 16) & 0xff00) is (X, 0).  */
2326      (if ((mask & zerobits) == mask)
2327       { build_int_cst (type, 0); }
2328       (with { newmask = mask | zerobits; }
2329        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2330         (with
2331          {
2332            /* Only do the transformation if NEWMASK is some integer
2333               mode's mask.  */
2334            for (prec = BITS_PER_UNIT;
2335                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2336              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2337                break;
2338          }
2339          (if (prec < HOST_BITS_PER_WIDE_INT
2340               || newmask == HOST_WIDE_INT_M1U)
2341           (with
2342            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2343            (if (!tree_int_cst_equal (newmaskt, @2))
2344             (if (shift_type != TREE_TYPE (@3))
2345              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2346              (bit_and @4 { newmaskt; })))))))))))))
2348 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2349    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
2350 (for shift (lshift rshift)
2351  (for bit_op (bit_and bit_xor bit_ior)
2352   (simplify
2353    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2354    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2355     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2356      (bit_op (shift (convert @0) @1) { mask; }))))))
2358 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
2359 (simplify
2360  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2361   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2362        && (element_precision (TREE_TYPE (@0))
2363            <= element_precision (TREE_TYPE (@1))
2364            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2365    (with
2366     { tree shift_type = TREE_TYPE (@0); }
2367      (convert (rshift (convert:shift_type @1) @2)))))
2369 /* ~(~X >>r Y) -> X >>r Y
2370    ~(~X <<r Y) -> X <<r Y */
2371 (for rotate (lrotate rrotate)
2372  (simplify
2373   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2374    (if ((element_precision (TREE_TYPE (@0))
2375          <= element_precision (TREE_TYPE (@1))
2376          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2377         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2378             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2379     (with
2380      { tree rotate_type = TREE_TYPE (@0); }
2381       (convert (rotate (convert:rotate_type @1) @2))))))
2383 /* Simplifications of conversions.  */
2385 /* Basic strip-useless-type-conversions / strip_nops.  */
2386 (for cvt (convert view_convert float fix_trunc)
2387  (simplify
2388   (cvt @0)
2389   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2390        || (GENERIC && type == TREE_TYPE (@0)))
2391    @0)))
2393 /* Contract view-conversions.  */
2394 (simplify
2395   (view_convert (view_convert @0))
2396   (view_convert @0))
2398 /* For integral conversions with the same precision or pointer
2399    conversions use a NOP_EXPR instead.  */
2400 (simplify
2401   (view_convert @0)
2402   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2403        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2404        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2405    (convert @0)))
2407 /* Strip inner integral conversions that do not change precision or size, or
2408    zero-extend while keeping the same size (for bool-to-char).  */
2409 (simplify
2410   (view_convert (convert@0 @1))
2411   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2412        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2413        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2414        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2415            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2416                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2417    (view_convert @1)))
2419 /* Re-association barriers around constants and other re-association
2420    barriers can be removed.  */
2421 (simplify
2422  (paren CONSTANT_CLASS_P@0)
2423  @0)
2424 (simplify
2425  (paren (paren@1 @0))
2426  @1)
2428 /* Handle cases of two conversions in a row.  */
2429 (for ocvt (convert float fix_trunc)
2430  (for icvt (convert float)
2431   (simplify
2432    (ocvt (icvt@1 @0))
2433    (with
2434     {
2435       tree inside_type = TREE_TYPE (@0);
2436       tree inter_type = TREE_TYPE (@1);
2437       int inside_int = INTEGRAL_TYPE_P (inside_type);
2438       int inside_ptr = POINTER_TYPE_P (inside_type);
2439       int inside_float = FLOAT_TYPE_P (inside_type);
2440       int inside_vec = VECTOR_TYPE_P (inside_type);
2441       unsigned int inside_prec = TYPE_PRECISION (inside_type);
2442       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2443       int inter_int = INTEGRAL_TYPE_P (inter_type);
2444       int inter_ptr = POINTER_TYPE_P (inter_type);
2445       int inter_float = FLOAT_TYPE_P (inter_type);
2446       int inter_vec = VECTOR_TYPE_P (inter_type);
2447       unsigned int inter_prec = TYPE_PRECISION (inter_type);
2448       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2449       int final_int = INTEGRAL_TYPE_P (type);
2450       int final_ptr = POINTER_TYPE_P (type);
2451       int final_float = FLOAT_TYPE_P (type);
2452       int final_vec = VECTOR_TYPE_P (type);
2453       unsigned int final_prec = TYPE_PRECISION (type);
2454       int final_unsignedp = TYPE_UNSIGNED (type);
2455     }
2456    (switch
2457     /* In addition to the cases of two conversions in a row
2458        handled below, if we are converting something to its own
2459        type via an object of identical or wider precision, neither
2460        conversion is needed.  */
2461     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2462           || (GENERIC
2463               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2464          && (((inter_int || inter_ptr) && final_int)
2465              || (inter_float && final_float))
2466          && inter_prec >= final_prec)
2467      (ocvt @0))
2469     /* Likewise, if the intermediate and initial types are either both
2470        float or both integer, we don't need the middle conversion if the
2471        former is wider than the latter and doesn't change the signedness
2472        (for integers).  Avoid this if the final type is a pointer since
2473        then we sometimes need the middle conversion.  */
2474     (if (((inter_int && inside_int) || (inter_float && inside_float))
2475          && (final_int || final_float)
2476          && inter_prec >= inside_prec
2477          && (inter_float || inter_unsignedp == inside_unsignedp))
2478      (ocvt @0))
2480     /* If we have a sign-extension of a zero-extended value, we can
2481        replace that by a single zero-extension.  Likewise if the
2482        final conversion does not change precision we can drop the
2483        intermediate conversion.  */
2484     (if (inside_int && inter_int && final_int
2485          && ((inside_prec < inter_prec && inter_prec < final_prec
2486               && inside_unsignedp && !inter_unsignedp)
2487              || final_prec == inter_prec))
2488      (ocvt @0))
2490     /* Two conversions in a row are not needed unless:
2491         - some conversion is floating-point (overstrict for now), or
2492         - some conversion is a vector (overstrict for now), or
2493         - the intermediate type is narrower than both initial and
2494           final, or
2495         - the intermediate type and innermost type differ in signedness,
2496           and the outermost type is wider than the intermediate, or
2497         - the initial type is a pointer type and the precisions of the
2498           intermediate and final types differ, or
2499         - the final type is a pointer type and the precisions of the
2500           initial and intermediate types differ.  */
2501     (if (! inside_float && ! inter_float && ! final_float
2502          && ! inside_vec && ! inter_vec && ! final_vec
2503          && (inter_prec >= inside_prec || inter_prec >= final_prec)
2504          && ! (inside_int && inter_int
2505                && inter_unsignedp != inside_unsignedp
2506                && inter_prec < final_prec)
2507          && ((inter_unsignedp && inter_prec > inside_prec)
2508              == (final_unsignedp && final_prec > inter_prec))
2509          && ! (inside_ptr && inter_prec != final_prec)
2510          && ! (final_ptr && inside_prec != inter_prec))
2511      (ocvt @0))
2513     /* A truncation to an unsigned type (a zero-extension) should be
2514        canonicalized as bitwise and of a mask.  */
2515     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
2516          && final_int && inter_int && inside_int
2517          && final_prec == inside_prec
2518          && final_prec > inter_prec
2519          && inter_unsignedp)
2520      (convert (bit_and @0 { wide_int_to_tree
2521                               (inside_type,
2522                                wi::mask (inter_prec, false,
2523                                          TYPE_PRECISION (inside_type))); })))
2525     /* If we are converting an integer to a floating-point that can
2526        represent it exactly and back to an integer, we can skip the
2527        floating-point conversion.  */
2528     (if (GIMPLE /* PR66211 */
2529          && inside_int && inter_float && final_int &&
2530          (unsigned) significand_size (TYPE_MODE (inter_type))
2531          >= inside_prec - !inside_unsignedp)
2532      (convert @0)))))))
2534 /* If we have a narrowing conversion to an integral type that is fed by a
2535    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2536    masks off bits outside the final type (and nothing else).  */
2537 (simplify
2538   (convert (bit_and @0 INTEGER_CST@1))
2539   (if (INTEGRAL_TYPE_P (type)
2540        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2541        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2542        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2543                                                     TYPE_PRECISION (type)), 0))
2544    (convert @0)))
2547 /* (X /[ex] A) * A -> X.  */
2548 (simplify
2549   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2550   (convert @0))
2552 /* Canonicalization of binary operations.  */
2554 /* Convert X + -C into X - C.  */
2555 (simplify
2556  (plus @0 REAL_CST@1)
2557  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2558   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2559    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2560     (minus @0 { tem; })))))
2562 /* Convert x+x into x*2.  */
2563 (simplify
2564  (plus @0 @0)
2565  (if (SCALAR_FLOAT_TYPE_P (type))
2566   (mult @0 { build_real (type, dconst2); })
2567   (if (INTEGRAL_TYPE_P (type))
2568    (mult @0 { build_int_cst (type, 2); }))))
2570 /* 0 - X  ->  -X.  */
2571 (simplify
2572  (minus integer_zerop @1)
2573  (negate @1))
2574 (simplify
2575  (pointer_diff integer_zerop @1)
2576  (negate (convert @1)))
2578 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
2579    ARG0 is zero and X + ARG0 reduces to X, since that would mean
2580    (-ARG1 + ARG0) reduces to -ARG1.  */
2581 (simplify
2582  (minus real_zerop@0 @1)
2583  (if (fold_real_zero_addition_p (type, @0, 0))
2584   (negate @1)))
2586 /* Transform x * -1 into -x.  */
2587 (simplify
2588  (mult @0 integer_minus_onep)
2589  (negate @0))
2591 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
2592    signed overflow for CST != 0 && CST != -1.  */
2593 (simplify
2594  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2595  (if (TREE_CODE (@2) != INTEGER_CST
2596       && single_use (@3)
2597       && !integer_zerop (@1) && !integer_minus_onep (@1))
2598   (mult (mult @0 @2) @1)))
2600 /* True if we can easily extract the real and imaginary parts of a complex
2601    number.  */
2602 (match compositional_complex
2603  (convert? (complex @0 @1)))
2605 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
2606 (simplify
2607  (complex (realpart @0) (imagpart @0))
2608  @0)
2609 (simplify
2610  (realpart (complex @0 @1))
2611  @0)
2612 (simplify
2613  (imagpart (complex @0 @1))
2614  @1)
2616 /* Sometimes we only care about half of a complex expression.  */
2617 (simplify
2618  (realpart (convert?:s (conj:s @0)))
2619  (convert (realpart @0)))
2620 (simplify
2621  (imagpart (convert?:s (conj:s @0)))
2622  (convert (negate (imagpart @0))))
2623 (for part (realpart imagpart)
2624  (for op (plus minus)
2625   (simplify
2626    (part (convert?:s@2 (op:s @0 @1)))
2627    (convert (op (part @0) (part @1))))))
2628 (simplify
2629  (realpart (convert?:s (CEXPI:s @0)))
2630  (convert (COS @0)))
2631 (simplify
2632  (imagpart (convert?:s (CEXPI:s @0)))
2633  (convert (SIN @0)))
2635 /* conj(conj(x)) -> x  */
2636 (simplify
2637  (conj (convert? (conj @0)))
2638  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2639   (convert @0)))
2641 /* conj({x,y}) -> {x,-y}  */
2642 (simplify
2643  (conj (convert?:s (complex:s @0 @1)))
2644  (with { tree itype = TREE_TYPE (type); }
2645   (complex (convert:itype @0) (negate (convert:itype @1)))))
2647 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
2648 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2649  (simplify
2650   (bswap (bswap @0))
2651   @0)
2652  (simplify
2653   (bswap (bit_not (bswap @0)))
2654   (bit_not @0))
2655  (for bitop (bit_xor bit_ior bit_and)
2656   (simplify
2657    (bswap (bitop:c (bswap @0) @1))
2658    (bitop @0 (bswap @1)))))
2661 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
2663 /* Simplify constant conditions.
2664    Only optimize constant conditions when the selected branch
2665    has the same type as the COND_EXPR.  This avoids optimizing
2666    away "c ? x : throw", where the throw has a void type.
2667    Note that we cannot throw away the fold-const.c variant nor
2668    this one as we depend on doing this transform before possibly
2669    A ? B : B -> B triggers and the fold-const.c one can optimize
2670    0 ? A : B to B even if A has side-effects.  Something
2671    genmatch cannot handle.  */
2672 (simplify
2673  (cond INTEGER_CST@0 @1 @2)
2674  (if (integer_zerop (@0))
2675   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2676    @2)
2677   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2678    @1)))
2679 (simplify
2680  (vec_cond VECTOR_CST@0 @1 @2)
2681  (if (integer_all_onesp (@0))
2682   @1
2683   (if (integer_zerop (@0))
2684    @2)))
2686 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
2687    be extended.  */
2688 /* This pattern implements two kinds simplification:
2690    Case 1)
2691    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2692      1) Conversions are type widening from smaller type.
2693      2) Const c1 equals to c2 after canonicalizing comparison.
2694      3) Comparison has tree code LT, LE, GT or GE.
2695    This specific pattern is needed when (cmp (convert x) c) may not
2696    be simplified by comparison patterns because of multiple uses of
2697    x.  It also makes sense here because simplifying across multiple
2698    referred var is always benefitial for complicated cases.
2700    Case 2)
2701    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
2702 (for cmp (lt le gt ge eq)
2703  (simplify
2704   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2705   (with
2706    {
2707      tree from_type = TREE_TYPE (@1);
2708      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2709      enum tree_code code = ERROR_MARK;
2711      if (INTEGRAL_TYPE_P (from_type)
2712          && int_fits_type_p (@2, from_type)
2713          && (types_match (c1_type, from_type)
2714              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2715                  && (TYPE_UNSIGNED (from_type)
2716                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2717          && (types_match (c2_type, from_type)
2718              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2719                  && (TYPE_UNSIGNED (from_type)
2720                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2721        {
2722          if (cmp != EQ_EXPR)
2723            {
2724              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2725                {
2726                  /* X <= Y - 1 equals to X < Y.  */
2727                  if (cmp == LE_EXPR)
2728                    code = LT_EXPR;
2729                  /* X > Y - 1 equals to X >= Y.  */
2730                  if (cmp == GT_EXPR)
2731                    code = GE_EXPR;
2732                }
2733              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2734                {
2735                  /* X < Y + 1 equals to X <= Y.  */
2736                  if (cmp == LT_EXPR)
2737                    code = LE_EXPR;
2738                  /* X >= Y + 1 equals to X > Y.  */
2739                  if (cmp == GE_EXPR)
2740                    code = GT_EXPR;
2741                }
2742              if (code != ERROR_MARK
2743                  || wi::to_widest (@2) == wi::to_widest (@3))
2744                {
2745                  if (cmp == LT_EXPR || cmp == LE_EXPR)
2746                    code = MIN_EXPR;
2747                  if (cmp == GT_EXPR || cmp == GE_EXPR)
2748                    code = MAX_EXPR;
2749                }
2750            }
2751          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
2752          else if (int_fits_type_p (@3, from_type))
2753            code = EQ_EXPR;
2754        }
2755    }
2756    (if (code == MAX_EXPR)
2757     (convert (max @1 (convert @2)))
2758     (if (code == MIN_EXPR)
2759      (convert (min @1 (convert @2)))
2760      (if (code == EQ_EXPR)
2761       (convert (cond (eq @1 (convert @3))
2762                      (convert:from_type @3) (convert:from_type @2)))))))))
2764 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2766      1) OP is PLUS or MINUS.
2767      2) CMP is LT, LE, GT or GE.
2768      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2770    This pattern also handles special cases like:
2772      A) Operand x is a unsigned to signed type conversion and c1 is
2773         integer zero.  In this case,
2774           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
2775           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
2776      B) Const c1 may not equal to (C3 op' C2).  In this case we also
2777         check equality for (c1+1) and (c1-1) by adjusting comparison
2778         code.
2780    TODO: Though signed type is handled by this pattern, it cannot be
2781    simplified at the moment because C standard requires additional
2782    type promotion.  In order to match&simplify it here, the IR needs
2783    to be cleaned up by other optimizers, i.e, VRP.  */
2784 (for op (plus minus)
2785  (for cmp (lt le gt ge)
2786   (simplify
2787    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2788    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2789     (if (types_match (from_type, to_type)
2790          /* Check if it is special case A).  */
2791          || (TYPE_UNSIGNED (from_type)
2792              && !TYPE_UNSIGNED (to_type)
2793              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2794              && integer_zerop (@1)
2795              && (cmp == LT_EXPR || cmp == GE_EXPR)))
2796      (with
2797       {
2798         bool overflow = false;
2799         enum tree_code code, cmp_code = cmp;
2800         wide_int real_c1;
2801         wide_int c1 = wi::to_wide (@1);
2802         wide_int c2 = wi::to_wide (@2);
2803         wide_int c3 = wi::to_wide (@3);
2804         signop sgn = TYPE_SIGN (from_type);
2806         /* Handle special case A), given x of unsigned type:
2807             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
2808             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
2809         if (!types_match (from_type, to_type))
2810           {
2811             if (cmp_code == LT_EXPR)
2812               cmp_code = GT_EXPR;
2813             if (cmp_code == GE_EXPR)
2814               cmp_code = LE_EXPR;
2815             c1 = wi::max_value (to_type);
2816           }
2817         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
2818            compute (c3 op' c2) and check if it equals to c1 with op' being
2819            the inverted operator of op.  Make sure overflow doesn't happen
2820            if it is undefined.  */
2821         if (op == PLUS_EXPR)
2822           real_c1 = wi::sub (c3, c2, sgn, &overflow);
2823         else
2824           real_c1 = wi::add (c3, c2, sgn, &overflow);
2826         code = cmp_code;
2827         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2828           {
2829             /* Check if c1 equals to real_c1.  Boundary condition is handled
2830                by adjusting comparison operation if necessary.  */
2831             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2832                 && !overflow)
2833               {
2834                 /* X <= Y - 1 equals to X < Y.  */
2835                 if (cmp_code == LE_EXPR)
2836                   code = LT_EXPR;
2837                 /* X > Y - 1 equals to X >= Y.  */
2838                 if (cmp_code == GT_EXPR)
2839                   code = GE_EXPR;
2840               }
2841             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2842                 && !overflow)
2843               {
2844                 /* X < Y + 1 equals to X <= Y.  */
2845                 if (cmp_code == LT_EXPR)
2846                   code = LE_EXPR;
2847                 /* X >= Y + 1 equals to X > Y.  */
2848                 if (cmp_code == GE_EXPR)
2849                   code = GT_EXPR;
2850               }
2851             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2852               {
2853                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2854                   code = MIN_EXPR;
2855                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2856                   code = MAX_EXPR;
2857               }
2858           }
2859       }
2860       (if (code == MAX_EXPR)
2861        (op (max @X { wide_int_to_tree (from_type, real_c1); })
2862            { wide_int_to_tree (from_type, c2); })
2863        (if (code == MIN_EXPR)
2864         (op (min @X { wide_int_to_tree (from_type, real_c1); })
2865             { wide_int_to_tree (from_type, c2); })))))))))
2867 (for cnd (cond vec_cond)
2868  /* A ? B : (A ? X : C) -> A ? B : C.  */
2869  (simplify
2870   (cnd @0 (cnd @0 @1 @2) @3)
2871   (cnd @0 @1 @3))
2872  (simplify
2873   (cnd @0 @1 (cnd @0 @2 @3))
2874   (cnd @0 @1 @3))
2875  /* A ? B : (!A ? C : X) -> A ? B : C.  */
2876  /* ???  This matches embedded conditions open-coded because genmatch
2877     would generate matching code for conditions in separate stmts only.
2878     The following is still important to merge then and else arm cases
2879     from if-conversion.  */
2880  (simplify
2881   (cnd @0 @1 (cnd @2 @3 @4))
2882   (if (COMPARISON_CLASS_P (@0)
2883        && COMPARISON_CLASS_P (@2)
2884        && invert_tree_comparison
2885            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2886        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2887        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2888    (cnd @0 @1 @3)))
2889  (simplify
2890   (cnd @0 (cnd @1 @2 @3) @4)
2891   (if (COMPARISON_CLASS_P (@0)
2892        && COMPARISON_CLASS_P (@1)
2893        && invert_tree_comparison
2894            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2895        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2896        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2897    (cnd @0 @3 @4)))
2899  /* A ? B : B -> B.  */
2900  (simplify
2901   (cnd @0 @1 @1)
2902   @1)
2904  /* !A ? B : C -> A ? C : B.  */
2905  (simplify
2906   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2907   (cnd @0 @2 @1)))
2909 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2910    return all -1 or all 0 results.  */
2911 /* ??? We could instead convert all instances of the vec_cond to negate,
2912    but that isn't necessarily a win on its own.  */
2913 (simplify
2914  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2915  (if (VECTOR_TYPE_P (type)
2916       && known_eq (TYPE_VECTOR_SUBPARTS (type),
2917                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2918       && (TYPE_MODE (TREE_TYPE (type))
2919           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2920   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2922 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
2923 (simplify
2924  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2925  (if (VECTOR_TYPE_P (type)
2926       && known_eq (TYPE_VECTOR_SUBPARTS (type),
2927                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2928       && (TYPE_MODE (TREE_TYPE (type))
2929           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2930   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2933 /* Simplifications of comparisons.  */
2935 /* See if we can reduce the magnitude of a constant involved in a
2936    comparison by changing the comparison code.  This is a canonicalization
2937    formerly done by maybe_canonicalize_comparison_1.  */
2938 (for cmp  (le gt)
2939      acmp (lt ge)
2940  (simplify
2941   (cmp @0 INTEGER_CST@1)
2942   (if (tree_int_cst_sgn (@1) == -1)
2943    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2944 (for cmp  (ge lt)
2945      acmp (gt le)
2946  (simplify
2947   (cmp @0 INTEGER_CST@1)
2948   (if (tree_int_cst_sgn (@1) == 1)
2949    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2952 /* We can simplify a logical negation of a comparison to the
2953    inverted comparison.  As we cannot compute an expression
2954    operator using invert_tree_comparison we have to simulate
2955    that with expression code iteration.  */
2956 (for cmp (tcc_comparison)
2957      icmp (inverted_tcc_comparison)
2958      ncmp (inverted_tcc_comparison_with_nans)
2959  /* Ideally we'd like to combine the following two patterns
2960     and handle some more cases by using
2961       (logical_inverted_value (cmp @0 @1))
2962     here but for that genmatch would need to "inline" that.
2963     For now implement what forward_propagate_comparison did.  */
2964  (simplify
2965   (bit_not (cmp @0 @1))
2966   (if (VECTOR_TYPE_P (type)
2967        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2968    /* Comparison inversion may be impossible for trapping math,
2969       invert_tree_comparison will tell us.  But we can't use
2970       a computed operator in the replacement tree thus we have
2971       to play the trick below.  */
2972    (with { enum tree_code ic = invert_tree_comparison
2973              (cmp, HONOR_NANS (@0)); }
2974     (if (ic == icmp)
2975      (icmp @0 @1)
2976      (if (ic == ncmp)
2977       (ncmp @0 @1))))))
2978  (simplify
2979   (bit_xor (cmp @0 @1) integer_truep)
2980   (with { enum tree_code ic = invert_tree_comparison
2981             (cmp, HONOR_NANS (@0)); }
2982    (if (ic == icmp)
2983     (icmp @0 @1)
2984     (if (ic == ncmp)
2985      (ncmp @0 @1))))))
2987 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2988    ??? The transformation is valid for the other operators if overflow
2989    is undefined for the type, but performing it here badly interacts
2990    with the transformation in fold_cond_expr_with_comparison which
2991    attempts to synthetize ABS_EXPR.  */
2992 (for cmp (eq ne)
2993  (for sub (minus pointer_diff)
2994   (simplify
2995    (cmp (sub@2 @0 @1) integer_zerop)
2996    (if (single_use (@2))
2997     (cmp @0 @1)))))
2999 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3000    signed arithmetic case.  That form is created by the compiler
3001    often enough for folding it to be of value.  One example is in
3002    computing loop trip counts after Operator Strength Reduction.  */
3003 (for cmp (simple_comparison)
3004      scmp (swapped_simple_comparison)
3005  (simplify
3006   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3007   /* Handle unfolded multiplication by zero.  */
3008   (if (integer_zerop (@1))
3009    (cmp @1 @2)
3010    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3011         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3012         && single_use (@3))
3013     /* If @1 is negative we swap the sense of the comparison.  */
3014     (if (tree_int_cst_sgn (@1) < 0)
3015      (scmp @0 @2)
3016      (cmp @0 @2))))))
3018 /* Simplify comparison of something with itself.  For IEEE
3019    floating-point, we can only do some of these simplifications.  */
3020 (for cmp (eq ge le)
3021  (simplify
3022   (cmp @0 @0)
3023   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3024        || ! HONOR_NANS (@0))
3025    { constant_boolean_node (true, type); }
3026    (if (cmp != EQ_EXPR)
3027     (eq @0 @0)))))
3028 (for cmp (ne gt lt)
3029  (simplify
3030   (cmp @0 @0)
3031   (if (cmp != NE_EXPR
3032        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3033        || ! HONOR_NANS (@0))
3034    { constant_boolean_node (false, type); })))
3035 (for cmp (unle unge uneq)
3036  (simplify
3037   (cmp @0 @0)
3038   { constant_boolean_node (true, type); }))
3039 (for cmp (unlt ungt)
3040  (simplify
3041   (cmp @0 @0)
3042   (unordered @0 @0)))
3043 (simplify
3044  (ltgt @0 @0)
3045  (if (!flag_trapping_math)
3046   { constant_boolean_node (false, type); }))
3048 /* Fold ~X op ~Y as Y op X.  */
3049 (for cmp (simple_comparison)
3050  (simplify
3051   (cmp (bit_not@2 @0) (bit_not@3 @1))
3052   (if (single_use (@2) && single_use (@3))
3053    (cmp @1 @0))))
3055 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
3056 (for cmp (simple_comparison)
3057      scmp (swapped_simple_comparison)
3058  (simplify
3059   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3060   (if (single_use (@2)
3061        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3062    (scmp @0 (bit_not @1)))))
3064 (for cmp (simple_comparison)
3065  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
3066  (simplify
3067   (cmp (convert@2 @0) (convert? @1))
3068   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3069        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3070            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3071        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3072            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3073    (with
3074     {
3075       tree type1 = TREE_TYPE (@1);
3076       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3077         {
3078           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3079           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3080               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3081             type1 = float_type_node;
3082           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3083               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3084             type1 = double_type_node;
3085         }
3086       tree newtype
3087         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3088            ? TREE_TYPE (@0) : type1); 
3089     }
3090     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3091      (cmp (convert:newtype @0) (convert:newtype @1))))))
3093  (simplify
3094   (cmp @0 REAL_CST@1)
3095   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
3096   (switch
3097    /* a CMP (-0) -> a CMP 0  */
3098    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3099     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3100    /* x != NaN is always true, other ops are always false.  */
3101    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3102         && ! HONOR_SNANS (@1))
3103     { constant_boolean_node (cmp == NE_EXPR, type); })
3104    /* Fold comparisons against infinity.  */
3105    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3106         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3107     (with
3108      {
3109        REAL_VALUE_TYPE max;
3110        enum tree_code code = cmp;
3111        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3112        if (neg)
3113          code = swap_tree_comparison (code);
3114      }
3115      (switch
3116       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
3117       (if (code == GT_EXPR
3118            && !(HONOR_NANS (@0) && flag_trapping_math))
3119        { constant_boolean_node (false, type); })
3120       (if (code == LE_EXPR)
3121        /* x <= +Inf is always true, if we don't care about NaNs.  */
3122        (if (! HONOR_NANS (@0))
3123         { constant_boolean_node (true, type); }
3124         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3125            an "invalid" exception.  */
3126         (if (!flag_trapping_math)
3127          (eq @0 @0))))
3128       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3129          for == this introduces an exception for x a NaN.  */
3130       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3131            || code == GE_EXPR)
3132        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3133         (if (neg)
3134          (lt @0 { build_real (TREE_TYPE (@0), max); })
3135          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3136       /* x < +Inf is always equal to x <= DBL_MAX.  */
3137       (if (code == LT_EXPR)
3138        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3139         (if (neg)
3140          (ge @0 { build_real (TREE_TYPE (@0), max); })
3141          (le @0 { build_real (TREE_TYPE (@0), max); }))))
3142       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3143          an exception for x a NaN so use an unordered comparison.  */
3144       (if (code == NE_EXPR)
3145        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3146         (if (! HONOR_NANS (@0))
3147          (if (neg)
3148           (ge @0 { build_real (TREE_TYPE (@0), max); })
3149           (le @0 { build_real (TREE_TYPE (@0), max); }))
3150          (if (neg)
3151           (unge @0 { build_real (TREE_TYPE (@0), max); })
3152           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3154  /* If this is a comparison of a real constant with a PLUS_EXPR
3155     or a MINUS_EXPR of a real constant, we can convert it into a
3156     comparison with a revised real constant as long as no overflow
3157     occurs when unsafe_math_optimizations are enabled.  */
3158  (if (flag_unsafe_math_optimizations)
3159   (for op (plus minus)
3160    (simplify
3161     (cmp (op @0 REAL_CST@1) REAL_CST@2)
3162     (with
3163      {
3164        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3165                                TREE_TYPE (@1), @2, @1);
3166      }
3167      (if (tem && !TREE_OVERFLOW (tem))
3168       (cmp @0 { tem; }))))))
3170  /* Likewise, we can simplify a comparison of a real constant with
3171     a MINUS_EXPR whose first operand is also a real constant, i.e.
3172     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
3173     floating-point types only if -fassociative-math is set.  */
3174  (if (flag_associative_math)
3175   (simplify
3176    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3177    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3178     (if (tem && !TREE_OVERFLOW (tem))
3179      (cmp { tem; } @1)))))
3181  /* Fold comparisons against built-in math functions.  */
3182  (if (flag_unsafe_math_optimizations
3183       && ! flag_errno_math)
3184   (for sq (SQRT)
3185    (simplify
3186     (cmp (sq @0) REAL_CST@1)
3187     (switch
3188      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3189       (switch
3190        /* sqrt(x) < y is always false, if y is negative.  */
3191        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3192         { constant_boolean_node (false, type); })
3193        /* sqrt(x) > y is always true, if y is negative and we
3194           don't care about NaNs, i.e. negative values of x.  */
3195        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3196         { constant_boolean_node (true, type); })
3197        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
3198        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3199      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3200       (switch
3201        /* sqrt(x) < 0 is always false.  */
3202        (if (cmp == LT_EXPR)
3203         { constant_boolean_node (false, type); })
3204        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
3205        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3206         { constant_boolean_node (true, type); })
3207        /* sqrt(x) <= 0 -> x == 0.  */
3208        (if (cmp == LE_EXPR)
3209         (eq @0 @1))
3210        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
3211           == or !=.  In the last case:
3213             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3215           if x is negative or NaN.  Due to -funsafe-math-optimizations,
3216           the results for other x follow from natural arithmetic.  */
3217        (cmp @0 @1)))
3218      (if (cmp == GT_EXPR || cmp == GE_EXPR)
3219       (with
3220        {
3221          REAL_VALUE_TYPE c2;
3222          real_arithmetic (&c2, MULT_EXPR,
3223                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3224          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3225        }
3226        (if (REAL_VALUE_ISINF (c2))
3227         /* sqrt(x) > y is x == +Inf, when y is very large.  */
3228         (if (HONOR_INFINITIES (@0))
3229          (eq @0 { build_real (TREE_TYPE (@0), c2); })
3230          { constant_boolean_node (false, type); })
3231         /* sqrt(x) > c is the same as x > c*c.  */
3232         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3233      (if (cmp == LT_EXPR || cmp == LE_EXPR)
3234       (with
3235        {
3236          REAL_VALUE_TYPE c2;
3237          real_arithmetic (&c2, MULT_EXPR,
3238                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3239          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3240        }
3241        (if (REAL_VALUE_ISINF (c2))
3242         (switch
3243          /* sqrt(x) < y is always true, when y is a very large
3244             value and we don't care about NaNs or Infinities.  */
3245          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3246           { constant_boolean_node (true, type); })
3247          /* sqrt(x) < y is x != +Inf when y is very large and we
3248             don't care about NaNs.  */
3249          (if (! HONOR_NANS (@0))
3250           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3251          /* sqrt(x) < y is x >= 0 when y is very large and we
3252             don't care about Infinities.  */
3253          (if (! HONOR_INFINITIES (@0))
3254           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3255          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
3256          (if (GENERIC)
3257           (truth_andif
3258            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3259            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3260         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
3261         (if (! HONOR_NANS (@0))
3262          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3263          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
3264          (if (GENERIC)
3265           (truth_andif
3266            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3267            (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3268    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
3269    (simplify
3270     (cmp (sq @0) (sq @1))
3271       (if (! HONOR_NANS (@0))
3272         (cmp @0 @1))))))
3274 /* Optimize various special cases of (FTYPE) N CMP CST.  */
3275 (for cmp  (lt le eq ne ge gt)
3276      icmp (le le eq ne ge ge)
3277  (simplify
3278   (cmp (float @0) REAL_CST@1)
3279    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3280         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3281     (with
3282      {
3283        tree itype = TREE_TYPE (@0);
3284        signop isign = TYPE_SIGN (itype);
3285        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3286        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3287        /* Be careful to preserve any potential exceptions due to
3288           NaNs.  qNaNs are ok in == or != context.
3289           TODO: relax under -fno-trapping-math or
3290           -fno-signaling-nans.  */
3291        bool exception_p
3292          = real_isnan (cst) && (cst->signalling
3293                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3294        /* INT?_MIN is power-of-two so it takes
3295           only one mantissa bit.  */
3296        bool signed_p = isign == SIGNED;
3297        bool itype_fits_ftype_p
3298          = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3299      }
3300      /* TODO: allow non-fitting itype and SNaNs when
3301         -fno-trapping-math.  */
3302      (if (itype_fits_ftype_p && ! exception_p)
3303       (with
3304        {
3305          REAL_VALUE_TYPE imin, imax;
3306          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3307          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3309          REAL_VALUE_TYPE icst;
3310          if (cmp == GT_EXPR || cmp == GE_EXPR)
3311            real_ceil (&icst, fmt, cst);
3312          else if (cmp == LT_EXPR || cmp == LE_EXPR)
3313            real_floor (&icst, fmt, cst);
3314          else
3315            real_trunc (&icst, fmt, cst);
3317          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3319          bool overflow_p = false;
3320          wide_int icst_val
3321            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3322        }
3323        (switch
3324         /* Optimize cases when CST is outside of ITYPE's range.  */
3325         (if (real_compare (LT_EXPR, cst, &imin))
3326          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3327                                   type); })
3328         (if (real_compare (GT_EXPR, cst, &imax))
3329          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3330                                   type); })
3331         /* Remove cast if CST is an integer representable by ITYPE.  */
3332         (if (cst_int_p)
3333          (cmp @0 { gcc_assert (!overflow_p);
3334                    wide_int_to_tree (itype, icst_val); })
3335         )
3336         /* When CST is fractional, optimize
3337             (FTYPE) N == CST -> 0
3338             (FTYPE) N != CST -> 1.  */
3339         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3340          { constant_boolean_node (cmp == NE_EXPR, type); }) 
3341         /* Otherwise replace with sensible integer constant.  */
3342         (with
3343          {
3344            gcc_checking_assert (!overflow_p);
3345          }
3346          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3348 /* Fold A /[ex] B CMP C to A CMP B * C.  */
3349 (for cmp (eq ne)
3350  (simplify
3351   (cmp (exact_div @0 @1) INTEGER_CST@2)
3352   (if (!integer_zerop (@1))
3353    (if (wi::to_wide (@2) == 0)
3354     (cmp @0 @2)
3355     (if (TREE_CODE (@1) == INTEGER_CST)
3356      (with
3357       {
3358         bool ovf;
3359         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3360                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3361       }
3362       (if (ovf)
3363        { constant_boolean_node (cmp == NE_EXPR, type); }
3364        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3365 (for cmp (lt le gt ge)
3366  (simplify
3367   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3368   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3369    (with
3370     {
3371       bool ovf;
3372       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3373                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3374     }
3375     (if (ovf)
3376      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3377                                         TYPE_SIGN (TREE_TYPE (@2)))
3378                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3379      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3381 /* Unordered tests if either argument is a NaN.  */
3382 (simplify
3383  (bit_ior (unordered @0 @0) (unordered @1 @1))
3384  (if (types_match (@0, @1))
3385   (unordered @0 @1)))
3386 (simplify
3387  (bit_and (ordered @0 @0) (ordered @1 @1))
3388  (if (types_match (@0, @1))
3389   (ordered @0 @1)))
3390 (simplify
3391  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3392  @2)
3393 (simplify
3394  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3395  @2)
3397 /* Simple range test simplifications.  */
3398 /* A < B || A >= B -> true.  */
3399 (for test1 (lt le le le ne ge)
3400      test2 (ge gt ge ne eq ne)
3401  (simplify
3402   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3403   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3404        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3405    { constant_boolean_node (true, type); })))
3406 /* A < B && A >= B -> false.  */
3407 (for test1 (lt lt lt le ne eq)
3408      test2 (ge gt eq gt eq gt)
3409  (simplify
3410   (bit_and:c (test1 @0 @1) (test2 @0 @1))
3411   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3412        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3413    { constant_boolean_node (false, type); })))
3415 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3416    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
3418    Note that comparisons
3419      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
3420      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
3421    will be canonicalized to above so there's no need to
3422    consider them here.
3423  */
3425 (for cmp (le gt)
3426      eqcmp (eq ne)
3427  (simplify
3428   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3429   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3430    (with
3431     {
3432      tree ty = TREE_TYPE (@0);
3433      unsigned prec = TYPE_PRECISION (ty);
3434      wide_int mask = wi::to_wide (@2, prec);
3435      wide_int rhs = wi::to_wide (@3, prec);
3436      signop sgn = TYPE_SIGN (ty);
3437     }
3438     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3439          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3440       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3441              { build_zero_cst (ty); }))))))
3443 /* -A CMP -B -> B CMP A.  */
3444 (for cmp (tcc_comparison)
3445      scmp (swapped_tcc_comparison)
3446  (simplify
3447   (cmp (negate @0) (negate @1))
3448   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3449        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3450            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3451    (scmp @0 @1)))
3452  (simplify
3453   (cmp (negate @0) CONSTANT_CLASS_P@1)
3454   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3455        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3456            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3457    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3458     (if (tem && !TREE_OVERFLOW (tem))
3459      (scmp @0 { tem; }))))))
3461 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
3462 (for op (eq ne)
3463  (simplify
3464   (op (abs @0) zerop@1)
3465   (op @0 @1)))
3467 /* From fold_sign_changed_comparison and fold_widened_comparison.
3468    FIXME: the lack of symmetry is disturbing.  */
3469 (for cmp (simple_comparison)
3470  (simplify
3471   (cmp (convert@0 @00) (convert?@1 @10))
3472   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3473        /* Disable this optimization if we're casting a function pointer
3474           type on targets that require function pointer canonicalization.  */
3475        && !(targetm.have_canonicalize_funcptr_for_compare ()
3476             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3477             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3478        && single_use (@0))
3479    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3480         && (TREE_CODE (@10) == INTEGER_CST
3481             || @1 != @10)
3482         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3483             || cmp == NE_EXPR
3484             || cmp == EQ_EXPR)
3485         && !POINTER_TYPE_P (TREE_TYPE (@00)))
3486     /* ???  The special-casing of INTEGER_CST conversion was in the original
3487        code and here to avoid a spurious overflow flag on the resulting
3488        constant which fold_convert produces.  */
3489     (if (TREE_CODE (@1) == INTEGER_CST)
3490      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3491                                 TREE_OVERFLOW (@1)); })
3492      (cmp @00 (convert @1)))
3494     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3495      /* If possible, express the comparison in the shorter mode.  */
3496      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3497            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3498            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3499                && TYPE_UNSIGNED (TREE_TYPE (@00))))
3500           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3501               || ((TYPE_PRECISION (TREE_TYPE (@00))
3502                    >= TYPE_PRECISION (TREE_TYPE (@10)))
3503                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
3504                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
3505               || (TREE_CODE (@10) == INTEGER_CST
3506                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3507                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
3508       (cmp @00 (convert @10))
3509       (if (TREE_CODE (@10) == INTEGER_CST
3510            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3511            && !int_fits_type_p (@10, TREE_TYPE (@00)))
3512        (with
3513         {
3514           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3515           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3516           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3517           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3518         }
3519         (if (above || below)
3520          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3521           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3522           (if (cmp == LT_EXPR || cmp == LE_EXPR)
3523            { constant_boolean_node (above ? true : false, type); }
3524            (if (cmp == GT_EXPR || cmp == GE_EXPR)
3525             { constant_boolean_node (above ? false : true, type); }))))))))))))
3527 (for cmp (eq ne)
3528  /* A local variable can never be pointed to by
3529     the default SSA name of an incoming parameter.
3530     SSA names are canonicalized to 2nd place.  */
3531  (simplify
3532   (cmp addr@0 SSA_NAME@1)
3533   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3534        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3535    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3536     (if (TREE_CODE (base) == VAR_DECL
3537          && auto_var_in_fn_p (base, current_function_decl))
3538      (if (cmp == NE_EXPR)
3539       { constant_boolean_node (true, type); }
3540       { constant_boolean_node (false, type); }))))))
3542 /* Equality compare simplifications from fold_binary  */
3543 (for cmp (eq ne)
3545  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3546     Similarly for NE_EXPR.  */
3547  (simplify
3548   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3549   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3550        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3551    { constant_boolean_node (cmp == NE_EXPR, type); }))
3553  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
3554  (simplify
3555   (cmp (bit_xor @0 @1) integer_zerop)
3556   (cmp @0 @1))
3558  /* (X ^ Y) == Y becomes X == 0.
3559     Likewise (X ^ Y) == X becomes Y == 0.  */
3560  (simplify
3561   (cmp:c (bit_xor:c @0 @1) @0)
3562   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3564  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
3565  (simplify
3566   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3567   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3568    (cmp @0 (bit_xor @1 (convert @2)))))
3570  (simplify
3571   (cmp (convert? addr@0) integer_zerop)
3572   (if (tree_single_nonzero_warnv_p (@0, NULL))
3573    { constant_boolean_node (cmp == NE_EXPR, type); })))
3575 /* If we have (A & C) == C where C is a power of 2, convert this into
3576    (A & C) != 0.  Similarly for NE_EXPR.  */
3577 (for cmp (eq ne)
3578      icmp (ne eq)
3579  (simplify
3580   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3581   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3583 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3584    convert this into a shift followed by ANDing with D.  */
3585 (simplify
3586  (cond
3587   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3588   INTEGER_CST@2 integer_zerop)
3589  (if (integer_pow2p (@2))
3590   (with {
3591      int shift = (wi::exact_log2 (wi::to_wide (@2))
3592                   - wi::exact_log2 (wi::to_wide (@1)));
3593    }
3594    (if (shift > 0)
3595     (bit_and
3596      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3597     (bit_and
3598      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3599      @2)))))
3601 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3602    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
3603 (for cmp (eq ne)
3604      ncmp (ge lt)
3605  (simplify
3606   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3607   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3608        && type_has_mode_precision_p (TREE_TYPE (@0))
3609        && element_precision (@2) >= element_precision (@0)
3610        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3611    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3612     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3614 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3615    this into a right shift or sign extension followed by ANDing with C.  */
3616 (simplify
3617  (cond
3618   (lt @0 integer_zerop)
3619   INTEGER_CST@1 integer_zerop)
3620  (if (integer_pow2p (@1)
3621       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3622   (with {
3623     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3624    }
3625    (if (shift >= 0)
3626     (bit_and
3627      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3628      @1)
3629     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3630        sign extension followed by AND with C will achieve the effect.  */
3631     (bit_and (convert @0) @1)))))
3633 /* When the addresses are not directly of decls compare base and offset.
3634    This implements some remaining parts of fold_comparison address
3635    comparisons but still no complete part of it.  Still it is good
3636    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
3637 (for cmp (simple_comparison)
3638  (simplify
3639   (cmp (convert1?@2 addr@0) (convert2? addr@1))
3640   (with
3641    {
3642      poly_int64 off0, off1;
3643      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3644      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3645      if (base0 && TREE_CODE (base0) == MEM_REF)
3646        {
3647          off0 += mem_ref_offset (base0).force_shwi ();
3648          base0 = TREE_OPERAND (base0, 0);
3649        }
3650      if (base1 && TREE_CODE (base1) == MEM_REF)
3651        {
3652          off1 += mem_ref_offset (base1).force_shwi ();
3653          base1 = TREE_OPERAND (base1, 0);
3654        }
3655    }
3656    (if (base0 && base1)
3657     (with
3658      {
3659        int equal = 2;
3660        /* Punt in GENERIC on variables with value expressions;
3661           the value expressions might point to fields/elements
3662           of other vars etc.  */
3663        if (GENERIC
3664            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3665                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3666          ;
3667        else if (decl_in_symtab_p (base0)
3668                 && decl_in_symtab_p (base1))
3669          equal = symtab_node::get_create (base0)
3670                    ->equal_address_to (symtab_node::get_create (base1));
3671        else if ((DECL_P (base0)
3672                  || TREE_CODE (base0) == SSA_NAME
3673                  || TREE_CODE (base0) == STRING_CST)
3674                 && (DECL_P (base1)
3675                     || TREE_CODE (base1) == SSA_NAME
3676                     || TREE_CODE (base1) == STRING_CST))
3677          equal = (base0 == base1);
3678      }
3679      (if (equal == 1
3680           && (cmp == EQ_EXPR || cmp == NE_EXPR
3681               /* If the offsets are equal we can ignore overflow.  */
3682               || known_eq (off0, off1)
3683               || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3684                  /* Or if we compare using pointers to decls or strings.  */
3685               || (POINTER_TYPE_P (TREE_TYPE (@2))
3686                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3687       (switch
3688        (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3689         { constant_boolean_node (known_eq (off0, off1), type); })
3690        (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3691         { constant_boolean_node (known_ne (off0, off1), type); })
3692        (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3693         { constant_boolean_node (known_lt (off0, off1), type); })
3694        (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3695         { constant_boolean_node (known_le (off0, off1), type); })
3696        (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3697         { constant_boolean_node (known_ge (off0, off1), type); })
3698        (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3699         { constant_boolean_node (known_gt (off0, off1), type); }))
3700       (if (equal == 0
3701            && DECL_P (base0) && DECL_P (base1)
3702            /* If we compare this as integers require equal offset.  */
3703            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3704                || known_eq (off0, off1)))
3705        (switch
3706         (if (cmp == EQ_EXPR)
3707          { constant_boolean_node (false, type); })
3708         (if (cmp == NE_EXPR)
3709          { constant_boolean_node (true, type); })))))))))
3711 /* Simplify pointer equality compares using PTA.  */
3712 (for neeq (ne eq)
3713  (simplify
3714   (neeq @0 @1)
3715   (if (POINTER_TYPE_P (TREE_TYPE (@0))
3716        && ptrs_compare_unequal (@0, @1))
3717    { constant_boolean_node (neeq != EQ_EXPR, type); })))
3719 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3720    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3721    Disable the transform if either operand is pointer to function.
3722    This broke pr22051-2.c for arm where function pointer
3723    canonicalizaion is not wanted.  */
3725 (for cmp (ne eq)
3726  (simplify
3727   (cmp (convert @0) INTEGER_CST@1)
3728   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3729          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3730          && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3731         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3732             && POINTER_TYPE_P (TREE_TYPE (@1))
3733             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3734        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3735    (cmp @0 (convert @1)))))
3737 /* Non-equality compare simplifications from fold_binary  */
3738 (for cmp (lt gt le ge)
3739  /* Comparisons with the highest or lowest possible integer of
3740     the specified precision will have known values.  */
3741  (simplify
3742   (cmp (convert?@2 @0) INTEGER_CST@1)
3743   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3744        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3745    (with
3746     {
3747       tree arg1_type = TREE_TYPE (@1);
3748       unsigned int prec = TYPE_PRECISION (arg1_type);
3749       wide_int max = wi::max_value (arg1_type);
3750       wide_int signed_max = wi::max_value (prec, SIGNED);
3751       wide_int min = wi::min_value (arg1_type);
3752     }
3753     (switch
3754      (if (wi::to_wide (@1) == max)
3755       (switch
3756        (if (cmp == GT_EXPR)
3757         { constant_boolean_node (false, type); })
3758        (if (cmp == GE_EXPR)
3759         (eq @2 @1))
3760        (if (cmp == LE_EXPR)
3761         { constant_boolean_node (true, type); })
3762        (if (cmp == LT_EXPR)
3763         (ne @2 @1))))
3764      (if (wi::to_wide (@1) == min)
3765       (switch
3766        (if (cmp == LT_EXPR)
3767         { constant_boolean_node (false, type); })
3768        (if (cmp == LE_EXPR)
3769         (eq @2 @1))
3770        (if (cmp == GE_EXPR)
3771         { constant_boolean_node (true, type); })
3772        (if (cmp == GT_EXPR)
3773         (ne @2 @1))))
3774      (if (wi::to_wide (@1) == max - 1)
3775       (switch
3776        (if (cmp == GT_EXPR)
3777         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3778        (if (cmp == LE_EXPR)
3779         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3780      (if (wi::to_wide (@1) == min + 1)
3781       (switch
3782        (if (cmp == GE_EXPR)
3783         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3784        (if (cmp == LT_EXPR)
3785         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3786      (if (wi::to_wide (@1) == signed_max
3787           && TYPE_UNSIGNED (arg1_type)
3788           /* We will flip the signedness of the comparison operator
3789              associated with the mode of @1, so the sign bit is
3790              specified by this mode.  Check that @1 is the signed
3791              max associated with this sign bit.  */
3792           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3793           /* signed_type does not work on pointer types.  */
3794           && INTEGRAL_TYPE_P (arg1_type))
3795       /* The following case also applies to X < signed_max+1
3796          and X >= signed_max+1 because previous transformations.  */
3797       (if (cmp == LE_EXPR || cmp == GT_EXPR)
3798        (with { tree st = signed_type_for (arg1_type); }
3799         (if (cmp == LE_EXPR)
3800          (ge (convert:st @0) { build_zero_cst (st); })
3801          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3803 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3804  /* If the second operand is NaN, the result is constant.  */
3805  (simplify
3806   (cmp @0 REAL_CST@1)
3807   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3808        && (cmp != LTGT_EXPR || ! flag_trapping_math))
3809    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3810                             ? false : true, type); })))
3812 /* bool_var != 0 becomes bool_var.  */
3813 (simplify
3814  (ne @0 integer_zerop)
3815  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3816       && types_match (type, TREE_TYPE (@0)))
3817   (non_lvalue @0)))
3818 /* bool_var == 1 becomes bool_var.  */
3819 (simplify
3820  (eq @0 integer_onep)
3821  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3822       && types_match (type, TREE_TYPE (@0)))
3823   (non_lvalue @0)))
3824 /* Do not handle
3825    bool_var == 0 becomes !bool_var or
3826    bool_var != 1 becomes !bool_var
3827    here because that only is good in assignment context as long
3828    as we require a tcc_comparison in GIMPLE_CONDs where we'd
3829    replace if (x == 0) with tem = ~x; if (tem != 0) which is
3830    clearly less optimal and which we'll transform again in forwprop.  */
3832 /* When one argument is a constant, overflow detection can be simplified.
3833    Currently restricted to single use so as not to interfere too much with
3834    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3835    A + CST CMP A  ->  A CMP' CST' */
3836 (for cmp (lt le ge gt)
3837      out (gt gt le le)
3838  (simplify
3839   (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3840   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3841        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3842        && wi::to_wide (@1) != 0
3843        && single_use (@2))
3844    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3845     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3846                                 wi::max_value (prec, UNSIGNED)
3847                                 - wi::to_wide (@1)); })))))
3849 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3850    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3851    expects the long form, so we restrict the transformation for now.  */
3852 (for cmp (gt le)
3853  (simplify
3854   (cmp:c (minus@2 @0 @1) @0)
3855   (if (single_use (@2)
3856        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3857        && TYPE_UNSIGNED (TREE_TYPE (@0))
3858        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3859    (cmp @1 @0))))
3861 /* Testing for overflow is unnecessary if we already know the result.  */
3862 /* A - B > A  */
3863 (for cmp (gt le)
3864      out (ne eq)
3865  (simplify
3866   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3867   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3868        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3869    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3870 /* A + B < A  */
3871 (for cmp (lt ge)
3872      out (ne eq)
3873  (simplify
3874   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3875   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3876        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3877    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3879 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3880    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
3881 (for cmp (lt ge)
3882      out (ne eq)
3883  (simplify
3884   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3885   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3886    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3887     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3889 /* Simplification of math builtins.  These rules must all be optimizations
3890    as well as IL simplifications.  If there is a possibility that the new
3891    form could be a pessimization, the rule should go in the canonicalization
3892    section that follows this one.
3894    Rules can generally go in this section if they satisfy one of
3895    the following:
3897    - the rule describes an identity
3899    - the rule replaces calls with something as simple as addition or
3900      multiplication
3902    - the rule contains unary calls only and simplifies the surrounding
3903      arithmetic.  (The idea here is to exclude non-unary calls in which
3904      one operand is constant and in which the call is known to be cheap
3905      when the operand has that value.)  */
3907 (if (flag_unsafe_math_optimizations)
3908  /* Simplify sqrt(x) * sqrt(x) -> x.  */
3909  (simplify
3910   (mult (SQRT_ALL@1 @0) @1)
3911   (if (!HONOR_SNANS (type))
3912    @0))
3914  (for op (plus minus)
3915   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
3916   (simplify
3917    (op (rdiv @0 @1)
3918        (rdiv @2 @1))
3919    (rdiv (op @0 @2) @1)))
3921  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
3922  (for root (SQRT CBRT)
3923   (simplify
3924    (mult (root:s @0) (root:s @1))
3925     (root (mult @0 @1))))
3927  /* Simplify expN(x) * expN(y) -> expN(x+y). */
3928  (for exps (EXP EXP2 EXP10 POW10)
3929   (simplify
3930    (mult (exps:s @0) (exps:s @1))
3931     (exps (plus @0 @1))))
3933  /* Simplify a/root(b/c) into a*root(c/b).  */
3934  (for root (SQRT CBRT)
3935   (simplify
3936    (rdiv @0 (root:s (rdiv:s @1 @2)))
3937     (mult @0 (root (rdiv @2 @1)))))
3939  /* Simplify x/expN(y) into x*expN(-y).  */
3940  (for exps (EXP EXP2 EXP10 POW10)
3941   (simplify
3942    (rdiv @0 (exps:s @1))
3943     (mult @0 (exps (negate @1)))))
3945  (for logs (LOG LOG2 LOG10 LOG10)
3946       exps (EXP EXP2 EXP10 POW10)
3947   /* logN(expN(x)) -> x.  */
3948   (simplify
3949    (logs (exps @0))
3950    @0)
3951   /* expN(logN(x)) -> x.  */
3952   (simplify
3953    (exps (logs @0))
3954    @0))
3956  /* Optimize logN(func()) for various exponential functions.  We
3957     want to determine the value "x" and the power "exponent" in
3958     order to transform logN(x**exponent) into exponent*logN(x).  */
3959  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
3960       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
3961   (simplify
3962    (logs (exps @0))
3963    (if (SCALAR_FLOAT_TYPE_P (type))
3964     (with {
3965       tree x;
3966       switch (exps)
3967         {
3968         CASE_CFN_EXP:
3969           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
3970           x = build_real_truncate (type, dconst_e ());
3971           break;
3972         CASE_CFN_EXP2:
3973           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
3974           x = build_real (type, dconst2);
3975           break;
3976         CASE_CFN_EXP10:
3977         CASE_CFN_POW10:
3978           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
3979           {
3980             REAL_VALUE_TYPE dconst10;
3981             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3982             x = build_real (type, dconst10);
3983           }
3984           break;
3985         default:
3986           gcc_unreachable ();
3987         }
3988       }
3989      (mult (logs { x; }) @0)))))
3991  (for logs (LOG LOG
3992             LOG2 LOG2
3993             LOG10 LOG10)
3994       exps (SQRT CBRT)
3995   (simplify
3996    (logs (exps @0))
3997    (if (SCALAR_FLOAT_TYPE_P (type))
3998     (with {
3999       tree x;
4000       switch (exps)
4001         {
4002         CASE_CFN_SQRT:
4003           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
4004           x = build_real (type, dconsthalf);
4005           break;
4006         CASE_CFN_CBRT:
4007           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
4008           x = build_real_truncate (type, dconst_third ());
4009           break;
4010         default:
4011           gcc_unreachable ();
4012         }
4013       }
4014      (mult { x; } (logs @0))))))
4016  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
4017  (for logs (LOG LOG2 LOG10)
4018       pows (POW)
4019   (simplify
4020    (logs (pows @0 @1))
4021    (mult @1 (logs @0))))
4023  /* pow(C,x) -> exp(log(C)*x) if C > 0,
4024     or if C is a positive power of 2,
4025     pow(C,x) -> exp2(log2(C)*x).  */
4026 #if GIMPLE
4027  (for pows (POW)
4028       exps (EXP)
4029       logs (LOG)
4030       exp2s (EXP2)
4031       log2s (LOG2)
4032   (simplify
4033    (pows REAL_CST@0 @1)
4034    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4035         && real_isfinite (TREE_REAL_CST_PTR (@0))
4036         /* As libmvec doesn't have a vectorized exp2, defer optimizing
4037            the use_exp2 case until after vectorization.  It seems actually
4038            beneficial for all constants to postpone this until later,
4039            because exp(log(C)*x), while faster, will have worse precision
4040            and if x folds into a constant too, that is unnecessary
4041            pessimization.  */
4042         && canonicalize_math_after_vectorization_p ())
4043     (with {
4044        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4045        bool use_exp2 = false;
4046        if (targetm.libc_has_function (function_c99_misc)
4047            && value->cl == rvc_normal)
4048          {
4049            REAL_VALUE_TYPE frac_rvt = *value;
4050            SET_REAL_EXP (&frac_rvt, 1);
4051            if (real_equal (&frac_rvt, &dconst1))
4052              use_exp2 = true;
4053          }
4054      }
4055      (if (!use_exp2)
4056       (if (optimize_pow_to_exp (@0, @1))
4057        (exps (mult (logs @0) @1)))
4058       (exp2s (mult (log2s @0) @1)))))))
4059 #endif
4061  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
4062  (for pows (POW)
4063       exps (EXP EXP2 EXP10 POW10)
4064       logs (LOG LOG2 LOG10 LOG10)
4065   (simplify
4066    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4067    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4068         && real_isfinite (TREE_REAL_CST_PTR (@0)))
4069     (exps (plus (mult (logs @0) @1) @2)))))
4071  (for sqrts (SQRT)
4072       cbrts (CBRT)
4073       pows (POW)
4074       exps (EXP EXP2 EXP10 POW10)
4075   /* sqrt(expN(x)) -> expN(x*0.5).  */
4076   (simplify
4077    (sqrts (exps @0))
4078    (exps (mult @0 { build_real (type, dconsthalf); })))
4079   /* cbrt(expN(x)) -> expN(x/3).  */
4080   (simplify
4081    (cbrts (exps @0))
4082    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4083   /* pow(expN(x), y) -> expN(x*y).  */
4084   (simplify
4085    (pows (exps @0) @1)
4086    (exps (mult @0 @1))))
4088  /* tan(atan(x)) -> x.  */
4089  (for tans (TAN)
4090       atans (ATAN)
4091   (simplify
4092    (tans (atans @0))
4093    @0)))
4095 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
4096 (simplify
4097  (CABS (complex:C @0 real_zerop@1))
4098  (abs @0))
4100 /* trunc(trunc(x)) -> trunc(x), etc.  */
4101 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4102  (simplify
4103   (fns (fns @0))
4104   (fns @0)))
4105 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
4106 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4107  (simplify
4108   (fns integer_valued_real_p@0)
4109   @0))
4111 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
4112 (simplify
4113  (HYPOT:c @0 real_zerop@1)
4114  (abs @0))
4116 /* pow(1,x) -> 1.  */
4117 (simplify
4118  (POW real_onep@0 @1)
4119  @0)
4121 (simplify
4122  /* copysign(x,x) -> x.  */
4123  (COPYSIGN_ALL @0 @0)
4124  @0)
4126 (simplify
4127  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
4128  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4129  (abs @0))
4131 (for scale (LDEXP SCALBN SCALBLN)
4132  /* ldexp(0, x) -> 0.  */
4133  (simplify
4134   (scale real_zerop@0 @1)
4135   @0)
4136  /* ldexp(x, 0) -> x.  */
4137  (simplify
4138   (scale @0 integer_zerop@1)
4139   @0)
4140  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
4141  (simplify
4142   (scale REAL_CST@0 @1)
4143   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4144    @0)))
4146 /* Canonicalization of sequences of math builtins.  These rules represent
4147    IL simplifications but are not necessarily optimizations.
4149    The sincos pass is responsible for picking "optimal" implementations
4150    of math builtins, which may be more complicated and can sometimes go
4151    the other way, e.g. converting pow into a sequence of sqrts.
4152    We only want to do these canonicalizations before the pass has run.  */
4154 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4155  /* Simplify tan(x) * cos(x) -> sin(x). */
4156  (simplify
4157   (mult:c (TAN:s @0) (COS:s @0))
4158    (SIN @0))
4160  /* Simplify x * pow(x,c) -> pow(x,c+1). */
4161  (simplify
4162   (mult:c @0 (POW:s @0 REAL_CST@1))
4163   (if (!TREE_OVERFLOW (@1))
4164    (POW @0 (plus @1 { build_one_cst (type); }))))
4166  /* Simplify sin(x) / cos(x) -> tan(x). */
4167  (simplify
4168   (rdiv (SIN:s @0) (COS:s @0))
4169    (TAN @0))
4171  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4172  (simplify
4173   (rdiv (COS:s @0) (SIN:s @0))
4174    (rdiv { build_one_cst (type); } (TAN @0)))
4176  /* Simplify sin(x) / tan(x) -> cos(x). */
4177  (simplify
4178   (rdiv (SIN:s @0) (TAN:s @0))
4179   (if (! HONOR_NANS (@0)
4180        && ! HONOR_INFINITIES (@0))
4181    (COS @0)))
4183  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4184  (simplify
4185   (rdiv (TAN:s @0) (SIN:s @0))
4186   (if (! HONOR_NANS (@0)
4187        && ! HONOR_INFINITIES (@0))
4188    (rdiv { build_one_cst (type); } (COS @0))))
4190  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4191  (simplify
4192   (mult (POW:s @0 @1) (POW:s @0 @2))
4193    (POW @0 (plus @1 @2)))
4195  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4196  (simplify
4197   (mult (POW:s @0 @1) (POW:s @2 @1))
4198    (POW (mult @0 @2) @1))
4200  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4201  (simplify
4202   (mult (POWI:s @0 @1) (POWI:s @2 @1))
4203    (POWI (mult @0 @2) @1))
4205  /* Simplify pow(x,c) / x -> pow(x,c-1). */
4206  (simplify
4207   (rdiv (POW:s @0 REAL_CST@1) @0)
4208   (if (!TREE_OVERFLOW (@1))
4209    (POW @0 (minus @1 { build_one_cst (type); }))))
4211  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4212  (simplify
4213   (rdiv @0 (POW:s @1 @2))
4214    (mult @0 (POW @1 (negate @2))))
4216  (for sqrts (SQRT)
4217       cbrts (CBRT)
4218       pows (POW)
4219   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
4220   (simplify
4221    (sqrts (sqrts @0))
4222    (pows @0 { build_real (type, dconst_quarter ()); }))
4223   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
4224   (simplify
4225    (sqrts (cbrts @0))
4226    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4227   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
4228   (simplify
4229    (cbrts (sqrts @0))
4230    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4231   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
4232   (simplify
4233    (cbrts (cbrts tree_expr_nonnegative_p@0))
4234    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4235   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
4236   (simplify
4237    (sqrts (pows @0 @1))
4238    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4239   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
4240   (simplify
4241    (cbrts (pows tree_expr_nonnegative_p@0 @1))
4242    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4243   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
4244   (simplify
4245    (pows (sqrts @0) @1)
4246    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4247   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
4248   (simplify
4249    (pows (cbrts tree_expr_nonnegative_p@0) @1)
4250    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4251   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
4252   (simplify
4253    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4254    (pows @0 (mult @1 @2))))
4256  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
4257  (simplify
4258   (CABS (complex @0 @0))
4259   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4261  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
4262  (simplify
4263   (HYPOT @0 @0)
4264   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4266  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
4267  (for cexps (CEXP)
4268       exps (EXP)
4269       cexpis (CEXPI)
4270   (simplify
4271    (cexps compositional_complex@0)
4272    (if (targetm.libc_has_function (function_c99_math_complex))
4273     (complex
4274      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4275      (mult @1 (imagpart @2)))))))
4277 (if (canonicalize_math_p ())
4278  /* floor(x) -> trunc(x) if x is nonnegative.  */
4279  (for floors (FLOOR_ALL)
4280       truncs (TRUNC_ALL)
4281   (simplify
4282    (floors tree_expr_nonnegative_p@0)
4283    (truncs @0))))
4285 (match double_value_p
4286  @0
4287  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4288 (for froms (BUILT_IN_TRUNCL
4289             BUILT_IN_FLOORL
4290             BUILT_IN_CEILL
4291             BUILT_IN_ROUNDL
4292             BUILT_IN_NEARBYINTL
4293             BUILT_IN_RINTL)
4294      tos (BUILT_IN_TRUNC
4295           BUILT_IN_FLOOR
4296           BUILT_IN_CEIL
4297           BUILT_IN_ROUND
4298           BUILT_IN_NEARBYINT
4299           BUILT_IN_RINT)
4300  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
4301  (if (optimize && canonicalize_math_p ())
4302   (simplify
4303    (froms (convert double_value_p@0))
4304    (convert (tos @0)))))
4306 (match float_value_p
4307  @0
4308  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4309 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4310             BUILT_IN_FLOORL BUILT_IN_FLOOR
4311             BUILT_IN_CEILL BUILT_IN_CEIL
4312             BUILT_IN_ROUNDL BUILT_IN_ROUND
4313             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4314             BUILT_IN_RINTL BUILT_IN_RINT)
4315      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4316           BUILT_IN_FLOORF BUILT_IN_FLOORF
4317           BUILT_IN_CEILF BUILT_IN_CEILF
4318           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4319           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4320           BUILT_IN_RINTF BUILT_IN_RINTF)
4321  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4322     if x is a float.  */
4323  (if (optimize && canonicalize_math_p ()
4324       && targetm.libc_has_function (function_c99_misc))
4325   (simplify
4326    (froms (convert float_value_p@0))
4327    (convert (tos @0)))))
4329 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4330      tos (XFLOOR XCEIL XROUND XRINT)
4331  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
4332  (if (optimize && canonicalize_math_p ())
4333   (simplify
4334    (froms (convert double_value_p@0))
4335    (tos @0))))
4337 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4338             XFLOOR XCEIL XROUND XRINT)
4339      tos (XFLOORF XCEILF XROUNDF XRINTF)
4340  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4341     if x is a float.  */
4342  (if (optimize && canonicalize_math_p ())
4343   (simplify
4344    (froms (convert float_value_p@0))
4345    (tos @0))))
4347 (if (canonicalize_math_p ())
4348  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
4349  (for floors (IFLOOR LFLOOR LLFLOOR)
4350   (simplify
4351    (floors tree_expr_nonnegative_p@0)
4352    (fix_trunc @0))))
4354 (if (canonicalize_math_p ())
4355  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
4356  (for fns (IFLOOR LFLOOR LLFLOOR
4357            ICEIL LCEIL LLCEIL
4358            IROUND LROUND LLROUND)
4359   (simplify
4360    (fns integer_valued_real_p@0)
4361    (fix_trunc @0)))
4362  (if (!flag_errno_math)
4363   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
4364   (for rints (IRINT LRINT LLRINT)
4365    (simplify
4366     (rints integer_valued_real_p@0)
4367     (fix_trunc @0)))))
4369 (if (canonicalize_math_p ())
4370  (for ifn (IFLOOR ICEIL IROUND IRINT)
4371       lfn (LFLOOR LCEIL LROUND LRINT)
4372       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4373   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4374      sizeof (int) == sizeof (long).  */
4375   (if (TYPE_PRECISION (integer_type_node)
4376        == TYPE_PRECISION (long_integer_type_node))
4377    (simplify
4378     (ifn @0)
4379     (lfn:long_integer_type_node @0)))
4380   /* Canonicalize llround (x) to lround (x) on LP64 targets where
4381      sizeof (long long) == sizeof (long).  */
4382   (if (TYPE_PRECISION (long_long_integer_type_node)
4383        == TYPE_PRECISION (long_integer_type_node))
4384    (simplify
4385     (llfn @0)
4386     (lfn:long_integer_type_node @0)))))
4388 /* cproj(x) -> x if we're ignoring infinities.  */
4389 (simplify
4390  (CPROJ @0)
4391  (if (!HONOR_INFINITIES (type))
4392    @0))
4394 /* If the real part is inf and the imag part is known to be
4395    nonnegative, return (inf + 0i).  */
4396 (simplify
4397  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4398  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4399   { build_complex_inf (type, false); }))
4401 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
4402 (simplify
4403  (CPROJ (complex @0 REAL_CST@1))
4404  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4405   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4407 (for pows (POW)
4408      sqrts (SQRT)
4409      cbrts (CBRT)
4410  (simplify
4411   (pows @0 REAL_CST@1)
4412   (with {
4413     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4414     REAL_VALUE_TYPE tmp;
4415    }
4416    (switch
4417     /* pow(x,0) -> 1.  */
4418     (if (real_equal (value, &dconst0))
4419      { build_real (type, dconst1); })
4420     /* pow(x,1) -> x.  */
4421     (if (real_equal (value, &dconst1))
4422      @0)
4423     /* pow(x,-1) -> 1/x.  */
4424     (if (real_equal (value, &dconstm1))
4425      (rdiv { build_real (type, dconst1); } @0))
4426     /* pow(x,0.5) -> sqrt(x).  */
4427     (if (flag_unsafe_math_optimizations
4428          && canonicalize_math_p ()
4429          && real_equal (value, &dconsthalf))
4430      (sqrts @0))
4431     /* pow(x,1/3) -> cbrt(x).  */
4432     (if (flag_unsafe_math_optimizations
4433          && canonicalize_math_p ()
4434          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4435              real_equal (value, &tmp)))
4436      (cbrts @0))))))
4438 /* powi(1,x) -> 1.  */
4439 (simplify
4440  (POWI real_onep@0 @1)
4441  @0)
4443 (simplify
4444  (POWI @0 INTEGER_CST@1)
4445  (switch
4446   /* powi(x,0) -> 1.  */
4447   (if (wi::to_wide (@1) == 0)
4448    { build_real (type, dconst1); })
4449   /* powi(x,1) -> x.  */
4450   (if (wi::to_wide (@1) == 1)
4451    @0)
4452   /* powi(x,-1) -> 1/x.  */
4453   (if (wi::to_wide (@1) == -1)
4454    (rdiv { build_real (type, dconst1); } @0))))
4456 /* Narrowing of arithmetic and logical operations. 
4458    These are conceptually similar to the transformations performed for
4459    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
4460    term we want to move all that code out of the front-ends into here.  */
4462 /* If we have a narrowing conversion of an arithmetic operation where
4463    both operands are widening conversions from the same type as the outer
4464    narrowing conversion.  Then convert the innermost operands to a suitable
4465    unsigned type (to avoid introducing undefined behavior), perform the
4466    operation and convert the result to the desired type.  */
4467 (for op (plus minus)
4468   (simplify
4469     (convert (op:s (convert@2 @0) (convert?@3 @1)))
4470     (if (INTEGRAL_TYPE_P (type)
4471          /* We check for type compatibility between @0 and @1 below,
4472             so there's no need to check that @1/@3 are integral types.  */
4473          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4474          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4475          /* The precision of the type of each operand must match the
4476             precision of the mode of each operand, similarly for the
4477             result.  */
4478          && type_has_mode_precision_p (TREE_TYPE (@0))
4479          && type_has_mode_precision_p (TREE_TYPE (@1))
4480          && type_has_mode_precision_p (type)
4481          /* The inner conversion must be a widening conversion.  */
4482          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4483          && types_match (@0, type)
4484          && (types_match (@0, @1)
4485              /* Or the second operand is const integer or converted const
4486                 integer from valueize.  */
4487              || TREE_CODE (@1) == INTEGER_CST))
4488       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4489         (op @0 (convert @1))
4490         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4491          (convert (op (convert:utype @0)
4492                       (convert:utype @1))))))))
4494 /* This is another case of narrowing, specifically when there's an outer
4495    BIT_AND_EXPR which masks off bits outside the type of the innermost
4496    operands.   Like the previous case we have to convert the operands
4497    to unsigned types to avoid introducing undefined behavior for the
4498    arithmetic operation.  */
4499 (for op (minus plus)
4500  (simplify
4501   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4502   (if (INTEGRAL_TYPE_P (type)
4503        /* We check for type compatibility between @0 and @1 below,
4504           so there's no need to check that @1/@3 are integral types.  */
4505        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4506        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4507        /* The precision of the type of each operand must match the
4508           precision of the mode of each operand, similarly for the
4509           result.  */
4510        && type_has_mode_precision_p (TREE_TYPE (@0))
4511        && type_has_mode_precision_p (TREE_TYPE (@1))
4512        && type_has_mode_precision_p (type)
4513        /* The inner conversion must be a widening conversion.  */
4514        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4515        && types_match (@0, @1)
4516        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4517            <= TYPE_PRECISION (TREE_TYPE (@0)))
4518        && (wi::to_wide (@4)
4519            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4520                        true, TYPE_PRECISION (type))) == 0)
4521    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4522     (with { tree ntype = TREE_TYPE (@0); }
4523      (convert (bit_and (op @0 @1) (convert:ntype @4))))
4524     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4525      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4526                (convert:utype @4))))))))
4528 /* Transform (@0 < @1 and @0 < @2) to use min, 
4529    (@0 > @1 and @0 > @2) to use max */
4530 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4531      op    (lt      le      gt      ge      lt      le      gt      ge     )
4532      ext   (min     min     max     max     max     max     min     min    )
4533  (simplify
4534   (logic (op:cs @0 @1) (op:cs @0 @2))
4535   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4536        && TREE_CODE (@0) != INTEGER_CST)
4537    (op @0 (ext @1 @2)))))
4539 (simplify
4540  /* signbit(x) -> 0 if x is nonnegative.  */
4541  (SIGNBIT tree_expr_nonnegative_p@0)
4542  { integer_zero_node; })
4544 (simplify
4545  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
4546  (SIGNBIT @0)
4547  (if (!HONOR_SIGNED_ZEROS (@0))
4548   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4550 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
4551 (for cmp (eq ne)
4552  (for op (plus minus)
4553       rop (minus plus)
4554   (simplify
4555    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4556    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4557         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4558         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4559         && !TYPE_SATURATING (TREE_TYPE (@0)))
4560     (with { tree res = int_const_binop (rop, @2, @1); }
4561      (if (TREE_OVERFLOW (res)
4562           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4563       { constant_boolean_node (cmp == NE_EXPR, type); }
4564       (if (single_use (@3))
4565        (cmp @0 { TREE_OVERFLOW (res)
4566                  ? drop_tree_overflow (res) : res; }))))))))
4567 (for cmp (lt le gt ge)
4568  (for op (plus minus)
4569       rop (minus plus)
4570   (simplify
4571    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4572    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4573         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4574     (with { tree res = int_const_binop (rop, @2, @1); }
4575      (if (TREE_OVERFLOW (res))
4576       {
4577         fold_overflow_warning (("assuming signed overflow does not occur "
4578                                 "when simplifying conditional to constant"),
4579                                WARN_STRICT_OVERFLOW_CONDITIONAL);
4580         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4581         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
4582         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4583                                   TYPE_SIGN (TREE_TYPE (@1)))
4584                         != (op == MINUS_EXPR);
4585         constant_boolean_node (less == ovf_high, type);
4586       }
4587       (if (single_use (@3))
4588        (with
4589         {
4590           fold_overflow_warning (("assuming signed overflow does not occur "
4591                                   "when changing X +- C1 cmp C2 to "
4592                                   "X cmp C2 -+ C1"),
4593                                  WARN_STRICT_OVERFLOW_COMPARISON);
4594         }
4595         (cmp @0 { res; })))))))))
4597 /* Canonicalizations of BIT_FIELD_REFs.  */
4599 (simplify
4600  (BIT_FIELD_REF @0 @1 @2)
4601  (switch
4602   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4603        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4604    (switch
4605     (if (integer_zerop (@2))
4606      (view_convert (realpart @0)))
4607     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4608      (view_convert (imagpart @0)))))
4609   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4610        && INTEGRAL_TYPE_P (type)
4611        /* On GIMPLE this should only apply to register arguments.  */
4612        && (! GIMPLE || is_gimple_reg (@0))
4613        /* A bit-field-ref that referenced the full argument can be stripped.  */
4614        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4615             && integer_zerop (@2))
4616            /* Low-parts can be reduced to integral conversions.
4617               ???  The following doesn't work for PDP endian.  */
4618            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4619                /* Don't even think about BITS_BIG_ENDIAN.  */
4620                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4621                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4622                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4623                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
4624                                             - TYPE_PRECISION (type))
4625                                          : 0)) == 0)))
4626    (convert @0))))
4628 /* Simplify vector extracts.  */
4630 (simplify
4631  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4632  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4633       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4634           || (VECTOR_TYPE_P (type)
4635               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4636   (with
4637    {
4638      tree ctor = (TREE_CODE (@0) == SSA_NAME
4639                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4640      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4641      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4642      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4643      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4644    }
4645    (if (n != 0
4646         && (idx % width) == 0
4647         && (n % width) == 0
4648         && known_le ((idx + n) / width,
4649                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4650     (with
4651      {
4652        idx = idx / width;
4653        n = n / width;
4654        /* Constructor elements can be subvectors.  */
4655        poly_uint64 k = 1;
4656        if (CONSTRUCTOR_NELTS (ctor) != 0)
4657          {
4658            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4659            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4660              k = TYPE_VECTOR_SUBPARTS (cons_elem);
4661          }
4662        unsigned HOST_WIDE_INT elt, count, const_k;
4663      }
4664      (switch
4665       /* We keep an exact subset of the constructor elements.  */
4666       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4667        (if (CONSTRUCTOR_NELTS (ctor) == 0)
4668         { build_constructor (type, NULL); }
4669         (if (count == 1)
4670          (if (elt < CONSTRUCTOR_NELTS (ctor))
4671           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4672           { build_zero_cst (type); })
4673          {
4674            vec<constructor_elt, va_gc> *vals;
4675            vec_alloc (vals, count);
4676            for (unsigned i = 0;
4677                 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4678              CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4679                                      CONSTRUCTOR_ELT (ctor, elt + i)->value);
4680            build_constructor (type, vals);
4681          })))
4682       /* The bitfield references a single constructor element.  */
4683       (if (k.is_constant (&const_k)
4684            && idx + n <= (idx / const_k + 1) * const_k)
4685        (switch
4686         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4687          { build_zero_cst (type); })
4688         (if (n == const_k)
4689          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4690         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4691                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
4693 /* Simplify a bit extraction from a bit insertion for the cases with
4694    the inserted element fully covering the extraction or the insertion
4695    not touching the extraction.  */
4696 (simplify
4697  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4698  (with
4699   {
4700     unsigned HOST_WIDE_INT isize;
4701     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4702       isize = TYPE_PRECISION (TREE_TYPE (@1));
4703     else
4704       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4705   }
4706   (switch
4707    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4708         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4709                       wi::to_wide (@ipos) + isize))
4710     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4711                                                  wi::to_wide (@rpos)
4712                                                  - wi::to_wide (@ipos)); }))
4713    (if (wi::geu_p (wi::to_wide (@ipos),
4714                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
4715         || wi::geu_p (wi::to_wide (@rpos),
4716                       wi::to_wide (@ipos) + isize))
4717     (BIT_FIELD_REF @0 @rsize @rpos)))))
4719 (if (canonicalize_math_after_vectorization_p ())
4720  (for fmas (FMA)
4721   (simplify
4722    (fmas:c (negate @0) @1 @2)
4723    (IFN_FNMA @0 @1 @2))
4724   (simplify
4725    (fmas @0 @1 (negate @2))
4726    (IFN_FMS @0 @1 @2))
4727   (simplify
4728    (fmas:c (negate @0) @1 (negate @2))
4729    (IFN_FNMS @0 @1 @2))
4730   (simplify
4731    (negate (fmas@3 @0 @1 @2))
4732    (if (single_use (@3))
4733     (IFN_FNMS @0 @1 @2))))
4735  (simplify
4736   (IFN_FMS:c (negate @0) @1 @2)
4737   (IFN_FNMS @0 @1 @2))
4738  (simplify
4739   (IFN_FMS @0 @1 (negate @2))
4740   (IFN_FMA @0 @1 @2))
4741  (simplify
4742   (IFN_FMS:c (negate @0) @1 (negate @2))
4743   (IFN_FNMA @0 @1 @2))
4744  (simplify
4745   (negate (IFN_FMS@3 @0 @1 @2))
4746    (if (single_use (@3))
4747     (IFN_FNMA @0 @1 @2)))
4749  (simplify
4750   (IFN_FNMA:c (negate @0) @1 @2)
4751   (IFN_FMA @0 @1 @2))
4752  (simplify
4753   (IFN_FNMA @0 @1 (negate @2))
4754   (IFN_FNMS @0 @1 @2))
4755  (simplify
4756   (IFN_FNMA:c (negate @0) @1 (negate @2))
4757   (IFN_FMS @0 @1 @2))
4758  (simplify
4759   (negate (IFN_FNMA@3 @0 @1 @2))
4760   (if (single_use (@3))
4761    (IFN_FMS @0 @1 @2)))
4763  (simplify
4764   (IFN_FNMS:c (negate @0) @1 @2)
4765   (IFN_FMS @0 @1 @2))
4766  (simplify
4767   (IFN_FNMS @0 @1 (negate @2))
4768   (IFN_FNMA @0 @1 @2))
4769  (simplify
4770   (IFN_FNMS:c (negate @0) @1 (negate @2))
4771   (IFN_FMA @0 @1 @2))
4772  (simplify
4773   (negate (IFN_FNMS@3 @0 @1 @2))
4774   (if (single_use (@3))
4775    (IFN_FMA @0 @1 @2))))
4777 /* POPCOUNT simplifications.  */
4778 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4779                BUILT_IN_POPCOUNTIMAX)
4780   /* popcount(X&1) is nop_expr(X&1).  */
4781   (simplify
4782     (popcount @0)
4783     (if (tree_nonzero_bits (@0) == 1)
4784       (convert @0)))
4785   /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
4786   (simplify
4787     (plus (popcount:s @0) (popcount:s @1))
4788     (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4789       (popcount (bit_ior @0 @1))))
4790   /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
4791   (for cmp (le eq ne gt)
4792        rep (eq eq ne ne)
4793     (simplify
4794       (cmp (popcount @0) integer_zerop)
4795       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4797 /* Simplify:
4799      a = a1 op a2
4800      r = c ? a : b;
4802    to:
4804      r = c ? a1 op a2 : b;
4806    if the target can do it in one go.  This makes the operation conditional
4807    on c, so could drop potentially-trapping arithmetic, but that's a valid
4808    simplification if the result of the operation isn't needed.  */
4809 (for uncond_op (UNCOND_BINARY)
4810      cond_op (COND_BINARY)
4811  (simplify
4812   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4813   (with { tree op_type = TREE_TYPE (@4); }
4814    (if (element_precision (type) == element_precision (op_type))
4815     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4816  (simplify
4817   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4818   (with { tree op_type = TREE_TYPE (@4); }
4819    (if (element_precision (type) == element_precision (op_type))
4820     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))