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