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