public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] match.pd: Fix up FFS -> CTZ + 1 optimization [PR97325]
@ 2023-02-28 10:09 Alex Coplan
  0 siblings, 0 replies; only message in thread
From: Alex Coplan @ 2023-02-28 10:09 UTC (permalink / raw)
  To: gcc-cvs

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

commit ea9d6de2ca1ae1d6931e5e8f55d1bc431920b057
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Oct 9 10:16:57 2020 +0200

    match.pd: Fix up FFS -> CTZ + 1 optimization [PR97325]
    
    And no testcase was included, I'm including one below.
    
    Anyway, this PR and the other CTZ related discussions led me to discover a
    bug I've made earlier, CLZ/CTZ builtins have unsigned arguments and e.g.
    both the vr-values.cc and now gimple-range.cc code heavily relies on that,
    but __builtin_ffs has a signed operand and this optimization was incorrectly
    making the operand signed too, so I guess it would greatly confuse VRP in
    some cases.
    
    2020-10-09  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/97325
            * match.pd (FFS(nonzero) -> CTZ(nonzero) + 1): Cast argument to
            corresponding unsigned type.
    
            * gcc.c-torture/execute/pr97325.c: New test.
    
    (cherry picked from commit 600cf1128e49f086522bfddc54490b4e39e30383)

Diff:
---
 gcc/match.pd                                  |  3 ++-
 gcc/testsuite/gcc.c-torture/execute/pr97325.c | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 07640ffe0a9..734e19fab0a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6211,7 +6211,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
 					 OPTIMIZE_FOR_SPEED))
-  (plus (CTZ:type @0) { build_one_cst (type); })))
+  (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
+   (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
 #endif
 
 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr97325.c b/gcc/testsuite/gcc.c-torture/execute/pr97325.c
new file mode 100644
index 00000000000..f734c85c711
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr97325.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/97325 */
+
+unsigned long long
+foo (unsigned long long c)
+{
+  return c ? __builtin_ffs (-(unsigned short) c) : 0;
+}
+
+int
+main ()
+{
+  if (foo (2) != 2)
+    __builtin_abort ();
+  return 0;
+}

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

only message in thread, other threads:[~2023-02-28 10:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 10:09 [gcc(refs/vendors/ARM/heads/morello)] match.pd: Fix up FFS -> CTZ + 1 optimization [PR97325] Alex Coplan

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).