From patchwork Fri Aug 30 00:08:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1978667 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=fwXgccTb; 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 4Wvz5m3QJQz1ydy for ; Fri, 30 Aug 2024 10:08:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C34E385EC54 for ; Fri, 30 Aug 2024 00:08:57 +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 2CB78385C6C8 for ; Fri, 30 Aug 2024 00:08:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CB78385C6C8 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 2CB78385C6C8 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=1724976509; cv=none; b=nlGweDvVyga8wwYRl1LmbpuIieTK+x4+vzTPi8Vr1UnGoIKTdOLnBV7gDuXsW90np6QR/tvs+jkv/kyFGR2OBoQR1Fo58Y/ie2CO8WSCbw8/BsuXUUfQrJdyNUHnQipZKAA5YVMoF+IVYwRQwcWbEYuBILA2aDfk5t5hNjA2Eco= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724976509; c=relaxed/simple; bh=xwLkbS/OT0oPPMyr5Vsu9s4QKWBKaWToV6ZRdKnchdM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BzYK5yui5ldqJ77cPIwpYKI3znsq3SCywTXYeuQCTng62mFiSOqeHgSszHvxoSYmf5kTEvLNp+I+XByF5Kha282LNwXgul4L6Xorq8onVysOmMxDJmBmtk2N065IRd/vvtPuRKUGpO+pZCCT4358/Q9ItxmDTQrsCkgw9+8DqLE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47TGYPld026770 for ; Fri, 30 Aug 2024 00:08:26 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=nWPqcumFKYr/2e/w5iMrhP NBkDmfq3c+BHXo4+aGArw=; b=fwXgccTb+CVhJPHUdF3+wOrPG4IyBpHKDN1xKH pH6bt577up8NZBzWsx/o0TxXB/OLI9w9aYkAQpIp39xNSNFxGKI46MTITby15sKY jzcgcTeZ0n6Qz9jsi2/6pXNBq65lt6m4sQVF9mHSPrnfgT4o1j60VnaQVw8+lvfp RBF5MDThexNf0ICdc4qknlelzOWWiTrMD9QIi/fahKzVvi6YCgshEpaDeN9lzk+Z fM39/gJ104ZRtXY5tbU6MQU743wb/uN56Km9L9KIT12sn09zmZjNEJEaWDHzPdVn pf6j81okkG6uIGskZqIpOQQ1k/6X19U/eGzfavlqpWiQu7gQ== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 419puvey2k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 30 Aug 2024 00:08:25 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA02.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 47U08PlK030266 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 30 Aug 2024 00:08:25 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; Thu, 29 Aug 2024 17:08:24 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] middle-end: also optimized `popcount(a) <= 1` [PR90693] Date: Thu, 29 Aug 2024 17:08:13 -0700 Message-ID: <20240830000813.145441-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-ORIG-GUID: AWnFvOWoKzDhMZxaSghXdanPau1F9-Oj X-Proofpoint-GUID: AWnFvOWoKzDhMZxaSghXdanPau1F9-Oj 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_06,2024-08-29_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 mlxlogscore=999 priorityscore=1501 bulkscore=0 impostorscore=0 adultscore=0 malwarescore=0 phishscore=0 lowpriorityscore=0 mlxscore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408290172 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 This expands on optimizing `popcount(a) == 1` to also handle `popcount(a) <= 1`. `<= 1` can be expanded as `(a & -a) == 0` like what is done for `== 1` if we know that a was nonzero. We have to do the optimization in 2 places due to if we have an optab entry for popcount or not. Built and tested for aarch64-linux-gnu. PR middle-end/90693 gcc/ChangeLog: * internal-fn.cc (expand_POPCOUNT): Handle the second argument being `-1` for `<= 1`. * tree-ssa-math-opts.cc (match_single_bit_test): Handle LE/GT cases. (math_opts_dom_walker::after_dom_children): Call match_single_bit_test for LE_EXPR/GT_EXPR also. gcc/testsuite/ChangeLog: * gcc.target/aarch64/popcnt-le-1.c: New test. * gcc.target/aarch64/popcnt-le-2.c: New test. * gcc.target/aarch64/popcnt-le-3.c: New test. Signed-off-by: Andrew Pinski --- gcc/internal-fn.cc | 20 +++++++++++- .../gcc.target/aarch64/popcnt-le-1.c | 29 +++++++++++++++++ .../gcc.target/aarch64/popcnt-le-2.c | 31 +++++++++++++++++++ .../gcc.target/aarch64/popcnt-le-3.c | 31 +++++++++++++++++++ gcc/tree-ssa-math-opts.cc | 25 +++++++++++---- 5 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.target/aarch64/popcnt-le-1.c create mode 100644 gcc/testsuite/gcc.target/aarch64/popcnt-le-2.c create mode 100644 gcc/testsuite/gcc.target/aarch64/popcnt-le-3.c diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index 4e33db365ac..b55f089cf56 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -5304,11 +5304,16 @@ expand_POPCOUNT (internal_fn fn, gcall *stmt) Use rtx costs in that case to determine if .POPCOUNT (arg) == 1 or (arg ^ (arg - 1)) > arg - 1 is cheaper. If .POPCOUNT second argument is 0, we additionally know that arg - is non-zero, so use arg & (arg - 1) == 0 instead. */ + is non-zero, so use arg & (arg - 1) == 0 instead. + If .POPCOUNT second argument is -1, the comparison was either `<= 1` + or `> 1`. */ bool speed_p = optimize_insn_for_speed_p (); tree lhs = gimple_call_lhs (stmt); tree arg = gimple_call_arg (stmt, 0); bool nonzero_arg = integer_zerop (gimple_call_arg (stmt, 1)); + bool was_le = integer_minus_onep (gimple_call_arg (stmt, 1)); + if (was_le) + nonzero_arg = true; tree type = TREE_TYPE (arg); machine_mode mode = TYPE_MODE (type); machine_mode lhsmode = TYPE_MODE (TREE_TYPE (lhs)); @@ -5360,10 +5365,23 @@ expand_POPCOUNT (internal_fn fn, gcall *stmt) emit_insn (popcount_insns); else { + start_sequence (); emit_insn (cmp_insns); plhs = expand_normal (lhs); if (GET_MODE (cmp) != GET_MODE (plhs)) cmp = convert_to_mode (GET_MODE (plhs), cmp, 1); + /* For `<= 1`, we need to produce `2 - cmp` or `cmp ? 1 : 2` as that + then gets compared against 1 and we need the false case to be 2. */ + if (was_le) + { + cmp = expand_simple_binop (GET_MODE (cmp), MINUS, const2_rtx, + cmp, NULL_RTX, 1, OPTAB_WIDEN); + if (!cmp) + goto fail; + } emit_move_insn (plhs, cmp); + rtx_insn *all_insns = get_insns (); + end_sequence (); + emit_insn (all_insns); } } diff --git a/gcc/testsuite/gcc.target/aarch64/popcnt-le-1.c b/gcc/testsuite/gcc.target/aarch64/popcnt-le-1.c new file mode 100644 index 00000000000..b4141da982c --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/popcnt-le-1.c @@ -0,0 +1,29 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-rtl-expand-details" } */ +/* { dg-final { check-function-bodies "**" "" } } */ +/* PR middle-end/90693 */ + +#pragma GCC target "+nocssc" + +/* +** le32: +** sub w([0-9]+), w0, #1 +** tst w0, w\1 +** cset w0, eq +** ret +*/ + +unsigned le32 (const unsigned int a) { + return __builtin_popcountg (a) <= 1; +} + +/* +** gt32: +** sub w([0-9]+), w0, #1 +** tst w0, w\1 +** cset w0, ne +** ret +*/ +unsigned gt32 (const unsigned int a) { + return __builtin_popcountg (a) > 1; +} diff --git a/gcc/testsuite/gcc.target/aarch64/popcnt-le-2.c b/gcc/testsuite/gcc.target/aarch64/popcnt-le-2.c new file mode 100644 index 00000000000..975552ca63e --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/popcnt-le-2.c @@ -0,0 +1,31 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mgeneral-regs-only -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-not "POPCOUNT \\\(" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_popcount \\\(" "optimized" } } */ +/* { dg-final { check-function-bodies "**" "" } } */ +/* PR middle-end/90693 */ + +#pragma GCC target "+nocssc" + +/* +** le32: +** sub w([0-9]+), w0, #1 +** tst w\1, w0 +** cset w0, eq +** ret +*/ + +unsigned le32 (const unsigned int a) { + return __builtin_popcountg (a) <= 1; +} + +/* +** gt32: +** sub w([0-9]+), w0, #1 +** tst w\1, w0 +** cset w0, ne +** ret +*/ +unsigned gt32 (const unsigned int a) { + return __builtin_popcountg (a) > 1; +} diff --git a/gcc/testsuite/gcc.target/aarch64/popcnt-le-3.c b/gcc/testsuite/gcc.target/aarch64/popcnt-le-3.c new file mode 100644 index 00000000000..b811e6f6e8f --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/popcnt-le-3.c @@ -0,0 +1,31 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-rtl-expand-details" } */ +/* { dg-final { check-function-bodies "**" "" } } */ +/* PR middle-end/90693 */ + +#pragma GCC target "+nocssc" + +/* +** le16: +** sub w([0-9]+), w0, #1 +** and w([0-9]+), w0, w\1 +** tst w\2, 65535 +** cset w0, eq +** ret +*/ + +unsigned le16 (const unsigned short a) { + return __builtin_popcountg (a) <= 1; +} + +/* +** gt16: +** sub w([0-9]+), w0, #1 +** and w([0-9]+), w0, w\1 +** tst w\2, 65535 +** cset w0, ne +** ret +*/ +unsigned gt16 (const unsigned short a) { + return __builtin_popcountg (a) > 1; +} diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 3c93fca5b53..d61668aacfc 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -5433,13 +5433,15 @@ match_uaddc_usubc (gimple_stmt_iterator *gsi, gimple *stmt, tree_code code) /* Replace .POPCOUNT (x) == 1 or .POPCOUNT (x) != 1 with (x & (x - 1)) > x - 1 or (x & (x - 1)) <= x - 1 if .POPCOUNT - isn't a direct optab. */ + isn't a direct optab. Also handle `<=`/`>` to be + `x & (x - 1) !=/== x`. */ static void match_single_bit_test (gimple_stmt_iterator *gsi, gimple *stmt) { tree clhs, crhs; enum tree_code code; + bool was_le = false; if (gimple_code (stmt) == GIMPLE_COND) { clhs = gimple_cond_lhs (stmt); @@ -5452,8 +5454,11 @@ match_single_bit_test (gimple_stmt_iterator *gsi, gimple *stmt) crhs = gimple_assign_rhs2 (stmt); code = gimple_assign_rhs_code (stmt); } - if (code != EQ_EXPR && code != NE_EXPR) + if (code != LE_EXPR && code != GT_EXPR + && code != EQ_EXPR && code != NE_EXPR) return; + if (code == LE_EXPR || code == GT_EXPR) + was_le = true; if (TREE_CODE (clhs) != SSA_NAME || !integer_onep (crhs)) return; gimple *call = SSA_NAME_DEF_STMT (clhs); @@ -5477,8 +5482,9 @@ match_single_bit_test (gimple_stmt_iterator *gsi, gimple *stmt) /* Tell expand_POPCOUNT the popcount result is only used in equality comparison with one, so that it can decide based on rtx costs. */ gimple *g = gimple_build_call_internal (IFN_POPCOUNT, 2, arg, - nonzero_arg ? integer_zero_node - : integer_one_node); + was_le ? integer_minus_one_node + : (nonzero_arg ? integer_zero_node + : integer_one_node)); gimple_call_set_lhs (g, gimple_call_lhs (call)); gimple_stmt_iterator gsi2 = gsi_for_stmt (call); gsi_replace (&gsi2, g, true); @@ -5489,11 +5495,16 @@ match_single_bit_test (gimple_stmt_iterator *gsi, gimple *stmt) build_int_cst (type, -1)); gsi_insert_before (gsi, g, GSI_SAME_STMT); g = gimple_build_assign (make_ssa_name (type), - nonzero_arg ? BIT_AND_EXPR : BIT_XOR_EXPR, + (nonzero_arg || was_le) ? BIT_AND_EXPR : BIT_XOR_EXPR, arg, argm1); gsi_insert_before (gsi, g, GSI_SAME_STMT); tree_code cmpcode; - if (nonzero_arg) + if (was_le) + { + argm1 = build_zero_cst (type); + cmpcode = code == LE_EXPR ? EQ_EXPR : NE_EXPR; + } + else if (nonzero_arg) { argm1 = build_zero_cst (type); cmpcode = code; @@ -6188,6 +6199,8 @@ math_opts_dom_walker::after_dom_children (basic_block bb) case EQ_EXPR: case NE_EXPR: + case LE_EXPR: + case GT_EXPR: match_single_bit_test (&gsi, stmt); break;