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, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains various simple utilities to analyze the CFG. */
25 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "insn-config.h"
33 #include "diagnostic-core.h"
42 /* Store the data structures necessary for depth-first search. */
43 struct depth_first_search_dsS
{
44 /* stack for backtracking during the algorithm */
47 /* number of edges in the stack. That is, positions 0, ..., sp-1
51 /* record of basic blocks already seen by depth-first search */
52 sbitmap visited_blocks
;
54 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
56 static void flow_dfs_compute_reverse_init (depth_first_search_ds
);
57 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds
,
59 static basic_block
flow_dfs_compute_reverse_execute (depth_first_search_ds
,
61 static void flow_dfs_compute_reverse_finish (depth_first_search_ds
);
62 static bool flow_active_insn_p (const_rtx
);
64 /* Like active_insn_p, except keep the return value clobber around
68 flow_active_insn_p (const_rtx insn
)
70 if (active_insn_p (insn
))
73 /* A clobber of the function return value exists for buggy
74 programs that fail to return a value. Its effect is to
75 keep the return value from being live across the entire
76 function. If we allow it to be skipped, we introduce the
77 possibility for register lifetime confusion. */
78 if (GET_CODE (PATTERN (insn
)) == CLOBBER
79 && REG_P (XEXP (PATTERN (insn
), 0))
80 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn
), 0)))
86 /* Return true if the block has no effect and only forwards control flow to
87 its single destination. */
90 forwarder_block_p (const_basic_block bb
)
94 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
95 || !single_succ_p (bb
))
98 for (insn
= BB_HEAD (bb
); insn
!= BB_END (bb
); insn
= NEXT_INSN (insn
))
99 if (INSN_P (insn
) && flow_active_insn_p (insn
))
102 return (!INSN_P (insn
)
103 || (JUMP_P (insn
) && simplejump_p (insn
))
104 || !flow_active_insn_p (insn
));
107 /* Return nonzero if we can reach target from src by falling through. */
110 can_fallthru (basic_block src
, basic_block target
)
112 rtx insn
= BB_END (src
);
117 if (target
== EXIT_BLOCK_PTR
)
119 if (src
->next_bb
!= target
)
121 FOR_EACH_EDGE (e
, ei
, src
->succs
)
122 if (e
->dest
== EXIT_BLOCK_PTR
123 && e
->flags
& EDGE_FALLTHRU
)
126 insn2
= BB_HEAD (target
);
127 if (insn2
&& !active_insn_p (insn2
))
128 insn2
= next_active_insn (insn2
);
130 /* ??? Later we may add code to move jump tables offline. */
131 return next_active_insn (insn
) == insn2
;
134 /* Return nonzero if we could reach target from src by falling through,
135 if the target was made adjacent. If we already have a fall-through
136 edge to the exit block, we can't do that. */
138 could_fall_through (basic_block src
, basic_block target
)
143 if (target
== EXIT_BLOCK_PTR
)
145 FOR_EACH_EDGE (e
, ei
, src
->succs
)
146 if (e
->dest
== EXIT_BLOCK_PTR
147 && e
->flags
& EDGE_FALLTHRU
)
152 /* Mark the back edges in DFS traversal.
153 Return nonzero if a loop (natural or otherwise) is present.
154 Inspired by Depth_First_Search_PP described in:
156 Advanced Compiler Design and Implementation
158 Morgan Kaufmann, 1997
160 and heavily borrowed from pre_and_rev_post_order_compute. */
163 mark_dfs_back_edges (void)
165 edge_iterator
*stack
;
174 /* Allocate the preorder and postorder number arrays. */
175 pre
= XCNEWVEC (int, last_basic_block
);
176 post
= XCNEWVEC (int, last_basic_block
);
178 /* Allocate stack for back-tracking up CFG. */
179 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
182 /* Allocate bitmap to track nodes that have been visited. */
183 visited
= sbitmap_alloc (last_basic_block
);
185 /* None of the nodes in the CFG have been visited yet. */
186 sbitmap_zero (visited
);
188 /* Push the first edge on to the stack. */
189 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR
->succs
);
197 /* Look at the edge on the top of the stack. */
199 src
= ei_edge (ei
)->src
;
200 dest
= ei_edge (ei
)->dest
;
201 ei_edge (ei
)->flags
&= ~EDGE_DFS_BACK
;
203 /* Check if the edge destination has been visited yet. */
204 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
206 /* Mark that we have visited the destination. */
207 SET_BIT (visited
, dest
->index
);
209 pre
[dest
->index
] = prenum
++;
210 if (EDGE_COUNT (dest
->succs
) > 0)
212 /* Since the DEST node has been visited for the first
213 time, check its successors. */
214 stack
[sp
++] = ei_start (dest
->succs
);
217 post
[dest
->index
] = postnum
++;
221 if (dest
!= EXIT_BLOCK_PTR
&& src
!= ENTRY_BLOCK_PTR
222 && pre
[src
->index
] >= pre
[dest
->index
]
223 && post
[dest
->index
] == 0)
224 ei_edge (ei
)->flags
|= EDGE_DFS_BACK
, found
= true;
226 if (ei_one_before_end_p (ei
) && src
!= ENTRY_BLOCK_PTR
)
227 post
[src
->index
] = postnum
++;
229 if (!ei_one_before_end_p (ei
))
230 ei_next (&stack
[sp
- 1]);
239 sbitmap_free (visited
);
244 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
247 set_edge_can_fallthru_flag (void)
256 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
258 e
->flags
&= ~EDGE_CAN_FALLTHRU
;
260 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
261 if (e
->flags
& EDGE_FALLTHRU
)
262 e
->flags
|= EDGE_CAN_FALLTHRU
;
265 /* If the BB ends with an invertible condjump all (2) edges are
266 CAN_FALLTHRU edges. */
267 if (EDGE_COUNT (bb
->succs
) != 2)
269 if (!any_condjump_p (BB_END (bb
)))
271 if (!invert_jump (BB_END (bb
), JUMP_LABEL (BB_END (bb
)), 0))
273 invert_jump (BB_END (bb
), JUMP_LABEL (BB_END (bb
)), 0);
274 EDGE_SUCC (bb
, 0)->flags
|= EDGE_CAN_FALLTHRU
;
275 EDGE_SUCC (bb
, 1)->flags
|= EDGE_CAN_FALLTHRU
;
279 /* Find unreachable blocks. An unreachable block will have 0 in
280 the reachable bit in block->flags. A nonzero value indicates the
281 block is reachable. */
284 find_unreachable_blocks (void)
288 basic_block
*tos
, *worklist
, bb
;
290 tos
= worklist
= XNEWVEC (basic_block
, n_basic_blocks
);
292 /* Clear all the reachability flags. */
295 bb
->flags
&= ~BB_REACHABLE
;
297 /* Add our starting points to the worklist. Almost always there will
298 be only one. It isn't inconceivable that we might one day directly
299 support Fortran alternate entry points. */
301 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
305 /* Mark the block reachable. */
306 e
->dest
->flags
|= BB_REACHABLE
;
309 /* Iterate: find everything reachable from what we've already seen. */
311 while (tos
!= worklist
)
313 basic_block b
= *--tos
;
315 FOR_EACH_EDGE (e
, ei
, b
->succs
)
317 basic_block dest
= e
->dest
;
319 if (!(dest
->flags
& BB_REACHABLE
))
322 dest
->flags
|= BB_REACHABLE
;
330 /* Functions to access an edge list with a vector representation.
331 Enough data is kept such that given an index number, the
332 pred and succ that edge represents can be determined, or
333 given a pred and a succ, its index number can be returned.
334 This allows algorithms which consume a lot of memory to
335 represent the normally full matrix of edge (pred,succ) with a
336 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
337 wasted space in the client code due to sparse flow graphs. */
339 /* This functions initializes the edge list. Basically the entire
340 flowgraph is processed, and all edges are assigned a number,
341 and the data structure is filled in. */
344 create_edge_list (void)
346 struct edge_list
*elist
;
353 block_count
= n_basic_blocks
; /* Include the entry and exit blocks. */
357 /* Determine the number of edges in the flow graph by counting successor
358 edges on each basic block. */
359 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
361 num_edges
+= EDGE_COUNT (bb
->succs
);
364 elist
= XNEW (struct edge_list
);
365 elist
->num_blocks
= block_count
;
366 elist
->num_edges
= num_edges
;
367 elist
->index_to_edge
= XNEWVEC (edge
, num_edges
);
371 /* Follow successors of blocks, and register these edges. */
372 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
373 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
374 elist
->index_to_edge
[num_edges
++] = e
;
379 /* This function free's memory associated with an edge list. */
382 free_edge_list (struct edge_list
*elist
)
386 free (elist
->index_to_edge
);
391 /* This function provides debug output showing an edge list. */
394 print_edge_list (FILE *f
, struct edge_list
*elist
)
398 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
399 elist
->num_blocks
, elist
->num_edges
);
401 for (x
= 0; x
< elist
->num_edges
; x
++)
403 fprintf (f
, " %-4d - edge(", x
);
404 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR
)
405 fprintf (f
, "entry,");
407 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
409 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR
)
410 fprintf (f
, "exit)\n");
412 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
416 /* This function provides an internal consistency check of an edge list,
417 verifying that all edges are present, and that there are no
421 verify_edge_list (FILE *f
, struct edge_list
*elist
)
423 int pred
, succ
, index
;
425 basic_block bb
, p
, s
;
428 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
430 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
432 pred
= e
->src
->index
;
433 succ
= e
->dest
->index
;
434 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
435 if (index
== EDGE_INDEX_NO_EDGE
)
437 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
441 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
442 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
443 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
444 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
445 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
446 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
450 /* We've verified that all the edges are in the list, now lets make sure
451 there are no spurious edges in the list. */
453 FOR_BB_BETWEEN (p
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
454 FOR_BB_BETWEEN (s
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
458 FOR_EACH_EDGE (e
, ei
, p
->succs
)
465 FOR_EACH_EDGE (e
, ei
, s
->preds
)
472 if (EDGE_INDEX (elist
, p
, s
)
473 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
474 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
476 if (EDGE_INDEX (elist
, p
, s
)
477 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
478 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
479 p
->index
, s
->index
, EDGE_INDEX (elist
, p
, s
));
483 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
484 If no such edge exists, return NULL. */
487 find_edge (basic_block pred
, basic_block succ
)
492 if (EDGE_COUNT (pred
->succs
) <= EDGE_COUNT (succ
->preds
))
494 FOR_EACH_EDGE (e
, ei
, pred
->succs
)
500 FOR_EACH_EDGE (e
, ei
, succ
->preds
)
508 /* This routine will determine what, if any, edge there is between
509 a specified predecessor and successor. */
512 find_edge_index (struct edge_list
*edge_list
, basic_block pred
, basic_block succ
)
516 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
517 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
518 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
521 return (EDGE_INDEX_NO_EDGE
);
524 /* Dump the list of basic blocks in the bitmap NODES. */
527 flow_nodes_print (const char *str
, const_sbitmap nodes
, FILE *file
)
529 unsigned int node
= 0;
530 sbitmap_iterator sbi
;
535 fprintf (file
, "%s { ", str
);
536 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, node
, sbi
)
537 fprintf (file
, "%d ", node
);
541 /* Dump the list of edges in the array EDGE_LIST. */
544 flow_edge_list_print (const char *str
, const edge
*edge_list
, int num_edges
, FILE *file
)
551 fprintf (file
, "%s { ", str
);
552 for (i
= 0; i
< num_edges
; i
++)
553 fprintf (file
, "%d->%d ", edge_list
[i
]->src
->index
,
554 edge_list
[i
]->dest
->index
);
560 /* This routine will remove any fake predecessor edges for a basic block.
561 When the edge is removed, it is also removed from whatever successor
565 remove_fake_predecessors (basic_block bb
)
570 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
572 if ((e
->flags
& EDGE_FAKE
) == EDGE_FAKE
)
579 /* This routine will remove all fake edges from the flow graph. If
580 we remove all fake successors, it will automatically remove all
581 fake predecessors. */
584 remove_fake_edges (void)
588 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
589 remove_fake_predecessors (bb
);
592 /* This routine will remove all fake edges to the EXIT_BLOCK. */
595 remove_fake_exit_edges (void)
597 remove_fake_predecessors (EXIT_BLOCK_PTR
);
601 /* This function will add a fake edge between any block which has no
602 successors, and the exit block. Some data flow equations require these
606 add_noreturn_fake_exit_edges (void)
611 if (EDGE_COUNT (bb
->succs
) == 0)
612 make_single_succ_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
615 /* This function adds a fake edge between any infinite loops to the
616 exit block. Some optimizations require a path from each node to
619 See also Morgan, Figure 3.10, pp. 82-83.
621 The current implementation is ugly, not attempting to minimize the
622 number of inserted fake edges. To reduce the number of fake edges
623 to insert, add fake edges from _innermost_ loops containing only
624 nodes not reachable from the exit block. */
627 connect_infinite_loops_to_exit (void)
629 basic_block unvisited_block
= EXIT_BLOCK_PTR
;
630 struct depth_first_search_dsS dfs_ds
;
632 /* Perform depth-first search in the reverse graph to find nodes
633 reachable from the exit block. */
634 flow_dfs_compute_reverse_init (&dfs_ds
);
635 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR
);
637 /* Repeatedly add fake edges, updating the unreachable nodes. */
640 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
,
642 if (!unvisited_block
)
645 make_edge (unvisited_block
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
646 flow_dfs_compute_reverse_add_bb (&dfs_ds
, unvisited_block
);
649 flow_dfs_compute_reverse_finish (&dfs_ds
);
653 /* Compute reverse top sort order. This is computing a post order
654 numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then then
655 ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
656 true, unreachable blocks are deleted. */
659 post_order_compute (int *post_order
, bool include_entry_exit
,
660 bool delete_unreachable
)
662 edge_iterator
*stack
;
664 int post_order_num
= 0;
668 if (include_entry_exit
)
669 post_order
[post_order_num
++] = EXIT_BLOCK
;
671 /* Allocate stack for back-tracking up CFG. */
672 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
675 /* Allocate bitmap to track nodes that have been visited. */
676 visited
= sbitmap_alloc (last_basic_block
);
678 /* None of the nodes in the CFG have been visited yet. */
679 sbitmap_zero (visited
);
681 /* Push the first edge on to the stack. */
682 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR
->succs
);
690 /* Look at the edge on the top of the stack. */
692 src
= ei_edge (ei
)->src
;
693 dest
= ei_edge (ei
)->dest
;
695 /* Check if the edge destination has been visited yet. */
696 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
698 /* Mark that we have visited the destination. */
699 SET_BIT (visited
, dest
->index
);
701 if (EDGE_COUNT (dest
->succs
) > 0)
702 /* Since the DEST node has been visited for the first
703 time, check its successors. */
704 stack
[sp
++] = ei_start (dest
->succs
);
706 post_order
[post_order_num
++] = dest
->index
;
710 if (ei_one_before_end_p (ei
) && src
!= ENTRY_BLOCK_PTR
)
711 post_order
[post_order_num
++] = src
->index
;
713 if (!ei_one_before_end_p (ei
))
714 ei_next (&stack
[sp
- 1]);
720 if (include_entry_exit
)
722 post_order
[post_order_num
++] = ENTRY_BLOCK
;
723 count
= post_order_num
;
726 count
= post_order_num
+ 2;
728 /* Delete the unreachable blocks if some were found and we are
729 supposed to do it. */
730 if (delete_unreachable
&& (count
!= n_basic_blocks
))
734 for (b
= ENTRY_BLOCK_PTR
->next_bb
; b
!= EXIT_BLOCK_PTR
; b
= next_bb
)
736 next_bb
= b
->next_bb
;
738 if (!(TEST_BIT (visited
, b
->index
)))
739 delete_basic_block (b
);
742 tidy_fallthru_edges ();
746 sbitmap_free (visited
);
747 return post_order_num
;
751 /* Helper routine for inverted_post_order_compute.
752 BB has to belong to a region of CFG
753 unreachable by inverted traversal from the exit.
754 i.e. there's no control flow path from ENTRY to EXIT
755 that contains this BB.
756 This can happen in two cases - if there's an infinite loop
757 or if there's a block that has no successor
758 (call to a function with no return).
759 Some RTL passes deal with this condition by
760 calling connect_infinite_loops_to_exit () and/or
761 add_noreturn_fake_exit_edges ().
762 However, those methods involve modifying the CFG itself
763 which may not be desirable.
764 Hence, we deal with the infinite loop/no return cases
765 by identifying a unique basic block that can reach all blocks
766 in such a region by inverted traversal.
767 This function returns a basic block that guarantees
768 that all blocks in the region are reachable
769 by starting an inverted traversal from the returned block. */
772 dfs_find_deadend (basic_block bb
)
774 sbitmap visited
= sbitmap_alloc (last_basic_block
);
775 sbitmap_zero (visited
);
779 SET_BIT (visited
, bb
->index
);
780 if (EDGE_COUNT (bb
->succs
) == 0
781 || TEST_BIT (visited
, EDGE_SUCC (bb
, 0)->dest
->index
))
783 sbitmap_free (visited
);
787 bb
= EDGE_SUCC (bb
, 0)->dest
;
794 /* Compute the reverse top sort order of the inverted CFG
795 i.e. starting from the exit block and following the edges backward
796 (from successors to predecessors).
797 This ordering can be used for forward dataflow problems among others.
799 This function assumes that all blocks in the CFG are reachable
800 from the ENTRY (but not necessarily from EXIT).
802 If there's an infinite loop,
803 a simple inverted traversal starting from the blocks
804 with no successors can't visit all blocks.
805 To solve this problem, we first do inverted traversal
806 starting from the blocks with no successor.
807 And if there's any block left that's not visited by the regular
808 inverted traversal from EXIT,
809 those blocks are in such problematic region.
810 Among those, we find one block that has
811 any visited predecessor (which is an entry into such a region),
812 and start looking for a "dead end" from that block
813 and do another inverted traversal from that block. */
816 inverted_post_order_compute (int *post_order
)
819 edge_iterator
*stack
;
821 int post_order_num
= 0;
824 /* Allocate stack for back-tracking up CFG. */
825 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
828 /* Allocate bitmap to track nodes that have been visited. */
829 visited
= sbitmap_alloc (last_basic_block
);
831 /* None of the nodes in the CFG have been visited yet. */
832 sbitmap_zero (visited
);
834 /* Put all blocks that have no successor into the initial work list. */
835 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
836 if (EDGE_COUNT (bb
->succs
) == 0)
838 /* Push the initial edge on to the stack. */
839 if (EDGE_COUNT (bb
->preds
) > 0)
841 stack
[sp
++] = ei_start (bb
->preds
);
842 SET_BIT (visited
, bb
->index
);
848 bool has_unvisited_bb
= false;
850 /* The inverted traversal loop. */
856 /* Look at the edge on the top of the stack. */
858 bb
= ei_edge (ei
)->dest
;
859 pred
= ei_edge (ei
)->src
;
861 /* Check if the predecessor has been visited yet. */
862 if (! TEST_BIT (visited
, pred
->index
))
864 /* Mark that we have visited the destination. */
865 SET_BIT (visited
, pred
->index
);
867 if (EDGE_COUNT (pred
->preds
) > 0)
868 /* Since the predecessor node has been visited for the first
869 time, check its predecessors. */
870 stack
[sp
++] = ei_start (pred
->preds
);
872 post_order
[post_order_num
++] = pred
->index
;
876 if (bb
!= EXIT_BLOCK_PTR
&& ei_one_before_end_p (ei
))
877 post_order
[post_order_num
++] = bb
->index
;
879 if (!ei_one_before_end_p (ei
))
880 ei_next (&stack
[sp
- 1]);
886 /* Detect any infinite loop and activate the kludge.
887 Note that this doesn't check EXIT_BLOCK itself
888 since EXIT_BLOCK is always added after the outer do-while loop. */
889 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
890 if (!TEST_BIT (visited
, bb
->index
))
892 has_unvisited_bb
= true;
894 if (EDGE_COUNT (bb
->preds
) > 0)
898 basic_block visited_pred
= NULL
;
900 /* Find an already visited predecessor. */
901 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
903 if (TEST_BIT (visited
, e
->src
->index
))
904 visited_pred
= e
->src
;
909 basic_block be
= dfs_find_deadend (bb
);
910 gcc_assert (be
!= NULL
);
911 SET_BIT (visited
, be
->index
);
912 stack
[sp
++] = ei_start (be
->preds
);
918 if (has_unvisited_bb
&& sp
== 0)
920 /* No blocks are reachable from EXIT at all.
921 Find a dead-end from the ENTRY, and restart the iteration. */
922 basic_block be
= dfs_find_deadend (ENTRY_BLOCK_PTR
);
923 gcc_assert (be
!= NULL
);
924 SET_BIT (visited
, be
->index
);
925 stack
[sp
++] = ei_start (be
->preds
);
928 /* The only case the below while fires is
929 when there's an infinite loop. */
933 /* EXIT_BLOCK is always included. */
934 post_order
[post_order_num
++] = EXIT_BLOCK
;
937 sbitmap_free (visited
);
938 return post_order_num
;
941 /* Compute the depth first search order and store in the array
942 PRE_ORDER if nonzero, marking the nodes visited in VISITED. If
943 REV_POST_ORDER is nonzero, return the reverse completion number for each
944 node. Returns the number of nodes visited. A depth first search
945 tries to get as far away from the starting point as quickly as
948 pre_order is a really a preorder numbering of the graph.
949 rev_post_order is really a reverse postorder numbering of the graph.
953 pre_and_rev_post_order_compute (int *pre_order
, int *rev_post_order
,
954 bool include_entry_exit
)
956 edge_iterator
*stack
;
958 int pre_order_num
= 0;
959 int rev_post_order_num
= n_basic_blocks
- 1;
962 /* Allocate stack for back-tracking up CFG. */
963 stack
= XNEWVEC (edge_iterator
, n_basic_blocks
+ 1);
966 if (include_entry_exit
)
969 pre_order
[pre_order_num
] = ENTRY_BLOCK
;
972 rev_post_order
[rev_post_order_num
--] = ENTRY_BLOCK
;
975 rev_post_order_num
-= NUM_FIXED_BLOCKS
;
977 /* Allocate bitmap to track nodes that have been visited. */
978 visited
= sbitmap_alloc (last_basic_block
);
980 /* None of the nodes in the CFG have been visited yet. */
981 sbitmap_zero (visited
);
983 /* Push the first edge on to the stack. */
984 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR
->succs
);
992 /* Look at the edge on the top of the stack. */
994 src
= ei_edge (ei
)->src
;
995 dest
= ei_edge (ei
)->dest
;
997 /* Check if the edge destination has been visited yet. */
998 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
1000 /* Mark that we have visited the destination. */
1001 SET_BIT (visited
, dest
->index
);
1004 pre_order
[pre_order_num
] = dest
->index
;
1008 if (EDGE_COUNT (dest
->succs
) > 0)
1009 /* Since the DEST node has been visited for the first
1010 time, check its successors. */
1011 stack
[sp
++] = ei_start (dest
->succs
);
1012 else if (rev_post_order
)
1013 /* There are no successors for the DEST node so assign
1014 its reverse completion number. */
1015 rev_post_order
[rev_post_order_num
--] = dest
->index
;
1019 if (ei_one_before_end_p (ei
) && src
!= ENTRY_BLOCK_PTR
1021 /* There are no more successors for the SRC node
1022 so assign its reverse completion number. */
1023 rev_post_order
[rev_post_order_num
--] = src
->index
;
1025 if (!ei_one_before_end_p (ei
))
1026 ei_next (&stack
[sp
- 1]);
1033 sbitmap_free (visited
);
1035 if (include_entry_exit
)
1038 pre_order
[pre_order_num
] = EXIT_BLOCK
;
1041 rev_post_order
[rev_post_order_num
--] = EXIT_BLOCK
;
1042 /* The number of nodes visited should be the number of blocks. */
1043 gcc_assert (pre_order_num
== n_basic_blocks
);
1046 /* The number of nodes visited should be the number of blocks minus
1047 the entry and exit blocks which are not visited here. */
1048 gcc_assert (pre_order_num
== n_basic_blocks
- NUM_FIXED_BLOCKS
);
1050 return pre_order_num
;
1053 /* Compute the depth first search order on the _reverse_ graph and
1054 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1055 Returns the number of nodes visited.
1057 The computation is split into three pieces:
1059 flow_dfs_compute_reverse_init () creates the necessary data
1062 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1063 structures. The block will start the search.
1065 flow_dfs_compute_reverse_execute () continues (or starts) the
1066 search using the block on the top of the stack, stopping when the
1069 flow_dfs_compute_reverse_finish () destroys the necessary data
1072 Thus, the user will probably call ..._init(), call ..._add_bb() to
1073 add a beginning basic block to the stack, call ..._execute(),
1074 possibly add another bb to the stack and again call ..._execute(),
1075 ..., and finally call _finish(). */
1077 /* Initialize the data structures used for depth-first search on the
1078 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1079 added to the basic block stack. DATA is the current depth-first
1080 search context. If INITIALIZE_STACK is nonzero, there is an
1081 element on the stack. */
1084 flow_dfs_compute_reverse_init (depth_first_search_ds data
)
1086 /* Allocate stack for back-tracking up CFG. */
1087 data
->stack
= XNEWVEC (basic_block
, n_basic_blocks
);
1090 /* Allocate bitmap to track nodes that have been visited. */
1091 data
->visited_blocks
= sbitmap_alloc (last_basic_block
);
1093 /* None of the nodes in the CFG have been visited yet. */
1094 sbitmap_zero (data
->visited_blocks
);
1099 /* Add the specified basic block to the top of the dfs data
1100 structures. When the search continues, it will start at the
1104 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data
, basic_block bb
)
1106 data
->stack
[data
->sp
++] = bb
;
1107 SET_BIT (data
->visited_blocks
, bb
->index
);
1110 /* Continue the depth-first search through the reverse graph starting with the
1111 block at the stack's top and ending when the stack is empty. Visited nodes
1112 are marked. Returns an unvisited basic block, or NULL if there is none
1116 flow_dfs_compute_reverse_execute (depth_first_search_ds data
,
1117 basic_block last_unvisited
)
1123 while (data
->sp
> 0)
1125 bb
= data
->stack
[--data
->sp
];
1127 /* Perform depth-first search on adjacent vertices. */
1128 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1129 if (!TEST_BIT (data
->visited_blocks
, e
->src
->index
))
1130 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
1133 /* Determine if there are unvisited basic blocks. */
1134 FOR_BB_BETWEEN (bb
, last_unvisited
, NULL
, prev_bb
)
1135 if (!TEST_BIT (data
->visited_blocks
, bb
->index
))
1141 /* Destroy the data structures needed for depth-first search on the
1145 flow_dfs_compute_reverse_finish (depth_first_search_ds data
)
1148 sbitmap_free (data
->visited_blocks
);
1151 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1152 if REVERSE, go against direction of edges. Returns number of blocks
1153 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
1155 dfs_enumerate_from (basic_block bb
, int reverse
,
1156 bool (*predicate
) (const_basic_block
, const void *),
1157 basic_block
*rslt
, int rslt_max
, const void *data
)
1159 basic_block
*st
, lbb
;
1163 /* A bitmap to keep track of visited blocks. Allocating it each time
1164 this function is called is not possible, since dfs_enumerate_from
1165 is often used on small (almost) disjoint parts of cfg (bodies of
1166 loops), and allocating a large sbitmap would lead to quadratic
1168 static sbitmap visited
;
1169 static unsigned v_size
;
1171 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
1172 #define UNMARK_VISITED(BB) (RESET_BIT (visited, (BB)->index))
1173 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
1175 /* Resize the VISITED sbitmap if necessary. */
1176 size
= last_basic_block
;
1183 visited
= sbitmap_alloc (size
);
1184 sbitmap_zero (visited
);
1187 else if (v_size
< size
)
1189 /* Ensure that we increase the size of the sbitmap exponentially. */
1190 if (2 * v_size
> size
)
1193 visited
= sbitmap_resize (visited
, size
, 0);
1197 st
= XCNEWVEC (basic_block
, rslt_max
);
1198 rslt
[tv
++] = st
[sp
++] = bb
;
1207 FOR_EACH_EDGE (e
, ei
, lbb
->preds
)
1208 if (!VISITED_P (e
->src
) && predicate (e
->src
, data
))
1210 gcc_assert (tv
!= rslt_max
);
1211 rslt
[tv
++] = st
[sp
++] = e
->src
;
1212 MARK_VISITED (e
->src
);
1217 FOR_EACH_EDGE (e
, ei
, lbb
->succs
)
1218 if (!VISITED_P (e
->dest
) && predicate (e
->dest
, data
))
1220 gcc_assert (tv
!= rslt_max
);
1221 rslt
[tv
++] = st
[sp
++] = e
->dest
;
1222 MARK_VISITED (e
->dest
);
1227 for (sp
= 0; sp
< tv
; sp
++)
1228 UNMARK_VISITED (rslt
[sp
]);
1231 #undef UNMARK_VISITED
1236 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1238 This algorithm can be found in Timothy Harvey's PhD thesis, at
1239 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1240 dominance algorithms.
1242 First, we identify each join point, j (any node with more than one
1243 incoming edge is a join point).
1245 We then examine each predecessor, p, of j and walk up the dominator tree
1248 We stop the walk when we reach j's immediate dominator - j is in the
1249 dominance frontier of each of the nodes in the walk, except for j's
1250 immediate dominator. Intuitively, all of the rest of j's dominators are
1251 shared by j's predecessors as well.
1252 Since they dominate j, they will not have j in their dominance frontiers.
1254 The number of nodes touched by this algorithm is equal to the size
1255 of the dominance frontiers, no more, no less.
1260 compute_dominance_frontiers_1 (bitmap_head
*frontiers
)
1267 if (EDGE_COUNT (b
->preds
) >= 2)
1269 FOR_EACH_EDGE (p
, ei
, b
->preds
)
1271 basic_block runner
= p
->src
;
1273 if (runner
== ENTRY_BLOCK_PTR
)
1276 domsb
= get_immediate_dominator (CDI_DOMINATORS
, b
);
1277 while (runner
!= domsb
)
1279 if (!bitmap_set_bit (&frontiers
[runner
->index
],
1282 runner
= get_immediate_dominator (CDI_DOMINATORS
,
1292 compute_dominance_frontiers (bitmap_head
*frontiers
)
1294 timevar_push (TV_DOM_FRONTIERS
);
1296 compute_dominance_frontiers_1 (frontiers
);
1298 timevar_pop (TV_DOM_FRONTIERS
);
1301 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1302 return a bitmap with all the blocks in the iterated dominance
1303 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
1304 frontier information as returned by compute_dominance_frontiers.
1306 The resulting set of blocks are the potential sites where PHI nodes
1307 are needed. The caller is responsible for freeing the memory
1308 allocated for the return value. */
1311 compute_idf (bitmap def_blocks
, bitmap_head
*dfs
)
1314 unsigned bb_index
, i
;
1315 VEC(int,heap
) *work_stack
;
1316 bitmap phi_insertion_points
;
1318 work_stack
= VEC_alloc (int, heap
, n_basic_blocks
);
1319 phi_insertion_points
= BITMAP_ALLOC (NULL
);
1321 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1322 VEC_quick_push here for speed. This is safe because we know that
1323 the number of definition blocks is no greater than the number of
1324 basic blocks, which is the initial capacity of WORK_STACK. */
1325 EXECUTE_IF_SET_IN_BITMAP (def_blocks
, 0, bb_index
, bi
)
1326 VEC_quick_push (int, work_stack
, bb_index
);
1328 /* Pop a block off the worklist, add every block that appears in
1329 the original block's DF that we have not already processed to
1330 the worklist. Iterate until the worklist is empty. Blocks
1331 which are added to the worklist are potential sites for
1333 while (VEC_length (int, work_stack
) > 0)
1335 bb_index
= VEC_pop (int, work_stack
);
1337 /* Since the registration of NEW -> OLD name mappings is done
1338 separately from the call to update_ssa, when updating the SSA
1339 form, the basic blocks where new and/or old names are defined
1340 may have disappeared by CFG cleanup calls. In this case,
1341 we may pull a non-existing block from the work stack. */
1342 gcc_assert (bb_index
< (unsigned) last_basic_block
);
1344 EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs
[bb_index
], phi_insertion_points
,
1347 /* Use a safe push because if there is a definition of VAR
1348 in every basic block, then WORK_STACK may eventually have
1349 more than N_BASIC_BLOCK entries. */
1350 VEC_safe_push (int, heap
, work_stack
, i
);
1351 bitmap_set_bit (phi_insertion_points
, i
);
1355 VEC_free (int, heap
, work_stack
);
1357 return phi_insertion_points
;