RISC-V memory-pair macro fusion requires two scalar accesses to be
adjacent in the final instruction stream.  Independent accesses can
be separated by other instructions or an in-place base update.  The
common pair-fusion pass can find and move such accesses using alias
and data-dependence information, but its gen_pair interface creates
one RTL insn for each selected pair.

Add a RISC-V pass before sched2 that uses the common pair-fusion search
and represents each selected pair as a temporary, recognized PARALLEL
insn containing two scalar SETs.  This gives sched2 one schedulable
unit for the pair.  A RISC-V pass immediately after sched2 restores
the two scalar insns, preserving their order, locations, register
notes, and frame-related notes.  Final assembly still contains the
ordinary scalar load or store instructions expected by the decoder.
The implementation does not modify gcc/pair-fusion.cc.

The common search can pair accesses across an in-place base update.
It moves the pair after the update and adjusts the earlier access's
offset to preserve its effective address.  For example:

    ld      a1,-8(s3)            addi    s3,s3,-8
    addi    s3,s3,-8      ->      ld      a4,8(s3)
    ld      a4,8(s3)              ld      a1,0(s3)

The RISC-V matcher checks the current tune's enabled pair operations,
access mode, register file, direction, offsets, and alignment.  The
pass is disabled by default.  It runs only with explicit
-mpair-fusion and a tune with a supported memory-pair operation.

Advantages of this representation are that it reuses the common
search and alias analysis without changing target-independent code,
keeps selected accesses adjacent through sched2, and emits the same
scalar ISA instructions as before.  The base-update case above is
also available without a separate target-specific search.

Its costs are an extra temporary RTL form and a late split.  The
split must keep register and CFI notes with the correct scalar insn;
this is covered by a targeted frame test but warrants review.  The
prototype currently handles direct 4- and 8-byte scalar memory SETs.
Byte and halfword stores are direct MEM SETs; adding them requires
RISC-V mode, matcher, and MD support when a tune enables such pairs.
The common pass discovers a load only when SET_SRC is a direct MEM.
It therefore misses sign- or zero-extended byte and halfword loads,
and sign-extended lw on RV64.  Supporting these RTL forms would likely
require extending common candidate discovery and pair construction,
or adding a RISC-V normalization step.  This is a lower-priority
follow-up.  The main RFC question is whether a temporary composite
RTL insn is an appropriate way to hold a pair through sched2.

C9501 SPEC CPU 2017 Integer was built with -march=rv64gcv,
-mabi=lp64d, -O3, -flto, and -mtune=xt-c9501fdvt.  The SPEC
configuration retained its other flags, including -static.  These
measurements use the pair pass; reproducing them requires
-mpair-fusion.  The table counts nonoverlapping adjacent pairs in
application functions in the final executables, using the C9501
rules within each basic block and excluding linked libraries.

Case                  Baseline  This patch  Change
500.perlbench_r           20636       22969  +11.31%
502.gcc_r                100603      106783   +6.14%
505.mcf_r                   159         177  +11.32%
520.omnetpp_r             18715       20387   +8.93%
523.xalancbmk_r           36113       38261   +5.95%
525.x264_r                 5004        5613  +12.17%
531.deepsjeng_r             495         557  +12.53%
541.leela_r                 846         927   +9.57%
548.exchange2_r            1900        2398  +26.21%
557.xz_r                   1504        1633   +8.58%
SUM                      185975      199705   +7.38%

Load-pair sites increased by 3.39% and store-pair sites by 12.18%.
Composite RTL counts also include pairs already adjacent in the
baseline, so the table uses the final scalar instruction stream.

This patch requires the preceding RISC-V macro-fusion matcher and
tune support used by the C9501 baseline.

gcc/ChangeLog:

        * config.gcc: Build the RISC-V pair fusion pass.
        * config/riscv/pair-fusion.md: New file.
        * config/riscv/riscv-pair-fusion.cc: New file.
        * config/riscv/riscv-passes.def: Register the fusion and split passes.
        * config/riscv/riscv-protos.h: Declare the pass and operand helpers.
        * config/riscv/riscv.md: Include pair-fusion.md.
        * config/riscv/riscv.opt: Add the pair fusion option and parameter.
        * config/riscv/t-riscv: Build riscv-pair-fusion.cc.
        * doc/invoke.texi: Document the pair fusion option.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/pair-fusion-composite-hazards.c: New test.
        * gcc.target/riscv/pair-fusion-composite.c: New test.
        * gcc.target/riscv/pair-fusion-composite-cfi.c: New test.
        * gcc.target/riscv/pair-fusion-default-off.c: New test.
        * gcc.target/riscv/pair-fusion-unsupported-tune.c: New test.

Signed-off-by: Jin Ma <[email protected]>
---
 gcc/config.gcc                                |   1 +
 gcc/config/riscv/pair-fusion.md               |  21 +
 gcc/config/riscv/riscv-pair-fusion.cc         | 408 ++++++++++++++++++
 gcc/config/riscv/riscv-passes.def             |   3 +-
 gcc/config/riscv/riscv-protos.h               |   4 +
 gcc/config/riscv/riscv.md                     |   1 +
 gcc/config/riscv/riscv.opt                    |   8 +
 gcc/config/riscv/t-riscv                      |   7 +
 gcc/doc/invoke.texi                           |  14 +-
 .../riscv/pair-fusion-composite-cfi.c         |  18 +
 .../riscv/pair-fusion-composite-hazards.c     |  55 +++
 .../gcc.target/riscv/pair-fusion-composite.c  |  71 +++
 .../riscv/pair-fusion-default-off.c           |  38 ++
 .../riscv/pair-fusion-unsupported-tune.c      |  38 ++
 14 files changed, 685 insertions(+), 2 deletions(-)
 create mode 100644 gcc/config/riscv/pair-fusion.md
 create mode 100644 gcc/config/riscv/riscv-pair-fusion.cc
 create mode 100644 gcc/testsuite/gcc.target/riscv/pair-fusion-composite-cfi.c
 create mode 100644 
gcc/testsuite/gcc.target/riscv/pair-fusion-composite-hazards.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/pair-fusion-composite.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/pair-fusion-default-off.c
 create mode 100644 
gcc/testsuite/gcc.target/riscv/pair-fusion-unsupported-tune.c

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 13a0757c5a61..bce2c4998016 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -585,6 +585,7 @@ riscv*)
        extra_objs="${extra_objs} riscv-v.o riscv-vsetvl.o riscv-vector-costs.o 
riscv-avlprop.o riscv-vect-permconst.o"
        extra_objs="${extra_objs} riscv-vector-builtins.o 
riscv-vector-builtins-shapes.o riscv-vector-builtins-bases.o 
sifive-vector-builtins-bases.o andes-vector-builtins-bases.o"
        extra_objs="${extra_objs} thead.o riscv-target-attr.o riscv-zicfilp.o 
riscv-bclr-lowest-set-bit.o riscv-opt-popretz.o riscv-fusion.o"
+       extra_objs="${extra_objs} riscv-pair-fusion.o"
        d_target_objs="riscv-d.o"
        extra_headers="riscv_vector.h riscv_crypto.h riscv_bitmanip.h 
riscv_th_vector.h sifive_vector.h andes_vector.h"
        target_gtfiles="$target_gtfiles 
