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"
33 /* Store the data structures necessary for depth-first search. */
34 struct depth_first_search_dsS
{
35 /* stack for backtracking during the algorithm */
38 /* number of edges in the stack. That is, positions 0, ..., sp-1
42 /* record of basic blocks already seen by depth-first search */
43 sbitmap visited_blocks
;
45 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
47 static void flow_dfs_compute_reverse_init
48 PARAMS ((depth_first_search_ds
));
49 static void flow_dfs_compute_reverse_add_bb
50 PARAMS ((depth_first_search_ds
, basic_block
));
51 static basic_block flow_dfs_compute_reverse_execute
52 PARAMS ((depth_first_search_ds
));
53 static void flow_dfs_compute_reverse_finish
54 PARAMS ((depth_first_search_ds
));
55 static void remove_fake_successors
PARAMS ((basic_block
));
56 static bool need_fake_edge_p
PARAMS ((rtx
));
58 /* Return true if the block has no effect and only forwards control flow to
59 its single destination. */
62 forwarder_block_p (bb
)
67 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
68 || !bb
->succ
|| bb
->succ
->succ_next
)
71 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
72 if (INSN_P (insn
) && active_insn_p (insn
))
75 return (!INSN_P (insn
)
76 || (GET_CODE (insn
) == JUMP_INSN
&& simplejump_p (insn
))
77 || !active_insn_p (insn
));
80 /* Return nonzero if we can reach target from src by falling through. */
83 can_fallthru (src
, target
)
84 basic_block src
, target
;
87 rtx insn2
= target
->head
;
89 if (src
->next_bb
!= target
)
92 if (!active_insn_p (insn2
))
93 insn2
= next_active_insn (insn2
);
95 /* ??? Later we may add code to move jump tables offline. */
96 return next_active_insn (insn
) == insn2
;
99 /* Mark the back edges in DFS traversal.
100 Return non-zero if a loop (natural or otherwise) is present.
101 Inspired by Depth_First_Search_PP described in:
103 Advanced Compiler Design and Implementation
105 Morgan Kaufmann, 1997
107 and heavily borrowed from flow_depth_first_order_compute. */
110 mark_dfs_back_edges ()
121 /* Allocate the preorder and postorder number arrays. */
122 pre
= (int *) xcalloc (last_basic_block
, sizeof (int));
123 post
= (int *) xcalloc (last_basic_block
, sizeof (int));
125 /* Allocate stack for back-tracking up CFG. */
126 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
129 /* Allocate bitmap to track nodes that have been visited. */
130 visited
= sbitmap_alloc (last_basic_block
);
132 /* None of the nodes in the CFG have been visited yet. */
133 sbitmap_zero (visited
);
135 /* Push the first edge on to the stack. */
136 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
144 /* Look at the edge on the top of the stack. */
148 e
->flags
&= ~EDGE_DFS_BACK
;
150 /* Check if the edge destination has been visited yet. */
151 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
153 /* Mark that we have visited the destination. */
154 SET_BIT (visited
, dest
->index
);
156 pre
[dest
->index
] = prenum
++;
159 /* Since the DEST node has been visited for the first
160 time, check its successors. */
161 stack
[sp
++] = dest
->succ
;
164 post
[dest
->index
] = postnum
++;
168 if (dest
!= EXIT_BLOCK_PTR
&& src
!= ENTRY_BLOCK_PTR
169 && pre
[src
->index
] >= pre
[dest
->index
]
170 && post
[dest
->index
] == 0)
171 e
->flags
|= EDGE_DFS_BACK
, found
= true;
173 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
174 post
[src
->index
] = postnum
++;
177 stack
[sp
- 1] = e
->succ_next
;
186 sbitmap_free (visited
);
191 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
194 set_edge_can_fallthru_flag ()
202 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
203 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
204 if (e
->flags
& EDGE_FALLTHRU
)
205 e
->flags
|= EDGE_CAN_FALLTHRU
;
207 /* If the BB ends with an invertable condjump all (2) edges are
208 CAN_FALLTHRU edges. */
209 if (!bb
->succ
|| !bb
->succ
->succ_next
|| bb
->succ
->succ_next
->succ_next
)
211 if (!any_condjump_p (bb
->end
))
213 if (!invert_jump (bb
->end
, JUMP_LABEL (bb
->end
), 0))
215 invert_jump (bb
->end
, JUMP_LABEL (bb
->end
), 0);
216 bb
->succ
->flags
|= EDGE_CAN_FALLTHRU
;
217 bb
->succ
->succ_next
->flags
|= EDGE_CAN_FALLTHRU
;
221 /* Return true if we need to add fake edge to exit.
222 Helper function for the flow_call_edges_add. */
225 need_fake_edge_p (insn
)
231 if ((GET_CODE (insn
) == CALL_INSN
232 && !SIBLING_CALL_P (insn
)
233 && !find_reg_note (insn
, REG_NORETURN
, NULL
)
234 && !find_reg_note (insn
, REG_ALWAYS_RETURN
, NULL
)
235 && !CONST_OR_PURE_CALL_P (insn
)))
238 return ((GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
239 && MEM_VOLATILE_P (PATTERN (insn
)))
240 || (GET_CODE (PATTERN (insn
)) == PARALLEL
241 && asm_noperands (insn
) != -1
242 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn
), 0, 0)))
243 || GET_CODE (PATTERN (insn
)) == ASM_INPUT
);
246 /* Add fake edges to the function exit for any non constant and non noreturn
247 calls, volatile inline assembly in the bitmap of blocks specified by
248 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
251 The goal is to expose cases in which entering a basic block does not imply
252 that all subsequent instructions must be executed. */
255 flow_call_edges_add (blocks
)
259 int blocks_split
= 0;
260 int last_bb
= last_basic_block
;
261 bool check_last_block
= false;
263 if (n_basic_blocks
== 0)
267 check_last_block
= true;
269 check_last_block
= TEST_BIT (blocks
, EXIT_BLOCK_PTR
->prev_bb
->index
);
271 /* In the last basic block, before epilogue generation, there will be
272 a fallthru edge to EXIT. Special care is required if the last insn
273 of the last basic block is a call because make_edge folds duplicate
274 edges, which would result in the fallthru edge also being marked
275 fake, which would result in the fallthru edge being removed by
276 remove_fake_edges, which would result in an invalid CFG.
278 Moreover, we can't elide the outgoing fake edge, since the block
279 profiler needs to take this into account in order to solve the minimal
280 spanning tree in the case that the call doesn't return.
282 Handle this by adding a dummy instruction in a new last basic block. */
283 if (check_last_block
)
285 basic_block bb
= EXIT_BLOCK_PTR
->prev_bb
;
288 /* Back up past insns that must be kept in the same block as a call. */
289 while (insn
!= bb
->head
290 && keep_with_call_p (insn
))
291 insn
= PREV_INSN (insn
);
293 if (need_fake_edge_p (insn
))
297 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
298 if (e
->dest
== EXIT_BLOCK_PTR
)
301 insert_insn_on_edge (gen_rtx_USE (VOIDmode
, const0_rtx
), e
);
302 commit_edge_insertions ();
306 /* Now add fake edges to the function exit for any non constant
307 calls since there is no way that we can determine if they will
310 for (i
= 0; i
< last_bb
; i
++)
312 basic_block bb
= BASIC_BLOCK (i
);
319 if (blocks
&& !TEST_BIT (blocks
, i
))
322 for (insn
= bb
->end
; ; insn
= prev_insn
)
324 prev_insn
= PREV_INSN (insn
);
325 if (need_fake_edge_p (insn
))
328 rtx split_at_insn
= insn
;
330 /* Don't split the block between a call and an insn that should
331 remain in the same block as the call. */
332 if (GET_CODE (insn
) == CALL_INSN
)
333 while (split_at_insn
!= bb
->end
334 && keep_with_call_p (NEXT_INSN (split_at_insn
)))
335 split_at_insn
= NEXT_INSN (split_at_insn
);
337 /* The handling above of the final block before the epilogue
338 should be enough to verify that there is no edge to the exit
339 block in CFG already. Calling make_edge in such case would
340 cause us to mark that edge as fake and remove it later. */
342 #ifdef ENABLE_CHECKING
343 if (split_at_insn
== bb
->end
)
344 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
345 if (e
->dest
== EXIT_BLOCK_PTR
)
349 /* Note that the following may create a new basic block
350 and renumber the existing basic blocks. */
351 if (split_at_insn
!= bb
->end
)
353 e
= split_block (bb
, split_at_insn
);
358 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
361 if (insn
== bb
->head
)
372 /* Find unreachable blocks. An unreachable block will have 0 in
373 the reachable bit in block->flags. A non-zero value indicates the
374 block is reachable. */
377 find_unreachable_blocks ()
380 basic_block
*tos
, *worklist
, bb
;
383 (basic_block
*) xmalloc (sizeof (basic_block
) * n_basic_blocks
);
385 /* Clear all the reachability flags. */
388 bb
->flags
&= ~BB_REACHABLE
;
390 /* Add our starting points to the worklist. Almost always there will
391 be only one. It isn't inconceivable that we might one day directly
392 support Fortran alternate entry points. */
394 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
398 /* Mark the block reachable. */
399 e
->dest
->flags
|= BB_REACHABLE
;
402 /* Iterate: find everything reachable from what we've already seen. */
404 while (tos
!= worklist
)
406 basic_block b
= *--tos
;
408 for (e
= b
->succ
; e
; e
= e
->succ_next
)
409 if (!(e
->dest
->flags
& BB_REACHABLE
))
412 e
->dest
->flags
|= BB_REACHABLE
;
419 /* Functions to access an edge list with a vector representation.
420 Enough data is kept such that given an index number, the
421 pred and succ that edge represents can be determined, or
422 given a pred and a succ, its index number can be returned.
423 This allows algorithms which consume a lot of memory to
424 represent the normally full matrix of edge (pred,succ) with a
425 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
426 wasted space in the client code due to sparse flow graphs. */
428 /* This functions initializes the edge list. Basically the entire
429 flowgraph is processed, and all edges are assigned a number,
430 and the data structure is filled in. */
435 struct edge_list
*elist
;
441 block_count
= n_basic_blocks
+ 2; /* Include the entry and exit blocks. */
445 /* Determine the number of edges in the flow graph by counting successor
446 edges on each basic block. */
447 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
449 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
453 elist
= (struct edge_list
*) xmalloc (sizeof (struct edge_list
));
454 elist
->num_blocks
= block_count
;
455 elist
->num_edges
= num_edges
;
456 elist
->index_to_edge
= (edge
*) xmalloc (sizeof (edge
) * num_edges
);
460 /* Follow successors of blocks, and register these edges. */
461 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
462 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
463 elist
->index_to_edge
[num_edges
++] = e
;
468 /* This function free's memory associated with an edge list. */
471 free_edge_list (elist
)
472 struct edge_list
*elist
;
476 free (elist
->index_to_edge
);
481 /* This function provides debug output showing an edge list. */
484 print_edge_list (f
, elist
)
486 struct edge_list
*elist
;
490 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
491 elist
->num_blocks
- 2, elist
->num_edges
);
493 for (x
= 0; x
< elist
->num_edges
; x
++)
495 fprintf (f
, " %-4d - edge(", x
);
496 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR
)
497 fprintf (f
, "entry,");
499 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
501 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR
)
502 fprintf (f
, "exit)\n");
504 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
508 /* This function provides an internal consistency check of an edge list,
509 verifying that all edges are present, and that there are no
513 verify_edge_list (f
, elist
)
515 struct edge_list
*elist
;
517 int pred
, succ
, index
;
519 basic_block bb
, p
, s
;
521 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
523 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
525 pred
= e
->src
->index
;
526 succ
= e
->dest
->index
;
527 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
528 if (index
== EDGE_INDEX_NO_EDGE
)
530 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
534 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
535 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
536 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
537 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
538 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
539 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
543 /* We've verified that all the edges are in the list, now lets make sure
544 there are no spurious edges in the list. */
546 FOR_BB_BETWEEN (p
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
547 FOR_BB_BETWEEN (s
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
551 for (e
= p
->succ
; e
; e
= e
->succ_next
)
558 for (e
= s
->pred
; e
; e
= e
->pred_next
)
565 if (EDGE_INDEX (elist
, p
, s
)
566 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
567 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
569 if (EDGE_INDEX (elist
, p
, s
)
570 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
571 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
572 p
->index
, s
->index
, EDGE_INDEX (elist
, p
, s
));
576 /* This routine will determine what, if any, edge there is between
577 a specified predecessor and successor. */
580 find_edge_index (edge_list
, pred
, succ
)
581 struct edge_list
*edge_list
;
582 basic_block pred
, succ
;
586 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
587 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
588 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
591 return (EDGE_INDEX_NO_EDGE
);
594 /* Dump the list of basic blocks in the bitmap NODES. */
597 flow_nodes_print (str
, nodes
, file
)
607 fprintf (file
, "%s { ", str
);
608 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, node
, {fprintf (file
, "%d ", node
);});
612 /* Dump the list of edges in the array EDGE_LIST. */
615 flow_edge_list_print (str
, edge_list
, num_edges
, file
)
617 const edge
*edge_list
;
626 fprintf (file
, "%s { ", str
);
627 for (i
= 0; i
< num_edges
; i
++)
628 fprintf (file
, "%d->%d ", edge_list
[i
]->src
->index
,
629 edge_list
[i
]->dest
->index
);
635 /* This routine will remove any fake successor edges for a basic block.
636 When the edge is removed, it is also removed from whatever predecessor
640 remove_fake_successors (bb
)
645 for (e
= bb
->succ
; e
;)
650 if ((tmp
->flags
& EDGE_FAKE
) == EDGE_FAKE
)
655 /* This routine will remove all fake edges from the flow graph. If
656 we remove all fake successors, it will automatically remove all
657 fake predecessors. */
664 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
665 remove_fake_successors (bb
);
668 /* This function will add a fake edge between any block which has no
669 successors, and the exit block. Some data flow equations require these
673 add_noreturn_fake_exit_edges ()
678 if (bb
->succ
== NULL
)
679 make_single_succ_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
682 /* This function adds a fake edge between any infinite loops to the
683 exit block. Some optimizations require a path from each node to
686 See also Morgan, Figure 3.10, pp. 82-83.
688 The current implementation is ugly, not attempting to minimize the
689 number of inserted fake edges. To reduce the number of fake edges
690 to insert, add fake edges from _innermost_ loops containing only
691 nodes not reachable from the exit block. */
694 connect_infinite_loops_to_exit ()
696 basic_block unvisited_block
;
697 struct depth_first_search_dsS dfs_ds
;
699 /* Perform depth-first search in the reverse graph to find nodes
700 reachable from the exit block. */
701 flow_dfs_compute_reverse_init (&dfs_ds
);
702 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR
);
704 /* Repeatedly add fake edges, updating the unreachable nodes. */
707 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
);
708 if (!unvisited_block
)
711 make_edge (unvisited_block
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
712 flow_dfs_compute_reverse_add_bb (&dfs_ds
, unvisited_block
);
715 flow_dfs_compute_reverse_finish (&dfs_ds
);
719 /* Compute reverse top sort order */
722 flow_reverse_top_sort_order_compute (rts_order
)
730 /* Allocate stack for back-tracking up CFG. */
731 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
734 /* Allocate bitmap to track nodes that have been visited. */
735 visited
= sbitmap_alloc (last_basic_block
);
737 /* None of the nodes in the CFG have been visited yet. */
738 sbitmap_zero (visited
);
740 /* Push the first edge on to the stack. */
741 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
749 /* Look at the edge on the top of the stack. */
754 /* Check if the edge destination has been visited yet. */
755 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
757 /* Mark that we have visited the destination. */
758 SET_BIT (visited
, dest
->index
);
761 /* Since the DEST node has been visited for the first
762 time, check its successors. */
763 stack
[sp
++] = dest
->succ
;
765 rts_order
[postnum
++] = dest
->index
;
769 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
770 rts_order
[postnum
++] = src
->index
;
773 stack
[sp
- 1] = e
->succ_next
;
780 sbitmap_free (visited
);
783 /* Compute the depth first search order and store in the array
784 DFS_ORDER if non-zero, marking the nodes visited in VISITED. If
785 RC_ORDER is non-zero, return the reverse completion number for each
786 node. Returns the number of nodes visited. A depth first search
787 tries to get as far away from the starting point as quickly as
791 flow_depth_first_order_compute (dfs_order
, rc_order
)
798 int rcnum
= n_basic_blocks
- 1;
801 /* Allocate stack for back-tracking up CFG. */
802 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
805 /* Allocate bitmap to track nodes that have been visited. */
806 visited
= sbitmap_alloc (last_basic_block
);
808 /* None of the nodes in the CFG have been visited yet. */
809 sbitmap_zero (visited
);
811 /* Push the first edge on to the stack. */
812 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
820 /* Look at the edge on the top of the stack. */
825 /* Check if the edge destination has been visited yet. */
826 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
828 /* Mark that we have visited the destination. */
829 SET_BIT (visited
, dest
->index
);
832 dfs_order
[dfsnum
] = dest
->index
;
837 /* Since the DEST node has been visited for the first
838 time, check its successors. */
839 stack
[sp
++] = dest
->succ
;
841 /* There are no successors for the DEST node so assign
842 its reverse completion number. */
843 rc_order
[rcnum
--] = dest
->index
;
847 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
849 /* There are no more successors for the SRC node
850 so assign its reverse completion number. */
851 rc_order
[rcnum
--] = src
->index
;
854 stack
[sp
- 1] = e
->succ_next
;
861 sbitmap_free (visited
);
863 /* The number of nodes visited should not be greater than
865 if (dfsnum
> n_basic_blocks
)
868 /* There are some nodes left in the CFG that are unreachable. */
869 if (dfsnum
< n_basic_blocks
)
878 struct dfst_node
**node
;
879 struct dfst_node
*up
;
882 /* Compute a preorder transversal ordering such that a sub-tree which
883 is the source of a cross edge appears before the sub-tree which is
884 the destination of the cross edge. This allows for easy detection
885 of all the entry blocks for a loop.
887 The ordering is compute by:
889 1) Generating a depth first spanning tree.
891 2) Walking the resulting tree from right to left. */
894 flow_preorder_transversal_compute (pot_order
)
903 struct dfst_node
*node
;
904 struct dfst_node
*dfst
;
907 /* Allocate stack for back-tracking up CFG. */
908 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
911 /* Allocate the tree. */
912 dfst
= (struct dfst_node
*) xcalloc (last_basic_block
,
913 sizeof (struct dfst_node
));
918 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
923 ? (struct dfst_node
**) xcalloc (max_successors
,
924 sizeof (struct dfst_node
*))
928 /* Allocate bitmap to track nodes that have been visited. */
929 visited
= sbitmap_alloc (last_basic_block
);
931 /* None of the nodes in the CFG have been visited yet. */
932 sbitmap_zero (visited
);
934 /* Push the first edge on to the stack. */
935 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
942 /* Look at the edge on the top of the stack. */
947 /* Check if the edge destination has been visited yet. */
948 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
950 /* Mark that we have visited the destination. */
951 SET_BIT (visited
, dest
->index
);
953 /* Add the destination to the preorder tree. */
954 if (src
!= ENTRY_BLOCK_PTR
)
956 dfst
[src
->index
].node
[dfst
[src
->index
].nnodes
++]
957 = &dfst
[dest
->index
];
958 dfst
[dest
->index
].up
= &dfst
[src
->index
];
962 /* Since the DEST node has been visited for the first
963 time, check its successors. */
964 stack
[sp
++] = dest
->succ
;
967 else if (e
->succ_next
)
968 stack
[sp
- 1] = e
->succ_next
;
974 sbitmap_free (visited
);
976 /* Record the preorder transversal order by
977 walking the tree from right to left. */
980 node
= &dfst
[ENTRY_BLOCK_PTR
->next_bb
->index
];
987 node
= node
->node
[--node
->nnodes
];
988 pot_order
[i
++] = node
- dfst
;
996 for (i
= 0; i
< last_basic_block
; i
++)
1003 /* Compute the depth first search order on the _reverse_ graph and
1004 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1005 Returns the number of nodes visited.
1007 The computation is split into three pieces:
1009 flow_dfs_compute_reverse_init () creates the necessary data
1012 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1013 structures. The block will start the search.
1015 flow_dfs_compute_reverse_execute () continues (or starts) the
1016 search using the block on the top of the stack, stopping when the
1019 flow_dfs_compute_reverse_finish () destroys the necessary data
1022 Thus, the user will probably call ..._init(), call ..._add_bb() to
1023 add a beginning basic block to the stack, call ..._execute(),
1024 possibly add another bb to the stack and again call ..._execute(),
1025 ..., and finally call _finish(). */
1027 /* Initialize the data structures used for depth-first search on the
1028 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1029 added to the basic block stack. DATA is the current depth-first
1030 search context. If INITIALIZE_STACK is non-zero, there is an
1031 element on the stack. */
1034 flow_dfs_compute_reverse_init (data
)
1035 depth_first_search_ds data
;
1037 /* Allocate stack for back-tracking up CFG. */
1038 data
->stack
= (basic_block
*) xmalloc ((n_basic_blocks
- (INVALID_BLOCK
+ 1))
1039 * sizeof (basic_block
));
1042 /* Allocate bitmap to track nodes that have been visited. */
1043 data
->visited_blocks
= sbitmap_alloc (last_basic_block
- (INVALID_BLOCK
+ 1));
1045 /* None of the nodes in the CFG have been visited yet. */
1046 sbitmap_zero (data
->visited_blocks
);
1051 /* Add the specified basic block to the top of the dfs data
1052 structures. When the search continues, it will start at the
1056 flow_dfs_compute_reverse_add_bb (data
, bb
)
1057 depth_first_search_ds data
;
1060 data
->stack
[data
->sp
++] = bb
;
1061 SET_BIT (data
->visited_blocks
, bb
->index
- (INVALID_BLOCK
+ 1));
1064 /* Continue the depth-first search through the reverse graph starting with the
1065 block at the stack's top and ending when the stack is empty. Visited nodes
1066 are marked. Returns an unvisited basic block, or NULL if there is none
1070 flow_dfs_compute_reverse_execute (data
)
1071 depth_first_search_ds data
;
1076 while (data
->sp
> 0)
1078 bb
= data
->stack
[--data
->sp
];
1080 /* Perform depth-first search on adjacent vertices. */
1081 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1082 if (!TEST_BIT (data
->visited_blocks
,
1083 e
->src
->index
- (INVALID_BLOCK
+ 1)))
1084 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
1087 /* Determine if there are unvisited basic blocks. */
1088 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR
, NULL
, prev_bb
)
1089 if (!TEST_BIT (data
->visited_blocks
, bb
->index
- (INVALID_BLOCK
+ 1)))
1095 /* Destroy the data structures needed for depth-first search on the
1099 flow_dfs_compute_reverse_finish (data
)
1100 depth_first_search_ds data
;
1103 sbitmap_free (data
->visited_blocks
);
1106 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1107 if REVERSE, go against direction of edges. Returns number of blocks
1108 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
1110 dfs_enumerate_from (bb
, reverse
, predicate
, rslt
, rslt_max
, data
)
1113 bool (*predicate
) (basic_block
, void *);
1118 basic_block
*st
, lbb
;
1121 st
= xcalloc (rslt_max
, sizeof (basic_block
));
1122 rslt
[tv
++] = st
[sp
++] = bb
;
1123 bb
->flags
|= BB_VISITED
;
1130 for (e
= lbb
->pred
; e
; e
= e
->pred_next
)
1131 if (!(e
->src
->flags
& BB_VISITED
) && predicate (e
->src
, data
))
1135 rslt
[tv
++] = st
[sp
++] = e
->src
;
1136 e
->src
->flags
|= BB_VISITED
;
1141 for (e
= lbb
->succ
; e
; e
= e
->succ_next
)
1142 if (!(e
->dest
->flags
& BB_VISITED
) && predicate (e
->dest
, data
))
1146 rslt
[tv
++] = st
[sp
++] = e
->dest
;
1147 e
->dest
->flags
|= BB_VISITED
;
1152 for (sp
= 0; sp
< tv
; sp
++)
1153 rslt
[sp
]->flags
&= ~BB_VISITED
;