Daily bump.
[gcc.git] / gcc / cfganal.c
1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987-2012 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 /* This file contains various simple utilities to analyze the CFG. */
21
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"
30
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;
35
36 /* number of edges in the stack. That is, positions 0, ..., sp-1
37 have edges. */
38 unsigned int sp;
39
40 /* record of basic blocks already seen by depth-first search */
41 sbitmap visited_blocks;
42 };
43 typedef struct depth_first_search_dsS *depth_first_search_ds;
44
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);
51 \f
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:
55
56 Advanced Compiler Design and Implementation
57 Steven Muchnick
58 Morgan Kaufmann, 1997
59
60 and heavily borrowed from pre_and_rev_post_order_compute. */
61
62 bool
63 mark_dfs_back_edges (void)
64 {
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;
73
74 /* Allocate the preorder and postorder number arrays. */
75 pre = XCNEWVEC (int, last_basic_block);
76 post = XCNEWVEC (int, last_basic_block);
77
78 /* Allocate stack for back-tracking up CFG. */
79 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
80 sp = 0;
81
82 /* Allocate bitmap to track nodes that have been visited. */
83 visited = sbitmap_alloc (last_basic_block);
84
85 /* None of the nodes in the CFG have been visited yet. */
86 bitmap_clear (visited);
87
88 /* Push the first edge on to the stack. */
89 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
90
91 while (sp)
92 {
93 edge_iterator ei;
94 basic_block src;
95 basic_block dest;
96
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;
102
103 /* Check if the edge destination has been visited yet. */
104 if (dest != EXIT_BLOCK_PTR && ! bitmap_bit_p (visited, dest->index))
105 {
106 /* Mark that we have visited the destination. */
107 bitmap_set_bit (visited, dest->index);
108
109 pre[dest->index] = prenum++;
110 if (EDGE_COUNT (dest->succs) > 0)
111 {
112 /* Since the DEST node has been visited for the first
113 time, check its successors. */
114 stack[sp++] = ei_start (dest->succs);
115 }
116 else
117 post[dest->index] = postnum++;
118 }
119 else
120 {
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;
125
126 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR)
127 post[src->index] = postnum++;
128
129 if (!ei_one_before_end_p (ei))
130 ei_next (&stack[sp - 1]);
131 else
132 sp--;
133 }
134 }
135
136 free (pre);
137 free (post);
138 free (stack);
139 sbitmap_free (visited);
140
141 return found;
142 }
143
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. */
147
148 void
149 find_unreachable_blocks (void)
150 {
151 edge e;
152 edge_iterator ei;
153 basic_block *tos, *worklist, bb;
154
155 tos = worklist = XNEWVEC (basic_block, n_basic_blocks);
156
157 /* Clear all the reachability flags. */
158
159 FOR_EACH_BB (bb)
160 bb->flags &= ~BB_REACHABLE;
161
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. */
165
166 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
167 {
168 *tos++ = e->dest;
169
170 /* Mark the block reachable. */
171 e->dest->flags |= BB_REACHABLE;
172 }
173
174 /* Iterate: find everything reachable from what we've already seen. */
175
176 while (tos != worklist)
177 {
178 basic_block b = *--tos;
179
180 FOR_EACH_EDGE (e, ei, b->succs)
181 {
182 basic_block dest = e->dest;
183
184 if (!(dest->flags & BB_REACHABLE))
185 {
186 *tos++ = dest;
187 dest->flags |= BB_REACHABLE;
188 }
189 }
190 }
191
192 free (worklist);
193 }
194 \f
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. */
203
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. */
207
208 struct edge_list *
209 create_edge_list (void)
210 {
211 struct edge_list *elist;
212 edge e;
213 int num_edges;
214 basic_block bb;
215 edge_iterator ei;
216
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)
221 {
222 num_edges += EDGE_COUNT (bb->succs);
223 }
224
225 elist = XNEW (struct edge_list);
226 elist->num_edges = num_edges;
227 elist->index_to_edge = XNEWVEC (edge, num_edges);
228
229 num_edges = 0;
230
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;
235
236 return elist;
237 }
238
239 /* This function free's memory associated with an edge list. */
240
241 void
242 free_edge_list (struct edge_list *elist)
243 {
244 if (elist)
245 {
246 free (elist->index_to_edge);
247 free (elist);
248 }
249 }
250
251 /* This function provides debug output showing an edge list. */
252
253 DEBUG_FUNCTION void
254 print_edge_list (FILE *f, struct edge_list *elist)
255 {
256 int x;
257
258 fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
259 n_basic_blocks, elist->num_edges);
260
261 for (x = 0; x < elist->num_edges; x++)
262 {
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);
268
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);
273 }
274 }
275
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. */
279
280 DEBUG_FUNCTION void
281 verify_edge_list (FILE *f, struct edge_list *elist)
282 {
283 int pred, succ, index;
284 edge e;
285 basic_block bb, p, s;
286 edge_iterator ei;
287
288 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
289 {
290 FOR_EACH_EDGE (e, ei, bb->succs)
291 {
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)
296 {
297 fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
298 continue;
299 }
300
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);
307 }
308 }
309
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! */
312
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)
315 {
316 int found_edge = 0;
317
318 FOR_EACH_EDGE (e, ei, p->succs)
319 if (e->dest == s)
320 {
321 found_edge = 1;
322 break;
323 }
324
325 FOR_EACH_EDGE (e, ei, s->preds)
326 if (e->src == p)
327 {
328 found_edge = 1;
329 break;
330 }
331
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));
340 }
341 }
342
343 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
344 If no such edge exists, return NULL. */
345
346 edge
347 find_edge (basic_block pred, basic_block succ)
348 {
349 edge e;
350 edge_iterator ei;
351
352 if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
353 {
354 FOR_EACH_EDGE (e, ei, pred->succs)
355 if (e->dest == succ)
356 return e;
357 }
358 else
359 {
360 FOR_EACH_EDGE (e, ei, succ->preds)
361 if (e->src == pred)
362 return e;
363 }
364
365 return NULL;
366 }
367
368 /* This routine will determine what, if any, edge there is between
369 a specified predecessor and successor. */
370
371 int
372 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
373 {
374 int x;
375
376 for (x = 0; x < NUM_EDGES (edge_list); x++)
377 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
378 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
379 return x;
380
381 return (EDGE_INDEX_NO_EDGE);
382 }
383 \f
384 /* This routine will remove any fake predecessor edges for a basic block.
385 When the edge is removed, it is also removed from whatever successor
386 list it is in. */
387
388 static void
389 remove_fake_predecessors (basic_block bb)
390 {
391 edge e;
392 edge_iterator ei;
393
394 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
395 {
396 if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
397 remove_edge (e);
398 else
399 ei_next (&ei);
400 }
401 }
402
403 /* This routine will remove all fake edges from the flow graph. If
404 we remove all fake successors, it will automatically remove all
405 fake predecessors. */
406
407 void
408 remove_fake_edges (void)
409 {
410 basic_block bb;
411
412 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
413 remove_fake_predecessors (bb);
414 }
415
416 /* This routine will remove all fake edges to the EXIT_BLOCK. */
417
418 void
419 remove_fake_exit_edges (void)
420 {
421 remove_fake_predecessors (EXIT_BLOCK_PTR);
422 }
423
424
425 /* This function will add a fake edge between any block which has no
426 successors, and the exit block. Some data flow equations require these
427 edges to exist. */
428
429 void
430 add_noreturn_fake_exit_edges (void)
431 {
432 basic_block bb;
433
434 FOR_EACH_BB (bb)
435 if (EDGE_COUNT (bb->succs) == 0)
436 make_single_succ_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
437 }
438
439 /* This function adds a fake edge between any infinite loops to the
440 exit block. Some optimizations require a path from each node to
441 the exit node.
442
443 See also Morgan, Figure 3.10, pp. 82-83.
444
445 The current implementation is ugly, not attempting to minimize the
446 number of inserted fake edges. To reduce the number of fake edges
447 to insert, add fake edges from _innermost_ loops containing only
448 nodes not reachable from the exit block. */
449
450 void
451 connect_infinite_loops_to_exit (void)
452 {
453 basic_block unvisited_block = EXIT_BLOCK_PTR;
454 basic_block deadend_block;
455 struct depth_first_search_dsS dfs_ds;
456
457 /* Perform depth-first search in the reverse graph to find nodes
458 reachable from the exit block. */
459 flow_dfs_compute_reverse_init (&dfs_ds);
460 flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
461
462 /* Repeatedly add fake edges, updating the unreachable nodes. */
463 while (1)
464 {
465 unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
466 unvisited_block);
467 if (!unvisited_block)
468 break;
469
470 deadend_block = dfs_find_deadend (unvisited_block);
471 make_edge (deadend_block, EXIT_BLOCK_PTR, EDGE_FAKE);
472 flow_dfs_compute_reverse_add_bb (&dfs_ds, deadend_block);
473 }
474
475 flow_dfs_compute_reverse_finish (&dfs_ds);
476 return;
477 }
478 \f
479 /* Compute reverse top sort order. This is computing a post order
480 numbering of the graph. If INCLUDE_ENTRY_EXIT is true, then
481 ENTRY_BLOCK and EXIT_BLOCK are included. If DELETE_UNREACHABLE is
482 true, unreachable blocks are deleted. */
483
484 int
485 post_order_compute (int *post_order, bool include_entry_exit,
486 bool delete_unreachable)
487 {
488 edge_iterator *stack;
489 int sp;
490 int post_order_num = 0;
491 sbitmap visited;
492 int count;
493
494 if (include_entry_exit)
495 post_order[post_order_num++] = EXIT_BLOCK;
496
497 /* Allocate stack for back-tracking up CFG. */
498 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
499 sp = 0;
500
501 /* Allocate bitmap to track nodes that have been visited. */
502 visited = sbitmap_alloc (last_basic_block);
503
504 /* None of the nodes in the CFG have been visited yet. */
505 bitmap_clear (visited);
506
507 /* Push the first edge on to the stack. */
508 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
509
510 while (sp)
511 {
512 edge_iterator ei;
513 basic_block src;
514 basic_block dest;
515
516 /* Look at the edge on the top of the stack. */
517 ei = stack[sp - 1];
518 src = ei_edge (ei)->src;
519 dest = ei_edge (ei)->dest;
520
521 /* Check if the edge destination has been visited yet. */
522 if (dest != EXIT_BLOCK_PTR && ! bitmap_bit_p (visited, dest->index))
523 {
524 /* Mark that we have visited the destination. */
525 bitmap_set_bit (visited, dest->index);
526
527 if (EDGE_COUNT (dest->succs) > 0)
528 /* Since the DEST node has been visited for the first
529 time, check its successors. */
530 stack[sp++] = ei_start (dest->succs);
531 else
532 post_order[post_order_num++] = dest->index;
533 }
534 else
535 {
536 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR)
537 post_order[post_order_num++] = src->index;
538
539 if (!ei_one_before_end_p (ei))
540 ei_next (&stack[sp - 1]);
541 else
542 sp--;
543 }
544 }
545
546 if (include_entry_exit)
547 {
548 post_order[post_order_num++] = ENTRY_BLOCK;
549 count = post_order_num;
550 }
551 else
552 count = post_order_num + 2;
553
554 /* Delete the unreachable blocks if some were found and we are
555 supposed to do it. */
556 if (delete_unreachable && (count != n_basic_blocks))
557 {
558 basic_block b;
559 basic_block next_bb;
560 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
561 {
562 next_bb = b->next_bb;
563
564 if (!(bitmap_bit_p (visited, b->index)))
565 delete_basic_block (b);
566 }
567
568 tidy_fallthru_edges ();
569 }
570
571 free (stack);
572 sbitmap_free (visited);
573 return post_order_num;
574 }
575
576
577 /* Helper routine for inverted_post_order_compute
578 flow_dfs_compute_reverse_execute, and the reverse-CFG
579 deapth first search in dominance.c.
580 BB has to belong to a region of CFG
581 unreachable by inverted traversal from the exit.
582 i.e. there's no control flow path from ENTRY to EXIT
583 that contains this BB.
584 This can happen in two cases - if there's an infinite loop
585 or if there's a block that has no successor
586 (call to a function with no return).
587 Some RTL passes deal with this condition by
588 calling connect_infinite_loops_to_exit () and/or
589 add_noreturn_fake_exit_edges ().
590 However, those methods involve modifying the CFG itself
591 which may not be desirable.
592 Hence, we deal with the infinite loop/no return cases
593 by identifying a unique basic block that can reach all blocks
594 in such a region by inverted traversal.
595 This function returns a basic block that guarantees
596 that all blocks in the region are reachable
597 by starting an inverted traversal from the returned block. */
598
599 basic_block
600 dfs_find_deadend (basic_block bb)
601 {
602 bitmap visited = BITMAP_ALLOC (NULL);
603
604 for (;;)
605 {
606 if (EDGE_COUNT (bb->succs) == 0
607 || ! bitmap_set_bit (visited, bb->index))
608 {
609 BITMAP_FREE (visited);
610 return bb;
611 }
612
613 bb = EDGE_SUCC (bb, 0)->dest;
614 }
615
616 gcc_unreachable ();
617 }
618
619
620 /* Compute the reverse top sort order of the inverted CFG
621 i.e. starting from the exit block and following the edges backward
622 (from successors to predecessors).
623 This ordering can be used for forward dataflow problems among others.
624
625 This function assumes that all blocks in the CFG are reachable
626 from the ENTRY (but not necessarily from EXIT).
627
628 If there's an infinite loop,
629 a simple inverted traversal starting from the blocks
630 with no successors can't visit all blocks.
631 To solve this problem, we first do inverted traversal
632 starting from the blocks with no successor.
633 And if there's any block left that's not visited by the regular
634 inverted traversal from EXIT,
635 those blocks are in such problematic region.
636 Among those, we find one block that has
637 any visited predecessor (which is an entry into such a region),
638 and start looking for a "dead end" from that block
639 and do another inverted traversal from that block. */
640
641 int
642 inverted_post_order_compute (int *post_order)
643 {
644 basic_block bb;
645 edge_iterator *stack;
646 int sp;
647 int post_order_num = 0;
648 sbitmap visited;
649
650 /* Allocate stack for back-tracking up CFG. */
651 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
652 sp = 0;
653
654 /* Allocate bitmap to track nodes that have been visited. */
655 visited = sbitmap_alloc (last_basic_block);
656
657 /* None of the nodes in the CFG have been visited yet. */
658 bitmap_clear (visited);
659
660 /* Put all blocks that have no successor into the initial work list. */
661 FOR_ALL_BB (bb)
662 if (EDGE_COUNT (bb->succs) == 0)
663 {
664 /* Push the initial edge on to the stack. */
665 if (EDGE_COUNT (bb->preds) > 0)
666 {
667 stack[sp++] = ei_start (bb->preds);
668 bitmap_set_bit (visited, bb->index);
669 }
670 }
671
672 do
673 {
674 bool has_unvisited_bb = false;
675
676 /* The inverted traversal loop. */
677 while (sp)
678 {
679 edge_iterator ei;
680 basic_block pred;
681
682 /* Look at the edge on the top of the stack. */
683 ei = stack[sp - 1];
684 bb = ei_edge (ei)->dest;
685 pred = ei_edge (ei)->src;
686
687 /* Check if the predecessor has been visited yet. */
688 if (! bitmap_bit_p (visited, pred->index))
689 {
690 /* Mark that we have visited the destination. */
691 bitmap_set_bit (visited, pred->index);
692
693 if (EDGE_COUNT (pred->preds) > 0)
694 /* Since the predecessor node has been visited for the first
695 time, check its predecessors. */
696 stack[sp++] = ei_start (pred->preds);
697 else
698 post_order[post_order_num++] = pred->index;
699 }
700 else
701 {
702 if (bb != EXIT_BLOCK_PTR && ei_one_before_end_p (ei))
703 post_order[post_order_num++] = bb->index;
704
705 if (!ei_one_before_end_p (ei))
706 ei_next (&stack[sp - 1]);
707 else
708 sp--;
709 }
710 }
711
712 /* Detect any infinite loop and activate the kludge.
713 Note that this doesn't check EXIT_BLOCK itself
714 since EXIT_BLOCK is always added after the outer do-while loop. */
715 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
716 if (!bitmap_bit_p (visited, bb->index))
717 {
718 has_unvisited_bb = true;
719
720 if (EDGE_COUNT (bb->preds) > 0)
721 {
722 edge_iterator ei;
723 edge e;
724 basic_block visited_pred = NULL;
725
726 /* Find an already visited predecessor. */
727 FOR_EACH_EDGE (e, ei, bb->preds)
728 {
729 if (bitmap_bit_p (visited, e->src->index))
730 visited_pred = e->src;
731 }
732
733 if (visited_pred)
734 {
735 basic_block be = dfs_find_deadend (bb);
736 gcc_assert (be != NULL);
737 bitmap_set_bit (visited, be->index);
738 stack[sp++] = ei_start (be->preds);
739 break;
740 }
741 }
742 }
743
744 if (has_unvisited_bb && sp == 0)
745 {
746 /* No blocks are reachable from EXIT at all.
747 Find a dead-end from the ENTRY, and restart the iteration. */
748 basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR);
749 gcc_assert (be != NULL);
750 bitmap_set_bit (visited, be->index);
751 stack[sp++] = ei_start (be->preds);
752 }
753
754 /* The only case the below while fires is
755 when there's an infinite loop. */
756 }
757 while (sp);
758
759 /* EXIT_BLOCK is always included. */
760 post_order[post_order_num++] = EXIT_BLOCK;
761
762 free (stack);
763 sbitmap_free (visited);
764 return post_order_num;
765 }
766
767 /* Compute the depth first search order and store in the array
768 PRE_ORDER if nonzero, marking the nodes visited in VISITED. If
769 REV_POST_ORDER is nonzero, return the reverse completion number for each
770 node. Returns the number of nodes visited. A depth first search
771 tries to get as far away from the starting point as quickly as
772 possible.
773
774 pre_order is a really a preorder numbering of the graph.
775 rev_post_order is really a reverse postorder numbering of the graph.
776 */
777
778 int
779 pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
780 bool include_entry_exit)
781 {
782 edge_iterator *stack;
783 int sp;
784 int pre_order_num = 0;
785 int rev_post_order_num = n_basic_blocks - 1;
786 sbitmap visited;
787
788 /* Allocate stack for back-tracking up CFG. */
789 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
790 sp = 0;
791
792 if (include_entry_exit)
793 {
794 if (pre_order)
795 pre_order[pre_order_num] = ENTRY_BLOCK;
796 pre_order_num++;
797 if (rev_post_order)
798 rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
799 }
800 else
801 rev_post_order_num -= NUM_FIXED_BLOCKS;
802
803 /* Allocate bitmap to track nodes that have been visited. */
804 visited = sbitmap_alloc (last_basic_block);
805
806 /* None of the nodes in the CFG have been visited yet. */
807 bitmap_clear (visited);
808
809 /* Push the first edge on to the stack. */
810 stack[sp++] = ei_start (ENTRY_BLOCK_PTR->succs);
811
812 while (sp)
813 {
814 edge_iterator ei;
815 basic_block src;
816 basic_block dest;
817
818 /* Look at the edge on the top of the stack. */
819 ei = stack[sp - 1];
820 src = ei_edge (ei)->src;
821 dest = ei_edge (ei)->dest;
822
823 /* Check if the edge destination has been visited yet. */
824 if (dest != EXIT_BLOCK_PTR && ! bitmap_bit_p (visited, dest->index))
825 {
826 /* Mark that we have visited the destination. */
827 bitmap_set_bit (visited, dest->index);
828
829 if (pre_order)
830 pre_order[pre_order_num] = dest->index;
831
832 pre_order_num++;
833
834 if (EDGE_COUNT (dest->succs) > 0)
835 /* Since the DEST node has been visited for the first
836 time, check its successors. */
837 stack[sp++] = ei_start (dest->succs);
838 else if (rev_post_order)
839 /* There are no successors for the DEST node so assign
840 its reverse completion number. */
841 rev_post_order[rev_post_order_num--] = dest->index;
842 }
843 else
844 {
845 if (ei_one_before_end_p (ei) && src != ENTRY_BLOCK_PTR
846 && rev_post_order)
847 /* There are no more successors for the SRC node
848 so assign its reverse completion number. */
849 rev_post_order[rev_post_order_num--] = src->index;
850
851 if (!ei_one_before_end_p (ei))
852 ei_next (&stack[sp - 1]);
853 else
854 sp--;
855 }
856 }
857
858 free (stack);
859 sbitmap_free (visited);
860
861 if (include_entry_exit)
862 {
863 if (pre_order)
864 pre_order[pre_order_num] = EXIT_BLOCK;
865 pre_order_num++;
866 if (rev_post_order)
867 rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
868 /* The number of nodes visited should be the number of blocks. */
869 gcc_assert (pre_order_num == n_basic_blocks);
870 }
871 else
872 /* The number of nodes visited should be the number of blocks minus
873 the entry and exit blocks which are not visited here. */
874 gcc_assert (pre_order_num == n_basic_blocks - NUM_FIXED_BLOCKS);
875
876 return pre_order_num;
877 }
878
879 /* Compute the depth first search order on the _reverse_ graph and
880 store in the array DFS_ORDER, marking the nodes visited in VISITED.
881 Returns the number of nodes visited.
882
883 The computation is split into three pieces:
884
885 flow_dfs_compute_reverse_init () creates the necessary data
886 structures.
887
888 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
889 structures. The block will start the search.
890
891 flow_dfs_compute_reverse_execute () continues (or starts) the
892 search using the block on the top of the stack, stopping when the
893 stack is empty.
894
895 flow_dfs_compute_reverse_finish () destroys the necessary data
896 structures.
897
898 Thus, the user will probably call ..._init(), call ..._add_bb() to
899 add a beginning basic block to the stack, call ..._execute(),
900 possibly add another bb to the stack and again call ..._execute(),
901 ..., and finally call _finish(). */
902
903 /* Initialize the data structures used for depth-first search on the
904 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
905 added to the basic block stack. DATA is the current depth-first
906 search context. If INITIALIZE_STACK is nonzero, there is an
907 element on the stack. */
908
909 static void
910 flow_dfs_compute_reverse_init (depth_first_search_ds data)
911 {
912 /* Allocate stack for back-tracking up CFG. */
913 data->stack = XNEWVEC (basic_block, n_basic_blocks);
914 data->sp = 0;
915
916 /* Allocate bitmap to track nodes that have been visited. */
917 data->visited_blocks = sbitmap_alloc (last_basic_block);
918
919 /* None of the nodes in the CFG have been visited yet. */
920 bitmap_clear (data->visited_blocks);
921
922 return;
923 }
924
925 /* Add the specified basic block to the top of the dfs data
926 structures. When the search continues, it will start at the
927 block. */
928
929 static void
930 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
931 {
932 data->stack[data->sp++] = bb;
933 bitmap_set_bit (data->visited_blocks, bb->index);
934 }
935
936 /* Continue the depth-first search through the reverse graph starting with the
937 block at the stack's top and ending when the stack is empty. Visited nodes
938 are marked. Returns an unvisited basic block, or NULL if there is none
939 available. */
940
941 static basic_block
942 flow_dfs_compute_reverse_execute (depth_first_search_ds data,
943 basic_block last_unvisited)
944 {
945 basic_block bb;
946 edge e;
947 edge_iterator ei;
948
949 while (data->sp > 0)
950 {
951 bb = data->stack[--data->sp];
952
953 /* Perform depth-first search on adjacent vertices. */
954 FOR_EACH_EDGE (e, ei, bb->preds)
955 if (!bitmap_bit_p (data->visited_blocks, e->src->index))
956 flow_dfs_compute_reverse_add_bb (data, e->src);
957 }
958
959 /* Determine if there are unvisited basic blocks. */
960 FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
961 if (!bitmap_bit_p (data->visited_blocks, bb->index))
962 return bb;
963
964 return NULL;
965 }
966
967 /* Destroy the data structures needed for depth-first search on the
968 reverse graph. */
969
970 static void
971 flow_dfs_compute_reverse_finish (depth_first_search_ds data)
972 {
973 free (data->stack);
974 sbitmap_free (data->visited_blocks);
975 }
976
977 /* Performs dfs search from BB over vertices satisfying PREDICATE;
978 if REVERSE, go against direction of edges. Returns number of blocks
979 found and their list in RSLT. RSLT can contain at most RSLT_MAX items. */
980 int
981 dfs_enumerate_from (basic_block bb, int reverse,
982 bool (*predicate) (const_basic_block, const void *),
983 basic_block *rslt, int rslt_max, const void *data)
984 {
985 basic_block *st, lbb;
986 int sp = 0, tv = 0;
987 unsigned size;
988
989 /* A bitmap to keep track of visited blocks. Allocating it each time
990 this function is called is not possible, since dfs_enumerate_from
991 is often used on small (almost) disjoint parts of cfg (bodies of
992 loops), and allocating a large sbitmap would lead to quadratic
993 behavior. */
994 static sbitmap visited;
995 static unsigned v_size;
996
997 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
998 #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
999 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1000
1001 /* Resize the VISITED sbitmap if necessary. */
1002 size = last_basic_block;
1003 if (size < 10)
1004 size = 10;
1005
1006 if (!visited)
1007 {
1008
1009 visited = sbitmap_alloc (size);
1010 bitmap_clear (visited);
1011 v_size = size;
1012 }
1013 else if (v_size < size)
1014 {
1015 /* Ensure that we increase the size of the sbitmap exponentially. */
1016 if (2 * v_size > size)
1017 size = 2 * v_size;
1018
1019 visited = sbitmap_resize (visited, size, 0);
1020 v_size = size;
1021 }
1022
1023 st = XNEWVEC (basic_block, rslt_max);
1024 rslt[tv++] = st[sp++] = bb;
1025 MARK_VISITED (bb);
1026 while (sp)
1027 {
1028 edge e;
1029 edge_iterator ei;
1030 lbb = st[--sp];
1031 if (reverse)
1032 {
1033 FOR_EACH_EDGE (e, ei, lbb->preds)
1034 if (!VISITED_P (e->src) && predicate (e->src, data))
1035 {
1036 gcc_assert (tv != rslt_max);
1037 rslt[tv++] = st[sp++] = e->src;
1038 MARK_VISITED (e->src);
1039 }
1040 }
1041 else
1042 {
1043 FOR_EACH_EDGE (e, ei, lbb->succs)
1044 if (!VISITED_P (e->dest) && predicate (e->dest, data))
1045 {
1046 gcc_assert (tv != rslt_max);
1047 rslt[tv++] = st[sp++] = e->dest;
1048 MARK_VISITED (e->dest);
1049 }
1050 }
1051 }
1052 free (st);
1053 for (sp = 0; sp < tv; sp++)
1054 UNMARK_VISITED (rslt[sp]);
1055 return tv;
1056 #undef MARK_VISITED
1057 #undef UNMARK_VISITED
1058 #undef VISITED_P
1059 }
1060
1061
1062 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1063
1064 This algorithm can be found in Timothy Harvey's PhD thesis, at
1065 http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1066 dominance algorithms.
1067
1068 First, we identify each join point, j (any node with more than one
1069 incoming edge is a join point).
1070
1071 We then examine each predecessor, p, of j and walk up the dominator tree
1072 starting at p.
1073
1074 We stop the walk when we reach j's immediate dominator - j is in the
1075 dominance frontier of each of the nodes in the walk, except for j's
1076 immediate dominator. Intuitively, all of the rest of j's dominators are
1077 shared by j's predecessors as well.
1078 Since they dominate j, they will not have j in their dominance frontiers.
1079
1080 The number of nodes touched by this algorithm is equal to the size
1081 of the dominance frontiers, no more, no less.
1082 */
1083
1084
1085 static void
1086 compute_dominance_frontiers_1 (bitmap_head *frontiers)
1087 {
1088 edge p;
1089 edge_iterator ei;
1090 basic_block b;
1091 FOR_EACH_BB (b)
1092 {
1093 if (EDGE_COUNT (b->preds) >= 2)
1094 {
1095 FOR_EACH_EDGE (p, ei, b->preds)
1096 {
1097 basic_block runner = p->src;
1098 basic_block domsb;
1099 if (runner == ENTRY_BLOCK_PTR)
1100 continue;
1101
1102 domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1103 while (runner != domsb)
1104 {
1105 if (!bitmap_set_bit (&frontiers[runner->index],
1106 b->index))
1107 break;
1108 runner = get_immediate_dominator (CDI_DOMINATORS,
1109 runner);
1110 }
1111 }
1112 }
1113 }
1114 }
1115
1116
1117 void
1118 compute_dominance_frontiers (bitmap_head *frontiers)
1119 {
1120 timevar_push (TV_DOM_FRONTIERS);
1121
1122 compute_dominance_frontiers_1 (frontiers);
1123
1124 timevar_pop (TV_DOM_FRONTIERS);
1125 }
1126
1127 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1128 return a bitmap with all the blocks in the iterated dominance
1129 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
1130 frontier information as returned by compute_dominance_frontiers.
1131
1132 The resulting set of blocks are the potential sites where PHI nodes
1133 are needed. The caller is responsible for freeing the memory
1134 allocated for the return value. */
1135
1136 bitmap
1137 compute_idf (bitmap def_blocks, bitmap_head *dfs)
1138 {
1139 bitmap_iterator bi;
1140 unsigned bb_index, i;
1141 vec<int> work_stack;
1142 bitmap phi_insertion_points;
1143
1144 work_stack.create (n_basic_blocks);
1145 phi_insertion_points = BITMAP_ALLOC (NULL);
1146
1147 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1148 vec::quick_push here for speed. This is safe because we know that
1149 the number of definition blocks is no greater than the number of
1150 basic blocks, which is the initial capacity of WORK_STACK. */
1151 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1152 work_stack.quick_push (bb_index);
1153
1154 /* Pop a block off the worklist, add every block that appears in
1155 the original block's DF that we have not already processed to
1156 the worklist. Iterate until the worklist is empty. Blocks
1157 which are added to the worklist are potential sites for
1158 PHI nodes. */
1159 while (work_stack.length () > 0)
1160 {
1161 bb_index = work_stack.pop ();
1162
1163 /* Since the registration of NEW -> OLD name mappings is done
1164 separately from the call to update_ssa, when updating the SSA
1165 form, the basic blocks where new and/or old names are defined
1166 may have disappeared by CFG cleanup calls. In this case,
1167 we may pull a non-existing block from the work stack. */
1168 gcc_assert (bb_index < (unsigned) last_basic_block);
1169
1170 EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
1171 0, i, bi)
1172 {
1173 /* Use a safe push because if there is a definition of VAR
1174 in every basic block, then WORK_STACK may eventually have
1175 more than N_BASIC_BLOCK entries. */
1176 work_stack.safe_push (i);
1177 bitmap_set_bit (phi_insertion_points, i);
1178 }
1179 }
1180
1181 work_stack.release ();
1182
1183 return phi_insertion_points;
1184 }
1185
1186 /* Intersection and union of preds/succs for sbitmap based data flow
1187 solvers. All four functions defined below take the same arguments:
1188 B is the basic block to perform the operation for. DST is the
1189 target sbitmap, i.e. the result. SRC is an sbitmap vector of size
1190 last_basic_block so that it can be indexed with basic block indices.
1191 DST may be (but does not have to be) SRC[B->index]. */
1192
1193 /* Set the bitmap DST to the intersection of SRC of successors of
1194 basic block B. */
1195
1196 void
1197 bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1198 {
1199 unsigned int set_size = dst->size;
1200 edge e;
1201 unsigned ix;
1202
1203 gcc_assert (!dst->popcount);
1204
1205 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1206 {
1207 e = EDGE_SUCC (b, ix);
1208 if (e->dest == EXIT_BLOCK_PTR)
1209 continue;
1210
1211 bitmap_copy (dst, src[e->dest->index]);
1212 break;
1213 }
1214
1215 if (e == 0)
1216 bitmap_ones (dst);
1217 else
1218 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
1219 {
1220 unsigned int i;
1221 SBITMAP_ELT_TYPE *p, *r;
1222
1223 e = EDGE_SUCC (b, ix);
1224 if (e->dest == EXIT_BLOCK_PTR)
1225 continue;
1226
1227 p = src[e->dest->index]->elms;
1228 r = dst->elms;
1229 for (i = 0; i < set_size; i++)
1230 *r++ &= *p++;
1231 }
1232 }
1233
1234 /* Set the bitmap DST to the intersection of SRC of predecessors of
1235 basic block B. */
1236
1237 void
1238 bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1239 {
1240 unsigned int set_size = dst->size;
1241 edge e;
1242 unsigned ix;
1243
1244 gcc_assert (!dst->popcount);
1245
1246 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1247 {
1248 e = EDGE_PRED (b, ix);
1249 if (e->src == ENTRY_BLOCK_PTR)
1250 continue;
1251
1252 bitmap_copy (dst, src[e->src->index]);
1253 break;
1254 }
1255
1256 if (e == 0)
1257 bitmap_ones (dst);
1258 else
1259 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
1260 {
1261 unsigned int i;
1262 SBITMAP_ELT_TYPE *p, *r;
1263
1264 e = EDGE_PRED (b, ix);
1265 if (e->src == ENTRY_BLOCK_PTR)
1266 continue;
1267
1268 p = src[e->src->index]->elms;
1269 r = dst->elms;
1270 for (i = 0; i < set_size; i++)
1271 *r++ &= *p++;
1272 }
1273 }
1274
1275 /* Set the bitmap DST to the union of SRC of successors of
1276 basic block B. */
1277
1278 void
1279 bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1280 {
1281 unsigned int set_size = dst->size;
1282 edge e;
1283 unsigned ix;
1284
1285 gcc_assert (!dst->popcount);
1286
1287 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1288 {
1289 e = EDGE_SUCC (b, ix);
1290 if (e->dest == EXIT_BLOCK_PTR)
1291 continue;
1292
1293 bitmap_copy (dst, src[e->dest->index]);
1294 break;
1295 }
1296
1297 if (ix == EDGE_COUNT (b->succs))
1298 bitmap_clear (dst);
1299 else
1300 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
1301 {
1302 unsigned int i;
1303 SBITMAP_ELT_TYPE *p, *r;
1304
1305 e = EDGE_SUCC (b, ix);
1306 if (e->dest == EXIT_BLOCK_PTR)
1307 continue;
1308
1309 p = src[e->dest->index]->elms;
1310 r = dst->elms;
1311 for (i = 0; i < set_size; i++)
1312 *r++ |= *p++;
1313 }
1314 }
1315
1316 /* Set the bitmap DST to the union of SRC of predecessors of
1317 basic block B. */
1318
1319 void
1320 bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1321 {
1322 unsigned int set_size = dst->size;
1323 edge e;
1324 unsigned ix;
1325
1326 gcc_assert (!dst->popcount);
1327
1328 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1329 {
1330 e = EDGE_PRED (b, ix);
1331 if (e->src== ENTRY_BLOCK_PTR)
1332 continue;
1333
1334 bitmap_copy (dst, src[e->src->index]);
1335 break;
1336 }
1337
1338 if (ix == EDGE_COUNT (b->preds))
1339 bitmap_clear (dst);
1340 else
1341 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
1342 {
1343 unsigned int i;
1344 SBITMAP_ELT_TYPE *p, *r;
1345
1346 e = EDGE_PRED (b, ix);
1347 if (e->src == ENTRY_BLOCK_PTR)
1348 continue;
1349
1350 p = src[e->src->index]->elms;
1351 r = dst->elms;
1352 for (i = 0; i < set_size; i++)
1353 *r++ |= *p++;
1354 }
1355 }