From patchwork Thu Aug 3 02:31:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 130258 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp863521vqx; Wed, 2 Aug 2023 19:32:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlE9XY40nbECMjvJMstgQ81jdqQDXl5oQsX7JLr0jvorp31pbdC+Lsw73+Zr7CqWfyrAyRun X-Received: by 2002:a05:6402:405:b0:522:4d1b:3acf with SMTP id q5-20020a056402040500b005224d1b3acfmr6045296edv.37.1691029926759; Wed, 02 Aug 2023 19:32:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691029926; cv=none; d=google.com; s=arc-20160816; b=UhrQ5w3Vnpjim7yP+ePfGeL9Stj60ItXdeBeMCeHJqg2TWqfgeOV/4UanuwufQJP6m gxWd7I2oOVoQ2SmpXlltPXCy6BCzoEiEAJCFIB11aA3ad6fh2lOGL9xwq51WgLMNUURb 2O8bKwWp2txZSp0xIxMaXOGa9If7DR+AjnRm6Y6klvO1K9UgLWJaWMCZSKe6FPjhHUnZ Ff5JNOcloE5n1sO2smNDtObITcLfv+H83/M5FhZ6WMSeRxmQVBN/kmuSH1uOQMkQKL4R 2lJed27BDrxmCCA39wTytkBvCGzCi5Kyl1NLvtezLZnMeP9/w5V5tKmnISup6U2qLX9E YT3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=UIThSyef/B0tOhsLDpJOy9hwYGpIkMLBRJW+1YPOxNY=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=FJhda4sBUOT+o4wQsO7pOoLRqQOIR4L9Vcy8rqDY0IoKGtzEqB4lDviShsU+xnrm1M b8jw7YWqzq3zSVAmntBf3oSZAs6fsO4EtNsW2aP4312HsIxBhQ9eHlGK8pa4SVXGyzxR XQK6hr+2tSP+x+XAzH/sRY90cypqjWMGiIX6r52IXFVnMKW4W+ZZw06/nDCmfxIZqyj7 aKXgc3p6UJsag7jlpE7DjbxcBRbLw5crU/8m3jSmvZI9zmHCnaNAYbDIwG77MQ5jydVr 9xZiRlTaWCKumkmQbpRwZjJWwuxx7aoMcG1vhB3HfUMvScTn8Lv4nEx6I4/sgOJ2eD/g PZbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wCOIJhj4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l22-20020aa7c316000000b00522d8e594c1si4623112edq.289.2023.08.02.19.32.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 19:32:06 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wCOIJhj4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B19F385840A for ; Thu, 3 Aug 2023 02:32:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B19F385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691029925; bh=UIThSyef/B0tOhsLDpJOy9hwYGpIkMLBRJW+1YPOxNY=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=wCOIJhj48IVkjRf+OdbFShdu5rSKtB1xExQ3Z6EfKjaufDmGkiYbohZC2TPISCq8T OOdGnwT67g0MvnnlCfw6lG28zvmvjB2QSDcIKlzzanzeUiHn35kJ+KQ4LSjHuWD9s7 47sLdqoIaQ9vy6VOq3zMqT46+dkselM8U7/OnVmw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 1C4273858D1E for ; Thu, 3 Aug 2023 02:31:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1C4273858D1E Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 372KA3Bu026602 for ; Wed, 2 Aug 2023 19:31:22 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3s529keser-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 02 Aug 2023 19:31:22 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 2 Aug 2023 19:31:20 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Wed, 2 Aug 2023 19:31:20 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id DFE985C68E4; Wed, 2 Aug 2023 19:31:19 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] Fix `~X & X` and `~X | X` patterns Date: Wed, 2 Aug 2023 19:31:10 -0700 Message-ID: <20230803023110.2271301-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: -McDMPEz9MNCKzqafxxP4PXQZbtWBk9Y X-Proofpoint-ORIG-GUID: -McDMPEz9MNCKzqafxxP4PXQZbtWBk9Y X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-02_21,2023-08-01_01,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773173396421685100 X-GMAIL-MSGID: 1773173396421685100 As Jakub noticed in https://gcc.gnu.org/pipermail/gcc-patches/2023-August/626039.html what I did was not totally correct because sometimes chosing the wrong type. So to get back to what the original code but keeping around the use of bitwise_inverted_equal_p, we just need to check if the types of the two catupures are the same type. Also adds a testcase for the problem Jakub found. Committed as obvious after a bootstrap and test. gcc/ChangeLog: * match.pd (`~X & X`): Check that the types match. (`~x | x`, `~x ^ x`): Likewise. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/20230802-1.c: New test. --- gcc/match.pd | 6 +- .../gcc.c-torture/execute/20230802-1.c | 68 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/20230802-1.c diff --git a/gcc/match.pd b/gcc/match.pd index c62f205c13c..53e622bf28f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1158,7 +1158,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplify ~X & X as zero. */ (simplify (bit_and (convert? @0) (convert? @1)) - (if (bitwise_inverted_equal_p (@0, @1)) + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) + && bitwise_inverted_equal_p (@0, @1)) { build_zero_cst (type); })) /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ @@ -1397,7 +1398,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (for op (bit_ior bit_xor) (simplify (op (convert? @0) (convert? @1)) - (if (bitwise_inverted_equal_p (@0, @1)) + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) + && bitwise_inverted_equal_p (@0, @1)) (convert { build_all_ones_cst (TREE_TYPE (@0)); })))) /* x ^ x -> 0 */ diff --git a/gcc/testsuite/gcc.c-torture/execute/20230802-1.c b/gcc/testsuite/gcc.c-torture/execute/20230802-1.c new file mode 100644 index 00000000000..8802ffa8238 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/20230802-1.c @@ -0,0 +1,68 @@ +/* We used to simplify these incorrectly. */ +__attribute__((noipa)) +long long +foo (unsigned int x) +{ + int y = x; + y = ~y; + return ((long long) x) & y; +} + +__attribute__((noipa)) +long long +foo_v (volatile unsigned int x) +{ + volatile int y = x; + y = ~y; + return ((long long) x) & y; +} + +__attribute__((noipa)) +long long +bar (unsigned int x) +{ + int y = x; + y = ~y; + return ((long long) x) ^ y; +} + +__attribute__((noipa)) +long long +bar_v (volatile unsigned int x) +{ + volatile int y = x; + y = ~y; + return ((long long) x) ^ y; +} + +__attribute__((noipa)) +long long +baz (unsigned int x) +{ + int y = x; + y = ~y; + return y ^ ((long long) x); +} + +__attribute__((noipa)) +long long +baz_v (volatile unsigned int x) +{ + volatile int y = x; + y = ~y; + return y ^ ((long long) x); +} + + +int main() +{ + for(int t = -1; t <= 1; t++) + { + if (foo(t) != foo_v(t)) + __builtin_abort (); + if (bar(t) != bar_v(t)) + __builtin_abort (); + if (baz(t) != baz_v(t)) + __builtin_abort (); + } +}