From patchwork Mon Jul 1 21:46:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1954948 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=Qn43cZly; 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 4WCfly5b5Gz1xqd for ; Tue, 2 Jul 2024 07:47:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6F28D3810B43 for ; Mon, 1 Jul 2024 21:47:39 +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 43C7E385C6CF for ; Mon, 1 Jul 2024 21:47:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 43C7E385C6CF 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 43C7E385C6CF 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=1719870431; cv=none; b=Sm2su8vlfIN0IVTLQGn+/OEF30/cbVIgNKtfK+CPtBK7QpVYMw87UPV5QLCln2Nb9IByt302EujC6b38UPxxZn8P4r/EKSaKgR+r7q8fR4j4V5UDmDu6c7yoosaxjbh0P95L9GPoW/4wKN2tfuHtsDT347iZwr6d7kPksiWtsQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719870431; c=relaxed/simple; bh=6QWUkEmywCrfx0e7K1uVBHfOaSrHOyXXEhzihw2thOs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=iLBySMJE1RoAUOn3oB41qVz1ji6EznD0Hfgcf8x/NkYwbdTRcsVcGkoU+tEpmkkYlWnuk8YwHRVPCC5F9voaYkOotDcdqmVSnA7dHvVqRhDeQZ55+VuPJCLr04mR+iKry/ir45S6swGK2sDnen5GToLZ1NV11DK7U8hCBllAS3c= 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 461JA25d003317 for ; Mon, 1 Jul 2024 21:47:09 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= I73JKn6gcx+P3roVxTKuL+OY295EDxZ5T/bZwZe63B4=; b=Qn43cZlyOZh3TCbQ 09jUuKB3a2ZSxEwB03YkR+VuR9OdreObQMTu0+TlcjJ7RhByc5vfaBCwPcjqXQBV L9U9kP1TuYO3azTPG+A1JW7Y1DwHsF1ITyiV2sygM+NqGe1siRK5e5os2OU41wuX ydQ2Lq2rl8rRlPR5MGDDwv62jamYqw2qowEHM/qJwpSeZKHZQwfW8LhXBqZr2o47 zgr3+u9HhZrFSx7qRcajb6FWZl9qx4iItyV4uv9/LVgNA0iNALh/V7UK6MM50mD+ Vq0+0OwuuADYSkGAWeaeDb+K1s1J5NjiwyvMiQiflocxm/fXJ4khYlpOxq3MI7N8 GijdDw== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4029uxdes6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 01 Jul 2024 21:47:09 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 461Ll8Mk015923 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 1 Jul 2024 21:47:08 GMT Received: from hu-apinski-lv.qualcomm.com (10.80.80.8) 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, 1 Jul 2024 14:47:08 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 1/4] Small optimization for complex addition, real/imag parts the same Date: Mon, 1 Jul 2024 14:46:54 -0700 Message-ID: <20240701214657.3163066-2-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701214657.3163066-1-quic_apinski@quicinc.com> References: <20240701214657.3163066-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) 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: JrJMGAtsN6lquqGWO-ra9GtVZiTV_CMF X-Proofpoint-ORIG-GUID: JrJMGAtsN6lquqGWO-ra9GtVZiTV_CMF 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-07-01_21,2024-07-01_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=507 priorityscore=1501 impostorscore=0 bulkscore=0 spamscore=0 mlxscore=0 clxscore=1015 phishscore=0 malwarescore=0 adultscore=0 lowpriorityscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2407010162 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 just a small optimization for the case where the real and imag parts are the same when lowering complex addition/subtraction. We only need to do the addition once when the real and imag parts are the same (on both sides of the operator). This gets done later on by FRE/PRE/DOM but having it done soon allows the cabs lowering to remove the sqrt and just change it to a multiply by a constant. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * tree-complex.cc (expand_complex_addition): If both operands have the same real and imag parts, only add the addition once. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/complex-8.c: New test. Signed-off-by: Andrew Pinski --- gcc/testsuite/gcc.dg/tree-ssa/complex-8.c | 12 ++++++++++++ gcc/tree-complex.cc | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/complex-8.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/complex-8.c b/gcc/testsuite/gcc.dg/tree-ssa/complex-8.c new file mode 100644 index 00000000000..a9636ff9e9a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/complex-8.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-cplxlower1-raw" } */ + +_Complex double f(double a, double c) +{ + _Complex double d = __builtin_complex (a, a); + d+=__builtin_complex(c, c); + return d; +} + +/* There should only be one plus as (a+c) is still (a+c) */ +/* { dg-final { scan-tree-dump-times "plus_expr, " 1 "cplxlower1" } } */ diff --git a/gcc/tree-complex.cc b/gcc/tree-complex.cc index 8a879acffca..dfebec18ec3 100644 --- a/gcc/tree-complex.cc +++ b/gcc/tree-complex.cc @@ -984,7 +984,12 @@ expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type, case PAIR (VARYING, VARYING): general: rr = gimple_build (&stmts, loc, code, inner_type, ar, br); - ri = gimple_build (&stmts, loc, code, inner_type, ai, bi); + /* (a+ai) + (b+bi) -> (a+b)+(a+b)i + small optimization to remove one new statement. */ + if (operand_equal_p (ar, ai) && operand_equal_p (br, bi)) + ri = rr; + else + ri = gimple_build (&stmts, loc, code, inner_type, ai, bi); break; default: From patchwork Mon Jul 1 21:46:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1954950 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=kORATi7n; 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 4WCfm56WxZz1xqb for ; Tue, 2 Jul 2024 07:47:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 365DE3810286 for ; Mon, 1 Jul 2024 21:47:48 +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 7C7BB3861878 for ; Mon, 1 Jul 2024 21:47:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C7BB3861878 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 7C7BB3861878 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=1719870433; cv=none; b=PbAL005JgMohTciZr8P6isGBcr4Eu5QjhWzNZDraU0yLVqgprldsmTMg3FUCkZpAWiKEqCNFPt1LHsCZur32K3Dl+B5D+K+Do70KbnszoTEDsmSZrDl+n7bvX9qw8T5P5h+HEV3k+52lt2viGT9dUE8mxijsIEGu6eIfcKfJJ+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719870433; c=relaxed/simple; bh=jrUv7lr8Gh5/KdTjU7nr0zbXQ5rZWDIlsY+z+LItbzg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aF7gi+f8fGXSzeF2iceSta6mI/zYLzVTQMJCCQxd6FhDF7I5vQuyZ4xRrbUnx/CFfPDb5wpy7ttWl39efCifJ7b/gk9J0JpaxM/x4j4lSL2nlkPBhSqubI5krMbrSFydFrKQZu/Tf9pYE1IkODu1rTA0AMQhSFeo1YRe+09ESnc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 461JA2uO019095 for ; Mon, 1 Jul 2024 21:47:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= mHSWd7MbMCYIhldvYow830uS7xm4nVUVBaXAEApo5y4=; b=kORATi7nQqGejWIC M4zV1Cj757ZLkTePi4IsN1hk8tcZRbY/0L/47qcuon+dyfWhV6vhy7yZhefp2RRr zQk3bTNzSMNZO1RAGnzVhQ7FOyxWC1U1DEDpT/CrNdSjFCxGCTFLFG2GGwdCqv33 O/ax6Qr21waqn5LZDsv+aNiLvoB2csf+KfNodLRwm7gg1qwtRKrv57LLx0MYcyif KSzsMuJEz1PytQcDNeYLEsmFCuPl0bCNbf9auMYDJHbkGFcL+XX5QMlppRsCOyj7 8VGJ06n8syr59hDG0fDyB3gZT10wDTbXkIuSbVeXWvVImps8Eg+768yfaIzvqAZm ZXcMIw== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 402an75dtp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 01 Jul 2024 21:47:09 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA03.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 461Ll8nR024385 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 1 Jul 2024 21:47:08 GMT Received: from hu-apinski-lv.qualcomm.com (10.80.80.8) 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, 1 Jul 2024 14:47:08 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 2/4] Move cabs expansion from powcabs to complex lowering [PR115710] Date: Mon, 1 Jul 2024 14:46:55 -0700 Message-ID: <20240701214657.3163066-3-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701214657.3163066-1-quic_apinski@quicinc.com> References: <20240701214657.3163066-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) 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: scijJ812anYZouK3AAxGtVqjPMsUrCvh X-Proofpoint-ORIG-GUID: scijJ812anYZouK3AAxGtVqjPMsUrCvh 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-07-01_21,2024-07-01_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxscore=0 clxscore=1015 adultscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 impostorscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2407010162 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 Expanding cabs in powcab might be too late as forwprop might recombine the load from a memory with the complex expr. Moving instead to complex lowering allows us to use directly the real/imag component from the loads instead. This allows for vectorization too. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/115710 gcc/ChangeLog: * tree-complex.cc (init_dont_simulate_again): Handle CABS. (gimple_expand_builtin_cabs): New function, moved mostly from tree-ssa-math-opts.cc. (expand_complex_operations_1): Call gimple_expand_builtin_cabs. * tree-ssa-math-opts.cc (gimple_expand_builtin_cabs): Remove. (build_and_insert_binop): Remove. (pass_data_expand_powcabs): Update comment. (pass_expand_powcabs::execute): Don't handle CABS. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/cabs-1.c: New test. * gcc.dg/tree-ssa/cabs-2.c: New test. * gfortran.dg/vect/pr115710.f90: New test. Signed-off-by: Andrew Pinski --- gcc/testsuite/gcc.dg/tree-ssa/cabs-1.c | 14 +++++ gcc/testsuite/gcc.dg/tree-ssa/cabs-2.c | 13 ++++ gcc/testsuite/gfortran.dg/vect/pr115710.f90 | 18 ++++++ gcc/tree-complex.cc | 68 +++++++++++++++++++- gcc/tree-ssa-math-opts.cc | 70 +-------------------- 5 files changed, 113 insertions(+), 70 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cabs-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cabs-2.c create mode 100644 gcc/testsuite/gfortran.dg/vect/pr115710.f90 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cabs-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cabs-1.c new file mode 100644 index 00000000000..12ff6049e63 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cabs-1.c @@ -0,0 +1,14 @@ +/* { dg-do compile { target sqrt_insn } } */ +/* { dg-options "-Ofast -fdump-tree-cplxlower1" } */ +/* { dg-add-options sqrt_insn } */ + + +double f(_Complex double a) +{ + a+= 1.0f; + return __builtin_cabs(a); +} + +/* Check that cabs is expanded during complex lowering. */ +/* { dg-final { scan-tree-dump-not "__builtin_cabs " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump "__builtin_sqrt " "cplxlower1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cabs-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cabs-2.c new file mode 100644 index 00000000000..efe3de90cba --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cabs-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-cplxlower1" } */ + + +double f(_Complex double a) +{ + a+= 1.0f; + return __builtin_cabs(a); +} + +/* Check that cabs is not expanded during complex lowering. */ +/* { dg-final { scan-tree-dump "__builtin_cabs " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_sqrt " "cplxlower1" } } */ diff --git a/gcc/testsuite/gfortran.dg/vect/pr115710.f90 b/gcc/testsuite/gfortran.dg/vect/pr115710.f90 new file mode 100644 index 00000000000..3749210ac80 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/vect/pr115710.f90 @@ -0,0 +1,18 @@ +! { dg-do compile } +! { dg-additional-options "-Ofast" } +! { dg-require-effective-target vect_float } +! { dg-require-effective-target vect_call_sqrtf } + +! { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } +! CABS expansion should allow for the vectorization to happen. + +subroutine foo(a,b,n) + complex(kind(1.0))::a(*) + real(kind(1.0))::b(*) + integer::i,n + + do i=1,n + b(i)=abs(a(i))**2 + end do + +end subroutine foo diff --git a/gcc/tree-complex.cc b/gcc/tree-complex.cc index dfebec18ec3..d1276dc1c2f 100644 --- a/gcc/tree-complex.cc +++ b/gcc/tree-complex.cc @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #include "system.h" #include "coretypes.h" #include "backend.h" +#include "target.h" #include "rtl.h" #include "tree.h" #include "gimple.h" @@ -42,7 +43,9 @@ along with GCC; see the file COPYING3. If not see #include "cfganal.h" #include "gimple-fold.h" #include "diagnostic-core.h" - +#include "case-cfn-macros.h" +#include "builtins.h" +#include "optabs-tree.h" /* For each complex ssa name, a lattice value. We're interested in finding out whether a complex number is degenerate in some way, having only real @@ -238,7 +241,18 @@ init_dont_simulate_again (void) { case GIMPLE_CALL: if (gimple_call_lhs (stmt)) - sim_again_p = is_complex_reg (gimple_call_lhs (stmt)); + { + sim_again_p = is_complex_reg (gimple_call_lhs (stmt)); + switch (gimple_call_combined_fn (stmt)) + { + CASE_CFN_CABS: + /* Expand cabs only if unsafe math and optimizing. */ + if (optimize && flag_unsafe_math_optimizations) + saw_a_complex_op = true; + break; + default:; + } + } break; case GIMPLE_ASSIGN: @@ -1686,6 +1700,46 @@ expand_complex_asm (gimple_stmt_iterator *gsi) } } + +/* ARG is the argument to a cabs builtin call in GSI with location info + LOC. Create a sequence of statements prior to GSI that calculates + sqrt(R*R + I*I), where R and I are the real and imaginary components + of ARG, respectively. */ + +static void +gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, gimple *old_stmt) +{ + tree real_part, imag_part, addend1, addend2, sum; + tree arg = gimple_call_arg (old_stmt, 0); + tree type = TREE_TYPE (TREE_TYPE (arg)); + tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); + machine_mode mode = TYPE_MODE (type); + gimple *new_stmt; + + if (!flag_unsafe_math_optimizations + || !optimize_bb_for_speed_p (gimple_bb (old_stmt)) + || !sqrtfn + || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing) + return; + + real_part = extract_component (gsi, arg, false, true); + imag_part = extract_component (gsi, arg, true, true); + location_t loc = gimple_location (old_stmt); + + gimple_seq stmts = NULL; + addend1 = gimple_build (&stmts, loc, MULT_EXPR, type, real_part, real_part); + addend2 = gimple_build (&stmts, loc, MULT_EXPR, type, imag_part, imag_part); + sum = gimple_build (&stmts, loc, PLUS_EXPR, type, addend1, addend2); + gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); + + /* Build the sqrt call. */ + new_stmt = gimple_build_call (sqrtfn, 1, sum); + gimple_set_location (new_stmt, loc); + tree lhs = gimple_call_lhs (old_stmt); + gimple_call_set_lhs (new_stmt, lhs); + gsi_replace (gsi, new_stmt, true); +} + /* Process one statement. If we identify a complex operation, expand it. */ static void @@ -1696,6 +1750,16 @@ expand_complex_operations_1 (gimple_stmt_iterator *gsi) tree ac, ar, ai, bc, br, bi; complex_lattice_t al, bl; enum tree_code code; + if (gimple_code (stmt) == GIMPLE_CALL) + { + switch (gimple_call_combined_fn (stmt)) + { + CASE_CFN_CABS: + gimple_expand_builtin_cabs (gsi, stmt); + return; + default:; + } + } if (gimple_code (stmt) == GIMPLE_ASM) { diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 3b5433ec000..71f896a9790 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -1622,22 +1622,6 @@ build_and_insert_binop (gimple_stmt_iterator *gsi, location_t loc, return result; } -/* Build a gimple reference operation with the given CODE and argument - ARG, assigning the result to a new SSA name of TYPE with NAME. - Insert the statement prior to GSI's current position, and return - the fresh SSA name. */ - -static inline tree -build_and_insert_ref (gimple_stmt_iterator *gsi, location_t loc, tree type, - const char *name, enum tree_code code, tree arg0) -{ - tree result = make_temp_ssa_name (type, NULL, name); - gimple *stmt = gimple_build_assign (result, build1 (code, type, arg0)); - gimple_set_location (stmt, loc); - gsi_insert_before (gsi, stmt, GSI_SAME_STMT); - return result; -} - /* Build a gimple assignment to cast VAL to TYPE. Insert the statement prior to GSI's current position, and return the fresh SSA name. */ @@ -2193,39 +2177,6 @@ gimple_expand_builtin_pow (gimple_stmt_iterator *gsi, location_t loc, return NULL_TREE; } -/* ARG is the argument to a cabs builtin call in GSI with location info - LOC. Create a sequence of statements prior to GSI that calculates - sqrt(R*R + I*I), where R and I are the real and imaginary components - of ARG, respectively. Return an expression holding the result. */ - -static tree -gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, location_t loc, tree arg) -{ - tree real_part, imag_part, addend1, addend2, sum, result; - tree type = TREE_TYPE (TREE_TYPE (arg)); - tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); - machine_mode mode = TYPE_MODE (type); - - if (!flag_unsafe_math_optimizations - || !optimize_bb_for_speed_p (gimple_bb (gsi_stmt (*gsi))) - || !sqrtfn - || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing) - return NULL_TREE; - - real_part = build_and_insert_ref (gsi, loc, type, "cabs", - REALPART_EXPR, arg); - addend1 = build_and_insert_binop (gsi, loc, "cabs", MULT_EXPR, - real_part, real_part); - imag_part = build_and_insert_ref (gsi, loc, type, "cabs", - IMAGPART_EXPR, arg); - addend2 = build_and_insert_binop (gsi, loc, "cabs", MULT_EXPR, - imag_part, imag_part); - sum = build_and_insert_binop (gsi, loc, "cabs", PLUS_EXPR, addend1, addend2); - result = build_and_insert_call (gsi, loc, sqrtfn, sum); - - return result; -} - /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1 on the SSA_NAME argument of each of them. */ @@ -2322,7 +2273,8 @@ make_pass_cse_sincos (gcc::context *ctxt) } /* Expand powi(x,n) into an optimal number of multiplies, when n is a constant. - Also expand CABS. */ + Note the name is powcabs but cabs expansion was moved to the lower complex + pass. */ namespace { const pass_data pass_data_expand_powcabs = @@ -2455,24 +2407,6 @@ pass_expand_powcabs::execute (function *fun) } break; - CASE_CFN_CABS: - arg0 = gimple_call_arg (stmt, 0); - loc = gimple_location (stmt); - result = gimple_expand_builtin_cabs (&gsi, loc, arg0); - - if (result) - { - tree lhs = gimple_get_lhs (stmt); - gassign *new_stmt = gimple_build_assign (lhs, result); - gimple_set_location (new_stmt, loc); - unlink_stmt_vdef (stmt); - gsi_replace (&gsi, new_stmt, true); - cleanup_eh = true; - if (gimple_vdef (stmt)) - release_ssa_name (gimple_vdef (stmt)); - } - break; - default:; } } From patchwork Mon Jul 1 21:46:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1954952 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=N50lHVBk; 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 4WCfn145Bxz1xqd for ; Tue, 2 Jul 2024 07:48:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CDD3938114D3 for ; Mon, 1 Jul 2024 21:48:35 +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 BA2D43870899 for ; Mon, 1 Jul 2024 21:47:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA2D43870899 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 BA2D43870899 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=1719870433; cv=none; b=PRl7uIuufeZGAdFU2Q76ylcuNxDkQ6oYUfEh5ON9JxTAGF1p+P2ShUAVQH6uzEeCo0h7ZzQsWi75oZA7C4NS1gFcRhdM9T+xTnGWyfRnGlrtTKg60B2+AuALAejz223xlPzh9Edg88qTse2xQxribJCb1lQUMk6IPhC+Q6SG7tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719870433; c=relaxed/simple; bh=BXSf6up4zdJoaz9oNDVEED3Rvbp2GBwW0r73CnOaTDo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NdOnurkobKOpZCgbSTOAsl3/iuLiQjOZHMvgrp7hRckGkgHgo+CU7kz0DJ3t6YCldsWNgSghSrIyhS5HH48J6wuZWua5EzI8ILS7LVdatPv3IPOQ1ArFS7kJeZwip3qMOPN2YnTRDV/JQT3ZIa3hCzSIRCcFkV0x6ihyyBpEES0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 461JBQLW009329 for ; Mon, 1 Jul 2024 21:47:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= UzKL5felnHuej4U064Wiw16KN/vTk1m2FCA5NJeegAE=; b=N50lHVBku3IxVANt nOyn5Mrl/NAnh0F9nkUIH/pMjp5o31nfoHWVm8I4yEJANZMVnh/9yRd1j91jsGq2 KGPtYzfzhNaRG7y4mgzUQyYO6VPDXi49fFJQgDxtLpn+rOgIAy3WKIjNHVWEuXlh qNgw/X9/CIy9W5tohMOE1vMR+Dde4TTBQRfm3pgXdM1g8bOYhHms2BQW8Vs5D3N4 8B0mkZpXbF7L9oOBZnJ7U84mshPgJQva8OxZdV+tpRM4mx2VR5iSpzctbOWttREI F9yF85f4GJd8lW8lF3ymvbTeYr/0XrYplCQbulaRlyZ/FnM48XMSozrENrCbqOGI 4cAWBA== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 402abtnfaw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 01 Jul 2024 21:47:10 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 461Ll92a015926 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 1 Jul 2024 21:47:09 GMT Received: from hu-apinski-lv.qualcomm.com (10.80.80.8) 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, 1 Jul 2024 14:47:08 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 3/4] Add some optimizations to gimple_expand_builtin_cabs Date: Mon, 1 Jul 2024 14:46:56 -0700 Message-ID: <20240701214657.3163066-4-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701214657.3163066-1-quic_apinski@quicinc.com> References: <20240701214657.3163066-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) 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: FeAcse2wuFKxLbbEt7hl72cV6MTyGiE7 X-Proofpoint-ORIG-GUID: FeAcse2wuFKxLbbEt7hl72cV6MTyGiE7 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-07-01_21,2024-07-01_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 priorityscore=1501 mlxscore=0 clxscore=1015 suspectscore=0 malwarescore=0 lowpriorityscore=0 phishscore=0 adultscore=0 spamscore=0 impostorscore=0 mlxlogscore=826 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2407010162 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 While looking into the original folding code for cabs (moved to match in r6-4111-gabcc43f5323869), I noticed that `cabs(x+0i)` was optimized even without the need of sqrt. I also noticed that now the code generation in this case will be worse if the target had a sqrt. So let's implement this small optimizations in gimple_expand_builtin_cabs. Note `cabs(x+0i)` is done without unsafe math optimizations. This is because the definition of `cabs(x+0i)` is `hypot(x, 0)` and the definition in the standard says that just returns `abs(x)`. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * tree-complex.cc (gimple_expand_builtin_cabs): Add `cabs(a+ai)`, `cabs(x+0i)` and `cabs(0+xi)` optimizations. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/cabs-3.c: New test. * gcc.dg/tree-ssa/cabs-4.c: New test. * gcc.dg/tree-ssa/cabs-5.c: New test. * gcc.dg/tree-ssa/cabs-6.c: New test. Signed-off-by: Andrew Pinski Fix optimizations Signed-off-by: Andrew Pinski --- gcc/testsuite/gcc.dg/tree-ssa/cabs-3.c | 24 +++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cabs-4.c | 16 ++++++++ gcc/testsuite/gcc.dg/tree-ssa/cabs-5.c | 22 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cabs-6.c | 16 ++++++++ gcc/tree-complex.cc | 56 +++++++++++++++++++++----- 5 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cabs-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cabs-4.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cabs-5.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cabs-6.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cabs-3.c b/gcc/testsuite/gcc.dg/tree-ssa/cabs-3.c new file mode 100644 index 00000000000..976c0169131 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cabs-3.c @@ -0,0 +1,24 @@ +/* { dg-do compile { target sqrt_insn } } */ +/* { dg-options "-Ofast -fdump-tree-cplxlower1" } */ +/* { dg-add-options sqrt_insn } */ + + +double f(double a, double c) +{ + _Complex double b = a; + b+= c; + return __builtin_cabs(b); +} + +double f1(double a, double c) +{ + _Complex double b = __builtin_complex(0.0, a); + b+= __builtin_complex(0.0, c); + return __builtin_cabs(b); +} + +/* Check that cabs is expanded during complex lowering. */ +/* { dg-final { scan-tree-dump-not "__builtin_cabs " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_sqrt " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "cplxlower1" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cabs-4.c b/gcc/testsuite/gcc.dg/tree-ssa/cabs-4.c new file mode 100644 index 00000000000..00aa3c9d4e7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cabs-4.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-cplxlower1" } */ + +double f(double a, double c) +{ + _Complex double d = __builtin_complex (a, a); + d+=__builtin_complex(1.0, 1.0); + return __builtin_cabs(d); +} + +/* Check that cabs is expanded during complex lowering and there is no sqrt (since it is a constant). */ +/* { dg-final { scan-tree-dump-not "__builtin_cabs " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_sqrt " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "cplxlower1" } } */ + + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cabs-5.c b/gcc/testsuite/gcc.dg/tree-ssa/cabs-5.c new file mode 100644 index 00000000000..dd794079921 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cabs-5.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-cplxlower1" } */ + +double f(double a, double c) +{ + _Complex double b = a; + b+= c; + return __builtin_cabs(b); +} + +double f1(double a, double c) +{ + _Complex double b = __builtin_complex(0.0, a); + b+= __builtin_complex(0.0, c); + return __builtin_cabs(b); +} + +/* Check that cabs is expanded into ABS for both f and f1 during complex lowering. */ +/* { dg-final { scan-tree-dump-not "__builtin_cabs " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_sqrt " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "cplxlower1" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cabs-6.c b/gcc/testsuite/gcc.dg/tree-ssa/cabs-6.c new file mode 100644 index 00000000000..bc88932449c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cabs-6.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-cplxlower1" } */ + +double f(double a, double c) +{ + _Complex double d = __builtin_complex (a, 0.0); + d+=__builtin_complex(0.0, a); + return __builtin_cabs(d); +} + +/* Check that cabs is expanded during complex lowering and there is no sqrt (since it is a constant). */ +/* { dg-final { scan-tree-dump-not "__builtin_cabs " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_sqrt " "cplxlower1" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "cplxlower1" } } */ + + diff --git a/gcc/tree-complex.cc b/gcc/tree-complex.cc index d1276dc1c2f..d2d9d76827d 100644 --- a/gcc/tree-complex.cc +++ b/gcc/tree-complex.cc @@ -1701,10 +1701,10 @@ expand_complex_asm (gimple_stmt_iterator *gsi) } -/* ARG is the argument to a cabs builtin call in GSI with location info - LOC. Create a sequence of statements prior to GSI that calculates - sqrt(R*R + I*I), where R and I are the real and imaginary components - of ARG, respectively. */ +/* ARG is the argument to a cabs builtin call in GSI from the + original OLD_STMT. Create a sequence of statements prior + to GSI that calculates sqrt(R*R + I*I), where R and + I are the real and imaginary components of ARG, respectively. */ static void gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, gimple *old_stmt) @@ -1712,21 +1712,56 @@ gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, gimple *old_stmt) tree real_part, imag_part, addend1, addend2, sum; tree arg = gimple_call_arg (old_stmt, 0); tree type = TREE_TYPE (TREE_TYPE (arg)); - tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); machine_mode mode = TYPE_MODE (type); gimple *new_stmt; - if (!flag_unsafe_math_optimizations - || !optimize_bb_for_speed_p (gimple_bb (old_stmt)) - || !sqrtfn - || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing) - return; + tree lhs = gimple_call_lhs (old_stmt); real_part = extract_component (gsi, arg, false, true); imag_part = extract_component (gsi, arg, true, true); location_t loc = gimple_location (old_stmt); gimple_seq stmts = NULL; + + /* cabs(x+0i) -> abs(x). + cabs(0+xi) -> abs(x). + These 2 can be done even without unsafe math optimizations. */ + if (real_zerop (imag_part) + || real_zerop (real_part)) + { + tree other = real_zerop (imag_part) ? real_part : imag_part; + sum = gimple_build (&stmts, loc, ABS_EXPR, type, other); + gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); + new_stmt = gimple_build_assign (lhs, sum); + gimple_set_location (new_stmt, loc); + gsi_replace (gsi, new_stmt, true); + return; + } + + if (!flag_unsafe_math_optimizations) + return; + + /* cabs(x+xi) -> fabs(x)*sqrt(2). */ + if (operand_equal_p (real_part, imag_part)) + { + tree sqrt2 = build_real_truncate (type, dconst_sqrt2 ()); + sum = gimple_build (&stmts, loc, ABS_EXPR, type, real_part); + sum = gimple_build (&stmts, loc, MULT_EXPR, type, sum, sqrt2); + gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); + new_stmt = gimple_build_assign (lhs, sum); + gimple_set_location (new_stmt, loc); + gsi_replace (gsi, new_stmt, true); + return; + } + + /* cabs(a+bi) -> sqrt(a*a+b*b) if sqrt exists on the target + and optimizing for speed. */ + tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); + if (!optimize_bb_for_speed_p (gimple_bb (old_stmt)) + || !sqrtfn + || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing) + return; + addend1 = gimple_build (&stmts, loc, MULT_EXPR, type, real_part, real_part); addend2 = gimple_build (&stmts, loc, MULT_EXPR, type, imag_part, imag_part); sum = gimple_build (&stmts, loc, PLUS_EXPR, type, addend1, addend2); @@ -1735,7 +1770,6 @@ gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, gimple *old_stmt) /* Build the sqrt call. */ new_stmt = gimple_build_call (sqrtfn, 1, sum); gimple_set_location (new_stmt, loc); - tree lhs = gimple_call_lhs (old_stmt); gimple_call_set_lhs (new_stmt, lhs); gsi_replace (gsi, new_stmt, true); } From patchwork Mon Jul 1 21:46: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: 1954951 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=OCKBtYbi; 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 4WCfn11j4rz1xqb for ; Tue, 2 Jul 2024 07:48:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7DF9D3882046 for ; Mon, 1 Jul 2024 21:48:35 +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 2524138708DA for ; Mon, 1 Jul 2024 21:47:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2524138708DA 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 2524138708DA 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=1719870433; cv=none; b=WiIh1EQTike/s0ODKYmyOwL4kBCO3XBdaK0AFlJaFY76YUnP/flcfI8dRBBlTDhMOisAKXFMwQZOr2V+RIYIZe/786qO5fMT+XzUwEOiPUTweMyeeqQBMPM5N58/DK0+UpJohXvGiD8xidIlob6kbjaq18gLN35laNCprjQp3xs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719870433; c=relaxed/simple; bh=yxKpBynkX+y80mh9saxSrS9Wg+Q+G2zOizh+qLO0/F8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jjT3sVKeygmoLz8Ha2MpiXWPJSGx1UdRs9aZSPevrfKA9NhWmqaGZ/YPUoxhz1yLI+R0I0rWLCbOjM7a4RFoPDVK+Ld9+uU2QIXM25ThZnQlEyZNMSucUaApitljjYk+KKn9vyjoL2aZciA6gaZJrF0MuSboR9926VksayTZbIs= 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 461J8AtJ023893 for ; Mon, 1 Jul 2024 21:47:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= mV+MXRLMbdQq9MXgxpO58G/PJWz/59FXyxOp3U0TByw=; b=OCKBtYbitH9/n/4i NTgEqMMSpzFxpNXPHY7PGFZ/F1AsVR9m/6F2/CGES2839jbUMBmyLtHvq5Ds2bDn LxRTFkNKy3bAaQev52odPk4odYFdKgFtjWK7BXlb7HnDcdcm6jcJFbAvVlDypFXC bMC/YH5nzfgFngZuoD/cz7vFgrxFx3hlxEp/kQ9hpMi9XQgGHPVJu0RM+ORmqvmr pjqbVgxG6lYqUo2RJiLR+cQQwOldQZHgveMU9TKbCoE1J+wZ8yU7B3ROKePjLBti a/0TMCvcZk2EobixZeLKo/ZCkCQGl+BKSpMfgEORjfJBMattOI3UKol2OmFZGONo 5Xe7JQ== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4027mnnrr1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 01 Jul 2024 21:47:10 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA05.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 461Ll9bY012650 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 1 Jul 2024 21:47:09 GMT Received: from hu-apinski-lv.qualcomm.com (10.80.80.8) 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, 1 Jul 2024 14:47:09 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 4/4] Rename expand_powcabs pass to expand_pow Date: Mon, 1 Jul 2024 14:46:57 -0700 Message-ID: <20240701214657.3163066-5-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701214657.3163066-1-quic_apinski@quicinc.com> References: <20240701214657.3163066-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) 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: f_4gsopJbBwhk2LhKSl56EjN5FCscOG0 X-Proofpoint-GUID: f_4gsopJbBwhk2LhKSl56EjN5FCscOG0 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-07-01_21,2024-07-01_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 bulkscore=0 spamscore=0 malwarescore=0 adultscore=0 priorityscore=1501 lowpriorityscore=0 phishscore=0 mlxscore=0 suspectscore=0 clxscore=1011 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2407010162 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 Since cabs expansion was removed from this pass, it would be good to rename it. Bootstrapped and tested on x86_64-linux-gnu gcc/ChangeLog: * passes.def (expand_pow): Renamed from expand_powcabs. * timevar.def (TV_TREE_POWCABS): Remove. (TV_TREE_POW): Add * tree-pass.h (make_pass_expand_powcabs): Rename to ... (make_pass_expand_pow): This. * tree-ssa-math-opts.cc (class pass_expand_powcabs): Rename to ... (class pass_expand_pow): This. (pass_expand_powcabs::execute): Rename to ... (pass_expand_pow::execute): This. (make_pass_expand_powcabs): Rename to ... (make_pass_expand_pow): This. gcc/testsuite/ChangeLog: * gcc.dg/pow-sqrt-synth-1.c: Update testcase for renamed pass. Signed-off-by: Andrew Pinski --- gcc/passes.def | 2 +- gcc/testsuite/gcc.dg/pow-sqrt-synth-1.c | 4 ++-- gcc/timevar.def | 2 +- gcc/tree-pass.h | 2 +- gcc/tree-ssa-math-opts.cc | 25 ++++++++++++------------- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/gcc/passes.def b/gcc/passes.def index 13c9dc34ddf..b8c21b1e435 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -265,7 +265,7 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_ccp, true /* nonzero_p */); /* After CCP we rewrite no longer addressed locals into SSA form if possible. */ - NEXT_PASS (pass_expand_powcabs); + NEXT_PASS (pass_expand_pow); NEXT_PASS (pass_optimize_bswap); NEXT_PASS (pass_laddress); NEXT_PASS (pass_lim); diff --git a/gcc/testsuite/gcc.dg/pow-sqrt-synth-1.c b/gcc/testsuite/gcc.dg/pow-sqrt-synth-1.c index 484b29a8fc8..be81e43ad9a 100644 --- a/gcc/testsuite/gcc.dg/pow-sqrt-synth-1.c +++ b/gcc/testsuite/gcc.dg/pow-sqrt-synth-1.c @@ -1,5 +1,5 @@ /* { dg-do compile { target sqrt_insn } } */ -/* { dg-options "-fdump-tree-powcabs -Ofast --param max-pow-sqrt-depth=8" } */ +/* { dg-options "-fdump-tree-pow -Ofast --param max-pow-sqrt-depth=8" } */ /* { dg-additional-options "-mfloat-abi=softfp -mfpu=neon-vfpv4" { target arm*-*-* } } */ double @@ -34,4 +34,4 @@ vecfoo (double *a) a[i] = __builtin_pow (a[i], 1.25); } -/* { dg-final { scan-tree-dump-times "synthesizing" 7 "powcabs" } } */ +/* { dg-final { scan-tree-dump-times "synthesizing" 7 "pow" } } */ diff --git a/gcc/timevar.def b/gcc/timevar.def index 6fc36859138..0f9d2c0b032 100644 --- a/gcc/timevar.def +++ b/gcc/timevar.def @@ -223,7 +223,7 @@ DEFTIMEVAR (TV_TREE_SWITCH_CONVERSION, "tree switch conversion") DEFTIMEVAR (TV_TREE_SWITCH_LOWERING, "tree switch lowering") DEFTIMEVAR (TV_TREE_RECIP , "gimple CSE reciprocals") DEFTIMEVAR (TV_TREE_SINCOS , "gimple CSE sin/cos") -DEFTIMEVAR (TV_TREE_POWCABS , "gimple expand pow/cabs") +DEFTIMEVAR (TV_TREE_POW , "gimple expand pow") DEFTIMEVAR (TV_TREE_WIDEN_MUL , "gimple widening/fma detection") DEFTIMEVAR (TV_TRANS_MEM , "transactional memory") DEFTIMEVAR (TV_TREE_STRLEN , "tree strlen optimization") diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h index 38902b1b01b..9843d189d27 100644 --- a/gcc/tree-pass.h +++ b/gcc/tree-pass.h @@ -451,7 +451,7 @@ extern gimple_opt_pass *make_pass_early_warn_uninitialized (gcc::context *ctxt); extern gimple_opt_pass *make_pass_late_warn_uninitialized (gcc::context *ctxt); extern gimple_opt_pass *make_pass_cse_reciprocals (gcc::context *ctxt); extern gimple_opt_pass *make_pass_cse_sincos (gcc::context *ctxt); -extern gimple_opt_pass *make_pass_expand_powcabs (gcc::context *ctxt); +extern gimple_opt_pass *make_pass_expand_pow (gcc::context *ctxt); extern gimple_opt_pass *make_pass_optimize_bswap (gcc::context *ctxt); extern gimple_opt_pass *make_pass_store_merging (gcc::context *ctxt); extern gimple_opt_pass *make_pass_optimize_widening_mul (gcc::context *ctxt); diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 71f896a9790..a35caf5f058 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -2272,17 +2272,16 @@ make_pass_cse_sincos (gcc::context *ctxt) return new pass_cse_sincos (ctxt); } -/* Expand powi(x,n) into an optimal number of multiplies, when n is a constant. - Note the name is powcabs but cabs expansion was moved to the lower complex - pass. */ +/* Expand powi(x,n) into an optimal number of multiplies, when n is a + constant. */ namespace { -const pass_data pass_data_expand_powcabs = +const pass_data pass_data_expand_pow = { GIMPLE_PASS, /* type */ - "powcabs", /* name */ + "pow", /* name */ OPTGROUP_NONE, /* optinfo_flags */ - TV_TREE_POWCABS, /* tv_id */ + TV_TREE_POW, /* tv_id */ PROP_ssa, /* properties_required */ PROP_gimple_opt_math, /* properties_provided */ 0, /* properties_destroyed */ @@ -2290,11 +2289,11 @@ const pass_data pass_data_expand_powcabs = TODO_update_ssa, /* todo_flags_finish */ }; -class pass_expand_powcabs : public gimple_opt_pass +class pass_expand_pow : public gimple_opt_pass { public: - pass_expand_powcabs (gcc::context *ctxt) - : gimple_opt_pass (pass_data_expand_powcabs, ctxt) + pass_expand_pow (gcc::context *ctxt) + : gimple_opt_pass (pass_data_expand_pow, ctxt) {} /* opt_pass methods: */ @@ -2305,10 +2304,10 @@ public: unsigned int execute (function *) final override; -}; // class pass_expand_powcabs +}; // class pass_expand_pow unsigned int -pass_expand_powcabs::execute (function *fun) +pass_expand_pow::execute (function *fun) { basic_block bb; bool cfg_changed = false; @@ -2421,9 +2420,9 @@ pass_expand_powcabs::execute (function *fun) } // anon namespace gimple_opt_pass * -make_pass_expand_powcabs (gcc::context *ctxt) +make_pass_expand_pow (gcc::context *ctxt) { - return new pass_expand_powcabs (ctxt); + return new pass_expand_pow (ctxt); } /* Return true if stmt is a type conversion operation that can be stripped