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 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
195 set_edge_can_fallthru_flag ()
198 for (i = 0; i < n_basic_blocks; i++)
200 basic_block bb = BASIC_BLOCK (i);
203 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
204 for (e = bb->succ; e; e = e->succ_next)
205 if (e->flags & EDGE_FALLTHRU)
206 e->flags |= EDGE_CAN_FALLTHRU;
208 /* If the BB ends with an invertable condjump all (2) edges are
209 CAN_FALLTHRU edges. */
210 if (!bb->succ || !bb->succ->succ_next || bb->succ->succ_next->succ_next)
212 if (!any_condjump_p (bb->end))
214 if (!invert_jump (bb->end, JUMP_LABEL (bb->end), 0))
216 invert_jump (bb->end, JUMP_LABEL (bb->end), 0);
217 bb->succ->flags |= EDGE_CAN_FALLTHRU;
218 bb->succ->succ_next->flags |= EDGE_CAN_FALLTHRU;
222 /* Return true if we need to add fake edge to exit.
223 Helper function for the flow_call_edges_add. */
226 need_fake_edge_p (insn)
232 if ((GET_CODE (insn) == CALL_INSN
233 && !SIBLING_CALL_P (insn)
234 && !find_reg_note (insn, REG_NORETURN, NULL)
235 && !find_reg_note (insn, REG_ALWAYS_RETURN, NULL)
236 && !CONST_OR_PURE_CALL_P (insn)))
239 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
240 && MEM_VOLATILE_P (PATTERN (insn)))
241 || (GET_CODE (PATTERN (insn)) == PARALLEL
242 && asm_noperands (insn) != -1
243 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
244 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
247 /* Add fake edges to the function exit for any non constant and non noreturn
248 calls, volatile inline assembly in the bitmap of blocks specified by
249 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
252 The goal is to expose cases in which entering a basic block does not imply
253 that all subsequent instructions must be executed. */
256 flow_call_edges_add (blocks)
260 int blocks_split = 0;
263 bool check_last_block = false;
265 /* Map bb indices into basic block pointers since split_block
266 will renumber the basic blocks. */
268 bbs = xmalloc (n_basic_blocks * sizeof (*bbs));
272 for (i = 0; i < n_basic_blocks; i++)
273 bbs[bb_num++] = BASIC_BLOCK (i);
275 check_last_block = true;
278 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
280 bbs[bb_num++] = BASIC_BLOCK (i);
281 if (i == n_basic_blocks - 1)
282 check_last_block = true;
285 /* In the last basic block, before epilogue generation, there will be
286 a fallthru edge to EXIT. Special care is required if the last insn
287 of the last basic block is a call because make_edge folds duplicate
288 edges, which would result in the fallthru edge also being marked
289 fake, which would result in the fallthru edge being removed by
290 remove_fake_edges, which would result in an invalid CFG.
292 Moreover, we can't elide the outgoing fake edge, since the block
293 profiler needs to take this into account in order to solve the minimal
294 spanning tree in the case that the call doesn't return.
296 Handle this by adding a dummy instruction in a new last basic block. */
297 if (check_last_block)
299 basic_block bb = BASIC_BLOCK (n_basic_blocks - 1);
302 /* Back up past insns that must be kept in the same block as a call. */
303 while (insn != bb->head
304 && keep_with_call_p (insn))
305 insn = PREV_INSN (insn);
307 if (need_fake_edge_p (insn))
311 for (e = bb->succ; e; e = e->succ_next)
312 if (e->dest == EXIT_BLOCK_PTR)
315 insert_insn_on_edge (gen_rtx_USE (VOIDmode, const0_rtx), e);
316 commit_edge_insertions ();
320 /* Now add fake edges to the function exit for any non constant
321 calls since there is no way that we can determine if they will
324 for (i = 0; i < bb_num; i++)
326 basic_block bb = bbs[i];
330 for (insn = bb->end; ; insn = prev_insn)
332 prev_insn = PREV_INSN (insn);
333 if (need_fake_edge_p (insn))
336 rtx split_at_insn = insn;
338 /* Don't split the block between a call and an insn that should
339 remain in the same block as the call. */
340 if (GET_CODE (insn) == CALL_INSN)
341 while (split_at_insn != bb->end
342 && keep_with_call_p (NEXT_INSN (split_at_insn)))
343 split_at_insn = NEXT_INSN (split_at_insn);
345 /* The handling above of the final block before the epilogue
346 should be enough to verify that there is no edge to the exit
347 block in CFG already. Calling make_edge in such case would
348 cause us to mark that edge as fake and remove it later. */
350 #ifdef ENABLE_CHECKING
351 if (split_at_insn == bb->end)
352 for (e = bb->succ; e; e = e->succ_next)
353 if (e->dest == EXIT_BLOCK_PTR)
357 /* Note that the following may create a new basic block
358 and renumber the existing basic blocks. */
359 if (split_at_insn != bb->end)
361 e = split_block (bb, split_at_insn);
366 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
369 if (insn == bb->head)
381 /* Find unreachable blocks. An unreachable block will have 0 in
382 the reachable bit in block->flags. A non-zero value indicates the
383 block is reachable. */
386 find_unreachable_blocks ()
390 basic_block *tos, *worklist;
393 tos = worklist = (basic_block *) xmalloc (sizeof (basic_block) * n);
395 /* Clear all the reachability flags. */
397 for (i = 0; i < n; ++i)
398 BASIC_BLOCK (i)->flags &= ~BB_REACHABLE;
400 /* Add our starting points to the worklist. Almost always there will
401 be only one. It isn't inconceivable that we might one day directly
402 support Fortran alternate entry points. */
404 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
408 /* Mark the block reachable. */
409 e->dest->flags |= BB_REACHABLE;
412 /* Iterate: find everything reachable from what we've already seen. */
414 while (tos != worklist)
416 basic_block b = *--tos;
418 for (e = b->succ; e; e = e->succ_next)
419 if (!(e->dest->flags & BB_REACHABLE))
422 e->dest->flags |= BB_REACHABLE;
429 /* Functions to access an edge list with a vector representation.
430 Enough data is kept such that given an index number, the
431 pred and succ that edge represents can be determined, or
432 given a pred and a succ, its index number can be returned.
433 This allows algorithms which consume a lot of memory to
434 represent the normally full matrix of edge (pred,succ) with a
435 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
436 wasted space in the client code due to sparse flow graphs. */
438 /* This functions initializes the edge list. Basically the entire
439 flowgraph is processed, and all edges are assigned a number,
440 and the data structure is filled in. */
445 struct edge_list *elist;
451 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
455 /* Determine the number of edges in the flow graph by counting successor
456 edges on each basic block. */
457 for (x = 0; x < n_basic_blocks; x++)
459 basic_block bb = BASIC_BLOCK (x);
461 for (e = bb->succ; e; e = e->succ_next)
465 /* Don't forget successors of the entry block. */
466 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
469 elist = (struct edge_list *) xmalloc (sizeof (struct edge_list));
470 elist->num_blocks = block_count;
471 elist->num_edges = num_edges;
472 elist->index_to_edge = (edge *) xmalloc (sizeof (edge) * num_edges);
476 /* Follow successors of the entry block, and register these edges. */
477 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
478 elist->index_to_edge[num_edges++] = e;
480 for (x = 0; x < n_basic_blocks; x++)
482 basic_block bb = BASIC_BLOCK (x);
484 /* Follow all successors of blocks, and register these edges. */
485 for (e = bb->succ; e; e = e->succ_next)
486 elist->index_to_edge[num_edges++] = e;
492 /* This function free's memory associated with an edge list. */
495 free_edge_list (elist)
496 struct edge_list *elist;
500 free (elist->index_to_edge);
505 /* This function provides debug output showing an edge list. */
508 print_edge_list (f, elist)
510 struct edge_list *elist;
514 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
515 elist->num_blocks - 2, elist->num_edges);
517 for (x = 0; x < elist->num_edges; x++)
519 fprintf (f, " %-4d - edge(", x);
520 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
521 fprintf (f, "entry,");
523 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
525 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
526 fprintf (f, "exit)\n");
528 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
532 /* This function provides an internal consistency check of an edge list,
533 verifying that all edges are present, and that there are no
537 verify_edge_list (f, elist)
539 struct edge_list *elist;
541 int x, pred, succ, index;
544 for (x = 0; x < n_basic_blocks; x++)
546 basic_block bb = BASIC_BLOCK (x);
548 for (e = bb->succ; e; e = e->succ_next)
550 pred = e->src->index;
551 succ = e->dest->index;
552 index = EDGE_INDEX (elist, e->src, e->dest);
553 if (index == EDGE_INDEX_NO_EDGE)
555 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
559 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
560 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
561 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
562 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
563 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
564 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
568 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
570 pred = e->src->index;
571 succ = e->dest->index;
572 index = EDGE_INDEX (elist, e->src, e->dest);
573 if (index == EDGE_INDEX_NO_EDGE)
575 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
579 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
580 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
581 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
582 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
583 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
584 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
587 /* We've verified that all the edges are in the list, no lets make sure
588 there are no spurious edges in the list. */
590 for (pred = 0; pred < n_basic_blocks; pred++)
591 for (succ = 0; succ < n_basic_blocks; succ++)
593 basic_block p = BASIC_BLOCK (pred);
594 basic_block s = BASIC_BLOCK (succ);
597 for (e = p->succ; e; e = e->succ_next)
604 for (e = s->pred; e; e = e->pred_next)
611 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
612 == EDGE_INDEX_NO_EDGE && found_edge != 0)
613 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
615 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
616 != EDGE_INDEX_NO_EDGE && found_edge == 0)
617 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
618 pred, succ, EDGE_INDEX (elist, BASIC_BLOCK (pred),
619 BASIC_BLOCK (succ)));
622 for (succ = 0; succ < n_basic_blocks; succ++)
624 basic_block p = ENTRY_BLOCK_PTR;
625 basic_block s = BASIC_BLOCK (succ);
628 for (e = p->succ; e; e = e->succ_next)
635 for (e = s->pred; e; e = e->pred_next)
642 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
643 == EDGE_INDEX_NO_EDGE && found_edge != 0)
644 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
646 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
647 != EDGE_INDEX_NO_EDGE && found_edge == 0)
648 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
649 succ, EDGE_INDEX (elist, ENTRY_BLOCK_PTR,
650 BASIC_BLOCK (succ)));
653 for (pred = 0; pred < n_basic_blocks; pred++)
655 basic_block p = BASIC_BLOCK (pred);
656 basic_block s = EXIT_BLOCK_PTR;
659 for (e = p->succ; e; e = e->succ_next)
666 for (e = s->pred; e; e = e->pred_next)
673 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
674 == EDGE_INDEX_NO_EDGE && found_edge != 0)
675 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
677 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
678 != EDGE_INDEX_NO_EDGE && found_edge == 0)
679 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
680 pred, EDGE_INDEX (elist, BASIC_BLOCK (pred),
685 /* This routine will determine what, if any, edge there is between
686 a specified predecessor and successor. */
689 find_edge_index (edge_list, pred, succ)
690 struct edge_list *edge_list;
691 basic_block pred, succ;
695 for (x = 0; x < NUM_EDGES (edge_list); x++)
696 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
697 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
700 return (EDGE_INDEX_NO_EDGE);
703 /* Dump the list of basic blocks in the bitmap NODES. */
706 flow_nodes_print (str, nodes, file)
716 fprintf (file, "%s { ", str);
717 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {fprintf (file, "%d ", node);});
721 /* Dump the list of edges in the array EDGE_LIST. */
724 flow_edge_list_print (str, edge_list, num_edges, file)
726 const edge *edge_list;
735 fprintf (file, "%s { ", str);
736 for (i = 0; i < num_edges; i++)
737 fprintf (file, "%d->%d ", edge_list[i]->src->index,
738 edge_list[i]->dest->index);
744 /* This routine will remove any fake successor edges for a basic block.
745 When the edge is removed, it is also removed from whatever predecessor
749 remove_fake_successors (bb)
754 for (e = bb->succ; e;)
759 if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
764 /* This routine will remove all fake edges from the flow graph. If
765 we remove all fake successors, it will automatically remove all
766 fake predecessors. */
773 for (x = 0; x < n_basic_blocks; x++)
774 remove_fake_successors (BASIC_BLOCK (x));
776 /* We've handled all successors except the entry block's. */
777 remove_fake_successors (ENTRY_BLOCK_PTR);
780 /* This function will add a fake edge between any block which has no
781 successors, and the exit block. Some data flow equations require these
785 add_noreturn_fake_exit_edges ()
789 for (x = 0; x < n_basic_blocks; x++)
790 if (BASIC_BLOCK (x)->succ == NULL)
791 make_single_succ_edge (BASIC_BLOCK (x), EXIT_BLOCK_PTR, EDGE_FAKE);
794 /* This function adds a fake edge between any infinite loops to the
795 exit block. Some optimizations require a path from each node to
798 See also Morgan, Figure 3.10, pp. 82-83.
800 The current implementation is ugly, not attempting to minimize the
801 number of inserted fake edges. To reduce the number of fake edges
802 to insert, add fake edges from _innermost_ loops containing only
803 nodes not reachable from the exit block. */
806 connect_infinite_loops_to_exit ()
808 basic_block unvisited_block;
809 struct depth_first_search_dsS dfs_ds;
811 /* Perform depth-first search in the reverse graph to find nodes
812 reachable from the exit block. */
813 flow_dfs_compute_reverse_init (&dfs_ds);
814 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
816 /* Repeatedly add fake edges, updating the unreachable nodes. */
819 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds);
820 if (!unvisited_block)
823 make_edge (unvisited_block, EXIT_BLOCK_PTR, EDGE_FAKE);
824 flow_dfs_compute_reverse_add_bb (&dfs_ds, unvisited_block);
827 flow_dfs_compute_reverse_finish (&dfs_ds);
831 /* Compute reverse top sort order */
834 flow_reverse_top_sort_order_compute (rts_order)
842 /* Allocate stack for back-tracking up CFG. */
843 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
846 /* Allocate bitmap to track nodes that have been visited. */
847 visited = sbitmap_alloc (n_basic_blocks);
849 /* None of the nodes in the CFG have been visited yet. */
850 sbitmap_zero (visited);
852 /* Push the first edge on to the stack. */
853 stack[sp++] = ENTRY_BLOCK_PTR->succ;
861 /* Look at the edge on the top of the stack. */
866 /* Check if the edge destination has been visited yet. */
867 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
869 /* Mark that we have visited the destination. */
870 SET_BIT (visited, dest->index);
873 /* Since the DEST node has been visited for the first
874 time, check its successors. */
875 stack[sp++] = dest->succ;
877 rts_order[postnum++] = dest->index;
881 if (! e->succ_next && src != ENTRY_BLOCK_PTR)
882 rts_order[postnum++] = src->index;
885 stack[sp - 1] = e->succ_next;
892 sbitmap_free (visited);
895 /* Compute the depth first search order and store in the array
896 DFS_ORDER if non-zero, marking the nodes visited in VISITED. If
897 RC_ORDER is non-zero, return the reverse completion number for each
898 node. Returns the number of nodes visited. A depth first search
899 tries to get as far away from the starting point as quickly as
903 flow_depth_first_order_compute (dfs_order, rc_order)
910 int rcnum = n_basic_blocks - 1;
913 /* Allocate stack for back-tracking up CFG. */
914 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
917 /* Allocate bitmap to track nodes that have been visited. */
918 visited = sbitmap_alloc (n_basic_blocks);
920 /* None of the nodes in the CFG have been visited yet. */
921 sbitmap_zero (visited);
923 /* Push the first edge on to the stack. */
924 stack[sp++] = ENTRY_BLOCK_PTR->succ;
932 /* Look at the edge on the top of the stack. */
937 /* Check if the edge destination has been visited yet. */
938 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
940 /* Mark that we have visited the destination. */
941 SET_BIT (visited, dest->index);
944 dfs_order[dfsnum] = dest->index;
949 /* Since the DEST node has been visited for the first
950 time, check its successors. */
951 stack[sp++] = dest->succ;
953 /* There are no successors for the DEST node so assign
954 its reverse completion number. */
955 rc_order[rcnum--] = dest->index;
959 if (! e->succ_next && src != ENTRY_BLOCK_PTR
961 /* There are no more successors for the SRC node
962 so assign its reverse completion number. */
963 rc_order[rcnum--] = src->index;
966 stack[sp - 1] = e->succ_next;
973 sbitmap_free (visited);
975 /* The number of nodes visited should not be greater than
977 if (dfsnum > n_basic_blocks)
980 /* There are some nodes left in the CFG that are unreachable. */
981 if (dfsnum < n_basic_blocks)
990 struct dfst_node **node;
991 struct dfst_node *up;
994 /* Compute a preorder transversal ordering such that a sub-tree which
995 is the source of a cross edge appears before the sub-tree which is
996 the destination of the cross edge. This allows for easy detection
997 of all the entry blocks for a loop.
999 The ordering is compute by:
1001 1) Generating a depth first spanning tree.
1003 2) Walking the resulting tree from right to left. */
1006 flow_preorder_transversal_compute (pot_order)
1015 struct dfst_node *node;
1016 struct dfst_node *dfst;
1018 /* Allocate stack for back-tracking up CFG. */
1019 stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
1022 /* Allocate the tree. */
1023 dfst = (struct dfst_node *) xcalloc (n_basic_blocks,
1024 sizeof (struct dfst_node));
1026 for (i = 0; i < n_basic_blocks; i++)
1029 for (e = BASIC_BLOCK (i)->succ; e; e = e->succ_next)
1034 ? (struct dfst_node **) xcalloc (max_successors,
1035 sizeof (struct dfst_node *))
1039 /* Allocate bitmap to track nodes that have been visited. */
1040 visited = sbitmap_alloc (n_basic_blocks);
1042 /* None of the nodes in the CFG have been visited yet. */
1043 sbitmap_zero (visited);
1045 /* Push the first edge on to the stack. */
1046 stack[sp++] = ENTRY_BLOCK_PTR->succ;
1053 /* Look at the edge on the top of the stack. */
1058 /* Check if the edge destination has been visited yet. */
1059 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
1061 /* Mark that we have visited the destination. */
1062 SET_BIT (visited, dest->index);
1064 /* Add the destination to the preorder tree. */
1065 if (src != ENTRY_BLOCK_PTR)
1067 dfst[src->index].node[dfst[src->index].nnodes++]
1068 = &dfst[dest->index];
1069 dfst[dest->index].up = &dfst[src->index];
1073 /* Since the DEST node has been visited for the first
1074 time, check its successors. */
1075 stack[sp++] = dest->succ;
1078 else if (e->succ_next)
1079 stack[sp - 1] = e->succ_next;
1085 sbitmap_free (visited);
1087 /* Record the preorder transversal order by
1088 walking the tree from right to left. */
1098 node = node->node[--node->nnodes];
1099 pot_order[i++] = node - dfst;
1105 /* Free the tree. */
1107 for (i = 0; i < n_basic_blocks; i++)
1109 free (dfst[i].node);
1114 /* Compute the depth first search order on the _reverse_ graph and
1115 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1116 Returns the number of nodes visited.
1118 The computation is split into three pieces:
1120 flow_dfs_compute_reverse_init () creates the necessary data
1123 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1124 structures. The block will start the search.
1126 flow_dfs_compute_reverse_execute () continues (or starts) the
1127 search using the block on the top of the stack, stopping when the
1130 flow_dfs_compute_reverse_finish () destroys the necessary data
1133 Thus, the user will probably call ..._init(), call ..._add_bb() to
1134 add a beginning basic block to the stack, call ..._execute(),
1135 possibly add another bb to the stack and again call ..._execute(),
1136 ..., and finally call _finish(). */
1138 /* Initialize the data structures used for depth-first search on the
1139 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1140 added to the basic block stack. DATA is the current depth-first
1141 search context. If INITIALIZE_STACK is non-zero, there is an
1142 element on the stack. */
1145 flow_dfs_compute_reverse_init (data)
1146 depth_first_search_ds data;
1148 /* Allocate stack for back-tracking up CFG. */
1149 data->stack = (basic_block *) xmalloc ((n_basic_blocks - (INVALID_BLOCK + 1))
1150 * sizeof (basic_block));
1153 /* Allocate bitmap to track nodes that have been visited. */
1154 data->visited_blocks = sbitmap_alloc (n_basic_blocks - (INVALID_BLOCK + 1));
1156 /* None of the nodes in the CFG have been visited yet. */
1157 sbitmap_zero (data->visited_blocks);
1162 /* Add the specified basic block to the top of the dfs data
1163 structures. When the search continues, it will start at the
1167 flow_dfs_compute_reverse_add_bb (data, bb)
1168 depth_first_search_ds data;
1171 data->stack[data->sp++] = bb;
1172 SET_BIT (data->visited_blocks, bb->index - (INVALID_BLOCK + 1));
1175 /* Continue the depth-first search through the reverse graph starting with the
1176 block at the stack's top and ending when the stack is empty. Visited nodes
1177 are marked. Returns an unvisited basic block, or NULL if there is none
1181 flow_dfs_compute_reverse_execute (data)
1182 depth_first_search_ds data;
1188 while (data->sp > 0)
1190 bb = data->stack[--data->sp];
1192 /* Perform depth-first search on adjacent vertices. */
1193 for (e = bb->pred; e; e = e->pred_next)
1194 if (!TEST_BIT (data->visited_blocks,
1195 e->src->index - (INVALID_BLOCK + 1)))
1196 flow_dfs_compute_reverse_add_bb (data, e->src);
1199 /* Determine if there are unvisited basic blocks. */
1200 for (i = n_basic_blocks - (INVALID_BLOCK + 1); --i >= 0; )
1201 if (!TEST_BIT (data->visited_blocks, i))
1202 return BASIC_BLOCK (i + (INVALID_BLOCK + 1));
1207 /* Destroy the data structures needed for depth-first search on the
1211 flow_dfs_compute_reverse_finish (data)
1212 depth_first_search_ds data;
1215 sbitmap_free (data->visited_blocks);