From patchwork Wed May 1 03:21:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1930031 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=lKvL7les; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VTj6x1N04z23hd for ; Wed, 1 May 2024 13:22:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C04D385EC56 for ; Wed, 1 May 2024 03:22:28 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 529453858D34 for ; Wed, 1 May 2024 03:22:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 529453858D34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 529453858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714533730; cv=none; b=IJGeeuaIrofSXcOrIy8K6oeUTm+tFw8kjg7G+4aQoyvkg9FCAN8oGudsBs580iwIv2nVPuJEptSOMdZW1zYylwrV4dhLrojKCXTtwS6aSQWvoDmfgdO2OsaGs9KGiumiGIhWQMKZQqMZX+oOJuOzNuls+jZSskA2uD9xz4pz5TY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714533730; c=relaxed/simple; bh=lyBIJNcst644joYvJ/paL0nXfOxTgbkQjuEqWGehHjc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Jo42cnKjI6dpWZJFnA9te3tA9o9nRiN+CRdTDf7H4STlzZ+xBlh2+NK/U0SxTj+hqv4oSLAsQf30RCXoTA1GHGV8beTvFXQ0Z8RCOF7f7qp2RQoeDbKWAQ50Xw1CCM3G0072Q2v0/fWjAWrmEMW+92J42Yo/OSeYGtk+HJByGjI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 44132mxT002506 for ; Wed, 1 May 2024 03:22:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=qcppdkim1; bh=QQ3SJ7k 5rAj7o80Uwp8Asb2WQM0w6FU+mrRyhfYEZKU=; b=lKvL7lesDG7aUh49qwPgHh7 VzhEJwXrSmamlctq7FgAjtZ0BCHNmoHh+6xpB0PakQEyMuhr5en/WsBcn43Yoo8l MwByj9dLSSup2TU8bAVeGpewyVsmVmI2NU1BmzwuCLHCAuNVuG9fy7kUbYa1tr24 KFfK/fuT2LmzFcf9Hvg1oPQjhWSmNnIlFLYK7c/ZN3Q6pTQq96qCHU/i9DyQCpzJ ttW9rpupdGERK5SVrdWjNPlZ/dM71G95W1hL6T3XYRvNPeCmPVxSb52X9JiKmW43 t1u+soEXebUqz0E/zW2SOp+1I8MX2xvpbqoFeEqvoN+RPyUEGsJQG94a8OFo5pw= = Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3xttw3jn90-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 01 May 2024 03:22:07 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 4413M656022101 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 1 May 2024 03:22:06 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 30 Apr 2024 20:22:06 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] MATCH: Add some more value_replacement simplifications (a != 0 ? expr : 0) to match Date: Tue, 30 Apr 2024 20:21:14 -0700 Message-ID: <20240501032114.1112795-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: BWLi0kgUfgAqtEuzrKulceK6K1DbW-iF X-Proofpoint-ORIG-GUID: BWLi0kgUfgAqtEuzrKulceK6K1DbW-iF X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-01_02,2024-04-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 adultscore=0 lowpriorityscore=0 clxscore=1015 phishscore=0 impostorscore=0 bulkscore=0 priorityscore=1501 mlxscore=0 malwarescore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2405010023 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PLING_QUERY, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This adds a few more of what is currently done in phiopt's value_replacement to match. I noticed this when I was hooking up phiopt's value_replacement code to use match and disabling the old code. But this can be done independently from the hooking up phiopt's value_replacement as phiopt is already hooked up for simplified versions already. /* a != 0 ? a / b : 0 -> a / b iff b is nonzero. */ /* a != 0 ? a * b : 0 -> a * b */ /* a != 0 ? a & b : 0 -> a & b */ We prefer the `cond ? a : 0` forms to allow optimization of `a * cond` which uses that form. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR treee-optimization/114894 gcc/ChangeLog: * match.pd (`a != 0 ? a / b : 0`): New pattern. (`a != 0 ? a * b : 0`): New pattern. (`a != 0 ? a & b : 0`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-value-5.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 18 +++++++++ .../gcc.dg/tree-ssa/phi-opt-value-5.c | 39 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-5.c diff --git a/gcc/match.pd b/gcc/match.pd index d401e7503e6..03a03c31233 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4290,6 +4290,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0)) @2)) +/* a != 0 ? a / b : 0 -> a / b iff b is nonzero. */ +(for op (trunc_div ceil_div floor_div round_div exact_div) + (simplify + (cond (ne @0 integer_zerop) (op@2 @3 @1) integer_zerop ) + (if (bitwise_equal_p (@0, @3) + && tree_expr_nonzero_p (@1)) + @2))) + +/* Note we prefer the != case here + as (a != 0) * (a * b) will generate that version. */ +/* a != 0 ? a * b : 0 -> a * b */ +/* a != 0 ? a & b : 0 -> a & b */ +(for op (mult bit_and) + (simplify + (cond (ne @0 integer_zerop) (op:c@2 @1 @3) integer_zerop) + (if (bitwise_equal_p (@0, @3)) + @2))) + /* Simplifications of shift and rotates. */ (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-5.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-5.c new file mode 100644 index 00000000000..8062eb19b11 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-5.c @@ -0,0 +1,39 @@ +/* { dg-do compile } */ +/* PR treee-optimization/114894 */ +/* Phi-OPT should be able to optimize these without sinking being invoked. */ +/* { dg-options "-O -fdump-tree-phiopt2 -fdump-tree-phiopt3 -fdump-tree-optimized -fno-tree-sink" } */ + +int fmul1(int a, int b) +{ + int c = a * b; + if (a != 0) + return c; + return 0; +} + + +int fand1(int a, int b) +{ + int c = a & b; + if (a != 0) + return c; + return 0; +} + + +void g(int); + +int fdiv1(int a, int b) +{ + int d = b|1; + g(d); + int c = a / d; + return a != 0 ? c : 0; +} + +/* fdiv1 requires until later than phiopt2 to be able to detect that + d is non-zero. to be able to remove the conditional. */ +/* { dg-final { scan-tree-dump-times "goto" 2 "phiopt2" } } */ +/* { dg-final { scan-tree-dump-not "goto" "phiopt3" } } */ +/* { dg-final { scan-tree-dump-not "goto" "optimized" } } */ +