1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
47 /* This file contains functions for building the Control Flow Graph (CFG)
48 for a function tree. */
50 /* Local declarations. */
52 /* Initial capacity for the basic block array. */
53 static const int initial_cfg_capacity
= 20;
55 /* Mapping of labels to their associated blocks. This can greatly speed up
56 building of the CFG in code with lots of gotos. */
57 static GTY(()) varray_type label_to_block_map
;
62 long num_merged_labels
;
65 static struct cfg_stats_d cfg_stats
;
67 /* Nonzero if we found a computed goto while building basic blocks. */
68 static bool found_computed_goto
;
70 /* Basic blocks and flowgraphs. */
71 static basic_block
create_bb (void *, void *, basic_block
);
72 static void create_block_annotation (basic_block
);
73 static void free_blocks_annotations (void);
74 static void clear_blocks_annotations (void);
75 static void make_blocks (tree
);
76 static void factor_computed_gotos (void);
79 static void make_edges (void);
80 static void make_ctrl_stmt_edges (basic_block
);
81 static void make_exit_edges (basic_block
);
82 static void make_cond_expr_edges (basic_block
);
83 static void make_switch_expr_edges (basic_block
);
84 static void make_goto_expr_edges (basic_block
);
85 static edge
tree_redirect_edge_and_branch (edge
, basic_block
);
86 static edge
tree_try_redirect_by_replacing_jump (edge
, basic_block
);
87 static void split_critical_edges (void);
89 /* Various helpers. */
90 static inline bool stmt_starts_bb_p (tree
, tree
);
91 static int tree_verify_flow_info (void);
92 static void tree_make_forwarder_block (edge
);
93 static bool thread_jumps (void);
94 static bool tree_forwarder_block_p (basic_block
);
95 static void bsi_commit_edge_inserts_1 (edge e
);
96 static void tree_cfg2vcg (FILE *);
98 /* Flowgraph optimization and cleanup. */
99 static void tree_merge_blocks (basic_block
, basic_block
);
100 static bool tree_can_merge_blocks_p (basic_block
, basic_block
);
101 static void remove_bb (basic_block
);
102 static bool cleanup_control_flow (void);
103 static bool cleanup_control_expr_graph (basic_block
, block_stmt_iterator
);
104 static edge
find_taken_edge_cond_expr (basic_block
, tree
);
105 static edge
find_taken_edge_switch_expr (basic_block
, tree
);
106 static tree
find_case_label_for_value (tree
, tree
);
107 static bool phi_alternatives_equal (basic_block
, edge
, edge
);
110 /*---------------------------------------------------------------------------
112 ---------------------------------------------------------------------------*/
114 /* Entry point to the CFG builder for trees. TP points to the list of
115 statements to be added to the flowgraph. */
118 build_tree_cfg (tree
*tp
)
120 /* Register specific tree functions. */
121 tree_register_cfg_hooks ();
123 /* Initialize rbi_pool. */
126 /* Initialize the basic block array. */
129 last_basic_block
= 0;
130 VARRAY_BB_INIT (basic_block_info
, initial_cfg_capacity
, "basic_block_info");
131 memset ((void *) &cfg_stats
, 0, sizeof (cfg_stats
));
133 /* Build a mapping of labels to their associated blocks. */
134 VARRAY_BB_INIT (label_to_block_map
, initial_cfg_capacity
,
135 "label to block map");
137 ENTRY_BLOCK_PTR
->next_bb
= EXIT_BLOCK_PTR
;
138 EXIT_BLOCK_PTR
->prev_bb
= ENTRY_BLOCK_PTR
;
140 found_computed_goto
= 0;
143 /* Computed gotos are hell to deal with, especially if there are
144 lots of them with a large number of destinations. So we factor
145 them to a common computed goto location before we build the
146 edge list. After we convert back to normal form, we will un-factor
147 the computed gotos since factoring introduces an unwanted jump. */
148 if (found_computed_goto
)
149 factor_computed_gotos ();
151 /* Make sure there is always at least one block, even if its empty. */
152 if (n_basic_blocks
== 0)
153 create_empty_bb (ENTRY_BLOCK_PTR
);
155 create_block_annotation (ENTRY_BLOCK_PTR
);
156 create_block_annotation (EXIT_BLOCK_PTR
);
158 /* Adjust the size of the array. */
159 VARRAY_GROW (basic_block_info
, n_basic_blocks
);
161 /* To speed up statement iterator walks, we first purge dead labels. */
162 cleanup_dead_labels ();
164 /* Group case nodes to reduce the number of edges.
165 We do this after cleaning up dead labels because otherwise we miss
166 a lot of obvious case merging opportunities. */
167 group_case_labels ();
169 /* Create the edges of the flowgraph. */
172 /* Debugging dumps. */
174 /* Write the flowgraph to a VCG file. */
176 int local_dump_flags
;
177 FILE *dump_file
= dump_begin (TDI_vcg
, &local_dump_flags
);
180 tree_cfg2vcg (dump_file
);
181 dump_end (TDI_vcg
, dump_file
);
185 /* Dump a textual representation of the flowgraph. */
187 dump_tree_cfg (dump_file
, dump_flags
);
191 execute_build_cfg (void)
193 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl
));
196 struct tree_opt_pass pass_build_cfg
=
200 execute_build_cfg
, /* execute */
203 0, /* static_pass_number */
204 TV_TREE_CFG
, /* tv_id */
205 PROP_gimple_leh
, /* properties_required */
206 PROP_cfg
, /* properties_provided */
207 0, /* properties_destroyed */
208 0, /* todo_flags_start */
209 TODO_verify_stmts
/* todo_flags_finish */
212 /* Search the CFG for any computed gotos. If found, factor them to a
213 common computed goto site. Also record the location of that site so
214 that we can un-factor the gotos after we have converted back to
218 factor_computed_gotos (void)
221 tree factored_label_decl
= NULL
;
223 tree factored_computed_goto_label
= NULL
;
224 tree factored_computed_goto
= NULL
;
226 /* We know there are one or more computed gotos in this function.
227 Examine the last statement in each basic block to see if the block
228 ends with a computed goto. */
232 block_stmt_iterator bsi
= bsi_last (bb
);
237 last
= bsi_stmt (bsi
);
239 /* Ignore the computed goto we create when we factor the original
241 if (last
== factored_computed_goto
)
244 /* If the last statement is a computed goto, factor it. */
245 if (computed_goto_p (last
))
249 /* The first time we find a computed goto we need to create
250 the factored goto block and the variable each original
251 computed goto will use for their goto destination. */
252 if (! factored_computed_goto
)
254 basic_block new_bb
= create_empty_bb (bb
);
255 block_stmt_iterator new_bsi
= bsi_start (new_bb
);
257 /* Create the destination of the factored goto. Each original
258 computed goto will put its desired destination into this
259 variable and jump to the label we create immediately
261 var
= create_tmp_var (ptr_type_node
, "gotovar");
263 /* Build a label for the new block which will contain the
264 factored computed goto. */
265 factored_label_decl
= create_artificial_label ();
266 factored_computed_goto_label
267 = build1 (LABEL_EXPR
, void_type_node
, factored_label_decl
);
268 bsi_insert_after (&new_bsi
, factored_computed_goto_label
,
271 /* Build our new computed goto. */
272 factored_computed_goto
= build1 (GOTO_EXPR
, void_type_node
, var
);
273 bsi_insert_after (&new_bsi
, factored_computed_goto
,
277 /* Copy the original computed goto's destination into VAR. */
278 assignment
= build (MODIFY_EXPR
, ptr_type_node
,
279 var
, GOTO_DESTINATION (last
));
280 bsi_insert_before (&bsi
, assignment
, BSI_SAME_STMT
);
282 /* And re-vector the computed goto to the new destination. */
283 GOTO_DESTINATION (last
) = factored_label_decl
;
289 /* Create annotations for a single basic block. */
292 create_block_annotation (basic_block bb
)
294 /* Verify that the tree_annotations field is clear. */
295 if (bb
->tree_annotations
)
297 bb
->tree_annotations
= ggc_alloc_cleared (sizeof (struct bb_ann_d
));
301 /* Free the annotations for all the basic blocks. */
303 static void free_blocks_annotations (void)
305 clear_blocks_annotations ();
309 /* Clear the annotations for all the basic blocks. */
312 clear_blocks_annotations (void)
316 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
317 bb
->tree_annotations
= NULL
;
321 /* Build a flowgraph for the statement_list STMT_LIST. */
324 make_blocks (tree stmt_list
)
326 tree_stmt_iterator i
= tsi_start (stmt_list
);
328 bool start_new_block
= true;
329 bool first_stmt_of_list
= true;
330 basic_block bb
= ENTRY_BLOCK_PTR
;
332 while (!tsi_end_p (i
))
339 /* If the statement starts a new basic block or if we have determined
340 in a previous pass that we need to create a new block for STMT, do
342 if (start_new_block
|| stmt_starts_bb_p (stmt
, prev_stmt
))
344 if (!first_stmt_of_list
)
345 stmt_list
= tsi_split_statement_list_before (&i
);
346 bb
= create_basic_block (stmt_list
, NULL
, bb
);
347 start_new_block
= false;
350 /* Now add STMT to BB and create the subgraphs for special statement
352 set_bb_for_stmt (stmt
, bb
);
354 if (computed_goto_p (stmt
))
355 found_computed_goto
= true;
357 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
359 if (stmt_ends_bb_p (stmt
))
360 start_new_block
= true;
363 first_stmt_of_list
= false;
368 /* Create and return a new empty basic block after bb AFTER. */
371 create_bb (void *h
, void *e
, basic_block after
)
378 /* Create and initialize a new basic block. */
380 memset (bb
, 0, sizeof (*bb
));
382 bb
->index
= last_basic_block
;
384 bb
->stmt_list
= h
? h
: alloc_stmt_list ();
386 /* Add the new block to the linked list of blocks. */
387 link_block (bb
, after
);
389 /* Grow the basic block array if needed. */
390 if ((size_t) last_basic_block
== VARRAY_SIZE (basic_block_info
))
392 size_t new_size
= last_basic_block
+ (last_basic_block
+ 3) / 4;
393 VARRAY_GROW (basic_block_info
, new_size
);
396 /* Add the newly created block to the array. */
397 BASIC_BLOCK (last_basic_block
) = bb
;
399 create_block_annotation (bb
);
404 initialize_bb_rbi (bb
);
409 /*---------------------------------------------------------------------------
411 ---------------------------------------------------------------------------*/
413 /* Join all the blocks in the flowgraph. */
420 /* Create an edge from entry to the first block with executable
422 make_edge (ENTRY_BLOCK_PTR
, BASIC_BLOCK (0), EDGE_FALLTHRU
);
424 /* Traverse basic block array placing edges. */
427 tree first
= first_stmt (bb
);
428 tree last
= last_stmt (bb
);
432 /* Edges for statements that always alter flow control. */
433 if (is_ctrl_stmt (last
))
434 make_ctrl_stmt_edges (bb
);
436 /* Edges for statements that sometimes alter flow control. */
437 if (is_ctrl_altering_stmt (last
))
438 make_exit_edges (bb
);
441 /* Finally, if no edges were created above, this is a regular
442 basic block that only needs a fallthru edge. */
443 if (bb
->succ
== NULL
)
444 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
447 /* We do not care about fake edges, so remove any that the CFG
448 builder inserted for completeness. */
449 remove_fake_exit_edges ();
451 /* Clean up the graph and warn for unreachable code. */
456 /* Create edges for control statement at basic block BB. */
459 make_ctrl_stmt_edges (basic_block bb
)
461 tree last
= last_stmt (bb
);
463 #if defined ENABLE_CHECKING
464 if (last
== NULL_TREE
)
468 switch (TREE_CODE (last
))
471 make_goto_expr_edges (bb
);
475 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
479 make_cond_expr_edges (bb
);
483 make_switch_expr_edges (bb
);
487 make_eh_edges (last
);
488 /* Yet another NORETURN hack. */
489 if (bb
->succ
== NULL
)
490 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
499 /* Create exit edges for statements in block BB that alter the flow of
500 control. Statements that alter the control flow are 'goto', 'return'
501 and calls to non-returning functions. */
504 make_exit_edges (basic_block bb
)
506 tree last
= last_stmt (bb
), op
;
508 if (last
== NULL_TREE
)
511 switch (TREE_CODE (last
))
514 /* If this function receives a nonlocal goto, then we need to
515 make edges from this call site to all the nonlocal goto
517 if (TREE_SIDE_EFFECTS (last
)
518 && current_function_has_nonlocal_label
)
519 make_goto_expr_edges (bb
);
521 /* If this statement has reachable exception handlers, then
522 create abnormal edges to them. */
523 make_eh_edges (last
);
525 /* Some calls are known not to return. For such calls we create
528 We really need to revamp how we build edges so that it's not
529 such a bloody pain to avoid creating edges for this case since
530 all we do is remove these edges when we're done building the
532 if (call_expr_flags (last
) & (ECF_NORETURN
| ECF_LONGJMP
))
534 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
538 /* Don't forget the fall-thru edge. */
539 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
543 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
544 may have an abnormal edge. Search the RHS for this case and
545 create any required edges. */
546 op
= get_call_expr_in (last
);
547 if (op
&& TREE_SIDE_EFFECTS (op
)
548 && current_function_has_nonlocal_label
)
549 make_goto_expr_edges (bb
);
551 make_eh_edges (last
);
552 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
561 /* Create the edges for a COND_EXPR starting at block BB.
562 At this point, both clauses must contain only simple gotos. */
565 make_cond_expr_edges (basic_block bb
)
567 tree entry
= last_stmt (bb
);
568 basic_block then_bb
, else_bb
;
569 tree then_label
, else_label
;
571 #if defined ENABLE_CHECKING
572 if (entry
== NULL_TREE
|| TREE_CODE (entry
) != COND_EXPR
)
576 /* Entry basic blocks for each component. */
577 then_label
= GOTO_DESTINATION (COND_EXPR_THEN (entry
));
578 else_label
= GOTO_DESTINATION (COND_EXPR_ELSE (entry
));
579 then_bb
= label_to_block (then_label
);
580 else_bb
= label_to_block (else_label
);
582 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
583 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
587 /* Create the edges for a SWITCH_EXPR starting at block BB.
588 At this point, the switch body has been lowered and the
589 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
592 make_switch_expr_edges (basic_block bb
)
594 tree entry
= last_stmt (bb
);
598 vec
= SWITCH_LABELS (entry
);
599 n
= TREE_VEC_LENGTH (vec
);
601 for (i
= 0; i
< n
; ++i
)
603 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
604 basic_block label_bb
= label_to_block (lab
);
605 make_edge (bb
, label_bb
, 0);
610 /* Return the basic block holding label DEST. */
613 label_to_block (tree dest
)
615 int uid
= LABEL_DECL_UID (dest
);
617 /* We would die hard when faced by undefined label. Emit label to
618 very first basic block. This will hopefully make even the dataflow
619 and undefined variable warnings quite right. */
620 if ((errorcount
|| sorrycount
) && uid
< 0)
622 block_stmt_iterator bsi
= bsi_start (BASIC_BLOCK (0));
625 stmt
= build1 (LABEL_EXPR
, void_type_node
, dest
);
626 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
627 uid
= LABEL_DECL_UID (dest
);
629 return VARRAY_BB (label_to_block_map
, uid
);
633 /* Create edges for a goto statement at block BB. */
636 make_goto_expr_edges (basic_block bb
)
639 basic_block target_bb
;
641 block_stmt_iterator last
= bsi_last (bb
);
643 goto_t
= bsi_stmt (last
);
645 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
646 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
647 from a nonlocal goto. */
648 if (TREE_CODE (goto_t
) != GOTO_EXPR
)
650 dest
= error_mark_node
;
655 dest
= GOTO_DESTINATION (goto_t
);
658 /* A GOTO to a local label creates normal edges. */
659 if (simple_goto_p (goto_t
))
661 edge e
= make_edge (bb
, label_to_block (dest
), EDGE_FALLTHRU
);
662 #ifdef USE_MAPPED_LOCATION
663 e
->goto_locus
= EXPR_LOCATION (goto_t
);
665 e
->goto_locus
= EXPR_LOCUS (goto_t
);
671 /* Nothing more to do for nonlocal gotos. */
672 if (TREE_CODE (dest
) == LABEL_DECL
)
675 /* Computed gotos remain. */
678 /* Look for the block starting with the destination label. In the
679 case of a computed goto, make an edge to any label block we find
681 FOR_EACH_BB (target_bb
)
683 block_stmt_iterator bsi
;
685 for (bsi
= bsi_start (target_bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
687 tree target
= bsi_stmt (bsi
);
689 if (TREE_CODE (target
) != LABEL_EXPR
)
693 /* Computed GOTOs. Make an edge to every label block that has
694 been marked as a potential target for a computed goto. */
695 (FORCED_LABEL (LABEL_EXPR_LABEL (target
)) && for_call
== 0)
696 /* Nonlocal GOTO target. Make an edge to every label block
697 that has been marked as a potential target for a nonlocal
699 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target
)) && for_call
== 1))
701 make_edge (bb
, target_bb
, EDGE_ABNORMAL
);
707 /* Degenerate case of computed goto with no labels. */
708 if (!for_call
&& !bb
->succ
)
709 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
713 /*---------------------------------------------------------------------------
715 ---------------------------------------------------------------------------*/
717 /* Remove unreachable blocks and other miscellaneous clean up work. */
720 cleanup_tree_cfg (void)
722 bool something_changed
= true;
724 timevar_push (TV_TREE_CLEANUP_CFG
);
726 /* These three transformations can cascade, so we iterate on them until
728 while (something_changed
)
730 something_changed
= cleanup_control_flow ();
731 something_changed
|= delete_unreachable_blocks ();
732 something_changed
|= thread_jumps ();
735 /* Merging the blocks creates no new opportunities for the other
736 optimizations, so do it here. */
741 #ifdef ENABLE_CHECKING
744 timevar_pop (TV_TREE_CLEANUP_CFG
);
748 /* Cleanup useless labels in basic blocks. This is something we wish
749 to do early because it allows us to group case labels before creating
750 the edges for the CFG, and it speeds up block statement iterators in
752 We only run this pass once, running it more than once is probably not
755 /* A map from basic block index to the leading label of that block. */
756 static tree
*label_for_bb
;
758 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
760 update_eh_label (struct eh_region
*region
)
762 tree old_label
= get_eh_region_tree_label (region
);
766 basic_block bb
= label_to_block (old_label
);
768 /* ??? After optimizing, there may be EH regions with labels
769 that have already been removed from the function body, so
770 there is no basic block for them. */
774 new_label
= label_for_bb
[bb
->index
];
775 set_eh_region_tree_label (region
, new_label
);
779 /* Given LABEL return the first label in the same basic block. */
781 main_block_label (tree label
)
783 basic_block bb
= label_to_block (label
);
785 /* label_to_block possibly inserted undefined label into the chain. */
786 if (!label_for_bb
[bb
->index
])
787 label_for_bb
[bb
->index
] = label
;
788 return label_for_bb
[bb
->index
];
791 /* Cleanup redundant labels. This is a three-steo process:
792 1) Find the leading label for each block.
793 2) Redirect all references to labels to the leading labels.
794 3) Cleanup all useless labels. */
797 cleanup_dead_labels (void)
800 label_for_bb
= xcalloc (last_basic_block
, sizeof (tree
));
802 /* Find a suitable label for each block. We use the first user-defined
803 label is there is one, or otherwise just the first label we see. */
806 block_stmt_iterator i
;
808 for (i
= bsi_start (bb
); !bsi_end_p (i
); bsi_next (&i
))
810 tree label
, stmt
= bsi_stmt (i
);
812 if (TREE_CODE (stmt
) != LABEL_EXPR
)
815 label
= LABEL_EXPR_LABEL (stmt
);
817 /* If we have not yet seen a label for the current block,
818 remember this one and see if there are more labels. */
819 if (! label_for_bb
[bb
->index
])
821 label_for_bb
[bb
->index
] = label
;
825 /* If we did see a label for the current block already, but it
826 is an artificially created label, replace it if the current
827 label is a user defined label. */
828 if (! DECL_ARTIFICIAL (label
)
829 && DECL_ARTIFICIAL (label_for_bb
[bb
->index
]))
831 label_for_bb
[bb
->index
] = label
;
837 /* Now redirect all jumps/branches to the selected label.
838 First do so for each block ending in a control statement. */
841 tree stmt
= last_stmt (bb
);
845 switch (TREE_CODE (stmt
))
849 tree true_branch
, false_branch
;
851 true_branch
= COND_EXPR_THEN (stmt
);
852 false_branch
= COND_EXPR_ELSE (stmt
);
854 GOTO_DESTINATION (true_branch
)
855 = main_block_label (GOTO_DESTINATION (true_branch
));
856 GOTO_DESTINATION (false_branch
)
857 = main_block_label (GOTO_DESTINATION (false_branch
));
865 tree vec
= SWITCH_LABELS (stmt
);
866 size_t n
= TREE_VEC_LENGTH (vec
);
868 /* Replace all destination labels. */
869 for (i
= 0; i
< n
; ++i
)
870 CASE_LABEL (TREE_VEC_ELT (vec
, i
))
871 = main_block_label (CASE_LABEL (TREE_VEC_ELT (vec
, i
)));
876 /* We have to handle GOTO_EXPRs until they're removed, and we don't
877 remove them until after we've created the CFG edges. */
879 if (! computed_goto_p (stmt
))
881 GOTO_DESTINATION (stmt
)
882 = main_block_label (GOTO_DESTINATION (stmt
));
891 for_each_eh_region (update_eh_label
);
893 /* Finally, purge dead labels. All user-defined labels and labels that
894 can be the target of non-local gotos are preserved. */
897 block_stmt_iterator i
;
898 tree label_for_this_bb
= label_for_bb
[bb
->index
];
900 if (! label_for_this_bb
)
903 for (i
= bsi_start (bb
); !bsi_end_p (i
); )
905 tree label
, stmt
= bsi_stmt (i
);
907 if (TREE_CODE (stmt
) != LABEL_EXPR
)
910 label
= LABEL_EXPR_LABEL (stmt
);
912 if (label
== label_for_this_bb
913 || ! DECL_ARTIFICIAL (label
)
914 || DECL_NONLOCAL (label
))
924 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
925 and scan the sorted vector of cases. Combine the ones jumping to the
927 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
930 group_case_labels (void)
936 tree stmt
= last_stmt (bb
);
937 if (stmt
&& TREE_CODE (stmt
) == SWITCH_EXPR
)
939 tree labels
= SWITCH_LABELS (stmt
);
940 int old_size
= TREE_VEC_LENGTH (labels
);
941 int i
, j
, new_size
= old_size
;
942 tree default_label
= TREE_VEC_ELT (labels
, old_size
- 1);
944 /* Look for possible opportunities to merge cases.
945 Ignore the last element of the label vector because it
946 must be the default case. */
948 while (i
< old_size
- 2)
950 tree base_case
, base_label
, base_high
, type
;
951 base_case
= TREE_VEC_ELT (labels
, i
);
956 base_label
= CASE_LABEL (base_case
);
958 /* Discard cases that have the same destination as the
960 if (base_label
== default_label
)
962 TREE_VEC_ELT (labels
, i
) = NULL_TREE
;
967 type
= TREE_TYPE (CASE_LOW (base_case
));
968 base_high
= CASE_HIGH (base_case
) ?
969 CASE_HIGH (base_case
) : CASE_LOW (base_case
);
971 /* Try to merge case labels. Break out when we reach the end
972 of the label vector or when we cannot merge the next case
973 label with the current one. */
974 while (i
< old_size
- 2)
976 tree merge_case
= TREE_VEC_ELT (labels
, ++i
);
977 tree merge_label
= CASE_LABEL (merge_case
);
978 tree t
= int_const_binop (PLUS_EXPR
, base_high
,
979 integer_one_node
, 1);
981 /* Merge the cases if they jump to the same place,
982 and their ranges are consecutive. */
983 if (merge_label
== base_label
984 && tree_int_cst_equal (CASE_LOW (merge_case
), t
))
986 base_high
= CASE_HIGH (merge_case
) ?
987 CASE_HIGH (merge_case
) : CASE_LOW (merge_case
);
988 CASE_HIGH (base_case
) = base_high
;
989 TREE_VEC_ELT (labels
, i
) = NULL_TREE
;
997 /* Compress the case labels in the label vector, and adjust the
998 length of the vector. */
999 for (i
= 0, j
= 0; i
< new_size
; i
++)
1001 while (! TREE_VEC_ELT (labels
, j
))
1003 TREE_VEC_ELT (labels
, i
) = TREE_VEC_ELT (labels
, j
++);
1005 TREE_VEC_LENGTH (labels
) = new_size
;
1010 /* Checks whether we can merge block B into block A. */
1013 tree_can_merge_blocks_p (basic_block a
, basic_block b
)
1016 block_stmt_iterator bsi
;
1019 || a
->succ
->succ_next
)
1022 if (a
->succ
->flags
& EDGE_ABNORMAL
)
1025 if (a
->succ
->dest
!= b
)
1028 if (b
== EXIT_BLOCK_PTR
)
1031 if (b
->pred
->pred_next
)
1034 /* If A ends by a statement causing exceptions or something similar, we
1035 cannot merge the blocks. */
1036 stmt
= last_stmt (a
);
1037 if (stmt
&& stmt_ends_bb_p (stmt
))
1040 /* Do not allow a block with only a non-local label to be merged. */
1041 if (stmt
&& TREE_CODE (stmt
) == LABEL_EXPR
1042 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
1045 /* There may be no phi nodes at the start of b. Most of these degenerate
1046 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1050 /* Do not remove user labels. */
1051 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1053 stmt
= bsi_stmt (bsi
);
1054 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1056 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt
)))
1064 /* Merge block B into block A. */
1067 tree_merge_blocks (basic_block a
, basic_block b
)
1069 block_stmt_iterator bsi
;
1070 tree_stmt_iterator last
;
1073 fprintf (dump_file
, "Merging blocks %d and %d\n", a
->index
, b
->index
);
1075 /* Ensure that B follows A. */
1076 move_block_after (b
, a
);
1078 if (!(a
->succ
->flags
& EDGE_FALLTHRU
))
1082 && stmt_ends_bb_p (last_stmt (a
)))
1085 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1086 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
);)
1088 if (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
)
1092 set_bb_for_stmt (bsi_stmt (bsi
), a
);
1097 /* Merge the chains. */
1098 last
= tsi_last (a
->stmt_list
);
1099 tsi_link_after (&last
, b
->stmt_list
, TSI_NEW_STMT
);
1100 b
->stmt_list
= NULL
;
1104 /* Walk the function tree removing unnecessary statements.
1106 * Empty statement nodes are removed
1108 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1110 * Unnecessary COND_EXPRs are removed
1112 * Some unnecessary BIND_EXPRs are removed
1114 Clearly more work could be done. The trick is doing the analysis
1115 and removal fast enough to be a net improvement in compile times.
1117 Note that when we remove a control structure such as a COND_EXPR
1118 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1119 to ensure we eliminate all the useless code. */
1130 static void remove_useless_stmts_1 (tree
*, struct rus_data
*);
1133 remove_useless_stmts_warn_notreached (tree stmt
)
1135 if (EXPR_HAS_LOCATION (stmt
))
1137 location_t loc
= EXPR_LOCATION (stmt
);
1138 warning ("%Hwill never be executed", &loc
);
1142 switch (TREE_CODE (stmt
))
1144 case STATEMENT_LIST
:
1146 tree_stmt_iterator i
;
1147 for (i
= tsi_start (stmt
); !tsi_end_p (i
); tsi_next (&i
))
1148 if (remove_useless_stmts_warn_notreached (tsi_stmt (i
)))
1154 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt
)))
1156 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt
)))
1158 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt
)))
1162 case TRY_FINALLY_EXPR
:
1163 case TRY_CATCH_EXPR
:
1164 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt
, 0)))
1166 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt
, 1)))
1171 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt
));
1172 case EH_FILTER_EXPR
:
1173 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt
));
1175 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt
));
1178 /* Not a live container. */
1186 remove_useless_stmts_cond (tree
*stmt_p
, struct rus_data
*data
)
1188 tree then_clause
, else_clause
, cond
;
1189 bool save_has_label
, then_has_label
, else_has_label
;
1191 save_has_label
= data
->has_label
;
1192 data
->has_label
= false;
1193 data
->last_goto
= NULL
;
1195 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p
), data
);
1197 then_has_label
= data
->has_label
;
1198 data
->has_label
= false;
1199 data
->last_goto
= NULL
;
1201 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p
), data
);
1203 else_has_label
= data
->has_label
;
1204 data
->has_label
= save_has_label
| then_has_label
| else_has_label
;
1207 then_clause
= COND_EXPR_THEN (*stmt_p
);
1208 else_clause
= COND_EXPR_ELSE (*stmt_p
);
1209 cond
= COND_EXPR_COND (*stmt_p
);
1211 /* If neither arm does anything at all, we can remove the whole IF. */
1212 if (!TREE_SIDE_EFFECTS (then_clause
) && !TREE_SIDE_EFFECTS (else_clause
))
1214 *stmt_p
= build_empty_stmt ();
1215 data
->repeat
= true;
1218 /* If there are no reachable statements in an arm, then we can
1219 zap the entire conditional. */
1220 else if (integer_nonzerop (cond
) && !else_has_label
)
1222 if (warn_notreached
)
1223 remove_useless_stmts_warn_notreached (else_clause
);
1224 *stmt_p
= then_clause
;
1225 data
->repeat
= true;
1227 else if (integer_zerop (cond
) && !then_has_label
)
1229 if (warn_notreached
)
1230 remove_useless_stmts_warn_notreached (then_clause
);
1231 *stmt_p
= else_clause
;
1232 data
->repeat
= true;
1235 /* Check a couple of simple things on then/else with single stmts. */
1238 tree then_stmt
= expr_only (then_clause
);
1239 tree else_stmt
= expr_only (else_clause
);
1241 /* Notice branches to a common destination. */
1242 if (then_stmt
&& else_stmt
1243 && TREE_CODE (then_stmt
) == GOTO_EXPR
1244 && TREE_CODE (else_stmt
) == GOTO_EXPR
1245 && (GOTO_DESTINATION (then_stmt
) == GOTO_DESTINATION (else_stmt
)))
1247 *stmt_p
= then_stmt
;
1248 data
->repeat
= true;
1251 /* If the THEN/ELSE clause merely assigns a value to a variable or
1252 parameter which is already known to contain that value, then
1253 remove the useless THEN/ELSE clause. */
1254 else if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
1257 && TREE_CODE (else_stmt
) == MODIFY_EXPR
1258 && TREE_OPERAND (else_stmt
, 0) == cond
1259 && integer_zerop (TREE_OPERAND (else_stmt
, 1)))
1260 COND_EXPR_ELSE (*stmt_p
) = alloc_stmt_list ();
1262 else if ((TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
1263 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1264 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
1265 && TREE_CONSTANT (TREE_OPERAND (cond
, 1)))
1267 tree stmt
= (TREE_CODE (cond
) == EQ_EXPR
1268 ? then_stmt
: else_stmt
);
1269 tree
*location
= (TREE_CODE (cond
) == EQ_EXPR
1270 ? &COND_EXPR_THEN (*stmt_p
)
1271 : &COND_EXPR_ELSE (*stmt_p
));
1274 && TREE_CODE (stmt
) == MODIFY_EXPR
1275 && TREE_OPERAND (stmt
, 0) == TREE_OPERAND (cond
, 0)
1276 && TREE_OPERAND (stmt
, 1) == TREE_OPERAND (cond
, 1))
1277 *location
= alloc_stmt_list ();
1281 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1282 would be re-introduced during lowering. */
1283 data
->last_goto
= NULL
;
1288 remove_useless_stmts_tf (tree
*stmt_p
, struct rus_data
*data
)
1290 bool save_may_branch
, save_may_throw
;
1291 bool this_may_branch
, this_may_throw
;
1293 /* Collect may_branch and may_throw information for the body only. */
1294 save_may_branch
= data
->may_branch
;
1295 save_may_throw
= data
->may_throw
;
1296 data
->may_branch
= false;
1297 data
->may_throw
= false;
1298 data
->last_goto
= NULL
;
1300 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
1302 this_may_branch
= data
->may_branch
;
1303 this_may_throw
= data
->may_throw
;
1304 data
->may_branch
|= save_may_branch
;
1305 data
->may_throw
|= save_may_throw
;
1306 data
->last_goto
= NULL
;
1308 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
1310 /* If the body is empty, then we can emit the FINALLY block without
1311 the enclosing TRY_FINALLY_EXPR. */
1312 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 0)))
1314 *stmt_p
= TREE_OPERAND (*stmt_p
, 1);
1315 data
->repeat
= true;
1318 /* If the handler is empty, then we can emit the TRY block without
1319 the enclosing TRY_FINALLY_EXPR. */
1320 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 1)))
1322 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1323 data
->repeat
= true;
1326 /* If the body neither throws, nor branches, then we can safely
1327 string the TRY and FINALLY blocks together. */
1328 else if (!this_may_branch
&& !this_may_throw
)
1330 tree stmt
= *stmt_p
;
1331 *stmt_p
= TREE_OPERAND (stmt
, 0);
1332 append_to_statement_list (TREE_OPERAND (stmt
, 1), stmt_p
);
1333 data
->repeat
= true;
1339 remove_useless_stmts_tc (tree
*stmt_p
, struct rus_data
*data
)
1341 bool save_may_throw
, this_may_throw
;
1342 tree_stmt_iterator i
;
1345 /* Collect may_throw information for the body only. */
1346 save_may_throw
= data
->may_throw
;
1347 data
->may_throw
= false;
1348 data
->last_goto
= NULL
;
1350 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
1352 this_may_throw
= data
->may_throw
;
1353 data
->may_throw
= save_may_throw
;
1355 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1356 if (!this_may_throw
)
1358 if (warn_notreached
)
1359 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p
, 1));
1360 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1361 data
->repeat
= true;
1365 /* Process the catch clause specially. We may be able to tell that
1366 no exceptions propagate past this point. */
1368 this_may_throw
= true;
1369 i
= tsi_start (TREE_OPERAND (*stmt_p
, 1));
1370 stmt
= tsi_stmt (i
);
1371 data
->last_goto
= NULL
;
1373 switch (TREE_CODE (stmt
))
1376 for (; !tsi_end_p (i
); tsi_next (&i
))
1378 stmt
= tsi_stmt (i
);
1379 /* If we catch all exceptions, then the body does not
1380 propagate exceptions past this point. */
1381 if (CATCH_TYPES (stmt
) == NULL
)
1382 this_may_throw
= false;
1383 data
->last_goto
= NULL
;
1384 remove_useless_stmts_1 (&CATCH_BODY (stmt
), data
);
1388 case EH_FILTER_EXPR
:
1389 if (EH_FILTER_MUST_NOT_THROW (stmt
))
1390 this_may_throw
= false;
1391 else if (EH_FILTER_TYPES (stmt
) == NULL
)
1392 this_may_throw
= false;
1393 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt
), data
);
1397 /* Otherwise this is a cleanup. */
1398 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
1400 /* If the cleanup is empty, then we can emit the TRY block without
1401 the enclosing TRY_CATCH_EXPR. */
1402 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 1)))
1404 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1405 data
->repeat
= true;
1409 data
->may_throw
|= this_may_throw
;
1414 remove_useless_stmts_bind (tree
*stmt_p
, struct rus_data
*data
)
1418 /* First remove anything underneath the BIND_EXPR. */
1419 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p
), data
);
1421 /* If the BIND_EXPR has no variables, then we can pull everything
1422 up one level and remove the BIND_EXPR, unless this is the toplevel
1423 BIND_EXPR for the current function or an inlined function.
1425 When this situation occurs we will want to apply this
1426 optimization again. */
1427 block
= BIND_EXPR_BLOCK (*stmt_p
);
1428 if (BIND_EXPR_VARS (*stmt_p
) == NULL_TREE
1429 && *stmt_p
!= DECL_SAVED_TREE (current_function_decl
)
1431 || ! BLOCK_ABSTRACT_ORIGIN (block
)
1432 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
))
1435 *stmt_p
= BIND_EXPR_BODY (*stmt_p
);
1436 data
->repeat
= true;
1442 remove_useless_stmts_goto (tree
*stmt_p
, struct rus_data
*data
)
1444 tree dest
= GOTO_DESTINATION (*stmt_p
);
1446 data
->may_branch
= true;
1447 data
->last_goto
= NULL
;
1449 /* Record the last goto expr, so that we can delete it if unnecessary. */
1450 if (TREE_CODE (dest
) == LABEL_DECL
)
1451 data
->last_goto
= stmt_p
;
1456 remove_useless_stmts_label (tree
*stmt_p
, struct rus_data
*data
)
1458 tree label
= LABEL_EXPR_LABEL (*stmt_p
);
1460 data
->has_label
= true;
1462 /* We do want to jump across non-local label receiver code. */
1463 if (DECL_NONLOCAL (label
))
1464 data
->last_goto
= NULL
;
1466 else if (data
->last_goto
&& GOTO_DESTINATION (*data
->last_goto
) == label
)
1468 *data
->last_goto
= build_empty_stmt ();
1469 data
->repeat
= true;
1472 /* ??? Add something here to delete unused labels. */
1476 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1477 decl. This allows us to eliminate redundant or useless
1478 calls to "const" functions.
1480 Gimplifier already does the same operation, but we may notice functions
1481 being const and pure once their calls has been gimplified, so we need
1482 to update the flag. */
1485 update_call_expr_flags (tree call
)
1487 tree decl
= get_callee_fndecl (call
);
1490 if (call_expr_flags (call
) & (ECF_CONST
| ECF_PURE
))
1491 TREE_SIDE_EFFECTS (call
) = 0;
1492 if (TREE_NOTHROW (decl
))
1493 TREE_NOTHROW (call
) = 1;
1497 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1500 notice_special_calls (tree t
)
1502 int flags
= call_expr_flags (t
);
1504 if (flags
& ECF_MAY_BE_ALLOCA
)
1505 current_function_calls_alloca
= true;
1506 if (flags
& ECF_RETURNS_TWICE
)
1507 current_function_calls_setjmp
= true;
1511 /* Clear flags set by notice_special_calls. Used by dead code removal
1512 to update the flags. */
1515 clear_special_calls (void)
1517 current_function_calls_alloca
= false;
1518 current_function_calls_setjmp
= false;
1523 remove_useless_stmts_1 (tree
*tp
, struct rus_data
*data
)
1527 switch (TREE_CODE (t
))
1530 remove_useless_stmts_cond (tp
, data
);
1533 case TRY_FINALLY_EXPR
:
1534 remove_useless_stmts_tf (tp
, data
);
1537 case TRY_CATCH_EXPR
:
1538 remove_useless_stmts_tc (tp
, data
);
1542 remove_useless_stmts_bind (tp
, data
);
1546 remove_useless_stmts_goto (tp
, data
);
1550 remove_useless_stmts_label (tp
, data
);
1555 data
->last_goto
= NULL
;
1556 data
->may_branch
= true;
1561 data
->last_goto
= NULL
;
1562 notice_special_calls (t
);
1563 update_call_expr_flags (t
);
1564 if (tree_could_throw_p (t
))
1565 data
->may_throw
= true;
1569 data
->last_goto
= NULL
;
1571 op
= get_call_expr_in (t
);
1574 update_call_expr_flags (op
);
1575 notice_special_calls (op
);
1577 if (tree_could_throw_p (t
))
1578 data
->may_throw
= true;
1581 case STATEMENT_LIST
:
1583 tree_stmt_iterator i
= tsi_start (t
);
1584 while (!tsi_end_p (i
))
1587 if (IS_EMPTY_STMT (t
))
1593 remove_useless_stmts_1 (tsi_stmt_ptr (i
), data
);
1596 if (TREE_CODE (t
) == STATEMENT_LIST
)
1598 tsi_link_before (&i
, t
, TSI_SAME_STMT
);
1608 data
->last_goto
= NULL
;
1612 data
->last_goto
= NULL
;
1618 remove_useless_stmts (void)
1620 struct rus_data data
;
1622 clear_special_calls ();
1626 memset (&data
, 0, sizeof (data
));
1627 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl
), &data
);
1629 while (data
.repeat
);
1633 struct tree_opt_pass pass_remove_useless_stmts
=
1635 "useless", /* name */
1637 remove_useless_stmts
, /* execute */
1640 0, /* static_pass_number */
1642 PROP_gimple_any
, /* properties_required */
1643 0, /* properties_provided */
1644 0, /* properties_destroyed */
1645 0, /* todo_flags_start */
1646 TODO_dump_func
/* todo_flags_finish */
1650 /* Remove obviously useless statements in basic block BB. */
1653 cfg_remove_useless_stmts_bb (basic_block bb
)
1655 block_stmt_iterator bsi
;
1656 tree stmt
= NULL_TREE
;
1657 tree cond
, var
= NULL_TREE
, val
= NULL_TREE
;
1658 struct var_ann_d
*ann
;
1660 /* Check whether we come here from a condition, and if so, get the
1663 || bb
->pred
->pred_next
1664 || !(bb
->pred
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
1667 cond
= COND_EXPR_COND (last_stmt (bb
->pred
->src
));
1669 if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
1672 val
= (bb
->pred
->flags
& EDGE_FALSE_VALUE
1673 ? boolean_false_node
: boolean_true_node
);
1675 else if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
1676 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1677 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
))
1679 var
= TREE_OPERAND (cond
, 0);
1680 val
= (bb
->pred
->flags
& EDGE_FALSE_VALUE
1681 ? boolean_true_node
: boolean_false_node
);
1685 if (bb
->pred
->flags
& EDGE_FALSE_VALUE
)
1686 cond
= invert_truthvalue (cond
);
1687 if (TREE_CODE (cond
) == EQ_EXPR
1688 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1689 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
1690 && (TREE_CODE (TREE_OPERAND (cond
, 1)) == VAR_DECL
1691 || TREE_CODE (TREE_OPERAND (cond
, 1)) == PARM_DECL
1692 || TREE_CONSTANT (TREE_OPERAND (cond
, 1))))
1694 var
= TREE_OPERAND (cond
, 0);
1695 val
= TREE_OPERAND (cond
, 1);
1701 /* Only work for normal local variables. */
1702 ann
= var_ann (var
);
1705 || TREE_ADDRESSABLE (var
))
1708 if (! TREE_CONSTANT (val
))
1710 ann
= var_ann (val
);
1713 || TREE_ADDRESSABLE (val
))
1717 /* Ignore floating point variables, since comparison behaves weird for
1719 if (FLOAT_TYPE_P (TREE_TYPE (var
)))
1722 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
);)
1724 stmt
= bsi_stmt (bsi
);
1726 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1727 which is already known to contain that value, then remove the useless
1728 THEN/ELSE clause. */
1729 if (TREE_CODE (stmt
) == MODIFY_EXPR
1730 && TREE_OPERAND (stmt
, 0) == var
1731 && operand_equal_p (val
, TREE_OPERAND (stmt
, 1), 0))
1737 /* Invalidate the var if we encounter something that could modify it. */
1738 if (TREE_CODE (stmt
) == ASM_EXPR
1739 || (TREE_CODE (stmt
) == MODIFY_EXPR
1740 && TREE_OPERAND (stmt
, 0) == var
))
1748 /* A CFG-aware version of remove_useless_stmts. */
1751 cfg_remove_useless_stmts (void)
1755 #ifdef ENABLE_CHECKING
1756 verify_flow_info ();
1761 cfg_remove_useless_stmts_bb (bb
);
1766 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1769 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb
)
1773 /* Since this block is no longer reachable, we can just delete all
1774 of its PHI nodes. */
1775 phi
= phi_nodes (bb
);
1778 tree next
= PHI_CHAIN (phi
);
1779 remove_phi_node (phi
, NULL_TREE
, bb
);
1783 /* Remove edges to BB's successors. */
1784 while (bb
->succ
!= NULL
)
1785 ssa_remove_edge (bb
->succ
);
1789 /* Remove statements of basic block BB. */
1792 remove_bb (basic_block bb
)
1794 block_stmt_iterator i
;
1795 source_locus loc
= 0;
1799 fprintf (dump_file
, "Removing basic block %d\n", bb
->index
);
1800 if (dump_flags
& TDF_DETAILS
)
1802 dump_bb (bb
, dump_file
, 0);
1803 fprintf (dump_file
, "\n");
1807 /* Remove all the instructions in the block. */
1808 for (i
= bsi_start (bb
); !bsi_end_p (i
); bsi_remove (&i
))
1810 tree stmt
= bsi_stmt (i
);
1812 set_bb_for_stmt (stmt
, NULL
);
1814 /* Don't warn for removed gotos. Gotos are often removed due to
1815 jump threading, thus resulting in bogus warnings. Not great,
1816 since this way we lose warnings for gotos in the original
1817 program that are indeed unreachable. */
1818 if (TREE_CODE (stmt
) != GOTO_EXPR
&& EXPR_HAS_LOCATION (stmt
) && !loc
)
1819 #ifdef USE_MAPPED_LOCATION
1820 loc
= EXPR_LOCATION (stmt
);
1822 loc
= EXPR_LOCUS (stmt
);
1826 /* If requested, give a warning that the first statement in the
1827 block is unreachable. We walk statements backwards in the
1828 loop above, so the last statement we process is the first statement
1830 if (warn_notreached
&& loc
)
1831 #ifdef USE_MAPPED_LOCATION
1832 warning ("%Hwill never be executed", &loc
);
1834 warning ("%Hwill never be executed", loc
);
1837 remove_phi_nodes_and_edges_for_unreachable_block (bb
);
1841 /* Examine BB to determine if it is a forwarding block (a block which only
1842 transfers control to a new destination). If BB is a forwarding block,
1843 then return the edge leading to the ultimate destination. */
1846 tree_block_forwards_to (basic_block bb
)
1848 block_stmt_iterator bsi
;
1849 bb_ann_t ann
= bb_ann (bb
);
1852 /* If this block is not forwardable, then avoid useless work. */
1853 if (! ann
->forwardable
)
1856 /* Set this block to not be forwardable. This prevents infinite loops since
1857 any block currently under examination is considered non-forwardable. */
1858 ann
->forwardable
= 0;
1860 /* No forwarding is possible if this block is a special block (ENTRY/EXIT),
1861 this block has more than one successor, this block's single successor is
1862 reached via an abnormal edge, this block has phi nodes, or this block's
1863 single successor has phi nodes. */
1864 if (bb
== EXIT_BLOCK_PTR
1865 || bb
== ENTRY_BLOCK_PTR
1867 || bb
->succ
->succ_next
1868 || bb
->succ
->dest
== EXIT_BLOCK_PTR
1869 || (bb
->succ
->flags
& EDGE_ABNORMAL
) != 0
1871 || phi_nodes (bb
->succ
->dest
))
1874 /* Walk past any labels at the start of this block. */
1875 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1877 stmt
= bsi_stmt (bsi
);
1878 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1882 /* If we reached the end of this block we may be able to optimize this
1884 if (bsi_end_p (bsi
))
1888 /* Recursive call to pick up chains of forwarding blocks. */
1889 dest
= tree_block_forwards_to (bb
->succ
->dest
);
1891 /* If none found, we forward to bb->succ at minimum. */
1895 ann
->forwardable
= 1;
1899 /* No forwarding possible. */
1904 /* Try to remove superfluous control structures. */
1907 cleanup_control_flow (void)
1910 block_stmt_iterator bsi
;
1911 bool retval
= false;
1916 bsi
= bsi_last (bb
);
1918 if (bsi_end_p (bsi
))
1921 stmt
= bsi_stmt (bsi
);
1922 if (TREE_CODE (stmt
) == COND_EXPR
1923 || TREE_CODE (stmt
) == SWITCH_EXPR
)
1924 retval
|= cleanup_control_expr_graph (bb
, bsi
);
1930 /* Disconnect an unreachable block in the control expression starting
1934 cleanup_control_expr_graph (basic_block bb
, block_stmt_iterator bsi
)
1937 bool retval
= false;
1938 tree expr
= bsi_stmt (bsi
), val
;
1940 if (bb
->succ
->succ_next
)
1944 switch (TREE_CODE (expr
))
1947 val
= COND_EXPR_COND (expr
);
1951 val
= SWITCH_COND (expr
);
1952 if (TREE_CODE (val
) != INTEGER_CST
)
1960 taken_edge
= find_taken_edge (bb
, val
);
1964 /* Remove all the edges except the one that is always executed. */
1965 for (e
= bb
->succ
; e
; e
= next
)
1967 next
= e
->succ_next
;
1968 if (e
!= taken_edge
)
1970 taken_edge
->probability
+= e
->probability
;
1971 taken_edge
->count
+= e
->count
;
1972 ssa_remove_edge (e
);
1976 if (taken_edge
->probability
> REG_BR_PROB_BASE
)
1977 taken_edge
->probability
= REG_BR_PROB_BASE
;
1980 taken_edge
= bb
->succ
;
1983 taken_edge
->flags
= EDGE_FALLTHRU
;
1985 /* We removed some paths from the cfg. */
1986 if (dom_computed
[CDI_DOMINATORS
] >= DOM_CONS_OK
)
1987 dom_computed
[CDI_DOMINATORS
] = DOM_CONS_OK
;
1993 /* Given a control block BB and a predicate VAL, return the edge that
1994 will be taken out of the block. If VAL does not match a unique
1995 edge, NULL is returned. */
1998 find_taken_edge (basic_block bb
, tree val
)
2002 stmt
= last_stmt (bb
);
2004 #if defined ENABLE_CHECKING
2005 if (stmt
== NULL_TREE
|| !is_ctrl_stmt (stmt
))
2009 /* If VAL is a predicate of the form N RELOP N, where N is an
2010 SSA_NAME, we can always determine its truth value (except when
2011 doing floating point comparisons that may involve NaNs). */
2013 && TREE_CODE_CLASS (TREE_CODE (val
)) == '<'
2014 && TREE_OPERAND (val
, 0) == TREE_OPERAND (val
, 1)
2015 && TREE_CODE (TREE_OPERAND (val
, 0)) == SSA_NAME
2016 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (val
, 0))) != REAL_TYPE
2017 || !HONOR_NANS (TYPE_MODE (TREE_TYPE (TREE_OPERAND (val
, 0))))))
2019 enum tree_code code
= TREE_CODE (val
);
2021 if (code
== EQ_EXPR
|| code
== LE_EXPR
|| code
== GE_EXPR
)
2022 val
= boolean_true_node
;
2023 else if (code
== LT_EXPR
|| code
== GT_EXPR
|| code
== NE_EXPR
)
2024 val
= boolean_false_node
;
2027 /* If VAL is not a constant, we can't determine which edge might
2029 if (val
== NULL
|| !really_constant_p (val
))
2032 if (TREE_CODE (stmt
) == COND_EXPR
)
2033 return find_taken_edge_cond_expr (bb
, val
);
2035 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
2036 return find_taken_edge_switch_expr (bb
, val
);
2042 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2043 statement, determine which of the two edges will be taken out of the
2044 block. Return NULL if either edge may be taken. */
2047 find_taken_edge_cond_expr (basic_block bb
, tree val
)
2049 edge true_edge
, false_edge
;
2051 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
2053 /* If both edges of the branch lead to the same basic block, it doesn't
2054 matter which edge is taken. */
2055 if (true_edge
->dest
== false_edge
->dest
)
2058 /* Otherwise, try to determine which branch of the if() will be taken.
2059 If VAL is a constant but it can't be reduced to a 0 or a 1, then
2060 we don't really know which edge will be taken at runtime. This
2061 may happen when comparing addresses (e.g., if (&var1 == 4)). */
2062 if (integer_nonzerop (val
))
2064 else if (integer_zerop (val
))
2071 /* Given a constant value VAL and the entry block BB to a SWITCH_EXPR
2072 statement, determine which edge will be taken out of the block. Return
2073 NULL if any edge may be taken. */
2076 find_taken_edge_switch_expr (basic_block bb
, tree val
)
2078 tree switch_expr
, taken_case
;
2079 basic_block dest_bb
;
2082 if (TREE_CODE (val
) != INTEGER_CST
)
2085 switch_expr
= last_stmt (bb
);
2086 taken_case
= find_case_label_for_value (switch_expr
, val
);
2087 dest_bb
= label_to_block (CASE_LABEL (taken_case
));
2089 e
= find_edge (bb
, dest_bb
);
2096 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2097 We can make optimal use here of the fact that the case labels are
2098 sorted: We can do a binary search for a case matching VAL. */
2101 find_case_label_for_value (tree switch_expr
, tree val
)
2103 tree vec
= SWITCH_LABELS (switch_expr
);
2104 size_t low
, high
, n
= TREE_VEC_LENGTH (vec
);
2105 tree default_case
= TREE_VEC_ELT (vec
, n
- 1);
2107 for (low
= -1, high
= n
- 1; high
- low
> 1; )
2109 size_t i
= (high
+ low
) / 2;
2110 tree t
= TREE_VEC_ELT (vec
, i
);
2113 /* Cache the result of comparing CASE_LOW and val. */
2114 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
2121 if (CASE_HIGH (t
) == NULL
)
2123 /* A singe-valued case label. */
2129 /* A case range. We can only handle integer ranges. */
2130 if (cmp
<= 0 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
2135 return default_case
;
2139 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2140 those alternatives are equal in each of the PHI nodes, then return
2141 true, else return false. */
2144 phi_alternatives_equal (basic_block dest
, edge e1
, edge e2
)
2146 tree phi
, val1
, val2
;
2149 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
2151 n1
= phi_arg_from_edge (phi
, e1
);
2152 n2
= phi_arg_from_edge (phi
, e2
);
2154 #ifdef ENABLE_CHECKING
2155 if (n1
< 0 || n2
< 0)
2159 val1
= PHI_ARG_DEF (phi
, n1
);
2160 val2
= PHI_ARG_DEF (phi
, n2
);
2162 if (!operand_equal_p (val1
, val2
, 0))
2170 /* Computing the Dominance Frontier:
2172 As described in Morgan, section 3.5, this may be done simply by
2173 walking the dominator tree bottom-up, computing the frontier for
2174 the children before the parent. When considering a block B,
2175 there are two cases:
2177 (1) A flow graph edge leaving B that does not lead to a child
2178 of B in the dominator tree must be a block that is either equal
2179 to B or not dominated by B. Such blocks belong in the frontier
2182 (2) Consider a block X in the frontier of one of the children C
2183 of B. If X is not equal to B and is not dominated by B, it
2184 is in the frontier of B. */
2187 compute_dominance_frontiers_1 (bitmap
*frontiers
, basic_block bb
, sbitmap done
)
2192 SET_BIT (done
, bb
->index
);
2194 /* Do the frontier of the children first. Not all children in the
2195 dominator tree (blocks dominated by this one) are children in the
2196 CFG, so check all blocks. */
2197 for (c
= first_dom_son (CDI_DOMINATORS
, bb
);
2199 c
= next_dom_son (CDI_DOMINATORS
, c
))
2201 if (! TEST_BIT (done
, c
->index
))
2202 compute_dominance_frontiers_1 (frontiers
, c
, done
);
2205 /* Find blocks conforming to rule (1) above. */
2206 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2208 if (e
->dest
== EXIT_BLOCK_PTR
)
2210 if (get_immediate_dominator (CDI_DOMINATORS
, e
->dest
) != bb
)
2211 bitmap_set_bit (frontiers
[bb
->index
], e
->dest
->index
);
2214 /* Find blocks conforming to rule (2). */
2215 for (c
= first_dom_son (CDI_DOMINATORS
, bb
);
2217 c
= next_dom_son (CDI_DOMINATORS
, c
))
2221 EXECUTE_IF_SET_IN_BITMAP (frontiers
[c
->index
], 0, x
,
2223 if (get_immediate_dominator (CDI_DOMINATORS
, BASIC_BLOCK (x
)) != bb
)
2224 bitmap_set_bit (frontiers
[bb
->index
], x
);
2231 compute_dominance_frontiers (bitmap
*frontiers
)
2233 sbitmap done
= sbitmap_alloc (last_basic_block
);
2235 timevar_push (TV_DOM_FRONTIERS
);
2237 sbitmap_zero (done
);
2239 compute_dominance_frontiers_1 (frontiers
, ENTRY_BLOCK_PTR
->succ
->dest
, done
);
2241 sbitmap_free (done
);
2243 timevar_pop (TV_DOM_FRONTIERS
);
2248 /*---------------------------------------------------------------------------
2250 ---------------------------------------------------------------------------*/
2252 /* Dump tree-specific information of block BB to file OUTF. */
2255 tree_dump_bb (basic_block bb
, FILE *outf
, int indent
)
2257 dump_generic_bb (outf
, bb
, indent
, TDF_VOPS
);
2261 /* Dump a basic block on stderr. */
2264 debug_tree_bb (basic_block bb
)
2266 dump_bb (bb
, stderr
, 0);
2270 /* Dump basic block with index N on stderr. */
2273 debug_tree_bb_n (int n
)
2275 debug_tree_bb (BASIC_BLOCK (n
));
2276 return BASIC_BLOCK (n
);
2280 /* Dump the CFG on stderr.
2282 FLAGS are the same used by the tree dumping functions
2283 (see TDF_* in tree.h). */
2286 debug_tree_cfg (int flags
)
2288 dump_tree_cfg (stderr
, flags
);
2292 /* Dump the program showing basic block boundaries on the given FILE.
2294 FLAGS are the same used by the tree dumping functions (see TDF_* in
2298 dump_tree_cfg (FILE *file
, int flags
)
2300 if (flags
& TDF_DETAILS
)
2302 const char *funcname
2303 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2306 fprintf (file
, ";; Function %s\n\n", funcname
);
2307 fprintf (file
, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2308 n_basic_blocks
, n_edges
, last_basic_block
);
2310 brief_dump_cfg (file
);
2311 fprintf (file
, "\n");
2314 if (flags
& TDF_STATS
)
2315 dump_cfg_stats (file
);
2317 dump_function_to_file (current_function_decl
, file
, flags
| TDF_BLOCKS
);
2321 /* Dump CFG statistics on FILE. */
2324 dump_cfg_stats (FILE *file
)
2326 static long max_num_merged_labels
= 0;
2327 unsigned long size
, total
= 0;
2330 const char * const fmt_str
= "%-30s%-13s%12s\n";
2331 const char * const fmt_str_1
= "%-30s%13d%11lu%c\n";
2332 const char * const fmt_str_3
= "%-43s%11lu%c\n";
2333 const char *funcname
2334 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2337 fprintf (file
, "\nCFG Statistics for %s\n\n", funcname
);
2339 fprintf (file
, "---------------------------------------------------------\n");
2340 fprintf (file
, fmt_str
, "", " Number of ", "Memory");
2341 fprintf (file
, fmt_str
, "", " instances ", "used ");
2342 fprintf (file
, "---------------------------------------------------------\n");
2344 size
= n_basic_blocks
* sizeof (struct basic_block_def
);
2346 fprintf (file
, fmt_str_1
, "Basic blocks", n_basic_blocks
,
2347 SCALE (size
), LABEL (size
));
2353 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2356 size
= n_edges
* sizeof (struct edge_def
);
2358 fprintf (file
, fmt_str_1
, "Edges", n_edges
, SCALE (size
), LABEL (size
));
2360 size
= n_basic_blocks
* sizeof (struct bb_ann_d
);
2362 fprintf (file
, fmt_str_1
, "Basic block annotations", n_basic_blocks
,
2363 SCALE (size
), LABEL (size
));
2365 fprintf (file
, "---------------------------------------------------------\n");
2366 fprintf (file
, fmt_str_3
, "Total memory used by CFG data", SCALE (total
),
2368 fprintf (file
, "---------------------------------------------------------\n");
2369 fprintf (file
, "\n");
2371 if (cfg_stats
.num_merged_labels
> max_num_merged_labels
)
2372 max_num_merged_labels
= cfg_stats
.num_merged_labels
;
2374 fprintf (file
, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2375 cfg_stats
.num_merged_labels
, max_num_merged_labels
);
2377 fprintf (file
, "\n");
2381 /* Dump CFG statistics on stderr. Keep extern so that it's always
2382 linked in the final executable. */
2385 debug_cfg_stats (void)
2387 dump_cfg_stats (stderr
);
2391 /* Dump the flowgraph to a .vcg FILE. */
2394 tree_cfg2vcg (FILE *file
)
2398 const char *funcname
2399 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2401 /* Write the file header. */
2402 fprintf (file
, "graph: { title: \"%s\"\n", funcname
);
2403 fprintf (file
, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2404 fprintf (file
, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2406 /* Write blocks and edges. */
2407 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
2409 fprintf (file
, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2412 if (e
->flags
& EDGE_FAKE
)
2413 fprintf (file
, " linestyle: dotted priority: 10");
2415 fprintf (file
, " linestyle: solid priority: 100");
2417 fprintf (file
, " }\n");
2423 enum tree_code head_code
, end_code
;
2424 const char *head_name
, *end_name
;
2427 tree first
= first_stmt (bb
);
2428 tree last
= last_stmt (bb
);
2432 head_code
= TREE_CODE (first
);
2433 head_name
= tree_code_name
[head_code
];
2434 head_line
= get_lineno (first
);
2437 head_name
= "no-statement";
2441 end_code
= TREE_CODE (last
);
2442 end_name
= tree_code_name
[end_code
];
2443 end_line
= get_lineno (last
);
2446 end_name
= "no-statement";
2448 fprintf (file
, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2449 bb
->index
, bb
->index
, head_name
, head_line
, end_name
,
2452 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2454 if (e
->dest
== EXIT_BLOCK_PTR
)
2455 fprintf (file
, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb
->index
);
2457 fprintf (file
, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb
->index
, e
->dest
->index
);
2459 if (e
->flags
& EDGE_FAKE
)
2460 fprintf (file
, " priority: 10 linestyle: dotted");
2462 fprintf (file
, " priority: 100 linestyle: solid");
2464 fprintf (file
, " }\n");
2467 if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
2471 fputs ("}\n\n", file
);
2476 /*---------------------------------------------------------------------------
2477 Miscellaneous helpers
2478 ---------------------------------------------------------------------------*/
2480 /* Return true if T represents a stmt that always transfers control. */
2483 is_ctrl_stmt (tree t
)
2485 return (TREE_CODE (t
) == COND_EXPR
2486 || TREE_CODE (t
) == SWITCH_EXPR
2487 || TREE_CODE (t
) == GOTO_EXPR
2488 || TREE_CODE (t
) == RETURN_EXPR
2489 || TREE_CODE (t
) == RESX_EXPR
);
2493 /* Return true if T is a statement that may alter the flow of control
2494 (e.g., a call to a non-returning function). */
2497 is_ctrl_altering_stmt (tree t
)
2501 #if defined ENABLE_CHECKING
2506 call
= get_call_expr_in (t
);
2509 /* A non-pure/const CALL_EXPR alters flow control if the current
2510 function has nonlocal labels. */
2511 if (TREE_SIDE_EFFECTS (call
) && current_function_has_nonlocal_label
)
2514 /* A CALL_EXPR also alters control flow if it does not return. */
2515 if (call_expr_flags (call
) & (ECF_NORETURN
| ECF_LONGJMP
))
2519 /* If a statement can throw, it alters control flow. */
2520 return tree_can_throw_internal (t
);
2524 /* Return true if T is a computed goto. */
2527 computed_goto_p (tree t
)
2529 return (TREE_CODE (t
) == GOTO_EXPR
2530 && TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
);
2534 /* Checks whether EXPR is a simple local goto. */
2537 simple_goto_p (tree expr
)
2539 return (TREE_CODE (expr
) == GOTO_EXPR
2540 && TREE_CODE (GOTO_DESTINATION (expr
)) == LABEL_DECL
);
2544 /* Return true if T should start a new basic block. PREV_T is the
2545 statement preceding T. It is used when T is a label or a case label.
2546 Labels should only start a new basic block if their previous statement
2547 wasn't a label. Otherwise, sequence of labels would generate
2548 unnecessary basic blocks that only contain a single label. */
2551 stmt_starts_bb_p (tree t
, tree prev_t
)
2553 enum tree_code code
;
2558 /* LABEL_EXPRs start a new basic block only if the preceding
2559 statement wasn't a label of the same type. This prevents the
2560 creation of consecutive blocks that have nothing but a single
2562 code
= TREE_CODE (t
);
2563 if (code
== LABEL_EXPR
)
2565 /* Nonlocal and computed GOTO targets always start a new block. */
2566 if (code
== LABEL_EXPR
2567 && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t
))
2568 || FORCED_LABEL (LABEL_EXPR_LABEL (t
))))
2571 if (prev_t
&& TREE_CODE (prev_t
) == code
)
2573 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t
)))
2576 cfg_stats
.num_merged_labels
++;
2587 /* Return true if T should end a basic block. */
2590 stmt_ends_bb_p (tree t
)
2592 return is_ctrl_stmt (t
) || is_ctrl_altering_stmt (t
);
2596 /* Add gotos that used to be represented implicitly in the CFG. */
2599 disband_implicit_edges (void)
2602 block_stmt_iterator last
;
2608 last
= bsi_last (bb
);
2609 stmt
= last_stmt (bb
);
2611 if (stmt
&& TREE_CODE (stmt
) == COND_EXPR
)
2613 /* Remove superfluous gotos from COND_EXPR branches. Moved
2614 from cfg_remove_useless_stmts here since it violates the
2615 invariants for tree--cfg correspondence and thus fits better
2616 here where we do it anyway. */
2617 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2619 if (e
->dest
!= bb
->next_bb
)
2622 if (e
->flags
& EDGE_TRUE_VALUE
)
2623 COND_EXPR_THEN (stmt
) = build_empty_stmt ();
2624 else if (e
->flags
& EDGE_FALSE_VALUE
)
2625 COND_EXPR_ELSE (stmt
) = build_empty_stmt ();
2628 e
->flags
|= EDGE_FALLTHRU
;
2634 if (stmt
&& TREE_CODE (stmt
) == RETURN_EXPR
)
2636 /* Remove the RETURN_EXPR if we may fall though to the exit
2639 || bb
->succ
->succ_next
2640 || bb
->succ
->dest
!= EXIT_BLOCK_PTR
)
2643 if (bb
->next_bb
== EXIT_BLOCK_PTR
2644 && !TREE_OPERAND (stmt
, 0))
2647 bb
->succ
->flags
|= EDGE_FALLTHRU
;
2652 /* There can be no fallthru edge if the last statement is a control
2654 if (stmt
&& is_ctrl_stmt (stmt
))
2657 /* Find a fallthru edge and emit the goto if necessary. */
2658 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2659 if (e
->flags
& EDGE_FALLTHRU
)
2662 if (!e
|| e
->dest
== bb
->next_bb
)
2665 if (e
->dest
== EXIT_BLOCK_PTR
)
2668 label
= tree_block_label (e
->dest
);
2670 stmt
= build1 (GOTO_EXPR
, void_type_node
, label
);
2671 #ifdef USE_MAPPED_LOCATION
2672 SET_EXPR_LOCATION (stmt
, e
->goto_locus
);
2674 SET_EXPR_LOCUS (stmt
, e
->goto_locus
);
2676 bsi_insert_after (&last
, stmt
, BSI_NEW_STMT
);
2677 e
->flags
&= ~EDGE_FALLTHRU
;
2681 /* Remove block annotations and other datastructures. */
2684 delete_tree_cfg_annotations (void)
2687 if (n_basic_blocks
> 0)
2688 free_blocks_annotations ();
2690 label_to_block_map
= NULL
;
2697 /* Return the first statement in basic block BB. */
2700 first_stmt (basic_block bb
)
2702 block_stmt_iterator i
= bsi_start (bb
);
2703 return !bsi_end_p (i
) ? bsi_stmt (i
) : NULL_TREE
;
2707 /* Return the last statement in basic block BB. */
2710 last_stmt (basic_block bb
)
2712 block_stmt_iterator b
= bsi_last (bb
);
2713 return !bsi_end_p (b
) ? bsi_stmt (b
) : NULL_TREE
;
2717 /* Return a pointer to the last statement in block BB. */
2720 last_stmt_ptr (basic_block bb
)
2722 block_stmt_iterator last
= bsi_last (bb
);
2723 return !bsi_end_p (last
) ? bsi_stmt_ptr (last
) : NULL
;
2727 /* Return the last statement of an otherwise empty block. Return NULL
2728 if the block is totally empty, or if it contains more than one
2732 last_and_only_stmt (basic_block bb
)
2734 block_stmt_iterator i
= bsi_last (bb
);
2740 last
= bsi_stmt (i
);
2745 /* Empty statements should no longer appear in the instruction stream.
2746 Everything that might have appeared before should be deleted by
2747 remove_useless_stmts, and the optimizers should just bsi_remove
2748 instead of smashing with build_empty_stmt.
2750 Thus the only thing that should appear here in a block containing
2751 one executable statement is a label. */
2752 prev
= bsi_stmt (i
);
2753 if (TREE_CODE (prev
) == LABEL_EXPR
)
2760 /* Mark BB as the basic block holding statement T. */
2763 set_bb_for_stmt (tree t
, basic_block bb
)
2765 if (TREE_CODE (t
) == STATEMENT_LIST
)
2767 tree_stmt_iterator i
;
2768 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2769 set_bb_for_stmt (tsi_stmt (i
), bb
);
2773 stmt_ann_t ann
= get_stmt_ann (t
);
2776 /* If the statement is a label, add the label to block-to-labels map
2777 so that we can speed up edge creation for GOTO_EXPRs. */
2778 if (TREE_CODE (t
) == LABEL_EXPR
)
2782 t
= LABEL_EXPR_LABEL (t
);
2783 uid
= LABEL_DECL_UID (t
);
2786 LABEL_DECL_UID (t
) = uid
= cfun
->last_label_uid
++;
2787 if (VARRAY_SIZE (label_to_block_map
) <= (unsigned) uid
)
2788 VARRAY_GROW (label_to_block_map
, 3 * uid
/ 2);
2792 #ifdef ENABLE_CHECKING
2793 /* We're moving an existing label. Make sure that we've
2794 removed it from the old block. */
2795 if (bb
&& VARRAY_BB (label_to_block_map
, uid
))
2799 VARRAY_BB (label_to_block_map
, uid
) = bb
;
2805 /* Insert statement (or statement list) T before the statement
2806 pointed-to by iterator I. M specifies how to update iterator I
2807 after insertion (see enum bsi_iterator_update). */
2810 bsi_insert_before (block_stmt_iterator
*i
, tree t
, enum bsi_iterator_update m
)
2812 set_bb_for_stmt (t
, i
->bb
);
2814 tsi_link_before (&i
->tsi
, t
, m
);
2818 /* Insert statement (or statement list) T after the statement
2819 pointed-to by iterator I. M specifies how to update iterator I
2820 after insertion (see enum bsi_iterator_update). */
2823 bsi_insert_after (block_stmt_iterator
*i
, tree t
, enum bsi_iterator_update m
)
2825 set_bb_for_stmt (t
, i
->bb
);
2827 tsi_link_after (&i
->tsi
, t
, m
);
2831 /* Remove the statement pointed to by iterator I. The iterator is updated
2832 to the next statement. */
2835 bsi_remove (block_stmt_iterator
*i
)
2837 tree t
= bsi_stmt (*i
);
2838 set_bb_for_stmt (t
, NULL
);
2840 tsi_delink (&i
->tsi
);
2844 /* Move the statement at FROM so it comes right after the statement at TO. */
2847 bsi_move_after (block_stmt_iterator
*from
, block_stmt_iterator
*to
)
2849 tree stmt
= bsi_stmt (*from
);
2851 bsi_insert_after (to
, stmt
, BSI_SAME_STMT
);
2855 /* Move the statement at FROM so it comes right before the statement at TO. */
2858 bsi_move_before (block_stmt_iterator
*from
, block_stmt_iterator
*to
)
2860 tree stmt
= bsi_stmt (*from
);
2862 bsi_insert_before (to
, stmt
, BSI_SAME_STMT
);
2866 /* Move the statement at FROM to the end of basic block BB. */
2869 bsi_move_to_bb_end (block_stmt_iterator
*from
, basic_block bb
)
2871 block_stmt_iterator last
= bsi_last (bb
);
2873 /* Have to check bsi_end_p because it could be an empty block. */
2874 if (!bsi_end_p (last
) && is_ctrl_stmt (bsi_stmt (last
)))
2875 bsi_move_before (from
, &last
);
2877 bsi_move_after (from
, &last
);
2881 /* Replace the contents of the statement pointed to by iterator BSI
2882 with STMT. If PRESERVE_EH_INFO is true, the exception handling
2883 information of the original statement is preserved. */
2886 bsi_replace (const block_stmt_iterator
*bsi
, tree stmt
, bool preserve_eh_info
)
2889 tree orig_stmt
= bsi_stmt (*bsi
);
2891 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (orig_stmt
));
2892 set_bb_for_stmt (stmt
, bsi
->bb
);
2894 /* Preserve EH region information from the original statement, if
2895 requested by the caller. */
2896 if (preserve_eh_info
)
2898 eh_region
= lookup_stmt_eh_region (orig_stmt
);
2900 add_stmt_to_eh_region (stmt
, eh_region
);
2903 *bsi_stmt_ptr (*bsi
) = stmt
;
2908 /* Insert the statement pointed-to by BSI into edge E. Every attempt
2909 is made to place the statement in an existing basic block, but
2910 sometimes that isn't possible. When it isn't possible, the edge is
2911 split and the statement is added to the new block.
2913 In all cases, the returned *BSI points to the correct location. The
2914 return value is true if insertion should be done after the location,
2915 or false if it should be done before the location. */
2918 tree_find_edge_insert_loc (edge e
, block_stmt_iterator
*bsi
)
2920 basic_block dest
, src
;
2926 /* If the destination has one predecessor which has no PHI nodes,
2927 insert there. Except for the exit block.
2929 The requirement for no PHI nodes could be relaxed. Basically we
2930 would have to examine the PHIs to prove that none of them used
2931 the value set by the statement we want to insert on E. That
2932 hardly seems worth the effort. */
2933 if (dest
->pred
->pred_next
== NULL
2934 && ! phi_nodes (dest
)
2935 && dest
!= EXIT_BLOCK_PTR
)
2937 *bsi
= bsi_start (dest
);
2938 if (bsi_end_p (*bsi
))
2941 /* Make sure we insert after any leading labels. */
2942 tmp
= bsi_stmt (*bsi
);
2943 while (TREE_CODE (tmp
) == LABEL_EXPR
)
2946 if (bsi_end_p (*bsi
))
2948 tmp
= bsi_stmt (*bsi
);
2951 if (bsi_end_p (*bsi
))
2953 *bsi
= bsi_last (dest
);
2960 /* If the source has one successor, the edge is not abnormal and
2961 the last statement does not end a basic block, insert there.
2962 Except for the entry block. */
2964 if ((e
->flags
& EDGE_ABNORMAL
) == 0
2965 && src
->succ
->succ_next
== NULL
2966 && src
!= ENTRY_BLOCK_PTR
)
2968 *bsi
= bsi_last (src
);
2969 if (bsi_end_p (*bsi
))
2972 tmp
= bsi_stmt (*bsi
);
2973 if (!stmt_ends_bb_p (tmp
))
2976 /* Insert code just before returning the value. We may need to decompose
2977 the return in the case it contains non-trivial operand. */
2978 if (TREE_CODE (tmp
) == RETURN_EXPR
)
2980 tree op
= TREE_OPERAND (tmp
, 0);
2981 if (!is_gimple_val (op
))
2983 if (TREE_CODE (op
) != MODIFY_EXPR
)
2985 bsi_insert_before (bsi
, op
, BSI_NEW_STMT
);
2986 TREE_OPERAND (tmp
, 0) = TREE_OPERAND (op
, 0);
2993 /* Otherwise, create a new basic block, and split this edge. */
2994 dest
= split_edge (e
);
3000 /* This routine will commit all pending edge insertions, creating any new
3001 basic blocks which are necessary.
3003 If specified, NEW_BLOCKS returns a count of the number of new basic
3004 blocks which were created. */
3007 bsi_commit_edge_inserts (int *new_blocks
)
3013 blocks
= n_basic_blocks
;
3015 bsi_commit_edge_inserts_1 (ENTRY_BLOCK_PTR
->succ
);
3018 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3019 bsi_commit_edge_inserts_1 (e
);
3022 *new_blocks
= n_basic_blocks
- blocks
;
3026 /* Commit insertions pending at edge E. */
3029 bsi_commit_edge_inserts_1 (edge e
)
3031 if (PENDING_STMT (e
))
3033 block_stmt_iterator bsi
;
3034 tree stmt
= PENDING_STMT (e
);
3036 PENDING_STMT (e
) = NULL_TREE
;
3038 if (tree_find_edge_insert_loc (e
, &bsi
))
3039 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3041 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3046 /* Add STMT to the pending list of edge E. No actual insertion is
3047 made until a call to bsi_commit_edge_inserts () is made. */
3050 bsi_insert_on_edge (edge e
, tree stmt
)
3052 append_to_statement_list (stmt
, &PENDING_STMT (e
));
3056 /*---------------------------------------------------------------------------
3057 Tree specific functions for CFG manipulation
3058 ---------------------------------------------------------------------------*/
3060 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3061 Abort on abnormal edges. */
3064 tree_split_edge (edge edge_in
)
3066 basic_block new_bb
, after_bb
, dest
, src
;
3071 /* Abnormal edges cannot be split. */
3072 if (edge_in
->flags
& EDGE_ABNORMAL
)
3076 dest
= edge_in
->dest
;
3078 /* Place the new block in the block list. Try to keep the new block
3079 near its "logical" location. This is of most help to humans looking
3080 at debugging dumps. */
3081 for (e
= dest
->pred
; e
; e
= e
->pred_next
)
3082 if (e
->src
->next_bb
== dest
)
3085 after_bb
= dest
->prev_bb
;
3087 after_bb
= edge_in
->src
;
3089 new_bb
= create_empty_bb (after_bb
);
3090 new_edge
= make_edge (new_bb
, dest
, EDGE_FALLTHRU
);
3092 /* Find all the PHI arguments on the original edge, and change them to
3093 the new edge. Do it before redirection, so that the argument does not
3095 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
3097 num_elem
= PHI_NUM_ARGS (phi
);
3098 for (i
= 0; i
< num_elem
; i
++)
3099 if (PHI_ARG_EDGE (phi
, i
) == edge_in
)
3101 PHI_ARG_EDGE (phi
, i
) = new_edge
;
3106 if (!redirect_edge_and_branch (edge_in
, new_bb
))
3109 if (PENDING_STMT (edge_in
))
3116 /* Return true when BB has label LABEL in it. */
3119 has_label_p (basic_block bb
, tree label
)
3121 block_stmt_iterator bsi
;
3123 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3125 tree stmt
= bsi_stmt (bsi
);
3127 if (TREE_CODE (stmt
) != LABEL_EXPR
)
3129 if (LABEL_EXPR_LABEL (stmt
) == label
)
3136 /* Callback for walk_tree, check that all elements with address taken are
3137 properly noticed as such. */
3140 verify_expr (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
3147 /* Check operand N for being valid GIMPLE and give error MSG if not.
3148 We check for constants explicitly since they are not considered
3149 gimple invariants if they overflowed. */
3150 #define CHECK_OP(N, MSG) \
3151 do { if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, N))) != 'c' \
3152 && !is_gimple_val (TREE_OPERAND (t, N))) \
3153 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3155 switch (TREE_CODE (t
))
3158 if (SSA_NAME_IN_FREE_LIST (t
))
3160 error ("SSA name in freelist but still referenced");
3166 x
= TREE_OPERAND (t
, 0);
3167 if (TREE_CODE (x
) == BIT_FIELD_REF
3168 && is_gimple_reg (TREE_OPERAND (x
, 0)))
3170 error ("GIMPLE register modified with BIT_FIELD_REF");
3176 /* Skip any references (they will be checked when we recurse down the
3177 tree) and ensure that any variable used as a prefix is marked
3179 for (x
= TREE_OPERAND (t
, 0);
3180 (handled_component_p (x
)
3181 || TREE_CODE (x
) == REALPART_EXPR
3182 || TREE_CODE (x
) == IMAGPART_EXPR
);
3183 x
= TREE_OPERAND (x
, 0))
3186 if (TREE_CODE (x
) != VAR_DECL
&& TREE_CODE (x
) != PARM_DECL
)
3188 if (!TREE_ADDRESSABLE (x
))
3190 error ("address taken, but ADDRESSABLE bit not set");
3196 x
= TREE_OPERAND (t
, 0);
3197 if (TREE_CODE (TREE_TYPE (x
)) != BOOLEAN_TYPE
)
3199 error ("non-boolean used in condition");
3206 case FIX_TRUNC_EXPR
:
3208 case FIX_FLOOR_EXPR
:
3209 case FIX_ROUND_EXPR
:
3214 case NON_LVALUE_EXPR
:
3215 case TRUTH_NOT_EXPR
:
3216 CHECK_OP (0, "Invalid operand to unary operator");
3223 case ARRAY_RANGE_REF
:
3225 case VIEW_CONVERT_EXPR
:
3226 /* We have a nest of references. Verify that each of the operands
3227 that determine where to reference is either a constant or a variable,
3228 verify that the base is valid, and then show we've already checked
3230 while (TREE_CODE (t
) == REALPART_EXPR
|| TREE_CODE (t
) == IMAGPART_EXPR
3231 || handled_component_p (t
))
3233 if (TREE_CODE (t
) == COMPONENT_REF
&& TREE_OPERAND (t
, 2))
3234 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3235 else if (TREE_CODE (t
) == ARRAY_REF
3236 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
3238 CHECK_OP (1, "Invalid array index.");
3239 if (TREE_OPERAND (t
, 2))
3240 CHECK_OP (2, "Invalid array lower bound.");
3241 if (TREE_OPERAND (t
, 3))
3242 CHECK_OP (3, "Invalid array stride.");
3244 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
3246 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3247 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3250 t
= TREE_OPERAND (t
, 0);
3253 if (TREE_CODE_CLASS (TREE_CODE (t
)) != 'c'
3254 && !is_gimple_lvalue (t
))
3256 error ("Invalid reference prefix.");
3268 case UNORDERED_EXPR
:
3279 case TRUNC_DIV_EXPR
:
3281 case FLOOR_DIV_EXPR
:
3282 case ROUND_DIV_EXPR
:
3283 case TRUNC_MOD_EXPR
:
3285 case FLOOR_MOD_EXPR
:
3286 case ROUND_MOD_EXPR
:
3288 case EXACT_DIV_EXPR
:
3298 CHECK_OP (0, "Invalid operand to binary operator");
3299 CHECK_OP (1, "Invalid operand to binary operator");
3311 /* Verify STMT, return true if STMT is not in GIMPLE form.
3312 TODO: Implement type checking. */
3315 verify_stmt (tree stmt
, bool last_in_block
)
3319 if (!is_gimple_stmt (stmt
))
3321 error ("Is not a valid GIMPLE statement.");
3325 addr
= walk_tree (&stmt
, verify_expr
, NULL
, NULL
);
3328 debug_generic_stmt (addr
);
3332 /* If the statement is marked as part of an EH region, then it is
3333 expected that the statement could throw. Verify that when we
3334 have optimizations that simplify statements such that we prove
3335 that they cannot throw, that we update other data structures
3337 if (lookup_stmt_eh_region (stmt
) >= 0)
3339 if (!tree_could_throw_p (stmt
))
3341 error ("Statement marked for throw, but doesn't.");
3344 if (!last_in_block
&& tree_can_throw_internal (stmt
))
3346 error ("Statement marked for throw in middle of block.");
3354 debug_generic_stmt (stmt
);
3359 /* Return true when the T can be shared. */
3362 tree_node_can_be_shared (tree t
)
3364 if (TYPE_P (t
) || DECL_P (t
)
3365 /* We check for constants explicitly since they are not considered
3366 gimple invariants if they overflowed. */
3367 || TREE_CODE_CLASS (TREE_CODE (t
)) == 'c'
3368 || is_gimple_min_invariant (t
)
3369 || TREE_CODE (t
) == SSA_NAME
)
3372 while (((TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
3373 /* We check for constants explicitly since they are not considered
3374 gimple invariants if they overflowed. */
3375 && (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t
, 1))) == 'c'
3376 || is_gimple_min_invariant (TREE_OPERAND (t
, 1))))
3377 || (TREE_CODE (t
) == COMPONENT_REF
3378 || TREE_CODE (t
) == REALPART_EXPR
3379 || TREE_CODE (t
) == IMAGPART_EXPR
))
3380 t
= TREE_OPERAND (t
, 0);
3389 /* Called via walk_trees. Verify tree sharing. */
3392 verify_node_sharing (tree
* tp
, int *walk_subtrees
, void *data
)
3394 htab_t htab
= (htab_t
) data
;
3397 if (tree_node_can_be_shared (*tp
))
3399 *walk_subtrees
= false;
3403 slot
= htab_find_slot (htab
, *tp
, INSERT
);
3412 /* Verify the GIMPLE statement chain. */
3418 block_stmt_iterator bsi
;
3423 timevar_push (TV_TREE_STMT_VERIFY
);
3424 htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
3431 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
3433 int phi_num_args
= PHI_NUM_ARGS (phi
);
3435 for (i
= 0; i
< phi_num_args
; i
++)
3437 tree t
= PHI_ARG_DEF (phi
, i
);
3440 /* Addressable variables do have SSA_NAMEs but they
3441 are not considered gimple values. */
3442 if (TREE_CODE (t
) != SSA_NAME
3443 && TREE_CODE (t
) != FUNCTION_DECL
3444 && !is_gimple_val (t
))
3446 error ("PHI def is not a GIMPLE value");
3447 debug_generic_stmt (phi
);
3448 debug_generic_stmt (t
);
3452 addr
= walk_tree (&t
, verify_expr
, NULL
, NULL
);
3455 debug_generic_stmt (addr
);
3459 addr
= walk_tree (&t
, verify_node_sharing
, htab
, NULL
);
3462 error ("Incorrect sharing of tree nodes");
3463 debug_generic_stmt (phi
);
3464 debug_generic_stmt (addr
);
3470 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
3472 tree stmt
= bsi_stmt (bsi
);
3474 err
|= verify_stmt (stmt
, bsi_end_p (bsi
));
3475 addr
= walk_tree (&stmt
, verify_node_sharing
, htab
, NULL
);
3478 error ("Incorrect sharing of tree nodes");
3479 debug_generic_stmt (stmt
);
3480 debug_generic_stmt (addr
);
3487 internal_error ("verify_stmts failed.");
3490 timevar_pop (TV_TREE_STMT_VERIFY
);
3494 /* Verifies that the flow information is OK. */
3497 tree_verify_flow_info (void)
3501 block_stmt_iterator bsi
;
3505 if (ENTRY_BLOCK_PTR
->stmt_list
)
3507 error ("ENTRY_BLOCK has a statement list associated with it\n");
3511 if (EXIT_BLOCK_PTR
->stmt_list
)
3513 error ("EXIT_BLOCK has a statement list associated with it\n");
3517 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
3518 if (e
->flags
& EDGE_FALLTHRU
)
3520 error ("Fallthru to exit from bb %d\n", e
->src
->index
);
3526 bool found_ctrl_stmt
= false;
3528 /* Skip labels on the start of basic block. */
3529 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3531 if (TREE_CODE (bsi_stmt (bsi
)) != LABEL_EXPR
)
3534 if (label_to_block (LABEL_EXPR_LABEL (bsi_stmt (bsi
))) != bb
)
3536 error ("Label %s to block does not match in bb %d\n",
3537 IDENTIFIER_POINTER (DECL_NAME (bsi_stmt (bsi
))),
3542 if (decl_function_context (LABEL_EXPR_LABEL (bsi_stmt (bsi
)))
3543 != current_function_decl
)
3545 error ("Label %s has incorrect context in bb %d\n",
3546 IDENTIFIER_POINTER (DECL_NAME (bsi_stmt (bsi
))),
3552 /* Verify that body of basic block BB is free of control flow. */
3553 for (; !bsi_end_p (bsi
); bsi_next (&bsi
))
3555 tree stmt
= bsi_stmt (bsi
);
3557 if (found_ctrl_stmt
)
3559 error ("Control flow in the middle of basic block %d\n",
3564 if (stmt_ends_bb_p (stmt
))
3565 found_ctrl_stmt
= true;
3567 if (TREE_CODE (stmt
) == LABEL_EXPR
)
3569 error ("Label %s in the middle of basic block %d\n",
3570 IDENTIFIER_POINTER (DECL_NAME (stmt
)),
3575 bsi
= bsi_last (bb
);
3576 if (bsi_end_p (bsi
))
3579 stmt
= bsi_stmt (bsi
);
3581 if (is_ctrl_stmt (stmt
))
3583 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3584 if (e
->flags
& EDGE_FALLTHRU
)
3586 error ("Fallthru edge after a control statement in bb %d \n",
3592 switch (TREE_CODE (stmt
))
3598 if (TREE_CODE (COND_EXPR_THEN (stmt
)) != GOTO_EXPR
3599 || TREE_CODE (COND_EXPR_ELSE (stmt
)) != GOTO_EXPR
)
3601 error ("Structured COND_EXPR at the end of bb %d\n", bb
->index
);
3605 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
3607 if (!true_edge
|| !false_edge
3608 || !(true_edge
->flags
& EDGE_TRUE_VALUE
)
3609 || !(false_edge
->flags
& EDGE_FALSE_VALUE
)
3610 || (true_edge
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
))
3611 || (false_edge
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
))
3612 || bb
->succ
->succ_next
->succ_next
)
3614 error ("Wrong outgoing edge flags at end of bb %d\n",
3619 if (!has_label_p (true_edge
->dest
,
3620 GOTO_DESTINATION (COND_EXPR_THEN (stmt
))))
3622 error ("`then' label does not match edge at end of bb %d\n",
3627 if (!has_label_p (false_edge
->dest
,
3628 GOTO_DESTINATION (COND_EXPR_ELSE (stmt
))))
3630 error ("`else' label does not match edge at end of bb %d\n",
3638 if (simple_goto_p (stmt
))
3640 error ("Explicit goto at end of bb %d\n", bb
->index
);
3645 /* FIXME. We should double check that the labels in the
3646 destination blocks have their address taken. */
3647 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3648 if ((e
->flags
& (EDGE_FALLTHRU
| EDGE_TRUE_VALUE
3649 | EDGE_FALSE_VALUE
))
3650 || !(e
->flags
& EDGE_ABNORMAL
))
3652 error ("Wrong outgoing edge flags at end of bb %d\n",
3660 if (!bb
->succ
|| bb
->succ
->succ_next
3661 || (bb
->succ
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
3662 | EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
3664 error ("Wrong outgoing edge flags at end of bb %d\n", bb
->index
);
3667 if (bb
->succ
->dest
!= EXIT_BLOCK_PTR
)
3669 error ("Return edge does not point to exit in bb %d\n",
3682 vec
= SWITCH_LABELS (stmt
);
3683 n
= TREE_VEC_LENGTH (vec
);
3685 /* Mark all the destination basic blocks. */
3686 for (i
= 0; i
< n
; ++i
)
3688 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
3689 basic_block label_bb
= label_to_block (lab
);
3691 if (label_bb
->aux
&& label_bb
->aux
!= (void *)1)
3693 label_bb
->aux
= (void *)1;
3696 /* Verify that the case labels are sorted. */
3697 prev
= TREE_VEC_ELT (vec
, 0);
3698 for (i
= 1; i
< n
- 1; ++i
)
3700 tree c
= TREE_VEC_ELT (vec
, i
);
3703 error ("Found default case not at end of case vector");
3707 if (! tree_int_cst_lt (CASE_LOW (prev
), CASE_LOW (c
)))
3709 error ("Case labels not sorted:\n ");
3710 print_generic_expr (stderr
, prev
, 0);
3711 fprintf (stderr
," is greater than ");
3712 print_generic_expr (stderr
, c
, 0);
3713 fprintf (stderr
," but comes before it.\n");
3718 if (CASE_LOW (TREE_VEC_ELT (vec
, n
- 1)))
3720 error ("No default case found at end of case vector");
3724 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3728 error ("Extra outgoing edge %d->%d\n",
3729 bb
->index
, e
->dest
->index
);
3732 e
->dest
->aux
= (void *)2;
3733 if ((e
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
3734 | EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
3736 error ("Wrong outgoing edge flags at end of bb %d\n",
3742 /* Check that we have all of them. */
3743 for (i
= 0; i
< n
; ++i
)
3745 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
3746 basic_block label_bb
= label_to_block (lab
);
3748 if (label_bb
->aux
!= (void *)2)
3750 error ("Missing edge %i->%i\n",
3751 bb
->index
, label_bb
->index
);
3756 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3757 e
->dest
->aux
= (void *)0;
3764 if (dom_computed
[CDI_DOMINATORS
] >= DOM_NO_FAST_QUERY
)
3765 verify_dominators (CDI_DOMINATORS
);
3771 /* Updates phi nodes after creating forwarder block joined
3772 by edge FALLTHRU. */
3775 tree_make_forwarder_block (edge fallthru
)
3778 basic_block dummy
, bb
;
3779 tree phi
, new_phi
, var
, prev
, next
;
3781 dummy
= fallthru
->src
;
3782 bb
= fallthru
->dest
;
3784 if (!bb
->pred
->pred_next
)
3787 /* If we redirected a branch we must create new phi nodes at the
3789 for (phi
= phi_nodes (dummy
); phi
; phi
= PHI_CHAIN (phi
))
3791 var
= PHI_RESULT (phi
);
3792 new_phi
= create_phi_node (var
, bb
);
3793 SSA_NAME_DEF_STMT (var
) = new_phi
;
3794 SET_PHI_RESULT (phi
, make_ssa_name (SSA_NAME_VAR (var
), phi
));
3795 add_phi_arg (&new_phi
, PHI_RESULT (phi
), fallthru
);
3798 /* Ensure that the PHI node chain is in the same order. */
3800 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
3802 next
= PHI_CHAIN (phi
);
3803 PHI_CHAIN (phi
) = prev
;
3806 set_phi_nodes (bb
, prev
);
3808 /* Add the arguments we have stored on edges. */
3809 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
3814 for (phi
= phi_nodes (bb
), var
= PENDING_STMT (e
);
3816 phi
= PHI_CHAIN (phi
), var
= TREE_CHAIN (var
))
3817 add_phi_arg (&phi
, TREE_VALUE (var
), e
);
3819 PENDING_STMT (e
) = NULL
;
3824 /* Return true if basic block BB does nothing except pass control
3825 flow to another block and that we can safely insert a label at
3826 the start of the successor block. */
3829 tree_forwarder_block_p (basic_block bb
)
3831 block_stmt_iterator bsi
;
3834 /* If we have already determined that this block is not forwardable,
3835 then no further checks are necessary. */
3836 if (! bb_ann (bb
)->forwardable
)
3839 /* BB must have a single outgoing normal edge. Otherwise it can not be
3840 a forwarder block. */
3842 || bb
->succ
->succ_next
3843 || bb
->succ
->dest
== EXIT_BLOCK_PTR
3844 || (bb
->succ
->flags
& EDGE_ABNORMAL
)
3845 || bb
== ENTRY_BLOCK_PTR
)
3847 bb_ann (bb
)->forwardable
= 0;
3851 /* Successors of the entry block are not forwarders. */
3852 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
3855 bb_ann (bb
)->forwardable
= 0;
3859 /* BB can not have any PHI nodes. This could potentially be relaxed
3860 early in compilation if we re-rewrote the variables appearing in
3861 any PHI nodes in forwarder blocks. */
3864 bb_ann (bb
)->forwardable
= 0;
3868 /* Now walk through the statements. We can ignore labels, anything else
3869 means this is not a forwarder block. */
3870 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3872 tree stmt
= bsi_stmt (bsi
);
3874 switch (TREE_CODE (stmt
))
3877 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
3882 bb_ann (bb
)->forwardable
= 0;
3891 /* Thread jumps over empty statements.
3893 This code should _not_ thread over obviously equivalent conditions
3894 as that requires nontrivial updates to the SSA graph. */
3899 edge e
, next
, last
, old
;
3900 basic_block bb
, dest
, tmp
, old_dest
, dom
;
3903 bool retval
= false;
3906 bb_ann (bb
)->forwardable
= 1;
3908 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
3910 /* Don't waste time on unreachable blocks. */
3914 /* Nor on forwarders. */
3915 if (tree_forwarder_block_p (bb
))
3918 /* This block is now part of a forwarding path, mark it as not
3919 forwardable so that we can detect loops. This bit will be
3921 bb_ann (bb
)->forwardable
= 0;
3923 /* Examine each of our block's successors to see if it is
3925 for (e
= bb
->succ
; e
; e
= next
)
3927 next
= e
->succ_next
;
3929 /* If the edge is abnormal or its destination is not
3930 forwardable, then there's nothing to do. */
3931 if ((e
->flags
& EDGE_ABNORMAL
)
3932 || !tree_forwarder_block_p (e
->dest
))
3935 /* Now walk through as many forwarder block as possible to
3936 find the ultimate destination we want to thread our jump
3938 last
= e
->dest
->succ
;
3939 bb_ann (e
->dest
)->forwardable
= 0;
3940 for (dest
= e
->dest
->succ
->dest
;
3941 tree_forwarder_block_p (dest
);
3943 dest
= dest
->succ
->dest
)
3945 /* An infinite loop detected. We redirect the edge anyway, so
3946 that the loop is shrunk into single basic block. */
3947 if (!bb_ann (dest
)->forwardable
)
3950 if (dest
->succ
->dest
== EXIT_BLOCK_PTR
)
3953 bb_ann (dest
)->forwardable
= 0;
3956 /* Reset the forwardable marks to 1. */
3959 tmp
= tmp
->succ
->dest
)
3960 bb_ann (tmp
)->forwardable
= 1;
3962 if (dest
== e
->dest
)
3965 old
= find_edge (bb
, dest
);
3968 /* If there already is an edge, check whether the values
3969 in phi nodes differ. */
3970 if (!phi_alternatives_equal (dest
, last
, old
))
3972 /* The previous block is forwarder. Redirect our jump
3973 to that target instead since we know it has no PHI
3974 nodes that will need updating. */
3977 /* That might mean that no forwarding at all is possible. */
3978 if (dest
== e
->dest
)
3981 old
= find_edge (bb
, dest
);
3985 /* Perform the redirection. */
3988 e
= redirect_edge_and_branch (e
, dest
);
3992 /* Update PHI nodes. We know that the new argument should
3993 have the same value as the argument associated with LAST.
3994 Otherwise we would have changed our target block above. */
3995 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
3997 arg
= phi_arg_from_edge (phi
, last
);
4000 add_phi_arg (&phi
, PHI_ARG_DEF (phi
, arg
), e
);
4004 /* Update the dominators. */
4005 if (dom_computed
[CDI_DOMINATORS
] >= DOM_CONS_OK
)
4007 /* Remove the unreachable blocks (observe that if all blocks
4008 were reachable before, only those in the path we threaded
4009 over and did not have any predecessor outside of the path
4010 become unreachable). */
4011 for (; old_dest
!= dest
; old_dest
= tmp
)
4013 tmp
= old_dest
->succ
->dest
;
4018 delete_basic_block (old_dest
);
4020 /* If the dominator of the destination was in the path, set its
4021 dominator to the start of the redirected edge. */
4022 if (get_immediate_dominator (CDI_DOMINATORS
, old_dest
) == NULL
)
4023 set_immediate_dominator (CDI_DOMINATORS
, old_dest
, bb
);
4025 /* Now proceed like if we forwarded just over one edge at a time.
4026 Algorithm for forwarding over edge A --> B then is
4029 idom (B) = idom (A);
4030 recount_idom (A); */
4032 for (; old_dest
!= dest
; old_dest
= tmp
)
4034 tmp
= old_dest
->succ
->dest
;
4036 if (get_immediate_dominator (CDI_DOMINATORS
, tmp
) == old_dest
)
4038 dom
= get_immediate_dominator (CDI_DOMINATORS
, old_dest
);
4039 set_immediate_dominator (CDI_DOMINATORS
, tmp
, dom
);
4042 dom
= recount_dominator (CDI_DOMINATORS
, old_dest
);
4043 set_immediate_dominator (CDI_DOMINATORS
, old_dest
, dom
);
4048 /* Reset the forwardable bit on our block since it's no longer in
4049 a forwarding chain path. */
4050 bb_ann (bb
)->forwardable
= 1;
4057 /* Return a non-special label in the head of basic block BLOCK.
4058 Create one if it doesn't exist. */
4061 tree_block_label (basic_block bb
)
4063 block_stmt_iterator i
, s
= bsi_start (bb
);
4067 for (i
= s
; !bsi_end_p (i
); first
= false, bsi_next (&i
))
4069 stmt
= bsi_stmt (i
);
4070 if (TREE_CODE (stmt
) != LABEL_EXPR
)
4072 label
= LABEL_EXPR_LABEL (stmt
);
4073 if (!DECL_NONLOCAL (label
))
4076 bsi_move_before (&i
, &s
);
4081 label
= create_artificial_label ();
4082 stmt
= build1 (LABEL_EXPR
, void_type_node
, label
);
4083 bsi_insert_before (&s
, stmt
, BSI_NEW_STMT
);
4088 /* Attempt to perform edge redirection by replacing a possibly complex
4089 jump instruction by a goto or by removing the jump completely.
4090 This can apply only if all edges now point to the same block. The
4091 parameters and return values are equivalent to
4092 redirect_edge_and_branch. */
4095 tree_try_redirect_by_replacing_jump (edge e
, basic_block target
)
4097 basic_block src
= e
->src
;
4099 block_stmt_iterator b
;
4102 /* Verify that all targets will be TARGET. */
4103 for (tmp
= src
->succ
; tmp
; tmp
= tmp
->succ_next
)
4104 if (tmp
->dest
!= target
&& tmp
!= e
)
4113 stmt
= bsi_stmt (b
);
4115 if (TREE_CODE (stmt
) == COND_EXPR
4116 || TREE_CODE (stmt
) == SWITCH_EXPR
)
4119 e
= ssa_redirect_edge (e
, target
);
4120 e
->flags
= EDGE_FALLTHRU
;
4128 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4129 edge representing the redirected branch. */
4132 tree_redirect_edge_and_branch (edge e
, basic_block dest
)
4134 basic_block bb
= e
->src
;
4135 block_stmt_iterator bsi
;
4139 if (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
))
4142 if (e
->src
!= ENTRY_BLOCK_PTR
4143 && (ret
= tree_try_redirect_by_replacing_jump (e
, dest
)))
4146 if (e
->dest
== dest
)
4149 label
= tree_block_label (dest
);
4151 bsi
= bsi_last (bb
);
4152 stmt
= bsi_end_p (bsi
) ? NULL
: bsi_stmt (bsi
);
4154 switch (stmt
? TREE_CODE (stmt
) : ERROR_MARK
)
4157 stmt
= (e
->flags
& EDGE_TRUE_VALUE
4158 ? COND_EXPR_THEN (stmt
)
4159 : COND_EXPR_ELSE (stmt
));
4160 GOTO_DESTINATION (stmt
) = label
;
4164 /* No non-abnormal edges should lead from a non-simple goto, and
4165 simple ones should be represented implicitly. */
4170 tree vec
= SWITCH_LABELS (stmt
);
4171 size_t i
, n
= TREE_VEC_LENGTH (vec
);
4173 for (i
= 0; i
< n
; ++i
)
4175 tree elt
= TREE_VEC_ELT (vec
, i
);
4176 if (label_to_block (CASE_LABEL (elt
)) == e
->dest
)
4177 CASE_LABEL (elt
) = label
;
4184 e
->flags
|= EDGE_FALLTHRU
;
4188 /* Otherwise it must be a fallthru edge, and we don't need to
4189 do anything besides redirecting it. */
4190 if (!(e
->flags
& EDGE_FALLTHRU
))
4195 /* Update/insert PHI nodes as necessary. */
4197 /* Now update the edges in the CFG. */
4198 e
= ssa_redirect_edge (e
, dest
);
4204 /* Simple wrapper, as we can always redirect fallthru edges. */
4207 tree_redirect_edge_and_branch_force (edge e
, basic_block dest
)
4209 e
= tree_redirect_edge_and_branch (e
, dest
);
4217 /* Splits basic block BB after statement STMT (but at least after the
4218 labels). If STMT is NULL, BB is split just after the labels. */
4221 tree_split_block (basic_block bb
, void *stmt
)
4223 block_stmt_iterator bsi
, bsi_tgt
;
4228 new_bb
= create_empty_bb (bb
);
4230 /* Redirect the outgoing edges. */
4231 new_bb
->succ
= bb
->succ
;
4233 for (e
= new_bb
->succ
; e
; e
= e
->succ_next
)
4236 if (stmt
&& TREE_CODE ((tree
) stmt
) == LABEL_EXPR
)
4239 /* Move everything from BSI to the new basic block. */
4240 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4242 act
= bsi_stmt (bsi
);
4243 if (TREE_CODE (act
) == LABEL_EXPR
)
4256 bsi_tgt
= bsi_start (new_bb
);
4257 while (!bsi_end_p (bsi
))
4259 act
= bsi_stmt (bsi
);
4261 bsi_insert_after (&bsi_tgt
, act
, BSI_NEW_STMT
);
4268 /* Moves basic block BB after block AFTER. */
4271 tree_move_block_after (basic_block bb
, basic_block after
)
4273 if (bb
->prev_bb
== after
)
4277 link_block (bb
, after
);
4283 /* Return true if basic_block can be duplicated. */
4286 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED
)
4292 /* Create a duplicate of the basic block BB. NOTE: This does not
4293 preserve SSA form. */
4296 tree_duplicate_bb (basic_block bb
)
4299 block_stmt_iterator bsi
, bsi_tgt
;
4301 new_bb
= create_empty_bb (EXIT_BLOCK_PTR
->prev_bb
);
4302 bsi_tgt
= bsi_start (new_bb
);
4303 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4305 tree stmt
= bsi_stmt (bsi
);
4308 if (TREE_CODE (stmt
) == LABEL_EXPR
)
4311 copy
= unshare_expr (stmt
);
4313 /* Copy also the virtual operands. */
4314 get_stmt_ann (copy
);
4315 copy_virtual_operands (copy
, stmt
);
4317 bsi_insert_after (&bsi_tgt
, copy
, BSI_NEW_STMT
);
4324 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
4327 dump_function_to_file (tree fn
, FILE *file
, int flags
)
4329 tree arg
, vars
, var
;
4330 bool ignore_topmost_bind
= false, any_var
= false;
4334 fprintf (file
, "%s (", lang_hooks
.decl_printable_name (fn
, 2));
4336 arg
= DECL_ARGUMENTS (fn
);
4339 print_generic_expr (file
, arg
, dump_flags
);
4340 if (TREE_CHAIN (arg
))
4341 fprintf (file
, ", ");
4342 arg
= TREE_CHAIN (arg
);
4344 fprintf (file
, ")\n");
4346 if (flags
& TDF_RAW
)
4348 dump_node (fn
, TDF_SLIM
| flags
, file
);
4352 /* When GIMPLE is lowered, the variables are no longer available in
4353 BIND_EXPRs, so display them separately. */
4354 if (cfun
&& cfun
->unexpanded_var_list
)
4356 ignore_topmost_bind
= true;
4358 fprintf (file
, "{\n");
4359 for (vars
= cfun
->unexpanded_var_list
; vars
; vars
= TREE_CHAIN (vars
))
4361 var
= TREE_VALUE (vars
);
4363 print_generic_decl (file
, var
, flags
);
4364 fprintf (file
, "\n");
4370 if (basic_block_info
)
4372 /* Make a CFG based dump. */
4373 if (!ignore_topmost_bind
)
4374 fprintf (file
, "{\n");
4376 if (any_var
&& n_basic_blocks
)
4377 fprintf (file
, "\n");
4380 dump_generic_bb (file
, bb
, 2, flags
);
4382 fprintf (file
, "}\n");
4388 /* Make a tree based dump. */
4389 chain
= DECL_SAVED_TREE (fn
);
4391 if (TREE_CODE (chain
) == BIND_EXPR
)
4393 if (ignore_topmost_bind
)
4395 chain
= BIND_EXPR_BODY (chain
);
4403 if (!ignore_topmost_bind
)
4404 fprintf (file
, "{\n");
4409 fprintf (file
, "\n");
4411 print_generic_stmt_indented (file
, chain
, flags
, indent
);
4412 if (ignore_topmost_bind
)
4413 fprintf (file
, "}\n");
4416 fprintf (file
, "\n\n");
4420 /* Pretty print of the loops intermediate representation. */
4421 static void print_loop (FILE *, struct loop
*, int);
4422 static void print_pred_bbs (FILE *, edge
);
4423 static void print_succ_bbs (FILE *, edge
);
4426 /* Print the predecessors indexes of edge E on FILE. */
4429 print_pred_bbs (FILE *file
, edge e
)
4434 else if (e
->pred_next
== NULL
)
4435 fprintf (file
, "bb_%d", e
->src
->index
);
4439 fprintf (file
, "bb_%d, ", e
->src
->index
);
4440 print_pred_bbs (file
, e
->pred_next
);
4445 /* Print the successors indexes of edge E on FILE. */
4448 print_succ_bbs (FILE *file
, edge e
)
4452 else if (e
->succ_next
== NULL
)
4453 fprintf (file
, "bb_%d", e
->dest
->index
);
4456 fprintf (file
, "bb_%d, ", e
->dest
->index
);
4457 print_succ_bbs (file
, e
->succ_next
);
4462 /* Pretty print LOOP on FILE, indented INDENT spaces. */
4465 print_loop (FILE *file
, struct loop
*loop
, int indent
)
4473 s_indent
= (char *) alloca ((size_t) indent
+ 1);
4474 memset ((void *) s_indent
, ' ', (size_t) indent
);
4475 s_indent
[indent
] = '\0';
4477 /* Print the loop's header. */
4478 fprintf (file
, "%sloop_%d\n", s_indent
, loop
->num
);
4480 /* Print the loop's body. */
4481 fprintf (file
, "%s{\n", s_indent
);
4483 if (bb
->loop_father
== loop
)
4485 /* Print the basic_block's header. */
4486 fprintf (file
, "%s bb_%d (preds = {", s_indent
, bb
->index
);
4487 print_pred_bbs (file
, bb
->pred
);
4488 fprintf (file
, "}, succs = {");
4489 print_succ_bbs (file
, bb
->succ
);
4490 fprintf (file
, "})\n");
4492 /* Print the basic_block's body. */
4493 fprintf (file
, "%s {\n", s_indent
);
4494 tree_dump_bb (bb
, file
, indent
+ 4);
4495 fprintf (file
, "%s }\n", s_indent
);
4498 print_loop (file
, loop
->inner
, indent
+ 2);
4499 fprintf (file
, "%s}\n", s_indent
);
4500 print_loop (file
, loop
->next
, indent
);
4504 /* Follow a CFG edge from the entry point of the program, and on entry
4505 of a loop, pretty print the loop structure on FILE. */
4508 print_loop_ir (FILE *file
)
4512 bb
= BASIC_BLOCK (0);
4513 if (bb
&& bb
->loop_father
)
4514 print_loop (file
, bb
->loop_father
, 0);
4518 /* Debugging loops structure at tree level. */
4521 debug_loop_ir (void)
4523 print_loop_ir (stderr
);
4527 /* Return true if BB ends with a call, possibly followed by some
4528 instructions that must stay with the call. Return false,
4532 tree_block_ends_with_call_p (basic_block bb
)
4534 block_stmt_iterator bsi
= bsi_last (bb
);
4535 return get_call_expr_in (bsi_stmt (bsi
)) != NULL
;
4539 /* Return true if BB ends with a conditional branch. Return false,
4543 tree_block_ends_with_condjump_p (basic_block bb
)
4545 tree stmt
= tsi_stmt (bsi_last (bb
).tsi
);
4546 return (TREE_CODE (stmt
) == COND_EXPR
);
4550 /* Return true if we need to add fake edge to exit at statement T.
4551 Helper function for tree_flow_call_edges_add. */
4554 need_fake_edge_p (tree t
)
4558 /* NORETURN and LONGJMP calls already have an edge to exit.
4559 CONST, PURE and ALWAYS_RETURN calls do not need one.
4560 We don't currently check for CONST and PURE here, although
4561 it would be a good idea, because those attributes are
4562 figured out from the RTL in mark_constant_function, and
4563 the counter incrementation code from -fprofile-arcs
4564 leads to different results from -fbranch-probabilities. */
4565 call
= get_call_expr_in (t
);
4567 && !(call_expr_flags (call
) &
4568 (ECF_NORETURN
| ECF_LONGJMP
| ECF_ALWAYS_RETURN
)))
4571 if (TREE_CODE (t
) == ASM_EXPR
4572 && (ASM_VOLATILE_P (t
) || ASM_INPUT_P (t
)))
4579 /* Add fake edges to the function exit for any non constant and non
4580 noreturn calls, volatile inline assembly in the bitmap of blocks
4581 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
4582 the number of blocks that were split.
4584 The goal is to expose cases in which entering a basic block does
4585 not imply that all subsequent instructions must be executed. */
4588 tree_flow_call_edges_add (sbitmap blocks
)
4591 int blocks_split
= 0;
4592 int last_bb
= last_basic_block
;
4593 bool check_last_block
= false;
4595 if (n_basic_blocks
== 0)
4599 check_last_block
= true;
4601 check_last_block
= TEST_BIT (blocks
, EXIT_BLOCK_PTR
->prev_bb
->index
);
4603 /* In the last basic block, before epilogue generation, there will be
4604 a fallthru edge to EXIT. Special care is required if the last insn
4605 of the last basic block is a call because make_edge folds duplicate
4606 edges, which would result in the fallthru edge also being marked
4607 fake, which would result in the fallthru edge being removed by
4608 remove_fake_edges, which would result in an invalid CFG.
4610 Moreover, we can't elide the outgoing fake edge, since the block
4611 profiler needs to take this into account in order to solve the minimal
4612 spanning tree in the case that the call doesn't return.
4614 Handle this by adding a dummy instruction in a new last basic block. */
4615 if (check_last_block
)
4617 basic_block bb
= EXIT_BLOCK_PTR
->prev_bb
;
4618 block_stmt_iterator bsi
= bsi_last (bb
);
4620 if (!bsi_end_p (bsi
))
4623 if (need_fake_edge_p (t
))
4627 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
4628 if (e
->dest
== EXIT_BLOCK_PTR
)
4630 bsi_insert_on_edge (e
, build_empty_stmt ());
4631 bsi_commit_edge_inserts ((int *)NULL
);
4637 /* Now add fake edges to the function exit for any non constant
4638 calls since there is no way that we can determine if they will
4640 for (i
= 0; i
< last_bb
; i
++)
4642 basic_block bb
= BASIC_BLOCK (i
);
4643 block_stmt_iterator bsi
;
4644 tree stmt
, last_stmt
;
4649 if (blocks
&& !TEST_BIT (blocks
, i
))
4652 bsi
= bsi_last (bb
);
4653 if (!bsi_end_p (bsi
))
4655 last_stmt
= bsi_stmt (bsi
);
4658 stmt
= bsi_stmt (bsi
);
4659 if (need_fake_edge_p (stmt
))
4662 /* The handling above of the final block before the
4663 epilogue should be enough to verify that there is
4664 no edge to the exit block in CFG already.
4665 Calling make_edge in such case would cause us to
4666 mark that edge as fake and remove it later. */
4667 #ifdef ENABLE_CHECKING
4668 if (stmt
== last_stmt
)
4669 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
4670 if (e
->dest
== EXIT_BLOCK_PTR
)
4674 /* Note that the following may create a new basic block
4675 and renumber the existing basic blocks. */
4676 if (stmt
!= last_stmt
)
4678 e
= split_block (bb
, stmt
);
4682 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
4686 while (!bsi_end_p (bsi
));
4691 verify_flow_info ();
4693 return blocks_split
;
4697 tree_purge_dead_eh_edges (basic_block bb
)
4699 bool changed
= false;
4701 tree stmt
= last_stmt (bb
);
4703 if (stmt
&& tree_can_throw_internal (stmt
))
4706 for (e
= bb
->succ
; e
; e
= next
)
4708 next
= e
->succ_next
;
4709 if (e
->flags
& EDGE_EH
)
4711 ssa_remove_edge (e
);
4720 tree_purge_all_dead_eh_edges (bitmap blocks
)
4722 bool changed
= false;
4725 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, i
,
4726 { changed
|= tree_purge_dead_eh_edges (BASIC_BLOCK (i
)); });
4731 struct cfg_hooks tree_cfg_hooks
= {
4733 tree_verify_flow_info
,
4734 tree_dump_bb
, /* dump_bb */
4735 create_bb
, /* create_basic_block */
4736 tree_redirect_edge_and_branch
,/* redirect_edge_and_branch */
4737 tree_redirect_edge_and_branch_force
,/* redirect_edge_and_branch_force */
4738 remove_bb
, /* delete_basic_block */
4739 tree_split_block
, /* split_block */
4740 tree_move_block_after
, /* move_block_after */
4741 tree_can_merge_blocks_p
, /* can_merge_blocks_p */
4742 tree_merge_blocks
, /* merge_blocks */
4743 tree_predict_edge
, /* predict_edge */
4744 tree_predicted_by_p
, /* predicted_by_p */
4745 tree_can_duplicate_bb_p
, /* can_duplicate_block_p */
4746 tree_duplicate_bb
, /* duplicate_block */
4747 tree_split_edge
, /* split_edge */
4748 tree_make_forwarder_block
, /* make_forward_block */
4749 NULL
, /* tidy_fallthru_edge */
4750 tree_block_ends_with_call_p
, /* block_ends_with_call_p */
4751 tree_block_ends_with_condjump_p
, /* block_ends_with_condjump_p */
4752 tree_flow_call_edges_add
/* flow_call_edges_add */
4756 /* Split all critical edges. */
4759 split_critical_edges (void)
4766 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
4767 if (EDGE_CRITICAL_P (e
) && !(e
->flags
& EDGE_ABNORMAL
))
4774 struct tree_opt_pass pass_split_crit_edges
=
4776 "crited", /* name */
4778 split_critical_edges
, /* execute */
4781 0, /* static_pass_number */
4782 TV_TREE_SPLIT_EDGES
, /* tv_id */
4783 PROP_cfg
, /* properties required */
4784 PROP_no_crit_edges
, /* properties_provided */
4785 0, /* properties_destroyed */
4786 0, /* todo_flags_start */
4787 TODO_dump_func
, /* todo_flags_finish */
4791 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
4792 a temporary, make sure and register it to be renamed if necessary,
4793 and finally return the temporary. Put the statements to compute
4794 EXP before the current statement in BSI. */
4797 gimplify_val (block_stmt_iterator
*bsi
, tree type
, tree exp
)
4799 tree t
, new_stmt
, orig_stmt
;
4801 if (is_gimple_val (exp
))
4804 t
= make_rename_temp (type
, NULL
);
4805 new_stmt
= build (MODIFY_EXPR
, type
, t
, exp
);
4807 orig_stmt
= bsi_stmt (*bsi
);
4808 SET_EXPR_LOCUS (new_stmt
, EXPR_LOCUS (orig_stmt
));
4809 TREE_BLOCK (new_stmt
) = TREE_BLOCK (orig_stmt
);
4811 bsi_insert_before (bsi
, new_stmt
, BSI_SAME_STMT
);
4816 /* Build a ternary operation and gimplify it. Emit code before BSI.
4817 Return the gimple_val holding the result. */
4820 gimplify_build3 (block_stmt_iterator
*bsi
, enum tree_code code
,
4821 tree type
, tree a
, tree b
, tree c
)
4825 ret
= fold (build3 (code
, type
, a
, b
, c
));
4828 return gimplify_val (bsi
, type
, ret
);
4831 /* Build a binary operation and gimplify it. Emit code before BSI.
4832 Return the gimple_val holding the result. */
4835 gimplify_build2 (block_stmt_iterator
*bsi
, enum tree_code code
,
4836 tree type
, tree a
, tree b
)
4840 ret
= fold (build2 (code
, type
, a
, b
));
4843 return gimplify_val (bsi
, type
, ret
);
4846 /* Build a unary operation and gimplify it. Emit code before BSI.
4847 Return the gimple_val holding the result. */
4850 gimplify_build1 (block_stmt_iterator
*bsi
, enum tree_code code
, tree type
,
4855 ret
= fold (build1 (code
, type
, a
));
4858 return gimplify_val (bsi
, type
, ret
);
4863 /* Emit return warnings. */
4866 execute_warn_function_return (void)
4868 #ifdef USE_MAPPED_LOCATION
4869 source_location location
;
4876 if (warn_missing_noreturn
4877 && !TREE_THIS_VOLATILE (cfun
->decl
)
4878 && EXIT_BLOCK_PTR
->pred
== NULL
4879 && !lang_hooks
.function
.missing_noreturn_ok_p (cfun
->decl
))
4880 warning ("%Jfunction might be possible candidate for attribute `noreturn'",
4883 /* If we have a path to EXIT, then we do return. */
4884 if (TREE_THIS_VOLATILE (cfun
->decl
)
4885 && EXIT_BLOCK_PTR
->pred
!= NULL
)
4887 #ifdef USE_MAPPED_LOCATION
4888 location
= UNKNOWN_LOCATION
;
4892 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
4894 last
= last_stmt (e
->src
);
4895 if (TREE_CODE (last
) == RETURN_EXPR
4896 #ifdef USE_MAPPED_LOCATION
4897 && (location
= EXPR_LOCATION (last
)) != UNKNOWN_LOCATION
)
4899 && (locus
= EXPR_LOCUS (last
)) != NULL
)
4903 #ifdef USE_MAPPED_LOCATION
4904 if (location
== UNKNOWN_LOCATION
)
4905 location
= cfun
->function_end_locus
;
4906 warning ("%H`noreturn' function does return", &location
);
4909 locus
= &cfun
->function_end_locus
;
4910 warning ("%H`noreturn' function does return", locus
);
4914 /* If we see "return;" in some basic block, then we do reach the end
4915 without returning a value. */
4916 else if (warn_return_type
4917 && EXIT_BLOCK_PTR
->pred
!= NULL
4918 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun
->decl
))))
4920 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
4922 tree last
= last_stmt (e
->src
);
4923 if (TREE_CODE (last
) == RETURN_EXPR
4924 && TREE_OPERAND (last
, 0) == NULL
)
4926 #ifdef USE_MAPPED_LOCATION
4927 location
= EXPR_LOCATION (last
);
4928 if (location
== UNKNOWN_LOCATION
)
4929 location
= cfun
->function_end_locus
;
4930 warning ("%Hcontrol reaches end of non-void function", &location
);
4932 locus
= EXPR_LOCUS (last
);
4934 locus
= &cfun
->function_end_locus
;
4935 warning ("%Hcontrol reaches end of non-void function", locus
);
4944 /* Given a basic block B which ends with a conditional and has
4945 precisely two successors, determine which of the edges is taken if
4946 the conditional is true and which is taken if the conditional is
4947 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
4950 extract_true_false_edges_from_block (basic_block b
,
4956 if (e
->flags
& EDGE_TRUE_VALUE
)
4959 *false_edge
= e
->succ_next
;
4964 *true_edge
= e
->succ_next
;
4968 struct tree_opt_pass pass_warn_function_return
=
4972 execute_warn_function_return
, /* execute */
4975 0, /* static_pass_number */
4977 PROP_cfg
, /* properties_required */
4978 0, /* properties_provided */
4979 0, /* properties_destroyed */
4980 0, /* todo_flags_start */
4981 0 /* todo_flags_finish */
4984 #include "gt-tree-cfg.h"