From patchwork Tue May 24 21:47:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 1635169 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=vrull.eu header.i=@vrull.eu header.a=rsa-sha256 header.s=google header.b=KWI6HWyt; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4L779f4xFSz9sGl for ; Wed, 25 May 2022 07:48:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D8ACF383D80F for ; Tue, 24 May 2022 21:48:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id 7CBE138425B2 for ; Tue, 24 May 2022 21:47:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7CBE138425B2 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-x133.google.com with SMTP id l13so26370223lfp.11 for ; Tue, 24 May 2022 14:47:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mKNQ195eorr9ntjXz+K+EWBXU7mx76ff3hCq5bbpymA=; b=KWI6HWyt+uXMq/ZdWIqpT1JbZ1Jdj+4cSgVWfBYDCypAQRARCgVciTb+norPaZ4Rdl vOPsscQFQD0q6xxSvwsPQwfaIMdXGvQy8UIME85K1TZIHgIInmcMkqTuT3iGk2WiuUqr n42cRunPb9GVVa7mj1C2l11rNhlx2Zwk2B8H6gT4hpvPrR0vaTF56QXl2Bq7umCDj2U1 +UndnpXNZl7WD98fuuSCkxn3eSMMCO/P8dzRCvvJjejSyK9SAxbQLDD376xZZMR4YIOz qCYDXXqGGfgo31e8CIsnhLxeEiaHHrvS7EfzaZIWwftCeQrO4dSqucA0vhlrv3qrpQ0x vv6w== 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=mKNQ195eorr9ntjXz+K+EWBXU7mx76ff3hCq5bbpymA=; b=J1mV+VGLDZ7JO31caq2/E9UjsHBo/jZbhlO3SQSyVwj5jD5OyCKAYwzyekkS09HntT oYqsCWaRaYELYMj/h05QyKghOKghASI/OvydkQrnWJA01K3+N+00RRSQEUqIXQn4Svgy 2lO5SWGo3uHml2Vr6Lr7ZVVAnjaUda8I7FbL4X5ZqwhyeIDB07IOWKHgFBzoiPySmmQz dhD654zDSuugY7mANKqXmIE9iR0c5jSmsncYfXYPoYvj5vHZsUuRlaGo9fO4Fb8Djhn8 WhnOk613un17lE7IGGoYL3jK/c6Dg+V59PR31mhyrUtrh8NuYZBPC2LWTXVLnWfIFEJC q6Kw== X-Gm-Message-State: AOAM5337IwOWcAVYJRCkDAazp7+cu1+04bfyXqjd7CbS+37AXqYMp8mm eN5rSBuTCIR5V+nUPgnc2TdlYKJpqxx7Q1x9 X-Google-Smtp-Source: ABdhPJyccfwmHlh6gVwI6SRCFT+qIgkBOYNu0w+MqXADID2seNxZExGGXVwhByjvJCA1YoVpcGZUSA== X-Received: by 2002:ac2:5bdc:0:b0:478:6d6d:9bf2 with SMTP id u28-20020ac25bdc000000b004786d6d9bf2mr9399156lfn.633.1653428833751; Tue, 24 May 2022 14:47:13 -0700 (PDT) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id c17-20020a2e9d91000000b0024f3d1dae98sm2724165ljj.32.2022.05.24.14.47.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 14:47:13 -0700 (PDT) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Subject: [PATCH v1 2/3] RISC-V: Split slli+sh[123]add.uw opportunities to avoid zext.w Date: Tue, 24 May 2022 23:47:02 +0200 Message-Id: <20220524214703.4022737-3-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220524214703.4022737-1-philipp.tomsich@vrull.eu> References: <20220524214703.4022737-1-philipp.tomsich@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , Cc: Andrew Waterman , Vineet Gupta , Kito Cheng , Philipp Tomsich Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When encountering a prescaled (biased) value as a candidate for sh[123]add.uw, the combine pass will present this as shifted by the aggregate amount (prescale + shift-amount) with an appropriately adjusted mask constant that has fewer than 32 bits set. E.g., here's the failing expression seen in combine for a prescale of 1 and a shift of 2 (note how 0x3fffffff8 >> 3 is 0x7fffffff). Trying 7, 8 -> 10: 7: r78:SI=r81:DI#0<<0x1 REG_DEAD r81:DI 8: r79:DI=zero_extend(r78:SI) REG_DEAD r78:SI 10: r80:DI=r79:DI<<0x2+r82:DI REG_DEAD r79:DI REG_DEAD r82:DI Failed to match this instruction: (set (reg:DI 80 [ cD.1491 ]) (plus:DI (and:DI (ashift:DI (reg:DI 81) (const_int 3 [0x3])) (const_int 17179869176 [0x3fffffff8])) (reg:DI 82))) To address this, we introduce a splitter handling these cases. gcc/ChangeLog: * config/riscv/bitmanip.md: Add split to handle opportunities for slli + sh[123]add.uw gcc/testsuite/ChangeLog: * gcc.target/riscv/zba-shadd.c: New test. Signed-off-by: Philipp Tomsich Co-developed-by: Manolis Tsamis --- gcc/config/riscv/bitmanip.md | 44 ++++++++++++++++++++++ gcc/testsuite/gcc.target/riscv/zba-shadd.c | 13 +++++++ 2 files changed, 57 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/zba-shadd.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 0ab9ffe3c0b..6c1ccc6f8c5 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -79,6 +79,50 @@ (define_insn "*shNadduw" [(set_attr "type" "bitmanip") (set_attr "mode" "DI")]) +;; During combine, we may encounter an attempt to combine +;; slli rtmp, rs, #imm +;; zext.w rtmp, rtmp +;; sh[123]add rd, rtmp, rs2 +;; which will lead to the immediate not satisfying the above constraints. +;; By splitting the compound expression, we can simplify to a slli and a +;; sh[123]add.uw. +(define_split + [(set (match_operand:DI 0 "register_operand") + (plus:DI (and:DI (ashift:DI (match_operand:DI 1 "register_operand") + (match_operand:QI 2 "immediate_operand")) + (match_operand:DI 3 "consecutive_bits_operand")) + (match_operand:DI 4 "register_operand"))) + (clobber (match_operand:DI 5 "register_operand"))] + "TARGET_64BIT && TARGET_ZBA" + [(set (match_dup 5) (ashift:DI (match_dup 1) (match_dup 6))) + (set (match_dup 0) (plus:DI (and:DI (ashift:DI (match_dup 5) + (match_dup 7)) + (match_dup 8)) + (match_dup 4)))] +{ + unsigned HOST_WIDE_INT mask = UINTVAL (operands[3]); + /* scale: shift within the sh[123]add.uw */ + int scale = 32 - clz_hwi (mask); + /* bias: pre-scale amount (i.e. the prior shift amount) */ + int bias = ctz_hwi (mask) - scale; + + /* If the bias + scale don't add up to operand[2], reject. */ + if ((scale + bias) != UINTVAL (operands[2])) + FAIL; + + /* If the shift-amount is out-of-range for sh[123]add.uw, reject. */ + if ((scale < 1) || (scale > 3)) + FAIL; + + /* If there's no bias, the '*shNadduw' pattern should have matched. */ + if (bias == 0) + FAIL; + + operands[6] = GEN_INT (bias); + operands[7] = GEN_INT (scale); + operands[8] = GEN_INT (0xffffffffULL << scale); +}) + (define_insn "*add.uw" [(set (match_operand:DI 0 "register_operand" "=r") (plus:DI (zero_extend:DI diff --git a/gcc/testsuite/gcc.target/riscv/zba-shadd.c b/gcc/testsuite/gcc.target/riscv/zba-shadd.c new file mode 100644 index 00000000000..33da2530f3f --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zba-shadd.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc_zba -mabi=lp64" } */ + +unsigned long foo(unsigned int a, unsigned long b) +{ + a = a << 1; + unsigned long c = (unsigned long) a; + unsigned long d = b + (c<<2); + return d; +} + +/* { dg-final { scan-assembler "sh2add.uw" } } */ +/* { dg-final { scan-assembler-not "zext" } } */ \ No newline at end of file