public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/kubaneko/heads/histogram)] adding histogram for empty bbs
@ 2023-02-16 16:27 Ondrej Kubanek
  0 siblings, 0 replies; 2+ messages in thread
From: Ondrej Kubanek @ 2023-02-16 16:27 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d7dd744d73928ce7a05ee2361467b3f601579055

commit d7dd744d73928ce7a05ee2361467b3f601579055
Author: Ondrej Kubanek <kubanek0ondrej@gmail.com>
Date:   Sat Aug 20 23:37:39 2022 +0200

    adding histogram for empty bbs

Diff:
---
 gcc/tree-profile.cc | 48 +++++++++++++++++++++++++++++++++++-------------
 gcc/value-prof.cc   | 24 ++++++++++++++++++++----
 gcc/value-prof.h    |  1 +
 3 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc
index d79212afddb..977e33065ca 100644
--- a/gcc/tree-profile.cc
+++ b/gcc/tree-profile.cc
@@ -352,25 +352,47 @@ gimple_gen_pow2_profiler (histogram_value value, unsigned tag)
   gsi_insert_before (&gsi, call, GSI_NEW_STMT);
 }
 
-/* Output instructions as GIMPLE trees to increment the power of two histogram
+/* Output instructions as GIMPLE trees to increment the histogram
    counter.  VALUE is the expression whose value is profiled.  TAG is the tag
    of the section for counters.  */
 
 void
-gimple_gen_histogram_profiler (histogram_value value, unsigned tag)
+gimple_gen_histogram_profiler (histogram_value value, unsigned tag) // , edge_def* edge
 {
   gimple *stmt = value->hvalue.stmt;
-  gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
-  tree ref_ptr = tree_coverage_counter_addr (tag, 0);
-  // TODO why does it crash
-  gcall *call;
-  tree val;
-
-  ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
-				      true, NULL_TREE, true, GSI_SAME_STMT);
-  val = prepare_instrumented_value (&gsi, value);
-  call = gimple_build_call (tree_histogram_profiler_fn, 2, ref_ptr, val);
-  gsi_insert_before (&gsi, call, GSI_NEW_STMT);
+  if (stmt){
+      gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
+      tree ref_ptr = tree_coverage_counter_addr (tag, 0);
+      gcall *call;
+      tree val;
+
+      ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
+                          true, NULL_TREE, true, GSI_SAME_STMT);
+      val = prepare_instrumented_value (&gsi, value);
+      call = gimple_build_call (tree_histogram_profiler_fn, 2, ref_ptr, val);
+      gsi_insert_before (&gsi, call, GSI_NEW_STMT);
+  } else {
+      edge_def *edge = value->hvalue.edge;
+      if (edge==NULL){
+          // BAD
+      }
+      gimple_stmt_iterator gsi;
+      gimple_seq seq = NULL;
+      gsi = gsi_start (seq);
+
+      tree ref_ptr = tree_coverage_counter_addr (tag, 0);
+      gcall *call;
+      tree val;
+
+      ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
+                          true, NULL_TREE, true, GSI_SAME_STMT);
+      val = prepare_instrumented_value (&gsi, value);
+      call = gimple_build_call (tree_histogram_profiler_fn, 2, ref_ptr, val);
+      gsi_insert_before (&gsi, call, GSI_NEW_STMT); // might crash
+
+      seq = gsi_seq (gsi);
+      gsi_insert_seq_on_edge (edge, seq);
+  }
 }
 
 /* Output instructions as GIMPLE trees for code to find the most N common
diff --git a/gcc/value-prof.cc b/gcc/value-prof.cc
index 06a584b392a..e65421ba49e 100644
--- a/gcc/value-prof.cc
+++ b/gcc/value-prof.cc
@@ -119,6 +119,19 @@ gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
    histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
    hist->hvalue.value = value;
    hist->hvalue.stmt = stmt;
+   hist->hvalue.edge = NULL;
+   hist->type = type;
+   return hist;
+}
+
+histogram_value
+gimple_alloc_histogram_value_edge (struct function *fun ATTRIBUTE_UNUSED,
+			      enum hist_type type, gimple *stmt, tree value, edge_def *edge)
+{
+   histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
+   hist->hvalue.value = value;
+   hist->hvalue.stmt = stmt;
+   hist->hvalue.edge = stmt ? NULL : edge;
    hist->type = type;
    return hist;
 }
@@ -1921,14 +1934,16 @@ gimple_histogram_values_to_profile(function *fun, histogram_values * values){
          tree var;
          gimple_stmt_iterator gsi;
          gsi = gsi_last_bb (loop->latch);
-         create_iv (build_int_cst_type (var, 0), build_int_cst (var, 1), NULL_TREE,
+         create_iv3 (build_int_cst_type (get_gcov_type(), 0), build_int_cst (get_gcov_type(), 1), NULL_TREE,
              loop, &gsi, true, &var, NULL);
          auto_vec<edge> exits = get_loop_exit_edges (loop);
          for ( auto exit : exits ){
               if (single_pred_p (exit->dest)){
-                values->safe_push (gimple_alloc_histogram_value (fun,
+                 gimple_stmt_iterator gsi_edge = gsi_start_bb (exit->src);
+                 gimple_seq seq = gsi_seq (gsi_edge);
+                 values->safe_push (gimple_alloc_histogram_value_edge (fun,
                                          HIST_TYPE_HISTOGRAM,
-                                         exit->src, var));
+                                         seq, var, exit));
               }
              //pridate ulozeni var do histogramu na zacated basic blocku exit->dest
              // TREE_TYPE (name)
@@ -1937,6 +1952,7 @@ gimple_histogram_values_to_profile(function *fun, histogram_values * values){
                }
          }
     }
+    gsi_commit_edge_inserts ();
 }
 
 /* Find values inside STMT for that we want to measure histograms and adds
@@ -1958,8 +1974,8 @@ gimple_find_values_to_profile (histogram_values *values)
   unsigned i;
   histogram_value hist = NULL;
   values->create (0);
+  gimple_histogram_values_to_profile(cfun, values);
   FOR_EACH_BB_FN (bb, cfun)
-    gimple_histogram_values_to_profile(cfun, values);
     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
       gimple_values_to_profile (gsi_stmt (gsi), values);
 
diff --git a/gcc/value-prof.h b/gcc/value-prof.h
index 91c46f2accb..0718f167ab1 100644
--- a/gcc/value-prof.h
+++ b/gcc/value-prof.h
@@ -48,6 +48,7 @@ struct histogram_value_t
     {
       tree value;		/* The value to profile.  */
       gimple *stmt;		/* Insn containing the value.  */
