ffi.c (ffi_prep_cif_machdep): Fix thinko.
[official-gcc.git] / gcc / tree-cfg.c
bloba8cb6276fc97ffa8236693369ccf2295ac2e193c
1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "flags.h"
33 #include "function.h"
34 #include "expr.h"
35 #include "ggc.h"
36 #include "langhooks.h"
37 #include "diagnostic.h"
38 #include "tree-flow.h"
39 #include "timevar.h"
40 #include "tree-dump.h"
41 #include "tree-pass.h"
42 #include "toplev.h"
43 #include "except.h"
44 #include "cfgloop.h"
45 #include "cfglayout.h"
46 #include "tree-ssa-propagate.h"
47 #include "value-prof.h"
48 #include "pointer-set.h"
49 #include "tree-inline.h"
51 /* This file contains functions for building the Control Flow Graph (CFG)
52 for a function tree. */
54 /* Local declarations. */
56 /* Initial capacity for the basic block array. */
57 static const int initial_cfg_capacity = 20;
59 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
60 which use a particular edge. The CASE_LABEL_EXPRs are chained together
61 via their TREE_CHAIN field, which we clear after we're done with the
62 hash table to prevent problems with duplication of SWITCH_EXPRs.
64 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
65 update the case vector in response to edge redirections.
67 Right now this table is set up and torn down at key points in the
68 compilation process. It would be nice if we could make the table
69 more persistent. The key is getting notification of changes to
70 the CFG (particularly edge removal, creation and redirection). */
72 static struct pointer_map_t *edge_to_cases;
74 /* CFG statistics. */
75 struct cfg_stats_d
77 long num_merged_labels;
80 static struct cfg_stats_d cfg_stats;
82 /* Nonzero if we found a computed goto while building basic blocks. */
83 static bool found_computed_goto;
85 /* Basic blocks and flowgraphs. */
86 static basic_block create_bb (void *, void *, basic_block);
87 static void make_blocks (tree);
88 static void factor_computed_gotos (void);
90 /* Edges. */
91 static void make_edges (void);
92 static void make_cond_expr_edges (basic_block);
93 static void make_switch_expr_edges (basic_block);
94 static void make_goto_expr_edges (basic_block);
95 static edge tree_redirect_edge_and_branch (edge, basic_block);
96 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
97 static unsigned int split_critical_edges (void);
99 /* Various helpers. */
100 static inline bool stmt_starts_bb_p (const_tree, const_tree);
101 static int tree_verify_flow_info (void);
102 static void tree_make_forwarder_block (edge);
103 static void tree_cfg2vcg (FILE *);
104 static inline void change_bb_for_stmt (tree t, basic_block bb);
106 /* Flowgraph optimization and cleanup. */
107 static void tree_merge_blocks (basic_block, basic_block);
108 static bool tree_can_merge_blocks_p (basic_block, basic_block);
109 static void remove_bb (basic_block);
110 static edge find_taken_edge_computed_goto (basic_block, tree);
111 static edge find_taken_edge_cond_expr (basic_block, tree);
112 static edge find_taken_edge_switch_expr (basic_block, tree);
113 static tree find_case_label_for_value (tree, tree);
115 void
116 init_empty_tree_cfg (void)
118 /* Initialize the basic block array. */
119 init_flow ();
120 profile_status = PROFILE_ABSENT;
121 n_basic_blocks = NUM_FIXED_BLOCKS;
122 last_basic_block = NUM_FIXED_BLOCKS;
123 basic_block_info = VEC_alloc (basic_block, gc, initial_cfg_capacity);
124 VEC_safe_grow_cleared (basic_block, gc, basic_block_info,
125 initial_cfg_capacity);
127 /* Build a mapping of labels to their associated blocks. */
128 label_to_block_map = VEC_alloc (basic_block, gc, initial_cfg_capacity);
129 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
130 initial_cfg_capacity);
132 SET_BASIC_BLOCK (ENTRY_BLOCK, ENTRY_BLOCK_PTR);
133 SET_BASIC_BLOCK (EXIT_BLOCK, EXIT_BLOCK_PTR);
134 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
135 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
138 /*---------------------------------------------------------------------------
139 Create basic blocks
140 ---------------------------------------------------------------------------*/
142 /* Entry point to the CFG builder for trees. TP points to the list of
143 statements to be added to the flowgraph. */
145 static void
146 build_tree_cfg (tree *tp)
148 /* Register specific tree functions. */
149 tree_register_cfg_hooks ();
151 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
153 init_empty_tree_cfg ();
155 found_computed_goto = 0;
156 make_blocks (*tp);
158 /* Computed gotos are hell to deal with, especially if there are
159 lots of them with a large number of destinations. So we factor
160 them to a common computed goto location before we build the
161 edge list. After we convert back to normal form, we will un-factor
162 the computed gotos since factoring introduces an unwanted jump. */
163 if (found_computed_goto)
164 factor_computed_gotos ();
166 /* Make sure there is always at least one block, even if it's empty. */
167 if (n_basic_blocks == NUM_FIXED_BLOCKS)
168 create_empty_bb (ENTRY_BLOCK_PTR);
170 /* Adjust the size of the array. */
171 if (VEC_length (basic_block, basic_block_info) < (size_t) n_basic_blocks)
172 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, n_basic_blocks);
174 /* To speed up statement iterator walks, we first purge dead labels. */
175 cleanup_dead_labels ();
177 /* Group case nodes to reduce the number of edges.
178 We do this after cleaning up dead labels because otherwise we miss
179 a lot of obvious case merging opportunities. */
180 group_case_labels ();
182 /* Create the edges of the flowgraph. */
183 make_edges ();
184 cleanup_dead_labels ();
186 /* Debugging dumps. */
188 /* Write the flowgraph to a VCG file. */
190 int local_dump_flags;
191 FILE *vcg_file = dump_begin (TDI_vcg, &local_dump_flags);
192 if (vcg_file)
194 tree_cfg2vcg (vcg_file);
195 dump_end (TDI_vcg, vcg_file);
199 #ifdef ENABLE_CHECKING
200 verify_stmts ();
201 #endif
203 /* Dump a textual representation of the flowgraph. */
204 if (dump_file)
205 dump_tree_cfg (dump_file, dump_flags);
208 static unsigned int
209 execute_build_cfg (void)
211 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
212 return 0;
215 struct tree_opt_pass pass_build_cfg =
217 "cfg", /* name */
218 NULL, /* gate */
219 execute_build_cfg, /* execute */
220 NULL, /* sub */
221 NULL, /* next */
222 0, /* static_pass_number */
223 TV_TREE_CFG, /* tv_id */
224 PROP_gimple_leh, /* properties_required */
225 PROP_cfg, /* properties_provided */
226 0, /* properties_destroyed */
227 0, /* todo_flags_start */
228 TODO_verify_stmts | TODO_cleanup_cfg, /* todo_flags_finish */
229 0 /* letter */
232 /* Search the CFG for any computed gotos. If found, factor them to a
233 common computed goto site. Also record the location of that site so
234 that we can un-factor the gotos after we have converted back to
235 normal form. */
237 static void
238 factor_computed_gotos (void)
240 basic_block bb;
241 tree factored_label_decl = NULL;
242 tree var = NULL;
243 tree factored_computed_goto_label = NULL;
244 tree factored_computed_goto = NULL;
246 /* We know there are one or more computed gotos in this function.
247 Examine the last statement in each basic block to see if the block
248 ends with a computed goto. */
250 FOR_EACH_BB (bb)
252 block_stmt_iterator bsi = bsi_last (bb);
253 tree last;
255 if (bsi_end_p (bsi))
256 continue;
257 last = bsi_stmt (bsi);
259 /* Ignore the computed goto we create when we factor the original
260 computed gotos. */
261 if (last == factored_computed_goto)
262 continue;
264 /* If the last statement is a computed goto, factor it. */
265 if (computed_goto_p (last))
267 tree assignment;
269 /* The first time we find a computed goto we need to create
270 the factored goto block and the variable each original
271 computed goto will use for their goto destination. */
272 if (! factored_computed_goto)
274 basic_block new_bb = create_empty_bb (bb);
275 block_stmt_iterator new_bsi = bsi_start (new_bb);
277 /* Create the destination of the factored goto. Each original
278 computed goto will put its desired destination into this
279 variable and jump to the label we create immediately
280 below. */
281 var = create_tmp_var (ptr_type_node, "gotovar");
283 /* Build a label for the new block which will contain the
284 factored computed goto. */
285 factored_label_decl = create_artificial_label ();
286 factored_computed_goto_label
287 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
288 bsi_insert_after (&new_bsi, factored_computed_goto_label,
289 BSI_NEW_STMT);
291 /* Build our new computed goto. */
292 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
293 bsi_insert_after (&new_bsi, factored_computed_goto,
294 BSI_NEW_STMT);
297 /* Copy the original computed goto's destination into VAR. */
298 assignment = build_gimple_modify_stmt (var,
299 GOTO_DESTINATION (last));
300 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
302 /* And re-vector the computed goto to the new destination. */
303 GOTO_DESTINATION (last) = factored_label_decl;
309 /* Build a flowgraph for the statement_list STMT_LIST. */
311 static void
312 make_blocks (tree stmt_list)
314 tree_stmt_iterator i = tsi_start (stmt_list);
315 tree stmt = NULL;
316 bool start_new_block = true;
317 bool first_stmt_of_list = true;
318 basic_block bb = ENTRY_BLOCK_PTR;
320 while (!tsi_end_p (i))
322 tree prev_stmt;
324 prev_stmt = stmt;
325 stmt = tsi_stmt (i);
327 /* If the statement starts a new basic block or if we have determined
328 in a previous pass that we need to create a new block for STMT, do
329 so now. */
330 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
332 if (!first_stmt_of_list)
333 stmt_list = tsi_split_statement_list_before (&i);
334 bb = create_basic_block (stmt_list, NULL, bb);
335 start_new_block = false;
338 /* Now add STMT to BB and create the subgraphs for special statement
339 codes. */
340 set_bb_for_stmt (stmt, bb);
342 if (computed_goto_p (stmt))
343 found_computed_goto = true;
345 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
346 next iteration. */
347 if (stmt_ends_bb_p (stmt))
348 start_new_block = true;
350 tsi_next (&i);
351 first_stmt_of_list = false;
356 /* Create and return a new empty basic block after bb AFTER. */
358 static basic_block
359 create_bb (void *h, void *e, basic_block after)
361 basic_block bb;
363 gcc_assert (!e);
365 /* Create and initialize a new basic block. Since alloc_block uses
366 ggc_alloc_cleared to allocate a basic block, we do not have to
367 clear the newly allocated basic block here. */
368 bb = alloc_block ();
370 bb->index = last_basic_block;
371 bb->flags = BB_NEW;
372 bb->il.tree = GGC_CNEW (struct tree_bb_info);
373 set_bb_stmt_list (bb, h ? (tree) h : alloc_stmt_list ());
375 /* Add the new block to the linked list of blocks. */
376 link_block (bb, after);
378 /* Grow the basic block array if needed. */
379 if ((size_t) last_basic_block == VEC_length (basic_block, basic_block_info))
381 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
382 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
385 /* Add the newly created block to the array. */
386 SET_BASIC_BLOCK (last_basic_block, bb);
388 n_basic_blocks++;
389 last_basic_block++;
391 return bb;
395 /*---------------------------------------------------------------------------
396 Edge creation
397 ---------------------------------------------------------------------------*/
399 /* Fold COND_EXPR_COND of each COND_EXPR. */
401 void
402 fold_cond_expr_cond (void)
404 basic_block bb;
406 FOR_EACH_BB (bb)
408 tree stmt = last_stmt (bb);
410 if (stmt
411 && TREE_CODE (stmt) == COND_EXPR)
413 tree cond;
414 bool zerop, onep;
416 fold_defer_overflow_warnings ();
417 cond = fold (COND_EXPR_COND (stmt));
418 zerop = integer_zerop (cond);
419 onep = integer_onep (cond);
420 fold_undefer_overflow_warnings (zerop || onep,
421 stmt,
422 WARN_STRICT_OVERFLOW_CONDITIONAL);
423 if (zerop)
424 COND_EXPR_COND (stmt) = boolean_false_node;
425 else if (onep)
426 COND_EXPR_COND (stmt) = boolean_true_node;
431 /* Join all the blocks in the flowgraph. */
433 static void
434 make_edges (void)
436 basic_block bb;
437 struct omp_region *cur_region = NULL;
439 /* Create an edge from entry to the first block with executable
440 statements in it. */
441 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (NUM_FIXED_BLOCKS), EDGE_FALLTHRU);
443 /* Traverse the basic block array placing edges. */
444 FOR_EACH_BB (bb)
446 tree last = last_stmt (bb);
447 bool fallthru;
449 if (last)
451 enum tree_code code = TREE_CODE (last);
452 switch (code)
454 case GOTO_EXPR:
455 make_goto_expr_edges (bb);
456 fallthru = false;
457 break;
458 case RETURN_EXPR:
459 make_edge (bb, EXIT_BLOCK_PTR, 0);
460 fallthru = false;
461 break;
462 case COND_EXPR:
463 make_cond_expr_edges (bb);
464 fallthru = false;
465 break;
466 case SWITCH_EXPR:
467 make_switch_expr_edges (bb);
468 fallthru = false;
469 break;
470 case RESX_EXPR:
471 make_eh_edges (last);
472 fallthru = false;
473 break;
475 case CALL_EXPR:
476 /* If this function receives a nonlocal goto, then we need to
477 make edges from this call site to all the nonlocal goto
478 handlers. */
479 if (tree_can_make_abnormal_goto (last))
480 make_abnormal_goto_edges (bb, true);
482 /* If this statement has reachable exception handlers, then
483 create abnormal edges to them. */
484 make_eh_edges (last);
486 /* Some calls are known not to return. */
487 fallthru = !(call_expr_flags (last) & ECF_NORETURN);
488 break;
490 case MODIFY_EXPR:
491 gcc_unreachable ();
493 case GIMPLE_MODIFY_STMT:
494 if (is_ctrl_altering_stmt (last))
496 /* A GIMPLE_MODIFY_STMT may have a CALL_EXPR on its RHS and
497 the CALL_EXPR may have an abnormal edge. Search the RHS
498 for this case and create any required edges. */
499 if (tree_can_make_abnormal_goto (last))
500 make_abnormal_goto_edges (bb, true);
502 make_eh_edges (last);
504 fallthru = true;
505 break;
507 case OMP_PARALLEL:
508 case OMP_FOR:
509 case OMP_SINGLE:
510 case OMP_MASTER:
511 case OMP_ORDERED:
512 case OMP_CRITICAL:
513 case OMP_SECTION:
514 cur_region = new_omp_region (bb, code, cur_region);
515 fallthru = true;
516 break;
518 case OMP_SECTIONS:
519 cur_region = new_omp_region (bb, code, cur_region);
520 fallthru = true;
521 break;
523 case OMP_SECTIONS_SWITCH:
524 fallthru = false;
525 break;
528 case OMP_ATOMIC_LOAD:
529 case OMP_ATOMIC_STORE:
530 fallthru = true;
531 break;
534 case OMP_RETURN:
535 /* In the case of an OMP_SECTION, the edge will go somewhere
536 other than the next block. This will be created later. */
537 cur_region->exit = bb;
538 fallthru = cur_region->type != OMP_SECTION;
539 cur_region = cur_region->outer;
540 break;
542 case OMP_CONTINUE:
543 cur_region->cont = bb;
544 switch (cur_region->type)
546 case OMP_FOR:
547 /* Make the loopback edge. */
548 make_edge (bb, single_succ (cur_region->entry), 0);
550 /* Create an edge from OMP_FOR to exit, which corresponds to
551 the case that the body of the loop is not executed at
552 all. */
553 make_edge (cur_region->entry, bb->next_bb, 0);
554 fallthru = true;
555 break;
557 case OMP_SECTIONS:
558 /* Wire up the edges into and out of the nested sections. */
560 basic_block switch_bb = single_succ (cur_region->entry);
562 struct omp_region *i;
563 for (i = cur_region->inner; i ; i = i->next)
565 gcc_assert (i->type == OMP_SECTION);
566 make_edge (switch_bb, i->entry, 0);
567 make_edge (i->exit, bb, EDGE_FALLTHRU);
570 /* Make the loopback edge to the block with
571 OMP_SECTIONS_SWITCH. */
572 make_edge (bb, switch_bb, 0);
574 /* Make the edge from the switch to exit. */
575 make_edge (switch_bb, bb->next_bb, 0);
576 fallthru = false;
578 break;
580 default:
581 gcc_unreachable ();
583 break;
585 default:
586 gcc_assert (!stmt_ends_bb_p (last));
587 fallthru = true;
590 else
591 fallthru = true;
593 if (fallthru)
594 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
597 if (root_omp_region)
598 free_omp_regions ();
600 /* Fold COND_EXPR_COND of each COND_EXPR. */
601 fold_cond_expr_cond ();
605 /* Create the edges for a COND_EXPR starting at block BB.
606 At this point, both clauses must contain only simple gotos. */
608 static void
609 make_cond_expr_edges (basic_block bb)
611 tree entry = last_stmt (bb);
612 basic_block then_bb, else_bb;
613 tree then_label, else_label;
614 edge e;
616 gcc_assert (entry);
617 gcc_assert (TREE_CODE (entry) == COND_EXPR);
619 /* Entry basic blocks for each component. */
620 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
621 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
622 then_bb = label_to_block (then_label);
623 else_bb = label_to_block (else_label);
625 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
626 #ifdef USE_MAPPED_LOCATION
627 e->goto_locus = EXPR_LOCATION (COND_EXPR_THEN (entry));
628 #else
629 e->goto_locus = EXPR_LOCUS (COND_EXPR_THEN (entry));
630 #endif
631 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
632 if (e)
634 #ifdef USE_MAPPED_LOCATION
635 e->goto_locus = EXPR_LOCATION (COND_EXPR_ELSE (entry));
636 #else
637 e->goto_locus = EXPR_LOCUS (COND_EXPR_ELSE (entry));
638 #endif
641 /* We do not need the gotos anymore. */
642 COND_EXPR_THEN (entry) = NULL_TREE;
643 COND_EXPR_ELSE (entry) = NULL_TREE;
647 /* Called for each element in the hash table (P) as we delete the
648 edge to cases hash table.
650 Clear all the TREE_CHAINs to prevent problems with copying of
651 SWITCH_EXPRs and structure sharing rules, then free the hash table
652 element. */
654 static bool
655 edge_to_cases_cleanup (const void *key ATTRIBUTE_UNUSED, void **value,
656 void *data ATTRIBUTE_UNUSED)
658 tree t, next;
660 for (t = (tree) *value; t; t = next)
662 next = TREE_CHAIN (t);
663 TREE_CHAIN (t) = NULL;
666 *value = NULL;
667 return false;
670 /* Start recording information mapping edges to case labels. */
672 void
673 start_recording_case_labels (void)
675 gcc_assert (edge_to_cases == NULL);
676 edge_to_cases = pointer_map_create ();
679 /* Return nonzero if we are recording information for case labels. */
681 static bool
682 recording_case_labels_p (void)
684 return (edge_to_cases != NULL);
687 /* Stop recording information mapping edges to case labels and
688 remove any information we have recorded. */
689 void
690 end_recording_case_labels (void)
692 pointer_map_traverse (edge_to_cases, edge_to_cases_cleanup, NULL);
693 pointer_map_destroy (edge_to_cases);
694 edge_to_cases = NULL;
697 /* If we are inside a {start,end}_recording_cases block, then return
698 a chain of CASE_LABEL_EXPRs from T which reference E.
700 Otherwise return NULL. */
702 static tree
703 get_cases_for_edge (edge e, tree t)
705 void **slot;
706 size_t i, n;
707 tree vec;
709 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
710 chains available. Return NULL so the caller can detect this case. */
711 if (!recording_case_labels_p ())
712 return NULL;
714 slot = pointer_map_contains (edge_to_cases, e);
715 if (slot)
716 return (tree) *slot;
718 /* If we did not find E in the hash table, then this must be the first
719 time we have been queried for information about E & T. Add all the
720 elements from T to the hash table then perform the query again. */
722 vec = SWITCH_LABELS (t);
723 n = TREE_VEC_LENGTH (vec);
724 for (i = 0; i < n; i++)
726 tree elt = TREE_VEC_ELT (vec, i);
727 tree lab = CASE_LABEL (elt);
728 basic_block label_bb = label_to_block (lab);
729 edge this_edge = find_edge (e->src, label_bb);
731 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
732 a new chain. */
733 slot = pointer_map_insert (edge_to_cases, this_edge);
734 TREE_CHAIN (elt) = (tree) *slot;
735 *slot = elt;
738 return (tree) *pointer_map_contains (edge_to_cases, e);
741 /* Create the edges for a SWITCH_EXPR starting at block BB.
742 At this point, the switch body has been lowered and the
743 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
745 static void
746 make_switch_expr_edges (basic_block bb)
748 tree entry = last_stmt (bb);
749 size_t i, n;
750 tree vec;
752 vec = SWITCH_LABELS (entry);
753 n = TREE_VEC_LENGTH (vec);
755 for (i = 0; i < n; ++i)
757 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
758 basic_block label_bb = label_to_block (lab);
759 make_edge (bb, label_bb, 0);
764 /* Return the basic block holding label DEST. */
766 basic_block
767 label_to_block_fn (struct function *ifun, tree dest)
769 int uid = LABEL_DECL_UID (dest);
771 /* We would die hard when faced by an undefined label. Emit a label to
772 the very first basic block. This will hopefully make even the dataflow
773 and undefined variable warnings quite right. */
774 if ((errorcount || sorrycount) && uid < 0)
776 block_stmt_iterator bsi =
777 bsi_start (BASIC_BLOCK (NUM_FIXED_BLOCKS));
778 tree stmt;
780 stmt = build1 (LABEL_EXPR, void_type_node, dest);
781 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
782 uid = LABEL_DECL_UID (dest);
784 if (VEC_length (basic_block, ifun->cfg->x_label_to_block_map)
785 <= (unsigned int) uid)
786 return NULL;
787 return VEC_index (basic_block, ifun->cfg->x_label_to_block_map, uid);
790 /* Create edges for an abnormal goto statement at block BB. If FOR_CALL
791 is true, the source statement is a CALL_EXPR instead of a GOTO_EXPR. */
793 void
794 make_abnormal_goto_edges (basic_block bb, bool for_call)
796 basic_block target_bb;
797 block_stmt_iterator bsi;
799 FOR_EACH_BB (target_bb)
800 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
802 tree target = bsi_stmt (bsi);
804 if (TREE_CODE (target) != LABEL_EXPR)
805 break;
807 target = LABEL_EXPR_LABEL (target);
809 /* Make an edge to every label block that has been marked as a
810 potential target for a computed goto or a non-local goto. */
811 if ((FORCED_LABEL (target) && !for_call)
812 || (DECL_NONLOCAL (target) && for_call))
814 make_edge (bb, target_bb, EDGE_ABNORMAL);
815 break;
820 /* Create edges for a goto statement at block BB. */
822 static void
823 make_goto_expr_edges (basic_block bb)
825 block_stmt_iterator last = bsi_last (bb);
826 tree goto_t = bsi_stmt (last);
828 /* A simple GOTO creates normal edges. */
829 if (simple_goto_p (goto_t))
831 tree dest = GOTO_DESTINATION (goto_t);
832 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
833 #ifdef USE_MAPPED_LOCATION
834 e->goto_locus = EXPR_LOCATION (goto_t);
835 #else
836 e->goto_locus = EXPR_LOCUS (goto_t);
837 #endif
838 bsi_remove (&last, true);
839 return;
842 /* A computed GOTO creates abnormal edges. */
843 make_abnormal_goto_edges (bb, false);
847 /*---------------------------------------------------------------------------
848 Flowgraph analysis
849 ---------------------------------------------------------------------------*/
851 /* Cleanup useless labels in basic blocks. This is something we wish
852 to do early because it allows us to group case labels before creating
853 the edges for the CFG, and it speeds up block statement iterators in
854 all passes later on.
855 We rerun this pass after CFG is created, to get rid of the labels that
856 are no longer referenced. After then we do not run it any more, since
857 (almost) no new labels should be created. */
859 /* A map from basic block index to the leading label of that block. */
860 static struct label_record
862 /* The label. */
863 tree label;
865 /* True if the label is referenced from somewhere. */
866 bool used;
867 } *label_for_bb;
869 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
870 static void
871 update_eh_label (struct eh_region *region)
873 tree old_label = get_eh_region_tree_label (region);
874 if (old_label)
876 tree new_label;
877 basic_block bb = label_to_block (old_label);
879 /* ??? After optimizing, there may be EH regions with labels
880 that have already been removed from the function body, so
881 there is no basic block for them. */
882 if (! bb)
883 return;
885 new_label = label_for_bb[bb->index].label;
886 label_for_bb[bb->index].used = true;
887 set_eh_region_tree_label (region, new_label);
891 /* Given LABEL return the first label in the same basic block. */
892 static tree
893 main_block_label (tree label)
895 basic_block bb = label_to_block (label);
896 tree main_label = label_for_bb[bb->index].label;
898 /* label_to_block possibly inserted undefined label into the chain. */
899 if (!main_label)
901 label_for_bb[bb->index].label = label;
902 main_label = label;
905 label_for_bb[bb->index].used = true;
906 return main_label;
909 /* Cleanup redundant labels. This is a three-step process:
910 1) Find the leading label for each block.
911 2) Redirect all references to labels to the leading labels.
912 3) Cleanup all useless labels. */
914 void
915 cleanup_dead_labels (void)
917 basic_block bb;
918 label_for_bb = XCNEWVEC (struct label_record, last_basic_block);
920 /* Find a suitable label for each block. We use the first user-defined
921 label if there is one, or otherwise just the first label we see. */
922 FOR_EACH_BB (bb)
924 block_stmt_iterator i;
926 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
928 tree label, stmt = bsi_stmt (i);
930 if (TREE_CODE (stmt) != LABEL_EXPR)
931 break;
933 label = LABEL_EXPR_LABEL (stmt);
935 /* If we have not yet seen a label for the current block,
936 remember this one and see if there are more labels. */
937 if (!label_for_bb[bb->index].label)
939 label_for_bb[bb->index].label = label;
940 continue;
943 /* If we did see a label for the current block already, but it
944 is an artificially created label, replace it if the current
945 label is a user defined label. */
946 if (!DECL_ARTIFICIAL (label)
947 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
949 label_for_bb[bb->index].label = label;
950 break;
955 /* Now redirect all jumps/branches to the selected label.
956 First do so for each block ending in a control statement. */
957 FOR_EACH_BB (bb)
959 tree stmt = last_stmt (bb);
960 if (!stmt)
961 continue;
963 switch (TREE_CODE (stmt))
965 case COND_EXPR:
967 tree true_branch, false_branch;
969 true_branch = COND_EXPR_THEN (stmt);
970 false_branch = COND_EXPR_ELSE (stmt);
972 if (true_branch)
973 GOTO_DESTINATION (true_branch)
974 = main_block_label (GOTO_DESTINATION (true_branch));
975 if (false_branch)
976 GOTO_DESTINATION (false_branch)
977 = main_block_label (GOTO_DESTINATION (false_branch));
979 break;
982 case SWITCH_EXPR:
984 size_t i;
985 tree vec = SWITCH_LABELS (stmt);
986 size_t n = TREE_VEC_LENGTH (vec);
988 /* Replace all destination labels. */
989 for (i = 0; i < n; ++i)
991 tree elt = TREE_VEC_ELT (vec, i);
992 tree label = main_block_label (CASE_LABEL (elt));
993 CASE_LABEL (elt) = label;
995 break;
998 /* We have to handle GOTO_EXPRs until they're removed, and we don't
999 remove them until after we've created the CFG edges. */
1000 case GOTO_EXPR:
1001 if (! computed_goto_p (stmt))
1003 GOTO_DESTINATION (stmt)
1004 = main_block_label (GOTO_DESTINATION (stmt));
1005 break;
1008 default:
1009 break;
1013 for_each_eh_region (update_eh_label);
1015 /* Finally, purge dead labels. All user-defined labels and labels that
1016 can be the target of non-local gotos and labels which have their
1017 address taken are preserved. */
1018 FOR_EACH_BB (bb)
1020 block_stmt_iterator i;
1021 tree label_for_this_bb = label_for_bb[bb->index].label;
1023 if (!label_for_this_bb)
1024 continue;
1026 /* If the main label of the block is unused, we may still remove it. */
1027 if (!label_for_bb[bb->index].used)
1028 label_for_this_bb = NULL;
1030 for (i = bsi_start (bb); !bsi_end_p (i); )
1032 tree label, stmt = bsi_stmt (i);
1034 if (TREE_CODE (stmt) != LABEL_EXPR)
1035 break;
1037 label = LABEL_EXPR_LABEL (stmt);
1039 if (label == label_for_this_bb
1040 || ! DECL_ARTIFICIAL (label)
1041 || DECL_NONLOCAL (label)
1042 || FORCED_LABEL (label))
1043 bsi_next (&i);
1044 else
1045 bsi_remove (&i, true);
1049 free (label_for_bb);
1052 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1053 and scan the sorted vector of cases. Combine the ones jumping to the
1054 same label.
1055 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1057 void
1058 group_case_labels (void)
1060 basic_block bb;
1062 FOR_EACH_BB (bb)
1064 tree stmt = last_stmt (bb);
1065 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1067 tree labels = SWITCH_LABELS (stmt);
1068 int old_size = TREE_VEC_LENGTH (labels);
1069 int i, j, new_size = old_size;
1070 tree default_case = TREE_VEC_ELT (labels, old_size - 1);
1071 tree default_label;
1073 /* The default label is always the last case in a switch
1074 statement after gimplification. */
1075 default_label = CASE_LABEL (default_case);
1077 /* Look for possible opportunities to merge cases.
1078 Ignore the last element of the label vector because it
1079 must be the default case. */
1080 i = 0;
1081 while (i < old_size - 1)
1083 tree base_case, base_label, base_high;
1084 base_case = TREE_VEC_ELT (labels, i);
1086 gcc_assert (base_case);
1087 base_label = CASE_LABEL (base_case);
1089 /* Discard cases that have the same destination as the
1090 default case. */
1091 if (base_label == default_label)
1093 TREE_VEC_ELT (labels, i) = NULL_TREE;
1094 i++;
1095 new_size--;
1096 continue;
1099 base_high = CASE_HIGH (base_case) ?
1100 CASE_HIGH (base_case) : CASE_LOW (base_case);
1101 i++;
1102 /* Try to merge case labels. Break out when we reach the end
1103 of the label vector or when we cannot merge the next case
1104 label with the current one. */
1105 while (i < old_size - 1)
1107 tree merge_case = TREE_VEC_ELT (labels, i);
1108 tree merge_label = CASE_LABEL (merge_case);
1109 tree t = int_const_binop (PLUS_EXPR, base_high,
1110 integer_one_node, 1);
1112 /* Merge the cases if they jump to the same place,
1113 and their ranges are consecutive. */
1114 if (merge_label == base_label
1115 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1117 base_high = CASE_HIGH (merge_case) ?
1118 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1119 CASE_HIGH (base_case) = base_high;
1120 TREE_VEC_ELT (labels, i) = NULL_TREE;
1121 new_size--;
1122 i++;
1124 else
1125 break;
1129 /* Compress the case labels in the label vector, and adjust the
1130 length of the vector. */
1131 for (i = 0, j = 0; i < new_size; i++)
1133 while (! TREE_VEC_ELT (labels, j))
1134 j++;
1135 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1137 TREE_VEC_LENGTH (labels) = new_size;
1142 /* Checks whether we can merge block B into block A. */
1144 static bool
1145 tree_can_merge_blocks_p (basic_block a, basic_block b)
1147 const_tree stmt;
1148 block_stmt_iterator bsi;
1149 tree phi;
1151 if (!single_succ_p (a))
1152 return false;
1154 if (single_succ_edge (a)->flags & EDGE_ABNORMAL)
1155 return false;
1157 if (single_succ (a) != b)
1158 return false;
1160 if (!single_pred_p (b))
1161 return false;
1163 if (b == EXIT_BLOCK_PTR)
1164 return false;
1166 /* If A ends by a statement causing exceptions or something similar, we
1167 cannot merge the blocks. */
1168 /* This CONST_CAST is okay because last_stmt doesn't modify its
1169 argument and the return value is assign to a const_tree. */
1170 stmt = last_stmt (CONST_CAST_BB (a));
1171 if (stmt && stmt_ends_bb_p (stmt))
1172 return false;
1174 /* Do not allow a block with only a non-local label to be merged. */
1175 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1176 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1177 return false;
1179 /* It must be possible to eliminate all phi nodes in B. If ssa form
1180 is not up-to-date, we cannot eliminate any phis; however, if only
1181 some symbols as whole are marked for renaming, this is not a problem,
1182 as phi nodes for those symbols are irrelevant in updating anyway. */
1183 phi = phi_nodes (b);
1184 if (phi)
1186 if (name_mappings_registered_p ())
1187 return false;
1189 for (; phi; phi = PHI_CHAIN (phi))
1190 if (!is_gimple_reg (PHI_RESULT (phi))
1191 && !may_propagate_copy (PHI_RESULT (phi), PHI_ARG_DEF (phi, 0)))
1192 return false;
1195 /* Do not remove user labels. */
1196 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1198 stmt = bsi_stmt (bsi);
1199 if (TREE_CODE (stmt) != LABEL_EXPR)
1200 break;
1201 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1202 return false;
1205 /* Protect the loop latches. */
1206 if (current_loops
1207 && b->loop_father->latch == b)
1208 return false;
1210 return true;
1213 /* Replaces all uses of NAME by VAL. */
1215 void
1216 replace_uses_by (tree name, tree val)
1218 imm_use_iterator imm_iter;
1219 use_operand_p use;
1220 tree stmt;
1221 edge e;
1223 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1225 if (TREE_CODE (stmt) != PHI_NODE)
1226 push_stmt_changes (&stmt);
1228 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1230 replace_exp (use, val);
1232 if (TREE_CODE (stmt) == PHI_NODE)
1234 e = PHI_ARG_EDGE (stmt, PHI_ARG_INDEX_FROM_USE (use));
1235 if (e->flags & EDGE_ABNORMAL)
1237 /* This can only occur for virtual operands, since
1238 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1239 would prevent replacement. */
1240 gcc_assert (!is_gimple_reg (name));
1241 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1246 if (TREE_CODE (stmt) != PHI_NODE)
1248 tree rhs;
1250 fold_stmt_inplace (stmt);
1251 if (cfgcleanup_altered_bbs)
1252 bitmap_set_bit (cfgcleanup_altered_bbs, bb_for_stmt (stmt)->index);
1254 /* FIXME. This should go in pop_stmt_changes. */
1255 rhs = get_rhs (stmt);
1256 if (TREE_CODE (rhs) == ADDR_EXPR)
1257 recompute_tree_invariant_for_addr_expr (rhs);
1259 maybe_clean_or_replace_eh_stmt (stmt, stmt);
1261 pop_stmt_changes (&stmt);
1265 gcc_assert (has_zero_uses (name));
1267 /* Also update the trees stored in loop structures. */
1268 if (current_loops)
1270 struct loop *loop;
1271 loop_iterator li;
1273 FOR_EACH_LOOP (li, loop, 0)
1275 substitute_in_loop_info (loop, name, val);
1280 /* Merge block B into block A. */
1282 static void
1283 tree_merge_blocks (basic_block a, basic_block b)
1285 block_stmt_iterator bsi;
1286 tree_stmt_iterator last;
1287 tree phi;
1289 if (dump_file)
1290 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1292 /* Remove all single-valued PHI nodes from block B of the form
1293 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1294 bsi = bsi_last (a);
1295 for (phi = phi_nodes (b); phi; phi = phi_nodes (b))
1297 tree def = PHI_RESULT (phi), use = PHI_ARG_DEF (phi, 0);
1298 tree copy;
1299 bool may_replace_uses = may_propagate_copy (def, use);
1301 /* In case we maintain loop closed ssa form, do not propagate arguments
1302 of loop exit phi nodes. */
1303 if (current_loops
1304 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1305 && is_gimple_reg (def)
1306 && TREE_CODE (use) == SSA_NAME
1307 && a->loop_father != b->loop_father)
1308 may_replace_uses = false;
1310 if (!may_replace_uses)
1312 gcc_assert (is_gimple_reg (def));
1314 /* Note that just emitting the copies is fine -- there is no problem
1315 with ordering of phi nodes. This is because A is the single
1316 predecessor of B, therefore results of the phi nodes cannot
1317 appear as arguments of the phi nodes. */
1318 copy = build_gimple_modify_stmt (def, use);
1319 bsi_insert_after (&bsi, copy, BSI_NEW_STMT);
1320 SSA_NAME_DEF_STMT (def) = copy;
1321 remove_phi_node (phi, NULL, false);
1323 else
1325 replace_uses_by (def, use);
1326 remove_phi_node (phi, NULL, true);
1330 /* Ensure that B follows A. */
1331 move_block_after (b, a);
1333 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1334 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1336 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1337 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1339 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1341 tree label = bsi_stmt (bsi);
1343 bsi_remove (&bsi, false);
1344 /* Now that we can thread computed gotos, we might have
1345 a situation where we have a forced label in block B
1346 However, the label at the start of block B might still be
1347 used in other ways (think about the runtime checking for
1348 Fortran assigned gotos). So we can not just delete the
1349 label. Instead we move the label to the start of block A. */
1350 if (FORCED_LABEL (LABEL_EXPR_LABEL (label)))
1352 block_stmt_iterator dest_bsi = bsi_start (a);
1353 bsi_insert_before (&dest_bsi, label, BSI_NEW_STMT);
1356 else
1358 change_bb_for_stmt (bsi_stmt (bsi), a);
1359 bsi_next (&bsi);
1363 /* Merge the chains. */
1364 last = tsi_last (bb_stmt_list (a));
1365 tsi_link_after (&last, bb_stmt_list (b), TSI_NEW_STMT);
1366 set_bb_stmt_list (b, NULL_TREE);
1368 if (cfgcleanup_altered_bbs)
1369 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1373 /* Return the one of two successors of BB that is not reachable by a
1374 reached by a complex edge, if there is one. Else, return BB. We use
1375 this in optimizations that use post-dominators for their heuristics,
1376 to catch the cases in C++ where function calls are involved. */
1378 basic_block
1379 single_noncomplex_succ (basic_block bb)
1381 edge e0, e1;
1382 if (EDGE_COUNT (bb->succs) != 2)
1383 return bb;
1385 e0 = EDGE_SUCC (bb, 0);
1386 e1 = EDGE_SUCC (bb, 1);
1387 if (e0->flags & EDGE_COMPLEX)
1388 return e1->dest;
1389 if (e1->flags & EDGE_COMPLEX)
1390 return e0->dest;
1392 return bb;
1396 /* Walk the function tree removing unnecessary statements.
1398 * Empty statement nodes are removed
1400 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1402 * Unnecessary COND_EXPRs are removed
1404 * Some unnecessary BIND_EXPRs are removed
1406 Clearly more work could be done. The trick is doing the analysis
1407 and removal fast enough to be a net improvement in compile times.
1409 Note that when we remove a control structure such as a COND_EXPR
1410 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1411 to ensure we eliminate all the useless code. */
1413 struct rus_data
1415 tree *last_goto;
1416 bool repeat;
1417 bool may_throw;
1418 bool may_branch;
1419 bool has_label;
1422 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1424 static bool
1425 remove_useless_stmts_warn_notreached (tree stmt)
1427 if (EXPR_HAS_LOCATION (stmt))
1429 location_t loc = EXPR_LOCATION (stmt);
1430 if (LOCATION_LINE (loc) > 0)
1432 warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
1433 return true;
1437 switch (TREE_CODE (stmt))
1439 case STATEMENT_LIST:
1441 tree_stmt_iterator i;
1442 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1443 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1444 return true;
1446 break;
1448 case COND_EXPR:
1449 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1450 return true;
1451 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1452 return true;
1453 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1454 return true;
1455 break;
1457 case TRY_FINALLY_EXPR:
1458 case TRY_CATCH_EXPR:
1459 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1460 return true;
1461 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1462 return true;
1463 break;
1465 case CATCH_EXPR:
1466 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1467 case EH_FILTER_EXPR:
1468 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1469 case BIND_EXPR:
1470 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1472 default:
1473 /* Not a live container. */
1474 break;
1477 return false;
1480 static void
1481 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1483 tree then_clause, else_clause, cond;
1484 bool save_has_label, then_has_label, else_has_label;
1486 save_has_label = data->has_label;
1487 data->has_label = false;
1488 data->last_goto = NULL;
1490 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1492 then_has_label = data->has_label;
1493 data->has_label = false;
1494 data->last_goto = NULL;
1496 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1498 else_has_label = data->has_label;
1499 data->has_label = save_has_label | then_has_label | else_has_label;
1501 then_clause = COND_EXPR_THEN (*stmt_p);
1502 else_clause = COND_EXPR_ELSE (*stmt_p);
1503 cond = fold (COND_EXPR_COND (*stmt_p));
1505 /* If neither arm does anything at all, we can remove the whole IF. */
1506 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1508 *stmt_p = build_empty_stmt ();
1509 data->repeat = true;
1512 /* If there are no reachable statements in an arm, then we can
1513 zap the entire conditional. */
1514 else if (integer_nonzerop (cond) && !else_has_label)
1516 if (warn_notreached)
1517 remove_useless_stmts_warn_notreached (else_clause);
1518 *stmt_p = then_clause;
1519 data->repeat = true;
1521 else if (integer_zerop (cond) && !then_has_label)
1523 if (warn_notreached)
1524 remove_useless_stmts_warn_notreached (then_clause);
1525 *stmt_p = else_clause;
1526 data->repeat = true;
1529 /* Check a couple of simple things on then/else with single stmts. */
1530 else
1532 tree then_stmt = expr_only (then_clause);
1533 tree else_stmt = expr_only (else_clause);
1535 /* Notice branches to a common destination. */
1536 if (then_stmt && else_stmt
1537 && TREE_CODE (then_stmt) == GOTO_EXPR
1538 && TREE_CODE (else_stmt) == GOTO_EXPR
1539 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1541 *stmt_p = then_stmt;
1542 data->repeat = true;
1545 /* If the THEN/ELSE clause merely assigns a value to a variable or
1546 parameter which is already known to contain that value, then
1547 remove the useless THEN/ELSE clause. */
1548 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1550 if (else_stmt
1551 && TREE_CODE (else_stmt) == GIMPLE_MODIFY_STMT
1552 && GIMPLE_STMT_OPERAND (else_stmt, 0) == cond
1553 && integer_zerop (GIMPLE_STMT_OPERAND (else_stmt, 1)))
1554 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1556 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1557 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1558 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1559 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1561 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1562 ? then_stmt : else_stmt);
1563 tree *location = (TREE_CODE (cond) == EQ_EXPR
1564 ? &COND_EXPR_THEN (*stmt_p)
1565 : &COND_EXPR_ELSE (*stmt_p));
1567 if (stmt
1568 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1569 && GIMPLE_STMT_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1570 && GIMPLE_STMT_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1571 *location = alloc_stmt_list ();
1575 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1576 would be re-introduced during lowering. */
1577 data->last_goto = NULL;
1581 static void
1582 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1584 bool save_may_branch, save_may_throw;
1585 bool this_may_branch, this_may_throw;
1587 /* Collect may_branch and may_throw information for the body only. */
1588 save_may_branch = data->may_branch;
1589 save_may_throw = data->may_throw;
1590 data->may_branch = false;
1591 data->may_throw = false;
1592 data->last_goto = NULL;
1594 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1596 this_may_branch = data->may_branch;
1597 this_may_throw = data->may_throw;
1598 data->may_branch |= save_may_branch;
1599 data->may_throw |= save_may_throw;
1600 data->last_goto = NULL;
1602 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1604 /* If the body is empty, then we can emit the FINALLY block without
1605 the enclosing TRY_FINALLY_EXPR. */
1606 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1608 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1609 data->repeat = true;
1612 /* If the handler is empty, then we can emit the TRY block without
1613 the enclosing TRY_FINALLY_EXPR. */
1614 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1616 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1617 data->repeat = true;
1620 /* If the body neither throws, nor branches, then we can safely
1621 string the TRY and FINALLY blocks together. */
1622 else if (!this_may_branch && !this_may_throw)
1624 tree stmt = *stmt_p;
1625 *stmt_p = TREE_OPERAND (stmt, 0);
1626 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1627 data->repeat = true;
1632 static void
1633 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1635 bool save_may_throw, this_may_throw;
1636 tree_stmt_iterator i;
1637 tree stmt;
1639 /* Collect may_throw information for the body only. */
1640 save_may_throw = data->may_throw;
1641 data->may_throw = false;
1642 data->last_goto = NULL;
1644 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1646 this_may_throw = data->may_throw;
1647 data->may_throw = save_may_throw;
1649 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1650 if (!this_may_throw)
1652 if (warn_notreached)
1653 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1654 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1655 data->repeat = true;
1656 return;
1659 /* Process the catch clause specially. We may be able to tell that
1660 no exceptions propagate past this point. */
1662 this_may_throw = true;
1663 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1664 stmt = tsi_stmt (i);
1665 data->last_goto = NULL;
1667 switch (TREE_CODE (stmt))
1669 case CATCH_EXPR:
1670 for (; !tsi_end_p (i); tsi_next (&i))
1672 stmt = tsi_stmt (i);
1673 /* If we catch all exceptions, then the body does not
1674 propagate exceptions past this point. */
1675 if (CATCH_TYPES (stmt) == NULL)
1676 this_may_throw = false;
1677 data->last_goto = NULL;
1678 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1680 break;
1682 case EH_FILTER_EXPR:
1683 if (EH_FILTER_MUST_NOT_THROW (stmt))
1684 this_may_throw = false;
1685 else if (EH_FILTER_TYPES (stmt) == NULL)
1686 this_may_throw = false;
1687 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1688 break;
1690 default:
1691 /* Otherwise this is a cleanup. */
1692 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1694 /* If the cleanup is empty, then we can emit the TRY block without
1695 the enclosing TRY_CATCH_EXPR. */
1696 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1698 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1699 data->repeat = true;
1701 break;
1703 data->may_throw |= this_may_throw;
1707 static void
1708 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1710 tree block;
1712 /* First remove anything underneath the BIND_EXPR. */
1713 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1715 /* If the BIND_EXPR has no variables, then we can pull everything
1716 up one level and remove the BIND_EXPR, unless this is the toplevel
1717 BIND_EXPR for the current function or an inlined function.
1719 When this situation occurs we will want to apply this
1720 optimization again. */
1721 block = BIND_EXPR_BLOCK (*stmt_p);
1722 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1723 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1724 && (! block
1725 || ! BLOCK_ABSTRACT_ORIGIN (block)
1726 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1727 != FUNCTION_DECL)))
1729 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1730 data->repeat = true;
1735 static void
1736 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1738 tree dest = GOTO_DESTINATION (*stmt_p);
1740 data->may_branch = true;
1741 data->last_goto = NULL;
1743 /* Record the last goto expr, so that we can delete it if unnecessary. */
1744 if (TREE_CODE (dest) == LABEL_DECL)
1745 data->last_goto = stmt_p;
1749 static void
1750 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1752 tree label = LABEL_EXPR_LABEL (*stmt_p);
1754 data->has_label = true;
1756 /* We do want to jump across non-local label receiver code. */
1757 if (DECL_NONLOCAL (label))
1758 data->last_goto = NULL;
1760 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1762 *data->last_goto = build_empty_stmt ();
1763 data->repeat = true;
1766 /* ??? Add something here to delete unused labels. */
1770 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1771 decl. This allows us to eliminate redundant or useless
1772 calls to "const" functions.
1774 Gimplifier already does the same operation, but we may notice functions
1775 being const and pure once their calls has been gimplified, so we need
1776 to update the flag. */
1778 static void
1779 update_call_expr_flags (tree call)
1781 tree decl = get_callee_fndecl (call);
1782 if (!decl)
1783 return;
1784 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1785 TREE_SIDE_EFFECTS (call) = 0;
1786 if (TREE_NOTHROW (decl))
1787 TREE_NOTHROW (call) = 1;
1791 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1793 void
1794 notice_special_calls (tree t)
1796 int flags = call_expr_flags (t);
1798 if (flags & ECF_MAY_BE_ALLOCA)
1799 current_function_calls_alloca = true;
1800 if (flags & ECF_RETURNS_TWICE)
1801 current_function_calls_setjmp = true;
1805 /* Clear flags set by notice_special_calls. Used by dead code removal
1806 to update the flags. */
1808 void
1809 clear_special_calls (void)
1811 current_function_calls_alloca = false;
1812 current_function_calls_setjmp = false;
1816 static void
1817 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1819 tree t = *tp, op;
1821 switch (TREE_CODE (t))
1823 case COND_EXPR:
1824 remove_useless_stmts_cond (tp, data);
1825 break;
1827 case TRY_FINALLY_EXPR:
1828 remove_useless_stmts_tf (tp, data);
1829 break;
1831 case TRY_CATCH_EXPR:
1832 remove_useless_stmts_tc (tp, data);
1833 break;
1835 case BIND_EXPR:
1836 remove_useless_stmts_bind (tp, data);
1837 break;
1839 case GOTO_EXPR:
1840 remove_useless_stmts_goto (tp, data);
1841 break;
1843 case LABEL_EXPR:
1844 remove_useless_stmts_label (tp, data);
1845 break;
1847 case RETURN_EXPR:
1848 fold_stmt (tp);
1849 data->last_goto = NULL;
1850 data->may_branch = true;
1851 break;
1853 case CALL_EXPR:
1854 fold_stmt (tp);
1855 data->last_goto = NULL;
1856 notice_special_calls (t);
1857 update_call_expr_flags (t);
1858 if (tree_could_throw_p (t))
1859 data->may_throw = true;
1860 break;
1862 case MODIFY_EXPR:
1863 gcc_unreachable ();
1865 case GIMPLE_MODIFY_STMT:
1866 data->last_goto = NULL;
1867 fold_stmt (tp);
1868 op = get_call_expr_in (t);
1869 if (op)
1871 update_call_expr_flags (op);
1872 notice_special_calls (op);
1874 if (tree_could_throw_p (t))
1875 data->may_throw = true;
1876 break;
1878 case STATEMENT_LIST:
1880 tree_stmt_iterator i = tsi_start (t);
1881 while (!tsi_end_p (i))
1883 t = tsi_stmt (i);
1884 if (IS_EMPTY_STMT (t))
1886 tsi_delink (&i);
1887 continue;
1890 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1892 t = tsi_stmt (i);
1893 if (TREE_CODE (t) == STATEMENT_LIST)
1895 tsi_link_before (&i, t, TSI_SAME_STMT);
1896 tsi_delink (&i);
1898 else
1899 tsi_next (&i);
1902 break;
1903 case ASM_EXPR:
1904 fold_stmt (tp);
1905 data->last_goto = NULL;
1906 break;
1908 default:
1909 data->last_goto = NULL;
1910 break;
1914 static unsigned int
1915 remove_useless_stmts (void)
1917 struct rus_data data;
1919 clear_special_calls ();
1923 memset (&data, 0, sizeof (data));
1924 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1926 while (data.repeat);
1927 return 0;
1931 struct tree_opt_pass pass_remove_useless_stmts =
1933 "useless", /* name */
1934 NULL, /* gate */
1935 remove_useless_stmts, /* execute */
1936 NULL, /* sub */
1937 NULL, /* next */
1938 0, /* static_pass_number */
1939 0, /* tv_id */
1940 PROP_gimple_any, /* properties_required */
1941 0, /* properties_provided */
1942 0, /* properties_destroyed */
1943 0, /* todo_flags_start */
1944 TODO_dump_func, /* todo_flags_finish */
1945 0 /* letter */
1948 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1950 static void
1951 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1953 tree phi;
1955 /* Since this block is no longer reachable, we can just delete all
1956 of its PHI nodes. */
1957 phi = phi_nodes (bb);
1958 while (phi)
1960 tree next = PHI_CHAIN (phi);
1961 remove_phi_node (phi, NULL_TREE, true);
1962 phi = next;
1965 /* Remove edges to BB's successors. */
1966 while (EDGE_COUNT (bb->succs) > 0)
1967 remove_edge (EDGE_SUCC (bb, 0));
1971 /* Remove statements of basic block BB. */
1973 static void
1974 remove_bb (basic_block bb)
1976 block_stmt_iterator i;
1977 #ifdef USE_MAPPED_LOCATION
1978 source_location loc = UNKNOWN_LOCATION;
1979 #else
1980 source_locus loc = 0;
1981 #endif
1983 if (dump_file)
1985 fprintf (dump_file, "Removing basic block %d\n", bb->index);
1986 if (dump_flags & TDF_DETAILS)
1988 dump_bb (bb, dump_file, 0);
1989 fprintf (dump_file, "\n");
1993 if (current_loops)
1995 struct loop *loop = bb->loop_father;
1997 /* If a loop gets removed, clean up the information associated
1998 with it. */
1999 if (loop->latch == bb
2000 || loop->header == bb)
2001 free_numbers_of_iterations_estimates_loop (loop);
2004 /* Remove all the instructions in the block. */
2005 if (bb_stmt_list (bb) != NULL_TREE)
2007 for (i = bsi_start (bb); !bsi_end_p (i);)
2009 tree stmt = bsi_stmt (i);
2010 if (TREE_CODE (stmt) == LABEL_EXPR
2011 && (FORCED_LABEL (LABEL_EXPR_LABEL (stmt))
2012 || DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt))))
2014 basic_block new_bb;
2015 block_stmt_iterator new_bsi;
2017 /* A non-reachable non-local label may still be referenced.
2018 But it no longer needs to carry the extra semantics of
2019 non-locality. */
2020 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
2022 DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)) = 0;
2023 FORCED_LABEL (LABEL_EXPR_LABEL (stmt)) = 1;
2026 new_bb = bb->prev_bb;
2027 new_bsi = bsi_start (new_bb);
2028 bsi_remove (&i, false);
2029 bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
2031 else
2033 /* Release SSA definitions if we are in SSA. Note that we
2034 may be called when not in SSA. For example,
2035 final_cleanup calls this function via
2036 cleanup_tree_cfg. */
2037 if (gimple_in_ssa_p (cfun))
2038 release_defs (stmt);
2040 bsi_remove (&i, true);
2043 /* Don't warn for removed gotos. Gotos are often removed due to
2044 jump threading, thus resulting in bogus warnings. Not great,
2045 since this way we lose warnings for gotos in the original
2046 program that are indeed unreachable. */
2047 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
2049 #ifdef USE_MAPPED_LOCATION
2050 if (EXPR_HAS_LOCATION (stmt))
2051 loc = EXPR_LOCATION (stmt);
2052 #else
2053 source_locus t;
2054 t = EXPR_LOCUS (stmt);
2055 if (t && LOCATION_LINE (*t) > 0)
2056 loc = t;
2057 #endif
2062 /* If requested, give a warning that the first statement in the
2063 block is unreachable. We walk statements backwards in the
2064 loop above, so the last statement we process is the first statement
2065 in the block. */
2066 #ifdef USE_MAPPED_LOCATION
2067 if (loc > BUILTINS_LOCATION && LOCATION_LINE (loc) > 0)
2068 warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
2069 #else
2070 if (loc)
2071 warning (OPT_Wunreachable_code, "%Hwill never be executed", loc);
2072 #endif
2074 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2075 bb->il.tree = NULL;
2079 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2080 predicate VAL, return the edge that will be taken out of the block.
2081 If VAL does not match a unique edge, NULL is returned. */
2083 edge
2084 find_taken_edge (basic_block bb, tree val)
2086 tree stmt;
2088 stmt = last_stmt (bb);
2090 gcc_assert (stmt);
2091 gcc_assert (is_ctrl_stmt (stmt));
2092 gcc_assert (val);
2094 if (! is_gimple_min_invariant (val))
2095 return NULL;
2097 if (TREE_CODE (stmt) == COND_EXPR)
2098 return find_taken_edge_cond_expr (bb, val);
2100 if (TREE_CODE (stmt) == SWITCH_EXPR)
2101 return find_taken_edge_switch_expr (bb, val);
2103 if (computed_goto_p (stmt))
2105 /* Only optimize if the argument is a label, if the argument is
2106 not a label then we can not construct a proper CFG.
2108 It may be the case that we only need to allow the LABEL_REF to
2109 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
2110 appear inside a LABEL_EXPR just to be safe. */
2111 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
2112 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
2113 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
2114 return NULL;
2117 gcc_unreachable ();
2120 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2121 statement, determine which of the outgoing edges will be taken out of the
2122 block. Return NULL if either edge may be taken. */
2124 static edge
2125 find_taken_edge_computed_goto (basic_block bb, tree val)
2127 basic_block dest;
2128 edge e = NULL;
2130 dest = label_to_block (val);
2131 if (dest)
2133 e = find_edge (bb, dest);
2134 gcc_assert (e != NULL);
2137 return e;
2140 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2141 statement, determine which of the two edges will be taken out of the
2142 block. Return NULL if either edge may be taken. */
2144 static edge
2145 find_taken_edge_cond_expr (basic_block bb, tree val)
2147 edge true_edge, false_edge;
2149 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2151 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2152 return (integer_zerop (val) ? false_edge : true_edge);
2155 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2156 statement, determine which edge will be taken out of the block. Return
2157 NULL if any edge may be taken. */
2159 static edge
2160 find_taken_edge_switch_expr (basic_block bb, tree val)
2162 tree switch_expr, taken_case;
2163 basic_block dest_bb;
2164 edge e;
2166 switch_expr = last_stmt (bb);
2167 taken_case = find_case_label_for_value (switch_expr, val);
2168 dest_bb = label_to_block (CASE_LABEL (taken_case));
2170 e = find_edge (bb, dest_bb);
2171 gcc_assert (e);
2172 return e;
2176 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2177 We can make optimal use here of the fact that the case labels are
2178 sorted: We can do a binary search for a case matching VAL. */
2180 static tree
2181 find_case_label_for_value (tree switch_expr, tree val)
2183 tree vec = SWITCH_LABELS (switch_expr);
2184 size_t low, high, n = TREE_VEC_LENGTH (vec);
2185 tree default_case = TREE_VEC_ELT (vec, n - 1);
2187 for (low = -1, high = n - 1; high - low > 1; )
2189 size_t i = (high + low) / 2;
2190 tree t = TREE_VEC_ELT (vec, i);
2191 int cmp;
2193 /* Cache the result of comparing CASE_LOW and val. */
2194 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2196 if (cmp > 0)
2197 high = i;
2198 else
2199 low = i;
2201 if (CASE_HIGH (t) == NULL)
2203 /* A singe-valued case label. */
2204 if (cmp == 0)
2205 return t;
2207 else
2209 /* A case range. We can only handle integer ranges. */
2210 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2211 return t;
2215 return default_case;
2221 /*---------------------------------------------------------------------------
2222 Debugging functions
2223 ---------------------------------------------------------------------------*/
2225 /* Dump tree-specific information of block BB to file OUTF. */
2227 void
2228 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2230 dump_generic_bb (outf, bb, indent, TDF_VOPS|TDF_MEMSYMS);
2234 /* Dump a basic block on stderr. */
2236 void
2237 debug_tree_bb (basic_block bb)
2239 dump_bb (bb, stderr, 0);
2243 /* Dump basic block with index N on stderr. */
2245 basic_block
2246 debug_tree_bb_n (int n)
2248 debug_tree_bb (BASIC_BLOCK (n));
2249 return BASIC_BLOCK (n);
2253 /* Dump the CFG on stderr.
2255 FLAGS are the same used by the tree dumping functions
2256 (see TDF_* in tree-pass.h). */
2258 void
2259 debug_tree_cfg (int flags)
2261 dump_tree_cfg (stderr, flags);
2265 /* Dump the program showing basic block boundaries on the given FILE.
2267 FLAGS are the same used by the tree dumping functions (see TDF_* in
2268 tree.h). */
2270 void
2271 dump_tree_cfg (FILE *file, int flags)
2273 if (flags & TDF_DETAILS)
2275 const char *funcname
2276 = lang_hooks.decl_printable_name (current_function_decl, 2);
2278 fputc ('\n', file);
2279 fprintf (file, ";; Function %s\n\n", funcname);
2280 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2281 n_basic_blocks, n_edges, last_basic_block);
2283 brief_dump_cfg (file);
2284 fprintf (file, "\n");
2287 if (flags & TDF_STATS)
2288 dump_cfg_stats (file);
2290 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2294 /* Dump CFG statistics on FILE. */
2296 void
2297 dump_cfg_stats (FILE *file)
2299 static long max_num_merged_labels = 0;
2300 unsigned long size, total = 0;
2301 long num_edges;
2302 basic_block bb;
2303 const char * const fmt_str = "%-30s%-13s%12s\n";
2304 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2305 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2306 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2307 const char *funcname
2308 = lang_hooks.decl_printable_name (current_function_decl, 2);
2311 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2313 fprintf (file, "---------------------------------------------------------\n");
2314 fprintf (file, fmt_str, "", " Number of ", "Memory");
2315 fprintf (file, fmt_str, "", " instances ", "used ");
2316 fprintf (file, "---------------------------------------------------------\n");
2318 size = n_basic_blocks * sizeof (struct basic_block_def);
2319 total += size;
2320 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2321 SCALE (size), LABEL (size));
2323 num_edges = 0;
2324 FOR_EACH_BB (bb)
2325 num_edges += EDGE_COUNT (bb->succs);
2326 size = num_edges * sizeof (struct edge_def);
2327 total += size;
2328 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2330 fprintf (file, "---------------------------------------------------------\n");
2331 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2332 LABEL (total));
2333 fprintf (file, "---------------------------------------------------------\n");
2334 fprintf (file, "\n");
2336 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2337 max_num_merged_labels = cfg_stats.num_merged_labels;
2339 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2340 cfg_stats.num_merged_labels, max_num_merged_labels);
2342 fprintf (file, "\n");
2346 /* Dump CFG statistics on stderr. Keep extern so that it's always
2347 linked in the final executable. */
2349 void
2350 debug_cfg_stats (void)
2352 dump_cfg_stats (stderr);
2356 /* Dump the flowgraph to a .vcg FILE. */
2358 static void
2359 tree_cfg2vcg (FILE *file)
2361 edge e;
2362 edge_iterator ei;
2363 basic_block bb;
2364 const char *funcname
2365 = lang_hooks.decl_printable_name (current_function_decl, 2);
2367 /* Write the file header. */
2368 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2369 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2370 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2372 /* Write blocks and edges. */
2373 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2375 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2376 e->dest->index);
2378 if (e->flags & EDGE_FAKE)
2379 fprintf (file, " linestyle: dotted priority: 10");
2380 else
2381 fprintf (file, " linestyle: solid priority: 100");
2383 fprintf (file, " }\n");
2385 fputc ('\n', file);
2387 FOR_EACH_BB (bb)
2389 enum tree_code head_code, end_code;
2390 const char *head_name, *end_name;
2391 int head_line = 0;
2392 int end_line = 0;
2393 tree first = first_stmt (bb);
2394 tree last = last_stmt (bb);
2396 if (first)
2398 head_code = TREE_CODE (first);
2399 head_name = tree_code_name[head_code];
2400 head_line = get_lineno (first);
2402 else
2403 head_name = "no-statement";
2405 if (last)
2407 end_code = TREE_CODE (last);
2408 end_name = tree_code_name[end_code];
2409 end_line = get_lineno (last);
2411 else
2412 end_name = "no-statement";
2414 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2415 bb->index, bb->index, head_name, head_line, end_name,
2416 end_line);
2418 FOR_EACH_EDGE (e, ei, bb->succs)
2420 if (e->dest == EXIT_BLOCK_PTR)
2421 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2422 else
2423 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2425 if (e->flags & EDGE_FAKE)
2426 fprintf (file, " priority: 10 linestyle: dotted");
2427 else
2428 fprintf (file, " priority: 100 linestyle: solid");
2430 fprintf (file, " }\n");
2433 if (bb->next_bb != EXIT_BLOCK_PTR)
2434 fputc ('\n', file);
2437 fputs ("}\n\n", file);
2442 /*---------------------------------------------------------------------------
2443 Miscellaneous helpers
2444 ---------------------------------------------------------------------------*/
2446 /* Return true if T represents a stmt that always transfers control. */
2448 bool
2449 is_ctrl_stmt (const_tree t)
2451 return (TREE_CODE (t) == COND_EXPR
2452 || TREE_CODE (t) == SWITCH_EXPR
2453 || TREE_CODE (t) == GOTO_EXPR
2454 || TREE_CODE (t) == RETURN_EXPR
2455 || TREE_CODE (t) == RESX_EXPR);
2459 /* Return true if T is a statement that may alter the flow of control
2460 (e.g., a call to a non-returning function). */
2462 bool
2463 is_ctrl_altering_stmt (const_tree t)
2465 const_tree call;
2467 gcc_assert (t);
2468 call = get_call_expr_in (CONST_CAST_TREE (t));
2469 if (call)
2471 /* A non-pure/const CALL_EXPR alters flow control if the current
2472 function has nonlocal labels. */
2473 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2474 return true;
2476 /* A CALL_EXPR also alters control flow if it does not return. */
2477 if (call_expr_flags (call) & ECF_NORETURN)
2478 return true;
2481 /* OpenMP directives alter control flow. */
2482 if (OMP_DIRECTIVE_P (t))
2483 return true;
2485 /* If a statement can throw, it alters control flow. */
2486 return tree_can_throw_internal (t);
2490 /* Return true if T is a computed goto. */
2492 bool
2493 computed_goto_p (const_tree t)
2495 return (TREE_CODE (t) == GOTO_EXPR
2496 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2500 /* Return true if T is a simple local goto. */
2502 bool
2503 simple_goto_p (const_tree t)
2505 return (TREE_CODE (t) == GOTO_EXPR
2506 && TREE_CODE (GOTO_DESTINATION (t)) == LABEL_DECL);
2510 /* Return true if T can make an abnormal transfer of control flow.
2511 Transfers of control flow associated with EH are excluded. */
2513 bool
2514 tree_can_make_abnormal_goto (const_tree t)
2516 if (computed_goto_p (t))
2517 return true;
2518 if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
2519 t = GIMPLE_STMT_OPERAND (t, 1);
2520 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2521 t = TREE_OPERAND (t, 0);
2522 if (TREE_CODE (t) == CALL_EXPR)
2523 return TREE_SIDE_EFFECTS (t) && current_function_has_nonlocal_label;
2524 return false;
2528 /* Return true if T should start a new basic block. PREV_T is the
2529 statement preceding T. It is used when T is a label or a case label.
2530 Labels should only start a new basic block if their previous statement
2531 wasn't a label. Otherwise, sequence of labels would generate
2532 unnecessary basic blocks that only contain a single label. */
2534 static inline bool
2535 stmt_starts_bb_p (const_tree t, const_tree prev_t)
2537 if (t == NULL_TREE)
2538 return false;
2540 /* LABEL_EXPRs start a new basic block only if the preceding
2541 statement wasn't a label of the same type. This prevents the
2542 creation of consecutive blocks that have nothing but a single
2543 label. */
2544 if (TREE_CODE (t) == LABEL_EXPR)
2546 /* Nonlocal and computed GOTO targets always start a new block. */
2547 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2548 || FORCED_LABEL (LABEL_EXPR_LABEL (t)))
2549 return true;
2551 if (prev_t && TREE_CODE (prev_t) == LABEL_EXPR)
2553 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2554 return true;
2556 cfg_stats.num_merged_labels++;
2557 return false;
2559 else
2560 return true;
2563 return false;
2567 /* Return true if T should end a basic block. */
2569 bool
2570 stmt_ends_bb_p (const_tree t)
2572 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2575 /* Remove block annotations and other datastructures. */
2577 void
2578 delete_tree_cfg_annotations (void)
2580 basic_block bb;
2581 block_stmt_iterator bsi;
2583 /* Remove annotations from every tree in the function. */
2584 FOR_EACH_BB (bb)
2585 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2587 tree stmt = bsi_stmt (bsi);
2588 ggc_free (stmt->base.ann);
2589 stmt->base.ann = NULL;
2591 label_to_block_map = NULL;
2595 /* Return the first statement in basic block BB. */
2597 tree
2598 first_stmt (basic_block bb)
2600 block_stmt_iterator i = bsi_start (bb);
2601 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2604 /* Return the last statement in basic block BB. */
2606 tree
2607 last_stmt (basic_block bb)
2609 block_stmt_iterator b = bsi_last (bb);
2610 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2613 /* Return the last statement of an otherwise empty block. Return NULL
2614 if the block is totally empty, or if it contains more than one
2615 statement. */
2617 tree
2618 last_and_only_stmt (basic_block bb)
2620 block_stmt_iterator i = bsi_last (bb);
2621 tree last, prev;
2623 if (bsi_end_p (i))
2624 return NULL_TREE;
2626 last = bsi_stmt (i);
2627 bsi_prev (&i);
2628 if (bsi_end_p (i))
2629 return last;
2631 /* Empty statements should no longer appear in the instruction stream.
2632 Everything that might have appeared before should be deleted by
2633 remove_useless_stmts, and the optimizers should just bsi_remove
2634 instead of smashing with build_empty_stmt.
2636 Thus the only thing that should appear here in a block containing
2637 one executable statement is a label. */
2638 prev = bsi_stmt (i);
2639 if (TREE_CODE (prev) == LABEL_EXPR)
2640 return last;
2641 else
2642 return NULL_TREE;
2646 /* Mark BB as the basic block holding statement T. */
2648 void
2649 set_bb_for_stmt (tree t, basic_block bb)
2651 if (TREE_CODE (t) == PHI_NODE)
2652 PHI_BB (t) = bb;
2653 else if (TREE_CODE (t) == STATEMENT_LIST)
2655 tree_stmt_iterator i;
2656 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2657 set_bb_for_stmt (tsi_stmt (i), bb);
2659 else
2661 stmt_ann_t ann = get_stmt_ann (t);
2662 ann->bb = bb;
2664 /* If the statement is a label, add the label to block-to-labels map
2665 so that we can speed up edge creation for GOTO_EXPRs. */
2666 if (TREE_CODE (t) == LABEL_EXPR)
2668 int uid;
2670 t = LABEL_EXPR_LABEL (t);
2671 uid = LABEL_DECL_UID (t);
2672 if (uid == -1)
2674 unsigned old_len = VEC_length (basic_block, label_to_block_map);
2675 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2676 if (old_len <= (unsigned) uid)
2678 unsigned new_len = 3 * uid / 2;
2680 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
2681 new_len);
2684 else
2685 /* We're moving an existing label. Make sure that we've
2686 removed it from the old block. */
2687 gcc_assert (!bb
2688 || !VEC_index (basic_block, label_to_block_map, uid));
2689 VEC_replace (basic_block, label_to_block_map, uid, bb);
2694 /* Faster version of set_bb_for_stmt that assume that statement is being moved
2695 from one basic block to another.
2696 For BB splitting we can run into quadratic case, so performance is quite
2697 important and knowing that the tables are big enough, change_bb_for_stmt
2698 can inline as leaf function. */
2699 static inline void
2700 change_bb_for_stmt (tree t, basic_block bb)
2702 get_stmt_ann (t)->bb = bb;
2703 if (TREE_CODE (t) == LABEL_EXPR)
2704 VEC_replace (basic_block, label_to_block_map,
2705 LABEL_DECL_UID (LABEL_EXPR_LABEL (t)), bb);
2708 /* Finds iterator for STMT. */
2710 extern block_stmt_iterator
2711 bsi_for_stmt (tree stmt)
2713 block_stmt_iterator bsi;
2715 for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
2716 if (bsi_stmt (bsi) == stmt)
2717 return bsi;
2719 gcc_unreachable ();
2722 /* Mark statement T as modified, and update it. */
2723 static inline void
2724 update_modified_stmts (tree t)
2726 if (!ssa_operands_active ())
2727 return;
2728 if (TREE_CODE (t) == STATEMENT_LIST)
2730 tree_stmt_iterator i;
2731 tree stmt;
2732 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2734 stmt = tsi_stmt (i);
2735 update_stmt_if_modified (stmt);
2738 else
2739 update_stmt_if_modified (t);
2742 /* Insert statement (or statement list) T before the statement
2743 pointed-to by iterator I. M specifies how to update iterator I
2744 after insertion (see enum bsi_iterator_update). */
2746 void
2747 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2749 set_bb_for_stmt (t, i->bb);
2750 update_modified_stmts (t);
2751 tsi_link_before (&i->tsi, t, m);
2755 /* Insert statement (or statement list) T after the statement
2756 pointed-to by iterator I. M specifies how to update iterator I
2757 after insertion (see enum bsi_iterator_update). */
2759 void
2760 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2762 set_bb_for_stmt (t, i->bb);
2763 update_modified_stmts (t);
2764 tsi_link_after (&i->tsi, t, m);
2768 /* Remove the statement pointed to by iterator I. The iterator is updated
2769 to the next statement.
2771 When REMOVE_EH_INFO is true we remove the statement pointed to by
2772 iterator I from the EH tables. Otherwise we do not modify the EH
2773 tables.
2775 Generally, REMOVE_EH_INFO should be true when the statement is going to
2776 be removed from the IL and not reinserted elsewhere. */
2778 void
2779 bsi_remove (block_stmt_iterator *i, bool remove_eh_info)
2781 tree t = bsi_stmt (*i);
2782 set_bb_for_stmt (t, NULL);
2783 delink_stmt_imm_use (t);
2784 tsi_delink (&i->tsi);
2785 mark_stmt_modified (t);
2786 if (remove_eh_info)
2788 remove_stmt_from_eh_region (t);
2789 gimple_remove_stmt_histograms (cfun, t);
2794 /* Move the statement at FROM so it comes right after the statement at TO. */
2796 void
2797 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
2799 tree stmt = bsi_stmt (*from);
2800 bsi_remove (from, false);
2801 /* We must have BSI_NEW_STMT here, as bsi_move_after is sometimes used to
2802 move statements to an empty block. */
2803 bsi_insert_after (to, stmt, BSI_NEW_STMT);
2807 /* Move the statement at FROM so it comes right before the statement at TO. */
2809 void
2810 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
2812 tree stmt = bsi_stmt (*from);
2813 bsi_remove (from, false);
2814 /* For consistency with bsi_move_after, it might be better to have
2815 BSI_NEW_STMT here; however, that breaks several places that expect
2816 that TO does not change. */
2817 bsi_insert_before (to, stmt, BSI_SAME_STMT);
2821 /* Move the statement at FROM to the end of basic block BB. */
2823 void
2824 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
2826 block_stmt_iterator last = bsi_last (bb);
2828 /* Have to check bsi_end_p because it could be an empty block. */
2829 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
2830 bsi_move_before (from, &last);
2831 else
2832 bsi_move_after (from, &last);
2836 /* Replace the contents of the statement pointed to by iterator BSI
2837 with STMT. If UPDATE_EH_INFO is true, the exception handling
2838 information of the original statement is moved to the new statement. */
2840 void
2841 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool update_eh_info)
2843 int eh_region;
2844 tree orig_stmt = bsi_stmt (*bsi);
2846 if (stmt == orig_stmt)
2847 return;
2848 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
2849 set_bb_for_stmt (stmt, bsi->bb);
2851 /* Preserve EH region information from the original statement, if
2852 requested by the caller. */
2853 if (update_eh_info)
2855 eh_region = lookup_stmt_eh_region (orig_stmt);
2856 if (eh_region >= 0)
2858 remove_stmt_from_eh_region (orig_stmt);
2859 add_stmt_to_eh_region (stmt, eh_region);
2863 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
2864 gimple_remove_stmt_histograms (cfun, orig_stmt);
2865 delink_stmt_imm_use (orig_stmt);
2866 *bsi_stmt_ptr (*bsi) = stmt;
2867 mark_stmt_modified (stmt);
2868 update_modified_stmts (stmt);
2872 /* Insert the statement pointed-to by BSI into edge E. Every attempt
2873 is made to place the statement in an existing basic block, but
2874 sometimes that isn't possible. When it isn't possible, the edge is
2875 split and the statement is added to the new block.
2877 In all cases, the returned *BSI points to the correct location. The
2878 return value is true if insertion should be done after the location,
2879 or false if it should be done before the location. If new basic block
2880 has to be created, it is stored in *NEW_BB. */
2882 static bool
2883 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
2884 basic_block *new_bb)
2886 basic_block dest, src;
2887 tree tmp;
2889 dest = e->dest;
2890 restart:
2892 /* If the destination has one predecessor which has no PHI nodes,
2893 insert there. Except for the exit block.
2895 The requirement for no PHI nodes could be relaxed. Basically we
2896 would have to examine the PHIs to prove that none of them used
2897 the value set by the statement we want to insert on E. That
2898 hardly seems worth the effort. */
2899 if (single_pred_p (dest)
2900 && ! phi_nodes (dest)
2901 && dest != EXIT_BLOCK_PTR)
2903 *bsi = bsi_start (dest);
2904 if (bsi_end_p (*bsi))
2905 return true;
2907 /* Make sure we insert after any leading labels. */
2908 tmp = bsi_stmt (*bsi);
2909 while (TREE_CODE (tmp) == LABEL_EXPR)
2911 bsi_next (bsi);
2912 if (bsi_end_p (*bsi))
2913 break;
2914 tmp = bsi_stmt (*bsi);
2917 if (bsi_end_p (*bsi))
2919 *bsi = bsi_last (dest);
2920 return true;
2922 else
2923 return false;
2926 /* If the source has one successor, the edge is not abnormal and
2927 the last statement does not end a basic block, insert there.
2928 Except for the entry block. */
2929 src = e->src;
2930 if ((e->flags & EDGE_ABNORMAL) == 0
2931 && single_succ_p (src)
2932 && src != ENTRY_BLOCK_PTR)
2934 *bsi = bsi_last (src);
2935 if (bsi_end_p (*bsi))
2936 return true;
2938 tmp = bsi_stmt (*bsi);
2939 if (!stmt_ends_bb_p (tmp))
2940 return true;
2942 /* Insert code just before returning the value. We may need to decompose
2943 the return in the case it contains non-trivial operand. */
2944 if (TREE_CODE (tmp) == RETURN_EXPR)
2946 tree op = TREE_OPERAND (tmp, 0);
2947 if (op && !is_gimple_val (op))
2949 gcc_assert (TREE_CODE (op) == GIMPLE_MODIFY_STMT);
2950 bsi_insert_before (bsi, op, BSI_NEW_STMT);
2951 TREE_OPERAND (tmp, 0) = GIMPLE_STMT_OPERAND (op, 0);
2953 bsi_prev (bsi);
2954 return true;
2958 /* Otherwise, create a new basic block, and split this edge. */
2959 dest = split_edge (e);
2960 if (new_bb)
2961 *new_bb = dest;
2962 e = single_pred_edge (dest);
2963 goto restart;
2967 /* This routine will commit all pending edge insertions, creating any new
2968 basic blocks which are necessary. */
2970 void
2971 bsi_commit_edge_inserts (void)
2973 basic_block bb;
2974 edge e;
2975 edge_iterator ei;
2977 bsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
2979 FOR_EACH_BB (bb)
2980 FOR_EACH_EDGE (e, ei, bb->succs)
2981 bsi_commit_one_edge_insert (e, NULL);
2985 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
2986 to this block, otherwise set it to NULL. */
2988 void
2989 bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
2991 if (new_bb)
2992 *new_bb = NULL;
2993 if (PENDING_STMT (e))
2995 block_stmt_iterator bsi;
2996 tree stmt = PENDING_STMT (e);
2998 PENDING_STMT (e) = NULL_TREE;
3000 if (tree_find_edge_insert_loc (e, &bsi, new_bb))
3001 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3002 else
3003 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3008 /* Add STMT to the pending list of edge E. No actual insertion is
3009 made until a call to bsi_commit_edge_inserts () is made. */
3011 void
3012 bsi_insert_on_edge (edge e, tree stmt)
3014 append_to_statement_list (stmt, &PENDING_STMT (e));
3017 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new
3018 block has to be created, it is returned. */
3020 basic_block
3021 bsi_insert_on_edge_immediate (edge e, tree stmt)
3023 block_stmt_iterator bsi;
3024 basic_block new_bb = NULL;
3026 gcc_assert (!PENDING_STMT (e));
3028 if (tree_find_edge_insert_loc (e, &bsi, &new_bb))
3029 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3030 else
3031 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3033 return new_bb;
3036 /*---------------------------------------------------------------------------
3037 Tree specific functions for CFG manipulation
3038 ---------------------------------------------------------------------------*/
3040 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
3042 static void
3043 reinstall_phi_args (edge new_edge, edge old_edge)
3045 tree var, phi;
3047 if (!PENDING_STMT (old_edge))
3048 return;
3050 for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest);
3051 var && phi;
3052 var = TREE_CHAIN (var), phi = PHI_CHAIN (phi))
3054 tree result = TREE_PURPOSE (var);
3055 tree arg = TREE_VALUE (var);
3057 gcc_assert (result == PHI_RESULT (phi));
3059 add_phi_arg (phi, arg, new_edge);
3062 PENDING_STMT (old_edge) = NULL;
3065 /* Returns the basic block after which the new basic block created
3066 by splitting edge EDGE_IN should be placed. Tries to keep the new block
3067 near its "logical" location. This is of most help to humans looking
3068 at debugging dumps. */
3070 static basic_block
3071 split_edge_bb_loc (edge edge_in)
3073 basic_block dest = edge_in->dest;
3075 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
3076 return edge_in->src;
3077 else
3078 return dest->prev_bb;
3081 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3082 Abort on abnormal edges. */
3084 static basic_block
3085 tree_split_edge (edge edge_in)
3087 basic_block new_bb, after_bb, dest;
3088 edge new_edge, e;
3090 /* Abnormal edges cannot be split. */
3091 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
3093 dest = edge_in->dest;
3095 after_bb = split_edge_bb_loc (edge_in);
3097 new_bb = create_empty_bb (after_bb);
3098 new_bb->frequency = EDGE_FREQUENCY (edge_in);
3099 new_bb->count = edge_in->count;
3100 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
3101 new_edge->probability = REG_BR_PROB_BASE;
3102 new_edge->count = edge_in->count;
3104 e = redirect_edge_and_branch (edge_in, new_bb);
3105 gcc_assert (e == edge_in);
3106 reinstall_phi_args (new_edge, e);
3108 return new_bb;
3111 /* Callback for walk_tree, check that all elements with address taken are
3112 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3113 inside a PHI node. */
3115 static tree
3116 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3118 tree t = *tp, x;
3119 bool in_phi = (data != NULL);
3121 if (TYPE_P (t))
3122 *walk_subtrees = 0;
3124 /* Check operand N for being valid GIMPLE and give error MSG if not. */
3125 #define CHECK_OP(N, MSG) \
3126 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
3127 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3129 switch (TREE_CODE (t))
3131 case SSA_NAME:
3132 if (SSA_NAME_IN_FREE_LIST (t))
3134 error ("SSA name in freelist but still referenced");
3135 return *tp;
3137 break;
3139 case ASSERT_EXPR:
3140 x = fold (ASSERT_EXPR_COND (t));
3141 if (x == boolean_false_node)
3143 error ("ASSERT_EXPR with an always-false condition");
3144 return *tp;
3146 break;
3148 case MODIFY_EXPR:
3149 gcc_unreachable ();
3151 case GIMPLE_MODIFY_STMT:
3152 x = GIMPLE_STMT_OPERAND (t, 0);
3153 if (TREE_CODE (x) == BIT_FIELD_REF
3154 && is_gimple_reg (TREE_OPERAND (x, 0)))
3156 error ("GIMPLE register modified with BIT_FIELD_REF");
3157 return t;
3159 break;
3161 case ADDR_EXPR:
3163 bool old_invariant;
3164 bool old_constant;
3165 bool old_side_effects;
3166 bool new_invariant;
3167 bool new_constant;
3168 bool new_side_effects;
3170 /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
3171 dead PHIs that take the address of something. But if the PHI
3172 result is dead, the fact that it takes the address of anything
3173 is irrelevant. Because we can not tell from here if a PHI result
3174 is dead, we just skip this check for PHIs altogether. This means
3175 we may be missing "valid" checks, but what can you do?
3176 This was PR19217. */
3177 if (in_phi)
3178 break;
3180 old_invariant = TREE_INVARIANT (t);
3181 old_constant = TREE_CONSTANT (t);
3182 old_side_effects = TREE_SIDE_EFFECTS (t);
3184 recompute_tree_invariant_for_addr_expr (t);
3185 new_invariant = TREE_INVARIANT (t);
3186 new_side_effects = TREE_SIDE_EFFECTS (t);
3187 new_constant = TREE_CONSTANT (t);
3189 if (old_invariant != new_invariant)
3191 error ("invariant not recomputed when ADDR_EXPR changed");
3192 return t;
3195 if (old_constant != new_constant)
3197 error ("constant not recomputed when ADDR_EXPR changed");
3198 return t;
3200 if (old_side_effects != new_side_effects)
3202 error ("side effects not recomputed when ADDR_EXPR changed");
3203 return t;
3206 /* Skip any references (they will be checked when we recurse down the
3207 tree) and ensure that any variable used as a prefix is marked
3208 addressable. */
3209 for (x = TREE_OPERAND (t, 0);
3210 handled_component_p (x);
3211 x = TREE_OPERAND (x, 0))
3214 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3215 return NULL;
3216 if (!TREE_ADDRESSABLE (x))
3218 error ("address taken, but ADDRESSABLE bit not set");
3219 return x;
3222 /* Stop recursing and verifying invariant ADDR_EXPRs, they tend
3223 to become arbitrary complicated. */
3224 if (is_gimple_min_invariant (t))
3225 *walk_subtrees = 0;
3226 break;
3229 case COND_EXPR:
3230 x = COND_EXPR_COND (t);
3231 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
3233 error ("non-integral used in condition");
3234 return x;
3236 if (!is_gimple_condexpr (x))
3238 error ("invalid conditional operand");
3239 return x;
3241 break;
3243 case NOP_EXPR:
3244 case CONVERT_EXPR:
3245 case FIX_TRUNC_EXPR:
3246 case FLOAT_EXPR:
3247 case NEGATE_EXPR:
3248 case ABS_EXPR:
3249 case BIT_NOT_EXPR:
3250 case NON_LVALUE_EXPR:
3251 case TRUTH_NOT_EXPR:
3252 CHECK_OP (0, "invalid operand to unary operator");
3253 break;
3255 case REALPART_EXPR:
3256 case IMAGPART_EXPR:
3257 case COMPONENT_REF:
3258 case ARRAY_REF:
3259 case ARRAY_RANGE_REF:
3260 case BIT_FIELD_REF:
3261 case VIEW_CONVERT_EXPR:
3262 /* We have a nest of references. Verify that each of the operands
3263 that determine where to reference is either a constant or a variable,
3264 verify that the base is valid, and then show we've already checked
3265 the subtrees. */
3266 while (handled_component_p (t))
3268 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3269 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
3270 else if (TREE_CODE (t) == ARRAY_REF
3271 || TREE_CODE (t) == ARRAY_RANGE_REF)
3273 CHECK_OP (1, "invalid array index");
3274 if (TREE_OPERAND (t, 2))
3275 CHECK_OP (2, "invalid array lower bound");
3276 if (TREE_OPERAND (t, 3))
3277 CHECK_OP (3, "invalid array stride");
3279 else if (TREE_CODE (t) == BIT_FIELD_REF)
3281 CHECK_OP (1, "invalid operand to BIT_FIELD_REF");
3282 CHECK_OP (2, "invalid operand to BIT_FIELD_REF");
3285 t = TREE_OPERAND (t, 0);
3288 if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t))
3290 error ("invalid reference prefix");
3291 return t;
3293 *walk_subtrees = 0;
3294 break;
3295 case PLUS_EXPR:
3296 case MINUS_EXPR:
3297 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
3298 POINTER_PLUS_EXPR. */
3299 if (POINTER_TYPE_P (TREE_TYPE (t)))
3301 error ("invalid operand to plus/minus, type is a pointer");
3302 return t;
3304 CHECK_OP (0, "invalid operand to binary operator");
3305 CHECK_OP (1, "invalid operand to binary operator");
3306 break;
3308 case POINTER_PLUS_EXPR:
3309 /* Check to make sure the first operand is a pointer or reference type. */
3310 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3312 error ("invalid operand to pointer plus, first operand is not a pointer");
3313 return t;
3315 /* Check to make sure the second operand is an integer with type of
3316 sizetype. */
3317 if (!useless_type_conversion_p (sizetype,
3318 TREE_TYPE (TREE_OPERAND (t, 1))))
3320 error ("invalid operand to pointer plus, second operand is not an "
3321 "integer with type of sizetype.");
3322 return t;
3324 /* FALLTHROUGH */
3325 case LT_EXPR:
3326 case LE_EXPR:
3327 case GT_EXPR:
3328 case GE_EXPR:
3329 case EQ_EXPR:
3330 case NE_EXPR:
3331 case UNORDERED_EXPR:
3332 case ORDERED_EXPR:
3333 case UNLT_EXPR:
3334 case UNLE_EXPR:
3335 case UNGT_EXPR:
3336 case UNGE_EXPR:
3337 case UNEQ_EXPR:
3338 case LTGT_EXPR:
3339 case MULT_EXPR:
3340 case TRUNC_DIV_EXPR:
3341 case CEIL_DIV_EXPR:
3342 case FLOOR_DIV_EXPR:
3343 case ROUND_DIV_EXPR:
3344 case TRUNC_MOD_EXPR:
3345 case CEIL_MOD_EXPR:
3346 case FLOOR_MOD_EXPR:
3347 case ROUND_MOD_EXPR:
3348 case RDIV_EXPR:
3349 case EXACT_DIV_EXPR:
3350 case MIN_EXPR:
3351 case MAX_EXPR:
3352 case LSHIFT_EXPR:
3353 case RSHIFT_EXPR:
3354 case LROTATE_EXPR:
3355 case RROTATE_EXPR:
3356 case BIT_IOR_EXPR:
3357 case BIT_XOR_EXPR:
3358 case BIT_AND_EXPR:
3359 CHECK_OP (0, "invalid operand to binary operator");
3360 CHECK_OP (1, "invalid operand to binary operator");
3361 break;
3363 case CONSTRUCTOR:
3364 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3365 *walk_subtrees = 0;
3366 break;
3368 default:
3369 break;
3371 return NULL;
3373 #undef CHECK_OP
3376 /* Verifies if EXPR is a valid GIMPLE unary expression. Returns true
3377 if there is an error, otherwise false. */
3379 static bool
3380 verify_gimple_unary_expr (const_tree expr)
3382 tree op = TREE_OPERAND (expr, 0);
3383 tree type = TREE_TYPE (expr);
3385 if (!is_gimple_val (op))
3387 error ("invalid operand in unary expression");
3388 return true;
3391 /* For general unary expressions we have the operations type
3392 as the effective type the operation is carried out on. So all
3393 we need to require is that the operand is trivially convertible
3394 to that type. */
3395 if (!useless_type_conversion_p (type, TREE_TYPE (op)))
3397 error ("type mismatch in unary expression");
3398 debug_generic_expr (type);
3399 debug_generic_expr (TREE_TYPE (op));
3400 return true;
3403 return false;
3406 /* Verifies if EXPR is a valid GIMPLE binary expression. Returns true
3407 if there is an error, otherwise false. */
3409 static bool
3410 verify_gimple_binary_expr (const_tree expr)
3412 tree op0 = TREE_OPERAND (expr, 0);
3413 tree op1 = TREE_OPERAND (expr, 1);
3414 tree type = TREE_TYPE (expr);
3416 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3418 error ("invalid operands in binary expression");
3419 return true;
3422 /* For general binary expressions we have the operations type
3423 as the effective type the operation is carried out on. So all
3424 we need to require is that both operands are trivially convertible
3425 to that type. */
3426 if (!useless_type_conversion_p (type, TREE_TYPE (op0))
3427 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
3429 error ("type mismatch in binary expression");
3430 debug_generic_stmt (type);
3431 debug_generic_stmt (TREE_TYPE (op0));
3432 debug_generic_stmt (TREE_TYPE (op1));
3433 return true;
3436 return false;
3439 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
3440 Returns true if there is an error, otherwise false. */
3442 static bool
3443 verify_gimple_min_lval (tree expr)
3445 tree op;
3447 if (is_gimple_id (expr))
3448 return false;
3450 if (TREE_CODE (expr) != INDIRECT_REF
3451 && TREE_CODE (expr) != ALIGN_INDIRECT_REF
3452 && TREE_CODE (expr) != MISALIGNED_INDIRECT_REF)
3454 error ("invalid expression for min lvalue");
3455 return true;
3458 op = TREE_OPERAND (expr, 0);
3459 if (!is_gimple_val (op))
3461 error ("invalid operand in indirect reference");
3462 debug_generic_stmt (op);
3463 return true;
3465 if (!useless_type_conversion_p (TREE_TYPE (expr),
3466 TREE_TYPE (TREE_TYPE (op))))
3468 error ("type mismatch in indirect reference");
3469 debug_generic_stmt (TREE_TYPE (expr));
3470 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3471 return true;
3474 return false;
3477 /* Verify if EXPR is a valid GIMPLE reference expression. Returns true
3478 if there is an error, otherwise false. */
3480 static bool
3481 verify_gimple_reference (tree expr)
3483 while (handled_component_p (expr))
3485 tree op = TREE_OPERAND (expr, 0);
3487 if (TREE_CODE (expr) == ARRAY_REF
3488 || TREE_CODE (expr) == ARRAY_RANGE_REF)
3490 if (!is_gimple_val (TREE_OPERAND (expr, 1))
3491 || (TREE_OPERAND (expr, 2)
3492 && !is_gimple_val (TREE_OPERAND (expr, 2)))
3493 || (TREE_OPERAND (expr, 3)
3494 && !is_gimple_val (TREE_OPERAND (expr, 3))))
3496 error ("invalid operands to array reference");
3497 debug_generic_stmt (expr);
3498 return true;
3502 /* Verify if the reference array element types are compatible. */
3503 if (TREE_CODE (expr) == ARRAY_REF
3504 && !useless_type_conversion_p (TREE_TYPE (expr),
3505 TREE_TYPE (TREE_TYPE (op))))
3507 error ("type mismatch in array reference");
3508 debug_generic_stmt (TREE_TYPE (expr));
3509 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3510 return true;
3512 if (TREE_CODE (expr) == ARRAY_RANGE_REF
3513 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
3514 TREE_TYPE (TREE_TYPE (op))))
3516 error ("type mismatch in array range reference");
3517 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
3518 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3519 return true;
3522 if ((TREE_CODE (expr) == REALPART_EXPR
3523 || TREE_CODE (expr) == IMAGPART_EXPR)
3524 && !useless_type_conversion_p (TREE_TYPE (expr),
3525 TREE_TYPE (TREE_TYPE (op))))
3527 error ("type mismatch in real/imagpart reference");
3528 debug_generic_stmt (TREE_TYPE (expr));
3529 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3530 return true;
3533 if (TREE_CODE (expr) == COMPONENT_REF
3534 && !useless_type_conversion_p (TREE_TYPE (expr),
3535 TREE_TYPE (TREE_OPERAND (expr, 1))))
3537 error ("type mismatch in component reference");
3538 debug_generic_stmt (TREE_TYPE (expr));
3539 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3540 return true;
3543 /* For VIEW_CONVERT_EXPRs which are allowed here, too, there
3544 is nothing to verify. Gross mismatches at most invoke
3545 undefined behavior. */
3547 expr = op;
3550 return verify_gimple_min_lval (expr);
3553 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3554 list of pointer-to types that is trivially convertible to DEST. */
3556 static bool
3557 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3559 tree src;
3561 if (!TYPE_POINTER_TO (src_obj))
3562 return true;
3564 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3565 if (useless_type_conversion_p (dest, src))
3566 return true;
3568 return false;
3571 /* Verify the GIMPLE expression EXPR. Returns true if there is an
3572 error, otherwise false. */
3574 static bool
3575 verify_gimple_expr (tree expr)
3577 tree type = TREE_TYPE (expr);
3579 if (is_gimple_val (expr))
3580 return false;
3582 /* Special codes we cannot handle via their class. */
3583 switch (TREE_CODE (expr))
3585 case NOP_EXPR:
3586 case CONVERT_EXPR:
3588 tree op = TREE_OPERAND (expr, 0);
3589 if (!is_gimple_val (op))
3591 error ("invalid operand in conversion");
3592 return true;
3595 /* Allow conversions between integral types and between
3596 pointer types. */
3597 if ((INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (op)))
3598 || (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (op))))
3599 return false;
3601 /* Allow conversions between integral types and pointers only if
3602 there is no sign or zero extension involved. */
3603 if (((POINTER_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (op)))
3604 || (POINTER_TYPE_P (TREE_TYPE (op)) && INTEGRAL_TYPE_P (type)))
3605 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op)))
3606 return false;
3608 /* Allow conversion from integer to offset type and vice versa. */
3609 if ((TREE_CODE (type) == OFFSET_TYPE
3610 && TREE_CODE (TREE_TYPE (op)) == INTEGER_TYPE)
3611 || (TREE_CODE (type) == INTEGER_TYPE
3612 && TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE))
3613 return false;
3615 /* Otherwise assert we are converting between types of the
3616 same kind. */
3617 if (TREE_CODE (type) != TREE_CODE (TREE_TYPE (op)))
3619 error ("invalid types in nop conversion");
3620 debug_generic_expr (type);
3621 debug_generic_expr (TREE_TYPE (op));
3622 return true;
3625 return false;
3628 case FLOAT_EXPR:
3630 tree op = TREE_OPERAND (expr, 0);
3631 if (!is_gimple_val (op))
3633 error ("invalid operand in int to float conversion");
3634 return true;
3636 if (!INTEGRAL_TYPE_P (TREE_TYPE (op))
3637 || !SCALAR_FLOAT_TYPE_P (type))
3639 error ("invalid types in conversion to floating point");
3640 debug_generic_expr (type);
3641 debug_generic_expr (TREE_TYPE (op));
3642 return true;
3644 return false;
3647 case FIX_TRUNC_EXPR:
3649 tree op = TREE_OPERAND (expr, 0);
3650 if (!is_gimple_val (op))
3652 error ("invalid operand in float to int conversion");
3653 return true;
3655 if (!INTEGRAL_TYPE_P (type)
3656 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
3658 error ("invalid types in conversion to integer");
3659 debug_generic_expr (type);
3660 debug_generic_expr (TREE_TYPE (op));
3661 return true;
3663 return false;
3666 case COMPLEX_EXPR:
3668 tree op0 = TREE_OPERAND (expr, 0);
3669 tree op1 = TREE_OPERAND (expr, 1);
3670 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3672 error ("invalid operands in complex expression");
3673 return true;
3675 if (!TREE_CODE (type) == COMPLEX_TYPE
3676 || !(TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
3677 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op0)))
3678 || !(TREE_CODE (TREE_TYPE (op1)) == INTEGER_TYPE
3679 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op1)))
3680 || !useless_type_conversion_p (TREE_TYPE (type),
3681 TREE_TYPE (op0))
3682 || !useless_type_conversion_p (TREE_TYPE (type),
3683 TREE_TYPE (op1)))
3685 error ("type mismatch in complex expression");
3686 debug_generic_stmt (TREE_TYPE (expr));
3687 debug_generic_stmt (TREE_TYPE (op0));
3688 debug_generic_stmt (TREE_TYPE (op1));
3689 return true;
3691 return false;
3694 case CONSTRUCTOR:
3696 /* This is used like COMPLEX_EXPR but for vectors. */
3697 if (TREE_CODE (type) != VECTOR_TYPE)
3699 error ("constructor not allowed for non-vector types");
3700 debug_generic_stmt (type);
3701 return true;
3703 /* FIXME: verify constructor arguments. */
3704 return false;
3707 case LSHIFT_EXPR:
3708 case RSHIFT_EXPR:
3709 case LROTATE_EXPR:
3710 case RROTATE_EXPR:
3712 tree op0 = TREE_OPERAND (expr, 0);
3713 tree op1 = TREE_OPERAND (expr, 1);
3714 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3716 error ("invalid operands in shift expression");
3717 return true;
3719 if (!TREE_CODE (TREE_TYPE (op1)) == INTEGER_TYPE
3720 || !useless_type_conversion_p (type, TREE_TYPE (op0)))
3722 error ("type mismatch in shift expression");
3723 debug_generic_stmt (TREE_TYPE (expr));
3724 debug_generic_stmt (TREE_TYPE (op0));
3725 debug_generic_stmt (TREE_TYPE (op1));
3726 return true;
3728 return false;
3731 case PLUS_EXPR:
3732 case MINUS_EXPR:
3734 tree op0 = TREE_OPERAND (expr, 0);
3735 tree op1 = TREE_OPERAND (expr, 1);
3736 if (POINTER_TYPE_P (type)
3737 || POINTER_TYPE_P (TREE_TYPE (op0))
3738 || POINTER_TYPE_P (TREE_TYPE (op1)))
3740 error ("invalid (pointer) operands to plus/minus");
3741 return true;
3743 /* Continue with generic binary expression handling. */
3744 break;
3747 case POINTER_PLUS_EXPR:
3749 tree op0 = TREE_OPERAND (expr, 0);
3750 tree op1 = TREE_OPERAND (expr, 1);
3751 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3753 error ("invalid operands in pointer plus expression");
3754 return true;
3756 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3757 || !useless_type_conversion_p (type, TREE_TYPE (op0))
3758 || !useless_type_conversion_p (sizetype, TREE_TYPE (op1)))
3760 error ("type mismatch in pointer plus expression");
3761 debug_generic_stmt (type);
3762 debug_generic_stmt (TREE_TYPE (op0));
3763 debug_generic_stmt (TREE_TYPE (op1));
3764 return true;
3766 return false;
3769 case COND_EXPR:
3771 tree op0 = TREE_OPERAND (expr, 0);
3772 tree op1 = TREE_OPERAND (expr, 1);
3773 tree op2 = TREE_OPERAND (expr, 2);
3774 if ((!is_gimple_val (op1)
3775 && TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3776 || (!is_gimple_val (op2)
3777 && TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE))
3779 error ("invalid operands in conditional expression");
3780 return true;
3782 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
3783 || (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE
3784 && !useless_type_conversion_p (type, TREE_TYPE (op1)))
3785 || (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE
3786 && !useless_type_conversion_p (type, TREE_TYPE (op2))))
3788 error ("type mismatch in conditional expression");
3789 debug_generic_stmt (type);
3790 debug_generic_stmt (TREE_TYPE (op0));
3791 debug_generic_stmt (TREE_TYPE (op1));
3792 debug_generic_stmt (TREE_TYPE (op2));
3793 return true;
3795 return verify_gimple_expr (op0);
3798 case ADDR_EXPR:
3800 tree op = TREE_OPERAND (expr, 0);
3801 if (!is_gimple_addressable (op))
3803 error ("invalid operand in unary expression");
3804 return true;
3806 if (!one_pointer_to_useless_type_conversion_p (type, TREE_TYPE (op))
3807 /* FIXME: a longstanding wart, &a == &a[0]. */
3808 && (TREE_CODE (TREE_TYPE (op)) != ARRAY_TYPE
3809 || !one_pointer_to_useless_type_conversion_p (type,
3810 TREE_TYPE (TREE_TYPE (op)))))
3812 error ("type mismatch in address expression");
3813 debug_generic_stmt (TREE_TYPE (expr));
3814 debug_generic_stmt (TYPE_POINTER_TO (TREE_TYPE (op)));
3815 return true;
3818 return verify_gimple_reference (op);
3821 case TRUTH_ANDIF_EXPR:
3822 case TRUTH_ORIF_EXPR:
3823 case TRUTH_AND_EXPR:
3824 case TRUTH_OR_EXPR:
3825 case TRUTH_XOR_EXPR:
3827 tree op0 = TREE_OPERAND (expr, 0);
3828 tree op1 = TREE_OPERAND (expr, 1);
3830 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3832 error ("invalid operands in truth expression");
3833 return true;
3836 /* We allow any kind of integral typed argument and result. */
3837 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
3838 || !INTEGRAL_TYPE_P (TREE_TYPE (op1))
3839 || !INTEGRAL_TYPE_P (type))
3841 error ("type mismatch in binary truth expression");
3842 debug_generic_stmt (type);
3843 debug_generic_stmt (TREE_TYPE (op0));
3844 debug_generic_stmt (TREE_TYPE (op1));
3845 return true;
3848 return false;
3851 case TRUTH_NOT_EXPR:
3853 tree op = TREE_OPERAND (expr, 0);
3855 if (!is_gimple_val (op))
3857 error ("invalid operand in unary not");
3858 return true;
3861 /* For TRUTH_NOT_EXPR we can have any kind of integral
3862 typed arguments and results. */
3863 if (!INTEGRAL_TYPE_P (TREE_TYPE (op))
3864 || !INTEGRAL_TYPE_P (type))
3866 error ("type mismatch in not expression");
3867 debug_generic_expr (TREE_TYPE (expr));
3868 debug_generic_expr (TREE_TYPE (op));
3869 return true;
3872 return false;
3875 case CALL_EXPR:
3876 /* FIXME. The C frontend passes unpromoted arguments in case it
3877 didn't see a function declaration before the call. */
3878 return false;
3880 case OBJ_TYPE_REF:
3881 /* FIXME. */
3882 return false;
3884 default:;
3887 /* Generic handling via classes. */
3888 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3890 case tcc_unary:
3891 return verify_gimple_unary_expr (expr);
3893 case tcc_binary:
3894 return verify_gimple_binary_expr (expr);
3896 case tcc_reference:
3897 return verify_gimple_reference (expr);
3899 case tcc_comparison:
3901 tree op0 = TREE_OPERAND (expr, 0);
3902 tree op1 = TREE_OPERAND (expr, 1);
3903 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3905 error ("invalid operands in comparison expression");
3906 return true;
3908 /* For comparisons we do not have the operations type as the
3909 effective type the comparison is carried out in. Instead
3910 we require that either the first operand is trivially
3911 convertible into the second, or the other way around.
3912 The resulting type of a comparison may be any integral type.
3913 Because we special-case pointers to void we allow
3914 comparisons of pointers with the same mode as well. */
3915 if ((!useless_type_conversion_p (TREE_TYPE (op0), TREE_TYPE (op1))
3916 && !useless_type_conversion_p (TREE_TYPE (op1), TREE_TYPE (op0))
3917 && (!POINTER_TYPE_P (TREE_TYPE (op0))
3918 || !POINTER_TYPE_P (TREE_TYPE (op1))
3919 || TYPE_MODE (TREE_TYPE (op0)) != TYPE_MODE (TREE_TYPE (op1))))
3920 || !INTEGRAL_TYPE_P (type))
3922 error ("type mismatch in comparison expression");
3923 debug_generic_stmt (TREE_TYPE (expr));
3924 debug_generic_stmt (TREE_TYPE (op0));
3925 debug_generic_stmt (TREE_TYPE (op1));
3926 return true;
3928 break;
3931 default:
3932 gcc_unreachable ();
3935 return false;
3938 /* Verify the GIMPLE assignment statement STMT. Returns true if there
3939 is an error, otherwise false. */
3941 static bool
3942 verify_gimple_modify_stmt (const_tree stmt)
3944 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3945 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3947 gcc_assert (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT);
3949 if (!useless_type_conversion_p (TREE_TYPE (lhs),
3950 TREE_TYPE (rhs)))
3952 error ("non-trivial conversion at assignment");
3953 debug_generic_expr (TREE_TYPE (lhs));
3954 debug_generic_expr (TREE_TYPE (rhs));
3955 return true;
3958 /* Loads/stores from/to a variable are ok. */
3959 if ((is_gimple_val (lhs)
3960 && is_gimple_variable (rhs))
3961 || (is_gimple_val (rhs)
3962 && is_gimple_variable (lhs)))
3963 return false;
3965 /* Aggregate copies are ok. */
3966 if (!is_gimple_reg_type (TREE_TYPE (lhs))
3967 && !is_gimple_reg_type (TREE_TYPE (rhs)))
3968 return false;
3970 /* We might get 'loads' from a parameter which is not a gimple value. */
3971 if (TREE_CODE (rhs) == PARM_DECL)
3972 return verify_gimple_expr (lhs);
3974 if (!is_gimple_variable (lhs)
3975 && verify_gimple_expr (lhs))
3976 return true;
3978 if (!is_gimple_variable (rhs)
3979 && verify_gimple_expr (rhs))
3980 return true;
3982 return false;
3985 /* Verify the GIMPLE statement STMT. Returns true if there is an
3986 error, otherwise false. */
3988 static bool
3989 verify_gimple_stmt (tree stmt)
3991 if (!is_gimple_stmt (stmt))
3993 error ("is not a valid GIMPLE statement");
3994 return true;
3997 if (OMP_DIRECTIVE_P (stmt))
3999 /* OpenMP directives are validated by the FE and never operated
4000 on by the optimizers. Furthermore, OMP_FOR may contain
4001 non-gimple expressions when the main index variable has had
4002 its address taken. This does not affect the loop itself
4003 because the header of an OMP_FOR is merely used to determine
4004 how to setup the parallel iteration. */
4005 return false;
4008 switch (TREE_CODE (stmt))
4010 case GIMPLE_MODIFY_STMT:
4011 return verify_gimple_modify_stmt (stmt);
4013 case GOTO_EXPR:
4014 case LABEL_EXPR:
4015 return false;
4017 case SWITCH_EXPR:
4018 if (!is_gimple_val (TREE_OPERAND (stmt, 0)))
4020 error ("invalid operand to switch statement");
4021 debug_generic_expr (TREE_OPERAND (stmt, 0));
4023 return false;
4025 case RETURN_EXPR:
4027 tree op = TREE_OPERAND (stmt, 0);
4029 if (TREE_CODE (TREE_TYPE (stmt)) != VOID_TYPE)
4031 error ("type error in return expression");
4032 return true;
4035 if (op == NULL_TREE
4036 || TREE_CODE (op) == RESULT_DECL)
4037 return false;
4039 return verify_gimple_modify_stmt (op);
4042 case CALL_EXPR:
4043 case COND_EXPR:
4044 return verify_gimple_expr (stmt);
4046 case NOP_EXPR:
4047 case CHANGE_DYNAMIC_TYPE_EXPR:
4048 case ASM_EXPR:
4049 return false;
4051 default:
4052 gcc_unreachable ();
4056 /* Verify the GIMPLE statements inside the statement list STMTS.
4057 Returns true if there were any errors. */
4059 static bool
4060 verify_gimple_2 (tree stmts)
4062 tree_stmt_iterator tsi;
4063 bool err = false;
4065 for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
4067 tree stmt = tsi_stmt (tsi);
4069 switch (TREE_CODE (stmt))
4071 case BIND_EXPR:
4072 err |= verify_gimple_2 (BIND_EXPR_BODY (stmt));
4073 break;
4075 case TRY_CATCH_EXPR:
4076 case TRY_FINALLY_EXPR:
4077 err |= verify_gimple_2 (TREE_OPERAND (stmt, 0));
4078 err |= verify_gimple_2 (TREE_OPERAND (stmt, 1));
4079 break;
4081 case CATCH_EXPR:
4082 err |= verify_gimple_2 (CATCH_BODY (stmt));
4083 break;
4085 case EH_FILTER_EXPR:
4086 err |= verify_gimple_2 (EH_FILTER_FAILURE (stmt));
4087 break;
4089 default:
4091 bool err2 = verify_gimple_stmt (stmt);
4092 if (err2)
4093 debug_generic_expr (stmt);
4094 err |= err2;
4099 return err;
4103 /* Verify the GIMPLE statements inside the statement list STMTS. */
4105 void
4106 verify_gimple_1 (tree stmts)
4108 if (verify_gimple_2 (stmts))
4109 internal_error ("verify_gimple failed");
4112 /* Verify the GIMPLE statements inside the current function. */
4114 void
4115 verify_gimple (void)
4117 verify_gimple_1 (BIND_EXPR_BODY (DECL_SAVED_TREE (cfun->decl)));
4120 /* Verify STMT, return true if STMT is not in GIMPLE form.
4121 TODO: Implement type checking. */
4123 static bool
4124 verify_stmt (tree stmt, bool last_in_block)
4126 tree addr;
4128 if (OMP_DIRECTIVE_P (stmt))
4130 /* OpenMP directives are validated by the FE and never operated
4131 on by the optimizers. Furthermore, OMP_FOR may contain
4132 non-gimple expressions when the main index variable has had
4133 its address taken. This does not affect the loop itself
4134 because the header of an OMP_FOR is merely used to determine
4135 how to setup the parallel iteration. */
4136 return false;
4139 if (!is_gimple_stmt (stmt))
4141 error ("is not a valid GIMPLE statement");
4142 goto fail;
4145 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
4146 if (addr)
4148 debug_generic_stmt (addr);
4149 return true;
4152 /* If the statement is marked as part of an EH region, then it is
4153 expected that the statement could throw. Verify that when we
4154 have optimizations that simplify statements such that we prove
4155 that they cannot throw, that we update other data structures
4156 to match. */
4157 if (lookup_stmt_eh_region (stmt) >= 0)
4159 if (!tree_could_throw_p (stmt))
4161 error ("statement marked for throw, but doesn%'t");
4162 goto fail;
4164 if (!last_in_block && tree_can_throw_internal (stmt))
4166 error ("statement marked for throw in middle of block");
4167 goto fail;
4171 return false;
4173 fail:
4174 debug_generic_stmt (stmt);
4175 return true;
4179 /* Return true when the T can be shared. */
4181 static bool
4182 tree_node_can_be_shared (tree t)
4184 if (IS_TYPE_OR_DECL_P (t)
4185 || is_gimple_min_invariant (t)
4186 || TREE_CODE (t) == SSA_NAME
4187 || t == error_mark_node
4188 || TREE_CODE (t) == IDENTIFIER_NODE)
4189 return true;
4191 if (TREE_CODE (t) == CASE_LABEL_EXPR)
4192 return true;
4194 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
4195 && is_gimple_min_invariant (TREE_OPERAND (t, 1)))
4196 || TREE_CODE (t) == COMPONENT_REF
4197 || TREE_CODE (t) == REALPART_EXPR
4198 || TREE_CODE (t) == IMAGPART_EXPR)
4199 t = TREE_OPERAND (t, 0);
4201 if (DECL_P (t))
4202 return true;
4204 return false;
4208 /* Called via walk_trees. Verify tree sharing. */
4210 static tree
4211 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
4213 struct pointer_set_t *visited = (struct pointer_set_t *) data;
4215 if (tree_node_can_be_shared (*tp))
4217 *walk_subtrees = false;
4218 return NULL;
4221 if (pointer_set_insert (visited, *tp))
4222 return *tp;
4224 return NULL;
4228 /* Helper function for verify_gimple_tuples. */
4230 static tree
4231 verify_gimple_tuples_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4232 void *data ATTRIBUTE_UNUSED)
4234 switch (TREE_CODE (*tp))
4236 case MODIFY_EXPR:
4237 error ("unexpected non-tuple");
4238 debug_tree (*tp);
4239 gcc_unreachable ();
4240 return NULL_TREE;
4242 default:
4243 return NULL_TREE;
4247 /* Verify that there are no trees that should have been converted to
4248 gimple tuples. Return true if T contains a node that should have
4249 been converted to a gimple tuple, but hasn't. */
4251 static bool
4252 verify_gimple_tuples (tree t)
4254 return walk_tree (&t, verify_gimple_tuples_1, NULL, NULL) != NULL;
4257 static bool eh_error_found;
4258 static int
4259 verify_eh_throw_stmt_node (void **slot, void *data)
4261 struct throw_stmt_node *node = (struct throw_stmt_node *)*slot;
4262 struct pointer_set_t *visited = (struct pointer_set_t *) data;
4264 if (!pointer_set_contains (visited, node->stmt))
4266 error ("Dead STMT in EH table");
4267 debug_generic_stmt (node->stmt);
4268 eh_error_found = true;
4270 return 0;
4273 /* Verify the GIMPLE statement chain. */
4275 void
4276 verify_stmts (void)
4278 basic_block bb;
4279 block_stmt_iterator bsi;
4280 bool err = false;
4281 struct pointer_set_t *visited, *visited_stmts;
4282 tree addr;
4284 timevar_push (TV_TREE_STMT_VERIFY);
4285 visited = pointer_set_create ();
4286 visited_stmts = pointer_set_create ();
4288 FOR_EACH_BB (bb)
4290 tree phi;
4291 int i;
4293 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4295 int phi_num_args = PHI_NUM_ARGS (phi);
4297 pointer_set_insert (visited_stmts, phi);
4298 if (bb_for_stmt (phi) != bb)
4300 error ("bb_for_stmt (phi) is set to a wrong basic block");
4301 err |= true;
4304 for (i = 0; i < phi_num_args; i++)
4306 tree t = PHI_ARG_DEF (phi, i);
4307 tree addr;
4309 if (!t)
4311 error ("missing PHI def");
4312 debug_generic_stmt (phi);
4313 err |= true;
4314 continue;
4316 /* Addressable variables do have SSA_NAMEs but they
4317 are not considered gimple values. */
4318 else if (TREE_CODE (t) != SSA_NAME
4319 && TREE_CODE (t) != FUNCTION_DECL
4320 && !is_gimple_val (t))
4322 error ("PHI def is not a GIMPLE value");
4323 debug_generic_stmt (phi);
4324 debug_generic_stmt (t);
4325 err |= true;
4328 addr = walk_tree (&t, verify_expr, (void *) 1, NULL);
4329 if (addr)
4331 debug_generic_stmt (addr);
4332 err |= true;
4335 addr = walk_tree (&t, verify_node_sharing, visited, NULL);
4336 if (addr)
4338 error ("incorrect sharing of tree nodes");
4339 debug_generic_stmt (phi);
4340 debug_generic_stmt (addr);
4341 err |= true;
4346 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
4348 tree stmt = bsi_stmt (bsi);
4350 pointer_set_insert (visited_stmts, stmt);
4351 err |= verify_gimple_tuples (stmt);
4353 if (bb_for_stmt (stmt) != bb)
4355 error ("bb_for_stmt (stmt) is set to a wrong basic block");
4356 err |= true;
4359 bsi_next (&bsi);
4360 err |= verify_stmt (stmt, bsi_end_p (bsi));
4361 addr = walk_tree (&stmt, verify_node_sharing, visited, NULL);
4362 if (addr)
4364 error ("incorrect sharing of tree nodes");
4365 debug_generic_stmt (stmt);
4366 debug_generic_stmt (addr);
4367 err |= true;
4371 eh_error_found = false;
4372 if (get_eh_throw_stmt_table (cfun))
4373 htab_traverse (get_eh_throw_stmt_table (cfun),
4374 verify_eh_throw_stmt_node,
4375 visited_stmts);
4377 if (err | eh_error_found)
4378 internal_error ("verify_stmts failed");
4380 pointer_set_destroy (visited);
4381 pointer_set_destroy (visited_stmts);
4382 verify_histograms ();
4383 timevar_pop (TV_TREE_STMT_VERIFY);
4387 /* Verifies that the flow information is OK. */
4389 static int
4390 tree_verify_flow_info (void)
4392 int err = 0;
4393 basic_block bb;
4394 block_stmt_iterator bsi;
4395 tree stmt;
4396 edge e;
4397 edge_iterator ei;
4399 if (ENTRY_BLOCK_PTR->il.tree)
4401 error ("ENTRY_BLOCK has IL associated with it");
4402 err = 1;
4405 if (EXIT_BLOCK_PTR->il.tree)
4407 error ("EXIT_BLOCK has IL associated with it");
4408 err = 1;
4411 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
4412 if (e->flags & EDGE_FALLTHRU)
4414 error ("fallthru to exit from bb %d", e->src->index);
4415 err = 1;
4418 FOR_EACH_BB (bb)
4420 bool found_ctrl_stmt = false;
4422 stmt = NULL_TREE;
4424 /* Skip labels on the start of basic block. */
4425 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4427 tree prev_stmt = stmt;
4429 stmt = bsi_stmt (bsi);
4431 if (TREE_CODE (stmt) != LABEL_EXPR)
4432 break;
4434 if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
4436 error ("nonlocal label ");
4437 print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
4438 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4439 bb->index);
4440 err = 1;
4443 if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
4445 error ("label ");
4446 print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
4447 fprintf (stderr, " to block does not match in bb %d",
4448 bb->index);
4449 err = 1;
4452 if (decl_function_context (LABEL_EXPR_LABEL (stmt))
4453 != current_function_decl)
4455 error ("label ");
4456 print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
4457 fprintf (stderr, " has incorrect context in bb %d",
4458 bb->index);
4459 err = 1;
4463 /* Verify that body of basic block BB is free of control flow. */
4464 for (; !bsi_end_p (bsi); bsi_next (&bsi))
4466 tree stmt = bsi_stmt (bsi);
4468 if (found_ctrl_stmt)
4470 error ("control flow in the middle of basic block %d",
4471 bb->index);
4472 err = 1;
4475 if (stmt_ends_bb_p (stmt))
4476 found_ctrl_stmt = true;
4478 if (TREE_CODE (stmt) == LABEL_EXPR)
4480 error ("label ");
4481 print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
4482 fprintf (stderr, " in the middle of basic block %d", bb->index);
4483 err = 1;
4487 bsi = bsi_last (bb);
4488 if (bsi_end_p (bsi))
4489 continue;
4491 stmt = bsi_stmt (bsi);
4493 err |= verify_eh_edges (stmt);
4495 if (is_ctrl_stmt (stmt))
4497 FOR_EACH_EDGE (e, ei, bb->succs)
4498 if (e->flags & EDGE_FALLTHRU)
4500 error ("fallthru edge after a control statement in bb %d",
4501 bb->index);
4502 err = 1;
4506 if (TREE_CODE (stmt) != COND_EXPR)
4508 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
4509 after anything else but if statement. */
4510 FOR_EACH_EDGE (e, ei, bb->succs)
4511 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
4513 error ("true/false edge after a non-COND_EXPR in bb %d",
4514 bb->index);
4515 err = 1;
4519 switch (TREE_CODE (stmt))
4521 case COND_EXPR:
4523 edge true_edge;
4524 edge false_edge;
4526 if (COND_EXPR_THEN (stmt) != NULL_TREE
4527 || COND_EXPR_ELSE (stmt) != NULL_TREE)
4529 error ("COND_EXPR with code in branches at the end of bb %d",
4530 bb->index);
4531 err = 1;
4534 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
4536 if (!true_edge || !false_edge
4537 || !(true_edge->flags & EDGE_TRUE_VALUE)
4538 || !(false_edge->flags & EDGE_FALSE_VALUE)
4539 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4540 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4541 || EDGE_COUNT (bb->succs) >= 3)
4543 error ("wrong outgoing edge flags at end of bb %d",
4544 bb->index);
4545 err = 1;
4548 break;
4550 case GOTO_EXPR:
4551 if (simple_goto_p (stmt))
4553 error ("explicit goto at end of bb %d", bb->index);
4554 err = 1;
4556 else
4558 /* FIXME. We should double check that the labels in the
4559 destination blocks have their address taken. */
4560 FOR_EACH_EDGE (e, ei, bb->succs)
4561 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
4562 | EDGE_FALSE_VALUE))
4563 || !(e->flags & EDGE_ABNORMAL))
4565 error ("wrong outgoing edge flags at end of bb %d",
4566 bb->index);
4567 err = 1;
4570 break;
4572 case RETURN_EXPR:
4573 if (!single_succ_p (bb)
4574 || (single_succ_edge (bb)->flags
4575 & (EDGE_FALLTHRU | EDGE_ABNORMAL
4576 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4578 error ("wrong outgoing edge flags at end of bb %d", bb->index);
4579 err = 1;
4581 if (single_succ (bb) != EXIT_BLOCK_PTR)
4583 error ("return edge does not point to exit in bb %d",
4584 bb->index);
4585 err = 1;
4587 break;
4589 case SWITCH_EXPR:
4591 tree prev;
4592 edge e;
4593 size_t i, n;
4594 tree vec;
4596 vec = SWITCH_LABELS (stmt);
4597 n = TREE_VEC_LENGTH (vec);
4599 /* Mark all the destination basic blocks. */
4600 for (i = 0; i < n; ++i)
4602 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
4603 basic_block label_bb = label_to_block (lab);
4605 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
4606 label_bb->aux = (void *)1;
4609 /* Verify that the case labels are sorted. */
4610 prev = TREE_VEC_ELT (vec, 0);
4611 for (i = 1; i < n - 1; ++i)
4613 tree c = TREE_VEC_ELT (vec, i);
4614 if (! CASE_LOW (c))
4616 error ("found default case not at end of case vector");
4617 err = 1;
4618 continue;
4620 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
4622 error ("case labels not sorted: ");
4623 print_generic_expr (stderr, prev, 0);
4624 fprintf (stderr," is greater than ");
4625 print_generic_expr (stderr, c, 0);
4626 fprintf (stderr," but comes before it.\n");
4627 err = 1;
4629 prev = c;
4631 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
4633 error ("no default case found at end of case vector");
4634 err = 1;
4637 FOR_EACH_EDGE (e, ei, bb->succs)
4639 if (!e->dest->aux)
4641 error ("extra outgoing edge %d->%d",
4642 bb->index, e->dest->index);
4643 err = 1;
4645 e->dest->aux = (void *)2;
4646 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
4647 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4649 error ("wrong outgoing edge flags at end of bb %d",
4650 bb->index);
4651 err = 1;
4655 /* Check that we have all of them. */
4656 for (i = 0; i < n; ++i)
4658 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
4659 basic_block label_bb = label_to_block (lab);
4661 if (label_bb->aux != (void *)2)
4663 error ("missing edge %i->%i",
4664 bb->index, label_bb->index);
4665 err = 1;
4669 FOR_EACH_EDGE (e, ei, bb->succs)
4670 e->dest->aux = (void *)0;
4673 default: ;
4677 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
4678 verify_dominators (CDI_DOMINATORS);
4680 return err;
4684 /* Updates phi nodes after creating a forwarder block joined
4685 by edge FALLTHRU. */
4687 static void
4688 tree_make_forwarder_block (edge fallthru)
4690 edge e;
4691 edge_iterator ei;
4692 basic_block dummy, bb;
4693 tree phi, new_phi, var;
4695 dummy = fallthru->src;
4696 bb = fallthru->dest;
4698 if (single_pred_p (bb))
4699 return;
4701 /* If we redirected a branch we must create new PHI nodes at the
4702 start of BB. */
4703 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
4705 var = PHI_RESULT (phi);
4706 new_phi = create_phi_node (var, bb);
4707 SSA_NAME_DEF_STMT (var) = new_phi;
4708 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
4709 add_phi_arg (new_phi, PHI_RESULT (phi), fallthru);
4712 /* Ensure that the PHI node chain is in the same order. */
4713 set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
4715 /* Add the arguments we have stored on edges. */
4716 FOR_EACH_EDGE (e, ei, bb->preds)
4718 if (e == fallthru)
4719 continue;
4721 flush_pending_stmts (e);
4726 /* Return a non-special label in the head of basic block BLOCK.
4727 Create one if it doesn't exist. */
4729 tree
4730 tree_block_label (basic_block bb)
4732 block_stmt_iterator i, s = bsi_start (bb);
4733 bool first = true;
4734 tree label, stmt;
4736 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4738 stmt = bsi_stmt (i);
4739 if (TREE_CODE (stmt) != LABEL_EXPR)
4740 break;
4741 label = LABEL_EXPR_LABEL (stmt);
4742 if (!DECL_NONLOCAL (label))
4744 if (!first)
4745 bsi_move_before (&i, &s);
4746 return label;
4750 label = create_artificial_label ();
4751 stmt = build1 (LABEL_EXPR, void_type_node, label);
4752 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4753 return label;
4757 /* Attempt to perform edge redirection by replacing a possibly complex
4758 jump instruction by a goto or by removing the jump completely.
4759 This can apply only if all edges now point to the same block. The
4760 parameters and return values are equivalent to
4761 redirect_edge_and_branch. */
4763 static edge
4764 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4766 basic_block src = e->src;
4767 block_stmt_iterator b;
4768 tree stmt;
4770 /* We can replace or remove a complex jump only when we have exactly
4771 two edges. */
4772 if (EDGE_COUNT (src->succs) != 2
4773 /* Verify that all targets will be TARGET. Specifically, the
4774 edge that is not E must also go to TARGET. */
4775 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4776 return NULL;
4778 b = bsi_last (src);
4779 if (bsi_end_p (b))
4780 return NULL;
4781 stmt = bsi_stmt (b);
4783 if (TREE_CODE (stmt) == COND_EXPR
4784 || TREE_CODE (stmt) == SWITCH_EXPR)
4786 bsi_remove (&b, true);
4787 e = ssa_redirect_edge (e, target);
4788 e->flags = EDGE_FALLTHRU;
4789 return e;
4792 return NULL;
4796 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4797 edge representing the redirected branch. */
4799 static edge
4800 tree_redirect_edge_and_branch (edge e, basic_block dest)
4802 basic_block bb = e->src;
4803 block_stmt_iterator bsi;
4804 edge ret;
4805 tree stmt;
4807 if (e->flags & EDGE_ABNORMAL)
4808 return NULL;
4810 if (e->src != ENTRY_BLOCK_PTR
4811 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4812 return ret;
4814 if (e->dest == dest)
4815 return NULL;
4817 bsi = bsi_last (bb);
4818 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4820 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4822 case COND_EXPR:
4823 /* For COND_EXPR, we only need to redirect the edge. */
4824 break;
4826 case GOTO_EXPR:
4827 /* No non-abnormal edges should lead from a non-simple goto, and
4828 simple ones should be represented implicitly. */
4829 gcc_unreachable ();
4831 case SWITCH_EXPR:
4833 tree cases = get_cases_for_edge (e, stmt);
4834 tree label = tree_block_label (dest);
4836 /* If we have a list of cases associated with E, then use it
4837 as it's a lot faster than walking the entire case vector. */
4838 if (cases)
4840 edge e2 = find_edge (e->src, dest);
4841 tree last, first;
4843 first = cases;
4844 while (cases)
4846 last = cases;
4847 CASE_LABEL (cases) = label;
4848 cases = TREE_CHAIN (cases);
4851 /* If there was already an edge in the CFG, then we need
4852 to move all the cases associated with E to E2. */
4853 if (e2)
4855 tree cases2 = get_cases_for_edge (e2, stmt);
4857 TREE_CHAIN (last) = TREE_CHAIN (cases2);
4858 TREE_CHAIN (cases2) = first;
4861 else
4863 tree vec = SWITCH_LABELS (stmt);
4864 size_t i, n = TREE_VEC_LENGTH (vec);
4866 for (i = 0; i < n; i++)
4868 tree elt = TREE_VEC_ELT (vec, i);
4870 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4871 CASE_LABEL (elt) = label;
4875 break;
4878 case RETURN_EXPR:
4879 bsi_remove (&bsi, true);
4880 e->flags |= EDGE_FALLTHRU;
4881 break;
4883 case OMP_RETURN:
4884 case OMP_CONTINUE:
4885 case OMP_SECTIONS_SWITCH:
4886 case OMP_FOR:
4887 /* The edges from OMP constructs can be simply redirected. */
4888 break;
4890 default:
4891 /* Otherwise it must be a fallthru edge, and we don't need to
4892 do anything besides redirecting it. */
4893 gcc_assert (e->flags & EDGE_FALLTHRU);
4894 break;
4897 /* Update/insert PHI nodes as necessary. */
4899 /* Now update the edges in the CFG. */
4900 e = ssa_redirect_edge (e, dest);
4902 return e;
4905 /* Returns true if it is possible to remove edge E by redirecting
4906 it to the destination of the other edge from E->src. */
4908 static bool
4909 tree_can_remove_branch_p (const_edge e)
4911 if (e->flags & EDGE_ABNORMAL)
4912 return false;
4914 return true;
4917 /* Simple wrapper, as we can always redirect fallthru edges. */
4919 static basic_block
4920 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4922 e = tree_redirect_edge_and_branch (e, dest);
4923 gcc_assert (e);
4925 return NULL;
4929 /* Splits basic block BB after statement STMT (but at least after the
4930 labels). If STMT is NULL, BB is split just after the labels. */
4932 static basic_block
4933 tree_split_block (basic_block bb, void *stmt)
4935 block_stmt_iterator bsi;
4936 tree_stmt_iterator tsi_tgt;
4937 tree act, list;
4938 basic_block new_bb;
4939 edge e;
4940 edge_iterator ei;
4942 new_bb = create_empty_bb (bb);
4944 /* Redirect the outgoing edges. */
4945 new_bb->succs = bb->succs;
4946 bb->succs = NULL;
4947 FOR_EACH_EDGE (e, ei, new_bb->succs)
4948 e->src = new_bb;
4950 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4951 stmt = NULL;
4953 /* Move everything from BSI to the new basic block. */
4954 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4956 act = bsi_stmt (bsi);
4957 if (TREE_CODE (act) == LABEL_EXPR)
4958 continue;
4960 if (!stmt)
4961 break;
4963 if (stmt == act)
4965 bsi_next (&bsi);
4966 break;
4970 if (bsi_end_p (bsi))
4971 return new_bb;
4973 /* Split the statement list - avoid re-creating new containers as this
4974 brings ugly quadratic memory consumption in the inliner.
4975 (We are still quadratic since we need to update stmt BB pointers,
4976 sadly.) */
4977 list = tsi_split_statement_list_before (&bsi.tsi);
4978 set_bb_stmt_list (new_bb, list);
4979 for (tsi_tgt = tsi_start (list);
4980 !tsi_end_p (tsi_tgt); tsi_next (&tsi_tgt))
4981 change_bb_for_stmt (tsi_stmt (tsi_tgt), new_bb);
4983 return new_bb;
4987 /* Moves basic block BB after block AFTER. */
4989 static bool
4990 tree_move_block_after (basic_block bb, basic_block after)
4992 if (bb->prev_bb == after)
4993 return true;
4995 unlink_block (bb);
4996 link_block (bb, after);
4998 return true;
5002 /* Return true if basic_block can be duplicated. */
5004 static bool
5005 tree_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
5007 return true;
5011 /* Create a duplicate of the basic block BB. NOTE: This does not
5012 preserve SSA form. */
5014 static basic_block
5015 tree_duplicate_bb (basic_block bb)
5017 basic_block new_bb;
5018 block_stmt_iterator bsi, bsi_tgt;
5019 tree phi;
5021 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
5023 /* Copy the PHI nodes. We ignore PHI node arguments here because
5024 the incoming edges have not been setup yet. */
5025 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5027 tree copy = create_phi_node (PHI_RESULT (phi), new_bb);
5028 create_new_def_for (PHI_RESULT (copy), copy, PHI_RESULT_PTR (copy));
5031 /* Keep the chain of PHI nodes in the same order so that they can be
5032 updated by ssa_redirect_edge. */
5033 set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb)));
5035 bsi_tgt = bsi_start (new_bb);
5036 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
5038 def_operand_p def_p;
5039 ssa_op_iter op_iter;
5040 tree stmt, copy;
5041 int region;
5043 stmt = bsi_stmt (bsi);
5044 if (TREE_CODE (stmt) == LABEL_EXPR)
5045 continue;
5047 /* Create a new copy of STMT and duplicate STMT's virtual
5048 operands. */
5049 copy = unshare_expr (stmt);
5050 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
5051 copy_virtual_operands (copy, stmt);
5052 region = lookup_stmt_eh_region (stmt);
5053 if (region >= 0)
5054 add_stmt_to_eh_region (copy, region);
5055 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
5057 /* Create new names for all the definitions created by COPY and
5058 add replacement mappings for each new name. */
5059 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
5060 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
5063 return new_bb;
5066 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
5068 static void
5069 add_phi_args_after_copy_edge (edge e_copy)
5071 basic_block bb, bb_copy = e_copy->src, dest;
5072 edge e;
5073 edge_iterator ei;
5074 tree phi, phi_copy, phi_next, def;
5076 if (!phi_nodes (e_copy->dest))
5077 return;
5079 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
5081 if (e_copy->dest->flags & BB_DUPLICATED)
5082 dest = get_bb_original (e_copy->dest);
5083 else
5084 dest = e_copy->dest;
5086 e = find_edge (bb, dest);
5087 if (!e)
5089 /* During loop unrolling the target of the latch edge is copied.
5090 In this case we are not looking for edge to dest, but to
5091 duplicated block whose original was dest. */
5092 FOR_EACH_EDGE (e, ei, bb->succs)
5094 if ((e->dest->flags & BB_DUPLICATED)
5095 && get_bb_original (e->dest) == dest)
5096 break;
5099 gcc_assert (e != NULL);
5102 for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
5103 phi;
5104 phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
5106 phi_next = PHI_CHAIN (phi);
5107 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5108 add_phi_arg (phi_copy, def, e_copy);
5113 /* Basic block BB_COPY was created by code duplication. Add phi node
5114 arguments for edges going out of BB_COPY. The blocks that were
5115 duplicated have BB_DUPLICATED set. */
5117 void
5118 add_phi_args_after_copy_bb (basic_block bb_copy)
5120 edge_iterator ei;
5121 edge e_copy;
5123 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
5125 add_phi_args_after_copy_edge (e_copy);
5129 /* Blocks in REGION_COPY array of length N_REGION were created by
5130 duplication of basic blocks. Add phi node arguments for edges
5131 going from these blocks. If E_COPY is not NULL, also add
5132 phi node arguments for its destination.*/
5134 void
5135 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
5136 edge e_copy)
5138 unsigned i;
5140 for (i = 0; i < n_region; i++)
5141 region_copy[i]->flags |= BB_DUPLICATED;
5143 for (i = 0; i < n_region; i++)
5144 add_phi_args_after_copy_bb (region_copy[i]);
5145 if (e_copy)
5146 add_phi_args_after_copy_edge (e_copy);
5148 for (i = 0; i < n_region; i++)
5149 region_copy[i]->flags &= ~BB_DUPLICATED;
5152 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5153 important exit edge EXIT. By important we mean that no SSA name defined
5154 inside region is live over the other exit edges of the region. All entry
5155 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5156 to the duplicate of the region. SSA form, dominance and loop information
5157 is updated. The new basic blocks are stored to REGION_COPY in the same
5158 order as they had in REGION, provided that REGION_COPY is not NULL.
5159 The function returns false if it is unable to copy the region,
5160 true otherwise. */
5162 bool
5163 tree_duplicate_sese_region (edge entry, edge exit,
5164 basic_block *region, unsigned n_region,
5165 basic_block *region_copy)
5167 unsigned i;
5168 bool free_region_copy = false, copying_header = false;
5169 struct loop *loop = entry->dest->loop_father;
5170 edge exit_copy;
5171 VEC (basic_block, heap) *doms;
5172 edge redirected;
5173 int total_freq = 0, entry_freq = 0;
5174 gcov_type total_count = 0, entry_count = 0;
5176 if (!can_copy_bbs_p (region, n_region))
5177 return false;
5179 /* Some sanity checking. Note that we do not check for all possible
5180 missuses of the functions. I.e. if you ask to copy something weird,
5181 it will work, but the state of structures probably will not be
5182 correct. */
5183 for (i = 0; i < n_region; i++)
5185 /* We do not handle subloops, i.e. all the blocks must belong to the
5186 same loop. */
5187 if (region[i]->loop_father != loop)
5188 return false;
5190 if (region[i] != entry->dest
5191 && region[i] == loop->header)
5192 return false;
5195 set_loop_copy (loop, loop);
5197 /* In case the function is used for loop header copying (which is the primary
5198 use), ensure that EXIT and its copy will be new latch and entry edges. */
5199 if (loop->header == entry->dest)
5201 copying_header = true;
5202 set_loop_copy (loop, loop_outer (loop));
5204 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5205 return false;
5207 for (i = 0; i < n_region; i++)
5208 if (region[i] != exit->src
5209 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5210 return false;
5213 if (!region_copy)
5215 region_copy = XNEWVEC (basic_block, n_region);
5216 free_region_copy = true;
5219 gcc_assert (!need_ssa_update_p ());
5221 /* Record blocks outside the region that are dominated by something
5222 inside. */
5223 doms = NULL;
5224 initialize_original_copy_tables ();
5226 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5228 if (entry->dest->count)
5230 total_count = entry->dest->count;
5231 entry_count = entry->count;
5232 /* Fix up corner cases, to avoid division by zero or creation of negative
5233 frequencies. */
5234 if (entry_count > total_count)
5235 entry_count = total_count;
5237 else
5239 total_freq = entry->dest->frequency;
5240 entry_freq = EDGE_FREQUENCY (entry);
5241 /* Fix up corner cases, to avoid division by zero or creation of negative
5242 frequencies. */
5243 if (total_freq == 0)
5244 total_freq = 1;
5245 else if (entry_freq > total_freq)
5246 entry_freq = total_freq;
5249 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
5250 split_edge_bb_loc (entry));
5251 if (total_count)
5253 scale_bbs_frequencies_gcov_type (region, n_region,
5254 total_count - entry_count,
5255 total_count);
5256 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
5257 total_count);
5259 else
5261 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
5262 total_freq);
5263 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
5266 if (copying_header)
5268 loop->header = exit->dest;
5269 loop->latch = exit->src;
5272 /* Redirect the entry and add the phi node arguments. */
5273 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
5274 gcc_assert (redirected != NULL);
5275 flush_pending_stmts (entry);
5277 /* Concerning updating of dominators: We must recount dominators
5278 for entry block and its copy. Anything that is outside of the
5279 region, but was dominated by something inside needs recounting as
5280 well. */
5281 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5282 VEC_safe_push (basic_block, heap, doms, get_bb_original (entry->dest));
5283 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5284 VEC_free (basic_block, heap, doms);
5286 /* Add the other PHI node arguments. */
5287 add_phi_args_after_copy (region_copy, n_region, NULL);
5289 /* Update the SSA web. */
5290 update_ssa (TODO_update_ssa);
5292 if (free_region_copy)
5293 free (region_copy);
5295 free_original_copy_tables ();
5296 return true;
5299 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
5300 are stored to REGION_COPY in the same order in that they appear
5301 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
5302 the region, EXIT an exit from it. The condition guarding EXIT
5303 is moved to ENTRY. Returns true if duplication succeeds, false
5304 otherwise.
5306 For example,
5308 some_code;
5309 if (cond)
5311 else
5314 is transformed to
5316 if (cond)
5318 some_code;
5321 else
5323 some_code;
5328 bool
5329 tree_duplicate_sese_tail (edge entry, edge exit,
5330 basic_block *region, unsigned n_region,
5331 basic_block *region_copy)
5333 unsigned i;
5334 bool free_region_copy = false;
5335 struct loop *loop = exit->dest->loop_father;
5336 struct loop *orig_loop = entry->dest->loop_father;
5337 basic_block switch_bb, entry_bb, nentry_bb;
5338 VEC (basic_block, heap) *doms;
5339 int total_freq = 0, exit_freq = 0;
5340 gcov_type total_count = 0, exit_count = 0;
5341 edge exits[2], nexits[2], e;
5342 block_stmt_iterator bsi;
5343 tree cond;
5344 edge sorig, snew;
5346 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
5347 exits[0] = exit;
5348 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
5350 if (!can_copy_bbs_p (region, n_region))
5351 return false;
5353 /* Some sanity checking. Note that we do not check for all possible
5354 missuses of the functions. I.e. if you ask to copy something weird
5355 (e.g., in the example, if there is a jump from inside to the middle
5356 of some_code, or come_code defines some of the values used in cond)
5357 it will work, but the resulting code will not be correct. */
5358 for (i = 0; i < n_region; i++)
5360 /* We do not handle subloops, i.e. all the blocks must belong to the
5361 same loop. */
5362 if (region[i]->loop_father != orig_loop)
5363 return false;
5365 if (region[i] == orig_loop->latch)
5366 return false;
5369 initialize_original_copy_tables ();
5370 set_loop_copy (orig_loop, loop);
5372 if (!region_copy)
5374 region_copy = XNEWVEC (basic_block, n_region);
5375 free_region_copy = true;
5378 gcc_assert (!need_ssa_update_p ());
5380 /* Record blocks outside the region that are dominated by something
5381 inside. */
5382 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5384 if (exit->src->count)
5386 total_count = exit->src->count;
5387 exit_count = exit->count;
5388 /* Fix up corner cases, to avoid division by zero or creation of negative
5389 frequencies. */
5390 if (exit_count > total_count)
5391 exit_count = total_count;
5393 else
5395 total_freq = exit->src->frequency;
5396 exit_freq = EDGE_FREQUENCY (exit);
5397 /* Fix up corner cases, to avoid division by zero or creation of negative
5398 frequencies. */
5399 if (total_freq == 0)
5400 total_freq = 1;
5401 if (exit_freq > total_freq)
5402 exit_freq = total_freq;
5405 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
5406 split_edge_bb_loc (exit));
5407 if (total_count)
5409 scale_bbs_frequencies_gcov_type (region, n_region,
5410 total_count - exit_count,
5411 total_count);
5412 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
5413 total_count);
5415 else
5417 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
5418 total_freq);
5419 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
5422 /* Create the switch block, and put the exit condition to it. */
5423 entry_bb = entry->dest;
5424 nentry_bb = get_bb_copy (entry_bb);
5425 if (!last_stmt (entry->src)
5426 || !stmt_ends_bb_p (last_stmt (entry->src)))
5427 switch_bb = entry->src;
5428 else
5429 switch_bb = split_edge (entry);
5430 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
5432 bsi = bsi_last (switch_bb);
5433 cond = last_stmt (exit->src);
5434 gcc_assert (TREE_CODE (cond) == COND_EXPR);
5435 bsi_insert_after (&bsi, unshare_expr (cond), BSI_NEW_STMT);
5437 sorig = single_succ_edge (switch_bb);
5438 sorig->flags = exits[1]->flags;
5439 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
5441 /* Register the new edge from SWITCH_BB in loop exit lists. */
5442 rescan_loop_exit (snew, true, false);
5444 /* Add the PHI node arguments. */
5445 add_phi_args_after_copy (region_copy, n_region, snew);
5447 /* Get rid of now superfluous conditions and associated edges (and phi node
5448 arguments). */
5449 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
5450 PENDING_STMT (e) = NULL_TREE;
5451 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
5452 PENDING_STMT (e) = NULL_TREE;
5454 /* Anything that is outside of the region, but was dominated by something
5455 inside needs to update dominance info. */
5456 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5457 VEC_free (basic_block, heap, doms);
5459 /* Update the SSA web. */
5460 update_ssa (TODO_update_ssa);
5462 if (free_region_copy)
5463 free (region_copy);
5465 free_original_copy_tables ();
5466 return true;
5470 DEF_VEC_P(basic_block);
5471 DEF_VEC_ALLOC_P(basic_block,heap);
5474 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
5475 adding blocks when the dominator traversal reaches EXIT. This
5476 function silently assumes that ENTRY strictly dominates EXIT. */
5478 static void
5479 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
5480 VEC(basic_block,heap) **bbs_p)
5482 basic_block son;
5484 for (son = first_dom_son (CDI_DOMINATORS, entry);
5485 son;
5486 son = next_dom_son (CDI_DOMINATORS, son))
5488 VEC_safe_push (basic_block, heap, *bbs_p, son);
5489 if (son != exit)
5490 gather_blocks_in_sese_region (son, exit, bbs_p);
5494 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
5495 The duplicates are recorded in VARS_MAP. */
5497 static void
5498 replace_by_duplicate_decl (tree *tp, struct pointer_map_t *vars_map,
5499 tree to_context)
5501 tree t = *tp, new_t;
5502 struct function *f = DECL_STRUCT_FUNCTION (to_context);
5503 void **loc;
5505 if (DECL_CONTEXT (t) == to_context)
5506 return;
5508 loc = pointer_map_contains (vars_map, t);
5510 if (!loc)
5512 loc = pointer_map_insert (vars_map, t);
5514 if (SSA_VAR_P (t))
5516 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
5517 f->unexpanded_var_list
5518 = tree_cons (NULL_TREE, new_t, f->unexpanded_var_list);
5520 else
5522 gcc_assert (TREE_CODE (t) == CONST_DECL);
5523 new_t = copy_node (t);
5525 DECL_CONTEXT (new_t) = to_context;
5527 *loc = new_t;
5529 else
5530 new_t = *loc;
5532 *tp = new_t;
5535 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
5536 VARS_MAP maps old ssa names and var_decls to the new ones. */
5538 static tree
5539 replace_ssa_name (tree name, struct pointer_map_t *vars_map,
5540 tree to_context)
5542 void **loc;
5543 tree new_name, decl = SSA_NAME_VAR (name);
5545 gcc_assert (is_gimple_reg (name));
5547 loc = pointer_map_contains (vars_map, name);
5549 if (!loc)
5551 replace_by_duplicate_decl (&decl, vars_map, to_context);
5553 push_cfun (DECL_STRUCT_FUNCTION (to_context));
5554 if (gimple_in_ssa_p (cfun))
5555 add_referenced_var (decl);
5557 new_name = make_ssa_name (decl, SSA_NAME_DEF_STMT (name));
5558 if (SSA_NAME_IS_DEFAULT_DEF (name))
5559 set_default_def (decl, new_name);
5560 pop_cfun ();
5562 loc = pointer_map_insert (vars_map, name);
5563 *loc = new_name;
5565 else
5566 new_name = *loc;
5568 return new_name;
5571 struct move_stmt_d
5573 tree block;
5574 tree from_context;
5575 tree to_context;
5576 struct pointer_map_t *vars_map;
5577 htab_t new_label_map;
5578 bool remap_decls_p;
5581 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
5582 contained in *TP and change the DECL_CONTEXT of every local
5583 variable referenced in *TP. */
5585 static tree
5586 move_stmt_r (tree *tp, int *walk_subtrees, void *data)
5588 struct move_stmt_d *p = (struct move_stmt_d *) data;
5589 tree t = *tp;
5591 if (p->block
5592 && (EXPR_P (t) || GIMPLE_STMT_P (t)))
5593 TREE_BLOCK (t) = p->block;
5595 if (OMP_DIRECTIVE_P (t)
5596 && TREE_CODE (t) != OMP_RETURN
5597 && TREE_CODE (t) != OMP_CONTINUE)
5599 /* Do not remap variables inside OMP directives. Variables
5600 referenced in clauses and directive header belong to the
5601 parent function and should not be moved into the child
5602 function. */
5603 bool save_remap_decls_p = p->remap_decls_p;
5604 p->remap_decls_p = false;
5605 *walk_subtrees = 0;
5607 walk_tree (&OMP_BODY (t), move_stmt_r, p, NULL);
5609 p->remap_decls_p = save_remap_decls_p;
5611 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
5613 if (TREE_CODE (t) == SSA_NAME)
5614 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
5615 else if (TREE_CODE (t) == LABEL_DECL)
5617 if (p->new_label_map)
5619 struct tree_map in, *out;
5620 in.base.from = t;
5621 out = htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
5622 if (out)
5623 *tp = t = out->to;
5626 DECL_CONTEXT (t) = p->to_context;
5628 else if (p->remap_decls_p)
5630 /* Replace T with its duplicate. T should no longer appear in the
5631 parent function, so this looks wasteful; however, it may appear
5632 in referenced_vars, and more importantly, as virtual operands of
5633 statements, and in alias lists of other variables. It would be
5634 quite difficult to expunge it from all those places. ??? It might
5635 suffice to do this for addressable variables. */
5636 if ((TREE_CODE (t) == VAR_DECL
5637 && !is_global_var (t))
5638 || TREE_CODE (t) == CONST_DECL)
5639 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
5641 if (SSA_VAR_P (t)
5642 && gimple_in_ssa_p (cfun))
5644 push_cfun (DECL_STRUCT_FUNCTION (p->to_context));
5645 add_referenced_var (*tp);
5646 pop_cfun ();
5649 *walk_subtrees = 0;
5651 else if (TYPE_P (t))
5652 *walk_subtrees = 0;
5654 return NULL_TREE;
5657 /* Marks virtual operands of all statements in basic blocks BBS for
5658 renaming. */
5660 static void
5661 mark_virtual_ops_in_region (VEC (basic_block,heap) *bbs)
5663 tree phi;
5664 block_stmt_iterator bsi;
5665 basic_block bb;
5666 unsigned i;
5668 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
5670 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5671 mark_virtual_ops_for_renaming (phi);
5673 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
5674 mark_virtual_ops_for_renaming (bsi_stmt (bsi));
5678 /* Move basic block BB from function CFUN to function DEST_FN. The
5679 block is moved out of the original linked list and placed after
5680 block AFTER in the new list. Also, the block is removed from the
5681 original array of blocks and placed in DEST_FN's array of blocks.
5682 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
5683 updated to reflect the moved edges.
5685 The local variables are remapped to new instances, VARS_MAP is used
5686 to record the mapping. */
5688 static void
5689 move_block_to_fn (struct function *dest_cfun, basic_block bb,
5690 basic_block after, bool update_edge_count_p,
5691 struct pointer_map_t *vars_map, htab_t new_label_map,
5692 int eh_offset)
5694 struct control_flow_graph *cfg;
5695 edge_iterator ei;
5696 edge e;
5697 block_stmt_iterator si;
5698 struct move_stmt_d d;
5699 unsigned old_len, new_len;
5700 tree phi, next_phi;
5702 /* Remove BB from dominance structures. */
5703 delete_from_dominance_info (CDI_DOMINATORS, bb);
5704 if (current_loops)
5705 remove_bb_from_loops (bb);
5707 /* Link BB to the new linked list. */
5708 move_block_after (bb, after);
5710 /* Update the edge count in the corresponding flowgraphs. */
5711 if (update_edge_count_p)
5712 FOR_EACH_EDGE (e, ei, bb->succs)
5714 cfun->cfg->x_n_edges--;
5715 dest_cfun->cfg->x_n_edges++;
5718 /* Remove BB from the original basic block array. */
5719 VEC_replace (basic_block, cfun->cfg->x_basic_block_info, bb->index, NULL);
5720 cfun->cfg->x_n_basic_blocks--;
5722 /* Grow DEST_CFUN's basic block array if needed. */
5723 cfg = dest_cfun->cfg;
5724 cfg->x_n_basic_blocks++;
5725 if (bb->index >= cfg->x_last_basic_block)
5726 cfg->x_last_basic_block = bb->index + 1;
5728 old_len = VEC_length (basic_block, cfg->x_basic_block_info);
5729 if ((unsigned) cfg->x_last_basic_block >= old_len)
5731 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
5732 VEC_safe_grow_cleared (basic_block, gc, cfg->x_basic_block_info,
5733 new_len);
5736 VEC_replace (basic_block, cfg->x_basic_block_info,
5737 bb->index, bb);
5739 /* Remap the variables in phi nodes. */
5740 for (phi = phi_nodes (bb); phi; phi = next_phi)
5742 use_operand_p use;
5743 tree op = PHI_RESULT (phi);
5744 ssa_op_iter oi;
5746 next_phi = PHI_CHAIN (phi);
5747 if (!is_gimple_reg (op))
5749 /* Remove the phi nodes for virtual operands (alias analysis will be
5750 run for the new function, anyway). */
5751 remove_phi_node (phi, NULL, true);
5752 continue;
5755 SET_PHI_RESULT (phi, replace_ssa_name (op, vars_map, dest_cfun->decl));
5756 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
5758 op = USE_FROM_PTR (use);
5759 if (TREE_CODE (op) == SSA_NAME)
5760 SET_USE (use, replace_ssa_name (op, vars_map, dest_cfun->decl));
5764 /* The statements in BB need to be associated with a new TREE_BLOCK.
5765 Labels need to be associated with a new label-to-block map. */
5766 memset (&d, 0, sizeof (d));
5767 d.vars_map = vars_map;
5768 d.from_context = cfun->decl;
5769 d.to_context = dest_cfun->decl;
5770 d.new_label_map = new_label_map;
5772 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
5774 tree stmt = bsi_stmt (si);
5775 int region;
5777 d.remap_decls_p = true;
5778 if (TREE_BLOCK (stmt))
5779 d.block = DECL_INITIAL (dest_cfun->decl);
5781 walk_tree (&stmt, move_stmt_r, &d, NULL);
5783 if (TREE_CODE (stmt) == LABEL_EXPR)
5785 tree label = LABEL_EXPR_LABEL (stmt);
5786 int uid = LABEL_DECL_UID (label);
5788 gcc_assert (uid > -1);
5790 old_len = VEC_length (basic_block, cfg->x_label_to_block_map);
5791 if (old_len <= (unsigned) uid)
5793 new_len = 3 * uid / 2;
5794 VEC_safe_grow_cleared (basic_block, gc,
5795 cfg->x_label_to_block_map, new_len);
5798 VEC_replace (basic_block, cfg->x_label_to_block_map, uid, bb);
5799 VEC_replace (basic_block, cfun->cfg->x_label_to_block_map, uid, NULL);
5801 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
5803 if (uid >= dest_cfun->last_label_uid)
5804 dest_cfun->last_label_uid = uid + 1;
5806 else if (TREE_CODE (stmt) == RESX_EXPR && eh_offset != 0)
5807 TREE_OPERAND (stmt, 0) =
5808 build_int_cst (NULL_TREE,
5809 TREE_INT_CST_LOW (TREE_OPERAND (stmt, 0))
5810 + eh_offset);
5812 region = lookup_stmt_eh_region (stmt);
5813 if (region >= 0)
5815 add_stmt_to_eh_region_fn (dest_cfun, stmt, region + eh_offset);
5816 remove_stmt_from_eh_region (stmt);
5817 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
5818 gimple_remove_stmt_histograms (cfun, stmt);
5821 /* We cannot leave any operands allocated from the operand caches of
5822 the current function. */
5823 free_stmt_operands (stmt);
5824 push_cfun (dest_cfun);
5825 update_stmt (stmt);
5826 pop_cfun ();
5830 /* Examine the statements in BB (which is in SRC_CFUN); find and return
5831 the outermost EH region. Use REGION as the incoming base EH region. */
5833 static int
5834 find_outermost_region_in_block (struct function *src_cfun,
5835 basic_block bb, int region)
5837 block_stmt_iterator si;
5839 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
5841 tree stmt = bsi_stmt (si);
5842 int stmt_region;
5844 if (TREE_CODE (stmt) == RESX_EXPR)
5845 stmt_region = TREE_INT_CST_LOW (TREE_OPERAND (stmt, 0));
5846 else
5847 stmt_region = lookup_stmt_eh_region_fn (src_cfun, stmt);
5848 if (stmt_region > 0)
5850 if (region < 0)
5851 region = stmt_region;
5852 else if (stmt_region != region)
5854 region = eh_region_outermost (src_cfun, stmt_region, region);
5855 gcc_assert (region != -1);
5860 return region;
5863 static tree
5864 new_label_mapper (tree decl, void *data)
5866 htab_t hash = (htab_t) data;
5867 struct tree_map *m;
5868 void **slot;
5870 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
5872 m = xmalloc (sizeof (struct tree_map));
5873 m->hash = DECL_UID (decl);
5874 m->base.from = decl;
5875 m->to = create_artificial_label ();
5876 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
5878 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
5879 gcc_assert (*slot == NULL);
5881 *slot = m;
5883 return m->to;
5886 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
5887 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
5888 single basic block in the original CFG and the new basic block is
5889 returned. DEST_CFUN must not have a CFG yet.
5891 Note that the region need not be a pure SESE region. Blocks inside
5892 the region may contain calls to abort/exit. The only restriction
5893 is that ENTRY_BB should be the only entry point and it must
5894 dominate EXIT_BB.
5896 All local variables referenced in the region are assumed to be in
5897 the corresponding BLOCK_VARS and unexpanded variable lists
5898 associated with DEST_CFUN. */
5900 basic_block
5901 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
5902 basic_block exit_bb)
5904 VEC(basic_block,heap) *bbs, *dom_bbs;
5905 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
5906 basic_block after, bb, *entry_pred, *exit_succ, abb;
5907 struct function *saved_cfun = cfun;
5908 int *entry_flag, *exit_flag, eh_offset;
5909 unsigned *entry_prob, *exit_prob;
5910 unsigned i, num_entry_edges, num_exit_edges;
5911 edge e;
5912 edge_iterator ei;
5913 htab_t new_label_map;
5914 struct pointer_map_t *vars_map;
5915 struct loop *loop = entry_bb->loop_father;
5917 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
5918 region. */
5919 gcc_assert (entry_bb != exit_bb
5920 && (!exit_bb
5921 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
5923 /* Collect all the blocks in the region. Manually add ENTRY_BB
5924 because it won't be added by dfs_enumerate_from. */
5925 bbs = NULL;
5926 VEC_safe_push (basic_block, heap, bbs, entry_bb);
5927 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
5929 /* The blocks that used to be dominated by something in BBS will now be
5930 dominated by the new block. */
5931 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
5932 VEC_address (basic_block, bbs),
5933 VEC_length (basic_block, bbs));
5935 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
5936 the predecessor edges to ENTRY_BB and the successor edges to
5937 EXIT_BB so that we can re-attach them to the new basic block that
5938 will replace the region. */
5939 num_entry_edges = EDGE_COUNT (entry_bb->preds);
5940 entry_pred = (basic_block *) xcalloc (num_entry_edges, sizeof (basic_block));
5941 entry_flag = (int *) xcalloc (num_entry_edges, sizeof (int));
5942 entry_prob = XNEWVEC (unsigned, num_entry_edges);
5943 i = 0;
5944 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
5946 entry_prob[i] = e->probability;
5947 entry_flag[i] = e->flags;
5948 entry_pred[i++] = e->src;
5949 remove_edge (e);
5952 if (exit_bb)
5954 num_exit_edges = EDGE_COUNT (exit_bb->succs);
5955 exit_succ = (basic_block *) xcalloc (num_exit_edges,
5956 sizeof (basic_block));
5957 exit_flag = (int *) xcalloc (num_exit_edges, sizeof (int));
5958 exit_prob = XNEWVEC (unsigned, num_exit_edges);
5959 i = 0;
5960 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
5962 exit_prob[i] = e->probability;
5963 exit_flag[i] = e->flags;
5964 exit_succ[i++] = e->dest;
5965 remove_edge (e);
5968 else
5970 num_exit_edges = 0;
5971 exit_succ = NULL;
5972 exit_flag = NULL;
5973 exit_prob = NULL;
5976 /* Switch context to the child function to initialize DEST_FN's CFG. */
5977 gcc_assert (dest_cfun->cfg == NULL);
5978 push_cfun (dest_cfun);
5980 init_empty_tree_cfg ();
5982 /* Initialize EH information for the new function. */
5983 eh_offset = 0;
5984 new_label_map = NULL;
5985 if (saved_cfun->eh)
5987 int region = -1;
5989 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
5990 region = find_outermost_region_in_block (saved_cfun, bb, region);
5992 init_eh_for_function ();
5993 if (region != -1)
5995 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
5996 eh_offset = duplicate_eh_regions (saved_cfun, new_label_mapper,
5997 new_label_map, region, 0);
6001 pop_cfun ();
6003 /* The ssa form for virtual operands in the source function will have to
6004 be repaired. We do not care for the real operands -- the sese region
6005 must be closed with respect to those. */
6006 mark_virtual_ops_in_region (bbs);
6008 /* Move blocks from BBS into DEST_CFUN. */
6009 gcc_assert (VEC_length (basic_block, bbs) >= 2);
6010 after = dest_cfun->cfg->x_entry_block_ptr;
6011 vars_map = pointer_map_create ();
6012 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
6014 /* No need to update edge counts on the last block. It has
6015 already been updated earlier when we detached the region from
6016 the original CFG. */
6017 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, vars_map,
6018 new_label_map, eh_offset);
6019 after = bb;
6022 if (new_label_map)
6023 htab_delete (new_label_map);
6024 pointer_map_destroy (vars_map);
6026 /* Rewire the entry and exit blocks. The successor to the entry
6027 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
6028 the child function. Similarly, the predecessor of DEST_FN's
6029 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
6030 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
6031 various CFG manipulation function get to the right CFG.
6033 FIXME, this is silly. The CFG ought to become a parameter to
6034 these helpers. */
6035 push_cfun (dest_cfun);
6036 make_edge (ENTRY_BLOCK_PTR, entry_bb, EDGE_FALLTHRU);
6037 if (exit_bb)
6038 make_edge (exit_bb, EXIT_BLOCK_PTR, 0);
6039 pop_cfun ();
6041 /* Back in the original function, the SESE region has disappeared,
6042 create a new basic block in its place. */
6043 bb = create_empty_bb (entry_pred[0]);
6044 if (current_loops)
6045 add_bb_to_loop (bb, loop);
6046 for (i = 0; i < num_entry_edges; i++)
6048 e = make_edge (entry_pred[i], bb, entry_flag[i]);
6049 e->probability = entry_prob[i];
6052 for (i = 0; i < num_exit_edges; i++)
6054 e = make_edge (bb, exit_succ[i], exit_flag[i]);
6055 e->probability = exit_prob[i];
6058 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
6059 for (i = 0; VEC_iterate (basic_block, dom_bbs, i, abb); i++)
6060 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
6061 VEC_free (basic_block, heap, dom_bbs);
6063 if (exit_bb)
6065 free (exit_prob);
6066 free (exit_flag);
6067 free (exit_succ);
6069 free (entry_prob);
6070 free (entry_flag);
6071 free (entry_pred);
6072 VEC_free (basic_block, heap, bbs);
6074 return bb;
6078 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
6080 void
6081 dump_function_to_file (tree fn, FILE *file, int flags)
6083 tree arg, vars, var;
6084 struct function *dsf;
6085 bool ignore_topmost_bind = false, any_var = false;
6086 basic_block bb;
6087 tree chain;
6089 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
6091 arg = DECL_ARGUMENTS (fn);
6092 while (arg)
6094 print_generic_expr (file, arg, dump_flags);
6095 if (TREE_CHAIN (arg))
6096 fprintf (file, ", ");
6097 arg = TREE_CHAIN (arg);
6099 fprintf (file, ")\n");
6101 dsf = DECL_STRUCT_FUNCTION (fn);
6102 if (dsf && (flags & TDF_DETAILS))
6103 dump_eh_tree (file, dsf);
6105 if (flags & TDF_RAW)
6107 dump_node (fn, TDF_SLIM | flags, file);
6108 return;
6111 /* Switch CFUN to point to FN. */
6112 push_cfun (DECL_STRUCT_FUNCTION (fn));
6114 /* When GIMPLE is lowered, the variables are no longer available in
6115 BIND_EXPRs, so display them separately. */
6116 if (cfun && cfun->decl == fn && cfun->unexpanded_var_list)
6118 ignore_topmost_bind = true;
6120 fprintf (file, "{\n");
6121 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
6123 var = TREE_VALUE (vars);
6125 print_generic_decl (file, var, flags);
6126 fprintf (file, "\n");
6128 any_var = true;
6132 if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
6134 /* Make a CFG based dump. */
6135 check_bb_profile (ENTRY_BLOCK_PTR, file);
6136 if (!ignore_topmost_bind)
6137 fprintf (file, "{\n");
6139 if (any_var && n_basic_blocks)
6140 fprintf (file, "\n");
6142 FOR_EACH_BB (bb)
6143 dump_generic_bb (file, bb, 2, flags);
6145 fprintf (file, "}\n");
6146 check_bb_profile (EXIT_BLOCK_PTR, file);
6148 else
6150 int indent;
6152 /* Make a tree based dump. */
6153 chain = DECL_SAVED_TREE (fn);
6155 if (chain && TREE_CODE (chain) == BIND_EXPR)
6157 if (ignore_topmost_bind)
6159 chain = BIND_EXPR_BODY (chain);
6160 indent = 2;
6162 else
6163 indent = 0;
6165 else
6167 if (!ignore_topmost_bind)
6168 fprintf (file, "{\n");
6169 indent = 2;
6172 if (any_var)
6173 fprintf (file, "\n");
6175 print_generic_stmt_indented (file, chain, flags, indent);
6176 if (ignore_topmost_bind)
6177 fprintf (file, "}\n");
6180 fprintf (file, "\n\n");
6182 /* Restore CFUN. */
6183 pop_cfun ();
6187 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
6189 void
6190 debug_function (tree fn, int flags)
6192 dump_function_to_file (fn, stderr, flags);
6196 /* Print on FILE the indexes for the predecessors of basic_block BB. */
6198 static void
6199 print_pred_bbs (FILE *file, basic_block bb)
6201 edge e;
6202 edge_iterator ei;
6204 FOR_EACH_EDGE (e, ei, bb->preds)
6205 fprintf (file, "bb_%d ", e->src->index);
6209 /* Print on FILE the indexes for the successors of basic_block BB. */
6211 static void
6212 print_succ_bbs (FILE *file, basic_block bb)
6214 edge e;
6215 edge_iterator ei;
6217 FOR_EACH_EDGE (e, ei, bb->succs)
6218 fprintf (file, "bb_%d ", e->dest->index);
6221 /* Print to FILE the basic block BB following the VERBOSITY level. */
6223 void
6224 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
6226 char *s_indent = (char *) alloca ((size_t) indent + 1);
6227 memset ((void *) s_indent, ' ', (size_t) indent);
6228 s_indent[indent] = '\0';
6230 /* Print basic_block's header. */
6231 if (verbosity >= 2)
6233 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
6234 print_pred_bbs (file, bb);
6235 fprintf (file, "}, succs = {");
6236 print_succ_bbs (file, bb);
6237 fprintf (file, "})\n");
6240 /* Print basic_block's body. */
6241 if (verbosity >= 3)
6243 fprintf (file, "%s {\n", s_indent);
6244 tree_dump_bb (bb, file, indent + 4);
6245 fprintf (file, "%s }\n", s_indent);
6249 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
6251 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
6252 VERBOSITY level this outputs the contents of the loop, or just its
6253 structure. */
6255 static void
6256 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
6258 char *s_indent;
6259 basic_block bb;
6261 if (loop == NULL)
6262 return;
6264 s_indent = (char *) alloca ((size_t) indent + 1);
6265 memset ((void *) s_indent, ' ', (size_t) indent);
6266 s_indent[indent] = '\0';
6268 /* Print loop's header. */
6269 fprintf (file, "%sloop_%d (header = %d, latch = %d", s_indent,
6270 loop->num, loop->header->index, loop->latch->index);
6271 fprintf (file, ", niter = ");
6272 print_generic_expr (file, loop->nb_iterations, 0);
6274 if (loop->any_upper_bound)
6276 fprintf (file, ", upper_bound = ");
6277 dump_double_int (file, loop->nb_iterations_upper_bound, true);
6280 if (loop->any_estimate)
6282 fprintf (file, ", estimate = ");
6283 dump_double_int (file, loop->nb_iterations_estimate, true);
6285 fprintf (file, ")\n");
6287 /* Print loop's body. */
6288 if (verbosity >= 1)
6290 fprintf (file, "%s{\n", s_indent);
6291 FOR_EACH_BB (bb)
6292 if (bb->loop_father == loop)
6293 print_loops_bb (file, bb, indent, verbosity);
6295 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
6296 fprintf (file, "%s}\n", s_indent);
6300 /* Print the LOOP and its sibling loops on FILE, indented INDENT
6301 spaces. Following VERBOSITY level this outputs the contents of the
6302 loop, or just its structure. */
6304 static void
6305 print_loop_and_siblings (FILE *file, struct loop *loop, int indent, int verbosity)
6307 if (loop == NULL)
6308 return;
6310 print_loop (file, loop, indent, verbosity);
6311 print_loop_and_siblings (file, loop->next, indent, verbosity);
6314 /* Follow a CFG edge from the entry point of the program, and on entry
6315 of a loop, pretty print the loop structure on FILE. */
6317 void
6318 print_loops (FILE *file, int verbosity)
6320 basic_block bb;
6322 bb = BASIC_BLOCK (NUM_FIXED_BLOCKS);
6323 if (bb && bb->loop_father)
6324 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
6328 /* Debugging loops structure at tree level, at some VERBOSITY level. */
6330 void
6331 debug_loops (int verbosity)
6333 print_loops (stderr, verbosity);
6336 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
6338 void
6339 debug_loop (struct loop *loop, int verbosity)
6341 print_loop (stderr, loop, 0, verbosity);
6344 /* Print on stderr the code of loop number NUM, at some VERBOSITY
6345 level. */
6347 void
6348 debug_loop_num (unsigned num, int verbosity)
6350 debug_loop (get_loop (num), verbosity);
6353 /* Return true if BB ends with a call, possibly followed by some
6354 instructions that must stay with the call. Return false,
6355 otherwise. */
6357 static bool
6358 tree_block_ends_with_call_p (basic_block bb)
6360 block_stmt_iterator bsi = bsi_last (bb);
6361 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
6365 /* Return true if BB ends with a conditional branch. Return false,
6366 otherwise. */
6368 static bool
6369 tree_block_ends_with_condjump_p (const_basic_block bb)
6371 /* This CONST_CAST is okay because last_stmt doesn't modify its
6372 argument and the return value is not modified. */
6373 const_tree stmt = last_stmt (CONST_CAST_BB(bb));
6374 return (stmt && TREE_CODE (stmt) == COND_EXPR);
6378 /* Return true if we need to add fake edge to exit at statement T.
6379 Helper function for tree_flow_call_edges_add. */
6381 static bool
6382 need_fake_edge_p (tree t)
6384 tree call;
6386 /* NORETURN and LONGJMP calls already have an edge to exit.
6387 CONST and PURE calls do not need one.
6388 We don't currently check for CONST and PURE here, although
6389 it would be a good idea, because those attributes are
6390 figured out from the RTL in mark_constant_function, and
6391 the counter incrementation code from -fprofile-arcs
6392 leads to different results from -fbranch-probabilities. */
6393 call = get_call_expr_in (t);
6394 if (call
6395 && !(call_expr_flags (call) & ECF_NORETURN))
6396 return true;
6398 if (TREE_CODE (t) == ASM_EXPR
6399 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
6400 return true;
6402 return false;
6406 /* Add fake edges to the function exit for any non constant and non
6407 noreturn calls, volatile inline assembly in the bitmap of blocks
6408 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
6409 the number of blocks that were split.
6411 The goal is to expose cases in which entering a basic block does
6412 not imply that all subsequent instructions must be executed. */
6414 static int
6415 tree_flow_call_edges_add (sbitmap blocks)
6417 int i;
6418 int blocks_split = 0;
6419 int last_bb = last_basic_block;
6420 bool check_last_block = false;
6422 if (n_basic_blocks == NUM_FIXED_BLOCKS)
6423 return 0;
6425 if (! blocks)
6426 check_last_block = true;
6427 else
6428 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
6430 /* In the last basic block, before epilogue generation, there will be
6431 a fallthru edge to EXIT. Special care is required if the last insn
6432 of the last basic block is a call because make_edge folds duplicate
6433 edges, which would result in the fallthru edge also being marked
6434 fake, which would result in the fallthru edge being removed by
6435 remove_fake_edges, which would result in an invalid CFG.
6437 Moreover, we can't elide the outgoing fake edge, since the block
6438 profiler needs to take this into account in order to solve the minimal
6439 spanning tree in the case that the call doesn't return.
6441 Handle this by adding a dummy instruction in a new last basic block. */
6442 if (check_last_block)
6444 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
6445 block_stmt_iterator bsi = bsi_last (bb);
6446 tree t = NULL_TREE;
6447 if (!bsi_end_p (bsi))
6448 t = bsi_stmt (bsi);
6450 if (t && need_fake_edge_p (t))
6452 edge e;
6454 e = find_edge (bb, EXIT_BLOCK_PTR);
6455 if (e)
6457 bsi_insert_on_edge (e, build_empty_stmt ());
6458 bsi_commit_edge_inserts ();
6463 /* Now add fake edges to the function exit for any non constant
6464 calls since there is no way that we can determine if they will
6465 return or not... */
6466 for (i = 0; i < last_bb; i++)
6468 basic_block bb = BASIC_BLOCK (i);
6469 block_stmt_iterator bsi;
6470 tree stmt, last_stmt;
6472 if (!bb)
6473 continue;
6475 if (blocks && !TEST_BIT (blocks, i))
6476 continue;
6478 bsi = bsi_last (bb);
6479 if (!bsi_end_p (bsi))
6481 last_stmt = bsi_stmt (bsi);
6484 stmt = bsi_stmt (bsi);
6485 if (need_fake_edge_p (stmt))
6487 edge e;
6488 /* The handling above of the final block before the
6489 epilogue should be enough to verify that there is
6490 no edge to the exit block in CFG already.
6491 Calling make_edge in such case would cause us to
6492 mark that edge as fake and remove it later. */
6493 #ifdef ENABLE_CHECKING
6494 if (stmt == last_stmt)
6496 e = find_edge (bb, EXIT_BLOCK_PTR);
6497 gcc_assert (e == NULL);
6499 #endif
6501 /* Note that the following may create a new basic block
6502 and renumber the existing basic blocks. */
6503 if (stmt != last_stmt)
6505 e = split_block (bb, stmt);
6506 if (e)
6507 blocks_split++;
6509 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
6511 bsi_prev (&bsi);
6513 while (!bsi_end_p (bsi));
6517 if (blocks_split)
6518 verify_flow_info ();
6520 return blocks_split;
6523 /* Purge dead abnormal call edges from basic block BB. */
6525 bool
6526 tree_purge_dead_abnormal_call_edges (basic_block bb)
6528 bool changed = tree_purge_dead_eh_edges (bb);
6530 if (current_function_has_nonlocal_label)
6532 tree stmt = last_stmt (bb);
6533 edge_iterator ei;
6534 edge e;
6536 if (!(stmt && tree_can_make_abnormal_goto (stmt)))
6537 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6539 if (e->flags & EDGE_ABNORMAL)
6541 remove_edge (e);
6542 changed = true;
6544 else
6545 ei_next (&ei);
6548 /* See tree_purge_dead_eh_edges below. */
6549 if (changed)
6550 free_dominance_info (CDI_DOMINATORS);
6553 return changed;
6556 /* Stores all basic blocks dominated by BB to DOM_BBS. */
6558 static void
6559 get_all_dominated_blocks (basic_block bb, VEC (basic_block, heap) **dom_bbs)
6561 basic_block son;
6563 VEC_safe_push (basic_block, heap, *dom_bbs, bb);
6564 for (son = first_dom_son (CDI_DOMINATORS, bb);
6565 son;
6566 son = next_dom_son (CDI_DOMINATORS, son))
6567 get_all_dominated_blocks (son, dom_bbs);
6570 /* Removes edge E and all the blocks dominated by it, and updates dominance
6571 information. The IL in E->src needs to be updated separately.
6572 If dominance info is not available, only the edge E is removed.*/
6574 void
6575 remove_edge_and_dominated_blocks (edge e)
6577 VEC (basic_block, heap) *bbs_to_remove = NULL;
6578 VEC (basic_block, heap) *bbs_to_fix_dom = NULL;
6579 bitmap df, df_idom;
6580 edge f;
6581 edge_iterator ei;
6582 bool none_removed = false;
6583 unsigned i;
6584 basic_block bb, dbb;
6585 bitmap_iterator bi;
6587 if (!dom_info_available_p (CDI_DOMINATORS))
6589 remove_edge (e);
6590 return;
6593 /* No updating is needed for edges to exit. */
6594 if (e->dest == EXIT_BLOCK_PTR)
6596 if (cfgcleanup_altered_bbs)
6597 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
6598 remove_edge (e);
6599 return;
6602 /* First, we find the basic blocks to remove. If E->dest has a predecessor
6603 that is not dominated by E->dest, then this set is empty. Otherwise,
6604 all the basic blocks dominated by E->dest are removed.
6606 Also, to DF_IDOM we store the immediate dominators of the blocks in
6607 the dominance frontier of E (i.e., of the successors of the
6608 removed blocks, if there are any, and of E->dest otherwise). */
6609 FOR_EACH_EDGE (f, ei, e->dest->preds)
6611 if (f == e)
6612 continue;
6614 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
6616 none_removed = true;
6617 break;
6621 df = BITMAP_ALLOC (NULL);
6622 df_idom = BITMAP_ALLOC (NULL);
6624 if (none_removed)
6625 bitmap_set_bit (df_idom,
6626 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
6627 else
6629 get_all_dominated_blocks (e->dest, &bbs_to_remove);
6630 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6632 FOR_EACH_EDGE (f, ei, bb->succs)
6634 if (f->dest != EXIT_BLOCK_PTR)
6635 bitmap_set_bit (df, f->dest->index);
6638 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6639 bitmap_clear_bit (df, bb->index);
6641 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
6643 bb = BASIC_BLOCK (i);
6644 bitmap_set_bit (df_idom,
6645 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
6649 if (cfgcleanup_altered_bbs)
6651 /* Record the set of the altered basic blocks. */
6652 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
6653 bitmap_ior_into (cfgcleanup_altered_bbs, df);
6656 /* Remove E and the cancelled blocks. */
6657 if (none_removed)
6658 remove_edge (e);
6659 else
6661 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6662 delete_basic_block (bb);
6665 /* Update the dominance information. The immediate dominator may change only
6666 for blocks whose immediate dominator belongs to DF_IDOM:
6668 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
6669 removal. Let Z the arbitrary block such that idom(Z) = Y and
6670 Z dominates X after the removal. Before removal, there exists a path P
6671 from Y to X that avoids Z. Let F be the last edge on P that is
6672 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
6673 dominates W, and because of P, Z does not dominate W), and W belongs to
6674 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
6675 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
6677 bb = BASIC_BLOCK (i);
6678 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
6679 dbb;
6680 dbb = next_dom_son (CDI_DOMINATORS, dbb))
6681 VEC_safe_push (basic_block, heap, bbs_to_fix_dom, dbb);
6684 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
6686 BITMAP_FREE (df);
6687 BITMAP_FREE (df_idom);
6688 VEC_free (basic_block, heap, bbs_to_remove);
6689 VEC_free (basic_block, heap, bbs_to_fix_dom);
6692 /* Purge dead EH edges from basic block BB. */
6694 bool
6695 tree_purge_dead_eh_edges (basic_block bb)
6697 bool changed = false;
6698 edge e;
6699 edge_iterator ei;
6700 tree stmt = last_stmt (bb);
6702 if (stmt && tree_can_throw_internal (stmt))
6703 return false;
6705 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6707 if (e->flags & EDGE_EH)
6709 remove_edge_and_dominated_blocks (e);
6710 changed = true;
6712 else
6713 ei_next (&ei);
6716 return changed;
6719 bool
6720 tree_purge_all_dead_eh_edges (const_bitmap blocks)
6722 bool changed = false;
6723 unsigned i;
6724 bitmap_iterator bi;
6726 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
6728 changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
6731 return changed;
6734 /* This function is called whenever a new edge is created or
6735 redirected. */
6737 static void
6738 tree_execute_on_growing_pred (edge e)
6740 basic_block bb = e->dest;
6742 if (phi_nodes (bb))
6743 reserve_phi_args_for_new_edge (bb);
6746 /* This function is called immediately before edge E is removed from
6747 the edge vector E->dest->preds. */
6749 static void
6750 tree_execute_on_shrinking_pred (edge e)
6752 if (phi_nodes (e->dest))
6753 remove_phi_args (e);
6756 /*---------------------------------------------------------------------------
6757 Helper functions for Loop versioning
6758 ---------------------------------------------------------------------------*/
6760 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
6761 of 'first'. Both of them are dominated by 'new_head' basic block. When
6762 'new_head' was created by 'second's incoming edge it received phi arguments
6763 on the edge by split_edge(). Later, additional edge 'e' was created to
6764 connect 'new_head' and 'first'. Now this routine adds phi args on this
6765 additional edge 'e' that new_head to second edge received as part of edge
6766 splitting.
6769 static void
6770 tree_lv_adjust_loop_header_phi (basic_block first, basic_block second,
6771 basic_block new_head, edge e)
6773 tree phi1, phi2;
6774 edge e2 = find_edge (new_head, second);
6776 /* Because NEW_HEAD has been created by splitting SECOND's incoming
6777 edge, we should always have an edge from NEW_HEAD to SECOND. */
6778 gcc_assert (e2 != NULL);
6780 /* Browse all 'second' basic block phi nodes and add phi args to
6781 edge 'e' for 'first' head. PHI args are always in correct order. */
6783 for (phi2 = phi_nodes (second), phi1 = phi_nodes (first);
6784 phi2 && phi1;
6785 phi2 = PHI_CHAIN (phi2), phi1 = PHI_CHAIN (phi1))
6787 tree def = PHI_ARG_DEF (phi2, e2->dest_idx);
6788 add_phi_arg (phi1, def, e);
6792 /* Adds a if else statement to COND_BB with condition COND_EXPR.
6793 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
6794 the destination of the ELSE part. */
6795 static void
6796 tree_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
6797 basic_block second_head ATTRIBUTE_UNUSED,
6798 basic_block cond_bb, void *cond_e)
6800 block_stmt_iterator bsi;
6801 tree new_cond_expr = NULL_TREE;
6802 tree cond_expr = (tree) cond_e;
6803 edge e0;
6805 /* Build new conditional expr */
6806 new_cond_expr = build3 (COND_EXPR, void_type_node, cond_expr,
6807 NULL_TREE, NULL_TREE);
6809 /* Add new cond in cond_bb. */
6810 bsi = bsi_start (cond_bb);
6811 bsi_insert_after (&bsi, new_cond_expr, BSI_NEW_STMT);
6812 /* Adjust edges appropriately to connect new head with first head
6813 as well as second head. */
6814 e0 = single_succ_edge (cond_bb);
6815 e0->flags &= ~EDGE_FALLTHRU;
6816 e0->flags |= EDGE_FALSE_VALUE;
6819 struct cfg_hooks tree_cfg_hooks = {
6820 "tree",
6821 tree_verify_flow_info,
6822 tree_dump_bb, /* dump_bb */
6823 create_bb, /* create_basic_block */
6824 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
6825 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
6826 tree_can_remove_branch_p, /* can_remove_branch_p */
6827 remove_bb, /* delete_basic_block */
6828 tree_split_block, /* split_block */
6829 tree_move_block_after, /* move_block_after */
6830 tree_can_merge_blocks_p, /* can_merge_blocks_p */
6831 tree_merge_blocks, /* merge_blocks */
6832 tree_predict_edge, /* predict_edge */
6833 tree_predicted_by_p, /* predicted_by_p */
6834 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
6835 tree_duplicate_bb, /* duplicate_block */
6836 tree_split_edge, /* split_edge */
6837 tree_make_forwarder_block, /* make_forward_block */
6838 NULL, /* tidy_fallthru_edge */
6839 tree_block_ends_with_call_p, /* block_ends_with_call_p */
6840 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
6841 tree_flow_call_edges_add, /* flow_call_edges_add */
6842 tree_execute_on_growing_pred, /* execute_on_growing_pred */
6843 tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
6844 tree_duplicate_loop_to_header_edge, /* duplicate loop for trees */
6845 tree_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
6846 tree_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
6847 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
6848 flush_pending_stmts /* flush_pending_stmts */
6852 /* Split all critical edges. */
6854 static unsigned int
6855 split_critical_edges (void)
6857 basic_block bb;
6858 edge e;
6859 edge_iterator ei;
6861 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
6862 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
6863 mappings around the calls to split_edge. */
6864 start_recording_case_labels ();
6865 FOR_ALL_BB (bb)
6867 FOR_EACH_EDGE (e, ei, bb->succs)
6868 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
6870 split_edge (e);
6873 end_recording_case_labels ();
6874 return 0;
6877 struct tree_opt_pass pass_split_crit_edges =
6879 "crited", /* name */
6880 NULL, /* gate */
6881 split_critical_edges, /* execute */
6882 NULL, /* sub */
6883 NULL, /* next */
6884 0, /* static_pass_number */
6885 TV_TREE_SPLIT_EDGES, /* tv_id */
6886 PROP_cfg, /* properties required */
6887 PROP_no_crit_edges, /* properties_provided */
6888 0, /* properties_destroyed */
6889 0, /* todo_flags_start */
6890 TODO_dump_func, /* todo_flags_finish */
6891 0 /* letter */
6895 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
6896 a temporary, make sure and register it to be renamed if necessary,
6897 and finally return the temporary. Put the statements to compute
6898 EXP before the current statement in BSI. */
6900 tree
6901 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
6903 tree t, new_stmt, orig_stmt;
6905 if (is_gimple_val (exp))
6906 return exp;
6908 t = make_rename_temp (type, NULL);
6909 new_stmt = build_gimple_modify_stmt (t, exp);
6911 orig_stmt = bsi_stmt (*bsi);
6912 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
6913 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
6915 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
6916 if (gimple_in_ssa_p (cfun))
6917 mark_symbols_for_renaming (new_stmt);
6919 return t;
6922 /* Build a ternary operation and gimplify it. Emit code before BSI.
6923 Return the gimple_val holding the result. */
6925 tree
6926 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
6927 tree type, tree a, tree b, tree c)
6929 tree ret;
6931 ret = fold_build3 (code, type, a, b, c);
6932 STRIP_NOPS (ret);
6934 return gimplify_val (bsi, type, ret);
6937 /* Build a binary operation and gimplify it. Emit code before BSI.
6938 Return the gimple_val holding the result. */
6940 tree
6941 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
6942 tree type, tree a, tree b)
6944 tree ret;
6946 ret = fold_build2 (code, type, a, b);
6947 STRIP_NOPS (ret);
6949 return gimplify_val (bsi, type, ret);
6952 /* Build a unary operation and gimplify it. Emit code before BSI.
6953 Return the gimple_val holding the result. */
6955 tree
6956 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
6957 tree a)
6959 tree ret;
6961 ret = fold_build1 (code, type, a);
6962 STRIP_NOPS (ret);
6964 return gimplify_val (bsi, type, ret);
6969 /* Emit return warnings. */
6971 static unsigned int
6972 execute_warn_function_return (void)
6974 #ifdef USE_MAPPED_LOCATION
6975 source_location location;
6976 #else
6977 location_t *locus;
6978 #endif
6979 tree last;
6980 edge e;
6981 edge_iterator ei;
6983 /* If we have a path to EXIT, then we do return. */
6984 if (TREE_THIS_VOLATILE (cfun->decl)
6985 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
6987 #ifdef USE_MAPPED_LOCATION
6988 location = UNKNOWN_LOCATION;
6989 #else
6990 locus = NULL;
6991 #endif
6992 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6994 last = last_stmt (e->src);
6995 if (TREE_CODE (last) == RETURN_EXPR
6996 #ifdef USE_MAPPED_LOCATION
6997 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
6998 #else
6999 && (locus = EXPR_LOCUS (last)) != NULL)
7000 #endif
7001 break;
7003 #ifdef USE_MAPPED_LOCATION
7004 if (location == UNKNOWN_LOCATION)
7005 location = cfun->function_end_locus;
7006 warning (0, "%H%<noreturn%> function does return", &location);
7007 #else
7008 if (!locus)
7009 locus = &cfun->function_end_locus;
7010 warning (0, "%H%<noreturn%> function does return", locus);
7011 #endif
7014 /* If we see "return;" in some basic block, then we do reach the end
7015 without returning a value. */
7016 else if (warn_return_type
7017 && !TREE_NO_WARNING (cfun->decl)
7018 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
7019 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
7021 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7023 tree last = last_stmt (e->src);
7024 if (TREE_CODE (last) == RETURN_EXPR
7025 && TREE_OPERAND (last, 0) == NULL
7026 && !TREE_NO_WARNING (last))
7028 #ifdef USE_MAPPED_LOCATION
7029 location = EXPR_LOCATION (last);
7030 if (location == UNKNOWN_LOCATION)
7031 location = cfun->function_end_locus;
7032 warning (OPT_Wreturn_type, "%Hcontrol reaches end of non-void function", &location);
7033 #else
7034 locus = EXPR_LOCUS (last);
7035 if (!locus)
7036 locus = &cfun->function_end_locus;
7037 warning (OPT_Wreturn_type, "%Hcontrol reaches end of non-void function", locus);
7038 #endif
7039 TREE_NO_WARNING (cfun->decl) = 1;
7040 break;
7044 return 0;
7048 /* Given a basic block B which ends with a conditional and has
7049 precisely two successors, determine which of the edges is taken if
7050 the conditional is true and which is taken if the conditional is
7051 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
7053 void
7054 extract_true_false_edges_from_block (basic_block b,
7055 edge *true_edge,
7056 edge *false_edge)
7058 edge e = EDGE_SUCC (b, 0);
7060 if (e->flags & EDGE_TRUE_VALUE)
7062 *true_edge = e;
7063 *false_edge = EDGE_SUCC (b, 1);
7065 else
7067 *false_edge = e;
7068 *true_edge = EDGE_SUCC (b, 1);
7072 struct tree_opt_pass pass_warn_function_return =
7074 NULL, /* name */
7075 NULL, /* gate */
7076 execute_warn_function_return, /* execute */
7077 NULL, /* sub */
7078 NULL, /* next */
7079 0, /* static_pass_number */
7080 0, /* tv_id */
7081 PROP_cfg, /* properties_required */
7082 0, /* properties_provided */
7083 0, /* properties_destroyed */
7084 0, /* todo_flags_start */
7085 0, /* todo_flags_finish */
7086 0 /* letter */
7089 /* Emit noreturn warnings. */
7091 static unsigned int
7092 execute_warn_function_noreturn (void)
7094 if (warn_missing_noreturn
7095 && !TREE_THIS_VOLATILE (cfun->decl)
7096 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
7097 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
7098 warning (OPT_Wmissing_noreturn, "%Jfunction might be possible candidate "
7099 "for attribute %<noreturn%>",
7100 cfun->decl);
7101 return 0;
7104 struct tree_opt_pass pass_warn_function_noreturn =
7106 NULL, /* name */
7107 NULL, /* gate */
7108 execute_warn_function_noreturn, /* execute */
7109 NULL, /* sub */
7110 NULL, /* next */
7111 0, /* static_pass_number */
7112 0, /* tv_id */
7113 PROP_cfg, /* properties_required */
7114 0, /* properties_provided */
7115 0, /* properties_destroyed */
7116 0, /* todo_flags_start */
7117 0, /* todo_flags_finish */
7118 0 /* letter */