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