Inconsistent samples can give an unknown edge a zero count even when
the edge must execute on a path to or from a sampled block.  Mark such
edges using dominators and postdominators and leave them to the guessed
profile.  Assume that calls return.

Bootstrapped and regression tested on aarch64-linux-gnu.

gcc/ChangeLog:

        * auto-profile.cc (edge_info): Track edges that must execute.
        (afdo_propagate_edge, afdo_propagate_circuit): Do not infer zero for
        such edges.
        (afdo_mark_nonzero_edges): New.
        (afdo_propagate): Call it.

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

diff --git a/gcc/auto-profile.cc b/gcc/auto-profile.cc
index 9b9ab5f4d9a..d33e42bc516 100644
--- a/gcc/auto-profile.cc
+++ b/gcc/auto-profile.cc
@@ -163,14 +163,24 @@ namespace autofdo
 class edge_info
 {
 public:
-  edge_info () : count_ (profile_count::zero ().afdo ()), annotated_ (false) {}
+  edge_info ()
+    : count_ (profile_count::zero ().afdo ()), annotated_ (false),
+      nonzero_ (false)
+  {}
   bool is_annotated () const { return annotated_; }
   void set_annotated () { annotated_ = true; }
   profile_count get_count () const { return count_; }
   void set_count (profile_count count) { count_ = count; }
+  bool is_nonzero () const { return nonzero_; }
+  void set_nonzero ()
+  {
+    nonzero_ = true;
+  }
 private:
   profile_count count_;
   bool annotated_;
+  /* True if the edge must have executed.  */
+  bool nonzero_;
 };
 
 /* Represent a source location: (function_decl, lineno).  */
@@ -3649,12 +3659,18 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
     int num_unknown_edges = 0;
     int num_edges = 0;
     profile_count total_known_count = profile_count::zero ().afdo ();
+    /* Do not infer zero for an edge that must have executed.  */
+    bool unknown_edges_nonzero = false;
 
     FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
       {
        gcc_assert (AFDO_EINFO (e) != NULL);
        if (! AFDO_EINFO (e)->is_annotated ())
-         num_unknown_edges++, unknown_edge = e;
+         {
+           num_unknown_edges++, unknown_edge = e;
+           if (AFDO_EINFO (e)->is_nonzero ())
+             unknown_edges_nonzero = true;
+         }
        else
          total_known_count += AFDO_EINFO (e)->get_count ();
        num_edges++;
@@ -3704,7 +3720,8 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
        bb->count = total_known_count;
        changed = true;
       }
-    else if (num_unknown_edges == 1 && is_bb_annotated (bb, *annotated_bb))
+    else if (num_unknown_edges == 1 && is_bb_annotated (bb, *annotated_bb)
+            && (bb->count > total_known_count || !unknown_edges_nonzero))
       {
        if (bb->count > total_known_count)
          {
@@ -3726,6 +3743,7 @@ afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
       }
     else if (num_unknown_edges > 1
             && is_bb_annotated (bb, *annotated_bb)
+            && !unknown_edges_nonzero
             && (total_known_count >= bb->count || !bb->count.nonzero_p ()))
       {
        FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
@@ -3858,13 +3876,16 @@ afdo_propagate_circuit (const bb_set &annotated_bb)
           total++;
           only_one = ep;
           if (! (AFDO_EINFO (e)->get_count ()).nonzero_p ()
-             && ! AFDO_EINFO (ep)->is_annotated ())
+             && ! AFDO_EINFO (ep)->is_annotated ()
+             && ! AFDO_EINFO (ep)->is_nonzero ())
            {
              AFDO_EINFO (ep)->set_count (profile_count::zero ().afdo ());
              AFDO_EINFO (ep)->set_annotated ();
            }
        }
-      if (total == 1 && ! AFDO_EINFO (only_one)->is_annotated ())
+      if (total == 1 && ! AFDO_EINFO (only_one)->is_annotated ()
+         && (AFDO_EINFO (e)->get_count ().nonzero_p ()
+             || ! AFDO_EINFO (only_one)->is_nonzero ()))
        {
          AFDO_EINFO (only_one)->set_count (AFDO_EINFO (e)->get_count ());
          AFDO_EINFO (only_one)->set_annotated ();
@@ -3873,6 +3894,39 @@ afdo_propagate_circuit (const bb_set &annotated_bb)
   }
 }
 
+/* Mark edges that must execute on paths to or from sampled blocks.
+   Assume that calls return.  */
+
+static void
+afdo_mark_nonzero_edges (const bb_set &annotated_bb)
+{
+  for (bool successors_p : {false, true})
+    {
+      cdi_direction dir = successors_p ? CDI_POST_DOMINATORS : CDI_DOMINATORS;
+      auto_sbitmap visited (last_basic_block_for_fn (cfun));
+      bitmap_clear (visited);
+      basic_block bb;
+      FOR_ALL_BB_FN (bb, cfun)
+       if (is_bb_annotated (bb, annotated_bb) && bb->count.nonzero_p ())
+         {
+           /* A single edge entering a dominator, or leaving a
+              postdominator, must execute.  */
+           for (basic_block b = bb; b && bitmap_set_bit (visited, b->index);
+                b = get_immediate_dominator (dir, b))
+             {
+               edge e, single = NULL;
+               edge_iterator ei;
+               int n = 0;
+               FOR_EACH_EDGE (e, ei, successors_p ? b->succs : b->preds)
+                 if (!dominated_by_p (dir, successors_p ? e->dest : e->src, b))
+                   n++, single = e;
+               if (n == 1)
+                 AFDO_EINFO (single)->set_nonzero ();
+             }
+         }
+    }
+}
+
 /* Propagate the basic block count and edge count on the control flow
    graph.  We do the propagation iteratively until stabilize.  */
 
@@ -3900,6 +3954,8 @@ afdo_propagate (bb_set *annotated_bb)
          }
       }
 
+  afdo_mark_nonzero_edges (*annotated_bb);
+
   while (changed && i++ < 100)
     {
       changed = false;
-- 
2.34.1

Reply via email to