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, 2003, 2004 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. */
25 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "insn-config.h"
35 /* Store the data structures necessary for depth-first search. */
36 struct depth_first_search_dsS
{
37 /* stack for backtracking during the algorithm */
40 /* number of edges in the stack. That is, positions 0, ..., sp-1
44 /* record of basic blocks already seen by depth-first search */
45 sbitmap visited_blocks
;
47 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
49 static void flow_dfs_compute_reverse_init (depth_first_search_ds
);
50 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds
,
52 static basic_block
flow_dfs_compute_reverse_execute (depth_first_search_ds
);
53 static void flow_dfs_compute_reverse_finish (depth_first_search_ds
);
54 static void remove_fake_successors (basic_block
);
55 static bool flow_active_insn_p (rtx
);
57 /* Like active_insn_p, except keep the return value clobber around
61 flow_active_insn_p (rtx insn
)
63 if (active_insn_p (insn
))
66 /* A clobber of the function return value exists for buggy
67 programs that fail to return a value. Its effect is to
68 keep the return value from being live across the entire
69 function. If we allow it to be skipped, we introduce the
70 possibility for register livetime aborts. */
71 if (GET_CODE (PATTERN (insn
)) == CLOBBER
72 && GET_CODE (XEXP (PATTERN (insn
), 0)) == REG
73 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn
), 0)))
79 /* Return true if the block has no effect and only forwards control flow to
80 its single destination. */
83 forwarder_block_p (basic_block bb
)
87 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
88 || !bb
->succ
|| bb
->succ
->succ_next
)
91 for (insn
= BB_HEAD (bb
); insn
!= BB_END (bb
); insn
= NEXT_INSN (insn
))
92 if (INSN_P (insn
) && flow_active_insn_p (insn
))
95 return (!INSN_P (insn
)
96 || (GET_CODE (insn
) == JUMP_INSN
&& simplejump_p (insn
))
97 || !flow_active_insn_p (insn
));
100 /* Return nonzero if we can reach target from src by falling through. */
103 can_fallthru (basic_block src
, basic_block target
)
105 rtx insn
= BB_END (src
);
106 rtx insn2
= target
== EXIT_BLOCK_PTR
? NULL
: BB_HEAD (target
);
108 if (src
->next_bb
!= target
)
111 if (insn2
&& !active_insn_p (insn2
))
112 insn2
= next_active_insn (insn2
);
114 /* ??? Later we may add code to move jump tables offline. */
115 return next_active_insn (insn
) == insn2
;
118 /* Mark the back edges in DFS traversal.
119 Return nonzero if a loop (natural or otherwise) is present.
120 Inspired by Depth_First_Search_PP described in:
122 Advanced Compiler Design and Implementation
124 Morgan Kaufmann, 1997
126 and heavily borrowed from flow_depth_first_order_compute. */
129 mark_dfs_back_edges (void)
140 /* Allocate the preorder and postorder number arrays. */
141 pre
= xcalloc (last_basic_block
, sizeof (int));
142 post
= xcalloc (last_basic_block
, sizeof (int));
144 /* Allocate stack for back-tracking up CFG. */
145 stack
= xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
148 /* Allocate bitmap to track nodes that have been visited. */
149 visited
= sbitmap_alloc (last_basic_block
);
151 /* None of the nodes in the CFG have been visited yet. */
152 sbitmap_zero (visited
);
154 /* Push the first edge on to the stack. */
155 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
163 /* Look at the edge on the top of the stack. */
167 e
->flags
&= ~EDGE_DFS_BACK
;
169 /* Check if the edge destination has been visited yet. */
170 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
172 /* Mark that we have visited the destination. */
173 SET_BIT (visited
, dest
->index
);
175 pre
[dest
->index
] = prenum
++;
178 /* Since the DEST node has been visited for the first
179 time, check its successors. */
180 stack
[sp
++] = dest
->succ
;
183 post
[dest
->index
] = postnum
++;
187 if (dest
!= EXIT_BLOCK_PTR
&& src
!= ENTRY_BLOCK_PTR
188 && pre
[src
->index
] >= pre
[dest
->index
]
189 && post
[dest
->index
] == 0)
190 e
->flags
|= EDGE_DFS_BACK
, found
= true;
192 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
193 post
[src
->index
] = postnum
++;
196 stack
[sp
- 1] = e
->succ_next
;
205 sbitmap_free (visited
);
210 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
213 set_edge_can_fallthru_flag (void)
221 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
223 e
->flags
&= ~EDGE_CAN_FALLTHRU
;
225 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
226 if (e
->flags
& EDGE_FALLTHRU
)
227 e
->flags
|= EDGE_CAN_FALLTHRU
;
230 /* If the BB ends with an invertible condjump all (2) edges are
231 CAN_FALLTHRU edges. */
232 if (!bb
->succ
|| !bb
->succ
->succ_next
|| bb
->succ
->succ_next
->succ_next
)
234 if (!any_condjump_p (BB_END (bb
)))
236 if (!invert_jump (BB_END (bb
), JUMP_LABEL (BB_END (bb
)), 0))
238 invert_jump (BB_END (bb
), JUMP_LABEL (BB_END (bb
)), 0);
239 bb
->succ
->flags
|= EDGE_CAN_FALLTHRU
;
240 bb
->succ
->succ_next
->flags
|= EDGE_CAN_FALLTHRU
;
244 /* Find unreachable blocks. An unreachable block will have 0 in
245 the reachable bit in block->flags. A nonzero value indicates the
246 block is reachable. */
249 find_unreachable_blocks (void)
252 basic_block
*tos
, *worklist
, bb
;
254 tos
= worklist
= xmalloc (sizeof (basic_block
) * n_basic_blocks
);
256 /* Clear all the reachability flags. */
259 bb
->flags
&= ~BB_REACHABLE
;
261 /* Add our starting points to the worklist. Almost always there will
262 be only one. It isn't inconceivable that we might one day directly
263 support Fortran alternate entry points. */
265 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
269 /* Mark the block reachable. */
270 e
->dest
->flags
|= BB_REACHABLE
;
273 /* Iterate: find everything reachable from what we've already seen. */
275 while (tos
!= worklist
)
277 basic_block b
= *--tos
;
279 for (e
= b
->succ
; e
; e
= e
->succ_next
)
280 if (!(e
->dest
->flags
& BB_REACHABLE
))
283 e
->dest
->flags
|= BB_REACHABLE
;
290 /* Functions to access an edge list with a vector representation.
291 Enough data is kept such that given an index number, the
292 pred and succ that edge represents can be determined, or
293 given a pred and a succ, its index number can be returned.
294 This allows algorithms which consume a lot of memory to
295 represent the normally full matrix of edge (pred,succ) with a
296 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
297 wasted space in the client code due to sparse flow graphs. */
299 /* This functions initializes the edge list. Basically the entire
300 flowgraph is processed, and all edges are assigned a number,
301 and the data structure is filled in. */
304 create_edge_list (void)
306 struct edge_list
*elist
;
312 block_count
= n_basic_blocks
+ 2; /* Include the entry and exit blocks. */
316 /* Determine the number of edges in the flow graph by counting successor
317 edges on each basic block. */
318 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
320 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
324 elist
= xmalloc (sizeof (struct edge_list
));
325 elist
->num_blocks
= block_count
;
326 elist
->num_edges
= num_edges
;
327 elist
->index_to_edge
= xmalloc (sizeof (edge
) * num_edges
);
331 /* Follow successors of blocks, and register these edges. */
332 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
333 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
334 elist
->index_to_edge
[num_edges
++] = e
;
339 /* This function free's memory associated with an edge list. */
342 free_edge_list (struct edge_list
*elist
)
346 free (elist
->index_to_edge
);
351 /* This function provides debug output showing an edge list. */
354 print_edge_list (FILE *f
, struct edge_list
*elist
)
358 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
359 elist
->num_blocks
- 2, elist
->num_edges
);
361 for (x
= 0; x
< elist
->num_edges
; x
++)
363 fprintf (f
, " %-4d - edge(", x
);
364 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR
)
365 fprintf (f
, "entry,");
367 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
369 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR
)
370 fprintf (f
, "exit)\n");
372 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
376 /* This function provides an internal consistency check of an edge list,
377 verifying that all edges are present, and that there are no
381 verify_edge_list (FILE *f
, struct edge_list
*elist
)
383 int pred
, succ
, index
;
385 basic_block bb
, p
, s
;
387 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
389 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
391 pred
= e
->src
->index
;
392 succ
= e
->dest
->index
;
393 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
394 if (index
== EDGE_INDEX_NO_EDGE
)
396 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
400 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
401 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
402 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
403 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
404 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
405 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
409 /* We've verified that all the edges are in the list, now lets make sure
410 there are no spurious edges in the list. */
412 FOR_BB_BETWEEN (p
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
413 FOR_BB_BETWEEN (s
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
417 for (e
= p
->succ
; e
; e
= e
->succ_next
)
424 for (e
= s
->pred
; e
; e
= e
->pred_next
)
431 if (EDGE_INDEX (elist
, p
, s
)
432 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
433 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
435 if (EDGE_INDEX (elist
, p
, s
)
436 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
437 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
438 p
->index
, s
->index
, EDGE_INDEX (elist
, p
, s
));
442 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
443 If no such edge exists, return NULL. */
446 find_edge (basic_block pred
, basic_block succ
)
450 for (e
= pred
->succ
; e
; e
= e
->succ_next
)
457 /* This routine will determine what, if any, edge there is between
458 a specified predecessor and successor. */
461 find_edge_index (struct edge_list
*edge_list
, basic_block pred
, basic_block succ
)
465 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
466 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
467 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
470 return (EDGE_INDEX_NO_EDGE
);
473 /* Dump the list of basic blocks in the bitmap NODES. */
476 flow_nodes_print (const char *str
, const sbitmap nodes
, FILE *file
)
483 fprintf (file
, "%s { ", str
);
484 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, node
, {fprintf (file
, "%d ", node
);});
488 /* Dump the list of edges in the array EDGE_LIST. */
491 flow_edge_list_print (const char *str
, const edge
*edge_list
, int num_edges
, FILE *file
)
498 fprintf (file
, "%s { ", str
);
499 for (i
= 0; i
< num_edges
; i
++)
500 fprintf (file
, "%d->%d ", edge_list
[i
]->src
->index
,
501 edge_list
[i
]->dest
->index
);
507 /* This routine will remove any fake successor edges for a basic block.
508 When the edge is removed, it is also removed from whatever predecessor
512 remove_fake_successors (basic_block bb
)
516 for (e
= bb
->succ
; e
;)
521 if ((tmp
->flags
& EDGE_FAKE
) == EDGE_FAKE
)
526 /* This routine will remove all fake edges from the flow graph. If
527 we remove all fake successors, it will automatically remove all
528 fake predecessors. */
531 remove_fake_edges (void)
535 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
536 remove_fake_successors (bb
);
539 /* This function will add a fake edge between any block which has no
540 successors, and the exit block. Some data flow equations require these
544 add_noreturn_fake_exit_edges (void)
549 if (bb
->succ
== NULL
)
550 make_single_succ_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
553 /* This function adds a fake edge between any infinite loops to the
554 exit block. Some optimizations require a path from each node to
557 See also Morgan, Figure 3.10, pp. 82-83.
559 The current implementation is ugly, not attempting to minimize the
560 number of inserted fake edges. To reduce the number of fake edges
561 to insert, add fake edges from _innermost_ loops containing only
562 nodes not reachable from the exit block. */
565 connect_infinite_loops_to_exit (void)
567 basic_block unvisited_block
;
568 struct depth_first_search_dsS dfs_ds
;
570 /* Perform depth-first search in the reverse graph to find nodes
571 reachable from the exit block. */
572 flow_dfs_compute_reverse_init (&dfs_ds
);
573 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR
);
575 /* Repeatedly add fake edges, updating the unreachable nodes. */
578 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
);
579 if (!unvisited_block
)
582 make_edge (unvisited_block
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
583 flow_dfs_compute_reverse_add_bb (&dfs_ds
, unvisited_block
);
586 flow_dfs_compute_reverse_finish (&dfs_ds
);
590 /* Compute reverse top sort order. */
593 flow_reverse_top_sort_order_compute (int *rts_order
)
600 /* Allocate stack for back-tracking up CFG. */
601 stack
= xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
604 /* Allocate bitmap to track nodes that have been visited. */
605 visited
= sbitmap_alloc (last_basic_block
);
607 /* None of the nodes in the CFG have been visited yet. */
608 sbitmap_zero (visited
);
610 /* Push the first edge on to the stack. */
611 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
619 /* Look at the edge on the top of the stack. */
624 /* Check if the edge destination has been visited yet. */
625 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
627 /* Mark that we have visited the destination. */
628 SET_BIT (visited
, dest
->index
);
631 /* Since the DEST node has been visited for the first
632 time, check its successors. */
633 stack
[sp
++] = dest
->succ
;
635 rts_order
[postnum
++] = dest
->index
;
639 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
640 rts_order
[postnum
++] = src
->index
;
643 stack
[sp
- 1] = e
->succ_next
;
650 sbitmap_free (visited
);
653 /* Compute the depth first search order and store in the array
654 DFS_ORDER if nonzero, marking the nodes visited in VISITED. If
655 RC_ORDER is nonzero, return the reverse completion number for each
656 node. Returns the number of nodes visited. A depth first search
657 tries to get as far away from the starting point as quickly as
661 flow_depth_first_order_compute (int *dfs_order
, int *rc_order
)
666 int rcnum
= n_basic_blocks
- 1;
669 /* Allocate stack for back-tracking up CFG. */
670 stack
= xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
673 /* Allocate bitmap to track nodes that have been visited. */
674 visited
= sbitmap_alloc (last_basic_block
);
676 /* None of the nodes in the CFG have been visited yet. */
677 sbitmap_zero (visited
);
679 /* Push the first edge on to the stack. */
680 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
688 /* Look at the edge on the top of the stack. */
693 /* Check if the edge destination has been visited yet. */
694 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
696 /* Mark that we have visited the destination. */
697 SET_BIT (visited
, dest
->index
);
700 dfs_order
[dfsnum
] = dest
->index
;
705 /* Since the DEST node has been visited for the first
706 time, check its successors. */
707 stack
[sp
++] = dest
->succ
;
709 /* There are no successors for the DEST node so assign
710 its reverse completion number. */
711 rc_order
[rcnum
--] = dest
->index
;
715 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
717 /* There are no more successors for the SRC node
718 so assign its reverse completion number. */
719 rc_order
[rcnum
--] = src
->index
;
722 stack
[sp
- 1] = e
->succ_next
;
729 sbitmap_free (visited
);
731 /* The number of nodes visited should not be greater than
733 if (dfsnum
> n_basic_blocks
)
736 /* There are some nodes left in the CFG that are unreachable. */
737 if (dfsnum
< n_basic_blocks
)
746 struct dfst_node
**node
;
747 struct dfst_node
*up
;
750 /* Compute a preorder transversal ordering such that a sub-tree which
751 is the source of a cross edge appears before the sub-tree which is
752 the destination of the cross edge. This allows for easy detection
753 of all the entry blocks for a loop.
755 The ordering is compute by:
757 1) Generating a depth first spanning tree.
759 2) Walking the resulting tree from right to left. */
762 flow_preorder_transversal_compute (int *pot_order
)
770 struct dfst_node
*node
;
771 struct dfst_node
*dfst
;
774 /* Allocate stack for back-tracking up CFG. */
775 stack
= xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
778 /* Allocate the tree. */
779 dfst
= xcalloc (last_basic_block
, sizeof (struct dfst_node
));
784 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
789 ? xcalloc (max_successors
, sizeof (struct dfst_node
*)) : NULL
);
792 /* Allocate bitmap to track nodes that have been visited. */
793 visited
= sbitmap_alloc (last_basic_block
);
795 /* None of the nodes in the CFG have been visited yet. */
796 sbitmap_zero (visited
);
798 /* Push the first edge on to the stack. */
799 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
806 /* Look at the edge on the top of the stack. */
811 /* Check if the edge destination has been visited yet. */
812 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
814 /* Mark that we have visited the destination. */
815 SET_BIT (visited
, dest
->index
);
817 /* Add the destination to the preorder tree. */
818 if (src
!= ENTRY_BLOCK_PTR
)
820 dfst
[src
->index
].node
[dfst
[src
->index
].nnodes
++]
821 = &dfst
[dest
->index
];
822 dfst
[dest
->index
].up
= &dfst
[src
->index
];
826 /* Since the DEST node has been visited for the first
827 time, check its successors. */
828 stack
[sp
++] = dest
->succ
;
831 else if (e
->succ_next
)
832 stack
[sp
- 1] = e
->succ_next
;
838 sbitmap_free (visited
);
840 /* Record the preorder transversal order by
841 walking the tree from right to left. */
844 node
= &dfst
[ENTRY_BLOCK_PTR
->next_bb
->index
];
851 node
= node
->node
[--node
->nnodes
];
852 pot_order
[i
++] = node
- dfst
;
860 for (i
= 0; i
< last_basic_block
; i
++)
867 /* Compute the depth first search order on the _reverse_ graph and
868 store in the array DFS_ORDER, marking the nodes visited in VISITED.
869 Returns the number of nodes visited.
871 The computation is split into three pieces:
873 flow_dfs_compute_reverse_init () creates the necessary data
876 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
877 structures. The block will start the search.
879 flow_dfs_compute_reverse_execute () continues (or starts) the
880 search using the block on the top of the stack, stopping when the
883 flow_dfs_compute_reverse_finish () destroys the necessary data
886 Thus, the user will probably call ..._init(), call ..._add_bb() to
887 add a beginning basic block to the stack, call ..._execute(),
888 possibly add another bb to the stack and again call ..._execute(),
889 ..., and finally call _finish(). */
891 /* Initialize the data structures used for depth-first search on the
892 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
893 added to the basic block stack. DATA is the current depth-first
894 search context. If INITIALIZE_STACK is nonzero, there is an
895 element on the stack. */
898 flow_dfs_compute_reverse_init (depth_first_search_ds data
)
900 /* Allocate stack for back-tracking up CFG. */
901 data
->stack
= xmalloc ((n_basic_blocks
- (INVALID_BLOCK
+ 1))
902 * sizeof (basic_block
));
905 /* Allocate bitmap to track nodes that have been visited. */
906 data
->visited_blocks
= sbitmap_alloc (last_basic_block
- (INVALID_BLOCK
+ 1));
908 /* None of the nodes in the CFG have been visited yet. */
909 sbitmap_zero (data
->visited_blocks
);
914 /* Add the specified basic block to the top of the dfs data
915 structures. When the search continues, it will start at the
919 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data
, basic_block bb
)
921 data
->stack
[data
->sp
++] = bb
;
922 SET_BIT (data
->visited_blocks
, bb
->index
- (INVALID_BLOCK
+ 1));
925 /* Continue the depth-first search through the reverse graph starting with the
926 block at the stack's top and ending when the stack is empty. Visited nodes
927 are marked. Returns an unvisited basic block, or NULL if there is none
931 flow_dfs_compute_reverse_execute (depth_first_search_ds data
)
938 bb
= data
->stack
[--data
->sp
];
940 /* Perform depth-first search on adjacent vertices. */
941 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
942 if (!TEST_BIT (data
->visited_blocks
,
943 e
->src
->index
- (INVALID_BLOCK
+ 1)))
944 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
947 /* Determine if there are unvisited basic blocks. */
948 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR
, NULL
, prev_bb
)
949 if (!TEST_BIT (data
->visited_blocks
, bb
->index
- (INVALID_BLOCK
+ 1)))
955 /* Destroy the data structures needed for depth-first search on the
959 flow_dfs_compute_reverse_finish (depth_first_search_ds data
)
962 sbitmap_free (data
->visited_blocks
);
965 /* Performs dfs search from BB over vertices satisfying PREDICATE;
966 if REVERSE, go against direction of edges. Returns number of blocks
967 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
969 dfs_enumerate_from (basic_block bb
, int reverse
,
970 bool (*predicate
) (basic_block
, void *),
971 basic_block
*rslt
, int rslt_max
, void *data
)
973 basic_block
*st
, lbb
;
976 st
= xcalloc (rslt_max
, sizeof (basic_block
));
977 rslt
[tv
++] = st
[sp
++] = bb
;
978 bb
->flags
|= BB_VISITED
;
985 for (e
= lbb
->pred
; e
; e
= e
->pred_next
)
986 if (!(e
->src
->flags
& BB_VISITED
) && predicate (e
->src
, data
))
990 rslt
[tv
++] = st
[sp
++] = e
->src
;
991 e
->src
->flags
|= BB_VISITED
;
996 for (e
= lbb
->succ
; e
; e
= e
->succ_next
)
997 if (!(e
->dest
->flags
& BB_VISITED
) && predicate (e
->dest
, data
))
1001 rslt
[tv
++] = st
[sp
++] = e
->dest
;
1002 e
->dest
->flags
|= BB_VISITED
;
1007 for (sp
= 0; sp
< tv
; sp
++)
1008 rslt
[sp
]->flags
&= ~BB_VISITED
;