1 /* CFG cleanup for trees.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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)
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.
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/>. */
22 #include "coretypes.h"
26 #include "basic-block.h"
27 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
34 #include "gimple-expr.h"
38 #include "gimple-iterator.h"
39 #include "gimple-ssa.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"
49 #include "tree-pass.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-scalar-evolution.h"
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
;
65 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
68 remove_fallthru_edge (vec
<edge
, va_gc
> *ev
)
73 FOR_EACH_EDGE (e
, ei
, ev
)
74 if ((e
->flags
& EDGE_FALLTHRU
) != 0)
76 if (e
->flags
& EDGE_COMPLEX
)
77 e
->flags
&= ~EDGE_FALLTHRU
;
79 remove_edge_and_dominated_blocks (e
);
86 /* Disconnect an unreachable block in the control expression starting
90 cleanup_control_expr_graph (basic_block bb
, gimple_stmt_iterator gsi
)
94 gimple stmt
= gsi_stmt (gsi
);
97 if (!single_succ_p (bb
))
104 fold_defer_overflow_warnings ();
105 loc
= gimple_location (stmt
);
106 switch (gimple_code (stmt
))
109 val
= fold_binary_loc (loc
, gimple_cond_code (stmt
),
111 gimple_cond_lhs (stmt
),
112 gimple_cond_rhs (stmt
));
116 val
= gimple_switch_index (stmt
);
122 taken_edge
= find_taken_edge (bb
, val
);
125 fold_undefer_and_ignore_overflow_warnings ();
129 /* Remove all the edges except the one that is always executed. */
131 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
137 fold_undefer_overflow_warnings
138 (true, stmt
, WARN_STRICT_OVERFLOW_CONDITIONAL
);
142 taken_edge
->probability
+= e
->probability
;
143 taken_edge
->count
+= e
->count
;
144 remove_edge_and_dominated_blocks (e
);
151 fold_undefer_and_ignore_overflow_warnings ();
152 if (taken_edge
->probability
> REG_BR_PROB_BASE
)
153 taken_edge
->probability
= REG_BR_PROB_BASE
;
156 taken_edge
= single_succ_edge (bb
);
158 bitmap_set_bit (cfgcleanup_altered_bbs
, bb
->index
);
159 gsi_remove (&gsi
, true);
160 taken_edge
->flags
= EDGE_FALLTHRU
;
165 /* Cleanup the GF_CALL_CTRL_ALTERING flag according to
166 to updated gimple_call_flags. */
169 cleanup_call_ctrl_altering_flag (gimple bb_end
)
171 if (!is_gimple_call (bb_end
)
172 || !gimple_call_ctrl_altering_p (bb_end
))
175 int flags
= gimple_call_flags (bb_end
);
176 if (((flags
& (ECF_CONST
| ECF_PURE
))
177 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
178 || (flags
& ECF_LEAF
))
179 gimple_call_set_ctrl_altering (bb_end
, false);
182 /* Try to remove superfluous control structures in basic block BB. Returns
183 true if anything changes. */
186 cleanup_control_flow_bb (basic_block bb
)
188 gimple_stmt_iterator gsi
;
192 /* If the last statement of the block could throw and now cannot,
193 we need to prune cfg. */
194 retval
|= gimple_purge_dead_eh_edges (bb
);
196 gsi
= gsi_last_bb (bb
);
200 stmt
= gsi_stmt (gsi
);
202 /* Try to cleanup ctrl altering flag for call which ends bb. */
203 cleanup_call_ctrl_altering_flag (stmt
);
205 if (gimple_code (stmt
) == GIMPLE_COND
206 || gimple_code (stmt
) == GIMPLE_SWITCH
)
207 retval
|= cleanup_control_expr_graph (bb
, gsi
);
208 else if (gimple_code (stmt
) == GIMPLE_GOTO
209 && TREE_CODE (gimple_goto_dest (stmt
)) == ADDR_EXPR
210 && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt
), 0))
213 /* If we had a computed goto which has a compile-time determinable
214 destination, then we can eliminate the goto. */
218 basic_block target_block
;
220 /* First look at all the outgoing edges. Delete any outgoing
221 edges which do not go to the right block. For the one
222 edge which goes to the right block, fix up its flags. */
223 label
= TREE_OPERAND (gimple_goto_dest (stmt
), 0);
224 target_block
= label_to_block (label
);
225 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
227 if (e
->dest
!= target_block
)
228 remove_edge_and_dominated_blocks (e
);
231 /* Turn off the EDGE_ABNORMAL flag. */
232 e
->flags
&= ~EDGE_ABNORMAL
;
234 /* And set EDGE_FALLTHRU. */
235 e
->flags
|= EDGE_FALLTHRU
;
240 bitmap_set_bit (cfgcleanup_altered_bbs
, bb
->index
);
241 bitmap_set_bit (cfgcleanup_altered_bbs
, target_block
->index
);
243 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
244 relevant information we need. */
245 gsi_remove (&gsi
, true);
249 /* Check for indirect calls that have been turned into
251 else if (is_gimple_call (stmt
)
252 && gimple_call_noreturn_p (stmt
)
253 && remove_fallthru_edge (bb
->succs
))
259 /* Return true if basic block BB does nothing except pass control
260 flow to another block and that we can safely insert a label at
261 the start of the successor block.
263 As a precondition, we require that BB be not equal to
267 tree_forwarder_block_p (basic_block bb
, bool phi_wanted
)
269 gimple_stmt_iterator gsi
;
272 /* BB must have a single outgoing edge. */
273 if (single_succ_p (bb
) != 1
274 /* If PHI_WANTED is false, BB must not have any PHI nodes.
275 Otherwise, BB must have PHI nodes. */
276 || gimple_seq_empty_p (phi_nodes (bb
)) == phi_wanted
277 /* BB may not be a predecessor of the exit block. */
278 || single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
279 /* Nor should this be an infinite loop. */
280 || single_succ (bb
) == bb
281 /* BB may not have an abnormal outgoing edge. */
282 || (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
))
285 gcc_checking_assert (bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
));
287 locus
= single_succ_edge (bb
)->goto_locus
;
289 /* There should not be an edge coming from entry, or an EH edge. */
294 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
295 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
) || (e
->flags
& EDGE_EH
))
297 /* If goto_locus of any of the edges differs, prevent removing
298 the forwarder block for -O0. */
299 else if (optimize
== 0 && e
->goto_locus
!= locus
)
303 /* Now walk through the statements backward. We can ignore labels,
304 anything else means this is not a forwarder block. */
305 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
307 gimple stmt
= gsi_stmt (gsi
);
309 switch (gimple_code (stmt
))
312 if (DECL_NONLOCAL (gimple_label_label (stmt
)))
314 if (optimize
== 0 && gimple_location (stmt
) != locus
)
318 /* ??? For now, hope there's a corresponding debug
319 assignment at the destination. */
331 /* Protect loop headers. */
332 if (bb
->loop_father
->header
== bb
)
335 dest
= EDGE_SUCC (bb
, 0)->dest
;
336 /* Protect loop preheaders and latches if requested. */
337 if (dest
->loop_father
->header
== dest
)
339 if (bb
->loop_father
== dest
->loop_father
)
341 if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES
))
343 /* If bb doesn't have a single predecessor we'd make this
344 loop have multiple latches. Don't do that if that
345 would in turn require disambiguating them. */
346 return (single_pred_p (bb
)
347 || loops_state_satisfies_p
348 (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
));
350 else if (bb
->loop_father
== loop_outer (dest
->loop_father
))
351 return !loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
);
352 /* Always preserve other edges into loop headers that are
353 not simple latches or preheaders. */
361 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
362 those alternatives are equal in each of the PHI nodes, then return
363 true, else return false. */
366 phi_alternatives_equal (basic_block dest
, edge e1
, edge e2
)
368 int n1
= e1
->dest_idx
;
369 int n2
= e2
->dest_idx
;
370 gimple_phi_iterator gsi
;
372 for (gsi
= gsi_start_phis (dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
374 gimple_phi phi
= gsi
.phi ();
375 tree val1
= gimple_phi_arg_def (phi
, n1
);
376 tree val2
= gimple_phi_arg_def (phi
, n2
);
378 gcc_assert (val1
!= NULL_TREE
);
379 gcc_assert (val2
!= NULL_TREE
);
381 if (!operand_equal_for_phi_arg_p (val1
, val2
))
388 /* Removes forwarder block BB. Returns false if this failed. */
391 remove_forwarder_block (basic_block bb
)
393 edge succ
= single_succ_edge (bb
), e
, s
;
394 basic_block dest
= succ
->dest
;
397 gimple_stmt_iterator gsi
, gsi_to
;
398 bool can_move_debug_stmts
;
400 /* We check for infinite loops already in tree_forwarder_block_p.
401 However it may happen that the infinite loop is created
402 afterwards due to removal of forwarders. */
406 /* If the destination block consists of a nonlocal label or is a
407 EH landing pad, do not merge it. */
408 label
= first_stmt (dest
);
410 && gimple_code (label
) == GIMPLE_LABEL
411 && (DECL_NONLOCAL (gimple_label_label (label
))
412 || EH_LANDING_PAD_NR (gimple_label_label (label
)) != 0))
415 /* If there is an abnormal edge to basic block BB, but not into
416 dest, problems might occur during removal of the phi node at out
417 of ssa due to overlapping live ranges of registers.
419 If there is an abnormal edge in DEST, the problems would occur
420 anyway since cleanup_dead_labels would then merge the labels for
421 two different eh regions, and rest of exception handling code
424 So if there is an abnormal edge to BB, proceed only if there is
425 no abnormal edge to DEST and there are no phi nodes in DEST. */
426 if (bb_has_abnormal_pred (bb
)
427 && (bb_has_abnormal_pred (dest
)
428 || !gimple_seq_empty_p (phi_nodes (dest
))))
431 /* If there are phi nodes in DEST, and some of the blocks that are
432 predecessors of BB are also predecessors of DEST, check that the
433 phi node arguments match. */
434 if (!gimple_seq_empty_p (phi_nodes (dest
)))
436 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
438 s
= find_edge (e
->src
, dest
);
442 if (!phi_alternatives_equal (dest
, succ
, s
))
447 can_move_debug_stmts
= MAY_HAVE_DEBUG_STMTS
&& single_pred_p (dest
);
449 basic_block pred
= NULL
;
450 if (single_pred_p (bb
))
451 pred
= single_pred (bb
);
453 /* Redirect the edges. */
454 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
456 bitmap_set_bit (cfgcleanup_altered_bbs
, e
->src
->index
);
458 if (e
->flags
& EDGE_ABNORMAL
)
460 /* If there is an abnormal edge, redirect it anyway, and
461 move the labels to the new block to make it legal. */
462 s
= redirect_edge_succ_nodup (e
, dest
);
465 s
= redirect_edge_and_branch (e
, dest
);
469 /* Create arguments for the phi nodes, since the edge was not
471 for (gsi
= gsi_start_phis (dest
);
475 gimple phi
= gsi_stmt (gsi
);
476 source_location l
= gimple_phi_arg_location_from_edge (phi
, succ
);
477 tree def
= gimple_phi_arg_def (phi
, succ
->dest_idx
);
478 add_phi_arg (phi
, unshare_expr (def
), s
, l
);
483 /* Move nonlocal labels and computed goto targets as well as user
484 defined labels and labels with an EH landing pad number to the
485 new block, so that the redirection of the abnormal edges works,
486 jump targets end up in a sane place and debug information for
487 labels is retained. */
488 gsi_to
= gsi_start_bb (dest
);
489 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
492 label
= gsi_stmt (gsi
);
493 if (is_gimple_debug (label
))
495 decl
= gimple_label_label (label
);
496 if (EH_LANDING_PAD_NR (decl
) != 0
497 || DECL_NONLOCAL (decl
)
498 || FORCED_LABEL (decl
)
499 || !DECL_ARTIFICIAL (decl
))
501 gsi_remove (&gsi
, false);
502 gsi_insert_before (&gsi_to
, label
, GSI_SAME_STMT
);
508 /* Move debug statements if the destination has a single predecessor. */
509 if (can_move_debug_stmts
)
511 gsi_to
= gsi_after_labels (dest
);
512 for (gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
); )
514 gimple debug
= gsi_stmt (gsi
);
515 if (!is_gimple_debug (debug
))
517 gsi_remove (&gsi
, false);
518 gsi_insert_before (&gsi_to
, debug
, GSI_SAME_STMT
);
522 bitmap_set_bit (cfgcleanup_altered_bbs
, dest
->index
);
524 /* Update the dominators. */
525 if (dom_info_available_p (CDI_DOMINATORS
))
527 basic_block dom
, dombb
, domdest
;
529 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
530 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
533 /* Shortcut to avoid calling (relatively expensive)
534 nearest_common_dominator unless necessary. */
538 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
540 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
543 /* Adjust latch infomation of BB's parent loop as otherwise
544 the cfg hook has a hard time not to kill the loop. */
545 if (current_loops
&& bb
->loop_father
->latch
== bb
)
546 bb
->loop_father
->latch
= pred
;
548 /* And kill the forwarder block. */
549 delete_basic_block (bb
);
554 /* STMT is a call that has been discovered noreturn. Fixup the CFG
555 and remove LHS. Return true if something changed. */
558 fixup_noreturn_call (gimple stmt
)
560 basic_block bb
= gimple_bb (stmt
);
561 bool changed
= false;
563 if (gimple_call_builtin_p (stmt
, BUILT_IN_RETURN
))
566 /* First split basic block if stmt is not last. */
567 if (stmt
!= gsi_stmt (gsi_last_bb (bb
)))
569 if (stmt
== gsi_stmt (gsi_last_nondebug_bb (bb
)))
571 /* Don't split if there are only debug stmts
572 after stmt, that can result in -fcompare-debug
573 failures. Remove the debug stmts instead,
574 they should be all unreachable anyway. */
575 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
576 for (gsi_next (&gsi
); !gsi_end_p (gsi
); )
577 gsi_remove (&gsi
, true);
580 split_block (bb
, stmt
);
583 changed
|= remove_fallthru_edge (bb
->succs
);
585 /* If there is LHS, remove it. */
586 if (gimple_call_lhs (stmt
))
588 tree op
= gimple_call_lhs (stmt
);
589 gimple_call_set_lhs (stmt
, NULL_TREE
);
591 /* We need to remove SSA name to avoid checking errors.
592 All uses are dominated by the noreturn and thus will
593 be removed afterwards.
594 We proactively remove affected non-PHI statements to avoid
595 fixup_cfg from trying to update them and crashing. */
596 if (TREE_CODE (op
) == SSA_NAME
)
599 imm_use_iterator iter
;
602 unsigned int bb_index
;
604 bitmap blocks
= BITMAP_ALLOC (NULL
);
606 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, op
)
608 if (gimple_code (use_stmt
) != GIMPLE_PHI
)
609 bitmap_set_bit (blocks
, gimple_bb (use_stmt
)->index
);
611 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
612 SET_USE (use_p
, error_mark_node
);
614 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, bb_index
, bi
)
615 delete_basic_block (BASIC_BLOCK_FOR_FN (cfun
, bb_index
));
616 BITMAP_FREE (blocks
);
617 release_ssa_name (op
);
626 /* Split basic blocks on calls in the middle of a basic block that are now
627 known not to return, and remove the unreachable code. */
630 split_bb_on_noreturn_calls (basic_block bb
)
632 bool changed
= false;
633 gimple_stmt_iterator gsi
;
635 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
637 gimple stmt
= gsi_stmt (gsi
);
639 if (!is_gimple_call (stmt
))
642 if (gimple_call_noreturn_p (stmt
))
643 changed
|= fixup_noreturn_call (stmt
);
647 bitmap_set_bit (cfgcleanup_altered_bbs
, bb
->index
);
651 /* Tries to cleanup cfg in basic block BB. Returns true if anything
655 cleanup_tree_cfg_bb (basic_block bb
)
657 bool retval
= cleanup_control_flow_bb (bb
);
659 if (tree_forwarder_block_p (bb
, false)
660 && remove_forwarder_block (bb
))
663 /* Merging the blocks may create new opportunities for folding
664 conditional branches (due to the elimination of single-valued PHI
666 if (single_succ_p (bb
)
667 && can_merge_blocks_p (bb
, single_succ (bb
)))
669 merge_blocks (bb
, single_succ (bb
));
676 /* Iterate the cfg cleanups, while anything changes. */
679 cleanup_tree_cfg_1 (void)
685 /* Prepare the worklists of altered blocks. */
686 cfgcleanup_altered_bbs
= BITMAP_ALLOC (NULL
);
688 /* During forwarder block cleanup, we may redirect edges out of
689 SWITCH_EXPRs, which can get expensive. So we want to enable
690 recording of edge to CASE_LABEL_EXPR. */
691 start_recording_case_labels ();
693 /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB_FN,
694 since the basic blocks may get removed. */
695 n
= last_basic_block_for_fn (cfun
);
696 for (i
= NUM_FIXED_BLOCKS
; i
< n
; i
++)
698 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
701 retval
|= cleanup_tree_cfg_bb (bb
);
702 retval
|= split_bb_on_noreturn_calls (bb
);
706 /* Now process the altered blocks, as long as any are available. */
707 while (!bitmap_empty_p (cfgcleanup_altered_bbs
))
709 i
= bitmap_first_set_bit (cfgcleanup_altered_bbs
);
710 bitmap_clear_bit (cfgcleanup_altered_bbs
, i
);
711 if (i
< NUM_FIXED_BLOCKS
)
714 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
718 retval
|= cleanup_tree_cfg_bb (bb
);
720 /* Rerun split_bb_on_noreturn_calls, in case we have altered any noreturn
722 retval
|= split_bb_on_noreturn_calls (bb
);
725 end_recording_case_labels ();
726 BITMAP_FREE (cfgcleanup_altered_bbs
);
731 /* Remove unreachable blocks and other miscellaneous clean up work.
732 Return true if the flowgraph was modified, false otherwise. */
735 cleanup_tree_cfg_noloop (void)
739 timevar_push (TV_TREE_CLEANUP_CFG
);
741 /* Iterate until there are no more cleanups left to do. If any
742 iteration changed the flowgraph, set CHANGED to true.
744 If dominance information is available, there cannot be any unreachable
746 if (!dom_info_available_p (CDI_DOMINATORS
))
748 changed
= delete_unreachable_blocks ();
749 calculate_dominance_info (CDI_DOMINATORS
);
753 #ifdef ENABLE_CHECKING
754 verify_dominators (CDI_DOMINATORS
);
759 changed
|= cleanup_tree_cfg_1 ();
761 gcc_assert (dom_info_available_p (CDI_DOMINATORS
));
764 #ifdef ENABLE_CHECKING
768 timevar_pop (TV_TREE_CLEANUP_CFG
);
770 if (changed
&& current_loops
)
771 loops_state_set (LOOPS_NEED_FIXUP
);
776 /* Repairs loop structures. */
779 repair_loop_structures (void)
782 unsigned n_new_loops
;
784 calculate_dominance_info (CDI_DOMINATORS
);
786 timevar_push (TV_REPAIR_LOOPS
);
787 changed_bbs
= BITMAP_ALLOC (NULL
);
788 n_new_loops
= fix_loop_structure (changed_bbs
);
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
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. */
795 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
))
796 rewrite_into_loop_closed_ssa (n_new_loops
? NULL
: changed_bbs
,
799 BITMAP_FREE (changed_bbs
);
801 #ifdef ENABLE_CHECKING
802 verify_loop_structure ();
806 timevar_pop (TV_REPAIR_LOOPS
);
809 /* Cleanup cfg and repair loop structures. */
812 cleanup_tree_cfg (void)
814 bool changed
= cleanup_tree_cfg_noloop ();
816 if (current_loops
!= NULL
817 && loops_state_satisfies_p (LOOPS_NEED_FIXUP
))
818 repair_loop_structures ();
823 /* Tries to merge the PHI nodes at BB into those at BB's sole successor.
824 Returns true if successful. */
827 remove_forwarder_block_with_phi (basic_block bb
)
829 edge succ
= single_succ_edge (bb
);
830 basic_block dest
= succ
->dest
;
832 basic_block dombb
, domdest
, dom
;
834 /* We check for infinite loops already in tree_forwarder_block_p.
835 However it may happen that the infinite loop is created
836 afterwards due to removal of forwarders. */
840 /* If the destination block consists of a nonlocal label, do not
842 label
= first_stmt (dest
);
844 && gimple_code (label
) == GIMPLE_LABEL
845 && DECL_NONLOCAL (gimple_label_label (label
)))
848 /* Record BB's single pred in case we need to update the father
849 loop's latch information later. */
850 basic_block pred
= NULL
;
851 if (single_pred_p (bb
))
852 pred
= single_pred (bb
);
854 /* Redirect each incoming edge to BB to DEST. */
855 while (EDGE_COUNT (bb
->preds
) > 0)
857 edge e
= EDGE_PRED (bb
, 0), s
;
858 gimple_phi_iterator gsi
;
860 s
= find_edge (e
->src
, dest
);
863 /* We already have an edge S from E->src to DEST. If S and
864 E->dest's sole successor edge have the same PHI arguments
865 at DEST, redirect S to DEST. */
866 if (phi_alternatives_equal (dest
, s
, succ
))
868 e
= redirect_edge_and_branch (e
, dest
);
869 redirect_edge_var_map_clear (e
);
873 /* PHI arguments are different. Create a forwarder block by
874 splitting E so that we can merge PHI arguments on E to
876 e
= single_succ_edge (split_edge (e
));
879 s
= redirect_edge_and_branch (e
, dest
);
881 /* redirect_edge_and_branch must not create a new edge. */
884 /* Add to the PHI nodes at DEST each PHI argument removed at the
886 for (gsi
= gsi_start_phis (dest
);
890 gimple_phi phi
= gsi
.phi ();
891 tree def
= gimple_phi_arg_def (phi
, succ
->dest_idx
);
892 source_location locus
= gimple_phi_arg_location_from_edge (phi
, succ
);
894 if (TREE_CODE (def
) == SSA_NAME
)
896 /* If DEF is one of the results of PHI nodes removed during
897 redirection, replace it with the PHI argument that used
899 vec
<edge_var_map
> *head
= redirect_edge_var_map_vector (e
);
900 size_t length
= head
? head
->length () : 0;
901 for (size_t i
= 0; i
< length
; i
++)
903 edge_var_map
*vm
= &(*head
)[i
];
904 tree old_arg
= redirect_edge_var_map_result (vm
);
905 tree new_arg
= redirect_edge_var_map_def (vm
);
910 locus
= redirect_edge_var_map_location (vm
);
916 add_phi_arg (phi
, def
, s
, locus
);
919 redirect_edge_var_map_clear (e
);
922 /* Update the dominators. */
923 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
924 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
927 /* Shortcut to avoid calling (relatively expensive)
928 nearest_common_dominator unless necessary. */
932 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
934 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
936 /* Adjust latch infomation of BB's parent loop as otherwise
937 the cfg hook has a hard time not to kill the loop. */
938 if (current_loops
&& bb
->loop_father
->latch
== bb
)
939 bb
->loop_father
->latch
= pred
;
941 /* Remove BB since all of BB's incoming edges have been redirected
943 delete_basic_block (bb
);
948 /* This pass merges PHI nodes if one feeds into another. For example,
949 suppose we have the following:
956 # tem_6 = PHI <tem_17(8), tem_23(7)>;
959 # tem_3 = PHI <tem_6(9), tem_2(5)>;
962 Then we merge the first PHI node into the second one like so:
969 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
975 const pass_data pass_data_merge_phi
=
977 GIMPLE_PASS
, /* type */
978 "mergephi", /* name */
979 OPTGROUP_NONE
, /* optinfo_flags */
980 TV_TREE_MERGE_PHI
, /* tv_id */
981 ( PROP_cfg
| PROP_ssa
), /* properties_required */
982 0, /* properties_provided */
983 0, /* properties_destroyed */
984 0, /* todo_flags_start */
985 0, /* todo_flags_finish */
988 class pass_merge_phi
: public gimple_opt_pass
991 pass_merge_phi (gcc::context
*ctxt
)
992 : gimple_opt_pass (pass_data_merge_phi
, ctxt
)
995 /* opt_pass methods: */
996 opt_pass
* clone () { return new pass_merge_phi (m_ctxt
); }
997 virtual unsigned int execute (function
*);
999 }; // class pass_merge_phi
1002 pass_merge_phi::execute (function
*fun
)
1004 basic_block
*worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (fun
));
1005 basic_block
*current
= worklist
;
1008 calculate_dominance_info (CDI_DOMINATORS
);
1010 /* Find all PHI nodes that we may be able to merge. */
1011 FOR_EACH_BB_FN (bb
, fun
)
1015 /* Look for a forwarder block with PHI nodes. */
1016 if (!tree_forwarder_block_p (bb
, true))
1019 dest
= single_succ (bb
);
1021 /* We have to feed into another basic block with PHI
1023 if (gimple_seq_empty_p (phi_nodes (dest
))
1024 /* We don't want to deal with a basic block with
1026 || bb_has_abnormal_pred (bb
))
1029 if (!dominated_by_p (CDI_DOMINATORS
, dest
, bb
))
1031 /* If BB does not dominate DEST, then the PHI nodes at
1032 DEST must be the only users of the results of the PHI
1038 gimple_phi_iterator gsi
;
1039 unsigned int dest_idx
= single_succ_edge (bb
)->dest_idx
;
1041 /* BB dominates DEST. There may be many users of the PHI
1042 nodes in BB. However, there is still a trivial case we
1043 can handle. If the result of every PHI in BB is used
1044 only by a PHI in DEST, then we can trivially merge the
1045 PHI nodes from BB into DEST. */
1046 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1049 gimple_phi phi
= gsi
.phi ();
1050 tree result
= gimple_phi_result (phi
);
1051 use_operand_p imm_use
;
1054 /* If the PHI's result is never used, then we can just
1056 if (has_zero_uses (result
))
1059 /* Get the single use of the result of this PHI node. */
1060 if (!single_imm_use (result
, &imm_use
, &use_stmt
)
1061 || gimple_code (use_stmt
) != GIMPLE_PHI
1062 || gimple_bb (use_stmt
) != dest
1063 || gimple_phi_arg_def (use_stmt
, dest_idx
) != result
)
1067 /* If the loop above iterated through all the PHI nodes
1068 in BB, then we can merge the PHIs from BB into DEST. */
1069 if (gsi_end_p (gsi
))
1074 /* Now let's drain WORKLIST. */
1075 bool changed
= false;
1076 while (current
!= worklist
)
1079 changed
|= remove_forwarder_block_with_phi (bb
);
1083 /* Removing forwarder blocks can cause formerly irreducible loops
1084 to become reducible if we merged two entry blocks. */
1087 loops_state_set (LOOPS_NEED_FIXUP
);
1095 make_pass_merge_phi (gcc::context
*ctxt
)
1097 return new pass_merge_phi (ctxt
);
1100 /* Pass: cleanup the CFG just before expanding trees to RTL.
1101 This is just a round of label cleanups and case node grouping
1102 because after the tree optimizers have run such cleanups may
1106 execute_cleanup_cfg_post_optimizing (void)
1108 unsigned int todo
= 0;
1109 if (cleanup_tree_cfg ())
1110 todo
|= TODO_update_ssa
;
1111 maybe_remove_unreachable_handlers ();
1112 cleanup_dead_labels ();
1113 group_case_labels ();
1114 if ((flag_compare_debug_opt
|| flag_compare_debug
)
1115 && flag_dump_final_insns
)
1117 FILE *final_output
= fopen (flag_dump_final_insns
, "a");
1121 error ("could not open final insn dump file %qs: %m",
1122 flag_dump_final_insns
);
1123 flag_dump_final_insns
= NULL
;
1127 int save_unnumbered
= flag_dump_unnumbered
;
1128 int save_noaddr
= flag_dump_noaddr
;
1130 flag_dump_noaddr
= flag_dump_unnumbered
= 1;
1131 fprintf (final_output
, "\n");
1132 dump_enumerated_decls (final_output
, dump_flags
| TDF_NOUID
);
1133 flag_dump_noaddr
= save_noaddr
;
1134 flag_dump_unnumbered
= save_unnumbered
;
1135 if (fclose (final_output
))
1137 error ("could not close final insn dump file %qs: %m",
1138 flag_dump_final_insns
);
1139 flag_dump_final_insns
= NULL
;
1148 const pass_data pass_data_cleanup_cfg_post_optimizing
=
1150 GIMPLE_PASS
, /* type */
1151 "optimized", /* name */
1152 OPTGROUP_NONE
, /* optinfo_flags */
1153 TV_TREE_CLEANUP_CFG
, /* tv_id */
1154 PROP_cfg
, /* properties_required */
1155 0, /* properties_provided */
1156 0, /* properties_destroyed */
1157 0, /* todo_flags_start */
1158 TODO_remove_unused_locals
, /* todo_flags_finish */
1161 class pass_cleanup_cfg_post_optimizing
: public gimple_opt_pass
1164 pass_cleanup_cfg_post_optimizing (gcc::context
*ctxt
)
1165 : gimple_opt_pass (pass_data_cleanup_cfg_post_optimizing
, ctxt
)
1168 /* opt_pass methods: */
1169 virtual unsigned int execute (function
*)
1171 return execute_cleanup_cfg_post_optimizing ();
1174 }; // class pass_cleanup_cfg_post_optimizing
1179 make_pass_cleanup_cfg_post_optimizing (gcc::context
*ctxt
)
1181 return new pass_cleanup_cfg_post_optimizing (ctxt
);