]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
rtl-ssa: Maintain clobber_group invariant [PR121757]
authorRichard Sandiford <rdsandiford@googlemail.com>
Fri, 5 Sep 2025 17:38:13 +0000 (18:38 +0100)
committerRichard Sandiford <rdsandiford@googlemail.com>
Fri, 5 Sep 2025 17:38:13 +0000 (18:38 +0100)
In order to reduce time complexity, rtl-ssa groups consecutive
clobbers together.  Each group of clobbers has a splay tree for
lookup and manipulation purposes.

This arrangement means that we might need to split a group (when
inserting a new non-clobber definition between two clobbers) or
to join consecutive groups together (when deleting an intervening
non-clobber definition).  To reduce the time complexity of these updates,
the back pointer from a clobber to its group is only updated lazily.
The invariant is supposed to be that the first clobber, last clobber,
and splay tree root have the right group at all times, whereas other
members of the group can have identifiably stale group pointers.

However, a lack of abstraction meant that only some splay tree lookups
correctly maintained this invariant.  Others did not update the group
pointer after installing a new root.

This patch adds a helper that maintains the invariant and uses it in
three places, one that was already correct and two that were wrong.
The original lookup_clobber is still used in other code that
manipulates groups as a whole.

gcc/
PR rtl-optimization/121757
* rtl-ssa/accesses.h (clobber_group::lookup_clobber): New member
function.
* rtl-ssa/accesses.cc (clobber_group::lookup_clobber): Likewise.
(clobber_group::prev_clobber, clobber_group::next_clobber)
(function_info::add_clobber): Use it.

gcc/testsuite/
PR rtl-optimization/121757
* g++.dg/pr121757.C: New test.

gcc/rtl-ssa/accesses.cc
gcc/rtl-ssa/accesses.h
gcc/testsuite/g++.dg/pr121757.C [new file with mode: 0644]

index 0415e976798185972d66ab524a2bf4bb94624b91..07dc52074eb7a15e40ded0a4194ff688ec3513b8 100644 (file)
@@ -398,22 +398,20 @@ set_node::print (pretty_printer *pp) const
 clobber_info *
 clobber_group::prev_clobber (insn_info *insn) const
 {
-  auto &tree = const_cast<clobber_tree &> (m_clobber_tree);
-  int comparison = lookup_clobber (tree, insn);
+  int comparison = lookup_clobber (insn);
   if (comparison <= 0)
-    return dyn_cast<clobber_info *> (tree.root ()->prev_def ());
-  return tree.root ();
+    return dyn_cast<clobber_info *> (m_clobber_tree.root ()->prev_def ());
+  return m_clobber_tree.root ();
 }
 
 // See the comment above the declaration.
 clobber_info *
 clobber_group::next_clobber (insn_info *insn) const
 {
-  auto &tree = const_cast<clobber_tree &> (m_clobber_tree);
-  int comparison = lookup_clobber (tree, insn);
+  int comparison = lookup_clobber (insn);
   if (comparison >= 0)
-    return dyn_cast<clobber_info *> (tree.root ()->next_def ());
-  return tree.root ();
+    return dyn_cast<clobber_info *> (m_clobber_tree.root ()->next_def ());
+  return m_clobber_tree.root ();
 }
 
 // See the comment above the declaration.
@@ -438,6 +436,17 @@ clobber_group::print (pretty_printer *pp) const
   pp_indentation (pp) -= 4;
 }
 
+// A wrapper around rtl_ssa::lookup_clobber that ensures that the root
+// of the splay tree always has the correct group.
+int
+clobber_group::lookup_clobber (insn_info *insn) const
+{
+  auto &tree = const_cast<clobber_tree &> (m_clobber_tree);
+  int result = rtl_ssa::lookup_clobber (tree, insn);
+  tree->update_group (const_cast<clobber_group *> (this));
+  return result;
+}
+
 // See the comment above the declaration.
 def_info *
 def_lookup::prev_def (insn_info *insn) const
@@ -659,14 +668,10 @@ function_info::add_clobber (clobber_info *clobber, clobber_group *group)
   // Search for either the previous or next clobber in the group.
   // The result is less than zero if CLOBBER should come before NEIGHBOR
   // or greater than zero if CLOBBER should come after NEIGHBOR.
-  int comparison = lookup_clobber (group->m_clobber_tree, clobber->insn ());
+  int comparison = group->lookup_clobber (clobber->insn ());
   gcc_checking_assert (comparison != 0);
   clobber_info *neighbor = group->m_clobber_tree.root ();
 
-  // Since HEIGHBOR is now the root of the splay tree, its group needs
-  // to be up-to-date.
-  neighbor->update_group (group);
-
   // If CLOBBER comes before NEIGHBOR, insert CLOBBER to NEIGHBOR's left,
   // otherwise insert CLOBBER to NEIGHBOR's right.
   clobber_info::splay_tree::insert_child (neighbor, comparison > 0, clobber);
index b3a31fb78db7e0bf569b4a585c8029f1d5e59312..8471a8d8d1313080b569de03ac915148d00e4aae 100644 (file)
@@ -944,6 +944,8 @@ private:
   void set_first_clobber (clobber_info *c) { m_clobber_or_set = c; }
   void set_last_clobber (clobber_info *c) { m_last_clobber = c; }
 
+  int lookup_clobber (insn_info *) const;
+
   // The value returned by last_clobber ().
   clobber_info *m_last_clobber;
 
diff --git a/gcc/testsuite/g++.dg/pr121757.C b/gcc/testsuite/g++.dg/pr121757.C
new file mode 100644 (file)
index 0000000..cf93976
--- /dev/null
@@ -0,0 +1,18 @@
+// { dg-require-effective-target shared }
+// { dg-options "-O2 -flto -shared" }
+
+struct QAbstractFileEngine {
+  virtual bool seek(long long);
+};
+struct QQmlPreviewFileEngine : QAbstractFileEngine {
+  bool seek(long long);
+  int m_contents;
+  QAbstractFileEngine * m_fallback;
+};
+bool f(void*);
+bool QQmlPreviewFileEngine::seek(long long newPos) {
+  if (m_fallback)
+    return  m_fallback->seek(newPos);
+  else
+    return f(&m_contents);
+}