* call.c (z_candidate::template_decl): Rename from template.
[official-gcc.git] / gcc / tree-cfg.c
blobc9d2424cf4cbd741f08ac078d31588c1029f94a7
1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "errors.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "ggc.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
40 #include "timevar.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "cfgloop.h"
47 /* This file contains functions for building the Control Flow Graph (CFG)
48 for a function tree. */
50 /* Local declarations. */
52 /* Initial capacity for the basic block array. */
53 static const int initial_cfg_capacity = 20;
55 /* Mapping of labels to their associated blocks. This can greatly speed up
56 building of the CFG in code with lots of gotos. */
57 static GTY(()) varray_type label_to_block_map;
59 /* CFG statistics. */
60 struct cfg_stats_d
62 long num_merged_labels;
65 static struct cfg_stats_d cfg_stats;
67 /* Nonzero if we found a computed goto while building basic blocks. */
68 static bool found_computed_goto;
70 /* Basic blocks and flowgraphs. */
71 static basic_block create_bb (void *, void *, basic_block);
72 static void create_block_annotation (basic_block);
73 static void free_blocks_annotations (void);
74 static void clear_blocks_annotations (void);
75 static void make_blocks (tree);
76 static void factor_computed_gotos (void);
78 /* Edges. */
79 static void make_edges (void);
80 static void make_ctrl_stmt_edges (basic_block);
81 static void make_exit_edges (basic_block);
82 static void make_cond_expr_edges (basic_block);
83 static void make_switch_expr_edges (basic_block);
84 static void make_goto_expr_edges (basic_block);
85 static edge tree_redirect_edge_and_branch (edge, basic_block);
86 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
87 static void split_critical_edges (void);
89 /* Various helpers. */
90 static inline bool stmt_starts_bb_p (tree, tree);
91 static int tree_verify_flow_info (void);
92 static void tree_make_forwarder_block (edge);
93 static bool thread_jumps (void);
94 static bool tree_forwarder_block_p (basic_block);
95 static void bsi_commit_edge_inserts_1 (edge e);
96 static void tree_cfg2vcg (FILE *);
98 /* Flowgraph optimization and cleanup. */
99 static void tree_merge_blocks (basic_block, basic_block);
100 static bool tree_can_merge_blocks_p (basic_block, basic_block);
101 static void remove_bb (basic_block);
102 static bool cleanup_control_flow (void);
103 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
104 static edge find_taken_edge_cond_expr (basic_block, tree);
105 static edge find_taken_edge_switch_expr (basic_block, tree);
106 static tree find_case_label_for_value (tree, tree);
107 static bool phi_alternatives_equal (basic_block, edge, edge);
110 /*---------------------------------------------------------------------------
111 Create basic blocks
112 ---------------------------------------------------------------------------*/
114 /* Entry point to the CFG builder for trees. TP points to the list of
115 statements to be added to the flowgraph. */
117 static void
118 build_tree_cfg (tree *tp)
120 /* Register specific tree functions. */
121 tree_register_cfg_hooks ();
123 /* Initialize rbi_pool. */
124 alloc_rbi_pool ();
126 /* Initialize the basic block array. */
127 init_flow ();
128 n_basic_blocks = 0;
129 last_basic_block = 0;
130 VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info");
131 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
133 /* Build a mapping of labels to their associated blocks. */
134 VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity,
135 "label to block map");
137 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
138 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
140 found_computed_goto = 0;
141 make_blocks (*tp);
143 /* Computed gotos are hell to deal with, especially if there are
144 lots of them with a large number of destinations. So we factor
145 them to a common computed goto location before we build the
146 edge list. After we convert back to normal form, we will un-factor
147 the computed gotos since factoring introduces an unwanted jump. */
148 if (found_computed_goto)
149 factor_computed_gotos ();
151 /* Make sure there is always at least one block, even if its empty. */
152 if (n_basic_blocks == 0)
153 create_empty_bb (ENTRY_BLOCK_PTR);
155 create_block_annotation (ENTRY_BLOCK_PTR);
156 create_block_annotation (EXIT_BLOCK_PTR);
158 /* Adjust the size of the array. */
159 VARRAY_GROW (basic_block_info, n_basic_blocks);
161 /* To speed up statement iterator walks, we first purge dead labels. */
162 cleanup_dead_labels ();
164 /* Group case nodes to reduce the number of edges.
165 We do this after cleaning up dead labels because otherwise we miss
166 a lot of obvious case merging opportunities. */
167 group_case_labels ();
169 /* Create the edges of the flowgraph. */
170 make_edges ();
172 /* Debugging dumps. */
174 /* Write the flowgraph to a VCG file. */
176 int local_dump_flags;
177 FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags);
178 if (dump_file)
180 tree_cfg2vcg (dump_file);
181 dump_end (TDI_vcg, dump_file);
185 /* Dump a textual representation of the flowgraph. */
186 if (dump_file)
187 dump_tree_cfg (dump_file, dump_flags);
190 static void
191 execute_build_cfg (void)
193 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
196 struct tree_opt_pass pass_build_cfg =
198 "cfg", /* name */
199 NULL, /* gate */
200 execute_build_cfg, /* execute */
201 NULL, /* sub */
202 NULL, /* next */
203 0, /* static_pass_number */
204 TV_TREE_CFG, /* tv_id */
205 PROP_gimple_leh, /* properties_required */
206 PROP_cfg, /* properties_provided */
207 0, /* properties_destroyed */
208 0, /* todo_flags_start */
209 TODO_verify_stmts /* todo_flags_finish */
212 /* Search the CFG for any computed gotos. If found, factor them to a
213 common computed goto site. Also record the location of that site so
214 that we can un-factor the gotos after we have converted back to
215 normal form. */
217 static void
218 factor_computed_gotos (void)
220 basic_block bb;
221 tree factored_label_decl = NULL;
222 tree var = NULL;
223 tree factored_computed_goto_label = NULL;
224 tree factored_computed_goto = NULL;
226 /* We know there are one or more computed gotos in this function.
227 Examine the last statement in each basic block to see if the block
228 ends with a computed goto. */
230 FOR_EACH_BB (bb)
232 block_stmt_iterator bsi = bsi_last (bb);
233 tree last;
235 if (bsi_end_p (bsi))
236 continue;
237 last = bsi_stmt (bsi);
239 /* Ignore the computed goto we create when we factor the original
240 computed gotos. */
241 if (last == factored_computed_goto)
242 continue;
244 /* If the last statement is a computed goto, factor it. */
245 if (computed_goto_p (last))
247 tree assignment;
249 /* The first time we find a computed goto we need to create
250 the factored goto block and the variable each original
251 computed goto will use for their goto destination. */
252 if (! factored_computed_goto)
254 basic_block new_bb = create_empty_bb (bb);
255 block_stmt_iterator new_bsi = bsi_start (new_bb);
257 /* Create the destination of the factored goto. Each original
258 computed goto will put its desired destination into this
259 variable and jump to the label we create immediately
260 below. */
261 var = create_tmp_var (ptr_type_node, "gotovar");
263 /* Build a label for the new block which will contain the
264 factored computed goto. */
265 factored_label_decl = create_artificial_label ();
266 factored_computed_goto_label
267 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
268 bsi_insert_after (&new_bsi, factored_computed_goto_label,
269 BSI_NEW_STMT);
271 /* Build our new computed goto. */
272 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
273 bsi_insert_after (&new_bsi, factored_computed_goto,
274 BSI_NEW_STMT);
277 /* Copy the original computed goto's destination into VAR. */
278 assignment = build (MODIFY_EXPR, ptr_type_node,
279 var, GOTO_DESTINATION (last));
280 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
282 /* And re-vector the computed goto to the new destination. */
283 GOTO_DESTINATION (last) = factored_label_decl;
289 /* Create annotations for a single basic block. */
291 static void
292 create_block_annotation (basic_block bb)
294 /* Verify that the tree_annotations field is clear. */
295 if (bb->tree_annotations)
296 abort ();
297 bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d));
301 /* Free the annotations for all the basic blocks. */
303 static void free_blocks_annotations (void)
305 clear_blocks_annotations ();
309 /* Clear the annotations for all the basic blocks. */
311 static void
312 clear_blocks_annotations (void)
314 basic_block bb;
316 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
317 bb->tree_annotations = NULL;
321 /* Build a flowgraph for the statement_list STMT_LIST. */
323 static void
324 make_blocks (tree stmt_list)
326 tree_stmt_iterator i = tsi_start (stmt_list);
327 tree stmt = NULL;
328 bool start_new_block = true;
329 bool first_stmt_of_list = true;
330 basic_block bb = ENTRY_BLOCK_PTR;
332 while (!tsi_end_p (i))
334 tree prev_stmt;
336 prev_stmt = stmt;
337 stmt = tsi_stmt (i);
339 /* If the statement starts a new basic block or if we have determined
340 in a previous pass that we need to create a new block for STMT, do
341 so now. */
342 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
344 if (!first_stmt_of_list)
345 stmt_list = tsi_split_statement_list_before (&i);
346 bb = create_basic_block (stmt_list, NULL, bb);
347 start_new_block = false;
350 /* Now add STMT to BB and create the subgraphs for special statement
351 codes. */
352 set_bb_for_stmt (stmt, bb);
354 if (computed_goto_p (stmt))
355 found_computed_goto = true;
357 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
358 next iteration. */
359 if (stmt_ends_bb_p (stmt))
360 start_new_block = true;
362 tsi_next (&i);
363 first_stmt_of_list = false;
368 /* Create and return a new empty basic block after bb AFTER. */
370 static basic_block
371 create_bb (void *h, void *e, basic_block after)
373 basic_block bb;
375 if (e)
376 abort ();
378 /* Create and initialize a new basic block. */
379 bb = alloc_block ();
380 memset (bb, 0, sizeof (*bb));
382 bb->index = last_basic_block;
383 bb->flags = BB_NEW;
384 bb->stmt_list = h ? h : alloc_stmt_list ();
386 /* Add the new block to the linked list of blocks. */
387 link_block (bb, after);
389 /* Grow the basic block array if needed. */
390 if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info))
392 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
393 VARRAY_GROW (basic_block_info, new_size);
396 /* Add the newly created block to the array. */
397 BASIC_BLOCK (last_basic_block) = bb;
399 create_block_annotation (bb);
401 n_basic_blocks++;
402 last_basic_block++;
404 initialize_bb_rbi (bb);
405 return bb;
409 /*---------------------------------------------------------------------------
410 Edge creation
411 ---------------------------------------------------------------------------*/
413 /* Join all the blocks in the flowgraph. */
415 static void
416 make_edges (void)
418 basic_block bb;
420 /* Create an edge from entry to the first block with executable
421 statements in it. */
422 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
424 /* Traverse basic block array placing edges. */
425 FOR_EACH_BB (bb)
427 tree first = first_stmt (bb);
428 tree last = last_stmt (bb);
430 if (first)
432 /* Edges for statements that always alter flow control. */
433 if (is_ctrl_stmt (last))
434 make_ctrl_stmt_edges (bb);
436 /* Edges for statements that sometimes alter flow control. */
437 if (is_ctrl_altering_stmt (last))
438 make_exit_edges (bb);
441 /* Finally, if no edges were created above, this is a regular
442 basic block that only needs a fallthru edge. */
443 if (bb->succ == NULL)
444 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
447 /* We do not care about fake edges, so remove any that the CFG
448 builder inserted for completeness. */
449 remove_fake_exit_edges ();
451 /* Clean up the graph and warn for unreachable code. */
452 cleanup_tree_cfg ();
456 /* Create edges for control statement at basic block BB. */
458 static void
459 make_ctrl_stmt_edges (basic_block bb)
461 tree last = last_stmt (bb);
463 #if defined ENABLE_CHECKING
464 if (last == NULL_TREE)
465 abort();
466 #endif
468 switch (TREE_CODE (last))
470 case GOTO_EXPR:
471 make_goto_expr_edges (bb);
472 break;
474 case RETURN_EXPR:
475 make_edge (bb, EXIT_BLOCK_PTR, 0);
476 break;
478 case COND_EXPR:
479 make_cond_expr_edges (bb);
480 break;
482 case SWITCH_EXPR:
483 make_switch_expr_edges (bb);
484 break;
486 case RESX_EXPR:
487 make_eh_edges (last);
488 /* Yet another NORETURN hack. */
489 if (bb->succ == NULL)
490 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
491 break;
493 default:
494 abort ();
499 /* Create exit edges for statements in block BB that alter the flow of
500 control. Statements that alter the control flow are 'goto', 'return'
501 and calls to non-returning functions. */
503 static void
504 make_exit_edges (basic_block bb)
506 tree last = last_stmt (bb), op;
508 if (last == NULL_TREE)
509 abort ();
511 switch (TREE_CODE (last))
513 case CALL_EXPR:
514 /* If this function receives a nonlocal goto, then we need to
515 make edges from this call site to all the nonlocal goto
516 handlers. */
517 if (TREE_SIDE_EFFECTS (last)
518 && current_function_has_nonlocal_label)
519 make_goto_expr_edges (bb);
521 /* If this statement has reachable exception handlers, then
522 create abnormal edges to them. */
523 make_eh_edges (last);
525 /* Some calls are known not to return. For such calls we create
526 a fake edge.
528 We really need to revamp how we build edges so that it's not
529 such a bloody pain to avoid creating edges for this case since
530 all we do is remove these edges when we're done building the
531 CFG. */
532 if (call_expr_flags (last) & (ECF_NORETURN | ECF_LONGJMP))
534 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
535 return;
538 /* Don't forget the fall-thru edge. */
539 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
540 break;
542 case MODIFY_EXPR:
543 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
544 may have an abnormal edge. Search the RHS for this case and
545 create any required edges. */
546 op = get_call_expr_in (last);
547 if (op && TREE_SIDE_EFFECTS (op)
548 && current_function_has_nonlocal_label)
549 make_goto_expr_edges (bb);
551 make_eh_edges (last);
552 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
553 break;
555 default:
556 abort ();
561 /* Create the edges for a COND_EXPR starting at block BB.
562 At this point, both clauses must contain only simple gotos. */
564 static void
565 make_cond_expr_edges (basic_block bb)
567 tree entry = last_stmt (bb);
568 basic_block then_bb, else_bb;
569 tree then_label, else_label;
571 #if defined ENABLE_CHECKING
572 if (entry == NULL_TREE || TREE_CODE (entry) != COND_EXPR)
573 abort ();
574 #endif
576 /* Entry basic blocks for each component. */
577 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
578 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
579 then_bb = label_to_block (then_label);
580 else_bb = label_to_block (else_label);
582 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
583 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
587 /* Create the edges for a SWITCH_EXPR starting at block BB.
588 At this point, the switch body has been lowered and the
589 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
591 static void
592 make_switch_expr_edges (basic_block bb)
594 tree entry = last_stmt (bb);
595 size_t i, n;
596 tree vec;
598 vec = SWITCH_LABELS (entry);
599 n = TREE_VEC_LENGTH (vec);
601 for (i = 0; i < n; ++i)
603 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
604 basic_block label_bb = label_to_block (lab);
605 make_edge (bb, label_bb, 0);
610 /* Return the basic block holding label DEST. */
612 basic_block
613 label_to_block (tree dest)
615 int uid = LABEL_DECL_UID (dest);
617 /* We would die hard when faced by undefined label. Emit label to
618 very first basic block. This will hopefully make even the dataflow
619 and undefined variable warnings quite right. */
620 if ((errorcount || sorrycount) && uid < 0)
622 block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0));
623 tree stmt;
625 stmt = build1 (LABEL_EXPR, void_type_node, dest);
626 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
627 uid = LABEL_DECL_UID (dest);
629 return VARRAY_BB (label_to_block_map, uid);
633 /* Create edges for a goto statement at block BB. */
635 static void
636 make_goto_expr_edges (basic_block bb)
638 tree goto_t, dest;
639 basic_block target_bb;
640 int for_call;
641 block_stmt_iterator last = bsi_last (bb);
643 goto_t = bsi_stmt (last);
645 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
646 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
647 from a nonlocal goto. */
648 if (TREE_CODE (goto_t) != GOTO_EXPR)
650 dest = error_mark_node;
651 for_call = 1;
653 else
655 dest = GOTO_DESTINATION (goto_t);
656 for_call = 0;
658 /* A GOTO to a local label creates normal edges. */
659 if (simple_goto_p (goto_t))
661 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
662 #ifdef USE_MAPPED_LOCATION
663 e->goto_locus = EXPR_LOCATION (goto_t);
664 #else
665 e->goto_locus = EXPR_LOCUS (goto_t);
666 #endif
667 bsi_remove (&last);
668 return;
671 /* Nothing more to do for nonlocal gotos. */
672 if (TREE_CODE (dest) == LABEL_DECL)
673 return;
675 /* Computed gotos remain. */
678 /* Look for the block starting with the destination label. In the
679 case of a computed goto, make an edge to any label block we find
680 in the CFG. */
681 FOR_EACH_BB (target_bb)
683 block_stmt_iterator bsi;
685 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
687 tree target = bsi_stmt (bsi);
689 if (TREE_CODE (target) != LABEL_EXPR)
690 break;
692 if (
693 /* Computed GOTOs. Make an edge to every label block that has
694 been marked as a potential target for a computed goto. */
695 (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0)
696 /* Nonlocal GOTO target. Make an edge to every label block
697 that has been marked as a potential target for a nonlocal
698 goto. */
699 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1))
701 make_edge (bb, target_bb, EDGE_ABNORMAL);
702 break;
707 /* Degenerate case of computed goto with no labels. */
708 if (!for_call && !bb->succ)
709 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
713 /*---------------------------------------------------------------------------
714 Flowgraph analysis
715 ---------------------------------------------------------------------------*/
717 /* Remove unreachable blocks and other miscellaneous clean up work. */
719 void
720 cleanup_tree_cfg (void)
722 bool something_changed = true;
724 timevar_push (TV_TREE_CLEANUP_CFG);
726 /* These three transformations can cascade, so we iterate on them until
727 nothing changes. */
728 while (something_changed)
730 something_changed = cleanup_control_flow ();
731 something_changed |= delete_unreachable_blocks ();
732 something_changed |= thread_jumps ();
735 /* Merging the blocks creates no new opportunities for the other
736 optimizations, so do it here. */
737 merge_seq_blocks ();
739 compact_blocks ();
741 #ifdef ENABLE_CHECKING
742 verify_flow_info ();
743 #endif
744 timevar_pop (TV_TREE_CLEANUP_CFG);
748 /* Cleanup useless labels in basic blocks. This is something we wish
749 to do early because it allows us to group case labels before creating
750 the edges for the CFG, and it speeds up block statement iterators in
751 all passes later on.
752 We only run this pass once, running it more than once is probably not
753 profitable. */
755 /* A map from basic block index to the leading label of that block. */
756 static tree *label_for_bb;
758 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
759 static void
760 update_eh_label (struct eh_region *region)
762 tree old_label = get_eh_region_tree_label (region);
763 if (old_label)
765 tree new_label;
766 basic_block bb = label_to_block (old_label);
768 /* ??? After optimizing, there may be EH regions with labels
769 that have already been removed from the function body, so
770 there is no basic block for them. */
771 if (! bb)
772 return;
774 new_label = label_for_bb[bb->index];
775 set_eh_region_tree_label (region, new_label);
779 /* Given LABEL return the first label in the same basic block. */
780 static tree
781 main_block_label (tree label)
783 basic_block bb = label_to_block (label);
785 /* label_to_block possibly inserted undefined label into the chain. */
786 if (!label_for_bb[bb->index])
787 label_for_bb[bb->index] = label;
788 return label_for_bb[bb->index];
791 /* Cleanup redundant labels. This is a three-steo process:
792 1) Find the leading label for each block.
793 2) Redirect all references to labels to the leading labels.
794 3) Cleanup all useless labels. */
796 void
797 cleanup_dead_labels (void)
799 basic_block bb;
800 label_for_bb = xcalloc (last_basic_block, sizeof (tree));
802 /* Find a suitable label for each block. We use the first user-defined
803 label is there is one, or otherwise just the first label we see. */
804 FOR_EACH_BB (bb)
806 block_stmt_iterator i;
808 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
810 tree label, stmt = bsi_stmt (i);
812 if (TREE_CODE (stmt) != LABEL_EXPR)
813 break;
815 label = LABEL_EXPR_LABEL (stmt);
817 /* If we have not yet seen a label for the current block,
818 remember this one and see if there are more labels. */
819 if (! label_for_bb[bb->index])
821 label_for_bb[bb->index] = label;
822 continue;
825 /* If we did see a label for the current block already, but it
826 is an artificially created label, replace it if the current
827 label is a user defined label. */
828 if (! DECL_ARTIFICIAL (label)
829 && DECL_ARTIFICIAL (label_for_bb[bb->index]))
831 label_for_bb[bb->index] = label;
832 break;
837 /* Now redirect all jumps/branches to the selected label.
838 First do so for each block ending in a control statement. */
839 FOR_EACH_BB (bb)
841 tree stmt = last_stmt (bb);
842 if (!stmt)
843 continue;
845 switch (TREE_CODE (stmt))
847 case COND_EXPR:
849 tree true_branch, false_branch;
851 true_branch = COND_EXPR_THEN (stmt);
852 false_branch = COND_EXPR_ELSE (stmt);
854 GOTO_DESTINATION (true_branch)
855 = main_block_label (GOTO_DESTINATION (true_branch));
856 GOTO_DESTINATION (false_branch)
857 = main_block_label (GOTO_DESTINATION (false_branch));
859 break;
862 case SWITCH_EXPR:
864 size_t i;
865 tree vec = SWITCH_LABELS (stmt);
866 size_t n = TREE_VEC_LENGTH (vec);
868 /* Replace all destination labels. */
869 for (i = 0; i < n; ++i)
870 CASE_LABEL (TREE_VEC_ELT (vec, i))
871 = main_block_label (CASE_LABEL (TREE_VEC_ELT (vec, i)));
873 break;
876 /* We have to handle GOTO_EXPRs until they're removed, and we don't
877 remove them until after we've created the CFG edges. */
878 case GOTO_EXPR:
879 if (! computed_goto_p (stmt))
881 GOTO_DESTINATION (stmt)
882 = main_block_label (GOTO_DESTINATION (stmt));
883 break;
886 default:
887 break;
891 for_each_eh_region (update_eh_label);
893 /* Finally, purge dead labels. All user-defined labels and labels that
894 can be the target of non-local gotos are preserved. */
895 FOR_EACH_BB (bb)
897 block_stmt_iterator i;
898 tree label_for_this_bb = label_for_bb[bb->index];
900 if (! label_for_this_bb)
901 continue;
903 for (i = bsi_start (bb); !bsi_end_p (i); )
905 tree label, stmt = bsi_stmt (i);
907 if (TREE_CODE (stmt) != LABEL_EXPR)
908 break;
910 label = LABEL_EXPR_LABEL (stmt);
912 if (label == label_for_this_bb
913 || ! DECL_ARTIFICIAL (label)
914 || DECL_NONLOCAL (label))
915 bsi_next (&i);
916 else
917 bsi_remove (&i);
921 free (label_for_bb);
924 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
925 and scan the sorted vector of cases. Combine the ones jumping to the
926 same label.
927 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
929 void
930 group_case_labels (void)
932 basic_block bb;
934 FOR_EACH_BB (bb)
936 tree stmt = last_stmt (bb);
937 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
939 tree labels = SWITCH_LABELS (stmt);
940 int old_size = TREE_VEC_LENGTH (labels);
941 int i, j, new_size = old_size;
942 tree default_label = TREE_VEC_ELT (labels, old_size - 1);
944 /* Look for possible opportunities to merge cases.
945 Ignore the last element of the label vector because it
946 must be the default case. */
947 i = 0;
948 while (i < old_size - 2)
950 tree base_case, base_label, base_high, type;
951 base_case = TREE_VEC_ELT (labels, i);
953 if (! base_case)
954 abort ();
956 base_label = CASE_LABEL (base_case);
958 /* Discard cases that have the same destination as the
959 default case. */
960 if (base_label == default_label)
962 TREE_VEC_ELT (labels, i) = NULL_TREE;
963 i++;
964 continue;
967 type = TREE_TYPE (CASE_LOW (base_case));
968 base_high = CASE_HIGH (base_case) ?
969 CASE_HIGH (base_case) : CASE_LOW (base_case);
971 /* Try to merge case labels. Break out when we reach the end
972 of the label vector or when we cannot merge the next case
973 label with the current one. */
974 while (i < old_size - 2)
976 tree merge_case = TREE_VEC_ELT (labels, ++i);
977 tree merge_label = CASE_LABEL (merge_case);
978 tree t = int_const_binop (PLUS_EXPR, base_high,
979 integer_one_node, 1);
981 /* Merge the cases if they jump to the same place,
982 and their ranges are consecutive. */
983 if (merge_label == base_label
984 && tree_int_cst_equal (CASE_LOW (merge_case), t))
986 base_high = CASE_HIGH (merge_case) ?
987 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
988 CASE_HIGH (base_case) = base_high;
989 TREE_VEC_ELT (labels, i) = NULL_TREE;
990 new_size--;
992 else
993 break;
997 /* Compress the case labels in the label vector, and adjust the
998 length of the vector. */
999 for (i = 0, j = 0; i < new_size; i++)
1001 while (! TREE_VEC_ELT (labels, j))
1002 j++;
1003 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1005 TREE_VEC_LENGTH (labels) = new_size;
1010 /* Checks whether we can merge block B into block A. */
1012 static bool
1013 tree_can_merge_blocks_p (basic_block a, basic_block b)
1015 tree stmt;
1016 block_stmt_iterator bsi;
1018 if (!a->succ
1019 || a->succ->succ_next)
1020 return false;
1022 if (a->succ->flags & EDGE_ABNORMAL)
1023 return false;
1025 if (a->succ->dest != b)
1026 return false;
1028 if (b == EXIT_BLOCK_PTR)
1029 return false;
1031 if (b->pred->pred_next)
1032 return false;
1034 /* If A ends by a statement causing exceptions or something similar, we
1035 cannot merge the blocks. */
1036 stmt = last_stmt (a);
1037 if (stmt && stmt_ends_bb_p (stmt))
1038 return false;
1040 /* Do not allow a block with only a non-local label to be merged. */
1041 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1042 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1043 return false;
1045 /* There may be no phi nodes at the start of b. Most of these degenerate
1046 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1047 if (phi_nodes (b))
1048 return false;
1050 /* Do not remove user labels. */
1051 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1053 stmt = bsi_stmt (bsi);
1054 if (TREE_CODE (stmt) != LABEL_EXPR)
1055 break;
1056 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1057 return false;
1060 return true;
1064 /* Merge block B into block A. */
1066 static void
1067 tree_merge_blocks (basic_block a, basic_block b)
1069 block_stmt_iterator bsi;
1070 tree_stmt_iterator last;
1072 if (dump_file)
1073 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1075 /* Ensure that B follows A. */
1076 move_block_after (b, a);
1078 if (!(a->succ->flags & EDGE_FALLTHRU))
1079 abort ();
1081 if (last_stmt (a)
1082 && stmt_ends_bb_p (last_stmt (a)))
1083 abort ();
1085 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1086 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1088 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1089 bsi_remove (&bsi);
1090 else
1092 set_bb_for_stmt (bsi_stmt (bsi), a);
1093 bsi_next (&bsi);
1097 /* Merge the chains. */
1098 last = tsi_last (a->stmt_list);
1099 tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT);
1100 b->stmt_list = NULL;
1104 /* Walk the function tree removing unnecessary statements.
1106 * Empty statement nodes are removed
1108 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1110 * Unnecessary COND_EXPRs are removed
1112 * Some unnecessary BIND_EXPRs are removed
1114 Clearly more work could be done. The trick is doing the analysis
1115 and removal fast enough to be a net improvement in compile times.
1117 Note that when we remove a control structure such as a COND_EXPR
1118 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1119 to ensure we eliminate all the useless code. */
1121 struct rus_data
1123 tree *last_goto;
1124 bool repeat;
1125 bool may_throw;
1126 bool may_branch;
1127 bool has_label;
1130 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1132 static bool
1133 remove_useless_stmts_warn_notreached (tree stmt)
1135 if (EXPR_HAS_LOCATION (stmt))
1137 location_t loc = EXPR_LOCATION (stmt);
1138 warning ("%Hwill never be executed", &loc);
1139 return true;
1142 switch (TREE_CODE (stmt))
1144 case STATEMENT_LIST:
1146 tree_stmt_iterator i;
1147 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1148 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1149 return true;
1151 break;
1153 case COND_EXPR:
1154 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1155 return true;
1156 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1157 return true;
1158 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1159 return true;
1160 break;
1162 case TRY_FINALLY_EXPR:
1163 case TRY_CATCH_EXPR:
1164 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1165 return true;
1166 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1167 return true;
1168 break;
1170 case CATCH_EXPR:
1171 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1172 case EH_FILTER_EXPR:
1173 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1174 case BIND_EXPR:
1175 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1177 default:
1178 /* Not a live container. */
1179 break;
1182 return false;
1185 static void
1186 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1188 tree then_clause, else_clause, cond;
1189 bool save_has_label, then_has_label, else_has_label;
1191 save_has_label = data->has_label;
1192 data->has_label = false;
1193 data->last_goto = NULL;
1195 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1197 then_has_label = data->has_label;
1198 data->has_label = false;
1199 data->last_goto = NULL;
1201 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1203 else_has_label = data->has_label;
1204 data->has_label = save_has_label | then_has_label | else_has_label;
1206 fold_stmt (stmt_p);
1207 then_clause = COND_EXPR_THEN (*stmt_p);
1208 else_clause = COND_EXPR_ELSE (*stmt_p);
1209 cond = COND_EXPR_COND (*stmt_p);
1211 /* If neither arm does anything at all, we can remove the whole IF. */
1212 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1214 *stmt_p = build_empty_stmt ();
1215 data->repeat = true;
1218 /* If there are no reachable statements in an arm, then we can
1219 zap the entire conditional. */
1220 else if (integer_nonzerop (cond) && !else_has_label)
1222 if (warn_notreached)
1223 remove_useless_stmts_warn_notreached (else_clause);
1224 *stmt_p = then_clause;
1225 data->repeat = true;
1227 else if (integer_zerop (cond) && !then_has_label)
1229 if (warn_notreached)
1230 remove_useless_stmts_warn_notreached (then_clause);
1231 *stmt_p = else_clause;
1232 data->repeat = true;
1235 /* Check a couple of simple things on then/else with single stmts. */
1236 else
1238 tree then_stmt = expr_only (then_clause);
1239 tree else_stmt = expr_only (else_clause);
1241 /* Notice branches to a common destination. */
1242 if (then_stmt && else_stmt
1243 && TREE_CODE (then_stmt) == GOTO_EXPR
1244 && TREE_CODE (else_stmt) == GOTO_EXPR
1245 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1247 *stmt_p = then_stmt;
1248 data->repeat = true;
1251 /* If the THEN/ELSE clause merely assigns a value to a variable or
1252 parameter which is already known to contain that value, then
1253 remove the useless THEN/ELSE clause. */
1254 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1256 if (else_stmt
1257 && TREE_CODE (else_stmt) == MODIFY_EXPR
1258 && TREE_OPERAND (else_stmt, 0) == cond
1259 && integer_zerop (TREE_OPERAND (else_stmt, 1)))
1260 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1262 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1263 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1264 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1265 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1267 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1268 ? then_stmt : else_stmt);
1269 tree *location = (TREE_CODE (cond) == EQ_EXPR
1270 ? &COND_EXPR_THEN (*stmt_p)
1271 : &COND_EXPR_ELSE (*stmt_p));
1273 if (stmt
1274 && TREE_CODE (stmt) == MODIFY_EXPR
1275 && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1276 && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1277 *location = alloc_stmt_list ();
1281 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1282 would be re-introduced during lowering. */
1283 data->last_goto = NULL;
1287 static void
1288 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1290 bool save_may_branch, save_may_throw;
1291 bool this_may_branch, this_may_throw;
1293 /* Collect may_branch and may_throw information for the body only. */
1294 save_may_branch = data->may_branch;
1295 save_may_throw = data->may_throw;
1296 data->may_branch = false;
1297 data->may_throw = false;
1298 data->last_goto = NULL;
1300 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1302 this_may_branch = data->may_branch;
1303 this_may_throw = data->may_throw;
1304 data->may_branch |= save_may_branch;
1305 data->may_throw |= save_may_throw;
1306 data->last_goto = NULL;
1308 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1310 /* If the body is empty, then we can emit the FINALLY block without
1311 the enclosing TRY_FINALLY_EXPR. */
1312 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1314 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1315 data->repeat = true;
1318 /* If the handler is empty, then we can emit the TRY block without
1319 the enclosing TRY_FINALLY_EXPR. */
1320 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1322 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1323 data->repeat = true;
1326 /* If the body neither throws, nor branches, then we can safely
1327 string the TRY and FINALLY blocks together. */
1328 else if (!this_may_branch && !this_may_throw)
1330 tree stmt = *stmt_p;
1331 *stmt_p = TREE_OPERAND (stmt, 0);
1332 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1333 data->repeat = true;
1338 static void
1339 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1341 bool save_may_throw, this_may_throw;
1342 tree_stmt_iterator i;
1343 tree stmt;
1345 /* Collect may_throw information for the body only. */
1346 save_may_throw = data->may_throw;
1347 data->may_throw = false;
1348 data->last_goto = NULL;
1350 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1352 this_may_throw = data->may_throw;
1353 data->may_throw = save_may_throw;
1355 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1356 if (!this_may_throw)
1358 if (warn_notreached)
1359 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1360 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1361 data->repeat = true;
1362 return;
1365 /* Process the catch clause specially. We may be able to tell that
1366 no exceptions propagate past this point. */
1368 this_may_throw = true;
1369 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1370 stmt = tsi_stmt (i);
1371 data->last_goto = NULL;
1373 switch (TREE_CODE (stmt))
1375 case CATCH_EXPR:
1376 for (; !tsi_end_p (i); tsi_next (&i))
1378 stmt = tsi_stmt (i);
1379 /* If we catch all exceptions, then the body does not
1380 propagate exceptions past this point. */
1381 if (CATCH_TYPES (stmt) == NULL)
1382 this_may_throw = false;
1383 data->last_goto = NULL;
1384 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1386 break;
1388 case EH_FILTER_EXPR:
1389 if (EH_FILTER_MUST_NOT_THROW (stmt))
1390 this_may_throw = false;
1391 else if (EH_FILTER_TYPES (stmt) == NULL)
1392 this_may_throw = false;
1393 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1394 break;
1396 default:
1397 /* Otherwise this is a cleanup. */
1398 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1400 /* If the cleanup is empty, then we can emit the TRY block without
1401 the enclosing TRY_CATCH_EXPR. */
1402 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1404 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1405 data->repeat = true;
1407 break;
1409 data->may_throw |= this_may_throw;
1413 static void
1414 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1416 tree block;
1418 /* First remove anything underneath the BIND_EXPR. */
1419 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1421 /* If the BIND_EXPR has no variables, then we can pull everything
1422 up one level and remove the BIND_EXPR, unless this is the toplevel
1423 BIND_EXPR for the current function or an inlined function.
1425 When this situation occurs we will want to apply this
1426 optimization again. */
1427 block = BIND_EXPR_BLOCK (*stmt_p);
1428 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1429 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1430 && (! block
1431 || ! BLOCK_ABSTRACT_ORIGIN (block)
1432 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1433 != FUNCTION_DECL)))
1435 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1436 data->repeat = true;
1441 static void
1442 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1444 tree dest = GOTO_DESTINATION (*stmt_p);
1446 data->may_branch = true;
1447 data->last_goto = NULL;
1449 /* Record the last goto expr, so that we can delete it if unnecessary. */
1450 if (TREE_CODE (dest) == LABEL_DECL)
1451 data->last_goto = stmt_p;
1455 static void
1456 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1458 tree label = LABEL_EXPR_LABEL (*stmt_p);
1460 data->has_label = true;
1462 /* We do want to jump across non-local label receiver code. */
1463 if (DECL_NONLOCAL (label))
1464 data->last_goto = NULL;
1466 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1468 *data->last_goto = build_empty_stmt ();
1469 data->repeat = true;
1472 /* ??? Add something here to delete unused labels. */
1476 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1477 decl. This allows us to eliminate redundant or useless
1478 calls to "const" functions.
1480 Gimplifier already does the same operation, but we may notice functions
1481 being const and pure once their calls has been gimplified, so we need
1482 to update the flag. */
1484 static void
1485 update_call_expr_flags (tree call)
1487 tree decl = get_callee_fndecl (call);
1488 if (!decl)
1489 return;
1490 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1491 TREE_SIDE_EFFECTS (call) = 0;
1492 if (TREE_NOTHROW (decl))
1493 TREE_NOTHROW (call) = 1;
1497 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1499 void
1500 notice_special_calls (tree t)
1502 int flags = call_expr_flags (t);
1504 if (flags & ECF_MAY_BE_ALLOCA)
1505 current_function_calls_alloca = true;
1506 if (flags & ECF_RETURNS_TWICE)
1507 current_function_calls_setjmp = true;
1511 /* Clear flags set by notice_special_calls. Used by dead code removal
1512 to update the flags. */
1514 void
1515 clear_special_calls (void)
1517 current_function_calls_alloca = false;
1518 current_function_calls_setjmp = false;
1522 static void
1523 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1525 tree t = *tp, op;
1527 switch (TREE_CODE (t))
1529 case COND_EXPR:
1530 remove_useless_stmts_cond (tp, data);
1531 break;
1533 case TRY_FINALLY_EXPR:
1534 remove_useless_stmts_tf (tp, data);
1535 break;
1537 case TRY_CATCH_EXPR:
1538 remove_useless_stmts_tc (tp, data);
1539 break;
1541 case BIND_EXPR:
1542 remove_useless_stmts_bind (tp, data);
1543 break;
1545 case GOTO_EXPR:
1546 remove_useless_stmts_goto (tp, data);
1547 break;
1549 case LABEL_EXPR:
1550 remove_useless_stmts_label (tp, data);
1551 break;
1553 case RETURN_EXPR:
1554 fold_stmt (tp);
1555 data->last_goto = NULL;
1556 data->may_branch = true;
1557 break;
1559 case CALL_EXPR:
1560 fold_stmt (tp);
1561 data->last_goto = NULL;
1562 notice_special_calls (t);
1563 update_call_expr_flags (t);
1564 if (tree_could_throw_p (t))
1565 data->may_throw = true;
1566 break;
1568 case MODIFY_EXPR:
1569 data->last_goto = NULL;
1570 fold_stmt (tp);
1571 op = get_call_expr_in (t);
1572 if (op)
1574 update_call_expr_flags (op);
1575 notice_special_calls (op);
1577 if (tree_could_throw_p (t))
1578 data->may_throw = true;
1579 break;
1581 case STATEMENT_LIST:
1583 tree_stmt_iterator i = tsi_start (t);
1584 while (!tsi_end_p (i))
1586 t = tsi_stmt (i);
1587 if (IS_EMPTY_STMT (t))
1589 tsi_delink (&i);
1590 continue;
1593 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1595 t = tsi_stmt (i);
1596 if (TREE_CODE (t) == STATEMENT_LIST)
1598 tsi_link_before (&i, t, TSI_SAME_STMT);
1599 tsi_delink (&i);
1601 else
1602 tsi_next (&i);
1605 break;
1606 case SWITCH_EXPR:
1607 fold_stmt (tp);
1608 data->last_goto = NULL;
1609 break;
1611 default:
1612 data->last_goto = NULL;
1613 break;
1617 static void
1618 remove_useless_stmts (void)
1620 struct rus_data data;
1622 clear_special_calls ();
1626 memset (&data, 0, sizeof (data));
1627 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1629 while (data.repeat);
1633 struct tree_opt_pass pass_remove_useless_stmts =
1635 "useless", /* name */
1636 NULL, /* gate */
1637 remove_useless_stmts, /* execute */
1638 NULL, /* sub */
1639 NULL, /* next */
1640 0, /* static_pass_number */
1641 0, /* tv_id */
1642 PROP_gimple_any, /* properties_required */
1643 0, /* properties_provided */
1644 0, /* properties_destroyed */
1645 0, /* todo_flags_start */
1646 TODO_dump_func /* todo_flags_finish */
1650 /* Remove obviously useless statements in basic block BB. */
1652 static void
1653 cfg_remove_useless_stmts_bb (basic_block bb)
1655 block_stmt_iterator bsi;
1656 tree stmt = NULL_TREE;
1657 tree cond, var = NULL_TREE, val = NULL_TREE;
1658 struct var_ann_d *ann;
1660 /* Check whether we come here from a condition, and if so, get the
1661 condition. */
1662 if (!bb->pred
1663 || bb->pred->pred_next
1664 || !(bb->pred->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1665 return;
1667 cond = COND_EXPR_COND (last_stmt (bb->pred->src));
1669 if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1671 var = cond;
1672 val = (bb->pred->flags & EDGE_FALSE_VALUE
1673 ? boolean_false_node : boolean_true_node);
1675 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR
1676 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1677 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL))
1679 var = TREE_OPERAND (cond, 0);
1680 val = (bb->pred->flags & EDGE_FALSE_VALUE
1681 ? boolean_true_node : boolean_false_node);
1683 else
1685 if (bb->pred->flags & EDGE_FALSE_VALUE)
1686 cond = invert_truthvalue (cond);
1687 if (TREE_CODE (cond) == EQ_EXPR
1688 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1689 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1690 && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL
1691 || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL
1692 || TREE_CONSTANT (TREE_OPERAND (cond, 1))))
1694 var = TREE_OPERAND (cond, 0);
1695 val = TREE_OPERAND (cond, 1);
1697 else
1698 return;
1701 /* Only work for normal local variables. */
1702 ann = var_ann (var);
1703 if (!ann
1704 || ann->may_aliases
1705 || TREE_ADDRESSABLE (var))
1706 return;
1708 if (! TREE_CONSTANT (val))
1710 ann = var_ann (val);
1711 if (!ann
1712 || ann->may_aliases
1713 || TREE_ADDRESSABLE (val))
1714 return;
1717 /* Ignore floating point variables, since comparison behaves weird for
1718 them. */
1719 if (FLOAT_TYPE_P (TREE_TYPE (var)))
1720 return;
1722 for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
1724 stmt = bsi_stmt (bsi);
1726 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1727 which is already known to contain that value, then remove the useless
1728 THEN/ELSE clause. */
1729 if (TREE_CODE (stmt) == MODIFY_EXPR
1730 && TREE_OPERAND (stmt, 0) == var
1731 && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0))
1733 bsi_remove (&bsi);
1734 continue;
1737 /* Invalidate the var if we encounter something that could modify it. */
1738 if (TREE_CODE (stmt) == ASM_EXPR
1739 || (TREE_CODE (stmt) == MODIFY_EXPR
1740 && TREE_OPERAND (stmt, 0) == var))
1741 return;
1743 bsi_next (&bsi);
1748 /* A CFG-aware version of remove_useless_stmts. */
1750 void
1751 cfg_remove_useless_stmts (void)
1753 basic_block bb;
1755 #ifdef ENABLE_CHECKING
1756 verify_flow_info ();
1757 #endif
1759 FOR_EACH_BB (bb)
1761 cfg_remove_useless_stmts_bb (bb);
1766 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1768 static void
1769 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1771 tree phi;
1773 /* Since this block is no longer reachable, we can just delete all
1774 of its PHI nodes. */
1775 phi = phi_nodes (bb);
1776 while (phi)
1778 tree next = PHI_CHAIN (phi);
1779 remove_phi_node (phi, NULL_TREE, bb);
1780 phi = next;
1783 /* Remove edges to BB's successors. */
1784 while (bb->succ != NULL)
1785 ssa_remove_edge (bb->succ);
1789 /* Remove statements of basic block BB. */
1791 static void
1792 remove_bb (basic_block bb)
1794 block_stmt_iterator i;
1795 source_locus loc = 0;
1797 if (dump_file)
1799 fprintf (dump_file, "Removing basic block %d\n", bb->index);
1800 if (dump_flags & TDF_DETAILS)
1802 dump_bb (bb, dump_file, 0);
1803 fprintf (dump_file, "\n");
1807 /* Remove all the instructions in the block. */
1808 for (i = bsi_start (bb); !bsi_end_p (i); bsi_remove (&i))
1810 tree stmt = bsi_stmt (i);
1812 set_bb_for_stmt (stmt, NULL);
1814 /* Don't warn for removed gotos. Gotos are often removed due to
1815 jump threading, thus resulting in bogus warnings. Not great,
1816 since this way we lose warnings for gotos in the original
1817 program that are indeed unreachable. */
1818 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
1819 #ifdef USE_MAPPED_LOCATION
1820 loc = EXPR_LOCATION (stmt);
1821 #else
1822 loc = EXPR_LOCUS (stmt);
1823 #endif
1826 /* If requested, give a warning that the first statement in the
1827 block is unreachable. We walk statements backwards in the
1828 loop above, so the last statement we process is the first statement
1829 in the block. */
1830 if (warn_notreached && loc)
1831 #ifdef USE_MAPPED_LOCATION
1832 warning ("%Hwill never be executed", &loc);
1833 #else
1834 warning ("%Hwill never be executed", loc);
1835 #endif
1837 remove_phi_nodes_and_edges_for_unreachable_block (bb);
1841 /* Examine BB to determine if it is a forwarding block (a block which only
1842 transfers control to a new destination). If BB is a forwarding block,
1843 then return the edge leading to the ultimate destination. */
1845 edge
1846 tree_block_forwards_to (basic_block bb)
1848 block_stmt_iterator bsi;
1849 bb_ann_t ann = bb_ann (bb);
1850 tree stmt;
1852 /* If this block is not forwardable, then avoid useless work. */
1853 if (! ann->forwardable)
1854 return NULL;
1856 /* Set this block to not be forwardable. This prevents infinite loops since
1857 any block currently under examination is considered non-forwardable. */
1858 ann->forwardable = 0;
1860 /* No forwarding is possible if this block is a special block (ENTRY/EXIT),
1861 this block has more than one successor, this block's single successor is
1862 reached via an abnormal edge, this block has phi nodes, or this block's
1863 single successor has phi nodes. */
1864 if (bb == EXIT_BLOCK_PTR
1865 || bb == ENTRY_BLOCK_PTR
1866 || !bb->succ
1867 || bb->succ->succ_next
1868 || bb->succ->dest == EXIT_BLOCK_PTR
1869 || (bb->succ->flags & EDGE_ABNORMAL) != 0
1870 || phi_nodes (bb)
1871 || phi_nodes (bb->succ->dest))
1872 return NULL;
1874 /* Walk past any labels at the start of this block. */
1875 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1877 stmt = bsi_stmt (bsi);
1878 if (TREE_CODE (stmt) != LABEL_EXPR)
1879 break;
1882 /* If we reached the end of this block we may be able to optimize this
1883 case. */
1884 if (bsi_end_p (bsi))
1886 edge dest;
1888 /* Recursive call to pick up chains of forwarding blocks. */
1889 dest = tree_block_forwards_to (bb->succ->dest);
1891 /* If none found, we forward to bb->succ at minimum. */
1892 if (!dest)
1893 dest = bb->succ;
1895 ann->forwardable = 1;
1896 return dest;
1899 /* No forwarding possible. */
1900 return NULL;
1904 /* Try to remove superfluous control structures. */
1906 static bool
1907 cleanup_control_flow (void)
1909 basic_block bb;
1910 block_stmt_iterator bsi;
1911 bool retval = false;
1912 tree stmt;
1914 FOR_EACH_BB (bb)
1916 bsi = bsi_last (bb);
1918 if (bsi_end_p (bsi))
1919 continue;
1921 stmt = bsi_stmt (bsi);
1922 if (TREE_CODE (stmt) == COND_EXPR
1923 || TREE_CODE (stmt) == SWITCH_EXPR)
1924 retval |= cleanup_control_expr_graph (bb, bsi);
1926 return retval;
1930 /* Disconnect an unreachable block in the control expression starting
1931 at block BB. */
1933 static bool
1934 cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
1936 edge taken_edge;
1937 bool retval = false;
1938 tree expr = bsi_stmt (bsi), val;
1940 if (bb->succ->succ_next)
1942 edge e, next;
1944 switch (TREE_CODE (expr))
1946 case COND_EXPR:
1947 val = COND_EXPR_COND (expr);
1948 break;
1950 case SWITCH_EXPR:
1951 val = SWITCH_COND (expr);
1952 if (TREE_CODE (val) != INTEGER_CST)
1953 return false;
1954 break;
1956 default:
1957 abort ();
1960 taken_edge = find_taken_edge (bb, val);
1961 if (!taken_edge)
1962 return false;
1964 /* Remove all the edges except the one that is always executed. */
1965 for (e = bb->succ; e; e = next)
1967 next = e->succ_next;
1968 if (e != taken_edge)
1970 taken_edge->probability += e->probability;
1971 taken_edge->count += e->count;
1972 ssa_remove_edge (e);
1973 retval = true;
1976 if (taken_edge->probability > REG_BR_PROB_BASE)
1977 taken_edge->probability = REG_BR_PROB_BASE;
1979 else
1980 taken_edge = bb->succ;
1982 bsi_remove (&bsi);
1983 taken_edge->flags = EDGE_FALLTHRU;
1985 /* We removed some paths from the cfg. */
1986 if (dom_computed[CDI_DOMINATORS] >= DOM_CONS_OK)
1987 dom_computed[CDI_DOMINATORS] = DOM_CONS_OK;
1989 return retval;
1993 /* Given a control block BB and a predicate VAL, return the edge that
1994 will be taken out of the block. If VAL does not match a unique
1995 edge, NULL is returned. */
1997 edge
1998 find_taken_edge (basic_block bb, tree val)
2000 tree stmt;
2002 stmt = last_stmt (bb);
2004 #if defined ENABLE_CHECKING
2005 if (stmt == NULL_TREE || !is_ctrl_stmt (stmt))
2006 abort ();
2007 #endif
2009 /* If VAL is a predicate of the form N RELOP N, where N is an
2010 SSA_NAME, we can always determine its truth value (except when
2011 doing floating point comparisons that may involve NaNs). */
2012 if (val
2013 && TREE_CODE_CLASS (TREE_CODE (val)) == '<'
2014 && TREE_OPERAND (val, 0) == TREE_OPERAND (val, 1)
2015 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME
2016 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (val, 0))) != REAL_TYPE
2017 || !HONOR_NANS (TYPE_MODE (TREE_TYPE (TREE_OPERAND (val, 0))))))
2019 enum tree_code code = TREE_CODE (val);
2021 if (code == EQ_EXPR || code == LE_EXPR || code == GE_EXPR)
2022 val = boolean_true_node;
2023 else if (code == LT_EXPR || code == GT_EXPR || code == NE_EXPR)
2024 val = boolean_false_node;
2027 /* If VAL is not a constant, we can't determine which edge might
2028 be taken. */
2029 if (val == NULL || !really_constant_p (val))
2030 return NULL;
2032 if (TREE_CODE (stmt) == COND_EXPR)
2033 return find_taken_edge_cond_expr (bb, val);
2035 if (TREE_CODE (stmt) == SWITCH_EXPR)
2036 return find_taken_edge_switch_expr (bb, val);
2038 return bb->succ;
2042 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2043 statement, determine which of the two edges will be taken out of the
2044 block. Return NULL if either edge may be taken. */
2046 static edge
2047 find_taken_edge_cond_expr (basic_block bb, tree val)
2049 edge true_edge, false_edge;
2051 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2053 /* If both edges of the branch lead to the same basic block, it doesn't
2054 matter which edge is taken. */
2055 if (true_edge->dest == false_edge->dest)
2056 return true_edge;
2058 /* Otherwise, try to determine which branch of the if() will be taken.
2059 If VAL is a constant but it can't be reduced to a 0 or a 1, then
2060 we don't really know which edge will be taken at runtime. This
2061 may happen when comparing addresses (e.g., if (&var1 == 4)). */
2062 if (integer_nonzerop (val))
2063 return true_edge;
2064 else if (integer_zerop (val))
2065 return false_edge;
2066 else
2067 return NULL;
2071 /* Given a constant value VAL and the entry block BB to a SWITCH_EXPR
2072 statement, determine which edge will be taken out of the block. Return
2073 NULL if any edge may be taken. */
2075 static edge
2076 find_taken_edge_switch_expr (basic_block bb, tree val)
2078 tree switch_expr, taken_case;
2079 basic_block dest_bb;
2080 edge e;
2082 if (TREE_CODE (val) != INTEGER_CST)
2083 return NULL;
2085 switch_expr = last_stmt (bb);
2086 taken_case = find_case_label_for_value (switch_expr, val);
2087 dest_bb = label_to_block (CASE_LABEL (taken_case));
2089 e = find_edge (bb, dest_bb);
2090 if (!e)
2091 abort ();
2092 return e;
2096 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2097 We can make optimal use here of the fact that the case labels are
2098 sorted: We can do a binary search for a case matching VAL. */
2100 static tree
2101 find_case_label_for_value (tree switch_expr, tree val)
2103 tree vec = SWITCH_LABELS (switch_expr);
2104 size_t low, high, n = TREE_VEC_LENGTH (vec);
2105 tree default_case = TREE_VEC_ELT (vec, n - 1);
2107 for (low = -1, high = n - 1; high - low > 1; )
2109 size_t i = (high + low) / 2;
2110 tree t = TREE_VEC_ELT (vec, i);
2111 int cmp;
2113 /* Cache the result of comparing CASE_LOW and val. */
2114 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2116 if (cmp > 0)
2117 high = i;
2118 else
2119 low = i;
2121 if (CASE_HIGH (t) == NULL)
2123 /* A singe-valued case label. */
2124 if (cmp == 0)
2125 return t;
2127 else
2129 /* A case range. We can only handle integer ranges. */
2130 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2131 return t;
2135 return default_case;
2139 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2140 those alternatives are equal in each of the PHI nodes, then return
2141 true, else return false. */
2143 static bool
2144 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
2146 tree phi, val1, val2;
2147 int n1, n2;
2149 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2151 n1 = phi_arg_from_edge (phi, e1);
2152 n2 = phi_arg_from_edge (phi, e2);
2154 #ifdef ENABLE_CHECKING
2155 if (n1 < 0 || n2 < 0)
2156 abort ();
2157 #endif
2159 val1 = PHI_ARG_DEF (phi, n1);
2160 val2 = PHI_ARG_DEF (phi, n2);
2162 if (!operand_equal_p (val1, val2, 0))
2163 return false;
2166 return true;
2170 /* Computing the Dominance Frontier:
2172 As described in Morgan, section 3.5, this may be done simply by
2173 walking the dominator tree bottom-up, computing the frontier for
2174 the children before the parent. When considering a block B,
2175 there are two cases:
2177 (1) A flow graph edge leaving B that does not lead to a child
2178 of B in the dominator tree must be a block that is either equal
2179 to B or not dominated by B. Such blocks belong in the frontier
2180 of B.
2182 (2) Consider a block X in the frontier of one of the children C
2183 of B. If X is not equal to B and is not dominated by B, it
2184 is in the frontier of B. */
2186 static void
2187 compute_dominance_frontiers_1 (bitmap *frontiers, basic_block bb, sbitmap done)
2189 edge e;
2190 basic_block c;
2192 SET_BIT (done, bb->index);
2194 /* Do the frontier of the children first. Not all children in the
2195 dominator tree (blocks dominated by this one) are children in the
2196 CFG, so check all blocks. */
2197 for (c = first_dom_son (CDI_DOMINATORS, bb);
2199 c = next_dom_son (CDI_DOMINATORS, c))
2201 if (! TEST_BIT (done, c->index))
2202 compute_dominance_frontiers_1 (frontiers, c, done);
2205 /* Find blocks conforming to rule (1) above. */
2206 for (e = bb->succ; e; e = e->succ_next)
2208 if (e->dest == EXIT_BLOCK_PTR)
2209 continue;
2210 if (get_immediate_dominator (CDI_DOMINATORS, e->dest) != bb)
2211 bitmap_set_bit (frontiers[bb->index], e->dest->index);
2214 /* Find blocks conforming to rule (2). */
2215 for (c = first_dom_son (CDI_DOMINATORS, bb);
2217 c = next_dom_son (CDI_DOMINATORS, c))
2219 int x;
2221 EXECUTE_IF_SET_IN_BITMAP (frontiers[c->index], 0, x,
2223 if (get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (x)) != bb)
2224 bitmap_set_bit (frontiers[bb->index], x);
2230 void
2231 compute_dominance_frontiers (bitmap *frontiers)
2233 sbitmap done = sbitmap_alloc (last_basic_block);
2235 timevar_push (TV_DOM_FRONTIERS);
2237 sbitmap_zero (done);
2239 compute_dominance_frontiers_1 (frontiers, ENTRY_BLOCK_PTR->succ->dest, done);
2241 sbitmap_free (done);
2243 timevar_pop (TV_DOM_FRONTIERS);
2248 /*---------------------------------------------------------------------------
2249 Debugging functions
2250 ---------------------------------------------------------------------------*/
2252 /* Dump tree-specific information of block BB to file OUTF. */
2254 void
2255 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2257 dump_generic_bb (outf, bb, indent, TDF_VOPS);
2261 /* Dump a basic block on stderr. */
2263 void
2264 debug_tree_bb (basic_block bb)
2266 dump_bb (bb, stderr, 0);
2270 /* Dump basic block with index N on stderr. */
2272 basic_block
2273 debug_tree_bb_n (int n)
2275 debug_tree_bb (BASIC_BLOCK (n));
2276 return BASIC_BLOCK (n);
2280 /* Dump the CFG on stderr.
2282 FLAGS are the same used by the tree dumping functions
2283 (see TDF_* in tree.h). */
2285 void
2286 debug_tree_cfg (int flags)
2288 dump_tree_cfg (stderr, flags);
2292 /* Dump the program showing basic block boundaries on the given FILE.
2294 FLAGS are the same used by the tree dumping functions (see TDF_* in
2295 tree.h). */
2297 void
2298 dump_tree_cfg (FILE *file, int flags)
2300 if (flags & TDF_DETAILS)
2302 const char *funcname
2303 = lang_hooks.decl_printable_name (current_function_decl, 2);
2305 fputc ('\n', file);
2306 fprintf (file, ";; Function %s\n\n", funcname);
2307 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2308 n_basic_blocks, n_edges, last_basic_block);
2310 brief_dump_cfg (file);
2311 fprintf (file, "\n");
2314 if (flags & TDF_STATS)
2315 dump_cfg_stats (file);
2317 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2321 /* Dump CFG statistics on FILE. */
2323 void
2324 dump_cfg_stats (FILE *file)
2326 static long max_num_merged_labels = 0;
2327 unsigned long size, total = 0;
2328 int n_edges;
2329 basic_block bb;
2330 const char * const fmt_str = "%-30s%-13s%12s\n";
2331 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2332 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2333 const char *funcname
2334 = lang_hooks.decl_printable_name (current_function_decl, 2);
2337 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2339 fprintf (file, "---------------------------------------------------------\n");
2340 fprintf (file, fmt_str, "", " Number of ", "Memory");
2341 fprintf (file, fmt_str, "", " instances ", "used ");
2342 fprintf (file, "---------------------------------------------------------\n");
2344 size = n_basic_blocks * sizeof (struct basic_block_def);
2345 total += size;
2346 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2347 SCALE (size), LABEL (size));
2349 n_edges = 0;
2350 FOR_EACH_BB (bb)
2352 edge e;
2353 for (e = bb->succ; e; e = e->succ_next)
2354 n_edges++;
2356 size = n_edges * sizeof (struct edge_def);
2357 total += size;
2358 fprintf (file, fmt_str_1, "Edges", n_edges, SCALE (size), LABEL (size));
2360 size = n_basic_blocks * sizeof (struct bb_ann_d);
2361 total += size;
2362 fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks,
2363 SCALE (size), LABEL (size));
2365 fprintf (file, "---------------------------------------------------------\n");
2366 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2367 LABEL (total));
2368 fprintf (file, "---------------------------------------------------------\n");
2369 fprintf (file, "\n");
2371 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2372 max_num_merged_labels = cfg_stats.num_merged_labels;
2374 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2375 cfg_stats.num_merged_labels, max_num_merged_labels);
2377 fprintf (file, "\n");
2381 /* Dump CFG statistics on stderr. Keep extern so that it's always
2382 linked in the final executable. */
2384 void
2385 debug_cfg_stats (void)
2387 dump_cfg_stats (stderr);
2391 /* Dump the flowgraph to a .vcg FILE. */
2393 static void
2394 tree_cfg2vcg (FILE *file)
2396 edge e;
2397 basic_block bb;
2398 const char *funcname
2399 = lang_hooks.decl_printable_name (current_function_decl, 2);
2401 /* Write the file header. */
2402 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2403 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2404 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2406 /* Write blocks and edges. */
2407 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
2409 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2410 e->dest->index);
2412 if (e->flags & EDGE_FAKE)
2413 fprintf (file, " linestyle: dotted priority: 10");
2414 else
2415 fprintf (file, " linestyle: solid priority: 100");
2417 fprintf (file, " }\n");
2419 fputc ('\n', file);
2421 FOR_EACH_BB (bb)
2423 enum tree_code head_code, end_code;
2424 const char *head_name, *end_name;
2425 int head_line = 0;
2426 int end_line = 0;
2427 tree first = first_stmt (bb);
2428 tree last = last_stmt (bb);
2430 if (first)
2432 head_code = TREE_CODE (first);
2433 head_name = tree_code_name[head_code];
2434 head_line = get_lineno (first);
2436 else
2437 head_name = "no-statement";
2439 if (last)
2441 end_code = TREE_CODE (last);
2442 end_name = tree_code_name[end_code];
2443 end_line = get_lineno (last);
2445 else
2446 end_name = "no-statement";
2448 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2449 bb->index, bb->index, head_name, head_line, end_name,
2450 end_line);
2452 for (e = bb->succ; e; e = e->succ_next)
2454 if (e->dest == EXIT_BLOCK_PTR)
2455 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2456 else
2457 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2459 if (e->flags & EDGE_FAKE)
2460 fprintf (file, " priority: 10 linestyle: dotted");
2461 else
2462 fprintf (file, " priority: 100 linestyle: solid");
2464 fprintf (file, " }\n");
2467 if (bb->next_bb != EXIT_BLOCK_PTR)
2468 fputc ('\n', file);
2471 fputs ("}\n\n", file);
2476 /*---------------------------------------------------------------------------
2477 Miscellaneous helpers
2478 ---------------------------------------------------------------------------*/
2480 /* Return true if T represents a stmt that always transfers control. */
2482 bool
2483 is_ctrl_stmt (tree t)
2485 return (TREE_CODE (t) == COND_EXPR
2486 || TREE_CODE (t) == SWITCH_EXPR
2487 || TREE_CODE (t) == GOTO_EXPR
2488 || TREE_CODE (t) == RETURN_EXPR
2489 || TREE_CODE (t) == RESX_EXPR);
2493 /* Return true if T is a statement that may alter the flow of control
2494 (e.g., a call to a non-returning function). */
2496 bool
2497 is_ctrl_altering_stmt (tree t)
2499 tree call;
2501 #if defined ENABLE_CHECKING
2502 if (t == NULL)
2503 abort ();
2504 #endif
2506 call = get_call_expr_in (t);
2507 if (call)
2509 /* A non-pure/const CALL_EXPR alters flow control if the current
2510 function has nonlocal labels. */
2511 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2512 return true;
2514 /* A CALL_EXPR also alters control flow if it does not return. */
2515 if (call_expr_flags (call) & (ECF_NORETURN | ECF_LONGJMP))
2516 return true;
2519 /* If a statement can throw, it alters control flow. */
2520 return tree_can_throw_internal (t);
2524 /* Return true if T is a computed goto. */
2526 bool
2527 computed_goto_p (tree t)
2529 return (TREE_CODE (t) == GOTO_EXPR
2530 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2534 /* Checks whether EXPR is a simple local goto. */
2536 bool
2537 simple_goto_p (tree expr)
2539 return (TREE_CODE (expr) == GOTO_EXPR
2540 && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL);
2544 /* Return true if T should start a new basic block. PREV_T is the
2545 statement preceding T. It is used when T is a label or a case label.
2546 Labels should only start a new basic block if their previous statement
2547 wasn't a label. Otherwise, sequence of labels would generate
2548 unnecessary basic blocks that only contain a single label. */
2550 static inline bool
2551 stmt_starts_bb_p (tree t, tree prev_t)
2553 enum tree_code code;
2555 if (t == NULL_TREE)
2556 return false;
2558 /* LABEL_EXPRs start a new basic block only if the preceding
2559 statement wasn't a label of the same type. This prevents the
2560 creation of consecutive blocks that have nothing but a single
2561 label. */
2562 code = TREE_CODE (t);
2563 if (code == LABEL_EXPR)
2565 /* Nonlocal and computed GOTO targets always start a new block. */
2566 if (code == LABEL_EXPR
2567 && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2568 || FORCED_LABEL (LABEL_EXPR_LABEL (t))))
2569 return true;
2571 if (prev_t && TREE_CODE (prev_t) == code)
2573 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2574 return true;
2576 cfg_stats.num_merged_labels++;
2577 return false;
2579 else
2580 return true;
2583 return false;
2587 /* Return true if T should end a basic block. */
2589 bool
2590 stmt_ends_bb_p (tree t)
2592 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2596 /* Add gotos that used to be represented implicitly in the CFG. */
2598 void
2599 disband_implicit_edges (void)
2601 basic_block bb;
2602 block_stmt_iterator last;
2603 edge e;
2604 tree stmt, label;
2606 FOR_EACH_BB (bb)
2608 last = bsi_last (bb);
2609 stmt = last_stmt (bb);
2611 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2613 /* Remove superfluous gotos from COND_EXPR branches. Moved
2614 from cfg_remove_useless_stmts here since it violates the
2615 invariants for tree--cfg correspondence and thus fits better
2616 here where we do it anyway. */
2617 for (e = bb->succ; e; e = e->succ_next)
2619 if (e->dest != bb->next_bb)
2620 continue;
2622 if (e->flags & EDGE_TRUE_VALUE)
2623 COND_EXPR_THEN (stmt) = build_empty_stmt ();
2624 else if (e->flags & EDGE_FALSE_VALUE)
2625 COND_EXPR_ELSE (stmt) = build_empty_stmt ();
2626 else
2627 abort ();
2628 e->flags |= EDGE_FALLTHRU;
2631 continue;
2634 if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
2636 /* Remove the RETURN_EXPR if we may fall though to the exit
2637 instead. */
2638 if (!bb->succ
2639 || bb->succ->succ_next
2640 || bb->succ->dest != EXIT_BLOCK_PTR)
2641 abort ();
2643 if (bb->next_bb == EXIT_BLOCK_PTR
2644 && !TREE_OPERAND (stmt, 0))
2646 bsi_remove (&last);
2647 bb->succ->flags |= EDGE_FALLTHRU;
2649 continue;
2652 /* There can be no fallthru edge if the last statement is a control
2653 one. */
2654 if (stmt && is_ctrl_stmt (stmt))
2655 continue;
2657 /* Find a fallthru edge and emit the goto if necessary. */
2658 for (e = bb->succ; e; e = e->succ_next)
2659 if (e->flags & EDGE_FALLTHRU)
2660 break;
2662 if (!e || e->dest == bb->next_bb)
2663 continue;
2665 if (e->dest == EXIT_BLOCK_PTR)
2666 abort ();
2668 label = tree_block_label (e->dest);
2670 stmt = build1 (GOTO_EXPR, void_type_node, label);
2671 #ifdef USE_MAPPED_LOCATION
2672 SET_EXPR_LOCATION (stmt, e->goto_locus);
2673 #else
2674 SET_EXPR_LOCUS (stmt, e->goto_locus);
2675 #endif
2676 bsi_insert_after (&last, stmt, BSI_NEW_STMT);
2677 e->flags &= ~EDGE_FALLTHRU;
2681 /* Remove block annotations and other datastructures. */
2683 void
2684 delete_tree_cfg_annotations (void)
2686 basic_block bb;
2687 if (n_basic_blocks > 0)
2688 free_blocks_annotations ();
2690 label_to_block_map = NULL;
2691 free_rbi_pool ();
2692 FOR_EACH_BB (bb)
2693 bb->rbi = NULL;
2697 /* Return the first statement in basic block BB. */
2699 tree
2700 first_stmt (basic_block bb)
2702 block_stmt_iterator i = bsi_start (bb);
2703 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2707 /* Return the last statement in basic block BB. */
2709 tree
2710 last_stmt (basic_block bb)
2712 block_stmt_iterator b = bsi_last (bb);
2713 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2717 /* Return a pointer to the last statement in block BB. */
2719 tree *
2720 last_stmt_ptr (basic_block bb)
2722 block_stmt_iterator last = bsi_last (bb);
2723 return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL;
2727 /* Return the last statement of an otherwise empty block. Return NULL
2728 if the block is totally empty, or if it contains more than one
2729 statement. */
2731 tree
2732 last_and_only_stmt (basic_block bb)
2734 block_stmt_iterator i = bsi_last (bb);
2735 tree last, prev;
2737 if (bsi_end_p (i))
2738 return NULL_TREE;
2740 last = bsi_stmt (i);
2741 bsi_prev (&i);
2742 if (bsi_end_p (i))
2743 return last;
2745 /* Empty statements should no longer appear in the instruction stream.
2746 Everything that might have appeared before should be deleted by
2747 remove_useless_stmts, and the optimizers should just bsi_remove
2748 instead of smashing with build_empty_stmt.
2750 Thus the only thing that should appear here in a block containing
2751 one executable statement is a label. */
2752 prev = bsi_stmt (i);
2753 if (TREE_CODE (prev) == LABEL_EXPR)
2754 return last;
2755 else
2756 return NULL_TREE;
2760 /* Mark BB as the basic block holding statement T. */
2762 void
2763 set_bb_for_stmt (tree t, basic_block bb)
2765 if (TREE_CODE (t) == STATEMENT_LIST)
2767 tree_stmt_iterator i;
2768 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2769 set_bb_for_stmt (tsi_stmt (i), bb);
2771 else
2773 stmt_ann_t ann = get_stmt_ann (t);
2774 ann->bb = bb;
2776 /* If the statement is a label, add the label to block-to-labels map
2777 so that we can speed up edge creation for GOTO_EXPRs. */
2778 if (TREE_CODE (t) == LABEL_EXPR)
2780 int uid;
2782 t = LABEL_EXPR_LABEL (t);
2783 uid = LABEL_DECL_UID (t);
2784 if (uid == -1)
2786 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2787 if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid)
2788 VARRAY_GROW (label_to_block_map, 3 * uid / 2);
2790 else
2792 #ifdef ENABLE_CHECKING
2793 /* We're moving an existing label. Make sure that we've
2794 removed it from the old block. */
2795 if (bb && VARRAY_BB (label_to_block_map, uid))
2796 abort ();
2797 #endif
2799 VARRAY_BB (label_to_block_map, uid) = bb;
2805 /* Insert statement (or statement list) T before the statement
2806 pointed-to by iterator I. M specifies how to update iterator I
2807 after insertion (see enum bsi_iterator_update). */
2809 void
2810 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2812 set_bb_for_stmt (t, i->bb);
2813 modify_stmt (t);
2814 tsi_link_before (&i->tsi, t, m);
2818 /* Insert statement (or statement list) T after the statement
2819 pointed-to by iterator I. M specifies how to update iterator I
2820 after insertion (see enum bsi_iterator_update). */
2822 void
2823 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2825 set_bb_for_stmt (t, i->bb);
2826 modify_stmt (t);
2827 tsi_link_after (&i->tsi, t, m);
2831 /* Remove the statement pointed to by iterator I. The iterator is updated
2832 to the next statement. */
2834 void
2835 bsi_remove (block_stmt_iterator *i)
2837 tree t = bsi_stmt (*i);
2838 set_bb_for_stmt (t, NULL);
2839 modify_stmt (t);
2840 tsi_delink (&i->tsi);
2844 /* Move the statement at FROM so it comes right after the statement at TO. */
2846 void
2847 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
2849 tree stmt = bsi_stmt (*from);
2850 bsi_remove (from);
2851 bsi_insert_after (to, stmt, BSI_SAME_STMT);
2855 /* Move the statement at FROM so it comes right before the statement at TO. */
2857 void
2858 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
2860 tree stmt = bsi_stmt (*from);
2861 bsi_remove (from);
2862 bsi_insert_before (to, stmt, BSI_SAME_STMT);
2866 /* Move the statement at FROM to the end of basic block BB. */
2868 void
2869 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
2871 block_stmt_iterator last = bsi_last (bb);
2873 /* Have to check bsi_end_p because it could be an empty block. */
2874 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
2875 bsi_move_before (from, &last);
2876 else
2877 bsi_move_after (from, &last);
2881 /* Replace the contents of the statement pointed to by iterator BSI
2882 with STMT. If PRESERVE_EH_INFO is true, the exception handling
2883 information of the original statement is preserved. */
2885 void
2886 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
2888 int eh_region;
2889 tree orig_stmt = bsi_stmt (*bsi);
2891 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
2892 set_bb_for_stmt (stmt, bsi->bb);
2894 /* Preserve EH region information from the original statement, if
2895 requested by the caller. */
2896 if (preserve_eh_info)
2898 eh_region = lookup_stmt_eh_region (orig_stmt);
2899 if (eh_region >= 0)
2900 add_stmt_to_eh_region (stmt, eh_region);
2903 *bsi_stmt_ptr (*bsi) = stmt;
2904 modify_stmt (stmt);
2908 /* Insert the statement pointed-to by BSI into edge E. Every attempt
2909 is made to place the statement in an existing basic block, but
2910 sometimes that isn't possible. When it isn't possible, the edge is
2911 split and the statement is added to the new block.
2913 In all cases, the returned *BSI points to the correct location. The
2914 return value is true if insertion should be done after the location,
2915 or false if it should be done before the location. */
2917 static bool
2918 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi)
2920 basic_block dest, src;
2921 tree tmp;
2923 dest = e->dest;
2924 restart:
2926 /* If the destination has one predecessor which has no PHI nodes,
2927 insert there. Except for the exit block.
2929 The requirement for no PHI nodes could be relaxed. Basically we
2930 would have to examine the PHIs to prove that none of them used
2931 the value set by the statement we want to insert on E. That
2932 hardly seems worth the effort. */
2933 if (dest->pred->pred_next == NULL
2934 && ! phi_nodes (dest)
2935 && dest != EXIT_BLOCK_PTR)
2937 *bsi = bsi_start (dest);
2938 if (bsi_end_p (*bsi))
2939 return true;
2941 /* Make sure we insert after any leading labels. */
2942 tmp = bsi_stmt (*bsi);
2943 while (TREE_CODE (tmp) == LABEL_EXPR)
2945 bsi_next (bsi);
2946 if (bsi_end_p (*bsi))
2947 break;
2948 tmp = bsi_stmt (*bsi);
2951 if (bsi_end_p (*bsi))
2953 *bsi = bsi_last (dest);
2954 return true;
2956 else
2957 return false;
2960 /* If the source has one successor, the edge is not abnormal and
2961 the last statement does not end a basic block, insert there.
2962 Except for the entry block. */
2963 src = e->src;
2964 if ((e->flags & EDGE_ABNORMAL) == 0
2965 && src->succ->succ_next == NULL
2966 && src != ENTRY_BLOCK_PTR)
2968 *bsi = bsi_last (src);
2969 if (bsi_end_p (*bsi))
2970 return true;
2972 tmp = bsi_stmt (*bsi);
2973 if (!stmt_ends_bb_p (tmp))
2974 return true;
2976 /* Insert code just before returning the value. We may need to decompose
2977 the return in the case it contains non-trivial operand. */
2978 if (TREE_CODE (tmp) == RETURN_EXPR)
2980 tree op = TREE_OPERAND (tmp, 0);
2981 if (!is_gimple_val (op))
2983 if (TREE_CODE (op) != MODIFY_EXPR)
2984 abort ();
2985 bsi_insert_before (bsi, op, BSI_NEW_STMT);
2986 TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0);
2988 bsi_prev (bsi);
2989 return true;
2993 /* Otherwise, create a new basic block, and split this edge. */
2994 dest = split_edge (e);
2995 e = dest->pred;
2996 goto restart;
3000 /* This routine will commit all pending edge insertions, creating any new
3001 basic blocks which are necessary.
3003 If specified, NEW_BLOCKS returns a count of the number of new basic
3004 blocks which were created. */
3006 void
3007 bsi_commit_edge_inserts (int *new_blocks)
3009 basic_block bb;
3010 edge e;
3011 int blocks;
3013 blocks = n_basic_blocks;
3015 bsi_commit_edge_inserts_1 (ENTRY_BLOCK_PTR->succ);
3017 FOR_EACH_BB (bb)
3018 for (e = bb->succ; e; e = e->succ_next)
3019 bsi_commit_edge_inserts_1 (e);
3021 if (new_blocks)
3022 *new_blocks = n_basic_blocks - blocks;
3026 /* Commit insertions pending at edge E. */
3028 static void
3029 bsi_commit_edge_inserts_1 (edge e)
3031 if (PENDING_STMT (e))
3033 block_stmt_iterator bsi;
3034 tree stmt = PENDING_STMT (e);
3036 PENDING_STMT (e) = NULL_TREE;
3038 if (tree_find_edge_insert_loc (e, &bsi))
3039 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3040 else
3041 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3046 /* Add STMT to the pending list of edge E. No actual insertion is
3047 made until a call to bsi_commit_edge_inserts () is made. */
3049 void
3050 bsi_insert_on_edge (edge e, tree stmt)
3052 append_to_statement_list (stmt, &PENDING_STMT (e));
3056 /*---------------------------------------------------------------------------
3057 Tree specific functions for CFG manipulation
3058 ---------------------------------------------------------------------------*/
3060 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3061 Abort on abnormal edges. */
3063 static basic_block
3064 tree_split_edge (edge edge_in)
3066 basic_block new_bb, after_bb, dest, src;
3067 edge new_edge, e;
3068 tree phi;
3069 int i, num_elem;
3071 /* Abnormal edges cannot be split. */
3072 if (edge_in->flags & EDGE_ABNORMAL)
3073 abort ();
3075 src = edge_in->src;
3076 dest = edge_in->dest;
3078 /* Place the new block in the block list. Try to keep the new block
3079 near its "logical" location. This is of most help to humans looking
3080 at debugging dumps. */
3081 for (e = dest->pred; e; e = e->pred_next)
3082 if (e->src->next_bb == dest)
3083 break;
3084 if (!e)
3085 after_bb = dest->prev_bb;
3086 else
3087 after_bb = edge_in->src;
3089 new_bb = create_empty_bb (after_bb);
3090 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
3092 /* Find all the PHI arguments on the original edge, and change them to
3093 the new edge. Do it before redirection, so that the argument does not
3094 get removed. */
3095 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
3097 num_elem = PHI_NUM_ARGS (phi);
3098 for (i = 0; i < num_elem; i++)
3099 if (PHI_ARG_EDGE (phi, i) == edge_in)
3101 PHI_ARG_EDGE (phi, i) = new_edge;
3102 break;
3106 if (!redirect_edge_and_branch (edge_in, new_bb))
3107 abort ();
3109 if (PENDING_STMT (edge_in))
3110 abort ();
3112 return new_bb;
3116 /* Return true when BB has label LABEL in it. */
3118 static bool
3119 has_label_p (basic_block bb, tree label)
3121 block_stmt_iterator bsi;
3123 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3125 tree stmt = bsi_stmt (bsi);
3127 if (TREE_CODE (stmt) != LABEL_EXPR)
3128 return false;
3129 if (LABEL_EXPR_LABEL (stmt) == label)
3130 return true;
3132 return false;
3136 /* Callback for walk_tree, check that all elements with address taken are
3137 properly noticed as such. */
3139 static tree
3140 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3142 tree t = *tp, x;
3144 if (TYPE_P (t))
3145 *walk_subtrees = 0;
3147 /* Check operand N for being valid GIMPLE and give error MSG if not.
3148 We check for constants explicitly since they are not considered
3149 gimple invariants if they overflowed. */
3150 #define CHECK_OP(N, MSG) \
3151 do { if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, N))) != 'c' \
3152 && !is_gimple_val (TREE_OPERAND (t, N))) \
3153 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3155 switch (TREE_CODE (t))
3157 case SSA_NAME:
3158 if (SSA_NAME_IN_FREE_LIST (t))
3160 error ("SSA name in freelist but still referenced");
3161 return *tp;
3163 break;
3165 case MODIFY_EXPR:
3166 x = TREE_OPERAND (t, 0);
3167 if (TREE_CODE (x) == BIT_FIELD_REF
3168 && is_gimple_reg (TREE_OPERAND (x, 0)))
3170 error ("GIMPLE register modified with BIT_FIELD_REF");
3171 return t;
3173 break;
3175 case ADDR_EXPR:
3176 /* Skip any references (they will be checked when we recurse down the
3177 tree) and ensure that any variable used as a prefix is marked
3178 addressable. */
3179 for (x = TREE_OPERAND (t, 0);
3180 (handled_component_p (x)
3181 || TREE_CODE (x) == REALPART_EXPR
3182 || TREE_CODE (x) == IMAGPART_EXPR);
3183 x = TREE_OPERAND (x, 0))
3186 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3187 return NULL;
3188 if (!TREE_ADDRESSABLE (x))
3190 error ("address taken, but ADDRESSABLE bit not set");
3191 return x;
3193 break;
3195 case COND_EXPR:
3196 x = TREE_OPERAND (t, 0);
3197 if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3199 error ("non-boolean used in condition");
3200 return x;
3202 break;
3204 case NOP_EXPR:
3205 case CONVERT_EXPR:
3206 case FIX_TRUNC_EXPR:
3207 case FIX_CEIL_EXPR:
3208 case FIX_FLOOR_EXPR:
3209 case FIX_ROUND_EXPR:
3210 case FLOAT_EXPR:
3211 case NEGATE_EXPR:
3212 case ABS_EXPR:
3213 case BIT_NOT_EXPR:
3214 case NON_LVALUE_EXPR:
3215 case TRUTH_NOT_EXPR:
3216 CHECK_OP (0, "Invalid operand to unary operator");
3217 break;
3219 case REALPART_EXPR:
3220 case IMAGPART_EXPR:
3221 case COMPONENT_REF:
3222 case ARRAY_REF:
3223 case ARRAY_RANGE_REF:
3224 case BIT_FIELD_REF:
3225 case VIEW_CONVERT_EXPR:
3226 /* We have a nest of references. Verify that each of the operands
3227 that determine where to reference is either a constant or a variable,
3228 verify that the base is valid, and then show we've already checked
3229 the subtrees. */
3230 while (TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR
3231 || handled_component_p (t))
3233 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3234 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3235 else if (TREE_CODE (t) == ARRAY_REF
3236 || TREE_CODE (t) == ARRAY_RANGE_REF)
3238 CHECK_OP (1, "Invalid array index.");
3239 if (TREE_OPERAND (t, 2))
3240 CHECK_OP (2, "Invalid array lower bound.");
3241 if (TREE_OPERAND (t, 3))
3242 CHECK_OP (3, "Invalid array stride.");
3244 else if (TREE_CODE (t) == BIT_FIELD_REF)
3246 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3247 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3250 t = TREE_OPERAND (t, 0);
3253 if (TREE_CODE_CLASS (TREE_CODE (t)) != 'c'
3254 && !is_gimple_lvalue (t))
3256 error ("Invalid reference prefix.");
3257 return t;
3259 *walk_subtrees = 0;
3260 break;
3262 case LT_EXPR:
3263 case LE_EXPR:
3264 case GT_EXPR:
3265 case GE_EXPR:
3266 case EQ_EXPR:
3267 case NE_EXPR:
3268 case UNORDERED_EXPR:
3269 case ORDERED_EXPR:
3270 case UNLT_EXPR:
3271 case UNLE_EXPR:
3272 case UNGT_EXPR:
3273 case UNGE_EXPR:
3274 case UNEQ_EXPR:
3275 case LTGT_EXPR:
3276 case PLUS_EXPR:
3277 case MINUS_EXPR:
3278 case MULT_EXPR:
3279 case TRUNC_DIV_EXPR:
3280 case CEIL_DIV_EXPR:
3281 case FLOOR_DIV_EXPR:
3282 case ROUND_DIV_EXPR:
3283 case TRUNC_MOD_EXPR:
3284 case CEIL_MOD_EXPR:
3285 case FLOOR_MOD_EXPR:
3286 case ROUND_MOD_EXPR:
3287 case RDIV_EXPR:
3288 case EXACT_DIV_EXPR:
3289 case MIN_EXPR:
3290 case MAX_EXPR:
3291 case LSHIFT_EXPR:
3292 case RSHIFT_EXPR:
3293 case LROTATE_EXPR:
3294 case RROTATE_EXPR:
3295 case BIT_IOR_EXPR:
3296 case BIT_XOR_EXPR:
3297 case BIT_AND_EXPR:
3298 CHECK_OP (0, "Invalid operand to binary operator");
3299 CHECK_OP (1, "Invalid operand to binary operator");
3300 break;
3302 default:
3303 break;
3305 return NULL;
3307 #undef CHECK_OP
3311 /* Verify STMT, return true if STMT is not in GIMPLE form.
3312 TODO: Implement type checking. */
3314 static bool
3315 verify_stmt (tree stmt, bool last_in_block)
3317 tree addr;
3319 if (!is_gimple_stmt (stmt))
3321 error ("Is not a valid GIMPLE statement.");
3322 goto fail;
3325 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
3326 if (addr)
3328 debug_generic_stmt (addr);
3329 return true;
3332 /* If the statement is marked as part of an EH region, then it is
3333 expected that the statement could throw. Verify that when we
3334 have optimizations that simplify statements such that we prove
3335 that they cannot throw, that we update other data structures
3336 to match. */
3337 if (lookup_stmt_eh_region (stmt) >= 0)
3339 if (!tree_could_throw_p (stmt))
3341 error ("Statement marked for throw, but doesn't.");
3342 goto fail;
3344 if (!last_in_block && tree_can_throw_internal (stmt))
3346 error ("Statement marked for throw in middle of block.");
3347 goto fail;
3351 return false;
3353 fail:
3354 debug_generic_stmt (stmt);
3355 return true;
3359 /* Return true when the T can be shared. */
3361 static bool
3362 tree_node_can_be_shared (tree t)
3364 if (TYPE_P (t) || DECL_P (t)
3365 /* We check for constants explicitly since they are not considered
3366 gimple invariants if they overflowed. */
3367 || TREE_CODE_CLASS (TREE_CODE (t)) == 'c'
3368 || is_gimple_min_invariant (t)
3369 || TREE_CODE (t) == SSA_NAME)
3370 return true;
3372 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3373 /* We check for constants explicitly since they are not considered
3374 gimple invariants if they overflowed. */
3375 && (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 1))) == 'c'
3376 || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
3377 || (TREE_CODE (t) == COMPONENT_REF
3378 || TREE_CODE (t) == REALPART_EXPR
3379 || TREE_CODE (t) == IMAGPART_EXPR))
3380 t = TREE_OPERAND (t, 0);
3382 if (DECL_P (t))
3383 return true;
3385 return false;
3389 /* Called via walk_trees. Verify tree sharing. */
3391 static tree
3392 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
3394 htab_t htab = (htab_t) data;
3395 void **slot;
3397 if (tree_node_can_be_shared (*tp))
3399 *walk_subtrees = false;
3400 return NULL;
3403 slot = htab_find_slot (htab, *tp, INSERT);
3404 if (*slot)
3405 return *slot;
3406 *slot = *tp;
3408 return NULL;
3412 /* Verify the GIMPLE statement chain. */
3414 void
3415 verify_stmts (void)
3417 basic_block bb;
3418 block_stmt_iterator bsi;
3419 bool err = false;
3420 htab_t htab;
3421 tree addr;
3423 timevar_push (TV_TREE_STMT_VERIFY);
3424 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3426 FOR_EACH_BB (bb)
3428 tree phi;
3429 int i;
3431 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3433 int phi_num_args = PHI_NUM_ARGS (phi);
3435 for (i = 0; i < phi_num_args; i++)
3437 tree t = PHI_ARG_DEF (phi, i);
3438 tree addr;
3440 /* Addressable variables do have SSA_NAMEs but they
3441 are not considered gimple values. */
3442 if (TREE_CODE (t) != SSA_NAME
3443 && TREE_CODE (t) != FUNCTION_DECL
3444 && !is_gimple_val (t))
3446 error ("PHI def is not a GIMPLE value");
3447 debug_generic_stmt (phi);
3448 debug_generic_stmt (t);
3449 err |= true;
3452 addr = walk_tree (&t, verify_expr, NULL, NULL);
3453 if (addr)
3455 debug_generic_stmt (addr);
3456 err |= true;
3459 addr = walk_tree (&t, verify_node_sharing, htab, NULL);
3460 if (addr)
3462 error ("Incorrect sharing of tree nodes");
3463 debug_generic_stmt (phi);
3464 debug_generic_stmt (addr);
3465 err |= true;
3470 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
3472 tree stmt = bsi_stmt (bsi);
3473 bsi_next (&bsi);
3474 err |= verify_stmt (stmt, bsi_end_p (bsi));
3475 addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
3476 if (addr)
3478 error ("Incorrect sharing of tree nodes");
3479 debug_generic_stmt (stmt);
3480 debug_generic_stmt (addr);
3481 err |= true;
3486 if (err)
3487 internal_error ("verify_stmts failed.");
3489 htab_delete (htab);
3490 timevar_pop (TV_TREE_STMT_VERIFY);
3494 /* Verifies that the flow information is OK. */
3496 static int
3497 tree_verify_flow_info (void)
3499 int err = 0;
3500 basic_block bb;
3501 block_stmt_iterator bsi;
3502 tree stmt;
3503 edge e;
3505 if (ENTRY_BLOCK_PTR->stmt_list)
3507 error ("ENTRY_BLOCK has a statement list associated with it\n");
3508 err = 1;
3511 if (EXIT_BLOCK_PTR->stmt_list)
3513 error ("EXIT_BLOCK has a statement list associated with it\n");
3514 err = 1;
3517 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
3518 if (e->flags & EDGE_FALLTHRU)
3520 error ("Fallthru to exit from bb %d\n", e->src->index);
3521 err = 1;
3524 FOR_EACH_BB (bb)
3526 bool found_ctrl_stmt = false;
3528 /* Skip labels on the start of basic block. */
3529 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3531 if (TREE_CODE (bsi_stmt (bsi)) != LABEL_EXPR)
3532 break;
3534 if (label_to_block (LABEL_EXPR_LABEL (bsi_stmt (bsi))) != bb)
3536 error ("Label %s to block does not match in bb %d\n",
3537 IDENTIFIER_POINTER (DECL_NAME (bsi_stmt (bsi))),
3538 bb->index);
3539 err = 1;
3542 if (decl_function_context (LABEL_EXPR_LABEL (bsi_stmt (bsi)))
3543 != current_function_decl)
3545 error ("Label %s has incorrect context in bb %d\n",
3546 IDENTIFIER_POINTER (DECL_NAME (bsi_stmt (bsi))),
3547 bb->index);
3548 err = 1;
3552 /* Verify that body of basic block BB is free of control flow. */
3553 for (; !bsi_end_p (bsi); bsi_next (&bsi))
3555 tree stmt = bsi_stmt (bsi);
3557 if (found_ctrl_stmt)
3559 error ("Control flow in the middle of basic block %d\n",
3560 bb->index);
3561 err = 1;
3564 if (stmt_ends_bb_p (stmt))
3565 found_ctrl_stmt = true;
3567 if (TREE_CODE (stmt) == LABEL_EXPR)
3569 error ("Label %s in the middle of basic block %d\n",
3570 IDENTIFIER_POINTER (DECL_NAME (stmt)),
3571 bb->index);
3572 err = 1;
3575 bsi = bsi_last (bb);
3576 if (bsi_end_p (bsi))
3577 continue;
3579 stmt = bsi_stmt (bsi);
3581 if (is_ctrl_stmt (stmt))
3583 for (e = bb->succ; e; e = e->succ_next)
3584 if (e->flags & EDGE_FALLTHRU)
3586 error ("Fallthru edge after a control statement in bb %d \n",
3587 bb->index);
3588 err = 1;
3592 switch (TREE_CODE (stmt))
3594 case COND_EXPR:
3596 edge true_edge;
3597 edge false_edge;
3598 if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR
3599 || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR)
3601 error ("Structured COND_EXPR at the end of bb %d\n", bb->index);
3602 err = 1;
3605 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
3607 if (!true_edge || !false_edge
3608 || !(true_edge->flags & EDGE_TRUE_VALUE)
3609 || !(false_edge->flags & EDGE_FALSE_VALUE)
3610 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3611 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3612 || bb->succ->succ_next->succ_next)
3614 error ("Wrong outgoing edge flags at end of bb %d\n",
3615 bb->index);
3616 err = 1;
3619 if (!has_label_p (true_edge->dest,
3620 GOTO_DESTINATION (COND_EXPR_THEN (stmt))))
3622 error ("`then' label does not match edge at end of bb %d\n",
3623 bb->index);
3624 err = 1;
3627 if (!has_label_p (false_edge->dest,
3628 GOTO_DESTINATION (COND_EXPR_ELSE (stmt))))
3630 error ("`else' label does not match edge at end of bb %d\n",
3631 bb->index);
3632 err = 1;
3635 break;
3637 case GOTO_EXPR:
3638 if (simple_goto_p (stmt))
3640 error ("Explicit goto at end of bb %d\n", bb->index);
3641 err = 1;
3643 else
3645 /* FIXME. We should double check that the labels in the
3646 destination blocks have their address taken. */
3647 for (e = bb->succ; e; e = e->succ_next)
3648 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
3649 | EDGE_FALSE_VALUE))
3650 || !(e->flags & EDGE_ABNORMAL))
3652 error ("Wrong outgoing edge flags at end of bb %d\n",
3653 bb->index);
3654 err = 1;
3657 break;
3659 case RETURN_EXPR:
3660 if (!bb->succ || bb->succ->succ_next
3661 || (bb->succ->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3662 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3664 error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
3665 err = 1;
3667 if (bb->succ->dest != EXIT_BLOCK_PTR)
3669 error ("Return edge does not point to exit in bb %d\n",
3670 bb->index);
3671 err = 1;
3673 break;
3675 case SWITCH_EXPR:
3677 tree prev;
3678 edge e;
3679 size_t i, n;
3680 tree vec;
3682 vec = SWITCH_LABELS (stmt);
3683 n = TREE_VEC_LENGTH (vec);
3685 /* Mark all the destination basic blocks. */
3686 for (i = 0; i < n; ++i)
3688 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3689 basic_block label_bb = label_to_block (lab);
3691 if (label_bb->aux && label_bb->aux != (void *)1)
3692 abort ();
3693 label_bb->aux = (void *)1;
3696 /* Verify that the case labels are sorted. */
3697 prev = TREE_VEC_ELT (vec, 0);
3698 for (i = 1; i < n - 1; ++i)
3700 tree c = TREE_VEC_ELT (vec, i);
3701 if (! CASE_LOW (c))
3703 error ("Found default case not at end of case vector");
3704 err = 1;
3705 continue;
3707 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
3709 error ("Case labels not sorted:\n ");
3710 print_generic_expr (stderr, prev, 0);
3711 fprintf (stderr," is greater than ");
3712 print_generic_expr (stderr, c, 0);
3713 fprintf (stderr," but comes before it.\n");
3714 err = 1;
3716 prev = c;
3718 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
3720 error ("No default case found at end of case vector");
3721 err = 1;
3724 for (e = bb->succ; e; e = e->succ_next)
3726 if (!e->dest->aux)
3728 error ("Extra outgoing edge %d->%d\n",
3729 bb->index, e->dest->index);
3730 err = 1;
3732 e->dest->aux = (void *)2;
3733 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3734 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3736 error ("Wrong outgoing edge flags at end of bb %d\n",
3737 bb->index);
3738 err = 1;
3742 /* Check that we have all of them. */
3743 for (i = 0; i < n; ++i)
3745 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3746 basic_block label_bb = label_to_block (lab);
3748 if (label_bb->aux != (void *)2)
3750 error ("Missing edge %i->%i\n",
3751 bb->index, label_bb->index);
3752 err = 1;
3756 for (e = bb->succ; e; e = e->succ_next)
3757 e->dest->aux = (void *)0;
3760 default: ;
3764 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
3765 verify_dominators (CDI_DOMINATORS);
3767 return err;
3771 /* Updates phi nodes after creating forwarder block joined
3772 by edge FALLTHRU. */
3774 static void
3775 tree_make_forwarder_block (edge fallthru)
3777 edge e;
3778 basic_block dummy, bb;
3779 tree phi, new_phi, var, prev, next;
3781 dummy = fallthru->src;
3782 bb = fallthru->dest;
3784 if (!bb->pred->pred_next)
3785 return;
3787 /* If we redirected a branch we must create new phi nodes at the
3788 start of BB. */
3789 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
3791 var = PHI_RESULT (phi);
3792 new_phi = create_phi_node (var, bb);
3793 SSA_NAME_DEF_STMT (var) = new_phi;
3794 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
3795 add_phi_arg (&new_phi, PHI_RESULT (phi), fallthru);
3798 /* Ensure that the PHI node chain is in the same order. */
3799 prev = NULL;
3800 for (phi = phi_nodes (bb); phi; phi = next)
3802 next = PHI_CHAIN (phi);
3803 PHI_CHAIN (phi) = prev;
3804 prev = phi;
3806 set_phi_nodes (bb, prev);
3808 /* Add the arguments we have stored on edges. */
3809 for (e = bb->pred; e; e = e->pred_next)
3811 if (e == fallthru)
3812 continue;
3814 for (phi = phi_nodes (bb), var = PENDING_STMT (e);
3815 phi;
3816 phi = PHI_CHAIN (phi), var = TREE_CHAIN (var))
3817 add_phi_arg (&phi, TREE_VALUE (var), e);
3819 PENDING_STMT (e) = NULL;
3824 /* Return true if basic block BB does nothing except pass control
3825 flow to another block and that we can safely insert a label at
3826 the start of the successor block. */
3828 static bool
3829 tree_forwarder_block_p (basic_block bb)
3831 block_stmt_iterator bsi;
3832 edge e;
3834 /* If we have already determined that this block is not forwardable,
3835 then no further checks are necessary. */
3836 if (! bb_ann (bb)->forwardable)
3837 return false;
3839 /* BB must have a single outgoing normal edge. Otherwise it can not be
3840 a forwarder block. */
3841 if (!bb->succ
3842 || bb->succ->succ_next
3843 || bb->succ->dest == EXIT_BLOCK_PTR
3844 || (bb->succ->flags & EDGE_ABNORMAL)
3845 || bb == ENTRY_BLOCK_PTR)
3847 bb_ann (bb)->forwardable = 0;
3848 return false;
3851 /* Successors of the entry block are not forwarders. */
3852 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
3853 if (e->dest == bb)
3855 bb_ann (bb)->forwardable = 0;
3856 return false;
3859 /* BB can not have any PHI nodes. This could potentially be relaxed
3860 early in compilation if we re-rewrote the variables appearing in
3861 any PHI nodes in forwarder blocks. */
3862 if (phi_nodes (bb))
3864 bb_ann (bb)->forwardable = 0;
3865 return false;
3868 /* Now walk through the statements. We can ignore labels, anything else
3869 means this is not a forwarder block. */
3870 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3872 tree stmt = bsi_stmt (bsi);
3874 switch (TREE_CODE (stmt))
3876 case LABEL_EXPR:
3877 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
3878 return false;
3879 break;
3881 default:
3882 bb_ann (bb)->forwardable = 0;
3883 return false;
3887 return true;
3891 /* Thread jumps over empty statements.
3893 This code should _not_ thread over obviously equivalent conditions
3894 as that requires nontrivial updates to the SSA graph. */
3896 static bool
3897 thread_jumps (void)
3899 edge e, next, last, old;
3900 basic_block bb, dest, tmp, old_dest, dom;
3901 tree phi;
3902 int arg;
3903 bool retval = false;
3905 FOR_EACH_BB (bb)
3906 bb_ann (bb)->forwardable = 1;
3908 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3910 /* Don't waste time on unreachable blocks. */
3911 if (!bb->pred)
3912 continue;
3914 /* Nor on forwarders. */
3915 if (tree_forwarder_block_p (bb))
3916 continue;
3918 /* This block is now part of a forwarding path, mark it as not
3919 forwardable so that we can detect loops. This bit will be
3920 reset below. */
3921 bb_ann (bb)->forwardable = 0;
3923 /* Examine each of our block's successors to see if it is
3924 forwardable. */
3925 for (e = bb->succ; e; e = next)
3927 next = e->succ_next;
3929 /* If the edge is abnormal or its destination is not
3930 forwardable, then there's nothing to do. */
3931 if ((e->flags & EDGE_ABNORMAL)
3932 || !tree_forwarder_block_p (e->dest))
3933 continue;
3935 /* Now walk through as many forwarder block as possible to
3936 find the ultimate destination we want to thread our jump
3937 to. */
3938 last = e->dest->succ;
3939 bb_ann (e->dest)->forwardable = 0;
3940 for (dest = e->dest->succ->dest;
3941 tree_forwarder_block_p (dest);
3942 last = dest->succ,
3943 dest = dest->succ->dest)
3945 /* An infinite loop detected. We redirect the edge anyway, so
3946 that the loop is shrunk into single basic block. */
3947 if (!bb_ann (dest)->forwardable)
3948 break;
3950 if (dest->succ->dest == EXIT_BLOCK_PTR)
3951 break;
3953 bb_ann (dest)->forwardable = 0;
3956 /* Reset the forwardable marks to 1. */
3957 for (tmp = e->dest;
3958 tmp != dest;
3959 tmp = tmp->succ->dest)
3960 bb_ann (tmp)->forwardable = 1;
3962 if (dest == e->dest)
3963 continue;
3965 old = find_edge (bb, dest);
3966 if (old)
3968 /* If there already is an edge, check whether the values
3969 in phi nodes differ. */
3970 if (!phi_alternatives_equal (dest, last, old))
3972 /* The previous block is forwarder. Redirect our jump
3973 to that target instead since we know it has no PHI
3974 nodes that will need updating. */
3975 dest = last->src;
3977 /* That might mean that no forwarding at all is possible. */
3978 if (dest == e->dest)
3979 continue;
3981 old = find_edge (bb, dest);
3985 /* Perform the redirection. */
3986 retval = true;
3987 old_dest = e->dest;
3988 e = redirect_edge_and_branch (e, dest);
3990 if (!old)
3992 /* Update PHI nodes. We know that the new argument should
3993 have the same value as the argument associated with LAST.
3994 Otherwise we would have changed our target block above. */
3995 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
3997 arg = phi_arg_from_edge (phi, last);
3998 if (arg < 0)
3999 abort ();
4000 add_phi_arg (&phi, PHI_ARG_DEF (phi, arg), e);
4004 /* Update the dominators. */
4005 if (dom_computed[CDI_DOMINATORS] >= DOM_CONS_OK)
4007 /* Remove the unreachable blocks (observe that if all blocks
4008 were reachable before, only those in the path we threaded
4009 over and did not have any predecessor outside of the path
4010 become unreachable). */
4011 for (; old_dest != dest; old_dest = tmp)
4013 tmp = old_dest->succ->dest;
4015 if (old_dest->pred)
4016 break;
4018 delete_basic_block (old_dest);
4020 /* If the dominator of the destination was in the path, set its
4021 dominator to the start of the redirected edge. */
4022 if (get_immediate_dominator (CDI_DOMINATORS, old_dest) == NULL)
4023 set_immediate_dominator (CDI_DOMINATORS, old_dest, bb);
4025 /* Now proceed like if we forwarded just over one edge at a time.
4026 Algorithm for forwarding over edge A --> B then is
4028 if (idom (B) == A)
4029 idom (B) = idom (A);
4030 recount_idom (A); */
4032 for (; old_dest != dest; old_dest = tmp)
4034 tmp = old_dest->succ->dest;
4036 if (get_immediate_dominator (CDI_DOMINATORS, tmp) == old_dest)
4038 dom = get_immediate_dominator (CDI_DOMINATORS, old_dest);
4039 set_immediate_dominator (CDI_DOMINATORS, tmp, dom);
4042 dom = recount_dominator (CDI_DOMINATORS, old_dest);
4043 set_immediate_dominator (CDI_DOMINATORS, old_dest, dom);
4048 /* Reset the forwardable bit on our block since it's no longer in
4049 a forwarding chain path. */
4050 bb_ann (bb)->forwardable = 1;
4053 return retval;
4057 /* Return a non-special label in the head of basic block BLOCK.
4058 Create one if it doesn't exist. */
4060 tree
4061 tree_block_label (basic_block bb)
4063 block_stmt_iterator i, s = bsi_start (bb);
4064 bool first = true;
4065 tree label, stmt;
4067 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4069 stmt = bsi_stmt (i);
4070 if (TREE_CODE (stmt) != LABEL_EXPR)
4071 break;
4072 label = LABEL_EXPR_LABEL (stmt);
4073 if (!DECL_NONLOCAL (label))
4075 if (!first)
4076 bsi_move_before (&i, &s);
4077 return label;
4081 label = create_artificial_label ();
4082 stmt = build1 (LABEL_EXPR, void_type_node, label);
4083 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4084 return label;
4088 /* Attempt to perform edge redirection by replacing a possibly complex
4089 jump instruction by a goto or by removing the jump completely.
4090 This can apply only if all edges now point to the same block. The
4091 parameters and return values are equivalent to
4092 redirect_edge_and_branch. */
4094 static edge
4095 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4097 basic_block src = e->src;
4098 edge tmp;
4099 block_stmt_iterator b;
4100 tree stmt;
4102 /* Verify that all targets will be TARGET. */
4103 for (tmp = src->succ; tmp; tmp = tmp->succ_next)
4104 if (tmp->dest != target && tmp != e)
4105 break;
4107 if (tmp)
4108 return NULL;
4110 b = bsi_last (src);
4111 if (bsi_end_p (b))
4112 return NULL;
4113 stmt = bsi_stmt (b);
4115 if (TREE_CODE (stmt) == COND_EXPR
4116 || TREE_CODE (stmt) == SWITCH_EXPR)
4118 bsi_remove (&b);
4119 e = ssa_redirect_edge (e, target);
4120 e->flags = EDGE_FALLTHRU;
4121 return e;
4124 return NULL;
4128 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4129 edge representing the redirected branch. */
4131 static edge
4132 tree_redirect_edge_and_branch (edge e, basic_block dest)
4134 basic_block bb = e->src;
4135 block_stmt_iterator bsi;
4136 edge ret;
4137 tree label, stmt;
4139 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4140 return NULL;
4142 if (e->src != ENTRY_BLOCK_PTR
4143 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4144 return ret;
4146 if (e->dest == dest)
4147 return NULL;
4149 label = tree_block_label (dest);
4151 bsi = bsi_last (bb);
4152 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4154 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4156 case COND_EXPR:
4157 stmt = (e->flags & EDGE_TRUE_VALUE
4158 ? COND_EXPR_THEN (stmt)
4159 : COND_EXPR_ELSE (stmt));
4160 GOTO_DESTINATION (stmt) = label;
4161 break;
4163 case GOTO_EXPR:
4164 /* No non-abnormal edges should lead from a non-simple goto, and
4165 simple ones should be represented implicitly. */
4166 abort ();
4168 case SWITCH_EXPR:
4170 tree vec = SWITCH_LABELS (stmt);
4171 size_t i, n = TREE_VEC_LENGTH (vec);
4173 for (i = 0; i < n; ++i)
4175 tree elt = TREE_VEC_ELT (vec, i);
4176 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4177 CASE_LABEL (elt) = label;
4180 break;
4182 case RETURN_EXPR:
4183 bsi_remove (&bsi);
4184 e->flags |= EDGE_FALLTHRU;
4185 break;
4187 default:
4188 /* Otherwise it must be a fallthru edge, and we don't need to
4189 do anything besides redirecting it. */
4190 if (!(e->flags & EDGE_FALLTHRU))
4191 abort ();
4192 break;
4195 /* Update/insert PHI nodes as necessary. */
4197 /* Now update the edges in the CFG. */
4198 e = ssa_redirect_edge (e, dest);
4200 return e;
4204 /* Simple wrapper, as we can always redirect fallthru edges. */
4206 static basic_block
4207 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4209 e = tree_redirect_edge_and_branch (e, dest);
4210 if (!e)
4211 abort ();
4213 return NULL;
4217 /* Splits basic block BB after statement STMT (but at least after the
4218 labels). If STMT is NULL, BB is split just after the labels. */
4220 static basic_block
4221 tree_split_block (basic_block bb, void *stmt)
4223 block_stmt_iterator bsi, bsi_tgt;
4224 tree act;
4225 basic_block new_bb;
4226 edge e;
4228 new_bb = create_empty_bb (bb);
4230 /* Redirect the outgoing edges. */
4231 new_bb->succ = bb->succ;
4232 bb->succ = NULL;
4233 for (e = new_bb->succ; e; e = e->succ_next)
4234 e->src = new_bb;
4236 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4237 stmt = NULL;
4239 /* Move everything from BSI to the new basic block. */
4240 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4242 act = bsi_stmt (bsi);
4243 if (TREE_CODE (act) == LABEL_EXPR)
4244 continue;
4246 if (!stmt)
4247 break;
4249 if (stmt == act)
4251 bsi_next (&bsi);
4252 break;
4256 bsi_tgt = bsi_start (new_bb);
4257 while (!bsi_end_p (bsi))
4259 act = bsi_stmt (bsi);
4260 bsi_remove (&bsi);
4261 bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT);
4264 return new_bb;
4268 /* Moves basic block BB after block AFTER. */
4270 static bool
4271 tree_move_block_after (basic_block bb, basic_block after)
4273 if (bb->prev_bb == after)
4274 return true;
4276 unlink_block (bb);
4277 link_block (bb, after);
4279 return true;
4283 /* Return true if basic_block can be duplicated. */
4285 static bool
4286 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED)
4288 return true;
4292 /* Create a duplicate of the basic block BB. NOTE: This does not
4293 preserve SSA form. */
4295 static basic_block
4296 tree_duplicate_bb (basic_block bb)
4298 basic_block new_bb;
4299 block_stmt_iterator bsi, bsi_tgt;
4301 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4302 bsi_tgt = bsi_start (new_bb);
4303 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4305 tree stmt = bsi_stmt (bsi);
4306 tree copy;
4308 if (TREE_CODE (stmt) == LABEL_EXPR)
4309 continue;
4311 copy = unshare_expr (stmt);
4313 /* Copy also the virtual operands. */
4314 get_stmt_ann (copy);
4315 copy_virtual_operands (copy, stmt);
4317 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
4320 return new_bb;
4324 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
4326 void
4327 dump_function_to_file (tree fn, FILE *file, int flags)
4329 tree arg, vars, var;
4330 bool ignore_topmost_bind = false, any_var = false;
4331 basic_block bb;
4332 tree chain;
4334 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
4336 arg = DECL_ARGUMENTS (fn);
4337 while (arg)
4339 print_generic_expr (file, arg, dump_flags);
4340 if (TREE_CHAIN (arg))
4341 fprintf (file, ", ");
4342 arg = TREE_CHAIN (arg);
4344 fprintf (file, ")\n");
4346 if (flags & TDF_RAW)
4348 dump_node (fn, TDF_SLIM | flags, file);
4349 return;
4352 /* When GIMPLE is lowered, the variables are no longer available in
4353 BIND_EXPRs, so display them separately. */
4354 if (cfun && cfun->unexpanded_var_list)
4356 ignore_topmost_bind = true;
4358 fprintf (file, "{\n");
4359 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
4361 var = TREE_VALUE (vars);
4363 print_generic_decl (file, var, flags);
4364 fprintf (file, "\n");
4366 any_var = true;
4370 if (basic_block_info)
4372 /* Make a CFG based dump. */
4373 if (!ignore_topmost_bind)
4374 fprintf (file, "{\n");
4376 if (any_var && n_basic_blocks)
4377 fprintf (file, "\n");
4379 FOR_EACH_BB (bb)
4380 dump_generic_bb (file, bb, 2, flags);
4382 fprintf (file, "}\n");
4384 else
4386 int indent;
4388 /* Make a tree based dump. */
4389 chain = DECL_SAVED_TREE (fn);
4391 if (TREE_CODE (chain) == BIND_EXPR)
4393 if (ignore_topmost_bind)
4395 chain = BIND_EXPR_BODY (chain);
4396 indent = 2;
4398 else
4399 indent = 0;
4401 else
4403 if (!ignore_topmost_bind)
4404 fprintf (file, "{\n");
4405 indent = 2;
4408 if (any_var)
4409 fprintf (file, "\n");
4411 print_generic_stmt_indented (file, chain, flags, indent);
4412 if (ignore_topmost_bind)
4413 fprintf (file, "}\n");
4416 fprintf (file, "\n\n");
4420 /* Pretty print of the loops intermediate representation. */
4421 static void print_loop (FILE *, struct loop *, int);
4422 static void print_pred_bbs (FILE *, edge);
4423 static void print_succ_bbs (FILE *, edge);
4426 /* Print the predecessors indexes of edge E on FILE. */
4428 static void
4429 print_pred_bbs (FILE *file, edge e)
4431 if (e == NULL)
4432 return;
4434 else if (e->pred_next == NULL)
4435 fprintf (file, "bb_%d", e->src->index);
4437 else
4439 fprintf (file, "bb_%d, ", e->src->index);
4440 print_pred_bbs (file, e->pred_next);
4445 /* Print the successors indexes of edge E on FILE. */
4447 static void
4448 print_succ_bbs (FILE *file, edge e)
4450 if (e == NULL)
4451 return;
4452 else if (e->succ_next == NULL)
4453 fprintf (file, "bb_%d", e->dest->index);
4454 else
4456 fprintf (file, "bb_%d, ", e->dest->index);
4457 print_succ_bbs (file, e->succ_next);
4462 /* Pretty print LOOP on FILE, indented INDENT spaces. */
4464 static void
4465 print_loop (FILE *file, struct loop *loop, int indent)
4467 char *s_indent;
4468 basic_block bb;
4470 if (loop == NULL)
4471 return;
4473 s_indent = (char *) alloca ((size_t) indent + 1);
4474 memset ((void *) s_indent, ' ', (size_t) indent);
4475 s_indent[indent] = '\0';
4477 /* Print the loop's header. */
4478 fprintf (file, "%sloop_%d\n", s_indent, loop->num);
4480 /* Print the loop's body. */
4481 fprintf (file, "%s{\n", s_indent);
4482 FOR_EACH_BB (bb)
4483 if (bb->loop_father == loop)
4485 /* Print the basic_block's header. */
4486 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
4487 print_pred_bbs (file, bb->pred);
4488 fprintf (file, "}, succs = {");
4489 print_succ_bbs (file, bb->succ);
4490 fprintf (file, "})\n");
4492 /* Print the basic_block's body. */
4493 fprintf (file, "%s {\n", s_indent);
4494 tree_dump_bb (bb, file, indent + 4);
4495 fprintf (file, "%s }\n", s_indent);
4498 print_loop (file, loop->inner, indent + 2);
4499 fprintf (file, "%s}\n", s_indent);
4500 print_loop (file, loop->next, indent);
4504 /* Follow a CFG edge from the entry point of the program, and on entry
4505 of a loop, pretty print the loop structure on FILE. */
4507 void
4508 print_loop_ir (FILE *file)
4510 basic_block bb;
4512 bb = BASIC_BLOCK (0);
4513 if (bb && bb->loop_father)
4514 print_loop (file, bb->loop_father, 0);
4518 /* Debugging loops structure at tree level. */
4520 void
4521 debug_loop_ir (void)
4523 print_loop_ir (stderr);
4527 /* Return true if BB ends with a call, possibly followed by some
4528 instructions that must stay with the call. Return false,
4529 otherwise. */
4531 static bool
4532 tree_block_ends_with_call_p (basic_block bb)
4534 block_stmt_iterator bsi = bsi_last (bb);
4535 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
4539 /* Return true if BB ends with a conditional branch. Return false,
4540 otherwise. */
4542 static bool
4543 tree_block_ends_with_condjump_p (basic_block bb)
4545 tree stmt = tsi_stmt (bsi_last (bb).tsi);
4546 return (TREE_CODE (stmt) == COND_EXPR);
4550 /* Return true if we need to add fake edge to exit at statement T.
4551 Helper function for tree_flow_call_edges_add. */
4553 static bool
4554 need_fake_edge_p (tree t)
4556 tree call;
4558 /* NORETURN and LONGJMP calls already have an edge to exit.
4559 CONST, PURE and ALWAYS_RETURN calls do not need one.
4560 We don't currently check for CONST and PURE here, although
4561 it would be a good idea, because those attributes are
4562 figured out from the RTL in mark_constant_function, and
4563 the counter incrementation code from -fprofile-arcs
4564 leads to different results from -fbranch-probabilities. */
4565 call = get_call_expr_in (t);
4566 if (call
4567 && !(call_expr_flags (call) &
4568 (ECF_NORETURN | ECF_LONGJMP | ECF_ALWAYS_RETURN)))
4569 return true;
4571 if (TREE_CODE (t) == ASM_EXPR
4572 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
4573 return true;
4575 return false;
4579 /* Add fake edges to the function exit for any non constant and non
4580 noreturn calls, volatile inline assembly in the bitmap of blocks
4581 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
4582 the number of blocks that were split.
4584 The goal is to expose cases in which entering a basic block does
4585 not imply that all subsequent instructions must be executed. */
4587 static int
4588 tree_flow_call_edges_add (sbitmap blocks)
4590 int i;
4591 int blocks_split = 0;
4592 int last_bb = last_basic_block;
4593 bool check_last_block = false;
4595 if (n_basic_blocks == 0)
4596 return 0;
4598 if (! blocks)
4599 check_last_block = true;
4600 else
4601 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
4603 /* In the last basic block, before epilogue generation, there will be
4604 a fallthru edge to EXIT. Special care is required if the last insn
4605 of the last basic block is a call because make_edge folds duplicate
4606 edges, which would result in the fallthru edge also being marked
4607 fake, which would result in the fallthru edge being removed by
4608 remove_fake_edges, which would result in an invalid CFG.
4610 Moreover, we can't elide the outgoing fake edge, since the block
4611 profiler needs to take this into account in order to solve the minimal
4612 spanning tree in the case that the call doesn't return.
4614 Handle this by adding a dummy instruction in a new last basic block. */
4615 if (check_last_block)
4617 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
4618 block_stmt_iterator bsi = bsi_last (bb);
4619 tree t = NULL_TREE;
4620 if (!bsi_end_p (bsi))
4621 t = bsi_stmt (bsi);
4623 if (need_fake_edge_p (t))
4625 edge e;
4627 for (e = bb->succ; e; e = e->succ_next)
4628 if (e->dest == EXIT_BLOCK_PTR)
4630 bsi_insert_on_edge (e, build_empty_stmt ());
4631 bsi_commit_edge_inserts ((int *)NULL);
4632 break;
4637 /* Now add fake edges to the function exit for any non constant
4638 calls since there is no way that we can determine if they will
4639 return or not... */
4640 for (i = 0; i < last_bb; i++)
4642 basic_block bb = BASIC_BLOCK (i);
4643 block_stmt_iterator bsi;
4644 tree stmt, last_stmt;
4646 if (!bb)
4647 continue;
4649 if (blocks && !TEST_BIT (blocks, i))
4650 continue;
4652 bsi = bsi_last (bb);
4653 if (!bsi_end_p (bsi))
4655 last_stmt = bsi_stmt (bsi);
4658 stmt = bsi_stmt (bsi);
4659 if (need_fake_edge_p (stmt))
4661 edge e;
4662 /* The handling above of the final block before the
4663 epilogue should be enough to verify that there is
4664 no edge to the exit block in CFG already.
4665 Calling make_edge in such case would cause us to
4666 mark that edge as fake and remove it later. */
4667 #ifdef ENABLE_CHECKING
4668 if (stmt == last_stmt)
4669 for (e = bb->succ; e; e = e->succ_next)
4670 if (e->dest == EXIT_BLOCK_PTR)
4671 abort ();
4672 #endif
4674 /* Note that the following may create a new basic block
4675 and renumber the existing basic blocks. */
4676 if (stmt != last_stmt)
4678 e = split_block (bb, stmt);
4679 if (e)
4680 blocks_split++;
4682 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
4684 bsi_prev (&bsi);
4686 while (!bsi_end_p (bsi));
4690 if (blocks_split)
4691 verify_flow_info ();
4693 return blocks_split;
4696 bool
4697 tree_purge_dead_eh_edges (basic_block bb)
4699 bool changed = false;
4700 edge e, next;
4701 tree stmt = last_stmt (bb);
4703 if (stmt && tree_can_throw_internal (stmt))
4704 return false;
4706 for (e = bb->succ; e ; e = next)
4708 next = e->succ_next;
4709 if (e->flags & EDGE_EH)
4711 ssa_remove_edge (e);
4712 changed = true;
4716 return changed;
4719 bool
4720 tree_purge_all_dead_eh_edges (bitmap blocks)
4722 bool changed = false;
4723 size_t i;
4725 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
4726 { changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i)); });
4728 return changed;
4731 struct cfg_hooks tree_cfg_hooks = {
4732 "tree",
4733 tree_verify_flow_info,
4734 tree_dump_bb, /* dump_bb */
4735 create_bb, /* create_basic_block */
4736 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
4737 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
4738 remove_bb, /* delete_basic_block */
4739 tree_split_block, /* split_block */
4740 tree_move_block_after, /* move_block_after */
4741 tree_can_merge_blocks_p, /* can_merge_blocks_p */
4742 tree_merge_blocks, /* merge_blocks */
4743 tree_predict_edge, /* predict_edge */
4744 tree_predicted_by_p, /* predicted_by_p */
4745 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
4746 tree_duplicate_bb, /* duplicate_block */
4747 tree_split_edge, /* split_edge */
4748 tree_make_forwarder_block, /* make_forward_block */
4749 NULL, /* tidy_fallthru_edge */
4750 tree_block_ends_with_call_p, /* block_ends_with_call_p */
4751 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
4752 tree_flow_call_edges_add /* flow_call_edges_add */
4756 /* Split all critical edges. */
4758 static void
4759 split_critical_edges (void)
4761 basic_block bb;
4762 edge e;
4764 FOR_ALL_BB (bb)
4766 for (e = bb->succ; e ; e = e->succ_next)
4767 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
4769 split_edge (e);
4774 struct tree_opt_pass pass_split_crit_edges =
4776 "crited", /* name */
4777 NULL, /* gate */
4778 split_critical_edges, /* execute */
4779 NULL, /* sub */
4780 NULL, /* next */
4781 0, /* static_pass_number */
4782 TV_TREE_SPLIT_EDGES, /* tv_id */
4783 PROP_cfg, /* properties required */
4784 PROP_no_crit_edges, /* properties_provided */
4785 0, /* properties_destroyed */
4786 0, /* todo_flags_start */
4787 TODO_dump_func, /* todo_flags_finish */
4791 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
4792 a temporary, make sure and register it to be renamed if necessary,
4793 and finally return the temporary. Put the statements to compute
4794 EXP before the current statement in BSI. */
4796 tree
4797 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
4799 tree t, new_stmt, orig_stmt;
4801 if (is_gimple_val (exp))
4802 return exp;
4804 t = make_rename_temp (type, NULL);
4805 new_stmt = build (MODIFY_EXPR, type, t, exp);
4807 orig_stmt = bsi_stmt (*bsi);
4808 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
4809 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
4811 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
4813 return t;
4816 /* Build a ternary operation and gimplify it. Emit code before BSI.
4817 Return the gimple_val holding the result. */
4819 tree
4820 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
4821 tree type, tree a, tree b, tree c)
4823 tree ret;
4825 ret = fold (build3 (code, type, a, b, c));
4826 STRIP_NOPS (ret);
4828 return gimplify_val (bsi, type, ret);
4831 /* Build a binary operation and gimplify it. Emit code before BSI.
4832 Return the gimple_val holding the result. */
4834 tree
4835 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
4836 tree type, tree a, tree b)
4838 tree ret;
4840 ret = fold (build2 (code, type, a, b));
4841 STRIP_NOPS (ret);
4843 return gimplify_val (bsi, type, ret);
4846 /* Build a unary operation and gimplify it. Emit code before BSI.
4847 Return the gimple_val holding the result. */
4849 tree
4850 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
4851 tree a)
4853 tree ret;
4855 ret = fold (build1 (code, type, a));
4856 STRIP_NOPS (ret);
4858 return gimplify_val (bsi, type, ret);
4863 /* Emit return warnings. */
4865 static void
4866 execute_warn_function_return (void)
4868 #ifdef USE_MAPPED_LOCATION
4869 source_location location;
4870 #else
4871 location_t *locus;
4872 #endif
4873 tree last;
4874 edge e;
4876 if (warn_missing_noreturn
4877 && !TREE_THIS_VOLATILE (cfun->decl)
4878 && EXIT_BLOCK_PTR->pred == NULL
4879 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
4880 warning ("%Jfunction might be possible candidate for attribute `noreturn'",
4881 cfun->decl);
4883 /* If we have a path to EXIT, then we do return. */
4884 if (TREE_THIS_VOLATILE (cfun->decl)
4885 && EXIT_BLOCK_PTR->pred != NULL)
4887 #ifdef USE_MAPPED_LOCATION
4888 location = UNKNOWN_LOCATION;
4889 #else
4890 locus = NULL;
4891 #endif
4892 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
4894 last = last_stmt (e->src);
4895 if (TREE_CODE (last) == RETURN_EXPR
4896 #ifdef USE_MAPPED_LOCATION
4897 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
4898 #else
4899 && (locus = EXPR_LOCUS (last)) != NULL)
4900 #endif
4901 break;
4903 #ifdef USE_MAPPED_LOCATION
4904 if (location == UNKNOWN_LOCATION)
4905 location = cfun->function_end_locus;
4906 warning ("%H`noreturn' function does return", &location);
4907 #else
4908 if (!locus)
4909 locus = &cfun->function_end_locus;
4910 warning ("%H`noreturn' function does return", locus);
4911 #endif
4914 /* If we see "return;" in some basic block, then we do reach the end
4915 without returning a value. */
4916 else if (warn_return_type
4917 && EXIT_BLOCK_PTR->pred != NULL
4918 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
4920 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
4922 tree last = last_stmt (e->src);
4923 if (TREE_CODE (last) == RETURN_EXPR
4924 && TREE_OPERAND (last, 0) == NULL)
4926 #ifdef USE_MAPPED_LOCATION
4927 location = EXPR_LOCATION (last);
4928 if (location == UNKNOWN_LOCATION)
4929 location = cfun->function_end_locus;
4930 warning ("%Hcontrol reaches end of non-void function", &location);
4931 #else
4932 locus = EXPR_LOCUS (last);
4933 if (!locus)
4934 locus = &cfun->function_end_locus;
4935 warning ("%Hcontrol reaches end of non-void function", locus);
4936 #endif
4937 break;
4944 /* Given a basic block B which ends with a conditional and has
4945 precisely two successors, determine which of the edges is taken if
4946 the conditional is true and which is taken if the conditional is
4947 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
4949 void
4950 extract_true_false_edges_from_block (basic_block b,
4951 edge *true_edge,
4952 edge *false_edge)
4954 edge e = b->succ;
4956 if (e->flags & EDGE_TRUE_VALUE)
4958 *true_edge = e;
4959 *false_edge = e->succ_next;
4961 else
4963 *false_edge = e;
4964 *true_edge = e->succ_next;
4968 struct tree_opt_pass pass_warn_function_return =
4970 NULL, /* name */
4971 NULL, /* gate */
4972 execute_warn_function_return, /* execute */
4973 NULL, /* sub */
4974 NULL, /* next */
4975 0, /* static_pass_number */
4976 0, /* tv_id */
4977 PROP_cfg, /* properties_required */
4978 0, /* properties_provided */
4979 0, /* properties_destroyed */
4980 0, /* todo_flags_start */
4981 0 /* todo_flags_finish */
4984 #include "gt-tree-cfg.h"