Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / cfganal.c
blobaabc065ca3521bd893616db51c54ed414d5abc71
1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987-2016 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 "backend.h"
26 #include "cfghooks.h"
27 #include "timevar.h"
28 #include "cfganal.h"
29 #include "cfgloop.h"
31 /* Store the data structures necessary for depth-first search. */
32 struct depth_first_search_ds {
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;
44 static void flow_dfs_compute_reverse_init (depth_first_search_ds *);
45 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds *,
46 basic_block);
47 static basic_block flow_dfs_compute_reverse_execute (depth_first_search_ds *,
48 basic_block);
49 static void flow_dfs_compute_reverse_finish (depth_first_search_ds *);
51 /* Mark the back edges in DFS traversal.
52 Return nonzero if a loop (natural or otherwise) is present.
53 Inspired by Depth_First_Search_PP described in:
55 Advanced Compiler Design and Implementation
56 Steven Muchnick
57 Morgan Kaufmann, 1997
59 and heavily borrowed from pre_and_rev_post_order_compute. */
61 bool
62 mark_dfs_back_edges (void)
64 edge_iterator *stack;
65 int *pre;
66 int *post;
67 int sp;
68 int prenum = 1;
69 int postnum = 1;
70 sbitmap visited;
71 bool found = false;
73 /* Allocate the preorder and postorder number arrays. */
74 pre = XCNEWVEC (int, last_basic_block_for_fn (cfun));
75 post = XCNEWVEC (int, last_basic_block_for_fn (cfun));
77 /* Allocate stack for back-tracking up CFG. */
78 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
79 sp = 0;
81 /* Allocate bitmap to track nodes that have been visited. */
82 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
84 /* None of the nodes in the CFG have been visited yet. */
85 bitmap_clear (visited);
87 /* Push the first edge on to the stack. */
88 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
90 while (sp)
92 edge_iterator ei;
93 basic_block src;
94 basic_block dest;
96 /* Look at the edge on the top of the stack. */
97 ei = stack[sp - 1];
98 src = ei_edge (ei)->src;
99 dest = ei_edge (ei)->dest;
100 ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
102 /* Check if the edge destination has been visited yet. */
103 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && ! bitmap_bit_p (visited,
104 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_FOR_FN (cfun)
122 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
123 && pre[src->index] >= pre[dest->index]
124 && post[dest->index] == 0)
125 ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
127 if (ei_one_before_end_p (ei)
128 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
129 post[src->index] = postnum++;
131 if (!ei_one_before_end_p (ei))
132 ei_next (&stack[sp - 1]);
133 else
134 sp--;
138 free (pre);
139 free (post);
140 free (stack);
141 sbitmap_free (visited);
143 return found;
146 /* Find unreachable blocks. An unreachable block will have 0 in
147 the reachable bit in block->flags. A nonzero value indicates the
148 block is reachable. */
150 void
151 find_unreachable_blocks (void)
153 edge e;
154 edge_iterator ei;
155 basic_block *tos, *worklist, bb;
157 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
159 /* Clear all the reachability flags. */
161 FOR_EACH_BB_FN (bb, cfun)
162 bb->flags &= ~BB_REACHABLE;
164 /* Add our starting points to the worklist. Almost always there will
165 be only one. It isn't inconceivable that we might one day directly
166 support Fortran alternate entry points. */
168 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
170 *tos++ = e->dest;
172 /* Mark the block reachable. */
173 e->dest->flags |= BB_REACHABLE;
176 /* Iterate: find everything reachable from what we've already seen. */
178 while (tos != worklist)
180 basic_block b = *--tos;
182 FOR_EACH_EDGE (e, ei, b->succs)
184 basic_block dest = e->dest;
186 if (!(dest->flags & BB_REACHABLE))
188 *tos++ = dest;
189 dest->flags |= BB_REACHABLE;
194 free (worklist);
197 /* Verify that there are no unreachable blocks in the current function. */
199 void
200 verify_no_unreachable_blocks (void)
202 find_unreachable_blocks ();
204 basic_block bb;
205 FOR_EACH_BB_FN (bb, cfun)
206 gcc_assert ((bb->flags & BB_REACHABLE) != 0);
210 /* Functions to access an edge list with a vector representation.
211 Enough data is kept such that given an index number, the
212 pred and succ that edge represents can be determined, or
213 given a pred and a succ, its index number can be returned.
214 This allows algorithms which consume a lot of memory to
215 represent the normally full matrix of edge (pred,succ) with a
216 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
217 wasted space in the client code due to sparse flow graphs. */
219 /* This functions initializes the edge list. Basically the entire
220 flowgraph is processed, and all edges are assigned a number,
221 and the data structure is filled in. */
223 struct edge_list *
224 create_edge_list (void)
226 struct edge_list *elist;
227 edge e;
228 int num_edges;
229 basic_block bb;
230 edge_iterator ei;
232 /* Determine the number of edges in the flow graph by counting successor
233 edges on each basic block. */
234 num_edges = 0;
235 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
236 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
238 num_edges += EDGE_COUNT (bb->succs);
241 elist = XNEW (struct edge_list);
242 elist->num_edges = num_edges;
243 elist->index_to_edge = XNEWVEC (edge, num_edges);
245 num_edges = 0;
247 /* Follow successors of blocks, and register these edges. */
248 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
249 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
250 FOR_EACH_EDGE (e, ei, bb->succs)
251 elist->index_to_edge[num_edges++] = e;
253 return elist;
256 /* This function free's memory associated with an edge list. */
258 void
259 free_edge_list (struct edge_list *elist)
261 if (elist)
263 free (elist->index_to_edge);
264 free (elist);
268 /* This function provides debug output showing an edge list. */
270 DEBUG_FUNCTION void
271 print_edge_list (FILE *f, struct edge_list *elist)
273 int x;
275 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
276 n_basic_blocks_for_fn (cfun), elist->num_edges);
278 for (x = 0; x < elist->num_edges; x++)
280 fprintf (f, " %-4d - edge(", x);
281 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
282 fprintf (f, "entry,");
283 else
284 fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
286 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR_FOR_FN (cfun))
287 fprintf (f, "exit)\n");
288 else
289 fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
293 /* This function provides an internal consistency check of an edge list,
294 verifying that all edges are present, and that there are no
295 extra edges. */
297 DEBUG_FUNCTION void
298 verify_edge_list (FILE *f, struct edge_list *elist)
300 int pred, succ, index;
301 edge e;
302 basic_block bb, p, s;
303 edge_iterator ei;
305 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
306 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
308 FOR_EACH_EDGE (e, ei, bb->succs)
310 pred = e->src->index;
311 succ = e->dest->index;
312 index = EDGE_INDEX (elist, e->src, e->dest);
313 if (index == EDGE_INDEX_NO_EDGE)
315 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
316 continue;
319 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
320 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
321 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
322 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
323 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
324 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
328 /* We've verified that all the edges are in the list, now lets make sure
329 there are no spurious edges in the list. This is an expensive check! */
331 FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR_FOR_FN (cfun),
332 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
333 FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
335 int found_edge = 0;
337 FOR_EACH_EDGE (e, ei, p->succs)
338 if (e->dest == s)
340 found_edge = 1;
341 break;
344 FOR_EACH_EDGE (e, ei, s->preds)
345 if (e->src == p)
347 found_edge = 1;
348 break;
351 if (EDGE_INDEX (elist, p, s)
352 == EDGE_INDEX_NO_EDGE && found_edge != 0)
353 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
354 p->index, s->index);
355 if (EDGE_INDEX (elist, p, s)
356 != EDGE_INDEX_NO_EDGE && found_edge == 0)
357 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
358 p->index, s->index, EDGE_INDEX (elist, p, s));
363 /* Functions to compute control dependences. */
365 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX. */
366 void
367 control_dependences::set_control_dependence_map_bit (basic_block bb,
368 int edge_index)
370 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
371 return;
372 gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
373 bitmap_set_bit (control_dependence_map[bb->index], edge_index);
376 /* Clear all control dependences for block BB. */
377 void
378 control_dependences::clear_control_dependence_bitmap (basic_block bb)
380 bitmap_clear (control_dependence_map[bb->index]);
383 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
384 This function is necessary because some blocks have negative numbers. */
386 static inline basic_block
387 find_pdom (basic_block block)
389 gcc_assert (block != ENTRY_BLOCK_PTR_FOR_FN (cfun));
391 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
392 return EXIT_BLOCK_PTR_FOR_FN (cfun);
393 else
395 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
396 if (! bb)
397 return EXIT_BLOCK_PTR_FOR_FN (cfun);
398 return bb;
402 /* Determine all blocks' control dependences on the given edge with edge_list
403 EL index EDGE_INDEX, ala Morgan, Section 3.6. */
405 void
406 control_dependences::find_control_dependence (int edge_index)
408 basic_block current_block;
409 basic_block ending_block;
411 gcc_assert (get_edge_src (edge_index) != EXIT_BLOCK_PTR_FOR_FN (cfun));
413 /* For abnormal edges, we don't make current_block control
414 dependent because instructions that throw are always necessary
415 anyway. */
416 edge e = find_edge (get_edge_src (edge_index), get_edge_dest (edge_index));
417 if (e->flags & EDGE_ABNORMAL)
418 return;
420 if (get_edge_src (edge_index) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
421 ending_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
422 else
423 ending_block = find_pdom (get_edge_src (edge_index));
425 for (current_block = get_edge_dest (edge_index);
426 current_block != ending_block
427 && current_block != EXIT_BLOCK_PTR_FOR_FN (cfun);
428 current_block = find_pdom (current_block))
429 set_control_dependence_map_bit (current_block, edge_index);
432 /* Record all blocks' control dependences on all edges in the edge
433 list EL, ala Morgan, Section 3.6. */
435 control_dependences::control_dependences ()
437 timevar_push (TV_CONTROL_DEPENDENCES);
439 /* Initialize the edge list. */
440 int num_edges = 0;
441 basic_block bb;
442 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
443 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
444 num_edges += EDGE_COUNT (bb->succs);
445 m_el.create (num_edges);
446 edge e;
447 edge_iterator ei;
448 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
449 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
450 FOR_EACH_EDGE (e, ei, bb->succs)
451 m_el.quick_push (std::make_pair (e->src->index, e->dest->index));
453 control_dependence_map.create (last_basic_block_for_fn (cfun));
454 for (int i = 0; i < last_basic_block_for_fn (cfun); ++i)
455 control_dependence_map.quick_push (BITMAP_ALLOC (NULL));
456 for (int i = 0; i < num_edges; ++i)
457 find_control_dependence (i);
459 timevar_pop (TV_CONTROL_DEPENDENCES);
462 /* Free control dependences and the associated edge list. */
464 control_dependences::~control_dependences ()
466 for (unsigned i = 0; i < control_dependence_map.length (); ++i)
467 BITMAP_FREE (control_dependence_map[i]);
468 control_dependence_map.release ();
469 m_el.release ();
472 /* Returns the bitmap of edges the basic-block I is dependent on. */
474 bitmap
475 control_dependences::get_edges_dependent_on (int i)
477 return control_dependence_map[i];
480 /* Returns the edge source with index I from the edge list. */
482 basic_block
483 control_dependences::get_edge_src (int i)
485 return BASIC_BLOCK_FOR_FN (cfun, m_el[i].first);
488 /* Returns the edge destination with index I from the edge list. */
490 basic_block
491 control_dependences::get_edge_dest (int i)
493 return BASIC_BLOCK_FOR_FN (cfun, m_el[i].second);
497 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
498 If no such edge exists, return NULL. */
500 edge
501 find_edge (basic_block pred, basic_block succ)
503 edge e;
504 edge_iterator ei;
506 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
508 FOR_EACH_EDGE (e, ei, pred->succs)
509 if (e->dest == succ)
510 return e;
512 else
514 FOR_EACH_EDGE (e, ei, succ->preds)
515 if (e->src == pred)
516 return e;
519 return NULL;
522 /* This routine will determine what, if any, edge there is between
523 a specified predecessor and successor. */
526 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
528 int x;
530 for (x = 0; x < NUM_EDGES (edge_list); x++)
531 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
532 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
533 return x;
535 return (EDGE_INDEX_NO_EDGE);
538 /* This routine will remove any fake predecessor edges for a basic block.
539 When the edge is removed, it is also removed from whatever successor
540 list it is in. */
542 static void
543 remove_fake_predecessors (basic_block bb)
545 edge e;
546 edge_iterator ei;
548 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
550 if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
551 remove_edge (e);
552 else
553 ei_next (&ei);
557 /* This routine will remove all fake edges from the flow graph. If
558 we remove all fake successors, it will automatically remove all
559 fake predecessors. */
561 void
562 remove_fake_edges (void)
564 basic_block bb;
566 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
567 remove_fake_predecessors (bb);
570 /* This routine will remove all fake edges to the EXIT_BLOCK. */
572 void
573 remove_fake_exit_edges (void)
575 remove_fake_predecessors (EXIT_BLOCK_PTR_FOR_FN (cfun));
579 /* This function will add a fake edge between any block which has no
580 successors, and the exit block. Some data flow equations require these
581 edges to exist. */
583 void
584 add_noreturn_fake_exit_edges (void)
586 basic_block bb;
588 FOR_EACH_BB_FN (bb, cfun)
589 if (EDGE_COUNT (bb->succs) == 0)
590 make_single_succ_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
593 /* This function adds a fake edge between any infinite loops to the
594 exit block. Some optimizations require a path from each node to
595 the exit node.
597 See also Morgan, Figure 3.10, pp. 82-83.
599 The current implementation is ugly, not attempting to minimize the
600 number of inserted fake edges. To reduce the number of fake edges
601 to insert, add fake edges from _innermost_ loops containing only
602 nodes not reachable from the exit block. */
604 void
605 connect_infinite_loops_to_exit (void)
607 basic_block unvisited_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
608 basic_block deadend_block;
609 depth_first_search_ds dfs_ds;
611 /* Perform depth-first search in the reverse graph to find nodes
612 reachable from the exit block. */
613 flow_dfs_compute_reverse_init (&dfs_ds);
614 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR_FOR_FN (cfun));
616 /* Repeatedly add fake edges, updating the unreachable nodes. */
617 while (1)
619 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
620 unvisited_block);
621 if (!unvisited_block)
622 break;
624 deadend_block = dfs_find_deadend (unvisited_block);
625 make_edge (deadend_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
626 flow_dfs_compute_reverse_add_bb (&dfs_ds, deadend_block);
629 flow_dfs_compute_reverse_finish (&dfs_ds);
630 return;
633 /* Compute reverse top sort order. This is computing a post order
634 numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then
635 ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
636 true, unreachable blocks are deleted. */
639 post_order_compute (int *post_order, bool include_entry_exit,
640 bool delete_unreachable)
642 edge_iterator *stack;
643 int sp;
644 int post_order_num = 0;
645 sbitmap visited;
646 int count;
648 if (include_entry_exit)
649 post_order[post_order_num++] = EXIT_BLOCK;
651 /* Allocate stack for back-tracking up CFG. */
652 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
653 sp = 0;
655 /* Allocate bitmap to track nodes that have been visited. */
656 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
658 /* None of the nodes in the CFG have been visited yet. */
659 bitmap_clear (visited);
661 /* Push the first edge on to the stack. */
662 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
664 while (sp)
666 edge_iterator ei;
667 basic_block src;
668 basic_block dest;
670 /* Look at the edge on the top of the stack. */
671 ei = stack[sp - 1];
672 src = ei_edge (ei)->src;
673 dest = ei_edge (ei)->dest;
675 /* Check if the edge destination has been visited yet. */
676 if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
677 && ! bitmap_bit_p (visited, dest->index))
679 /* Mark that we have visited the destination. */
680 bitmap_set_bit (visited, dest->index);
682 if (EDGE_COUNT (dest->succs) > 0)
683 /* Since the DEST node has been visited for the first
684 time, check its successors. */
685 stack[sp++] = ei_start (dest->succs);
686 else
687 post_order[post_order_num++] = dest->index;
689 else
691 if (ei_one_before_end_p (ei)
692 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
693 post_order[post_order_num++] = src->index;
695 if (!ei_one_before_end_p (ei))
696 ei_next (&stack[sp - 1]);
697 else
698 sp--;
702 if (include_entry_exit)
704 post_order[post_order_num++] = ENTRY_BLOCK;
705 count = post_order_num;
707 else
708 count = post_order_num + 2;
710 /* Delete the unreachable blocks if some were found and we are
711 supposed to do it. */
712 if (delete_unreachable && (count != n_basic_blocks_for_fn (cfun)))
714 basic_block b;
715 basic_block next_bb;
716 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
717 != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
719 next_bb = b->next_bb;
721 if (!(bitmap_bit_p (visited, b->index)))
722 delete_basic_block (b);
725 tidy_fallthru_edges ();
728 free (stack);
729 sbitmap_free (visited);
730 return post_order_num;
734 /* Helper routine for inverted_post_order_compute
735 flow_dfs_compute_reverse_execute, and the reverse-CFG
736 deapth first search in dominance.c.
737 BB has to belong to a region of CFG
738 unreachable by inverted traversal from the exit.
739 i.e. there's no control flow path from ENTRY to EXIT
740 that contains this BB.
741 This can happen in two cases - if there's an infinite loop
742 or if there's a block that has no successor
743 (call to a function with no return).
744 Some RTL passes deal with this condition by
745 calling connect_infinite_loops_to_exit () and/or
746 add_noreturn_fake_exit_edges ().
747 However, those methods involve modifying the CFG itself
748 which may not be desirable.
749 Hence, we deal with the infinite loop/no return cases
750 by identifying a unique basic block that can reach all blocks
751 in such a region by inverted traversal.
752 This function returns a basic block that guarantees
753 that all blocks in the region are reachable
754 by starting an inverted traversal from the returned block. */
756 basic_block
757 dfs_find_deadend (basic_block bb)
759 bitmap visited = BITMAP_ALLOC (NULL);
761 for (;;)
763 if (EDGE_COUNT (bb->succs) == 0
764 || ! bitmap_set_bit (visited, bb->index))
766 BITMAP_FREE (visited);
767 return bb;
770 /* If we are in an analyzed cycle make sure to try exiting it.
771 Note this is a heuristic only and expected to work when loop
772 fixup is needed as well. */
773 if (! bb->loop_father
774 || ! loop_outer (bb->loop_father))
775 bb = EDGE_SUCC (bb, 0)->dest;
776 else
778 edge_iterator ei;
779 edge e;
780 FOR_EACH_EDGE (e, ei, bb->succs)
781 if (loop_exit_edge_p (bb->loop_father, e))
782 break;
783 bb = e ? e->dest : EDGE_SUCC (bb, 0)->dest;
787 gcc_unreachable ();
791 /* Compute the reverse top sort order of the inverted CFG
792 i.e. starting from the exit block and following the edges backward
793 (from successors to predecessors).
794 This ordering can be used for forward dataflow problems among others.
796 Optionally if START_POINTS is specified, start from exit block and all
797 basic blocks in START_POINTS. This is used by CD-DCE.
799 This function assumes that all blocks in the CFG are reachable
800 from the ENTRY (but not necessarily from EXIT).
802 If there's an infinite loop,
803 a simple inverted traversal starting from the blocks
804 with no successors can't visit all blocks.
805 To solve this problem, we first do inverted traversal
806 starting from the blocks with no successor.
807 And if there's any block left that's not visited by the regular
808 inverted traversal from EXIT,
809 those blocks are in such problematic region.
810 Among those, we find one block that has
811 any visited predecessor (which is an entry into such a region),
812 and start looking for a "dead end" from that block
813 and do another inverted traversal from that block. */
816 inverted_post_order_compute (int *post_order,
817 sbitmap *start_points)
819 basic_block bb;
820 edge_iterator *stack;
821 int sp;
822 int post_order_num = 0;
823 sbitmap visited;
825 if (flag_checking)
826 verify_no_unreachable_blocks ();
828 /* Allocate stack for back-tracking up CFG. */
829 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
830 sp = 0;
832 /* Allocate bitmap to track nodes that have been visited. */
833 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
835 /* None of the nodes in the CFG have been visited yet. */
836 bitmap_clear (visited);
838 if (start_points)
840 FOR_ALL_BB_FN (bb, cfun)
841 if (bitmap_bit_p (*start_points, bb->index)
842 && EDGE_COUNT (bb->preds) > 0)
844 stack[sp++] = ei_start (bb->preds);
845 bitmap_set_bit (visited, bb->index);
847 if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
849 stack[sp++] = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
850 bitmap_set_bit (visited, EXIT_BLOCK_PTR_FOR_FN (cfun)->index);
853 else
854 /* Put all blocks that have no successor into the initial work list. */
855 FOR_ALL_BB_FN (bb, cfun)
856 if (EDGE_COUNT (bb->succs) == 0)
858 /* Push the initial edge on to the stack. */
859 if (EDGE_COUNT (bb->preds) > 0)
861 stack[sp++] = ei_start (bb->preds);
862 bitmap_set_bit (visited, bb->index);
868 bool has_unvisited_bb = false;
870 /* The inverted traversal loop. */
871 while (sp)
873 edge_iterator ei;
874 basic_block pred;
876 /* Look at the edge on the top of the stack. */
877 ei = stack[sp - 1];
878 bb = ei_edge (ei)->dest;
879 pred = ei_edge (ei)->src;
881 /* Check if the predecessor has been visited yet. */
882 if (! bitmap_bit_p (visited, pred->index))
884 /* Mark that we have visited the destination. */
885 bitmap_set_bit (visited, pred->index);
887 if (EDGE_COUNT (pred->preds) > 0)
888 /* Since the predecessor node has been visited for the first
889 time, check its predecessors. */
890 stack[sp++] = ei_start (pred->preds);
891 else
892 post_order[post_order_num++] = pred->index;
894 else
896 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
897 && ei_one_before_end_p (ei))
898 post_order[post_order_num++] = bb->index;
900 if (!ei_one_before_end_p (ei))
901 ei_next (&stack[sp - 1]);
902 else
903 sp--;
907 /* Detect any infinite loop and activate the kludge.
908 Note that this doesn't check EXIT_BLOCK itself
909 since EXIT_BLOCK is always added after the outer do-while loop. */
910 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
911 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
912 if (!bitmap_bit_p (visited, bb->index))
914 has_unvisited_bb = true;
916 if (EDGE_COUNT (bb->preds) > 0)
918 edge_iterator ei;
919 edge e;
920 basic_block visited_pred = NULL;
922 /* Find an already visited predecessor. */
923 FOR_EACH_EDGE (e, ei, bb->preds)
925 if (bitmap_bit_p (visited, e->src->index))
926 visited_pred = e->src;
929 if (visited_pred)
931 basic_block be = dfs_find_deadend (bb);
932 gcc_assert (be != NULL);
933 bitmap_set_bit (visited, be->index);
934 stack[sp++] = ei_start (be->preds);
935 break;
940 if (has_unvisited_bb && sp == 0)
942 /* No blocks are reachable from EXIT at all.
943 Find a dead-end from the ENTRY, and restart the iteration. */
944 basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR_FOR_FN (cfun));
945 gcc_assert (be != NULL);
946 bitmap_set_bit (visited, be->index);
947 stack[sp++] = ei_start (be->preds);
950 /* The only case the below while fires is
951 when there's an infinite loop. */
953 while (sp);
955 /* EXIT_BLOCK is always included. */
956 post_order[post_order_num++] = EXIT_BLOCK;
958 free (stack);
959 sbitmap_free (visited);
960 return post_order_num;
963 /* Compute the depth first search order of FN and store in the array
964 PRE_ORDER if nonzero. If REV_POST_ORDER is nonzero, return the
965 reverse completion number for each node. Returns the number of nodes
966 visited. A depth first search tries to get as far away from the starting
967 point as quickly as possible.
969 In case the function has unreachable blocks the number of nodes
970 visited does not include them.
972 pre_order is a really a preorder numbering of the graph.
973 rev_post_order is really a reverse postorder numbering of the graph. */
976 pre_and_rev_post_order_compute_fn (struct function *fn,
977 int *pre_order, int *rev_post_order,
978 bool include_entry_exit)
980 edge_iterator *stack;
981 int sp;
982 int pre_order_num = 0;
983 int rev_post_order_num = n_basic_blocks_for_fn (cfun) - 1;
984 sbitmap visited;
986 /* Allocate stack for back-tracking up CFG. */
987 stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
988 sp = 0;
990 if (include_entry_exit)
992 if (pre_order)
993 pre_order[pre_order_num] = ENTRY_BLOCK;
994 pre_order_num++;
995 if (rev_post_order)
996 rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
998 else
999 rev_post_order_num -= NUM_FIXED_BLOCKS;
1001 /* Allocate bitmap to track nodes that have been visited. */
1002 visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
1004 /* None of the nodes in the CFG have been visited yet. */
1005 bitmap_clear (visited);
1007 /* Push the first edge on to the stack. */
1008 stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs);
1010 while (sp)
1012 edge_iterator ei;
1013 basic_block src;
1014 basic_block dest;
1016 /* Look at the edge on the top of the stack. */
1017 ei = stack[sp - 1];
1018 src = ei_edge (ei)->src;
1019 dest = ei_edge (ei)->dest;
1021 /* Check if the edge destination has been visited yet. */
1022 if (dest != EXIT_BLOCK_PTR_FOR_FN (fn)
1023 && ! bitmap_bit_p (visited, dest->index))
1025 /* Mark that we have visited the destination. */
1026 bitmap_set_bit (visited, dest->index);
1028 if (pre_order)
1029 pre_order[pre_order_num] = dest->index;
1031 pre_order_num++;
1033 if (EDGE_COUNT (dest->succs) > 0)
1034 /* Since the DEST node has been visited for the first
1035 time, check its successors. */
1036 stack[sp++] = ei_start (dest->succs);
1037 else if (rev_post_order)
1038 /* There are no successors for the DEST node so assign
1039 its reverse completion number. */
1040 rev_post_order[rev_post_order_num--] = dest->index;
1042 else
1044 if (ei_one_before_end_p (ei)
1045 && src != ENTRY_BLOCK_PTR_FOR_FN (fn)
1046 && rev_post_order)
1047 /* There are no more successors for the SRC node
1048 so assign its reverse completion number. */
1049 rev_post_order[rev_post_order_num--] = src->index;
1051 if (!ei_one_before_end_p (ei))
1052 ei_next (&stack[sp - 1]);
1053 else
1054 sp--;
1058 free (stack);
1059 sbitmap_free (visited);
1061 if (include_entry_exit)
1063 if (pre_order)
1064 pre_order[pre_order_num] = EXIT_BLOCK;
1065 pre_order_num++;
1066 if (rev_post_order)
1067 rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
1070 return pre_order_num;
1073 /* Like pre_and_rev_post_order_compute_fn but operating on the
1074 current function and asserting that all nodes were visited. */
1077 pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
1078 bool include_entry_exit)
1080 int pre_order_num
1081 = pre_and_rev_post_order_compute_fn (cfun, pre_order, rev_post_order,
1082 include_entry_exit);
1083 if (include_entry_exit)
1084 /* The number of nodes visited should be the number of blocks. */
1085 gcc_assert (pre_order_num == n_basic_blocks_for_fn (cfun));
1086 else
1087 /* The number of nodes visited should be the number of blocks minus
1088 the entry and exit blocks which are not visited here. */
1089 gcc_assert (pre_order_num
1090 == (n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS));
1092 return pre_order_num;
1095 /* Compute the depth first search order on the _reverse_ graph and
1096 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1097 Returns the number of nodes visited.
1099 The computation is split into three pieces:
1101 flow_dfs_compute_reverse_init () creates the necessary data
1102 structures.
1104 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1105 structures. The block will start the search.
1107 flow_dfs_compute_reverse_execute () continues (or starts) the
1108 search using the block on the top of the stack, stopping when the
1109 stack is empty.
1111 flow_dfs_compute_reverse_finish () destroys the necessary data
1112 structures.
1114 Thus, the user will probably call ..._init(), call ..._add_bb() to
1115 add a beginning basic block to the stack, call ..._execute(),
1116 possibly add another bb to the stack and again call ..._execute(),
1117 ..., and finally call _finish(). */
1119 /* Initialize the data structures used for depth-first search on the
1120 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1121 added to the basic block stack. DATA is the current depth-first
1122 search context. If INITIALIZE_STACK is nonzero, there is an
1123 element on the stack. */
1125 static void
1126 flow_dfs_compute_reverse_init (depth_first_search_ds *data)
1128 /* Allocate stack for back-tracking up CFG. */
1129 data->stack = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1130 data->sp = 0;
1132 /* Allocate bitmap to track nodes that have been visited. */
1133 data->visited_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
1135 /* None of the nodes in the CFG have been visited yet. */
1136 bitmap_clear (data->visited_blocks);
1138 return;
1141 /* Add the specified basic block to the top of the dfs data
1142 structures. When the search continues, it will start at the
1143 block. */
1145 static void
1146 flow_dfs_compute_reverse_add_bb (depth_first_search_ds *data, basic_block bb)
1148 data->stack[data->sp++] = bb;
1149 bitmap_set_bit (data->visited_blocks, bb->index);
1152 /* Continue the depth-first search through the reverse graph starting with the
1153 block at the stack's top and ending when the stack is empty. Visited nodes
1154 are marked. Returns an unvisited basic block, or NULL if there is none
1155 available. */
1157 static basic_block
1158 flow_dfs_compute_reverse_execute (depth_first_search_ds *data,
1159 basic_block last_unvisited)
1161 basic_block bb;
1162 edge e;
1163 edge_iterator ei;
1165 while (data->sp > 0)
1167 bb = data->stack[--data->sp];
1169 /* Perform depth-first search on adjacent vertices. */
1170 FOR_EACH_EDGE (e, ei, bb->preds)
1171 if (!bitmap_bit_p (data->visited_blocks, e->src->index))
1172 flow_dfs_compute_reverse_add_bb (data, e->src);
1175 /* Determine if there are unvisited basic blocks. */
1176 FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
1177 if (!bitmap_bit_p (data->visited_blocks, bb->index))
1178 return bb;
1180 return NULL;
1183 /* Destroy the data structures needed for depth-first search on the
1184 reverse graph. */
1186 static void
1187 flow_dfs_compute_reverse_finish (depth_first_search_ds *data)
1189 free (data->stack);
1190 sbitmap_free (data->visited_blocks);
1193 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1194 if REVERSE, go against direction of edges. Returns number of blocks
1195 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
1197 dfs_enumerate_from (basic_block bb, int reverse,
1198 bool (*predicate) (const_basic_block, const void *),
1199 basic_block *rslt, int rslt_max, const void *data)
1201 basic_block *st, lbb;
1202 int sp = 0, tv = 0;
1203 unsigned size;
1205 /* A bitmap to keep track of visited blocks. Allocating it each time
1206 this function is called is not possible, since dfs_enumerate_from
1207 is often used on small (almost) disjoint parts of cfg (bodies of
1208 loops), and allocating a large sbitmap would lead to quadratic
1209 behavior. */
1210 static sbitmap visited;
1211 static unsigned v_size;
1213 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1214 #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
1215 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1217 /* Resize the VISITED sbitmap if necessary. */
1218 size = last_basic_block_for_fn (cfun);
1219 if (size < 10)
1220 size = 10;
1222 if (!visited)
1225 visited = sbitmap_alloc (size);
1226 bitmap_clear (visited);
1227 v_size = size;
1229 else if (v_size < size)
1231 /* Ensure that we increase the size of the sbitmap exponentially. */
1232 if (2 * v_size > size)
1233 size = 2 * v_size;
1235 visited = sbitmap_resize (visited, size, 0);
1236 v_size = size;
1239 st = XNEWVEC (basic_block, rslt_max);
1240 rslt[tv++] = st[sp++] = bb;
1241 MARK_VISITED (bb);
1242 while (sp)
1244 edge e;
1245 edge_iterator ei;
1246 lbb = st[--sp];
1247 if (reverse)
1249 FOR_EACH_EDGE (e, ei, lbb->preds)
1250 if (!VISITED_P (e->src) && predicate (e->src, data))
1252 gcc_assert (tv != rslt_max);
1253 rslt[tv++] = st[sp++] = e->src;
1254 MARK_VISITED (e->src);
1257 else
1259 FOR_EACH_EDGE (e, ei, lbb->succs)
1260 if (!VISITED_P (e->dest) && predicate (e->dest, data))
1262 gcc_assert (tv != rslt_max);
1263 rslt[tv++] = st[sp++] = e->dest;
1264 MARK_VISITED (e->dest);
1268 free (st);
1269 for (sp = 0; sp < tv; sp++)
1270 UNMARK_VISITED (rslt[sp]);
1271 return tv;
1272 #undef MARK_VISITED
1273 #undef UNMARK_VISITED
1274 #undef VISITED_P
1278 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1280 This algorithm can be found in Timothy Harvey's PhD thesis, at
1281 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1282 dominance algorithms.
1284 First, we identify each join point, j (any node with more than one
1285 incoming edge is a join point).
1287 We then examine each predecessor, p, of j and walk up the dominator tree
1288 starting at p.
1290 We stop the walk when we reach j's immediate dominator - j is in the
1291 dominance frontier of each of the nodes in the walk, except for j's
1292 immediate dominator. Intuitively, all of the rest of j's dominators are
1293 shared by j's predecessors as well.
1294 Since they dominate j, they will not have j in their dominance frontiers.
1296 The number of nodes touched by this algorithm is equal to the size
1297 of the dominance frontiers, no more, no less.
1301 static void
1302 compute_dominance_frontiers_1 (bitmap_head *frontiers)
1304 edge p;
1305 edge_iterator ei;
1306 basic_block b;
1307 FOR_EACH_BB_FN (b, cfun)
1309 if (EDGE_COUNT (b->preds) >= 2)
1311 FOR_EACH_EDGE (p, ei, b->preds)
1313 basic_block runner = p->src;
1314 basic_block domsb;
1315 if (runner == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1316 continue;
1318 domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1319 while (runner != domsb)
1321 if (!bitmap_set_bit (&frontiers[runner->index],
1322 b->index))
1323 break;
1324 runner = get_immediate_dominator (CDI_DOMINATORS,
1325 runner);
1333 void
1334 compute_dominance_frontiers (bitmap_head *frontiers)
1336 timevar_push (TV_DOM_FRONTIERS);
1338 compute_dominance_frontiers_1 (frontiers);
1340 timevar_pop (TV_DOM_FRONTIERS);
1343 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1344 return a bitmap with all the blocks in the iterated dominance
1345 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
1346 frontier information as returned by compute_dominance_frontiers.
1348 The resulting set of blocks are the potential sites where PHI nodes
1349 are needed. The caller is responsible for freeing the memory
1350 allocated for the return value. */
1352 bitmap
1353 compute_idf (bitmap def_blocks, bitmap_head *dfs)
1355 bitmap_iterator bi;
1356 unsigned bb_index, i;
1357 bitmap phi_insertion_points;
1359 /* Each block can appear at most twice on the work-stack. */
1360 auto_vec<int> work_stack (2 * n_basic_blocks_for_fn (cfun));
1361 phi_insertion_points = BITMAP_ALLOC (NULL);
1363 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1364 vec::quick_push here for speed. This is safe because we know that
1365 the number of definition blocks is no greater than the number of
1366 basic blocks, which is the initial capacity of WORK_STACK. */
1367 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1368 work_stack.quick_push (bb_index);
1370 /* Pop a block off the worklist, add every block that appears in
1371 the original block's DF that we have not already processed to
1372 the worklist. Iterate until the worklist is empty. Blocks
1373 which are added to the worklist are potential sites for
1374 PHI nodes. */
1375 while (work_stack.length () > 0)
1377 bb_index = work_stack.pop ();
1379 /* Since the registration of NEW -> OLD name mappings is done
1380 separately from the call to update_ssa, when updating the SSA
1381 form, the basic blocks where new and/or old names are defined
1382 may have disappeared by CFG cleanup calls. In this case,
1383 we may pull a non-existing block from the work stack. */
1384 gcc_checking_assert (bb_index
1385 < (unsigned) last_basic_block_for_fn (cfun));
1387 EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
1388 0, i, bi)
1390 work_stack.quick_push (i);
1391 bitmap_set_bit (phi_insertion_points, i);
1395 return phi_insertion_points;
1398 /* Intersection and union of preds/succs for sbitmap based data flow
1399 solvers. All four functions defined below take the same arguments:
1400 B is the basic block to perform the operation for. DST is the
1401 target sbitmap, i.e. the result. SRC is an sbitmap vector of size
1402 last_basic_block so that it can be indexed with basic block indices.
1403 DST may be (but does not have to be) SRC[B->index]. */
1405 /* Set the bitmap DST to the intersection of SRC of successors of
1406 basic block B. */
1408 void
1409 bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1411 unsigned int set_size = dst->size;
1412 edge e;
1413 unsigned ix;
1415 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1417 e = EDGE_SUCC (b, ix);
1418 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1419 continue;
1421 bitmap_copy (dst, src[e->dest->index]);
1422 break;
1425 if (e == 0)
1426 bitmap_ones (dst);
1427 else
1428 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
1430 unsigned int i;
1431 SBITMAP_ELT_TYPE *p, *r;
1433 e = EDGE_SUCC (b, ix);
1434 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1435 continue;
1437 p = src[e->dest->index]->elms;
1438 r = dst->elms;
1439 for (i = 0; i < set_size; i++)
1440 *r++ &= *p++;
1444 /* Set the bitmap DST to the intersection of SRC of predecessors of
1445 basic block B. */
1447 void
1448 bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1450 unsigned int set_size = dst->size;
1451 edge e;
1452 unsigned ix;
1454 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1456 e = EDGE_PRED (b, ix);
1457 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1458 continue;
1460 bitmap_copy (dst, src[e->src->index]);
1461 break;
1464 if (e == 0)
1465 bitmap_ones (dst);
1466 else
1467 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
1469 unsigned int i;
1470 SBITMAP_ELT_TYPE *p, *r;
1472 e = EDGE_PRED (b, ix);
1473 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1474 continue;
1476 p = src[e->src->index]->elms;
1477 r = dst->elms;
1478 for (i = 0; i < set_size; i++)
1479 *r++ &= *p++;
1483 /* Set the bitmap DST to the union of SRC of successors of
1484 basic block B. */
1486 void
1487 bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1489 unsigned int set_size = dst->size;
1490 edge e;
1491 unsigned ix;
1493 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1495 e = EDGE_SUCC (b, ix);
1496 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1497 continue;
1499 bitmap_copy (dst, src[e->dest->index]);
1500 break;
1503 if (ix == EDGE_COUNT (b->succs))
1504 bitmap_clear (dst);
1505 else
1506 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
1508 unsigned int i;
1509 SBITMAP_ELT_TYPE *p, *r;
1511 e = EDGE_SUCC (b, ix);
1512 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1513 continue;
1515 p = src[e->dest->index]->elms;
1516 r = dst->elms;
1517 for (i = 0; i < set_size; i++)
1518 *r++ |= *p++;
1522 /* Set the bitmap DST to the union of SRC of predecessors of
1523 basic block B. */
1525 void
1526 bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1528 unsigned int set_size = dst->size;
1529 edge e;
1530 unsigned ix;
1532 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1534 e = EDGE_PRED (b, ix);
1535 if (e->src== ENTRY_BLOCK_PTR_FOR_FN (cfun))
1536 continue;
1538 bitmap_copy (dst, src[e->src->index]);
1539 break;
1542 if (ix == EDGE_COUNT (b->preds))
1543 bitmap_clear (dst);
1544 else
1545 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
1547 unsigned int i;
1548 SBITMAP_ELT_TYPE *p, *r;
1550 e = EDGE_PRED (b, ix);
1551 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1552 continue;
1554 p = src[e->src->index]->elms;
1555 r = dst->elms;
1556 for (i = 0; i < set_size; i++)
1557 *r++ |= *p++;
1561 /* Returns the list of basic blocks in the function in an order that guarantees
1562 that if a block X has just a single predecessor Y, then Y is after X in the
1563 ordering. */
1565 basic_block *
1566 single_pred_before_succ_order (void)
1568 basic_block x, y;
1569 basic_block *order = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1570 unsigned n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
1571 unsigned np, i;
1572 sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
1574 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1575 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1577 bitmap_clear (visited);
1579 MARK_VISITED (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1580 FOR_EACH_BB_FN (x, cfun)
1582 if (VISITED_P (x))
1583 continue;
1585 /* Walk the predecessors of x as long as they have precisely one
1586 predecessor and add them to the list, so that they get stored
1587 after x. */
1588 for (y = x, np = 1;
1589 single_pred_p (y) && !VISITED_P (single_pred (y));
1590 y = single_pred (y))
1591 np++;
1592 for (y = x, i = n - np;
1593 single_pred_p (y) && !VISITED_P (single_pred (y));
1594 y = single_pred (y), i++)
1596 order[i] = y;
1597 MARK_VISITED (y);
1599 order[i] = y;
1600 MARK_VISITED (y);
1602 gcc_assert (i == n - 1);
1603 n -= np;
1606 sbitmap_free (visited);
1607 gcc_assert (n == 0);
1608 return order;
1610 #undef MARK_VISITED
1611 #undef VISITED_P