Update ChangeLog and version files for release
[official-gcc.git] / gcc / match.pd
blobc579781e4c5670cf76c8d9132b372d580dde4be9
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-2016 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    integer_valued_real_p
35    integer_pow2p
36    HONOR_NANS)
38 /* Operator lists.  */
39 (define_operator_list tcc_comparison
40   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
48 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
50 #include "cfn-operators.pd"
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53    where the versions prefixed with "i" return an int, those prefixed with
54    "l" return a long and those prefixed with "ll" return a long long.
56    Also define operand lists:
58      X<FN>F for all float functions, in the order i, l, ll
59      X<FN> for all double functions, in the same order
60      X<FN>L for all long double functions, in the same order.  */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
63                                  BUILT_IN_L##FN##F \
64                                  BUILT_IN_LL##FN##F) \
65   (define_operator_list X##FN BUILT_IN_I##FN \
66                               BUILT_IN_L##FN \
67                               BUILT_IN_LL##FN) \
68   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
69                                  BUILT_IN_L##FN##L \
70                                  BUILT_IN_LL##FN##L)
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77 /* Simplifications of operations with one constant operand and
78    simplifications to constants or single values.  */
80 (for op (plus pointer_plus minus bit_ior bit_xor)
81   (simplify
82     (op @0 integer_zerop)
83     (non_lvalue @0)))
85 /* 0 +p index -> (type)index */
86 (simplify
87  (pointer_plus integer_zerop @1)
88  (non_lvalue (convert @1)))
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91    Likewise if the operands are reversed.  */
92 (simplify
93  (plus:c @0 real_zerop@1)
94  (if (fold_real_zero_addition_p (type, @1, 0))
95   (non_lvalue @0)))
97 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
98 (simplify
99  (minus @0 real_zerop@1)
100  (if (fold_real_zero_addition_p (type, @1, 1))
101   (non_lvalue @0)))
103 /* Simplify x - x.
104    This is unsafe for certain floats even in non-IEEE formats.
105    In IEEE, it is unsafe because it does wrong for NaNs.
106    Also note that operand_equal_p is always false if an operand
107    is volatile.  */
108 (simplify
109  (minus @0 @0)
110  (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111   { build_zero_cst (type); }))
113 (simplify
114  (mult @0 integer_zerop@1)
115  @1)
117 /* Maybe fold x * 0 to 0.  The expressions aren't the same
118    when x is NaN, since x * 0 is also NaN.  Nor are they the
119    same in modes with signed zeros, since multiplying a
120    negative value by 0 gives -0, not +0.  */
121 (simplify
122  (mult @0 real_zerop@1)
123  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
124   @1))
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127    Likewise for complex arithmetic with signed zeros.  */
128 (simplify
129  (mult @0 real_onep)
130  (if (!HONOR_SNANS (type)
131       && (!HONOR_SIGNED_ZEROS (type)
132           || !COMPLEX_FLOAT_TYPE_P (type)))
133   (non_lvalue @0)))
135 /* Transform x * -1.0 into -x.  */
136 (simplify
137  (mult @0 real_minus_onep)
138   (if (!HONOR_SNANS (type)
139        && (!HONOR_SIGNED_ZEROS (type)
140            || !COMPLEX_FLOAT_TYPE_P (type)))
141    (negate @0)))
143 /* Make sure to preserve divisions by zero.  This is the reason why
144    we don't simplify x / x to 1 or 0 / x to 0.  */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
146   (simplify
147     (op @0 integer_onep)
148     (non_lvalue @0)))
150 /* X / -1 is -X.  */
151 (for div (trunc_div ceil_div floor_div round_div exact_div)
152  (simplify
153    (div @0 integer_minus_onep@1)
154    (if (!TYPE_UNSIGNED (type))
155     (negate @0))))
157 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
158    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
159 (simplify
160  (floor_div @0 @1)
161  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
162       && TYPE_UNSIGNED (type))
163   (trunc_div @0 @1)))
165 /* Combine two successive divisions.  Note that combining ceil_div
166    and floor_div is trickier and combining round_div even more so.  */
167 (for div (trunc_div exact_div)
168  (simplify
169   (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
170   (with {
171     bool overflow_p;
172     wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
173    }
174    (if (!overflow_p)
175     (div @0 { wide_int_to_tree (type, mul); })
176     (if (TYPE_UNSIGNED (type)
177          || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
178      { build_zero_cst (type); })))))
180 /* Optimize A / A to 1.0 if we don't care about
181    NaNs or Infinities.  */
182 (simplify
183  (rdiv @0 @0)
184  (if (FLOAT_TYPE_P (type)
185       && ! HONOR_NANS (type)
186       && ! HONOR_INFINITIES (type))
187   { build_one_cst (type); }))
189 /* Optimize -A / A to -1.0 if we don't care about
190    NaNs or Infinities.  */
191 (simplify
192  (rdiv:c @0 (negate @0))
193  (if (FLOAT_TYPE_P (type)
194       && ! HONOR_NANS (type)
195       && ! HONOR_INFINITIES (type))
196   { build_minus_one_cst (type); }))
198 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
199 (simplify
200  (rdiv @0 real_onep)
201  (if (!HONOR_SNANS (type))
202   (non_lvalue @0)))
204 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
205 (simplify
206  (rdiv @0 real_minus_onep)
207  (if (!HONOR_SNANS (type))
208   (negate @0)))
210 (if (flag_reciprocal_math)
211  /* Convert (A/B)/C to A/(B*C)  */
212  (simplify
213   (rdiv (rdiv:s @0 @1) @2)
214    (rdiv @0 (mult @1 @2)))
216  /* Convert A/(B/C) to (A/B)*C  */
217  (simplify
218   (rdiv @0 (rdiv:s @1 @2))
219    (mult (rdiv @0 @1) @2)))
221 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
222 (for div (trunc_div ceil_div floor_div round_div exact_div)
223  (simplify
224   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
225   (if (integer_pow2p (@2)
226        && tree_int_cst_sgn (@2) > 0
227        && wi::add (@2, @1) == 0
228        && tree_nop_conversion_p (type, TREE_TYPE (@0)))
229    (rshift (convert @0) { build_int_cst (integer_type_node,
230                                          wi::exact_log2 (@2)); }))))
232 /* If ARG1 is a constant, we can convert this to a multiply by the
233    reciprocal.  This does not have the same rounding properties,
234    so only do this if -freciprocal-math.  We can actually
235    always safely do it if ARG1 is a power of two, but it's hard to
236    tell if it is or not in a portable manner.  */
237 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
238  (simplify
239   (rdiv @0 cst@1)
240   (if (optimize)
241    (if (flag_reciprocal_math
242         && !real_zerop (@1))
243     (with
244      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
245      (if (tem)
246       (mult @0 { tem; } )))
247     (if (cst != COMPLEX_CST)
248      (with { tree inverse = exact_inverse (type, @1); }
249       (if (inverse)
250        (mult @0 { inverse; } ))))))))
252 /* Same applies to modulo operations, but fold is inconsistent here
253    and simplifies 0 % x to 0, only preserving literal 0 % 0.  */
254 (for mod (ceil_mod floor_mod round_mod trunc_mod)
255  /* 0 % X is always zero.  */
256  (simplify
257   (mod integer_zerop@0 @1)
258   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
259   (if (!integer_zerop (@1))
260    @0))
261  /* X % 1 is always zero.  */
262  (simplify
263   (mod @0 integer_onep)
264   { build_zero_cst (type); })
265  /* X % -1 is zero.  */
266  (simplify
267   (mod @0 integer_minus_onep@1)
268   (if (!TYPE_UNSIGNED (type))
269    { build_zero_cst (type); }))
270  /* (X % Y) % Y is just X % Y.  */
271  (simplify
272   (mod (mod@2 @0 @1) @1)
273   @2)
274  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
275  (simplify
276   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
277   (if (ANY_INTEGRAL_TYPE_P (type)
278        && TYPE_OVERFLOW_UNDEFINED (type)
279        && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
280    { build_zero_cst (type); })))
282 /* X % -C is the same as X % C.  */
283 (simplify
284  (trunc_mod @0 INTEGER_CST@1)
285   (if (TYPE_SIGN (type) == SIGNED
286        && !TREE_OVERFLOW (@1)
287        && wi::neg_p (@1)
288        && !TYPE_OVERFLOW_TRAPS (type)
289        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
290        && !sign_bit_p (@1, @1))
291    (trunc_mod @0 (negate @1))))
293 /* X % -Y is the same as X % Y.  */
294 (simplify
295  (trunc_mod @0 (convert? (negate @1)))
296  (if (INTEGRAL_TYPE_P (type)
297       && !TYPE_UNSIGNED (type)
298       && !TYPE_OVERFLOW_TRAPS (type)
299       && tree_nop_conversion_p (type, TREE_TYPE (@1))
300       /* Avoid this transformation if X might be INT_MIN or
301          Y might be -1, because we would then change valid
302          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
303       && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
304           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
305                                                         (TREE_TYPE (@1))))))
306   (trunc_mod @0 (convert @1))))
308 /* X - (X / Y) * Y is the same as X % Y.  */
309 (simplify
310  (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
311  /* We cannot use matching captures here, since in the case of
312     constants we really want the type of @0, not @2.  */
313  (if (operand_equal_p (@0, @2, 0)
314       && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
315   (convert (trunc_mod @0 @1))))
317 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
318    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
319    Also optimize A % (C << N)  where C is a power of 2,
320    to A & ((C << N) - 1).  */
321 (match (power_of_two_cand @1)
322  INTEGER_CST@1)
323 (match (power_of_two_cand @1)
324  (lshift INTEGER_CST@1 @2))
325 (for mod (trunc_mod floor_mod)
326  (simplify
327   (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
328   (if ((TYPE_UNSIGNED (type)
329         || tree_expr_nonnegative_p (@0))
330         && tree_nop_conversion_p (type, TREE_TYPE (@3))
331         && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
332    (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
334 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
335 (simplify
336  (trunc_div (mult @0 integer_pow2p@1) @1)
337  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
338   (bit_and @0 { wide_int_to_tree
339                 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
340                                  false, TYPE_PRECISION (type))); })))
342 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
343 (simplify
344  (mult (trunc_div @0 integer_pow2p@1) @1)
345  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
346   (bit_and @0 (negate @1))))
348 /* Simplify (t * 2) / 2) -> t.  */
349 (for div (trunc_div ceil_div floor_div round_div exact_div)
350  (simplify
351   (div (mult @0 @1) @1)
352   (if (ANY_INTEGRAL_TYPE_P (type)
353        && TYPE_OVERFLOW_UNDEFINED (type))
354    @0)))
356 (for op (negate abs)
357  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
358  (for coss (COS COSH)
359   (simplify
360    (coss (op @0))
361     (coss @0)))
362  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
363  (for pows (POW)
364   (simplify
365    (pows (op @0) REAL_CST@1)
366    (with { HOST_WIDE_INT n; }
367     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
368      (pows @0 @1)))))
369  /* Strip negate and abs from both operands of hypot.  */
370  (for hypots (HYPOT)
371   (simplify
372    (hypots (op @0) @1)
373    (hypots @0 @1))
374   (simplify
375    (hypots @0 (op @1))
376    (hypots @0 @1)))
377  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
378  (for copysigns (COPYSIGN)
379   (simplify
380    (copysigns (op @0) @1)
381    (copysigns @0 @1))))
383 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
384 (simplify
385  (mult (abs@1 @0) @1)
386  (mult @0 @0))
388 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
389 (for coss (COS COSH)
390      copysigns (COPYSIGN)
391  (simplify
392   (coss (copysigns @0 @1))
393    (coss @0)))
395 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
396 (for pows (POW)
397      copysigns (COPYSIGN)
398  (simplify
399   (pows (copysigns @0 @1) REAL_CST@1)
400   (with { HOST_WIDE_INT n; }
401    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
402     (pows @0 @1)))))
404 (for hypots (HYPOT)
405      copysigns (COPYSIGN)
406  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
407  (simplify
408   (hypots (copysigns @0 @1) @2)
409   (hypots @0 @2))
410  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
411  (simplify
412   (hypots @0 (copysigns @1 @2))
413   (hypots @0 @1)))
415 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
416 (for copysigns (COPYSIGN)
417  (simplify
418   (copysigns (copysigns @0 @1) @2)
419   (copysigns @0 @2)))
421 /* copysign(x,y)*copysign(x,y) -> x*x.  */
422 (for copysigns (COPYSIGN)
423  (simplify
424   (mult (copysigns@2 @0 @1) @2)
425   (mult @0 @0)))
427 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
428 (for ccoss (CCOS CCOSH)
429  (simplify
430   (ccoss (negate @0))
431    (ccoss @0)))
433 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
434 (for ops (conj negate)
435  (for cabss (CABS)
436   (simplify
437    (cabss (ops @0))
438    (cabss @0))))
440 /* Fold (a * (1 << b)) into (a << b)  */
441 (simplify
442  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
443   (if (! FLOAT_TYPE_P (type)
444        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
445    (lshift @0 @2)))
447 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
448 (simplify
449  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
450   (if (flag_associative_math
451        && single_use (@3))
452    (with
453     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
454     (if (tem)
455      (rdiv { tem; } @1)))))
457 /* Convert C1/(X*C2) into (C1/C2)/X  */
458 (simplify
459  (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
460   (if (flag_reciprocal_math)
461    (with
462     { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
463     (if (tem)
464      (rdiv { tem; } @1)))))
466 /* Simplify ~X & X as zero.  */
467 (simplify
468  (bit_and:c (convert? @0) (convert? (bit_not @0)))
469   { build_zero_cst (type); })
471 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
472 (simplify
473  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
474   (minus (bit_xor @0 @1) @1))
475 (simplify
476  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
477  (if (wi::bit_not (@2) == @1)
478   (minus (bit_xor @0 @1) @1)))
480 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
481 (simplify
482  (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
483   (minus @1 (bit_xor @0 @1)))
485 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y.  */
486 (simplify
487  (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
488   (bit_xor @0 @1))
489 (simplify
490  (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
491  (if (wi::bit_not (@2) == @1)
492   (bit_xor @0 @1)))
494 /* X % Y is smaller than Y.  */
495 (for cmp (lt ge)
496  (simplify
497   (cmp (trunc_mod @0 @1) @1)
498   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
499    { constant_boolean_node (cmp == LT_EXPR, type); })))
500 (for cmp (gt le)
501  (simplify
502   (cmp @1 (trunc_mod @0 @1))
503   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
504    { constant_boolean_node (cmp == GT_EXPR, type); })))
506 /* x | ~0 -> ~0  */
507 (simplify
508   (bit_ior @0 integer_all_onesp@1)
509   @1)
511 /* x & 0 -> 0  */
512 (simplify
513   (bit_and @0 integer_zerop@1)
514   @1)
516 /* ~x | x -> -1 */
517 /* ~x ^ x -> -1 */
518 /* ~x + x -> -1 */
519 (for op (bit_ior bit_xor plus)
520  (simplify
521   (op:c (convert? @0) (convert? (bit_not @0)))
522   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
524 /* x ^ x -> 0 */
525 (simplify
526   (bit_xor @0 @0)
527   { build_zero_cst (type); })
529 /* Canonicalize X ^ ~0 to ~X.  */
530 (simplify
531   (bit_xor @0 integer_all_onesp@1)
532   (bit_not @0))
534 /* x & ~0 -> x  */
535 (simplify
536  (bit_and @0 integer_all_onesp)
537   (non_lvalue @0))
539 /* x & x -> x,  x | x -> x  */
540 (for bitop (bit_and bit_ior)
541  (simplify
542   (bitop @0 @0)
543   (non_lvalue @0)))
545 /* x + (x & 1) -> (x + 1) & ~1 */
546 (simplify
547  (plus:c @0 (bit_and:s @0 integer_onep@1))
548  (bit_and (plus @0 @1) (bit_not @1)))
550 /* x & ~(x & y) -> x & ~y */
551 /* x | ~(x | y) -> x | ~y  */
552 (for bitop (bit_and bit_ior)
553  (simplify
554   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
555   (bitop @0 (bit_not @1))))
557 /* (x | y) & ~x -> y & ~x */
558 /* (x & y) | ~x -> y | ~x */
559 (for bitop (bit_and bit_ior)
560      rbitop (bit_ior bit_and)
561  (simplify
562   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
563   (bitop @1 @2)))
565 /* (x & y) ^ (x | y) -> x ^ y */
566 (simplify
567  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
568  (bit_xor @0 @1))
570 /* (x ^ y) ^ (x | y) -> x & y */
571 (simplify
572  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
573  (bit_and @0 @1))
575 /* (x & y) + (x ^ y) -> x | y */
576 /* (x & y) | (x ^ y) -> x | y */
577 /* (x & y) ^ (x ^ y) -> x | y */
578 (for op (plus bit_ior bit_xor)
579  (simplify
580   (op:c (bit_and @0 @1) (bit_xor @0 @1))
581   (bit_ior @0 @1)))
583 /* (x & y) + (x | y) -> x + y */
584 (simplify
585  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
586  (plus @0 @1))
588 /* (x + y) - (x | y) -> x & y */
589 (simplify
590  (minus (plus @0 @1) (bit_ior @0 @1))
591  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
592       && !TYPE_SATURATING (type))
593   (bit_and @0 @1)))
595 /* (x + y) - (x & y) -> x | y */
596 (simplify
597  (minus (plus @0 @1) (bit_and @0 @1))
598  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
599       && !TYPE_SATURATING (type))
600   (bit_ior @0 @1)))
602 /* (x | y) - (x ^ y) -> x & y */
603 (simplify
604  (minus (bit_ior @0 @1) (bit_xor @0 @1))
605  (bit_and @0 @1))
607 /* (x | y) - (x & y) -> x ^ y */
608 (simplify
609  (minus (bit_ior @0 @1) (bit_and @0 @1))
610  (bit_xor @0 @1))
612 /* (x | y) & ~(x & y) -> x ^ y */
613 (simplify
614  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
615  (bit_xor @0 @1))
617 /* (x | y) & (~x ^ y) -> x & y */
618 (simplify
619  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
620  (bit_and @0 @1))
622 /* ~x & ~y -> ~(x | y)
623    ~x | ~y -> ~(x & y) */
624 (for op (bit_and bit_ior)
625      rop (bit_ior bit_and)
626  (simplify
627   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
628   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
629        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
630    (bit_not (rop (convert @0) (convert @1))))))
632 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
633    with a constant, and the two constants have no bits in common,
634    we should treat this as a BIT_IOR_EXPR since this may produce more
635    simplifications.  */
636 (for op (bit_xor plus)
637  (simplify
638   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
639       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
640   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
641        && tree_nop_conversion_p (type, TREE_TYPE (@2))
642        && wi::bit_and (@1, @3) == 0)
643    (bit_ior (convert @4) (convert @5)))))
645 /* (X | Y) ^ X -> Y & ~ X*/
646 (simplify
647  (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
648  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
649   (convert (bit_and @1 (bit_not @0)))))
651 /* Convert ~X ^ ~Y to X ^ Y.  */
652 (simplify
653  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
654  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
655       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
656   (bit_xor (convert @0) (convert @1))))
658 /* Convert ~X ^ C to X ^ ~C.  */
659 (simplify
660  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
661  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
662   (bit_xor (convert @0) (bit_not @1))))
664 /* Fold (X & Y) ^ Y as ~X & Y.  */
665 (simplify
666  (bit_xor:c (bit_and:c @0 @1) @1)
667  (bit_and (bit_not @0) @1))
669 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
670    operands are another bit-wise operation with a common input.  If so,
671    distribute the bit operations to save an operation and possibly two if
672    constants are involved.  For example, convert
673      (A | B) & (A | C) into A | (B & C)
674    Further simplification will occur if B and C are constants.  */
675 (for op (bit_and bit_ior)
676      rop (bit_ior bit_and)
677  (simplify
678   (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
679   (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
680    (rop (convert @0) (op (convert @1) (convert @2))))))
683 (simplify
684  (abs (abs@1 @0))
685  @1)
686 (simplify
687  (abs (negate @0))
688  (abs @0))
689 (simplify
690  (abs tree_expr_nonnegative_p@0)
691  @0)
693 /* A few cases of fold-const.c negate_expr_p predicate.  */
694 (match negate_expr_p
695  INTEGER_CST
696  (if ((INTEGRAL_TYPE_P (type)
697        && TYPE_OVERFLOW_WRAPS (type))
698       || (!TYPE_OVERFLOW_SANITIZED (type)
699           && may_negate_without_overflow_p (t)))))
700 (match negate_expr_p
701  FIXED_CST)
702 (match negate_expr_p
703  (negate @0)
704  (if (!TYPE_OVERFLOW_SANITIZED (type))))
705 (match negate_expr_p
706  REAL_CST
707  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
708 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
709    ways.  */
710 (match negate_expr_p
711  VECTOR_CST
712  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
714 /* (-A) * (-B) -> A * B  */
715 (simplify
716  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
717   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
718        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
719    (mult (convert @0) (convert (negate @1)))))
721 /* -(A + B) -> (-B) - A.  */
722 (simplify
723  (negate (plus:c @0 negate_expr_p@1))
724  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
725       && !HONOR_SIGNED_ZEROS (element_mode (type)))
726   (minus (negate @1) @0)))
728 /* A - B -> A + (-B) if B is easily negatable.  */
729 (simplify
730  (minus @0 negate_expr_p@1)
731  (if (!FIXED_POINT_TYPE_P (type))
732  (plus @0 (negate @1))))
734 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
735    when profitable.
736    For bitwise binary operations apply operand conversions to the
737    binary operation result instead of to the operands.  This allows
738    to combine successive conversions and bitwise binary operations.
739    We combine the above two cases by using a conditional convert.  */
740 (for bitop (bit_and bit_ior bit_xor)
741  (simplify
742   (bitop (convert @0) (convert? @1))
743   (if (((TREE_CODE (@1) == INTEGER_CST
744          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
745          && int_fits_type_p (@1, TREE_TYPE (@0)))
746         || types_match (@0, @1))
747        /* ???  This transform conflicts with fold-const.c doing
748           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
749           constants (if x has signed type, the sign bit cannot be set
750           in c).  This folds extension into the BIT_AND_EXPR.
751           Restrict it to GIMPLE to avoid endless recursions.  */
752        && (bitop != BIT_AND_EXPR || GIMPLE)
753        && (/* That's a good idea if the conversion widens the operand, thus
754               after hoisting the conversion the operation will be narrower.  */
755            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
756            /* It's also a good idea if the conversion is to a non-integer
757               mode.  */
758            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
759            /* Or if the precision of TO is not the same as the precision
760               of its mode.  */
761            || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
762    (convert (bitop @0 (convert @1))))))
764 (for bitop (bit_and bit_ior)
765      rbitop (bit_ior bit_and)
766   /* (x | y) & x -> x */
767   /* (x & y) | x -> x */
768  (simplify
769   (bitop:c (rbitop:c @0 @1) @0)
770   @0)
771  /* (~x | y) & x -> x & y */
772  /* (~x & y) | x -> x | y */
773  (simplify
774   (bitop:c (rbitop:c (bit_not @0) @1) @0)
775   (bitop @0 @1)))
777 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
778 (for bitop (bit_and bit_ior bit_xor)
779  (simplify
780   (bitop (bit_and:c @0 @1) (bit_and @2 @1))
781   (bit_and (bitop @0 @2) @1)))
783 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
784 (simplify
785   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
786   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
788 /* Combine successive equal operations with constants.  */
789 (for bitop (bit_and bit_ior bit_xor)
790  (simplify
791   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
792   (bitop @0 (bitop @1 @2))))
794 /* Try simple folding for X op !X, and X op X with the help
795    of the truth_valued_p and logical_inverted_value predicates.  */
796 (match truth_valued_p
797  @0
798  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
799 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
800  (match truth_valued_p
801   (op @0 @1)))
802 (match truth_valued_p
803   (truth_not @0))
805 (match (logical_inverted_value @0)
806  (truth_not @0))
807 (match (logical_inverted_value @0)
808  (bit_not truth_valued_p@0))
809 (match (logical_inverted_value @0)
810  (eq @0 integer_zerop))
811 (match (logical_inverted_value @0)
812  (ne truth_valued_p@0 integer_truep))
813 (match (logical_inverted_value @0)
814  (bit_xor truth_valued_p@0 integer_truep))
816 /* X & !X -> 0.  */
817 (simplify
818  (bit_and:c @0 (logical_inverted_value @0))
819  { build_zero_cst (type); })
820 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
821 (for op (bit_ior bit_xor)
822  (simplify
823   (op:c truth_valued_p@0 (logical_inverted_value @0))
824   { constant_boolean_node (true, type); }))
825 /* X ==/!= !X is false/true.  */
826 (for op (eq ne)
827  (simplify
828   (op:c truth_valued_p@0 (logical_inverted_value @0))
829   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
831 /* ~~x -> x */
832 (simplify
833   (bit_not (bit_not @0))
834   @0)
836 /* Convert ~ (-A) to A - 1.  */
837 (simplify
838  (bit_not (convert? (negate @0)))
839  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
840   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
842 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
843 (simplify
844  (bit_not (convert? (minus @0 integer_each_onep)))
845  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
846   (convert (negate @0))))
847 (simplify
848  (bit_not (convert? (plus @0 integer_all_onesp)))
849  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
850   (convert (negate @0))))
852 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
853 (simplify
854  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
855  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
856   (convert (bit_xor @0 (bit_not @1)))))
857 (simplify
858  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
859  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
860   (convert (bit_xor @0 @1))))
862 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
863 (simplify
864  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
865  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
867 /* Fold A - (A & B) into ~B & A.  */
868 (simplify
869  (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
870  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
871       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
872   (convert (bit_and (bit_not @1) @0))))
876 /* ((X inner_op C0) outer_op C1)
877    With X being a tree where value_range has reasoned certain bits to always be
878    zero throughout its computed value range,
879    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
880    where zero_mask has 1's for all bits that are sure to be 0 in
881    and 0's otherwise.
882    if (inner_op == '^') C0 &= ~C1;
883    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
884    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
886 (for inner_op (bit_ior bit_xor)
887      outer_op (bit_xor bit_ior)
888 (simplify
889  (outer_op
890   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
891  (with
892   {
893     bool fail = false;
894     wide_int zero_mask_not;
895     wide_int C0;
896     wide_int cst_emit;
898     if (TREE_CODE (@2) == SSA_NAME)
899       zero_mask_not = get_nonzero_bits (@2);
900     else
901       fail = true;
903     if (inner_op == BIT_XOR_EXPR)
904       {
905         C0 = wi::bit_and_not (@0, @1);
906         cst_emit = wi::bit_or (C0, @1);
907       }
908     else
909       {
910         C0 = @0;
911         cst_emit = wi::bit_xor (@0, @1);
912       }
913   }
914   (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
915    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
916    (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
917     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
919 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
920 (simplify
921   (pointer_plus (pointer_plus:s @0 @1) @3)
922   (pointer_plus @0 (plus @1 @3)))
924 /* Pattern match
925      tem1 = (long) ptr1;
926      tem2 = (long) ptr2;
927      tem3 = tem2 - tem1;
928      tem4 = (unsigned long) tem3;
929      tem5 = ptr1 + tem4;
930    and produce
931      tem5 = ptr2;  */
932 (simplify
933   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
934   /* Conditionally look through a sign-changing conversion.  */
935   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
936        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
937             || (GENERIC && type == TREE_TYPE (@1))))
938    @1))
940 /* Pattern match
941      tem = (sizetype) ptr;
942      tem = tem & algn;
943      tem = -tem;
944      ... = ptr p+ tem;
945    and produce the simpler and easier to analyze with respect to alignment
946      ... = ptr & ~algn;  */
947 (simplify
948   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
949   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
950    (bit_and @0 { algn; })))
952 /* Try folding difference of addresses.  */
953 (simplify
954  (minus (convert ADDR_EXPR@0) (convert @1))
955  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
956   (with { HOST_WIDE_INT diff; }
957    (if (ptr_difference_const (@0, @1, &diff))
958     { build_int_cst_type (type, diff); }))))
959 (simplify
960  (minus (convert @0) (convert ADDR_EXPR@1))
961  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
962   (with { HOST_WIDE_INT diff; }
963    (if (ptr_difference_const (@0, @1, &diff))
964     { build_int_cst_type (type, diff); }))))
966 /* If arg0 is derived from the address of an object or function, we may
967    be able to fold this expression using the object or function's
968    alignment.  */
969 (simplify
970  (bit_and (convert? @0) INTEGER_CST@1)
971  (if (POINTER_TYPE_P (TREE_TYPE (@0))
972       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
973   (with
974    {
975      unsigned int align;
976      unsigned HOST_WIDE_INT bitpos;
977      get_pointer_alignment_1 (@0, &align, &bitpos);
978    }
979    (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
980     { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
983 /* We can't reassociate at all for saturating types.  */
984 (if (!TYPE_SATURATING (type))
986  /* Contract negates.  */
987  /* A + (-B) -> A - B */
988  (simplify
989   (plus:c (convert1? @0) (convert2? (negate @1)))
990   /* Apply STRIP_NOPS on @0 and the negate.  */
991   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
992        && tree_nop_conversion_p (type, TREE_TYPE (@1))
993        && !TYPE_OVERFLOW_SANITIZED (type))
994    (minus (convert @0) (convert @1))))
995  /* A - (-B) -> A + B */
996  (simplify
997   (minus (convert1? @0) (convert2? (negate @1)))
998   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
999        && tree_nop_conversion_p (type, TREE_TYPE (@1))
1000        && !TYPE_OVERFLOW_SANITIZED (type))
1001    (plus (convert @0) (convert @1))))
1002  /* -(-A) -> A */
1003  (simplify
1004   (negate (convert? (negate @1)))
1005   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1006        && !TYPE_OVERFLOW_SANITIZED (type))
1007    (convert @1)))
1009  /* We can't reassociate floating-point unless -fassociative-math
1010     or fixed-point plus or minus because of saturation to +-Inf.  */
1011  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1012       && !FIXED_POINT_TYPE_P (type))
1014   /* Match patterns that allow contracting a plus-minus pair
1015      irrespective of overflow issues.  */
1016   /* (A +- B) - A       ->  +- B */
1017   /* (A +- B) -+ B      ->  A */
1018   /* A - (A +- B)       -> -+ B */
1019   /* A +- (B -+ A)      ->  +- B */
1020   (simplify
1021     (minus (plus:c @0 @1) @0)
1022     @1)
1023   (simplify
1024     (minus (minus @0 @1) @0)
1025     (negate @1))
1026   (simplify
1027     (plus:c (minus @0 @1) @1)
1028     @0)
1029   (simplify
1030    (minus @0 (plus:c @0 @1))
1031    (negate @1))
1032   (simplify
1033    (minus @0 (minus @0 @1))
1034    @1)
1036   /* (A +- CST) +- CST -> A + CST  */
1037   (for outer_op (plus minus)
1038    (for inner_op (plus minus)
1039     (simplify
1040      (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1041      /* If the constant operation overflows we cannot do the transform
1042         as we would introduce undefined overflow, for example
1043         with (a - 1) + INT_MIN.  */
1044      (with { tree cst = const_binop (outer_op == inner_op
1045                                      ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1046       (if (cst && !TREE_OVERFLOW (cst))
1047        (inner_op @0 { cst; } ))))))
1049   /* (CST - A) +- CST -> CST - A  */
1050   (for outer_op (plus minus)
1051    (simplify
1052     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1053     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1054      (if (cst && !TREE_OVERFLOW (cst))
1055       (minus { cst; } @0)))))
1057   /* ~A + A -> -1 */
1058   (simplify
1059    (plus:c (bit_not @0) @0)
1060    (if (!TYPE_OVERFLOW_TRAPS (type))
1061     { build_all_ones_cst (type); }))
1063   /* ~A + 1 -> -A */
1064   (simplify
1065    (plus (convert? (bit_not @0)) integer_each_onep)
1066    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1067     (negate (convert @0))))
1069   /* -A - 1 -> ~A */
1070   (simplify
1071    (minus (convert? (negate @0)) integer_each_onep)
1072    (if (!TYPE_OVERFLOW_TRAPS (type)
1073         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1074     (bit_not (convert @0))))
1076   /* -1 - A -> ~A */
1077   (simplify
1078    (minus integer_all_onesp @0)
1079    (bit_not @0))
1081   /* (T)(P + A) - (T)P -> (T) A */
1082   (for add (plus pointer_plus)
1083    (simplify
1084     (minus (convert (add @0 @1))
1085      (convert @0))
1086     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1087          /* For integer types, if A has a smaller type
1088             than T the result depends on the possible
1089             overflow in P + A.
1090             E.g. T=size_t, A=(unsigned)429497295, P>0.
1091             However, if an overflow in P + A would cause
1092             undefined behavior, we can assume that there
1093             is no overflow.  */
1094          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1095              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1096          /* For pointer types, if the conversion of A to the
1097             final type requires a sign- or zero-extension,
1098             then we have to punt - it is not defined which
1099             one is correct.  */
1100          || (POINTER_TYPE_P (TREE_TYPE (@0))
1101              && TREE_CODE (@1) == INTEGER_CST
1102              && tree_int_cst_sign_bit (@1) == 0))
1103      (convert @1))))
1105   /* (T)P - (T)(P + A) -> -(T) A */
1106   (for add (plus pointer_plus)
1107    (simplify
1108     (minus (convert @0)
1109      (convert (add @0 @1)))
1110     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1111          /* For integer types, if A has a smaller type
1112             than T the result depends on the possible
1113             overflow in P + A.
1114             E.g. T=size_t, A=(unsigned)429497295, P>0.
1115             However, if an overflow in P + A would cause
1116             undefined behavior, we can assume that there
1117             is no overflow.  */
1118          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1119              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1120          /* For pointer types, if the conversion of A to the
1121             final type requires a sign- or zero-extension,
1122             then we have to punt - it is not defined which
1123             one is correct.  */
1124          || (POINTER_TYPE_P (TREE_TYPE (@0))
1125              && TREE_CODE (@1) == INTEGER_CST
1126              && tree_int_cst_sign_bit (@1) == 0))
1127      (negate (convert @1)))))
1129   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1130   (for add (plus pointer_plus)
1131    (simplify
1132     (minus (convert (add @0 @1))
1133      (convert (add @0 @2)))
1134     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1135          /* For integer types, if A has a smaller type
1136             than T the result depends on the possible
1137             overflow in P + A.
1138             E.g. T=size_t, A=(unsigned)429497295, P>0.
1139             However, if an overflow in P + A would cause
1140             undefined behavior, we can assume that there
1141             is no overflow.  */
1142          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1143              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1144          /* For pointer types, if the conversion of A to the
1145             final type requires a sign- or zero-extension,
1146             then we have to punt - it is not defined which
1147             one is correct.  */
1148          || (POINTER_TYPE_P (TREE_TYPE (@0))
1149              && TREE_CODE (@1) == INTEGER_CST
1150              && tree_int_cst_sign_bit (@1) == 0
1151              && TREE_CODE (@2) == INTEGER_CST
1152              && tree_int_cst_sign_bit (@2) == 0))
1153      (minus (convert @1) (convert @2)))))))
1156 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
1158 (for minmax (min max FMIN FMAX)
1159  (simplify
1160   (minmax @0 @0)
1161   @0))
1162 /* min(max(x,y),y) -> y.  */
1163 (simplify
1164  (min:c (max:c @0 @1) @1)
1165  @1)
1166 /* max(min(x,y),y) -> y.  */
1167 (simplify
1168  (max:c (min:c @0 @1) @1)
1169  @1)
1170 (simplify
1171  (min @0 @1)
1172  (if (INTEGRAL_TYPE_P (type)
1173       && TYPE_MIN_VALUE (type)
1174       && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1175   @1))
1176 (simplify
1177  (max @0 @1)
1178  (if (INTEGRAL_TYPE_P (type)
1179       && TYPE_MAX_VALUE (type)
1180       && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1181   @1))
1182 (for minmax (FMIN FMAX)
1183  /* If either argument is NaN, return the other one.  Avoid the
1184     transformation if we get (and honor) a signalling NaN.  */
1185  (simplify
1186   (minmax:c @0 REAL_CST@1)
1187   (if (real_isnan (TREE_REAL_CST_PTR (@1))
1188        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1189    @0)))
1190 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
1191    functions to return the numeric arg if the other one is NaN.
1192    MIN and MAX don't honor that, so only transform if -ffinite-math-only
1193    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
1194    worry about it either.  */
1195 (if (flag_finite_math_only)
1196  (simplify
1197   (FMIN @0 @1)
1198   (min @0 @1))
1199  (simplify
1200   (FMAX @0 @1)
1201   (max @0 @1)))
1203 /* Simplifications of shift and rotates.  */
1205 (for rotate (lrotate rrotate)
1206  (simplify
1207   (rotate integer_all_onesp@0 @1)
1208   @0))
1210 /* Optimize -1 >> x for arithmetic right shifts.  */
1211 (simplify
1212  (rshift integer_all_onesp@0 @1)
1213  (if (!TYPE_UNSIGNED (type)
1214       && tree_expr_nonnegative_p (@1))
1215   @0))
1217 /* Optimize (x >> c) << c into x & (-1<<c).  */
1218 (simplify
1219  (lshift (rshift @0 INTEGER_CST@1) @1)
1220  (if (wi::ltu_p (@1, element_precision (type)))
1221   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1223 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1224    types.  */
1225 (simplify
1226  (rshift (lshift @0 INTEGER_CST@1) @1)
1227  (if (TYPE_UNSIGNED (type)
1228       && (wi::ltu_p (@1, element_precision (type))))
1229   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1231 (for shiftrotate (lrotate rrotate lshift rshift)
1232  (simplify
1233   (shiftrotate @0 integer_zerop)
1234   (non_lvalue @0))
1235  (simplify
1236   (shiftrotate integer_zerop@0 @1)
1237   @0)
1238  /* Prefer vector1 << scalar to vector1 << vector2
1239     if vector2 is uniform.  */
1240  (for vec (VECTOR_CST CONSTRUCTOR)
1241   (simplify
1242    (shiftrotate @0 vec@1)
1243    (with { tree tem = uniform_vector_p (@1); }
1244     (if (tem)
1245      (shiftrotate @0 { tem; }))))))
1247 /* Rewrite an LROTATE_EXPR by a constant into an
1248    RROTATE_EXPR by a new constant.  */
1249 (simplify
1250  (lrotate @0 INTEGER_CST@1)
1251  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1252                             build_int_cst (TREE_TYPE (@1),
1253                                            element_precision (type)), @1); }))
1255 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
1256 (for op (lrotate rrotate rshift lshift)
1257  (simplify
1258   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1259   (with { unsigned int prec = element_precision (type); }
1260    (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1261         && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1262         && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1263         && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1264     (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1265      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1266         being well defined.  */
1267      (if (low >= prec)
1268       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1269        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1270        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1271         { build_zero_cst (type); }
1272         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1273       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1276 /* ((1 << A) & 1) != 0 -> A == 0
1277    ((1 << A) & 1) == 0 -> A != 0 */
1278 (for cmp (ne eq)
1279      icmp (eq ne)
1280  (simplify
1281   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1282   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1284 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1285    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1286    if CST2 != 0.  */
1287 (for cmp (ne eq)
1288  (simplify
1289   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1290   (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1291    (if (cand < 0
1292         || (!integer_zerop (@2)
1293             && wi::ne_p (wi::lshift (@0, cand), @2)))
1294     { constant_boolean_node (cmp == NE_EXPR, type); }
1295     (if (!integer_zerop (@2)
1296          && wi::eq_p (wi::lshift (@0, cand), @2))
1297      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1299 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1300         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1301    if the new mask might be further optimized.  */
1302 (for shift (lshift rshift)
1303  (simplify
1304   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1305            INTEGER_CST@2)
1306    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1307         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1308         && tree_fits_uhwi_p (@1)
1309         && tree_to_uhwi (@1) > 0
1310         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1311     (with
1312      {
1313        unsigned int shiftc = tree_to_uhwi (@1);
1314        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1315        unsigned HOST_WIDE_INT newmask, zerobits = 0;
1316        tree shift_type = TREE_TYPE (@3);
1317        unsigned int prec;
1319        if (shift == LSHIFT_EXPR)
1320          zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1321        else if (shift == RSHIFT_EXPR
1322                 && (TYPE_PRECISION (shift_type)
1323                     == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1324          {
1325            prec = TYPE_PRECISION (TREE_TYPE (@3));
1326            tree arg00 = @0;
1327            /* See if more bits can be proven as zero because of
1328               zero extension.  */
1329            if (@3 != @0
1330                && TYPE_UNSIGNED (TREE_TYPE (@0)))
1331              {
1332                tree inner_type = TREE_TYPE (@0);
1333                if ((TYPE_PRECISION (inner_type)
1334                     == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1335                    && TYPE_PRECISION (inner_type) < prec)
1336                  {
1337                    prec = TYPE_PRECISION (inner_type);
1338                    /* See if we can shorten the right shift.  */
1339                    if (shiftc < prec)
1340                      shift_type = inner_type;
1341                    /* Otherwise X >> C1 is all zeros, so we'll optimize
1342                       it into (X, 0) later on by making sure zerobits
1343                       is all ones.  */
1344                  }
1345              }
1346            zerobits = ~(unsigned HOST_WIDE_INT) 0;
1347            if (shiftc < prec)
1348              {
1349                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1350                zerobits <<= prec - shiftc;
1351              }
1352            /* For arithmetic shift if sign bit could be set, zerobits
1353               can contain actually sign bits, so no transformation is
1354               possible, unless MASK masks them all away.  In that
1355               case the shift needs to be converted into logical shift.  */
1356            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1357                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1358              {
1359                if ((mask & zerobits) == 0)
1360                  shift_type = unsigned_type_for (TREE_TYPE (@3));
1361                else
1362                  zerobits = 0;
1363              }
1364          }
1365      }
1366      /* ((X << 16) & 0xff00) is (X, 0).  */
1367      (if ((mask & zerobits) == mask)
1368       { build_int_cst (type, 0); }
1369       (with { newmask = mask | zerobits; }
1370        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1371         (with
1372          {
1373            /* Only do the transformation if NEWMASK is some integer
1374               mode's mask.  */
1375            for (prec = BITS_PER_UNIT;
1376                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1377              if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1378                break;
1379          }
1380          (if (prec < HOST_BITS_PER_WIDE_INT
1381               || newmask == ~(unsigned HOST_WIDE_INT) 0)
1382           (with
1383            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1384            (if (!tree_int_cst_equal (newmaskt, @2))
1385             (if (shift_type != TREE_TYPE (@3))
1386              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1387              (bit_and @4 { newmaskt; })))))))))))))
1389 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1390    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
1391 (for shift (lshift rshift)
1392  (for bit_op (bit_and bit_xor bit_ior)
1393   (simplify
1394    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1395    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1396     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1397      (bit_op (shift (convert @0) @1) { mask; }))))))
1400 /* Simplifications of conversions.  */
1402 /* Basic strip-useless-type-conversions / strip_nops.  */
1403 (for cvt (convert view_convert float fix_trunc)
1404  (simplify
1405   (cvt @0)
1406   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1407        || (GENERIC && type == TREE_TYPE (@0)))
1408    @0)))
1410 /* Contract view-conversions.  */
1411 (simplify
1412   (view_convert (view_convert @0))
1413   (view_convert @0))
1415 /* For integral conversions with the same precision or pointer
1416    conversions use a NOP_EXPR instead.  */
1417 (simplify
1418   (view_convert @0)
1419   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1420        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1421        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1422    (convert @0)))
1424 /* Strip inner integral conversions that do not change precision or size.  */
1425 (simplify
1426   (view_convert (convert@0 @1))
1427   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1428        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1429        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1430        && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1431    (view_convert @1)))
1433 /* Re-association barriers around constants and other re-association
1434    barriers can be removed.  */
1435 (simplify
1436  (paren CONSTANT_CLASS_P@0)
1437  @0)
1438 (simplify
1439  (paren (paren@1 @0))
1440  @1)
1442 /* Handle cases of two conversions in a row.  */
1443 (for ocvt (convert float fix_trunc)
1444  (for icvt (convert float)
1445   (simplify
1446    (ocvt (icvt@1 @0))
1447    (with
1448     {
1449       tree inside_type = TREE_TYPE (@0);
1450       tree inter_type = TREE_TYPE (@1);
1451       int inside_int = INTEGRAL_TYPE_P (inside_type);
1452       int inside_ptr = POINTER_TYPE_P (inside_type);
1453       int inside_float = FLOAT_TYPE_P (inside_type);
1454       int inside_vec = VECTOR_TYPE_P (inside_type);
1455       unsigned int inside_prec = TYPE_PRECISION (inside_type);
1456       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1457       int inter_int = INTEGRAL_TYPE_P (inter_type);
1458       int inter_ptr = POINTER_TYPE_P (inter_type);
1459       int inter_float = FLOAT_TYPE_P (inter_type);
1460       int inter_vec = VECTOR_TYPE_P (inter_type);
1461       unsigned int inter_prec = TYPE_PRECISION (inter_type);
1462       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1463       int final_int = INTEGRAL_TYPE_P (type);
1464       int final_ptr = POINTER_TYPE_P (type);
1465       int final_float = FLOAT_TYPE_P (type);
1466       int final_vec = VECTOR_TYPE_P (type);
1467       unsigned int final_prec = TYPE_PRECISION (type);
1468       int final_unsignedp = TYPE_UNSIGNED (type);
1469     }
1470    (switch
1471     /* In addition to the cases of two conversions in a row
1472        handled below, if we are converting something to its own
1473        type via an object of identical or wider precision, neither
1474        conversion is needed.  */
1475     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1476           || (GENERIC
1477               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1478          && (((inter_int || inter_ptr) && final_int)
1479              || (inter_float && final_float))
1480          && inter_prec >= final_prec)
1481      (ocvt @0))
1483     /* Likewise, if the intermediate and initial types are either both
1484        float or both integer, we don't need the middle conversion if the
1485        former is wider than the latter and doesn't change the signedness
1486        (for integers).  Avoid this if the final type is a pointer since
1487        then we sometimes need the middle conversion.  Likewise if the
1488        final type has a precision not equal to the size of its mode.  */
1489     (if (((inter_int && inside_int) || (inter_float && inside_float))
1490          && (final_int || final_float)
1491          && inter_prec >= inside_prec
1492          && (inter_float || inter_unsignedp == inside_unsignedp)
1493          && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1494                && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1495      (ocvt @0))
1497     /* If we have a sign-extension of a zero-extended value, we can
1498        replace that by a single zero-extension.  Likewise if the
1499        final conversion does not change precision we can drop the
1500        intermediate conversion.  */
1501     (if (inside_int && inter_int && final_int
1502          && ((inside_prec < inter_prec && inter_prec < final_prec
1503               && inside_unsignedp && !inter_unsignedp)
1504              || final_prec == inter_prec))
1505      (ocvt @0))
1507     /* Two conversions in a row are not needed unless:
1508         - some conversion is floating-point (overstrict for now), or
1509         - some conversion is a vector (overstrict for now), or
1510         - the intermediate type is narrower than both initial and
1511           final, or
1512         - the intermediate type and innermost type differ in signedness,
1513           and the outermost type is wider than the intermediate, or
1514         - the initial type is a pointer type and the precisions of the
1515           intermediate and final types differ, or
1516         - the final type is a pointer type and the precisions of the
1517           initial and intermediate types differ.  */
1518     (if (! inside_float && ! inter_float && ! final_float
1519          && ! inside_vec && ! inter_vec && ! final_vec
1520          && (inter_prec >= inside_prec || inter_prec >= final_prec)
1521          && ! (inside_int && inter_int
1522                && inter_unsignedp != inside_unsignedp
1523                && inter_prec < final_prec)
1524          && ((inter_unsignedp && inter_prec > inside_prec)
1525              == (final_unsignedp && final_prec > inter_prec))
1526          && ! (inside_ptr && inter_prec != final_prec)
1527          && ! (final_ptr && inside_prec != inter_prec)
1528          && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1529                && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1530      (ocvt @0))
1532     /* A truncation to an unsigned type (a zero-extension) should be
1533        canonicalized as bitwise and of a mask.  */
1534     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
1535          && final_int && inter_int && inside_int
1536          && final_prec == inside_prec
1537          && final_prec > inter_prec
1538          && inter_unsignedp)
1539      (convert (bit_and @0 { wide_int_to_tree
1540                               (inside_type,
1541                                wi::mask (inter_prec, false,
1542                                          TYPE_PRECISION (inside_type))); })))
1544     /* If we are converting an integer to a floating-point that can
1545        represent it exactly and back to an integer, we can skip the
1546        floating-point conversion.  */
1547     (if (GIMPLE /* PR66211 */
1548          && inside_int && inter_float && final_int &&
1549          (unsigned) significand_size (TYPE_MODE (inter_type))
1550          >= inside_prec - !inside_unsignedp)
1551      (convert @0)))))))
1553 /* If we have a narrowing conversion to an integral type that is fed by a
1554    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1555    masks off bits outside the final type (and nothing else).  */
1556 (simplify
1557   (convert (bit_and @0 INTEGER_CST@1))
1558   (if (INTEGRAL_TYPE_P (type)
1559        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1560        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1561        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1562                                                     TYPE_PRECISION (type)), 0))
1563    (convert @0)))
1566 /* (X /[ex] A) * A -> X.  */
1567 (simplify
1568   (mult (convert? (exact_div @0 @1)) @1)
1569   /* Look through a sign-changing conversion.  */
1570   (convert @0))
1572 /* Canonicalization of binary operations.  */
1574 /* Convert X + -C into X - C.  */
1575 (simplify
1576  (plus @0 REAL_CST@1)
1577  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1578   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1579    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1580     (minus @0 { tem; })))))
1582 /* Convert x+x into x*2.0.  */
1583 (simplify
1584  (plus @0 @0)
1585  (if (SCALAR_FLOAT_TYPE_P (type))
1586   (mult @0 { build_real (type, dconst2); })))
1588 (simplify
1589  (minus integer_zerop @1)
1590  (negate @1))
1592 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
1593    ARG0 is zero and X + ARG0 reduces to X, since that would mean
1594    (-ARG1 + ARG0) reduces to -ARG1.  */
1595 (simplify
1596  (minus real_zerop@0 @1)
1597  (if (fold_real_zero_addition_p (type, @0, 0))
1598   (negate @1)))
1600 /* Transform x * -1 into -x.  */
1601 (simplify
1602  (mult @0 integer_minus_onep)
1603  (negate @0))
1605 /* True if we can easily extract the real and imaginary parts of a complex
1606    number.  */
1607 (match compositional_complex
1608  (convert? (complex @0 @1)))
1610 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
1611 (simplify
1612  (complex (realpart @0) (imagpart @0))
1613  @0)
1614 (simplify
1615  (realpart (complex @0 @1))
1616  @0)
1617 (simplify
1618  (imagpart (complex @0 @1))
1619  @1)
1621 /* Sometimes we only care about half of a complex expression.  */
1622 (simplify
1623  (realpart (convert?:s (conj:s @0)))
1624  (convert (realpart @0)))
1625 (simplify
1626  (imagpart (convert?:s (conj:s @0)))
1627  (convert (negate (imagpart @0))))
1628 (for part (realpart imagpart)
1629  (for op (plus minus)
1630   (simplify
1631    (part (convert?:s@2 (op:s @0 @1)))
1632    (convert (op (part @0) (part @1))))))
1633 (simplify
1634  (realpart (convert?:s (CEXPI:s @0)))
1635  (convert (COS @0)))
1636 (simplify
1637  (imagpart (convert?:s (CEXPI:s @0)))
1638  (convert (SIN @0)))
1640 /* conj(conj(x)) -> x  */
1641 (simplify
1642  (conj (convert? (conj @0)))
1643  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1644   (convert @0)))
1646 /* conj({x,y}) -> {x,-y}  */
1647 (simplify
1648  (conj (convert?:s (complex:s @0 @1)))
1649  (with { tree itype = TREE_TYPE (type); }
1650   (complex (convert:itype @0) (negate (convert:itype @1)))))
1652 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
1653 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1654  (simplify
1655   (bswap (bswap @0))
1656   @0)
1657  (simplify
1658   (bswap (bit_not (bswap @0)))
1659   (bit_not @0))
1660  (for bitop (bit_xor bit_ior bit_and)
1661   (simplify
1662    (bswap (bitop:c (bswap @0) @1))
1663    (bitop @0 (bswap @1)))))
1666 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
1668 /* Simplify constant conditions.
1669    Only optimize constant conditions when the selected branch
1670    has the same type as the COND_EXPR.  This avoids optimizing
1671    away "c ? x : throw", where the throw has a void type.
1672    Note that we cannot throw away the fold-const.c variant nor
1673    this one as we depend on doing this transform before possibly
1674    A ? B : B -> B triggers and the fold-const.c one can optimize
1675    0 ? A : B to B even if A has side-effects.  Something
1676    genmatch cannot handle.  */
1677 (simplify
1678  (cond INTEGER_CST@0 @1 @2)
1679  (if (integer_zerop (@0))
1680   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1681    @2)
1682   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1683    @1)))
1684 (simplify
1685  (vec_cond VECTOR_CST@0 @1 @2)
1686  (if (integer_all_onesp (@0))
1687   @1
1688   (if (integer_zerop (@0))
1689    @2)))
1691 (for cnd (cond vec_cond)
1692  /* A ? B : (A ? X : C) -> A ? B : C.  */
1693  (simplify
1694   (cnd @0 (cnd @0 @1 @2) @3)
1695   (cnd @0 @1 @3))
1696  (simplify
1697   (cnd @0 @1 (cnd @0 @2 @3))
1698   (cnd @0 @1 @3))
1699  /* A ? B : (!A ? C : X) -> A ? B : C.  */
1700  /* ???  This matches embedded conditions open-coded because genmatch
1701     would generate matching code for conditions in separate stmts only.
1702     The following is still important to merge then and else arm cases
1703     from if-conversion.  */
1704  (simplify
1705   (cnd @0 @1 (cnd @2 @3 @4))
1706   (if (COMPARISON_CLASS_P (@0)
1707        && COMPARISON_CLASS_P (@2)
1708        && invert_tree_comparison
1709            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1710        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1711        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1712    (cnd @0 @1 @3)))
1713  (simplify
1714   (cnd @0 (cnd @1 @2 @3) @4)
1715   (if (COMPARISON_CLASS_P (@0)
1716        && COMPARISON_CLASS_P (@1)
1717        && invert_tree_comparison
1718            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1719        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1720        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1721    (cnd @0 @3 @4)))
1723  /* A ? B : B -> B.  */
1724  (simplify
1725   (cnd @0 @1 @1)
1726   @1)
1728  /* !A ? B : C -> A ? C : B.  */
1729  (simplify
1730   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1731   (cnd @0 @2 @1)))
1733 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1734    return all -1 or all 0 results.  */
1735 /* ??? We could instead convert all instances of the vec_cond to negate,
1736    but that isn't necessarily a win on its own.  */
1737 (simplify
1738  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1739  (if (VECTOR_TYPE_P (type)
1740       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1741       && (TYPE_MODE (TREE_TYPE (type))
1742           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1743   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1745 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
1746 (simplify
1747  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1748  (if (VECTOR_TYPE_P (type)
1749       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1750       && (TYPE_MODE (TREE_TYPE (type))
1751           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1752   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1755 /* Simplifications of comparisons.  */
1757 /* See if we can reduce the magnitude of a constant involved in a
1758    comparison by changing the comparison code.  This is a canonicalization
1759    formerly done by maybe_canonicalize_comparison_1.  */
1760 (for cmp  (le gt)
1761      acmp (lt ge)
1762  (simplify
1763   (cmp @0 INTEGER_CST@1)
1764   (if (tree_int_cst_sgn (@1) == -1)
1765    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1766 (for cmp  (ge lt)
1767      acmp (gt le)
1768  (simplify
1769   (cmp @0 INTEGER_CST@1)
1770   (if (tree_int_cst_sgn (@1) == 1)
1771    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1774 /* We can simplify a logical negation of a comparison to the
1775    inverted comparison.  As we cannot compute an expression
1776    operator using invert_tree_comparison we have to simulate
1777    that with expression code iteration.  */
1778 (for cmp (tcc_comparison)
1779      icmp (inverted_tcc_comparison)
1780      ncmp (inverted_tcc_comparison_with_nans)
1781  /* Ideally we'd like to combine the following two patterns
1782     and handle some more cases by using
1783       (logical_inverted_value (cmp @0 @1))
1784     here but for that genmatch would need to "inline" that.
1785     For now implement what forward_propagate_comparison did.  */
1786  (simplify
1787   (bit_not (cmp @0 @1))
1788   (if (VECTOR_TYPE_P (type)
1789        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1790    /* Comparison inversion may be impossible for trapping math,
1791       invert_tree_comparison will tell us.  But we can't use
1792       a computed operator in the replacement tree thus we have
1793       to play the trick below.  */
1794    (with { enum tree_code ic = invert_tree_comparison
1795              (cmp, HONOR_NANS (@0)); }
1796     (if (ic == icmp)
1797      (icmp @0 @1)
1798      (if (ic == ncmp)
1799       (ncmp @0 @1))))))
1800  (simplify
1801   (bit_xor (cmp @0 @1) integer_truep)
1802   (with { enum tree_code ic = invert_tree_comparison
1803             (cmp, HONOR_NANS (@0)); }
1804    (if (ic == icmp)
1805     (icmp @0 @1)
1806     (if (ic == ncmp)
1807      (ncmp @0 @1))))))
1809 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1810    ??? The transformation is valid for the other operators if overflow
1811    is undefined for the type, but performing it here badly interacts
1812    with the transformation in fold_cond_expr_with_comparison which
1813    attempts to synthetize ABS_EXPR.  */
1814 (for cmp (eq ne)
1815  (simplify
1816   (cmp (minus@2 @0 @1) integer_zerop)
1817   (if (single_use (@2))
1818    (cmp @0 @1))))
1820 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1821    signed arithmetic case.  That form is created by the compiler
1822    often enough for folding it to be of value.  One example is in
1823    computing loop trip counts after Operator Strength Reduction.  */
1824 (for cmp (simple_comparison)
1825      scmp (swapped_simple_comparison)
1826  (simplify
1827   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1828   /* Handle unfolded multiplication by zero.  */
1829   (if (integer_zerop (@1))
1830    (cmp @1 @2)
1831    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1832         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1833         && single_use (@3))
1834     /* If @1 is negative we swap the sense of the comparison.  */
1835     (if (tree_int_cst_sgn (@1) < 0)
1836      (scmp @0 @2)
1837      (cmp @0 @2))))))
1839 /* Simplify comparison of something with itself.  For IEEE
1840    floating-point, we can only do some of these simplifications.  */
1841 (for cmp (eq ge le)
1842  (simplify
1843   (cmp @0 @0)
1844   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1845        || ! HONOR_NANS (@0))
1846    { constant_boolean_node (true, type); }
1847    (if (cmp != EQ_EXPR)
1848     (eq @0 @0)))))
1849 (for cmp (ne gt lt)
1850  (simplify
1851   (cmp @0 @0)
1852   (if (cmp != NE_EXPR
1853        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1854        || ! HONOR_NANS (@0))
1855    { constant_boolean_node (false, type); })))
1856 (for cmp (unle unge uneq)
1857  (simplify
1858   (cmp @0 @0)
1859   { constant_boolean_node (true, type); }))
1860 (simplify
1861  (ltgt @0 @0)
1862  (if (!flag_trapping_math)
1863   { constant_boolean_node (false, type); }))
1865 /* Fold ~X op ~Y as Y op X.  */
1866 (for cmp (simple_comparison)
1867  (simplify
1868   (cmp (bit_not@2 @0) (bit_not@3 @1))
1869   (if (single_use (@2) && single_use (@3))
1870    (cmp @1 @0))))
1872 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
1873 (for cmp (simple_comparison)
1874      scmp (swapped_simple_comparison)
1875  (simplify
1876   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
1877   (if (single_use (@2)
1878        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
1879    (scmp @0 (bit_not @1)))))
1881 (for cmp (simple_comparison)
1882  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
1883  (simplify
1884   (cmp (convert@2 @0) (convert? @1))
1885   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1886        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1887            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1888        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1889            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1890    (with
1891     {
1892       tree type1 = TREE_TYPE (@1);
1893       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1894         {
1895           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1896           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1897               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1898             type1 = float_type_node;
1899           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1900               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1901             type1 = double_type_node;
1902         }
1903       tree newtype
1904         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1905            ? TREE_TYPE (@0) : type1); 
1906     }
1907     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1908      (cmp (convert:newtype @0) (convert:newtype @1))))))
1910  (simplify
1911   (cmp @0 REAL_CST@1)
1912   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
1913   (switch
1914    /* a CMP (-0) -> a CMP 0  */
1915    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1916     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1917    /* x != NaN is always true, other ops are always false.  */
1918    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1919         && ! HONOR_SNANS (@1))
1920     { constant_boolean_node (cmp == NE_EXPR, type); })
1921    /* Fold comparisons against infinity.  */
1922    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1923         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1924     (with
1925      {
1926        REAL_VALUE_TYPE max;
1927        enum tree_code code = cmp;
1928        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1929        if (neg)
1930          code = swap_tree_comparison (code);
1931      }
1932      (switch
1933       /* x > +Inf is always false, if with ignore sNANs.  */
1934       (if (code == GT_EXPR
1935            && ! HONOR_SNANS (@0))
1936        { constant_boolean_node (false, type); })
1937       (if (code == LE_EXPR)
1938        /* x <= +Inf is always true, if we don't case about NaNs.  */
1939        (if (! HONOR_NANS (@0))
1940         { constant_boolean_node (true, type); }
1941         /* x <= +Inf is the same as x == x, i.e. !isnan(x).  */
1942         (eq @0 @0)))
1943       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
1944       (if (code == EQ_EXPR || code == GE_EXPR)
1945        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1946         (if (neg)
1947          (lt @0 { build_real (TREE_TYPE (@0), max); })
1948          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1949       /* x < +Inf is always equal to x <= DBL_MAX.  */
1950       (if (code == LT_EXPR)
1951        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1952         (if (neg)
1953          (ge @0 { build_real (TREE_TYPE (@0), max); })
1954          (le @0 { build_real (TREE_TYPE (@0), max); }))))
1955       /* x != +Inf is always equal to !(x > DBL_MAX).  */
1956       (if (code == NE_EXPR)
1957        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1958         (if (! HONOR_NANS (@0))
1959          (if (neg)
1960           (ge @0 { build_real (TREE_TYPE (@0), max); })
1961           (le @0 { build_real (TREE_TYPE (@0), max); }))
1962          (if (neg)
1963           (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1964            { build_one_cst (type); })
1965           (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1966            { build_one_cst (type); }))))))))))
1968  /* If this is a comparison of a real constant with a PLUS_EXPR
1969     or a MINUS_EXPR of a real constant, we can convert it into a
1970     comparison with a revised real constant as long as no overflow
1971     occurs when unsafe_math_optimizations are enabled.  */
1972  (if (flag_unsafe_math_optimizations)
1973   (for op (plus minus)
1974    (simplify
1975     (cmp (op @0 REAL_CST@1) REAL_CST@2)
1976     (with
1977      {
1978        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1979                                TREE_TYPE (@1), @2, @1);
1980      }
1981      (if (tem && !TREE_OVERFLOW (tem))
1982       (cmp @0 { tem; }))))))
1984  /* Likewise, we can simplify a comparison of a real constant with
1985     a MINUS_EXPR whose first operand is also a real constant, i.e.
1986     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
1987     floating-point types only if -fassociative-math is set.  */
1988  (if (flag_associative_math)
1989   (simplify
1990    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1991    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1992     (if (tem && !TREE_OVERFLOW (tem))
1993      (cmp { tem; } @1)))))
1995  /* Fold comparisons against built-in math functions.  */
1996  (if (flag_unsafe_math_optimizations
1997       && ! flag_errno_math)
1998   (for sq (SQRT)
1999    (simplify
2000     (cmp (sq @0) REAL_CST@1)
2001     (switch
2002      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2003       (switch
2004        /* sqrt(x) < y is always false, if y is negative.  */
2005        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2006         { constant_boolean_node (false, type); })
2007        /* sqrt(x) > y is always true, if y is negative and we
2008           don't care about NaNs, i.e. negative values of x.  */
2009        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2010         { constant_boolean_node (true, type); })
2011        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
2012        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2013      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2014       (switch
2015        /* sqrt(x) < 0 is always false.  */
2016        (if (cmp == LT_EXPR)
2017         { constant_boolean_node (false, type); })
2018        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
2019        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2020         { constant_boolean_node (true, type); })
2021        /* sqrt(x) <= 0 -> x == 0.  */
2022        (if (cmp == LE_EXPR)
2023         (eq @0 @1))
2024        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
2025           == or !=.  In the last case:
2027             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2029           if x is negative or NaN.  Due to -funsafe-math-optimizations,
2030           the results for other x follow from natural arithmetic.  */
2031        (cmp @0 @1)))
2032      (if (cmp == GT_EXPR || cmp == GE_EXPR)
2033       (with
2034        {
2035          REAL_VALUE_TYPE c2;
2036          real_arithmetic (&c2, MULT_EXPR,
2037                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2038          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2039        }
2040        (if (REAL_VALUE_ISINF (c2))
2041         /* sqrt(x) > y is x == +Inf, when y is very large.  */
2042         (if (HONOR_INFINITIES (@0))
2043          (eq @0 { build_real (TREE_TYPE (@0), c2); })
2044          { constant_boolean_node (false, type); })
2045         /* sqrt(x) > c is the same as x > c*c.  */
2046         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2047      (if (cmp == LT_EXPR || cmp == LE_EXPR)
2048       (with
2049        {
2050          REAL_VALUE_TYPE c2;
2051          real_arithmetic (&c2, MULT_EXPR,
2052                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2053          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2054        }
2055        (if (REAL_VALUE_ISINF (c2))
2056         (switch
2057          /* sqrt(x) < y is always true, when y is a very large
2058             value and we don't care about NaNs or Infinities.  */
2059          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2060           { constant_boolean_node (true, type); })
2061          /* sqrt(x) < y is x != +Inf when y is very large and we
2062             don't care about NaNs.  */
2063          (if (! HONOR_NANS (@0))
2064           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2065          /* sqrt(x) < y is x >= 0 when y is very large and we
2066             don't care about Infinities.  */
2067          (if (! HONOR_INFINITIES (@0))
2068           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2069          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
2070          (if (GENERIC)
2071           (truth_andif
2072            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2073            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2074         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
2075         (if (! HONOR_NANS (@0))
2076          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2077          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
2078          (if (GENERIC)
2079           (truth_andif
2080            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2081            (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2083 /* Unordered tests if either argument is a NaN.  */
2084 (simplify
2085  (bit_ior (unordered @0 @0) (unordered @1 @1))
2086  (if (types_match (@0, @1))
2087   (unordered @0 @1)))
2088 (simplify
2089  (bit_and (ordered @0 @0) (ordered @1 @1))
2090  (if (types_match (@0, @1))
2091   (ordered @0 @1)))
2092 (simplify
2093  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2094  @2)
2095 (simplify
2096  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2097  @2)
2099 /* Simple range test simplifications.  */
2100 /* A < B || A >= B -> true.  */
2101 (for test1 (lt le le le ne ge)
2102      test2 (ge gt ge ne eq ne)
2103  (simplify
2104   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2105   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2106        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2107    { constant_boolean_node (true, type); })))
2108 /* A < B && A >= B -> false.  */
2109 (for test1 (lt lt lt le ne eq)
2110      test2 (ge gt eq gt eq gt)
2111  (simplify
2112   (bit_and:c (test1 @0 @1) (test2 @0 @1))
2113   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2114        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2115    { constant_boolean_node (false, type); })))
2117 /* -A CMP -B -> B CMP A.  */
2118 (for cmp (tcc_comparison)
2119      scmp (swapped_tcc_comparison)
2120  (simplify
2121   (cmp (negate @0) (negate @1))
2122   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2123        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2124            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2125    (scmp @0 @1)))
2126  (simplify
2127   (cmp (negate @0) CONSTANT_CLASS_P@1)
2128   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2129        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2130            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2131    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2132     (if (tem && !TREE_OVERFLOW (tem))
2133      (scmp @0 { tem; }))))))
2135 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
2136 (for op (eq ne)
2137  (simplify
2138   (op (abs @0) zerop@1)
2139   (op @0 @1)))
2141 /* From fold_sign_changed_comparison and fold_widened_comparison.  */
2142 (for cmp (simple_comparison)
2143  (simplify
2144   (cmp (convert@0 @00) (convert?@1 @10))
2145   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2146        /* Disable this optimization if we're casting a function pointer
2147           type on targets that require function pointer canonicalization.  */
2148        && !(targetm.have_canonicalize_funcptr_for_compare ()
2149             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2150             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2151        && single_use (@0))
2152    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2153         && (TREE_CODE (@10) == INTEGER_CST
2154             || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2155         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2156             || cmp == NE_EXPR
2157             || cmp == EQ_EXPR)
2158         && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2159     /* ???  The special-casing of INTEGER_CST conversion was in the original
2160        code and here to avoid a spurious overflow flag on the resulting
2161        constant which fold_convert produces.  */
2162     (if (TREE_CODE (@1) == INTEGER_CST)
2163      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2164                                 TREE_OVERFLOW (@1)); })
2165      (cmp @00 (convert @1)))
2167     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2168      /* If possible, express the comparison in the shorter mode.  */
2169      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2170            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2171           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2172               || ((TYPE_PRECISION (TREE_TYPE (@00))
2173                    >= TYPE_PRECISION (TREE_TYPE (@10)))
2174                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
2175                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
2176               || (TREE_CODE (@10) == INTEGER_CST
2177                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2178                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
2179       (cmp @00 (convert @10))
2180       (if (TREE_CODE (@10) == INTEGER_CST
2181            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2182            && !int_fits_type_p (@10, TREE_TYPE (@00)))
2183        (with
2184         {
2185           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2186           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2187           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2188           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2189         }
2190         (if (above || below)
2191          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2192           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2193           (if (cmp == LT_EXPR || cmp == LE_EXPR)
2194            { constant_boolean_node (above ? true : false, type); }
2195            (if (cmp == GT_EXPR || cmp == GE_EXPR)
2196             { constant_boolean_node (above ? false : true, type); }))))))))))))
2198 (for cmp (eq ne)
2199  /* A local variable can never be pointed to by
2200     the default SSA name of an incoming parameter.
2201     SSA names are canonicalized to 2nd place.  */
2202  (simplify
2203   (cmp addr@0 SSA_NAME@1)
2204   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2205        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2206    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2207     (if (TREE_CODE (base) == VAR_DECL
2208          && auto_var_in_fn_p (base, current_function_decl))
2209      (if (cmp == NE_EXPR)
2210       { constant_boolean_node (true, type); }
2211       { constant_boolean_node (false, type); }))))))
2213 /* Equality compare simplifications from fold_binary  */
2214 (for cmp (eq ne)
2216  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2217     Similarly for NE_EXPR.  */
2218  (simplify
2219   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2220   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2221        && wi::bit_and_not (@1, @2) != 0)
2222    { constant_boolean_node (cmp == NE_EXPR, type); }))
2224  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
2225  (simplify
2226   (cmp (bit_xor @0 @1) integer_zerop)
2227   (cmp @0 @1))
2229  /* (X ^ Y) == Y becomes X == 0.
2230     Likewise (X ^ Y) == X becomes Y == 0.  */
2231  (simplify
2232   (cmp:c (bit_xor:c @0 @1) @0)
2233   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2235  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
2236  (simplify
2237   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2238   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2239    (cmp @0 (bit_xor @1 (convert @2)))))
2241  (simplify
2242   (cmp (convert? addr@0) integer_zerop)
2243   (if (tree_single_nonzero_warnv_p (@0, NULL))
2244    { constant_boolean_node (cmp == NE_EXPR, type); })))
2246 /* If we have (A & C) == C where C is a power of 2, convert this into
2247    (A & C) != 0.  Similarly for NE_EXPR.  */
2248 (for cmp (eq ne)
2249      icmp (ne eq)
2250  (simplify
2251   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2252   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2254 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2255    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
2256 (for cmp (eq ne)
2257      ncmp (ge lt)
2258  (simplify
2259   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2260   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2261        && (TYPE_PRECISION (TREE_TYPE (@0))
2262            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2263        && element_precision (@2) >= element_precision (@0)
2264        && wi::only_sign_bit_p (@1, element_precision (@0)))
2265    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2266     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2268 /* When the addresses are not directly of decls compare base and offset.
2269    This implements some remaining parts of fold_comparison address
2270    comparisons but still no complete part of it.  Still it is good
2271    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
2272 (for cmp (simple_comparison)
2273  (simplify
2274   (cmp (convert1?@2 addr@0) (convert2? addr@1))
2275   (with
2276    {
2277      HOST_WIDE_INT off0, off1;
2278      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2279      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2280      if (base0 && TREE_CODE (base0) == MEM_REF)
2281        {
2282          off0 += mem_ref_offset (base0).to_short_addr ();
2283          base0 = TREE_OPERAND (base0, 0);
2284        }
2285      if (base1 && TREE_CODE (base1) == MEM_REF)
2286        {
2287          off1 += mem_ref_offset (base1).to_short_addr ();
2288          base1 = TREE_OPERAND (base1, 0);
2289        }
2290    }
2291    (if (base0 && base1)
2292     (with
2293      {
2294        int equal = 2;
2295        if (decl_in_symtab_p (base0)
2296            && decl_in_symtab_p (base1))
2297          equal = symtab_node::get_create (base0)
2298                    ->equal_address_to (symtab_node::get_create (base1));
2299        else if ((DECL_P (base0)
2300                  || TREE_CODE (base0) == SSA_NAME
2301                  || TREE_CODE (base0) == STRING_CST)
2302                 && (DECL_P (base1)
2303                     || TREE_CODE (base1) == SSA_NAME
2304                     || TREE_CODE (base1) == STRING_CST))
2305          equal = (base0 == base1);
2306      }
2307      (if (equal == 1
2308           && (cmp == EQ_EXPR || cmp == NE_EXPR
2309               /* If the offsets are equal we can ignore overflow.  */
2310               || off0 == off1
2311               || POINTER_TYPE_OVERFLOW_UNDEFINED
2312               /* Or if we compare using pointers to decls or strings.  */
2313               || (POINTER_TYPE_P (TREE_TYPE (@2))
2314                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2315       (switch
2316        (if (cmp == EQ_EXPR)
2317         { constant_boolean_node (off0 == off1, type); })
2318        (if (cmp == NE_EXPR)
2319         { constant_boolean_node (off0 != off1, type); })
2320        (if (cmp == LT_EXPR)
2321         { constant_boolean_node (off0 < off1, type); })
2322        (if (cmp == LE_EXPR)
2323         { constant_boolean_node (off0 <= off1, type); })
2324        (if (cmp == GE_EXPR)
2325         { constant_boolean_node (off0 >= off1, type); })
2326        (if (cmp == GT_EXPR)
2327         { constant_boolean_node (off0 > off1, type); }))
2328       (if (equal == 0
2329            && DECL_P (base0) && DECL_P (base1)
2330            /* If we compare this as integers require equal offset.  */
2331            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2332                || off0 == off1))
2333        (switch
2334         (if (cmp == EQ_EXPR)
2335          { constant_boolean_node (false, type); })
2336         (if (cmp == NE_EXPR)
2337          { constant_boolean_node (true, type); })))))))))
2339 /* Non-equality compare simplifications from fold_binary  */
2340 (for cmp (lt gt le ge)
2341  /* Comparisons with the highest or lowest possible integer of
2342     the specified precision will have known values.  */
2343  (simplify
2344   (cmp (convert?@2 @0) INTEGER_CST@1)
2345   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2346        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2347    (with
2348     {
2349       tree arg1_type = TREE_TYPE (@1);
2350       unsigned int prec = TYPE_PRECISION (arg1_type);
2351       wide_int max = wi::max_value (arg1_type);
2352       wide_int signed_max = wi::max_value (prec, SIGNED);
2353       wide_int min = wi::min_value (arg1_type);
2354     }
2355     (switch
2356      (if (wi::eq_p (@1, max))
2357       (switch
2358        (if (cmp == GT_EXPR)
2359         { constant_boolean_node (false, type); })
2360        (if (cmp == GE_EXPR)
2361         (eq @2 @1))
2362        (if (cmp == LE_EXPR)
2363         { constant_boolean_node (true, type); })
2364        (if (cmp == LT_EXPR)
2365         (ne @2 @1))))
2366      (if (wi::eq_p (@1, min))
2367       (switch
2368        (if (cmp == LT_EXPR)
2369         { constant_boolean_node (false, type); })
2370        (if (cmp == LE_EXPR)
2371         (eq @2 @1))
2372        (if (cmp == GE_EXPR)
2373         { constant_boolean_node (true, type); })
2374        (if (cmp == GT_EXPR)
2375         (ne @2 @1))))
2376      (if (wi::eq_p (@1, max - 1))
2377       (switch
2378        (if (cmp == GT_EXPR)
2379         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2380        (if (cmp == LE_EXPR)
2381         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2382      (if (wi::eq_p (@1, min + 1))
2383       (switch
2384        (if (cmp == GE_EXPR)
2385         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2386        (if (cmp == LT_EXPR)
2387         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2388      (if (wi::eq_p (@1, signed_max)
2389           && TYPE_UNSIGNED (arg1_type)
2390           /* We will flip the signedness of the comparison operator
2391              associated with the mode of @1, so the sign bit is
2392              specified by this mode.  Check that @1 is the signed
2393              max associated with this sign bit.  */
2394           && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2395           /* signed_type does not work on pointer types.  */
2396           && INTEGRAL_TYPE_P (arg1_type))
2397       /* The following case also applies to X < signed_max+1
2398          and X >= signed_max+1 because previous transformations.  */
2399       (if (cmp == LE_EXPR || cmp == GT_EXPR)
2400        (with { tree st = signed_type_for (arg1_type); }
2401         (if (cmp == LE_EXPR)
2402          (ge (convert:st @0) { build_zero_cst (st); })
2403          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2405 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2406  /* If the second operand is NaN, the result is constant.  */
2407  (simplify
2408   (cmp @0 REAL_CST@1)
2409   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2410        && (cmp != LTGT_EXPR || ! flag_trapping_math))
2411    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2412                             ? false : true, type); })))
2414 /* bool_var != 0 becomes bool_var.  */
2415 (simplify
2416  (ne @0 integer_zerop)
2417  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2418       && types_match (type, TREE_TYPE (@0)))
2419   (non_lvalue @0)))
2420 /* bool_var == 1 becomes bool_var.  */
2421 (simplify
2422  (eq @0 integer_onep)
2423  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2424       && types_match (type, TREE_TYPE (@0)))
2425   (non_lvalue @0)))
2426 /* Do not handle
2427    bool_var == 0 becomes !bool_var or
2428    bool_var != 1 becomes !bool_var
2429    here because that only is good in assignment context as long
2430    as we require a tcc_comparison in GIMPLE_CONDs where we'd
2431    replace if (x == 0) with tem = ~x; if (tem != 0) which is
2432    clearly less optimal and which we'll transform again in forwprop.  */
2435 /* Simplification of math builtins.  These rules must all be optimizations
2436    as well as IL simplifications.  If there is a possibility that the new
2437    form could be a pessimization, the rule should go in the canonicalization
2438    section that follows this one.
2440    Rules can generally go in this section if they satisfy one of
2441    the following:
2443    - the rule describes an identity
2445    - the rule replaces calls with something as simple as addition or
2446      multiplication
2448    - the rule contains unary calls only and simplifies the surrounding
2449      arithmetic.  (The idea here is to exclude non-unary calls in which
2450      one operand is constant and in which the call is known to be cheap
2451      when the operand has that value.)  */
2453 (if (flag_unsafe_math_optimizations)
2454  /* Simplify sqrt(x) * sqrt(x) -> x.  */
2455  (simplify
2456   (mult (SQRT@1 @0) @1)
2457   (if (!HONOR_SNANS (type))
2458    @0))
2460  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
2461  (for root (SQRT CBRT)
2462   (simplify
2463    (mult (root:s @0) (root:s @1))
2464     (root (mult @0 @1))))
2466  /* Simplify expN(x) * expN(y) -> expN(x+y). */
2467  (for exps (EXP EXP2 EXP10 POW10)
2468   (simplify
2469    (mult (exps:s @0) (exps:s @1))
2470     (exps (plus @0 @1))))
2472  /* Simplify a/root(b/c) into a*root(c/b).  */
2473  (for root (SQRT CBRT)
2474   (simplify
2475    (rdiv @0 (root:s (rdiv:s @1 @2)))
2476     (mult @0 (root (rdiv @2 @1)))))
2478  /* Simplify x/expN(y) into x*expN(-y).  */
2479  (for exps (EXP EXP2 EXP10 POW10)
2480   (simplify
2481    (rdiv @0 (exps:s @1))
2482     (mult @0 (exps (negate @1)))))
2484  (for logs (LOG LOG2 LOG10 LOG10)
2485       exps (EXP EXP2 EXP10 POW10)
2486   /* logN(expN(x)) -> x.  */
2487   (simplify
2488    (logs (exps @0))
2489    @0)
2490   /* expN(logN(x)) -> x.  */
2491   (simplify
2492    (exps (logs @0))
2493    @0))
2495  /* Optimize logN(func()) for various exponential functions.  We
2496     want to determine the value "x" and the power "exponent" in
2497     order to transform logN(x**exponent) into exponent*logN(x).  */
2498  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
2499       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
2500   (simplify
2501    (logs (exps @0))
2502    (if (SCALAR_FLOAT_TYPE_P (type))
2503     (with {
2504       tree x;
2505       switch (exps)
2506         {
2507         CASE_CFN_EXP:
2508           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
2509           x = build_real_truncate (type, dconst_e ());
2510           break;
2511         CASE_CFN_EXP2:
2512           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
2513           x = build_real (type, dconst2);
2514           break;
2515         CASE_CFN_EXP10:
2516         CASE_CFN_POW10:
2517           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
2518           {
2519             REAL_VALUE_TYPE dconst10;
2520             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2521             x = build_real (type, dconst10);
2522           }
2523           break;
2524         default:
2525           gcc_unreachable ();
2526         }
2527       }
2528      (mult (logs { x; }) @0)))))
2530  (for logs (LOG LOG
2531             LOG2 LOG2
2532             LOG10 LOG10)
2533       exps (SQRT CBRT)
2534   (simplify
2535    (logs (exps @0))
2536    (if (SCALAR_FLOAT_TYPE_P (type))
2537     (with {
2538       tree x;
2539       switch (exps)
2540         {
2541         CASE_CFN_SQRT:
2542           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
2543           x = build_real (type, dconsthalf);
2544           break;
2545         CASE_CFN_CBRT:
2546           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
2547           x = build_real_truncate (type, dconst_third ());
2548           break;
2549         default:
2550           gcc_unreachable ();
2551         }
2552       }
2553      (mult { x; } (logs @0))))))
2555  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
2556  (for logs (LOG LOG2 LOG10)
2557       pows (POW)
2558   (simplify
2559    (logs (pows @0 @1))
2560    (mult @1 (logs @0))))
2562  (for sqrts (SQRT)
2563       cbrts (CBRT)
2564       pows (POW)
2565       exps (EXP EXP2 EXP10 POW10)
2566   /* sqrt(expN(x)) -> expN(x*0.5).  */
2567   (simplify
2568    (sqrts (exps @0))
2569    (exps (mult @0 { build_real (type, dconsthalf); })))
2570   /* cbrt(expN(x)) -> expN(x/3).  */
2571   (simplify
2572    (cbrts (exps @0))
2573    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2574   /* pow(expN(x), y) -> expN(x*y).  */
2575   (simplify
2576    (pows (exps @0) @1)
2577    (exps (mult @0 @1))))
2579  /* tan(atan(x)) -> x.  */
2580  (for tans (TAN)
2581       atans (ATAN)
2582   (simplify
2583    (tans (atans @0))
2584    @0)))
2586 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
2587 (simplify
2588  (CABS (complex:c @0 real_zerop@1))
2589  (abs @0))
2591 /* trunc(trunc(x)) -> trunc(x), etc.  */
2592 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2593  (simplify
2594   (fns (fns @0))
2595   (fns @0)))
2596 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
2597 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2598  (simplify
2599   (fns integer_valued_real_p@0)
2600   @0))
2602 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
2603 (simplify
2604  (HYPOT:c @0 real_zerop@1)
2605  (abs @0))
2607 /* pow(1,x) -> 1.  */
2608 (simplify
2609  (POW real_onep@0 @1)
2610  @0)
2612 (simplify
2613  /* copysign(x,x) -> x.  */
2614  (COPYSIGN @0 @0)
2615  @0)
2617 (simplify
2618  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
2619  (COPYSIGN @0 tree_expr_nonnegative_p@1)
2620  (abs @0))
2622 (for scale (LDEXP SCALBN SCALBLN)
2623  /* ldexp(0, x) -> 0.  */
2624  (simplify
2625   (scale real_zerop@0 @1)
2626   @0)
2627  /* ldexp(x, 0) -> x.  */
2628  (simplify
2629   (scale @0 integer_zerop@1)
2630   @0)
2631  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
2632  (simplify
2633   (scale REAL_CST@0 @1)
2634   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2635    @0)))
2637 /* Canonicalization of sequences of math builtins.  These rules represent
2638    IL simplifications but are not necessarily optimizations.
2640    The sincos pass is responsible for picking "optimal" implementations
2641    of math builtins, which may be more complicated and can sometimes go
2642    the other way, e.g. converting pow into a sequence of sqrts.
2643    We only want to do these canonicalizations before the pass has run.  */
2645 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2646  /* Simplify tan(x) * cos(x) -> sin(x). */
2647  (simplify
2648   (mult:c (TAN:s @0) (COS:s @0))
2649    (SIN @0))
2651  /* Simplify x * pow(x,c) -> pow(x,c+1). */
2652  (simplify
2653   (mult @0 (POW:s @0 REAL_CST@1))
2654   (if (!TREE_OVERFLOW (@1))
2655    (POW @0 (plus @1 { build_one_cst (type); }))))
2657  /* Simplify sin(x) / cos(x) -> tan(x). */
2658  (simplify
2659   (rdiv (SIN:s @0) (COS:s @0))
2660    (TAN @0))
2662  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2663  (simplify
2664   (rdiv (COS:s @0) (SIN:s @0))
2665    (rdiv { build_one_cst (type); } (TAN @0)))
2667  /* Simplify sin(x) / tan(x) -> cos(x). */
2668  (simplify
2669   (rdiv (SIN:s @0) (TAN:s @0))
2670   (if (! HONOR_NANS (@0)
2671        && ! HONOR_INFINITIES (@0))
2672    (COS @0)))
2674  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2675  (simplify
2676   (rdiv (TAN:s @0) (SIN:s @0))
2677   (if (! HONOR_NANS (@0)
2678        && ! HONOR_INFINITIES (@0))
2679    (rdiv { build_one_cst (type); } (COS @0))))
2681  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2682  (simplify
2683   (mult (POW:s @0 @1) (POW:s @0 @2))
2684    (POW @0 (plus @1 @2)))
2686  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2687  (simplify
2688   (mult (POW:s @0 @1) (POW:s @2 @1))
2689    (POW (mult @0 @2) @1))
2691  /* Simplify pow(x,c) / x -> pow(x,c-1). */
2692  (simplify
2693   (rdiv (POW:s @0 REAL_CST@1) @0)
2694   (if (!TREE_OVERFLOW (@1))
2695    (POW @0 (minus @1 { build_one_cst (type); }))))
2697  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2698  (simplify
2699   (rdiv @0 (POW:s @1 @2))
2700    (mult @0 (POW @1 (negate @2))))
2702  (for sqrts (SQRT)
2703       cbrts (CBRT)
2704       pows (POW)
2705   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
2706   (simplify
2707    (sqrts (sqrts @0))
2708    (pows @0 { build_real (type, dconst_quarter ()); }))
2709   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
2710   (simplify
2711    (sqrts (cbrts @0))
2712    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2713   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
2714   (simplify
2715    (cbrts (sqrts @0))
2716    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2717   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
2718   (simplify
2719    (cbrts (cbrts tree_expr_nonnegative_p@0))
2720    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2721   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
2722   (simplify
2723    (sqrts (pows @0 @1))
2724    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2725   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
2726   (simplify
2727    (cbrts (pows tree_expr_nonnegative_p@0 @1))
2728    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2729   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
2730   (simplify
2731    (pows (sqrts @0) @1)
2732    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2733   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
2734   (simplify
2735    (pows (cbrts tree_expr_nonnegative_p@0) @1)
2736    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2737   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
2738   (simplify
2739    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2740    (pows @0 (mult @1 @2))))
2742  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
2743  (simplify
2744   (CABS (complex @0 @0))
2745   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2747  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
2748  (simplify
2749   (HYPOT @0 @0)
2750   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2752  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
2753  (for cexps (CEXP)
2754       exps (EXP)
2755       cexpis (CEXPI)
2756   (simplify
2757    (cexps compositional_complex@0)
2758    (if (targetm.libc_has_function (function_c99_math_complex))
2759     (complex
2760      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2761      (mult @1 (imagpart @2)))))))
2763 (if (canonicalize_math_p ())
2764  /* floor(x) -> trunc(x) if x is nonnegative.  */
2765  (for floors (FLOOR)
2766       truncs (TRUNC)
2767   (simplify
2768    (floors tree_expr_nonnegative_p@0)
2769    (truncs @0))))
2771 (match double_value_p
2772  @0
2773  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2774 (for froms (BUILT_IN_TRUNCL
2775             BUILT_IN_FLOORL
2776             BUILT_IN_CEILL
2777             BUILT_IN_ROUNDL
2778             BUILT_IN_NEARBYINTL
2779             BUILT_IN_RINTL)
2780      tos (BUILT_IN_TRUNC
2781           BUILT_IN_FLOOR
2782           BUILT_IN_CEIL
2783           BUILT_IN_ROUND
2784           BUILT_IN_NEARBYINT
2785           BUILT_IN_RINT)
2786  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
2787  (if (optimize && canonicalize_math_p ())
2788   (simplify
2789    (froms (convert double_value_p@0))
2790    (convert (tos @0)))))
2792 (match float_value_p
2793  @0
2794  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
2795 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
2796             BUILT_IN_FLOORL BUILT_IN_FLOOR
2797             BUILT_IN_CEILL BUILT_IN_CEIL
2798             BUILT_IN_ROUNDL BUILT_IN_ROUND
2799             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
2800             BUILT_IN_RINTL BUILT_IN_RINT)
2801      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
2802           BUILT_IN_FLOORF BUILT_IN_FLOORF
2803           BUILT_IN_CEILF BUILT_IN_CEILF
2804           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
2805           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
2806           BUILT_IN_RINTF BUILT_IN_RINTF)
2807  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
2808     if x is a float.  */
2809  (if (optimize && canonicalize_math_p ()
2810       && targetm.libc_has_function (function_c99_misc))
2811   (simplify
2812    (froms (convert float_value_p@0))
2813    (convert (tos @0)))))
2815 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
2816      tos (XFLOOR XCEIL XROUND XRINT)
2817  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
2818  (if (optimize && canonicalize_math_p ())
2819   (simplify
2820    (froms (convert double_value_p@0))
2821    (tos @0))))
2823 (for froms (XFLOORL XCEILL XROUNDL XRINTL
2824             XFLOOR XCEIL XROUND XRINT)
2825      tos (XFLOORF XCEILF XROUNDF XRINTF)
2826  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
2827     if x is a float.  */
2828  (if (optimize && canonicalize_math_p ())
2829   (simplify
2830    (froms (convert float_value_p@0))
2831    (tos @0))))
2833 (if (canonicalize_math_p ())
2834  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
2835  (for floors (IFLOOR LFLOOR LLFLOOR)
2836   (simplify
2837    (floors tree_expr_nonnegative_p@0)
2838    (fix_trunc @0))))
2840 (if (canonicalize_math_p ())
2841  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
2842  (for fns (IFLOOR LFLOOR LLFLOOR
2843            ICEIL LCEIL LLCEIL
2844            IROUND LROUND LLROUND)
2845   (simplify
2846    (fns integer_valued_real_p@0)
2847    (fix_trunc @0)))
2848  (if (!flag_errno_math)
2849   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
2850   (for rints (IRINT LRINT LLRINT)
2851    (simplify
2852     (rints integer_valued_real_p@0)
2853     (fix_trunc @0)))))
2855 (if (canonicalize_math_p ())
2856  (for ifn (IFLOOR ICEIL IROUND IRINT)
2857       lfn (LFLOOR LCEIL LROUND LRINT)
2858       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
2859   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
2860      sizeof (int) == sizeof (long).  */
2861   (if (TYPE_PRECISION (integer_type_node)
2862        == TYPE_PRECISION (long_integer_type_node))
2863    (simplify
2864     (ifn @0)
2865     (lfn:long_integer_type_node @0)))
2866   /* Canonicalize llround (x) to lround (x) on LP64 targets where
2867      sizeof (long long) == sizeof (long).  */
2868   (if (TYPE_PRECISION (long_long_integer_type_node)
2869        == TYPE_PRECISION (long_integer_type_node))
2870    (simplify
2871     (llfn @0)
2872     (lfn:long_integer_type_node @0)))))
2874 /* cproj(x) -> x if we're ignoring infinities.  */
2875 (simplify
2876  (CPROJ @0)
2877  (if (!HONOR_INFINITIES (type))
2878    @0))
2880 /* If the real part is inf and the imag part is known to be
2881    nonnegative, return (inf + 0i).  */
2882 (simplify
2883  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2884  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2885   { build_complex_inf (type, false); }))
2887 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
2888 (simplify
2889  (CPROJ (complex @0 REAL_CST@1))
2890  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2891   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2893 (for pows (POW)
2894      sqrts (SQRT)
2895      cbrts (CBRT)
2896  (simplify
2897   (pows @0 REAL_CST@1)
2898   (with {
2899     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
2900     REAL_VALUE_TYPE tmp;
2901    }
2902    (switch
2903     /* pow(x,0) -> 1.  */
2904     (if (real_equal (value, &dconst0))
2905      { build_real (type, dconst1); })
2906     /* pow(x,1) -> x.  */
2907     (if (real_equal (value, &dconst1))
2908      @0)
2909     /* pow(x,-1) -> 1/x.  */
2910     (if (real_equal (value, &dconstm1))
2911      (rdiv { build_real (type, dconst1); } @0))
2912     /* pow(x,0.5) -> sqrt(x).  */
2913     (if (flag_unsafe_math_optimizations
2914          && canonicalize_math_p ()
2915          && real_equal (value, &dconsthalf))
2916      (sqrts @0))
2917     /* pow(x,1/3) -> cbrt(x).  */
2918     (if (flag_unsafe_math_optimizations
2919          && canonicalize_math_p ()
2920          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
2921              real_equal (value, &tmp)))
2922      (cbrts @0))))))
2924 /* powi(1,x) -> 1.  */
2925 (simplify
2926  (POWI real_onep@0 @1)
2927  @0)
2929 (simplify
2930  (POWI @0 INTEGER_CST@1)
2931  (switch
2932   /* powi(x,0) -> 1.  */
2933   (if (wi::eq_p (@1, 0))
2934    { build_real (type, dconst1); })
2935   /* powi(x,1) -> x.  */
2936   (if (wi::eq_p (@1, 1))
2937    @0)
2938   /* powi(x,-1) -> 1/x.  */
2939   (if (wi::eq_p (@1, -1))
2940    (rdiv { build_real (type, dconst1); } @0))))
2942 /* Narrowing of arithmetic and logical operations. 
2944    These are conceptually similar to the transformations performed for
2945    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
2946    term we want to move all that code out of the front-ends into here.  */
2948 /* If we have a narrowing conversion of an arithmetic operation where
2949    both operands are widening conversions from the same type as the outer
2950    narrowing conversion.  Then convert the innermost operands to a suitable
2951    unsigned type (to avoid introducing undefined behavior), perform the
2952    operation and convert the result to the desired type.  */
2953 (for op (plus minus)
2954   (simplify
2955     (convert (op:s (convert@2 @0) (convert@3 @1)))
2956     (if (INTEGRAL_TYPE_P (type)
2957          /* We check for type compatibility between @0 and @1 below,
2958             so there's no need to check that @1/@3 are integral types.  */
2959          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2960          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2961          /* The precision of the type of each operand must match the
2962             precision of the mode of each operand, similarly for the
2963             result.  */
2964          && (TYPE_PRECISION (TREE_TYPE (@0))
2965              == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2966          && (TYPE_PRECISION (TREE_TYPE (@1))
2967              == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2968          && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2969          /* The inner conversion must be a widening conversion.  */
2970          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2971          && types_match (@0, @1)
2972          && types_match (@0, type))
2973       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2974         (convert (op @0 @1))
2975         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2976          (convert (op (convert:utype @0) (convert:utype @1))))))))
2978 /* This is another case of narrowing, specifically when there's an outer
2979    BIT_AND_EXPR which masks off bits outside the type of the innermost
2980    operands.   Like the previous case we have to convert the operands
2981    to unsigned types to avoid introducing undefined behavior for the
2982    arithmetic operation.  */
2983 (for op (minus plus)
2984  (simplify
2985   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2986   (if (INTEGRAL_TYPE_P (type)
2987        /* We check for type compatibility between @0 and @1 below,
2988           so there's no need to check that @1/@3 are integral types.  */
2989        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2990        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2991        /* The precision of the type of each operand must match the
2992           precision of the mode of each operand, similarly for the
2993           result.  */
2994        && (TYPE_PRECISION (TREE_TYPE (@0))
2995            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2996        && (TYPE_PRECISION (TREE_TYPE (@1))
2997            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2998        && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2999        /* The inner conversion must be a widening conversion.  */
3000        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3001        && types_match (@0, @1)
3002        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3003            <= TYPE_PRECISION (TREE_TYPE (@0)))
3004        && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3005                         true, TYPE_PRECISION (type))) == 0))
3006    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3007     (with { tree ntype = TREE_TYPE (@0); }
3008      (convert (bit_and (op @0 @1) (convert:ntype @4))))
3009     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3010      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3011                (convert:utype @4))))))))
3013 /* Transform (@0 < @1 and @0 < @2) to use min, 
3014    (@0 > @1 and @0 > @2) to use max */
3015 (for op (lt le gt ge)
3016      ext (min min max max)
3017  (simplify
3018   (bit_and (op:s @0 @1) (op:s @0 @2))
3019   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3020    (op @0 (ext @1 @2)))))
3022 (simplify
3023  /* signbit(x) -> 0 if x is nonnegative.  */
3024  (SIGNBIT tree_expr_nonnegative_p@0)
3025  { integer_zero_node; })
3027 (simplify
3028  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
3029  (SIGNBIT @0)
3030  (if (!HONOR_SIGNED_ZEROS (@0))
3031   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))