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: 17216 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2940073wru; Tue, 8 Nov 2022 12:47:29 -0800 (PST) X-Google-Smtp-Source: AA0mqf6KyaECYYWPgBp9FFRu5Aud5mAyIRV8yKS6OUA/4BvOBKEsYF91VA8o5x4UkxizqJqz2rQG X-Received: by 2002:a17:906:18e2:b0:7ae:563c:7bf8 with SMTP id e2-20020a17090618e200b007ae563c7bf8mr16505309ejf.391.1667940449527; Tue, 08 Nov 2022 12:47:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667940449; cv=none; d=google.com; s=arc-20160816; b=W1gWUwYCebfgVGOQNldHV5rckhJ1guzEtaup9tL7TaRKFPpnzRrZiV2YArlc9X6iZN bAZm7CsWq5IF30Vi9YJscVl3WKV+x5RqTIXFzug4JLAazV/OEx9HKh6poJ5xoT1/noE/ OJZ02m84qYzU0ekLZ6QxUttFNELZq7rC0D3zyL4rHdKeiPi6KzMc396KtqE2IoCBnWFh G5E41NhTHauTl9lGMmSG/BC03RJbjNC6RQYCZyjCTEmgANA2FM5Fx6URTrYFtsUDjRfF 9qH02HyVBhFdJDLbBQNS0WUlBvkf5Fo2Zcg/XQMu346AV0C69gHYsmdLIZbBzp/Pn8jn pk3w== 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=S0Zm4gKF+jCABJxBr0y3bgNgfHf0LQQiXGgQONVZWvo=; b=bxLxvZS1Ss3+RO9PqJWmA3yqL8bvkJoHtek8GPVCmvhPvmBjTu85CUIZkpXtFOjpjv ZE5XDocfC+ouRAWbf+e+1liuHTdn65se+BUR9YW13Z9i55p9kU12jiV8lIJ0o3+cBl1Z gS1Gr2+4QwHzDYjenAbvnmUGk9sJYW5J6SogjdzMtKTRfvoggqB7uq0DT+oRYDzJ0GlH lW1fMQmzTVKSHnH0ZWSZB2eXVOzP4tMcqacC0noboGCwyOvKfaSkPmYR6vTmkMo/NqU2 4bkpbFfL1de+HyGg/PN+p9tBTnV+W7hyqHXF2Uf1Ns4cmZBiXEgy8VEDMie8f6OeJEH5 es7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=VgL3n9uQ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a19-20020a50e713000000b00447b4fdc29fsi13057774edn.82.2022.11.08.12.47.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 12:47:29 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=VgL3n9uQ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 C93AB38582B9 for ; Tue, 8 Nov 2022 20:47:26 +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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748962325055221638?= X-GMAIL-MSGID: =?utf-8?q?1748962325055221638?= 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" } } */ +