https://gcc.gnu.org/g:1c6bee45b4c3d9f2500430371ef61d9487a5b8e7

commit r17-6509-g1c6bee45b4c3d9f2500430371ef61d9487a5b8e7
Author: Bohan Lei <[email protected]>
Date:   Fri Nov 20 16:27:24 2026 -0700

    [PATCH v2] RISC-V: Allow IRA to allocate FPR to integer modes
    
    his is based on Zhijin Zeng's v3 patch
    (https://gcc.gnu.org/pipermail/gcc-patches/2025-January/673406.html).
    This commit modifies riscv_can_change_mode_class and
    riscv_hard_regno_mode_ok to allow IRA to allocate an FPR to a pseudo
    that is used in a interger mode in some places.
    
    A typical scenario would be cases with bit-level uses of floating-point
    variables.  The current IRA can only allocate them in GPRs, and any FP
    use of it would create a new FPR reload, which can lead to lots of
    redundant fmv.d.x instructions in the function.  The attached case
    (LLM-generated) is a reduced one that can still show the underlying
    issue.
    
    I am not using Zhijin's original testcase in this version because
    register renaming has accidentally helped eliminate the redundant fmv
    instructions in that case.
    
    Also note that the tiebility change in that patch is not included in
    this version.  We found that change can lead to more regressions and
    would require special handling.  Nonetheless, we found one redundant
    fmv.x.d instruction in our complete lgamma case downstream, not verified
    with the trunk though, which can be eliminated with the tieability
    change.  (After all, FP and integer modes being tieable makes it hard
    track separate live ranges of integer uses and FP uses of the same
    variable.)  We may submit a separate tieability patch after gathering
    enough evidence that this could lead to real performance issues.
    
    The earlier version failed to handle xtheadfmemidx correctly, and this
    version contains a fix for that route.  A local Sashiko review found a
    preexisting issue in th_memidx_classify_address_index, for which after
    confirming I will send a separate patch as it is not directly related to
    this.
    
    Regtested on rv64gcv and rv32gcv, no regression.
    
    Assisted-by: Codex
    Co-authored-by: Zhijin Zeng <[email protected]>
    
    gcc/ChangeLog:
    
            * config/riscv/constraints.md (th_m_fpr): New constraint for FPR
            load/store.
            * config/riscv/riscv.cc (riscv_hard_regno_mode_ok): Allow SImode
            and DImode in GPRs.
            (riscv_can_change_mode_class): Allow mode changing between
            integer and FP modes.
            * config/riscv/riscv.md: Add f,f constraints to SI and HI moves.
            * config/riscv/thead.cc (th_fmemidx_output_index): Take into
            account the scenario where MODE is integral.
            (th_output_move): Remove instruction class conditions.
            * config/riscv/vector.md: Restore X constraint for vr,vr LRA
            move patterns to prevent ICE caused by FPR-allocated scratch.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/fmv-lgamma.c: New test.
            * gcc.target/riscv/xtheadmemidx-fpr-int-1.c: New test.
            * gcc.target/riscv/xtheadmemidx-fpr-int-2.c: New test.
            * gcc.target/riscv/xtheadmemidx-fpr-int.h: New test.

Diff:
---
 gcc/config/riscv/constraints.md                    |  9 +++++++
 gcc/config/riscv/riscv.cc                          | 29 +++++++++++++++++++++-
 gcc/config/riscv/riscv.md                          | 28 ++++++++++-----------
 gcc/config/riscv/thead.cc                          | 21 ++++++++++------
 gcc/config/riscv/vector.md                         |  6 ++---
 gcc/testsuite/gcc.target/riscv/fmv-lgamma.c        | 20 +++++++++++++++
 .../gcc.target/riscv/xtheadmemidx-fpr-int-1.c      |  8 ++++++
 .../gcc.target/riscv/xtheadmemidx-fpr-int-2.c      |  7 ++++++
 .../gcc.target/riscv/xtheadmemidx-fpr-int.h        | 25 +++++++++++++++++++
 9 files changed, 128 insertions(+), 25 deletions(-)

diff --git a/gcc/config/riscv/constraints.md b/gcc/config/riscv/constraints.md
index 666d5a246ceb..73534cf77101 100644
--- a/gcc/config/riscv/constraints.md
+++ b/gcc/config/riscv/constraints.md
@@ -354,6 +354,15 @@
                 (and (match_test "!th_memidx_legitimate_index_p (op, false)")
                      (match_test "!th_memidx_legitimate_index_p (op, 
true)"))))))
 
+(define_memory_constraint "th_m_fpr"
+  "@internal
+   A memory operand usable by an FPR.  XTheadMemIdx writeback addresses
+   require a GPR, and indexed FPR accesses require XTheadFMemIdx."
+  (and (match_operand 0 "memory_operand")
+       (ior (match_test "satisfies_constraint_th__m__noi (op)")
+           (and (match_test "TARGET_XTHEADFMEMIDX")
+                (match_test "th_memidx_legitimate_index_p (op)")))))
+
 ;; CORE-V Constraints
 (define_constraint "CV_alu_pow2"
   "@internal
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 4607f6e7d3b1..5ab6c5bf85f9 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -11846,7 +11846,10 @@ riscv_hard_regno_mode_ok (unsigned int regno, 
machine_mode mode)
        return false;
 
       if (GET_MODE_CLASS (mode) != MODE_FLOAT
-         && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
+         && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
+         && !((mode == DImode && TARGET_DOUBLE_FLOAT && TARGET_64BIT)
+              || (mode == SImode && TARGET_HARD_FLOAT)
+              || (mode == HImode && TARGET_ZFHMIN)))
        return false;
 
       /* Only use callee-saved registers if a potential callee is guaranteed
@@ -13484,6 +13487,30 @@ riscv_can_change_mode_class (machine_mode from, 
machine_mode to,
       && !ordered_p (GET_MODE_PRECISION (from), GET_MODE_PRECISION (to)))
     return false;
 
+  if ((SCALAR_INT_MODE_P (to) || SCALAR_FLOAT_MODE_P (to))
+      && (rclass == FP_REGS || rclass == RVC_FP_REGS
+         || rclass == GR_REGS || rclass == RVC_GR_REGS
+         || rclass == JALR_REGS))
+    {
+      /* fmv.x.w or fmv.w.x.  */
+      if (TARGET_HARD_FLOAT)
+       if ((from == SFmode && to == SImode)
+           || (from == SImode && to == SFmode))
+         return true;
+
+      /* fmv.x.d or fmv.d.x.  */
+      if (TARGET_DOUBLE_FLOAT && TARGET_64BIT)
+       if ((from == DFmode && to == DImode)
+           || (from == DImode && to == DFmode))
+         return true;
+
+      /* fmv.x.h or fmv.h.x.  */
+      if (TARGET_ZFHMIN)
+       if ((from == HFmode && to == HImode)
+           || (from == HImode && to == HFmode))
+         return true;
+    }
+
   return !reg_classes_intersect_p (FP_REGS, rclass);
 }
 
diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index 22579ad1ef4c..25e5001e9602 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -2637,8 +2637,8 @@
 })
 
 (define_insn "*movdi_32bit"
-  [(set (match_operand:DI 0 "nonimmediate_operand" "=r,r,r, m,  
*f,*f,*r,*f,*m,r")
-       (match_operand:DI 1 "move_operand"         " 
r,i,m,rJ,*J*r,*m,*f,*f,*f,vp"))]
+  [(set (match_operand:DI 0 "nonimmediate_operand" "=r,r,r, m,  
*f,*f,*r,*f,*th_m_fpr,r")
+       (match_operand:DI 1 "move_operand"         " 
r,i,m,rJ,*J*r,*th_m_fpr,*f,*f,*f,vp"))]
   "!TARGET_64BIT
    && (register_operand (operands[0], DImode)
        || reg_or_0_operand (operands[1], DImode))"
@@ -2649,8 +2649,8 @@
    (set_attr "ext" "base,base,base,base,d,d,d,d,d,vector")])
 
 (define_insn "*movdi_64bit"
-  [(set (match_operand:DI 0 "nonimmediate_operand" "=r,r,r, m,  
*f,*f,*r,*f,*m,r")
-       (match_operand:DI 1 "move_operand"         " 
r,T,m,rJ,*r*J,*m,*f,*f,*f,vp"))]
+  [(set (match_operand:DI 0 "nonimmediate_operand" "=r,r,r, m,  
*f,*f,*r,*f,*th_m_fpr,r")
+       (match_operand:DI 1 "move_operand"         " 
r,T,m,rJ,*r*J,*th_m_fpr,*f,*f,*f,vp"))]
   "TARGET_64BIT
    && (register_operand (operands[0], DImode)
        || reg_or_0_operand (operands[1], DImode))"
@@ -2683,16 +2683,16 @@
 })
 
 (define_insn "*movsi_internal"
-  [(set (match_operand:SI 0 "nonimmediate_operand" "=r,r,r, m,  *f,*f,*r,*m,r")
-       (match_operand:SI 1 "move_operand"         " 
r,T,m,rJ,*r*J,*m,*f,*f,vp"))]
+  [(set (match_operand:SI 0 "nonimmediate_operand" "=r,r,r, m,  
*f,*f,*r,*f,*th_m_fpr,r")
+       (match_operand:SI 1 "move_operand"         " 
r,T,m,rJ,*r*J,*th_m_fpr,*f,*f,*f,vp"))]
   "(register_operand (operands[0], SImode)
     || reg_or_0_operand (operands[1], SImode))
     && !(REG_P (operands[1]) && VL_REG_P (REGNO (operands[1])))"
   { return riscv_output_move (operands[0], operands[1]); }
-  [(set_attr "move_type" 
"move,const,load,store,mtc,fpload,mfc,fpstore,rdvlenb")
+  [(set_attr "move_type" 
"move,const,load,store,mtc,fpload,mfc,fmove,fpstore,rdvlenb")
    (set_attr "mode" "SI")
-   (set_attr "type" "move,move,load,store,mtc,fpload,mfc,fpstore,move")
-   (set_attr "ext" "base,base,base,base,f,f,f,f,vector")])
+   (set_attr "type" "move,move,load,store,mtc,fpload,mfc,fmove,fpstore,move")
+   (set_attr "ext" "base,base,base,base,f,f,f,f,f,vector")])
 
 ;; 16-bit Integer moves
 
@@ -2711,15 +2711,15 @@
 })
 
 (define_insn "*movhi_internal"
-  [(set (match_operand:HI 0 "nonimmediate_operand" "=r,r,r, m,  *f,*r,r")
-       (match_operand:HI 1 "move_operand"         " r,T,m,rJ,*r*J,*f,vp"))]
+  [(set (match_operand:HI 0 "nonimmediate_operand" "=r,r,r, m,  *f,*r,*f,r")
+       (match_operand:HI 1 "move_operand"         " r,T,m,rJ,*r*J,*f,*f,vp"))]
   "(register_operand (operands[0], HImode)
     || reg_or_0_operand (operands[1], HImode))"
   { return riscv_output_move (operands[0], operands[1]); }
-  [(set_attr "move_type" "move,const,load,store,mtc,mfc,rdvlenb")
+  [(set_attr "move_type" "move,const,load,store,mtc,mfc,fmove,rdvlenb")
    (set_attr "mode" "HI")
-   (set_attr "type" "move,move,load,store,mtc,mfc,move")
-   (set_attr "ext" "base,base,base,base,f,f,vector")])
+   (set_attr "type" "move,move,load,store,mtc,mfc,fmove,move")
+   (set_attr "ext" "base,base,base,base,f,f,f,vector")])
 
 ;; HImode constant generation; see riscv_move_integer for details.
 ;; si+si->hi without truncation is legal because of
diff --git a/gcc/config/riscv/thead.cc b/gcc/config/riscv/thead.cc
index 33d9dc28dd45..59d9f4b65901 100644
--- a/gcc/config/riscv/thead.cc
+++ b/gcc/config/riscv/thead.cc
@@ -840,6 +840,16 @@ th_memidx_output_index (rtx dest, rtx src, machine_mode 
mode, bool load)
 static const char *
 th_fmemidx_output_index (rtx dest, rtx src, machine_mode mode, bool load)
 {
+  /* Integer-mode values can also reside in FPRs.  */
+  machine_mode fmode = mode;
+  if (mode == SImode)
+    fmode = SFmode;
+  else if (mode == DImode)
+    fmode = DFmode;
+
+  if (!TARGET_XTHEADFMEMIDX || !is_fmemidx_mode (fmode))
+    return NULL;
+
   struct riscv_address_info info;
   char format[24];
   rtx output_operands[2];
@@ -920,15 +930,14 @@ th_output_move (rtx dest, rtx src)
 
   if (dest_code == REG && src_code == MEM)
     {
-      if (GET_MODE_CLASS (mode) == MODE_INT
-         || (GET_MODE_CLASS (mode) == MODE_FLOAT && GP_REG_P (REGNO (dest))))
+      if (GP_REG_P (REGNO (dest)))
        {
          if ((insn = th_memidx_output_index (dest, src, mode, true)))
            return insn;
          if ((insn = th_memidx_output_modify (dest, src, mode, true)))
            return insn;
        }
-      else if (GET_MODE_CLASS (mode) == MODE_FLOAT && HARDFP_REG_P (REGNO 
(dest)))
+      else if (HARDFP_REG_P (REGNO (dest)))
        {
          if ((insn = th_fmemidx_output_index (dest, src, mode, true)))
            return insn;
@@ -936,16 +945,14 @@ th_output_move (rtx dest, rtx src)
     }
   else if (dest_code == MEM && (src_code == REG || src == CONST0_RTX (mode)))
     {
-      if (GET_MODE_CLASS (mode) == MODE_INT
-         || src == CONST0_RTX (mode)
-         || (GET_MODE_CLASS (mode) == MODE_FLOAT && GP_REG_P (REGNO (src))))
+      if (src == CONST0_RTX (mode) || GP_REG_P (REGNO (src)))
        {
          if ((insn = th_memidx_output_index (dest, src, mode, false)))
            return insn;
          if ((insn = th_memidx_output_modify (dest, src, mode, false)))
            return insn;
        }
-      else if (GET_MODE_CLASS (mode) == MODE_FLOAT && HARDFP_REG_P (REGNO 
(src)))
+      else if (HARDFP_REG_P (REGNO (src)))
        {
          if ((insn = th_fmemidx_output_index (dest, src, mode, false)))
            return insn;
diff --git a/gcc/config/riscv/vector.md b/gcc/config/riscv/vector.md
index 5413bedcb3f3..d8a4b47855da 100644
--- a/gcc/config/riscv/vector.md
+++ b/gcc/config/riscv/vector.md
@@ -1374,7 +1374,7 @@
 (define_insn_and_split "*mov<V_FRACT:mode><P:mode>_lra"
   [(set (match_operand:V_FRACT 0 "reg_or_mem_operand" "=vr, m,vr")
        (match_operand:V_FRACT 1 "reg_or_mem_operand" "  m,vr,vr"))
-   (clobber (match_scratch:P 2 "=&r,&r,r"))]
+   (clobber (match_operand:P 2 "" "=&r,&r,X"))]
   "TARGET_VECTOR && (lra_in_progress || reload_completed)"
   "#"
   "&& reload_completed"
@@ -1396,7 +1396,7 @@
 (define_insn_and_split "*mov<VB:mode><P:mode>_lra"
   [(set (match_operand:VB 0 "reg_or_mem_operand" "=vr, m,vr")
        (match_operand:VB 1 "reg_or_mem_operand" "  m,vr,vr"))
-   (clobber (match_scratch:P 2 "=&r,&r,r"))]
+   (clobber (match_operand:P 2 "" "=&r,&r,X"))]
   "TARGET_VECTOR && (lra_in_progress || reload_completed)"
   "#"
   "&& reload_completed"
@@ -1524,7 +1524,7 @@
 (define_insn_and_split "*mov<VLS_AVL_REG:mode><P:mode>_lra"
   [(set (match_operand:VLS_AVL_REG 0 "reg_or_mem_operand" "=vr, m,vr")
        (match_operand:VLS_AVL_REG 1 "reg_or_mem_operand" "  m,vr,vr"))
-   (clobber (match_scratch:P 2 "=&r,&r,r"))]
+   (clobber (match_operand:P 2 "" "=&r,&r,X"))]
   "TARGET_VECTOR && (lra_in_progress || reload_completed)
    && (register_operand (operands[0], <VLS_AVL_REG:MODE>mode)
        || register_operand (operands[1], <VLS_AVL_REG:MODE>mode))"
diff --git a/gcc/testsuite/gcc.target/riscv/fmv-lgamma.c 
b/gcc/testsuite/gcc.target/riscv/fmv-lgamma.c
new file mode 100644
index 000000000000..dabd143d212a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/fmv-lgamma.c
@@ -0,0 +1,20 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-options "-O2 -march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt" } */
+
+/* Keep the floating-point value in an FPR when its bits are also used by
+   an integer comparison.  Reduced from lgamma.  */
+
+double
+f (double x)
+{
+  if (((union { double d; unsigned long u; }) { x }).u)
+    return 0;
+
+  return x * (x * 3);
+}
+
+/* The floating-point operations must not reload x from a GPR or a spill
+   slot after the integer comparison.  Allow fmv.d.x from zero, which
+   constructs the return value of zero.  */
+/* { dg-final { scan-assembler-not {\tfmv\.d\.x\t[^,\n]+,(?!zero)} } } */
+/* { dg-final { scan-assembler-not {\tfsd\t} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int-1.c 
b/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int-1.c
new file mode 100644
index 000000000000..78e98f1af3e5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int-1.c
@@ -0,0 +1,8 @@
+/* { dg-do assemble { target { rv64 } } } */
+/* { dg-options "-O3 -march=rv64imafdc_xtheadfmemidx_xtheadmemidx -mabi=lp64d 
-save-temps" } */
+
+#include "xtheadmemidx-fpr-int.h"
+
+/* { dg-final { scan-assembler {\mth\.fsrd\s} } } */
+/* { dg-final { scan-assembler {\mth\.fsrw\s} } } */
+/* { dg-final { scan-assembler-not {\mth\.[ls][a-z]+\s+f[ast][0-9]+,} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int-2.c 
b/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int-2.c
new file mode 100644
index 000000000000..8b54158ae4a3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int-2.c
@@ -0,0 +1,7 @@
+/* { dg-do assemble { target { rv64 } } } */
+/* { dg-options "-O3 -march=rv64imafdc_xtheadmemidx -mabi=lp64d -save-temps" } 
*/
+
+#include "xtheadmemidx-fpr-int.h"
+
+/* { dg-final { scan-assembler-not {\mth\.f[ls]} } } */
+/* { dg-final { scan-assembler-not {\mth\.[ls][a-z]+\s+f[ast][0-9]+,} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int.h 
b/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int.h
new file mode 100644
index 000000000000..ddbce59bb6d6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/xtheadmemidx-fpr-int.h
@@ -0,0 +1,25 @@
+/* Keep integer-mode values in FPRs across an empty asm, so the tests do
+   not depend on IRA choosing a particular class for an unconstrained copy.  */
+
+#define STORE(NAME, ITYPE, FTYPE)                 \
+void NAME (ITYPE *dst, long index, FTYPE value)   \
+{                                               \
+  union { FTYPE f; ITYPE i; } bits = { value };   \
+  asm ("" : "+f" (bits.i));                     \
+  dst[index] = bits.i;                           \
+}
+
+STORE (store_di, unsigned long long, double)
+STORE (store_si, unsigned int, float)
+
+void
+store_post (unsigned long long *dst, double value, int n)
+{
+  union { double f; unsigned long long i; } bits = { value };
+  asm ("" : "+f" (bits.i));
+  for (int i = 0; i < n; ++i)
+    {
+      *dst++ = bits.i;
+      asm volatile ("" ::: "memory");
+    }
+}

Reply via email to