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