From patchwork Mon May 13 15:24:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1934759 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=ineW63GN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4VdNbT1zDGz1yfq for ; Tue, 14 May 2024 01:25:23 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0F22338708C7 for ; Mon, 13 May 2024 15:25:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id BFF29384AB47 for ; Mon, 13 May 2024 15:24:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BFF29384AB47 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 BFF29384AB47 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715613898; cv=none; b=feMg9nc8GYGHxeRt7mPqzz6RQonTGOsP7VcDWUW1xFz+/fg7zn69ak4xLcuCZwIxsD7pTXP0nKYkpHKaf/QjsuUBzf53iwgVI/bPUz5wNSwyH5pneKDyQg9naPTcNep7LkPtTcaQINtFOPjlNgwkuRUxEryKwRv7QBVnECu1srg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715613898; c=relaxed/simple; bh=3Acv+c08k/m62I+YQPsbWMr7SEUzqEWn6CtMyekAMZw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wlfC04f+wY+xlyvqpyqgRcF8DXiR7FGUBX19W2nTWL03VZW1dRnDdQ7l5fbt1xIlEubxiyjud2IfYwPHHCGAeGthE9yILcOd/vWjjqC0UJ1NdolZo2vzg2BJ0k++t9oAwpF2i1KMNJaZ8fBwjloPIBegiiDwRaZOjffd5H1hcZM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 44DCX0jd023320 for ; Mon, 13 May 2024 15:24:55 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=N0aSnaz puTXY/2zcSR8aMMMx54msom7I0sIY8NFBzlM=; b=ineW63GNdf2acmfiMnG733v v6CxY7bGD6Kup63hbcrW3kEEpLTTephFfVY21eT8AYBRMR0bTAgMqGov1pAtAPFY 7BKB2zboGwVSvl+V7MkEqn59PkgYBkM18VFlPdOmGiUcGDLkVazmy+l6kmno8y3N BnRz1NH6yIhCV3O3mEdUGMCRrf15d14DgMgr6nuodKj7iMCATvEQ7d4Oax19M7qH BU4Ex4kMjjyDkfFrwxKu13/R4tVB/p6zfhWVXVVipRVqriyBNHSiDKIgwZwbVczn GT4M+pX9LZuMfkRMGW+bsbI+reCwSw+m8FtQS77KqePpVipAzafeeR2917zgKDg= = Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3y21edbsye-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 13 May 2024 15:24:55 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 44DFOsvE023696 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 13 May 2024 15:24:54 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; Mon, 13 May 2024 08:24:52 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] Match: optimize `a == CST & unary(a)` [PR111487] Date: Mon, 13 May 2024 08:24:38 -0700 Message-ID: <20240513152438.4132420-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: nalasex01b.na.qualcomm.com (10.47.209.197) 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: mOUb_c3DHgnNYYqMe2jcfomV0BtAMOhz X-Proofpoint-ORIG-GUID: mOUb_c3DHgnNYYqMe2jcfomV0BtAMOhz X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-13_10,2024-05-10_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=936 adultscore=0 priorityscore=1501 bulkscore=0 spamscore=0 mlxscore=0 malwarescore=0 suspectscore=0 impostorscore=0 clxscore=1015 lowpriorityscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405010000 definitions=main-2405130100 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 is an expansion of the optimize `a == CST & a` to handle more than just casts. It adds optimization for unary. The patch for binary operators will come later. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111487 gcc/ChangeLog: * match.pd (tcc_int_unary): New operator list. (`a == CST & unary(a)`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/and-unary-1.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 12 ++++ gcc/testsuite/gcc.dg/tree-ssa/and-unary-1.c | 61 +++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/and-unary-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 07e743ae464..3ee28a3d8fc 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -57,6 +57,10 @@ along with GCC; see the file COPYING3. If not see #include "cfn-operators.pd" +/* integer unary operators that return the same type. */ +(define_operator_list tcc_int_unary + abs absu negate bit_not BSWAP POPCOUNT CTZ CLZ PARITY) + /* Define operand lists for math rounding functions {,i,l,ll}FN, where the versions prefixed with "i" return an int, those prefixed with "l" return a long and those prefixed with "ll" return a long long. @@ -5451,6 +5455,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) @2 { build_zero_cst (type); })) +/* `(a == CST) & unary(a)` can be simplified to `(a == CST) & unary(CST)`. */ +(simplify + (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) + (convert? (tcc_int_unary @3))) + (if (bitwise_equal_p (@0, @3)) + (with { tree inner_type = TREE_TYPE (@3); } + (bit_and @2 (convert (tcc_int_unary (convert:inner_type @1))))))) + /* Optimize # x_5 in range [cst1, cst2] where cst2 = cst1 + 1 x_5 == cstN ? cst4 : cst3 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/and-unary-1.c b/gcc/testsuite/gcc.dg/tree-ssa/and-unary-1.c new file mode 100644 index 00000000000..c157bc11b00 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/and-unary-1.c @@ -0,0 +1,61 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop1-raw -fdump-tree-optimized-raw" } */ +/* unary part of PR tree-optimization/111487 */ + +int abs1(int a) +{ + int b = __builtin_abs(a); + return (a == 1) & b; +} +int absu1(int a) +{ + int b; + b = a > 0 ? -a:a; + b = -b; + return (a == 1) & b; +} + +int bswap1(int a) +{ + int b = __builtin_bswap32(a); + return (a == 1) & b; +} + +int ctz1(int a) +{ + int b = __builtin_ctz(a); + return (a == 1) & b; +} +int pop1(int a) +{ + int b = __builtin_popcount(a); + return (a == 1) & b; +} +int neg1(int a) +{ + int b = -(a); + return (a == 1) & b; +} +int not1(int a) +{ + int b = ~(a); + return (a == 1) & b; +} +int partity1(int a) +{ + int b = __builtin_parity(a); + return (a == 1) & b; +} + + +/* We should optimize out the unary operator for each. + For ctz we can optimize directly to `return 0`. + For bswap1 and not1, we can do the same but not until after forwprop1. */ +/* { dg-final { scan-tree-dump-times "eq_expr, " 7 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "eq_expr, " 5 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "abs_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "absu_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "bit_not_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "negate_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "gimple_call <" "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "bit_and_expr, " "forwprop1" } } */