PR tree-optimization/82838
[official-gcc.git] / gcc / match.pd
blobf22286e0c2e49108809cb0586e584d77badc0f8d
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 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
1284 (for op (lt le gt ge)
1285  (simplify
1286   (op:c (plus:c@2 @0 @1) @1)
1287   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1288        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1289        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1290    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1291 /* For equality, this is also true with wrapping overflow.  */
1292 (for op (eq ne)
1293  (simplify
1294   (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1295   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1296        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1297            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1298        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1299        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1300        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1301    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1302  (simplify
1303   (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1304   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1305        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1306        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1307    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1309 /* X - Y < X is the same as Y > 0 when there is no overflow.
1310    For equality, this is also true with wrapping overflow.  */
1311 (for op (simple_comparison)
1312  (simplify
1313   (op:c @0 (minus@2 @0 @1))
1314   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1315        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1316            || ((op == EQ_EXPR || op == NE_EXPR)
1317                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1318        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1319    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1321 /* Transform:
1322  * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1323  * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1324  */
1325 (for cmp (eq ne)
1326      ocmp (lt ge)
1327  (simplify
1328   (cmp (trunc_div @0 @1) integer_zerop)
1329   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1330        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1331    (ocmp @0 @1))))
1333 /* X == C - X can never be true if C is odd.  */
1334 (for cmp (eq ne)
1335  (simplify
1336   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1337   (if (TREE_INT_CST_LOW (@1) & 1)
1338    { constant_boolean_node (cmp == NE_EXPR, type); })))
1340 /* Arguments on which one can call get_nonzero_bits to get the bits
1341    possibly set.  */
1342 (match with_possible_nonzero_bits
1343  INTEGER_CST@0)
1344 (match with_possible_nonzero_bits
1345  SSA_NAME@0
1346  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1347 /* Slightly extended version, do not make it recursive to keep it cheap.  */
1348 (match (with_possible_nonzero_bits2 @0)
1349  with_possible_nonzero_bits@0)
1350 (match (with_possible_nonzero_bits2 @0)
1351  (bit_and:c with_possible_nonzero_bits@0 @2))
1353 /* Same for bits that are known to be set, but we do not have
1354    an equivalent to get_nonzero_bits yet.  */
1355 (match (with_certain_nonzero_bits2 @0)
1356  INTEGER_CST@0)
1357 (match (with_certain_nonzero_bits2 @0)
1358  (bit_ior @1 INTEGER_CST@0))
1360 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
1361 (for cmp (eq ne)
1362  (simplify
1363   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1364   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1365    { constant_boolean_node (cmp == NE_EXPR, type); })))
1367 /* ((X inner_op C0) outer_op C1)
1368    With X being a tree where value_range has reasoned certain bits to always be
1369    zero throughout its computed value range,
1370    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1371    where zero_mask has 1's for all bits that are sure to be 0 in
1372    and 0's otherwise.
1373    if (inner_op == '^') C0 &= ~C1;
1374    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1375    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1377 (for inner_op (bit_ior bit_xor)
1378      outer_op (bit_xor bit_ior)
1379 (simplify
1380  (outer_op
1381   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1382  (with
1383   {
1384     bool fail = false;
1385     wide_int zero_mask_not;
1386     wide_int C0;
1387     wide_int cst_emit;
1389     if (TREE_CODE (@2) == SSA_NAME)
1390       zero_mask_not = get_nonzero_bits (@2);
1391     else
1392       fail = true;
1394     if (inner_op == BIT_XOR_EXPR)
1395       {
1396         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1397         cst_emit = C0 | wi::to_wide (@1);
1398       }
1399     else
1400       {
1401         C0 = wi::to_wide (@0);
1402         cst_emit = C0 ^ wi::to_wide (@1);
1403       }
1404   }
1405   (if (!fail && (C0 & zero_mask_not) == 0)
1406    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1407    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1408     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1410 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
1411 (simplify
1412   (pointer_plus (pointer_plus:s @0 @1) @3)
1413   (pointer_plus @0 (plus @1 @3)))
1415 /* Pattern match
1416      tem1 = (long) ptr1;
1417      tem2 = (long) ptr2;
1418      tem3 = tem2 - tem1;
1419      tem4 = (unsigned long) tem3;
1420      tem5 = ptr1 + tem4;
1421    and produce
1422      tem5 = ptr2;  */
1423 (simplify
1424   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1425   /* Conditionally look through a sign-changing conversion.  */
1426   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1427        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1428             || (GENERIC && type == TREE_TYPE (@1))))
1429    @1))
1431 /* Pattern match
1432      tem = (sizetype) ptr;
1433      tem = tem & algn;
1434      tem = -tem;
1435      ... = ptr p+ tem;
1436    and produce the simpler and easier to analyze with respect to alignment
1437      ... = ptr & ~algn;  */
1438 (simplify
1439   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1440   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1441    (bit_and @0 { algn; })))
1443 /* Try folding difference of addresses.  */
1444 (simplify
1445  (minus (convert ADDR_EXPR@0) (convert @1))
1446  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1447   (with { HOST_WIDE_INT diff; }
1448    (if (ptr_difference_const (@0, @1, &diff))
1449     { build_int_cst_type (type, diff); }))))
1450 (simplify
1451  (minus (convert @0) (convert ADDR_EXPR@1))
1452  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1453   (with { HOST_WIDE_INT diff; }
1454    (if (ptr_difference_const (@0, @1, &diff))
1455     { build_int_cst_type (type, diff); }))))
1457 /* If arg0 is derived from the address of an object or function, we may
1458    be able to fold this expression using the object or function's
1459    alignment.  */
1460 (simplify
1461  (bit_and (convert? @0) INTEGER_CST@1)
1462  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1463       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1464   (with
1465    {
1466      unsigned int align;
1467      unsigned HOST_WIDE_INT bitpos;
1468      get_pointer_alignment_1 (@0, &align, &bitpos);
1469    }
1470    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1471     { wide_int_to_tree (type, (wi::to_wide (@1)
1472                                & (bitpos / BITS_PER_UNIT))); }))))
1475 /* We can't reassociate at all for saturating types.  */
1476 (if (!TYPE_SATURATING (type))
1478  /* Contract negates.  */
1479  /* A + (-B) -> A - B */
1480  (simplify
1481   (plus:c @0 (convert? (negate @1)))
1482   /* Apply STRIP_NOPS on the negate.  */
1483   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1484        && !TYPE_OVERFLOW_SANITIZED (type))
1485    (with
1486     {
1487      tree t1 = type;
1488      if (INTEGRAL_TYPE_P (type)
1489          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1490        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1491     }
1492     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1493  /* A - (-B) -> A + B */
1494  (simplify
1495   (minus @0 (convert? (negate @1)))
1496   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1497        && !TYPE_OVERFLOW_SANITIZED (type))
1498    (with
1499     {
1500      tree t1 = type;
1501      if (INTEGRAL_TYPE_P (type)
1502          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1503        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1504     }
1505     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1506  /* -(T)(-A) -> (T)A
1507     Sign-extension is ok except for INT_MIN, which thankfully cannot
1508     happen without overflow.  */
1509  (simplify
1510   (negate (convert (negate @1)))
1511   (if (INTEGRAL_TYPE_P (type)
1512        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1513            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1514                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1515        && !TYPE_OVERFLOW_SANITIZED (type)
1516        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1517    (convert @1)))
1518  (simplify
1519   (negate (convert negate_expr_p@1))
1520   (if (SCALAR_FLOAT_TYPE_P (type)
1521        && ((DECIMAL_FLOAT_TYPE_P (type)
1522             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1523             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1524            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1525    (convert (negate @1))))
1526  (simplify
1527   (negate (nop_convert (negate @1)))
1528   (if (!TYPE_OVERFLOW_SANITIZED (type)
1529        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1530    (view_convert @1)))
1532  /* We can't reassociate floating-point unless -fassociative-math
1533     or fixed-point plus or minus because of saturation to +-Inf.  */
1534  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1535       && !FIXED_POINT_TYPE_P (type))
1537   /* Match patterns that allow contracting a plus-minus pair
1538      irrespective of overflow issues.  */
1539   /* (A +- B) - A       ->  +- B */
1540   /* (A +- B) -+ B      ->  A */
1541   /* A - (A +- B)       -> -+ B */
1542   /* A +- (B -+ A)      ->  +- B */
1543   (simplify
1544     (minus (plus:c @0 @1) @0)
1545     @1)
1546   (simplify
1547     (minus (minus @0 @1) @0)
1548     (negate @1))
1549   (simplify
1550     (plus:c (minus @0 @1) @1)
1551     @0)
1552   (simplify
1553    (minus @0 (plus:c @0 @1))
1554    (negate @1))
1555   (simplify
1556    (minus @0 (minus @0 @1))
1557    @1)
1558   /* (A +- B) + (C - A)   -> C +- B */
1559   /* (A +  B) - (A - C)   -> B + C */
1560   /* More cases are handled with comparisons.  */
1561   (simplify
1562    (plus:c (plus:c @0 @1) (minus @2 @0))
1563    (plus @2 @1))
1564   (simplify
1565    (plus:c (minus @0 @1) (minus @2 @0))
1566    (minus @2 @1))
1567   (simplify
1568    (minus (plus:c @0 @1) (minus @0 @2))
1569    (plus @1 @2))
1571   /* (A +- CST1) +- CST2 -> A + CST3
1572      Use view_convert because it is safe for vectors and equivalent for
1573      scalars.  */
1574   (for outer_op (plus minus)
1575    (for inner_op (plus minus)
1576         neg_inner_op (minus plus)
1577     (simplify
1578      (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1579                CONSTANT_CLASS_P@2)
1580      /* If one of the types wraps, use that one.  */
1581      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1582       (if (outer_op == PLUS_EXPR)
1583        (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1584        (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1585       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1586            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1587        (if (outer_op == PLUS_EXPR)
1588         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1589         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1590        /* If the constant operation overflows we cannot do the transform
1591           directly as we would introduce undefined overflow, for example
1592           with (a - 1) + INT_MIN.  */
1593        (if (types_match (type, @0))
1594         (with { tree cst = const_binop (outer_op == inner_op
1595                                         ? PLUS_EXPR : MINUS_EXPR,
1596                                         type, @1, @2); }
1597          (if (cst && !TREE_OVERFLOW (cst))
1598           (inner_op @0 { cst; } )
1599           /* X+INT_MAX+1 is X-INT_MIN.  */
1600           (if (INTEGRAL_TYPE_P (type) && cst
1601                && wi::to_wide (cst) == wi::min_value (type))
1602            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1603            /* Last resort, use some unsigned type.  */
1604            (with { tree utype = unsigned_type_for (type); }
1605             (view_convert (inner_op
1606                            (view_convert:utype @0)
1607                            (view_convert:utype
1608                             { drop_tree_overflow (cst); })))))))))))))
1610   /* (CST1 - A) +- CST2 -> CST3 - A  */
1611   (for outer_op (plus minus)
1612    (simplify
1613     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1614     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1615      (if (cst && !TREE_OVERFLOW (cst))
1616       (minus { cst; } @0)))))
1618   /* CST1 - (CST2 - A) -> CST3 + A  */
1619   (simplify
1620    (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1621    (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1622     (if (cst && !TREE_OVERFLOW (cst))
1623      (plus { cst; } @0))))
1625   /* ~A + A -> -1 */
1626   (simplify
1627    (plus:c (bit_not @0) @0)
1628    (if (!TYPE_OVERFLOW_TRAPS (type))
1629     { build_all_ones_cst (type); }))
1631   /* ~A + 1 -> -A */
1632   (simplify
1633    (plus (convert? (bit_not @0)) integer_each_onep)
1634    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1635     (negate (convert @0))))
1637   /* -A - 1 -> ~A */
1638   (simplify
1639    (minus (convert? (negate @0)) integer_each_onep)
1640    (if (!TYPE_OVERFLOW_TRAPS (type)
1641         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1642     (bit_not (convert @0))))
1644   /* -1 - A -> ~A */
1645   (simplify
1646    (minus integer_all_onesp @0)
1647    (bit_not @0))
1649   /* (T)(P + A) - (T)P -> (T) A */
1650   (for add (plus pointer_plus)
1651    (simplify
1652     (minus (convert (add @@0 @1))
1653      (convert @0))
1654     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1655          /* For integer types, if A has a smaller type
1656             than T the result depends on the possible
1657             overflow in P + A.
1658             E.g. T=size_t, A=(unsigned)429497295, P>0.
1659             However, if an overflow in P + A would cause
1660             undefined behavior, we can assume that there
1661             is no overflow.  */
1662          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1663              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1664          /* For pointer types, if the conversion of A to the
1665             final type requires a sign- or zero-extension,
1666             then we have to punt - it is not defined which
1667             one is correct.  */
1668          || (POINTER_TYPE_P (TREE_TYPE (@0))
1669              && TREE_CODE (@1) == INTEGER_CST
1670              && tree_int_cst_sign_bit (@1) == 0))
1671      (convert @1))))
1673   /* (T)P - (T)(P + A) -> -(T) A */
1674   (for add (plus pointer_plus)
1675    (simplify
1676     (minus (convert @0)
1677      (convert (add @@0 @1)))
1678     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1679          /* For integer types, if A has a smaller type
1680             than T the result depends on the possible
1681             overflow in P + A.
1682             E.g. T=size_t, A=(unsigned)429497295, P>0.
1683             However, if an overflow in P + A would cause
1684             undefined behavior, we can assume that there
1685             is no overflow.  */
1686          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1687              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1688          /* For pointer types, if the conversion of A to the
1689             final type requires a sign- or zero-extension,
1690             then we have to punt - it is not defined which
1691             one is correct.  */
1692          || (POINTER_TYPE_P (TREE_TYPE (@0))
1693              && TREE_CODE (@1) == INTEGER_CST
1694              && tree_int_cst_sign_bit (@1) == 0))
1695      (negate (convert @1)))))
1697   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1698   (for add (plus pointer_plus)
1699    (simplify
1700     (minus (convert (add @@0 @1))
1701      (convert (add @0 @2)))
1702     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1703          /* For integer types, if A has a smaller type
1704             than T the result depends on the possible
1705             overflow in P + A.
1706             E.g. T=size_t, A=(unsigned)429497295, P>0.
1707             However, if an overflow in P + A would cause
1708             undefined behavior, we can assume that there
1709             is no overflow.  */
1710          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1711              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1712          /* For pointer types, if the conversion of A to the
1713             final type requires a sign- or zero-extension,
1714             then we have to punt - it is not defined which
1715             one is correct.  */
1716          || (POINTER_TYPE_P (TREE_TYPE (@0))
1717              && TREE_CODE (@1) == INTEGER_CST
1718              && tree_int_cst_sign_bit (@1) == 0
1719              && TREE_CODE (@2) == INTEGER_CST
1720              && tree_int_cst_sign_bit (@2) == 0))
1721      (minus (convert @1) (convert @2)))))))
1724 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
1726 (for minmax (min max FMIN FMAX)
1727  (simplify
1728   (minmax @0 @0)
1729   @0))
1730 /* min(max(x,y),y) -> y.  */
1731 (simplify
1732  (min:c (max:c @0 @1) @1)
1733  @1)
1734 /* max(min(x,y),y) -> y.  */
1735 (simplify
1736  (max:c (min:c @0 @1) @1)
1737  @1)
1738 /* max(a,-a) -> abs(a).  */
1739 (simplify
1740  (max:c @0 (negate @0))
1741  (if (TREE_CODE (type) != COMPLEX_TYPE
1742       && (! ANY_INTEGRAL_TYPE_P (type)
1743           || TYPE_OVERFLOW_UNDEFINED (type)))
1744   (abs @0)))
1745 /* min(a,-a) -> -abs(a).  */
1746 (simplify
1747  (min:c @0 (negate @0))
1748  (if (TREE_CODE (type) != COMPLEX_TYPE
1749       && (! ANY_INTEGRAL_TYPE_P (type)
1750           || TYPE_OVERFLOW_UNDEFINED (type)))
1751   (negate (abs @0))))
1752 (simplify
1753  (min @0 @1)
1754  (switch
1755   (if (INTEGRAL_TYPE_P (type)
1756        && TYPE_MIN_VALUE (type)
1757        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1758    @1)
1759   (if (INTEGRAL_TYPE_P (type)
1760        && TYPE_MAX_VALUE (type)
1761        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1762    @0)))
1763 (simplify
1764  (max @0 @1)
1765  (switch
1766   (if (INTEGRAL_TYPE_P (type)
1767        && TYPE_MAX_VALUE (type)
1768        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1769    @1)
1770   (if (INTEGRAL_TYPE_P (type)
1771        && TYPE_MIN_VALUE (type)
1772        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1773    @0)))
1775 /* max (a, a + CST) -> a + CST where CST is positive.  */
1776 /* max (a, a + CST) -> a where CST is negative.  */
1777 (simplify
1778  (max:c @0 (plus@2 @0 INTEGER_CST@1))
1779   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1780    (if (tree_int_cst_sgn (@1) > 0)
1781     @2
1782     @0)))
1784 /* min (a, a + CST) -> a where CST is positive.  */
1785 /* min (a, a + CST) -> a + CST where CST is negative. */
1786 (simplify
1787  (min:c @0 (plus@2 @0 INTEGER_CST@1))
1788   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1789    (if (tree_int_cst_sgn (@1) > 0)
1790     @0
1791     @2)))
1793 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1794    and the outer convert demotes the expression back to x's type.  */
1795 (for minmax (min max)
1796  (simplify
1797   (convert (minmax@0 (convert @1) INTEGER_CST@2))
1798   (if (INTEGRAL_TYPE_P (type)
1799        && types_match (@1, type) && int_fits_type_p (@2, type)
1800        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1801        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1802    (minmax @1 (convert @2)))))
1804 (for minmax (FMIN FMAX)
1805  /* If either argument is NaN, return the other one.  Avoid the
1806     transformation if we get (and honor) a signalling NaN.  */
1807  (simplify
1808   (minmax:c @0 REAL_CST@1)
1809   (if (real_isnan (TREE_REAL_CST_PTR (@1))
1810        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1811    @0)))
1812 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
1813    functions to return the numeric arg if the other one is NaN.
1814    MIN and MAX don't honor that, so only transform if -ffinite-math-only
1815    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
1816    worry about it either.  */
1817 (if (flag_finite_math_only)
1818  (simplify
1819   (FMIN @0 @1)
1820   (min @0 @1))
1821  (simplify
1822   (FMAX @0 @1)
1823   (max @0 @1)))
1824 /* min (-A, -B) -> -max (A, B)  */
1825 (for minmax (min max FMIN FMAX)
1826      maxmin (max min FMAX FMIN)
1827  (simplify
1828   (minmax (negate:s@2 @0) (negate:s@3 @1))
1829   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1830        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1831            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1832    (negate (maxmin @0 @1)))))
1833 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1834    MAX (~X, ~Y) -> ~MIN (X, Y)  */
1835 (for minmax (min max)
1836  maxmin (max min)
1837  (simplify
1838   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1839   (bit_not (maxmin @0 @1))))
1841 /* MIN (X, Y) == X -> X <= Y  */
1842 (for minmax (min min max max)
1843      cmp    (eq  ne  eq  ne )
1844      out    (le  gt  ge  lt )
1845  (simplify
1846   (cmp:c (minmax:c @0 @1) @0)
1847   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1848    (out @0 @1))))
1849 /* MIN (X, 5) == 0 -> X == 0
1850    MIN (X, 5) == 7 -> false  */
1851 (for cmp (eq ne)
1852  (simplify
1853   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1854   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1855                  TYPE_SIGN (TREE_TYPE (@0))))
1856    { constant_boolean_node (cmp == NE_EXPR, type); }
1857    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1858                   TYPE_SIGN (TREE_TYPE (@0))))
1859     (cmp @0 @2)))))
1860 (for cmp (eq ne)
1861  (simplify
1862   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1863   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1864                  TYPE_SIGN (TREE_TYPE (@0))))
1865    { constant_boolean_node (cmp == NE_EXPR, type); }
1866    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1867                   TYPE_SIGN (TREE_TYPE (@0))))
1868     (cmp @0 @2)))))
1869 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
1870 (for minmax (min     min     max     max     min     min     max     max    )
1871      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
1872      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1873  (simplify
1874   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1875   (comb (cmp @0 @2) (cmp @1 @2))))
1877 /* Simplifications of shift and rotates.  */
1879 (for rotate (lrotate rrotate)
1880  (simplify
1881   (rotate integer_all_onesp@0 @1)
1882   @0))
1884 /* Optimize -1 >> x for arithmetic right shifts.  */
1885 (simplify
1886  (rshift integer_all_onesp@0 @1)
1887  (if (!TYPE_UNSIGNED (type)
1888       && tree_expr_nonnegative_p (@1))
1889   @0))
1891 /* Optimize (x >> c) << c into x & (-1<<c).  */
1892 (simplify
1893  (lshift (rshift @0 INTEGER_CST@1) @1)
1894  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
1895   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1897 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1898    types.  */
1899 (simplify
1900  (rshift (lshift @0 INTEGER_CST@1) @1)
1901  (if (TYPE_UNSIGNED (type)
1902       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
1903   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1905 (for shiftrotate (lrotate rrotate lshift rshift)
1906  (simplify
1907   (shiftrotate @0 integer_zerop)
1908   (non_lvalue @0))
1909  (simplify
1910   (shiftrotate integer_zerop@0 @1)
1911   @0)
1912  /* Prefer vector1 << scalar to vector1 << vector2
1913     if vector2 is uniform.  */
1914  (for vec (VECTOR_CST CONSTRUCTOR)
1915   (simplify
1916    (shiftrotate @0 vec@1)
1917    (with { tree tem = uniform_vector_p (@1); }
1918     (if (tem)
1919      (shiftrotate @0 { tem; }))))))
1921 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1922    Y is 0.  Similarly for X >> Y.  */
1923 #if GIMPLE
1924 (for shift (lshift rshift)
1925  (simplify
1926   (shift @0 SSA_NAME@1)
1927    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1928     (with {
1929       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1930       int prec = TYPE_PRECISION (TREE_TYPE (@1));
1931      }
1932      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1933       @0)))))
1934 #endif
1936 /* Rewrite an LROTATE_EXPR by a constant into an
1937    RROTATE_EXPR by a new constant.  */
1938 (simplify
1939  (lrotate @0 INTEGER_CST@1)
1940  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1941                             build_int_cst (TREE_TYPE (@1),
1942                                            element_precision (type)), @1); }))
1944 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
1945 (for op (lrotate rrotate rshift lshift)
1946  (simplify
1947   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1948   (with { unsigned int prec = element_precision (type); }
1949    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
1950         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
1951         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
1952         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
1953     (with { unsigned int low = (tree_to_uhwi (@1)
1954                                 + tree_to_uhwi (@2)); }
1955      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1956         being well defined.  */
1957      (if (low >= prec)
1958       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1959        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1960        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1961         { build_zero_cst (type); }
1962         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1963       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1966 /* ((1 << A) & 1) != 0 -> A == 0
1967    ((1 << A) & 1) == 0 -> A != 0 */
1968 (for cmp (ne eq)
1969      icmp (eq ne)
1970  (simplify
1971   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1972   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1974 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1975    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1976    if CST2 != 0.  */
1977 (for cmp (ne eq)
1978  (simplify
1979   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1980   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
1981    (if (cand < 0
1982         || (!integer_zerop (@2)
1983             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
1984     { constant_boolean_node (cmp == NE_EXPR, type); }
1985     (if (!integer_zerop (@2)
1986          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
1987      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1989 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1990         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1991    if the new mask might be further optimized.  */
1992 (for shift (lshift rshift)
1993  (simplify
1994   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1995            INTEGER_CST@2)
1996    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1997         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1998         && tree_fits_uhwi_p (@1)
1999         && tree_to_uhwi (@1) > 0
2000         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2001     (with
2002      {
2003        unsigned int shiftc = tree_to_uhwi (@1);
2004        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2005        unsigned HOST_WIDE_INT newmask, zerobits = 0;
2006        tree shift_type = TREE_TYPE (@3);
2007        unsigned int prec;
2009        if (shift == LSHIFT_EXPR)
2010          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2011        else if (shift == RSHIFT_EXPR
2012                 && type_has_mode_precision_p (shift_type))
2013          {
2014            prec = TYPE_PRECISION (TREE_TYPE (@3));
2015            tree arg00 = @0;
2016            /* See if more bits can be proven as zero because of
2017               zero extension.  */
2018            if (@3 != @0
2019                && TYPE_UNSIGNED (TREE_TYPE (@0)))
2020              {
2021                tree inner_type = TREE_TYPE (@0);
2022                if (type_has_mode_precision_p (inner_type)
2023                    && TYPE_PRECISION (inner_type) < prec)
2024                  {
2025                    prec = TYPE_PRECISION (inner_type);
2026                    /* See if we can shorten the right shift.  */
2027                    if (shiftc < prec)
2028                      shift_type = inner_type;
2029                    /* Otherwise X >> C1 is all zeros, so we'll optimize
2030                       it into (X, 0) later on by making sure zerobits
2031                       is all ones.  */
2032                  }
2033              }
2034            zerobits = HOST_WIDE_INT_M1U;
2035            if (shiftc < prec)
2036              {
2037                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2038                zerobits <<= prec - shiftc;
2039              }
2040            /* For arithmetic shift if sign bit could be set, zerobits
2041               can contain actually sign bits, so no transformation is
2042               possible, unless MASK masks them all away.  In that
2043               case the shift needs to be converted into logical shift.  */
2044            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2045                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2046              {
2047                if ((mask & zerobits) == 0)
2048                  shift_type = unsigned_type_for (TREE_TYPE (@3));
2049                else
2050                  zerobits = 0;
2051              }
2052          }
2053      }
2054      /* ((X << 16) & 0xff00) is (X, 0).  */
2055      (if ((mask & zerobits) == mask)
2056       { build_int_cst (type, 0); }
2057       (with { newmask = mask | zerobits; }
2058        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2059         (with
2060          {
2061            /* Only do the transformation if NEWMASK is some integer
2062               mode's mask.  */
2063            for (prec = BITS_PER_UNIT;
2064                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2065              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2066                break;
2067          }
2068          (if (prec < HOST_BITS_PER_WIDE_INT
2069               || newmask == HOST_WIDE_INT_M1U)
2070           (with
2071            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2072            (if (!tree_int_cst_equal (newmaskt, @2))
2073             (if (shift_type != TREE_TYPE (@3))
2074              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2075              (bit_and @4 { newmaskt; })))))))))))))
2077 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2078    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
2079 (for shift (lshift rshift)
2080  (for bit_op (bit_and bit_xor bit_ior)
2081   (simplify
2082    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2083    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2084     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2085      (bit_op (shift (convert @0) @1) { mask; }))))))
2087 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
2088 (simplify
2089  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2090   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2091        && (element_precision (TREE_TYPE (@0))
2092            <= element_precision (TREE_TYPE (@1))
2093            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2094    (with
2095     { tree shift_type = TREE_TYPE (@0); }
2096      (convert (rshift (convert:shift_type @1) @2)))))
2098 /* ~(~X >>r Y) -> X >>r Y
2099    ~(~X <<r Y) -> X <<r Y */
2100 (for rotate (lrotate rrotate)
2101  (simplify
2102   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2103    (if ((element_precision (TREE_TYPE (@0))
2104          <= element_precision (TREE_TYPE (@1))
2105          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2106         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2107             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2108     (with
2109      { tree rotate_type = TREE_TYPE (@0); }
2110       (convert (rotate (convert:rotate_type @1) @2))))))
2112 /* Simplifications of conversions.  */
2114 /* Basic strip-useless-type-conversions / strip_nops.  */
2115 (for cvt (convert view_convert float fix_trunc)
2116  (simplify
2117   (cvt @0)
2118   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2119        || (GENERIC && type == TREE_TYPE (@0)))
2120    @0)))
2122 /* Contract view-conversions.  */
2123 (simplify
2124   (view_convert (view_convert @0))
2125   (view_convert @0))
2127 /* For integral conversions with the same precision or pointer
2128    conversions use a NOP_EXPR instead.  */
2129 (simplify
2130   (view_convert @0)
2131   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2132        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2133        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2134    (convert @0)))
2136 /* Strip inner integral conversions that do not change precision or size, or
2137    zero-extend while keeping the same size (for bool-to-char).  */
2138 (simplify
2139   (view_convert (convert@0 @1))
2140   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2141        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2142        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2143        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2144            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2145                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2146    (view_convert @1)))
2148 /* Re-association barriers around constants and other re-association
2149    barriers can be removed.  */
2150 (simplify
2151  (paren CONSTANT_CLASS_P@0)
2152  @0)
2153 (simplify
2154  (paren (paren@1 @0))
2155  @1)
2157 /* Handle cases of two conversions in a row.  */
2158 (for ocvt (convert float fix_trunc)
2159  (for icvt (convert float)
2160   (simplify
2161    (ocvt (icvt@1 @0))
2162    (with
2163     {
2164       tree inside_type = TREE_TYPE (@0);
2165       tree inter_type = TREE_TYPE (@1);
2166       int inside_int = INTEGRAL_TYPE_P (inside_type);
2167       int inside_ptr = POINTER_TYPE_P (inside_type);
2168       int inside_float = FLOAT_TYPE_P (inside_type);
2169       int inside_vec = VECTOR_TYPE_P (inside_type);
2170       unsigned int inside_prec = TYPE_PRECISION (inside_type);
2171       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2172       int inter_int = INTEGRAL_TYPE_P (inter_type);
2173       int inter_ptr = POINTER_TYPE_P (inter_type);
2174       int inter_float = FLOAT_TYPE_P (inter_type);
2175       int inter_vec = VECTOR_TYPE_P (inter_type);
2176       unsigned int inter_prec = TYPE_PRECISION (inter_type);
2177       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2178       int final_int = INTEGRAL_TYPE_P (type);
2179       int final_ptr = POINTER_TYPE_P (type);
2180       int final_float = FLOAT_TYPE_P (type);
2181       int final_vec = VECTOR_TYPE_P (type);
2182       unsigned int final_prec = TYPE_PRECISION (type);
2183       int final_unsignedp = TYPE_UNSIGNED (type);
2184     }
2185    (switch
2186     /* In addition to the cases of two conversions in a row
2187        handled below, if we are converting something to its own
2188        type via an object of identical or wider precision, neither
2189        conversion is needed.  */
2190     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2191           || (GENERIC
2192               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2193          && (((inter_int || inter_ptr) && final_int)
2194              || (inter_float && final_float))
2195          && inter_prec >= final_prec)
2196      (ocvt @0))
2198     /* Likewise, if the intermediate and initial types are either both
2199        float or both integer, we don't need the middle conversion if the
2200        former is wider than the latter and doesn't change the signedness
2201        (for integers).  Avoid this if the final type is a pointer since
2202        then we sometimes need the middle conversion.  */
2203     (if (((inter_int && inside_int) || (inter_float && inside_float))
2204          && (final_int || final_float)
2205          && inter_prec >= inside_prec
2206          && (inter_float || inter_unsignedp == inside_unsignedp))
2207      (ocvt @0))
2209     /* If we have a sign-extension of a zero-extended value, we can
2210        replace that by a single zero-extension.  Likewise if the
2211        final conversion does not change precision we can drop the
2212        intermediate conversion.  */
2213     (if (inside_int && inter_int && final_int
2214          && ((inside_prec < inter_prec && inter_prec < final_prec
2215               && inside_unsignedp && !inter_unsignedp)
2216              || final_prec == inter_prec))
2217      (ocvt @0))
2219     /* Two conversions in a row are not needed unless:
2220         - some conversion is floating-point (overstrict for now), or
2221         - some conversion is a vector (overstrict for now), or
2222         - the intermediate type is narrower than both initial and
2223           final, or
2224         - the intermediate type and innermost type differ in signedness,
2225           and the outermost type is wider than the intermediate, or
2226         - the initial type is a pointer type and the precisions of the
2227           intermediate and final types differ, or
2228         - the final type is a pointer type and the precisions of the
2229           initial and intermediate types differ.  */
2230     (if (! inside_float && ! inter_float && ! final_float
2231          && ! inside_vec && ! inter_vec && ! final_vec
2232          && (inter_prec >= inside_prec || inter_prec >= final_prec)
2233          && ! (inside_int && inter_int
2234                && inter_unsignedp != inside_unsignedp
2235                && inter_prec < final_prec)
2236          && ((inter_unsignedp && inter_prec > inside_prec)
2237              == (final_unsignedp && final_prec > inter_prec))
2238          && ! (inside_ptr && inter_prec != final_prec)
2239          && ! (final_ptr && inside_prec != inter_prec))
2240      (ocvt @0))
2242     /* A truncation to an unsigned type (a zero-extension) should be
2243        canonicalized as bitwise and of a mask.  */
2244     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
2245          && final_int && inter_int && inside_int
2246          && final_prec == inside_prec
2247          && final_prec > inter_prec
2248          && inter_unsignedp)
2249      (convert (bit_and @0 { wide_int_to_tree
2250                               (inside_type,
2251                                wi::mask (inter_prec, false,
2252                                          TYPE_PRECISION (inside_type))); })))
2254     /* If we are converting an integer to a floating-point that can
2255        represent it exactly and back to an integer, we can skip the
2256        floating-point conversion.  */
2257     (if (GIMPLE /* PR66211 */
2258          && inside_int && inter_float && final_int &&
2259          (unsigned) significand_size (TYPE_MODE (inter_type))
2260          >= inside_prec - !inside_unsignedp)
2261      (convert @0)))))))
2263 /* If we have a narrowing conversion to an integral type that is fed by a
2264    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2265    masks off bits outside the final type (and nothing else).  */
2266 (simplify
2267   (convert (bit_and @0 INTEGER_CST@1))
2268   (if (INTEGRAL_TYPE_P (type)
2269        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2270        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2271        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2272                                                     TYPE_PRECISION (type)), 0))
2273    (convert @0)))
2276 /* (X /[ex] A) * A -> X.  */
2277 (simplify
2278   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2279   (convert @0))
2281 /* Canonicalization of binary operations.  */
2283 /* Convert X + -C into X - C.  */
2284 (simplify
2285  (plus @0 REAL_CST@1)
2286  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2287   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2288    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2289     (minus @0 { tem; })))))
2291 /* Convert x+x into x*2.  */
2292 (simplify
2293  (plus @0 @0)
2294  (if (SCALAR_FLOAT_TYPE_P (type))
2295   (mult @0 { build_real (type, dconst2); })
2296   (if (INTEGRAL_TYPE_P (type))
2297    (mult @0 { build_int_cst (type, 2); }))))
2299 (simplify
2300  (minus integer_zerop @1)
2301  (negate @1))
2303 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
2304    ARG0 is zero and X + ARG0 reduces to X, since that would mean
2305    (-ARG1 + ARG0) reduces to -ARG1.  */
2306 (simplify
2307  (minus real_zerop@0 @1)
2308  (if (fold_real_zero_addition_p (type, @0, 0))
2309   (negate @1)))
2311 /* Transform x * -1 into -x.  */
2312 (simplify
2313  (mult @0 integer_minus_onep)
2314  (negate @0))
2316 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
2317    signed overflow for CST != 0 && CST != -1.  */
2318 (simplify
2319  (mult:c (mult:s @0 INTEGER_CST@1) @2)
2320  (if (TREE_CODE (@2) != INTEGER_CST
2321       && !integer_zerop (@1) && !integer_minus_onep (@1))
2322   (mult (mult @0 @2) @1)))
2324 /* True if we can easily extract the real and imaginary parts of a complex
2325    number.  */
2326 (match compositional_complex
2327  (convert? (complex @0 @1)))
2329 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
2330 (simplify
2331  (complex (realpart @0) (imagpart @0))
2332  @0)
2333 (simplify
2334  (realpart (complex @0 @1))
2335  @0)
2336 (simplify
2337  (imagpart (complex @0 @1))
2338  @1)
2340 /* Sometimes we only care about half of a complex expression.  */
2341 (simplify
2342  (realpart (convert?:s (conj:s @0)))
2343  (convert (realpart @0)))
2344 (simplify
2345  (imagpart (convert?:s (conj:s @0)))
2346  (convert (negate (imagpart @0))))
2347 (for part (realpart imagpart)
2348  (for op (plus minus)
2349   (simplify
2350    (part (convert?:s@2 (op:s @0 @1)))
2351    (convert (op (part @0) (part @1))))))
2352 (simplify
2353  (realpart (convert?:s (CEXPI:s @0)))
2354  (convert (COS @0)))
2355 (simplify
2356  (imagpart (convert?:s (CEXPI:s @0)))
2357  (convert (SIN @0)))
2359 /* conj(conj(x)) -> x  */
2360 (simplify
2361  (conj (convert? (conj @0)))
2362  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2363   (convert @0)))
2365 /* conj({x,y}) -> {x,-y}  */
2366 (simplify
2367  (conj (convert?:s (complex:s @0 @1)))
2368  (with { tree itype = TREE_TYPE (type); }
2369   (complex (convert:itype @0) (negate (convert:itype @1)))))
2371 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
2372 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2373  (simplify
2374   (bswap (bswap @0))
2375   @0)
2376  (simplify
2377   (bswap (bit_not (bswap @0)))
2378   (bit_not @0))
2379  (for bitop (bit_xor bit_ior bit_and)
2380   (simplify
2381    (bswap (bitop:c (bswap @0) @1))
2382    (bitop @0 (bswap @1)))))
2385 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
2387 /* Simplify constant conditions.
2388    Only optimize constant conditions when the selected branch
2389    has the same type as the COND_EXPR.  This avoids optimizing
2390    away "c ? x : throw", where the throw has a void type.
2391    Note that we cannot throw away the fold-const.c variant nor
2392    this one as we depend on doing this transform before possibly
2393    A ? B : B -> B triggers and the fold-const.c one can optimize
2394    0 ? A : B to B even if A has side-effects.  Something
2395    genmatch cannot handle.  */
2396 (simplify
2397  (cond INTEGER_CST@0 @1 @2)
2398  (if (integer_zerop (@0))
2399   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2400    @2)
2401   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2402    @1)))
2403 (simplify
2404  (vec_cond VECTOR_CST@0 @1 @2)
2405  (if (integer_all_onesp (@0))
2406   @1
2407   (if (integer_zerop (@0))
2408    @2)))
2410 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
2411    be extended.  */
2412 /* This pattern implements two kinds simplification:
2414    Case 1)
2415    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2416      1) Conversions are type widening from smaller type.
2417      2) Const c1 equals to c2 after canonicalizing comparison.
2418      3) Comparison has tree code LT, LE, GT or GE.
2419    This specific pattern is needed when (cmp (convert x) c) may not
2420    be simplified by comparison patterns because of multiple uses of
2421    x.  It also makes sense here because simplifying across multiple
2422    referred var is always benefitial for complicated cases.
2424    Case 2)
2425    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
2426 (for cmp (lt le gt ge eq)
2427  (simplify
2428   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2429   (with
2430    {
2431      tree from_type = TREE_TYPE (@1);
2432      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2433      enum tree_code code = ERROR_MARK;
2435      if (INTEGRAL_TYPE_P (from_type)
2436          && int_fits_type_p (@2, from_type)
2437          && (types_match (c1_type, from_type)
2438              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2439                  && (TYPE_UNSIGNED (from_type)
2440                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2441          && (types_match (c2_type, from_type)
2442              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2443                  && (TYPE_UNSIGNED (from_type)
2444                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2445        {
2446          if (cmp != EQ_EXPR)
2447            {
2448              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2449                {
2450                  /* X <= Y - 1 equals to X < Y.  */
2451                  if (cmp == LE_EXPR)
2452                    code = LT_EXPR;
2453                  /* X > Y - 1 equals to X >= Y.  */
2454                  if (cmp == GT_EXPR)
2455                    code = GE_EXPR;
2456                }
2457              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2458                {
2459                  /* X < Y + 1 equals to X <= Y.  */
2460                  if (cmp == LT_EXPR)
2461                    code = LE_EXPR;
2462                  /* X >= Y + 1 equals to X > Y.  */
2463                  if (cmp == GE_EXPR)
2464                    code = GT_EXPR;
2465                }
2466              if (code != ERROR_MARK
2467                  || wi::to_widest (@2) == wi::to_widest (@3))
2468                {
2469                  if (cmp == LT_EXPR || cmp == LE_EXPR)
2470                    code = MIN_EXPR;
2471                  if (cmp == GT_EXPR || cmp == GE_EXPR)
2472                    code = MAX_EXPR;
2473                }
2474            }
2475          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
2476          else if (int_fits_type_p (@3, from_type))
2477            code = EQ_EXPR;
2478        }
2479    }
2480    (if (code == MAX_EXPR)
2481     (convert (max @1 (convert @2)))
2482     (if (code == MIN_EXPR)
2483      (convert (min @1 (convert @2)))
2484      (if (code == EQ_EXPR)
2485       (convert (cond (eq @1 (convert @3))
2486                      (convert:from_type @3) (convert:from_type @2)))))))))
2488 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2490      1) OP is PLUS or MINUS.
2491      2) CMP is LT, LE, GT or GE.
2492      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2494    This pattern also handles special cases like:
2496      A) Operand x is a unsigned to signed type conversion and c1 is
2497         integer zero.  In this case,
2498           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
2499           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
2500      B) Const c1 may not equal to (C3 op' C2).  In this case we also
2501         check equality for (c1+1) and (c1-1) by adjusting comparison
2502         code.
2504    TODO: Though signed type is handled by this pattern, it cannot be
2505    simplified at the moment because C standard requires additional
2506    type promotion.  In order to match&simplify it here, the IR needs
2507    to be cleaned up by other optimizers, i.e, VRP.  */
2508 (for op (plus minus)
2509  (for cmp (lt le gt ge)
2510   (simplify
2511    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2512    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2513     (if (types_match (from_type, to_type)
2514          /* Check if it is special case A).  */
2515          || (TYPE_UNSIGNED (from_type)
2516              && !TYPE_UNSIGNED (to_type)
2517              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2518              && integer_zerop (@1)
2519              && (cmp == LT_EXPR || cmp == GE_EXPR)))
2520      (with
2521       {
2522         bool overflow = false;
2523         enum tree_code code, cmp_code = cmp;
2524         wide_int real_c1;
2525         wide_int c1 = wi::to_wide (@1);
2526         wide_int c2 = wi::to_wide (@2);
2527         wide_int c3 = wi::to_wide (@3);
2528         signop sgn = TYPE_SIGN (from_type);
2530         /* Handle special case A), given x of unsigned type:
2531             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
2532             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
2533         if (!types_match (from_type, to_type))
2534           {
2535             if (cmp_code == LT_EXPR)
2536               cmp_code = GT_EXPR;
2537             if (cmp_code == GE_EXPR)
2538               cmp_code = LE_EXPR;
2539             c1 = wi::max_value (to_type);
2540           }
2541         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
2542            compute (c3 op' c2) and check if it equals to c1 with op' being
2543            the inverted operator of op.  Make sure overflow doesn't happen
2544            if it is undefined.  */
2545         if (op == PLUS_EXPR)
2546           real_c1 = wi::sub (c3, c2, sgn, &overflow);
2547         else
2548           real_c1 = wi::add (c3, c2, sgn, &overflow);
2550         code = cmp_code;
2551         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2552           {
2553             /* Check if c1 equals to real_c1.  Boundary condition is handled
2554                by adjusting comparison operation if necessary.  */
2555             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2556                 && !overflow)
2557               {
2558                 /* X <= Y - 1 equals to X < Y.  */
2559                 if (cmp_code == LE_EXPR)
2560                   code = LT_EXPR;
2561                 /* X > Y - 1 equals to X >= Y.  */
2562                 if (cmp_code == GT_EXPR)
2563                   code = GE_EXPR;
2564               }
2565             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2566                 && !overflow)
2567               {
2568                 /* X < Y + 1 equals to X <= Y.  */
2569                 if (cmp_code == LT_EXPR)
2570                   code = LE_EXPR;
2571                 /* X >= Y + 1 equals to X > Y.  */
2572                 if (cmp_code == GE_EXPR)
2573                   code = GT_EXPR;
2574               }
2575             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2576               {
2577                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2578                   code = MIN_EXPR;
2579                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2580                   code = MAX_EXPR;
2581               }
2582           }
2583       }
2584       (if (code == MAX_EXPR)
2585        (op (max @X { wide_int_to_tree (from_type, real_c1); })
2586            { wide_int_to_tree (from_type, c2); })
2587        (if (code == MIN_EXPR)
2588         (op (min @X { wide_int_to_tree (from_type, real_c1); })
2589             { wide_int_to_tree (from_type, c2); })))))))))
2591 (for cnd (cond vec_cond)
2592  /* A ? B : (A ? X : C) -> A ? B : C.  */
2593  (simplify
2594   (cnd @0 (cnd @0 @1 @2) @3)
2595   (cnd @0 @1 @3))
2596  (simplify
2597   (cnd @0 @1 (cnd @0 @2 @3))
2598   (cnd @0 @1 @3))
2599  /* A ? B : (!A ? C : X) -> A ? B : C.  */
2600  /* ???  This matches embedded conditions open-coded because genmatch
2601     would generate matching code for conditions in separate stmts only.
2602     The following is still important to merge then and else arm cases
2603     from if-conversion.  */
2604  (simplify
2605   (cnd @0 @1 (cnd @2 @3 @4))
2606   (if (COMPARISON_CLASS_P (@0)
2607        && COMPARISON_CLASS_P (@2)
2608        && invert_tree_comparison
2609            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2610        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2611        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2612    (cnd @0 @1 @3)))
2613  (simplify
2614   (cnd @0 (cnd @1 @2 @3) @4)
2615   (if (COMPARISON_CLASS_P (@0)
2616        && COMPARISON_CLASS_P (@1)
2617        && invert_tree_comparison
2618            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2619        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2620        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2621    (cnd @0 @3 @4)))
2623  /* A ? B : B -> B.  */
2624  (simplify
2625   (cnd @0 @1 @1)
2626   @1)
2628  /* !A ? B : C -> A ? C : B.  */
2629  (simplify
2630   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2631   (cnd @0 @2 @1)))
2633 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2634    return all -1 or all 0 results.  */
2635 /* ??? We could instead convert all instances of the vec_cond to negate,
2636    but that isn't necessarily a win on its own.  */
2637 (simplify
2638  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2639  (if (VECTOR_TYPE_P (type)
2640       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2641       && (TYPE_MODE (TREE_TYPE (type))
2642           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2643   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2645 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
2646 (simplify
2647  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2648  (if (VECTOR_TYPE_P (type)
2649       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2650       && (TYPE_MODE (TREE_TYPE (type))
2651           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2652   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2655 /* Simplifications of comparisons.  */
2657 /* See if we can reduce the magnitude of a constant involved in a
2658    comparison by changing the comparison code.  This is a canonicalization
2659    formerly done by maybe_canonicalize_comparison_1.  */
2660 (for cmp  (le gt)
2661      acmp (lt ge)
2662  (simplify
2663   (cmp @0 INTEGER_CST@1)
2664   (if (tree_int_cst_sgn (@1) == -1)
2665    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2666 (for cmp  (ge lt)
2667      acmp (gt le)
2668  (simplify
2669   (cmp @0 INTEGER_CST@1)
2670   (if (tree_int_cst_sgn (@1) == 1)
2671    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2674 /* We can simplify a logical negation of a comparison to the
2675    inverted comparison.  As we cannot compute an expression
2676    operator using invert_tree_comparison we have to simulate
2677    that with expression code iteration.  */
2678 (for cmp (tcc_comparison)
2679      icmp (inverted_tcc_comparison)
2680      ncmp (inverted_tcc_comparison_with_nans)
2681  /* Ideally we'd like to combine the following two patterns
2682     and handle some more cases by using
2683       (logical_inverted_value (cmp @0 @1))
2684     here but for that genmatch would need to "inline" that.
2685     For now implement what forward_propagate_comparison did.  */
2686  (simplify
2687   (bit_not (cmp @0 @1))
2688   (if (VECTOR_TYPE_P (type)
2689        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2690    /* Comparison inversion may be impossible for trapping math,
2691       invert_tree_comparison will tell us.  But we can't use
2692       a computed operator in the replacement tree thus we have
2693       to play the trick below.  */
2694    (with { enum tree_code ic = invert_tree_comparison
2695              (cmp, HONOR_NANS (@0)); }
2696     (if (ic == icmp)
2697      (icmp @0 @1)
2698      (if (ic == ncmp)
2699       (ncmp @0 @1))))))
2700  (simplify
2701   (bit_xor (cmp @0 @1) integer_truep)
2702   (with { enum tree_code ic = invert_tree_comparison
2703             (cmp, HONOR_NANS (@0)); }
2704    (if (ic == icmp)
2705     (icmp @0 @1)
2706     (if (ic == ncmp)
2707      (ncmp @0 @1))))))
2709 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2710    ??? The transformation is valid for the other operators if overflow
2711    is undefined for the type, but performing it here badly interacts
2712    with the transformation in fold_cond_expr_with_comparison which
2713    attempts to synthetize ABS_EXPR.  */
2714 (for cmp (eq ne)
2715  (simplify
2716   (cmp (minus@2 @0 @1) integer_zerop)
2717   (if (single_use (@2))
2718    (cmp @0 @1))))
2720 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2721    signed arithmetic case.  That form is created by the compiler
2722    often enough for folding it to be of value.  One example is in
2723    computing loop trip counts after Operator Strength Reduction.  */
2724 (for cmp (simple_comparison)
2725      scmp (swapped_simple_comparison)
2726  (simplify
2727   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2728   /* Handle unfolded multiplication by zero.  */
2729   (if (integer_zerop (@1))
2730    (cmp @1 @2)
2731    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2732         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2733         && single_use (@3))
2734     /* If @1 is negative we swap the sense of the comparison.  */
2735     (if (tree_int_cst_sgn (@1) < 0)
2736      (scmp @0 @2)
2737      (cmp @0 @2))))))
2739 /* Simplify comparison of something with itself.  For IEEE
2740    floating-point, we can only do some of these simplifications.  */
2741 (for cmp (eq ge le)
2742  (simplify
2743   (cmp @0 @0)
2744   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2745        || ! HONOR_NANS (@0))
2746    { constant_boolean_node (true, type); }
2747    (if (cmp != EQ_EXPR)
2748     (eq @0 @0)))))
2749 (for cmp (ne gt lt)
2750  (simplify
2751   (cmp @0 @0)
2752   (if (cmp != NE_EXPR
2753        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2754        || ! HONOR_NANS (@0))
2755    { constant_boolean_node (false, type); })))
2756 (for cmp (unle unge uneq)
2757  (simplify
2758   (cmp @0 @0)
2759   { constant_boolean_node (true, type); }))
2760 (for cmp (unlt ungt)
2761  (simplify
2762   (cmp @0 @0)
2763   (unordered @0 @0)))
2764 (simplify
2765  (ltgt @0 @0)
2766  (if (!flag_trapping_math)
2767   { constant_boolean_node (false, type); }))
2769 /* Fold ~X op ~Y as Y op X.  */
2770 (for cmp (simple_comparison)
2771  (simplify
2772   (cmp (bit_not@2 @0) (bit_not@3 @1))
2773   (if (single_use (@2) && single_use (@3))
2774    (cmp @1 @0))))
2776 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
2777 (for cmp (simple_comparison)
2778      scmp (swapped_simple_comparison)
2779  (simplify
2780   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2781   (if (single_use (@2)
2782        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2783    (scmp @0 (bit_not @1)))))
2785 (for cmp (simple_comparison)
2786  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
2787  (simplify
2788   (cmp (convert@2 @0) (convert? @1))
2789   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2790        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2791            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2792        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2793            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2794    (with
2795     {
2796       tree type1 = TREE_TYPE (@1);
2797       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2798         {
2799           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2800           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2801               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2802             type1 = float_type_node;
2803           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2804               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2805             type1 = double_type_node;
2806         }
2807       tree newtype
2808         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2809            ? TREE_TYPE (@0) : type1); 
2810     }
2811     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2812      (cmp (convert:newtype @0) (convert:newtype @1))))))
2814  (simplify
2815   (cmp @0 REAL_CST@1)
2816   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
2817   (switch
2818    /* a CMP (-0) -> a CMP 0  */
2819    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2820     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2821    /* x != NaN is always true, other ops are always false.  */
2822    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2823         && ! HONOR_SNANS (@1))
2824     { constant_boolean_node (cmp == NE_EXPR, type); })
2825    /* Fold comparisons against infinity.  */
2826    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2827         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2828     (with
2829      {
2830        REAL_VALUE_TYPE max;
2831        enum tree_code code = cmp;
2832        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2833        if (neg)
2834          code = swap_tree_comparison (code);
2835      }
2836      (switch
2837       /* x > +Inf is always false, if with ignore sNANs.  */
2838       (if (code == GT_EXPR
2839            && ! HONOR_SNANS (@0))
2840        { constant_boolean_node (false, type); })
2841       (if (code == LE_EXPR)
2842        /* x <= +Inf is always true, if we don't case about NaNs.  */
2843        (if (! HONOR_NANS (@0))
2844         { constant_boolean_node (true, type); }
2845         /* x <= +Inf is the same as x == x, i.e. !isnan(x).  */
2846         (eq @0 @0)))
2847       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
2848       (if (code == EQ_EXPR || code == GE_EXPR)
2849        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2850         (if (neg)
2851          (lt @0 { build_real (TREE_TYPE (@0), max); })
2852          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2853       /* x < +Inf is always equal to x <= DBL_MAX.  */
2854       (if (code == LT_EXPR)
2855        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2856         (if (neg)
2857          (ge @0 { build_real (TREE_TYPE (@0), max); })
2858          (le @0 { build_real (TREE_TYPE (@0), max); }))))
2859       /* x != +Inf is always equal to !(x > DBL_MAX).  */
2860       (if (code == NE_EXPR)
2861        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2862         (if (! HONOR_NANS (@0))
2863          (if (neg)
2864           (ge @0 { build_real (TREE_TYPE (@0), max); })
2865           (le @0 { build_real (TREE_TYPE (@0), max); }))
2866          (if (neg)
2867           (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2868            { build_one_cst (type); })
2869           (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2870            { build_one_cst (type); }))))))))))
2872  /* If this is a comparison of a real constant with a PLUS_EXPR
2873     or a MINUS_EXPR of a real constant, we can convert it into a
2874     comparison with a revised real constant as long as no overflow
2875     occurs when unsafe_math_optimizations are enabled.  */
2876  (if (flag_unsafe_math_optimizations)
2877   (for op (plus minus)
2878    (simplify
2879     (cmp (op @0 REAL_CST@1) REAL_CST@2)
2880     (with
2881      {
2882        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2883                                TREE_TYPE (@1), @2, @1);
2884      }
2885      (if (tem && !TREE_OVERFLOW (tem))
2886       (cmp @0 { tem; }))))))
2888  /* Likewise, we can simplify a comparison of a real constant with
2889     a MINUS_EXPR whose first operand is also a real constant, i.e.
2890     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
2891     floating-point types only if -fassociative-math is set.  */
2892  (if (flag_associative_math)
2893   (simplify
2894    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2895    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2896     (if (tem && !TREE_OVERFLOW (tem))
2897      (cmp { tem; } @1)))))
2899  /* Fold comparisons against built-in math functions.  */
2900  (if (flag_unsafe_math_optimizations
2901       && ! flag_errno_math)
2902   (for sq (SQRT)
2903    (simplify
2904     (cmp (sq @0) REAL_CST@1)
2905     (switch
2906      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2907       (switch
2908        /* sqrt(x) < y is always false, if y is negative.  */
2909        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2910         { constant_boolean_node (false, type); })
2911        /* sqrt(x) > y is always true, if y is negative and we
2912           don't care about NaNs, i.e. negative values of x.  */
2913        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2914         { constant_boolean_node (true, type); })
2915        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
2916        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2917      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2918       (switch
2919        /* sqrt(x) < 0 is always false.  */
2920        (if (cmp == LT_EXPR)
2921         { constant_boolean_node (false, type); })
2922        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
2923        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2924         { constant_boolean_node (true, type); })
2925        /* sqrt(x) <= 0 -> x == 0.  */
2926        (if (cmp == LE_EXPR)
2927         (eq @0 @1))
2928        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
2929           == or !=.  In the last case:
2931             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2933           if x is negative or NaN.  Due to -funsafe-math-optimizations,
2934           the results for other x follow from natural arithmetic.  */
2935        (cmp @0 @1)))
2936      (if (cmp == GT_EXPR || cmp == GE_EXPR)
2937       (with
2938        {
2939          REAL_VALUE_TYPE c2;
2940          real_arithmetic (&c2, MULT_EXPR,
2941                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2942          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2943        }
2944        (if (REAL_VALUE_ISINF (c2))
2945         /* sqrt(x) > y is x == +Inf, when y is very large.  */
2946         (if (HONOR_INFINITIES (@0))
2947          (eq @0 { build_real (TREE_TYPE (@0), c2); })
2948          { constant_boolean_node (false, type); })
2949         /* sqrt(x) > c is the same as x > c*c.  */
2950         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2951      (if (cmp == LT_EXPR || cmp == LE_EXPR)
2952       (with
2953        {
2954          REAL_VALUE_TYPE c2;
2955          real_arithmetic (&c2, MULT_EXPR,
2956                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2957          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2958        }
2959        (if (REAL_VALUE_ISINF (c2))
2960         (switch
2961          /* sqrt(x) < y is always true, when y is a very large
2962             value and we don't care about NaNs or Infinities.  */
2963          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2964           { constant_boolean_node (true, type); })
2965          /* sqrt(x) < y is x != +Inf when y is very large and we
2966             don't care about NaNs.  */
2967          (if (! HONOR_NANS (@0))
2968           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2969          /* sqrt(x) < y is x >= 0 when y is very large and we
2970             don't care about Infinities.  */
2971          (if (! HONOR_INFINITIES (@0))
2972           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2973          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
2974          (if (GENERIC)
2975           (truth_andif
2976            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2977            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2978         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
2979         (if (! HONOR_NANS (@0))
2980          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2981          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
2982          (if (GENERIC)
2983           (truth_andif
2984            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2985            (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2986    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
2987    (simplify
2988     (cmp (sq @0) (sq @1))
2989       (if (! HONOR_NANS (@0))
2990         (cmp @0 @1))))))
2992 /* Optimize various special cases of (FTYPE) N CMP CST.  */
2993 (for cmp  (lt le eq ne ge gt)
2994      icmp (le le eq ne ge ge)
2995  (simplify
2996   (cmp (float @0) REAL_CST@1)
2997    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
2998         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
2999     (with
3000      {
3001        tree itype = TREE_TYPE (@0);
3002        signop isign = TYPE_SIGN (itype);
3003        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3004        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3005        /* Be careful to preserve any potential exceptions due to
3006           NaNs.  qNaNs are ok in == or != context.
3007           TODO: relax under -fno-trapping-math or
3008           -fno-signaling-nans.  */
3009        bool exception_p
3010          = real_isnan (cst) && (cst->signalling
3011                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3012        /* INT?_MIN is power-of-two so it takes
3013           only one mantissa bit.  */
3014        bool signed_p = isign == SIGNED;
3015        bool itype_fits_ftype_p
3016          = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3017      }
3018      /* TODO: allow non-fitting itype and SNaNs when
3019         -fno-trapping-math.  */
3020      (if (itype_fits_ftype_p && ! exception_p)
3021       (with
3022        {
3023          REAL_VALUE_TYPE imin, imax;
3024          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3025          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3027          REAL_VALUE_TYPE icst;
3028          if (cmp == GT_EXPR || cmp == GE_EXPR)
3029            real_ceil (&icst, fmt, cst);
3030          else if (cmp == LT_EXPR || cmp == LE_EXPR)
3031            real_floor (&icst, fmt, cst);
3032          else
3033            real_trunc (&icst, fmt, cst);
3035          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3037          bool overflow_p = false;
3038          wide_int icst_val
3039            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3040        }
3041        (switch
3042         /* Optimize cases when CST is outside of ITYPE's range.  */
3043         (if (real_compare (LT_EXPR, cst, &imin))
3044          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3045                                   type); })
3046         (if (real_compare (GT_EXPR, cst, &imax))
3047          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3048                                   type); })
3049         /* Remove cast if CST is an integer representable by ITYPE.  */
3050         (if (cst_int_p)
3051          (cmp @0 { gcc_assert (!overflow_p);
3052                    wide_int_to_tree (itype, icst_val); })
3053         )
3054         /* When CST is fractional, optimize
3055             (FTYPE) N == CST -> 0
3056             (FTYPE) N != CST -> 1.  */
3057         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3058          { constant_boolean_node (cmp == NE_EXPR, type); }) 
3059         /* Otherwise replace with sensible integer constant.  */
3060         (with
3061          {
3062            gcc_checking_assert (!overflow_p);
3063          }
3064          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3066 /* Fold A /[ex] B CMP C to A CMP B * C.  */
3067 (for cmp (eq ne)
3068  (simplify
3069   (cmp (exact_div @0 @1) INTEGER_CST@2)
3070   (if (!integer_zerop (@1))
3071    (if (wi::to_wide (@2) == 0)
3072     (cmp @0 @2)
3073     (if (TREE_CODE (@1) == INTEGER_CST)
3074      (with
3075       {
3076         bool ovf;
3077         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3078                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3079       }
3080       (if (ovf)
3081        { constant_boolean_node (cmp == NE_EXPR, type); }
3082        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3083 (for cmp (lt le gt ge)
3084  (simplify
3085   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3086   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3087    (with
3088     {
3089       bool ovf;
3090       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3091                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3092     }
3093     (if (ovf)
3094      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3095                                         TYPE_SIGN (TREE_TYPE (@2)))
3096                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3097      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3099 /* Unordered tests if either argument is a NaN.  */
3100 (simplify
3101  (bit_ior (unordered @0 @0) (unordered @1 @1))
3102  (if (types_match (@0, @1))
3103   (unordered @0 @1)))
3104 (simplify
3105  (bit_and (ordered @0 @0) (ordered @1 @1))
3106  (if (types_match (@0, @1))
3107   (ordered @0 @1)))
3108 (simplify
3109  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3110  @2)
3111 (simplify
3112  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3113  @2)
3115 /* Simple range test simplifications.  */
3116 /* A < B || A >= B -> true.  */
3117 (for test1 (lt le le le ne ge)
3118      test2 (ge gt ge ne eq ne)
3119  (simplify
3120   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3121   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3122        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3123    { constant_boolean_node (true, type); })))
3124 /* A < B && A >= B -> false.  */
3125 (for test1 (lt lt lt le ne eq)
3126      test2 (ge gt eq gt eq gt)
3127  (simplify
3128   (bit_and:c (test1 @0 @1) (test2 @0 @1))
3129   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3130        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3131    { constant_boolean_node (false, type); })))
3133 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3134    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
3136    Note that comparisons
3137      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
3138      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
3139    will be canonicalized to above so there's no need to
3140    consider them here.
3141  */
3143 (for cmp (le gt)
3144      eqcmp (eq ne)
3145  (simplify
3146   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3147   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3148    (with
3149     {
3150      tree ty = TREE_TYPE (@0);
3151      unsigned prec = TYPE_PRECISION (ty);
3152      wide_int mask = wi::to_wide (@2, prec);
3153      wide_int rhs = wi::to_wide (@3, prec);
3154      signop sgn = TYPE_SIGN (ty);
3155     }
3156     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3157          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3158       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3159              { build_zero_cst (ty); }))))))
3161 /* -A CMP -B -> B CMP A.  */
3162 (for cmp (tcc_comparison)
3163      scmp (swapped_tcc_comparison)
3164  (simplify
3165   (cmp (negate @0) (negate @1))
3166   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3167        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3168            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3169    (scmp @0 @1)))
3170  (simplify
3171   (cmp (negate @0) CONSTANT_CLASS_P@1)
3172   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3173        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3174            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3175    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3176     (if (tem && !TREE_OVERFLOW (tem))
3177      (scmp @0 { tem; }))))))
3179 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
3180 (for op (eq ne)
3181  (simplify
3182   (op (abs @0) zerop@1)
3183   (op @0 @1)))
3185 /* From fold_sign_changed_comparison and fold_widened_comparison.
3186    FIXME: the lack of symmetry is disturbing.  */
3187 (for cmp (simple_comparison)
3188  (simplify
3189   (cmp (convert@0 @00) (convert?@1 @10))
3190   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3191        /* Disable this optimization if we're casting a function pointer
3192           type on targets that require function pointer canonicalization.  */
3193        && !(targetm.have_canonicalize_funcptr_for_compare ()
3194             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3195             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3196        && single_use (@0))
3197    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3198         && (TREE_CODE (@10) == INTEGER_CST
3199             || @1 != @10)
3200         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3201             || cmp == NE_EXPR
3202             || cmp == EQ_EXPR)
3203         && !POINTER_TYPE_P (TREE_TYPE (@00)))
3204     /* ???  The special-casing of INTEGER_CST conversion was in the original
3205        code and here to avoid a spurious overflow flag on the resulting
3206        constant which fold_convert produces.  */
3207     (if (TREE_CODE (@1) == INTEGER_CST)
3208      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3209                                 TREE_OVERFLOW (@1)); })
3210      (cmp @00 (convert @1)))
3212     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3213      /* If possible, express the comparison in the shorter mode.  */
3214      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3215            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3216            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3217                && TYPE_UNSIGNED (TREE_TYPE (@00))))
3218           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3219               || ((TYPE_PRECISION (TREE_TYPE (@00))
3220                    >= TYPE_PRECISION (TREE_TYPE (@10)))
3221                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
3222                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
3223               || (TREE_CODE (@10) == INTEGER_CST
3224                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3225                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
3226       (cmp @00 (convert @10))
3227       (if (TREE_CODE (@10) == INTEGER_CST
3228            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3229            && !int_fits_type_p (@10, TREE_TYPE (@00)))
3230        (with
3231         {
3232           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3233           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3234           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3235           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3236         }
3237         (if (above || below)
3238          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3239           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3240           (if (cmp == LT_EXPR || cmp == LE_EXPR)
3241            { constant_boolean_node (above ? true : false, type); }
3242            (if (cmp == GT_EXPR || cmp == GE_EXPR)
3243             { constant_boolean_node (above ? false : true, type); }))))))))))))
3245 (for cmp (eq ne)
3246  /* A local variable can never be pointed to by
3247     the default SSA name of an incoming parameter.
3248     SSA names are canonicalized to 2nd place.  */
3249  (simplify
3250   (cmp addr@0 SSA_NAME@1)
3251   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3252        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3253    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3254     (if (TREE_CODE (base) == VAR_DECL
3255          && auto_var_in_fn_p (base, current_function_decl))
3256      (if (cmp == NE_EXPR)
3257       { constant_boolean_node (true, type); }
3258       { constant_boolean_node (false, type); }))))))
3260 /* Equality compare simplifications from fold_binary  */
3261 (for cmp (eq ne)
3263  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3264     Similarly for NE_EXPR.  */
3265  (simplify
3266   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3267   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3268        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3269    { constant_boolean_node (cmp == NE_EXPR, type); }))
3271  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
3272  (simplify
3273   (cmp (bit_xor @0 @1) integer_zerop)
3274   (cmp @0 @1))
3276  /* (X ^ Y) == Y becomes X == 0.
3277     Likewise (X ^ Y) == X becomes Y == 0.  */
3278  (simplify
3279   (cmp:c (bit_xor:c @0 @1) @0)
3280   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3282  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
3283  (simplify
3284   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3285   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3286    (cmp @0 (bit_xor @1 (convert @2)))))
3288  (simplify
3289   (cmp (convert? addr@0) integer_zerop)
3290   (if (tree_single_nonzero_warnv_p (@0, NULL))
3291    { constant_boolean_node (cmp == NE_EXPR, type); })))
3293 /* If we have (A & C) == C where C is a power of 2, convert this into
3294    (A & C) != 0.  Similarly for NE_EXPR.  */
3295 (for cmp (eq ne)
3296      icmp (ne eq)
3297  (simplify
3298   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3299   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3301 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3302    convert this into a shift followed by ANDing with D.  */
3303 (simplify
3304  (cond
3305   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3306   integer_pow2p@2 integer_zerop)
3307  (with {
3308     int shift = (wi::exact_log2 (wi::to_wide (@2))
3309                  - wi::exact_log2 (wi::to_wide (@1)));
3310   }
3311   (if (shift > 0)
3312    (bit_and
3313     (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3314    (bit_and
3315     (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3317 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3318    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
3319 (for cmp (eq ne)
3320      ncmp (ge lt)
3321  (simplify
3322   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3323   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3324        && type_has_mode_precision_p (TREE_TYPE (@0))
3325        && element_precision (@2) >= element_precision (@0)
3326        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3327    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3328     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3330 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3331    this into a right shift or sign extension followed by ANDing with C.  */
3332 (simplify
3333  (cond
3334   (lt @0 integer_zerop)
3335   integer_pow2p@1 integer_zerop)
3336  (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3337   (with {
3338     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3339    }
3340    (if (shift >= 0)
3341     (bit_and
3342      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3343      @1)
3344     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3345        sign extension followed by AND with C will achieve the effect.  */
3346     (bit_and (convert @0) @1)))))
3348 /* When the addresses are not directly of decls compare base and offset.
3349    This implements some remaining parts of fold_comparison address
3350    comparisons but still no complete part of it.  Still it is good
3351    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
3352 (for cmp (simple_comparison)
3353  (simplify
3354   (cmp (convert1?@2 addr@0) (convert2? addr@1))
3355   (with
3356    {
3357      HOST_WIDE_INT off0, off1;
3358      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3359      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3360      if (base0 && TREE_CODE (base0) == MEM_REF)
3361        {
3362          off0 += mem_ref_offset (base0).to_short_addr ();
3363          base0 = TREE_OPERAND (base0, 0);
3364        }
3365      if (base1 && TREE_CODE (base1) == MEM_REF)
3366        {
3367          off1 += mem_ref_offset (base1).to_short_addr ();
3368          base1 = TREE_OPERAND (base1, 0);
3369        }
3370    }
3371    (if (base0 && base1)
3372     (with
3373      {
3374        int equal = 2;
3375        /* Punt in GENERIC on variables with value expressions;
3376           the value expressions might point to fields/elements
3377           of other vars etc.  */
3378        if (GENERIC
3379            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3380                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3381          ;
3382        else if (decl_in_symtab_p (base0)
3383                 && decl_in_symtab_p (base1))
3384          equal = symtab_node::get_create (base0)
3385                    ->equal_address_to (symtab_node::get_create (base1));
3386        else if ((DECL_P (base0)
3387                  || TREE_CODE (base0) == SSA_NAME
3388                  || TREE_CODE (base0) == STRING_CST)
3389                 && (DECL_P (base1)
3390                     || TREE_CODE (base1) == SSA_NAME
3391                     || TREE_CODE (base1) == STRING_CST))
3392          equal = (base0 == base1);
3393      }
3394      (if (equal == 1)
3395       (switch
3396        (if (cmp == EQ_EXPR)
3397         { constant_boolean_node (off0 == off1, type); })
3398        (if (cmp == NE_EXPR)
3399         { constant_boolean_node (off0 != off1, type); })
3400        (if (cmp == LT_EXPR)
3401         { constant_boolean_node (off0 < off1, type); })
3402        (if (cmp == LE_EXPR)
3403         { constant_boolean_node (off0 <= off1, type); })
3404        (if (cmp == GE_EXPR)
3405         { constant_boolean_node (off0 >= off1, type); })
3406        (if (cmp == GT_EXPR)
3407         { constant_boolean_node (off0 > off1, type); }))
3408       (if (equal == 0
3409            && DECL_P (base0) && DECL_P (base1)
3410            /* If we compare this as integers require equal offset.  */
3411            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3412                || off0 == off1))
3413        (switch
3414         (if (cmp == EQ_EXPR)
3415          { constant_boolean_node (false, type); })
3416         (if (cmp == NE_EXPR)
3417          { constant_boolean_node (true, type); })))))))))
3419 /* Simplify pointer equality compares using PTA.  */
3420 (for neeq (ne eq)
3421  (simplify
3422   (neeq @0 @1)
3423   (if (POINTER_TYPE_P (TREE_TYPE (@0))
3424        && ptrs_compare_unequal (@0, @1))
3425    { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3427 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3428    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3429    Disable the transform if either operand is pointer to function.
3430    This broke pr22051-2.c for arm where function pointer
3431    canonicalizaion is not wanted.  */
3433 (for cmp (ne eq)
3434  (simplify
3435   (cmp (convert @0) INTEGER_CST@1)
3436   (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3437         && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3438       || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3439           && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3440    (cmp @0 (convert @1)))))
3442 /* Non-equality compare simplifications from fold_binary  */
3443 (for cmp (lt gt le ge)
3444  /* Comparisons with the highest or lowest possible integer of
3445     the specified precision will have known values.  */
3446  (simplify
3447   (cmp (convert?@2 @0) INTEGER_CST@1)
3448   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3449        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3450    (with
3451     {
3452       tree arg1_type = TREE_TYPE (@1);
3453       unsigned int prec = TYPE_PRECISION (arg1_type);
3454       wide_int max = wi::max_value (arg1_type);
3455       wide_int signed_max = wi::max_value (prec, SIGNED);
3456       wide_int min = wi::min_value (arg1_type);
3457     }
3458     (switch
3459      (if (wi::to_wide (@1) == max)
3460       (switch
3461        (if (cmp == GT_EXPR)
3462         { constant_boolean_node (false, type); })
3463        (if (cmp == GE_EXPR)
3464         (eq @2 @1))
3465        (if (cmp == LE_EXPR)
3466         { constant_boolean_node (true, type); })
3467        (if (cmp == LT_EXPR)
3468         (ne @2 @1))))
3469      (if (wi::to_wide (@1) == min)
3470       (switch
3471        (if (cmp == LT_EXPR)
3472         { constant_boolean_node (false, type); })
3473        (if (cmp == LE_EXPR)
3474         (eq @2 @1))
3475        (if (cmp == GE_EXPR)
3476         { constant_boolean_node (true, type); })
3477        (if (cmp == GT_EXPR)
3478         (ne @2 @1))))
3479      (if (wi::to_wide (@1) == max - 1)
3480       (switch
3481        (if (cmp == GT_EXPR)
3482         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3483        (if (cmp == LE_EXPR)
3484         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3485      (if (wi::to_wide (@1) == min + 1)
3486       (switch
3487        (if (cmp == GE_EXPR)
3488         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3489        (if (cmp == LT_EXPR)
3490         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3491      (if (wi::to_wide (@1) == signed_max
3492           && TYPE_UNSIGNED (arg1_type)
3493           /* We will flip the signedness of the comparison operator
3494              associated with the mode of @1, so the sign bit is
3495              specified by this mode.  Check that @1 is the signed
3496              max associated with this sign bit.  */
3497           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3498           /* signed_type does not work on pointer types.  */
3499           && INTEGRAL_TYPE_P (arg1_type))
3500       /* The following case also applies to X < signed_max+1
3501          and X >= signed_max+1 because previous transformations.  */
3502       (if (cmp == LE_EXPR || cmp == GT_EXPR)
3503        (with { tree st = signed_type_for (arg1_type); }
3504         (if (cmp == LE_EXPR)
3505          (ge (convert:st @0) { build_zero_cst (st); })
3506          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3508 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3509  /* If the second operand is NaN, the result is constant.  */
3510  (simplify
3511   (cmp @0 REAL_CST@1)
3512   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3513        && (cmp != LTGT_EXPR || ! flag_trapping_math))
3514    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3515                             ? false : true, type); })))
3517 /* bool_var != 0 becomes bool_var.  */
3518 (simplify
3519  (ne @0 integer_zerop)
3520  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3521       && types_match (type, TREE_TYPE (@0)))
3522   (non_lvalue @0)))
3523 /* bool_var == 1 becomes bool_var.  */
3524 (simplify
3525  (eq @0 integer_onep)
3526  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3527       && types_match (type, TREE_TYPE (@0)))
3528   (non_lvalue @0)))
3529 /* Do not handle
3530    bool_var == 0 becomes !bool_var or
3531    bool_var != 1 becomes !bool_var
3532    here because that only is good in assignment context as long
3533    as we require a tcc_comparison in GIMPLE_CONDs where we'd
3534    replace if (x == 0) with tem = ~x; if (tem != 0) which is
3535    clearly less optimal and which we'll transform again in forwprop.  */
3537 /* When one argument is a constant, overflow detection can be simplified.
3538    Currently restricted to single use so as not to interfere too much with
3539    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3540    A + CST CMP A  ->  A CMP' CST' */
3541 (for cmp (lt le ge gt)
3542      out (gt gt le le)
3543  (simplify
3544   (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3545   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3546        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3547        && wi::to_wide (@1) != 0
3548        && single_use (@2))
3549    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3550     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3551                                 wi::max_value (prec, UNSIGNED)
3552                                 - wi::to_wide (@1)); })))))
3554 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3555    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3556    expects the long form, so we restrict the transformation for now.  */
3557 (for cmp (gt le)
3558  (simplify
3559   (cmp:c (minus@2 @0 @1) @0)
3560   (if (single_use (@2)
3561        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3562        && TYPE_UNSIGNED (TREE_TYPE (@0))
3563        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3564    (cmp @1 @0))))
3566 /* Testing for overflow is unnecessary if we already know the result.  */
3567 /* A - B > A  */
3568 (for cmp (gt le)
3569      out (ne eq)
3570  (simplify
3571   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3572   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3573        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3574    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3575 /* A + B < A  */
3576 (for cmp (lt ge)
3577      out (ne eq)
3578  (simplify
3579   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3580   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3581        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3582    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3584 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3585    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
3586 (for cmp (lt ge)
3587      out (ne eq)
3588  (simplify
3589   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3590   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3591    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3592     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3594 /* Simplification of math builtins.  These rules must all be optimizations
3595    as well as IL simplifications.  If there is a possibility that the new
3596    form could be a pessimization, the rule should go in the canonicalization
3597    section that follows this one.
3599    Rules can generally go in this section if they satisfy one of
3600    the following:
3602    - the rule describes an identity
3604    - the rule replaces calls with something as simple as addition or
3605      multiplication
3607    - the rule contains unary calls only and simplifies the surrounding
3608      arithmetic.  (The idea here is to exclude non-unary calls in which
3609      one operand is constant and in which the call is known to be cheap
3610      when the operand has that value.)  */
3612 (if (flag_unsafe_math_optimizations)
3613  /* Simplify sqrt(x) * sqrt(x) -> x.  */
3614  (simplify
3615   (mult (SQRT@1 @0) @1)
3616   (if (!HONOR_SNANS (type))
3617    @0))
3619  (for op (plus minus)
3620   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
3621   (simplify
3622    (op (rdiv @0 @1)
3623        (rdiv @2 @1))
3624    (rdiv (op @0 @2) @1)))
3626  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
3627  (for root (SQRT CBRT)
3628   (simplify
3629    (mult (root:s @0) (root:s @1))
3630     (root (mult @0 @1))))
3632  /* Simplify expN(x) * expN(y) -> expN(x+y). */
3633  (for exps (EXP EXP2 EXP10 POW10)
3634   (simplify
3635    (mult (exps:s @0) (exps:s @1))
3636     (exps (plus @0 @1))))
3638  /* Simplify a/root(b/c) into a*root(c/b).  */
3639  (for root (SQRT CBRT)
3640   (simplify
3641    (rdiv @0 (root:s (rdiv:s @1 @2)))
3642     (mult @0 (root (rdiv @2 @1)))))
3644  /* Simplify x/expN(y) into x*expN(-y).  */
3645  (for exps (EXP EXP2 EXP10 POW10)
3646   (simplify
3647    (rdiv @0 (exps:s @1))
3648     (mult @0 (exps (negate @1)))))
3650  (for logs (LOG LOG2 LOG10 LOG10)
3651       exps (EXP EXP2 EXP10 POW10)
3652   /* logN(expN(x)) -> x.  */
3653   (simplify
3654    (logs (exps @0))
3655    @0)
3656   /* expN(logN(x)) -> x.  */
3657   (simplify
3658    (exps (logs @0))
3659    @0))
3661  /* Optimize logN(func()) for various exponential functions.  We
3662     want to determine the value "x" and the power "exponent" in
3663     order to transform logN(x**exponent) into exponent*logN(x).  */
3664  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
3665       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
3666   (simplify
3667    (logs (exps @0))
3668    (if (SCALAR_FLOAT_TYPE_P (type))
3669     (with {
3670       tree x;
3671       switch (exps)
3672         {
3673         CASE_CFN_EXP:
3674           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
3675           x = build_real_truncate (type, dconst_e ());
3676           break;
3677         CASE_CFN_EXP2:
3678           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
3679           x = build_real (type, dconst2);
3680           break;
3681         CASE_CFN_EXP10:
3682         CASE_CFN_POW10:
3683           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
3684           {
3685             REAL_VALUE_TYPE dconst10;
3686             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3687             x = build_real (type, dconst10);
3688           }
3689           break;
3690         default:
3691           gcc_unreachable ();
3692         }
3693       }
3694      (mult (logs { x; }) @0)))))
3696  (for logs (LOG LOG
3697             LOG2 LOG2
3698             LOG10 LOG10)
3699       exps (SQRT CBRT)
3700   (simplify
3701    (logs (exps @0))
3702    (if (SCALAR_FLOAT_TYPE_P (type))
3703     (with {
3704       tree x;
3705       switch (exps)
3706         {
3707         CASE_CFN_SQRT:
3708           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
3709           x = build_real (type, dconsthalf);
3710           break;
3711         CASE_CFN_CBRT:
3712           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
3713           x = build_real_truncate (type, dconst_third ());
3714           break;
3715         default:
3716           gcc_unreachable ();
3717         }
3718       }
3719      (mult { x; } (logs @0))))))
3721  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
3722  (for logs (LOG LOG2 LOG10)
3723       pows (POW)
3724   (simplify
3725    (logs (pows @0 @1))
3726    (mult @1 (logs @0))))
3728  /* pow(C,x) -> exp(log(C)*x) if C > 0.  */
3729  (for pows (POW)
3730       exps (EXP)
3731       logs (LOG)
3732   (simplify
3733    (pows REAL_CST@0 @1)
3734     (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3735          && real_isfinite (TREE_REAL_CST_PTR (@0)))
3736      (exps (mult (logs @0) @1)))))
3738  (for sqrts (SQRT)
3739       cbrts (CBRT)
3740       pows (POW)
3741       exps (EXP EXP2 EXP10 POW10)
3742   /* sqrt(expN(x)) -> expN(x*0.5).  */
3743   (simplify
3744    (sqrts (exps @0))
3745    (exps (mult @0 { build_real (type, dconsthalf); })))
3746   /* cbrt(expN(x)) -> expN(x/3).  */
3747   (simplify
3748    (cbrts (exps @0))
3749    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3750   /* pow(expN(x), y) -> expN(x*y).  */
3751   (simplify
3752    (pows (exps @0) @1)
3753    (exps (mult @0 @1))))
3755  /* tan(atan(x)) -> x.  */
3756  (for tans (TAN)
3757       atans (ATAN)
3758   (simplify
3759    (tans (atans @0))
3760    @0)))
3762 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
3763 (simplify
3764  (CABS (complex:C @0 real_zerop@1))
3765  (abs @0))
3767 /* trunc(trunc(x)) -> trunc(x), etc.  */
3768 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3769  (simplify
3770   (fns (fns @0))
3771   (fns @0)))
3772 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
3773 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3774  (simplify
3775   (fns integer_valued_real_p@0)
3776   @0))
3778 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
3779 (simplify
3780  (HYPOT:c @0 real_zerop@1)
3781  (abs @0))
3783 /* pow(1,x) -> 1.  */
3784 (simplify
3785  (POW real_onep@0 @1)
3786  @0)
3788 (simplify
3789  /* copysign(x,x) -> x.  */
3790  (COPYSIGN @0 @0)
3791  @0)
3793 (simplify
3794  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
3795  (COPYSIGN @0 tree_expr_nonnegative_p@1)
3796  (abs @0))
3798 (for scale (LDEXP SCALBN SCALBLN)
3799  /* ldexp(0, x) -> 0.  */
3800  (simplify
3801   (scale real_zerop@0 @1)
3802   @0)
3803  /* ldexp(x, 0) -> x.  */
3804  (simplify
3805   (scale @0 integer_zerop@1)
3806   @0)
3807  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
3808  (simplify
3809   (scale REAL_CST@0 @1)
3810   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3811    @0)))
3813 /* Canonicalization of sequences of math builtins.  These rules represent
3814    IL simplifications but are not necessarily optimizations.
3816    The sincos pass is responsible for picking "optimal" implementations
3817    of math builtins, which may be more complicated and can sometimes go
3818    the other way, e.g. converting pow into a sequence of sqrts.
3819    We only want to do these canonicalizations before the pass has run.  */
3821 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3822  /* Simplify tan(x) * cos(x) -> sin(x). */
3823  (simplify
3824   (mult:c (TAN:s @0) (COS:s @0))
3825    (SIN @0))
3827  /* Simplify x * pow(x,c) -> pow(x,c+1). */
3828  (simplify
3829   (mult:c @0 (POW:s @0 REAL_CST@1))
3830   (if (!TREE_OVERFLOW (@1))
3831    (POW @0 (plus @1 { build_one_cst (type); }))))
3833  /* Simplify sin(x) / cos(x) -> tan(x). */
3834  (simplify
3835   (rdiv (SIN:s @0) (COS:s @0))
3836    (TAN @0))
3838  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3839  (simplify
3840   (rdiv (COS:s @0) (SIN:s @0))
3841    (rdiv { build_one_cst (type); } (TAN @0)))
3843  /* Simplify sin(x) / tan(x) -> cos(x). */
3844  (simplify
3845   (rdiv (SIN:s @0) (TAN:s @0))
3846   (if (! HONOR_NANS (@0)
3847        && ! HONOR_INFINITIES (@0))
3848    (COS @0)))
3850  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3851  (simplify
3852   (rdiv (TAN:s @0) (SIN:s @0))
3853   (if (! HONOR_NANS (@0)
3854        && ! HONOR_INFINITIES (@0))
3855    (rdiv { build_one_cst (type); } (COS @0))))
3857  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3858  (simplify
3859   (mult (POW:s @0 @1) (POW:s @0 @2))
3860    (POW @0 (plus @1 @2)))
3862  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3863  (simplify
3864   (mult (POW:s @0 @1) (POW:s @2 @1))
3865    (POW (mult @0 @2) @1))
3867  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3868  (simplify
3869   (mult (POWI:s @0 @1) (POWI:s @2 @1))
3870    (POWI (mult @0 @2) @1))
3872  /* Simplify pow(x,c) / x -> pow(x,c-1). */
3873  (simplify
3874   (rdiv (POW:s @0 REAL_CST@1) @0)
3875   (if (!TREE_OVERFLOW (@1))
3876    (POW @0 (minus @1 { build_one_cst (type); }))))
3878  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3879  (simplify
3880   (rdiv @0 (POW:s @1 @2))
3881    (mult @0 (POW @1 (negate @2))))
3883  (for sqrts (SQRT)
3884       cbrts (CBRT)
3885       pows (POW)
3886   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
3887   (simplify
3888    (sqrts (sqrts @0))
3889    (pows @0 { build_real (type, dconst_quarter ()); }))
3890   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
3891   (simplify
3892    (sqrts (cbrts @0))
3893    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3894   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
3895   (simplify
3896    (cbrts (sqrts @0))
3897    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3898   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
3899   (simplify
3900    (cbrts (cbrts tree_expr_nonnegative_p@0))
3901    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3902   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
3903   (simplify
3904    (sqrts (pows @0 @1))
3905    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3906   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
3907   (simplify
3908    (cbrts (pows tree_expr_nonnegative_p@0 @1))
3909    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3910   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
3911   (simplify
3912    (pows (sqrts @0) @1)
3913    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3914   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
3915   (simplify
3916    (pows (cbrts tree_expr_nonnegative_p@0) @1)
3917    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3918   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
3919   (simplify
3920    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3921    (pows @0 (mult @1 @2))))
3923  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
3924  (simplify
3925   (CABS (complex @0 @0))
3926   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3928  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
3929  (simplify
3930   (HYPOT @0 @0)
3931   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3933  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
3934  (for cexps (CEXP)
3935       exps (EXP)
3936       cexpis (CEXPI)
3937   (simplify
3938    (cexps compositional_complex@0)
3939    (if (targetm.libc_has_function (function_c99_math_complex))
3940     (complex
3941      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3942      (mult @1 (imagpart @2)))))))
3944 (if (canonicalize_math_p ())
3945  /* floor(x) -> trunc(x) if x is nonnegative.  */
3946  (for floors (FLOOR)
3947       truncs (TRUNC)
3948   (simplify
3949    (floors tree_expr_nonnegative_p@0)
3950    (truncs @0))))
3952 (match double_value_p
3953  @0
3954  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3955 (for froms (BUILT_IN_TRUNCL
3956             BUILT_IN_FLOORL
3957             BUILT_IN_CEILL
3958             BUILT_IN_ROUNDL
3959             BUILT_IN_NEARBYINTL
3960             BUILT_IN_RINTL)
3961      tos (BUILT_IN_TRUNC
3962           BUILT_IN_FLOOR
3963           BUILT_IN_CEIL
3964           BUILT_IN_ROUND
3965           BUILT_IN_NEARBYINT
3966           BUILT_IN_RINT)
3967  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
3968  (if (optimize && canonicalize_math_p ())
3969   (simplify
3970    (froms (convert double_value_p@0))
3971    (convert (tos @0)))))
3973 (match float_value_p
3974  @0
3975  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3976 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3977             BUILT_IN_FLOORL BUILT_IN_FLOOR
3978             BUILT_IN_CEILL BUILT_IN_CEIL
3979             BUILT_IN_ROUNDL BUILT_IN_ROUND
3980             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3981             BUILT_IN_RINTL BUILT_IN_RINT)
3982      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3983           BUILT_IN_FLOORF BUILT_IN_FLOORF
3984           BUILT_IN_CEILF BUILT_IN_CEILF
3985           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3986           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3987           BUILT_IN_RINTF BUILT_IN_RINTF)
3988  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3989     if x is a float.  */
3990  (if (optimize && canonicalize_math_p ()
3991       && targetm.libc_has_function (function_c99_misc))
3992   (simplify
3993    (froms (convert float_value_p@0))
3994    (convert (tos @0)))))
3996 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3997      tos (XFLOOR XCEIL XROUND XRINT)
3998  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
3999  (if (optimize && canonicalize_math_p ())
4000   (simplify
4001    (froms (convert double_value_p@0))
4002    (tos @0))))
4004 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4005             XFLOOR XCEIL XROUND XRINT)
4006      tos (XFLOORF XCEILF XROUNDF XRINTF)
4007  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4008     if x is a float.  */
4009  (if (optimize && canonicalize_math_p ())
4010   (simplify
4011    (froms (convert float_value_p@0))
4012    (tos @0))))
4014 (if (canonicalize_math_p ())
4015  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
4016  (for floors (IFLOOR LFLOOR LLFLOOR)
4017   (simplify
4018    (floors tree_expr_nonnegative_p@0)
4019    (fix_trunc @0))))
4021 (if (canonicalize_math_p ())
4022  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
4023  (for fns (IFLOOR LFLOOR LLFLOOR
4024            ICEIL LCEIL LLCEIL
4025            IROUND LROUND LLROUND)
4026   (simplify
4027    (fns integer_valued_real_p@0)
4028    (fix_trunc @0)))
4029  (if (!flag_errno_math)
4030   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
4031   (for rints (IRINT LRINT LLRINT)
4032    (simplify
4033     (rints integer_valued_real_p@0)
4034     (fix_trunc @0)))))
4036 (if (canonicalize_math_p ())
4037  (for ifn (IFLOOR ICEIL IROUND IRINT)
4038       lfn (LFLOOR LCEIL LROUND LRINT)
4039       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4040   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4041      sizeof (int) == sizeof (long).  */
4042   (if (TYPE_PRECISION (integer_type_node)
4043        == TYPE_PRECISION (long_integer_type_node))
4044    (simplify
4045     (ifn @0)
4046     (lfn:long_integer_type_node @0)))
4047   /* Canonicalize llround (x) to lround (x) on LP64 targets where
4048      sizeof (long long) == sizeof (long).  */
4049   (if (TYPE_PRECISION (long_long_integer_type_node)
4050        == TYPE_PRECISION (long_integer_type_node))
4051    (simplify
4052     (llfn @0)
4053     (lfn:long_integer_type_node @0)))))
4055 /* cproj(x) -> x if we're ignoring infinities.  */
4056 (simplify
4057  (CPROJ @0)
4058  (if (!HONOR_INFINITIES (type))
4059    @0))
4061 /* If the real part is inf and the imag part is known to be
4062    nonnegative, return (inf + 0i).  */
4063 (simplify
4064  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4065  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4066   { build_complex_inf (type, false); }))
4068 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
4069 (simplify
4070  (CPROJ (complex @0 REAL_CST@1))
4071  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4072   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4074 (for pows (POW)
4075      sqrts (SQRT)
4076      cbrts (CBRT)
4077  (simplify
4078   (pows @0 REAL_CST@1)
4079   (with {
4080     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4081     REAL_VALUE_TYPE tmp;
4082    }
4083    (switch
4084     /* pow(x,0) -> 1.  */
4085     (if (real_equal (value, &dconst0))
4086      { build_real (type, dconst1); })
4087     /* pow(x,1) -> x.  */
4088     (if (real_equal (value, &dconst1))
4089      @0)
4090     /* pow(x,-1) -> 1/x.  */
4091     (if (real_equal (value, &dconstm1))
4092      (rdiv { build_real (type, dconst1); } @0))
4093     /* pow(x,0.5) -> sqrt(x).  */
4094     (if (flag_unsafe_math_optimizations
4095          && canonicalize_math_p ()
4096          && real_equal (value, &dconsthalf))
4097      (sqrts @0))
4098     /* pow(x,1/3) -> cbrt(x).  */
4099     (if (flag_unsafe_math_optimizations
4100          && canonicalize_math_p ()
4101          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4102              real_equal (value, &tmp)))
4103      (cbrts @0))))))
4105 /* powi(1,x) -> 1.  */
4106 (simplify
4107  (POWI real_onep@0 @1)
4108  @0)
4110 (simplify
4111  (POWI @0 INTEGER_CST@1)
4112  (switch
4113   /* powi(x,0) -> 1.  */
4114   (if (wi::to_wide (@1) == 0)
4115    { build_real (type, dconst1); })
4116   /* powi(x,1) -> x.  */
4117   (if (wi::to_wide (@1) == 1)
4118    @0)
4119   /* powi(x,-1) -> 1/x.  */
4120   (if (wi::to_wide (@1) == -1)
4121    (rdiv { build_real (type, dconst1); } @0))))
4123 /* Narrowing of arithmetic and logical operations. 
4125    These are conceptually similar to the transformations performed for
4126    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
4127    term we want to move all that code out of the front-ends into here.  */
4129 /* If we have a narrowing conversion of an arithmetic operation where
4130    both operands are widening conversions from the same type as the outer
4131    narrowing conversion.  Then convert the innermost operands to a suitable
4132    unsigned type (to avoid introducing undefined behavior), perform the
4133    operation and convert the result to the desired type.  */
4134 (for op (plus minus)
4135   (simplify
4136     (convert (op:s (convert@2 @0) (convert?@3 @1)))
4137     (if (INTEGRAL_TYPE_P (type)
4138          /* We check for type compatibility between @0 and @1 below,
4139             so there's no need to check that @1/@3 are integral types.  */
4140          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4141          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4142          /* The precision of the type of each operand must match the
4143             precision of the mode of each operand, similarly for the
4144             result.  */
4145          && type_has_mode_precision_p (TREE_TYPE (@0))
4146          && type_has_mode_precision_p (TREE_TYPE (@1))
4147          && type_has_mode_precision_p (type)
4148          /* The inner conversion must be a widening conversion.  */
4149          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4150          && types_match (@0, type)
4151          && (types_match (@0, @1)
4152              /* Or the second operand is const integer or converted const
4153                 integer from valueize.  */
4154              || TREE_CODE (@1) == INTEGER_CST))
4155       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4156         (op @0 (convert @1))
4157         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4158          (convert (op (convert:utype @0)
4159                       (convert:utype @1))))))))
4161 /* This is another case of narrowing, specifically when there's an outer
4162    BIT_AND_EXPR which masks off bits outside the type of the innermost
4163    operands.   Like the previous case we have to convert the operands
4164    to unsigned types to avoid introducing undefined behavior for the
4165    arithmetic operation.  */
4166 (for op (minus plus)
4167  (simplify
4168   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4169   (if (INTEGRAL_TYPE_P (type)
4170        /* We check for type compatibility between @0 and @1 below,
4171           so there's no need to check that @1/@3 are integral types.  */
4172        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4173        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4174        /* The precision of the type of each operand must match the
4175           precision of the mode of each operand, similarly for the
4176           result.  */
4177        && type_has_mode_precision_p (TREE_TYPE (@0))
4178        && type_has_mode_precision_p (TREE_TYPE (@1))
4179        && type_has_mode_precision_p (type)
4180        /* The inner conversion must be a widening conversion.  */
4181        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4182        && types_match (@0, @1)
4183        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4184            <= TYPE_PRECISION (TREE_TYPE (@0)))
4185        && (wi::to_wide (@4)
4186            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4187                        true, TYPE_PRECISION (type))) == 0)
4188    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4189     (with { tree ntype = TREE_TYPE (@0); }
4190      (convert (bit_and (op @0 @1) (convert:ntype @4))))
4191     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4192      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4193                (convert:utype @4))))))))
4195 /* Transform (@0 < @1 and @0 < @2) to use min, 
4196    (@0 > @1 and @0 > @2) to use max */
4197 (for op (lt le gt ge)
4198      ext (min min max max)
4199  (simplify
4200   (bit_and (op:cs @0 @1) (op:cs @0 @2))
4201   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4202        && TREE_CODE (@0) != INTEGER_CST)
4203    (op @0 (ext @1 @2)))))
4205 (simplify
4206  /* signbit(x) -> 0 if x is nonnegative.  */
4207  (SIGNBIT tree_expr_nonnegative_p@0)
4208  { integer_zero_node; })
4210 (simplify
4211  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
4212  (SIGNBIT @0)
4213  (if (!HONOR_SIGNED_ZEROS (@0))
4214   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4216 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
4217 (for cmp (eq ne)
4218  (for op (plus minus)
4219       rop (minus plus)
4220   (simplify
4221    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4222    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4223         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4224         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4225         && !TYPE_SATURATING (TREE_TYPE (@0)))
4226     (with { tree res = int_const_binop (rop, @2, @1); }
4227      (if (TREE_OVERFLOW (res)
4228           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4229       { constant_boolean_node (cmp == NE_EXPR, type); }
4230       (if (single_use (@3))
4231        (cmp @0 { res; }))))))))
4232 (for cmp (lt le gt ge)
4233  (for op (plus minus)
4234       rop (minus plus)
4235   (simplify
4236    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4237    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4238         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4239     (with { tree res = int_const_binop (rop, @2, @1); }
4240      (if (TREE_OVERFLOW (res))
4241       {
4242         fold_overflow_warning (("assuming signed overflow does not occur "
4243                                 "when simplifying conditional to constant"),
4244                                WARN_STRICT_OVERFLOW_CONDITIONAL);
4245         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4246         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
4247         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4248                                   TYPE_SIGN (TREE_TYPE (@1)))
4249                         != (op == MINUS_EXPR);
4250         constant_boolean_node (less == ovf_high, type);
4251       }
4252       (if (single_use (@3))
4253        (with
4254         {
4255           fold_overflow_warning (("assuming signed overflow does not occur "
4256                                   "when changing X +- C1 cmp C2 to "
4257                                   "X cmp C2 -+ C1"),
4258                                  WARN_STRICT_OVERFLOW_COMPARISON);
4259         }
4260         (cmp @0 { res; })))))))))
4262 /* Canonicalizations of BIT_FIELD_REFs.  */
4264 (simplify
4265  (BIT_FIELD_REF @0 @1 @2)
4266  (switch
4267   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4268        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4269    (switch
4270     (if (integer_zerop (@2))
4271      (view_convert (realpart @0)))
4272     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4273      (view_convert (imagpart @0)))))
4274   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4275        && INTEGRAL_TYPE_P (type)
4276        /* On GIMPLE this should only apply to register arguments.  */
4277        && (! GIMPLE || is_gimple_reg (@0))
4278        /* A bit-field-ref that referenced the full argument can be stripped.  */
4279        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4280             && integer_zerop (@2))
4281            /* Low-parts can be reduced to integral conversions.
4282               ???  The following doesn't work for PDP endian.  */
4283            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4284                /* Don't even think about BITS_BIG_ENDIAN.  */
4285                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4286                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4287                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4288                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
4289                                             - TYPE_PRECISION (type))
4290                                          : 0)) == 0)))
4291    (convert @0))))
4293 /* Simplify vector extracts.  */
4295 (simplify
4296  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4297  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4298       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4299           || (VECTOR_TYPE_P (type)
4300               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4301   (with
4302    {
4303      tree ctor = (TREE_CODE (@0) == SSA_NAME
4304                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4305      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4306      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4307      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4308      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4309    }
4310    (if (n != 0
4311         && (idx % width) == 0
4312         && (n % width) == 0
4313         && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4314     (with
4315      {
4316        idx = idx / width;
4317        n = n / width;
4318        /* Constructor elements can be subvectors.  */
4319        unsigned HOST_WIDE_INT k = 1;
4320        if (CONSTRUCTOR_NELTS (ctor) != 0)
4321          {
4322            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4323            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4324              k = TYPE_VECTOR_SUBPARTS (cons_elem);
4325          }
4326      }
4327      (switch
4328       /* We keep an exact subset of the constructor elements.  */
4329       (if ((idx % k) == 0 && (n % k) == 0)
4330        (if (CONSTRUCTOR_NELTS (ctor) == 0)
4331         { build_constructor (type, NULL); }
4332         (with
4333          {
4334            idx /= k;
4335            n /= k;
4336          }
4337          (if (n == 1)
4338           (if (idx < CONSTRUCTOR_NELTS (ctor))
4339            { CONSTRUCTOR_ELT (ctor, idx)->value; }
4340            { build_zero_cst (type); })
4341           {
4342             vec<constructor_elt, va_gc> *vals;
4343             vec_alloc (vals, n);
4344             for (unsigned i = 0;
4345                  i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4346               CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4347                                       CONSTRUCTOR_ELT (ctor, idx + i)->value);
4348             build_constructor (type, vals);
4349           }))))
4350       /* The bitfield references a single constructor element.  */
4351       (if (idx + n <= (idx / k + 1) * k)
4352        (switch
4353         (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4354          { build_zero_cst (type); })
4355         (if (n == k)
4356          { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4357         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4358                        @1 { bitsize_int ((idx % k) * width); })))))))))
4360 /* Simplify a bit extraction from a bit insertion for the cases with
4361    the inserted element fully covering the extraction or the insertion
4362    not touching the extraction.  */
4363 (simplify
4364  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4365  (with
4366   {
4367     unsigned HOST_WIDE_INT isize;
4368     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4369       isize = TYPE_PRECISION (TREE_TYPE (@1));
4370     else
4371       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4372   }
4373   (switch
4374    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4375         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4376                       wi::to_wide (@ipos) + isize))
4377     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4378                                                  wi::to_wide (@rpos)
4379                                                  - wi::to_wide (@ipos)); }))
4380    (if (wi::geu_p (wi::to_wide (@ipos),
4381                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
4382         || wi::geu_p (wi::to_wide (@rpos),
4383                       wi::to_wide (@ipos) + isize))
4384     (BIT_FIELD_REF @0 @rsize @rpos)))))