From patchwork Wed Nov 9 00:06:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 17283 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp47920wru; Tue, 8 Nov 2022 16:08:41 -0800 (PST) X-Google-Smtp-Source: AMsMyM5JfB+34kkCgc0tvxxMdkWrpW20Hqy9EZ1W6EJ6hpQBQPGGNFy5ELrunVvm8MvBjOD8eAe6 X-Received: by 2002:a05:6402:298d:b0:451:5fc5:d423 with SMTP id eq13-20020a056402298d00b004515fc5d423mr55248833edb.102.1667952521551; Tue, 08 Nov 2022 16:08:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667952521; cv=none; d=google.com; s=arc-20160816; b=kFuQe7MNd5pnImxHkzi2ge2c3yNE6gt+8hfLl506wGlpMyXkEKoeDFC8bAuwlN2YEQ 9PFsMuNydJWrnVLCmfog2Bfj12Ba2yRwzeRgfxQ7g9CkuM7LbNh+f6OuopcTCgFTmOtK 2nfDMUFJowOpUEK7SEOqCQMmS/IIdUXabGgOdj4iwO0lvtw+8boRR52gEo44iJEyyOmu YzV4uxZPi6akvEQaCpcUMfGjzUKg3RE3SG7wmy4jOO96/GwCel83FPRMLjBVFF7wnrvr /EZkbFoCTYwCW/UQwgdB7MF5fiT3G8xXHTSl6m/5v9H5T2CQecU9MtMMbASNW19vf/ob 5sPA== 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=RT5fInPPYkL2pYAXO88QJ1nLYQIPANf9pdfxXICI1oQ=; b=a7BtQHR8UdrcOgXCPnp4uqJqExKoD4sm28+lw3m1Ga3mXPm8+Jzn/5e72XhFaD+PZi yczAundmbMiWFViwnGKgX7Ji7W4EsiNb5vGHN/V5vPYRMuAuCqmpBxm/5YjZxYLzNZOd rEpN9htOmUE9oEb9boInsFgBt6+ORb9DiNyvHCFkyi4w+zQ0s/fFzae7kLHmx9KDFHyX MbPiT8uApZ45kBJaTPXIRv2bNyYDe5jfUXEeXnj2Uwn1iwCZ0TckYOmJ4ewbz1vrPR8J BGH+b/IGZ3LKjar/Feb6/hz75h+u0NVLv07Hw7VXrW1k349O8/h3+Gz/QgwVGdETS/UJ wAng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=CMufbhBK; 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 m22-20020a056402051600b0045f802c0b98si11577378edv.443.2022.11.08.16.08.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 16:08:41 -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=CMufbhBK; 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 B97E93857C4A for ; Wed, 9 Nov 2022 00:08:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 3BE713858C53 for ; Wed, 9 Nov 2022 00:06:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3BE713858C53 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-x234.google.com with SMTP id u2so23488184ljl.3 for ; Tue, 08 Nov 2022 16:06:38 -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=RT5fInPPYkL2pYAXO88QJ1nLYQIPANf9pdfxXICI1oQ=; b=CMufbhBKJKJQHGQz9qCvVZxBWhbXkA6NhthONbfm+WCzKbXgr+Cjdr0BEZ2Gnd4hOB M70vGP2V8rBMyBdr5/pRZYjzzkR1HOl3f46qjYr4k6X3znhLFcfe6wscY6bmI3ps9Gc0 0W/cTB5hLYTH2gvrtttBRbz6GaNzRbDX6XzSObo3MhQkI3t7FAebKZWnYk1TdKMzzksL 3pWsPx50Ia8bsriJcu0O23W9loGA7RGBZb4ZQRlc1flsVqMxZXHbB7Pad1qaGt9V6crl pw009AXmNmiIvmxwxljIjzRv5yBVG82XVklp5+34udlyG5yJjNmHaANkbbYZ3dP+vnFC NERQ== 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=RT5fInPPYkL2pYAXO88QJ1nLYQIPANf9pdfxXICI1oQ=; b=fYbEvYZ/jjn0V4b4mHjsxTl7jz/qyqiENtwdwPsRQpjym1mOBAFKmyXtBfmjLt3yYN jMZZqgl4UzMwUAwZZ5hC/st104aeBOgXH5d9SYqVYsAr2Rts03E90A/BN2L6dMZjSOYo ZSN3UrXYL433ljaZz2452omaD2rCEVFtPG1uBwC7DXVT4NabzA04baWlCq69vLugU2q1 buWZHwIYeIC5jw4GHTyfdptE+bklVO+dxnHAS9kfBt4gdIB1n2kPQN+w5nQXbBgW4xWO tZ09AdQJRk7WyPoTtj6WNt7gWzR32N+Qmu1oR1fvliQx20azY3GxT8vdE+8GcKXFMF1j 1uHA== X-Gm-Message-State: ACrzQf06DCk/Z0KehKQ1tlFvTXsScex/p9VloVZLHEJSwVOfSFNiULIB OnCzmgX0u8tvJA9aJ6RzmYowmU/9lj9oq5JH X-Received: by 2002:a2e:9c82:0:b0:277:3b89:9505 with SMTP id x2-20020a2e9c82000000b002773b899505mr19969232lji.273.1667952396231; Tue, 08 Nov 2022 16:06:36 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id bg36-20020a05651c0ba400b002770eafaafbsm251520ljb.99.2022.11.08.16.06.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 16:06:35 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Kito Cheng , Jeff Law , Christoph Muellner , Palmer Dabbelt , Vineet Gupta , Philipp Tomsich Subject: [PATCH v2] RISC-V: No extensions for SImode min/max against safe constant Date: Wed, 9 Nov 2022 01:06:31 +0100 Message-Id: <20221109000631.2814859-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?1748974983027047552?= 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 --- Changes in v2: - fixes the use of minmax_optab (was: bitmanip_optab), which is a change that dropped off the cherry-pick on the previous submission 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..bb23ceb86d9 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" } } */ +