From patchwork Thu Aug 29 01:35:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1978202 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=QS3QKXoh; 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 4WvP620RHcz1yfn for ; Thu, 29 Aug 2024 11:37:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A1F7385DDE6 for ; Thu, 29 Aug 2024 01:37:11 +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 2FAB33858CD9 for ; Thu, 29 Aug 2024 01:36:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2FAB33858CD9 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 2FAB33858CD9 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=1724895406; cv=none; b=v+bV2jbZ2qgJdz92NY/3/vIJEK4ZASX2blKC81FcZVSjcpjNqP8gJBTZq5IvzX8Gx8QDTC6xC8MnSi0lqldorEqJThTv1LbyVGJYPkbut5wDX/UIofKLE4EeTwb2+3QFY00Ru1gNB1Q365fWAbB7n/3paQJDP86ve+eUaPoWZjg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724895406; c=relaxed/simple; bh=KucDsfyupupmQwi+csa8ZgIR3E7TxRA5kivXYVKVPcQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bovtAsQMKmptbqVOy8By5n0wuvX3VZm/pjB6YZHxPjQPB1RCdKbN81Y7LJ82KmETMRBnH7Ho5+rn9F9hZ3EcagQrL7uzbFOlefI6UgUPNt44aqwSeEo4NiziJndGTKUdCdlfCU0UluW1U7l6Dm5Roizkmg0QrKvCHLyDBP13J2Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47SJM20n031817 for ; Thu, 29 Aug 2024 01:36:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=HuhVTuAb5qjVAn/hA/bLzi B2p06RbHLL10nMcWEk/ao=; b=QS3QKXohF1AriuKGPxDFteHCA5m+4HomE4P0ty CPi3LkeTI95mhos3tRJC0/JQRPVh0l/xjYX0aL5hcpzYqDtgVy/C1xnubhinx9o7 qC7wZCKl71T1BU5HHLsko5E4K2AZxVzPepoX5gFMfV7I5/wNyTl1mqL0uDn8xRL3 +e7lhH+SfAkvDWNVfD3RVVAhgO3ag2yC236GaHJUm0MYByQFssYZFq+Y+03CGurc ZmXBOfi3y/1Ja2G860jsXpIIyEq55w01YaM5aaNi1A1FuBG6ixgFvNJnUSUK2j7L NI69I/bX+2QISmEf5qT2IOTnQkdywbzeOzrXLsDxlkF2+YOA== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 419puw3jyf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 29 Aug 2024 01:36:44 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 47T1a7aq028726 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 29 Aug 2024 01:36:07 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; Wed, 28 Aug 2024 18:36:07 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] expand: Allow widdening optab when expanding popcount==1 [PR116508] Date: Wed, 28 Aug 2024 18:35:57 -0700 Message-ID: <20240829013557.3827912-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-ORIG-GUID: Bgnk5cgsQivuD04sdFzc7l8pgcEvnbc7 X-Proofpoint-GUID: Bgnk5cgsQivuD04sdFzc7l8pgcEvnbc7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-29_01,2024-08-28_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=1 clxscore=1015 priorityscore=1501 malwarescore=0 bulkscore=0 mlxscore=1 lowpriorityscore=0 phishscore=0 mlxlogscore=216 suspectscore=0 adultscore=0 spamscore=1 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408290011 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, 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.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 After adding popcount{qi,hi}2 to the aarch64 backend, I noticed that the expansion for popcount==1 was no longer trying to do the trick of handling popcount==1 as `(arg ^ (arg - 1)) > arg - 1`. The problem is the expansion was using OPTAB_DIRECT, when using OPTAB_WIDEN will allow modes which are smaller than SImode (in the aarch64 case). Note QImode's cost still needs some improvements so part of popcnt-eq-1.c is xfailed. Though there is a check to make sure the costs are compared now. Built and tested on aarch64-linux-gnu. PR middle-end/116508 gcc/ChangeLog: * internal-fn.cc (expand_POPCOUNT): Use OPTAB_WIDEN for PLUS and XOR/AND expansion. gcc/testsuite/ChangeLog: * gcc.target/aarch64/popcnt-eq-1.c: New test. Signed-off-by: Andrew Pinski --- gcc/internal-fn.cc | 4 +- .../gcc.target/aarch64/popcnt-eq-1.c | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.target/aarch64/popcnt-eq-1.c diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index 78997ef056a..4e33db365ac 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -5332,11 +5332,11 @@ expand_POPCOUNT (internal_fn fn, gcall *stmt) start_sequence (); rtx op0 = expand_normal (arg); rtx argm1 = expand_simple_binop (mode, PLUS, op0, constm1_rtx, NULL_RTX, - 1, OPTAB_DIRECT); + 1, OPTAB_WIDEN); if (argm1 == NULL_RTX) goto fail; rtx argxorargm1 = expand_simple_binop (mode, nonzero_arg ? AND : XOR, op0, - argm1, NULL_RTX, 1, OPTAB_DIRECT); + argm1, NULL_RTX, 1, OPTAB_WIDEN); if (argxorargm1 == NULL_RTX) goto fail; rtx cmp; diff --git a/gcc/testsuite/gcc.target/aarch64/popcnt-eq-1.c b/gcc/testsuite/gcc.target/aarch64/popcnt-eq-1.c new file mode 100644 index 00000000000..bb9e2bf0a54 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/popcnt-eq-1.c @@ -0,0 +1,45 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-rtl-expand-details" } */ +/* { dg-final { check-function-bodies "**" "" } } */ +/* PR middle-end/116508 */ + +#pragma GCC target "+nocssc" + +/* +** h16: +** sub w([0-9]+), w0, #1 +** eor w([0-9]+), w0, w\1 +** and w([0-9]+), w\1, 65535 +** cmp w\3, w\2, uxth +** cset w0, cc +** ret +*/ + +/* when expanding popcount == 1, should use + `(arg ^ (arg - 1)) > arg - 1` as that has a lower latency + than doing the popcount then comparing against 1. + The popcount/addv can be costly. */ +unsigned h16 (const unsigned short a) { + return __builtin_popcountg (a) == 1; +} + +/* unsigned char should also do the same trick */ +/* Currently xfailed since the cost does not take into account the + moving between gprs and vector regs correctly. */ +/* +** h8: { xfail *-*-* } +** sub w([0-9]+), w0, #1 +** eor w([0-9]+), w0, w\1 +** and w([0-9]+), w\1, 255 +** cmp w\3, w\2, uxtb +** cset w0, cc +** ret +*/ + + +unsigned h8 (const unsigned char a) { + return __builtin_popcountg (a) == 1; +} + +/* There should be printing out the costs for h8 and h16's popcount == 1 */ +/* { dg-final { scan-rtl-dump-times "popcount == 1:" 2 "expand"} } */