public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-917] match.pd: Optimize __builtin_mul_overflow_p (x, cst, (utype)0) to x > ~(utype)0 / cst [PR30314]
@ 2022-06-01 15:55 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-06-01 15:55 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:cf78d8411d00e21c30512d2af895e70d38bbfb77

commit r13-917-gcf78d8411d00e21c30512d2af895e70d38bbfb77
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jun 1 17:54:39 2022 +0200

    match.pd: Optimize __builtin_mul_overflow_p (x, cst, (utype)0) to x > ~(utype)0 / cst [PR30314]
    
    A comparison with a constant is most likely always faster than
    .MUL_OVERFLOW from which we only check whether it overflowed and not the
    multiplication result, and even if not, it is simpler operation on GIMPLE
    and even if a target exists where such multiplications with overflow checking
    are cheaper than comparisons, because comparisons are so much more common
    than overflow checking multiplications, it would be nice if it simply
    arranged for comparisons to be emitted like those multiplications on its
    own...
    
    2022-06-01  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/30314
            * match.pd (__builtin_mul_overflow_p (x, cst, (utype) 0) ->
            x > ~(utype)0 / cst): New simplification.
    
            * gcc.dg/tree-ssa/pr30314.c: New test.

Diff:
---
 gcc/match.pd                            | 11 +++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr30314.c | 18 ++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 88c6c414881..2d3ffc4482e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5969,6 +5969,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
    (ovf @1 @0))))
 
+/* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
+   are unsigned to x > (umax / cst).  */
+(simplify
+ (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+       && TYPE_UNSIGNED (TREE_TYPE (@0))
+       && TYPE_MAX_VALUE (TREE_TYPE (@0))
+       && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
+       && int_fits_type_p (@1, TREE_TYPE (@0)))
+   (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))))
+
 /* Simplification of math builtins.  These rules must all be optimizations
    as well as IL simplifications.  If there is a possibility that the new
    form could be a pessimization, the rule should go in the canonicalization
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr30314.c b/gcc/testsuite/gcc.dg/tree-ssa/pr30314.c
new file mode 100644
index 00000000000..91388afde22
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr30314.c
@@ -0,0 +1,18 @@
+/* PR middle-end/30314 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-not "\.MUL_OVERFLOW " "optimized" } } */
+/* { dg-final { scan-tree-dump " > 122713351" "optimized" { target int32 } } } */
+/* { dg-final { scan-tree-dump " > 527049830677415760" "optimized" { target lp64 } } } */
+
+int
+foo (unsigned int x)
+{
+  return __builtin_mul_overflow_p (x, 35U, 0U);
+}
+
+int
+bar (unsigned long int x)
+{
+  return __builtin_mul_overflow_p (x, 35UL, 0UL);
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-01 15:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-01 15:55 [gcc r13-917] match.pd: Optimize __builtin_mul_overflow_p (x, cst, (utype)0) to x > ~(utype)0 / cst [PR30314] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).