From patchwork Mon Nov 15 13:39:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1555293 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=h40lZYco; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Ht9L24Fx9z9sR4 for ; Tue, 16 Nov 2021 00:40:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C1363857C44 for ; Mon, 15 Nov 2021 13:40:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C1363857C44 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636983607; bh=Bms09/3g8vCM45efrfdvBMECl6HGorv/nofAROpN7UQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=h40lZYcoNG1F9dEd7OzaMAmkzx+sPdcbErc7HfZzcpfsppht+Ei2uuIWNIJErHP6D yUxw2f94YG0fPTm5bLy7/RNcK7ORLAOtPE7Rcwuht6tViKBDQ+ARGFSWXwWGnK70g7 inERfu9eF8WphRiI+RDARJg6rwmY5Zi1CT0p0Wrk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 478673858D39 for ; Mon, 15 Nov 2021 13:39:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 478673858D39 Received: by mail-pl1-x635.google.com with SMTP id t21so14478193plr.6 for ; Mon, 15 Nov 2021 05:39:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=Bms09/3g8vCM45efrfdvBMECl6HGorv/nofAROpN7UQ=; b=mtLgxyQ/rWXZzHsw4/1mirKcUqV7Eco56Rje/MRNWCVmp3DOjLHZLXcFYlxgJMzUig KLGTG+lU9vp4AjjlJP8c7lUIGXYUxJwix1q7/tXMrwik5e3HT8hJEz4UfyHINRqwH5Re X6JABUzL1UDA25c0X4MUJXnJiPv8rjfnIHQDcSfjk2I4WhuL9X/EbMTi9efswF5zX1tP IAGzXNNltpWnN5Cwb8aTiO3ow+QBW517xTGGxcssZBvigg7rVbdTSwQjo9ayNGTU44ea bTZYJ604xoKrx2adiAwFgZrQxl7CBW/eTskWTGafNVsC8XaE1jYn29sFgyhaqDfVjIIp BWwQ== X-Gm-Message-State: AOAM53062nTp9izi8iYAxfgUgAa1T5Et8wnkowTEg7G6i73IMKRqJWNR N6rTolMMoMKWGmvyMaV8tEQ/rUMNxhw= X-Google-Smtp-Source: ABdhPJzDGoLUamkqBu+1OE+tAsAsLLJhF64TLSQblhFsRIaL5W2PdwiPCZ5XEuNHOmxo8Un2K1PuFg== X-Received: by 2002:a17:90b:4c44:: with SMTP id np4mr46151448pjb.195.1636983585023; Mon, 15 Nov 2021 05:39:45 -0800 (PST) Received: from gnu-cfl-2.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id k16sm17416654pfu.183.2021.11.15.05.39.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Nov 2021 05:39:44 -0800 (PST) Received: from gnu-cfl-2.lan (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id B011E1A0173 for ; Mon, 15 Nov 2021 05:39:43 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Check optab before transforming atomic bit test and operations Date: Mon, 15 Nov 2021 05:39:43 -0800 Message-Id: <20211115133943.440104-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3029.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. --- gcc/testsuite/gcc.dg/pr103184-1.c | 43 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr103184-2.c | 12 +++++++++ gcc/tree-ssa-ccp.c | 34 +++++++++++++----------- 3 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr103184-1.c create mode 100644 gcc/testsuite/gcc.dg/pr103184-2.c 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 + +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..fec68b5fc73 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; @@ -3628,21 +3647,6 @@ optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip, } } - 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) return;