Offlining an unrealized inline instance can leave its call site without
samples.  Estimate a call count from the instance, preserve existing
positive samples, and combine instances at the same position.

Bootstrapped and regression tested on aarch64-linux-gnu.

gcc/ChangeLog:

        * auto-profile.cc (function_instance::head_count_estimate): New.
        (function_instance::offline_if_not_realized): Add an estimated count
        to unsampled call sites.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/auto-profile.cc | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index 174c3e86009..3fcff752ee7 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -422,6 +422,8 @@ public:
     return head_count_;
   }
 
+  gcov_type head_count_estimate () const;
+
   gcov_type
   timestamp () const
   {
@@ -2211,6 +2213,25 @@ function_instance::remove_external_functions
     }
 }
 
+/* Estimate the entry count from the first body sample or callsite.  Return
+   at least 1 when the instance has samples.  */
+
+gcov_type
+function_instance::head_count_estimate () const
+{
+  gcov_type count = 0;
+  if (!pos_counts.empty ())
+    count = pos_counts.begin ()->second.count;
+  else if (!callsites.empty ())
+    {
+      unsigned loc = callsites.begin ()->first.first;
+      for (auto const &iter : callsites)
+       if (iter.first.first == loc)
+         count += iter.second->head_count_estimate ();
+    }
+  return count ? count : total_count_ > 0;
+}
+
 /* Look for inline instances that was not realized and
    remove them while possibly merging them to offline variants.  */
 
@@ -2218,11 +2239,14 @@ void
 function_instance::offline_if_not_realized
        (vec <function_instance *> &new_functions)
 {
+  std::set<unsigned> added;
   for (callsite_map::const_iterator iter = callsites.begin ();
        iter != callsites.end ();)
     if (!iter->second->realized_p ())
       {
        function_instance *f = iter->second;
+       unsigned loc = iter->first.first;
+       gcov_type count = f->head_count_estimate ();
        if (dump_file)
          {
            fprintf (dump_file, "Offlining unrealized inline ");
@@ -2231,6 +2255,18 @@ function_instance::offline_if_not_realized
          }
        iter = callsites.erase (iter);
        offline (f, new_functions);
+       /* Keep the call block sampled.  Preserve positive samples and replace
+          an explicit zero.  */
+       position_count_map::const_iterator pos = pos_counts.find (loc);
+       if (count
+           && (pos == pos_counts.end () || pos->second.count == 0
+               || added.count (loc)))
+         {
+           added.insert (loc);
+           pos_counts[loc].count += count;
+           for (function_instance *s = this; s; s = s->inlined_to ())
+             s->total_count_ += count;
+         }
       }
     else
       {
-- 
2.34.1

Reply via email to