From patchwork Tue Nov 8 20:46:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 60235 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 9CAE13858421 for ; Tue, 8 Nov 2022 20:47:19 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id 5182A38582A7 for ; Tue, 8 Nov 2022 20:46:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5182A38582A7 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-x229.google.com with SMTP id s24so22865907ljs.11 for ; Tue, 08 Nov 2022 12:46:41 -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=S0Zm4gKF+jCABJxBr0y3bgNgfHf0LQQiXGgQONVZWvo=; b=VgL3n9uQebe74Gzh6+jPA6E8OqxiRM9k1XnVWEtOG2sx86BWjkxetkxFSW2hwfs8ef Iu0SF36+WYH7N7jsnqwYBlvwLyajsT/xWq41h1r193s/K8ssfWVQI+L0fUjpjU30utV+ ZKs2mwU9FMj8DYgQ1Quif7VDTcT7I+KI8YppLdTqJCxn3R0ZPjdT9oyKRU4wfp90ybpL wzfOZQcUqg/74uNlCaHzSqTBQ/eMXqYCUqfRStcE4ePNFYdr69emL7dnCZM4GMYISt+v zOItBLzDZ4eIcLEi6YOcrObvDfWoA0awmEGSgd+6cILCtEF5rFBYvTjx2TVSBuE+Hcrv x/6Q== 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=S0Zm4gKF+jCABJxBr0y3bgNgfHf0LQQiXGgQONVZWvo=; b=3JQoBpMU7JALKPdt+VKIMy/jLGe7OmcXe2KSDfsfb2tIOvzYLu8z/vuptrNE1DYXxx sxFr8y/fgc4BNShC93+3FDV+OiFIoOt5BSdB/440LkNuNQn6pFHyJyw5e1iZAHtY0aSI 35wLu4axQi1q7anvj1fhMrMWYuLt+jNxvRNgZehx0OeCIf0BPi3r8myQIQG1f9ktvGqb KxL6bLXeI5pyI9OxdjhE9+ny0T4i2fbXv4CJxwcFc7kmqeIcxNL93UhEBlJ30PDag1nv iusspePzAL/4O5tVuCl5nHaQhHKqI/VAQ4c5SGnl6CY7Rtk6Rw4ml+FXKuPijq/ALgR2 EdFQ== X-Gm-Message-State: ACrzQf0tX16osWK8H/lW3cnY8yAP9UXP3nSK2hLOYZQ4rKYyf/3yBGuM 0c/BucopUfBP5pwzsNEtcx48Lqs8EHCcoyWr X-Google-Smtp-Source: AMsMyM7QUBMb2QUP9PuZtfFoVB0E70UUXxoF9At1OGRke0HM9NewfAPwTGquZ4wFThS/5QBpkfszpw== X-Received: by 2002:a05:651c:98a:b0:26d:fbd0:2c43 with SMTP id b10-20020a05651c098a00b0026dfbd02c43mr21088049ljq.517.1667940399513; Tue, 08 Nov 2022 12:46:39 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id f27-20020a19381b000000b0049ad315cfc3sm1919222lfa.162.2022.11.08.12.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 12:46:38 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Christoph Muellner , Vineet Gupta , Palmer Dabbelt , Jeff Law , Kito Cheng , Philipp Tomsich Subject: [PATCH] RISC-V: No extensions for SImode min/max against safe constant Date: Tue, 8 Nov 2022 21:46:36 +0100 Message-Id: <20221108204637.2794952-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, LIKELY_SPAM_BODY, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Optimize the common case of a SImode min/max against a constant that is safe both for sign- and zero-extension. E.g., consider the case int f(unsigned int* a) { const int C = 1000; return *a * 3 > C ? C : *a * 3; } where the constant C will yield the same result in DImode whether sign- or zero-extended. This should eventually go away once the lowering to RTL smartens up and considers the precision/signedness and the value-ranges of the operands to MIN_EXPR nad MAX_EXPR. gcc/ChangeLog: * config/riscv/bitmanip.md (*minmax): Additional pattern for min/max against constants that are extension-invariant. * config/riscv/iterators.md (minmax_optab): Add an iterator that has only min and max rtl. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbb-min-max-02.c: New test. Signed-off-by: Philipp Tomsich --- gcc/config/riscv/bitmanip.md | 18 ++++++++++++++++++ gcc/config/riscv/iterators.md | 4 ++++ .../gcc.target/riscv/zbb-min-max-02.c | 14 ++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index dddd3422c43..7e2ff4f79f9 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -360,6 +360,24 @@ "\t%0,%1,%2" [(set_attr "type" "bitmanip")]) +;; Optimize the common case of a SImode min/max against a constant +;; that is safe both for sign- and zero-extension. +(define_insn_and_split "*minmax" + [(set (match_operand:DI 0 "register_operand" "=r") + (sign_extend:DI + (subreg:SI + (bitmanip_minmax:DI (zero_extend:DI (match_operand:SI 1 "register_operand" "r")) + (match_operand:DI 2 "immediate_operand" "i")) + 0))) + (clobber (match_scratch:DI 3 "=&r")) + (clobber (match_scratch:DI 4 "=&r"))] + "TARGET_64BIT && TARGET_ZBB && sext_hwi (INTVAL (operands[2]), 32) >= 0" + "#" + "&& reload_completed" + [(set (match_dup 3) (sign_extend:DI (match_dup 1))) + (set (match_dup 4) (match_dup 2)) + (set (match_dup 0) (:DI (match_dup 3) (match_dup 4)))]) + ;; ZBS extension. (define_insn "*bset" diff --git a/gcc/config/riscv/iterators.md b/gcc/config/riscv/iterators.md index 50380ecfac9..cbbf61f6514 100644 --- a/gcc/config/riscv/iterators.md +++ b/gcc/config/riscv/iterators.md @@ -213,6 +213,10 @@ [(plus "add") (ior "or") (xor "xor") (and "and")]) ; bitmanip code attributes +(define_code_attr minmax_optab [(smin "smin") + (smax "smax") + (umin "umin") + (umax "umax")]) (define_code_attr bitmanip_optab [(smin "smin") (smax "smax") (umin "umin") diff --git a/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c b/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c new file mode 100644 index 00000000000..b462859f10f --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zba_zbb -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-Os" "-Oz" "-Og" } } */ + +int f(unsigned int* a) +{ + const int C = 1000; + return *a * 3 > C ? C : *a * 3; +} + +/* { dg-final { scan-assembler-times "minu" 1 } } */ +/* { dg-final { scan-assembler-times "sext.w" 1 } } */ +/* { dg-final { scan-assembler-not "zext.w" } } */ +