On 9/5/24 1:50 PM, Raphael Moreira Zinsly wrote:
Changes since v1:
        - Fix bit31.
        - Remove negative shift checks.
        - Fix synthesis-7.c expected output.

-- >8 --

Improve handling of large constants in riscv_build_integer, generate
better code for constants where the high half can be constructed
by shifting/shiftNadding the low half or if the halves differ by less
than 2k.

gcc/ChangeLog:
        * config/riscv/riscv.cc (riscv_build_integer): Detect new case
        of constants that can be improved.
        (riscv_move_integer): Add synthesys for concatening constants
        without Zbkb.

gcc/testsuite/ChangeLog:
        * gcc.target/riscv/synthesis-7.c: Adjust expected output.
        * gcc.target/riscv/synthesis-12.c: New test.
        * gcc.target/riscv/synthesis-13.c: New test.
        * gcc.target/riscv/synthesis-14.c: New test.
---
  gcc/config/riscv/riscv.cc                     | 138 +++++++++++++++++-
  gcc/testsuite/gcc.target/riscv/synthesis-12.c |  26 ++++
  gcc/testsuite/gcc.target/riscv/synthesis-13.c |  26 ++++
  gcc/testsuite/gcc.target/riscv/synthesis-14.c |  28 ++++
  gcc/testsuite/gcc.target/riscv/synthesis-7.c  |   2 +-
  5 files changed, 213 insertions(+), 7 deletions(-)
  create mode 100644 gcc/testsuite/gcc.target/riscv/synthesis-12.c
  create mode 100644 gcc/testsuite/gcc.target/riscv/synthesis-13.c
  create mode 100644 gcc/testsuite/gcc.target/riscv/synthesis-14.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index a38cb72f09f..df8a5a1c1e2 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -1231,6 +1231,122 @@ riscv_build_integer (struct riscv_integer_op *codes, 
HOST_WIDE_INT value,
        }
}
+  else if (cost > 4 && TARGET_64BIT && can_create_pseudo_p ()
+          && allow_new_pseudos)
+    {
+      struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
+      int alt_cost;
+
+      unsigned HOST_WIDE_INT loval = value & 0xffffffff;
+      unsigned HOST_WIDE_INT hival = (value & ~loval) >> 32;
+      bool bit31 = (loval & 0x80000000) != 0;
+      int trailing_shift = ctz_hwi (loval) - ctz_hwi (hival);
+      int leading_shift = clz_hwi (loval) - clz_hwi (hival);
+      int shiftval = 0;
+
+      /* Adjust the shift into the high half accordingly.  */
+      if ((trailing_shift > 0 && hival == (loval >> trailing_shift)))
+       shiftval = 32 - trailing_shift;
+      else if ((leading_shift > 0 && hival == (loval << leading_shift)))
+       shiftval = 32 + leading_shift;
+
+      if (shiftval && !bit31)
+       alt_cost = 2 + riscv_build_integer_1 (alt_codes, sext_hwi (loval, 32),
+                                             mode);
+
+      /* For constants where the upper half is a shift of the lower half we
+        can do a shift followed by an or.  */
+      if (shiftval && alt_cost < cost && !bit31)
So if shiftval is not zero and bit31 is also not zero, then doesn't this test alt_cost < cost without having initialized alt_cost? I think this can be fixed by testing !bit31 before the alt_cost < cost check.




+       {
+         /* We need to save the first constant we build.  */
+         alt_codes[alt_cost - 3].save_temporary = true;
+
+         /* Now we want to shift the previously generated constant into the
+            high half.  */
+         alt_codes[alt_cost - 2].code = ASHIFT;
+         alt_codes[alt_cost - 2].value = shiftval;
+         alt_codes[alt_cost - 2].use_uw = false;
+         alt_codes[alt_cost - 2].save_temporary = false;
+
+         /* And the final step, IOR the two halves together.  Since this uses
+            the saved temporary, use CONCAT similar to what we do for Zbkb.  */
+         alt_codes[alt_cost - 1].code = CONCAT;
+         alt_codes[alt_cost - 1].value = 0;
+         alt_codes[alt_cost - 1].use_uw = false;
+         alt_codes[alt_cost - 1].save_temporary = false;
+
+         memcpy (codes, alt_codes, sizeof (alt_codes));
+         cost = alt_cost;
+       }
+
+      if (cost > 4 && !bit31 && TARGET_ZBA)
+       {
+         int value = 0;
+
+         /* Check for a shNadd.  */
+         if (hival == loval * 3)
+           value = 3;
+         else if (hival == loval * 5)
+           value = 5;
+         else if (hival == loval * 9)
+           value = 9;
+
+         if (value)
+           alt_cost = 2 + riscv_build_integer_1 (alt_codes,
+                                                 sext_hwi (loval, 32), mode);
+
+         /* For constants where the upper half is a shNadd of the lower half
+            we can do a similar transformation.  */
+         if (value && alt_cost < cost)
+           {
+             alt_codes[alt_cost - 3].save_temporary = true;
+             alt_codes[alt_cost - 2].code = FMA;
+             alt_codes[alt_cost - 2].value = value;
+             alt_codes[alt_cost - 2].use_uw = false;
+             alt_codes[alt_cost - 2].save_temporary = false;
+             alt_codes[alt_cost - 1].code = CONCAT;
+             alt_codes[alt_cost - 1].value = 0;
+             alt_codes[alt_cost - 1].use_uw = false;
+             alt_codes[alt_cost - 1].save_temporary = false;
+
+             memcpy (codes, alt_codes, sizeof (alt_codes));
+             cost = alt_cost;
+           }
+       }
+
+      if (cost > 4 && !bit31)
+       {
+         int value = hival - loval;
+
+         /* For constants were the halves differ by less than 2048 we can
+            generate the upper half by using an addi on the lower half then
+            using a shift 32 followed by an or.  */
+         if (abs (value) <= 2047)
Using IN_RANGE (value, -2048, 2047) would probably be better and capture one more case, -2048 :-)


I know your out of the office, so I'll make those two minor adjustments do a quick test and push this to the trunk.


jeff

Reply via email to