Group eligible scalar memory accesses by register file, access kind,
mode and base register.  Reuse the memory-pair rules to choose their
offset order, preferring increasing offsets for bidirectional candidates.
Use the common scheduling fusion pass gate.  Leave final pair
recognition to the macro-fusion matcher.

Builds of the ten SPEC CPU 2017 Integer benchmarks used the LP64D ABI
and -mcpu=xt-c9501fdvt -O3 -flto -static.  The baseline is the parent
of this patch.  Both builds used the default scheduling options.

The tables count static macro-fusion pairs in the final LTO sched2
dumps for each benchmark's main executable, collected with
-fsched-verbose=9 -fdump-rtl-sched2-all.  The load/store subset
includes both increasing and decreasing offset pairs.

All macro-fusion pairs

Case            Baseline This patch   Change
500.perlbench_r    31421      34370   +9.39%
502.gcc_r         125528     135648   +8.06%
505.mcf_r            227        254  +11.89%
520.omnetpp_r      23942      25621   +7.01%
523.xalancbmk_r    48627      50971   +4.82%
525.x264_r          7515       8301  +10.46%
531.deepsjeng_r      887        982  +10.71%
541.leela_r         1346       1436   +6.69%
548.exchange2_r     2233       2868  +28.44%
557.xz_r            2069       2289  +10.63%
SUM               243795     262740   +7.77%

Load/store pairs

Case            Baseline This patch   Change
500.perlbench_r    18803      21637  +15.07%
502.gcc_r          89154      98676  +10.68%
505.mcf_r            132        156  +18.18%
520.omnetpp_r      17989      19628   +9.11%
523.xalancbmk_r    38268      40639   +6.20%
525.x264_r          4548       5247  +15.37%
531.deepsjeng_r      385        464  +20.52%
541.leela_r          899        990  +10.12%
548.exchange2_r     1388       1985  +43.01%
557.xz_r            1422       1631  +14.70%
SUM               172988     191053  +10.44%

Assisted-by: qwenwork

gcc/ChangeLog:

        * config/riscv/riscv-fusion.cc (riscv_fuse_mem_direction): New function.
        (riscv_sched_fusion_priority): Likewise.
        * config/riscv/riscv-protos.h (riscv_sched_fusion_priority): Declare.
        * config/riscv/riscv.cc (TARGET_SCHED_FUSION_PRIORITY): Define.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/sched-fusion-priority.c: New test.
        * gcc.target/riscv/sched-fusion-priority-extended.c: Likewise.
        * gcc.target/riscv/sched-fusion-priority-off.c: Likewise.
        * gcc.target/riscv/sched-fusion-default.c: New test.
        * gcc.target/riscv/sched-fusion-option-restore.c: Likewise.
        * gcc.target/riscv/sched-fusion-unsupported-tune.c: Likewise.

Co-Authored-By: Michiel Derhaeg <[email protected]>
Signed-off-by: Jin Ma <[email protected]>
---
 gcc/config/riscv/riscv-fusion.cc              |  95 ++++++++
 gcc/config/riscv/riscv-protos.h               |   1 +
 gcc/config/riscv/riscv.cc                     |   2 +
 .../gcc.target/riscv/sched-fusion-default.c   |  14 ++
 .../riscv/sched-fusion-option-restore.c       |  50 ++++
 .../riscv/sched-fusion-priority-extended.c    | 221 ++++++++++++++++++
 .../riscv/sched-fusion-priority-off.c         |  14 ++
 .../gcc.target/riscv/sched-fusion-priority.c  | 126 ++++++++++
 .../riscv/sched-fusion-unsupported-tune.c     |  17 ++
 9 files changed, 540 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/sched-fusion-default.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sched-fusion-option-restore.c
 create mode 100644 
gcc/testsuite/gcc.target/riscv/sched-fusion-priority-extended.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sched-fusion-priority-off.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/sched-fusion-priority.c
 create mode 100644 
gcc/testsuite/gcc.target/riscv/sched-fusion-unsupported-tune.c

