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