1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains various simple utilities to analyze the CFG. */
24 #include "coretypes.h"
25 #include "basic-block.h"
31 /* Store the data structures necessary for depth-first search. */
32 struct depth_first_search_dsS
{
33 /* stack for backtracking during the algorithm */
36 /* number of edges in the stack. That is, positions 0, ..., sp-1
40 /* record of basic blocks already seen by depth-first search */
41 sbitmap visited_blocks
;
43 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
45 static void flow_dfs_compute_reverse_init (depth_first_search_ds
);
46 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds
,
48 static basic_block
flow_dfs_compute_reverse_execute (depth_first_search_ds
,
50 static void flow_dfs_compute_reverse_finish (depth_first_search_ds
);
52 /* Mark the back edges in DFS traversal.
53 Return nonzero if a loop (natural or otherwise) is present.
54 Inspired by Depth_First_Search_PP described in:
56 Advanced Compiler Design and Implementation
60 and heavily borrowed from pre_and_rev_post_order_compute. */
63 mark_dfs_back_edges (void)
74 /* Allocate the preorder and postorder number arrays. */
75 pre
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
76 post
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
78 /* Allocate stack for back-tracking up CFG. */
79 stack
= XNEWVEC (edge_iterator
, n_basic_blocks_for_fn (cfun
) + 1);
82 /* Allocate bitmap to track nodes that have been visited. */
83 visited
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
85 /* None of the nodes in the CFG have been visited yet. */
86 bitmap_clear (visited
);
88 /* Push the first edge on to the stack. */
89 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->succs
);
97 /* Look at the edge on the top of the stack. */
99 src
= ei_edge (ei
)->src
;
100 dest
= ei_edge (ei
)->dest
;
101 ei_edge (ei
)->flags
&= ~EDGE_DFS_BACK
;
103 /* Check if the edge destination has been visited yet. */
104 if (dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
) && ! bitmap_bit_p (visited
,
107 /* Mark that we have visited the destination. */
108 bitmap_set_bit (visited
, dest
->index
);
110 pre
[dest
->index
] = prenum
++;
111 if (EDGE_COUNT (dest
->succs
) > 0)
113 /* Since the DEST node has been visited for the first
114 time, check its successors. */
115 stack
[sp
++] = ei_start (dest
->succs
);
118 post
[dest
->index
] = postnum
++;
122 if (dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
123 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
124 && pre
[src
->index
] >= pre
[dest
->index
]
125 && post
[dest
->index
] == 0)
126 ei_edge (ei
)->flags
|= EDGE_DFS_BACK
, found
= true;
128 if (ei_one_before_end_p (ei
)
129 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
130 post
[src
->index
] = postnum
++;
132 if (!ei_one_before_end_p (ei
))
133 ei_next (&stack
[sp
- 1]);
142 sbitmap_free (visited
);
147 /* Find unreachable blocks. An unreachable block will have 0 in
148 the reachable bit in block->flags. A nonzero value indicates the
149 block is reachable. */
152 find_unreachable_blocks (void)
156 basic_block
*tos
, *worklist
, bb
;
158 tos
= worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
160 /* Clear all the reachability flags. */
162 FOR_EACH_BB_FN (bb
, cfun
)
163 bb
->flags
&= ~BB_REACHABLE
;
165 /* Add our starting points to the worklist. Almost always there will
166 be only one. It isn't inconceivable that we might one day directly
167 support Fortran alternate entry points. */
169 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->succs
)
173 /* Mark the block reachable. */
174 e
->dest
->flags
|= BB_REACHABLE
;
177 /* Iterate: find everything reachable from what we've already seen. */
179 while (tos
!= worklist
)
181 basic_block b
= *--tos
;
183 FOR_EACH_EDGE (e
, ei
, b
->succs
)
185 basic_block dest
= e
->dest
;
187 if (!(dest
->flags
& BB_REACHABLE
))
190 dest
->flags
|= BB_REACHABLE
;
198 /* Functions to access an edge list with a vector representation.
199 Enough data is kept such that given an index number, the
200 pred and succ that edge represents can be determined, or
201 given a pred and a succ, its index number can be returned.
202 This allows algorithms which consume a lot of memory to
203 represent the normally full matrix of edge (pred,succ) with a
204 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
205 wasted space in the client code due to sparse flow graphs. */
207 /* This functions initializes the edge list. Basically the entire
208 flowgraph is processed, and all edges are assigned a number,
209 and the data structure is filled in. */
212 create_edge_list (void)
214 struct edge_list
*elist
;
220 /* Determine the number of edges in the flow graph by counting successor
221 edges on each basic block. */
223 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
),
224 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
226 num_edges
+= EDGE_COUNT (bb
->succs
);
229 elist
= XNEW (struct edge_list
);
230 elist
->num_edges
= num_edges
;
231 elist
->index_to_edge
= XNEWVEC (edge
, num_edges
);
235 /* Follow successors of blocks, and register these edges. */
236 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
),
237 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
238 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
239 elist
->index_to_edge
[num_edges
++] = e
;
244 /* This function free's memory associated with an edge list. */
247 free_edge_list (struct edge_list
*elist
)
251 free (elist
->index_to_edge
);
256 /* This function provides debug output showing an edge list. */
259 print_edge_list (FILE *f
, struct edge_list
*elist
)
263 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
264 n_basic_blocks_for_fn (cfun
), elist
->num_edges
);
266 for (x
= 0; x
< elist
->num_edges
; x
++)
268 fprintf (f
, " %-4d - edge(", x
);
269 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
270 fprintf (f
, "entry,");
272 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
274 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR_FOR_FN (cfun
))
275 fprintf (f
, "exit)\n");
277 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
281 /* This function provides an internal consistency check of an edge list,
282 verifying that all edges are present, and that there are no
286 verify_edge_list (FILE *f
, struct edge_list
*elist
)
288 int pred
, succ
, index
;
290 basic_block bb
, p
, s
;
293 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
),
294 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
296 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
298 pred
= e
->src
->index
;
299 succ
= e
->dest
->index
;
300 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
301 if (index
== EDGE_INDEX_NO_EDGE
)
303 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
307 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
308 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
309 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
310 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
311 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
312 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
316 /* We've verified that all the edges are in the list, now lets make sure
317 there are no spurious edges in the list. This is an expensive check! */
319 FOR_BB_BETWEEN (p
, ENTRY_BLOCK_PTR_FOR_FN (cfun
),
320 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
321 FOR_BB_BETWEEN (s
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
, NULL
, next_bb
)
325 FOR_EACH_EDGE (e
, ei
, p
->succs
)
332 FOR_EACH_EDGE (e
, ei
, s
->preds
)
339 if (EDGE_INDEX (elist
, p
, s
)
340 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
341 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
343 if (EDGE_INDEX (elist
, p
, s
)
344 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
345 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
346 p
->index
, s
->index
, EDGE_INDEX (elist
, p
, s
));
351 /* Functions to compute control dependences. */
353 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
355 control_dependences::set_control_dependence_map_bit (basic_block bb
,
358 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
360 gcc_assert (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
361 bitmap_set_bit (control_dependence_map
[bb
->index
], edge_index
);
364 /* Clear all control dependences for block BB. */
366 control_dependences::clear_control_dependence_bitmap (basic_block bb
)
368 bitmap_clear (control_dependence_map
[bb
->index
]);
371 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
372 This function is necessary because some blocks have negative numbers. */
374 static inline basic_block
375 find_pdom (basic_block block
)
377 gcc_assert (block
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
));
379 if (block
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
380 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
383 basic_block bb
= get_immediate_dominator (CDI_POST_DOMINATORS
, block
);
385 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
390 /* Determine all blocks' control dependences on the given edge with edge_list
391 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
394 control_dependences::find_control_dependence (int edge_index
)
396 basic_block current_block
;
397 basic_block ending_block
;
399 gcc_assert (INDEX_EDGE_PRED_BB (m_el
, edge_index
)
400 != EXIT_BLOCK_PTR_FOR_FN (cfun
));
402 if (INDEX_EDGE_PRED_BB (m_el
, edge_index
) == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
403 ending_block
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
405 ending_block
= find_pdom (INDEX_EDGE_PRED_BB (m_el
, edge_index
));
407 for (current_block
= INDEX_EDGE_SUCC_BB (m_el
, edge_index
);
408 current_block
!= ending_block
409 && current_block
!= EXIT_BLOCK_PTR_FOR_FN (cfun
);
410 current_block
= find_pdom (current_block
))
412 edge e
= INDEX_EDGE (m_el
, edge_index
);
414 /* For abnormal edges, we don't make current_block control
415 dependent because instructions that throw are always necessary
417 if (e
->flags
& EDGE_ABNORMAL
)
420 set_control_dependence_map_bit (current_block
, edge_index
);
424 /* Record all blocks' control dependences on all edges in the edge
425 list EL, ala Morgan, Section 3.6. */
427 control_dependences::control_dependences (struct edge_list
*edges
)
430 timevar_push (TV_CONTROL_DEPENDENCES
);
431 control_dependence_map
.create (last_basic_block_for_fn (cfun
));
432 for (int i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
433 control_dependence_map
.quick_push (BITMAP_ALLOC (NULL
));
434 for (int i
= 0; i
< NUM_EDGES (m_el
); ++i
)
435 find_control_dependence (i
);
436 timevar_pop (TV_CONTROL_DEPENDENCES
);
439 /* Free control dependences and the associated edge list. */
441 control_dependences::~control_dependences ()
443 for (unsigned i
= 0; i
< control_dependence_map
.length (); ++i
)
444 BITMAP_FREE (control_dependence_map
[i
]);
445 control_dependence_map
.release ();
446 free_edge_list (m_el
);
449 /* Returns the bitmap of edges the basic-block I is dependent on. */
452 control_dependences::get_edges_dependent_on (int i
)
454 return control_dependence_map
[i
];
457 /* Returns the edge with index I from the edge list. */
460 control_dependences::get_edge (int i
)
462 return INDEX_EDGE (m_el
, i
);
466 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
467 If no such edge exists, return NULL. */
470 find_edge (basic_block pred
, basic_block succ
)
475 if (EDGE_COUNT (pred
->succs
) <= EDGE_COUNT (succ
->preds
))
477 FOR_EACH_EDGE (e
, ei
, pred
->succs
)
483 FOR_EACH_EDGE (e
, ei
, succ
->preds
)
491 /* This routine will determine what, if any, edge there is between
492 a specified predecessor and successor. */
495 find_edge_index (struct edge_list
*edge_list
, basic_block pred
, basic_block succ
)
499 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
500 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
501 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
504 return (EDGE_INDEX_NO_EDGE
);
507 /* This routine will remove any fake predecessor edges for a basic block.
508 When the edge is removed, it is also removed from whatever successor
512 remove_fake_predecessors (basic_block bb
)
517 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
519 if ((e
->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_FOR_FN (cfun
)->next_bb
, NULL
, next_bb
)
536 remove_fake_predecessors (bb
);
539 /* This routine will remove all fake edges to the EXIT_BLOCK. */
542 remove_fake_exit_edges (void)
544 remove_fake_predecessors (EXIT_BLOCK_PTR_FOR_FN (cfun
));
548 /* This function will add a fake edge between any block which has no
549 successors, and the exit block. Some data flow equations require these
553 add_noreturn_fake_exit_edges (void)
557 FOR_EACH_BB_FN (bb
, cfun
)
558 if (EDGE_COUNT (bb
->succs
) == 0)
559 make_single_succ_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), EDGE_FAKE
);
562 /* This function adds a fake edge between any infinite loops to the
563 exit block. Some optimizations require a path from each node to
566 See also Morgan, Figure 3.10, pp. 82-83.
568 The current implementation is ugly, not attempting to minimize the
569 number of inserted fake edges. To reduce the number of fake edges
570 to insert, add fake edges from _innermost_ loops containing only
571 nodes not reachable from the exit block. */
574 connect_infinite_loops_to_exit (void)
576 basic_block unvisited_block
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
577 basic_block deadend_block
;
578 struct depth_first_search_dsS dfs_ds
;
580 /* Perform depth-first search in the reverse graph to find nodes
581 reachable from the exit block. */
582 flow_dfs_compute_reverse_init (&dfs_ds
);
583 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR_FOR_FN (cfun
));
585 /* Repeatedly add fake edges, updating the unreachable nodes. */
588 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
,
590 if (!unvisited_block
)
593 deadend_block
= dfs_find_deadend (unvisited_block
);
594 make_edge (deadend_block
, EXIT_BLOCK_PTR_FOR_FN (cfun
), EDGE_FAKE
);
595 flow_dfs_compute_reverse_add_bb (&dfs_ds
, deadend_block
);
598 flow_dfs_compute_reverse_finish (&dfs_ds
);
602 /* Compute reverse top sort order. This is computing a post order
603 numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then
604 ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
605 true, unreachable blocks are deleted. */
608 post_order_compute (int *post_order
, bool include_entry_exit
,
609 bool delete_unreachable
)
611 edge_iterator
*stack
;
613 int post_order_num
= 0;
617 if (include_entry_exit
)
618 post_order
[post_order_num
++] = EXIT_BLOCK
;
620 /* Allocate stack for back-tracking up CFG. */
621 stack
= XNEWVEC (edge_iterator
, n_basic_blocks_for_fn (cfun
) + 1);
624 /* Allocate bitmap to track nodes that have been visited. */
625 visited
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
627 /* None of the nodes in the CFG have been visited yet. */
628 bitmap_clear (visited
);
630 /* Push the first edge on to the stack. */
631 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->succs
);
639 /* Look at the edge on the top of the stack. */
641 src
= ei_edge (ei
)->src
;
642 dest
= ei_edge (ei
)->dest
;
644 /* Check if the edge destination has been visited yet. */
645 if (dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
646 && ! bitmap_bit_p (visited
, dest
->index
))
648 /* Mark that we have visited the destination. */
649 bitmap_set_bit (visited
, dest
->index
);
651 if (EDGE_COUNT (dest
->succs
) > 0)
652 /* Since the DEST node has been visited for the first
653 time, check its successors. */
654 stack
[sp
++] = ei_start (dest
->succs
);
656 post_order
[post_order_num
++] = dest
->index
;
660 if (ei_one_before_end_p (ei
)
661 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
662 post_order
[post_order_num
++] = src
->index
;
664 if (!ei_one_before_end_p (ei
))
665 ei_next (&stack
[sp
- 1]);
671 if (include_entry_exit
)
673 post_order
[post_order_num
++] = ENTRY_BLOCK
;
674 count
= post_order_num
;
677 count
= post_order_num
+ 2;
679 /* Delete the unreachable blocks if some were found and we are
680 supposed to do it. */
681 if (delete_unreachable
&& (count
!= n_basic_blocks_for_fn (cfun
)))
685 for (b
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
; b
686 != EXIT_BLOCK_PTR_FOR_FN (cfun
); b
= next_bb
)
688 next_bb
= b
->next_bb
;
690 if (!(bitmap_bit_p (visited
, b
->index
)))
691 delete_basic_block (b
);
694 tidy_fallthru_edges ();
698 sbitmap_free (visited
);
699 return post_order_num
;
703 /* Helper routine for inverted_post_order_compute
704 flow_dfs_compute_reverse_execute, and the reverse-CFG
705 deapth first search in dominance.c.
706 BB has to belong to a region of CFG
707 unreachable by inverted traversal from the exit.
708 i.e. there's no control flow path from ENTRY to EXIT
709 that contains this BB.
710 This can happen in two cases - if there's an infinite loop
711 or if there's a block that has no successor
712 (call to a function with no return).
713 Some RTL passes deal with this condition by
714 calling connect_infinite_loops_to_exit () and/or
715 add_noreturn_fake_exit_edges ().
716 However, those methods involve modifying the CFG itself
717 which may not be desirable.
718 Hence, we deal with the infinite loop/no return cases
719 by identifying a unique basic block that can reach all blocks
720 in such a region by inverted traversal.
721 This function returns a basic block that guarantees
722 that all blocks in the region are reachable
723 by starting an inverted traversal from the returned block. */
726 dfs_find_deadend (basic_block bb
)
728 bitmap visited
= BITMAP_ALLOC (NULL
);
732 if (EDGE_COUNT (bb
->succs
) == 0
733 || ! bitmap_set_bit (visited
, bb
->index
))
735 BITMAP_FREE (visited
);
739 bb
= EDGE_SUCC (bb
, 0)->dest
;
746 /* Compute the reverse top sort order of the inverted CFG
747 i.e. starting from the exit block and following the edges backward
748 (from successors to predecessors).
749 This ordering can be used for forward dataflow problems among others.
751 This function assumes that all blocks in the CFG are reachable
752 from the ENTRY (but not necessarily from EXIT).
754 If there's an infinite loop,
755 a simple inverted traversal starting from the blocks
756 with no successors can't visit all blocks.
757 To solve this problem, we first do inverted traversal
758 starting from the blocks with no successor.
759 And if there's any block left that's not visited by the regular
760 inverted traversal from EXIT,
761 those blocks are in such problematic region.
762 Among those, we find one block that has
763 any visited predecessor (which is an entry into such a region),
764 and start looking for a "dead end" from that block
765 and do another inverted traversal from that block. */
768 inverted_post_order_compute (int *post_order
)
771 edge_iterator
*stack
;
773 int post_order_num
= 0;
776 /* Allocate stack for back-tracking up CFG. */
777 stack
= XNEWVEC (edge_iterator
, n_basic_blocks_for_fn (cfun
) + 1);
780 /* Allocate bitmap to track nodes that have been visited. */
781 visited
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
783 /* None of the nodes in the CFG have been visited yet. */
784 bitmap_clear (visited
);
786 /* Put all blocks that have no successor into the initial work list. */
787 FOR_ALL_BB_FN (bb
, cfun
)
788 if (EDGE_COUNT (bb
->succs
) == 0)
790 /* Push the initial edge on to the stack. */
791 if (EDGE_COUNT (bb
->preds
) > 0)
793 stack
[sp
++] = ei_start (bb
->preds
);
794 bitmap_set_bit (visited
, bb
->index
);
800 bool has_unvisited_bb
= false;
802 /* The inverted traversal loop. */
808 /* Look at the edge on the top of the stack. */
810 bb
= ei_edge (ei
)->dest
;
811 pred
= ei_edge (ei
)->src
;
813 /* Check if the predecessor has been visited yet. */
814 if (! bitmap_bit_p (visited
, pred
->index
))
816 /* Mark that we have visited the destination. */
817 bitmap_set_bit (visited
, pred
->index
);
819 if (EDGE_COUNT (pred
->preds
) > 0)
820 /* Since the predecessor node has been visited for the first
821 time, check its predecessors. */
822 stack
[sp
++] = ei_start (pred
->preds
);
824 post_order
[post_order_num
++] = pred
->index
;
828 if (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
829 && ei_one_before_end_p (ei
))
830 post_order
[post_order_num
++] = bb
->index
;
832 if (!ei_one_before_end_p (ei
))
833 ei_next (&stack
[sp
- 1]);
839 /* Detect any infinite loop and activate the kludge.
840 Note that this doesn't check EXIT_BLOCK itself
841 since EXIT_BLOCK is always added after the outer do-while loop. */
842 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
),
843 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
844 if (!bitmap_bit_p (visited
, bb
->index
))
846 has_unvisited_bb
= true;
848 if (EDGE_COUNT (bb
->preds
) > 0)
852 basic_block visited_pred
= NULL
;
854 /* Find an already visited predecessor. */
855 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
857 if (bitmap_bit_p (visited
, e
->src
->index
))
858 visited_pred
= e
->src
;
863 basic_block be
= dfs_find_deadend (bb
);
864 gcc_assert (be
!= NULL
);
865 bitmap_set_bit (visited
, be
->index
);
866 stack
[sp
++] = ei_start (be
->preds
);
872 if (has_unvisited_bb
&& sp
== 0)
874 /* No blocks are reachable from EXIT at all.
875 Find a dead-end from the ENTRY, and restart the iteration. */
876 basic_block be
= dfs_find_deadend (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
877 gcc_assert (be
!= NULL
);
878 bitmap_set_bit (visited
, be
->index
);
879 stack
[sp
++] = ei_start (be
->preds
);
882 /* The only case the below while fires is
883 when there's an infinite loop. */
887 /* EXIT_BLOCK is always included. */
888 post_order
[post_order_num
++] = EXIT_BLOCK
;
891 sbitmap_free (visited
);
892 return post_order_num
;
895 /* Compute the depth first search order of FN and store in the array
896 PRE_ORDER if nonzero. If REV_POST_ORDER is nonzero, return the
897 reverse completion number for each node. Returns the number of nodes
898 visited. A depth first search tries to get as far away from the starting
899 point as quickly as possible.
901 In case the function has unreachable blocks the number of nodes
902 visited does not include them.
904 pre_order is a really a preorder numbering of the graph.
905 rev_post_order is really a reverse postorder numbering of the graph. */
908 pre_and_rev_post_order_compute_fn (struct function
*fn
,
909 int *pre_order
, int *rev_post_order
,
910 bool include_entry_exit
)
912 edge_iterator
*stack
;
914 int pre_order_num
= 0;
915 int rev_post_order_num
= n_basic_blocks_for_fn (cfun
) - 1;
918 /* Allocate stack for back-tracking up CFG. */
919 stack
= XNEWVEC (edge_iterator
, n_basic_blocks_for_fn (cfun
) + 1);
922 if (include_entry_exit
)
925 pre_order
[pre_order_num
] = ENTRY_BLOCK
;
928 rev_post_order
[rev_post_order_num
--] = ENTRY_BLOCK
;
931 rev_post_order_num
-= NUM_FIXED_BLOCKS
;
933 /* Allocate bitmap to track nodes that have been visited. */
934 visited
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
936 /* None of the nodes in the CFG have been visited yet. */
937 bitmap_clear (visited
);
939 /* Push the first edge on to the stack. */
940 stack
[sp
++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (fn
)->succs
);
948 /* Look at the edge on the top of the stack. */
950 src
= ei_edge (ei
)->src
;
951 dest
= ei_edge (ei
)->dest
;
953 /* Check if the edge destination has been visited yet. */
954 if (dest
!= EXIT_BLOCK_PTR_FOR_FN (fn
)
955 && ! bitmap_bit_p (visited
, dest
->index
))
957 /* Mark that we have visited the destination. */
958 bitmap_set_bit (visited
, dest
->index
);
961 pre_order
[pre_order_num
] = dest
->index
;
965 if (EDGE_COUNT (dest
->succs
) > 0)
966 /* Since the DEST node has been visited for the first
967 time, check its successors. */
968 stack
[sp
++] = ei_start (dest
->succs
);
969 else if (rev_post_order
)
970 /* There are no successors for the DEST node so assign
971 its reverse completion number. */
972 rev_post_order
[rev_post_order_num
--] = dest
->index
;
976 if (ei_one_before_end_p (ei
)
977 && src
!= ENTRY_BLOCK_PTR_FOR_FN (fn
)
979 /* There are no more successors for the SRC node
980 so assign its reverse completion number. */
981 rev_post_order
[rev_post_order_num
--] = src
->index
;
983 if (!ei_one_before_end_p (ei
))
984 ei_next (&stack
[sp
- 1]);
991 sbitmap_free (visited
);
993 if (include_entry_exit
)
996 pre_order
[pre_order_num
] = EXIT_BLOCK
;
999 rev_post_order
[rev_post_order_num
--] = EXIT_BLOCK
;
1002 return pre_order_num
;
1005 /* Like pre_and_rev_post_order_compute_fn but operating on the
1006 current function and asserting that all nodes were visited. */
1009 pre_and_rev_post_order_compute (int *pre_order
, int *rev_post_order
,
1010 bool include_entry_exit
)
1013 = pre_and_rev_post_order_compute_fn (cfun
, pre_order
, rev_post_order
,
1014 include_entry_exit
);
1015 if (include_entry_exit
)
1016 /* The number of nodes visited should be the number of blocks. */
1017 gcc_assert (pre_order_num
== n_basic_blocks_for_fn (cfun
));
1019 /* The number of nodes visited should be the number of blocks minus
1020 the entry and exit blocks which are not visited here. */
1021 gcc_assert (pre_order_num
1022 == (n_basic_blocks_for_fn (cfun
) - NUM_FIXED_BLOCKS
));
1024 return pre_order_num
;
1027 /* Compute the depth first search order on the _reverse_ graph and
1028 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1029 Returns the number of nodes visited.
1031 The computation is split into three pieces:
1033 flow_dfs_compute_reverse_init () creates the necessary data
1036 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1037 structures. The block will start the search.
1039 flow_dfs_compute_reverse_execute () continues (or starts) the
1040 search using the block on the top of the stack, stopping when the
1043 flow_dfs_compute_reverse_finish () destroys the necessary data
1046 Thus, the user will probably call ..._init(), call ..._add_bb() to
1047 add a beginning basic block to the stack, call ..._execute(),
1048 possibly add another bb to the stack and again call ..._execute(),
1049 ..., and finally call _finish(). */
1051 /* Initialize the data structures used for depth-first search on the
1052 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1053 added to the basic block stack. DATA is the current depth-first
1054 search context. If INITIALIZE_STACK is nonzero, there is an
1055 element on the stack. */
1058 flow_dfs_compute_reverse_init (depth_first_search_ds data
)
1060 /* Allocate stack for back-tracking up CFG. */
1061 data
->stack
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
1064 /* Allocate bitmap to track nodes that have been visited. */
1065 data
->visited_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
1067 /* None of the nodes in the CFG have been visited yet. */
1068 bitmap_clear (data
->visited_blocks
);
1073 /* Add the specified basic block to the top of the dfs data
1074 structures. When the search continues, it will start at the
1078 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data
, basic_block bb
)
1080 data
->stack
[data
->sp
++] = bb
;
1081 bitmap_set_bit (data
->visited_blocks
, bb
->index
);
1084 /* Continue the depth-first search through the reverse graph starting with the
1085 block at the stack's top and ending when the stack is empty. Visited nodes
1086 are marked. Returns an unvisited basic block, or NULL if there is none
1090 flow_dfs_compute_reverse_execute (depth_first_search_ds data
,
1091 basic_block last_unvisited
)
1097 while (data
->sp
> 0)
1099 bb
= data
->stack
[--data
->sp
];
1101 /* Perform depth-first search on adjacent vertices. */
1102 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1103 if (!bitmap_bit_p (data
->visited_blocks
, e
->src
->index
))
1104 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
1107 /* Determine if there are unvisited basic blocks. */
1108 FOR_BB_BETWEEN (bb
, last_unvisited
, NULL
, prev_bb
)
1109 if (!bitmap_bit_p (data
->visited_blocks
, bb
->index
))
1115 /* Destroy the data structures needed for depth-first search on the
1119 flow_dfs_compute_reverse_finish (depth_first_search_ds data
)
1122 sbitmap_free (data
->visited_blocks
);
1125 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1126 if REVERSE, go against direction of edges. Returns number of blocks
1127 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
1129 dfs_enumerate_from (basic_block bb
, int reverse
,
1130 bool (*predicate
) (const_basic_block
, const void *),
1131 basic_block
*rslt
, int rslt_max
, const void *data
)
1133 basic_block
*st
, lbb
;
1137 /* A bitmap to keep track of visited blocks. Allocating it each time
1138 this function is called is not possible, since dfs_enumerate_from
1139 is often used on small (almost) disjoint parts of cfg (bodies of
1140 loops), and allocating a large sbitmap would lead to quadratic
1142 static sbitmap visited
;
1143 static unsigned v_size
;
1145 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1146 #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
1147 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1149 /* Resize the VISITED sbitmap if necessary. */
1150 size
= last_basic_block_for_fn (cfun
);
1157 visited
= sbitmap_alloc (size
);
1158 bitmap_clear (visited
);
1161 else if (v_size
< size
)
1163 /* Ensure that we increase the size of the sbitmap exponentially. */
1164 if (2 * v_size
> size
)
1167 visited
= sbitmap_resize (visited
, size
, 0);
1171 st
= XNEWVEC (basic_block
, rslt_max
);
1172 rslt
[tv
++] = st
[sp
++] = bb
;
1181 FOR_EACH_EDGE (e
, ei
, lbb
->preds
)
1182 if (!VISITED_P (e
->src
) && predicate (e
->src
, data
))
1184 gcc_assert (tv
!= rslt_max
);
1185 rslt
[tv
++] = st
[sp
++] = e
->src
;
1186 MARK_VISITED (e
->src
);
1191 FOR_EACH_EDGE (e
, ei
, lbb
->succs
)
1192 if (!VISITED_P (e
->dest
) && predicate (e
->dest
, data
))
1194 gcc_assert (tv
!= rslt_max
);
1195 rslt
[tv
++] = st
[sp
++] = e
->dest
;
1196 MARK_VISITED (e
->dest
);
1201 for (sp
= 0; sp
< tv
; sp
++)
1202 UNMARK_VISITED (rslt
[sp
]);
1205 #undef UNMARK_VISITED
1210 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1212 This algorithm can be found in Timothy Harvey's PhD thesis, at
1213 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1214 dominance algorithms.
1216 First, we identify each join point, j (any node with more than one
1217 incoming edge is a join point).
1219 We then examine each predecessor, p, of j and walk up the dominator tree
1222 We stop the walk when we reach j's immediate dominator - j is in the
1223 dominance frontier of each of the nodes in the walk, except for j's
1224 immediate dominator. Intuitively, all of the rest of j's dominators are
1225 shared by j's predecessors as well.
1226 Since they dominate j, they will not have j in their dominance frontiers.
1228 The number of nodes touched by this algorithm is equal to the size
1229 of the dominance frontiers, no more, no less.
1234 compute_dominance_frontiers_1 (bitmap_head
*frontiers
)
1239 FOR_EACH_BB_FN (b
, cfun
)
1241 if (EDGE_COUNT (b
->preds
) >= 2)
1243 FOR_EACH_EDGE (p
, ei
, b
->preds
)
1245 basic_block runner
= p
->src
;
1247 if (runner
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1250 domsb
= get_immediate_dominator (CDI_DOMINATORS
, b
);
1251 while (runner
!= domsb
)
1253 if (!bitmap_set_bit (&frontiers
[runner
->index
],
1256 runner
= get_immediate_dominator (CDI_DOMINATORS
,
1266 compute_dominance_frontiers (bitmap_head
*frontiers
)
1268 timevar_push (TV_DOM_FRONTIERS
);
1270 compute_dominance_frontiers_1 (frontiers
);
1272 timevar_pop (TV_DOM_FRONTIERS
);
1275 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1276 return a bitmap with all the blocks in the iterated dominance
1277 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
1278 frontier information as returned by compute_dominance_frontiers.
1280 The resulting set of blocks are the potential sites where PHI nodes
1281 are needed. The caller is responsible for freeing the memory
1282 allocated for the return value. */
1285 compute_idf (bitmap def_blocks
, bitmap_head
*dfs
)
1288 unsigned bb_index
, i
;
1289 bitmap phi_insertion_points
;
1291 /* Each block can appear at most twice on the work-stack. */
1292 auto_vec
<int> work_stack (2 * n_basic_blocks_for_fn (cfun
));
1293 phi_insertion_points
= BITMAP_ALLOC (NULL
);
1295 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1296 vec::quick_push here for speed. This is safe because we know that
1297 the number of definition blocks is no greater than the number of
1298 basic blocks, which is the initial capacity of WORK_STACK. */
1299 EXECUTE_IF_SET_IN_BITMAP (def_blocks
, 0, bb_index
, bi
)
1300 work_stack
.quick_push (bb_index
);
1302 /* Pop a block off the worklist, add every block that appears in
1303 the original block's DF that we have not already processed to
1304 the worklist. Iterate until the worklist is empty. Blocks
1305 which are added to the worklist are potential sites for
1307 while (work_stack
.length () > 0)
1309 bb_index
= work_stack
.pop ();
1311 /* Since the registration of NEW -> OLD name mappings is done
1312 separately from the call to update_ssa, when updating the SSA
1313 form, the basic blocks where new and/or old names are defined
1314 may have disappeared by CFG cleanup calls. In this case,
1315 we may pull a non-existing block from the work stack. */
1316 gcc_checking_assert (bb_index
1317 < (unsigned) last_basic_block_for_fn (cfun
));
1319 EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs
[bb_index
], phi_insertion_points
,
1322 work_stack
.quick_push (i
);
1323 bitmap_set_bit (phi_insertion_points
, i
);
1327 return phi_insertion_points
;
1330 /* Intersection and union of preds/succs for sbitmap based data flow
1331 solvers. All four functions defined below take the same arguments:
1332 B is the basic block to perform the operation for. DST is the
1333 target sbitmap, i.e. the result. SRC is an sbitmap vector of size
1334 last_basic_block so that it can be indexed with basic block indices.
1335 DST may be (but does not have to be) SRC[B->index]. */
1337 /* Set the bitmap DST to the intersection of SRC of successors of
1341 bitmap_intersection_of_succs (sbitmap dst
, sbitmap
*src
, basic_block b
)
1343 unsigned int set_size
= dst
->size
;
1347 gcc_assert (!dst
->popcount
);
1349 for (e
= NULL
, ix
= 0; ix
< EDGE_COUNT (b
->succs
); ix
++)
1351 e
= EDGE_SUCC (b
, ix
);
1352 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1355 bitmap_copy (dst
, src
[e
->dest
->index
]);
1362 for (++ix
; ix
< EDGE_COUNT (b
->succs
); ix
++)
1365 SBITMAP_ELT_TYPE
*p
, *r
;
1367 e
= EDGE_SUCC (b
, ix
);
1368 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1371 p
= src
[e
->dest
->index
]->elms
;
1373 for (i
= 0; i
< set_size
; i
++)
1378 /* Set the bitmap DST to the intersection of SRC of predecessors of
1382 bitmap_intersection_of_preds (sbitmap dst
, sbitmap
*src
, basic_block b
)
1384 unsigned int set_size
= dst
->size
;
1388 gcc_assert (!dst
->popcount
);
1390 for (e
= NULL
, ix
= 0; ix
< EDGE_COUNT (b
->preds
); ix
++)
1392 e
= EDGE_PRED (b
, ix
);
1393 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1396 bitmap_copy (dst
, src
[e
->src
->index
]);
1403 for (++ix
; ix
< EDGE_COUNT (b
->preds
); ix
++)
1406 SBITMAP_ELT_TYPE
*p
, *r
;
1408 e
= EDGE_PRED (b
, ix
);
1409 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1412 p
= src
[e
->src
->index
]->elms
;
1414 for (i
= 0; i
< set_size
; i
++)
1419 /* Set the bitmap DST to the union of SRC of successors of
1423 bitmap_union_of_succs (sbitmap dst
, sbitmap
*src
, basic_block b
)
1425 unsigned int set_size
= dst
->size
;
1429 gcc_assert (!dst
->popcount
);
1431 for (ix
= 0; ix
< EDGE_COUNT (b
->succs
); ix
++)
1433 e
= EDGE_SUCC (b
, ix
);
1434 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1437 bitmap_copy (dst
, src
[e
->dest
->index
]);
1441 if (ix
== EDGE_COUNT (b
->succs
))
1444 for (ix
++; ix
< EDGE_COUNT (b
->succs
); ix
++)
1447 SBITMAP_ELT_TYPE
*p
, *r
;
1449 e
= EDGE_SUCC (b
, ix
);
1450 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1453 p
= src
[e
->dest
->index
]->elms
;
1455 for (i
= 0; i
< set_size
; i
++)
1460 /* Set the bitmap DST to the union of SRC of predecessors of
1464 bitmap_union_of_preds (sbitmap dst
, sbitmap
*src
, basic_block b
)
1466 unsigned int set_size
= dst
->size
;
1470 gcc_assert (!dst
->popcount
);
1472 for (ix
= 0; ix
< EDGE_COUNT (b
->preds
); ix
++)
1474 e
= EDGE_PRED (b
, ix
);
1475 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1478 bitmap_copy (dst
, src
[e
->src
->index
]);
1482 if (ix
== EDGE_COUNT (b
->preds
))
1485 for (ix
++; ix
< EDGE_COUNT (b
->preds
); ix
++)
1488 SBITMAP_ELT_TYPE
*p
, *r
;
1490 e
= EDGE_PRED (b
, ix
);
1491 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1494 p
= src
[e
->src
->index
]->elms
;
1496 for (i
= 0; i
< set_size
; i
++)
1501 /* Returns the list of basic blocks in the function in an order that guarantees
1502 that if a block X has just a single predecessor Y, then Y is after X in the
1506 single_pred_before_succ_order (void)
1509 basic_block
*order
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
1510 unsigned n
= n_basic_blocks_for_fn (cfun
) - NUM_FIXED_BLOCKS
;
1512 sbitmap visited
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
1514 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1515 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1517 bitmap_clear (visited
);
1519 MARK_VISITED (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1520 FOR_EACH_BB_FN (x
, cfun
)
1525 /* Walk the predecessors of x as long as they have precisely one
1526 predecessor and add them to the list, so that they get stored
1529 single_pred_p (y
) && !VISITED_P (single_pred (y
));
1530 y
= single_pred (y
))
1532 for (y
= x
, i
= n
- np
;
1533 single_pred_p (y
) && !VISITED_P (single_pred (y
));
1534 y
= single_pred (y
), i
++)
1542 gcc_assert (i
== n
- 1);
1546 sbitmap_free (visited
);
1547 gcc_assert (n
== 0);