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