1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
46 #include "cfglayout.h"
49 /* This file contains functions for building the Control Flow Graph (CFG)
50 for a function tree. */
52 /* Local declarations. */
54 /* Initial capacity for the basic block array. */
55 static const int initial_cfg_capacity = 20;
57 /* Mapping of labels to their associated blocks. This can greatly speed up
58 building of the CFG in code with lots of gotos. */
59 static GTY(()) varray_type label_to_block_map;
61 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
62 which use a particular edge. The CASE_LABEL_EXPRs are chained together
63 via their TREE_CHAIN field, which we clear after we're done with the
64 hash table to prevent problems with duplication of SWITCH_EXPRs.
66 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
67 update the case vector in response to edge redirections.
69 Right now this table is set up and torn down at key points in the
70 compilation process. It would be nice if we could make the table
71 more persistent. The key is getting notification of changes to
72 the CFG (particularly edge removal, creation and redirection). */
74 struct edge_to_cases_elt
76 /* The edge itself. Necessary for hashing and equality tests. */
79 /* The case labels associated with this edge. We link these up via
80 their TREE_CHAIN field, then we wipe out the TREE_CHAIN fields
81 when we destroy the hash table. This prevents problems when copying
86 static htab_t edge_to_cases;
91 long num_merged_labels;
94 static struct cfg_stats_d cfg_stats;
96 /* Nonzero if we found a computed goto while building basic blocks. */
97 static bool found_computed_goto;
99 /* Basic blocks and flowgraphs. */
100 static basic_block create_bb (void *, void *, basic_block);
101 static void create_block_annotation (basic_block);
102 static void free_blocks_annotations (void);
103 static void clear_blocks_annotations (void);
104 static void make_blocks (tree);
105 static void factor_computed_gotos (void);
108 static void make_edges (void);
109 static void make_ctrl_stmt_edges (basic_block);
110 static void make_exit_edges (basic_block);
111 static void make_cond_expr_edges (basic_block);
112 static void make_switch_expr_edges (basic_block);
113 static void make_goto_expr_edges (basic_block);
114 static edge tree_redirect_edge_and_branch (edge, basic_block);
115 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
116 static void split_critical_edges (void);
117 static bool remove_fallthru_edge (VEC(edge) *);
119 /* Various helpers. */
120 static inline bool stmt_starts_bb_p (tree, tree);
121 static int tree_verify_flow_info (void);
122 static void tree_make_forwarder_block (edge);
123 static bool tree_forwarder_block_p (basic_block, bool);
124 static void tree_cfg2vcg (FILE *);
126 /* Flowgraph optimization and cleanup. */
127 static void tree_merge_blocks (basic_block, basic_block);
128 static bool tree_can_merge_blocks_p (basic_block, basic_block);
129 static void remove_bb (basic_block);
130 static bool cleanup_control_flow (void);
131 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
132 static edge find_taken_edge_cond_expr (basic_block, tree);
133 static edge find_taken_edge_switch_expr (basic_block, tree);
134 static tree find_case_label_for_value (tree, tree);
135 static bool phi_alternatives_equal (basic_block, edge, edge);
136 static bool cleanup_forwarder_blocks (void);
139 /*---------------------------------------------------------------------------
141 ---------------------------------------------------------------------------*/
143 /* Entry point to the CFG builder for trees. TP points to the list of
144 statements to be added to the flowgraph. */
147 build_tree_cfg (tree *tp)
149 /* Register specific tree functions. */
150 tree_register_cfg_hooks ();
152 /* Initialize rbi_pool. */
155 /* Initialize the basic block array. */
157 profile_status = PROFILE_ABSENT;
159 last_basic_block = 0;
160 VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info");
161 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
163 /* Build a mapping of labels to their associated blocks. */
164 VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity,
165 "label to block map");
167 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
168 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
170 found_computed_goto = 0;
173 /* Computed gotos are hell to deal with, especially if there are
174 lots of them with a large number of destinations. So we factor
175 them to a common computed goto location before we build the
176 edge list. After we convert back to normal form, we will un-factor
177 the computed gotos since factoring introduces an unwanted jump. */
178 if (found_computed_goto)
179 factor_computed_gotos ();
181 /* Make sure there is always at least one block, even if it's empty. */
182 if (n_basic_blocks == 0)
183 create_empty_bb (ENTRY_BLOCK_PTR);
185 create_block_annotation (ENTRY_BLOCK_PTR);
186 create_block_annotation (EXIT_BLOCK_PTR);
188 /* Adjust the size of the array. */
189 VARRAY_GROW (basic_block_info, n_basic_blocks);
191 /* To speed up statement iterator walks, we first purge dead labels. */
192 cleanup_dead_labels ();
194 /* Group case nodes to reduce the number of edges.
195 We do this after cleaning up dead labels because otherwise we miss
196 a lot of obvious case merging opportunities. */
197 group_case_labels ();
199 /* Create the edges of the flowgraph. */
202 /* Debugging dumps. */
204 /* Write the flowgraph to a VCG file. */
206 int local_dump_flags;
207 FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags);
210 tree_cfg2vcg (dump_file);
211 dump_end (TDI_vcg, dump_file);
215 /* Dump a textual representation of the flowgraph. */
217 dump_tree_cfg (dump_file, dump_flags);
221 execute_build_cfg (void)
223 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
226 struct tree_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_flags_finish */
243 /* Search the CFG for any computed gotos. If found, factor them to a
244 common computed goto site. Also record the location of that site so
245 that we can un-factor the gotos after we have converted back to
249 factor_computed_gotos (void)
252 tree factored_label_decl = NULL;
254 tree factored_computed_goto_label = NULL;
255 tree factored_computed_goto = NULL;
257 /* We know there are one or more computed gotos in this function.
258 Examine the last statement in each basic block to see if the block
259 ends with a computed goto. */
263 block_stmt_iterator bsi = bsi_last (bb);
268 last = bsi_stmt (bsi);
270 /* Ignore the computed goto we create when we factor the original
272 if (last == factored_computed_goto)
275 /* If the last statement is a computed goto, factor it. */
276 if (computed_goto_p (last))
280 /* The first time we find a computed goto we need to create
281 the factored goto block and the variable each original
282 computed goto will use for their goto destination. */
283 if (! factored_computed_goto)
285 basic_block new_bb = create_empty_bb (bb);
286 block_stmt_iterator new_bsi = bsi_start (new_bb);
288 /* Create the destination of the factored goto. Each original
289 computed goto will put its desired destination into this
290 variable and jump to the label we create immediately
292 var = create_tmp_var (ptr_type_node, "gotovar");
294 /* Build a label for the new block which will contain the
295 factored computed goto. */
296 factored_label_decl = create_artificial_label ();
297 factored_computed_goto_label
298 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
299 bsi_insert_after (&new_bsi, factored_computed_goto_label,
302 /* Build our new computed goto. */
303 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
304 bsi_insert_after (&new_bsi, factored_computed_goto,
308 /* Copy the original computed goto's destination into VAR. */
309 assignment = build (MODIFY_EXPR, ptr_type_node,
310 var, GOTO_DESTINATION (last));
311 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
313 /* And re-vector the computed goto to the new destination. */
314 GOTO_DESTINATION (last) = factored_label_decl;
320 /* Create annotations for a single basic block. */
323 create_block_annotation (basic_block bb)
325 /* Verify that the tree_annotations field is clear. */
326 gcc_assert (!bb->tree_annotations);
327 bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d));
331 /* Free the annotations for all the basic blocks. */
333 static void free_blocks_annotations (void)
335 clear_blocks_annotations ();
339 /* Clear the annotations for all the basic blocks. */
342 clear_blocks_annotations (void)
346 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
347 bb->tree_annotations = NULL;
351 /* Build a flowgraph for the statement_list STMT_LIST. */
354 make_blocks (tree stmt_list)
356 tree_stmt_iterator i = tsi_start (stmt_list);
358 bool start_new_block = true;
359 bool first_stmt_of_list = true;
360 basic_block bb = ENTRY_BLOCK_PTR;
362 while (!tsi_end_p (i))
369 /* If the statement starts a new basic block or if we have determined
370 in a previous pass that we need to create a new block for STMT, do
372 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
374 if (!first_stmt_of_list)
375 stmt_list = tsi_split_statement_list_before (&i);
376 bb = create_basic_block (stmt_list, NULL, bb);
377 start_new_block = false;
380 /* Now add STMT to BB and create the subgraphs for special statement
382 set_bb_for_stmt (stmt, bb);
384 if (computed_goto_p (stmt))
385 found_computed_goto = true;
387 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
389 if (stmt_ends_bb_p (stmt))
390 start_new_block = true;
393 first_stmt_of_list = false;
398 /* Create and return a new empty basic block after bb AFTER. */
401 create_bb (void *h, void *e, basic_block after)
407 /* Create and initialize a new basic block. Since alloc_block uses
408 ggc_alloc_cleared to allocate a basic block, we do not have to
409 clear the newly allocated basic block here. */
412 bb->index = last_basic_block;
414 bb->stmt_list = h ? h : alloc_stmt_list ();
416 /* Add the new block to the linked list of blocks. */
417 link_block (bb, after);
419 /* Grow the basic block array if needed. */
420 if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info))
422 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
423 VARRAY_GROW (basic_block_info, new_size);
426 /* Add the newly created block to the array. */
427 BASIC_BLOCK (last_basic_block) = bb;
429 create_block_annotation (bb);
434 initialize_bb_rbi (bb);
439 /*---------------------------------------------------------------------------
441 ---------------------------------------------------------------------------*/
443 /* Fold COND_EXPR_COND of each COND_EXPR. */
446 fold_cond_expr_cond (void)
452 tree stmt = last_stmt (bb);
455 && TREE_CODE (stmt) == COND_EXPR)
457 tree cond = fold (COND_EXPR_COND (stmt));
458 if (integer_zerop (cond))
459 COND_EXPR_COND (stmt) = integer_zero_node;
460 else if (integer_onep (cond))
461 COND_EXPR_COND (stmt) = integer_one_node;
466 /* Join all the blocks in the flowgraph. */
473 /* Create an edge from entry to the first block with executable
475 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
477 /* Traverse the basic block array placing edges. */
480 tree first = first_stmt (bb);
481 tree last = last_stmt (bb);
485 /* Edges for statements that always alter flow control. */
486 if (is_ctrl_stmt (last))
487 make_ctrl_stmt_edges (bb);
489 /* Edges for statements that sometimes alter flow control. */
490 if (is_ctrl_altering_stmt (last))
491 make_exit_edges (bb);
494 /* Finally, if no edges were created above, this is a regular
495 basic block that only needs a fallthru edge. */
496 if (EDGE_COUNT (bb->succs) == 0)
497 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
500 /* We do not care about fake edges, so remove any that the CFG
501 builder inserted for completeness. */
502 remove_fake_exit_edges ();
504 /* Fold COND_EXPR_COND of each COND_EXPR. */
505 fold_cond_expr_cond ();
507 /* Clean up the graph and warn for unreachable code. */
512 /* Create edges for control statement at basic block BB. */
515 make_ctrl_stmt_edges (basic_block bb)
517 tree last = last_stmt (bb);
520 switch (TREE_CODE (last))
523 make_goto_expr_edges (bb);
527 make_edge (bb, EXIT_BLOCK_PTR, 0);
531 make_cond_expr_edges (bb);
535 make_switch_expr_edges (bb);
539 make_eh_edges (last);
540 /* Yet another NORETURN hack. */
541 if (EDGE_COUNT (bb->succs) == 0)
542 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
551 /* Create exit edges for statements in block BB that alter the flow of
552 control. Statements that alter the control flow are 'goto', 'return'
553 and calls to non-returning functions. */
556 make_exit_edges (basic_block bb)
558 tree last = last_stmt (bb), op;
561 switch (TREE_CODE (last))
564 /* If this function receives a nonlocal goto, then we need to
565 make edges from this call site to all the nonlocal goto
567 if (TREE_SIDE_EFFECTS (last)
568 && current_function_has_nonlocal_label)
569 make_goto_expr_edges (bb);
571 /* If this statement has reachable exception handlers, then
572 create abnormal edges to them. */
573 make_eh_edges (last);
575 /* Some calls are known not to return. For such calls we create
578 We really need to revamp how we build edges so that it's not
579 such a bloody pain to avoid creating edges for this case since
580 all we do is remove these edges when we're done building the
582 if (call_expr_flags (last) & ECF_NORETURN)
584 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
588 /* Don't forget the fall-thru edge. */
589 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
593 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
594 may have an abnormal edge. Search the RHS for this case and
595 create any required edges. */
596 op = get_call_expr_in (last);
597 if (op && TREE_SIDE_EFFECTS (op)
598 && current_function_has_nonlocal_label)
599 make_goto_expr_edges (bb);
601 make_eh_edges (last);
602 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
611 /* Create the edges for a COND_EXPR starting at block BB.
612 At this point, both clauses must contain only simple gotos. */
615 make_cond_expr_edges (basic_block bb)
617 tree entry = last_stmt (bb);
618 basic_block then_bb, else_bb;
619 tree then_label, else_label;
622 gcc_assert (TREE_CODE (entry) == COND_EXPR);
624 /* Entry basic blocks for each component. */
625 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
626 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
627 then_bb = label_to_block (then_label);
628 else_bb = label_to_block (else_label);
630 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
631 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
634 /* Hashing routine for EDGE_TO_CASES. */
637 edge_to_cases_hash (const void *p)
639 edge e = ((struct edge_to_cases_elt *)p)->e;
641 /* Hash on the edge itself (which is a pointer). */
642 return htab_hash_pointer (e);
645 /* Equality routine for EDGE_TO_CASES, edges are unique, so testing
646 for equality is just a pointer comparison. */
649 edge_to_cases_eq (const void *p1, const void *p2)
651 edge e1 = ((struct edge_to_cases_elt *)p1)->e;
652 edge e2 = ((struct edge_to_cases_elt *)p2)->e;
657 /* Called for each element in the hash table (P) as we delete the
658 edge to cases hash table.
660 Clear all the TREE_CHAINs to prevent problems with copying of
661 SWITCH_EXPRs and structure sharing rules, then free the hash table
665 edge_to_cases_cleanup (void *p)
667 struct edge_to_cases_elt *elt = p;
670 for (t = elt->case_labels; t; t = next)
672 next = TREE_CHAIN (t);
673 TREE_CHAIN (t) = NULL;
678 /* Start recording information mapping edges to case labels. */
681 start_recording_case_labels (void)
683 gcc_assert (edge_to_cases == NULL);
685 edge_to_cases = htab_create (37,
688 edge_to_cases_cleanup);
691 /* Return nonzero if we are recording information for case labels. */
694 recording_case_labels_p (void)
696 return (edge_to_cases != NULL);
699 /* Stop recording information mapping edges to case labels and
700 remove any information we have recorded. */
702 end_recording_case_labels (void)
704 htab_delete (edge_to_cases);
705 edge_to_cases = NULL;
708 /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */
711 record_switch_edge (edge e, tree case_label)
713 struct edge_to_cases_elt *elt;
716 /* Build a hash table element so we can see if E is already
718 elt = xmalloc (sizeof (struct edge_to_cases_elt));
720 elt->case_labels = case_label;
722 slot = htab_find_slot (edge_to_cases, elt, INSERT);
726 /* E was not in the hash table. Install E into the hash table. */
731 /* E was already in the hash table. Free ELT as we do not need it
735 /* Get the entry stored in the hash table. */
736 elt = (struct edge_to_cases_elt *) *slot;
738 /* Add it to the chain of CASE_LABEL_EXPRs referencing E. */
739 TREE_CHAIN (case_label) = elt->case_labels;
740 elt->case_labels = case_label;
744 /* If we are inside a {start,end}_recording_cases block, then return
745 a chain of CASE_LABEL_EXPRs from T which reference E.
747 Otherwise return NULL. */
750 get_cases_for_edge (edge e, tree t)
752 struct edge_to_cases_elt elt, *elt_p;
757 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
758 chains available. Return NULL so the caller can detect this case. */
759 if (!recording_case_labels_p ())
764 elt.case_labels = NULL;
765 slot = htab_find_slot (edge_to_cases, &elt, NO_INSERT);
769 elt_p = (struct edge_to_cases_elt *)*slot;
770 return elt_p->case_labels;
773 /* If we did not find E in the hash table, then this must be the first
774 time we have been queried for information about E & T. Add all the
775 elements from T to the hash table then perform the query again. */
777 vec = SWITCH_LABELS (t);
778 n = TREE_VEC_LENGTH (vec);
779 for (i = 0; i < n; i++)
781 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
782 basic_block label_bb = label_to_block (lab);
783 record_switch_edge (find_edge (e->src, label_bb), TREE_VEC_ELT (vec, i));
788 /* Create the edges for a SWITCH_EXPR starting at block BB.
789 At this point, the switch body has been lowered and the
790 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
793 make_switch_expr_edges (basic_block bb)
795 tree entry = last_stmt (bb);
799 vec = SWITCH_LABELS (entry);
800 n = TREE_VEC_LENGTH (vec);
802 for (i = 0; i < n; ++i)
804 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
805 basic_block label_bb = label_to_block (lab);
806 make_edge (bb, label_bb, 0);
811 /* Return the basic block holding label DEST. */
814 label_to_block (tree dest)
816 int uid = LABEL_DECL_UID (dest);
818 /* We would die hard when faced by an undefined label. Emit a label to
819 the very first basic block. This will hopefully make even the dataflow
820 and undefined variable warnings quite right. */
821 if ((errorcount || sorrycount) && uid < 0)
823 block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0));
826 stmt = build1 (LABEL_EXPR, void_type_node, dest);
827 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
828 uid = LABEL_DECL_UID (dest);
830 return VARRAY_BB (label_to_block_map, uid);
834 /* Create edges for a goto statement at block BB. */
837 make_goto_expr_edges (basic_block bb)
840 basic_block target_bb;
842 block_stmt_iterator last = bsi_last (bb);
844 goto_t = bsi_stmt (last);
846 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
847 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
848 from a nonlocal goto. */
849 if (TREE_CODE (goto_t) != GOTO_EXPR)
851 dest = error_mark_node;
856 dest = GOTO_DESTINATION (goto_t);
859 /* A GOTO to a local label creates normal edges. */
860 if (simple_goto_p (goto_t))
862 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
863 #ifdef USE_MAPPED_LOCATION
864 e->goto_locus = EXPR_LOCATION (goto_t);
866 e->goto_locus = EXPR_LOCUS (goto_t);
872 /* Nothing more to do for nonlocal gotos. */
873 if (TREE_CODE (dest) == LABEL_DECL)
876 /* Computed gotos remain. */
879 /* Look for the block starting with the destination label. In the
880 case of a computed goto, make an edge to any label block we find
882 FOR_EACH_BB (target_bb)
884 block_stmt_iterator bsi;
886 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
888 tree target = bsi_stmt (bsi);
890 if (TREE_CODE (target) != LABEL_EXPR)
894 /* Computed GOTOs. Make an edge to every label block that has
895 been marked as a potential target for a computed goto. */
896 (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0)
897 /* Nonlocal GOTO target. Make an edge to every label block
898 that has been marked as a potential target for a nonlocal
900 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1))
902 make_edge (bb, target_bb, EDGE_ABNORMAL);
908 /* Degenerate case of computed goto with no labels. */
909 if (!for_call && EDGE_COUNT (bb->succs) == 0)
910 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
914 /*---------------------------------------------------------------------------
916 ---------------------------------------------------------------------------*/
918 /* Remove unreachable blocks and other miscellaneous clean up work. */
921 cleanup_tree_cfg (void)
925 timevar_push (TV_TREE_CLEANUP_CFG);
927 retval = cleanup_control_flow ();
928 retval |= delete_unreachable_blocks ();
930 /* cleanup_forwarder_blocks can redirect edges out of SWITCH_EXPRs,
931 which can get expensive. So we want to enable recording of edge
932 to CASE_LABEL_EXPR mappings around the call to
933 cleanup_forwarder_blocks. */
934 start_recording_case_labels ();
935 retval |= cleanup_forwarder_blocks ();
936 end_recording_case_labels ();
938 #ifdef ENABLE_CHECKING
941 gcc_assert (!cleanup_control_flow ());
942 gcc_assert (!delete_unreachable_blocks ());
943 gcc_assert (!cleanup_forwarder_blocks ());
947 /* Merging the blocks creates no new opportunities for the other
948 optimizations, so do it here. */
949 retval |= merge_seq_blocks ();
953 #ifdef ENABLE_CHECKING
956 timevar_pop (TV_TREE_CLEANUP_CFG);
961 /* Cleanup useless labels in basic blocks. This is something we wish
962 to do early because it allows us to group case labels before creating
963 the edges for the CFG, and it speeds up block statement iterators in
965 We only run this pass once, running it more than once is probably not
968 /* A map from basic block index to the leading label of that block. */
969 static tree *label_for_bb;
971 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
973 update_eh_label (struct eh_region *region)
975 tree old_label = get_eh_region_tree_label (region);
979 basic_block bb = label_to_block (old_label);
981 /* ??? After optimizing, there may be EH regions with labels
982 that have already been removed from the function body, so
983 there is no basic block for them. */
987 new_label = label_for_bb[bb->index];
988 set_eh_region_tree_label (region, new_label);
992 /* Given LABEL return the first label in the same basic block. */
994 main_block_label (tree label)
996 basic_block bb = label_to_block (label);
998 /* label_to_block possibly inserted undefined label into the chain. */
999 if (!label_for_bb[bb->index])
1000 label_for_bb[bb->index] = label;
1001 return label_for_bb[bb->index];
1004 /* Cleanup redundant labels. This is a three-step process:
1005 1) Find the leading label for each block.
1006 2) Redirect all references to labels to the leading labels.
1007 3) Cleanup all useless labels. */
1010 cleanup_dead_labels (void)
1013 label_for_bb = xcalloc (last_basic_block, sizeof (tree));
1015 /* Find a suitable label for each block. We use the first user-defined
1016 label if there is one, or otherwise just the first label we see. */
1019 block_stmt_iterator i;
1021 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
1023 tree label, stmt = bsi_stmt (i);
1025 if (TREE_CODE (stmt) != LABEL_EXPR)
1028 label = LABEL_EXPR_LABEL (stmt);
1030 /* If we have not yet seen a label for the current block,
1031 remember this one and see if there are more labels. */
1032 if (! label_for_bb[bb->index])
1034 label_for_bb[bb->index] = label;
1038 /* If we did see a label for the current block already, but it
1039 is an artificially created label, replace it if the current
1040 label is a user defined label. */
1041 if (! DECL_ARTIFICIAL (label)
1042 && DECL_ARTIFICIAL (label_for_bb[bb->index]))
1044 label_for_bb[bb->index] = label;
1050 /* Now redirect all jumps/branches to the selected label.
1051 First do so for each block ending in a control statement. */
1054 tree stmt = last_stmt (bb);
1058 switch (TREE_CODE (stmt))
1062 tree true_branch, false_branch;
1064 true_branch = COND_EXPR_THEN (stmt);
1065 false_branch = COND_EXPR_ELSE (stmt);
1067 GOTO_DESTINATION (true_branch)
1068 = main_block_label (GOTO_DESTINATION (true_branch));
1069 GOTO_DESTINATION (false_branch)
1070 = main_block_label (GOTO_DESTINATION (false_branch));
1078 tree vec = SWITCH_LABELS (stmt);
1079 size_t n = TREE_VEC_LENGTH (vec);
1081 /* Replace all destination labels. */
1082 for (i = 0; i < n; ++i)
1084 tree elt = TREE_VEC_ELT (vec, i);
1085 tree label = main_block_label (CASE_LABEL (elt));
1086 CASE_LABEL (elt) = label;
1091 /* We have to handle GOTO_EXPRs until they're removed, and we don't
1092 remove them until after we've created the CFG edges. */
1094 if (! computed_goto_p (stmt))
1096 GOTO_DESTINATION (stmt)
1097 = main_block_label (GOTO_DESTINATION (stmt));
1106 for_each_eh_region (update_eh_label);
1108 /* Finally, purge dead labels. All user-defined labels and labels that
1109 can be the target of non-local gotos are preserved. */
1112 block_stmt_iterator i;
1113 tree label_for_this_bb = label_for_bb[bb->index];
1115 if (! label_for_this_bb)
1118 for (i = bsi_start (bb); !bsi_end_p (i); )
1120 tree label, stmt = bsi_stmt (i);
1122 if (TREE_CODE (stmt) != LABEL_EXPR)
1125 label = LABEL_EXPR_LABEL (stmt);
1127 if (label == label_for_this_bb
1128 || ! DECL_ARTIFICIAL (label)
1129 || DECL_NONLOCAL (label))
1136 free (label_for_bb);
1139 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1140 and scan the sorted vector of cases. Combine the ones jumping to the
1142 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1145 group_case_labels (void)
1151 tree stmt = last_stmt (bb);
1152 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1154 tree labels = SWITCH_LABELS (stmt);
1155 int old_size = TREE_VEC_LENGTH (labels);
1156 int i, j, new_size = old_size;
1157 tree default_case = TREE_VEC_ELT (labels, old_size - 1);
1160 /* The default label is always the last case in a switch
1161 statement after gimplification. */
1162 default_label = CASE_LABEL (default_case);
1164 /* Look for possible opportunities to merge cases.
1165 Ignore the last element of the label vector because it
1166 must be the default case. */
1168 while (i < old_size - 1)
1170 tree base_case, base_label, base_high, type;
1171 base_case = TREE_VEC_ELT (labels, i);
1173 gcc_assert (base_case);
1174 base_label = CASE_LABEL (base_case);
1176 /* Discard cases that have the same destination as the
1178 if (base_label == default_label)
1180 TREE_VEC_ELT (labels, i) = NULL_TREE;
1186 type = TREE_TYPE (CASE_LOW (base_case));
1187 base_high = CASE_HIGH (base_case) ?
1188 CASE_HIGH (base_case) : CASE_LOW (base_case);
1190 /* Try to merge case labels. Break out when we reach the end
1191 of the label vector or when we cannot merge the next case
1192 label with the current one. */
1193 while (i < old_size - 1)
1195 tree merge_case = TREE_VEC_ELT (labels, i);
1196 tree merge_label = CASE_LABEL (merge_case);
1197 tree t = int_const_binop (PLUS_EXPR, base_high,
1198 integer_one_node, 1);
1200 /* Merge the cases if they jump to the same place,
1201 and their ranges are consecutive. */
1202 if (merge_label == base_label
1203 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1205 base_high = CASE_HIGH (merge_case) ?
1206 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1207 CASE_HIGH (base_case) = base_high;
1208 TREE_VEC_ELT (labels, i) = NULL_TREE;
1217 /* Compress the case labels in the label vector, and adjust the
1218 length of the vector. */
1219 for (i = 0, j = 0; i < new_size; i++)
1221 while (! TREE_VEC_ELT (labels, j))
1223 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1225 TREE_VEC_LENGTH (labels) = new_size;
1230 /* Checks whether we can merge block B into block A. */
1233 tree_can_merge_blocks_p (basic_block a, basic_block b)
1236 block_stmt_iterator bsi;
1238 if (EDGE_COUNT (a->succs) != 1)
1241 if (EDGE_SUCC (a, 0)->flags & EDGE_ABNORMAL)
1244 if (EDGE_SUCC (a, 0)->dest != b)
1247 if (EDGE_COUNT (b->preds) > 1)
1250 if (b == EXIT_BLOCK_PTR)
1253 /* If A ends by a statement causing exceptions or something similar, we
1254 cannot merge the blocks. */
1255 stmt = last_stmt (a);
1256 if (stmt && stmt_ends_bb_p (stmt))
1259 /* Do not allow a block with only a non-local label to be merged. */
1260 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1261 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1264 /* There may be no phi nodes at the start of b. Most of these degenerate
1265 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1269 /* Do not remove user labels. */
1270 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1272 stmt = bsi_stmt (bsi);
1273 if (TREE_CODE (stmt) != LABEL_EXPR)
1275 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1283 /* Merge block B into block A. */
1286 tree_merge_blocks (basic_block a, basic_block b)
1288 block_stmt_iterator bsi;
1289 tree_stmt_iterator last;
1292 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1294 /* Ensure that B follows A. */
1295 move_block_after (b, a);
1297 gcc_assert (EDGE_SUCC (a, 0)->flags & EDGE_FALLTHRU);
1298 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1300 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1301 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1303 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1307 set_bb_for_stmt (bsi_stmt (bsi), a);
1312 /* Merge the chains. */
1313 last = tsi_last (a->stmt_list);
1314 tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT);
1315 b->stmt_list = NULL;
1319 /* Walk the function tree removing unnecessary statements.
1321 * Empty statement nodes are removed
1323 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1325 * Unnecessary COND_EXPRs are removed
1327 * Some unnecessary BIND_EXPRs are removed
1329 Clearly more work could be done. The trick is doing the analysis
1330 and removal fast enough to be a net improvement in compile times.
1332 Note that when we remove a control structure such as a COND_EXPR
1333 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1334 to ensure we eliminate all the useless code. */
1345 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1348 remove_useless_stmts_warn_notreached (tree stmt)
1350 if (EXPR_HAS_LOCATION (stmt))
1352 location_t loc = EXPR_LOCATION (stmt);
1353 if (LOCATION_LINE (loc) > 0)
1355 warning ("%Hwill never be executed", &loc);
1360 switch (TREE_CODE (stmt))
1362 case STATEMENT_LIST:
1364 tree_stmt_iterator i;
1365 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1366 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1372 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1374 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1376 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1380 case TRY_FINALLY_EXPR:
1381 case TRY_CATCH_EXPR:
1382 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1384 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1389 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1390 case EH_FILTER_EXPR:
1391 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1393 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1396 /* Not a live container. */
1404 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1406 tree then_clause, else_clause, cond;
1407 bool save_has_label, then_has_label, else_has_label;
1409 save_has_label = data->has_label;
1410 data->has_label = false;
1411 data->last_goto = NULL;
1413 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1415 then_has_label = data->has_label;
1416 data->has_label = false;
1417 data->last_goto = NULL;
1419 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1421 else_has_label = data->has_label;
1422 data->has_label = save_has_label | then_has_label | else_has_label;
1424 then_clause = COND_EXPR_THEN (*stmt_p);
1425 else_clause = COND_EXPR_ELSE (*stmt_p);
1426 cond = fold (COND_EXPR_COND (*stmt_p));
1428 /* If neither arm does anything at all, we can remove the whole IF. */
1429 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1431 *stmt_p = build_empty_stmt ();
1432 data->repeat = true;
1435 /* If there are no reachable statements in an arm, then we can
1436 zap the entire conditional. */
1437 else if (integer_nonzerop (cond) && !else_has_label)
1439 if (warn_notreached)
1440 remove_useless_stmts_warn_notreached (else_clause);
1441 *stmt_p = then_clause;
1442 data->repeat = true;
1444 else if (integer_zerop (cond) && !then_has_label)
1446 if (warn_notreached)
1447 remove_useless_stmts_warn_notreached (then_clause);
1448 *stmt_p = else_clause;
1449 data->repeat = true;
1452 /* Check a couple of simple things on then/else with single stmts. */
1455 tree then_stmt = expr_only (then_clause);
1456 tree else_stmt = expr_only (else_clause);
1458 /* Notice branches to a common destination. */
1459 if (then_stmt && else_stmt
1460 && TREE_CODE (then_stmt) == GOTO_EXPR
1461 && TREE_CODE (else_stmt) == GOTO_EXPR
1462 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1464 *stmt_p = then_stmt;
1465 data->repeat = true;
1468 /* If the THEN/ELSE clause merely assigns a value to a variable or
1469 parameter which is already known to contain that value, then
1470 remove the useless THEN/ELSE clause. */
1471 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1474 && TREE_CODE (else_stmt) == MODIFY_EXPR
1475 && TREE_OPERAND (else_stmt, 0) == cond
1476 && integer_zerop (TREE_OPERAND (else_stmt, 1)))
1477 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1479 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1480 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1481 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1482 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1484 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1485 ? then_stmt : else_stmt);
1486 tree *location = (TREE_CODE (cond) == EQ_EXPR
1487 ? &COND_EXPR_THEN (*stmt_p)
1488 : &COND_EXPR_ELSE (*stmt_p));
1491 && TREE_CODE (stmt) == MODIFY_EXPR
1492 && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1493 && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1494 *location = alloc_stmt_list ();
1498 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1499 would be re-introduced during lowering. */
1500 data->last_goto = NULL;
1505 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1507 bool save_may_branch, save_may_throw;
1508 bool this_may_branch, this_may_throw;
1510 /* Collect may_branch and may_throw information for the body only. */
1511 save_may_branch = data->may_branch;
1512 save_may_throw = data->may_throw;
1513 data->may_branch = false;
1514 data->may_throw = false;
1515 data->last_goto = NULL;
1517 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1519 this_may_branch = data->may_branch;
1520 this_may_throw = data->may_throw;
1521 data->may_branch |= save_may_branch;
1522 data->may_throw |= save_may_throw;
1523 data->last_goto = NULL;
1525 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1527 /* If the body is empty, then we can emit the FINALLY block without
1528 the enclosing TRY_FINALLY_EXPR. */
1529 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1531 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1532 data->repeat = true;
1535 /* If the handler is empty, then we can emit the TRY block without
1536 the enclosing TRY_FINALLY_EXPR. */
1537 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1539 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1540 data->repeat = true;
1543 /* If the body neither throws, nor branches, then we can safely
1544 string the TRY and FINALLY blocks together. */
1545 else if (!this_may_branch && !this_may_throw)
1547 tree stmt = *stmt_p;
1548 *stmt_p = TREE_OPERAND (stmt, 0);
1549 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1550 data->repeat = true;
1556 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1558 bool save_may_throw, this_may_throw;
1559 tree_stmt_iterator i;
1562 /* Collect may_throw information for the body only. */
1563 save_may_throw = data->may_throw;
1564 data->may_throw = false;
1565 data->last_goto = NULL;
1567 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1569 this_may_throw = data->may_throw;
1570 data->may_throw = save_may_throw;
1572 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1573 if (!this_may_throw)
1575 if (warn_notreached)
1576 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1577 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1578 data->repeat = true;
1582 /* Process the catch clause specially. We may be able to tell that
1583 no exceptions propagate past this point. */
1585 this_may_throw = true;
1586 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1587 stmt = tsi_stmt (i);
1588 data->last_goto = NULL;
1590 switch (TREE_CODE (stmt))
1593 for (; !tsi_end_p (i); tsi_next (&i))
1595 stmt = tsi_stmt (i);
1596 /* If we catch all exceptions, then the body does not
1597 propagate exceptions past this point. */
1598 if (CATCH_TYPES (stmt) == NULL)
1599 this_may_throw = false;
1600 data->last_goto = NULL;
1601 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1605 case EH_FILTER_EXPR:
1606 if (EH_FILTER_MUST_NOT_THROW (stmt))
1607 this_may_throw = false;
1608 else if (EH_FILTER_TYPES (stmt) == NULL)
1609 this_may_throw = false;
1610 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1614 /* Otherwise this is a cleanup. */
1615 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1617 /* If the cleanup is empty, then we can emit the TRY block without
1618 the enclosing TRY_CATCH_EXPR. */
1619 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1621 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1622 data->repeat = true;
1626 data->may_throw |= this_may_throw;
1631 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1635 /* First remove anything underneath the BIND_EXPR. */
1636 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1638 /* If the BIND_EXPR has no variables, then we can pull everything
1639 up one level and remove the BIND_EXPR, unless this is the toplevel
1640 BIND_EXPR for the current function or an inlined function.
1642 When this situation occurs we will want to apply this
1643 optimization again. */
1644 block = BIND_EXPR_BLOCK (*stmt_p);
1645 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1646 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1648 || ! BLOCK_ABSTRACT_ORIGIN (block)
1649 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1652 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1653 data->repeat = true;
1659 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1661 tree dest = GOTO_DESTINATION (*stmt_p);
1663 data->may_branch = true;
1664 data->last_goto = NULL;
1666 /* Record the last goto expr, so that we can delete it if unnecessary. */
1667 if (TREE_CODE (dest) == LABEL_DECL)
1668 data->last_goto = stmt_p;
1673 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1675 tree label = LABEL_EXPR_LABEL (*stmt_p);
1677 data->has_label = true;
1679 /* We do want to jump across non-local label receiver code. */
1680 if (DECL_NONLOCAL (label))
1681 data->last_goto = NULL;
1683 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1685 *data->last_goto = build_empty_stmt ();
1686 data->repeat = true;
1689 /* ??? Add something here to delete unused labels. */
1693 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1694 decl. This allows us to eliminate redundant or useless
1695 calls to "const" functions.
1697 Gimplifier already does the same operation, but we may notice functions
1698 being const and pure once their calls has been gimplified, so we need
1699 to update the flag. */
1702 update_call_expr_flags (tree call)
1704 tree decl = get_callee_fndecl (call);
1707 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1708 TREE_SIDE_EFFECTS (call) = 0;
1709 if (TREE_NOTHROW (decl))
1710 TREE_NOTHROW (call) = 1;
1714 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1717 notice_special_calls (tree t)
1719 int flags = call_expr_flags (t);
1721 if (flags & ECF_MAY_BE_ALLOCA)
1722 current_function_calls_alloca = true;
1723 if (flags & ECF_RETURNS_TWICE)
1724 current_function_calls_setjmp = true;
1728 /* Clear flags set by notice_special_calls. Used by dead code removal
1729 to update the flags. */
1732 clear_special_calls (void)
1734 current_function_calls_alloca = false;
1735 current_function_calls_setjmp = false;
1740 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1744 switch (TREE_CODE (t))
1747 remove_useless_stmts_cond (tp, data);
1750 case TRY_FINALLY_EXPR:
1751 remove_useless_stmts_tf (tp, data);
1754 case TRY_CATCH_EXPR:
1755 remove_useless_stmts_tc (tp, data);
1759 remove_useless_stmts_bind (tp, data);
1763 remove_useless_stmts_goto (tp, data);
1767 remove_useless_stmts_label (tp, data);
1772 data->last_goto = NULL;
1773 data->may_branch = true;
1778 data->last_goto = NULL;
1779 notice_special_calls (t);
1780 update_call_expr_flags (t);
1781 if (tree_could_throw_p (t))
1782 data->may_throw = true;
1786 data->last_goto = NULL;
1788 op = get_call_expr_in (t);
1791 update_call_expr_flags (op);
1792 notice_special_calls (op);
1794 if (tree_could_throw_p (t))
1795 data->may_throw = true;
1798 case STATEMENT_LIST:
1800 tree_stmt_iterator i = tsi_start (t);
1801 while (!tsi_end_p (i))
1804 if (IS_EMPTY_STMT (t))
1810 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1813 if (TREE_CODE (t) == STATEMENT_LIST)
1815 tsi_link_before (&i, t, TSI_SAME_STMT);
1825 data->last_goto = NULL;
1829 data->last_goto = NULL;
1835 remove_useless_stmts (void)
1837 struct rus_data data;
1839 clear_special_calls ();
1843 memset (&data, 0, sizeof (data));
1844 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1846 while (data.repeat);
1850 struct tree_opt_pass pass_remove_useless_stmts =
1852 "useless", /* name */
1854 remove_useless_stmts, /* execute */
1857 0, /* static_pass_number */
1859 PROP_gimple_any, /* properties_required */
1860 0, /* properties_provided */
1861 0, /* properties_destroyed */
1862 0, /* todo_flags_start */
1863 TODO_dump_func, /* todo_flags_finish */
1868 /* Remove obviously useless statements in basic block BB. */
1871 cfg_remove_useless_stmts_bb (basic_block bb)
1873 block_stmt_iterator bsi;
1874 tree stmt = NULL_TREE;
1875 tree cond, var = NULL_TREE, val = NULL_TREE;
1876 struct var_ann_d *ann;
1878 /* Check whether we come here from a condition, and if so, get the
1880 if (EDGE_COUNT (bb->preds) != 1
1881 || !(EDGE_PRED (bb, 0)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1884 cond = COND_EXPR_COND (last_stmt (EDGE_PRED (bb, 0)->src));
1886 if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1889 val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
1890 ? boolean_false_node : boolean_true_node);
1892 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR
1893 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1894 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL))
1896 var = TREE_OPERAND (cond, 0);
1897 val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
1898 ? boolean_true_node : boolean_false_node);
1902 if (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE)
1903 cond = invert_truthvalue (cond);
1904 if (TREE_CODE (cond) == EQ_EXPR
1905 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1906 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1907 && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL
1908 || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL
1909 || TREE_CONSTANT (TREE_OPERAND (cond, 1))))
1911 var = TREE_OPERAND (cond, 0);
1912 val = TREE_OPERAND (cond, 1);
1918 /* Only work for normal local variables. */
1919 ann = var_ann (var);
1922 || TREE_ADDRESSABLE (var))
1925 if (! TREE_CONSTANT (val))
1927 ann = var_ann (val);
1930 || TREE_ADDRESSABLE (val))
1934 /* Ignore floating point variables, since comparison behaves weird for
1936 if (FLOAT_TYPE_P (TREE_TYPE (var)))
1939 for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
1941 stmt = bsi_stmt (bsi);
1943 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1944 which is already known to contain that value, then remove the useless
1945 THEN/ELSE clause. */
1946 if (TREE_CODE (stmt) == MODIFY_EXPR
1947 && TREE_OPERAND (stmt, 0) == var
1948 && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0))
1954 /* Invalidate the var if we encounter something that could modify it.
1955 Likewise for the value it was previously set to. Note that we only
1956 consider values that are either a VAR_DECL or PARM_DECL so we
1957 can test for conflict very simply. */
1958 if (TREE_CODE (stmt) == ASM_EXPR
1959 || (TREE_CODE (stmt) == MODIFY_EXPR
1960 && (TREE_OPERAND (stmt, 0) == var
1961 || TREE_OPERAND (stmt, 0) == val)))
1969 /* A CFG-aware version of remove_useless_stmts. */
1972 cfg_remove_useless_stmts (void)
1976 #ifdef ENABLE_CHECKING
1977 verify_flow_info ();
1982 cfg_remove_useless_stmts_bb (bb);
1987 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1990 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1994 /* Since this block is no longer reachable, we can just delete all
1995 of its PHI nodes. */
1996 phi = phi_nodes (bb);
1999 tree next = PHI_CHAIN (phi);
2000 remove_phi_node (phi, NULL_TREE, bb);
2004 /* Remove edges to BB's successors. */
2005 while (EDGE_COUNT (bb->succs) > 0)
2006 remove_edge (EDGE_SUCC (bb, 0));
2010 /* Remove statements of basic block BB. */
2013 remove_bb (basic_block bb)
2015 block_stmt_iterator i;
2016 source_locus loc = 0;
2020 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2021 if (dump_flags & TDF_DETAILS)
2023 dump_bb (bb, dump_file, 0);
2024 fprintf (dump_file, "\n");
2028 /* Remove all the instructions in the block. */
2029 for (i = bsi_start (bb); !bsi_end_p (i);)
2031 tree stmt = bsi_stmt (i);
2032 if (TREE_CODE (stmt) == LABEL_EXPR
2033 && FORCED_LABEL (LABEL_EXPR_LABEL (stmt)))
2035 basic_block new_bb = bb->prev_bb;
2036 block_stmt_iterator new_bsi = bsi_start (new_bb);
2039 bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
2043 release_defs (stmt);
2045 set_bb_for_stmt (stmt, NULL);
2049 /* Don't warn for removed gotos. Gotos are often removed due to
2050 jump threading, thus resulting in bogus warnings. Not great,
2051 since this way we lose warnings for gotos in the original
2052 program that are indeed unreachable. */
2053 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
2057 #ifdef USE_MAPPED_LOCATION
2058 t = EXPR_LOCATION (stmt);
2060 t = EXPR_LOCUS (stmt);
2062 if (t && LOCATION_LINE (*t) > 0)
2067 /* If requested, give a warning that the first statement in the
2068 block is unreachable. We walk statements backwards in the
2069 loop above, so the last statement we process is the first statement
2071 if (warn_notreached && loc)
2072 #ifdef USE_MAPPED_LOCATION
2073 warning ("%Hwill never be executed", &loc);
2075 warning ("%Hwill never be executed", loc);
2078 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2081 /* A list of all the noreturn calls passed to modify_stmt.
2082 cleanup_control_flow uses it to detect cases where a mid-block
2083 indirect call has been turned into a noreturn call. When this
2084 happens, all the instructions after the call are no longer
2085 reachable and must be deleted as dead. */
2087 VEC(tree) *modified_noreturn_calls;
2089 /* Try to remove superfluous control structures. */
2092 cleanup_control_flow (void)
2095 block_stmt_iterator bsi;
2096 bool retval = false;
2099 /* Detect cases where a mid-block call is now known not to return. */
2100 while (VEC_length (tree, modified_noreturn_calls))
2102 stmt = VEC_pop (tree, modified_noreturn_calls);
2103 bb = bb_for_stmt (stmt);
2104 if (bb != NULL && last_stmt (bb) != stmt && noreturn_call_p (stmt))
2105 split_block (bb, stmt);
2110 bsi = bsi_last (bb);
2112 if (bsi_end_p (bsi))
2115 stmt = bsi_stmt (bsi);
2116 if (TREE_CODE (stmt) == COND_EXPR
2117 || TREE_CODE (stmt) == SWITCH_EXPR)
2118 retval |= cleanup_control_expr_graph (bb, bsi);
2120 /* Check for indirect calls that have been turned into
2122 if (noreturn_call_p (stmt) && remove_fallthru_edge (bb->succs))
2124 free_dominance_info (CDI_DOMINATORS);
2132 /* Disconnect an unreachable block in the control expression starting
2136 cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
2139 bool retval = false;
2140 tree expr = bsi_stmt (bsi), val;
2142 if (EDGE_COUNT (bb->succs) > 1)
2147 switch (TREE_CODE (expr))
2150 val = COND_EXPR_COND (expr);
2154 val = SWITCH_COND (expr);
2155 if (TREE_CODE (val) != INTEGER_CST)
2163 taken_edge = find_taken_edge (bb, val);
2167 /* Remove all the edges except the one that is always executed. */
2168 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2170 if (e != taken_edge)
2172 taken_edge->probability += e->probability;
2173 taken_edge->count += e->count;
2180 if (taken_edge->probability > REG_BR_PROB_BASE)
2181 taken_edge->probability = REG_BR_PROB_BASE;
2184 taken_edge = EDGE_SUCC (bb, 0);
2187 taken_edge->flags = EDGE_FALLTHRU;
2189 /* We removed some paths from the cfg. */
2190 free_dominance_info (CDI_DOMINATORS);
2195 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
2198 remove_fallthru_edge (VEC(edge) *ev)
2203 FOR_EACH_EDGE (e, ei, ev)
2204 if ((e->flags & EDGE_FALLTHRU) != 0)
2212 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2213 predicate VAL, return the edge that will be taken out of the block.
2214 If VAL does not match a unique edge, NULL is returned. */
2217 find_taken_edge (basic_block bb, tree val)
2221 stmt = last_stmt (bb);
2224 gcc_assert (is_ctrl_stmt (stmt));
2227 if (TREE_CODE (val) != INTEGER_CST)
2230 if (TREE_CODE (stmt) == COND_EXPR)
2231 return find_taken_edge_cond_expr (bb, val);
2233 if (TREE_CODE (stmt) == SWITCH_EXPR)
2234 return find_taken_edge_switch_expr (bb, val);
2240 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2241 statement, determine which of the two edges will be taken out of the
2242 block. Return NULL if either edge may be taken. */
2245 find_taken_edge_cond_expr (basic_block bb, tree val)
2247 edge true_edge, false_edge;
2249 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2251 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2252 return (zero_p (val) ? false_edge : true_edge);
2255 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2256 statement, determine which edge will be taken out of the block. Return
2257 NULL if any edge may be taken. */
2260 find_taken_edge_switch_expr (basic_block bb, tree val)
2262 tree switch_expr, taken_case;
2263 basic_block dest_bb;
2266 switch_expr = last_stmt (bb);
2267 taken_case = find_case_label_for_value (switch_expr, val);
2268 dest_bb = label_to_block (CASE_LABEL (taken_case));
2270 e = find_edge (bb, dest_bb);
2276 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2277 We can make optimal use here of the fact that the case labels are
2278 sorted: We can do a binary search for a case matching VAL. */
2281 find_case_label_for_value (tree switch_expr, tree val)
2283 tree vec = SWITCH_LABELS (switch_expr);
2284 size_t low, high, n = TREE_VEC_LENGTH (vec);
2285 tree default_case = TREE_VEC_ELT (vec, n - 1);
2287 for (low = -1, high = n - 1; high - low > 1; )
2289 size_t i = (high + low) / 2;
2290 tree t = TREE_VEC_ELT (vec, i);
2293 /* Cache the result of comparing CASE_LOW and val. */
2294 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2301 if (CASE_HIGH (t) == NULL)
2303 /* A singe-valued case label. */
2309 /* A case range. We can only handle integer ranges. */
2310 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2315 return default_case;
2319 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2320 those alternatives are equal in each of the PHI nodes, then return
2321 true, else return false. */
2324 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
2326 int n1 = e1->dest_idx;
2327 int n2 = e2->dest_idx;
2330 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2332 tree val1 = PHI_ARG_DEF (phi, n1);
2333 tree val2 = PHI_ARG_DEF (phi, n2);
2335 gcc_assert (val1 != NULL_TREE);
2336 gcc_assert (val2 != NULL_TREE);
2338 if (!operand_equal_for_phi_arg_p (val1, val2))
2346 /*---------------------------------------------------------------------------
2348 ---------------------------------------------------------------------------*/
2350 /* Dump tree-specific information of block BB to file OUTF. */
2353 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2355 dump_generic_bb (outf, bb, indent, TDF_VOPS);
2359 /* Dump a basic block on stderr. */
2362 debug_tree_bb (basic_block bb)
2364 dump_bb (bb, stderr, 0);
2368 /* Dump basic block with index N on stderr. */
2371 debug_tree_bb_n (int n)
2373 debug_tree_bb (BASIC_BLOCK (n));
2374 return BASIC_BLOCK (n);
2378 /* Dump the CFG on stderr.
2380 FLAGS are the same used by the tree dumping functions
2381 (see TDF_* in tree.h). */
2384 debug_tree_cfg (int flags)
2386 dump_tree_cfg (stderr, flags);
2390 /* Dump the program showing basic block boundaries on the given FILE.
2392 FLAGS are the same used by the tree dumping functions (see TDF_* in
2396 dump_tree_cfg (FILE *file, int flags)
2398 if (flags & TDF_DETAILS)
2400 const char *funcname
2401 = lang_hooks.decl_printable_name (current_function_decl, 2);
2404 fprintf (file, ";; Function %s\n\n", funcname);
2405 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2406 n_basic_blocks, n_edges, last_basic_block);
2408 brief_dump_cfg (file);
2409 fprintf (file, "\n");
2412 if (flags & TDF_STATS)
2413 dump_cfg_stats (file);
2415 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2419 /* Dump CFG statistics on FILE. */
2422 dump_cfg_stats (FILE *file)
2424 static long max_num_merged_labels = 0;
2425 unsigned long size, total = 0;
2428 const char * const fmt_str = "%-30s%-13s%12s\n";
2429 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2430 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2431 const char *funcname
2432 = lang_hooks.decl_printable_name (current_function_decl, 2);
2435 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2437 fprintf (file, "---------------------------------------------------------\n");
2438 fprintf (file, fmt_str, "", " Number of ", "Memory");
2439 fprintf (file, fmt_str, "", " instances ", "used ");
2440 fprintf (file, "---------------------------------------------------------\n");
2442 size = n_basic_blocks * sizeof (struct basic_block_def);
2444 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2445 SCALE (size), LABEL (size));
2449 n_edges += EDGE_COUNT (bb->succs);
2450 size = n_edges * sizeof (struct edge_def);
2452 fprintf (file, fmt_str_1, "Edges", n_edges, SCALE (size), LABEL (size));
2454 size = n_basic_blocks * sizeof (struct bb_ann_d);
2456 fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks,
2457 SCALE (size), LABEL (size));
2459 fprintf (file, "---------------------------------------------------------\n");
2460 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2462 fprintf (file, "---------------------------------------------------------\n");
2463 fprintf (file, "\n");
2465 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2466 max_num_merged_labels = cfg_stats.num_merged_labels;
2468 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2469 cfg_stats.num_merged_labels, max_num_merged_labels);
2471 fprintf (file, "\n");
2475 /* Dump CFG statistics on stderr. Keep extern so that it's always
2476 linked in the final executable. */
2479 debug_cfg_stats (void)
2481 dump_cfg_stats (stderr);
2485 /* Dump the flowgraph to a .vcg FILE. */
2488 tree_cfg2vcg (FILE *file)
2493 const char *funcname
2494 = lang_hooks.decl_printable_name (current_function_decl, 2);
2496 /* Write the file header. */
2497 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2498 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2499 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2501 /* Write blocks and edges. */
2502 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2504 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2507 if (e->flags & EDGE_FAKE)
2508 fprintf (file, " linestyle: dotted priority: 10");
2510 fprintf (file, " linestyle: solid priority: 100");
2512 fprintf (file, " }\n");
2518 enum tree_code head_code, end_code;
2519 const char *head_name, *end_name;
2522 tree first = first_stmt (bb);
2523 tree last = last_stmt (bb);
2527 head_code = TREE_CODE (first);
2528 head_name = tree_code_name[head_code];
2529 head_line = get_lineno (first);
2532 head_name = "no-statement";
2536 end_code = TREE_CODE (last);
2537 end_name = tree_code_name[end_code];
2538 end_line = get_lineno (last);
2541 end_name = "no-statement";
2543 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2544 bb->index, bb->index, head_name, head_line, end_name,
2547 FOR_EACH_EDGE (e, ei, bb->succs)
2549 if (e->dest == EXIT_BLOCK_PTR)
2550 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2552 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2554 if (e->flags & EDGE_FAKE)
2555 fprintf (file, " priority: 10 linestyle: dotted");
2557 fprintf (file, " priority: 100 linestyle: solid");
2559 fprintf (file, " }\n");
2562 if (bb->next_bb != EXIT_BLOCK_PTR)
2566 fputs ("}\n\n", file);
2571 /*---------------------------------------------------------------------------
2572 Miscellaneous helpers
2573 ---------------------------------------------------------------------------*/
2575 /* Return true if T represents a stmt that always transfers control. */
2578 is_ctrl_stmt (tree t)
2580 return (TREE_CODE (t) == COND_EXPR
2581 || TREE_CODE (t) == SWITCH_EXPR
2582 || TREE_CODE (t) == GOTO_EXPR
2583 || TREE_CODE (t) == RETURN_EXPR
2584 || TREE_CODE (t) == RESX_EXPR);
2588 /* Return true if T is a statement that may alter the flow of control
2589 (e.g., a call to a non-returning function). */
2592 is_ctrl_altering_stmt (tree t)
2597 call = get_call_expr_in (t);
2600 /* A non-pure/const CALL_EXPR alters flow control if the current
2601 function has nonlocal labels. */
2602 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2605 /* A CALL_EXPR also alters control flow if it does not return. */
2606 if (call_expr_flags (call) & ECF_NORETURN)
2610 /* If a statement can throw, it alters control flow. */
2611 return tree_can_throw_internal (t);
2615 /* Return true if T is a computed goto. */
2618 computed_goto_p (tree t)
2620 return (TREE_CODE (t) == GOTO_EXPR
2621 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2625 /* Checks whether EXPR is a simple local goto. */
2628 simple_goto_p (tree expr)
2630 return (TREE_CODE (expr) == GOTO_EXPR
2631 && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL);
2635 /* Return true if T should start a new basic block. PREV_T is the
2636 statement preceding T. It is used when T is a label or a case label.
2637 Labels should only start a new basic block if their previous statement
2638 wasn't a label. Otherwise, sequence of labels would generate
2639 unnecessary basic blocks that only contain a single label. */
2642 stmt_starts_bb_p (tree t, tree prev_t)
2644 enum tree_code code;
2649 /* LABEL_EXPRs start a new basic block only if the preceding
2650 statement wasn't a label of the same type. This prevents the
2651 creation of consecutive blocks that have nothing but a single
2653 code = TREE_CODE (t);
2654 if (code == LABEL_EXPR)
2656 /* Nonlocal and computed GOTO targets always start a new block. */
2657 if (code == LABEL_EXPR
2658 && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2659 || FORCED_LABEL (LABEL_EXPR_LABEL (t))))
2662 if (prev_t && TREE_CODE (prev_t) == code)
2664 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2667 cfg_stats.num_merged_labels++;
2678 /* Return true if T should end a basic block. */
2681 stmt_ends_bb_p (tree t)
2683 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2687 /* Add gotos that used to be represented implicitly in the CFG. */
2690 disband_implicit_edges (void)
2693 block_stmt_iterator last;
2700 last = bsi_last (bb);
2701 stmt = last_stmt (bb);
2703 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2705 /* Remove superfluous gotos from COND_EXPR branches. Moved
2706 from cfg_remove_useless_stmts here since it violates the
2707 invariants for tree--cfg correspondence and thus fits better
2708 here where we do it anyway. */
2709 e = find_edge (bb, bb->next_bb);
2712 if (e->flags & EDGE_TRUE_VALUE)
2713 COND_EXPR_THEN (stmt) = build_empty_stmt ();
2714 else if (e->flags & EDGE_FALSE_VALUE)
2715 COND_EXPR_ELSE (stmt) = build_empty_stmt ();
2718 e->flags |= EDGE_FALLTHRU;
2724 if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
2726 /* Remove the RETURN_EXPR if we may fall though to the exit
2728 gcc_assert (EDGE_COUNT (bb->succs) == 1);
2729 gcc_assert (EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR);
2731 if (bb->next_bb == EXIT_BLOCK_PTR
2732 && !TREE_OPERAND (stmt, 0))
2735 EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
2740 /* There can be no fallthru edge if the last statement is a control
2742 if (stmt && is_ctrl_stmt (stmt))
2745 /* Find a fallthru edge and emit the goto if necessary. */
2746 FOR_EACH_EDGE (e, ei, bb->succs)
2747 if (e->flags & EDGE_FALLTHRU)
2750 if (!e || e->dest == bb->next_bb)
2753 gcc_assert (e->dest != EXIT_BLOCK_PTR);
2754 label = tree_block_label (e->dest);
2756 stmt = build1 (GOTO_EXPR, void_type_node, label);
2757 #ifdef USE_MAPPED_LOCATION
2758 SET_EXPR_LOCATION (stmt, e->goto_locus);
2760 SET_EXPR_LOCUS (stmt, e->goto_locus);
2762 bsi_insert_after (&last, stmt, BSI_NEW_STMT);
2763 e->flags &= ~EDGE_FALLTHRU;
2767 /* Remove block annotations and other datastructures. */
2770 delete_tree_cfg_annotations (void)
2773 if (n_basic_blocks > 0)
2774 free_blocks_annotations ();
2776 label_to_block_map = NULL;
2783 /* Return the first statement in basic block BB. */
2786 first_stmt (basic_block bb)
2788 block_stmt_iterator i = bsi_start (bb);
2789 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2793 /* Return the last statement in basic block BB. */
2796 last_stmt (basic_block bb)
2798 block_stmt_iterator b = bsi_last (bb);
2799 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2803 /* Return a pointer to the last statement in block BB. */
2806 last_stmt_ptr (basic_block bb)
2808 block_stmt_iterator last = bsi_last (bb);
2809 return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL;
2813 /* Return the last statement of an otherwise empty block. Return NULL
2814 if the block is totally empty, or if it contains more than one
2818 last_and_only_stmt (basic_block bb)
2820 block_stmt_iterator i = bsi_last (bb);
2826 last = bsi_stmt (i);
2831 /* Empty statements should no longer appear in the instruction stream.
2832 Everything that might have appeared before should be deleted by
2833 remove_useless_stmts, and the optimizers should just bsi_remove
2834 instead of smashing with build_empty_stmt.
2836 Thus the only thing that should appear here in a block containing
2837 one executable statement is a label. */
2838 prev = bsi_stmt (i);
2839 if (TREE_CODE (prev) == LABEL_EXPR)
2846 /* Mark BB as the basic block holding statement T. */
2849 set_bb_for_stmt (tree t, basic_block bb)
2851 if (TREE_CODE (t) == PHI_NODE)
2853 else if (TREE_CODE (t) == STATEMENT_LIST)
2855 tree_stmt_iterator i;
2856 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2857 set_bb_for_stmt (tsi_stmt (i), bb);
2861 stmt_ann_t ann = get_stmt_ann (t);
2864 /* If the statement is a label, add the label to block-to-labels map
2865 so that we can speed up edge creation for GOTO_EXPRs. */
2866 if (TREE_CODE (t) == LABEL_EXPR)
2870 t = LABEL_EXPR_LABEL (t);
2871 uid = LABEL_DECL_UID (t);
2874 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2875 if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid)
2876 VARRAY_GROW (label_to_block_map, 3 * uid / 2);
2879 /* We're moving an existing label. Make sure that we've
2880 removed it from the old block. */
2881 gcc_assert (!bb || !VARRAY_BB (label_to_block_map, uid));
2882 VARRAY_BB (label_to_block_map, uid) = bb;
2887 /* Finds iterator for STMT. */
2889 extern block_stmt_iterator
2890 bsi_for_stmt (tree stmt)
2892 block_stmt_iterator bsi;
2894 for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
2895 if (bsi_stmt (bsi) == stmt)
2901 /* Insert statement (or statement list) T before the statement
2902 pointed-to by iterator I. M specifies how to update iterator I
2903 after insertion (see enum bsi_iterator_update). */
2906 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2908 set_bb_for_stmt (t, i->bb);
2909 tsi_link_before (&i->tsi, t, m);
2914 /* Insert statement (or statement list) T after the statement
2915 pointed-to by iterator I. M specifies how to update iterator I
2916 after insertion (see enum bsi_iterator_update). */
2919 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2921 set_bb_for_stmt (t, i->bb);
2922 tsi_link_after (&i->tsi, t, m);
2927 /* Remove the statement pointed to by iterator I. The iterator is updated
2928 to the next statement. */
2931 bsi_remove (block_stmt_iterator *i)
2933 tree t = bsi_stmt (*i);
2934 set_bb_for_stmt (t, NULL);
2935 tsi_delink (&i->tsi);
2939 /* Move the statement at FROM so it comes right after the statement at TO. */
2942 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
2944 tree stmt = bsi_stmt (*from);
2946 bsi_insert_after (to, stmt, BSI_SAME_STMT);
2950 /* Move the statement at FROM so it comes right before the statement at TO. */
2953 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
2955 tree stmt = bsi_stmt (*from);
2957 bsi_insert_before (to, stmt, BSI_SAME_STMT);
2961 /* Move the statement at FROM to the end of basic block BB. */
2964 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
2966 block_stmt_iterator last = bsi_last (bb);
2968 /* Have to check bsi_end_p because it could be an empty block. */
2969 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
2970 bsi_move_before (from, &last);
2972 bsi_move_after (from, &last);
2976 /* Replace the contents of the statement pointed to by iterator BSI
2977 with STMT. If PRESERVE_EH_INFO is true, the exception handling
2978 information of the original statement is preserved. */
2981 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
2984 tree orig_stmt = bsi_stmt (*bsi);
2986 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
2987 set_bb_for_stmt (stmt, bsi->bb);
2989 /* Preserve EH region information from the original statement, if
2990 requested by the caller. */
2991 if (preserve_eh_info)
2993 eh_region = lookup_stmt_eh_region (orig_stmt);
2995 add_stmt_to_eh_region (stmt, eh_region);
2998 *bsi_stmt_ptr (*bsi) = stmt;
3003 /* Insert the statement pointed-to by BSI into edge E. Every attempt
3004 is made to place the statement in an existing basic block, but
3005 sometimes that isn't possible. When it isn't possible, the edge is
3006 split and the statement is added to the new block.
3008 In all cases, the returned *BSI points to the correct location. The
3009 return value is true if insertion should be done after the location,
3010 or false if it should be done before the location. If new basic block
3011 has to be created, it is stored in *NEW_BB. */
3014 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
3015 basic_block *new_bb)
3017 basic_block dest, src;
3023 /* If the destination has one predecessor which has no PHI nodes,
3024 insert there. Except for the exit block.
3026 The requirement for no PHI nodes could be relaxed. Basically we
3027 would have to examine the PHIs to prove that none of them used
3028 the value set by the statement we want to insert on E. That
3029 hardly seems worth the effort. */
3030 if (EDGE_COUNT (dest->preds) == 1
3031 && ! phi_nodes (dest)
3032 && dest != EXIT_BLOCK_PTR)
3034 *bsi = bsi_start (dest);
3035 if (bsi_end_p (*bsi))
3038 /* Make sure we insert after any leading labels. */
3039 tmp = bsi_stmt (*bsi);
3040 while (TREE_CODE (tmp) == LABEL_EXPR)
3043 if (bsi_end_p (*bsi))
3045 tmp = bsi_stmt (*bsi);
3048 if (bsi_end_p (*bsi))
3050 *bsi = bsi_last (dest);
3057 /* If the source has one successor, the edge is not abnormal and
3058 the last statement does not end a basic block, insert there.
3059 Except for the entry block. */
3061 if ((e->flags & EDGE_ABNORMAL) == 0
3062 && EDGE_COUNT (src->succs) == 1
3063 && src != ENTRY_BLOCK_PTR)
3065 *bsi = bsi_last (src);
3066 if (bsi_end_p (*bsi))
3069 tmp = bsi_stmt (*bsi);
3070 if (!stmt_ends_bb_p (tmp))
3073 /* Insert code just before returning the value. We may need to decompose
3074 the return in the case it contains non-trivial operand. */
3075 if (TREE_CODE (tmp) == RETURN_EXPR)
3077 tree op = TREE_OPERAND (tmp, 0);
3078 if (!is_gimple_val (op))
3080 gcc_assert (TREE_CODE (op) == MODIFY_EXPR);
3081 bsi_insert_before (bsi, op, BSI_NEW_STMT);
3082 TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0);
3089 /* Otherwise, create a new basic block, and split this edge. */
3090 dest = split_edge (e);
3093 e = EDGE_PRED (dest, 0);
3098 /* This routine will commit all pending edge insertions, creating any new
3099 basic blocks which are necessary. */
3102 bsi_commit_edge_inserts (void)
3108 bsi_commit_one_edge_insert (EDGE_SUCC (ENTRY_BLOCK_PTR, 0), NULL);
3111 FOR_EACH_EDGE (e, ei, bb->succs)
3112 bsi_commit_one_edge_insert (e, NULL);
3116 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
3117 to this block, otherwise set it to NULL. */
3120 bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
3124 if (PENDING_STMT (e))
3126 block_stmt_iterator bsi;
3127 tree stmt = PENDING_STMT (e);
3129 PENDING_STMT (e) = NULL_TREE;
3131 if (tree_find_edge_insert_loc (e, &bsi, new_bb))
3132 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3134 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3139 /* Add STMT to the pending list of edge E. No actual insertion is
3140 made until a call to bsi_commit_edge_inserts () is made. */
3143 bsi_insert_on_edge (edge e, tree stmt)
3145 append_to_statement_list (stmt, &PENDING_STMT (e));
3148 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new
3149 block has to be created, it is returned. */
3152 bsi_insert_on_edge_immediate (edge e, tree stmt)
3154 block_stmt_iterator bsi;
3155 basic_block new_bb = NULL;
3157 gcc_assert (!PENDING_STMT (e));
3159 if (tree_find_edge_insert_loc (e, &bsi, &new_bb))
3160 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3162 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3167 /*---------------------------------------------------------------------------
3168 Tree specific functions for CFG manipulation
3169 ---------------------------------------------------------------------------*/
3171 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
3174 reinstall_phi_args (edge new_edge, edge old_edge)
3178 if (!PENDING_STMT (old_edge))
3181 for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest);
3183 var = TREE_CHAIN (var), phi = PHI_CHAIN (phi))
3185 tree result = TREE_PURPOSE (var);
3186 tree arg = TREE_VALUE (var);
3188 gcc_assert (result == PHI_RESULT (phi));
3190 add_phi_arg (phi, arg, new_edge);
3193 PENDING_STMT (old_edge) = NULL;
3196 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3197 Abort on abnormal edges. */
3200 tree_split_edge (edge edge_in)
3202 basic_block new_bb, after_bb, dest, src;
3205 /* Abnormal edges cannot be split. */
3206 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
3209 dest = edge_in->dest;
3211 /* Place the new block in the block list. Try to keep the new block
3212 near its "logical" location. This is of most help to humans looking
3213 at debugging dumps. */
3214 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
3215 after_bb = edge_in->src;
3217 after_bb = dest->prev_bb;
3219 new_bb = create_empty_bb (after_bb);
3220 new_bb->frequency = EDGE_FREQUENCY (edge_in);
3221 new_bb->count = edge_in->count;
3222 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
3223 new_edge->probability = REG_BR_PROB_BASE;
3224 new_edge->count = edge_in->count;
3226 e = redirect_edge_and_branch (edge_in, new_bb);
3228 reinstall_phi_args (new_edge, e);
3234 /* Return true when BB has label LABEL in it. */
3237 has_label_p (basic_block bb, tree label)
3239 block_stmt_iterator bsi;
3241 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3243 tree stmt = bsi_stmt (bsi);
3245 if (TREE_CODE (stmt) != LABEL_EXPR)
3247 if (LABEL_EXPR_LABEL (stmt) == label)
3254 /* Callback for walk_tree, check that all elements with address taken are
3255 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3256 inside a PHI node. */
3259 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3262 bool in_phi = (data != NULL);
3267 /* Check operand N for being valid GIMPLE and give error MSG if not.
3268 We check for constants explicitly since they are not considered
3269 gimple invariants if they overflowed. */
3270 #define CHECK_OP(N, MSG) \
3271 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3272 && !is_gimple_val (TREE_OPERAND (t, N))) \
3273 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3275 switch (TREE_CODE (t))
3278 if (SSA_NAME_IN_FREE_LIST (t))
3280 error ("SSA name in freelist but still referenced");
3286 x = TREE_OPERAND (t, 0);
3287 if (TREE_CODE (x) == BIT_FIELD_REF
3288 && is_gimple_reg (TREE_OPERAND (x, 0)))
3290 error ("GIMPLE register modified with BIT_FIELD_REF");
3296 /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
3297 dead PHIs that take the address of something. But if the PHI
3298 result is dead, the fact that it takes the address of anything
3299 is irrelevant. Because we can not tell from here if a PHI result
3300 is dead, we just skip this check for PHIs altogether. This means
3301 we may be missing "valid" checks, but what can you do?
3302 This was PR19217. */
3306 /* Skip any references (they will be checked when we recurse down the
3307 tree) and ensure that any variable used as a prefix is marked
3309 for (x = TREE_OPERAND (t, 0);
3310 handled_component_p (x);
3311 x = TREE_OPERAND (x, 0))
3314 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3316 if (!TREE_ADDRESSABLE (x))
3318 error ("address taken, but ADDRESSABLE bit not set");
3324 x = COND_EXPR_COND (t);
3325 if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3327 error ("non-boolean used in condition");
3334 case FIX_TRUNC_EXPR:
3336 case FIX_FLOOR_EXPR:
3337 case FIX_ROUND_EXPR:
3342 case NON_LVALUE_EXPR:
3343 case TRUTH_NOT_EXPR:
3344 CHECK_OP (0, "Invalid operand to unary operator");
3351 case ARRAY_RANGE_REF:
3353 case VIEW_CONVERT_EXPR:
3354 /* We have a nest of references. Verify that each of the operands
3355 that determine where to reference is either a constant or a variable,
3356 verify that the base is valid, and then show we've already checked
3358 while (handled_component_p (t))
3360 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3361 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3362 else if (TREE_CODE (t) == ARRAY_REF
3363 || TREE_CODE (t) == ARRAY_RANGE_REF)
3365 CHECK_OP (1, "Invalid array index.");
3366 if (TREE_OPERAND (t, 2))
3367 CHECK_OP (2, "Invalid array lower bound.");
3368 if (TREE_OPERAND (t, 3))
3369 CHECK_OP (3, "Invalid array stride.");
3371 else if (TREE_CODE (t) == BIT_FIELD_REF)
3373 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3374 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3377 t = TREE_OPERAND (t, 0);
3380 if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t))
3382 error ("Invalid reference prefix.");
3394 case UNORDERED_EXPR:
3405 case TRUNC_DIV_EXPR:
3407 case FLOOR_DIV_EXPR:
3408 case ROUND_DIV_EXPR:
3409 case TRUNC_MOD_EXPR:
3411 case FLOOR_MOD_EXPR:
3412 case ROUND_MOD_EXPR:
3414 case EXACT_DIV_EXPR:
3424 CHECK_OP (0, "Invalid operand to binary operator");
3425 CHECK_OP (1, "Invalid operand to binary operator");
3437 /* Verify STMT, return true if STMT is not in GIMPLE form.
3438 TODO: Implement type checking. */
3441 verify_stmt (tree stmt, bool last_in_block)
3445 if (!is_gimple_stmt (stmt))
3447 error ("Is not a valid GIMPLE statement.");
3451 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
3454 debug_generic_stmt (addr);
3458 /* If the statement is marked as part of an EH region, then it is
3459 expected that the statement could throw. Verify that when we
3460 have optimizations that simplify statements such that we prove
3461 that they cannot throw, that we update other data structures
3463 if (lookup_stmt_eh_region (stmt) >= 0)
3465 if (!tree_could_throw_p (stmt))
3467 error ("Statement marked for throw, but doesn%'t.");
3470 if (!last_in_block && tree_can_throw_internal (stmt))
3472 error ("Statement marked for throw in middle of block.");
3480 debug_generic_stmt (stmt);
3485 /* Return true when the T can be shared. */
3488 tree_node_can_be_shared (tree t)
3490 if (IS_TYPE_OR_DECL_P (t)
3491 /* We check for constants explicitly since they are not considered
3492 gimple invariants if they overflowed. */
3493 || CONSTANT_CLASS_P (t)
3494 || is_gimple_min_invariant (t)
3495 || TREE_CODE (t) == SSA_NAME
3496 || t == error_mark_node)
3499 if (TREE_CODE (t) == CASE_LABEL_EXPR)
3502 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3503 /* We check for constants explicitly since they are not considered
3504 gimple invariants if they overflowed. */
3505 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 1))
3506 || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
3507 || (TREE_CODE (t) == COMPONENT_REF
3508 || TREE_CODE (t) == REALPART_EXPR
3509 || TREE_CODE (t) == IMAGPART_EXPR))
3510 t = TREE_OPERAND (t, 0);
3519 /* Called via walk_trees. Verify tree sharing. */
3522 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
3524 htab_t htab = (htab_t) data;
3527 if (tree_node_can_be_shared (*tp))
3529 *walk_subtrees = false;
3533 slot = htab_find_slot (htab, *tp, INSERT);
3542 /* Verify the GIMPLE statement chain. */
3548 block_stmt_iterator bsi;
3553 timevar_push (TV_TREE_STMT_VERIFY);
3554 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3561 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3563 int phi_num_args = PHI_NUM_ARGS (phi);
3565 for (i = 0; i < phi_num_args; i++)
3567 tree t = PHI_ARG_DEF (phi, i);
3570 /* Addressable variables do have SSA_NAMEs but they
3571 are not considered gimple values. */
3572 if (TREE_CODE (t) != SSA_NAME
3573 && TREE_CODE (t) != FUNCTION_DECL
3574 && !is_gimple_val (t))
3576 error ("PHI def is not a GIMPLE value");
3577 debug_generic_stmt (phi);
3578 debug_generic_stmt (t);
3582 addr = walk_tree (&t, verify_expr, (void *) 1, NULL);
3585 debug_generic_stmt (addr);
3589 addr = walk_tree (&t, verify_node_sharing, htab, NULL);
3592 error ("Incorrect sharing of tree nodes");
3593 debug_generic_stmt (phi);
3594 debug_generic_stmt (addr);
3600 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
3602 tree stmt = bsi_stmt (bsi);
3604 err |= verify_stmt (stmt, bsi_end_p (bsi));
3605 addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
3608 error ("Incorrect sharing of tree nodes");
3609 debug_generic_stmt (stmt);
3610 debug_generic_stmt (addr);
3617 internal_error ("verify_stmts failed.");
3620 timevar_pop (TV_TREE_STMT_VERIFY);
3624 /* Verifies that the flow information is OK. */
3627 tree_verify_flow_info (void)
3631 block_stmt_iterator bsi;
3636 if (ENTRY_BLOCK_PTR->stmt_list)
3638 error ("ENTRY_BLOCK has a statement list associated with it\n");
3642 if (EXIT_BLOCK_PTR->stmt_list)
3644 error ("EXIT_BLOCK has a statement list associated with it\n");
3648 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3649 if (e->flags & EDGE_FALLTHRU)
3651 error ("Fallthru to exit from bb %d\n", e->src->index);
3657 bool found_ctrl_stmt = false;
3661 /* Skip labels on the start of basic block. */
3662 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3664 tree prev_stmt = stmt;
3666 stmt = bsi_stmt (bsi);
3668 if (TREE_CODE (stmt) != LABEL_EXPR)
3671 if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
3673 error ("Nonlocal label %s is not first "
3674 "in a sequence of labels in bb %d",
3675 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3680 if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
3682 error ("Label %s to block does not match in bb %d\n",
3683 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3688 if (decl_function_context (LABEL_EXPR_LABEL (stmt))
3689 != current_function_decl)
3691 error ("Label %s has incorrect context in bb %d\n",
3692 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3698 /* Verify that body of basic block BB is free of control flow. */
3699 for (; !bsi_end_p (bsi); bsi_next (&bsi))
3701 tree stmt = bsi_stmt (bsi);
3703 if (found_ctrl_stmt)
3705 error ("Control flow in the middle of basic block %d\n",
3710 if (stmt_ends_bb_p (stmt))
3711 found_ctrl_stmt = true;
3713 if (TREE_CODE (stmt) == LABEL_EXPR)
3715 error ("Label %s in the middle of basic block %d\n",
3716 IDENTIFIER_POINTER (DECL_NAME (stmt)),
3721 bsi = bsi_last (bb);
3722 if (bsi_end_p (bsi))
3725 stmt = bsi_stmt (bsi);
3727 if (is_ctrl_stmt (stmt))
3729 FOR_EACH_EDGE (e, ei, bb->succs)
3730 if (e->flags & EDGE_FALLTHRU)
3732 error ("Fallthru edge after a control statement in bb %d \n",
3738 switch (TREE_CODE (stmt))
3744 if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR
3745 || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR)
3747 error ("Structured COND_EXPR at the end of bb %d\n", bb->index);
3751 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
3753 if (!true_edge || !false_edge
3754 || !(true_edge->flags & EDGE_TRUE_VALUE)
3755 || !(false_edge->flags & EDGE_FALSE_VALUE)
3756 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3757 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3758 || EDGE_COUNT (bb->succs) >= 3)
3760 error ("Wrong outgoing edge flags at end of bb %d\n",
3765 if (!has_label_p (true_edge->dest,
3766 GOTO_DESTINATION (COND_EXPR_THEN (stmt))))
3768 error ("%<then%> label does not match edge at end of bb %d\n",
3773 if (!has_label_p (false_edge->dest,
3774 GOTO_DESTINATION (COND_EXPR_ELSE (stmt))))
3776 error ("%<else%> label does not match edge at end of bb %d\n",
3784 if (simple_goto_p (stmt))
3786 error ("Explicit goto at end of bb %d\n", bb->index);
3791 /* FIXME. We should double check that the labels in the
3792 destination blocks have their address taken. */
3793 FOR_EACH_EDGE (e, ei, bb->succs)
3794 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
3795 | EDGE_FALSE_VALUE))
3796 || !(e->flags & EDGE_ABNORMAL))
3798 error ("Wrong outgoing edge flags at end of bb %d\n",
3806 if (EDGE_COUNT (bb->succs) != 1
3807 || (EDGE_SUCC (bb, 0)->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3808 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3810 error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
3813 if (EDGE_SUCC (bb, 0)->dest != EXIT_BLOCK_PTR)
3815 error ("Return edge does not point to exit in bb %d\n",
3828 vec = SWITCH_LABELS (stmt);
3829 n = TREE_VEC_LENGTH (vec);
3831 /* Mark all the destination basic blocks. */
3832 for (i = 0; i < n; ++i)
3834 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3835 basic_block label_bb = label_to_block (lab);
3837 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
3838 label_bb->aux = (void *)1;
3841 /* Verify that the case labels are sorted. */
3842 prev = TREE_VEC_ELT (vec, 0);
3843 for (i = 1; i < n - 1; ++i)
3845 tree c = TREE_VEC_ELT (vec, i);
3848 error ("Found default case not at end of case vector");
3852 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
3854 error ("Case labels not sorted:\n ");
3855 print_generic_expr (stderr, prev, 0);
3856 fprintf (stderr," is greater than ");
3857 print_generic_expr (stderr, c, 0);
3858 fprintf (stderr," but comes before it.\n");
3863 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
3865 error ("No default case found at end of case vector");
3869 FOR_EACH_EDGE (e, ei, bb->succs)
3873 error ("Extra outgoing edge %d->%d\n",
3874 bb->index, e->dest->index);
3877 e->dest->aux = (void *)2;
3878 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3879 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3881 error ("Wrong outgoing edge flags at end of bb %d\n",
3887 /* Check that we have all of them. */
3888 for (i = 0; i < n; ++i)
3890 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3891 basic_block label_bb = label_to_block (lab);
3893 if (label_bb->aux != (void *)2)
3895 error ("Missing edge %i->%i",
3896 bb->index, label_bb->index);
3901 FOR_EACH_EDGE (e, ei, bb->succs)
3902 e->dest->aux = (void *)0;
3909 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
3910 verify_dominators (CDI_DOMINATORS);
3916 /* Updates phi nodes after creating a forwarder block joined
3917 by edge FALLTHRU. */
3920 tree_make_forwarder_block (edge fallthru)
3924 basic_block dummy, bb;
3925 tree phi, new_phi, var;
3927 dummy = fallthru->src;
3928 bb = fallthru->dest;
3930 if (EDGE_COUNT (bb->preds) == 1)
3933 /* If we redirected a branch we must create new phi nodes at the
3935 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
3937 var = PHI_RESULT (phi);
3938 new_phi = create_phi_node (var, bb);
3939 SSA_NAME_DEF_STMT (var) = new_phi;
3940 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
3941 add_phi_arg (new_phi, PHI_RESULT (phi), fallthru);
3944 /* Ensure that the PHI node chain is in the same order. */
3945 set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
3947 /* Add the arguments we have stored on edges. */
3948 FOR_EACH_EDGE (e, ei, bb->preds)
3953 flush_pending_stmts (e);
3958 /* Return true if basic block BB does nothing except pass control
3959 flow to another block and that we can safely insert a label at
3960 the start of the successor block.
3962 As a precondition, we require that BB be not equal to
3966 tree_forwarder_block_p (basic_block bb, bool phi_wanted)
3968 block_stmt_iterator bsi;
3970 /* BB must have a single outgoing edge. */
3971 if (EDGE_COUNT (bb->succs) != 1
3972 /* If PHI_WANTED is false, BB must not have any PHI nodes.
3973 Otherwise, BB must have PHI nodes. */
3974 || (phi_nodes (bb) != NULL_TREE) != phi_wanted
3975 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
3976 || EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR
3977 /* Nor should this be an infinite loop. */
3978 || EDGE_SUCC (bb, 0)->dest == bb
3979 /* BB may not have an abnormal outgoing edge. */
3980 || (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL))
3984 gcc_assert (bb != ENTRY_BLOCK_PTR);
3987 /* Now walk through the statements backward. We can ignore labels,
3988 anything else means this is not a forwarder block. */
3989 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3991 tree stmt = bsi_stmt (bsi);
3993 switch (TREE_CODE (stmt))
3996 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
4005 if (find_edge (ENTRY_BLOCK_PTR, bb))
4011 /* Return true if BB has at least one abnormal incoming edge. */
4014 has_abnormal_incoming_edge_p (basic_block bb)
4019 FOR_EACH_EDGE (e, ei, bb->preds)
4020 if (e->flags & EDGE_ABNORMAL)
4026 /* Removes forwarder block BB. Returns false if this failed. If a new
4027 forwarder block is created due to redirection of edges, it is
4028 stored to worklist. */
4031 remove_forwarder_block (basic_block bb, basic_block **worklist)
4033 edge succ = EDGE_SUCC (bb, 0), e, s;
4034 basic_block dest = succ->dest;
4038 block_stmt_iterator bsi, bsi_to;
4039 bool seen_abnormal_edge = false;
4041 /* We check for infinite loops already in tree_forwarder_block_p.
4042 However it may happen that the infinite loop is created
4043 afterwards due to removal of forwarders. */
4047 /* If the destination block consists of a nonlocal label, do not merge
4049 label = first_stmt (dest);
4051 && TREE_CODE (label) == LABEL_EXPR
4052 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4055 /* If there is an abnormal edge to basic block BB, but not into
4056 dest, problems might occur during removal of the phi node at out
4057 of ssa due to overlapping live ranges of registers.
4059 If there is an abnormal edge in DEST, the problems would occur
4060 anyway since cleanup_dead_labels would then merge the labels for
4061 two different eh regions, and rest of exception handling code
4064 So if there is an abnormal edge to BB, proceed only if there is
4065 no abnormal edge to DEST and there are no phi nodes in DEST. */
4066 if (has_abnormal_incoming_edge_p (bb))
4068 seen_abnormal_edge = true;
4070 if (has_abnormal_incoming_edge_p (dest)
4071 || phi_nodes (dest) != NULL_TREE)
4075 /* If there are phi nodes in DEST, and some of the blocks that are
4076 predecessors of BB are also predecessors of DEST, check that the
4077 phi node arguments match. */
4078 if (phi_nodes (dest))
4080 FOR_EACH_EDGE (e, ei, bb->preds)
4082 s = find_edge (e->src, dest);
4086 if (!phi_alternatives_equal (dest, succ, s))
4091 /* Redirect the edges. */
4092 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4094 if (e->flags & EDGE_ABNORMAL)
4096 /* If there is an abnormal edge, redirect it anyway, and
4097 move the labels to the new block to make it legal. */
4098 s = redirect_edge_succ_nodup (e, dest);
4101 s = redirect_edge_and_branch (e, dest);
4105 /* Create arguments for the phi nodes, since the edge was not
4107 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4108 add_phi_arg (phi, PHI_ARG_DEF (phi, succ->dest_idx), s);
4112 /* The source basic block might become a forwarder. We know
4113 that it was not a forwarder before, since it used to have
4114 at least two outgoing edges, so we may just add it to
4116 if (tree_forwarder_block_p (s->src, false))
4117 *(*worklist)++ = s->src;
4121 if (seen_abnormal_edge)
4123 /* Move the labels to the new block, so that the redirection of
4124 the abnormal edges works. */
4126 bsi_to = bsi_start (dest);
4127 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
4129 label = bsi_stmt (bsi);
4130 gcc_assert (TREE_CODE (label) == LABEL_EXPR);
4132 bsi_insert_before (&bsi_to, label, BSI_CONTINUE_LINKING);
4136 /* Update the dominators. */
4137 if (dom_info_available_p (CDI_DOMINATORS))
4139 basic_block dom, dombb, domdest;
4141 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4142 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4145 /* Shortcut to avoid calling (relatively expensive)
4146 nearest_common_dominator unless necessary. */
4150 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4152 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4155 /* And kill the forwarder block. */
4156 delete_basic_block (bb);
4161 /* Removes forwarder blocks. */
4164 cleanup_forwarder_blocks (void)
4167 bool changed = false;
4168 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4169 basic_block *current = worklist;
4173 if (tree_forwarder_block_p (bb, false))
4177 while (current != worklist)
4180 changed |= remove_forwarder_block (bb, ¤t);
4187 /* Merge the PHI nodes at BB into those at BB's sole successor. */
4190 remove_forwarder_block_with_phi (basic_block bb)
4192 edge succ = EDGE_SUCC (bb, 0);
4193 basic_block dest = succ->dest;
4195 basic_block dombb, domdest, dom;
4197 /* We check for infinite loops already in tree_forwarder_block_p.
4198 However it may happen that the infinite loop is created
4199 afterwards due to removal of forwarders. */
4203 /* If the destination block consists of a nonlocal label, do not
4205 label = first_stmt (dest);
4207 && TREE_CODE (label) == LABEL_EXPR
4208 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4211 /* Redirect each incoming edge to BB to DEST. */
4212 while (EDGE_COUNT (bb->preds) > 0)
4214 edge e = EDGE_PRED (bb, 0), s;
4217 s = find_edge (e->src, dest);
4220 /* We already have an edge S from E->src to DEST. If S and
4221 E->dest's sole successor edge have the same PHI arguments
4222 at DEST, redirect S to DEST. */
4223 if (phi_alternatives_equal (dest, s, succ))
4225 e = redirect_edge_and_branch (e, dest);
4226 PENDING_STMT (e) = NULL_TREE;
4230 /* PHI arguments are different. Create a forwarder block by
4231 splitting E so that we can merge PHI arguments on E to
4233 e = EDGE_SUCC (split_edge (e), 0);
4236 s = redirect_edge_and_branch (e, dest);
4238 /* redirect_edge_and_branch must not create a new edge. */
4239 gcc_assert (s == e);
4241 /* Add to the PHI nodes at DEST each PHI argument removed at the
4242 destination of E. */
4243 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4245 tree def = PHI_ARG_DEF (phi, succ->dest_idx);
4247 if (TREE_CODE (def) == SSA_NAME)
4251 /* If DEF is one of the results of PHI nodes removed during
4252 redirection, replace it with the PHI argument that used
4254 for (var = PENDING_STMT (e); var; var = TREE_CHAIN (var))
4256 tree old_arg = TREE_PURPOSE (var);
4257 tree new_arg = TREE_VALUE (var);
4267 add_phi_arg (phi, def, s);
4270 PENDING_STMT (e) = NULL;
4273 /* Update the dominators. */
4274 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4275 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4278 /* Shortcut to avoid calling (relatively expensive)
4279 nearest_common_dominator unless necessary. */
4283 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4285 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4287 /* Remove BB since all of BB's incoming edges have been redirected
4289 delete_basic_block (bb);
4292 /* This pass merges PHI nodes if one feeds into another. For example,
4293 suppose we have the following:
4300 # tem_6 = PHI <tem_17(8), tem_23(7)>;
4303 # tem_3 = PHI <tem_6(9), tem_2(5)>;
4306 Then we merge the first PHI node into the second one like so:
4308 goto <bb 9> (<L10>);
4313 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
4318 merge_phi_nodes (void)
4320 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4321 basic_block *current = worklist;
4324 calculate_dominance_info (CDI_DOMINATORS);
4326 /* Find all PHI nodes that we may be able to merge. */
4331 /* Look for a forwarder block with PHI nodes. */
4332 if (!tree_forwarder_block_p (bb, true))
4335 dest = EDGE_SUCC (bb, 0)->dest;
4337 /* We have to feed into another basic block with PHI
4339 if (!phi_nodes (dest)
4340 /* We don't want to deal with a basic block with
4342 || has_abnormal_incoming_edge_p (bb))
4345 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
4347 /* If BB does not dominate DEST, then the PHI nodes at
4348 DEST must be the only users of the results of the PHI
4354 /* Now let's drain WORKLIST. */
4355 while (current != worklist)
4358 remove_forwarder_block_with_phi (bb);
4365 gate_merge_phi (void)
4370 struct tree_opt_pass pass_merge_phi = {
4371 "mergephi", /* name */
4372 gate_merge_phi, /* gate */
4373 merge_phi_nodes, /* execute */
4376 0, /* static_pass_number */
4377 TV_TREE_MERGE_PHI, /* tv_id */
4378 PROP_cfg | PROP_ssa, /* properties_required */
4379 0, /* properties_provided */
4380 0, /* properties_destroyed */
4381 0, /* todo_flags_start */
4382 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */
4387 /* Return a non-special label in the head of basic block BLOCK.
4388 Create one if it doesn't exist. */
4391 tree_block_label (basic_block bb)
4393 block_stmt_iterator i, s = bsi_start (bb);
4397 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4399 stmt = bsi_stmt (i);
4400 if (TREE_CODE (stmt) != LABEL_EXPR)
4402 label = LABEL_EXPR_LABEL (stmt);
4403 if (!DECL_NONLOCAL (label))
4406 bsi_move_before (&i, &s);
4411 label = create_artificial_label ();
4412 stmt = build1 (LABEL_EXPR, void_type_node, label);
4413 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4418 /* Attempt to perform edge redirection by replacing a possibly complex
4419 jump instruction by a goto or by removing the jump completely.
4420 This can apply only if all edges now point to the same block. The
4421 parameters and return values are equivalent to
4422 redirect_edge_and_branch. */
4425 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4427 basic_block src = e->src;
4428 block_stmt_iterator b;
4431 /* We can replace or remove a complex jump only when we have exactly
4433 if (EDGE_COUNT (src->succs) != 2
4434 /* Verify that all targets will be TARGET. Specifically, the
4435 edge that is not E must also go to TARGET. */
4436 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4442 stmt = bsi_stmt (b);
4444 if (TREE_CODE (stmt) == COND_EXPR
4445 || TREE_CODE (stmt) == SWITCH_EXPR)
4448 e = ssa_redirect_edge (e, target);
4449 e->flags = EDGE_FALLTHRU;
4457 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4458 edge representing the redirected branch. */
4461 tree_redirect_edge_and_branch (edge e, basic_block dest)
4463 basic_block bb = e->src;
4464 block_stmt_iterator bsi;
4468 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4471 if (e->src != ENTRY_BLOCK_PTR
4472 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4475 if (e->dest == dest)
4478 label = tree_block_label (dest);
4480 bsi = bsi_last (bb);
4481 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4483 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4486 stmt = (e->flags & EDGE_TRUE_VALUE
4487 ? COND_EXPR_THEN (stmt)
4488 : COND_EXPR_ELSE (stmt));
4489 GOTO_DESTINATION (stmt) = label;
4493 /* No non-abnormal edges should lead from a non-simple goto, and
4494 simple ones should be represented implicitly. */
4499 tree cases = get_cases_for_edge (e, stmt);
4501 /* If we have a list of cases associated with E, then use it
4502 as it's a lot faster than walking the entire case vector. */
4505 edge e2 = find_edge (e->src, dest);
4512 CASE_LABEL (cases) = label;
4513 cases = TREE_CHAIN (cases);
4516 /* If there was already an edge in the CFG, then we need
4517 to move all the cases associated with E to E2. */
4520 tree cases2 = get_cases_for_edge (e2, stmt);
4522 TREE_CHAIN (last) = TREE_CHAIN (cases2);
4523 TREE_CHAIN (cases2) = first;
4528 tree vec = SWITCH_LABELS (stmt);
4529 size_t i, n = TREE_VEC_LENGTH (vec);
4531 for (i = 0; i < n; i++)
4533 tree elt = TREE_VEC_ELT (vec, i);
4535 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4536 CASE_LABEL (elt) = label;
4545 e->flags |= EDGE_FALLTHRU;
4549 /* Otherwise it must be a fallthru edge, and we don't need to
4550 do anything besides redirecting it. */
4551 gcc_assert (e->flags & EDGE_FALLTHRU);
4555 /* Update/insert PHI nodes as necessary. */
4557 /* Now update the edges in the CFG. */
4558 e = ssa_redirect_edge (e, dest);
4564 /* Simple wrapper, as we can always redirect fallthru edges. */
4567 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4569 e = tree_redirect_edge_and_branch (e, dest);
4576 /* Splits basic block BB after statement STMT (but at least after the
4577 labels). If STMT is NULL, BB is split just after the labels. */
4580 tree_split_block (basic_block bb, void *stmt)
4582 block_stmt_iterator bsi, bsi_tgt;
4588 new_bb = create_empty_bb (bb);
4590 /* Redirect the outgoing edges. */
4591 new_bb->succs = bb->succs;
4593 FOR_EACH_EDGE (e, ei, new_bb->succs)
4596 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4599 /* Move everything from BSI to the new basic block. */
4600 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4602 act = bsi_stmt (bsi);
4603 if (TREE_CODE (act) == LABEL_EXPR)
4616 bsi_tgt = bsi_start (new_bb);
4617 while (!bsi_end_p (bsi))
4619 act = bsi_stmt (bsi);
4621 bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT);
4628 /* Moves basic block BB after block AFTER. */
4631 tree_move_block_after (basic_block bb, basic_block after)
4633 if (bb->prev_bb == after)
4637 link_block (bb, after);
4643 /* Return true if basic_block can be duplicated. */
4646 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED)
4651 /* Create a duplicate of the basic block BB. NOTE: This does not
4652 preserve SSA form. */
4655 tree_duplicate_bb (basic_block bb)
4658 block_stmt_iterator bsi, bsi_tgt;
4660 ssa_op_iter op_iter;
4662 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4664 /* First copy the phi nodes. We do not copy phi node arguments here,
4665 since the edges are not ready yet. Keep the chain of phi nodes in
4666 the same order, so that we can add them later. */
4667 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4669 mark_for_rewrite (PHI_RESULT (phi));
4670 create_phi_node (PHI_RESULT (phi), new_bb);
4672 set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb)));
4674 bsi_tgt = bsi_start (new_bb);
4675 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4677 tree stmt = bsi_stmt (bsi);
4680 if (TREE_CODE (stmt) == LABEL_EXPR)
4683 /* Record the definitions. */
4684 get_stmt_operands (stmt);
4686 FOR_EACH_SSA_TREE_OPERAND (val, stmt, op_iter, SSA_OP_ALL_DEFS)
4687 mark_for_rewrite (val);
4689 copy = unshare_expr (stmt);
4691 /* Copy also the virtual operands. */
4692 get_stmt_ann (copy);
4693 copy_virtual_operands (copy, stmt);
4695 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
4701 /* Basic block BB_COPY was created by code duplication. Add phi node
4702 arguments for edges going out of BB_COPY. The blocks that were
4703 duplicated have rbi->duplicated set to one. */
4706 add_phi_args_after_copy_bb (basic_block bb_copy)
4708 basic_block bb, dest;
4711 tree phi, phi_copy, phi_next, def;
4713 bb = bb_copy->rbi->original;
4715 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
4717 if (!phi_nodes (e_copy->dest))
4720 if (e_copy->dest->rbi->duplicated)
4721 dest = e_copy->dest->rbi->original;
4723 dest = e_copy->dest;
4725 e = find_edge (bb, dest);
4728 /* During loop unrolling the target of the latch edge is copied.
4729 In this case we are not looking for edge to dest, but to
4730 duplicated block whose original was dest. */
4731 FOR_EACH_EDGE (e, ei, bb->succs)
4732 if (e->dest->rbi->duplicated
4733 && e->dest->rbi->original == dest)
4736 gcc_assert (e != NULL);
4739 for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
4741 phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
4743 phi_next = PHI_CHAIN (phi);
4745 gcc_assert (PHI_RESULT (phi) == PHI_RESULT (phi_copy));
4746 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4747 add_phi_arg (phi_copy, def, e_copy);
4752 /* Blocks in REGION_COPY array of length N_REGION were created by
4753 duplication of basic blocks. Add phi node arguments for edges
4754 going from these blocks. */
4757 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region)
4761 for (i = 0; i < n_region; i++)
4762 region_copy[i]->rbi->duplicated = 1;
4764 for (i = 0; i < n_region; i++)
4765 add_phi_args_after_copy_bb (region_copy[i]);
4767 for (i = 0; i < n_region; i++)
4768 region_copy[i]->rbi->duplicated = 0;
4771 /* Maps the old ssa name FROM_NAME to TO_NAME. */
4773 struct ssa_name_map_entry
4779 /* Hash function for ssa_name_map_entry. */
4782 ssa_name_map_entry_hash (const void *entry)
4784 const struct ssa_name_map_entry *en = entry;
4785 return SSA_NAME_VERSION (en->from_name);
4788 /* Equality function for ssa_name_map_entry. */
4791 ssa_name_map_entry_eq (const void *in_table, const void *ssa_name)
4793 const struct ssa_name_map_entry *en = in_table;
4795 return en->from_name == ssa_name;
4798 /* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping
4802 allocate_ssa_names (bitmap definitions, htab_t *map)
4805 struct ssa_name_map_entry *entry;
4811 *map = htab_create (10, ssa_name_map_entry_hash,
4812 ssa_name_map_entry_eq, free);
4813 EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
4815 name = ssa_name (ver);
4816 slot = htab_find_slot_with_hash (*map, name, SSA_NAME_VERSION (name),
4822 entry = xmalloc (sizeof (struct ssa_name_map_entry));
4823 entry->from_name = name;
4826 entry->to_name = duplicate_ssa_name (name, SSA_NAME_DEF_STMT (name));
4830 /* Rewrite the definition DEF in statement STMT to new ssa name as specified
4831 by the mapping MAP. */
4834 rewrite_to_new_ssa_names_def (def_operand_p def, tree stmt, htab_t map)
4836 tree name = DEF_FROM_PTR (def);
4837 struct ssa_name_map_entry *entry;
4839 gcc_assert (TREE_CODE (name) == SSA_NAME);
4841 entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name));
4845 SET_DEF (def, entry->to_name);
4846 SSA_NAME_DEF_STMT (entry->to_name) = stmt;
4849 /* Rewrite the USE to new ssa name as specified by the mapping MAP. */
4852 rewrite_to_new_ssa_names_use (use_operand_p use, htab_t map)
4854 tree name = USE_FROM_PTR (use);
4855 struct ssa_name_map_entry *entry;
4857 if (TREE_CODE (name) != SSA_NAME)
4860 entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name));
4864 SET_USE (use, entry->to_name);
4867 /* Rewrite the ssa names in basic block BB to new ones as specified by the
4871 rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map)
4877 block_stmt_iterator bsi;
4881 v_may_def_optype v_may_defs;
4882 v_must_def_optype v_must_defs;
4885 FOR_EACH_EDGE (e, ei, bb->preds)
4886 if (e->flags & EDGE_ABNORMAL)
4889 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4891 rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi), phi, map);
4893 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
4896 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4898 stmt = bsi_stmt (bsi);
4899 get_stmt_operands (stmt);
4900 ann = stmt_ann (stmt);
4902 uses = USE_OPS (ann);
4903 for (i = 0; i < NUM_USES (uses); i++)
4904 rewrite_to_new_ssa_names_use (USE_OP_PTR (uses, i), map);
4906 defs = DEF_OPS (ann);
4907 for (i = 0; i < NUM_DEFS (defs); i++)
4908 rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs, i), stmt, map);
4910 vuses = VUSE_OPS (ann);
4911 for (i = 0; i < NUM_VUSES (vuses); i++)
4912 rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses, i), map);
4914 v_may_defs = V_MAY_DEF_OPS (ann);
4915 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
4917 rewrite_to_new_ssa_names_use
4918 (V_MAY_DEF_OP_PTR (v_may_defs, i), map);
4919 rewrite_to_new_ssa_names_def
4920 (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt, map);
4923 v_must_defs = V_MUST_DEF_OPS (ann);
4924 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
4926 rewrite_to_new_ssa_names_def
4927 (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt, map);
4928 rewrite_to_new_ssa_names_use
4929 (V_MUST_DEF_KILL_PTR (v_must_defs, i), map);
4933 FOR_EACH_EDGE (e, ei, bb->succs)
4934 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
4936 rewrite_to_new_ssa_names_use
4937 (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), map);
4939 if (e->flags & EDGE_ABNORMAL)
4941 tree op = PHI_ARG_DEF_FROM_EDGE (phi, e);
4942 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1;
4947 /* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified
4948 by the mapping MAP. */
4951 rewrite_to_new_ssa_names (basic_block *region, unsigned n_region, htab_t map)
4955 for (r = 0; r < n_region; r++)
4956 rewrite_to_new_ssa_names_bb (region[r], map);
4959 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4960 important exit edge EXIT. By important we mean that no SSA name defined
4961 inside region is live over the other exit edges of the region. All entry
4962 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4963 to the duplicate of the region. SSA form, dominance and loop information
4964 is updated. The new basic blocks are stored to REGION_COPY in the same
4965 order as they had in REGION, provided that REGION_COPY is not NULL.
4966 The function returns false if it is unable to copy the region,
4970 tree_duplicate_sese_region (edge entry, edge exit,
4971 basic_block *region, unsigned n_region,
4972 basic_block *region_copy)
4974 unsigned i, n_doms, ver;
4975 bool free_region_copy = false, copying_header = false;
4976 struct loop *loop = entry->dest->loop_father;
4981 htab_t ssa_name_map = NULL;
4985 if (!can_copy_bbs_p (region, n_region))
4988 /* Some sanity checking. Note that we do not check for all possible
4989 missuses of the functions. I.e. if you ask to copy something weird,
4990 it will work, but the state of structures probably will not be
4993 for (i = 0; i < n_region; i++)
4995 /* We do not handle subloops, i.e. all the blocks must belong to the
4997 if (region[i]->loop_father != loop)
5000 if (region[i] != entry->dest
5001 && region[i] == loop->header)
5007 /* In case the function is used for loop header copying (which is the primary
5008 use), ensure that EXIT and its copy will be new latch and entry edges. */
5009 if (loop->header == entry->dest)
5011 copying_header = true;
5012 loop->copy = loop->outer;
5014 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5017 for (i = 0; i < n_region; i++)
5018 if (region[i] != exit->src
5019 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5025 region_copy = xmalloc (sizeof (basic_block) * n_region);
5026 free_region_copy = true;
5029 gcc_assert (!any_marked_for_rewrite_p ());
5031 /* Record blocks outside the region that are duplicated by something
5033 doms = xmalloc (sizeof (basic_block) * n_basic_blocks);
5034 n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms);
5036 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop);
5037 definitions = marked_ssa_names ();
5041 loop->header = exit->dest;
5042 loop->latch = exit->src;
5045 /* Redirect the entry and add the phi node arguments. */
5046 redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy);
5047 gcc_assert (redirected != NULL);
5048 flush_pending_stmts (entry);
5050 /* Concerning updating of dominators: We must recount dominators
5051 for entry block and its copy. Anything that is outside of the region, but
5052 was dominated by something inside needs recounting as well. */
5053 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5054 doms[n_doms++] = entry->dest->rbi->original;
5055 iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms);
5058 /* Add the other phi node arguments. */
5059 add_phi_args_after_copy (region_copy, n_region);
5061 /* Add phi nodes for definitions at exit. TODO -- once we have immediate
5062 uses, it should be possible to emit phi nodes just for definitions that
5063 are used outside region. */
5064 EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
5066 tree name = ssa_name (ver);
5068 phi = create_phi_node (name, exit->dest);
5069 add_phi_arg (phi, name, exit);
5070 add_phi_arg (phi, name, exit_copy);
5072 SSA_NAME_DEF_STMT (name) = phi;
5075 /* And create new definitions inside region and its copy. TODO -- once we
5076 have immediate uses, it might be better to leave definitions in region
5077 unchanged, create new ssa names for phi nodes on exit, and rewrite
5078 the uses, to avoid changing the copied region. */
5079 allocate_ssa_names (definitions, &ssa_name_map);
5080 rewrite_to_new_ssa_names (region, n_region, ssa_name_map);
5081 allocate_ssa_names (definitions, &ssa_name_map);
5082 rewrite_to_new_ssa_names (region_copy, n_region, ssa_name_map);
5083 htab_delete (ssa_name_map);
5085 if (free_region_copy)
5088 unmark_all_for_rewrite ();
5089 BITMAP_FREE (definitions);
5094 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
5097 dump_function_to_file (tree fn, FILE *file, int flags)
5099 tree arg, vars, var;
5100 bool ignore_topmost_bind = false, any_var = false;
5104 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
5106 arg = DECL_ARGUMENTS (fn);
5109 print_generic_expr (file, arg, dump_flags);
5110 if (TREE_CHAIN (arg))
5111 fprintf (file, ", ");
5112 arg = TREE_CHAIN (arg);
5114 fprintf (file, ")\n");
5116 if (flags & TDF_RAW)
5118 dump_node (fn, TDF_SLIM | flags, file);
5122 /* When GIMPLE is lowered, the variables are no longer available in
5123 BIND_EXPRs, so display them separately. */
5124 if (cfun && cfun->unexpanded_var_list)
5126 ignore_topmost_bind = true;
5128 fprintf (file, "{\n");
5129 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
5131 var = TREE_VALUE (vars);
5133 print_generic_decl (file, var, flags);
5134 fprintf (file, "\n");
5140 if (basic_block_info)
5142 /* Make a CFG based dump. */
5143 check_bb_profile (ENTRY_BLOCK_PTR, file);
5144 if (!ignore_topmost_bind)
5145 fprintf (file, "{\n");
5147 if (any_var && n_basic_blocks)
5148 fprintf (file, "\n");
5151 dump_generic_bb (file, bb, 2, flags);
5153 fprintf (file, "}\n");
5154 check_bb_profile (EXIT_BLOCK_PTR, file);
5160 /* Make a tree based dump. */
5161 chain = DECL_SAVED_TREE (fn);
5163 if (TREE_CODE (chain) == BIND_EXPR)
5165 if (ignore_topmost_bind)
5167 chain = BIND_EXPR_BODY (chain);
5175 if (!ignore_topmost_bind)
5176 fprintf (file, "{\n");
5181 fprintf (file, "\n");
5183 print_generic_stmt_indented (file, chain, flags, indent);
5184 if (ignore_topmost_bind)
5185 fprintf (file, "}\n");
5188 fprintf (file, "\n\n");
5192 /* Pretty print of the loops intermediate representation. */
5193 static void print_loop (FILE *, struct loop *, int);
5194 static void print_pred_bbs (FILE *, basic_block bb);
5195 static void print_succ_bbs (FILE *, basic_block bb);
5198 /* Print the predecessors indexes of edge E on FILE. */
5201 print_pred_bbs (FILE *file, basic_block bb)
5206 FOR_EACH_EDGE (e, ei, bb->preds)
5207 fprintf (file, "bb_%d", e->src->index);
5211 /* Print the successors indexes of edge E on FILE. */
5214 print_succ_bbs (FILE *file, basic_block bb)
5219 FOR_EACH_EDGE (e, ei, bb->succs)
5220 fprintf (file, "bb_%d", e->src->index);
5224 /* Pretty print LOOP on FILE, indented INDENT spaces. */
5227 print_loop (FILE *file, struct loop *loop, int indent)
5235 s_indent = (char *) alloca ((size_t) indent + 1);
5236 memset ((void *) s_indent, ' ', (size_t) indent);
5237 s_indent[indent] = '\0';
5239 /* Print the loop's header. */
5240 fprintf (file, "%sloop_%d\n", s_indent, loop->num);
5242 /* Print the loop's body. */
5243 fprintf (file, "%s{\n", s_indent);
5245 if (bb->loop_father == loop)
5247 /* Print the basic_block's header. */
5248 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
5249 print_pred_bbs (file, bb);
5250 fprintf (file, "}, succs = {");
5251 print_succ_bbs (file, bb);
5252 fprintf (file, "})\n");
5254 /* Print the basic_block's body. */
5255 fprintf (file, "%s {\n", s_indent);
5256 tree_dump_bb (bb, file, indent + 4);
5257 fprintf (file, "%s }\n", s_indent);
5260 print_loop (file, loop->inner, indent + 2);
5261 fprintf (file, "%s}\n", s_indent);
5262 print_loop (file, loop->next, indent);
5266 /* Follow a CFG edge from the entry point of the program, and on entry
5267 of a loop, pretty print the loop structure on FILE. */
5270 print_loop_ir (FILE *file)
5274 bb = BASIC_BLOCK (0);
5275 if (bb && bb->loop_father)
5276 print_loop (file, bb->loop_father, 0);
5280 /* Debugging loops structure at tree level. */
5283 debug_loop_ir (void)
5285 print_loop_ir (stderr);
5289 /* Return true if BB ends with a call, possibly followed by some
5290 instructions that must stay with the call. Return false,
5294 tree_block_ends_with_call_p (basic_block bb)
5296 block_stmt_iterator bsi = bsi_last (bb);
5297 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
5301 /* Return true if BB ends with a conditional branch. Return false,
5305 tree_block_ends_with_condjump_p (basic_block bb)
5307 tree stmt = tsi_stmt (bsi_last (bb).tsi);
5308 return (TREE_CODE (stmt) == COND_EXPR);
5312 /* Return true if we need to add fake edge to exit at statement T.
5313 Helper function for tree_flow_call_edges_add. */
5316 need_fake_edge_p (tree t)
5320 /* NORETURN and LONGJMP calls already have an edge to exit.
5321 CONST, PURE and ALWAYS_RETURN calls do not need one.
5322 We don't currently check for CONST and PURE here, although
5323 it would be a good idea, because those attributes are
5324 figured out from the RTL in mark_constant_function, and
5325 the counter incrementation code from -fprofile-arcs
5326 leads to different results from -fbranch-probabilities. */
5327 call = get_call_expr_in (t);
5329 && !(call_expr_flags (call) & (ECF_NORETURN | ECF_ALWAYS_RETURN)))
5332 if (TREE_CODE (t) == ASM_EXPR
5333 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
5340 /* Add fake edges to the function exit for any non constant and non
5341 noreturn calls, volatile inline assembly in the bitmap of blocks
5342 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
5343 the number of blocks that were split.
5345 The goal is to expose cases in which entering a basic block does
5346 not imply that all subsequent instructions must be executed. */
5349 tree_flow_call_edges_add (sbitmap blocks)
5352 int blocks_split = 0;
5353 int last_bb = last_basic_block;
5354 bool check_last_block = false;
5356 if (n_basic_blocks == 0)
5360 check_last_block = true;
5362 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
5364 /* In the last basic block, before epilogue generation, there will be
5365 a fallthru edge to EXIT. Special care is required if the last insn
5366 of the last basic block is a call because make_edge folds duplicate
5367 edges, which would result in the fallthru edge also being marked
5368 fake, which would result in the fallthru edge being removed by
5369 remove_fake_edges, which would result in an invalid CFG.
5371 Moreover, we can't elide the outgoing fake edge, since the block
5372 profiler needs to take this into account in order to solve the minimal
5373 spanning tree in the case that the call doesn't return.
5375 Handle this by adding a dummy instruction in a new last basic block. */
5376 if (check_last_block)
5378 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
5379 block_stmt_iterator bsi = bsi_last (bb);
5381 if (!bsi_end_p (bsi))
5384 if (need_fake_edge_p (t))
5388 e = find_edge (bb, EXIT_BLOCK_PTR);
5391 bsi_insert_on_edge (e, build_empty_stmt ());
5392 bsi_commit_edge_inserts ();
5397 /* Now add fake edges to the function exit for any non constant
5398 calls since there is no way that we can determine if they will
5400 for (i = 0; i < last_bb; i++)
5402 basic_block bb = BASIC_BLOCK (i);
5403 block_stmt_iterator bsi;
5404 tree stmt, last_stmt;
5409 if (blocks && !TEST_BIT (blocks, i))
5412 bsi = bsi_last (bb);
5413 if (!bsi_end_p (bsi))
5415 last_stmt = bsi_stmt (bsi);
5418 stmt = bsi_stmt (bsi);
5419 if (need_fake_edge_p (stmt))
5422 /* The handling above of the final block before the
5423 epilogue should be enough to verify that there is
5424 no edge to the exit block in CFG already.
5425 Calling make_edge in such case would cause us to
5426 mark that edge as fake and remove it later. */
5427 #ifdef ENABLE_CHECKING
5428 if (stmt == last_stmt)
5430 e = find_edge (bb, EXIT_BLOCK_PTR);
5431 gcc_assert (e == NULL);
5435 /* Note that the following may create a new basic block
5436 and renumber the existing basic blocks. */
5437 if (stmt != last_stmt)
5439 e = split_block (bb, stmt);
5443 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
5447 while (!bsi_end_p (bsi));
5452 verify_flow_info ();
5454 return blocks_split;
5458 tree_purge_dead_eh_edges (basic_block bb)
5460 bool changed = false;
5463 tree stmt = last_stmt (bb);
5465 if (stmt && tree_can_throw_internal (stmt))
5468 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5470 if (e->flags & EDGE_EH)
5479 /* Removal of dead EH edges might change dominators of not
5480 just immediate successors. E.g. when bb1 is changed so that
5481 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5482 eh edges purged by this function in:
5494 idom(bb5) must be recomputed. For now just free the dominance
5497 free_dominance_info (CDI_DOMINATORS);
5503 tree_purge_all_dead_eh_edges (bitmap blocks)
5505 bool changed = false;
5509 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
5511 changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
5517 /* This function is called whenever a new edge is created or
5521 tree_execute_on_growing_pred (edge e)
5523 basic_block bb = e->dest;
5526 reserve_phi_args_for_new_edge (bb);
5529 /* This function is called immediately before edge E is removed from
5530 the edge vector E->dest->preds. */
5533 tree_execute_on_shrinking_pred (edge e)
5535 if (phi_nodes (e->dest))
5536 remove_phi_args (e);
5539 struct cfg_hooks tree_cfg_hooks = {
5541 tree_verify_flow_info,
5542 tree_dump_bb, /* dump_bb */
5543 create_bb, /* create_basic_block */
5544 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
5545 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
5546 remove_bb, /* delete_basic_block */
5547 tree_split_block, /* split_block */
5548 tree_move_block_after, /* move_block_after */
5549 tree_can_merge_blocks_p, /* can_merge_blocks_p */
5550 tree_merge_blocks, /* merge_blocks */
5551 tree_predict_edge, /* predict_edge */
5552 tree_predicted_by_p, /* predicted_by_p */
5553 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
5554 tree_duplicate_bb, /* duplicate_block */
5555 tree_split_edge, /* split_edge */
5556 tree_make_forwarder_block, /* make_forward_block */
5557 NULL, /* tidy_fallthru_edge */
5558 tree_block_ends_with_call_p, /* block_ends_with_call_p */
5559 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
5560 tree_flow_call_edges_add, /* flow_call_edges_add */
5561 tree_execute_on_growing_pred, /* execute_on_growing_pred */
5562 tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
5566 /* Split all critical edges. */
5569 split_critical_edges (void)
5575 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
5576 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
5577 mappings around the calls to split_edge. */
5578 start_recording_case_labels ();
5581 FOR_EACH_EDGE (e, ei, bb->succs)
5582 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
5587 end_recording_case_labels ();
5590 struct tree_opt_pass pass_split_crit_edges =
5592 "crited", /* name */
5594 split_critical_edges, /* execute */
5597 0, /* static_pass_number */
5598 TV_TREE_SPLIT_EDGES, /* tv_id */
5599 PROP_cfg, /* properties required */
5600 PROP_no_crit_edges, /* properties_provided */
5601 0, /* properties_destroyed */
5602 0, /* todo_flags_start */
5603 TODO_dump_func, /* todo_flags_finish */
5608 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5609 a temporary, make sure and register it to be renamed if necessary,
5610 and finally return the temporary. Put the statements to compute
5611 EXP before the current statement in BSI. */
5614 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
5616 tree t, new_stmt, orig_stmt;
5618 if (is_gimple_val (exp))
5621 t = make_rename_temp (type, NULL);
5622 new_stmt = build (MODIFY_EXPR, type, t, exp);
5624 orig_stmt = bsi_stmt (*bsi);
5625 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
5626 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
5628 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
5633 /* Build a ternary operation and gimplify it. Emit code before BSI.
5634 Return the gimple_val holding the result. */
5637 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
5638 tree type, tree a, tree b, tree c)
5642 ret = fold (build3 (code, type, a, b, c));
5645 return gimplify_val (bsi, type, ret);
5648 /* Build a binary operation and gimplify it. Emit code before BSI.
5649 Return the gimple_val holding the result. */
5652 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
5653 tree type, tree a, tree b)
5657 ret = fold (build2 (code, type, a, b));
5660 return gimplify_val (bsi, type, ret);
5663 /* Build a unary operation and gimplify it. Emit code before BSI.
5664 Return the gimple_val holding the result. */
5667 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
5672 ret = fold (build1 (code, type, a));
5675 return gimplify_val (bsi, type, ret);
5680 /* Emit return warnings. */
5683 execute_warn_function_return (void)
5685 #ifdef USE_MAPPED_LOCATION
5686 source_location location;
5694 if (warn_missing_noreturn
5695 && !TREE_THIS_VOLATILE (cfun->decl)
5696 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
5697 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
5698 warning ("%Jfunction might be possible candidate for "
5699 "attribute %<noreturn%>",
5702 /* If we have a path to EXIT, then we do return. */
5703 if (TREE_THIS_VOLATILE (cfun->decl)
5704 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
5706 #ifdef USE_MAPPED_LOCATION
5707 location = UNKNOWN_LOCATION;
5711 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5713 last = last_stmt (e->src);
5714 if (TREE_CODE (last) == RETURN_EXPR
5715 #ifdef USE_MAPPED_LOCATION
5716 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
5718 && (locus = EXPR_LOCUS (last)) != NULL)
5722 #ifdef USE_MAPPED_LOCATION
5723 if (location == UNKNOWN_LOCATION)
5724 location = cfun->function_end_locus;
5725 warning ("%H%<noreturn%> function does return", &location);
5728 locus = &cfun->function_end_locus;
5729 warning ("%H%<noreturn%> function does return", locus);
5733 /* If we see "return;" in some basic block, then we do reach the end
5734 without returning a value. */
5735 else if (warn_return_type
5736 && !TREE_NO_WARNING (cfun->decl)
5737 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
5738 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
5740 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5742 tree last = last_stmt (e->src);
5743 if (TREE_CODE (last) == RETURN_EXPR
5744 && TREE_OPERAND (last, 0) == NULL)
5746 #ifdef USE_MAPPED_LOCATION
5747 location = EXPR_LOCATION (last);
5748 if (location == UNKNOWN_LOCATION)
5749 location = cfun->function_end_locus;
5750 warning ("%Hcontrol reaches end of non-void function", &location);
5752 locus = EXPR_LOCUS (last);
5754 locus = &cfun->function_end_locus;
5755 warning ("%Hcontrol reaches end of non-void function", locus);
5757 TREE_NO_WARNING (cfun->decl) = 1;
5765 /* Given a basic block B which ends with a conditional and has
5766 precisely two successors, determine which of the edges is taken if
5767 the conditional is true and which is taken if the conditional is
5768 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5771 extract_true_false_edges_from_block (basic_block b,
5775 edge e = EDGE_SUCC (b, 0);
5777 if (e->flags & EDGE_TRUE_VALUE)
5780 *false_edge = EDGE_SUCC (b, 1);
5785 *true_edge = EDGE_SUCC (b, 1);
5789 struct tree_opt_pass pass_warn_function_return =
5793 execute_warn_function_return, /* execute */
5796 0, /* static_pass_number */
5798 PROP_cfg, /* properties_required */
5799 0, /* properties_provided */
5800 0, /* properties_destroyed */
5801 0, /* todo_flags_start */
5802 0, /* todo_flags_finish */
5806 #include "gt-tree-cfg.h"