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