1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003 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"
46 /* This file contains functions for building the Control Flow Graph (CFG)
47 for a function tree. */
49 /* Statements in basic blocks are kept in double-linked list of tree_cell
50 nodes. On the beginning of the block there may be some labels, then
51 statements follow. The last statement is either control one (COND_EXPR
52 or SWITCH_EXPR), the one that ends the block from some other reason
55 If it is COND_EXPR, its then and else branches contain GOTO_EXPR to the
56 target location. None of the outgoing edges is marked fallthru.
58 If it is SWITCH_EXPR, its body contains a list (linked by COMPOUND_EXPRs)
59 containing pairs of CASE_LABEL_EXPRs and GOTO_EXPRs (CASE_* macros below
60 should be used to access them). There always must be a default branch.
61 None of the outgoing edges is marked fallthru. There is a link case_edge
62 from statement annotation of the CASE_LABEL_EXPRs to the appropriate edges.
64 If it is other block-ending statement (exception handling), the normal
65 flow edge is marked fallthru. It does not have to really lead to the
68 If it is any other statement, there is exactly one fallthru edge to the
69 following block. (This is ugly. The explicit gotos should not be
70 here at all. Still we cannot get rid of them completely -- computed gotos;
71 so some special handling would be needed anyway, so let it be for now
74 Some of these invariants may be violated by optimization passes.
75 New blocks may be created on fallthru edges, and edges may be removed
76 when they become unreachable. The function tree_cleanup_edges fixes
77 these issues (but it is ugly too. The optimizations should be fixed
78 to keep the state consistent.) */
80 /* Local declarations. */
82 /* Initial capacity for the basic block array. */
83 static const int initial_cfg_capacity
= 20;
85 /* Dump files and flags. */
86 static FILE *dump_file
; /* CFG dump file. */
87 static int dump_flags
; /* CFG dump flags. */
89 /* Mapping of labels to their associated blocks. This can greatly speed up
90 building of the CFG in code with lots of gotos. */
91 static varray_type label_to_block_map
;
93 /* Set if we no longer want bb_for_stmt to be kept. */
99 long num_merged_cases
;
100 long num_merged_labels
;
101 long num_failed_bind_expr_merges
;
104 static dominance_info pdom_info
= NULL
;
106 static struct cfg_stats_d cfg_stats
;
108 static struct obstack block_ann_obstack
;
109 static void *first_block_ann_obj
= 0;
111 /* Control structure decomposition tree. */
124 } type
; /* Type of the structure. */
126 struct control
*outer
; /* Outer sutructure. */
127 struct control
*son
; /* The first substructure. */
128 struct control
*next
, *prev
; /* The next control structure in a chain. */
130 int level
; /* Nesting level of the structure. */
131 bool visited
; /* A mark. */
132 varray_type exits
; /* Exits from the structure. */
133 struct control
*lay_head
, *lay_next
, *lay_last
;
134 struct control
*follow_node
; /* The next cs after the structure. */
135 /* Chain of layout. */
136 struct control
*else_node
;
138 tree artificial_label
; /* In case artificial label is to be added
139 to the start of the block, its set here. */
146 } fa_then
, fa_else
; /* What to do with final statement? */
148 basic_block header
; /* Entry of the structure. */
149 basic_block latch
; /* Latch of a loop. */
150 basic_block follow
; /* The next block after the structure. */
153 /* Basic blocks and flowgraphs. */
154 static void make_blocks (tree_cell
);
155 static inline void append_stmt_to_bb (tree_cell
, basic_block
);
156 extern void debug_tree_chain (tree_cell
);
157 static void create_blocks_annotations (void);
158 static void create_block_annotation (basic_block
);
159 static void free_blocks_annotations (void);
160 static void clear_blocks_annotations (void);
163 static void make_edges (void);
164 static void make_ctrl_stmt_edges (basic_block
);
165 static void make_exit_edges (basic_block
);
166 static void make_cond_expr_edges (basic_block
);
167 static void make_goto_expr_edges (basic_block
);
168 static void make_switch_expr_edges (basic_block
);
169 static bool tree_redirect_edge_and_branch (edge
, basic_block
);
171 /* Various helpers. */
172 static inline bool stmt_starts_bb_p (tree
, tree
);
173 static inline bool stmt_ends_bb_p (tree
);
174 static int tree_verify_flow_info (void);
175 static basic_block
tree_make_forwarder_block (basic_block
, int, int, edge
, int);
176 static void replace_stmt (tree_cell
, tree
);
177 static struct loops
*tree_loop_optimizer_init (FILE *);
178 static void tree_loop_optimizer_finalize (struct loops
*, FILE *);
180 /* Flowgraph optimization and cleanup. */
181 static void remove_bb (basic_block
);
182 static void tree_merge_blocks (basic_block
, basic_block
);
183 static void remove_stmt (tree_cell
, basic_block
, bool);
184 static edge
find_taken_edge_cond_expr (basic_block
, tree
);
185 static edge
find_taken_edge_switch_expr (basic_block
, tree
);
186 static tree
find_edge_goto (tree
, edge
);
187 static bool value_matches_label (edge
, tree
, tree
, edge
*);
188 static void tree_cleanup_edges (void);
189 static void remove_superfluous_labels (void);
190 static void thread_jumps (void);
191 static bool tree_forwarder_block_p (basic_block
);
192 static void merge_seq_blocks (void);
193 static tree
CASE_GOTO (tree_stmt_iterator
);
194 static struct control
*determine_structures (struct control
*[]);
195 static struct control
*make_control_node (enum cs_type
, struct control
*,
196 basic_block
, basic_block
,
198 static void try_move_control_node (struct control
*, struct control
*,
200 static void move_control_node (struct control
*, struct control
*);
201 static void dump_cs_tree (FILE *, int, struct control
*, struct control
*[]);
202 static void free_cs_tree (struct control
*);
203 static void add_bb_cs_nodes (struct control
*[]);
204 static void assign_levels (struct control
*, int);
205 static void compute_structure_exits (struct control
*[]);
206 static void layout_structures (struct control
*, struct control
*[]);
207 static void layout_structure (struct control
*, struct control
*,
209 static void add_to_layout (struct control
*);
210 static void finish_layout (struct control
*);
211 static tree
reconstruct_tree (struct control
*, struct control
*[]);
212 static bool inside_cs (basic_block
, struct control
*, struct control
*[]);
213 static struct control
*lift (struct control
*, int);
214 static basic_block
first_cs_bb (struct control
*);
215 static void fixup_gotos (struct control
*, struct control
*[],
216 struct control
*, basic_block
, basic_block
);
217 static void establish_gotos (tree
*, enum fa
, struct control
*);
218 static enum fa
decide_final_action (basic_block
, basic_block
, basic_block
,
219 basic_block
, struct control
*[]);
221 /* Location to track pending stmt for edge insertion. */
222 #define PENDING_STMT(e) ((tree)(e->insns))
224 /* Set the pending stmt field. */
225 #define SET_PENDING_STMT(e, t) ((e->insns) = (rtx)t)
227 /* Accessors for switch statement case list. */
228 #define CASE_START(STMT) tsi_start (&SWITCH_BODY (STMT))
229 #define CASE_NEXT(TSI) tsi_next (&TSI), tsi_next (&TSI)
230 #define CASE_END_P(TSI) tsi_end_p (TSI)
232 CASE_GOTO (tree_stmt_iterator tsi
)
235 return tsi_stmt (tsi
);
237 #define CASE_DESTINATION(TSI) (GOTO_DESTINATION (CASE_GOTO (TSI)))
238 #define CASE_CASE(TSI) (tsi_stmt(TSI))
239 #define CASE_EDGE(TSI) (get_stmt_ann (CASE_CASE (TSI))->case_edge)
241 /* FIXME These need to be filled in with appropriate pointers. But this
242 implies an ABI change in some functions. */
243 struct cfg_hooks tree_cfg_hooks
= {
244 tree_verify_flow_info
,
246 NULL
, /* create_basic_block */
247 tree_redirect_edge_and_branch
, /* redirect_edge_and_branch */
248 NULL
, /* redirect_edge_and_branch_force */
249 remove_bb
, /* delete_basic_block */
250 NULL
, /* split_block */
251 NULL
, /* can_merge_blocks_p */
252 tree_merge_blocks
, /* merge_blocks */
253 tree_split_edge
, /* cfgh_split_edge */
254 tree_make_forwarder_block
, /* cfgh_make_forward_block */
255 tree_cleanup_edges
, /* cfgh_tidy_fallthru_edges */
256 tree_loop_optimizer_init
, /* cfgh_loop_optimizer_init */
257 tree_loop_optimizer_finalize
/* cfgh_loop_optimizer_finalize */
260 /*---------------------------------------------------------------------------
262 ---------------------------------------------------------------------------*/
264 /* Entry point to the CFG builder for trees. FNBODY is the body of the
265 function to process. */
268 build_tree_cfg (tree_cell fnbody
)
270 timevar_push (TV_TREE_CFG
);
272 /* Register specific tree functions. */
273 tree_register_cfg_hooks ();
275 /* Initialize the basic block array. */
277 last_basic_block
= 0;
278 VARRAY_BB_INIT (basic_block_info
, initial_cfg_capacity
, "basic_block_info");
279 memset ((void *) &cfg_stats
, 0, sizeof (cfg_stats
));
280 no_bb_for_stmt
= false;
282 /* Build a mapping of labels to their associated blocks. */
283 VARRAY_BB_INIT (label_to_block_map
, initial_cfg_capacity
,
284 "label to block map");
286 ENTRY_BLOCK_PTR
->next_bb
= EXIT_BLOCK_PTR
;
287 EXIT_BLOCK_PTR
->prev_bb
= ENTRY_BLOCK_PTR
;
289 /* Find the basic blocks for the flowgraph. Ignore empty functions. */
292 timevar_pop (TV_TREE_CFG
);
296 /* Create block annotations. */
297 create_blocks_annotations ();
299 make_blocks (fnbody
);
301 if (n_basic_blocks
> 0)
303 /* Adjust the size of the array. */
304 VARRAY_GROW (basic_block_info
, n_basic_blocks
);
306 /* Create the edges of the flowgraph. */
312 /* The loop analyzer should be initialized right after the CFG
313 construction because some loops will need latch blocks, and these
314 need to be added before we do anything else. If you use this
315 structure you'll have to ensure that optimizers don't invalidate the
316 information gathered in the loops structure via modifications to the
317 underlying structure: the CFG. */
318 struct loops
*loops
= loop_optimizer_init (NULL
);
320 /* Once initialized, it's not really necessary to keep the loop data
321 structures around. They may be rescanned using flow_loops_find. */
322 loop_optimizer_finalize (loops
, NULL
);
326 timevar_pop (TV_TREE_CFG
);
328 cleanup_tree_cfg (true);
329 #if defined ENABLE_CHECKING
333 /* Debugging dumps. */
334 if (n_basic_blocks
> 0)
336 /* Write the flowgraph to a dot file. */
337 dump_file
= dump_begin (TDI_dot
, &dump_flags
);
340 tree_cfg2dot (dump_file
);
341 dump_end (TDI_dot
, dump_file
);
344 /* Dump a textual representation of the flowgraph. */
345 dump_file
= dump_begin (TDI_cfg
, &dump_flags
);
348 dump_tree_cfg (dump_file
, dump_flags
);
349 dump_end (TDI_cfg
, dump_file
);
354 /* Create annotations for all the basic blocks. */
356 static void create_blocks_annotations (void)
359 static int initialized
;
363 gcc_obstack_init (&block_ann_obstack
);
366 /* Check whether TREE_ANNOTATIONS data are still allocated. */
367 else if (first_block_ann_obj
)
370 first_block_ann_obj
= obstack_alloc (&block_ann_obstack
, 0);
372 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
373 create_block_annotation (bb
);
376 /* Create annotations for a single basic block. */
378 static void create_block_annotation (basic_block bb
)
380 /* Verify that the tree_annotations field is clear. */
381 if (bb
->tree_annotations
|| !first_block_ann_obj
)
383 bb
->tree_annotations
= obstack_alloc (&block_ann_obstack
,
384 sizeof (struct bb_ann_d
));
385 memset (bb
->tree_annotations
, 0, sizeof (struct bb_ann_d
));
388 /* Free the annotations for all the basic blocks. */
390 static void free_blocks_annotations (void)
392 if (!first_block_ann_obj
)
394 obstack_free (&block_ann_obstack
, first_block_ann_obj
);
395 first_block_ann_obj
= NULL
;
397 clear_blocks_annotations ();
400 /* Clear the annotations for all the basic blocks. */
403 clear_blocks_annotations (void)
407 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
408 bb
->tree_annotations
= NULL
;
411 /* Build a flowgraph for the statements starting at the statement STMT. */
414 make_blocks (tree_cell stmt
)
417 basic_block bb
= NULL
;
421 || stmt
->stmt
== error_mark_node
)
424 for (act
= stmt
; act
; act
= next
)
428 prev_stmt
= act
->prev
? act
->prev
->stmt
: NULL_TREE
;
429 if (!bb
|| stmt_starts_bb_p (act
->stmt
, prev_stmt
))
432 act
->prev
->next
= NULL
;
437 append_stmt_to_bb (act
, bb
);
439 if (stmt_ends_bb_p (act
->stmt
))
447 /* Add statement STMT to basic block BB and update BB's
448 boundaries accordingly. */
451 append_stmt_to_bb (tree_cell stmt
, basic_block bb
)
453 set_bb_for_stmt (stmt
->stmt
, bb
);
455 /* Update the head and tail of the block. */
456 if (bb
->head_tree
== NULL
)
457 bb
->head_tree
= stmt
;
462 /* Create and return a new basic block. */
469 /* Create and initialize a new basic block. */
471 memset (bb
, 0, sizeof (*bb
));
472 create_block_annotation (bb
);
474 bb
->index
= last_basic_block
;
477 /* Add the new block to the linked list of blocks. */
478 link_block (bb
, EXIT_BLOCK_PTR
->prev_bb
);
480 /* Grow the basic block array if needed. */
481 if ((size_t) n_basic_blocks
== VARRAY_SIZE (basic_block_info
))
482 VARRAY_GROW (basic_block_info
, n_basic_blocks
+ (n_basic_blocks
+ 3) / 4);
484 /* Add the newly created block to the array. */
485 BASIC_BLOCK (n_basic_blocks
) = bb
;
492 /*---------------------------------------------------------------------------
494 ---------------------------------------------------------------------------*/
496 /* Join all the blocks in the flowgraph. */
503 /* Create an edge from entry to the first block with executable
505 make_edge (ENTRY_BLOCK_PTR
, BASIC_BLOCK (0), EDGE_FALLTHRU
);
507 /* Traverse basic block array placing edges. */
510 tree first
= first_stmt (bb
);
511 tree last
= last_stmt (bb
);
515 /* Edges for statements that always alter flow control. */
516 if (is_ctrl_stmt (last
))
517 make_ctrl_stmt_edges (bb
);
519 /* Edges for statements that sometimes alter flow control. */
520 if (is_ctrl_altering_stmt (last
))
521 make_exit_edges (bb
);
524 /* Finally, if no edges were created above, this is a regular
525 basic block that only needs a fallthru edge. */
526 if (bb
->succ
== NULL
)
527 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
530 /* We do not care about fake edges, so remove any that the CFG
531 builder inserted for completeness. */
532 remove_fake_edges ();
535 /* Create edges for control statement at basic block BB. */
538 make_ctrl_stmt_edges (basic_block bb
)
540 tree last
= last_stmt (bb
);
542 #if defined ENABLE_CHECKING
543 if (last
== NULL_TREE
)
547 switch (TREE_CODE (last
))
550 make_goto_expr_edges (bb
);
552 /* If this is potentially a nonlocal goto, then this should also
553 create an edge to the exit block. */
554 if ((TREE_CODE (GOTO_DESTINATION (last
)) == LABEL_DECL
555 && (decl_function_context (GOTO_DESTINATION (last
))
556 != current_function_decl
))
557 || (TREE_CODE (GOTO_DESTINATION (last
)) != LABEL_DECL
558 && DECL_CONTEXT (current_function_decl
)))
559 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_ABNORMAL
);
563 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
567 make_cond_expr_edges (bb
);
571 make_switch_expr_edges (bb
);
575 make_eh_edges (last
);
576 /* Yet another NORETURN hack. */
577 if (bb
->succ
== NULL
)
578 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
586 /* Checks whether STMT is potentially a nonlocal goto. */
588 nonlocal_goto_p (tree stmt
)
590 return (TREE_CODE (GOTO_DESTINATION (stmt
)) == LABEL_DECL
591 && (decl_function_context (GOTO_DESTINATION (stmt
))
592 != current_function_decl
))
593 || (TREE_CODE (GOTO_DESTINATION (stmt
)) != LABEL_DECL
594 && DECL_CONTEXT (current_function_decl
));
597 /* Create exit edges for statements in block BB that alter the flow of
598 control. Statements that alter the control flow are 'goto', 'return'
599 and calls to non-returning functions. */
602 make_exit_edges (basic_block bb
)
604 tree last
= last_stmt (bb
);
606 if (last
== NULL_TREE
)
609 switch (TREE_CODE (last
))
612 /* If this function receives a nonlocal goto, then we need to
613 make edges from this call site to all the nonlocal goto
615 if (FUNCTION_RECEIVES_NONLOCAL_GOTO (current_function_decl
))
616 make_goto_expr_edges (bb
);
618 /* If this statement has reachable exception handlers, then
619 create abnormal edges to them. */
620 make_eh_edges (last
);
622 /* Some calls are known not to return. For such calls we create
625 We really need to revamp how we build edges so that it's not
626 such a bloody pain to avoid creating edges for this case since
627 all we do is remove these edges when we're done building the
629 if (call_expr_flags (last
) & (ECF_NORETURN
| ECF_LONGJMP
))
631 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
635 /* Don't forget the fall-thru edge. */
636 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
640 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
641 may have an abnormal edge. Search the RHS for this case and
642 create any required edges. */
643 if (TREE_CODE (TREE_OPERAND (last
, 1)) == CALL_EXPR
644 && FUNCTION_RECEIVES_NONLOCAL_GOTO (current_function_decl
))
645 make_goto_expr_edges (bb
);
647 make_eh_edges (last
);
648 make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
656 /* Create the edges for a COND_EXPR starting at block BB. */
659 make_cond_expr_edges (basic_block bb
)
661 tree entry
= last_stmt (bb
);
662 basic_block then_bb
, else_bb
;
663 tree then_label
, else_label
;
665 #if defined ENABLE_CHECKING
666 if (entry
== NULL_TREE
|| TREE_CODE (entry
) != COND_EXPR
)
670 then_label
= GOTO_DESTINATION (COND_EXPR_THEN (entry
));
671 else_label
= GOTO_DESTINATION (COND_EXPR_ELSE (entry
));
672 then_bb
= VARRAY_BB (label_to_block_map
, LABEL_DECL_INDEX (then_label
));
673 else_bb
= VARRAY_BB (label_to_block_map
, LABEL_DECL_INDEX (else_label
));
674 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
675 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
678 /* Create the edges for a SWITCH_EXPR starting at block BB. */
681 make_switch_expr_edges (basic_block bb
)
683 tree entry
= last_stmt (bb
);
684 basic_block label_bb
;
686 tree_stmt_iterator case_entry
;
689 #if defined ENABLE_CHECKING
690 if (entry
== NULL_TREE
|| TREE_CODE (entry
) != SWITCH_EXPR
)
694 for (case_entry
= CASE_START (entry
);
695 !CASE_END_P (case_entry
);
696 CASE_NEXT (case_entry
))
698 label
= CASE_DESTINATION (case_entry
);
699 label_bb
= VARRAY_BB (label_to_block_map
, LABEL_DECL_INDEX (label
));
700 e
= make_edge (bb
, label_bb
, 0);
702 e
= find_edge (bb
, label_bb
);
705 CASE_EDGE (case_entry
) = e
;
709 /* Redirects edge E to basic block DEST. */
711 tree_redirect_edge_and_branch (edge e
, basic_block dest
)
713 block_stmt_iterator bsi
;
715 basic_block bb
= e
->src
;
717 tree_stmt_iterator case_entry
;
722 if (e
->flags
& EDGE_ABNORMAL
)
725 stmt
= last_stmt (bb
);
728 /* If some edge is already there, use it if possible. */
729 old
= find_edge (bb
, dest
);
730 if (old
&& (old
->flags
& EDGE_ABNORMAL
))
734 || !stmt_ends_bb_p (stmt
))
736 if (!(e
->flags
& EDGE_FALLTHRU
))
745 if (TREE_CODE (stmt
) == COND_EXPR
746 || TREE_CODE (stmt
) == GOTO_EXPR
)
748 gto
= find_edge_goto (stmt
, e
);
749 GOTO_DESTINATION (gto
) = tree_block_label (dest
);
753 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
755 /* This is more complicated, since there may be more gotos corresponding
757 for (case_entry
= CASE_START (stmt
);
758 !CASE_END_P (case_entry
);
759 CASE_NEXT (case_entry
))
760 if (CASE_EDGE (case_entry
) == e
)
762 CASE_DESTINATION (case_entry
) = tree_block_label (dest
);
764 CASE_EDGE (case_entry
) = old
;
770 if (e
->flags
& EDGE_FALLTHRU
)
782 for (phi
= phi_nodes (e
->dest
); phi
; phi
= TREE_CHAIN (phi
))
783 remove_phi_arg (phi
, e
->src
);
784 redirect_edge_succ (e
, dest
);
786 tree_cleanup_block_edges (bb
, false);
791 label_to_block (tree dest
)
793 return VARRAY_BB (label_to_block_map
, LABEL_DECL_INDEX (dest
));
796 /* Create edges for a goto statement at block BB. */
799 make_goto_expr_edges (basic_block bb
)
802 basic_block target_bb
;
806 goto_t
= last_stmt (bb
);
808 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
809 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
810 from a nonlocal goto. */
811 if (TREE_CODE (goto_t
) != GOTO_EXPR
)
813 dest
= error_mark_node
;
815 edge_flags
= EDGE_ABNORMAL
;
819 dest
= GOTO_DESTINATION (goto_t
);
822 /* A GOTO to a local label creates normal edges. */
823 if (TREE_CODE (dest
) == LABEL_DECL
&& ! NONLOCAL_LABEL (dest
))
825 make_edge (bb
, label_to_block (dest
), 0);
829 /* If we reach here, then we either have a computed goto or
831 edge_flags
= EDGE_ABNORMAL
;
834 /* Look for the block starting with the destination label. In the
835 case of a computed goto, make an edge to any label block we find
837 FOR_EACH_BB (target_bb
)
839 block_stmt_iterator bsi
;
841 for (bsi
= bsi_start (target_bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
843 tree target
= bsi_stmt (bsi
);
845 if (TREE_CODE (target
) != LABEL_EXPR
)
848 /* Computed GOTOs. Make an edge to every label block that has
849 been marked as a potential target for a computed goto. */
850 if (TREE_CODE (dest
) != LABEL_DECL
851 && FORCED_LABEL (LABEL_EXPR_LABEL (target
))
853 make_edge (bb
, target_bb
, edge_flags
);
855 /* Nonlocal GOTO target. Make an edge to every label block that has
856 been marked as a potential target for a nonlocal goto. */
857 else if (TREE_CODE (dest
) != LABEL_DECL
858 && NONLOCAL_LABEL (LABEL_EXPR_LABEL (target
))
860 make_edge (bb
, target_bb
, edge_flags
);
865 /*---------------------------------------------------------------------------
867 ---------------------------------------------------------------------------*/
869 /* Remove unreachable blocks and other miscellaneous clean up work. */
872 cleanup_tree_cfg (int expensive
)
874 int orig_n_basic_blocks
= n_basic_blocks
;
876 timevar_push (TV_TREE_CLEANUP_CFG
);
877 if (pdom_info
!= NULL
)
879 free_dominance_info (pdom_info
);
882 tree_cleanup_edges ();
885 remove_superfluous_labels ();
888 delete_unreachable_blocks ();
891 #ifdef ENABLE_CHECKING
895 /* If we expunged any basic blocks, then the dominator tree is
897 if (n_basic_blocks
!= orig_n_basic_blocks
)
902 clear_dom_children (bb
);
905 timevar_pop (TV_TREE_CLEANUP_CFG
);
908 /* Remove PHI nodes associated with basic block BB and all edges into
911 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb
)
913 /* Remove the edges into and out of this block. */
914 while (bb
->pred
!= NULL
)
918 /* Since this block is no longer reachable, we can just delete all
920 phi
= phi_nodes (bb
);
923 tree next
= TREE_CHAIN (phi
);
924 remove_phi_node (phi
, NULL_TREE
, bb
);
928 remove_edge (bb
->pred
);
931 /* Remove edges to BB's successors. */
932 while (bb
->succ
!= NULL
)
933 ssa_remove_edge (bb
->succ
);
936 /* Remove block BB and its statements from the flowgraph. */
939 remove_bb (basic_block bb
)
941 block_stmt_iterator i
;
944 dump_file
= dump_begin (TDI_cfg
, &dump_flags
);
947 fprintf (dump_file
, "Removing basic block %d\n", bb
->index
);
948 dump_tree_bb (dump_file
, "", bb
, 0);
949 fprintf (dump_file
, "\n");
950 dump_end (TDI_cfg
, dump_file
);
956 /* Remove all the instructions in the block. */
957 for (i
= bsi_last (bb
); !bsi_end_p (i
); i
= bsi_last (bb
))
959 tree stmt
= bsi_stmt (i
);
961 if (get_lineno (stmt
) >= 0)
963 loc
.file
= get_filename (stmt
);
964 loc
.line
= get_lineno (stmt
);
967 set_bb_for_stmt (stmt
, NULL
);
970 /* If requested, give a warning that the first statement in the
971 block is unreachable. We walk statements backwards in the
972 loop above, so the last statement we process is the first statement
974 if (warn_notreached
&& loc
.line
>= 0)
975 warning ("%Hwill never be executed", &loc
);
977 remove_phi_nodes_and_edges_for_unreachable_block (bb
);
979 /* If we have pdom information, then we must also make sure to
980 clean up the dominance information. */
982 delete_from_dominance_info (pdom_info
, bb
);
984 /* Remove the basic block from the array. */
988 /* Merges block B into block A. */
990 tree_merge_blocks (basic_block a
, basic_block b
)
992 block_stmt_iterator bsi
;
996 /* Ensure that b follows a. */
998 tree_move_block_after (b
, a
, false);
1000 if (!(a
->succ
->flags
& EDGE_FALLTHRU
))
1004 && stmt_ends_bb_p (last_stmt (a
)))
1007 /* Turn phi nodes into assignments. */
1009 for (phi
= phi_nodes (b
); phi
; phi
= TREE_CHAIN (phi
))
1011 tree src
= PHI_ARG_DEF (phi
, 0);
1012 tree dest
= PHI_RESULT (phi
);
1014 if (virtual_op_p (SSA_NAME_VAR (dest
)))
1018 stmt
= build_empty_stmt ();
1019 get_stmt_ann (stmt
);
1020 add_vdef (SSA_NAME_VAR (dest
), stmt
, NULL
);
1021 vdef
= VARRAY_TREE (vdef_ops (stmt
), 0);
1022 VDEF_RESULT (vdef
) = dest
;
1023 VDEF_OP (vdef
) = src
;
1026 stmt
= build (MODIFY_EXPR
, TREE_TYPE (dest
), dest
, src
);
1028 SSA_NAME_DEF_STMT (dest
) = stmt
;
1029 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
1032 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1033 for (bsi
= bsi_start (b
); !bsi_end_p (bsi
);)
1035 if (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
)
1039 set_bb_for_stmt (bsi_stmt (bsi
), a
);
1044 remove_edge (a
->succ
);
1046 /* Merge the chains. */
1048 a
->end_tree
->next
= b
->head_tree
;
1050 a
->head_tree
= b
->head_tree
;
1053 b
->head_tree
->prev
= a
->end_tree
;
1054 a
->end_tree
= b
->end_tree
;
1056 b
->head_tree
= b
->end_tree
= NULL
;
1058 /* Redirect the edges. */
1061 for (e
= a
->succ
; e
; e
= e
->succ_next
)
1065 delete_basic_block (b
);
1068 /* Remove statement pointed by iterator I. */
1071 bsi_remove (block_stmt_iterator
*i
)
1073 tree_cell cell
= bsi_cell (*i
);
1077 remove_stmt (cell
, i
->bb
, true);
1080 /* Remove statement pointed by iterator I, but leave the annotations. */
1083 bsi_remove_leave_annot (block_stmt_iterator
*i
)
1085 tree_cell cell
= bsi_cell (*i
);
1089 remove_stmt (cell
, i
->bb
, false);
1092 /* Move the statement at FROM so it comes right after the statement at
1095 bsi_move_after (block_stmt_iterator from
, block_stmt_iterator to
)
1097 tree stmt
= bsi_stmt (from
);
1098 bsi_remove_leave_annot (&from
);
1099 bsi_insert_after (&to
, stmt
, BSI_SAME_STMT
);
1102 /* Move the statement at FROM so it comes right before the statement
1105 bsi_move_before (block_stmt_iterator from
, block_stmt_iterator to
)
1107 tree stmt
= bsi_stmt (from
);
1108 bsi_remove_leave_annot (&from
);
1109 bsi_insert_before (&to
, stmt
, BSI_SAME_STMT
);
1112 /* Move the statement at FROM to the end of basic block BB, */
1114 bsi_move_to_bb_end (block_stmt_iterator from
, basic_block bb
)
1116 block_stmt_iterator last
= bsi_last (bb
);
1118 /* Have to check bsi_end_p because it could be an empty block. */
1119 if (!bsi_end_p (last
)
1120 && is_ctrl_stmt (bsi_stmt (last
)))
1122 bsi_move_before (from
, last
);
1126 bsi_move_after (from
, last
);
1130 /* Replace the contents of a stmt with another. The replacement cannot be
1131 a COMPOUND_EXPR node, only a gimple stmt. */
1134 bsi_replace (block_stmt_iterator bsi
, tree stmt
)
1136 if (TREE_CODE (stmt
) == COMPOUND_EXPR
)
1139 replace_stmt (bsi_cell (bsi
), stmt
);
1140 modify_stmt (bsi_stmt (bsi
));
1143 /* Remove statement CELL in basic block BB. Reset the annotations if
1144 REMOVE_ANNOTATIONS is true. */
1147 remove_stmt (tree_cell cell
, basic_block bb
, bool remove_annotations
)
1152 tree stmt
= cell
->stmt
;
1154 /* If the statement is a LABEL_EXPR, remove the LABEL_DECL from
1155 the symbol table. */
1156 if (TREE_CODE (stmt
) == LABEL_EXPR
)
1157 remove_decl (LABEL_EXPR_LABEL (stmt
), DECL_INITIAL (current_function_decl
));
1159 if (remove_annotations
)
1161 /* If the statement is already in SSA form, mark all the
1162 definitions made in the statement invalid.
1164 FIXME: We should probably traverse all the def-use edges
1165 originating at this statement to update each use of the
1166 definitions made here, but that is expensive and can easily
1167 be checked by every pass by checking if SSA_NAME_DEF_STMT is
1169 defs
= def_ops (stmt
);
1170 for (i
= 0; defs
&& i
< VARRAY_ACTIVE_SIZE (defs
); i
++)
1172 tree
*def_p
= VARRAY_GENERIC_PTR (defs
, i
);
1173 if (TREE_CODE (*def_p
) == SSA_NAME
)
1174 SSA_NAME_DEF_STMT (*def_p
) = build_empty_stmt ();
1177 vdefs
= vdef_ops (stmt
);
1178 for (i
= 0; vdefs
&& i
< VARRAY_ACTIVE_SIZE (vdefs
); i
++)
1180 tree vdef
= VDEF_RESULT (VARRAY_TREE (vdefs
, i
));
1181 if (TREE_CODE (vdef
) == SSA_NAME
)
1182 SSA_NAME_DEF_STMT (vdef
) = build_empty_stmt ();
1185 stmt
->common
.ann
= NULL
;
1188 /* The RHS of a MODIFY_EXPR has an annotation for the benefit of
1189 SSA-PRE. Make sure to remove that annotation as well.
1191 We're somewhat conservative here in that we do not remove all
1192 annotations on the RHS of the MODIFY_EXPR, just those of type
1193 TREE_ANN_COMMON. If the annotation had another type such
1194 as VAR_ANN other code may still need it and it'll get removed
1195 when we remove all the VAR_ANNs as we tear down the SSA form. */
1196 if (TREE_CODE (stmt
) == MODIFY_EXPR
1197 && TREE_OPERAND (stmt
, 1)->common
.ann
1198 && TREE_OPERAND (stmt
, 1)->common
.ann
->common
.type
== TREE_ANN_COMMON
)
1199 TREE_OPERAND (stmt
, 1)->common
.ann
= NULL
;
1202 cell
->prev
->next
= cell
->next
;
1204 bb
->head_tree
= cell
->next
;
1207 cell
->next
->prev
= cell
->prev
;
1209 bb
->end_tree
= cell
->prev
;
1211 cell
->prev
= cell
->next
= NULL
;
1214 /* Given a control block BB and a constant value VAL, return the edge that
1215 will be taken out of the block. If VAL does not match a unique edge,
1216 NULL is returned. */
1219 find_taken_edge (basic_block bb
, tree val
)
1223 stmt
= last_stmt (bb
);
1225 #if defined ENABLE_CHECKING
1226 if (stmt
== NULL_TREE
|| !is_ctrl_stmt (stmt
))
1230 /* If VAL is not a constant, we can't determine which edge might
1232 if (val
== NULL
|| !really_constant_p (val
))
1235 if (TREE_CODE (stmt
) == COND_EXPR
)
1236 return find_taken_edge_cond_expr (bb
, val
);
1238 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
1239 return find_taken_edge_switch_expr (bb
, val
);
1245 /* Given a constant value VAL and the entry block BB to a COND_EXPR
1246 statement, determine which of the two edges will be taken out of the
1247 block. Return NULL if either edge may be taken. */
1250 find_taken_edge_cond_expr (basic_block bb
, tree val
)
1256 /* Determine which branch of the if() will be taken. */
1257 always_false
= (simple_cst_equal (val
, integer_zero_node
) == 1);
1258 always_true
= (simple_cst_equal (val
, integer_one_node
) == 1);
1260 /* If VAL is a constant but it can't be reduced to a 0 or a 1, then
1261 we don't really know which edge will be taken at runtime. This
1262 may happen when comparing addresses (e.g., if (&var1 == 4)) */
1263 if (!always_false
&& !always_true
)
1266 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1267 if (((e
->flags
& EDGE_TRUE_VALUE
) && always_true
)
1268 || ((e
->flags
& EDGE_FALSE_VALUE
) && always_false
))
1275 /* Given a constant value VAL and the entry block BB to a SWITCH_EXPR
1276 statement, determine which edge will be taken out of the block. Return
1277 NULL if any edge may be taken. */
1280 find_taken_edge_switch_expr (basic_block bb
, tree val
)
1282 edge e
, default_edge
;
1284 tree_stmt_iterator case_entry
;
1286 /* See if the switch() value matches one of the case labels. */
1287 default_edge
= NULL
;
1288 for (case_entry
= CASE_START (last_stmt (bb
));
1289 !CASE_END_P (case_entry
);
1290 CASE_NEXT (case_entry
))
1292 e
= CASE_EDGE (case_entry
);
1293 case_label
= CASE_CASE (case_entry
);
1295 if (value_matches_label (e
, val
, case_label
, &default_edge
))
1299 /* If no case exists for the value used in the switch(), we use the
1304 return default_edge
;
1307 /* Return true if VAL matches the LABEL. If one of the LABEL is the DEFAULT
1308 label, DEST_EDGE is stored into *DEFAULT_EDGE_P to indicate that this edge
1309 goes to the DEFAULT label. This is used by the caller when no other case
1310 label is found to match VAL. */
1313 value_matches_label (edge dest_edge
, tree val
, tree label
, edge
*default_edge_p
)
1315 if (TREE_CODE (label
) != CASE_LABEL_EXPR
)
1318 /* Remember that we found a default label, just in case no other
1319 label matches the switch() value. */
1320 if (CASE_LOW (label
) == NULL_TREE
)
1321 *default_edge_p
= dest_edge
;
1324 /* If we found a match, we are done. */
1325 tree label_val
= CASE_LOW (label
);
1326 if (simple_cst_equal (label_val
, val
) == 1)
1333 /*---------------------------------------------------------------------------
1335 ---------------------------------------------------------------------------*/
1337 /* Dump a basic block to a file. */
1340 dump_tree_bb (FILE *outf
, const char *prefix
, basic_block bb
, int indent
)
1344 block_stmt_iterator si
;
1347 s_indent
= (char *) alloca ((size_t) indent
+ 1);
1348 memset ((void *) s_indent
, ' ', (size_t) indent
);
1349 s_indent
[indent
] = '\0';
1351 fprintf (outf
, "%s%sBLOCK %d\n", s_indent
, prefix
, bb
->index
);
1353 fprintf (outf
, "%s%sPRED: ", s_indent
, prefix
);
1354 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1355 dump_edge_info (outf
, e
, 0);
1358 fprintf (outf
, "%s%sSUCC: ", s_indent
, prefix
);
1359 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1360 dump_edge_info (outf
, e
, 1);
1363 fprintf (outf
, "%s%sLOOP DEPTH: %d\n", s_indent
, prefix
, bb
->loop_depth
);
1365 fprintf (outf
, "%s%sNEXT BLOCK: ", s_indent
, prefix
);
1367 fprintf (outf
, "%d\n", bb
->next_bb
->index
);
1369 fprintf (outf
, "nil\n");
1371 fprintf (outf
, "%s%sPREV BLOCK: ", s_indent
, prefix
);
1373 fprintf (outf
, "%d\n", bb
->prev_bb
->index
);
1375 fprintf (outf
, "nil\n");
1377 if (bb
->tree_annotations
)
1378 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
1380 fprintf (outf
, "%s%s# ", s_indent
, prefix
);
1381 print_generic_stmt (outf
, phi
, 0);
1384 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
1386 fprintf (outf
, "%s%s%d ", s_indent
, prefix
, get_lineno (bsi_stmt (si
)));
1387 print_generic_stmt (outf
, bsi_stmt (si
), 0);
1388 fprintf (outf
, "\n");
1393 /* Dump a basic block on stderr. */
1396 debug_tree_bb (basic_block bb
)
1398 dump_tree_bb (stderr
, "", bb
, 0);
1401 /* Dump a basic block N on stderr. */
1404 debug_tree_bb_n (int n
)
1406 debug_tree_bb (BASIC_BLOCK (n
));
1407 return BASIC_BLOCK (n
);
1410 /* Dump the CFG on stderr.
1412 FLAGS are the same used by the tree dumping functions
1413 (see TDF_* in tree.h). */
1416 debug_tree_cfg (int flags
)
1418 dump_tree_cfg (stderr
, flags
);
1422 /* Dump the program showing basic block boundaries on the given FILE.
1424 FLAGS are the same used by the tree dumping functions (see TDF_* in
1428 dump_tree_cfg (FILE *file
, int flags
)
1432 if (flags
& TDF_DETAILS
)
1434 const char *funcname
1435 = (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2);
1438 fprintf (file
, ";; Function %s\n\n", funcname
);
1439 fprintf (file
, ";; \n%d basic blocks, %d edges, last basic block %d.\n",
1440 n_basic_blocks
, n_edges
, last_basic_block
);
1444 dump_tree_bb (file
, "", bb
, 0);
1449 if (flags
& TDF_STATS
)
1450 dump_cfg_stats (file
);
1452 if (n_basic_blocks
> 0)
1453 dump_cfg_function_to_file (current_function_decl
, file
, flags
|TDF_BLOCKS
);
1456 /* Dumps function FN to FILE, with details given by FLAGS. Function body is
1459 dump_cfg_function_to_file (tree fn
, FILE *file
, int flags
)
1463 block_stmt_iterator si
;
1465 int show_bb_headers
= flags
& TDF_BLOCKS
;
1467 flags
&= ~TDF_BLOCKS
;
1469 fprintf (file
, "\n;; Function %s",
1470 (*lang_hooks
.decl_printable_name
) (fn
, 2));
1471 fprintf (file
, " (%s)\n",
1472 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn
)));
1473 fprintf (file
, "\n");
1475 fprintf (file
, "%s (", (*lang_hooks
.decl_printable_name
) (fn
, 2));
1477 arg
= DECL_ARGUMENTS (fn
);
1480 print_generic_expr (file
, arg
, 0);
1481 if (TREE_CHAIN (arg
))
1482 fprintf (file
, ", ");
1483 arg
= TREE_CHAIN (arg
);
1485 fprintf (file
, ")\n");
1487 if (flags
& TDF_RECONSTRUCT
)
1489 struct control
*cs_tree
, **bb_to_cs
;
1490 bb_to_cs
= xmalloc (sizeof (struct control
*) * last_basic_block
);
1492 cs_tree
= determine_structures (bb_to_cs
);
1493 dump_cs_tree (file
, flags
, cs_tree
, bb_to_cs
);
1495 free_cs_tree (cs_tree
);
1500 fprintf (file
, "{\n");
1503 if (show_bb_headers
)
1505 fprintf (file
, "# BLOCK %d\n ", bb
->index
);
1506 fprintf (file
, "# PRED");
1507 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1508 dump_edge_info (file
, e
, 0);
1511 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
1513 fprintf (file
, "\t# ");
1514 print_generic_stmt (file
, phi
, flags
);
1515 fprintf (file
, "\n");
1518 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
1520 fprintf (file
, "%d\t", get_lineno (bsi_stmt (si
)));
1521 print_generic_stmt (file
, bsi_stmt (si
), flags
& ~TDF_VOPS
);
1522 fprintf (file
, "\n");
1525 if (show_bb_headers
)
1527 fprintf (file
, "# SUCC");
1528 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1529 dump_edge_info (file
, e
, 1);
1530 fprintf (file
, "\n\n");
1533 fprintf (file
, "}\n\n");
1537 /* Determine control structures, returning their tree and assignment of
1538 blocks to them in BELONGS_TO. */
1540 static struct control
*
1541 determine_structures (struct control
*bb_to_cs
[])
1543 struct control
*root
, *nw
, *old
;
1544 int i
, *rc_order
, *rc_number
;
1545 basic_block header
, follow
;
1547 dominance_info dom
= calculate_dominance_info (CDI_DOMINATORS
);
1549 root
= make_control_node (CS_TOP
, NULL
, ENTRY_BLOCK_PTR
->succ
->dest
, NULL
,
1552 rc_order
= xmalloc (n_basic_blocks
* sizeof (int));
1553 flow_depth_first_order_compute (NULL
, rc_order
);
1555 mark_dfs_back_edges ();
1557 rc_number
= xmalloc (last_basic_block
* sizeof (int));
1558 for (i
= 0; i
< n_basic_blocks
; i
++)
1560 bb_to_cs
[rc_order
[i
]] = root
;
1561 rc_number
[rc_order
[i
]] = i
;
1564 for (i
= 0; i
< n_basic_blocks
; i
++)
1566 header
= BASIC_BLOCK (rc_order
[i
]);
1569 for (e
= header
->pred
; e
; e
= e
->pred_next
)
1571 if (e
->flags
& EDGE_ABNORMAL
)
1577 if ((e
->flags
& EDGE_DFS_BACK
)
1578 && dominated_by_p (dom
, e
->src
, header
))
1581 /* Take the latch with the greatest rc number. */
1582 || rc_number
[e
->src
->index
] > rc_number
[latch
->src
->index
])
1590 nw
= make_control_node (CS_INFINITE_LOOP
, bb_to_cs
[header
->index
],
1591 header
, latch
->src
, NULL
);
1593 /* Mark the nodes belonging to the loop. */
1594 DFS_BACKWARD (latch
->src
, bb
, e
,
1596 bb_to_cs
[bb
->index
] = nw
,);
1598 /* Determine follow and the type of the loop. If the header has
1599 a successor outside of loop, it is a while loop and the successor is
1602 for (e
= header
->succ
; e
; e
= e
->succ_next
)
1603 if (e
->dest
== EXIT_BLOCK_PTR
1604 || bb_to_cs
[e
->dest
->index
] != nw
)
1608 nw
->type
= CS_WHILE_LOOP
;
1609 nw
->follow
= follow
;
1613 /* If latch has a successor outside of loop, it is a do-while loop. */
1615 for (e
= latch
->src
->succ
; e
; e
= e
->succ_next
)
1616 if (e
->dest
== EXIT_BLOCK_PTR
1617 || bb_to_cs
[e
->dest
->index
] != nw
)
1621 nw
->type
= CS_DO_WHILE_LOOP
;
1622 nw
->follow
= follow
;
1626 /* Otherwise it is an endless loop. Set follow as the successor with the
1627 lowest rc number. */
1629 DFS_BACKWARD (latch
->src
, bb
, f
,
1630 bb_to_cs
[f
->dest
->index
] != nw
,
1631 if (bb_to_cs
[bb
->index
] != nw
1633 || rc_number
[follow
->index
] > rc_number
[bb
->index
]))
1636 nw
->follow
= follow
;
1639 /* Now we have a loop tree. Add information about if then else
1640 structures. Traverse the basic blocks in the completion order,
1641 so that inner structures are identified first. */
1642 for (i
= n_basic_blocks
- 1; i
>= 0; i
--)
1645 edge e_then
, e_else
;
1649 header
= BASIC_BLOCK (rc_order
[i
]);
1650 stmt
= last_stmt (header
);
1652 if (!stmt
|| TREE_CODE (stmt
) != COND_EXPR
)
1655 e_then
= e_else
= NULL
;
1656 for (e
= header
->succ
; e
; e
= e
->succ_next
)
1658 if (e
->flags
& EDGE_TRUE_VALUE
)
1660 if (e
->flags
& EDGE_FALSE_VALUE
)
1664 if (!e_then
|| !e_else
|| e_then
== e_else
1665 || e_then
->dest
== header
1666 || e_else
->dest
== header
)
1669 /* Clasify the structure. */
1671 old
= bb_to_cs
[header
->index
];
1672 if (dominated_by_p (dom
, e_then
->dest
, header
))
1674 for (e
= e_else
->dest
->pred
; e
; e
= e
->pred_next
)
1675 if (dominated_by_p (dom
, e
->src
, e_then
->dest
))
1680 if (!inside_cs (e
->src
, old
, bb_to_cs
))
1683 follow
= e_else
->dest
;
1686 else if (dominated_by_p (dom
, e_else
->dest
, header
))
1688 if (!inside_cs (e_then
->dest
, old
, bb_to_cs
)
1689 || !inside_cs (e_else
->dest
, old
, bb_to_cs
))
1691 type
= CS_IF_THEN_ELSE
;
1698 else if (dominated_by_p (dom
, e_else
->dest
, header
))
1700 for (e
= e_then
->dest
->pred
; e
; e
= e
->pred_next
)
1701 if (dominated_by_p (dom
, e
->src
, e_else
->dest
))
1706 if (!inside_cs (e
->src
, old
, bb_to_cs
))
1709 follow
= e_then
->dest
;
1718 nw
= make_control_node (type
, bb_to_cs
[header
->index
],
1721 /* Find the basic blocks belonging to the structure. */
1722 bb_to_cs
[header
->index
] = nw
;
1723 if (type
== CS_IF_THEN
|| type
== CS_IF_THEN_ELSE
)
1724 DFS_FORWARD (e_then
->dest
, bb
, e
,
1725 dominated_by_p (dom
, e
->dest
, e_then
->dest
)
1726 && inside_cs (e
->dest
, old
, bb_to_cs
),
1727 if (bb_to_cs
[bb
->index
] == old
)
1728 bb_to_cs
[bb
->index
] = nw
;
1730 try_move_control_node (bb_to_cs
[bb
->index
], old
, nw
);,);
1731 if (type
== CS_IF_ELSE
|| type
== CS_IF_THEN_ELSE
)
1732 DFS_FORWARD (e_else
->dest
, bb
, e
,
1733 dominated_by_p (dom
, e
->dest
, e_else
->dest
)
1734 && inside_cs (e
->dest
, old
, bb_to_cs
),
1735 if (bb_to_cs
[bb
->index
] == old
)
1736 bb_to_cs
[bb
->index
] = nw
;
1738 try_move_control_node (bb_to_cs
[bb
->index
], old
, nw
);,);
1739 if (type
== CS_IF_THEN_ELSE
)
1741 /* Determine the follow node as the basic block that has predecessors
1742 in both branches. */
1743 DFS_FORWARD (e_then
->dest
, bb
, f
,
1744 dominated_by_p (dom
, f
->src
, e_then
->dest
)
1745 && inside_cs (f
->src
, old
, bb_to_cs
),
1746 if (bb_to_cs
[bb
->index
] != nw
)
1748 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1749 if (bb_to_cs
[e
->src
->index
] == nw
1750 && dominated_by_p (dom
, e
->src
, e_else
->dest
)
1752 || rc_number
[follow
->index
] > rc_number
[bb
->index
]))
1761 free_dominance_info (dom
);
1766 /* Releases memory occupied by control structures tree CS_TREE. */
1769 free_cs_tree (struct control
*cs_tree
)
1771 struct control
*son
, *next
;
1776 son
->prev
->next
= NULL
;
1778 for (; son
; son
= next
)
1788 /* Dumps the program to FILE with details described by FLAGS. Structures are
1789 recreated according to CS_TREE. Blocks are assigned to structures according
1793 dump_cs_tree (FILE *file
, int flags
, struct control
*cs_tree
,
1794 struct control
*bb_to_cs
[])
1798 add_bb_cs_nodes (bb_to_cs
);
1799 assign_levels (cs_tree
, 0);
1800 compute_structure_exits (bb_to_cs
);
1801 layout_structures (cs_tree
, bb_to_cs
);
1802 fixup_gotos (cs_tree
, bb_to_cs
, NULL
, NULL
, NULL
);
1804 body
= reconstruct_tree (cs_tree
, bb_to_cs
);
1805 print_generic_stmt (file
, body
, flags
);
1808 /* Adds or removes gotos and labels as needed for structures in NODE.
1809 NEXT is the next structure after NODE in layout. BREAK_DEST and CONT_DEST
1810 are the blocks to that continue/break statements would go. BB_TO_CS maps
1811 basic blocks to control structures. */
1814 fixup_gotos (struct control
*node
, struct control
*bb_to_cs
[],
1815 struct control
*next
, basic_block break_dest
,
1816 basic_block cont_dest
)
1819 basic_block fall
, bb
;
1820 edge e_then
, e_else
, e
;
1825 for (s
= node
->lay_head
; s
; s
= s
->lay_next
)
1826 fixup_gotos (s
, bb_to_cs
, s
->lay_next
, NULL
, NULL
);
1830 case CS_DO_WHILE_LOOP
:
1831 case CS_INFINITE_LOOP
:
1832 break_dest
= next
? first_cs_bb (next
) : NULL
;
1833 cont_dest
= first_cs_bb (node
->lay_head
);
1834 for (s
= node
->lay_head
; s
; s
= s
->lay_next
)
1835 fixup_gotos (s
, bb_to_cs
, s
->lay_next
? s
->lay_next
: node
->lay_head
,
1836 break_dest
, cont_dest
);
1841 node
->lay_head
->fa_then
= node
->lay_head
->fa_else
= ADD_GOTO
;
1842 for (s
= node
->lay_head
->lay_next
; s
; s
= s
->lay_next
)
1843 fixup_gotos (s
, bb_to_cs
, s
->lay_next
? s
->lay_next
: next
,
1844 break_dest
, cont_dest
);
1847 case CS_IF_THEN_ELSE
:
1848 node
->lay_head
->fa_then
= node
->lay_head
->fa_else
= ADD_GOTO
;
1849 for (s
= node
->lay_head
->lay_next
; s
!= node
->else_node
; s
= s
->lay_next
)
1850 fixup_gotos (s
, bb_to_cs
,
1851 s
->lay_next
!= node
->else_node
? s
->lay_next
: next
,
1852 break_dest
, cont_dest
);
1853 for (; s
; s
= s
->lay_next
)
1854 fixup_gotos (s
, bb_to_cs
, s
->lay_next
? s
->lay_next
: next
,
1855 break_dest
, cont_dest
);
1863 if (!bb
->succ
->succ_next
)
1870 e_then
= e_else
= NULL
;
1871 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1872 if (e
->flags
& EDGE_TRUE_VALUE
)
1874 else if (e
->flags
& EDGE_FALSE_VALUE
)
1876 else if (e
->flags
& EDGE_FALLTHRU
)
1879 if (!e_then
&& !e_else
)
1883 fall
= next
? first_cs_bb (next
) : NULL
;
1885 node
->fa_then
= decide_final_action (e_then
->dest
, fall
, break_dest
,
1886 cont_dest
, bb_to_cs
);
1888 node
->fa_else
= decide_final_action (e_else
->dest
, fall
, break_dest
,
1889 cont_dest
, bb_to_cs
);
1897 /* Decides how to replace goto to basic block BB. FALL is the block to that
1898 it would fallthru, BREAK_DEST and CONT_DEST are destinations of break and
1899 continue statements. BB_TO_CS maps basic blocks to control structures. */
1902 decide_final_action (basic_block bb
, basic_block fall
, basic_block break_dest
,
1903 basic_block cont_dest
, struct control
*bb_to_cs
[])
1906 block_stmt_iterator bsi
;
1908 if (bb
== fall
|| bb
== EXIT_BLOCK_PTR
)
1909 return MAKE_FALLTHRU
;
1910 else if (bb
== break_dest
)
1912 else if (bb
== cont_dest
)
1913 return ADD_CONTINUE
;
1916 s
= bb_to_cs
[bb
->index
];
1917 bsi
= bsi_start (bb
);
1919 if (!s
->artificial_label
1921 || TREE_CODE (bsi_stmt (bsi
)) != LABEL_EXPR
))
1922 s
->artificial_label
= build_new_label ();
1928 /* Create a structured program from structrure description and layout
1929 in NODE. BB_TO_CS maps basic blocks to control structures. */
1932 reconstruct_tree (struct control
*node
, struct control
*bb_to_cs
[])
1934 tree b
= NULL_TREE
, tmp
, *stmt
, b1
;
1935 tree_stmt_iterator tsi
= tsi_start (&b
), atsi
;
1936 block_stmt_iterator bsi
;
1939 edge e
, e_then
, e_else
;
1944 for (s
= node
->lay_head
; s
; s
= s
->lay_next
)
1946 tmp
= reconstruct_tree (s
, bb_to_cs
);
1947 tsi_link_chain_after (&tsi
, tmp
, TSI_CONTINUE_LINKING
);
1949 return build (BIND_EXPR
, void_type_node
,
1955 case CS_DO_WHILE_LOOP
:
1956 case CS_INFINITE_LOOP
:
1957 for (s
= node
->lay_head
; s
; s
= s
->lay_next
)
1959 tmp
= reconstruct_tree (s
, bb_to_cs
);
1960 tsi_link_chain_after (&tsi
, tmp
, TSI_CONTINUE_LINKING
);
1962 return build (LOOP_EXPR
, void_type_node
, b
);
1966 for (s
= node
->lay_head
->lay_next
; s
; s
= s
->lay_next
)
1968 tmp
= reconstruct_tree (s
, bb_to_cs
);
1969 tsi_link_chain_after (&tsi
, tmp
, TSI_CONTINUE_LINKING
);
1971 tmp
= reconstruct_tree (node
->lay_head
, bb_to_cs
);
1972 for (atsi
= tsi_start (&tmp
); !tsi_one_before_end_p (atsi
);
1975 stmt
= tsi_stmt_ptr (atsi
);
1976 if (TREE_CODE (*stmt
) != COND_EXPR
)
1979 if (node
->type
== CS_IF_THEN
)
1981 *stmt
= build (COND_EXPR
, void_type_node
,
1982 COND_EXPR_COND (*stmt
), b
,
1985 : COND_EXPR_ELSE (*stmt
)));
1989 *stmt
= build (COND_EXPR
, void_type_node
,
1990 COND_EXPR_COND (*stmt
),
1993 : COND_EXPR_THEN (*stmt
)),
1998 case CS_IF_THEN_ELSE
:
1999 for (s
= node
->lay_head
->lay_next
; s
!= node
->else_node
; s
= s
->lay_next
)
2001 tmp
= reconstruct_tree (s
, bb_to_cs
);
2002 tsi_link_chain_after (&tsi
, tmp
, TSI_CONTINUE_LINKING
);
2007 tsi
= tsi_start (&b
);
2008 for (; s
; s
= s
->lay_next
)
2010 tmp
= reconstruct_tree (s
, bb_to_cs
);
2011 tsi_link_chain_after (&tsi
, tmp
, TSI_CONTINUE_LINKING
);
2014 tmp
= reconstruct_tree (node
->lay_head
, bb_to_cs
);
2015 for (atsi
= tsi_start (&tmp
); !tsi_one_before_end_p (atsi
);
2018 stmt
= tsi_stmt_ptr (atsi
);
2019 if (TREE_CODE (*stmt
) != COND_EXPR
)
2022 *stmt
= build (COND_EXPR
, void_type_node
,
2023 COND_EXPR_COND (*stmt
), b1
, b
);
2028 if (node
->artificial_label
)
2029 tsi_link_after (&tsi
, build1 (LABEL_EXPR
, void_type_node
,
2030 s
->artificial_label
), TSI_NEW_STMT
);
2031 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
2033 if (TREE_CODE (bsi_stmt (bsi
)) == GOTO_EXPR
2034 || TREE_CODE (bsi_stmt (bsi
)) == COND_EXPR
)
2036 tsi_link_after (&tsi
, bsi_stmt (bsi
), TSI_NEW_STMT
);
2038 tmp
= bsi_end_p (bsi
) ? NULL_TREE
: bsi_stmt (bsi
);
2039 if (tmp
&& TREE_CODE (tmp
) == COND_EXPR
)
2040 tmp
= build (COND_EXPR
, void_type_node
, COND_EXPR_COND (tmp
),
2041 COND_EXPR_THEN (tmp
), COND_EXPR_ELSE (tmp
));
2045 if (!bb
->succ
->succ_next
)
2052 e_then
= e_else
= NULL
;
2053 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2054 if (e
->flags
& EDGE_TRUE_VALUE
)
2056 else if (e
->flags
& EDGE_FALSE_VALUE
)
2058 else if (e
->flags
& EDGE_FALLTHRU
)
2064 if (tmp
&& TREE_CODE (tmp
) == COND_EXPR
)
2065 stmt
= &COND_EXPR_THEN (tmp
);
2068 establish_gotos (stmt
, node
->fa_then
,
2069 bb_to_cs
[e_then
->dest
->index
]);
2073 establish_gotos (&COND_EXPR_ELSE (tmp
), node
->fa_else
,
2074 bb_to_cs
[e_else
->dest
->index
]);
2077 tsi_link_after (&tsi
, tmp
, TSI_NEW_STMT
);
2080 b
= build_empty_stmt ();
2089 /* Set gotos of STMT according to ACTION; the jump goes to DEST. */
2092 establish_gotos (tree
*stmt
, enum fa action
, struct control
*dest
)
2107 if (dest
->artificial_label
)
2108 *stmt
= build1 (GOTO_EXPR
, void_type_node
, dest
->artificial_label
);
2110 *stmt
= build1 (GOTO_EXPR
, void_type_node
, tree_block_label (dst
));
2115 *stmt
= build (BREAK_EXPR
, void_type_node
);
2119 *stmt
= build (CONTINUE_EXPR
, void_type_node
);
2124 /* Checks whether BB belongs to NODE according to BB_TO_CS. */
2127 inside_cs (basic_block bb
, struct control
*node
, struct control
*bb_to_cs
[])
2129 struct control
*a
= bb_to_cs
[bb
->index
];
2131 while (a
!= node
&& a
)
2137 /* Returns the superstructure of NODE on level L. */
2139 static struct control
*
2140 lift (struct control
*node
, int l
)
2142 while (node
->level
> l
)
2148 /* Returns first basic block of the NODE. */
2151 first_cs_bb (struct control
*node
)
2153 while (node
->type
!= CS_BB
)
2154 node
= node
->lay_head
;
2156 return node
->header
;
2159 /* Add structure nodes for basic blocks, using BB_TO_CS array. */
2162 add_bb_cs_nodes (struct control
*bb_to_cs
[])
2164 basic_block bb
, follow
;
2170 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2171 if (e
->flags
& EDGE_FALLTHRU
)
2173 if (e
&& e
->dest
!= EXIT_BLOCK_PTR
)
2177 nw
= make_control_node (CS_BB
, bb_to_cs
[bb
->index
], bb
, NULL
, follow
);
2178 bb_to_cs
[bb
->index
] = nw
;
2182 /* Assign level L to NODE and propagate the levels to its subnodes. */
2185 assign_levels (struct control
*node
, int l
)
2187 struct control
*son
;
2196 assign_levels (son
, l
+ 1);
2199 while (son
!= node
->son
);
2202 /* Compute exits from the structures using the BB_TO_CS array. */
2205 compute_structure_exits (struct control
*bb_to_cs
[])
2209 struct control
*cs
, *ct
;
2213 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2215 if (e
->dest
== EXIT_BLOCK_PTR
)
2218 cs
= bb_to_cs
[bb
->index
];
2219 ct
= bb_to_cs
[e
->dest
->index
];
2221 while (ct
->level
> cs
->level
)
2223 while (cs
->level
> ct
->level
)
2225 while (ct
->outer
!= cs
->outer
)
2231 VARRAY_PUSH_GENERIC_PTR (cs
->exits
, ct
);
2236 /* Layout substructures of NODE in dfs order preferring the follow edges,
2237 using the BB_TO_CS array. */
2240 layout_structures (struct control
*node
, struct control
*bb_to_cs
[])
2242 struct control
*header_node
, *latch_node
, *then_node
, *else_node
, *son
;
2243 edge e_then
, e_else
, e
;
2245 if (node
->type
== CS_BB
)
2248 header_node
= lift (bb_to_cs
[node
->header
->index
], node
->level
+ 1);
2250 latch_node
= lift (bb_to_cs
[node
->latch
->index
], node
->level
+ 1);
2257 case CS_DO_WHILE_LOOP
:
2258 case CS_INFINITE_LOOP
:
2262 layout_structure (header_node
, latch_node
, bb_to_cs
);
2263 if (latch_node
&& latch_node
!= header_node
)
2264 add_to_layout (latch_node
);
2266 case CS_IF_THEN_ELSE
:
2267 e_then
= e_else
= NULL
;
2268 for (e
= node
->header
->succ
; e
; e
= e
->succ_next
)
2270 if (e
->flags
& EDGE_TRUE_VALUE
)
2272 if (e
->flags
& EDGE_FALSE_VALUE
)
2275 then_node
= lift (bb_to_cs
[e_then
->dest
->index
], node
->level
+ 1);
2276 else_node
= lift (bb_to_cs
[e_else
->dest
->index
], node
->level
+ 1);
2277 node
->else_node
= else_node
;
2279 add_to_layout (header_node
);
2280 layout_structure (then_node
, NULL
, bb_to_cs
);
2281 layout_structure (else_node
, NULL
, bb_to_cs
);
2288 if (node
->type
!= CS_TOP
)
2289 finish_layout (node
);
2294 layout_structures (son
, bb_to_cs
);
2297 while (son
!= node
->son
);
2300 /* Layout area reachable by dfs from HEADER, excluding the LATCH. Uses
2301 the BB_TO_CS array. */
2304 layout_structure (struct control
*header
, struct control
*latch
,
2305 struct control
*bb_to_cs
[])
2310 add_to_layout (header
);
2313 && inside_cs (header
->follow
, header
->outer
, bb_to_cs
))
2315 cs
= lift (bb_to_cs
[header
->follow
->index
], header
->level
);
2316 header
->follow_node
= cs
;
2317 if (cs
!= latch
&& !cs
->visited
)
2318 layout_structure (cs
, latch
, bb_to_cs
);
2321 for (i
= 0; i
< (int) VARRAY_ACTIVE_SIZE (header
->exits
); i
++)
2323 cs
= VARRAY_GENERIC_PTR (header
->exits
, i
);
2329 layout_structure (cs
, latch
, bb_to_cs
);
2333 /* Add a NODE at the end of current layout. */
2336 add_to_layout (struct control
*node
)
2338 struct control
*f
= node
->outer
;
2341 && f
->lay_last
->follow_node
!= node
)
2342 f
->lay_last
->follow_node
= NULL
;
2344 node
->lay_next
= NULL
;
2346 f
->lay_last
->lay_next
= node
;
2351 node
->visited
= true;
2354 /* Perform actions when NODE's substructures layout is fully determined. */
2357 finish_layout (struct control
*node
)
2360 node
->lay_last
->follow_node
= NULL
;
2363 /* Creates a new control node of type TYPE with father node OUTER, entry node
2364 HEADER, latch node LATCH and follow node FOLLOW. */
2366 static struct control
*
2367 make_control_node (enum cs_type type
, struct control
*outer
,
2368 basic_block header
, basic_block latch
, basic_block follow
)
2370 struct control
*nw
= xcalloc (1, sizeof (struct control
));
2377 nw
->next
= outer
->son
;
2379 nw
->prev
= outer
->son
->prev
;
2383 nw
->prev
->next
= nw
;
2384 nw
->next
->prev
= nw
;
2388 nw
->next
= nw
->prev
= NULL
;
2390 nw
->header
= header
;
2392 nw
->follow
= follow
;
2393 VARRAY_GENERIC_PTR_INIT (nw
->exits
, 2, "node_exits");
2398 /* Checks whether NODE is a substructure of OLD but not NW. If so,
2402 try_move_control_node (struct control
*node
, struct control
*old
,
2405 while (node
->outer
!= old
)
2409 move_control_node (node
, nw
);
2412 /* Makes TO an outer node of NODE. */
2415 move_control_node (struct control
*node
, struct control
*to
)
2417 if (node
->outer
->son
== node
)
2418 node
->outer
->son
= node
->next
;
2420 if (node
->outer
->son
== node
)
2421 node
->outer
->son
= NULL
;
2423 node
->prev
->next
= node
->next
;
2424 node
->next
->prev
= node
->prev
;
2427 node
->next
= to
->son
;
2430 node
->prev
= to
->son
->prev
;
2434 node
->prev
->next
= node
;
2435 node
->next
->prev
= node
;
2439 /* Dump CFG statistics on FILE. */
2442 dump_cfg_stats (FILE *file
)
2444 static long max_num_merged_cases
= 0;
2445 static long max_num_merged_labels
= 0;
2446 unsigned long size
, total
= 0;
2449 const char * const fmt_str
= "%-30s%-13s%12s\n";
2450 const char * const fmt_str_1
= "%-30s%13lu%11lu%c\n";
2451 const char * const fmt_str_3
= "%-43s%11lu%c\n";
2452 const char *funcname
2453 = (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2);
2456 fprintf (file
, "\nCFG Statistics for %s\n\n", funcname
);
2458 fprintf (file
, "---------------------------------------------------------\n");
2459 fprintf (file
, fmt_str
, "", " Number of ", "Memory");
2460 fprintf (file
, fmt_str
, "", " instances ", "used ");
2461 fprintf (file
, "---------------------------------------------------------\n");
2463 size
= n_basic_blocks
* sizeof (struct basic_block_def
);
2465 fprintf (file
, fmt_str_1
, "Basic blocks", n_basic_blocks
, SCALE (size
),
2472 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2475 size
= n_edges
* sizeof (struct edge_def
);
2477 fprintf (file
, fmt_str_1
, "Edges", n_edges
, SCALE (size
), LABEL (size
));
2479 size
= n_basic_blocks
* sizeof (struct bb_ann_d
);
2481 fprintf (file
, fmt_str_1
, "Basic block annotations", n_basic_blocks
,
2482 SCALE (size
), LABEL (size
));
2484 fprintf (file
, "---------------------------------------------------------\n");
2485 fprintf (file
, fmt_str_3
, "Total memory used by CFG data", SCALE (total
),
2487 fprintf (file
, "---------------------------------------------------------\n");
2488 fprintf (file
, "\n");
2490 if (cfg_stats
.num_merged_labels
> max_num_merged_labels
)
2491 max_num_merged_labels
= cfg_stats
.num_merged_labels
;
2493 fprintf (file
, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2494 cfg_stats
.num_merged_labels
, max_num_merged_labels
);
2497 if (cfg_stats
.num_merged_cases
> max_num_merged_cases
)
2498 max_num_merged_cases
= cfg_stats
.num_merged_cases
;
2500 fprintf (file
, "Coalesced case label blocks: %ld (Max so far: %ld)\n",
2501 cfg_stats
.num_merged_cases
, max_num_merged_cases
);
2503 fprintf (file
, "Number of unnecessary blocks created due to lexical scopes: %ld (%.0f%%)\n",
2504 cfg_stats
.num_failed_bind_expr_merges
,
2505 PERCENT (cfg_stats
.num_failed_bind_expr_merges
, n_basic_blocks
));
2507 fprintf (file
, "\n");
2511 /* Dump CFG statistics on stderr. */
2514 debug_cfg_stats (void)
2516 dump_cfg_stats (stderr
);
2520 /* Dump the flowgraph to a .dot FILE. */
2523 tree_cfg2dot (FILE *file
)
2527 const char *funcname
2528 = (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2);
2530 /* Write the file header. */
2531 fprintf (file
, "digraph %s\n{\n", funcname
);
2533 /* Write blocks and edges. */
2534 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
2536 fprintf (file
, "\tENTRY -> %d", e
->dest
->index
);
2538 if (e
->flags
& EDGE_FAKE
)
2539 fprintf (file
, " [weight=0, style=dotted]");
2541 fprintf (file
, ";\n");
2547 enum tree_code head_code
, end_code
;
2548 const char *head_name
, *end_name
;
2551 tree first
= first_stmt (bb
);
2552 tree last
= last_stmt (bb
);
2556 head_code
= TREE_CODE (first
);
2557 head_name
= tree_code_name
[head_code
];
2558 head_line
= get_lineno (bb
->head_tree
->stmt
);
2561 head_name
= "no-statement";
2565 end_code
= TREE_CODE (last
);
2566 end_name
= tree_code_name
[end_code
];
2567 end_line
= get_lineno (bb
->end_tree
->stmt
);
2570 end_name
= "no-statement";
2573 fprintf (file
, "\t%d [label=\"#%d\\n%s (%d)\\n%s (%d)\"];\n",
2574 bb
->index
, bb
->index
, head_name
, head_line
, end_name
,
2577 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
2579 if (e
->dest
== EXIT_BLOCK_PTR
)
2580 fprintf (file
, "\t%d -> EXIT", bb
->index
);
2582 fprintf (file
, "\t%d -> %d", bb
->index
, e
->dest
->index
);
2584 if (e
->flags
& EDGE_FAKE
)
2585 fprintf (file
, " [weight=0, style=dotted]");
2587 fprintf (file
, ";\n");
2590 if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
2594 fputs ("}\n\n", file
);
2599 /*---------------------------------------------------------------------------
2600 Miscellaneous helpers
2601 ---------------------------------------------------------------------------*/
2604 /* Return true if T represents a stmt that always transfers control. */
2607 is_ctrl_stmt (tree t
)
2609 return (TREE_CODE (t
) == COND_EXPR
2610 || TREE_CODE (t
) == SWITCH_EXPR
2611 || TREE_CODE (t
) == GOTO_EXPR
2612 || TREE_CODE (t
) == RETURN_EXPR
2613 || TREE_CODE (t
) == RESX_EXPR
);
2616 /* Return true if T is a stmt that may or may not alter the flow of control
2617 (i.e., a call to a non-returning function). */
2620 is_ctrl_altering_stmt (tree t
)
2624 #if defined ENABLE_CHECKING
2629 switch (TREE_CODE (t
))
2632 /* A MODIFY_EXPR with a rhs of a call has the characteristics
2634 call
= TREE_OPERAND (t
, 1);
2635 if (TREE_CODE (call
) != CALL_EXPR
)
2640 /* A CALL_EXPR alters flow control if the current function has
2642 if (FUNCTION_RECEIVES_NONLOCAL_GOTO (current_function_decl
))
2645 /* A CALL_EXPR also alters flow control if it does not return. */
2646 if (call_expr_flags (call
) & (ECF_NORETURN
| ECF_LONGJMP
))
2654 /* If a statement can throw, it alters control flow. */
2655 return tree_can_throw_internal (t
);
2659 /* Return flags associated with the function called by T (see ECF_* in
2663 call_expr_flags (tree t
)
2666 tree decl
= get_callee_fndecl (t
);
2669 flags
= flags_from_decl_or_type (decl
);
2672 t
= TREE_OPERAND (t
, 0);
2673 flags
= flags_from_decl_or_type (TREE_TYPE (TREE_TYPE (t
)));
2680 /* Return true if T is a computed goto. */
2683 is_computed_goto (tree t
)
2685 return (TREE_CODE (t
) == GOTO_EXPR
2686 && TREE_CODE (GOTO_DESTINATION (t
)) != LABEL_DECL
);
2689 /* Return true if T should start a new basic block. PREV_T is the
2690 statement preceding T. It is used when T is a label or a case label.
2691 Labels should only start a new basic block if their previous statement
2692 wasn't a label. Otherwise, sequence of labels would generate
2693 unnecessary basic blocks that only contain a single label. */
2696 stmt_starts_bb_p (tree t
, tree prev_t
)
2698 enum tree_code code
;
2703 /* LABEL_EXPRs and CASE_LABEL_EXPRs start a new basic block only if the
2704 preceding statement wasn't a label of the same type. This prevents
2705 the creation of consecutive blocks that have nothing but a single
2707 code
= TREE_CODE (t
);
2708 if (code
== LABEL_EXPR
|| code
== CASE_LABEL_EXPR
)
2710 /* Nonlocal and computed GOTO targets always start a new block. */
2711 if (code
== LABEL_EXPR
2712 && (NONLOCAL_LABEL (LABEL_EXPR_LABEL (t
))
2713 || FORCED_LABEL (LABEL_EXPR_LABEL (t
))))
2716 if (prev_t
&& TREE_CODE (prev_t
) == code
)
2718 if (code
== LABEL_EXPR
)
2719 cfg_stats
.num_merged_labels
++;
2721 cfg_stats
.num_merged_cases
++;
2733 /* Return true if T should end a basic block. */
2736 stmt_ends_bb_p (tree t
)
2738 return (is_ctrl_stmt (t
)
2739 || is_ctrl_altering_stmt (t
));
2743 /* Remove all the blocks and edges that make up the flowgraph. */
2746 delete_tree_cfg (void)
2748 if (n_basic_blocks
> 0)
2749 free_blocks_annotations ();
2751 free_basic_block_vars (0);
2755 /* Return the first statement in basic block BB, stripped of any NOP
2759 first_stmt (basic_block bb
)
2761 block_stmt_iterator i
;
2763 if (bb
== ENTRY_BLOCK_PTR
2764 || bb
== EXIT_BLOCK_PTR
)
2768 return (!bsi_end_p (i
)) ? bsi_stmt (i
) : NULL_TREE
;
2771 /* Finds a label in basic block BB or creates one if it does not exit. */
2773 tree_block_label (basic_block bb
)
2775 tree label
= first_stmt (bb
);
2777 if (!label
|| TREE_CODE (label
) != LABEL_EXPR
)
2779 block_stmt_iterator first
= bsi_start (bb
);
2780 tree label_decl
= build_new_label ();
2782 label
= build1 (LABEL_EXPR
, void_type_node
, label_decl
);
2783 bsi_insert_before (&first
, label
, BSI_NEW_STMT
);
2786 return LABEL_EXPR_LABEL (label
);
2789 /* Return the last statement in basic block BB. */
2792 last_stmt (basic_block bb
)
2794 block_stmt_iterator b
;
2796 if (bb
== ENTRY_BLOCK_PTR
2797 || bb
== EXIT_BLOCK_PTR
)
2801 return (!bsi_end_p (b
)) ? bsi_stmt (b
) : NULL_TREE
;
2804 /* Return the pointer to last statement in basic block BB. */
2807 last_stmt_ptr (basic_block bb
)
2809 block_stmt_iterator b
;
2812 return (!bsi_end_p (b
)) ? bsi_stmt_ptr (b
) : NULL
;
2815 /* Similar to tsi_start() but initializes the iterator at the first
2816 statement in basic block BB which isn't an empty statement node.
2818 NULL is returned if there are no such statements. */
2821 bsi_start (basic_block bb
)
2823 block_stmt_iterator i
;
2826 if (bb
&& bb
->index
!= INVALID_BLOCK
)
2827 i
.curr_stmt
= bb
->head_tree
;
2834 /* Returns bsi positioned so that insering before it will insert after the
2835 labels in the basic block bb. */
2838 bsi_real_start (basic_block bb
)
2840 block_stmt_iterator bsi
= bsi_start (bb
);
2842 for (; !bsi_end_p (bsi
); bsi_next (&bsi
))
2843 if (TREE_CODE (bsi_stmt (bsi
)) != LABEL_EXPR
)
2849 /* This routine will return a block iterator which points to the last stmt in
2850 a basic block, if there is one. */
2853 bsi_last (basic_block bb
)
2855 block_stmt_iterator i
;
2858 if (bb
&& bb
->index
!= INVALID_BLOCK
)
2859 i
.curr_stmt
= bb
->end_tree
;
2867 /* Find the previous iterator value. */
2870 bsi_prev (block_stmt_iterator
*i
)
2872 i
->curr_stmt
= i
->curr_stmt
->prev
;
2875 /* Insert statement T into basic block BB. */
2878 set_bb_for_stmt (tree t
, basic_block bb
)
2885 /* If the statement is a label, add the label to block-to-labels map
2886 so that we can speed up edge creation for GOTO_EXPRs. */
2887 if (TREE_CODE (t
) == LABEL_EXPR
)
2889 LABEL_DECL_INDEX (LABEL_EXPR_LABEL (t
))
2890 = VARRAY_ACTIVE_SIZE (label_to_block_map
);
2891 VARRAY_PUSH_BB (label_to_block_map
, bb
);
2894 ann
= get_stmt_ann (t
);
2899 /* Insert routines. */
2901 /* This routine inserts a stmt after the stmt iterator passed in.
2902 The final parameter determines whether the statement iterator
2903 is updated to point to the new stmt, or left pointing to the original
2907 bsi_insert_after (block_stmt_iterator
*curr_bsi
, tree t
,
2908 enum bsi_iterator_update mode
)
2910 basic_block bb
= curr_bsi
->bb
;
2911 tree_cell curr
, nw
= tree_cell_alloc (t
);
2913 set_bb_for_stmt (t
, bb
);
2914 curr
= bsi_cell (*curr_bsi
);
2918 /* Inserting after end of bb. Only valid if the bb is empty. */
2922 nw
->prev
= nw
->next
= NULL
;
2923 bb
->head_tree
= bb
->end_tree
= nw
;
2924 *curr_bsi
= bsi_start (bb
);
2928 nw
->next
= curr
->next
;
2932 nw
->next
->prev
= nw
;
2936 if (mode
== BSI_NEW_STMT
)
2937 bsi_next (curr_bsi
);
2939 /* Now update the required SSA bits. */
2946 /* This routine inserts a stmt before the stmt iterator passed in.
2947 The final parameter determines whether the statement iterator
2948 is updated to point to the new stmt, or left pointing to the original
2951 bsi_insert_before (block_stmt_iterator
*curr_bsi
, tree t
,
2952 enum bsi_iterator_update mode
)
2954 basic_block bb
= curr_bsi
->bb
;
2955 tree_cell curr
, nw
= tree_cell_alloc (t
);
2957 set_bb_for_stmt (t
, bb
);
2958 curr
= bsi_cell (*curr_bsi
);
2962 /* Inserting just after end of the basic block. */
2964 nw
->prev
= bb
->end_tree
;
2969 nw
->prev
->next
= nw
;
2972 if (mode
== BSI_NEW_STMT
)
2973 curr_bsi
->curr_stmt
= nw
;
2977 nw
->prev
= curr
->prev
;
2981 nw
->prev
->next
= nw
;
2985 if (mode
== BSI_NEW_STMT
)
2986 bsi_prev (curr_bsi
);
2988 /* Now update the required SSA bits. */
2992 /* This routine inserts a stmt on an edge. Every attempt is made to place the
2993 stmt in an existing basic block, but sometimes that isn't possible. When
2994 it isn't possible, a new basic block is created, edges updated, and the
2995 stmt is added to the new block. An iterator to the new stmt is returned.
2996 If a pointer to a BSI is passed in, and the stmt is inserted before or after
2997 an existing stmt in a block, old_bsi will be returned with an iterator for
2998 that stmt (The equivalent of BSI_SAME_STMT on an insert_before or after.
2999 If a created_block is passed in, and the edge is split, the new block is
3000 returned through this parameter. */
3003 bsi_insert_on_edge_immediate (edge e
, tree stmt
, block_stmt_iterator
*old_bsi
,
3004 basic_block
*created_block
)
3006 basic_block src
, dest
, new_bb
;
3007 block_stmt_iterator bsi
, tmp
;
3008 int num_exit
, num_entry
;
3014 old_bsi
->curr_stmt
= NULL
;
3016 *created_block
= (basic_block
)NULL
;
3021 /* Cannot insert on an abnormal edge. */
3022 if (e
->flags
& EDGE_ABNORMAL
)
3025 /* No immediate edge insertion if there are already pending inserts. */
3026 if (PENDING_STMT (e
))
3029 num_exit
= num_entry
= 0;
3031 for (e2
= src
->succ
; e2
; e2
= e2
->succ_next
)
3034 for (e2
= dest
->pred
; e2
; e2
= e2
->pred_next
)
3037 /* If src is a single-exit block, and it isn't the entry block, then
3038 insert at the end of the block, if we can. */
3040 if (num_exit
== 1 && src
!= ENTRY_BLOCK_PTR
)
3042 bsi
= bsi_last (src
);
3043 /* If it is an empty block, simply insert after this bsi, and the new stmt
3044 will become the only stmt in the block. */
3045 if (bsi_end_p (bsi
))
3047 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3050 last
= bsi_stmt (bsi
);
3052 /* If the last stmt isn't a control altering stmt, then we can simply
3053 append this stmt to the basic block. This should mean the edge is
3054 a fallthrough edge. */
3056 if (!is_ctrl_stmt (last
) && !is_ctrl_altering_stmt (last
))
3058 bsi_insert_after (&bsi
, stmt
, BSI_SAME_STMT
);
3065 /* If the last stmt is a GOTO, the we can simply insert before it. */
3066 if (TREE_CODE (last
) == GOTO_EXPR
)
3068 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3078 /* If dest is a single entry destination, and it isn't the exit block, the new
3079 stmt can be inserted at the beginning of the destination block. */
3081 if (num_entry
== 1 && dest
!= EXIT_BLOCK_PTR
)
3083 bsi
= bsi_start (dest
);
3084 /* If it is an empty block, simply insert after this bsi, and the new stmt
3085 will become the only stmt in the block. */
3086 if (bsi_end_p (bsi
))
3088 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3092 /* Skip any labels, and insert before the first non-label. */
3093 for (tmp
= bsi
, bsi_next (&bsi
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3095 if (!is_label_stmt (bsi_stmt (bsi
)))
3097 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3108 /* If this point is reached, then the block consists of nothing but
3109 labels, and tmp points to the last one. Insert after it. */
3110 bsi_insert_after (&tmp
, stmt
, BSI_SAME_STMT
);
3117 /* Otherwise, create a new basic block, and split this edge. */
3118 new_bb
= split_edge (e
);
3119 ann
= bb_ann (new_bb
);
3122 *created_block
= new_bb
;
3124 bsi
= bsi_start (new_bb
);
3126 && TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
)
3127 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3129 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
3134 /* This routine will commit all pending edge insertions, creating any new
3135 basic blocks which are necessary. The number of edges which were inserted
3136 is returned. If the flag update_annotations is true, then new bitmaps are
3137 created for the dominator children, and they are updated. If specified,
3138 new_blocks returned a count of the number of new basic blocks which were
3142 bsi_commit_edge_inserts (int update_annotations
, int *new_blocks
)
3145 block_stmt_iterator bsi
, ret
;
3147 tree stmt
, next_stmt
;
3148 int blocks
, count
= 0;
3150 blocks
= n_basic_blocks
;
3154 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3155 if (PENDING_STMT (e
))
3157 stmt
= PENDING_STMT (e
);
3158 SET_PENDING_STMT (e
, NULL_TREE
);
3159 next_stmt
= TREE_CHAIN (stmt
);
3160 /* The first insert will create a new basic block if needed. */
3161 ret
= bsi
= bsi_insert_on_edge_immediate (e
, stmt
, NULL
, NULL
);
3164 for ( ; stmt
; stmt
= next_stmt
)
3166 /* All further inserts can simply follow the first one. */
3167 next_stmt
= TREE_CHAIN (stmt
);
3168 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
3176 *new_blocks
= n_basic_blocks
- blocks
;
3178 /* Expand arrays if we created new blocks and need to update them. */
3179 if (update_annotations
&& blocks
!= n_basic_blocks
)
3182 /* TODO. Unimplemented at the moment. */
3188 /* This routine adds a stmt to the pending list on an edge. No actual
3189 insertion is made until a call to bsi_commit_edge_inserts () is made. */
3192 bsi_insert_on_edge (edge e
, tree stmt
)
3196 t
= PENDING_STMT (e
);
3198 SET_PENDING_STMT (e
, stmt
);
3201 for ( ; TREE_CHAIN (t
); t
= TREE_CHAIN (t
))
3203 TREE_CHAIN (t
) = stmt
;
3204 TREE_CHAIN (stmt
) = NULL_TREE
;
3209 /* Replace the statement TP1 with the statement TP2. */
3212 replace_stmt (tree_cell tp1
, tree t
)
3216 /* Relocate annotations for the replacement statement. */
3217 SET_EXPR_LOCUS (t
, EXPR_LOCUS (tp1
->stmt
));
3218 bb
= bb_for_stmt (tp1
->stmt
);
3220 set_bb_for_stmt (t
, bb
);
3223 /*---------------------------------------------------------------------------
3224 Tree specific functions for the cfg loop optimizer
3225 ---------------------------------------------------------------------------*/
3227 /* Split a (typically critical) edge. Return the new block.
3228 Abort on abnormal edges. */
3231 tree_split_edge (edge edge_in
)
3233 basic_block new_bb
, dest
;
3238 /* Abnormal edges cannot be split. */
3239 if (edge_in
->flags
& EDGE_ABNORMAL
)
3242 dest
= edge_in
->dest
;
3243 new_bb
= create_bb ();
3245 new_edge
= make_edge (new_bb
, dest
, EDGE_FALLTHRU
);
3246 /* Find all the PHI arguments on the original edge, and change them to
3247 the new edge. Do it now, since redirect_edge_and_branch would cancel
3248 the args otherwise. */
3249 for (phi
= phi_nodes (dest
); phi
; phi
= TREE_CHAIN (phi
))
3251 num_elem
= PHI_NUM_ARGS (phi
);
3252 for (i
= 0; i
< num_elem
; i
++)
3253 if (PHI_ARG_EDGE (phi
, i
) == edge_in
)
3255 PHI_ARG_EDGE (phi
, i
) = new_edge
;
3260 if (!redirect_edge_and_branch (edge_in
, new_bb
))
3262 tree_cleanup_block_edges (new_bb
, false);
3268 /* Splits the basic block BB into two after BSI and returns the created
3271 tree_split_block (basic_block bb
, block_stmt_iterator bsi
)
3273 basic_block new_bb
= create_bb ();
3277 /* Place the new block in front of the old one. */
3278 unlink_block (new_bb
);
3279 link_block (new_bb
, bb
->prev_bb
);
3281 new_bb
->head_tree
= bb
->head_tree
;
3282 new_bb
->end_tree
= bsi_cell (bsi
);
3284 end
= bsi_end_p (bsi
);
3288 end
= bsi_end_p (bsi
);
3293 /* Unless the block has only a fallthru edge, splitting the block
3294 would be wrong, as the edges coming from the control flow altering
3295 statement would be on the wrong place. */
3297 || bb
->succ
->succ_next
3298 || !(bb
->flags
& EDGE_FALLTHRU
))
3301 new_bb
->end_tree
= bb
->end_tree
;
3302 bb
->head_tree
= NULL
;
3303 bb
->end_tree
= NULL
;
3307 bb
->head_tree
= bsi_cell (bsi
);
3308 new_bb
->end_tree
->next
= NULL
;
3309 bb
->head_tree
->prev
= NULL
;
3312 new_bb
->pred
= bb
->pred
;
3314 for (e
= new_bb
->pred
; e
; e
= e
->pred_next
)
3317 return make_edge (new_bb
, bb
, EDGE_FALLTHRU
);
3320 /* Moves basic block BB after block AFTER. */
3322 tree_move_block_after (basic_block bb
, basic_block after
, int no_false_fallthru
)
3324 basic_block prev
= bb
->prev_bb
;
3326 if (bb
->prev_bb
== after
)
3330 link_block (bb
, after
);
3332 /* Fix up fallthru edges. */
3333 if (prev
&& prev
!= ENTRY_BLOCK_PTR
)
3334 tree_cleanup_block_edges (prev
, no_false_fallthru
);
3335 tree_cleanup_block_edges (bb
, no_false_fallthru
);
3336 tree_cleanup_block_edges (after
, no_false_fallthru
);
3339 /* Verifies that the flow information is OK. */
3342 tree_verify_flow_info (void)
3345 tree_cell stmt
, last
;
3346 tree t
, l
, label
, phi
;
3347 block_stmt_iterator si
;
3349 int err
= 0, degree
;
3357 for (stmt
= bb
->head_tree
; stmt
; last
= stmt
, stmt
= stmt
->next
)
3359 if (stmt
->prev
!= last
)
3361 fprintf (stderr
, "chain inconsistent in bb %d\n", bb
->index
);
3366 && stmt_ends_bb_p (last
->stmt
))
3368 fprintf (stderr
, "control statement in the middle of bb %d\n",
3373 if (TREE_CODE (stmt
->stmt
) == COMPOUND_EXPR
3374 || TREE_CODE (stmt
->stmt
) == CASE_LABEL_EXPR
3375 || TREE_CODE (stmt
->stmt
) == BIND_EXPR
)
3377 fprintf (stderr
, "forbidden statement in the middle of bb %d\n",
3382 if (bb_for_stmt (stmt
->stmt
) != bb
)
3384 fprintf (stderr
, "bb_for_stmt inconsistent in bb %d\n",
3389 if (last
!= bb
->end_tree
)
3391 fprintf (stderr
, "end_tree inconsistent in bb %d\n",
3397 for (stmt
= bb
->end_tree
; stmt
; last
= stmt
, stmt
= stmt
->prev
)
3398 if (stmt
->next
!= last
)
3400 fprintf (stderr
, "chain inconsistent in bb %d\n", bb
->index
);
3403 if (last
!= bb
->head_tree
)
3405 fprintf (stderr
, "head_tree inconsistent in bb %d\n",
3413 switch (TREE_CODE (t
))
3418 fprintf (stderr
, "goto without successors %d\n", bb
->index
);
3421 if (is_computed_goto (t
)
3422 || nonlocal_goto_p (t
))
3426 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3428 if (!(e
->flags
& EDGE_ABNORMAL
))
3432 fprintf (stderr
, "goto with more than one successor %d\n",
3440 if (e
->flags
& EDGE_FALLTHRU
)
3442 fprintf (stderr
, "goto with fallthru %d\n", bb
->index
);
3448 fprintf (stderr
, "goto without successors %d\n", bb
->index
);
3452 label
= GOTO_DESTINATION (t
);
3453 for (si
= bsi_start (le
->dest
);
3458 if (TREE_CODE (l
) == LABEL_EXPR
3459 && LABEL_EXPR_LABEL (l
) == label
)
3464 fprintf (stderr
, "%d: goto label not in dest block %d\n",
3465 bb
->index
, le
->dest
->index
);
3471 if (!bb
->succ
|| !bb
->succ
->succ_next
)
3473 fprintf (stderr
, "condition with less than 2 successors %d\n",
3479 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3481 if (e
->flags
& EDGE_FALLTHRU
)
3483 fprintf (stderr
, "condition with fallthru %d\n", bb
->index
);
3486 if (e
->flags
& EDGE_ABNORMAL
)
3489 if (e
->flags
& EDGE_TRUE_VALUE
)
3493 fprintf (stderr
, "more than one true edge %d\n",
3501 if (e
->flags
& EDGE_FALSE_VALUE
)
3505 fprintf (stderr
, "more than one false edge %d\n",
3515 fprintf (stderr
, "condition with no true edge %d\n",
3521 fprintf (stderr
, "condition with no false edge %d\n",
3526 label
= GOTO_DESTINATION (COND_EXPR_THEN (t
));
3527 for (si
= bsi_start (te
->dest
); !bsi_end_p (si
); bsi_next (&si
))
3530 if (TREE_CODE (l
) == LABEL_EXPR
3531 && LABEL_EXPR_LABEL (l
) == label
)
3536 fprintf (stderr
, "%d: goto label not in dest block %d\n",
3537 bb
->index
, te
->dest
->index
);
3541 label
= GOTO_DESTINATION (COND_EXPR_ELSE (t
));
3542 for (si
= bsi_start (fe
->dest
); !bsi_end_p (si
); bsi_next (&si
))
3545 if (TREE_CODE (l
) == LABEL_EXPR
3546 && LABEL_EXPR_LABEL (l
) == label
)
3551 fprintf (stderr
, "%d: goto label not in dest block %d\n",
3552 bb
->index
, fe
->dest
->index
);
3562 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
3564 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
3565 if (phi_arg_from_edge (phi
, e
) < 0)
3568 "No entry for edge in phi node: %d\n", bb
->index
);
3573 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
3574 if (PHI_NUM_ARGS (phi
) != degree
)
3577 "Superfluous entries in phi node: %d\n", bb
->index
);
3582 /* Entry block should have just one successor. Exit block should have
3583 at most one fallthru predecessor. */
3584 if (!ENTRY_BLOCK_PTR
->succ
3585 || ENTRY_BLOCK_PTR
->succ
->succ_next
)
3587 fprintf (stderr
, "Wrong amount of edges from entry\n");
3591 if (!(ENTRY_BLOCK_PTR
->succ
->flags
& EDGE_FALLTHRU
))
3593 fprintf (stderr
, "Entry is not fallthru\n");
3598 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
3599 if (e
->flags
& EDGE_FALLTHRU
)
3603 fprintf (stderr
, "More than one fallthru to exit\n");
3614 /* Split BB into entry part and rest; if REDIRECT_LATCH, redirect edges
3615 marked as latch into entry part, analogically for REDIRECT_NONLATCH.
3616 In both of these cases, ignore edge EXCEPT. If CONN_LATCH, set edge
3617 between created entry part and BB as latch one. Return created entry
3621 tree_make_forwarder_block (basic_block bb
, int redirect_latch
,
3622 int redirect_nonlatch
, edge except
, int conn_latch
)
3624 edge e
, next_e
, fallthru
;
3627 /* Create the new basic block. */
3628 dummy
= create_bb ();
3629 dummy
->count
= bb
->count
;
3630 dummy
->frequency
= bb
->frequency
;
3631 dummy
->loop_depth
= bb
->loop_depth
;
3632 dummy
->head_tree
= NULL
;
3633 dummy
->end_tree
= NULL
;
3635 /* Redirect the incoming edges. */
3636 dummy
->pred
= bb
->pred
;
3638 for (e
= dummy
->pred
; e
; e
= e
->pred_next
)
3641 fallthru
= make_edge (dummy
, bb
, 0);
3643 HEADER_BLOCK (dummy
) = 0;
3644 HEADER_BLOCK (bb
) = 1;
3646 /* Redirect back edges we want to keep. */
3647 for (e
= dummy
->pred
; e
; e
= next_e
)
3649 next_e
= e
->pred_next
;
3651 || !((redirect_latch
&& LATCH_EDGE (e
))
3652 || (redirect_nonlatch
&& !LATCH_EDGE (e
))))
3654 dummy
->frequency
-= EDGE_FREQUENCY (e
);
3655 dummy
->count
-= e
->count
;
3656 if (dummy
->frequency
< 0)
3657 dummy
->frequency
= 0;
3658 if (dummy
->count
< 0)
3660 redirect_edge_succ (e
, bb
);
3664 alloc_aux_for_edge (fallthru
, sizeof (int));
3665 LATCH_EDGE (fallthru
) = conn_latch
;
3670 /* Cleanup the cfg -- edges correspondence for a single block BB. If
3671 NO_FALSE_FALLTHRU, just remove false fallthru edges. */
3673 tree_cleanup_block_edges (basic_block bb
, int no_false_fallthru
)
3675 edge e
, e_true
, e_false
, next
;
3676 block_stmt_iterator bsi
;
3678 tree_stmt_iterator alt
;
3680 if (bb
== ENTRY_BLOCK_PTR
)
3684 stmt
= last_stmt (bb
);
3685 bsi
= bsi_last (bb
);
3688 || !stmt_ends_bb_p (stmt
)
3689 || no_false_fallthru
)
3691 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3692 if (e
->flags
& EDGE_FALLTHRU
)
3695 if (e
&& e
->dest
!= bb
->next_bb
&& e
->dest
!= EXIT_BLOCK_PTR
)
3697 tree label
= tree_block_label (e
->dest
);
3699 bsi_insert_after (&bsi
,
3700 build1 (GOTO_EXPR
, void_type_node
, label
),
3702 e
->flags
&= ~EDGE_FALLTHRU
;
3708 || no_false_fallthru
)
3711 switch (TREE_CODE (stmt
))
3714 if (is_computed_goto (stmt
)
3715 || nonlocal_goto_p (stmt
))
3719 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3721 if (e
->flags
& EDGE_ABNORMAL
)
3733 if (e_true
->dest
== bb
->next_bb
)
3736 e_true
->flags
|= EDGE_FALLTHRU
;
3741 e_true
= e_false
= NULL
;
3743 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
3745 if (e
->flags
& EDGE_TRUE_VALUE
)
3751 if (e
->flags
& EDGE_FALSE_VALUE
)
3759 /* During cfg generation it may happen that both branches of COND_EXPR
3760 lead to the same target. */
3761 if (e_true
== e_false
)
3764 if ((!e_true
&& !e_false
)
3765 || (e_true
&& e_false
))
3769 bsi_replace (bsi
, COND_EXPR_ELSE (stmt
));
3771 bsi_replace (bsi
, COND_EXPR_THEN (stmt
));
3773 /* Try optimizing the resulting goto. */
3780 alt
= CASE_START (stmt
);
3781 if (bb
->succ
->succ_next
)
3783 /* If there are more edges, but only one matches, we may proceed
3785 e_true
= find_taken_edge (bb
, SWITCH_COND (stmt
));
3788 for (; !CASE_END_P (alt
); CASE_NEXT (alt
))
3789 if (CASE_EDGE (alt
) == e_true
)
3791 if (CASE_END_P (alt
))
3794 for (e
= bb
->succ
; e
; e
= next
)
3796 next
= e
->succ_next
;
3798 || (e
->flags
& EDGE_ABNORMAL
))
3801 ssa_remove_edge (e
);
3805 bsi_replace (bsi
, CASE_GOTO (alt
));
3812 /* Cleanup the cfg -- edges correspondence damaged by previous passes.
3813 Edges could be removed while unreachable jumps from their control
3814 statemens were not. */
3816 tree_cleanup_edges ()
3822 tree_cleanup_block_edges (bb
, false);
3826 /* Initialization of functions specific to the tree IR. */
3829 tree_register_cfg_hooks ()
3831 cfg_hooks
= &tree_cfg_hooks
;
3834 /* Dumps information about tree_cell chain START to stderr. */
3836 debug_tree_chain (tree_cell start
)
3838 for (; start
; start
= start
->next
)
3839 debug_generic_stmt (start
->stmt
);
3842 /* Initialize loop optimizer. */
3844 static struct loops
*
3845 tree_loop_optimizer_init (FILE *dumpfile
)
3847 struct loops
*loops
= xcalloc (1, sizeof (struct loops
));
3849 /* Find the loops. */
3850 if (flow_loops_find (loops
, LOOP_TREE
) <= 1)
3853 flow_loops_free (loops
);
3858 /* Not going to update these. */
3859 free (loops
->cfg
.rc_order
);
3860 loops
->cfg
.rc_order
= NULL
;
3861 free (loops
->cfg
.dfs_order
);
3862 loops
->cfg
.dfs_order
= NULL
;
3864 /* Force all latches to have only single successor. */
3865 force_single_succ_latches (loops
);
3867 /* Mark irreducible loops. */
3868 mark_irreducible_loops (loops
);
3871 flow_loops_dump (loops
, dumpfile
, NULL
, 1);
3873 #ifdef ENABLE_CHECKING
3874 verify_dominators (loops
->cfg
.dom
);
3875 verify_loop_structure (loops
);
3881 /* Finalize loop optimizer. */
3883 tree_loop_optimizer_finalize (struct loops
*loops
, FILE *dumpfile
)
3889 flow_loops_dump (loops
, dumpfile
, NULL
, 1);
3892 flow_loops_free (loops
);
3896 #ifdef ENABLE_CHECKING
3897 verify_flow_info ();
3901 /* Ensures that there are not useless labels. It makes all jumps to
3902 the block to target one label (non-artificial ones are preferred),
3903 and removes all other artificial labels (the named ones are left
3904 for debugging purposes. */
3906 remove_superfluous_labels ()
3910 block_stmt_iterator bsi
;
3912 tree_stmt_iterator alt
;
3917 bsi
= bsi_start (bb
);
3918 stmt
= first_stmt (bb
);
3920 if (!stmt
|| TREE_CODE (stmt
) != LABEL_EXPR
)
3923 /* Try to find a label that must stay anyway. */
3924 label
= LABEL_EXPR_LABEL (stmt
);
3926 DECL_ARTIFICIAL (label
) && !FORCED_LABEL (label
) && !bsi_end_p (bsi
);
3929 stmt
= bsi_stmt (bsi
);
3930 if (TREE_CODE (stmt
) != LABEL_EXPR
)
3933 label
= LABEL_EXPR_LABEL (stmt
);
3936 /* Replace to it within all jumps to the block. */
3938 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
3940 if ((e
->flags
& EDGE_ABNORMAL
)
3941 || (e
->flags
& EDGE_FALLTHRU
))
3944 stmt
= last_stmt (e
->src
);
3945 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
3947 for (alt
= CASE_START (stmt
);
3950 if (CASE_EDGE (alt
) == e
)
3951 CASE_DESTINATION (alt
) = label
;
3955 stmt
= find_edge_goto (last_stmt (e
->src
), e
);
3958 GOTO_DESTINATION (stmt
) = label
;
3967 /* Remove unnecesary labels. Due to way we have chosen the label to
3968 keep, the kept label will then be the first one of the block,
3969 therefore the one returned by tree_block_label, which is fine. */
3970 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
3972 stmt
= bsi_stmt (bsi
);
3973 if (TREE_CODE (stmt
) != LABEL_EXPR
)
3976 if (LABEL_EXPR_LABEL (stmt
) == label
3977 || !DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt
))
3978 || FORCED_LABEL (LABEL_EXPR_LABEL (stmt
)))
3989 /* Checks whether the basic block BB does nothing except for jump. */
3991 tree_forwarder_block_p (basic_block bb
)
3993 block_stmt_iterator bsi
;
3996 || bb
->succ
->succ_next
3997 || (bb
->succ
->flags
& EDGE_ABNORMAL
)
3998 || bb
== ENTRY_BLOCK_PTR
)
4004 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
4005 switch (TREE_CODE (bsi_stmt (bsi
)))
4018 /* Threads jumps over empty statements. Later we may add threading over
4019 equivalent conditions. */
4023 edge e
, next
, last
, old
;
4024 basic_block bb
, dest
, slow
;
4026 tree phi
, val1
, val2
;
4029 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
4031 /* Don't waste time on unreachable blocks. */
4035 /* Nor on forwarders. */
4036 if (tree_forwarder_block_p (bb
))
4039 for (e
= bb
->succ
; e
; e
= next
)
4041 next
= e
->succ_next
;
4043 if ((e
->flags
& EDGE_ABNORMAL
)
4044 || e
->dest
== EXIT_BLOCK_PTR
4045 || !tree_forwarder_block_p (e
->dest
)
4046 || e
->dest
->succ
->dest
== EXIT_BLOCK_PTR
)
4052 last
= e
->dest
->succ
;
4053 for (dest
= e
->dest
->succ
->dest
;
4054 tree_forwarder_block_p (dest
);
4056 dest
= dest
->succ
->dest
,
4059 /* Infinite loop detected. */
4063 slow
= slow
->succ
->dest
;
4065 if (dest
->succ
->dest
== EXIT_BLOCK_PTR
)
4069 if (dest
== e
->dest
)
4072 old
= find_edge (bb
, dest
);
4075 /* If there already is an edge, check whether the values of
4076 in phi nodes differ. */
4077 for (phi
= phi_nodes (dest
); phi
; phi
= TREE_CHAIN (phi
))
4079 val1
= PHI_ARG_DEF (phi
, phi_arg_from_edge (phi
, last
));
4080 val2
= PHI_ARG_DEF (phi
, phi_arg_from_edge (phi
, old
));
4082 if (!operand_equal_p (val1
, val2
, false))
4087 /* The previous block is forwarder, so there are no
4088 phi nodes to update. */
4093 if (dest
== e
->dest
)
4096 if (redirect_edge_and_branch (e
, dest
)
4099 /* Update phi nodes. */
4100 for (phi
= phi_nodes (dest
); phi
; phi
= TREE_CHAIN (phi
))
4102 arg
= phi_arg_from_edge (phi
, last
);
4105 add_phi_arg (&phi
, PHI_ARG_DEF (phi
, arg
), e
);
4112 /* Returns a GOTO_EXPR for edge E found in STMT. */
4114 find_edge_goto (tree stmt
, edge e
)
4119 switch (TREE_CODE (stmt
))
4122 /* Using this function for switch_exprs is dangerous, since there
4123 may be more than one goto to the one edge. */
4127 if (e
->flags
& EDGE_TRUE_VALUE
)
4128 return COND_EXPR_THEN (stmt
);
4129 else if (e
->flags
& EDGE_FALSE_VALUE
)
4130 return COND_EXPR_ELSE (stmt
);
4142 /* Merges blocks if possible. */
4148 for (bb
= ENTRY_BLOCK_PTR
->next_bb
; bb
!= EXIT_BLOCK_PTR
; )
4151 /* It must have a single succesor. */
4153 && !bb
->succ
->succ_next
4154 && !(bb
->succ
->flags
& EDGE_ABNORMAL
)
4155 && bb
->succ
->dest
!= EXIT_BLOCK_PTR
4156 && bb
->succ
->dest
!= bb
4157 /* That has a single predecessor. */
4158 && !bb
->succ
->dest
->pred
->pred_next
)
4160 /* Merge the blocks and retry. */
4161 merge_blocks (bb
, bb
->succ
->dest
);
4169 /* Non-cfg cleanup. */
4171 /* Walk the function tree removing unnecessary statements and
4174 * Empty statement nodes are removed
4176 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
4178 * Unnecessary COND_EXPRs are removed
4180 * Some unnecessary BIND_EXPRs are removed
4182 Clearly more work could be done. The trick is doing the analysis
4183 and removal fast enough to be a net improvement in compile times.
4185 Note that when we remove a control structure such as a COND_EXPR
4186 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
4187 to ensure we eliminate all the useless code. */
4192 bool remove_unused_vars
;
4197 static void remove_useless_stmts_and_vars_1 (tree
*, struct rusv_data
*);
4200 remove_useless_stmts_and_vars_cond (tree
*stmt_p
, struct rusv_data
*data
)
4202 tree then_clause
, else_clause
, cond
;
4204 remove_useless_stmts_and_vars_1 (&COND_EXPR_THEN (*stmt_p
), data
);
4205 remove_useless_stmts_and_vars_1 (&COND_EXPR_ELSE (*stmt_p
), data
);
4207 then_clause
= COND_EXPR_THEN (*stmt_p
);
4208 else_clause
= COND_EXPR_ELSE (*stmt_p
);
4209 cond
= COND_EXPR_COND (*stmt_p
);
4211 /* We may not have been able to completely optimize away the condition
4212 previously due to the existence of a label in one arm. If the label
4213 has since become unreachable then we may be able to zap the entire
4214 conditional here. If so, replace the COND_EXPR and set up to repeat
4215 this optimization pass. */
4216 if (integer_nonzerop (cond
) && IS_EMPTY_STMT (else_clause
))
4218 *stmt_p
= then_clause
;
4219 data
->repeat
= true;
4221 else if (integer_zerop (cond
) && IS_EMPTY_STMT (then_clause
))
4223 *stmt_p
= else_clause
;
4224 data
->repeat
= true;
4227 /* Notice branches to a common destination. */
4228 else if (TREE_CODE (then_clause
) == GOTO_EXPR
4229 && TREE_CODE (else_clause
) == GOTO_EXPR
4230 && (GOTO_DESTINATION (then_clause
)
4231 == GOTO_DESTINATION (else_clause
)))
4233 *stmt_p
= then_clause
;
4234 data
->repeat
= true;
4237 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
4238 which is already known to contain that value, then remove the useless
4239 THEN/ELSE clause. */
4240 else if (TREE_CODE (cond
) == VAR_DECL
|| TREE_CODE (cond
) == PARM_DECL
)
4242 if (TREE_CODE (else_clause
) == MODIFY_EXPR
4243 && TREE_OPERAND (else_clause
, 0) == cond
4244 && integer_zerop (TREE_OPERAND (else_clause
, 1)))
4245 COND_EXPR_ELSE (*stmt_p
) = build_empty_stmt ();
4247 else if ((TREE_CODE (cond
) == EQ_EXPR
|| TREE_CODE (cond
) == NE_EXPR
)
4248 && (TREE_CODE (TREE_OPERAND (cond
, 0)) == VAR_DECL
4249 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PARM_DECL
)
4250 && TREE_CONSTANT (TREE_OPERAND (cond
, 1)))
4252 tree clause
= (TREE_CODE (cond
) == EQ_EXPR
4253 ? then_clause
: else_clause
);
4254 tree
*location
= (TREE_CODE (cond
) == EQ_EXPR
4255 ? &COND_EXPR_THEN (*stmt_p
)
4256 : &COND_EXPR_ELSE (*stmt_p
));
4258 if (TREE_CODE (clause
) == MODIFY_EXPR
4259 && TREE_OPERAND (clause
, 0) == TREE_OPERAND (cond
, 0)
4260 && TREE_OPERAND (clause
, 1) == TREE_OPERAND (cond
, 1))
4261 *location
= build_empty_stmt ();
4266 remove_useless_stmts_and_vars_tf (tree
*stmt_p
, struct rusv_data
*data
)
4268 bool save_may_branch
, save_may_throw
;
4269 bool this_may_branch
, this_may_throw
;
4271 /* Collect may_branch and may_throw information for the body only. */
4272 save_may_branch
= data
->may_branch
;
4273 save_may_throw
= data
->may_throw
;
4274 data
->may_branch
= false;
4275 data
->may_throw
= false;
4277 remove_useless_stmts_and_vars_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
4279 this_may_branch
= data
->may_branch
;
4280 this_may_throw
= data
->may_throw
;
4281 data
->may_branch
|= save_may_branch
;
4282 data
->may_throw
|= save_may_throw
;
4284 remove_useless_stmts_and_vars_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
4286 /* If the body is empty, then we can emit the FINALLY block without
4287 the enclosing TRY_FINALLY_EXPR. */
4288 if (IS_EMPTY_STMT (TREE_OPERAND (*stmt_p
, 0)))
4290 *stmt_p
= TREE_OPERAND (*stmt_p
, 1);
4291 data
->repeat
= true;
4294 /* If the handler is empty, then we can emit the TRY block without
4295 the enclosing TRY_FINALLY_EXPR. */
4296 else if (IS_EMPTY_STMT (TREE_OPERAND (*stmt_p
, 1)))
4298 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
4299 data
->repeat
= true;
4302 /* If the body neither throws, nor branches, then we can safely string
4303 the TRY and FINALLY blocks together. We'll reassociate this in the
4304 main body of remove_useless_stmts_and_vars. */
4305 else if (!this_may_branch
&& !this_may_throw
)
4306 TREE_SET_CODE (*stmt_p
, COMPOUND_EXPR
);
4310 remove_useless_stmts_and_vars_tc (tree
*stmt_p
, struct rusv_data
*data
)
4312 bool save_may_throw
, this_may_throw
;
4313 tree_stmt_iterator i
;
4316 /* Collect may_throw information for the body only. */
4317 save_may_throw
= data
->may_throw
;
4318 data
->may_throw
= false;
4320 remove_useless_stmts_and_vars_1 (&TREE_OPERAND (*stmt_p
, 0), data
);
4322 this_may_throw
= data
->may_throw
;
4323 data
->may_throw
= save_may_throw
;
4325 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
4326 if (!this_may_throw
)
4328 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
4329 data
->repeat
= true;
4333 /* Process the catch clause specially. We may be able to tell that
4334 no exceptions propagate past this point. */
4336 this_may_throw
= true;
4337 i
= tsi_start (&TREE_OPERAND (*stmt_p
, 1));
4338 stmt
= tsi_stmt (i
);
4340 switch (TREE_CODE (stmt
))
4343 for (; !tsi_end_p (i
); tsi_next (&i
))
4345 stmt
= tsi_stmt (i
);
4346 /* If we catch all exceptions, then the body does not
4347 propagate exceptions past this point. */
4348 if (CATCH_TYPES (stmt
) == NULL
)
4349 this_may_throw
= false;
4350 remove_useless_stmts_and_vars_1 (&CATCH_BODY (stmt
), data
);
4354 case EH_FILTER_EXPR
:
4355 if (EH_FILTER_MUST_NOT_THROW (stmt
))
4356 this_may_throw
= false;
4357 else if (EH_FILTER_TYPES (stmt
) == NULL
)
4358 this_may_throw
= false;
4359 remove_useless_stmts_and_vars_1 (&EH_FILTER_FAILURE (stmt
), data
);
4363 /* Otherwise this is a cleanup. */
4364 remove_useless_stmts_and_vars_1 (&TREE_OPERAND (*stmt_p
, 1), data
);
4366 /* If the cleanup is empty, then we can emit the TRY block without
4367 the enclosing TRY_CATCH_EXPR. */
4368 if (IS_EMPTY_STMT (TREE_OPERAND (*stmt_p
, 1)))
4370 *stmt_p
= TREE_OPERAND (*stmt_p
, 0);
4371 data
->repeat
= true;
4375 data
->may_throw
|= this_may_throw
;
4379 remove_useless_stmts_and_vars_bind (tree
*stmt_p
, struct rusv_data
*data
)
4383 /* First remove anything underneath the BIND_EXPR. */
4384 remove_useless_stmts_and_vars_1 (&BIND_EXPR_BODY (*stmt_p
), data
);
4386 /* If the BIND_EXPR has no variables, then we can pull everything
4387 up one level and remove the BIND_EXPR, unless this is the toplevel
4388 BIND_EXPR for the current function or an inlined function.
4390 When this situation occurs we will want to apply this
4391 optimization again. */
4392 block
= BIND_EXPR_BLOCK (*stmt_p
);
4393 if (BIND_EXPR_VARS (*stmt_p
) == NULL_TREE
4394 && *stmt_p
!= DECL_SAVED_TREE (current_function_decl
)
4396 || ! BLOCK_ABSTRACT_ORIGIN (block
)
4397 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
))
4400 *stmt_p
= BIND_EXPR_BODY (*stmt_p
);
4401 data
->repeat
= true;
4403 else if (data
->remove_unused_vars
)
4405 /* If we were unable to completely eliminate the BIND_EXPR,
4406 go ahead and prune out any unused variables. We do not
4407 want to expand them as that is a waste of time. If we
4408 happen to remove all the variables, then we may be able
4409 to eliminate the BIND_EXPR as well. */
4410 tree vars
, prev_var
;
4412 /* Walk all the variables associated with the BIND_EXPR. */
4413 for (prev_var
= NULL
, vars
= BIND_EXPR_VARS (*stmt_p
);
4415 vars
= TREE_CHAIN (vars
))
4417 struct var_ann_d
*ann
;
4419 /* We could have function declarations and the like
4420 on this list. Ignore them. */
4421 if (TREE_CODE (vars
) != VAR_DECL
)
4427 /* Remove all unused, unaliased temporaries. Also remove
4428 unused, unaliased local variables during highly
4429 optimizing compilations. */
4430 ann
= var_ann (vars
);
4432 && ! ann
->may_aliases
4434 && ! ann
->has_hidden_use
4435 && ! TREE_ADDRESSABLE (vars
)
4436 && (DECL_ARTIFICIAL (vars
) || optimize
>= 2))
4438 /* Remove the variable from the BLOCK structures. */
4443 : DECL_INITIAL (current_function_decl
)));
4445 /* And splice the variable out of BIND_EXPR_VARS. */
4447 TREE_CHAIN (prev_var
) = TREE_CHAIN (vars
);
4449 BIND_EXPR_VARS (*stmt_p
) = TREE_CHAIN (vars
);
4455 /* If there are no variables left after removing unused
4456 variables, then go ahead and remove this BIND_EXPR. */
4457 if (BIND_EXPR_VARS (*stmt_p
) == NULL_TREE
4458 && *stmt_p
!= DECL_SAVED_TREE (current_function_decl
)
4460 || ! BLOCK_ABSTRACT_ORIGIN (block
)
4461 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
))
4464 *stmt_p
= BIND_EXPR_BODY (*stmt_p
);
4465 data
->repeat
= true;
4471 remove_useless_stmts_and_vars_goto (tree_stmt_iterator i
, tree
*stmt_p
,
4472 struct rusv_data
*data
)
4474 tree_stmt_iterator tsi
= i
;
4476 /* Step past the GOTO_EXPR statement. */
4478 if (! tsi_end_p (tsi
))
4480 /* If we are not at the end of this tree, then see if
4481 we are at the target label. If so, then this jump
4485 label
= tsi_stmt (tsi
);
4486 if (TREE_CODE (label
) == LABEL_EXPR
4487 && LABEL_EXPR_LABEL (label
) == GOTO_DESTINATION (*stmt_p
))
4489 data
->repeat
= true;
4490 *stmt_p
= build_empty_stmt ();
4495 data
->may_branch
= true;
4499 remove_useless_stmts_and_vars_1 (tree
*first_p
, struct rusv_data
*data
)
4501 tree_stmt_iterator i
;
4503 for (i
= tsi_start (first_p
); !tsi_end_p (i
); tsi_next (&i
))
4505 tree
*container_p
= tsi_container (i
);
4507 enum tree_code code
;
4509 while (TREE_CODE (*container_p
) == COMPOUND_EXPR
)
4511 /* If either operand of a COMPOUND_EXPR is an empty statement,
4512 then remove the empty statement and the COMPOUND_EXPR itself. */
4513 if (IS_EMPTY_STMT (TREE_OPERAND (*container_p
, 1)))
4514 *container_p
= TREE_OPERAND (*container_p
, 0);
4515 else if (IS_EMPTY_STMT (TREE_OPERAND (*container_p
, 0)))
4516 *container_p
= TREE_OPERAND (*container_p
, 1);
4521 /* Dive into control structures. */
4522 stmt_p
= tsi_stmt_ptr (i
);
4523 code
= TREE_CODE (*stmt_p
);
4527 remove_useless_stmts_and_vars_cond (stmt_p
, data
);
4530 remove_useless_stmts_and_vars_1 (&SWITCH_BODY (*stmt_p
), data
);
4532 case TRY_FINALLY_EXPR
:
4533 remove_useless_stmts_and_vars_tf (stmt_p
, data
);
4535 case TRY_CATCH_EXPR
:
4536 remove_useless_stmts_and_vars_tc (stmt_p
, data
);
4539 remove_useless_stmts_and_vars_bind (stmt_p
, data
);
4542 remove_useless_stmts_and_vars_goto (i
, stmt_p
, data
);
4545 data
->may_branch
= true;
4549 if (tree_could_throw_p (*stmt_p
))
4550 data
->may_throw
= true;
4556 /* We need to keep the tree in gimple form, so we may have to
4557 re-rationalize COMPOUND_EXPRs. */
4558 if (TREE_CODE (*container_p
) == COMPOUND_EXPR
4559 && TREE_CODE (TREE_OPERAND (*container_p
, 0)) == COMPOUND_EXPR
)
4560 *container_p
= rationalize_compound_expr (*container_p
);
4565 remove_useless_stmts_and_vars (tree
*first_p
, bool remove_unused_vars
)
4567 struct rusv_data data
;
4570 memset (&data
, 0, sizeof (data
));
4571 data
.remove_unused_vars
= remove_unused_vars
;
4572 remove_unused_vars
= false;
4574 remove_useless_stmts_and_vars_1 (first_p
, &data
);
4576 while (data
.repeat
);
4579 /* Splits critical edges in the cfg. */
4582 split_critical_edges ()
4584 struct edge_list
*el
= create_edge_list ();
4588 for (i
= 0; i
< NUM_EDGES (el
); i
++)
4590 e
= INDEX_EDGE (el
, i
);
4591 if (EDGE_CRITICAL_P (e
) && !(e
->flags
& EDGE_ABNORMAL
))
4594 free_edge_list (el
);