Make graph dumping work for fn != cfun

Message ID 20220128093801.1ED8213A6D@imap2.suse-dmz.suse.de
State Committed
Commit b500d2591ea0080459d5dee127f63c010530f6b6
Headers
Series Make graph dumping work for fn != cfun |

Commit Message

Richard Biener Jan. 28, 2022, 9:38 a.m. UTC
  The following makes dumping of a function as graph work as intended
when specifying a function other than cfun.  Unfortunately the loop
and the dominance APIs are not set up to work for other functions
than cfun so you won't get any fancy loop dumps but the non-loop
dump works up to reaching mark_dfs_back_edges which I trivially made
function aware and adjusted current callers with a wrapper.

With all this, doing dot-fn id->src_cfun from the debugger when
debugging inlining works.  Previously you got a strange mix of
the src and dest functions visualized ;)

Bootstrap & regtest running on x86_64-unknown-linux-gnu, will push
if it succeeds.

Richard.

2022-01-28  Richard Biener  <rguenther@suse.de>

	* cfganal.h (mark_dfs_back_edges): Provide API with struct
	function argument.
	* cfganal.cc (mark_dfs_back_edges): Take a struct function
	to work on, add a wrapper passing cfun.
	* graph.cc (draw_cfg_nodes_no_loops): Replace stray cfun
	uses with fun which is already passed.
	(draw_cfg_edges): Likewise.
	(draw_cfg_nodes_for_loop): Do not use draw_cfg_nodes_for_loop
	for fun != cfun.
---
 gcc/cfganal.cc | 28 +++++++++++++++++-----------
 gcc/cfganal.h  |  1 +
 gcc/graph.cc   | 15 ++++++++-------
 3 files changed, 26 insertions(+), 18 deletions(-)
  

Patch

diff --git a/gcc/cfganal.cc b/gcc/cfganal.cc
index e570d27768b..79c627a1716 100644
--- a/gcc/cfganal.cc
+++ b/gcc/cfganal.cc
@@ -58,7 +58,7 @@  private:
    and heavily borrowed from pre_and_rev_post_order_compute.  */
 
 bool
-mark_dfs_back_edges (void)
+mark_dfs_back_edges (struct function *fun)
 {
   int *pre;
   int *post;
@@ -67,20 +67,20 @@  mark_dfs_back_edges (void)
   bool found = false;
 
   /* Allocate the preorder and postorder number arrays.  */
-  pre = XCNEWVEC (int, last_basic_block_for_fn (cfun));
-  post = XCNEWVEC (int, last_basic_block_for_fn (cfun));
+  pre = XCNEWVEC (int, last_basic_block_for_fn (fun));
+  post = XCNEWVEC (int, last_basic_block_for_fn (fun));
 
   /* Allocate stack for back-tracking up CFG.  */
-  auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 1);
+  auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (fun) + 1);
 
   /* Allocate bitmap to track nodes that have been visited.  */
-  auto_sbitmap visited (last_basic_block_for_fn (cfun));
+  auto_sbitmap visited (last_basic_block_for_fn (fun));
 
   /* None of the nodes in the CFG have been visited yet.  */
   bitmap_clear (visited);
 
   /* Push the first edge on to the stack.  */
-  stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs));
+  stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (fun)->succs));
 
   while (!stack.is_empty ())
     {
@@ -94,8 +94,8 @@  mark_dfs_back_edges (void)
       ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
 
       /* Check if the edge destination has been visited yet.  */
-      if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && ! bitmap_bit_p (visited,
-								  dest->index))
+      if (dest != EXIT_BLOCK_PTR_FOR_FN (fun) && ! bitmap_bit_p (visited,
+								 dest->index))
 	{
 	  /* Mark that we have visited the destination.  */
 	  bitmap_set_bit (visited, dest->index);
@@ -112,14 +112,14 @@  mark_dfs_back_edges (void)
 	}
       else
 	{
-	  if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
-	      && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
+	  if (dest != EXIT_BLOCK_PTR_FOR_FN (fun)
+	      && src != ENTRY_BLOCK_PTR_FOR_FN (fun)
 	      && pre[src->index] >= pre[dest->index]
 	      && post[dest->index] == 0)
 	    ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
 
 	  if (ei_one_before_end_p (ei)
-	      && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
+	      && src != ENTRY_BLOCK_PTR_FOR_FN (fun))
 	    post[src->index] = postnum++;
 
 	  if (!ei_one_before_end_p (ei))
@@ -135,6 +135,12 @@  mark_dfs_back_edges (void)
   return found;
 }
 
