1 /* CFG cleanup for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
46 #include "cfglayout.h"
48 #include "tree-ssa-propagate.h"
49 #include "tree-scalar-evolution.h"
51 /* The set of blocks in that at least one of the following changes happened:
52 -- the statement at the end of the block was changed
53 -- the block was newly created
54 -- the set of the predecessors of the block changed
55 -- the set of the successors of the block changed
56 ??? Maybe we could track these changes separately, since they determine
57 what cleanups it makes sense to try on the block. */
58 bitmap cfgcleanup_altered_bbs
;
60 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
63 remove_fallthru_edge (VEC(edge
,gc
) *ev
)
68 FOR_EACH_EDGE (e
, ei
, ev
)
69 if ((e
->flags
& EDGE_FALLTHRU
) != 0)
71 remove_edge_and_dominated_blocks (e
);
77 /* Disconnect an unreachable block in the control expression starting
81 cleanup_control_expr_graph (basic_block bb
, block_stmt_iterator bsi
)
85 tree expr
= bsi_stmt (bsi
), val
;
87 if (!single_succ_p (bb
))
93 fold_defer_overflow_warnings ();
95 switch (TREE_CODE (expr
))
98 val
= fold (COND_EXPR_COND (expr
));
102 val
= fold (SWITCH_COND (expr
));
103 if (TREE_CODE (val
) != INTEGER_CST
)
105 fold_undefer_and_ignore_overflow_warnings ();
114 taken_edge
= find_taken_edge (bb
, val
);
117 fold_undefer_and_ignore_overflow_warnings ();
121 /* Remove all the edges except the one that is always executed. */
123 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
129 fold_undefer_overflow_warnings
130 (true, expr
, WARN_STRICT_OVERFLOW_CONDITIONAL
);
134 taken_edge
->probability
+= e
->probability
;
135 taken_edge
->count
+= e
->count
;
136 remove_edge_and_dominated_blocks (e
);
143 fold_undefer_and_ignore_overflow_warnings ();
144 if (taken_edge
->probability
> REG_BR_PROB_BASE
)
145 taken_edge
->probability
= REG_BR_PROB_BASE
;
148 taken_edge
= single_succ_edge (bb
);
150 bitmap_set_bit (cfgcleanup_altered_bbs
, bb
->index
);
151 bsi_remove (&bsi
, true);
152 taken_edge
->flags
= EDGE_FALLTHRU
;
157 /* Try to remove superfluous control structures in basic block BB. Returns
158 true if anything changes. */
161 cleanup_control_flow_bb (basic_block bb
)
163 block_stmt_iterator bsi
;
167 /* If the last statement of the block could throw and now cannot,
168 we need to prune cfg. */
169 retval
|= tree_purge_dead_eh_edges (bb
);
175 stmt
= bsi_stmt (bsi
);
177 if (TREE_CODE (stmt
) == COND_EXPR
178 || TREE_CODE (stmt
) == SWITCH_EXPR
)
179 retval
|= cleanup_control_expr_graph (bb
, bsi
);
180 /* If we had a computed goto which has a compile-time determinable
181 destination, then we can eliminate the goto. */
182 else if (TREE_CODE (stmt
) == GOTO_EXPR
183 && TREE_CODE (GOTO_DESTINATION (stmt
)) == ADDR_EXPR
184 && (TREE_CODE (TREE_OPERAND (GOTO_DESTINATION (stmt
), 0))
190 basic_block target_block
;
192 /* First look at all the outgoing edges. Delete any outgoing
193 edges which do not go to the right block. For the one
194 edge which goes to the right block, fix up its flags. */
195 label
= TREE_OPERAND (GOTO_DESTINATION (stmt
), 0);
196 target_block
= label_to_block (label
);
197 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
199 if (e
->dest
!= target_block
)
200 remove_edge_and_dominated_blocks (e
);
203 /* Turn off the EDGE_ABNORMAL flag. */
204 e
->flags
&= ~EDGE_ABNORMAL
;
206 /* And set EDGE_FALLTHRU. */
207 e
->flags
|= EDGE_FALLTHRU
;
212 bitmap_set_bit (cfgcleanup_altered_bbs
, bb
->index
);
213 bitmap_set_bit (cfgcleanup_altered_bbs
, target_block
->index
);
215 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
216 relevant information we need. */
217 bsi_remove (&bsi
, true);
221 /* Check for indirect calls that have been turned into
223 else if (noreturn_call_p (stmt
) && remove_fallthru_edge (bb
->succs
))
229 /* Return true if basic block BB does nothing except pass control
230 flow to another block and that we can safely insert a label at
231 the start of the successor block.
233 As a precondition, we require that BB be not equal to
237 tree_forwarder_block_p (basic_block bb
, bool phi_wanted
)
239 block_stmt_iterator bsi
;
244 /* BB must have a single outgoing edge. */
245 if (single_succ_p (bb
) != 1
246 /* If PHI_WANTED is false, BB must not have any PHI nodes.
247 Otherwise, BB must have PHI nodes. */
248 || (phi_nodes (bb
) != NULL_TREE
) != phi_wanted
249 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
250 || single_succ (bb
) == EXIT_BLOCK_PTR
251 /* Nor should this be an infinite loop. */
252 || single_succ (bb
) == bb
253 /* BB may not have an abnormal outgoing edge. */
254 || (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
))
258 gcc_assert (bb
!= ENTRY_BLOCK_PTR
);
261 /* Now walk through the statements backward. We can ignore labels,
262 anything else means this is not a forwarder block. */
263 for (bsi
= bsi_last (bb
); !bsi_end_p (bsi
); bsi_prev (&bsi
))
265 tree stmt
= bsi_stmt (bsi
);
267 switch (TREE_CODE (stmt
))
270 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
279 if (find_edge (ENTRY_BLOCK_PTR
, bb
))
285 /* Protect loop latches, headers and preheaders. */
286 if (bb
->loop_father
->header
== bb
)
288 dest
= EDGE_SUCC (bb
, 0)->dest
;
290 if (dest
->loop_father
->header
== dest
)
294 /* If we have an EH edge leaving this block, make sure that the
295 destination of this block has only one predecessor. This ensures
296 that we don't get into the situation where we try to remove two
297 forwarders that go to the same basic block but are handlers for
298 different EH regions. */
299 succ
= single_succ_edge (bb
);
301 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
303 if (e
->flags
& EDGE_EH
)
305 if (!single_pred_p (dest
))
313 /* Return true if BB has at least one abnormal incoming edge. */
316 has_abnormal_incoming_edge_p (basic_block bb
)
321 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
322 if (e
->flags
& EDGE_ABNORMAL
)
328 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
329 those alternatives are equal in each of the PHI nodes, then return
330 true, else return false. */
333 phi_alternatives_equal (basic_block dest
, edge e1
, edge e2
)
335 int n1
= e1
->dest_idx
;
336 int n2
= e2
->dest_idx
;
339 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
341 tree val1
= PHI_ARG_DEF (phi
, n1
);
342 tree val2
= PHI_ARG_DEF (phi
, n2
);
344 gcc_assert (val1
!= NULL_TREE
);
345 gcc_assert (val2
!= NULL_TREE
);
347 if (!operand_equal_for_phi_arg_p (val1
, val2
))
354 /* Removes forwarder block BB. Returns false if this failed. */
357 remove_forwarder_block (basic_block bb
)
359 edge succ
= single_succ_edge (bb
), e
, s
;
360 basic_block dest
= succ
->dest
;
364 block_stmt_iterator bsi
, bsi_to
;
365 bool seen_abnormal_edge
= false;
367 /* We check for infinite loops already in tree_forwarder_block_p.
368 However it may happen that the infinite loop is created
369 afterwards due to removal of forwarders. */
373 /* If the destination block consists of a nonlocal label, do not merge
375 label
= first_stmt (dest
);
377 && TREE_CODE (label
) == LABEL_EXPR
378 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label
)))
381 /* If there is an abnormal edge to basic block BB, but not into
382 dest, problems might occur during removal of the phi node at out
383 of ssa due to overlapping live ranges of registers.
385 If there is an abnormal edge in DEST, the problems would occur
386 anyway since cleanup_dead_labels would then merge the labels for
387 two different eh regions, and rest of exception handling code
390 So if there is an abnormal edge to BB, proceed only if there is
391 no abnormal edge to DEST and there are no phi nodes in DEST. */
392 if (has_abnormal_incoming_edge_p (bb
))
394 seen_abnormal_edge
= true;
396 if (has_abnormal_incoming_edge_p (dest
)
397 || phi_nodes (dest
) != NULL_TREE
)
401 /* If there are phi nodes in DEST, and some of the blocks that are
402 predecessors of BB are also predecessors of DEST, check that the
403 phi node arguments match. */
404 if (phi_nodes (dest
))
406 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
408 s
= find_edge (e
->src
, dest
);
412 if (!phi_alternatives_equal (dest
, succ
, s
))
417 /* Redirect the edges. */
418 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
420 bitmap_set_bit (cfgcleanup_altered_bbs
, e
->src
->index
);
422 if (e
->flags
& EDGE_ABNORMAL
)
424 /* If there is an abnormal edge, redirect it anyway, and
425 move the labels to the new block to make it legal. */
426 s
= redirect_edge_succ_nodup (e
, dest
);
429 s
= redirect_edge_and_branch (e
, dest
);
433 /* Create arguments for the phi nodes, since the edge was not
435 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
436 add_phi_arg (phi
, PHI_ARG_DEF (phi
, succ
->dest_idx
), s
);
440 if (seen_abnormal_edge
)
442 /* Move the labels to the new block, so that the redirection of
443 the abnormal edges works. */
445 bsi_to
= bsi_start (dest
);
446 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
448 label
= bsi_stmt (bsi
);
449 gcc_assert (TREE_CODE (label
) == LABEL_EXPR
);
450 bsi_remove (&bsi
, false);
451 bsi_insert_before (&bsi_to
, label
, BSI_CONTINUE_LINKING
);
455 bitmap_set_bit (cfgcleanup_altered_bbs
, dest
->index
);
457 /* Update the dominators. */
458 if (dom_info_available_p (CDI_DOMINATORS
))
460 basic_block dom
, dombb
, domdest
;
462 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
463 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
466 /* Shortcut to avoid calling (relatively expensive)
467 nearest_common_dominator unless necessary. */
471 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
473 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
476 /* And kill the forwarder block. */
477 delete_basic_block (bb
);
482 /* Split basic blocks on calls in the middle of a basic block that are now
483 known not to return, and remove the unreachable code. */
486 split_bbs_on_noreturn_calls (void)
488 bool changed
= false;
492 /* Detect cases where a mid-block call is now known not to return. */
494 while (VEC_length (tree
, MODIFIED_NORETURN_CALLS (cfun
)))
496 stmt
= VEC_pop (tree
, MODIFIED_NORETURN_CALLS (cfun
));
497 bb
= bb_for_stmt (stmt
);
499 || last_stmt (bb
) == stmt
500 || !noreturn_call_p (stmt
))
504 split_block (bb
, stmt
);
505 remove_fallthru_edge (bb
->succs
);
511 /* Tries to cleanup cfg in basic block BB. Returns true if anything
515 cleanup_tree_cfg_bb (basic_block bb
)
519 retval
= cleanup_control_flow_bb (bb
);
521 /* Forwarder blocks can carry line number information which is
522 useful when debugging, so we only clean them up when
526 && tree_forwarder_block_p (bb
, false)
527 && remove_forwarder_block (bb
))
530 /* Merging the blocks may create new opportunities for folding
531 conditional branches (due to the elimination of single-valued PHI
533 if (single_succ_p (bb
)
534 && can_merge_blocks_p (bb
, single_succ (bb
)))
536 merge_blocks (bb
, single_succ (bb
));
543 /* Iterate the cfg cleanups, while anything changes. */
546 cleanup_tree_cfg_1 (void)
552 retval
|= split_bbs_on_noreturn_calls ();
554 /* Prepare the worklists of altered blocks. */
555 cfgcleanup_altered_bbs
= BITMAP_ALLOC (NULL
);
557 /* During forwarder block cleanup, we may redirect edges out of
558 SWITCH_EXPRs, which can get expensive. So we want to enable
559 recording of edge to CASE_LABEL_EXPR. */
560 start_recording_case_labels ();
562 /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB,
563 since the basic blocks may get removed. */
564 n
= last_basic_block
;
565 for (i
= NUM_FIXED_BLOCKS
; i
< n
; i
++)
567 bb
= BASIC_BLOCK (i
);
569 retval
|= cleanup_tree_cfg_bb (bb
);
572 /* Now process the altered blocks, as long as any are available. */
573 while (!bitmap_empty_p (cfgcleanup_altered_bbs
))
575 i
= bitmap_first_set_bit (cfgcleanup_altered_bbs
);
576 bitmap_clear_bit (cfgcleanup_altered_bbs
, i
);
577 if (i
< NUM_FIXED_BLOCKS
)
580 bb
= BASIC_BLOCK (i
);
584 retval
|= cleanup_tree_cfg_bb (bb
);
586 /* Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn
588 retval
|= split_bbs_on_noreturn_calls ();
591 end_recording_case_labels ();
592 BITMAP_FREE (cfgcleanup_altered_bbs
);
597 /* Remove unreachable blocks and other miscellaneous clean up work.
598 Return true if the flowgraph was modified, false otherwise. */
601 cleanup_tree_cfg (void)
605 timevar_push (TV_TREE_CLEANUP_CFG
);
607 /* Iterate until there are no more cleanups left to do. If any
608 iteration changed the flowgraph, set CHANGED to true.
610 If dominance information is available, there cannot be any unreachable
612 if (!dom_info_available_p (CDI_DOMINATORS
))
614 changed
= delete_unreachable_blocks ();
615 calculate_dominance_info (CDI_DOMINATORS
);
619 #ifdef ENABLE_CHECKING
620 verify_dominators (CDI_DOMINATORS
);
625 changed
|= cleanup_tree_cfg_1 ();
627 gcc_assert (dom_info_available_p (CDI_DOMINATORS
));
630 #ifdef ENABLE_CHECKING
634 timevar_pop (TV_TREE_CLEANUP_CFG
);
639 /* Cleanup cfg and repair loop structures. */
642 cleanup_tree_cfg_loop (void)
644 bool changed
= cleanup_tree_cfg ();
646 if (changed
&& current_loops
!= NULL
)
648 bitmap changed_bbs
= BITMAP_ALLOC (NULL
);
649 fix_loop_structure (changed_bbs
);
651 /* This usually does nothing. But sometimes parts of cfg that originally
652 were inside a loop get out of it due to edge removal (since they
653 become unreachable by back edges from latch). */
654 if ((current_loops
->state
& LOOP_CLOSED_SSA
) != 0)
655 rewrite_into_loop_closed_ssa (changed_bbs
, TODO_update_ssa
);
657 BITMAP_FREE (changed_bbs
);
659 #ifdef ENABLE_CHECKING
660 verify_loop_structure ();
667 /* Merge the PHI nodes at BB into those at BB's sole successor. */
670 remove_forwarder_block_with_phi (basic_block bb
)
672 edge succ
= single_succ_edge (bb
);
673 basic_block dest
= succ
->dest
;
675 basic_block dombb
, domdest
, dom
;
677 /* We check for infinite loops already in tree_forwarder_block_p.
678 However it may happen that the infinite loop is created
679 afterwards due to removal of forwarders. */
683 /* If the destination block consists of a nonlocal label, do not
685 label
= first_stmt (dest
);
687 && TREE_CODE (label
) == LABEL_EXPR
688 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label
)))
691 /* Redirect each incoming edge to BB to DEST. */
692 while (EDGE_COUNT (bb
->preds
) > 0)
694 edge e
= EDGE_PRED (bb
, 0), s
;
697 s
= find_edge (e
->src
, dest
);
700 /* We already have an edge S from E->src to DEST. If S and
701 E->dest's sole successor edge have the same PHI arguments
702 at DEST, redirect S to DEST. */
703 if (phi_alternatives_equal (dest
, s
, succ
))
705 e
= redirect_edge_and_branch (e
, dest
);
706 PENDING_STMT (e
) = NULL_TREE
;
710 /* PHI arguments are different. Create a forwarder block by
711 splitting E so that we can merge PHI arguments on E to
713 e
= single_succ_edge (split_edge (e
));
716 s
= redirect_edge_and_branch (e
, dest
);
718 /* redirect_edge_and_branch must not create a new edge. */
721 /* Add to the PHI nodes at DEST each PHI argument removed at the
723 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
725 tree def
= PHI_ARG_DEF (phi
, succ
->dest_idx
);
727 if (TREE_CODE (def
) == SSA_NAME
)
731 /* If DEF is one of the results of PHI nodes removed during
732 redirection, replace it with the PHI argument that used
734 for (var
= PENDING_STMT (e
); var
; var
= TREE_CHAIN (var
))
736 tree old_arg
= TREE_PURPOSE (var
);
737 tree new_arg
= TREE_VALUE (var
);
747 add_phi_arg (phi
, def
, s
);
750 PENDING_STMT (e
) = NULL
;
753 /* Update the dominators. */
754 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
755 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
758 /* Shortcut to avoid calling (relatively expensive)
759 nearest_common_dominator unless necessary. */
763 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
765 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
767 /* Remove BB since all of BB's incoming edges have been redirected
769 delete_basic_block (bb
);
772 /* This pass merges PHI nodes if one feeds into another. For example,
773 suppose we have the following:
780 # tem_6 = PHI <tem_17(8), tem_23(7)>;
783 # tem_3 = PHI <tem_6(9), tem_2(5)>;
786 Then we merge the first PHI node into the second one like so:
793 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
798 merge_phi_nodes (void)
800 basic_block
*worklist
= XNEWVEC (basic_block
, n_basic_blocks
);
801 basic_block
*current
= worklist
;
804 calculate_dominance_info (CDI_DOMINATORS
);
806 /* Find all PHI nodes that we may be able to merge. */
811 /* Look for a forwarder block with PHI nodes. */
812 if (!tree_forwarder_block_p (bb
, true))
815 dest
= single_succ (bb
);
817 /* We have to feed into another basic block with PHI
819 if (!phi_nodes (dest
)
820 /* We don't want to deal with a basic block with
822 || has_abnormal_incoming_edge_p (bb
))
825 if (!dominated_by_p (CDI_DOMINATORS
, dest
, bb
))
827 /* If BB does not dominate DEST, then the PHI nodes at
828 DEST must be the only users of the results of the PHI
835 unsigned int dest_idx
= single_succ_edge (bb
)->dest_idx
;
837 /* BB dominates DEST. There may be many users of the PHI
838 nodes in BB. However, there is still a trivial case we
839 can handle. If the result of every PHI in BB is used
840 only by a PHI in DEST, then we can trivially merge the
841 PHI nodes from BB into DEST. */
842 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
844 tree result
= PHI_RESULT (phi
);
845 use_operand_p imm_use
;
848 /* If the PHI's result is never used, then we can just
850 if (has_zero_uses (result
))
853 /* Get the single use of the result of this PHI node. */
854 if (!single_imm_use (result
, &imm_use
, &use_stmt
)
855 || TREE_CODE (use_stmt
) != PHI_NODE
856 || bb_for_stmt (use_stmt
) != dest
857 || PHI_ARG_DEF (use_stmt
, dest_idx
) != result
)
861 /* If the loop above iterated through all the PHI nodes
862 in BB, then we can merge the PHIs from BB into DEST. */
868 /* Now let's drain WORKLIST. */
869 while (current
!= worklist
)
872 remove_forwarder_block_with_phi (bb
);
880 gate_merge_phi (void)
885 struct tree_opt_pass pass_merge_phi
= {
886 "mergephi", /* name */
887 gate_merge_phi
, /* gate */
888 merge_phi_nodes
, /* execute */
891 0, /* static_pass_number */
892 TV_TREE_MERGE_PHI
, /* tv_id */
893 PROP_cfg
| PROP_ssa
, /* properties_required */
894 0, /* properties_provided */
895 0, /* properties_destroyed */
896 0, /* todo_flags_start */
897 TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */