From patchwork Wed Feb 1 01:21:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 51121 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp20466wrn; Tue, 31 Jan 2023 17:36:47 -0800 (PST) X-Google-Smtp-Source: AK7set/5AonNJyB3Te5hSijZBo1Ke/vynoAxlhQGNhSi8TfMV1qVip2qJ2ePPtwHKgV+FkuAqC0S X-Received: by 2002:aa7:c444:0:b0:493:a6eb:874e with SMTP id n4-20020aa7c444000000b00493a6eb874emr204521edr.5.1675215407337; Tue, 31 Jan 2023 17:36:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675215407; cv=none; d=google.com; s=arc-20160816; b=E9NmSObdz6aBLqZOmz+WI1wvwoEWajp+1MDPQ/50XH1ihZBp9Q7P5+XSxrSOXDTe0c sOtmemUE6NffIIkgoWFQFLa1iGt45j4mjMzHAupx1OZ2p0cV3K1itxlEre5/BUaOgZjh 5FdG/4B/1xtRcoMv+6bIIabPkLxe2IjWEgqBMAD6jGoO5/hqysejiFlfbRXVFU2loay0 vyxaFbmtaGPqSXDCrBhdVJz9cx0gLT8L3snwqCGUUYV0UQXOOzrwFgYo/Zu4kHRIK8Nb bgWw3uoGRCGYpOcrU9aWnzuaNzGwbBz6fghc1yJLLY3JrpIxMP4Fb6SG2OYpPU0G6lUO pTHg== 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=EowG+y/j43BG3mTqQhtvxtXLs8vA/VlF1GTd4NbB0bM=; b=R9YvvDRcKMJaxoiPyqgbYPZG3s0XAz6oqYGI0dQVIicem0OiVsR+Qr9A6oAaN7h4yw 4RfIrJBJboHH/cjJDVuwEAEBkLDM4XR8py2YkQZbscI2p2Cvph8uXFFCD6+wjkeNlwNK 5Gass3/agEvXoOncrc8pdVIilnUv4BF+aqL+9iZKKPc0PZClKpy5S0jY/uqmp7hmEyRw lr87plkYpcta10FYARn0LZ4h0g8X4gyP+9yBYdnZKmnZeTyJAKbnWsmYA/+t6aPPtXQ9 4OI88AYYWSl6WS7zUezSfpx+ZjNqK62hXG/mCLA9/gxLaUOHJG+L80Yn9L4XKZ4hAMos qKdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AHzdAEaT; 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z12-20020a056402274c00b0049e5332b5e7si26428328edd.46.2023.01.31.17.36.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 17:36:47 -0800 (PST) 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=AHzdAEaT; 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 230C3385828D for ; Wed, 1 Feb 2023 01:36:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 230C3385828D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675215406; bh=EowG+y/j43BG3mTqQhtvxtXLs8vA/VlF1GTd4NbB0bM=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=AHzdAEaTKxBrxVm/45tUF1B7JHaMfJpj3mo965W1bAj9xUjlVNp3qbYwPkWzgtGko gMICr+ogQVQT2KLGFKEyWK0UKzcM1t1EUkLq0lkZqWlfaJaCueKGR0wpCm5gTj8n9h UFdE02Hf3TzDzCOr/oL5yTUc4+xNsYifdIcp7Z9g= 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 108823858D39 for ; Wed, 1 Feb 2023 01:36:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 108823858D39 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 30VMW3sO008211 for ; Tue, 31 Jan 2023 17:21:22 -0800 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3nd442uy19-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Tue, 31 Jan 2023 17:21:22 -0800 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Tue, 31 Jan 2023 17:21:20 -0800 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.42 via Frontend Transport; Tue, 31 Jan 2023 17:21:20 -0800 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.67]) by maili.marvell.com (Postfix) with ESMTP id D4E393F7082; Tue, 31 Jan 2023 17:21:19 -0800 (PST) To: CC: Andrew Pinski Subject: [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1" Date: Tue, 31 Jan 2023 17:21:12 -0800 Message-ID: <20230201012112.1300516-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: rKAG5aLsDjygZQpwNJp9IAmbaYh8LcJR X-Proofpoint-ORIG-GUID: rKAG5aLsDjygZQpwNJp9IAmbaYh8LcJR X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.122.1 definitions=2023-01-31_08,2023-01-31_01,2022-06-22_01 X-Spam-Status: No, score=-14.5 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_NONE, 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756590259377534743?= X-GMAIL-MSGID: =?utf-8?q?1756590670823831842?= For bool values, it is easier to deal with xor 1 rather than having 1 - a. This is because we are more likely to simplify the xor further in many cases. This is a special case for (MASK - b) where MASK is a powerof2 - 1 and b <= MASK but only for bool ranges ([0,1]) as that is the main case where the difference comes into play. Note this is enabled for gimple folding only as the ranges are only know while doing gimple folding and cfun is not always set when fold is called. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/108355 PR tree-optimization/96921 * match.pd: Add pattern for "1 - bool_val". gcc/testsuite/ChangeLog: PR tree-optimization/108355 PR tree-optimization/96921 * gcc.dg/tree-ssa/bool-minus-1.c: New test. * gcc.dg/tree-ssa/bool-minus-2.c: New test. * gcc.dg/tree-ssa/pr108354-1.c: New test. --- gcc/match.pd | 13 ++++++++ gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c | 11 +++++++ gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c | 33 ++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c | 26 +++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c diff --git a/gcc/match.pd b/gcc/match.pd index f605b798c44..c9e8bebede2 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1732,6 +1732,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (!FIXED_POINT_TYPE_P (type)) (plus @0 (negate @1)))) +#if GIMPLE +/* 1 - a is a ^ 1 if a had a bool range. */ +/* This is only enabled for gimple as sometimes + cfun is not set for the function which contains + the SSA_NAME (e.g. while IPA passes are happening, + fold might be called). */ +(simplify + (minus integer_onep@0 SSA_NAME@1) + (if (INTEGRAL_TYPE_P (type) + && ssa_name_has_boolean_range (@1)) + (bit_xor @1 @0))) +#endif + /* Other simplifications of negation (c.f. fold_negate_expr_1). */ (simplify (negate (mult:c@0 @1 negate_expr_p@2)) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c new file mode 100644 index 00000000000..e434ff9507a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c @@ -0,0 +1,11 @@ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +_Bool +foo (_Bool a) +{ + int c = 1 - a; + return c; +} + +/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "~a" 1 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c new file mode 100644 index 00000000000..b77d36c1d3c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c @@ -0,0 +1,33 @@ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +_Bool +foo (_Bool a, _Bool b) +{ + int c = 1 - a; + int d = 1 - b; + int e = c & d; + return 1 - e; +} + +_Bool +bar (_Bool a, _Bool b) +{ + int c = 1 - a; + int d = 1 - b; + _Bool e = c & d; + return 1 - e; +} + +_Bool +baz (_Bool a, _Bool b) +{ + _Bool c = 1 - a; + _Bool d = 1 - b; + _Bool e = c & d; + return 1 - e; +} + +/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "~a" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "~b" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." 3 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c new file mode 100644 index 00000000000..60d1dbc281e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c @@ -0,0 +1,26 @@ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int b; +int *c; +int e; +static int *f = &e; +int g; +void foo(); +short(a)(short h, short i) { return h - i; } +int(d)(int h) { return h == 83647 ? 0 : -h; } +int main() { + short j; + int *k = &e, *l = &b; + *f = 0 == c; + j = a(0 != 2, *k); + if (d(j ^ (0 == l || *k)) != *k) + ; + else + foo(); + c = &g; +} + +/* { dg-final { scan-tree-dump-times " 1 - " 0 "optimized" } } */ +/* There should be no calls to foo. */ +/* { dg-final { scan-tree-dump-times "foo " 0 "optimized" } } */ +