public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Raphael Moreira Zinsly <rzinsly@ventanamicro.com>,
	gcc-patches@gcc.gnu.org
Cc: jlaw@ventanamicro.com, jakub@redhat.com, palmer@dabbelt.com,
	philipp.tomsich@vrull.eu
Subject: Re: [PATCH v2] RISC-V: Produce better code with complex constants [PR95632] [PR106602]
Date: Tue, 27 Dec 2022 16:32:04 -0700	[thread overview]
Message-ID: <578012d0-0e2f-8663-3ff3-0362f7de9d6b@gmail.com> (raw)
In-Reply-To: <20221209182510.43515-1-rzinsly@ventanamicro.com>

[-- Attachment #1: Type: text/plain, Size: 1718 bytes --]



On 12/9/22 11:25, Raphael Moreira Zinsly wrote:
> Changes since v1:
> 	- Fixed formatting issues.
> 	- Added a name to the define_insn_and_split pattern.
> 	- Set the target on the 'dg-do compile' in pr106602.c.
> 	- Removed the rv32 restriction in pr95632.c.
> 
> -- >8 --
> 
> Due to RISC-V limitations on operations with big constants combine
> is failing to match such operations and is not being able to
> produce optimal code as it keeps splitting them.  By pretending we
> can do those operations we can get more opportunities for
> simplification of surrounding instructions.
> 
> 2022-12-06  Raphael Moreira Zinsly  <rzinsly@ventanamicro.com>
> 	    Jeff Law  <jlaw@ventanamicro.com>
> 
> gcc/Changelog:
> 	PR target/95632
> 	PR target/106602
> 	* config/riscv/riscv.md: New pattern to simulate complex
> 	const_int loads.
> 
> gcc/testsuite/ChangeLog:
> 	* gcc.target/riscv/pr95632.c: New test.
> 	* gcc.target/riscv/pr106602.c: New test.
Here's the final version of the patch that addresses these two BZs.

This version tightens slightly the condition for the new pattern so that 
it doesn't match two special cases.

In particular, we avoid certain constants which are used in 
define_splits where the constant load feeds a logical AND which can 
ultimately be implemented via a pair of shifts.

Those cases are 3->2 splits on the trunk.  If we allowed those constants 
in the new pattern, then we'd need to support 2->2 splits in combine.c 
which is a non-starter.

Bootstrapped and regression tested on riscv64-linux-gnu.  I also 
compared resultant assembly code with/without this change for all the 
source files in the compiler natively to look for further regressions.

Committed to the trunk,
Jeff

[-- Attachment #2: P --]
[-- Type: text/plain, Size: 2626 bytes --]

commit 2e886eef7f2b5aadb00171af868f0895b647c3a4
Author: Raphael Moreira Zinsly <rzinsly@ventanamicro.com>
Date:   Tue Dec 27 18:29:25 2022 -0500

    RISC-V: Produce better code with complex constants [PR95632] [PR106602]
    
    gcc/Changelog:
            PR target/95632
            PR target/106602
            * config/riscv/riscv.md: New pattern to simulate complex
            const_int loads.
    
    gcc/testsuite/ChangeLog:
            * gcc.target/riscv/pr95632.c: New test.
            * gcc.target/riscv/pr106602.c: New test.

diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index a8bb331f25c..020833b9206 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -1670,6 +1670,23 @@ (define_split
 		      MAX_MACHINE_MODE, &operands[3], TRUE);
 })
 
+;; Pretend to have the ability to load complex const_int in order to get
+;; better code generation around them.
+;;
+;; But avoid constants that are special cased elsewhere.
+(define_insn_and_split "*mvconst_internal"
+  [(set (match_operand:GPR 0 "register_operand" "=r")
+        (match_operand:GPR 1 "splittable_const_int_operand" "i"))]
+  "!(p2m1_shift_operand (operands[1]) || high_mask_shift_operand (operands[1]))"
+  "#"
+  "&& 1"
+  [(const_int 0)]
+{
+  riscv_move_integer (operands[0], operands[0], INTVAL (operands[1]),
+                      <MODE>mode, TRUE);
+  DONE;
+})
+
 ;; 64-bit integer moves
 
 (define_expand "movdi"
diff --git a/gcc/testsuite/gcc.target/riscv/pr106602.c b/gcc/testsuite/gcc.target/riscv/pr106602.c
new file mode 100644
index 00000000000..825b1a143b5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr106602.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { riscv64*-*-* } } } */
+/* { dg-options "-O2" } */
+
+unsigned long
+foo2 (unsigned long a)
+{
+  return (unsigned long)(unsigned int) a << 6;
+}
+
+/* { dg-final { scan-assembler-times "slli\t" 1 } } */
+/* { dg-final { scan-assembler-times "srli\t" 1 } } */
+/* { dg-final { scan-assembler-not "\tli\t" } } */
+/* { dg-final { scan-assembler-not "addi\t" } } */
+/* { dg-final { scan-assembler-not "and\t" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/pr95632.c b/gcc/testsuite/gcc.target/riscv/pr95632.c
new file mode 100644
index 00000000000..b865c2f2e97
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr95632.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+unsigned short
+foo (unsigned short crc)
+{
+  crc ^= 0x4002;
+  crc >>= 1;
+  crc |= 0x8000;
+
+  return crc;
+}
+
+/* { dg-final { scan-assembler-times "srli\t" 1 } } */
+/* { dg-final { scan-assembler-not "slli\t" } } */

  parent reply	other threads:[~2022-12-27 23:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-09 18:25 Raphael Moreira Zinsly
2022-12-16 17:19 ` Jeff Law
2022-12-27 23:32 ` Jeff Law [this message]
2023-02-23 21:23 ` Andrew Pinski
2023-03-05 18:13   ` Jeff Law
2023-03-05 19:03     ` Andrew Pinski
2023-03-05 19:07       ` Jeff Law

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=578012d0-0e2f-8663-3ff3-0362f7de9d6b@gmail.com \
    --to=jeffreyalaw@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jlaw@ventanamicro.com \
    --cc=palmer@dabbelt.com \
    --cc=philipp.tomsich@vrull.eu \
    --cc=rzinsly@ventanamicro.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).