public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] tree-optimization/106984 - tsan and COND_EXPR GIMPLE
Date: Wed, 21 Sep 2022 10:43:45 +0000 (UTC)	[thread overview]
Message-ID: <20220921104345.V19hPQ1H0jcgInBe2-rNox6dPzpaB1J19LLngd5vG-E@z> (raw)

The following adjusts a missed spot in TSAN for the RHS COND_EXPR
GIMPLE IL rework.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	PR tree-optimization/106984
	* tsan.cc (instrument_builtin_call): Build the COND_EXPR condition in
	a separate statement.

	* gcc.dg/tsan/pr106984.c: New testcase.
---
 gcc/testsuite/gcc.dg/tsan/pr106984.c |  7 +++++++
 gcc/tsan.cc                          | 13 +++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tsan/pr106984.c

diff --git a/gcc/testsuite/gcc.dg/tsan/pr106984.c b/gcc/testsuite/gcc.dg/tsan/pr106984.c
new file mode 100644
index 00000000000..69cf83d547a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tsan/pr106984.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=thread" } */
+
+int svcsw (int *ptr, int oldval, int newval)
+{
+  return __sync_val_compare_and_swap (ptr, oldval, newval);
+}
diff --git a/gcc/tsan.cc b/gcc/tsan.cc
index 79d4582acd1..2406527c96a 100644
--- a/gcc/tsan.cc
+++ b/gcc/tsan.cc
@@ -620,15 +620,16 @@ instrument_builtin_call (gimple_stmt_iterator *gsi)
 	    maybe_clean_or_replace_eh_stmt (stmt, gsi_stmt (*gsi));
 	    if (tsan_atomic_table[i].action == val_cas && lhs)
 	      {
-		tree cond;
 		stmt = gsi_stmt (*gsi);
-		g = gimple_build_assign (make_ssa_name (TREE_TYPE (t)), t);
+		tree t2 = make_ssa_name (TREE_TYPE (t));
+		g = gimple_build_assign (t2, t);
 		gsi_insert_after (gsi, g, GSI_NEW_STMT);
 		t = make_ssa_name (TREE_TYPE (TREE_TYPE (decl)), stmt);
-		cond = build2 (NE_EXPR, boolean_type_node, t,
-			       build_int_cst (TREE_TYPE (t), 0));
-		g = gimple_build_assign (lhs, COND_EXPR, cond, args[1],
-					 gimple_assign_lhs (g));
+		tree cond = make_ssa_name (boolean_type_node);
+		g = gimple_build_assign (cond, NE_EXPR,
+					 t, build_zero_cst (TREE_TYPE (t)));
+		gsi_insert_after (gsi, g, GSI_NEW_STMT);
+		g = gimple_build_assign (lhs, COND_EXPR, cond, args[1], t2);
 		gimple_call_set_lhs (stmt, t);
 		update_stmt (stmt);
 		gsi_insert_after (gsi, g, GSI_NEW_STMT);
-- 
2.35.3

                 reply	other threads:[~2022-09-21 10:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220921104345.V19hPQ1H0jcgInBe2-rNox6dPzpaB1J19LLngd5vG-E@z \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).