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 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
54 remove_fallthru_edge (VEC(edge
,gc
) *ev
)
59 FOR_EACH_EDGE (e
, ei
, ev
)
60 if ((e
->flags
& EDGE_FALLTHRU
) != 0)
68 /* Disconnect an unreachable block in the control expression starting
72 cleanup_control_expr_graph (basic_block bb
, block_stmt_iterator bsi
)
76 tree expr
= bsi_stmt (bsi
), val
;
78 if (!single_succ_p (bb
))
84 fold_defer_overflow_warnings ();
86 switch (TREE_CODE (expr
))
89 val
= fold (COND_EXPR_COND (expr
));
93 val
= fold (SWITCH_COND (expr
));
94 if (TREE_CODE (val
) != INTEGER_CST
)
96 fold_undefer_and_ignore_overflow_warnings ();
105 taken_edge
= find_taken_edge (bb
, val
);
108 fold_undefer_and_ignore_overflow_warnings ();
112 /* Remove all the edges except the one that is always executed. */
114 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
120 fold_undefer_overflow_warnings
121 (true, expr
, WARN_STRICT_OVERFLOW_CONDITIONAL
);
125 taken_edge
->probability
+= e
->probability
;
126 taken_edge
->count
+= e
->count
;
134 fold_undefer_and_ignore_overflow_warnings ();
135 if (taken_edge
->probability
> REG_BR_PROB_BASE
)
136 taken_edge
->probability
= REG_BR_PROB_BASE
;
139 taken_edge
= single_succ_edge (bb
);
141 bsi_remove (&bsi
, true);
142 taken_edge
->flags
= EDGE_FALLTHRU
;
144 /* We removed some paths from the cfg. */
145 free_dominance_info (CDI_DOMINATORS
);
150 /* A list of all the noreturn calls passed to modify_stmt.
151 cleanup_control_flow uses it to detect cases where a mid-block
152 indirect call has been turned into a noreturn call. When this
153 happens, all the instructions after the call are no longer
154 reachable and must be deleted as dead. */
156 VEC(tree
,gc
) *modified_noreturn_calls
;
158 /* Try to remove superfluous control structures. */
161 cleanup_control_flow (void)
164 block_stmt_iterator bsi
;
168 /* Detect cases where a mid-block call is now known not to return. */
169 while (VEC_length (tree
, modified_noreturn_calls
))
171 stmt
= VEC_pop (tree
, modified_noreturn_calls
);
172 bb
= bb_for_stmt (stmt
);
173 if (bb
!= NULL
&& last_stmt (bb
) != stmt
&& noreturn_call_p (stmt
))
174 split_block (bb
, stmt
);
181 /* If the last statement of the block could throw and now cannot,
182 we need to prune cfg. */
183 retval
|= tree_purge_dead_eh_edges (bb
);
188 stmt
= bsi_stmt (bsi
);
190 if (TREE_CODE (stmt
) == COND_EXPR
191 || TREE_CODE (stmt
) == SWITCH_EXPR
)
192 retval
|= cleanup_control_expr_graph (bb
, bsi
);
193 /* If we had a computed goto which has a compile-time determinable
194 destination, then we can eliminate the goto. */
195 else if (TREE_CODE (stmt
) == GOTO_EXPR
196 && TREE_CODE (GOTO_DESTINATION (stmt
)) == ADDR_EXPR
197 && (TREE_CODE (TREE_OPERAND (GOTO_DESTINATION (stmt
), 0))
203 basic_block target_block
;
204 bool removed_edge
= false;
206 /* First look at all the outgoing edges. Delete any outgoing
207 edges which do not go to the right block. For the one
208 edge which goes to the right block, fix up its flags. */
209 label
= TREE_OPERAND (GOTO_DESTINATION (stmt
), 0);
210 target_block
= label_to_block (label
);
211 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
213 if (e
->dest
!= target_block
)
220 /* Turn off the EDGE_ABNORMAL flag. */
221 e
->flags
&= ~EDGE_ABNORMAL
;
223 /* And set EDGE_FALLTHRU. */
224 e
->flags
|= EDGE_FALLTHRU
;
229 /* If we removed one or more edges, then we will need to fix the
230 dominators. It may be possible to incrementally update them. */
232 free_dominance_info (CDI_DOMINATORS
);
234 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
235 relevant information we need. */
236 bsi_remove (&bsi
, true);
240 /* Check for indirect calls that have been turned into
242 else if (noreturn_call_p (stmt
) && remove_fallthru_edge (bb
->succs
))
244 free_dominance_info (CDI_DOMINATORS
);
251 /* Return true if basic block BB does nothing except pass control
252 flow to another block and that we can safely insert a label at
253 the start of the successor block.
255 As a precondition, we require that BB be not equal to
259 tree_forwarder_block_p (basic_block bb
, bool phi_wanted
)
261 block_stmt_iterator bsi
;
266 /* BB must have a single outgoing edge. */
267 if (single_succ_p (bb
) != 1
268 /* If PHI_WANTED is false, BB must not have any PHI nodes.
269 Otherwise, BB must have PHI nodes. */
270 || (phi_nodes (bb
) != NULL_TREE
) != phi_wanted
271 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
272 || single_succ (bb
) == EXIT_BLOCK_PTR
273 /* Nor should this be an infinite loop. */
274 || single_succ (bb
) == bb
275 /* BB may not have an abnormal outgoing edge. */
276 || (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
))
280 gcc_assert (bb
!= ENTRY_BLOCK_PTR
);
283 /* Now walk through the statements backward. We can ignore labels,
284 anything else means this is not a forwarder block. */
285 for (bsi
= bsi_last (bb
); !bsi_end_p (bsi
); bsi_prev (&bsi
))
287 tree stmt
= bsi_stmt (bsi
);
289 switch (TREE_CODE (stmt
))
292 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
301 if (find_edge (ENTRY_BLOCK_PTR
, bb
))
307 /* Protect loop latches, headers and preheaders. */
308 if (bb
->loop_father
->header
== bb
)
310 dest
= EDGE_SUCC (bb
, 0)->dest
;
312 if (dest
->loop_father
->header
== dest
)
316 /* If we have an EH edge leaving this block, make sure that the
317 destination of this block has only one predecessor. This ensures
318 that we don't get into the situation where we try to remove two
319 forwarders that go to the same basic block but are handlers for
320 different EH regions. */
321 succ
= single_succ_edge (bb
);
323 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
325 if (e
->flags
& EDGE_EH
)
327 if (!single_pred_p (dest
))
335 /* Return true if BB has at least one abnormal incoming edge. */
338 has_abnormal_incoming_edge_p (basic_block bb
)
343 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
344 if (e
->flags
& EDGE_ABNORMAL
)
350 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
351 those alternatives are equal in each of the PHI nodes, then return
352 true, else return false. */
355 phi_alternatives_equal (basic_block dest
, edge e1
, edge e2
)
357 int n1
= e1
->dest_idx
;
358 int n2
= e2
->dest_idx
;
361 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
363 tree val1
= PHI_ARG_DEF (phi
, n1
);
364 tree val2
= PHI_ARG_DEF (phi
, n2
);
366 gcc_assert (val1
!= NULL_TREE
);
367 gcc_assert (val2
!= NULL_TREE
);
369 if (!operand_equal_for_phi_arg_p (val1
, val2
))
376 /* Removes forwarder block BB. Returns false if this failed. If a new
377 forwarder block is created due to redirection of edges, it is
378 stored to worklist. */
381 remove_forwarder_block (basic_block bb
, basic_block
**worklist
)
383 edge succ
= single_succ_edge (bb
), e
, s
;
384 basic_block dest
= succ
->dest
;
388 block_stmt_iterator bsi
, bsi_to
;
389 bool seen_abnormal_edge
= false;
391 /* We check for infinite loops already in tree_forwarder_block_p.
392 However it may happen that the infinite loop is created
393 afterwards due to removal of forwarders. */
397 /* If the destination block consists of a nonlocal label, do not merge
399 label
= first_stmt (dest
);
401 && TREE_CODE (label
) == LABEL_EXPR
402 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label
)))
405 /* If there is an abnormal edge to basic block BB, but not into
406 dest, problems might occur during removal of the phi node at out
407 of ssa due to overlapping live ranges of registers.
409 If there is an abnormal edge in DEST, the problems would occur
410 anyway since cleanup_dead_labels would then merge the labels for
411 two different eh regions, and rest of exception handling code
414 So if there is an abnormal edge to BB, proceed only if there is
415 no abnormal edge to DEST and there are no phi nodes in DEST. */
416 if (has_abnormal_incoming_edge_p (bb
))
418 seen_abnormal_edge
= true;
420 if (has_abnormal_incoming_edge_p (dest
)
421 || phi_nodes (dest
) != NULL_TREE
)
425 /* If there are phi nodes in DEST, and some of the blocks that are
426 predecessors of BB are also predecessors of DEST, check that the
427 phi node arguments match. */
428 if (phi_nodes (dest
))
430 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
432 s
= find_edge (e
->src
, dest
);
436 if (!phi_alternatives_equal (dest
, succ
, s
))
441 /* Redirect the edges. */
442 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
444 if (e
->flags
& EDGE_ABNORMAL
)
446 /* If there is an abnormal edge, redirect it anyway, and
447 move the labels to the new block to make it legal. */
448 s
= redirect_edge_succ_nodup (e
, dest
);
451 s
= redirect_edge_and_branch (e
, dest
);
455 /* Create arguments for the phi nodes, since the edge was not
457 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
458 add_phi_arg (phi
, PHI_ARG_DEF (phi
, succ
->dest_idx
), s
);
462 /* The source basic block might become a forwarder. We know
463 that it was not a forwarder before, since it used to have
464 at least two outgoing edges, so we may just add it to
466 if (tree_forwarder_block_p (s
->src
, false))
467 *(*worklist
)++ = s
->src
;
471 if (seen_abnormal_edge
)
473 /* Move the labels to the new block, so that the redirection of
474 the abnormal edges works. */
476 bsi_to
= bsi_start (dest
);
477 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
479 label
= bsi_stmt (bsi
);
480 gcc_assert (TREE_CODE (label
) == LABEL_EXPR
);
481 bsi_remove (&bsi
, false);
482 bsi_insert_before (&bsi_to
, label
, BSI_CONTINUE_LINKING
);
486 /* Update the dominators. */
487 if (dom_info_available_p (CDI_DOMINATORS
))
489 basic_block dom
, dombb
, domdest
;
491 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
492 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
495 /* Shortcut to avoid calling (relatively expensive)
496 nearest_common_dominator unless necessary. */
500 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
502 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
505 /* And kill the forwarder block. */
506 delete_basic_block (bb
);
511 /* Removes forwarder blocks. */
514 cleanup_forwarder_blocks (void)
517 bool changed
= false;
518 basic_block
*worklist
= XNEWVEC (basic_block
, n_basic_blocks
);
519 basic_block
*current
= worklist
;
523 if (tree_forwarder_block_p (bb
, false))
527 while (current
!= worklist
)
530 changed
|= remove_forwarder_block (bb
, ¤t
);
537 /* Do one round of CFG cleanup. */
540 cleanup_tree_cfg_1 (void)
544 retval
= cleanup_control_flow ();
545 retval
|= delete_unreachable_blocks ();
547 /* Forwarder blocks can carry line number information which is
548 useful when debugging, so we only clean them up when
553 /* cleanup_forwarder_blocks can redirect edges out of
554 SWITCH_EXPRs, which can get expensive. So we want to enable
555 recording of edge to CASE_LABEL_EXPR mappings around the call
556 to cleanup_forwarder_blocks. */
557 start_recording_case_labels ();
558 retval
|= cleanup_forwarder_blocks ();
559 end_recording_case_labels ();
562 /* Merging the blocks may create new opportunities for folding
563 conditional branches (due to the elimination of single-valued PHI
565 retval
|= merge_seq_blocks ();
571 /* Remove unreachable blocks and other miscellaneous clean up work.
572 Return true if the flowgraph was modified, false otherwise. */
575 cleanup_tree_cfg (void)
577 bool retval
, changed
;
579 timevar_push (TV_TREE_CLEANUP_CFG
);
581 /* Iterate until there are no more cleanups left to do. If any
582 iteration changed the flowgraph, set CHANGED to true. */
586 retval
= cleanup_tree_cfg_1 ();
593 #ifdef ENABLE_CHECKING
597 timevar_pop (TV_TREE_CLEANUP_CFG
);
602 /* Cleanup cfg and repair loop structures. */
605 cleanup_tree_cfg_loop (void)
607 bool changed
= cleanup_tree_cfg ();
611 bitmap changed_bbs
= BITMAP_ALLOC (NULL
);
612 fix_loop_structure (current_loops
, changed_bbs
);
613 calculate_dominance_info (CDI_DOMINATORS
);
615 /* This usually does nothing. But sometimes parts of cfg that originally
616 were inside a loop get out of it due to edge removal (since they
617 become unreachable by back edges from latch). */
618 rewrite_into_loop_closed_ssa (changed_bbs
, TODO_update_ssa
);
620 BITMAP_FREE (changed_bbs
);
622 #ifdef ENABLE_CHECKING
623 verify_loop_structure (current_loops
);
629 /* Merge the PHI nodes at BB into those at BB's sole successor. */
632 remove_forwarder_block_with_phi (basic_block bb
)
634 edge succ
= single_succ_edge (bb
);
635 basic_block dest
= succ
->dest
;
637 basic_block dombb
, domdest
, dom
;
639 /* We check for infinite loops already in tree_forwarder_block_p.
640 However it may happen that the infinite loop is created
641 afterwards due to removal of forwarders. */
645 /* If the destination block consists of a nonlocal label, do not
647 label
= first_stmt (dest
);
649 && TREE_CODE (label
) == LABEL_EXPR
650 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label
)))
653 /* Redirect each incoming edge to BB to DEST. */
654 while (EDGE_COUNT (bb
->preds
) > 0)
656 edge e
= EDGE_PRED (bb
, 0), s
;
659 s
= find_edge (e
->src
, dest
);
662 /* We already have an edge S from E->src to DEST. If S and
663 E->dest's sole successor edge have the same PHI arguments
664 at DEST, redirect S to DEST. */
665 if (phi_alternatives_equal (dest
, s
, succ
))
667 e
= redirect_edge_and_branch (e
, dest
);
668 PENDING_STMT (e
) = NULL_TREE
;
672 /* PHI arguments are different. Create a forwarder block by
673 splitting E so that we can merge PHI arguments on E to
675 e
= single_succ_edge (split_edge (e
));
678 s
= redirect_edge_and_branch (e
, dest
);
680 /* redirect_edge_and_branch must not create a new edge. */
683 /* Add to the PHI nodes at DEST each PHI argument removed at the
685 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
687 tree def
= PHI_ARG_DEF (phi
, succ
->dest_idx
);
689 if (TREE_CODE (def
) == SSA_NAME
)
693 /* If DEF is one of the results of PHI nodes removed during
694 redirection, replace it with the PHI argument that used
696 for (var
= PENDING_STMT (e
); var
; var
= TREE_CHAIN (var
))
698 tree old_arg
= TREE_PURPOSE (var
);
699 tree new_arg
= TREE_VALUE (var
);
709 add_phi_arg (phi
, def
, s
);
712 PENDING_STMT (e
) = NULL
;
715 /* Update the dominators. */
716 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
717 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
720 /* Shortcut to avoid calling (relatively expensive)
721 nearest_common_dominator unless necessary. */
725 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
727 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
729 /* Remove BB since all of BB's incoming edges have been redirected
731 delete_basic_block (bb
);
734 /* This pass merges PHI nodes if one feeds into another. For example,
735 suppose we have the following:
742 # tem_6 = PHI <tem_17(8), tem_23(7)>;
745 # tem_3 = PHI <tem_6(9), tem_2(5)>;
748 Then we merge the first PHI node into the second one like so:
755 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
760 merge_phi_nodes (void)
762 basic_block
*worklist
= XNEWVEC (basic_block
, n_basic_blocks
);
763 basic_block
*current
= worklist
;
766 calculate_dominance_info (CDI_DOMINATORS
);
768 /* Find all PHI nodes that we may be able to merge. */
773 /* Look for a forwarder block with PHI nodes. */
774 if (!tree_forwarder_block_p (bb
, true))
777 dest
= single_succ (bb
);
779 /* We have to feed into another basic block with PHI
781 if (!phi_nodes (dest
)
782 /* We don't want to deal with a basic block with
784 || has_abnormal_incoming_edge_p (bb
))
787 if (!dominated_by_p (CDI_DOMINATORS
, dest
, bb
))
789 /* If BB does not dominate DEST, then the PHI nodes at
790 DEST must be the only users of the results of the PHI
797 unsigned int dest_idx
= single_succ_edge (bb
)->dest_idx
;
799 /* BB dominates DEST. There may be many users of the PHI
800 nodes in BB. However, there is still a trivial case we
801 can handle. If the result of every PHI in BB is used
802 only by a PHI in DEST, then we can trivially merge the
803 PHI nodes from BB into DEST. */
804 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
806 tree result
= PHI_RESULT (phi
);
807 use_operand_p imm_use
;
810 /* If the PHI's result is never used, then we can just
812 if (has_zero_uses (result
))
815 /* Get the single use of the result of this PHI node. */
816 if (!single_imm_use (result
, &imm_use
, &use_stmt
)
817 || TREE_CODE (use_stmt
) != PHI_NODE
818 || bb_for_stmt (use_stmt
) != dest
819 || PHI_ARG_DEF (use_stmt
, dest_idx
) != result
)
823 /* If the loop above iterated through all the PHI nodes
824 in BB, then we can merge the PHIs from BB into DEST. */
830 /* Now let's drain WORKLIST. */
831 while (current
!= worklist
)
834 remove_forwarder_block_with_phi (bb
);
842 gate_merge_phi (void)
847 struct tree_opt_pass pass_merge_phi
= {
848 "mergephi", /* name */
849 gate_merge_phi
, /* gate */
850 merge_phi_nodes
, /* execute */
853 0, /* static_pass_number */
854 TV_TREE_MERGE_PHI
, /* tv_id */
855 PROP_cfg
| PROP_ssa
, /* properties_required */
856 0, /* properties_provided */
857 0, /* properties_destroyed */
858 0, /* todo_flags_start */
859 TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */