The fold-mem-offsets pass contained a target-specific workaround that
skipped basic blocks optimized for size, to avoid conflicting with
RISC-V's shorten-memrefs pass.  This penalized all targets.

Move the workaround to the RISC-V backend by disabling fold-mem-offsets
via SET_OPTION_IF_UNSET in riscv_option_override when optimizing for
size with compressed instructions enabled (the same condition that gates
the shorten-memrefs pass).  This preserves the RISC-V behavior while
allowing other targets to fold offsets in size-optimized blocks.

gcc/ChangeLog:

        * fold-mem-offsets.cc (pass_fold_mem_offsets::execute): Remove
        optimize_bb_for_size_p check.
        * config/riscv/riscv.cc (riscv_option_override): Disable
        flag_fold_mem_offsets when optimizing for size with compressed
        instructions.
---

 gcc/config/riscv/riscv.cc | 6 ++++++
 gcc/fold-mem-offsets.cc   | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index ff46ffdb4566..a7d83b1fbeaf 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -12547,6 +12547,12 @@ riscv_option_override (void)
                       param_cycle_accurate_model,
                       0);
 
+  /* Disable fold-mem-offsets when optimizing for size with compressed
+     instructions, as it conflicts with the shorten-memrefs pass.  */
+  if (optimize_size && (TARGET_RVC || TARGET_ZCA))
+    SET_OPTION_IF_UNSET (&global_options, &global_options_set,
+                        flag_fold_mem_offsets, 0);
+
   /* Function to allocate machine-dependent function status.  */
   init_machine_status = &riscv_init_machine_status;
 
diff --git a/gcc/fold-mem-offsets.cc b/gcc/fold-mem-offsets.cc
index a3c67cf11e95..2c1b7f154c01 100644
--- a/gcc/fold-mem-offsets.cc
+++ b/gcc/fold-mem-offsets.cc
@@ -882,12 +882,6 @@ pass_fold_mem_offsets::execute (function *fn)
   rtx_insn *insn;
   FOR_ALL_BB_FN (bb, fn)
     {
-      /* There is a conflict between this pass and RISCV's shorten-memrefs
-        pass.  For now disable folding if optimizing for size because
-        otherwise this cancels the effects of shorten-memrefs.  */
-      if (optimize_bb_for_size_p (bb))
-       continue;
-
       fold_info_map fold_info;
 
       bitmap_clear (&can_fold_insns);
-- 
2.34.1

Reply via email to