* class.c (build_base_path): Tidy a bit.
[official-gcc.git] / gcc / cfganal.c
blob1a7d280513f7bc1f693ac128dbfa32302d3bf972
1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2003, 2004 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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 /* This file contains various simple utilities to analyze the CFG. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "toplev.h"
33 #include "tm_p.h"
35 /* Store the data structures necessary for depth-first search. */
36 struct depth_first_search_dsS {
37 /* stack for backtracking during the algorithm */
38 basic_block *stack;
40 /* number of edges in the stack. That is, positions 0, ..., sp-1
41 have edges. */
42 unsigned int sp;
44 /* record of basic blocks already seen by depth-first search */
45 sbitmap visited_blocks;
47 typedef struct depth_first_search_dsS *depth_first_search_ds;
49 static void flow_dfs_compute_reverse_init (depth_first_search_ds);
50 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds,
51 basic_block);
52 static basic_block flow_dfs_compute_reverse_execute (depth_first_search_ds);
53 static void flow_dfs_compute_reverse_finish (depth_first_search_ds);
54 static void remove_fake_successors (basic_block);
55 static bool flow_active_insn_p (rtx);
57 /* Like active_insn_p, except keep the return value clobber around
58 even after reload. */
60 static bool
61 flow_active_insn_p (rtx insn)
63 if (active_insn_p (insn))
64 return true;
66 /* A clobber of the function return value exists for buggy
67 programs that fail to return a value. Its effect is to
68 keep the return value from being live across the entire
69 function. If we allow it to be skipped, we introduce the
70 possibility for register livetime aborts. */
71 if (GET_CODE (PATTERN (insn)) == CLOBBER
72 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG
73 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
74 return true;
76 return false;
79 /* Return true if the block has no effect and only forwards control flow to
80 its single destination. */
82 bool
83 forwarder_block_p (basic_block bb)
85 rtx insn;
87 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR
88 || !bb->succ || bb->succ->succ_next)
89 return false;
91 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
92 if (INSN_P (insn) && flow_active_insn_p (insn))
93 return false;
95 return (!INSN_P (insn)
96 || (GET_CODE (insn) == JUMP_INSN && simplejump_p (insn))
97 || !flow_active_insn_p (insn));
100 /* Return nonzero if we can reach target from src by falling through. */
102 bool
103 can_fallthru (basic_block src, basic_block target)
105 rtx insn = BB_END (src);
106 rtx insn2 = target == EXIT_BLOCK_PTR ? NULL : BB_HEAD (target);
108 if (src->next_bb != target)
109 return 0;
111 if (insn2 && !active_insn_p (insn2))
112 insn2 = next_active_insn (insn2);
114 /* ??? Later we may add code to move jump tables offline. */
115 return next_active_insn (insn) == insn2;
118 /* Mark the back edges in DFS traversal.
119 Return nonzero if a loop (natural or otherwise) is present.
120 Inspired by Depth_First_Search_PP described in:
122 Advanced Compiler Design and Implementation
123 Steven Muchnick
124 Morgan Kaufmann, 1997
126 and heavily borrowed from flow_depth_first_order_compute. */
128 bool
129 mark_dfs_back_edges (void)
131 edge *stack;
132 int *pre;
133 int *post;
134 int sp;
135 int prenum = 1;
136 int postnum = 1;
137 sbitmap visited;
138 bool found = false;
140 /* Allocate the preorder and postorder number arrays. */
141 pre = xcalloc (last_basic_block, sizeof (int));
142 post = xcalloc (last_basic_block, sizeof (int));
144 /* Allocate stack for back-tracking up CFG. */
145 stack = xmalloc ((n_basic_blocks + 1) * sizeof (edge));
146 sp = 0;
148 /* Allocate bitmap to track nodes that have been visited. */
149 visited = sbitmap_alloc (last_basic_block);
151 /* None of the nodes in the CFG have been visited yet. */
152 sbitmap_zero (visited);
154 /* Push the first edge on to the stack. */
155 stack[sp++] = ENTRY_BLOCK_PTR->succ;
157 while (sp)
159 edge e;
160 basic_block src;
161 basic_block dest;
163 /* Look at the edge on the top of the stack. */
164 e = stack[sp - 1];
165 src = e->src;
166 dest = e->dest;
167 e->flags &= ~EDGE_DFS_BACK;
169 /* Check if the edge destination has been visited yet. */
170 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
172 /* Mark that we have visited the destination. */
173 SET_BIT (visited, dest->index);
175 pre[dest->index] = prenum++;
176 if (dest->succ)
178 /* Since the DEST node has been visited for the first
179 time, check its successors. */
180 stack[sp++] = dest->succ;
182 else
183 post[dest->index] = postnum++;
185 else
187 if (dest != EXIT_BLOCK_PTR && src != ENTRY_BLOCK_PTR
188 && pre[src->index] >= pre[dest->index]
189 && post[dest->index] == 0)
190 e->flags |= EDGE_DFS_BACK, found = true;
192 if (! e->succ_next && src != ENTRY_BLOCK_PTR)
193 post[src->index] = postnum++;
195 if (e->succ_next)
196 stack[sp - 1] = e->succ_next;
197 else
198 sp--;
202 free (pre);
203 free (post);
204 free (stack);
205 sbitmap_free (visited);
207 return found;
210 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
212 void
213 set_edge_can_fallthru_flag (void)
215 basic_block bb;
217 FOR_EACH_BB (bb)
219 edge e;
221 for (e = bb->succ; e; e = e->succ_next)
223 e->flags &= ~EDGE_CAN_FALLTHRU;
225 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
226 if (e->flags & EDGE_FALLTHRU)
227 e->flags |= EDGE_CAN_FALLTHRU;
230 /* If the BB ends with an invertible condjump all (2) edges are
231 CAN_FALLTHRU edges. */
232 if (!bb->succ || !bb->succ->succ_next || bb->succ->succ_next->succ_next)
233 continue;
234 if (!any_condjump_p (BB_END (bb)))
235 continue;
236 if (!invert_jump (BB_END (bb), JUMP_LABEL (BB_END (bb)), 0))
237 continue;
238 invert_jump (BB_END (bb), JUMP_LABEL (BB_END (bb)), 0);
239 bb->succ->flags |= EDGE_CAN_FALLTHRU;
240 bb->succ->succ_next->flags |= EDGE_CAN_FALLTHRU;
244 /* Find unreachable blocks. An unreachable block will have 0 in
245 the reachable bit in block->flags. A nonzero value indicates the
246 block is reachable. */
248 void
249 find_unreachable_blocks (void)
251 edge e;
252 basic_block *tos, *worklist, bb;
254 tos = worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
256 /* Clear all the reachability flags. */
258 FOR_EACH_BB (bb)
259 bb->flags &= ~BB_REACHABLE;
261 /* Add our starting points to the worklist. Almost always there will
262 be only one. It isn't inconceivable that we might one day directly
263 support Fortran alternate entry points. */
265 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
267 *tos++ = e->dest;
269 /* Mark the block reachable. */
270 e->dest->flags |= BB_REACHABLE;
273 /* Iterate: find everything reachable from what we've already seen. */
275 while (tos != worklist)
277 basic_block b = *--tos;
279 for (e = b->succ; e; e = e->succ_next)
280 if (!(e->dest->flags & BB_REACHABLE))
282 *tos++ = e->dest;
283 e->dest->flags |= BB_REACHABLE;
287 free (worklist);
290 /* Functions to access an edge list with a vector representation.
291 Enough data is kept such that given an index number, the
292 pred and succ that edge represents can be determined, or
293 given a pred and a succ, its index number can be returned.
294 This allows algorithms which consume a lot of memory to
295 represent the normally full matrix of edge (pred,succ) with a
296 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
297 wasted space in the client code due to sparse flow graphs. */
299 /* This functions initializes the edge list. Basically the entire
300 flowgraph is processed, and all edges are assigned a number,
301 and the data structure is filled in. */
303 struct edge_list *
304 create_edge_list (void)
306 struct edge_list *elist;
307 edge e;
308 int num_edges;
309 int block_count;
310 basic_block bb;
312 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
314 num_edges = 0;
316 /* Determine the number of edges in the flow graph by counting successor
317 edges on each basic block. */
318 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
320 for (e = bb->succ; e; e = e->succ_next)
321 num_edges++;
324 elist = xmalloc (sizeof (struct edge_list));
325 elist->num_blocks = block_count;
326 elist->num_edges = num_edges;
327 elist->index_to_edge = xmalloc (sizeof (edge) * num_edges);
329 num_edges = 0;
331 /* Follow successors of blocks, and register these edges. */
332 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
333 for (e = bb->succ; e; e = e->succ_next)
334 elist->index_to_edge[num_edges++] = e;
336 return elist;
339 /* This function free's memory associated with an edge list. */
341 void
342 free_edge_list (struct edge_list *elist)
344 if (elist)
346 free (elist->index_to_edge);
347 free (elist);
351 /* This function provides debug output showing an edge list. */
353 void
354 print_edge_list (FILE *f, struct edge_list *elist)
356 int x;
358 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
359 elist->num_blocks - 2, elist->num_edges);
361 for (x = 0; x < elist->num_edges; x++)
363 fprintf (f, " %-4d - edge(", x);
364 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
365 fprintf (f, "entry,");
366 else
367 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
369 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
370 fprintf (f, "exit)\n");
371 else
372 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
376 /* This function provides an internal consistency check of an edge list,
377 verifying that all edges are present, and that there are no
378 extra edges. */
380 void
381 verify_edge_list (FILE *f, struct edge_list *elist)
383 int pred, succ, index;
384 edge e;
385 basic_block bb, p, s;
387 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
389 for (e = bb->succ; e; e = e->succ_next)
391 pred = e->src->index;
392 succ = e->dest->index;
393 index = EDGE_INDEX (elist, e->src, e->dest);
394 if (index == EDGE_INDEX_NO_EDGE)
396 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
397 continue;
400 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
401 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
402 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
403 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
404 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
405 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
409 /* We've verified that all the edges are in the list, now lets make sure
410 there are no spurious edges in the list. */
412 FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
413 FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
415 int found_edge = 0;
417 for (e = p->succ; e; e = e->succ_next)
418 if (e->dest == s)
420 found_edge = 1;
421 break;
424 for (e = s->pred; e; e = e->pred_next)
425 if (e->src == p)
427 found_edge = 1;
428 break;
431 if (EDGE_INDEX (elist, p, s)
432 == EDGE_INDEX_NO_EDGE && found_edge != 0)
433 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
434 p->index, s->index);
435 if (EDGE_INDEX (elist, p, s)
436 != EDGE_INDEX_NO_EDGE && found_edge == 0)
437 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
438 p->index, s->index, EDGE_INDEX (elist, p, s));
442 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
443 If no such edge exists, return NULL. */
445 edge
446 find_edge (basic_block pred, basic_block succ)
448 edge e;
450 for (e = pred->succ; e; e = e->succ_next)
451 if (e->dest == succ)
452 return e;
454 return NULL;
457 /* This routine will determine what, if any, edge there is between
458 a specified predecessor and successor. */
461 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
463 int x;
465 for (x = 0; x < NUM_EDGES (edge_list); x++)
466 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
467 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
468 return x;
470 return (EDGE_INDEX_NO_EDGE);
473 /* Dump the list of basic blocks in the bitmap NODES. */
475 void
476 flow_nodes_print (const char *str, const sbitmap nodes, FILE *file)
478 int node;
480 if (! nodes)
481 return;
483 fprintf (file, "%s { ", str);
484 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {fprintf (file, "%d ", node);});
485 fputs ("}\n", file);
488 /* Dump the list of edges in the array EDGE_LIST. */
490 void
491 flow_edge_list_print (const char *str, const edge *edge_list, int num_edges, FILE *file)
493 int i;
495 if (! edge_list)
496 return;
498 fprintf (file, "%s { ", str);
499 for (i = 0; i < num_edges; i++)
500 fprintf (file, "%d->%d ", edge_list[i]->src->index,
501 edge_list[i]->dest->index);
503 fputs ("}\n", file);
507 /* This routine will remove any fake successor edges for a basic block.
508 When the edge is removed, it is also removed from whatever predecessor
509 list it is in. */
511 static void
512 remove_fake_successors (basic_block bb)
514 edge e;
516 for (e = bb->succ; e;)
518 edge tmp = e;
520 e = e->succ_next;
521 if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
522 remove_edge (tmp);
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. */
530 void
531 remove_fake_edges (void)
533 basic_block bb;
535 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
536 remove_fake_successors (bb);
539 /* This function will add a fake edge between any block which has no
540 successors, and the exit block. Some data flow equations require these
541 edges to exist. */
543 void
544 add_noreturn_fake_exit_edges (void)
546 basic_block bb;
548 FOR_EACH_BB (bb)
549 if (bb->succ == NULL)
550 make_single_succ_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
553 /* This function adds a fake edge between any infinite loops to the
554 exit block. Some optimizations require a path from each node to
555 the exit node.
557 See also Morgan, Figure 3.10, pp. 82-83.
559 The current implementation is ugly, not attempting to minimize the
560 number of inserted fake edges. To reduce the number of fake edges
561 to insert, add fake edges from _innermost_ loops containing only
562 nodes not reachable from the exit block. */
564 void
565 connect_infinite_loops_to_exit (void)
567 basic_block unvisited_block;
568 struct depth_first_search_dsS dfs_ds;
570 /* Perform depth-first search in the reverse graph to find nodes
571 reachable from the exit block. */
572 flow_dfs_compute_reverse_init (&dfs_ds);
573 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
575 /* Repeatedly add fake edges, updating the unreachable nodes. */
576 while (1)
578 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds);
579 if (!unvisited_block)
580 break;
582 make_edge (unvisited_block, EXIT_BLOCK_PTR, EDGE_FAKE);
583 flow_dfs_compute_reverse_add_bb (&dfs_ds, unvisited_block);
586 flow_dfs_compute_reverse_finish (&dfs_ds);
587 return;
590 /* Compute reverse top sort order. */
592 void
593 flow_reverse_top_sort_order_compute (int *rts_order)
595 edge *stack;
596 int sp;
597 int postnum = 0;
598 sbitmap visited;
600 /* Allocate stack for back-tracking up CFG. */
601 stack = xmalloc ((n_basic_blocks + 1) * sizeof (edge));
602 sp = 0;
604 /* Allocate bitmap to track nodes that have been visited. */
605 visited = sbitmap_alloc (last_basic_block);
607 /* None of the nodes in the CFG have been visited yet. */
608 sbitmap_zero (visited);
610 /* Push the first edge on to the stack. */
611 stack[sp++] = ENTRY_BLOCK_PTR->succ;
613 while (sp)
615 edge e;
616 basic_block src;
617 basic_block dest;
619 /* Look at the edge on the top of the stack. */
620 e = stack[sp - 1];
621 src = e->src;
622 dest = e->dest;
624 /* Check if the edge destination has been visited yet. */
625 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
627 /* Mark that we have visited the destination. */
628 SET_BIT (visited, dest->index);
630 if (dest->succ)
631 /* Since the DEST node has been visited for the first
632 time, check its successors. */
633 stack[sp++] = dest->succ;
634 else
635 rts_order[postnum++] = dest->index;
637 else
639 if (! e->succ_next && src != ENTRY_BLOCK_PTR)
640 rts_order[postnum++] = src->index;
642 if (e->succ_next)
643 stack[sp - 1] = e->succ_next;
644 else
645 sp--;
649 free (stack);
650 sbitmap_free (visited);
653 /* Compute the depth first search order and store in the array
654 DFS_ORDER if nonzero, marking the nodes visited in VISITED. If
655 RC_ORDER is nonzero, return the reverse completion number for each
656 node. Returns the number of nodes visited. A depth first search
657 tries to get as far away from the starting point as quickly as
658 possible. */
661 flow_depth_first_order_compute (int *dfs_order, int *rc_order)
663 edge *stack;
664 int sp;
665 int dfsnum = 0;
666 int rcnum = n_basic_blocks - 1;
667 sbitmap visited;
669 /* Allocate stack for back-tracking up CFG. */
670 stack = xmalloc ((n_basic_blocks + 1) * sizeof (edge));
671 sp = 0;
673 /* Allocate bitmap to track nodes that have been visited. */
674 visited = sbitmap_alloc (last_basic_block);
676 /* None of the nodes in the CFG have been visited yet. */
677 sbitmap_zero (visited);
679 /* Push the first edge on to the stack. */
680 stack[sp++] = ENTRY_BLOCK_PTR->succ;
682 while (sp)
684 edge e;
685 basic_block src;
686 basic_block dest;
688 /* Look at the edge on the top of the stack. */
689 e = stack[sp - 1];
690 src = e->src;
691 dest = e->dest;
693 /* Check if the edge destination has been visited yet. */
694 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
696 /* Mark that we have visited the destination. */
697 SET_BIT (visited, dest->index);
699 if (dfs_order)
700 dfs_order[dfsnum] = dest->index;
702 dfsnum++;
704 if (dest->succ)
705 /* Since the DEST node has been visited for the first
706 time, check its successors. */
707 stack[sp++] = dest->succ;
708 else if (rc_order)
709 /* There are no successors for the DEST node so assign
710 its reverse completion number. */
711 rc_order[rcnum--] = dest->index;
713 else
715 if (! e->succ_next && src != ENTRY_BLOCK_PTR
716 && rc_order)
717 /* There are no more successors for the SRC node
718 so assign its reverse completion number. */
719 rc_order[rcnum--] = src->index;
721 if (e->succ_next)
722 stack[sp - 1] = e->succ_next;
723 else
724 sp--;
728 free (stack);
729 sbitmap_free (visited);
731 /* The number of nodes visited should not be greater than
732 n_basic_blocks. */
733 if (dfsnum > n_basic_blocks)
734 abort ();
736 /* There are some nodes left in the CFG that are unreachable. */
737 if (dfsnum < n_basic_blocks)
738 abort ();
740 return dfsnum;
743 struct dfst_node
745 unsigned nnodes;
746 struct dfst_node **node;
747 struct dfst_node *up;
750 /* Compute a preorder transversal ordering such that a sub-tree which
751 is the source of a cross edge appears before the sub-tree which is
752 the destination of the cross edge. This allows for easy detection
753 of all the entry blocks for a loop.
755 The ordering is compute by:
757 1) Generating a depth first spanning tree.
759 2) Walking the resulting tree from right to left. */
761 void
762 flow_preorder_transversal_compute (int *pot_order)
764 edge e;
765 edge *stack;
766 int i;
767 int max_successors;
768 int sp;
769 sbitmap visited;
770 struct dfst_node *node;
771 struct dfst_node *dfst;
772 basic_block bb;
774 /* Allocate stack for back-tracking up CFG. */
775 stack = xmalloc ((n_basic_blocks + 1) * sizeof (edge));
776 sp = 0;
778 /* Allocate the tree. */
779 dfst = xcalloc (last_basic_block, sizeof (struct dfst_node));
781 FOR_EACH_BB (bb)
783 max_successors = 0;
784 for (e = bb->succ; e; e = e->succ_next)
785 max_successors++;
787 dfst[bb->index].node
788 = (max_successors
789 ? xcalloc (max_successors, sizeof (struct dfst_node *)) : NULL);
792 /* Allocate bitmap to track nodes that have been visited. */
793 visited = sbitmap_alloc (last_basic_block);
795 /* None of the nodes in the CFG have been visited yet. */
796 sbitmap_zero (visited);
798 /* Push the first edge on to the stack. */
799 stack[sp++] = ENTRY_BLOCK_PTR->succ;
801 while (sp)
803 basic_block src;
804 basic_block dest;
806 /* Look at the edge on the top of the stack. */
807 e = stack[sp - 1];
808 src = e->src;
809 dest = e->dest;
811 /* Check if the edge destination has been visited yet. */
812 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
814 /* Mark that we have visited the destination. */
815 SET_BIT (visited, dest->index);
817 /* Add the destination to the preorder tree. */
818 if (src != ENTRY_BLOCK_PTR)
820 dfst[src->index].node[dfst[src->index].nnodes++]
821 = &dfst[dest->index];
822 dfst[dest->index].up = &dfst[src->index];
825 if (dest->succ)
826 /* Since the DEST node has been visited for the first
827 time, check its successors. */
828 stack[sp++] = dest->succ;
831 else if (e->succ_next)
832 stack[sp - 1] = e->succ_next;
833 else
834 sp--;
837 free (stack);
838 sbitmap_free (visited);
840 /* Record the preorder transversal order by
841 walking the tree from right to left. */
843 i = 0;
844 node = &dfst[ENTRY_BLOCK_PTR->next_bb->index];
845 pot_order[i++] = 0;
847 while (node)
849 if (node->nnodes)
851 node = node->node[--node->nnodes];
852 pot_order[i++] = node - dfst;
854 else
855 node = node->up;
858 /* Free the tree. */
860 for (i = 0; i < last_basic_block; i++)
861 if (dfst[i].node)
862 free (dfst[i].node);
864 free (dfst);
867 /* Compute the depth first search order on the _reverse_ graph and
868 store in the array DFS_ORDER, marking the nodes visited in VISITED.
869 Returns the number of nodes visited.
871 The computation is split into three pieces:
873 flow_dfs_compute_reverse_init () creates the necessary data
874 structures.
876 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
877 structures. The block will start the search.
879 flow_dfs_compute_reverse_execute () continues (or starts) the
880 search using the block on the top of the stack, stopping when the
881 stack is empty.
883 flow_dfs_compute_reverse_finish () destroys the necessary data
884 structures.
886 Thus, the user will probably call ..._init(), call ..._add_bb() to
887 add a beginning basic block to the stack, call ..._execute(),
888 possibly add another bb to the stack and again call ..._execute(),
889 ..., and finally call _finish(). */
891 /* Initialize the data structures used for depth-first search on the
892 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
893 added to the basic block stack. DATA is the current depth-first
894 search context. If INITIALIZE_STACK is nonzero, there is an
895 element on the stack. */
897 static void
898 flow_dfs_compute_reverse_init (depth_first_search_ds data)
900 /* Allocate stack for back-tracking up CFG. */
901 data->stack = xmalloc ((n_basic_blocks - (INVALID_BLOCK + 1))
902 * sizeof (basic_block));
903 data->sp = 0;
905 /* Allocate bitmap to track nodes that have been visited. */
906 data->visited_blocks = sbitmap_alloc (last_basic_block - (INVALID_BLOCK + 1));
908 /* None of the nodes in the CFG have been visited yet. */
909 sbitmap_zero (data->visited_blocks);
911 return;
914 /* Add the specified basic block to the top of the dfs data
915 structures. When the search continues, it will start at the
916 block. */
918 static void
919 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
921 data->stack[data->sp++] = bb;
922 SET_BIT (data->visited_blocks, bb->index - (INVALID_BLOCK + 1));
925 /* Continue the depth-first search through the reverse graph starting with the
926 block at the stack's top and ending when the stack is empty. Visited nodes
927 are marked. Returns an unvisited basic block, or NULL if there is none
928 available. */
930 static basic_block
931 flow_dfs_compute_reverse_execute (depth_first_search_ds data)
933 basic_block bb;
934 edge e;
936 while (data->sp > 0)
938 bb = data->stack[--data->sp];
940 /* Perform depth-first search on adjacent vertices. */
941 for (e = bb->pred; e; e = e->pred_next)
942 if (!TEST_BIT (data->visited_blocks,
943 e->src->index - (INVALID_BLOCK + 1)))
944 flow_dfs_compute_reverse_add_bb (data, e->src);
947 /* Determine if there are unvisited basic blocks. */
948 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
949 if (!TEST_BIT (data->visited_blocks, bb->index - (INVALID_BLOCK + 1)))
950 return bb;
952 return NULL;
955 /* Destroy the data structures needed for depth-first search on the
956 reverse graph. */
958 static void
959 flow_dfs_compute_reverse_finish (depth_first_search_ds data)
961 free (data->stack);
962 sbitmap_free (data->visited_blocks);
965 /* Performs dfs search from BB over vertices satisfying PREDICATE;
966 if REVERSE, go against direction of edges. Returns number of blocks
967 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
969 dfs_enumerate_from (basic_block bb, int reverse,
970 bool (*predicate) (basic_block, void *),
971 basic_block *rslt, int rslt_max, void *data)
973 basic_block *st, lbb;
974 int sp = 0, tv = 0;
976 st = xcalloc (rslt_max, sizeof (basic_block));
977 rslt[tv++] = st[sp++] = bb;
978 bb->flags |= BB_VISITED;
979 while (sp)
981 edge e;
982 lbb = st[--sp];
983 if (reverse)
985 for (e = lbb->pred; e; e = e->pred_next)
986 if (!(e->src->flags & BB_VISITED) && predicate (e->src, data))
988 if (tv == rslt_max)
989 abort ();
990 rslt[tv++] = st[sp++] = e->src;
991 e->src->flags |= BB_VISITED;
994 else
996 for (e = lbb->succ; e; e = e->succ_next)
997 if (!(e->dest->flags & BB_VISITED) && predicate (e->dest, data))
999 if (tv == rslt_max)
1000 abort ();
1001 rslt[tv++] = st[sp++] = e->dest;
1002 e->dest->flags |= BB_VISITED;
1006 free (st);
1007 for (sp = 0; sp < tv; sp++)
1008 rslt[sp]->flags &= ~BB_VISITED;
1009 return tv;