1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
46 #include "cfglayout.h"
49 /* This file contains functions for building the Control Flow Graph (CFG)
50 for a function tree. */
52 /* Local declarations. */
54 /* Initial capacity for the basic block array. */
55 static const int initial_cfg_capacity
= 20;
57 /* Mapping of labels to their associated blocks. This can greatly speed up
58 building of the CFG in code with lots of gotos. */
59 static GTY(()) varray_type label_to_block_map
;
61 /* This hash table allows us to efficiently lookup the one and only one
62 CASE_LABEL_EXPR which contains the LABEL_DECL for the target block
63 of one or more case statements. Efficient access to this node
64 allows us to efficiently update the case vector in response to
65 edge redirections and similar operations.
67 Right now this is only used to set up case label leaders. In the
68 future we hope to make this table more persistent and use it to
69 more efficiently update case labels. */
71 struct edge_to_case_leader_elt
73 /* The edge itself. Necessary for hashing and equality tests. */
76 /* The "leader" for all the CASE_LABEL_EXPRs which transfer control
77 to E->dest. When we change the destination of E, we will need to
78 update the CASE_LEADER_OR_LABEL of this CASE_LABEL_EXPR (and no
83 static htab_t edge_to_case_leader
;
88 long num_merged_labels
;
91 static struct cfg_stats_d cfg_stats
;
93 /* Nonzero if we found a computed goto while building basic blocks. */
94 static bool found_computed_goto
;
96 /* Basic blocks and flowgraphs. */
97 static basic_block
create_bb (void *, void *, basic_block
);
98 static void create_block_annotation (basic_block
);
99 static void free_blocks_annotations (void);
100 static void clear_blocks_annotations (void);
101 static void make_blocks (tree
);
102 static void factor_computed_gotos (void);
105 static void make_edges (void);
106 static void make_ctrl_stmt_edges (basic_block
);
107 static void make_exit_edges (basic_block
);
108 static void make_cond_expr_edges (basic_block
);
109 static void make_switch_expr_edges (basic_block
);
110 static void make_goto_expr_edges (basic_block
);
111 static edge
tree_redirect_edge_and_branch (edge
, basic_block
);
112 static edge
tree_try_redirect_by_replacing_jump (edge
, basic_block
);
113 static void split_critical_edges (void);
115 /* Various helpers. */
116 static inline bool stmt_starts_bb_p (tree
, tree
);
117 static int tree_verify_flow_info (void);
118 static void tree_make_forwarder_block (edge
);
119 static bool thread_jumps (void);
120 static bool tree_forwarder_block_p (basic_block
);
121 static void tree_cfg2vcg (FILE *);
123 /* Flowgraph optimization and cleanup. */
124 static void tree_merge_blocks (basic_block
, basic_block
);
125 static bool tree_can_merge_blocks_p (basic_block
, basic_block
);
126 static void remove_bb (basic_block
);
127 static bool cleanup_control_flow (void);
128 static bool cleanup_control_expr_graph (basic_block
, block_stmt_iterator
);
129 static edge
find_taken_edge_cond_expr (basic_block
, tree
);
130 static edge
find_taken_edge_switch_expr (basic_block
, tree
);
131 static tree
find_case_label_for_value (tree
, tree
);
132 static bool phi_alternatives_equal (basic_block
, edge
, edge
);
135 /*---------------------------------------------------------------------------
137 ---------------------------------------------------------------------------*/
139 /* Entry point to the CFG builder for trees. TP points to the list of
140 statements to be added to the flowgraph. */
143 build_tree_cfg (tree
*tp
)
145 /* Register specific tree functions. */
146 tree_register_cfg_hooks ();
148 /* Initialize rbi_pool. */
151 /* Initialize the basic block array. */
153 profile_status
= PROFILE_ABSENT
;
155 last_basic_block
= 0;
156 VARRAY_BB_INIT (basic_block_info
, initial_cfg_capacity
, "basic_block_info");
157 memset ((void *) &cfg_stats
, 0, sizeof (cfg_stats
));
159 /* Build a mapping of labels to their associated blocks. */
160 VARRAY_BB_INIT (label_to_block_map
, initial_cfg_capacity
,
161 "label to block map");
163 ENTRY_BLOCK_PTR
->next_bb
= EXIT_BLOCK_PTR
;
164 EXIT_BLOCK_PTR
->prev_bb
= ENTRY_BLOCK_PTR
;
166 found_computed_goto
= 0;
169 /* Computed gotos are hell to deal with, especially if there are
170 lots of them with a large number of destinations. So we factor
171 them to a common computed goto location before we build the
172 edge list. After we convert back to normal form, we will un-factor
173 the computed gotos since factoring introduces an unwanted jump. */
174 if (found_computed_goto
)
175 factor_computed_gotos ();
177 /* Make sure there is always at least one block, even if it's empty. */
178 if (n_basic_blocks
== 0)
179 create_empty_bb (ENTRY_BLOCK_PTR
);
181 create_block_annotation (ENTRY_BLOCK_PTR
);
182 create_block_annotation (EXIT_BLOCK_PTR
);
184 /* Adjust the size of the array. */
185 VARRAY_GROW (basic_block_info
, n_basic_blocks
);
187 /* To speed up statement iterator walks, we first purge dead labels. */
188 cleanup_dead_labels ();
190 /* Group case nodes to reduce the number of edges.
191 We do this after cleaning up dead labels because otherwise we miss
192 a lot of obvious case merging opportunities. */
193 group_case_labels ();
195 /* Create the edges of the flowgraph. */
198 /* Debugging dumps. */
200 /* Write the flowgraph to a VCG file. */
202 int local_dump_flags
;
203 FILE *dump_file
= dump_begin (TDI_vcg
, &local_dump_flags
);
206 tree_cfg2vcg (dump_file
);
207 dump_end (TDI_vcg
, dump_file
);
211 /* Dump a textual representation of the flowgraph. */
213 dump_tree_cfg (dump_file
, dump_flags
);
217 execute_build_cfg (void)
219 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl
));
222 struct tree_opt_pass pass_build_cfg
=
226 execute_build_cfg
, /* execute */
229 0, /* static_pass_number */
230 TV_TREE_CFG
, /* tv_id */
231 PROP_gimple_leh
, /* properties_required */
232 PROP_cfg
, /* properties_provided */
233 0, /* properties_destroyed */
234 0, /* todo_flags_start */
235 TODO_verify_stmts
, /* todo_flags_finish */
239 /* Search the CFG for any computed gotos. If found, factor them to a
240 common computed goto site. Also record the location of that site so
241 that we can un-factor the gotos after we have converted back to
245 factor_computed_gotos (void)
248 tree factored_label_decl
= NULL
;
250 tree factored_computed_goto_label
= NULL
;
251 tree factored_computed_goto
= NULL
;
253 /* We know there are one or more computed gotos in this function.
254 Examine the last statement in each basic block to see if the block
255 ends with a computed goto. */
259 block_stmt_iterator bsi
= bsi_last (bb
);
264 last
= bsi_stmt (bsi
);
266 /* Ignore the computed goto we create when we factor the original
268 if (last
== factored_computed_goto
)
271 /* If the last statement is a computed goto, factor it. */
272 if (computed_goto_p (last
))
276 /* The first time we find a computed goto we need to create
277 the factored goto block and the variable each original
278 computed goto will use for their goto destination. */
279 if (! factored_computed_goto
)
281 basic_block new_bb
= create_empty_bb (bb
);
282 block_stmt_iterator new_bsi
= bsi_start (new_bb
);
284 /* Create the destination of the factored goto. Each original
285 computed goto will put its desired destination into this
286 variable and jump to the label we create immediately
288 var
= create_tmp_var (ptr_type_node
, "gotovar");
290 /* Build a label for the new block which will contain the
291 factored computed goto. */
292 factored_label_decl
= create_artificial_label ();
293 factored_computed_goto_label
294 = build1 (LABEL_EXPR
, void_type_node
, factored_label_decl
);
295 bsi_insert_after (&new_bsi
, factored_computed_goto_label
,
298 /* Build our new computed goto. */
299 factored_computed_goto
= build1 (GOTO_EXPR
, void_type_node
, var
);
300 bsi_insert_after (&new_bsi
, factored_computed_goto
,
304 /* Copy the original computed goto's destination into VAR. */
305 assignment
= build (MODIFY_EXPR
, ptr_type_node
,
306 var
, GOTO_DESTINATION (last
));
307 bsi_insert_before (&bsi
, assignment
, BSI_SAME_STMT
);
309 /* And re-vector the computed goto to the new destination. */
310 GOTO_DESTINATION (last
) = factored_label_decl
;
316 /* Create annotations for a single basic block. */
319 create_block_annotation (basic_block bb
)
321 /* Verify that the tree_annotations field is clear. */
322 gcc_assert (!bb
->tree_annotations
);
323 bb
->tree_annotations
= ggc_alloc_cleared (sizeof (struct bb_ann_d
));
327 /* Free the annotations for all the basic blocks. */
329 static void free_blocks_annotations (void)
331 clear_blocks_annotations ();
335 /* Clear the annotations for all the basic blocks. */
338 clear_blocks_annotations (void)
342 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
343 bb
->tree_annotations
= NULL
;
347 /* Build a flowgraph for the statement_list STMT_LIST. */
350 make_blocks (tree stmt_list
)
352 tree_stmt_iterator i
= tsi_start (stmt_list
);
354 bool start_new_block
= true;
355 bool first_stmt_of_list
= true;
356 basic_block bb
= ENTRY_BLOCK_PTR
;
358 while (!tsi_end_p (i
))
365 /* If the statement starts a new basic block or if we have determined
366 in a previous pass that we need to create a new block for STMT, do
368 if (start_new_block
|| stmt_starts_bb_p (stmt
, prev_stmt
))
370 if (!first_stmt_of_list
)
371 stmt_list
= tsi_split_statement_list_before (&i
);
372 bb
= create_basic_block (stmt_list
, NULL
, bb
);
373 start_new_block
= false;
376 /* Now add STMT to BB and create the subgraphs for special statement
378 set_bb_for_stmt (stmt
, bb
);
380 if (computed_goto_p (stmt
))
381 found_computed_goto
= true;
383 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
385 if (stmt_ends_bb_p (stmt
))
386 start_new_block
= true;
389 first_stmt_of_list
= false;
394 /* Create and return a new empty basic block after bb AFTER. */
397 create_bb (void *h
, void *e
, basic_block after
)
403 /* Create and initialize a new basic block. Since alloc_block uses
404 ggc_alloc_cleared to allocate a basic block, we do not have to
405 clear the newly allocated basic block here. */
408 bb
->index
= last_basic_block
;
410 bb
->stmt_list
= h
? h
: alloc_stmt_list ();
412 /* Add the new block to the linked list of blocks. */
413 link_block (bb
, after
);
415 /* Grow the basic block array if needed. */
416 if ((size_t) last_basic_block
== VARRAY_SIZE (basic_block_info
))
418 size_t new_size
= last_basic_block
+ (last_basic_block
+ 3) / 4;
419 VARRAY_GROW (basic_block_info
, new_size
);
422 /* Add the newly created block to the array. */
423 BASIC_BLOCK (last_basic_block
) = bb
;
425 create_block_annotation (bb
);
430 initialize_bb_rbi (bb
);
435 /*---------------------------------------------------------------------------
437 ---------------------------------------------------------------------------*/
439 /* Join all the blocks in the flowgraph. */
446 /* Create an edge from entry to the first block with executable
448 make_edge (ENTRY_BLOCK_PTR
, BASIC_BLOCK (0), EDGE_FALLTHRU
);
450 /* Traverse basic block array placing edges. */
453 tree first
= first_stmt (bb
);
454 tree last
= last_stmt (bb
);
458 /* Edges for statements that always alter flow control. */
459 if (is_ctrl_stmt (last
))
460 make_ctrl_stmt_edges (bb
);
462 /* Edges for statements that sometimes alter flow control. */
463 if (is_ctrl_altering_stmt (last
))
464 make_exit_edges (bb
);
467 /* Finally, if no edges were created above, this is a regular
468 basic block that only needs a fallthru edge. */
469 if (EDGE_COUNT (bb
->succs
) == 0)
470 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
473 /* We do not care about fake edges, so remove any that the CFG
474 builder inserted for completeness. */
475 remove_fake_exit_edges ();
477 /* Clean up the graph and warn for unreachable code. */
482 /* Create edges for control statement at basic block BB. */
485 make_ctrl_stmt_edges (basic_block bb
)
487 tree last
= last_stmt (bb
);
490 switch (TREE_CODE (last
))
493 make_goto_expr_edges (bb
);
497 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
501 make_cond_expr_edges (bb
);
505 make_switch_expr_edges (bb
);
509 make_eh_edges (last
);
510 /* Yet another NORETURN hack. */
511 if (EDGE_COUNT (bb
->succs
) == 0)
512 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
521 /* Create exit edges for statements in block BB that alter the flow of
522 control. Statements that alter the control flow are 'goto', 'return'
523 and calls to non-returning functions. */
526 make_exit_edges (basic_block bb
)
528 tree last
= last_stmt (bb
), op
;
531 switch (TREE_CODE (last
))
534 /* If this function receives a nonlocal goto, then we need to
535 make edges from this call site to all the nonlocal goto
537 if (TREE_SIDE_EFFECTS (last
)
538 && current_function_has_nonlocal_label
)
539 make_goto_expr_edges (bb
);
541 /* If this statement has reachable exception handlers, then
542 create abnormal edges to them. */
543 make_eh_edges (last
);
545 /* Some calls are known not to return. For such calls we create
548 We really need to revamp how we build edges so that it's not
549 such a bloody pain to avoid creating edges for this case since
550 all we do is remove these edges when we're done building the
552 if (call_expr_flags (last
) & (ECF_NORETURN
| ECF_LONGJMP
))
554 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
558 /* Don't forget the fall-thru edge. */
559 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
563 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
564 may have an abnormal edge. Search the RHS for this case and
565 create any required edges. */
566 op
= get_call_expr_in (last
);
567 if (op
&& TREE_SIDE_EFFECTS (op
)
568 && current_function_has_nonlocal_label
)
569 make_goto_expr_edges (bb
);
571 make_eh_edges (last
);
572 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
581 /* Create the edges for a COND_EXPR starting at block BB.
582 At this point, both clauses must contain only simple gotos. */
585 make_cond_expr_edges (basic_block bb
)
587 tree entry
= last_stmt (bb
);
588 basic_block then_bb
, else_bb
;
589 tree then_label
, else_label
;
592 gcc_assert (TREE_CODE (entry
) == COND_EXPR
);
594 /* Entry basic blocks for each component. */
595 then_label
= GOTO_DESTINATION (COND_EXPR_THEN (entry
));
596 else_label
= GOTO_DESTINATION (COND_EXPR_ELSE (entry
));
597 then_bb
= label_to_block (then_label
);
598 else_bb
= label_to_block (else_label
);
600 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
601 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
604 /* Hashing routine for EDGE_TO_CASE_LEADER. */
607 edge_to_case_leader_hash (const void *p
)
609 edge e
= ((struct edge_to_case_leader_elt
*)p
)->e
;
611 /* Hash on the edge itself (which is a pointer). */
612 return htab_hash_pointer (e
);
615 /* Equality routine for EDGE_TO_CASE_LEADER, edges are unique, so testing
616 for equality is just a pointer comparison. */
619 edge_to_case_leader_eq (const void *p1
, const void *p2
)
621 edge e1
= ((struct edge_to_case_leader_elt
*)p1
)->e
;
622 edge e2
= ((struct edge_to_case_leader_elt
*)p2
)->e
;
627 /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */
630 record_switch_edge (edge e
, tree case_label
)
632 struct edge_to_case_leader_elt
*elt
;
635 /* Build a hash table element so we can see if E is already
637 elt
= xmalloc (sizeof (struct edge_to_case_leader_elt
));
639 elt
->case_label
= case_label
;
641 slot
= htab_find_slot (edge_to_case_leader
, elt
, INSERT
);
645 /* E was not in the hash table. Install E into the hash table. */
650 /* E was already in the hash table. Free ELT as we do not need it
654 /* Get the entry stored in the hash table. */
655 elt
= (struct edge_to_case_leader_elt
*) *slot
;
657 /* Make ELT->case_label the leader for CASE_LABEL. */
658 CASE_LEADER_OR_LABEL (case_label
) = elt
->case_label
;
662 /* Subroutine of get_case_leader_for_edge; returns the case leader for the
663 chain of CASE_LABEL_EXPRs associated with E using a hash table lookup. */
666 get_case_leader_for_edge_hash (edge e
)
668 struct edge_to_case_leader_elt elt
, *elt_p
;
672 elt
.case_label
= NULL
;
673 slot
= htab_find_slot (edge_to_case_leader
, &elt
, NO_INSERT
);
679 elt_p
= (struct edge_to_case_leader_elt
*)*slot
;
680 t
= elt_p
->case_label
;
682 while (TREE_CODE (CASE_LEADER_OR_LABEL (t
)) == CASE_LABEL_EXPR
)
683 t
= CASE_LEADER_OR_LABEL (t
);
690 /* Given an edge E, return the case leader for the chain of CASE_LABEL_EXPRs
694 get_case_leader_for_edge (edge e
)
699 /* If we have a hash table, then use it as it's significantly faster. */
700 if (edge_to_case_leader
)
701 return get_case_leader_for_edge_hash (e
);
703 /* No hash table. We have to walk the case vector. */
704 stmt
= bsi_stmt (bsi_last (e
->src
));
705 vec
= SWITCH_LABELS (stmt
);
706 n
= TREE_VEC_LENGTH (vec
);
708 for (i
= 0; i
< n
; i
++)
710 tree elt
= TREE_VEC_ELT (vec
, i
);
711 tree t
= CASE_LEADER_OR_LABEL (elt
);
713 if (TREE_CODE (t
) == LABEL_DECL
714 && label_to_block (t
) == e
->dest
)
721 /* Create the edges for a SWITCH_EXPR starting at block BB.
722 At this point, the switch body has been lowered and the
723 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
726 make_switch_expr_edges (basic_block bb
)
728 tree entry
= last_stmt (bb
);
732 vec
= SWITCH_LABELS (entry
);
733 n
= TREE_VEC_LENGTH (vec
);
736 = htab_create (n
, edge_to_case_leader_hash
, edge_to_case_leader_eq
, free
);
738 for (i
= 0; i
< n
; ++i
)
740 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
741 basic_block label_bb
= label_to_block (lab
);
742 edge e
= make_edge (bb
, label_bb
, 0);
745 e
= find_edge (bb
, label_bb
);
747 record_switch_edge (e
, TREE_VEC_ELT (vec
, i
));
749 htab_delete (edge_to_case_leader
);
750 edge_to_case_leader
= NULL
;
754 /* Return the basic block holding label DEST. */
757 label_to_block (tree dest
)
759 int uid
= LABEL_DECL_UID (dest
);
761 /* We would die hard when faced by an undefined label. Emit a label to
762 the very first basic block. This will hopefully make even the dataflow
763 and undefined variable warnings quite right. */
764 if ((errorcount
|| sorrycount
) && uid
< 0)
766 block_stmt_iterator bsi
= bsi_start (BASIC_BLOCK (0));
769 stmt
= build1 (LABEL_EXPR
, void_type_node
, dest
);
770 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
771 uid
= LABEL_DECL_UID (dest
);
773 return VARRAY_BB (label_to_block_map
, uid
);
777 /* Create edges for a goto statement at block BB. */
780 make_goto_expr_edges (basic_block bb
)
783 basic_block target_bb
;
785 block_stmt_iterator last
= bsi_last (bb
);
787 goto_t
= bsi_stmt (last
);
789 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
790 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
791 from a nonlocal goto. */
792 if (TREE_CODE (goto_t
) != GOTO_EXPR
)
794 dest
= error_mark_node
;
799 dest
= GOTO_DESTINATION (goto_t
);
802 /* A GOTO to a local label creates normal edges. */
803 if (simple_goto_p (goto_t
))
805 edge e
= make_edge (bb
, label_to_block (dest
), EDGE_FALLTHRU
);
806 #ifdef USE_MAPPED_LOCATION
807 e
->goto_locus
= EXPR_LOCATION (goto_t
);
809 e
->goto_locus
= EXPR_LOCUS (goto_t
);
815 /* Nothing more to do for nonlocal gotos. */
816 if (TREE_CODE (dest
) == LABEL_DECL
)
819 /* Computed gotos remain. */
822 /* Look for the block starting with the destination label. In the
823 case of a computed goto, make an edge to any label block we find
825 FOR_EACH_BB (target_bb
)
827 block_stmt_iterator bsi
;
829 for (bsi
= bsi_start (target_bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
831 tree target
= bsi_stmt (bsi
);
833 if (TREE_CODE (target
) != LABEL_EXPR
)
837 /* Computed GOTOs. Make an edge to every label block that has
838 been marked as a potential target for a computed goto. */
839 (FORCED_LABEL (LABEL_EXPR_LABEL (target
)) && for_call
== 0)
840 /* Nonlocal GOTO target. Make an edge to every label block
841 that has been marked as a potential target for a nonlocal
843 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target
)) && for_call
== 1))
845 make_edge (bb
, target_bb
, EDGE_ABNORMAL
);
851 /* Degenerate case of computed goto with no labels. */
852 if (!for_call
&& EDGE_COUNT (bb
->succs
) == 0)
853 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
857 /*---------------------------------------------------------------------------
859 ---------------------------------------------------------------------------*/
861 /* Remove unreachable blocks and other miscellaneous clean up work. */
864 cleanup_tree_cfg (void)
868 timevar_push (TV_TREE_CLEANUP_CFG
);
870 retval
= cleanup_control_flow ();
871 retval
|= delete_unreachable_blocks ();
872 retval
|= thread_jumps ();
874 #ifdef ENABLE_CHECKING
877 gcc_assert (!cleanup_control_flow ());
878 gcc_assert (!delete_unreachable_blocks ());
879 gcc_assert (!thread_jumps ());
883 /* Merging the blocks creates no new opportunities for the other
884 optimizations, so do it here. */
889 #ifdef ENABLE_CHECKING
892 timevar_pop (TV_TREE_CLEANUP_CFG
);
897 /* Cleanup useless labels in basic blocks. This is something we wish
898 to do early because it allows us to group case labels before creating
899 the edges for the CFG, and it speeds up block statement iterators in
901 We only run this pass once, running it more than once is probably not
904 /* A map from basic block index to the leading label of that block. */
905 static tree
*label_for_bb
;
907 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
909 update_eh_label (struct eh_region
*region
)
911 tree old_label
= get_eh_region_tree_label (region
);
915 basic_block bb
= label_to_block (old_label
);
917 /* ??? After optimizing, there may be EH regions with labels
918 that have already been removed from the function body, so
919 there is no basic block for them. */
923 new_label
= label_for_bb
[bb
->index
];
924 set_eh_region_tree_label (region
, new_label
);
928 /* Given LABEL return the first label in the same basic block. */
930 main_block_label (tree label
)
932 basic_block bb
= label_to_block (label
);
934 /* label_to_block possibly inserted undefined label into the chain. */
935 if (!label_for_bb
[bb
->index
])
936 label_for_bb
[bb
->index
] = label
;
937 return label_for_bb
[bb
->index
];
940 /* Cleanup redundant labels. This is a three-step process:
941 1) Find the leading label for each block.
942 2) Redirect all references to labels to the leading labels.
943 3) Cleanup all useless labels. */
946 cleanup_dead_labels (void)
949 label_for_bb
= xcalloc (last_basic_block
, sizeof (tree
));
951 /* Find a suitable label for each block. We use the first user-defined
952 label if there is one, or otherwise just the first label we see. */
955 block_stmt_iterator i
;
957 for (i
= bsi_start (bb
); !bsi_end_p (i
); bsi_next (&i
))
959 tree label
, stmt
= bsi_stmt (i
);
961 if (TREE_CODE (stmt
) != LABEL_EXPR
)
964 label
= LABEL_EXPR_LABEL (stmt
);
966 /* If we have not yet seen a label for the current block,
967 remember this one and see if there are more labels. */
968 if (! label_for_bb
[bb
->index
])
970 label_for_bb
[bb
->index
] = label
;
974 /* If we did see a label for the current block already, but it
975 is an artificially created label, replace it if the current
976 label is a user defined label. */
977 if (! DECL_ARTIFICIAL (label
)
978 && DECL_ARTIFICIAL (label_for_bb
[bb
->index
]))
980 label_for_bb
[bb
->index
] = label
;
986 /* Now redirect all jumps/branches to the selected label.
987 First do so for each block ending in a control statement. */
990 tree stmt
= last_stmt (bb
);
994 switch (TREE_CODE (stmt
))
998 tree true_branch
, false_branch
;
1000 true_branch
= COND_EXPR_THEN (stmt
);
1001 false_branch
= COND_EXPR_ELSE (stmt
);
1003 GOTO_DESTINATION (true_branch
)
1004 = main_block_label (GOTO_DESTINATION (true_branch
));
1005 GOTO_DESTINATION (false_branch
)
1006 = main_block_label (GOTO_DESTINATION (false_branch
));
1014 tree vec
= SWITCH_LABELS (stmt
);
1015 size_t n
= TREE_VEC_LENGTH (vec
);
1017 /* Replace all destination labels. */
1018 for (i
= 0; i
< n
; ++i
)
1020 tree elt
= TREE_VEC_ELT (vec
, i
);
1021 tree label
= main_block_label (CASE_LABEL (elt
));
1022 CASE_LEADER_OR_LABEL (elt
) = label
;
1027 /* We have to handle GOTO_EXPRs until they're removed, and we don't
1028 remove them until after we've created the CFG edges. */
1030 if (! computed_goto_p (stmt
))
1032 GOTO_DESTINATION (stmt
)
1033 = main_block_label (GOTO_DESTINATION (stmt
));
1042 for_each_eh_region (update_eh_label
);
1044 /* Finally, purge dead labels. All user-defined labels and labels that
1045 can be the target of non-local gotos are preserved. */
1048 block_stmt_iterator i
;
1049 tree label_for_this_bb
= label_for_bb
[bb
->index
];
1051 if (! label_for_this_bb
)
1054 for (i
= bsi_start (bb
); !bsi_end_p (i
); )
1056 tree label
, stmt
= bsi_stmt (i
);
1058 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1061 label
= LABEL_EXPR_LABEL (stmt
);
1063 if (label
== label_for_this_bb
1064 || ! DECL_ARTIFICIAL (label
)
1065 || DECL_NONLOCAL (label
))
1072 free (label_for_bb
);
1075 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1076 and scan the sorted vector of cases. Combine the ones jumping to the
1078 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1081 group_case_labels (void)
1087 tree stmt
= last_stmt (bb
);
1088 if (stmt
&& TREE_CODE (stmt
) == SWITCH_EXPR
)
1090 tree labels
= SWITCH_LABELS (stmt
);
1091 int old_size
= TREE_VEC_LENGTH (labels
);
1092 int i
, j
, new_size
= old_size
;
1093 tree default_case
= TREE_VEC_ELT (labels
, old_size
- 1);
1096 /* The default label is always the last case in a switch
1097 statement after gimplification. */
1098 default_label
= CASE_LABEL (default_case
);
1100 /* Look for possible opportunities to merge cases.
1101 Ignore the last element of the label vector because it
1102 must be the default case. */
1104 while (i
< old_size
- 1)
1106 tree base_case
, base_label
, base_high
, type
;
1107 base_case
= TREE_VEC_ELT (labels
, i
);
1109 gcc_assert (base_case
);
1110 base_label
= CASE_LABEL (base_case
);
1112 /* Discard cases that have the same destination as the
1114 if (base_label
== default_label
)
1116 TREE_VEC_ELT (labels
, i
) = NULL_TREE
;
1122 type
= TREE_TYPE (CASE_LOW (base_case
));
1123 base_high
= CASE_HIGH (base_case
) ?
1124 CASE_HIGH (base_case
) : CASE_LOW (base_case
);
1126 /* Try to merge case labels. Break out when we reach the end
1127 of the label vector or when we cannot merge the next case
1128 label with the current one. */
1129 while (i
< old_size
- 1)
1131 tree merge_case
= TREE_VEC_ELT (labels
, i
);
1132 tree merge_label
= CASE_LABEL (merge_case
);
1133 tree t
= int_const_binop (PLUS_EXPR
, base_high
,
1134 integer_one_node
, 1);
1136 /* Merge the cases if they jump to the same place,
1137 and their ranges are consecutive. */
1138 if (merge_label
== base_label
1139 && tree_int_cst_equal (CASE_LOW (merge_case
), t
))
1141 base_high
= CASE_HIGH (merge_case
) ?
1142 CASE_HIGH (merge_case
) : CASE_LOW (merge_case
);
1143 CASE_HIGH (base_case
) = base_high
;
1144 TREE_VEC_ELT (labels
, i
) = NULL_TREE
;
1153 /* Compress the case labels in the label vector, and adjust the
1154 length of the vector. */
1155 for (i
= 0, j
= 0; i
< new_size
; i
++)
1157 while (! TREE_VEC_ELT (labels
, j
))
1159 TREE_VEC_ELT (labels
, i
) = TREE_VEC_ELT (labels
, j
++);
1161 TREE_VEC_LENGTH (labels
) = new_size
;
1166 /* Checks whether we can merge block B into block A. */
1169 tree_can_merge_blocks_p (basic_block a
, basic_block b
)
1172 block_stmt_iterator bsi
;
1174 if (EDGE_COUNT (a
->succs
) != 1)
1177 if (EDGE_SUCC (a
, 0)->flags
& EDGE_ABNORMAL
)
1180 if (EDGE_SUCC (a
, 0)->dest
!= b
)
1183 if (b
== EXIT_BLOCK_PTR
)
1186 if (EDGE_COUNT (b
->preds
) > 1)
1189 /* If A ends by a statement causing exceptions or something similar, we
1190 cannot merge the blocks. */
1191 stmt
= last_stmt (a
);
1192 if (stmt
&& stmt_ends_bb_p (stmt
))
1195 /* Do not allow a block with only a non-local label to be merged. */
1196 if (stmt
&& TREE_CODE (stmt
) == LABEL_EXPR
1197 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
1200 /* There may be no phi nodes at the start of b. Most of these degenerate
1201 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1205 /* Do not remove user labels. */
1206 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1208 stmt
= bsi_stmt (bsi
);
1209 if (TREE_CODE (stmt
) != LABEL_EXPR
)
1211 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt
)))
1219 /* Merge block B into block A. */
1222 tree_merge_blocks (basic_block a
, basic_block b
)
1224 block_stmt_iterator bsi
;
1225 tree_stmt_iterator last
;
1228 fprintf (dump_file
, "Merging blocks %d and %d\n", a
->index
, b
->index
);
1230 /* Ensure that B follows A. */
1231 move_block_after (b
, a
);
1233 gcc_assert (EDGE_SUCC (a
, 0)->flags
& EDGE_FALLTHRU
);
1234 gcc_assert (!last_stmt (a
) || !stmt_ends_bb_p (last_stmt (a
)));
1236 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1237 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
);)
1239 if (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
)
1243 set_bb_for_stmt (bsi_stmt (bsi
), a
);
1248 /* Merge the chains. */
1249 last
= tsi_last (a
->stmt_list
);
1250 tsi_link_after (&last
, b
->stmt_list
, TSI_NEW_STMT
);
1251 b
->stmt_list
= NULL
;
1255 /* Walk the function tree removing unnecessary statements.
1257 * Empty statement nodes are removed
1259 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1261 * Unnecessary COND_EXPRs are removed
1263 * Some unnecessary BIND_EXPRs are removed
1265 Clearly more work could be done. The trick is doing the analysis
1266 and removal fast enough to be a net improvement in compile times.
1268 Note that when we remove a control structure such as a COND_EXPR
1269 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1270 to ensure we eliminate all the useless code. */
1281 static void remove_useless_stmts_1 (tree
*, struct rus_data
*);
1284 remove_useless_stmts_warn_notreached (tree stmt
)
1286 if (EXPR_HAS_LOCATION (stmt
))
1288 location_t loc
= EXPR_LOCATION (stmt
);
1289 warning ("%Hwill never be executed", &loc
);
1293 switch (TREE_CODE (stmt
))
1295 case STATEMENT_LIST
:
1297 tree_stmt_iterator i
;
1298 for (i
= tsi_start (stmt
); !tsi_end_p (i
); tsi_next (&i
))
1299 if (remove_useless_stmts_warn_notreached (tsi_stmt (i
)))
1305 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt
)))
1307 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt
)))
1309 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt
)))
1313 case TRY_FINALLY_EXPR
:
1314 case TRY_CATCH_EXPR
:
1315 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt
, 0)))
1317 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt
, 1)))
1322 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt
));
1323 case EH_FILTER_EXPR
:
1324 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt
));
1326 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt
));
1329 /* Not a live container. */
1337 remove_useless_stmts_cond (tree
*stmt_p
, struct rus_data
*data
)
1339 tree then_clause
, else_clause
, cond
;
1340 bool save_has_label
, then_has_label
, else_has_label
;
1342 save_has_label
= data
->has_label
;
1343 data
->has_label
= false;
1344 data
->last_goto
= NULL
;
1346 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p
), data
);
1348 then_has_label
= data
->has_label
;
1349 data
->has_label
= false;
1350 data
->last_goto
= NULL
;
1352 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p
), data
);
1354 else_has_label
= data
->has_label
;
1355 data
->has_label
= save_has_label
| then_has_label
| else_has_label
;
1357 then_clause
= COND_EXPR_THEN (*stmt_p
);
1358 else_clause
= COND_EXPR_ELSE (*stmt_p
);
1359 cond
= COND_EXPR_COND (*stmt_p
);
1361 /* If neither arm does anything at all, we can remove the whole IF. */
1362 if (!TREE_SIDE_EFFECTS (then_clause
) && !TREE_SIDE_EFFECTS (else_clause
))
1364 *stmt_p
= build_empty_stmt ();
1365 data
->repeat
= true;
1368 /* If there are no reachable statements in an arm, then we can
1369 zap the entire conditional. */
1370 else if (integer_nonzerop (cond
) && !else_has_label
)
1372 if (warn_notreached
)
1373 remove_useless_stmts_warn_notreached (else_clause
);
1374 *stmt_p
= then_clause
;
1375 data
->repeat
= true;
1377 else if (integer_zerop (cond
) && !then_has_label
)
1379 if (warn_notreached
)
1380 remove_useless_stmts_warn_notreached (then_clause
);
1381 *stmt_p
= else_clause
;
1382 data
->repeat
= true;
1385 /* Check a couple of simple things on then/else with single stmts. */
1388 tree then_stmt
= expr_only (then_clause
);
1389 tree else_stmt
= expr_only (else_clause
);
1391 /* Notice branches to a common destination. */
1392 if (then_stmt
&& else_stmt
1393 && TREE_CODE (then_stmt
) == GOTO_EXPR
1394 && TREE_CODE (else_stmt
) == GOTO_EXPR
1395 && (GOTO_DESTINATION (then_stmt
) == GOTO_DESTINATION (else_stmt
)))
1397 *stmt_p
= then_stmt
;
1398 data
->repeat
= true;
1401 /* If the THEN/ELSE clause merely assigns a value to a variable or
1402 parameter which is already known to contain that value, then
1403 remove the useless THEN/ELSE clause. */
1404 else if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
1407 && TREE_CODE (else_stmt
) == MODIFY_EXPR
1408 && TREE_OPERAND (else_stmt
, 0) == cond
1409 && integer_zerop (TREE_OPERAND (else_stmt
, 1)))
1410 COND_EXPR_ELSE (*stmt_p
) = alloc_stmt_list ();
1412 else if ((TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
1413 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1414 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
1415 && TREE_CONSTANT (TREE_OPERAND (cond
, 1)))
1417 tree stmt
= (TREE_CODE (cond
) == EQ_EXPR
1418 ? then_stmt
: else_stmt
);
1419 tree
*location
= (TREE_CODE (cond
) == EQ_EXPR
1420 ? &COND_EXPR_THEN (*stmt_p
)
1421 : &COND_EXPR_ELSE (*stmt_p
));
1424 && TREE_CODE (stmt
) == MODIFY_EXPR
1425 && TREE_OPERAND (stmt
, 0) == TREE_OPERAND (cond
, 0)
1426 && TREE_OPERAND (stmt
, 1) == TREE_OPERAND (cond
, 1))
1427 *location
= alloc_stmt_list ();
1431 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1432 would be re-introduced during lowering. */
1433 data
->last_goto
= NULL
;
1438 remove_useless_stmts_tf (tree
*stmt_p
, struct rus_data
*data
)
1440 bool save_may_branch
, save_may_throw
;
1441 bool this_may_branch
, this_may_throw
;
1443 /* Collect may_branch and may_throw information for the body only. */
1444 save_may_branch
= data
->may_branch
;
1445 save_may_throw
= data
->may_throw
;
1446 data
->may_branch
= false;
1447 data
->may_throw
= false;
1448 data
->last_goto
= NULL
;
1450 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
1452 this_may_branch
= data
->may_branch
;
1453 this_may_throw
= data
->may_throw
;
1454 data
->may_branch
|= save_may_branch
;
1455 data
->may_throw
|= save_may_throw
;
1456 data
->last_goto
= NULL
;
1458 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
1460 /* If the body is empty, then we can emit the FINALLY block without
1461 the enclosing TRY_FINALLY_EXPR. */
1462 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 0)))
1464 *stmt_p
= TREE_OPERAND (*stmt_p
, 1);
1465 data
->repeat
= true;
1468 /* If the handler is empty, then we can emit the TRY block without
1469 the enclosing TRY_FINALLY_EXPR. */
1470 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 1)))
1472 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1473 data
->repeat
= true;
1476 /* If the body neither throws, nor branches, then we can safely
1477 string the TRY and FINALLY blocks together. */
1478 else if (!this_may_branch
&& !this_may_throw
)
1480 tree stmt
= *stmt_p
;
1481 *stmt_p
= TREE_OPERAND (stmt
, 0);
1482 append_to_statement_list (TREE_OPERAND (stmt
, 1), stmt_p
);
1483 data
->repeat
= true;
1489 remove_useless_stmts_tc (tree
*stmt_p
, struct rus_data
*data
)
1491 bool save_may_throw
, this_may_throw
;
1492 tree_stmt_iterator i
;
1495 /* Collect may_throw information for the body only. */
1496 save_may_throw
= data
->may_throw
;
1497 data
->may_throw
= false;
1498 data
->last_goto
= NULL
;
1500 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
1502 this_may_throw
= data
->may_throw
;
1503 data
->may_throw
= save_may_throw
;
1505 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1506 if (!this_may_throw
)
1508 if (warn_notreached
)
1509 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p
, 1));
1510 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1511 data
->repeat
= true;
1515 /* Process the catch clause specially. We may be able to tell that
1516 no exceptions propagate past this point. */
1518 this_may_throw
= true;
1519 i
= tsi_start (TREE_OPERAND (*stmt_p
, 1));
1520 stmt
= tsi_stmt (i
);
1521 data
->last_goto
= NULL
;
1523 switch (TREE_CODE (stmt
))
1526 for (; !tsi_end_p (i
); tsi_next (&i
))
1528 stmt
= tsi_stmt (i
);
1529 /* If we catch all exceptions, then the body does not
1530 propagate exceptions past this point. */
1531 if (CATCH_TYPES (stmt
) == NULL
)
1532 this_may_throw
= false;
1533 data
->last_goto
= NULL
;
1534 remove_useless_stmts_1 (&CATCH_BODY (stmt
), data
);
1538 case EH_FILTER_EXPR
:
1539 if (EH_FILTER_MUST_NOT_THROW (stmt
))
1540 this_may_throw
= false;
1541 else if (EH_FILTER_TYPES (stmt
) == NULL
)
1542 this_may_throw
= false;
1543 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt
), data
);
1547 /* Otherwise this is a cleanup. */
1548 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
1550 /* If the cleanup is empty, then we can emit the TRY block without
1551 the enclosing TRY_CATCH_EXPR. */
1552 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p
, 1)))
1554 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
1555 data
->repeat
= true;
1559 data
->may_throw
|= this_may_throw
;
1564 remove_useless_stmts_bind (tree
*stmt_p
, struct rus_data
*data
)
1568 /* First remove anything underneath the BIND_EXPR. */
1569 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p
), data
);
1571 /* If the BIND_EXPR has no variables, then we can pull everything
1572 up one level and remove the BIND_EXPR, unless this is the toplevel
1573 BIND_EXPR for the current function or an inlined function.
1575 When this situation occurs we will want to apply this
1576 optimization again. */
1577 block
= BIND_EXPR_BLOCK (*stmt_p
);
1578 if (BIND_EXPR_VARS (*stmt_p
) == NULL_TREE
1579 && *stmt_p
!= DECL_SAVED_TREE (current_function_decl
)
1581 || ! BLOCK_ABSTRACT_ORIGIN (block
)
1582 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
))
1585 *stmt_p
= BIND_EXPR_BODY (*stmt_p
);
1586 data
->repeat
= true;
1592 remove_useless_stmts_goto (tree
*stmt_p
, struct rus_data
*data
)
1594 tree dest
= GOTO_DESTINATION (*stmt_p
);
1596 data
->may_branch
= true;
1597 data
->last_goto
= NULL
;
1599 /* Record the last goto expr, so that we can delete it if unnecessary. */
1600 if (TREE_CODE (dest
) == LABEL_DECL
)
1601 data
->last_goto
= stmt_p
;
1606 remove_useless_stmts_label (tree
*stmt_p
, struct rus_data
*data
)
1608 tree label
= LABEL_EXPR_LABEL (*stmt_p
);
1610 data
->has_label
= true;
1612 /* We do want to jump across non-local label receiver code. */
1613 if (DECL_NONLOCAL (label
))
1614 data
->last_goto
= NULL
;
1616 else if (data
->last_goto
&& GOTO_DESTINATION (*data
->last_goto
) == label
)
1618 *data
->last_goto
= build_empty_stmt ();
1619 data
->repeat
= true;
1622 /* ??? Add something here to delete unused labels. */
1626 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1627 decl. This allows us to eliminate redundant or useless
1628 calls to "const" functions.
1630 Gimplifier already does the same operation, but we may notice functions
1631 being const and pure once their calls has been gimplified, so we need
1632 to update the flag. */
1635 update_call_expr_flags (tree call
)
1637 tree decl
= get_callee_fndecl (call
);
1640 if (call_expr_flags (call
) & (ECF_CONST
| ECF_PURE
))
1641 TREE_SIDE_EFFECTS (call
) = 0;
1642 if (TREE_NOTHROW (decl
))
1643 TREE_NOTHROW (call
) = 1;
1647 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1650 notice_special_calls (tree t
)
1652 int flags
= call_expr_flags (t
);
1654 if (flags
& ECF_MAY_BE_ALLOCA
)
1655 current_function_calls_alloca
= true;
1656 if (flags
& ECF_RETURNS_TWICE
)
1657 current_function_calls_setjmp
= true;
1661 /* Clear flags set by notice_special_calls. Used by dead code removal
1662 to update the flags. */
1665 clear_special_calls (void)
1667 current_function_calls_alloca
= false;
1668 current_function_calls_setjmp
= false;
1673 remove_useless_stmts_1 (tree
*tp
, struct rus_data
*data
)
1677 switch (TREE_CODE (t
))
1680 remove_useless_stmts_cond (tp
, data
);
1683 case TRY_FINALLY_EXPR
:
1684 remove_useless_stmts_tf (tp
, data
);
1687 case TRY_CATCH_EXPR
:
1688 remove_useless_stmts_tc (tp
, data
);
1692 remove_useless_stmts_bind (tp
, data
);
1696 remove_useless_stmts_goto (tp
, data
);
1700 remove_useless_stmts_label (tp
, data
);
1705 data
->last_goto
= NULL
;
1706 data
->may_branch
= true;
1711 data
->last_goto
= NULL
;
1712 notice_special_calls (t
);
1713 update_call_expr_flags (t
);
1714 if (tree_could_throw_p (t
))
1715 data
->may_throw
= true;
1719 data
->last_goto
= NULL
;
1721 op
= get_call_expr_in (t
);
1724 update_call_expr_flags (op
);
1725 notice_special_calls (op
);
1727 if (tree_could_throw_p (t
))
1728 data
->may_throw
= true;
1731 case STATEMENT_LIST
:
1733 tree_stmt_iterator i
= tsi_start (t
);
1734 while (!tsi_end_p (i
))
1737 if (IS_EMPTY_STMT (t
))
1743 remove_useless_stmts_1 (tsi_stmt_ptr (i
), data
);
1746 if (TREE_CODE (t
) == STATEMENT_LIST
)
1748 tsi_link_before (&i
, t
, TSI_SAME_STMT
);
1758 data
->last_goto
= NULL
;
1762 data
->last_goto
= NULL
;
1768 remove_useless_stmts (void)
1770 struct rus_data data
;
1772 clear_special_calls ();
1776 memset (&data
, 0, sizeof (data
));
1777 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl
), &data
);
1779 while (data
.repeat
);
1783 struct tree_opt_pass pass_remove_useless_stmts
=
1785 "useless", /* name */
1787 remove_useless_stmts
, /* execute */
1790 0, /* static_pass_number */
1792 PROP_gimple_any
, /* properties_required */
1793 0, /* properties_provided */
1794 0, /* properties_destroyed */
1795 0, /* todo_flags_start */
1796 TODO_dump_func
, /* todo_flags_finish */
1801 /* Remove obviously useless statements in basic block BB. */
1804 cfg_remove_useless_stmts_bb (basic_block bb
)
1806 block_stmt_iterator bsi
;
1807 tree stmt
= NULL_TREE
;
1808 tree cond
, var
= NULL_TREE
, val
= NULL_TREE
;
1809 struct var_ann_d
*ann
;
1811 /* Check whether we come here from a condition, and if so, get the
1813 if (EDGE_COUNT (bb
->preds
) != 1
1814 || !(EDGE_PRED (bb
, 0)->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
1817 cond
= COND_EXPR_COND (last_stmt (EDGE_PRED (bb
, 0)->src
));
1819 if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
1822 val
= (EDGE_PRED (bb
, 0)->flags
& EDGE_FALSE_VALUE
1823 ? boolean_false_node
: boolean_true_node
);
1825 else if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
1826 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1827 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
))
1829 var
= TREE_OPERAND (cond
, 0);
1830 val
= (EDGE_PRED (bb
, 0)->flags
& EDGE_FALSE_VALUE
1831 ? boolean_true_node
: boolean_false_node
);
1835 if (EDGE_PRED (bb
, 0)->flags
& EDGE_FALSE_VALUE
)
1836 cond
= invert_truthvalue (cond
);
1837 if (TREE_CODE (cond
) == EQ_EXPR
1838 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
1839 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
1840 && (TREE_CODE (TREE_OPERAND (cond
, 1)) == VAR_DECL
1841 || TREE_CODE (TREE_OPERAND (cond
, 1)) == PARM_DECL
1842 || TREE_CONSTANT (TREE_OPERAND (cond
, 1))))
1844 var
= TREE_OPERAND (cond
, 0);
1845 val
= TREE_OPERAND (cond
, 1);
1851 /* Only work for normal local variables. */
1852 ann
= var_ann (var
);
1855 || TREE_ADDRESSABLE (var
))
1858 if (! TREE_CONSTANT (val
))
1860 ann
= var_ann (val
);
1863 || TREE_ADDRESSABLE (val
))
1867 /* Ignore floating point variables, since comparison behaves weird for
1869 if (FLOAT_TYPE_P (TREE_TYPE (var
)))
1872 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
);)
1874 stmt
= bsi_stmt (bsi
);
1876 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1877 which is already known to contain that value, then remove the useless
1878 THEN/ELSE clause. */
1879 if (TREE_CODE (stmt
) == MODIFY_EXPR
1880 && TREE_OPERAND (stmt
, 0) == var
1881 && operand_equal_p (val
, TREE_OPERAND (stmt
, 1), 0))
1887 /* Invalidate the var if we encounter something that could modify it.
1888 Likewise for the value it was previously set to. Note that we only
1889 consider values that are either a VAR_DECL or PARM_DECL so we
1890 can test for conflict very simply. */
1891 if (TREE_CODE (stmt
) == ASM_EXPR
1892 || (TREE_CODE (stmt
) == MODIFY_EXPR
1893 && (TREE_OPERAND (stmt
, 0) == var
1894 || TREE_OPERAND (stmt
, 0) == val
)))
1902 /* A CFG-aware version of remove_useless_stmts. */
1905 cfg_remove_useless_stmts (void)
1909 #ifdef ENABLE_CHECKING
1910 verify_flow_info ();
1915 cfg_remove_useless_stmts_bb (bb
);
1920 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1923 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb
)
1927 /* Since this block is no longer reachable, we can just delete all
1928 of its PHI nodes. */
1929 phi
= phi_nodes (bb
);
1932 tree next
= PHI_CHAIN (phi
);
1933 remove_phi_node (phi
, NULL_TREE
, bb
);
1937 /* Remove edges to BB's successors. */
1938 while (EDGE_COUNT (bb
->succs
) > 0)
1939 ssa_remove_edge (EDGE_SUCC (bb
, 0));
1943 /* Remove statements of basic block BB. */
1946 remove_bb (basic_block bb
)
1948 block_stmt_iterator i
;
1949 source_locus loc
= 0;
1953 fprintf (dump_file
, "Removing basic block %d\n", bb
->index
);
1954 if (dump_flags
& TDF_DETAILS
)
1956 dump_bb (bb
, dump_file
, 0);
1957 fprintf (dump_file
, "\n");
1961 /* Remove all the instructions in the block. */
1962 for (i
= bsi_start (bb
); !bsi_end_p (i
);)
1964 tree stmt
= bsi_stmt (i
);
1965 if (TREE_CODE (stmt
) == LABEL_EXPR
1966 && FORCED_LABEL (LABEL_EXPR_LABEL (stmt
)))
1968 basic_block new_bb
= bb
->prev_bb
;
1969 block_stmt_iterator new_bsi
= bsi_after_labels (new_bb
);
1972 bsi_insert_after (&new_bsi
, stmt
, BSI_NEW_STMT
);
1976 release_defs (stmt
);
1978 set_bb_for_stmt (stmt
, NULL
);
1982 /* Don't warn for removed gotos. Gotos are often removed due to
1983 jump threading, thus resulting in bogus warnings. Not great,
1984 since this way we lose warnings for gotos in the original
1985 program that are indeed unreachable. */
1986 if (TREE_CODE (stmt
) != GOTO_EXPR
&& EXPR_HAS_LOCATION (stmt
) && !loc
)
1987 #ifdef USE_MAPPED_LOCATION
1988 loc
= EXPR_LOCATION (stmt
);
1990 loc
= EXPR_LOCUS (stmt
);
1994 /* If requested, give a warning that the first statement in the
1995 block is unreachable. We walk statements backwards in the
1996 loop above, so the last statement we process is the first statement
1998 if (warn_notreached
&& loc
)
1999 #ifdef USE_MAPPED_LOCATION
2000 warning ("%Hwill never be executed", &loc
);
2002 warning ("%Hwill never be executed", loc
);
2005 remove_phi_nodes_and_edges_for_unreachable_block (bb
);
2008 /* Try to remove superfluous control structures. */
2011 cleanup_control_flow (void)
2014 block_stmt_iterator bsi
;
2015 bool retval
= false;
2020 bsi
= bsi_last (bb
);
2022 if (bsi_end_p (bsi
))
2025 stmt
= bsi_stmt (bsi
);
2026 if (TREE_CODE (stmt
) == COND_EXPR
2027 || TREE_CODE (stmt
) == SWITCH_EXPR
)
2028 retval
|= cleanup_control_expr_graph (bb
, bsi
);
2034 /* Disconnect an unreachable block in the control expression starting
2038 cleanup_control_expr_graph (basic_block bb
, block_stmt_iterator bsi
)
2041 bool retval
= false;
2042 tree expr
= bsi_stmt (bsi
), val
;
2044 if (EDGE_COUNT (bb
->succs
) > 1)
2049 switch (TREE_CODE (expr
))
2052 val
= COND_EXPR_COND (expr
);
2056 val
= SWITCH_COND (expr
);
2057 if (TREE_CODE (val
) != INTEGER_CST
)
2065 taken_edge
= find_taken_edge (bb
, val
);
2069 /* Remove all the edges except the one that is always executed. */
2070 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
2072 if (e
!= taken_edge
)
2074 taken_edge
->probability
+= e
->probability
;
2075 taken_edge
->count
+= e
->count
;
2076 ssa_remove_edge (e
);
2082 if (taken_edge
->probability
> REG_BR_PROB_BASE
)
2083 taken_edge
->probability
= REG_BR_PROB_BASE
;
2086 taken_edge
= EDGE_SUCC (bb
, 0);
2089 taken_edge
->flags
= EDGE_FALLTHRU
;
2091 /* We removed some paths from the cfg. */
2092 free_dominance_info (CDI_DOMINATORS
);
2098 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2099 predicate VAL, return the edge that will be taken out of the block.
2100 If VAL does not match a unique edge, NULL is returned. */
2103 find_taken_edge (basic_block bb
, tree val
)
2107 stmt
= last_stmt (bb
);
2110 gcc_assert (is_ctrl_stmt (stmt
));
2113 /* If VAL is a predicate of the form N RELOP N, where N is an
2114 SSA_NAME, we can usually determine its truth value. */
2115 if (COMPARISON_CLASS_P (val
))
2118 /* If VAL is not a constant, we can't determine which edge might
2120 if (!really_constant_p (val
))
2123 if (TREE_CODE (stmt
) == COND_EXPR
)
2124 return find_taken_edge_cond_expr (bb
, val
);
2126 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
2127 return find_taken_edge_switch_expr (bb
, val
);
2133 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2134 statement, determine which of the two edges will be taken out of the
2135 block. Return NULL if either edge may be taken. */
2138 find_taken_edge_cond_expr (basic_block bb
, tree val
)
2140 edge true_edge
, false_edge
;
2142 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
2144 /* Otherwise, try to determine which branch of the if() will be taken.
2145 If VAL is a constant but it can't be reduced to a 0 or a 1, then
2146 we don't really know which edge will be taken at runtime. This
2147 may happen when comparing addresses (e.g., if (&var1 == 4)). */
2148 if (integer_nonzerop (val
))
2150 else if (integer_zerop (val
))
2157 /* Given a constant value VAL and the entry block BB to a SWITCH_EXPR
2158 statement, determine which edge will be taken out of the block. Return
2159 NULL if any edge may be taken. */
2162 find_taken_edge_switch_expr (basic_block bb
, tree val
)
2164 tree switch_expr
, taken_case
;
2165 basic_block dest_bb
;
2168 if (TREE_CODE (val
) != INTEGER_CST
)
2171 switch_expr
= last_stmt (bb
);
2172 taken_case
= find_case_label_for_value (switch_expr
, val
);
2173 dest_bb
= label_to_block (CASE_LABEL (taken_case
));
2175 e
= find_edge (bb
, dest_bb
);
2181 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2182 We can make optimal use here of the fact that the case labels are
2183 sorted: We can do a binary search for a case matching VAL. */
2186 find_case_label_for_value (tree switch_expr
, tree val
)
2188 tree vec
= SWITCH_LABELS (switch_expr
);
2189 size_t low
, high
, n
= TREE_VEC_LENGTH (vec
);
2190 tree default_case
= TREE_VEC_ELT (vec
, n
- 1);
2192 for (low
= -1, high
= n
- 1; high
- low
> 1; )
2194 size_t i
= (high
+ low
) / 2;
2195 tree t
= TREE_VEC_ELT (vec
, i
);
2198 /* Cache the result of comparing CASE_LOW and val. */
2199 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
2206 if (CASE_HIGH (t
) == NULL
)
2208 /* A singe-valued case label. */
2214 /* A case range. We can only handle integer ranges. */
2215 if (cmp
<= 0 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
2220 return default_case
;
2224 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2225 those alternatives are equal in each of the PHI nodes, then return
2226 true, else return false. */
2229 phi_alternatives_equal (basic_block dest
, edge e1
, edge e2
)
2231 tree phi
, val1
, val2
;
2234 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
2236 n1
= phi_arg_from_edge (phi
, e1
);
2237 n2
= phi_arg_from_edge (phi
, e2
);
2239 gcc_assert (n1
>= 0);
2240 gcc_assert (n2
>= 0);
2242 val1
= PHI_ARG_DEF (phi
, n1
);
2243 val2
= PHI_ARG_DEF (phi
, n2
);
2245 if (!operand_equal_p (val1
, val2
, 0))
2253 /*---------------------------------------------------------------------------
2255 ---------------------------------------------------------------------------*/
2257 /* Dump tree-specific information of block BB to file OUTF. */
2260 tree_dump_bb (basic_block bb
, FILE *outf
, int indent
)
2262 dump_generic_bb (outf
, bb
, indent
, TDF_VOPS
);
2266 /* Dump a basic block on stderr. */
2269 debug_tree_bb (basic_block bb
)
2271 dump_bb (bb
, stderr
, 0);
2275 /* Dump basic block with index N on stderr. */
2278 debug_tree_bb_n (int n
)
2280 debug_tree_bb (BASIC_BLOCK (n
));
2281 return BASIC_BLOCK (n
);
2285 /* Dump the CFG on stderr.
2287 FLAGS are the same used by the tree dumping functions
2288 (see TDF_* in tree.h). */
2291 debug_tree_cfg (int flags
)
2293 dump_tree_cfg (stderr
, flags
);
2297 /* Dump the program showing basic block boundaries on the given FILE.
2299 FLAGS are the same used by the tree dumping functions (see TDF_* in
2303 dump_tree_cfg (FILE *file
, int flags
)
2305 if (flags
& TDF_DETAILS
)
2307 const char *funcname
2308 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2311 fprintf (file
, ";; Function %s\n\n", funcname
);
2312 fprintf (file
, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2313 n_basic_blocks
, n_edges
, last_basic_block
);
2315 brief_dump_cfg (file
);
2316 fprintf (file
, "\n");
2319 if (flags
& TDF_STATS
)
2320 dump_cfg_stats (file
);
2322 dump_function_to_file (current_function_decl
, file
, flags
| TDF_BLOCKS
);
2326 /* Dump CFG statistics on FILE. */
2329 dump_cfg_stats (FILE *file
)
2331 static long max_num_merged_labels
= 0;
2332 unsigned long size
, total
= 0;
2335 const char * const fmt_str
= "%-30s%-13s%12s\n";
2336 const char * const fmt_str_1
= "%-30s%13d%11lu%c\n";
2337 const char * const fmt_str_3
= "%-43s%11lu%c\n";
2338 const char *funcname
2339 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2342 fprintf (file
, "\nCFG Statistics for %s\n\n", funcname
);
2344 fprintf (file
, "---------------------------------------------------------\n");
2345 fprintf (file
, fmt_str
, "", " Number of ", "Memory");
2346 fprintf (file
, fmt_str
, "", " instances ", "used ");
2347 fprintf (file
, "---------------------------------------------------------\n");
2349 size
= n_basic_blocks
* sizeof (struct basic_block_def
);
2351 fprintf (file
, fmt_str_1
, "Basic blocks", n_basic_blocks
,
2352 SCALE (size
), LABEL (size
));
2356 n_edges
+= EDGE_COUNT (bb
->succs
);
2357 size
= n_edges
* sizeof (struct edge_def
);
2359 fprintf (file
, fmt_str_1
, "Edges", n_edges
, SCALE (size
), LABEL (size
));
2361 size
= n_basic_blocks
* sizeof (struct bb_ann_d
);
2363 fprintf (file
, fmt_str_1
, "Basic block annotations", n_basic_blocks
,
2364 SCALE (size
), LABEL (size
));
2366 fprintf (file
, "---------------------------------------------------------\n");
2367 fprintf (file
, fmt_str_3
, "Total memory used by CFG data", SCALE (total
),
2369 fprintf (file
, "---------------------------------------------------------\n");
2370 fprintf (file
, "\n");
2372 if (cfg_stats
.num_merged_labels
> max_num_merged_labels
)
2373 max_num_merged_labels
= cfg_stats
.num_merged_labels
;
2375 fprintf (file
, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2376 cfg_stats
.num_merged_labels
, max_num_merged_labels
);
2378 fprintf (file
, "\n");
2382 /* Dump CFG statistics on stderr. Keep extern so that it's always
2383 linked in the final executable. */
2386 debug_cfg_stats (void)
2388 dump_cfg_stats (stderr
);
2392 /* Dump the flowgraph to a .vcg FILE. */
2395 tree_cfg2vcg (FILE *file
)
2400 const char *funcname
2401 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
2403 /* Write the file header. */
2404 fprintf (file
, "graph: { title: \"%s\"\n", funcname
);
2405 fprintf (file
, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2406 fprintf (file
, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2408 /* Write blocks and edges. */
2409 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
2411 fprintf (file
, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2414 if (e
->flags
& EDGE_FAKE
)
2415 fprintf (file
, " linestyle: dotted priority: 10");
2417 fprintf (file
, " linestyle: solid priority: 100");
2419 fprintf (file
, " }\n");
2425 enum tree_code head_code
, end_code
;
2426 const char *head_name
, *end_name
;
2429 tree first
= first_stmt (bb
);
2430 tree last
= last_stmt (bb
);
2434 head_code
= TREE_CODE (first
);
2435 head_name
= tree_code_name
[head_code
];
2436 head_line
= get_lineno (first
);
2439 head_name
= "no-statement";
2443 end_code
= TREE_CODE (last
);
2444 end_name
= tree_code_name
[end_code
];
2445 end_line
= get_lineno (last
);
2448 end_name
= "no-statement";
2450 fprintf (file
, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2451 bb
->index
, bb
->index
, head_name
, head_line
, end_name
,
2454 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2456 if (e
->dest
== EXIT_BLOCK_PTR
)
2457 fprintf (file
, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb
->index
);
2459 fprintf (file
, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb
->index
, e
->dest
->index
);
2461 if (e
->flags
& EDGE_FAKE
)
2462 fprintf (file
, " priority: 10 linestyle: dotted");
2464 fprintf (file
, " priority: 100 linestyle: solid");
2466 fprintf (file
, " }\n");
2469 if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
2473 fputs ("}\n\n", file
);
2478 /*---------------------------------------------------------------------------
2479 Miscellaneous helpers
2480 ---------------------------------------------------------------------------*/
2482 /* Return true if T represents a stmt that always transfers control. */
2485 is_ctrl_stmt (tree t
)
2487 return (TREE_CODE (t
) == COND_EXPR
2488 || TREE_CODE (t
) == SWITCH_EXPR
2489 || TREE_CODE (t
) == GOTO_EXPR
2490 || TREE_CODE (t
) == RETURN_EXPR
2491 || TREE_CODE (t
) == RESX_EXPR
);
2495 /* Return true if T is a statement that may alter the flow of control
2496 (e.g., a call to a non-returning function). */
2499 is_ctrl_altering_stmt (tree t
)
2504 call
= get_call_expr_in (t
);
2507 /* A non-pure/const CALL_EXPR alters flow control if the current
2508 function has nonlocal labels. */
2509 if (TREE_SIDE_EFFECTS (call
) && current_function_has_nonlocal_label
)
2512 /* A CALL_EXPR also alters control flow if it does not return. */
2513 if (call_expr_flags (call
) & (ECF_NORETURN
| ECF_LONGJMP
))
2517 /* If a statement can throw, it alters control flow. */
2518 return tree_can_throw_internal (t
);
2522 /* Return true if T is a computed goto. */
2525 computed_goto_p (tree t
)
2527 return (TREE_CODE (t
) == GOTO_EXPR
2528 && TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
);
2532 /* Checks whether EXPR is a simple local goto. */
2535 simple_goto_p (tree expr
)
2537 return (TREE_CODE (expr
) == GOTO_EXPR
2538 && TREE_CODE (GOTO_DESTINATION (expr
)) == LABEL_DECL
);
2542 /* Return true if T should start a new basic block. PREV_T is the
2543 statement preceding T. It is used when T is a label or a case label.
2544 Labels should only start a new basic block if their previous statement
2545 wasn't a label. Otherwise, sequence of labels would generate
2546 unnecessary basic blocks that only contain a single label. */
2549 stmt_starts_bb_p (tree t
, tree prev_t
)
2551 enum tree_code code
;
2556 /* LABEL_EXPRs start a new basic block only if the preceding
2557 statement wasn't a label of the same type. This prevents the
2558 creation of consecutive blocks that have nothing but a single
2560 code
= TREE_CODE (t
);
2561 if (code
== LABEL_EXPR
)
2563 /* Nonlocal and computed GOTO targets always start a new block. */
2564 if (code
== LABEL_EXPR
2565 && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t
))
2566 || FORCED_LABEL (LABEL_EXPR_LABEL (t
))))
2569 if (prev_t
&& TREE_CODE (prev_t
) == code
)
2571 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t
)))
2574 cfg_stats
.num_merged_labels
++;
2585 /* Return true if T should end a basic block. */
2588 stmt_ends_bb_p (tree t
)
2590 return is_ctrl_stmt (t
) || is_ctrl_altering_stmt (t
);
2594 /* Add gotos that used to be represented implicitly in the CFG. */
2597 disband_implicit_edges (void)
2600 block_stmt_iterator last
;
2607 last
= bsi_last (bb
);
2608 stmt
= last_stmt (bb
);
2610 if (stmt
&& TREE_CODE (stmt
) == COND_EXPR
)
2612 /* Remove superfluous gotos from COND_EXPR branches. Moved
2613 from cfg_remove_useless_stmts here since it violates the
2614 invariants for tree--cfg correspondence and thus fits better
2615 here where we do it anyway. */
2616 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2618 if (e
->dest
!= bb
->next_bb
)
2621 if (e
->flags
& EDGE_TRUE_VALUE
)
2622 COND_EXPR_THEN (stmt
) = build_empty_stmt ();
2623 else if (e
->flags
& EDGE_FALSE_VALUE
)
2624 COND_EXPR_ELSE (stmt
) = build_empty_stmt ();
2627 e
->flags
|= EDGE_FALLTHRU
;
2633 if (stmt
&& TREE_CODE (stmt
) == RETURN_EXPR
)
2635 /* Remove the RETURN_EXPR if we may fall though to the exit
2637 gcc_assert (EDGE_COUNT (bb
->succs
) == 1);
2638 gcc_assert (EDGE_SUCC (bb
, 0)->dest
== EXIT_BLOCK_PTR
);
2640 if (bb
->next_bb
== EXIT_BLOCK_PTR
2641 && !TREE_OPERAND (stmt
, 0))
2644 EDGE_SUCC (bb
, 0)->flags
|= EDGE_FALLTHRU
;
2649 /* There can be no fallthru edge if the last statement is a control
2651 if (stmt
&& is_ctrl_stmt (stmt
))
2654 /* Find a fallthru edge and emit the goto if necessary. */
2655 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2656 if (e
->flags
& EDGE_FALLTHRU
)
2659 if (!e
|| e
->dest
== bb
->next_bb
)
2662 gcc_assert (e
->dest
!= EXIT_BLOCK_PTR
);
2663 label
= tree_block_label (e
->dest
);
2665 stmt
= build1 (GOTO_EXPR
, void_type_node
, label
);
2666 #ifdef USE_MAPPED_LOCATION
2667 SET_EXPR_LOCATION (stmt
, e
->goto_locus
);
2669 SET_EXPR_LOCUS (stmt
, e
->goto_locus
);
2671 bsi_insert_after (&last
, stmt
, BSI_NEW_STMT
);
2672 e
->flags
&= ~EDGE_FALLTHRU
;
2676 /* Remove block annotations and other datastructures. */
2679 delete_tree_cfg_annotations (void)
2682 if (n_basic_blocks
> 0)
2683 free_blocks_annotations ();
2685 label_to_block_map
= NULL
;
2692 /* Return the first statement in basic block BB. */
2695 first_stmt (basic_block bb
)
2697 block_stmt_iterator i
= bsi_start (bb
);
2698 return !bsi_end_p (i
) ? bsi_stmt (i
) : NULL_TREE
;
2702 /* Return the last statement in basic block BB. */
2705 last_stmt (basic_block bb
)
2707 block_stmt_iterator b
= bsi_last (bb
);
2708 return !bsi_end_p (b
) ? bsi_stmt (b
) : NULL_TREE
;
2712 /* Return a pointer to the last statement in block BB. */
2715 last_stmt_ptr (basic_block bb
)
2717 block_stmt_iterator last
= bsi_last (bb
);
2718 return !bsi_end_p (last
) ? bsi_stmt_ptr (last
) : NULL
;
2722 /* Return the last statement of an otherwise empty block. Return NULL
2723 if the block is totally empty, or if it contains more than one
2727 last_and_only_stmt (basic_block bb
)
2729 block_stmt_iterator i
= bsi_last (bb
);
2735 last
= bsi_stmt (i
);
2740 /* Empty statements should no longer appear in the instruction stream.
2741 Everything that might have appeared before should be deleted by
2742 remove_useless_stmts, and the optimizers should just bsi_remove
2743 instead of smashing with build_empty_stmt.
2745 Thus the only thing that should appear here in a block containing
2746 one executable statement is a label. */
2747 prev
= bsi_stmt (i
);
2748 if (TREE_CODE (prev
) == LABEL_EXPR
)
2755 /* Mark BB as the basic block holding statement T. */
2758 set_bb_for_stmt (tree t
, basic_block bb
)
2760 if (TREE_CODE (t
) == PHI_NODE
)
2762 else if (TREE_CODE (t
) == STATEMENT_LIST
)
2764 tree_stmt_iterator i
;
2765 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2766 set_bb_for_stmt (tsi_stmt (i
), bb
);
2770 stmt_ann_t ann
= get_stmt_ann (t
);
2773 /* If the statement is a label, add the label to block-to-labels map
2774 so that we can speed up edge creation for GOTO_EXPRs. */
2775 if (TREE_CODE (t
) == LABEL_EXPR
)
2779 t
= LABEL_EXPR_LABEL (t
);
2780 uid
= LABEL_DECL_UID (t
);
2783 LABEL_DECL_UID (t
) = uid
= cfun
->last_label_uid
++;
2784 if (VARRAY_SIZE (label_to_block_map
) <= (unsigned) uid
)
2785 VARRAY_GROW (label_to_block_map
, 3 * uid
/ 2);
2788 /* We're moving an existing label. Make sure that we've
2789 removed it from the old block. */
2790 gcc_assert (!bb
|| !VARRAY_BB (label_to_block_map
, uid
));
2791 VARRAY_BB (label_to_block_map
, uid
) = bb
;
2796 /* Finds iterator for STMT. */
2798 extern block_stmt_iterator
2799 bsi_for_stmt (tree stmt
)
2801 block_stmt_iterator bsi
;
2803 for (bsi
= bsi_start (bb_for_stmt (stmt
)); !bsi_end_p (bsi
); bsi_next (&bsi
))
2804 if (bsi_stmt (bsi
) == stmt
)
2810 /* Insert statement (or statement list) T before the statement
2811 pointed-to by iterator I. M specifies how to update iterator I
2812 after insertion (see enum bsi_iterator_update). */
2815 bsi_insert_before (block_stmt_iterator
*i
, tree t
, enum bsi_iterator_update m
)
2817 set_bb_for_stmt (t
, i
->bb
);
2818 tsi_link_before (&i
->tsi
, t
, m
);
2823 /* Insert statement (or statement list) T after the statement
2824 pointed-to by iterator I. M specifies how to update iterator I
2825 after insertion (see enum bsi_iterator_update). */
2828 bsi_insert_after (block_stmt_iterator
*i
, tree t
, enum bsi_iterator_update m
)
2830 set_bb_for_stmt (t
, i
->bb
);
2831 tsi_link_after (&i
->tsi
, t
, m
);
2836 /* Remove the statement pointed to by iterator I. The iterator is updated
2837 to the next statement. */
2840 bsi_remove (block_stmt_iterator
*i
)
2842 tree t
= bsi_stmt (*i
);
2843 set_bb_for_stmt (t
, NULL
);
2844 tsi_delink (&i
->tsi
);
2848 /* Move the statement at FROM so it comes right after the statement at TO. */
2851 bsi_move_after (block_stmt_iterator
*from
, block_stmt_iterator
*to
)
2853 tree stmt
= bsi_stmt (*from
);
2855 bsi_insert_after (to
, stmt
, BSI_SAME_STMT
);
2859 /* Move the statement at FROM so it comes right before the statement at TO. */
2862 bsi_move_before (block_stmt_iterator
*from
, block_stmt_iterator
*to
)
2864 tree stmt
= bsi_stmt (*from
);
2866 bsi_insert_before (to
, stmt
, BSI_SAME_STMT
);
2870 /* Move the statement at FROM to the end of basic block BB. */
2873 bsi_move_to_bb_end (block_stmt_iterator
*from
, basic_block bb
)
2875 block_stmt_iterator last
= bsi_last (bb
);
2877 /* Have to check bsi_end_p because it could be an empty block. */
2878 if (!bsi_end_p (last
) && is_ctrl_stmt (bsi_stmt (last
)))
2879 bsi_move_before (from
, &last
);
2881 bsi_move_after (from
, &last
);
2885 /* Replace the contents of the statement pointed to by iterator BSI
2886 with STMT. If PRESERVE_EH_INFO is true, the exception handling
2887 information of the original statement is preserved. */
2890 bsi_replace (const block_stmt_iterator
*bsi
, tree stmt
, bool preserve_eh_info
)
2893 tree orig_stmt
= bsi_stmt (*bsi
);
2895 SET_EXPR_LOCUS (stmt
, EXPR_LOCUS (orig_stmt
));
2896 set_bb_for_stmt (stmt
, bsi
->bb
);
2898 /* Preserve EH region information from the original statement, if
2899 requested by the caller. */
2900 if (preserve_eh_info
)
2902 eh_region
= lookup_stmt_eh_region (orig_stmt
);
2904 add_stmt_to_eh_region (stmt
, eh_region
);
2907 *bsi_stmt_ptr (*bsi
) = stmt
;
2912 /* Insert the statement pointed-to by BSI into edge E. Every attempt
2913 is made to place the statement in an existing basic block, but
2914 sometimes that isn't possible. When it isn't possible, the edge is
2915 split and the statement is added to the new block.
2917 In all cases, the returned *BSI points to the correct location. The
2918 return value is true if insertion should be done after the location,
2919 or false if it should be done before the location. If new basic block
2920 has to be created, it is stored in *NEW_BB. */
2923 tree_find_edge_insert_loc (edge e
, block_stmt_iterator
*bsi
,
2924 basic_block
*new_bb
)
2926 basic_block dest
, src
;
2932 /* If the destination has one predecessor which has no PHI nodes,
2933 insert there. Except for the exit block.
2935 The requirement for no PHI nodes could be relaxed. Basically we
2936 would have to examine the PHIs to prove that none of them used
2937 the value set by the statement we want to insert on E. That
2938 hardly seems worth the effort. */
2939 if (EDGE_COUNT (dest
->preds
) == 1
2940 && ! phi_nodes (dest
)
2941 && dest
!= EXIT_BLOCK_PTR
)
2943 *bsi
= bsi_start (dest
);
2944 if (bsi_end_p (*bsi
))
2947 /* Make sure we insert after any leading labels. */
2948 tmp
= bsi_stmt (*bsi
);
2949 while (TREE_CODE (tmp
) == LABEL_EXPR
)
2952 if (bsi_end_p (*bsi
))
2954 tmp
= bsi_stmt (*bsi
);
2957 if (bsi_end_p (*bsi
))
2959 *bsi
= bsi_last (dest
);
2966 /* If the source has one successor, the edge is not abnormal and
2967 the last statement does not end a basic block, insert there.
2968 Except for the entry block. */
2970 if ((e
->flags
& EDGE_ABNORMAL
) == 0
2971 && EDGE_COUNT (src
->succs
) == 1
2972 && src
!= ENTRY_BLOCK_PTR
)
2974 *bsi
= bsi_last (src
);
2975 if (bsi_end_p (*bsi
))
2978 tmp
= bsi_stmt (*bsi
);
2979 if (!stmt_ends_bb_p (tmp
))
2982 /* Insert code just before returning the value. We may need to decompose
2983 the return in the case it contains non-trivial operand. */
2984 if (TREE_CODE (tmp
) == RETURN_EXPR
)
2986 tree op
= TREE_OPERAND (tmp
, 0);
2987 if (!is_gimple_val (op
))
2989 gcc_assert (TREE_CODE (op
) == MODIFY_EXPR
);
2990 bsi_insert_before (bsi
, op
, BSI_NEW_STMT
);
2991 TREE_OPERAND (tmp
, 0) = TREE_OPERAND (op
, 0);
2998 /* Otherwise, create a new basic block, and split this edge. */
2999 dest
= split_edge (e
);
3002 e
= EDGE_PRED (dest
, 0);
3007 /* This routine will commit all pending edge insertions, creating any new
3008 basic blocks which are necessary. */
3011 bsi_commit_edge_inserts (void)
3017 bsi_commit_one_edge_insert (EDGE_SUCC (ENTRY_BLOCK_PTR
, 0), NULL
);
3020 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3021 bsi_commit_one_edge_insert (e
, NULL
);
3025 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
3026 to this block, otherwise set it to NULL. */
3029 bsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
3033 if (PENDING_STMT (e
))
3035 block_stmt_iterator bsi
;
3036 tree stmt
= PENDING_STMT (e
);
3038 PENDING_STMT (e
) = NULL_TREE
;
3040 if (tree_find_edge_insert_loc (e
, &bsi
, new_bb
))
3041 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3043 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3048 /* Add STMT to the pending list of edge E. No actual insertion is
3049 made until a call to bsi_commit_edge_inserts () is made. */
3052 bsi_insert_on_edge (edge e
, tree stmt
)
3054 append_to_statement_list (stmt
, &PENDING_STMT (e
));
3057 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If new block has to
3058 be created, it is returned. */
3061 bsi_insert_on_edge_immediate (edge e
, tree stmt
)
3063 block_stmt_iterator bsi
;
3064 basic_block new_bb
= NULL
;
3066 gcc_assert (!PENDING_STMT (e
));
3068 if (tree_find_edge_insert_loc (e
, &bsi
, &new_bb
))
3069 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3071 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3076 /*---------------------------------------------------------------------------
3077 Tree specific functions for CFG manipulation
3078 ---------------------------------------------------------------------------*/
3080 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3081 Abort on abnormal edges. */
3084 tree_split_edge (edge edge_in
)
3086 basic_block new_bb
, after_bb
, dest
, src
;
3092 /* Abnormal edges cannot be split. */
3093 gcc_assert (!(edge_in
->flags
& EDGE_ABNORMAL
));
3096 dest
= edge_in
->dest
;
3098 /* Place the new block in the block list. Try to keep the new block
3099 near its "logical" location. This is of most help to humans looking
3100 at debugging dumps. */
3101 FOR_EACH_EDGE (e
, ei
, dest
->preds
)
3102 if (e
->src
->next_bb
== dest
)
3105 after_bb
= dest
->prev_bb
;
3107 after_bb
= edge_in
->src
;
3109 new_bb
= create_empty_bb (after_bb
);
3110 new_bb
->frequency
= EDGE_FREQUENCY (edge_in
);
3111 new_bb
->count
= edge_in
->count
;
3112 new_edge
= make_edge (new_bb
, dest
, EDGE_FALLTHRU
);
3113 new_edge
->probability
= REG_BR_PROB_BASE
;
3114 new_edge
->count
= edge_in
->count
;
3116 /* Find all the PHI arguments on the original edge, and change them to
3117 the new edge. Do it before redirection, so that the argument does not
3119 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
3121 num_elem
= PHI_NUM_ARGS (phi
);
3122 for (i
= 0; i
< num_elem
; i
++)
3123 if (PHI_ARG_EDGE (phi
, i
) == edge_in
)
3125 PHI_ARG_EDGE (phi
, i
) = new_edge
;
3130 e
= redirect_edge_and_branch (edge_in
, new_bb
);
3132 gcc_assert (!PENDING_STMT (edge_in
));
3138 /* Return true when BB has label LABEL in it. */
3141 has_label_p (basic_block bb
, tree label
)
3143 block_stmt_iterator bsi
;
3145 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3147 tree stmt
= bsi_stmt (bsi
);
3149 if (TREE_CODE (stmt
) != LABEL_EXPR
)
3151 if (LABEL_EXPR_LABEL (stmt
) == label
)
3158 /* Callback for walk_tree, check that all elements with address taken are
3159 properly noticed as such. */
3162 verify_expr (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
3169 /* Check operand N for being valid GIMPLE and give error MSG if not.
3170 We check for constants explicitly since they are not considered
3171 gimple invariants if they overflowed. */
3172 #define CHECK_OP(N, MSG) \
3173 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3174 && !is_gimple_val (TREE_OPERAND (t, N))) \
3175 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3177 switch (TREE_CODE (t
))
3180 if (SSA_NAME_IN_FREE_LIST (t
))
3182 error ("SSA name in freelist but still referenced");
3188 x
= TREE_OPERAND (t
, 0);
3189 if (TREE_CODE (x
) == BIT_FIELD_REF
3190 && is_gimple_reg (TREE_OPERAND (x
, 0)))
3192 error ("GIMPLE register modified with BIT_FIELD_REF");
3198 /* Skip any references (they will be checked when we recurse down the
3199 tree) and ensure that any variable used as a prefix is marked
3201 for (x
= TREE_OPERAND (t
, 0);
3202 (handled_component_p (x
)
3203 || TREE_CODE (x
) == REALPART_EXPR
3204 || TREE_CODE (x
) == IMAGPART_EXPR
);
3205 x
= TREE_OPERAND (x
, 0))
3208 if (TREE_CODE (x
) != VAR_DECL
&& TREE_CODE (x
) != PARM_DECL
)
3210 if (!TREE_ADDRESSABLE (x
))
3212 error ("address taken, but ADDRESSABLE bit not set");
3218 x
= COND_EXPR_COND (t
);
3219 if (TREE_CODE (TREE_TYPE (x
)) != BOOLEAN_TYPE
)
3221 error ("non-boolean used in condition");
3228 case FIX_TRUNC_EXPR
:
3230 case FIX_FLOOR_EXPR
:
3231 case FIX_ROUND_EXPR
:
3236 case NON_LVALUE_EXPR
:
3237 case TRUTH_NOT_EXPR
:
3238 CHECK_OP (0, "Invalid operand to unary operator");
3245 case ARRAY_RANGE_REF
:
3247 case VIEW_CONVERT_EXPR
:
3248 /* We have a nest of references. Verify that each of the operands
3249 that determine where to reference is either a constant or a variable,
3250 verify that the base is valid, and then show we've already checked
3252 while (TREE_CODE (t
) == REALPART_EXPR
|| TREE_CODE (t
) == IMAGPART_EXPR
3253 || handled_component_p (t
))
3255 if (TREE_CODE (t
) == COMPONENT_REF
&& TREE_OPERAND (t
, 2))
3256 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3257 else if (TREE_CODE (t
) == ARRAY_REF
3258 || TREE_CODE (t
) == ARRAY_RANGE_REF
)
3260 CHECK_OP (1, "Invalid array index.");
3261 if (TREE_OPERAND (t
, 2))
3262 CHECK_OP (2, "Invalid array lower bound.");
3263 if (TREE_OPERAND (t
, 3))
3264 CHECK_OP (3, "Invalid array stride.");
3266 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
3268 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3269 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3272 t
= TREE_OPERAND (t
, 0);
3275 if (!CONSTANT_CLASS_P (t
) && !is_gimple_lvalue (t
))
3277 error ("Invalid reference prefix.");
3289 case UNORDERED_EXPR
:
3300 case TRUNC_DIV_EXPR
:
3302 case FLOOR_DIV_EXPR
:
3303 case ROUND_DIV_EXPR
:
3304 case TRUNC_MOD_EXPR
:
3306 case FLOOR_MOD_EXPR
:
3307 case ROUND_MOD_EXPR
:
3309 case EXACT_DIV_EXPR
:
3319 CHECK_OP (0, "Invalid operand to binary operator");
3320 CHECK_OP (1, "Invalid operand to binary operator");
3332 /* Verify STMT, return true if STMT is not in GIMPLE form.
3333 TODO: Implement type checking. */
3336 verify_stmt (tree stmt
, bool last_in_block
)
3340 if (!is_gimple_stmt (stmt
))
3342 error ("Is not a valid GIMPLE statement.");
3346 addr
= walk_tree (&stmt
, verify_expr
, NULL
, NULL
);
3349 debug_generic_stmt (addr
);
3353 /* If the statement is marked as part of an EH region, then it is
3354 expected that the statement could throw. Verify that when we
3355 have optimizations that simplify statements such that we prove
3356 that they cannot throw, that we update other data structures
3358 if (lookup_stmt_eh_region (stmt
) >= 0)
3360 if (!tree_could_throw_p (stmt
))
3362 error ("Statement marked for throw, but doesn%'t.");
3365 if (!last_in_block
&& tree_can_throw_internal (stmt
))
3367 error ("Statement marked for throw in middle of block.");
3375 debug_generic_stmt (stmt
);
3380 /* Return true when the T can be shared. */
3383 tree_node_can_be_shared (tree t
)
3385 if (IS_TYPE_OR_DECL_P (t
)
3386 /* We check for constants explicitly since they are not considered
3387 gimple invariants if they overflowed. */
3388 || CONSTANT_CLASS_P (t
)
3389 || is_gimple_min_invariant (t
)
3390 || TREE_CODE (t
) == SSA_NAME
)
3393 if (TREE_CODE (t
) == CASE_LABEL_EXPR
)
3396 while (((TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
3397 /* We check for constants explicitly since they are not considered
3398 gimple invariants if they overflowed. */
3399 && (CONSTANT_CLASS_P (TREE_OPERAND (t
, 1))
3400 || is_gimple_min_invariant (TREE_OPERAND (t
, 1))))
3401 || (TREE_CODE (t
) == COMPONENT_REF
3402 || TREE_CODE (t
) == REALPART_EXPR
3403 || TREE_CODE (t
) == IMAGPART_EXPR
))
3404 t
= TREE_OPERAND (t
, 0);
3413 /* Called via walk_trees. Verify tree sharing. */
3416 verify_node_sharing (tree
* tp
, int *walk_subtrees
, void *data
)
3418 htab_t htab
= (htab_t
) data
;
3421 if (tree_node_can_be_shared (*tp
))
3423 *walk_subtrees
= false;
3427 slot
= htab_find_slot (htab
, *tp
, INSERT
);
3436 /* Verify the GIMPLE statement chain. */
3442 block_stmt_iterator bsi
;
3447 timevar_push (TV_TREE_STMT_VERIFY
);
3448 htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
3455 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
3457 int phi_num_args
= PHI_NUM_ARGS (phi
);
3459 for (i
= 0; i
< phi_num_args
; i
++)
3461 tree t
= PHI_ARG_DEF (phi
, i
);
3464 /* Addressable variables do have SSA_NAMEs but they
3465 are not considered gimple values. */
3466 if (TREE_CODE (t
) != SSA_NAME
3467 && TREE_CODE (t
) != FUNCTION_DECL
3468 && !is_gimple_val (t
))
3470 error ("PHI def is not a GIMPLE value");
3471 debug_generic_stmt (phi
);
3472 debug_generic_stmt (t
);
3476 addr
= walk_tree (&t
, verify_expr
, NULL
, NULL
);
3479 debug_generic_stmt (addr
);
3483 addr
= walk_tree (&t
, verify_node_sharing
, htab
, NULL
);
3486 error ("Incorrect sharing of tree nodes");
3487 debug_generic_stmt (phi
);
3488 debug_generic_stmt (addr
);
3494 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
3496 tree stmt
= bsi_stmt (bsi
);
3498 err
|= verify_stmt (stmt
, bsi_end_p (bsi
));
3499 addr
= walk_tree (&stmt
, verify_node_sharing
, htab
, NULL
);
3502 error ("Incorrect sharing of tree nodes");
3503 debug_generic_stmt (stmt
);
3504 debug_generic_stmt (addr
);
3511 internal_error ("verify_stmts failed.");
3514 timevar_pop (TV_TREE_STMT_VERIFY
);
3518 /* Verifies that the flow information is OK. */
3521 tree_verify_flow_info (void)
3525 block_stmt_iterator bsi
;
3530 if (ENTRY_BLOCK_PTR
->stmt_list
)
3532 error ("ENTRY_BLOCK has a statement list associated with it\n");
3536 if (EXIT_BLOCK_PTR
->stmt_list
)
3538 error ("EXIT_BLOCK has a statement list associated with it\n");
3542 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
3543 if (e
->flags
& EDGE_FALLTHRU
)
3545 error ("Fallthru to exit from bb %d\n", e
->src
->index
);
3551 bool found_ctrl_stmt
= false;
3553 /* Skip labels on the start of basic block. */
3554 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3556 if (TREE_CODE (bsi_stmt (bsi
)) != LABEL_EXPR
)
3559 if (label_to_block (LABEL_EXPR_LABEL (bsi_stmt (bsi
))) != bb
)
3561 tree stmt
= bsi_stmt (bsi
);
3562 error ("Label %s to block does not match in bb %d\n",
3563 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt
))),
3568 if (decl_function_context (LABEL_EXPR_LABEL (bsi_stmt (bsi
)))
3569 != current_function_decl
)
3571 tree stmt
= bsi_stmt (bsi
);
3572 error ("Label %s has incorrect context in bb %d\n",
3573 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt
))),
3579 /* Verify that body of basic block BB is free of control flow. */
3580 for (; !bsi_end_p (bsi
); bsi_next (&bsi
))
3582 tree stmt
= bsi_stmt (bsi
);
3584 if (found_ctrl_stmt
)
3586 error ("Control flow in the middle of basic block %d\n",
3591 if (stmt_ends_bb_p (stmt
))
3592 found_ctrl_stmt
= true;
3594 if (TREE_CODE (stmt
) == LABEL_EXPR
)
3596 error ("Label %s in the middle of basic block %d\n",
3597 IDENTIFIER_POINTER (DECL_NAME (stmt
)),
3602 bsi
= bsi_last (bb
);
3603 if (bsi_end_p (bsi
))
3606 stmt
= bsi_stmt (bsi
);
3608 if (is_ctrl_stmt (stmt
))
3610 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3611 if (e
->flags
& EDGE_FALLTHRU
)
3613 error ("Fallthru edge after a control statement in bb %d \n",
3619 switch (TREE_CODE (stmt
))
3625 if (TREE_CODE (COND_EXPR_THEN (stmt
)) != GOTO_EXPR
3626 || TREE_CODE (COND_EXPR_ELSE (stmt
)) != GOTO_EXPR
)
3628 error ("Structured COND_EXPR at the end of bb %d\n", bb
->index
);
3632 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
3634 if (!true_edge
|| !false_edge
3635 || !(true_edge
->flags
& EDGE_TRUE_VALUE
)
3636 || !(false_edge
->flags
& EDGE_FALSE_VALUE
)
3637 || (true_edge
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
))
3638 || (false_edge
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
))
3639 || EDGE_COUNT (bb
->succs
) >= 3)
3641 error ("Wrong outgoing edge flags at end of bb %d\n",
3646 if (!has_label_p (true_edge
->dest
,
3647 GOTO_DESTINATION (COND_EXPR_THEN (stmt
))))
3649 error ("%<then%> label does not match edge at end of bb %d\n",
3654 if (!has_label_p (false_edge
->dest
,
3655 GOTO_DESTINATION (COND_EXPR_ELSE (stmt
))))
3657 error ("%<else%> label does not match edge at end of bb %d\n",
3665 if (simple_goto_p (stmt
))
3667 error ("Explicit goto at end of bb %d\n", bb
->index
);
3672 /* FIXME. We should double check that the labels in the
3673 destination blocks have their address taken. */
3674 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3675 if ((e
->flags
& (EDGE_FALLTHRU
| EDGE_TRUE_VALUE
3676 | EDGE_FALSE_VALUE
))
3677 || !(e
->flags
& EDGE_ABNORMAL
))
3679 error ("Wrong outgoing edge flags at end of bb %d\n",
3687 if (EDGE_COUNT (bb
->succs
) != 1
3688 || (EDGE_SUCC (bb
, 0)->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
3689 | EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
3691 error ("Wrong outgoing edge flags at end of bb %d\n", bb
->index
);
3694 if (EDGE_SUCC (bb
, 0)->dest
!= EXIT_BLOCK_PTR
)
3696 error ("Return edge does not point to exit in bb %d\n",
3709 vec
= SWITCH_LABELS (stmt
);
3710 n
= TREE_VEC_LENGTH (vec
);
3712 /* Mark all the destination basic blocks. */
3713 for (i
= 0; i
< n
; ++i
)
3715 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
3716 basic_block label_bb
= label_to_block (lab
);
3718 gcc_assert (!label_bb
->aux
|| label_bb
->aux
== (void *)1);
3719 label_bb
->aux
= (void *)1;
3722 /* Verify that the case labels are sorted. */
3723 prev
= TREE_VEC_ELT (vec
, 0);
3724 for (i
= 1; i
< n
- 1; ++i
)
3726 tree c
= TREE_VEC_ELT (vec
, i
);
3729 error ("Found default case not at end of case vector");
3733 if (! tree_int_cst_lt (CASE_LOW (prev
), CASE_LOW (c
)))
3735 error ("Case labels not sorted:\n ");
3736 print_generic_expr (stderr
, prev
, 0);
3737 fprintf (stderr
," is greater than ");
3738 print_generic_expr (stderr
, c
, 0);
3739 fprintf (stderr
," but comes before it.\n");
3744 if (CASE_LOW (TREE_VEC_ELT (vec
, n
- 1)))
3746 error ("No default case found at end of case vector");
3750 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3754 error ("Extra outgoing edge %d->%d\n",
3755 bb
->index
, e
->dest
->index
);
3758 e
->dest
->aux
= (void *)2;
3759 if ((e
->flags
& (EDGE_FALLTHRU
| EDGE_ABNORMAL
3760 | EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
3762 error ("Wrong outgoing edge flags at end of bb %d\n",
3768 /* Check that we have all of them. */
3769 for (i
= 0; i
< n
; ++i
)
3771 tree lab
= CASE_LABEL (TREE_VEC_ELT (vec
, i
));
3772 basic_block label_bb
= label_to_block (lab
);
3774 if (label_bb
->aux
!= (void *)2)
3776 error ("Missing edge %i->%i\n",
3777 bb
->index
, label_bb
->index
);
3782 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3783 e
->dest
->aux
= (void *)0;
3790 if (dom_computed
[CDI_DOMINATORS
] >= DOM_NO_FAST_QUERY
)
3791 verify_dominators (CDI_DOMINATORS
);
3797 /* Updates phi nodes after creating a forwarder block joined
3798 by edge FALLTHRU. */
3801 tree_make_forwarder_block (edge fallthru
)
3805 basic_block dummy
, bb
;
3806 tree phi
, new_phi
, var
;
3808 dummy
= fallthru
->src
;
3809 bb
= fallthru
->dest
;
3811 if (EDGE_COUNT (bb
->preds
) == 1)
3814 /* If we redirected a branch we must create new phi nodes at the
3816 for (phi
= phi_nodes (dummy
); phi
; phi
= PHI_CHAIN (phi
))
3818 var
= PHI_RESULT (phi
);
3819 new_phi
= create_phi_node (var
, bb
);
3820 SSA_NAME_DEF_STMT (var
) = new_phi
;
3821 SET_PHI_RESULT (phi
, make_ssa_name (SSA_NAME_VAR (var
), phi
));
3822 add_phi_arg (&new_phi
, PHI_RESULT (phi
), fallthru
);
3825 /* Ensure that the PHI node chain is in the same order. */
3826 set_phi_nodes (bb
, phi_reverse (phi_nodes (bb
)));
3828 /* Add the arguments we have stored on edges. */
3829 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3834 flush_pending_stmts (e
);
3839 /* Return true if basic block BB does nothing except pass control
3840 flow to another block and that we can safely insert a label at
3841 the start of the successor block.
3843 As a precondition, we require that BB be not equal to
3847 tree_forwarder_block_p (basic_block bb
)
3849 block_stmt_iterator bsi
;
3853 /* BB must have a single outgoing edge. */
3854 if (EDGE_COUNT (bb
->succs
) != 1
3855 /* BB can not have any PHI nodes. This could potentially be
3856 relaxed early in compilation if we re-rewrote the variables
3857 appearing in any PHI nodes in forwarder blocks. */
3859 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
3860 || EDGE_SUCC (bb
, 0)->dest
== EXIT_BLOCK_PTR
3861 /* BB may not have an abnormal outgoing edge. */
3862 || (EDGE_SUCC (bb
, 0)->flags
& EDGE_ABNORMAL
))
3866 gcc_assert (bb
!= ENTRY_BLOCK_PTR
);
3869 /* Successors of the entry block are not forwarders. */
3870 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
3874 /* Now walk through the statements. We can ignore labels, anything else
3875 means this is not a forwarder block. */
3876 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3878 tree stmt
= bsi_stmt (bsi
);
3880 switch (TREE_CODE (stmt
))
3883 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt
)))
3895 /* Thread jumps from BB. */
3898 thread_jumps_from_bb (basic_block bb
)
3902 bool retval
= false;
3904 /* Examine each of our block's successors to see if it is
3906 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
3911 basic_block dest
, tmp
, curr
, old_dest
;
3915 /* If the edge is abnormal or its destination is not
3916 forwardable, then there's nothing to do. */
3917 if ((e
->flags
& EDGE_ABNORMAL
)
3918 || !bb_ann (e
->dest
)->forwardable
)
3924 /* Now walk through as many forwarder blocks as possible to find
3925 the ultimate destination we want to thread our jump to. */
3926 last
= EDGE_SUCC (e
->dest
, 0);
3927 bb_ann (e
->dest
)->forwardable
= 0;
3928 for (dest
= EDGE_SUCC (e
->dest
, 0)->dest
;
3929 bb_ann (dest
)->forwardable
;
3930 last
= EDGE_SUCC (dest
, 0),
3931 dest
= EDGE_SUCC (dest
, 0)->dest
)
3932 bb_ann (dest
)->forwardable
= 0;
3934 /* Reset the forwardable marks to 1. */
3937 tmp
= EDGE_SUCC (tmp
, 0)->dest
)
3938 bb_ann (tmp
)->forwardable
= 1;
3940 if (dest
== e
->dest
)
3946 old
= find_edge (bb
, dest
);
3949 /* If there already is an edge, check whether the values in
3950 phi nodes differ. */
3951 if (!phi_alternatives_equal (dest
, last
, old
))
3953 /* The previous block is forwarder. Redirect our jump
3954 to that target instead since we know it has no PHI
3955 nodes that will need updating. */
3958 /* That might mean that no forwarding at all is
3960 if (dest
== e
->dest
)
3966 old
= find_edge (bb
, dest
);
3970 /* Perform the redirection. */
3973 freq
= EDGE_FREQUENCY (e
);
3975 e
= redirect_edge_and_branch (e
, dest
);
3977 /* Update the profile. */
3978 if (profile_status
!= PROFILE_ABSENT
)
3979 for (curr
= old_dest
;
3981 curr
= EDGE_SUCC (curr
, 0)->dest
)
3983 curr
->frequency
-= freq
;
3984 if (curr
->frequency
< 0)
3985 curr
->frequency
= 0;
3986 curr
->count
-= count
;
3987 if (curr
->count
< 0)
3989 EDGE_SUCC (curr
, 0)->count
-= count
;
3990 if (EDGE_SUCC (curr
, 0)->count
< 0)
3991 EDGE_SUCC (curr
, 0)->count
= 0;
3996 /* Update PHI nodes. We know that the new argument should
3997 have the same value as the argument associated with LAST.
3998 Otherwise we would have changed our target block
4000 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
4002 arg
= phi_arg_from_edge (phi
, last
);
4003 gcc_assert (arg
>= 0);
4004 add_phi_arg (&phi
, PHI_ARG_DEF (phi
, arg
), e
);
4008 /* Remove the unreachable blocks (observe that if all blocks
4009 were reachable before, only those in the path we threaded
4010 over and did not have any predecessor outside of the path
4011 become unreachable). */
4012 for (; old_dest
!= dest
; old_dest
= tmp
)
4014 tmp
= EDGE_SUCC (old_dest
, 0)->dest
;
4016 if (EDGE_COUNT (old_dest
->preds
) > 0)
4019 delete_basic_block (old_dest
);
4022 /* Update the dominators. */
4023 if (dom_info_available_p (CDI_DOMINATORS
))
4025 /* If the dominator of the destination was in the
4026 path, set its dominator to the start of the
4028 if (get_immediate_dominator (CDI_DOMINATORS
, old_dest
) == NULL
)
4029 set_immediate_dominator (CDI_DOMINATORS
, old_dest
, bb
);
4031 /* Now proceed like if we forwarded just over one edge at a
4032 time. Algorithm for forwarding edge S --> A over
4033 edge A --> B then is
4036 && !dominated_by (S, B))
4037 idom (B) = idom (A);
4038 recount_idom (A); */
4040 for (; old_dest
!= dest
; old_dest
= tmp
)
4044 tmp
= EDGE_SUCC (old_dest
, 0)->dest
;
4046 if (get_immediate_dominator (CDI_DOMINATORS
, tmp
) == old_dest
4047 && !dominated_by_p (CDI_DOMINATORS
, bb
, tmp
))
4049 dom
= get_immediate_dominator (CDI_DOMINATORS
, old_dest
);
4050 set_immediate_dominator (CDI_DOMINATORS
, tmp
, dom
);
4053 dom
= recount_dominator (CDI_DOMINATORS
, old_dest
);
4054 set_immediate_dominator (CDI_DOMINATORS
, old_dest
, dom
);
4063 /* Thread jumps over empty statements.
4065 This code should _not_ thread over obviously equivalent conditions
4066 as that requires nontrivial updates to the SSA graph.
4068 As a precondition, we require that all basic blocks be reachable.
4069 That is, there should be no opportunities left for
4070 delete_unreachable_blocks. */
4076 bool retval
= false;
4077 basic_block
*worklist
= xmalloc (sizeof (basic_block
) * last_basic_block
);
4078 basic_block
*current
= worklist
;
4082 bb_ann (bb
)->forwardable
= tree_forwarder_block_p (bb
);
4083 bb
->flags
&= ~BB_VISITED
;
4086 /* We pretend to have ENTRY_BLOCK_PTR in WORKLIST. This way,
4087 ENTRY_BLOCK_PTR will never be entered into WORKLIST. */
4088 ENTRY_BLOCK_PTR
->flags
|= BB_VISITED
;
4090 /* Initialize WORKLIST by putting non-forwarder blocks that
4091 immediately precede forwarder blocks because those are the ones
4092 that we know we can thread jumps from. We use BB_VISITED to
4093 indicate whether a given basic block is in WORKLIST or not,
4094 thereby avoiding duplicates in WORKLIST. */
4100 /* We are not interested in finding non-forwarder blocks
4101 directly. We want to find non-forwarder blocks as
4102 predecessors of a forwarder block. */
4103 if (!bb_ann (bb
)->forwardable
)
4106 /* Now we know BB is a forwarder block. Visit each of its
4107 incoming edges and add to WORKLIST all non-forwarder blocks
4108 among BB's predecessors. */
4109 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4111 /* We don't want to put a duplicate into WORKLIST. */
4112 if ((e
->src
->flags
& BB_VISITED
) == 0
4113 /* We are not interested in threading jumps from a forwarder
4115 && !bb_ann (e
->src
)->forwardable
)
4117 e
->src
->flags
|= BB_VISITED
;
4118 *current
++ = e
->src
;
4123 /* Now let's drain WORKLIST. */
4124 while (worklist
!= current
)
4128 /* BB is no longer in WORKLIST, so clear BB_VISITED. */
4129 bb
->flags
&= ~BB_VISITED
;
4131 if (thread_jumps_from_bb (bb
))
4135 if (tree_forwarder_block_p (bb
))
4140 bb_ann (bb
)->forwardable
= true;
4142 /* Attempts to thread through BB may have been blocked
4143 because BB was not a forwarder block before. Now
4144 that BB is a forwarder block, we should revisit BB's
4146 FOR_EACH_EDGE (f
, ej
, bb
->preds
)
4148 /* We don't want to put a duplicate into WORKLIST. */
4149 if ((f
->src
->flags
& BB_VISITED
) == 0
4150 /* We are not interested in threading jumps from a
4152 && !bb_ann (f
->src
)->forwardable
)
4154 f
->src
->flags
|= BB_VISITED
;
4155 *current
++ = f
->src
;
4162 ENTRY_BLOCK_PTR
->flags
&= ~BB_VISITED
;
4170 /* Return a non-special label in the head of basic block BLOCK.
4171 Create one if it doesn't exist. */
4174 tree_block_label (basic_block bb
)
4176 block_stmt_iterator i
, s
= bsi_start (bb
);
4180 for (i
= s
; !bsi_end_p (i
); first
= false, bsi_next (&i
))
4182 stmt
= bsi_stmt (i
);
4183 if (TREE_CODE (stmt
) != LABEL_EXPR
)
4185 label
= LABEL_EXPR_LABEL (stmt
);
4186 if (!DECL_NONLOCAL (label
))
4189 bsi_move_before (&i
, &s
);
4194 label
= create_artificial_label ();
4195 stmt
= build1 (LABEL_EXPR
, void_type_node
, label
);
4196 bsi_insert_before (&s
, stmt
, BSI_NEW_STMT
);
4201 /* Attempt to perform edge redirection by replacing a possibly complex
4202 jump instruction by a goto or by removing the jump completely.
4203 This can apply only if all edges now point to the same block. The
4204 parameters and return values are equivalent to
4205 redirect_edge_and_branch. */
4208 tree_try_redirect_by_replacing_jump (edge e
, basic_block target
)
4210 basic_block src
= e
->src
;
4212 block_stmt_iterator b
;
4216 /* Verify that all targets will be TARGET. */
4217 FOR_EACH_EDGE (tmp
, ei
, src
->succs
)
4218 if (tmp
->dest
!= target
&& tmp
!= e
)
4227 stmt
= bsi_stmt (b
);
4229 if (TREE_CODE (stmt
) == COND_EXPR
4230 || TREE_CODE (stmt
) == SWITCH_EXPR
)
4233 e
= ssa_redirect_edge (e
, target
);
4234 e
->flags
= EDGE_FALLTHRU
;
4242 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4243 edge representing the redirected branch. */
4246 tree_redirect_edge_and_branch (edge e
, basic_block dest
)
4248 basic_block bb
= e
->src
;
4249 block_stmt_iterator bsi
;
4253 if (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
))
4256 if (e
->src
!= ENTRY_BLOCK_PTR
4257 && (ret
= tree_try_redirect_by_replacing_jump (e
, dest
)))
4260 if (e
->dest
== dest
)
4263 label
= tree_block_label (dest
);
4265 bsi
= bsi_last (bb
);
4266 stmt
= bsi_end_p (bsi
) ? NULL
: bsi_stmt (bsi
);
4268 switch (stmt
? TREE_CODE (stmt
) : ERROR_MARK
)
4271 stmt
= (e
->flags
& EDGE_TRUE_VALUE
4272 ? COND_EXPR_THEN (stmt
)
4273 : COND_EXPR_ELSE (stmt
));
4274 GOTO_DESTINATION (stmt
) = label
;
4278 /* No non-abnormal edges should lead from a non-simple goto, and
4279 simple ones should be represented implicitly. */
4286 /* We need to update the LABEL_DECL in the switch vector to
4287 reflect the edge redirection.
4289 There is precisely one CASE_LABEL_EXPR in the switch vector
4290 which needs updating. Either its label needs to be updated
4291 or it needs to be directed to a new case leader. */
4292 e2
= find_edge (e
->src
, dest
);
4295 /* In this case we need to change the case leader for the
4296 current leader of E to be the case leader for E2. */
4297 tree e_leader
= get_case_leader_for_edge (e
);
4298 tree e2_leader
= get_case_leader_for_edge (e2
);
4299 CASE_LEADER_OR_LABEL (e_leader
) = e2_leader
;
4303 /* No edge exists from E->src to DEST, so we will simply
4304 change E->dest. The case leader does not change, but
4305 the LABEL_DECL for the leader does change. */
4306 CASE_LEADER_OR_LABEL (get_case_leader_for_edge (e
)) = label
;
4313 e
->flags
|= EDGE_FALLTHRU
;
4317 /* Otherwise it must be a fallthru edge, and we don't need to
4318 do anything besides redirecting it. */
4319 gcc_assert (e
->flags
& EDGE_FALLTHRU
);
4323 /* Update/insert PHI nodes as necessary. */
4325 /* Now update the edges in the CFG. */
4326 e
= ssa_redirect_edge (e
, dest
);
4332 /* Simple wrapper, as we can always redirect fallthru edges. */
4335 tree_redirect_edge_and_branch_force (edge e
, basic_block dest
)
4337 e
= tree_redirect_edge_and_branch (e
, dest
);
4344 /* Splits basic block BB after statement STMT (but at least after the
4345 labels). If STMT is NULL, BB is split just after the labels. */
4348 tree_split_block (basic_block bb
, void *stmt
)
4350 block_stmt_iterator bsi
, bsi_tgt
;
4356 new_bb
= create_empty_bb (bb
);
4358 /* Redirect the outgoing edges. */
4359 new_bb
->succs
= bb
->succs
;
4361 FOR_EACH_EDGE (e
, ei
, new_bb
->succs
)
4364 if (stmt
&& TREE_CODE ((tree
) stmt
) == LABEL_EXPR
)
4367 /* Move everything from BSI to the new basic block. */
4368 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4370 act
= bsi_stmt (bsi
);
4371 if (TREE_CODE (act
) == LABEL_EXPR
)
4384 bsi_tgt
= bsi_start (new_bb
);
4385 while (!bsi_end_p (bsi
))
4387 act
= bsi_stmt (bsi
);
4389 bsi_insert_after (&bsi_tgt
, act
, BSI_NEW_STMT
);
4396 /* Moves basic block BB after block AFTER. */
4399 tree_move_block_after (basic_block bb
, basic_block after
)
4401 if (bb
->prev_bb
== after
)
4405 link_block (bb
, after
);
4411 /* Return true if basic_block can be duplicated. */
4414 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED
)
4419 /* Create a duplicate of the basic block BB. NOTE: This does not
4420 preserve SSA form. */
4423 tree_duplicate_bb (basic_block bb
)
4426 block_stmt_iterator bsi
, bsi_tgt
;
4428 ssa_op_iter op_iter
;
4430 new_bb
= create_empty_bb (EXIT_BLOCK_PTR
->prev_bb
);
4432 /* First copy the phi nodes. We do not copy phi node arguments here,
4433 since the edges are not ready yet. Keep the chain of phi nodes in
4434 the same order, so that we can add them later. */
4435 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
4437 mark_for_rewrite (PHI_RESULT (phi
));
4438 create_phi_node (PHI_RESULT (phi
), new_bb
);
4440 set_phi_nodes (new_bb
, phi_reverse (phi_nodes (new_bb
)));
4442 bsi_tgt
= bsi_start (new_bb
);
4443 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4445 tree stmt
= bsi_stmt (bsi
);
4448 if (TREE_CODE (stmt
) == LABEL_EXPR
)
4451 /* Record the definitions. */
4452 get_stmt_operands (stmt
);
4454 FOR_EACH_SSA_TREE_OPERAND (val
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
4455 mark_for_rewrite (val
);
4457 copy
= unshare_expr (stmt
);
4459 /* Copy also the virtual operands. */
4460 get_stmt_ann (copy
);
4461 copy_virtual_operands (copy
, stmt
);
4463 bsi_insert_after (&bsi_tgt
, copy
, BSI_NEW_STMT
);
4469 /* Basic block BB_COPY was created by code duplication. Add phi node
4470 arguments for edges going out of BB_COPY. The blocks that were
4471 duplicated have rbi->duplicated set to one. */
4474 add_phi_args_after_copy_bb (basic_block bb_copy
)
4476 basic_block bb
, dest
;
4479 tree phi
, phi_copy
, phi_next
, def
;
4481 bb
= bb_copy
->rbi
->original
;
4483 FOR_EACH_EDGE (e_copy
, ei
, bb_copy
->succs
)
4485 if (!phi_nodes (e_copy
->dest
))
4488 if (e_copy
->dest
->rbi
->duplicated
)
4489 dest
= e_copy
->dest
->rbi
->original
;
4491 dest
= e_copy
->dest
;
4493 e
= find_edge (bb
, dest
);
4496 /* During loop unrolling the target of the latch edge is copied.
4497 In this case we are not looking for edge to dest, but to
4498 duplicated block whose original was dest. */
4499 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4500 if (e
->dest
->rbi
->duplicated
4501 && e
->dest
->rbi
->original
== dest
)
4504 gcc_assert (e
!= NULL
);
4507 for (phi
= phi_nodes (e
->dest
), phi_copy
= phi_nodes (e_copy
->dest
);
4509 phi
= phi_next
, phi_copy
= PHI_CHAIN (phi_copy
))
4511 phi_next
= PHI_CHAIN (phi
);
4513 gcc_assert (PHI_RESULT (phi
) == PHI_RESULT (phi_copy
));
4514 def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4515 add_phi_arg (&phi_copy
, def
, e_copy
);
4520 /* Blocks in REGION_COPY array of length N_REGION were created by
4521 duplication of basic blocks. Add phi node arguments for edges
4522 going from these blocks. */
4525 add_phi_args_after_copy (basic_block
*region_copy
, unsigned n_region
)
4529 for (i
= 0; i
< n_region
; i
++)
4530 region_copy
[i
]->rbi
->duplicated
= 1;
4532 for (i
= 0; i
< n_region
; i
++)
4533 add_phi_args_after_copy_bb (region_copy
[i
]);
4535 for (i
= 0; i
< n_region
; i
++)
4536 region_copy
[i
]->rbi
->duplicated
= 0;
4539 /* Maps the old ssa name FROM_NAME to TO_NAME. */
4541 struct ssa_name_map_entry
4547 /* Hash function for ssa_name_map_entry. */
4550 ssa_name_map_entry_hash (const void *entry
)
4552 const struct ssa_name_map_entry
*en
= entry
;
4553 return SSA_NAME_VERSION (en
->from_name
);
4556 /* Equality function for ssa_name_map_entry. */
4559 ssa_name_map_entry_eq (const void *in_table
, const void *ssa_name
)
4561 const struct ssa_name_map_entry
*en
= in_table
;
4563 return en
->from_name
== ssa_name
;
4566 /* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping
4570 allocate_ssa_names (bitmap definitions
, htab_t
*map
)
4573 struct ssa_name_map_entry
*entry
;
4579 *map
= htab_create (10, ssa_name_map_entry_hash
,
4580 ssa_name_map_entry_eq
, free
);
4581 EXECUTE_IF_SET_IN_BITMAP (definitions
, 0, ver
, bi
)
4583 name
= ssa_name (ver
);
4584 slot
= htab_find_slot_with_hash (*map
, name
, SSA_NAME_VERSION (name
),
4590 entry
= xmalloc (sizeof (struct ssa_name_map_entry
));
4591 entry
->from_name
= name
;
4594 entry
->to_name
= duplicate_ssa_name (name
, SSA_NAME_DEF_STMT (name
));
4598 /* Rewrite the definition DEF in statement STMT to new ssa name as specified
4599 by the mapping MAP. */
4602 rewrite_to_new_ssa_names_def (def_operand_p def
, tree stmt
, htab_t map
)
4604 tree name
= DEF_FROM_PTR (def
);
4605 struct ssa_name_map_entry
*entry
;
4607 gcc_assert (TREE_CODE (name
) == SSA_NAME
);
4609 entry
= htab_find_with_hash (map
, name
, SSA_NAME_VERSION (name
));
4613 SET_DEF (def
, entry
->to_name
);
4614 SSA_NAME_DEF_STMT (entry
->to_name
) = stmt
;
4617 /* Rewrite the USE to new ssa name as specified by the mapping MAP. */
4620 rewrite_to_new_ssa_names_use (use_operand_p use
, htab_t map
)
4622 tree name
= USE_FROM_PTR (use
);
4623 struct ssa_name_map_entry
*entry
;
4625 if (TREE_CODE (name
) != SSA_NAME
)
4628 entry
= htab_find_with_hash (map
, name
, SSA_NAME_VERSION (name
));
4632 SET_USE (use
, entry
->to_name
);
4635 /* Rewrite the ssa names in basic block BB to new ones as specified by the
4639 rewrite_to_new_ssa_names_bb (basic_block bb
, htab_t map
)
4645 block_stmt_iterator bsi
;
4649 v_may_def_optype v_may_defs
;
4650 v_must_def_optype v_must_defs
;
4653 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4654 if (e
->flags
& EDGE_ABNORMAL
)
4657 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
4659 rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi
), phi
, map
);
4661 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi
)) = 1;
4664 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4666 stmt
= bsi_stmt (bsi
);
4667 get_stmt_operands (stmt
);
4668 ann
= stmt_ann (stmt
);
4670 uses
= USE_OPS (ann
);
4671 for (i
= 0; i
< NUM_USES (uses
); i
++)
4672 rewrite_to_new_ssa_names_use (USE_OP_PTR (uses
, i
), map
);
4674 defs
= DEF_OPS (ann
);
4675 for (i
= 0; i
< NUM_DEFS (defs
); i
++)
4676 rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs
, i
), stmt
, map
);
4678 vuses
= VUSE_OPS (ann
);
4679 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
4680 rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses
, i
), map
);
4682 v_may_defs
= V_MAY_DEF_OPS (ann
);
4683 for (i
= 0; i
< NUM_V_MAY_DEFS (v_may_defs
); i
++)
4685 rewrite_to_new_ssa_names_use
4686 (V_MAY_DEF_OP_PTR (v_may_defs
, i
), map
);
4687 rewrite_to_new_ssa_names_def
4688 (V_MAY_DEF_RESULT_PTR (v_may_defs
, i
), stmt
, map
);
4691 v_must_defs
= V_MUST_DEF_OPS (ann
);
4692 for (i
= 0; i
< NUM_V_MUST_DEFS (v_must_defs
); i
++)
4694 rewrite_to_new_ssa_names_def
4695 (V_MUST_DEF_RESULT_PTR (v_must_defs
, i
), stmt
, map
);
4696 rewrite_to_new_ssa_names_use
4697 (V_MUST_DEF_KILL_PTR (v_must_defs
, i
), map
);
4701 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4702 for (phi
= phi_nodes (e
->dest
); phi
; phi
= PHI_CHAIN (phi
))
4704 rewrite_to_new_ssa_names_use
4705 (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
), map
);
4707 if (e
->flags
& EDGE_ABNORMAL
)
4709 tree op
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
4710 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
) = 1;
4715 /* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified
4716 by the mapping MAP. */
4719 rewrite_to_new_ssa_names (basic_block
*region
, unsigned n_region
, htab_t map
)
4723 for (r
= 0; r
< n_region
; r
++)
4724 rewrite_to_new_ssa_names_bb (region
[r
], map
);
4727 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4728 important exit edge EXIT. By important we mean that no SSA name defined
4729 inside region is live over the other exit edges of the region. All entry
4730 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4731 to the duplicate of the region. SSA form, dominance and loop information
4732 is updated. The new basic blocks are stored to REGION_COPY in the same
4733 order as they had in REGION, provided that REGION_COPY is not NULL.
4734 The function returns false if it is unable to copy the region,
4738 tree_duplicate_sese_region (edge entry
, edge exit
,
4739 basic_block
*region
, unsigned n_region
,
4740 basic_block
*region_copy
)
4742 unsigned i
, n_doms
, ver
;
4743 bool free_region_copy
= false, copying_header
= false;
4744 struct loop
*loop
= entry
->dest
->loop_father
;
4749 htab_t ssa_name_map
= NULL
;
4753 if (!can_copy_bbs_p (region
, n_region
))
4756 /* Some sanity checking. Note that we do not check for all possible
4757 missuses of the functions. I.e. if you ask to copy something weird,
4758 it will work, but the state of structures probably will not be
4761 for (i
= 0; i
< n_region
; i
++)
4763 /* We do not handle subloops, i.e. all the blocks must belong to the
4765 if (region
[i
]->loop_father
!= loop
)
4768 if (region
[i
] != entry
->dest
4769 && region
[i
] == loop
->header
)
4775 /* In case the function is used for loop header copying (which is the primary
4776 use), ensure that EXIT and its copy will be new latch and entry edges. */
4777 if (loop
->header
== entry
->dest
)
4779 copying_header
= true;
4780 loop
->copy
= loop
->outer
;
4782 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit
->src
))
4785 for (i
= 0; i
< n_region
; i
++)
4786 if (region
[i
] != exit
->src
4787 && dominated_by_p (CDI_DOMINATORS
, region
[i
], exit
->src
))
4793 region_copy
= xmalloc (sizeof (basic_block
) * n_region
);
4794 free_region_copy
= true;
4797 gcc_assert (!any_marked_for_rewrite_p ());
4799 /* Record blocks outside the region that are duplicated by something
4801 doms
= xmalloc (sizeof (basic_block
) * n_basic_blocks
);
4802 n_doms
= get_dominated_by_region (CDI_DOMINATORS
, region
, n_region
, doms
);
4804 copy_bbs (region
, n_region
, region_copy
, &exit
, 1, &exit_copy
, loop
);
4805 definitions
= marked_ssa_names ();
4809 loop
->header
= exit
->dest
;
4810 loop
->latch
= exit
->src
;
4813 /* Redirect the entry and add the phi node arguments. */
4814 redirected
= redirect_edge_and_branch (entry
, entry
->dest
->rbi
->copy
);
4815 gcc_assert (redirected
!= NULL
);
4816 flush_pending_stmts (entry
);
4818 /* Concerning updating of dominators: We must recount dominators
4819 for entry block and its copy. Anything that is outside of the region, but
4820 was dominated by something inside needs recounting as well. */
4821 set_immediate_dominator (CDI_DOMINATORS
, entry
->dest
, entry
->src
);
4822 doms
[n_doms
++] = entry
->dest
->rbi
->original
;
4823 iterate_fix_dominators (CDI_DOMINATORS
, doms
, n_doms
);
4826 /* Add the other phi node arguments. */
4827 add_phi_args_after_copy (region_copy
, n_region
);
4829 /* Add phi nodes for definitions at exit. TODO -- once we have immediate
4830 uses, it should be possible to emit phi nodes just for definitions that
4831 are used outside region. */
4832 EXECUTE_IF_SET_IN_BITMAP (definitions
, 0, ver
, bi
)
4834 tree name
= ssa_name (ver
);
4836 phi
= create_phi_node (name
, exit
->dest
);
4837 add_phi_arg (&phi
, name
, exit
);
4838 add_phi_arg (&phi
, name
, exit_copy
);
4840 SSA_NAME_DEF_STMT (name
) = phi
;
4843 /* And create new definitions inside region and its copy. TODO -- once we
4844 have immediate uses, it might be better to leave definitions in region
4845 unchanged, create new ssa names for phi nodes on exit, and rewrite
4846 the uses, to avoid changing the copied region. */
4847 allocate_ssa_names (definitions
, &ssa_name_map
);
4848 rewrite_to_new_ssa_names (region
, n_region
, ssa_name_map
);
4849 allocate_ssa_names (definitions
, &ssa_name_map
);
4850 rewrite_to_new_ssa_names (region_copy
, n_region
, ssa_name_map
);
4851 htab_delete (ssa_name_map
);
4853 if (free_region_copy
)
4856 unmark_all_for_rewrite ();
4857 BITMAP_XFREE (definitions
);
4862 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
4865 dump_function_to_file (tree fn
, FILE *file
, int flags
)
4867 tree arg
, vars
, var
;
4868 bool ignore_topmost_bind
= false, any_var
= false;
4872 fprintf (file
, "%s (", lang_hooks
.decl_printable_name (fn
, 2));
4874 arg
= DECL_ARGUMENTS (fn
);
4877 print_generic_expr (file
, arg
, dump_flags
);
4878 if (TREE_CHAIN (arg
))
4879 fprintf (file
, ", ");
4880 arg
= TREE_CHAIN (arg
);
4882 fprintf (file
, ")\n");
4884 if (flags
& TDF_RAW
)
4886 dump_node (fn
, TDF_SLIM
| flags
, file
);
4890 /* When GIMPLE is lowered, the variables are no longer available in
4891 BIND_EXPRs, so display them separately. */
4892 if (cfun
&& cfun
->unexpanded_var_list
)
4894 ignore_topmost_bind
= true;
4896 fprintf (file
, "{\n");
4897 for (vars
= cfun
->unexpanded_var_list
; vars
; vars
= TREE_CHAIN (vars
))
4899 var
= TREE_VALUE (vars
);
4901 print_generic_decl (file
, var
, flags
);
4902 fprintf (file
, "\n");
4908 if (basic_block_info
)
4910 /* Make a CFG based dump. */
4911 check_bb_profile (ENTRY_BLOCK_PTR
, file
);
4912 if (!ignore_topmost_bind
)
4913 fprintf (file
, "{\n");
4915 if (any_var
&& n_basic_blocks
)
4916 fprintf (file
, "\n");
4919 dump_generic_bb (file
, bb
, 2, flags
);
4921 fprintf (file
, "}\n");
4922 check_bb_profile (EXIT_BLOCK_PTR
, file
);
4928 /* Make a tree based dump. */
4929 chain
= DECL_SAVED_TREE (fn
);
4931 if (TREE_CODE (chain
) == BIND_EXPR
)
4933 if (ignore_topmost_bind
)
4935 chain
= BIND_EXPR_BODY (chain
);
4943 if (!ignore_topmost_bind
)
4944 fprintf (file
, "{\n");
4949 fprintf (file
, "\n");
4951 print_generic_stmt_indented (file
, chain
, flags
, indent
);
4952 if (ignore_topmost_bind
)
4953 fprintf (file
, "}\n");
4956 fprintf (file
, "\n\n");
4960 /* Pretty print of the loops intermediate representation. */
4961 static void print_loop (FILE *, struct loop
*, int);
4962 static void print_pred_bbs (FILE *, basic_block bb
);
4963 static void print_succ_bbs (FILE *, basic_block bb
);
4966 /* Print the predecessors indexes of edge E on FILE. */
4969 print_pred_bbs (FILE *file
, basic_block bb
)
4974 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4975 fprintf (file
, "bb_%d", e
->src
->index
);
4979 /* Print the successors indexes of edge E on FILE. */
4982 print_succ_bbs (FILE *file
, basic_block bb
)
4987 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4988 fprintf (file
, "bb_%d", e
->src
->index
);
4992 /* Pretty print LOOP on FILE, indented INDENT spaces. */
4995 print_loop (FILE *file
, struct loop
*loop
, int indent
)
5003 s_indent
= (char *) alloca ((size_t) indent
+ 1);
5004 memset ((void *) s_indent
, ' ', (size_t) indent
);
5005 s_indent
[indent
] = '\0';
5007 /* Print the loop's header. */
5008 fprintf (file
, "%sloop_%d\n", s_indent
, loop
->num
);
5010 /* Print the loop's body. */
5011 fprintf (file
, "%s{\n", s_indent
);
5013 if (bb
->loop_father
== loop
)
5015 /* Print the basic_block's header. */
5016 fprintf (file
, "%s bb_%d (preds = {", s_indent
, bb
->index
);
5017 print_pred_bbs (file
, bb
);
5018 fprintf (file
, "}, succs = {");
5019 print_succ_bbs (file
, bb
);
5020 fprintf (file
, "})\n");
5022 /* Print the basic_block's body. */
5023 fprintf (file
, "%s {\n", s_indent
);
5024 tree_dump_bb (bb
, file
, indent
+ 4);
5025 fprintf (file
, "%s }\n", s_indent
);
5028 print_loop (file
, loop
->inner
, indent
+ 2);
5029 fprintf (file
, "%s}\n", s_indent
);
5030 print_loop (file
, loop
->next
, indent
);
5034 /* Follow a CFG edge from the entry point of the program, and on entry
5035 of a loop, pretty print the loop structure on FILE. */
5038 print_loop_ir (FILE *file
)
5042 bb
= BASIC_BLOCK (0);
5043 if (bb
&& bb
->loop_father
)
5044 print_loop (file
, bb
->loop_father
, 0);
5048 /* Debugging loops structure at tree level. */
5051 debug_loop_ir (void)
5053 print_loop_ir (stderr
);
5057 /* Return true if BB ends with a call, possibly followed by some
5058 instructions that must stay with the call. Return false,
5062 tree_block_ends_with_call_p (basic_block bb
)
5064 block_stmt_iterator bsi
= bsi_last (bb
);
5065 return get_call_expr_in (bsi_stmt (bsi
)) != NULL
;
5069 /* Return true if BB ends with a conditional branch. Return false,
5073 tree_block_ends_with_condjump_p (basic_block bb
)
5075 tree stmt
= tsi_stmt (bsi_last (bb
).tsi
);
5076 return (TREE_CODE (stmt
) == COND_EXPR
);
5080 /* Return true if we need to add fake edge to exit at statement T.
5081 Helper function for tree_flow_call_edges_add. */
5084 need_fake_edge_p (tree t
)
5088 /* NORETURN and LONGJMP calls already have an edge to exit.
5089 CONST, PURE and ALWAYS_RETURN calls do not need one.
5090 We don't currently check for CONST and PURE here, although
5091 it would be a good idea, because those attributes are
5092 figured out from the RTL in mark_constant_function, and
5093 the counter incrementation code from -fprofile-arcs
5094 leads to different results from -fbranch-probabilities. */
5095 call
= get_call_expr_in (t
);
5097 && !(call_expr_flags (call
) &
5098 (ECF_NORETURN
| ECF_LONGJMP
| ECF_ALWAYS_RETURN
)))
5101 if (TREE_CODE (t
) == ASM_EXPR
5102 && (ASM_VOLATILE_P (t
) || ASM_INPUT_P (t
)))
5109 /* Add fake edges to the function exit for any non constant and non
5110 noreturn calls, volatile inline assembly in the bitmap of blocks
5111 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
5112 the number of blocks that were split.
5114 The goal is to expose cases in which entering a basic block does
5115 not imply that all subsequent instructions must be executed. */
5118 tree_flow_call_edges_add (sbitmap blocks
)
5121 int blocks_split
= 0;
5122 int last_bb
= last_basic_block
;
5123 bool check_last_block
= false;
5125 if (n_basic_blocks
== 0)
5129 check_last_block
= true;
5131 check_last_block
= TEST_BIT (blocks
, EXIT_BLOCK_PTR
->prev_bb
->index
);
5133 /* In the last basic block, before epilogue generation, there will be
5134 a fallthru edge to EXIT. Special care is required if the last insn
5135 of the last basic block is a call because make_edge folds duplicate
5136 edges, which would result in the fallthru edge also being marked
5137 fake, which would result in the fallthru edge being removed by
5138 remove_fake_edges, which would result in an invalid CFG.
5140 Moreover, we can't elide the outgoing fake edge, since the block
5141 profiler needs to take this into account in order to solve the minimal
5142 spanning tree in the case that the call doesn't return.
5144 Handle this by adding a dummy instruction in a new last basic block. */
5145 if (check_last_block
)
5148 basic_block bb
= EXIT_BLOCK_PTR
->prev_bb
;
5149 block_stmt_iterator bsi
= bsi_last (bb
);
5151 if (!bsi_end_p (bsi
))
5154 if (need_fake_edge_p (t
))
5158 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5159 if (e
->dest
== EXIT_BLOCK_PTR
)
5161 bsi_insert_on_edge (e
, build_empty_stmt ());
5162 bsi_commit_edge_inserts ();
5168 /* Now add fake edges to the function exit for any non constant
5169 calls since there is no way that we can determine if they will
5171 for (i
= 0; i
< last_bb
; i
++)
5173 basic_block bb
= BASIC_BLOCK (i
);
5174 block_stmt_iterator bsi
;
5175 tree stmt
, last_stmt
;
5180 if (blocks
&& !TEST_BIT (blocks
, i
))
5183 bsi
= bsi_last (bb
);
5184 if (!bsi_end_p (bsi
))
5186 last_stmt
= bsi_stmt (bsi
);
5189 stmt
= bsi_stmt (bsi
);
5190 if (need_fake_edge_p (stmt
))
5193 /* The handling above of the final block before the
5194 epilogue should be enough to verify that there is
5195 no edge to the exit block in CFG already.
5196 Calling make_edge in such case would cause us to
5197 mark that edge as fake and remove it later. */
5198 #ifdef ENABLE_CHECKING
5199 if (stmt
== last_stmt
)
5202 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5203 gcc_assert (e
->dest
!= EXIT_BLOCK_PTR
);
5207 /* Note that the following may create a new basic block
5208 and renumber the existing basic blocks. */
5209 if (stmt
!= last_stmt
)
5211 e
= split_block (bb
, stmt
);
5215 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
5219 while (!bsi_end_p (bsi
));
5224 verify_flow_info ();
5226 return blocks_split
;
5230 tree_purge_dead_eh_edges (basic_block bb
)
5232 bool changed
= false;
5235 tree stmt
= last_stmt (bb
);
5237 if (stmt
&& tree_can_throw_internal (stmt
))
5240 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
5242 if (e
->flags
& EDGE_EH
)
5244 ssa_remove_edge (e
);
5251 /* Removal of dead EH edges might change dominators of not
5252 just immediate successors. E.g. when bb1 is changed so that
5253 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5254 eh edges purged by this function in:
5266 idom(bb5) must be recomputed. For now just free the dominance
5269 free_dominance_info (CDI_DOMINATORS
);
5275 tree_purge_all_dead_eh_edges (bitmap blocks
)
5277 bool changed
= false;
5281 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, i
, bi
)
5283 changed
|= tree_purge_dead_eh_edges (BASIC_BLOCK (i
));
5289 struct cfg_hooks tree_cfg_hooks
= {
5291 tree_verify_flow_info
,
5292 tree_dump_bb
, /* dump_bb */
5293 create_bb
, /* create_basic_block */
5294 tree_redirect_edge_and_branch
,/* redirect_edge_and_branch */
5295 tree_redirect_edge_and_branch_force
,/* redirect_edge_and_branch_force */
5296 remove_bb
, /* delete_basic_block */
5297 tree_split_block
, /* split_block */
5298 tree_move_block_after
, /* move_block_after */
5299 tree_can_merge_blocks_p
, /* can_merge_blocks_p */
5300 tree_merge_blocks
, /* merge_blocks */
5301 tree_predict_edge
, /* predict_edge */
5302 tree_predicted_by_p
, /* predicted_by_p */
5303 tree_can_duplicate_bb_p
, /* can_duplicate_block_p */
5304 tree_duplicate_bb
, /* duplicate_block */
5305 tree_split_edge
, /* split_edge */
5306 tree_make_forwarder_block
, /* make_forward_block */
5307 NULL
, /* tidy_fallthru_edge */
5308 tree_block_ends_with_call_p
, /* block_ends_with_call_p */
5309 tree_block_ends_with_condjump_p
, /* block_ends_with_condjump_p */
5310 tree_flow_call_edges_add
/* flow_call_edges_add */
5314 /* Split all critical edges. */
5317 split_critical_edges (void)
5325 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5326 if (EDGE_CRITICAL_P (e
) && !(e
->flags
& EDGE_ABNORMAL
))
5333 struct tree_opt_pass pass_split_crit_edges
=
5335 "crited", /* name */
5337 split_critical_edges
, /* execute */
5340 0, /* static_pass_number */
5341 TV_TREE_SPLIT_EDGES
, /* tv_id */
5342 PROP_cfg
, /* properties required */
5343 PROP_no_crit_edges
, /* properties_provided */
5344 0, /* properties_destroyed */
5345 0, /* todo_flags_start */
5346 TODO_dump_func
, /* todo_flags_finish */
5351 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5352 a temporary, make sure and register it to be renamed if necessary,
5353 and finally return the temporary. Put the statements to compute
5354 EXP before the current statement in BSI. */
5357 gimplify_val (block_stmt_iterator
*bsi
, tree type
, tree exp
)
5359 tree t
, new_stmt
, orig_stmt
;
5361 if (is_gimple_val (exp
))
5364 t
= make_rename_temp (type
, NULL
);
5365 new_stmt
= build (MODIFY_EXPR
, type
, t
, exp
);
5367 orig_stmt
= bsi_stmt (*bsi
);
5368 SET_EXPR_LOCUS (new_stmt
, EXPR_LOCUS (orig_stmt
));
5369 TREE_BLOCK (new_stmt
) = TREE_BLOCK (orig_stmt
);
5371 bsi_insert_before (bsi
, new_stmt
, BSI_SAME_STMT
);
5376 /* Build a ternary operation and gimplify it. Emit code before BSI.
5377 Return the gimple_val holding the result. */
5380 gimplify_build3 (block_stmt_iterator
*bsi
, enum tree_code code
,
5381 tree type
, tree a
, tree b
, tree c
)
5385 ret
= fold (build3 (code
, type
, a
, b
, c
));
5388 return gimplify_val (bsi
, type
, ret
);
5391 /* Build a binary operation and gimplify it. Emit code before BSI.
5392 Return the gimple_val holding the result. */
5395 gimplify_build2 (block_stmt_iterator
*bsi
, enum tree_code code
,
5396 tree type
, tree a
, tree b
)
5400 ret
= fold (build2 (code
, type
, a
, b
));
5403 return gimplify_val (bsi
, type
, ret
);
5406 /* Build a unary operation and gimplify it. Emit code before BSI.
5407 Return the gimple_val holding the result. */
5410 gimplify_build1 (block_stmt_iterator
*bsi
, enum tree_code code
, tree type
,
5415 ret
= fold (build1 (code
, type
, a
));
5418 return gimplify_val (bsi
, type
, ret
);
5423 /* Emit return warnings. */
5426 execute_warn_function_return (void)
5428 #ifdef USE_MAPPED_LOCATION
5429 source_location location
;
5437 if (warn_missing_noreturn
5438 && !TREE_THIS_VOLATILE (cfun
->decl
)
5439 && EDGE_COUNT (EXIT_BLOCK_PTR
->preds
) == 0
5440 && !lang_hooks
.function
.missing_noreturn_ok_p (cfun
->decl
))
5441 warning ("%Jfunction might be possible candidate for "
5442 "attribute %<noreturn%>",
5445 /* If we have a path to EXIT, then we do return. */
5446 if (TREE_THIS_VOLATILE (cfun
->decl
)
5447 && EDGE_COUNT (EXIT_BLOCK_PTR
->preds
) > 0)
5449 #ifdef USE_MAPPED_LOCATION
5450 location
= UNKNOWN_LOCATION
;
5454 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
5456 last
= last_stmt (e
->src
);
5457 if (TREE_CODE (last
) == RETURN_EXPR
5458 #ifdef USE_MAPPED_LOCATION
5459 && (location
= EXPR_LOCATION (last
)) != UNKNOWN_LOCATION
)
5461 && (locus
= EXPR_LOCUS (last
)) != NULL
)
5465 #ifdef USE_MAPPED_LOCATION
5466 if (location
== UNKNOWN_LOCATION
)
5467 location
= cfun
->function_end_locus
;
5468 warning ("%H%<noreturn%> function does return", &location
);
5471 locus
= &cfun
->function_end_locus
;
5472 warning ("%H%<noreturn%> function does return", locus
);
5476 /* If we see "return;" in some basic block, then we do reach the end
5477 without returning a value. */
5478 else if (warn_return_type
5479 && EDGE_COUNT (EXIT_BLOCK_PTR
->preds
) > 0
5480 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun
->decl
))))
5482 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
5484 tree last
= last_stmt (e
->src
);
5485 if (TREE_CODE (last
) == RETURN_EXPR
5486 && TREE_OPERAND (last
, 0) == NULL
)
5488 #ifdef USE_MAPPED_LOCATION
5489 location
= EXPR_LOCATION (last
);
5490 if (location
== UNKNOWN_LOCATION
)
5491 location
= cfun
->function_end_locus
;
5492 warning ("%Hcontrol reaches end of non-void function", &location
);
5494 locus
= EXPR_LOCUS (last
);
5496 locus
= &cfun
->function_end_locus
;
5497 warning ("%Hcontrol reaches end of non-void function", locus
);
5506 /* Given a basic block B which ends with a conditional and has
5507 precisely two successors, determine which of the edges is taken if
5508 the conditional is true and which is taken if the conditional is
5509 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5512 extract_true_false_edges_from_block (basic_block b
,
5516 edge e
= EDGE_SUCC (b
, 0);
5518 if (e
->flags
& EDGE_TRUE_VALUE
)
5521 *false_edge
= EDGE_SUCC (b
, 1);
5526 *true_edge
= EDGE_SUCC (b
, 1);
5530 struct tree_opt_pass pass_warn_function_return
=
5534 execute_warn_function_return
, /* execute */
5537 0, /* static_pass_number */
5539 PROP_cfg
, /* properties_required */
5540 0, /* properties_provided */
5541 0, /* properties_destroyed */
5542 0, /* todo_flags_start */
5543 0, /* todo_flags_finish */
5547 #include "gt-tree-cfg.h"