]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-cfgcleanup.c
Zero-initialize pass_manager
[thirdparty/gcc.git] / gcc / tree-cfgcleanup.c
CommitLineData
c9784e6d 1/* CFG cleanup for trees.
d1e082c2 2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
c9784e6d
KH
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
9dcd6f09 8the Free Software Foundation; either version 3, or (at your option)
c9784e6d
KH
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
c9784e6d
KH
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "tm.h"
24#include "tree.h"
c9784e6d 25#include "tm_p.h"
c9784e6d 26#include "basic-block.h"
718f9c0f 27#include "diagnostic-core.h"
c9784e6d
KH
28#include "flags.h"
29#include "function.h"
c9784e6d
KH
30#include "ggc.h"
31#include "langhooks.h"
c9784e6d 32#include "tree-flow.h"
c9784e6d 33#include "tree-pass.h"
c9784e6d
KH
34#include "except.h"
35#include "cfgloop.h"
c9784e6d
KH
36#include "hashtab.h"
37#include "tree-ssa-propagate.h"
17684618 38#include "tree-scalar-evolution.h"
c9784e6d 39
672987e8
ZD
40/* The set of blocks in that at least one of the following changes happened:
41 -- the statement at the end of the block was changed
42 -- the block was newly created
43 -- the set of the predecessors of the block changed
44 -- the set of the successors of the block changed
45 ??? Maybe we could track these changes separately, since they determine
46 what cleanups it makes sense to try on the block. */
47bitmap cfgcleanup_altered_bbs;
48
c9784e6d
KH
49/* Remove any fallthru edge from EV. Return true if an edge was removed. */
50
51static bool
9771b263 52remove_fallthru_edge (vec<edge, va_gc> *ev)
c9784e6d
KH
53{
54 edge_iterator ei;
55 edge e;
56
57 FOR_EACH_EDGE (e, ei, ev)
58 if ((e->flags & EDGE_FALLTHRU) != 0)
59 {
0107dca2
RB
60 if (e->flags & EDGE_COMPLEX)
61 e->flags &= ~EDGE_FALLTHRU;
62 else
63 remove_edge_and_dominated_blocks (e);
c9784e6d
KH
64 return true;
65 }
66 return false;
67}
68
726a989a 69
c9784e6d
KH
70/* Disconnect an unreachable block in the control expression starting
71 at block BB. */
72
73static bool
726a989a 74cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
c9784e6d
KH
75{
76 edge taken_edge;
77 bool retval = false;
726a989a
RB
78 gimple stmt = gsi_stmt (gsi);
79 tree val;
c9784e6d
KH
80
81 if (!single_succ_p (bb))
82 {
83 edge e;
84 edge_iterator ei;
6ac01510 85 bool warned;
37530014 86 location_t loc;
6ac01510
ILT
87
88 fold_defer_overflow_warnings ();
37530014
RG
89 loc = gimple_location (stmt);
90 switch (gimple_code (stmt))
91 {
92 case GIMPLE_COND:
fea4ea73
EB
93 val = fold_binary_loc (loc, gimple_cond_code (stmt),
94 boolean_type_node,
95 gimple_cond_lhs (stmt),
96 gimple_cond_rhs (stmt));
97 break;
37530014
RG
98
99 case GIMPLE_SWITCH:
100 val = gimple_switch_index (stmt);
101 break;
102
103 default:
104 val = NULL_TREE;
105 }
c9784e6d
KH
106 taken_edge = find_taken_edge (bb, val);
107 if (!taken_edge)
6ac01510
ILT
108 {
109 fold_undefer_and_ignore_overflow_warnings ();
110 return false;
111 }
c9784e6d
KH
112
113 /* Remove all the edges except the one that is always executed. */
6ac01510 114 warned = false;
c9784e6d
KH
115 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
116 {
117 if (e != taken_edge)
118 {
6ac01510
ILT
119 if (!warned)
120 {
121 fold_undefer_overflow_warnings
726a989a 122 (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
6ac01510
ILT
123 warned = true;
124 }
125
c9784e6d
KH
126 taken_edge->probability += e->probability;
127 taken_edge->count += e->count;
672987e8 128 remove_edge_and_dominated_blocks (e);
c9784e6d
KH
129 retval = true;
130 }
131 else
132 ei_next (&ei);
133 }
6ac01510
ILT
134 if (!warned)
135 fold_undefer_and_ignore_overflow_warnings ();
c9784e6d
KH
136 if (taken_edge->probability > REG_BR_PROB_BASE)
137 taken_edge->probability = REG_BR_PROB_BASE;
138 }
139 else
140 taken_edge = single_succ_edge (bb);
141
672987e8 142 bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
726a989a 143 gsi_remove (&gsi, true);
c9784e6d
KH
144 taken_edge->flags = EDGE_FALLTHRU;
145
c9784e6d
KH
146 return retval;
147}
148
672987e8
ZD
149/* Try to remove superfluous control structures in basic block BB. Returns
150 true if anything changes. */
c9784e6d
KH
151
152static bool
672987e8 153cleanup_control_flow_bb (basic_block bb)
c9784e6d 154{
726a989a 155 gimple_stmt_iterator gsi;
c9784e6d 156 bool retval = false;
726a989a 157 gimple stmt;
c9784e6d 158
672987e8
ZD
159 /* If the last statement of the block could throw and now cannot,
160 we need to prune cfg. */
726a989a 161 retval |= gimple_purge_dead_eh_edges (bb);
672987e8 162
726a989a
RB
163 gsi = gsi_last_bb (bb);
164 if (gsi_end_p (gsi))
672987e8
ZD
165 return retval;
166
726a989a 167 stmt = gsi_stmt (gsi);
672987e8 168
726a989a
RB
169 if (gimple_code (stmt) == GIMPLE_COND
170 || gimple_code (stmt) == GIMPLE_SWITCH)
171 retval |= cleanup_control_expr_graph (bb, gsi);
172 else if (gimple_code (stmt) == GIMPLE_GOTO
173 && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
174 && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0))
672987e8 175 == LABEL_DECL))
c9784e6d 176 {
726a989a
RB
177 /* If we had a computed goto which has a compile-time determinable
178 destination, then we can eliminate the goto. */
672987e8
ZD
179 edge e;
180 tree label;
181 edge_iterator ei;
182 basic_block target_block;
c9784e6d 183
672987e8
ZD
184 /* First look at all the outgoing edges. Delete any outgoing
185 edges which do not go to the right block. For the one
186 edge which goes to the right block, fix up its flags. */
726a989a 187 label = TREE_OPERAND (gimple_goto_dest (stmt), 0);
672987e8
ZD
188 target_block = label_to_block (label);
189 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
c9784e6d 190 {
672987e8
ZD
191 if (e->dest != target_block)
192 remove_edge_and_dominated_blocks (e);
193 else
c9784e6d 194 {
672987e8
ZD
195 /* Turn off the EDGE_ABNORMAL flag. */
196 e->flags &= ~EDGE_ABNORMAL;
c9784e6d 197
672987e8
ZD
198 /* And set EDGE_FALLTHRU. */
199 e->flags |= EDGE_FALLTHRU;
200 ei_next (&ei);
201 }
c9784e6d
KH
202 }
203
672987e8
ZD
204 bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
205 bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index);
206
207 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
208 relevant information we need. */
726a989a 209 gsi_remove (&gsi, true);
672987e8 210 retval = true;
c9784e6d 211 }
672987e8
ZD
212
213 /* Check for indirect calls that have been turned into
214 noreturn calls. */
726a989a
RB
215 else if (is_gimple_call (stmt)
216 && gimple_call_noreturn_p (stmt)
217 && remove_fallthru_edge (bb->succs))
672987e8
ZD
218 retval = true;
219
c9784e6d
KH
220 return retval;
221}
222
223/* Return true if basic block BB does nothing except pass control
224 flow to another block and that we can safely insert a label at
225 the start of the successor block.
226
227 As a precondition, we require that BB be not equal to
228 ENTRY_BLOCK_PTR. */
229
230static bool
231tree_forwarder_block_p (basic_block bb, bool phi_wanted)
232{
726a989a 233 gimple_stmt_iterator gsi;
f99fcb3b 234 location_t locus;
c9784e6d
KH
235
236 /* BB must have a single outgoing edge. */
237 if (single_succ_p (bb) != 1
238 /* If PHI_WANTED is false, BB must not have any PHI nodes.
239 Otherwise, BB must have PHI nodes. */
726a989a 240 || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
c9784e6d
KH
241 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
242 || single_succ (bb) == EXIT_BLOCK_PTR
243 /* Nor should this be an infinite loop. */
244 || single_succ (bb) == bb
245 /* BB may not have an abnormal outgoing edge. */
246 || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
247 return false;
248
77a74ed7 249 gcc_checking_assert (bb != ENTRY_BLOCK_PTR);
c9784e6d 250
f99fcb3b
JJ
251 locus = single_succ_edge (bb)->goto_locus;
252
1d65f45c
RH
253 /* There should not be an edge coming from entry, or an EH edge. */
254 {
255 edge_iterator ei;
256 edge e;
257
258 FOR_EACH_EDGE (e, ei, bb->preds)
259 if (e->src == ENTRY_BLOCK_PTR || (e->flags & EDGE_EH))
260 return false;
f99fcb3b
JJ
261 /* If goto_locus of any of the edges differs, prevent removing
262 the forwarder block for -O0. */
263 else if (optimize == 0 && e->goto_locus != locus)
264 return false;
1d65f45c
RH
265 }
266
c9784e6d
KH
267 /* Now walk through the statements backward. We can ignore labels,
268 anything else means this is not a forwarder block. */
726a989a 269 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
c9784e6d 270 {
726a989a 271 gimple stmt = gsi_stmt (gsi);
c9784e6d 272
726a989a 273 switch (gimple_code (stmt))
c9784e6d 274 {
726a989a
RB
275 case GIMPLE_LABEL:
276 if (DECL_NONLOCAL (gimple_label_label (stmt)))
c9784e6d 277 return false;
f99fcb3b
JJ
278 if (optimize == 0 && gimple_location (stmt) != locus)
279 return false;
c9784e6d
KH
280 break;
281
b5b8b0ac
AO
282 /* ??? For now, hope there's a corresponding debug
283 assignment at the destination. */
284 case GIMPLE_DEBUG:
285 break;
286
c9784e6d
KH
287 default:
288 return false;
289 }
290 }
291
c9784e6d
KH
292 if (current_loops)
293 {
294 basic_block dest;
295 /* Protect loop latches, headers and preheaders. */
296 if (bb->loop_father->header == bb)
297 return false;
298 dest = EDGE_SUCC (bb, 0)->dest;
299
300 if (dest->loop_father->header == dest)
301 return false;
302 }
c9784e6d
KH
303 return true;
304}
305
c9784e6d
KH
306/* If all the PHI nodes in DEST have alternatives for E1 and E2 and
307 those alternatives are equal in each of the PHI nodes, then return
308 true, else return false. */
309
310static bool
311phi_alternatives_equal (basic_block dest, edge e1, edge e2)
312{
313 int n1 = e1->dest_idx;
314 int n2 = e2->dest_idx;
726a989a 315 gimple_stmt_iterator gsi;
c9784e6d 316
726a989a 317 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
c9784e6d 318 {
726a989a
RB
319 gimple phi = gsi_stmt (gsi);
320 tree val1 = gimple_phi_arg_def (phi, n1);
321 tree val2 = gimple_phi_arg_def (phi, n2);
c9784e6d
KH
322
323 gcc_assert (val1 != NULL_TREE);
324 gcc_assert (val2 != NULL_TREE);
325
326 if (!operand_equal_for_phi_arg_p (val1, val2))
327 return false;
328 }
329
330 return true;
331}
332
672987e8 333/* Removes forwarder block BB. Returns false if this failed. */
c9784e6d
KH
334
335static bool
672987e8 336remove_forwarder_block (basic_block bb)
c9784e6d
KH
337{
338 edge succ = single_succ_edge (bb), e, s;
339 basic_block dest = succ->dest;
726a989a 340 gimple label;
c9784e6d 341 edge_iterator ei;
726a989a 342 gimple_stmt_iterator gsi, gsi_to;
70235ab9 343 bool can_move_debug_stmts;
c9784e6d
KH
344
345 /* We check for infinite loops already in tree_forwarder_block_p.
346 However it may happen that the infinite loop is created
347 afterwards due to removal of forwarders. */
348 if (dest == bb)
349 return false;
350
28e5ca15
RB
351 /* If the destination block consists of a nonlocal label or is a
352 EH landing pad, do not merge it. */
c9784e6d
KH
353 label = first_stmt (dest);
354 if (label
726a989a 355 && gimple_code (label) == GIMPLE_LABEL
28e5ca15
RB
356 && (DECL_NONLOCAL (gimple_label_label (label))
357 || EH_LANDING_PAD_NR (gimple_label_label (label)) != 0))
c9784e6d
KH
358 return false;
359
360 /* If there is an abnormal edge to basic block BB, but not into
361 dest, problems might occur during removal of the phi node at out
362 of ssa due to overlapping live ranges of registers.
363
364 If there is an abnormal edge in DEST, the problems would occur
365 anyway since cleanup_dead_labels would then merge the labels for
366 two different eh regions, and rest of exception handling code
367 does not like it.
368
369 So if there is an abnormal edge to BB, proceed only if there is
370 no abnormal edge to DEST and there are no phi nodes in DEST. */
31ff2426
NF
371 if (bb_has_abnormal_pred (bb)
372 && (bb_has_abnormal_pred (dest)
1197e789
RG
373 || !gimple_seq_empty_p (phi_nodes (dest))))
374 return false;
c9784e6d
KH
375
376 /* If there are phi nodes in DEST, and some of the blocks that are
377 predecessors of BB are also predecessors of DEST, check that the
378 phi node arguments match. */
726a989a 379 if (!gimple_seq_empty_p (phi_nodes (dest)))
c9784e6d
KH
380 {
381 FOR_EACH_EDGE (e, ei, bb->preds)
382 {
383 s = find_edge (e->src, dest);
384 if (!s)
385 continue;
386
387 if (!phi_alternatives_equal (dest, succ, s))
388 return false;
389 }
390 }
391
dc764d10 392 can_move_debug_stmts = MAY_HAVE_DEBUG_STMTS && single_pred_p (dest);
70235ab9 393
c9784e6d
KH
394 /* Redirect the edges. */
395 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
396 {
672987e8
ZD
397 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
398
c9784e6d
KH
399 if (e->flags & EDGE_ABNORMAL)
400 {
401 /* If there is an abnormal edge, redirect it anyway, and
402 move the labels to the new block to make it legal. */
403 s = redirect_edge_succ_nodup (e, dest);
404 }
405 else
406 s = redirect_edge_and_branch (e, dest);
407
408 if (s == e)
409 {
410 /* Create arguments for the phi nodes, since the edge was not
411 here before. */
726a989a
RB
412 for (gsi = gsi_start_phis (dest);
413 !gsi_end_p (gsi);
414 gsi_next (&gsi))
415 {
416 gimple phi = gsi_stmt (gsi);
f5045c96 417 source_location l = gimple_phi_arg_location_from_edge (phi, succ);
2724573f
RB
418 tree def = gimple_phi_arg_def (phi, succ->dest_idx);
419 add_phi_arg (phi, unshare_expr (def), s, l);
726a989a 420 }
c9784e6d 421 }
c9784e6d
KH
422 }
423
1197e789
RG
424 /* Move nonlocal labels and computed goto targets as well as user
425 defined labels and labels with an EH landing pad number to the
426 new block, so that the redirection of the abnormal edges works,
427 jump targets end up in a sane place and debug information for
428 labels is retained. */
429 gsi_to = gsi_start_bb (dest);
430 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
431 {
432 tree decl;
433 label = gsi_stmt (gsi);
434 if (is_gimple_debug (label))
435 break;
436 decl = gimple_label_label (label);
437 if (EH_LANDING_PAD_NR (decl) != 0
438 || DECL_NONLOCAL (decl)
439 || FORCED_LABEL (decl)
440 || !DECL_ARTIFICIAL (decl))
441 {
442 gsi_remove (&gsi, false);
443 gsi_insert_before (&gsi_to, label, GSI_SAME_STMT);
444 }
445 else
446 gsi_next (&gsi);
447 }
448
dc764d10 449 /* Move debug statements if the destination has a single predecessor. */
70235ab9 450 if (can_move_debug_stmts)
c9784e6d 451 {
1197e789
RG
452 gsi_to = gsi_after_labels (dest);
453 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); )
c9784e6d 454 {
70235ab9
JJ
455 gimple debug = gsi_stmt (gsi);
456 if (!is_gimple_debug (debug))
1197e789 457 break;
726a989a 458 gsi_remove (&gsi, false);
70235ab9 459 gsi_insert_before (&gsi_to, debug, GSI_SAME_STMT);
c9784e6d
KH
460 }
461 }
462
672987e8
ZD
463 bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
464
c9784e6d
KH
465 /* Update the dominators. */
466 if (dom_info_available_p (CDI_DOMINATORS))
467 {
468 basic_block dom, dombb, domdest;
469
470 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
471 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
472 if (domdest == bb)
473 {
474 /* Shortcut to avoid calling (relatively expensive)
475 nearest_common_dominator unless necessary. */
476 dom = dombb;
477 }
478 else
479 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
480
481 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
482 }
483
484 /* And kill the forwarder block. */
485 delete_basic_block (bb);
486
487 return true;
488}
489
566d09ef
JH
490/* STMT is a call that has been discovered noreturn. Fixup the CFG
491 and remove LHS. Return true if something changed. */
492
493bool
494fixup_noreturn_call (gimple stmt)
495{
496 basic_block bb = gimple_bb (stmt);
497 bool changed = false;
498
499 if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
500 return false;
501
502 /* First split basic block if stmt is not last. */
503 if (stmt != gsi_stmt (gsi_last_bb (bb)))
504 split_block (bb, stmt);
505
506 changed |= remove_fallthru_edge (bb->succs);
507
508 /* If there is LHS, remove it. */
509 if (gimple_call_lhs (stmt))
510 {
511 tree op = gimple_call_lhs (stmt);
512 gimple_call_set_lhs (stmt, NULL_TREE);
02d635a2 513
acb6411a 514 /* We need to remove SSA name to avoid checking errors.
566d09ef 515 All uses are dominated by the noreturn and thus will
02d635a2
JH
516 be removed afterwards.
517 We proactively remove affected non-PHI statements to avoid
518 fixup_cfg from trying to update them and crashing. */
566d09ef
JH
519 if (TREE_CODE (op) == SSA_NAME)
520 {
521 use_operand_p use_p;
522 imm_use_iterator iter;
523 gimple use_stmt;
02d635a2
JH
524 bitmap_iterator bi;
525 unsigned int bb_index;
526
527 bitmap blocks = BITMAP_ALLOC (NULL);
566d09ef
JH
528
529 FOR_EACH_IMM_USE_STMT (use_stmt, iter, op)
02d635a2
JH
530 {
531 if (gimple_code (use_stmt) != GIMPLE_PHI)
532 bitmap_set_bit (blocks, gimple_bb (use_stmt)->index);
533 else
534 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
535 SET_USE (use_p, error_mark_node);
536 }
537 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, bb_index, bi)
538 delete_basic_block (BASIC_BLOCK (bb_index));
539 BITMAP_FREE (blocks);
540 release_ssa_name (op);
566d09ef
JH
541 }
542 update_stmt (stmt);
543 changed = true;
544 }
8ba0479e
JJ
545 /* Similarly remove VDEF if there is any. */
546 else if (gimple_vdef (stmt))
547 update_stmt (stmt);
566d09ef
JH
548 return changed;
549}
550
551
672987e8
ZD
552/* Split basic blocks on calls in the middle of a basic block that are now
553 known not to return, and remove the unreachable code. */
c9784e6d
KH
554
555static bool
672987e8 556split_bbs_on_noreturn_calls (void)
c9784e6d 557{
c9784e6d 558 bool changed = false;
726a989a 559 gimple stmt;
672987e8 560 basic_block bb;
c9784e6d 561
672987e8
ZD
562 /* Detect cases where a mid-block call is now known not to return. */
563 if (cfun->gimple_df)
9771b263 564 while (vec_safe_length (MODIFIED_NORETURN_CALLS (cfun)))
672987e8 565 {
9771b263 566 stmt = MODIFIED_NORETURN_CALLS (cfun)->pop ();
726a989a 567 bb = gimple_bb (stmt);
4d731f17
JJ
568 /* BB might be deleted at this point, so verify first
569 BB is present in the cfg. */
672987e8 570 if (bb == NULL
4d731f17 571 || bb->index < NUM_FIXED_BLOCKS
30f1e6de 572 || bb->index >= last_basic_block
4d731f17 573 || BASIC_BLOCK (bb->index) != bb
726a989a 574 || !gimple_call_noreturn_p (stmt))
672987e8
ZD
575 continue;
576
566d09ef 577 changed |= fixup_noreturn_call (stmt);
672987e8 578 }
c9784e6d 579
c9784e6d
KH
580 return changed;
581}
582
672987e8
ZD
583/* Tries to cleanup cfg in basic block BB. Returns true if anything
584 changes. */
c9784e6d 585
89e80dd4 586static bool
672987e8 587cleanup_tree_cfg_bb (basic_block bb)
c9784e6d 588{
65e7bfe3 589 bool retval = cleanup_control_flow_bb (bb);
b8698a0f 590
f99fcb3b 591 if (tree_forwarder_block_p (bb, false)
672987e8
ZD
592 && remove_forwarder_block (bb))
593 return true;
c9784e6d 594
89e80dd4
DN
595 /* Merging the blocks may create new opportunities for folding
596 conditional branches (due to the elimination of single-valued PHI
597 nodes). */
672987e8
ZD
598 if (single_succ_p (bb)
599 && can_merge_blocks_p (bb, single_succ (bb)))
600 {
601 merge_blocks (bb, single_succ (bb));
602 return true;
603 }
604
605 return retval;
606}
607
608/* Iterate the cfg cleanups, while anything changes. */
609
610static bool
611cleanup_tree_cfg_1 (void)
612{
613 bool retval = false;
614 basic_block bb;
615 unsigned i, n;
616
617 retval |= split_bbs_on_noreturn_calls ();
618
619 /* Prepare the worklists of altered blocks. */
620 cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL);
621
622 /* During forwarder block cleanup, we may redirect edges out of
623 SWITCH_EXPRs, which can get expensive. So we want to enable
624 recording of edge to CASE_LABEL_EXPR. */
625 start_recording_case_labels ();
89e80dd4 626
672987e8
ZD
627 /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB,
628 since the basic blocks may get removed. */
629 n = last_basic_block;
630 for (i = NUM_FIXED_BLOCKS; i < n; i++)
631 {
632 bb = BASIC_BLOCK (i);
633 if (bb)
634 retval |= cleanup_tree_cfg_bb (bb);
635 }
636
637 /* Now process the altered blocks, as long as any are available. */
638 while (!bitmap_empty_p (cfgcleanup_altered_bbs))
639 {
640 i = bitmap_first_set_bit (cfgcleanup_altered_bbs);
641 bitmap_clear_bit (cfgcleanup_altered_bbs, i);
642 if (i < NUM_FIXED_BLOCKS)
643 continue;
644
645 bb = BASIC_BLOCK (i);
646 if (!bb)
647 continue;
648
649 retval |= cleanup_tree_cfg_bb (bb);
650
651 /* Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn
652 calls. */
653 retval |= split_bbs_on_noreturn_calls ();
654 }
b8698a0f 655
672987e8
ZD
656 end_recording_case_labels ();
657 BITMAP_FREE (cfgcleanup_altered_bbs);
89e80dd4
DN
658 return retval;
659}
660
661
e3594cb3
DN
662/* Remove unreachable blocks and other miscellaneous clean up work.
663 Return true if the flowgraph was modified, false otherwise. */
89e80dd4 664
592c303d
ZD
665static bool
666cleanup_tree_cfg_noloop (void)
89e80dd4 667{
672987e8 668 bool changed;
89e80dd4
DN
669
670 timevar_push (TV_TREE_CLEANUP_CFG);
671
e3594cb3 672 /* Iterate until there are no more cleanups left to do. If any
672987e8
ZD
673 iteration changed the flowgraph, set CHANGED to true.
674
675 If dominance information is available, there cannot be any unreachable
676 blocks. */
2b28c07a 677 if (!dom_info_available_p (CDI_DOMINATORS))
e3594cb3 678 {
672987e8
ZD
679 changed = delete_unreachable_blocks ();
680 calculate_dominance_info (CDI_DOMINATORS);
e3594cb3 681 }
672987e8 682 else
30251f7a
ZD
683 {
684#ifdef ENABLE_CHECKING
685 verify_dominators (CDI_DOMINATORS);
686#endif
687 changed = false;
688 }
c9784e6d 689
672987e8
ZD
690 changed |= cleanup_tree_cfg_1 ();
691
2b28c07a 692 gcc_assert (dom_info_available_p (CDI_DOMINATORS));
c9784e6d
KH
693 compact_blocks ();
694
695#ifdef ENABLE_CHECKING
696 verify_flow_info ();
697#endif
89e80dd4 698
c9784e6d 699 timevar_pop (TV_TREE_CLEANUP_CFG);
89e80dd4 700
592c303d 701 if (changed && current_loops)
f87000d0 702 loops_state_set (LOOPS_NEED_FIXUP);
592c303d 703
e3594cb3 704 return changed;
c9784e6d
KH
705}
706
592c303d 707/* Repairs loop structures. */
c9784e6d 708
592c303d
ZD
709static void
710repair_loop_structures (void)
c9784e6d 711{
a222c01a 712 bitmap changed_bbs;
8e89b5b5 713 unsigned n_new_loops;
a222c01a 714
cc360b36
SB
715 calculate_dominance_info (CDI_DOMINATORS);
716
a222c01a
MM
717 timevar_push (TV_REPAIR_LOOPS);
718 changed_bbs = BITMAP_ALLOC (NULL);
8e89b5b5 719 n_new_loops = fix_loop_structure (changed_bbs);
c9784e6d 720
592c303d
ZD
721 /* This usually does nothing. But sometimes parts of cfg that originally
722 were inside a loop get out of it due to edge removal (since they
8e89b5b5
RB
723 become unreachable by back edges from latch). Also a former
724 irreducible loop can become reducible - in this case force a full
725 rewrite into loop-closed SSA form. */
f87000d0 726 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
8e89b5b5
RB
727 rewrite_into_loop_closed_ssa (n_new_loops ? NULL : changed_bbs,
728 TODO_update_ssa);
c9784e6d 729
592c303d 730 BITMAP_FREE (changed_bbs);
c9784e6d
KH
731
732#ifdef ENABLE_CHECKING
592c303d 733 verify_loop_structure ();
c9784e6d 734#endif
592c303d
ZD
735 scev_reset ();
736
a222c01a 737 timevar_pop (TV_REPAIR_LOOPS);
592c303d
ZD
738}
739
740/* Cleanup cfg and repair loop structures. */
741
742bool
743cleanup_tree_cfg (void)
744{
745 bool changed = cleanup_tree_cfg_noloop ();
746
747 if (current_loops != NULL
f87000d0 748 && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
592c303d
ZD
749 repair_loop_structures ();
750
1994bfea 751 return changed;
c9784e6d
KH
752}
753
a9e0d843
RB
754/* Tries to merge the PHI nodes at BB into those at BB's sole successor.
755 Returns true if successful. */
c9784e6d 756
a9e0d843 757static bool
c9784e6d
KH
758remove_forwarder_block_with_phi (basic_block bb)
759{
760 edge succ = single_succ_edge (bb);
761 basic_block dest = succ->dest;
726a989a 762 gimple label;
c9784e6d
KH
763 basic_block dombb, domdest, dom;
764
765 /* We check for infinite loops already in tree_forwarder_block_p.
766 However it may happen that the infinite loop is created
767 afterwards due to removal of forwarders. */
768 if (dest == bb)
a9e0d843 769 return false;
c9784e6d
KH
770
771 /* If the destination block consists of a nonlocal label, do not
772 merge it. */
773 label = first_stmt (dest);
774 if (label
726a989a
RB
775 && gimple_code (label) == GIMPLE_LABEL
776 && DECL_NONLOCAL (gimple_label_label (label)))
a9e0d843 777 return false;
c9784e6d
KH
778
779 /* Redirect each incoming edge to BB to DEST. */
780 while (EDGE_COUNT (bb->preds) > 0)
781 {
782 edge e = EDGE_PRED (bb, 0), s;
726a989a 783 gimple_stmt_iterator gsi;
c9784e6d
KH
784
785 s = find_edge (e->src, dest);
786 if (s)
787 {
788 /* We already have an edge S from E->src to DEST. If S and
789 E->dest's sole successor edge have the same PHI arguments
790 at DEST, redirect S to DEST. */
791 if (phi_alternatives_equal (dest, s, succ))
792 {
793 e = redirect_edge_and_branch (e, dest);
ea7e6d5a 794 redirect_edge_var_map_clear (e);
c9784e6d
KH
795 continue;
796 }
797
798 /* PHI arguments are different. Create a forwarder block by
799 splitting E so that we can merge PHI arguments on E to
800 DEST. */
801 e = single_succ_edge (split_edge (e));
802 }
803
804 s = redirect_edge_and_branch (e, dest);
805
806 /* redirect_edge_and_branch must not create a new edge. */
807 gcc_assert (s == e);
808
809 /* Add to the PHI nodes at DEST each PHI argument removed at the
810 destination of E. */
726a989a
RB
811 for (gsi = gsi_start_phis (dest);
812 !gsi_end_p (gsi);
813 gsi_next (&gsi))
c9784e6d 814 {
726a989a
RB
815 gimple phi = gsi_stmt (gsi);
816 tree def = gimple_phi_arg_def (phi, succ->dest_idx);
f5045c96 817 source_location locus = gimple_phi_arg_location_from_edge (phi, succ);
c9784e6d
KH
818
819 if (TREE_CODE (def) == SSA_NAME)
820 {
9771b263 821 edge_var_map_vector *head;
ea7e6d5a
AH
822 edge_var_map *vm;
823 size_t i;
c9784e6d
KH
824
825 /* If DEF is one of the results of PHI nodes removed during
826 redirection, replace it with the PHI argument that used
827 to be on E. */
ea7e6d5a 828 head = redirect_edge_var_map_vector (e);
6fa5e0ed 829 FOR_EACH_VEC_SAFE_ELT (head, i, vm)
c9784e6d 830 {
ea7e6d5a
AH
831 tree old_arg = redirect_edge_var_map_result (vm);
832 tree new_arg = redirect_edge_var_map_def (vm);
c9784e6d
KH
833
834 if (def == old_arg)
835 {
836 def = new_arg;
f5045c96 837 locus = redirect_edge_var_map_location (vm);
c9784e6d
KH
838 break;
839 }
840 }
841 }
842
9e227d60 843 add_phi_arg (phi, def, s, locus);
c9784e6d
KH
844 }
845
ea7e6d5a 846 redirect_edge_var_map_clear (e);
c9784e6d
KH
847 }
848
849 /* Update the dominators. */
850 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
851 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
852 if (domdest == bb)
853 {
854 /* Shortcut to avoid calling (relatively expensive)
855 nearest_common_dominator unless necessary. */
856 dom = dombb;
857 }
858 else
859 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
860
861 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
862
863 /* Remove BB since all of BB's incoming edges have been redirected
864 to DEST. */
865 delete_basic_block (bb);
a9e0d843
RB
866
867 return true;
c9784e6d
KH
868}
869
870/* This pass merges PHI nodes if one feeds into another. For example,
871 suppose we have the following:
872
873 goto <bb 9> (<L9>);
874
875<L8>:;
876 tem_17 = foo ();
877
878 # tem_6 = PHI <tem_17(8), tem_23(7)>;
879<L9>:;
880
881 # tem_3 = PHI <tem_6(9), tem_2(5)>;
882<L10>:;
883
884 Then we merge the first PHI node into the second one like so:
885
886 goto <bb 9> (<L10>);
887
888<L8>:;
889 tem_17 = foo ();
890
891 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
892<L10>:;
893*/
894
c2924966 895static unsigned int
c9784e6d
KH
896merge_phi_nodes (void)
897{
5ed6ace5 898 basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks);
c9784e6d
KH
899 basic_block *current = worklist;
900 basic_block bb;
901
902 calculate_dominance_info (CDI_DOMINATORS);
903
904 /* Find all PHI nodes that we may be able to merge. */
905 FOR_EACH_BB (bb)
906 {
907 basic_block dest;
908
909 /* Look for a forwarder block with PHI nodes. */
910 if (!tree_forwarder_block_p (bb, true))
911 continue;
912
913 dest = single_succ (bb);
914
915 /* We have to feed into another basic block with PHI
916 nodes. */
8eacd016 917 if (gimple_seq_empty_p (phi_nodes (dest))
c9784e6d
KH
918 /* We don't want to deal with a basic block with
919 abnormal edges. */
31ff2426 920 || bb_has_abnormal_pred (bb))
c9784e6d
KH
921 continue;
922
923 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
924 {
925 /* If BB does not dominate DEST, then the PHI nodes at
926 DEST must be the only users of the results of the PHI
927 nodes at BB. */
928 *current++ = bb;
929 }
ea65cd37
JL
930 else
931 {
726a989a 932 gimple_stmt_iterator gsi;
338b5886 933 unsigned int dest_idx = single_succ_edge (bb)->dest_idx;
ea65cd37
JL
934
935 /* BB dominates DEST. There may be many users of the PHI
936 nodes in BB. However, there is still a trivial case we
937 can handle. If the result of every PHI in BB is used
938 only by a PHI in DEST, then we can trivially merge the
939 PHI nodes from BB into DEST. */
726a989a
RB
940 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
941 gsi_next (&gsi))
ea65cd37 942 {
726a989a
RB
943 gimple phi = gsi_stmt (gsi);
944 tree result = gimple_phi_result (phi);
ea65cd37 945 use_operand_p imm_use;
726a989a 946 gimple use_stmt;
ea65cd37
JL
947
948 /* If the PHI's result is never used, then we can just
949 ignore it. */
bfc646bf 950 if (has_zero_uses (result))
ea65cd37
JL
951 continue;
952
953 /* Get the single use of the result of this PHI node. */
954 if (!single_imm_use (result, &imm_use, &use_stmt)
726a989a
RB
955 || gimple_code (use_stmt) != GIMPLE_PHI
956 || gimple_bb (use_stmt) != dest
957 || gimple_phi_arg_def (use_stmt, dest_idx) != result)
ea65cd37
JL
958 break;
959 }
960
c0220ea4 961 /* If the loop above iterated through all the PHI nodes
ea65cd37 962 in BB, then we can merge the PHIs from BB into DEST. */
726a989a 963 if (gsi_end_p (gsi))
ea65cd37
JL
964 *current++ = bb;
965 }
c9784e6d
KH
966 }
967
968 /* Now let's drain WORKLIST. */
a9e0d843 969 bool changed = false;
c9784e6d
KH
970 while (current != worklist)
971 {
972 bb = *--current;
a9e0d843 973 changed |= remove_forwarder_block_with_phi (bb);
c9784e6d 974 }
c9784e6d 975 free (worklist);
a9e0d843
RB
976
977 /* Removing forwarder blocks can cause formerly irreducible loops
978 to become reducible if we merged two entry blocks. */
979 if (changed
980 && current_loops)
981 loops_state_set (LOOPS_NEED_FIXUP);
982
c2924966 983 return 0;
c9784e6d
KH
984}
985
986static bool
987gate_merge_phi (void)
988{
989 return 1;
990}
991
b8698a0f 992struct gimple_opt_pass pass_merge_phi =
8ddbbcae
JH
993{
994 {
995 GIMPLE_PASS,
c9784e6d 996 "mergephi", /* name */
2b4e6bf1 997 OPTGROUP_NONE, /* optinfo_flags */
c9784e6d
KH
998 gate_merge_phi, /* gate */
999 merge_phi_nodes, /* execute */
1000 NULL, /* sub */
1001 NULL, /* next */
1002 0, /* static_pass_number */
1003 TV_TREE_MERGE_PHI, /* tv_id */
1004 PROP_cfg | PROP_ssa, /* properties_required */
1005 0, /* properties_provided */
1006 0, /* properties_destroyed */
1007 0, /* todo_flags_start */
bb313b93 1008 TODO_verify_ssa /* todo_flags_finish */
8ddbbcae 1009 }
c9784e6d 1010};