From patchwork Wed Dec 28 18:18:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Raphael Moreira Zinsly X-Patchwork-Id: 37261 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2023105wrt; Wed, 28 Dec 2022 10:19:05 -0800 (PST) X-Google-Smtp-Source: AMrXdXvWoaUgLdbYlBQWKGBZo1TtriNaOllvQSjPQb7fbBm2guDBIdmJejOJoAvZpKkZmAlP06+8 X-Received: by 2002:a50:9f09:0:b0:46f:7453:c9b6 with SMTP id b9-20020a509f09000000b0046f7453c9b6mr23612750edf.8.1672251545848; Wed, 28 Dec 2022 10:19:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672251545; cv=none; d=google.com; s=arc-20160816; b=pTPeDu/nC1eiHbmB6s8yiqr3mfBEaE/h72hPlkFkMd4TJbzd6a0vyI2zfBDuNXUW3G zy0lK0uEH2zKdkFEWiXIKvT7sXWR6QOayj2/vrWer2BCZihR1qiUwoHrbsOnFtM4ejVe I8novKnKhcEqFSmMNKobumZ1LmXASUYlj03k8NB4tEOdNLRzFRdr4iQV97Mv6PLYVqyU EUMZMa9VooqXzaZpeawAkdqbNOF3bwqKeI5fgrmahHPaGPSDEaeK+o4QfszvRgkMfopB QvVP0eW/vQFJnaUoKfhd8Uo6HB9GFUOw4nPs5cvy2pfonXN3PtvzYCAaeOkFgGo7JrdP /djQ== 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=OATrbedgnWuHl4XAbcKkJKdbj2f1VYlzm3/+97zV2LA=; b=kcyeFkla5LLUp784Sjs4aqLDFtgCtmG2ZWeNYOVwDVLxvO4xff7U0HM8CzyGbGKcvm wGC32M88cgQcn0ayKvl8V3rfnGQqQICEpcFsA2VA0aRPxhYGSZF4UfhBa7oRharEh/N8 zMzt3bq6PnmLBrVKhqexrlGF+6fokO4W7oU88kkjNl7EvIWndyUPx4VO60ZFwgf+JLGa 04mOybJABr/k6xeKQALalQYswsZoQRO+GpmzmGe8ADdkhaN/xirWinSkpMmCIivMOz8o JXjmfqLff2IW5BuSFkm9GeF0477jhI/2Gq51EzWj2iY/HewmNpqnUclHpRQ6BJZbygVI rzxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=ETPo8fZP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o12-20020a056402444c00b00486e2db6579si4219128edb.418.2022.12.28.10.19.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Dec 2022 10:19:05 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=ETPo8fZP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 5CC4A385842A for ; Wed, 28 Dec 2022 18:19:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oa1-x2b.google.com (mail-oa1-x2b.google.com [IPv6:2001:4860:4864:20::2b]) by sourceware.org (Postfix) with ESMTPS id 81F983858D35 for ; Wed, 28 Dec 2022 18:18:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81F983858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ventanamicro.com Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-14fb3809eaeso13368189fac.1 for ; Wed, 28 Dec 2022 10:18:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; 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=OATrbedgnWuHl4XAbcKkJKdbj2f1VYlzm3/+97zV2LA=; b=ETPo8fZPRh9eK5bgz96Prkuz6ppwDvF/kOdnbIV4ExwaS8tq5MF6f6dY4qfnl1EODY AjMZ7+kFQ1blS8HNcdRynYhNmTS0HjFcbXFoiShdzLEzatrJf38sHpxEP9j1x6pHrA1K t34SNv474yFqtO019zQI8Hsp4p/AYj21FhcyGoqm9HJi6fUlYN0+oPLpZxgYGeGNzhIP iBbIV0+K3Asn0cLCf1bmMq9rniEL4FLOkrZOwI4Z6/mOqEdmNZuOTv7pae5rBqo8JzYz DMVupF8lvnmDgynzcVVFQvcp2EDD0X65ssg1eGRgYizq55rVcgFxVHmctfDkq6wCHzEo NwYw== 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=OATrbedgnWuHl4XAbcKkJKdbj2f1VYlzm3/+97zV2LA=; b=iqqJ8WRyA17hEOFY6XWLFUUaU+b2ywXNcESfHmAZeHYr2NOaLkyI3qTvd/lfBT76RO GbwTBr8KUI09qbLHhKcmN5nBlI0jb0EksITHrx8AeKTVvn0X6UkrQ9Bat4xvpOXzlB++ G46uCED9izOoYkrLg0v77woX1UHI4iuI1ium99xmwrQGB5hJ+dIfMEs2YrlS8bbquV7f ZKwzmtRsBDOfJH410sXHMAs0qAUU5lzY8k6pfv5KdgArqbZKW6d/aT2PB8+Jg+JUp6nj +etEntXCZeaig5CHYLfDRnfQ5fpIND8Y6X6KtMAZVNlHwtJ/Uy8ARvbAUkvPSrxaqhtZ h76w== X-Gm-Message-State: AFqh2koWzp62rT7xirSDoVSgV1eharjaPT4R0SBMBY/R032LbJfhFPfq oBgOhtiPbZ3wmciQUTIj1gwLYQcAZ+FyFY0J X-Received: by 2002:a05:6871:414:b0:144:57b1:c8cf with SMTP id d20-20020a056871041400b0014457b1c8cfmr17503640oag.47.1672251512404; Wed, 28 Dec 2022 10:18:32 -0800 (PST) Received: from marvin.dc1.ventanamicro.com ([2804:14d:baa4:50a9::1000]) by smtp.gmail.com with ESMTPSA id s24-20020a056870ea9800b0014c7958c55bsm7731479oap.42.2022.12.28.10.18.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Dec 2022 10:18:31 -0800 (PST) From: Raphael Moreira Zinsly To: gcc-patches@gcc.gnu.org Cc: jeffreyalaw@gmail.com, philipp.tomsich@vrull.eu, Raphael Moreira Zinsly Subject: [PATCH] RISC-V: Optimize min/max with SImode sources on 64-bit Date: Wed, 28 Dec 2022 15:18:17 -0300 Message-Id: <20221228181817.193462-1-rzinsly@ventanamicro.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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?1753482836753637952?= X-GMAIL-MSGID: =?utf-8?q?1753482836753637952?= The Zbb min/max pattern was not matching 32-bit sources when compiling for 64-bit. This patch separates the pattern into SImode and DImode, and use a define_expand to handle SImode on 64-bit. zbb-min-max-02.c generates different code as a result of the new expander.  The resulting code is as efficient as the old code. Furthermore, the special sh1add pattern that appeared in zbb-min-max-02.c is tested by the zba-shNadd-* tests. gcc/ChangeLog: * config/riscv/bitmanip.md (3): Divide pattern into si3_insn and di3. (si3): Handle SImode sources on TARGET_64BIT. gcc/testsuite: * gcc.target/riscv/zbb-abs.c: New test. * gcc.target/riscv/zbb-min-max-02.c: Addapt the expected output. --- gcc/config/riscv/bitmanip.md | 38 ++++++++++++++++--- gcc/testsuite/gcc.target/riscv/zbb-abs.c | 18 +++++++++ .../gcc.target/riscv/zbb-min-max-02.c | 2 +- 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-abs.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index d17133d58c1..abf08a29e89 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -360,14 +360,42 @@ DONE; }) -(define_insn "3" - [(set (match_operand:X 0 "register_operand" "=r") - (bitmanip_minmax:X (match_operand:X 1 "register_operand" "r") - (match_operand:X 2 "register_operand" "r")))] - "TARGET_ZBB" +(define_insn "si3_insn" + [(set (match_operand:SI 0 "register_operand" "=r") + (bitmanip_minmax:SI (match_operand:SI 1 "register_operand" "r") + (match_operand:SI 2 "register_operand" "r")))] + "!TARGET_64BIT && TARGET_ZBB" "\t%0,%1,%2" [(set_attr "type" "bitmanip")]) +(define_insn "di3" + [(set (match_operand:DI 0 "register_operand" "=r") + (bitmanip_minmax:DI (match_operand:DI 1 "register_operand" "r") + (match_operand:DI 2 "register_operand" "r")))] + "TARGET_64BIT && TARGET_ZBB" + "\t%0,%1,%2" + [(set_attr "type" "bitmanip")]) + +(define_expand "si3" + [(set (match_operand:SI 0 "register_operand" "=r") + (bitmanip_minmax:SI (match_operand:SI 1 "register_operand" "r") + (match_operand:SI 2 "register_operand" "r")))] + "TARGET_ZBB" + " +{ + if (TARGET_64BIT) + { + rtx op1_x = gen_reg_rtx (DImode); + emit_move_insn (op1_x, gen_rtx_SIGN_EXTEND (DImode, operands[1])); + rtx op2_x = gen_reg_rtx (DImode); + emit_move_insn (op2_x, gen_rtx_SIGN_EXTEND (DImode, operands[2])); + rtx dst_x = gen_reg_rtx (DImode); + emit_insn (gen_di3 (dst_x, op1_x, op2_x)); + emit_move_insn (operands[0], gen_lowpart (SImode, dst_x)); + DONE; + } +}") + ;; 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" diff --git a/gcc/testsuite/gcc.target/riscv/zbb-abs.c b/gcc/testsuite/gcc.target/riscv/zbb-abs.c new file mode 100644 index 00000000000..6ef7efdbd49 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbb-abs.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" } } */ + +#define ABS(x) (((x) >= 0) ? (x) : -(x)) + +int +foo (int x) +{ + return ABS(x); +} + +/* { dg-final { scan-assembler-times "neg" 1 } } */ +/* { dg-final { scan-assembler-times "max" 1 } } */ +/* { dg-final { scan-assembler-not "sraiw" } } */ +/* { dg-final { scan-assembler-not "xor" } } */ +/* { dg-final { scan-assembler-not "subw" } } */ + diff --git a/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c b/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c index b462859f10f..b9db655d55d 100644 --- a/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c +++ b/gcc/testsuite/gcc.target/riscv/zbb-min-max-02.c @@ -9,6 +9,6 @@ int f(unsigned int* a) } /* { dg-final { scan-assembler-times "minu" 1 } } */ -/* { dg-final { scan-assembler-times "sext.w" 1 } } */ +/* { dg-final { scan-assembler-times "sext.w|addw" 1 } } */ /* { dg-final { scan-assembler-not "zext.w" } } */