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

commit r15-8948-gdc9ca4b2701f91b1efb82ea605f0ec7254e80974
Author: Jørgen Kvalsvik <j...@lambda.is>
Date:   Wed Mar 26 22:15:26 2025 +0100

    Add coverage_instrumentation_p
    
    Provide a helper for checking if any coverage (arc, conditions, paths)
    is enabled, rather than manually checking all the flags. This should
    make the intent clearer, and make it easier to maintain the checks when
    more flags are added.
    
    The function is forward declared in two header files as different passes
    tend to include different headers (profile.h vs value-prof.h). This
    could maybe be merged at some points, but profiling related symbols are
    already a bit spread out and should probably be handled in a targeted
    effort.
    
    gcc/ChangeLog:
    
            * builtins.cc (expand_builtin_fork_or_exec): Call
            coverage_instrumentation_p.
            * ipa-inline.cc (can_early_inline_edge_p): Likewise.
            * passes.cc (finish_optimization_passes): Likewise.
            * profile.cc (coverage_instrumentation_p): New function.
            * profile.h (coverage_instrumentation_p): New declaration.
            * tree-profile.cc (tree_profiling): Call
            coverage_instrumentation_p.
            (pass_ipa_tree_profile::gate): Likewise.
            * value-prof.h (coverage_instrumentation_p): New declaration.

Diff:
---
 gcc/builtins.cc     |  2 +-
 gcc/ipa-inline.cc   |  2 +-
 gcc/passes.cc       |  4 ++--
 gcc/profile.cc      |  7 +++++++
 gcc/profile.h       |  4 ++++
 gcc/tree-profile.cc | 11 ++++-------
 gcc/value-prof.h    |  4 ++++
 7 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index ff48546eafd6..a5f711a7b6a2 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -6362,7 +6362,7 @@ expand_builtin_fork_or_exec (tree fn, tree exp, rtx 
target, int ignore)
   tree call;
 
   /* If we are not profiling, just call the function.  */
-  if (!profile_arc_flag && !condition_coverage_flag && !path_coverage_flag)
+  if (!coverage_instrumentation_p ())
     return NULL_RTX;
 
   /* Otherwise call the wrapper.  This should be equivalent for the rest of
diff --git a/gcc/ipa-inline.cc b/gcc/ipa-inline.cc
index 38b4c9899012..d9fc111a9e76 100644
--- a/gcc/ipa-inline.cc
+++ b/gcc/ipa-inline.cc
@@ -701,7 +701,7 @@ can_early_inline_edge_p (struct cgraph_edge *e)
     }
   gcc_assert (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
              && gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)));
-  if ((profile_arc_flag || condition_coverage_flag || path_coverage_flag)
+  if (coverage_instrumentation_p ()
       && ((lookup_attribute ("no_profile_instrument_function",
                            DECL_ATTRIBUTES (caller->decl)) == NULL_TREE)
          != (lookup_attribute ("no_profile_instrument_function",
diff --git a/gcc/passes.cc b/gcc/passes.cc
index a8c75520c8d9..3c28db78f095 100644
--- a/gcc/passes.cc
+++ b/gcc/passes.cc
@@ -352,8 +352,8 @@ finish_optimization_passes (void)
   gcc::dump_manager *dumps = m_ctxt->get_dumps ();
 
   timevar_push (TV_DUMP);
-  if (profile_arc_flag || condition_coverage_flag || path_coverage_flag
-      || flag_test_coverage || flag_branch_probabilities)
+  if (coverage_instrumentation_p () || flag_test_coverage
+      || flag_branch_probabilities)
     {
       dumps->dump_start (pass_profile_1->static_pass_number, NULL);
       end_branch_prob ();
diff --git a/gcc/profile.cc b/gcc/profile.cc
index 8bba8b4398ff..0b222cf38640 100644
--- a/gcc/profile.cc
+++ b/gcc/profile.cc
@@ -1798,3 +1798,10 @@ end_branch_prob (void)
               total_num_conds);
     }
 }
+
+/* Return true if any cfg coverage/profiling is enabled; -fprofile-arcs
+   -fcondition-coverage -fpath-coverage.  */
+bool coverage_instrumentation_p ()
+{
+  return profile_arc_flag || condition_coverage_flag || path_coverage_flag;
+}
diff --git a/gcc/profile.h b/gcc/profile.h
index 78d69f45ff27..a97445b8f6f3 100644
--- a/gcc/profile.h
+++ b/gcc/profile.h
@@ -77,4 +77,8 @@ extern void get_working_sets (void);
    profile.cc.  */
 extern struct gcov_summary *profile_info;
 
+/* Return true if any cfg coverage/profiling is enabled; -fprofile-arcs
+   -fcondition-coverage -fpath-coverage.  */
+extern bool coverage_instrumentation_p ();
+
 #endif /* PROFILE_H */
diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc
index 8aaed78e60c0..fed218eb60bc 100644
--- a/gcc/tree-profile.cc
+++ b/gcc/tree-profile.cc
@@ -1908,7 +1908,7 @@ tree_profiling (void)
          thunk = true;
          /* When generate profile, expand thunk to gimple so it can be
             instrumented same way as other functions.  */
-         if (profile_arc_flag || condition_coverage_flag || path_coverage_flag)
+         if (coverage_instrumentation_p ())
            expand_thunk (node, false, true);
          /* Read cgraph profile but keep function as thunk at profile-use
             time.  */
@@ -1953,8 +1953,7 @@ tree_profiling (void)
   release_profile_file_filtering ();
 
   /* Drop pure/const flags from instrumented functions.  */
-  if (profile_arc_flag || condition_coverage_flag || path_coverage_flag
-      || flag_test_coverage)
+  if (coverage_instrumentation_p () || flag_test_coverage)
     FOR_EACH_DEFINED_FUNCTION (node)
       {
        if (!gimple_has_body_p (node->decl)
@@ -1986,8 +1985,7 @@ tree_profiling (void)
 
       push_cfun (DECL_STRUCT_FUNCTION (node->decl));
 
-      if (profile_arc_flag || condition_coverage_flag || path_coverage_flag
-         || flag_test_coverage)
+      if (coverage_instrumentation_p () || flag_test_coverage)
        FOR_EACH_BB_FN (bb, cfun)
          {
            gimple_stmt_iterator gsi;
@@ -2072,8 +2070,7 @@ pass_ipa_tree_profile::gate (function *)
      disabled.  */
   return (!in_lto_p && !flag_auto_profile
          && (flag_branch_probabilities || flag_test_coverage
-             || profile_arc_flag || condition_coverage_flag
-             || path_coverage_flag)
+             || coverage_instrumentation_p ())
          && !seen_error ());
 }
 
diff --git a/gcc/value-prof.h b/gcc/value-prof.h
index 5b1145a5aa2d..3d5395ecea70 100644
--- a/gcc/value-prof.h
+++ b/gcc/value-prof.h
@@ -116,5 +116,9 @@ extern void branch_prob (bool);
 extern void read_thunk_profile (struct cgraph_node *);
 extern void end_branch_prob (void);
 
+/* Return true if any cfg coverage/profiling is enabled; -fprofile-arcs
+   -fcondition-coverage -fpath-coverage.  */
+extern bool coverage_instrumentation_p ();
+
 #endif /* GCC_VALUE_PROF_H */

Reply via email to