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

commit f949608860c4f86d87232ae607fba8465269a209
Author: Andrew Pinski <[email protected]>
Date:   Wed Apr 15 13:39:51 2026 -0700

    cfghooks: Pass data to callback function of make_forwarder_block
    
    This makes a cleanup that is way overdue and should have been done
    years ago. Instead of setting some global/static variables for the
    callback function to check here, we pass down the data to the callback
    function.  This reduces the number of global variables (which should help
    with Parallel GCC project). Plus since mfb_keep_just was exported outside
    of cfgloopmanip.cc (it was used in tree-ssa-threadupdate.cc), it reduces
    is shared between files.
    
    I found this useful when working on PR 123113 as I needed a new callback
    function.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
    gcc/ChangeLog:
    
            * cfghooks.cc (make_forwarder_block): New data argument,
            pass it down to redirect_edge_p.
            * cfghooks.h (make_forwarder_block): Add void* argument.
            * cfgloop.cc (mfb_reis_set): Remove.
            (mfb_redirect_edges_in_set): Add new data argument.
            Use it instead of mfb_reis_set.
            (form_subloop): Create a local variable instead of
            mfb_areis_set. Update call to make_forwarder_block.
            (merge_latch_edges): Likewise.
            * cfgloopmanip.cc (mfb_kj_edge): Remove.
            (mfb_keep_just):  Add new data argument.
            Use it instead of mfb_kj_edge.
            (create_preheader): Use local variable instead of
            mfb_kj_edge. Update call to make_forwarder_block.
            * cfgloopmanip.h (mfb_keep_just): Add void* argument.
            * tree-cfgcleanup.cc (mfb_keep_latches): Add unused void* arugment.
            (cleanup_tree_cfg_noloop): Update call to make_forwarder_block.
            * tree-ssa-threadupdate.cc
            (fwd_jt_path_registry::thread_through_loop_header): Use local
            variable instead of mfb_kj_edge. Update call to 
make_forwarder_block.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/cfghooks.cc              |  7 ++++---
 gcc/cfghooks.h               |  2 +-
 gcc/cfgloop.cc               | 24 ++++++++++++------------
 gcc/cfgloopmanip.cc          | 15 ++++++++-------
 gcc/cfgloopmanip.h           |  4 +---
 gcc/tree-cfgcleanup.cc       |  7 +++++--
 gcc/tree-ssa-threadupdate.cc |  7 ++++---
 7 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/gcc/cfghooks.cc b/gcc/cfghooks.cc
index 063639fe1c9a..6415d0fdec25 100644
--- a/gcc/cfghooks.cc
+++ b/gcc/cfghooks.cc
@@ -923,10 +923,11 @@ merge_blocks (basic_block a, basic_block b)
 
 /* Split BB into entry part and the rest (the rest is the newly created block).
    Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
-   part.  Returns the edge connecting the entry part to the rest.  */
+   part.  Returns the edge connecting the entry part to the rest.
+   DATA gets passed on to REDIRECT_EDGE_P.  */
 
 edge
-make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge))
+make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge, void*), 
void *data)
 {
   edge e, fallthru;
   edge_iterator ei;
@@ -947,7 +948,7 @@ make_forwarder_block (basic_block bb, bool 
(*redirect_edge_p) (edge))
     {
       basic_block e_src;
 
-      if (redirect_edge_p (e))
+      if (redirect_edge_p (e, data))
        {
          dummy->count += e->count ();
          ei_next (&ei);
diff --git a/gcc/cfghooks.h b/gcc/cfghooks.h
index 6cc1b66baef5..bc7956be2a8a 100644
--- a/gcc/cfghooks.h
+++ b/gcc/cfghooks.h
@@ -245,7 +245,7 @@ extern basic_block create_basic_block (gimple_seq, 
basic_block);
 extern basic_block create_empty_bb (basic_block);
 extern bool can_merge_blocks_p (basic_block, basic_block);
 extern void merge_blocks (basic_block, basic_block);
-extern edge make_forwarder_block (basic_block, bool (*)(edge));
+extern edge make_forwarder_block (basic_block, bool (*)(edge, void*), void*);
 extern basic_block force_nonfallthru (edge);
 extern void tidy_fallthru_edge (edge);
 extern void tidy_fallthru_edges (void);
diff --git a/gcc/cfgloop.cc b/gcc/cfgloop.cc
index fc4d635d8b65..70f2996a4165 100644
--- a/gcc/cfgloop.cc
+++ b/gcc/cfgloop.cc
@@ -709,13 +709,13 @@ find_subloop_latch_edge (class loop *loop)
 }
 
 /* Callback for make_forwarder_block.  Returns true if the edge E is marked
-   in the set MFB_REIS_SET.  */
+   in the set SET(DATA).  */
 
-static hash_set<edge> *mfb_reis_set;
 static bool
-mfb_redirect_edges_in_set (edge e)
+mfb_redirect_edges_in_set (edge e, void *data)
 {
-  return mfb_reis_set->contains (e);
+  hash_set<edge> *set = (hash_set<edge> *)data;
+  return set->contains (e);
 }
 
 /* Creates a subloop of LOOP with latch edge LATCH.  */
@@ -727,14 +727,14 @@ form_subloop (class loop *loop, edge latch)
   edge e, new_entry;
   class loop *new_loop;
 
-  mfb_reis_set = new hash_set<edge>;
+  hash_set<edge> *reis_set = new hash_set<edge>;
   FOR_EACH_EDGE (e, ei, loop->header->preds)
     {
       if (e != latch)
-       mfb_reis_set->add (e);
+       reis_set->add (e);
     }
-  new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set);
-  delete mfb_reis_set;
+  new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set, 
reis_set);
+  delete reis_set;
 
   loop->header = new_entry->src;
 
