public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-5282] Check optab before transforming atomic bit test and operations
@ 2021-11-15 20:59 H.J. Lu
  0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2021-11-15 20:59 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4c19122bf5afa5cb479fd9445f0c591c52add09b

commit r12-5282-g4c19122bf5afa5cb479fd9445f0c591c52add09b
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Nov 12 07:21:43 2021 -0800

    Check optab before transforming atomic bit test and operations
    
    Check optab before transforming equivalent, but slighly different cases
    of atomic bit test and operations to their canonical forms.
    
    gcc/
    
            PR middle-end/103184
            * tree-ssa-ccp.c (optimize_atomic_bit_test_and): Check optab
            before transforming equivalent, but slighly different cases to
            their canonical forms.
    
    gcc/testsuite/
    
            PR middle-end/103184
            * gcc.dg/pr103184-1.c: New test.
            * gcc.dg/pr103184-2.c: Likewise.

Diff:
---
 gcc/testsuite/gcc.dg/pr103184-1.c | 43 +++++++++++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr103184-2.c | 12 +++++++++++
 gcc/tree-ssa-ccp.c                | 38 ++++++++++++++++++----------------
 3 files changed, 76 insertions(+), 17 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/pr103184-1.c b/gcc/testsuite/gcc.dg/pr103184-1.c
new file mode 100644
index 00000000000..e567f95f63f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr103184-1.c
@@ -0,0 +1,43 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+extern char foo;
+extern unsigned char bar;
+
+int
+foo1 (void)
+{
+  return __sync_fetch_and_and (&foo, ~1) & 1;
+}
+
+int
+foo2 (void)
+{
+  return __sync_fetch_and_or (&foo, 1) & 1;
+}
+
+int
+foo3 (void)
+{
+  return __sync_fetch_and_xor (&foo, 1) & 1;
+}
+
+unsigned short
+bar1 (void)
+{
+  return __sync_fetch_and_and (&bar, ~1) & 1;
+}
+
+unsigned short
+bar2 (void)
+{
+  return __sync_fetch_and_or (&bar, 1) & 1;
+}
+
+unsigned short
+bar3 (void)
+{
+  return __sync_fetch_and_xor (&bar, 1) & 1;
+}
+
+/* { dg-final { scan-assembler-times "lock;?\[ \t\]*cmpxchgb" 6 { target { x86_64-*-* i?86-*-* } } } } */
diff --git a/gcc/testsuite/gcc.dg/pr103184-2.c b/gcc/testsuite/gcc.dg/pr103184-2.c
new file mode 100644
index 00000000000..499761fdbfd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr103184-2.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+#include <stdatomic.h>
+
+int
+tbit0 (_Atomic int* a, int n)
+{
+#define BIT (0x1 << n)
+  return atomic_fetch_or (a, BIT) & BIT;
+#undef BIT
+}
diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c
index 0f79e9f05bd..0666dc652d0 100644
--- a/gcc/tree-ssa-ccp.c
+++ b/gcc/tree-ssa-ccp.c
@@ -3366,6 +3366,21 @@ optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip,
       || !gimple_vdef (call))
     return;
 
+  switch (fn)
+    {
+    case IFN_ATOMIC_BIT_TEST_AND_SET:
+      optab = atomic_bit_test_and_set_optab;
+      break;
+    case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
+      optab = atomic_bit_test_and_complement_optab;
+      break;
+    case IFN_ATOMIC_BIT_TEST_AND_RESET:
+      optab = atomic_bit_test_and_reset_optab;
+      break;
+    default:
+      return;
+    }
+
   tree bit = nullptr;
 
   mask = gimple_call_arg (call, 1);
@@ -3384,6 +3399,10 @@ optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip,
       if (lhs != use_rhs)
 	return;
 
+      if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
+	  == CODE_FOR_nothing)
+	return;
+
       gimple *g;
       gimple_stmt_iterator gsi;
       tree var;
@@ -3627,23 +3646,8 @@ optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip,
 	  bit = build_int_cst (TREE_TYPE (lhs), ibit);
 	}
     }
-
-  switch (fn)
-    {
-    case IFN_ATOMIC_BIT_TEST_AND_SET:
-      optab = atomic_bit_test_and_set_optab;
-      break;
-    case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
-      optab = atomic_bit_test_and_complement_optab;
-      break;
-    case IFN_ATOMIC_BIT_TEST_AND_RESET:
-      optab = atomic_bit_test_and_reset_optab;
-      break;
-    default:
-      return;
-    }
-
-  if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs))) == CODE_FOR_nothing)
+  else if (optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)))
+	   == CODE_FOR_nothing)
     return;
 
   tree use_lhs = gimple_assign_lhs (use_stmt);


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

only message in thread, other threads:[~2021-11-15 20:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-15 20:59 [gcc r12-5282] Check optab before transforming atomic bit test and operations H.J. Lu

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