]>
Commit | Line | Data |
---|---|---|
bfd49939 | 1 | /* CFG cleanup for trees. |
3aea1f79 | 2 | Copyright (C) 2001-2014 Free Software Foundation, Inc. |
bfd49939 | 3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8c4c00c1 | 8 | the Free Software Foundation; either version 3, or (at your option) |
bfd49939 | 9 | any later version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License 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/>. */ | |
bfd49939 | 19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tm.h" | |
24 | #include "tree.h" | |
bfd49939 | 25 | #include "tm_p.h" |
bfd49939 | 26 | #include "basic-block.h" |
0b205f4c | 27 | #include "diagnostic-core.h" |
bfd49939 | 28 | #include "flags.h" |
29 | #include "function.h" | |
bfd49939 | 30 | #include "langhooks.h" |
bc61cadb | 31 | #include "tree-ssa-alias.h" |
32 | #include "internal-fn.h" | |
33 | #include "tree-eh.h" | |
34 | #include "gimple-expr.h" | |
35 | #include "is-a.h" | |
e795d6e1 | 36 | #include "gimple.h" |
a8783bee | 37 | #include "gimplify.h" |
dcf1a1ec | 38 | #include "gimple-iterator.h" |
073c1fd5 | 39 | #include "gimple-ssa.h" |
40 | #include "tree-cfg.h" | |
41 | #include "tree-phinodes.h" | |
42 | #include "ssa-iterators.h" | |
9ed99284 | 43 | #include "stringpool.h" |
073c1fd5 | 44 | #include "tree-ssanames.h" |
05d9c18a | 45 | #include "tree-ssa-loop-manip.h" |
9ed99284 | 46 | #include "expr.h" |
073c1fd5 | 47 | #include "tree-dfa.h" |
69ee5dbb | 48 | #include "tree-ssa.h" |
bfd49939 | 49 | #include "tree-pass.h" |
bfd49939 | 50 | #include "except.h" |
51 | #include "cfgloop.h" | |
bfd49939 | 52 | #include "hashtab.h" |
53 | #include "tree-ssa-propagate.h" | |
b30560de | 54 | #include "tree-scalar-evolution.h" |
bfd49939 | 55 | |
31a8456e | 56 | /* The set of blocks in that at least one of the following changes happened: |
57 | -- the statement at the end of the block was changed | |
58 | -- the block was newly created | |
59 | -- the set of the predecessors of the block changed | |
60 | -- the set of the successors of the block changed | |
61 | ??? Maybe we could track these changes separately, since they determine | |
62 | what cleanups it makes sense to try on the block. */ | |
63 | bitmap cfgcleanup_altered_bbs; | |
64 | ||
bfd49939 | 65 | /* Remove any fallthru edge from EV. Return true if an edge was removed. */ |
66 | ||
67 | static bool | |
f1f41a6c | 68 | remove_fallthru_edge (vec<edge, va_gc> *ev) |
bfd49939 | 69 | { |
70 | edge_iterator ei; | |
71 | edge e; | |
72 | ||
73 | FOR_EACH_EDGE (e, ei, ev) | |
74 | if ((e->flags & EDGE_FALLTHRU) != 0) | |
75 | { | |
e2a6b9da | 76 | if (e->flags & EDGE_COMPLEX) |
77 | e->flags &= ~EDGE_FALLTHRU; | |
78 | else | |
79 | remove_edge_and_dominated_blocks (e); | |
bfd49939 | 80 | return true; |
81 | } | |
82 | return false; | |
83 | } | |
84 | ||
75a70cf9 | 85 | |
bfd49939 | 86 | /* Disconnect an unreachable block in the control expression starting |
87 | at block BB. */ | |
88 | ||
89 | static bool | |
75a70cf9 | 90 | cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi) |
bfd49939 | 91 | { |
92 | edge taken_edge; | |
93 | bool retval = false; | |
75a70cf9 | 94 | gimple stmt = gsi_stmt (gsi); |
95 | tree val; | |
bfd49939 | 96 | |
97 | if (!single_succ_p (bb)) | |
98 | { | |
99 | edge e; | |
100 | edge_iterator ei; | |
add6ee5e | 101 | bool warned; |
db2cc26b | 102 | location_t loc; |
add6ee5e | 103 | |
104 | fold_defer_overflow_warnings (); | |
db2cc26b | 105 | loc = gimple_location (stmt); |
106 | switch (gimple_code (stmt)) | |
107 | { | |
108 | case GIMPLE_COND: | |
2ebfc881 | 109 | val = fold_binary_loc (loc, gimple_cond_code (stmt), |
110 | boolean_type_node, | |
111 | gimple_cond_lhs (stmt), | |
112 | gimple_cond_rhs (stmt)); | |
113 | break; | |
db2cc26b | 114 | |
115 | case GIMPLE_SWITCH: | |
116 | val = gimple_switch_index (stmt); | |
117 | break; | |
118 | ||
119 | default: | |
120 | val = NULL_TREE; | |
121 | } | |
bfd49939 | 122 | taken_edge = find_taken_edge (bb, val); |
123 | if (!taken_edge) | |
add6ee5e | 124 | { |
125 | fold_undefer_and_ignore_overflow_warnings (); | |
126 | return false; | |
127 | } | |
bfd49939 | 128 | |
129 | /* Remove all the edges except the one that is always executed. */ | |
add6ee5e | 130 | warned = false; |
bfd49939 | 131 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
132 | { | |
133 | if (e != taken_edge) | |
134 | { | |
add6ee5e | 135 | if (!warned) |
136 | { | |
137 | fold_undefer_overflow_warnings | |
75a70cf9 | 138 | (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL); |
add6ee5e | 139 | warned = true; |
140 | } | |
141 | ||
bfd49939 | 142 | taken_edge->probability += e->probability; |
143 | taken_edge->count += e->count; | |
31a8456e | 144 | remove_edge_and_dominated_blocks (e); |
bfd49939 | 145 | retval = true; |
146 | } | |
147 | else | |
148 | ei_next (&ei); | |
149 | } | |
add6ee5e | 150 | if (!warned) |
151 | fold_undefer_and_ignore_overflow_warnings (); | |
bfd49939 | 152 | if (taken_edge->probability > REG_BR_PROB_BASE) |
153 | taken_edge->probability = REG_BR_PROB_BASE; | |
154 | } | |
155 | else | |
156 | taken_edge = single_succ_edge (bb); | |
157 | ||
31a8456e | 158 | bitmap_set_bit (cfgcleanup_altered_bbs, bb->index); |
75a70cf9 | 159 | gsi_remove (&gsi, true); |
bfd49939 | 160 | taken_edge->flags = EDGE_FALLTHRU; |
161 | ||
bfd49939 | 162 | return retval; |
163 | } | |
164 | ||
31a8456e | 165 | /* Try to remove superfluous control structures in basic block BB. Returns |
166 | true if anything changes. */ | |
bfd49939 | 167 | |
168 | static bool | |
31a8456e | 169 | cleanup_control_flow_bb (basic_block bb) |
bfd49939 | 170 | { |
75a70cf9 | 171 | gimple_stmt_iterator gsi; |
bfd49939 | 172 | bool retval = false; |
75a70cf9 | 173 | gimple stmt; |
bfd49939 | 174 | |
31a8456e | 175 | /* If the last statement of the block could throw and now cannot, |
176 | we need to prune cfg. */ | |
75a70cf9 | 177 | retval |= gimple_purge_dead_eh_edges (bb); |
31a8456e | 178 | |
75a70cf9 | 179 | gsi = gsi_last_bb (bb); |
180 | if (gsi_end_p (gsi)) | |
31a8456e | 181 | return retval; |
182 | ||
75a70cf9 | 183 | stmt = gsi_stmt (gsi); |
31a8456e | 184 | |
75a70cf9 | 185 | if (gimple_code (stmt) == GIMPLE_COND |
186 | || gimple_code (stmt) == GIMPLE_SWITCH) | |
187 | retval |= cleanup_control_expr_graph (bb, gsi); | |
188 | else if (gimple_code (stmt) == GIMPLE_GOTO | |
189 | && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR | |
190 | && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0)) | |
31a8456e | 191 | == LABEL_DECL)) |
bfd49939 | 192 | { |
75a70cf9 | 193 | /* If we had a computed goto which has a compile-time determinable |
194 | destination, then we can eliminate the goto. */ | |
31a8456e | 195 | edge e; |
196 | tree label; | |
197 | edge_iterator ei; | |
198 | basic_block target_block; | |
bfd49939 | 199 | |
31a8456e | 200 | /* First look at all the outgoing edges. Delete any outgoing |
201 | edges which do not go to the right block. For the one | |
202 | edge which goes to the right block, fix up its flags. */ | |
75a70cf9 | 203 | label = TREE_OPERAND (gimple_goto_dest (stmt), 0); |
31a8456e | 204 | target_block = label_to_block (label); |
205 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
bfd49939 | 206 | { |
31a8456e | 207 | if (e->dest != target_block) |
208 | remove_edge_and_dominated_blocks (e); | |
209 | else | |
bfd49939 | 210 | { |
31a8456e | 211 | /* Turn off the EDGE_ABNORMAL flag. */ |
212 | e->flags &= ~EDGE_ABNORMAL; | |
bfd49939 | 213 | |
31a8456e | 214 | /* And set EDGE_FALLTHRU. */ |
215 | e->flags |= EDGE_FALLTHRU; | |
216 | ei_next (&ei); | |
217 | } | |
bfd49939 | 218 | } |
219 | ||
31a8456e | 220 | bitmap_set_bit (cfgcleanup_altered_bbs, bb->index); |
221 | bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index); | |
222 | ||
223 | /* Remove the GOTO_EXPR as it is not needed. The CFG has all the | |
224 | relevant information we need. */ | |
75a70cf9 | 225 | gsi_remove (&gsi, true); |
31a8456e | 226 | retval = true; |
bfd49939 | 227 | } |
31a8456e | 228 | |
229 | /* Check for indirect calls that have been turned into | |
230 | noreturn calls. */ | |
75a70cf9 | 231 | else if (is_gimple_call (stmt) |
232 | && gimple_call_noreturn_p (stmt) | |
233 | && remove_fallthru_edge (bb->succs)) | |
31a8456e | 234 | retval = true; |
235 | ||
bfd49939 | 236 | return retval; |
237 | } | |
238 | ||
239 | /* Return true if basic block BB does nothing except pass control | |
240 | flow to another block and that we can safely insert a label at | |
241 | the start of the successor block. | |
242 | ||
243 | As a precondition, we require that BB be not equal to | |
efee62d1 | 244 | the entry block. */ |
bfd49939 | 245 | |
246 | static bool | |
247 | tree_forwarder_block_p (basic_block bb, bool phi_wanted) | |
248 | { | |
75a70cf9 | 249 | gimple_stmt_iterator gsi; |
65138c0f | 250 | location_t locus; |
bfd49939 | 251 | |
252 | /* BB must have a single outgoing edge. */ | |
253 | if (single_succ_p (bb) != 1 | |
254 | /* If PHI_WANTED is false, BB must not have any PHI nodes. | |
255 | Otherwise, BB must have PHI nodes. */ | |
75a70cf9 | 256 | || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted |
efee62d1 | 257 | /* BB may not be a predecessor of the exit block. */ |
34154e27 | 258 | || single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun) |
bfd49939 | 259 | /* Nor should this be an infinite loop. */ |
260 | || single_succ (bb) == bb | |
261 | /* BB may not have an abnormal outgoing edge. */ | |
262 | || (single_succ_edge (bb)->flags & EDGE_ABNORMAL)) | |
263 | return false; | |
264 | ||
34154e27 | 265 | gcc_checking_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
bfd49939 | 266 | |
65138c0f | 267 | locus = single_succ_edge (bb)->goto_locus; |
268 | ||
e38def9c | 269 | /* There should not be an edge coming from entry, or an EH edge. */ |
270 | { | |
271 | edge_iterator ei; | |
272 | edge e; | |
273 | ||
274 | FOR_EACH_EDGE (e, ei, bb->preds) | |
34154e27 | 275 | if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || (e->flags & EDGE_EH)) |
e38def9c | 276 | return false; |
65138c0f | 277 | /* If goto_locus of any of the edges differs, prevent removing |
278 | the forwarder block for -O0. */ | |
279 | else if (optimize == 0 && e->goto_locus != locus) | |
280 | return false; | |
e38def9c | 281 | } |
282 | ||
bfd49939 | 283 | /* Now walk through the statements backward. We can ignore labels, |
284 | anything else means this is not a forwarder block. */ | |
75a70cf9 | 285 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi)) |
bfd49939 | 286 | { |
75a70cf9 | 287 | gimple stmt = gsi_stmt (gsi); |
bfd49939 | 288 | |
75a70cf9 | 289 | switch (gimple_code (stmt)) |
bfd49939 | 290 | { |
75a70cf9 | 291 | case GIMPLE_LABEL: |
292 | if (DECL_NONLOCAL (gimple_label_label (stmt))) | |
bfd49939 | 293 | return false; |
65138c0f | 294 | if (optimize == 0 && gimple_location (stmt) != locus) |
295 | return false; | |
bfd49939 | 296 | break; |
297 | ||
9845d120 | 298 | /* ??? For now, hope there's a corresponding debug |
299 | assignment at the destination. */ | |
300 | case GIMPLE_DEBUG: | |
301 | break; | |
302 | ||
bfd49939 | 303 | default: |
304 | return false; | |
305 | } | |
306 | } | |
307 | ||
bfd49939 | 308 | if (current_loops) |
309 | { | |
310 | basic_block dest; | |
3c2e3ed6 | 311 | /* Protect loop headers. */ |
bfd49939 | 312 | if (bb->loop_father->header == bb) |
313 | return false; | |
bfd49939 | 314 | |
3c2e3ed6 | 315 | dest = EDGE_SUCC (bb, 0)->dest; |
316 | /* Protect loop preheaders and latches if requested. */ | |
bfd49939 | 317 | if (dest->loop_father->header == dest) |
3c2e3ed6 | 318 | { |
0ab3f603 | 319 | if (bb->loop_father == dest->loop_father) |
320 | { | |
321 | if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES)) | |
322 | return false; | |
323 | /* If bb doesn't have a single predecessor we'd make this | |
324 | loop have multiple latches. Don't do that if that | |
325 | would in turn require disambiguating them. */ | |
326 | return (single_pred_p (bb) | |
327 | || loops_state_satisfies_p | |
328 | (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)); | |
329 | } | |
330 | else if (bb->loop_father == loop_outer (dest->loop_father)) | |
331 | return !loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS); | |
332 | /* Always preserve other edges into loop headers that are | |
333 | not simple latches or preheaders. */ | |
334 | return false; | |
3c2e3ed6 | 335 | } |
bfd49939 | 336 | } |
3c2e3ed6 | 337 | |
bfd49939 | 338 | return true; |
339 | } | |
340 | ||
bfd49939 | 341 | /* If all the PHI nodes in DEST have alternatives for E1 and E2 and |
342 | those alternatives are equal in each of the PHI nodes, then return | |
343 | true, else return false. */ | |
344 | ||
345 | static bool | |
346 | phi_alternatives_equal (basic_block dest, edge e1, edge e2) | |
347 | { | |
348 | int n1 = e1->dest_idx; | |
349 | int n2 = e2->dest_idx; | |
75a70cf9 | 350 | gimple_stmt_iterator gsi; |
bfd49939 | 351 | |
75a70cf9 | 352 | for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi)) |
bfd49939 | 353 | { |
75a70cf9 | 354 | gimple phi = gsi_stmt (gsi); |
355 | tree val1 = gimple_phi_arg_def (phi, n1); | |
356 | tree val2 = gimple_phi_arg_def (phi, n2); | |
bfd49939 | 357 | |
358 | gcc_assert (val1 != NULL_TREE); | |
359 | gcc_assert (val2 != NULL_TREE); | |
360 | ||
361 | if (!operand_equal_for_phi_arg_p (val1, val2)) | |
362 | return false; | |
363 | } | |
364 | ||
365 | return true; | |
366 | } | |
367 | ||
31a8456e | 368 | /* Removes forwarder block BB. Returns false if this failed. */ |
bfd49939 | 369 | |
370 | static bool | |
31a8456e | 371 | remove_forwarder_block (basic_block bb) |
bfd49939 | 372 | { |
373 | edge succ = single_succ_edge (bb), e, s; | |
374 | basic_block dest = succ->dest; | |
75a70cf9 | 375 | gimple label; |
bfd49939 | 376 | edge_iterator ei; |
75a70cf9 | 377 | gimple_stmt_iterator gsi, gsi_to; |
f24c0e3a | 378 | bool can_move_debug_stmts; |
bfd49939 | 379 | |
380 | /* We check for infinite loops already in tree_forwarder_block_p. | |
381 | However it may happen that the infinite loop is created | |
382 | afterwards due to removal of forwarders. */ | |
383 | if (dest == bb) | |
384 | return false; | |
385 | ||
11d28f28 | 386 | /* If the destination block consists of a nonlocal label or is a |
387 | EH landing pad, do not merge it. */ | |
bfd49939 | 388 | label = first_stmt (dest); |
389 | if (label | |
75a70cf9 | 390 | && gimple_code (label) == GIMPLE_LABEL |
11d28f28 | 391 | && (DECL_NONLOCAL (gimple_label_label (label)) |
392 | || EH_LANDING_PAD_NR (gimple_label_label (label)) != 0)) | |
bfd49939 | 393 | return false; |
394 | ||
395 | /* If there is an abnormal edge to basic block BB, but not into | |
396 | dest, problems might occur during removal of the phi node at out | |
397 | of ssa due to overlapping live ranges of registers. | |
398 | ||
399 | If there is an abnormal edge in DEST, the problems would occur | |
400 | anyway since cleanup_dead_labels would then merge the labels for | |
401 | two different eh regions, and rest of exception handling code | |
402 | does not like it. | |
403 | ||
404 | So if there is an abnormal edge to BB, proceed only if there is | |
405 | no abnormal edge to DEST and there are no phi nodes in DEST. */ | |
f1047120 | 406 | if (bb_has_abnormal_pred (bb) |
407 | && (bb_has_abnormal_pred (dest) | |
7b960d37 | 408 | || !gimple_seq_empty_p (phi_nodes (dest)))) |
409 | return false; | |
bfd49939 | 410 | |
411 | /* If there are phi nodes in DEST, and some of the blocks that are | |
412 | predecessors of BB are also predecessors of DEST, check that the | |
413 | phi node arguments match. */ | |
75a70cf9 | 414 | if (!gimple_seq_empty_p (phi_nodes (dest))) |
bfd49939 | 415 | { |
416 | FOR_EACH_EDGE (e, ei, bb->preds) | |
417 | { | |
418 | s = find_edge (e->src, dest); | |
419 | if (!s) | |
420 | continue; | |
421 | ||
422 | if (!phi_alternatives_equal (dest, succ, s)) | |
423 | return false; | |
424 | } | |
425 | } | |
426 | ||
6d31b223 | 427 | can_move_debug_stmts = MAY_HAVE_DEBUG_STMTS && single_pred_p (dest); |
f24c0e3a | 428 | |
0ab3f603 | 429 | basic_block pred = NULL; |
430 | if (single_pred_p (bb)) | |
431 | pred = single_pred (bb); | |
432 | ||
bfd49939 | 433 | /* Redirect the edges. */ |
434 | for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); ) | |
435 | { | |
31a8456e | 436 | bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index); |
437 | ||
bfd49939 | 438 | if (e->flags & EDGE_ABNORMAL) |
439 | { | |
440 | /* If there is an abnormal edge, redirect it anyway, and | |
441 | move the labels to the new block to make it legal. */ | |
442 | s = redirect_edge_succ_nodup (e, dest); | |
443 | } | |
444 | else | |
445 | s = redirect_edge_and_branch (e, dest); | |
446 | ||
447 | if (s == e) | |
448 | { | |
449 | /* Create arguments for the phi nodes, since the edge was not | |
450 | here before. */ | |
75a70cf9 | 451 | for (gsi = gsi_start_phis (dest); |
452 | !gsi_end_p (gsi); | |
453 | gsi_next (&gsi)) | |
454 | { | |
455 | gimple phi = gsi_stmt (gsi); | |
efbcb6de | 456 | source_location l = gimple_phi_arg_location_from_edge (phi, succ); |
11db727d | 457 | tree def = gimple_phi_arg_def (phi, succ->dest_idx); |
458 | add_phi_arg (phi, unshare_expr (def), s, l); | |
75a70cf9 | 459 | } |
bfd49939 | 460 | } |
bfd49939 | 461 | } |
462 | ||
7b960d37 | 463 | /* Move nonlocal labels and computed goto targets as well as user |
464 | defined labels and labels with an EH landing pad number to the | |
465 | new block, so that the redirection of the abnormal edges works, | |
466 | jump targets end up in a sane place and debug information for | |
467 | labels is retained. */ | |
468 | gsi_to = gsi_start_bb (dest); | |
469 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); ) | |
470 | { | |
471 | tree decl; | |
472 | label = gsi_stmt (gsi); | |
473 | if (is_gimple_debug (label)) | |
474 | break; | |
475 | decl = gimple_label_label (label); | |
476 | if (EH_LANDING_PAD_NR (decl) != 0 | |
477 | || DECL_NONLOCAL (decl) | |
478 | || FORCED_LABEL (decl) | |
479 | || !DECL_ARTIFICIAL (decl)) | |
480 | { | |
481 | gsi_remove (&gsi, false); | |
482 | gsi_insert_before (&gsi_to, label, GSI_SAME_STMT); | |
483 | } | |
484 | else | |
485 | gsi_next (&gsi); | |
486 | } | |
487 | ||
6d31b223 | 488 | /* Move debug statements if the destination has a single predecessor. */ |
f24c0e3a | 489 | if (can_move_debug_stmts) |
bfd49939 | 490 | { |
7b960d37 | 491 | gsi_to = gsi_after_labels (dest); |
492 | for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); ) | |
bfd49939 | 493 | { |
f24c0e3a | 494 | gimple debug = gsi_stmt (gsi); |
495 | if (!is_gimple_debug (debug)) | |
7b960d37 | 496 | break; |
75a70cf9 | 497 | gsi_remove (&gsi, false); |
f24c0e3a | 498 | gsi_insert_before (&gsi_to, debug, GSI_SAME_STMT); |
bfd49939 | 499 | } |
500 | } | |
501 | ||
31a8456e | 502 | bitmap_set_bit (cfgcleanup_altered_bbs, dest->index); |
503 | ||
bfd49939 | 504 | /* Update the dominators. */ |
505 | if (dom_info_available_p (CDI_DOMINATORS)) | |
506 | { | |
507 | basic_block dom, dombb, domdest; | |
508 | ||
509 | dombb = get_immediate_dominator (CDI_DOMINATORS, bb); | |
510 | domdest = get_immediate_dominator (CDI_DOMINATORS, dest); | |
511 | if (domdest == bb) | |
512 | { | |
513 | /* Shortcut to avoid calling (relatively expensive) | |
514 | nearest_common_dominator unless necessary. */ | |
515 | dom = dombb; | |
516 | } | |
517 | else | |
518 | dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb); | |
519 | ||
520 | set_immediate_dominator (CDI_DOMINATORS, dest, dom); | |
521 | } | |
522 | ||
3c2e3ed6 | 523 | /* Adjust latch infomation of BB's parent loop as otherwise |
524 | the cfg hook has a hard time not to kill the loop. */ | |
525 | if (current_loops && bb->loop_father->latch == bb) | |
0ab3f603 | 526 | bb->loop_father->latch = pred; |
3c2e3ed6 | 527 | |
bfd49939 | 528 | /* And kill the forwarder block. */ |
529 | delete_basic_block (bb); | |
530 | ||
531 | return true; | |
532 | } | |
533 | ||
3b4fe440 | 534 | /* STMT is a call that has been discovered noreturn. Fixup the CFG |
535 | and remove LHS. Return true if something changed. */ | |
536 | ||
537 | bool | |
538 | fixup_noreturn_call (gimple stmt) | |
539 | { | |
540 | basic_block bb = gimple_bb (stmt); | |
541 | bool changed = false; | |
542 | ||
543 | if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN)) | |
544 | return false; | |
545 | ||
546 | /* First split basic block if stmt is not last. */ | |
547 | if (stmt != gsi_stmt (gsi_last_bb (bb))) | |
548 | split_block (bb, stmt); | |
549 | ||
550 | changed |= remove_fallthru_edge (bb->succs); | |
551 | ||
552 | /* If there is LHS, remove it. */ | |
553 | if (gimple_call_lhs (stmt)) | |
554 | { | |
555 | tree op = gimple_call_lhs (stmt); | |
556 | gimple_call_set_lhs (stmt, NULL_TREE); | |
b703d146 | 557 | |
c2023864 | 558 | /* We need to remove SSA name to avoid checking errors. |
3b4fe440 | 559 | All uses are dominated by the noreturn and thus will |
b703d146 | 560 | be removed afterwards. |
561 | We proactively remove affected non-PHI statements to avoid | |
562 | fixup_cfg from trying to update them and crashing. */ | |
3b4fe440 | 563 | if (TREE_CODE (op) == SSA_NAME) |
564 | { | |
565 | use_operand_p use_p; | |
566 | imm_use_iterator iter; | |
567 | gimple use_stmt; | |
b703d146 | 568 | bitmap_iterator bi; |
569 | unsigned int bb_index; | |
570 | ||
571 | bitmap blocks = BITMAP_ALLOC (NULL); | |
3b4fe440 | 572 | |
573 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, op) | |
b703d146 | 574 | { |
575 | if (gimple_code (use_stmt) != GIMPLE_PHI) | |
576 | bitmap_set_bit (blocks, gimple_bb (use_stmt)->index); | |
577 | else | |
578 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
579 | SET_USE (use_p, error_mark_node); | |
580 | } | |
581 | EXECUTE_IF_SET_IN_BITMAP (blocks, 0, bb_index, bi) | |
f5a6b05f | 582 | delete_basic_block (BASIC_BLOCK_FOR_FN (cfun, bb_index)); |
b703d146 | 583 | BITMAP_FREE (blocks); |
584 | release_ssa_name (op); | |
3b4fe440 | 585 | } |
586 | update_stmt (stmt); | |
587 | changed = true; | |
588 | } | |
589 | return changed; | |
590 | } | |
591 | ||
592 | ||
31a8456e | 593 | /* Split basic blocks on calls in the middle of a basic block that are now |
594 | known not to return, and remove the unreachable code. */ | |
bfd49939 | 595 | |
596 | static bool | |
31a8456e | 597 | split_bbs_on_noreturn_calls (void) |
bfd49939 | 598 | { |
bfd49939 | 599 | bool changed = false; |
75a70cf9 | 600 | gimple stmt; |
31a8456e | 601 | basic_block bb; |
bfd49939 | 602 | |
31a8456e | 603 | /* Detect cases where a mid-block call is now known not to return. */ |
604 | if (cfun->gimple_df) | |
f1f41a6c | 605 | while (vec_safe_length (MODIFIED_NORETURN_CALLS (cfun))) |
31a8456e | 606 | { |
f1f41a6c | 607 | stmt = MODIFIED_NORETURN_CALLS (cfun)->pop (); |
75a70cf9 | 608 | bb = gimple_bb (stmt); |
7c707785 | 609 | /* BB might be deleted at this point, so verify first |
610 | BB is present in the cfg. */ | |
31a8456e | 611 | if (bb == NULL |
7c707785 | 612 | || bb->index < NUM_FIXED_BLOCKS |
fe672ac0 | 613 | || bb->index >= last_basic_block_for_fn (cfun) |
f5a6b05f | 614 | || BASIC_BLOCK_FOR_FN (cfun, bb->index) != bb |
75a70cf9 | 615 | || !gimple_call_noreturn_p (stmt)) |
31a8456e | 616 | continue; |
617 | ||
3b4fe440 | 618 | changed |= fixup_noreturn_call (stmt); |
31a8456e | 619 | } |
bfd49939 | 620 | |
bfd49939 | 621 | return changed; |
622 | } | |
623 | ||
31a8456e | 624 | /* Tries to cleanup cfg in basic block BB. Returns true if anything |
625 | changes. */ | |
bfd49939 | 626 | |
0c6b4703 | 627 | static bool |
31a8456e | 628 | cleanup_tree_cfg_bb (basic_block bb) |
bfd49939 | 629 | { |
d244d9de | 630 | bool retval = cleanup_control_flow_bb (bb); |
48e1416a | 631 | |
65138c0f | 632 | if (tree_forwarder_block_p (bb, false) |
31a8456e | 633 | && remove_forwarder_block (bb)) |
634 | return true; | |
bfd49939 | 635 | |
0c6b4703 | 636 | /* Merging the blocks may create new opportunities for folding |
637 | conditional branches (due to the elimination of single-valued PHI | |
638 | nodes). */ | |
31a8456e | 639 | if (single_succ_p (bb) |
640 | && can_merge_blocks_p (bb, single_succ (bb))) | |
641 | { | |
642 | merge_blocks (bb, single_succ (bb)); | |
643 | return true; | |
644 | } | |
645 | ||
646 | return retval; | |
647 | } | |
648 | ||
649 | /* Iterate the cfg cleanups, while anything changes. */ | |
650 | ||
651 | static bool | |
652 | cleanup_tree_cfg_1 (void) | |
653 | { | |
654 | bool retval = false; | |
655 | basic_block bb; | |
656 | unsigned i, n; | |
657 | ||
658 | retval |= split_bbs_on_noreturn_calls (); | |
659 | ||
660 | /* Prepare the worklists of altered blocks. */ | |
661 | cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL); | |
662 | ||
663 | /* During forwarder block cleanup, we may redirect edges out of | |
664 | SWITCH_EXPRs, which can get expensive. So we want to enable | |
665 | recording of edge to CASE_LABEL_EXPR. */ | |
666 | start_recording_case_labels (); | |
0c6b4703 | 667 | |
fc00614f | 668 | /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB_FN, |
31a8456e | 669 | since the basic blocks may get removed. */ |
fe672ac0 | 670 | n = last_basic_block_for_fn (cfun); |
31a8456e | 671 | for (i = NUM_FIXED_BLOCKS; i < n; i++) |
672 | { | |
f5a6b05f | 673 | bb = BASIC_BLOCK_FOR_FN (cfun, i); |
31a8456e | 674 | if (bb) |
675 | retval |= cleanup_tree_cfg_bb (bb); | |
676 | } | |
677 | ||
678 | /* Now process the altered blocks, as long as any are available. */ | |
679 | while (!bitmap_empty_p (cfgcleanup_altered_bbs)) | |
680 | { | |
681 | i = bitmap_first_set_bit (cfgcleanup_altered_bbs); | |
682 | bitmap_clear_bit (cfgcleanup_altered_bbs, i); | |
683 | if (i < NUM_FIXED_BLOCKS) | |
684 | continue; | |
685 | ||
f5a6b05f | 686 | bb = BASIC_BLOCK_FOR_FN (cfun, i); |
31a8456e | 687 | if (!bb) |
688 | continue; | |
689 | ||
690 | retval |= cleanup_tree_cfg_bb (bb); | |
691 | ||
692 | /* Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn | |
693 | calls. */ | |
694 | retval |= split_bbs_on_noreturn_calls (); | |
695 | } | |
48e1416a | 696 | |
31a8456e | 697 | end_recording_case_labels (); |
698 | BITMAP_FREE (cfgcleanup_altered_bbs); | |
0c6b4703 | 699 | return retval; |
700 | } | |
701 | ||
702 | ||
9ff23274 | 703 | /* Remove unreachable blocks and other miscellaneous clean up work. |
704 | Return true if the flowgraph was modified, false otherwise. */ | |
0c6b4703 | 705 | |
eb2a640e | 706 | static bool |
707 | cleanup_tree_cfg_noloop (void) | |
0c6b4703 | 708 | { |
31a8456e | 709 | bool changed; |
0c6b4703 | 710 | |
711 | timevar_push (TV_TREE_CLEANUP_CFG); | |
712 | ||
9ff23274 | 713 | /* Iterate until there are no more cleanups left to do. If any |
31a8456e | 714 | iteration changed the flowgraph, set CHANGED to true. |
715 | ||
716 | If dominance information is available, there cannot be any unreachable | |
717 | blocks. */ | |
50b08d37 | 718 | if (!dom_info_available_p (CDI_DOMINATORS)) |
9ff23274 | 719 | { |
31a8456e | 720 | changed = delete_unreachable_blocks (); |
721 | calculate_dominance_info (CDI_DOMINATORS); | |
9ff23274 | 722 | } |
31a8456e | 723 | else |
72e1a42b | 724 | { |
725 | #ifdef ENABLE_CHECKING | |
726 | verify_dominators (CDI_DOMINATORS); | |
727 | #endif | |
728 | changed = false; | |
729 | } | |
bfd49939 | 730 | |
31a8456e | 731 | changed |= cleanup_tree_cfg_1 (); |
732 | ||
50b08d37 | 733 | gcc_assert (dom_info_available_p (CDI_DOMINATORS)); |
bfd49939 | 734 | compact_blocks (); |
735 | ||
736 | #ifdef ENABLE_CHECKING | |
737 | verify_flow_info (); | |
738 | #endif | |
0c6b4703 | 739 | |
bfd49939 | 740 | timevar_pop (TV_TREE_CLEANUP_CFG); |
0c6b4703 | 741 | |
eb2a640e | 742 | if (changed && current_loops) |
f24ec26f | 743 | loops_state_set (LOOPS_NEED_FIXUP); |
eb2a640e | 744 | |
9ff23274 | 745 | return changed; |
bfd49939 | 746 | } |
747 | ||
eb2a640e | 748 | /* Repairs loop structures. */ |
bfd49939 | 749 | |
eb2a640e | 750 | static void |
751 | repair_loop_structures (void) | |
bfd49939 | 752 | { |
4b366dd3 | 753 | bitmap changed_bbs; |
b6f3c6f1 | 754 | unsigned n_new_loops; |
4b366dd3 | 755 | |
4e976818 | 756 | calculate_dominance_info (CDI_DOMINATORS); |
757 | ||
4b366dd3 | 758 | timevar_push (TV_REPAIR_LOOPS); |
759 | changed_bbs = BITMAP_ALLOC (NULL); | |
b6f3c6f1 | 760 | n_new_loops = fix_loop_structure (changed_bbs); |
bfd49939 | 761 | |
eb2a640e | 762 | /* This usually does nothing. But sometimes parts of cfg that originally |
763 | were inside a loop get out of it due to edge removal (since they | |
b6f3c6f1 | 764 | become unreachable by back edges from latch). Also a former |
765 | irreducible loop can become reducible - in this case force a full | |
766 | rewrite into loop-closed SSA form. */ | |
f24ec26f | 767 | if (loops_state_satisfies_p (LOOP_CLOSED_SSA)) |
b6f3c6f1 | 768 | rewrite_into_loop_closed_ssa (n_new_loops ? NULL : changed_bbs, |
769 | TODO_update_ssa); | |
bfd49939 | 770 | |
eb2a640e | 771 | BITMAP_FREE (changed_bbs); |
bfd49939 | 772 | |
773 | #ifdef ENABLE_CHECKING | |
eb2a640e | 774 | verify_loop_structure (); |
bfd49939 | 775 | #endif |
eb2a640e | 776 | scev_reset (); |
777 | ||
4b366dd3 | 778 | timevar_pop (TV_REPAIR_LOOPS); |
eb2a640e | 779 | } |
780 | ||
781 | /* Cleanup cfg and repair loop structures. */ | |
782 | ||
783 | bool | |
784 | cleanup_tree_cfg (void) | |
785 | { | |
786 | bool changed = cleanup_tree_cfg_noloop (); | |
787 | ||
788 | if (current_loops != NULL | |
f24ec26f | 789 | && loops_state_satisfies_p (LOOPS_NEED_FIXUP)) |
eb2a640e | 790 | repair_loop_structures (); |
791 | ||
6fa78c7b | 792 | return changed; |
bfd49939 | 793 | } |
794 | ||
f6568ea4 | 795 | /* Tries to merge the PHI nodes at BB into those at BB's sole successor. |
796 | Returns true if successful. */ | |
bfd49939 | 797 | |
f6568ea4 | 798 | static bool |
bfd49939 | 799 | remove_forwarder_block_with_phi (basic_block bb) |
800 | { | |
801 | edge succ = single_succ_edge (bb); | |
802 | basic_block dest = succ->dest; | |
75a70cf9 | 803 | gimple label; |
bfd49939 | 804 | basic_block dombb, domdest, dom; |
805 | ||
806 | /* We check for infinite loops already in tree_forwarder_block_p. | |
807 | However it may happen that the infinite loop is created | |
808 | afterwards due to removal of forwarders. */ | |
809 | if (dest == bb) | |
f6568ea4 | 810 | return false; |
bfd49939 | 811 | |
812 | /* If the destination block consists of a nonlocal label, do not | |
813 | merge it. */ | |
814 | label = first_stmt (dest); | |
815 | if (label | |
75a70cf9 | 816 | && gimple_code (label) == GIMPLE_LABEL |
817 | && DECL_NONLOCAL (gimple_label_label (label))) | |
f6568ea4 | 818 | return false; |
bfd49939 | 819 | |
31a370d3 | 820 | /* Record BB's single pred in case we need to update the father |
821 | loop's latch information later. */ | |
822 | basic_block pred = NULL; | |
823 | if (single_pred_p (bb)) | |
824 | pred = single_pred (bb); | |
825 | ||
bfd49939 | 826 | /* Redirect each incoming edge to BB to DEST. */ |
827 | while (EDGE_COUNT (bb->preds) > 0) | |
828 | { | |
829 | edge e = EDGE_PRED (bb, 0), s; | |
75a70cf9 | 830 | gimple_stmt_iterator gsi; |
bfd49939 | 831 | |
832 | s = find_edge (e->src, dest); | |
833 | if (s) | |
834 | { | |
835 | /* We already have an edge S from E->src to DEST. If S and | |
836 | E->dest's sole successor edge have the same PHI arguments | |
837 | at DEST, redirect S to DEST. */ | |
838 | if (phi_alternatives_equal (dest, s, succ)) | |
839 | { | |
840 | e = redirect_edge_and_branch (e, dest); | |
d03ba86f | 841 | redirect_edge_var_map_clear (e); |
bfd49939 | 842 | continue; |
843 | } | |
844 | ||
845 | /* PHI arguments are different. Create a forwarder block by | |
846 | splitting E so that we can merge PHI arguments on E to | |
847 | DEST. */ | |
848 | e = single_succ_edge (split_edge (e)); | |
849 | } | |
850 | ||
851 | s = redirect_edge_and_branch (e, dest); | |
852 | ||
853 | /* redirect_edge_and_branch must not create a new edge. */ | |
854 | gcc_assert (s == e); | |
855 | ||
856 | /* Add to the PHI nodes at DEST each PHI argument removed at the | |
857 | destination of E. */ | |
75a70cf9 | 858 | for (gsi = gsi_start_phis (dest); |
859 | !gsi_end_p (gsi); | |
860 | gsi_next (&gsi)) | |
bfd49939 | 861 | { |
75a70cf9 | 862 | gimple phi = gsi_stmt (gsi); |
863 | tree def = gimple_phi_arg_def (phi, succ->dest_idx); | |
efbcb6de | 864 | source_location locus = gimple_phi_arg_location_from_edge (phi, succ); |
bfd49939 | 865 | |
866 | if (TREE_CODE (def) == SSA_NAME) | |
867 | { | |
bfd49939 | 868 | /* If DEF is one of the results of PHI nodes removed during |
869 | redirection, replace it with the PHI argument that used | |
870 | to be on E. */ | |
06ecf488 | 871 | vec<edge_var_map> *head = redirect_edge_var_map_vector (e); |
872 | size_t length = head ? head->length () : 0; | |
873 | for (size_t i = 0; i < length; i++) | |
bfd49939 | 874 | { |
06ecf488 | 875 | edge_var_map *vm = &(*head)[i]; |
d03ba86f | 876 | tree old_arg = redirect_edge_var_map_result (vm); |
877 | tree new_arg = redirect_edge_var_map_def (vm); | |
bfd49939 | 878 | |
879 | if (def == old_arg) | |
880 | { | |
881 | def = new_arg; | |
efbcb6de | 882 | locus = redirect_edge_var_map_location (vm); |
bfd49939 | 883 | break; |
884 | } | |
885 | } | |
886 | } | |
887 | ||
60d535d2 | 888 | add_phi_arg (phi, def, s, locus); |
bfd49939 | 889 | } |
890 | ||
d03ba86f | 891 | redirect_edge_var_map_clear (e); |
bfd49939 | 892 | } |
893 | ||
894 | /* Update the dominators. */ | |
895 | dombb = get_immediate_dominator (CDI_DOMINATORS, bb); | |
896 | domdest = get_immediate_dominator (CDI_DOMINATORS, dest); | |
897 | if (domdest == bb) | |
898 | { | |
899 | /* Shortcut to avoid calling (relatively expensive) | |
900 | nearest_common_dominator unless necessary. */ | |
901 | dom = dombb; | |
902 | } | |
903 | else | |
904 | dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb); | |
905 | ||
906 | set_immediate_dominator (CDI_DOMINATORS, dest, dom); | |
907 | ||
31a370d3 | 908 | /* Adjust latch infomation of BB's parent loop as otherwise |
909 | the cfg hook has a hard time not to kill the loop. */ | |
910 | if (current_loops && bb->loop_father->latch == bb) | |
911 | bb->loop_father->latch = pred; | |
912 | ||
bfd49939 | 913 | /* Remove BB since all of BB's incoming edges have been redirected |
914 | to DEST. */ | |
915 | delete_basic_block (bb); | |
f6568ea4 | 916 | |
917 | return true; | |
bfd49939 | 918 | } |
919 | ||
920 | /* This pass merges PHI nodes if one feeds into another. For example, | |
921 | suppose we have the following: | |
922 | ||
923 | goto <bb 9> (<L9>); | |
924 | ||
925 | <L8>:; | |
926 | tem_17 = foo (); | |
927 | ||
928 | # tem_6 = PHI <tem_17(8), tem_23(7)>; | |
929 | <L9>:; | |
930 | ||
931 | # tem_3 = PHI <tem_6(9), tem_2(5)>; | |
932 | <L10>:; | |
933 | ||
934 | Then we merge the first PHI node into the second one like so: | |
935 | ||
936 | goto <bb 9> (<L10>); | |
937 | ||
938 | <L8>:; | |
939 | tem_17 = foo (); | |
940 | ||
941 | # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>; | |
942 | <L10>:; | |
943 | */ | |
944 | ||
65b0537f | 945 | namespace { |
946 | ||
947 | const pass_data pass_data_merge_phi = | |
948 | { | |
949 | GIMPLE_PASS, /* type */ | |
950 | "mergephi", /* name */ | |
951 | OPTGROUP_NONE, /* optinfo_flags */ | |
65b0537f | 952 | TV_TREE_MERGE_PHI, /* tv_id */ |
953 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
954 | 0, /* properties_provided */ | |
955 | 0, /* properties_destroyed */ | |
956 | 0, /* todo_flags_start */ | |
8b88439e | 957 | 0, /* todo_flags_finish */ |
65b0537f | 958 | }; |
959 | ||
960 | class pass_merge_phi : public gimple_opt_pass | |
bfd49939 | 961 | { |
65b0537f | 962 | public: |
963 | pass_merge_phi (gcc::context *ctxt) | |
964 | : gimple_opt_pass (pass_data_merge_phi, ctxt) | |
965 | {} | |
966 | ||
967 | /* opt_pass methods: */ | |
968 | opt_pass * clone () { return new pass_merge_phi (m_ctxt); } | |
969 | virtual unsigned int execute (function *); | |
970 | ||
971 | }; // class pass_merge_phi | |
972 | ||
973 | unsigned int | |
974 | pass_merge_phi::execute (function *fun) | |
975 | { | |
976 | basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun)); | |
bfd49939 | 977 | basic_block *current = worklist; |
978 | basic_block bb; | |
979 | ||
980 | calculate_dominance_info (CDI_DOMINATORS); | |
981 | ||
982 | /* Find all PHI nodes that we may be able to merge. */ | |
65b0537f | 983 | FOR_EACH_BB_FN (bb, fun) |
bfd49939 | 984 | { |
985 | basic_block dest; | |
986 | ||
987 | /* Look for a forwarder block with PHI nodes. */ | |
988 | if (!tree_forwarder_block_p (bb, true)) | |
989 | continue; | |
990 | ||
991 | dest = single_succ (bb); | |
992 | ||
993 | /* We have to feed into another basic block with PHI | |
994 | nodes. */ | |
be2517f5 | 995 | if (gimple_seq_empty_p (phi_nodes (dest)) |
bfd49939 | 996 | /* We don't want to deal with a basic block with |
997 | abnormal edges. */ | |
f1047120 | 998 | || bb_has_abnormal_pred (bb)) |
bfd49939 | 999 | continue; |
1000 | ||
1001 | if (!dominated_by_p (CDI_DOMINATORS, dest, bb)) | |
1002 | { | |
1003 | /* If BB does not dominate DEST, then the PHI nodes at | |
1004 | DEST must be the only users of the results of the PHI | |
1005 | nodes at BB. */ | |
1006 | *current++ = bb; | |
1007 | } | |
cbbe9173 | 1008 | else |
1009 | { | |
75a70cf9 | 1010 | gimple_stmt_iterator gsi; |
f9ead664 | 1011 | unsigned int dest_idx = single_succ_edge (bb)->dest_idx; |
cbbe9173 | 1012 | |
1013 | /* BB dominates DEST. There may be many users of the PHI | |
1014 | nodes in BB. However, there is still a trivial case we | |
1015 | can handle. If the result of every PHI in BB is used | |
1016 | only by a PHI in DEST, then we can trivially merge the | |
1017 | PHI nodes from BB into DEST. */ | |
75a70cf9 | 1018 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
1019 | gsi_next (&gsi)) | |
cbbe9173 | 1020 | { |
75a70cf9 | 1021 | gimple phi = gsi_stmt (gsi); |
1022 | tree result = gimple_phi_result (phi); | |
cbbe9173 | 1023 | use_operand_p imm_use; |
75a70cf9 | 1024 | gimple use_stmt; |
cbbe9173 | 1025 | |
1026 | /* If the PHI's result is never used, then we can just | |
1027 | ignore it. */ | |
72e3ec84 | 1028 | if (has_zero_uses (result)) |
cbbe9173 | 1029 | continue; |
1030 | ||
1031 | /* Get the single use of the result of this PHI node. */ | |
1032 | if (!single_imm_use (result, &imm_use, &use_stmt) | |
75a70cf9 | 1033 | || gimple_code (use_stmt) != GIMPLE_PHI |
1034 | || gimple_bb (use_stmt) != dest | |
1035 | || gimple_phi_arg_def (use_stmt, dest_idx) != result) | |
cbbe9173 | 1036 | break; |
1037 | } | |
1038 | ||
334ec2d8 | 1039 | /* If the loop above iterated through all the PHI nodes |
cbbe9173 | 1040 | in BB, then we can merge the PHIs from BB into DEST. */ |
75a70cf9 | 1041 | if (gsi_end_p (gsi)) |
cbbe9173 | 1042 | *current++ = bb; |
1043 | } | |
bfd49939 | 1044 | } |
1045 | ||
1046 | /* Now let's drain WORKLIST. */ | |
f6568ea4 | 1047 | bool changed = false; |
bfd49939 | 1048 | while (current != worklist) |
1049 | { | |
1050 | bb = *--current; | |
f6568ea4 | 1051 | changed |= remove_forwarder_block_with_phi (bb); |
bfd49939 | 1052 | } |
bfd49939 | 1053 | free (worklist); |
f6568ea4 | 1054 | |
1055 | /* Removing forwarder blocks can cause formerly irreducible loops | |
1056 | to become reducible if we merged two entry blocks. */ | |
1057 | if (changed | |
1058 | && current_loops) | |
1059 | loops_state_set (LOOPS_NEED_FIXUP); | |
1060 | ||
2a1990e9 | 1061 | return 0; |
bfd49939 | 1062 | } |
1063 | ||
cbe8bda8 | 1064 | } // anon namespace |
1065 | ||
1066 | gimple_opt_pass * | |
1067 | make_pass_merge_phi (gcc::context *ctxt) | |
1068 | { | |
1069 | return new pass_merge_phi (ctxt); | |
1070 | } | |
424a4a92 | 1071 | |
1072 | /* Pass: cleanup the CFG just before expanding trees to RTL. | |
1073 | This is just a round of label cleanups and case node grouping | |
1074 | because after the tree optimizers have run such cleanups may | |
1075 | be necessary. */ | |
1076 | ||
1077 | static unsigned int | |
1078 | execute_cleanup_cfg_post_optimizing (void) | |
1079 | { | |
1080 | unsigned int todo = 0; | |
1081 | if (cleanup_tree_cfg ()) | |
1082 | todo |= TODO_update_ssa; | |
1083 | maybe_remove_unreachable_handlers (); | |
1084 | cleanup_dead_labels (); | |
1085 | group_case_labels (); | |
1086 | if ((flag_compare_debug_opt || flag_compare_debug) | |
1087 | && flag_dump_final_insns) | |
1088 | { | |
1089 | FILE *final_output = fopen (flag_dump_final_insns, "a"); | |
1090 | ||
1091 | if (!final_output) | |
1092 | { | |
1093 | error ("could not open final insn dump file %qs: %m", | |
1094 | flag_dump_final_insns); | |
1095 | flag_dump_final_insns = NULL; | |
1096 | } | |
1097 | else | |
1098 | { | |
1099 | int save_unnumbered = flag_dump_unnumbered; | |
1100 | int save_noaddr = flag_dump_noaddr; | |
1101 | ||
1102 | flag_dump_noaddr = flag_dump_unnumbered = 1; | |
1103 | fprintf (final_output, "\n"); | |
1104 | dump_enumerated_decls (final_output, dump_flags | TDF_NOUID); | |
1105 | flag_dump_noaddr = save_noaddr; | |
1106 | flag_dump_unnumbered = save_unnumbered; | |
1107 | if (fclose (final_output)) | |
1108 | { | |
1109 | error ("could not close final insn dump file %qs: %m", | |
1110 | flag_dump_final_insns); | |
1111 | flag_dump_final_insns = NULL; | |
1112 | } | |
1113 | } | |
1114 | } | |
1115 | return todo; | |
1116 | } | |
1117 | ||
1118 | namespace { | |
1119 | ||
1120 | const pass_data pass_data_cleanup_cfg_post_optimizing = | |
1121 | { | |
1122 | GIMPLE_PASS, /* type */ | |
1123 | "optimized", /* name */ | |
1124 | OPTGROUP_NONE, /* optinfo_flags */ | |
424a4a92 | 1125 | TV_TREE_CLEANUP_CFG, /* tv_id */ |
1126 | PROP_cfg, /* properties_required */ | |
1127 | 0, /* properties_provided */ | |
1128 | 0, /* properties_destroyed */ | |
1129 | 0, /* todo_flags_start */ | |
1130 | TODO_remove_unused_locals, /* todo_flags_finish */ | |
1131 | }; | |
1132 | ||
1133 | class pass_cleanup_cfg_post_optimizing : public gimple_opt_pass | |
1134 | { | |
1135 | public: | |
1136 | pass_cleanup_cfg_post_optimizing (gcc::context *ctxt) | |
1137 | : gimple_opt_pass (pass_data_cleanup_cfg_post_optimizing, ctxt) | |
1138 | {} | |
1139 | ||
1140 | /* opt_pass methods: */ | |
65b0537f | 1141 | virtual unsigned int execute (function *) |
1142 | { | |
1143 | return execute_cleanup_cfg_post_optimizing (); | |
1144 | } | |
424a4a92 | 1145 | |
1146 | }; // class pass_cleanup_cfg_post_optimizing | |
1147 | ||
1148 | } // anon namespace | |
1149 | ||
1150 | gimple_opt_pass * | |
1151 | make_pass_cleanup_cfg_post_optimizing (gcc::context *ctxt) | |
1152 | { | |
1153 | return new pass_cleanup_cfg_post_optimizing (ctxt); | |
1154 | } | |
1155 | ||
1156 |