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