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