https://gcc.gnu.org/g:4a43a06c7b2bcc3402ac69d6e5ce7b8008acc69a

commit r15-1545-g4a43a06c7b2bcc3402ac69d6e5ce7b8008acc69a
Author: Richard Sandiford <richard.sandif...@arm.com>
Date:   Fri Jun 21 15:40:10 2024 +0100

    rtl-ssa: Don't cost no-op moves
    
    No-op moves are given the code NOOP_MOVE_INSN_CODE if we plan
    to delete them later.  Such insns shouldn't be costed, partly
    because they're going to disappear, and partly because targets
    won't recognise the insn code.
    
    gcc/
            * rtl-ssa/changes.cc (rtl_ssa::changes_are_worthwhile): Don't
            cost no-op moves.
            * rtl-ssa/insns.cc (insn_info::calculate_cost): Likewise.

Diff:
---
 gcc/rtl-ssa/changes.cc | 6 +++++-
 gcc/rtl-ssa/insns.cc   | 7 ++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/gcc/rtl-ssa/changes.cc b/gcc/rtl-ssa/changes.cc
index 11639e81bb7..3101f2dc4fc 100644
--- a/gcc/rtl-ssa/changes.cc
+++ b/gcc/rtl-ssa/changes.cc
@@ -177,13 +177,17 @@ rtl_ssa::changes_are_worthwhile (array_slice<insn_change 
*const> changes,
   auto entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
   for (insn_change *change : changes)
     {
+      // Count zero for the old cost if the old instruction was a no-op
+      // move or had an unknown cost.  This should reduce the chances of
+      // making an unprofitable change.
       old_cost += change->old_cost ();
       basic_block cfg_bb = change->bb ()->cfg_bb ();
       bool for_speed = optimize_bb_for_speed_p (cfg_bb);
       if (for_speed)
        weighted_old_cost += (cfg_bb->count.to_sreal_scale (entry_count)
                              * change->old_cost ());
-      if (!change->is_deletion ())
+      if (!change->is_deletion ()
+         && INSN_CODE (change->rtl ()) != NOOP_MOVE_INSN_CODE)
        {
          change->new_cost = insn_cost (change->rtl (), for_speed);
          new_cost += change->new_cost;
diff --git a/gcc/rtl-ssa/insns.cc b/gcc/rtl-ssa/insns.cc
index 0171d93c357..68365e323ec 100644
--- a/gcc/rtl-ssa/insns.cc
+++ b/gcc/rtl-ssa/insns.cc
@@ -48,7 +48,12 @@ insn_info::calculate_cost () const
 {
   basic_block cfg_bb = BLOCK_FOR_INSN (m_rtl);
   temporarily_undo_changes (0);
-  m_cost_or_uid = insn_cost (m_rtl, optimize_bb_for_speed_p (cfg_bb));
+  if (INSN_CODE (m_rtl) == NOOP_MOVE_INSN_CODE)
+    // insn_cost also uses 0 to mean "don't know".  Callers that
+    // want to distinguish the cases will need to check INSN_CODE.
+    m_cost_or_uid = 0;
+  else
+    m_cost_or_uid = insn_cost (m_rtl, optimize_bb_for_speed_p (cfg_bb));
   redo_changes (0);
 }

Reply via email to