1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file contains various simple utilities to analyze the CFG. */
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "insn-config.h"
34 /* Store the data structures necessary for depth-first search. */
35 struct depth_first_search_dsS
{
36 /* stack for backtracking during the algorithm */
39 /* number of edges in the stack. That is, positions 0, ..., sp-1
43 /* record of basic blocks already seen by depth-first search */
44 sbitmap visited_blocks
;
46 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
48 static void flow_dfs_compute_reverse_init
49 PARAMS ((depth_first_search_ds
));
50 static void flow_dfs_compute_reverse_add_bb
51 PARAMS ((depth_first_search_ds
, basic_block
));
52 static basic_block flow_dfs_compute_reverse_execute
53 PARAMS ((depth_first_search_ds
));
54 static void flow_dfs_compute_reverse_finish
55 PARAMS ((depth_first_search_ds
));
56 static void remove_fake_successors
PARAMS ((basic_block
));
57 static bool need_fake_edge_p
PARAMS ((rtx
));
59 /* Return true if the block has no effect and only forwards control flow to
60 its single destination. */
63 forwarder_block_p (bb
)
68 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
69 || !bb
->succ
|| bb
->succ
->succ_next
)
72 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
73 if (INSN_P (insn
) && active_insn_p (insn
))
76 return (!INSN_P (insn
)
77 || (GET_CODE (insn
) == JUMP_INSN
&& simplejump_p (insn
))
78 || !active_insn_p (insn
));
81 /* Return nonzero if we can reach target from src by falling through. */
84 can_fallthru (src
, target
)
85 basic_block src
, target
;
88 rtx insn2
= target
->head
;
90 if (src
->index
+ 1 == target
->index
&& !active_insn_p (insn2
))
91 insn2
= next_active_insn (insn2
);
93 /* ??? Later we may add code to move jump tables offline. */
94 return next_active_insn (insn
) == insn2
;
97 /* Mark the back edges in DFS traversal.
98 Return non-zero if a loop (natural or otherwise) is present.
99 Inspired by Depth_First_Search_PP described in:
101 Advanced Compiler Design and Implementation
103 Morgan Kaufmann, 1997
105 and heavily borrowed from flow_depth_first_order_compute. */
108 mark_dfs_back_edges ()
119 /* Allocate the preorder and postorder number arrays. */
120 pre
= (int *) xcalloc (n_basic_blocks
, sizeof (int));
121 post
= (int *) xcalloc (n_basic_blocks
, sizeof (int));
123 /* Allocate stack for back-tracking up CFG. */
124 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
127 /* Allocate bitmap to track nodes that have been visited. */
128 visited
= sbitmap_alloc (n_basic_blocks
);
130 /* None of the nodes in the CFG have been visited yet. */
131 sbitmap_zero (visited
);
133 /* Push the first edge on to the stack. */
134 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
142 /* Look at the edge on the top of the stack. */
146 e
->flags
&= ~EDGE_DFS_BACK
;
148 /* Check if the edge destination has been visited yet. */
149 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
151 /* Mark that we have visited the destination. */
152 SET_BIT (visited
, dest
->index
);
154 pre
[dest
->index
] = prenum
++;
157 /* Since the DEST node has been visited for the first
158 time, check its successors. */
159 stack
[sp
++] = dest
->succ
;
162 post
[dest
->index
] = postnum
++;
166 if (dest
!= EXIT_BLOCK_PTR
&& src
!= ENTRY_BLOCK_PTR
167 && pre
[src
->index
] >= pre
[dest
->index
]
168 && post
[dest
->index
] == 0)
169 e
->flags
|= EDGE_DFS_BACK
, found
= true;
171 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
172 post
[src
->index
] = postnum
++;
175 stack
[sp
- 1] = e
->succ_next
;
184 sbitmap_free (visited
);
189 /* Return true if we need to add fake edge to exit.
190 Helper function for the flow_call_edges_add. */
193 need_fake_edge_p (insn
)
199 if ((GET_CODE (insn
) == CALL_INSN
200 && !SIBLING_CALL_P (insn
)
201 && !find_reg_note (insn
, REG_NORETURN
, NULL
)
202 && !find_reg_note (insn
, REG_ALWAYS_RETURN
, NULL
)
203 && !CONST_OR_PURE_CALL_P (insn
)))
206 return ((GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
207 && MEM_VOLATILE_P (PATTERN (insn
)))
208 || (GET_CODE (PATTERN (insn
)) == PARALLEL
209 && asm_noperands (insn
) != -1
210 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn
), 0, 0)))
211 || GET_CODE (PATTERN (insn
)) == ASM_INPUT
);
214 /* Add fake edges to the function exit for any non constant and non noreturn
215 calls, volatile inline assembly in the bitmap of blocks specified by
216 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
219 The goal is to expose cases in which entering a basic block does not imply
220 that all subsequent instructions must be executed. */
223 flow_call_edges_add (blocks
)
227 int blocks_split
= 0;
230 bool check_last_block
= false;
232 /* Map bb indices into basic block pointers since split_block
233 will renumber the basic blocks. */
235 bbs
= xmalloc (n_basic_blocks
* sizeof (*bbs
));
239 for (i
= 0; i
< n_basic_blocks
; i
++)
240 bbs
[bb_num
++] = BASIC_BLOCK (i
);
242 check_last_block
= true;
245 EXECUTE_IF_SET_IN_SBITMAP (blocks
, 0, i
,
247 bbs
[bb_num
++] = BASIC_BLOCK (i
);
248 if (i
== n_basic_blocks
- 1)
249 check_last_block
= true;
252 /* In the last basic block, before epilogue generation, there will be
253 a fallthru edge to EXIT. Special care is required if the last insn
254 of the last basic block is a call because make_edge folds duplicate
255 edges, which would result in the fallthru edge also being marked
256 fake, which would result in the fallthru edge being removed by
257 remove_fake_edges, which would result in an invalid CFG.
259 Moreover, we can't elide the outgoing fake edge, since the block
260 profiler needs to take this into account in order to solve the minimal
261 spanning tree in the case that the call doesn't return.
263 Handle this by adding a dummy instruction in a new last basic block. */
264 if (check_last_block
)
266 basic_block bb
= BASIC_BLOCK (n_basic_blocks
- 1);
269 /* Back up past insns that must be kept in the same block as a call. */
270 while (insn
!= bb
->head
271 && keep_with_call_p (insn
))
272 insn
= PREV_INSN (insn
);
274 if (need_fake_edge_p (insn
))
278 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
279 if (e
->dest
== EXIT_BLOCK_PTR
)
282 insert_insn_on_edge (gen_rtx_USE (VOIDmode
, const0_rtx
), e
);
283 commit_edge_insertions ();
287 /* Now add fake edges to the function exit for any non constant
288 calls since there is no way that we can determine if they will
291 for (i
= 0; i
< bb_num
; i
++)
293 basic_block bb
= bbs
[i
];
297 for (insn
= bb
->end
; ; insn
= prev_insn
)
299 prev_insn
= PREV_INSN (insn
);
300 if (need_fake_edge_p (insn
))
303 rtx split_at_insn
= insn
;
305 /* Don't split the block between a call and an insn that should
306 remain in the same block as the call. */
307 if (GET_CODE (insn
) == CALL_INSN
)
308 while (split_at_insn
!= bb
->end
309 && keep_with_call_p (NEXT_INSN (split_at_insn
)))
310 split_at_insn
= NEXT_INSN (split_at_insn
);
312 /* The handling above of the final block before the epilogue
313 should be enough to verify that there is no edge to the exit
314 block in CFG already. Calling make_edge in such case would
315 cause us to mark that edge as fake and remove it later. */
317 #ifdef ENABLE_CHECKING
318 if (split_at_insn
== bb
->end
)
319 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
320 if (e
->dest
== EXIT_BLOCK_PTR
)
324 /* Note that the following may create a new basic block
325 and renumber the existing basic blocks. */
326 e
= split_block (bb
, split_at_insn
);
330 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
333 if (insn
== bb
->head
)
345 /* Find unreachable blocks. An unreachable block will have 0 in
346 the reachable bit in block->flags. A non-zero value indicates the
347 block is reachable. */
350 find_unreachable_blocks ()
354 basic_block
*tos
, *worklist
;
357 tos
= worklist
= (basic_block
*) xmalloc (sizeof (basic_block
) * n
);
359 /* Clear all the reachability flags. */
361 for (i
= 0; i
< n
; ++i
)
362 BASIC_BLOCK (i
)->flags
&= ~BB_REACHABLE
;
364 /* Add our starting points to the worklist. Almost always there will
365 be only one. It isn't inconceivable that we might one day directly
366 support Fortran alternate entry points. */
368 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
372 /* Mark the block reachable. */
373 e
->dest
->flags
|= BB_REACHABLE
;
376 /* Iterate: find everything reachable from what we've already seen. */
378 while (tos
!= worklist
)
380 basic_block b
= *--tos
;
382 for (e
= b
->succ
; e
; e
= e
->succ_next
)
383 if (!(e
->dest
->flags
& BB_REACHABLE
))
386 e
->dest
->flags
|= BB_REACHABLE
;
393 /* Functions to access an edge list with a vector representation.
394 Enough data is kept such that given an index number, the
395 pred and succ that edge represents can be determined, or
396 given a pred and a succ, its index number can be returned.
397 This allows algorithms which consume a lot of memory to
398 represent the normally full matrix of edge (pred,succ) with a
399 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
400 wasted space in the client code due to sparse flow graphs. */
402 /* This functions initializes the edge list. Basically the entire
403 flowgraph is processed, and all edges are assigned a number,
404 and the data structure is filled in. */
409 struct edge_list
*elist
;
415 block_count
= n_basic_blocks
+ 2; /* Include the entry and exit blocks. */
419 /* Determine the number of edges in the flow graph by counting successor
420 edges on each basic block. */
421 for (x
= 0; x
< n_basic_blocks
; x
++)
423 basic_block bb
= BASIC_BLOCK (x
);
425 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
429 /* Don't forget successors of the entry block. */
430 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
433 elist
= (struct edge_list
*) xmalloc (sizeof (struct edge_list
));
434 elist
->num_blocks
= block_count
;
435 elist
->num_edges
= num_edges
;
436 elist
->index_to_edge
= (edge
*) xmalloc (sizeof (edge
) * num_edges
);
440 /* Follow successors of the entry block, and register these edges. */
441 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
442 elist
->index_to_edge
[num_edges
++] = e
;
444 for (x
= 0; x
< n_basic_blocks
; x
++)
446 basic_block bb
= BASIC_BLOCK (x
);
448 /* Follow all successors of blocks, and register these edges. */
449 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
450 elist
->index_to_edge
[num_edges
++] = e
;
456 /* This function free's memory associated with an edge list. */
459 free_edge_list (elist
)
460 struct edge_list
*elist
;
464 free (elist
->index_to_edge
);
469 /* This function provides debug output showing an edge list. */
472 print_edge_list (f
, elist
)
474 struct edge_list
*elist
;
478 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
479 elist
->num_blocks
- 2, elist
->num_edges
);
481 for (x
= 0; x
< elist
->num_edges
; x
++)
483 fprintf (f
, " %-4d - edge(", x
);
484 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR
)
485 fprintf (f
, "entry,");
487 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
489 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR
)
490 fprintf (f
, "exit)\n");
492 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
496 /* This function provides an internal consistency check of an edge list,
497 verifying that all edges are present, and that there are no
501 verify_edge_list (f
, elist
)
503 struct edge_list
*elist
;
505 int x
, pred
, succ
, index
;
508 for (x
= 0; x
< n_basic_blocks
; x
++)
510 basic_block bb
= BASIC_BLOCK (x
);
512 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
514 pred
= e
->src
->index
;
515 succ
= e
->dest
->index
;
516 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
517 if (index
== EDGE_INDEX_NO_EDGE
)
519 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
523 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
524 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
525 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
526 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
527 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
528 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
532 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
534 pred
= e
->src
->index
;
535 succ
= e
->dest
->index
;
536 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
537 if (index
== EDGE_INDEX_NO_EDGE
)
539 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
543 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
544 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
545 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
546 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
547 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
548 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
551 /* We've verified that all the edges are in the list, no lets make sure
552 there are no spurious edges in the list. */
554 for (pred
= 0; pred
< n_basic_blocks
; pred
++)
555 for (succ
= 0; succ
< n_basic_blocks
; succ
++)
557 basic_block p
= BASIC_BLOCK (pred
);
558 basic_block s
= BASIC_BLOCK (succ
);
561 for (e
= p
->succ
; e
; e
= e
->succ_next
)
568 for (e
= s
->pred
; e
; e
= e
->pred_next
)
575 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), BASIC_BLOCK (succ
))
576 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
577 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
579 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), BASIC_BLOCK (succ
))
580 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
581 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
582 pred
, succ
, EDGE_INDEX (elist
, BASIC_BLOCK (pred
),
583 BASIC_BLOCK (succ
)));
586 for (succ
= 0; succ
< n_basic_blocks
; succ
++)
588 basic_block p
= ENTRY_BLOCK_PTR
;
589 basic_block s
= BASIC_BLOCK (succ
);
592 for (e
= p
->succ
; e
; e
= e
->succ_next
)
599 for (e
= s
->pred
; e
; e
= e
->pred_next
)
606 if (EDGE_INDEX (elist
, ENTRY_BLOCK_PTR
, BASIC_BLOCK (succ
))
607 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
608 fprintf (f
, "*** Edge (entry, %d) appears to not have an index\n",
610 if (EDGE_INDEX (elist
, ENTRY_BLOCK_PTR
, BASIC_BLOCK (succ
))
611 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
612 fprintf (f
, "*** Edge (entry, %d) has index %d, but no edge exists\n",
613 succ
, EDGE_INDEX (elist
, ENTRY_BLOCK_PTR
,
614 BASIC_BLOCK (succ
)));
617 for (pred
= 0; pred
< n_basic_blocks
; pred
++)
619 basic_block p
= BASIC_BLOCK (pred
);
620 basic_block s
= EXIT_BLOCK_PTR
;
623 for (e
= p
->succ
; e
; e
= e
->succ_next
)
630 for (e
= s
->pred
; e
; e
= e
->pred_next
)
637 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), EXIT_BLOCK_PTR
)
638 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
639 fprintf (f
, "*** Edge (%d, exit) appears to not have an index\n",
641 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), EXIT_BLOCK_PTR
)
642 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
643 fprintf (f
, "*** Edge (%d, exit) has index %d, but no edge exists\n",
644 pred
, EDGE_INDEX (elist
, BASIC_BLOCK (pred
),
649 /* This routine will determine what, if any, edge there is between
650 a specified predecessor and successor. */
653 find_edge_index (edge_list
, pred
, succ
)
654 struct edge_list
*edge_list
;
655 basic_block pred
, succ
;
659 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
660 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
661 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
664 return (EDGE_INDEX_NO_EDGE
);
667 /* Dump the list of basic blocks in the bitmap NODES. */
670 flow_nodes_print (str
, nodes
, file
)
680 fprintf (file
, "%s { ", str
);
681 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, node
, {fprintf (file
, "%d ", node
);});
685 /* Dump the list of edges in the array EDGE_LIST. */
688 flow_edge_list_print (str
, edge_list
, num_edges
, file
)
690 const edge
*edge_list
;
699 fprintf (file
, "%s { ", str
);
700 for (i
= 0; i
< num_edges
; i
++)
701 fprintf (file
, "%d->%d ", edge_list
[i
]->src
->index
,
702 edge_list
[i
]->dest
->index
);
708 /* This routine will remove any fake successor edges for a basic block.
709 When the edge is removed, it is also removed from whatever predecessor
713 remove_fake_successors (bb
)
718 for (e
= bb
->succ
; e
;)
723 if ((tmp
->flags
& EDGE_FAKE
) == EDGE_FAKE
)
728 /* This routine will remove all fake edges from the flow graph. If
729 we remove all fake successors, it will automatically remove all
730 fake predecessors. */
737 for (x
= 0; x
< n_basic_blocks
; x
++)
738 remove_fake_successors (BASIC_BLOCK (x
));
740 /* We've handled all successors except the entry block's. */
741 remove_fake_successors (ENTRY_BLOCK_PTR
);
744 /* This function will add a fake edge between any block which has no
745 successors, and the exit block. Some data flow equations require these
749 add_noreturn_fake_exit_edges ()
753 for (x
= 0; x
< n_basic_blocks
; x
++)
754 if (BASIC_BLOCK (x
)->succ
== NULL
)
755 make_single_succ_edge (BASIC_BLOCK (x
), EXIT_BLOCK_PTR
, EDGE_FAKE
);
758 /* This function adds a fake edge between any infinite loops to the
759 exit block. Some optimizations require a path from each node to
762 See also Morgan, Figure 3.10, pp. 82-83.
764 The current implementation is ugly, not attempting to minimize the
765 number of inserted fake edges. To reduce the number of fake edges
766 to insert, add fake edges from _innermost_ loops containing only
767 nodes not reachable from the exit block. */
770 connect_infinite_loops_to_exit ()
772 basic_block unvisited_block
;
773 struct depth_first_search_dsS dfs_ds
;
775 /* Perform depth-first search in the reverse graph to find nodes
776 reachable from the exit block. */
777 flow_dfs_compute_reverse_init (&dfs_ds
);
778 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR
);
780 /* Repeatedly add fake edges, updating the unreachable nodes. */
783 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
);
784 if (!unvisited_block
)
787 make_edge (unvisited_block
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
788 flow_dfs_compute_reverse_add_bb (&dfs_ds
, unvisited_block
);
791 flow_dfs_compute_reverse_finish (&dfs_ds
);
795 /* Compute reverse top sort order */
798 flow_reverse_top_sort_order_compute (rts_order
)
806 /* Allocate stack for back-tracking up CFG. */
807 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
810 /* Allocate bitmap to track nodes that have been visited. */
811 visited
= sbitmap_alloc (n_basic_blocks
);
813 /* None of the nodes in the CFG have been visited yet. */
814 sbitmap_zero (visited
);
816 /* Push the first edge on to the stack. */
817 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
825 /* Look at the edge on the top of the stack. */
830 /* Check if the edge destination has been visited yet. */
831 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
833 /* Mark that we have visited the destination. */
834 SET_BIT (visited
, dest
->index
);
837 /* Since the DEST node has been visited for the first
838 time, check its successors. */
839 stack
[sp
++] = dest
->succ
;
841 rts_order
[postnum
++] = dest
->index
;
845 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
846 rts_order
[postnum
++] = src
->index
;
849 stack
[sp
- 1] = e
->succ_next
;
856 sbitmap_free (visited
);
859 /* Compute the depth first search order and store in the array
860 DFS_ORDER if non-zero, marking the nodes visited in VISITED. If
861 RC_ORDER is non-zero, return the reverse completion number for each
862 node. Returns the number of nodes visited. A depth first search
863 tries to get as far away from the starting point as quickly as
867 flow_depth_first_order_compute (dfs_order
, rc_order
)
874 int rcnum
= n_basic_blocks
- 1;
877 /* Allocate stack for back-tracking up CFG. */
878 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
881 /* Allocate bitmap to track nodes that have been visited. */
882 visited
= sbitmap_alloc (n_basic_blocks
);
884 /* None of the nodes in the CFG have been visited yet. */
885 sbitmap_zero (visited
);
887 /* Push the first edge on to the stack. */
888 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
896 /* Look at the edge on the top of the stack. */
901 /* Check if the edge destination has been visited yet. */
902 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
904 /* Mark that we have visited the destination. */
905 SET_BIT (visited
, dest
->index
);
908 dfs_order
[dfsnum
] = dest
->index
;
913 /* Since the DEST node has been visited for the first
914 time, check its successors. */
915 stack
[sp
++] = dest
->succ
;
917 /* There are no successors for the DEST node so assign
918 its reverse completion number. */
919 rc_order
[rcnum
--] = dest
->index
;
923 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
925 /* There are no more successors for the SRC node
926 so assign its reverse completion number. */
927 rc_order
[rcnum
--] = src
->index
;
930 stack
[sp
- 1] = e
->succ_next
;
937 sbitmap_free (visited
);
939 /* The number of nodes visited should not be greater than
941 if (dfsnum
> n_basic_blocks
)
944 /* There are some nodes left in the CFG that are unreachable. */
945 if (dfsnum
< n_basic_blocks
)
954 struct dfst_node
**node
;
955 struct dfst_node
*up
;
958 /* Compute a preorder transversal ordering such that a sub-tree which
959 is the source of a cross edge appears before the sub-tree which is
960 the destination of the cross edge. This allows for easy detection
961 of all the entry blocks for a loop.
963 The ordering is compute by:
965 1) Generating a depth first spanning tree.
967 2) Walking the resulting tree from right to left. */
970 flow_preorder_transversal_compute (pot_order
)
979 struct dfst_node
*node
;
980 struct dfst_node
*dfst
;
982 /* Allocate stack for back-tracking up CFG. */
983 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
986 /* Allocate the tree. */
987 dfst
= (struct dfst_node
*) xcalloc (n_basic_blocks
,
988 sizeof (struct dfst_node
));
990 for (i
= 0; i
< n_basic_blocks
; i
++)
993 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
998 ? (struct dfst_node
**) xcalloc (max_successors
,
999 sizeof (struct dfst_node
*))
1003 /* Allocate bitmap to track nodes that have been visited. */
1004 visited
= sbitmap_alloc (n_basic_blocks
);
1006 /* None of the nodes in the CFG have been visited yet. */
1007 sbitmap_zero (visited
);
1009 /* Push the first edge on to the stack. */
1010 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
1017 /* Look at the edge on the top of the stack. */
1022 /* Check if the edge destination has been visited yet. */
1023 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
1025 /* Mark that we have visited the destination. */
1026 SET_BIT (visited
, dest
->index
);
1028 /* Add the destination to the preorder tree. */
1029 if (src
!= ENTRY_BLOCK_PTR
)
1031 dfst
[src
->index
].node
[dfst
[src
->index
].nnodes
++]
1032 = &dfst
[dest
->index
];
1033 dfst
[dest
->index
].up
= &dfst
[src
->index
];
1037 /* Since the DEST node has been visited for the first
1038 time, check its successors. */
1039 stack
[sp
++] = dest
->succ
;
1042 else if (e
->succ_next
)
1043 stack
[sp
- 1] = e
->succ_next
;
1049 sbitmap_free (visited
);
1051 /* Record the preorder transversal order by
1052 walking the tree from right to left. */
1062 node
= node
->node
[--node
->nnodes
];
1063 pot_order
[i
++] = node
- dfst
;
1069 /* Free the tree. */
1071 for (i
= 0; i
< n_basic_blocks
; i
++)
1073 free (dfst
[i
].node
);
1078 /* Compute the depth first search order on the _reverse_ graph and
1079 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1080 Returns the number of nodes visited.
1082 The computation is split into three pieces:
1084 flow_dfs_compute_reverse_init () creates the necessary data
1087 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1088 structures. The block will start the search.
1090 flow_dfs_compute_reverse_execute () continues (or starts) the
1091 search using the block on the top of the stack, stopping when the
1094 flow_dfs_compute_reverse_finish () destroys the necessary data
1097 Thus, the user will probably call ..._init(), call ..._add_bb() to
1098 add a beginning basic block to the stack, call ..._execute(),
1099 possibly add another bb to the stack and again call ..._execute(),
1100 ..., and finally call _finish(). */
1102 /* Initialize the data structures used for depth-first search on the
1103 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1104 added to the basic block stack. DATA is the current depth-first
1105 search context. If INITIALIZE_STACK is non-zero, there is an
1106 element on the stack. */
1109 flow_dfs_compute_reverse_init (data
)
1110 depth_first_search_ds data
;
1112 /* Allocate stack for back-tracking up CFG. */
1113 data
->stack
= (basic_block
*) xmalloc ((n_basic_blocks
- (INVALID_BLOCK
+ 1))
1114 * sizeof (basic_block
));
1117 /* Allocate bitmap to track nodes that have been visited. */
1118 data
->visited_blocks
= sbitmap_alloc (n_basic_blocks
- (INVALID_BLOCK
+ 1));
1120 /* None of the nodes in the CFG have been visited yet. */
1121 sbitmap_zero (data
->visited_blocks
);
1126 /* Add the specified basic block to the top of the dfs data
1127 structures. When the search continues, it will start at the
1131 flow_dfs_compute_reverse_add_bb (data
, bb
)
1132 depth_first_search_ds data
;
1135 data
->stack
[data
->sp
++] = bb
;
1136 SET_BIT (data
->visited_blocks
, bb
->index
- (INVALID_BLOCK
+ 1));
1139 /* Continue the depth-first search through the reverse graph starting with the
1140 block at the stack's top and ending when the stack is empty. Visited nodes
1141 are marked. Returns an unvisited basic block, or NULL if there is none
1145 flow_dfs_compute_reverse_execute (data
)
1146 depth_first_search_ds data
;
1152 while (data
->sp
> 0)
1154 bb
= data
->stack
[--data
->sp
];
1156 /* Perform depth-first search on adjacent vertices. */
1157 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1158 if (!TEST_BIT (data
->visited_blocks
,
1159 e
->src
->index
- (INVALID_BLOCK
+ 1)))
1160 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
1163 /* Determine if there are unvisited basic blocks. */
1164 for (i
= n_basic_blocks
- (INVALID_BLOCK
+ 1); --i
>= 0; )
1165 if (!TEST_BIT (data
->visited_blocks
, i
))
1166 return BASIC_BLOCK (i
+ (INVALID_BLOCK
+ 1));
1171 /* Destroy the data structures needed for depth-first search on the
1175 flow_dfs_compute_reverse_finish (data
)
1176 depth_first_search_ds data
;
1179 sbitmap_free (data
->visited_blocks
);