1 /* CFG cleanup for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
36 #include "langhooks.h"
37 #include "diagnostic.h"
38 #include "tree-flow.h"
40 #include "tree-dump.h"
41 #include "tree-pass.h"
45 #include "cfglayout.h"
47 #include "tree-ssa-propagate.h"
48 #include "tree-scalar-evolution.h"
50 /* The set of blocks in that at least one of the following changes happened:
51 -- the statement at the end of the block was changed
52 -- the block was newly created
53 -- the set of the predecessors of the block changed
54 -- the set of the successors of the block changed
55 ??? Maybe we could track these changes separately, since they determine
56 what cleanups it makes sense to try on the block. */
57 bitmap cfgcleanup_altered_bbs;
59 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
62 remove_fallthru_edge (VEC(edge,gc) *ev)
67 FOR_EACH_EDGE (e, ei, ev)
68 if ((e->flags & EDGE_FALLTHRU) != 0)
70 remove_edge_and_dominated_blocks (e);
77 /* Disconnect an unreachable block in the control expression starting
81 cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
85 gimple stmt = gsi_stmt (gsi);
88 if (!single_succ_p (bb))
94 fold_defer_overflow_warnings ();
95 val = gimple_fold (stmt);
96 taken_edge = find_taken_edge (bb, val);
99 fold_undefer_and_ignore_overflow_warnings ();
103 /* Remove all the edges except the one that is always executed. */
105 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
111 fold_undefer_overflow_warnings
112 (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
116 taken_edge->probability += e->probability;
117 taken_edge->count += e->count;
118 remove_edge_and_dominated_blocks (e);
125 fold_undefer_and_ignore_overflow_warnings ();
126 if (taken_edge->probability > REG_BR_PROB_BASE)
127 taken_edge->probability = REG_BR_PROB_BASE;
130 taken_edge = single_succ_edge (bb);
132 bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
133 gsi_remove (&gsi, true);
134 taken_edge->flags = EDGE_FALLTHRU;
139 /* Try to remove superfluous control structures in basic block BB. Returns
140 true if anything changes. */
143 cleanup_control_flow_bb (basic_block bb)
145 gimple_stmt_iterator gsi;
149 /* If the last statement of the block could throw and now cannot,
150 we need to prune cfg. */
151 retval |= gimple_purge_dead_eh_edges (bb);
153 gsi = gsi_last_bb (bb);
157 stmt = gsi_stmt (gsi);
159 if (gimple_code (stmt) == GIMPLE_COND
160 || gimple_code (stmt) == GIMPLE_SWITCH)
161 retval |= cleanup_control_expr_graph (bb, gsi);
162 else if (gimple_code (stmt) == GIMPLE_GOTO
163 && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
164 && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0))
167 /* If we had a computed goto which has a compile-time determinable
168 destination, then we can eliminate the goto. */
172 basic_block target_block;
174 /* First look at all the outgoing edges. Delete any outgoing
175 edges which do not go to the right block. For the one
176 edge which goes to the right block, fix up its flags. */
177 label = TREE_OPERAND (gimple_goto_dest (stmt), 0);
178 target_block = label_to_block (label);
179 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
181 if (e->dest != target_block)
182 remove_edge_and_dominated_blocks (e);
185 /* Turn off the EDGE_ABNORMAL flag. */
186 e->flags &= ~EDGE_ABNORMAL;
188 /* And set EDGE_FALLTHRU. */
189 e->flags |= EDGE_FALLTHRU;
194 bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
195 bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index);
197 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
198 relevant information we need. */
199 gsi_remove (&gsi, true);
203 /* Check for indirect calls that have been turned into
205 else if (is_gimple_call (stmt)
206 && gimple_call_noreturn_p (stmt)
207 && remove_fallthru_edge (bb->succs))
213 /* Return true if basic block BB does nothing except pass control
214 flow to another block and that we can safely insert a label at
215 the start of the successor block.
217 As a precondition, we require that BB be not equal to
221 tree_forwarder_block_p (basic_block bb, bool phi_wanted)
223 gimple_stmt_iterator gsi;
225 /* BB must have a single outgoing edge. */
226 if (single_succ_p (bb) != 1
227 /* If PHI_WANTED is false, BB must not have any PHI nodes.
228 Otherwise, BB must have PHI nodes. */
229 || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
230 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
231 || single_succ (bb) == EXIT_BLOCK_PTR
232 /* Nor should this be an infinite loop. */
233 || single_succ (bb) == bb
234 /* BB may not have an abnormal outgoing edge. */
235 || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
239 gcc_assert (bb != ENTRY_BLOCK_PTR);
242 /* Now walk through the statements backward. We can ignore labels,
243 anything else means this is not a forwarder block. */
244 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
246 gimple stmt = gsi_stmt (gsi);
248 switch (gimple_code (stmt))
251 if (DECL_NONLOCAL (gimple_label_label (stmt)))
255 /* ??? For now, hope there's a corresponding debug
256 assignment at the destination. */
265 if (find_edge (ENTRY_BLOCK_PTR, bb))
271 /* Protect loop latches, headers and preheaders. */
272 if (bb->loop_father->header == bb)
274 dest = EDGE_SUCC (bb, 0)->dest;
276 if (dest->loop_father->header == dest)
282 /* Return true if BB has at least one abnormal incoming edge. */
285 has_abnormal_incoming_edge_p (basic_block bb)
290 FOR_EACH_EDGE (e, ei, bb->preds)
291 if (e->flags & EDGE_ABNORMAL)
297 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
298 those alternatives are equal in each of the PHI nodes, then return
299 true, else return false. */
302 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
304 int n1 = e1->dest_idx;
305 int n2 = e2->dest_idx;
306 gimple_stmt_iterator gsi;
308 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
310 gimple phi = gsi_stmt (gsi);
311 tree val1 = gimple_phi_arg_def (phi, n1);
312 tree val2 = gimple_phi_arg_def (phi, n2);
314 gcc_assert (val1 != NULL_TREE);
315 gcc_assert (val2 != NULL_TREE);
317 if (!operand_equal_for_phi_arg_p (val1, val2))
324 /* Removes forwarder block BB. Returns false if this failed. */
327 remove_forwarder_block (basic_block bb)
329 edge succ = single_succ_edge (bb), e, s;
330 basic_block dest = succ->dest;
333 gimple_stmt_iterator gsi, gsi_to;
334 bool seen_abnormal_edge = false;
336 /* We check for infinite loops already in tree_forwarder_block_p.
337 However it may happen that the infinite loop is created
338 afterwards due to removal of forwarders. */
342 /* If the destination block consists of a nonlocal label, do not merge
344 label = first_stmt (dest);
346 && gimple_code (label) == GIMPLE_LABEL
347 && DECL_NONLOCAL (gimple_label_label (label)))
350 /* If there is an abnormal edge to basic block BB, but not into
351 dest, problems might occur during removal of the phi node at out
352 of ssa due to overlapping live ranges of registers.
354 If there is an abnormal edge in DEST, the problems would occur
355 anyway since cleanup_dead_labels would then merge the labels for
356 two different eh regions, and rest of exception handling code
359 So if there is an abnormal edge to BB, proceed only if there is
360 no abnormal edge to DEST and there are no phi nodes in DEST. */
361 if (has_abnormal_incoming_edge_p (bb))
363 seen_abnormal_edge = true;
365 if (has_abnormal_incoming_edge_p (dest)
366 || !gimple_seq_empty_p (phi_nodes (dest)))
370 /* If there are phi nodes in DEST, and some of the blocks that are
371 predecessors of BB are also predecessors of DEST, check that the
372 phi node arguments match. */
373 if (!gimple_seq_empty_p (phi_nodes (dest)))
375 FOR_EACH_EDGE (e, ei, bb->preds)
377 s = find_edge (e->src, dest);
381 if (!phi_alternatives_equal (dest, succ, s))
386 /* Redirect the edges. */
387 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
389 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
391 if (e->flags & EDGE_ABNORMAL)
393 /* If there is an abnormal edge, redirect it anyway, and
394 move the labels to the new block to make it legal. */
395 s = redirect_edge_succ_nodup (e, dest);
398 s = redirect_edge_and_branch (e, dest);
402 /* Create arguments for the phi nodes, since the edge was not
404 for (gsi = gsi_start_phis (dest);
408 gimple phi = gsi_stmt (gsi);
409 source_location l = gimple_phi_arg_location_from_edge (phi, succ);
410 add_phi_arg (phi, gimple_phi_arg_def (phi, succ->dest_idx), s, l);
415 if (seen_abnormal_edge)
417 /* Move the labels to the new block, so that the redirection of
418 the abnormal edges works. */
419 gsi_to = gsi_start_bb (dest);
420 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
422 label = gsi_stmt (gsi);
423 gcc_assert (gimple_code (label) == GIMPLE_LABEL
424 || is_gimple_debug (label));
425 gsi_remove (&gsi, false);
426 gsi_insert_before (&gsi_to, label, GSI_SAME_STMT);
430 bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
432 /* Update the dominators. */
433 if (dom_info_available_p (CDI_DOMINATORS))
435 basic_block dom, dombb, domdest;
437 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
438 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
441 /* Shortcut to avoid calling (relatively expensive)
442 nearest_common_dominator unless necessary. */
446 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
448 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
451 /* And kill the forwarder block. */
452 delete_basic_block (bb);
457 /* Split basic blocks on calls in the middle of a basic block that are now
458 known not to return, and remove the unreachable code. */
461 split_bbs_on_noreturn_calls (void)
463 bool changed = false;
467 /* Detect cases where a mid-block call is now known not to return. */
469 while (VEC_length (gimple, MODIFIED_NORETURN_CALLS (cfun)))
471 stmt = VEC_pop (gimple, MODIFIED_NORETURN_CALLS (cfun));
472 bb = gimple_bb (stmt);
473 /* BB might be deleted at this point, so verify first
474 BB is present in the cfg. */
476 || bb->index < NUM_FIXED_BLOCKS
477 || bb->index >= n_basic_blocks
478 || BASIC_BLOCK (bb->index) != bb
479 || last_stmt (bb) == stmt
480 || !gimple_call_noreturn_p (stmt))
484 split_block (bb, stmt);
485 remove_fallthru_edge (bb->succs);
491 /* If GIMPLE_OMP_RETURN in basic block BB is unreachable, remove it. */
494 cleanup_omp_return (basic_block bb)
496 gimple stmt = last_stmt (bb);
497 basic_block control_bb;
500 || gimple_code (stmt) != GIMPLE_OMP_RETURN
501 || !single_pred_p (bb))
504 control_bb = single_pred (bb);
505 stmt = last_stmt (control_bb);
507 if (gimple_code (stmt) != GIMPLE_OMP_SECTIONS_SWITCH)
510 /* The block with the control statement normally has two entry edges -- one
511 from entry, one from continue. If continue is removed, return is
512 unreachable, so we remove it here as well. */
513 if (EDGE_COUNT (control_bb->preds) == 2)
516 gcc_assert (EDGE_COUNT (control_bb->preds) == 1);
517 remove_edge_and_dominated_blocks (single_pred_edge (bb));
521 /* Tries to cleanup cfg in basic block BB. Returns true if anything
525 cleanup_tree_cfg_bb (basic_block bb)
529 if (cleanup_omp_return (bb))
532 retval = cleanup_control_flow_bb (bb);
534 /* Forwarder blocks can carry line number information which is
535 useful when debugging, so we only clean them up when
538 && tree_forwarder_block_p (bb, false)
539 && remove_forwarder_block (bb))
542 /* Merging the blocks may create new opportunities for folding
543 conditional branches (due to the elimination of single-valued PHI
545 if (single_succ_p (bb)
546 && can_merge_blocks_p (bb, single_succ (bb)))
548 merge_blocks (bb, single_succ (bb));
555 /* Iterate the cfg cleanups, while anything changes. */
558 cleanup_tree_cfg_1 (void)
564 retval |= split_bbs_on_noreturn_calls ();
566 /* Prepare the worklists of altered blocks. */
567 cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL);
569 /* During forwarder block cleanup, we may redirect edges out of
570 SWITCH_EXPRs, which can get expensive. So we want to enable
571 recording of edge to CASE_LABEL_EXPR. */
572 start_recording_case_labels ();
574 /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB,
575 since the basic blocks may get removed. */
576 n = last_basic_block;
577 for (i = NUM_FIXED_BLOCKS; i < n; i++)
579 bb = BASIC_BLOCK (i);
581 retval |= cleanup_tree_cfg_bb (bb);
584 /* Now process the altered blocks, as long as any are available. */
585 while (!bitmap_empty_p (cfgcleanup_altered_bbs))
587 i = bitmap_first_set_bit (cfgcleanup_altered_bbs);
588 bitmap_clear_bit (cfgcleanup_altered_bbs, i);
589 if (i < NUM_FIXED_BLOCKS)
592 bb = BASIC_BLOCK (i);
596 retval |= cleanup_tree_cfg_bb (bb);
598 /* Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn
600 retval |= split_bbs_on_noreturn_calls ();
603 end_recording_case_labels ();
604 BITMAP_FREE (cfgcleanup_altered_bbs);
609 /* Remove unreachable blocks and other miscellaneous clean up work.
610 Return true if the flowgraph was modified, false otherwise. */
613 cleanup_tree_cfg_noloop (void)
617 timevar_push (TV_TREE_CLEANUP_CFG);
619 /* Iterate until there are no more cleanups left to do. If any
620 iteration changed the flowgraph, set CHANGED to true.
622 If dominance information is available, there cannot be any unreachable
624 if (!dom_info_available_p (CDI_DOMINATORS))
626 changed = delete_unreachable_blocks ();
627 calculate_dominance_info (CDI_DOMINATORS);
631 #ifdef ENABLE_CHECKING
632 verify_dominators (CDI_DOMINATORS);
637 changed |= cleanup_tree_cfg_1 ();
639 gcc_assert (dom_info_available_p (CDI_DOMINATORS));
642 #ifdef ENABLE_CHECKING
646 timevar_pop (TV_TREE_CLEANUP_CFG);
648 if (changed && current_loops)
649 loops_state_set (LOOPS_NEED_FIXUP);
654 /* Repairs loop structures. */
657 repair_loop_structures (void)
659 bitmap changed_bbs = BITMAP_ALLOC (NULL);
660 fix_loop_structure (changed_bbs);
662 /* This usually does nothing. But sometimes parts of cfg that originally
663 were inside a loop get out of it due to edge removal (since they
664 become unreachable by back edges from latch). */
665 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
666 rewrite_into_loop_closed_ssa (changed_bbs, TODO_update_ssa);
668 BITMAP_FREE (changed_bbs);
670 #ifdef ENABLE_CHECKING
671 verify_loop_structure ();
675 loops_state_clear (LOOPS_NEED_FIXUP);
678 /* Cleanup cfg and repair loop structures. */
681 cleanup_tree_cfg (void)
683 bool changed = cleanup_tree_cfg_noloop ();
685 if (current_loops != NULL
686 && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
687 repair_loop_structures ();
692 /* Merge the PHI nodes at BB into those at BB's sole successor. */
695 remove_forwarder_block_with_phi (basic_block bb)
697 edge succ = single_succ_edge (bb);
698 basic_block dest = succ->dest;
700 basic_block dombb, domdest, dom;
702 /* We check for infinite loops already in tree_forwarder_block_p.
703 However it may happen that the infinite loop is created
704 afterwards due to removal of forwarders. */
708 /* If the destination block consists of a nonlocal label, do not
710 label = first_stmt (dest);
712 && gimple_code (label) == GIMPLE_LABEL
713 && DECL_NONLOCAL (gimple_label_label (label)))
716 /* Redirect each incoming edge to BB to DEST. */
717 while (EDGE_COUNT (bb->preds) > 0)
719 edge e = EDGE_PRED (bb, 0), s;
720 gimple_stmt_iterator gsi;
722 s = find_edge (e->src, dest);
725 /* We already have an edge S from E->src to DEST. If S and
726 E->dest's sole successor edge have the same PHI arguments
727 at DEST, redirect S to DEST. */
728 if (phi_alternatives_equal (dest, s, succ))
730 e = redirect_edge_and_branch (e, dest);
731 redirect_edge_var_map_clear (e);
735 /* PHI arguments are different. Create a forwarder block by
736 splitting E so that we can merge PHI arguments on E to
738 e = single_succ_edge (split_edge (e));
741 s = redirect_edge_and_branch (e, dest);
743 /* redirect_edge_and_branch must not create a new edge. */
746 /* Add to the PHI nodes at DEST each PHI argument removed at the
748 for (gsi = gsi_start_phis (dest);
752 gimple phi = gsi_stmt (gsi);
753 tree def = gimple_phi_arg_def (phi, succ->dest_idx);
754 source_location locus = gimple_phi_arg_location_from_edge (phi, succ);
756 if (TREE_CODE (def) == SSA_NAME)
758 edge_var_map_vector head;
762 /* If DEF is one of the results of PHI nodes removed during
763 redirection, replace it with the PHI argument that used
765 head = redirect_edge_var_map_vector (e);
766 for (i = 0; VEC_iterate (edge_var_map, head, i, vm); ++i)
768 tree old_arg = redirect_edge_var_map_result (vm);
769 tree new_arg = redirect_edge_var_map_def (vm);
774 locus = redirect_edge_var_map_location (vm);
780 add_phi_arg (phi, def, s, locus);
783 redirect_edge_var_map_clear (e);
786 /* Update the dominators. */
787 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
788 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
791 /* Shortcut to avoid calling (relatively expensive)
792 nearest_common_dominator unless necessary. */
796 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
798 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
800 /* Remove BB since all of BB's incoming edges have been redirected
802 delete_basic_block (bb);
805 /* This pass merges PHI nodes if one feeds into another. For example,
806 suppose we have the following:
813 # tem_6 = PHI <tem_17(8), tem_23(7)>;
816 # tem_3 = PHI <tem_6(9), tem_2(5)>;
819 Then we merge the first PHI node into the second one like so:
826 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
831 merge_phi_nodes (void)
833 basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks);
834 basic_block *current = worklist;
837 calculate_dominance_info (CDI_DOMINATORS);
839 /* Find all PHI nodes that we may be able to merge. */
844 /* Look for a forwarder block with PHI nodes. */
845 if (!tree_forwarder_block_p (bb, true))
848 dest = single_succ (bb);
850 /* We have to feed into another basic block with PHI
852 if (!phi_nodes (dest)
853 /* We don't want to deal with a basic block with
855 || has_abnormal_incoming_edge_p (bb))
858 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
860 /* If BB does not dominate DEST, then the PHI nodes at
861 DEST must be the only users of the results of the PHI
867 gimple_stmt_iterator gsi;
868 unsigned int dest_idx = single_succ_edge (bb)->dest_idx;
870 /* BB dominates DEST. There may be many users of the PHI
871 nodes in BB. However, there is still a trivial case we
872 can handle. If the result of every PHI in BB is used
873 only by a PHI in DEST, then we can trivially merge the
874 PHI nodes from BB into DEST. */
875 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
878 gimple phi = gsi_stmt (gsi);
879 tree result = gimple_phi_result (phi);
880 use_operand_p imm_use;
883 /* If the PHI's result is never used, then we can just
885 if (has_zero_uses (result))
888 /* Get the single use of the result of this PHI node. */
889 if (!single_imm_use (result, &imm_use, &use_stmt)
890 || gimple_code (use_stmt) != GIMPLE_PHI
891 || gimple_bb (use_stmt) != dest
892 || gimple_phi_arg_def (use_stmt, dest_idx) != result)
896 /* If the loop above iterated through all the PHI nodes
897 in BB, then we can merge the PHIs from BB into DEST. */
903 /* Now let's drain WORKLIST. */
904 while (current != worklist)
907 remove_forwarder_block_with_phi (bb);
915 gate_merge_phi (void)
920 struct gimple_opt_pass pass_merge_phi =
924 "mergephi", /* name */
925 gate_merge_phi, /* gate */
926 merge_phi_nodes, /* execute */
929 0, /* static_pass_number */
930 TV_TREE_MERGE_PHI, /* tv_id */
931 PROP_cfg | PROP_ssa, /* properties_required */
932 0, /* properties_provided */
933 0, /* properties_destroyed */
934 0, /* todo_flags_start */
935 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */