From patchwork Mon Aug 7 05:06:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 131666 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1232459vqr; Sun, 6 Aug 2023 22:07:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG6JeGa84YW0DrVT6HdCFuKfKW2l7im8NFUwOvQVx1qmGmIrMvyRS/xfiBFodpwfI/k1j+W X-Received: by 2002:a17:906:5388:b0:99b:e243:9ecc with SMTP id g8-20020a170906538800b0099be2439eccmr7910618ejo.36.1691384859622; Sun, 06 Aug 2023 22:07:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691384859; cv=none; d=google.com; s=arc-20160816; b=HGnfXFkzyVhOgA8WnxVfqAiufv2anzaL5I4WKMa2hiAwHnv9wXP1Hv9LThb73C9X8R +0mqbXt4Dk7jNhW4tcc8UagU6eiqRQ8Ellu0msCObMjU4naPnCAEvOdJU6hg3uR2ifWH 5HKHQv7eklfyK4XDVhD2Irc/uBS72+1zPNEzzUbNb9/9aQZXnKEYeCHWeCXE9M2f+xwR 2cSJWZ2xHKAO6nsQs7xClLmZO07jSlOlqoPlf4Gv/RSq/tqpCYdAtJl3EquT7RsQc1W2 1YZG5bCdGqo1iNNTDyzQ3y409baFBN3U33ZVfzYsBlASdHYaN4PfjgWOrsASCAoG0fq6 H1hQ== 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=4WeA7zkHqf7eHXSy85VvUByftvQf1KEc25asFGhnmQ4=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=ULKfcBceMLoiAtAgRckdqA56HQKslYBtoF9G5EcZf9+Q/rcPUi9TG5wNL0RkQy2D+A ZXkU3n6xQx8T4hr6XpDAT5iGbyWn/NPurv0i/JYvnwEYlhF80lK8oIMvLSekCkQDayTV 7Uqn09haC5gzj3hUif3yPpZTkpcB8HWd28Uqbao3Edqg8gZSS5mx9tc/4mQ1UoS2bqBH xxqzzgwQfAICdPUuteUBs4sfVwnAZJt26y3pJiGRHeHV73AcwmqulRMAP0tDO2BO9F7Z ytovv4GnBrbmrxiJbVaU2R8ucltk2H4SAARr2VmUkqOUndiL45Msve0jn5JizJwhGR3Y aiHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="x69/UuBD"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s27-20020a1709060c1b00b0099b6e6c095csi5527747ejf.698.2023.08.06.22.07.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Aug 2023 22:07:39 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="x69/UuBD"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 8BB033858407 for ; Mon, 7 Aug 2023 05:07:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BB033858407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691384858; bh=4WeA7zkHqf7eHXSy85VvUByftvQf1KEc25asFGhnmQ4=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=x69/UuBDGF8RIBHawpTe3GYo2aTUpZEn87sB3k9vKGVheGYWrPJuH4BGuQAk9PadF NGwmQw5UwExSf0fz/UaKVyB8YBjKlJG2GA15N+kKAYGHQ16/X2RkVb+R5vN0W0O9Ji lkQYTkwQtV1447hNrZ4uk4j22bY6CUCOdrwqnydc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 7C69C3858D28 for ; Mon, 7 Aug 2023 05:06:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C69C3858D28 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 376MhGRI024834 for ; Sun, 6 Aug 2023 22:06:54 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3s9kssc68m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 06 Aug 2023 22:06:53 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 6 Aug 2023 22:06:52 -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; Sun, 6 Aug 2023 22:06:52 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id CE0673F704B; Sun, 6 Aug 2023 22:06:51 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: [PR109959] `(uns <= 1) & uns` could be optimized to `uns == 1` Date: Sun, 6 Aug 2023 22:06:31 -0700 Message-ID: <20230807050631.2514046-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: qIVc1ZupzGvOrnQpD9iQUlHPrE4aHTZS X-Proofpoint-ORIG-GUID: qIVc1ZupzGvOrnQpD9iQUlHPrE4aHTZS X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-07_03,2023-08-03_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.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: 1773545570426184624 X-GMAIL-MSGID: 1773545570426184624 I noticed while looking into some code generation of bitmap_single_bit_set_p, that sometimes: ``` if (uns > 1) return 0; return uns == 1; ``` Would not optimize down to just: ``` return uns == 1; ``` In this case, VRP likes to change `a == 1` into `(bool)a` if a has a range of [0,1] due to `a <= 1` side of the branch. We might end up with this similar code even without VRP, in the case of builtin-sprintf-warn-23.c (and Wrestrict.c), we had: ``` if (s < 0 || 1 < s) s = 0; ``` Which is the same as `s = ((unsigned)s) <= 1 ? s : 0`; So we should be able to catch that also. This adds 2 patterns to catch `(uns <= 1) & uns` and `(uns > 1) ? 0 : uns` and convert those into: `(convert) uns == 1`. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/109959 gcc/ChangeLog: * match.pd (`(a > 1) ? 0 : (cast)a`, `(a <= 1) & (cast)a`): New patterns. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/builtin-sprintf-warn-23.c: Remove xfail. * c-c++-common/Wrestrict.c: Update test and remove some xfail. * gcc.dg/tree-ssa/cmpeq-1.c: New test. * gcc.dg/tree-ssa/cmpeq-2.c: New test. * gcc.dg/tree-ssa/cmpeq-3.c: New test. --- gcc/match.pd | 20 +++++++++++ gcc/testsuite/c-c++-common/Wrestrict.c | 11 +++--- .../gcc.dg/tree-ssa/builtin-sprintf-warn-23.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c | 36 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c | 32 +++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c | 22 ++++++++++++ 6 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c diff --git a/gcc/match.pd b/gcc/match.pd index de54b17abba..9b4819e5be7 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4902,6 +4902,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ) ) +/* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1) + for unsigned types. */ +(simplify + (cond (gt @0 integer_onep@1) integer_zerop (convert? @2)) + (if (TYPE_UNSIGNED (TREE_TYPE (@0)) + && bitwise_equal_p (@0, @2)) + (convert (eq @0 @1)) + ) +) + +/* (a <= 1) & (cast)a is the same as (cast)(a == 1) + for unsigned types. */ +(simplify + (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2)) + (if (TYPE_UNSIGNED (TREE_TYPE (@0)) + && bitwise_equal_p (@0, @2)) + (convert (eq @0 @1)) + ) +) + (simplify (cond @0 zero_one_valued_p@1 zero_one_valued_p@2) (switch diff --git a/gcc/testsuite/c-c++-common/Wrestrict.c b/gcc/testsuite/c-c++-common/Wrestrict.c index 9eb02bdbfcb..4d005a618b3 100644 --- a/gcc/testsuite/c-c++-common/Wrestrict.c +++ b/gcc/testsuite/c-c++-common/Wrestrict.c @@ -681,7 +681,7 @@ void test_strcpy_range (void) ptrdiff_t r; r = SR (0, 1); - T (8, "0", a + r, a); /* { dg-warning "accessing between 1 and 2 bytes at offsets \\\[0, 1] and 0 overlaps up to 2 bytes at offset \\\[0, 1]" "strcpy" { xfail *-*-*} } */ + T (8, "0", a + r, a); /* { dg-warning "accessing 2 bytes at offsets \\\[0, 1] and 0 overlaps between 1 and 2 bytes at offset \\\[0, 1]" "strcpy" } */ r = SR (2, 5); T (8, "01", a + r, a); /* { dg-warning "accessing 3 bytes at offsets \\\[2, 5] and 0 may overlap 1 byte at offset 2" } */ @@ -860,10 +860,11 @@ void test_strncpy_range (char *d, size_t n) i = SR (0, 1); T ("0123", a, a + i, 0); - T ("0123", a, a + i, 1); - /* Offset in the range [0, i] is represented as a PHI (&a, &a + i) - that the implementation isn't equipped to handle yet. */ - T ("0123", a, a + i, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and \\\[0, 1] may overlap 1 byte at offset 1" "strncpy" { xfail *-*-* } } */ + T ("0123", a, a + i, 1); /* { dg-warning "accessing 1 byte at offsets 0 and \\\[0, 1] may overlap 1 byte at offset 0" } */ + /* When i == 1 the following overlaps at least 1 byte: the nul at a[1] + (if a + 1 is the empty string). If a + 1 is not empty then it overlaps + it plus as many non-nul characters after it, up to the total of 2. */ + T ("0123", a, a + i, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and \\\[0, 1] overlaps between 1 and 2 bytes at offset \\\[0, 1]" "strncpy" } */ i = SR (1, 5); T ("0123", a, a + i, 0); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c index 112b08afc44..051c58892e6 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c @@ -719,5 +719,5 @@ void test_overlap_with_precision (char *d, int i, int j) T (d, "%.*s", i, d + 0); /* { dg-warning "may overlap" } */ T (d, "%.*s", i, d + 1); /* { dg-warning "may overlap" } */ T (d, "%.*s", i, d + 2); - T (d, "%.*s", i, d + i); /* { dg-warning "may overlap" "" { xfail *-*-* } } */ + T (d, "%.*s", i, d + i); /* { dg-warning "may overlap" } */ } diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c new file mode 100644 index 00000000000..1d5b960b663 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c @@ -0,0 +1,36 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/109959 */ + +unsigned fu(unsigned a) +{ + _Bool t = a <= 1; + return t & a; +} + +_Bool fb(unsigned a) +{ + _Bool t = a <= 1; + return t & a; +} + +_Bool fb1(unsigned a) +{ + _Bool t = a <= 1; + _Bool t1 = a; + return t & t1; +} + +signed fui(unsigned a) +{ + _Bool t = a <= 1; + int ai = a; + return t & ai; +} + +/* These all should be optimized to `a == 1` */ +/* { dg-final { scan-tree-dump-times "eq_expr," 4 "optimized"} } */ +/* { dg-final { scan-tree-dump-not "le_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized"} } */ + + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c new file mode 100644 index 00000000000..c727b9e7143 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/109959 */ + +unsigned f(unsigned a) +{ + if (a <= 1) + return a; + return 0; +} + +unsigned f0(unsigned a) +{ + if (a > 1) + return 0; + return a; +} + +_Bool fb(unsigned a) +{ + if (a > 1) + return 0; + return a == 1; +} + +/* These all should be optimized to `a == 1` */ +/* { dg-final { scan-tree-dump-times "eq_expr," 3 "optimized"} } */ +/* { dg-final { scan-tree-dump-not "le_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_and," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized"} } */ + + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c new file mode 100644 index 00000000000..f2546b8813f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/109959 */ + + +_Bool f2(unsigned a, int t) +{ + void g(void); + if (t) + return 0; + g(); + if (a > 1) + return 0; + return a == 1; +} + +/* These all should be optimized to `a == 1` */ +/* { dg-final { scan-tree-dump-times "eq_expr," 1 "optimized"} } */ +/* { dg-final { scan-tree-dump-not "le_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized"} } */ + +