1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file contains various simple utilities to analyze the CFG. */
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "insn-config.h"
34 /* Store the data structures necessary for depth-first search. */
35 struct depth_first_search_dsS {
36 /* stack for backtracking during the algorithm */
39 /* number of edges in the stack. That is, positions 0, ..., sp-1
43 /* record of basic blocks already seen by depth-first search */
44 sbitmap visited_blocks;
46 typedef struct depth_first_search_dsS *depth_first_search_ds;
48 static void flow_dfs_compute_reverse_init
49 PARAMS ((depth_first_search_ds));
50 static void flow_dfs_compute_reverse_add_bb
51 PARAMS ((depth_first_search_ds, basic_block));
52 static basic_block flow_dfs_compute_reverse_execute
53 PARAMS ((depth_first_search_ds));
54 static void flow_dfs_compute_reverse_finish
55 PARAMS ((depth_first_search_ds));
56 static void remove_fake_successors PARAMS ((basic_block));
57 static bool need_fake_edge_p PARAMS ((rtx));
59 /* Return true if the block has no effect and only forwards control flow to
60 its single destination. */
63 forwarder_block_p (bb)
68 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR
69 || !bb->succ || bb->succ->succ_next)
72 for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
73 if (INSN_P (insn) && active_insn_p (insn))
76 return (!INSN_P (insn)
77 || (GET_CODE (insn) == JUMP_INSN && simplejump_p (insn))
78 || !active_insn_p (insn));
81 /* Return nonzero if we can reach target from src by falling through. */
84 can_fallthru (src, target)
85 basic_block src, target;
88 rtx insn2 = target->head;
90 if (src->index + 1 != target->index)
93 if (!active_insn_p (insn2))
94 insn2 = next_active_insn (insn2);
96 /* ??? Later we may add code to move jump tables offline. */
97 return next_active_insn (insn) == insn2;
100 /* Mark the back edges in DFS traversal.
101 Return non-zero if a loop (natural or otherwise) is present.
102 Inspired by Depth_First_Search_PP described in:
104 Advanced Compiler Design and Implementation
106 Morgan Kaufmann, 1997
108 and heavily borrowed from flow_depth_first_order_compute. */
111 mark_dfs_back_edges ()
122 /* Allocate the preorder and postorder number arrays. */
123 pre = (int *) xcalloc (n_basic_blocks, sizeof (int));
124 post = (int *) xcalloc (n_basic_blocks, sizeof (int));
126 /* Allocate stack for back-tracking up CFG. */
127 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
130 /* Allocate bitmap to track nodes that have been visited. */
131 visited = sbitmap_alloc (n_basic_blocks);
133 /* None of the nodes in the CFG have been visited yet. */
134 sbitmap_zero (visited);
136 /* Push the first edge on to the stack. */
137 stack[sp++] = ENTRY_BLOCK_PTR->succ;
145 /* Look at the edge on the top of the stack. */
149 e->flags &= ~EDGE_DFS_BACK;
151 /* Check if the edge destination has been visited yet. */
152 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
154 /* Mark that we have visited the destination. */
155 SET_BIT (visited, dest->index);
157 pre[dest->index] = prenum++;
160 /* Since the DEST node has been visited for the first
161 time, check its successors. */
162 stack[sp++] = dest->succ;
165 post[dest->index] = postnum++;
169 if (dest != EXIT_BLOCK_PTR && src != ENTRY_BLOCK_PTR
170 && pre[src->index] >= pre[dest->index]
171 && post[dest->index] == 0)
172 e->flags |= EDGE_DFS_BACK, found = true;
174 if (! e->succ_next && src != ENTRY_BLOCK_PTR)
175 post[src->index] = postnum++;
178 stack[sp - 1] = e->succ_next;
187 sbitmap_free (visited);
192 /* Return true if we need to add fake edge to exit.
193 Helper function for the flow_call_edges_add. */
196 need_fake_edge_p (insn)
202 if ((GET_CODE (insn) == CALL_INSN
203 && !SIBLING_CALL_P (insn)
204 && !find_reg_note (insn, REG_NORETURN, NULL)
205 && !find_reg_note (insn, REG_ALWAYS_RETURN, NULL)
206 && !CONST_OR_PURE_CALL_P (insn)))
209 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
210 && MEM_VOLATILE_P (PATTERN (insn)))
211 || (GET_CODE (PATTERN (insn)) == PARALLEL
212 && asm_noperands (insn) != -1
213 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
214 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
217 /* Add fake edges to the function exit for any non constant and non noreturn
218 calls, volatile inline assembly in the bitmap of blocks specified by
219 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
222 The goal is to expose cases in which entering a basic block does not imply
223 that all subsequent instructions must be executed. */
226 flow_call_edges_add (blocks)
230 int blocks_split = 0;
233 bool check_last_block = false;
235 /* Map bb indices into basic block pointers since split_block
236 will renumber the basic blocks. */
238 bbs = xmalloc (n_basic_blocks * sizeof (*bbs));
242 for (i = 0; i < n_basic_blocks; i++)
243 bbs[bb_num++] = BASIC_BLOCK (i);
245 check_last_block = true;
248 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
250 bbs[bb_num++] = BASIC_BLOCK (i);
251 if (i == n_basic_blocks - 1)
252 check_last_block = true;
255 /* In the last basic block, before epilogue generation, there will be
256 a fallthru edge to EXIT. Special care is required if the last insn
257 of the last basic block is a call because make_edge folds duplicate
258 edges, which would result in the fallthru edge also being marked
259 fake, which would result in the fallthru edge being removed by
260 remove_fake_edges, which would result in an invalid CFG.
262 Moreover, we can't elide the outgoing fake edge, since the block
263 profiler needs to take this into account in order to solve the minimal
264 spanning tree in the case that the call doesn't return.
266 Handle this by adding a dummy instruction in a new last basic block. */
267 if (check_last_block)
269 basic_block bb = BASIC_BLOCK (n_basic_blocks - 1);
272 /* Back up past insns that must be kept in the same block as a call. */
273 while (insn != bb->head
274 && keep_with_call_p (insn))
275 insn = PREV_INSN (insn);
277 if (need_fake_edge_p (insn))
281 for (e = bb->succ; e; e = e->succ_next)
282 if (e->dest == EXIT_BLOCK_PTR)
285 insert_insn_on_edge (gen_rtx_USE (VOIDmode, const0_rtx), e);
286 commit_edge_insertions ();
290 /* Now add fake edges to the function exit for any non constant
291 calls since there is no way that we can determine if they will
294 for (i = 0; i < bb_num; i++)
296 basic_block bb = bbs[i];
300 for (insn = bb->end; ; insn = prev_insn)
302 prev_insn = PREV_INSN (insn);
303 if (need_fake_edge_p (insn))
306 rtx split_at_insn = insn;
308 /* Don't split the block between a call and an insn that should
309 remain in the same block as the call. */
310 if (GET_CODE (insn) == CALL_INSN)
311 while (split_at_insn != bb->end
312 && keep_with_call_p (NEXT_INSN (split_at_insn)))
313 split_at_insn = NEXT_INSN (split_at_insn);
315 /* The handling above of the final block before the epilogue
316 should be enough to verify that there is no edge to the exit
317 block in CFG already. Calling make_edge in such case would
318 cause us to mark that edge as fake and remove it later. */
320 #ifdef ENABLE_CHECKING
321 if (split_at_insn == bb->end)
322 for (e = bb->succ; e; e = e->succ_next)
323 if (e->dest == EXIT_BLOCK_PTR)
327 /* Note that the following may create a new basic block
328 and renumber the existing basic blocks. */
329 e = split_block (bb, split_at_insn);
333 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
336 if (insn == bb->head)
348 /* Find unreachable blocks. An unreachable block will have 0 in
349 the reachable bit in block->flags. A non-zero value indicates the
350 block is reachable. */
353 find_unreachable_blocks ()
357 basic_block *tos, *worklist;
360 tos = worklist = (basic_block *) xmalloc (sizeof (basic_block) * n);
362 /* Clear all the reachability flags. */
364 for (i = 0; i < n; ++i)
365 BASIC_BLOCK (i)->flags &= ~BB_REACHABLE;
367 /* Add our starting points to the worklist. Almost always there will
368 be only one. It isn't inconceivable that we might one day directly
369 support Fortran alternate entry points. */
371 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
375 /* Mark the block reachable. */
376 e->dest->flags |= BB_REACHABLE;
379 /* Iterate: find everything reachable from what we've already seen. */
381 while (tos != worklist)
383 basic_block b = *--tos;
385 for (e = b->succ; e; e = e->succ_next)
386 if (!(e->dest->flags & BB_REACHABLE))
389 e->dest->flags |= BB_REACHABLE;
396 /* Functions to access an edge list with a vector representation.
397 Enough data is kept such that given an index number, the
398 pred and succ that edge represents can be determined, or
399 given a pred and a succ, its index number can be returned.
400 This allows algorithms which consume a lot of memory to
401 represent the normally full matrix of edge (pred,succ) with a
402 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
403 wasted space in the client code due to sparse flow graphs. */
405 /* This functions initializes the edge list. Basically the entire
406 flowgraph is processed, and all edges are assigned a number,
407 and the data structure is filled in. */
412 struct edge_list *elist;
418 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
422 /* Determine the number of edges in the flow graph by counting successor
423 edges on each basic block. */
424 for (x = 0; x < n_basic_blocks; x++)
426 basic_block bb = BASIC_BLOCK (x);
428 for (e = bb->succ; e; e = e->succ_next)
432 /* Don't forget successors of the entry block. */
433 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
436 elist = (struct edge_list *) xmalloc (sizeof (struct edge_list));
437 elist->num_blocks = block_count;
438 elist->num_edges = num_edges;
439 elist->index_to_edge = (edge *) xmalloc (sizeof (edge) * num_edges);
443 /* Follow successors of the entry block, and register these edges. */
444 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
445 elist->index_to_edge[num_edges++] = e;
447 for (x = 0; x < n_basic_blocks; x++)
449 basic_block bb = BASIC_BLOCK (x);
451 /* Follow all successors of blocks, and register these edges. */
452 for (e = bb->succ; e; e = e->succ_next)
453 elist->index_to_edge[num_edges++] = e;
459 /* This function free's memory associated with an edge list. */
462 free_edge_list (elist)
463 struct edge_list *elist;
467 free (elist->index_to_edge);
472 /* This function provides debug output showing an edge list. */
475 print_edge_list (f, elist)
477 struct edge_list *elist;
481 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
482 elist->num_blocks - 2, elist->num_edges);
484 for (x = 0; x < elist->num_edges; x++)
486 fprintf (f, " %-4d - edge(", x);
487 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
488 fprintf (f, "entry,");
490 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
492 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
493 fprintf (f, "exit)\n");
495 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
499 /* This function provides an internal consistency check of an edge list,
500 verifying that all edges are present, and that there are no
504 verify_edge_list (f, elist)
506 struct edge_list *elist;
508 int x, pred, succ, index;
511 for (x = 0; x < n_basic_blocks; x++)
513 basic_block bb = BASIC_BLOCK (x);
515 for (e = bb->succ; e; e = e->succ_next)
517 pred = e->src->index;
518 succ = e->dest->index;
519 index = EDGE_INDEX (elist, e->src, e->dest);
520 if (index == EDGE_INDEX_NO_EDGE)
522 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
526 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
527 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
528 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
529 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
530 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
531 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
535 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
537 pred = e->src->index;
538 succ = e->dest->index;
539 index = EDGE_INDEX (elist, e->src, e->dest);
540 if (index == EDGE_INDEX_NO_EDGE)
542 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
546 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
547 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
548 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
549 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
550 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
551 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
554 /* We've verified that all the edges are in the list, no lets make sure
555 there are no spurious edges in the list. */
557 for (pred = 0; pred < n_basic_blocks; pred++)
558 for (succ = 0; succ < n_basic_blocks; succ++)
560 basic_block p = BASIC_BLOCK (pred);
561 basic_block s = BASIC_BLOCK (succ);
564 for (e = p->succ; e; e = e->succ_next)
571 for (e = s->pred; e; e = e->pred_next)
578 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
579 == EDGE_INDEX_NO_EDGE && found_edge != 0)
580 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
582 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
583 != EDGE_INDEX_NO_EDGE && found_edge == 0)
584 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
585 pred, succ, EDGE_INDEX (elist, BASIC_BLOCK (pred),
586 BASIC_BLOCK (succ)));
589 for (succ = 0; succ < n_basic_blocks; succ++)
591 basic_block p = ENTRY_BLOCK_PTR;
592 basic_block s = BASIC_BLOCK (succ);
595 for (e = p->succ; e; e = e->succ_next)
602 for (e = s->pred; e; e = e->pred_next)
609 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
610 == EDGE_INDEX_NO_EDGE && found_edge != 0)
611 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
613 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
614 != EDGE_INDEX_NO_EDGE && found_edge == 0)
615 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
616 succ, EDGE_INDEX (elist, ENTRY_BLOCK_PTR,
617 BASIC_BLOCK (succ)));
620 for (pred = 0; pred < n_basic_blocks; pred++)
622 basic_block p = BASIC_BLOCK (pred);
623 basic_block s = EXIT_BLOCK_PTR;
626 for (e = p->succ; e; e = e->succ_next)
633 for (e = s->pred; e; e = e->pred_next)
640 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
641 == EDGE_INDEX_NO_EDGE && found_edge != 0)
642 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
644 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
645 != EDGE_INDEX_NO_EDGE && found_edge == 0)
646 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
647 pred, EDGE_INDEX (elist, BASIC_BLOCK (pred),
652 /* This routine will determine what, if any, edge there is between
653 a specified predecessor and successor. */
656 find_edge_index (edge_list, pred, succ)
657 struct edge_list *edge_list;
658 basic_block pred, succ;
662 for (x = 0; x < NUM_EDGES (edge_list); x++)
663 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
664 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
667 return (EDGE_INDEX_NO_EDGE);
670 /* Dump the list of basic blocks in the bitmap NODES. */
673 flow_nodes_print (str, nodes, file)
683 fprintf (file, "%s { ", str);
684 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {fprintf (file, "%d ", node);});
688 /* Dump the list of edges in the array EDGE_LIST. */
691 flow_edge_list_print (str, edge_list, num_edges, file)
693 const edge *edge_list;
702 fprintf (file, "%s { ", str);
703 for (i = 0; i < num_edges; i++)
704 fprintf (file, "%d->%d ", edge_list[i]->src->index,
705 edge_list[i]->dest->index);
711 /* This routine will remove any fake successor edges for a basic block.
712 When the edge is removed, it is also removed from whatever predecessor
716 remove_fake_successors (bb)
721 for (e = bb->succ; e;)
726 if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
731 /* This routine will remove all fake edges from the flow graph. If
732 we remove all fake successors, it will automatically remove all
733 fake predecessors. */
740 for (x = 0; x < n_basic_blocks; x++)
741 remove_fake_successors (BASIC_BLOCK (x));
743 /* We've handled all successors except the entry block's. */
744 remove_fake_successors (ENTRY_BLOCK_PTR);
747 /* This function will add a fake edge between any block which has no
748 successors, and the exit block. Some data flow equations require these
752 add_noreturn_fake_exit_edges ()
756 for (x = 0; x < n_basic_blocks; x++)
757 if (BASIC_BLOCK (x)->succ == NULL)
758 make_single_succ_edge (BASIC_BLOCK (x), EXIT_BLOCK_PTR, EDGE_FAKE);
761 /* This function adds a fake edge between any infinite loops to the
762 exit block. Some optimizations require a path from each node to
765 See also Morgan, Figure 3.10, pp. 82-83.
767 The current implementation is ugly, not attempting to minimize the
768 number of inserted fake edges. To reduce the number of fake edges
769 to insert, add fake edges from _innermost_ loops containing only
770 nodes not reachable from the exit block. */
773 connect_infinite_loops_to_exit ()
775 basic_block unvisited_block;
776 struct depth_first_search_dsS dfs_ds;
778 /* Perform depth-first search in the reverse graph to find nodes
779 reachable from the exit block. */
780 flow_dfs_compute_reverse_init (&dfs_ds);
781 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
783 /* Repeatedly add fake edges, updating the unreachable nodes. */
786 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds);
787 if (!unvisited_block)
790 make_edge (unvisited_block, EXIT_BLOCK_PTR, EDGE_FAKE);
791 flow_dfs_compute_reverse_add_bb (&dfs_ds, unvisited_block);
794 flow_dfs_compute_reverse_finish (&dfs_ds);
798 /* Compute reverse top sort order */
801 flow_reverse_top_sort_order_compute (rts_order)
809 /* Allocate stack for back-tracking up CFG. */
810 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
813 /* Allocate bitmap to track nodes that have been visited. */
814 visited = sbitmap_alloc (n_basic_blocks);
816 /* None of the nodes in the CFG have been visited yet. */
817 sbitmap_zero (visited);
819 /* Push the first edge on to the stack. */
820 stack[sp++] = ENTRY_BLOCK_PTR->succ;
828 /* Look at the edge on the top of the stack. */
833 /* Check if the edge destination has been visited yet. */
834 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
836 /* Mark that we have visited the destination. */
837 SET_BIT (visited, dest->index);
840 /* Since the DEST node has been visited for the first
841 time, check its successors. */
842 stack[sp++] = dest->succ;
844 rts_order[postnum++] = dest->index;
848 if (! e->succ_next && src != ENTRY_BLOCK_PTR)
849 rts_order[postnum++] = src->index;
852 stack[sp - 1] = e->succ_next;
859 sbitmap_free (visited);
862 /* Compute the depth first search order and store in the array
863 DFS_ORDER if non-zero, marking the nodes visited in VISITED. If
864 RC_ORDER is non-zero, return the reverse completion number for each
865 node. Returns the number of nodes visited. A depth first search
866 tries to get as far away from the starting point as quickly as
870 flow_depth_first_order_compute (dfs_order, rc_order)
877 int rcnum = n_basic_blocks - 1;
880 /* Allocate stack for back-tracking up CFG. */
881 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
884 /* Allocate bitmap to track nodes that have been visited. */
885 visited = sbitmap_alloc (n_basic_blocks);
887 /* None of the nodes in the CFG have been visited yet. */
888 sbitmap_zero (visited);
890 /* Push the first edge on to the stack. */
891 stack[sp++] = ENTRY_BLOCK_PTR->succ;
899 /* Look at the edge on the top of the stack. */
904 /* Check if the edge destination has been visited yet. */
905 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
907 /* Mark that we have visited the destination. */
908 SET_BIT (visited, dest->index);
911 dfs_order[dfsnum] = dest->index;
916 /* Since the DEST node has been visited for the first
917 time, check its successors. */
918 stack[sp++] = dest->succ;
920 /* There are no successors for the DEST node so assign
921 its reverse completion number. */
922 rc_order[rcnum--] = dest->index;
926 if (! e->succ_next && src != ENTRY_BLOCK_PTR
928 /* There are no more successors for the SRC node
929 so assign its reverse completion number. */
930 rc_order[rcnum--] = src->index;
933 stack[sp - 1] = e->succ_next;
940 sbitmap_free (visited);
942 /* The number of nodes visited should not be greater than
944 if (dfsnum > n_basic_blocks)
947 /* There are some nodes left in the CFG that are unreachable. */
948 if (dfsnum < n_basic_blocks)
957 struct dfst_node **node;
958 struct dfst_node *up;
961 /* Compute a preorder transversal ordering such that a sub-tree which
962 is the source of a cross edge appears before the sub-tree which is
963 the destination of the cross edge. This allows for easy detection
964 of all the entry blocks for a loop.
966 The ordering is compute by:
968 1) Generating a depth first spanning tree.
970 2) Walking the resulting tree from right to left. */
973 flow_preorder_transversal_compute (pot_order)
982 struct dfst_node *node;
983 struct dfst_node *dfst;
985 /* Allocate stack for back-tracking up CFG. */
986 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
989 /* Allocate the tree. */
990 dfst = (struct dfst_node *) xcalloc (n_basic_blocks,
991 sizeof (struct dfst_node));
993 for (i = 0; i < n_basic_blocks; i++)
996 for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
1001 ? (struct dfst_node **) xcalloc (max_successors,
1002 sizeof (struct dfst_node *))
1006 /* Allocate bitmap to track nodes that have been visited. */
1007 visited = sbitmap_alloc (n_basic_blocks);
1009 /* None of the nodes in the CFG have been visited yet. */
1010 sbitmap_zero (visited);
1012 /* Push the first edge on to the stack. */
1013 stack[sp++] = ENTRY_BLOCK_PTR->succ;
1020 /* Look at the edge on the top of the stack. */
1025 /* Check if the edge destination has been visited yet. */
1026 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
1028 /* Mark that we have visited the destination. */
1029 SET_BIT (visited, dest->index);
1031 /* Add the destination to the preorder tree. */
1032 if (src != ENTRY_BLOCK_PTR)
1034 dfst[src->index].node[dfst[src->index].nnodes++]
1035 = &dfst[dest->index];
1036 dfst[dest->index].up = &dfst[src->index];
1040 /* Since the DEST node has been visited for the first
1041 time, check its successors. */
1042 stack[sp++] = dest->succ;
1045 else if (e->succ_next)
1046 stack[sp - 1] = e->succ_next;
1052 sbitmap_free (visited);
1054 /* Record the preorder transversal order by
1055 walking the tree from right to left. */
1065 node = node->node[--node->nnodes];
1066 pot_order[i++] = node - dfst;
1072 /* Free the tree. */
1074 for (i = 0; i < n_basic_blocks; i++)
1076 free (dfst[i].node);
1081 /* Compute the depth first search order on the _reverse_ graph and
1082 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1083 Returns the number of nodes visited.
1085 The computation is split into three pieces:
1087 flow_dfs_compute_reverse_init () creates the necessary data
1090 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1091 structures. The block will start the search.
1093 flow_dfs_compute_reverse_execute () continues (or starts) the
1094 search using the block on the top of the stack, stopping when the
1097 flow_dfs_compute_reverse_finish () destroys the necessary data
1100 Thus, the user will probably call ..._init(), call ..._add_bb() to
1101 add a beginning basic block to the stack, call ..._execute(),
1102 possibly add another bb to the stack and again call ..._execute(),
1103 ..., and finally call _finish(). */
1105 /* Initialize the data structures used for depth-first search on the
1106 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1107 added to the basic block stack. DATA is the current depth-first
1108 search context. If INITIALIZE_STACK is non-zero, there is an
1109 element on the stack. */
1112 flow_dfs_compute_reverse_init (data)
1113 depth_first_search_ds data;
1115 /* Allocate stack for back-tracking up CFG. */
1116 data->stack = (basic_block *) xmalloc ((n_basic_blocks - (INVALID_BLOCK + 1))
1117 * sizeof (basic_block));
1120 /* Allocate bitmap to track nodes that have been visited. */
1121 data->visited_blocks = sbitmap_alloc (n_basic_blocks - (INVALID_BLOCK + 1));
1123 /* None of the nodes in the CFG have been visited yet. */
1124 sbitmap_zero (data->visited_blocks);
1129 /* Add the specified basic block to the top of the dfs data
1130 structures. When the search continues, it will start at the
1134 flow_dfs_compute_reverse_add_bb (data, bb)
1135 depth_first_search_ds data;
1138 data->stack[data->sp++] = bb;
1139 SET_BIT (data->visited_blocks, bb->index - (INVALID_BLOCK + 1));
1142 /* Continue the depth-first search through the reverse graph starting with the
1143 block at the stack's top and ending when the stack is empty. Visited nodes
1144 are marked. Returns an unvisited basic block, or NULL if there is none
1148 flow_dfs_compute_reverse_execute (data)
1149 depth_first_search_ds data;
1155 while (data->sp > 0)
1157 bb = data->stack[--data->sp];
1159 /* Perform depth-first search on adjacent vertices. */
1160 for (e = bb->pred; e; e = e->pred_next)
1161 if (!TEST_BIT (data->visited_blocks,
1162 e->src->index - (INVALID_BLOCK + 1)))
1163 flow_dfs_compute_reverse_add_bb (data, e->src);
1166 /* Determine if there are unvisited basic blocks. */
1167 for (i = n_basic_blocks - (INVALID_BLOCK + 1); --i >= 0; )
1168 if (!TEST_BIT (data->visited_blocks, i))
1169 return BASIC_BLOCK (i + (INVALID_BLOCK + 1));
1174 /* Destroy the data structures needed for depth-first search on the
1178 flow_dfs_compute_reverse_finish (data)
1179 depth_first_search_ds data;
1182 sbitmap_free (data->visited_blocks);