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
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
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. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
30 real_zerop real_onep real_minus_onep
32 tree_expr_nonnegative_p)
35 /* Simplifications of operations with one constant operand and
36 simplifications to constants or single values. */
38 (for op (plus pointer_plus minus bit_ior bit_xor)
43 /* 0 +p index -> (type)index */
45 (pointer_plus integer_zerop @1)
46 (non_lvalue (convert @1)))
49 This is unsafe for certain floats even in non-IEEE formats.
50 In IEEE, it is unsafe because it does wrong for NaNs.
51 Also note that operand_equal_p is always false if an operand
55 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
56 { build_zero_cst (type); }))
59 (mult @0 integer_zerop@1)
62 /* Make sure to preserve divisions by zero. This is the reason why
63 we don't simplify x / x to 1 or 0 / x to 0. */
64 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
69 /* Same applies to modulo operations, but fold is inconsistent here
70 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
71 (for op (ceil_mod floor_mod round_mod trunc_mod)
72 /* 0 % X is always zero. */
74 (op integer_zerop@0 @1)
75 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
76 (if (!integer_zerop (@1))
78 /* X % 1 is always zero. */
81 { build_zero_cst (type); }))
85 (bit_ior @0 integer_all_onesp@1)
90 (bit_and @0 integer_zerop@1)
96 { build_zero_cst (type); })
98 /* Canonicalize X ^ ~0 to ~X. */
100 (bit_xor @0 integer_all_onesp@1)
105 (bit_and @0 integer_all_onesp)
108 /* x & x -> x, x | x -> x */
109 (for bitop (bit_and bit_ior)
118 (abs tree_expr_nonnegative_p@0)
122 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
124 For bitwise binary operations apply operand conversions to the
125 binary operation result instead of to the operands. This allows
126 to combine successive conversions and bitwise binary operations.
127 We combine the above two cases by using a conditional convert. */
128 (for bitop (bit_and bit_ior bit_xor)
130 (bitop (convert @0) (convert? @1))
131 (if (((TREE_CODE (@1) == INTEGER_CST
132 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
133 && int_fits_type_p (@1, TREE_TYPE (@0)))
134 || (GIMPLE && types_compatible_p (TREE_TYPE (@0), TREE_TYPE (@1)))
135 || (GENERIC && TREE_TYPE (@0) == TREE_TYPE (@1)))
136 /* ??? This transform conflicts with fold-const.c doing
137 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
138 constants (if x has signed type, the sign bit cannot be set
139 in c). This folds extension into the BIT_AND_EXPR.
140 Restrict it to GIMPLE to avoid endless recursions. */
141 && (bitop != BIT_AND_EXPR || GIMPLE)
142 && (/* That's a good idea if the conversion widens the operand, thus
143 after hoisting the conversion the operation will be narrower. */
144 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
145 /* It's also a good idea if the conversion is to a non-integer
147 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
148 /* Or if the precision of TO is not the same as the precision
150 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
151 (convert (bitop @0 (convert @1))))))
153 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
154 (for bitop (bit_and bit_ior bit_xor)
156 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
157 (bit_and (bitop @0 @2) @1)))
159 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
161 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
162 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
164 /* Combine successive equal operations with constants. */
165 (for bitop (bit_and bit_ior bit_xor)
167 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
168 (bitop @0 (bitop @1 @2))))
170 /* Try simple folding for X op !X, and X op X with the help
171 of the truth_valued_p and logical_inverted_value predicates. */
172 (match truth_valued_p
174 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
175 (for op (lt le eq ne ge gt truth_and truth_andif truth_or truth_orif truth_xor)
176 (match truth_valued_p
178 (match truth_valued_p
181 (match (logical_inverted_value @0)
182 (bit_not truth_valued_p@0))
183 (match (logical_inverted_value @0)
184 (eq @0 integer_zerop)
185 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))))
186 (match (logical_inverted_value @0)
187 (ne truth_valued_p@0 integer_onep)
188 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))))
189 (match (logical_inverted_value @0)
190 (bit_xor truth_valued_p@0 integer_onep))
194 (bit_and:c @0 (logical_inverted_value @0))
195 { build_zero_cst (type); })
196 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
197 (for op (bit_ior bit_xor)
199 (op:c truth_valued_p@0 (logical_inverted_value @0))
200 { build_one_cst (type); }))
202 (for bitop (bit_and bit_ior)
203 rbitop (bit_ior bit_and)
204 /* (x | y) & x -> x */
205 /* (x & y) | x -> x */
207 (bitop:c (rbitop:c @0 @1) @0)
209 /* (~x | y) & x -> x & y */
210 /* (~x & y) | x -> x | y */
212 (bitop:c (rbitop:c (bit_not @0) @1) @0)
215 /* If arg1 and arg2 are booleans (or any single bit type)
216 then try to simplify:
223 But only do this if our result feeds into a comparison as
224 this transformation is not always a win, particularly on
225 targets with and-not instructions.
226 -> simplify_bitwise_binary_boolean */
228 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
229 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
230 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
233 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
234 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
235 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
240 (bit_not (bit_not @0))
244 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
246 (pointer_plus (pointer_plus @0 @1) @3)
247 (pointer_plus @0 (plus @1 @3)))
253 tem4 = (unsigned long) tem3;
258 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
259 /* Conditionally look through a sign-changing conversion. */
260 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
261 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
262 || (GENERIC && type == TREE_TYPE (@1))))
266 tem = (sizetype) ptr;
270 and produce the simpler and easier to analyze with respect to alignment
271 ... = ptr & ~algn; */
273 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
274 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
275 (bit_and @0 { algn; })))
278 /* We can't reassociate at all for saturating types. */
279 (if (!TYPE_SATURATING (type))
281 /* Contract negates. */
282 /* A + (-B) -> A - B */
284 (plus:c (convert1? @0) (convert2? (negate @1)))
285 /* Apply STRIP_NOPS on @0 and the negate. */
286 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
287 && tree_nop_conversion_p (type, TREE_TYPE (@1))
288 && (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
289 (minus (convert @0) (convert @1))))
290 /* A - (-B) -> A + B */
292 (minus (convert1? @0) (convert2? (negate @1)))
293 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
294 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
295 (plus (convert @0) (convert @1))))
298 (negate (convert? (negate @1)))
299 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
300 && (TYPE_OVERFLOW_WRAPS (type)
301 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
304 /* We can't reassociate floating-point or fixed-point plus or minus
305 because of saturation to +-Inf. */
306 (if (!FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
308 /* Match patterns that allow contracting a plus-minus pair
309 irrespective of overflow issues. */
310 /* (A +- B) - A -> +- B */
311 /* (A +- B) -+ B -> A */
312 /* A - (A +- B) -> -+ B */
313 /* A +- (B -+ A) -> +- B */
315 (minus (plus:c @0 @1) @0)
318 (minus (minus @0 @1) @0)
321 (plus:c (minus @0 @1) @1)
324 (minus @0 (plus:c @0 @1))
327 (minus @0 (minus @0 @1))
330 /* (A +- CST) +- CST -> A + CST */
331 (for outer_op (plus minus)
332 (for inner_op (plus minus)
334 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
335 /* If the constant operation overflows we cannot do the transform
336 as we would introduce undefined overflow, for example
337 with (a - 1) + INT_MIN. */
338 (with { tree cst = fold_binary (outer_op == inner_op
339 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
340 (if (cst && !TREE_OVERFLOW (cst))
341 (inner_op @0 { cst; } ))))))
343 /* (CST - A) +- CST -> CST - A */
344 (for outer_op (plus minus)
346 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
347 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
348 (if (cst && !TREE_OVERFLOW (cst))
349 (minus { cst; } @0)))))
353 (plus:c (bit_not @0) @0)
354 (if (!TYPE_OVERFLOW_TRAPS (type))
355 { build_all_ones_cst (type); }))
359 (plus (bit_not @0) integer_each_onep)
362 /* (T)(P + A) - (T)P -> (T) A */
363 (for add (plus pointer_plus)
365 (minus (convert (add @0 @1))
367 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
368 /* For integer types, if A has a smaller type
369 than T the result depends on the possible
371 E.g. T=size_t, A=(unsigned)429497295, P>0.
372 However, if an overflow in P + A would cause
373 undefined behavior, we can assume that there
375 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
376 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
377 /* For pointer types, if the conversion of A to the
378 final type requires a sign- or zero-extension,
379 then we have to punt - it is not defined which
381 || (POINTER_TYPE_P (TREE_TYPE (@0))
382 && TREE_CODE (@1) == INTEGER_CST
383 && tree_int_cst_sign_bit (@1) == 0))
388 /* Simplifications of conversions. */
390 /* Basic strip-useless-type-conversions / strip_nops. */
391 (for cvt (convert view_convert float fix_trunc)
394 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
395 || (GENERIC && type == TREE_TYPE (@0)))
398 /* Contract view-conversions. */
400 (view_convert (view_convert @0))
403 /* For integral conversions with the same precision or pointer
404 conversions use a NOP_EXPR instead. */
407 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
408 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
409 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
412 /* Strip inner integral conversions that do not change precision or size. */
414 (view_convert (convert@0 @1))
415 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
416 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
417 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
418 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
421 /* Re-association barriers around constants and other re-association
422 barriers can be removed. */
424 (paren CONSTANT_CLASS_P@0)
430 /* Handle cases of two conversions in a row. */
431 (for ocvt (convert float fix_trunc)
432 (for icvt (convert float)
437 tree inside_type = TREE_TYPE (@0);
438 tree inter_type = TREE_TYPE (@1);
439 int inside_int = INTEGRAL_TYPE_P (inside_type);
440 int inside_ptr = POINTER_TYPE_P (inside_type);
441 int inside_float = FLOAT_TYPE_P (inside_type);
442 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
443 unsigned int inside_prec = TYPE_PRECISION (inside_type);
444 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
445 int inter_int = INTEGRAL_TYPE_P (inter_type);
446 int inter_ptr = POINTER_TYPE_P (inter_type);
447 int inter_float = FLOAT_TYPE_P (inter_type);
448 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
449 unsigned int inter_prec = TYPE_PRECISION (inter_type);
450 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
451 int final_int = INTEGRAL_TYPE_P (type);
452 int final_ptr = POINTER_TYPE_P (type);
453 int final_float = FLOAT_TYPE_P (type);
454 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
455 unsigned int final_prec = TYPE_PRECISION (type);
456 int final_unsignedp = TYPE_UNSIGNED (type);
458 /* In addition to the cases of two conversions in a row
459 handled below, if we are converting something to its own
460 type via an object of identical or wider precision, neither
461 conversion is needed. */
462 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
464 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
465 && (((inter_int || inter_ptr) && final_int)
466 || (inter_float && final_float))
467 && inter_prec >= final_prec)
470 /* Likewise, if the intermediate and initial types are either both
471 float or both integer, we don't need the middle conversion if the
472 former is wider than the latter and doesn't change the signedness
473 (for integers). Avoid this if the final type is a pointer since
474 then we sometimes need the middle conversion. Likewise if the
475 final type has a precision not equal to the size of its mode. */
476 (if (((inter_int && inside_int)
477 || (inter_float && inside_float)
478 || (inter_vec && inside_vec))
479 && inter_prec >= inside_prec
480 && (inter_float || inter_vec
481 || inter_unsignedp == inside_unsignedp)
482 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
483 && TYPE_MODE (type) == TYPE_MODE (inter_type))
485 && (! final_vec || inter_prec == inside_prec))
488 /* If we have a sign-extension of a zero-extended value, we can
489 replace that by a single zero-extension. Likewise if the
490 final conversion does not change precision we can drop the
491 intermediate conversion. */
492 (if (inside_int && inter_int && final_int
493 && ((inside_prec < inter_prec && inter_prec < final_prec
494 && inside_unsignedp && !inter_unsignedp)
495 || final_prec == inter_prec))
498 /* Two conversions in a row are not needed unless:
499 - some conversion is floating-point (overstrict for now), or
500 - some conversion is a vector (overstrict for now), or
501 - the intermediate type is narrower than both initial and
503 - the intermediate type and innermost type differ in signedness,
504 and the outermost type is wider than the intermediate, or
505 - the initial type is a pointer type and the precisions of the
506 intermediate and final types differ, or
507 - the final type is a pointer type and the precisions of the
508 initial and intermediate types differ. */
509 (if (! inside_float && ! inter_float && ! final_float
510 && ! inside_vec && ! inter_vec && ! final_vec
511 && (inter_prec >= inside_prec || inter_prec >= final_prec)
512 && ! (inside_int && inter_int
513 && inter_unsignedp != inside_unsignedp
514 && inter_prec < final_prec)
515 && ((inter_unsignedp && inter_prec > inside_prec)
516 == (final_unsignedp && final_prec > inter_prec))
517 && ! (inside_ptr && inter_prec != final_prec)
518 && ! (final_ptr && inside_prec != inter_prec)
519 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
520 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
523 /* A truncation to an unsigned type (a zero-extension) should be
524 canonicalized as bitwise and of a mask. */
525 (if (final_int && inter_int && inside_int
526 && final_prec == inside_prec
527 && final_prec > inter_prec
529 (convert (bit_and @0 { wide_int_to_tree
531 wi::mask (inter_prec, false,
532 TYPE_PRECISION (inside_type))); })))
534 /* If we are converting an integer to a floating-point that can
535 represent it exactly and back to an integer, we can skip the
536 floating-point conversion. */
537 (if (inside_int && inter_float && final_int &&
538 (unsigned) significand_size (TYPE_MODE (inter_type))
539 >= inside_prec - !inside_unsignedp)
542 /* If we have a narrowing conversion to an integral type that is fed by a
543 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
544 masks off bits outside the final type (and nothing else). */
546 (convert (bit_and @0 INTEGER_CST@1))
547 (if (INTEGRAL_TYPE_P (type)
548 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
549 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
550 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
551 TYPE_PRECISION (type)), 0))
555 /* (X /[ex] A) * A -> X. */
557 (mult (convert? (exact_div @0 @1)) @1)
558 /* Look through a sign-changing conversion. */
559 (if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))