\$(srcdir)/config/riscv/riscv-vector-builtins.cc"
diff --git a/gcc/config/riscv/pair-fusion.md b/gcc/config/riscv/pair-fusion.md
new file mode 100644
index 000000000000..861a4c8b6310
--- /dev/null
+++ b/gcc/config/riscv/pair-fusion.md
@@ -0,0 +1,21 @@
+;; Scalar load/store pairs stay together through sched2 and split before final.
+
+(define_insn "*riscv_fused_load_pair"
+  [(set (match_operand 0 "register_operand" "=r,f")
+       (match_operand 1 "memory_operand" "m,m"))
+   (set (match_operand 2 "register_operand" "=r,f")
+       (match_operand 3 "memory_operand" "m,m"))]
+  "riscv_pair_load_operands_p (operands)"
+  "#"
+  [(set_attr "type" "multi")
+   (set_attr "length" "8")])
+
+(define_insn "*riscv_fused_store_pair"
+  [(set (match_operand 0 "memory_operand" "=m,m")
+       (match_operand 1 "reg_or_0_operand" "rJ,f"))
+   (set (match_operand 2 "memory_operand" "=m,m")
+       (match_operand 3 "reg_or_0_operand" "rJ,f"))]
+  "riscv_pair_store_operands_p (operands)"
+  "#"
+  [(set_attr "type" "multi")
+   (set_attr "length" "8")])
diff --git a/gcc/config/riscv/riscv-pair-fusion.cc 
b/gcc/config/riscv/riscv-pair-fusion.cc
new file mode 100644
index 000000000000..94fc786f516a
--- /dev/null
+++ b/gcc/config/riscv/riscv-pair-fusion.cc
@@ -0,0 +1,408 @@
+/* Scalar memory pair fusion for RISC-V.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GCC is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC; see the file COPYING3.  If not see
+   <http://www.gnu.org/licenses/>.  */
+
+#define IN_TARGET_CODE 1
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "backend.h"
+#include "rtl.h"
+#include "memmodel.h"
+#include "emit-rtl.h"
+#include "cfgrtl.h"
+#include "tm_p.h"
+#include "tree-pass.h"
+#include "df.h"
+#include "pair-fusion.h"
+
+namespace {
+
+/* Extract a hard-register base and a constant offset.  */
+
+static bool
+riscv_pair_address_p (rtx mem, rtx *base, HOST_WIDE_INT *offset)
+{
+  rtx addr = XEXP (mem, 0);
+  if (REG_P (addr) && HARD_REGISTER_P (addr))
+    {
+      *base = addr;
+      *offset = 0;
+      return true;
+    }
+
+  if (GET_CODE (addr) != PLUS
+      || !REG_P (XEXP (addr, 0))
+      || !HARD_REGISTER_P (XEXP (addr, 0))
+      || !CONST_INT_P (XEXP (addr, 1)))
+    return false;
+
+  *base = XEXP (addr, 0);
+  *offset = INTVAL (XEXP (addr, 1));
+  return true;
+}
+
+/* Check two scalar accesses in instruction order against the enabled pairs.  
*/
+
+static bool
+riscv_pair_patterns_p (rtx *pats, bool load_p)
+{
+  rtx mem[2], reg[2], base[2];
+  HOST_WIDE_INT offset[2];
+  bool fp[2];
+
+  for (int i = 0; i < 2; ++i)
+    {
+      if (GET_CODE (pats[i]) != SET)
+       return false;
+      mem[i] = XEXP (pats[i], load_p);
+      reg[i] = XEXP (pats[i], !load_p);
+      if (!MEM_P (mem[i]) || MEM_VOLATILE_P (mem[i])
+         || !riscv_pair_address_p (mem[i], &base[i], &offset[i]))
+       return false;
+
+      if (REG_P (reg[i]) && HARD_REGISTER_P (reg[i]))
+       fp[i] = FP_REG_RTX_P (reg[i]);
+      else if (!load_p && reg[i] == const0_rtx)
+       fp[i] = false;
+      else
+       return false;
+    }
+
+  machine_mode mode = GET_MODE (mem[0]);
+  if (mode != GET_MODE (mem[1])
+      || !rtx_equal_p (base[0], base[1])
+      || fp[0] != fp[1])
+    return false;
+
+  poly_uint64 size = GET_MODE_SIZE (mode);
+  if (!known_eq (size, 4U) && !known_eq (size, 8U))
+    return false;
+
+  if (fp[0] ? !SCALAR_FLOAT_MODE_P (mode) : !SCALAR_INT_MODE_P (mode))
+    return false;
+
+  for (int i = 0; i < 2; ++i)
+    if (REG_P (reg[i]) && GET_MODE (reg[i]) != mode)
+      return false;
+
+  HOST_WIDE_INT access_size = size.to_constant ();
+  unsigned HOST_WIDE_INT unsigned_size = access_size;
+  bool inc_p = (offset[1] > offset[0]
+               && ((unsigned HOST_WIDE_INT) offset[1]
+                   - (unsigned HOST_WIDE_INT) offset[0]) == unsigned_size);
+  bool dec_p = (offset[0] > offset[1]
+               && ((unsigned HOST_WIDE_INT) offset[0]
+                   - (unsigned HOST_WIDE_INT) offset[1]) == unsigned_size);
+  unsigned HOST_WIDE_INT ops = riscv_get_fusible_ops ();
+  enum riscv_fusion_pairs inc_op = fp[0]
+    ? RISCV_FUSE_FLDFST_PAIR_INC : RISCV_FUSE_LDST_PAIR_INC;
+  enum riscv_fusion_pairs dec_op = fp[0]
+    ? RISCV_FUSE_FLDFST_PAIR_DEC : RISCV_FUSE_LDST_PAIR_DEC;
+
+  if (load_p
+      && (rtx_equal_p (reg[0], reg[1])
+         || rtx_equal_p (reg[0], base[0])))
+    return false;
+
+  if ((inc_p && (ops & inc_op)) || (dec_p && (ops & dec_op)))
+    return true;
+
+  return (!load_p && !fp[0] && (ops & RISCV_FUSE_ALIGNED_STD)
+         && (inc_p || dec_p)
+         && MIN (offset[0], offset[1]) % (2 * access_size) == 0);
+}
+
+/* Construct the scalar SETs represented by an MD pair pattern.  */
+
+static bool
+riscv_pair_operands_p (rtx *operands, bool load_p)
+{
+  rtx pats[2] = {
+    gen_rtx_SET (operands[0], operands[1]),
+    gen_rtx_SET (operands[2], operands[3])
+  };
+  return riscv_pair_patterns_p (pats, load_p);
+}
+
+class riscv_pair_fusion : public pair_fusion
+{
+public:
+  bool fpsimd_op_p (rtx reg_op, machine_mode, bool) final override
+  {
+    return REG_P (reg_op) && FP_REG_RTX_P (reg_op);
+  }
+
+  bool pair_operand_mode_ok_p (machine_mode mode) final override
+  {
+    poly_uint64 size = GET_MODE_SIZE (mode);
+    return (SCALAR_INT_MODE_P (mode) || SCALAR_FLOAT_MODE_P (mode))
+          && (known_eq (size, 4U) || known_eq (size, 8U));
+  }
+
+  bool pair_reg_operand_ok_p (bool load_p, rtx reg_op,
+                             machine_mode) final override
+  {
+    return (REG_P (reg_op) && HARD_REGISTER_P (reg_op))
+          || (!load_p && reg_op == const0_rtx);
+  }
+
+  int pair_mem_alias_check_limit () final override
+  {
+    return riscv_pair_fusion_alias_check_limit;
+  }
+
+  bool should_handle_writeback (writeback_type) final override
+  {
+    return false;
+  }
+
+  bool pair_mem_ok_with_policy (rtx, bool) final override
+  {
+    return true;
+  }
+
+  bool pair_mem_insn_p (rtx_insn *, bool &) final override
+  {
+    return false;
+  }
+
+  bool pair_mem_in_range_p (HOST_WIDE_INT) final override
+  {
+    return true;
+  }
+
+  rtx gen_pair (rtx *pats, rtx writeback, bool load_p) final override
+  {
+    gcc_assert (!writeback);
+    rtx ordered[2] = { pats[1], pats[0] };
+    if (!riscv_pair_patterns_p (ordered, load_p))
+      {
+       ordered[0] = pats[0];
+       ordered[1] = pats[1];
+      }
+    return gen_rtx_PARALLEL (VOIDmode,
+                            gen_rtvec (2, ordered[0], ordered[1]));
+  }
+
+  rtx destructure_pair (rtx[2], rtx, bool) final override
+  {
+    gcc_unreachable ();
+  }
+
+  rtx gen_promote_writeback_pair (rtx, rtx, rtx[2], bool) final override
+  {
+    gcc_unreachable ();
+  }
+};
+
+const pass_data pass_data_riscv_pair_fusion =
+{
+  RTL_PASS,
+  "riscv_pair_fusion",
+  OPTGROUP_NONE,
+  TV_MACH_DEP,
+  0, 0, 0, 0,
+  TODO_df_finish,
+};
+
+class pass_riscv_pair_fusion : public rtl_opt_pass
+{
+public:
+  pass_riscv_pair_fusion (gcc::context *ctxt)
+    : rtl_opt_pass (pass_data_riscv_pair_fusion, ctxt)
+  {}
+
+  bool gate (function *) final override
+  {
+    return (optimize > 0 && !optimize_debug && flag_riscv_pair_fusion
+           && reload_completed && !flag_selective_scheduling2
+           && !cfun->can_throw_non_call_exceptions
+           && (riscv_get_fusible_ops ()
+               & (RISCV_FUSE_LDST_PAIR_INC | RISCV_FUSE_LDST_PAIR_DEC
+                  | RISCV_FUSE_FLDFST_PAIR_INC
+                  | RISCV_FUSE_FLDFST_PAIR_DEC
+                  | RISCV_FUSE_ALIGNED_STD)) != 0);
+  }
+
+  unsigned int execute (function *) final override
+  {
+    riscv_pair_fusion pass;
+    pass.run ();
+    return 0;
+  }
+};
+
+/* Find the scalar instruction to which a frame note belongs.  */
+
+static int
+riscv_pair_note_arm (rtx note, rtx pats[2])
+{
+  if (GET_CODE (note) == SET)
+    {
+      for (int i = 0; i < 2; ++i)
+       if (rtx_equal_p (SET_DEST (note), SET_DEST (pats[i])))
+         return i;
+    }
+  else if (REG_P (note))
+    {
+      for (int i = 0; i < 2; ++i)
+       if (rtx_equal_p (note, SET_DEST (pats[i])))
+         return i;
+    }
+
+  return -1;
+}
+
+/* Attach the frame expression to the corresponding scalar instruction.  */
+
+static void
+riscv_split_pair_frame_expr (rtx expr, rtx pats[2], rtx_insn *insns[2])
+{
+  if (GET_CODE (expr) == PARALLEL)
+    {
+      for (int i = 0; i < XVECLEN (expr, 0); ++i)
+       riscv_split_pair_frame_expr (XVECEXP (expr, 0, i), pats, insns);
+      return;
+    }
+
+  int arm = riscv_pair_note_arm (expr, pats);
+  gcc_assert (arm >= 0);
+  RTX_FRAME_RELATED_P (insns[arm]) = 1;
+  add_reg_note (insns[arm], REG_FRAME_RELATED_EXPR, copy_rtx (expr));
+}
+
+/* Restore scalar RTL and distribute the CFI notes kept by pair_fusion.  */
+
+static void
+riscv_split_pair (rtx_insn *insn)
+{
+  rtx pat = PATTERN (insn);
+  rtx pats[2] = { XVECEXP (pat, 0, 0), XVECEXP (pat, 0, 1) };
+  rtx_insn *split[2];
+
+  for (int i = 0; i < 2; ++i)
+    {
+      split[i] = emit_insn_before (copy_rtx (pats[i]), insn);
+      INSN_LOCATION (split[i]) = INSN_LOCATION (insn);
+      BLOCK_FOR_INSN (split[i]) = BLOCK_FOR_INSN (insn);
+    }
+
+  for (rtx note = REG_NOTES (insn); note; note = XEXP (note, 1))
+    {
+      enum reg_note kind = REG_NOTE_KIND (note);
+      rtx value = XEXP (note, 0);
+      if (kind == REG_FRAME_RELATED_EXPR)
+       {
+         riscv_split_pair_frame_expr (value, pats, split);
+         continue;
+       }
+
+      int arm = riscv_pair_note_arm (value, pats);
+      if (arm < 0 && kind == REG_DEAD)
+       {
+         for (int i = 1; i >= 0; --i)
+           if (reg_mentioned_p (value, pats[i]))
+             {
+               arm = i;
+               break;
+             }
+       }
+      if (arm < 0)
+       arm = 1;
+
+      if (kind == REG_CFA_DEF_CFA || kind == REG_CFA_OFFSET
+         || kind == REG_CFA_RESTORE)
+       RTX_FRAME_RELATED_P (split[arm]) = 1;
+      add_reg_note (split[arm], kind, copy_rtx (value));
+    }
+
+  for (int i = 0; i < 2; ++i)
+    df_insn_rescan (split[i]);
+  df_insn_delete (insn);
+  delete_insn (insn);
+}
+
+const pass_data pass_data_riscv_pair_split =
+{
+  RTL_PASS,
+  "riscv_pair_split",
+  OPTGROUP_NONE,
+  TV_MACH_DEP,
+  0, 0, 0, 0, 0,
+};
+
+class pass_riscv_pair_split : public rtl_opt_pass
+{
+public:
+  pass_riscv_pair_split (gcc::context *ctxt)
+    : rtl_opt_pass (pass_data_riscv_pair_split, ctxt)
+  {}
+
+  unsigned int execute (function *) final override
+  {
+    for (rtx_insn *insn = get_insns (); insn;)
+      {
+       rtx_insn *next = NEXT_INSN (insn);
+       if (NONDEBUG_INSN_P (insn))
+         {
+           rtx pat = PATTERN (insn);
+           if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
+             {
+               rtx pats[2] = { XVECEXP (pat, 0, 0),
+                               XVECEXP (pat, 0, 1) };
+               if (GET_CODE (pats[0]) == SET)
+                 {
+                   bool load_p = MEM_P (SET_SRC (pats[0]));
+                   if (riscv_pair_patterns_p (pats, load_p))
+                     riscv_split_pair (insn);
+                 }
+             }
+         }
+       insn = next;
+      }
+    return 0;
+  }
+};
+
+}
+
+bool
+riscv_pair_load_operands_p (rtx *operands)
+{
+  return riscv_pair_operands_p (operands, true);
+}
+
+bool
+riscv_pair_store_operands_p (rtx *operands)
+{
+  return riscv_pair_operands_p (operands, false);
+}
+
+rtl_opt_pass *
+make_pass_riscv_pair_fusion (gcc::context *ctxt)
+{
+  return new pass_riscv_pair_fusion (ctxt);
+}
+
+rtl_opt_pass *
+make_pass_riscv_pair_split (gcc::context *ctxt)
+{
+  return new pass_riscv_pair_split (ctxt);
+}
diff --git a/gcc/config/riscv/riscv-passes.def 
b/gcc/config/riscv/riscv-passes.def
index bc6b3c8b98d8..a5ccb95a6d4c 100644
--- a/gcc/config/riscv/riscv-passes.def
+++ b/gcc/config/riscv/riscv-passes.def
@@ -24,4 +24,5 @@ INSERT_PASS_BEFORE (pass_fast_rtl_dce, 1, pass_vsetvl);
 INSERT_PASS_BEFORE (pass_shorten_branches, 1, pass_insert_landing_pad);
 INSERT_PASS_BEFORE (pass_shorten_branches, 1, pass_combine_popretz);
 INSERT_PASS_AFTER (pass_cse2, 1, pass_vector_permconst);
