From patchwork Mon Aug 8 09:06:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 427 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:20da:b0:2d3:3019:e567 with SMTP id n26csp1724836pxc; Mon, 8 Aug 2022 02:06:38 -0700 (PDT) X-Google-Smtp-Source: AA6agR5dSUOQHbw0KYKUkRNMct3NFl+Fhu6CzYMWqXSoO+eBK0F6qFagpwcGEIDTiZMJ24oY36ge X-Received: by 2002:a05:6402:270e:b0:43d:e3e1:847a with SMTP id y14-20020a056402270e00b0043de3e1847amr17248235edd.130.1659949598469; Mon, 08 Aug 2022 02:06:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659949598; cv=none; d=google.com; s=arc-20160816; b=wGjbWimYmCZGHyZ7rMWlCgeTUlCf/cYQRRYlZMxhyC+xHQUJdkMl4YWyCDy7pqRBjP 2VBBrToxCC00YJwJ3IJByzuaxuQzCa+9J+MDB/aVI05/890mF8tBeFuVi1GtPQhhn/1E BA5vS8lNqJ9CLUubxMaTv9FdGxFMBBLsoHgVkjJ0f3DpkBSTwxELIp78QAky3Fkbfcm9 mXCYpJlBbh66nOTAI4j8Ecce0+aJNQNfJVi3hY1D8M0WKNHriN0X1v5Lsl2NaMLzvFk3 rg3QPCgdtVs3PombMTzQ4Zmmx9fNZT8Fb2qKT6UnQGC9CseeMdBGAqmpYL3pUJzR4cwD HCxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:delivered-to; bh=34VzJP3ilk3PBPE+Dk+RC2r8zCOX18RBvm44XvPQpHQ=; b=aGudSqVr5W/48aoTyt+5nO7J7yaZ7rPwd562hr1WGI6eu9JwEYsRcEXWyQ2YT/K/jJ IBILhGjIJODRrNfF2aiL1cUaUucD7CUkmZR2hi3hEkNvgH1Gdr3Is6BkZwBJixYJtc2C H/8u7AgPidU4voHiqVEs+jAy2iZByapYHI++vPdoG0LLgj18H8Yv5dORuaEZlgmeRXAL 9XLKrV/3mxmkIKqCnoKvVgm3kzvgqNbK7tlDnPEWkZQ7Hp8MwpqAzvJtTP0W1PF+R0nu X1f7EZ92Sul5PSWP5YK2REQf3WuA1XPYg0J2XFminIu7QQktpI1blw2YqtqfOVqD7FHE 70MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=aEUIEwpg; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x11-20020a50ba8b000000b0043ddc200046si6026873ede.454.2022.08.08.02.06.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 02:06:38 -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=fail header.i=@nextmovesoftware.com header.s=default header.b=aEUIEwpg; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92CCC3858287 for ; Mon, 8 Aug 2022 09:06:33 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 6BD0038582B3 for ; Mon, 8 Aug 2022 09:06:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6BD0038582B3 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=34VzJP3ilk3PBPE+Dk+RC2r8zCOX18RBvm44XvPQpHQ=; b=aEUIEwpgmxjK53QxqKeKJwdui6 1YxjxQiDNayF1a08201bCgXpFeaWkw4yZFueg6fHlXPP5mVFzup08pk2YmnP8qn4DWsgaPEYk+ATz 2A9Y/SB7ox+RV49E5MRdGsSkP+rwEhInD4QzPqp/s4DuJK8xHSqYGIi98axQWupCmnvldD1ejI97X FiVWpjaJ8CH+AgVJlKgR7a/anW55FbjfBD6YCjRCa/olSpYRTyg6dMR2fd6pTC9PprP19Zfe3Q9fU K5O+DLp0bIwI+xS7qw2SnXnXpwhO3JPAjKq+k0bV2v1pf05ZNXypyG/vwU9ro3z0z5HCp4u3Pb1Hk j/144M1w==; Received: from host86-169-41-119.range86-169.btcentralplus.com ([86.169.41.119]:57070 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1oKyi9-00085T-ML; Mon, 08 Aug 2022 05:06:09 -0400 From: "Roger Sayle" To: Subject: [PATCH] PR tree-optimization/64992: (B << 2) != 0 is B when B is Boolean. Date: Mon, 8 Aug 2022 10:06:05 +0100 Message-ID: <00ce01d8ab06$19113060$4b339120$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdirBaEKwGUos4itTrmBOpbgNtqTfw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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: , Cc: '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?1740583310340636158?= X-GMAIL-MSGID: =?utf-8?q?1740583310340636158?= This patch resolves both PR tree-optimization/64992 and PR tree-optimization/98956 which are missed optimization enhancement request, for which Andrew Pinski already has a proposed solution (related to a fix for PR tree-optimization/98954). Yesterday, I proposed an alternate improved patch for PR98954, which although superior in most respects, alas didn't address this case [which doesn't include a BIT_AND_EXPR], hence this follow-up fix. For many functions, F(B), of a (zero-one) Boolean value B, the expression F(B) != 0 can often be simplified to just B. Hence "(B * 5) != 0" is B, "-B != 0" is B, "bswap(B) != 0" is B, "(B >>r 3) != 0" is B. These are all currently optimized by GCC, with the strange exception of left shifts by a constant (possibly due to the undefined/implementation defined behaviour when the shift constant is larger than the first operand's precision). This patch adds support for this particular case, when the shift constant is valid. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32}, with no new failures. Ok for mainline? 2022-08-08 Roger Sayle gcc/ChangeLog PR tree-optimization/64992 PR tree-optimization/98956 * match.pd (ne (lshift @0 @1) 0): Simplify (X << C) != 0 to X when X is zero_one_valued_p and the shift constant C is valid. (eq (lshift @0 @1) 0): Likewise, simplify (X << C) == 0 to !X when X is zero_one_valued_p and the shift constant C is valid. gcc/testsuite/ChangeLog PR tree-optimization/64992 * gcc.dg/pr64992.c: New test case. Thanks in advance, Roger diff --git a/gcc/match.pd b/gcc/match.pd index f82f94a..ef6d8e2 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1900,6 +1900,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))) (mult (convert @1) (convert @2)))) +/* (X << C) != 0 can be simplified to X, when X is zero_one_valued_p. */ +(simplify + (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) + (if (tree_fits_shwi_p (@1) + && tree_to_shwi (@1) > 0 + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) + (convert @0))) + +/* (X << C) == 0 can be simplified to X == 0, when X is zero_one_valued_p. */ +(simplify + (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) + (if (tree_fits_shwi_p (@1) + && tree_to_shwi (@1) > 0 + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) + (eq @0 @2))) + /* Convert ~ (-A) to A - 1. */ (simplify (bit_not (convert? (negate @0))) diff --git a/gcc/testsuite/gcc.dg/pr64992.c b/gcc/testsuite/gcc.dg/pr64992.c new file mode 100644 index 0000000..43fbcf7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr64992.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +_Bool foo(_Bool x) { return (x << 2) != 0; } +_Bool bar(_Bool x) { return (x << 2) == 0; } + +/* { dg-final { scan-tree-dump-not " << " "optimized" } } */