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