Daily bump.
[official-gcc.git] / gcc / match.pd
blob6c225b483d26226eabef41df42f5dc76671fe148
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 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
30    real_zerop real_onep real_minus_onep
31    CONSTANT_CLASS_P
32    tree_expr_nonnegative_p)
34 /* Operator lists.  */
35 (define_operator_list tcc_comparison
36   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
37 (define_operator_list inverted_tcc_comparison
38   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
39 (define_operator_list inverted_tcc_comparison_with_nans
40   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
43 /* Simplifications of operations with one constant operand and
44    simplifications to constants or single values.  */
46 (for op (plus pointer_plus minus bit_ior bit_xor)
47   (simplify
48     (op @0 integer_zerop)
49     (non_lvalue @0)))
51 /* 0 +p index -> (type)index */
52 (simplify
53  (pointer_plus integer_zerop @1)
54  (non_lvalue (convert @1)))
56 /* See if ARG1 is zero and X + ARG1 reduces to X.
57    Likewise if the operands are reversed.  */
58 (simplify
59  (plus:c @0 real_zerop@1)
60  (if (fold_real_zero_addition_p (type, @1, 0))
61   (non_lvalue @0)))
63 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
64 (simplify
65  (minus @0 real_zerop@1)
66  (if (fold_real_zero_addition_p (type, @1, 1))
67   (non_lvalue @0)))
69 /* Simplify x - x.
70    This is unsafe for certain floats even in non-IEEE formats.
71    In IEEE, it is unsafe because it does wrong for NaNs.
72    Also note that operand_equal_p is always false if an operand
73    is volatile.  */
74 (simplify
75  (minus @0 @0)
76  (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (element_mode (type)))
77   { build_zero_cst (type); }))
79 (simplify
80  (mult @0 integer_zerop@1)
81  @1)
83 /* Maybe fold x * 0 to 0.  The expressions aren't the same
84    when x is NaN, since x * 0 is also NaN.  Nor are they the
85    same in modes with signed zeros, since multiplying a
86    negative value by 0 gives -0, not +0.  */
87 (simplify
88  (mult @0 real_zerop@1)
89  (if (!HONOR_NANS (element_mode (type))
90       && !HONOR_SIGNED_ZEROS (element_mode (type)))
91   @1))
93 /* In IEEE floating point, x*1 is not equivalent to x for snans.
94    Likewise for complex arithmetic with signed zeros.  */
95 (simplify
96  (mult @0 real_onep)
97  (if (!HONOR_SNANS (element_mode (type))
98       && (!HONOR_SIGNED_ZEROS (element_mode (type))
99           || !COMPLEX_FLOAT_TYPE_P (type)))
100   (non_lvalue @0)))
102 /* Transform x * -1.0 into -x.  */
103 (simplify
104  (mult @0 real_minus_onep)
105   (if (!HONOR_SNANS (element_mode (type))
106        && (!HONOR_SIGNED_ZEROS (element_mode (type))
107            || !COMPLEX_FLOAT_TYPE_P (type)))
108    (negate @0)))
110 /* Make sure to preserve divisions by zero.  This is the reason why
111    we don't simplify x / x to 1 or 0 / x to 0.  */
112 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
113   (simplify
114     (op @0 integer_onep)
115     (non_lvalue @0)))
117 /* X / -1 is -X.  */
118 (for div (trunc_div ceil_div floor_div round_div exact_div)
119  (simplify
120    (div @0 integer_minus_onep@1)
121    (if (!TYPE_UNSIGNED (type))
122     (negate @0))))
124 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
125    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
126 (simplify
127  (floor_div @0 @1)
128  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
129       && TYPE_UNSIGNED (type))
130   (trunc_div @0 @1)))
132 /* Combine two successive divisions.  Note that combining ceil_div
133    and floor_div is trickier and combining round_div even more so.  */
134 (for div (trunc_div exact_div)
135  (simplify
136   (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
137   (with {
138     bool overflow_p;
139     wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
140    }
141    (if (!overflow_p)
142     (div @0 { wide_int_to_tree (type, mul); }))
143    (if (overflow_p
144         && (TYPE_UNSIGNED (type)
145             || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)))
146     { build_zero_cst (type); }))))
148 /* Optimize A / A to 1.0 if we don't care about
149    NaNs or Infinities.  */
150 (simplify
151  (rdiv @0 @0)
152  (if (FLOAT_TYPE_P (type)
153       && ! HONOR_NANS (element_mode (type))
154       && ! HONOR_INFINITIES (element_mode (type)))
155   { build_one_cst (type); }))
157 /* Optimize -A / A to -1.0 if we don't care about
158    NaNs or Infinities.  */
159 (simplify
160  (rdiv:c @0 (negate @0))
161  (if (FLOAT_TYPE_P (type)
162       && ! HONOR_NANS (element_mode (type))
163       && ! HONOR_INFINITIES (element_mode (type)))
164   { build_minus_one_cst (type); }))
166 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
167 (simplify
168  (rdiv @0 real_onep)
169  (if (!HONOR_SNANS (element_mode (type)))
170   (non_lvalue @0)))
172 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
173 (simplify
174  (rdiv @0 real_minus_onep)
175  (if (!HONOR_SNANS (element_mode (type)))
176   (negate @0)))
178 /* If ARG1 is a constant, we can convert this to a multiply by the
179    reciprocal.  This does not have the same rounding properties,
180    so only do this if -freciprocal-math.  We can actually
181    always safely do it if ARG1 is a power of two, but it's hard to
182    tell if it is or not in a portable manner.  */
183 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
184  (simplify
185   (rdiv @0 cst@1)
186   (if (optimize)
187    (if (flag_reciprocal_math
188         && !real_zerop (@1))
189     (with
190      { tree tem = fold_binary (RDIV_EXPR, type, build_one_cst (type), @1); }
191      (if (tem)
192       (mult @0 { tem; } ))))
193    (if (cst != COMPLEX_CST)
194     (with { tree inverse = exact_inverse (type, @1); }
195      (if (inverse)
196       (mult @0 { inverse; } )))))))
198 /* Same applies to modulo operations, but fold is inconsistent here
199    and simplifies 0 % x to 0, only preserving literal 0 % 0.  */
200 (for mod (ceil_mod floor_mod round_mod trunc_mod)
201  /* 0 % X is always zero.  */
202  (simplify
203   (mod integer_zerop@0 @1)
204   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
205   (if (!integer_zerop (@1))
206    @0))
207  /* X % 1 is always zero.  */
208  (simplify
209   (mod @0 integer_onep)
210   { build_zero_cst (type); })
211  /* X % -1 is zero.  */
212  (simplify
213   (mod @0 integer_minus_onep@1)
214   (if (!TYPE_UNSIGNED (type))
215    { build_zero_cst (type); })))
217 /* X % -C is the same as X % C.  */
218 (simplify
219  (trunc_mod @0 INTEGER_CST@1)
220   (if (TYPE_SIGN (type) == SIGNED
221        && !TREE_OVERFLOW (@1)
222        && wi::neg_p (@1)
223        && !TYPE_OVERFLOW_TRAPS (type)
224        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
225        && !sign_bit_p (@1, @1))
226    (trunc_mod @0 (negate @1))))
228 /* x | ~0 -> ~0  */
229 (simplify
230   (bit_ior @0 integer_all_onesp@1)
231   @1)
233 /* x & 0 -> 0  */
234 (simplify
235   (bit_and @0 integer_zerop@1)
236   @1)
238 /* x ^ x -> 0 */
239 (simplify
240   (bit_xor @0 @0)
241   { build_zero_cst (type); })
243 /* Canonicalize X ^ ~0 to ~X.  */
244 (simplify
245   (bit_xor @0 integer_all_onesp@1)
246   (bit_not @0))
248 /* x & ~0 -> x  */
249 (simplify
250  (bit_and @0 integer_all_onesp)
251   (non_lvalue @0))
253 /* x & x -> x,  x | x -> x  */
254 (for bitop (bit_and bit_ior)
255  (simplify
256   (bitop @0 @0)
257   (non_lvalue @0)))
259 (simplify
260  (abs (negate @0))
261  (abs @0))
262 (simplify
263  (abs tree_expr_nonnegative_p@0)
264  @0)
267 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
268    when profitable.
269    For bitwise binary operations apply operand conversions to the
270    binary operation result instead of to the operands.  This allows
271    to combine successive conversions and bitwise binary operations.
272    We combine the above two cases by using a conditional convert.  */
273 (for bitop (bit_and bit_ior bit_xor)
274  (simplify
275   (bitop (convert @0) (convert? @1))
276   (if (((TREE_CODE (@1) == INTEGER_CST
277          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
278          && int_fits_type_p (@1, TREE_TYPE (@0)))
279         || (GIMPLE && types_compatible_p (TREE_TYPE (@0), TREE_TYPE (@1)))
280         || (GENERIC && TREE_TYPE (@0) == TREE_TYPE (@1)))
281        /* ???  This transform conflicts with fold-const.c doing
282           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
283           constants (if x has signed type, the sign bit cannot be set
284           in c).  This folds extension into the BIT_AND_EXPR.
285           Restrict it to GIMPLE to avoid endless recursions.  */
286        && (bitop != BIT_AND_EXPR || GIMPLE)
287        && (/* That's a good idea if the conversion widens the operand, thus
288               after hoisting the conversion the operation will be narrower.  */
289            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
290            /* It's also a good idea if the conversion is to a non-integer
291               mode.  */
292            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
293            /* Or if the precision of TO is not the same as the precision
294               of its mode.  */
295            || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
296    (convert (bitop @0 (convert @1))))))
298 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
299 (for bitop (bit_and bit_ior bit_xor)
300  (simplify
301   (bitop (bit_and:c @0 @1) (bit_and @2 @1))
302   (bit_and (bitop @0 @2) @1)))
304 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
305 (simplify
306   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
307   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
309 /* Combine successive equal operations with constants.  */
310 (for bitop (bit_and bit_ior bit_xor)
311  (simplify
312   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
313   (bitop @0 (bitop @1 @2))))
315 /* Try simple folding for X op !X, and X op X with the help
316    of the truth_valued_p and logical_inverted_value predicates.  */
317 (match truth_valued_p
318  @0
319  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
320 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
321  (match truth_valued_p
322   (op @0 @1)))
323 (match truth_valued_p
324   (truth_not @0))
326 (match (logical_inverted_value @0)
327  (bit_not truth_valued_p@0))
328 (match (logical_inverted_value @0)
329  (eq @0 integer_zerop))
330 (match (logical_inverted_value @0)
331  (ne truth_valued_p@0 integer_truep))
332 (match (logical_inverted_value @0)
333  (bit_xor truth_valued_p@0 integer_truep))
335 /* X & !X -> 0.  */
336 (simplify
337  (bit_and:c @0 (logical_inverted_value @0))
338  { build_zero_cst (type); })
339 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
340 (for op (bit_ior bit_xor)
341  (simplify
342   (op:c truth_valued_p@0 (logical_inverted_value @0))
343   { constant_boolean_node (true, type); }))
345 (for bitop (bit_and bit_ior)
346      rbitop (bit_ior bit_and)
347   /* (x | y) & x -> x */
348   /* (x & y) | x -> x */
349  (simplify
350   (bitop:c (rbitop:c @0 @1) @0)
351   @0)
352  /* (~x | y) & x -> x & y */
353  /* (~x & y) | x -> x | y */
354  (simplify
355   (bitop:c (rbitop:c (bit_not @0) @1) @0)
356   (bitop @0 @1)))
358 /* If arg1 and arg2 are booleans (or any single bit type)
359    then try to simplify:
361    (~X & Y) -> X < Y
362    (X & ~Y) -> Y < X
363    (~X | Y) -> X <= Y
364    (X | ~Y) -> Y <= X
366    But only do this if our result feeds into a comparison as
367    this transformation is not always a win, particularly on
368    targets with and-not instructions.
369    -> simplify_bitwise_binary_boolean */
370 (simplify
371   (ne (bit_and:c (bit_not @0) @1) integer_zerop)
372   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
373        && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
374    (lt @0 @1)))
375 (simplify
376   (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
377   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
378        && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
379    (le @0 @1)))
381 /* ~~x -> x */
382 (simplify
383   (bit_not (bit_not @0))
384   @0)
387 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
388 (simplify
389   (pointer_plus (pointer_plus@2 @0 @1) @3)
390   (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
391    (pointer_plus @0 (plus @1 @3))))
393 /* Pattern match
394      tem1 = (long) ptr1;
395      tem2 = (long) ptr2;
396      tem3 = tem2 - tem1;
397      tem4 = (unsigned long) tem3;
398      tem5 = ptr1 + tem4;
399    and produce
400      tem5 = ptr2;  */
401 (simplify
402   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
403   /* Conditionally look through a sign-changing conversion.  */
404   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
405        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
406             || (GENERIC && type == TREE_TYPE (@1))))
407    @1))
409 /* Pattern match
410      tem = (sizetype) ptr;
411      tem = tem & algn;
412      tem = -tem;
413      ... = ptr p+ tem;
414    and produce the simpler and easier to analyze with respect to alignment
415      ... = ptr & ~algn;  */
416 (simplify
417   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
418   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
419    (bit_and @0 { algn; })))
422 /* We can't reassociate at all for saturating types.  */
423 (if (!TYPE_SATURATING (type))
425  /* Contract negates.  */
426  /* A + (-B) -> A - B */
427  (simplify
428   (plus:c (convert1? @0) (convert2? (negate @1)))
429   /* Apply STRIP_NOPS on @0 and the negate.  */
430   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
431        && tree_nop_conversion_p (type, TREE_TYPE (@1))
432        && !TYPE_OVERFLOW_SANITIZED (type))
433    (minus (convert @0) (convert @1))))
434  /* A - (-B) -> A + B */
435  (simplify
436   (minus (convert1? @0) (convert2? (negate @1)))
437   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
438        && tree_nop_conversion_p (type, TREE_TYPE (@1))
439        && !TYPE_OVERFLOW_SANITIZED (type))
440    (plus (convert @0) (convert @1))))
441  /* -(-A) -> A */
442  (simplify
443   (negate (convert? (negate @1)))
444   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
445        && !TYPE_OVERFLOW_SANITIZED (type))
446    (convert @1)))
448  /* We can't reassociate floating-point or fixed-point plus or minus
449     because of saturation to +-Inf.  */
450  (if (!FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
452   /* Match patterns that allow contracting a plus-minus pair
453      irrespective of overflow issues.  */
454   /* (A +- B) - A       ->  +- B */
455   /* (A +- B) -+ B      ->  A */
456   /* A - (A +- B)       -> -+ B */
457   /* A +- (B -+ A)      ->  +- B */
458   (simplify
459     (minus (plus:c @0 @1) @0)
460     @1)
461   (simplify
462     (minus (minus @0 @1) @0)
463     (negate @1))
464   (simplify
465     (plus:c (minus @0 @1) @1)
466     @0)
467   (simplify
468    (minus @0 (plus:c @0 @1))
469    (negate @1))
470   (simplify
471    (minus @0 (minus @0 @1))
472    @1)
474   /* (A +- CST) +- CST -> A + CST  */
475   (for outer_op (plus minus)
476    (for inner_op (plus minus)
477     (simplify
478      (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
479      /* If the constant operation overflows we cannot do the transform
480         as we would introduce undefined overflow, for example
481         with (a - 1) + INT_MIN.  */
482      (with { tree cst = fold_binary (outer_op == inner_op
483                                      ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
484       (if (cst && !TREE_OVERFLOW (cst))
485        (inner_op @0 { cst; } ))))))
487   /* (CST - A) +- CST -> CST - A  */
488   (for outer_op (plus minus)
489    (simplify
490     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
491     (with { tree cst = fold_binary (outer_op, type, @1, @2); }
492      (if (cst && !TREE_OVERFLOW (cst))
493       (minus { cst; } @0)))))
495   /* ~A + A -> -1 */
496   (simplify
497    (plus:c (bit_not @0) @0)
498    (if (!TYPE_OVERFLOW_TRAPS (type))
499     { build_all_ones_cst (type); }))
501   /* ~A + 1 -> -A */
502   (simplify
503    (plus (convert? (bit_not @0)) integer_each_onep)
504    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
505     (negate (convert @0))))
507   /* -A - 1 -> ~A */
508   (simplify
509    (minus (convert? (negate @0)) integer_each_onep)
510    (if (!TYPE_OVERFLOW_TRAPS (type)
511         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
512     (bit_not (convert @0))))
514   /* -1 - A -> ~A */
515   (simplify
516    (minus integer_all_onesp @0)
517    (if (TREE_CODE (type) != COMPLEX_TYPE)
518     (bit_not @0)))
520   /* (T)(P + A) - (T)P -> (T) A */
521   (for add (plus pointer_plus)
522    (simplify
523     (minus (convert (add @0 @1))
524      (convert @0))
525     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
526          /* For integer types, if A has a smaller type
527             than T the result depends on the possible
528             overflow in P + A.
529             E.g. T=size_t, A=(unsigned)429497295, P>0.
530             However, if an overflow in P + A would cause
531             undefined behavior, we can assume that there
532             is no overflow.  */
533          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
534              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
535          /* For pointer types, if the conversion of A to the
536             final type requires a sign- or zero-extension,
537             then we have to punt - it is not defined which
538             one is correct.  */
539          || (POINTER_TYPE_P (TREE_TYPE (@0))
540              && TREE_CODE (@1) == INTEGER_CST
541              && tree_int_cst_sign_bit (@1) == 0))
542      (convert @1))))))
545 /* Simplifications of MIN_EXPR and MAX_EXPR.  */
547 (for minmax (min max)
548  (simplify
549   (minmax @0 @0)
550   @0))
551 (simplify
552  (min @0 @1)
553  (if (INTEGRAL_TYPE_P (type)
554       && TYPE_MIN_VALUE (type)
555       && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
556   @1))
557 (simplify
558  (max @0 @1)
559  (if (INTEGRAL_TYPE_P (type)
560       && TYPE_MAX_VALUE (type)
561       && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
562   @1))
565 /* Simplifications of shift and rotates.  */
567 (for rotate (lrotate rrotate)
568  (simplify
569   (rotate integer_all_onesp@0 @1)
570   @0))
572 /* Optimize -1 >> x for arithmetic right shifts.  */
573 (simplify
574  (rshift integer_all_onesp@0 @1)
575  (if (!TYPE_UNSIGNED (type)
576       && tree_expr_nonnegative_p (@1))
577   @0))
579 (for shiftrotate (lrotate rrotate lshift rshift)
580  (simplify
581   (shiftrotate @0 integer_zerop)
582   (non_lvalue @0))
583  (simplify
584   (shiftrotate integer_zerop@0 @1)
585   @0)
586  /* Prefer vector1 << scalar to vector1 << vector2
587     if vector2 is uniform.  */
588  (for vec (VECTOR_CST CONSTRUCTOR)
589   (simplify
590    (shiftrotate @0 vec@1)
591    (with { tree tem = uniform_vector_p (@1); }
592     (if (tem)
593      (shiftrotate @0 { tem; }))))))
595 /* Rewrite an LROTATE_EXPR by a constant into an
596    RROTATE_EXPR by a new constant.  */
597 (simplify
598  (lrotate @0 INTEGER_CST@1)
599  (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
600                             build_int_cst (TREE_TYPE (@1),
601                                            element_precision (type)), @1); }))
604 /* Simplifications of conversions.  */
606 /* Basic strip-useless-type-conversions / strip_nops.  */
607 (for cvt (convert view_convert float fix_trunc)
608  (simplify
609   (cvt @0)
610   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
611        || (GENERIC && type == TREE_TYPE (@0)))
612    @0)))
614 /* Contract view-conversions.  */
615 (simplify
616   (view_convert (view_convert @0))
617   (view_convert @0))
619 /* For integral conversions with the same precision or pointer
620    conversions use a NOP_EXPR instead.  */
621 (simplify
622   (view_convert @0)
623   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
624        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
625        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
626    (convert @0)))
628 /* Strip inner integral conversions that do not change precision or size.  */
629 (simplify
630   (view_convert (convert@0 @1))
631   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
632        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
633        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
634        && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
635    (view_convert @1)))
637 /* Re-association barriers around constants and other re-association
638    barriers can be removed.  */
639 (simplify
640  (paren CONSTANT_CLASS_P@0)
641  @0)
642 (simplify
643  (paren (paren@1 @0))
644  @1)
646 /* Handle cases of two conversions in a row.  */
647 (for ocvt (convert float fix_trunc)
648  (for icvt (convert float)
649   (simplify
650    (ocvt (icvt@1 @0))
651    (with
652     {
653       tree inside_type = TREE_TYPE (@0);
654       tree inter_type = TREE_TYPE (@1);
655       int inside_int = INTEGRAL_TYPE_P (inside_type);
656       int inside_ptr = POINTER_TYPE_P (inside_type);
657       int inside_float = FLOAT_TYPE_P (inside_type);
658       int inside_vec = VECTOR_TYPE_P (inside_type);
659       unsigned int inside_prec = TYPE_PRECISION (inside_type);
660       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
661       int inter_int = INTEGRAL_TYPE_P (inter_type);
662       int inter_ptr = POINTER_TYPE_P (inter_type);
663       int inter_float = FLOAT_TYPE_P (inter_type);
664       int inter_vec = VECTOR_TYPE_P (inter_type);
665       unsigned int inter_prec = TYPE_PRECISION (inter_type);
666       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
667       int final_int = INTEGRAL_TYPE_P (type);
668       int final_ptr = POINTER_TYPE_P (type);
669       int final_float = FLOAT_TYPE_P (type);
670       int final_vec = VECTOR_TYPE_P (type);
671       unsigned int final_prec = TYPE_PRECISION (type);
672       int final_unsignedp = TYPE_UNSIGNED (type);
673     }
674    /* In addition to the cases of two conversions in a row
675       handled below, if we are converting something to its own
676       type via an object of identical or wider precision, neither
677       conversion is needed.  */
678    (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
679          || (GENERIC
680              && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
681         && (((inter_int || inter_ptr) && final_int)
682             || (inter_float && final_float))
683         && inter_prec >= final_prec)
684     (ocvt @0))
686    /* Likewise, if the intermediate and initial types are either both
687       float or both integer, we don't need the middle conversion if the
688       former is wider than the latter and doesn't change the signedness
689       (for integers).  Avoid this if the final type is a pointer since
690       then we sometimes need the middle conversion.  Likewise if the
691       final type has a precision not equal to the size of its mode.  */
692    (if (((inter_int && inside_int)
693          || (inter_float && inside_float)
694          || (inter_vec && inside_vec))
695         && inter_prec >= inside_prec
696         && (inter_float || inter_vec
697             || inter_unsignedp == inside_unsignedp)
698         && ! (final_prec != GET_MODE_PRECISION (element_mode (type))
699               && element_mode (type) == element_mode (inter_type))
700         && ! final_ptr
701         && (! final_vec || inter_prec == inside_prec))
702     (ocvt @0))
704    /* If we have a sign-extension of a zero-extended value, we can
705       replace that by a single zero-extension.  Likewise if the
706       final conversion does not change precision we can drop the
707       intermediate conversion.  */
708    (if (inside_int && inter_int && final_int
709         && ((inside_prec < inter_prec && inter_prec < final_prec
710              && inside_unsignedp && !inter_unsignedp)
711             || final_prec == inter_prec))
712     (ocvt @0))
714    /* Two conversions in a row are not needed unless:
715         - some conversion is floating-point (overstrict for now), or
716         - some conversion is a vector (overstrict for now), or
717         - the intermediate type is narrower than both initial and
718           final, or
719         - the intermediate type and innermost type differ in signedness,
720           and the outermost type is wider than the intermediate, or
721         - the initial type is a pointer type and the precisions of the
722           intermediate and final types differ, or
723         - the final type is a pointer type and the precisions of the
724           initial and intermediate types differ.  */
725    (if (! inside_float && ! inter_float && ! final_float
726         && ! inside_vec && ! inter_vec && ! final_vec
727         && (inter_prec >= inside_prec || inter_prec >= final_prec)
728         && ! (inside_int && inter_int
729               && inter_unsignedp != inside_unsignedp
730               && inter_prec < final_prec)
731         && ((inter_unsignedp && inter_prec > inside_prec)
732             == (final_unsignedp && final_prec > inter_prec))
733         && ! (inside_ptr && inter_prec != final_prec)
734         && ! (final_ptr && inside_prec != inter_prec)
735         && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
736               && TYPE_MODE (type) == TYPE_MODE (inter_type)))
737     (ocvt @0))
739    /* A truncation to an unsigned type (a zero-extension) should be
740       canonicalized as bitwise and of a mask.  */
741    (if (final_int && inter_int && inside_int
742         && final_prec == inside_prec
743         && final_prec > inter_prec
744         && inter_unsignedp)
745     (convert (bit_and @0 { wide_int_to_tree
746                              (inside_type,
747                               wi::mask (inter_prec, false,
748                                         TYPE_PRECISION (inside_type))); })))
750    /* If we are converting an integer to a floating-point that can
751       represent it exactly and back to an integer, we can skip the
752       floating-point conversion.  */
753    (if (inside_int && inter_float && final_int &&
754         (unsigned) significand_size (TYPE_MODE (inter_type))
755         >= inside_prec - !inside_unsignedp)
756     (convert @0))))))
758 /* If we have a narrowing conversion to an integral type that is fed by a
759    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
760    masks off bits outside the final type (and nothing else).  */
761 (simplify
762   (convert (bit_and @0 INTEGER_CST@1))
763   (if (INTEGRAL_TYPE_P (type)
764        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
765        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
766        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
767                                                     TYPE_PRECISION (type)), 0))
768    (convert @0)))
771 /* (X /[ex] A) * A -> X.  */
772 (simplify
773   (mult (convert? (exact_div @0 @1)) @1)
774   /* Look through a sign-changing conversion.  */
775   (if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
776    (convert @0)))
778 /* Canonicalization of binary operations.  */
780 /* Convert X + -C into X - C.  */
781 (simplify
782  (plus @0 REAL_CST@1)
783  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
784   (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
785    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
786     (minus @0 { tem; })))))
788 /* Convert x+x into x*2.0.  */
789 (simplify
790  (plus @0 @0)
791  (if (SCALAR_FLOAT_TYPE_P (type))
792   (mult @0 { build_real (type, dconst2); })))
794 (simplify
795  (minus integer_zerop @1)
796  (negate @1))
798 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
799    ARG0 is zero and X + ARG0 reduces to X, since that would mean
800    (-ARG1 + ARG0) reduces to -ARG1.  */
801 (simplify
802  (minus real_zerop@0 @1)
803  (if (fold_real_zero_addition_p (type, @0, 0))
804   (negate @1)))
806 /* Transform x * -1 into -x.  */
807 (simplify
808  (mult @0 integer_minus_onep)
809  (negate @0))
811 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
812 (simplify
813  (complex (realpart @0) (imagpart @0))
814  @0)
815 (simplify
816  (realpart (complex @0 @1))
817  @0)
818 (simplify
819  (imagpart (complex @0 @1))
820  @1)
823 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
824 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
825  (simplify
826   (bswap (bswap @0))
827   @0)
828  (simplify
829   (bswap (bit_not (bswap @0)))
830   (bit_not @0))
831  (for bitop (bit_xor bit_ior bit_and)
832   (simplify
833    (bswap (bitop:c (bswap @0) @1))
834    (bitop @0 (bswap @1)))))
837 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
839 /* Simplify constant conditions.
840    Only optimize constant conditions when the selected branch
841    has the same type as the COND_EXPR.  This avoids optimizing
842    away "c ? x : throw", where the throw has a void type.
843    Note that we cannot throw away the fold-const.c variant nor
844    this one as we depend on doing this transform before possibly
845    A ? B : B -> B triggers and the fold-const.c one can optimize
846    0 ? A : B to B even if A has side-effects.  Something
847    genmatch cannot handle.  */
848 (simplify
849  (cond INTEGER_CST@0 @1 @2)
850  (if (integer_zerop (@0)
851       && (!VOID_TYPE_P (TREE_TYPE (@2))
852           || VOID_TYPE_P (type)))
853   @2)
854  (if (!integer_zerop (@0)
855       && (!VOID_TYPE_P (TREE_TYPE (@1))
856           || VOID_TYPE_P (type)))
857   @1))
858 (simplify
859  (vec_cond VECTOR_CST@0 @1 @2)
860  (if (integer_all_onesp (@0))
861   @1)
862  (if (integer_zerop (@0))
863   @2))
865 (for cnd (cond vec_cond)
866  /* A ? B : (A ? X : C) -> A ? B : C.  */
867  (simplify
868   (cnd @0 (cnd @0 @1 @2) @3)
869   (cnd @0 @1 @3))
870  (simplify
871   (cnd @0 @1 (cnd @0 @2 @3))
872   (cnd @0 @1 @3))
874  /* A ? B : B -> B.  */
875  (simplify
876   (cnd @0 @1 @1)
877   @1)
879  /* !A ? B : C -> A ? C : B.  */
880  (simplify
881   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
882   (cnd @0 @2 @1)))
885 /* Simplifications of comparisons.  */
887 /* We can simplify a logical negation of a comparison to the
888    inverted comparison.  As we cannot compute an expression
889    operator using invert_tree_comparison we have to simulate
890    that with expression code iteration.  */
891 (for cmp (tcc_comparison)
892      icmp (inverted_tcc_comparison)
893      ncmp (inverted_tcc_comparison_with_nans)
894  /* Ideally we'd like to combine the following two patterns
895     and handle some more cases by using
896       (logical_inverted_value (cmp @0 @1))
897     here but for that genmatch would need to "inline" that.
898     For now implement what forward_propagate_comparison did.  */
899  (simplify
900   (bit_not (cmp @0 @1))
901   (if (VECTOR_TYPE_P (type)
902        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
903    /* Comparison inversion may be impossible for trapping math,
904       invert_tree_comparison will tell us.  But we can't use
905       a computed operator in the replacement tree thus we have
906       to play the trick below.  */
907    (with { enum tree_code ic = invert_tree_comparison
908              (cmp, HONOR_NANS (element_mode (@0))); }
909     (if (ic == icmp)
910      (icmp @0 @1))
911     (if (ic == ncmp)
912      (ncmp @0 @1)))))
913  (simplify
914   (bit_xor (cmp @0 @1) integer_truep)
915   (with { enum tree_code ic = invert_tree_comparison
916             (cmp, HONOR_NANS (element_mode (@0))); }
917    (if (ic == icmp)
918     (icmp @0 @1))
919    (if (ic == ncmp)
920     (ncmp @0 @1)))))