@@ -765,11 +765,11 @@ merge_latch_edges (class loop *loop)
       if (dump_file)
        fprintf (dump_file, "Merged latch edges of loop %d\n", loop->num);
 
-      mfb_reis_set = new hash_set<edge>;
+      hash_set<edge> *reis_set = new hash_set<edge>;
       FOR_EACH_VEC_ELT (latches, i, e)
-       mfb_reis_set->add (e);
-      latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set);
-      delete mfb_reis_set;
+       reis_set->add (e);
+      latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set, 
reis_set);
+      delete reis_set;
 
       loop->header = latch->dest;
       loop->latch = latch->src;
diff --git a/gcc/cfgloopmanip.cc b/gcc/cfgloopmanip.cc
index c953bc95b40f..dd5ae82f73ec 100644
--- a/gcc/cfgloopmanip.cc
+++ b/gcc/cfgloopmanip.cc
@@ -1640,14 +1640,14 @@ duplicate_loop_body_to_header_edge (class loop *loop, 
edge e,
 }
 
 /* A callback for make_forwarder block, to redirect all edges except for
-   MFB_KJ_EDGE to the entry part.  E is the edge for that we should decide
+   OTHER(DATA) to the entry part.  E is the edge for that we should decide
    whether to redirect it.  */
 
-edge mfb_kj_edge;
 bool
-mfb_keep_just (edge e)
+mfb_keep_just (edge e, void *data)
 {
-  return e != mfb_kj_edge;
+  edge other = (edge)data;
+  return e != other;
 }
 
 /* True when a candidate preheader BLOCK has predecessors from LOOP.  */
@@ -1729,15 +1729,16 @@ create_preheader (class loop *loop, int flags)
        return NULL;
     }
 
-  mfb_kj_edge = loop_latch_edge (loop);
-  latch_edge_was_fallthru = (mfb_kj_edge->flags & EDGE_FALLTHRU) != 0;
+  edge latch;
+  latch = loop_latch_edge (loop);
+  latch_edge_was_fallthru = (latch->flags & EDGE_FALLTHRU) != 0;
   if (nentry == 1
       && ((flags & CP_FALLTHRU_PREHEADERS) == 0
          || (single_entry->flags & EDGE_CROSSING) == 0))
     dummy = split_edge (single_entry);
   else
     {
-      edge fallthru = make_forwarder_block (loop->header, mfb_keep_just);
+      edge fallthru = make_forwarder_block (loop->header, mfb_keep_just, 
latch);
       dummy = fallthru->src;
       loop->header = fallthru->dest;
     }
diff --git a/gcc/cfgloopmanip.h b/gcc/cfgloopmanip.h
index 396e6d69e8c4..106cc7d005af 100644
--- a/gcc/cfgloopmanip.h
+++ b/gcc/cfgloopmanip.h
@@ -38,8 +38,6 @@ enum
                                                      discriminators to
                                                      distinguish loop
                                                      iterations.  */
-extern edge mfb_kj_edge;
-
 extern bool remove_path (edge, bool * = NULL, bitmap = NULL);
 extern void place_new_loop (struct function *, class loop *);
 extern void add_loop (class loop *, class loop *);
@@ -62,7 +60,7 @@ extern bool can_duplicate_loop_p (const class loop *loop);
 extern bool
 duplicate_loop_body_to_header_edge (class loop *, edge, unsigned, sbitmap, 
edge,
                                    vec<edge> *, int);
-extern bool mfb_keep_just (edge);
+extern bool mfb_keep_just (edge, void *);
 basic_block create_preheader (class loop *, int);
 extern void create_preheaders (int);
 extern void force_single_succ_latches (void);
diff --git a/gcc/tree-cfgcleanup.cc b/gcc/tree-cfgcleanup.cc
index c8b1a683824d..0f47f13591c1 100644
--- a/gcc/tree-cfgcleanup.cc
+++ b/gcc/tree-cfgcleanup.cc
@@ -1131,8 +1131,11 @@ cleanup_control_flow_pre ()
   return retval;
 }
 
+/* Callback function for make_forwarder_block which returns
+   true when E is not a latch.  */
+
 static bool
-mfb_keep_latches (edge e)
+mfb_keep_latches (edge e, void*)
 {
   return !((dom_info_available_p (CDI_DOMINATORS)
            && dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
@@ -1192,7 +1195,7 @@ cleanup_tree_cfg_noloop (unsigned ssa_update_flags)
               create a forwarder.  */
            if (found_latch && ! any_abnormal && n > 1)
              {
-               edge fallthru = make_forwarder_block (bb, mfb_keep_latches);
+               edge fallthru = make_forwarder_block (bb, mfb_keep_latches, 
NULL);
                loop->header = fallthru->dest;
                if (! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
                  {
diff --git a/gcc/tree-ssa-threadupdate.cc b/gcc/tree-ssa-threadupdate.cc
index 0e437b145514..db3520b42fc9 100644
--- a/gcc/tree-ssa-threadupdate.cc
+++ b/gcc/tree-ssa-threadupdate.cc
@@ -1867,9 +1867,10 @@ fwd_jt_path_registry::thread_through_loop_header (class 
loop *loop,
      must have only a single successor, but the original header had at
      least two successors.  */
   loop->latch = NULL;
-  mfb_kj_edge = single_succ_edge (new_preheader);
-  loop->header = mfb_kj_edge->dest;
-  latch = make_forwarder_block (tgt_bb, mfb_keep_just);
+  edge keep_edge;
+  keep_edge = single_succ_edge (new_preheader);
+  loop->header = keep_edge->dest;
+  latch = make_forwarder_block (tgt_bb, mfb_keep_just, keep_edge);
   loop->header = latch->dest;
   loop->latch = latch->src;
   return true;

Reply via email to