diff --git a/gcc/config/riscv/riscv-fusion.cc b/gcc/config/riscv/riscv-fusion.cc
index 6781951d91a..d46ceb2e0cf 100644
--- a/gcc/config/riscv/riscv-fusion.cc
+++ b/gcc/config/riscv/riscv-fusion.cc
@@ -2103,3 +2103,98 @@ riscv_macro_fusion_pair_p (rtx_insn *prev, rtx_insn 
*curr)
             INSN_UID (prev), INSN_UID (curr), riscv_fusion_type_name (op));
   return true;
 }
+
+/* Return a scheduling direction for MEM under the current tune.  Sorting
+   existing accesses requires their current offsets to be size-aligned.
+   This candidate query does not prove that a fusible partner exists.  */
+
+static enum riscv_fusion_direction
+riscv_fuse_mem_direction (const struct riscv_fusion_mem_info *mem)
+{
+  unsigned HOST_WIDE_INT fusible_ops = riscv_get_fusible_ops ();
+  bool inc_p = false, dec_p = false, any_p = false;
+  /* Collect directions from all enabled rules that accept this access.  */
+  for (const riscv_fusion_mem_rule &rule : riscv_fusion_mem_rules)
+    {
+      if (!(fusible_ops & rule.op)
+         || !riscv_fuse_mem_candidate_p (mem, rule))
+       continue;
+
+      HOST_WIDE_INT access_size = GET_MODE_SIZE (mem->mode).to_constant ();
+      if (INTVAL (mem->addr.offset) % access_size != 0)
+       continue;
+
+      inc_p |= rule.direction == RISCV_FUSION_INC;
+      dec_p |= rule.direction == RISCV_FUSION_DEC;
+      any_p |= rule.direction == RISCV_FUSION_ANY;
+    }
+
+  /* Prefer directional rules over bidirectional rules to preserve their
+     scheduling order when both are eligible.  */
+  if (inc_p && dec_p)
+    return RISCV_FUSION_ANY;
+  if (inc_p)
+    return RISCV_FUSION_INC;
+  if (dec_p)
+    return RISCV_FUSION_DEC;
+  return any_p ? RISCV_FUSION_ANY : RISCV_FUSION_NONE;
+}
+
+/* Implement TARGET_SCHED_FUSION_PRIORITY.  Group load/store pair candidates
+   by register file, access kind, mode and base register, then by offset.  */
+
+void
+riscv_sched_fusion_priority (rtx_insn *insn, int max_pri,
+                            int *fusion_pri, int *pri)
+{
+  struct riscv_fusion_mem_info mem;
+  unsigned int base_regno;
+  bool isload_p, fp_p, inc_p;
+  int fusion_type, tmp;
+
+  gcc_assert (INSN_P (insn));
+
+  /* Keep default priorities unless this instruction is a pair candidate.  */
+  tmp = max_pri - 1;
+  *fusion_pri = tmp;
+  *pri = tmp;
+
+  if (!riscv_fuse_mem_p (insn, &mem))
+    return;
+
+  enum riscv_fusion_direction direction
+    = riscv_fuse_mem_direction (&mem);
+  if (direction == RISCV_FUSION_NONE)
+    return;
+
+  base_regno = riscv_regno (mem.addr.reg);
+  if (base_regno >= FIRST_PSEUDO_REGISTER)
+    return;
+
+  isload_p = mem.type != SCHED_FUSION_ST;
+  fp_p = mem.fp_p;
+  /* Prefer increasing offsets when either direction is possible.  */
+  inc_p = direction != RISCV_FUSION_DEC;
+
+  /* Give each load/store class and base register a distinct priority below
+     that of unrelated instructions.  */
+  fusion_type = (fp_p ? 2 : 0) + (isload_p ? 0 : 1);
+  fusion_type *= NUM_MACHINE_MODES;
+  fusion_type += (int) mem.mode + 1;
+  *fusion_pri -= (fusion_type * FIRST_PSEUDO_REGISTER
+                 + (int) base_regno);
+
+  tmp /= 2;
+  HOST_WIDE_INT off_val = INTVAL (mem.addr.offset);
+  /* Use unsigned arithmetic to handle the most negative offset.  */
+  unsigned HOST_WIDE_INT magnitude = off_val < 0
+                                      ? -(unsigned HOST_WIDE_INT) off_val
+                                      : off_val;
+  int offset_pri = magnitude & 0xfffff;
+
+  /* Order offsets in the preferred pair direction.  */
+  if (inc_p == (off_val >= 0))
+    *pri = tmp - offset_pri;
+  else
+    *pri = tmp + offset_pri;
+}
diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index a89e93cbe30..153b315072a 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -889,6 +889,7 @@ extern bool riscv_macro_fusion_p (void);
 extern bool riscv_macro_fusion_pair_p (rtx_insn *, rtx_insn *);
 extern enum riscv_fusion_pairs riscv_get_fusion_pair_type (rtx_insn *,
                                                    rtx_insn *);
