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, 2005, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains various simple utilities to analyze the CFG. */
24 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "insn-config.h"
36 /* Store the data structures necessary for depth-first search. */
37 struct depth_first_search_dsS
{
38 /* stack for backtracking during the algorithm */
41 /* number of edges in the stack. That is, positions 0, ..., sp-1
45 /* record of basic blocks already seen by depth-first search */
46 sbitmap visited_blocks
;
48 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
50 static void flow_dfs_compute_reverse_init (depth_first_search_ds
);
51 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds
,
53 static basic_block
flow_dfs_compute_reverse_execute (depth_first_search_ds
,
55 static void flow_dfs_compute_reverse_finish (depth_first_search_ds
);
56 static bool flow_active_insn_p (rtx
);
58 /* Like active_insn_p, except keep the return value clobber around
62 flow_active_insn_p (rtx insn
)
64 if (active_insn_p (insn
))
67 /* A clobber of the function return value exists for buggy
68 programs that fail to return a value. Its effect is to
69 keep the return value from being live across the entire
70 function. If we allow it to be skipped, we introduce the
71 possibility for register lifetime confusion. */
72 if (GET_CODE (PATTERN (insn
)) == CLOBBER
73 && REG_P (XEXP (PATTERN (insn
), 0))
74 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn
), 0)))
80 /* Return true if the block has no effect and only forwards control flow to
81 its single destination. */
84 forwarder_block_p (basic_block bb
)
88 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
89 || !single_succ_p (bb
))
92 for (insn
= BB_HEAD (bb
); insn
!= BB_END (bb
); insn
= NEXT_INSN (insn
))
93 if (INSN_P (insn
) && flow_active_insn_p (insn
))
96 return (!INSN_P (insn
)
97 || (JUMP_P (insn
) && simplejump_p (insn
))
98 || !flow_active_insn_p (insn
));
101 /* Return nonzero if we can reach target from src by falling through. */
104 can_fallthru (basic_block src
, basic_block target
)
106 rtx insn
= BB_END (src
);
111 if (target
== EXIT_BLOCK_PTR
)
113 if (src
->next_bb
!= target
)
115 FOR_EACH_EDGE (e
, ei
, src
->succs
)
116 if (e
->dest
== EXIT_BLOCK_PTR
117 && e
->flags
& EDGE_FALLTHRU
)
120 insn2
= BB_HEAD (target
);
121 if (insn2
&& !active_insn_p (insn2
))
122 insn2
= next_active_insn (insn2
);
124 /* ??? Later we may add code to move jump tables offline. */
125 return next_active_insn (insn
) == insn2
;
128 /* Return nonzero if we could reach target from src by falling through,
129 if the target was made adjacent. If we already have a fall-through
130 edge to the exit block, we can't do that. */
132 could_fall_through (basic_block src
, basic_block target
)
137 if (target
== EXIT_BLOCK_PTR
)
139 FOR_EACH_EDGE (e
, ei
, src
->succs
)
140 if (e
->dest
== EXIT_BLOCK_PTR
141 && e
->flags
& EDGE_FALLTHRU
)
146 /* Mark the back edges in DFS traversal.
147 Return nonzero if a loop (natural or otherwise) is present.
148 Inspired by Depth_First_Search_PP described in:
150 Advanced Compiler Design and Implementation
152 Morgan Kaufmann, 1997
154 and heavily borrowed from pre_and_rev_post_order_compute. */
157 mark_dfs_back_edges (void)
159 edge_iterator
*stack
;
168 /* Allocate the preorder and postorder number arrays. */
169 pre
= XCNEWVEC (int, last_basic_block
);
170 post
= XCNEWVEC (int, last_basic_block
);
172 /* Allocate stack for back-tracking up CFG. */
173 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
176 /* Allocate bitmap to track nodes that have been visited. */
177 visited
= sbitmap_alloc (last_basic_block
);
179 /* None of the nodes in the CFG have been visited yet. */
180 sbitmap_zero (visited
);
182 /* Push the first edge on to the stack. */
183 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR
->succs
);
191 /* Look at the edge on the top of the stack. */
193 src
= ei_edge (ei
)->src
;
194 dest
= ei_edge (ei
)->dest
;
195 ei_edge (ei
)->flags
&= ~EDGE_DFS_BACK
;
197 /* Check if the edge destination has been visited yet. */
198 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
200 /* Mark that we have visited the destination. */
201 SET_BIT (visited
, dest
->index
);
203 pre
[dest
->index
] = prenum
++;
204 if (EDGE_COUNT (dest
->succs
) > 0)
206 /* Since the DEST node has been visited for the first
207 time, check its successors. */
208 stack
[sp
++] = ei_start (dest
->succs
);
211 post
[dest
->index
] = postnum
++;
215 if (dest
!= EXIT_BLOCK_PTR
&& src
!= ENTRY_BLOCK_PTR
216 && pre
[src
->index
] >= pre
[dest
->index
]
217 && post
[dest
->index
] == 0)
218 ei_edge (ei
)->flags
|= EDGE_DFS_BACK
, found
= true;
220 if (ei_one_before_end_p (ei
) && src
!= ENTRY_BLOCK_PTR
)
221 post
[src
->index
] = postnum
++;
223 if (!ei_one_before_end_p (ei
))
224 ei_next (&stack
[sp
- 1]);
233 sbitmap_free (visited
);
238 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
241 set_edge_can_fallthru_flag (void)
250 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
252 e
->flags
&= ~EDGE_CAN_FALLTHRU
;
254 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
255 if (e
->flags
& EDGE_FALLTHRU
)
256 e
->flags
|= EDGE_CAN_FALLTHRU
;
259 /* If the BB ends with an invertible condjump all (2) edges are
260 CAN_FALLTHRU edges. */
261 if (EDGE_COUNT (bb
->succs
) != 2)
263 if (!any_condjump_p (BB_END (bb
)))
265 if (!invert_jump (BB_END (bb
), JUMP_LABEL (BB_END (bb
)), 0))
267 invert_jump (BB_END (bb
), JUMP_LABEL (BB_END (bb
)), 0);
268 EDGE_SUCC (bb
, 0)->flags
|= EDGE_CAN_FALLTHRU
;
269 EDGE_SUCC (bb
, 1)->flags
|= EDGE_CAN_FALLTHRU
;
273 /* Find unreachable blocks. An unreachable block will have 0 in
274 the reachable bit in block->flags. A nonzero value indicates the
275 block is reachable. */
278 find_unreachable_blocks (void)
282 basic_block
*tos
, *worklist
, bb
;
284 tos
= worklist
= XNEWVEC (basic_block
, n_basic_blocks
);
286 /* Clear all the reachability flags. */
289 bb
->flags
&= ~BB_REACHABLE
;
291 /* Add our starting points to the worklist. Almost always there will
292 be only one. It isn't inconceivable that we might one day directly
293 support Fortran alternate entry points. */
295 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
299 /* Mark the block reachable. */
300 e
->dest
->flags
|= BB_REACHABLE
;
303 /* Iterate: find everything reachable from what we've already seen. */
305 while (tos
!= worklist
)
307 basic_block b
= *--tos
;
309 FOR_EACH_EDGE (e
, ei
, b
->succs
)
311 basic_block dest
= e
->dest
;
313 if (!(dest
->flags
& BB_REACHABLE
))
316 dest
->flags
|= BB_REACHABLE
;
324 /* Functions to access an edge list with a vector representation.
325 Enough data is kept such that given an index number, the
326 pred and succ that edge represents can be determined, or
327 given a pred and a succ, its index number can be returned.
328 This allows algorithms which consume a lot of memory to
329 represent the normally full matrix of edge (pred,succ) with a
330 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
331 wasted space in the client code due to sparse flow graphs. */
333 /* This functions initializes the edge list. Basically the entire
334 flowgraph is processed, and all edges are assigned a number,
335 and the data structure is filled in. */
338 create_edge_list (void)
340 struct edge_list
*elist
;
347 block_count
= n_basic_blocks
; /* Include the entry and exit blocks. */
351 /* Determine the number of edges in the flow graph by counting successor
352 edges on each basic block. */
353 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
355 num_edges
+= EDGE_COUNT (bb
->succs
);
358 elist
= XNEW (struct edge_list
);
359 elist
->num_blocks
= block_count
;
360 elist
->num_edges
= num_edges
;
361 elist
->index_to_edge
= XNEWVEC (edge
, num_edges
);
365 /* Follow successors of blocks, and register these edges. */
366 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
367 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
368 elist
->index_to_edge
[num_edges
++] = e
;
373 /* This function free's memory associated with an edge list. */
376 free_edge_list (struct edge_list
*elist
)
380 free (elist
->index_to_edge
);
385 /* This function provides debug output showing an edge list. */
388 print_edge_list (FILE *f
, struct edge_list
*elist
)
392 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
393 elist
->num_blocks
, elist
->num_edges
);
395 for (x
= 0; x
< elist
->num_edges
; x
++)
397 fprintf (f
, " %-4d - edge(", x
);
398 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR
)
399 fprintf (f
, "entry,");
401 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
403 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR
)
404 fprintf (f
, "exit)\n");
406 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
410 /* This function provides an internal consistency check of an edge list,
411 verifying that all edges are present, and that there are no
415 verify_edge_list (FILE *f
, struct edge_list
*elist
)
417 int pred
, succ
, index
;
419 basic_block bb
, p
, s
;
422 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
424 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
426 pred
= e
->src
->index
;
427 succ
= e
->dest
->index
;
428 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
429 if (index
== EDGE_INDEX_NO_EDGE
)
431 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
435 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
436 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
437 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
438 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
439 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
440 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
444 /* We've verified that all the edges are in the list, now lets make sure
445 there are no spurious edges in the list. */
447 FOR_BB_BETWEEN (p
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
448 FOR_BB_BETWEEN (s
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
452 FOR_EACH_EDGE (e
, ei
, p
->succs
)
459 FOR_EACH_EDGE (e
, ei
, s
->preds
)
466 if (EDGE_INDEX (elist
, p
, s
)
467 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
468 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
470 if (EDGE_INDEX (elist
, p
, s
)
471 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
472 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
473 p
->index
, s
->index
, EDGE_INDEX (elist
, p
, s
));
477 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
478 If no such edge exists, return NULL. */
481 find_edge (basic_block pred
, basic_block succ
)
486 if (EDGE_COUNT (pred
->succs
) <= EDGE_COUNT (succ
->preds
))
488 FOR_EACH_EDGE (e
, ei
, pred
->succs
)
494 FOR_EACH_EDGE (e
, ei
, succ
->preds
)
502 /* This routine will determine what, if any, edge there is between
503 a specified predecessor and successor. */
506 find_edge_index (struct edge_list
*edge_list
, basic_block pred
, basic_block succ
)
510 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
511 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
512 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
515 return (EDGE_INDEX_NO_EDGE
);
518 /* Dump the list of basic blocks in the bitmap NODES. */
521 flow_nodes_print (const char *str
, const sbitmap nodes
, FILE *file
)
523 unsigned int node
= 0;
524 sbitmap_iterator sbi
;
529 fprintf (file
, "%s { ", str
);
530 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, node
, sbi
)
531 fprintf (file
, "%d ", node
);
535 /* Dump the list of edges in the array EDGE_LIST. */
538 flow_edge_list_print (const char *str
, const edge
*edge_list
, int num_edges
, FILE *file
)
545 fprintf (file
, "%s { ", str
);
546 for (i
= 0; i
< num_edges
; i
++)
547 fprintf (file
, "%d->%d ", edge_list
[i
]->src
->index
,
548 edge_list
[i
]->dest
->index
);
554 /* This routine will remove any fake predecessor edges for a basic block.
555 When the edge is removed, it is also removed from whatever successor
559 remove_fake_predecessors (basic_block bb
)
564 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
566 if ((e
->flags
& EDGE_FAKE
) == EDGE_FAKE
)
573 /* This routine will remove all fake edges from the flow graph. If
574 we remove all fake successors, it will automatically remove all
575 fake predecessors. */
578 remove_fake_edges (void)
582 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
583 remove_fake_predecessors (bb
);
586 /* This routine will remove all fake edges to the EXIT_BLOCK. */
589 remove_fake_exit_edges (void)
591 remove_fake_predecessors (EXIT_BLOCK_PTR
);
595 /* This function will add a fake edge between any block which has no
596 successors, and the exit block. Some data flow equations require these
600 add_noreturn_fake_exit_edges (void)
605 if (EDGE_COUNT (bb
->succs
) == 0)
606 make_single_succ_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
609 /* This function adds a fake edge between any infinite loops to the
610 exit block. Some optimizations require a path from each node to
613 See also Morgan, Figure 3.10, pp. 82-83.
615 The current implementation is ugly, not attempting to minimize the
616 number of inserted fake edges. To reduce the number of fake edges
617 to insert, add fake edges from _innermost_ loops containing only
618 nodes not reachable from the exit block. */
621 connect_infinite_loops_to_exit (void)
623 basic_block unvisited_block
= EXIT_BLOCK_PTR
;
624 struct depth_first_search_dsS dfs_ds
;
626 /* Perform depth-first search in the reverse graph to find nodes
627 reachable from the exit block. */
628 flow_dfs_compute_reverse_init (&dfs_ds
);
629 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR
);
631 /* Repeatedly add fake edges, updating the unreachable nodes. */
634 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
,
636 if (!unvisited_block
)
639 make_edge (unvisited_block
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
640 flow_dfs_compute_reverse_add_bb (&dfs_ds
, unvisited_block
);
643 flow_dfs_compute_reverse_finish (&dfs_ds
);
647 /* Compute reverse top sort order.
648 This is computing a post order numbering of the graph. */
651 post_order_compute (int *post_order
, bool include_entry_exit
)
653 edge_iterator
*stack
;
655 int post_order_num
= 0;
658 if (include_entry_exit
)
659 post_order
[post_order_num
++] = EXIT_BLOCK
;
661 /* Allocate stack for back-tracking up CFG. */
662 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
665 /* Allocate bitmap to track nodes that have been visited. */
666 visited
= sbitmap_alloc (last_basic_block
);
668 /* None of the nodes in the CFG have been visited yet. */
669 sbitmap_zero (visited
);
671 /* Push the first edge on to the stack. */
672 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR
->succs
);
680 /* Look at the edge on the top of the stack. */
682 src
= ei_edge (ei
)->src
;
683 dest
= ei_edge (ei
)->dest
;
685 /* Check if the edge destination has been visited yet. */
686 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
688 /* Mark that we have visited the destination. */
689 SET_BIT (visited
, dest
->index
);
691 if (EDGE_COUNT (dest
->succs
) > 0)
692 /* Since the DEST node has been visited for the first
693 time, check its successors. */
694 stack
[sp
++] = ei_start (dest
->succs
);
696 post_order
[post_order_num
++] = dest
->index
;
700 if (ei_one_before_end_p (ei
) && src
!= ENTRY_BLOCK_PTR
)
701 post_order
[post_order_num
++] = src
->index
;
703 if (!ei_one_before_end_p (ei
))
704 ei_next (&stack
[sp
- 1]);
710 if (include_entry_exit
)
711 post_order
[post_order_num
++] = ENTRY_BLOCK
;
714 sbitmap_free (visited
);
715 return post_order_num
;
718 /* Compute the depth first search order and store in the array
719 PRE_ORDER if nonzero, marking the nodes visited in VISITED. If
720 REV_POST_ORDER is nonzero, return the reverse completion number for each
721 node. Returns the number of nodes visited. A depth first search
722 tries to get as far away from the starting point as quickly as
725 pre_order is a really a preorder numbering of the graph.
726 rev_post_order is really a reverse postorder numbering of the graph.
730 pre_and_rev_post_order_compute (int *pre_order
, int *rev_post_order
,
731 bool include_entry_exit
)
733 edge_iterator
*stack
;
735 int pre_order_num
= 0;
736 int rev_post_order_num
= n_basic_blocks
- 1;
739 /* Allocate stack for back-tracking up CFG. */
740 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
743 if (include_entry_exit
)
746 pre_order
[pre_order_num
] = ENTRY_BLOCK
;
749 rev_post_order
[rev_post_order_num
--] = ENTRY_BLOCK
;
752 rev_post_order_num
-= NUM_FIXED_BLOCKS
;
754 /* Allocate bitmap to track nodes that have been visited. */
755 visited
= sbitmap_alloc (last_basic_block
);
757 /* None of the nodes in the CFG have been visited yet. */
758 sbitmap_zero (visited
);
760 /* Push the first edge on to the stack. */
761 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR
->succs
);
769 /* Look at the edge on the top of the stack. */
771 src
= ei_edge (ei
)->src
;
772 dest
= ei_edge (ei
)->dest
;
774 /* Check if the edge destination has been visited yet. */
775 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
777 /* Mark that we have visited the destination. */
778 SET_BIT (visited
, dest
->index
);
781 pre_order
[pre_order_num
] = dest
->index
;
785 if (EDGE_COUNT (dest
->succs
) > 0)
786 /* Since the DEST node has been visited for the first
787 time, check its successors. */
788 stack
[sp
++] = ei_start (dest
->succs
);
789 else if (rev_post_order
)
790 /* There are no successors for the DEST node so assign
791 its reverse completion number. */
792 rev_post_order
[rev_post_order_num
--] = dest
->index
;
796 if (ei_one_before_end_p (ei
) && src
!= ENTRY_BLOCK_PTR
798 /* There are no more successors for the SRC node
799 so assign its reverse completion number. */
800 rev_post_order
[rev_post_order_num
--] = src
->index
;
802 if (!ei_one_before_end_p (ei
))
803 ei_next (&stack
[sp
- 1]);
810 sbitmap_free (visited
);
812 if (include_entry_exit
)
815 pre_order
[pre_order_num
] = EXIT_BLOCK
;
818 rev_post_order
[rev_post_order_num
--] = EXIT_BLOCK
;
819 /* The number of nodes visited should be the number of blocks. */
820 gcc_assert (pre_order_num
== n_basic_blocks
);
823 /* The number of nodes visited should be the number of blocks minus
824 the entry and exit blocks which are not visited here. */
825 gcc_assert (pre_order_num
== n_basic_blocks
- NUM_FIXED_BLOCKS
);
827 return pre_order_num
;
830 /* Compute the depth first search order on the _reverse_ graph and
831 store in the array DFS_ORDER, marking the nodes visited in VISITED.
832 Returns the number of nodes visited.
834 The computation is split into three pieces:
836 flow_dfs_compute_reverse_init () creates the necessary data
839 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
840 structures. The block will start the search.
842 flow_dfs_compute_reverse_execute () continues (or starts) the
843 search using the block on the top of the stack, stopping when the
846 flow_dfs_compute_reverse_finish () destroys the necessary data
849 Thus, the user will probably call ..._init(), call ..._add_bb() to
850 add a beginning basic block to the stack, call ..._execute(),
851 possibly add another bb to the stack and again call ..._execute(),
852 ..., and finally call _finish(). */
854 /* Initialize the data structures used for depth-first search on the
855 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
856 added to the basic block stack. DATA is the current depth-first
857 search context. If INITIALIZE_STACK is nonzero, there is an
858 element on the stack. */
861 flow_dfs_compute_reverse_init (depth_first_search_ds data
)
863 /* Allocate stack for back-tracking up CFG. */
864 data
->stack
= XNEWVEC (basic_block
, n_basic_blocks
);
867 /* Allocate bitmap to track nodes that have been visited. */
868 data
->visited_blocks
= sbitmap_alloc (last_basic_block
);
870 /* None of the nodes in the CFG have been visited yet. */
871 sbitmap_zero (data
->visited_blocks
);
876 /* Add the specified basic block to the top of the dfs data
877 structures. When the search continues, it will start at the
881 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data
, basic_block bb
)
883 data
->stack
[data
->sp
++] = bb
;
884 SET_BIT (data
->visited_blocks
, bb
->index
);
887 /* Continue the depth-first search through the reverse graph starting with the
888 block at the stack's top and ending when the stack is empty. Visited nodes
889 are marked. Returns an unvisited basic block, or NULL if there is none
893 flow_dfs_compute_reverse_execute (depth_first_search_ds data
,
894 basic_block last_unvisited
)
902 bb
= data
->stack
[--data
->sp
];
904 /* Perform depth-first search on adjacent vertices. */
905 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
906 if (!TEST_BIT (data
->visited_blocks
, e
->src
->index
))
907 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
910 /* Determine if there are unvisited basic blocks. */
911 FOR_BB_BETWEEN (bb
, last_unvisited
, NULL
, prev_bb
)
912 if (!TEST_BIT (data
->visited_blocks
, bb
->index
))
918 /* Destroy the data structures needed for depth-first search on the
922 flow_dfs_compute_reverse_finish (depth_first_search_ds data
)
925 sbitmap_free (data
->visited_blocks
);
928 /* Performs dfs search from BB over vertices satisfying PREDICATE;
929 if REVERSE, go against direction of edges. Returns number of blocks
930 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
932 dfs_enumerate_from (basic_block bb
, int reverse
,
933 bool (*predicate
) (basic_block
, void *),
934 basic_block
*rslt
, int rslt_max
, void *data
)
936 basic_block
*st
, lbb
;
940 /* A bitmap to keep track of visited blocks. Allocating it each time
941 this function is called is not possible, since dfs_enumerate_from
942 is often used on small (almost) disjoint parts of cfg (bodies of
943 loops), and allocating a large sbitmap would lead to quadratic
945 static sbitmap visited
;
946 static unsigned v_size
;
948 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
949 #define UNMARK_VISITED(BB) (RESET_BIT (visited, (BB)->index))
950 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
952 /* Resize the VISITED sbitmap if necessary. */
953 size
= last_basic_block
;
960 visited
= sbitmap_alloc (size
);
961 sbitmap_zero (visited
);
964 else if (v_size
< size
)
966 /* Ensure that we increase the size of the sbitmap exponentially. */
967 if (2 * v_size
> size
)
970 visited
= sbitmap_resize (visited
, size
, 0);
974 st
= XCNEWVEC (basic_block
, rslt_max
);
975 rslt
[tv
++] = st
[sp
++] = bb
;
984 FOR_EACH_EDGE (e
, ei
, lbb
->preds
)
985 if (!VISITED_P (e
->src
) && predicate (e
->src
, data
))
987 gcc_assert (tv
!= rslt_max
);
988 rslt
[tv
++] = st
[sp
++] = e
->src
;
989 MARK_VISITED (e
->src
);
994 FOR_EACH_EDGE (e
, ei
, lbb
->succs
)
995 if (!VISITED_P (e
->dest
) && predicate (e
->dest
, data
))
997 gcc_assert (tv
!= rslt_max
);
998 rslt
[tv
++] = st
[sp
++] = e
->dest
;
999 MARK_VISITED (e
->dest
);
1004 for (sp
= 0; sp
< tv
; sp
++)
1005 UNMARK_VISITED (rslt
[sp
]);
1008 #undef UNMARK_VISITED
1013 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1015 This algorithm can be found in Timothy Harvey's PhD thesis, at
1016 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1017 dominance algorithms.
1019 First, we identify each join point, j (any node with more than one
1020 incoming edge is a join point).
1022 We then examine each predecessor, p, of j and walk up the dominator tree
1025 We stop the walk when we reach j's immediate dominator - j is in the
1026 dominance frontier of each of the nodes in the walk, except for j's
1027 immediate dominator. Intuitively, all of the rest of j's dominators are
1028 shared by j's predecessors as well.
1029 Since they dominate j, they will not have j in their dominance frontiers.
1031 The number of nodes touched by this algorithm is equal to the size
1032 of the dominance frontiers, no more, no less.
1037 compute_dominance_frontiers_1 (bitmap
*frontiers
)
1044 if (EDGE_COUNT (b
->preds
) >= 2)
1046 FOR_EACH_EDGE (p
, ei
, b
->preds
)
1048 basic_block runner
= p
->src
;
1050 if (runner
== ENTRY_BLOCK_PTR
)
1053 domsb
= get_immediate_dominator (CDI_DOMINATORS
, b
);
1054 while (runner
!= domsb
)
1056 if (bitmap_bit_p (frontiers
[runner
->index
], b
->index
))
1058 bitmap_set_bit (frontiers
[runner
->index
],
1060 runner
= get_immediate_dominator (CDI_DOMINATORS
,
1070 compute_dominance_frontiers (bitmap
*frontiers
)
1072 timevar_push (TV_DOM_FRONTIERS
);
1074 compute_dominance_frontiers_1 (frontiers
);
1076 timevar_pop (TV_DOM_FRONTIERS
);