From patchwork Thu Nov 11 14:10:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 47469 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 30C2B385B805 for ; Thu, 11 Nov 2021 14:14:46 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id C422B3857C43 for ; Thu, 11 Nov 2021 14:10:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C422B3857C43 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-lj1-x22a.google.com with SMTP id e9so12171213ljl.5 for ; Thu, 11 Nov 2021 06:10:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull-eu.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ffzBkEz8pHEWlkeWXQL89ffmXABHe5GflU+Fi4bXXv4=; b=VLFHiVqE/sYi/w5LOHqW//QrWXTwOZ8W6Mszyd23YzwePkGty1esDwmXdjee9ePgiJ o0imRRr4SQ8fwHWsej7+uGPxY2tR5mwKGz22xqYU5EA6Nx4FH5CDZ3mRpUwSjmp/kys3 Cm587bkX/TznHuV0Cb+AZlcPA3KYje8vMeaR7DofgF2utxBGDWV7rk9MLQ1WvheTbWQg XURj1dVvyiE2GAIfV9LUV4ow47cBsamsGcNHs3e1aLDpUctkVGlspH6Q0sWuK4kYp0+m w4QYC9vYX5+vWc9/ORN0sgrhB5TVWzJh7fU1q4sizZ95da77lNkkUFPG75IO3WvQ5TQ4 kT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ffzBkEz8pHEWlkeWXQL89ffmXABHe5GflU+Fi4bXXv4=; b=FP7MRbVzI0bDryCferMeQSL0XlYIH/nMHgaUEtqRqtRZE9xSxDARI+5EATixrXnLWo X29zcftlV7kGTWTT5yihXAg1rqjNbTGB3fnJ5nsH9p9AVzVL81dnfqinZOzptg0V+49G lzHS0F5VEor8eFUdNzVNxOG+DHVA6FPTw8etSMalbD9Xa0TZJsxmQzKhDeENnhjRahgy 3DViETa1KWN+OL0m6eiXD3QiU0jcIYGMVqEz3N6P/shSRrxMatMz0wh8XDW4QylqGks5 C7H0btxS8xly1RR/Z7TcIwFvpxO8h+H7Bzp84jESMEnx6cSX8zBVUGTWX54yHCXvyVSr it8g== X-Gm-Message-State: AOAM531gyRjK018Os8RizoLML0pnnKlGuxn5Kr+1zZpyhu3vX/WP10qU qBCs6YcfzNjAQtRsORTQq7DZb2wxQgs6IHjC X-Google-Smtp-Source: ABdhPJzHRKDSo8eHbjA5OxvJ1coSvpYWBDYmbicZ2mFbdsS/wu8xagc4grxqKSYWqkPqQfVMT5JEHg== X-Received: by 2002:a05:651c:211c:: with SMTP id a28mr7545319ljq.323.1636639826342; Thu, 11 Nov 2021 06:10:26 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id a23sm274427ljh.140.2021.11.11.06.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 06:10:26 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Subject: [PATCH v1 3/8] RISC-V: costs: support shift-and-add in strength-reduction Date: Thu, 11 Nov 2021 15:10:15 +0100 Message-Id: <20211111141020.2738001-4-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211111141020.2738001-1-philipp.tomsich@vrull.eu> References: <20211111141020.2738001-1-philipp.tomsich@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: wilson@tuliptree.org, kito.cheng@gmail.com, Philipp Tomsich Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. Signed-off-by: Philipp Tomsich --- gcc/config/riscv/riscv.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gcc/config/riscv/riscv.c b/gcc/config/riscv/riscv.c index 8480cf09294..dff4e370471 100644 --- a/gcc/config/riscv/riscv.c +++ b/gcc/config/riscv/riscv.c @@ -2020,6 +2020,20 @@ 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 + && ((!TARGET_64BIT && (mode == SImode)) || + (TARGET_64BIT && (mode == DImode))) + && (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