+bool
+mark_dfs_back_edges (void)
+{
+  return mark_dfs_back_edges (cfun);
+}
+
 /* Find unreachable blocks.  An unreachable block will have 0 in
    the reachable bit in block->flags.  A nonzero value indicates the
    block is reachable.  */
diff --git a/gcc/cfganal.h b/gcc/cfganal.h
index 386cfbf211f..ac637de2b5a 100644
--- a/gcc/cfganal.h
+++ b/gcc/cfganal.h
@@ -49,6 +49,7 @@  private:
   bitmap_obstack m_bitmaps;
 };
 
+extern bool mark_dfs_back_edges (struct function *);
 extern bool mark_dfs_back_edges (void);
 extern void find_unreachable_blocks (void);
 extern void verify_no_unreachable_blocks (void);
diff --git a/gcc/graph.cc b/gcc/graph.cc
index 9990c8eccfd..bc29862fcad 100644
--- a/gcc/graph.cc
+++ b/gcc/graph.cc
@@ -169,14 +169,14 @@  draw_cfg_nodes_no_loops (pretty_printer *pp, struct function *fun)
   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
   int i, n;
 
-  auto_sbitmap visited (last_basic_block_for_fn (cfun));
+  auto_sbitmap visited (last_basic_block_for_fn (fun));
   bitmap_clear (visited);
 
   n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, true);
   for (i = n_basic_blocks_for_fn (fun) - n;
        i < n_basic_blocks_for_fn (fun); i++)
     {
-      basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
+      basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
       draw_cfg_node (pp, fun->funcdef_no, bb);
       bitmap_set_bit (visited, bb->index);
     }
@@ -248,7 +248,8 @@  draw_cfg_nodes_for_loop (pretty_printer *pp, int funcdef_no,
 static void
 draw_cfg_nodes (pretty_printer *pp, struct function *fun)
 {
-  if (loops_for_fn (fun))
+  /* ???  The loop and dominance APIs are dependent on fun == cfun.  */
+  if (fun == cfun && loops_for_fn (fun))
     draw_cfg_nodes_for_loop (pp, fun->funcdef_no, get_loop (fun, 0));
   else
     draw_cfg_nodes_no_loops (pp, fun);
@@ -267,7 +268,7 @@  draw_cfg_edges (pretty_printer *pp, struct function *fun)
   edge e;
   edge_iterator ei;
   unsigned int idx = 0;
-  FOR_EACH_BB_FN (bb, cfun)
+  FOR_EACH_BB_FN (bb, fun)
     FOR_EACH_EDGE (e, ei, bb->succs)
       {
 	if (e->flags & EDGE_DFS_BACK)
@@ -275,13 +276,13 @@  draw_cfg_edges (pretty_printer *pp, struct function *fun)
 	idx++;
       }
 
-  mark_dfs_back_edges ();
-  FOR_ALL_BB_FN (bb, cfun)
+  mark_dfs_back_edges (fun);
+  FOR_ALL_BB_FN (bb, fun)
     draw_cfg_node_succ_edges (pp, fun->funcdef_no, bb);
 
   /* Restore EDGE_DFS_BACK flag from dfs_back.  */
   idx = 0;
-  FOR_EACH_BB_FN (bb, cfun)
+  FOR_EACH_BB_FN (bb, fun)
     FOR_EACH_EDGE (e, ei, bb->succs)
       {
 	if (bitmap_bit_p (dfs_back, idx))