Update .po files.
[official-gcc.git] / gcc / match.pd
blob0e63328c43c911ff0e5de3c5f51729c9365d95ff
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  /* Likewise for powi.  */
370  (for pows (POWI)
371   (simplify
372    (pows (op @0) INTEGER_CST@1)
373    (if (wi::bit_and (@1, 1) == 0)
374     (pows @0 @1))))
375  /* Strip negate and abs from both operands of hypot.  */
376  (for hypots (HYPOT)
377   (simplify
378    (hypots (op @0) @1)
379    (hypots @0 @1))
380   (simplify
381    (hypots @0 (op @1))
382    (hypots @0 @1)))
383  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
384  (for copysigns (COPYSIGN)
385   (simplify
386    (copysigns (op @0) @1)
387    (copysigns @0 @1))))
389 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
390 (simplify
391  (mult (abs@1 @0) @1)
392  (mult @0 @0))
394 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
395 (for coss (COS COSH)
396      copysigns (COPYSIGN)
397  (simplify
398   (coss (copysigns @0 @1))
399    (coss @0)))
401 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
402 (for pows (POW)
403      copysigns (COPYSIGN)
404  (simplify
405   (pows (copysigns @0 @2) REAL_CST@1)
406   (with { HOST_WIDE_INT n; }
407    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
408     (pows @0 @1)))))
409 /* Likewise for powi.  */
410 (for pows (POWI)
411      copysigns (COPYSIGN)
412  (simplify
413   (pows (copysigns @0 @2) INTEGER_CST@1)
414   (if (wi::bit_and (@1, 1) == 0)
415    (pows @0 @1))))
417 (for hypots (HYPOT)
418      copysigns (COPYSIGN)
419  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
420  (simplify
421   (hypots (copysigns @0 @1) @2)
422   (hypots @0 @2))
423  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
424  (simplify
425   (hypots @0 (copysigns @1 @2))
426   (hypots @0 @1)))
428 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
429 (for copysigns (COPYSIGN)
430  (simplify
431   (copysigns (copysigns @0 @1) @2)
432   (copysigns @0 @2)))
434 /* copysign(x,y)*copysign(x,y) -> x*x.  */
435 (for copysigns (COPYSIGN)
436  (simplify
437   (mult (copysigns@2 @0 @1) @2)
438   (mult @0 @0)))
440 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
441 (for ccoss (CCOS CCOSH)
442  (simplify
443   (ccoss (negate @0))
444    (ccoss @0)))
446 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
447 (for ops (conj negate)
448  (for cabss (CABS)
449   (simplify
450    (cabss (ops @0))
451    (cabss @0))))
453 /* Fold (a * (1 << b)) into (a << b)  */
454 (simplify
455  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
456   (if (! FLOAT_TYPE_P (type)
457        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
458    (lshift @0 @2)))
460 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
461 (simplify
462  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
463   (if (flag_associative_math
464        && single_use (@3))
465    (with
466     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
467     (if (tem)
468      (rdiv { tem; } @1)))))
470 /* Convert C1/(X*C2) into (C1/C2)/X  */
471 (simplify
472  (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
473   (if (flag_reciprocal_math)
474    (with
475     { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
476     (if (tem)
477      (rdiv { tem; } @1)))))
479 /* Simplify ~X & X as zero.  */
480 (simplify
481  (bit_and:c (convert? @0) (convert? (bit_not @0)))
482   { build_zero_cst (type); })
484 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
485 (simplify
486  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
487   (minus (bit_xor @0 @1) @1))
488 (simplify
489  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
490  (if (wi::bit_not (@2) == @1)
491   (minus (bit_xor @0 @1) @1)))
493 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
494 (simplify
495  (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
496   (minus @1 (bit_xor @0 @1)))
498 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y.  */
499 (simplify
500  (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
501   (bit_xor @0 @1))
502 (simplify
503  (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
504  (if (wi::bit_not (@2) == @1)
505   (bit_xor @0 @1)))
507 /* X % Y is smaller than Y.  */
508 (for cmp (lt ge)
509  (simplify
510   (cmp (trunc_mod @0 @1) @1)
511   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
512    { constant_boolean_node (cmp == LT_EXPR, type); })))
513 (for cmp (gt le)
514  (simplify
515   (cmp @1 (trunc_mod @0 @1))
516   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
517    { constant_boolean_node (cmp == GT_EXPR, type); })))
519 /* x | ~0 -> ~0  */
520 (simplify
521   (bit_ior @0 integer_all_onesp@1)
522   @1)
524 /* x & 0 -> 0  */
525 (simplify
526   (bit_and @0 integer_zerop@1)
527   @1)
529 /* ~x | x -> -1 */
530 /* ~x ^ x -> -1 */
531 /* ~x + x -> -1 */
532 (for op (bit_ior bit_xor plus)
533  (simplify
534   (op:c (convert? @0) (convert? (bit_not @0)))
535   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
537 /* x ^ x -> 0 */
538 (simplify
539   (bit_xor @0 @0)
540   { build_zero_cst (type); })
542 /* Canonicalize X ^ ~0 to ~X.  */
543 (simplify
544   (bit_xor @0 integer_all_onesp@1)
545   (bit_not @0))
547 /* x & ~0 -> x  */
548 (simplify
549  (bit_and @0 integer_all_onesp)
550   (non_lvalue @0))
552 /* x & x -> x,  x | x -> x  */
553 (for bitop (bit_and bit_ior)
554  (simplify
555   (bitop @0 @0)
556   (non_lvalue @0)))
558 /* x + (x & 1) -> (x + 1) & ~1 */
559 (simplify
560  (plus:c @0 (bit_and:s @0 integer_onep@1))
561  (bit_and (plus @0 @1) (bit_not @1)))
563 /* x & ~(x & y) -> x & ~y */
564 /* x | ~(x | y) -> x | ~y  */
565 (for bitop (bit_and bit_ior)
566  (simplify
567   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
568   (bitop @0 (bit_not @1))))
570 /* (x | y) & ~x -> y & ~x */
571 /* (x & y) | ~x -> y | ~x */
572 (for bitop (bit_and bit_ior)
573      rbitop (bit_ior bit_and)
574  (simplify
575   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
576   (bitop @1 @2)))
578 /* (x & y) ^ (x | y) -> x ^ y */
579 (simplify
580  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
581  (bit_xor @0 @1))
583 /* (x ^ y) ^ (x | y) -> x & y */
584 (simplify
585  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
586  (bit_and @0 @1))
588 /* (x & y) + (x ^ y) -> x | y */
589 /* (x & y) | (x ^ y) -> x | y */
590 /* (x & y) ^ (x ^ y) -> x | y */
591 (for op (plus bit_ior bit_xor)
592  (simplify
593   (op:c (bit_and @0 @1) (bit_xor @0 @1))
594   (bit_ior @0 @1)))
596 /* (x & y) + (x | y) -> x + y */
597 (simplify
598  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
599  (plus @0 @1))
601 /* (x + y) - (x | y) -> x & y */
602 (simplify
603  (minus (plus @0 @1) (bit_ior @0 @1))
604  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
605       && !TYPE_SATURATING (type))
606   (bit_and @0 @1)))
608 /* (x + y) - (x & y) -> x | y */
609 (simplify
610  (minus (plus @0 @1) (bit_and @0 @1))
611  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
612       && !TYPE_SATURATING (type))
613   (bit_ior @0 @1)))
615 /* (x | y) - (x ^ y) -> x & y */
616 (simplify
617  (minus (bit_ior @0 @1) (bit_xor @0 @1))
618  (bit_and @0 @1))
620 /* (x | y) - (x & y) -> x ^ y */
621 (simplify
622  (minus (bit_ior @0 @1) (bit_and @0 @1))
623  (bit_xor @0 @1))
625 /* (x | y) & ~(x & y) -> x ^ y */
626 (simplify
627  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
628  (bit_xor @0 @1))
630 /* (x | y) & (~x ^ y) -> x & y */
631 (simplify
632  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
633  (bit_and @0 @1))
635 /* ~x & ~y -> ~(x | y)
636    ~x | ~y -> ~(x & y) */
637 (for op (bit_and bit_ior)
638      rop (bit_ior bit_and)
639  (simplify
640   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
641   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
642        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
643    (bit_not (rop (convert @0) (convert @1))))))
645 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
646    with a constant, and the two constants have no bits in common,
647    we should treat this as a BIT_IOR_EXPR since this may produce more
648    simplifications.  */
649 (for op (bit_xor plus)
650  (simplify
651   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
652       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
653   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
654        && tree_nop_conversion_p (type, TREE_TYPE (@2))
655        && wi::bit_and (@1, @3) == 0)
656    (bit_ior (convert @4) (convert @5)))))
658 /* (X | Y) ^ X -> Y & ~ X*/
659 (simplify
660  (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
661  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
662   (convert (bit_and @1 (bit_not @0)))))
664 /* Convert ~X ^ ~Y to X ^ Y.  */
665 (simplify
666  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
667  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
668       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
669   (bit_xor (convert @0) (convert @1))))
671 /* Convert ~X ^ C to X ^ ~C.  */
672 (simplify
673  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
674  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
675   (bit_xor (convert @0) (bit_not @1))))
677 /* Fold (X & Y) ^ Y as ~X & Y.  */
678 (simplify
679  (bit_xor:c (bit_and:c @0 @1) @1)
680  (bit_and (bit_not @0) @1))
682 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
683    operands are another bit-wise operation with a common input.  If so,
684    distribute the bit operations to save an operation and possibly two if
685    constants are involved.  For example, convert
686      (A | B) & (A | C) into A | (B & C)
687    Further simplification will occur if B and C are constants.  */
688 (for op (bit_and bit_ior)
689      rop (bit_ior bit_and)
690  (simplify
691   (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
692   (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
693    (rop (convert @0) (op (convert @1) (convert @2))))))
696 (simplify
697  (abs (abs@1 @0))
698  @1)
699 (simplify
700  (abs (negate @0))
701  (abs @0))
702 (simplify
703  (abs tree_expr_nonnegative_p@0)
704  @0)
706 /* A few cases of fold-const.c negate_expr_p predicate.  */
707 (match negate_expr_p
708  INTEGER_CST
709  (if ((INTEGRAL_TYPE_P (type)
710        && TYPE_OVERFLOW_WRAPS (type))
711       || (!TYPE_OVERFLOW_SANITIZED (type)
712           && may_negate_without_overflow_p (t)))))
713 (match negate_expr_p
714  FIXED_CST)
715 (match negate_expr_p
716  (negate @0)
717  (if (!TYPE_OVERFLOW_SANITIZED (type))))
718 (match negate_expr_p
719  REAL_CST
720  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
721 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
722    ways.  */
723 (match negate_expr_p
724  VECTOR_CST
725  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
727 /* (-A) * (-B) -> A * B  */
728 (simplify
729  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
730   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
731        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
732    (mult (convert @0) (convert (negate @1)))))
734 /* -(A + B) -> (-B) - A.  */
735 (simplify
736  (negate (plus:c @0 negate_expr_p@1))
737  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
738       && !HONOR_SIGNED_ZEROS (element_mode (type)))
739   (minus (negate @1) @0)))
741 /* A - B -> A + (-B) if B is easily negatable.  */
742 (simplify
743  (minus @0 negate_expr_p@1)
744  (if (!FIXED_POINT_TYPE_P (type))
745  (plus @0 (negate @1))))
747 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
748    when profitable.
749    For bitwise binary operations apply operand conversions to the
750    binary operation result instead of to the operands.  This allows
751    to combine successive conversions and bitwise binary operations.
752    We combine the above two cases by using a conditional convert.  */
753 (for bitop (bit_and bit_ior bit_xor)
754  (simplify
755   (bitop (convert @0) (convert? @1))
756   (if (((TREE_CODE (@1) == INTEGER_CST
757          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
758          && int_fits_type_p (@1, TREE_TYPE (@0)))
759         || types_match (@0, @1))
760        /* ???  This transform conflicts with fold-const.c doing
761           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
762           constants (if x has signed type, the sign bit cannot be set
763           in c).  This folds extension into the BIT_AND_EXPR.
764           Restrict it to GIMPLE to avoid endless recursions.  */
765        && (bitop != BIT_AND_EXPR || GIMPLE)
766        && (/* That's a good idea if the conversion widens the operand, thus
767               after hoisting the conversion the operation will be narrower.  */
768            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
769            /* It's also a good idea if the conversion is to a non-integer
770               mode.  */
771            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
772            /* Or if the precision of TO is not the same as the precision
773               of its mode.  */
774            || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
775    (convert (bitop @0 (convert @1))))))
777 (for bitop (bit_and bit_ior)
778      rbitop (bit_ior bit_and)
779   /* (x | y) & x -> x */
780   /* (x & y) | x -> x */
781  (simplify
782   (bitop:c (rbitop:c @0 @1) @0)
783   @0)
784  /* (~x | y) & x -> x & y */
785  /* (~x & y) | x -> x | y */
786  (simplify
787   (bitop:c (rbitop:c (bit_not @0) @1) @0)
788   (bitop @0 @1)))
790 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
791 (for bitop (bit_and bit_ior bit_xor)
792  (simplify
793   (bitop (bit_and:c @0 @1) (bit_and @2 @1))
794   (bit_and (bitop @0 @2) @1)))
796 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
797 (simplify
798   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
799   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
801 /* Combine successive equal operations with constants.  */
802 (for bitop (bit_and bit_ior bit_xor)
803  (simplify
804   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
805   (bitop @0 (bitop @1 @2))))
807 /* Try simple folding for X op !X, and X op X with the help
808    of the truth_valued_p and logical_inverted_value predicates.  */
809 (match truth_valued_p
810  @0
811  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
812 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
813  (match truth_valued_p
814   (op @0 @1)))
815 (match truth_valued_p
816   (truth_not @0))
818 (match (logical_inverted_value @0)
819  (truth_not @0))
820 (match (logical_inverted_value @0)
821  (bit_not truth_valued_p@0))
822 (match (logical_inverted_value @0)
823  (eq @0 integer_zerop))
824 (match (logical_inverted_value @0)
825  (ne truth_valued_p@0 integer_truep))
826 (match (logical_inverted_value @0)
827  (bit_xor truth_valued_p@0 integer_truep))
829 /* X & !X -> 0.  */
830 (simplify
831  (bit_and:c @0 (logical_inverted_value @0))
832  { build_zero_cst (type); })
833 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
834 (for op (bit_ior bit_xor)
835  (simplify
836   (op:c truth_valued_p@0 (logical_inverted_value @0))
837   { constant_boolean_node (true, type); }))
838 /* X ==/!= !X is false/true.  */
839 (for op (eq ne)
840  (simplify
841   (op:c truth_valued_p@0 (logical_inverted_value @0))
842   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
844 /* If arg1 and arg2 are booleans (or any single bit type)
845    then try to simplify:
847    (~X & Y) -> X < Y
848    (X & ~Y) -> Y < X
849    (~X | Y) -> X <= Y
850    (X | ~Y) -> Y <= X
852    But only do this if our result feeds into a comparison as
853    this transformation is not always a win, particularly on
854    targets with and-not instructions.
855    -> simplify_bitwise_binary_boolean */
856 (simplify
857   (ne (bit_and:c (bit_not @0) @1) integer_zerop)
858   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
859        && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
860    (lt @0 @1)))
861 (simplify
862   (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
863   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
864        && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
865    (le @0 @1)))
867 /* ~~x -> x */
868 (simplify
869   (bit_not (bit_not @0))
870   @0)
872 /* Convert ~ (-A) to A - 1.  */
873 (simplify
874  (bit_not (convert? (negate @0)))
875  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
876   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
878 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
879 (simplify
880  (bit_not (convert? (minus @0 integer_each_onep)))
881  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
882   (convert (negate @0))))
883 (simplify
884  (bit_not (convert? (plus @0 integer_all_onesp)))
885  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
886   (convert (negate @0))))
888 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
889 (simplify
890  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
891  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
892   (convert (bit_xor @0 (bit_not @1)))))
893 (simplify
894  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
895  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
896   (convert (bit_xor @0 @1))))
898 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
899 (simplify
900  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
901  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
903 /* Fold A - (A & B) into ~B & A.  */
904 (simplify
905  (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
906  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
907       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
908   (convert (bit_and (bit_not @1) @0))))
912 /* ((X inner_op C0) outer_op C1)
913    With X being a tree where value_range has reasoned certain bits to always be
914    zero throughout its computed value range,
915    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
916    where zero_mask has 1's for all bits that are sure to be 0 in
917    and 0's otherwise.
918    if (inner_op == '^') C0 &= ~C1;
919    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
920    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
922 (for inner_op (bit_ior bit_xor)
923      outer_op (bit_xor bit_ior)
924 (simplify
925  (outer_op
926   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
927  (with
928   {
929     bool fail = false;
930     wide_int zero_mask_not;
931     wide_int C0;
932     wide_int cst_emit;
934     if (TREE_CODE (@2) == SSA_NAME)
935       zero_mask_not = get_nonzero_bits (@2);
936     else
937       fail = true;
939     if (inner_op == BIT_XOR_EXPR)
940       {
941         C0 = wi::bit_and_not (@0, @1);
942         cst_emit = wi::bit_or (C0, @1);
943       }
944     else
945       {
946         C0 = @0;
947         cst_emit = wi::bit_xor (@0, @1);
948       }
949   }
950   (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
951    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
952    (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
953     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
955 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
956 (simplify
957   (pointer_plus (pointer_plus:s @0 @1) @3)
958   (pointer_plus @0 (plus @1 @3)))
960 /* Pattern match
961      tem1 = (long) ptr1;
962      tem2 = (long) ptr2;
963      tem3 = tem2 - tem1;
964      tem4 = (unsigned long) tem3;
965      tem5 = ptr1 + tem4;
966    and produce
967      tem5 = ptr2;  */
968 (simplify
969   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
970   /* Conditionally look through a sign-changing conversion.  */
971   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
972        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
973             || (GENERIC && type == TREE_TYPE (@1))))
974    @1))
976 /* Pattern match
977      tem = (sizetype) ptr;
978      tem = tem & algn;
979      tem = -tem;
980      ... = ptr p+ tem;
981    and produce the simpler and easier to analyze with respect to alignment
982      ... = ptr & ~algn;  */
983 (simplify
984   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
985   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
986    (bit_and @0 { algn; })))
988 /* Try folding difference of addresses.  */
989 (simplify
990  (minus (convert ADDR_EXPR@0) (convert @1))
991  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
992   (with { HOST_WIDE_INT diff; }
993    (if (ptr_difference_const (@0, @1, &diff))
994     { build_int_cst_type (type, diff); }))))
995 (simplify
996  (minus (convert @0) (convert ADDR_EXPR@1))
997  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
998   (with { HOST_WIDE_INT diff; }
999    (if (ptr_difference_const (@0, @1, &diff))
1000     { build_int_cst_type (type, diff); }))))
1002 /* If arg0 is derived from the address of an object or function, we may
1003    be able to fold this expression using the object or function's
1004    alignment.  */
1005 (simplify
1006  (bit_and (convert? @0) INTEGER_CST@1)
1007  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1008       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1009   (with
1010    {
1011      unsigned int align;
1012      unsigned HOST_WIDE_INT bitpos;
1013      get_pointer_alignment_1 (@0, &align, &bitpos);
1014    }
1015    (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1016     { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1019 /* We can't reassociate at all for saturating types.  */
1020 (if (!TYPE_SATURATING (type))
1022  /* Contract negates.  */
1023  /* A + (-B) -> A - B */
1024  (simplify
1025   (plus:c (convert1? @0) (convert2? (negate @1)))
1026   /* Apply STRIP_NOPS on @0 and the negate.  */
1027   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1028        && tree_nop_conversion_p (type, TREE_TYPE (@1))
1029        && !TYPE_OVERFLOW_SANITIZED (type))
1030    (minus (convert @0) (convert @1))))
1031  /* A - (-B) -> A + B */
1032  (simplify
1033   (minus (convert1? @0) (convert2? (negate @1)))
1034   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1035        && tree_nop_conversion_p (type, TREE_TYPE (@1))
1036        && !TYPE_OVERFLOW_SANITIZED (type))
1037    (plus (convert @0) (convert @1))))
1038  /* -(-A) -> A */
1039  (simplify
1040   (negate (convert? (negate @1)))
1041   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1042        && !TYPE_OVERFLOW_SANITIZED (type))
1043    (convert @1)))
1045  /* We can't reassociate floating-point unless -fassociative-math
1046     or fixed-point plus or minus because of saturation to +-Inf.  */
1047  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1048       && !FIXED_POINT_TYPE_P (type))
1050   /* Match patterns that allow contracting a plus-minus pair
1051      irrespective of overflow issues.  */
1052   /* (A +- B) - A       ->  +- B */
1053   /* (A +- B) -+ B      ->  A */
1054   /* A - (A +- B)       -> -+ B */
1055   /* A +- (B -+ A)      ->  +- B */
1056   (simplify
1057     (minus (plus:c @0 @1) @0)
1058     @1)
1059   (simplify
1060     (minus (minus @0 @1) @0)
1061     (negate @1))
1062   (simplify
1063     (plus:c (minus @0 @1) @1)
1064     @0)
1065   (simplify
1066    (minus @0 (plus:c @0 @1))
1067    (negate @1))
1068   (simplify
1069    (minus @0 (minus @0 @1))
1070    @1)
1072   /* (A +- CST) +- CST -> A + CST  */
1073   (for outer_op (plus minus)
1074    (for inner_op (plus minus)
1075     (simplify
1076      (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1077      /* If the constant operation overflows we cannot do the transform
1078         as we would introduce undefined overflow, for example
1079         with (a - 1) + INT_MIN.  */
1080      (with { tree cst = const_binop (outer_op == inner_op
1081                                      ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1082       (if (cst && !TREE_OVERFLOW (cst))
1083        (inner_op @0 { cst; } ))))))
1085   /* (CST - A) +- CST -> CST - A  */
1086   (for outer_op (plus minus)
1087    (simplify
1088     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1089     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1090      (if (cst && !TREE_OVERFLOW (cst))
1091       (minus { cst; } @0)))))
1093   /* ~A + A -> -1 */
1094   (simplify
1095    (plus:c (bit_not @0) @0)
1096    (if (!TYPE_OVERFLOW_TRAPS (type))
1097     { build_all_ones_cst (type); }))
1099   /* ~A + 1 -> -A */
1100   (simplify
1101    (plus (convert? (bit_not @0)) integer_each_onep)
1102    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1103     (negate (convert @0))))
1105   /* -A - 1 -> ~A */
1106   (simplify
1107    (minus (convert? (negate @0)) integer_each_onep)
1108    (if (!TYPE_OVERFLOW_TRAPS (type)
1109         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1110     (bit_not (convert @0))))
1112   /* -1 - A -> ~A */
1113   (simplify
1114    (minus integer_all_onesp @0)
1115    (bit_not @0))
1117   /* (T)(P + A) - (T)P -> (T) A */
1118   (for add (plus pointer_plus)
1119    (simplify
1120     (minus (convert (add @0 @1))
1121      (convert @0))
1122     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1123          /* For integer types, if A has a smaller type
1124             than T the result depends on the possible
1125             overflow in P + A.
1126             E.g. T=size_t, A=(unsigned)429497295, P>0.
1127             However, if an overflow in P + A would cause
1128             undefined behavior, we can assume that there
1129             is no overflow.  */
1130          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1131              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1132          /* For pointer types, if the conversion of A to the
1133             final type requires a sign- or zero-extension,
1134             then we have to punt - it is not defined which
1135             one is correct.  */
1136          || (POINTER_TYPE_P (TREE_TYPE (@0))
1137              && TREE_CODE (@1) == INTEGER_CST
1138              && tree_int_cst_sign_bit (@1) == 0))
1139      (convert @1))))
1141   /* (T)P - (T)(P + A) -> -(T) A */
1142   (for add (plus pointer_plus)
1143    (simplify
1144     (minus (convert @0)
1145      (convert (add @0 @1)))
1146     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1147          /* For integer types, if A has a smaller type
1148             than T the result depends on the possible
1149             overflow in P + A.
1150             E.g. T=size_t, A=(unsigned)429497295, P>0.
1151             However, if an overflow in P + A would cause
1152             undefined behavior, we can assume that there
1153             is no overflow.  */
1154          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1155              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1156          /* For pointer types, if the conversion of A to the
1157             final type requires a sign- or zero-extension,
1158             then we have to punt - it is not defined which
1159             one is correct.  */
1160          || (POINTER_TYPE_P (TREE_TYPE (@0))
1161              && TREE_CODE (@1) == INTEGER_CST
1162              && tree_int_cst_sign_bit (@1) == 0))
1163      (negate (convert @1)))))
1165   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1166   (for add (plus pointer_plus)
1167    (simplify
1168     (minus (convert (add @0 @1))
1169      (convert (add @0 @2)))
1170     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1171          /* For integer types, if A has a smaller type
1172             than T the result depends on the possible
1173             overflow in P + A.
1174             E.g. T=size_t, A=(unsigned)429497295, P>0.
1175             However, if an overflow in P + A would cause
1176             undefined behavior, we can assume that there
1177             is no overflow.  */
1178          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1179              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1180          /* For pointer types, if the conversion of A to the
1181             final type requires a sign- or zero-extension,
1182             then we have to punt - it is not defined which
1183             one is correct.  */
1184          || (POINTER_TYPE_P (TREE_TYPE (@0))
1185              && TREE_CODE (@1) == INTEGER_CST
1186              && tree_int_cst_sign_bit (@1) == 0
1187              && TREE_CODE (@2) == INTEGER_CST
1188              && tree_int_cst_sign_bit (@2) == 0))
1189      (minus (convert @1) (convert @2)))))))
1192 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
1194 (for minmax (min max FMIN FMAX)
1195  (simplify
1196   (minmax @0 @0)
1197   @0))
1198 /* min(max(x,y),y) -> y.  */
1199 (simplify
1200  (min:c (max:c @0 @1) @1)
1201  @1)
1202 /* max(min(x,y),y) -> y.  */
1203 (simplify
1204  (max:c (min:c @0 @1) @1)
1205  @1)
1206 (simplify
1207  (min @0 @1)
1208  (switch
1209   (if (INTEGRAL_TYPE_P (type)
1210        && TYPE_MIN_VALUE (type)
1211        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1212    @1)
1213   (if (INTEGRAL_TYPE_P (type)
1214        && TYPE_MAX_VALUE (type)
1215        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1216    @0)))
1217 (simplify
1218  (max @0 @1)
1219  (switch
1220   (if (INTEGRAL_TYPE_P (type)
1221        && TYPE_MAX_VALUE (type)
1222        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1223    @1)
1224   (if (INTEGRAL_TYPE_P (type)
1225        && TYPE_MIN_VALUE (type)
1226        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1227    @0)))
1228 (for minmax (FMIN FMAX)
1229  /* If either argument is NaN, return the other one.  Avoid the
1230     transformation if we get (and honor) a signalling NaN.  */
1231  (simplify
1232   (minmax:c @0 REAL_CST@1)
1233   (if (real_isnan (TREE_REAL_CST_PTR (@1))
1234        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1235    @0)))
1236 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
1237    functions to return the numeric arg if the other one is NaN.
1238    MIN and MAX don't honor that, so only transform if -ffinite-math-only
1239    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
1240    worry about it either.  */
1241 (if (flag_finite_math_only)
1242  (simplify
1243   (FMIN @0 @1)
1244   (min @0 @1))
1245  (simplify
1246   (FMAX @0 @1)
1247   (max @0 @1)))
1248 /* min (-A, -B) -> -max (A, B)  */
1249 (for minmax (min max FMIN FMAX)
1250      maxmin (max min FMAX FMIN)
1251  (simplify
1252   (minmax (negate:s@2 @0) (negate:s@3 @1))
1253   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1254        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1255            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1256    (negate (maxmin @0 @1)))))
1257 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1258    MAX (~X, ~Y) -> ~MIN (X, Y)  */
1259 (for minmax (min max)
1260  maxmin (max min)
1261  (simplify
1262   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1263   (bit_not (maxmin @0 @1))))
1265 /* Simplifications of shift and rotates.  */
1267 (for rotate (lrotate rrotate)
1268  (simplify
1269   (rotate integer_all_onesp@0 @1)
1270   @0))
1272 /* Optimize -1 >> x for arithmetic right shifts.  */
1273 (simplify
1274  (rshift integer_all_onesp@0 @1)
1275  (if (!TYPE_UNSIGNED (type)
1276       && tree_expr_nonnegative_p (@1))
1277   @0))
1279 /* Optimize (x >> c) << c into x & (-1<<c).  */
1280 (simplify
1281  (lshift (rshift @0 INTEGER_CST@1) @1)
1282  (if (wi::ltu_p (@1, element_precision (type)))
1283   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1285 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1286    types.  */
1287 (simplify
1288  (rshift (lshift @0 INTEGER_CST@1) @1)
1289  (if (TYPE_UNSIGNED (type)
1290       && (wi::ltu_p (@1, element_precision (type))))
1291   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1293 (for shiftrotate (lrotate rrotate lshift rshift)
1294  (simplify
1295   (shiftrotate @0 integer_zerop)
1296   (non_lvalue @0))
1297  (simplify
1298   (shiftrotate integer_zerop@0 @1)
1299   @0)
1300  /* Prefer vector1 << scalar to vector1 << vector2
1301     if vector2 is uniform.  */
1302  (for vec (VECTOR_CST CONSTRUCTOR)
1303   (simplify
1304    (shiftrotate @0 vec@1)
1305    (with { tree tem = uniform_vector_p (@1); }
1306     (if (tem)
1307      (shiftrotate @0 { tem; }))))))
1309 /* Rewrite an LROTATE_EXPR by a constant into an
1310    RROTATE_EXPR by a new constant.  */
1311 (simplify
1312  (lrotate @0 INTEGER_CST@1)
1313  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1314                             build_int_cst (TREE_TYPE (@1),
1315                                            element_precision (type)), @1); }))
1317 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
1318 (for op (lrotate rrotate rshift lshift)
1319  (simplify
1320   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1321   (with { unsigned int prec = element_precision (type); }
1322    (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1323         && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1324         && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1325         && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1326     (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1327      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1328         being well defined.  */
1329      (if (low >= prec)
1330       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1331        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1332        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1333         { build_zero_cst (type); }
1334         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1335       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1338 /* ((1 << A) & 1) != 0 -> A == 0
1339    ((1 << A) & 1) == 0 -> A != 0 */
1340 (for cmp (ne eq)
1341      icmp (eq ne)
1342  (simplify
1343   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1344   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1346 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1347    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1348    if CST2 != 0.  */
1349 (for cmp (ne eq)
1350  (simplify
1351   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1352   (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1353    (if (cand < 0
1354         || (!integer_zerop (@2)
1355             && wi::ne_p (wi::lshift (@0, cand), @2)))
1356     { constant_boolean_node (cmp == NE_EXPR, type); }
1357     (if (!integer_zerop (@2)
1358          && wi::eq_p (wi::lshift (@0, cand), @2))
1359      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1361 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1362         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1363    if the new mask might be further optimized.  */
1364 (for shift (lshift rshift)
1365  (simplify
1366   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1367            INTEGER_CST@2)
1368    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1369         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1370         && tree_fits_uhwi_p (@1)
1371         && tree_to_uhwi (@1) > 0
1372         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1373     (with
1374      {
1375        unsigned int shiftc = tree_to_uhwi (@1);
1376        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1377        unsigned HOST_WIDE_INT newmask, zerobits = 0;
1378        tree shift_type = TREE_TYPE (@3);
1379        unsigned int prec;
1381        if (shift == LSHIFT_EXPR)
1382          zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1383        else if (shift == RSHIFT_EXPR
1384                 && (TYPE_PRECISION (shift_type)
1385                     == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1386          {
1387            prec = TYPE_PRECISION (TREE_TYPE (@3));
1388            tree arg00 = @0;
1389            /* See if more bits can be proven as zero because of
1390               zero extension.  */
1391            if (@3 != @0
1392                && TYPE_UNSIGNED (TREE_TYPE (@0)))
1393              {
1394                tree inner_type = TREE_TYPE (@0);
1395                if ((TYPE_PRECISION (inner_type)
1396                     == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1397                    && TYPE_PRECISION (inner_type) < prec)
1398                  {
1399                    prec = TYPE_PRECISION (inner_type);
1400                    /* See if we can shorten the right shift.  */
1401                    if (shiftc < prec)
1402                      shift_type = inner_type;
1403                    /* Otherwise X >> C1 is all zeros, so we'll optimize
1404                       it into (X, 0) later on by making sure zerobits
1405                       is all ones.  */
1406                  }
1407              }
1408            zerobits = ~(unsigned HOST_WIDE_INT) 0;
1409            if (shiftc < prec)
1410              {
1411                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1412                zerobits <<= prec - shiftc;
1413              }
1414            /* For arithmetic shift if sign bit could be set, zerobits
1415               can contain actually sign bits, so no transformation is
1416               possible, unless MASK masks them all away.  In that
1417               case the shift needs to be converted into logical shift.  */
1418            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1419                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1420              {
1421                if ((mask & zerobits) == 0)
1422                  shift_type = unsigned_type_for (TREE_TYPE (@3));
1423                else
1424                  zerobits = 0;
1425              }
1426          }
1427      }
1428      /* ((X << 16) & 0xff00) is (X, 0).  */
1429      (if ((mask & zerobits) == mask)
1430       { build_int_cst (type, 0); }
1431       (with { newmask = mask | zerobits; }
1432        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1433         (with
1434          {
1435            /* Only do the transformation if NEWMASK is some integer
1436               mode's mask.  */
1437            for (prec = BITS_PER_UNIT;
1438                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1439              if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1440                break;
1441          }
1442          (if (prec < HOST_BITS_PER_WIDE_INT
1443               || newmask == ~(unsigned HOST_WIDE_INT) 0)
1444           (with
1445            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1446            (if (!tree_int_cst_equal (newmaskt, @2))
1447             (if (shift_type != TREE_TYPE (@3))
1448              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1449              (bit_and @4 { newmaskt; })))))))))))))
1451 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1452    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
1453 (for shift (lshift rshift)
1454  (for bit_op (bit_and bit_xor bit_ior)
1455   (simplify
1456    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1457    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1458     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1459      (bit_op (shift (convert @0) @1) { mask; }))))))
1462 /* Simplifications of conversions.  */
1464 /* Basic strip-useless-type-conversions / strip_nops.  */
1465 (for cvt (convert view_convert float fix_trunc)
1466  (simplify
1467   (cvt @0)
1468   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1469        || (GENERIC && type == TREE_TYPE (@0)))
1470    @0)))
1472 /* Contract view-conversions.  */
1473 (simplify
1474   (view_convert (view_convert @0))
1475   (view_convert @0))
1477 /* For integral conversions with the same precision or pointer
1478    conversions use a NOP_EXPR instead.  */
1479 (simplify
1480   (view_convert @0)
1481   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1482        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1483        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1484    (convert @0)))
1486 /* Strip inner integral conversions that do not change precision or size.  */
1487 (simplify
1488   (view_convert (convert@0 @1))
1489   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1490        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1491        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1492        && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1493    (view_convert @1)))
1495 /* Re-association barriers around constants and other re-association
1496    barriers can be removed.  */
1497 (simplify
1498  (paren CONSTANT_CLASS_P@0)
1499  @0)
1500 (simplify
1501  (paren (paren@1 @0))
1502  @1)
1504 /* Handle cases of two conversions in a row.  */
1505 (for ocvt (convert float fix_trunc)
1506  (for icvt (convert float)
1507   (simplify
1508    (ocvt (icvt@1 @0))
1509    (with
1510     {
1511       tree inside_type = TREE_TYPE (@0);
1512       tree inter_type = TREE_TYPE (@1);
1513       int inside_int = INTEGRAL_TYPE_P (inside_type);
1514       int inside_ptr = POINTER_TYPE_P (inside_type);
1515       int inside_float = FLOAT_TYPE_P (inside_type);
1516       int inside_vec = VECTOR_TYPE_P (inside_type);
1517       unsigned int inside_prec = TYPE_PRECISION (inside_type);
1518       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1519       int inter_int = INTEGRAL_TYPE_P (inter_type);
1520       int inter_ptr = POINTER_TYPE_P (inter_type);
1521       int inter_float = FLOAT_TYPE_P (inter_type);
1522       int inter_vec = VECTOR_TYPE_P (inter_type);
1523       unsigned int inter_prec = TYPE_PRECISION (inter_type);
1524       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1525       int final_int = INTEGRAL_TYPE_P (type);
1526       int final_ptr = POINTER_TYPE_P (type);
1527       int final_float = FLOAT_TYPE_P (type);
1528       int final_vec = VECTOR_TYPE_P (type);
1529       unsigned int final_prec = TYPE_PRECISION (type);
1530       int final_unsignedp = TYPE_UNSIGNED (type);
1531     }
1532    (switch
1533     /* In addition to the cases of two conversions in a row
1534        handled below, if we are converting something to its own
1535        type via an object of identical or wider precision, neither
1536        conversion is needed.  */
1537     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1538           || (GENERIC
1539               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1540          && (((inter_int || inter_ptr) && final_int)
1541              || (inter_float && final_float))
1542          && inter_prec >= final_prec)
1543      (ocvt @0))
1545     /* Likewise, if the intermediate and initial types are either both
1546        float or both integer, we don't need the middle conversion if the
1547        former is wider than the latter and doesn't change the signedness
1548        (for integers).  Avoid this if the final type is a pointer since
1549        then we sometimes need the middle conversion.  Likewise if the
1550        final type has a precision not equal to the size of its mode.  */
1551     (if (((inter_int && inside_int) || (inter_float && inside_float))
1552          && (final_int || final_float)
1553          && inter_prec >= inside_prec
1554          && (inter_float || inter_unsignedp == inside_unsignedp)
1555          && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1556                && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1557      (ocvt @0))
1559     /* If we have a sign-extension of a zero-extended value, we can
1560        replace that by a single zero-extension.  Likewise if the
1561        final conversion does not change precision we can drop the
1562        intermediate conversion.  */
1563     (if (inside_int && inter_int && final_int
1564          && ((inside_prec < inter_prec && inter_prec < final_prec
1565               && inside_unsignedp && !inter_unsignedp)
1566              || final_prec == inter_prec))
1567      (ocvt @0))
1569     /* Two conversions in a row are not needed unless:
1570         - some conversion is floating-point (overstrict for now), or
1571         - some conversion is a vector (overstrict for now), or
1572         - the intermediate type is narrower than both initial and
1573           final, or
1574         - the intermediate type and innermost type differ in signedness,
1575           and the outermost type is wider than the intermediate, or
1576         - the initial type is a pointer type and the precisions of the
1577           intermediate and final types differ, or
1578         - the final type is a pointer type and the precisions of the
1579           initial and intermediate types differ.  */
1580     (if (! inside_float && ! inter_float && ! final_float
1581          && ! inside_vec && ! inter_vec && ! final_vec
1582          && (inter_prec >= inside_prec || inter_prec >= final_prec)
1583          && ! (inside_int && inter_int
1584                && inter_unsignedp != inside_unsignedp
1585                && inter_prec < final_prec)
1586          && ((inter_unsignedp && inter_prec > inside_prec)
1587              == (final_unsignedp && final_prec > inter_prec))
1588          && ! (inside_ptr && inter_prec != final_prec)
1589          && ! (final_ptr && inside_prec != inter_prec)
1590          && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1591                && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1592      (ocvt @0))
1594     /* A truncation to an unsigned type (a zero-extension) should be
1595        canonicalized as bitwise and of a mask.  */
1596     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
1597          && final_int && inter_int && inside_int
1598          && final_prec == inside_prec
1599          && final_prec > inter_prec
1600          && inter_unsignedp)
1601      (convert (bit_and @0 { wide_int_to_tree
1602                               (inside_type,
1603                                wi::mask (inter_prec, false,
1604                                          TYPE_PRECISION (inside_type))); })))
1606     /* If we are converting an integer to a floating-point that can
1607        represent it exactly and back to an integer, we can skip the
1608        floating-point conversion.  */
1609     (if (GIMPLE /* PR66211 */
1610          && inside_int && inter_float && final_int &&
1611          (unsigned) significand_size (TYPE_MODE (inter_type))
1612          >= inside_prec - !inside_unsignedp)
1613      (convert @0)))))))
1615 /* If we have a narrowing conversion to an integral type that is fed by a
1616    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1617    masks off bits outside the final type (and nothing else).  */
1618 (simplify
1619   (convert (bit_and @0 INTEGER_CST@1))
1620   (if (INTEGRAL_TYPE_P (type)
1621        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1622        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1623        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1624                                                     TYPE_PRECISION (type)), 0))
1625    (convert @0)))
1628 /* (X /[ex] A) * A -> X.  */
1629 (simplify
1630   (mult (convert? (exact_div @0 @1)) @1)
1631   /* Look through a sign-changing conversion.  */
1632   (convert @0))
1634 /* Canonicalization of binary operations.  */
1636 /* Convert X + -C into X - C.  */
1637 (simplify
1638  (plus @0 REAL_CST@1)
1639  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1640   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1641    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1642     (minus @0 { tem; })))))
1644 /* Convert x+x into x*2.  */
1645 (simplify
1646  (plus @0 @0)
1647  (if (SCALAR_FLOAT_TYPE_P (type))
1648   (mult @0 { build_real (type, dconst2); })
1649   (if (INTEGRAL_TYPE_P (type))
1650    (mult @0 { build_int_cst (type, 2); }))))
1652 (simplify
1653  (minus integer_zerop @1)
1654  (negate @1))
1656 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
1657    ARG0 is zero and X + ARG0 reduces to X, since that would mean
1658    (-ARG1 + ARG0) reduces to -ARG1.  */
1659 (simplify
1660  (minus real_zerop@0 @1)
1661  (if (fold_real_zero_addition_p (type, @0, 0))
1662   (negate @1)))
1664 /* Transform x * -1 into -x.  */
1665 (simplify
1666  (mult @0 integer_minus_onep)
1667  (negate @0))
1669 /* True if we can easily extract the real and imaginary parts of a complex
1670    number.  */
1671 (match compositional_complex
1672  (convert? (complex @0 @1)))
1674 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
1675 (simplify
1676  (complex (realpart @0) (imagpart @0))
1677  @0)
1678 (simplify
1679  (realpart (complex @0 @1))
1680  @0)
1681 (simplify
1682  (imagpart (complex @0 @1))
1683  @1)
1685 /* Sometimes we only care about half of a complex expression.  */
1686 (simplify
1687  (realpart (convert?:s (conj:s @0)))
1688  (convert (realpart @0)))
1689 (simplify
1690  (imagpart (convert?:s (conj:s @0)))
1691  (convert (negate (imagpart @0))))
1692 (for part (realpart imagpart)
1693  (for op (plus minus)
1694   (simplify
1695    (part (convert?:s@2 (op:s @0 @1)))
1696    (convert (op (part @0) (part @1))))))
1697 (simplify
1698  (realpart (convert?:s (CEXPI:s @0)))
1699  (convert (COS @0)))
1700 (simplify
1701  (imagpart (convert?:s (CEXPI:s @0)))
1702  (convert (SIN @0)))
1704 /* conj(conj(x)) -> x  */
1705 (simplify
1706  (conj (convert? (conj @0)))
1707  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1708   (convert @0)))
1710 /* conj({x,y}) -> {x,-y}  */
1711 (simplify
1712  (conj (convert?:s (complex:s @0 @1)))
1713  (with { tree itype = TREE_TYPE (type); }
1714   (complex (convert:itype @0) (negate (convert:itype @1)))))
1716 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
1717 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1718  (simplify
1719   (bswap (bswap @0))
1720   @0)
1721  (simplify
1722   (bswap (bit_not (bswap @0)))
1723   (bit_not @0))
1724  (for bitop (bit_xor bit_ior bit_and)
1725   (simplify
1726    (bswap (bitop:c (bswap @0) @1))
1727    (bitop @0 (bswap @1)))))
1730 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
1732 /* Simplify constant conditions.
1733    Only optimize constant conditions when the selected branch
1734    has the same type as the COND_EXPR.  This avoids optimizing
1735    away "c ? x : throw", where the throw has a void type.
1736    Note that we cannot throw away the fold-const.c variant nor
1737    this one as we depend on doing this transform before possibly
1738    A ? B : B -> B triggers and the fold-const.c one can optimize
1739    0 ? A : B to B even if A has side-effects.  Something
1740    genmatch cannot handle.  */
1741 (simplify
1742  (cond INTEGER_CST@0 @1 @2)
1743  (if (integer_zerop (@0))
1744   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1745    @2)
1746   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1747    @1)))
1748 (simplify
1749  (vec_cond VECTOR_CST@0 @1 @2)
1750  (if (integer_all_onesp (@0))
1751   @1
1752   (if (integer_zerop (@0))
1753    @2)))
1755 (for cnd (cond vec_cond)
1756  /* A ? B : (A ? X : C) -> A ? B : C.  */
1757  (simplify
1758   (cnd @0 (cnd @0 @1 @2) @3)
1759   (cnd @0 @1 @3))
1760  (simplify
1761   (cnd @0 @1 (cnd @0 @2 @3))
1762   (cnd @0 @1 @3))
1763  /* A ? B : (!A ? C : X) -> A ? B : C.  */
1764  /* ???  This matches embedded conditions open-coded because genmatch
1765     would generate matching code for conditions in separate stmts only.
1766     The following is still important to merge then and else arm cases
1767     from if-conversion.  */
1768  (simplify
1769   (cnd @0 @1 (cnd @2 @3 @4))
1770   (if (COMPARISON_CLASS_P (@0)
1771        && COMPARISON_CLASS_P (@2)
1772        && invert_tree_comparison
1773            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1774        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1775        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1776    (cnd @0 @1 @3)))
1777  (simplify
1778   (cnd @0 (cnd @1 @2 @3) @4)
1779   (if (COMPARISON_CLASS_P (@0)
1780        && COMPARISON_CLASS_P (@1)
1781        && invert_tree_comparison
1782            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1783        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1784        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1785    (cnd @0 @3 @4)))
1787  /* A ? B : B -> B.  */
1788  (simplify
1789   (cnd @0 @1 @1)
1790   @1)
1792  /* !A ? B : C -> A ? C : B.  */
1793  (simplify
1794   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1795   (cnd @0 @2 @1)))
1797 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1798    return all -1 or all 0 results.  */
1799 /* ??? We could instead convert all instances of the vec_cond to negate,
1800    but that isn't necessarily a win on its own.  */
1801 (simplify
1802  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1803  (if (VECTOR_TYPE_P (type)
1804       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1805       && (TYPE_MODE (TREE_TYPE (type))
1806           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1807   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1809 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
1810 (simplify
1811  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1812  (if (VECTOR_TYPE_P (type)
1813       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1814       && (TYPE_MODE (TREE_TYPE (type))
1815           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1816   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1819 /* Simplifications of comparisons.  */
1821 /* See if we can reduce the magnitude of a constant involved in a
1822    comparison by changing the comparison code.  This is a canonicalization
1823    formerly done by maybe_canonicalize_comparison_1.  */
1824 (for cmp  (le gt)
1825      acmp (lt ge)
1826  (simplify
1827   (cmp @0 INTEGER_CST@1)
1828   (if (tree_int_cst_sgn (@1) == -1)
1829    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1830 (for cmp  (ge lt)
1831      acmp (gt le)
1832  (simplify
1833   (cmp @0 INTEGER_CST@1)
1834   (if (tree_int_cst_sgn (@1) == 1)
1835    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1838 /* We can simplify a logical negation of a comparison to the
1839    inverted comparison.  As we cannot compute an expression
1840    operator using invert_tree_comparison we have to simulate
1841    that with expression code iteration.  */
1842 (for cmp (tcc_comparison)
1843      icmp (inverted_tcc_comparison)
1844      ncmp (inverted_tcc_comparison_with_nans)
1845  /* Ideally we'd like to combine the following two patterns
1846     and handle some more cases by using
1847       (logical_inverted_value (cmp @0 @1))
1848     here but for that genmatch would need to "inline" that.
1849     For now implement what forward_propagate_comparison did.  */
1850  (simplify
1851   (bit_not (cmp @0 @1))
1852   (if (VECTOR_TYPE_P (type)
1853        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1854    /* Comparison inversion may be impossible for trapping math,
1855       invert_tree_comparison will tell us.  But we can't use
1856       a computed operator in the replacement tree thus we have
1857       to play the trick below.  */
1858    (with { enum tree_code ic = invert_tree_comparison
1859              (cmp, HONOR_NANS (@0)); }
1860     (if (ic == icmp)
1861      (icmp @0 @1)
1862      (if (ic == ncmp)
1863       (ncmp @0 @1))))))
1864  (simplify
1865   (bit_xor (cmp @0 @1) integer_truep)
1866   (with { enum tree_code ic = invert_tree_comparison
1867             (cmp, HONOR_NANS (@0)); }
1868    (if (ic == icmp)
1869     (icmp @0 @1)
1870     (if (ic == ncmp)
1871      (ncmp @0 @1))))))
1873 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1874    ??? The transformation is valid for the other operators if overflow
1875    is undefined for the type, but performing it here badly interacts
1876    with the transformation in fold_cond_expr_with_comparison which
1877    attempts to synthetize ABS_EXPR.  */
1878 (for cmp (eq ne)
1879  (simplify
1880   (cmp (minus@2 @0 @1) integer_zerop)
1881   (if (single_use (@2))
1882    (cmp @0 @1))))
1884 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1885    signed arithmetic case.  That form is created by the compiler
1886    often enough for folding it to be of value.  One example is in
1887    computing loop trip counts after Operator Strength Reduction.  */
1888 (for cmp (simple_comparison)
1889      scmp (swapped_simple_comparison)
1890  (simplify
1891   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1892   /* Handle unfolded multiplication by zero.  */
1893   (if (integer_zerop (@1))
1894    (cmp @1 @2)
1895    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1896         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1897         && single_use (@3))
1898     /* If @1 is negative we swap the sense of the comparison.  */
1899     (if (tree_int_cst_sgn (@1) < 0)
1900      (scmp @0 @2)
1901      (cmp @0 @2))))))
1903 /* Simplify comparison of something with itself.  For IEEE
1904    floating-point, we can only do some of these simplifications.  */
1905 (for cmp (eq ge le)
1906  (simplify
1907   (cmp @0 @0)
1908   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1909        || ! HONOR_NANS (@0))
1910    { constant_boolean_node (true, type); }
1911    (if (cmp != EQ_EXPR)
1912     (eq @0 @0)))))
1913 (for cmp (ne gt lt)
1914  (simplify
1915   (cmp @0 @0)
1916   (if (cmp != NE_EXPR
1917        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1918        || ! HONOR_NANS (@0))
1919    { constant_boolean_node (false, type); })))
1920 (for cmp (unle unge uneq)
1921  (simplify
1922   (cmp @0 @0)
1923   { constant_boolean_node (true, type); }))
1924 (simplify
1925  (ltgt @0 @0)
1926  (if (!flag_trapping_math)
1927   { constant_boolean_node (false, type); }))
1929 /* Fold ~X op ~Y as Y op X.  */
1930 (for cmp (simple_comparison)
1931  (simplify
1932   (cmp (bit_not@2 @0) (bit_not@3 @1))
1933   (if (single_use (@2) && single_use (@3))
1934    (cmp @1 @0))))
1936 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
1937 (for cmp (simple_comparison)
1938      scmp (swapped_simple_comparison)
1939  (simplify
1940   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
1941   (if (single_use (@2)
1942        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
1943    (scmp @0 (bit_not @1)))))
1945 (for cmp (simple_comparison)
1946  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
1947  (simplify
1948   (cmp (convert@2 @0) (convert? @1))
1949   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1950        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1951            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1952        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1953            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1954    (with
1955     {
1956       tree type1 = TREE_TYPE (@1);
1957       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1958         {
1959           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1960           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1961               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1962             type1 = float_type_node;
1963           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1964               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1965             type1 = double_type_node;
1966         }
1967       tree newtype
1968         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1969            ? TREE_TYPE (@0) : type1); 
1970     }
1971     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1972      (cmp (convert:newtype @0) (convert:newtype @1))))))
1974  (simplify
1975   (cmp @0 REAL_CST@1)
1976   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
1977   (switch
1978    /* a CMP (-0) -> a CMP 0  */
1979    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1980     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1981    /* x != NaN is always true, other ops are always false.  */
1982    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1983         && ! HONOR_SNANS (@1))
1984     { constant_boolean_node (cmp == NE_EXPR, type); })
1985    /* Fold comparisons against infinity.  */
1986    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1987         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1988     (with
1989      {
1990        REAL_VALUE_TYPE max;
1991        enum tree_code code = cmp;
1992        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1993        if (neg)
1994          code = swap_tree_comparison (code);
1995      }
1996      (switch
1997       /* x > +Inf is always false, if with ignore sNANs.  */
1998       (if (code == GT_EXPR
1999            && ! HONOR_SNANS (@0))
2000        { constant_boolean_node (false, type); })
2001       (if (code == LE_EXPR)
2002        /* x <= +Inf is always true, if we don't case about NaNs.  */
2003        (if (! HONOR_NANS (@0))
2004         { constant_boolean_node (true, type); }
2005         /* x <= +Inf is the same as x == x, i.e. !isnan(x).  */
2006         (eq @0 @0)))
2007       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
2008       (if (code == EQ_EXPR || code == GE_EXPR)
2009        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2010         (if (neg)
2011          (lt @0 { build_real (TREE_TYPE (@0), max); })
2012          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2013       /* x < +Inf is always equal to x <= DBL_MAX.  */
2014       (if (code == LT_EXPR)
2015        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2016         (if (neg)
2017          (ge @0 { build_real (TREE_TYPE (@0), max); })
2018          (le @0 { build_real (TREE_TYPE (@0), max); }))))
2019       /* x != +Inf is always equal to !(x > DBL_MAX).  */
2020       (if (code == NE_EXPR)
2021        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2022         (if (! HONOR_NANS (@0))
2023          (if (neg)
2024           (ge @0 { build_real (TREE_TYPE (@0), max); })
2025           (le @0 { build_real (TREE_TYPE (@0), max); }))
2026          (if (neg)
2027           (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2028            { build_one_cst (type); })
2029           (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2030            { build_one_cst (type); }))))))))))
2032  /* If this is a comparison of a real constant with a PLUS_EXPR
2033     or a MINUS_EXPR of a real constant, we can convert it into a
2034     comparison with a revised real constant as long as no overflow
2035     occurs when unsafe_math_optimizations are enabled.  */
2036  (if (flag_unsafe_math_optimizations)
2037   (for op (plus minus)
2038    (simplify
2039     (cmp (op @0 REAL_CST@1) REAL_CST@2)
2040     (with
2041      {
2042        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2043                                TREE_TYPE (@1), @2, @1);
2044      }
2045      (if (tem && !TREE_OVERFLOW (tem))
2046       (cmp @0 { tem; }))))))
2048  /* Likewise, we can simplify a comparison of a real constant with
2049     a MINUS_EXPR whose first operand is also a real constant, i.e.
2050     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
2051     floating-point types only if -fassociative-math is set.  */
2052  (if (flag_associative_math)
2053   (simplify
2054    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2055    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2056     (if (tem && !TREE_OVERFLOW (tem))
2057      (cmp { tem; } @1)))))
2059  /* Fold comparisons against built-in math functions.  */
2060  (if (flag_unsafe_math_optimizations
2061       && ! flag_errno_math)
2062   (for sq (SQRT)
2063    (simplify
2064     (cmp (sq @0) REAL_CST@1)
2065     (switch
2066      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2067       (switch
2068        /* sqrt(x) < y is always false, if y is negative.  */
2069        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2070         { constant_boolean_node (false, type); })
2071        /* sqrt(x) > y is always true, if y is negative and we
2072           don't care about NaNs, i.e. negative values of x.  */
2073        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2074         { constant_boolean_node (true, type); })
2075        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
2076        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2077      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2078       (switch
2079        /* sqrt(x) < 0 is always false.  */
2080        (if (cmp == LT_EXPR)
2081         { constant_boolean_node (false, type); })
2082        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
2083        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2084         { constant_boolean_node (true, type); })
2085        /* sqrt(x) <= 0 -> x == 0.  */
2086        (if (cmp == LE_EXPR)
2087         (eq @0 @1))
2088        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
2089           == or !=.  In the last case:
2091             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2093           if x is negative or NaN.  Due to -funsafe-math-optimizations,
2094           the results for other x follow from natural arithmetic.  */
2095        (cmp @0 @1)))
2096      (if (cmp == GT_EXPR || cmp == GE_EXPR)
2097       (with
2098        {
2099          REAL_VALUE_TYPE c2;
2100          real_arithmetic (&c2, MULT_EXPR,
2101                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2102          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2103        }
2104        (if (REAL_VALUE_ISINF (c2))
2105         /* sqrt(x) > y is x == +Inf, when y is very large.  */
2106         (if (HONOR_INFINITIES (@0))
2107          (eq @0 { build_real (TREE_TYPE (@0), c2); })
2108          { constant_boolean_node (false, type); })
2109         /* sqrt(x) > c is the same as x > c*c.  */
2110         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2111      (if (cmp == LT_EXPR || cmp == LE_EXPR)
2112       (with
2113        {
2114          REAL_VALUE_TYPE c2;
2115          real_arithmetic (&c2, MULT_EXPR,
2116                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2117          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2118        }
2119        (if (REAL_VALUE_ISINF (c2))
2120         (switch
2121          /* sqrt(x) < y is always true, when y is a very large
2122             value and we don't care about NaNs or Infinities.  */
2123          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2124           { constant_boolean_node (true, type); })
2125          /* sqrt(x) < y is x != +Inf when y is very large and we
2126             don't care about NaNs.  */
2127          (if (! HONOR_NANS (@0))
2128           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2129          /* sqrt(x) < y is x >= 0 when y is very large and we
2130             don't care about Infinities.  */
2131          (if (! HONOR_INFINITIES (@0))
2132           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2133          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
2134          (if (GENERIC)
2135           (truth_andif
2136            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2137            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2138         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
2139         (if (! HONOR_NANS (@0))
2140          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2141          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
2142          (if (GENERIC)
2143           (truth_andif
2144            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2145            (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2147 /* Unordered tests if either argument is a NaN.  */
2148 (simplify
2149  (bit_ior (unordered @0 @0) (unordered @1 @1))
2150  (if (types_match (@0, @1))
2151   (unordered @0 @1)))
2152 (simplify
2153  (bit_and (ordered @0 @0) (ordered @1 @1))
2154  (if (types_match (@0, @1))
2155   (ordered @0 @1)))
2156 (simplify
2157  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2158  @2)
2159 (simplify
2160  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2161  @2)
2163 /* Simple range test simplifications.  */
2164 /* A < B || A >= B -> true.  */
2165 (for test1 (lt le le le ne ge)
2166      test2 (ge gt ge ne eq ne)
2167  (simplify
2168   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2169   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2170        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2171    { constant_boolean_node (true, type); })))
2172 /* A < B && A >= B -> false.  */
2173 (for test1 (lt lt lt le ne eq)
2174      test2 (ge gt eq gt eq gt)
2175  (simplify
2176   (bit_and:c (test1 @0 @1) (test2 @0 @1))
2177   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2178        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2179    { constant_boolean_node (false, type); })))
2181 /* -A CMP -B -> B CMP A.  */
2182 (for cmp (tcc_comparison)
2183      scmp (swapped_tcc_comparison)
2184  (simplify
2185   (cmp (negate @0) (negate @1))
2186   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2187        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2188            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2189    (scmp @0 @1)))
2190  (simplify
2191   (cmp (negate @0) CONSTANT_CLASS_P@1)
2192   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2193        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2194            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2195    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2196     (if (tem && !TREE_OVERFLOW (tem))
2197      (scmp @0 { tem; }))))))
2199 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
2200 (for op (eq ne)
2201  (simplify
2202   (op (abs @0) zerop@1)
2203   (op @0 @1)))
2205 /* From fold_sign_changed_comparison and fold_widened_comparison.  */
2206 (for cmp (simple_comparison)
2207  (simplify
2208   (cmp (convert@0 @00) (convert?@1 @10))
2209   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2210        /* Disable this optimization if we're casting a function pointer
2211           type on targets that require function pointer canonicalization.  */
2212        && !(targetm.have_canonicalize_funcptr_for_compare ()
2213             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2214             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2215        && single_use (@0))
2216    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2217         && (TREE_CODE (@10) == INTEGER_CST
2218             || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2219         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2220             || cmp == NE_EXPR
2221             || cmp == EQ_EXPR)
2222         && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2223     /* ???  The special-casing of INTEGER_CST conversion was in the original
2224        code and here to avoid a spurious overflow flag on the resulting
2225        constant which fold_convert produces.  */
2226     (if (TREE_CODE (@1) == INTEGER_CST)
2227      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2228                                 TREE_OVERFLOW (@1)); })
2229      (cmp @00 (convert @1)))
2231     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2232      /* If possible, express the comparison in the shorter mode.  */
2233      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2234            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2235           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2236               || ((TYPE_PRECISION (TREE_TYPE (@00))
2237                    >= TYPE_PRECISION (TREE_TYPE (@10)))
2238                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
2239                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
2240               || (TREE_CODE (@10) == INTEGER_CST
2241                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2242                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
2243       (cmp @00 (convert @10))
2244       (if (TREE_CODE (@10) == INTEGER_CST
2245            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2246            && !int_fits_type_p (@10, TREE_TYPE (@00)))
2247        (with
2248         {
2249           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2250           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2251           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2252           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2253         }
2254         (if (above || below)
2255          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2256           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2257           (if (cmp == LT_EXPR || cmp == LE_EXPR)
2258            { constant_boolean_node (above ? true : false, type); }
2259            (if (cmp == GT_EXPR || cmp == GE_EXPR)
2260             { constant_boolean_node (above ? false : true, type); }))))))))))))
2262 (for cmp (eq ne)
2263  /* A local variable can never be pointed to by
2264     the default SSA name of an incoming parameter.
2265     SSA names are canonicalized to 2nd place.  */
2266  (simplify
2267   (cmp addr@0 SSA_NAME@1)
2268   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2269        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2270    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2271     (if (TREE_CODE (base) == VAR_DECL
2272          && auto_var_in_fn_p (base, current_function_decl))
2273      (if (cmp == NE_EXPR)
2274       { constant_boolean_node (true, type); }
2275       { constant_boolean_node (false, type); }))))))
2277 /* Equality compare simplifications from fold_binary  */
2278 (for cmp (eq ne)
2280  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2281     Similarly for NE_EXPR.  */
2282  (simplify
2283   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2284   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2285        && wi::bit_and_not (@1, @2) != 0)
2286    { constant_boolean_node (cmp == NE_EXPR, type); }))
2288  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
2289  (simplify
2290   (cmp (bit_xor @0 @1) integer_zerop)
2291   (cmp @0 @1))
2293  /* (X ^ Y) == Y becomes X == 0.
2294     Likewise (X ^ Y) == X becomes Y == 0.  */
2295  (simplify
2296   (cmp:c (bit_xor:c @0 @1) @0)
2297   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2299  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
2300  (simplify
2301   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2302   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2303    (cmp @0 (bit_xor @1 (convert @2)))))
2305  (simplify
2306   (cmp (convert? addr@0) integer_zerop)
2307   (if (tree_single_nonzero_warnv_p (@0, NULL))
2308    { constant_boolean_node (cmp == NE_EXPR, type); })))
2310 /* If we have (A & C) == C where C is a power of 2, convert this into
2311    (A & C) != 0.  Similarly for NE_EXPR.  */
2312 (for cmp (eq ne)
2313      icmp (ne eq)
2314  (simplify
2315   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2316   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2318 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2319    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
2320 (for cmp (eq ne)
2321      ncmp (ge lt)
2322  (simplify
2323   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2324   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2325        && (TYPE_PRECISION (TREE_TYPE (@0))
2326            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2327        && element_precision (@2) >= element_precision (@0)
2328        && wi::only_sign_bit_p (@1, element_precision (@0)))
2329    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2330     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2332 /* When the addresses are not directly of decls compare base and offset.
2333    This implements some remaining parts of fold_comparison address
2334    comparisons but still no complete part of it.  Still it is good
2335    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
2336 (for cmp (simple_comparison)
2337  (simplify
2338   (cmp (convert1?@2 addr@0) (convert2? addr@1))
2339   (with
2340    {
2341      HOST_WIDE_INT off0, off1;
2342      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2343      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2344      if (base0 && TREE_CODE (base0) == MEM_REF)
2345        {
2346          off0 += mem_ref_offset (base0).to_short_addr ();
2347          base0 = TREE_OPERAND (base0, 0);
2348        }
2349      if (base1 && TREE_CODE (base1) == MEM_REF)
2350        {
2351          off1 += mem_ref_offset (base1).to_short_addr ();
2352          base1 = TREE_OPERAND (base1, 0);
2353        }
2354    }
2355    (if (base0 && base1)
2356     (with
2357      {
2358        int equal = 2;
2359        if (decl_in_symtab_p (base0)
2360            && decl_in_symtab_p (base1))
2361          equal = symtab_node::get_create (base0)
2362                    ->equal_address_to (symtab_node::get_create (base1));
2363        else if ((DECL_P (base0)
2364                  || TREE_CODE (base0) == SSA_NAME
2365                  || TREE_CODE (base0) == STRING_CST)
2366                 && (DECL_P (base1)
2367                     || TREE_CODE (base1) == SSA_NAME
2368                     || TREE_CODE (base1) == STRING_CST))
2369          equal = (base0 == base1);
2370      }
2371      (if (equal == 1
2372           && (cmp == EQ_EXPR || cmp == NE_EXPR
2373               /* If the offsets are equal we can ignore overflow.  */
2374               || off0 == off1
2375               || POINTER_TYPE_OVERFLOW_UNDEFINED
2376               /* Or if we compare using pointers to decls or strings.  */
2377               || (POINTER_TYPE_P (TREE_TYPE (@2))
2378                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2379       (switch
2380        (if (cmp == EQ_EXPR)
2381         { constant_boolean_node (off0 == off1, type); })
2382        (if (cmp == NE_EXPR)
2383         { constant_boolean_node (off0 != off1, type); })
2384        (if (cmp == LT_EXPR)
2385         { constant_boolean_node (off0 < off1, type); })
2386        (if (cmp == LE_EXPR)
2387         { constant_boolean_node (off0 <= off1, type); })
2388        (if (cmp == GE_EXPR)
2389         { constant_boolean_node (off0 >= off1, type); })
2390        (if (cmp == GT_EXPR)
2391         { constant_boolean_node (off0 > off1, type); }))
2392       (if (equal == 0
2393            && DECL_P (base0) && DECL_P (base1)
2394            /* If we compare this as integers require equal offset.  */
2395            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2396                || off0 == off1))
2397        (switch
2398         (if (cmp == EQ_EXPR)
2399          { constant_boolean_node (false, type); })
2400         (if (cmp == NE_EXPR)
2401          { constant_boolean_node (true, type); })))))))))
2403 /* Non-equality compare simplifications from fold_binary  */
2404 (for cmp (lt gt le ge)
2405  /* Comparisons with the highest or lowest possible integer of
2406     the specified precision will have known values.  */
2407  (simplify
2408   (cmp (convert?@2 @0) INTEGER_CST@1)
2409   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2410        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2411    (with
2412     {
2413       tree arg1_type = TREE_TYPE (@1);
2414       unsigned int prec = TYPE_PRECISION (arg1_type);
2415       wide_int max = wi::max_value (arg1_type);
2416       wide_int signed_max = wi::max_value (prec, SIGNED);
2417       wide_int min = wi::min_value (arg1_type);
2418     }
2419     (switch
2420      (if (wi::eq_p (@1, max))
2421       (switch
2422        (if (cmp == GT_EXPR)
2423         { constant_boolean_node (false, type); })
2424        (if (cmp == GE_EXPR)
2425         (eq @2 @1))
2426        (if (cmp == LE_EXPR)
2427         { constant_boolean_node (true, type); })
2428        (if (cmp == LT_EXPR)
2429         (ne @2 @1))))
2430      (if (wi::eq_p (@1, min))
2431       (switch
2432        (if (cmp == LT_EXPR)
2433         { constant_boolean_node (false, type); })
2434        (if (cmp == LE_EXPR)
2435         (eq @2 @1))
2436        (if (cmp == GE_EXPR)
2437         { constant_boolean_node (true, type); })
2438        (if (cmp == GT_EXPR)
2439         (ne @2 @1))))
2440      (if (wi::eq_p (@1, max - 1))
2441       (switch
2442        (if (cmp == GT_EXPR)
2443         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2444        (if (cmp == LE_EXPR)
2445         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2446      (if (wi::eq_p (@1, min + 1))
2447       (switch
2448        (if (cmp == GE_EXPR)
2449         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2450        (if (cmp == LT_EXPR)
2451         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2452      (if (wi::eq_p (@1, signed_max)
2453           && TYPE_UNSIGNED (arg1_type)
2454           /* We will flip the signedness of the comparison operator
2455              associated with the mode of @1, so the sign bit is
2456              specified by this mode.  Check that @1 is the signed
2457              max associated with this sign bit.  */
2458           && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2459           /* signed_type does not work on pointer types.  */
2460           && INTEGRAL_TYPE_P (arg1_type))
2461       /* The following case also applies to X < signed_max+1
2462          and X >= signed_max+1 because previous transformations.  */
2463       (if (cmp == LE_EXPR || cmp == GT_EXPR)
2464        (with { tree st = signed_type_for (arg1_type); }
2465         (if (cmp == LE_EXPR)
2466          (ge (convert:st @0) { build_zero_cst (st); })
2467          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2469 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2470  /* If the second operand is NaN, the result is constant.  */
2471  (simplify
2472   (cmp @0 REAL_CST@1)
2473   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2474        && (cmp != LTGT_EXPR || ! flag_trapping_math))
2475    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2476                             ? false : true, type); })))
2478 /* bool_var != 0 becomes bool_var.  */
2479 (simplify
2480  (ne @0 integer_zerop)
2481  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2482       && types_match (type, TREE_TYPE (@0)))
2483   (non_lvalue @0)))
2484 /* bool_var == 1 becomes bool_var.  */
2485 (simplify
2486  (eq @0 integer_onep)
2487  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2488       && types_match (type, TREE_TYPE (@0)))
2489   (non_lvalue @0)))
2490 /* Do not handle
2491    bool_var == 0 becomes !bool_var or
2492    bool_var != 1 becomes !bool_var
2493    here because that only is good in assignment context as long
2494    as we require a tcc_comparison in GIMPLE_CONDs where we'd
2495    replace if (x == 0) with tem = ~x; if (tem != 0) which is
2496    clearly less optimal and which we'll transform again in forwprop.  */
2498 /* When one argument is a constant, overflow detection can be simplified.
2499    Currently restricted to single use so as not to interfere too much with
2500    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2501    A + CST CMP A  ->  A CMP' CST' */
2502 (for cmp (lt le ge gt)
2503      out (gt gt le le)
2504  (simplify
2505   (cmp (plus@2 @0 INTEGER_CST@1) @0)
2506   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2507        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2508        && wi::ne_p (@1, 0)
2509        && single_use (@2))
2510    (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2511                (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2512 /* A CMP A + CST  ->  A CMP' CST' */
2513 (for cmp (gt ge le lt)
2514      out (gt gt le le)
2515  (simplify
2516   (cmp @0 (plus@2 @0 INTEGER_CST@1))
2517   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2518        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2519        && wi::ne_p (@1, 0)
2520        && single_use (@2))
2521    (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2522                (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2524 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2525    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2526    expects the long form, so we restrict the transformation for now.  */
2527 (for cmp (gt le)
2528  (simplify
2529   (cmp (minus@2 @0 @1) @0)
2530   (if (single_use (@2)
2531        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2532        && TYPE_UNSIGNED (TREE_TYPE (@0))
2533        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2534    (cmp @1 @0))))
2535 (for cmp (lt ge)
2536  (simplify
2537   (cmp @0 (minus@2 @0 @1))
2538   (if (single_use (@2)
2539        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2540        && TYPE_UNSIGNED (TREE_TYPE (@0))
2541        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2542    (cmp @0 @1))))
2544 /* Testing for overflow is unnecessary if we already know the result.  */
2545 /* A < A - B  */
2546 (for cmp (lt ge)
2547      out (ne eq)
2548  (simplify
2549   (cmp @0 (realpart (IFN_SUB_OVERFLOW@2 @0 @1)))
2550   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2551        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2552    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2553 /* A - B > A  */
2554 (for cmp (gt le)
2555      out (ne eq)
2556  (simplify
2557   (cmp (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2558   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2559        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2560    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2561 /* A + B < A  */
2562 (for cmp (lt ge)
2563      out (ne eq)
2564  (simplify
2565   (cmp (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2566   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2567        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2568    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2569 /* A > A + B  */
2570 (for cmp (gt le)
2571      out (ne eq)
2572  (simplify
2573   (cmp @0 (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)))
2574   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2575        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2576    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2579 /* Simplification of math builtins.  These rules must all be optimizations
2580    as well as IL simplifications.  If there is a possibility that the new
2581    form could be a pessimization, the rule should go in the canonicalization
2582    section that follows this one.
2584    Rules can generally go in this section if they satisfy one of
2585    the following:
2587    - the rule describes an identity
2589    - the rule replaces calls with something as simple as addition or
2590      multiplication
2592    - the rule contains unary calls only and simplifies the surrounding
2593      arithmetic.  (The idea here is to exclude non-unary calls in which
2594      one operand is constant and in which the call is known to be cheap
2595      when the operand has that value.)  */
2597 (if (flag_unsafe_math_optimizations)
2598  /* Simplify sqrt(x) * sqrt(x) -> x.  */
2599  (simplify
2600   (mult (SQRT@1 @0) @1)
2601   (if (!HONOR_SNANS (type))
2602    @0))
2604  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
2605  (for root (SQRT CBRT)
2606   (simplify
2607    (mult (root:s @0) (root:s @1))
2608     (root (mult @0 @1))))
2610  /* Simplify expN(x) * expN(y) -> expN(x+y). */
2611  (for exps (EXP EXP2 EXP10 POW10)
2612   (simplify
2613    (mult (exps:s @0) (exps:s @1))
2614     (exps (plus @0 @1))))
2616  /* Simplify a/root(b/c) into a*root(c/b).  */
2617  (for root (SQRT CBRT)
2618   (simplify
2619    (rdiv @0 (root:s (rdiv:s @1 @2)))
2620     (mult @0 (root (rdiv @2 @1)))))
2622  /* Simplify x/expN(y) into x*expN(-y).  */
2623  (for exps (EXP EXP2 EXP10 POW10)
2624   (simplify
2625    (rdiv @0 (exps:s @1))
2626     (mult @0 (exps (negate @1)))))
2628  (for logs (LOG LOG2 LOG10 LOG10)
2629       exps (EXP EXP2 EXP10 POW10)
2630   /* logN(expN(x)) -> x.  */
2631   (simplify
2632    (logs (exps @0))
2633    @0)
2634   /* expN(logN(x)) -> x.  */
2635   (simplify
2636    (exps (logs @0))
2637    @0))
2639  /* Optimize logN(func()) for various exponential functions.  We
2640     want to determine the value "x" and the power "exponent" in
2641     order to transform logN(x**exponent) into exponent*logN(x).  */
2642  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
2643       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
2644   (simplify
2645    (logs (exps @0))
2646    (if (SCALAR_FLOAT_TYPE_P (type))
2647     (with {
2648       tree x;
2649       switch (exps)
2650         {
2651         CASE_CFN_EXP:
2652           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
2653           x = build_real_truncate (type, dconst_e ());
2654           break;
2655         CASE_CFN_EXP2:
2656           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
2657           x = build_real (type, dconst2);
2658           break;
2659         CASE_CFN_EXP10:
2660         CASE_CFN_POW10:
2661           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
2662           {
2663             REAL_VALUE_TYPE dconst10;
2664             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2665             x = build_real (type, dconst10);
2666           }
2667           break;
2668         default:
2669           gcc_unreachable ();
2670         }
2671       }
2672      (mult (logs { x; }) @0)))))
2674  (for logs (LOG LOG
2675             LOG2 LOG2
2676             LOG10 LOG10)
2677       exps (SQRT CBRT)
2678   (simplify
2679    (logs (exps @0))
2680    (if (SCALAR_FLOAT_TYPE_P (type))
2681     (with {
2682       tree x;
2683       switch (exps)
2684         {
2685         CASE_CFN_SQRT:
2686           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
2687           x = build_real (type, dconsthalf);
2688           break;
2689         CASE_CFN_CBRT:
2690           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
2691           x = build_real_truncate (type, dconst_third ());
2692           break;
2693         default:
2694           gcc_unreachable ();
2695         }
2696       }
2697      (mult { x; } (logs @0))))))
2699  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
2700  (for logs (LOG LOG2 LOG10)
2701       pows (POW)
2702   (simplify
2703    (logs (pows @0 @1))
2704    (mult @1 (logs @0))))
2706  (for sqrts (SQRT)
2707       cbrts (CBRT)
2708       pows (POW)
2709       exps (EXP EXP2 EXP10 POW10)
2710   /* sqrt(expN(x)) -> expN(x*0.5).  */
2711   (simplify
2712    (sqrts (exps @0))
2713    (exps (mult @0 { build_real (type, dconsthalf); })))
2714   /* cbrt(expN(x)) -> expN(x/3).  */
2715   (simplify
2716    (cbrts (exps @0))
2717    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2718   /* pow(expN(x), y) -> expN(x*y).  */
2719   (simplify
2720    (pows (exps @0) @1)
2721    (exps (mult @0 @1))))
2723  /* tan(atan(x)) -> x.  */
2724  (for tans (TAN)
2725       atans (ATAN)
2726   (simplify
2727    (tans (atans @0))
2728    @0)))
2730 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
2731 (simplify
2732  (CABS (complex:c @0 real_zerop@1))
2733  (abs @0))
2735 /* trunc(trunc(x)) -> trunc(x), etc.  */
2736 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2737  (simplify
2738   (fns (fns @0))
2739   (fns @0)))
2740 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
2741 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2742  (simplify
2743   (fns integer_valued_real_p@0)
2744   @0))
2746 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
2747 (simplify
2748  (HYPOT:c @0 real_zerop@1)
2749  (abs @0))
2751 /* pow(1,x) -> 1.  */
2752 (simplify
2753  (POW real_onep@0 @1)
2754  @0)
2756 (simplify
2757  /* copysign(x,x) -> x.  */
2758  (COPYSIGN @0 @0)
2759  @0)
2761 (simplify
2762  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
2763  (COPYSIGN @0 tree_expr_nonnegative_p@1)
2764  (abs @0))
2766 (for scale (LDEXP SCALBN SCALBLN)
2767  /* ldexp(0, x) -> 0.  */
2768  (simplify
2769   (scale real_zerop@0 @1)
2770   @0)
2771  /* ldexp(x, 0) -> x.  */
2772  (simplify
2773   (scale @0 integer_zerop@1)
2774   @0)
2775  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
2776  (simplify
2777   (scale REAL_CST@0 @1)
2778   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2779    @0)))
2781 /* Canonicalization of sequences of math builtins.  These rules represent
2782    IL simplifications but are not necessarily optimizations.
2784    The sincos pass is responsible for picking "optimal" implementations
2785    of math builtins, which may be more complicated and can sometimes go
2786    the other way, e.g. converting pow into a sequence of sqrts.
2787    We only want to do these canonicalizations before the pass has run.  */
2789 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2790  /* Simplify tan(x) * cos(x) -> sin(x). */
2791  (simplify
2792   (mult:c (TAN:s @0) (COS:s @0))
2793    (SIN @0))
2795  /* Simplify x * pow(x,c) -> pow(x,c+1). */
2796  (simplify
2797   (mult:c @0 (POW:s @0 REAL_CST@1))
2798   (if (!TREE_OVERFLOW (@1))
2799    (POW @0 (plus @1 { build_one_cst (type); }))))
2801  /* Simplify sin(x) / cos(x) -> tan(x). */
2802  (simplify
2803   (rdiv (SIN:s @0) (COS:s @0))
2804    (TAN @0))
2806  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2807  (simplify
2808   (rdiv (COS:s @0) (SIN:s @0))
2809    (rdiv { build_one_cst (type); } (TAN @0)))
2811  /* Simplify sin(x) / tan(x) -> cos(x). */
2812  (simplify
2813   (rdiv (SIN:s @0) (TAN:s @0))
2814   (if (! HONOR_NANS (@0)
2815        && ! HONOR_INFINITIES (@0))
2816    (COS @0)))
2818  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2819  (simplify
2820   (rdiv (TAN:s @0) (SIN:s @0))
2821   (if (! HONOR_NANS (@0)
2822        && ! HONOR_INFINITIES (@0))
2823    (rdiv { build_one_cst (type); } (COS @0))))
2825  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2826  (simplify
2827   (mult (POW:s @0 @1) (POW:s @0 @2))
2828    (POW @0 (plus @1 @2)))
2830  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2831  (simplify
2832   (mult (POW:s @0 @1) (POW:s @2 @1))
2833    (POW (mult @0 @2) @1))
2835  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2836  (simplify
2837   (mult (POWI:s @0 @1) (POWI:s @2 @1))
2838    (POWI (mult @0 @2) @1))
2840  /* Simplify pow(x,c) / x -> pow(x,c-1). */
2841  (simplify
2842   (rdiv (POW:s @0 REAL_CST@1) @0)
2843   (if (!TREE_OVERFLOW (@1))
2844    (POW @0 (minus @1 { build_one_cst (type); }))))
2846  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2847  (simplify
2848   (rdiv @0 (POW:s @1 @2))
2849    (mult @0 (POW @1 (negate @2))))
2851  (for sqrts (SQRT)
2852       cbrts (CBRT)
2853       pows (POW)
2854   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
2855   (simplify
2856    (sqrts (sqrts @0))
2857    (pows @0 { build_real (type, dconst_quarter ()); }))
2858   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
2859   (simplify
2860    (sqrts (cbrts @0))
2861    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2862   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
2863   (simplify
2864    (cbrts (sqrts @0))
2865    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2866   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
2867   (simplify
2868    (cbrts (cbrts tree_expr_nonnegative_p@0))
2869    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2870   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
2871   (simplify
2872    (sqrts (pows @0 @1))
2873    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2874   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
2875   (simplify
2876    (cbrts (pows tree_expr_nonnegative_p@0 @1))
2877    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2878   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
2879   (simplify
2880    (pows (sqrts @0) @1)
2881    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2882   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
2883   (simplify
2884    (pows (cbrts tree_expr_nonnegative_p@0) @1)
2885    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2886   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
2887   (simplify
2888    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2889    (pows @0 (mult @1 @2))))
2891  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
2892  (simplify
2893   (CABS (complex @0 @0))
2894   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2896  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
2897  (simplify
2898   (HYPOT @0 @0)
2899   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2901  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
2902  (for cexps (CEXP)
2903       exps (EXP)
2904       cexpis (CEXPI)
2905   (simplify
2906    (cexps compositional_complex@0)
2907    (if (targetm.libc_has_function (function_c99_math_complex))
2908     (complex
2909      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2910      (mult @1 (imagpart @2)))))))
2912 (if (canonicalize_math_p ())
2913  /* floor(x) -> trunc(x) if x is nonnegative.  */
2914  (for floors (FLOOR)
2915       truncs (TRUNC)
2916   (simplify
2917    (floors tree_expr_nonnegative_p@0)
2918    (truncs @0))))
2920 (match double_value_p
2921  @0
2922  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2923 (for froms (BUILT_IN_TRUNCL
2924             BUILT_IN_FLOORL
2925             BUILT_IN_CEILL
2926             BUILT_IN_ROUNDL
2927             BUILT_IN_NEARBYINTL
2928             BUILT_IN_RINTL)
2929      tos (BUILT_IN_TRUNC
2930           BUILT_IN_FLOOR
2931           BUILT_IN_CEIL
2932           BUILT_IN_ROUND
2933           BUILT_IN_NEARBYINT
2934           BUILT_IN_RINT)
2935  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
2936  (if (optimize && canonicalize_math_p ())
2937   (simplify
2938    (froms (convert double_value_p@0))
2939    (convert (tos @0)))))
2941 (match float_value_p
2942  @0
2943  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
2944 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
2945             BUILT_IN_FLOORL BUILT_IN_FLOOR
2946             BUILT_IN_CEILL BUILT_IN_CEIL
2947             BUILT_IN_ROUNDL BUILT_IN_ROUND
2948             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
2949             BUILT_IN_RINTL BUILT_IN_RINT)
2950      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
2951           BUILT_IN_FLOORF BUILT_IN_FLOORF
2952           BUILT_IN_CEILF BUILT_IN_CEILF
2953           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
2954           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
2955           BUILT_IN_RINTF BUILT_IN_RINTF)
2956  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
2957     if x is a float.  */
2958  (if (optimize && canonicalize_math_p ()
2959       && targetm.libc_has_function (function_c99_misc))
2960   (simplify
2961    (froms (convert float_value_p@0))
2962    (convert (tos @0)))))
2964 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
2965      tos (XFLOOR XCEIL XROUND XRINT)
2966  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
2967  (if (optimize && canonicalize_math_p ())
2968   (simplify
2969    (froms (convert double_value_p@0))
2970    (tos @0))))
2972 (for froms (XFLOORL XCEILL XROUNDL XRINTL
2973             XFLOOR XCEIL XROUND XRINT)
2974      tos (XFLOORF XCEILF XROUNDF XRINTF)
2975  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
2976     if x is a float.  */
2977  (if (optimize && canonicalize_math_p ())
2978   (simplify
2979    (froms (convert float_value_p@0))
2980    (tos @0))))
2982 (if (canonicalize_math_p ())
2983  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
2984  (for floors (IFLOOR LFLOOR LLFLOOR)
2985   (simplify
2986    (floors tree_expr_nonnegative_p@0)
2987    (fix_trunc @0))))
2989 (if (canonicalize_math_p ())
2990  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
2991  (for fns (IFLOOR LFLOOR LLFLOOR
2992            ICEIL LCEIL LLCEIL
2993            IROUND LROUND LLROUND)
2994   (simplify
2995    (fns integer_valued_real_p@0)
2996    (fix_trunc @0)))
2997  (if (!flag_errno_math)
2998   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
2999   (for rints (IRINT LRINT LLRINT)
3000    (simplify
3001     (rints integer_valued_real_p@0)
3002     (fix_trunc @0)))))
3004 (if (canonicalize_math_p ())
3005  (for ifn (IFLOOR ICEIL IROUND IRINT)
3006       lfn (LFLOOR LCEIL LROUND LRINT)
3007       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3008   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3009      sizeof (int) == sizeof (long).  */
3010   (if (TYPE_PRECISION (integer_type_node)
3011        == TYPE_PRECISION (long_integer_type_node))
3012    (simplify
3013     (ifn @0)
3014     (lfn:long_integer_type_node @0)))
3015   /* Canonicalize llround (x) to lround (x) on LP64 targets where
3016      sizeof (long long) == sizeof (long).  */
3017   (if (TYPE_PRECISION (long_long_integer_type_node)
3018        == TYPE_PRECISION (long_integer_type_node))
3019    (simplify
3020     (llfn @0)
3021     (lfn:long_integer_type_node @0)))))
3023 /* cproj(x) -> x if we're ignoring infinities.  */
3024 (simplify
3025  (CPROJ @0)
3026  (if (!HONOR_INFINITIES (type))
3027    @0))
3029 /* If the real part is inf and the imag part is known to be
3030    nonnegative, return (inf + 0i).  */
3031 (simplify
3032  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3033  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3034   { build_complex_inf (type, false); }))
3036 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
3037 (simplify
3038  (CPROJ (complex @0 REAL_CST@1))
3039  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3040   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3042 (for pows (POW)
3043      sqrts (SQRT)
3044      cbrts (CBRT)
3045  (simplify
3046   (pows @0 REAL_CST@1)
3047   (with {
3048     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3049     REAL_VALUE_TYPE tmp;
3050    }
3051    (switch
3052     /* pow(x,0) -> 1.  */
3053     (if (real_equal (value, &dconst0))
3054      { build_real (type, dconst1); })
3055     /* pow(x,1) -> x.  */
3056     (if (real_equal (value, &dconst1))
3057      @0)
3058     /* pow(x,-1) -> 1/x.  */
3059     (if (real_equal (value, &dconstm1))
3060      (rdiv { build_real (type, dconst1); } @0))
3061     /* pow(x,0.5) -> sqrt(x).  */
3062     (if (flag_unsafe_math_optimizations
3063          && canonicalize_math_p ()
3064          && real_equal (value, &dconsthalf))
3065      (sqrts @0))
3066     /* pow(x,1/3) -> cbrt(x).  */
3067     (if (flag_unsafe_math_optimizations
3068          && canonicalize_math_p ()
3069          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3070              real_equal (value, &tmp)))
3071      (cbrts @0))))))
3073 /* powi(1,x) -> 1.  */
3074 (simplify
3075  (POWI real_onep@0 @1)
3076  @0)
3078 (simplify
3079  (POWI @0 INTEGER_CST@1)
3080  (switch
3081   /* powi(x,0) -> 1.  */
3082   (if (wi::eq_p (@1, 0))
3083    { build_real (type, dconst1); })
3084   /* powi(x,1) -> x.  */
3085   (if (wi::eq_p (@1, 1))
3086    @0)
3087   /* powi(x,-1) -> 1/x.  */
3088   (if (wi::eq_p (@1, -1))
3089    (rdiv { build_real (type, dconst1); } @0))))
3091 /* Narrowing of arithmetic and logical operations. 
3093    These are conceptually similar to the transformations performed for
3094    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
3095    term we want to move all that code out of the front-ends into here.  */
3097 /* If we have a narrowing conversion of an arithmetic operation where
3098    both operands are widening conversions from the same type as the outer
3099    narrowing conversion.  Then convert the innermost operands to a suitable
3100    unsigned type (to avoid introducing undefined behavior), perform the
3101    operation and convert the result to the desired type.  */
3102 (for op (plus minus)
3103   (simplify
3104     (convert (op:s (convert@2 @0) (convert@3 @1)))
3105     (if (INTEGRAL_TYPE_P (type)
3106          /* We check for type compatibility between @0 and @1 below,
3107             so there's no need to check that @1/@3 are integral types.  */
3108          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3109          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3110          /* The precision of the type of each operand must match the
3111             precision of the mode of each operand, similarly for the
3112             result.  */
3113          && (TYPE_PRECISION (TREE_TYPE (@0))
3114              == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3115          && (TYPE_PRECISION (TREE_TYPE (@1))
3116              == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3117          && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3118          /* The inner conversion must be a widening conversion.  */
3119          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3120          && types_match (@0, @1)
3121          && types_match (@0, type))
3122       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3123         (convert (op @0 @1))
3124         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3125          (convert (op (convert:utype @0) (convert:utype @1))))))))
3127 /* This is another case of narrowing, specifically when there's an outer
3128    BIT_AND_EXPR which masks off bits outside the type of the innermost
3129    operands.   Like the previous case we have to convert the operands
3130    to unsigned types to avoid introducing undefined behavior for the
3131    arithmetic operation.  */
3132 (for op (minus plus)
3133  (simplify
3134   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3135   (if (INTEGRAL_TYPE_P (type)
3136        /* We check for type compatibility between @0 and @1 below,
3137           so there's no need to check that @1/@3 are integral types.  */
3138        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3139        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3140        /* The precision of the type of each operand must match the
3141           precision of the mode of each operand, similarly for the
3142           result.  */
3143        && (TYPE_PRECISION (TREE_TYPE (@0))
3144            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3145        && (TYPE_PRECISION (TREE_TYPE (@1))
3146            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3147        && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3148        /* The inner conversion must be a widening conversion.  */
3149        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3150        && types_match (@0, @1)
3151        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3152            <= TYPE_PRECISION (TREE_TYPE (@0)))
3153        && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3154                         true, TYPE_PRECISION (type))) == 0))
3155    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3156     (with { tree ntype = TREE_TYPE (@0); }
3157      (convert (bit_and (op @0 @1) (convert:ntype @4))))
3158     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3159      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3160                (convert:utype @4))))))))
3162 /* Transform (@0 < @1 and @0 < @2) to use min, 
3163    (@0 > @1 and @0 > @2) to use max */
3164 (for op (lt le gt ge)
3165      ext (min min max max)
3166  (simplify
3167   (bit_and (op:s @0 @1) (op:s @0 @2))
3168   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3169    (op @0 (ext @1 @2)))))
3171 (simplify
3172  /* signbit(x) -> 0 if x is nonnegative.  */
3173  (SIGNBIT tree_expr_nonnegative_p@0)
3174  { integer_zero_node; })
3176 (simplify
3177  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
3178  (SIGNBIT @0)
3179  (if (!HONOR_SIGNED_ZEROS (@0))
3180   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))