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); }