Enable dumping of alias graphs.
[official-gcc/Ramakrishna.git] / gcc / tree-cfg.c
blob81d95d75e6e74e8add5ce34f30ab2fc7f28eed87
1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 GIMPLE_SWITCHes.
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 /* Hash table to store last discriminator assigned for each locus. */
86 struct locus_discrim_map
88 location_t locus;
89 int discriminator;
91 static htab_t discriminator_per_locus;
93 /* Basic blocks and flowgraphs. */
94 static void make_blocks (gimple_seq);
95 static void factor_computed_gotos (void);
97 /* Edges. */
98 static void make_edges (void);
99 static void make_cond_expr_edges (basic_block);
100 static void make_gimple_switch_edges (basic_block);
101 static void make_goto_expr_edges (basic_block);
102 static unsigned int locus_map_hash (const void *);
103 static int locus_map_eq (const void *, const void *);
104 static void assign_discriminator (location_t, basic_block);
105 static edge gimple_redirect_edge_and_branch (edge, basic_block);
106 static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
107 static unsigned int split_critical_edges (void);
109 /* Various helpers. */
110 static inline bool stmt_starts_bb_p (gimple, gimple);
111 static int gimple_verify_flow_info (void);
112 static void gimple_make_forwarder_block (edge);
113 static void gimple_cfg2vcg (FILE *);
114 static gimple first_non_label_stmt (basic_block);
116 /* Flowgraph optimization and cleanup. */
117 static void gimple_merge_blocks (basic_block, basic_block);
118 static bool gimple_can_merge_blocks_p (basic_block, basic_block);
119 static void remove_bb (basic_block);
120 static edge find_taken_edge_computed_goto (basic_block, tree);
121 static edge find_taken_edge_cond_expr (basic_block, tree);
122 static edge find_taken_edge_switch_expr (basic_block, tree);
123 static tree find_case_label_for_value (gimple, tree);
125 void
126 init_empty_tree_cfg_for_function (struct function *fn)
128 /* Initialize the basic block array. */
129 init_flow (fn);
130 profile_status_for_function (fn) = PROFILE_ABSENT;
131 n_basic_blocks_for_function (fn) = NUM_FIXED_BLOCKS;
132 last_basic_block_for_function (fn) = NUM_FIXED_BLOCKS;
133 basic_block_info_for_function (fn)
134 = VEC_alloc (basic_block, gc, initial_cfg_capacity);
135 VEC_safe_grow_cleared (basic_block, gc,
136 basic_block_info_for_function (fn),
137 initial_cfg_capacity);
139 /* Build a mapping of labels to their associated blocks. */
140 label_to_block_map_for_function (fn)
141 = VEC_alloc (basic_block, gc, initial_cfg_capacity);
142 VEC_safe_grow_cleared (basic_block, gc,
143 label_to_block_map_for_function (fn),
144 initial_cfg_capacity);
146 SET_BASIC_BLOCK_FOR_FUNCTION (fn, ENTRY_BLOCK,
147 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn));
148 SET_BASIC_BLOCK_FOR_FUNCTION (fn, EXIT_BLOCK,
149 EXIT_BLOCK_PTR_FOR_FUNCTION (fn));
151 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn)->next_bb
152 = EXIT_BLOCK_PTR_FOR_FUNCTION (fn);
153 EXIT_BLOCK_PTR_FOR_FUNCTION (fn)->prev_bb
154 = ENTRY_BLOCK_PTR_FOR_FUNCTION (fn);
157 void
158 init_empty_tree_cfg (void)
160 init_empty_tree_cfg_for_function (cfun);
163 /*---------------------------------------------------------------------------
164 Create basic blocks
165 ---------------------------------------------------------------------------*/
167 /* Entry point to the CFG builder for trees. SEQ is the sequence of
168 statements to be added to the flowgraph. */
170 static void
171 build_gimple_cfg (gimple_seq seq)
173 /* Register specific gimple functions. */
174 gimple_register_cfg_hooks ();
176 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
178 init_empty_tree_cfg ();
180 found_computed_goto = 0;
181 make_blocks (seq);
183 /* Computed gotos are hell to deal with, especially if there are
184 lots of them with a large number of destinations. So we factor
185 them to a common computed goto location before we build the
186 edge list. After we convert back to normal form, we will un-factor
187 the computed gotos since factoring introduces an unwanted jump. */
188 if (found_computed_goto)
189 factor_computed_gotos ();
191 /* Make sure there is always at least one block, even if it's empty. */
192 if (n_basic_blocks == NUM_FIXED_BLOCKS)
193 create_empty_bb (ENTRY_BLOCK_PTR);
195 /* Adjust the size of the array. */
196 if (VEC_length (basic_block, basic_block_info) < (size_t) n_basic_blocks)
197 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, n_basic_blocks);
199 /* To speed up statement iterator walks, we first purge dead labels. */
200 cleanup_dead_labels ();
202 /* Group case nodes to reduce the number of edges.
203 We do this after cleaning up dead labels because otherwise we miss
204 a lot of obvious case merging opportunities. */
205 group_case_labels ();
207 /* Create the edges of the flowgraph. */
208 discriminator_per_locus = htab_create (13, locus_map_hash, locus_map_eq,
209 free);
210 make_edges ();
211 cleanup_dead_labels ();
212 htab_delete (discriminator_per_locus);
214 /* Debugging dumps. */
216 /* Write the flowgraph to a VCG file. */
218 int local_dump_flags;
219 FILE *vcg_file = dump_begin (TDI_vcg, &local_dump_flags);
220 if (vcg_file)
222 gimple_cfg2vcg (vcg_file);
223 dump_end (TDI_vcg, vcg_file);
227 #ifdef ENABLE_CHECKING
228 verify_stmts ();
229 #endif
232 static unsigned int
233 execute_build_cfg (void)
235 gimple_seq body = gimple_body (current_function_decl);
237 build_gimple_cfg (body);
238 gimple_set_body (current_function_decl, NULL);
239 if (dump_file && (dump_flags & TDF_DETAILS))
241 fprintf (dump_file, "Scope blocks:\n");
242 dump_scope_blocks (dump_file, dump_flags);
244 return 0;
247 struct gimple_opt_pass pass_build_cfg =
250 GIMPLE_PASS,
251 "cfg", /* name */
252 NULL, /* gate */
253 execute_build_cfg, /* execute */
254 NULL, /* sub */
255 NULL, /* next */
256 0, /* static_pass_number */
257 TV_TREE_CFG, /* tv_id */
258 PROP_gimple_leh, /* properties_required */
259 PROP_cfg, /* properties_provided */
260 0, /* properties_destroyed */
261 0, /* todo_flags_start */
262 TODO_verify_stmts | TODO_cleanup_cfg
263 | TODO_dump_func /* todo_flags_finish */
268 /* Return true if T is a computed goto. */
270 static bool
271 computed_goto_p (gimple t)
273 return (gimple_code (t) == GIMPLE_GOTO
274 && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
278 /* Search the CFG for any computed gotos. If found, factor them to a
279 common computed goto site. Also record the location of that site so
280 that we can un-factor the gotos after we have converted back to
281 normal form. */
283 static void
284 factor_computed_gotos (void)
286 basic_block bb;
287 tree factored_label_decl = NULL;
288 tree var = NULL;
289 gimple factored_computed_goto_label = NULL;
290 gimple factored_computed_goto = NULL;
292 /* We know there are one or more computed gotos in this function.
293 Examine the last statement in each basic block to see if the block
294 ends with a computed goto. */
296 FOR_EACH_BB (bb)
298 gimple_stmt_iterator gsi = gsi_last_bb (bb);
299 gimple last;
301 if (gsi_end_p (gsi))
302 continue;
304 last = gsi_stmt (gsi);
306 /* Ignore the computed goto we create when we factor the original
307 computed gotos. */
308 if (last == factored_computed_goto)
309 continue;
311 /* If the last statement is a computed goto, factor it. */
312 if (computed_goto_p (last))
314 gimple assignment;
316 /* The first time we find a computed goto we need to create
317 the factored goto block and the variable each original
318 computed goto will use for their goto destination. */
319 if (!factored_computed_goto)
321 basic_block new_bb = create_empty_bb (bb);
322 gimple_stmt_iterator new_gsi = gsi_start_bb (new_bb);
324 /* Create the destination of the factored goto. Each original
325 computed goto will put its desired destination into this
326 variable and jump to the label we create immediately
327 below. */
328 var = create_tmp_var (ptr_type_node, "gotovar");
330 /* Build a label for the new block which will contain the
331 factored computed goto. */
332 factored_label_decl = create_artificial_label (UNKNOWN_LOCATION);
333 factored_computed_goto_label
334 = gimple_build_label (factored_label_decl);
335 gsi_insert_after (&new_gsi, factored_computed_goto_label,
336 GSI_NEW_STMT);
338 /* Build our new computed goto. */
339 factored_computed_goto = gimple_build_goto (var);
340 gsi_insert_after (&new_gsi, factored_computed_goto, GSI_NEW_STMT);
343 /* Copy the original computed goto's destination into VAR. */
344 assignment = gimple_build_assign (var, gimple_goto_dest (last));
345 gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
347 /* And re-vector the computed goto to the new destination. */
348 gimple_goto_set_dest (last, factored_label_decl);
354 /* Build a flowgraph for the sequence of stmts SEQ. */
356 static void
357 make_blocks (gimple_seq seq)
359 gimple_stmt_iterator i = gsi_start (seq);
360 gimple stmt = NULL;
361 bool start_new_block = true;
362 bool first_stmt_of_seq = true;
363 basic_block bb = ENTRY_BLOCK_PTR;
365 while (!gsi_end_p (i))
367 gimple prev_stmt;
369 prev_stmt = stmt;
370 stmt = gsi_stmt (i);
372 /* If the statement starts a new basic block or if we have determined
373 in a previous pass that we need to create a new block for STMT, do
374 so now. */
375 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
377 if (!first_stmt_of_seq)
378 seq = gsi_split_seq_before (&i);
379 bb = create_basic_block (seq, NULL, bb);
380 start_new_block = false;
383 /* Now add STMT to BB and create the subgraphs for special statement
384 codes. */
385 gimple_set_bb (stmt, bb);
387 if (computed_goto_p (stmt))
388 found_computed_goto = true;
390 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
391 next iteration. */
392 if (stmt_ends_bb_p (stmt))
394 /* If the stmt can make abnormal goto use a new temporary
395 for the assignment to the LHS. This makes sure the old value
396 of the LHS is available on the abnormal edge. Otherwise
397 we will end up with overlapping life-ranges for abnormal
398 SSA names. */
399 if (gimple_has_lhs (stmt)
400 && stmt_can_make_abnormal_goto (stmt)
401 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
403 tree lhs = gimple_get_lhs (stmt);
404 tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
405 gimple s = gimple_build_assign (lhs, tmp);
406 gimple_set_location (s, gimple_location (stmt));
407 gimple_set_block (s, gimple_block (stmt));
408 gimple_set_lhs (stmt, tmp);
409 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
410 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
411 DECL_GIMPLE_REG_P (tmp) = 1;
412 gsi_insert_after (&i, s, GSI_SAME_STMT);
414 start_new_block = true;
417 gsi_next (&i);
418 first_stmt_of_seq = false;
423 /* Create and return a new empty basic block after bb AFTER. */
425 static basic_block
426 create_bb (void *h, void *e, basic_block after)
428 basic_block bb;
430 gcc_assert (!e);
432 /* Create and initialize a new basic block. Since alloc_block uses
433 ggc_alloc_cleared to allocate a basic block, we do not have to
434 clear the newly allocated basic block here. */
435 bb = alloc_block ();
437 bb->index = last_basic_block;
438 bb->flags = BB_NEW;
439 bb->il.gimple = GGC_CNEW (struct gimple_bb_info);
440 set_bb_seq (bb, h ? (gimple_seq) h : gimple_seq_alloc ());
442 /* Add the new block to the linked list of blocks. */
443 link_block (bb, after);
445 /* Grow the basic block array if needed. */
446 if ((size_t) last_basic_block == VEC_length (basic_block, basic_block_info))
448 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
449 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
452 /* Add the newly created block to the array. */
453 SET_BASIC_BLOCK (last_basic_block, bb);
455 n_basic_blocks++;
456 last_basic_block++;
458 return bb;
462 /*---------------------------------------------------------------------------
463 Edge creation
464 ---------------------------------------------------------------------------*/
466 /* Fold COND_EXPR_COND of each COND_EXPR. */
468 void
469 fold_cond_expr_cond (void)
471 basic_block bb;
473 FOR_EACH_BB (bb)
475 gimple stmt = last_stmt (bb);
477 if (stmt && gimple_code (stmt) == GIMPLE_COND)
479 location_t loc = gimple_location (stmt);
480 tree cond;
481 bool zerop, onep;
483 fold_defer_overflow_warnings ();
484 cond = fold_binary_loc (loc, gimple_cond_code (stmt), boolean_type_node,
485 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
486 if (cond)
488 zerop = integer_zerop (cond);
489 onep = integer_onep (cond);
491 else
492 zerop = onep = false;
494 fold_undefer_overflow_warnings (zerop || onep,
495 stmt,
496 WARN_STRICT_OVERFLOW_CONDITIONAL);
497 if (zerop)
498 gimple_cond_make_false (stmt);
499 else if (onep)
500 gimple_cond_make_true (stmt);
505 /* Join all the blocks in the flowgraph. */
507 static void
508 make_edges (void)
510 basic_block bb;
511 struct omp_region *cur_region = NULL;
513 /* Create an edge from entry to the first block with executable
514 statements in it. */
515 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (NUM_FIXED_BLOCKS), EDGE_FALLTHRU);
517 /* Traverse the basic block array placing edges. */
518 FOR_EACH_BB (bb)
520 gimple last = last_stmt (bb);
521 bool fallthru;
523 if (last)
525 enum gimple_code code = gimple_code (last);
526 switch (code)
528 case GIMPLE_GOTO:
529 make_goto_expr_edges (bb);
530 fallthru = false;
531 break;
532 case GIMPLE_RETURN:
533 make_edge (bb, EXIT_BLOCK_PTR, 0);
534 fallthru = false;
535 break;
536 case GIMPLE_COND:
537 make_cond_expr_edges (bb);
538 fallthru = false;
539 break;
540 case GIMPLE_SWITCH:
541 make_gimple_switch_edges (bb);
542 fallthru = false;
543 break;
544 case GIMPLE_RESX:
545 make_eh_edges (last);
546 fallthru = false;
547 break;
549 case GIMPLE_CALL:
550 /* If this function receives a nonlocal goto, then we need to
551 make edges from this call site to all the nonlocal goto
552 handlers. */
553 if (stmt_can_make_abnormal_goto (last))
554 make_abnormal_goto_edges (bb, true);
556 /* If this statement has reachable exception handlers, then
557 create abnormal edges to them. */
558 make_eh_edges (last);
560 /* Some calls are known not to return. */
561 fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
562 break;
564 case GIMPLE_ASSIGN:
565 /* A GIMPLE_ASSIGN may throw internally and thus be considered
566 control-altering. */
567 if (is_ctrl_altering_stmt (last))
569 make_eh_edges (last);
571 fallthru = true;
572 break;
574 case GIMPLE_OMP_PARALLEL:
575 case GIMPLE_OMP_TASK:
576 case GIMPLE_OMP_FOR:
577 case GIMPLE_OMP_SINGLE:
578 case GIMPLE_OMP_MASTER:
579 case GIMPLE_OMP_ORDERED:
580 case GIMPLE_OMP_CRITICAL:
581 case GIMPLE_OMP_SECTION:
582 cur_region = new_omp_region (bb, code, cur_region);
583 fallthru = true;
584 break;
586 case GIMPLE_OMP_SECTIONS:
587 cur_region = new_omp_region (bb, code, cur_region);
588 fallthru = true;
589 break;
591 case GIMPLE_OMP_SECTIONS_SWITCH:
592 fallthru = false;
593 break;
596 case GIMPLE_OMP_ATOMIC_LOAD:
597 case GIMPLE_OMP_ATOMIC_STORE:
598 fallthru = true;
599 break;
602 case GIMPLE_OMP_RETURN:
603 /* In the case of a GIMPLE_OMP_SECTION, the edge will go
604 somewhere other than the next block. This will be
605 created later. */
606 cur_region->exit = bb;
607 fallthru = cur_region->type != GIMPLE_OMP_SECTION;
608 cur_region = cur_region->outer;
609 break;
611 case GIMPLE_OMP_CONTINUE:
612 cur_region->cont = bb;
613 switch (cur_region->type)
615 case GIMPLE_OMP_FOR:
616 /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
617 succs edges as abnormal to prevent splitting
618 them. */
619 single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
620 /* Make the loopback edge. */
621 make_edge (bb, single_succ (cur_region->entry),
622 EDGE_ABNORMAL);
624 /* Create an edge from GIMPLE_OMP_FOR to exit, which
625 corresponds to the case that the body of the loop
626 is not executed at all. */
627 make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
628 make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
629 fallthru = false;
630 break;
632 case GIMPLE_OMP_SECTIONS:
633 /* Wire up the edges into and out of the nested sections. */
635 basic_block switch_bb = single_succ (cur_region->entry);
637 struct omp_region *i;
638 for (i = cur_region->inner; i ; i = i->next)
640 gcc_assert (i->type == GIMPLE_OMP_SECTION);
641 make_edge (switch_bb, i->entry, 0);
642 make_edge (i->exit, bb, EDGE_FALLTHRU);
645 /* Make the loopback edge to the block with
646 GIMPLE_OMP_SECTIONS_SWITCH. */
647 make_edge (bb, switch_bb, 0);
649 /* Make the edge from the switch to exit. */
650 make_edge (switch_bb, bb->next_bb, 0);
651 fallthru = false;
653 break;
655 default:
656 gcc_unreachable ();
658 break;
660 default:
661 gcc_assert (!stmt_ends_bb_p (last));
662 fallthru = true;
665 else
666 fallthru = true;
668 if (fallthru)
670 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
671 if (last)
672 assign_discriminator (gimple_location (last), bb->next_bb);
676 if (root_omp_region)
677 free_omp_regions ();
679 /* Fold COND_EXPR_COND of each COND_EXPR. */
680 fold_cond_expr_cond ();
683 /* Trivial hash function for a location_t. ITEM is a pointer to
684 a hash table entry that maps a location_t to a discriminator. */
686 static unsigned int
687 locus_map_hash (const void *item)
689 return ((const struct locus_discrim_map *) item)->locus;
692 /* Equality function for the locus-to-discriminator map. VA and VB
693 point to the two hash table entries to compare. */
695 static int
696 locus_map_eq (const void *va, const void *vb)
698 const struct locus_discrim_map *a = (const struct locus_discrim_map *) va;
699 const struct locus_discrim_map *b = (const struct locus_discrim_map *) vb;
700 return a->locus == b->locus;
703 /* Find the next available discriminator value for LOCUS. The
704 discriminator distinguishes among several basic blocks that
705 share a common locus, allowing for more accurate sample-based
706 profiling. */
708 static int
709 next_discriminator_for_locus (location_t locus)
711 struct locus_discrim_map item;
712 struct locus_discrim_map **slot;
714 item.locus = locus;
715 item.discriminator = 0;
716 slot = (struct locus_discrim_map **)
717 htab_find_slot_with_hash (discriminator_per_locus, (void *) &item,
718 (hashval_t) locus, INSERT);
719 gcc_assert (slot);
720 if (*slot == HTAB_EMPTY_ENTRY)
722 *slot = XNEW (struct locus_discrim_map);
723 gcc_assert (*slot);
724 (*slot)->locus = locus;
725 (*slot)->discriminator = 0;
727 (*slot)->discriminator++;
728 return (*slot)->discriminator;
731 /* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line. */
733 static bool
734 same_line_p (location_t locus1, location_t locus2)
736 expanded_location from, to;
738 if (locus1 == locus2)
739 return true;
741 from = expand_location (locus1);
742 to = expand_location (locus2);
744 if (from.line != to.line)
745 return false;
746 if (from.file == to.file)
747 return true;
748 return (from.file != NULL
749 && to.file != NULL
750 && strcmp (from.file, to.file) == 0);
753 /* Assign a unique discriminator value to block BB if it begins at the same
754 LOCUS as its predecessor block. */
756 static void
757 assign_discriminator (location_t locus, basic_block bb)
759 gimple first_in_to_bb, last_in_to_bb;
761 if (locus == 0 || bb->discriminator != 0)
762 return;
764 first_in_to_bb = first_non_label_stmt (bb);
765 last_in_to_bb = last_stmt (bb);
766 if ((first_in_to_bb && same_line_p (locus, gimple_location (first_in_to_bb)))
767 || (last_in_to_bb && same_line_p (locus, gimple_location (last_in_to_bb))))
768 bb->discriminator = next_discriminator_for_locus (locus);
771 /* Create the edges for a GIMPLE_COND starting at block BB. */
773 static void
774 make_cond_expr_edges (basic_block bb)
776 gimple entry = last_stmt (bb);
777 gimple then_stmt, else_stmt;
778 basic_block then_bb, else_bb;
779 tree then_label, else_label;
780 edge e;
781 location_t entry_locus;
783 gcc_assert (entry);
784 gcc_assert (gimple_code (entry) == GIMPLE_COND);
786 entry_locus = gimple_location (entry);
788 /* Entry basic blocks for each component. */
789 then_label = gimple_cond_true_label (entry);
790 else_label = gimple_cond_false_label (entry);
791 then_bb = label_to_block (then_label);
792 else_bb = label_to_block (else_label);
793 then_stmt = first_stmt (then_bb);
794 else_stmt = first_stmt (else_bb);
796 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
797 assign_discriminator (entry_locus, then_bb);
798 e->goto_locus = gimple_location (then_stmt);
799 if (e->goto_locus)
800 e->goto_block = gimple_block (then_stmt);
801 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
802 if (e)
804 assign_discriminator (entry_locus, else_bb);
805 e->goto_locus = gimple_location (else_stmt);
806 if (e->goto_locus)
807 e->goto_block = gimple_block (else_stmt);
810 /* We do not need the labels anymore. */
811 gimple_cond_set_true_label (entry, NULL_TREE);
812 gimple_cond_set_false_label (entry, NULL_TREE);
816 /* Called for each element in the hash table (P) as we delete the
817 edge to cases hash table.
819 Clear all the TREE_CHAINs to prevent problems with copying of
820 SWITCH_EXPRs and structure sharing rules, then free the hash table
821 element. */
823 static bool
824 edge_to_cases_cleanup (const void *key ATTRIBUTE_UNUSED, void **value,
825 void *data ATTRIBUTE_UNUSED)
827 tree t, next;
829 for (t = (tree) *value; t; t = next)
831 next = TREE_CHAIN (t);
832 TREE_CHAIN (t) = NULL;
835 *value = NULL;
836 return false;
839 /* Start recording information mapping edges to case labels. */
841 void
842 start_recording_case_labels (void)
844 gcc_assert (edge_to_cases == NULL);
845 edge_to_cases = pointer_map_create ();
848 /* Return nonzero if we are recording information for case labels. */
850 static bool
851 recording_case_labels_p (void)
853 return (edge_to_cases != NULL);
856 /* Stop recording information mapping edges to case labels and
857 remove any information we have recorded. */
858 void
859 end_recording_case_labels (void)
861 pointer_map_traverse (edge_to_cases, edge_to_cases_cleanup, NULL);
862 pointer_map_destroy (edge_to_cases);
863 edge_to_cases = NULL;
866 /* If we are inside a {start,end}_recording_cases block, then return
867 a chain of CASE_LABEL_EXPRs from T which reference E.
869 Otherwise return NULL. */
871 static tree
872 get_cases_for_edge (edge e, gimple t)
874 void **slot;
875 size_t i, n;
877 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
878 chains available. Return NULL so the caller can detect this case. */
879 if (!recording_case_labels_p ())
880 return NULL;
882 slot = pointer_map_contains (edge_to_cases, e);
883 if (slot)
884 return (tree) *slot;
886 /* If we did not find E in the hash table, then this must be the first
887 time we have been queried for information about E & T. Add all the
888 elements from T to the hash table then perform the query again. */
890 n = gimple_switch_num_labels (t);
891 for (i = 0; i < n; i++)
893 tree elt = gimple_switch_label (t, i);
894 tree lab = CASE_LABEL (elt);
895 basic_block label_bb = label_to_block (lab);
896 edge this_edge = find_edge (e->src, label_bb);
898 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
899 a new chain. */
900 slot = pointer_map_insert (edge_to_cases, this_edge);
901 TREE_CHAIN (elt) = (tree) *slot;
902 *slot = elt;
905 return (tree) *pointer_map_contains (edge_to_cases, e);
908 /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
910 static void
911 make_gimple_switch_edges (basic_block bb)
913 gimple entry = last_stmt (bb);
914 location_t entry_locus;
915 size_t i, n;
917 entry_locus = gimple_location (entry);
919 n = gimple_switch_num_labels (entry);
921 for (i = 0; i < n; ++i)
923 tree lab = CASE_LABEL (gimple_switch_label (entry, i));
924 basic_block label_bb = label_to_block (lab);
925 make_edge (bb, label_bb, 0);
926 assign_discriminator (entry_locus, label_bb);
931 /* Return the basic block holding label DEST. */
933 basic_block
934 label_to_block_fn (struct function *ifun, tree dest)
936 int uid = LABEL_DECL_UID (dest);
938 /* We would die hard when faced by an undefined label. Emit a label to
939 the very first basic block. This will hopefully make even the dataflow
940 and undefined variable warnings quite right. */
941 if ((errorcount || sorrycount) && uid < 0)
943 gimple_stmt_iterator gsi = gsi_start_bb (BASIC_BLOCK (NUM_FIXED_BLOCKS));
944 gimple stmt;
946 stmt = gimple_build_label (dest);
947 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
948 uid = LABEL_DECL_UID (dest);
950 if (VEC_length (basic_block, ifun->cfg->x_label_to_block_map)
951 <= (unsigned int) uid)
952 return NULL;
953 return VEC_index (basic_block, ifun->cfg->x_label_to_block_map, uid);
956 /* Create edges for an abnormal goto statement at block BB. If FOR_CALL
957 is true, the source statement is a CALL_EXPR instead of a GOTO_EXPR. */
959 void
960 make_abnormal_goto_edges (basic_block bb, bool for_call)
962 basic_block target_bb;
963 gimple_stmt_iterator gsi;
965 FOR_EACH_BB (target_bb)
966 for (gsi = gsi_start_bb (target_bb); !gsi_end_p (gsi); gsi_next (&gsi))
968 gimple label_stmt = gsi_stmt (gsi);
969 tree target;
971 if (gimple_code (label_stmt) != GIMPLE_LABEL)
972 break;
974 target = gimple_label_label (label_stmt);
976 /* Make an edge to every label block that has been marked as a
977 potential target for a computed goto or a non-local goto. */
978 if ((FORCED_LABEL (target) && !for_call)
979 || (DECL_NONLOCAL (target) && for_call))
981 make_edge (bb, target_bb, EDGE_ABNORMAL);
982 break;
987 /* Create edges for a goto statement at block BB. */
989 static void
990 make_goto_expr_edges (basic_block bb)
992 gimple_stmt_iterator last = gsi_last_bb (bb);
993 gimple goto_t = gsi_stmt (last);
995 /* A simple GOTO creates normal edges. */
996 if (simple_goto_p (goto_t))
998 tree dest = gimple_goto_dest (goto_t);
999 basic_block label_bb = label_to_block (dest);
1000 edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
1001 e->goto_locus = gimple_location (goto_t);
1002 assign_discriminator (e->goto_locus, label_bb);
1003 if (e->goto_locus)
1004 e->goto_block = gimple_block (goto_t);
1005 gsi_remove (&last, true);
1006 return;
1009 /* A computed GOTO creates abnormal edges. */
1010 make_abnormal_goto_edges (bb, false);
1014 /*---------------------------------------------------------------------------
1015 Flowgraph analysis
1016 ---------------------------------------------------------------------------*/
1018 /* Cleanup useless labels in basic blocks. This is something we wish
1019 to do early because it allows us to group case labels before creating
1020 the edges for the CFG, and it speeds up block statement iterators in
1021 all passes later on.
1022 We rerun this pass after CFG is created, to get rid of the labels that
1023 are no longer referenced. After then we do not run it any more, since
1024 (almost) no new labels should be created. */
1026 /* A map from basic block index to the leading label of that block. */
1027 static struct label_record
1029 /* The label. */
1030 tree label;
1032 /* True if the label is referenced from somewhere. */
1033 bool used;
1034 } *label_for_bb;
1036 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
1037 static void
1038 update_eh_label (struct eh_region_d *region)
1040 tree old_label = get_eh_region_tree_label (region);
1041 if (old_label)
1043 tree new_label;
1044 basic_block bb = label_to_block (old_label);
1046 /* ??? After optimizing, there may be EH regions with labels
1047 that have already been removed from the function body, so
1048 there is no basic block for them. */
1049 if (! bb)
1050 return;
1052 new_label = label_for_bb[bb->index].label;
1053 label_for_bb[bb->index].used = true;
1054 set_eh_region_tree_label (region, new_label);
1059 /* Given LABEL return the first label in the same basic block. */
1061 static tree
1062 main_block_label (tree label)
1064 basic_block bb = label_to_block (label);
1065 tree main_label = label_for_bb[bb->index].label;
1067 /* label_to_block possibly inserted undefined label into the chain. */
1068 if (!main_label)
1070 label_for_bb[bb->index].label = label;
1071 main_label = label;
1074 label_for_bb[bb->index].used = true;
1075 return main_label;
1078 /* Cleanup redundant labels. This is a three-step process:
1079 1) Find the leading label for each block.
1080 2) Redirect all references to labels to the leading labels.
1081 3) Cleanup all useless labels. */
1083 void
1084 cleanup_dead_labels (void)
1086 basic_block bb;
1087 label_for_bb = XCNEWVEC (struct label_record, last_basic_block);
1089 /* Find a suitable label for each block. We use the first user-defined
1090 label if there is one, or otherwise just the first label we see. */
1091 FOR_EACH_BB (bb)
1093 gimple_stmt_iterator i;
1095 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1097 tree label;
1098 gimple stmt = gsi_stmt (i);
1100 if (gimple_code (stmt) != GIMPLE_LABEL)
1101 break;
1103 label = gimple_label_label (stmt);
1105 /* If we have not yet seen a label for the current block,
1106 remember this one and see if there are more labels. */
1107 if (!label_for_bb[bb->index].label)
1109 label_for_bb[bb->index].label = label;
1110 continue;
1113 /* If we did see a label for the current block already, but it
1114 is an artificially created label, replace it if the current
1115 label is a user defined label. */
1116 if (!DECL_ARTIFICIAL (label)
1117 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
1119 label_for_bb[bb->index].label = label;
1120 break;
1125 /* Now redirect all jumps/branches to the selected label.
1126 First do so for each block ending in a control statement. */
1127 FOR_EACH_BB (bb)
1129 gimple stmt = last_stmt (bb);
1130 if (!stmt)
1131 continue;
1133 switch (gimple_code (stmt))
1135 case GIMPLE_COND:
1137 tree true_label = gimple_cond_true_label (stmt);
1138 tree false_label = gimple_cond_false_label (stmt);
1140 if (true_label)
1141 gimple_cond_set_true_label (stmt, main_block_label (true_label));
1142 if (false_label)
1143 gimple_cond_set_false_label (stmt, main_block_label (false_label));
1144 break;
1147 case GIMPLE_SWITCH:
1149 size_t i, n = gimple_switch_num_labels (stmt);
1151 /* Replace all destination labels. */
1152 for (i = 0; i < n; ++i)
1154 tree case_label = gimple_switch_label (stmt, i);
1155 tree label = main_block_label (CASE_LABEL (case_label));
1156 CASE_LABEL (case_label) = label;
1158 break;
1161 /* We have to handle gotos until they're removed, and we don't
1162 remove them until after we've created the CFG edges. */
1163 case GIMPLE_GOTO:
1164 if (!computed_goto_p (stmt))
1166 tree new_dest = main_block_label (gimple_goto_dest (stmt));
1167 gimple_goto_set_dest (stmt, new_dest);
1168 break;
1171 default:
1172 break;
1176 for_each_eh_region (update_eh_label);
1178 /* Finally, purge dead labels. All user-defined labels and labels that
1179 can be the target of non-local gotos and labels which have their
1180 address taken are preserved. */
1181 FOR_EACH_BB (bb)
1183 gimple_stmt_iterator i;
1184 tree label_for_this_bb = label_for_bb[bb->index].label;
1186 if (!label_for_this_bb)
1187 continue;
1189 /* If the main label of the block is unused, we may still remove it. */
1190 if (!label_for_bb[bb->index].used)
1191 label_for_this_bb = NULL;
1193 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1195 tree label;
1196 gimple stmt = gsi_stmt (i);
1198 if (gimple_code (stmt) != GIMPLE_LABEL)
1199 break;
1201 label = gimple_label_label (stmt);
1203 if (label == label_for_this_bb
1204 || !DECL_ARTIFICIAL (label)
1205 || DECL_NONLOCAL (label)
1206 || FORCED_LABEL (label))
1207 gsi_next (&i);
1208 else
1209 gsi_remove (&i, true);
1213 free (label_for_bb);
1216 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1217 and scan the sorted vector of cases. Combine the ones jumping to the
1218 same label.
1219 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1221 void
1222 group_case_labels (void)
1224 basic_block bb;
1226 FOR_EACH_BB (bb)
1228 gimple stmt = last_stmt (bb);
1229 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1231 int old_size = gimple_switch_num_labels (stmt);
1232 int i, j, new_size = old_size;
1233 tree default_case = NULL_TREE;
1234 tree default_label = NULL_TREE;
1235 bool has_default;
1237 /* The default label is always the first case in a switch
1238 statement after gimplification if it was not optimized
1239 away */
1240 if (!CASE_LOW (gimple_switch_default_label (stmt))
1241 && !CASE_HIGH (gimple_switch_default_label (stmt)))
1243 default_case = gimple_switch_default_label (stmt);
1244 default_label = CASE_LABEL (default_case);
1245 has_default = true;
1247 else
1248 has_default = false;
1250 /* Look for possible opportunities to merge cases. */
1251 if (has_default)
1252 i = 1;
1253 else
1254 i = 0;
1255 while (i < old_size)
1257 tree base_case, base_label, base_high;
1258 base_case = gimple_switch_label (stmt, i);
1260 gcc_assert (base_case);
1261 base_label = CASE_LABEL (base_case);
1263 /* Discard cases that have the same destination as the
1264 default case. */
1265 if (base_label == default_label)
1267 gimple_switch_set_label (stmt, i, NULL_TREE);
1268 i++;
1269 new_size--;
1270 continue;
1273 base_high = CASE_HIGH (base_case)
1274 ? CASE_HIGH (base_case)
1275 : CASE_LOW (base_case);
1276 i++;
1278 /* Try to merge case labels. Break out when we reach the end
1279 of the label vector or when we cannot merge the next case
1280 label with the current one. */
1281 while (i < old_size)
1283 tree merge_case = gimple_switch_label (stmt, i);
1284 tree merge_label = CASE_LABEL (merge_case);
1285 tree t = int_const_binop (PLUS_EXPR, base_high,
1286 integer_one_node, 1);
1288 /* Merge the cases if they jump to the same place,
1289 and their ranges are consecutive. */
1290 if (merge_label == base_label
1291 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1293 base_high = CASE_HIGH (merge_case) ?
1294 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1295 CASE_HIGH (base_case) = base_high;
1296 gimple_switch_set_label (stmt, i, NULL_TREE);
1297 new_size--;
1298 i++;
1300 else
1301 break;
1305 /* Compress the case labels in the label vector, and adjust the
1306 length of the vector. */
1307 for (i = 0, j = 0; i < new_size; i++)
1309 while (! gimple_switch_label (stmt, j))
1310 j++;
1311 gimple_switch_set_label (stmt, i,
1312 gimple_switch_label (stmt, j++));
1315 gcc_assert (new_size <= old_size);
1316 gimple_switch_set_num_labels (stmt, new_size);
1321 /* Checks whether we can merge block B into block A. */
1323 static bool
1324 gimple_can_merge_blocks_p (basic_block a, basic_block b)
1326 gimple stmt;
1327 gimple_stmt_iterator gsi;
1328 gimple_seq phis;
1330 if (!single_succ_p (a))
1331 return false;
1333 if (single_succ_edge (a)->flags & (EDGE_ABNORMAL | EDGE_EH))
1334 return false;
1336 if (single_succ (a) != b)
1337 return false;
1339 if (!single_pred_p (b))
1340 return false;
1342 if (b == EXIT_BLOCK_PTR)
1343 return false;
1345 /* If A ends by a statement causing exceptions or something similar, we
1346 cannot merge the blocks. */
1347 stmt = last_stmt (a);
1348 if (stmt && stmt_ends_bb_p (stmt))
1349 return false;
1351 /* Do not allow a block with only a non-local label to be merged. */
1352 if (stmt
1353 && gimple_code (stmt) == GIMPLE_LABEL
1354 && DECL_NONLOCAL (gimple_label_label (stmt)))
1355 return false;
1357 /* It must be possible to eliminate all phi nodes in B. If ssa form
1358 is not up-to-date, we cannot eliminate any phis; however, if only
1359 some symbols as whole are marked for renaming, this is not a problem,
1360 as phi nodes for those symbols are irrelevant in updating anyway. */
1361 phis = phi_nodes (b);
1362 if (!gimple_seq_empty_p (phis))
1364 gimple_stmt_iterator i;
1366 if (name_mappings_registered_p ())
1367 return false;
1369 for (i = gsi_start (phis); !gsi_end_p (i); gsi_next (&i))
1371 gimple phi = gsi_stmt (i);
1373 if (!is_gimple_reg (gimple_phi_result (phi))
1374 && !may_propagate_copy (gimple_phi_result (phi),
1375 gimple_phi_arg_def (phi, 0)))
1376 return false;
1380 /* Do not remove user labels. */
1381 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
1383 stmt = gsi_stmt (gsi);
1384 if (gimple_code (stmt) != GIMPLE_LABEL)
1385 break;
1386 if (!DECL_ARTIFICIAL (gimple_label_label (stmt)))
1387 return false;
1390 /* Protect the loop latches. */
1391 if (current_loops
1392 && b->loop_father->latch == b)
1393 return false;
1395 return true;
1398 /* Return true if the var whose chain of uses starts at PTR has no
1399 nondebug uses. */
1400 bool
1401 has_zero_uses_1 (const ssa_use_operand_t *head)
1403 const ssa_use_operand_t *ptr;
1405 for (ptr = head->next; ptr != head; ptr = ptr->next)
1406 if (!is_gimple_debug (USE_STMT (ptr)))
1407 return false;
1409 return true;
1412 /* Return true if the var whose chain of uses starts at PTR has a
1413 single nondebug use. Set USE_P and STMT to that single nondebug
1414 use, if so, or to NULL otherwise. */
1415 bool
1416 single_imm_use_1 (const ssa_use_operand_t *head,
1417 use_operand_p *use_p, gimple *stmt)
1419 ssa_use_operand_t *ptr, *single_use = 0;
1421 for (ptr = head->next; ptr != head; ptr = ptr->next)
1422 if (!is_gimple_debug (USE_STMT (ptr)))
1424 if (single_use)
1426 single_use = NULL;
1427 break;
1429 single_use = ptr;
1432 if (use_p)
1433 *use_p = single_use;
1435 if (stmt)
1436 *stmt = single_use ? single_use->loc.stmt : NULL;
1438 return !!single_use;
1441 /* Replaces all uses of NAME by VAL. */
1443 void
1444 replace_uses_by (tree name, tree val)
1446 imm_use_iterator imm_iter;
1447 use_operand_p use;
1448 gimple stmt;
1449 edge e;
1451 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1453 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1455 replace_exp (use, val);
1457 if (gimple_code (stmt) == GIMPLE_PHI)
1459 e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
1460 if (e->flags & EDGE_ABNORMAL)
1462 /* This can only occur for virtual operands, since
1463 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1464 would prevent replacement. */
1465 gcc_assert (!is_gimple_reg (name));
1466 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1471 if (gimple_code (stmt) != GIMPLE_PHI)
1473 size_t i;
1475 fold_stmt_inplace (stmt);
1476 if (cfgcleanup_altered_bbs)
1477 bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1479 /* FIXME. This should go in update_stmt. */
1480 for (i = 0; i < gimple_num_ops (stmt); i++)
1482 tree op = gimple_op (stmt, i);
1483 /* Operands may be empty here. For example, the labels
1484 of a GIMPLE_COND are nulled out following the creation
1485 of the corresponding CFG edges. */
1486 if (op && TREE_CODE (op) == ADDR_EXPR)
1487 recompute_tree_invariant_for_addr_expr (op);
1490 maybe_clean_or_replace_eh_stmt (stmt, stmt);
1491 update_stmt (stmt);
1495 gcc_assert (has_zero_uses (name));
1497 /* Also update the trees stored in loop structures. */
1498 if (current_loops)
1500 struct loop *loop;
1501 loop_iterator li;
1503 FOR_EACH_LOOP (li, loop, 0)
1505 substitute_in_loop_info (loop, name, val);
1510 /* Merge block B into block A. */
1512 static void
1513 gimple_merge_blocks (basic_block a, basic_block b)
1515 gimple_stmt_iterator last, gsi, psi;
1516 gimple_seq phis = phi_nodes (b);
1518 if (dump_file)
1519 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1521 /* Remove all single-valued PHI nodes from block B of the form
1522 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1523 gsi = gsi_last_bb (a);
1524 for (psi = gsi_start (phis); !gsi_end_p (psi); )
1526 gimple phi = gsi_stmt (psi);
1527 tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
1528 gimple copy;
1529 bool may_replace_uses = !is_gimple_reg (def)
1530 || may_propagate_copy (def, use);
1532 /* In case we maintain loop closed ssa form, do not propagate arguments
1533 of loop exit phi nodes. */
1534 if (current_loops
1535 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1536 && is_gimple_reg (def)
1537 && TREE_CODE (use) == SSA_NAME
1538 && a->loop_father != b->loop_father)
1539 may_replace_uses = false;
1541 if (!may_replace_uses)
1543 gcc_assert (is_gimple_reg (def));
1545 /* Note that just emitting the copies is fine -- there is no problem
1546 with ordering of phi nodes. This is because A is the single
1547 predecessor of B, therefore results of the phi nodes cannot
1548 appear as arguments of the phi nodes. */
1549 copy = gimple_build_assign (def, use);
1550 gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
1551 remove_phi_node (&psi, false);
1553 else
1555 /* If we deal with a PHI for virtual operands, we can simply
1556 propagate these without fussing with folding or updating
1557 the stmt. */
1558 if (!is_gimple_reg (def))
1560 imm_use_iterator iter;
1561 use_operand_p use_p;
1562 gimple stmt;
1564 FOR_EACH_IMM_USE_STMT (stmt, iter, def)
1565 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1566 SET_USE (use_p, use);
1568 else
1569 replace_uses_by (def, use);
1571 remove_phi_node (&psi, true);
1575 /* Ensure that B follows A. */
1576 move_block_after (b, a);
1578 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1579 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1581 /* Remove labels from B and set gimple_bb to A for other statements. */
1582 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
1584 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1586 gimple label = gsi_stmt (gsi);
1588 gsi_remove (&gsi, false);
1590 /* Now that we can thread computed gotos, we might have
1591 a situation where we have a forced label in block B
1592 However, the label at the start of block B might still be
1593 used in other ways (think about the runtime checking for
1594 Fortran assigned gotos). So we can not just delete the
1595 label. Instead we move the label to the start of block A. */
1596 if (FORCED_LABEL (gimple_label_label (label)))
1598 gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
1599 gsi_insert_before (&dest_gsi, label, GSI_NEW_STMT);
1602 else
1604 gimple_set_bb (gsi_stmt (gsi), a);
1605 gsi_next (&gsi);
1609 /* Merge the sequences. */
1610 last = gsi_last_bb (a);
1611 gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
1612 set_bb_seq (b, NULL);
1614 if (cfgcleanup_altered_bbs)
1615 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1619 /* Return the one of two successors of BB that is not reachable by a
1620 complex edge, if there is one. Else, return BB. We use
1621 this in optimizations that use post-dominators for their heuristics,
1622 to catch the cases in C++ where function calls are involved. */
1624 basic_block
1625 single_noncomplex_succ (basic_block bb)
1627 edge e0, e1;
1628 if (EDGE_COUNT (bb->succs) != 2)
1629 return bb;
1631 e0 = EDGE_SUCC (bb, 0);
1632 e1 = EDGE_SUCC (bb, 1);
1633 if (e0->flags & EDGE_COMPLEX)
1634 return e1->dest;
1635 if (e1->flags & EDGE_COMPLEX)
1636 return e0->dest;
1638 return bb;
1642 /* Walk the function tree removing unnecessary statements.
1644 * Empty statement nodes are removed
1646 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1648 * Unnecessary COND_EXPRs are removed
1650 * Some unnecessary BIND_EXPRs are removed
1652 * GOTO_EXPRs immediately preceding destination are removed.
1654 Clearly more work could be done. The trick is doing the analysis
1655 and removal fast enough to be a net improvement in compile times.
1657 Note that when we remove a control structure such as a COND_EXPR
1658 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1659 to ensure we eliminate all the useless code. */
1661 struct rus_data
1663 bool repeat;
1664 bool may_throw;
1665 bool may_branch;
1666 bool has_label;
1667 bool last_was_goto;
1668 gimple_stmt_iterator last_goto_gsi;
1672 static void remove_useless_stmts_1 (gimple_stmt_iterator *gsi, struct rus_data *);
1674 /* Given a statement sequence, find the first executable statement with
1675 location information, and warn that it is unreachable. When searching,
1676 descend into containers in execution order. */
1678 static bool
1679 remove_useless_stmts_warn_notreached (gimple_seq stmts)
1681 gimple_stmt_iterator gsi;
1683 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
1685 gimple stmt = gsi_stmt (gsi);
1687 if (gimple_no_warning_p (stmt)) return false;
1689 if (gimple_has_location (stmt))
1691 location_t loc = gimple_location (stmt);
1692 if (LOCATION_LINE (loc) > 0)
1694 warning_at (loc, OPT_Wunreachable_code, "will never be executed");
1695 return true;
1699 switch (gimple_code (stmt))
1701 /* Unfortunately, we need the CFG now to detect unreachable
1702 branches in a conditional, so conditionals are not handled here. */
1704 case GIMPLE_TRY:
1705 if (remove_useless_stmts_warn_notreached (gimple_try_eval (stmt)))
1706 return true;
1707 if (remove_useless_stmts_warn_notreached (gimple_try_cleanup (stmt)))
1708 return true;
1709 break;
1711 case GIMPLE_CATCH:
1712 return remove_useless_stmts_warn_notreached (gimple_catch_handler (stmt));
1714 case GIMPLE_EH_FILTER:
1715 return remove_useless_stmts_warn_notreached (gimple_eh_filter_failure (stmt));
1717 case GIMPLE_BIND:
1718 return remove_useless_stmts_warn_notreached (gimple_bind_body (stmt));
1720 default:
1721 break;
1725 return false;
1728 /* Helper for remove_useless_stmts_1. Handle GIMPLE_COND statements. */
1730 static void
1731 remove_useless_stmts_cond (gimple_stmt_iterator *gsi, struct rus_data *data)
1733 gimple stmt = gsi_stmt (*gsi);
1735 /* The folded result must still be a conditional statement. */
1736 fold_stmt (gsi);
1737 gcc_assert (gsi_stmt (*gsi) == stmt);
1739 data->may_branch = true;
1741 /* Replace trivial conditionals with gotos. */
1742 if (gimple_cond_true_p (stmt))
1744 /* Goto THEN label. */
1745 tree then_label = gimple_cond_true_label (stmt);
1747 gsi_replace (gsi, gimple_build_goto (then_label), false);
1748 data->last_goto_gsi = *gsi;
1749 data->last_was_goto = true;
1750 data->repeat = true;
1752 else if (gimple_cond_false_p (stmt))
1754 /* Goto ELSE label. */
1755 tree else_label = gimple_cond_false_label (stmt);
1757 gsi_replace (gsi, gimple_build_goto (else_label), false);
1758 data->last_goto_gsi = *gsi;
1759 data->last_was_goto = true;
1760 data->repeat = true;
1762 else
1764 tree then_label = gimple_cond_true_label (stmt);
1765 tree else_label = gimple_cond_false_label (stmt);
1767 if (then_label == else_label)
1769 /* Goto common destination. */
1770 gsi_replace (gsi, gimple_build_goto (then_label), false);
1771 data->last_goto_gsi = *gsi;
1772 data->last_was_goto = true;
1773 data->repeat = true;
1777 gsi_next (gsi);
1779 data->last_was_goto = false;
1782 /* Helper for remove_useless_stmts_1.
1783 Handle the try-finally case for GIMPLE_TRY statements. */
1785 static void
1786 remove_useless_stmts_tf (gimple_stmt_iterator *gsi, struct rus_data *data)
1788 bool save_may_branch, save_may_throw;
1789 bool this_may_branch, this_may_throw;
1791 gimple_seq eval_seq, cleanup_seq;
1792 gimple_stmt_iterator eval_gsi, cleanup_gsi;
1794 gimple stmt = gsi_stmt (*gsi);
1796 /* Collect may_branch and may_throw information for the body only. */
1797 save_may_branch = data->may_branch;
1798 save_may_throw = data->may_throw;
1799 data->may_branch = false;
1800 data->may_throw = false;
1801 data->last_was_goto = false;
1803 eval_seq = gimple_try_eval (stmt);
1804 eval_gsi = gsi_start (eval_seq);
1805 remove_useless_stmts_1 (&eval_gsi, data);
1807 this_may_branch = data->may_branch;
1808 this_may_throw = data->may_throw;
1809 data->may_branch |= save_may_branch;
1810 data->may_throw |= save_may_throw;
1811 data->last_was_goto = false;
1813 cleanup_seq = gimple_try_cleanup (stmt);
1814 cleanup_gsi = gsi_start (cleanup_seq);
1815 remove_useless_stmts_1 (&cleanup_gsi, data);
1817 /* If the body is empty, then we can emit the FINALLY block without
1818 the enclosing TRY_FINALLY_EXPR. */
1819 if (gimple_seq_empty_p (eval_seq))
1821 gsi_insert_seq_before (gsi, cleanup_seq, GSI_SAME_STMT);
1822 gsi_remove (gsi, false);
1823 data->repeat = true;
1826 /* If the handler is empty, then we can emit the TRY block without
1827 the enclosing TRY_FINALLY_EXPR. */
1828 else if (gimple_seq_empty_p (cleanup_seq))
1830 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1831 gsi_remove (gsi, false);
1832 data->repeat = true;
1835 /* If the body neither throws, nor branches, then we can safely
1836 string the TRY and FINALLY blocks together. */
1837 else if (!this_may_branch && !this_may_throw)
1839 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1840 gsi_insert_seq_before (gsi, cleanup_seq, GSI_SAME_STMT);
1841 gsi_remove (gsi, false);
1842 data->repeat = true;
1844 else
1845 gsi_next (gsi);
1848 /* Helper for remove_useless_stmts_1.
1849 Handle the try-catch case for GIMPLE_TRY statements. */
1851 static void
1852 remove_useless_stmts_tc (gimple_stmt_iterator *gsi, struct rus_data *data)
1854 bool save_may_throw, this_may_throw;
1856 gimple_seq eval_seq, cleanup_seq, handler_seq, failure_seq;
1857 gimple_stmt_iterator eval_gsi, cleanup_gsi, handler_gsi, failure_gsi;
1859 gimple stmt = gsi_stmt (*gsi);
1861 /* Collect may_throw information for the body only. */
1862 save_may_throw = data->may_throw;
1863 data->may_throw = false;
1864 data->last_was_goto = false;
1866 eval_seq = gimple_try_eval (stmt);
1867 eval_gsi = gsi_start (eval_seq);
1868 remove_useless_stmts_1 (&eval_gsi, data);
1870 this_may_throw = data->may_throw;
1871 data->may_throw = save_may_throw;
1873 cleanup_seq = gimple_try_cleanup (stmt);
1875 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1876 if (!this_may_throw)
1878 if (warn_notreached)
1880 remove_useless_stmts_warn_notreached (cleanup_seq);
1882 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1883 gsi_remove (gsi, false);
1884 data->repeat = true;
1885 return;
1888 /* Process the catch clause specially. We may be able to tell that
1889 no exceptions propagate past this point. */
1891 this_may_throw = true;
1892 cleanup_gsi = gsi_start (cleanup_seq);
1893 stmt = gsi_stmt (cleanup_gsi);
1894 data->last_was_goto = false;
1896 switch (gimple_code (stmt))
1898 case GIMPLE_CATCH:
1899 /* If the first element is a catch, they all must be. */
1900 while (!gsi_end_p (cleanup_gsi))
1902 stmt = gsi_stmt (cleanup_gsi);
1903 /* If we catch all exceptions, then the body does not
1904 propagate exceptions past this point. */
1905 if (gimple_catch_types (stmt) == NULL)
1906 this_may_throw = false;
1907 data->last_was_goto = false;
1908 handler_seq = gimple_catch_handler (stmt);
1909 handler_gsi = gsi_start (handler_seq);
1910 remove_useless_stmts_1 (&handler_gsi, data);
1911 gsi_next (&cleanup_gsi);
1913 gsi_next (gsi);
1914 break;
1916 case GIMPLE_EH_FILTER:
1917 /* If the first element is an eh_filter, it should stand alone. */
1918 if (gimple_eh_filter_must_not_throw (stmt))
1919 this_may_throw = false;
1920 else if (gimple_eh_filter_types (stmt) == NULL)
1921 this_may_throw = false;
1922 failure_seq = gimple_eh_filter_failure (stmt);
1923 failure_gsi = gsi_start (failure_seq);
1924 remove_useless_stmts_1 (&failure_gsi, data);
1925 gsi_next (gsi);
1926 break;
1928 default:
1929 /* Otherwise this is a list of cleanup statements. */
1930 remove_useless_stmts_1 (&cleanup_gsi, data);
1932 /* If the cleanup is empty, then we can emit the TRY block without
1933 the enclosing TRY_CATCH_EXPR. */
1934 if (gimple_seq_empty_p (cleanup_seq))
1936 gsi_insert_seq_before (gsi, eval_seq, GSI_SAME_STMT);
1937 gsi_remove(gsi, false);
1938 data->repeat = true;
1940 else
1941 gsi_next (gsi);
1942 break;
1945 data->may_throw |= this_may_throw;
1948 /* Helper for remove_useless_stmts_1. Handle GIMPLE_BIND statements. */
1950 static void
1951 remove_useless_stmts_bind (gimple_stmt_iterator *gsi, struct rus_data *data ATTRIBUTE_UNUSED)
1953 tree block;
1954 gimple_seq body_seq, fn_body_seq;
1955 gimple_stmt_iterator body_gsi;
1957 gimple stmt = gsi_stmt (*gsi);
1959 /* First remove anything underneath the BIND_EXPR. */
1961 body_seq = gimple_bind_body (stmt);
1962 body_gsi = gsi_start (body_seq);
1963 remove_useless_stmts_1 (&body_gsi, data);
1965 /* If the GIMPLE_BIND has no variables, then we can pull everything
1966 up one level and remove the GIMPLE_BIND, unless this is the toplevel
1967 GIMPLE_BIND for the current function or an inlined function.
1969 When this situation occurs we will want to apply this
1970 optimization again. */
1971 block = gimple_bind_block (stmt);
1972 fn_body_seq = gimple_body (current_function_decl);
1973 if (gimple_bind_vars (stmt) == NULL_TREE
1974 && (gimple_seq_empty_p (fn_body_seq)
1975 || stmt != gimple_seq_first_stmt (fn_body_seq))
1976 && (! block
1977 || ! BLOCK_ABSTRACT_ORIGIN (block)
1978 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1979 != FUNCTION_DECL)))
1981 tree var = NULL_TREE;
1982 /* Even if there are no gimple_bind_vars, there might be other
1983 decls in BLOCK_VARS rendering the GIMPLE_BIND not useless. */
1984 if (block && !BLOCK_NUM_NONLOCALIZED_VARS (block))
1985 for (var = BLOCK_VARS (block); var; var = TREE_CHAIN (var))
1986 if (TREE_CODE (var) == IMPORTED_DECL)
1987 break;
1988 if (var || (block && BLOCK_NUM_NONLOCALIZED_VARS (block)))
1989 gsi_next (gsi);
1990 else
1992 gsi_insert_seq_before (gsi, body_seq, GSI_SAME_STMT);
1993 gsi_remove (gsi, false);
1994 data->repeat = true;
1997 else
1998 gsi_next (gsi);
2001 /* Helper for remove_useless_stmts_1. Handle GIMPLE_GOTO statements. */
2003 static void
2004 remove_useless_stmts_goto (gimple_stmt_iterator *gsi, struct rus_data *data)
2006 gimple stmt = gsi_stmt (*gsi);
2008 tree dest = gimple_goto_dest (stmt);
2010 data->may_branch = true;
2011 data->last_was_goto = false;
2013 /* Record iterator for last goto expr, so that we can delete it if unnecessary. */
2014 if (TREE_CODE (dest) == LABEL_DECL)
2016 data->last_goto_gsi = *gsi;
2017 data->last_was_goto = true;
2020 gsi_next(gsi);
2023 /* Helper for remove_useless_stmts_1. Handle GIMPLE_LABEL statements. */
2025 static void
2026 remove_useless_stmts_label (gimple_stmt_iterator *gsi, struct rus_data *data)
2028 gimple stmt = gsi_stmt (*gsi);
2030 tree label = gimple_label_label (stmt);
2032 data->has_label = true;
2034 /* We do want to jump across non-local label receiver code. */
2035 if (DECL_NONLOCAL (label))
2036 data->last_was_goto = false;
2038 else if (data->last_was_goto
2039 && gimple_goto_dest (gsi_stmt (data->last_goto_gsi)) == label)
2041 /* Replace the preceding GIMPLE_GOTO statement with
2042 a GIMPLE_NOP, which will be subsequently removed.
2043 In this way, we avoid invalidating other iterators
2044 active on the statement sequence. */
2045 gsi_replace(&data->last_goto_gsi, gimple_build_nop(), false);
2046 data->last_was_goto = false;
2047 data->repeat = true;
2050 /* ??? Add something here to delete unused labels. */
2052 gsi_next (gsi);
2056 /* T is CALL_EXPR. Set current_function_calls_* flags. */
2058 void
2059 notice_special_calls (gimple call)
2061 int flags = gimple_call_flags (call);
2063 if (flags & ECF_MAY_BE_ALLOCA)
2064 cfun->calls_alloca = true;
2065 if (flags & ECF_RETURNS_TWICE)
2066 cfun->calls_setjmp = true;
2070 /* Clear flags set by notice_special_calls. Used by dead code removal
2071 to update the flags. */
2073 void
2074 clear_special_calls (void)
2076 cfun->calls_alloca = false;
2077 cfun->calls_setjmp = false;
2080 /* Remove useless statements from a statement sequence, and perform
2081 some preliminary simplifications. */
2083 static void
2084 remove_useless_stmts_1 (gimple_stmt_iterator *gsi, struct rus_data *data)
2086 while (!gsi_end_p (*gsi))
2088 gimple stmt = gsi_stmt (*gsi);
2090 switch (gimple_code (stmt))
2092 case GIMPLE_COND:
2093 remove_useless_stmts_cond (gsi, data);
2094 break;
2096 case GIMPLE_GOTO:
2097 remove_useless_stmts_goto (gsi, data);
2098 break;
2100 case GIMPLE_LABEL:
2101 remove_useless_stmts_label (gsi, data);
2102 break;
2104 case GIMPLE_ASSIGN:
2105 fold_stmt (gsi);
2106 stmt = gsi_stmt (*gsi);
2107 data->last_was_goto = false;
2108 if (stmt_could_throw_p (stmt))
2109 data->may_throw = true;
2110 gsi_next (gsi);
2111 break;
2113 case GIMPLE_ASM:
2114 fold_stmt (gsi);
2115 data->last_was_goto = false;
2116 gsi_next (gsi);
2117 break;
2119 case GIMPLE_CALL:
2120 fold_stmt (gsi);
2121 stmt = gsi_stmt (*gsi);
2122 data->last_was_goto = false;
2123 if (is_gimple_call (stmt))
2124 notice_special_calls (stmt);
2126 /* We used to call update_gimple_call_flags here,
2127 which copied side-effects and nothrows status
2128 from the function decl to the call. In the new
2129 tuplified GIMPLE, the accessors for this information
2130 always consult the function decl, so this copying
2131 is no longer necessary. */
2132 if (stmt_could_throw_p (stmt))
2133 data->may_throw = true;
2134 gsi_next (gsi);
2135 break;
2137 case GIMPLE_RETURN:
2138 fold_stmt (gsi);
2139 data->last_was_goto = false;
2140 data->may_branch = true;
2141 gsi_next (gsi);
2142 break;
2144 case GIMPLE_BIND:
2145 remove_useless_stmts_bind (gsi, data);
2146 break;
2148 case GIMPLE_TRY:
2149 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
2150 remove_useless_stmts_tc (gsi, data);
2151 else if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
2152 remove_useless_stmts_tf (gsi, data);
2153 else
2154 gcc_unreachable ();
2155 break;
2157 case GIMPLE_CATCH:
2158 gcc_unreachable ();
2159 break;
2161 case GIMPLE_NOP:
2162 gsi_remove (gsi, false);
2163 break;
2165 case GIMPLE_OMP_FOR:
2167 gimple_seq pre_body_seq = gimple_omp_for_pre_body (stmt);
2168 gimple_stmt_iterator pre_body_gsi = gsi_start (pre_body_seq);
2170 remove_useless_stmts_1 (&pre_body_gsi, data);
2171 data->last_was_goto = false;
2173 /* FALLTHROUGH */
2174 case GIMPLE_OMP_CRITICAL:
2175 case GIMPLE_OMP_CONTINUE:
2176 case GIMPLE_OMP_MASTER:
2177 case GIMPLE_OMP_ORDERED:
2178 case GIMPLE_OMP_SECTION:
2179 case GIMPLE_OMP_SECTIONS:
2180 case GIMPLE_OMP_SINGLE:
2182 gimple_seq body_seq = gimple_omp_body (stmt);
2183 gimple_stmt_iterator body_gsi = gsi_start (body_seq);
2185 remove_useless_stmts_1 (&body_gsi, data);
2186 data->last_was_goto = false;
2187 gsi_next (gsi);
2189 break;
2191 case GIMPLE_OMP_PARALLEL:
2192 case GIMPLE_OMP_TASK:
2194 /* Make sure the outermost GIMPLE_BIND isn't removed
2195 as useless. */
2196 gimple_seq body_seq = gimple_omp_body (stmt);
2197 gimple bind = gimple_seq_first_stmt (body_seq);
2198 gimple_seq bind_seq = gimple_bind_body (bind);
2199 gimple_stmt_iterator bind_gsi = gsi_start (bind_seq);
2201 remove_useless_stmts_1 (&bind_gsi, data);
2202 data->last_was_goto = false;
2203 gsi_next (gsi);
2205 break;
2207 default:
2208 data->last_was_goto = false;
2209 gsi_next (gsi);
2210 break;
2215 /* Walk the function tree, removing useless statements and performing
2216 some preliminary simplifications. */
2218 static unsigned int
2219 remove_useless_stmts (void)
2221 struct rus_data data;
2223 clear_special_calls ();
2227 gimple_stmt_iterator gsi;
2229 gsi = gsi_start (gimple_body (current_function_decl));
2230 memset (&data, 0, sizeof (data));
2231 remove_useless_stmts_1 (&gsi, &data);
2233 while (data.repeat);
2235 #ifdef ENABLE_TYPES_CHECKING
2236 verify_types_in_gimple_seq (gimple_body (current_function_decl));
2237 #endif
2239 return 0;
2243 struct gimple_opt_pass pass_remove_useless_stmts =
2246 GIMPLE_PASS,
2247 "useless", /* name */
2248 NULL, /* gate */
2249 remove_useless_stmts, /* execute */
2250 NULL, /* sub */
2251 NULL, /* next */
2252 0, /* static_pass_number */
2253 TV_NONE, /* tv_id */
2254 PROP_gimple_any, /* properties_required */
2255 0, /* properties_provided */
2256 0, /* properties_destroyed */
2257 0, /* todo_flags_start */
2258 TODO_dump_func /* todo_flags_finish */
2262 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
2264 static void
2265 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
2267 /* Since this block is no longer reachable, we can just delete all
2268 of its PHI nodes. */
2269 remove_phi_nodes (bb);
2271 /* Remove edges to BB's successors. */
2272 while (EDGE_COUNT (bb->succs) > 0)
2273 remove_edge (EDGE_SUCC (bb, 0));
2277 /* Remove statements of basic block BB. */
2279 static void
2280 remove_bb (basic_block bb)
2282 gimple_stmt_iterator i;
2283 source_location loc = UNKNOWN_LOCATION;
2285 if (dump_file)
2287 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2288 if (dump_flags & TDF_DETAILS)
2290 dump_bb (bb, dump_file, 0);
2291 fprintf (dump_file, "\n");
2295 if (current_loops)
2297 struct loop *loop = bb->loop_father;
2299 /* If a loop gets removed, clean up the information associated
2300 with it. */
2301 if (loop->latch == bb
2302 || loop->header == bb)
2303 free_numbers_of_iterations_estimates_loop (loop);
2306 /* Remove all the instructions in the block. */
2307 if (bb_seq (bb) != NULL)
2309 /* Walk backwards so as to get a chance to substitute all
2310 released DEFs into debug stmts. See
2311 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
2312 details. */
2313 for (i = gsi_last_bb (bb); !gsi_end_p (i);)
2315 gimple stmt = gsi_stmt (i);
2316 if (gimple_code (stmt) == GIMPLE_LABEL
2317 && (FORCED_LABEL (gimple_label_label (stmt))
2318 || DECL_NONLOCAL (gimple_label_label (stmt))))
2320 basic_block new_bb;
2321 gimple_stmt_iterator new_gsi;
2323 /* A non-reachable non-local label may still be referenced.
2324 But it no longer needs to carry the extra semantics of
2325 non-locality. */
2326 if (DECL_NONLOCAL (gimple_label_label (stmt)))
2328 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
2329 FORCED_LABEL (gimple_label_label (stmt)) = 1;
2332 new_bb = bb->prev_bb;
2333 new_gsi = gsi_start_bb (new_bb);
2334 gsi_remove (&i, false);
2335 gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
2337 else
2339 /* Release SSA definitions if we are in SSA. Note that we
2340 may be called when not in SSA. For example,
2341 final_cleanup calls this function via
2342 cleanup_tree_cfg. */
2343 if (gimple_in_ssa_p (cfun))
2344 release_defs (stmt);
2346 gsi_remove (&i, true);
2349 if (gsi_end_p (i))
2350 i = gsi_last_bb (bb);
2351 else
2352 gsi_prev (&i);
2354 /* Don't warn for removed gotos. Gotos are often removed due to
2355 jump threading, thus resulting in bogus warnings. Not great,
2356 since this way we lose warnings for gotos in the original
2357 program that are indeed unreachable. */
2358 if (gimple_code (stmt) != GIMPLE_GOTO
2359 && gimple_has_location (stmt))
2360 loc = gimple_location (stmt);
2364 /* If requested, give a warning that the first statement in the
2365 block is unreachable. We walk statements backwards in the
2366 loop above, so the last statement we process is the first statement
2367 in the block. */
2368 if (loc > BUILTINS_LOCATION && LOCATION_LINE (loc) > 0)
2369 warning_at (loc, OPT_Wunreachable_code, "will never be executed");
2371 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2372 bb->il.gimple = NULL;
2376 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2377 predicate VAL, return the edge that will be taken out of the block.
2378 If VAL does not match a unique edge, NULL is returned. */
2380 edge
2381 find_taken_edge (basic_block bb, tree val)
2383 gimple stmt;
2385 stmt = last_stmt (bb);
2387 gcc_assert (stmt);
2388 gcc_assert (is_ctrl_stmt (stmt));
2390 if (val == NULL)
2391 return NULL;
2393 if (!is_gimple_min_invariant (val))
2394 return NULL;
2396 if (gimple_code (stmt) == GIMPLE_COND)
2397 return find_taken_edge_cond_expr (bb, val);
2399 if (gimple_code (stmt) == GIMPLE_SWITCH)
2400 return find_taken_edge_switch_expr (bb, val);
2402 if (computed_goto_p (stmt))
2404 /* Only optimize if the argument is a label, if the argument is
2405 not a label then we can not construct a proper CFG.
2407 It may be the case that we only need to allow the LABEL_REF to
2408 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
2409 appear inside a LABEL_EXPR just to be safe. */
2410 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
2411 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
2412 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
2413 return NULL;
2416 gcc_unreachable ();
2419 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2420 statement, determine which of the outgoing edges will be taken out of the
2421 block. Return NULL if either edge may be taken. */
2423 static edge
2424 find_taken_edge_computed_goto (basic_block bb, tree val)
2426 basic_block dest;
2427 edge e = NULL;
2429 dest = label_to_block (val);
2430 if (dest)
2432 e = find_edge (bb, dest);
2433 gcc_assert (e != NULL);
2436 return e;
2439 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2440 statement, determine which of the two edges will be taken out of the
2441 block. Return NULL if either edge may be taken. */
2443 static edge
2444 find_taken_edge_cond_expr (basic_block bb, tree val)
2446 edge true_edge, false_edge;
2448 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2450 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2451 return (integer_zerop (val) ? false_edge : true_edge);
2454 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2455 statement, determine which edge will be taken out of the block. Return
2456 NULL if any edge may be taken. */
2458 static edge
2459 find_taken_edge_switch_expr (basic_block bb, tree val)
2461 basic_block dest_bb;
2462 edge e;
2463 gimple switch_stmt;
2464 tree taken_case;
2466 switch_stmt = last_stmt (bb);
2467 taken_case = find_case_label_for_value (switch_stmt, val);
2468 dest_bb = label_to_block (CASE_LABEL (taken_case));
2470 e = find_edge (bb, dest_bb);
2471 gcc_assert (e);
2472 return e;
2476 /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
2477 We can make optimal use here of the fact that the case labels are
2478 sorted: We can do a binary search for a case matching VAL. */
2480 static tree
2481 find_case_label_for_value (gimple switch_stmt, tree val)
2483 size_t low, high, n = gimple_switch_num_labels (switch_stmt);
2484 tree default_case = gimple_switch_default_label (switch_stmt);
2486 for (low = 0, high = n; high - low > 1; )
2488 size_t i = (high + low) / 2;
2489 tree t = gimple_switch_label (switch_stmt, i);
2490 int cmp;
2492 /* Cache the result of comparing CASE_LOW and val. */
2493 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2495 if (cmp > 0)
2496 high = i;
2497 else
2498 low = i;
2500 if (CASE_HIGH (t) == NULL)
2502 /* A singe-valued case label. */
2503 if (cmp == 0)
2504 return t;
2506 else
2508 /* A case range. We can only handle integer ranges. */
2509 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2510 return t;
2514 return default_case;
2518 /* Dump a basic block on stderr. */
2520 void
2521 gimple_debug_bb (basic_block bb)
2523 gimple_dump_bb (bb, stderr, 0, TDF_VOPS|TDF_MEMSYMS);
2527 /* Dump basic block with index N on stderr. */
2529 basic_block
2530 gimple_debug_bb_n (int n)
2532 gimple_debug_bb (BASIC_BLOCK (n));
2533 return BASIC_BLOCK (n);
2537 /* Dump the CFG on stderr.
2539 FLAGS are the same used by the tree dumping functions
2540 (see TDF_* in tree-pass.h). */
2542 void
2543 gimple_debug_cfg (int flags)
2545 gimple_dump_cfg (stderr, flags);
2549 /* Dump the program showing basic block boundaries on the given FILE.
2551 FLAGS are the same used by the tree dumping functions (see TDF_* in
2552 tree.h). */
2554 void
2555 gimple_dump_cfg (FILE *file, int flags)
2557 if (flags & TDF_DETAILS)
2559 const char *funcname
2560 = lang_hooks.decl_printable_name (current_function_decl, 2);
2562 fputc ('\n', file);
2563 fprintf (file, ";; Function %s\n\n", funcname);
2564 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2565 n_basic_blocks, n_edges, last_basic_block);
2567 brief_dump_cfg (file);
2568 fprintf (file, "\n");
2571 if (flags & TDF_STATS)
2572 dump_cfg_stats (file);
2574 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2578 /* Dump CFG statistics on FILE. */
2580 void
2581 dump_cfg_stats (FILE *file)
2583 static long max_num_merged_labels = 0;
2584 unsigned long size, total = 0;
2585 long num_edges;
2586 basic_block bb;
2587 const char * const fmt_str = "%-30s%-13s%12s\n";
2588 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2589 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2590 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2591 const char *funcname
2592 = lang_hooks.decl_printable_name (current_function_decl, 2);
2595 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2597 fprintf (file, "---------------------------------------------------------\n");
2598 fprintf (file, fmt_str, "", " Number of ", "Memory");
2599 fprintf (file, fmt_str, "", " instances ", "used ");
2600 fprintf (file, "---------------------------------------------------------\n");
2602 size = n_basic_blocks * sizeof (struct basic_block_def);
2603 total += size;
2604 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2605 SCALE (size), LABEL (size));
2607 num_edges = 0;
2608 FOR_EACH_BB (bb)
2609 num_edges += EDGE_COUNT (bb->succs);
2610 size = num_edges * sizeof (struct edge_def);
2611 total += size;
2612 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2614 fprintf (file, "---------------------------------------------------------\n");
2615 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2616 LABEL (total));
2617 fprintf (file, "---------------------------------------------------------\n");
2618 fprintf (file, "\n");
2620 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2621 max_num_merged_labels = cfg_stats.num_merged_labels;
2623 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2624 cfg_stats.num_merged_labels, max_num_merged_labels);
2626 fprintf (file, "\n");
2630 /* Dump CFG statistics on stderr. Keep extern so that it's always
2631 linked in the final executable. */
2633 void
2634 debug_cfg_stats (void)
2636 dump_cfg_stats (stderr);
2640 /* Dump the flowgraph to a .vcg FILE. */
2642 static void
2643 gimple_cfg2vcg (FILE *file)
2645 edge e;
2646 edge_iterator ei;
2647 basic_block bb;
2648 const char *funcname
2649 = lang_hooks.decl_printable_name (current_function_decl, 2);
2651 /* Write the file header. */
2652 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2653 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2654 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2656 /* Write blocks and edges. */
2657 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2659 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2660 e->dest->index);
2662 if (e->flags & EDGE_FAKE)
2663 fprintf (file, " linestyle: dotted priority: 10");
2664 else
2665 fprintf (file, " linestyle: solid priority: 100");
2667 fprintf (file, " }\n");
2669 fputc ('\n', file);
2671 FOR_EACH_BB (bb)
2673 enum gimple_code head_code, end_code;
2674 const char *head_name, *end_name;
2675 int head_line = 0;
2676 int end_line = 0;
2677 gimple first = first_stmt (bb);
2678 gimple last = last_stmt (bb);
2680 if (first)
2682 head_code = gimple_code (first);
2683 head_name = gimple_code_name[head_code];
2684 head_line = get_lineno (first);
2686 else
2687 head_name = "no-statement";
2689 if (last)
2691 end_code = gimple_code (last);
2692 end_name = gimple_code_name[end_code];
2693 end_line = get_lineno (last);
2695 else
2696 end_name = "no-statement";
2698 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2699 bb->index, bb->index, head_name, head_line, end_name,
2700 end_line);
2702 FOR_EACH_EDGE (e, ei, bb->succs)
2704 if (e->dest == EXIT_BLOCK_PTR)
2705 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2706 else
2707 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2709 if (e->flags & EDGE_FAKE)
2710 fprintf (file, " priority: 10 linestyle: dotted");
2711 else
2712 fprintf (file, " priority: 100 linestyle: solid");
2714 fprintf (file, " }\n");
2717 if (bb->next_bb != EXIT_BLOCK_PTR)
2718 fputc ('\n', file);
2721 fputs ("}\n\n", file);
2726 /*---------------------------------------------------------------------------
2727 Miscellaneous helpers
2728 ---------------------------------------------------------------------------*/
2730 /* Return true if T represents a stmt that always transfers control. */
2732 bool
2733 is_ctrl_stmt (gimple t)
2735 return gimple_code (t) == GIMPLE_COND
2736 || gimple_code (t) == GIMPLE_SWITCH
2737 || gimple_code (t) == GIMPLE_GOTO
2738 || gimple_code (t) == GIMPLE_RETURN
2739 || gimple_code (t) == GIMPLE_RESX;
2743 /* Return true if T is a statement that may alter the flow of control
2744 (e.g., a call to a non-returning function). */
2746 bool
2747 is_ctrl_altering_stmt (gimple t)
2749 gcc_assert (t);
2751 if (is_gimple_call (t))
2753 int flags = gimple_call_flags (t);
2755 /* A non-pure/const call alters flow control if the current
2756 function has nonlocal labels. */
2757 if (!(flags & (ECF_CONST | ECF_PURE))
2758 && cfun->has_nonlocal_label)
2759 return true;
2761 /* A call also alters control flow if it does not return. */
2762 if (gimple_call_flags (t) & ECF_NORETURN)
2763 return true;
2766 /* OpenMP directives alter control flow. */
2767 if (is_gimple_omp (t))
2768 return true;
2770 /* If a statement can throw, it alters control flow. */
2771 return stmt_can_throw_internal (t);
2775 /* Return true if T is a simple local goto. */
2777 bool
2778 simple_goto_p (gimple t)
2780 return (gimple_code (t) == GIMPLE_GOTO
2781 && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2785 /* Return true if T can make an abnormal transfer of control flow.
2786 Transfers of control flow associated with EH are excluded. */
2788 bool
2789 stmt_can_make_abnormal_goto (gimple t)
2791 if (computed_goto_p (t))
2792 return true;
2793 if (is_gimple_call (t))
2794 return gimple_has_side_effects (t) && cfun->has_nonlocal_label;
2795 return false;
2799 /* Return true if STMT should start a new basic block. PREV_STMT is
2800 the statement preceding STMT. It is used when STMT is a label or a
2801 case label. Labels should only start a new basic block if their
2802 previous statement wasn't a label. Otherwise, sequence of labels
2803 would generate unnecessary basic blocks that only contain a single
2804 label. */
2806 static inline bool
2807 stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
2809 if (stmt == NULL)
2810 return false;
2812 /* Labels start a new basic block only if the preceding statement
2813 wasn't a label of the same type. This prevents the creation of
2814 consecutive blocks that have nothing but a single label. */
2815 if (gimple_code (stmt) == GIMPLE_LABEL)
2817 /* Nonlocal and computed GOTO targets always start a new block. */
2818 if (DECL_NONLOCAL (gimple_label_label (stmt))
2819 || FORCED_LABEL (gimple_label_label (stmt)))
2820 return true;
2822 if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
2824 if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
2825 return true;
2827 cfg_stats.num_merged_labels++;
2828 return false;
2830 else
2831 return true;
2834 return false;
2838 /* Return true if T should end a basic block. */
2840 bool
2841 stmt_ends_bb_p (gimple t)
2843 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2846 /* Remove block annotations and other data structures. */
2848 void
2849 delete_tree_cfg_annotations (void)
2851 label_to_block_map = NULL;
2855 /* Return the first statement in basic block BB. */
2857 gimple
2858 first_stmt (basic_block bb)
2860 gimple_stmt_iterator i = gsi_start_bb (bb);
2861 gimple stmt = NULL;
2863 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2865 gsi_next (&i);
2866 stmt = NULL;
2868 return stmt;
2871 /* Return the first non-label statement in basic block BB. */
2873 static gimple
2874 first_non_label_stmt (basic_block bb)
2876 gimple_stmt_iterator i = gsi_start_bb (bb);
2877 while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
2878 gsi_next (&i);
2879 return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
2882 /* Return the last statement in basic block BB. */
2884 gimple
2885 last_stmt (basic_block bb)
2887 gimple_stmt_iterator i = gsi_last_bb (bb);
2888 gimple stmt = NULL;
2890 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2892 gsi_prev (&i);
2893 stmt = NULL;
2895 return stmt;
2898 /* Return the last statement of an otherwise empty block. Return NULL
2899 if the block is totally empty, or if it contains more than one
2900 statement. */
2902 gimple
2903 last_and_only_stmt (basic_block bb)
2905 gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
2906 gimple last, prev;
2908 if (gsi_end_p (i))
2909 return NULL;
2911 last = gsi_stmt (i);
2912 gsi_prev_nondebug (&i);
2913 if (gsi_end_p (i))
2914 return last;
2916 /* Empty statements should no longer appear in the instruction stream.
2917 Everything that might have appeared before should be deleted by
2918 remove_useless_stmts, and the optimizers should just gsi_remove
2919 instead of smashing with build_empty_stmt.
2921 Thus the only thing that should appear here in a block containing
2922 one executable statement is a label. */
2923 prev = gsi_stmt (i);
2924 if (gimple_code (prev) == GIMPLE_LABEL)
2925 return last;
2926 else
2927 return NULL;
2930 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
2932 static void
2933 reinstall_phi_args (edge new_edge, edge old_edge)
2935 edge_var_map_vector v;
2936 edge_var_map *vm;
2937 int i;
2938 gimple_stmt_iterator phis;
2940 v = redirect_edge_var_map_vector (old_edge);
2941 if (!v)
2942 return;
2944 for (i = 0, phis = gsi_start_phis (new_edge->dest);
2945 VEC_iterate (edge_var_map, v, i, vm) && !gsi_end_p (phis);
2946 i++, gsi_next (&phis))
2948 gimple phi = gsi_stmt (phis);
2949 tree result = redirect_edge_var_map_result (vm);
2950 tree arg = redirect_edge_var_map_def (vm);
2952 gcc_assert (result == gimple_phi_result (phi));
2954 add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
2957 redirect_edge_var_map_clear (old_edge);
2960 /* Returns the basic block after which the new basic block created
2961 by splitting edge EDGE_IN should be placed. Tries to keep the new block
2962 near its "logical" location. This is of most help to humans looking
2963 at debugging dumps. */
2965 static basic_block
2966 split_edge_bb_loc (edge edge_in)
2968 basic_block dest = edge_in->dest;
2970 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
2971 return edge_in->src;
2972 else
2973 return dest->prev_bb;
2976 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2977 Abort on abnormal edges. */
2979 static basic_block
2980 gimple_split_edge (edge edge_in)
2982 basic_block new_bb, after_bb, dest;
2983 edge new_edge, e;
2985 /* Abnormal edges cannot be split. */
2986 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2988 dest = edge_in->dest;
2990 after_bb = split_edge_bb_loc (edge_in);
2992 new_bb = create_empty_bb (after_bb);
2993 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2994 new_bb->count = edge_in->count;
2995 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2996 new_edge->probability = REG_BR_PROB_BASE;
2997 new_edge->count = edge_in->count;
2999 e = redirect_edge_and_branch (edge_in, new_bb);
3000 gcc_assert (e == edge_in);
3001 reinstall_phi_args (new_edge, e);
3003 return new_bb;
3006 /* Callback for walk_tree, check that all elements with address taken are
3007 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3008 inside a PHI node. */
3010 static tree
3011 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3013 tree t = *tp, x;
3015 if (TYPE_P (t))
3016 *walk_subtrees = 0;
3018 /* Check operand N for being valid GIMPLE and give error MSG if not. */
3019 #define CHECK_OP(N, MSG) \
3020 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
3021 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3023 switch (TREE_CODE (t))
3025 case SSA_NAME:
3026 if (SSA_NAME_IN_FREE_LIST (t))
3028 error ("SSA name in freelist but still referenced");
3029 return *tp;
3031 break;
3033 case INDIRECT_REF:
3034 x = TREE_OPERAND (t, 0);
3035 if (!is_gimple_reg (x) && !is_gimple_min_invariant (x))
3037 error ("Indirect reference's operand is not a register or a constant.");
3038 return x;
3040 break;
3042 case ASSERT_EXPR:
3043 x = fold (ASSERT_EXPR_COND (t));
3044 if (x == boolean_false_node)
3046 error ("ASSERT_EXPR with an always-false condition");
3047 return *tp;
3049 break;
3051 case MODIFY_EXPR:
3052 error ("MODIFY_EXPR not expected while having tuples.");
3053 return *tp;
3055 case ADDR_EXPR:
3057 bool old_constant;
3058 bool old_side_effects;
3059 bool new_constant;
3060 bool new_side_effects;
3062 gcc_assert (is_gimple_address (t));
3064 old_constant = TREE_CONSTANT (t);
3065 old_side_effects = TREE_SIDE_EFFECTS (t);
3067 recompute_tree_invariant_for_addr_expr (t);
3068 new_side_effects = TREE_SIDE_EFFECTS (t);
3069 new_constant = TREE_CONSTANT (t);
3071 if (old_constant != new_constant)
3073 error ("constant not recomputed when ADDR_EXPR changed");
3074 return t;
3076 if (old_side_effects != new_side_effects)
3078 error ("side effects not recomputed when ADDR_EXPR changed");
3079 return t;
3082 /* Skip any references (they will be checked when we recurse down the
3083 tree) and ensure that any variable used as a prefix is marked
3084 addressable. */
3085 for (x = TREE_OPERAND (t, 0);
3086 handled_component_p (x);
3087 x = TREE_OPERAND (x, 0))
3090 if (!(TREE_CODE (x) == VAR_DECL
3091 || TREE_CODE (x) == PARM_DECL
3092 || TREE_CODE (x) == RESULT_DECL))
3093 return NULL;
3094 if (!TREE_ADDRESSABLE (x))
3096 error ("address taken, but ADDRESSABLE bit not set");
3097 return x;
3099 if (DECL_GIMPLE_REG_P (x))
3101 error ("DECL_GIMPLE_REG_P set on a variable with address taken");
3102 return x;
3105 break;
3108 case COND_EXPR:
3109 x = COND_EXPR_COND (t);
3110 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
3112 error ("non-integral used in condition");
3113 return x;
3115 if (!is_gimple_condexpr (x))
3117 error ("invalid conditional operand");
3118 return x;
3120 break;
3122 case NON_LVALUE_EXPR:
3123 gcc_unreachable ();
3125 CASE_CONVERT:
3126 case FIX_TRUNC_EXPR:
3127 case FLOAT_EXPR:
3128 case NEGATE_EXPR:
3129 case ABS_EXPR:
3130 case BIT_NOT_EXPR:
3131 case TRUTH_NOT_EXPR:
3132 CHECK_OP (0, "invalid operand to unary operator");
3133 break;
3135 case REALPART_EXPR:
3136 case IMAGPART_EXPR:
3137 case COMPONENT_REF:
3138 case ARRAY_REF:
3139 case ARRAY_RANGE_REF:
3140 case BIT_FIELD_REF:
3141 case VIEW_CONVERT_EXPR:
3142 /* We have a nest of references. Verify that each of the operands
3143 that determine where to reference is either a constant or a variable,
3144 verify that the base is valid, and then show we've already checked
3145 the subtrees. */
3146 while (handled_component_p (t))
3148 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3149 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
3150 else if (TREE_CODE (t) == ARRAY_REF
3151 || TREE_CODE (t) == ARRAY_RANGE_REF)
3153 CHECK_OP (1, "invalid array index");
3154 if (TREE_OPERAND (t, 2))
3155 CHECK_OP (2, "invalid array lower bound");
3156 if (TREE_OPERAND (t, 3))
3157 CHECK_OP (3, "invalid array stride");
3159 else if (TREE_CODE (t) == BIT_FIELD_REF)
3161 if (!host_integerp (TREE_OPERAND (t, 1), 1)
3162 || !host_integerp (TREE_OPERAND (t, 2), 1))
3164 error ("invalid position or size operand to BIT_FIELD_REF");
3165 return t;
3167 else if (INTEGRAL_TYPE_P (TREE_TYPE (t))
3168 && (TYPE_PRECISION (TREE_TYPE (t))
3169 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
3171 error ("integral result type precision does not match "
3172 "field size of BIT_FIELD_REF");
3173 return t;
3175 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
3176 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
3177 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
3179 error ("mode precision of non-integral result does not "
3180 "match field size of BIT_FIELD_REF");
3181 return t;
3185 t = TREE_OPERAND (t, 0);
3188 if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
3190 error ("invalid reference prefix");
3191 return t;
3193 *walk_subtrees = 0;
3194 break;
3195 case PLUS_EXPR:
3196 case MINUS_EXPR:
3197 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
3198 POINTER_PLUS_EXPR. */
3199 if (POINTER_TYPE_P (TREE_TYPE (t)))
3201 error ("invalid operand to plus/minus, type is a pointer");
3202 return t;
3204 CHECK_OP (0, "invalid operand to binary operator");
3205 CHECK_OP (1, "invalid operand to binary operator");
3206 break;
3208 case POINTER_PLUS_EXPR:
3209 /* Check to make sure the first operand is a pointer or reference type. */
3210 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3212 error ("invalid operand to pointer plus, first operand is not a pointer");
3213 return t;
3215 /* Check to make sure the second operand is an integer with type of
3216 sizetype. */
3217 if (!useless_type_conversion_p (sizetype,
3218 TREE_TYPE (TREE_OPERAND (t, 1))))
3220 error ("invalid operand to pointer plus, second operand is not an "
3221 "integer with type of sizetype.");
3222 return t;
3224 /* FALLTHROUGH */
3225 case LT_EXPR:
3226 case LE_EXPR:
3227 case GT_EXPR:
3228 case GE_EXPR:
3229 case EQ_EXPR:
3230 case NE_EXPR:
3231 case UNORDERED_EXPR:
3232 case ORDERED_EXPR:
3233 case UNLT_EXPR:
3234 case UNLE_EXPR:
3235 case UNGT_EXPR:
3236 case UNGE_EXPR:
3237 case UNEQ_EXPR:
3238 case LTGT_EXPR:
3239 case MULT_EXPR:
3240 case TRUNC_DIV_EXPR:
3241 case CEIL_DIV_EXPR:
3242 case FLOOR_DIV_EXPR:
3243 case ROUND_DIV_EXPR:
3244 case TRUNC_MOD_EXPR:
3245 case CEIL_MOD_EXPR:
3246 case FLOOR_MOD_EXPR:
3247 case ROUND_MOD_EXPR:
3248 case RDIV_EXPR:
3249 case EXACT_DIV_EXPR:
3250 case MIN_EXPR:
3251 case MAX_EXPR:
3252 case LSHIFT_EXPR:
3253 case RSHIFT_EXPR:
3254 case LROTATE_EXPR:
3255 case RROTATE_EXPR:
3256 case BIT_IOR_EXPR:
3257 case BIT_XOR_EXPR:
3258 case BIT_AND_EXPR:
3259 CHECK_OP (0, "invalid operand to binary operator");
3260 CHECK_OP (1, "invalid operand to binary operator");
3261 break;
3263 case CONSTRUCTOR:
3264 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3265 *walk_subtrees = 0;
3266 break;
3268 default:
3269 break;
3271 return NULL;
3273 #undef CHECK_OP
3277 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
3278 Returns true if there is an error, otherwise false. */
3280 static bool
3281 verify_types_in_gimple_min_lval (tree expr)
3283 tree op;
3285 if (is_gimple_id (expr))
3286 return false;
3288 if (!INDIRECT_REF_P (expr)
3289 && TREE_CODE (expr) != TARGET_MEM_REF)
3291 error ("invalid expression for min lvalue");
3292 return true;
3295 /* TARGET_MEM_REFs are strange beasts. */
3296 if (TREE_CODE (expr) == TARGET_MEM_REF)
3297 return false;
3299 op = TREE_OPERAND (expr, 0);
3300 if (!is_gimple_val (op))
3302 error ("invalid operand in indirect reference");
3303 debug_generic_stmt (op);
3304 return true;
3306 if (!useless_type_conversion_p (TREE_TYPE (expr),
3307 TREE_TYPE (TREE_TYPE (op))))
3309 error ("type mismatch in indirect reference");
3310 debug_generic_stmt (TREE_TYPE (expr));
3311 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3312 return true;
3315 return false;
3318 /* Verify if EXPR is a valid GIMPLE reference expression. If
3319 REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
3320 if there is an error, otherwise false. */
3322 static bool
3323 verify_types_in_gimple_reference (tree expr, bool require_lvalue)
3325 while (handled_component_p (expr))
3327 tree op = TREE_OPERAND (expr, 0);
3329 if (TREE_CODE (expr) == ARRAY_REF
3330 || TREE_CODE (expr) == ARRAY_RANGE_REF)
3332 if (!is_gimple_val (TREE_OPERAND (expr, 1))
3333 || (TREE_OPERAND (expr, 2)
3334 && !is_gimple_val (TREE_OPERAND (expr, 2)))
3335 || (TREE_OPERAND (expr, 3)
3336 && !is_gimple_val (TREE_OPERAND (expr, 3))))
3338 error ("invalid operands to array reference");
3339 debug_generic_stmt (expr);
3340 return true;
3344 /* Verify if the reference array element types are compatible. */
3345 if (TREE_CODE (expr) == ARRAY_REF
3346 && !useless_type_conversion_p (TREE_TYPE (expr),
3347 TREE_TYPE (TREE_TYPE (op))))
3349 error ("type mismatch in array reference");
3350 debug_generic_stmt (TREE_TYPE (expr));
3351 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3352 return true;
3354 if (TREE_CODE (expr) == ARRAY_RANGE_REF
3355 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
3356 TREE_TYPE (TREE_TYPE (op))))
3358 error ("type mismatch in array range reference");
3359 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
3360 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3361 return true;
3364 if ((TREE_CODE (expr) == REALPART_EXPR
3365 || TREE_CODE (expr) == IMAGPART_EXPR)
3366 && !useless_type_conversion_p (TREE_TYPE (expr),
3367 TREE_TYPE (TREE_TYPE (op))))
3369 error ("type mismatch in real/imagpart reference");
3370 debug_generic_stmt (TREE_TYPE (expr));
3371 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3372 return true;
3375 if (TREE_CODE (expr) == COMPONENT_REF
3376 && !useless_type_conversion_p (TREE_TYPE (expr),
3377 TREE_TYPE (TREE_OPERAND (expr, 1))))
3379 error ("type mismatch in component reference");
3380 debug_generic_stmt (TREE_TYPE (expr));
3381 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3382 return true;
3385 /* For VIEW_CONVERT_EXPRs which are allowed here, too, there
3386 is nothing to verify. Gross mismatches at most invoke
3387 undefined behavior. */
3388 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR
3389 && !handled_component_p (op))
3390 return false;
3392 expr = op;
3395 return ((require_lvalue || !is_gimple_min_invariant (expr))
3396 && verify_types_in_gimple_min_lval (expr));
3399 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3400 list of pointer-to types that is trivially convertible to DEST. */
3402 static bool
3403 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3405 tree src;
3407 if (!TYPE_POINTER_TO (src_obj))
3408 return true;
3410 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3411 if (useless_type_conversion_p (dest, src))
3412 return true;
3414 return false;
3417 /* Return true if TYPE1 is a fixed-point type and if conversions to and
3418 from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
3420 static bool
3421 valid_fixed_convert_types_p (tree type1, tree type2)
3423 return (FIXED_POINT_TYPE_P (type1)
3424 && (INTEGRAL_TYPE_P (type2)
3425 || SCALAR_FLOAT_TYPE_P (type2)
3426 || FIXED_POINT_TYPE_P (type2)));
3429 /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
3430 is a problem, otherwise false. */
3432 static bool
3433 verify_gimple_call (gimple stmt)
3435 tree fn = gimple_call_fn (stmt);
3436 tree fntype;
3438 if (!POINTER_TYPE_P (TREE_TYPE (fn))
3439 || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
3440 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE))
3442 error ("non-function in gimple call");
3443 return true;
3446 if (gimple_call_lhs (stmt)
3447 && !is_gimple_lvalue (gimple_call_lhs (stmt)))
3449 error ("invalid LHS in gimple call");
3450 return true;
3453 fntype = TREE_TYPE (TREE_TYPE (fn));
3454 if (gimple_call_lhs (stmt)
3455 && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
3456 TREE_TYPE (fntype))
3457 /* ??? At least C++ misses conversions at assignments from
3458 void * call results.
3459 ??? Java is completely off. Especially with functions
3460 returning java.lang.Object.
3461 For now simply allow arbitrary pointer type conversions. */
3462 && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
3463 && POINTER_TYPE_P (TREE_TYPE (fntype))))
3465 error ("invalid conversion in gimple call");
3466 debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
3467 debug_generic_stmt (TREE_TYPE (fntype));
3468 return true;
3471 /* ??? The C frontend passes unpromoted arguments in case it
3472 didn't see a function declaration before the call. So for now
3473 leave the call arguments unverified. Once we gimplify
3474 unit-at-a-time we have a chance to fix this. */
3476 return false;
3479 /* Verifies the gimple comparison with the result type TYPE and
3480 the operands OP0 and OP1. */
3482 static bool
3483 verify_gimple_comparison (tree type, tree op0, tree op1)
3485 tree op0_type = TREE_TYPE (op0);
3486 tree op1_type = TREE_TYPE (op1);
3488 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3490 error ("invalid operands in gimple comparison");
3491 return true;
3494 /* For comparisons we do not have the operations type as the
3495 effective type the comparison is carried out in. Instead
3496 we require that either the first operand is trivially
3497 convertible into the second, or the other way around.
3498 The resulting type of a comparison may be any integral type.
3499 Because we special-case pointers to void we allow
3500 comparisons of pointers with the same mode as well. */
3501 if ((!useless_type_conversion_p (op0_type, op1_type)
3502 && !useless_type_conversion_p (op1_type, op0_type)
3503 && (!POINTER_TYPE_P (op0_type)
3504 || !POINTER_TYPE_P (op1_type)
3505 || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
3506 || !INTEGRAL_TYPE_P (type))
3508 error ("type mismatch in comparison expression");
3509 debug_generic_expr (type);
3510 debug_generic_expr (op0_type);
3511 debug_generic_expr (op1_type);
3512 return true;
3515 return false;
3518 /* Verify a gimple assignment statement STMT with an unary rhs.
3519 Returns true if anything is wrong. */
3521 static bool
3522 verify_gimple_assign_unary (gimple stmt)
3524 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3525 tree lhs = gimple_assign_lhs (stmt);
3526 tree lhs_type = TREE_TYPE (lhs);
3527 tree rhs1 = gimple_assign_rhs1 (stmt);
3528 tree rhs1_type = TREE_TYPE (rhs1);
3530 if (!is_gimple_reg (lhs)
3531 && !(optimize == 0
3532 && TREE_CODE (lhs_type) == COMPLEX_TYPE))
3534 error ("non-register as LHS of unary operation");
3535 return true;
3538 if (!is_gimple_val (rhs1))
3540 error ("invalid operand in unary operation");
3541 return true;
3544 /* First handle conversions. */
3545 switch (rhs_code)
3547 CASE_CONVERT:
3549 /* Allow conversions between integral types and pointers only if
3550 there is no sign or zero extension involved.
3551 For targets were the precision of sizetype doesn't match that
3552 of pointers we need to allow arbitrary conversions from and
3553 to sizetype. */
3554 if ((POINTER_TYPE_P (lhs_type)
3555 && INTEGRAL_TYPE_P (rhs1_type)
3556 && (TYPE_PRECISION (lhs_type) >= TYPE_PRECISION (rhs1_type)
3557 || rhs1_type == sizetype))
3558 || (POINTER_TYPE_P (rhs1_type)
3559 && INTEGRAL_TYPE_P (lhs_type)
3560 && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
3561 || lhs_type == sizetype)))
3562 return false;
3564 /* Allow conversion from integer to offset type and vice versa. */
3565 if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3566 && TREE_CODE (rhs1_type) == INTEGER_TYPE)
3567 || (TREE_CODE (lhs_type) == INTEGER_TYPE
3568 && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3569 return false;
3571 /* Otherwise assert we are converting between types of the
3572 same kind. */
3573 if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3575 error ("invalid types in nop conversion");
3576 debug_generic_expr (lhs_type);
3577 debug_generic_expr (rhs1_type);
3578 return true;
3581 return false;
3584 case FIXED_CONVERT_EXPR:
3586 if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3587 && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3589 error ("invalid types in fixed-point conversion");
3590 debug_generic_expr (lhs_type);
3591 debug_generic_expr (rhs1_type);
3592 return true;
3595 return false;
3598 case FLOAT_EXPR:
3600 if (!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3602 error ("invalid types in conversion to floating point");
3603 debug_generic_expr (lhs_type);
3604 debug_generic_expr (rhs1_type);
3605 return true;
3608 return false;
3611 case FIX_TRUNC_EXPR:
3613 if (!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3615 error ("invalid types in conversion to integer");
3616 debug_generic_expr (lhs_type);
3617 debug_generic_expr (rhs1_type);
3618 return true;
3621 return false;
3624 case VEC_UNPACK_HI_EXPR:
3625 case VEC_UNPACK_LO_EXPR:
3626 case REDUC_MAX_EXPR:
3627 case REDUC_MIN_EXPR:
3628 case REDUC_PLUS_EXPR:
3629 case VEC_UNPACK_FLOAT_HI_EXPR:
3630 case VEC_UNPACK_FLOAT_LO_EXPR:
3631 /* FIXME. */
3632 return false;
3634 case TRUTH_NOT_EXPR:
3635 case NEGATE_EXPR:
3636 case ABS_EXPR:
3637 case BIT_NOT_EXPR:
3638 case PAREN_EXPR:
3639 case NON_LVALUE_EXPR:
3640 case CONJ_EXPR:
3641 break;
3643 default:
3644 gcc_unreachable ();
3647 /* For the remaining codes assert there is no conversion involved. */
3648 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3650 error ("non-trivial conversion in unary operation");
3651 debug_generic_expr (lhs_type);
3652 debug_generic_expr (rhs1_type);
3653 return true;
3656 return false;
3659 /* Verify a gimple assignment statement STMT with a binary rhs.
3660 Returns true if anything is wrong. */
3662 static bool
3663 verify_gimple_assign_binary (gimple stmt)
3665 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3666 tree lhs = gimple_assign_lhs (stmt);
3667 tree lhs_type = TREE_TYPE (lhs);
3668 tree rhs1 = gimple_assign_rhs1 (stmt);
3669 tree rhs1_type = TREE_TYPE (rhs1);
3670 tree rhs2 = gimple_assign_rhs2 (stmt);
3671 tree rhs2_type = TREE_TYPE (rhs2);
3673 if (!is_gimple_reg (lhs)
3674 && !(optimize == 0
3675 && TREE_CODE (lhs_type) == COMPLEX_TYPE))
3677 error ("non-register as LHS of binary operation");
3678 return true;
3681 if (!is_gimple_val (rhs1)
3682 || !is_gimple_val (rhs2))
3684 error ("invalid operands in binary operation");
3685 return true;
3688 /* First handle operations that involve different types. */
3689 switch (rhs_code)
3691 case COMPLEX_EXPR:
3693 if (TREE_CODE (lhs_type) != COMPLEX_TYPE
3694 || !(INTEGRAL_TYPE_P (rhs1_type)
3695 || SCALAR_FLOAT_TYPE_P (rhs1_type))
3696 || !(INTEGRAL_TYPE_P (rhs2_type)
3697 || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3699 error ("type mismatch in complex expression");
3700 debug_generic_expr (lhs_type);
3701 debug_generic_expr (rhs1_type);
3702 debug_generic_expr (rhs2_type);
3703 return true;
3706 return false;
3709 case LSHIFT_EXPR:
3710 case RSHIFT_EXPR:
3711 case LROTATE_EXPR:
3712 case RROTATE_EXPR:
3714 /* Shifts and rotates are ok on integral types, fixed point
3715 types and integer vector types. */
3716 if ((!INTEGRAL_TYPE_P (rhs1_type)
3717 && !FIXED_POINT_TYPE_P (rhs1_type)
3718 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3719 && TREE_CODE (TREE_TYPE (rhs1_type)) == INTEGER_TYPE))
3720 || (!INTEGRAL_TYPE_P (rhs2_type)
3721 /* Vector shifts of vectors are also ok. */
3722 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3723 && TREE_CODE (TREE_TYPE (rhs1_type)) == INTEGER_TYPE
3724 && TREE_CODE (rhs2_type) == VECTOR_TYPE
3725 && TREE_CODE (TREE_TYPE (rhs2_type)) == INTEGER_TYPE))
3726 || !useless_type_conversion_p (lhs_type, rhs1_type))
3728 error ("type mismatch in shift expression");
3729 debug_generic_expr (lhs_type);
3730 debug_generic_expr (rhs1_type);
3731 debug_generic_expr (rhs2_type);
3732 return true;
3735 return false;
3738 case VEC_LSHIFT_EXPR:
3739 case VEC_RSHIFT_EXPR:
3741 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3742 || !(INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3743 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1_type))
3744 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
3745 || (!INTEGRAL_TYPE_P (rhs2_type)
3746 && (TREE_CODE (rhs2_type) != VECTOR_TYPE
3747 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3748 || !useless_type_conversion_p (lhs_type, rhs1_type))
3750 error ("type mismatch in vector shift expression");
3751 debug_generic_expr (lhs_type);
3752 debug_generic_expr (rhs1_type);
3753 debug_generic_expr (rhs2_type);
3754 return true;
3756 /* For shifting a vector of floating point components we
3757 only allow shifting by a constant multiple of the element size. */
3758 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type))
3759 && (TREE_CODE (rhs2) != INTEGER_CST
3760 || !div_if_zero_remainder (EXACT_DIV_EXPR, rhs2,
3761 TYPE_SIZE (TREE_TYPE (rhs1_type)))))
3763 error ("non-element sized vector shift of floating point vector");
3764 return true;
3767 return false;
3770 case PLUS_EXPR:
3772 /* We use regular PLUS_EXPR for vectors.
3773 ??? This just makes the checker happy and may not be what is
3774 intended. */
3775 if (TREE_CODE (lhs_type) == VECTOR_TYPE
3776 && POINTER_TYPE_P (TREE_TYPE (lhs_type)))
3778 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3779 || TREE_CODE (rhs2_type) != VECTOR_TYPE)
3781 error ("invalid non-vector operands to vector valued plus");
3782 return true;
3784 lhs_type = TREE_TYPE (lhs_type);
3785 rhs1_type = TREE_TYPE (rhs1_type);
3786 rhs2_type = TREE_TYPE (rhs2_type);
3787 /* PLUS_EXPR is commutative, so we might end up canonicalizing
3788 the pointer to 2nd place. */
3789 if (POINTER_TYPE_P (rhs2_type))
3791 tree tem = rhs1_type;
3792 rhs1_type = rhs2_type;
3793 rhs2_type = tem;
3795 goto do_pointer_plus_expr_check;
3798 /* Fallthru. */
3799 case MINUS_EXPR:
3801 if (POINTER_TYPE_P (lhs_type)
3802 || POINTER_TYPE_P (rhs1_type)
3803 || POINTER_TYPE_P (rhs2_type))
3805 error ("invalid (pointer) operands to plus/minus");
3806 return true;
3809 /* Continue with generic binary expression handling. */
3810 break;
3813 case POINTER_PLUS_EXPR:
3815 do_pointer_plus_expr_check:
3816 if (!POINTER_TYPE_P (rhs1_type)
3817 || !useless_type_conversion_p (lhs_type, rhs1_type)
3818 || !useless_type_conversion_p (sizetype, rhs2_type))
3820 error ("type mismatch in pointer plus expression");
3821 debug_generic_stmt (lhs_type);
3822 debug_generic_stmt (rhs1_type);
3823 debug_generic_stmt (rhs2_type);
3824 return true;
3827 return false;
3830 case TRUTH_ANDIF_EXPR:
3831 case TRUTH_ORIF_EXPR:
3832 gcc_unreachable ();
3834 case TRUTH_AND_EXPR:
3835 case TRUTH_OR_EXPR:
3836 case TRUTH_XOR_EXPR:
3838 /* We allow any kind of integral typed argument and result. */
3839 if (!INTEGRAL_TYPE_P (rhs1_type)
3840 || !INTEGRAL_TYPE_P (rhs2_type)
3841 || !INTEGRAL_TYPE_P (lhs_type))
3843 error ("type mismatch in binary truth expression");
3844 debug_generic_expr (lhs_type);
3845 debug_generic_expr (rhs1_type);
3846 debug_generic_expr (rhs2_type);
3847 return true;
3850 return false;
3853 case LT_EXPR:
3854 case LE_EXPR:
3855 case GT_EXPR:
3856 case GE_EXPR:
3857 case EQ_EXPR:
3858 case NE_EXPR:
3859 case UNORDERED_EXPR:
3860 case ORDERED_EXPR:
3861 case UNLT_EXPR:
3862 case UNLE_EXPR:
3863 case UNGT_EXPR:
3864 case UNGE_EXPR:
3865 case UNEQ_EXPR:
3866 case LTGT_EXPR:
3867 /* Comparisons are also binary, but the result type is not
3868 connected to the operand types. */
3869 return verify_gimple_comparison (lhs_type, rhs1, rhs2);
3871 case WIDEN_SUM_EXPR:
3872 case WIDEN_MULT_EXPR:
3873 case VEC_WIDEN_MULT_HI_EXPR:
3874 case VEC_WIDEN_MULT_LO_EXPR:
3875 case VEC_PACK_TRUNC_EXPR:
3876 case VEC_PACK_SAT_EXPR:
3877 case VEC_PACK_FIX_TRUNC_EXPR:
3878 case VEC_EXTRACT_EVEN_EXPR:
3879 case VEC_EXTRACT_ODD_EXPR:
3880 case VEC_INTERLEAVE_HIGH_EXPR:
3881 case VEC_INTERLEAVE_LOW_EXPR:
3882 /* FIXME. */
3883 return false;
3885 case MULT_EXPR:
3886 case TRUNC_DIV_EXPR:
3887 case CEIL_DIV_EXPR:
3888 case FLOOR_DIV_EXPR:
3889 case ROUND_DIV_EXPR:
3890 case TRUNC_MOD_EXPR:
3891 case CEIL_MOD_EXPR:
3892 case FLOOR_MOD_EXPR:
3893 case ROUND_MOD_EXPR:
3894 case RDIV_EXPR:
3895 case EXACT_DIV_EXPR:
3896 case MIN_EXPR:
3897 case MAX_EXPR:
3898 case BIT_IOR_EXPR:
3899 case BIT_XOR_EXPR:
3900 case BIT_AND_EXPR:
3901 /* Continue with generic binary expression handling. */
3902 break;
3904 default:
3905 gcc_unreachable ();
3908 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3909 || !useless_type_conversion_p (lhs_type, rhs2_type))
3911 error ("type mismatch in binary expression");
3912 debug_generic_stmt (lhs_type);
3913 debug_generic_stmt (rhs1_type);
3914 debug_generic_stmt (rhs2_type);
3915 return true;
3918 return false;
3921 /* Verify a gimple assignment statement STMT with a single rhs.
3922 Returns true if anything is wrong. */
3924 static bool
3925 verify_gimple_assign_single (gimple stmt)
3927 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3928 tree lhs = gimple_assign_lhs (stmt);
3929 tree lhs_type = TREE_TYPE (lhs);
3930 tree rhs1 = gimple_assign_rhs1 (stmt);
3931 tree rhs1_type = TREE_TYPE (rhs1);
3932 bool res = false;
3934 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3936 error ("non-trivial conversion at assignment");
3937 debug_generic_expr (lhs_type);
3938 debug_generic_expr (rhs1_type);
3939 return true;
3942 if (handled_component_p (lhs))
3943 res |= verify_types_in_gimple_reference (lhs, true);
3945 /* Special codes we cannot handle via their class. */
3946 switch (rhs_code)
3948 case ADDR_EXPR:
3950 tree op = TREE_OPERAND (rhs1, 0);
3951 if (!is_gimple_addressable (op))
3953 error ("invalid operand in unary expression");
3954 return true;
3957 if (!types_compatible_p (TREE_TYPE (op), TREE_TYPE (TREE_TYPE (rhs1)))
3958 && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
3959 TREE_TYPE (op)))
3961 error ("type mismatch in address expression");
3962 debug_generic_stmt (TREE_TYPE (rhs1));
3963 debug_generic_stmt (TREE_TYPE (op));
3964 return true;
3967 return verify_types_in_gimple_reference (op, true);
3970 /* tcc_reference */
3971 case COMPONENT_REF:
3972 case BIT_FIELD_REF:
3973 case INDIRECT_REF:
3974 case ALIGN_INDIRECT_REF:
3975 case MISALIGNED_INDIRECT_REF:
3976 case ARRAY_REF:
3977 case ARRAY_RANGE_REF:
3978 case VIEW_CONVERT_EXPR:
3979 case REALPART_EXPR:
3980 case IMAGPART_EXPR:
3981 case TARGET_MEM_REF:
3982 if (!is_gimple_reg (lhs)
3983 && is_gimple_reg_type (TREE_TYPE (lhs)))
3985 error ("invalid rhs for gimple memory store");
3986 debug_generic_stmt (lhs);
3987 debug_generic_stmt (rhs1);
3988 return true;
3990 return res || verify_types_in_gimple_reference (rhs1, false);
3992 /* tcc_constant */
3993 case SSA_NAME:
3994 case INTEGER_CST:
3995 case REAL_CST:
3996 case FIXED_CST:
3997 case COMPLEX_CST:
3998 case VECTOR_CST:
3999 case STRING_CST:
4000 return res;
4002 /* tcc_declaration */
4003 case CONST_DECL:
4004 return res;
4005 case VAR_DECL:
4006 case PARM_DECL:
4007 if (!is_gimple_reg (lhs)
4008 && !is_gimple_reg (rhs1)
4009 && is_gimple_reg_type (TREE_TYPE (lhs)))
4011 error ("invalid rhs for gimple memory store");
4012 debug_generic_stmt (lhs);
4013 debug_generic_stmt (rhs1);
4014 return true;
4016 return res;
4018 case COND_EXPR:
4019 case CONSTRUCTOR:
4020 case OBJ_TYPE_REF:
4021 case ASSERT_EXPR:
4022 case WITH_SIZE_EXPR:
4023 case EXC_PTR_EXPR:
4024 case FILTER_EXPR:
4025 case POLYNOMIAL_CHREC:
4026 case DOT_PROD_EXPR:
4027 case VEC_COND_EXPR:
4028 case REALIGN_LOAD_EXPR:
4029 /* FIXME. */
4030 return res;
4032 default:;
4035 return res;
4038 /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
4039 is a problem, otherwise false. */
4041 static bool
4042 verify_gimple_assign (gimple stmt)
4044 switch (gimple_assign_rhs_class (stmt))
4046 case GIMPLE_SINGLE_RHS:
4047 return verify_gimple_assign_single (stmt);
4049 case GIMPLE_UNARY_RHS:
4050 return verify_gimple_assign_unary (stmt);
4052 case GIMPLE_BINARY_RHS:
4053 return verify_gimple_assign_binary (stmt);
4055 default:
4056 gcc_unreachable ();
4060 /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
4061 is a problem, otherwise false. */
4063 static bool
4064 verify_gimple_return (gimple stmt)
4066 tree op = gimple_return_retval (stmt);
4067 tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
4069 /* We cannot test for present return values as we do not fix up missing
4070 return values from the original source. */
4071 if (op == NULL)
4072 return false;
4074 if (!is_gimple_val (op)
4075 && TREE_CODE (op) != RESULT_DECL)
4077 error ("invalid operand in return statement");
4078 debug_generic_stmt (op);
4079 return true;
4082 if (!useless_type_conversion_p (restype, TREE_TYPE (op))
4083 /* ??? With C++ we can have the situation that the result
4084 decl is a reference type while the return type is an aggregate. */
4085 && !(TREE_CODE (op) == RESULT_DECL
4086 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE
4087 && useless_type_conversion_p (restype, TREE_TYPE (TREE_TYPE (op)))))
4089 error ("invalid conversion in return statement");
4090 debug_generic_stmt (restype);
4091 debug_generic_stmt (TREE_TYPE (op));
4092 return true;
4095 return false;
4099 /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
4100 is a problem, otherwise false. */
4102 static bool
4103 verify_gimple_goto (gimple stmt)
4105 tree dest = gimple_goto_dest (stmt);
4107 /* ??? We have two canonical forms of direct goto destinations, a
4108 bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
4109 if (TREE_CODE (dest) != LABEL_DECL
4110 && (!is_gimple_val (dest)
4111 || !POINTER_TYPE_P (TREE_TYPE (dest))))
4113 error ("goto destination is neither a label nor a pointer");
4114 return true;
4117 return false;
4120 /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
4121 is a problem, otherwise false. */
4123 static bool
4124 verify_gimple_switch (gimple stmt)
4126 if (!is_gimple_val (gimple_switch_index (stmt)))
4128 error ("invalid operand to switch statement");
4129 debug_generic_stmt (gimple_switch_index (stmt));
4130 return true;
4133 return false;
4137 /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
4138 and false otherwise. */
4140 static bool
4141 verify_gimple_phi (gimple stmt)
4143 tree type = TREE_TYPE (gimple_phi_result (stmt));
4144 unsigned i;
4146 if (!is_gimple_variable (gimple_phi_result (stmt)))
4148 error ("Invalid PHI result");
4149 return true;
4152 for (i = 0; i < gimple_phi_num_args (stmt); i++)
4154 tree arg = gimple_phi_arg_def (stmt, i);
4155 if ((is_gimple_reg (gimple_phi_result (stmt))
4156 && !is_gimple_val (arg))
4157 || (!is_gimple_reg (gimple_phi_result (stmt))
4158 && !is_gimple_addressable (arg)))
4160 error ("Invalid PHI argument");
4161 debug_generic_stmt (arg);
4162 return true;
4164 if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
4166 error ("Incompatible types in PHI argument %u", i);
4167 debug_generic_stmt (type);
4168 debug_generic_stmt (TREE_TYPE (arg));
4169 return true;
4173 return false;
4177 /* Verify a gimple debug statement STMT.
4178 Returns true if anything is wrong. */
4180 static bool
4181 verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
4183 /* There isn't much that could be wrong in a gimple debug stmt. A
4184 gimple debug bind stmt, for example, maps a tree, that's usually
4185 a VAR_DECL or a PARM_DECL, but that could also be some scalarized
4186 component or member of an aggregate type, to another tree, that
4187 can be an arbitrary expression. These stmts expand into debug
4188 insns, and are converted to debug notes by var-tracking.c. */
4189 return false;
4193 /* Verify the GIMPLE statement STMT. Returns true if there is an
4194 error, otherwise false. */
4196 static bool
4197 verify_types_in_gimple_stmt (gimple stmt)
4199 if (is_gimple_omp (stmt))
4201 /* OpenMP directives are validated by the FE and never operated
4202 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
4203 non-gimple expressions when the main index variable has had
4204 its address taken. This does not affect the loop itself
4205 because the header of an GIMPLE_OMP_FOR is merely used to determine
4206 how to setup the parallel iteration. */
4207 return false;
4210 switch (gimple_code (stmt))
4212 case GIMPLE_ASSIGN:
4213 return verify_gimple_assign (stmt);
4215 case GIMPLE_LABEL:
4216 return TREE_CODE (gimple_label_label (stmt)) != LABEL_DECL;
4218 case GIMPLE_CALL:
4219 return verify_gimple_call (stmt);
4221 case GIMPLE_COND:
4222 return verify_gimple_comparison (boolean_type_node,
4223 gimple_cond_lhs (stmt),
4224 gimple_cond_rhs (stmt));
4226 case GIMPLE_GOTO:
4227 return verify_gimple_goto (stmt);
4229 case GIMPLE_SWITCH:
4230 return verify_gimple_switch (stmt);
4232 case GIMPLE_RETURN:
4233 return verify_gimple_return (stmt);
4235 case GIMPLE_ASM:
4236 return false;
4238 case GIMPLE_PHI:
4239 return verify_gimple_phi (stmt);
4241 /* Tuples that do not have tree operands. */
4242 case GIMPLE_NOP:
4243 case GIMPLE_RESX:
4244 case GIMPLE_PREDICT:
4245 return false;
4247 case GIMPLE_DEBUG:
4248 return verify_gimple_debug (stmt);
4250 default:
4251 gcc_unreachable ();
4255 /* Verify the GIMPLE statements inside the sequence STMTS. */
4257 static bool
4258 verify_types_in_gimple_seq_2 (gimple_seq stmts)
4260 gimple_stmt_iterator ittr;
4261 bool err = false;
4263 for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
4265 gimple stmt = gsi_stmt (ittr);
4267 switch (gimple_code (stmt))
4269 case GIMPLE_BIND:
4270 err |= verify_types_in_gimple_seq_2 (gimple_bind_body (stmt));
4271 break;
4273 case GIMPLE_TRY:
4274 err |= verify_types_in_gimple_seq_2 (gimple_try_eval (stmt));
4275 err |= verify_types_in_gimple_seq_2 (gimple_try_cleanup (stmt));
4276 break;
4278 case GIMPLE_EH_FILTER:
4279 err |= verify_types_in_gimple_seq_2 (gimple_eh_filter_failure (stmt));
4280 break;
4282 case GIMPLE_CATCH:
4283 err |= verify_types_in_gimple_seq_2 (gimple_catch_handler (stmt));
4284 break;
4286 default:
4288 bool err2 = verify_types_in_gimple_stmt (stmt);
4289 if (err2)
4290 debug_gimple_stmt (stmt);
4291 err |= err2;
4296 return err;
4300 /* Verify the GIMPLE statements inside the statement list STMTS. */
4302 void
4303 verify_types_in_gimple_seq (gimple_seq stmts)
4305 if (verify_types_in_gimple_seq_2 (stmts))
4306 internal_error ("verify_gimple failed");
4310 /* Verify STMT, return true if STMT is not in GIMPLE form.
4311 TODO: Implement type checking. */
4313 static bool
4314 verify_stmt (gimple_stmt_iterator *gsi)
4316 tree addr;
4317 struct walk_stmt_info wi;
4318 bool last_in_block = gsi_one_before_end_p (*gsi);
4319 gimple stmt = gsi_stmt (*gsi);
4321 if (is_gimple_omp (stmt))
4323 /* OpenMP directives are validated by the FE and never operated
4324 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
4325 non-gimple expressions when the main index variable has had
4326 its address taken. This does not affect the loop itself
4327 because the header of an GIMPLE_OMP_FOR is merely used to determine
4328 how to setup the parallel iteration. */
4329 return false;
4332 /* FIXME. The C frontend passes unpromoted arguments in case it
4333 didn't see a function declaration before the call. */
4334 if (is_gimple_call (stmt))
4336 tree decl;
4338 if (!is_gimple_call_addr (gimple_call_fn (stmt)))
4340 error ("invalid function in call statement");
4341 return true;
4344 decl = gimple_call_fndecl (stmt);
4345 if (decl
4346 && TREE_CODE (decl) == FUNCTION_DECL
4347 && DECL_LOOPING_CONST_OR_PURE_P (decl)
4348 && (!DECL_PURE_P (decl))
4349 && (!TREE_READONLY (decl)))
4351 error ("invalid pure const state for function");
4352 return true;
4356 if (is_gimple_debug (stmt))
4357 return false;
4359 memset (&wi, 0, sizeof (wi));
4360 addr = walk_gimple_op (gsi_stmt (*gsi), verify_expr, &wi);
4361 if (addr)
4363 debug_generic_expr (addr);
4364 inform (gimple_location (gsi_stmt (*gsi)), "in statement");
4365 debug_gimple_stmt (stmt);
4366 return true;
4369 /* If the statement is marked as part of an EH region, then it is
4370 expected that the statement could throw. Verify that when we
4371 have optimizations that simplify statements such that we prove
4372 that they cannot throw, that we update other data structures
4373 to match. */
4374 if (lookup_stmt_eh_region (stmt) >= 0)
4376 /* During IPA passes, ipa-pure-const sets nothrow flags on calls
4377 and they are updated on statements only after fixup_cfg
4378 is executed at beggining of expansion stage. */
4379 if (!stmt_could_throw_p (stmt) && cgraph_state != CGRAPH_STATE_IPA_SSA)
4381 error ("statement marked for throw, but doesn%'t");
4382 goto fail;
4384 if (!last_in_block && stmt_can_throw_internal (stmt))
4386 error ("statement marked for throw in middle of block");
4387 goto fail;
4391 return false;
4393 fail:
4394 debug_gimple_stmt (stmt);
4395 return true;
4399 /* Return true when the T can be shared. */
4401 static bool
4402 tree_node_can_be_shared (tree t)
4404 if (IS_TYPE_OR_DECL_P (t)
4405 || is_gimple_min_invariant (t)
4406 || TREE_CODE (t) == SSA_NAME
4407 || t == error_mark_node
4408 || TREE_CODE (t) == IDENTIFIER_NODE)
4409 return true;
4411 if (TREE_CODE (t) == CASE_LABEL_EXPR)
4412 return true;
4414 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
4415 && is_gimple_min_invariant (TREE_OPERAND (t, 1)))
4416 || TREE_CODE (t) == COMPONENT_REF
4417 || TREE_CODE (t) == REALPART_EXPR
4418 || TREE_CODE (t) == IMAGPART_EXPR)
4419 t = TREE_OPERAND (t, 0);
4421 if (DECL_P (t))
4422 return true;
4424 return false;
4428 /* Called via walk_gimple_stmt. Verify tree sharing. */
4430 static tree
4431 verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
4433 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4434 struct pointer_set_t *visited = (struct pointer_set_t *) wi->info;
4436 if (tree_node_can_be_shared (*tp))
4438 *walk_subtrees = false;
4439 return NULL;
4442 if (pointer_set_insert (visited, *tp))
4443 return *tp;
4445 return NULL;
4449 static bool eh_error_found;
4450 static int
4451 verify_eh_throw_stmt_node (void **slot, void *data)
4453 struct throw_stmt_node *node = (struct throw_stmt_node *)*slot;
4454 struct pointer_set_t *visited = (struct pointer_set_t *) data;
4456 if (!pointer_set_contains (visited, node->stmt))
4458 error ("Dead STMT in EH table");
4459 debug_gimple_stmt (node->stmt);
4460 eh_error_found = true;
4462 return 1;
4466 /* Verify the GIMPLE statements in every basic block. */
4468 void
4469 verify_stmts (void)
4471 basic_block bb;
4472 gimple_stmt_iterator gsi;
4473 bool err = false;
4474 struct pointer_set_t *visited, *visited_stmts;
4475 tree addr;
4476 struct walk_stmt_info wi;
4478 timevar_push (TV_TREE_STMT_VERIFY);
4479 visited = pointer_set_create ();
4480 visited_stmts = pointer_set_create ();
4482 memset (&wi, 0, sizeof (wi));
4483 wi.info = (void *) visited;
4485 FOR_EACH_BB (bb)
4487 gimple phi;
4488 size_t i;
4490 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4492 phi = gsi_stmt (gsi);
4493 pointer_set_insert (visited_stmts, phi);
4494 if (gimple_bb (phi) != bb)
4496 error ("gimple_bb (phi) is set to a wrong basic block");
4497 err |= true;
4500 for (i = 0; i < gimple_phi_num_args (phi); i++)
4502 tree t = gimple_phi_arg_def (phi, i);
4503 tree addr;
4505 if (!t)
4507 error ("missing PHI def");
4508 debug_gimple_stmt (phi);
4509 err |= true;
4510 continue;
4512 /* Addressable variables do have SSA_NAMEs but they
4513 are not considered gimple values. */
4514 else if (TREE_CODE (t) != SSA_NAME
4515 && TREE_CODE (t) != FUNCTION_DECL
4516 && !is_gimple_min_invariant (t))
4518 error ("PHI argument is not a GIMPLE value");
4519 debug_gimple_stmt (phi);
4520 debug_generic_expr (t);
4521 err |= true;
4524 addr = walk_tree (&t, verify_node_sharing, visited, NULL);
4525 if (addr)
4527 error ("incorrect sharing of tree nodes");
4528 debug_gimple_stmt (phi);
4529 debug_generic_expr (addr);
4530 err |= true;
4534 #ifdef ENABLE_TYPES_CHECKING
4535 if (verify_gimple_phi (phi))
4537 debug_gimple_stmt (phi);
4538 err |= true;
4540 #endif
4543 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
4545 gimple stmt = gsi_stmt (gsi);
4547 if (gimple_code (stmt) == GIMPLE_WITH_CLEANUP_EXPR
4548 || gimple_code (stmt) == GIMPLE_BIND)
4550 error ("invalid GIMPLE statement");
4551 debug_gimple_stmt (stmt);
4552 err |= true;
4555 pointer_set_insert (visited_stmts, stmt);
4557 if (gimple_bb (stmt) != bb)
4559 error ("gimple_bb (stmt) is set to a wrong basic block");
4560 debug_gimple_stmt (stmt);
4561 err |= true;
4564 if (gimple_code (stmt) == GIMPLE_LABEL)
4566 tree decl = gimple_label_label (stmt);
4567 int uid = LABEL_DECL_UID (decl);
4569 if (uid == -1
4570 || VEC_index (basic_block, label_to_block_map, uid) != bb)
4572 error ("incorrect entry in label_to_block_map.\n");
4573 err |= true;
4577 err |= verify_stmt (&gsi);
4579 #ifdef ENABLE_TYPES_CHECKING
4580 if (verify_types_in_gimple_stmt (gsi_stmt (gsi)))
4582 debug_gimple_stmt (stmt);
4583 err |= true;
4585 #endif
4586 addr = walk_gimple_op (gsi_stmt (gsi), verify_node_sharing, &wi);
4587 if (addr)
4589 error ("incorrect sharing of tree nodes");
4590 debug_gimple_stmt (stmt);
4591 debug_generic_expr (addr);
4592 err |= true;
4594 gsi_next (&gsi);
4598 eh_error_found = false;
4599 if (get_eh_throw_stmt_table (cfun))
4600 htab_traverse (get_eh_throw_stmt_table (cfun),
4601 verify_eh_throw_stmt_node,
4602 visited_stmts);
4604 if (err | eh_error_found)
4605 internal_error ("verify_stmts failed");
4607 pointer_set_destroy (visited);
4608 pointer_set_destroy (visited_stmts);
4609 verify_histograms ();
4610 timevar_pop (TV_TREE_STMT_VERIFY);
4614 /* Verifies that the flow information is OK. */
4616 static int
4617 gimple_verify_flow_info (void)
4619 int err = 0;
4620 basic_block bb;
4621 gimple_stmt_iterator gsi;
4622 gimple stmt;
4623 edge e;
4624 edge_iterator ei;
4626 if (ENTRY_BLOCK_PTR->il.gimple)
4628 error ("ENTRY_BLOCK has IL associated with it");
4629 err = 1;
4632 if (EXIT_BLOCK_PTR->il.gimple)
4634 error ("EXIT_BLOCK has IL associated with it");
4635 err = 1;
4638 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
4639 if (e->flags & EDGE_FALLTHRU)
4641 error ("fallthru to exit from bb %d", e->src->index);
4642 err = 1;
4645 FOR_EACH_BB (bb)
4647 bool found_ctrl_stmt = false;
4649 stmt = NULL;
4651 /* Skip labels on the start of basic block. */
4652 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4654 tree label;
4655 gimple prev_stmt = stmt;
4657 stmt = gsi_stmt (gsi);
4659 if (gimple_code (stmt) != GIMPLE_LABEL)
4660 break;
4662 label = gimple_label_label (stmt);
4663 if (prev_stmt && DECL_NONLOCAL (label))
4665 error ("nonlocal label ");
4666 print_generic_expr (stderr, label, 0);
4667 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4668 bb->index);
4669 err = 1;
4672 if (label_to_block (label) != bb)
4674 error ("label ");
4675 print_generic_expr (stderr, label, 0);
4676 fprintf (stderr, " to block does not match in bb %d",
4677 bb->index);
4678 err = 1;
4681 if (decl_function_context (label) != current_function_decl)
4683 error ("label ");
4684 print_generic_expr (stderr, label, 0);
4685 fprintf (stderr, " has incorrect context in bb %d",
4686 bb->index);
4687 err = 1;
4691 /* Verify that body of basic block BB is free of control flow. */
4692 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4694 gimple stmt = gsi_stmt (gsi);
4696 if (found_ctrl_stmt)
4698 error ("control flow in the middle of basic block %d",
4699 bb->index);
4700 err = 1;
4703 if (stmt_ends_bb_p (stmt))
4704 found_ctrl_stmt = true;
4706 if (gimple_code (stmt) == GIMPLE_LABEL)
4708 error ("label ");
4709 print_generic_expr (stderr, gimple_label_label (stmt), 0);
4710 fprintf (stderr, " in the middle of basic block %d", bb->index);
4711 err = 1;
4715 gsi = gsi_last_bb (bb);
4716 if (gsi_end_p (gsi))
4717 continue;
4719 stmt = gsi_stmt (gsi);
4721 err |= verify_eh_edges (stmt);
4723 if (is_ctrl_stmt (stmt))
4725 FOR_EACH_EDGE (e, ei, bb->succs)
4726 if (e->flags & EDGE_FALLTHRU)
4728 error ("fallthru edge after a control statement in bb %d",
4729 bb->index);
4730 err = 1;
4734 if (gimple_code (stmt) != GIMPLE_COND)
4736 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
4737 after anything else but if statement. */
4738 FOR_EACH_EDGE (e, ei, bb->succs)
4739 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
4741 error ("true/false edge after a non-GIMPLE_COND in bb %d",
4742 bb->index);
4743 err = 1;
4747 switch (gimple_code (stmt))
4749 case GIMPLE_COND:
4751 edge true_edge;
4752 edge false_edge;
4754 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
4756 if (!true_edge
4757 || !false_edge
4758 || !(true_edge->flags & EDGE_TRUE_VALUE)
4759 || !(false_edge->flags & EDGE_FALSE_VALUE)
4760 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4761 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4762 || EDGE_COUNT (bb->succs) >= 3)
4764 error ("wrong outgoing edge flags at end of bb %d",
4765 bb->index);
4766 err = 1;
4769 break;
4771 case GIMPLE_GOTO:
4772 if (simple_goto_p (stmt))
4774 error ("explicit goto at end of bb %d", bb->index);
4775 err = 1;
4777 else
4779 /* FIXME. We should double check that the labels in the
4780 destination blocks have their address taken. */
4781 FOR_EACH_EDGE (e, ei, bb->succs)
4782 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
4783 | EDGE_FALSE_VALUE))
4784 || !(e->flags & EDGE_ABNORMAL))
4786 error ("wrong outgoing edge flags at end of bb %d",
4787 bb->index);
4788 err = 1;
4791 break;
4793 case GIMPLE_RETURN:
4794 if (!single_succ_p (bb)
4795 || (single_succ_edge (bb)->flags
4796 & (EDGE_FALLTHRU | EDGE_ABNORMAL
4797 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4799 error ("wrong outgoing edge flags at end of bb %d", bb->index);
4800 err = 1;
4802 if (single_succ (bb) != EXIT_BLOCK_PTR)
4804 error ("return edge does not point to exit in bb %d",
4805 bb->index);
4806 err = 1;
4808 break;
4810 case GIMPLE_SWITCH:
4812 tree prev;
4813 edge e;
4814 size_t i, n;
4816 n = gimple_switch_num_labels (stmt);
4818 /* Mark all the destination basic blocks. */
4819 for (i = 0; i < n; ++i)
4821 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4822 basic_block label_bb = label_to_block (lab);
4823 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
4824 label_bb->aux = (void *)1;
4827 /* Verify that the case labels are sorted. */
4828 prev = gimple_switch_label (stmt, 0);
4829 for (i = 1; i < n; ++i)
4831 tree c = gimple_switch_label (stmt, i);
4832 if (!CASE_LOW (c))
4834 error ("found default case not at the start of "
4835 "case vector");
4836 err = 1;
4837 continue;
4839 if (CASE_LOW (prev)
4840 && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
4842 error ("case labels not sorted: ");
4843 print_generic_expr (stderr, prev, 0);
4844 fprintf (stderr," is greater than ");
4845 print_generic_expr (stderr, c, 0);
4846 fprintf (stderr," but comes before it.\n");
4847 err = 1;
4849 prev = c;
4851 /* VRP will remove the default case if it can prove it will
4852 never be executed. So do not verify there always exists
4853 a default case here. */
4855 FOR_EACH_EDGE (e, ei, bb->succs)
4857 if (!e->dest->aux)
4859 error ("extra outgoing edge %d->%d",
4860 bb->index, e->dest->index);
4861 err = 1;
4864 e->dest->aux = (void *)2;
4865 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
4866 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4868 error ("wrong outgoing edge flags at end of bb %d",
4869 bb->index);
4870 err = 1;
4874 /* Check that we have all of them. */
4875 for (i = 0; i < n; ++i)
4877 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4878 basic_block label_bb = label_to_block (lab);
4880 if (label_bb->aux != (void *)2)
4882 error ("missing edge %i->%i", bb->index, label_bb->index);
4883 err = 1;
4887 FOR_EACH_EDGE (e, ei, bb->succs)
4888 e->dest->aux = (void *)0;
4891 default: ;
4895 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
4896 verify_dominators (CDI_DOMINATORS);
4898 return err;
4902 /* Updates phi nodes after creating a forwarder block joined
4903 by edge FALLTHRU. */
4905 static void
4906 gimple_make_forwarder_block (edge fallthru)
4908 edge e;
4909 edge_iterator ei;
4910 basic_block dummy, bb;
4911 tree var;
4912 gimple_stmt_iterator gsi;
4914 dummy = fallthru->src;
4915 bb = fallthru->dest;
4917 if (single_pred_p (bb))
4918 return;
4920 /* If we redirected a branch we must create new PHI nodes at the
4921 start of BB. */
4922 for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
4924 gimple phi, new_phi;
4926 phi = gsi_stmt (gsi);
4927 var = gimple_phi_result (phi);
4928 new_phi = create_phi_node (var, bb);
4929 SSA_NAME_DEF_STMT (var) = new_phi;
4930 gimple_phi_set_result (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
4931 add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
4932 UNKNOWN_LOCATION);
4935 /* Add the arguments we have stored on edges. */
4936 FOR_EACH_EDGE (e, ei, bb->preds)
4938 if (e == fallthru)
4939 continue;
4941 flush_pending_stmts (e);
4946 /* Return a non-special label in the head of basic block BLOCK.
4947 Create one if it doesn't exist. */
4949 tree
4950 gimple_block_label (basic_block bb)
4952 gimple_stmt_iterator i, s = gsi_start_bb (bb);
4953 bool first = true;
4954 tree label;
4955 gimple stmt;
4957 for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
4959 stmt = gsi_stmt (i);
4960 if (gimple_code (stmt) != GIMPLE_LABEL)
4961 break;
4962 label = gimple_label_label (stmt);
4963 if (!DECL_NONLOCAL (label))
4965 if (!first)
4966 gsi_move_before (&i, &s);
4967 return label;
4971 label = create_artificial_label (UNKNOWN_LOCATION);
4972 stmt = gimple_build_label (label);
4973 gsi_insert_before (&s, stmt, GSI_NEW_STMT);
4974 return label;
4978 /* Attempt to perform edge redirection by replacing a possibly complex
4979 jump instruction by a goto or by removing the jump completely.
4980 This can apply only if all edges now point to the same block. The
4981 parameters and return values are equivalent to
4982 redirect_edge_and_branch. */
4984 static edge
4985 gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
4987 basic_block src = e->src;
4988 gimple_stmt_iterator i;
4989 gimple stmt;
4991 /* We can replace or remove a complex jump only when we have exactly
4992 two edges. */
4993 if (EDGE_COUNT (src->succs) != 2
4994 /* Verify that all targets will be TARGET. Specifically, the
4995 edge that is not E must also go to TARGET. */
4996 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4997 return NULL;
4999 i = gsi_last_bb (src);
5000 if (gsi_end_p (i))
5001 return NULL;
5003 stmt = gsi_stmt (i);
5005 if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
5007 gsi_remove (&i, true);
5008 e = ssa_redirect_edge (e, target);
5009 e->flags = EDGE_FALLTHRU;
5010 return e;
5013 return NULL;
5017 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
5018 edge representing the redirected branch. */
5020 static edge
5021 gimple_redirect_edge_and_branch (edge e, basic_block dest)
5023 basic_block bb = e->src;
5024 gimple_stmt_iterator gsi;
5025 edge ret;
5026 gimple stmt;
5028 if (e->flags & EDGE_ABNORMAL)
5029 return NULL;
5031 if (e->src != ENTRY_BLOCK_PTR
5032 && (ret = gimple_try_redirect_by_replacing_jump (e, dest)))
5033 return ret;
5035 if (e->dest == dest)
5036 return NULL;
5038 if (e->flags & EDGE_EH)
5039 return redirect_eh_edge (e, dest);
5041 gsi = gsi_last_bb (bb);
5042 stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
5044 switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
5046 case GIMPLE_COND:
5047 /* For COND_EXPR, we only need to redirect the edge. */
5048 break;
5050 case GIMPLE_GOTO:
5051 /* No non-abnormal edges should lead from a non-simple goto, and
5052 simple ones should be represented implicitly. */
5053 gcc_unreachable ();
5055 case GIMPLE_SWITCH:
5057 tree label = gimple_block_label (dest);
5058 tree cases = get_cases_for_edge (e, stmt);
5060 /* If we have a list of cases associated with E, then use it
5061 as it's a lot faster than walking the entire case vector. */
5062 if (cases)
5064 edge e2 = find_edge (e->src, dest);
5065 tree last, first;
5067 first = cases;
5068 while (cases)
5070 last = cases;
5071 CASE_LABEL (cases) = label;
5072 cases = TREE_CHAIN (cases);
5075 /* If there was already an edge in the CFG, then we need
5076 to move all the cases associated with E to E2. */
5077 if (e2)
5079 tree cases2 = get_cases_for_edge (e2, stmt);
5081 TREE_CHAIN (last) = TREE_CHAIN (cases2);
5082 TREE_CHAIN (cases2) = first;
5085 else
5087 size_t i, n = gimple_switch_num_labels (stmt);
5089 for (i = 0; i < n; i++)
5091 tree elt = gimple_switch_label (stmt, i);
5092 if (label_to_block (CASE_LABEL (elt)) == e->dest)
5093 CASE_LABEL (elt) = label;
5097 break;
5100 case GIMPLE_RETURN:
5101 gsi_remove (&gsi, true);
5102 e->flags |= EDGE_FALLTHRU;
5103 break;
5105 case GIMPLE_OMP_RETURN:
5106 case GIMPLE_OMP_CONTINUE:
5107 case GIMPLE_OMP_SECTIONS_SWITCH:
5108 case GIMPLE_OMP_FOR:
5109 /* The edges from OMP constructs can be simply redirected. */
5110 break;
5112 default:
5113 /* Otherwise it must be a fallthru edge, and we don't need to
5114 do anything besides redirecting it. */
5115 gcc_assert (e->flags & EDGE_FALLTHRU);
5116 break;
5119 /* Update/insert PHI nodes as necessary. */
5121 /* Now update the edges in the CFG. */
5122 e = ssa_redirect_edge (e, dest);
5124 return e;
5127 /* Returns true if it is possible to remove edge E by redirecting
5128 it to the destination of the other edge from E->src. */
5130 static bool
5131 gimple_can_remove_branch_p (const_edge e)
5133 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
5134 return false;
5136 return true;
5139 /* Simple wrapper, as we can always redirect fallthru edges. */
5141 static basic_block
5142 gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
5144 e = gimple_redirect_edge_and_branch (e, dest);
5145 gcc_assert (e);
5147 return NULL;
5151 /* Splits basic block BB after statement STMT (but at least after the
5152 labels). If STMT is NULL, BB is split just after the labels. */
5154 static basic_block
5155 gimple_split_block (basic_block bb, void *stmt)
5157 gimple_stmt_iterator gsi;
5158 gimple_stmt_iterator gsi_tgt;
5159 gimple act;
5160 gimple_seq list;
5161 basic_block new_bb;
5162 edge e;
5163 edge_iterator ei;
5165 new_bb = create_empty_bb (bb);
5167 /* Redirect the outgoing edges. */
5168 new_bb->succs = bb->succs;
5169 bb->succs = NULL;
5170 FOR_EACH_EDGE (e, ei, new_bb->succs)
5171 e->src = new_bb;
5173 if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
5174 stmt = NULL;
5176 /* Move everything from GSI to the new basic block. */
5177 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5179 act = gsi_stmt (gsi);
5180 if (gimple_code (act) == GIMPLE_LABEL)
5181 continue;
5183 if (!stmt)
5184 break;
5186 if (stmt == act)
5188 gsi_next (&gsi);
5189 break;
5193 if (gsi_end_p (gsi))
5194 return new_bb;
5196 /* Split the statement list - avoid re-creating new containers as this
5197 brings ugly quadratic memory consumption in the inliner.
5198 (We are still quadratic since we need to update stmt BB pointers,
5199 sadly.) */
5200 list = gsi_split_seq_before (&gsi);
5201 set_bb_seq (new_bb, list);
5202 for (gsi_tgt = gsi_start (list);
5203 !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
5204 gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
5206 return new_bb;
5210 /* Moves basic block BB after block AFTER. */
5212 static bool
5213 gimple_move_block_after (basic_block bb, basic_block after)
5215 if (bb->prev_bb == after)
5216 return true;
5218 unlink_block (bb);
5219 link_block (bb, after);
5221 return true;
5225 /* Return true if basic_block can be duplicated. */
5227 static bool
5228 gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
5230 return true;
5233 /* Create a duplicate of the basic block BB. NOTE: This does not
5234 preserve SSA form. */
5236 static basic_block
5237 gimple_duplicate_bb (basic_block bb)
5239 basic_block new_bb;
5240 gimple_stmt_iterator gsi, gsi_tgt;
5241 gimple_seq phis = phi_nodes (bb);
5242 gimple phi, stmt, copy;
5244 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
5246 /* Copy the PHI nodes. We ignore PHI node arguments here because
5247 the incoming edges have not been setup yet. */
5248 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
5250 phi = gsi_stmt (gsi);
5251 copy = create_phi_node (gimple_phi_result (phi), new_bb);
5252 create_new_def_for (gimple_phi_result (copy), copy,
5253 gimple_phi_result_ptr (copy));
5256 gsi_tgt = gsi_start_bb (new_bb);
5257 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5259 def_operand_p def_p;
5260 ssa_op_iter op_iter;
5261 int region;
5263 stmt = gsi_stmt (gsi);
5264 if (gimple_code (stmt) == GIMPLE_LABEL)
5265 continue;
5267 /* Create a new copy of STMT and duplicate STMT's virtual
5268 operands. */
5269 copy = gimple_copy (stmt);
5270 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
5271 region = lookup_stmt_eh_region (stmt);
5272 if (region >= 0)
5273 add_stmt_to_eh_region (copy, region);
5274 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
5276 /* Create new names for all the definitions created by COPY and
5277 add replacement mappings for each new name. */
5278 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
5279 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
5282 return new_bb;
5285 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
5287 static void
5288 add_phi_args_after_copy_edge (edge e_copy)
5290 basic_block bb, bb_copy = e_copy->src, dest;
5291 edge e;
5292 edge_iterator ei;
5293 gimple phi, phi_copy;
5294 tree def;
5295 gimple_stmt_iterator psi, psi_copy;
5297 if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
5298 return;
5300 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
5302 if (e_copy->dest->flags & BB_DUPLICATED)
5303 dest = get_bb_original (e_copy->dest);
5304 else
5305 dest = e_copy->dest;
5307 e = find_edge (bb, dest);
5308 if (!e)
5310 /* During loop unrolling the target of the latch edge is copied.
5311 In this case we are not looking for edge to dest, but to
5312 duplicated block whose original was dest. */
5313 FOR_EACH_EDGE (e, ei, bb->succs)
5315 if ((e->dest->flags & BB_DUPLICATED)
5316 && get_bb_original (e->dest) == dest)
5317 break;
5320 gcc_assert (e != NULL);
5323 for (psi = gsi_start_phis (e->dest),
5324 psi_copy = gsi_start_phis (e_copy->dest);
5325 !gsi_end_p (psi);
5326 gsi_next (&psi), gsi_next (&psi_copy))
5328 phi = gsi_stmt (psi);
5329 phi_copy = gsi_stmt (psi_copy);
5330 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5331 add_phi_arg (phi_copy, def, e_copy,
5332 gimple_phi_arg_location_from_edge (phi, e));
5337 /* Basic block BB_COPY was created by code duplication. Add phi node
5338 arguments for edges going out of BB_COPY. The blocks that were
5339 duplicated have BB_DUPLICATED set. */
5341 void
5342 add_phi_args_after_copy_bb (basic_block bb_copy)
5344 edge e_copy;
5345 edge_iterator ei;
5347 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
5349 add_phi_args_after_copy_edge (e_copy);
5353 /* Blocks in REGION_COPY array of length N_REGION were created by
5354 duplication of basic blocks. Add phi node arguments for edges
5355 going from these blocks. If E_COPY is not NULL, also add
5356 phi node arguments for its destination.*/
5358 void
5359 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
5360 edge e_copy)
5362 unsigned i;
5364 for (i = 0; i < n_region; i++)
5365 region_copy[i]->flags |= BB_DUPLICATED;
5367 for (i = 0; i < n_region; i++)
5368 add_phi_args_after_copy_bb (region_copy[i]);
5369 if (e_copy)
5370 add_phi_args_after_copy_edge (e_copy);
5372 for (i = 0; i < n_region; i++)
5373 region_copy[i]->flags &= ~BB_DUPLICATED;
5376 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5377 important exit edge EXIT. By important we mean that no SSA name defined
5378 inside region is live over the other exit edges of the region. All entry
5379 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5380 to the duplicate of the region. SSA form, dominance and loop information
5381 is updated. The new basic blocks are stored to REGION_COPY in the same
5382 order as they had in REGION, provided that REGION_COPY is not NULL.
5383 The function returns false if it is unable to copy the region,
5384 true otherwise. */
5386 bool
5387 gimple_duplicate_sese_region (edge entry, edge exit,
5388 basic_block *region, unsigned n_region,
5389 basic_block *region_copy)
5391 unsigned i;
5392 bool free_region_copy = false, copying_header = false;
5393 struct loop *loop = entry->dest->loop_father;
5394 edge exit_copy;
5395 VEC (basic_block, heap) *doms;
5396 edge redirected;
5397 int total_freq = 0, entry_freq = 0;
5398 gcov_type total_count = 0, entry_count = 0;
5400 if (!can_copy_bbs_p (region, n_region))
5401 return false;
5403 /* Some sanity checking. Note that we do not check for all possible
5404 missuses of the functions. I.e. if you ask to copy something weird,
5405 it will work, but the state of structures probably will not be
5406 correct. */
5407 for (i = 0; i < n_region; i++)
5409 /* We do not handle subloops, i.e. all the blocks must belong to the
5410 same loop. */
5411 if (region[i]->loop_father != loop)
5412 return false;
5414 if (region[i] != entry->dest
5415 && region[i] == loop->header)
5416 return false;
5419 set_loop_copy (loop, loop);
5421 /* In case the function is used for loop header copying (which is the primary
5422 use), ensure that EXIT and its copy will be new latch and entry edges. */
5423 if (loop->header == entry->dest)
5425 copying_header = true;
5426 set_loop_copy (loop, loop_outer (loop));
5428 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5429 return false;
5431 for (i = 0; i < n_region; i++)
5432 if (region[i] != exit->src
5433 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5434 return false;
5437 if (!region_copy)
5439 region_copy = XNEWVEC (basic_block, n_region);
5440 free_region_copy = true;
5443 gcc_assert (!need_ssa_update_p (cfun));
5445 /* Record blocks outside the region that are dominated by something
5446 inside. */
5447 doms = NULL;
5448 initialize_original_copy_tables ();
5450 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5452 if (entry->dest->count)
5454 total_count = entry->dest->count;
5455 entry_count = entry->count;
5456 /* Fix up corner cases, to avoid division by zero or creation of negative
5457 frequencies. */
5458 if (entry_count > total_count)
5459 entry_count = total_count;
5461 else
5463 total_freq = entry->dest->frequency;
5464 entry_freq = EDGE_FREQUENCY (entry);
5465 /* Fix up corner cases, to avoid division by zero or creation of negative
5466 frequencies. */
5467 if (total_freq == 0)
5468 total_freq = 1;
5469 else if (entry_freq > total_freq)
5470 entry_freq = total_freq;
5473 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
5474 split_edge_bb_loc (entry));
5475 if (total_count)
5477 scale_bbs_frequencies_gcov_type (region, n_region,
5478 total_count - entry_count,
5479 total_count);
5480 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
5481 total_count);
5483 else
5485 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
5486 total_freq);
5487 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
5490 if (copying_header)
5492 loop->header = exit->dest;
5493 loop->latch = exit->src;
5496 /* Redirect the entry and add the phi node arguments. */
5497 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
5498 gcc_assert (redirected != NULL);
5499 flush_pending_stmts (entry);
5501 /* Concerning updating of dominators: We must recount dominators
5502 for entry block and its copy. Anything that is outside of the
5503 region, but was dominated by something inside needs recounting as
5504 well. */
5505 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5506 VEC_safe_push (basic_block, heap, doms, get_bb_original (entry->dest));
5507 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5508 VEC_free (basic_block, heap, doms);
5510 /* Add the other PHI node arguments. */
5511 add_phi_args_after_copy (region_copy, n_region, NULL);
5513 /* Update the SSA web. */
5514 update_ssa (TODO_update_ssa);
5516 if (free_region_copy)
5517 free (region_copy);
5519 free_original_copy_tables ();
5520 return true;
5523 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
5524 are stored to REGION_COPY in the same order in that they appear
5525 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
5526 the region, EXIT an exit from it. The condition guarding EXIT
5527 is moved to ENTRY. Returns true if duplication succeeds, false
5528 otherwise.
5530 For example,
5532 some_code;
5533 if (cond)
5535 else
5538 is transformed to
5540 if (cond)
5542 some_code;
5545 else
5547 some_code;
5552 bool
5553 gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
5554 basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
5555 basic_block *region_copy ATTRIBUTE_UNUSED)
5557 unsigned i;
5558 bool free_region_copy = false;
5559 struct loop *loop = exit->dest->loop_father;
5560 struct loop *orig_loop = entry->dest->loop_father;
5561 basic_block switch_bb, entry_bb, nentry_bb;
5562 VEC (basic_block, heap) *doms;
5563 int total_freq = 0, exit_freq = 0;
5564 gcov_type total_count = 0, exit_count = 0;
5565 edge exits[2], nexits[2], e;
5566 gimple_stmt_iterator gsi;
5567 gimple cond_stmt;
5568 edge sorig, snew;
5570 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
5571 exits[0] = exit;
5572 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
5574 if (!can_copy_bbs_p (region, n_region))
5575 return false;
5577 /* Some sanity checking. Note that we do not check for all possible
5578 missuses of the functions. I.e. if you ask to copy something weird
5579 (e.g., in the example, if there is a jump from inside to the middle
5580 of some_code, or come_code defines some of the values used in cond)
5581 it will work, but the resulting code will not be correct. */
5582 for (i = 0; i < n_region; i++)
5584 /* We do not handle subloops, i.e. all the blocks must belong to the
5585 same loop. */
5586 if (region[i]->loop_father != orig_loop)
5587 return false;
5589 if (region[i] == orig_loop->latch)
5590 return false;
5593 initialize_original_copy_tables ();
5594 set_loop_copy (orig_loop, loop);
5596 if (!region_copy)
5598 region_copy = XNEWVEC (basic_block, n_region);
5599 free_region_copy = true;
5602 gcc_assert (!need_ssa_update_p (cfun));
5604 /* Record blocks outside the region that are dominated by something
5605 inside. */
5606 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5608 if (exit->src->count)
5610 total_count = exit->src->count;
5611 exit_count = exit->count;
5612 /* Fix up corner cases, to avoid division by zero or creation of negative
5613 frequencies. */
5614 if (exit_count > total_count)
5615 exit_count = total_count;
5617 else
5619 total_freq = exit->src->frequency;
5620 exit_freq = EDGE_FREQUENCY (exit);
5621 /* Fix up corner cases, to avoid division by zero or creation of negative
5622 frequencies. */
5623 if (total_freq == 0)
5624 total_freq = 1;
5625 if (exit_freq > total_freq)
5626 exit_freq = total_freq;
5629 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
5630 split_edge_bb_loc (exit));
5631 if (total_count)
5633 scale_bbs_frequencies_gcov_type (region, n_region,
5634 total_count - exit_count,
5635 total_count);
5636 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
5637 total_count);
5639 else
5641 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
5642 total_freq);
5643 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
5646 /* Create the switch block, and put the exit condition to it. */
5647 entry_bb = entry->dest;
5648 nentry_bb = get_bb_copy (entry_bb);
5649 if (!last_stmt (entry->src)
5650 || !stmt_ends_bb_p (last_stmt (entry->src)))
5651 switch_bb = entry->src;
5652 else
5653 switch_bb = split_edge (entry);
5654 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
5656 gsi = gsi_last_bb (switch_bb);
5657 cond_stmt = last_stmt (exit->src);
5658 gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
5659 cond_stmt = gimple_copy (cond_stmt);
5660 gimple_cond_set_lhs (cond_stmt, unshare_expr (gimple_cond_lhs (cond_stmt)));
5661 gimple_cond_set_rhs (cond_stmt, unshare_expr (gimple_cond_rhs (cond_stmt)));
5662 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
5664 sorig = single_succ_edge (switch_bb);
5665 sorig->flags = exits[1]->flags;
5666 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
5668 /* Register the new edge from SWITCH_BB in loop exit lists. */
5669 rescan_loop_exit (snew, true, false);
5671 /* Add the PHI node arguments. */
5672 add_phi_args_after_copy (region_copy, n_region, snew);
5674 /* Get rid of now superfluous conditions and associated edges (and phi node
5675 arguments). */
5676 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
5677 PENDING_STMT (e) = NULL;
5678 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
5679 PENDING_STMT (e) = NULL;
5681 /* Anything that is outside of the region, but was dominated by something
5682 inside needs to update dominance info. */
5683 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5684 VEC_free (basic_block, heap, doms);
5686 /* Update the SSA web. */
5687 update_ssa (TODO_update_ssa);
5689 if (free_region_copy)
5690 free (region_copy);
5692 free_original_copy_tables ();
5693 return true;
5696 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
5697 adding blocks when the dominator traversal reaches EXIT. This
5698 function silently assumes that ENTRY strictly dominates EXIT. */
5700 void
5701 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
5702 VEC(basic_block,heap) **bbs_p)
5704 basic_block son;
5706 for (son = first_dom_son (CDI_DOMINATORS, entry);
5707 son;
5708 son = next_dom_son (CDI_DOMINATORS, son))
5710 VEC_safe_push (basic_block, heap, *bbs_p, son);
5711 if (son != exit)
5712 gather_blocks_in_sese_region (son, exit, bbs_p);
5716 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
5717 The duplicates are recorded in VARS_MAP. */
5719 static void
5720 replace_by_duplicate_decl (tree *tp, struct pointer_map_t *vars_map,
5721 tree to_context)
5723 tree t = *tp, new_t;
5724 struct function *f = DECL_STRUCT_FUNCTION (to_context);
5725 void **loc;
5727 if (DECL_CONTEXT (t) == to_context)
5728 return;
5730 loc = pointer_map_contains (vars_map, t);
5732 if (!loc)
5734 loc = pointer_map_insert (vars_map, t);
5736 if (SSA_VAR_P (t))
5738 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
5739 f->local_decls = tree_cons (NULL_TREE, new_t, f->local_decls);
5741 else
5743 gcc_assert (TREE_CODE (t) == CONST_DECL);
5744 new_t = copy_node (t);
5746 DECL_CONTEXT (new_t) = to_context;
5748 *loc = new_t;
5750 else
5751 new_t = (tree) *loc;
5753 *tp = new_t;
5757 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
5758 VARS_MAP maps old ssa names and var_decls to the new ones. */
5760 static tree
5761 replace_ssa_name (tree name, struct pointer_map_t *vars_map,
5762 tree to_context)
5764 void **loc;
5765 tree new_name, decl = SSA_NAME_VAR (name);
5767 gcc_assert (is_gimple_reg (name));
5769 loc = pointer_map_contains (vars_map, name);
5771 if (!loc)
5773 replace_by_duplicate_decl (&decl, vars_map, to_context);
5775 push_cfun (DECL_STRUCT_FUNCTION (to_context));
5776 if (gimple_in_ssa_p (cfun))
5777 add_referenced_var (decl);
5779 new_name = make_ssa_name (decl, SSA_NAME_DEF_STMT (name));
5780 if (SSA_NAME_IS_DEFAULT_DEF (name))
5781 set_default_def (decl, new_name);
5782 pop_cfun ();
5784 loc = pointer_map_insert (vars_map, name);
5785 *loc = new_name;
5787 else
5788 new_name = (tree) *loc;
5790 return new_name;
5793 struct move_stmt_d
5795 tree orig_block;
5796 tree new_block;
5797 tree from_context;
5798 tree to_context;
5799 struct pointer_map_t *vars_map;
5800 htab_t new_label_map;
5801 bool remap_decls_p;
5804 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
5805 contained in *TP if it has been ORIG_BLOCK previously and change the
5806 DECL_CONTEXT of every local variable referenced in *TP. */
5808 static tree
5809 move_stmt_op (tree *tp, int *walk_subtrees, void *data)
5811 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5812 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
5813 tree t = *tp;
5815 if (EXPR_P (t))
5816 /* We should never have TREE_BLOCK set on non-statements. */
5817 gcc_assert (!TREE_BLOCK (t));
5819 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
5821 if (TREE_CODE (t) == SSA_NAME)
5822 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
5823 else if (TREE_CODE (t) == LABEL_DECL)
5825 if (p->new_label_map)
5827 struct tree_map in, *out;
5828 in.base.from = t;
5829 out = (struct tree_map *)
5830 htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
5831 if (out)
5832 *tp = t = out->to;
5835 DECL_CONTEXT (t) = p->to_context;
5837 else if (p->remap_decls_p)
5839 /* Replace T with its duplicate. T should no longer appear in the
5840 parent function, so this looks wasteful; however, it may appear
5841 in referenced_vars, and more importantly, as virtual operands of
5842 statements, and in alias lists of other variables. It would be
5843 quite difficult to expunge it from all those places. ??? It might
5844 suffice to do this for addressable variables. */
5845 if ((TREE_CODE (t) == VAR_DECL
5846 && !is_global_var (t))
5847 || TREE_CODE (t) == CONST_DECL)
5848 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
5850 if (SSA_VAR_P (t)
5851 && gimple_in_ssa_p (cfun))
5853 push_cfun (DECL_STRUCT_FUNCTION (p->to_context));
5854 add_referenced_var (*tp);
5855 pop_cfun ();
5858 *walk_subtrees = 0;
5860 else if (TYPE_P (t))
5861 *walk_subtrees = 0;
5863 return NULL_TREE;
5866 /* Like move_stmt_op, but for gimple statements.
5868 Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
5869 contained in the current statement in *GSI_P and change the
5870 DECL_CONTEXT of every local variable referenced in the current
5871 statement. */
5873 static tree
5874 move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
5875 struct walk_stmt_info *wi)
5877 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
5878 gimple stmt = gsi_stmt (*gsi_p);
5879 tree block = gimple_block (stmt);
5881 if (p->orig_block == NULL_TREE
5882 || block == p->orig_block
5883 || block == NULL_TREE)
5884 gimple_set_block (stmt, p->new_block);
5885 #ifdef ENABLE_CHECKING
5886 else if (block != p->new_block)
5888 while (block && block != p->orig_block)
5889 block = BLOCK_SUPERCONTEXT (block);
5890 gcc_assert (block);
5892 #endif
5894 if (is_gimple_omp (stmt)
5895 && gimple_code (stmt) != GIMPLE_OMP_RETURN
5896 && gimple_code (stmt) != GIMPLE_OMP_CONTINUE)
5898 /* Do not remap variables inside OMP directives. Variables
5899 referenced in clauses and directive header belong to the
5900 parent function and should not be moved into the child
5901 function. */
5902 bool save_remap_decls_p = p->remap_decls_p;
5903 p->remap_decls_p = false;
5904 *handled_ops_p = true;
5906 walk_gimple_seq (gimple_omp_body (stmt), move_stmt_r, move_stmt_op, wi);
5908 p->remap_decls_p = save_remap_decls_p;
5911 return NULL_TREE;
5914 /* Marks virtual operands of all statements in basic blocks BBS for
5915 renaming. */
5917 void
5918 mark_virtual_ops_in_bb (basic_block bb)
5920 gimple_stmt_iterator gsi;
5922 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5923 mark_virtual_ops_for_renaming (gsi_stmt (gsi));
5925 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5926 mark_virtual_ops_for_renaming (gsi_stmt (gsi));
5929 /* Move basic block BB from function CFUN to function DEST_FN. The
5930 block is moved out of the original linked list and placed after
5931 block AFTER in the new list. Also, the block is removed from the
5932 original array of blocks and placed in DEST_FN's array of blocks.
5933 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
5934 updated to reflect the moved edges.
5936 The local variables are remapped to new instances, VARS_MAP is used
5937 to record the mapping. */
5939 static void
5940 move_block_to_fn (struct function *dest_cfun, basic_block bb,
5941 basic_block after, bool update_edge_count_p,
5942 struct move_stmt_d *d, int eh_offset)
5944 struct control_flow_graph *cfg;
5945 edge_iterator ei;
5946 edge e;
5947 gimple_stmt_iterator si;
5948 unsigned old_len, new_len;
5950 /* Remove BB from dominance structures. */
5951 delete_from_dominance_info (CDI_DOMINATORS, bb);
5952 if (current_loops)
5953 remove_bb_from_loops (bb);
5955 /* Link BB to the new linked list. */
5956 move_block_after (bb, after);
5958 /* Update the edge count in the corresponding flowgraphs. */
5959 if (update_edge_count_p)
5960 FOR_EACH_EDGE (e, ei, bb->succs)
5962 cfun->cfg->x_n_edges--;
5963 dest_cfun->cfg->x_n_edges++;
5966 /* Remove BB from the original basic block array. */
5967 VEC_replace (basic_block, cfun->cfg->x_basic_block_info, bb->index, NULL);
5968 cfun->cfg->x_n_basic_blocks--;
5970 /* Grow DEST_CFUN's basic block array if needed. */
5971 cfg = dest_cfun->cfg;
5972 cfg->x_n_basic_blocks++;
5973 if (bb->index >= cfg->x_last_basic_block)
5974 cfg->x_last_basic_block = bb->index + 1;
5976 old_len = VEC_length (basic_block, cfg->x_basic_block_info);
5977 if ((unsigned) cfg->x_last_basic_block >= old_len)
5979 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
5980 VEC_safe_grow_cleared (basic_block, gc, cfg->x_basic_block_info,
5981 new_len);
5984 VEC_replace (basic_block, cfg->x_basic_block_info,
5985 bb->index, bb);
5987 /* Remap the variables in phi nodes. */
5988 for (si = gsi_start_phis (bb); !gsi_end_p (si); )
5990 gimple phi = gsi_stmt (si);
5991 use_operand_p use;
5992 tree op = PHI_RESULT (phi);
5993 ssa_op_iter oi;
5995 if (!is_gimple_reg (op))
5997 /* Remove the phi nodes for virtual operands (alias analysis will be
5998 run for the new function, anyway). */
5999 remove_phi_node (&si, true);
6000 continue;
6003 SET_PHI_RESULT (phi,
6004 replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6005 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
6007 op = USE_FROM_PTR (use);
6008 if (TREE_CODE (op) == SSA_NAME)
6009 SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6012 gsi_next (&si);
6015 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6017 gimple stmt = gsi_stmt (si);
6018 int region;
6019 struct walk_stmt_info wi;
6021 memset (&wi, 0, sizeof (wi));
6022 wi.info = d;
6023 walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
6025 if (gimple_code (stmt) == GIMPLE_LABEL)
6027 tree label = gimple_label_label (stmt);
6028 int uid = LABEL_DECL_UID (label);
6030 gcc_assert (uid > -1);
6032 old_len = VEC_length (basic_block, cfg->x_label_to_block_map);
6033 if (old_len <= (unsigned) uid)
6035 new_len = 3 * uid / 2 + 1;
6036 VEC_safe_grow_cleared (basic_block, gc,
6037 cfg->x_label_to_block_map, new_len);
6040 VEC_replace (basic_block, cfg->x_label_to_block_map, uid, bb);
6041 VEC_replace (basic_block, cfun->cfg->x_label_to_block_map, uid, NULL);
6043 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
6045 if (uid >= dest_cfun->cfg->last_label_uid)
6046 dest_cfun->cfg->last_label_uid = uid + 1;
6048 else if (gimple_code (stmt) == GIMPLE_RESX && eh_offset != 0)
6049 gimple_resx_set_region (stmt, gimple_resx_region (stmt) + eh_offset);
6051 region = lookup_stmt_eh_region (stmt);
6052 if (region >= 0)
6054 add_stmt_to_eh_region_fn (dest_cfun, stmt, region + eh_offset);
6055 remove_stmt_from_eh_region (stmt);
6056 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
6057 gimple_remove_stmt_histograms (cfun, stmt);
6060 /* We cannot leave any operands allocated from the operand caches of
6061 the current function. */
6062 free_stmt_operands (stmt);
6063 push_cfun (dest_cfun);
6064 update_stmt (stmt);
6065 pop_cfun ();
6068 FOR_EACH_EDGE (e, ei, bb->succs)
6069 if (e->goto_locus)
6071 tree block = e->goto_block;
6072 if (d->orig_block == NULL_TREE
6073 || block == d->orig_block)
6074 e->goto_block = d->new_block;
6075 #ifdef ENABLE_CHECKING
6076 else if (block != d->new_block)
6078 while (block && block != d->orig_block)
6079 block = BLOCK_SUPERCONTEXT (block);
6080 gcc_assert (block);
6082 #endif
6086 /* Examine the statements in BB (which is in SRC_CFUN); find and return
6087 the outermost EH region. Use REGION as the incoming base EH region. */
6089 static int
6090 find_outermost_region_in_block (struct function *src_cfun,
6091 basic_block bb, int region)
6093 gimple_stmt_iterator si;
6095 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6097 gimple stmt = gsi_stmt (si);
6098 int stmt_region;
6100 if (gimple_code (stmt) == GIMPLE_RESX)
6101 stmt_region = gimple_resx_region (stmt);
6102 else
6103 stmt_region = lookup_stmt_eh_region_fn (src_cfun, stmt);
6104 if (stmt_region > 0)
6106 if (region < 0)
6107 region = stmt_region;
6108 else if (stmt_region != region)
6110 region = eh_region_outermost (src_cfun, stmt_region, region);
6111 gcc_assert (region != -1);
6116 return region;
6119 static tree
6120 new_label_mapper (tree decl, void *data)
6122 htab_t hash = (htab_t) data;
6123 struct tree_map *m;
6124 void **slot;
6126 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
6128 m = XNEW (struct tree_map);
6129 m->hash = DECL_UID (decl);
6130 m->base.from = decl;
6131 m->to = create_artificial_label (UNKNOWN_LOCATION);
6132 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
6133 if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
6134 cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
6136 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
6137 gcc_assert (*slot == NULL);
6139 *slot = m;
6141 return m->to;
6144 /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
6145 subblocks. */
6147 static void
6148 replace_block_vars_by_duplicates (tree block, struct pointer_map_t *vars_map,
6149 tree to_context)
6151 tree *tp, t;
6153 for (tp = &BLOCK_VARS (block); *tp; tp = &TREE_CHAIN (*tp))
6155 t = *tp;
6156 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
6157 continue;
6158 replace_by_duplicate_decl (&t, vars_map, to_context);
6159 if (t != *tp)
6161 if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
6163 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
6164 DECL_HAS_VALUE_EXPR_P (t) = 1;
6166 TREE_CHAIN (t) = TREE_CHAIN (*tp);
6167 *tp = t;
6171 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
6172 replace_block_vars_by_duplicates (block, vars_map, to_context);
6175 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
6176 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
6177 single basic block in the original CFG and the new basic block is
6178 returned. DEST_CFUN must not have a CFG yet.
6180 Note that the region need not be a pure SESE region. Blocks inside
6181 the region may contain calls to abort/exit. The only restriction
6182 is that ENTRY_BB should be the only entry point and it must
6183 dominate EXIT_BB.
6185 Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
6186 functions outermost BLOCK, move all subblocks of ORIG_BLOCK
6187 to the new function.
6189 All local variables referenced in the region are assumed to be in
6190 the corresponding BLOCK_VARS and unexpanded variable lists
6191 associated with DEST_CFUN. */
6193 basic_block
6194 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
6195 basic_block exit_bb, tree orig_block)
6197 VEC(basic_block,heap) *bbs, *dom_bbs;
6198 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
6199 basic_block after, bb, *entry_pred, *exit_succ, abb;
6200 struct function *saved_cfun = cfun;
6201 int *entry_flag, *exit_flag, eh_offset;
6202 unsigned *entry_prob, *exit_prob;
6203 unsigned i, num_entry_edges, num_exit_edges;
6204 edge e;
6205 edge_iterator ei;
6206 htab_t new_label_map;
6207 struct pointer_map_t *vars_map;
6208 struct loop *loop = entry_bb->loop_father;
6209 struct move_stmt_d d;
6211 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
6212 region. */
6213 gcc_assert (entry_bb != exit_bb
6214 && (!exit_bb
6215 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
6217 /* Collect all the blocks in the region. Manually add ENTRY_BB
6218 because it won't be added by dfs_enumerate_from. */
6219 bbs = NULL;
6220 VEC_safe_push (basic_block, heap, bbs, entry_bb);
6221 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
6223 /* The blocks that used to be dominated by something in BBS will now be
6224 dominated by the new block. */
6225 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
6226 VEC_address (basic_block, bbs),
6227 VEC_length (basic_block, bbs));
6229 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
6230 the predecessor edges to ENTRY_BB and the successor edges to
6231 EXIT_BB so that we can re-attach them to the new basic block that
6232 will replace the region. */
6233 num_entry_edges = EDGE_COUNT (entry_bb->preds);
6234 entry_pred = (basic_block *) xcalloc (num_entry_edges, sizeof (basic_block));
6235 entry_flag = (int *) xcalloc (num_entry_edges, sizeof (int));
6236 entry_prob = XNEWVEC (unsigned, num_entry_edges);
6237 i = 0;
6238 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
6240 entry_prob[i] = e->probability;
6241 entry_flag[i] = e->flags;
6242 entry_pred[i++] = e->src;
6243 remove_edge (e);
6246 if (exit_bb)
6248 num_exit_edges = EDGE_COUNT (exit_bb->succs);
6249 exit_succ = (basic_block *) xcalloc (num_exit_edges,
6250 sizeof (basic_block));
6251 exit_flag = (int *) xcalloc (num_exit_edges, sizeof (int));
6252 exit_prob = XNEWVEC (unsigned, num_exit_edges);
6253 i = 0;
6254 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
6256 exit_prob[i] = e->probability;
6257 exit_flag[i] = e->flags;
6258 exit_succ[i++] = e->dest;
6259 remove_edge (e);
6262 else
6264 num_exit_edges = 0;
6265 exit_succ = NULL;
6266 exit_flag = NULL;
6267 exit_prob = NULL;
6270 /* Switch context to the child function to initialize DEST_FN's CFG. */
6271 gcc_assert (dest_cfun->cfg == NULL);
6272 push_cfun (dest_cfun);
6274 init_empty_tree_cfg ();
6276 /* Initialize EH information for the new function. */
6277 eh_offset = 0;
6278 new_label_map = NULL;
6279 if (saved_cfun->eh)
6281 int region = -1;
6283 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
6284 region = find_outermost_region_in_block (saved_cfun, bb, region);
6286 init_eh_for_function ();
6287 if (region != -1)
6289 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
6290 eh_offset = duplicate_eh_regions (saved_cfun, new_label_mapper,
6291 new_label_map, region, 0);
6295 pop_cfun ();
6297 /* Move blocks from BBS into DEST_CFUN. */
6298 gcc_assert (VEC_length (basic_block, bbs) >= 2);
6299 after = dest_cfun->cfg->x_entry_block_ptr;
6300 vars_map = pointer_map_create ();
6302 memset (&d, 0, sizeof (d));
6303 d.vars_map = vars_map;
6304 d.from_context = cfun->decl;
6305 d.to_context = dest_cfun->decl;
6306 d.new_label_map = new_label_map;
6307 d.remap_decls_p = true;
6308 d.orig_block = orig_block;
6309 d.new_block = DECL_INITIAL (dest_cfun->decl);
6311 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
6313 /* No need to update edge counts on the last block. It has
6314 already been updated earlier when we detached the region from
6315 the original CFG. */
6316 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d, eh_offset);
6317 after = bb;
6320 /* Rewire BLOCK_SUBBLOCKS of orig_block. */
6321 if (orig_block)
6323 tree block;
6324 gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
6325 == NULL_TREE);
6326 BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
6327 = BLOCK_SUBBLOCKS (orig_block);
6328 for (block = BLOCK_SUBBLOCKS (orig_block);
6329 block; block = BLOCK_CHAIN (block))
6330 BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
6331 BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
6334 replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
6335 vars_map, dest_cfun->decl);
6337 if (new_label_map)
6338 htab_delete (new_label_map);
6339 pointer_map_destroy (vars_map);
6341 /* Rewire the entry and exit blocks. The successor to the entry
6342 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
6343 the child function. Similarly, the predecessor of DEST_FN's
6344 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
6345 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
6346 various CFG manipulation function get to the right CFG.
6348 FIXME, this is silly. The CFG ought to become a parameter to
6349 these helpers. */
6350 push_cfun (dest_cfun);
6351 make_edge (ENTRY_BLOCK_PTR, entry_bb, EDGE_FALLTHRU);
6352 if (exit_bb)
6353 make_edge (exit_bb, EXIT_BLOCK_PTR, 0);
6354 pop_cfun ();
6356 /* Back in the original function, the SESE region has disappeared,
6357 create a new basic block in its place. */
6358 bb = create_empty_bb (entry_pred[0]);
6359 if (current_loops)
6360 add_bb_to_loop (bb, loop);
6361 for (i = 0; i < num_entry_edges; i++)
6363 e = make_edge (entry_pred[i], bb, entry_flag[i]);
6364 e->probability = entry_prob[i];
6367 for (i = 0; i < num_exit_edges; i++)
6369 e = make_edge (bb, exit_succ[i], exit_flag[i]);
6370 e->probability = exit_prob[i];
6373 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
6374 for (i = 0; VEC_iterate (basic_block, dom_bbs, i, abb); i++)
6375 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
6376 VEC_free (basic_block, heap, dom_bbs);
6378 if (exit_bb)
6380 free (exit_prob);
6381 free (exit_flag);
6382 free (exit_succ);
6384 free (entry_prob);
6385 free (entry_flag);
6386 free (entry_pred);
6387 VEC_free (basic_block, heap, bbs);
6389 return bb;
6393 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree-pass.h)
6396 void
6397 dump_function_to_file (tree fn, FILE *file, int flags)
6399 tree arg, vars, var;
6400 struct function *dsf;
6401 bool ignore_topmost_bind = false, any_var = false;
6402 basic_block bb;
6403 tree chain;
6405 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
6407 arg = DECL_ARGUMENTS (fn);
6408 while (arg)
6410 print_generic_expr (file, TREE_TYPE (arg), dump_flags);
6411 fprintf (file, " ");
6412 print_generic_expr (file, arg, dump_flags);
6413 if (flags & TDF_VERBOSE)
6414 print_node (file, "", arg, 4);
6415 if (TREE_CHAIN (arg))
6416 fprintf (file, ", ");
6417 arg = TREE_CHAIN (arg);
6419 fprintf (file, ")\n");
6421 if (flags & TDF_VERBOSE)
6422 print_node (file, "", fn, 2);
6424 dsf = DECL_STRUCT_FUNCTION (fn);
6425 if (dsf && (flags & TDF_DETAILS))
6426 dump_eh_tree (file, dsf);
6428 if (flags & TDF_RAW && !gimple_has_body_p (fn))
6430 dump_node (fn, TDF_SLIM | flags, file);
6431 return;
6434 /* Switch CFUN to point to FN. */
6435 push_cfun (DECL_STRUCT_FUNCTION (fn));
6437 /* When GIMPLE is lowered, the variables are no longer available in
6438 BIND_EXPRs, so display them separately. */
6439 if (cfun && cfun->decl == fn && cfun->local_decls)
6441 ignore_topmost_bind = true;
6443 fprintf (file, "{\n");
6444 for (vars = cfun->local_decls; vars; vars = TREE_CHAIN (vars))
6446 var = TREE_VALUE (vars);
6448 print_generic_decl (file, var, flags);
6449 if (flags & TDF_VERBOSE)
6450 print_node (file, "", var, 4);
6451 fprintf (file, "\n");
6453 any_var = true;
6457 if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
6459 /* If the CFG has been built, emit a CFG-based dump. */
6460 check_bb_profile (ENTRY_BLOCK_PTR, file);
6461 if (!ignore_topmost_bind)
6462 fprintf (file, "{\n");
6464 if (any_var && n_basic_blocks)
6465 fprintf (file, "\n");
6467 FOR_EACH_BB (bb)
6468 gimple_dump_bb (bb, file, 2, flags);
6470 fprintf (file, "}\n");
6471 check_bb_profile (EXIT_BLOCK_PTR, file);
6473 else if (DECL_SAVED_TREE (fn) == NULL)
6475 /* The function is now in GIMPLE form but the CFG has not been
6476 built yet. Emit the single sequence of GIMPLE statements
6477 that make up its body. */
6478 gimple_seq body = gimple_body (fn);
6480 if (gimple_seq_first_stmt (body)
6481 && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
6482 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
6483 print_gimple_seq (file, body, 0, flags);
6484 else
6486 if (!ignore_topmost_bind)
6487 fprintf (file, "{\n");
6489 if (any_var)
6490 fprintf (file, "\n");
6492 print_gimple_seq (file, body, 2, flags);
6493 fprintf (file, "}\n");
6496 else
6498 int indent;
6500 /* Make a tree based dump. */
6501 chain = DECL_SAVED_TREE (fn);
6503 if (chain && TREE_CODE (chain) == BIND_EXPR)
6505 if (ignore_topmost_bind)
6507 chain = BIND_EXPR_BODY (chain);
6508 indent = 2;
6510 else
6511 indent = 0;
6513 else
6515 if (!ignore_topmost_bind)
6516 fprintf (file, "{\n");
6517 indent = 2;
6520 if (any_var)
6521 fprintf (file, "\n");
6523 print_generic_stmt_indented (file, chain, flags, indent);
6524 if (ignore_topmost_bind)
6525 fprintf (file, "}\n");
6528 fprintf (file, "\n\n");
6530 /* Restore CFUN. */
6531 pop_cfun ();
6535 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
6537 void
6538 debug_function (tree fn, int flags)
6540 dump_function_to_file (fn, stderr, flags);
6544 /* Print on FILE the indexes for the predecessors of basic_block BB. */
6546 static void
6547 print_pred_bbs (FILE *file, basic_block bb)
6549 edge e;
6550 edge_iterator ei;
6552 FOR_EACH_EDGE (e, ei, bb->preds)
6553 fprintf (file, "bb_%d ", e->src->index);
6557 /* Print on FILE the indexes for the successors of basic_block BB. */
6559 static void
6560 print_succ_bbs (FILE *file, basic_block bb)
6562 edge e;
6563 edge_iterator ei;
6565 FOR_EACH_EDGE (e, ei, bb->succs)
6566 fprintf (file, "bb_%d ", e->dest->index);
6569 /* Print to FILE the basic block BB following the VERBOSITY level. */
6571 void
6572 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
6574 char *s_indent = (char *) alloca ((size_t) indent + 1);
6575 memset ((void *) s_indent, ' ', (size_t) indent);
6576 s_indent[indent] = '\0';
6578 /* Print basic_block's header. */
6579 if (verbosity >= 2)
6581 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
6582 print_pred_bbs (file, bb);
6583 fprintf (file, "}, succs = {");
6584 print_succ_bbs (file, bb);
6585 fprintf (file, "})\n");
6588 /* Print basic_block's body. */
6589 if (verbosity >= 3)
6591 fprintf (file, "%s {\n", s_indent);
6592 gimple_dump_bb (bb, file, indent + 4, TDF_VOPS|TDF_MEMSYMS);
6593 fprintf (file, "%s }\n", s_indent);
6597 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
6599 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
6600 VERBOSITY level this outputs the contents of the loop, or just its
6601 structure. */
6603 static void
6604 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
6606 char *s_indent;
6607 basic_block bb;
6609 if (loop == NULL)
6610 return;
6612 s_indent = (char *) alloca ((size_t) indent + 1);
6613 memset ((void *) s_indent, ' ', (size_t) indent);
6614 s_indent[indent] = '\0';
6616 /* Print loop's header. */
6617 fprintf (file, "%sloop_%d (header = %d, latch = %d", s_indent,
6618 loop->num, loop->header->index, loop->latch->index);
6619 fprintf (file, ", niter = ");
6620 print_generic_expr (file, loop->nb_iterations, 0);
6622 if (loop->any_upper_bound)
6624 fprintf (file, ", upper_bound = ");
6625 dump_double_int (file, loop->nb_iterations_upper_bound, true);
6628 if (loop->any_estimate)
6630 fprintf (file, ", estimate = ");
6631 dump_double_int (file, loop->nb_iterations_estimate, true);
6633 fprintf (file, ")\n");
6635 /* Print loop's body. */
6636 if (verbosity >= 1)
6638 fprintf (file, "%s{\n", s_indent);
6639 FOR_EACH_BB (bb)
6640 if (bb->loop_father == loop)
6641 print_loops_bb (file, bb, indent, verbosity);
6643 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
6644 fprintf (file, "%s}\n", s_indent);
6648 /* Print the LOOP and its sibling loops on FILE, indented INDENT
6649 spaces. Following VERBOSITY level this outputs the contents of the
6650 loop, or just its structure. */
6652 static void
6653 print_loop_and_siblings (FILE *file, struct loop *loop, int indent, int verbosity)
6655 if (loop == NULL)
6656 return;
6658 print_loop (file, loop, indent, verbosity);
6659 print_loop_and_siblings (file, loop->next, indent, verbosity);
6662 /* Follow a CFG edge from the entry point of the program, and on entry
6663 of a loop, pretty print the loop structure on FILE. */
6665 void
6666 print_loops (FILE *file, int verbosity)
6668 basic_block bb;
6670 bb = ENTRY_BLOCK_PTR;
6671 if (bb && bb->loop_father)
6672 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
6676 /* Debugging loops structure at tree level, at some VERBOSITY level. */
6678 void
6679 debug_loops (int verbosity)
6681 print_loops (stderr, verbosity);
6684 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
6686 void
6687 debug_loop (struct loop *loop, int verbosity)
6689 print_loop (stderr, loop, 0, verbosity);
6692 /* Print on stderr the code of loop number NUM, at some VERBOSITY
6693 level. */
6695 void
6696 debug_loop_num (unsigned num, int verbosity)
6698 debug_loop (get_loop (num), verbosity);
6701 /* Return true if BB ends with a call, possibly followed by some
6702 instructions that must stay with the call. Return false,
6703 otherwise. */
6705 static bool
6706 gimple_block_ends_with_call_p (basic_block bb)
6708 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
6709 return is_gimple_call (gsi_stmt (gsi));
6713 /* Return true if BB ends with a conditional branch. Return false,
6714 otherwise. */
6716 static bool
6717 gimple_block_ends_with_condjump_p (const_basic_block bb)
6719 gimple stmt = last_stmt (CONST_CAST_BB (bb));
6720 return (stmt && gimple_code (stmt) == GIMPLE_COND);
6724 /* Return true if we need to add fake edge to exit at statement T.
6725 Helper function for gimple_flow_call_edges_add. */
6727 static bool
6728 need_fake_edge_p (gimple t)
6730 tree fndecl = NULL_TREE;
6731 int call_flags = 0;
6733 /* NORETURN and LONGJMP calls already have an edge to exit.
6734 CONST and PURE calls do not need one.
6735 We don't currently check for CONST and PURE here, although
6736 it would be a good idea, because those attributes are
6737 figured out from the RTL in mark_constant_function, and
6738 the counter incrementation code from -fprofile-arcs
6739 leads to different results from -fbranch-probabilities. */
6740 if (is_gimple_call (t))
6742 fndecl = gimple_call_fndecl (t);
6743 call_flags = gimple_call_flags (t);
6746 if (is_gimple_call (t)
6747 && fndecl
6748 && DECL_BUILT_IN (fndecl)
6749 && (call_flags & ECF_NOTHROW)
6750 && !(call_flags & ECF_RETURNS_TWICE)
6751 /* fork() doesn't really return twice, but the effect of
6752 wrapping it in __gcov_fork() which calls __gcov_flush()
6753 and clears the counters before forking has the same
6754 effect as returning twice. Force a fake edge. */
6755 && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6756 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
6757 return false;
6759 if (is_gimple_call (t)
6760 && !(call_flags & ECF_NORETURN))
6761 return true;
6763 if (gimple_code (t) == GIMPLE_ASM
6764 && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
6765 return true;
6767 return false;
6771 /* Add fake edges to the function exit for any non constant and non
6772 noreturn calls, volatile inline assembly in the bitmap of blocks
6773 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
6774 the number of blocks that were split.
6776 The goal is to expose cases in which entering a basic block does
6777 not imply that all subsequent instructions must be executed. */
6779 static int
6780 gimple_flow_call_edges_add (sbitmap blocks)
6782 int i;
6783 int blocks_split = 0;
6784 int last_bb = last_basic_block;
6785 bool check_last_block = false;
6787 if (n_basic_blocks == NUM_FIXED_BLOCKS)
6788 return 0;
6790 if (! blocks)
6791 check_last_block = true;
6792 else
6793 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
6795 /* In the last basic block, before epilogue generation, there will be
6796 a fallthru edge to EXIT. Special care is required if the last insn
6797 of the last basic block is a call because make_edge folds duplicate
6798 edges, which would result in the fallthru edge also being marked
6799 fake, which would result in the fallthru edge being removed by
6800 remove_fake_edges, which would result in an invalid CFG.
6802 Moreover, we can't elide the outgoing fake edge, since the block
6803 profiler needs to take this into account in order to solve the minimal
6804 spanning tree in the case that the call doesn't return.
6806 Handle this by adding a dummy instruction in a new last basic block. */
6807 if (check_last_block)
6809 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
6810 gimple_stmt_iterator gsi = gsi_last_bb (bb);
6811 gimple t = NULL;
6813 if (!gsi_end_p (gsi))
6814 t = gsi_stmt (gsi);
6816 if (t && need_fake_edge_p (t))
6818 edge e;
6820 e = find_edge (bb, EXIT_BLOCK_PTR);
6821 if (e)
6823 gsi_insert_on_edge (e, gimple_build_nop ());
6824 gsi_commit_edge_inserts ();
6829 /* Now add fake edges to the function exit for any non constant
6830 calls since there is no way that we can determine if they will
6831 return or not... */
6832 for (i = 0; i < last_bb; i++)
6834 basic_block bb = BASIC_BLOCK (i);
6835 gimple_stmt_iterator gsi;
6836 gimple stmt, last_stmt;
6838 if (!bb)
6839 continue;
6841 if (blocks && !TEST_BIT (blocks, i))
6842 continue;
6844 gsi = gsi_last_bb (bb);
6845 if (!gsi_end_p (gsi))
6847 last_stmt = gsi_stmt (gsi);
6850 stmt = gsi_stmt (gsi);
6851 if (need_fake_edge_p (stmt))
6853 edge e;
6855 /* The handling above of the final block before the
6856 epilogue should be enough to verify that there is
6857 no edge to the exit block in CFG already.
6858 Calling make_edge in such case would cause us to
6859 mark that edge as fake and remove it later. */
6860 #ifdef ENABLE_CHECKING
6861 if (stmt == last_stmt)
6863 e = find_edge (bb, EXIT_BLOCK_PTR);
6864 gcc_assert (e == NULL);
6866 #endif
6868 /* Note that the following may create a new basic block
6869 and renumber the existing basic blocks. */
6870 if (stmt != last_stmt)
6872 e = split_block (bb, stmt);
6873 if (e)
6874 blocks_split++;
6876 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
6878 gsi_prev (&gsi);
6880 while (!gsi_end_p (gsi));
6884 if (blocks_split)
6885 verify_flow_info ();
6887 return blocks_split;
6890 /* Purge dead abnormal call edges from basic block BB. */
6892 bool
6893 gimple_purge_dead_abnormal_call_edges (basic_block bb)
6895 bool changed = gimple_purge_dead_eh_edges (bb);
6897 if (cfun->has_nonlocal_label)
6899 gimple stmt = last_stmt (bb);
6900 edge_iterator ei;
6901 edge e;
6903 if (!(stmt && stmt_can_make_abnormal_goto (stmt)))
6904 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6906 if (e->flags & EDGE_ABNORMAL)
6908 remove_edge (e);
6909 changed = true;
6911 else
6912 ei_next (&ei);
6915 /* See gimple_purge_dead_eh_edges below. */
6916 if (changed)
6917 free_dominance_info (CDI_DOMINATORS);
6920 return changed;
6923 /* Removes edge E and all the blocks dominated by it, and updates dominance
6924 information. The IL in E->src needs to be updated separately.
6925 If dominance info is not available, only the edge E is removed.*/
6927 void
6928 remove_edge_and_dominated_blocks (edge e)
6930 VEC (basic_block, heap) *bbs_to_remove = NULL;
6931 VEC (basic_block, heap) *bbs_to_fix_dom = NULL;
6932 bitmap df, df_idom;
6933 edge f;
6934 edge_iterator ei;
6935 bool none_removed = false;
6936 unsigned i;
6937 basic_block bb, dbb;
6938 bitmap_iterator bi;
6940 if (!dom_info_available_p (CDI_DOMINATORS))
6942 remove_edge (e);
6943 return;
6946 /* No updating is needed for edges to exit. */
6947 if (e->dest == EXIT_BLOCK_PTR)
6949 if (cfgcleanup_altered_bbs)
6950 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
6951 remove_edge (e);
6952 return;
6955 /* First, we find the basic blocks to remove. If E->dest has a predecessor
6956 that is not dominated by E->dest, then this set is empty. Otherwise,
6957 all the basic blocks dominated by E->dest are removed.
6959 Also, to DF_IDOM we store the immediate dominators of the blocks in
6960 the dominance frontier of E (i.e., of the successors of the
6961 removed blocks, if there are any, and of E->dest otherwise). */
6962 FOR_EACH_EDGE (f, ei, e->dest->preds)
6964 if (f == e)
6965 continue;
6967 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
6969 none_removed = true;
6970 break;
6974 df = BITMAP_ALLOC (NULL);
6975 df_idom = BITMAP_ALLOC (NULL);
6977 if (none_removed)
6978 bitmap_set_bit (df_idom,
6979 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
6980 else
6982 bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
6983 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6985 FOR_EACH_EDGE (f, ei, bb->succs)
6987 if (f->dest != EXIT_BLOCK_PTR)
6988 bitmap_set_bit (df, f->dest->index);
6991 for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
6992 bitmap_clear_bit (df, bb->index);
6994 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
6996 bb = BASIC_BLOCK (i);
6997 bitmap_set_bit (df_idom,
6998 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
7002 if (cfgcleanup_altered_bbs)
7004 /* Record the set of the altered basic blocks. */
7005 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7006 bitmap_ior_into (cfgcleanup_altered_bbs, df);
7009 /* Remove E and the cancelled blocks. */
7010 if (none_removed)
7011 remove_edge (e);
7012 else
7014 /* Walk backwards so as to get a chance to substitute all
7015 released DEFs into debug stmts. See
7016 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
7017 details. */
7018 for (i = VEC_length (basic_block, bbs_to_remove); i-- > 0; )
7019 delete_basic_block (VEC_index (basic_block, bbs_to_remove, i));
7022 /* Update the dominance information. The immediate dominator may change only
7023 for blocks whose immediate dominator belongs to DF_IDOM:
7025 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
7026 removal. Let Z the arbitrary block such that idom(Z) = Y and
7027 Z dominates X after the removal. Before removal, there exists a path P
7028 from Y to X that avoids Z. Let F be the last edge on P that is
7029 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
7030 dominates W, and because of P, Z does not dominate W), and W belongs to
7031 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
7032 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
7034 bb = BASIC_BLOCK (i);
7035 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
7036 dbb;
7037 dbb = next_dom_son (CDI_DOMINATORS, dbb))
7038 VEC_safe_push (basic_block, heap, bbs_to_fix_dom, dbb);
7041 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
7043 BITMAP_FREE (df);
7044 BITMAP_FREE (df_idom);
7045 VEC_free (basic_block, heap, bbs_to_remove);
7046 VEC_free (basic_block, heap, bbs_to_fix_dom);
7049 /* Purge dead EH edges from basic block BB. */
7051 bool
7052 gimple_purge_dead_eh_edges (basic_block bb)
7054 bool changed = false;
7055 edge e;
7056 edge_iterator ei;
7057 gimple stmt = last_stmt (bb);
7059 if (stmt && stmt_can_throw_internal (stmt))
7060 return false;
7062 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7064 if (e->flags & EDGE_EH)
7066 remove_edge_and_dominated_blocks (e);
7067 changed = true;
7069 else
7070 ei_next (&ei);
7073 return changed;
7076 bool
7077 gimple_purge_all_dead_eh_edges (const_bitmap blocks)
7079 bool changed = false;
7080 unsigned i;
7081 bitmap_iterator bi;
7083 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7085 basic_block bb = BASIC_BLOCK (i);
7087 /* Earlier gimple_purge_dead_eh_edges could have removed
7088 this basic block already. */
7089 gcc_assert (bb || changed);
7090 if (bb != NULL)
7091 changed |= gimple_purge_dead_eh_edges (bb);
7094 return changed;
7097 /* This function is called whenever a new edge is created or
7098 redirected. */
7100 static void
7101 gimple_execute_on_growing_pred (edge e)
7103 basic_block bb = e->dest;
7105 if (phi_nodes (bb))
7106 reserve_phi_args_for_new_edge (bb);
7109 /* This function is called immediately before edge E is removed from
7110 the edge vector E->dest->preds. */
7112 static void
7113 gimple_execute_on_shrinking_pred (edge e)
7115 if (phi_nodes (e->dest))
7116 remove_phi_args (e);
7119 /*---------------------------------------------------------------------------
7120 Helper functions for Loop versioning
7121 ---------------------------------------------------------------------------*/
7123 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
7124 of 'first'. Both of them are dominated by 'new_head' basic block. When
7125 'new_head' was created by 'second's incoming edge it received phi arguments
7126 on the edge by split_edge(). Later, additional edge 'e' was created to
7127 connect 'new_head' and 'first'. Now this routine adds phi args on this
7128 additional edge 'e' that new_head to second edge received as part of edge
7129 splitting. */
7131 static void
7132 gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
7133 basic_block new_head, edge e)
7135 gimple phi1, phi2;
7136 gimple_stmt_iterator psi1, psi2;
7137 tree def;
7138 edge e2 = find_edge (new_head, second);
7140 /* Because NEW_HEAD has been created by splitting SECOND's incoming
7141 edge, we should always have an edge from NEW_HEAD to SECOND. */
7142 gcc_assert (e2 != NULL);
7144 /* Browse all 'second' basic block phi nodes and add phi args to
7145 edge 'e' for 'first' head. PHI args are always in correct order. */
7147 for (psi2 = gsi_start_phis (second),
7148 psi1 = gsi_start_phis (first);
7149 !gsi_end_p (psi2) && !gsi_end_p (psi1);
7150 gsi_next (&psi2), gsi_next (&psi1))
7152 phi1 = gsi_stmt (psi1);
7153 phi2 = gsi_stmt (psi2);
7154 def = PHI_ARG_DEF (phi2, e2->dest_idx);
7155 add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
7160 /* Adds a if else statement to COND_BB with condition COND_EXPR.
7161 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
7162 the destination of the ELSE part. */
7164 static void
7165 gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
7166 basic_block second_head ATTRIBUTE_UNUSED,
7167 basic_block cond_bb, void *cond_e)
7169 gimple_stmt_iterator gsi;
7170 gimple new_cond_expr;
7171 tree cond_expr = (tree) cond_e;
7172 edge e0;
7174 /* Build new conditional expr */
7175 new_cond_expr = gimple_build_cond_from_tree (cond_expr,
7176 NULL_TREE, NULL_TREE);
7178 /* Add new cond in cond_bb. */
7179 gsi = gsi_last_bb (cond_bb);
7180 gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
7182 /* Adjust edges appropriately to connect new head with first head
7183 as well as second head. */
7184 e0 = single_succ_edge (cond_bb);
7185 e0->flags &= ~EDGE_FALLTHRU;
7186 e0->flags |= EDGE_FALSE_VALUE;
7189 struct cfg_hooks gimple_cfg_hooks = {
7190 "gimple",
7191 gimple_verify_flow_info,
7192 gimple_dump_bb, /* dump_bb */
7193 create_bb, /* create_basic_block */
7194 gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
7195 gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
7196 gimple_can_remove_branch_p, /* can_remove_branch_p */
7197 remove_bb, /* delete_basic_block */
7198 gimple_split_block, /* split_block */
7199 gimple_move_block_after, /* move_block_after */
7200 gimple_can_merge_blocks_p, /* can_merge_blocks_p */
7201 gimple_merge_blocks, /* merge_blocks */
7202 gimple_predict_edge, /* predict_edge */
7203 gimple_predicted_by_p, /* predicted_by_p */
7204 gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
7205 gimple_duplicate_bb, /* duplicate_block */
7206 gimple_split_edge, /* split_edge */
7207 gimple_make_forwarder_block, /* make_forward_block */
7208 NULL, /* tidy_fallthru_edge */
7209 gimple_block_ends_with_call_p,/* block_ends_with_call_p */
7210 gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
7211 gimple_flow_call_edges_add, /* flow_call_edges_add */
7212 gimple_execute_on_growing_pred, /* execute_on_growing_pred */
7213 gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
7214 gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
7215 gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
7216 gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
7217 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
7218 flush_pending_stmts /* flush_pending_stmts */
7222 /* Split all critical edges. */
7224 static unsigned int
7225 split_critical_edges (void)
7227 basic_block bb;
7228 edge e;
7229 edge_iterator ei;
7231 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
7232 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
7233 mappings around the calls to split_edge. */
7234 start_recording_case_labels ();
7235 FOR_ALL_BB (bb)
7237 FOR_EACH_EDGE (e, ei, bb->succs)
7239 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
7240 split_edge (e);
7241 /* PRE inserts statements to edges and expects that
7242 since split_critical_edges was done beforehand, committing edge
7243 insertions will not split more edges. In addition to critical
7244 edges we must split edges that have multiple successors and
7245 end by control flow statements, such as RESX.
7246 Go ahead and split them too. This matches the logic in
7247 gimple_find_edge_insert_loc. */
7248 else if ((!single_pred_p (e->dest)
7249 || phi_nodes (e->dest)
7250 || e->dest == EXIT_BLOCK_PTR)
7251 && e->src != ENTRY_BLOCK_PTR
7252 && !(e->flags & EDGE_ABNORMAL))
7254 gimple_stmt_iterator gsi;
7256 gsi = gsi_last_bb (e->src);
7257 if (!gsi_end_p (gsi)
7258 && stmt_ends_bb_p (gsi_stmt (gsi))
7259 && gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN)
7260 split_edge (e);
7264 end_recording_case_labels ();
7265 return 0;
7268 struct gimple_opt_pass pass_split_crit_edges =
7271 GIMPLE_PASS,
7272 "crited", /* name */
7273 NULL, /* gate */
7274 split_critical_edges, /* execute */
7275 NULL, /* sub */
7276 NULL, /* next */
7277 0, /* static_pass_number */
7278 TV_TREE_SPLIT_EDGES, /* tv_id */
7279 PROP_cfg, /* properties required */
7280 PROP_no_crit_edges, /* properties_provided */
7281 0, /* properties_destroyed */
7282 0, /* todo_flags_start */
7283 TODO_dump_func | TODO_verify_flow /* todo_flags_finish */
7288 /* Build a ternary operation and gimplify it. Emit code before GSI.
7289 Return the gimple_val holding the result. */
7291 tree
7292 gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
7293 tree type, tree a, tree b, tree c)
7295 tree ret;
7296 location_t loc = gimple_location (gsi_stmt (*gsi));
7298 ret = fold_build3_loc (loc, code, type, a, b, c);
7299 STRIP_NOPS (ret);
7301 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7302 GSI_SAME_STMT);
7305 /* Build a binary operation and gimplify it. Emit code before GSI.
7306 Return the gimple_val holding the result. */
7308 tree
7309 gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
7310 tree type, tree a, tree b)
7312 tree ret;
7314 ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
7315 STRIP_NOPS (ret);
7317 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7318 GSI_SAME_STMT);
7321 /* Build a unary operation and gimplify it. Emit code before GSI.
7322 Return the gimple_val holding the result. */
7324 tree
7325 gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
7326 tree a)
7328 tree ret;
7330 ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
7331 STRIP_NOPS (ret);
7333 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7334 GSI_SAME_STMT);
7339 /* Emit return warnings. */
7341 static unsigned int
7342 execute_warn_function_return (void)
7344 source_location location;
7345 gimple last;
7346 edge e;
7347 edge_iterator ei;
7349 /* If we have a path to EXIT, then we do return. */
7350 if (TREE_THIS_VOLATILE (cfun->decl)
7351 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
7353 location = UNKNOWN_LOCATION;
7354 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7356 last = last_stmt (e->src);
7357 if (gimple_code (last) == GIMPLE_RETURN
7358 && (location = gimple_location (last)) != UNKNOWN_LOCATION)
7359 break;
7361 if (location == UNKNOWN_LOCATION)
7362 location = cfun->function_end_locus;
7363 warning_at (location, 0, "%<noreturn%> function does return");
7366 /* If we see "return;" in some basic block, then we do reach the end
7367 without returning a value. */
7368 else if (warn_return_type
7369 && !TREE_NO_WARNING (cfun->decl)
7370 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
7371 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
7373 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7375 gimple last = last_stmt (e->src);
7376 if (gimple_code (last) == GIMPLE_RETURN
7377 && gimple_return_retval (last) == NULL
7378 && !gimple_no_warning_p (last))
7380 location = gimple_location (last);
7381 if (location == UNKNOWN_LOCATION)
7382 location = cfun->function_end_locus;
7383 warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
7384 TREE_NO_WARNING (cfun->decl) = 1;
7385 break;
7389 return 0;
7393 /* Given a basic block B which ends with a conditional and has
7394 precisely two successors, determine which of the edges is taken if
7395 the conditional is true and which is taken if the conditional is
7396 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
7398 void
7399 extract_true_false_edges_from_block (basic_block b,
7400 edge *true_edge,
7401 edge *false_edge)
7403 edge e = EDGE_SUCC (b, 0);
7405 if (e->flags & EDGE_TRUE_VALUE)
7407 *true_edge = e;
7408 *false_edge = EDGE_SUCC (b, 1);
7410 else
7412 *false_edge = e;
7413 *true_edge = EDGE_SUCC (b, 1);
7417 struct gimple_opt_pass pass_warn_function_return =
7420 GIMPLE_PASS,
7421 NULL, /* name */
7422 NULL, /* gate */
7423 execute_warn_function_return, /* execute */
7424 NULL, /* sub */
7425 NULL, /* next */
7426 0, /* static_pass_number */
7427 TV_NONE, /* tv_id */
7428 PROP_cfg, /* properties_required */
7429 0, /* properties_provided */
7430 0, /* properties_destroyed */
7431 0, /* todo_flags_start */
7432 0 /* todo_flags_finish */
7436 /* Emit noreturn warnings. */
7438 static unsigned int
7439 execute_warn_function_noreturn (void)
7441 if (warn_missing_noreturn
7442 && !TREE_THIS_VOLATILE (cfun->decl)
7443 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
7444 && !lang_hooks.missing_noreturn_ok_p (cfun->decl))
7445 warning_at (DECL_SOURCE_LOCATION (cfun->decl), OPT_Wmissing_noreturn,
7446 "function might be possible candidate "
7447 "for attribute %<noreturn%>");
7448 return 0;
7451 struct gimple_opt_pass pass_warn_function_noreturn =
7454 GIMPLE_PASS,
7455 NULL, /* name */
7456 NULL, /* gate */
7457 execute_warn_function_noreturn, /* execute */
7458 NULL, /* sub */
7459 NULL, /* next */
7460 0, /* static_pass_number */
7461 TV_NONE, /* tv_id */
7462 PROP_cfg, /* properties_required */
7463 0, /* properties_provided */
7464 0, /* properties_destroyed */
7465 0, /* todo_flags_start */
7466 0 /* todo_flags_finish */
7471 /* Walk a gimplified function and warn for functions whose return value is
7472 ignored and attribute((warn_unused_result)) is set. This is done before
7473 inlining, so we don't have to worry about that. */
7475 static void
7476 do_warn_unused_result (gimple_seq seq)
7478 tree fdecl, ftype;
7479 gimple_stmt_iterator i;
7481 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
7483 gimple g = gsi_stmt (i);
7485 switch (gimple_code (g))
7487 case GIMPLE_BIND:
7488 do_warn_unused_result (gimple_bind_body (g));
7489 break;
7490 case GIMPLE_TRY:
7491 do_warn_unused_result (gimple_try_eval (g));
7492 do_warn_unused_result (gimple_try_cleanup (g));
7493 break;
7494 case GIMPLE_CATCH:
7495 do_warn_unused_result (gimple_catch_handler (g));
7496 break;
7497 case GIMPLE_EH_FILTER:
7498 do_warn_unused_result (gimple_eh_filter_failure (g));
7499 break;
7501 case GIMPLE_CALL:
7502 if (gimple_call_lhs (g))
7503 break;
7505 /* This is a naked call, as opposed to a GIMPLE_CALL with an
7506 LHS. All calls whose value is ignored should be
7507 represented like this. Look for the attribute. */
7508 fdecl = gimple_call_fndecl (g);
7509 ftype = TREE_TYPE (TREE_TYPE (gimple_call_fn (g)));
7511 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
7513 location_t loc = gimple_location (g);
7515 if (fdecl)
7516 warning_at (loc, OPT_Wunused_result,
7517 "ignoring return value of %qD, "
7518 "declared with attribute warn_unused_result",
7519 fdecl);
7520 else
7521 warning_at (loc, OPT_Wunused_result,
7522 "ignoring return value of function "
7523 "declared with attribute warn_unused_result");
7525 break;
7527 default:
7528 /* Not a container, not a call, or a call whose value is used. */
7529 break;
7534 static unsigned int
7535 run_warn_unused_result (void)
7537 do_warn_unused_result (gimple_body (current_function_decl));
7538 return 0;
7541 static bool
7542 gate_warn_unused_result (void)
7544 return flag_warn_unused_result;
7547 struct gimple_opt_pass pass_warn_unused_result =
7550 GIMPLE_PASS,
7551 "*warn_unused_result", /* name */
7552 gate_warn_unused_result, /* gate */
7553 run_warn_unused_result, /* execute */
7554 NULL, /* sub */
7555 NULL, /* next */
7556 0, /* static_pass_number */
7557 TV_NONE, /* tv_id */
7558 PROP_gimple_any, /* properties_required */
7559 0, /* properties_provided */
7560 0, /* properties_destroyed */
7561 0, /* todo_flags_start */
7562 0, /* todo_flags_finish */