Merge branch 'vendor/GCC50' - gcc 5.0 snapshot 1 FEB 2015
[dragonfly.git] / contrib / gcc-5.0 / gcc / cfganal.c
1 /* Control flow graph analysis code for GNU compiler.
2    Copyright (C) 1987-2015 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 "predict.h"
26 #include "vec.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfganal.h"
37 #include "basic-block.h"
38 #include "bitmap.h"
39 #include "sbitmap.h"
40 #include "timevar.h"
41
42 /* Store the data structures necessary for depth-first search.  */
43 struct depth_first_search_dsS {
44   /* stack for backtracking during the algorithm */
45   basic_block *stack;
46
47   /* number of edges in the stack.  That is, positions 0, ..., sp-1
48      have edges.  */
49   unsigned int sp;
50
51   /* record of basic blocks already seen by depth-first search */
52   sbitmap visited_blocks;
53 };
54 typedef struct depth_first_search_dsS *depth_first_search_ds;
55
56 static void flow_dfs_compute_reverse_init (depth_first_search_ds);
57 static void flow_dfs_compute_reverse_add_bb (depth_first_search_ds,
58                                              basic_block);
59 static basic_block flow_dfs_compute_reverse_execute (depth_first_search_ds,
60                                                      basic_block);
61 static void flow_dfs_compute_reverse_finish (depth_first_search_ds);
62 \f
63 /* Mark the back edges in DFS traversal.
64    Return nonzero if a loop (natural or otherwise) is present.
65    Inspired by Depth_First_Search_PP described in:
66
67      Advanced Compiler Design and Implementation
68      Steven Muchnick
69      Morgan Kaufmann, 1997
70
71    and heavily borrowed from pre_and_rev_post_order_compute.  */
72
73 bool
74 mark_dfs_back_edges (void)
75 {
76   edge_iterator *stack;
77   int *pre;
78   int *post;
79   int sp;
80   int prenum = 1;
81   int postnum = 1;
82   sbitmap visited;
83   bool found = false;
84
85   /* Allocate the preorder and postorder number arrays.  */
86   pre = XCNEWVEC (int, last_basic_block_for_fn (cfun));
87   post = XCNEWVEC (int, last_basic_block_for_fn (cfun));
88
89   /* Allocate stack for back-tracking up CFG.  */
90   stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
91   sp = 0;
92
93   /* Allocate bitmap to track nodes that have been visited.  */
94   visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
95
96   /* None of the nodes in the CFG have been visited yet.  */
97   bitmap_clear (visited);
98
99   /* Push the first edge on to the stack.  */
100   stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
101
102   while (sp)
103     {
104       edge_iterator ei;
105       basic_block src;
106       basic_block dest;
107
108       /* Look at the edge on the top of the stack.  */
109       ei = stack[sp - 1];
110       src = ei_edge (ei)->src;
111       dest = ei_edge (ei)->dest;
112       ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
113
114       /* Check if the edge destination has been visited yet.  */
115       if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && ! bitmap_bit_p (visited,
116                                                                   dest->index))
117         {
118           /* Mark that we have visited the destination.  */
119           bitmap_set_bit (visited, dest->index);
120
121           pre[dest->index] = prenum++;
122           if (EDGE_COUNT (dest->succs) > 0)
123             {
124               /* Since the DEST node has been visited for the first
125                  time, check its successors.  */
126               stack[sp++] = ei_start (dest->succs);
127             }
128           else
129             post[dest->index] = postnum++;
130         }
131       else
132         {
133           if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
134               && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
135               && pre[src->index] >= pre[dest->index]
136               && post[dest->index] == 0)
137             ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
138
139           if (ei_one_before_end_p (ei)
140               && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
141             post[src->index] = postnum++;
142
143           if (!ei_one_before_end_p (ei))
144             ei_next (&stack[sp - 1]);
145           else
146             sp--;
147         }
148     }
149
150   free (pre);
151   free (post);
152   free (stack);
153   sbitmap_free (visited);
154
155   return found;
156 }
157
158 /* Find unreachable blocks.  An unreachable block will have 0 in
159    the reachable bit in block->flags.  A nonzero value indicates the
160    block is reachable.  */
161
162 void
163 find_unreachable_blocks (void)
164 {
165   edge e;
166   edge_iterator ei;
167   basic_block *tos, *worklist, bb;
168
169   tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
170
171   /* Clear all the reachability flags.  */
172
173   FOR_EACH_BB_FN (bb, cfun)
174     bb->flags &= ~BB_REACHABLE;
175
176   /* Add our starting points to the worklist.  Almost always there will
177      be only one.  It isn't inconceivable that we might one day directly
178      support Fortran alternate entry points.  */
179
180   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
181     {
182       *tos++ = e->dest;
183
184       /* Mark the block reachable.  */
185       e->dest->flags |= BB_REACHABLE;
186     }
187
188   /* Iterate: find everything reachable from what we've already seen.  */
189
190   while (tos != worklist)
191     {
192       basic_block b = *--tos;
193
194       FOR_EACH_EDGE (e, ei, b->succs)
195         {
196           basic_block dest = e->dest;
197
198           if (!(dest->flags & BB_REACHABLE))
199             {
200               *tos++ = dest;
201               dest->flags |= BB_REACHABLE;
202             }
203         }
204     }
205
206   free (worklist);
207 }
208 \f
209 /* Functions to access an edge list with a vector representation.
210    Enough data is kept such that given an index number, the
211    pred and succ that edge represents can be determined, or
212    given a pred and a succ, its index number can be returned.
213    This allows algorithms which consume a lot of memory to
214    represent the normally full matrix of edge (pred,succ) with a
215    single indexed vector,  edge (EDGE_INDEX (pred, succ)), with no
216    wasted space in the client code due to sparse flow graphs.  */
217
218 /* This functions initializes the edge list. Basically the entire
219    flowgraph is processed, and all edges are assigned a number,
220    and the data structure is filled in.  */
221
222 struct edge_list *
223 create_edge_list (void)
224 {
225   struct edge_list *elist;
226   edge e;
227   int num_edges;
228   basic_block bb;
229   edge_iterator ei;
230
231   /* Determine the number of edges in the flow graph by counting successor
232      edges on each basic block.  */
233   num_edges = 0;
234   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
235                   EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
236     {
237       num_edges += EDGE_COUNT (bb->succs);
238     }
239
240   elist = XNEW (struct edge_list);
241   elist->num_edges = num_edges;
242   elist->index_to_edge = XNEWVEC (edge, num_edges);
243
244   num_edges = 0;
245
246   /* Follow successors of blocks, and register these edges.  */
247   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
248                   EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
249     FOR_EACH_EDGE (e, ei, bb->succs)
250       elist->index_to_edge[num_edges++] = e;
251
252   return elist;
253 }
254
255 /* This function free's memory associated with an edge list.  */
256
257 void
258 free_edge_list (struct edge_list *elist)
259 {
260   if (elist)
261     {
262       free (elist->index_to_edge);
263       free (elist);
264     }
265 }
266
267 /* This function provides debug output showing an edge list.  */
268
269 DEBUG_FUNCTION void
270 print_edge_list (FILE *f, struct edge_list *elist)
271 {
272   int x;
273
274   fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
275            n_basic_blocks_for_fn (cfun), elist->num_edges);
276
277   for (x = 0; x < elist->num_edges; x++)
278     {
279       fprintf (f, " %-4d - edge(", x);
280       if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
281         fprintf (f, "entry,");
282       else
283         fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
284
285       if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR_FOR_FN (cfun))
286         fprintf (f, "exit)\n");
287       else
288         fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
289     }
290 }
291
292 /* This function provides an internal consistency check of an edge list,
293    verifying that all edges are present, and that there are no
294    extra edges.  */
295
296 DEBUG_FUNCTION void
297 verify_edge_list (FILE *f, struct edge_list *elist)
298 {
299   int pred, succ, index;
300   edge e;
301   basic_block bb, p, s;
302   edge_iterator ei;
303
304   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
305                   EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
306     {
307       FOR_EACH_EDGE (e, ei, bb->succs)
308         {
309           pred = e->src->index;
310           succ = e->dest->index;
311           index = EDGE_INDEX (elist, e->src, e->dest);
312           if (index == EDGE_INDEX_NO_EDGE)
313             {
314               fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
315               continue;
316             }
317
318           if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
319             fprintf (f, "*p* Pred for index %d should be %d not %d\n",
320                      index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
321           if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
322             fprintf (f, "*p* Succ for index %d should be %d not %d\n",
323                      index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
324         }
325     }
326
327   /* We've verified that all the edges are in the list, now lets make sure
328      there are no spurious edges in the list.  This is an expensive check!  */
329
330   FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR_FOR_FN (cfun),
331                   EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
332     FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
333       {
334         int found_edge = 0;
335
336         FOR_EACH_EDGE (e, ei, p->succs)
337           if (e->dest == s)
338             {
339               found_edge = 1;
340               break;
341             }
342
343         FOR_EACH_EDGE (e, ei, s->preds)
344           if (e->src == p)
345             {
346               found_edge = 1;
347               break;
348             }
349
350         if (EDGE_INDEX (elist, p, s)
351             == EDGE_INDEX_NO_EDGE && found_edge != 0)
352           fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
353                    p->index, s->index);
354         if (EDGE_INDEX (elist, p, s)
355             != EDGE_INDEX_NO_EDGE && found_edge == 0)
356           fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
357                    p->index, s->index, EDGE_INDEX (elist, p, s));
358       }
359 }
360
361
362 /* Functions to compute control dependences.  */
363
364 /* Indicate block BB is control dependent on an edge with index EDGE_INDEX.  */
365 void
366 control_dependences::set_control_dependence_map_bit (basic_block bb,
367                                                      int edge_index)
368 {
369   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
370     return;
371   gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
372   bitmap_set_bit (control_dependence_map[bb->index], edge_index);
373 }
374
375 /* Clear all control dependences for block BB.  */
376 void
377 control_dependences::clear_control_dependence_bitmap (basic_block bb)
378 {
379   bitmap_clear (control_dependence_map[bb->index]);
380 }
381
382 /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
383    This function is necessary because some blocks have negative numbers.  */
384
385 static inline basic_block
386 find_pdom (basic_block block)
387 {
388   gcc_assert (block != ENTRY_BLOCK_PTR_FOR_FN (cfun));
389
390   if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
391     return EXIT_BLOCK_PTR_FOR_FN (cfun);
392   else
393     {
394       basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
395       if (! bb)
396         return EXIT_BLOCK_PTR_FOR_FN (cfun);
397       return bb;
398     }
399 }
400
401 /* Determine all blocks' control dependences on the given edge with edge_list
402    EL index EDGE_INDEX, ala Morgan, Section 3.6.  */
403
404 void
405 control_dependences::find_control_dependence (int edge_index)
406 {
407   basic_block current_block;
408   basic_block ending_block;
409
410   gcc_assert (INDEX_EDGE_PRED_BB (m_el, edge_index)
411               != EXIT_BLOCK_PTR_FOR_FN (cfun));
412
413   if (INDEX_EDGE_PRED_BB (m_el, edge_index) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
414     ending_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
415   else
416     ending_block = find_pdom (INDEX_EDGE_PRED_BB (m_el, edge_index));
417
418   for (current_block = INDEX_EDGE_SUCC_BB (m_el, edge_index);
419        current_block != ending_block
420        && current_block != EXIT_BLOCK_PTR_FOR_FN (cfun);
421        current_block = find_pdom (current_block))
422     {
423       edge e = INDEX_EDGE (m_el, edge_index);
424
425       /* For abnormal edges, we don't make current_block control
426          dependent because instructions that throw are always necessary
427          anyway.  */
428       if (e->flags & EDGE_ABNORMAL)
429         continue;
430
431       set_control_dependence_map_bit (current_block, edge_index);
432     }
433 }
434
435 /* Record all blocks' control dependences on all edges in the edge
436    list EL, ala Morgan, Section 3.6.  */
437
438 control_dependences::control_dependences (struct edge_list *edges)
439   : m_el (edges)
440 {
441   timevar_push (TV_CONTROL_DEPENDENCES);
442   control_dependence_map.create (last_basic_block_for_fn (cfun));
443   for (int i = 0; i < last_basic_block_for_fn (cfun); ++i)
444     control_dependence_map.quick_push (BITMAP_ALLOC (NULL));
445   for (int i = 0; i < NUM_EDGES (m_el); ++i)
446     find_control_dependence (i);
447   timevar_pop (TV_CONTROL_DEPENDENCES);
448 }
449
450 /* Free control dependences and the associated edge list.  */
451
452 control_dependences::~control_dependences ()
453 {
454   for (unsigned i = 0; i < control_dependence_map.length (); ++i)
455     BITMAP_FREE (control_dependence_map[i]);
456   control_dependence_map.release ();
457   free_edge_list (m_el);
458 }
459
460 /* Returns the bitmap of edges the basic-block I is dependent on.  */
461
462 bitmap
463 control_dependences::get_edges_dependent_on (int i)
464 {
465   return control_dependence_map[i];
466 }
467
468 /* Returns the edge with index I from the edge list.  */
469
470 edge
471 control_dependences::get_edge (int i)
472 {
473   return INDEX_EDGE (m_el, i);
474 }
475
476
477 /* Given PRED and SUCC blocks, return the edge which connects the blocks.
478    If no such edge exists, return NULL.  */
479
480 edge
481 find_edge (basic_block pred, basic_block succ)
482 {
483   edge e;
484   edge_iterator ei;
485
486   if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
487     {
488       FOR_EACH_EDGE (e, ei, pred->succs)
489         if (e->dest == succ)
490           return e;
491     }
492   else
493     {
494       FOR_EACH_EDGE (e, ei, succ->preds)
495         if (e->src == pred)
496           return e;
497     }
498
499   return NULL;
500 }
501
502 /* This routine will determine what, if any, edge there is between
503    a specified predecessor and successor.  */
504
505 int
506 find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
507 {
508   int x;
509
510   for (x = 0; x < NUM_EDGES (edge_list); x++)
511     if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
512         && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
513       return x;
514
515   return (EDGE_INDEX_NO_EDGE);
516 }
517 \f
518 /* This routine will remove any fake predecessor edges for a basic block.
519    When the edge is removed, it is also removed from whatever successor
520    list it is in.  */
521
522 static void
523 remove_fake_predecessors (basic_block bb)
524 {
525   edge e;
526   edge_iterator ei;
527
528   for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
529     {
530       if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
531         remove_edge (e);
532       else
533         ei_next (&ei);
534     }
535 }
536
537 /* This routine will remove all fake edges from the flow graph.  If
538    we remove all fake successors, it will automatically remove all
539    fake predecessors.  */
540
541 void
542 remove_fake_edges (void)
543 {
544   basic_block bb;
545
546   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
547     remove_fake_predecessors (bb);
548 }
549
550 /* This routine will remove all fake edges to the EXIT_BLOCK.  */
551
552 void
553 remove_fake_exit_edges (void)
554 {
555   remove_fake_predecessors (EXIT_BLOCK_PTR_FOR_FN (cfun));
556 }
557
558
559 /* This function will add a fake edge between any block which has no
560    successors, and the exit block. Some data flow equations require these
561    edges to exist.  */
562
563 void
564 add_noreturn_fake_exit_edges (void)
565 {
566   basic_block bb;
567
568   FOR_EACH_BB_FN (bb, cfun)
569     if (EDGE_COUNT (bb->succs) == 0)
570       make_single_succ_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
571 }
572
573 /* This function adds a fake edge between any infinite loops to the
574    exit block.  Some optimizations require a path from each node to
575    the exit node.
576
577    See also Morgan, Figure 3.10, pp. 82-83.
578
579    The current implementation is ugly, not attempting to minimize the
580    number of inserted fake edges.  To reduce the number of fake edges
581    to insert, add fake edges from _innermost_ loops containing only
582    nodes not reachable from the exit block.  */
583
584 void
585 connect_infinite_loops_to_exit (void)
586 {
587   basic_block unvisited_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
588   basic_block deadend_block;
589   struct depth_first_search_dsS dfs_ds;
590
591   /* Perform depth-first search in the reverse graph to find nodes
592      reachable from the exit block.  */
593   flow_dfs_compute_reverse_init (&dfs_ds);
594   flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR_FOR_FN (cfun));
595
596   /* Repeatedly add fake edges, updating the unreachable nodes.  */
597   while (1)
598     {
599       unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds,
600                                                           unvisited_block);
601       if (!unvisited_block)
602         break;
603
604       deadend_block = dfs_find_deadend (unvisited_block);
605       make_edge (deadend_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
606       flow_dfs_compute_reverse_add_bb (&dfs_ds, deadend_block);
607     }
608
609   flow_dfs_compute_reverse_finish (&dfs_ds);
610   return;
611 }
612 \f
613 /* Compute reverse top sort order.  This is computing a post order
614    numbering of the graph.  If INCLUDE_ENTRY_EXIT is true, then
615    ENTRY_BLOCK and EXIT_BLOCK are included.  If DELETE_UNREACHABLE is
616    true, unreachable blocks are deleted.  */
617
618 int
619 post_order_compute (int *post_order, bool include_entry_exit,
620                     bool delete_unreachable)
621 {
622   edge_iterator *stack;
623   int sp;
624   int post_order_num = 0;
625   sbitmap visited;
626   int count;
627
628   if (include_entry_exit)
629     post_order[post_order_num++] = EXIT_BLOCK;
630
631   /* Allocate stack for back-tracking up CFG.  */
632   stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
633   sp = 0;
634
635   /* Allocate bitmap to track nodes that have been visited.  */
636   visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
637
638   /* None of the nodes in the CFG have been visited yet.  */
639   bitmap_clear (visited);
640
641   /* Push the first edge on to the stack.  */
642   stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
643
644   while (sp)
645     {
646       edge_iterator ei;
647       basic_block src;
648       basic_block dest;
649
650       /* Look at the edge on the top of the stack.  */
651       ei = stack[sp - 1];
652       src = ei_edge (ei)->src;
653       dest = ei_edge (ei)->dest;
654
655       /* Check if the edge destination has been visited yet.  */
656       if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
657           && ! bitmap_bit_p (visited, dest->index))
658         {
659           /* Mark that we have visited the destination.  */
660           bitmap_set_bit (visited, dest->index);
661
662           if (EDGE_COUNT (dest->succs) > 0)
663             /* Since the DEST node has been visited for the first
664                time, check its successors.  */
665             stack[sp++] = ei_start (dest->succs);
666           else
667             post_order[post_order_num++] = dest->index;
668         }
669       else
670         {
671           if (ei_one_before_end_p (ei)
672               && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
673             post_order[post_order_num++] = src->index;
674
675           if (!ei_one_before_end_p (ei))
676             ei_next (&stack[sp - 1]);
677           else
678             sp--;
679         }
680     }
681
682   if (include_entry_exit)
683     {
684       post_order[post_order_num++] = ENTRY_BLOCK;
685       count = post_order_num;
686     }
687   else
688     count = post_order_num + 2;
689
690   /* Delete the unreachable blocks if some were found and we are
691      supposed to do it.  */
692   if (delete_unreachable && (count != n_basic_blocks_for_fn (cfun)))
693     {
694       basic_block b;
695       basic_block next_bb;
696       for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
697            != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
698         {
699           next_bb = b->next_bb;
700
701           if (!(bitmap_bit_p (visited, b->index)))
702             delete_basic_block (b);
703         }
704
705       tidy_fallthru_edges ();
706     }
707
708   free (stack);
709   sbitmap_free (visited);
710   return post_order_num;
711 }
712
713
714 /* Helper routine for inverted_post_order_compute
715    flow_dfs_compute_reverse_execute, and the reverse-CFG
716    deapth first search in dominance.c.
717    BB has to belong to a region of CFG
718    unreachable by inverted traversal from the exit.
719    i.e. there's no control flow path from ENTRY to EXIT
720    that contains this BB.
721    This can happen in two cases - if there's an infinite loop
722    or if there's a block that has no successor
723    (call to a function with no return).
724    Some RTL passes deal with this condition by
725    calling connect_infinite_loops_to_exit () and/or
726    add_noreturn_fake_exit_edges ().
727    However, those methods involve modifying the CFG itself
728    which may not be desirable.
729    Hence, we deal with the infinite loop/no return cases
730    by identifying a unique basic block that can reach all blocks
731    in such a region by inverted traversal.
732    This function returns a basic block that guarantees
733    that all blocks in the region are reachable
734    by starting an inverted traversal from the returned block.  */
735
736 basic_block
737 dfs_find_deadend (basic_block bb)
738 {
739   bitmap visited = BITMAP_ALLOC (NULL);
740
741   for (;;)
742     {
743       if (EDGE_COUNT (bb->succs) == 0
744           || ! bitmap_set_bit (visited, bb->index))
745         {
746           BITMAP_FREE (visited);
747           return bb;
748         }
749
750       bb = EDGE_SUCC (bb, 0)->dest;
751     }
752
753   gcc_unreachable ();
754 }
755
756
757 /* Compute the reverse top sort order of the inverted CFG
758    i.e. starting from the exit block and following the edges backward
759    (from successors to predecessors).
760    This ordering can be used for forward dataflow problems among others.
761
762    This function assumes that all blocks in the CFG are reachable
763    from the ENTRY (but not necessarily from EXIT).
764
765    If there's an infinite loop,
766    a simple inverted traversal starting from the blocks
767    with no successors can't visit all blocks.
768    To solve this problem, we first do inverted traversal
769    starting from the blocks with no successor.
770    And if there's any block left that's not visited by the regular
771    inverted traversal from EXIT,
772    those blocks are in such problematic region.
773    Among those, we find one block that has
774    any visited predecessor (which is an entry into such a region),
775    and start looking for a "dead end" from that block
776    and do another inverted traversal from that block.  */
777
778 int
779 inverted_post_order_compute (int *post_order)
780 {
781   basic_block bb;
782   edge_iterator *stack;
783   int sp;
784   int post_order_num = 0;
785   sbitmap visited;
786
787   /* Allocate stack for back-tracking up CFG.  */
788   stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
789   sp = 0;
790
791   /* Allocate bitmap to track nodes that have been visited.  */
792   visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
793
794   /* None of the nodes in the CFG have been visited yet.  */
795   bitmap_clear (visited);
796
797   /* Put all blocks that have no successor into the initial work list.  */
798   FOR_ALL_BB_FN (bb, cfun)
799     if (EDGE_COUNT (bb->succs) == 0)
800       {
801         /* Push the initial edge on to the stack.  */
802         if (EDGE_COUNT (bb->preds) > 0)
803           {
804             stack[sp++] = ei_start (bb->preds);
805             bitmap_set_bit (visited, bb->index);
806           }
807       }
808
809   do
810     {
811       bool has_unvisited_bb = false;
812
813       /* The inverted traversal loop. */
814       while (sp)
815         {
816           edge_iterator ei;
817           basic_block pred;
818
819           /* Look at the edge on the top of the stack.  */
820           ei = stack[sp - 1];
821           bb = ei_edge (ei)->dest;
822           pred = ei_edge (ei)->src;
823
824           /* Check if the predecessor has been visited yet.  */
825           if (! bitmap_bit_p (visited, pred->index))
826             {
827               /* Mark that we have visited the destination.  */
828               bitmap_set_bit (visited, pred->index);
829
830               if (EDGE_COUNT (pred->preds) > 0)
831                 /* Since the predecessor node has been visited for the first
832                    time, check its predecessors.  */
833                 stack[sp++] = ei_start (pred->preds);
834               else
835                 post_order[post_order_num++] = pred->index;
836             }
837           else
838             {
839               if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
840                   && ei_one_before_end_p (ei))
841                 post_order[post_order_num++] = bb->index;
842
843               if (!ei_one_before_end_p (ei))
844                 ei_next (&stack[sp - 1]);
845               else
846                 sp--;
847             }
848         }
849
850       /* Detect any infinite loop and activate the kludge.
851          Note that this doesn't check EXIT_BLOCK itself
852          since EXIT_BLOCK is always added after the outer do-while loop.  */
853       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
854                       EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
855         if (!bitmap_bit_p (visited, bb->index))
856           {
857             has_unvisited_bb = true;
858
859             if (EDGE_COUNT (bb->preds) > 0)
860               {
861                 edge_iterator ei;
862                 edge e;
863                 basic_block visited_pred = NULL;
864
865                 /* Find an already visited predecessor.  */
866                 FOR_EACH_EDGE (e, ei, bb->preds)
867                   {
868                     if (bitmap_bit_p (visited, e->src->index))
869                       visited_pred = e->src;
870                   }
871
872                 if (visited_pred)
873                   {
874                     basic_block be = dfs_find_deadend (bb);
875                     gcc_assert (be != NULL);
876                     bitmap_set_bit (visited, be->index);
877                     stack[sp++] = ei_start (be->preds);
878                     break;
879                   }
880               }
881           }
882
883       if (has_unvisited_bb && sp == 0)
884         {
885           /* No blocks are reachable from EXIT at all.
886              Find a dead-end from the ENTRY, and restart the iteration. */
887           basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR_FOR_FN (cfun));
888           gcc_assert (be != NULL);
889           bitmap_set_bit (visited, be->index);
890           stack[sp++] = ei_start (be->preds);
891         }
892
893       /* The only case the below while fires is
894          when there's an infinite loop.  */
895     }
896   while (sp);
897
898   /* EXIT_BLOCK is always included.  */
899   post_order[post_order_num++] = EXIT_BLOCK;
900
901   free (stack);
902   sbitmap_free (visited);
903   return post_order_num;
904 }
905
906 /* Compute the depth first search order of FN and store in the array
907    PRE_ORDER if nonzero.  If REV_POST_ORDER is nonzero, return the
908    reverse completion number for each node.  Returns the number of nodes
909    visited.  A depth first search tries to get as far away from the starting
910    point as quickly as possible.
911
912    In case the function has unreachable blocks the number of nodes
913    visited does not include them.
914
915    pre_order is a really a preorder numbering of the graph.
916    rev_post_order is really a reverse postorder numbering of the graph.  */
917
918 int
919 pre_and_rev_post_order_compute_fn (struct function *fn,
920                                    int *pre_order, int *rev_post_order,
921                                    bool include_entry_exit)
922 {
923   edge_iterator *stack;
924   int sp;
925   int pre_order_num = 0;
926   int rev_post_order_num = n_basic_blocks_for_fn (cfun) - 1;
927   sbitmap visited;
928
929   /* Allocate stack for back-tracking up CFG.  */
930   stack = XNEWVEC (edge_iterator, n_basic_blocks_for_fn (cfun) + 1);
931   sp = 0;
932
933   if (include_entry_exit)
934     {
935       if (pre_order)
936         pre_order[pre_order_num] = ENTRY_BLOCK;
937       pre_order_num++;
938       if (rev_post_order)
939         rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
940     }
941   else
942     rev_post_order_num -= NUM_FIXED_BLOCKS;
943
944   /* Allocate bitmap to track nodes that have been visited.  */
945   visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
946
947   /* None of the nodes in the CFG have been visited yet.  */
948   bitmap_clear (visited);
949
950   /* Push the first edge on to the stack.  */
951   stack[sp++] = ei_start (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs);
952
953   while (sp)
954     {
955       edge_iterator ei;
956       basic_block src;
957       basic_block dest;
958
959       /* Look at the edge on the top of the stack.  */
960       ei = stack[sp - 1];
961       src = ei_edge (ei)->src;
962       dest = ei_edge (ei)->dest;
963
964       /* Check if the edge destination has been visited yet.  */
965       if (dest != EXIT_BLOCK_PTR_FOR_FN (fn)
966           && ! bitmap_bit_p (visited, dest->index))
967         {
968           /* Mark that we have visited the destination.  */
969           bitmap_set_bit (visited, dest->index);
970
971           if (pre_order)
972             pre_order[pre_order_num] = dest->index;
973
974           pre_order_num++;
975
976           if (EDGE_COUNT (dest->succs) > 0)
977             /* Since the DEST node has been visited for the first
978                time, check its successors.  */
979             stack[sp++] = ei_start (dest->succs);
980           else if (rev_post_order)
981             /* There are no successors for the DEST node so assign
982                its reverse completion number.  */
983             rev_post_order[rev_post_order_num--] = dest->index;
984         }
985       else
986         {
987           if (ei_one_before_end_p (ei)
988               && src != ENTRY_BLOCK_PTR_FOR_FN (fn)
989               && rev_post_order)
990             /* There are no more successors for the SRC node
991                so assign its reverse completion number.  */
992             rev_post_order[rev_post_order_num--] = src->index;
993
994           if (!ei_one_before_end_p (ei))
995             ei_next (&stack[sp - 1]);
996           else
997             sp--;
998         }
999     }
1000
1001   free (stack);
1002   sbitmap_free (visited);
1003
1004   if (include_entry_exit)
1005     {
1006       if (pre_order)
1007         pre_order[pre_order_num] = EXIT_BLOCK;
1008       pre_order_num++;
1009       if (rev_post_order)
1010         rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
1011     }
1012
1013   return pre_order_num;
1014 }
1015
1016 /* Like pre_and_rev_post_order_compute_fn but operating on the
1017    current function and asserting that all nodes were visited.  */
1018
1019 int
1020 pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
1021                                 bool include_entry_exit)
1022 {
1023   int pre_order_num
1024     = pre_and_rev_post_order_compute_fn (cfun, pre_order, rev_post_order,
1025                                          include_entry_exit);
1026   if (include_entry_exit)
1027     /* The number of nodes visited should be the number of blocks.  */
1028     gcc_assert (pre_order_num == n_basic_blocks_for_fn (cfun));
1029   else
1030     /* The number of nodes visited should be the number of blocks minus
1031        the entry and exit blocks which are not visited here.  */
1032     gcc_assert (pre_order_num
1033                 == (n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS));
1034
1035   return pre_order_num;
1036 }
1037
1038 /* Compute the depth first search order on the _reverse_ graph and
1039    store in the array DFS_ORDER, marking the nodes visited in VISITED.
1040    Returns the number of nodes visited.
1041
1042    The computation is split into three pieces:
1043
1044    flow_dfs_compute_reverse_init () creates the necessary data
1045    structures.
1046
1047    flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1048    structures.  The block will start the search.
1049
1050    flow_dfs_compute_reverse_execute () continues (or starts) the
1051    search using the block on the top of the stack, stopping when the
1052    stack is empty.
1053
1054    flow_dfs_compute_reverse_finish () destroys the necessary data
1055    structures.
1056
1057    Thus, the user will probably call ..._init(), call ..._add_bb() to
1058    add a beginning basic block to the stack, call ..._execute(),
1059    possibly add another bb to the stack and again call ..._execute(),
1060    ..., and finally call _finish().  */
1061
1062 /* Initialize the data structures used for depth-first search on the
1063    reverse graph.  If INITIALIZE_STACK is nonzero, the exit block is
1064    added to the basic block stack.  DATA is the current depth-first
1065    search context.  If INITIALIZE_STACK is nonzero, there is an
1066    element on the stack.  */
1067
1068 static void
1069 flow_dfs_compute_reverse_init (depth_first_search_ds data)
1070 {
1071   /* Allocate stack for back-tracking up CFG.  */
1072   data->stack = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1073   data->sp = 0;
1074
1075   /* Allocate bitmap to track nodes that have been visited.  */
1076   data->visited_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
1077
1078   /* None of the nodes in the CFG have been visited yet.  */
1079   bitmap_clear (data->visited_blocks);
1080
1081   return;
1082 }
1083
1084 /* Add the specified basic block to the top of the dfs data
1085    structures.  When the search continues, it will start at the
1086    block.  */
1087
1088 static void
1089 flow_dfs_compute_reverse_add_bb (depth_first_search_ds data, basic_block bb)
1090 {
1091   data->stack[data->sp++] = bb;
1092   bitmap_set_bit (data->visited_blocks, bb->index);
1093 }
1094
1095 /* Continue the depth-first search through the reverse graph starting with the
1096    block at the stack's top and ending when the stack is empty.  Visited nodes
1097    are marked.  Returns an unvisited basic block, or NULL if there is none
1098    available.  */
1099
1100 static basic_block
1101 flow_dfs_compute_reverse_execute (depth_first_search_ds data,
1102                                   basic_block last_unvisited)
1103 {
1104   basic_block bb;
1105   edge e;
1106   edge_iterator ei;
1107
1108   while (data->sp > 0)
1109     {
1110       bb = data->stack[--data->sp];
1111
1112       /* Perform depth-first search on adjacent vertices.  */
1113       FOR_EACH_EDGE (e, ei, bb->preds)
1114         if (!bitmap_bit_p (data->visited_blocks, e->src->index))
1115           flow_dfs_compute_reverse_add_bb (data, e->src);
1116     }
1117
1118   /* Determine if there are unvisited basic blocks.  */
1119   FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
1120     if (!bitmap_bit_p (data->visited_blocks, bb->index))
1121       return bb;
1122
1123   return NULL;
1124 }
1125
1126 /* Destroy the data structures needed for depth-first search on the
1127    reverse graph.  */
1128
1129 static void
1130 flow_dfs_compute_reverse_finish (depth_first_search_ds data)
1131 {
1132   free (data->stack);
1133   sbitmap_free (data->visited_blocks);
1134 }
1135
1136 /* Performs dfs search from BB over vertices satisfying PREDICATE;
1137    if REVERSE, go against direction of edges.  Returns number of blocks
1138    found and their list in RSLT.  RSLT can contain at most RSLT_MAX items.  */
1139 int
1140 dfs_enumerate_from (basic_block bb, int reverse,
1141                     bool (*predicate) (const_basic_block, const void *),
1142                     basic_block *rslt, int rslt_max, const void *data)
1143 {
1144   basic_block *st, lbb;
1145   int sp = 0, tv = 0;
1146   unsigned size;
1147
1148   /* A bitmap to keep track of visited blocks.  Allocating it each time
1149      this function is called is not possible, since dfs_enumerate_from
1150      is often used on small (almost) disjoint parts of cfg (bodies of
1151      loops), and allocating a large sbitmap would lead to quadratic
1152      behavior.  */
1153   static sbitmap visited;
1154   static unsigned v_size;
1155
1156 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1157 #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
1158 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1159
1160   /* Resize the VISITED sbitmap if necessary.  */
1161   size = last_basic_block_for_fn (cfun);
1162   if (size < 10)
1163     size = 10;
1164
1165   if (!visited)
1166     {
1167
1168       visited = sbitmap_alloc (size);
1169       bitmap_clear (visited);
1170       v_size = size;
1171     }
1172   else if (v_size < size)
1173     {
1174       /* Ensure that we increase the size of the sbitmap exponentially.  */
1175       if (2 * v_size > size)
1176         size = 2 * v_size;
1177
1178       visited = sbitmap_resize (visited, size, 0);
1179       v_size = size;
1180     }
1181
1182   st = XNEWVEC (basic_block, rslt_max);
1183   rslt[tv++] = st[sp++] = bb;
1184   MARK_VISITED (bb);
1185   while (sp)
1186     {
1187       edge e;
1188       edge_iterator ei;
1189       lbb = st[--sp];
1190       if (reverse)
1191         {
1192           FOR_EACH_EDGE (e, ei, lbb->preds)
1193             if (!VISITED_P (e->src) && predicate (e->src, data))
1194               {
1195                 gcc_assert (tv != rslt_max);
1196                 rslt[tv++] = st[sp++] = e->src;
1197                 MARK_VISITED (e->src);
1198               }
1199         }
1200       else
1201         {
1202           FOR_EACH_EDGE (e, ei, lbb->succs)
1203             if (!VISITED_P (e->dest) && predicate (e->dest, data))
1204               {
1205                 gcc_assert (tv != rslt_max);
1206                 rslt[tv++] = st[sp++] = e->dest;
1207                 MARK_VISITED (e->dest);
1208               }
1209         }
1210     }
1211   free (st);
1212   for (sp = 0; sp < tv; sp++)
1213     UNMARK_VISITED (rslt[sp]);
1214   return tv;
1215 #undef MARK_VISITED
1216 #undef UNMARK_VISITED
1217 #undef VISITED_P
1218 }
1219
1220
1221 /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1222
1223    This algorithm can be found in Timothy Harvey's PhD thesis, at
1224    http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1225    dominance algorithms.
1226
1227    First, we identify each join point, j (any node with more than one
1228    incoming edge is a join point).
1229
1230    We then examine each predecessor, p, of j and walk up the dominator tree
1231    starting at p.
1232
1233    We stop the walk when we reach j's immediate dominator - j is in the
1234    dominance frontier of each of  the nodes in the walk, except for j's
1235    immediate dominator. Intuitively, all of the rest of j's dominators are
1236    shared by j's predecessors as well.
1237    Since they dominate j, they will not have j in their dominance frontiers.
1238
1239    The number of nodes touched by this algorithm is equal to the size
1240    of the dominance frontiers, no more, no less.
1241 */
1242
1243
1244 static void
1245 compute_dominance_frontiers_1 (bitmap_head *frontiers)
1246 {
1247   edge p;
1248   edge_iterator ei;
1249   basic_block b;
1250   FOR_EACH_BB_FN (b, cfun)
1251     {
1252       if (EDGE_COUNT (b->preds) >= 2)
1253         {
1254           FOR_EACH_EDGE (p, ei, b->preds)
1255             {
1256               basic_block runner = p->src;
1257               basic_block domsb;
1258               if (runner == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1259                 continue;
1260
1261               domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1262               while (runner != domsb)
1263                 {
1264                   if (!bitmap_set_bit (&frontiers[runner->index],
1265                                        b->index))
1266                     break;
1267                   runner = get_immediate_dominator (CDI_DOMINATORS,
1268                                                     runner);
1269                 }
1270             }
1271         }
1272     }
1273 }
1274
1275
1276 void
1277 compute_dominance_frontiers (bitmap_head *frontiers)
1278 {
1279   timevar_push (TV_DOM_FRONTIERS);
1280
1281   compute_dominance_frontiers_1 (frontiers);
1282
1283   timevar_pop (TV_DOM_FRONTIERS);
1284 }
1285
1286 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1287    return a bitmap with all the blocks in the iterated dominance
1288    frontier of the blocks in DEF_BLOCKS.  DFS contains dominance
1289    frontier information as returned by compute_dominance_frontiers.
1290
1291    The resulting set of blocks are the potential sites where PHI nodes
1292    are needed.  The caller is responsible for freeing the memory
1293    allocated for the return value.  */
1294
1295 bitmap
1296 compute_idf (bitmap def_blocks, bitmap_head *dfs)
1297 {
1298   bitmap_iterator bi;
1299   unsigned bb_index, i;
1300   bitmap phi_insertion_points;
1301
1302   /* Each block can appear at most twice on the work-stack.  */
1303   auto_vec<int> work_stack (2 * n_basic_blocks_for_fn (cfun));
1304   phi_insertion_points = BITMAP_ALLOC (NULL);
1305
1306   /* Seed the work list with all the blocks in DEF_BLOCKS.  We use
1307      vec::quick_push here for speed.  This is safe because we know that
1308      the number of definition blocks is no greater than the number of
1309      basic blocks, which is the initial capacity of WORK_STACK.  */
1310   EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1311     work_stack.quick_push (bb_index);
1312
1313   /* Pop a block off the worklist, add every block that appears in
1314      the original block's DF that we have not already processed to
1315      the worklist.  Iterate until the worklist is empty.   Blocks
1316      which are added to the worklist are potential sites for
1317      PHI nodes.  */
1318   while (work_stack.length () > 0)
1319     {
1320       bb_index = work_stack.pop ();
1321
1322       /* Since the registration of NEW -> OLD name mappings is done
1323          separately from the call to update_ssa, when updating the SSA
1324          form, the basic blocks where new and/or old names are defined
1325          may have disappeared by CFG cleanup calls.  In this case,
1326          we may pull a non-existing block from the work stack.  */
1327       gcc_checking_assert (bb_index
1328                            < (unsigned) last_basic_block_for_fn (cfun));
1329
1330       EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
1331                                       0, i, bi)
1332         {
1333           work_stack.quick_push (i);
1334           bitmap_set_bit (phi_insertion_points, i);
1335         }
1336     }
1337
1338   return phi_insertion_points;
1339 }
1340
1341 /* Intersection and union of preds/succs for sbitmap based data flow
1342    solvers.  All four functions defined below take the same arguments:
1343    B is the basic block to perform the operation for.  DST is the
1344    target sbitmap, i.e. the result.  SRC is an sbitmap vector of size
1345    last_basic_block so that it can be indexed with basic block indices.
1346    DST may be (but does not have to be) SRC[B->index].  */
1347
1348 /* Set the bitmap DST to the intersection of SRC of successors of
1349    basic block B.  */
1350
1351 void
1352 bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1353 {
1354   unsigned int set_size = dst->size;
1355   edge e;
1356   unsigned ix;
1357
1358   gcc_assert (!dst->popcount);
1359
1360   for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1361     {
1362       e = EDGE_SUCC (b, ix);
1363       if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1364         continue;
1365
1366       bitmap_copy (dst, src[e->dest->index]);
1367       break;
1368     }
1369
1370   if (e == 0)
1371     bitmap_ones (dst);
1372   else
1373     for (++ix; ix < EDGE_COUNT (b->succs); ix++)
1374       {
1375         unsigned int i;
1376         SBITMAP_ELT_TYPE *p, *r;
1377
1378         e = EDGE_SUCC (b, ix);
1379         if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1380           continue;
1381
1382         p = src[e->dest->index]->elms;
1383         r = dst->elms;
1384         for (i = 0; i < set_size; i++)
1385           *r++ &= *p++;
1386       }
1387 }
1388
1389 /* Set the bitmap DST to the intersection of SRC of predecessors of
1390    basic block B.  */
1391
1392 void
1393 bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1394 {
1395   unsigned int set_size = dst->size;
1396   edge e;
1397   unsigned ix;
1398
1399   gcc_assert (!dst->popcount);
1400
1401   for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1402     {
1403       e = EDGE_PRED (b, ix);
1404       if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1405         continue;
1406
1407       bitmap_copy (dst, src[e->src->index]);
1408       break;
1409     }
1410
1411   if (e == 0)
1412     bitmap_ones (dst);
1413   else
1414     for (++ix; ix < EDGE_COUNT (b->preds); ix++)
1415       {
1416         unsigned int i;
1417         SBITMAP_ELT_TYPE *p, *r;
1418
1419         e = EDGE_PRED (b, ix);
1420         if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1421           continue;
1422
1423         p = src[e->src->index]->elms;
1424         r = dst->elms;
1425         for (i = 0; i < set_size; i++)
1426           *r++ &= *p++;
1427       }
1428 }
1429
1430 /* Set the bitmap DST to the union of SRC of successors of
1431    basic block B.  */
1432
1433 void
1434 bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1435 {
1436   unsigned int set_size = dst->size;
1437   edge e;
1438   unsigned ix;
1439
1440   gcc_assert (!dst->popcount);
1441
1442   for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1443     {
1444       e = EDGE_SUCC (b, ix);
1445       if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1446         continue;
1447
1448       bitmap_copy (dst, src[e->dest->index]);
1449       break;
1450     }
1451
1452   if (ix == EDGE_COUNT (b->succs))
1453     bitmap_clear (dst);
1454   else
1455     for (ix++; ix < EDGE_COUNT (b->succs); ix++)
1456       {
1457         unsigned int i;
1458         SBITMAP_ELT_TYPE *p, *r;
1459
1460         e = EDGE_SUCC (b, ix);
1461         if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1462           continue;
1463
1464         p = src[e->dest->index]->elms;
1465         r = dst->elms;
1466         for (i = 0; i < set_size; i++)
1467           *r++ |= *p++;
1468       }
1469 }
1470
1471 /* Set the bitmap DST to the union of SRC of predecessors of
1472    basic block B.  */
1473
1474 void
1475 bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1476 {
1477   unsigned int set_size = dst->size;
1478   edge e;
1479   unsigned ix;
1480
1481   gcc_assert (!dst->popcount);
1482
1483   for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1484     {
1485       e = EDGE_PRED (b, ix);
1486       if (e->src== ENTRY_BLOCK_PTR_FOR_FN (cfun))
1487         continue;
1488
1489       bitmap_copy (dst, src[e->src->index]);
1490       break;
1491     }
1492
1493   if (ix == EDGE_COUNT (b->preds))
1494     bitmap_clear (dst);
1495   else
1496     for (ix++; ix < EDGE_COUNT (b->preds); ix++)
1497       {
1498         unsigned int i;
1499         SBITMAP_ELT_TYPE *p, *r;
1500
1501         e = EDGE_PRED (b, ix);
1502         if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1503           continue;
1504
1505         p = src[e->src->index]->elms;
1506         r = dst->elms;
1507         for (i = 0; i < set_size; i++)
1508           *r++ |= *p++;
1509       }
1510 }
1511
1512 /* Returns the list of basic blocks in the function in an order that guarantees
1513    that if a block X has just a single predecessor Y, then Y is after X in the
1514    ordering.  */
1515
1516 basic_block *
1517 single_pred_before_succ_order (void)
1518 {
1519   basic_block x, y;
1520   basic_block *order = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1521   unsigned n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
1522   unsigned np, i;
1523   sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
1524
1525 #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1526 #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1527
1528   bitmap_clear (visited);
1529
1530   MARK_VISITED (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1531   FOR_EACH_BB_FN (x, cfun)
1532     {
1533       if (VISITED_P (x))
1534         continue;
1535
1536       /* Walk the predecessors of x as long as they have precisely one
1537          predecessor and add them to the list, so that they get stored
1538          after x.  */
1539       for (y = x, np = 1;
1540            single_pred_p (y) && !VISITED_P (single_pred (y));
1541            y = single_pred (y))
1542         np++;
1543       for (y = x, i = n - np;
1544            single_pred_p (y) && !VISITED_P (single_pred (y));
1545            y = single_pred (y), i++)
1546         {
1547           order[i] = y;
1548           MARK_VISITED (y);
1549         }
1550       order[i] = y;
1551       MARK_VISITED (y);
1552
1553       gcc_assert (i == n - 1);
1554       n -= np;
1555     }
1556
1557   sbitmap_free (visited);
1558   gcc_assert (n == 0);
1559   return order;
1560
1561 #undef MARK_VISITED
1562 #undef VISITED_P
1563 }