+      edge_def *edge;    /* For adding to empty bbs */
       gcov_type *counters;		        /* Pointer to first counter.  */
       struct histogram_value_t *next;		/* Linked list pointer.  */
     } hvalue;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gcc(refs/users/kubaneko/heads/histogram)] adding histogram for empty bbs
@ 2023-02-23 23:21 Ondrej Kubanek
  0 siblings, 0 replies; 2+ messages in thread
From: Ondrej Kubanek @ 2023-02-23 23:21 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0509b471bde3eff530fdc53c1707810fbc088602

commit 0509b471bde3eff530fdc53c1707810fbc088602
Author: Ondrej Kubanek <kubanek0ondrej@gmail.com>
Date:   Sat Aug 20 23:37:39 2022 +0200

    adding histogram for empty bbs

Diff:
---
 gcc/tree-profile.cc | 48 +++++++++++++++++++++++++++++++++++-------------
 gcc/value-prof.cc   | 24 ++++++++++++++++++++----
 gcc/value-prof.h    |  1 +
 3 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc
index 868fcb01ef3..327fb701144 100644
--- a/gcc/tree-profile.cc
+++ b/gcc/tree-profile.cc
@@ -352,25 +352,47 @@ gimple_gen_pow2_profiler (histogram_value value, unsigned tag)
   gsi_insert_before (&gsi, call, GSI_NEW_STMT);
 }
 
-/* Output instructions as GIMPLE trees to increment the power of two histogram
+/* Output instructions as GIMPLE trees to increment the histogram
    counter.  VALUE is the expression whose value is profiled.  TAG is the tag
    of the section for counters.  */
 
 void
