* rw.po: Remove.
[official-gcc.git] / gcc / cfganal.c
blob3095a3070a506141ffe4394b14e2bf4dcd7a000e
1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2003, 2004, 2005, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains various simple utilities to analyze the CFG. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "obstack.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"
34 #include "timevar.h"
36 /* Store the data structures necessary for depth-first search. */
37 struct depth_first_search_dsS {
38 /* stack for backtracking during the algorithm */
39 basic_block *stack;
41 /* number of edges in the stack. That is, positions 0, ..., sp-1
42 have edges. */
43 unsigned int sp;
45 /* record of basic blocks already seen by depth-first search */
46 sbitmap visited_blocks;
48 typedef struct depth_first_search_dsS *depth_first_search_ds;
50 static void flow_dfs_compute_reverse_init (depth_first_search_ds);
51 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds,
52 basic_block);
53 static basic_block flow_dfs_compute_reverse_execute (depth_first_search_ds,
54 basic_block);
55 static void flow_dfs_compute_reverse_finish (depth_first_search_ds);
56 static bool flow_active_insn_p (rtx);
58 /* Like active_insn_p, except keep the return value clobber around
59 even after reload. */
61 static bool
62 flow_active_insn_p (rtx insn)
64 if (active_insn_p (insn))
65 return true;
67 /* A clobber of the function return value exists for buggy
68 programs that fail to return a value. Its effect is to
69 keep the return value from being live across the entire
70 function. If we allow it to be skipped, we introduce the
71 possibility for register lifetime confusion. */
72 if (GET_CODE (PATTERN (insn)) == CLOBBER
73 && REG_P (XEXP (PATTERN (insn), 0))
74 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
75 return true;
77 return false;
80 /* Return true if the block has no effect and only forwards control flow to
81 its single destination. */
83 bool
84 forwarder_block_p (basic_block bb)
86 rtx insn;
88 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR
89 || !single_succ_p (bb))
90 return false;
92 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
93 if (INSN_P (insn) && flow_active_insn_p (insn))
94 return false;
96 return (!INSN_P (insn)
97 || (JUMP_P (insn) && simplejump_p (insn))
98 || !flow_active_insn_p (insn));
101 /* Return nonzero if we can reach target from src by falling through. */
103 bool
104 can_fallthru (basic_block src, basic_block target)
106 rtx insn = BB_END (src);
107 rtx insn2;
108 edge e;
109 edge_iterator ei;
111 if (target == EXIT_BLOCK_PTR)
112 return true;
113 if (src->next_bb != target)
114 return 0;
115 FOR_EACH_EDGE (e, ei, src->succs)
116 if (e->dest == EXIT_BLOCK_PTR
117 && e->flags & EDGE_FALLTHRU)
118 return 0;
120 insn2 = BB_HEAD (target);
121 if (insn2 && !active_insn_p (insn2))
122 insn2 = next_active_insn (insn2);
124 /* ??? Later we may add code to move jump tables offline. */
125 return next_active_insn (insn) == insn2;
128 /* Return nonzero if we could reach target from src by falling through,
129 if the target was made adjacent. If we already have a fall-through
130 edge to the exit block, we can't do that. */
131 bool
132 could_fall_through (basic_block src, basic_block target)
134 edge e;
135 edge_iterator ei;
137 if (target == EXIT_BLOCK_PTR)
138 return true;
139 FOR_EACH_EDGE (e, ei, src->succs)
140 if (e->dest == EXIT_BLOCK_PTR
141 && e->flags & EDGE_FALLTHRU)
142 return 0;
143 return true;
146 /* Mark the back edges in DFS traversal.
147 Return nonzero if a loop (natural or otherwise) is present.
148 Inspired by Depth_First_Search_PP described in:
150 Advanced Compiler Design and Implementation
151 Steven Muchnick
152 Morgan Kaufmann, 1997
154 and heavily borrowed from pre_and_rev_post_order_compute. */
156 bool
157 mark_dfs_back_edges (void)
159 edge_iterator *stack;
160 int *pre;
161 int *post;
162 int sp;
163 int prenum = 1;
164 int postnum = 1;
165 sbitmap visited;
166 bool found = false;
168 /* Allocate the preorder and postorder number arrays. */
169 pre = XCNEWVEC (int, last_basic_block);
170 post = XCNEWVEC (int, last_basic_block);
172 /* Allocate stack for back-tracking up CFG. */
173 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
174 sp = 0;
176 /* Allocate bitmap to track nodes that have been visited. */
177 visited = sbitmap_alloc (last_basic_block);
179 /* None of the nodes in the CFG have been visited yet. */
180 sbitmap_zero (visited);
182 /* Push the first edge on to the stack. */
183 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
185 while (sp)
187 edge_iterator ei;
188 basic_block src;
189 basic_block dest;
191 /* Look at the edge on the top of the stack. */
192 ei = stack[sp - 1];
193 src = ei_edge (ei)->src;
194 dest = ei_edge (ei)->dest;
195 ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
197 /* Check if the edge destination has been visited yet. */
198 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
200 /* Mark that we have visited the destination. */
201 SET_BIT (visited, dest->index);
203 pre[dest->index] = prenum++;
204 if (EDGE_COUNT (dest->succs) > 0)
206 /* Since the DEST node has been visited for the first
207 time, check its successors. */
208 stack[sp++] = ei_start (dest->succs);
210 else
211 post[dest->index] = postnum++;
213 else
215 if (dest != EXIT_BLOCK_PTR && src != ENTRY_BLOCK_PTR
216 && pre[src->index] >= pre[dest->index]
217 && post[dest->index] == 0)
218 ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
220 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR)
221 post[src->index] = postnum++;
223 if (!ei_one_before_end_p (ei))
224 ei_next (&stack[sp - 1]);
225 else
226 sp--;
230 free (pre);
231 free (post);
232 free (stack);
233 sbitmap_free (visited);
235 return found;
238 /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
240 void
241 set_edge_can_fallthru_flag (void)
243 basic_block bb;
245 FOR_EACH_BB (bb)
247 edge e;
248 edge_iterator ei;
250 FOR_EACH_EDGE (e, ei, bb->succs)
252 e->flags &= ~EDGE_CAN_FALLTHRU;
254 /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
255 if (e->flags & EDGE_FALLTHRU)
256 e->flags |= EDGE_CAN_FALLTHRU;
259 /* If the BB ends with an invertible condjump all (2) edges are
260 CAN_FALLTHRU edges. */
261 if (EDGE_COUNT (bb->succs) != 2)
262 continue;
263 if (!any_condjump_p (BB_END (bb)))
264 continue;
265 if (!invert_jump (BB_END (bb), JUMP_LABEL (BB_END (bb)), 0))
266 continue;
267 invert_jump (BB_END (bb), JUMP_LABEL (BB_END (bb)), 0);
268 EDGE_SUCC (bb, 0)->flags |= EDGE_CAN_FALLTHRU;
269 EDGE_SUCC (bb, 1)->flags |= EDGE_CAN_FALLTHRU;
273 /* Find unreachable blocks. An unreachable block will have 0 in
274 the reachable bit in block->flags. A nonzero value indicates the
275 block is reachable. */
277 void
278 find_unreachable_blocks (void)
280 edge e;
281 edge_iterator ei;
282 basic_block *tos, *worklist, bb;
284 tos = worklist = XNEWVEC (basic_block, n_basic_blocks);
286 /* Clear all the reachability flags. */
288 FOR_EACH_BB (bb)
289 bb->flags &= ~BB_REACHABLE;
291 /* Add our starting points to the worklist. Almost always there will
292 be only one. It isn't inconceivable that we might one day directly
293 support Fortran alternate entry points. */
295 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
297 *tos++ = e->dest;
299 /* Mark the block reachable. */
300 e->dest->flags |= BB_REACHABLE;
303 /* Iterate: find everything reachable from what we've already seen. */
305 while (tos != worklist)
307 basic_block b = *--tos;
309 FOR_EACH_EDGE (e, ei, b->succs)
311 basic_block dest = e->dest;
313 if (!(dest->flags & BB_REACHABLE))
315 *tos++ = dest;
316 dest->flags |= BB_REACHABLE;
321 free (worklist);
324 /* Functions to access an edge list with a vector representation.
325 Enough data is kept such that given an index number, the
326 pred and succ that edge represents can be determined, or
327 given a pred and a succ, its index number can be returned.
328 This allows algorithms which consume a lot of memory to
329 represent the normally full matrix of edge (pred,succ) with a
330 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
331 wasted space in the client code due to sparse flow graphs. */
333 /* This functions initializes the edge list. Basically the entire
334 flowgraph is processed, and all edges are assigned a number,
335 and the data structure is filled in. */
337 struct edge_list *
338 create_edge_list (void)
340 struct edge_list *elist;
341 edge e;
342 int num_edges;
343 int block_count;
344 basic_block bb;
345 edge_iterator ei;
347 block_count = n_basic_blocks; /* Include the entry and exit blocks. */
349 num_edges = 0;
351 /* Determine the number of edges in the flow graph by counting successor
352 edges on each basic block. */
353 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
355 num_edges += EDGE_COUNT (bb->succs);
358 elist = XNEW (struct edge_list);
359 elist->num_blocks = block_count;
360 elist->num_edges = num_edges;
361 elist->index_to_edge = XNEWVEC (edge, num_edges);
363 num_edges = 0;
365 /* Follow successors of blocks, and register these edges. */
366 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
367 FOR_EACH_EDGE (e, ei, bb->succs)
368 elist->index_to_edge[num_edges++] = e;
370 return elist;
373 /* This function free's memory associated with an edge list. */
375 void
376 free_edge_list (struct edge_list *elist)
378 if (elist)
380 free (elist->index_to_edge);
381 free (elist);
385 /* This function provides debug output showing an edge list. */
387 void
388 print_edge_list (FILE *f, struct edge_list *elist)
390 int x;
392 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
393 elist->num_blocks, elist->num_edges);
395 for (x = 0; x < elist->num_edges; x++)
397 fprintf (f, " %-4d - edge(", x);
398 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
399 fprintf (f, "entry,");
400 else
401 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
403 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
404 fprintf (f, "exit)\n");
405 else
406 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
410 /* This function provides an internal consistency check of an edge list,
411 verifying that all edges are present, and that there are no
412 extra edges. */
414 void
415 verify_edge_list (FILE *f, struct edge_list *elist)
417 int pred, succ, index;
418 edge e;
419 basic_block bb, p, s;
420 edge_iterator ei;
422 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
424 FOR_EACH_EDGE (e, ei, bb->succs)
426 pred = e->src->index;
427 succ = e->dest->index;
428 index = EDGE_INDEX (elist, e->src, e->dest);
429 if (index == EDGE_INDEX_NO_EDGE)
431 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
432 continue;
435 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
436 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
437 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
438 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
439 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
440 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
444 /* We've verified that all the edges are in the list, now lets make sure
445 there are no spurious edges in the list. */
447 FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
448 FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
450 int found_edge = 0;
452 FOR_EACH_EDGE (e, ei, p->succs)
453 if (e->dest == s)
455 found_edge = 1;
456 break;
459 FOR_EACH_EDGE (e, ei, s->preds)
460 if (e->src == p)
462 found_edge = 1;
463 break;
466 if (EDGE_INDEX (elist, p, s)
467 == EDGE_INDEX_NO_EDGE && found_edge != 0)
468 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
469 p->index, s->index);
470 if (EDGE_INDEX (elist, p, s)
471 != EDGE_INDEX_NO_EDGE && found_edge == 0)
472 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
473 p->index, s->index, EDGE_INDEX (elist, p, s));
477 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
478 If no such edge exists, return NULL. */
480 edge
481 find_edge (basic_block pred, basic_block succ)
483 edge e;
484 edge_iterator ei;
486 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
488 FOR_EACH_EDGE (e, ei, pred->succs)
489 if (e->dest == succ)
490 return e;
492 else
494 FOR_EACH_EDGE (e, ei, succ->preds)
495 if (e->src == pred)
496 return e;
499 return NULL;
502 /* This routine will determine what, if any, edge there is between
503 a specified predecessor and successor. */
506 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
508 int x;
510 for (x = 0; x < NUM_EDGES (edge_list); x++)
511 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
512 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
513 return x;
515 return (EDGE_INDEX_NO_EDGE);
518 /* Dump the list of basic blocks in the bitmap NODES. */
520 void
521 flow_nodes_print (const char *str, const sbitmap nodes, FILE *file)
523 unsigned int node = 0;
524 sbitmap_iterator sbi;
526 if (! nodes)
527 return;
529 fprintf (file, "%s { ", str);
530 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, sbi)
531 fprintf (file, "%d ", node);
532 fputs ("}\n", file);
535 /* Dump the list of edges in the array EDGE_LIST. */
537 void
538 flow_edge_list_print (const char *str, const edge *edge_list, int num_edges, FILE *file)
540 int i;
542 if (! edge_list)
543 return;
545 fprintf (file, "%s { ", str);
546 for (i = 0; i < num_edges; i++)
547 fprintf (file, "%d->%d ", edge_list[i]->src->index,
548 edge_list[i]->dest->index);
550 fputs ("}\n", file);
554 /* This routine will remove any fake predecessor edges for a basic block.
555 When the edge is removed, it is also removed from whatever successor
556 list it is in. */
558 static void
559 remove_fake_predecessors (basic_block bb)
561 edge e;
562 edge_iterator ei;
564 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
566 if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
567 remove_edge (e);
568 else
569 ei_next (&ei);
573 /* This routine will remove all fake edges from the flow graph. If
574 we remove all fake successors, it will automatically remove all
575 fake predecessors. */
577 void
578 remove_fake_edges (void)
580 basic_block bb;
582 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
583 remove_fake_predecessors (bb);
586 /* This routine will remove all fake edges to the EXIT_BLOCK. */
588 void
589 remove_fake_exit_edges (void)
591 remove_fake_predecessors (EXIT_BLOCK_PTR);
595 /* This function will add a fake edge between any block which has no
596 successors, and the exit block. Some data flow equations require these
597 edges to exist. */
599 void
600 add_noreturn_fake_exit_edges (void)
602 basic_block bb;
604 FOR_EACH_BB (bb)
605 if (EDGE_COUNT (bb->succs) == 0)
606 make_single_succ_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
609 /* This function adds a fake edge between any infinite loops to the
610 exit block. Some optimizations require a path from each node to
611 the exit node.
613 See also Morgan, Figure 3.10, pp. 82-83.
615 The current implementation is ugly, not attempting to minimize the
616 number of inserted fake edges. To reduce the number of fake edges
617 to insert, add fake edges from _innermost_ loops containing only
618 nodes not reachable from the exit block. */
620 void
621 connect_infinite_loops_to_exit (void)
623 basic_block unvisited_block = EXIT_BLOCK_PTR;
624 struct depth_first_search_dsS dfs_ds;
626 /* Perform depth-first search in the reverse graph to find nodes
627 reachable from the exit block. */
628 flow_dfs_compute_reverse_init (&dfs_ds);
629 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
631 /* Repeatedly add fake edges, updating the unreachable nodes. */
632 while (1)
634 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
635 unvisited_block);
636 if (!unvisited_block)
637 break;
639 make_edge (unvisited_block, EXIT_BLOCK_PTR, EDGE_FAKE);
640 flow_dfs_compute_reverse_add_bb (&dfs_ds, unvisited_block);
643 flow_dfs_compute_reverse_finish (&dfs_ds);
644 return;
647 /* Compute reverse top sort order.
648 This is computing a post order numbering of the graph. */
651 post_order_compute (int *post_order, bool include_entry_exit)
653 edge_iterator *stack;
654 int sp;
655 int post_order_num = 0;
656 sbitmap visited;
658 if (include_entry_exit)
659 post_order[post_order_num++] = EXIT_BLOCK;
661 /* Allocate stack for back-tracking up CFG. */
662 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
663 sp = 0;
665 /* Allocate bitmap to track nodes that have been visited. */
666 visited = sbitmap_alloc (last_basic_block);
668 /* None of the nodes in the CFG have been visited yet. */
669 sbitmap_zero (visited);
671 /* Push the first edge on to the stack. */
672 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
674 while (sp)
676 edge_iterator ei;
677 basic_block src;
678 basic_block dest;
680 /* Look at the edge on the top of the stack. */
681 ei = stack[sp - 1];
682 src = ei_edge (ei)->src;
683 dest = ei_edge (ei)->dest;
685 /* Check if the edge destination has been visited yet. */
686 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
688 /* Mark that we have visited the destination. */
689 SET_BIT (visited, dest->index);
691 if (EDGE_COUNT (dest->succs) > 0)
692 /* Since the DEST node has been visited for the first
693 time, check its successors. */
694 stack[sp++] = ei_start (dest->succs);
695 else
696 post_order[post_order_num++] = dest->index;
698 else
700 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR)
701 post_order[post_order_num++] = src->index;
703 if (!ei_one_before_end_p (ei))
704 ei_next (&stack[sp - 1]);
705 else
706 sp--;
710 if (include_entry_exit)
711 post_order[post_order_num++] = ENTRY_BLOCK;
713 free (stack);
714 sbitmap_free (visited);
715 return post_order_num;
718 /* Compute the depth first search order and store in the array
719 PRE_ORDER if nonzero, marking the nodes visited in VISITED. If
720 REV_POST_ORDER is nonzero, return the reverse completion number for each
721 node. Returns the number of nodes visited. A depth first search
722 tries to get as far away from the starting point as quickly as
723 possible.
725 pre_order is a really a preorder numbering of the graph.
726 rev_post_order is really a reverse postorder numbering of the graph.
730 pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
731 bool include_entry_exit)
733 edge_iterator *stack;
734 int sp;
735 int pre_order_num = 0;
736 int rev_post_order_num = n_basic_blocks - 1;
737 sbitmap visited;
739 /* Allocate stack for back-tracking up CFG. */
740 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
741 sp = 0;
743 if (include_entry_exit)
745 if (pre_order)
746 pre_order[pre_order_num] = ENTRY_BLOCK;
747 pre_order_num++;
748 if (rev_post_order)
749 rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
751 else
752 rev_post_order_num -= NUM_FIXED_BLOCKS;
754 /* Allocate bitmap to track nodes that have been visited. */
755 visited = sbitmap_alloc (last_basic_block);
757 /* None of the nodes in the CFG have been visited yet. */
758 sbitmap_zero (visited);
760 /* Push the first edge on to the stack. */
761 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
763 while (sp)
765 edge_iterator ei;
766 basic_block src;
767 basic_block dest;
769 /* Look at the edge on the top of the stack. */
770 ei = stack[sp - 1];
771 src = ei_edge (ei)->src;
772 dest = ei_edge (ei)->dest;
774 /* Check if the edge destination has been visited yet. */
775 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
777 /* Mark that we have visited the destination. */
778 SET_BIT (visited, dest->index);
780 if (pre_order)
781 pre_order[pre_order_num] = dest->index;
783 pre_order_num++;
785 if (EDGE_COUNT (dest->succs) > 0)
786 /* Since the DEST node has been visited for the first
787 time, check its successors. */
788 stack[sp++] = ei_start (dest->succs);
789 else if (rev_post_order)
790 /* There are no successors for the DEST node so assign
791 its reverse completion number. */
792 rev_post_order[rev_post_order_num--] = dest->index;
794 else
796 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR
797 && rev_post_order)
798 /* There are no more successors for the SRC node
799 so assign its reverse completion number. */
800 rev_post_order[rev_post_order_num--] = src->index;
802 if (!ei_one_before_end_p (ei))
803 ei_next (&stack[sp - 1]);
804 else
805 sp--;
809 free (stack);
810 sbitmap_free (visited);
812 if (include_entry_exit)
814 if (pre_order)
815 pre_order[pre_order_num] = EXIT_BLOCK;
816 pre_order_num++;
817 if (rev_post_order)
818 rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
819 /* The number of nodes visited should be the number of blocks. */
820 gcc_assert (pre_order_num == n_basic_blocks);
822 else
823 /* The number of nodes visited should be the number of blocks minus
824 the entry and exit blocks which are not visited here. */
825 gcc_assert (pre_order_num == n_basic_blocks - NUM_FIXED_BLOCKS);
827 return pre_order_num;
830 /* Compute the depth first search order on the _reverse_ graph and
831 store in the array DFS_ORDER, marking the nodes visited in VISITED.
832 Returns the number of nodes visited.
834 The computation is split into three pieces:
836 flow_dfs_compute_reverse_init () creates the necessary data
837 structures.
839 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
840 structures. The block will start the search.
842 flow_dfs_compute_reverse_execute () continues (or starts) the
843 search using the block on the top of the stack, stopping when the
844 stack is empty.
846 flow_dfs_compute_reverse_finish () destroys the necessary data
847 structures.
849 Thus, the user will probably call ..._init(), call ..._add_bb() to
850 add a beginning basic block to the stack, call ..._execute(),
851 possibly add another bb to the stack and again call ..._execute(),
852 ..., and finally call _finish(). */
854 /* Initialize the data structures used for depth-first search on the
855 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
856 added to the basic block stack. DATA is the current depth-first
857 search context. If INITIALIZE_STACK is nonzero, there is an
858 element on the stack. */
860 static void
861 flow_dfs_compute_reverse_init (depth_first_search_ds data)
863 /* Allocate stack for back-tracking up CFG. */
864 data->stack = XNEWVEC (basic_block, n_basic_blocks);
865 data->sp = 0;
867 /* Allocate bitmap to track nodes that have been visited. */
868 data->visited_blocks = sbitmap_alloc (last_basic_block);
870 /* None of the nodes in the CFG have been visited yet. */
871 sbitmap_zero (data->visited_blocks);
873 return;
876 /* Add the specified basic block to the top of the dfs data
877 structures. When the search continues, it will start at the
878 block. */
880 static void
881 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
883 data->stack[data->sp++] = bb;
884 SET_BIT (data->visited_blocks, bb->index);
887 /* Continue the depth-first search through the reverse graph starting with the
888 block at the stack's top and ending when the stack is empty. Visited nodes
889 are marked. Returns an unvisited basic block, or NULL if there is none
890 available. */
892 static basic_block
893 flow_dfs_compute_reverse_execute (depth_first_search_ds data,
894 basic_block last_unvisited)
896 basic_block bb;
897 edge e;
898 edge_iterator ei;
900 while (data->sp > 0)
902 bb = data->stack[--data->sp];
904 /* Perform depth-first search on adjacent vertices. */
905 FOR_EACH_EDGE (e, ei, bb->preds)
906 if (!TEST_BIT (data->visited_blocks, e->src->index))
907 flow_dfs_compute_reverse_add_bb (data, e->src);
910 /* Determine if there are unvisited basic blocks. */
911 FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
912 if (!TEST_BIT (data->visited_blocks, bb->index))
913 return bb;
915 return NULL;
918 /* Destroy the data structures needed for depth-first search on the
919 reverse graph. */
921 static void
922 flow_dfs_compute_reverse_finish (depth_first_search_ds data)
924 free (data->stack);
925 sbitmap_free (data->visited_blocks);
928 /* Performs dfs search from BB over vertices satisfying PREDICATE;
929 if REVERSE, go against direction of edges. Returns number of blocks
930 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
932 dfs_enumerate_from (basic_block bb, int reverse,
933 bool (*predicate) (basic_block, void *),
934 basic_block *rslt, int rslt_max, void *data)
936 basic_block *st, lbb;
937 int sp = 0, tv = 0;
938 unsigned size;
940 /* A bitmap to keep track of visited blocks. Allocating it each time
941 this function is called is not possible, since dfs_enumerate_from
942 is often used on small (almost) disjoint parts of cfg (bodies of
943 loops), and allocating a large sbitmap would lead to quadratic
944 behavior. */
945 static sbitmap visited;
946 static unsigned v_size;
948 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
949 #define UNMARK_VISITED(BB) (RESET_BIT (visited, (BB)->index))
950 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
952 /* Resize the VISITED sbitmap if necessary. */
953 size = last_basic_block;
954 if (size < 10)
955 size = 10;
957 if (!visited)
960 visited = sbitmap_alloc (size);
961 sbitmap_zero (visited);
962 v_size = size;
964 else if (v_size < size)
966 /* Ensure that we increase the size of the sbitmap exponentially. */
967 if (2 * v_size > size)
968 size = 2 * v_size;
970 visited = sbitmap_resize (visited, size, 0);
971 v_size = size;
974 st = XCNEWVEC (basic_block, rslt_max);
975 rslt[tv++] = st[sp++] = bb;
976 MARK_VISITED (bb);
977 while (sp)
979 edge e;
980 edge_iterator ei;
981 lbb = st[--sp];
982 if (reverse)
984 FOR_EACH_EDGE (e, ei, lbb->preds)
985 if (!VISITED_P (e->src) && predicate (e->src, data))
987 gcc_assert (tv != rslt_max);
988 rslt[tv++] = st[sp++] = e->src;
989 MARK_VISITED (e->src);
992 else
994 FOR_EACH_EDGE (e, ei, lbb->succs)
995 if (!VISITED_P (e->dest) && predicate (e->dest, data))
997 gcc_assert (tv != rslt_max);
998 rslt[tv++] = st[sp++] = e->dest;
999 MARK_VISITED (e->dest);
1003 free (st);
1004 for (sp = 0; sp < tv; sp++)
1005 UNMARK_VISITED (rslt[sp]);
1006 return tv;
1007 #undef MARK_VISITED
1008 #undef UNMARK_VISITED
1009 #undef VISITED_P
1013 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1015 This algorithm can be found in Timothy Harvey's PhD thesis, at
1016 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1017 dominance algorithms.
1019 First, we identify each join point, j (any node with more than one
1020 incoming edge is a join point).
1022 We then examine each predecessor, p, of j and walk up the dominator tree
1023 starting at p.
1025 We stop the walk when we reach j's immediate dominator - j is in the
1026 dominance frontier of each of the nodes in the walk, except for j's
1027 immediate dominator. Intuitively, all of the rest of j's dominators are
1028 shared by j's predecessors as well.
1029 Since they dominate j, they will not have j in their dominance frontiers.
1031 The number of nodes touched by this algorithm is equal to the size
1032 of the dominance frontiers, no more, no less.
1036 static void
1037 compute_dominance_frontiers_1 (bitmap *frontiers)
1039 edge p;
1040 edge_iterator ei;
1041 basic_block b;
1042 FOR_EACH_BB (b)
1044 if (EDGE_COUNT (b->preds) >= 2)
1046 FOR_EACH_EDGE (p, ei, b->preds)
1048 basic_block runner = p->src;
1049 basic_block domsb;
1050 if (runner == ENTRY_BLOCK_PTR)
1051 continue;
1053 domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1054 while (runner != domsb)
1056 if (bitmap_bit_p (frontiers[runner->index], b->index))
1057 break;
1058 bitmap_set_bit (frontiers[runner->index],
1059 b->index);
1060 runner = get_immediate_dominator (CDI_DOMINATORS,
1061 runner);
1069 void
1070 compute_dominance_frontiers (bitmap *frontiers)
1072 timevar_push (TV_DOM_FRONTIERS);
1074 compute_dominance_frontiers_1 (frontiers);
1076 timevar_pop (TV_DOM_FRONTIERS);