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