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