-gimple_gen_histogram_profiler (histogram_value value, unsigned tag)
+gimple_gen_histogram_profiler (histogram_value value, unsigned tag) // , edge_def* edge
 {
   gimple *stmt = value->hvalue.stmt;
-  gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
-  tree ref_ptr = tree_coverage_counter_addr (tag, 0);
-  // TODO why does it crash
-  gcall *call;
-  tree val;
-
-  ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
-				      true, NULL_TREE, true, GSI_SAME_STMT);
-  val = prepare_instrumented_value (&gsi, value);
-  call = gimple_build_call (tree_histogram_profiler_fn, 2, ref_ptr, val);
-  gsi_insert_before (&gsi, call, GSI_NEW_STMT);
+  if (stmt){
+      gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
+      tree ref_ptr = tree_coverage_counter_addr (tag, 0);
+      gcall *call;
+      tree val;
+
+      ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
+                          true, NULL_TREE, true, GSI_SAME_STMT);
+      val = prepare_instrumented_value (&gsi, value);
+      call = gimple_build_call (tree_histogram_profiler_fn, 2, ref_ptr, val);
+      gsi_insert_before (&gsi, call, GSI_NEW_STMT);
+  } else {
+      edge_def *edge = value->hvalue.edge;
+      if (edge==NULL){
+          // BAD
+      }
+      gimple_stmt_iterator gsi;
+      gimple_seq seq = NULL;
+      gsi = gsi_start (seq);
+
+      tree ref_ptr = tree_coverage_counter_addr (tag, 0);
+      gcall *call;
+      tree val;
+
+      ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
+                          true, NULL_TREE, true, GSI_SAME_STMT);
+      val = prepare_instrumented_value (&gsi, value);
+      call = gimple_build_call (tree_histogram_profiler_fn, 2, ref_ptr, val);
+      gsi_insert_before (&gsi, call, GSI_NEW_STMT); // might crash
+
+      seq = gsi_seq (gsi);
+      gsi_insert_seq_on_edge (edge, seq);
+  }
 }
 
 /* Output instructions as GIMPLE trees for code to find the most N common
diff --git a/gcc/value-prof.cc b/gcc/value-prof.cc
index bb0a752ce30..3cd68b6a584 100644
--- a/gcc/value-prof.cc
+++ b/gcc/value-prof.cc
@@ -119,6 +119,19 @@ gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
    histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
    hist->hvalue.value = value;
    hist->hvalue.stmt = stmt;
+   hist->hvalue.edge = NULL;
+   hist->type = type;
+   return hist;
+}
+
+histogram_value
+gimple_alloc_histogram_value_edge (struct function *fun ATTRIBUTE_UNUSED,
+			      enum hist_type type, gimple *stmt, tree value, edge_def *edge)
+{
+   histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
+   hist->hvalue.value = value;
+   hist->hvalue.stmt = stmt;
+   hist->hvalue.edge = stmt ? NULL : edge;
    hist->type = type;
    return hist;
 }
@@ -1921,14 +1934,16 @@ gimple_histogram_values_to_profile(function *fun, histogram_values * values){
          tree var;
          gimple_stmt_iterator gsi;
          gsi = gsi_last_bb (loop->latch);
-         create_iv (build_int_cst_type (var, 0), build_int_cst (var, 1), NULL_TREE,
+         create_iv3 (build_int_cst_type (get_gcov_type(), 0), build_int_cst (get_gcov_type(), 1), NULL_TREE,
              loop, &gsi, true, &var, NULL);
          auto_vec<edge> exits = get_loop_exit_edges (loop);
          for ( auto exit : exits ){
               if (single_pred_p (exit->dest)){
-                values->safe_push (gimple_alloc_histogram_value (fun,
+                 gimple_stmt_iterator gsi_edge = gsi_start_bb (exit->src);
+                 gimple_seq seq = gsi_seq (gsi_edge);
+                 values->safe_push (gimple_alloc_histogram_value_edge (fun,
                                          HIST_TYPE_HISTOGRAM,
-                                         exit->src, var));
+                                         seq, var, exit));
               }
              //pridate ulozeni var do histogramu na zacated basic blocku exit->dest
              // TREE_TYPE (name)
@@ -1937,6 +1952,7 @@ gimple_histogram_values_to_profile(function *fun, histogram_values * values){
                }
          }
     }
+    gsi_commit_edge_inserts ();
 }
 
 /* Find values inside STMT for that we want to measure histograms and adds
@@ -1958,8 +1974,8 @@ gimple_find_values_to_profile (histogram_values *values)
   unsigned i;
   histogram_value hist = NULL;
   values->create (0);
+  gimple_histogram_values_to_profile(cfun, values);
   FOR_EACH_BB_FN (bb, cfun)
-    gimple_histogram_values_to_profile(cfun, values);
     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
       gimple_values_to_profile (gsi_stmt (gsi), values);
 
diff --git a/gcc/value-prof.h b/gcc/value-prof.h
index 991f29f8c6b..0097c7a6f4f 100644
--- a/gcc/value-prof.h
+++ b/gcc/value-prof.h
@@ -48,6 +48,7 @@ struct histogram_value_t
     {
       tree value;		/* The value to profile.  */
       gimple *stmt;		/* Insn containing the value.  */
+      edge_def *edge;    /* For adding to empty bbs */
       gcov_type *counters;		        /* Pointer to first counter.  */
       struct histogram_value_t *next;		/* Linked list pointer.  */
     } hvalue;

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-23 23:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-16 16:27 [gcc(refs/users/kubaneko/heads/histogram)] adding histogram for empty bbs Ondrej Kubanek
2023-02-23 23:21 Ondrej Kubanek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).