* symtab.c (symtab_node::reset_section): New method.
[official-gcc.git] / gcc / cfganal.c
blob4118857608a6b35a6bc6af27597e9b934825f367
1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987-2014 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_for_fn (cfun));
76 post = XCNEWVEC (int, last_basic_block_for_fn (cfun));
78 /* Allocate stack for back-tracking up CFG. */
79 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
80 sp = 0;
82 /* Allocate bitmap to track nodes that have been visited. */
83 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
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_FOR_FN (cfun)->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_FOR_FN (cfun) && ! bitmap_bit_p (visited,
105 dest->index))
107 /* Mark that we have visited the destination. */
108 bitmap_set_bit (visited, dest->index);
110 pre[dest->index] = prenum++;
111 if (EDGE_COUNT (dest->succs) > 0)
113 /* Since the DEST node has been visited for the first
114 time, check its successors. */
115 stack[sp++] = ei_start (dest->succs);
117 else
118 post[dest->index] = postnum++;
120 else
122 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
123 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
124 && pre[src->index] >= pre[dest->index]
125 && post[dest->index] == 0)
126 ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
128 if (ei_one_before_end_p (ei)
129 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
130 post[src->index] = postnum++;
132 if (!ei_one_before_end_p (ei))
133 ei_next (&stack[sp - 1]);
134 else
135 sp--;
139 free (pre);
140 free (post);
141 free (stack);
142 sbitmap_free (visited);
144 return found;
147 /* Find unreachable blocks. An unreachable block will have 0 in
148 the reachable bit in block->flags. A nonzero value indicates the
149 block is reachable. */
151 void
152 find_unreachable_blocks (void)
154 edge e;
155 edge_iterator ei;
156 basic_block *tos, *worklist, bb;
158 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
160 /* Clear all the reachability flags. */
162 FOR_EACH_BB_FN (bb, cfun)
163 bb->flags &= ~BB_REACHABLE;
165 /* Add our starting points to the worklist. Almost always there will
166 be only one. It isn't inconceivable that we might one day directly
167 support Fortran alternate entry points. */
169 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
171 *tos++ = e->dest;
173 /* Mark the block reachable. */
174 e->dest->flags |= BB_REACHABLE;
177 /* Iterate: find everything reachable from what we've already seen. */
179 while (tos != worklist)
181 basic_block b = *--tos;
183 FOR_EACH_EDGE (e, ei, b->succs)
185 basic_block dest = e->dest;
187 if (!(dest->flags & BB_REACHABLE))
189 *tos++ = dest;
190 dest->flags |= BB_REACHABLE;
195 free (worklist);
198 /* Functions to access an edge list with a vector representation.
199 Enough data is kept such that given an index number, the
200 pred and succ that edge represents can be determined, or
201 given a pred and a succ, its index number can be returned.
202 This allows algorithms which consume a lot of memory to
203 represent the normally full matrix of edge (pred,succ) with a
204 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
205 wasted space in the client code due to sparse flow graphs. */
207 /* This functions initializes the edge list. Basically the entire
208 flowgraph is processed, and all edges are assigned a number,
209 and the data structure is filled in. */
211 struct edge_list *
212 create_edge_list (void)
214 struct edge_list *elist;
215 edge e;
216 int num_edges;
217 basic_block bb;
218 edge_iterator ei;
220 /* Determine the number of edges in the flow graph by counting successor
221 edges on each basic block. */
222 num_edges = 0;
223 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
224 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
226 num_edges += EDGE_COUNT (bb->succs);
229 elist = XNEW (struct edge_list);
230 elist->num_edges = num_edges;
231 elist->index_to_edge = XNEWVEC (edge, num_edges);
233 num_edges = 0;
235 /* Follow successors of blocks, and register these edges. */
236 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
237 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
238 FOR_EACH_EDGE (e, ei, bb->succs)
239 elist->index_to_edge[num_edges++] = e;
241 return elist;
244 /* This function free's memory associated with an edge list. */
246 void
247 free_edge_list (struct edge_list *elist)
249 if (elist)
251 free (elist->index_to_edge);
252 free (elist);
256 /* This function provides debug output showing an edge list. */
258 DEBUG_FUNCTION void
259 print_edge_list (FILE *f, struct edge_list *elist)
261 int x;
263 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
264 n_basic_blocks_for_fn (cfun), elist->num_edges);
266 for (x = 0; x < elist->num_edges; x++)
268 fprintf (f, " %-4d - edge(", x);
269 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
270 fprintf (f, "entry,");
271 else
272 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
274 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR_FOR_FN (cfun))
275 fprintf (f, "exit)\n");
276 else
277 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
281 /* This function provides an internal consistency check of an edge list,
282 verifying that all edges are present, and that there are no
283 extra edges. */
285 DEBUG_FUNCTION void
286 verify_edge_list (FILE *f, struct edge_list *elist)
288 int pred, succ, index;
289 edge e;
290 basic_block bb, p, s;
291 edge_iterator ei;
293 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
294 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
296 FOR_EACH_EDGE (e, ei, bb->succs)
298 pred = e->src->index;
299 succ = e->dest->index;
300 index = EDGE_INDEX (elist, e->src, e->dest);
301 if (index == EDGE_INDEX_NO_EDGE)
303 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
304 continue;
307 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
308 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
309 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
310 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
311 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
312 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
316 /* We've verified that all the edges are in the list, now lets make sure
317 there are no spurious edges in the list. This is an expensive check! */
319 FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR_FOR_FN (cfun),
320 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
321 FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
323 int found_edge = 0;
325 FOR_EACH_EDGE (e, ei, p->succs)
326 if (e->dest == s)
328 found_edge = 1;
329 break;
332 FOR_EACH_EDGE (e, ei, s->preds)
333 if (e->src == p)
335 found_edge = 1;
336 break;
339 if (EDGE_INDEX (elist, p, s)
340 == EDGE_INDEX_NO_EDGE && found_edge != 0)
341 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
342 p->index, s->index);
343 if (EDGE_INDEX (elist, p, s)
344 != EDGE_INDEX_NO_EDGE && found_edge == 0)
345 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
346 p->index, s->index, EDGE_INDEX (elist, p, s));
351 /* Functions to compute control dependences. */
353 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
354 void
355 control_dependences::set_control_dependence_map_bit (basic_block bb,
356 int edge_index)
358 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
359 return;
360 gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
361 bitmap_set_bit (control_dependence_map[bb->index], edge_index);
364 /* Clear all control dependences for block BB. */
365 void
366 control_dependences::clear_control_dependence_bitmap (basic_block bb)
368 bitmap_clear (control_dependence_map[bb->index]);
371 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
372 This function is necessary because some blocks have negative numbers. */
374 static inline basic_block
375 find_pdom (basic_block block)
377 gcc_assert (block != ENTRY_BLOCK_PTR_FOR_FN (cfun));
379 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
380 return EXIT_BLOCK_PTR_FOR_FN (cfun);
381 else
383 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
384 if (! bb)
385 return EXIT_BLOCK_PTR_FOR_FN (cfun);
386 return bb;
390 /* Determine all blocks' control dependences on the given edge with edge_list
391 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
393 void
394 control_dependences::find_control_dependence (int edge_index)
396 basic_block current_block;
397 basic_block ending_block;
399 gcc_assert (INDEX_EDGE_PRED_BB (m_el, edge_index)
400 != EXIT_BLOCK_PTR_FOR_FN (cfun));
402 if (INDEX_EDGE_PRED_BB (m_el, edge_index) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
403 ending_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
404 else
405 ending_block = find_pdom (INDEX_EDGE_PRED_BB (m_el, edge_index));
407 for (current_block = INDEX_EDGE_SUCC_BB (m_el, edge_index);
408 current_block != ending_block
409 && current_block != EXIT_BLOCK_PTR_FOR_FN (cfun);
410 current_block = find_pdom (current_block))
412 edge e = INDEX_EDGE (m_el, edge_index);
414 /* For abnormal edges, we don't make current_block control
415 dependent because instructions that throw are always necessary
416 anyway. */
417 if (e->flags & EDGE_ABNORMAL)
418 continue;
420 set_control_dependence_map_bit (current_block, edge_index);
424 /* Record all blocks' control dependences on all edges in the edge
425 list EL, ala Morgan, Section 3.6. */
427 control_dependences::control_dependences (struct edge_list *edges)
428 : m_el (edges)
430 timevar_push (TV_CONTROL_DEPENDENCES);
431 control_dependence_map.create (last_basic_block_for_fn (cfun));
432 for (int i = 0; i < last_basic_block_for_fn (cfun); ++i)
433 control_dependence_map.quick_push (BITMAP_ALLOC (NULL));
434 for (int i = 0; i < NUM_EDGES (m_el); ++i)
435 find_control_dependence (i);
436 timevar_pop (TV_CONTROL_DEPENDENCES);
439 /* Free control dependences and the associated edge list. */
441 control_dependences::~control_dependences ()
443 for (unsigned i = 0; i < control_dependence_map.length (); ++i)
444 BITMAP_FREE (control_dependence_map[i]);
445 control_dependence_map.release ();
446 free_edge_list (m_el);
449 /* Returns the bitmap of edges the basic-block I is dependent on. */
451 bitmap
452 control_dependences::get_edges_dependent_on (int i)
454 return control_dependence_map[i];
457 /* Returns the edge with index I from the edge list. */
459 edge
460 control_dependences::get_edge (int i)
462 return INDEX_EDGE (m_el, i);
466 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
467 If no such edge exists, return NULL. */
469 edge
470 find_edge (basic_block pred, basic_block succ)
472 edge e;
473 edge_iterator ei;
475 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
477 FOR_EACH_EDGE (e, ei, pred->succs)
478 if (e->dest == succ)
479 return e;
481 else
483 FOR_EACH_EDGE (e, ei, succ->preds)
484 if (e->src == pred)
485 return e;
488 return NULL;
491 /* This routine will determine what, if any, edge there is between
492 a specified predecessor and successor. */
495 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
497 int x;
499 for (x = 0; x < NUM_EDGES (edge_list); x++)
500 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
501 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
502 return x;
504 return (EDGE_INDEX_NO_EDGE);
507 /* This routine will remove any fake predecessor edges for a basic block.
508 When the edge is removed, it is also removed from whatever successor
509 list it is in. */
511 static void
512 remove_fake_predecessors (basic_block bb)
514 edge e;
515 edge_iterator ei;
517 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
519 if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
520 remove_edge (e);
521 else
522 ei_next (&ei);
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_FOR_FN (cfun)->next_bb, NULL, next_bb)
536 remove_fake_predecessors (bb);
539 /* This routine will remove all fake edges to the EXIT_BLOCK. */
541 void
542 remove_fake_exit_edges (void)
544 remove_fake_predecessors (EXIT_BLOCK_PTR_FOR_FN (cfun));
548 /* This function will add a fake edge between any block which has no
549 successors, and the exit block. Some data flow equations require these
550 edges to exist. */
552 void
553 add_noreturn_fake_exit_edges (void)
555 basic_block bb;
557 FOR_EACH_BB_FN (bb, cfun)
558 if (EDGE_COUNT (bb->succs) == 0)
559 make_single_succ_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
562 /* This function adds a fake edge between any infinite loops to the
563 exit block. Some optimizations require a path from each node to
564 the exit node.
566 See also Morgan, Figure 3.10, pp. 82-83.
568 The current implementation is ugly, not attempting to minimize the
569 number of inserted fake edges. To reduce the number of fake edges
570 to insert, add fake edges from _innermost_ loops containing only
571 nodes not reachable from the exit block. */
573 void
574 connect_infinite_loops_to_exit (void)
576 basic_block unvisited_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
577 basic_block deadend_block;
578 struct depth_first_search_dsS dfs_ds;
580 /* Perform depth-first search in the reverse graph to find nodes
581 reachable from the exit block. */
582 flow_dfs_compute_reverse_init (&dfs_ds);
583 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR_FOR_FN (cfun));
585 /* Repeatedly add fake edges, updating the unreachable nodes. */
586 while (1)
588 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
589 unvisited_block);
590 if (!unvisited_block)
591 break;
593 deadend_block = dfs_find_deadend (unvisited_block);
594 make_edge (deadend_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
595 flow_dfs_compute_reverse_add_bb (&dfs_ds, deadend_block);
598 flow_dfs_compute_reverse_finish (&dfs_ds);
599 return;
602 /* Compute reverse top sort order. This is computing a post order
603 numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then
604 ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
605 true, unreachable blocks are deleted. */
608 post_order_compute (int *post_order, bool include_entry_exit,
609 bool delete_unreachable)
611 edge_iterator *stack;
612 int sp;
613 int post_order_num = 0;
614 sbitmap visited;
615 int count;
617 if (include_entry_exit)
618 post_order[post_order_num++] = EXIT_BLOCK;
620 /* Allocate stack for back-tracking up CFG. */
621 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
622 sp = 0;
624 /* Allocate bitmap to track nodes that have been visited. */
625 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
627 /* None of the nodes in the CFG have been visited yet. */
628 bitmap_clear (visited);
630 /* Push the first edge on to the stack. */
631 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
633 while (sp)
635 edge_iterator ei;
636 basic_block src;
637 basic_block dest;
639 /* Look at the edge on the top of the stack. */
640 ei = stack[sp - 1];
641 src = ei_edge (ei)->src;
642 dest = ei_edge (ei)->dest;
644 /* Check if the edge destination has been visited yet. */
645 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
646 && ! bitmap_bit_p (visited, dest->index))
648 /* Mark that we have visited the destination. */
649 bitmap_set_bit (visited, dest->index);
651 if (EDGE_COUNT (dest->succs) > 0)
652 /* Since the DEST node has been visited for the first
653 time, check its successors. */
654 stack[sp++] = ei_start (dest->succs);
655 else
656 post_order[post_order_num++] = dest->index;
658 else
660 if (ei_one_before_end_p (ei)
661 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
662 post_order[post_order_num++] = src->index;
664 if (!ei_one_before_end_p (ei))
665 ei_next (&stack[sp - 1]);
666 else
667 sp--;
671 if (include_entry_exit)
673 post_order[post_order_num++] = ENTRY_BLOCK;
674 count = post_order_num;
676 else
677 count = post_order_num + 2;
679 /* Delete the unreachable blocks if some were found and we are
680 supposed to do it. */
681 if (delete_unreachable && (count != n_basic_blocks_for_fn (cfun)))
683 basic_block b;
684 basic_block next_bb;
685 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
686 != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
688 next_bb = b->next_bb;
690 if (!(bitmap_bit_p (visited, b->index)))
691 delete_basic_block (b);
694 tidy_fallthru_edges ();
697 free (stack);
698 sbitmap_free (visited);
699 return post_order_num;
703 /* Helper routine for inverted_post_order_compute
704 flow_dfs_compute_reverse_execute, and the reverse-CFG
705 deapth first search in dominance.c.
706 BB has to belong to a region of CFG
707 unreachable by inverted traversal from the exit.
708 i.e. there's no control flow path from ENTRY to EXIT
709 that contains this BB.
710 This can happen in two cases - if there's an infinite loop
711 or if there's a block that has no successor
712 (call to a function with no return).
713 Some RTL passes deal with this condition by
714 calling connect_infinite_loops_to_exit () and/or
715 add_noreturn_fake_exit_edges ().
716 However, those methods involve modifying the CFG itself
717 which may not be desirable.
718 Hence, we deal with the infinite loop/no return cases
719 by identifying a unique basic block that can reach all blocks
720 in such a region by inverted traversal.
721 This function returns a basic block that guarantees
722 that all blocks in the region are reachable
723 by starting an inverted traversal from the returned block. */
725 basic_block
726 dfs_find_deadend (basic_block bb)
728 bitmap visited = BITMAP_ALLOC (NULL);
730 for (;;)
732 if (EDGE_COUNT (bb->succs) == 0
733 || ! bitmap_set_bit (visited, bb->index))
735 BITMAP_FREE (visited);
736 return bb;
739 bb = EDGE_SUCC (bb, 0)->dest;
742 gcc_unreachable ();
746 /* Compute the reverse top sort order of the inverted CFG
747 i.e. starting from the exit block and following the edges backward
748 (from successors to predecessors).
749 This ordering can be used for forward dataflow problems among others.
751 This function assumes that all blocks in the CFG are reachable
752 from the ENTRY (but not necessarily from EXIT).
754 If there's an infinite loop,
755 a simple inverted traversal starting from the blocks
756 with no successors can't visit all blocks.
757 To solve this problem, we first do inverted traversal
758 starting from the blocks with no successor.
759 And if there's any block left that's not visited by the regular
760 inverted traversal from EXIT,
761 those blocks are in such problematic region.
762 Among those, we find one block that has
763 any visited predecessor (which is an entry into such a region),
764 and start looking for a "dead end" from that block
765 and do another inverted traversal from that block. */
768 inverted_post_order_compute (int *post_order)
770 basic_block bb;
771 edge_iterator *stack;
772 int sp;
773 int post_order_num = 0;
774 sbitmap visited;
776 /* Allocate stack for back-tracking up CFG. */
777 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
778 sp = 0;
780 /* Allocate bitmap to track nodes that have been visited. */
781 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
783 /* None of the nodes in the CFG have been visited yet. */
784 bitmap_clear (visited);
786 /* Put all blocks that have no successor into the initial work list. */
787 FOR_ALL_BB_FN (bb, cfun)
788 if (EDGE_COUNT (bb->succs) == 0)
790 /* Push the initial edge on to the stack. */
791 if (EDGE_COUNT (bb->preds) > 0)
793 stack[sp++] = ei_start (bb->preds);
794 bitmap_set_bit (visited, bb->index);
800 bool has_unvisited_bb = false;
802 /* The inverted traversal loop. */
803 while (sp)
805 edge_iterator ei;
806 basic_block pred;
808 /* Look at the edge on the top of the stack. */
809 ei = stack[sp - 1];
810 bb = ei_edge (ei)->dest;
811 pred = ei_edge (ei)->src;
813 /* Check if the predecessor has been visited yet. */
814 if (! bitmap_bit_p (visited, pred->index))
816 /* Mark that we have visited the destination. */
817 bitmap_set_bit (visited, pred->index);
819 if (EDGE_COUNT (pred->preds) > 0)
820 /* Since the predecessor node has been visited for the first
821 time, check its predecessors. */
822 stack[sp++] = ei_start (pred->preds);
823 else
824 post_order[post_order_num++] = pred->index;
826 else
828 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
829 && ei_one_before_end_p (ei))
830 post_order[post_order_num++] = bb->index;
832 if (!ei_one_before_end_p (ei))
833 ei_next (&stack[sp - 1]);
834 else
835 sp--;
839 /* Detect any infinite loop and activate the kludge.
840 Note that this doesn't check EXIT_BLOCK itself
841 since EXIT_BLOCK is always added after the outer do-while loop. */
842 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
843 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
844 if (!bitmap_bit_p (visited, bb->index))
846 has_unvisited_bb = true;
848 if (EDGE_COUNT (bb->preds) > 0)
850 edge_iterator ei;
851 edge e;
852 basic_block visited_pred = NULL;
854 /* Find an already visited predecessor. */
855 FOR_EACH_EDGE (e, ei, bb->preds)
857 if (bitmap_bit_p (visited, e->src->index))
858 visited_pred = e->src;
861 if (visited_pred)
863 basic_block be = dfs_find_deadend (bb);
864 gcc_assert (be != NULL);
865 bitmap_set_bit (visited, be->index);
866 stack[sp++] = ei_start (be->preds);
867 break;
872 if (has_unvisited_bb && sp == 0)
874 /* No blocks are reachable from EXIT at all.
875 Find a dead-end from the ENTRY, and restart the iteration. */
876 basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR_FOR_FN (cfun));
877 gcc_assert (be != NULL);
878 bitmap_set_bit (visited, be->index);
879 stack[sp++] = ei_start (be->preds);
882 /* The only case the below while fires is
883 when there's an infinite loop. */
885 while (sp);
887 /* EXIT_BLOCK is always included. */
888 post_order[post_order_num++] = EXIT_BLOCK;
890 free (stack);
891 sbitmap_free (visited);
892 return post_order_num;
895 /* Compute the depth first search order of FN and store in the array
896 PRE_ORDER if nonzero. If REV_POST_ORDER is nonzero, return the
897 reverse completion number for each node. Returns the number of nodes
898 visited. A depth first search tries to get as far away from the starting
899 point as quickly as possible.
901 In case the function has unreachable blocks the number of nodes
902 visited does not include them.
904 pre_order is a really a preorder numbering of the graph.
905 rev_post_order is really a reverse postorder numbering of the graph. */
908 pre_and_rev_post_order_compute_fn (struct function *fn,
909 int *pre_order, int *rev_post_order,
910 bool include_entry_exit)
912 edge_iterator *stack;
913 int sp;
914 int pre_order_num = 0;
915 int rev_post_order_num = n_basic_blocks_for_fn (cfun) - 1;
916 sbitmap visited;
918 /* Allocate stack for back-tracking up CFG. */
919 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
920 sp = 0;
922 if (include_entry_exit)
924 if (pre_order)
925 pre_order[pre_order_num] = ENTRY_BLOCK;
926 pre_order_num++;
927 if (rev_post_order)
928 rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
930 else
931 rev_post_order_num -= NUM_FIXED_BLOCKS;
933 /* Allocate bitmap to track nodes that have been visited. */
934 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
936 /* None of the nodes in the CFG have been visited yet. */
937 bitmap_clear (visited);
939 /* Push the first edge on to the stack. */
940 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs);
942 while (sp)
944 edge_iterator ei;
945 basic_block src;
946 basic_block dest;
948 /* Look at the edge on the top of the stack. */
949 ei = stack[sp - 1];
950 src = ei_edge (ei)->src;
951 dest = ei_edge (ei)->dest;
953 /* Check if the edge destination has been visited yet. */
954 if (dest != EXIT_BLOCK_PTR_FOR_FN (fn)
955 && ! bitmap_bit_p (visited, dest->index))
957 /* Mark that we have visited the destination. */
958 bitmap_set_bit (visited, dest->index);
960 if (pre_order)
961 pre_order[pre_order_num] = dest->index;
963 pre_order_num++;
965 if (EDGE_COUNT (dest->succs) > 0)
966 /* Since the DEST node has been visited for the first
967 time, check its successors. */
968 stack[sp++] = ei_start (dest->succs);
969 else if (rev_post_order)
970 /* There are no successors for the DEST node so assign
971 its reverse completion number. */
972 rev_post_order[rev_post_order_num--] = dest->index;
974 else
976 if (ei_one_before_end_p (ei)
977 && src != ENTRY_BLOCK_PTR_FOR_FN (fn)
978 && rev_post_order)
979 /* There are no more successors for the SRC node
980 so assign its reverse completion number. */
981 rev_post_order[rev_post_order_num--] = src->index;
983 if (!ei_one_before_end_p (ei))
984 ei_next (&stack[sp - 1]);
985 else
986 sp--;
990 free (stack);
991 sbitmap_free (visited);
993 if (include_entry_exit)
995 if (pre_order)
996 pre_order[pre_order_num] = EXIT_BLOCK;
997 pre_order_num++;
998 if (rev_post_order)
999 rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
1002 return pre_order_num;
1005 /* Like pre_and_rev_post_order_compute_fn but operating on the
1006 current function and asserting that all nodes were visited. */
1009 pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
1010 bool include_entry_exit)
1012 int pre_order_num
1013 = pre_and_rev_post_order_compute_fn (cfun, pre_order, rev_post_order,
1014 include_entry_exit);
1015 if (include_entry_exit)
1016 /* The number of nodes visited should be the number of blocks. */
1017 gcc_assert (pre_order_num == n_basic_blocks_for_fn (cfun));
1018 else
1019 /* The number of nodes visited should be the number of blocks minus
1020 the entry and exit blocks which are not visited here. */
1021 gcc_assert (pre_order_num
1022 == (n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS));
1024 return pre_order_num;
1027 /* Compute the depth first search order on the _reverse_ graph and
1028 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1029 Returns the number of nodes visited.
1031 The computation is split into three pieces:
1033 flow_dfs_compute_reverse_init () creates the necessary data
1034 structures.
1036 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1037 structures. The block will start the search.
1039 flow_dfs_compute_reverse_execute () continues (or starts) the
1040 search using the block on the top of the stack, stopping when the
1041 stack is empty.
1043 flow_dfs_compute_reverse_finish () destroys the necessary data
1044 structures.
1046 Thus, the user will probably call ..._init(), call ..._add_bb() to
1047 add a beginning basic block to the stack, call ..._execute(),
1048 possibly add another bb to the stack and again call ..._execute(),
1049 ..., and finally call _finish(). */
1051 /* Initialize the data structures used for depth-first search on the
1052 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1053 added to the basic block stack. DATA is the current depth-first
1054 search context. If INITIALIZE_STACK is nonzero, there is an
1055 element on the stack. */
1057 static void
1058 flow_dfs_compute_reverse_init (depth_first_search_ds data)
1060 /* Allocate stack for back-tracking up CFG. */
1061 data->stack = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1062 data->sp = 0;
1064 /* Allocate bitmap to track nodes that have been visited. */
1065 data->visited_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
1067 /* None of the nodes in the CFG have been visited yet. */
1068 bitmap_clear (data->visited_blocks);
1070 return;
1073 /* Add the specified basic block to the top of the dfs data
1074 structures. When the search continues, it will start at the
1075 block. */
1077 static void
1078 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
1080 data->stack[data->sp++] = bb;
1081 bitmap_set_bit (data->visited_blocks, bb->index);
1084 /* Continue the depth-first search through the reverse graph starting with the
1085 block at the stack's top and ending when the stack is empty. Visited nodes
1086 are marked. Returns an unvisited basic block, or NULL if there is none
1087 available. */
1089 static basic_block
1090 flow_dfs_compute_reverse_execute (depth_first_search_ds data,
1091 basic_block last_unvisited)
1093 basic_block bb;
1094 edge e;
1095 edge_iterator ei;
1097 while (data->sp > 0)
1099 bb = data->stack[--data->sp];
1101 /* Perform depth-first search on adjacent vertices. */
1102 FOR_EACH_EDGE (e, ei, bb->preds)
1103 if (!bitmap_bit_p (data->visited_blocks, e->src->index))
1104 flow_dfs_compute_reverse_add_bb (data, e->src);
1107 /* Determine if there are unvisited basic blocks. */
1108 FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
1109 if (!bitmap_bit_p (data->visited_blocks, bb->index))
1110 return bb;
1112 return NULL;
1115 /* Destroy the data structures needed for depth-first search on the
1116 reverse graph. */
1118 static void
1119 flow_dfs_compute_reverse_finish (depth_first_search_ds data)
1121 free (data->stack);
1122 sbitmap_free (data->visited_blocks);
1125 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1126 if REVERSE, go against direction of edges. Returns number of blocks
1127 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
1129 dfs_enumerate_from (basic_block bb, int reverse,
1130 bool (*predicate) (const_basic_block, const void *),
1131 basic_block *rslt, int rslt_max, const void *data)
1133 basic_block *st, lbb;
1134 int sp = 0, tv = 0;
1135 unsigned size;
1137 /* A bitmap to keep track of visited blocks. Allocating it each time
1138 this function is called is not possible, since dfs_enumerate_from
1139 is often used on small (almost) disjoint parts of cfg (bodies of
1140 loops), and allocating a large sbitmap would lead to quadratic
1141 behavior. */
1142 static sbitmap visited;
1143 static unsigned v_size;
1145 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1146 #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
1147 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1149 /* Resize the VISITED sbitmap if necessary. */
1150 size = last_basic_block_for_fn (cfun);
1151 if (size < 10)
1152 size = 10;
1154 if (!visited)
1157 visited = sbitmap_alloc (size);
1158 bitmap_clear (visited);
1159 v_size = size;
1161 else if (v_size < size)
1163 /* Ensure that we increase the size of the sbitmap exponentially. */
1164 if (2 * v_size > size)
1165 size = 2 * v_size;
1167 visited = sbitmap_resize (visited, size, 0);
1168 v_size = size;
1171 st = XNEWVEC (basic_block, rslt_max);
1172 rslt[tv++] = st[sp++] = bb;
1173 MARK_VISITED (bb);
1174 while (sp)
1176 edge e;
1177 edge_iterator ei;
1178 lbb = st[--sp];
1179 if (reverse)
1181 FOR_EACH_EDGE (e, ei, lbb->preds)
1182 if (!VISITED_P (e->src) && predicate (e->src, data))
1184 gcc_assert (tv != rslt_max);
1185 rslt[tv++] = st[sp++] = e->src;
1186 MARK_VISITED (e->src);
1189 else
1191 FOR_EACH_EDGE (e, ei, lbb->succs)
1192 if (!VISITED_P (e->dest) && predicate (e->dest, data))
1194 gcc_assert (tv != rslt_max);
1195 rslt[tv++] = st[sp++] = e->dest;
1196 MARK_VISITED (e->dest);
1200 free (st);
1201 for (sp = 0; sp < tv; sp++)
1202 UNMARK_VISITED (rslt[sp]);
1203 return tv;
1204 #undef MARK_VISITED
1205 #undef UNMARK_VISITED
1206 #undef VISITED_P
1210 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1212 This algorithm can be found in Timothy Harvey's PhD thesis, at
1213 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1214 dominance algorithms.
1216 First, we identify each join point, j (any node with more than one
1217 incoming edge is a join point).
1219 We then examine each predecessor, p, of j and walk up the dominator tree
1220 starting at p.
1222 We stop the walk when we reach j's immediate dominator - j is in the
1223 dominance frontier of each of the nodes in the walk, except for j's
1224 immediate dominator. Intuitively, all of the rest of j's dominators are
1225 shared by j's predecessors as well.
1226 Since they dominate j, they will not have j in their dominance frontiers.
1228 The number of nodes touched by this algorithm is equal to the size
1229 of the dominance frontiers, no more, no less.
1233 static void
1234 compute_dominance_frontiers_1 (bitmap_head *frontiers)
1236 edge p;
1237 edge_iterator ei;
1238 basic_block b;
1239 FOR_EACH_BB_FN (b, cfun)
1241 if (EDGE_COUNT (b->preds) >= 2)
1243 FOR_EACH_EDGE (p, ei, b->preds)
1245 basic_block runner = p->src;
1246 basic_block domsb;
1247 if (runner == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1248 continue;
1250 domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1251 while (runner != domsb)
1253 if (!bitmap_set_bit (&frontiers[runner->index],
1254 b->index))
1255 break;
1256 runner = get_immediate_dominator (CDI_DOMINATORS,
1257 runner);
1265 void
1266 compute_dominance_frontiers (bitmap_head *frontiers)
1268 timevar_push (TV_DOM_FRONTIERS);
1270 compute_dominance_frontiers_1 (frontiers);
1272 timevar_pop (TV_DOM_FRONTIERS);
1275 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1276 return a bitmap with all the blocks in the iterated dominance
1277 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
1278 frontier information as returned by compute_dominance_frontiers.
1280 The resulting set of blocks are the potential sites where PHI nodes
1281 are needed. The caller is responsible for freeing the memory
1282 allocated for the return value. */
1284 bitmap
1285 compute_idf (bitmap def_blocks, bitmap_head *dfs)
1287 bitmap_iterator bi;
1288 unsigned bb_index, i;
1289 bitmap phi_insertion_points;
1291 /* Each block can appear at most twice on the work-stack. */
1292 auto_vec<int> work_stack (2 * n_basic_blocks_for_fn (cfun));
1293 phi_insertion_points = BITMAP_ALLOC (NULL);
1295 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1296 vec::quick_push here for speed. This is safe because we know that
1297 the number of definition blocks is no greater than the number of
1298 basic blocks, which is the initial capacity of WORK_STACK. */
1299 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1300 work_stack.quick_push (bb_index);
1302 /* Pop a block off the worklist, add every block that appears in
1303 the original block's DF that we have not already processed to
1304 the worklist. Iterate until the worklist is empty. Blocks
1305 which are added to the worklist are potential sites for
1306 PHI nodes. */
1307 while (work_stack.length () > 0)
1309 bb_index = work_stack.pop ();
1311 /* Since the registration of NEW -> OLD name mappings is done
1312 separately from the call to update_ssa, when updating the SSA
1313 form, the basic blocks where new and/or old names are defined
1314 may have disappeared by CFG cleanup calls. In this case,
1315 we may pull a non-existing block from the work stack. */
1316 gcc_checking_assert (bb_index
1317 < (unsigned) last_basic_block_for_fn (cfun));
1319 EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
1320 0, i, bi)
1322 work_stack.quick_push (i);
1323 bitmap_set_bit (phi_insertion_points, i);
1327 return phi_insertion_points;
1330 /* Intersection and union of preds/succs for sbitmap based data flow
1331 solvers. All four functions defined below take the same arguments:
1332 B is the basic block to perform the operation for. DST is the
1333 target sbitmap, i.e. the result. SRC is an sbitmap vector of size
1334 last_basic_block so that it can be indexed with basic block indices.
1335 DST may be (but does not have to be) SRC[B->index]. */
1337 /* Set the bitmap DST to the intersection of SRC of successors of
1338 basic block B. */
1340 void
1341 bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1343 unsigned int set_size = dst->size;
1344 edge e;
1345 unsigned ix;
1347 gcc_assert (!dst->popcount);
1349 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1351 e = EDGE_SUCC (b, ix);
1352 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1353 continue;
1355 bitmap_copy (dst, src[e->dest->index]);
1356 break;
1359 if (e == 0)
1360 bitmap_ones (dst);
1361 else
1362 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
1364 unsigned int i;
1365 SBITMAP_ELT_TYPE *p, *r;
1367 e = EDGE_SUCC (b, ix);
1368 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1369 continue;
1371 p = src[e->dest->index]->elms;
1372 r = dst->elms;
1373 for (i = 0; i < set_size; i++)
1374 *r++ &= *p++;
1378 /* Set the bitmap DST to the intersection of SRC of predecessors of
1379 basic block B. */
1381 void
1382 bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1384 unsigned int set_size = dst->size;
1385 edge e;
1386 unsigned ix;
1388 gcc_assert (!dst->popcount);
1390 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1392 e = EDGE_PRED (b, ix);
1393 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1394 continue;
1396 bitmap_copy (dst, src[e->src->index]);
1397 break;
1400 if (e == 0)
1401 bitmap_ones (dst);
1402 else
1403 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
1405 unsigned int i;
1406 SBITMAP_ELT_TYPE *p, *r;
1408 e = EDGE_PRED (b, ix);
1409 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1410 continue;
1412 p = src[e->src->index]->elms;
1413 r = dst->elms;
1414 for (i = 0; i < set_size; i++)
1415 *r++ &= *p++;
1419 /* Set the bitmap DST to the union of SRC of successors of
1420 basic block B. */
1422 void
1423 bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1425 unsigned int set_size = dst->size;
1426 edge e;
1427 unsigned ix;
1429 gcc_assert (!dst->popcount);
1431 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1433 e = EDGE_SUCC (b, ix);
1434 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1435 continue;
1437 bitmap_copy (dst, src[e->dest->index]);
1438 break;
1441 if (ix == EDGE_COUNT (b->succs))
1442 bitmap_clear (dst);
1443 else
1444 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
1446 unsigned int i;
1447 SBITMAP_ELT_TYPE *p, *r;
1449 e = EDGE_SUCC (b, ix);
1450 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1451 continue;
1453 p = src[e->dest->index]->elms;
1454 r = dst->elms;
1455 for (i = 0; i < set_size; i++)
1456 *r++ |= *p++;
1460 /* Set the bitmap DST to the union of SRC of predecessors of
1461 basic block B. */
1463 void
1464 bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1466 unsigned int set_size = dst->size;
1467 edge e;
1468 unsigned ix;
1470 gcc_assert (!dst->popcount);
1472 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1474 e = EDGE_PRED (b, ix);
1475 if (e->src== ENTRY_BLOCK_PTR_FOR_FN (cfun))
1476 continue;
1478 bitmap_copy (dst, src[e->src->index]);
1479 break;
1482 if (ix == EDGE_COUNT (b->preds))
1483 bitmap_clear (dst);
1484 else
1485 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
1487 unsigned int i;
1488 SBITMAP_ELT_TYPE *p, *r;
1490 e = EDGE_PRED (b, ix);
1491 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1492 continue;
1494 p = src[e->src->index]->elms;
1495 r = dst->elms;
1496 for (i = 0; i < set_size; i++)
1497 *r++ |= *p++;
1501 /* Returns the list of basic blocks in the function in an order that guarantees
1502 that if a block X has just a single predecessor Y, then Y is after X in the
1503 ordering. */
1505 basic_block *
1506 single_pred_before_succ_order (void)
1508 basic_block x, y;
1509 basic_block *order = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1510 unsigned n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
1511 unsigned np, i;
1512 sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
1514 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1515 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1517 bitmap_clear (visited);
1519 MARK_VISITED (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1520 FOR_EACH_BB_FN (x, cfun)
1522 if (VISITED_P (x))
1523 continue;
1525 /* Walk the predecessors of x as long as they have precisely one
1526 predecessor and add them to the list, so that they get stored
1527 after x. */
1528 for (y = x, np = 1;
1529 single_pred_p (y) && !VISITED_P (single_pred (y));
1530 y = single_pred (y))
1531 np++;
1532 for (y = x, i = n - np;
1533 single_pred_p (y) && !VISITED_P (single_pred (y));
1534 y = single_pred (y), i++)
1536 order[i] = y;
1537 MARK_VISITED (y);
1539 order[i] = y;
1540 MARK_VISITED (y);
1542 gcc_assert (i == n - 1);
1543 n -= np;
1546 sbitmap_free (visited);
1547 gcc_assert (n == 0);
1548 return order;
1550 #undef MARK_VISITED
1551 #undef VISITED_P