+extern void riscv_sched_fusion_priority (rtx_insn *, int, int *, int *);
 extern unsigned HOST_WIDE_INT riscv_get_fusible_ops (void);
 
 /* Routines implemented in thead.cc.  */
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 2a10bb70f90..d401d5c67c3 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -17222,6 +17222,8 @@ riscv_memtag_tag_bitsize ()
 #define TARGET_SCHED_MACRO_FUSION_P riscv_macro_fusion_p
 #undef TARGET_SCHED_MACRO_FUSION_PAIR_P
 #define TARGET_SCHED_MACRO_FUSION_PAIR_P riscv_macro_fusion_pair_p
+#undef TARGET_SCHED_FUSION_PRIORITY
+#define TARGET_SCHED_FUSION_PRIORITY riscv_sched_fusion_priority
 
 #undef TARGET_SCHED_INIT
 #define TARGET_SCHED_INIT riscv_sched_init
diff --git a/gcc/testsuite/gcc.target/riscv/sched-fusion-default.c 
b/gcc/testsuite/gcc.target/riscv/sched-fusion-default.c
new file mode 100644
index 00000000000..e2db021d50c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sched-fusion-default.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL input" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } 
} */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/* At -O2 the scheduler uses the tune's memory-pair priorities by default.  */
+/*
+**sched_fusion_load_pair:
+**     add     a3,a4,a5
+**     ld      a1,0\(a0\)
+**     ld      a2,8\(a0\)
+**     ret
+*/
+#include "sched-fusion-priority.c"
diff --git a/gcc/testsuite/gcc.target/riscv/sched-fusion-option-restore.c 
b/gcc/testsuite/gcc.target/riscv/sched-fusion-option-restore.c
new file mode 100644
index 00000000000..43854d5fa9a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sched-fusion-option-restore.c
@@ -0,0 +1,50 @@
+/* Check the common scheduling-fusion default at -Os and -O3 across
+   functions with explicit scheduling-fusion options.  */
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -Os -mtune=xt-c9501fdvt 
-fno-ipa-icf -fdump-rtl-sched_fusion" } */
+/* { dg-final { scan-rtl-dump-times ";; Function fusion_fast" 2 "sched_fusion" 
} } */
+/* { dg-final { scan-rtl-dump ";; Function fusion_explicit_on" "sched_fusion" 
} } */
+/* { dg-final { scan-rtl-dump-times ";; Function fusion_size" 3 "sched_fusion" 
} } */
+/* { dg-final { scan-rtl-dump-not ";; Function fusion_explicit_off" 
"sched_fusion" } } */
+
+int
+fusion_size_before (int a)
+{
+  return a + 1;
+}
+
+__attribute__ ((optimize ("O3"))) int
+fusion_fast (int a)
+{
+  return a + 2;
+}
+
+__attribute__ ((optimize ("Os"))) int
+fusion_size_local (int a)
+{
+  return a + 3;
+}
+
+__attribute__ ((optimize ("O3", "no-schedule-fusion"))) int
+fusion_explicit_off (int a)
+{
+  return a + 4;
+}
+
+__attribute__ ((optimize ("Os", "schedule-fusion"))) int
+fusion_explicit_on (int a)
+{
+  return a + 5;
+}
+
+__attribute__ ((optimize ("O3"))) int
+fusion_fast_again (int a)
+{
+  return a + 6;
+}
+
+int
+fusion_size_after (int a)
+{
+  return a + 7;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/sched-fusion-priority-extended.c 
b/gcc/testsuite/gcc.target/riscv/sched-fusion-priority-extended.c
new file mode 100644
index 00000000000..43ec1a0ba49
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sched-fusion-priority-extended.c
@@ -0,0 +1,221 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL input" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } 
} */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-fschedule-fusion -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/* Exercise signed loads, stores, zero stores and offset ordering on either
+   side of zero.  Unsigned word loads are not C9501 pair candidates.  */
+
+/*
+**sched_fusion_signed_loads:
+**     add     a3,a4,a5
+**     lw      a1,-8\(a0\)
+**     lw      a2,-4\(a0\)
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_signed_loads (void)
+{
+(function "sched_fusion_signed_loads"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (sign_extend:DI
+                      (mem:SI (plus:DI (reg:DI a0) (const_int -8))
+                      [0 S4 A32]))))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (reg:DI a2)
+                    (sign_extend:DI
+                      (mem:SI (plus:DI (reg:DI a0) (const_int -4))
+                      [0 S4 A32]))))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
+
+/*
+**sched_fusion_negative_loads:
+**     add     a3,a4,a5
+**     ld      a1,-16\(a0\)
+**     ld      a2,-8\(a0\)
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_negative_loads (void)
+{
+(function "sched_fusion_negative_loads"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (mem:DI (plus:DI (reg:DI a0) (const_int -16))
+                      [0 S8 A64])))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (reg:DI a2)
+                    (mem:DI (plus:DI (reg:DI a0) (const_int -8))
+                      [0 S8 A64])))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
+
+/*
+**sched_fusion_stores:
+**     add     a3,a4,a5
+**     sd      a1,0\(a0\)
+**     sd      a2,8\(a0\)
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_stores (void)
+{
+(function "sched_fusion_stores"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (mem:DI (reg:DI a0)
+                      [0 S8 A64])
+                    (reg:DI a1)))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (mem:DI (plus:DI (reg:DI a0) (const_int 8))
+                      [0 S8 A64])
+                    (reg:DI a2)))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
+
+/*
+**sched_fusion_cross_zero_stores:
+**     add     a3,a4,a5
+**     sw      a1,-4\(a0\)
+**     sw      a2,0\(a0\)
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_cross_zero_stores (void)
+{
+(function "sched_fusion_cross_zero_stores"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (mem:SI (plus:DI (reg:DI a0) (const_int -4))
+                      [0 S4 A32])
+                    (reg:SI a1)))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (mem:SI (reg:DI a0)
+                      [0 S4 A32])
+                    (reg:SI a2)))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
+
+/*
+**sched_fusion_zero_stores:
+**     add     a3,a4,a5
+**     sd      zero,16\(a0\)
+**     sd      zero,24\(a0\)
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_zero_stores (void)
+{
+(function "sched_fusion_zero_stores"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (mem:DI (plus:DI (reg:DI a0) (const_int 16))
+                      [0 S8 A64])
+                    (const_int 0)))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (mem:DI (plus:DI (reg:DI a0) (const_int 24))
+                      [0 S8 A64])
+                    (const_int 0)))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
+
+/*
+**sched_fusion_unsigned_loads:
+**     lwu     a1,0\(a0\)
+**     add     a3,a4,a5
+**     lwu     a2,4\(a0\)
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_unsigned_loads (void)
+{
+(function "sched_fusion_unsigned_loads"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (zero_extend:DI
+                      (mem:SI (reg:DI a0)
+                      [0 S4 A32]))))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (reg:DI a2)
+                    (zero_extend:DI
+                      (mem:SI (plus:DI (reg:DI a0) (const_int 4))
+                      [0 S4 A32]))))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
diff --git a/gcc/testsuite/gcc.target/riscv/sched-fusion-priority-off.c 
b/gcc/testsuite/gcc.target/riscv/sched-fusion-priority-off.c
new file mode 100644
index 00000000000..4bfda128a7a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sched-fusion-priority-off.c
@@ -0,0 +1,14 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL input" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } 
} */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-fno-schedule-fusion -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/* Disabling the pass preserves the original order.  */
+/*
+**sched_fusion_load_pair:
+**     ld      a1,0\(a0\)
+**     add     a3,a4,a5
+**     ld      a2,8\(a0\)
+**     ret
+*/
+#include "sched-fusion-priority.c"
diff --git a/gcc/testsuite/gcc.target/riscv/sched-fusion-priority.c 
b/gcc/testsuite/gcc.target/riscv/sched-fusion-priority.c
new file mode 100644
index 00000000000..9bbe56f3309
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sched-fusion-priority.c
@@ -0,0 +1,126 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2" } */
+/* { dg-additional-options "-fschedule-fusion -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/* Start before sched_fusion and disable sched2 to isolate the priority
+   hook.  */
+
+/*
+**sched_fusion_load_pair:
+**     ...
+**     ld      [a-z][0-9]+,0\(a0\)
+**     ld      [a-z][0-9]+,8\(a0\)
+**     ...
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_load_pair (void)
+{
+(function "sched_fusion_load_pair"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (mem:DI (reg:DI a0) [0 S8 A64])))
+      (cinsn 4 (set (reg:DI a3)
+                    (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (reg:DI a2)
+                    (mem:DI
+                      (plus:DI (reg:DI a0)
+                               (const_int 8)) [0 S8 A64])))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 10)
+  ) ;; insn-chain
+  (crtl
+    (return_rtx
+      (reg/i:DI a0)
+    ) ;; return_rtx
+  ) ;; crtl
+) ;; function "sched_fusion_load_pair"
+}
+
+/* Unaligned offsets must not receive memory-pair priorities.  */
+/*
+**sched_fusion_unaligned_loads:
+**     ...
+**     ld      [a-z][0-9]+,4\(a0\)
+**     add     [a-z][0-9]+,[a-z][0-9]+,[a-z][0-9]+
+**     ld      [a-z][0-9]+,12\(a0\)
+**     ...
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_unaligned_loads (void)
+{
+(function "sched_fusion_unaligned_loads"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (mem:DI
+                      (plus:DI (reg:DI a0)
+                               (const_int 4)) [0 S8 A32])))
+      (cinsn 4 (set (reg:DI a3)
+                    (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (reg:DI a2)
+                    (mem:DI
+                      (plus:DI (reg:DI a0)
+                               (const_int 12)) [0 S8 A32])))
+      (cinsn 6 (use (reg:DI a1)))
+      (cinsn 7 (use (reg:DI a2)))
+      (cinsn 8 (use (reg:DI a3)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 10)
+  ) ;; insn-chain
+  (crtl
+    (return_rtx (reg/i:DI a0))
+  ) ;; crtl
+) ;; function "sched_fusion_unaligned_loads"
+}
+
+/* Group initially decreasing, separated accesses in increasing order.  */
+/*
+**sched_fusion_reversed_input:
+**     add     a3,a4,a5
+**     ld      a2,0\(a0\)
+**     ld      a1,8\(a0\)
+**     add     a0,a1,a2
+**     ret
+*/
+long __RTL (startwith ("compgotos"))
+sched_fusion_reversed_input (void)
+{
+(function "sched_fusion_reversed_input"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (mem:DI (plus:DI (reg:DI a0) (const_int 8)) [0 S8 A64])))
+      (cinsn 4 (set (reg:DI a3) (plus:DI (reg:DI a4) (reg:DI a5))))
+      (cinsn 5 (set (reg:DI a2) (mem:DI (reg:DI a0) [0 S8 A64])))
+      (cinsn 6 (set (reg:DI a0) (plus:DI (reg:DI a1) (reg:DI a2))))
+      (cinsn 7 (use (reg:DI a3)))
+      (cinsn 8 (use (reg/i:DI a0)))
+      (cjump_insn 9 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
diff --git a/gcc/testsuite/gcc.target/riscv/sched-fusion-unsupported-tune.c 
b/gcc/testsuite/gcc.target/riscv/sched-fusion-unsupported-tune.c
new file mode 100644
index 00000000000..d26fa20818b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/sched-fusion-unsupported-tune.c
@@ -0,0 +1,17 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=rocket -O2 -fno-ipa-icf 
-fdump-rtl-sched_fusion" } */
+/* { dg-final { scan-rtl-dump ";; Function fusion_default" "sched_fusion" } } 
*/
+/* { dg-final { scan-rtl-dump ";; Function fusion_forced" "sched_fusion" } } */
+
+/* Rocket uses the common -O2 default even without memory-pair rules.  */
+int
+fusion_default (int a)
+{
+  return a + 1;
+}
+
+__attribute__ ((optimize ("schedule-fusion"))) int
+fusion_forced (int a)
+{
+  return a + 2;
+}
-- 
2.52.0

Reply via email to