-
+INSERT_PASS_BEFORE (pass_sched2, 1, pass_riscv_pair_fusion);
+INSERT_PASS_AFTER (pass_sched2, 1, pass_riscv_pair_split);
diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index e1f90621f071..632ee67799f2 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -215,6 +215,8 @@ rtl_opt_pass * make_pass_insert_landing_pad (gcc::context 
*ctxt);
 rtl_opt_pass * make_pass_vector_permconst (gcc::context *ctxt);
 rtl_opt_pass * make_pass_bclr_lowest_set_bit (gcc::context *ctxt);
 rtl_opt_pass * make_pass_combine_popretz (gcc::context *ctxt);
+rtl_opt_pass *make_pass_riscv_pair_fusion (gcc::context *ctxt);
+rtl_opt_pass *make_pass_riscv_pair_split (gcc::context *ctxt);
 
 /* Routines implemented in riscv-vsetvl.cc.  */
 extern bool has_vtype_op (rtx_insn *);
@@ -887,6 +889,8 @@ enum riscv_fusion_pairs
 
 extern bool riscv_macro_fusion_p (void);
 extern bool riscv_macro_fusion_pair_p (rtx_insn *, rtx_insn *);
+extern bool riscv_pair_load_operands_p (rtx *);
+extern bool riscv_pair_store_operands_p (rtx *);
 extern unsigned HOST_WIDE_INT riscv_get_fusible_ops (void);
 
 /* Routines implemented in thead.cc.  */
diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index da6394d1ae6b..e2a650e20eae 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -5514,6 +5514,7 @@ (define_expand "aop<mode>3"
 (include "mips-insn.md")
 (include "sfb.md")
 (include "zc.md")
+(include "pair-fusion.md")
 ;; Vendor extensions
 (include "thead.md")
 (include "corev.md")
diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt
index 3226367bf8a9..48d2b5492355 100644
--- a/gcc/config/riscv/riscv.opt
+++ b/gcc/config/riscv/riscv.opt
@@ -120,6 +120,14 @@ Convert BASE + LARGE_OFFSET addresses to NEW_BASE + 
SMALL_OFFSET to allow more
 memory accesses to be generated as compressed instructions.  Currently targets
 32-bit integer load/stores.
 
+mpair-fusion
+Target Var(flag_riscv_pair_fusion) Optimization Init(0)
+Group scalar memory accesses for macro fusion after register allocation.
+
+-param=riscv-pair-fusion-alias-check-limit=
+Target Joined UInteger Var(riscv_pair_fusion_alias_check_limit) Init(8) 
IntegerRange(0,65536) Param
+Limit the number of alias checks for a candidate memory fusion pair.
+
 mcmodel=
 Target RejectNegative Joined Enum(code_model) Var(riscv_cmodel) 
Init(TARGET_DEFAULT_CMODEL) Save
 Specify the code model.
diff --git a/gcc/config/riscv/t-riscv b/gcc/config/riscv/t-riscv
index cdf2f87128ab..4a60dfdceaaf 100644
--- a/gcc/config/riscv/t-riscv
+++ b/gcc/config/riscv/t-riscv
@@ -102,6 +102,13 @@ riscv-fusion.o: $(srcdir)/config/riscv/riscv-fusion.cc 
$(CONFIG_H) \
        $(COMPILE) $<
        $(POSTCOMPILE)
 
+riscv-pair-fusion.o: $(srcdir)/config/riscv/riscv-pair-fusion.cc $(CONFIG_H) \
+  $(SYSTEM_H) coretypes.h $(TM_H) $(RTL_H) $(TREE_PASS_H) pair-fusion.h \
+  $(FUNCTION_H) $(EMIT_RTL_H) $(TM_P_H) $(DF_H) \
+  $(srcdir)/config/riscv/riscv-protos.h
+       $(COMPILE) $<
+       $(POSTCOMPILE)
+
 riscv-c.o: $(srcdir)/config/riscv/riscv-c.cc $(CONFIG_H) $(SYSTEM_H) \
     coretypes.h $(TM_H) $(TREE_H) output.h $(C_COMMON_H) $(TARGET_H)
        $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 66d80f17e740..4d24f0b058b6 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -1338,7 +1338,7 @@ See RS/6000 and PowerPC Options.
 -mcpu=@var{processor-string}  -mtune=@var{processor-string}
 -mpreferred-stack-boundary=@var{num}
 -msmall-data-limit=@var{N-bytes}
--msave-restore  -mno-shorten-memrefs
+-msave-restore  -mno-shorten-memrefs  -mpair-fusion
 -mstrict-align  -mscalar-strict-align  -mno-vector-strict-align
 -mzilsd-word-align  -mzilsd-strict-align
 -mcmodel=medlow  -mcmodel=medany  -mcmodel=large
@@ -31604,6 +31604,18 @@ of 'new base + small offset'.  If the new base gets 
stored in a compressed
 register, then the new load/store can be compressed.  Currently targets 32-bit
 integer load/stores only.
 
+@opindex mpair-fusion
+@opindex mno-pair-fusion
+@item -mpair-fusion
+@itemx -mno-pair-fusion
+Enable or disable scalar load/store pair fusion before the second instruction
+scheduling pass.  A paired RTL instruction is split back into two scalar
+instructions after scheduling.  The option is disabled by default.  It has
+an effect only when the selected tune supports load/store pair fusion.
+
+@item --param=riscv-pair-fusion-alias-check-limit=@var{n}
+Limit alias checks when considering a memory pair.  The default is 8.
+
 @opindex mstrict-align
 @opindex mno-strict-align
 @item -mstrict-align
diff --git a/gcc/testsuite/gcc.target/riscv/pair-fusion-composite-cfi.c 
b/gcc/testsuite/gcc.target/riscv/pair-fusion-composite-cfi.c
new file mode 100644
index 000000000000..60649d2e96bc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pair-fusion-composite-cfi.c
@@ -0,0 +1,18 @@
+/* Verify that frame-related pairs split before final and retain CFI notes.  */
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-mpair-fusion -fno-schedule-fusion -fno-schedule-insns2 -funwind-tables 
-fdump-rtl-riscv_pair_fusion-details" } */
+/* { dg-final { scan-rtl-dump "fusing pair" "riscv_pair_fusion" } } */
+/* { dg-final { scan-assembler {\.cfi_offset 8,} } } */
+/* { dg-final { scan-assembler {\.cfi_offset 9,} } } */
+/* { dg-final { scan-assembler {\.cfi_restore 8} } } */
+/* { dg-final { scan-assembler {\.cfi_restore 9} } } */
+
+extern void escape (void);
+
+void __attribute__ ((noinline, noipa))
+pair_backend_frame (void)
+{
+  asm volatile ("" ::: "s0", "s1");
+  escape ();
+  asm volatile ("" ::: "s0", "s1");
+}
diff --git a/gcc/testsuite/gcc.target/riscv/pair-fusion-composite-hazards.c 
b/gcc/testsuite/gcc.target/riscv/pair-fusion-composite-hazards.c
new file mode 100644
index 000000000000..6bcb6824cfca
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pair-fusion-composite-hazards.c
@@ -0,0 +1,55 @@
+/* Verify that dependencies and aliasing prevent composite pairs.  */
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL functions do not support LTO" { *-*-* } { "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-mpair-fusion -fno-schedule-fusion -fno-schedule-insns2 
-fdump-rtl-riscv_pair_fusion-details" } */
+/* { dg-final { scan-rtl-dump-not "fusing pair" "riscv_pair_fusion" } } */
+
+long __RTL (startwith ("*leaf_regs"))
+pair_backend_reg_hazard (void)
+{
+(function "pair_backend_reg_hazard"
+  (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 a1) (reg:DI a2))))
+      (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)))
+)
+}
+
+long __RTL (startwith ("*leaf_regs"))
+pair_backend_memory_barrier (void)
+{
+(function "pair_backend_memory_barrier"
+  (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 (mem:DI (reg:DI a3) [0 S8 A64]) (reg:DI a4)))
+      (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)))
+      (cjump_insn 8 (simple_return))
+      (edge-to exit)
+    )
+    (cbarrier 9)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
diff --git a/gcc/testsuite/gcc.target/riscv/pair-fusion-composite.c 
b/gcc/testsuite/gcc.target/riscv/pair-fusion-composite.c
new file mode 100644
index 000000000000..07a05a861085
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pair-fusion-composite.c
@@ -0,0 +1,71 @@
+/* Verify that composite pairs split into adjacent scalar instructions.  */
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL functions do not support LTO" { *-*-* } { "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-mpair-fusion -fno-schedule-fusion -fno-schedule-insns2 
-fdump-rtl-riscv_pair_fusion-details" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-final { scan-rtl-dump-times "fusing pair" 2 "riscv_pair_fusion" } } */
+
+/*
+**pair_backend_load:
+**     add     a3,a4,a5
+**     ld      a2,8\(a0\)
+**     ld      a1,0\(a0\)
+**     ret
+*/
+long __RTL (startwith ("*leaf_regs"))
+pair_backend_load (void)
+{
+(function "pair_backend_load"
+  (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)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
+
+/*
+**pair_backend_store:
+**     sd      a2,8\(a0\)
+**     sd      a1,0\(a0\)
+**     add     a3,a4,a5
+**     ret
+*/
+long __RTL (startwith ("*leaf_regs"))
+pair_backend_store (void)
+{
+(function "pair_backend_store"
+  (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)))
+)
+}
diff --git a/gcc/testsuite/gcc.target/riscv/pair-fusion-default-off.c 
b/gcc/testsuite/gcc.target/riscv/pair-fusion-default-off.c
new file mode 100644
index 000000000000..485a64d9ab59
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pair-fusion-default-off.c
@@ -0,0 +1,38 @@
+/* Verify that scalar pair fusion requires an explicit option.  */
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL functions do not support LTO" { *-*-* } { "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-fno-schedule-fusion -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/*
+**pair_backend_default_off:
+**     ld      a1,0\(a0\)
+**     add     a3,a4,a5
+**     ld      a2,8\(a0\)
+**     ret
+*/
+long __RTL (startwith ("*leaf_regs"))
+pair_backend_default_off (void)
+{
+(function "pair_backend_default_off"
+  (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)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}
diff --git a/gcc/testsuite/gcc.target/riscv/pair-fusion-unsupported-tune.c 
b/gcc/testsuite/gcc.target/riscv/pair-fusion-unsupported-tune.c
new file mode 100644
index 000000000000..cf3a7b4e41e2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pair-fusion-unsupported-tune.c
@@ -0,0 +1,38 @@
+/* Verify that an unsupported tune does not run scalar pair fusion.  */
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "RTL functions do not support LTO" { *-*-* } { "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=rocket -mpair-fusion -O2 
-fno-schedule-fusion -fno-schedule-insns2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/*
+**pair_backend_unsupported_tune:
+**     ld      a1,0\(a0\)
+**     add     a3,a4,a5
+**     ld      a2,8\(a0\)
+**     ret
+*/
+long __RTL (startwith ("*leaf_regs"))
+pair_backend_unsupported_tune (void)
+{
+(function "pair_backend_unsupported_tune"
+  (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)
+    )
+    (cbarrier 10)
+  )
+  (crtl (return_rtx (reg/i:DI a0)))
+)
+}

base-commit: 354716aa354505573f05fcfd4cd7e966bb94e1c4
-- 
2.52.0

Reply via email to