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