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_computed_goto (basic_block
, tree
);
133 static edge
find_taken_edge_cond_expr (basic_block
, tree
);
134 static edge
find_taken_edge_switch_expr (basic_block
, tree
);
135 static tree
find_case_label_for_value (tree
, tree
);
136 static bool phi_alternatives_equal (basic_block
, edge
, edge
);
137 static bool cleanup_forwarder_blocks (void);
140 /*---------------------------------------------------------------------------
142 ---------------------------------------------------------------------------*/
144 /* Entry point to the CFG builder for trees. TP points to the list of
145 statements to be added to the flowgraph. */
148 build_tree_cfg (tree
*tp
)
150 /* Register specific tree functions. */
151 tree_register_cfg_hooks ();
153 /* Initialize rbi_pool. */
156 /* Initialize the basic block array. */
158 profile_status
= PROFILE_ABSENT
;
160 last_basic_block
= 0;
161 VARRAY_BB_INIT (basic_block_info
, initial_cfg_capacity
, "basic_block_info");
162 memset ((void *) &cfg_stats
, 0, sizeof (cfg_stats
));
164 /* Build a mapping of labels to their associated blocks. */
165 VARRAY_BB_INIT (label_to_block_map
, initial_cfg_capacity
,
166 "label to block map");
168 ENTRY_BLOCK_PTR
->next_bb
= EXIT_BLOCK_PTR
;
169 EXIT_BLOCK_PTR
->prev_bb
= ENTRY_BLOCK_PTR
;
171 found_computed_goto
= 0;
174 /* Computed gotos are hell to deal with, especially if there are
175 lots of them with a large number of destinations. So we factor
176 them to a common computed goto location before we build the
177 edge list. After we convert back to normal form, we will un-factor
178 the computed gotos since factoring introduces an unwanted jump. */
179 if (found_computed_goto
)
180 factor_computed_gotos ();
182 /* Make sure there is always at least one block, even if it's empty. */
183 if (n_basic_blocks
== 0)
184 create_empty_bb (ENTRY_BLOCK_PTR
);
186 create_block_annotation (ENTRY_BLOCK_PTR
);
187 create_block_annotation (EXIT_BLOCK_PTR
);
189 /* Adjust the size of the array. */
190 VARRAY_GROW (basic_block_info
, n_basic_blocks
);
192 /* To speed up statement iterator walks, we first purge dead labels. */
193 cleanup_dead_labels ();
195 /* Group case nodes to reduce the number of edges.
196 We do this after cleaning up dead labels because otherwise we miss
197 a lot of obvious case merging opportunities. */
198 group_case_labels ();
200 /* Create the edges of the flowgraph. */
203 /* Debugging dumps. */
205 /* Write the flowgraph to a VCG file. */
207 int local_dump_flags
;
208 FILE *dump_file
= dump_begin (TDI_vcg
, &local_dump_flags
);
211 tree_cfg2vcg (dump_file
);
212 dump_end (TDI_vcg
, dump_file
);
216 /* Dump a textual representation of the flowgraph. */
218 dump_tree_cfg (dump_file
, dump_flags
);
222 execute_build_cfg (void)
224 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl
));
227 struct tree_opt_pass pass_build_cfg
=
231 execute_build_cfg
, /* execute */
234 0, /* static_pass_number */
235 TV_TREE_CFG
, /* tv_id */
236 PROP_gimple_leh
, /* properties_required */
237 PROP_cfg
, /* properties_provided */
238 0, /* properties_destroyed */
239 0, /* todo_flags_start */
240 TODO_verify_stmts
, /* todo_flags_finish */
244 /* Search the CFG for any computed gotos. If found, factor them to a
245 common computed goto site. Also record the location of that site so
246 that we can un-factor the gotos after we have converted back to
250 factor_computed_gotos (void)
253 tree factored_label_decl
= NULL
;
255 tree factored_computed_goto_label
= NULL
;
256 tree factored_computed_goto
= NULL
;
258 /* We know there are one or more computed gotos in this function.
259 Examine the last statement in each basic block to see if the block
260 ends with a computed goto. */
264 block_stmt_iterator bsi
= bsi_last (bb
);
269 last
= bsi_stmt (bsi
);
271 /* Ignore the computed goto we create when we factor the original
273 if (last
== factored_computed_goto
)
276 /* If the last statement is a computed goto, factor it. */
277 if (computed_goto_p (last
))
281 /* The first time we find a computed goto we need to create
282 the factored goto block and the variable each original
283 computed goto will use for their goto destination. */
284 if (! factored_computed_goto
)
286 basic_block new_bb
= create_empty_bb (bb
);
287 block_stmt_iterator new_bsi
= bsi_start (new_bb
);
289 /* Create the destination of the factored goto. Each original
290 computed goto will put its desired destination into this
291 variable and jump to the label we create immediately
293 var
= create_tmp_var (ptr_type_node
, "gotovar");
295 /* Build a label for the new block which will contain the
296 factored computed goto. */
297 factored_label_decl
= create_artificial_label ();
298 factored_computed_goto_label
299 = build1 (LABEL_EXPR
, void_type_node
, factored_label_decl
);
300 bsi_insert_after (&new_bsi
, factored_computed_goto_label
,
303 /* Build our new computed goto. */
304 factored_computed_goto
= build1 (GOTO_EXPR
, void_type_node
, var
);
305 bsi_insert_after (&new_bsi
, factored_computed_goto
,
309 /* Copy the original computed goto's destination into VAR. */
310 assignment
= build (MODIFY_EXPR
, ptr_type_node
,
311 var
, GOTO_DESTINATION (last
));
312 bsi_insert_before (&bsi
, assignment
, BSI_SAME_STMT
);
314 /* And re-vector the computed goto to the new destination. */
315 GOTO_DESTINATION (last
) = factored_label_decl
;
321 /* Create annotations for a single basic block. */
324 create_block_annotation (basic_block bb
)
326 /* Verify that the tree_annotations field is clear. */
327 gcc_assert (!bb
->tree_annotations
);
328 bb
->tree_annotations
= ggc_alloc_cleared (sizeof (struct bb_ann_d
));
332 /* Free the annotations for all the basic blocks. */
334 static void free_blocks_annotations (void)
336 clear_blocks_annotations ();
340 /* Clear the annotations for all the basic blocks. */
343 clear_blocks_annotations (void)
347 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
348 bb
->tree_annotations
= NULL
;
352 /* Build a flowgraph for the statement_list STMT_LIST. */
355 make_blocks (tree stmt_list
)
357 tree_stmt_iterator i
= tsi_start (stmt_list
);
359 bool start_new_block
= true;
360 bool first_stmt_of_list
= true;
361 basic_block bb
= ENTRY_BLOCK_PTR
;
363 while (!tsi_end_p (i
))
370 /* If the statement starts a new basic block or if we have determined
371 in a previous pass that we need to create a new block for STMT, do
373 if (start_new_block
|| stmt_starts_bb_p (stmt
, prev_stmt
))
375 if (!first_stmt_of_list
)
376 stmt_list
= tsi_split_statement_list_before (&i
);
377 bb
= create_basic_block (stmt_list
, NULL
, bb
);
378 start_new_block
= false;
381 /* Now add STMT to BB and create the subgraphs for special statement
383 set_bb_for_stmt (stmt
, bb
);
385 if (computed_goto_p (stmt
))
386 found_computed_goto
= true;
388 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
390 if (stmt_ends_bb_p (stmt
))
391 start_new_block
= true;
394 first_stmt_of_list
= false;
399 /* Create and return a new empty basic block after bb AFTER. */
402 create_bb (void *h
, void *e
, basic_block after
)
408 /* Create and initialize a new basic block. Since alloc_block uses
409 ggc_alloc_cleared to allocate a basic block, we do not have to
410 clear the newly allocated basic block here. */
413 bb
->index
= last_basic_block
;
415 bb
->stmt_list
= h
? h
: alloc_stmt_list ();
417 /* Add the new block to the linked list of blocks. */
418 link_block (bb
, after
);
420 /* Grow the basic block array if needed. */
421 if ((size_t) last_basic_block
== VARRAY_SIZE (basic_block_info
))
423 size_t new_size
= last_basic_block
+ (last_basic_block
+ 3) / 4;
424 VARRAY_GROW (basic_block_info
, new_size
);
427 /* Add the newly created block to the array. */
428 BASIC_BLOCK (last_basic_block
) = bb
;
430 create_block_annotation (bb
);
435 initialize_bb_rbi (bb
);
440 /*---------------------------------------------------------------------------
442 ---------------------------------------------------------------------------*/
444 /* Fold COND_EXPR_COND of each COND_EXPR. */
447 fold_cond_expr_cond (void)
453 tree stmt
= last_stmt (bb
);
456 && TREE_CODE (stmt
) == COND_EXPR
)
458 tree cond
= fold (COND_EXPR_COND (stmt
));
459 if (integer_zerop (cond
))
460 COND_EXPR_COND (stmt
) = integer_zero_node
;
461 else if (integer_onep (cond
))
462 COND_EXPR_COND (stmt
) = integer_one_node
;
467 /* Join all the blocks in the flowgraph. */
474 /* Create an edge from entry to the first block with executable
476 make_edge (ENTRY_BLOCK_PTR
, BASIC_BLOCK (0), EDGE_FALLTHRU
);
478 /* Traverse the basic block array placing edges. */
481 tree first
= first_stmt (bb
);
482 tree last
= last_stmt (bb
);
486 /* Edges for statements that always alter flow control. */
487 if (is_ctrl_stmt (last
))
488 make_ctrl_stmt_edges (bb
);
490 /* Edges for statements that sometimes alter flow control. */
491 if (is_ctrl_altering_stmt (last
))
492 make_exit_edges (bb
);
495 /* Finally, if no edges were created above, this is a regular
496 basic block that only needs a fallthru edge. */
497 if (EDGE_COUNT (bb
->succs
) == 0)
498 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
501 /* We do not care about fake edges, so remove any that the CFG
502 builder inserted for completeness. */
503 remove_fake_exit_edges ();
505 /* Fold COND_EXPR_COND of each COND_EXPR. */
506 fold_cond_expr_cond ();
508 /* Clean up the graph and warn for unreachable code. */
513 /* Create edges for control statement at basic block BB. */
516 make_ctrl_stmt_edges (basic_block bb
)
518 tree last
= last_stmt (bb
);
521 switch (TREE_CODE (last
))
524 make_goto_expr_edges (bb
);
528 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
532 make_cond_expr_edges (bb
);
536 make_switch_expr_edges (bb
);
540 make_eh_edges (last
);
541 /* Yet another NORETURN hack. */
542 if (EDGE_COUNT (bb
->succs
) == 0)
543 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
552 /* Create exit edges for statements in block BB that alter the flow of
553 control. Statements that alter the control flow are 'goto', 'return'
554 and calls to non-returning functions. */
557 make_exit_edges (basic_block bb
)
559 tree last
= last_stmt (bb
), op
;
562 switch (TREE_CODE (last
))
565 /* If this function receives a nonlocal goto, then we need to
566 make edges from this call site to all the nonlocal goto
568 if (TREE_SIDE_EFFECTS (last
)
569 && current_function_has_nonlocal_label
)
570 make_goto_expr_edges (bb
);
572 /* If this statement has reachable exception handlers, then
573 create abnormal edges to them. */
574 make_eh_edges (last
);
576 /* Some calls are known not to return. For such calls we create
579 We really need to revamp how we build edges so that it's not
580 such a bloody pain to avoid creating edges for this case since
581 all we do is remove these edges when we're done building the
583 if (call_expr_flags (last
) & ECF_NORETURN
)
585 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
589 /* Don't forget the fall-thru edge. */
590 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
594 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
595 may have an abnormal edge. Search the RHS for this case and
596 create any required edges. */
597 op
= get_call_expr_in (last
);
598 if (op
&& TREE_SIDE_EFFECTS (op
)
599 && current_function_has_nonlocal_label
)
600 make_goto_expr_edges (bb
);
602 make_eh_edges (last
);
603 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
612 /* Create the edges for a COND_EXPR starting at block BB.
613 At this point, both clauses must contain only simple gotos. */
616 make_cond_expr_edges (basic_block bb
)
618 tree entry
= last_stmt (bb
);
619 basic_block then_bb
, else_bb
;
620 tree then_label
, else_label
;
623 gcc_assert (TREE_CODE (entry
) == COND_EXPR
);
625 /* Entry basic blocks for each component. */
626 then_label
= GOTO_DESTINATION (COND_EXPR_THEN (entry
));
627 else_label
= GOTO_DESTINATION (COND_EXPR_ELSE (entry
));
628 then_bb
= label_to_block (then_label
);
629 else_bb
= label_to_block (else_label
);
631 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
632 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
635 /* Hashing routine for EDGE_TO_CASES. */
638 edge_to_cases_hash (const void *p
)
640 edge e
= ((struct edge_to_cases_elt
*)p
)->e
;
642 /* Hash on the edge itself (which is a pointer). */
643 return htab_hash_pointer (e
);
646 /* Equality routine for EDGE_TO_CASES, edges are unique, so testing
647 for equality is just a pointer comparison. */
650 edge_to_cases_eq (const void *p1
, const void *p2
)
652 edge e1
= ((struct edge_to_cases_elt
*)p1
)->e
;
653 edge e2
= ((struct edge_to_cases_elt
*)p2
)->e
;
658 /* Called for each element in the hash table (P) as we delete the
659 edge to cases hash table.
661 Clear all the TREE_CHAINs to prevent problems with copying of
662 SWITCH_EXPRs and structure sharing rules, then free the hash table
666 edge_to_cases_cleanup (void *p
)
668 struct edge_to_cases_elt
*elt
= p
;
671 for (t
= elt
->case_labels
; t
; t
= next
)
673 next
= TREE_CHAIN (t
);
674 TREE_CHAIN (t
) = NULL
;
679 /* Start recording information mapping edges to case labels. */
682 start_recording_case_labels (void)
684 gcc_assert (edge_to_cases
== NULL
);
686 edge_to_cases
= htab_create (37,
689 edge_to_cases_cleanup
);
692 /* Return nonzero if we are recording information for case labels. */
695 recording_case_labels_p (void)
697 return (edge_to_cases
!= NULL
);
700 /* Stop recording information mapping edges to case labels and
701 remove any information we have recorded. */
703 end_recording_case_labels (void)
705 htab_delete (edge_to_cases
);
706 edge_to_cases
= NULL
;
709 /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */
712 record_switch_edge (edge e
, tree case_label
)
714 struct edge_to_cases_elt
*elt
;
717 /* Build a hash table element so we can see if E is already
719 elt
= xmalloc (sizeof (struct edge_to_cases_elt
));
721 elt
->case_labels
= case_label
;
723 slot
= htab_find_slot (edge_to_cases
, elt
, INSERT
);
727 /* E was not in the hash table. Install E into the hash table. */
732 /* E was already in the hash table. Free ELT as we do not need it
736 /* Get the entry stored in the hash table. */
737 elt
= (struct edge_to_cases_elt
*) *slot
;
739 /* Add it to the chain of CASE_LABEL_EXPRs referencing E. */
740 TREE_CHAIN (case_label
) = elt
->case_labels
;
741 elt
->case_labels
= case_label
;
745 /* If we are inside a {start,end}_recording_cases block, then return
746 a chain of CASE_LABEL_EXPRs from T which reference E.
748 Otherwise return NULL. */
751 get_cases_for_edge (edge e
, tree t
)
753 struct edge_to_cases_elt elt
, *elt_p
;
758 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
759 chains available. Return NULL so the caller can detect this case. */
760 if (!recording_case_labels_p ())
765 elt
.case_labels
= NULL
;
766 slot
= htab_find_slot (edge_to_cases
, &elt
, NO_INSERT
);
770 elt_p
= (struct edge_to_cases_elt
*)*slot
;
771 return elt_p
->case_labels
;
774 /* If we did not find E in the hash table, then this must be the first
775 time we have been queried for information about E & T. Add all the
776 elements from T to the hash table then perform the query again. */
778 vec
= SWITCH_LABELS (t
);
779 n
= TREE_VEC_LENGTH (vec
);
780 for (i
= 0; i
< n
; i
++)
782 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
783 basic_block label_bb
= label_to_block (lab
);
784 record_switch_edge (find_edge (e
->src
, label_bb
), TREE_VEC_ELT (vec
, i
));
789 /* Create the edges for a SWITCH_EXPR starting at block BB.
790 At this point, the switch body has been lowered and the
791 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
794 make_switch_expr_edges (basic_block bb
)
796 tree entry
= last_stmt (bb
);
800 vec
= SWITCH_LABELS (entry
);
801 n
= TREE_VEC_LENGTH (vec
);
803 for (i
= 0; i
< n
; ++i
)
805 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
806 basic_block label_bb
= label_to_block (lab
);
807 make_edge (bb
, label_bb
, 0);
812 /* Return the basic block holding label DEST. */
815 label_to_block (tree dest
)
817 int uid
= LABEL_DECL_UID (dest
);
819 /* We would die hard when faced by an undefined label. Emit a label to
820 the very first basic block. This will hopefully make even the dataflow
821 and undefined variable warnings quite right. */
822 if ((errorcount
|| sorrycount
) && uid
< 0)
824 block_stmt_iterator bsi
= bsi_start (BASIC_BLOCK (0));
827 stmt
= build1 (LABEL_EXPR
, void_type_node
, dest
);
828 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
829 uid
= LABEL_DECL_UID (dest
);
831 return VARRAY_BB (label_to_block_map
, uid
);
835 /* Create edges for a goto statement at block BB. */
838 make_goto_expr_edges (basic_block bb
)
841 basic_block target_bb
;
843 block_stmt_iterator last
= bsi_last (bb
);
845 goto_t
= bsi_stmt (last
);
847 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
848 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
849 from a nonlocal goto. */
850 if (TREE_CODE (goto_t
) != GOTO_EXPR
)
854 tree dest
= GOTO_DESTINATION (goto_t
);
857 /* A GOTO to a local label creates normal edges. */
858 if (simple_goto_p (goto_t
))
860 edge e
= make_edge (bb
, label_to_block (dest
), EDGE_FALLTHRU
);
861 #ifdef USE_MAPPED_LOCATION
862 e
->goto_locus
= EXPR_LOCATION (goto_t
);
864 e
->goto_locus
= EXPR_LOCUS (goto_t
);
870 /* Nothing more to do for nonlocal gotos. */
871 if (TREE_CODE (dest
) == LABEL_DECL
)
874 /* Computed gotos remain. */
877 /* Look for the block starting with the destination label. In the
878 case of a computed goto, make an edge to any label block we find
880 FOR_EACH_BB (target_bb
)
882 block_stmt_iterator bsi
;
884 for (bsi
= bsi_start (target_bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
886 tree target
= bsi_stmt (bsi
);
888 if (TREE_CODE (target
) != LABEL_EXPR
)
892 /* Computed GOTOs. Make an edge to every label block that has
893 been marked as a potential target for a computed goto. */
894 (FORCED_LABEL (LABEL_EXPR_LABEL (target
)) && for_call
== 0)
895 /* Nonlocal GOTO target. Make an edge to every label block
896 that has been marked as a potential target for a nonlocal
898 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target
)) && for_call
== 1))
900 make_edge (bb
, target_bb
, EDGE_ABNORMAL
);
906 /* Degenerate case of computed goto with no labels. */
907 if (!for_call
&& EDGE_COUNT (bb
->succs
) == 0)
908 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
912 /*---------------------------------------------------------------------------
914 ---------------------------------------------------------------------------*/
916 /* Remove unreachable blocks and other miscellaneous clean up work. */
919 cleanup_tree_cfg (void)
923 timevar_push (TV_TREE_CLEANUP_CFG
);
925 retval
= cleanup_control_flow ();
926 retval
|= delete_unreachable_blocks ();
928 /* cleanup_forwarder_blocks can redirect edges out of SWITCH_EXPRs,
929 which can get expensive. So we want to enable recording of edge
930 to CASE_LABEL_EXPR mappings around the call to
931 cleanup_forwarder_blocks. */
932 start_recording_case_labels ();
933 retval
|= cleanup_forwarder_blocks ();
934 end_recording_case_labels ();
936 #ifdef ENABLE_CHECKING
939 gcc_assert (!cleanup_control_flow ());
940 gcc_assert (!delete_unreachable_blocks ());
941 gcc_assert (!cleanup_forwarder_blocks ());
945 /* Merging the blocks creates no new opportunities for the other
946 optimizations, so do it here. */
947 retval
|= merge_seq_blocks ();
951 #ifdef ENABLE_CHECKING
954 timevar_pop (TV_TREE_CLEANUP_CFG
);
959 /* Cleanup cfg and repair loop structures. */
962 cleanup_tree_cfg_loop (void)
964 bitmap changed_bbs
= BITMAP_ALLOC (NULL
);
968 fix_loop_structure (current_loops
, changed_bbs
);
969 calculate_dominance_info (CDI_DOMINATORS
);
971 /* This usually does nothing. But sometimes parts of cfg that originally
972 were inside a loop get out of it due to edge removal (since they
973 become unreachable by back edges from latch). */
974 rewrite_into_loop_closed_ssa (changed_bbs
);
976 BITMAP_FREE (changed_bbs
);
978 #ifdef ENABLE_CHECKING
979 verify_loop_structure (current_loops
);
983 /* Cleanup useless labels in basic blocks. This is something we wish
984 to do early because it allows us to group case labels before creating
985 the edges for the CFG, and it speeds up block statement iterators in
987 We only run this pass once, running it more than once is probably not
990 /* A map from basic block index to the leading label of that block. */
991 static tree
*label_for_bb
;
993 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
995 update_eh_label (struct eh_region
*region
)
997 tree old_label
= get_eh_region_tree_label (region
);
1001 basic_block bb
= label_to_block (old_label
);
1003 /* ??? After optimizing, there may be EH regions with labels
1004 that have already been removed from the function body, so
1005 there is no basic block for them. */
1009 new_label
= label_for_bb
[bb
->index
];
1010 set_eh_region_tree_label (region
, new_label
);
1014 /* Given LABEL return the first label in the same basic block. */
1016 main_block_label (tree label
)
1018 basic_block bb
= label_to_block (label
);
1020 /* label_to_block possibly inserted undefined label into the chain. */
1021 if (!label_for_bb
[bb
->index
])
1022 label_for_bb
[bb
->index
] = label
;
1023 return label_for_bb
[bb
->index
];
1026 /* Cleanup redundant labels. This is a three-step process:
1027 1) Find the leading label for each block.
1028 2) Redirect all references to labels to the leading labels.
1029 3) Cleanup all useless labels. */
1032 cleanup_dead_labels (void)
1035 label_for_bb
= xcalloc (last_basic_block
, sizeof (tree
));
1037 /* Find a suitable label for each block. We use the first user-defined
1038 label if there is one, or otherwise just the first label we see. */
1041 block_stmt_iterator i
;
1043 for (i
= bsi_start (bb
); !bsi_end_p (i
); bsi_next (&i
))
1045 tree label
, stmt
= bsi_stmt (i
);
1047 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1050 label
= LABEL_EXPR_LABEL (stmt
);
1052 /* If we have not yet seen a label for the current block,
1053 remember this one and see if there are more labels. */
1054 if (! label_for_bb
[bb
->index
])
1056 label_for_bb
[bb
->index
] = label
;
1060 /* If we did see a label for the current block already, but it
1061 is an artificially created label, replace it if the current
1062 label is a user defined label. */
1063 if (! DECL_ARTIFICIAL (label
)
1064 && DECL_ARTIFICIAL (label_for_bb
[bb
->index
]))
1066 label_for_bb
[bb
->index
] = label
;
1072 /* Now redirect all jumps/branches to the selected label.
1073 First do so for each block ending in a control statement. */
1076 tree stmt
= last_stmt (bb
);
1080 switch (TREE_CODE (stmt
))
1084 tree true_branch
, false_branch
;
1086 true_branch
= COND_EXPR_THEN (stmt
);
1087 false_branch
= COND_EXPR_ELSE (stmt
);
1089 GOTO_DESTINATION (true_branch
)
1090 = main_block_label (GOTO_DESTINATION (true_branch
));
1091 GOTO_DESTINATION (false_branch
)
1092 = main_block_label (GOTO_DESTINATION (false_branch
));
1100 tree vec
= SWITCH_LABELS (stmt
);
1101 size_t n
= TREE_VEC_LENGTH (vec
);
1103 /* Replace all destination labels. */
1104 for (i
= 0; i
< n
; ++i
)
1106 tree elt
= TREE_VEC_ELT (vec
, i
);
1107 tree label
= main_block_label (CASE_LABEL (elt
));
1108 CASE_LABEL (elt
) = label
;
1113 /* We have to handle GOTO_EXPRs until they're removed, and we don't
1114 remove them until after we've created the CFG edges. */
1116 if (! computed_goto_p (stmt
))
1118 GOTO_DESTINATION (stmt
)
1119 = main_block_label (GOTO_DESTINATION (stmt
));
1128 for_each_eh_region (update_eh_label
);
1130 /* Finally, purge dead labels. All user-defined labels and labels that
1131 can be the target of non-local gotos are preserved. */
1134 block_stmt_iterator i
;
1135 tree label_for_this_bb
= label_for_bb
[bb
->index
];
1137 if (! label_for_this_bb
)
1140 for (i
= bsi_start (bb
); !bsi_end_p (i
); )
1142 tree label
, stmt
= bsi_stmt (i
);
1144 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1147 label
= LABEL_EXPR_LABEL (stmt
);
1149 if (label
== label_for_this_bb
1150 || ! DECL_ARTIFICIAL (label
)
1151 || DECL_NONLOCAL (label
))
1158 free (label_for_bb
);
1161 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1162 and scan the sorted vector of cases. Combine the ones jumping to the
1164 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1167 group_case_labels (void)
1173 tree stmt
= last_stmt (bb
);
1174 if (stmt
&& TREE_CODE (stmt
) == SWITCH_EXPR
)
1176 tree labels
= SWITCH_LABELS (stmt
);
1177 int old_size
= TREE_VEC_LENGTH (labels
);
1178 int i
, j
, new_size
= old_size
;
1179 tree default_case
= TREE_VEC_ELT (labels
, old_size
- 1);
1182 /* The default label is always the last case in a switch
1183 statement after gimplification. */
1184 default_label
= CASE_LABEL (default_case
);
1186 /* Look for possible opportunities to merge cases.
1187 Ignore the last element of the label vector because it
1188 must be the default case. */
1190 while (i
< old_size
- 1)
1192 tree base_case
, base_label
, base_high
;
1193 base_case
= TREE_VEC_ELT (labels
, i
);
1195 gcc_assert (base_case
);
1196 base_label
= CASE_LABEL (base_case
);
1198 /* Discard cases that have the same destination as the
1200 if (base_label
== default_label
)
1202 TREE_VEC_ELT (labels
, i
) = NULL_TREE
;
1208 base_high
= CASE_HIGH (base_case
) ?
1209 CASE_HIGH (base_case
) : CASE_LOW (base_case
);
1211 /* Try to merge case labels. Break out when we reach the end
1212 of the label vector or when we cannot merge the next case
1213 label with the current one. */
1214 while (i
< old_size
- 1)
1216 tree merge_case
= TREE_VEC_ELT (labels
, i
);
1217 tree merge_label
= CASE_LABEL (merge_case
);
1218 tree t
= int_const_binop (PLUS_EXPR
, base_high
,
1219 integer_one_node
, 1);
1221 /* Merge the cases if they jump to the same place,
1222 and their ranges are consecutive. */
1223 if (merge_label
== base_label
1224 && tree_int_cst_equal (CASE_LOW (merge_case
), t
))
1226 base_high
= CASE_HIGH (merge_case
) ?
1227 CASE_HIGH (merge_case
) : CASE_LOW (merge_case
);
1228 CASE_HIGH (base_case
) = base_high
;
1229 TREE_VEC_ELT (labels
, i
) = NULL_TREE
;
1238 /* Compress the case labels in the label vector, and adjust the
1239 length of the vector. */
1240 for (i
= 0, j
= 0; i
< new_size
; i
++)
1242 while (! TREE_VEC_ELT (labels
, j
))
1244 TREE_VEC_ELT (labels
, i
) = TREE_VEC_ELT (labels
, j
++);
1246 TREE_VEC_LENGTH (labels
) = new_size
;
1251 /* Checks whether we can merge block B into block A. */
1254 tree_can_merge_blocks_p (basic_block a
, basic_block b
)
1257 block_stmt_iterator bsi
;
1259 if (!single_succ_p (a
))
1262 if (single_succ_edge (a
)->flags
& EDGE_ABNORMAL
)
1265 if (single_succ (a
) != b
)
1268 if (!single_pred_p (b
))
1271 if (b
== EXIT_BLOCK_PTR
)
1274 /* If A ends by a statement causing exceptions or something similar, we
1275 cannot merge the blocks. */
1276 stmt
= last_stmt (a
);
1277 if (stmt
&& stmt_ends_bb_p (stmt
))
1280 /* Do not allow a block with only a non-local label to be merged. */
1281 if (stmt
&& TREE_CODE (stmt
) == LABEL_EXPR
1282 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
1285 /* There may be no phi nodes at the start of b. Most of these degenerate
1286 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1290 /* Do not remove user labels. */
1291 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1293 stmt
= bsi_stmt (bsi
);
1294 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1296 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt
)))
1300 /* Protect the loop latches. */
1302 && b
->loop_father
->latch
== b
)
1309 /* Merge block B into block A. */
1312 tree_merge_blocks (basic_block a
, basic_block b
)
1314 block_stmt_iterator bsi
;
1315 tree_stmt_iterator last
;
1318 fprintf (dump_file
, "Merging blocks %d and %d\n", a
->index
, b
->index
);
1320 /* Ensure that B follows A. */
1321 move_block_after (b
, a
);
1323 gcc_assert (single_succ_edge (a
)->flags
& EDGE_FALLTHRU
);
1324 gcc_assert (!last_stmt (a
) || !stmt_ends_bb_p (last_stmt (a
)));
1326 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1327 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
);)
1329 if (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
)
1331 tree label
= bsi_stmt (bsi
);
1334 /* Now that we can thread computed gotos, we might have
1335 a situation where we have a forced label in block B
1336 However, the label at the start of block B might still be
1337 used in other ways (think about the runtime checking for
1338 Fortran assigned gotos). So we can not just delete the
1339 label. Instead we move the label to the start of block A. */
1340 if (FORCED_LABEL (LABEL_EXPR_LABEL (label
)))
1342 block_stmt_iterator dest_bsi
= bsi_start (a
);
1343 bsi_insert_before (&dest_bsi
, label
, BSI_NEW_STMT
);
1348 set_bb_for_stmt (bsi_stmt (bsi
), a
);
1353 /* Merge the chains. */
1354 last
= tsi_last (a
->stmt_list
);
1355 tsi_link_after (&last
, b
->stmt_list
, TSI_NEW_STMT
);
1356 b
->stmt_list
= NULL
;
1360 /* Walk the function tree removing unnecessary statements.
1362 * Empty statement nodes are removed
1364 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1366 * Unnecessary COND_EXPRs are removed
1368 * Some unnecessary BIND_EXPRs are removed
1370 Clearly more work could be done. The trick is doing the analysis
1371 and removal fast enough to be a net improvement in compile times.
1373 Note that when we remove a control structure such as a COND_EXPR
1374 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1375 to ensure we eliminate all the useless code. */
1386 static void remove_useless_stmts_1 (tree
*, struct rus_data
*);
1389 remove_useless_stmts_warn_notreached (tree stmt
)
1391 if (EXPR_HAS_LOCATION (stmt
))
1393 location_t loc
= EXPR_LOCATION (stmt
);
1394 if (LOCATION_LINE (loc
) > 0)
1396 warning ("%Hwill never be executed", &loc
);
1401 switch (TREE_CODE (stmt
))
1403 case STATEMENT_LIST
:
1405 tree_stmt_iterator i
;
1406 for (i
= tsi_start (stmt
); !tsi_end_p (i
); tsi_next (&i
))
1407 if (remove_useless_stmts_warn_notreached (tsi_stmt (i
)))
1413 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt
)))
1415 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt
)))
1417 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt
)))
1421 case TRY_FINALLY_EXPR
:
1422 case TRY_CATCH_EXPR
:
1423 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt
, 0)))
1425 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt
, 1)))
1430 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt
));
1431 case EH_FILTER_EXPR
:
1432 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt
));
1434 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt
));
1437 /* Not a live container. */
1445 remove_useless_stmts_cond (tree
*stmt_p
, struct rus_data
*data
)
1447 tree then_clause
, else_clause
, cond
;
1448 bool save_has_label
, then_has_label
, else_has_label
;
1450 save_has_label
= data
->has_label
;
1451 data
->has_label
= false;
1452 data
->last_goto
= NULL
;
1454 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p
), data
);
1456 then_has_label
= data
->has_label
;
1457 data
->has_label
= false;
1458 data
->last_goto
= NULL
;
1460 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p
), data
);
1462 else_has_label
= data
->has_label
;
1463 data
->has_label
= save_has_label
| then_has_label
| else_has_label
;
1465 then_clause
= COND_EXPR_THEN (*stmt_p
);
1466 else_clause
= COND_EXPR_ELSE (*stmt_p
);
1467 cond
= fold (COND_EXPR_COND (*stmt_p
));
1469 /* If neither arm does anything at all, we can remove the whole IF. */
1470 if (!TREE_SIDE_EFFECTS (then_clause
) && !TREE_SIDE_EFFECTS (else_clause
))
1472 *stmt_p
= build_empty_stmt ();
1473 data
->repeat
= true;
1476 /* If there are no reachable statements in an arm, then we can
1477 zap the entire conditional. */
1478 else if (integer_nonzerop (cond
) && !else_has_label
)
1480 if (warn_notreached
)
1481 remove_useless_stmts_warn_notreached (else_clause
);
1482 *stmt_p
= then_clause
;
1483 data
->repeat
= true;
1485 else if (integer_zerop (cond
) && !then_has_label
)
1487 if (warn_notreached
)
1488 remove_useless_stmts_warn_notreached (then_clause
);
1489 *stmt_p
= else_clause
;
1490 data
->repeat
= true;
1493 /* Check a couple of simple things on then/else with single stmts. */
1496 tree then_stmt
= expr_only (then_clause
);
1497 tree else_stmt
= expr_only (else_clause
);
1499 /* Notice branches to a common destination. */
1500 if (then_stmt
&& else_stmt
1501 && TREE_CODE (then_stmt
) == GOTO_EXPR
1502 && TREE_CODE (else_stmt
) == GOTO_EXPR
1503 && (GOTO_DESTINATION (then_stmt
) == GOTO_DESTINATION (else_stmt
)))
1505 *stmt_p
= then_stmt
;
1506 data
->repeat
= true;
1509 /* If the THEN/ELSE clause merely assigns a value to a variable or
1510 parameter which is already known to contain that value, then
1511 remove the useless THEN/ELSE clause. */
1512 else if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
1515 && TREE_CODE (else_stmt
) == MODIFY_EXPR
1516 && TREE_OPERAND (else_stmt
, 0) == cond
1517 && integer_zerop (TREE_OPERAND (else_stmt
, 1)))
1518 COND_EXPR_ELSE (*stmt_p
) = alloc_stmt_list ();
1520 else if ((TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
1521 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1522 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
1523 && TREE_CONSTANT (TREE_OPERAND (cond
, 1)))
1525 tree stmt
= (TREE_CODE (cond
) == EQ_EXPR
1526 ? then_stmt
: else_stmt
);
1527 tree
*location
= (TREE_CODE (cond
) == EQ_EXPR
1528 ? &COND_EXPR_THEN (*stmt_p
)
1529 : &COND_EXPR_ELSE (*stmt_p
));
1532 && TREE_CODE (stmt
) == MODIFY_EXPR
1533 && TREE_OPERAND (stmt
, 0) == TREE_OPERAND (cond
, 0)
1534 && TREE_OPERAND (stmt
, 1) == TREE_OPERAND (cond
, 1))
1535 *location
= alloc_stmt_list ();
1539 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1540 would be re-introduced during lowering. */
1541 data
->last_goto
= NULL
;
1546 remove_useless_stmts_tf (tree
*stmt_p
, struct rus_data
*data
)
1548 bool save_may_branch
, save_may_throw
;
1549 bool this_may_branch
, this_may_throw
;
1551 /* Collect may_branch and may_throw information for the body only. */
1552 save_may_branch
= data
->may_branch
;
1553 save_may_throw
= data
->may_throw
;
1554 data
->may_branch
= false;
1555 data
->may_throw
= false;
1556 data
->last_goto
= NULL
;
1558 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
1560 this_may_branch
= data
->may_branch
;
1561 this_may_throw
= data
->may_throw
;
1562 data
->may_branch
|= save_may_branch
;
1563 data
->may_throw
|= save_may_throw
;
1564 data
->last_goto
= NULL
;
1566 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
1568 /* If the body is empty, then we can emit the FINALLY block without
1569 the enclosing TRY_FINALLY_EXPR. */
1570 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 0)))
1572 *stmt_p
= TREE_OPERAND (*stmt_p
, 1);
1573 data
->repeat
= true;
1576 /* If the handler is empty, then we can emit the TRY block without
1577 the enclosing TRY_FINALLY_EXPR. */
1578 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 1)))
1580 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1581 data
->repeat
= true;
1584 /* If the body neither throws, nor branches, then we can safely
1585 string the TRY and FINALLY blocks together. */
1586 else if (!this_may_branch
&& !this_may_throw
)
1588 tree stmt
= *stmt_p
;
1589 *stmt_p
= TREE_OPERAND (stmt
, 0);
1590 append_to_statement_list (TREE_OPERAND (stmt
, 1), stmt_p
);
1591 data
->repeat
= true;
1597 remove_useless_stmts_tc (tree
*stmt_p
, struct rus_data
*data
)
1599 bool save_may_throw
, this_may_throw
;
1600 tree_stmt_iterator i
;
1603 /* Collect may_throw information for the body only. */
1604 save_may_throw
= data
->may_throw
;
1605 data
->may_throw
= false;
1606 data
->last_goto
= NULL
;
1608 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
1610 this_may_throw
= data
->may_throw
;
1611 data
->may_throw
= save_may_throw
;
1613 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1614 if (!this_may_throw
)
1616 if (warn_notreached
)
1617 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p
, 1));
1618 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1619 data
->repeat
= true;
1623 /* Process the catch clause specially. We may be able to tell that
1624 no exceptions propagate past this point. */
1626 this_may_throw
= true;
1627 i
= tsi_start (TREE_OPERAND (*stmt_p
, 1));
1628 stmt
= tsi_stmt (i
);
1629 data
->last_goto
= NULL
;
1631 switch (TREE_CODE (stmt
))
1634 for (; !tsi_end_p (i
); tsi_next (&i
))
1636 stmt
= tsi_stmt (i
);
1637 /* If we catch all exceptions, then the body does not
1638 propagate exceptions past this point. */
1639 if (CATCH_TYPES (stmt
) == NULL
)
1640 this_may_throw
= false;
1641 data
->last_goto
= NULL
;
1642 remove_useless_stmts_1 (&CATCH_BODY (stmt
), data
);
1646 case EH_FILTER_EXPR
:
1647 if (EH_FILTER_MUST_NOT_THROW (stmt
))
1648 this_may_throw
= false;
1649 else if (EH_FILTER_TYPES (stmt
) == NULL
)
1650 this_may_throw
= false;
1651 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt
), data
);
1655 /* Otherwise this is a cleanup. */
1656 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
1658 /* If the cleanup is empty, then we can emit the TRY block without
1659 the enclosing TRY_CATCH_EXPR. */
1660 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 1)))
1662 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1663 data
->repeat
= true;
1667 data
->may_throw
|= this_may_throw
;
1672 remove_useless_stmts_bind (tree
*stmt_p
, struct rus_data
*data
)
1676 /* First remove anything underneath the BIND_EXPR. */
1677 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p
), data
);
1679 /* If the BIND_EXPR has no variables, then we can pull everything
1680 up one level and remove the BIND_EXPR, unless this is the toplevel
1681 BIND_EXPR for the current function or an inlined function.
1683 When this situation occurs we will want to apply this
1684 optimization again. */
1685 block
= BIND_EXPR_BLOCK (*stmt_p
);
1686 if (BIND_EXPR_VARS (*stmt_p
) == NULL_TREE
1687 && *stmt_p
!= DECL_SAVED_TREE (current_function_decl
)
1689 || ! BLOCK_ABSTRACT_ORIGIN (block
)
1690 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
))
1693 *stmt_p
= BIND_EXPR_BODY (*stmt_p
);
1694 data
->repeat
= true;
1700 remove_useless_stmts_goto (tree
*stmt_p
, struct rus_data
*data
)
1702 tree dest
= GOTO_DESTINATION (*stmt_p
);
1704 data
->may_branch
= true;
1705 data
->last_goto
= NULL
;
1707 /* Record the last goto expr, so that we can delete it if unnecessary. */
1708 if (TREE_CODE (dest
) == LABEL_DECL
)
1709 data
->last_goto
= stmt_p
;
1714 remove_useless_stmts_label (tree
*stmt_p
, struct rus_data
*data
)
1716 tree label
= LABEL_EXPR_LABEL (*stmt_p
);
1718 data
->has_label
= true;
1720 /* We do want to jump across non-local label receiver code. */
1721 if (DECL_NONLOCAL (label
))
1722 data
->last_goto
= NULL
;
1724 else if (data
->last_goto
&& GOTO_DESTINATION (*data
->last_goto
) == label
)
1726 *data
->last_goto
= build_empty_stmt ();
1727 data
->repeat
= true;
1730 /* ??? Add something here to delete unused labels. */
1734 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1735 decl. This allows us to eliminate redundant or useless
1736 calls to "const" functions.
1738 Gimplifier already does the same operation, but we may notice functions
1739 being const and pure once their calls has been gimplified, so we need
1740 to update the flag. */
1743 update_call_expr_flags (tree call
)
1745 tree decl
= get_callee_fndecl (call
);
1748 if (call_expr_flags (call
) & (ECF_CONST
| ECF_PURE
))
1749 TREE_SIDE_EFFECTS (call
) = 0;
1750 if (TREE_NOTHROW (decl
))
1751 TREE_NOTHROW (call
) = 1;
1755 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1758 notice_special_calls (tree t
)
1760 int flags
= call_expr_flags (t
);
1762 if (flags
& ECF_MAY_BE_ALLOCA
)
1763 current_function_calls_alloca
= true;
1764 if (flags
& ECF_RETURNS_TWICE
)
1765 current_function_calls_setjmp
= true;
1769 /* Clear flags set by notice_special_calls. Used by dead code removal
1770 to update the flags. */
1773 clear_special_calls (void)
1775 current_function_calls_alloca
= false;
1776 current_function_calls_setjmp
= false;
1781 remove_useless_stmts_1 (tree
*tp
, struct rus_data
*data
)
1785 switch (TREE_CODE (t
))
1788 remove_useless_stmts_cond (tp
, data
);
1791 case TRY_FINALLY_EXPR
:
1792 remove_useless_stmts_tf (tp
, data
);
1795 case TRY_CATCH_EXPR
:
1796 remove_useless_stmts_tc (tp
, data
);
1800 remove_useless_stmts_bind (tp
, data
);
1804 remove_useless_stmts_goto (tp
, data
);
1808 remove_useless_stmts_label (tp
, data
);
1813 data
->last_goto
= NULL
;
1814 data
->may_branch
= true;
1819 data
->last_goto
= NULL
;
1820 notice_special_calls (t
);
1821 update_call_expr_flags (t
);
1822 if (tree_could_throw_p (t
))
1823 data
->may_throw
= true;
1827 data
->last_goto
= NULL
;
1829 op
= get_call_expr_in (t
);
1832 update_call_expr_flags (op
);
1833 notice_special_calls (op
);
1835 if (tree_could_throw_p (t
))
1836 data
->may_throw
= true;
1839 case STATEMENT_LIST
:
1841 tree_stmt_iterator i
= tsi_start (t
);
1842 while (!tsi_end_p (i
))
1845 if (IS_EMPTY_STMT (t
))
1851 remove_useless_stmts_1 (tsi_stmt_ptr (i
), data
);
1854 if (TREE_CODE (t
) == STATEMENT_LIST
)
1856 tsi_link_before (&i
, t
, TSI_SAME_STMT
);
1866 data
->last_goto
= NULL
;
1870 data
->last_goto
= NULL
;
1876 remove_useless_stmts (void)
1878 struct rus_data data
;
1880 clear_special_calls ();
1884 memset (&data
, 0, sizeof (data
));
1885 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl
), &data
);
1887 while (data
.repeat
);
1891 struct tree_opt_pass pass_remove_useless_stmts
=
1893 "useless", /* name */
1895 remove_useless_stmts
, /* execute */
1898 0, /* static_pass_number */
1900 PROP_gimple_any
, /* properties_required */
1901 0, /* properties_provided */
1902 0, /* properties_destroyed */
1903 0, /* todo_flags_start */
1904 TODO_dump_func
, /* todo_flags_finish */
1909 /* Remove obviously useless statements in basic block BB. */
1912 cfg_remove_useless_stmts_bb (basic_block bb
)
1914 block_stmt_iterator bsi
;
1915 tree stmt
= NULL_TREE
;
1916 tree cond
, var
= NULL_TREE
, val
= NULL_TREE
;
1917 struct var_ann_d
*ann
;
1919 /* Check whether we come here from a condition, and if so, get the
1921 if (!single_pred_p (bb
)
1922 || !(single_pred_edge (bb
)->flags
1923 & (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
1926 cond
= COND_EXPR_COND (last_stmt (single_pred (bb
)));
1928 if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
1931 val
= (single_pred_edge (bb
)->flags
& EDGE_FALSE_VALUE
1932 ? boolean_false_node
: boolean_true_node
);
1934 else if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
1935 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1936 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
))
1938 var
= TREE_OPERAND (cond
, 0);
1939 val
= (single_pred_edge (bb
)->flags
& EDGE_FALSE_VALUE
1940 ? boolean_true_node
: boolean_false_node
);
1944 if (single_pred_edge (bb
)->flags
& EDGE_FALSE_VALUE
)
1945 cond
= invert_truthvalue (cond
);
1946 if (TREE_CODE (cond
) == EQ_EXPR
1947 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1948 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
1949 && (TREE_CODE (TREE_OPERAND (cond
, 1)) == VAR_DECL
1950 || TREE_CODE (TREE_OPERAND (cond
, 1)) == PARM_DECL
1951 || TREE_CONSTANT (TREE_OPERAND (cond
, 1))))
1953 var
= TREE_OPERAND (cond
, 0);
1954 val
= TREE_OPERAND (cond
, 1);
1960 /* Only work for normal local variables. */
1961 ann
= var_ann (var
);
1964 || TREE_ADDRESSABLE (var
))
1967 if (! TREE_CONSTANT (val
))
1969 ann
= var_ann (val
);
1972 || TREE_ADDRESSABLE (val
))
1976 /* Ignore floating point variables, since comparison behaves weird for
1978 if (FLOAT_TYPE_P (TREE_TYPE (var
)))
1981 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
);)
1983 stmt
= bsi_stmt (bsi
);
1985 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1986 which is already known to contain that value, then remove the useless
1987 THEN/ELSE clause. */
1988 if (TREE_CODE (stmt
) == MODIFY_EXPR
1989 && TREE_OPERAND (stmt
, 0) == var
1990 && operand_equal_p (val
, TREE_OPERAND (stmt
, 1), 0))
1996 /* Invalidate the var if we encounter something that could modify it.
1997 Likewise for the value it was previously set to. Note that we only
1998 consider values that are either a VAR_DECL or PARM_DECL so we
1999 can test for conflict very simply. */
2000 if (TREE_CODE (stmt
) == ASM_EXPR
2001 || (TREE_CODE (stmt
) == MODIFY_EXPR
2002 && (TREE_OPERAND (stmt
, 0) == var
2003 || TREE_OPERAND (stmt
, 0) == val
)))
2011 /* A CFG-aware version of remove_useless_stmts. */
2014 cfg_remove_useless_stmts (void)
2018 #ifdef ENABLE_CHECKING
2019 verify_flow_info ();
2024 cfg_remove_useless_stmts_bb (bb
);
2029 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
2032 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb
)
2036 /* Since this block is no longer reachable, we can just delete all
2037 of its PHI nodes. */
2038 phi
= phi_nodes (bb
);
2041 tree next
= PHI_CHAIN (phi
);
2042 remove_phi_node (phi
, NULL_TREE
);
2046 /* Remove edges to BB's successors. */
2047 while (EDGE_COUNT (bb
->succs
) > 0)
2048 remove_edge (EDGE_SUCC (bb
, 0));
2052 /* Remove statements of basic block BB. */
2055 remove_bb (basic_block bb
)
2057 block_stmt_iterator i
;
2058 #ifdef USE_MAPPED_LOCATION
2059 source_location loc
= UNKNOWN_LOCATION
;
2061 source_locus loc
= 0;
2066 fprintf (dump_file
, "Removing basic block %d\n", bb
->index
);
2067 if (dump_flags
& TDF_DETAILS
)
2069 dump_bb (bb
, dump_file
, 0);
2070 fprintf (dump_file
, "\n");
2074 /* If we remove the header or the latch of a loop, mark the loop for
2075 removal by setting its header and latch to NULL. */
2078 struct loop
*loop
= bb
->loop_father
;
2080 if (loop
->latch
== bb
2081 || loop
->header
== bb
)
2084 loop
->header
= NULL
;
2088 /* Remove all the instructions in the block. */
2089 for (i
= bsi_start (bb
); !bsi_end_p (i
);)
2091 tree stmt
= bsi_stmt (i
);
2092 if (TREE_CODE (stmt
) == LABEL_EXPR
2093 && FORCED_LABEL (LABEL_EXPR_LABEL (stmt
)))
2095 basic_block new_bb
= bb
->prev_bb
;
2096 block_stmt_iterator new_bsi
= bsi_start (new_bb
);
2099 bsi_insert_before (&new_bsi
, stmt
, BSI_NEW_STMT
);
2103 release_defs (stmt
);
2105 set_bb_for_stmt (stmt
, NULL
);
2109 /* Don't warn for removed gotos. Gotos are often removed due to
2110 jump threading, thus resulting in bogus warnings. Not great,
2111 since this way we lose warnings for gotos in the original
2112 program that are indeed unreachable. */
2113 if (TREE_CODE (stmt
) != GOTO_EXPR
&& EXPR_HAS_LOCATION (stmt
) && !loc
)
2115 #ifdef USE_MAPPED_LOCATION
2116 if (EXPR_HAS_LOCATION (stmt
))
2117 loc
= EXPR_LOCATION (stmt
);
2120 t
= EXPR_LOCUS (stmt
);
2121 if (t
&& LOCATION_LINE (*t
) > 0)
2127 /* If requested, give a warning that the first statement in the
2128 block is unreachable. We walk statements backwards in the
2129 loop above, so the last statement we process is the first statement
2131 #ifdef USE_MAPPED_LOCATION
2132 if (warn_notreached
&& loc
!= UNKNOWN_LOCATION
)
2133 warning ("%Hwill never be executed", &loc
);
2135 if (warn_notreached
&& loc
)
2136 warning ("%Hwill never be executed", loc
);
2139 remove_phi_nodes_and_edges_for_unreachable_block (bb
);
2142 /* A list of all the noreturn calls passed to modify_stmt.
2143 cleanup_control_flow uses it to detect cases where a mid-block
2144 indirect call has been turned into a noreturn call. When this
2145 happens, all the instructions after the call are no longer
2146 reachable and must be deleted as dead. */
2148 VEC(tree
) *modified_noreturn_calls
;
2150 /* Try to remove superfluous control structures. */
2153 cleanup_control_flow (void)
2156 block_stmt_iterator bsi
;
2157 bool retval
= false;
2160 /* Detect cases where a mid-block call is now known not to return. */
2161 while (VEC_length (tree
, modified_noreturn_calls
))
2163 stmt
= VEC_pop (tree
, modified_noreturn_calls
);
2164 bb
= bb_for_stmt (stmt
);
2165 if (bb
!= NULL
&& last_stmt (bb
) != stmt
&& noreturn_call_p (stmt
))
2166 split_block (bb
, stmt
);
2171 bsi
= bsi_last (bb
);
2173 if (bsi_end_p (bsi
))
2176 stmt
= bsi_stmt (bsi
);
2177 if (TREE_CODE (stmt
) == COND_EXPR
2178 || TREE_CODE (stmt
) == SWITCH_EXPR
)
2179 retval
|= cleanup_control_expr_graph (bb
, bsi
);
2181 /* If we had a computed goto which has a compile-time determinable
2182 destination, then we can eliminate the goto. */
2183 if (TREE_CODE (stmt
) == GOTO_EXPR
2184 && TREE_CODE (GOTO_DESTINATION (stmt
)) == ADDR_EXPR
2185 && TREE_CODE (TREE_OPERAND (GOTO_DESTINATION (stmt
), 0)) == LABEL_DECL
)
2190 basic_block target_block
;
2191 bool removed_edge
= false;
2193 /* First look at all the outgoing edges. Delete any outgoing
2194 edges which do not go to the right block. For the one
2195 edge which goes to the right block, fix up its flags. */
2196 label
= TREE_OPERAND (GOTO_DESTINATION (stmt
), 0);
2197 target_block
= label_to_block (label
);
2198 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
2200 if (e
->dest
!= target_block
)
2202 removed_edge
= true;
2207 /* Turn off the EDGE_ABNORMAL flag. */
2208 e
->flags
&= ~EDGE_ABNORMAL
;
2210 /* And set EDGE_FALLTHRU. */
2211 e
->flags
|= EDGE_FALLTHRU
;
2216 /* If we removed one or more edges, then we will need to fix the
2217 dominators. It may be possible to incrementally update them. */
2219 free_dominance_info (CDI_DOMINATORS
);
2221 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
2222 relevant information we need. */
2227 /* Check for indirect calls that have been turned into
2229 if (noreturn_call_p (stmt
) && remove_fallthru_edge (bb
->succs
))
2231 free_dominance_info (CDI_DOMINATORS
);
2239 /* Disconnect an unreachable block in the control expression starting
2243 cleanup_control_expr_graph (basic_block bb
, block_stmt_iterator bsi
)
2246 bool retval
= false;
2247 tree expr
= bsi_stmt (bsi
), val
;
2249 if (!single_succ_p (bb
))
2254 switch (TREE_CODE (expr
))
2257 val
= COND_EXPR_COND (expr
);
2261 val
= SWITCH_COND (expr
);
2262 if (TREE_CODE (val
) != INTEGER_CST
)
2270 taken_edge
= find_taken_edge (bb
, val
);
2274 /* Remove all the edges except the one that is always executed. */
2275 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
2277 if (e
!= taken_edge
)
2279 taken_edge
->probability
+= e
->probability
;
2280 taken_edge
->count
+= e
->count
;
2287 if (taken_edge
->probability
> REG_BR_PROB_BASE
)
2288 taken_edge
->probability
= REG_BR_PROB_BASE
;
2291 taken_edge
= single_succ_edge (bb
);
2294 taken_edge
->flags
= EDGE_FALLTHRU
;
2296 /* We removed some paths from the cfg. */
2297 free_dominance_info (CDI_DOMINATORS
);
2302 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
2305 remove_fallthru_edge (VEC(edge
) *ev
)
2310 FOR_EACH_EDGE (e
, ei
, ev
)
2311 if ((e
->flags
& EDGE_FALLTHRU
) != 0)
2319 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2320 predicate VAL, return the edge that will be taken out of the block.
2321 If VAL does not match a unique edge, NULL is returned. */
2324 find_taken_edge (basic_block bb
, tree val
)
2328 stmt
= last_stmt (bb
);
2331 gcc_assert (is_ctrl_stmt (stmt
));
2334 if (! is_gimple_min_invariant (val
))
2337 if (TREE_CODE (stmt
) == COND_EXPR
)
2338 return find_taken_edge_cond_expr (bb
, val
);
2340 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
2341 return find_taken_edge_switch_expr (bb
, val
);
2343 if (computed_goto_p (stmt
))
2344 return find_taken_edge_computed_goto (bb
, TREE_OPERAND( val
, 0));
2349 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2350 statement, determine which of the outgoing edges will be taken out of the
2351 block. Return NULL if either edge may be taken. */
2354 find_taken_edge_computed_goto (basic_block bb
, tree val
)
2359 dest
= label_to_block (val
);
2362 e
= find_edge (bb
, dest
);
2363 gcc_assert (e
!= NULL
);
2369 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2370 statement, determine which of the two edges will be taken out of the
2371 block. Return NULL if either edge may be taken. */
2374 find_taken_edge_cond_expr (basic_block bb
, tree val
)
2376 edge true_edge
, false_edge
;
2378 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
2380 /* Otherwise, try to determine which branch of the if() will be taken.
2381 If VAL is a constant but it can't be reduced to a 0 or a 1, then
2382 we don't really know which edge will be taken at runtime. This
2383 may happen when comparing addresses (e.g., if (&var1 == 4)). */
2384 if (integer_nonzerop (val
))
2386 else if (integer_zerop (val
))
2393 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2394 statement, determine which edge will be taken out of the block. Return
2395 NULL if any edge may be taken. */
2398 find_taken_edge_switch_expr (basic_block bb
, tree val
)
2400 tree switch_expr
, taken_case
;
2401 basic_block dest_bb
;
2404 switch_expr
= last_stmt (bb
);
2405 taken_case
= find_case_label_for_value (switch_expr
, val
);
2406 dest_bb
= label_to_block (CASE_LABEL (taken_case
));
2408 e
= find_edge (bb
, dest_bb
);
2414 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2415 We can make optimal use here of the fact that the case labels are
2416 sorted: We can do a binary search for a case matching VAL. */
2419 find_case_label_for_value (tree switch_expr
, tree val
)
2421 tree vec
= SWITCH_LABELS (switch_expr
);
2422 size_t low
, high
, n
= TREE_VEC_LENGTH (vec
);
2423 tree default_case
= TREE_VEC_ELT (vec
, n
- 1);
2425 for (low
= -1, high
= n
- 1; high
- low
> 1; )
2427 size_t i
= (high
+ low
) / 2;
2428 tree t
= TREE_VEC_ELT (vec
, i
);
2431 /* Cache the result of comparing CASE_LOW and val. */
2432 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
2439 if (CASE_HIGH (t
) == NULL
)
2441 /* A singe-valued case label. */
2447 /* A case range. We can only handle integer ranges. */
2448 if (cmp
<= 0 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
2453 return default_case
;
2457 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2458 those alternatives are equal in each of the PHI nodes, then return
2459 true, else return false. */
2462 phi_alternatives_equal (basic_block dest
, edge e1
, edge e2
)
2464 int n1
= e1
->dest_idx
;
2465 int n2
= e2
->dest_idx
;
2468 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
2470 tree val1
= PHI_ARG_DEF (phi
, n1
);
2471 tree val2
= PHI_ARG_DEF (phi
, n2
);
2473 gcc_assert (val1
!= NULL_TREE
);
2474 gcc_assert (val2
!= NULL_TREE
);
2476 if (!operand_equal_for_phi_arg_p (val1
, val2
))
2484 /*---------------------------------------------------------------------------
2486 ---------------------------------------------------------------------------*/
2488 /* Dump tree-specific information of block BB to file OUTF. */
2491 tree_dump_bb (basic_block bb
, FILE *outf
, int indent
)
2493 dump_generic_bb (outf
, bb
, indent
, TDF_VOPS
);
2497 /* Dump a basic block on stderr. */
2500 debug_tree_bb (basic_block bb
)
2502 dump_bb (bb
, stderr
, 0);
2506 /* Dump basic block with index N on stderr. */
2509 debug_tree_bb_n (int n
)
2511 debug_tree_bb (BASIC_BLOCK (n
));
2512 return BASIC_BLOCK (n
);
2516 /* Dump the CFG on stderr.
2518 FLAGS are the same used by the tree dumping functions
2519 (see TDF_* in tree.h). */
2522 debug_tree_cfg (int flags
)
2524 dump_tree_cfg (stderr
, flags
);
2528 /* Dump the program showing basic block boundaries on the given FILE.
2530 FLAGS are the same used by the tree dumping functions (see TDF_* in
2534 dump_tree_cfg (FILE *file
, int flags
)
2536 if (flags
& TDF_DETAILS
)
2538 const char *funcname
2539 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2542 fprintf (file
, ";; Function %s\n\n", funcname
);
2543 fprintf (file
, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2544 n_basic_blocks
, n_edges
, last_basic_block
);
2546 brief_dump_cfg (file
);
2547 fprintf (file
, "\n");
2550 if (flags
& TDF_STATS
)
2551 dump_cfg_stats (file
);
2553 dump_function_to_file (current_function_decl
, file
, flags
| TDF_BLOCKS
);
2557 /* Dump CFG statistics on FILE. */
2560 dump_cfg_stats (FILE *file
)
2562 static long max_num_merged_labels
= 0;
2563 unsigned long size
, total
= 0;
2566 const char * const fmt_str
= "%-30s%-13s%12s\n";
2567 const char * const fmt_str_1
= "%-30s%13d%11lu%c\n";
2568 const char * const fmt_str_3
= "%-43s%11lu%c\n";
2569 const char *funcname
2570 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2573 fprintf (file
, "\nCFG Statistics for %s\n\n", funcname
);
2575 fprintf (file
, "---------------------------------------------------------\n");
2576 fprintf (file
, fmt_str
, "", " Number of ", "Memory");
2577 fprintf (file
, fmt_str
, "", " instances ", "used ");
2578 fprintf (file
, "---------------------------------------------------------\n");
2580 size
= n_basic_blocks
* sizeof (struct basic_block_def
);
2582 fprintf (file
, fmt_str_1
, "Basic blocks", n_basic_blocks
,
2583 SCALE (size
), LABEL (size
));
2587 n_edges
+= EDGE_COUNT (bb
->succs
);
2588 size
= n_edges
* sizeof (struct edge_def
);
2590 fprintf (file
, fmt_str_1
, "Edges", n_edges
, SCALE (size
), LABEL (size
));
2592 size
= n_basic_blocks
* sizeof (struct bb_ann_d
);
2594 fprintf (file
, fmt_str_1
, "Basic block annotations", n_basic_blocks
,
2595 SCALE (size
), LABEL (size
));
2597 fprintf (file
, "---------------------------------------------------------\n");
2598 fprintf (file
, fmt_str_3
, "Total memory used by CFG data", SCALE (total
),
2600 fprintf (file
, "---------------------------------------------------------\n");
2601 fprintf (file
, "\n");
2603 if (cfg_stats
.num_merged_labels
> max_num_merged_labels
)
2604 max_num_merged_labels
= cfg_stats
.num_merged_labels
;
2606 fprintf (file
, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2607 cfg_stats
.num_merged_labels
, max_num_merged_labels
);
2609 fprintf (file
, "\n");
2613 /* Dump CFG statistics on stderr. Keep extern so that it's always
2614 linked in the final executable. */
2617 debug_cfg_stats (void)
2619 dump_cfg_stats (stderr
);
2623 /* Dump the flowgraph to a .vcg FILE. */
2626 tree_cfg2vcg (FILE *file
)
2631 const char *funcname
2632 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2634 /* Write the file header. */
2635 fprintf (file
, "graph: { title: \"%s\"\n", funcname
);
2636 fprintf (file
, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2637 fprintf (file
, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2639 /* Write blocks and edges. */
2640 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
2642 fprintf (file
, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2645 if (e
->flags
& EDGE_FAKE
)
2646 fprintf (file
, " linestyle: dotted priority: 10");
2648 fprintf (file
, " linestyle: solid priority: 100");
2650 fprintf (file
, " }\n");
2656 enum tree_code head_code
, end_code
;
2657 const char *head_name
, *end_name
;
2660 tree first
= first_stmt (bb
);
2661 tree last
= last_stmt (bb
);
2665 head_code
= TREE_CODE (first
);
2666 head_name
= tree_code_name
[head_code
];
2667 head_line
= get_lineno (first
);
2670 head_name
= "no-statement";
2674 end_code
= TREE_CODE (last
);
2675 end_name
= tree_code_name
[end_code
];
2676 end_line
= get_lineno (last
);
2679 end_name
= "no-statement";
2681 fprintf (file
, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2682 bb
->index
, bb
->index
, head_name
, head_line
, end_name
,
2685 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2687 if (e
->dest
== EXIT_BLOCK_PTR
)
2688 fprintf (file
, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb
->index
);
2690 fprintf (file
, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb
->index
, e
->dest
->index
);
2692 if (e
->flags
& EDGE_FAKE
)
2693 fprintf (file
, " priority: 10 linestyle: dotted");
2695 fprintf (file
, " priority: 100 linestyle: solid");
2697 fprintf (file
, " }\n");
2700 if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
2704 fputs ("}\n\n", file
);
2709 /*---------------------------------------------------------------------------
2710 Miscellaneous helpers
2711 ---------------------------------------------------------------------------*/
2713 /* Return true if T represents a stmt that always transfers control. */
2716 is_ctrl_stmt (tree t
)
2718 return (TREE_CODE (t
) == COND_EXPR
2719 || TREE_CODE (t
) == SWITCH_EXPR
2720 || TREE_CODE (t
) == GOTO_EXPR
2721 || TREE_CODE (t
) == RETURN_EXPR
2722 || TREE_CODE (t
) == RESX_EXPR
);
2726 /* Return true if T is a statement that may alter the flow of control
2727 (e.g., a call to a non-returning function). */
2730 is_ctrl_altering_stmt (tree t
)
2735 call
= get_call_expr_in (t
);
2738 /* A non-pure/const CALL_EXPR alters flow control if the current
2739 function has nonlocal labels. */
2740 if (TREE_SIDE_EFFECTS (call
) && current_function_has_nonlocal_label
)
2743 /* A CALL_EXPR also alters control flow if it does not return. */
2744 if (call_expr_flags (call
) & ECF_NORETURN
)
2748 /* If a statement can throw, it alters control flow. */
2749 return tree_can_throw_internal (t
);
2753 /* Return true if T is a computed goto. */
2756 computed_goto_p (tree t
)
2758 return (TREE_CODE (t
) == GOTO_EXPR
2759 && TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
);
2763 /* Checks whether EXPR is a simple local goto. */
2766 simple_goto_p (tree expr
)
2768 return (TREE_CODE (expr
) == GOTO_EXPR
2769 && TREE_CODE (GOTO_DESTINATION (expr
)) == LABEL_DECL
);
2773 /* Return true if T should start a new basic block. PREV_T is the
2774 statement preceding T. It is used when T is a label or a case label.
2775 Labels should only start a new basic block if their previous statement
2776 wasn't a label. Otherwise, sequence of labels would generate
2777 unnecessary basic blocks that only contain a single label. */
2780 stmt_starts_bb_p (tree t
, tree prev_t
)
2785 /* LABEL_EXPRs start a new basic block only if the preceding
2786 statement wasn't a label of the same type. This prevents the
2787 creation of consecutive blocks that have nothing but a single
2789 if (TREE_CODE (t
) == LABEL_EXPR
)
2791 /* Nonlocal and computed GOTO targets always start a new block. */
2792 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (t
))
2793 || FORCED_LABEL (LABEL_EXPR_LABEL (t
)))
2796 if (prev_t
&& TREE_CODE (prev_t
) == LABEL_EXPR
)
2798 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t
)))
2801 cfg_stats
.num_merged_labels
++;
2812 /* Return true if T should end a basic block. */
2815 stmt_ends_bb_p (tree t
)
2817 return is_ctrl_stmt (t
) || is_ctrl_altering_stmt (t
);
2821 /* Add gotos that used to be represented implicitly in the CFG. */
2824 disband_implicit_edges (void)
2827 block_stmt_iterator last
;
2834 last
= bsi_last (bb
);
2835 stmt
= last_stmt (bb
);
2837 if (stmt
&& TREE_CODE (stmt
) == COND_EXPR
)
2839 /* Remove superfluous gotos from COND_EXPR branches. Moved
2840 from cfg_remove_useless_stmts here since it violates the
2841 invariants for tree--cfg correspondence and thus fits better
2842 here where we do it anyway. */
2843 e
= find_edge (bb
, bb
->next_bb
);
2846 if (e
->flags
& EDGE_TRUE_VALUE
)
2847 COND_EXPR_THEN (stmt
) = build_empty_stmt ();
2848 else if (e
->flags
& EDGE_FALSE_VALUE
)
2849 COND_EXPR_ELSE (stmt
) = build_empty_stmt ();
2852 e
->flags
|= EDGE_FALLTHRU
;
2858 if (stmt
&& TREE_CODE (stmt
) == RETURN_EXPR
)
2860 /* Remove the RETURN_EXPR if we may fall though to the exit
2862 gcc_assert (single_succ_p (bb
));
2863 gcc_assert (single_succ (bb
) == EXIT_BLOCK_PTR
);
2865 if (bb
->next_bb
== EXIT_BLOCK_PTR
2866 && !TREE_OPERAND (stmt
, 0))
2869 single_succ_edge (bb
)->flags
|= EDGE_FALLTHRU
;
2874 /* There can be no fallthru edge if the last statement is a control
2876 if (stmt
&& is_ctrl_stmt (stmt
))
2879 /* Find a fallthru edge and emit the goto if necessary. */
2880 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2881 if (e
->flags
& EDGE_FALLTHRU
)
2884 if (!e
|| e
->dest
== bb
->next_bb
)
2887 gcc_assert (e
->dest
!= EXIT_BLOCK_PTR
);
2888 label
= tree_block_label (e
->dest
);
2890 stmt
= build1 (GOTO_EXPR
, void_type_node
, label
);
2891 #ifdef USE_MAPPED_LOCATION
2892 SET_EXPR_LOCATION (stmt
, e
->goto_locus
);
2894 SET_EXPR_LOCUS (stmt
, e
->goto_locus
);
2896 bsi_insert_after (&last
, stmt
, BSI_NEW_STMT
);
2897 e
->flags
&= ~EDGE_FALLTHRU
;
2901 /* Remove block annotations and other datastructures. */
2904 delete_tree_cfg_annotations (void)
2907 if (n_basic_blocks
> 0)
2908 free_blocks_annotations ();
2910 label_to_block_map
= NULL
;
2917 /* Return the first statement in basic block BB. */
2920 first_stmt (basic_block bb
)
2922 block_stmt_iterator i
= bsi_start (bb
);
2923 return !bsi_end_p (i
) ? bsi_stmt (i
) : NULL_TREE
;
2927 /* Return the last statement in basic block BB. */
2930 last_stmt (basic_block bb
)
2932 block_stmt_iterator b
= bsi_last (bb
);
2933 return !bsi_end_p (b
) ? bsi_stmt (b
) : NULL_TREE
;
2937 /* Return a pointer to the last statement in block BB. */
2940 last_stmt_ptr (basic_block bb
)
2942 block_stmt_iterator last
= bsi_last (bb
);
2943 return !bsi_end_p (last
) ? bsi_stmt_ptr (last
) : NULL
;
2947 /* Return the last statement of an otherwise empty block. Return NULL
2948 if the block is totally empty, or if it contains more than one
2952 last_and_only_stmt (basic_block bb
)
2954 block_stmt_iterator i
= bsi_last (bb
);
2960 last
= bsi_stmt (i
);
2965 /* Empty statements should no longer appear in the instruction stream.
2966 Everything that might have appeared before should be deleted by
2967 remove_useless_stmts, and the optimizers should just bsi_remove
2968 instead of smashing with build_empty_stmt.
2970 Thus the only thing that should appear here in a block containing
2971 one executable statement is a label. */
2972 prev
= bsi_stmt (i
);
2973 if (TREE_CODE (prev
) == LABEL_EXPR
)
2980 /* Mark BB as the basic block holding statement T. */
2983 set_bb_for_stmt (tree t
, basic_block bb
)
2985 if (TREE_CODE (t
) == PHI_NODE
)
2987 else if (TREE_CODE (t
) == STATEMENT_LIST
)
2989 tree_stmt_iterator i
;
2990 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2991 set_bb_for_stmt (tsi_stmt (i
), bb
);
2995 stmt_ann_t ann
= get_stmt_ann (t
);
2998 /* If the statement is a label, add the label to block-to-labels map
2999 so that we can speed up edge creation for GOTO_EXPRs. */
3000 if (TREE_CODE (t
) == LABEL_EXPR
)
3004 t
= LABEL_EXPR_LABEL (t
);
3005 uid
= LABEL_DECL_UID (t
);
3008 LABEL_DECL_UID (t
) = uid
= cfun
->last_label_uid
++;
3009 if (VARRAY_SIZE (label_to_block_map
) <= (unsigned) uid
)
3010 VARRAY_GROW (label_to_block_map
, 3 * uid
/ 2);
3013 /* We're moving an existing label. Make sure that we've
3014 removed it from the old block. */
3015 gcc_assert (!bb
|| !VARRAY_BB (label_to_block_map
, uid
));
3016 VARRAY_BB (label_to_block_map
, uid
) = bb
;
3021 /* Finds iterator for STMT. */
3023 extern block_stmt_iterator
3024 bsi_for_stmt (tree stmt
)
3026 block_stmt_iterator bsi
;
3028 for (bsi
= bsi_start (bb_for_stmt (stmt
)); !bsi_end_p (bsi
); bsi_next (&bsi
))
3029 if (bsi_stmt (bsi
) == stmt
)
3035 /* Insert statement (or statement list) T before the statement
3036 pointed-to by iterator I. M specifies how to update iterator I
3037 after insertion (see enum bsi_iterator_update). */
3040 bsi_insert_before (block_stmt_iterator
*i
, tree t
, enum bsi_iterator_update m
)
3042 set_bb_for_stmt (t
, i
->bb
);
3043 tsi_link_before (&i
->tsi
, t
, m
);
3048 /* Insert statement (or statement list) T after the statement
3049 pointed-to by iterator I. M specifies how to update iterator I
3050 after insertion (see enum bsi_iterator_update). */
3053 bsi_insert_after (block_stmt_iterator
*i
, tree t
, enum bsi_iterator_update m
)
3055 set_bb_for_stmt (t
, i
->bb
);
3056 tsi_link_after (&i
->tsi
, t
, m
);
3061 /* Remove the statement pointed to by iterator I. The iterator is updated
3062 to the next statement. */
3065 bsi_remove (block_stmt_iterator
*i
)
3067 tree t
= bsi_stmt (*i
);
3068 set_bb_for_stmt (t
, NULL
);
3069 tsi_delink (&i
->tsi
);
3073 /* Move the statement at FROM so it comes right after the statement at TO. */
3076 bsi_move_after (block_stmt_iterator
*from
, block_stmt_iterator
*to
)
3078 tree stmt
= bsi_stmt (*from
);
3080 bsi_insert_after (to
, stmt
, BSI_SAME_STMT
);
3084 /* Move the statement at FROM so it comes right before the statement at TO. */
3087 bsi_move_before (block_stmt_iterator
*from
, block_stmt_iterator
*to
)
3089 tree stmt
= bsi_stmt (*from
);
3091 bsi_insert_before (to
, stmt
, BSI_SAME_STMT
);
3095 /* Move the statement at FROM to the end of basic block BB. */
3098 bsi_move_to_bb_end (block_stmt_iterator
*from
, basic_block bb
)
3100 block_stmt_iterator last
= bsi_last (bb
);
3102 /* Have to check bsi_end_p because it could be an empty block. */
3103 if (!bsi_end_p (last
) && is_ctrl_stmt (bsi_stmt (last
)))
3104 bsi_move_before (from
, &last
);
3106 bsi_move_after (from
, &last
);
3110 /* Replace the contents of the statement pointed to by iterator BSI
3111 with STMT. If PRESERVE_EH_INFO is true, the exception handling
3112 information of the original statement is preserved. */
3115 bsi_replace (const block_stmt_iterator
*bsi
, tree stmt
, bool preserve_eh_info
)
3118 tree orig_stmt
= bsi_stmt (*bsi
);
3120 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (orig_stmt
));
3121 set_bb_for_stmt (stmt
, bsi
->bb
);
3123 /* Preserve EH region information from the original statement, if
3124 requested by the caller. */
3125 if (preserve_eh_info
)
3127 eh_region
= lookup_stmt_eh_region (orig_stmt
);
3129 add_stmt_to_eh_region (stmt
, eh_region
);
3132 *bsi_stmt_ptr (*bsi
) = stmt
;
3137 /* Insert the statement pointed-to by BSI into edge E. Every attempt
3138 is made to place the statement in an existing basic block, but
3139 sometimes that isn't possible. When it isn't possible, the edge is
3140 split and the statement is added to the new block.
3142 In all cases, the returned *BSI points to the correct location. The
3143 return value is true if insertion should be done after the location,
3144 or false if it should be done before the location. If new basic block
3145 has to be created, it is stored in *NEW_BB. */
3148 tree_find_edge_insert_loc (edge e
, block_stmt_iterator
*bsi
,
3149 basic_block
*new_bb
)
3151 basic_block dest
, src
;
3157 /* If the destination has one predecessor which has no PHI nodes,
3158 insert there. Except for the exit block.
3160 The requirement for no PHI nodes could be relaxed. Basically we
3161 would have to examine the PHIs to prove that none of them used
3162 the value set by the statement we want to insert on E. That
3163 hardly seems worth the effort. */
3164 if (single_pred_p (dest
)
3165 && ! phi_nodes (dest
)
3166 && dest
!= EXIT_BLOCK_PTR
)
3168 *bsi
= bsi_start (dest
);
3169 if (bsi_end_p (*bsi
))
3172 /* Make sure we insert after any leading labels. */
3173 tmp
= bsi_stmt (*bsi
);
3174 while (TREE_CODE (tmp
) == LABEL_EXPR
)
3177 if (bsi_end_p (*bsi
))
3179 tmp
= bsi_stmt (*bsi
);
3182 if (bsi_end_p (*bsi
))
3184 *bsi
= bsi_last (dest
);
3191 /* If the source has one successor, the edge is not abnormal and
3192 the last statement does not end a basic block, insert there.
3193 Except for the entry block. */
3195 if ((e
->flags
& EDGE_ABNORMAL
) == 0
3196 && single_succ_p (src
)
3197 && src
!= ENTRY_BLOCK_PTR
)
3199 *bsi
= bsi_last (src
);
3200 if (bsi_end_p (*bsi
))
3203 tmp
= bsi_stmt (*bsi
);
3204 if (!stmt_ends_bb_p (tmp
))
3207 /* Insert code just before returning the value. We may need to decompose
3208 the return in the case it contains non-trivial operand. */
3209 if (TREE_CODE (tmp
) == RETURN_EXPR
)
3211 tree op
= TREE_OPERAND (tmp
, 0);
3212 if (!is_gimple_val (op
))
3214 gcc_assert (TREE_CODE (op
) == MODIFY_EXPR
);
3215 bsi_insert_before (bsi
, op
, BSI_NEW_STMT
);
3216 TREE_OPERAND (tmp
, 0) = TREE_OPERAND (op
, 0);
3223 /* Otherwise, create a new basic block, and split this edge. */
3224 dest
= split_edge (e
);
3227 e
= single_pred_edge (dest
);
3232 /* This routine will commit all pending edge insertions, creating any new
3233 basic blocks which are necessary. */
3236 bsi_commit_edge_inserts (void)
3242 bsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR
), NULL
);
3245 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3246 bsi_commit_one_edge_insert (e
, NULL
);
3250 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
3251 to this block, otherwise set it to NULL. */
3254 bsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
3258 if (PENDING_STMT (e
))
3260 block_stmt_iterator bsi
;
3261 tree stmt
= PENDING_STMT (e
);
3263 PENDING_STMT (e
) = NULL_TREE
;
3265 if (tree_find_edge_insert_loc (e
, &bsi
, new_bb
))
3266 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3268 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3273 /* Add STMT to the pending list of edge E. No actual insertion is
3274 made until a call to bsi_commit_edge_inserts () is made. */
3277 bsi_insert_on_edge (edge e
, tree stmt
)
3279 append_to_statement_list (stmt
, &PENDING_STMT (e
));
3282 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new
3283 block has to be created, it is returned. */
3286 bsi_insert_on_edge_immediate (edge e
, tree stmt
)
3288 block_stmt_iterator bsi
;
3289 basic_block new_bb
= NULL
;
3291 gcc_assert (!PENDING_STMT (e
));
3293 if (tree_find_edge_insert_loc (e
, &bsi
, &new_bb
))
3294 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3296 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3301 /*---------------------------------------------------------------------------
3302 Tree specific functions for CFG manipulation
3303 ---------------------------------------------------------------------------*/
3305 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
3308 reinstall_phi_args (edge new_edge
, edge old_edge
)
3312 if (!PENDING_STMT (old_edge
))
3315 for (var
= PENDING_STMT (old_edge
), phi
= phi_nodes (new_edge
->dest
);
3317 var
= TREE_CHAIN (var
), phi
= PHI_CHAIN (phi
))
3319 tree result
= TREE_PURPOSE (var
);
3320 tree arg
= TREE_VALUE (var
);
3322 gcc_assert (result
== PHI_RESULT (phi
));
3324 add_phi_arg (phi
, arg
, new_edge
);
3327 PENDING_STMT (old_edge
) = NULL
;
3330 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3331 Abort on abnormal edges. */
3334 tree_split_edge (edge edge_in
)
3336 basic_block new_bb
, after_bb
, dest
, src
;
3339 /* Abnormal edges cannot be split. */
3340 gcc_assert (!(edge_in
->flags
& EDGE_ABNORMAL
));
3343 dest
= edge_in
->dest
;
3345 /* Place the new block in the block list. Try to keep the new block
3346 near its "logical" location. This is of most help to humans looking
3347 at debugging dumps. */
3348 if (dest
->prev_bb
&& find_edge (dest
->prev_bb
, dest
))
3349 after_bb
= edge_in
->src
;
3351 after_bb
= dest
->prev_bb
;
3353 new_bb
= create_empty_bb (after_bb
);
3354 new_bb
->frequency
= EDGE_FREQUENCY (edge_in
);
3355 new_bb
->count
= edge_in
->count
;
3356 new_edge
= make_edge (new_bb
, dest
, EDGE_FALLTHRU
);
3357 new_edge
->probability
= REG_BR_PROB_BASE
;
3358 new_edge
->count
= edge_in
->count
;
3360 e
= redirect_edge_and_branch (edge_in
, new_bb
);
3362 reinstall_phi_args (new_edge
, e
);
3368 /* Return true when BB has label LABEL in it. */
3371 has_label_p (basic_block bb
, tree label
)
3373 block_stmt_iterator bsi
;
3375 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3377 tree stmt
= bsi_stmt (bsi
);
3379 if (TREE_CODE (stmt
) != LABEL_EXPR
)
3381 if (LABEL_EXPR_LABEL (stmt
) == label
)
3388 /* Callback for walk_tree, check that all elements with address taken are
3389 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3390 inside a PHI node. */
3393 verify_expr (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
3396 bool in_phi
= (data
!= NULL
);
3401 /* Check operand N for being valid GIMPLE and give error MSG if not.
3402 We check for constants explicitly since they are not considered
3403 gimple invariants if they overflowed. */
3404 #define CHECK_OP(N, MSG) \
3405 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3406 && !is_gimple_val (TREE_OPERAND (t, N))) \
3407 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3409 switch (TREE_CODE (t
))
3412 if (SSA_NAME_IN_FREE_LIST (t
))
3414 error ("SSA name in freelist but still referenced");
3420 x
= TREE_OPERAND (t
, 0);
3421 if (TREE_CODE (x
) == BIT_FIELD_REF
3422 && is_gimple_reg (TREE_OPERAND (x
, 0)))
3424 error ("GIMPLE register modified with BIT_FIELD_REF");
3430 /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
3431 dead PHIs that take the address of something. But if the PHI
3432 result is dead, the fact that it takes the address of anything
3433 is irrelevant. Because we can not tell from here if a PHI result
3434 is dead, we just skip this check for PHIs altogether. This means
3435 we may be missing "valid" checks, but what can you do?
3436 This was PR19217. */
3440 /* Skip any references (they will be checked when we recurse down the
3441 tree) and ensure that any variable used as a prefix is marked
3443 for (x
= TREE_OPERAND (t
, 0);
3444 handled_component_p (x
);
3445 x
= TREE_OPERAND (x
, 0))
3448 if (TREE_CODE (x
) != VAR_DECL
&& TREE_CODE (x
) != PARM_DECL
)
3450 if (!TREE_ADDRESSABLE (x
))
3452 error ("address taken, but ADDRESSABLE bit not set");
3458 x
= COND_EXPR_COND (t
);
3459 if (TREE_CODE (TREE_TYPE (x
)) != BOOLEAN_TYPE
)
3461 error ("non-boolean used in condition");
3468 case FIX_TRUNC_EXPR
:
3470 case FIX_FLOOR_EXPR
:
3471 case FIX_ROUND_EXPR
:
3476 case NON_LVALUE_EXPR
:
3477 case TRUTH_NOT_EXPR
:
3478 CHECK_OP (0, "Invalid operand to unary operator");
3485 case ARRAY_RANGE_REF
:
3487 case VIEW_CONVERT_EXPR
:
3488 /* We have a nest of references. Verify that each of the operands
3489 that determine where to reference is either a constant or a variable,
3490 verify that the base is valid, and then show we've already checked
3492 while (handled_component_p (t
))
3494 if (TREE_CODE (t
) == COMPONENT_REF
&& TREE_OPERAND (t
, 2))
3495 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3496 else if (TREE_CODE (t
) == ARRAY_REF
3497 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
3499 CHECK_OP (1, "Invalid array index.");
3500 if (TREE_OPERAND (t
, 2))
3501 CHECK_OP (2, "Invalid array lower bound.");
3502 if (TREE_OPERAND (t
, 3))
3503 CHECK_OP (3, "Invalid array stride.");
3505 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
3507 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3508 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3511 t
= TREE_OPERAND (t
, 0);
3514 if (!CONSTANT_CLASS_P (t
) && !is_gimple_lvalue (t
))
3516 error ("Invalid reference prefix.");
3528 case UNORDERED_EXPR
:
3539 case TRUNC_DIV_EXPR
:
3541 case FLOOR_DIV_EXPR
:
3542 case ROUND_DIV_EXPR
:
3543 case TRUNC_MOD_EXPR
:
3545 case FLOOR_MOD_EXPR
:
3546 case ROUND_MOD_EXPR
:
3548 case EXACT_DIV_EXPR
:
3558 CHECK_OP (0, "Invalid operand to binary operator");
3559 CHECK_OP (1, "Invalid operand to binary operator");
3571 /* Verify STMT, return true if STMT is not in GIMPLE form.
3572 TODO: Implement type checking. */
3575 verify_stmt (tree stmt
, bool last_in_block
)
3579 if (!is_gimple_stmt (stmt
))
3581 error ("Is not a valid GIMPLE statement.");
3585 addr
= walk_tree (&stmt
, verify_expr
, NULL
, NULL
);
3588 debug_generic_stmt (addr
);
3592 /* If the statement is marked as part of an EH region, then it is
3593 expected that the statement could throw. Verify that when we
3594 have optimizations that simplify statements such that we prove
3595 that they cannot throw, that we update other data structures
3597 if (lookup_stmt_eh_region (stmt
) >= 0)
3599 if (!tree_could_throw_p (stmt
))
3601 error ("Statement marked for throw, but doesn%'t.");
3604 if (!last_in_block
&& tree_can_throw_internal (stmt
))
3606 error ("Statement marked for throw in middle of block.");
3614 debug_generic_stmt (stmt
);
3619 /* Return true when the T can be shared. */
3622 tree_node_can_be_shared (tree t
)
3624 if (IS_TYPE_OR_DECL_P (t
)
3625 /* We check for constants explicitly since they are not considered
3626 gimple invariants if they overflowed. */
3627 || CONSTANT_CLASS_P (t
)
3628 || is_gimple_min_invariant (t
)
3629 || TREE_CODE (t
) == SSA_NAME
3630 || t
== error_mark_node
)
3633 if (TREE_CODE (t
) == CASE_LABEL_EXPR
)
3636 while (((TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
3637 /* We check for constants explicitly since they are not considered
3638 gimple invariants if they overflowed. */
3639 && (CONSTANT_CLASS_P (TREE_OPERAND (t
, 1))
3640 || is_gimple_min_invariant (TREE_OPERAND (t
, 1))))
3641 || (TREE_CODE (t
) == COMPONENT_REF
3642 || TREE_CODE (t
) == REALPART_EXPR
3643 || TREE_CODE (t
) == IMAGPART_EXPR
))
3644 t
= TREE_OPERAND (t
, 0);
3653 /* Called via walk_trees. Verify tree sharing. */
3656 verify_node_sharing (tree
* tp
, int *walk_subtrees
, void *data
)
3658 htab_t htab
= (htab_t
) data
;
3661 if (tree_node_can_be_shared (*tp
))
3663 *walk_subtrees
= false;
3667 slot
= htab_find_slot (htab
, *tp
, INSERT
);
3676 /* Verify the GIMPLE statement chain. */
3682 block_stmt_iterator bsi
;
3687 timevar_push (TV_TREE_STMT_VERIFY
);
3688 htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
3695 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
3697 int phi_num_args
= PHI_NUM_ARGS (phi
);
3699 if (bb_for_stmt (phi
) != bb
)
3701 error ("bb_for_stmt (phi) is set to a wrong basic block\n");
3705 for (i
= 0; i
< phi_num_args
; i
++)
3707 tree t
= PHI_ARG_DEF (phi
, i
);
3710 /* Addressable variables do have SSA_NAMEs but they
3711 are not considered gimple values. */
3712 if (TREE_CODE (t
) != SSA_NAME
3713 && TREE_CODE (t
) != FUNCTION_DECL
3714 && !is_gimple_val (t
))
3716 error ("PHI def is not a GIMPLE value");
3717 debug_generic_stmt (phi
);
3718 debug_generic_stmt (t
);
3722 addr
= walk_tree (&t
, verify_expr
, (void *) 1, NULL
);
3725 debug_generic_stmt (addr
);
3729 addr
= walk_tree (&t
, verify_node_sharing
, htab
, NULL
);
3732 error ("Incorrect sharing of tree nodes");
3733 debug_generic_stmt (phi
);
3734 debug_generic_stmt (addr
);
3740 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
3742 tree stmt
= bsi_stmt (bsi
);
3744 if (bb_for_stmt (stmt
) != bb
)
3746 error ("bb_for_stmt (stmt) is set to a wrong basic block\n");
3751 err
|= verify_stmt (stmt
, bsi_end_p (bsi
));
3752 addr
= walk_tree (&stmt
, verify_node_sharing
, htab
, NULL
);
3755 error ("Incorrect sharing of tree nodes");
3756 debug_generic_stmt (stmt
);
3757 debug_generic_stmt (addr
);
3764 internal_error ("verify_stmts failed.");
3767 timevar_pop (TV_TREE_STMT_VERIFY
);
3771 /* Verifies that the flow information is OK. */
3774 tree_verify_flow_info (void)
3778 block_stmt_iterator bsi
;
3783 if (ENTRY_BLOCK_PTR
->stmt_list
)
3785 error ("ENTRY_BLOCK has a statement list associated with it\n");
3789 if (EXIT_BLOCK_PTR
->stmt_list
)
3791 error ("EXIT_BLOCK has a statement list associated with it\n");
3795 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
3796 if (e
->flags
& EDGE_FALLTHRU
)
3798 error ("Fallthru to exit from bb %d\n", e
->src
->index
);
3804 bool found_ctrl_stmt
= false;
3808 /* Skip labels on the start of basic block. */
3809 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3811 tree prev_stmt
= stmt
;
3813 stmt
= bsi_stmt (bsi
);
3815 if (TREE_CODE (stmt
) != LABEL_EXPR
)
3818 if (prev_stmt
&& DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
3820 error ("Nonlocal label %s is not first "
3821 "in a sequence of labels in bb %d",
3822 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt
))),
3827 if (label_to_block (LABEL_EXPR_LABEL (stmt
)) != bb
)
3829 error ("Label %s to block does not match in bb %d\n",
3830 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt
))),
3835 if (decl_function_context (LABEL_EXPR_LABEL (stmt
))
3836 != current_function_decl
)
3838 error ("Label %s has incorrect context in bb %d\n",
3839 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt
))),
3845 /* Verify that body of basic block BB is free of control flow. */
3846 for (; !bsi_end_p (bsi
); bsi_next (&bsi
))
3848 tree stmt
= bsi_stmt (bsi
);
3850 if (found_ctrl_stmt
)
3852 error ("Control flow in the middle of basic block %d\n",
3857 if (stmt_ends_bb_p (stmt
))
3858 found_ctrl_stmt
= true;
3860 if (TREE_CODE (stmt
) == LABEL_EXPR
)
3862 error ("Label %s in the middle of basic block %d\n",
3863 IDENTIFIER_POINTER (DECL_NAME (stmt
)),
3868 bsi
= bsi_last (bb
);
3869 if (bsi_end_p (bsi
))
3872 stmt
= bsi_stmt (bsi
);
3874 if (is_ctrl_stmt (stmt
))
3876 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3877 if (e
->flags
& EDGE_FALLTHRU
)
3879 error ("Fallthru edge after a control statement in bb %d \n",
3885 switch (TREE_CODE (stmt
))
3891 if (TREE_CODE (COND_EXPR_THEN (stmt
)) != GOTO_EXPR
3892 || TREE_CODE (COND_EXPR_ELSE (stmt
)) != GOTO_EXPR
)
3894 error ("Structured COND_EXPR at the end of bb %d\n", bb
->index
);
3898 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
3900 if (!true_edge
|| !false_edge
3901 || !(true_edge
->flags
& EDGE_TRUE_VALUE
)
3902 || !(false_edge
->flags
& EDGE_FALSE_VALUE
)
3903 || (true_edge
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
))
3904 || (false_edge
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
))
3905 || EDGE_COUNT (bb
->succs
) >= 3)
3907 error ("Wrong outgoing edge flags at end of bb %d\n",
3912 if (!has_label_p (true_edge
->dest
,
3913 GOTO_DESTINATION (COND_EXPR_THEN (stmt
))))
3915 error ("%<then%> label does not match edge at end of bb %d\n",
3920 if (!has_label_p (false_edge
->dest
,
3921 GOTO_DESTINATION (COND_EXPR_ELSE (stmt
))))
3923 error ("%<else%> label does not match edge at end of bb %d\n",
3931 if (simple_goto_p (stmt
))
3933 error ("Explicit goto at end of bb %d\n", bb
->index
);
3938 /* FIXME. We should double check that the labels in the
3939 destination blocks have their address taken. */
3940 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3941 if ((e
->flags
& (EDGE_FALLTHRU
| EDGE_TRUE_VALUE
3942 | EDGE_FALSE_VALUE
))
3943 || !(e
->flags
& EDGE_ABNORMAL
))
3945 error ("Wrong outgoing edge flags at end of bb %d\n",
3953 if (!single_succ_p (bb
)
3954 || (single_succ_edge (bb
)->flags
3955 & (EDGE_FALLTHRU
| EDGE_ABNORMAL
3956 | EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
3958 error ("Wrong outgoing edge flags at end of bb %d\n", bb
->index
);
3961 if (single_succ (bb
) != EXIT_BLOCK_PTR
)
3963 error ("Return edge does not point to exit in bb %d\n",
3976 vec
= SWITCH_LABELS (stmt
);
3977 n
= TREE_VEC_LENGTH (vec
);
3979 /* Mark all the destination basic blocks. */
3980 for (i
= 0; i
< n
; ++i
)
3982 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
3983 basic_block label_bb
= label_to_block (lab
);
3985 gcc_assert (!label_bb
->aux
|| label_bb
->aux
== (void *)1);
3986 label_bb
->aux
= (void *)1;
3989 /* Verify that the case labels are sorted. */
3990 prev
= TREE_VEC_ELT (vec
, 0);
3991 for (i
= 1; i
< n
- 1; ++i
)
3993 tree c
= TREE_VEC_ELT (vec
, i
);
3996 error ("Found default case not at end of case vector");
4000 if (! tree_int_cst_lt (CASE_LOW (prev
), CASE_LOW (c
)))
4002 error ("Case labels not sorted:\n ");
4003 print_generic_expr (stderr
, prev
, 0);
4004 fprintf (stderr
," is greater than ");
4005 print_generic_expr (stderr
, c
, 0);
4006 fprintf (stderr
," but comes before it.\n");
4011 if (CASE_LOW (TREE_VEC_ELT (vec
, n
- 1)))
4013 error ("No default case found at end of case vector");
4017 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4021 error ("Extra outgoing edge %d->%d\n",
4022 bb
->index
, e
->dest
->index
);
4025 e
->dest
->aux
= (void *)2;
4026 if ((e
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
4027 | EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
4029 error ("Wrong outgoing edge flags at end of bb %d\n",
4035 /* Check that we have all of them. */
4036 for (i
= 0; i
< n
; ++i
)
4038 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
4039 basic_block label_bb
= label_to_block (lab
);
4041 if (label_bb
->aux
!= (void *)2)
4043 error ("Missing edge %i->%i",
4044 bb
->index
, label_bb
->index
);
4049 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4050 e
->dest
->aux
= (void *)0;
4057 if (dom_computed
[CDI_DOMINATORS
] >= DOM_NO_FAST_QUERY
)
4058 verify_dominators (CDI_DOMINATORS
);
4064 /* Updates phi nodes after creating a forwarder block joined
4065 by edge FALLTHRU. */
4068 tree_make_forwarder_block (edge fallthru
)
4072 basic_block dummy
, bb
;
4073 tree phi
, new_phi
, var
;
4075 dummy
= fallthru
->src
;
4076 bb
= fallthru
->dest
;
4078 if (single_pred_p (bb
))
4081 /* If we redirected a branch we must create new phi nodes at the
4083 for (phi
= phi_nodes (dummy
); phi
; phi
= PHI_CHAIN (phi
))
4085 var
= PHI_RESULT (phi
);
4086 new_phi
= create_phi_node (var
, bb
);
4087 SSA_NAME_DEF_STMT (var
) = new_phi
;
4088 SET_PHI_RESULT (phi
, make_ssa_name (SSA_NAME_VAR (var
), phi
));
4089 add_phi_arg (new_phi
, PHI_RESULT (phi
), fallthru
);
4092 /* Ensure that the PHI node chain is in the same order. */
4093 set_phi_nodes (bb
, phi_reverse (phi_nodes (bb
)));
4095 /* Add the arguments we have stored on edges. */
4096 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4101 flush_pending_stmts (e
);
4106 /* Return true if basic block BB does nothing except pass control
4107 flow to another block and that we can safely insert a label at
4108 the start of the successor block.
4110 As a precondition, we require that BB be not equal to
4114 tree_forwarder_block_p (basic_block bb
, bool phi_wanted
)
4116 block_stmt_iterator bsi
;
4118 /* BB must have a single outgoing edge. */
4119 if (single_succ_p (bb
) != 1
4120 /* If PHI_WANTED is false, BB must not have any PHI nodes.
4121 Otherwise, BB must have PHI nodes. */
4122 || (phi_nodes (bb
) != NULL_TREE
) != phi_wanted
4123 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
4124 || single_succ (bb
) == EXIT_BLOCK_PTR
4125 /* Nor should this be an infinite loop. */
4126 || single_succ (bb
) == bb
4127 /* BB may not have an abnormal outgoing edge. */
4128 || (single_succ_edge (bb
)->flags
& EDGE_ABNORMAL
))
4132 gcc_assert (bb
!= ENTRY_BLOCK_PTR
);
4135 /* Now walk through the statements backward. We can ignore labels,
4136 anything else means this is not a forwarder block. */
4137 for (bsi
= bsi_last (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4139 tree stmt
= bsi_stmt (bsi
);
4141 switch (TREE_CODE (stmt
))
4144 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
4153 if (find_edge (ENTRY_BLOCK_PTR
, bb
))
4159 /* Protect loop latches, headers and preheaders. */
4160 if (bb
->loop_father
->header
== bb
)
4162 dest
= EDGE_SUCC (bb
, 0)->dest
;
4164 if (dest
->loop_father
->header
== dest
)
4171 /* Return true if BB has at least one abnormal incoming edge. */
4174 has_abnormal_incoming_edge_p (basic_block bb
)
4179 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4180 if (e
->flags
& EDGE_ABNORMAL
)
4186 /* Removes forwarder block BB. Returns false if this failed. If a new
4187 forwarder block is created due to redirection of edges, it is
4188 stored to worklist. */
4191 remove_forwarder_block (basic_block bb
, basic_block
**worklist
)
4193 edge succ
= single_succ_edge (bb
), e
, s
;
4194 basic_block dest
= succ
->dest
;
4198 block_stmt_iterator bsi
, bsi_to
;
4199 bool seen_abnormal_edge
= false;
4201 /* We check for infinite loops already in tree_forwarder_block_p.
4202 However it may happen that the infinite loop is created
4203 afterwards due to removal of forwarders. */
4207 /* If the destination block consists of a nonlocal label, do not merge
4209 label
= first_stmt (dest
);
4211 && TREE_CODE (label
) == LABEL_EXPR
4212 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label
)))
4215 /* If there is an abnormal edge to basic block BB, but not into
4216 dest, problems might occur during removal of the phi node at out
4217 of ssa due to overlapping live ranges of registers.
4219 If there is an abnormal edge in DEST, the problems would occur
4220 anyway since cleanup_dead_labels would then merge the labels for
4221 two different eh regions, and rest of exception handling code
4224 So if there is an abnormal edge to BB, proceed only if there is
4225 no abnormal edge to DEST and there are no phi nodes in DEST. */
4226 if (has_abnormal_incoming_edge_p (bb
))
4228 seen_abnormal_edge
= true;
4230 if (has_abnormal_incoming_edge_p (dest
)
4231 || phi_nodes (dest
) != NULL_TREE
)
4235 /* If there are phi nodes in DEST, and some of the blocks that are
4236 predecessors of BB are also predecessors of DEST, check that the
4237 phi node arguments match. */
4238 if (phi_nodes (dest
))
4240 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4242 s
= find_edge (e
->src
, dest
);
4246 if (!phi_alternatives_equal (dest
, succ
, s
))
4251 /* Redirect the edges. */
4252 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4254 if (e
->flags
& EDGE_ABNORMAL
)
4256 /* If there is an abnormal edge, redirect it anyway, and
4257 move the labels to the new block to make it legal. */
4258 s
= redirect_edge_succ_nodup (e
, dest
);
4261 s
= redirect_edge_and_branch (e
, dest
);
4265 /* Create arguments for the phi nodes, since the edge was not
4267 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
4268 add_phi_arg (phi
, PHI_ARG_DEF (phi
, succ
->dest_idx
), s
);
4272 /* The source basic block might become a forwarder. We know
4273 that it was not a forwarder before, since it used to have
4274 at least two outgoing edges, so we may just add it to
4276 if (tree_forwarder_block_p (s
->src
, false))
4277 *(*worklist
)++ = s
->src
;
4281 if (seen_abnormal_edge
)
4283 /* Move the labels to the new block, so that the redirection of
4284 the abnormal edges works. */
4286 bsi_to
= bsi_start (dest
);
4287 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
4289 label
= bsi_stmt (bsi
);
4290 gcc_assert (TREE_CODE (label
) == LABEL_EXPR
);
4292 bsi_insert_before (&bsi_to
, label
, BSI_CONTINUE_LINKING
);
4296 /* Update the dominators. */
4297 if (dom_info_available_p (CDI_DOMINATORS
))
4299 basic_block dom
, dombb
, domdest
;
4301 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
4302 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
4305 /* Shortcut to avoid calling (relatively expensive)
4306 nearest_common_dominator unless necessary. */
4310 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
4312 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
4315 /* And kill the forwarder block. */
4316 delete_basic_block (bb
);
4321 /* Removes forwarder blocks. */
4324 cleanup_forwarder_blocks (void)
4327 bool changed
= false;
4328 basic_block
*worklist
= xmalloc (sizeof (basic_block
) * n_basic_blocks
);
4329 basic_block
*current
= worklist
;
4333 if (tree_forwarder_block_p (bb
, false))
4337 while (current
!= worklist
)
4340 changed
|= remove_forwarder_block (bb
, ¤t
);
4347 /* Merge the PHI nodes at BB into those at BB's sole successor. */
4350 remove_forwarder_block_with_phi (basic_block bb
)
4352 edge succ
= single_succ_edge (bb
);
4353 basic_block dest
= succ
->dest
;
4355 basic_block dombb
, domdest
, dom
;
4357 /* We check for infinite loops already in tree_forwarder_block_p.
4358 However it may happen that the infinite loop is created
4359 afterwards due to removal of forwarders. */
4363 /* If the destination block consists of a nonlocal label, do not
4365 label
= first_stmt (dest
);
4367 && TREE_CODE (label
) == LABEL_EXPR
4368 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label
)))
4371 /* Redirect each incoming edge to BB to DEST. */
4372 while (EDGE_COUNT (bb
->preds
) > 0)
4374 edge e
= EDGE_PRED (bb
, 0), s
;
4377 s
= find_edge (e
->src
, dest
);
4380 /* We already have an edge S from E->src to DEST. If S and
4381 E->dest's sole successor edge have the same PHI arguments
4382 at DEST, redirect S to DEST. */
4383 if (phi_alternatives_equal (dest
, s
, succ
))
4385 e
= redirect_edge_and_branch (e
, dest
);
4386 PENDING_STMT (e
) = NULL_TREE
;
4390 /* PHI arguments are different. Create a forwarder block by
4391 splitting E so that we can merge PHI arguments on E to
4393 e
= single_succ_edge (split_edge (e
));
4396 s
= redirect_edge_and_branch (e
, dest
);
4398 /* redirect_edge_and_branch must not create a new edge. */
4399 gcc_assert (s
== e
);
4401 /* Add to the PHI nodes at DEST each PHI argument removed at the
4402 destination of E. */
4403 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
4405 tree def
= PHI_ARG_DEF (phi
, succ
->dest_idx
);
4407 if (TREE_CODE (def
) == SSA_NAME
)
4411 /* If DEF is one of the results of PHI nodes removed during
4412 redirection, replace it with the PHI argument that used
4414 for (var
= PENDING_STMT (e
); var
; var
= TREE_CHAIN (var
))
4416 tree old_arg
= TREE_PURPOSE (var
);
4417 tree new_arg
= TREE_VALUE (var
);
4427 add_phi_arg (phi
, def
, s
);
4430 PENDING_STMT (e
) = NULL
;
4433 /* Update the dominators. */
4434 dombb
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
4435 domdest
= get_immediate_dominator (CDI_DOMINATORS
, dest
);
4438 /* Shortcut to avoid calling (relatively expensive)
4439 nearest_common_dominator unless necessary. */
4443 dom
= nearest_common_dominator (CDI_DOMINATORS
, domdest
, dombb
);
4445 set_immediate_dominator (CDI_DOMINATORS
, dest
, dom
);
4447 /* Remove BB since all of BB's incoming edges have been redirected
4449 delete_basic_block (bb
);
4452 /* This pass merges PHI nodes if one feeds into another. For example,
4453 suppose we have the following:
4460 # tem_6 = PHI <tem_17(8), tem_23(7)>;
4463 # tem_3 = PHI <tem_6(9), tem_2(5)>;
4466 Then we merge the first PHI node into the second one like so:
4468 goto <bb 9> (<L10>);
4473 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
4478 merge_phi_nodes (void)
4480 basic_block
*worklist
= xmalloc (sizeof (basic_block
) * n_basic_blocks
);
4481 basic_block
*current
= worklist
;
4484 calculate_dominance_info (CDI_DOMINATORS
);
4486 /* Find all PHI nodes that we may be able to merge. */
4491 /* Look for a forwarder block with PHI nodes. */
4492 if (!tree_forwarder_block_p (bb
, true))
4495 dest
= single_succ (bb
);
4497 /* We have to feed into another basic block with PHI
4499 if (!phi_nodes (dest
)
4500 /* We don't want to deal with a basic block with
4502 || has_abnormal_incoming_edge_p (bb
))
4505 if (!dominated_by_p (CDI_DOMINATORS
, dest
, bb
))
4507 /* If BB does not dominate DEST, then the PHI nodes at
4508 DEST must be the only users of the results of the PHI
4514 /* Now let's drain WORKLIST. */
4515 while (current
!= worklist
)
4518 remove_forwarder_block_with_phi (bb
);
4525 gate_merge_phi (void)
4530 struct tree_opt_pass pass_merge_phi
= {
4531 "mergephi", /* name */
4532 gate_merge_phi
, /* gate */
4533 merge_phi_nodes
, /* execute */
4536 0, /* static_pass_number */
4537 TV_TREE_MERGE_PHI
, /* tv_id */
4538 PROP_cfg
| PROP_ssa
, /* properties_required */
4539 0, /* properties_provided */
4540 0, /* properties_destroyed */
4541 0, /* todo_flags_start */
4542 TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */
4547 /* Return a non-special label in the head of basic block BLOCK.
4548 Create one if it doesn't exist. */
4551 tree_block_label (basic_block bb
)
4553 block_stmt_iterator i
, s
= bsi_start (bb
);
4557 for (i
= s
; !bsi_end_p (i
); first
= false, bsi_next (&i
))
4559 stmt
= bsi_stmt (i
);
4560 if (TREE_CODE (stmt
) != LABEL_EXPR
)
4562 label
= LABEL_EXPR_LABEL (stmt
);
4563 if (!DECL_NONLOCAL (label
))
4566 bsi_move_before (&i
, &s
);
4571 label
= create_artificial_label ();
4572 stmt
= build1 (LABEL_EXPR
, void_type_node
, label
);
4573 bsi_insert_before (&s
, stmt
, BSI_NEW_STMT
);
4578 /* Attempt to perform edge redirection by replacing a possibly complex
4579 jump instruction by a goto or by removing the jump completely.
4580 This can apply only if all edges now point to the same block. The
4581 parameters and return values are equivalent to
4582 redirect_edge_and_branch. */
4585 tree_try_redirect_by_replacing_jump (edge e
, basic_block target
)
4587 basic_block src
= e
->src
;
4588 block_stmt_iterator b
;
4591 /* We can replace or remove a complex jump only when we have exactly
4593 if (EDGE_COUNT (src
->succs
) != 2
4594 /* Verify that all targets will be TARGET. Specifically, the
4595 edge that is not E must also go to TARGET. */
4596 || EDGE_SUCC (src
, EDGE_SUCC (src
, 0) == e
)->dest
!= target
)
4602 stmt
= bsi_stmt (b
);
4604 if (TREE_CODE (stmt
) == COND_EXPR
4605 || TREE_CODE (stmt
) == SWITCH_EXPR
)
4608 e
= ssa_redirect_edge (e
, target
);
4609 e
->flags
= EDGE_FALLTHRU
;
4617 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4618 edge representing the redirected branch. */
4621 tree_redirect_edge_and_branch (edge e
, basic_block dest
)
4623 basic_block bb
= e
->src
;
4624 block_stmt_iterator bsi
;
4628 if (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
))
4631 if (e
->src
!= ENTRY_BLOCK_PTR
4632 && (ret
= tree_try_redirect_by_replacing_jump (e
, dest
)))
4635 if (e
->dest
== dest
)
4638 label
= tree_block_label (dest
);
4640 bsi
= bsi_last (bb
);
4641 stmt
= bsi_end_p (bsi
) ? NULL
: bsi_stmt (bsi
);
4643 switch (stmt
? TREE_CODE (stmt
) : ERROR_MARK
)
4646 stmt
= (e
->flags
& EDGE_TRUE_VALUE
4647 ? COND_EXPR_THEN (stmt
)
4648 : COND_EXPR_ELSE (stmt
));
4649 GOTO_DESTINATION (stmt
) = label
;
4653 /* No non-abnormal edges should lead from a non-simple goto, and
4654 simple ones should be represented implicitly. */
4659 tree cases
= get_cases_for_edge (e
, stmt
);
4661 /* If we have a list of cases associated with E, then use it
4662 as it's a lot faster than walking the entire case vector. */
4665 edge e2
= find_edge (e
->src
, dest
);
4672 CASE_LABEL (cases
) = label
;
4673 cases
= TREE_CHAIN (cases
);
4676 /* If there was already an edge in the CFG, then we need
4677 to move all the cases associated with E to E2. */
4680 tree cases2
= get_cases_for_edge (e2
, stmt
);
4682 TREE_CHAIN (last
) = TREE_CHAIN (cases2
);
4683 TREE_CHAIN (cases2
) = first
;
4688 tree vec
= SWITCH_LABELS (stmt
);
4689 size_t i
, n
= TREE_VEC_LENGTH (vec
);
4691 for (i
= 0; i
< n
; i
++)
4693 tree elt
= TREE_VEC_ELT (vec
, i
);
4695 if (label_to_block (CASE_LABEL (elt
)) == e
->dest
)
4696 CASE_LABEL (elt
) = label
;
4705 e
->flags
|= EDGE_FALLTHRU
;
4709 /* Otherwise it must be a fallthru edge, and we don't need to
4710 do anything besides redirecting it. */
4711 gcc_assert (e
->flags
& EDGE_FALLTHRU
);
4715 /* Update/insert PHI nodes as necessary. */
4717 /* Now update the edges in the CFG. */
4718 e
= ssa_redirect_edge (e
, dest
);
4724 /* Simple wrapper, as we can always redirect fallthru edges. */
4727 tree_redirect_edge_and_branch_force (edge e
, basic_block dest
)
4729 e
= tree_redirect_edge_and_branch (e
, dest
);
4736 /* Splits basic block BB after statement STMT (but at least after the
4737 labels). If STMT is NULL, BB is split just after the labels. */
4740 tree_split_block (basic_block bb
, void *stmt
)
4742 block_stmt_iterator bsi
, bsi_tgt
;
4748 new_bb
= create_empty_bb (bb
);
4750 /* Redirect the outgoing edges. */
4751 new_bb
->succs
= bb
->succs
;
4753 FOR_EACH_EDGE (e
, ei
, new_bb
->succs
)
4756 if (stmt
&& TREE_CODE ((tree
) stmt
) == LABEL_EXPR
)
4759 /* Move everything from BSI to the new basic block. */
4760 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4762 act
= bsi_stmt (bsi
);
4763 if (TREE_CODE (act
) == LABEL_EXPR
)
4776 bsi_tgt
= bsi_start (new_bb
);
4777 while (!bsi_end_p (bsi
))
4779 act
= bsi_stmt (bsi
);
4781 bsi_insert_after (&bsi_tgt
, act
, BSI_NEW_STMT
);
4788 /* Moves basic block BB after block AFTER. */
4791 tree_move_block_after (basic_block bb
, basic_block after
)
4793 if (bb
->prev_bb
== after
)
4797 link_block (bb
, after
);
4803 /* Return true if basic_block can be duplicated. */
4806 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED
)
4811 /* Create a duplicate of the basic block BB. NOTE: This does not
4812 preserve SSA form. */
4815 tree_duplicate_bb (basic_block bb
)
4818 block_stmt_iterator bsi
, bsi_tgt
;
4820 ssa_op_iter op_iter
;
4822 new_bb
= create_empty_bb (EXIT_BLOCK_PTR
->prev_bb
);
4824 /* First copy the phi nodes. We do not copy phi node arguments here,
4825 since the edges are not ready yet. Keep the chain of phi nodes in
4826 the same order, so that we can add them later. */
4827 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
4829 mark_for_rewrite (PHI_RESULT (phi
));
4830 create_phi_node (PHI_RESULT (phi
), new_bb
);
4832 set_phi_nodes (new_bb
, phi_reverse (phi_nodes (new_bb
)));
4834 bsi_tgt
= bsi_start (new_bb
);
4835 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4837 tree stmt
= bsi_stmt (bsi
);
4840 if (TREE_CODE (stmt
) == LABEL_EXPR
)
4843 /* Record the definitions. */
4844 get_stmt_operands (stmt
);
4846 FOR_EACH_SSA_TREE_OPERAND (val
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
4847 mark_for_rewrite (val
);
4849 copy
= unshare_expr (stmt
);
4851 /* Copy also the virtual operands. */
4852 get_stmt_ann (copy
);
4853 copy_virtual_operands (copy
, stmt
);
4855 bsi_insert_after (&bsi_tgt
, copy
, BSI_NEW_STMT
);
4861 /* Basic block BB_COPY was created by code duplication. Add phi node
4862 arguments for edges going out of BB_COPY. The blocks that were
4863 duplicated have rbi->duplicated set to one. */
4866 add_phi_args_after_copy_bb (basic_block bb_copy
)
4868 basic_block bb
, dest
;
4871 tree phi
, phi_copy
, phi_next
, def
;
4873 bb
= bb_copy
->rbi
->original
;
4875 FOR_EACH_EDGE (e_copy
, ei
, bb_copy
->succs
)
4877 if (!phi_nodes (e_copy
->dest
))
4880 if (e_copy
->dest
->rbi
->duplicated
)
4881 dest
= e_copy
->dest
->rbi
->original
;
4883 dest
= e_copy
->dest
;
4885 e
= find_edge (bb
, dest
);
4888 /* During loop unrolling the target of the latch edge is copied.
4889 In this case we are not looking for edge to dest, but to
4890 duplicated block whose original was dest. */
4891 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4892 if (e
->dest
->rbi
->duplicated
4893 && e
->dest
->rbi
->original
== dest
)
4896 gcc_assert (e
!= NULL
);
4899 for (phi
= phi_nodes (e
->dest
), phi_copy
= phi_nodes (e_copy
->dest
);
4901 phi
= phi_next
, phi_copy
= PHI_CHAIN (phi_copy
))
4903 phi_next
= PHI_CHAIN (phi
);
4905 gcc_assert (PHI_RESULT (phi
) == PHI_RESULT (phi_copy
));
4906 def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4907 add_phi_arg (phi_copy
, def
, e_copy
);
4912 /* Blocks in REGION_COPY array of length N_REGION were created by
4913 duplication of basic blocks. Add phi node arguments for edges
4914 going from these blocks. */
4917 add_phi_args_after_copy (basic_block
*region_copy
, unsigned n_region
)
4921 for (i
= 0; i
< n_region
; i
++)
4922 region_copy
[i
]->rbi
->duplicated
= 1;
4924 for (i
= 0; i
< n_region
; i
++)
4925 add_phi_args_after_copy_bb (region_copy
[i
]);
4927 for (i
= 0; i
< n_region
; i
++)
4928 region_copy
[i
]->rbi
->duplicated
= 0;
4931 /* Maps the old ssa name FROM_NAME to TO_NAME. */
4933 struct ssa_name_map_entry
4939 /* Hash function for ssa_name_map_entry. */
4942 ssa_name_map_entry_hash (const void *entry
)
4944 const struct ssa_name_map_entry
*en
= entry
;
4945 return SSA_NAME_VERSION (en
->from_name
);
4948 /* Equality function for ssa_name_map_entry. */
4951 ssa_name_map_entry_eq (const void *in_table
, const void *ssa_name
)
4953 const struct ssa_name_map_entry
*en
= in_table
;
4955 return en
->from_name
== ssa_name
;
4958 /* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping
4962 allocate_ssa_names (bitmap definitions
, htab_t
*map
)
4965 struct ssa_name_map_entry
*entry
;
4971 *map
= htab_create (10, ssa_name_map_entry_hash
,
4972 ssa_name_map_entry_eq
, free
);
4973 EXECUTE_IF_SET_IN_BITMAP (definitions
, 0, ver
, bi
)
4975 name
= ssa_name (ver
);
4976 slot
= htab_find_slot_with_hash (*map
, name
, SSA_NAME_VERSION (name
),
4982 entry
= xmalloc (sizeof (struct ssa_name_map_entry
));
4983 entry
->from_name
= name
;
4986 entry
->to_name
= duplicate_ssa_name (name
, SSA_NAME_DEF_STMT (name
));
4990 /* Rewrite the definition DEF in statement STMT to new ssa name as specified
4991 by the mapping MAP. */
4994 rewrite_to_new_ssa_names_def (def_operand_p def
, tree stmt
, htab_t map
)
4996 tree name
= DEF_FROM_PTR (def
);
4997 struct ssa_name_map_entry
*entry
;
4999 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
5001 entry
= htab_find_with_hash (map
, name
, SSA_NAME_VERSION (name
));
5005 SET_DEF (def
, entry
->to_name
);
5006 SSA_NAME_DEF_STMT (entry
->to_name
) = stmt
;
5009 /* Rewrite the USE to new ssa name as specified by the mapping MAP. */
5012 rewrite_to_new_ssa_names_use (use_operand_p use
, htab_t map
)
5014 tree name
= USE_FROM_PTR (use
);
5015 struct ssa_name_map_entry
*entry
;
5017 if (TREE_CODE (name
) != SSA_NAME
)
5020 entry
= htab_find_with_hash (map
, name
, SSA_NAME_VERSION (name
));
5024 SET_USE (use
, entry
->to_name
);
5027 /* Rewrite the ssa names in basic block BB to new ones as specified by the
5031 rewrite_to_new_ssa_names_bb (basic_block bb
, htab_t map
)
5037 block_stmt_iterator bsi
;
5041 v_may_def_optype v_may_defs
;
5042 v_must_def_optype v_must_defs
;
5045 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5046 if (e
->flags
& EDGE_ABNORMAL
)
5049 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
5051 rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi
), phi
, map
);
5053 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi
)) = 1;
5056 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
5058 stmt
= bsi_stmt (bsi
);
5059 get_stmt_operands (stmt
);
5060 ann
= stmt_ann (stmt
);
5062 uses
= USE_OPS (ann
);
5063 for (i
= 0; i
< NUM_USES (uses
); i
++)
5064 rewrite_to_new_ssa_names_use (USE_OP_PTR (uses
, i
), map
);
5066 defs
= DEF_OPS (ann
);
5067 for (i
= 0; i
< NUM_DEFS (defs
); i
++)
5068 rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs
, i
), stmt
, map
);
5070 vuses
= VUSE_OPS (ann
);
5071 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
5072 rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses
, i
), map
);
5074 v_may_defs
= V_MAY_DEF_OPS (ann
);
5075 for (i
= 0; i
< NUM_V_MAY_DEFS (v_may_defs
); i
++)
5077 rewrite_to_new_ssa_names_use
5078 (V_MAY_DEF_OP_PTR (v_may_defs
, i
), map
);
5079 rewrite_to_new_ssa_names_def
5080 (V_MAY_DEF_RESULT_PTR (v_may_defs
, i
), stmt
, map
);
5083 v_must_defs
= V_MUST_DEF_OPS (ann
);
5084 for (i
= 0; i
< NUM_V_MUST_DEFS (v_must_defs
); i
++)
5086 rewrite_to_new_ssa_names_def
5087 (V_MUST_DEF_RESULT_PTR (v_must_defs
, i
), stmt
, map
);
5088 rewrite_to_new_ssa_names_use
5089 (V_MUST_DEF_KILL_PTR (v_must_defs
, i
), map
);
5093 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5094 for (phi
= phi_nodes (e
->dest
); phi
; phi
= PHI_CHAIN (phi
))
5096 rewrite_to_new_ssa_names_use
5097 (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
), map
);
5099 if (e
->flags
& EDGE_ABNORMAL
)
5101 tree op
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
5102 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
) = 1;
5107 /* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified
5108 by the mapping MAP. */
5111 rewrite_to_new_ssa_names (basic_block
*region
, unsigned n_region
, htab_t map
)
5115 for (r
= 0; r
< n_region
; r
++)
5116 rewrite_to_new_ssa_names_bb (region
[r
], map
);
5119 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5120 important exit edge EXIT. By important we mean that no SSA name defined
5121 inside region is live over the other exit edges of the region. All entry
5122 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5123 to the duplicate of the region. SSA form, dominance and loop information
5124 is updated. The new basic blocks are stored to REGION_COPY in the same
5125 order as they had in REGION, provided that REGION_COPY is not NULL.
5126 The function returns false if it is unable to copy the region,
5130 tree_duplicate_sese_region (edge entry
, edge exit
,
5131 basic_block
*region
, unsigned n_region
,
5132 basic_block
*region_copy
)
5134 unsigned i
, n_doms
, ver
;
5135 bool free_region_copy
= false, copying_header
= false;
5136 struct loop
*loop
= entry
->dest
->loop_father
;
5141 htab_t ssa_name_map
= NULL
;
5145 if (!can_copy_bbs_p (region
, n_region
))
5148 /* Some sanity checking. Note that we do not check for all possible
5149 missuses of the functions. I.e. if you ask to copy something weird,
5150 it will work, but the state of structures probably will not be
5153 for (i
= 0; i
< n_region
; i
++)
5155 /* We do not handle subloops, i.e. all the blocks must belong to the
5157 if (region
[i
]->loop_father
!= loop
)
5160 if (region
[i
] != entry
->dest
5161 && region
[i
] == loop
->header
)
5167 /* In case the function is used for loop header copying (which is the primary
5168 use), ensure that EXIT and its copy will be new latch and entry edges. */
5169 if (loop
->header
== entry
->dest
)
5171 copying_header
= true;
5172 loop
->copy
= loop
->outer
;
5174 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit
->src
))
5177 for (i
= 0; i
< n_region
; i
++)
5178 if (region
[i
] != exit
->src
5179 && dominated_by_p (CDI_DOMINATORS
, region
[i
], exit
->src
))
5185 region_copy
= xmalloc (sizeof (basic_block
) * n_region
);
5186 free_region_copy
= true;
5189 gcc_assert (!any_marked_for_rewrite_p ());
5191 /* Record blocks outside the region that are duplicated by something
5193 doms
= xmalloc (sizeof (basic_block
) * n_basic_blocks
);
5194 n_doms
= get_dominated_by_region (CDI_DOMINATORS
, region
, n_region
, doms
);
5196 copy_bbs (region
, n_region
, region_copy
, &exit
, 1, &exit_copy
, loop
);
5197 definitions
= marked_ssa_names ();
5201 loop
->header
= exit
->dest
;
5202 loop
->latch
= exit
->src
;
5205 /* Redirect the entry and add the phi node arguments. */
5206 redirected
= redirect_edge_and_branch (entry
, entry
->dest
->rbi
->copy
);
5207 gcc_assert (redirected
!= NULL
);
5208 flush_pending_stmts (entry
);
5210 /* Concerning updating of dominators: We must recount dominators
5211 for entry block and its copy. Anything that is outside of the region, but
5212 was dominated by something inside needs recounting as well. */
5213 set_immediate_dominator (CDI_DOMINATORS
, entry
->dest
, entry
->src
);
5214 doms
[n_doms
++] = entry
->dest
->rbi
->original
;
5215 iterate_fix_dominators (CDI_DOMINATORS
, doms
, n_doms
);
5218 /* Add the other phi node arguments. */
5219 add_phi_args_after_copy (region_copy
, n_region
);
5221 /* Add phi nodes for definitions at exit. TODO -- once we have immediate
5222 uses, it should be possible to emit phi nodes just for definitions that
5223 are used outside region. */
5224 EXECUTE_IF_SET_IN_BITMAP (definitions
, 0, ver
, bi
)
5226 tree name
= ssa_name (ver
);
5228 phi
= create_phi_node (name
, exit
->dest
);
5229 add_phi_arg (phi
, name
, exit
);
5230 add_phi_arg (phi
, name
, exit_copy
);
5232 SSA_NAME_DEF_STMT (name
) = phi
;
5235 /* And create new definitions inside region and its copy. TODO -- once we
5236 have immediate uses, it might be better to leave definitions in region
5237 unchanged, create new ssa names for phi nodes on exit, and rewrite
5238 the uses, to avoid changing the copied region. */
5239 allocate_ssa_names (definitions
, &ssa_name_map
);
5240 rewrite_to_new_ssa_names (region
, n_region
, ssa_name_map
);
5241 allocate_ssa_names (definitions
, &ssa_name_map
);
5242 rewrite_to_new_ssa_names (region_copy
, n_region
, ssa_name_map
);
5243 htab_delete (ssa_name_map
);
5245 if (free_region_copy
)
5248 unmark_all_for_rewrite ();
5249 BITMAP_FREE (definitions
);
5254 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
5257 dump_function_to_file (tree fn
, FILE *file
, int flags
)
5259 tree arg
, vars
, var
;
5260 bool ignore_topmost_bind
= false, any_var
= false;
5264 fprintf (file
, "%s (", lang_hooks
.decl_printable_name (fn
, 2));
5266 arg
= DECL_ARGUMENTS (fn
);
5269 print_generic_expr (file
, arg
, dump_flags
);
5270 if (TREE_CHAIN (arg
))
5271 fprintf (file
, ", ");
5272 arg
= TREE_CHAIN (arg
);
5274 fprintf (file
, ")\n");
5276 if (flags
& TDF_RAW
)
5278 dump_node (fn
, TDF_SLIM
| flags
, file
);
5282 /* When GIMPLE is lowered, the variables are no longer available in
5283 BIND_EXPRs, so display them separately. */
5284 if (cfun
&& cfun
->unexpanded_var_list
)
5286 ignore_topmost_bind
= true;
5288 fprintf (file
, "{\n");
5289 for (vars
= cfun
->unexpanded_var_list
; vars
; vars
= TREE_CHAIN (vars
))
5291 var
= TREE_VALUE (vars
);
5293 print_generic_decl (file
, var
, flags
);
5294 fprintf (file
, "\n");
5300 if (basic_block_info
)
5302 /* Make a CFG based dump. */
5303 check_bb_profile (ENTRY_BLOCK_PTR
, file
);
5304 if (!ignore_topmost_bind
)
5305 fprintf (file
, "{\n");
5307 if (any_var
&& n_basic_blocks
)
5308 fprintf (file
, "\n");
5311 dump_generic_bb (file
, bb
, 2, flags
);
5313 fprintf (file
, "}\n");
5314 check_bb_profile (EXIT_BLOCK_PTR
, file
);
5320 /* Make a tree based dump. */
5321 chain
= DECL_SAVED_TREE (fn
);
5323 if (TREE_CODE (chain
) == BIND_EXPR
)
5325 if (ignore_topmost_bind
)
5327 chain
= BIND_EXPR_BODY (chain
);
5335 if (!ignore_topmost_bind
)
5336 fprintf (file
, "{\n");
5341 fprintf (file
, "\n");
5343 print_generic_stmt_indented (file
, chain
, flags
, indent
);
5344 if (ignore_topmost_bind
)
5345 fprintf (file
, "}\n");
5348 fprintf (file
, "\n\n");
5352 /* Pretty print of the loops intermediate representation. */
5353 static void print_loop (FILE *, struct loop
*, int);
5354 static void print_pred_bbs (FILE *, basic_block bb
);
5355 static void print_succ_bbs (FILE *, basic_block bb
);
5358 /* Print the predecessors indexes of edge E on FILE. */
5361 print_pred_bbs (FILE *file
, basic_block bb
)
5366 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5367 fprintf (file
, "bb_%d", e
->src
->index
);
5371 /* Print the successors indexes of edge E on FILE. */
5374 print_succ_bbs (FILE *file
, basic_block bb
)
5379 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5380 fprintf (file
, "bb_%d", e
->src
->index
);
5384 /* Pretty print LOOP on FILE, indented INDENT spaces. */
5387 print_loop (FILE *file
, struct loop
*loop
, int indent
)
5395 s_indent
= (char *) alloca ((size_t) indent
+ 1);
5396 memset ((void *) s_indent
, ' ', (size_t) indent
);
5397 s_indent
[indent
] = '\0';
5399 /* Print the loop's header. */
5400 fprintf (file
, "%sloop_%d\n", s_indent
, loop
->num
);
5402 /* Print the loop's body. */
5403 fprintf (file
, "%s{\n", s_indent
);
5405 if (bb
->loop_father
== loop
)
5407 /* Print the basic_block's header. */
5408 fprintf (file
, "%s bb_%d (preds = {", s_indent
, bb
->index
);
5409 print_pred_bbs (file
, bb
);
5410 fprintf (file
, "}, succs = {");
5411 print_succ_bbs (file
, bb
);
5412 fprintf (file
, "})\n");
5414 /* Print the basic_block's body. */
5415 fprintf (file
, "%s {\n", s_indent
);
5416 tree_dump_bb (bb
, file
, indent
+ 4);
5417 fprintf (file
, "%s }\n", s_indent
);
5420 print_loop (file
, loop
->inner
, indent
+ 2);
5421 fprintf (file
, "%s}\n", s_indent
);
5422 print_loop (file
, loop
->next
, indent
);
5426 /* Follow a CFG edge from the entry point of the program, and on entry
5427 of a loop, pretty print the loop structure on FILE. */
5430 print_loop_ir (FILE *file
)
5434 bb
= BASIC_BLOCK (0);
5435 if (bb
&& bb
->loop_father
)
5436 print_loop (file
, bb
->loop_father
, 0);
5440 /* Debugging loops structure at tree level. */
5443 debug_loop_ir (void)
5445 print_loop_ir (stderr
);
5449 /* Return true if BB ends with a call, possibly followed by some
5450 instructions that must stay with the call. Return false,
5454 tree_block_ends_with_call_p (basic_block bb
)
5456 block_stmt_iterator bsi
= bsi_last (bb
);
5457 return get_call_expr_in (bsi_stmt (bsi
)) != NULL
;
5461 /* Return true if BB ends with a conditional branch. Return false,
5465 tree_block_ends_with_condjump_p (basic_block bb
)
5467 tree stmt
= tsi_stmt (bsi_last (bb
).tsi
);
5468 return (TREE_CODE (stmt
) == COND_EXPR
);
5472 /* Return true if we need to add fake edge to exit at statement T.
5473 Helper function for tree_flow_call_edges_add. */
5476 need_fake_edge_p (tree t
)
5480 /* NORETURN and LONGJMP calls already have an edge to exit.
5481 CONST and PURE calls do not need one.
5482 We don't currently check for CONST and PURE here, although
5483 it would be a good idea, because those attributes are
5484 figured out from the RTL in mark_constant_function, and
5485 the counter incrementation code from -fprofile-arcs
5486 leads to different results from -fbranch-probabilities. */
5487 call
= get_call_expr_in (t
);
5489 && !(call_expr_flags (call
) & ECF_NORETURN
))
5492 if (TREE_CODE (t
) == ASM_EXPR
5493 && (ASM_VOLATILE_P (t
) || ASM_INPUT_P (t
)))
5500 /* Add fake edges to the function exit for any non constant and non
5501 noreturn calls, volatile inline assembly in the bitmap of blocks
5502 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
5503 the number of blocks that were split.
5505 The goal is to expose cases in which entering a basic block does
5506 not imply that all subsequent instructions must be executed. */
5509 tree_flow_call_edges_add (sbitmap blocks
)
5512 int blocks_split
= 0;
5513 int last_bb
= last_basic_block
;
5514 bool check_last_block
= false;
5516 if (n_basic_blocks
== 0)
5520 check_last_block
= true;
5522 check_last_block
= TEST_BIT (blocks
, EXIT_BLOCK_PTR
->prev_bb
->index
);
5524 /* In the last basic block, before epilogue generation, there will be
5525 a fallthru edge to EXIT. Special care is required if the last insn
5526 of the last basic block is a call because make_edge folds duplicate
5527 edges, which would result in the fallthru edge also being marked
5528 fake, which would result in the fallthru edge being removed by
5529 remove_fake_edges, which would result in an invalid CFG.
5531 Moreover, we can't elide the outgoing fake edge, since the block
5532 profiler needs to take this into account in order to solve the minimal
5533 spanning tree in the case that the call doesn't return.
5535 Handle this by adding a dummy instruction in a new last basic block. */
5536 if (check_last_block
)
5538 basic_block bb
= EXIT_BLOCK_PTR
->prev_bb
;
5539 block_stmt_iterator bsi
= bsi_last (bb
);
5541 if (!bsi_end_p (bsi
))
5544 if (need_fake_edge_p (t
))
5548 e
= find_edge (bb
, EXIT_BLOCK_PTR
);
5551 bsi_insert_on_edge (e
, build_empty_stmt ());
5552 bsi_commit_edge_inserts ();
5557 /* Now add fake edges to the function exit for any non constant
5558 calls since there is no way that we can determine if they will
5560 for (i
= 0; i
< last_bb
; i
++)
5562 basic_block bb
= BASIC_BLOCK (i
);
5563 block_stmt_iterator bsi
;
5564 tree stmt
, last_stmt
;
5569 if (blocks
&& !TEST_BIT (blocks
, i
))
5572 bsi
= bsi_last (bb
);
5573 if (!bsi_end_p (bsi
))
5575 last_stmt
= bsi_stmt (bsi
);
5578 stmt
= bsi_stmt (bsi
);
5579 if (need_fake_edge_p (stmt
))
5582 /* The handling above of the final block before the
5583 epilogue should be enough to verify that there is
5584 no edge to the exit block in CFG already.
5585 Calling make_edge in such case would cause us to
5586 mark that edge as fake and remove it later. */
5587 #ifdef ENABLE_CHECKING
5588 if (stmt
== last_stmt
)
5590 e
= find_edge (bb
, EXIT_BLOCK_PTR
);
5591 gcc_assert (e
== NULL
);
5595 /* Note that the following may create a new basic block
5596 and renumber the existing basic blocks. */
5597 if (stmt
!= last_stmt
)
5599 e
= split_block (bb
, stmt
);
5603 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
5607 while (!bsi_end_p (bsi
));
5612 verify_flow_info ();
5614 return blocks_split
;
5618 tree_purge_dead_eh_edges (basic_block bb
)
5620 bool changed
= false;
5623 tree stmt
= last_stmt (bb
);
5625 if (stmt
&& tree_can_throw_internal (stmt
))
5628 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
5630 if (e
->flags
& EDGE_EH
)
5639 /* Removal of dead EH edges might change dominators of not
5640 just immediate successors. E.g. when bb1 is changed so that
5641 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5642 eh edges purged by this function in:
5654 idom(bb5) must be recomputed. For now just free the dominance
5657 free_dominance_info (CDI_DOMINATORS
);
5663 tree_purge_all_dead_eh_edges (bitmap blocks
)
5665 bool changed
= false;
5669 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, i
, bi
)
5671 changed
|= tree_purge_dead_eh_edges (BASIC_BLOCK (i
));
5677 /* This function is called whenever a new edge is created or
5681 tree_execute_on_growing_pred (edge e
)
5683 basic_block bb
= e
->dest
;
5686 reserve_phi_args_for_new_edge (bb
);
5689 /* This function is called immediately before edge E is removed from
5690 the edge vector E->dest->preds. */
5693 tree_execute_on_shrinking_pred (edge e
)
5695 if (phi_nodes (e
->dest
))
5696 remove_phi_args (e
);
5699 struct cfg_hooks tree_cfg_hooks
= {
5701 tree_verify_flow_info
,
5702 tree_dump_bb
, /* dump_bb */
5703 create_bb
, /* create_basic_block */
5704 tree_redirect_edge_and_branch
,/* redirect_edge_and_branch */
5705 tree_redirect_edge_and_branch_force
,/* redirect_edge_and_branch_force */
5706 remove_bb
, /* delete_basic_block */
5707 tree_split_block
, /* split_block */
5708 tree_move_block_after
, /* move_block_after */
5709 tree_can_merge_blocks_p
, /* can_merge_blocks_p */
5710 tree_merge_blocks
, /* merge_blocks */
5711 tree_predict_edge
, /* predict_edge */
5712 tree_predicted_by_p
, /* predicted_by_p */
5713 tree_can_duplicate_bb_p
, /* can_duplicate_block_p */
5714 tree_duplicate_bb
, /* duplicate_block */
5715 tree_split_edge
, /* split_edge */
5716 tree_make_forwarder_block
, /* make_forward_block */
5717 NULL
, /* tidy_fallthru_edge */
5718 tree_block_ends_with_call_p
, /* block_ends_with_call_p */
5719 tree_block_ends_with_condjump_p
, /* block_ends_with_condjump_p */
5720 tree_flow_call_edges_add
, /* flow_call_edges_add */
5721 tree_execute_on_growing_pred
, /* execute_on_growing_pred */
5722 tree_execute_on_shrinking_pred
, /* execute_on_shrinking_pred */
5726 /* Split all critical edges. */
5729 split_critical_edges (void)
5735 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
5736 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
5737 mappings around the calls to split_edge. */
5738 start_recording_case_labels ();
5741 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5742 if (EDGE_CRITICAL_P (e
) && !(e
->flags
& EDGE_ABNORMAL
))
5747 end_recording_case_labels ();
5750 struct tree_opt_pass pass_split_crit_edges
=
5752 "crited", /* name */
5754 split_critical_edges
, /* execute */
5757 0, /* static_pass_number */
5758 TV_TREE_SPLIT_EDGES
, /* tv_id */
5759 PROP_cfg
, /* properties required */
5760 PROP_no_crit_edges
, /* properties_provided */
5761 0, /* properties_destroyed */
5762 0, /* todo_flags_start */
5763 TODO_dump_func
, /* todo_flags_finish */
5768 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5769 a temporary, make sure and register it to be renamed if necessary,
5770 and finally return the temporary. Put the statements to compute
5771 EXP before the current statement in BSI. */
5774 gimplify_val (block_stmt_iterator
*bsi
, tree type
, tree exp
)
5776 tree t
, new_stmt
, orig_stmt
;
5778 if (is_gimple_val (exp
))
5781 t
= make_rename_temp (type
, NULL
);
5782 new_stmt
= build (MODIFY_EXPR
, type
, t
, exp
);
5784 orig_stmt
= bsi_stmt (*bsi
);
5785 SET_EXPR_LOCUS (new_stmt
, EXPR_LOCUS (orig_stmt
));
5786 TREE_BLOCK (new_stmt
) = TREE_BLOCK (orig_stmt
);
5788 bsi_insert_before (bsi
, new_stmt
, BSI_SAME_STMT
);
5793 /* Build a ternary operation and gimplify it. Emit code before BSI.
5794 Return the gimple_val holding the result. */
5797 gimplify_build3 (block_stmt_iterator
*bsi
, enum tree_code code
,
5798 tree type
, tree a
, tree b
, tree c
)
5802 ret
= fold (build3 (code
, type
, a
, b
, c
));
5805 return gimplify_val (bsi
, type
, ret
);
5808 /* Build a binary operation and gimplify it. Emit code before BSI.
5809 Return the gimple_val holding the result. */
5812 gimplify_build2 (block_stmt_iterator
*bsi
, enum tree_code code
,
5813 tree type
, tree a
, tree b
)
5817 ret
= fold (build2 (code
, type
, a
, b
));
5820 return gimplify_val (bsi
, type
, ret
);
5823 /* Build a unary operation and gimplify it. Emit code before BSI.
5824 Return the gimple_val holding the result. */
5827 gimplify_build1 (block_stmt_iterator
*bsi
, enum tree_code code
, tree type
,
5832 ret
= fold (build1 (code
, type
, a
));
5835 return gimplify_val (bsi
, type
, ret
);
5840 /* Emit return warnings. */
5843 execute_warn_function_return (void)
5845 #ifdef USE_MAPPED_LOCATION
5846 source_location location
;
5854 if (warn_missing_noreturn
5855 && !TREE_THIS_VOLATILE (cfun
->decl
)
5856 && EDGE_COUNT (EXIT_BLOCK_PTR
->preds
) == 0
5857 && !lang_hooks
.function
.missing_noreturn_ok_p (cfun
->decl
))
5858 warning ("%Jfunction might be possible candidate for "
5859 "attribute %<noreturn%>",
5862 /* If we have a path to EXIT, then we do return. */
5863 if (TREE_THIS_VOLATILE (cfun
->decl
)
5864 && EDGE_COUNT (EXIT_BLOCK_PTR
->preds
) > 0)
5866 #ifdef USE_MAPPED_LOCATION
5867 location
= UNKNOWN_LOCATION
;
5871 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
5873 last
= last_stmt (e
->src
);
5874 if (TREE_CODE (last
) == RETURN_EXPR
5875 #ifdef USE_MAPPED_LOCATION
5876 && (location
= EXPR_LOCATION (last
)) != UNKNOWN_LOCATION
)
5878 && (locus
= EXPR_LOCUS (last
)) != NULL
)
5882 #ifdef USE_MAPPED_LOCATION
5883 if (location
== UNKNOWN_LOCATION
)
5884 location
= cfun
->function_end_locus
;
5885 warning ("%H%<noreturn%> function does return", &location
);
5888 locus
= &cfun
->function_end_locus
;
5889 warning ("%H%<noreturn%> function does return", locus
);
5893 /* If we see "return;" in some basic block, then we do reach the end
5894 without returning a value. */
5895 else if (warn_return_type
5896 && !TREE_NO_WARNING (cfun
->decl
)
5897 && EDGE_COUNT (EXIT_BLOCK_PTR
->preds
) > 0
5898 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun
->decl
))))
5900 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
5902 tree last
= last_stmt (e
->src
);
5903 if (TREE_CODE (last
) == RETURN_EXPR
5904 && TREE_OPERAND (last
, 0) == NULL
)
5906 #ifdef USE_MAPPED_LOCATION
5907 location
= EXPR_LOCATION (last
);
5908 if (location
== UNKNOWN_LOCATION
)
5909 location
= cfun
->function_end_locus
;
5910 warning ("%Hcontrol reaches end of non-void function", &location
);
5912 locus
= EXPR_LOCUS (last
);
5914 locus
= &cfun
->function_end_locus
;
5915 warning ("%Hcontrol reaches end of non-void function", locus
);
5917 TREE_NO_WARNING (cfun
->decl
) = 1;
5925 /* Given a basic block B which ends with a conditional and has
5926 precisely two successors, determine which of the edges is taken if
5927 the conditional is true and which is taken if the conditional is
5928 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5931 extract_true_false_edges_from_block (basic_block b
,
5935 edge e
= EDGE_SUCC (b
, 0);
5937 if (e
->flags
& EDGE_TRUE_VALUE
)
5940 *false_edge
= EDGE_SUCC (b
, 1);
5945 *true_edge
= EDGE_SUCC (b
, 1);
5949 struct tree_opt_pass pass_warn_function_return
=
5953 execute_warn_function_return
, /* execute */
5956 0, /* static_pass_number */
5958 PROP_cfg
, /* properties_required */
5959 0, /* properties_provided */
5960 0, /* properties_destroyed */
5961 0, /* todo_flags_start */
5962 0, /* todo_flags_finish */
5966 #include "gt-tree-cfg.h"