From patchwork Thu Jul 11 18:13:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 1959481 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hzPlaL/E; 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 4WKjY24Rdxz1xpd for ; Fri, 12 Jul 2024 04:14:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 99AEE387546A for ; Thu, 11 Jul 2024 18:14:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc2e.google.com (mail-oo1-xc2e.google.com [IPv6:2607:f8b0:4864:20::c2e]) by sourceware.org (Postfix) with ESMTPS id 66193383E525 for ; Thu, 11 Jul 2024 18:13:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66193383E525 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 66193383E525 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c2e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720721630; cv=none; b=on5hxNalCjkzNyRJ+4K/t5zBEss1LlVVzNEfzvA2z18X9SxUNXeTBh9HkzGRSHiGxbgRFnVY2LL2x8BsPKCFBxFyy4oHtbvyPr59mncnyYZbiG0egpcdG30AzqTwfhvQXJ3Zy1Zxh5USApGKU/3sKTvl8gO9Gq5YaAxWgOalBkg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720721630; c=relaxed/simple; bh=wxSEeQ57YLvXnJw56V6NVcmUT3pj2M81xtmP8KgZCMc=; h=DKIM-Signature:Message-ID:Date:MIME-Version:From:To:Subject; b=RlShcsXfHcInyMvIQobUNjsUUahY02O1gDD71+PnmFjoKkebOHZsf8qeNIdDJvd4ooFWRWC2Xs81Wo07bSFnV8oKajwphR+jg/kzrT6xkKww0RgFuSEDDvbyk+8msxWVrGCFL4mCkaebs/d2NNiUlpki0iMXH54KIls4o8npNOY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc2e.google.com with SMTP id 006d021491bc7-5c47ad9967cso643112eaf.2 for ; Thu, 11 Jul 2024 11:13:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720721627; x=1721326427; darn=gcc.gnu.org; h=subject:to:from:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=bHJXrI9E5226xafme+TJptJJr3Ba5OpDPL+MjGbkYNA=; b=hzPlaL/EU188gLoJe70YvAro2bVZWBkTb5MOg/Oo5G/0exaBbLRYUBkyWfnLKeez1b 5bHAdag9XxK0sElbi7wJd3G3cK+1QQ5KyM/AAmKrxIOAXcYgX+rmLd2pnyqikgBJve3R a19t4LstknF62ORILQj0jwVlsUauptJea0+R+49QrPb4ywB/hC9VSD63RMt9i4/3vjca 9KPVrIkvW08aHda+BHi+jitpThMBE2pGuncsD3upxg9LtHmvIeYTBbIIXm+PaQHbHEfK 5VEsMSsCKcRHA77NsPwnSj1VaquFnWSKd14szqe34GhLi/1KMB3V/lsp2jWa/khZjOPi ehiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720721627; x=1721326427; h=subject:to:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bHJXrI9E5226xafme+TJptJJr3Ba5OpDPL+MjGbkYNA=; b=b4lLSZij9U5KpnOAeqIiLqoM1t2Vut+JJ1VxZkZmFhfgovJXjShwFIXjrgFgRdfXQz nAccq/s0AmygOJvmakjr71GcFEuKbpzUvdMonXYU9MK+c6/hA5fG37JTdwn4ItEWhpvH wi9tcBZeeAZ6gnPElvzBM+yU8GZU3U3LwTJ2X9nHGQ82SYl+fzQYswPLUlZwbvbjs0uX nRBkeMZaMh4aiuiGtljTUTApl3sEfSnGSBDbvj0ZRLOsS6wX4o1xS/+zRu0ckGQ7MYAS IbVANd/spD+yjIhgAy65tdh3fKFQSwX+tcqI6NJV7QnJtYWTYLERDzYGMEu0Jkhk53+S oIXQ== X-Gm-Message-State: AOJu0YwSPHkhXAEgp+LZlbD2sX3DRsR/SIqkVuwOVcakkImLbOH7sE/e HxKBbRdzL4FGiGXpaKn4Afuw98iU+Vs+sVFryS0XqcnY0Y31W+w+cOllXQ== X-Google-Smtp-Source: AGHT+IGLcmWtm+UFuO9que7pjJkm0anmNsbIyshe3XrUZ/NiuBxPd6e/CPLwMhBoDlT/bYsGN98T9A== X-Received: by 2002:a05:6870:71c5:b0:254:b3cc:a6da with SMTP id 586e51a60fabf-25eae831d1emr7787135fac.29.1720721626760; Thu, 11 Jul 2024 11:13:46 -0700 (PDT) Received: from [172.31.0.109] ([136.36.72.243]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70815de7ba2sm300632a34.26.2024.07.11.11.13.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 Jul 2024 11:13:46 -0700 (PDT) Message-ID: <2fcf8ec4-a55d-47ce-b8da-2d9453519781@gmail.com> Date: Thu, 11 Jul 2024 12:13:45 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Content-Language: en-US From: Jeff Law To: "gcc-patches@gcc.gnu.org" Subject: [to-be-committed][RISC-V] Avoid unnecessary sign extension after memcmp X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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.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 Similar to the str[n]cmp work, this adjusts the block compare expansion to do its work in X mode with an appropriate lowpart extraction of the results at the end of the sequence. This has gone through my tester on rv32 and rv64, but that's it. Waiting on pre-commit testing before moving forward. Jeff * config/riscv/riscv-string.cc (emit_memcmp_scalar_load_and_compare): Set RESULT directly rather than using a temporary. (emit_memcmp_scalar_result_calculation): Similarly. (riscv_expand_block_compare_scalar): Use CONST0_RTX rather than generating new RTL. * config/riscv/riscv.md (cmpmemsi): Pass an X mode temporary to the expansion routines. If necessary extract low part of the word to store in final result location. diff --git a/gcc/config/riscv/riscv-string.cc b/gcc/config/riscv/riscv-string.cc index 4736228e6f1..80d22e87d57 100644 --- a/gcc/config/riscv/riscv-string.cc +++ b/gcc/config/riscv/riscv-string.cc @@ -663,9 +663,7 @@ emit_memcmp_scalar_load_and_compare (rtx result, rtx src1, rtx src2, /* Fast-path for a single byte. */ if (cmp_bytes == 1) { - rtx tmp = gen_reg_rtx (Xmode); - do_sub3 (tmp, data1, data2); - emit_insn (gen_movsi (result, gen_lowpart (SImode, tmp))); + do_sub3 (result, data1, data2); emit_jump_insn (gen_jump (final_label)); emit_barrier (); /* No fall-through. */ return; @@ -702,12 +700,11 @@ emit_memcmp_scalar_result_calculation (rtx result, rtx data1, rtx data2) /* Get bytes in big-endian order and compare as words. */ do_bswap2 (data1, data1); do_bswap2 (data2, data2); + /* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence. */ - rtx tmp = gen_reg_rtx (Xmode); - emit_insn (gen_slt_3 (LTU, Xmode, Xmode, tmp, data1, data2)); - do_neg2 (tmp, tmp); - do_ior3 (tmp, tmp, const1_rtx); - emit_insn (gen_movsi (result, gen_lowpart (SImode, tmp))); + emit_insn (gen_slt_3 (LTU, Xmode, Xmode, result, data1, data2)); + do_neg2 (result, result); + do_ior3 (result, result, const1_rtx); } /* Expand memcmp using scalar instructions (incl. Zbb). @@ -773,7 +770,7 @@ riscv_expand_block_compare_scalar (rtx result, rtx src1, rtx src2, rtx nbytes) data1, data2, diff_label, final_label); - emit_insn (gen_rtx_SET (result, gen_rtx_CONST_INT (SImode, 0))); + emit_move_insn (result, CONST0_RTX (GET_MODE (result))); emit_jump_insn (gen_jump (final_label)); emit_barrier (); /* No fall-through. */ diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 2e2379dfca4..5dee837a587 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -2675,9 +2675,19 @@ (define_expand "cmpmemsi" operands[2], operands[3])) DONE; - if (riscv_expand_block_compare (operands[0], operands[1], operands[2], + rtx temp = gen_reg_rtx (word_mode); + if (riscv_expand_block_compare (temp, operands[1], operands[2], operands[3])) - DONE; + { + if (TARGET_64BIT) + { + temp = gen_lowpart (SImode, temp); + SUBREG_PROMOTED_VAR_P (temp) = 1; + SUBREG_PROMOTED_SET (temp, SRP_SIGNED); + } + emit_move_insn (operands[0], temp); + DONE; + } else FAIL; })