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