1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
36 #include "langhooks.h"
37 #include "diagnostic.h"
38 #include "tree-flow.h"
40 #include "tree-dump.h"
41 #include "tree-pass.h"
45 #include "cfglayout.h"
46 #include "tree-ssa-propagate.h"
47 #include "value-prof.h"
48 #include "pointer-set.h"
49 #include "tree-inline.h"
51 /* This file contains functions for building the Control Flow Graph (CFG)
52 for a function tree. */
54 /* Local declarations. */
56 /* Initial capacity for the basic block array. */
57 static const int initial_cfg_capacity = 20;
59 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
60 which use a particular edge. The CASE_LABEL_EXPRs are chained together
61 via their TREE_CHAIN field, which we clear after we're done with the
62 hash table to prevent problems with duplication of GIMPLE_SWITCHes.
64 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
65 update the case vector in response to edge redirections.
67 Right now this table is set up and torn down at key points in the
68 compilation process. It would be nice if we could make the table
69 more persistent. The key is getting notification of changes to
70 the CFG (particularly edge removal, creation and redirection). */
72 static struct pointer_map_t *edge_to_cases;
77 long num_merged_labels;
80 static struct cfg_stats_d cfg_stats;
82 /* Nonzero if we found a computed goto while building basic blocks. */
83 static bool found_computed_goto;
85 /* Basic blocks and flowgraphs. */
86 static void make_blocks (gimple_seq);
87 static void factor_computed_gotos (void);
90 static void make_edges (void);
91 static void make_cond_expr_edges (basic_block);
92 static void make_gimple_switch_edges (basic_block);
93 static void make_goto_expr_edges (basic_block);
94 static edge gimple_redirect_edge_and_branch (edge, basic_block);
95 static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
96 static unsigned int split_critical_edges (void);
98 /* Various helpers. */
99 static inline bool stmt_starts_bb_p (gimple, gimple);
100 static int gimple_verify_flow_info (void);
101 static void gimple_make_forwarder_block (edge);
102 static void gimple_cfg2vcg (FILE *);
104 /* Flowgraph optimization and cleanup. */
105 static void gimple_merge_blocks (basic_block, basic_block);
106 static bool gimple_can_merge_blocks_p (basic_block, basic_block);
107 static void remove_bb (basic_block);
108 static edge find_taken_edge_computed_goto (basic_block, tree);
109 static edge find_taken_edge_cond_expr (basic_block, tree);
110 static edge find_taken_edge_switch_expr (basic_block, tree);
111 static tree find_case_label_for_value (gimple, tree);
114 init_empty_tree_cfg_for_function (struct function *fn)
116 /* Initialize the basic block array. */
118 profile_status_for_function (fn) = PROFILE_ABSENT;
119 n_basic_blocks_for_function (fn) = NUM_FIXED_BLOCKS;
120 last_basic_block_for_function (fn) = NUM_FIXED_BLOCKS;
121 basic_block_info_for_function (fn)
122 = VEC_alloc (basic_block, gc, initial_cfg_capacity);
123 VEC_safe_grow_cleared (basic_block, gc,
124 basic_block_info_for_function (fn),
125 initial_cfg_capacity);
127 /* Build a mapping of labels to their associated blocks. */
128 label_to_block_map_for_function (fn)
129 = VEC_alloc (basic_block, gc, initial_cfg_capacity);
130 VEC_safe_grow_cleared (basic_block, gc,
131 label_to_block_map_for_function (fn),
132 initial_cfg_capacity);
134 SET_BASIC_BLOCK_FOR_FUNCTION (fn, ENTRY_BLOCK,
135 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn));
136 SET_BASIC_BLOCK_FOR_FUNCTION (fn, EXIT_BLOCK,
137 EXIT_BLOCK_PTR_FOR_FUNCTION (fn));
139 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn)->next_bb
140 = EXIT_BLOCK_PTR_FOR_FUNCTION (fn);
141 EXIT_BLOCK_PTR_FOR_FUNCTION (fn)->prev_bb
142 = ENTRY_BLOCK_PTR_FOR_FUNCTION (fn);
146 init_empty_tree_cfg (void)
148 init_empty_tree_cfg_for_function (cfun);
151 /*---------------------------------------------------------------------------
153 ---------------------------------------------------------------------------*/
155 /* Entry point to the CFG builder for trees. SEQ is the sequence of
156 statements to be added to the flowgraph. */
159 build_gimple_cfg (gimple_seq seq)
161 /* Register specific gimple functions. */
162 gimple_register_cfg_hooks ();
164 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
166 init_empty_tree_cfg ();
168 found_computed_goto = 0;
171 /* Computed gotos are hell to deal with, especially if there are
172 lots of them with a large number of destinations. So we factor
173 them to a common computed goto location before we build the
174 edge list. After we convert back to normal form, we will un-factor
175 the computed gotos since factoring introduces an unwanted jump. */
176 if (found_computed_goto)
177 factor_computed_gotos ();
179 /* Make sure there is always at least one block, even if it's empty. */
180 if (n_basic_blocks == NUM_FIXED_BLOCKS)
181 create_empty_bb (ENTRY_BLOCK_PTR);
183 /* Adjust the size of the array. */
184 if (VEC_length (basic_block, basic_block_info) < (size_t) n_basic_blocks)
185 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, n_basic_blocks);
187 /* To speed up statement iterator walks, we first purge dead labels. */
188 cleanup_dead_labels ();
190 /* Group case nodes to reduce the number of edges.
191 We do this after cleaning up dead labels because otherwise we miss
192 a lot of obvious case merging opportunities. */
193 group_case_labels ();
195 /* Create the edges of the flowgraph. */
197 cleanup_dead_labels ();
199 /* Debugging dumps. */
201 /* Write the flowgraph to a VCG file. */
203 int local_dump_flags;
204 FILE *vcg_file = dump_begin (TDI_vcg, &local_dump_flags);
207 gimple_cfg2vcg (vcg_file);
208 dump_end (TDI_vcg, vcg_file);
212 #ifdef ENABLE_CHECKING
218 execute_build_cfg (void)
220 build_gimple_cfg (gimple_body (current_function_decl));
224 struct gimple_opt_pass pass_build_cfg =
230 execute_build_cfg, /* execute */
233 0, /* static_pass_number */
234 TV_TREE_CFG, /* tv_id */
235 PROP_gimple_leh, /* properties_required */
236 PROP_cfg, /* properties_provided */
237 0, /* properties_destroyed */
238 0, /* todo_flags_start */
239 TODO_verify_stmts | TODO_cleanup_cfg
240 | TODO_dump_func /* todo_flags_finish */
245 /* Return true if T is a computed goto. */
248 computed_goto_p (gimple t)
250 return (gimple_code (t) == GIMPLE_GOTO
251 && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
255 /* Search the CFG for any computed gotos. If found, factor them to a
256 common computed goto site. Also record the location of that site so
257 that we can un-factor the gotos after we have converted back to
261 factor_computed_gotos (void)
264 tree factored_label_decl = NULL;
266 gimple factored_computed_goto_label = NULL;
267 gimple factored_computed_goto = NULL;
269 /* We know there are one or more computed gotos in this function.
270 Examine the last statement in each basic block to see if the block
271 ends with a computed goto. */
275 gimple_stmt_iterator gsi = gsi_last_bb (bb);
281 last = gsi_stmt (gsi);
283 /* Ignore the computed goto we create when we factor the original
285 if (last == factored_computed_goto)
288 /* If the last statement is a computed goto, factor it. */
289 if (computed_goto_p (last))
293 /* The first time we find a computed goto we need to create
294 the factored goto block and the variable each original
295 computed goto will use for their goto destination. */
296 if (!factored_computed_goto)
298 basic_block new_bb = create_empty_bb (bb);
299 gimple_stmt_iterator new_gsi = gsi_start_bb (new_bb);
301 /* Create the destination of the factored goto. Each original
302 computed goto will put its desired destination into this
303 variable and jump to the label we create immediately
305 var = create_tmp_var (ptr_type_node, "gotovar");
307 /* Build a label for the new block which will contain the
308 factored computed goto. */
309 factored_label_decl = create_artificial_label ();
310 factored_computed_goto_label
311 = gimple_build_label (factored_label_decl);
312 gsi_insert_after (&new_gsi, factored_computed_goto_label,
315 /* Build our new computed goto. */
316 factored_computed_goto = gimple_build_goto (var);
317 gsi_insert_after (&new_gsi, factored_computed_goto, GSI_NEW_STMT);
320 /* Copy the original computed goto's destination into VAR. */
321 assignment = gimple_build_assign (var, gimple_goto_dest (last));
322 gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
324 /* And re-vector the computed goto to the new destination. */
325 gimple_goto_set_dest (last, factored_label_decl);
331 /* Build a flowgraph for the sequence of stmts SEQ. */
334 make_blocks (gimple_seq seq)
336 gimple_stmt_iterator i = gsi_start (seq);
338 bool start_new_block = true;
339 bool first_stmt_of_seq = true;
340 basic_block bb = ENTRY_BLOCK_PTR;
342 while (!gsi_end_p (i))
349 /* If the statement starts a new basic block or if we have determined
350 in a previous pass that we need to create a new block for STMT, do
352 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
354 if (!first_stmt_of_seq)
355 seq = gsi_split_seq_before (&i);
356 bb = create_basic_block (seq, NULL, bb);
357 start_new_block = false;
360 /* Now add STMT to BB and create the subgraphs for special statement
362 gimple_set_bb (stmt, bb);
364 if (computed_goto_p (stmt))
365 found_computed_goto = true;
367 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
369 if (stmt_ends_bb_p (stmt))
370 start_new_block = true;
373 first_stmt_of_seq = false;
378 /* Create and return a new empty basic block after bb AFTER. */
381 create_bb (void *h, void *e, basic_block after)
387 /* Create and initialize a new basic block. Since alloc_block uses
388 ggc_alloc_cleared to allocate a basic block, we do not have to
389 clear the newly allocated basic block here. */
392 bb->index = last_basic_block;
394 bb->il.gimple = GGC_CNEW (struct gimple_bb_info);
395 set_bb_seq (bb, h ? (gimple_seq) h : gimple_seq_alloc ());
397 /* Add the new block to the linked list of blocks. */
398 link_block (bb, after);
400 /* Grow the basic block array if needed. */
401 if ((size_t) last_basic_block == VEC_length (basic_block, basic_block_info))
403 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
404 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
407 /* Add the newly created block to the array. */
408 SET_BASIC_BLOCK (last_basic_block, bb);
417 /*---------------------------------------------------------------------------
419 ---------------------------------------------------------------------------*/
421 /* Fold COND_EXPR_COND of each COND_EXPR. */
424 fold_cond_expr_cond (void)
430 gimple stmt = last_stmt (bb);
432 if (stmt && gimple_code (stmt) == GIMPLE_COND)
437 fold_defer_overflow_warnings ();
438 cond = fold_binary (gimple_cond_code (stmt), boolean_type_node,
439 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
442 zerop = integer_zerop (cond);
443 onep = integer_onep (cond);
446 zerop = onep = false;
448 fold_undefer_overflow_warnings (zerop || onep,
450 WARN_STRICT_OVERFLOW_CONDITIONAL);
452 gimple_cond_make_false (stmt);
454 gimple_cond_make_true (stmt);
459 /* Join all the blocks in the flowgraph. */
465 struct omp_region *cur_region = NULL;
467 /* Create an edge from entry to the first block with executable
469 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (NUM_FIXED_BLOCKS), EDGE_FALLTHRU);
471 /* Traverse the basic block array placing edges. */
474 gimple last = last_stmt (bb);
479 enum gimple_code code = gimple_code (last);
483 make_goto_expr_edges (bb);
487 make_edge (bb, EXIT_BLOCK_PTR, 0);
491 make_cond_expr_edges (bb);
495 make_gimple_switch_edges (bb);
499 make_eh_edges (last);
504 /* If this function receives a nonlocal goto, then we need to
505 make edges from this call site to all the nonlocal goto
507 if (stmt_can_make_abnormal_goto (last))
508 make_abnormal_goto_edges (bb, true);
510 /* If this statement has reachable exception handlers, then
511 create abnormal edges to them. */
512 make_eh_edges (last);
514 /* Some calls are known not to return. */
515 fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
519 /* A GIMPLE_ASSIGN may throw internally and thus be considered
521 if (is_ctrl_altering_stmt (last))
523 make_eh_edges (last);
528 case GIMPLE_OMP_PARALLEL:
529 case GIMPLE_OMP_TASK:
531 case GIMPLE_OMP_SINGLE:
532 case GIMPLE_OMP_MASTER:
533 case GIMPLE_OMP_ORDERED:
534 case GIMPLE_OMP_CRITICAL:
535 case GIMPLE_OMP_SECTION:
536 cur_region = new_omp_region (bb, code, cur_region);
540 case GIMPLE_OMP_SECTIONS:
541 cur_region = new_omp_region (bb, code, cur_region);
545 case GIMPLE_OMP_SECTIONS_SWITCH:
550 case GIMPLE_OMP_ATOMIC_LOAD:
551 case GIMPLE_OMP_ATOMIC_STORE:
556 case GIMPLE_OMP_RETURN:
557 /* In the case of a GIMPLE_OMP_SECTION, the edge will go
558 somewhere other than the next block. This will be
560 cur_region->exit = bb;
561 fallthru = cur_region->type != GIMPLE_OMP_SECTION;
562 cur_region = cur_region->outer;
565 case GIMPLE_OMP_CONTINUE:
566 cur_region->cont = bb;
567 switch (cur_region->type)
570 /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
571 succs edges as abnormal to prevent splitting
573 single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
574 /* Make the loopback edge. */
575 make_edge (bb, single_succ (cur_region->entry),
578 /* Create an edge from GIMPLE_OMP_FOR to exit, which
579 corresponds to the case that the body of the loop
580 is not executed at all. */
581 make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
582 make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
586 case GIMPLE_OMP_SECTIONS:
587 /* Wire up the edges into and out of the nested sections. */
589 basic_block switch_bb = single_succ (cur_region->entry);
591 struct omp_region *i;
592 for (i = cur_region->inner; i ; i = i->next)
594 gcc_assert (i->type == GIMPLE_OMP_SECTION);
595 make_edge (switch_bb, i->entry, 0);
596 make_edge (i->exit, bb, EDGE_FALLTHRU);
599 /* Make the loopback edge to the block with
600 GIMPLE_OMP_SECTIONS_SWITCH. */
601 make_edge (bb, switch_bb, 0);
603 /* Make the edge from the switch to exit. */
604 make_edge (switch_bb, bb->next_bb, 0);
615 gcc_assert (!stmt_ends_bb_p (last));
623 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
629 /* Fold COND_EXPR_COND of each COND_EXPR. */
630 fold_cond_expr_cond ();
634 /* Create the edges for a GIMPLE_COND starting at block BB. */
637 make_cond_expr_edges (basic_block bb)
639 gimple entry = last_stmt (bb);
640 gimple then_stmt, else_stmt;
641 basic_block then_bb, else_bb;
642 tree then_label, else_label;
646 gcc_assert (gimple_code (entry) == GIMPLE_COND);
648 /* Entry basic blocks for each component. */
649 then_label = gimple_cond_true_label (entry);
650 else_label = gimple_cond_false_label (entry);
651 then_bb = label_to_block (then_label);
652 else_bb = label_to_block (else_label);
653 then_stmt = first_stmt (then_bb);
654 else_stmt = first_stmt (else_bb);
656 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
657 e->goto_locus = gimple_location (then_stmt);
658 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
660 e->goto_locus = gimple_location (else_stmt);
662 /* We do not need the labels anymore. */
663 gimple_cond_set_true_label (entry, NULL_TREE);
664 gimple_cond_set_false_label (entry, NULL_TREE);
668 /* Called for each element in the hash table (P) as we delete the
669 edge to cases hash table.
671 Clear all the TREE_CHAINs to prevent problems with copying of
672 SWITCH_EXPRs and structure sharing rules, then free the hash table
676 edge_to_cases_cleanup (const void *key ATTRIBUTE_UNUSED, void **value,
677 void *data ATTRIBUTE_UNUSED)
681 for (t = (tree) *value; t; t = next)
683 next = TREE_CHAIN (t);
684 TREE_CHAIN (t) = NULL;
691 /* Start recording information mapping edges to case labels. */
694 start_recording_case_labels (void)
696 gcc_assert (edge_to_cases == NULL);
697 edge_to_cases = pointer_map_create ();
700 /* Return nonzero if we are recording information for case labels. */
703 recording_case_labels_p (void)
705 return (edge_to_cases != NULL);
708 /* Stop recording information mapping edges to case labels and
709 remove any information we have recorded. */
711 end_recording_case_labels (void)
713 pointer_map_traverse (edge_to_cases, edge_to_cases_cleanup, NULL);
714 pointer_map_destroy (edge_to_cases);
715 edge_to_cases = NULL;
718 /* If we are inside a {start,end}_recording_cases block, then return
719 a chain of CASE_LABEL_EXPRs from T which reference E.
721 Otherwise return NULL. */
724 get_cases_for_edge (edge e, gimple t)
729 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
730 chains available. Return NULL so the caller can detect this case. */
731 if (!recording_case_labels_p ())
734 slot = pointer_map_contains (edge_to_cases, e);
738 /* If we did not find E in the hash table, then this must be the first
739 time we have been queried for information about E & T. Add all the
740 elements from T to the hash table then perform the query again. */
742 n = gimple_switch_num_labels (t);
743 for (i = 0; i < n; i++)
745 tree elt = gimple_switch_label (t, i);
746 tree lab = CASE_LABEL (elt);
747 basic_block label_bb = label_to_block (lab);
748 edge this_edge = find_edge (e->src, label_bb);
750 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
752 slot = pointer_map_insert (edge_to_cases, this_edge);
753 TREE_CHAIN (elt) = (tree) *slot;
757 return (tree) *pointer_map_contains (edge_to_cases, e);
760 /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
763 make_gimple_switch_edges (basic_block bb)
765 gimple entry = last_stmt (bb);
768 n = gimple_switch_num_labels (entry);
770 for (i = 0; i < n; ++i)
772 tree lab = CASE_LABEL (gimple_switch_label (entry, i));
773 basic_block label_bb = label_to_block (lab);
774 make_edge (bb, label_bb, 0);
779 /* Return the basic block holding label DEST. */
782 label_to_block_fn (struct function *ifun, tree dest)
784 int uid = LABEL_DECL_UID (dest);
786 /* We would die hard when faced by an undefined label. Emit a label to
787 the very first basic block. This will hopefully make even the dataflow
788 and undefined variable warnings quite right. */
789 if ((errorcount || sorrycount) && uid < 0)
791 gimple_stmt_iterator gsi = gsi_start_bb (BASIC_BLOCK (NUM_FIXED_BLOCKS));
794 stmt = gimple_build_label (dest);
795 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
796 uid = LABEL_DECL_UID (dest);
798 if (VEC_length (basic_block, ifun->cfg->x_label_to_block_map)
799 <= (unsigned int) uid)
801 return VEC_index (basic_block, ifun->cfg->x_label_to_block_map, uid);
804 /* Create edges for an abnormal goto statement at block BB. If FOR_CALL
805 is true, the source statement is a CALL_EXPR instead of a GOTO_EXPR. */
808 make_abnormal_goto_edges (basic_block bb, bool for_call)
810 basic_block target_bb;
811 gimple_stmt_iterator gsi;
813 FOR_EACH_BB (target_bb)
814 for (gsi = gsi_start_bb (target_bb); !gsi_end_p (gsi); gsi_next (&gsi))
816 gimple label_stmt = gsi_stmt (gsi);
819 if (gimple_code (label_stmt) != GIMPLE_LABEL)
822 target = gimple_label_label (label_stmt);
824 /* Make an edge to every label block that has been marked as a
825 potential target for a computed goto or a non-local goto. */
826 if ((FORCED_LABEL (target) && !for_call)
827 || (DECL_NONLOCAL (target) && for_call))
829 make_edge (bb, target_bb, EDGE_ABNORMAL);
835 /* Create edges for a goto statement at block BB. */
838 make_goto_expr_edges (basic_block bb)
840 gimple_stmt_iterator last = gsi_last_bb (bb);
841 gimple goto_t = gsi_stmt (last);
843 /* A simple GOTO creates normal edges. */
844 if (simple_goto_p (goto_t))
846 tree dest = gimple_goto_dest (goto_t);
847 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
848 e->goto_locus = gimple_location (goto_t);
849 gsi_remove (&last, true);
853 /* A computed GOTO creates abnormal edges. */
854 make_abnormal_goto_edges (bb, false);
858 /*---------------------------------------------------------------------------
860 ---------------------------------------------------------------------------*/
862 /* Cleanup useless labels in basic blocks. This is something we wish
863 to do early because it allows us to group case labels before creating
864 the edges for the CFG, and it speeds up block statement iterators in
866 We rerun this pass after CFG is created, to get rid of the labels that
867 are no longer referenced. After then we do not run it any more, since
868 (almost) no new labels should be created. */
870 /* A map from basic block index to the leading label of that block. */
871 static struct label_record
876 /* True if the label is referenced from somewhere. */
880 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
882 update_eh_label (struct eh_region *region)
884 tree old_label = get_eh_region_tree_label (region);
888 basic_block bb = label_to_block (old_label);
890 /* ??? After optimizing, there may be EH regions with labels
891 that have already been removed from the function body, so
892 there is no basic block for them. */
896 new_label = label_for_bb[bb->index].label;
897 label_for_bb[bb->index].used = true;
898 set_eh_region_tree_label (region, new_label);
903 /* Given LABEL return the first label in the same basic block. */
906 main_block_label (tree label)
908 basic_block bb = label_to_block (label);
909 tree main_label = label_for_bb[bb->index].label;
911 /* label_to_block possibly inserted undefined label into the chain. */
914 label_for_bb[bb->index].label = label;
918 label_for_bb[bb->index].used = true;
922 /* Cleanup redundant labels. This is a three-step process:
923 1) Find the leading label for each block.
924 2) Redirect all references to labels to the leading labels.
925 3) Cleanup all useless labels. */
928 cleanup_dead_labels (void)
931 label_for_bb = XCNEWVEC (struct label_record, last_basic_block);
933 /* Find a suitable label for each block. We use the first user-defined
934 label if there is one, or otherwise just the first label we see. */
937 gimple_stmt_iterator i;
939 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
942 gimple stmt = gsi_stmt (i);
944 if (gimple_code (stmt) != GIMPLE_LABEL)
947 label = gimple_label_label (stmt);
949 /* If we have not yet seen a label for the current block,
950 remember this one and see if there are more labels. */
951 if (!label_for_bb[bb->index].label)
953 label_for_bb[bb->index].label = label;
957 /* If we did see a label for the current block already, but it
958 is an artificially created label, replace it if the current
959 label is a user defined label. */
960 if (!DECL_ARTIFICIAL (label)
961 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
963 label_for_bb[bb->index].label = label;
969 /* Now redirect all jumps/branches to the selected label.
970 First do so for each block ending in a control statement. */
973 gimple stmt = last_stmt (bb);
977 switch (gimple_code (stmt))
981 tree true_label = gimple_cond_true_label (stmt);
982 tree false_label = gimple_cond_false_label (stmt);
985 gimple_cond_set_true_label (stmt, main_block_label (true_label));
987 gimple_cond_set_false_label (stmt, main_block_label (false_label));
993 size_t i, n = gimple_switch_num_labels (stmt);
995 /* Replace all destination labels. */
996 for (i = 0; i < n; ++i)
998 tree case_label = gimple_switch_label (stmt, i);
999 tree label = main_block_label (CASE_LABEL (case_label));
1000 CASE_LABEL (case_label) = label;
1005 /* We have to handle gotos until they're removed, and we don't
1006 remove them until after we've created the CFG edges. */
1008 if (!computed_goto_p (stmt))
1010 tree new_dest = main_block_label (gimple_goto_dest (stmt));
1011 gimple_goto_set_dest (stmt, new_dest);
1020 for_each_eh_region (update_eh_label);
1022 /* Finally, purge dead labels. All user-defined labels and labels that
1023 can be the target of non-local gotos and labels which have their
1024 address taken are preserved. */
1027 gimple_stmt_iterator i;
1028 tree label_for_this_bb = label_for_bb[bb->index].label;
1030 if (!label_for_this_bb)
1033 /* If the main label of the block is unused, we may still remove it. */
1034 if (!label_for_bb[bb->index].used)
1035 label_for_this_bb = NULL;
1037 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1040 gimple stmt = gsi_stmt (i);
1042 if (gimple_code (stmt) != GIMPLE_LABEL)
1045 label = gimple_label_label (stmt);
1047 if (label == label_for_this_bb
1048 || !DECL_ARTIFICIAL (label)
1049 || DECL_NONLOCAL (label)
1050 || FORCED_LABEL (label))
1053 gsi_remove (&i, true);
1057 free (label_for_bb);
1060 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1061 and scan the sorted vector of cases. Combine the ones jumping to the
1063 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1066 group_case_labels (void)
1072 gimple stmt = last_stmt (bb);
1073 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1075 int old_size = gimple_switch_num_labels (stmt);
1076 int i, j, new_size = old_size;
1077 tree default_case = NULL_TREE;
1078 tree default_label = NULL_TREE;
1081 /* The default label is always the first case in a switch
1082 statement after gimplification if it was not optimized
1084 if (!CASE_LOW (gimple_switch_default_label (stmt))
1085 && !CASE_HIGH (gimple_switch_default_label (stmt)))
1087 default_case = gimple_switch_default_label (stmt);
1088 default_label = CASE_LABEL (default_case);
1092 has_default = false;
1094 /* Look for possible opportunities to merge cases. */
1099 while (i < old_size)
1101 tree base_case, base_label, base_high;
1102 base_case = gimple_switch_label (stmt, i);
1104 gcc_assert (base_case);
1105 base_label = CASE_LABEL (base_case);
1107 /* Discard cases that have the same destination as the
1109 if (base_label == default_label)
1111 gimple_switch_set_label (stmt, i, NULL_TREE);
1117 base_high = CASE_HIGH (base_case)
1118 ? CASE_HIGH (base_case)
1119 : CASE_LOW (base_case);
1122 /* Try to merge case labels. Break out when we reach the end
1123 of the label vector or when we cannot merge the next case
1124 label with the current one. */
1125 while (i < old_size)
1127 tree merge_case = gimple_switch_label (stmt, i);
1128 tree merge_label = CASE_LABEL (merge_case);
1129 tree t = int_const_binop (PLUS_EXPR, base_high,
1130 integer_one_node, 1);
1132 /* Merge the cases if they jump to the same place,
1133 and their ranges are consecutive. */
1134 if (merge_label == base_label
1135 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1137 base_high = CASE_HIGH (merge_case) ?
1138 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1139 CASE_HIGH (base_case) = base_high;
1140 gimple_switch_set_label (stmt, i, NULL_TREE);
1149 /* Compress the case labels in the label vector, and adjust the
1150 length of the vector. */
1151 for (i = 0, j = 0; i < new_size; i++)
1153 while (! gimple_switch_label (stmt, j))
1155 gimple_switch_set_label (stmt, i,
1156 gimple_switch_label (stmt, j++));
1159 gcc_assert (new_size <= old_size);
1160 gimple_switch_set_num_labels (stmt, new_size);
1165 /* Checks whether we can merge block B into block A. */
1168 gimple_can_merge_blocks_p (basic_block a, basic_block b)
1171 gimple_stmt_iterator gsi;
1174 if (!single_succ_p (a))
1177 if (single_succ_edge (a)->flags & EDGE_ABNORMAL)
1180 if (single_succ (a) != b)
1183 if (!single_pred_p (b))
1186 if (b == EXIT_BLOCK_PTR)
1189 /* If A ends by a statement causing exceptions or something similar, we
1190 cannot merge the blocks. */
1191 stmt = last_stmt (a);
1192 if (stmt && stmt_ends_bb_p (stmt))
1195 /* Do not allow a block with only a non-local label to be merged. */
1197 && gimple_code (stmt) == GIMPLE_LABEL
1198 && DECL_NONLOCAL (gimple_label_label (stmt)))
1201 /* It must be possible to eliminate all phi nodes in B. If ssa form
1202 is not up-to-date, we cannot eliminate any phis; however, if only
1203 some symbols as whole are marked for renaming, this is not a problem,
1204 as phi nodes for those symbols are irrelevant in updating anyway. */
1205 phis = phi_nodes (b);
1206 if (!gimple_seq_empty_p (phis))
1208 gimple_stmt_iterator i;
1210 if (name_mappings_registered_p ())
1213 for (i = gsi_start (phis); !gsi_end_p (i); gsi_next (&i))
1215 gimple phi = gsi_stmt (i);
1217 if (!is_gimple_reg (gimple_phi_result (phi))
1218 && !may_propagate_copy (gimple_phi_result (phi),
1219 gimple_phi_arg_def (phi, 0)))
1224 /* Do not remove user labels. */
1225 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
1227 stmt = gsi_stmt (gsi);
1228 if (gimple_code (stmt) != GIMPLE_LABEL)
1230 if (!DECL_ARTIFICIAL (gimple_label_label (stmt)))
1234 /* Protect the loop latches. */
1236 && b->loop_father->latch == b)
1242 /* Replaces all uses of NAME by VAL. */
1245 replace_uses_by (tree name, tree val)
1247 imm_use_iterator imm_iter;
1252 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1254 if (gimple_code (stmt) != GIMPLE_PHI)
1255 push_stmt_changes (&stmt);
1257 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1259 replace_exp (use, val);
1261 if (gimple_code (stmt) == GIMPLE_PHI)
1263 e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
1264 if (e->flags & EDGE_ABNORMAL)
1266 /* This can only occur for virtual operands, since
1267 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1268 would prevent replacement. */
1269 gcc_assert (!is_gimple_reg (name));
1270 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1275 if (gimple_code (stmt) != GIMPLE_PHI)
1279 fold_stmt_inplace (stmt);
1280 if (cfgcleanup_altered_bbs)
1281 bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1283 /* FIXME. This should go in pop_stmt_changes. */
1284 for (i = 0; i < gimple_num_ops (stmt); i++)
1286 tree op = gimple_op (stmt, i);
1287 /* Operands may be empty here. For example, the labels
1288 of a GIMPLE_COND are nulled out following the creation
1289 of the corresponding CFG edges. */
1290 if (op && TREE_CODE (op) == ADDR_EXPR)
1291 recompute_tree_invariant_for_addr_expr (op);
1294 maybe_clean_or_replace_eh_stmt (stmt, stmt);
1296 pop_stmt_changes (&stmt);
1300 gcc_assert (has_zero_uses (name));
1302 /* Also update the trees stored in loop structures. */
1308 FOR_EACH_LOOP (li, loop, 0)
1310 substitute_in_loop_info (loop, name, val);
1315 /* Merge block B into block A. */
1318 gimple_merge_blocks (basic_block a, basic_block b)
1320 gimple_stmt_iterator last, gsi, psi;
1321 gimple_seq phis = phi_nodes (b);
1324 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1326 /* Remove all single-valued PHI nodes from block B of the form
1327 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1328 gsi = gsi_last_bb (a);
1329 for (psi = gsi_start (phis); !gsi_end_p (psi); )
1331 gimple phi = gsi_stmt (psi);
1332 tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
1334 bool may_replace_uses = !is_gimple_reg (def)
1335 || may_propagate_copy (def, use);
1337 /* In case we maintain loop closed ssa form, do not propagate arguments
1338 of loop exit phi nodes. */
1340 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1341 && is_gimple_reg (def)
1342 && TREE_CODE (use) == SSA_NAME
1343 && a->loop_father != b->loop_father)
1344 may_replace_uses = false;
1346 if (!may_replace_uses)
1348 gcc_assert (is_gimple_reg (def));
1350 /* Note that just emitting the copies is fine -- there is no problem
1351 with ordering of phi nodes. This is because A is the single
1352 predecessor of B, therefore results of the phi nodes cannot
1353 appear as arguments of the phi nodes. */
1354 copy = gimple_build_assign (def, use);
1355 gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
1356 remove_phi_node (&psi, false);
1360 /* If we deal with a PHI for virtual operands, we can simply
1361 propagate these without fussing with folding or updating
1363 if (!is_gimple_reg (def))
1365 imm_use_iterator iter;
1366 use_operand_p use_p;
1369 FOR_EACH_IMM_USE_STMT (stmt, iter, def)
1370 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1371 SET_USE (use_p, use);
1374 replace_uses_by (def, use);
1376 remove_phi_node (&psi, true);
1380 /* Ensure that B follows A. */
1381 move_block_after (b, a);
1383 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1384 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1386 /* Remove labels from B and set gimple_bb to A for other statements. */
1387 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
1389 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1391 gimple label = gsi_stmt (gsi);
1393 gsi_remove (&gsi, false);
1395 /* Now that we can thread computed gotos, we might have
1396 a situation where we have a forced label in block B
1397 However, the label at the start of block B might still be
1398 used in other ways (think about the runtime checking for
1399 Fortran assigned gotos). So we can not just delete the
1400 label. Instead we move the label to the start of block A. */
1401 if (FORCED_LABEL (gimple_label_label (label)))
1403 gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
1404 gsi_insert_before (&dest_gsi, label, GSI_NEW_STMT);
1409 gimple_set_bb (gsi_stmt (gsi), a);
1414 /* Merge the sequences. */
1415 last = gsi_last_bb (a);
1416 gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
1417 set_bb_seq (b, NULL);
1419 if (cfgcleanup_altered_bbs)
1420 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1424 /* Return the one of two successors of BB that is not reachable by a
1425 reached by a complex edge, if there is one. Else, return BB. We use
1426 this in optimizations that use post-dominators for their heuristics,
1427 to catch the cases in C++ where function calls are involved. */
1430 single_noncomplex_succ (basic_block bb)
1433 if (EDGE_COUNT (bb->succs) != 2)
1436 e0 = EDGE_SUCC (bb, 0);
1437 e1 = EDGE_SUCC (bb, 1);
1438 if (e0->flags & EDGE_COMPLEX)
1440 if (e1->flags & EDGE_COMPLEX)
1447 /* Walk the function tree removing unnecessary statements.
1449 * Empty statement nodes are removed
1451 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1453 * Unnecessary COND_EXPRs are removed
1455 * Some unnecessary BIND_EXPRs are removed
1457 * GOTO_EXPRs immediately preceding destination are removed.
1459 Clearly more work could be done. The trick is doing the analysis
1460 and removal fast enough to be a net improvement in compile times.
1462 Note that when we remove a control structure such as a COND_EXPR
1463 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1464 to ensure we eliminate all the useless code. */
1473 gimple_stmt_iterator last_goto_gsi;
1477 static void remove_useless_stmts_1 (gimple_stmt_iterator *gsi, struct rus_data *);
1479 /* Given a statement sequence, find the first executable statement with
1480 location information, and warn that it is unreachable. When searching,
1481 descend into containers in execution order. */
1484 remove_useless_stmts_warn_notreached (gimple_seq stmts)
1486 gimple_stmt_iterator gsi;
1488 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
1490 gimple stmt = gsi_stmt (gsi);
1492 if (gimple_has_location (stmt))
1494 location_t loc = gimple_location (stmt);
1495 if (LOCATION_LINE (loc) > 0)
1497 warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
1502 switch (gimple_code (stmt))
1504 /* Unfortunately, we need the CFG now to detect unreachable
1505 branches in a conditional, so conditionals are not handled here. */
1508 if (remove_useless_stmts_warn_notreached (gimple_try_eval (stmt)))
1510 if (remove_useless_stmts_warn_notreached (gimple_try_cleanup (stmt)))
1515 return remove_useless_stmts_warn_notreached (gimple_catch_handler (stmt));
1517 case GIMPLE_EH_FILTER:
1518 return remove_useless_stmts_warn_notreached (gimple_eh_filter_failure (stmt));
1521 return remove_useless_stmts_warn_notreached (gimple_bind_body (stmt));
1531 /* Helper for remove_useless_stmts_1. Handle GIMPLE_COND statements. */
1534 remove_useless_stmts_cond (gimple_stmt_iterator *gsi, struct rus_data *data)
1536 gimple stmt = gsi_stmt (*gsi);
1538 /* The folded result must still be a conditional statement. */
1539 fold_stmt_inplace (stmt);
1541 data->may_branch = true;
1543 /* Replace trivial conditionals with gotos. */
1544 if (gimple_cond_true_p (stmt))
1546 /* Goto THEN label. */
1547 tree then_label = gimple_cond_true_label (stmt);
1549 gsi_replace (gsi, gimple_build_goto (then_label), false);
1550 data->last_goto_gsi = *gsi;
1551 data->last_was_goto = true;
1552 data->repeat = true;
1554 else if (gimple_cond_false_p (stmt))
1556 /* Goto ELSE label. */
1557 tree else_label = gimple_cond_false_label (stmt);
1559 gsi_replace (gsi, gimple_build_goto (else_label), false);
1560 data->last_goto_gsi = *gsi;
1561 data->last_was_goto = true;
1562 data->repeat = true;
1566 tree then_label = gimple_cond_true_label (stmt);
1567 tree else_label = gimple_cond_false_label (stmt);
1569 if (then_label == else_label)
1571 /* Goto common destination. */
1572 gsi_replace (gsi, gimple_build_goto (then_label), false);
1573 data->last_goto_gsi = *gsi;
1574 data->last_was_goto = true;
1575 data->repeat = true;
1581 data->last_was_goto = false;
1584 /* Helper for remove_useless_stmts_1.
1585 Handle the try-finally case for GIMPLE_TRY statements. */
1588 remove_useless_stmts_tf (gimple_stmt_iterator *gsi, struct rus_data *data)
1590 bool save_may_branch, save_may_throw;
1591 bool this_may_branch, this_may_throw;
1593 gimple_seq eval_seq, cleanup_seq;
1594 gimple_stmt_iterator eval_gsi, cleanup_gsi;
1596 gimple stmt = gsi_stmt (*gsi);
1598 /* Collect may_branch and may_throw information for the body only. */
1599 save_may_branch = data->may_branch;
1600 save_may_throw = data->may_throw;
1601 data->may_branch = false;
1602 data->may_throw = false;
1603 data->last_was_goto = false;
1605 eval_seq = gimple_try_eval (stmt);
1606 eval_gsi = gsi_start (eval_seq);
1607 remove_useless_stmts_1 (&eval_gsi, data);
1609 this_may_branch = data->may_branch;
1610 this_may_throw = data->may_throw;
1611 data->may_branch |= save_may_branch;
1612 data->may_throw |= save_may_throw;
1613 data->last_was_goto = false;
1615 cleanup_seq = gimple_try_cleanup (stmt);
1616 cleanup_gsi = gsi_start (cleanup_seq);
1617 remove_useless_stmts_1 (&cleanup_gsi, data);
1619 /* If the body is empty, then we can emit the FINALLY block without
1620 the enclosing TRY_FINALLY_EXPR. */
1621 if (gimple_seq_empty_p (eval_seq))
1623 gsi_insert_seq_before (gsi, cleanup_seq, GSI_SAME_STMT);
1624 gsi_remove (gsi, false);
1625 data->repeat = true;
1628 /* If the handler is empty, then we can emit the TRY block without
1629 the enclosing TRY_FINALLY_EXPR. */
1630 else if (gimple_seq_empty_p (cleanup_seq))
1632 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1633 gsi_remove (gsi, false);
1634 data->repeat = true;
1637 /* If the body neither throws, nor branches, then we can safely
1638 string the TRY and FINALLY blocks together. */
1639 else if (!this_may_branch && !this_may_throw)
1641 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1642 gsi_insert_seq_before (gsi, cleanup_seq, GSI_SAME_STMT);
1643 gsi_remove (gsi, false);
1644 data->repeat = true;
1650 /* Helper for remove_useless_stmts_1.
1651 Handle the try-catch case for GIMPLE_TRY statements. */
1654 remove_useless_stmts_tc (gimple_stmt_iterator *gsi, struct rus_data *data)
1656 bool save_may_throw, this_may_throw;
1658 gimple_seq eval_seq, cleanup_seq, handler_seq, failure_seq;
1659 gimple_stmt_iterator eval_gsi, cleanup_gsi, handler_gsi, failure_gsi;
1661 gimple stmt = gsi_stmt (*gsi);
1663 /* Collect may_throw information for the body only. */
1664 save_may_throw = data->may_throw;
1665 data->may_throw = false;
1666 data->last_was_goto = false;
1668 eval_seq = gimple_try_eval (stmt);
1669 eval_gsi = gsi_start (eval_seq);
1670 remove_useless_stmts_1 (&eval_gsi, data);
1672 this_may_throw = data->may_throw;
1673 data->may_throw = save_may_throw;
1675 cleanup_seq = gimple_try_cleanup (stmt);
1677 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1678 if (!this_may_throw)
1680 if (warn_notreached)
1682 remove_useless_stmts_warn_notreached (cleanup_seq);
1684 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1685 gsi_remove (gsi, false);
1686 data->repeat = true;
1690 /* Process the catch clause specially. We may be able to tell that
1691 no exceptions propagate past this point. */
1693 this_may_throw = true;
1694 cleanup_gsi = gsi_start (cleanup_seq);
1695 stmt = gsi_stmt (cleanup_gsi);
1696 data->last_was_goto = false;
1698 switch (gimple_code (stmt))
1701 /* If the first element is a catch, they all must be. */
1702 while (!gsi_end_p (cleanup_gsi))
1704 stmt = gsi_stmt (cleanup_gsi);
1705 /* If we catch all exceptions, then the body does not
1706 propagate exceptions past this point. */
1707 if (gimple_catch_types (stmt) == NULL)
1708 this_may_throw = false;
1709 data->last_was_goto = false;
1710 handler_seq = gimple_catch_handler (stmt);
1711 handler_gsi = gsi_start (handler_seq);
1712 remove_useless_stmts_1 (&handler_gsi, data);
1713 gsi_next (&cleanup_gsi);
1718 case GIMPLE_EH_FILTER:
1719 /* If the first element is an eh_filter, it should stand alone. */
1720 if (gimple_eh_filter_must_not_throw (stmt))
1721 this_may_throw = false;
1722 else if (gimple_eh_filter_types (stmt) == NULL)
1723 this_may_throw = false;
1724 failure_seq = gimple_eh_filter_failure (stmt);
1725 failure_gsi = gsi_start (failure_seq);
1726 remove_useless_stmts_1 (&failure_gsi, data);
1731 /* Otherwise this is a list of cleanup statements. */
1732 remove_useless_stmts_1 (&cleanup_gsi, data);
1734 /* If the cleanup is empty, then we can emit the TRY block without
1735 the enclosing TRY_CATCH_EXPR. */
1736 if (gimple_seq_empty_p (cleanup_seq))
1738 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1739 gsi_remove(gsi, false);
1740 data->repeat = true;
1747 data->may_throw |= this_may_throw;
1750 /* Helper for remove_useless_stmts_1. Handle GIMPLE_BIND statements. */
1753 remove_useless_stmts_bind (gimple_stmt_iterator *gsi, struct rus_data *data ATTRIBUTE_UNUSED)
1756 gimple_seq body_seq, fn_body_seq;
1757 gimple_stmt_iterator body_gsi;
1759 gimple stmt = gsi_stmt (*gsi);
1761 /* First remove anything underneath the BIND_EXPR. */
1763 body_seq = gimple_bind_body (stmt);
1764 body_gsi = gsi_start (body_seq);
1765 remove_useless_stmts_1 (&body_gsi, data);
1767 /* If the GIMPLE_BIND has no variables, then we can pull everything
1768 up one level and remove the GIMPLE_BIND, unless this is the toplevel
1769 GIMPLE_BIND for the current function or an inlined function.
1771 When this situation occurs we will want to apply this
1772 optimization again. */
1773 block = gimple_bind_block (stmt);
1774 fn_body_seq = gimple_body (current_function_decl);
1775 if (gimple_bind_vars (stmt) == NULL_TREE
1776 && (gimple_seq_empty_p (fn_body_seq)
1777 || stmt != gimple_seq_first_stmt (fn_body_seq))
1779 || ! BLOCK_ABSTRACT_ORIGIN (block)
1780 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1783 gsi_insert_seq_before (gsi, body_seq, GSI_SAME_STMT);
1784 gsi_remove (gsi, false);
1785 data->repeat = true;
1791 /* Helper for remove_useless_stmts_1. Handle GIMPLE_GOTO statements. */
1794 remove_useless_stmts_goto (gimple_stmt_iterator *gsi, struct rus_data *data)
1796 gimple stmt = gsi_stmt (*gsi);
1798 tree dest = gimple_goto_dest (stmt);
1800 data->may_branch = true;
1801 data->last_was_goto = false;
1803 /* Record iterator for last goto expr, so that we can delete it if unnecessary. */
1804 if (TREE_CODE (dest) == LABEL_DECL)
1806 data->last_goto_gsi = *gsi;
1807 data->last_was_goto = true;
1813 /* Helper for remove_useless_stmts_1. Handle GIMPLE_LABEL statements. */
1816 remove_useless_stmts_label (gimple_stmt_iterator *gsi, struct rus_data *data)
1818 gimple stmt = gsi_stmt (*gsi);
1820 tree label = gimple_label_label (stmt);
1822 data->has_label = true;
1824 /* We do want to jump across non-local label receiver code. */
1825 if (DECL_NONLOCAL (label))
1826 data->last_was_goto = false;
1828 else if (data->last_was_goto
1829 && gimple_goto_dest (gsi_stmt (data->last_goto_gsi)) == label)
1831 /* Replace the preceding GIMPLE_GOTO statement with
1832 a GIMPLE_NOP, which will be subsequently removed.
1833 In this way, we avoid invalidating other iterators
1834 active on the statement sequence. */
1835 gsi_replace(&data->last_goto_gsi, gimple_build_nop(), false);
1836 data->last_was_goto = false;
1837 data->repeat = true;
1840 /* ??? Add something here to delete unused labels. */
1846 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1849 notice_special_calls (gimple call)
1851 int flags = gimple_call_flags (call);
1853 if (flags & ECF_MAY_BE_ALLOCA)
1854 cfun->calls_alloca = true;
1855 if (flags & ECF_RETURNS_TWICE)
1856 cfun->calls_setjmp = true;
1860 /* Clear flags set by notice_special_calls. Used by dead code removal
1861 to update the flags. */
1864 clear_special_calls (void)
1866 cfun->calls_alloca = false;
1867 cfun->calls_setjmp = false;
1870 /* Remove useless statements from a statement sequence, and perform
1871 some preliminary simplifications. */
1874 remove_useless_stmts_1 (gimple_stmt_iterator *gsi, struct rus_data *data)
1876 while (!gsi_end_p (*gsi))
1878 gimple stmt = gsi_stmt (*gsi);
1880 switch (gimple_code (stmt))
1883 remove_useless_stmts_cond (gsi, data);
1887 remove_useless_stmts_goto (gsi, data);
1891 remove_useless_stmts_label (gsi, data);
1896 stmt = gsi_stmt (*gsi);
1897 data->last_was_goto = false;
1898 if (stmt_could_throw_p (stmt))
1899 data->may_throw = true;
1905 data->last_was_goto = false;
1911 stmt = gsi_stmt (*gsi);
1912 data->last_was_goto = false;
1913 if (is_gimple_call (stmt))
1914 notice_special_calls (stmt);
1916 /* We used to call update_gimple_call_flags here,
1917 which copied side-effects and nothrows status
1918 from the function decl to the call. In the new
1919 tuplified GIMPLE, the accessors for this information
1920 always consult the function decl, so this copying
1921 is no longer necessary. */
1922 if (stmt_could_throw_p (stmt))
1923 data->may_throw = true;
1929 data->last_was_goto = false;
1930 data->may_branch = true;
1935 remove_useless_stmts_bind (gsi, data);
1939 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
1940 remove_useless_stmts_tc (gsi, data);
1941 else if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
1942 remove_useless_stmts_tf (gsi, data);
1952 gsi_remove (gsi, false);
1955 case GIMPLE_OMP_FOR:
1957 gimple_seq pre_body_seq = gimple_omp_for_pre_body (stmt);
1958 gimple_stmt_iterator pre_body_gsi = gsi_start (pre_body_seq);
1960 remove_useless_stmts_1 (&pre_body_gsi, data);
1961 data->last_was_goto = false;
1964 case GIMPLE_OMP_CRITICAL:
1965 case GIMPLE_OMP_CONTINUE:
1966 case GIMPLE_OMP_MASTER:
1967 case GIMPLE_OMP_ORDERED:
1968 case GIMPLE_OMP_SECTION:
1969 case GIMPLE_OMP_SECTIONS:
1970 case GIMPLE_OMP_SINGLE:
1972 gimple_seq body_seq = gimple_omp_body (stmt);
1973 gimple_stmt_iterator body_gsi = gsi_start (body_seq);
1975 remove_useless_stmts_1 (&body_gsi, data);
1976 data->last_was_goto = false;
1981 case GIMPLE_OMP_PARALLEL:
1982 case GIMPLE_OMP_TASK:
1984 /* Make sure the outermost GIMPLE_BIND isn't removed
1986 gimple_seq body_seq = gimple_omp_body (stmt);
1987 gimple bind = gimple_seq_first_stmt (body_seq);
1988 gimple_seq bind_seq = gimple_bind_body (bind);
1989 gimple_stmt_iterator bind_gsi = gsi_start (bind_seq);
1991 remove_useless_stmts_1 (&bind_gsi, data);
1992 data->last_was_goto = false;
1998 data->last_was_goto = false;
2005 /* Walk the function tree, removing useless statements and performing
2006 some preliminary simplifications. */
2009 remove_useless_stmts (void)
2011 struct rus_data data;
2013 clear_special_calls ();
2017 gimple_stmt_iterator gsi;
2019 gsi = gsi_start (gimple_body (current_function_decl));
2020 memset (&data, 0, sizeof (data));
2021 remove_useless_stmts_1 (&gsi, &data);
2023 while (data.repeat);
2028 struct gimple_opt_pass pass_remove_useless_stmts =
2032 "useless", /* name */
2034 remove_useless_stmts, /* execute */
2037 0, /* static_pass_number */
2039 PROP_gimple_any, /* properties_required */
2040 0, /* properties_provided */
2041 0, /* properties_destroyed */
2042 0, /* todo_flags_start */
2043 TODO_dump_func /* todo_flags_finish */
2047 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
2050 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
2052 gimple_stmt_iterator gsi;
2054 /* Since this block is no longer reachable, we can just delete all
2055 of its PHI nodes. */
2056 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
2057 remove_phi_node (&gsi, true);
2059 set_phi_nodes (bb, NULL);
2061 /* Remove edges to BB's successors. */
2062 while (EDGE_COUNT (bb->succs) > 0)
2063 remove_edge (EDGE_SUCC (bb, 0));
2067 /* Remove statements of basic block BB. */
2070 remove_bb (basic_block bb)
2072 gimple_stmt_iterator i;
2073 source_location loc = UNKNOWN_LOCATION;
2077 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2078 if (dump_flags & TDF_DETAILS)
2080 dump_bb (bb, dump_file, 0);
2081 fprintf (dump_file, "\n");
2087 struct loop *loop = bb->loop_father;
2089 /* If a loop gets removed, clean up the information associated
2091 if (loop->latch == bb
2092 || loop->header == bb)
2093 free_numbers_of_iterations_estimates_loop (loop);
2096 /* Remove all the instructions in the block. */
2097 if (bb_seq (bb) != NULL)
2099 for (i = gsi_start_bb (bb); !gsi_end_p (i);)
2101 gimple stmt = gsi_stmt (i);
2102 if (gimple_code (stmt) == GIMPLE_LABEL
2103 && (FORCED_LABEL (gimple_label_label (stmt))
2104 || DECL_NONLOCAL (gimple_label_label (stmt))))
2107 gimple_stmt_iterator new_gsi;
2109 /* A non-reachable non-local label may still be referenced.
2110 But it no longer needs to carry the extra semantics of
2112 if (DECL_NONLOCAL (gimple_label_label (stmt)))
2114 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
2115 FORCED_LABEL (gimple_label_label (stmt)) = 1;
2118 new_bb = bb->prev_bb;
2119 new_gsi = gsi_start_bb (new_bb);
2120 gsi_remove (&i, false);
2121 gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
2125 /* Release SSA definitions if we are in SSA. Note that we
2126 may be called when not in SSA. For example,
2127 final_cleanup calls this function via
2128 cleanup_tree_cfg. */
2129 if (gimple_in_ssa_p (cfun))
2130 release_defs (stmt);
2132 gsi_remove (&i, true);
2135 /* Don't warn for removed gotos. Gotos are often removed due to
2136 jump threading, thus resulting in bogus warnings. Not great,
2137 since this way we lose warnings for gotos in the original
2138 program that are indeed unreachable. */
2139 if (gimple_code (stmt) != GIMPLE_GOTO
2140 && gimple_has_location (stmt)
2142 loc = gimple_location (stmt);
2146 /* If requested, give a warning that the first statement in the
2147 block is unreachable. We walk statements backwards in the
2148 loop above, so the last statement we process is the first statement
2150 if (loc > BUILTINS_LOCATION && LOCATION_LINE (loc) > 0)
2151 warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
2153 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2154 bb->il.gimple = NULL;
2158 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2159 predicate VAL, return the edge that will be taken out of the block.
2160 If VAL does not match a unique edge, NULL is returned. */
2163 find_taken_edge (basic_block bb, tree val)
2167 stmt = last_stmt (bb);
2170 gcc_assert (is_ctrl_stmt (stmt));
2175 if (!is_gimple_min_invariant (val))
2178 if (gimple_code (stmt) == GIMPLE_COND)
2179 return find_taken_edge_cond_expr (bb, val);
2181 if (gimple_code (stmt) == GIMPLE_SWITCH)
2182 return find_taken_edge_switch_expr (bb, val);
2184 if (computed_goto_p (stmt))
2186 /* Only optimize if the argument is a label, if the argument is
2187 not a label then we can not construct a proper CFG.
2189 It may be the case that we only need to allow the LABEL_REF to
2190 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
2191 appear inside a LABEL_EXPR just to be safe. */
2192 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
2193 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
2194 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
2201 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2202 statement, determine which of the outgoing edges will be taken out of the
2203 block. Return NULL if either edge may be taken. */
2206 find_taken_edge_computed_goto (basic_block bb, tree val)
2211 dest = label_to_block (val);
2214 e = find_edge (bb, dest);
2215 gcc_assert (e != NULL);
2221 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2222 statement, determine which of the two edges will be taken out of the
2223 block. Return NULL if either edge may be taken. */
2226 find_taken_edge_cond_expr (basic_block bb, tree val)
2228 edge true_edge, false_edge;
2230 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2232 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2233 return (integer_zerop (val) ? false_edge : true_edge);
2236 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2237 statement, determine which edge will be taken out of the block. Return
2238 NULL if any edge may be taken. */
2241 find_taken_edge_switch_expr (basic_block bb, tree val)
2243 basic_block dest_bb;
2248 switch_stmt = last_stmt (bb);
2249 taken_case = find_case_label_for_value (switch_stmt, val);
2250 dest_bb = label_to_block (CASE_LABEL (taken_case));
2252 e = find_edge (bb, dest_bb);
2258 /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
2259 We can make optimal use here of the fact that the case labels are
2260 sorted: We can do a binary search for a case matching VAL. */
2263 find_case_label_for_value (gimple switch_stmt, tree val)
2265 size_t low, high, n = gimple_switch_num_labels (switch_stmt);
2266 tree default_case = gimple_switch_default_label (switch_stmt);
2268 for (low = 0, high = n; high - low > 1; )
2270 size_t i = (high + low) / 2;
2271 tree t = gimple_switch_label (switch_stmt, i);
2274 /* Cache the result of comparing CASE_LOW and val. */
2275 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2282 if (CASE_HIGH (t) == NULL)
2284 /* A singe-valued case label. */
2290 /* A case range. We can only handle integer ranges. */
2291 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2296 return default_case;
2300 /* Dump a basic block on stderr. */
2303 gimple_debug_bb (basic_block bb)
2305 gimple_dump_bb (bb, stderr, 0, TDF_VOPS|TDF_MEMSYMS);
2309 /* Dump basic block with index N on stderr. */
2312 gimple_debug_bb_n (int n)
2314 gimple_debug_bb (BASIC_BLOCK (n));
2315 return BASIC_BLOCK (n);
2319 /* Dump the CFG on stderr.
2321 FLAGS are the same used by the tree dumping functions
2322 (see TDF_* in tree-pass.h). */
2325 gimple_debug_cfg (int flags)
2327 gimple_dump_cfg (stderr, flags);
2331 /* Dump the program showing basic block boundaries on the given FILE.
2333 FLAGS are the same used by the tree dumping functions (see TDF_* in
2337 gimple_dump_cfg (FILE *file, int flags)
2339 if (flags & TDF_DETAILS)
2341 const char *funcname
2342 = lang_hooks.decl_printable_name (current_function_decl, 2);
2345 fprintf (file, ";; Function %s\n\n", funcname);
2346 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2347 n_basic_blocks, n_edges, last_basic_block);
2349 brief_dump_cfg (file);
2350 fprintf (file, "\n");
2353 if (flags & TDF_STATS)
2354 dump_cfg_stats (file);
2356 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2360 /* Dump CFG statistics on FILE. */
2363 dump_cfg_stats (FILE *file)
2365 static long max_num_merged_labels = 0;
2366 unsigned long size, total = 0;
2369 const char * const fmt_str = "%-30s%-13s%12s\n";
2370 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2371 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2372 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2373 const char *funcname
2374 = lang_hooks.decl_printable_name (current_function_decl, 2);
2377 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2379 fprintf (file, "---------------------------------------------------------\n");
2380 fprintf (file, fmt_str, "", " Number of ", "Memory");
2381 fprintf (file, fmt_str, "", " instances ", "used ");
2382 fprintf (file, "---------------------------------------------------------\n");
2384 size = n_basic_blocks * sizeof (struct basic_block_def);
2386 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2387 SCALE (size), LABEL (size));
2391 num_edges += EDGE_COUNT (bb->succs);
2392 size = num_edges * sizeof (struct edge_def);
2394 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2396 fprintf (file, "---------------------------------------------------------\n");
2397 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2399 fprintf (file, "---------------------------------------------------------\n");
2400 fprintf (file, "\n");
2402 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2403 max_num_merged_labels = cfg_stats.num_merged_labels;
2405 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2406 cfg_stats.num_merged_labels, max_num_merged_labels);
2408 fprintf (file, "\n");
2412 /* Dump CFG statistics on stderr. Keep extern so that it's always
2413 linked in the final executable. */
2416 debug_cfg_stats (void)
2418 dump_cfg_stats (stderr);
2422 /* Dump the flowgraph to a .vcg FILE. */
2425 gimple_cfg2vcg (FILE *file)
2430 const char *funcname
2431 = lang_hooks.decl_printable_name (current_function_decl, 2);
2433 /* Write the file header. */
2434 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2435 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2436 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2438 /* Write blocks and edges. */
2439 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2441 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2444 if (e->flags & EDGE_FAKE)
2445 fprintf (file, " linestyle: dotted priority: 10");
2447 fprintf (file, " linestyle: solid priority: 100");
2449 fprintf (file, " }\n");
2455 enum gimple_code head_code, end_code;
2456 const char *head_name, *end_name;
2459 gimple first = first_stmt (bb);
2460 gimple last = last_stmt (bb);
2464 head_code = gimple_code (first);
2465 head_name = gimple_code_name[head_code];
2466 head_line = get_lineno (first);
2469 head_name = "no-statement";
2473 end_code = gimple_code (last);
2474 end_name = gimple_code_name[end_code];
2475 end_line = get_lineno (last);
2478 end_name = "no-statement";
2480 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2481 bb->index, bb->index, head_name, head_line, end_name,
2484 FOR_EACH_EDGE (e, ei, bb->succs)
2486 if (e->dest == EXIT_BLOCK_PTR)
2487 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2489 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2491 if (e->flags & EDGE_FAKE)
2492 fprintf (file, " priority: 10 linestyle: dotted");
2494 fprintf (file, " priority: 100 linestyle: solid");
2496 fprintf (file, " }\n");
2499 if (bb->next_bb != EXIT_BLOCK_PTR)
2503 fputs ("}\n\n", file);
2508 /*---------------------------------------------------------------------------
2509 Miscellaneous helpers
2510 ---------------------------------------------------------------------------*/
2512 /* Return true if T represents a stmt that always transfers control. */
2515 is_ctrl_stmt (gimple t)
2517 return gimple_code (t) == GIMPLE_COND
2518 || gimple_code (t) == GIMPLE_SWITCH
2519 || gimple_code (t) == GIMPLE_GOTO
2520 || gimple_code (t) == GIMPLE_RETURN
2521 || gimple_code (t) == GIMPLE_RESX;
2525 /* Return true if T is a statement that may alter the flow of control
2526 (e.g., a call to a non-returning function). */
2529 is_ctrl_altering_stmt (gimple t)
2533 if (is_gimple_call (t))
2535 int flags = gimple_call_flags (t);
2537 /* A non-pure/const call alters flow control if the current
2538 function has nonlocal labels. */
2539 if (!(flags & (ECF_CONST | ECF_PURE))
2540 && cfun->has_nonlocal_label)
2543 /* A call also alters control flow if it does not return. */
2544 if (gimple_call_flags (t) & ECF_NORETURN)
2548 /* OpenMP directives alter control flow. */
2549 if (is_gimple_omp (t))
2552 /* If a statement can throw, it alters control flow. */
2553 return stmt_can_throw_internal (t);
2557 /* Return true if T is a simple local goto. */
2560 simple_goto_p (gimple t)
2562 return (gimple_code (t) == GIMPLE_GOTO
2563 && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2567 /* Return true if T can make an abnormal transfer of control flow.
2568 Transfers of control flow associated with EH are excluded. */
2571 stmt_can_make_abnormal_goto (gimple t)
2573 if (computed_goto_p (t))
2575 if (is_gimple_call (t))
2576 return gimple_has_side_effects (t) && cfun->has_nonlocal_label;
2581 /* Return true if STMT should start a new basic block. PREV_STMT is
2582 the statement preceding STMT. It is used when STMT is a label or a
2583 case label. Labels should only start a new basic block if their
2584 previous statement wasn't a label. Otherwise, sequence of labels
2585 would generate unnecessary basic blocks that only contain a single
2589 stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
2594 /* Labels start a new basic block only if the preceding statement
2595 wasn't a label of the same type. This prevents the creation of
2596 consecutive blocks that have nothing but a single label. */
2597 if (gimple_code (stmt) == GIMPLE_LABEL)
2599 /* Nonlocal and computed GOTO targets always start a new block. */
2600 if (DECL_NONLOCAL (gimple_label_label (stmt))
2601 || FORCED_LABEL (gimple_label_label (stmt)))
2604 if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
2606 if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
2609 cfg_stats.num_merged_labels++;
2620 /* Return true if T should end a basic block. */
2623 stmt_ends_bb_p (gimple t)
2625 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2628 /* Remove block annotations and other data structures. */
2631 delete_tree_cfg_annotations (void)
2633 label_to_block_map = NULL;
2637 /* Return the first statement in basic block BB. */
2640 first_stmt (basic_block bb)
2642 gimple_stmt_iterator i = gsi_start_bb (bb);
2643 return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
2646 /* Return the last statement in basic block BB. */
2649 last_stmt (basic_block bb)
2651 gimple_stmt_iterator b = gsi_last_bb (bb);
2652 return !gsi_end_p (b) ? gsi_stmt (b) : NULL;
2655 /* Return the last statement of an otherwise empty block. Return NULL
2656 if the block is totally empty, or if it contains more than one
2660 last_and_only_stmt (basic_block bb)
2662 gimple_stmt_iterator i = gsi_last_bb (bb);
2668 last = gsi_stmt (i);
2673 /* Empty statements should no longer appear in the instruction stream.
2674 Everything that might have appeared before should be deleted by
2675 remove_useless_stmts, and the optimizers should just gsi_remove
2676 instead of smashing with build_empty_stmt.
2678 Thus the only thing that should appear here in a block containing
2679 one executable statement is a label. */
2680 prev = gsi_stmt (i);
2681 if (gimple_code (prev) == GIMPLE_LABEL)
2687 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
2690 reinstall_phi_args (edge new_edge, edge old_edge)
2692 edge_var_map_vector v;
2695 gimple_stmt_iterator phis;
2697 v = redirect_edge_var_map_vector (old_edge);
2701 for (i = 0, phis = gsi_start_phis (new_edge->dest);
2702 VEC_iterate (edge_var_map, v, i, vm) && !gsi_end_p (phis);
2703 i++, gsi_next (&phis))
2705 gimple phi = gsi_stmt (phis);
2706 tree result = redirect_edge_var_map_result (vm);
2707 tree arg = redirect_edge_var_map_def (vm);
2709 gcc_assert (result == gimple_phi_result (phi));
2711 add_phi_arg (phi, arg, new_edge);
2714 redirect_edge_var_map_clear (old_edge);
2717 /* Returns the basic block after which the new basic block created
2718 by splitting edge EDGE_IN should be placed. Tries to keep the new block
2719 near its "logical" location. This is of most help to humans looking
2720 at debugging dumps. */
2723 split_edge_bb_loc (edge edge_in)
2725 basic_block dest = edge_in->dest;
2727 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
2728 return edge_in->src;
2730 return dest->prev_bb;
2733 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2734 Abort on abnormal edges. */
2737 gimple_split_edge (edge edge_in)
2739 basic_block new_bb, after_bb, dest;
2742 /* Abnormal edges cannot be split. */
2743 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2745 dest = edge_in->dest;
2747 after_bb = split_edge_bb_loc (edge_in);
2749 new_bb = create_empty_bb (after_bb);
2750 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2751 new_bb->count = edge_in->count;
2752 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2753 new_edge->probability = REG_BR_PROB_BASE;
2754 new_edge->count = edge_in->count;
2756 e = redirect_edge_and_branch (edge_in, new_bb);
2757 gcc_assert (e == edge_in);
2758 reinstall_phi_args (new_edge, e);
2763 /* Callback for walk_tree, check that all elements with address taken are
2764 properly noticed as such. The DATA is an int* that is 1 if TP was seen
2765 inside a PHI node. */
2768 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2775 /* Check operand N for being valid GIMPLE and give error MSG if not. */
2776 #define CHECK_OP(N, MSG) \
2777 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
2778 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
2780 switch (TREE_CODE (t))
2783 if (SSA_NAME_IN_FREE_LIST (t))
2785 error ("SSA name in freelist but still referenced");
2791 x = fold (ASSERT_EXPR_COND (t));
2792 if (x == boolean_false_node)
2794 error ("ASSERT_EXPR with an always-false condition");
2800 x = TREE_OPERAND (t, 0);
2801 if (TREE_CODE (x) == BIT_FIELD_REF
2802 && is_gimple_reg (TREE_OPERAND (x, 0)))
2804 error ("GIMPLE register modified with BIT_FIELD_REF");
2812 bool old_side_effects;
2814 bool new_side_effects;
2816 gcc_assert (is_gimple_address (t));
2818 old_constant = TREE_CONSTANT (t);
2819 old_side_effects = TREE_SIDE_EFFECTS (t);
2821 recompute_tree_invariant_for_addr_expr (t);
2822 new_side_effects = TREE_SIDE_EFFECTS (t);
2823 new_constant = TREE_CONSTANT (t);
2825 if (old_constant != new_constant)
2827 error ("constant not recomputed when ADDR_EXPR changed");
2830 if (old_side_effects != new_side_effects)
2832 error ("side effects not recomputed when ADDR_EXPR changed");
2836 /* Skip any references (they will be checked when we recurse down the
2837 tree) and ensure that any variable used as a prefix is marked
2839 for (x = TREE_OPERAND (t, 0);
2840 handled_component_p (x);
2841 x = TREE_OPERAND (x, 0))
2844 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
2846 if (!TREE_ADDRESSABLE (x))
2848 error ("address taken, but ADDRESSABLE bit not set");
2856 x = COND_EXPR_COND (t);
2857 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
2859 error ("non-integral used in condition");
2862 if (!is_gimple_condexpr (x))
2864 error ("invalid conditional operand");
2869 case NON_LVALUE_EXPR:
2873 case FIX_TRUNC_EXPR:
2878 case TRUTH_NOT_EXPR:
2879 CHECK_OP (0, "invalid operand to unary operator");
2886 case ARRAY_RANGE_REF:
2888 case VIEW_CONVERT_EXPR:
2889 /* We have a nest of references. Verify that each of the operands
2890 that determine where to reference is either a constant or a variable,
2891 verify that the base is valid, and then show we've already checked
2893 while (handled_component_p (t))
2895 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
2896 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
2897 else if (TREE_CODE (t) == ARRAY_REF
2898 || TREE_CODE (t) == ARRAY_RANGE_REF)
2900 CHECK_OP (1, "invalid array index");
2901 if (TREE_OPERAND (t, 2))
2902 CHECK_OP (2, "invalid array lower bound");
2903 if (TREE_OPERAND (t, 3))
2904 CHECK_OP (3, "invalid array stride");
2906 else if (TREE_CODE (t) == BIT_FIELD_REF)
2908 if (!host_integerp (TREE_OPERAND (t, 1), 1)
2909 || !host_integerp (TREE_OPERAND (t, 2), 1))
2911 error ("invalid position or size operand to BIT_FIELD_REF");
2914 else if (INTEGRAL_TYPE_P (TREE_TYPE (t))
2915 && (TYPE_PRECISION (TREE_TYPE (t))
2916 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
2918 error ("integral result type precision does not match "
2919 "field size of BIT_FIELD_REF");
2922 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
2923 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
2924 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
2926 error ("mode precision of non-integral result does not "
2927 "match field size of BIT_FIELD_REF");
2932 t = TREE_OPERAND (t, 0);
2935 if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
2937 error ("invalid reference prefix");
2944 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
2945 POINTER_PLUS_EXPR. */
2946 if (POINTER_TYPE_P (TREE_TYPE (t)))
2948 error ("invalid operand to plus/minus, type is a pointer");
2951 CHECK_OP (0, "invalid operand to binary operator");
2952 CHECK_OP (1, "invalid operand to binary operator");
2955 case POINTER_PLUS_EXPR:
2956 /* Check to make sure the first operand is a pointer or reference type. */
2957 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2959 error ("invalid operand to pointer plus, first operand is not a pointer");
2962 /* Check to make sure the second operand is an integer with type of
2964 if (!useless_type_conversion_p (sizetype,
2965 TREE_TYPE (TREE_OPERAND (t, 1))))
2967 error ("invalid operand to pointer plus, second operand is not an "
2968 "integer with type of sizetype.");
2978 case UNORDERED_EXPR:
2987 case TRUNC_DIV_EXPR:
2989 case FLOOR_DIV_EXPR:
2990 case ROUND_DIV_EXPR:
2991 case TRUNC_MOD_EXPR:
2993 case FLOOR_MOD_EXPR:
2994 case ROUND_MOD_EXPR:
2996 case EXACT_DIV_EXPR:
3006 CHECK_OP (0, "invalid operand to binary operator");
3007 CHECK_OP (1, "invalid operand to binary operator");
3011 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3024 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
3025 Returns true if there is an error, otherwise false. */
3028 verify_types_in_gimple_min_lval (tree expr)
3032 if (is_gimple_id (expr))
3035 if (TREE_CODE (expr) != INDIRECT_REF
3036 && TREE_CODE (expr) != ALIGN_INDIRECT_REF
3037 && TREE_CODE (expr) != MISALIGNED_INDIRECT_REF)
3039 error ("invalid expression for min lvalue");
3043 op = TREE_OPERAND (expr, 0);
3044 if (!is_gimple_val (op))
3046 error ("invalid operand in indirect reference");
3047 debug_generic_stmt (op);
3050 if (!useless_type_conversion_p (TREE_TYPE (expr),
3051 TREE_TYPE (TREE_TYPE (op))))
3053 error ("type mismatch in indirect reference");
3054 debug_generic_stmt (TREE_TYPE (expr));
3055 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3062 /* Verify if EXPR is a valid GIMPLE reference expression. Returns true
3063 if there is an error, otherwise false. */
3066 verify_types_in_gimple_reference (tree expr)
3068 while (handled_component_p (expr))
3070 tree op = TREE_OPERAND (expr, 0);
3072 if (TREE_CODE (expr) == ARRAY_REF
3073 || TREE_CODE (expr) == ARRAY_RANGE_REF)
3075 if (!is_gimple_val (TREE_OPERAND (expr, 1))
3076 || (TREE_OPERAND (expr, 2)
3077 && !is_gimple_val (TREE_OPERAND (expr, 2)))
3078 || (TREE_OPERAND (expr, 3)
3079 && !is_gimple_val (TREE_OPERAND (expr, 3))))
3081 error ("invalid operands to array reference");
3082 debug_generic_stmt (expr);
3087 /* Verify if the reference array element types are compatible. */
3088 if (TREE_CODE (expr) == ARRAY_REF
3089 && !useless_type_conversion_p (TREE_TYPE (expr),
3090 TREE_TYPE (TREE_TYPE (op))))
3092 error ("type mismatch in array reference");
3093 debug_generic_stmt (TREE_TYPE (expr));
3094 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3097 if (TREE_CODE (expr) == ARRAY_RANGE_REF
3098 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
3099 TREE_TYPE (TREE_TYPE (op))))
3101 error ("type mismatch in array range reference");
3102 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
3103 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3107 if ((TREE_CODE (expr) == REALPART_EXPR
3108 || TREE_CODE (expr) == IMAGPART_EXPR)
3109 && !useless_type_conversion_p (TREE_TYPE (expr),
3110 TREE_TYPE (TREE_TYPE (op))))
3112 error ("type mismatch in real/imagpart reference");
3113 debug_generic_stmt (TREE_TYPE (expr));
3114 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3118 if (TREE_CODE (expr) == COMPONENT_REF
3119 && !useless_type_conversion_p (TREE_TYPE (expr),
3120 TREE_TYPE (TREE_OPERAND (expr, 1))))
3122 error ("type mismatch in component reference");
3123 debug_generic_stmt (TREE_TYPE (expr));
3124 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3128 /* For VIEW_CONVERT_EXPRs which are allowed here, too, there
3129 is nothing to verify. Gross mismatches at most invoke
3130 undefined behavior. */
3135 return verify_types_in_gimple_min_lval (expr);
3138 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3139 list of pointer-to types that is trivially convertible to DEST. */
3142 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3146 if (!TYPE_POINTER_TO (src_obj))
3149 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3150 if (useless_type_conversion_p (dest, src))
3156 /* Return true if TYPE1 is a fixed-point type and if conversions to and
3157 from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
3160 valid_fixed_convert_types_p (tree type1, tree type2)
3162 return (FIXED_POINT_TYPE_P (type1)
3163 && (INTEGRAL_TYPE_P (type2)
3164 || SCALAR_FLOAT_TYPE_P (type2)
3165 || FIXED_POINT_TYPE_P (type2)));
3168 /* Verify that OP is a valid GIMPLE operand. Return true if there is
3169 an error, false otherwise. */
3172 verify_types_in_gimple_op (tree op)
3174 if (!is_gimple_val (op) && !is_gimple_lvalue (op))
3176 error ("Invalid GIMPLE operand");
3177 debug_generic_expr (op);
3185 /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
3186 is a problem, otherwise false. */
3189 verify_types_in_gimple_call (gimple stmt)
3191 bool failed = false;
3195 if (gimple_call_lhs (stmt))
3196 failed |= verify_types_in_gimple_op (gimple_call_lhs (stmt));
3198 fn = gimple_call_fn (stmt);
3199 if (TREE_CODE (fn) != OBJ_TYPE_REF
3200 && verify_types_in_gimple_op (fn))
3203 if (gimple_call_chain (stmt))
3204 failed |= verify_types_in_gimple_op (gimple_call_chain (stmt));
3206 for (i = 0; i < gimple_call_num_args (stmt); i++)
3207 failed |= verify_types_in_gimple_op (gimple_call_arg (stmt,i));
3213 /* Verify the contents of a GIMPLE_COND STMT. Returns true when there
3214 is a problem, otherwise false. */
3217 verify_types_in_gimple_cond (gimple stmt)
3219 bool failed = false;
3221 failed |= verify_types_in_gimple_op (gimple_cond_lhs (stmt));
3222 failed |= verify_types_in_gimple_op (gimple_cond_rhs (stmt));
3223 failed |= verify_types_in_gimple_op (gimple_cond_true_label (stmt));
3224 failed |= verify_types_in_gimple_op (gimple_cond_false_label (stmt));
3230 /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
3231 is a problem, otherwise false.
3233 Verify that the types of the LHS and the RHS operands are
3234 compatible. This verification largely depends on what kind of
3235 operation is done on the RHS of the assignment. It is not always
3236 the case that all the types of the operands must match (e.g., 'a =
3237 (unsigned long) b' or 'ptr = ptr + 1'). */
3240 verify_types_in_gimple_assign (gimple stmt)
3242 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3243 tree lhs = gimple_assign_lhs (stmt);
3244 tree rhs1 = gimple_assign_rhs1 (stmt);
3245 tree rhs2 = (gimple_num_ops (stmt) == 3) ? gimple_assign_rhs2 (stmt) : NULL;
3246 tree lhs_type = TREE_TYPE (lhs);
3247 tree rhs1_type = TREE_TYPE (rhs1);
3248 tree rhs2_type = (rhs2) ? TREE_TYPE (rhs2) : NULL;
3250 /* Special codes we cannot handle via their class. */
3255 if (!is_gimple_val (rhs1))
3257 error ("invalid operand in conversion");
3261 /* Allow conversions between integral types and pointers only if
3262 there is no sign or zero extension involved. */
3263 if (((POINTER_TYPE_P (lhs_type) && INTEGRAL_TYPE_P (rhs1_type))
3264 || (POINTER_TYPE_P (rhs1_type) && INTEGRAL_TYPE_P (lhs_type)))
3265 && (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (rhs1_type)
3266 /* For targets were the precision of sizetype doesn't
3267 match that of pointers we need the following. */
3268 || lhs_type == sizetype || rhs1_type == sizetype))
3271 /* Allow conversion from integer to offset type and vice versa. */
3272 if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3273 && TREE_CODE (rhs1_type) == INTEGER_TYPE)
3274 || (TREE_CODE (lhs_type) == INTEGER_TYPE
3275 && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3278 /* Otherwise assert we are converting between types of the
3280 if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3282 error ("invalid types in nop conversion");
3283 debug_generic_expr (lhs_type);
3284 debug_generic_expr (rhs1_type);
3291 case FIXED_CONVERT_EXPR:
3293 if (!is_gimple_val (rhs1))
3295 error ("invalid operand in conversion");
3299 if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3300 && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3302 error ("invalid types in fixed-point conversion");
3303 debug_generic_expr (lhs_type);
3304 debug_generic_expr (rhs1_type);
3313 if (!is_gimple_val (rhs1))
3315 error ("invalid operand in int to float conversion");
3319 if (!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3321 error ("invalid types in conversion to floating point");
3322 debug_generic_expr (lhs_type);
3323 debug_generic_expr (rhs1_type);
3330 case FIX_TRUNC_EXPR:
3332 if (!is_gimple_val (rhs1))
3334 error ("invalid operand in float to int conversion");
3338 if (!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3340 error ("invalid types in conversion to integer");
3341 debug_generic_expr (lhs_type);
3342 debug_generic_expr (rhs1_type);
3351 if (!is_gimple_val (rhs1) || !is_gimple_val (rhs2))
3353 error ("invalid operands in complex expression");
3357 if (!TREE_CODE (lhs_type) == COMPLEX_TYPE
3358 || !(TREE_CODE (rhs1_type) == INTEGER_TYPE
3359 || SCALAR_FLOAT_TYPE_P (rhs1_type))
3360 || !(TREE_CODE (rhs2_type) == INTEGER_TYPE
3361 || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3363 error ("type mismatch in complex expression");
3364 debug_generic_expr (lhs_type);
3365 debug_generic_expr (rhs1_type);
3366 debug_generic_expr (rhs2_type);
3375 /* In this context we know that we are on the RHS of an
3376 assignment, so CONSTRUCTOR operands are OK. */
3377 /* FIXME: verify constructor arguments. */
3386 if (!is_gimple_val (rhs1) || !is_gimple_val (rhs2))
3388 error ("invalid operands in shift expression");
3392 if (!TREE_CODE (rhs1_type) == INTEGER_TYPE
3393 || !useless_type_conversion_p (lhs_type, rhs1_type))
3395 error ("type mismatch in shift expression");
3396 debug_generic_expr (lhs_type);
3397 debug_generic_expr (rhs1_type);
3398 debug_generic_expr (rhs2_type);
3408 if (POINTER_TYPE_P (lhs_type)
3409 || POINTER_TYPE_P (rhs1_type)
3410 || POINTER_TYPE_P (rhs2_type))
3412 error ("invalid (pointer) operands to plus/minus");
3416 /* Continue with generic binary expression handling. */
3420 case POINTER_PLUS_EXPR:
3422 if (!is_gimple_val (rhs1) || !is_gimple_val (rhs2))
3424 error ("invalid operands in pointer plus expression");
3427 if (!POINTER_TYPE_P (rhs1_type)
3428 || !useless_type_conversion_p (lhs_type, rhs1_type)
3429 || !useless_type_conversion_p (sizetype, rhs2_type))
3431 error ("type mismatch in pointer plus expression");
3432 debug_generic_stmt (lhs_type);
3433 debug_generic_stmt (rhs1_type);
3434 debug_generic_stmt (rhs2_type);
3443 tree op = TREE_OPERAND (rhs1, 0);
3444 if (!is_gimple_addressable (op))
3446 error ("invalid operand in unary expression");
3450 if (!one_pointer_to_useless_type_conversion_p (lhs_type, TREE_TYPE (op))
3451 /* FIXME: a longstanding wart, &a == &a[0]. */
3452 && (TREE_CODE (TREE_TYPE (op)) != ARRAY_TYPE
3453 || !one_pointer_to_useless_type_conversion_p (lhs_type,
3454 TREE_TYPE (TREE_TYPE (op)))))
3456 error ("type mismatch in address expression");
3457 debug_generic_stmt (lhs_type);
3458 debug_generic_stmt (TYPE_POINTER_TO (TREE_TYPE (op)));
3462 return verify_types_in_gimple_reference (TREE_OPERAND (rhs1, 0));
3465 case TRUTH_ANDIF_EXPR:
3466 case TRUTH_ORIF_EXPR:
3469 case TRUTH_AND_EXPR:
3471 case TRUTH_XOR_EXPR:
3473 if (!is_gimple_val (rhs1) || !is_gimple_val (rhs2))
3475 error ("invalid operands in truth expression");
3479 /* We allow any kind of integral typed argument and result. */
3480 if (!INTEGRAL_TYPE_P (rhs1_type)
3481 || !INTEGRAL_TYPE_P (rhs2_type)
3482 || !INTEGRAL_TYPE_P (lhs_type))
3484 error ("type mismatch in binary truth expression");
3485 debug_generic_expr (lhs_type);
3486 debug_generic_expr (rhs1_type);
3487 debug_generic_expr (rhs2_type);
3494 case TRUTH_NOT_EXPR:
3496 if (!is_gimple_val (rhs1))
3498 error ("invalid operand in unary not");
3502 /* For TRUTH_NOT_EXPR we can have any kind of integral
3503 typed arguments and results. */
3504 if (!INTEGRAL_TYPE_P (rhs1_type)
3505 || !INTEGRAL_TYPE_P (lhs_type))
3507 error ("type mismatch in not expression");
3508 debug_generic_expr (lhs_type);
3509 debug_generic_expr (rhs1_type);
3516 /* After gimplification we should not have any of these. */
3528 case TRY_FINALLY_EXPR:
3529 case TRY_CATCH_EXPR:
3530 case EH_FILTER_EXPR:
3531 case STATEMENT_LIST:
3533 error ("tree node that should already be gimple.");
3544 /* Generic handling via classes. */
3545 switch (TREE_CODE_CLASS (rhs_code))
3547 case tcc_exceptional: /* for SSA_NAME */
3549 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3551 error ("non-trivial conversion at assignment");
3552 debug_generic_expr (lhs);
3553 debug_generic_expr (rhs1);
3559 /* All tcc_reference trees are GIMPLE_SINGLE_RHS. Verify that
3560 no implicit type change happens here. */
3561 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3563 error ("non-trivial conversion at assignment");
3564 debug_generic_expr (lhs);
3565 debug_generic_expr (rhs1);
3568 return verify_types_in_gimple_reference (rhs1);
3570 case tcc_comparison:
3572 if (!is_gimple_val (rhs1) || !is_gimple_val (rhs2))
3574 error ("invalid operands in comparison expression");
3578 /* For comparisons we do not have the operations type as the
3579 effective type the comparison is carried out in. Instead
3580 we require that either the first operand is trivially
3581 convertible into the second, or the other way around.
3582 The resulting type of a comparison may be any integral type.
3583 Because we special-case pointers to void we allow
3584 comparisons of pointers with the same mode as well. */
3585 if ((!useless_type_conversion_p (rhs1_type, rhs2_type)
3586 && !useless_type_conversion_p (rhs2_type, rhs1_type)
3587 && (!POINTER_TYPE_P (rhs1_type)
3588 || !POINTER_TYPE_P (rhs2_type)
3589 || TYPE_MODE (rhs1_type) != TYPE_MODE (rhs2_type)))
3590 || !INTEGRAL_TYPE_P (lhs_type))
3592 error ("type mismatch in comparison expression");
3593 debug_generic_expr (lhs_type);
3594 debug_generic_expr (rhs1_type);
3595 debug_generic_expr (rhs2_type);
3608 /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
3609 is a problem, otherwise false. */
3612 verify_types_in_gimple_return (gimple stmt)
3614 tree op = gimple_return_retval (stmt);
3619 return verify_types_in_gimple_op (op);
3623 /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
3624 is a problem, otherwise false. */
3627 verify_types_in_gimple_switch (gimple stmt)
3629 if (!is_gimple_val (gimple_switch_index (stmt)))
3631 error ("invalid operand to switch statement");
3632 debug_generic_expr (gimple_switch_index (stmt));
3640 /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
3641 and false otherwise. */
3644 verify_types_in_gimple_phi (gimple stmt)
3648 if (verify_types_in_gimple_op (gimple_phi_result (stmt)))
3651 for (i = 0; i < gimple_phi_num_args (stmt); i++)
3652 if (verify_types_in_gimple_op (gimple_phi_arg_def (stmt, i)))
3659 /* Verify the GIMPLE statement STMT. Returns true if there is an
3660 error, otherwise false. */
3663 verify_types_in_gimple_stmt (gimple stmt)
3665 if (is_gimple_omp (stmt))
3667 /* OpenMP directives are validated by the FE and never operated
3668 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
3669 non-gimple expressions when the main index variable has had
3670 its address taken. This does not affect the loop itself
3671 because the header of an GIMPLE_OMP_FOR is merely used to determine
3672 how to setup the parallel iteration. */
3676 switch (gimple_code (stmt))
3679 return verify_types_in_gimple_assign (stmt);
3682 return TREE_CODE (gimple_label_label (stmt)) != LABEL_DECL;
3685 return verify_types_in_gimple_call (stmt);
3688 return verify_types_in_gimple_cond (stmt);
3691 return verify_types_in_gimple_op (gimple_goto_dest (stmt));
3694 case GIMPLE_PREDICT:
3698 return verify_types_in_gimple_switch (stmt);
3701 return verify_types_in_gimple_return (stmt);
3706 case GIMPLE_CHANGE_DYNAMIC_TYPE:
3707 return verify_types_in_gimple_op (gimple_cdt_location (stmt));
3710 return verify_types_in_gimple_phi (stmt);
3717 /* Verify the GIMPLE statements inside the sequence STMTS. */
3720 verify_types_in_gimple_seq_2 (gimple_seq stmts)
3722 gimple_stmt_iterator ittr;
3725 for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
3727 gimple stmt = gsi_stmt (ittr);
3729 switch (gimple_code (stmt))
3732 err |= verify_types_in_gimple_seq_2 (gimple_bind_body (stmt));
3736 err |= verify_types_in_gimple_seq_2 (gimple_try_eval (stmt));
3737 err |= verify_types_in_gimple_seq_2 (gimple_try_cleanup (stmt));
3740 case GIMPLE_EH_FILTER:
3741 err |= verify_types_in_gimple_seq_2
3742 (gimple_eh_filter_failure (stmt));
3746 err |= verify_types_in_gimple_seq_2 (gimple_catch_handler (stmt));
3749 case GIMPLE_OMP_CRITICAL:
3750 case GIMPLE_OMP_CONTINUE:
3751 case GIMPLE_OMP_MASTER:
3752 case GIMPLE_OMP_ORDERED:
3753 case GIMPLE_OMP_SECTION:
3754 case GIMPLE_OMP_FOR:
3755 case GIMPLE_OMP_PARALLEL:
3756 case GIMPLE_OMP_TASK:
3757 case GIMPLE_OMP_SECTIONS:
3758 case GIMPLE_OMP_SINGLE:
3759 case GIMPLE_OMP_ATOMIC_STORE:
3760 case GIMPLE_OMP_ATOMIC_LOAD:
3763 /* Tuples that do not have trees. */
3766 case GIMPLE_OMP_RETURN:
3767 case GIMPLE_PREDICT:
3772 bool err2 = verify_types_in_gimple_stmt (stmt);
3774 debug_gimple_stmt (stmt);
3784 /* Verify the GIMPLE statements inside the statement list STMTS. */
3787 verify_types_in_gimple_seq (gimple_seq stmts)
3789 if (verify_types_in_gimple_seq_2 (stmts))
3790 internal_error ("verify_gimple failed");
3794 /* Verify STMT, return true if STMT is not in GIMPLE form.
3795 TODO: Implement type checking. */
3798 verify_stmt (gimple_stmt_iterator *gsi)
3801 struct walk_stmt_info wi;
3802 bool last_in_block = gsi_one_before_end_p (*gsi);
3803 gimple stmt = gsi_stmt (*gsi);
3805 if (is_gimple_omp (stmt))
3807 /* OpenMP directives are validated by the FE and never operated
3808 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
3809 non-gimple expressions when the main index variable has had
3810 its address taken. This does not affect the loop itself
3811 because the header of an GIMPLE_OMP_FOR is merely used to determine
3812 how to setup the parallel iteration. */
3816 /* FIXME. The C frontend passes unpromoted arguments in case it
3817 didn't see a function declaration before the call. */
3818 if (is_gimple_call (stmt))
3822 if (!is_gimple_call_addr (gimple_call_fn (stmt)))
3824 error ("invalid function in call statement");
3828 decl = gimple_call_fndecl (stmt);
3830 && TREE_CODE (decl) == FUNCTION_DECL
3831 && DECL_LOOPING_CONST_OR_PURE_P (decl)
3832 && (!DECL_PURE_P (decl))
3833 && (!TREE_READONLY (decl)))
3835 error ("invalid pure const state for function");
3840 memset (&wi, 0, sizeof (wi));
3841 addr = walk_gimple_op (gsi_stmt (*gsi), verify_expr, &wi);
3844 debug_generic_expr (addr);
3845 inform ("in statement");
3846 debug_gimple_stmt (stmt);
3850 /* If the statement is marked as part of an EH region, then it is
3851 expected that the statement could throw. Verify that when we
3852 have optimizations that simplify statements such that we prove
3853 that they cannot throw, that we update other data structures
3855 if (lookup_stmt_eh_region (stmt) >= 0)
3857 if (!stmt_could_throw_p (stmt))
3859 error ("statement marked for throw, but doesn%'t");
3862 if (!last_in_block && stmt_can_throw_internal (stmt))
3864 error ("statement marked for throw in middle of block");
3872 debug_gimple_stmt (stmt);
3877 /* Return true when the T can be shared. */
3880 tree_node_can_be_shared (tree t)
3882 if (IS_TYPE_OR_DECL_P (t)
3883 || is_gimple_min_invariant (t)
3884 || TREE_CODE (t) == SSA_NAME
3885 || t == error_mark_node
3886 || TREE_CODE (t) == IDENTIFIER_NODE)
3889 if (TREE_CODE (t) == CASE_LABEL_EXPR)
3892 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3893 && is_gimple_min_invariant (TREE_OPERAND (t, 1)))
3894 || TREE_CODE (t) == COMPONENT_REF
3895 || TREE_CODE (t) == REALPART_EXPR
3896 || TREE_CODE (t) == IMAGPART_EXPR)
3897 t = TREE_OPERAND (t, 0);
3906 /* Called via walk_gimple_stmt. Verify tree sharing. */
3909 verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
3911 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
3912 struct pointer_set_t *visited = (struct pointer_set_t *) wi->info;
3914 if (tree_node_can_be_shared (*tp))
3916 *walk_subtrees = false;
3920 if (pointer_set_insert (visited, *tp))
3927 static bool eh_error_found;
3929 verify_eh_throw_stmt_node (void **slot, void *data)
3931 struct throw_stmt_node *node = (struct throw_stmt_node *)*slot;
3932 struct pointer_set_t *visited = (struct pointer_set_t *) data;
3934 if (!pointer_set_contains (visited, node->stmt))
3936 error ("Dead STMT in EH table");
3937 debug_gimple_stmt (node->stmt);
3938 eh_error_found = true;
3944 /* Verify the GIMPLE statements in every basic block. */
3950 gimple_stmt_iterator gsi;
3952 struct pointer_set_t *visited, *visited_stmts;
3954 struct walk_stmt_info wi;
3956 timevar_push (TV_TREE_STMT_VERIFY);
3957 visited = pointer_set_create ();
3958 visited_stmts = pointer_set_create ();
3960 memset (&wi, 0, sizeof (wi));
3961 wi.info = (void *) visited;
3968 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3970 phi = gsi_stmt (gsi);
3971 pointer_set_insert (visited_stmts, phi);
3972 if (gimple_bb (phi) != bb)
3974 error ("gimple_bb (phi) is set to a wrong basic block");
3978 for (i = 0; i < gimple_phi_num_args (phi); i++)
3980 tree t = gimple_phi_arg_def (phi, i);
3985 error ("missing PHI def");
3986 debug_gimple_stmt (phi);
3990 /* Addressable variables do have SSA_NAMEs but they
3991 are not considered gimple values. */
3992 else if (TREE_CODE (t) != SSA_NAME
3993 && TREE_CODE (t) != FUNCTION_DECL
3994 && !is_gimple_min_invariant (t))
3996 error ("PHI argument is not a GIMPLE value");
3997 debug_gimple_stmt (phi);
3998 debug_generic_expr (t);
4002 addr = walk_tree (&t, verify_node_sharing, visited, NULL);
4005 error ("incorrect sharing of tree nodes");
4006 debug_gimple_stmt (phi);
4007 debug_generic_expr (addr);
4013 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
4015 gimple stmt = gsi_stmt (gsi);
4017 if (gimple_code (stmt) == GIMPLE_WITH_CLEANUP_EXPR
4018 || gimple_code (stmt) == GIMPLE_BIND)
4020 error ("invalid GIMPLE statement");
4021 debug_gimple_stmt (stmt);
4025 pointer_set_insert (visited_stmts, stmt);
4027 if (gimple_bb (stmt) != bb)
4029 error ("gimple_bb (stmt) is set to a wrong basic block");
4033 if (gimple_code (stmt) == GIMPLE_LABEL)
4035 tree decl = gimple_label_label (stmt);
4036 int uid = LABEL_DECL_UID (decl);
4039 || VEC_index (basic_block, label_to_block_map, uid) != bb)
4041 error ("incorrect entry in label_to_block_map.\n");
4046 err |= verify_stmt (&gsi);
4047 addr = walk_gimple_op (gsi_stmt (gsi), verify_node_sharing, &wi);
4050 error ("incorrect sharing of tree nodes");
4051 debug_gimple_stmt (stmt);
4052 debug_generic_expr (addr);
4059 eh_error_found = false;
4060 if (get_eh_throw_stmt_table (cfun))
4061 htab_traverse (get_eh_throw_stmt_table (cfun),
4062 verify_eh_throw_stmt_node,
4065 if (err | eh_error_found)
4066 internal_error ("verify_stmts failed");
4068 pointer_set_destroy (visited);
4069 pointer_set_destroy (visited_stmts);
4070 verify_histograms ();
4071 timevar_pop (TV_TREE_STMT_VERIFY);
4075 /* Verifies that the flow information is OK. */
4078 gimple_verify_flow_info (void)
4082 gimple_stmt_iterator gsi;
4087 if (ENTRY_BLOCK_PTR->il.gimple)
4089 error ("ENTRY_BLOCK has IL associated with it");
4093 if (EXIT_BLOCK_PTR->il.gimple)
4095 error ("EXIT_BLOCK has IL associated with it");
4099 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
4100 if (e->flags & EDGE_FALLTHRU)
4102 error ("fallthru to exit from bb %d", e->src->index);
4108 bool found_ctrl_stmt = false;
4112 /* Skip labels on the start of basic block. */
4113 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4116 gimple prev_stmt = stmt;
4118 stmt = gsi_stmt (gsi);
4120 if (gimple_code (stmt) != GIMPLE_LABEL)
4123 label = gimple_label_label (stmt);
4124 if (prev_stmt && DECL_NONLOCAL (label))
4126 error ("nonlocal label ");
4127 print_generic_expr (stderr, label, 0);
4128 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4133 if (label_to_block (label) != bb)
4136 print_generic_expr (stderr, label, 0);
4137 fprintf (stderr, " to block does not match in bb %d",
4142 if (decl_function_context (label) != current_function_decl)
4145 print_generic_expr (stderr, label, 0);
4146 fprintf (stderr, " has incorrect context in bb %d",
4152 /* Verify that body of basic block BB is free of control flow. */
4153 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4155 gimple stmt = gsi_stmt (gsi);
4157 if (found_ctrl_stmt)
4159 error ("control flow in the middle of basic block %d",
4164 if (stmt_ends_bb_p (stmt))
4165 found_ctrl_stmt = true;
4167 if (gimple_code (stmt) == GIMPLE_LABEL)
4170 print_generic_expr (stderr, gimple_label_label (stmt), 0);
4171 fprintf (stderr, " in the middle of basic block %d", bb->index);
4176 gsi = gsi_last_bb (bb);
4177 if (gsi_end_p (gsi))
4180 stmt = gsi_stmt (gsi);
4182 err |= verify_eh_edges (stmt);
4184 if (is_ctrl_stmt (stmt))
4186 FOR_EACH_EDGE (e, ei, bb->succs)
4187 if (e->flags & EDGE_FALLTHRU)
4189 error ("fallthru edge after a control statement in bb %d",
4195 if (gimple_code (stmt) != GIMPLE_COND)
4197 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
4198 after anything else but if statement. */
4199 FOR_EACH_EDGE (e, ei, bb->succs)
4200 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
4202 error ("true/false edge after a non-GIMPLE_COND in bb %d",
4208 switch (gimple_code (stmt))
4215 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
4219 || !(true_edge->flags & EDGE_TRUE_VALUE)
4220 || !(false_edge->flags & EDGE_FALSE_VALUE)
4221 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4222 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4223 || EDGE_COUNT (bb->succs) >= 3)
4225 error ("wrong outgoing edge flags at end of bb %d",
4233 if (simple_goto_p (stmt))
4235 error ("explicit goto at end of bb %d", bb->index);
4240 /* FIXME. We should double check that the labels in the
4241 destination blocks have their address taken. */
4242 FOR_EACH_EDGE (e, ei, bb->succs)
4243 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
4244 | EDGE_FALSE_VALUE))
4245 || !(e->flags & EDGE_ABNORMAL))
4247 error ("wrong outgoing edge flags at end of bb %d",
4255 if (!single_succ_p (bb)
4256 || (single_succ_edge (bb)->flags
4257 & (EDGE_FALLTHRU | EDGE_ABNORMAL
4258 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4260 error ("wrong outgoing edge flags at end of bb %d", bb->index);
4263 if (single_succ (bb) != EXIT_BLOCK_PTR)
4265 error ("return edge does not point to exit in bb %d",
4277 n = gimple_switch_num_labels (stmt);
4279 /* Mark all the destination basic blocks. */
4280 for (i = 0; i < n; ++i)
4282 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4283 basic_block label_bb = label_to_block (lab);
4284 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
4285 label_bb->aux = (void *)1;
4288 /* Verify that the case labels are sorted. */
4289 prev = gimple_switch_label (stmt, 0);
4290 for (i = 1; i < n; ++i)
4292 tree c = gimple_switch_label (stmt, i);
4295 error ("found default case not at the start of "
4301 && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
4303 error ("case labels not sorted: ");
4304 print_generic_expr (stderr, prev, 0);
4305 fprintf (stderr," is greater than ");
4306 print_generic_expr (stderr, c, 0);
4307 fprintf (stderr," but comes before it.\n");
4312 /* VRP will remove the default case if it can prove it will
4313 never be executed. So do not verify there always exists
4314 a default case here. */
4316 FOR_EACH_EDGE (e, ei, bb->succs)
4320 error ("extra outgoing edge %d->%d",
4321 bb->index, e->dest->index);
4325 e->dest->aux = (void *)2;
4326 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
4327 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4329 error ("wrong outgoing edge flags at end of bb %d",
4335 /* Check that we have all of them. */
4336 for (i = 0; i < n; ++i)
4338 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4339 basic_block label_bb = label_to_block (lab);
4341 if (label_bb->aux != (void *)2)
4343 error ("missing edge %i->%i", bb->index, label_bb->index);
4348 FOR_EACH_EDGE (e, ei, bb->succs)
4349 e->dest->aux = (void *)0;
4356 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
4357 verify_dominators (CDI_DOMINATORS);
4363 /* Updates phi nodes after creating a forwarder block joined
4364 by edge FALLTHRU. */
4367 gimple_make_forwarder_block (edge fallthru)
4371 basic_block dummy, bb;
4373 gimple_stmt_iterator gsi;
4375 dummy = fallthru->src;
4376 bb = fallthru->dest;
4378 if (single_pred_p (bb))
4381 /* If we redirected a branch we must create new PHI nodes at the
4383 for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
4385 gimple phi, new_phi;
4387 phi = gsi_stmt (gsi);
4388 var = gimple_phi_result (phi);
4389 new_phi = create_phi_node (var, bb);
4390 SSA_NAME_DEF_STMT (var) = new_phi;
4391 gimple_phi_set_result (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
4392 add_phi_arg (new_phi, gimple_phi_result (phi), fallthru);
4395 /* Add the arguments we have stored on edges. */
4396 FOR_EACH_EDGE (e, ei, bb->preds)
4401 flush_pending_stmts (e);
4406 /* Return a non-special label in the head of basic block BLOCK.
4407 Create one if it doesn't exist. */
4410 gimple_block_label (basic_block bb)
4412 gimple_stmt_iterator i, s = gsi_start_bb (bb);
4417 for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
4419 stmt = gsi_stmt (i);
4420 if (gimple_code (stmt) != GIMPLE_LABEL)
4422 label = gimple_label_label (stmt);
4423 if (!DECL_NONLOCAL (label))
4426 gsi_move_before (&i, &s);
4431 label = create_artificial_label ();
4432 stmt = gimple_build_label (label);
4433 gsi_insert_before (&s, stmt, GSI_NEW_STMT);
4438 /* Attempt to perform edge redirection by replacing a possibly complex
4439 jump instruction by a goto or by removing the jump completely.
4440 This can apply only if all edges now point to the same block. The
4441 parameters and return values are equivalent to
4442 redirect_edge_and_branch. */
4445 gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
4447 basic_block src = e->src;
4448 gimple_stmt_iterator i;
4451 /* We can replace or remove a complex jump only when we have exactly
4453 if (EDGE_COUNT (src->succs) != 2
4454 /* Verify that all targets will be TARGET. Specifically, the
4455 edge that is not E must also go to TARGET. */
4456 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4459 i = gsi_last_bb (src);
4463 stmt = gsi_stmt (i);
4465 if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
4467 gsi_remove (&i, true);
4468 e = ssa_redirect_edge (e, target);
4469 e->flags = EDGE_FALLTHRU;
4477 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4478 edge representing the redirected branch. */
4481 gimple_redirect_edge_and_branch (edge e, basic_block dest)
4483 basic_block bb = e->src;
4484 gimple_stmt_iterator gsi;
4488 if (e->flags & EDGE_ABNORMAL)
4491 if (e->src != ENTRY_BLOCK_PTR
4492 && (ret = gimple_try_redirect_by_replacing_jump (e, dest)))
4495 if (e->dest == dest)
4498 gsi = gsi_last_bb (bb);
4499 stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
4501 switch (stmt ? gimple_code (stmt) : ERROR_MARK)
4504 /* For COND_EXPR, we only need to redirect the edge. */
4508 /* No non-abnormal edges should lead from a non-simple goto, and
4509 simple ones should be represented implicitly. */
4514 tree label = gimple_block_label (dest);
4515 tree cases = get_cases_for_edge (e, stmt);
4517 /* If we have a list of cases associated with E, then use it
4518 as it's a lot faster than walking the entire case vector. */
4521 edge e2 = find_edge (e->src, dest);
4528 CASE_LABEL (cases) = label;
4529 cases = TREE_CHAIN (cases);
4532 /* If there was already an edge in the CFG, then we need
4533 to move all the cases associated with E to E2. */
4536 tree cases2 = get_cases_for_edge (e2, stmt);
4538 TREE_CHAIN (last) = TREE_CHAIN (cases2);
4539 TREE_CHAIN (cases2) = first;
4544 size_t i, n = gimple_switch_num_labels (stmt);
4546 for (i = 0; i < n; i++)
4548 tree elt = gimple_switch_label (stmt, i);
4549 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4550 CASE_LABEL (elt) = label;
4558 gsi_remove (&gsi, true);
4559 e->flags |= EDGE_FALLTHRU;
4562 case GIMPLE_OMP_RETURN:
4563 case GIMPLE_OMP_CONTINUE:
4564 case GIMPLE_OMP_SECTIONS_SWITCH:
4565 case GIMPLE_OMP_FOR:
4566 /* The edges from OMP constructs can be simply redirected. */
4570 /* Otherwise it must be a fallthru edge, and we don't need to
4571 do anything besides redirecting it. */
4572 gcc_assert (e->flags & EDGE_FALLTHRU);
4576 /* Update/insert PHI nodes as necessary. */
4578 /* Now update the edges in the CFG. */
4579 e = ssa_redirect_edge (e, dest);
4584 /* Returns true if it is possible to remove edge E by redirecting
4585 it to the destination of the other edge from E->src. */
4588 gimple_can_remove_branch_p (const_edge e)
4590 if (e->flags & EDGE_ABNORMAL)
4596 /* Simple wrapper, as we can always redirect fallthru edges. */
4599 gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
4601 e = gimple_redirect_edge_and_branch (e, dest);
4608 /* Splits basic block BB after statement STMT (but at least after the
4609 labels). If STMT is NULL, BB is split just after the labels. */
4612 gimple_split_block (basic_block bb, void *stmt)
4614 gimple_stmt_iterator gsi;
4615 gimple_stmt_iterator gsi_tgt;
4622 new_bb = create_empty_bb (bb);
4624 /* Redirect the outgoing edges. */
4625 new_bb->succs = bb->succs;
4627 FOR_EACH_EDGE (e, ei, new_bb->succs)
4630 if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
4633 /* Move everything from GSI to the new basic block. */
4634 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4636 act = gsi_stmt (gsi);
4637 if (gimple_code (act) == GIMPLE_LABEL)
4650 if (gsi_end_p (gsi))
4653 /* Split the statement list - avoid re-creating new containers as this
4654 brings ugly quadratic memory consumption in the inliner.
4655 (We are still quadratic since we need to update stmt BB pointers,
4657 list = gsi_split_seq_before (&gsi);
4658 set_bb_seq (new_bb, list);
4659 for (gsi_tgt = gsi_start (list);
4660 !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
4661 gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
4667 /* Moves basic block BB after block AFTER. */
4670 gimple_move_block_after (basic_block bb, basic_block after)
4672 if (bb->prev_bb == after)
4676 link_block (bb, after);
4682 /* Return true if basic_block can be duplicated. */
4685 gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
4690 /* Create a duplicate of the basic block BB. NOTE: This does not
4691 preserve SSA form. */
4694 gimple_duplicate_bb (basic_block bb)
4697 gimple_stmt_iterator gsi, gsi_tgt;
4698 gimple_seq phis = phi_nodes (bb);
4699 gimple phi, stmt, copy;
4701 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4703 /* Copy the PHI nodes. We ignore PHI node arguments here because
4704 the incoming edges have not been setup yet. */
4705 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
4707 phi = gsi_stmt (gsi);
4708 copy = create_phi_node (gimple_phi_result (phi), new_bb);
4709 create_new_def_for (gimple_phi_result (copy), copy,
4710 gimple_phi_result_ptr (copy));
4713 gsi_tgt = gsi_start_bb (new_bb);
4714 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4716 def_operand_p def_p;
4717 ssa_op_iter op_iter;
4720 stmt = gsi_stmt (gsi);
4721 if (gimple_code (stmt) == GIMPLE_LABEL)
4724 /* Create a new copy of STMT and duplicate STMT's virtual
4726 copy = gimple_copy (stmt);
4727 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
4728 copy_virtual_operands (copy, stmt);
4729 region = lookup_stmt_eh_region (stmt);
4731 add_stmt_to_eh_region (copy, region);
4732 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
4734 /* Create new names for all the definitions created by COPY and
4735 add replacement mappings for each new name. */
4736 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
4737 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
4743 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
4746 add_phi_args_after_copy_edge (edge e_copy)
4748 basic_block bb, bb_copy = e_copy->src, dest;
4751 gimple phi, phi_copy;
4753 gimple_stmt_iterator psi, psi_copy;
4755 if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
4758 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
4760 if (e_copy->dest->flags & BB_DUPLICATED)
4761 dest = get_bb_original (e_copy->dest);
4763 dest = e_copy->dest;
4765 e = find_edge (bb, dest);
4768 /* During loop unrolling the target of the latch edge is copied.
4769 In this case we are not looking for edge to dest, but to
4770 duplicated block whose original was dest. */
4771 FOR_EACH_EDGE (e, ei, bb->succs)
4773 if ((e->dest->flags & BB_DUPLICATED)
4774 && get_bb_original (e->dest) == dest)
4778 gcc_assert (e != NULL);
4781 for (psi = gsi_start_phis (e->dest),
4782 psi_copy = gsi_start_phis (e_copy->dest);
4784 gsi_next (&psi), gsi_next (&psi_copy))
4786 phi = gsi_stmt (psi);
4787 phi_copy = gsi_stmt (psi_copy);
4788 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4789 add_phi_arg (phi_copy, def, e_copy);
4794 /* Basic block BB_COPY was created by code duplication. Add phi node
4795 arguments for edges going out of BB_COPY. The blocks that were
4796 duplicated have BB_DUPLICATED set. */
4799 add_phi_args_after_copy_bb (basic_block bb_copy)
4804 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
4806 add_phi_args_after_copy_edge (e_copy);
4810 /* Blocks in REGION_COPY array of length N_REGION were created by
4811 duplication of basic blocks. Add phi node arguments for edges
4812 going from these blocks. If E_COPY is not NULL, also add
4813 phi node arguments for its destination.*/
4816 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
4821 for (i = 0; i < n_region; i++)
4822 region_copy[i]->flags |= BB_DUPLICATED;
4824 for (i = 0; i < n_region; i++)
4825 add_phi_args_after_copy_bb (region_copy[i]);
4827 add_phi_args_after_copy_edge (e_copy);
4829 for (i = 0; i < n_region; i++)
4830 region_copy[i]->flags &= ~BB_DUPLICATED;
4833 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4834 important exit edge EXIT. By important we mean that no SSA name defined
4835 inside region is live over the other exit edges of the region. All entry
4836 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4837 to the duplicate of the region. SSA form, dominance and loop information
4838 is updated. The new basic blocks are stored to REGION_COPY in the same
4839 order as they had in REGION, provided that REGION_COPY is not NULL.
4840 The function returns false if it is unable to copy the region,
4844 gimple_duplicate_sese_region (edge entry, edge exit,
4845 basic_block *region, unsigned n_region,
4846 basic_block *region_copy)
4849 bool free_region_copy = false, copying_header = false;
4850 struct loop *loop = entry->dest->loop_father;
4852 VEC (basic_block, heap) *doms;
4854 int total_freq = 0, entry_freq = 0;
4855 gcov_type total_count = 0, entry_count = 0;
4857 if (!can_copy_bbs_p (region, n_region))
4860 /* Some sanity checking. Note that we do not check for all possible
4861 missuses of the functions. I.e. if you ask to copy something weird,
4862 it will work, but the state of structures probably will not be
4864 for (i = 0; i < n_region; i++)
4866 /* We do not handle subloops, i.e. all the blocks must belong to the
4868 if (region[i]->loop_father != loop)
4871 if (region[i] != entry->dest
4872 && region[i] == loop->header)
4876 set_loop_copy (loop, loop);
4878 /* In case the function is used for loop header copying (which is the primary
4879 use), ensure that EXIT and its copy will be new latch and entry edges. */
4880 if (loop->header == entry->dest)
4882 copying_header = true;
4883 set_loop_copy (loop, loop_outer (loop));
4885 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
4888 for (i = 0; i < n_region; i++)
4889 if (region[i] != exit->src
4890 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
4896 region_copy = XNEWVEC (basic_block, n_region);
4897 free_region_copy = true;
4900 gcc_assert (!need_ssa_update_p ());
4902 /* Record blocks outside the region that are dominated by something
4905 initialize_original_copy_tables ();
4907 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
4909 if (entry->dest->count)
4911 total_count = entry->dest->count;
4912 entry_count = entry->count;
4913 /* Fix up corner cases, to avoid division by zero or creation of negative
4915 if (entry_count > total_count)
4916 entry_count = total_count;
4920 total_freq = entry->dest->frequency;
4921 entry_freq = EDGE_FREQUENCY (entry);
4922 /* Fix up corner cases, to avoid division by zero or creation of negative
4924 if (total_freq == 0)
4926 else if (entry_freq > total_freq)
4927 entry_freq = total_freq;
4930 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
4931 split_edge_bb_loc (entry));
4934 scale_bbs_frequencies_gcov_type (region, n_region,
4935 total_count - entry_count,
4937 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
4942 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
4944 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
4949 loop->header = exit->dest;
4950 loop->latch = exit->src;
4953 /* Redirect the entry and add the phi node arguments. */
4954 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
4955 gcc_assert (redirected != NULL);
4956 flush_pending_stmts (entry);
4958 /* Concerning updating of dominators: We must recount dominators
4959 for entry block and its copy. Anything that is outside of the
4960 region, but was dominated by something inside needs recounting as
4962 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
4963 VEC_safe_push (basic_block, heap, doms, get_bb_original (entry->dest));
4964 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
4965 VEC_free (basic_block, heap, doms);
4967 /* Add the other PHI node arguments. */
4968 add_phi_args_after_copy (region_copy, n_region, NULL);
4970 /* Update the SSA web. */
4971 update_ssa (TODO_update_ssa);
4973 if (free_region_copy)
4976 free_original_copy_tables ();
4980 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
4981 are stored to REGION_COPY in the same order in that they appear
4982 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
4983 the region, EXIT an exit from it. The condition guarding EXIT
4984 is moved to ENTRY. Returns true if duplication succeeds, false
5010 gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
5011 basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
5012 basic_block *region_copy ATTRIBUTE_UNUSED)
5015 bool free_region_copy = false;
5016 struct loop *loop = exit->dest->loop_father;
5017 struct loop *orig_loop = entry->dest->loop_father;
5018 basic_block switch_bb, entry_bb, nentry_bb;
5019 VEC (basic_block, heap) *doms;
5020 int total_freq = 0, exit_freq = 0;
5021 gcov_type total_count = 0, exit_count = 0;
5022 edge exits[2], nexits[2], e;
5023 gimple_stmt_iterator gsi;
5027 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
5029 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
5031 if (!can_copy_bbs_p (region, n_region))
5034 /* Some sanity checking. Note that we do not check for all possible
5035 missuses of the functions. I.e. if you ask to copy something weird
5036 (e.g., in the example, if there is a jump from inside to the middle
5037 of some_code, or come_code defines some of the values used in cond)
5038 it will work, but the resulting code will not be correct. */
5039 for (i = 0; i < n_region; i++)
5041 /* We do not handle subloops, i.e. all the blocks must belong to the
5043 if (region[i]->loop_father != orig_loop)
5046 if (region[i] == orig_loop->latch)
5050 initialize_original_copy_tables ();
5051 set_loop_copy (orig_loop, loop);
5055 region_copy = XNEWVEC (basic_block, n_region);
5056 free_region_copy = true;
5059 gcc_assert (!need_ssa_update_p ());
5061 /* Record blocks outside the region that are dominated by something
5063 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5065 if (exit->src->count)
5067 total_count = exit->src->count;
5068 exit_count = exit->count;
5069 /* Fix up corner cases, to avoid division by zero or creation of negative
5071 if (exit_count > total_count)
5072 exit_count = total_count;
5076 total_freq = exit->src->frequency;
5077 exit_freq = EDGE_FREQUENCY (exit);
5078 /* Fix up corner cases, to avoid division by zero or creation of negative
5080 if (total_freq == 0)
5082 if (exit_freq > total_freq)
5083 exit_freq = total_freq;
5086 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
5087 split_edge_bb_loc (exit));
5090 scale_bbs_frequencies_gcov_type (region, n_region,
5091 total_count - exit_count,
5093 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
5098 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
5100 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
5103 /* Create the switch block, and put the exit condition to it. */
5104 entry_bb = entry->dest;
5105 nentry_bb = get_bb_copy (entry_bb);
5106 if (!last_stmt (entry->src)
5107 || !stmt_ends_bb_p (last_stmt (entry->src)))
5108 switch_bb = entry->src;
5110 switch_bb = split_edge (entry);
5111 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
5113 gsi = gsi_last_bb (switch_bb);
5114 cond_stmt = last_stmt (exit->src);
5115 gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
5116 cond_stmt = gimple_copy (cond_stmt);
5117 gimple_cond_set_lhs (cond_stmt, unshare_expr (gimple_cond_lhs (cond_stmt)));
5118 gimple_cond_set_rhs (cond_stmt, unshare_expr (gimple_cond_rhs (cond_stmt)));
5119 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
5121 sorig = single_succ_edge (switch_bb);
5122 sorig->flags = exits[1]->flags;
5123 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
5125 /* Register the new edge from SWITCH_BB in loop exit lists. */
5126 rescan_loop_exit (snew, true, false);
5128 /* Add the PHI node arguments. */
5129 add_phi_args_after_copy (region_copy, n_region, snew);
5131 /* Get rid of now superfluous conditions and associated edges (and phi node
5133 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
5134 PENDING_STMT (e) = NULL;
5135 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
5136 PENDING_STMT (e) = NULL;
5138 /* Anything that is outside of the region, but was dominated by something
5139 inside needs to update dominance info. */
5140 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5141 VEC_free (basic_block, heap, doms);
5143 /* Update the SSA web. */
5144 update_ssa (TODO_update_ssa);
5146 if (free_region_copy)
5149 free_original_copy_tables ();
5153 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
5154 adding blocks when the dominator traversal reaches EXIT. This
5155 function silently assumes that ENTRY strictly dominates EXIT. */
5158 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
5159 VEC(basic_block,heap) **bbs_p)
5163 for (son = first_dom_son (CDI_DOMINATORS, entry);
5165 son = next_dom_son (CDI_DOMINATORS, son))
5167 VEC_safe_push (basic_block, heap, *bbs_p, son);
5169 gather_blocks_in_sese_region (son, exit, bbs_p);
5173 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
5174 The duplicates are recorded in VARS_MAP. */
5177 replace_by_duplicate_decl (tree *tp, struct pointer_map_t *vars_map,
5180 tree t = *tp, new_t;
5181 struct function *f = DECL_STRUCT_FUNCTION (to_context);
5184 if (DECL_CONTEXT (t) == to_context)
5187 loc = pointer_map_contains (vars_map, t);
5191 loc = pointer_map_insert (vars_map, t);
5195 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
5196 f->local_decls = tree_cons (NULL_TREE, new_t, f->local_decls);
5200 gcc_assert (TREE_CODE (t) == CONST_DECL);
5201 new_t = copy_node (t);
5203 DECL_CONTEXT (new_t) = to_context;
5208 new_t = (tree) *loc;
5214 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
5215 VARS_MAP maps old ssa names and var_decls to the new ones. */
5218 replace_ssa_name (tree name, struct pointer_map_t *vars_map,
5222 tree new_name, decl = SSA_NAME_VAR (name);
5224 gcc_assert (is_gimple_reg (name));
5226 loc = pointer_map_contains (vars_map, name);
5230 replace_by_duplicate_decl (&decl, vars_map, to_context);
5232 push_cfun (DECL_STRUCT_FUNCTION (to_context));
5233 if (gimple_in_ssa_p (cfun))
5234 add_referenced_var (decl);
5236 new_name = make_ssa_name (decl, SSA_NAME_DEF_STMT (name));
5237 if (SSA_NAME_IS_DEFAULT_DEF (name))
5238 set_default_def (decl, new_name);
5241 loc = pointer_map_insert (vars_map, name);
5245 new_name = (tree) *loc;
5256 struct pointer_map_t *vars_map;
5257 htab_t new_label_map;
5261 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
5262 contained in *TP if it has been ORIG_BLOCK previously and change the
5263 DECL_CONTEXT of every local variable referenced in *TP. */
5266 move_stmt_op (tree *tp, int *walk_subtrees, void *data)
5268 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5269 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
5273 /* We should never have TREE_BLOCK set on non-statements. */
5274 gcc_assert (!TREE_BLOCK (t));
5276 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
5278 if (TREE_CODE (t) == SSA_NAME)
5279 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
5280 else if (TREE_CODE (t) == LABEL_DECL)
5282 if (p->new_label_map)
5284 struct tree_map in, *out;
5286 out = (struct tree_map *)
5287 htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
5292 DECL_CONTEXT (t) = p->to_context;
5294 else if (p->remap_decls_p)
5296 /* Replace T with its duplicate. T should no longer appear in the
5297 parent function, so this looks wasteful; however, it may appear
5298 in referenced_vars, and more importantly, as virtual operands of
5299 statements, and in alias lists of other variables. It would be
5300 quite difficult to expunge it from all those places. ??? It might
5301 suffice to do this for addressable variables. */
5302 if ((TREE_CODE (t) == VAR_DECL
5303 && !is_global_var (t))
5304 || TREE_CODE (t) == CONST_DECL)
5305 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
5308 && gimple_in_ssa_p (cfun))
5310 push_cfun (DECL_STRUCT_FUNCTION (p->to_context));
5311 add_referenced_var (*tp);
5317 else if (TYPE_P (t))
5323 /* Like move_stmt_op, but for gimple statements.
5325 Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
5326 contained in the current statement in *GSI_P and change the
5327 DECL_CONTEXT of every local variable referenced in the current
5331 move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
5332 struct walk_stmt_info *wi)
5334 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
5335 gimple stmt = gsi_stmt (*gsi_p);
5336 tree block = gimple_block (stmt);
5338 if (p->orig_block == NULL_TREE
5339 || block == p->orig_block
5340 || block == NULL_TREE)
5341 gimple_set_block (stmt, p->new_block);
5342 #ifdef ENABLE_CHECKING
5343 else if (block != p->new_block)
5345 while (block && block != p->orig_block)
5346 block = BLOCK_SUPERCONTEXT (block);
5351 if (is_gimple_omp (stmt)
5352 && gimple_code (stmt) != GIMPLE_OMP_RETURN
5353 && gimple_code (stmt) != GIMPLE_OMP_CONTINUE)
5355 /* Do not remap variables inside OMP directives. Variables
5356 referenced in clauses and directive header belong to the
5357 parent function and should not be moved into the child
5359 bool save_remap_decls_p = p->remap_decls_p;
5360 p->remap_decls_p = false;
5361 *handled_ops_p = true;
5363 walk_gimple_seq (gimple_omp_body (stmt), move_stmt_r, move_stmt_op, wi);
5365 p->remap_decls_p = save_remap_decls_p;
5371 /* Marks virtual operands of all statements in basic blocks BBS for
5375 mark_virtual_ops_in_bb (basic_block bb)
5377 gimple_stmt_iterator gsi;
5379 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5380 mark_virtual_ops_for_renaming (gsi_stmt (gsi));
5382 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5383 mark_virtual_ops_for_renaming (gsi_stmt (gsi));
5386 /* Marks virtual operands of all statements in basic blocks BBS for
5390 mark_virtual_ops_in_region (VEC (basic_block,heap) *bbs)
5395 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
5396 mark_virtual_ops_in_bb (bb);
5399 /* Move basic block BB from function CFUN to function DEST_FN. The
5400 block is moved out of the original linked list and placed after
5401 block AFTER in the new list. Also, the block is removed from the
5402 original array of blocks and placed in DEST_FN's array of blocks.
5403 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
5404 updated to reflect the moved edges.
5406 The local variables are remapped to new instances, VARS_MAP is used
5407 to record the mapping. */
5410 move_block_to_fn (struct function *dest_cfun, basic_block bb,
5411 basic_block after, bool update_edge_count_p,
5412 struct move_stmt_d *d, int eh_offset)
5414 struct control_flow_graph *cfg;
5417 gimple_stmt_iterator si;
5418 unsigned old_len, new_len;
5420 /* Remove BB from dominance structures. */
5421 delete_from_dominance_info (CDI_DOMINATORS, bb);
5423 remove_bb_from_loops (bb);
5425 /* Link BB to the new linked list. */
5426 move_block_after (bb, after);
5428 /* Update the edge count in the corresponding flowgraphs. */
5429 if (update_edge_count_p)
5430 FOR_EACH_EDGE (e, ei, bb->succs)
5432 cfun->cfg->x_n_edges--;
5433 dest_cfun->cfg->x_n_edges++;
5436 /* Remove BB from the original basic block array. */
5437 VEC_replace (basic_block, cfun->cfg->x_basic_block_info, bb->index, NULL);
5438 cfun->cfg->x_n_basic_blocks--;
5440 /* Grow DEST_CFUN's basic block array if needed. */
5441 cfg = dest_cfun->cfg;
5442 cfg->x_n_basic_blocks++;
5443 if (bb->index >= cfg->x_last_basic_block)
5444 cfg->x_last_basic_block = bb->index + 1;
5446 old_len = VEC_length (basic_block, cfg->x_basic_block_info);
5447 if ((unsigned) cfg->x_last_basic_block >= old_len)
5449 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
5450 VEC_safe_grow_cleared (basic_block, gc, cfg->x_basic_block_info,
5454 VEC_replace (basic_block, cfg->x_basic_block_info,
5457 /* Remap the variables in phi nodes. */
5458 for (si = gsi_start_phis (bb); !gsi_end_p (si); )
5460 gimple phi = gsi_stmt (si);
5462 tree op = PHI_RESULT (phi);
5465 if (!is_gimple_reg (op))
5467 /* Remove the phi nodes for virtual operands (alias analysis will be
5468 run for the new function, anyway). */
5469 remove_phi_node (&si, true);
5473 SET_PHI_RESULT (phi,
5474 replace_ssa_name (op, d->vars_map, dest_cfun->decl));
5475 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
5477 op = USE_FROM_PTR (use);
5478 if (TREE_CODE (op) == SSA_NAME)
5479 SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
5485 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5487 gimple stmt = gsi_stmt (si);
5489 struct walk_stmt_info wi;
5491 memset (&wi, 0, sizeof (wi));
5493 walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
5495 if (gimple_code (stmt) == GIMPLE_LABEL)
5497 tree label = gimple_label_label (stmt);
5498 int uid = LABEL_DECL_UID (label);
5500 gcc_assert (uid > -1);
5502 old_len = VEC_length (basic_block, cfg->x_label_to_block_map);
5503 if (old_len <= (unsigned) uid)
5505 new_len = 3 * uid / 2;
5506 VEC_safe_grow_cleared (basic_block, gc,
5507 cfg->x_label_to_block_map, new_len);
5510 VEC_replace (basic_block, cfg->x_label_to_block_map, uid, bb);
5511 VEC_replace (basic_block, cfun->cfg->x_label_to_block_map, uid, NULL);
5513 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
5515 if (uid >= dest_cfun->cfg->last_label_uid)
5516 dest_cfun->cfg->last_label_uid = uid + 1;
5518 else if (gimple_code (stmt) == GIMPLE_RESX && eh_offset != 0)
5519 gimple_resx_set_region (stmt, gimple_resx_region (stmt) + eh_offset);
5521 region = lookup_stmt_eh_region (stmt);
5524 add_stmt_to_eh_region_fn (dest_cfun, stmt, region + eh_offset);
5525 remove_stmt_from_eh_region (stmt);
5526 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
5527 gimple_remove_stmt_histograms (cfun, stmt);
5530 /* We cannot leave any operands allocated from the operand caches of
5531 the current function. */
5532 free_stmt_operands (stmt);
5533 push_cfun (dest_cfun);
5539 /* Examine the statements in BB (which is in SRC_CFUN); find and return
5540 the outermost EH region. Use REGION as the incoming base EH region. */
5543 find_outermost_region_in_block (struct function *src_cfun,
5544 basic_block bb, int region)
5546 gimple_stmt_iterator si;
5548 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5550 gimple stmt = gsi_stmt (si);
5553 if (gimple_code (stmt) == GIMPLE_RESX)
5554 stmt_region = gimple_resx_region (stmt);
5556 stmt_region = lookup_stmt_eh_region_fn (src_cfun, stmt);
5557 if (stmt_region > 0)
5560 region = stmt_region;
5561 else if (stmt_region != region)
5563 region = eh_region_outermost (src_cfun, stmt_region, region);
5564 gcc_assert (region != -1);
5573 new_label_mapper (tree decl, void *data)
5575 htab_t hash = (htab_t) data;
5579 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
5581 m = XNEW (struct tree_map);
5582 m->hash = DECL_UID (decl);
5583 m->base.from = decl;
5584 m->to = create_artificial_label ();
5585 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
5586 if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
5587 cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
5589 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
5590 gcc_assert (*slot == NULL);
5597 /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
5601 replace_block_vars_by_duplicates (tree block, struct pointer_map_t *vars_map,
5606 for (tp = &BLOCK_VARS (block); *tp; tp = &TREE_CHAIN (*tp))
5609 replace_by_duplicate_decl (&t, vars_map, to_context);
5612 if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
5614 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
5615 DECL_HAS_VALUE_EXPR_P (t) = 1;
5617 TREE_CHAIN (t) = TREE_CHAIN (*tp);
5622 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
5623 replace_block_vars_by_duplicates (block, vars_map, to_context);
5626 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
5627 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
5628 single basic block in the original CFG and the new basic block is
5629 returned. DEST_CFUN must not have a CFG yet.
5631 Note that the region need not be a pure SESE region. Blocks inside
5632 the region may contain calls to abort/exit. The only restriction
5633 is that ENTRY_BB should be the only entry point and it must
5636 Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
5637 functions outermost BLOCK, move all subblocks of ORIG_BLOCK
5638 to the new function.
5640 All local variables referenced in the region are assumed to be in
5641 the corresponding BLOCK_VARS and unexpanded variable lists
5642 associated with DEST_CFUN. */
5645 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
5646 basic_block exit_bb, tree orig_block)
5648 VEC(basic_block,heap) *bbs, *dom_bbs;
5649 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
5650 basic_block after, bb, *entry_pred, *exit_succ, abb;
5651 struct function *saved_cfun = cfun;
5652 int *entry_flag, *exit_flag, eh_offset;
5653 unsigned *entry_prob, *exit_prob;
5654 unsigned i, num_entry_edges, num_exit_edges;
5657 htab_t new_label_map;
5658 struct pointer_map_t *vars_map;
5659 struct loop *loop = entry_bb->loop_father;
5660 struct move_stmt_d d;
5662 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
5664 gcc_assert (entry_bb != exit_bb
5666 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
5668 /* Collect all the blocks in the region. Manually add ENTRY_BB
5669 because it won't be added by dfs_enumerate_from. */
5671 VEC_safe_push (basic_block, heap, bbs, entry_bb);
5672 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
5674 /* The blocks that used to be dominated by something in BBS will now be
5675 dominated by the new block. */
5676 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
5677 VEC_address (basic_block, bbs),
5678 VEC_length (basic_block, bbs));
5680 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
5681 the predecessor edges to ENTRY_BB and the successor edges to
5682 EXIT_BB so that we can re-attach them to the new basic block that
5683 will replace the region. */
5684 num_entry_edges = EDGE_COUNT (entry_bb->preds);
5685 entry_pred = (basic_block *) xcalloc (num_entry_edges, sizeof (basic_block));
5686 entry_flag = (int *) xcalloc (num_entry_edges, sizeof (int));
5687 entry_prob = XNEWVEC (unsigned, num_entry_edges);
5689 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
5691 entry_prob[i] = e->probability;
5692 entry_flag[i] = e->flags;
5693 entry_pred[i++] = e->src;
5699 num_exit_edges = EDGE_COUNT (exit_bb->succs);
5700 exit_succ = (basic_block *) xcalloc (num_exit_edges,
5701 sizeof (basic_block));
5702 exit_flag = (int *) xcalloc (num_exit_edges, sizeof (int));
5703 exit_prob = XNEWVEC (unsigned, num_exit_edges);
5705 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
5707 exit_prob[i] = e->probability;
5708 exit_flag[i] = e->flags;
5709 exit_succ[i++] = e->dest;
5721 /* Switch context to the child function to initialize DEST_FN's CFG. */
5722 gcc_assert (dest_cfun->cfg == NULL);
5723 push_cfun (dest_cfun);
5725 init_empty_tree_cfg ();
5727 /* Initialize EH information for the new function. */
5729 new_label_map = NULL;
5734 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
5735 region = find_outermost_region_in_block (saved_cfun, bb, region);
5737 init_eh_for_function ();
5740 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
5741 eh_offset = duplicate_eh_regions (saved_cfun, new_label_mapper,
5742 new_label_map, region, 0);
5748 /* The ssa form for virtual operands in the source function will have to
5749 be repaired. We do not care for the real operands -- the sese region
5750 must be closed with respect to those. */
5751 mark_virtual_ops_in_region (bbs);
5753 /* Move blocks from BBS into DEST_CFUN. */
5754 gcc_assert (VEC_length (basic_block, bbs) >= 2);
5755 after = dest_cfun->cfg->x_entry_block_ptr;
5756 vars_map = pointer_map_create ();
5758 memset (&d, 0, sizeof (d));
5759 d.vars_map = vars_map;
5760 d.from_context = cfun->decl;
5761 d.to_context = dest_cfun->decl;
5762 d.new_label_map = new_label_map;
5763 d.remap_decls_p = true;
5764 d.orig_block = orig_block;
5765 d.new_block = DECL_INITIAL (dest_cfun->decl);
5767 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
5769 /* No need to update edge counts on the last block. It has
5770 already been updated earlier when we detached the region from
5771 the original CFG. */
5772 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d, eh_offset);
5776 /* Rewire BLOCK_SUBBLOCKS of orig_block. */
5780 gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
5782 BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
5783 = BLOCK_SUBBLOCKS (orig_block);
5784 for (block = BLOCK_SUBBLOCKS (orig_block);
5785 block; block = BLOCK_CHAIN (block))
5786 BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
5787 BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
5790 replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
5791 vars_map, dest_cfun->decl);
5794 htab_delete (new_label_map);
5795 pointer_map_destroy (vars_map);
5797 /* Rewire the entry and exit blocks. The successor to the entry
5798 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
5799 the child function. Similarly, the predecessor of DEST_FN's
5800 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
5801 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
5802 various CFG manipulation function get to the right CFG.
5804 FIXME, this is silly. The CFG ought to become a parameter to
5806 push_cfun (dest_cfun);
5807 make_edge (ENTRY_BLOCK_PTR, entry_bb, EDGE_FALLTHRU);
5809 make_edge (exit_bb, EXIT_BLOCK_PTR, 0);
5812 /* Back in the original function, the SESE region has disappeared,
5813 create a new basic block in its place. */
5814 bb = create_empty_bb (entry_pred[0]);
5816 add_bb_to_loop (bb, loop);
5817 for (i = 0; i < num_entry_edges; i++)
5819 e = make_edge (entry_pred[i], bb, entry_flag[i]);
5820 e->probability = entry_prob[i];
5823 for (i = 0; i < num_exit_edges; i++)
5825 e = make_edge (bb, exit_succ[i], exit_flag[i]);
5826 e->probability = exit_prob[i];
5829 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
5830 for (i = 0; VEC_iterate (basic_block, dom_bbs, i, abb); i++)
5831 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
5832 VEC_free (basic_block, heap, dom_bbs);
5843 VEC_free (basic_block, heap, bbs);
5849 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree-pass.h)
5853 dump_function_to_file (tree fn, FILE *file, int flags)
5855 tree arg, vars, var;
5856 struct function *dsf;
5857 bool ignore_topmost_bind = false, any_var = false;
5861 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
5863 arg = DECL_ARGUMENTS (fn);
5866 print_generic_expr (file, TREE_TYPE (arg), dump_flags);
5867 fprintf (file, " ");
5868 print_generic_expr (file, arg, dump_flags);
5869 if (flags & TDF_VERBOSE)
5870 print_node (file, "", arg, 4);
5871 if (TREE_CHAIN (arg))
5872 fprintf (file, ", ");
5873 arg = TREE_CHAIN (arg);
5875 fprintf (file, ")\n");
5877 if (flags & TDF_VERBOSE)
5878 print_node (file, "", fn, 2);
5880 dsf = DECL_STRUCT_FUNCTION (fn);
5881 if (dsf && (flags & TDF_DETAILS))
5882 dump_eh_tree (file, dsf);
5884 if (flags & TDF_RAW && !gimple_body (fn))
5886 dump_node (fn, TDF_SLIM | flags, file);
5890 /* Switch CFUN to point to FN. */
5891 push_cfun (DECL_STRUCT_FUNCTION (fn));
5893 /* When GIMPLE is lowered, the variables are no longer available in
5894 BIND_EXPRs, so display them separately. */
5895 if (cfun && cfun->decl == fn && cfun->local_decls)
5897 ignore_topmost_bind = true;
5899 fprintf (file, "{\n");
5900 for (vars = cfun->local_decls; vars; vars = TREE_CHAIN (vars))
5902 var = TREE_VALUE (vars);
5904 print_generic_decl (file, var, flags);
5905 if (flags & TDF_VERBOSE)
5906 print_node (file, "", var, 4);
5907 fprintf (file, "\n");
5913 if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
5915 /* If the CFG has been built, emit a CFG-based dump. */
5916 check_bb_profile (ENTRY_BLOCK_PTR, file);
5917 if (!ignore_topmost_bind)
5918 fprintf (file, "{\n");
5920 if (any_var && n_basic_blocks)
5921 fprintf (file, "\n");
5924 gimple_dump_bb (bb, file, 2, flags);
5926 fprintf (file, "}\n");
5927 check_bb_profile (EXIT_BLOCK_PTR, file);
5929 else if (DECL_SAVED_TREE (fn) == NULL)
5931 /* The function is now in GIMPLE form but the CFG has not been
5932 built yet. Emit the single sequence of GIMPLE statements
5933 that make up its body. */
5934 gimple_seq body = gimple_body (fn);
5936 if (gimple_seq_first_stmt (body)
5937 && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
5938 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
5939 print_gimple_seq (file, body, 0, flags);
5942 if (!ignore_topmost_bind)
5943 fprintf (file, "{\n");
5946 fprintf (file, "\n");
5948 print_gimple_seq (file, body, 2, flags);
5949 fprintf (file, "}\n");
5956 /* Make a tree based dump. */
5957 chain = DECL_SAVED_TREE (fn);
5959 if (chain && TREE_CODE (chain) == BIND_EXPR)
5961 if (ignore_topmost_bind)
5963 chain = BIND_EXPR_BODY (chain);
5971 if (!ignore_topmost_bind)
5972 fprintf (file, "{\n");
5977 fprintf (file, "\n");
5979 print_generic_stmt_indented (file, chain, flags, indent);
5980 if (ignore_topmost_bind)
5981 fprintf (file, "}\n");
5984 fprintf (file, "\n\n");
5991 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
5994 debug_function (tree fn, int flags)
5996 dump_function_to_file (fn, stderr, flags);
6000 /* Print on FILE the indexes for the predecessors of basic_block BB. */
6003 print_pred_bbs (FILE *file, basic_block bb)
6008 FOR_EACH_EDGE (e, ei, bb->preds)
6009 fprintf (file, "bb_%d ", e->src->index);
6013 /* Print on FILE the indexes for the successors of basic_block BB. */
6016 print_succ_bbs (FILE *file, basic_block bb)
6021 FOR_EACH_EDGE (e, ei, bb->succs)
6022 fprintf (file, "bb_%d ", e->dest->index);
6025 /* Print to FILE the basic block BB following the VERBOSITY level. */
6028 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
6030 char *s_indent = (char *) alloca ((size_t) indent + 1);
6031 memset ((void *) s_indent, ' ', (size_t) indent);
6032 s_indent[indent] = '\0';
6034 /* Print basic_block's header. */
6037 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
6038 print_pred_bbs (file, bb);
6039 fprintf (file, "}, succs = {");
6040 print_succ_bbs (file, bb);
6041 fprintf (file, "})\n");
6044 /* Print basic_block's body. */
6047 fprintf (file, "%s {\n", s_indent);
6048 gimple_dump_bb (bb, file, indent + 4, TDF_VOPS|TDF_MEMSYMS);
6049 fprintf (file, "%s }\n", s_indent);
6053 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
6055 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
6056 VERBOSITY level this outputs the contents of the loop, or just its
6060 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
6068 s_indent = (char *) alloca ((size_t) indent + 1);
6069 memset ((void *) s_indent, ' ', (size_t) indent);
6070 s_indent[indent] = '\0';
6072 /* Print loop's header. */
6073 fprintf (file, "%sloop_%d (header = %d, latch = %d", s_indent,
6074 loop->num, loop->header->index, loop->latch->index);
6075 fprintf (file, ", niter = ");
6076 print_generic_expr (file, loop->nb_iterations, 0);
6078 if (loop->any_upper_bound)
6080 fprintf (file, ", upper_bound = ");
6081 dump_double_int (file, loop->nb_iterations_upper_bound, true);
6084 if (loop->any_estimate)
6086 fprintf (file, ", estimate = ");
6087 dump_double_int (file, loop->nb_iterations_estimate, true);
6089 fprintf (file, ")\n");
6091 /* Print loop's body. */
6094 fprintf (file, "%s{\n", s_indent);
6096 if (bb->loop_father == loop)
6097 print_loops_bb (file, bb, indent, verbosity);
6099 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
6100 fprintf (file, "%s}\n", s_indent);
6104 /* Print the LOOP and its sibling loops on FILE, indented INDENT
6105 spaces. Following VERBOSITY level this outputs the contents of the
6106 loop, or just its structure. */
6109 print_loop_and_siblings (FILE *file, struct loop *loop, int indent, int verbosity)
6114 print_loop (file, loop, indent, verbosity);
6115 print_loop_and_siblings (file, loop->next, indent, verbosity);
6118 /* Follow a CFG edge from the entry point of the program, and on entry
6119 of a loop, pretty print the loop structure on FILE. */
6122 print_loops (FILE *file, int verbosity)
6126 bb = BASIC_BLOCK (NUM_FIXED_BLOCKS);
6127 if (bb && bb->loop_father)
6128 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
6132 /* Debugging loops structure at tree level, at some VERBOSITY level. */
6135 debug_loops (int verbosity)
6137 print_loops (stderr, verbosity);
6140 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
6143 debug_loop (struct loop *loop, int verbosity)
6145 print_loop (stderr, loop, 0, verbosity);
6148 /* Print on stderr the code of loop number NUM, at some VERBOSITY
6152 debug_loop_num (unsigned num, int verbosity)
6154 debug_loop (get_loop (num), verbosity);
6157 /* Return true if BB ends with a call, possibly followed by some
6158 instructions that must stay with the call. Return false,
6162 gimple_block_ends_with_call_p (basic_block bb)
6164 gimple_stmt_iterator gsi = gsi_last_bb (bb);
6165 return is_gimple_call (gsi_stmt (gsi));
6169 /* Return true if BB ends with a conditional branch. Return false,
6173 gimple_block_ends_with_condjump_p (const_basic_block bb)
6175 gimple stmt = last_stmt (CONST_CAST_BB (bb));
6176 return (stmt && gimple_code (stmt) == GIMPLE_COND);
6180 /* Return true if we need to add fake edge to exit at statement T.
6181 Helper function for gimple_flow_call_edges_add. */
6184 need_fake_edge_p (gimple t)
6186 tree fndecl = NULL_TREE;
6189 /* NORETURN and LONGJMP calls already have an edge to exit.
6190 CONST and PURE calls do not need one.
6191 We don't currently check for CONST and PURE here, although
6192 it would be a good idea, because those attributes are
6193 figured out from the RTL in mark_constant_function, and
6194 the counter incrementation code from -fprofile-arcs
6195 leads to different results from -fbranch-probabilities. */
6196 if (is_gimple_call (t))
6198 fndecl = gimple_call_fndecl (t);
6199 call_flags = gimple_call_flags (t);
6202 if (is_gimple_call (t)
6204 && DECL_BUILT_IN (fndecl)
6205 && (call_flags & ECF_NOTHROW)
6206 && !(call_flags & ECF_NORETURN)
6207 && !(call_flags & ECF_RETURNS_TWICE))
6210 if (is_gimple_call (t)
6211 && !(call_flags & ECF_NORETURN))
6214 if (gimple_code (t) == ASM_EXPR
6215 && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
6222 /* Add fake edges to the function exit for any non constant and non
6223 noreturn calls, volatile inline assembly in the bitmap of blocks
6224 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
6225 the number of blocks that were split.
6227 The goal is to expose cases in which entering a basic block does
6228 not imply that all subsequent instructions must be executed. */
6231 gimple_flow_call_edges_add (sbitmap blocks)
6234 int blocks_split = 0;
6235 int last_bb = last_basic_block;
6236 bool check_last_block = false;
6238 if (n_basic_blocks == NUM_FIXED_BLOCKS)
6242 check_last_block = true;
6244 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
6246 /* In the last basic block, before epilogue generation, there will be
6247 a fallthru edge to EXIT. Special care is required if the last insn
6248 of the last basic block is a call because make_edge folds duplicate
6249 edges, which would result in the fallthru edge also being marked
6250 fake, which would result in the fallthru edge being removed by
6251 remove_fake_edges, which would result in an invalid CFG.
6253 Moreover, we can't elide the outgoing fake edge, since the block
6254 profiler needs to take this into account in order to solve the minimal
6255 spanning tree in the case that the call doesn't return.
6257 Handle this by adding a dummy instruction in a new last basic block. */
6258 if (check_last_block)
6260 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
6261 gimple_stmt_iterator gsi = gsi_last_bb (bb);
6264 if (!gsi_end_p (gsi))
6267 if (t && need_fake_edge_p (t))
6271 e = find_edge (bb, EXIT_BLOCK_PTR);
6274 gsi_insert_on_edge (e, gimple_build_nop ());
6275 gsi_commit_edge_inserts ();
6280 /* Now add fake edges to the function exit for any non constant
6281 calls since there is no way that we can determine if they will
6283 for (i = 0; i < last_bb; i++)
6285 basic_block bb = BASIC_BLOCK (i);
6286 gimple_stmt_iterator gsi;
6287 gimple stmt, last_stmt;
6292 if (blocks && !TEST_BIT (blocks, i))
6295 gsi = gsi_last_bb (bb);
6296 if (!gsi_end_p (gsi))
6298 last_stmt = gsi_stmt (gsi);
6301 stmt = gsi_stmt (gsi);
6302 if (need_fake_edge_p (stmt))
6306 /* The handling above of the final block before the
6307 epilogue should be enough to verify that there is
6308 no edge to the exit block in CFG already.
6309 Calling make_edge in such case would cause us to
6310 mark that edge as fake and remove it later. */
6311 #ifdef ENABLE_CHECKING
6312 if (stmt == last_stmt)
6314 e = find_edge (bb, EXIT_BLOCK_PTR);
6315 gcc_assert (e == NULL);
6319 /* Note that the following may create a new basic block
6320 and renumber the existing basic blocks. */
6321 if (stmt != last_stmt)
6323 e = split_block (bb, stmt);
6327 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
6331 while (!gsi_end_p (gsi));
6336 verify_flow_info ();
6338 return blocks_split;
6341 /* Purge dead abnormal call edges from basic block BB. */
6344 gimple_purge_dead_abnormal_call_edges (basic_block bb)
6346 bool changed = gimple_purge_dead_eh_edges (bb);
6348 if (cfun->has_nonlocal_label)
6350 gimple stmt = last_stmt (bb);
6354 if (!(stmt && stmt_can_make_abnormal_goto (stmt)))
6355 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6357 if (e->flags & EDGE_ABNORMAL)
6366 /* See gimple_purge_dead_eh_edges below. */
6368 free_dominance_info (CDI_DOMINATORS);
6374 /* Stores all basic blocks dominated by BB to DOM_BBS. */
6377 get_all_dominated_blocks (basic_block bb, VEC (basic_block, heap) **dom_bbs)
6381 VEC_safe_push (basic_block, heap, *dom_bbs, bb);
6382 for (son = first_dom_son (CDI_DOMINATORS, bb);
6384 son = next_dom_son (CDI_DOMINATORS, son))
6385 get_all_dominated_blocks (son, dom_bbs);
6388 /* Removes edge E and all the blocks dominated by it, and updates dominance
6389 information. The IL in E->src needs to be updated separately.
6390 If dominance info is not available, only the edge E is removed.*/
6393 remove_edge_and_dominated_blocks (edge e)
6395 VEC (basic_block, heap) *bbs_to_remove = NULL;
6396 VEC (basic_block, heap) *bbs_to_fix_dom = NULL;
6400 bool none_removed = false;
6402 basic_block bb, dbb;
6405 if (!dom_info_available_p (CDI_DOMINATORS))
6411 /* No updating is needed for edges to exit. */
6412 if (e->dest == EXIT_BLOCK_PTR)
6414 if (cfgcleanup_altered_bbs)
6415 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
6420 /* First, we find the basic blocks to remove. If E->dest has a predecessor
6421 that is not dominated by E->dest, then this set is empty. Otherwise,
6422 all the basic blocks dominated by E->dest are removed.
6424 Also, to DF_IDOM we store the immediate dominators of the blocks in
6425 the dominance frontier of E (i.e., of the successors of the
6426 removed blocks, if there are any, and of E->dest otherwise). */
6427 FOR_EACH_EDGE (f, ei, e->dest->preds)
6432 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
6434 none_removed = true;
6439 df = BITMAP_ALLOC (NULL);
6440 df_idom = BITMAP_ALLOC (NULL);
6443 bitmap_set_bit (df_idom,
6444 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
6447 get_all_dominated_blocks (e->dest, &bbs_to_remove);
6448 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6450 FOR_EACH_EDGE (f, ei, bb->succs)
6452 if (f->dest != EXIT_BLOCK_PTR)
6453 bitmap_set_bit (df, f->dest->index);
6456 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6457 bitmap_clear_bit (df, bb->index);
6459 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
6461 bb = BASIC_BLOCK (i);
6462 bitmap_set_bit (df_idom,
6463 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
6467 if (cfgcleanup_altered_bbs)
6469 /* Record the set of the altered basic blocks. */
6470 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
6471 bitmap_ior_into (cfgcleanup_altered_bbs, df);
6474 /* Remove E and the cancelled blocks. */
6479 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6480 delete_basic_block (bb);
6483 /* Update the dominance information. The immediate dominator may change only
6484 for blocks whose immediate dominator belongs to DF_IDOM:
6486 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
6487 removal. Let Z the arbitrary block such that idom(Z) = Y and
6488 Z dominates X after the removal. Before removal, there exists a path P
6489 from Y to X that avoids Z. Let F be the last edge on P that is
6490 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
6491 dominates W, and because of P, Z does not dominate W), and W belongs to
6492 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
6493 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
6495 bb = BASIC_BLOCK (i);
6496 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
6498 dbb = next_dom_son (CDI_DOMINATORS, dbb))
6499 VEC_safe_push (basic_block, heap, bbs_to_fix_dom, dbb);
6502 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
6505 BITMAP_FREE (df_idom);
6506 VEC_free (basic_block, heap, bbs_to_remove);
6507 VEC_free (basic_block, heap, bbs_to_fix_dom);
6510 /* Purge dead EH edges from basic block BB. */
6513 gimple_purge_dead_eh_edges (basic_block bb)
6515 bool changed = false;
6518 gimple stmt = last_stmt (bb);
6520 if (stmt && stmt_can_throw_internal (stmt))
6523 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6525 if (e->flags & EDGE_EH)
6527 remove_edge_and_dominated_blocks (e);
6538 gimple_purge_all_dead_eh_edges (const_bitmap blocks)
6540 bool changed = false;
6544 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
6546 changed |= gimple_purge_dead_eh_edges (BASIC_BLOCK (i));
6552 /* This function is called whenever a new edge is created or
6556 gimple_execute_on_growing_pred (edge e)
6558 basic_block bb = e->dest;
6561 reserve_phi_args_for_new_edge (bb);
6564 /* This function is called immediately before edge E is removed from
6565 the edge vector E->dest->preds. */
6568 gimple_execute_on_shrinking_pred (edge e)
6570 if (phi_nodes (e->dest))
6571 remove_phi_args (e);
6574 /*---------------------------------------------------------------------------
6575 Helper functions for Loop versioning
6576 ---------------------------------------------------------------------------*/
6578 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
6579 of 'first'. Both of them are dominated by 'new_head' basic block. When
6580 'new_head' was created by 'second's incoming edge it received phi arguments
6581 on the edge by split_edge(). Later, additional edge 'e' was created to
6582 connect 'new_head' and 'first'. Now this routine adds phi args on this
6583 additional edge 'e' that new_head to second edge received as part of edge
6587 gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
6588 basic_block new_head, edge e)
6591 gimple_stmt_iterator psi1, psi2;
6593 edge e2 = find_edge (new_head, second);
6595 /* Because NEW_HEAD has been created by splitting SECOND's incoming
6596 edge, we should always have an edge from NEW_HEAD to SECOND. */
6597 gcc_assert (e2 != NULL);
6599 /* Browse all 'second' basic block phi nodes and add phi args to
6600 edge 'e' for 'first' head. PHI args are always in correct order. */
6602 for (psi2 = gsi_start_phis (second),
6603 psi1 = gsi_start_phis (first);
6604 !gsi_end_p (psi2) && !gsi_end_p (psi1);
6605 gsi_next (&psi2), gsi_next (&psi1))
6607 phi1 = gsi_stmt (psi1);
6608 phi2 = gsi_stmt (psi2);
6609 def = PHI_ARG_DEF (phi2, e2->dest_idx);
6610 add_phi_arg (phi1, def, e);
6615 /* Adds a if else statement to COND_BB with condition COND_EXPR.
6616 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
6617 the destination of the ELSE part. */
6620 gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
6621 basic_block second_head ATTRIBUTE_UNUSED,
6622 basic_block cond_bb, void *cond_e)
6624 gimple_stmt_iterator gsi;
6625 gimple new_cond_expr;
6626 tree cond_expr = (tree) cond_e;
6629 /* Build new conditional expr */
6630 new_cond_expr = gimple_build_cond_from_tree (cond_expr,
6631 NULL_TREE, NULL_TREE);
6633 /* Add new cond in cond_bb. */
6634 gsi = gsi_last_bb (cond_bb);
6635 gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
6637 /* Adjust edges appropriately to connect new head with first head
6638 as well as second head. */
6639 e0 = single_succ_edge (cond_bb);
6640 e0->flags &= ~EDGE_FALLTHRU;
6641 e0->flags |= EDGE_FALSE_VALUE;
6644 struct cfg_hooks gimple_cfg_hooks = {
6646 gimple_verify_flow_info,
6647 gimple_dump_bb, /* dump_bb */
6648 create_bb, /* create_basic_block */
6649 gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
6650 gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
6651 gimple_can_remove_branch_p, /* can_remove_branch_p */
6652 remove_bb, /* delete_basic_block */
6653 gimple_split_block, /* split_block */
6654 gimple_move_block_after, /* move_block_after */
6655 gimple_can_merge_blocks_p, /* can_merge_blocks_p */
6656 gimple_merge_blocks, /* merge_blocks */
6657 gimple_predict_edge, /* predict_edge */
6658 gimple_predicted_by_p, /* predicted_by_p */
6659 gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
6660 gimple_duplicate_bb, /* duplicate_block */
6661 gimple_split_edge, /* split_edge */
6662 gimple_make_forwarder_block, /* make_forward_block */
6663 NULL, /* tidy_fallthru_edge */
6664 gimple_block_ends_with_call_p,/* block_ends_with_call_p */
6665 gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
6666 gimple_flow_call_edges_add, /* flow_call_edges_add */
6667 gimple_execute_on_growing_pred, /* execute_on_growing_pred */
6668 gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
6669 gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
6670 gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
6671 gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
6672 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
6673 flush_pending_stmts /* flush_pending_stmts */
6677 /* Split all critical edges. */
6680 split_critical_edges (void)
6686 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
6687 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
6688 mappings around the calls to split_edge. */
6689 start_recording_case_labels ();
6692 FOR_EACH_EDGE (e, ei, bb->succs)
6693 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
6698 end_recording_case_labels ();
6702 struct gimple_opt_pass pass_split_crit_edges =
6706 "crited", /* name */
6708 split_critical_edges, /* execute */
6711 0, /* static_pass_number */
6712 TV_TREE_SPLIT_EDGES, /* tv_id */
6713 PROP_cfg, /* properties required */
6714 PROP_no_crit_edges, /* properties_provided */
6715 0, /* properties_destroyed */
6716 0, /* todo_flags_start */
6717 TODO_dump_func /* todo_flags_finish */
6722 /* Build a ternary operation and gimplify it. Emit code before GSI.
6723 Return the gimple_val holding the result. */
6726 gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
6727 tree type, tree a, tree b, tree c)
6731 ret = fold_build3 (code, type, a, b, c);
6734 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
6738 /* Build a binary operation and gimplify it. Emit code before GSI.
6739 Return the gimple_val holding the result. */
6742 gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
6743 tree type, tree a, tree b)
6747 ret = fold_build2 (code, type, a, b);
6750 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
6754 /* Build a unary operation and gimplify it. Emit code before GSI.
6755 Return the gimple_val holding the result. */
6758 gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
6763 ret = fold_build1 (code, type, a);
6766 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
6772 /* Emit return warnings. */
6775 execute_warn_function_return (void)
6777 source_location location;
6782 /* If we have a path to EXIT, then we do return. */
6783 if (TREE_THIS_VOLATILE (cfun->decl)
6784 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
6786 location = UNKNOWN_LOCATION;
6787 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6789 last = last_stmt (e->src);
6790 if (gimple_code (last) == GIMPLE_RETURN
6791 && (location = gimple_location (last)) != UNKNOWN_LOCATION)
6794 if (location == UNKNOWN_LOCATION)
6795 location = cfun->function_end_locus;
6796 warning (0, "%H%<noreturn%> function does return", &location);
6799 /* If we see "return;" in some basic block, then we do reach the end
6800 without returning a value. */
6801 else if (warn_return_type
6802 && !TREE_NO_WARNING (cfun->decl)
6803 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
6804 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
6806 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6808 gimple last = last_stmt (e->src);
6809 if (gimple_code (last) == GIMPLE_RETURN
6810 && gimple_return_retval (last) == NULL
6811 && !gimple_no_warning_p (last))
6813 location = gimple_location (last);
6814 if (location == UNKNOWN_LOCATION)
6815 location = cfun->function_end_locus;
6816 warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
6817 TREE_NO_WARNING (cfun->decl) = 1;
6826 /* Given a basic block B which ends with a conditional and has
6827 precisely two successors, determine which of the edges is taken if
6828 the conditional is true and which is taken if the conditional is
6829 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
6832 extract_true_false_edges_from_block (basic_block b,
6836 edge e = EDGE_SUCC (b, 0);
6838 if (e->flags & EDGE_TRUE_VALUE)
6841 *false_edge = EDGE_SUCC (b, 1);
6846 *true_edge = EDGE_SUCC (b, 1);
6850 struct gimple_opt_pass pass_warn_function_return =
6856 execute_warn_function_return, /* execute */
6859 0, /* static_pass_number */
6861 PROP_cfg, /* properties_required */
6862 0, /* properties_provided */
6863 0, /* properties_destroyed */
6864 0, /* todo_flags_start */
6865 0 /* todo_flags_finish */
6869 /* Emit noreturn warnings. */
6872 execute_warn_function_noreturn (void)
6874 if (warn_missing_noreturn
6875 && !TREE_THIS_VOLATILE (cfun->decl)
6876 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
6877 && !lang_hooks.missing_noreturn_ok_p (cfun->decl))
6878 warning (OPT_Wmissing_noreturn, "%Jfunction might be possible candidate "
6879 "for attribute %<noreturn%>",
6884 struct gimple_opt_pass pass_warn_function_noreturn =
6890 execute_warn_function_noreturn, /* execute */
6893 0, /* static_pass_number */
6895 PROP_cfg, /* properties_required */
6896 0, /* properties_provided */
6897 0, /* properties_destroyed */
6898 0, /* todo_flags_start */
6899 0 /* todo_flags_finish */