https://gcc.gnu.org/g:72fbd3b2b2a497dbbe6599239bd61c5624203ed0

commit r15-2298-g72fbd3b2b2a497dbbe6599239bd61c5624203ed0
Author: Richard Sandiford <richard.sandif...@arm.com>
Date:   Thu Jul 25 08:54:22 2024 +0100

    rtl-ssa: Fix split_clobber_group tree insertion [PR116044]
    
    PR116044 is a regression in the testsuite on AMD GCN caused (again)
    by the split_clobber_group code.  The first patch in this area
    (g:71b31690a7c52413496e91bcc5ee4c68af2f366f) fixed a bug caused
    by carrying the old group over as one of the split ones.  That
    patch instead:
    
    - created two new groups
    - inserted them in the splay tree as neighbours of the old group
    - removed the old group, and
    - invalidated the old group (to force lazy recomputation when
      a clobber's parent group is queried)
    
    However, this left add_def trying to insert the new definition
    relative to a stale splay tree root.  The second patch
    (g:34f33ea801563e2eabb348e8d3e9344a91abfd48) attempted to fix
    that by inserting it relative to the new root.  But that's not
    always correct either.  We specifically want to insert it after
    the first of the two new groups, whether that group is the root
    or not.
    
    This patch does that, and tries to refactor the code to make
    it a bit less brittle.
    
    gcc/
            PR rtl-optimization/116044
            * rtl-ssa/functions.h (function_info::split_clobber_group): Return
            an array of two clobber_groups.
            * rtl-ssa/accesses.cc (function_info::split_clobber_group): Return
            the new clobber groups.  Don't modify the splay tree here.
            (function_info::add_def): Update call accordingly.  Generalize
            the splay tree insertion code so that the new definition can be
            inserted as a child of any existing node, not just the root.
            Fix the insertion used after calling split_clobber_group.

Diff:
---
 gcc/rtl-ssa/accesses.cc | 66 +++++++++++++++++++++++++++----------------------
 gcc/rtl-ssa/functions.h |  3 ++-
 2 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/gcc/rtl-ssa/accesses.cc b/gcc/rtl-ssa/accesses.cc
index 0bba8391b002..5450ea118d1b 100644
--- a/gcc/rtl-ssa/accesses.cc
+++ b/gcc/rtl-ssa/accesses.cc
@@ -792,12 +792,12 @@ function_info::merge_clobber_groups (clobber_info 
*clobber1,
 }
 
 // GROUP spans INSN, and INSN now sets the resource that GROUP clobbers.
-// Split GROUP around INSN, to form two new groups, and return the clobber
-// that comes immediately before INSN.
+// Split GROUP around INSN, to form two new groups.  The first of the
+// returned groups comes before INSN and the second comes after INSN.
 //
-// The resource that GROUP clobbers is known to have an associated
-// splay tree.  The caller must remove GROUP from the tree on return.
-clobber_info *
+// The caller is responsible for updating the def_splay_tree and chaining
+// the defs together.
+std::array<clobber_group *, 2>
 function_info::split_clobber_group (clobber_group *group, insn_info *insn)
 {
   // Search for either the previous or next clobber in the group.
@@ -835,14 +835,10 @@ function_info::split_clobber_group (clobber_group *group, 
insn_info *insn)
   auto *group1 = allocate<clobber_group> (first_clobber, prev, tree1.root ());
   auto *group2 = allocate<clobber_group> (next, last_clobber, tree2.root ());
 
-  // Insert GROUP2 into the splay tree as an immediate successor of GROUP1.
-  def_splay_tree::insert_child (group, 1, group2);
-  def_splay_tree::insert_child (group, 1, group1);
-
   // Invalidate the old group.
   group->set_last_clobber (nullptr);
 
-  return prev;
+  return { group1, group2 };
 }
 
 // Add DEF to the end of the function's list of definitions of
@@ -899,7 +895,7 @@ function_info::add_def (def_info *def)
   insn_info *insn = def->insn ();
 
   int comparison;
-  def_node *root = nullptr;
+  def_node *neighbor = nullptr;
   def_info *prev = nullptr;
   def_info *next = nullptr;
   if (*insn > *last->insn ())
@@ -909,8 +905,8 @@ function_info::add_def (def_info *def)
       if (def_splay_tree tree = last->splay_root ())
        {
          tree.splay_max_node ();
-         root = tree.root ();
-         last->set_splay_root (root);
+         last->set_splay_root (tree.root ());
+         neighbor = tree.root ();
        }
       prev = last;
     }
@@ -921,8 +917,8 @@ function_info::add_def (def_info *def)
       if (def_splay_tree tree = last->splay_root ())
        {
          tree.splay_min_node ();
-         root = tree.root ();
-         last->set_splay_root (root);
+         last->set_splay_root (tree.root ());
+         neighbor = tree.root ();
        }
       next = first;
     }
@@ -931,8 +927,8 @@ function_info::add_def (def_info *def)
       // Search the splay tree for an insertion point.
       def_splay_tree tree = need_def_splay_tree (last);
       comparison = lookup_def (tree, insn);
-      root = tree.root ();
-      last->set_splay_root (root);
+      last->set_splay_root (tree.root ());
+      neighbor = tree.root ();
 
       // Deal with cases in which we found an overlapping live range.
       if (comparison == 0)
@@ -943,22 +939,34 @@ function_info::add_def (def_info *def)
              add_clobber (clobber, group);
              return;
            }
-         prev = split_clobber_group (group, insn);
-         next = prev->next_def ();
+         auto new_groups = split_clobber_group (group, insn);
+
+         // Insert the two new groups immediately after GROUP.
+         def_splay_tree::insert_child (group, 1, new_groups[1]);
+         def_splay_tree::insert_child (group, 1, new_groups[0]);
+
+         // Remove GROUP.
          tree.remove_root ();
-         root = tree.root ();
-         last->set_splay_root (root);
+         last->set_splay_root (tree.root ());
+
+         prev = new_groups[0]->last_clobber ();
+         next = new_groups[1]->first_clobber ();
+
+         // DEF comes after the first group.  (new_groups[1] and -1 would
+         // also work.)
+         neighbor = new_groups[0];
+         comparison = 1;
        }
-      // COMPARISON is < 0 if DEF comes before ROOT or > 0 if DEF comes
-      // after ROOT.
+      // COMPARISON is < 0 if DEF comes before NEIGHBOR or > 0 if DEF comes
+      // after NEIGHBOR.
       else if (comparison < 0)
        {
-         next = first_def (root);
+         next = first_def (neighbor);
          prev = next->prev_def ();
        }
       else
        {
-         prev = last_def (root);
+         prev = last_def (neighbor);
          next = prev->next_def ();
        }
     }
@@ -973,12 +981,12 @@ function_info::add_def (def_info *def)
     append_clobber_to_group (clobber, need_clobber_group (prev_clobber));
   else if (clobber && next_clobber)
     prepend_clobber_to_group (clobber, need_clobber_group (next_clobber));
-  else if (root)
+  else if (neighbor)
     {
-      // If DEF comes before ROOT, insert DEF to ROOT's left,
-      // otherwise insert DEF to ROOT's right.
+      // If DEF comes before NEIGHBOR, insert DEF to NEIGHBOR's left,
+      // otherwise insert DEF to NEIGHBOR's right.
       def_node *node = need_def_node (def);
-      def_splay_tree::insert_child (root, comparison >= 0, node);
+      def_splay_tree::insert_child (neighbor, comparison >= 0, node);
     }
   if (prev)
     insert_def_after (def, prev);
diff --git a/gcc/rtl-ssa/functions.h b/gcc/rtl-ssa/functions.h
index 8be04f1aa969..567701c7995b 100644
--- a/gcc/rtl-ssa/functions.h
+++ b/gcc/rtl-ssa/functions.h
@@ -257,7 +257,8 @@ private:
   void append_clobber_to_group (clobber_info *, clobber_group *);
   void merge_clobber_groups (clobber_info *, clobber_info *,
                             def_info *);
-  clobber_info *split_clobber_group (clobber_group *, insn_info *);
+  std::array<clobber_group *, 2> split_clobber_group (clobber_group *,
+                                                     insn_info *);
 
   void append_def (def_info *);
   void add_def (def_info *);

Reply via email to