From patchwork Tue Nov 8 19:54:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 17191 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2917222wru; Tue, 8 Nov 2022 11:55:37 -0800 (PST) X-Google-Smtp-Source: AMsMyM5YpSESjM0b3LfSfhHG9xcitmB9ooLtGNRIXGpoc48PHuqNbcLwMhmRDAeeLoH6t/rzJbEO X-Received: by 2002:a17:907:da0:b0:7ad:9760:e1d2 with SMTP id go32-20020a1709070da000b007ad9760e1d2mr53052185ejc.419.1667937336951; Tue, 08 Nov 2022 11:55:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667937336; cv=none; d=google.com; s=arc-20160816; b=LozIkKZ93ygWvrRBf5/IYgPsK/G3HRvTrfPzROBgcqY0v1paqQwdQ+srvP0hNi+YB+ rQbj2nJBc7zdr5qp2Y2ileykNL+aNi0KERGQZJ30yhdQFjfUXM/byjvJoRmhKNylHgiG txFZ4H50Y/9N50vTD0C/IBU28mbEl6FLuy1M+y4vfdC3dftq6NjkqZh75XdxJYhzRno2 QPSTyapzeyKTPCJ6iCD1PDGEts7JZaF97mTwXJxob3GBbiz3hc24LbADBg47/TrWSIC5 YklHeKpYfitkT/Y7htEjD5EvALKYqw+hal+UFZCvH4kxhobAVAKhEdgBj4uCa2MIemtV yuTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=N8jMqa/NOrP4ZzfDi8dAtjtERecSw1Rr2L1UQGK7810=; b=aK09HbcG8/7deq5HuQr572obpQbfXGFdKhN5cSz6AoA2XZIuk+kBayQDQwfAaFS8GZ s9y04aXxv0nBUJ2eDbdp353xI3h4uC176QQ5CBSDeLx0Iwgn3nL4XKMBfZ69V/YQkBxN KgkK4mfA/s9k4TqRMDE8ShhoexhQzMnowOiqEnOgn+R6Q11FLxQlTObwP8XuPpHl45+Q ZWPrv/xPUnb/smrmZsujgUesGi4qoofLYJgRL+YkevNIt7q4Ei6gMe6uRsKOepqU+bT5 AuVSxNBVylolP5FMCLYUbTyErK4aymiJSsDrMYaHTQhUMpFOFanVE7ksy6OtHocs+8ke unfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=I30ynGey; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g16-20020a056402321000b0046277d2cb0csi13763517eda.470.2022.11.08.11.55.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 11:55:36 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=I30ynGey; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E814E3857422 for ; Tue, 8 Nov 2022 19:55:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 51FF1385800B for ; Tue, 8 Nov 2022 19:54:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 51FF1385800B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-lf1-x12b.google.com with SMTP id r12so22711880lfp.1 for ; Tue, 08 Nov 2022 11:54:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=N8jMqa/NOrP4ZzfDi8dAtjtERecSw1Rr2L1UQGK7810=; b=I30ynGeyxAPgeYSDPP1Lty0L3gE6V8ZBrZm3q8KTUuJ9/qE8+lQraqKQrkqWhEzAS1 mqESiGdf6gU0FaxheC12rvb2SBpginZNzzh4hRjVUJkDypBVVFdx7hejw6JIBIHmclez U/AQ44gZlT2Y9Glx4aOlMbeNbUfuJvX4VxkKLrBhX8CpF74/ZCRBErOSX7hEqlPO3X1L xWueN6S5MJEIAXk+zQahCeWwRnohdmc3R95N8DvgkpjVcO0kJhqCC5Olt0xvTDvXZL3U aQ3SGf59lVMIXMRHtkWBscayLsKh+qMf3cMi4Djxazx9012PwEq7WPxQx95aQVcmHUBX 17eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=N8jMqa/NOrP4ZzfDi8dAtjtERecSw1Rr2L1UQGK7810=; b=BHbxL7vEAWvRjEyfrUIYw5Q4dutdl2kfGebIK72ZFJeBaSYJT2WlMh5yNj3XEF0GFC iIDp7s+2BOKv9aTt1ifugiHk0q9qUlno73xB7nD+XLvy/3WIT61I3YMkmOBHoIUrPmB+ SsmRCuCRquMDffQ/Kv7WbuyUo0WjX0PN7UeyuXRgjFjgZd7LGIG/+NZ4rlSx2uH2VdVy RzUDGOFM3iTCTa57KVC4D2UCohBLp6GFOhzG+N3ZnKDGjhQ+Jck8JeMRG8KJvtb9HpPp lX8gNfAfiSXosXG0nBzxXlBEpmSSAPytAbUyT8AIjq8WPpovHPLrjuKwMcdKjAYnvl8X F+hg== X-Gm-Message-State: ACrzQf0aNmgeORzn5kzKIjmCuMZesodgWiWJ/xVsNM8JHVMMtNIZrYEq uVe/omkd8uQvhxJxv3WpBY4aK1fm6s1mnlj4 X-Received: by 2002:ac2:4bc7:0:b0:4a4:6eb6:8da4 with SMTP id o7-20020ac24bc7000000b004a46eb68da4mr20574791lfq.84.1667937276484; Tue, 08 Nov 2022 11:54:36 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id h6-20020a197006000000b004a2386b8cf4sm1913615lfc.258.2022.11.08.11.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 11:54:36 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Kito Cheng , Vineet Gupta , Christoph Muellner , Palmer Dabbelt , Jeff Law , Philipp Tomsich Subject: [PATCH] RISC-V: costs: support shift-and-add in strength-reduction Date: Tue, 8 Nov 2022 20:54:34 +0100 Message-Id: <20221108195434.2701247-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748959061032937259?= X-GMAIL-MSGID: =?utf-8?q?1748959061032937259?= The strength-reduction implementation in expmed.c will assess the profitability of using shift-and-add using a RTL expression that wraps a MULT (with a power-of-2) in a PLUS. Unless the RISC-V rtx_costs function recognizes this as expressing a sh[123]add instruction, we will return an inflated cost---thus defeating the optimization. This change adds the necessary idiom recognition to provide an accurate cost for this for of expressing sh[123]add. Instead on expanding to li a5,200 mulw a0,a5,a0 with this change, the expression 'a * 200' is sythesized as: sh2add a0,a0,a0 // *5 = a + 4 * a sh2add a0,a0,a0 // *5 = a + 4 * a slli a0,a0,3 // *8 gcc/ChangeLog: * config/riscv/riscv.c (riscv_rtx_costs): Recognize shNadd, if expressed as a plus and multiplication with a power-of-2. --- gcc/config/riscv/riscv.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index ab6c745c722..0b2c4b3599d 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -2451,6 +2451,19 @@ riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UN *total = COSTS_N_INSNS (1); return true; } + /* Before strength-reduction, the shNadd can be expressed as the addition + of a multiplication with a power-of-two. If this case is not handled, + the strength-reduction in expmed.c will calculate an inflated cost. */ + if (TARGET_ZBA + && mode == word_mode + && GET_CODE (XEXP (x, 0)) == MULT + && REG_P (XEXP (XEXP (x, 0), 0)) + && CONST_INT_P (XEXP (XEXP (x, 0), 1)) + && IN_RANGE (pow2p_hwi (INTVAL (XEXP (XEXP (x, 0), 1))), 1, 3)) + { + *total = COSTS_N_INSNS (1); + return true; + } /* shNadd.uw pattern for zba. [(set (match_operand:DI 0 "register_operand" "=r") (plus:DI