1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 /* This file contains functions for building the Control Flow Graph (CFG)
49 for a function tree. */
51 /* Local declarations. */
53 /* Initial capacity for the basic block array. */
54 static const int initial_cfg_capacity = 20;
56 /* Mapping of labels to their associated blocks. This can greatly speed up
57 building of the CFG in code with lots of gotos. */
58 static GTY(()) varray_type label_to_block_map;
63 long num_merged_labels;
66 static struct cfg_stats_d cfg_stats;
68 /* Nonzero if we found a computed goto while building basic blocks. */
69 static bool found_computed_goto;
71 /* Basic blocks and flowgraphs. */
72 static basic_block create_bb (void *, void *, basic_block);
73 static void create_block_annotation (basic_block);
74 static void free_blocks_annotations (void);
75 static void clear_blocks_annotations (void);
76 static void make_blocks (tree);
77 static void factor_computed_gotos (void);
80 static void make_edges (void);
81 static void make_ctrl_stmt_edges (basic_block);
82 static void make_exit_edges (basic_block);
83 static void make_cond_expr_edges (basic_block);
84 static void make_switch_expr_edges (basic_block);
85 static void make_goto_expr_edges (basic_block);
86 static edge tree_redirect_edge_and_branch (edge, basic_block);
87 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
88 static void split_critical_edges (void);
90 /* Various helpers. */
91 static inline bool stmt_starts_bb_p (tree, tree);
92 static int tree_verify_flow_info (void);
93 static void tree_make_forwarder_block (edge);
94 static bool thread_jumps (void);
95 static bool tree_forwarder_block_p (basic_block);
96 static void tree_cfg2vcg (FILE *);
98 /* Flowgraph optimization and cleanup. */
99 static void tree_merge_blocks (basic_block, basic_block);
100 static bool tree_can_merge_blocks_p (basic_block, basic_block);
101 static void remove_bb (basic_block);
102 static bool cleanup_control_flow (void);
103 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
104 static edge find_taken_edge_cond_expr (basic_block, tree);
105 static edge find_taken_edge_switch_expr (basic_block, tree);
106 static tree find_case_label_for_value (tree, tree);
107 static bool phi_alternatives_equal (basic_block, edge, edge);
110 /*---------------------------------------------------------------------------
112 ---------------------------------------------------------------------------*/
114 /* Entry point to the CFG builder for trees. TP points to the list of
115 statements to be added to the flowgraph. */
118 build_tree_cfg (tree *tp)
120 /* Register specific tree functions. */
121 tree_register_cfg_hooks ();
123 /* Initialize rbi_pool. */
126 /* Initialize the basic block array. */
128 profile_status = PROFILE_ABSENT;
130 last_basic_block = 0;
131 VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info");
132 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
134 /* Build a mapping of labels to their associated blocks. */
135 VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity,
136 "label to block map");
138 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
139 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
141 found_computed_goto = 0;
144 /* Computed gotos are hell to deal with, especially if there are
145 lots of them with a large number of destinations. So we factor
146 them to a common computed goto location before we build the
147 edge list. After we convert back to normal form, we will un-factor
148 the computed gotos since factoring introduces an unwanted jump. */
149 if (found_computed_goto)
150 factor_computed_gotos ();
152 /* Make sure there is always at least one block, even if it's empty. */
153 if (n_basic_blocks == 0)
154 create_empty_bb (ENTRY_BLOCK_PTR);
156 create_block_annotation (ENTRY_BLOCK_PTR);
157 create_block_annotation (EXIT_BLOCK_PTR);
159 /* Adjust the size of the array. */
160 VARRAY_GROW (basic_block_info, n_basic_blocks);
162 /* To speed up statement iterator walks, we first purge dead labels. */
163 cleanup_dead_labels ();
165 /* Group case nodes to reduce the number of edges.
166 We do this after cleaning up dead labels because otherwise we miss
167 a lot of obvious case merging opportunities. */
168 group_case_labels ();
170 /* Create the edges of the flowgraph. */
173 /* Debugging dumps. */
175 /* Write the flowgraph to a VCG file. */
177 int local_dump_flags;
178 FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags);
181 tree_cfg2vcg (dump_file);
182 dump_end (TDI_vcg, dump_file);
186 /* Dump a textual representation of the flowgraph. */
188 dump_tree_cfg (dump_file, dump_flags);
192 execute_build_cfg (void)
194 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
197 struct tree_opt_pass pass_build_cfg =
201 execute_build_cfg, /* execute */
204 0, /* static_pass_number */
205 TV_TREE_CFG, /* tv_id */
206 PROP_gimple_leh, /* properties_required */
207 PROP_cfg, /* properties_provided */
208 0, /* properties_destroyed */
209 0, /* todo_flags_start */
210 TODO_verify_stmts, /* todo_flags_finish */
214 /* Search the CFG for any computed gotos. If found, factor them to a
215 common computed goto site. Also record the location of that site so
216 that we can un-factor the gotos after we have converted back to
220 factor_computed_gotos (void)
223 tree factored_label_decl = NULL;
225 tree factored_computed_goto_label = NULL;
226 tree factored_computed_goto = NULL;
228 /* We know there are one or more computed gotos in this function.
229 Examine the last statement in each basic block to see if the block
230 ends with a computed goto. */
234 block_stmt_iterator bsi = bsi_last (bb);
239 last = bsi_stmt (bsi);
241 /* Ignore the computed goto we create when we factor the original
243 if (last == factored_computed_goto)
246 /* If the last statement is a computed goto, factor it. */
247 if (computed_goto_p (last))
251 /* The first time we find a computed goto we need to create
252 the factored goto block and the variable each original
253 computed goto will use for their goto destination. */
254 if (! factored_computed_goto)
256 basic_block new_bb = create_empty_bb (bb);
257 block_stmt_iterator new_bsi = bsi_start (new_bb);
259 /* Create the destination of the factored goto. Each original
260 computed goto will put its desired destination into this
261 variable and jump to the label we create immediately
263 var = create_tmp_var (ptr_type_node, "gotovar");
265 /* Build a label for the new block which will contain the
266 factored computed goto. */
267 factored_label_decl = create_artificial_label ();
268 factored_computed_goto_label
269 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
270 bsi_insert_after (&new_bsi, factored_computed_goto_label,
273 /* Build our new computed goto. */
274 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
275 bsi_insert_after (&new_bsi, factored_computed_goto,
279 /* Copy the original computed goto's destination into VAR. */
280 assignment = build (MODIFY_EXPR, ptr_type_node,
281 var, GOTO_DESTINATION (last));
282 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
284 /* And re-vector the computed goto to the new destination. */
285 GOTO_DESTINATION (last) = factored_label_decl;
291 /* Create annotations for a single basic block. */
294 create_block_annotation (basic_block bb)
296 /* Verify that the tree_annotations field is clear. */
297 gcc_assert (!bb->tree_annotations);
298 bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d));
302 /* Free the annotations for all the basic blocks. */
304 static void free_blocks_annotations (void)
306 clear_blocks_annotations ();
310 /* Clear the annotations for all the basic blocks. */
313 clear_blocks_annotations (void)
317 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
318 bb->tree_annotations = NULL;
322 /* Build a flowgraph for the statement_list STMT_LIST. */
325 make_blocks (tree stmt_list)
327 tree_stmt_iterator i = tsi_start (stmt_list);
329 bool start_new_block = true;
330 bool first_stmt_of_list = true;
331 basic_block bb = ENTRY_BLOCK_PTR;
333 while (!tsi_end_p (i))
340 /* If the statement starts a new basic block or if we have determined
341 in a previous pass that we need to create a new block for STMT, do
343 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
345 if (!first_stmt_of_list)
346 stmt_list = tsi_split_statement_list_before (&i);
347 bb = create_basic_block (stmt_list, NULL, bb);
348 start_new_block = false;
351 /* Now add STMT to BB and create the subgraphs for special statement
353 set_bb_for_stmt (stmt, bb);
355 if (computed_goto_p (stmt))
356 found_computed_goto = true;
358 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
360 if (stmt_ends_bb_p (stmt))
361 start_new_block = true;
364 first_stmt_of_list = false;
369 /* Create and return a new empty basic block after bb AFTER. */
372 create_bb (void *h, void *e, basic_block after)
378 /* Create and initialize a new basic block. */
380 memset (bb, 0, sizeof (*bb));
382 bb->index = last_basic_block;
384 bb->stmt_list = h ? h : alloc_stmt_list ();
386 /* Add the new block to the linked list of blocks. */
387 link_block (bb, after);
389 /* Grow the basic block array if needed. */
390 if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info))
392 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
393 VARRAY_GROW (basic_block_info, new_size);
396 /* Add the newly created block to the array. */
397 BASIC_BLOCK (last_basic_block) = bb;
399 create_block_annotation (bb);
404 initialize_bb_rbi (bb);
409 /*---------------------------------------------------------------------------
411 ---------------------------------------------------------------------------*/
413 /* Join all the blocks in the flowgraph. */
420 /* Create an edge from entry to the first block with executable
422 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
424 /* Traverse basic block array placing edges. */
427 tree first = first_stmt (bb);
428 tree last = last_stmt (bb);
432 /* Edges for statements that always alter flow control. */
433 if (is_ctrl_stmt (last))
434 make_ctrl_stmt_edges (bb);
436 /* Edges for statements that sometimes alter flow control. */
437 if (is_ctrl_altering_stmt (last))
438 make_exit_edges (bb);
441 /* Finally, if no edges were created above, this is a regular
442 basic block that only needs a fallthru edge. */
443 if (EDGE_COUNT (bb->succs) == 0)
444 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
447 /* We do not care about fake edges, so remove any that the CFG
448 builder inserted for completeness. */
449 remove_fake_exit_edges ();
451 /* Clean up the graph and warn for unreachable code. */
456 /* Create edges for control statement at basic block BB. */
459 make_ctrl_stmt_edges (basic_block bb)
461 tree last = last_stmt (bb);
464 switch (TREE_CODE (last))
467 make_goto_expr_edges (bb);
471 make_edge (bb, EXIT_BLOCK_PTR, 0);
475 make_cond_expr_edges (bb);
479 make_switch_expr_edges (bb);
483 make_eh_edges (last);
484 /* Yet another NORETURN hack. */
485 if (EDGE_COUNT (bb->succs) == 0)
486 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
495 /* Create exit edges for statements in block BB that alter the flow of
496 control. Statements that alter the control flow are 'goto', 'return'
497 and calls to non-returning functions. */
500 make_exit_edges (basic_block bb)
502 tree last = last_stmt (bb), op;
505 switch (TREE_CODE (last))
508 /* If this function receives a nonlocal goto, then we need to
509 make edges from this call site to all the nonlocal goto
511 if (TREE_SIDE_EFFECTS (last)
512 && current_function_has_nonlocal_label)
513 make_goto_expr_edges (bb);
515 /* If this statement has reachable exception handlers, then
516 create abnormal edges to them. */
517 make_eh_edges (last);
519 /* Some calls are known not to return. For such calls we create
522 We really need to revamp how we build edges so that it's not
523 such a bloody pain to avoid creating edges for this case since
524 all we do is remove these edges when we're done building the
526 if (call_expr_flags (last) & (ECF_NORETURN | ECF_LONGJMP))
528 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
532 /* Don't forget the fall-thru edge. */
533 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
537 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
538 may have an abnormal edge. Search the RHS for this case and
539 create any required edges. */
540 op = get_call_expr_in (last);
541 if (op && TREE_SIDE_EFFECTS (op)
542 && current_function_has_nonlocal_label)
543 make_goto_expr_edges (bb);
545 make_eh_edges (last);
546 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
555 /* Create the edges for a COND_EXPR starting at block BB.
556 At this point, both clauses must contain only simple gotos. */
559 make_cond_expr_edges (basic_block bb)
561 tree entry = last_stmt (bb);
562 basic_block then_bb, else_bb;
563 tree then_label, else_label;
566 gcc_assert (TREE_CODE (entry) == COND_EXPR);
568 /* Entry basic blocks for each component. */
569 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
570 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
571 then_bb = label_to_block (then_label);
572 else_bb = label_to_block (else_label);
574 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
575 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
579 /* Create the edges for a SWITCH_EXPR starting at block BB.
580 At this point, the switch body has been lowered and the
581 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
584 make_switch_expr_edges (basic_block bb)
586 tree entry = last_stmt (bb);
590 vec = SWITCH_LABELS (entry);
591 n = TREE_VEC_LENGTH (vec);
593 for (i = 0; i < n; ++i)
595 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
596 basic_block label_bb = label_to_block (lab);
597 make_edge (bb, label_bb, 0);
602 /* Return the basic block holding label DEST. */
605 label_to_block (tree dest)
607 int uid = LABEL_DECL_UID (dest);
609 /* We would die hard when faced by an undefined label. Emit a label to
610 the very first basic block. This will hopefully make even the dataflow
611 and undefined variable warnings quite right. */
612 if ((errorcount || sorrycount) && uid < 0)
614 block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0));
617 stmt = build1 (LABEL_EXPR, void_type_node, dest);
618 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
619 uid = LABEL_DECL_UID (dest);
621 return VARRAY_BB (label_to_block_map, uid);
625 /* Create edges for a goto statement at block BB. */
628 make_goto_expr_edges (basic_block bb)
631 basic_block target_bb;
633 block_stmt_iterator last = bsi_last (bb);
635 goto_t = bsi_stmt (last);
637 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
638 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
639 from a nonlocal goto. */
640 if (TREE_CODE (goto_t) != GOTO_EXPR)
642 dest = error_mark_node;
647 dest = GOTO_DESTINATION (goto_t);
650 /* A GOTO to a local label creates normal edges. */
651 if (simple_goto_p (goto_t))
653 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
654 #ifdef USE_MAPPED_LOCATION
655 e->goto_locus = EXPR_LOCATION (goto_t);
657 e->goto_locus = EXPR_LOCUS (goto_t);
663 /* Nothing more to do for nonlocal gotos. */
664 if (TREE_CODE (dest) == LABEL_DECL)
667 /* Computed gotos remain. */
670 /* Look for the block starting with the destination label. In the
671 case of a computed goto, make an edge to any label block we find
673 FOR_EACH_BB (target_bb)
675 block_stmt_iterator bsi;
677 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
679 tree target = bsi_stmt (bsi);
681 if (TREE_CODE (target) != LABEL_EXPR)
685 /* Computed GOTOs. Make an edge to every label block that has
686 been marked as a potential target for a computed goto. */
687 (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0)
688 /* Nonlocal GOTO target. Make an edge to every label block
689 that has been marked as a potential target for a nonlocal
691 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1))
693 make_edge (bb, target_bb, EDGE_ABNORMAL);
699 /* Degenerate case of computed goto with no labels. */
700 if (!for_call && EDGE_COUNT (bb->succs) == 0)
701 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
705 /*---------------------------------------------------------------------------
707 ---------------------------------------------------------------------------*/
709 /* Remove unreachable blocks and other miscellaneous clean up work. */
712 cleanup_tree_cfg (void)
716 timevar_push (TV_TREE_CLEANUP_CFG);
718 retval = cleanup_control_flow ();
719 retval |= delete_unreachable_blocks ();
720 retval |= thread_jumps ();
722 #ifdef ENABLE_CHECKING
725 gcc_assert (!cleanup_control_flow ());
726 gcc_assert (!delete_unreachable_blocks ());
727 gcc_assert (!thread_jumps ());
731 /* Merging the blocks creates no new opportunities for the other
732 optimizations, so do it here. */
737 #ifdef ENABLE_CHECKING
740 timevar_pop (TV_TREE_CLEANUP_CFG);
745 /* Cleanup useless labels in basic blocks. This is something we wish
746 to do early because it allows us to group case labels before creating
747 the edges for the CFG, and it speeds up block statement iterators in
749 We only run this pass once, running it more than once is probably not
752 /* A map from basic block index to the leading label of that block. */
753 static tree *label_for_bb;
755 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
757 update_eh_label (struct eh_region *region)
759 tree old_label = get_eh_region_tree_label (region);
763 basic_block bb = label_to_block (old_label);
765 /* ??? After optimizing, there may be EH regions with labels
766 that have already been removed from the function body, so
767 there is no basic block for them. */
771 new_label = label_for_bb[bb->index];
772 set_eh_region_tree_label (region, new_label);
776 /* Given LABEL return the first label in the same basic block. */
778 main_block_label (tree label)
780 basic_block bb = label_to_block (label);
782 /* label_to_block possibly inserted undefined label into the chain. */
783 if (!label_for_bb[bb->index])
784 label_for_bb[bb->index] = label;
785 return label_for_bb[bb->index];
788 /* Cleanup redundant labels. This is a three-step process:
789 1) Find the leading label for each block.
790 2) Redirect all references to labels to the leading labels.
791 3) Cleanup all useless labels. */
794 cleanup_dead_labels (void)
797 label_for_bb = xcalloc (last_basic_block, sizeof (tree));
799 /* Find a suitable label for each block. We use the first user-defined
800 label if there is one, or otherwise just the first label we see. */
803 block_stmt_iterator i;
805 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
807 tree label, stmt = bsi_stmt (i);
809 if (TREE_CODE (stmt) != LABEL_EXPR)
812 label = LABEL_EXPR_LABEL (stmt);
814 /* If we have not yet seen a label for the current block,
815 remember this one and see if there are more labels. */
816 if (! label_for_bb[bb->index])
818 label_for_bb[bb->index] = label;
822 /* If we did see a label for the current block already, but it
823 is an artificially created label, replace it if the current
824 label is a user defined label. */
825 if (! DECL_ARTIFICIAL (label)
826 && DECL_ARTIFICIAL (label_for_bb[bb->index]))
828 label_for_bb[bb->index] = label;
834 /* Now redirect all jumps/branches to the selected label.
835 First do so for each block ending in a control statement. */
838 tree stmt = last_stmt (bb);
842 switch (TREE_CODE (stmt))
846 tree true_branch, false_branch;
848 true_branch = COND_EXPR_THEN (stmt);
849 false_branch = COND_EXPR_ELSE (stmt);
851 GOTO_DESTINATION (true_branch)
852 = main_block_label (GOTO_DESTINATION (true_branch));
853 GOTO_DESTINATION (false_branch)
854 = main_block_label (GOTO_DESTINATION (false_branch));
862 tree vec = SWITCH_LABELS (stmt);
863 size_t n = TREE_VEC_LENGTH (vec);
865 /* Replace all destination labels. */
866 for (i = 0; i < n; ++i)
867 CASE_LABEL (TREE_VEC_ELT (vec, i))
868 = main_block_label (CASE_LABEL (TREE_VEC_ELT (vec, i)));
873 /* We have to handle GOTO_EXPRs until they're removed, and we don't
874 remove them until after we've created the CFG edges. */
876 if (! computed_goto_p (stmt))
878 GOTO_DESTINATION (stmt)
879 = main_block_label (GOTO_DESTINATION (stmt));
888 for_each_eh_region (update_eh_label);
890 /* Finally, purge dead labels. All user-defined labels and labels that
891 can be the target of non-local gotos are preserved. */
894 block_stmt_iterator i;
895 tree label_for_this_bb = label_for_bb[bb->index];
897 if (! label_for_this_bb)
900 for (i = bsi_start (bb); !bsi_end_p (i); )
902 tree label, stmt = bsi_stmt (i);
904 if (TREE_CODE (stmt) != LABEL_EXPR)
907 label = LABEL_EXPR_LABEL (stmt);
909 if (label == label_for_this_bb
910 || ! DECL_ARTIFICIAL (label)
911 || DECL_NONLOCAL (label))
921 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
922 and scan the sorted vector of cases. Combine the ones jumping to the
924 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
927 group_case_labels (void)
933 tree stmt = last_stmt (bb);
934 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
936 tree labels = SWITCH_LABELS (stmt);
937 int old_size = TREE_VEC_LENGTH (labels);
938 int i, j, new_size = old_size;
939 tree default_case = TREE_VEC_ELT (labels, old_size - 1);
942 /* The default label is always the last case in a switch
943 statement after gimplification. */
944 default_label = CASE_LABEL (default_case);
946 /* Look for possible opportunities to merge cases.
947 Ignore the last element of the label vector because it
948 must be the default case. */
950 while (i < old_size - 1)
952 tree base_case, base_label, base_high, type;
953 base_case = TREE_VEC_ELT (labels, i);
955 gcc_assert (base_case);
956 base_label = CASE_LABEL (base_case);
958 /* Discard cases that have the same destination as the
960 if (base_label == default_label)
962 TREE_VEC_ELT (labels, i) = NULL_TREE;
968 type = TREE_TYPE (CASE_LOW (base_case));
969 base_high = CASE_HIGH (base_case) ?
970 CASE_HIGH (base_case) : CASE_LOW (base_case);
972 /* Try to merge case labels. Break out when we reach the end
973 of the label vector or when we cannot merge the next case
974 label with the current one. */
975 while (i < old_size - 1)
977 tree merge_case = TREE_VEC_ELT (labels, i);
978 tree merge_label = CASE_LABEL (merge_case);
979 tree t = int_const_binop (PLUS_EXPR, base_high,
980 integer_one_node, 1);
982 /* Merge the cases if they jump to the same place,
983 and their ranges are consecutive. */
984 if (merge_label == base_label
985 && tree_int_cst_equal (CASE_LOW (merge_case), t))
987 base_high = CASE_HIGH (merge_case) ?
988 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
989 CASE_HIGH (base_case) = base_high;
990 TREE_VEC_ELT (labels, i) = NULL_TREE;
999 /* Compress the case labels in the label vector, and adjust the
1000 length of the vector. */
1001 for (i = 0, j = 0; i < new_size; i++)
1003 while (! TREE_VEC_ELT (labels, j))
1005 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1007 TREE_VEC_LENGTH (labels) = new_size;
1012 /* Checks whether we can merge block B into block A. */
1015 tree_can_merge_blocks_p (basic_block a, basic_block b)
1018 block_stmt_iterator bsi;
1020 if (EDGE_COUNT (a->succs) != 1)
1023 if (EDGE_SUCC (a, 0)->flags & EDGE_ABNORMAL)
1026 if (EDGE_SUCC (a, 0)->dest != b)
1029 if (b == EXIT_BLOCK_PTR)
1032 if (EDGE_COUNT (b->preds) > 1)
1035 /* If A ends by a statement causing exceptions or something similar, we
1036 cannot merge the blocks. */
1037 stmt = last_stmt (a);
1038 if (stmt && stmt_ends_bb_p (stmt))
1041 /* Do not allow a block with only a non-local label to be merged. */
1042 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1043 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1046 /* There may be no phi nodes at the start of b. Most of these degenerate
1047 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1051 /* Do not remove user labels. */
1052 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1054 stmt = bsi_stmt (bsi);
1055 if (TREE_CODE (stmt) != LABEL_EXPR)
1057 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1065 /* Merge block B into block A. */
1068 tree_merge_blocks (basic_block a, basic_block b)
1070 block_stmt_iterator bsi;
1071 tree_stmt_iterator last;
1074 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1076 /* Ensure that B follows A. */
1077 move_block_after (b, a);
1079 gcc_assert (EDGE_SUCC (a, 0)->flags & EDGE_FALLTHRU);
1080 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1082 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1083 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1085 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1089 set_bb_for_stmt (bsi_stmt (bsi), a);
1094 /* Merge the chains. */
1095 last = tsi_last (a->stmt_list);
1096 tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT);
1097 b->stmt_list = NULL;
1101 /* Walk the function tree removing unnecessary statements.
1103 * Empty statement nodes are removed
1105 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1107 * Unnecessary COND_EXPRs are removed
1109 * Some unnecessary BIND_EXPRs are removed
1111 Clearly more work could be done. The trick is doing the analysis
1112 and removal fast enough to be a net improvement in compile times.
1114 Note that when we remove a control structure such as a COND_EXPR
1115 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1116 to ensure we eliminate all the useless code. */
1127 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1130 remove_useless_stmts_warn_notreached (tree stmt)
1132 if (EXPR_HAS_LOCATION (stmt))
1134 location_t loc = EXPR_LOCATION (stmt);
1135 warning ("%Hwill never be executed", &loc);
1139 switch (TREE_CODE (stmt))
1141 case STATEMENT_LIST:
1143 tree_stmt_iterator i;
1144 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1145 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1151 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1153 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1155 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1159 case TRY_FINALLY_EXPR:
1160 case TRY_CATCH_EXPR:
1161 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1163 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1168 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1169 case EH_FILTER_EXPR:
1170 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1172 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1175 /* Not a live container. */
1183 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1185 tree then_clause, else_clause, cond;
1186 bool save_has_label, then_has_label, else_has_label;
1188 save_has_label = data->has_label;
1189 data->has_label = false;
1190 data->last_goto = NULL;
1192 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1194 then_has_label = data->has_label;
1195 data->has_label = false;
1196 data->last_goto = NULL;
1198 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1200 else_has_label = data->has_label;
1201 data->has_label = save_has_label | then_has_label | else_has_label;
1203 then_clause = COND_EXPR_THEN (*stmt_p);
1204 else_clause = COND_EXPR_ELSE (*stmt_p);
1205 cond = COND_EXPR_COND (*stmt_p);
1207 /* If neither arm does anything at all, we can remove the whole IF. */
1208 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1210 *stmt_p = build_empty_stmt ();
1211 data->repeat = true;
1214 /* If there are no reachable statements in an arm, then we can
1215 zap the entire conditional. */
1216 else if (integer_nonzerop (cond) && !else_has_label)
1218 if (warn_notreached)
1219 remove_useless_stmts_warn_notreached (else_clause);
1220 *stmt_p = then_clause;
1221 data->repeat = true;
1223 else if (integer_zerop (cond) && !then_has_label)
1225 if (warn_notreached)
1226 remove_useless_stmts_warn_notreached (then_clause);
1227 *stmt_p = else_clause;
1228 data->repeat = true;
1231 /* Check a couple of simple things on then/else with single stmts. */
1234 tree then_stmt = expr_only (then_clause);
1235 tree else_stmt = expr_only (else_clause);
1237 /* Notice branches to a common destination. */
1238 if (then_stmt && else_stmt
1239 && TREE_CODE (then_stmt) == GOTO_EXPR
1240 && TREE_CODE (else_stmt) == GOTO_EXPR
1241 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1243 *stmt_p = then_stmt;
1244 data->repeat = true;
1247 /* If the THEN/ELSE clause merely assigns a value to a variable or
1248 parameter which is already known to contain that value, then
1249 remove the useless THEN/ELSE clause. */
1250 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1253 && TREE_CODE (else_stmt) == MODIFY_EXPR
1254 && TREE_OPERAND (else_stmt, 0) == cond
1255 && integer_zerop (TREE_OPERAND (else_stmt, 1)))
1256 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1258 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1259 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1260 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1261 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1263 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1264 ? then_stmt : else_stmt);
1265 tree *location = (TREE_CODE (cond) == EQ_EXPR
1266 ? &COND_EXPR_THEN (*stmt_p)
1267 : &COND_EXPR_ELSE (*stmt_p));
1270 && TREE_CODE (stmt) == MODIFY_EXPR
1271 && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1272 && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1273 *location = alloc_stmt_list ();
1277 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1278 would be re-introduced during lowering. */
1279 data->last_goto = NULL;
1284 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1286 bool save_may_branch, save_may_throw;
1287 bool this_may_branch, this_may_throw;
1289 /* Collect may_branch and may_throw information for the body only. */
1290 save_may_branch = data->may_branch;
1291 save_may_throw = data->may_throw;
1292 data->may_branch = false;
1293 data->may_throw = false;
1294 data->last_goto = NULL;
1296 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1298 this_may_branch = data->may_branch;
1299 this_may_throw = data->may_throw;
1300 data->may_branch |= save_may_branch;
1301 data->may_throw |= save_may_throw;
1302 data->last_goto = NULL;
1304 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1306 /* If the body is empty, then we can emit the FINALLY block without
1307 the enclosing TRY_FINALLY_EXPR. */
1308 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1310 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1311 data->repeat = true;
1314 /* If the handler is empty, then we can emit the TRY block without
1315 the enclosing TRY_FINALLY_EXPR. */
1316 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1318 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1319 data->repeat = true;
1322 /* If the body neither throws, nor branches, then we can safely
1323 string the TRY and FINALLY blocks together. */
1324 else if (!this_may_branch && !this_may_throw)
1326 tree stmt = *stmt_p;
1327 *stmt_p = TREE_OPERAND (stmt, 0);
1328 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1329 data->repeat = true;
1335 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1337 bool save_may_throw, this_may_throw;
1338 tree_stmt_iterator i;
1341 /* Collect may_throw information for the body only. */
1342 save_may_throw = data->may_throw;
1343 data->may_throw = false;
1344 data->last_goto = NULL;
1346 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1348 this_may_throw = data->may_throw;
1349 data->may_throw = save_may_throw;
1351 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1352 if (!this_may_throw)
1354 if (warn_notreached)
1355 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1356 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1357 data->repeat = true;
1361 /* Process the catch clause specially. We may be able to tell that
1362 no exceptions propagate past this point. */
1364 this_may_throw = true;
1365 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1366 stmt = tsi_stmt (i);
1367 data->last_goto = NULL;
1369 switch (TREE_CODE (stmt))
1372 for (; !tsi_end_p (i); tsi_next (&i))
1374 stmt = tsi_stmt (i);
1375 /* If we catch all exceptions, then the body does not
1376 propagate exceptions past this point. */
1377 if (CATCH_TYPES (stmt) == NULL)
1378 this_may_throw = false;
1379 data->last_goto = NULL;
1380 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1384 case EH_FILTER_EXPR:
1385 if (EH_FILTER_MUST_NOT_THROW (stmt))
1386 this_may_throw = false;
1387 else if (EH_FILTER_TYPES (stmt) == NULL)
1388 this_may_throw = false;
1389 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1393 /* Otherwise this is a cleanup. */
1394 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1396 /* If the cleanup is empty, then we can emit the TRY block without
1397 the enclosing TRY_CATCH_EXPR. */
1398 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1400 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1401 data->repeat = true;
1405 data->may_throw |= this_may_throw;
1410 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1414 /* First remove anything underneath the BIND_EXPR. */
1415 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1417 /* If the BIND_EXPR has no variables, then we can pull everything
1418 up one level and remove the BIND_EXPR, unless this is the toplevel
1419 BIND_EXPR for the current function or an inlined function.
1421 When this situation occurs we will want to apply this
1422 optimization again. */
1423 block = BIND_EXPR_BLOCK (*stmt_p);
1424 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1425 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1427 || ! BLOCK_ABSTRACT_ORIGIN (block)
1428 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1431 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1432 data->repeat = true;
1438 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1440 tree dest = GOTO_DESTINATION (*stmt_p);
1442 data->may_branch = true;
1443 data->last_goto = NULL;
1445 /* Record the last goto expr, so that we can delete it if unnecessary. */
1446 if (TREE_CODE (dest) == LABEL_DECL)
1447 data->last_goto = stmt_p;
1452 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1454 tree label = LABEL_EXPR_LABEL (*stmt_p);
1456 data->has_label = true;
1458 /* We do want to jump across non-local label receiver code. */
1459 if (DECL_NONLOCAL (label))
1460 data->last_goto = NULL;
1462 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1464 *data->last_goto = build_empty_stmt ();
1465 data->repeat = true;
1468 /* ??? Add something here to delete unused labels. */
1472 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1473 decl. This allows us to eliminate redundant or useless
1474 calls to "const" functions.
1476 Gimplifier already does the same operation, but we may notice functions
1477 being const and pure once their calls has been gimplified, so we need
1478 to update the flag. */
1481 update_call_expr_flags (tree call)
1483 tree decl = get_callee_fndecl (call);
1486 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1487 TREE_SIDE_EFFECTS (call) = 0;
1488 if (TREE_NOTHROW (decl))
1489 TREE_NOTHROW (call) = 1;
1493 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1496 notice_special_calls (tree t)
1498 int flags = call_expr_flags (t);
1500 if (flags & ECF_MAY_BE_ALLOCA)
1501 current_function_calls_alloca = true;
1502 if (flags & ECF_RETURNS_TWICE)
1503 current_function_calls_setjmp = true;
1507 /* Clear flags set by notice_special_calls. Used by dead code removal
1508 to update the flags. */
1511 clear_special_calls (void)
1513 current_function_calls_alloca = false;
1514 current_function_calls_setjmp = false;
1519 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1523 switch (TREE_CODE (t))
1526 remove_useless_stmts_cond (tp, data);
1529 case TRY_FINALLY_EXPR:
1530 remove_useless_stmts_tf (tp, data);
1533 case TRY_CATCH_EXPR:
1534 remove_useless_stmts_tc (tp, data);
1538 remove_useless_stmts_bind (tp, data);
1542 remove_useless_stmts_goto (tp, data);
1546 remove_useless_stmts_label (tp, data);
1551 data->last_goto = NULL;
1552 data->may_branch = true;
1557 data->last_goto = NULL;
1558 notice_special_calls (t);
1559 update_call_expr_flags (t);
1560 if (tree_could_throw_p (t))
1561 data->may_throw = true;
1565 data->last_goto = NULL;
1567 op = get_call_expr_in (t);
1570 update_call_expr_flags (op);
1571 notice_special_calls (op);
1573 if (tree_could_throw_p (t))
1574 data->may_throw = true;
1577 case STATEMENT_LIST:
1579 tree_stmt_iterator i = tsi_start (t);
1580 while (!tsi_end_p (i))
1583 if (IS_EMPTY_STMT (t))
1589 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1592 if (TREE_CODE (t) == STATEMENT_LIST)
1594 tsi_link_before (&i, t, TSI_SAME_STMT);
1604 data->last_goto = NULL;
1608 data->last_goto = NULL;
1614 remove_useless_stmts (void)
1616 struct rus_data data;
1618 clear_special_calls ();
1622 memset (&data, 0, sizeof (data));
1623 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1625 while (data.repeat);
1629 struct tree_opt_pass pass_remove_useless_stmts =
1631 "useless", /* name */
1633 remove_useless_stmts, /* execute */
1636 0, /* static_pass_number */
1638 PROP_gimple_any, /* properties_required */
1639 0, /* properties_provided */
1640 0, /* properties_destroyed */
1641 0, /* todo_flags_start */
1642 TODO_dump_func, /* todo_flags_finish */
1647 /* Remove obviously useless statements in basic block BB. */
1650 cfg_remove_useless_stmts_bb (basic_block bb)
1652 block_stmt_iterator bsi;
1653 tree stmt = NULL_TREE;
1654 tree cond, var = NULL_TREE, val = NULL_TREE;
1655 struct var_ann_d *ann;
1657 /* Check whether we come here from a condition, and if so, get the
1659 if (EDGE_COUNT (bb->preds) != 1
1660 || !(EDGE_PRED (bb, 0)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1663 cond = COND_EXPR_COND (last_stmt (EDGE_PRED (bb, 0)->src));
1665 if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1668 val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
1669 ? boolean_false_node : boolean_true_node);
1671 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR
1672 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1673 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL))
1675 var = TREE_OPERAND (cond, 0);
1676 val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
1677 ? boolean_true_node : boolean_false_node);
1681 if (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE)
1682 cond = invert_truthvalue (cond);
1683 if (TREE_CODE (cond) == EQ_EXPR
1684 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1685 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1686 && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL
1687 || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL
1688 || TREE_CONSTANT (TREE_OPERAND (cond, 1))))
1690 var = TREE_OPERAND (cond, 0);
1691 val = TREE_OPERAND (cond, 1);
1697 /* Only work for normal local variables. */
1698 ann = var_ann (var);
1701 || TREE_ADDRESSABLE (var))
1704 if (! TREE_CONSTANT (val))
1706 ann = var_ann (val);
1709 || TREE_ADDRESSABLE (val))
1713 /* Ignore floating point variables, since comparison behaves weird for
1715 if (FLOAT_TYPE_P (TREE_TYPE (var)))
1718 for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
1720 stmt = bsi_stmt (bsi);
1722 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1723 which is already known to contain that value, then remove the useless
1724 THEN/ELSE clause. */
1725 if (TREE_CODE (stmt) == MODIFY_EXPR
1726 && TREE_OPERAND (stmt, 0) == var
1727 && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0))
1733 /* Invalidate the var if we encounter something that could modify it.
1734 Likewise for the value it was previously set to. Note that we only
1735 consider values that are either a VAR_DECL or PARM_DECL so we
1736 can test for conflict very simply. */
1737 if (TREE_CODE (stmt) == ASM_EXPR
1738 || (TREE_CODE (stmt) == MODIFY_EXPR
1739 && (TREE_OPERAND (stmt, 0) == var
1740 || TREE_OPERAND (stmt, 0) == val)))
1748 /* A CFG-aware version of remove_useless_stmts. */
1751 cfg_remove_useless_stmts (void)
1755 #ifdef ENABLE_CHECKING
1756 verify_flow_info ();
1761 cfg_remove_useless_stmts_bb (bb);
1766 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1769 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1773 /* Since this block is no longer reachable, we can just delete all
1774 of its PHI nodes. */
1775 phi = phi_nodes (bb);
1778 tree next = PHI_CHAIN (phi);
1779 remove_phi_node (phi, NULL_TREE, bb);
1783 /* Remove edges to BB's successors. */
1784 while (EDGE_COUNT (bb->succs) > 0)
1785 ssa_remove_edge (EDGE_SUCC (bb, 0));
1789 /* Remove statements of basic block BB. */
1792 remove_bb (basic_block bb)
1794 block_stmt_iterator i;
1795 source_locus loc = 0;
1799 fprintf (dump_file, "Removing basic block %d\n", bb->index);
1800 if (dump_flags & TDF_DETAILS)
1802 dump_bb (bb, dump_file, 0);
1803 fprintf (dump_file, "\n");
1807 /* Remove all the instructions in the block. */
1808 for (i = bsi_start (bb); !bsi_end_p (i);)
1810 tree stmt = bsi_stmt (i);
1811 if (TREE_CODE (stmt) == LABEL_EXPR
1812 && FORCED_LABEL (LABEL_EXPR_LABEL (stmt)))
1814 basic_block new_bb = bb->prev_bb;
1815 block_stmt_iterator new_bsi = bsi_after_labels (new_bb);
1818 bsi_insert_after (&new_bsi, stmt, BSI_NEW_STMT);
1822 release_defs (stmt);
1824 set_bb_for_stmt (stmt, NULL);
1828 /* Don't warn for removed gotos. Gotos are often removed due to
1829 jump threading, thus resulting in bogus warnings. Not great,
1830 since this way we lose warnings for gotos in the original
1831 program that are indeed unreachable. */
1832 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
1833 #ifdef USE_MAPPED_LOCATION
1834 loc = EXPR_LOCATION (stmt);
1836 loc = EXPR_LOCUS (stmt);
1840 /* If requested, give a warning that the first statement in the
1841 block is unreachable. We walk statements backwards in the
1842 loop above, so the last statement we process is the first statement
1844 if (warn_notreached && loc)
1845 #ifdef USE_MAPPED_LOCATION
1846 warning ("%Hwill never be executed", &loc);
1848 warning ("%Hwill never be executed", loc);
1851 remove_phi_nodes_and_edges_for_unreachable_block (bb);
1854 /* Try to remove superfluous control structures. */
1857 cleanup_control_flow (void)
1860 block_stmt_iterator bsi;
1861 bool retval = false;
1866 bsi = bsi_last (bb);
1868 if (bsi_end_p (bsi))
1871 stmt = bsi_stmt (bsi);
1872 if (TREE_CODE (stmt) == COND_EXPR
1873 || TREE_CODE (stmt) == SWITCH_EXPR)
1874 retval |= cleanup_control_expr_graph (bb, bsi);
1880 /* Disconnect an unreachable block in the control expression starting
1884 cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
1887 bool retval = false;
1888 tree expr = bsi_stmt (bsi), val;
1890 if (EDGE_COUNT (bb->succs) > 1)
1895 switch (TREE_CODE (expr))
1898 val = COND_EXPR_COND (expr);
1902 val = SWITCH_COND (expr);
1903 if (TREE_CODE (val) != INTEGER_CST)
1911 taken_edge = find_taken_edge (bb, val);
1915 /* Remove all the edges except the one that is always executed. */
1916 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1918 if (e != taken_edge)
1920 taken_edge->probability += e->probability;
1921 taken_edge->count += e->count;
1922 ssa_remove_edge (e);
1928 if (taken_edge->probability > REG_BR_PROB_BASE)
1929 taken_edge->probability = REG_BR_PROB_BASE;
1932 taken_edge = EDGE_SUCC (bb, 0);
1935 taken_edge->flags = EDGE_FALLTHRU;
1937 /* We removed some paths from the cfg. */
1938 free_dominance_info (CDI_DOMINATORS);
1944 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
1945 predicate VAL, return the edge that will be taken out of the block.
1946 If VAL does not match a unique edge, NULL is returned. */
1949 find_taken_edge (basic_block bb, tree val)
1953 stmt = last_stmt (bb);
1956 gcc_assert (is_ctrl_stmt (stmt));
1959 /* If VAL is a predicate of the form N RELOP N, where N is an
1960 SSA_NAME, we can usually determine its truth value. */
1961 if (COMPARISON_CLASS_P (val))
1964 /* If VAL is not a constant, we can't determine which edge might
1966 if (!really_constant_p (val))
1969 if (TREE_CODE (stmt) == COND_EXPR)
1970 return find_taken_edge_cond_expr (bb, val);
1972 if (TREE_CODE (stmt) == SWITCH_EXPR)
1973 return find_taken_edge_switch_expr (bb, val);
1979 /* Given a constant value VAL and the entry block BB to a COND_EXPR
1980 statement, determine which of the two edges will be taken out of the
1981 block. Return NULL if either edge may be taken. */
1984 find_taken_edge_cond_expr (basic_block bb, tree val)
1986 edge true_edge, false_edge;
1988 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1990 /* Otherwise, try to determine which branch of the if() will be taken.
1991 If VAL is a constant but it can't be reduced to a 0 or a 1, then
1992 we don't really know which edge will be taken at runtime. This
1993 may happen when comparing addresses (e.g., if (&var1 == 4)). */
1994 if (integer_nonzerop (val))
1996 else if (integer_zerop (val))
2003 /* Given a constant value VAL and the entry block BB to a SWITCH_EXPR
2004 statement, determine which edge will be taken out of the block. Return
2005 NULL if any edge may be taken. */
2008 find_taken_edge_switch_expr (basic_block bb, tree val)
2010 tree switch_expr, taken_case;
2011 basic_block dest_bb;
2014 if (TREE_CODE (val) != INTEGER_CST)
2017 switch_expr = last_stmt (bb);
2018 taken_case = find_case_label_for_value (switch_expr, val);
2019 dest_bb = label_to_block (CASE_LABEL (taken_case));
2021 e = find_edge (bb, dest_bb);
2027 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2028 We can make optimal use here of the fact that the case labels are
2029 sorted: We can do a binary search for a case matching VAL. */
2032 find_case_label_for_value (tree switch_expr, tree val)
2034 tree vec = SWITCH_LABELS (switch_expr);
2035 size_t low, high, n = TREE_VEC_LENGTH (vec);
2036 tree default_case = TREE_VEC_ELT (vec, n - 1);
2038 for (low = -1, high = n - 1; high - low > 1; )
2040 size_t i = (high + low) / 2;
2041 tree t = TREE_VEC_ELT (vec, i);
2044 /* Cache the result of comparing CASE_LOW and val. */
2045 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2052 if (CASE_HIGH (t) == NULL)
2054 /* A singe-valued case label. */
2060 /* A case range. We can only handle integer ranges. */
2061 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2066 return default_case;
2070 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2071 those alternatives are equal in each of the PHI nodes, then return
2072 true, else return false. */
2075 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
2077 tree phi, val1, val2;
2080 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2082 n1 = phi_arg_from_edge (phi, e1);
2083 n2 = phi_arg_from_edge (phi, e2);
2085 gcc_assert (n1 >= 0);
2086 gcc_assert (n2 >= 0);
2088 val1 = PHI_ARG_DEF (phi, n1);
2089 val2 = PHI_ARG_DEF (phi, n2);
2091 if (!operand_equal_p (val1, val2, 0))
2099 /*---------------------------------------------------------------------------
2101 ---------------------------------------------------------------------------*/
2103 /* Dump tree-specific information of block BB to file OUTF. */
2106 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2108 dump_generic_bb (outf, bb, indent, TDF_VOPS);
2112 /* Dump a basic block on stderr. */
2115 debug_tree_bb (basic_block bb)
2117 dump_bb (bb, stderr, 0);
2121 /* Dump basic block with index N on stderr. */
2124 debug_tree_bb_n (int n)
2126 debug_tree_bb (BASIC_BLOCK (n));
2127 return BASIC_BLOCK (n);
2131 /* Dump the CFG on stderr.
2133 FLAGS are the same used by the tree dumping functions
2134 (see TDF_* in tree.h). */
2137 debug_tree_cfg (int flags)
2139 dump_tree_cfg (stderr, flags);
2143 /* Dump the program showing basic block boundaries on the given FILE.
2145 FLAGS are the same used by the tree dumping functions (see TDF_* in
2149 dump_tree_cfg (FILE *file, int flags)
2151 if (flags & TDF_DETAILS)
2153 const char *funcname
2154 = lang_hooks.decl_printable_name (current_function_decl, 2);
2157 fprintf (file, ";; Function %s\n\n", funcname);
2158 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2159 n_basic_blocks, n_edges, last_basic_block);
2161 brief_dump_cfg (file);
2162 fprintf (file, "\n");
2165 if (flags & TDF_STATS)
2166 dump_cfg_stats (file);
2168 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2172 /* Dump CFG statistics on FILE. */
2175 dump_cfg_stats (FILE *file)
2177 static long max_num_merged_labels = 0;
2178 unsigned long size, total = 0;
2181 const char * const fmt_str = "%-30s%-13s%12s\n";
2182 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2183 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2184 const char *funcname
2185 = lang_hooks.decl_printable_name (current_function_decl, 2);
2188 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2190 fprintf (file, "---------------------------------------------------------\n");
2191 fprintf (file, fmt_str, "", " Number of ", "Memory");
2192 fprintf (file, fmt_str, "", " instances ", "used ");
2193 fprintf (file, "---------------------------------------------------------\n");
2195 size = n_basic_blocks * sizeof (struct basic_block_def);
2197 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2198 SCALE (size), LABEL (size));
2202 n_edges += EDGE_COUNT (bb->succs);
2203 size = n_edges * sizeof (struct edge_def);
2205 fprintf (file, fmt_str_1, "Edges", n_edges, SCALE (size), LABEL (size));
2207 size = n_basic_blocks * sizeof (struct bb_ann_d);
2209 fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks,
2210 SCALE (size), LABEL (size));
2212 fprintf (file, "---------------------------------------------------------\n");
2213 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2215 fprintf (file, "---------------------------------------------------------\n");
2216 fprintf (file, "\n");
2218 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2219 max_num_merged_labels = cfg_stats.num_merged_labels;
2221 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2222 cfg_stats.num_merged_labels, max_num_merged_labels);
2224 fprintf (file, "\n");
2228 /* Dump CFG statistics on stderr. Keep extern so that it's always
2229 linked in the final executable. */
2232 debug_cfg_stats (void)
2234 dump_cfg_stats (stderr);
2238 /* Dump the flowgraph to a .vcg FILE. */
2241 tree_cfg2vcg (FILE *file)
2246 const char *funcname
2247 = lang_hooks.decl_printable_name (current_function_decl, 2);
2249 /* Write the file header. */
2250 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2251 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2252 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2254 /* Write blocks and edges. */
2255 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2257 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2260 if (e->flags & EDGE_FAKE)
2261 fprintf (file, " linestyle: dotted priority: 10");
2263 fprintf (file, " linestyle: solid priority: 100");
2265 fprintf (file, " }\n");
2271 enum tree_code head_code, end_code;
2272 const char *head_name, *end_name;
2275 tree first = first_stmt (bb);
2276 tree last = last_stmt (bb);
2280 head_code = TREE_CODE (first);
2281 head_name = tree_code_name[head_code];
2282 head_line = get_lineno (first);
2285 head_name = "no-statement";
2289 end_code = TREE_CODE (last);
2290 end_name = tree_code_name[end_code];
2291 end_line = get_lineno (last);
2294 end_name = "no-statement";
2296 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2297 bb->index, bb->index, head_name, head_line, end_name,
2300 FOR_EACH_EDGE (e, ei, bb->succs)
2302 if (e->dest == EXIT_BLOCK_PTR)
2303 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2305 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2307 if (e->flags & EDGE_FAKE)
2308 fprintf (file, " priority: 10 linestyle: dotted");
2310 fprintf (file, " priority: 100 linestyle: solid");
2312 fprintf (file, " }\n");
2315 if (bb->next_bb != EXIT_BLOCK_PTR)
2319 fputs ("}\n\n", file);
2324 /*---------------------------------------------------------------------------
2325 Miscellaneous helpers
2326 ---------------------------------------------------------------------------*/
2328 /* Return true if T represents a stmt that always transfers control. */
2331 is_ctrl_stmt (tree t)
2333 return (TREE_CODE (t) == COND_EXPR
2334 || TREE_CODE (t) == SWITCH_EXPR
2335 || TREE_CODE (t) == GOTO_EXPR
2336 || TREE_CODE (t) == RETURN_EXPR
2337 || TREE_CODE (t) == RESX_EXPR);
2341 /* Return true if T is a statement that may alter the flow of control
2342 (e.g., a call to a non-returning function). */
2345 is_ctrl_altering_stmt (tree t)
2350 call = get_call_expr_in (t);
2353 /* A non-pure/const CALL_EXPR alters flow control if the current
2354 function has nonlocal labels. */
2355 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2358 /* A CALL_EXPR also alters control flow if it does not return. */
2359 if (call_expr_flags (call) & (ECF_NORETURN | ECF_LONGJMP))
2363 /* If a statement can throw, it alters control flow. */
2364 return tree_can_throw_internal (t);
2368 /* Return true if T is a computed goto. */
2371 computed_goto_p (tree t)
2373 return (TREE_CODE (t) == GOTO_EXPR
2374 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2378 /* Checks whether EXPR is a simple local goto. */
2381 simple_goto_p (tree expr)
2383 return (TREE_CODE (expr) == GOTO_EXPR
2384 && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL);
2388 /* Return true if T should start a new basic block. PREV_T is the
2389 statement preceding T. It is used when T is a label or a case label.
2390 Labels should only start a new basic block if their previous statement
2391 wasn't a label. Otherwise, sequence of labels would generate
2392 unnecessary basic blocks that only contain a single label. */
2395 stmt_starts_bb_p (tree t, tree prev_t)
2397 enum tree_code code;
2402 /* LABEL_EXPRs start a new basic block only if the preceding
2403 statement wasn't a label of the same type. This prevents the
2404 creation of consecutive blocks that have nothing but a single
2406 code = TREE_CODE (t);
2407 if (code == LABEL_EXPR)
2409 /* Nonlocal and computed GOTO targets always start a new block. */
2410 if (code == LABEL_EXPR
2411 && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2412 || FORCED_LABEL (LABEL_EXPR_LABEL (t))))
2415 if (prev_t && TREE_CODE (prev_t) == code)
2417 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2420 cfg_stats.num_merged_labels++;
2431 /* Return true if T should end a basic block. */
2434 stmt_ends_bb_p (tree t)
2436 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2440 /* Add gotos that used to be represented implicitly in the CFG. */
2443 disband_implicit_edges (void)
2446 block_stmt_iterator last;
2453 last = bsi_last (bb);
2454 stmt = last_stmt (bb);
2456 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2458 /* Remove superfluous gotos from COND_EXPR branches. Moved
2459 from cfg_remove_useless_stmts here since it violates the
2460 invariants for tree--cfg correspondence and thus fits better
2461 here where we do it anyway. */
2462 FOR_EACH_EDGE (e, ei, bb->succs)
2464 if (e->dest != bb->next_bb)
2467 if (e->flags & EDGE_TRUE_VALUE)
2468 COND_EXPR_THEN (stmt) = build_empty_stmt ();
2469 else if (e->flags & EDGE_FALSE_VALUE)
2470 COND_EXPR_ELSE (stmt) = build_empty_stmt ();
2473 e->flags |= EDGE_FALLTHRU;
2479 if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
2481 /* Remove the RETURN_EXPR if we may fall though to the exit
2483 gcc_assert (EDGE_COUNT (bb->succs) == 1);
2484 gcc_assert (EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR);
2486 if (bb->next_bb == EXIT_BLOCK_PTR
2487 && !TREE_OPERAND (stmt, 0))
2490 EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
2495 /* There can be no fallthru edge if the last statement is a control
2497 if (stmt && is_ctrl_stmt (stmt))
2500 /* Find a fallthru edge and emit the goto if necessary. */
2501 FOR_EACH_EDGE (e, ei, bb->succs)
2502 if (e->flags & EDGE_FALLTHRU)
2505 if (!e || e->dest == bb->next_bb)
2508 gcc_assert (e->dest != EXIT_BLOCK_PTR);
2509 label = tree_block_label (e->dest);
2511 stmt = build1 (GOTO_EXPR, void_type_node, label);
2512 #ifdef USE_MAPPED_LOCATION
2513 SET_EXPR_LOCATION (stmt, e->goto_locus);
2515 SET_EXPR_LOCUS (stmt, e->goto_locus);
2517 bsi_insert_after (&last, stmt, BSI_NEW_STMT);
2518 e->flags &= ~EDGE_FALLTHRU;
2522 /* Remove block annotations and other datastructures. */
2525 delete_tree_cfg_annotations (void)
2528 if (n_basic_blocks > 0)
2529 free_blocks_annotations ();
2531 label_to_block_map = NULL;
2538 /* Return the first statement in basic block BB. */
2541 first_stmt (basic_block bb)
2543 block_stmt_iterator i = bsi_start (bb);
2544 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2548 /* Return the last statement in basic block BB. */
2551 last_stmt (basic_block bb)
2553 block_stmt_iterator b = bsi_last (bb);
2554 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2558 /* Return a pointer to the last statement in block BB. */
2561 last_stmt_ptr (basic_block bb)
2563 block_stmt_iterator last = bsi_last (bb);
2564 return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL;
2568 /* Return the last statement of an otherwise empty block. Return NULL
2569 if the block is totally empty, or if it contains more than one
2573 last_and_only_stmt (basic_block bb)
2575 block_stmt_iterator i = bsi_last (bb);
2581 last = bsi_stmt (i);
2586 /* Empty statements should no longer appear in the instruction stream.
2587 Everything that might have appeared before should be deleted by
2588 remove_useless_stmts, and the optimizers should just bsi_remove
2589 instead of smashing with build_empty_stmt.
2591 Thus the only thing that should appear here in a block containing
2592 one executable statement is a label. */
2593 prev = bsi_stmt (i);
2594 if (TREE_CODE (prev) == LABEL_EXPR)
2601 /* Mark BB as the basic block holding statement T. */
2604 set_bb_for_stmt (tree t, basic_block bb)
2606 if (TREE_CODE (t) == PHI_NODE)
2608 else if (TREE_CODE (t) == STATEMENT_LIST)
2610 tree_stmt_iterator i;
2611 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2612 set_bb_for_stmt (tsi_stmt (i), bb);
2616 stmt_ann_t ann = get_stmt_ann (t);
2619 /* If the statement is a label, add the label to block-to-labels map
2620 so that we can speed up edge creation for GOTO_EXPRs. */
2621 if (TREE_CODE (t) == LABEL_EXPR)
2625 t = LABEL_EXPR_LABEL (t);
2626 uid = LABEL_DECL_UID (t);
2629 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2630 if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid)
2631 VARRAY_GROW (label_to_block_map, 3 * uid / 2);
2634 /* We're moving an existing label. Make sure that we've
2635 removed it from the old block. */
2636 gcc_assert (!bb || !VARRAY_BB (label_to_block_map, uid));
2637 VARRAY_BB (label_to_block_map, uid) = bb;
2642 /* Finds iterator for STMT. */
2644 extern block_stmt_iterator
2645 bsi_for_stmt (tree stmt)
2647 block_stmt_iterator bsi;
2649 for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
2650 if (bsi_stmt (bsi) == stmt)
2656 /* Insert statement (or statement list) T before the statement
2657 pointed-to by iterator I. M specifies how to update iterator I
2658 after insertion (see enum bsi_iterator_update). */
2661 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2663 set_bb_for_stmt (t, i->bb);
2664 tsi_link_before (&i->tsi, t, m);
2669 /* Insert statement (or statement list) T after the statement
2670 pointed-to by iterator I. M specifies how to update iterator I
2671 after insertion (see enum bsi_iterator_update). */
2674 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2676 set_bb_for_stmt (t, i->bb);
2677 tsi_link_after (&i->tsi, t, m);
2682 /* Remove the statement pointed to by iterator I. The iterator is updated
2683 to the next statement. */
2686 bsi_remove (block_stmt_iterator *i)
2688 tree t = bsi_stmt (*i);
2689 set_bb_for_stmt (t, NULL);
2690 tsi_delink (&i->tsi);
2694 /* Move the statement at FROM so it comes right after the statement at TO. */
2697 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
2699 tree stmt = bsi_stmt (*from);
2701 bsi_insert_after (to, stmt, BSI_SAME_STMT);
2705 /* Move the statement at FROM so it comes right before the statement at TO. */
2708 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
2710 tree stmt = bsi_stmt (*from);
2712 bsi_insert_before (to, stmt, BSI_SAME_STMT);
2716 /* Move the statement at FROM to the end of basic block BB. */
2719 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
2721 block_stmt_iterator last = bsi_last (bb);
2723 /* Have to check bsi_end_p because it could be an empty block. */
2724 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
2725 bsi_move_before (from, &last);
2727 bsi_move_after (from, &last);
2731 /* Replace the contents of the statement pointed to by iterator BSI
2732 with STMT. If PRESERVE_EH_INFO is true, the exception handling
2733 information of the original statement is preserved. */
2736 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
2739 tree orig_stmt = bsi_stmt (*bsi);
2741 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
2742 set_bb_for_stmt (stmt, bsi->bb);
2744 /* Preserve EH region information from the original statement, if
2745 requested by the caller. */
2746 if (preserve_eh_info)
2748 eh_region = lookup_stmt_eh_region (orig_stmt);
2750 add_stmt_to_eh_region (stmt, eh_region);
2753 *bsi_stmt_ptr (*bsi) = stmt;
2758 /* Insert the statement pointed-to by BSI into edge E. Every attempt
2759 is made to place the statement in an existing basic block, but
2760 sometimes that isn't possible. When it isn't possible, the edge is
2761 split and the statement is added to the new block.
2763 In all cases, the returned *BSI points to the correct location. The
2764 return value is true if insertion should be done after the location,
2765 or false if it should be done before the location. If new basic block
2766 has to be created, it is stored in *NEW_BB. */
2769 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
2770 basic_block *new_bb)
2772 basic_block dest, src;
2778 /* If the destination has one predecessor which has no PHI nodes,
2779 insert there. Except for the exit block.
2781 The requirement for no PHI nodes could be relaxed. Basically we
2782 would have to examine the PHIs to prove that none of them used
2783 the value set by the statement we want to insert on E. That
2784 hardly seems worth the effort. */
2785 if (EDGE_COUNT (dest->preds) == 1
2786 && ! phi_nodes (dest)
2787 && dest != EXIT_BLOCK_PTR)
2789 *bsi = bsi_start (dest);
2790 if (bsi_end_p (*bsi))
2793 /* Make sure we insert after any leading labels. */
2794 tmp = bsi_stmt (*bsi);
2795 while (TREE_CODE (tmp) == LABEL_EXPR)
2798 if (bsi_end_p (*bsi))
2800 tmp = bsi_stmt (*bsi);
2803 if (bsi_end_p (*bsi))
2805 *bsi = bsi_last (dest);
2812 /* If the source has one successor, the edge is not abnormal and
2813 the last statement does not end a basic block, insert there.
2814 Except for the entry block. */
2816 if ((e->flags & EDGE_ABNORMAL) == 0
2817 && EDGE_COUNT (src->succs) == 1
2818 && src != ENTRY_BLOCK_PTR)
2820 *bsi = bsi_last (src);
2821 if (bsi_end_p (*bsi))
2824 tmp = bsi_stmt (*bsi);
2825 if (!stmt_ends_bb_p (tmp))
2828 /* Insert code just before returning the value. We may need to decompose
2829 the return in the case it contains non-trivial operand. */
2830 if (TREE_CODE (tmp) == RETURN_EXPR)
2832 tree op = TREE_OPERAND (tmp, 0);
2833 if (!is_gimple_val (op))
2835 gcc_assert (TREE_CODE (op) == MODIFY_EXPR);
2836 bsi_insert_before (bsi, op, BSI_NEW_STMT);
2837 TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0);
2844 /* Otherwise, create a new basic block, and split this edge. */
2845 dest = split_edge (e);
2848 e = EDGE_PRED (dest, 0);
2853 /* This routine will commit all pending edge insertions, creating any new
2854 basic blocks which are necessary.
2856 If specified, NEW_BLOCKS returns a count of the number of new basic
2857 blocks which were created. */
2860 bsi_commit_edge_inserts (int *new_blocks)
2867 blocks = n_basic_blocks;
2869 bsi_commit_one_edge_insert (EDGE_SUCC (ENTRY_BLOCK_PTR, 0), NULL);
2872 FOR_EACH_EDGE (e, ei, bb->succs)
2873 bsi_commit_one_edge_insert (e, NULL);
2876 *new_blocks = n_basic_blocks - blocks;
2880 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
2881 to this block, otherwise set it to NULL. */
2884 bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
2888 if (PENDING_STMT (e))
2890 block_stmt_iterator bsi;
2891 tree stmt = PENDING_STMT (e);
2893 PENDING_STMT (e) = NULL_TREE;
2895 if (tree_find_edge_insert_loc (e, &bsi, new_bb))
2896 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2898 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2903 /* Add STMT to the pending list of edge E. No actual insertion is
2904 made until a call to bsi_commit_edge_inserts () is made. */
2907 bsi_insert_on_edge (edge e, tree stmt)
2909 append_to_statement_list (stmt, &PENDING_STMT (e));
2912 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If new block has to
2913 be created, it is returned. */
2916 bsi_insert_on_edge_immediate (edge e, tree stmt)
2918 block_stmt_iterator bsi;
2919 basic_block new_bb = NULL;
2921 gcc_assert (!PENDING_STMT (e));
2923 if (tree_find_edge_insert_loc (e, &bsi, &new_bb))
2924 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2926 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2931 /*---------------------------------------------------------------------------
2932 Tree specific functions for CFG manipulation
2933 ---------------------------------------------------------------------------*/
2935 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2936 Abort on abnormal edges. */
2939 tree_split_edge (edge edge_in)
2941 basic_block new_bb, after_bb, dest, src;
2947 /* Abnormal edges cannot be split. */
2948 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2951 dest = edge_in->dest;
2953 /* Place the new block in the block list. Try to keep the new block
2954 near its "logical" location. This is of most help to humans looking
2955 at debugging dumps. */
2956 FOR_EACH_EDGE (e, ei, dest->preds)
2957 if (e->src->next_bb == dest)
2960 after_bb = dest->prev_bb;
2962 after_bb = edge_in->src;
2964 new_bb = create_empty_bb (after_bb);
2965 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2966 new_bb->count = edge_in->count;
2967 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2968 new_edge->probability = REG_BR_PROB_BASE;
2969 new_edge->count = edge_in->count;
2971 /* Find all the PHI arguments on the original edge, and change them to
2972 the new edge. Do it before redirection, so that the argument does not
2974 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2976 num_elem = PHI_NUM_ARGS (phi);
2977 for (i = 0; i < num_elem; i++)
2978 if (PHI_ARG_EDGE (phi, i) == edge_in)
2980 PHI_ARG_EDGE (phi, i) = new_edge;
2985 e = redirect_edge_and_branch (edge_in, new_bb);
2987 gcc_assert (!PENDING_STMT (edge_in));
2993 /* Return true when BB has label LABEL in it. */
2996 has_label_p (basic_block bb, tree label)
2998 block_stmt_iterator bsi;
3000 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3002 tree stmt = bsi_stmt (bsi);
3004 if (TREE_CODE (stmt) != LABEL_EXPR)
3006 if (LABEL_EXPR_LABEL (stmt) == label)
3013 /* Callback for walk_tree, check that all elements with address taken are
3014 properly noticed as such. */
3017 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3024 /* Check operand N for being valid GIMPLE and give error MSG if not.
3025 We check for constants explicitly since they are not considered
3026 gimple invariants if they overflowed. */
3027 #define CHECK_OP(N, MSG) \
3028 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3029 && !is_gimple_val (TREE_OPERAND (t, N))) \
3030 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3032 switch (TREE_CODE (t))
3035 if (SSA_NAME_IN_FREE_LIST (t))
3037 error ("SSA name in freelist but still referenced");
3043 x = TREE_OPERAND (t, 0);
3044 if (TREE_CODE (x) == BIT_FIELD_REF
3045 && is_gimple_reg (TREE_OPERAND (x, 0)))
3047 error ("GIMPLE register modified with BIT_FIELD_REF");
3053 /* Skip any references (they will be checked when we recurse down the
3054 tree) and ensure that any variable used as a prefix is marked
3056 for (x = TREE_OPERAND (t, 0);
3057 (handled_component_p (x)
3058 || TREE_CODE (x) == REALPART_EXPR
3059 || TREE_CODE (x) == IMAGPART_EXPR);
3060 x = TREE_OPERAND (x, 0))
3063 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3065 if (!TREE_ADDRESSABLE (x))
3067 error ("address taken, but ADDRESSABLE bit not set");
3073 x = TREE_OPERAND (t, 0);
3074 if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3076 error ("non-boolean used in condition");
3083 case FIX_TRUNC_EXPR:
3085 case FIX_FLOOR_EXPR:
3086 case FIX_ROUND_EXPR:
3091 case NON_LVALUE_EXPR:
3092 case TRUTH_NOT_EXPR:
3093 CHECK_OP (0, "Invalid operand to unary operator");
3100 case ARRAY_RANGE_REF:
3102 case VIEW_CONVERT_EXPR:
3103 /* We have a nest of references. Verify that each of the operands
3104 that determine where to reference is either a constant or a variable,
3105 verify that the base is valid, and then show we've already checked
3107 while (TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR
3108 || handled_component_p (t))
3110 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3111 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3112 else if (TREE_CODE (t) == ARRAY_REF
3113 || TREE_CODE (t) == ARRAY_RANGE_REF)
3115 CHECK_OP (1, "Invalid array index.");
3116 if (TREE_OPERAND (t, 2))
3117 CHECK_OP (2, "Invalid array lower bound.");
3118 if (TREE_OPERAND (t, 3))
3119 CHECK_OP (3, "Invalid array stride.");
3121 else if (TREE_CODE (t) == BIT_FIELD_REF)
3123 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3124 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3127 t = TREE_OPERAND (t, 0);
3130 if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t))
3132 error ("Invalid reference prefix.");
3144 case UNORDERED_EXPR:
3155 case TRUNC_DIV_EXPR:
3157 case FLOOR_DIV_EXPR:
3158 case ROUND_DIV_EXPR:
3159 case TRUNC_MOD_EXPR:
3161 case FLOOR_MOD_EXPR:
3162 case ROUND_MOD_EXPR:
3164 case EXACT_DIV_EXPR:
3174 CHECK_OP (0, "Invalid operand to binary operator");
3175 CHECK_OP (1, "Invalid operand to binary operator");
3187 /* Verify STMT, return true if STMT is not in GIMPLE form.
3188 TODO: Implement type checking. */
3191 verify_stmt (tree stmt, bool last_in_block)
3195 if (!is_gimple_stmt (stmt))
3197 error ("Is not a valid GIMPLE statement.");
3201 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
3204 debug_generic_stmt (addr);
3208 /* If the statement is marked as part of an EH region, then it is
3209 expected that the statement could throw. Verify that when we
3210 have optimizations that simplify statements such that we prove
3211 that they cannot throw, that we update other data structures
3213 if (lookup_stmt_eh_region (stmt) >= 0)
3215 if (!tree_could_throw_p (stmt))
3217 error ("Statement marked for throw, but doesn%'t.");
3220 if (!last_in_block && tree_can_throw_internal (stmt))
3222 error ("Statement marked for throw in middle of block.");
3230 debug_generic_stmt (stmt);
3235 /* Return true when the T can be shared. */
3238 tree_node_can_be_shared (tree t)
3240 if (IS_TYPE_OR_DECL_P (t)
3241 /* We check for constants explicitly since they are not considered
3242 gimple invariants if they overflowed. */
3243 || CONSTANT_CLASS_P (t)
3244 || is_gimple_min_invariant (t)
3245 || TREE_CODE (t) == SSA_NAME)
3248 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3249 /* We check for constants explicitly since they are not considered
3250 gimple invariants if they overflowed. */
3251 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 1))
3252 || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
3253 || (TREE_CODE (t) == COMPONENT_REF
3254 || TREE_CODE (t) == REALPART_EXPR
3255 || TREE_CODE (t) == IMAGPART_EXPR))
3256 t = TREE_OPERAND (t, 0);
3265 /* Called via walk_trees. Verify tree sharing. */
3268 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
3270 htab_t htab = (htab_t) data;
3273 if (tree_node_can_be_shared (*tp))
3275 *walk_subtrees = false;
3279 slot = htab_find_slot (htab, *tp, INSERT);
3288 /* Verify the GIMPLE statement chain. */
3294 block_stmt_iterator bsi;
3299 timevar_push (TV_TREE_STMT_VERIFY);
3300 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3307 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3309 int phi_num_args = PHI_NUM_ARGS (phi);
3311 for (i = 0; i < phi_num_args; i++)
3313 tree t = PHI_ARG_DEF (phi, i);
3316 /* Addressable variables do have SSA_NAMEs but they
3317 are not considered gimple values. */
3318 if (TREE_CODE (t) != SSA_NAME
3319 && TREE_CODE (t) != FUNCTION_DECL
3320 && !is_gimple_val (t))
3322 error ("PHI def is not a GIMPLE value");
3323 debug_generic_stmt (phi);
3324 debug_generic_stmt (t);
3328 addr = walk_tree (&t, verify_expr, NULL, NULL);
3331 debug_generic_stmt (addr);
3335 addr = walk_tree (&t, verify_node_sharing, htab, NULL);
3338 error ("Incorrect sharing of tree nodes");
3339 debug_generic_stmt (phi);
3340 debug_generic_stmt (addr);
3346 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
3348 tree stmt = bsi_stmt (bsi);
3350 err |= verify_stmt (stmt, bsi_end_p (bsi));
3351 addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
3354 error ("Incorrect sharing of tree nodes");
3355 debug_generic_stmt (stmt);
3356 debug_generic_stmt (addr);
3363 internal_error ("verify_stmts failed.");
3366 timevar_pop (TV_TREE_STMT_VERIFY);
3370 /* Verifies that the flow information is OK. */
3373 tree_verify_flow_info (void)
3377 block_stmt_iterator bsi;
3382 if (ENTRY_BLOCK_PTR->stmt_list)
3384 error ("ENTRY_BLOCK has a statement list associated with it\n");
3388 if (EXIT_BLOCK_PTR->stmt_list)
3390 error ("EXIT_BLOCK has a statement list associated with it\n");
3394 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3395 if (e->flags & EDGE_FALLTHRU)
3397 error ("Fallthru to exit from bb %d\n", e->src->index);
3403 bool found_ctrl_stmt = false;
3405 /* Skip labels on the start of basic block. */
3406 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3408 if (TREE_CODE (bsi_stmt (bsi)) != LABEL_EXPR)
3411 if (label_to_block (LABEL_EXPR_LABEL (bsi_stmt (bsi))) != bb)
3413 tree stmt = bsi_stmt (bsi);
3414 error ("Label %s to block does not match in bb %d\n",
3415 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3420 if (decl_function_context (LABEL_EXPR_LABEL (bsi_stmt (bsi)))
3421 != current_function_decl)
3423 tree stmt = bsi_stmt (bsi);
3424 error ("Label %s has incorrect context in bb %d\n",
3425 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3431 /* Verify that body of basic block BB is free of control flow. */
3432 for (; !bsi_end_p (bsi); bsi_next (&bsi))
3434 tree stmt = bsi_stmt (bsi);
3436 if (found_ctrl_stmt)
3438 error ("Control flow in the middle of basic block %d\n",
3443 if (stmt_ends_bb_p (stmt))
3444 found_ctrl_stmt = true;
3446 if (TREE_CODE (stmt) == LABEL_EXPR)
3448 error ("Label %s in the middle of basic block %d\n",
3449 IDENTIFIER_POINTER (DECL_NAME (stmt)),
3454 bsi = bsi_last (bb);
3455 if (bsi_end_p (bsi))
3458 stmt = bsi_stmt (bsi);
3460 if (is_ctrl_stmt (stmt))
3462 FOR_EACH_EDGE (e, ei, bb->succs)
3463 if (e->flags & EDGE_FALLTHRU)
3465 error ("Fallthru edge after a control statement in bb %d \n",
3471 switch (TREE_CODE (stmt))
3477 if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR
3478 || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR)
3480 error ("Structured COND_EXPR at the end of bb %d\n", bb->index);
3484 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
3486 if (!true_edge || !false_edge
3487 || !(true_edge->flags & EDGE_TRUE_VALUE)
3488 || !(false_edge->flags & EDGE_FALSE_VALUE)
3489 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3490 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3491 || EDGE_COUNT (bb->succs) >= 3)
3493 error ("Wrong outgoing edge flags at end of bb %d\n",
3498 if (!has_label_p (true_edge->dest,
3499 GOTO_DESTINATION (COND_EXPR_THEN (stmt))))
3501 error ("%<then%> label does not match edge at end of bb %d\n",
3506 if (!has_label_p (false_edge->dest,
3507 GOTO_DESTINATION (COND_EXPR_ELSE (stmt))))
3509 error ("%<else%> label does not match edge at end of bb %d\n",
3517 if (simple_goto_p (stmt))
3519 error ("Explicit goto at end of bb %d\n", bb->index);
3524 /* FIXME. We should double check that the labels in the
3525 destination blocks have their address taken. */
3526 FOR_EACH_EDGE (e, ei, bb->succs)
3527 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
3528 | EDGE_FALSE_VALUE))
3529 || !(e->flags & EDGE_ABNORMAL))
3531 error ("Wrong outgoing edge flags at end of bb %d\n",
3539 if (EDGE_COUNT (bb->succs) != 1
3540 || (EDGE_SUCC (bb, 0)->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3541 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3543 error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
3546 if (EDGE_SUCC (bb, 0)->dest != EXIT_BLOCK_PTR)
3548 error ("Return edge does not point to exit in bb %d\n",
3561 vec = SWITCH_LABELS (stmt);
3562 n = TREE_VEC_LENGTH (vec);
3564 /* Mark all the destination basic blocks. */
3565 for (i = 0; i < n; ++i)
3567 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3568 basic_block label_bb = label_to_block (lab);
3570 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
3571 label_bb->aux = (void *)1;
3574 /* Verify that the case labels are sorted. */
3575 prev = TREE_VEC_ELT (vec, 0);
3576 for (i = 1; i < n - 1; ++i)
3578 tree c = TREE_VEC_ELT (vec, i);
3581 error ("Found default case not at end of case vector");
3585 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
3587 error ("Case labels not sorted:\n ");
3588 print_generic_expr (stderr, prev, 0);
3589 fprintf (stderr," is greater than ");
3590 print_generic_expr (stderr, c, 0);
3591 fprintf (stderr," but comes before it.\n");
3596 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
3598 error ("No default case found at end of case vector");
3602 FOR_EACH_EDGE (e, ei, bb->succs)
3606 error ("Extra outgoing edge %d->%d\n",
3607 bb->index, e->dest->index);
3610 e->dest->aux = (void *)2;
3611 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3612 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3614 error ("Wrong outgoing edge flags at end of bb %d\n",
3620 /* Check that we have all of them. */
3621 for (i = 0; i < n; ++i)
3623 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3624 basic_block label_bb = label_to_block (lab);
3626 if (label_bb->aux != (void *)2)
3628 error ("Missing edge %i->%i\n",
3629 bb->index, label_bb->index);
3634 FOR_EACH_EDGE (e, ei, bb->succs)
3635 e->dest->aux = (void *)0;
3642 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
3643 verify_dominators (CDI_DOMINATORS);
3649 /* Updates phi nodes after creating a forwarder block joined
3650 by edge FALLTHRU. */
3653 tree_make_forwarder_block (edge fallthru)
3657 basic_block dummy, bb;
3658 tree phi, new_phi, var, prev, next;
3660 dummy = fallthru->src;
3661 bb = fallthru->dest;
3663 if (EDGE_COUNT (bb->preds) == 1)
3666 /* If we redirected a branch we must create new phi nodes at the
3668 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
3670 var = PHI_RESULT (phi);
3671 new_phi = create_phi_node (var, bb);
3672 SSA_NAME_DEF_STMT (var) = new_phi;
3673 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
3674 add_phi_arg (&new_phi, PHI_RESULT (phi), fallthru);
3677 /* Ensure that the PHI node chain is in the same order. */
3679 for (phi = phi_nodes (bb); phi; phi = next)
3681 next = PHI_CHAIN (phi);
3682 PHI_CHAIN (phi) = prev;
3685 set_phi_nodes (bb, prev);
3687 /* Add the arguments we have stored on edges. */
3688 FOR_EACH_EDGE (e, ei, bb->preds)
3693 flush_pending_stmts (e);
3698 /* Return true if basic block BB does nothing except pass control
3699 flow to another block and that we can safely insert a label at
3700 the start of the successor block.
3702 As a precondition, we require that BB be not equal to
3706 tree_forwarder_block_p (basic_block bb)
3708 block_stmt_iterator bsi;
3712 /* BB must have a single outgoing edge. */
3713 if (EDGE_COUNT (bb->succs) != 1
3714 /* BB can not have any PHI nodes. This could potentially be
3715 relaxed early in compilation if we re-rewrote the variables
3716 appearing in any PHI nodes in forwarder blocks. */
3718 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
3719 || EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR
3720 /* BB may not have an abnormal outgoing edge. */
3721 || (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL))
3725 gcc_assert (bb != ENTRY_BLOCK_PTR);
3728 /* Successors of the entry block are not forwarders. */
3729 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3733 /* Now walk through the statements. We can ignore labels, anything else
3734 means this is not a forwarder block. */
3735 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3737 tree stmt = bsi_stmt (bsi);
3739 switch (TREE_CODE (stmt))
3742 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
3754 /* Thread jumps from BB. */
3757 thread_jumps_from_bb (basic_block bb)
3761 bool retval = false;
3763 /* Examine each of our block's successors to see if it is
3765 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3770 basic_block dest, tmp, curr, old_dest;
3774 /* If the edge is abnormal or its destination is not
3775 forwardable, then there's nothing to do. */
3776 if ((e->flags & EDGE_ABNORMAL)
3777 || !bb_ann (e->dest)->forwardable)
3783 /* Now walk through as many forwarder blocks as possible to find
3784 the ultimate destination we want to thread our jump to. */
3785 last = EDGE_SUCC (e->dest, 0);
3786 bb_ann (e->dest)->forwardable = 0;
3787 for (dest = EDGE_SUCC (e->dest, 0)->dest;
3788 bb_ann (dest)->forwardable;
3789 last = EDGE_SUCC (dest, 0),
3790 dest = EDGE_SUCC (dest, 0)->dest)
3791 bb_ann (dest)->forwardable = 0;
3793 /* Reset the forwardable marks to 1. */
3796 tmp = EDGE_SUCC (tmp, 0)->dest)
3797 bb_ann (tmp)->forwardable = 1;
3799 if (dest == e->dest)
3805 old = find_edge (bb, dest);
3808 /* If there already is an edge, check whether the values in
3809 phi nodes differ. */
3810 if (!phi_alternatives_equal (dest, last, old))
3812 /* The previous block is forwarder. Redirect our jump
3813 to that target instead since we know it has no PHI
3814 nodes that will need updating. */
3817 /* That might mean that no forwarding at all is
3819 if (dest == e->dest)
3825 old = find_edge (bb, dest);
3829 /* Perform the redirection. */
3832 freq = EDGE_FREQUENCY (e);
3834 e = redirect_edge_and_branch (e, dest);
3836 /* Update the profile. */
3837 if (profile_status != PROFILE_ABSENT)
3838 for (curr = old_dest;
3840 curr = EDGE_SUCC (curr, 0)->dest)
3842 curr->frequency -= freq;
3843 if (curr->frequency < 0)
3844 curr->frequency = 0;
3845 curr->count -= count;
3846 if (curr->count < 0)
3848 EDGE_SUCC (curr, 0)->count -= count;
3849 if (EDGE_SUCC (curr, 0)->count < 0)
3850 EDGE_SUCC (curr, 0)->count = 0;
3855 /* Update PHI nodes. We know that the new argument should
3856 have the same value as the argument associated with LAST.
3857 Otherwise we would have changed our target block
3859 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
3861 arg = phi_arg_from_edge (phi, last);
3862 gcc_assert (arg >= 0);
3863 add_phi_arg (&phi, PHI_ARG_DEF (phi, arg), e);
3867 /* Remove the unreachable blocks (observe that if all blocks
3868 were reachable before, only those in the path we threaded
3869 over and did not have any predecessor outside of the path
3870 become unreachable). */
3871 for (; old_dest != dest; old_dest = tmp)
3873 tmp = EDGE_SUCC (old_dest, 0)->dest;
3875 if (EDGE_COUNT (old_dest->preds) > 0)
3878 delete_basic_block (old_dest);
3881 /* Update the dominators. */
3882 if (dom_info_available_p (CDI_DOMINATORS))
3884 /* If the dominator of the destination was in the
3885 path, set its dominator to the start of the
3887 if (get_immediate_dominator (CDI_DOMINATORS, old_dest) == NULL)
3888 set_immediate_dominator (CDI_DOMINATORS, old_dest, bb);
3890 /* Now proceed like if we forwarded just over one edge at a
3891 time. Algorithm for forwarding edge S --> A over
3892 edge A --> B then is
3895 && !dominated_by (S, B))
3896 idom (B) = idom (A);
3897 recount_idom (A); */
3899 for (; old_dest != dest; old_dest = tmp)
3903 tmp = EDGE_SUCC (old_dest, 0)->dest;
3905 if (get_immediate_dominator (CDI_DOMINATORS, tmp) == old_dest
3906 && !dominated_by_p (CDI_DOMINATORS, bb, tmp))
3908 dom = get_immediate_dominator (CDI_DOMINATORS, old_dest);
3909 set_immediate_dominator (CDI_DOMINATORS, tmp, dom);
3912 dom = recount_dominator (CDI_DOMINATORS, old_dest);
3913 set_immediate_dominator (CDI_DOMINATORS, old_dest, dom);
3922 /* Thread jumps over empty statements.
3924 This code should _not_ thread over obviously equivalent conditions
3925 as that requires nontrivial updates to the SSA graph.
3927 As a precondition, we require that all basic blocks be reachable.
3928 That is, there should be no opportunities left for
3929 delete_unreachable_blocks. */
3935 bool retval = false;
3936 basic_block *worklist = xmalloc (sizeof (basic_block) * last_basic_block);
3937 unsigned int size = 0;
3941 bb_ann (bb)->forwardable = tree_forwarder_block_p (bb);
3942 bb->flags &= ~BB_VISITED;
3945 /* We pretend to have ENTRY_BLOCK_PTR in WORKLIST. This way,
3946 ENTRY_BLOCK_PTR will never be entered into WORKLIST. */
3947 ENTRY_BLOCK_PTR->flags |= BB_VISITED;
3949 /* Initialize WORKLIST by putting non-forwarder blocks that
3950 immediately precede forwarder blocks because those are the ones
3951 that we know we can thread jumps from. We use BB_VISITED to
3952 indicate whether a given basic block is in WORKLIST or not,
3953 thereby avoiding duplicates in WORKLIST. */
3959 /* We are not interested in finding non-forwarder blocks
3960 directly. We want to find non-forwarder blocks as
3961 predecessors of a forwarder block. */
3962 if (!bb_ann (bb)->forwardable)
3965 /* Now we know BB is a forwarder block. Visit each of its
3966 incoming edges and add to WORKLIST all non-forwarder blocks
3967 among BB's predecessors. */
3968 FOR_EACH_EDGE (e, ei, bb->preds)
3970 /* We don't want to put a duplicate into WORKLIST. */
3971 if ((e->src->flags & BB_VISITED) == 0
3972 /* We are not interested in threading jumps from a forwarder
3974 && !bb_ann (e->src)->forwardable)
3976 e->src->flags |= BB_VISITED;
3977 worklist[size] = e->src;
3983 /* Now let's drain WORKLIST. */
3987 bb = worklist[size];
3989 /* BB is no longer in WORKLIST, so clear BB_VISITED. */
3990 bb->flags &= ~BB_VISITED;
3992 if (thread_jumps_from_bb (bb))
3996 if (tree_forwarder_block_p (bb))
4001 bb_ann (bb)->forwardable = true;
4003 /* Attempts to thread through BB may have been blocked
4004 because BB was not a forwarder block before. Now
4005 that BB is a forwarder block, we should revisit BB's
4007 FOR_EACH_EDGE (f, ej, bb->preds)
4009 /* We don't want to put a duplicate into WORKLIST. */
4010 if ((f->src->flags & BB_VISITED) == 0
4011 /* We are not interested in threading jumps from a
4013 && !bb_ann (f->src)->forwardable)
4015 f->src->flags |= BB_VISITED;
4016 worklist[size] = f->src;
4024 ENTRY_BLOCK_PTR->flags &= ~BB_VISITED;
4032 /* Return a non-special label in the head of basic block BLOCK.
4033 Create one if it doesn't exist. */
4036 tree_block_label (basic_block bb)
4038 block_stmt_iterator i, s = bsi_start (bb);
4042 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4044 stmt = bsi_stmt (i);
4045 if (TREE_CODE (stmt) != LABEL_EXPR)
4047 label = LABEL_EXPR_LABEL (stmt);
4048 if (!DECL_NONLOCAL (label))
4051 bsi_move_before (&i, &s);
4056 label = create_artificial_label ();
4057 stmt = build1 (LABEL_EXPR, void_type_node, label);
4058 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4063 /* Attempt to perform edge redirection by replacing a possibly complex
4064 jump instruction by a goto or by removing the jump completely.
4065 This can apply only if all edges now point to the same block. The
4066 parameters and return values are equivalent to
4067 redirect_edge_and_branch. */
4070 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4072 basic_block src = e->src;
4074 block_stmt_iterator b;
4078 /* Verify that all targets will be TARGET. */
4079 FOR_EACH_EDGE (tmp, ei, src->succs)
4080 if (tmp->dest != target && tmp != e)
4089 stmt = bsi_stmt (b);
4091 if (TREE_CODE (stmt) == COND_EXPR
4092 || TREE_CODE (stmt) == SWITCH_EXPR)
4095 e = ssa_redirect_edge (e, target);
4096 e->flags = EDGE_FALLTHRU;
4104 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4105 edge representing the redirected branch. */
4108 tree_redirect_edge_and_branch (edge e, basic_block dest)
4110 basic_block bb = e->src;
4111 block_stmt_iterator bsi;
4115 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4118 if (e->src != ENTRY_BLOCK_PTR
4119 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4122 if (e->dest == dest)
4125 label = tree_block_label (dest);
4127 bsi = bsi_last (bb);
4128 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4130 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4133 stmt = (e->flags & EDGE_TRUE_VALUE
4134 ? COND_EXPR_THEN (stmt)
4135 : COND_EXPR_ELSE (stmt));
4136 GOTO_DESTINATION (stmt) = label;
4140 /* No non-abnormal edges should lead from a non-simple goto, and
4141 simple ones should be represented implicitly. */
4146 tree vec = SWITCH_LABELS (stmt);
4147 size_t i, n = TREE_VEC_LENGTH (vec);
4149 for (i = 0; i < n; ++i)
4151 tree elt = TREE_VEC_ELT (vec, i);
4152 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4153 CASE_LABEL (elt) = label;
4160 e->flags |= EDGE_FALLTHRU;
4164 /* Otherwise it must be a fallthru edge, and we don't need to
4165 do anything besides redirecting it. */
4166 gcc_assert (e->flags & EDGE_FALLTHRU);
4170 /* Update/insert PHI nodes as necessary. */
4172 /* Now update the edges in the CFG. */
4173 e = ssa_redirect_edge (e, dest);
4179 /* Simple wrapper, as we can always redirect fallthru edges. */
4182 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4184 e = tree_redirect_edge_and_branch (e, dest);
4191 /* Splits basic block BB after statement STMT (but at least after the
4192 labels). If STMT is NULL, BB is split just after the labels. */
4195 tree_split_block (basic_block bb, void *stmt)
4197 block_stmt_iterator bsi, bsi_tgt;
4203 new_bb = create_empty_bb (bb);
4205 /* Redirect the outgoing edges. */
4206 new_bb->succs = bb->succs;
4208 FOR_EACH_EDGE (e, ei, new_bb->succs)
4211 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4214 /* Move everything from BSI to the new basic block. */
4215 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4217 act = bsi_stmt (bsi);
4218 if (TREE_CODE (act) == LABEL_EXPR)
4231 bsi_tgt = bsi_start (new_bb);
4232 while (!bsi_end_p (bsi))
4234 act = bsi_stmt (bsi);
4236 bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT);
4243 /* Moves basic block BB after block AFTER. */
4246 tree_move_block_after (basic_block bb, basic_block after)
4248 if (bb->prev_bb == after)
4252 link_block (bb, after);
4258 /* Return true if basic_block can be duplicated. */
4261 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED)
4266 /* Create a duplicate of the basic block BB. NOTE: This does not
4267 preserve SSA form. */
4270 tree_duplicate_bb (basic_block bb)
4273 block_stmt_iterator bsi, bsi_tgt;
4275 ssa_op_iter op_iter;
4277 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4279 /* First copy the phi nodes. We do not copy phi node arguments here,
4280 since the edges are not ready yet. Keep the chain of phi nodes in
4281 the same order, so that we can add them later. */
4282 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
4284 mark_for_rewrite (PHI_RESULT (phi));
4285 create_phi_node (PHI_RESULT (phi), new_bb);
4287 set_phi_nodes (new_bb, nreverse (phi_nodes (new_bb)));
4289 bsi_tgt = bsi_start (new_bb);
4290 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4292 tree stmt = bsi_stmt (bsi);
4295 if (TREE_CODE (stmt) == LABEL_EXPR)
4298 /* Record the definitions. */
4299 get_stmt_operands (stmt);
4301 FOR_EACH_SSA_TREE_OPERAND (val, stmt, op_iter, SSA_OP_ALL_DEFS)
4302 mark_for_rewrite (val);
4304 copy = unshare_expr (stmt);
4306 /* Copy also the virtual operands. */
4307 get_stmt_ann (copy);
4308 copy_virtual_operands (copy, stmt);
4310 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
4316 /* Basic block BB_COPY was created by code duplication. Add phi node
4317 arguments for edges going out of BB_COPY. The blocks that were
4318 duplicated have rbi->duplicated set to one. */
4321 add_phi_args_after_copy_bb (basic_block bb_copy)
4323 basic_block bb, dest;
4326 tree phi, phi_copy, phi_next, def;
4328 bb = bb_copy->rbi->original;
4330 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
4332 if (!phi_nodes (e_copy->dest))
4335 if (e_copy->dest->rbi->duplicated)
4336 dest = e_copy->dest->rbi->original;
4338 dest = e_copy->dest;
4340 e = find_edge (bb, dest);
4343 /* During loop unrolling the target of the latch edge is copied.
4344 In this case we are not looking for edge to dest, but to
4345 duplicated block whose original was dest. */
4346 FOR_EACH_EDGE (e, ei, bb->succs)
4347 if (e->dest->rbi->duplicated
4348 && e->dest->rbi->original == dest)
4351 gcc_assert (e != NULL);
4354 for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
4356 phi = phi_next, phi_copy = TREE_CHAIN (phi_copy))
4358 phi_next = TREE_CHAIN (phi);
4360 gcc_assert (PHI_RESULT (phi) == PHI_RESULT (phi_copy));
4361 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4362 add_phi_arg (&phi_copy, def, e_copy);
4367 /* Blocks in REGION_COPY array of length N_REGION were created by
4368 duplication of basic blocks. Add phi node arguments for edges
4369 going from these blocks. */
4372 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region)
4376 for (i = 0; i < n_region; i++)
4377 region_copy[i]->rbi->duplicated = 1;
4379 for (i = 0; i < n_region; i++)
4380 add_phi_args_after_copy_bb (region_copy[i]);
4382 for (i = 0; i < n_region; i++)
4383 region_copy[i]->rbi->duplicated = 0;
4386 /* Maps the old ssa name FROM_NAME to TO_NAME. */
4388 struct ssa_name_map_entry
4394 /* Hash function for ssa_name_map_entry. */
4397 ssa_name_map_entry_hash (const void *entry)
4399 const struct ssa_name_map_entry *en = entry;
4400 return SSA_NAME_VERSION (en->from_name);
4403 /* Equality function for ssa_name_map_entry. */
4406 ssa_name_map_entry_eq (const void *in_table, const void *ssa_name)
4408 const struct ssa_name_map_entry *en = in_table;
4410 return en->from_name == ssa_name;
4413 /* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping
4417 allocate_ssa_names (bitmap definitions, htab_t *map)
4420 struct ssa_name_map_entry *entry;
4426 *map = htab_create (10, ssa_name_map_entry_hash,
4427 ssa_name_map_entry_eq, free);
4428 EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
4430 name = ssa_name (ver);
4431 slot = htab_find_slot_with_hash (*map, name, SSA_NAME_VERSION (name),
4437 entry = xmalloc (sizeof (struct ssa_name_map_entry));
4438 entry->from_name = name;
4441 entry->to_name = duplicate_ssa_name (name, SSA_NAME_DEF_STMT (name));
4445 /* Rewrite the definition DEF in statement STMT to new ssa name as specified
4446 by the mapping MAP. */
4449 rewrite_to_new_ssa_names_def (def_operand_p def, tree stmt, htab_t map)
4451 tree name = DEF_FROM_PTR (def);
4452 struct ssa_name_map_entry *entry;
4454 gcc_assert (TREE_CODE (name) == SSA_NAME);
4456 entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name));
4460 SET_DEF (def, entry->to_name);
4461 SSA_NAME_DEF_STMT (entry->to_name) = stmt;
4464 /* Rewrite the USE to new ssa name as specified by the mapping MAP. */
4467 rewrite_to_new_ssa_names_use (use_operand_p use, htab_t map)
4469 tree name = USE_FROM_PTR (use);
4470 struct ssa_name_map_entry *entry;
4472 if (TREE_CODE (name) != SSA_NAME)
4475 entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name));
4479 SET_USE (use, entry->to_name);
4482 /* Rewrite the ssa names in basic block BB to new ones as specified by the
4486 rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map)
4492 block_stmt_iterator bsi;
4496 v_may_def_optype v_may_defs;
4497 v_must_def_optype v_must_defs;
4500 FOR_EACH_EDGE (e, ei, bb->preds)
4501 if (e->flags & EDGE_ABNORMAL)
4504 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
4506 rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi), phi, map);
4508 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
4511 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4513 stmt = bsi_stmt (bsi);
4514 get_stmt_operands (stmt);
4515 ann = stmt_ann (stmt);
4517 uses = USE_OPS (ann);
4518 for (i = 0; i < NUM_USES (uses); i++)
4519 rewrite_to_new_ssa_names_use (USE_OP_PTR (uses, i), map);
4521 defs = DEF_OPS (ann);
4522 for (i = 0; i < NUM_DEFS (defs); i++)
4523 rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs, i), stmt, map);
4525 vuses = VUSE_OPS (ann);
4526 for (i = 0; i < NUM_VUSES (vuses); i++)
4527 rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses, i), map);
4529 v_may_defs = V_MAY_DEF_OPS (ann);
4530 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
4532 rewrite_to_new_ssa_names_use
4533 (V_MAY_DEF_OP_PTR (v_may_defs, i), map);
4534 rewrite_to_new_ssa_names_def
4535 (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt, map);
4538 v_must_defs = V_MUST_DEF_OPS (ann);
4539 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
4541 rewrite_to_new_ssa_names_def
4542 (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt, map);
4543 rewrite_to_new_ssa_names_use
4544 (V_MUST_DEF_KILL_PTR (v_must_defs, i), map);
4548 FOR_EACH_EDGE (e, ei, bb->succs)
4549 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
4551 rewrite_to_new_ssa_names_use
4552 (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), map);
4554 if (e->flags & EDGE_ABNORMAL)
4556 tree op = PHI_ARG_DEF_FROM_EDGE (phi, e);
4557 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1;
4562 /* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified
4563 by the mapping MAP. */
4566 rewrite_to_new_ssa_names (basic_block *region, unsigned n_region, htab_t map)
4570 for (r = 0; r < n_region; r++)
4571 rewrite_to_new_ssa_names_bb (region[r], map);
4574 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4575 important exit edge EXIT. By important we mean that no SSA name defined
4576 inside region is live over the other exit edges of the region. All entry
4577 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4578 to the duplicate of the region. SSA form, dominance and loop information
4579 is updated. The new basic blocks are stored to REGION_COPY in the same
4580 order as they had in REGION, provided that REGION_COPY is not NULL.
4581 The function returns false if it is unable to copy the region,
4585 tree_duplicate_sese_region (edge entry, edge exit,
4586 basic_block *region, unsigned n_region,
4587 basic_block *region_copy)
4589 unsigned i, n_doms, ver;
4590 bool free_region_copy = false, copying_header = false;
4591 struct loop *loop = entry->dest->loop_father;
4596 htab_t ssa_name_map = NULL;
4600 if (!can_copy_bbs_p (region, n_region))
4603 /* Some sanity checking. Note that we do not check for all possible
4604 missuses of the functions. I.e. if you ask to copy something weird,
4605 it will work, but the state of structures probably will not be
4608 for (i = 0; i < n_region; i++)
4610 /* We do not handle subloops, i.e. all the blocks must belong to the
4612 if (region[i]->loop_father != loop)
4615 if (region[i] != entry->dest
4616 && region[i] == loop->header)
4622 /* In case the function is used for loop header copying (which is the primary
4623 use), ensure that EXIT and its copy will be new latch and entry edges. */
4624 if (loop->header == entry->dest)
4626 copying_header = true;
4627 loop->copy = loop->outer;
4629 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
4632 for (i = 0; i < n_region; i++)
4633 if (region[i] != exit->src
4634 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
4640 region_copy = xmalloc (sizeof (basic_block) * n_region);
4641 free_region_copy = true;
4644 gcc_assert (!any_marked_for_rewrite_p ());
4646 /* Record blocks outside the region that are duplicated by something
4648 doms = xmalloc (sizeof (basic_block) * n_basic_blocks);
4649 n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms);
4651 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop);
4652 definitions = marked_ssa_names ();
4656 loop->header = exit->dest;
4657 loop->latch = exit->src;
4660 /* Redirect the entry and add the phi node arguments. */
4661 redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy);
4662 gcc_assert (redirected != NULL);
4663 flush_pending_stmts (entry);
4665 /* Concerning updating of dominators: We must recount dominators
4666 for entry block and its copy. Anything that is outside of the region, but
4667 was dominated by something inside needs recounting as well. */
4668 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
4669 doms[n_doms++] = entry->dest->rbi->original;
4670 iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms);
4673 /* Add the other phi node arguments. */
4674 add_phi_args_after_copy (region_copy, n_region);
4676 /* Add phi nodes for definitions at exit. TODO -- once we have immediate
4677 uses, it should be possible to emit phi nodes just for definitions that
4678 are used outside region. */
4679 EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
4681 tree name = ssa_name (ver);
4683 phi = create_phi_node (name, exit->dest);
4684 add_phi_arg (&phi, name, exit);
4685 add_phi_arg (&phi, name, exit_copy);
4687 SSA_NAME_DEF_STMT (name) = phi;
4690 /* And create new definitions inside region and its copy. TODO -- once we
4691 have immediate uses, it might be better to leave definitions in region
4692 unchanged, create new ssa names for phi nodes on exit, and rewrite
4693 the uses, to avoid changing the copied region. */
4694 allocate_ssa_names (definitions, &ssa_name_map);
4695 rewrite_to_new_ssa_names (region, n_region, ssa_name_map);
4696 allocate_ssa_names (definitions, &ssa_name_map);
4697 rewrite_to_new_ssa_names (region_copy, n_region, ssa_name_map);
4698 htab_delete (ssa_name_map);
4700 if (free_region_copy)
4703 unmark_all_for_rewrite ();
4704 BITMAP_XFREE (definitions);
4709 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
4712 dump_function_to_file (tree fn, FILE *file, int flags)
4714 tree arg, vars, var;
4715 bool ignore_topmost_bind = false, any_var = false;
4719 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
4721 arg = DECL_ARGUMENTS (fn);
4724 print_generic_expr (file, arg, dump_flags);
4725 if (TREE_CHAIN (arg))
4726 fprintf (file, ", ");
4727 arg = TREE_CHAIN (arg);
4729 fprintf (file, ")\n");
4731 if (flags & TDF_RAW)
4733 dump_node (fn, TDF_SLIM | flags, file);
4737 /* When GIMPLE is lowered, the variables are no longer available in
4738 BIND_EXPRs, so display them separately. */
4739 if (cfun && cfun->unexpanded_var_list)
4741 ignore_topmost_bind = true;
4743 fprintf (file, "{\n");
4744 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
4746 var = TREE_VALUE (vars);
4748 print_generic_decl (file, var, flags);
4749 fprintf (file, "\n");
4755 if (basic_block_info)
4757 /* Make a CFG based dump. */
4758 check_bb_profile (ENTRY_BLOCK_PTR, file);
4759 if (!ignore_topmost_bind)
4760 fprintf (file, "{\n");
4762 if (any_var && n_basic_blocks)
4763 fprintf (file, "\n");
4766 dump_generic_bb (file, bb, 2, flags);
4768 fprintf (file, "}\n");
4769 check_bb_profile (EXIT_BLOCK_PTR, file);
4775 /* Make a tree based dump. */
4776 chain = DECL_SAVED_TREE (fn);
4778 if (TREE_CODE (chain) == BIND_EXPR)
4780 if (ignore_topmost_bind)
4782 chain = BIND_EXPR_BODY (chain);
4790 if (!ignore_topmost_bind)
4791 fprintf (file, "{\n");
4796 fprintf (file, "\n");
4798 print_generic_stmt_indented (file, chain, flags, indent);
4799 if (ignore_topmost_bind)
4800 fprintf (file, "}\n");
4803 fprintf (file, "\n\n");
4807 /* Pretty print of the loops intermediate representation. */
4808 static void print_loop (FILE *, struct loop *, int);
4809 static void print_pred_bbs (FILE *, basic_block bb);
4810 static void print_succ_bbs (FILE *, basic_block bb);
4813 /* Print the predecessors indexes of edge E on FILE. */
4816 print_pred_bbs (FILE *file, basic_block bb)
4821 FOR_EACH_EDGE (e, ei, bb->preds)
4822 fprintf (file, "bb_%d", e->src->index);
4826 /* Print the successors indexes of edge E on FILE. */
4829 print_succ_bbs (FILE *file, basic_block bb)
4834 FOR_EACH_EDGE (e, ei, bb->succs)
4835 fprintf (file, "bb_%d", e->src->index);
4839 /* Pretty print LOOP on FILE, indented INDENT spaces. */
4842 print_loop (FILE *file, struct loop *loop, int indent)
4850 s_indent = (char *) alloca ((size_t) indent + 1);
4851 memset ((void *) s_indent, ' ', (size_t) indent);
4852 s_indent[indent] = '\0';
4854 /* Print the loop's header. */
4855 fprintf (file, "%sloop_%d\n", s_indent, loop->num);
4857 /* Print the loop's body. */
4858 fprintf (file, "%s{\n", s_indent);
4860 if (bb->loop_father == loop)
4862 /* Print the basic_block's header. */
4863 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
4864 print_pred_bbs (file, bb);
4865 fprintf (file, "}, succs = {");
4866 print_succ_bbs (file, bb);
4867 fprintf (file, "})\n");
4869 /* Print the basic_block's body. */
4870 fprintf (file, "%s {\n", s_indent);
4871 tree_dump_bb (bb, file, indent + 4);
4872 fprintf (file, "%s }\n", s_indent);
4875 print_loop (file, loop->inner, indent + 2);
4876 fprintf (file, "%s}\n", s_indent);
4877 print_loop (file, loop->next, indent);
4881 /* Follow a CFG edge from the entry point of the program, and on entry
4882 of a loop, pretty print the loop structure on FILE. */
4885 print_loop_ir (FILE *file)
4889 bb = BASIC_BLOCK (0);
4890 if (bb && bb->loop_father)
4891 print_loop (file, bb->loop_father, 0);
4895 /* Debugging loops structure at tree level. */
4898 debug_loop_ir (void)
4900 print_loop_ir (stderr);
4904 /* Return true if BB ends with a call, possibly followed by some
4905 instructions that must stay with the call. Return false,
4909 tree_block_ends_with_call_p (basic_block bb)
4911 block_stmt_iterator bsi = bsi_last (bb);
4912 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
4916 /* Return true if BB ends with a conditional branch. Return false,
4920 tree_block_ends_with_condjump_p (basic_block bb)
4922 tree stmt = tsi_stmt (bsi_last (bb).tsi);
4923 return (TREE_CODE (stmt) == COND_EXPR);
4927 /* Return true if we need to add fake edge to exit at statement T.
4928 Helper function for tree_flow_call_edges_add. */
4931 need_fake_edge_p (tree t)
4935 /* NORETURN and LONGJMP calls already have an edge to exit.
4936 CONST, PURE and ALWAYS_RETURN calls do not need one.
4937 We don't currently check for CONST and PURE here, although
4938 it would be a good idea, because those attributes are
4939 figured out from the RTL in mark_constant_function, and
4940 the counter incrementation code from -fprofile-arcs
4941 leads to different results from -fbranch-probabilities. */
4942 call = get_call_expr_in (t);
4944 && !(call_expr_flags (call) &
4945 (ECF_NORETURN | ECF_LONGJMP | ECF_ALWAYS_RETURN)))
4948 if (TREE_CODE (t) == ASM_EXPR
4949 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
4956 /* Add fake edges to the function exit for any non constant and non
4957 noreturn calls, volatile inline assembly in the bitmap of blocks
4958 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
4959 the number of blocks that were split.
4961 The goal is to expose cases in which entering a basic block does
4962 not imply that all subsequent instructions must be executed. */
4965 tree_flow_call_edges_add (sbitmap blocks)
4968 int blocks_split = 0;
4969 int last_bb = last_basic_block;
4970 bool check_last_block = false;
4972 if (n_basic_blocks == 0)
4976 check_last_block = true;
4978 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
4980 /* In the last basic block, before epilogue generation, there will be
4981 a fallthru edge to EXIT. Special care is required if the last insn
4982 of the last basic block is a call because make_edge folds duplicate
4983 edges, which would result in the fallthru edge also being marked
4984 fake, which would result in the fallthru edge being removed by
4985 remove_fake_edges, which would result in an invalid CFG.
4987 Moreover, we can't elide the outgoing fake edge, since the block
4988 profiler needs to take this into account in order to solve the minimal
4989 spanning tree in the case that the call doesn't return.
4991 Handle this by adding a dummy instruction in a new last basic block. */
4992 if (check_last_block)
4995 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
4996 block_stmt_iterator bsi = bsi_last (bb);
4998 if (!bsi_end_p (bsi))
5001 if (need_fake_edge_p (t))
5005 FOR_EACH_EDGE (e, ei, bb->succs)
5006 if (e->dest == EXIT_BLOCK_PTR)
5008 bsi_insert_on_edge (e, build_empty_stmt ());
5009 bsi_commit_edge_inserts ((int *)NULL);
5015 /* Now add fake edges to the function exit for any non constant
5016 calls since there is no way that we can determine if they will
5018 for (i = 0; i < last_bb; i++)
5020 basic_block bb = BASIC_BLOCK (i);
5021 block_stmt_iterator bsi;
5022 tree stmt, last_stmt;
5027 if (blocks && !TEST_BIT (blocks, i))
5030 bsi = bsi_last (bb);
5031 if (!bsi_end_p (bsi))
5033 last_stmt = bsi_stmt (bsi);
5036 stmt = bsi_stmt (bsi);
5037 if (need_fake_edge_p (stmt))
5040 /* The handling above of the final block before the
5041 epilogue should be enough to verify that there is
5042 no edge to the exit block in CFG already.
5043 Calling make_edge in such case would cause us to
5044 mark that edge as fake and remove it later. */
5045 #ifdef ENABLE_CHECKING
5046 if (stmt == last_stmt)
5049 FOR_EACH_EDGE (e, ei, bb->succs)
5050 gcc_assert (e->dest != EXIT_BLOCK_PTR);
5054 /* Note that the following may create a new basic block
5055 and renumber the existing basic blocks. */
5056 if (stmt != last_stmt)
5058 e = split_block (bb, stmt);
5062 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
5066 while (!bsi_end_p (bsi));
5071 verify_flow_info ();
5073 return blocks_split;
5077 tree_purge_dead_eh_edges (basic_block bb)
5079 bool changed = false;
5082 tree stmt = last_stmt (bb);
5084 if (stmt && tree_can_throw_internal (stmt))
5087 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5089 if (e->flags & EDGE_EH)
5091 ssa_remove_edge (e);
5098 /* Removal of dead EH edges might change dominators of not
5099 just immediate successors. E.g. when bb1 is changed so that
5100 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5101 eh edges purged by this function in:
5113 idom(bb5) must be recomputed. For now just free the dominance
5116 free_dominance_info (CDI_DOMINATORS);
5122 tree_purge_all_dead_eh_edges (bitmap blocks)
5124 bool changed = false;
5128 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
5130 changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
5136 struct cfg_hooks tree_cfg_hooks = {
5138 tree_verify_flow_info,
5139 tree_dump_bb, /* dump_bb */
5140 create_bb, /* create_basic_block */
5141 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
5142 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
5143 remove_bb, /* delete_basic_block */
5144 tree_split_block, /* split_block */
5145 tree_move_block_after, /* move_block_after */
5146 tree_can_merge_blocks_p, /* can_merge_blocks_p */
5147 tree_merge_blocks, /* merge_blocks */
5148 tree_predict_edge, /* predict_edge */
5149 tree_predicted_by_p, /* predicted_by_p */
5150 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
5151 tree_duplicate_bb, /* duplicate_block */
5152 tree_split_edge, /* split_edge */
5153 tree_make_forwarder_block, /* make_forward_block */
5154 NULL, /* tidy_fallthru_edge */
5155 tree_block_ends_with_call_p, /* block_ends_with_call_p */
5156 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
5157 tree_flow_call_edges_add /* flow_call_edges_add */
5161 /* Split all critical edges. */
5164 split_critical_edges (void)
5172 FOR_EACH_EDGE (e, ei, bb->succs)
5173 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
5180 struct tree_opt_pass pass_split_crit_edges =
5182 "crited", /* name */
5184 split_critical_edges, /* execute */
5187 0, /* static_pass_number */
5188 TV_TREE_SPLIT_EDGES, /* tv_id */
5189 PROP_cfg, /* properties required */
5190 PROP_no_crit_edges, /* properties_provided */
5191 0, /* properties_destroyed */
5192 0, /* todo_flags_start */
5193 TODO_dump_func, /* todo_flags_finish */
5198 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5199 a temporary, make sure and register it to be renamed if necessary,
5200 and finally return the temporary. Put the statements to compute
5201 EXP before the current statement in BSI. */
5204 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
5206 tree t, new_stmt, orig_stmt;
5208 if (is_gimple_val (exp))
5211 t = make_rename_temp (type, NULL);
5212 new_stmt = build (MODIFY_EXPR, type, t, exp);
5214 orig_stmt = bsi_stmt (*bsi);
5215 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
5216 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
5218 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
5223 /* Build a ternary operation and gimplify it. Emit code before BSI.
5224 Return the gimple_val holding the result. */
5227 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
5228 tree type, tree a, tree b, tree c)
5232 ret = fold (build3 (code, type, a, b, c));
5235 return gimplify_val (bsi, type, ret);
5238 /* Build a binary operation and gimplify it. Emit code before BSI.
5239 Return the gimple_val holding the result. */
5242 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
5243 tree type, tree a, tree b)
5247 ret = fold (build2 (code, type, a, b));
5250 return gimplify_val (bsi, type, ret);
5253 /* Build a unary operation and gimplify it. Emit code before BSI.
5254 Return the gimple_val holding the result. */
5257 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
5262 ret = fold (build1 (code, type, a));
5265 return gimplify_val (bsi, type, ret);
5270 /* Emit return warnings. */
5273 execute_warn_function_return (void)
5275 #ifdef USE_MAPPED_LOCATION
5276 source_location location;
5284 if (warn_missing_noreturn
5285 && !TREE_THIS_VOLATILE (cfun->decl)
5286 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
5287 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
5288 warning ("%Jfunction might be possible candidate for "
5289 "attribute %<noreturn%>",
5292 /* If we have a path to EXIT, then we do return. */
5293 if (TREE_THIS_VOLATILE (cfun->decl)
5294 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
5296 #ifdef USE_MAPPED_LOCATION
5297 location = UNKNOWN_LOCATION;
5301 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5303 last = last_stmt (e->src);
5304 if (TREE_CODE (last) == RETURN_EXPR
5305 #ifdef USE_MAPPED_LOCATION
5306 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
5308 && (locus = EXPR_LOCUS (last)) != NULL)
5312 #ifdef USE_MAPPED_LOCATION
5313 if (location == UNKNOWN_LOCATION)
5314 location = cfun->function_end_locus;
5315 warning ("%H%<noreturn%> function does return", &location);
5318 locus = &cfun->function_end_locus;
5319 warning ("%H%<noreturn%> function does return", locus);
5323 /* If we see "return;" in some basic block, then we do reach the end
5324 without returning a value. */
5325 else if (warn_return_type
5326 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
5327 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
5329 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5331 tree last = last_stmt (e->src);
5332 if (TREE_CODE (last) == RETURN_EXPR
5333 && TREE_OPERAND (last, 0) == NULL)
5335 #ifdef USE_MAPPED_LOCATION
5336 location = EXPR_LOCATION (last);
5337 if (location == UNKNOWN_LOCATION)
5338 location = cfun->function_end_locus;
5339 warning ("%Hcontrol reaches end of non-void function", &location);
5341 locus = EXPR_LOCUS (last);
5343 locus = &cfun->function_end_locus;
5344 warning ("%Hcontrol reaches end of non-void function", locus);
5353 /* Given a basic block B which ends with a conditional and has
5354 precisely two successors, determine which of the edges is taken if
5355 the conditional is true and which is taken if the conditional is
5356 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5359 extract_true_false_edges_from_block (basic_block b,
5363 edge e = EDGE_SUCC (b, 0);
5365 if (e->flags & EDGE_TRUE_VALUE)
5368 *false_edge = EDGE_SUCC (b, 1);
5373 *true_edge = EDGE_SUCC (b, 1);
5377 struct tree_opt_pass pass_warn_function_return =
5381 execute_warn_function_return, /* execute */
5384 0, /* static_pass_number */
5386 PROP_cfg, /* properties_required */
5387 0, /* properties_provided */
5388 0, /* properties_destroyed */
5389 0, /* todo_flags_start */
5390 0, /* todo_flags_finish */
5394 #include "gt-tree-cfg.h"