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: 93157 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7AAE6389ECA0 for ; Mon, 1 Jul 2024 21:47:56 +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~patchwork=sourceware.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:; } }