From patchwork Mon Sep 16 08:11:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xianmiao Qu X-Patchwork-Id: 1985995 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=JiL0Ox+C; 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 4X6d141MKGz1y1m for ; Mon, 16 Sep 2024 18:11:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0C1563858C3A for ; Mon, 16 Sep 2024 08:11:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) by sourceware.org (Postfix) with ESMTPS id 260593858C33 for ; Mon, 16 Sep 2024 08:11:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 260593858C33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.alibaba.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 260593858C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=115.124.30.98 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726474290; cv=none; b=xfRDGU33syEMSNDnrA2qQmAjaR4032M0bzNOTo2PFAA2doB6T5cFW1vfvZib9IoYIeynqi8YAzb/pfZeb7dRuIzO1VzP4m0uBdvahc2TvI5WqfFGkIBqUGi4GUEnbVUk++Q9dW8cdXVnjY2IpInQrEt27ukbaBrAmgqbj8Ixf0w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726474290; c=relaxed/simple; bh=qcycODqqOGQGWj87ByieOfrINh9iCvtgziZL+kkNkvs=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=CZnzX2YJRQDK+OESBXjJRQ2TiN6aTWbfwXrLGK8JUe28XNGyk9lvp973zYTS4RvyrQTY8+AEjBB98Zn0kASS/j1kaHnoZ90D2ESCyrTzti0R70TyWfj+TAQ+eZM/pzMD+4WCjBUZdQB8clzUWKX4DhOOicQlCvdZRtDVV4V6WEY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1726474285; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Cy2+1+zvHj/xsbiMfksyAAMI1v+MEq4DcvfMHj52K5s=; b=JiL0Ox+CCllsIITm0Hz84tdovKuQ+NCtsBxeG75dTFoxB5KDEpkTmnysn4drxPIQe1Uyy+3FHiovxRtYbwZNvB77OB/4jnaRd1qbCPnC0UVN2ZZhrZ3w0F55zMw/GIZFXodita74F3UHUMPZiQ8rd96uJkDPjdvJp4S8hYU5Zz4= Received: from localhost(mailfrom:cooper.qu@linux.alibaba.com fp:SMTPD_---0WF3Kt1V_1726474282) by smtp.aliyun-inc.com; Mon, 16 Sep 2024 16:11:22 +0800 From: Xianmiao Qu To: gcc-patches@gcc.gnu.org, jeffreyalaw@gmail.com, kito.cheng@gmail.com Cc: Xianmiao Qu Subject: [PATCH 2/2] RISC-V: Optimize the cost of the LO_SUM expression. Date: Mon, 16 Sep 2024 16:11:21 +0800 Message-Id: <20240916081121.29707-1-cooper.qu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) MIME-Version: 1.0 X-Spam-Status: No, score=-27.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, ENV_AND_HDR_SPF_MATCH, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, UNPARSEABLE_RELAY, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL 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 Currently, the cost of the LO_SUM expression is based on the cost of calculating the first subexpression. When the first subexpression is a register, the cost result will be zero. It seems a bit unreasonable for a SET expression to have a zero cost when its source is LO_SUM. Moreover, having a cost of zero for the expression will lead the loop invariant pass to calculate its benefits of being moved outside the loop as zero, thus preventing the out-of-loop placement of the loop invariant. As an example, consider the following test case: long a; long b[]; long *c; foo () { for (;;) *c = b[a]; } When compiling with -march=rv64gc -mabi=lp64d -Os, the following code is generated: .cfi_startproc lui a5,%hi(c) ld a4,%lo(c)(a5) lui a2,%hi(b) lui a1,%hi(a) .L2: ld a5,%lo(a)(a1) addi a3,a2,%lo(b) slli a5,a5,3 add a5,a5,a3 ld a5,0(a5) sd a5,0(a4) j .L2 After adjust the cost of the LO_SUM expression, the instruction addi will be moved outside the loop: .cfi_startproc lui a5,%hi(c) ld a3,%lo(c)(a5) lui a4,%hi(b) lui a2,%hi(a) addi a4,a4,%lo(b) .L2: ld a5,%lo(a)(a2) slli a5,a5,3 add a5,a5,a4 ld a5,0(a5) sd a5,0(a3) j .L2 gcc/ * config/riscv/riscv.cc (riscv_rtx_costs): Optimize the cost of the LO_SUM expression. --- gcc/config/riscv/riscv.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 1b4a5c39c667..cb9692dad715 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -3994,7 +3994,10 @@ riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UN return false; case LO_SUM: - *total = set_src_cost (XEXP (x, 0), mode, speed); + if (outer_code == SET && REG_P (XEXP (x, 0))) + *total = COSTS_N_INSNS (1); + else + *total = set_src_cost (XEXP (x, 0), mode, speed); return true; case LT: