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