* config/rl78/mulsi3.S: Remove a few unneeded moves and branches.
[official-gcc.git] / gcc / cfganal.c
blobc4ea7dd0a1f2318e5c0475c07bc768470e387edf
1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains various simple utilities to analyze the CFG. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "basic-block.h"
26 #include "vec.h"
27 #include "bitmap.h"
28 #include "sbitmap.h"
29 #include "timevar.h"
31 /* Store the data structures necessary for depth-first search. */
32 struct depth_first_search_dsS {
33 /* stack for backtracking during the algorithm */
34 basic_block *stack;
36 /* number of edges in the stack. That is, positions 0, ..., sp-1
37 have edges. */
38 unsigned int sp;
40 /* record of basic blocks already seen by depth-first search */
41 sbitmap visited_blocks;
43 typedef struct depth_first_search_dsS *depth_first_search_ds;
45 static void flow_dfs_compute_reverse_init (depth_first_search_ds);
46 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds,
47 basic_block);
48 static basic_block flow_dfs_compute_reverse_execute (depth_first_search_ds,
49 basic_block);
50 static void flow_dfs_compute_reverse_finish (depth_first_search_ds);
52 /* Mark the back edges in DFS traversal.
53 Return nonzero if a loop (natural or otherwise) is present.
54 Inspired by Depth_First_Search_PP described in:
56 Advanced Compiler Design and Implementation
57 Steven Muchnick
58 Morgan Kaufmann, 1997
60 and heavily borrowed from pre_and_rev_post_order_compute. */
62 bool
63 mark_dfs_back_edges (void)
65 edge_iterator *stack;
66 int *pre;
67 int *post;
68 int sp;
69 int prenum = 1;
70 int postnum = 1;
71 sbitmap visited;
72 bool found = false;
74 /* Allocate the preorder and postorder number arrays. */
75 pre = XCNEWVEC (int, last_basic_block);
76 post = XCNEWVEC (int, last_basic_block);
78 /* Allocate stack for back-tracking up CFG. */
79 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
80 sp = 0;
82 /* Allocate bitmap to track nodes that have been visited. */
83 visited = sbitmap_alloc (last_basic_block);
85 /* None of the nodes in the CFG have been visited yet. */
86 bitmap_clear (visited);
88 /* Push the first edge on to the stack. */
89 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
91 while (sp)
93 edge_iterator ei;
94 basic_block src;
95 basic_block dest;
97 /* Look at the edge on the top of the stack. */
98 ei = stack[sp - 1];
99 src = ei_edge (ei)->src;
100 dest = ei_edge (ei)->dest;
101 ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
103 /* Check if the edge destination has been visited yet. */
104 if (dest != EXIT_BLOCK_PTR && ! bitmap_bit_p (visited, dest->index))
106 /* Mark that we have visited the destination. */
107 bitmap_set_bit (visited, dest->index);
109 pre[dest->index] = prenum++;
110 if (EDGE_COUNT (dest->succs) > 0)
112 /* Since the DEST node has been visited for the first
113 time, check its successors. */
114 stack[sp++] = ei_start (dest->succs);
116 else
117 post[dest->index] = postnum++;
119 else
121 if (dest != EXIT_BLOCK_PTR && src != ENTRY_BLOCK_PTR
122 && pre[src->index] >= pre[dest->index]
123 && post[dest->index] == 0)
124 ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
126 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR)
127 post[src->index] = postnum++;
129 if (!ei_one_before_end_p (ei))
130 ei_next (&stack[sp - 1]);
131 else
132 sp--;
136 free (pre);
137 free (post);
138 free (stack);
139 sbitmap_free (visited);
141 return found;
144 /* Find unreachable blocks. An unreachable block will have 0 in
145 the reachable bit in block->flags. A nonzero value indicates the
146 block is reachable. */
148 void
149 find_unreachable_blocks (void)
151 edge e;
152 edge_iterator ei;
153 basic_block *tos, *worklist, bb;
155 tos = worklist = XNEWVEC (basic_block, n_basic_blocks);
157 /* Clear all the reachability flags. */
159 FOR_EACH_BB (bb)
160 bb->flags &= ~BB_REACHABLE;
162 /* Add our starting points to the worklist. Almost always there will
163 be only one. It isn't inconceivable that we might one day directly
164 support Fortran alternate entry points. */
166 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
168 *tos++ = e->dest;
170 /* Mark the block reachable. */
171 e->dest->flags |= BB_REACHABLE;
174 /* Iterate: find everything reachable from what we've already seen. */
176 while (tos != worklist)
178 basic_block b = *--tos;
180 FOR_EACH_EDGE (e, ei, b->succs)
182 basic_block dest = e->dest;
184 if (!(dest->flags & BB_REACHABLE))
186 *tos++ = dest;
187 dest->flags |= BB_REACHABLE;
192 free (worklist);
195 /* Functions to access an edge list with a vector representation.
196 Enough data is kept such that given an index number, the
197 pred and succ that edge represents can be determined, or
198 given a pred and a succ, its index number can be returned.
199 This allows algorithms which consume a lot of memory to
200 represent the normally full matrix of edge (pred,succ) with a
201 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
202 wasted space in the client code due to sparse flow graphs. */
204 /* This functions initializes the edge list. Basically the entire
205 flowgraph is processed, and all edges are assigned a number,
206 and the data structure is filled in. */
208 struct edge_list *
209 create_edge_list (void)
211 struct edge_list *elist;
212 edge e;
213 int num_edges;
214 basic_block bb;
215 edge_iterator ei;
217 /* Determine the number of edges in the flow graph by counting successor
218 edges on each basic block. */
219 num_edges = 0;
220 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
222 num_edges += EDGE_COUNT (bb->succs);
225 elist = XNEW (struct edge_list);
226 elist->num_edges = num_edges;
227 elist->index_to_edge = XNEWVEC (edge, num_edges);
229 num_edges = 0;
231 /* Follow successors of blocks, and register these edges. */
232 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
233 FOR_EACH_EDGE (e, ei, bb->succs)
234 elist->index_to_edge[num_edges++] = e;
236 return elist;
239 /* This function free's memory associated with an edge list. */
241 void
242 free_edge_list (struct edge_list *elist)
244 if (elist)
246 free (elist->index_to_edge);
247 free (elist);
251 /* This function provides debug output showing an edge list. */
253 DEBUG_FUNCTION void
254 print_edge_list (FILE *f, struct edge_list *elist)
256 int x;
258 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
259 n_basic_blocks, elist->num_edges);
261 for (x = 0; x < elist->num_edges; x++)
263 fprintf (f, " %-4d - edge(", x);
264 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
265 fprintf (f, "entry,");
266 else
267 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
269 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
270 fprintf (f, "exit)\n");
271 else
272 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
276 /* This function provides an internal consistency check of an edge list,
277 verifying that all edges are present, and that there are no
278 extra edges. */
280 DEBUG_FUNCTION void
281 verify_edge_list (FILE *f, struct edge_list *elist)
283 int pred, succ, index;
284 edge e;
285 basic_block bb, p, s;
286 edge_iterator ei;
288 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
290 FOR_EACH_EDGE (e, ei, bb->succs)
292 pred = e->src->index;
293 succ = e->dest->index;
294 index = EDGE_INDEX (elist, e->src, e->dest);
295 if (index == EDGE_INDEX_NO_EDGE)
297 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
298 continue;
301 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
302 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
303 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
304 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
305 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
306 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
310 /* We've verified that all the edges are in the list, now lets make sure
311 there are no spurious edges in the list. This is an expensive check! */
313 FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
314 FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
316 int found_edge = 0;
318 FOR_EACH_EDGE (e, ei, p->succs)
319 if (e->dest == s)
321 found_edge = 1;
322 break;
325 FOR_EACH_EDGE (e, ei, s->preds)
326 if (e->src == p)
328 found_edge = 1;
329 break;
332 if (EDGE_INDEX (elist, p, s)
333 == EDGE_INDEX_NO_EDGE && found_edge != 0)
334 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
335 p->index, s->index);
336 if (EDGE_INDEX (elist, p, s)
337 != EDGE_INDEX_NO_EDGE && found_edge == 0)
338 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
339 p->index, s->index, EDGE_INDEX (elist, p, s));
344 /* Functions to compute control dependences. */
346 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
347 void
348 control_dependences::set_control_dependence_map_bit (basic_block bb,
349 int edge_index)
351 if (bb == ENTRY_BLOCK_PTR)
352 return;
353 gcc_assert (bb != EXIT_BLOCK_PTR);
354 bitmap_set_bit (control_dependence_map[bb->index], edge_index);
357 /* Clear all control dependences for block BB. */
358 void
359 control_dependences::clear_control_dependence_bitmap (basic_block bb)
361 bitmap_clear (control_dependence_map[bb->index]);
364 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
365 This function is necessary because some blocks have negative numbers. */
367 static inline basic_block
368 find_pdom (basic_block block)
370 gcc_assert (block != ENTRY_BLOCK_PTR);
372 if (block == EXIT_BLOCK_PTR)
373 return EXIT_BLOCK_PTR;
374 else
376 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
377 if (! bb)
378 return EXIT_BLOCK_PTR;
379 return bb;
383 /* Determine all blocks' control dependences on the given edge with edge_list
384 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
386 void
387 control_dependences::find_control_dependence (int edge_index)
389 basic_block current_block;
390 basic_block ending_block;
392 gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR);
394 if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR)
395 ending_block = single_succ (ENTRY_BLOCK_PTR);
396 else
397 ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index));
399 for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index);
400 current_block != ending_block && current_block != EXIT_BLOCK_PTR;
401 current_block = find_pdom (current_block))
403 edge e = INDEX_EDGE (el, edge_index);
405 /* For abnormal edges, we don't make current_block control
406 dependent because instructions that throw are always necessary
407 anyway. */
408 if (e->flags & EDGE_ABNORMAL)
409 continue;
411 set_control_dependence_map_bit (current_block, edge_index);
415 /* Record all blocks' control dependences on all edges in the edge
416 list EL, ala Morgan, Section 3.6. */
418 control_dependences::control_dependences (struct edge_list *edges)
419 : el (edges)
421 timevar_push (TV_CONTROL_DEPENDENCES);
422 control_dependence_map.create (last_basic_block);
423 for (int i = 0; i < last_basic_block; ++i)
424 control_dependence_map.quick_push (BITMAP_ALLOC (NULL));
425 for (int i = 0; i < NUM_EDGES (el); ++i)
426 find_control_dependence (i);
427 timevar_pop (TV_CONTROL_DEPENDENCES);
430 /* Free control dependences and the associated edge list. */
432 control_dependences::~control_dependences ()
434 for (unsigned i = 0; i < control_dependence_map.length (); ++i)
435 BITMAP_FREE (control_dependence_map[i]);
436 control_dependence_map.release ();
437 free_edge_list (el);
440 /* Returns the bitmap of edges the basic-block I is dependent on. */
442 bitmap
443 control_dependences::get_edges_dependent_on (int i)
445 return control_dependence_map[i];
448 /* Returns the edge with index I from the edge list. */
450 edge
451 control_dependences::get_edge (int i)
453 return INDEX_EDGE (el, i);
457 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
458 If no such edge exists, return NULL. */
460 edge
461 find_edge (basic_block pred, basic_block succ)
463 edge e;
464 edge_iterator ei;
466 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
468 FOR_EACH_EDGE (e, ei, pred->succs)
469 if (e->dest == succ)
470 return e;
472 else
474 FOR_EACH_EDGE (e, ei, succ->preds)
475 if (e->src == pred)
476 return e;
479 return NULL;
482 /* This routine will determine what, if any, edge there is between
483 a specified predecessor and successor. */
486 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
488 int x;
490 for (x = 0; x < NUM_EDGES (edge_list); x++)
491 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
492 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
493 return x;
495 return (EDGE_INDEX_NO_EDGE);
498 /* This routine will remove any fake predecessor edges for a basic block.
499 When the edge is removed, it is also removed from whatever successor
500 list it is in. */
502 static void
503 remove_fake_predecessors (basic_block bb)
505 edge e;
506 edge_iterator ei;
508 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
510 if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
511 remove_edge (e);
512 else
513 ei_next (&ei);
517 /* This routine will remove all fake edges from the flow graph. If
518 we remove all fake successors, it will automatically remove all
519 fake predecessors. */
521 void
522 remove_fake_edges (void)
524 basic_block bb;
526 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
527 remove_fake_predecessors (bb);
530 /* This routine will remove all fake edges to the EXIT_BLOCK. */
532 void
533 remove_fake_exit_edges (void)
535 remove_fake_predecessors (EXIT_BLOCK_PTR);
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 (EDGE_COUNT (bb->succs) == 0)
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 = EXIT_BLOCK_PTR;
568 basic_block deadend_block;
569 struct depth_first_search_dsS dfs_ds;
571 /* Perform depth-first search in the reverse graph to find nodes
572 reachable from the exit block. */
573 flow_dfs_compute_reverse_init (&dfs_ds);
574 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
576 /* Repeatedly add fake edges, updating the unreachable nodes. */
577 while (1)
579 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
580 unvisited_block);
581 if (!unvisited_block)
582 break;
584 deadend_block = dfs_find_deadend (unvisited_block);
585 make_edge (deadend_block, EXIT_BLOCK_PTR, EDGE_FAKE);
586 flow_dfs_compute_reverse_add_bb (&dfs_ds, deadend_block);
589 flow_dfs_compute_reverse_finish (&dfs_ds);
590 return;
593 /* Compute reverse top sort order. This is computing a post order
594 numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then
595 ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
596 true, unreachable blocks are deleted. */
599 post_order_compute (int *post_order, bool include_entry_exit,
600 bool delete_unreachable)
602 edge_iterator *stack;
603 int sp;
604 int post_order_num = 0;
605 sbitmap visited;
606 int count;
608 if (include_entry_exit)
609 post_order[post_order_num++] = EXIT_BLOCK;
611 /* Allocate stack for back-tracking up CFG. */
612 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
613 sp = 0;
615 /* Allocate bitmap to track nodes that have been visited. */
616 visited = sbitmap_alloc (last_basic_block);
618 /* None of the nodes in the CFG have been visited yet. */
619 bitmap_clear (visited);
621 /* Push the first edge on to the stack. */
622 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
624 while (sp)
626 edge_iterator ei;
627 basic_block src;
628 basic_block dest;
630 /* Look at the edge on the top of the stack. */
631 ei = stack[sp - 1];
632 src = ei_edge (ei)->src;
633 dest = ei_edge (ei)->dest;
635 /* Check if the edge destination has been visited yet. */
636 if (dest != EXIT_BLOCK_PTR && ! bitmap_bit_p (visited, dest->index))
638 /* Mark that we have visited the destination. */
639 bitmap_set_bit (visited, dest->index);
641 if (EDGE_COUNT (dest->succs) > 0)
642 /* Since the DEST node has been visited for the first
643 time, check its successors. */
644 stack[sp++] = ei_start (dest->succs);
645 else
646 post_order[post_order_num++] = dest->index;
648 else
650 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR)
651 post_order[post_order_num++] = src->index;
653 if (!ei_one_before_end_p (ei))
654 ei_next (&stack[sp - 1]);
655 else
656 sp--;
660 if (include_entry_exit)
662 post_order[post_order_num++] = ENTRY_BLOCK;
663 count = post_order_num;
665 else
666 count = post_order_num + 2;
668 /* Delete the unreachable blocks if some were found and we are
669 supposed to do it. */
670 if (delete_unreachable && (count != n_basic_blocks))
672 basic_block b;
673 basic_block next_bb;
674 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
676 next_bb = b->next_bb;
678 if (!(bitmap_bit_p (visited, b->index)))
679 delete_basic_block (b);
682 tidy_fallthru_edges ();
685 free (stack);
686 sbitmap_free (visited);
687 return post_order_num;
691 /* Helper routine for inverted_post_order_compute
692 flow_dfs_compute_reverse_execute, and the reverse-CFG
693 deapth first search in dominance.c.
694 BB has to belong to a region of CFG
695 unreachable by inverted traversal from the exit.
696 i.e. there's no control flow path from ENTRY to EXIT
697 that contains this BB.
698 This can happen in two cases - if there's an infinite loop
699 or if there's a block that has no successor
700 (call to a function with no return).
701 Some RTL passes deal with this condition by
702 calling connect_infinite_loops_to_exit () and/or
703 add_noreturn_fake_exit_edges ().
704 However, those methods involve modifying the CFG itself
705 which may not be desirable.
706 Hence, we deal with the infinite loop/no return cases
707 by identifying a unique basic block that can reach all blocks
708 in such a region by inverted traversal.
709 This function returns a basic block that guarantees
710 that all blocks in the region are reachable
711 by starting an inverted traversal from the returned block. */
713 basic_block
714 dfs_find_deadend (basic_block bb)
716 bitmap visited = BITMAP_ALLOC (NULL);
718 for (;;)
720 if (EDGE_COUNT (bb->succs) == 0
721 || ! bitmap_set_bit (visited, bb->index))
723 BITMAP_FREE (visited);
724 return bb;
727 bb = EDGE_SUCC (bb, 0)->dest;
730 gcc_unreachable ();
734 /* Compute the reverse top sort order of the inverted CFG
735 i.e. starting from the exit block and following the edges backward
736 (from successors to predecessors).
737 This ordering can be used for forward dataflow problems among others.
739 This function assumes that all blocks in the CFG are reachable
740 from the ENTRY (but not necessarily from EXIT).
742 If there's an infinite loop,
743 a simple inverted traversal starting from the blocks
744 with no successors can't visit all blocks.
745 To solve this problem, we first do inverted traversal
746 starting from the blocks with no successor.
747 And if there's any block left that's not visited by the regular
748 inverted traversal from EXIT,
749 those blocks are in such problematic region.
750 Among those, we find one block that has
751 any visited predecessor (which is an entry into such a region),
752 and start looking for a "dead end" from that block
753 and do another inverted traversal from that block. */
756 inverted_post_order_compute (int *post_order)
758 basic_block bb;
759 edge_iterator *stack;
760 int sp;
761 int post_order_num = 0;
762 sbitmap visited;
764 /* Allocate stack for back-tracking up CFG. */
765 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
766 sp = 0;
768 /* Allocate bitmap to track nodes that have been visited. */
769 visited = sbitmap_alloc (last_basic_block);
771 /* None of the nodes in the CFG have been visited yet. */
772 bitmap_clear (visited);
774 /* Put all blocks that have no successor into the initial work list. */
775 FOR_ALL_BB (bb)
776 if (EDGE_COUNT (bb->succs) == 0)
778 /* Push the initial edge on to the stack. */
779 if (EDGE_COUNT (bb->preds) > 0)
781 stack[sp++] = ei_start (bb->preds);
782 bitmap_set_bit (visited, bb->index);
788 bool has_unvisited_bb = false;
790 /* The inverted traversal loop. */
791 while (sp)
793 edge_iterator ei;
794 basic_block pred;
796 /* Look at the edge on the top of the stack. */
797 ei = stack[sp - 1];
798 bb = ei_edge (ei)->dest;
799 pred = ei_edge (ei)->src;
801 /* Check if the predecessor has been visited yet. */
802 if (! bitmap_bit_p (visited, pred->index))
804 /* Mark that we have visited the destination. */
805 bitmap_set_bit (visited, pred->index);
807 if (EDGE_COUNT (pred->preds) > 0)
808 /* Since the predecessor node has been visited for the first
809 time, check its predecessors. */
810 stack[sp++] = ei_start (pred->preds);
811 else
812 post_order[post_order_num++] = pred->index;
814 else
816 if (bb != EXIT_BLOCK_PTR && ei_one_before_end_p (ei))
817 post_order[post_order_num++] = bb->index;
819 if (!ei_one_before_end_p (ei))
820 ei_next (&stack[sp - 1]);
821 else
822 sp--;
826 /* Detect any infinite loop and activate the kludge.
827 Note that this doesn't check EXIT_BLOCK itself
828 since EXIT_BLOCK is always added after the outer do-while loop. */
829 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
830 if (!bitmap_bit_p (visited, bb->index))
832 has_unvisited_bb = true;
834 if (EDGE_COUNT (bb->preds) > 0)
836 edge_iterator ei;
837 edge e;
838 basic_block visited_pred = NULL;
840 /* Find an already visited predecessor. */
841 FOR_EACH_EDGE (e, ei, bb->preds)
843 if (bitmap_bit_p (visited, e->src->index))
844 visited_pred = e->src;
847 if (visited_pred)
849 basic_block be = dfs_find_deadend (bb);
850 gcc_assert (be != NULL);
851 bitmap_set_bit (visited, be->index);
852 stack[sp++] = ei_start (be->preds);
853 break;
858 if (has_unvisited_bb && sp == 0)
860 /* No blocks are reachable from EXIT at all.
861 Find a dead-end from the ENTRY, and restart the iteration. */
862 basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR);
863 gcc_assert (be != NULL);
864 bitmap_set_bit (visited, be->index);
865 stack[sp++] = ei_start (be->preds);
868 /* The only case the below while fires is
869 when there's an infinite loop. */
871 while (sp);
873 /* EXIT_BLOCK is always included. */
874 post_order[post_order_num++] = EXIT_BLOCK;
876 free (stack);
877 sbitmap_free (visited);
878 return post_order_num;
881 /* Compute the depth first search order and store in the array
882 PRE_ORDER if nonzero, marking the nodes visited in VISITED. If
883 REV_POST_ORDER is nonzero, return the reverse completion number for each
884 node. Returns the number of nodes visited. A depth first search
885 tries to get as far away from the starting point as quickly as
886 possible.
888 pre_order is a really a preorder numbering of the graph.
889 rev_post_order is really a reverse postorder numbering of the graph.
893 pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
894 bool include_entry_exit)
896 edge_iterator *stack;
897 int sp;
898 int pre_order_num = 0;
899 int rev_post_order_num = n_basic_blocks - 1;
900 sbitmap visited;
902 /* Allocate stack for back-tracking up CFG. */
903 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
904 sp = 0;
906 if (include_entry_exit)
908 if (pre_order)
909 pre_order[pre_order_num] = ENTRY_BLOCK;
910 pre_order_num++;
911 if (rev_post_order)
912 rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
914 else
915 rev_post_order_num -= NUM_FIXED_BLOCKS;
917 /* Allocate bitmap to track nodes that have been visited. */
918 visited = sbitmap_alloc (last_basic_block);
920 /* None of the nodes in the CFG have been visited yet. */
921 bitmap_clear (visited);
923 /* Push the first edge on to the stack. */
924 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
926 while (sp)
928 edge_iterator ei;
929 basic_block src;
930 basic_block dest;
932 /* Look at the edge on the top of the stack. */
933 ei = stack[sp - 1];
934 src = ei_edge (ei)->src;
935 dest = ei_edge (ei)->dest;
937 /* Check if the edge destination has been visited yet. */
938 if (dest != EXIT_BLOCK_PTR && ! bitmap_bit_p (visited, dest->index))
940 /* Mark that we have visited the destination. */
941 bitmap_set_bit (visited, dest->index);
943 if (pre_order)
944 pre_order[pre_order_num] = dest->index;
946 pre_order_num++;
948 if (EDGE_COUNT (dest->succs) > 0)
949 /* Since the DEST node has been visited for the first
950 time, check its successors. */
951 stack[sp++] = ei_start (dest->succs);
952 else if (rev_post_order)
953 /* There are no successors for the DEST node so assign
954 its reverse completion number. */
955 rev_post_order[rev_post_order_num--] = dest->index;
957 else
959 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR
960 && rev_post_order)
961 /* There are no more successors for the SRC node
962 so assign its reverse completion number. */
963 rev_post_order[rev_post_order_num--] = src->index;
965 if (!ei_one_before_end_p (ei))
966 ei_next (&stack[sp - 1]);
967 else
968 sp--;
972 free (stack);
973 sbitmap_free (visited);
975 if (include_entry_exit)
977 if (pre_order)
978 pre_order[pre_order_num] = EXIT_BLOCK;
979 pre_order_num++;
980 if (rev_post_order)
981 rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
982 /* The number of nodes visited should be the number of blocks. */
983 gcc_assert (pre_order_num == n_basic_blocks);
985 else
986 /* The number of nodes visited should be the number of blocks minus
987 the entry and exit blocks which are not visited here. */
988 gcc_assert (pre_order_num == n_basic_blocks - NUM_FIXED_BLOCKS);
990 return pre_order_num;
993 /* Compute the depth first search order on the _reverse_ graph and
994 store in the array DFS_ORDER, marking the nodes visited in VISITED.
995 Returns the number of nodes visited.
997 The computation is split into three pieces:
999 flow_dfs_compute_reverse_init () creates the necessary data
1000 structures.
1002 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1003 structures. The block will start the search.
1005 flow_dfs_compute_reverse_execute () continues (or starts) the
1006 search using the block on the top of the stack, stopping when the
1007 stack is empty.
1009 flow_dfs_compute_reverse_finish () destroys the necessary data
1010 structures.
1012 Thus, the user will probably call ..._init(), call ..._add_bb() to
1013 add a beginning basic block to the stack, call ..._execute(),
1014 possibly add another bb to the stack and again call ..._execute(),
1015 ..., and finally call _finish(). */
1017 /* Initialize the data structures used for depth-first search on the
1018 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1019 added to the basic block stack. DATA is the current depth-first
1020 search context. If INITIALIZE_STACK is nonzero, there is an
1021 element on the stack. */
1023 static void
1024 flow_dfs_compute_reverse_init (depth_first_search_ds data)
1026 /* Allocate stack for back-tracking up CFG. */
1027 data->stack = XNEWVEC (basic_block, n_basic_blocks);
1028 data->sp = 0;
1030 /* Allocate bitmap to track nodes that have been visited. */
1031 data->visited_blocks = sbitmap_alloc (last_basic_block);
1033 /* None of the nodes in the CFG have been visited yet. */
1034 bitmap_clear (data->visited_blocks);
1036 return;
1039 /* Add the specified basic block to the top of the dfs data
1040 structures. When the search continues, it will start at the
1041 block. */
1043 static void
1044 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
1046 data->stack[data->sp++] = bb;
1047 bitmap_set_bit (data->visited_blocks, bb->index);
1050 /* Continue the depth-first search through the reverse graph starting with the
1051 block at the stack's top and ending when the stack is empty. Visited nodes
1052 are marked. Returns an unvisited basic block, or NULL if there is none
1053 available. */
1055 static basic_block
1056 flow_dfs_compute_reverse_execute (depth_first_search_ds data,
1057 basic_block last_unvisited)
1059 basic_block bb;
1060 edge e;
1061 edge_iterator ei;
1063 while (data->sp > 0)
1065 bb = data->stack[--data->sp];
1067 /* Perform depth-first search on adjacent vertices. */
1068 FOR_EACH_EDGE (e, ei, bb->preds)
1069 if (!bitmap_bit_p (data->visited_blocks, e->src->index))
1070 flow_dfs_compute_reverse_add_bb (data, e->src);
1073 /* Determine if there are unvisited basic blocks. */
1074 FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
1075 if (!bitmap_bit_p (data->visited_blocks, bb->index))
1076 return bb;
1078 return NULL;
1081 /* Destroy the data structures needed for depth-first search on the
1082 reverse graph. */
1084 static void
1085 flow_dfs_compute_reverse_finish (depth_first_search_ds data)
1087 free (data->stack);
1088 sbitmap_free (data->visited_blocks);
1091 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1092 if REVERSE, go against direction of edges. Returns number of blocks
1093 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
1095 dfs_enumerate_from (basic_block bb, int reverse,
1096 bool (*predicate) (const_basic_block, const void *),
1097 basic_block *rslt, int rslt_max, const void *data)
1099 basic_block *st, lbb;
1100 int sp = 0, tv = 0;
1101 unsigned size;
1103 /* A bitmap to keep track of visited blocks. Allocating it each time
1104 this function is called is not possible, since dfs_enumerate_from
1105 is often used on small (almost) disjoint parts of cfg (bodies of
1106 loops), and allocating a large sbitmap would lead to quadratic
1107 behavior. */
1108 static sbitmap visited;
1109 static unsigned v_size;
1111 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1112 #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
1113 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1115 /* Resize the VISITED sbitmap if necessary. */
1116 size = last_basic_block;
1117 if (size < 10)
1118 size = 10;
1120 if (!visited)
1123 visited = sbitmap_alloc (size);
1124 bitmap_clear (visited);
1125 v_size = size;
1127 else if (v_size < size)
1129 /* Ensure that we increase the size of the sbitmap exponentially. */
1130 if (2 * v_size > size)
1131 size = 2 * v_size;
1133 visited = sbitmap_resize (visited, size, 0);
1134 v_size = size;
1137 st = XNEWVEC (basic_block, rslt_max);
1138 rslt[tv++] = st[sp++] = bb;
1139 MARK_VISITED (bb);
1140 while (sp)
1142 edge e;
1143 edge_iterator ei;
1144 lbb = st[--sp];
1145 if (reverse)
1147 FOR_EACH_EDGE (e, ei, lbb->preds)
1148 if (!VISITED_P (e->src) && predicate (e->src, data))
1150 gcc_assert (tv != rslt_max);
1151 rslt[tv++] = st[sp++] = e->src;
1152 MARK_VISITED (e->src);
1155 else
1157 FOR_EACH_EDGE (e, ei, lbb->succs)
1158 if (!VISITED_P (e->dest) && predicate (e->dest, data))
1160 gcc_assert (tv != rslt_max);
1161 rslt[tv++] = st[sp++] = e->dest;
1162 MARK_VISITED (e->dest);
1166 free (st);
1167 for (sp = 0; sp < tv; sp++)
1168 UNMARK_VISITED (rslt[sp]);
1169 return tv;
1170 #undef MARK_VISITED
1171 #undef UNMARK_VISITED
1172 #undef VISITED_P
1176 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1178 This algorithm can be found in Timothy Harvey's PhD thesis, at
1179 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1180 dominance algorithms.
1182 First, we identify each join point, j (any node with more than one
1183 incoming edge is a join point).
1185 We then examine each predecessor, p, of j and walk up the dominator tree
1186 starting at p.
1188 We stop the walk when we reach j's immediate dominator - j is in the
1189 dominance frontier of each of the nodes in the walk, except for j's
1190 immediate dominator. Intuitively, all of the rest of j's dominators are
1191 shared by j's predecessors as well.
1192 Since they dominate j, they will not have j in their dominance frontiers.
1194 The number of nodes touched by this algorithm is equal to the size
1195 of the dominance frontiers, no more, no less.
1199 static void
1200 compute_dominance_frontiers_1 (bitmap_head *frontiers)
1202 edge p;
1203 edge_iterator ei;
1204 basic_block b;
1205 FOR_EACH_BB (b)
1207 if (EDGE_COUNT (b->preds) >= 2)
1209 FOR_EACH_EDGE (p, ei, b->preds)
1211 basic_block runner = p->src;
1212 basic_block domsb;
1213 if (runner == ENTRY_BLOCK_PTR)
1214 continue;
1216 domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1217 while (runner != domsb)
1219 if (!bitmap_set_bit (&frontiers[runner->index],
1220 b->index))
1221 break;
1222 runner = get_immediate_dominator (CDI_DOMINATORS,
1223 runner);
1231 void
1232 compute_dominance_frontiers (bitmap_head *frontiers)
1234 timevar_push (TV_DOM_FRONTIERS);
1236 compute_dominance_frontiers_1 (frontiers);
1238 timevar_pop (TV_DOM_FRONTIERS);
1241 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1242 return a bitmap with all the blocks in the iterated dominance
1243 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
1244 frontier information as returned by compute_dominance_frontiers.
1246 The resulting set of blocks are the potential sites where PHI nodes
1247 are needed. The caller is responsible for freeing the memory
1248 allocated for the return value. */
1250 bitmap
1251 compute_idf (bitmap def_blocks, bitmap_head *dfs)
1253 bitmap_iterator bi;
1254 unsigned bb_index, i;
1255 vec<int> work_stack;
1256 bitmap phi_insertion_points;
1258 /* Each block can appear at most twice on the work-stack. */
1259 work_stack.create (2 * n_basic_blocks);
1260 phi_insertion_points = BITMAP_ALLOC (NULL);
1262 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1263 vec::quick_push here for speed. This is safe because we know that
1264 the number of definition blocks is no greater than the number of
1265 basic blocks, which is the initial capacity of WORK_STACK. */
1266 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1267 work_stack.quick_push (bb_index);
1269 /* Pop a block off the worklist, add every block that appears in
1270 the original block's DF that we have not already processed to
1271 the worklist. Iterate until the worklist is empty. Blocks
1272 which are added to the worklist are potential sites for
1273 PHI nodes. */
1274 while (work_stack.length () > 0)
1276 bb_index = work_stack.pop ();
1278 /* Since the registration of NEW -> OLD name mappings is done
1279 separately from the call to update_ssa, when updating the SSA
1280 form, the basic blocks where new and/or old names are defined
1281 may have disappeared by CFG cleanup calls. In this case,
1282 we may pull a non-existing block from the work stack. */
1283 gcc_checking_assert (bb_index < (unsigned) last_basic_block);
1285 EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
1286 0, i, bi)
1288 work_stack.quick_push (i);
1289 bitmap_set_bit (phi_insertion_points, i);
1293 work_stack.release ();
1295 return phi_insertion_points;
1298 /* Intersection and union of preds/succs for sbitmap based data flow
1299 solvers. All four functions defined below take the same arguments:
1300 B is the basic block to perform the operation for. DST is the
1301 target sbitmap, i.e. the result. SRC is an sbitmap vector of size
1302 last_basic_block so that it can be indexed with basic block indices.
1303 DST may be (but does not have to be) SRC[B->index]. */
1305 /* Set the bitmap DST to the intersection of SRC of successors of
1306 basic block B. */
1308 void
1309 bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1311 unsigned int set_size = dst->size;
1312 edge e;
1313 unsigned ix;
1315 gcc_assert (!dst->popcount);
1317 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1319 e = EDGE_SUCC (b, ix);
1320 if (e->dest == EXIT_BLOCK_PTR)
1321 continue;
1323 bitmap_copy (dst, src[e->dest->index]);
1324 break;
1327 if (e == 0)
1328 bitmap_ones (dst);
1329 else
1330 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
1332 unsigned int i;
1333 SBITMAP_ELT_TYPE *p, *r;
1335 e = EDGE_SUCC (b, ix);
1336 if (e->dest == EXIT_BLOCK_PTR)
1337 continue;
1339 p = src[e->dest->index]->elms;
1340 r = dst->elms;
1341 for (i = 0; i < set_size; i++)
1342 *r++ &= *p++;
1346 /* Set the bitmap DST to the intersection of SRC of predecessors of
1347 basic block B. */
1349 void
1350 bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1352 unsigned int set_size = dst->size;
1353 edge e;
1354 unsigned ix;
1356 gcc_assert (!dst->popcount);
1358 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1360 e = EDGE_PRED (b, ix);
1361 if (e->src == ENTRY_BLOCK_PTR)
1362 continue;
1364 bitmap_copy (dst, src[e->src->index]);
1365 break;
1368 if (e == 0)
1369 bitmap_ones (dst);
1370 else
1371 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
1373 unsigned int i;
1374 SBITMAP_ELT_TYPE *p, *r;
1376 e = EDGE_PRED (b, ix);
1377 if (e->src == ENTRY_BLOCK_PTR)
1378 continue;
1380 p = src[e->src->index]->elms;
1381 r = dst->elms;
1382 for (i = 0; i < set_size; i++)
1383 *r++ &= *p++;
1387 /* Set the bitmap DST to the union of SRC of successors of
1388 basic block B. */
1390 void
1391 bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1393 unsigned int set_size = dst->size;
1394 edge e;
1395 unsigned ix;
1397 gcc_assert (!dst->popcount);
1399 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1401 e = EDGE_SUCC (b, ix);
1402 if (e->dest == EXIT_BLOCK_PTR)
1403 continue;
1405 bitmap_copy (dst, src[e->dest->index]);
1406 break;
1409 if (ix == EDGE_COUNT (b->succs))
1410 bitmap_clear (dst);
1411 else
1412 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
1414 unsigned int i;
1415 SBITMAP_ELT_TYPE *p, *r;
1417 e = EDGE_SUCC (b, ix);
1418 if (e->dest == EXIT_BLOCK_PTR)
1419 continue;
1421 p = src[e->dest->index]->elms;
1422 r = dst->elms;
1423 for (i = 0; i < set_size; i++)
1424 *r++ |= *p++;
1428 /* Set the bitmap DST to the union of SRC of predecessors of
1429 basic block B. */
1431 void
1432 bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1434 unsigned int set_size = dst->size;
1435 edge e;
1436 unsigned ix;
1438 gcc_assert (!dst->popcount);
1440 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1442 e = EDGE_PRED (b, ix);
1443 if (e->src== ENTRY_BLOCK_PTR)
1444 continue;
1446 bitmap_copy (dst, src[e->src->index]);
1447 break;
1450 if (ix == EDGE_COUNT (b->preds))
1451 bitmap_clear (dst);
1452 else
1453 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
1455 unsigned int i;
1456 SBITMAP_ELT_TYPE *p, *r;
1458 e = EDGE_PRED (b, ix);
1459 if (e->src == ENTRY_BLOCK_PTR)
1460 continue;
1462 p = src[e->src->index]->elms;
1463 r = dst->elms;
1464 for (i = 0; i < set_size; i++)
1465 *r++ |= *p++;