From patchwork Sat Sep 16 15:59:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 141076 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp1731882vqi; Sat, 16 Sep 2023 09:00:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFq3m0+Kd10mnXBdqgDPdijzd45o6qeH4d/WYe5RT1v6wZZ5wuLvNEAhtEAzaAEY4ajhxCe X-Received: by 2002:a17:906:74d9:b0:9a1:f928:dddc with SMTP id z25-20020a17090674d900b009a1f928dddcmr4309584ejl.41.1694880028047; Sat, 16 Sep 2023 09:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694880028; cv=none; d=google.com; s=arc-20160816; b=mGWMAIocMNyLcVec/Da9QMA9ppvbomoNkS02ErWShtgzTrOP3NeTEPoWK02GZz6ZyX YhjTqDGPJgCRCiqZQc7A7TQxfmmtWq1ij18GdntX83IdhNYRq44M9UrG7NAlzs2yCbaJ Beffet8txlaxXDr1MYFIGfzz1LZKmJyKge9VCbhWmJ8VU8M/8q71cjLevq8PObUOoHD0 mguOL/1Ya1xDp5V0LcQiH+4ZZUWNADXuibU2/SUNN9d5ddLEridI6DLE0vbZeWCNSEpL S/UyA26JHRfxRdaWakyo5/nkYM1im8Fa1wwFjkcnsJulqRPlJm/QslonIUkVK1lkK4IB ffrA== 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=otgIUNfuJQQXkCTGIQgDosrFWLR2QrIeNiUR65duseM=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=p1Gqam/c+0U5pp9pZkNk8/9b2vhMtEXM4/LzmJaHUdyAR8oLJa4pN1D3pJDUHfzDIn g/Lr0W21Z0jGDgok7Ic7J8I+N8VO8sCtXSF+iKGManFRVje0rRUxY/RAAVuo/Ia5mogS DRWNTdwy1vKLlT5P1CPI9H8bjklYs5KWE03jQ4p5qLoSN2PWZNv4AEdjMsanmdbDLBMi nd6Pt7E25WDYIIdP/rhfuuNCa97dyORvJMCpjVjUJamIBDgoQioN+fpSypZDS63U3HtY TQ+FUbmAA2P9xuzag0wm4n6ZRslifpetZFsSQuXan9ztGXCTs8wD4SWLynqGOwdNDhSK H7Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gIxiBFAN; 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 gl22-20020a170906e0d600b009ad87fd04dasi5229578ejb.105.2023.09.16.09.00.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Sep 2023 09:00:28 -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=gIxiBFAN; 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 264DC3858D37 for ; Sat, 16 Sep 2023 16:00:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 264DC3858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694880025; bh=otgIUNfuJQQXkCTGIQgDosrFWLR2QrIeNiUR65duseM=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=gIxiBFANxFGZyhCow3juqxGgNhzSQxZDxysHhhJg0pyGEB32v6SoWJa+oBR6bQL0r JunsuVb2cjJ6gKnXIFN1Va6TEuLxPMjmejDJFHws4c9ocNrWrOZqddhOycaQ/6yODG sLocEskYaYoZDslzuexgcyzQSOnUpQsUxOQ5bwFg= 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 AE2FD3858D28 for ; Sat, 16 Sep 2023 15:59:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE2FD3858D28 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 38GBrT6b026934 for ; Sat, 16 Sep 2023 08:59:40 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3t5bvkga4c-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sat, 16 Sep 2023 08:59:39 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sat, 16 Sep 2023 08:59:35 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Sat, 16 Sep 2023 08:59:35 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 818073F7040; Sat, 16 Sep 2023 08:59:35 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Add simplifications of `(a == CST) & a` Date: Sat, 16 Sep 2023 08:59:24 -0700 Message-ID: <20230916155924.1679335-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: HMDj5S3gzOkblPed0MzieHKmd7qhNjye X-Proofpoint-ORIG-GUID: HMDj5S3gzOkblPed0MzieHKmd7qhNjye X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-15_20,2023-09-15_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, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 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: 1777210520386116781 X-GMAIL-MSGID: 1777210520386116781 `(a == CST) & a` can be either simplified to simplying `a == CST` or 0 depending on the first bit of the CST. This is an extension of the already pattern of `X & !X` and allows us to remove the 2 xfails on gcc.dg/binop-notand1a.c and gcc.dg/binop-notand4a.c. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111431 gcc/ChangeLog: * match.pd (`(a == CST) & a`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/binop-notand1a.c: Remove xfail. * gcc.dg/binop-notand4a.c: Likewise. * gcc.c-torture/execute/pr111431-1.c: New test. * gcc.dg/binop-andeq1.c: New test. * gcc.dg/binop-andeq2.c: New test. * gcc.dg/binop-notand7.c: New test. * gcc.dg/binop-notand7a.c: New test. --- gcc/match.pd | 8 ++++ .../gcc.c-torture/execute/pr111431-1.c | 39 +++++++++++++++++++ gcc/testsuite/gcc.dg/binop-andeq1.c | 12 ++++++ gcc/testsuite/gcc.dg/binop-andeq2.c | 14 +++++++ gcc/testsuite/gcc.dg/binop-notand1a.c | 4 +- gcc/testsuite/gcc.dg/binop-notand4a.c | 4 +- gcc/testsuite/gcc.dg/binop-notand7.c | 12 ++++++ gcc/testsuite/gcc.dg/binop-notand7a.c | 12 ++++++ 8 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111431-1.c create mode 100644 gcc/testsuite/gcc.dg/binop-andeq1.c create mode 100644 gcc/testsuite/gcc.dg/binop-andeq2.c create mode 100644 gcc/testsuite/gcc.dg/binop-notand7.c create mode 100644 gcc/testsuite/gcc.dg/binop-notand7a.c diff --git a/gcc/match.pd b/gcc/match.pd index ebb50ee0581..65960a1701e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5172,6 +5172,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ) ) +/* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending + on the first bit of the CST. */ +(simplify + (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0)) + (if ((wi::to_wide (@1) & 1) != 0) + @2 + { build_zero_cst (type); })) + /* Optimize # x_5 in range [cst1, cst2] where cst2 = cst1 + 1 x_5 ? cstN ? cst4 : cst3 diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c new file mode 100644 index 00000000000..a96dbadf2b5 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c @@ -0,0 +1,39 @@ +int +foo (int a) +{ + int b = a == 0; + return (a & b); +} + +#define function(vol,cst) \ +__attribute__((noipa)) \ +_Bool func_##cst##_##vol(vol int a) \ +{ \ + vol int b = a == cst; \ + return (a & b); \ +} + +#define funcdefs(cst) \ +function(,cst) \ +function(volatile,cst) + +#define funcs(f) \ +f(0) \ +f(1) \ +f(5) + +funcs(funcdefs) + +#define test(cst) \ +do { \ + if(func_##cst##_(a) != func_##cst##_volatile(a))\ + __builtin_abort(); \ +} while(0); +int main(void) +{ + for(int a = -10; a <= 10; a++) + { + funcs(test) + } +} + diff --git a/gcc/testsuite/gcc.dg/binop-andeq1.c b/gcc/testsuite/gcc.dg/binop-andeq1.c new file mode 100644 index 00000000000..2a92b8f95df --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-andeq1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/111431 */ + +int +foo (int a) +{ + int b = a == 2; + return (a & b); +} + +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-andeq2.c b/gcc/testsuite/gcc.dg/binop-andeq2.c new file mode 100644 index 00000000000..895262fc17e --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-andeq2.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/111431 */ + +int +foo (int a) +{ + int b = a == 1025; + return (a & b); +} + +/* { dg-final { scan-tree-dump-not "return 0" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ +/* { dg-final { scan-tree-dump-times " == 1025;" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand1a.c b/gcc/testsuite/gcc.dg/binop-notand1a.c index c7e932b2638..d94685eb4ce 100644 --- a/gcc/testsuite/gcc.dg/binop-notand1a.c +++ b/gcc/testsuite/gcc.dg/binop-notand1a.c @@ -7,6 +7,4 @@ foo (char a, unsigned short b) return (a & !a) | (b & !b); } -/* As long as comparisons aren't boolified and casts from boolean-types - aren't preserved, the folding of X & !X to zero fails. */ -/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand4a.c b/gcc/testsuite/gcc.dg/binop-notand4a.c index dce6a5c7eb5..bd9c7cce638 100644 --- a/gcc/testsuite/gcc.dg/binop-notand4a.c +++ b/gcc/testsuite/gcc.dg/binop-notand4a.c @@ -7,6 +7,4 @@ foo (unsigned char a, _Bool b) return (!a & a) | (b & !b); } -/* As long as comparisons aren't boolified and casts from boolean-types - aren't preserved, the folding of X & !X to zero fails. */ -/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand7.c b/gcc/testsuite/gcc.dg/binop-notand7.c new file mode 100644 index 00000000000..c2bb6a0449d --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-notand7.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/111431 */ + +int +foo (int a) +{ + int b = !a; + return (a & b); +} + +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand7a.c b/gcc/testsuite/gcc.dg/binop-notand7a.c new file mode 100644 index 00000000000..dd50916da61 --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-notand7a.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/111431 */ + +unsigned +foo (int a) +{ + int b = !a; + return (a & b); +} + +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */