Mark ChangeLog
[official-gcc.git] / gcc / tree-cfg.c
blobb32da2ef6fefb2a8c2ff6b111656afe8b33207f1
1 /* Control flow functions for trees.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "flags.h"
29 #include "function.h"
30 #include "ggc.h"
31 #include "gimple-pretty-print.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "tree-pass.h"
35 #include "diagnostic-core.h"
36 #include "except.h"
37 #include "cfgloop.h"
38 #include "tree-ssa-propagate.h"
39 #include "value-prof.h"
40 #include "pointer-set.h"
41 #include "tree-inline.h"
42 #include "target.h"
44 /* This file contains functions for building the Control Flow Graph (CFG)
45 for a function tree. */
47 /* Local declarations. */
49 /* Initial capacity for the basic block array. */
50 static const int initial_cfg_capacity = 20;
52 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
53 which use a particular edge. The CASE_LABEL_EXPRs are chained together
54 via their CASE_CHAIN field, which we clear after we're done with the
55 hash table to prevent problems with duplication of GIMPLE_SWITCHes.
57 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
58 update the case vector in response to edge redirections.
60 Right now this table is set up and torn down at key points in the
61 compilation process. It would be nice if we could make the table
62 more persistent. The key is getting notification of changes to
63 the CFG (particularly edge removal, creation and redirection). */
65 static struct pointer_map_t *edge_to_cases;
67 /* If we record edge_to_cases, this bitmap will hold indexes
68 of basic blocks that end in a GIMPLE_SWITCH which we touched
69 due to edge manipulations. */
71 static bitmap touched_switch_bbs;
73 /* CFG statistics. */
74 struct cfg_stats_d
76 long num_merged_labels;
79 static struct cfg_stats_d cfg_stats;
81 /* Nonzero if we found a computed goto while building basic blocks. */
82 static bool found_computed_goto;
84 /* Hash table to store last discriminator assigned for each locus. */
85 struct locus_discrim_map
87 location_t locus;
88 int discriminator;
90 static htab_t discriminator_per_locus;
92 /* Basic blocks and flowgraphs. */
93 static void make_blocks (gimple_seq);
94 static void factor_computed_gotos (void);
96 /* Edges. */
97 static void make_edges (void);
98 static void make_cond_expr_edges (basic_block);
99 static void make_gimple_switch_edges (basic_block);
100 static void make_goto_expr_edges (basic_block);
101 static void make_gimple_asm_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);
108 /* Various helpers. */
109 static inline bool stmt_starts_bb_p (gimple, gimple);
110 static int gimple_verify_flow_info (void);
111 static void gimple_make_forwarder_block (edge);
112 static gimple first_non_label_stmt (basic_block);
113 static bool verify_gimple_transaction (gimple);
115 /* Flowgraph optimization and cleanup. */
116 static void gimple_merge_blocks (basic_block, basic_block);
117 static bool gimple_can_merge_blocks_p (basic_block, basic_block);
118 static void remove_bb (basic_block);
119 static edge find_taken_edge_computed_goto (basic_block, tree);
120 static edge find_taken_edge_cond_expr (basic_block, tree);
121 static edge find_taken_edge_switch_expr (basic_block, tree);
122 static tree find_case_label_for_value (gimple, tree);
124 void
125 init_empty_tree_cfg_for_function (struct function *fn)
127 /* Initialize the basic block array. */
128 init_flow (fn);
129 profile_status_for_function (fn) = PROFILE_ABSENT;
130 n_basic_blocks_for_function (fn) = NUM_FIXED_BLOCKS;
131 last_basic_block_for_function (fn) = NUM_FIXED_BLOCKS;
132 vec_alloc (basic_block_info_for_function (fn), initial_cfg_capacity);
133 vec_safe_grow_cleared (basic_block_info_for_function (fn),
134 initial_cfg_capacity);
136 /* Build a mapping of labels to their associated blocks. */
137 vec_alloc (label_to_block_map_for_function (fn), initial_cfg_capacity);
138 vec_safe_grow_cleared (label_to_block_map_for_function (fn),
139 initial_cfg_capacity);
141 SET_BASIC_BLOCK_FOR_FUNCTION (fn, ENTRY_BLOCK,
142 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn));
143 SET_BASIC_BLOCK_FOR_FUNCTION (fn, EXIT_BLOCK,
144 EXIT_BLOCK_PTR_FOR_FUNCTION (fn));
146 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn)->next_bb
147 = EXIT_BLOCK_PTR_FOR_FUNCTION (fn);
148 EXIT_BLOCK_PTR_FOR_FUNCTION (fn)->prev_bb
149 = ENTRY_BLOCK_PTR_FOR_FUNCTION (fn);
152 void
153 init_empty_tree_cfg (void)
155 init_empty_tree_cfg_for_function (cfun);
158 /*---------------------------------------------------------------------------
159 Create basic blocks
160 ---------------------------------------------------------------------------*/
162 /* Entry point to the CFG builder for trees. SEQ is the sequence of
163 statements to be added to the flowgraph. */
165 static void
166 build_gimple_cfg (gimple_seq seq)
168 /* Register specific gimple functions. */
169 gimple_register_cfg_hooks ();
171 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
173 init_empty_tree_cfg ();
175 found_computed_goto = 0;
176 make_blocks (seq);
178 /* Computed gotos are hell to deal with, especially if there are
179 lots of them with a large number of destinations. So we factor
180 them to a common computed goto location before we build the
181 edge list. After we convert back to normal form, we will un-factor
182 the computed gotos since factoring introduces an unwanted jump. */
183 if (found_computed_goto)
184 factor_computed_gotos ();
186 /* Make sure there is always at least one block, even if it's empty. */
187 if (n_basic_blocks == NUM_FIXED_BLOCKS)
188 create_empty_bb (ENTRY_BLOCK_PTR);
190 /* Adjust the size of the array. */
191 if (basic_block_info->length () < (size_t) n_basic_blocks)
192 vec_safe_grow_cleared (basic_block_info, n_basic_blocks);
194 /* To speed up statement iterator walks, we first purge dead labels. */
195 cleanup_dead_labels ();
197 /* Group case nodes to reduce the number of edges.
198 We do this after cleaning up dead labels because otherwise we miss
199 a lot of obvious case merging opportunities. */
200 group_case_labels ();
202 /* Create the edges of the flowgraph. */
203 discriminator_per_locus = htab_create (13, locus_map_hash, locus_map_eq,
204 free);
205 make_edges ();
206 cleanup_dead_labels ();
207 htab_delete (discriminator_per_locus);
210 static unsigned int
211 execute_build_cfg (void)
213 gimple_seq body = gimple_body (current_function_decl);
215 build_gimple_cfg (body);
216 gimple_set_body (current_function_decl, NULL);
217 if (dump_file && (dump_flags & TDF_DETAILS))
219 fprintf (dump_file, "Scope blocks:\n");
220 dump_scope_blocks (dump_file, dump_flags);
222 return 0;
225 struct gimple_opt_pass pass_build_cfg =
228 GIMPLE_PASS,
229 "cfg", /* name */
230 OPTGROUP_NONE, /* optinfo_flags */
231 NULL, /* gate */
232 execute_build_cfg, /* execute */
233 NULL, /* sub */
234 NULL, /* next */
235 0, /* static_pass_number */
236 TV_TREE_CFG, /* tv_id */
237 PROP_gimple_leh, /* properties_required */
238 PROP_cfg, /* properties_provided */
239 0, /* properties_destroyed */
240 0, /* todo_flags_start */
241 TODO_verify_stmts | TODO_cleanup_cfg /* todo_flags_finish */
246 /* Return true if T is a computed goto. */
248 static bool
249 computed_goto_p (gimple t)
251 return (gimple_code (t) == GIMPLE_GOTO
252 && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
256 /* Search the CFG for any computed gotos. If found, factor them to a
257 common computed goto site. Also record the location of that site so
258 that we can un-factor the gotos after we have converted back to
259 normal form. */
261 static void
262 factor_computed_gotos (void)
264 basic_block bb;
265 tree factored_label_decl = NULL;
266 tree var = NULL;
267 gimple factored_computed_goto_label = NULL;
268 gimple factored_computed_goto = NULL;
270 /* We know there are one or more computed gotos in this function.
271 Examine the last statement in each basic block to see if the block
272 ends with a computed goto. */
274 FOR_EACH_BB (bb)
276 gimple_stmt_iterator gsi = gsi_last_bb (bb);
277 gimple last;
279 if (gsi_end_p (gsi))
280 continue;
282 last = gsi_stmt (gsi);
284 /* Ignore the computed goto we create when we factor the original
285 computed gotos. */
286 if (last == factored_computed_goto)
287 continue;
289 /* If the last statement is a computed goto, factor it. */
290 if (computed_goto_p (last))
292 gimple assignment;
294 /* The first time we find a computed goto we need to create
295 the factored goto block and the variable each original
296 computed goto will use for their goto destination. */
297 if (!factored_computed_goto)
299 basic_block new_bb = create_empty_bb (bb);
300 gimple_stmt_iterator new_gsi = gsi_start_bb (new_bb);
302 /* Create the destination of the factored goto. Each original
303 computed goto will put its desired destination into this
304 variable and jump to the label we create immediately
305 below. */
306 var = create_tmp_var (ptr_type_node, "gotovar");
308 /* Build a label for the new block which will contain the
309 factored computed goto. */
310 factored_label_decl = create_artificial_label (UNKNOWN_LOCATION);
311 factored_computed_goto_label
312 = gimple_build_label (factored_label_decl);
313 gsi_insert_after (&new_gsi, factored_computed_goto_label,
314 GSI_NEW_STMT);
316 /* Build our new computed goto. */
317 factored_computed_goto = gimple_build_goto (var);
318 gsi_insert_after (&new_gsi, factored_computed_goto, GSI_NEW_STMT);
321 /* Copy the original computed goto's destination into VAR. */
322 assignment = gimple_build_assign (var, gimple_goto_dest (last));
323 gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
325 /* And re-vector the computed goto to the new destination. */
326 gimple_goto_set_dest (last, factored_label_decl);
332 /* Build a flowgraph for the sequence of stmts SEQ. */
334 static void
335 make_blocks (gimple_seq seq)
337 gimple_stmt_iterator i = gsi_start (seq);
338 gimple stmt = NULL;
339 bool start_new_block = true;
340 bool first_stmt_of_seq = true;
341 basic_block bb = ENTRY_BLOCK_PTR;
343 while (!gsi_end_p (i))
345 gimple prev_stmt;
347 prev_stmt = stmt;
348 stmt = gsi_stmt (i);
350 /* If the statement starts a new basic block or if we have determined
351 in a previous pass that we need to create a new block for STMT, do
352 so now. */
353 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
355 if (!first_stmt_of_seq)
356 gsi_split_seq_before (&i, &seq);
357 bb = create_basic_block (seq, NULL, bb);
358 start_new_block = false;
361 /* Now add STMT to BB and create the subgraphs for special statement
362 codes. */
363 gimple_set_bb (stmt, bb);
365 if (computed_goto_p (stmt))
366 found_computed_goto = true;
368 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
369 next iteration. */
370 if (stmt_ends_bb_p (stmt))
372 /* If the stmt can make abnormal goto use a new temporary
373 for the assignment to the LHS. This makes sure the old value
374 of the LHS is available on the abnormal edge. Otherwise
375 we will end up with overlapping life-ranges for abnormal
376 SSA names. */
377 if (gimple_has_lhs (stmt)
378 && stmt_can_make_abnormal_goto (stmt)
379 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
381 tree lhs = gimple_get_lhs (stmt);
382 tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
383 gimple s = gimple_build_assign (lhs, tmp);
384 gimple_set_location (s, gimple_location (stmt));
385 gimple_set_block (s, gimple_block (stmt));
386 gimple_set_lhs (stmt, tmp);
387 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
388 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
389 DECL_GIMPLE_REG_P (tmp) = 1;
390 gsi_insert_after (&i, s, GSI_SAME_STMT);
392 start_new_block = true;
395 gsi_next (&i);
396 first_stmt_of_seq = false;
401 /* Create and return a new empty basic block after bb AFTER. */
403 static basic_block
404 create_bb (void *h, void *e, basic_block after)
406 basic_block bb;
408 gcc_assert (!e);
410 /* Create and initialize a new basic block. Since alloc_block uses
411 GC allocation that clears memory to allocate a basic block, we do
412 not have to clear the newly allocated basic block here. */
413 bb = alloc_block ();
415 bb->index = last_basic_block;
416 bb->flags = BB_NEW;
417 set_bb_seq (bb, h ? (gimple_seq) h : NULL);
419 /* Add the new block to the linked list of blocks. */
420 link_block (bb, after);
422 /* Grow the basic block array if needed. */
423 if ((size_t) last_basic_block == basic_block_info->length ())
425 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
426 vec_safe_grow_cleared (basic_block_info, new_size);
429 /* Add the newly created block to the array. */
430 SET_BASIC_BLOCK (last_basic_block, bb);
432 n_basic_blocks++;
433 last_basic_block++;
435 return bb;
439 /*---------------------------------------------------------------------------
440 Edge creation
441 ---------------------------------------------------------------------------*/
443 /* Fold COND_EXPR_COND of each COND_EXPR. */
445 void
446 fold_cond_expr_cond (void)
448 basic_block bb;
450 FOR_EACH_BB (bb)
452 gimple stmt = last_stmt (bb);
454 if (stmt && gimple_code (stmt) == GIMPLE_COND)
456 location_t loc = gimple_location (stmt);
457 tree cond;
458 bool zerop, onep;
460 fold_defer_overflow_warnings ();
461 cond = fold_binary_loc (loc, gimple_cond_code (stmt), boolean_type_node,
462 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
463 if (cond)
465 zerop = integer_zerop (cond);
466 onep = integer_onep (cond);
468 else
469 zerop = onep = false;
471 fold_undefer_overflow_warnings (zerop || onep,
472 stmt,
473 WARN_STRICT_OVERFLOW_CONDITIONAL);
474 if (zerop)
475 gimple_cond_make_false (stmt);
476 else if (onep)
477 gimple_cond_make_true (stmt);
482 /* Join all the blocks in the flowgraph. */
484 static void
485 make_edges (void)
487 basic_block bb;
488 struct omp_region *cur_region = NULL;
490 /* Create an edge from entry to the first block with executable
491 statements in it. */
492 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (NUM_FIXED_BLOCKS), EDGE_FALLTHRU);
494 /* Traverse the basic block array placing edges. */
495 FOR_EACH_BB (bb)
497 gimple last = last_stmt (bb);
498 bool fallthru;
500 if (last)
502 enum gimple_code code = gimple_code (last);
503 switch (code)
505 case GIMPLE_GOTO:
506 make_goto_expr_edges (bb);
507 fallthru = false;
508 break;
509 case GIMPLE_RETURN:
510 make_edge (bb, EXIT_BLOCK_PTR, 0);
511 fallthru = false;
512 break;
513 case GIMPLE_COND:
514 make_cond_expr_edges (bb);
515 fallthru = false;
516 break;
517 case GIMPLE_SWITCH:
518 make_gimple_switch_edges (bb);
519 fallthru = false;
520 break;
521 case GIMPLE_RESX:
522 make_eh_edges (last);
523 fallthru = false;
524 break;
525 case GIMPLE_EH_DISPATCH:
526 fallthru = make_eh_dispatch_edges (last);
527 break;
529 case GIMPLE_CALL:
530 /* If this function receives a nonlocal goto, then we need to
531 make edges from this call site to all the nonlocal goto
532 handlers. */
533 if (stmt_can_make_abnormal_goto (last))
534 make_abnormal_goto_edges (bb, true);
536 /* If this statement has reachable exception handlers, then
537 create abnormal edges to them. */
538 make_eh_edges (last);
540 /* BUILTIN_RETURN is really a return statement. */
541 if (gimple_call_builtin_p (last, BUILT_IN_RETURN))
542 make_edge (bb, EXIT_BLOCK_PTR, 0), fallthru = false;
543 /* Some calls are known not to return. */
544 else
545 fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
546 break;
548 case GIMPLE_ASSIGN:
549 /* A GIMPLE_ASSIGN may throw internally and thus be considered
550 control-altering. */
551 if (is_ctrl_altering_stmt (last))
552 make_eh_edges (last);
553 fallthru = true;
554 break;
556 case GIMPLE_ASM:
557 make_gimple_asm_edges (bb);
558 fallthru = true;
559 break;
561 case GIMPLE_OMP_PARALLEL:
562 case GIMPLE_OMP_TASK:
563 case GIMPLE_OMP_FOR:
564 case GIMPLE_OMP_SINGLE:
565 case GIMPLE_OMP_MASTER:
566 case GIMPLE_OMP_ORDERED:
567 case GIMPLE_OMP_CRITICAL:
568 case GIMPLE_OMP_SECTION:
569 cur_region = new_omp_region (bb, code, cur_region);
570 fallthru = true;
571 break;
573 case GIMPLE_OMP_SECTIONS:
574 cur_region = new_omp_region (bb, code, cur_region);
575 fallthru = true;
576 break;
578 case GIMPLE_OMP_SECTIONS_SWITCH:
579 fallthru = false;
580 break;
582 case GIMPLE_OMP_ATOMIC_LOAD:
583 case GIMPLE_OMP_ATOMIC_STORE:
584 fallthru = true;
585 break;
587 case GIMPLE_OMP_RETURN:
588 /* In the case of a GIMPLE_OMP_SECTION, the edge will go
589 somewhere other than the next block. This will be
590 created later. */
591 cur_region->exit = bb;
592 fallthru = cur_region->type != GIMPLE_OMP_SECTION;
593 cur_region = cur_region->outer;
594 break;
596 case GIMPLE_OMP_CONTINUE:
597 cur_region->cont = bb;
598 switch (cur_region->type)
600 case GIMPLE_OMP_FOR:
601 /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
602 succs edges as abnormal to prevent splitting
603 them. */
604 single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
605 /* Make the loopback edge. */
606 make_edge (bb, single_succ (cur_region->entry),
607 EDGE_ABNORMAL);
609 /* Create an edge from GIMPLE_OMP_FOR to exit, which
610 corresponds to the case that the body of the loop
611 is not executed at all. */
612 make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
613 make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
614 fallthru = false;
615 break;
617 case GIMPLE_OMP_SECTIONS:
618 /* Wire up the edges into and out of the nested sections. */
620 basic_block switch_bb = single_succ (cur_region->entry);
622 struct omp_region *i;
623 for (i = cur_region->inner; i ; i = i->next)
625 gcc_assert (i->type == GIMPLE_OMP_SECTION);
626 make_edge (switch_bb, i->entry, 0);
627 make_edge (i->exit, bb, EDGE_FALLTHRU);
630 /* Make the loopback edge to the block with
631 GIMPLE_OMP_SECTIONS_SWITCH. */
632 make_edge (bb, switch_bb, 0);
634 /* Make the edge from the switch to exit. */
635 make_edge (switch_bb, bb->next_bb, 0);
636 fallthru = false;
638 break;
640 default:
641 gcc_unreachable ();
643 break;
645 case GIMPLE_TRANSACTION:
647 tree abort_label = gimple_transaction_label (last);
648 if (abort_label)
649 make_edge (bb, label_to_block (abort_label), EDGE_TM_ABORT);
650 fallthru = true;
652 break;
654 default:
655 gcc_assert (!stmt_ends_bb_p (last));
656 fallthru = true;
659 else
660 fallthru = true;
662 if (fallthru)
664 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
665 if (last)
666 assign_discriminator (gimple_location (last), bb->next_bb);
670 if (root_omp_region)
671 free_omp_regions ();
673 /* Fold COND_EXPR_COND of each COND_EXPR. */
674 fold_cond_expr_cond ();
677 /* Trivial hash function for a location_t. ITEM is a pointer to
678 a hash table entry that maps a location_t to a discriminator. */
680 static unsigned int
681 locus_map_hash (const void *item)
683 return ((const struct locus_discrim_map *) item)->locus;
686 /* Equality function for the locus-to-discriminator map. VA and VB
687 point to the two hash table entries to compare. */
689 static int
690 locus_map_eq (const void *va, const void *vb)
692 const struct locus_discrim_map *a = (const struct locus_discrim_map *) va;
693 const struct locus_discrim_map *b = (const struct locus_discrim_map *) vb;
694 return a->locus == b->locus;
697 /* Find the next available discriminator value for LOCUS. The
698 discriminator distinguishes among several basic blocks that
699 share a common locus, allowing for more accurate sample-based
700 profiling. */
702 static int
703 next_discriminator_for_locus (location_t locus)
705 struct locus_discrim_map item;
706 struct locus_discrim_map **slot;
708 item.locus = locus;
709 item.discriminator = 0;
710 slot = (struct locus_discrim_map **)
711 htab_find_slot_with_hash (discriminator_per_locus, (void *) &item,
712 (hashval_t) locus, INSERT);
713 gcc_assert (slot);
714 if (*slot == HTAB_EMPTY_ENTRY)
716 *slot = XNEW (struct locus_discrim_map);
717 gcc_assert (*slot);
718 (*slot)->locus = locus;
719 (*slot)->discriminator = 0;
721 (*slot)->discriminator++;
722 return (*slot)->discriminator;
725 /* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line. */
727 static bool
728 same_line_p (location_t locus1, location_t locus2)
730 expanded_location from, to;
732 if (locus1 == locus2)
733 return true;
735 from = expand_location (locus1);
736 to = expand_location (locus2);
738 if (from.line != to.line)
739 return false;
740 if (from.file == to.file)
741 return true;
742 return (from.file != NULL
743 && to.file != NULL
744 && filename_cmp (from.file, to.file) == 0);
747 /* Assign a unique discriminator value to block BB if it begins at the same
748 LOCUS as its predecessor block. */
750 static void
751 assign_discriminator (location_t locus, basic_block bb)
753 gimple first_in_to_bb, last_in_to_bb;
755 if (locus == 0 || bb->discriminator != 0)
756 return;
758 first_in_to_bb = first_non_label_stmt (bb);
759 last_in_to_bb = last_stmt (bb);
760 if ((first_in_to_bb && same_line_p (locus, gimple_location (first_in_to_bb)))
761 || (last_in_to_bb && same_line_p (locus, gimple_location (last_in_to_bb))))
762 bb->discriminator = next_discriminator_for_locus (locus);
765 /* Create the edges for a GIMPLE_COND starting at block BB. */
767 static void
768 make_cond_expr_edges (basic_block bb)
770 gimple entry = last_stmt (bb);
771 gimple then_stmt, else_stmt;
772 basic_block then_bb, else_bb;
773 tree then_label, else_label;
774 edge e;
775 location_t entry_locus;
777 gcc_assert (entry);
778 gcc_assert (gimple_code (entry) == GIMPLE_COND);
780 entry_locus = gimple_location (entry);
782 /* Entry basic blocks for each component. */
783 then_label = gimple_cond_true_label (entry);
784 else_label = gimple_cond_false_label (entry);
785 then_bb = label_to_block (then_label);
786 else_bb = label_to_block (else_label);
787 then_stmt = first_stmt (then_bb);
788 else_stmt = first_stmt (else_bb);
790 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
791 assign_discriminator (entry_locus, then_bb);
792 e->goto_locus = gimple_location (then_stmt);
793 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
794 if (e)
796 assign_discriminator (entry_locus, else_bb);
797 e->goto_locus = gimple_location (else_stmt);
800 /* We do not need the labels anymore. */
801 gimple_cond_set_true_label (entry, NULL_TREE);
802 gimple_cond_set_false_label (entry, NULL_TREE);
806 /* Called for each element in the hash table (P) as we delete the
807 edge to cases hash table.
809 Clear all the TREE_CHAINs to prevent problems with copying of
810 SWITCH_EXPRs and structure sharing rules, then free the hash table
811 element. */
813 static bool
814 edge_to_cases_cleanup (const void *key ATTRIBUTE_UNUSED, void **value,
815 void *data ATTRIBUTE_UNUSED)
817 tree t, next;
819 for (t = (tree) *value; t; t = next)
821 next = CASE_CHAIN (t);
822 CASE_CHAIN (t) = NULL;
825 *value = NULL;
826 return true;
829 /* Start recording information mapping edges to case labels. */
831 void
832 start_recording_case_labels (void)
834 gcc_assert (edge_to_cases == NULL);
835 edge_to_cases = pointer_map_create ();
836 touched_switch_bbs = BITMAP_ALLOC (NULL);
839 /* Return nonzero if we are recording information for case labels. */
841 static bool
842 recording_case_labels_p (void)
844 return (edge_to_cases != NULL);
847 /* Stop recording information mapping edges to case labels and
848 remove any information we have recorded. */
849 void
850 end_recording_case_labels (void)
852 bitmap_iterator bi;
853 unsigned i;
854 pointer_map_traverse (edge_to_cases, edge_to_cases_cleanup, NULL);
855 pointer_map_destroy (edge_to_cases);
856 edge_to_cases = NULL;
857 EXECUTE_IF_SET_IN_BITMAP (touched_switch_bbs, 0, i, bi)
859 basic_block bb = BASIC_BLOCK (i);
860 if (bb)
862 gimple stmt = last_stmt (bb);
863 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
864 group_case_labels_stmt (stmt);
867 BITMAP_FREE (touched_switch_bbs);
870 /* If we are inside a {start,end}_recording_cases block, then return
871 a chain of CASE_LABEL_EXPRs from T which reference E.
873 Otherwise return NULL. */
875 static tree
876 get_cases_for_edge (edge e, gimple t)
878 void **slot;
879 size_t i, n;
881 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
882 chains available. Return NULL so the caller can detect this case. */
883 if (!recording_case_labels_p ())
884 return NULL;
886 slot = pointer_map_contains (edge_to_cases, e);
887 if (slot)
888 return (tree) *slot;
890 /* If we did not find E in the hash table, then this must be the first
891 time we have been queried for information about E & T. Add all the
892 elements from T to the hash table then perform the query again. */
894 n = gimple_switch_num_labels (t);
895 for (i = 0; i < n; i++)
897 tree elt = gimple_switch_label (t, i);
898 tree lab = CASE_LABEL (elt);
899 basic_block label_bb = label_to_block (lab);
900 edge this_edge = find_edge (e->src, label_bb);
902 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
903 a new chain. */
904 slot = pointer_map_insert (edge_to_cases, this_edge);
905 CASE_CHAIN (elt) = (tree) *slot;
906 *slot = elt;
909 return (tree) *pointer_map_contains (edge_to_cases, e);
912 /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
914 static void
915 make_gimple_switch_edges (basic_block bb)
917 gimple entry = last_stmt (bb);
918 location_t entry_locus;
919 size_t i, n;
921 entry_locus = gimple_location (entry);
923 n = gimple_switch_num_labels (entry);
925 for (i = 0; i < n; ++i)
927 tree lab = CASE_LABEL (gimple_switch_label (entry, i));
928 basic_block label_bb = label_to_block (lab);
929 make_edge (bb, label_bb, 0);
930 assign_discriminator (entry_locus, label_bb);
935 /* Return the basic block holding label DEST. */
937 basic_block
938 label_to_block_fn (struct function *ifun, tree dest)
940 int uid = LABEL_DECL_UID (dest);
942 /* We would die hard when faced by an undefined label. Emit a label to
943 the very first basic block. This will hopefully make even the dataflow
944 and undefined variable warnings quite right. */
945 if (seen_error () && uid < 0)
947 gimple_stmt_iterator gsi = gsi_start_bb (BASIC_BLOCK (NUM_FIXED_BLOCKS));
948 gimple stmt;
950 stmt = gimple_build_label (dest);
951 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
952 uid = LABEL_DECL_UID (dest);
954 if (vec_safe_length (ifun->cfg->x_label_to_block_map) <= (unsigned int) uid)
955 return NULL;
956 return (*ifun->cfg->x_label_to_block_map)[uid];
959 /* Create edges for an abnormal goto statement at block BB. If FOR_CALL
960 is true, the source statement is a CALL_EXPR instead of a GOTO_EXPR. */
962 void
963 make_abnormal_goto_edges (basic_block bb, bool for_call)
965 basic_block target_bb;
966 gimple_stmt_iterator gsi;
968 FOR_EACH_BB (target_bb)
969 for (gsi = gsi_start_bb (target_bb); !gsi_end_p (gsi); gsi_next (&gsi))
971 gimple label_stmt = gsi_stmt (gsi);
972 tree target;
974 if (gimple_code (label_stmt) != GIMPLE_LABEL)
975 break;
977 target = gimple_label_label (label_stmt);
979 /* Make an edge to every label block that has been marked as a
980 potential target for a computed goto or a non-local goto. */
981 if ((FORCED_LABEL (target) && !for_call)
982 || (DECL_NONLOCAL (target) && for_call))
984 make_edge (bb, target_bb, EDGE_ABNORMAL);
985 break;
990 /* Create edges for a goto statement at block BB. */
992 static void
993 make_goto_expr_edges (basic_block bb)
995 gimple_stmt_iterator last = gsi_last_bb (bb);
996 gimple goto_t = gsi_stmt (last);
998 /* A simple GOTO creates normal edges. */
999 if (simple_goto_p (goto_t))
1001 tree dest = gimple_goto_dest (goto_t);
1002 basic_block label_bb = label_to_block (dest);
1003 edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
1004 e->goto_locus = gimple_location (goto_t);
1005 assign_discriminator (e->goto_locus, label_bb);
1006 gsi_remove (&last, true);
1007 return;
1010 /* A computed GOTO creates abnormal edges. */
1011 make_abnormal_goto_edges (bb, false);
1014 /* Create edges for an asm statement with labels at block BB. */
1016 static void
1017 make_gimple_asm_edges (basic_block bb)
1019 gimple stmt = last_stmt (bb);
1020 location_t stmt_loc = gimple_location (stmt);
1021 int i, n = gimple_asm_nlabels (stmt);
1023 for (i = 0; i < n; ++i)
1025 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
1026 basic_block label_bb = label_to_block (label);
1027 make_edge (bb, label_bb, 0);
1028 assign_discriminator (stmt_loc, label_bb);
1032 /*---------------------------------------------------------------------------
1033 Flowgraph analysis
1034 ---------------------------------------------------------------------------*/
1036 /* Cleanup useless labels in basic blocks. This is something we wish
1037 to do early because it allows us to group case labels before creating
1038 the edges for the CFG, and it speeds up block statement iterators in
1039 all passes later on.
1040 We rerun this pass after CFG is created, to get rid of the labels that
1041 are no longer referenced. After then we do not run it any more, since
1042 (almost) no new labels should be created. */
1044 /* A map from basic block index to the leading label of that block. */
1045 static struct label_record
1047 /* The label. */
1048 tree label;
1050 /* True if the label is referenced from somewhere. */
1051 bool used;
1052 } *label_for_bb;
1054 /* Given LABEL return the first label in the same basic block. */
1056 static tree
1057 main_block_label (tree label)
1059 basic_block bb = label_to_block (label);
1060 tree main_label = label_for_bb[bb->index].label;
1062 /* label_to_block possibly inserted undefined label into the chain. */
1063 if (!main_label)
1065 label_for_bb[bb->index].label = label;
1066 main_label = label;
1069 label_for_bb[bb->index].used = true;
1070 return main_label;
1073 /* Clean up redundant labels within the exception tree. */
1075 static void
1076 cleanup_dead_labels_eh (void)
1078 eh_landing_pad lp;
1079 eh_region r;
1080 tree lab;
1081 int i;
1083 if (cfun->eh == NULL)
1084 return;
1086 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1087 if (lp && lp->post_landing_pad)
1089 lab = main_block_label (lp->post_landing_pad);
1090 if (lab != lp->post_landing_pad)
1092 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1093 EH_LANDING_PAD_NR (lab) = lp->index;
1097 FOR_ALL_EH_REGION (r)
1098 switch (r->type)
1100 case ERT_CLEANUP:
1101 case ERT_MUST_NOT_THROW:
1102 break;
1104 case ERT_TRY:
1106 eh_catch c;
1107 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
1109 lab = c->label;
1110 if (lab)
1111 c->label = main_block_label (lab);
1114 break;
1116 case ERT_ALLOWED_EXCEPTIONS:
1117 lab = r->u.allowed.label;
1118 if (lab)
1119 r->u.allowed.label = main_block_label (lab);
1120 break;
1125 /* Cleanup redundant labels. This is a three-step process:
1126 1) Find the leading label for each block.
1127 2) Redirect all references to labels to the leading labels.
1128 3) Cleanup all useless labels. */
1130 void
1131 cleanup_dead_labels (void)
1133 basic_block bb;
1134 label_for_bb = XCNEWVEC (struct label_record, last_basic_block);
1136 /* Find a suitable label for each block. We use the first user-defined
1137 label if there is one, or otherwise just the first label we see. */
1138 FOR_EACH_BB (bb)
1140 gimple_stmt_iterator i;
1142 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1144 tree label;
1145 gimple stmt = gsi_stmt (i);
1147 if (gimple_code (stmt) != GIMPLE_LABEL)
1148 break;
1150 label = gimple_label_label (stmt);
1152 /* If we have not yet seen a label for the current block,
1153 remember this one and see if there are more labels. */
1154 if (!label_for_bb[bb->index].label)
1156 label_for_bb[bb->index].label = label;
1157 continue;
1160 /* If we did see a label for the current block already, but it
1161 is an artificially created label, replace it if the current
1162 label is a user defined label. */
1163 if (!DECL_ARTIFICIAL (label)
1164 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
1166 label_for_bb[bb->index].label = label;
1167 break;
1172 /* Now redirect all jumps/branches to the selected label.
1173 First do so for each block ending in a control statement. */
1174 FOR_EACH_BB (bb)
1176 gimple stmt = last_stmt (bb);
1177 tree label, new_label;
1179 if (!stmt)
1180 continue;
1182 switch (gimple_code (stmt))
1184 case GIMPLE_COND:
1185 label = gimple_cond_true_label (stmt);
1186 if (label)
1188 new_label = main_block_label (label);
1189 if (new_label != label)
1190 gimple_cond_set_true_label (stmt, new_label);
1193 label = gimple_cond_false_label (stmt);
1194 if (label)
1196 new_label = main_block_label (label);
1197 if (new_label != label)
1198 gimple_cond_set_false_label (stmt, new_label);
1200 break;
1202 case GIMPLE_SWITCH:
1204 size_t i, n = gimple_switch_num_labels (stmt);
1206 /* Replace all destination labels. */
1207 for (i = 0; i < n; ++i)
1209 tree case_label = gimple_switch_label (stmt, i);
1210 label = CASE_LABEL (case_label);
1211 new_label = main_block_label (label);
1212 if (new_label != label)
1213 CASE_LABEL (case_label) = new_label;
1215 break;
1218 case GIMPLE_ASM:
1220 int i, n = gimple_asm_nlabels (stmt);
1222 for (i = 0; i < n; ++i)
1224 tree cons = gimple_asm_label_op (stmt, i);
1225 tree label = main_block_label (TREE_VALUE (cons));
1226 TREE_VALUE (cons) = label;
1228 break;
1231 /* We have to handle gotos until they're removed, and we don't
1232 remove them until after we've created the CFG edges. */
1233 case GIMPLE_GOTO:
1234 if (!computed_goto_p (stmt))
1236 label = gimple_goto_dest (stmt);
1237 new_label = main_block_label (label);
1238 if (new_label != label)
1239 gimple_goto_set_dest (stmt, new_label);
1241 break;
1243 case GIMPLE_TRANSACTION:
1245 tree label = gimple_transaction_label (stmt);
1246 if (label)
1248 tree new_label = main_block_label (label);
1249 if (new_label != label)
1250 gimple_transaction_set_label (stmt, new_label);
1253 break;
1255 default:
1256 break;
1260 /* Do the same for the exception region tree labels. */
1261 cleanup_dead_labels_eh ();
1263 /* Finally, purge dead labels. All user-defined labels and labels that
1264 can be the target of non-local gotos and labels which have their
1265 address taken are preserved. */
1266 FOR_EACH_BB (bb)
1268 gimple_stmt_iterator i;
1269 tree label_for_this_bb = label_for_bb[bb->index].label;
1271 if (!label_for_this_bb)
1272 continue;
1274 /* If the main label of the block is unused, we may still remove it. */
1275 if (!label_for_bb[bb->index].used)
1276 label_for_this_bb = NULL;
1278 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1280 tree label;
1281 gimple stmt = gsi_stmt (i);
1283 if (gimple_code (stmt) != GIMPLE_LABEL)
1284 break;
1286 label = gimple_label_label (stmt);
1288 if (label == label_for_this_bb
1289 || !DECL_ARTIFICIAL (label)
1290 || DECL_NONLOCAL (label)
1291 || FORCED_LABEL (label))
1292 gsi_next (&i);
1293 else
1294 gsi_remove (&i, true);
1298 free (label_for_bb);
1301 /* Scan the sorted vector of cases in STMT (a GIMPLE_SWITCH) and combine
1302 the ones jumping to the same label.
1303 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1305 void
1306 group_case_labels_stmt (gimple stmt)
1308 int old_size = gimple_switch_num_labels (stmt);
1309 int i, j, new_size = old_size;
1310 basic_block default_bb = NULL;
1312 default_bb = label_to_block (CASE_LABEL (gimple_switch_default_label (stmt)));
1314 /* Look for possible opportunities to merge cases. */
1315 i = 1;
1316 while (i < old_size)
1318 tree base_case, base_high;
1319 basic_block base_bb;
1321 base_case = gimple_switch_label (stmt, i);
1323 gcc_assert (base_case);
1324 base_bb = label_to_block (CASE_LABEL (base_case));
1326 /* Discard cases that have the same destination as the
1327 default case. */
1328 if (base_bb == default_bb)
1330 gimple_switch_set_label (stmt, i, NULL_TREE);
1331 i++;
1332 new_size--;
1333 continue;
1336 base_high = CASE_HIGH (base_case)
1337 ? CASE_HIGH (base_case)
1338 : CASE_LOW (base_case);
1339 i++;
1341 /* Try to merge case labels. Break out when we reach the end
1342 of the label vector or when we cannot merge the next case
1343 label with the current one. */
1344 while (i < old_size)
1346 tree merge_case = gimple_switch_label (stmt, i);
1347 basic_block merge_bb = label_to_block (CASE_LABEL (merge_case));
1348 double_int bhp1 = tree_to_double_int (base_high) + double_int_one;
1350 /* Merge the cases if they jump to the same place,
1351 and their ranges are consecutive. */
1352 if (merge_bb == base_bb
1353 && tree_to_double_int (CASE_LOW (merge_case)) == bhp1)
1355 base_high = CASE_HIGH (merge_case) ?
1356 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1357 CASE_HIGH (base_case) = base_high;
1358 gimple_switch_set_label (stmt, i, NULL_TREE);
1359 new_size--;
1360 i++;
1362 else
1363 break;
1367 /* Compress the case labels in the label vector, and adjust the
1368 length of the vector. */
1369 for (i = 0, j = 0; i < new_size; i++)
1371 while (! gimple_switch_label (stmt, j))
1372 j++;
1373 gimple_switch_set_label (stmt, i,
1374 gimple_switch_label (stmt, j++));
1377 gcc_assert (new_size <= old_size);
1378 gimple_switch_set_num_labels (stmt, new_size);
1381 /* Look for blocks ending in a multiway branch (a GIMPLE_SWITCH),
1382 and scan the sorted vector of cases. Combine the ones jumping to the
1383 same label. */
1385 void
1386 group_case_labels (void)
1388 basic_block bb;
1390 FOR_EACH_BB (bb)
1392 gimple stmt = last_stmt (bb);
1393 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1394 group_case_labels_stmt (stmt);
1398 /* Checks whether we can merge block B into block A. */
1400 static bool
1401 gimple_can_merge_blocks_p (basic_block a, basic_block b)
1403 gimple stmt;
1404 gimple_stmt_iterator gsi;
1406 if (!single_succ_p (a))
1407 return false;
1409 if (single_succ_edge (a)->flags & EDGE_COMPLEX)
1410 return false;
1412 if (single_succ (a) != b)
1413 return false;
1415 if (!single_pred_p (b))
1416 return false;
1418 if (b == EXIT_BLOCK_PTR)
1419 return false;
1421 /* If A ends by a statement causing exceptions or something similar, we
1422 cannot merge the blocks. */
1423 stmt = last_stmt (a);
1424 if (stmt && stmt_ends_bb_p (stmt))
1425 return false;
1427 /* Do not allow a block with only a non-local label to be merged. */
1428 if (stmt
1429 && gimple_code (stmt) == GIMPLE_LABEL
1430 && DECL_NONLOCAL (gimple_label_label (stmt)))
1431 return false;
1433 /* Examine the labels at the beginning of B. */
1434 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
1436 tree lab;
1437 stmt = gsi_stmt (gsi);
1438 if (gimple_code (stmt) != GIMPLE_LABEL)
1439 break;
1440 lab = gimple_label_label (stmt);
1442 /* Do not remove user forced labels or for -O0 any user labels. */
1443 if (!DECL_ARTIFICIAL (lab) && (!optimize || FORCED_LABEL (lab)))
1444 return false;
1447 /* Protect the loop latches. */
1448 if (current_loops && b->loop_father->latch == b)
1449 return false;
1451 /* It must be possible to eliminate all phi nodes in B. If ssa form
1452 is not up-to-date and a name-mapping is registered, we cannot eliminate
1453 any phis. Symbols marked for renaming are never a problem though. */
1454 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi); gsi_next (&gsi))
1456 gimple phi = gsi_stmt (gsi);
1457 /* Technically only new names matter. */
1458 if (name_registered_for_update_p (PHI_RESULT (phi)))
1459 return false;
1462 /* When not optimizing, don't merge if we'd lose goto_locus. */
1463 if (!optimize
1464 && single_succ_edge (a)->goto_locus != UNKNOWN_LOCATION)
1466 location_t goto_locus = single_succ_edge (a)->goto_locus;
1467 gimple_stmt_iterator prev, next;
1468 prev = gsi_last_nondebug_bb (a);
1469 next = gsi_after_labels (b);
1470 if (!gsi_end_p (next) && is_gimple_debug (gsi_stmt (next)))
1471 gsi_next_nondebug (&next);
1472 if ((gsi_end_p (prev)
1473 || gimple_location (gsi_stmt (prev)) != goto_locus)
1474 && (gsi_end_p (next)
1475 || gimple_location (gsi_stmt (next)) != goto_locus))
1476 return false;
1479 return true;
1482 /* Return true if the var whose chain of uses starts at PTR has no
1483 nondebug uses. */
1484 bool
1485 has_zero_uses_1 (const ssa_use_operand_t *head)
1487 const ssa_use_operand_t *ptr;
1489 for (ptr = head->next; ptr != head; ptr = ptr->next)
1490 if (!is_gimple_debug (USE_STMT (ptr)))
1491 return false;
1493 return true;
1496 /* Return true if the var whose chain of uses starts at PTR has a
1497 single nondebug use. Set USE_P and STMT to that single nondebug
1498 use, if so, or to NULL otherwise. */
1499 bool
1500 single_imm_use_1 (const ssa_use_operand_t *head,
1501 use_operand_p *use_p, gimple *stmt)
1503 ssa_use_operand_t *ptr, *single_use = 0;
1505 for (ptr = head->next; ptr != head; ptr = ptr->next)
1506 if (!is_gimple_debug (USE_STMT (ptr)))
1508 if (single_use)
1510 single_use = NULL;
1511 break;
1513 single_use = ptr;
1516 if (use_p)
1517 *use_p = single_use;
1519 if (stmt)
1520 *stmt = single_use ? single_use->loc.stmt : NULL;
1522 return !!single_use;
1525 /* Replaces all uses of NAME by VAL. */
1527 void
1528 replace_uses_by (tree name, tree val)
1530 imm_use_iterator imm_iter;
1531 use_operand_p use;
1532 gimple stmt;
1533 edge e;
1535 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1537 /* Mark the block if we change the last stmt in it. */
1538 if (cfgcleanup_altered_bbs
1539 && stmt_ends_bb_p (stmt))
1540 bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1542 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1544 replace_exp (use, val);
1546 if (gimple_code (stmt) == GIMPLE_PHI)
1548 e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
1549 if (e->flags & EDGE_ABNORMAL)
1551 /* This can only occur for virtual operands, since
1552 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1553 would prevent replacement. */
1554 gcc_checking_assert (virtual_operand_p (name));
1555 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1560 if (gimple_code (stmt) != GIMPLE_PHI)
1562 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1563 gimple orig_stmt = stmt;
1564 size_t i;
1566 /* FIXME. It shouldn't be required to keep TREE_CONSTANT
1567 on ADDR_EXPRs up-to-date on GIMPLE. Propagation will
1568 only change sth from non-invariant to invariant, and only
1569 when propagating constants. */
1570 if (is_gimple_min_invariant (val))
1571 for (i = 0; i < gimple_num_ops (stmt); i++)
1573 tree op = gimple_op (stmt, i);
1574 /* Operands may be empty here. For example, the labels
1575 of a GIMPLE_COND are nulled out following the creation
1576 of the corresponding CFG edges. */
1577 if (op && TREE_CODE (op) == ADDR_EXPR)
1578 recompute_tree_invariant_for_addr_expr (op);
1581 if (fold_stmt (&gsi))
1582 stmt = gsi_stmt (gsi);
1584 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1585 gimple_purge_dead_eh_edges (gimple_bb (stmt));
1587 update_stmt (stmt);
1591 gcc_checking_assert (has_zero_uses (name));
1593 /* Also update the trees stored in loop structures. */
1594 if (current_loops)
1596 struct loop *loop;
1597 loop_iterator li;
1599 FOR_EACH_LOOP (li, loop, 0)
1601 substitute_in_loop_info (loop, name, val);
1606 /* Merge block B into block A. */
1608 static void
1609 gimple_merge_blocks (basic_block a, basic_block b)
1611 gimple_stmt_iterator last, gsi, psi;
1613 if (dump_file)
1614 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1616 /* Remove all single-valued PHI nodes from block B of the form
1617 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1618 gsi = gsi_last_bb (a);
1619 for (psi = gsi_start_phis (b); !gsi_end_p (psi); )
1621 gimple phi = gsi_stmt (psi);
1622 tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
1623 gimple copy;
1624 bool may_replace_uses = (virtual_operand_p (def)
1625 || may_propagate_copy (def, use));
1627 /* In case we maintain loop closed ssa form, do not propagate arguments
1628 of loop exit phi nodes. */
1629 if (current_loops
1630 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1631 && !virtual_operand_p (def)
1632 && TREE_CODE (use) == SSA_NAME
1633 && a->loop_father != b->loop_father)
1634 may_replace_uses = false;
1636 if (!may_replace_uses)
1638 gcc_assert (!virtual_operand_p (def));
1640 /* Note that just emitting the copies is fine -- there is no problem
1641 with ordering of phi nodes. This is because A is the single
1642 predecessor of B, therefore results of the phi nodes cannot
1643 appear as arguments of the phi nodes. */
1644 copy = gimple_build_assign (def, use);
1645 gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
1646 remove_phi_node (&psi, false);
1648 else
1650 /* If we deal with a PHI for virtual operands, we can simply
1651 propagate these without fussing with folding or updating
1652 the stmt. */
1653 if (virtual_operand_p (def))
1655 imm_use_iterator iter;
1656 use_operand_p use_p;
1657 gimple stmt;
1659 FOR_EACH_IMM_USE_STMT (stmt, iter, def)
1660 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1661 SET_USE (use_p, use);
1663 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1664 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use) = 1;
1666 else
1667 replace_uses_by (def, use);
1669 remove_phi_node (&psi, true);
1673 /* Ensure that B follows A. */
1674 move_block_after (b, a);
1676 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1677 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1679 /* Remove labels from B and set gimple_bb to A for other statements. */
1680 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
1682 gimple stmt = gsi_stmt (gsi);
1683 if (gimple_code (stmt) == GIMPLE_LABEL)
1685 tree label = gimple_label_label (stmt);
1686 int lp_nr;
1688 gsi_remove (&gsi, false);
1690 /* Now that we can thread computed gotos, we might have
1691 a situation where we have a forced label in block B
1692 However, the label at the start of block B might still be
1693 used in other ways (think about the runtime checking for
1694 Fortran assigned gotos). So we can not just delete the
1695 label. Instead we move the label to the start of block A. */
1696 if (FORCED_LABEL (label))
1698 gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
1699 gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
1701 /* Other user labels keep around in a form of a debug stmt. */
1702 else if (!DECL_ARTIFICIAL (label) && MAY_HAVE_DEBUG_STMTS)
1704 gimple dbg = gimple_build_debug_bind (label,
1705 integer_zero_node,
1706 stmt);
1707 gimple_debug_bind_reset_value (dbg);
1708 gsi_insert_before (&gsi, dbg, GSI_SAME_STMT);
1711 lp_nr = EH_LANDING_PAD_NR (label);
1712 if (lp_nr)
1714 eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
1715 lp->post_landing_pad = NULL;
1718 else
1720 gimple_set_bb (stmt, a);
1721 gsi_next (&gsi);
1725 /* Merge the sequences. */
1726 last = gsi_last_bb (a);
1727 gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
1728 set_bb_seq (b, NULL);
1730 if (cfgcleanup_altered_bbs)
1731 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1735 /* Return the one of two successors of BB that is not reachable by a
1736 complex edge, if there is one. Else, return BB. We use
1737 this in optimizations that use post-dominators for their heuristics,
1738 to catch the cases in C++ where function calls are involved. */
1740 basic_block
1741 single_noncomplex_succ (basic_block bb)
1743 edge e0, e1;
1744 if (EDGE_COUNT (bb->succs) != 2)
1745 return bb;
1747 e0 = EDGE_SUCC (bb, 0);
1748 e1 = EDGE_SUCC (bb, 1);
1749 if (e0->flags & EDGE_COMPLEX)
1750 return e1->dest;
1751 if (e1->flags & EDGE_COMPLEX)
1752 return e0->dest;
1754 return bb;
1757 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1759 void
1760 notice_special_calls (gimple call)
1762 int flags = gimple_call_flags (call);
1764 if (flags & ECF_MAY_BE_ALLOCA)
1765 cfun->calls_alloca = true;
1766 if (flags & ECF_RETURNS_TWICE)
1767 cfun->calls_setjmp = true;
1771 /* Clear flags set by notice_special_calls. Used by dead code removal
1772 to update the flags. */
1774 void
1775 clear_special_calls (void)
1777 cfun->calls_alloca = false;
1778 cfun->calls_setjmp = false;
1781 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1783 static void
1784 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1786 /* Since this block is no longer reachable, we can just delete all
1787 of its PHI nodes. */
1788 remove_phi_nodes (bb);
1790 /* Remove edges to BB's successors. */
1791 while (EDGE_COUNT (bb->succs) > 0)
1792 remove_edge (EDGE_SUCC (bb, 0));
1796 /* Remove statements of basic block BB. */
1798 static void
1799 remove_bb (basic_block bb)
1801 gimple_stmt_iterator i;
1803 if (dump_file)
1805 fprintf (dump_file, "Removing basic block %d\n", bb->index);
1806 if (dump_flags & TDF_DETAILS)
1808 dump_bb (dump_file, bb, 0, dump_flags);
1809 fprintf (dump_file, "\n");
1813 if (current_loops)
1815 struct loop *loop = bb->loop_father;
1817 /* If a loop gets removed, clean up the information associated
1818 with it. */
1819 if (loop->latch == bb
1820 || loop->header == bb)
1821 free_numbers_of_iterations_estimates_loop (loop);
1824 /* Remove all the instructions in the block. */
1825 if (bb_seq (bb) != NULL)
1827 /* Walk backwards so as to get a chance to substitute all
1828 released DEFs into debug stmts. See
1829 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
1830 details. */
1831 for (i = gsi_last_bb (bb); !gsi_end_p (i);)
1833 gimple stmt = gsi_stmt (i);
1834 if (gimple_code (stmt) == GIMPLE_LABEL
1835 && (FORCED_LABEL (gimple_label_label (stmt))
1836 || DECL_NONLOCAL (gimple_label_label (stmt))))
1838 basic_block new_bb;
1839 gimple_stmt_iterator new_gsi;
1841 /* A non-reachable non-local label may still be referenced.
1842 But it no longer needs to carry the extra semantics of
1843 non-locality. */
1844 if (DECL_NONLOCAL (gimple_label_label (stmt)))
1846 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
1847 FORCED_LABEL (gimple_label_label (stmt)) = 1;
1850 new_bb = bb->prev_bb;
1851 new_gsi = gsi_start_bb (new_bb);
1852 gsi_remove (&i, false);
1853 gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
1855 else
1857 /* Release SSA definitions if we are in SSA. Note that we
1858 may be called when not in SSA. For example,
1859 final_cleanup calls this function via
1860 cleanup_tree_cfg. */
1861 if (gimple_in_ssa_p (cfun))
1862 release_defs (stmt);
1864 gsi_remove (&i, true);
1867 if (gsi_end_p (i))
1868 i = gsi_last_bb (bb);
1869 else
1870 gsi_prev (&i);
1874 remove_phi_nodes_and_edges_for_unreachable_block (bb);
1875 bb->il.gimple.seq = NULL;
1876 bb->il.gimple.phi_nodes = NULL;
1880 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
1881 predicate VAL, return the edge that will be taken out of the block.
1882 If VAL does not match a unique edge, NULL is returned. */
1884 edge
1885 find_taken_edge (basic_block bb, tree val)
1887 gimple stmt;
1889 stmt = last_stmt (bb);
1891 gcc_assert (stmt);
1892 gcc_assert (is_ctrl_stmt (stmt));
1894 if (val == NULL)
1895 return NULL;
1897 if (!is_gimple_min_invariant (val))
1898 return NULL;
1900 if (gimple_code (stmt) == GIMPLE_COND)
1901 return find_taken_edge_cond_expr (bb, val);
1903 if (gimple_code (stmt) == GIMPLE_SWITCH)
1904 return find_taken_edge_switch_expr (bb, val);
1906 if (computed_goto_p (stmt))
1908 /* Only optimize if the argument is a label, if the argument is
1909 not a label then we can not construct a proper CFG.
1911 It may be the case that we only need to allow the LABEL_REF to
1912 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
1913 appear inside a LABEL_EXPR just to be safe. */
1914 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
1915 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
1916 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
1917 return NULL;
1920 gcc_unreachable ();
1923 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
1924 statement, determine which of the outgoing edges will be taken out of the
1925 block. Return NULL if either edge may be taken. */
1927 static edge
1928 find_taken_edge_computed_goto (basic_block bb, tree val)
1930 basic_block dest;
1931 edge e = NULL;
1933 dest = label_to_block (val);
1934 if (dest)
1936 e = find_edge (bb, dest);
1937 gcc_assert (e != NULL);
1940 return e;
1943 /* Given a constant value VAL and the entry block BB to a COND_EXPR
1944 statement, determine which of the two edges will be taken out of the
1945 block. Return NULL if either edge may be taken. */
1947 static edge
1948 find_taken_edge_cond_expr (basic_block bb, tree val)
1950 edge true_edge, false_edge;
1952 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1954 gcc_assert (TREE_CODE (val) == INTEGER_CST);
1955 return (integer_zerop (val) ? false_edge : true_edge);
1958 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
1959 statement, determine which edge will be taken out of the block. Return
1960 NULL if any edge may be taken. */
1962 static edge
1963 find_taken_edge_switch_expr (basic_block bb, tree val)
1965 basic_block dest_bb;
1966 edge e;
1967 gimple switch_stmt;
1968 tree taken_case;
1970 switch_stmt = last_stmt (bb);
1971 taken_case = find_case_label_for_value (switch_stmt, val);
1972 dest_bb = label_to_block (CASE_LABEL (taken_case));
1974 e = find_edge (bb, dest_bb);
1975 gcc_assert (e);
1976 return e;
1980 /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
1981 We can make optimal use here of the fact that the case labels are
1982 sorted: We can do a binary search for a case matching VAL. */
1984 static tree
1985 find_case_label_for_value (gimple switch_stmt, tree val)
1987 size_t low, high, n = gimple_switch_num_labels (switch_stmt);
1988 tree default_case = gimple_switch_default_label (switch_stmt);
1990 for (low = 0, high = n; high - low > 1; )
1992 size_t i = (high + low) / 2;
1993 tree t = gimple_switch_label (switch_stmt, i);
1994 int cmp;
1996 /* Cache the result of comparing CASE_LOW and val. */
1997 cmp = tree_int_cst_compare (CASE_LOW (t), val);
1999 if (cmp > 0)
2000 high = i;
2001 else
2002 low = i;
2004 if (CASE_HIGH (t) == NULL)
2006 /* A singe-valued case label. */
2007 if (cmp == 0)
2008 return t;
2010 else
2012 /* A case range. We can only handle integer ranges. */
2013 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2014 return t;
2018 return default_case;
2022 /* Dump a basic block on stderr. */
2024 void
2025 gimple_debug_bb (basic_block bb)
2027 dump_bb (stderr, bb, 0, TDF_VOPS|TDF_MEMSYMS|TDF_BLOCKS);
2031 /* Dump basic block with index N on stderr. */
2033 basic_block
2034 gimple_debug_bb_n (int n)
2036 gimple_debug_bb (BASIC_BLOCK (n));
2037 return BASIC_BLOCK (n);
2041 /* Dump the CFG on stderr.
2043 FLAGS are the same used by the tree dumping functions
2044 (see TDF_* in dumpfile.h). */
2046 void
2047 gimple_debug_cfg (int flags)
2049 gimple_dump_cfg (stderr, flags);
2053 /* Dump the program showing basic block boundaries on the given FILE.
2055 FLAGS are the same used by the tree dumping functions (see TDF_* in
2056 tree.h). */
2058 void
2059 gimple_dump_cfg (FILE *file, int flags)
2061 if (flags & TDF_DETAILS)
2063 dump_function_header (file, current_function_decl, flags);
2064 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2065 n_basic_blocks, n_edges, last_basic_block);
2067 brief_dump_cfg (file, flags | TDF_COMMENT);
2068 fprintf (file, "\n");
2071 if (flags & TDF_STATS)
2072 dump_cfg_stats (file);
2074 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2078 /* Dump CFG statistics on FILE. */
2080 void
2081 dump_cfg_stats (FILE *file)
2083 static long max_num_merged_labels = 0;
2084 unsigned long size, total = 0;
2085 long num_edges;
2086 basic_block bb;
2087 const char * const fmt_str = "%-30s%-13s%12s\n";
2088 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2089 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2090 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2091 const char *funcname = current_function_name ();
2093 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2095 fprintf (file, "---------------------------------------------------------\n");
2096 fprintf (file, fmt_str, "", " Number of ", "Memory");
2097 fprintf (file, fmt_str, "", " instances ", "used ");
2098 fprintf (file, "---------------------------------------------------------\n");
2100 size = n_basic_blocks * sizeof (struct basic_block_def);
2101 total += size;
2102 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2103 SCALE (size), LABEL (size));
2105 num_edges = 0;
2106 FOR_EACH_BB (bb)
2107 num_edges += EDGE_COUNT (bb->succs);
2108 size = num_edges * sizeof (struct edge_def);
2109 total += size;
2110 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2112 fprintf (file, "---------------------------------------------------------\n");
2113 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2114 LABEL (total));
2115 fprintf (file, "---------------------------------------------------------\n");
2116 fprintf (file, "\n");
2118 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2119 max_num_merged_labels = cfg_stats.num_merged_labels;
2121 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2122 cfg_stats.num_merged_labels, max_num_merged_labels);
2124 fprintf (file, "\n");
2128 /* Dump CFG statistics on stderr. Keep extern so that it's always
2129 linked in the final executable. */
2131 DEBUG_FUNCTION void
2132 debug_cfg_stats (void)
2134 dump_cfg_stats (stderr);
2137 /*---------------------------------------------------------------------------
2138 Miscellaneous helpers
2139 ---------------------------------------------------------------------------*/
2141 /* Return true if T, a GIMPLE_CALL, can make an abnormal transfer of control
2142 flow. Transfers of control flow associated with EH are excluded. */
2144 static bool
2145 call_can_make_abnormal_goto (gimple t)
2147 /* If the function has no non-local labels, then a call cannot make an
2148 abnormal transfer of control. */
2149 if (!cfun->has_nonlocal_label)
2150 return false;
2152 /* Likewise if the call has no side effects. */
2153 if (!gimple_has_side_effects (t))
2154 return false;
2156 /* Likewise if the called function is leaf. */
2157 if (gimple_call_flags (t) & ECF_LEAF)
2158 return false;
2160 return true;
2164 /* Return true if T can make an abnormal transfer of control flow.
2165 Transfers of control flow associated with EH are excluded. */
2167 bool
2168 stmt_can_make_abnormal_goto (gimple t)
2170 if (computed_goto_p (t))
2171 return true;
2172 if (is_gimple_call (t))
2173 return call_can_make_abnormal_goto (t);
2174 return false;
2178 /* Return true if T represents a stmt that always transfers control. */
2180 bool
2181 is_ctrl_stmt (gimple t)
2183 switch (gimple_code (t))
2185 case GIMPLE_COND:
2186 case GIMPLE_SWITCH:
2187 case GIMPLE_GOTO:
2188 case GIMPLE_RETURN:
2189 case GIMPLE_RESX:
2190 return true;
2191 default:
2192 return false;
2197 /* Return true if T is a statement that may alter the flow of control
2198 (e.g., a call to a non-returning function). */
2200 bool
2201 is_ctrl_altering_stmt (gimple t)
2203 gcc_assert (t);
2205 switch (gimple_code (t))
2207 case GIMPLE_CALL:
2209 int flags = gimple_call_flags (t);
2211 /* A call alters control flow if it can make an abnormal goto. */
2212 if (call_can_make_abnormal_goto (t))
2213 return true;
2215 /* A call also alters control flow if it does not return. */
2216 if (flags & ECF_NORETURN)
2217 return true;
2219 /* TM ending statements have backedges out of the transaction.
2220 Return true so we split the basic block containing them.
2221 Note that the TM_BUILTIN test is merely an optimization. */
2222 if ((flags & ECF_TM_BUILTIN)
2223 && is_tm_ending_fndecl (gimple_call_fndecl (t)))
2224 return true;
2226 /* BUILT_IN_RETURN call is same as return statement. */
2227 if (gimple_call_builtin_p (t, BUILT_IN_RETURN))
2228 return true;
2230 break;
2232 case GIMPLE_EH_DISPATCH:
2233 /* EH_DISPATCH branches to the individual catch handlers at
2234 this level of a try or allowed-exceptions region. It can
2235 fallthru to the next statement as well. */
2236 return true;
2238 case GIMPLE_ASM:
2239 if (gimple_asm_nlabels (t) > 0)
2240 return true;
2241 break;
2243 CASE_GIMPLE_OMP:
2244 /* OpenMP directives alter control flow. */
2245 return true;
2247 case GIMPLE_TRANSACTION:
2248 /* A transaction start alters control flow. */
2249 return true;
2251 default:
2252 break;
2255 /* If a statement can throw, it alters control flow. */
2256 return stmt_can_throw_internal (t);
2260 /* Return true if T is a simple local goto. */
2262 bool
2263 simple_goto_p (gimple t)
2265 return (gimple_code (t) == GIMPLE_GOTO
2266 && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2270 /* Return true if STMT should start a new basic block. PREV_STMT is
2271 the statement preceding STMT. It is used when STMT is a label or a
2272 case label. Labels should only start a new basic block if their
2273 previous statement wasn't a label. Otherwise, sequence of labels
2274 would generate unnecessary basic blocks that only contain a single
2275 label. */
2277 static inline bool
2278 stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
2280 if (stmt == NULL)
2281 return false;
2283 /* Labels start a new basic block only if the preceding statement
2284 wasn't a label of the same type. This prevents the creation of
2285 consecutive blocks that have nothing but a single label. */
2286 if (gimple_code (stmt) == GIMPLE_LABEL)
2288 /* Nonlocal and computed GOTO targets always start a new block. */
2289 if (DECL_NONLOCAL (gimple_label_label (stmt))
2290 || FORCED_LABEL (gimple_label_label (stmt)))
2291 return true;
2293 if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
2295 if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
2296 return true;
2298 cfg_stats.num_merged_labels++;
2299 return false;
2301 else
2302 return true;
2305 return false;
2309 /* Return true if T should end a basic block. */
2311 bool
2312 stmt_ends_bb_p (gimple t)
2314 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2317 /* Remove block annotations and other data structures. */
2319 void
2320 delete_tree_cfg_annotations (void)
2322 vec_free (label_to_block_map);
2326 /* Return the first statement in basic block BB. */
2328 gimple
2329 first_stmt (basic_block bb)
2331 gimple_stmt_iterator i = gsi_start_bb (bb);
2332 gimple stmt = NULL;
2334 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2336 gsi_next (&i);
2337 stmt = NULL;
2339 return stmt;
2342 /* Return the first non-label statement in basic block BB. */
2344 static gimple
2345 first_non_label_stmt (basic_block bb)
2347 gimple_stmt_iterator i = gsi_start_bb (bb);
2348 while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
2349 gsi_next (&i);
2350 return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
2353 /* Return the last statement in basic block BB. */
2355 gimple
2356 last_stmt (basic_block bb)
2358 gimple_stmt_iterator i = gsi_last_bb (bb);
2359 gimple stmt = NULL;
2361 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2363 gsi_prev (&i);
2364 stmt = NULL;
2366 return stmt;
2369 /* Return the last statement of an otherwise empty block. Return NULL
2370 if the block is totally empty, or if it contains more than one
2371 statement. */
2373 gimple
2374 last_and_only_stmt (basic_block bb)
2376 gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
2377 gimple last, prev;
2379 if (gsi_end_p (i))
2380 return NULL;
2382 last = gsi_stmt (i);
2383 gsi_prev_nondebug (&i);
2384 if (gsi_end_p (i))
2385 return last;
2387 /* Empty statements should no longer appear in the instruction stream.
2388 Everything that might have appeared before should be deleted by
2389 remove_useless_stmts, and the optimizers should just gsi_remove
2390 instead of smashing with build_empty_stmt.
2392 Thus the only thing that should appear here in a block containing
2393 one executable statement is a label. */
2394 prev = gsi_stmt (i);
2395 if (gimple_code (prev) == GIMPLE_LABEL)
2396 return last;
2397 else
2398 return NULL;
2401 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
2403 static void
2404 reinstall_phi_args (edge new_edge, edge old_edge)
2406 edge_var_map_vector *v;
2407 edge_var_map *vm;
2408 int i;
2409 gimple_stmt_iterator phis;
2411 v = redirect_edge_var_map_vector (old_edge);
2412 if (!v)
2413 return;
2415 for (i = 0, phis = gsi_start_phis (new_edge->dest);
2416 v->iterate (i, &vm) && !gsi_end_p (phis);
2417 i++, gsi_next (&phis))
2419 gimple phi = gsi_stmt (phis);
2420 tree result = redirect_edge_var_map_result (vm);
2421 tree arg = redirect_edge_var_map_def (vm);
2423 gcc_assert (result == gimple_phi_result (phi));
2425 add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
2428 redirect_edge_var_map_clear (old_edge);
2431 /* Returns the basic block after which the new basic block created
2432 by splitting edge EDGE_IN should be placed. Tries to keep the new block
2433 near its "logical" location. This is of most help to humans looking
2434 at debugging dumps. */
2436 static basic_block
2437 split_edge_bb_loc (edge edge_in)
2439 basic_block dest = edge_in->dest;
2440 basic_block dest_prev = dest->prev_bb;
2442 if (dest_prev)
2444 edge e = find_edge (dest_prev, dest);
2445 if (e && !(e->flags & EDGE_COMPLEX))
2446 return edge_in->src;
2448 return dest_prev;
2451 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2452 Abort on abnormal edges. */
2454 static basic_block
2455 gimple_split_edge (edge edge_in)
2457 basic_block new_bb, after_bb, dest;
2458 edge new_edge, e;
2460 /* Abnormal edges cannot be split. */
2461 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2463 dest = edge_in->dest;
2465 after_bb = split_edge_bb_loc (edge_in);
2467 new_bb = create_empty_bb (after_bb);
2468 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2469 new_bb->count = edge_in->count;
2470 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2471 new_edge->probability = REG_BR_PROB_BASE;
2472 new_edge->count = edge_in->count;
2474 e = redirect_edge_and_branch (edge_in, new_bb);
2475 gcc_assert (e == edge_in);
2476 reinstall_phi_args (new_edge, e);
2478 return new_bb;
2482 /* Verify properties of the address expression T with base object BASE. */
2484 static tree
2485 verify_address (tree t, tree base)
2487 bool old_constant;
2488 bool old_side_effects;
2489 bool new_constant;
2490 bool new_side_effects;
2492 old_constant = TREE_CONSTANT (t);
2493 old_side_effects = TREE_SIDE_EFFECTS (t);
2495 recompute_tree_invariant_for_addr_expr (t);
2496 new_side_effects = TREE_SIDE_EFFECTS (t);
2497 new_constant = TREE_CONSTANT (t);
2499 if (old_constant != new_constant)
2501 error ("constant not recomputed when ADDR_EXPR changed");
2502 return t;
2504 if (old_side_effects != new_side_effects)
2506 error ("side effects not recomputed when ADDR_EXPR changed");
2507 return t;
2510 if (!(TREE_CODE (base) == VAR_DECL
2511 || TREE_CODE (base) == PARM_DECL
2512 || TREE_CODE (base) == RESULT_DECL))
2513 return NULL_TREE;
2515 if (DECL_GIMPLE_REG_P (base))
2517 error ("DECL_GIMPLE_REG_P set on a variable with address taken");
2518 return base;
2521 return NULL_TREE;
2524 /* Callback for walk_tree, check that all elements with address taken are
2525 properly noticed as such. The DATA is an int* that is 1 if TP was seen
2526 inside a PHI node. */
2528 static tree
2529 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2531 tree t = *tp, x;
2533 if (TYPE_P (t))
2534 *walk_subtrees = 0;
2536 /* Check operand N for being valid GIMPLE and give error MSG if not. */
2537 #define CHECK_OP(N, MSG) \
2538 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
2539 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
2541 switch (TREE_CODE (t))
2543 case SSA_NAME:
2544 if (SSA_NAME_IN_FREE_LIST (t))
2546 error ("SSA name in freelist but still referenced");
2547 return *tp;
2549 break;
2551 case INDIRECT_REF:
2552 error ("INDIRECT_REF in gimple IL");
2553 return t;
2555 case MEM_REF:
2556 x = TREE_OPERAND (t, 0);
2557 if (!POINTER_TYPE_P (TREE_TYPE (x))
2558 || !is_gimple_mem_ref_addr (x))
2560 error ("invalid first operand of MEM_REF");
2561 return x;
2563 if (TREE_CODE (TREE_OPERAND (t, 1)) != INTEGER_CST
2564 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 1))))
2566 error ("invalid offset operand of MEM_REF");
2567 return TREE_OPERAND (t, 1);
2569 if (TREE_CODE (x) == ADDR_EXPR
2570 && (x = verify_address (x, TREE_OPERAND (x, 0))))
2571 return x;
2572 *walk_subtrees = 0;
2573 break;
2575 case ASSERT_EXPR:
2576 x = fold (ASSERT_EXPR_COND (t));
2577 if (x == boolean_false_node)
2579 error ("ASSERT_EXPR with an always-false condition");
2580 return *tp;
2582 break;
2584 case MODIFY_EXPR:
2585 error ("MODIFY_EXPR not expected while having tuples");
2586 return *tp;
2588 case ADDR_EXPR:
2590 tree tem;
2592 gcc_assert (is_gimple_address (t));
2594 /* Skip any references (they will be checked when we recurse down the
2595 tree) and ensure that any variable used as a prefix is marked
2596 addressable. */
2597 for (x = TREE_OPERAND (t, 0);
2598 handled_component_p (x);
2599 x = TREE_OPERAND (x, 0))
2602 if ((tem = verify_address (t, x)))
2603 return tem;
2605 if (!(TREE_CODE (x) == VAR_DECL
2606 || TREE_CODE (x) == PARM_DECL
2607 || TREE_CODE (x) == RESULT_DECL))
2608 return NULL;
2610 if (!TREE_ADDRESSABLE (x))
2612 error ("address taken, but ADDRESSABLE bit not set");
2613 return x;
2616 break;
2619 case COND_EXPR:
2620 x = COND_EXPR_COND (t);
2621 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
2623 error ("non-integral used in condition");
2624 return x;
2626 if (!is_gimple_condexpr (x))
2628 error ("invalid conditional operand");
2629 return x;
2631 break;
2633 case NON_LVALUE_EXPR:
2634 case TRUTH_NOT_EXPR:
2635 gcc_unreachable ();
2637 CASE_CONVERT:
2638 case FIX_TRUNC_EXPR:
2639 case FLOAT_EXPR:
2640 case NEGATE_EXPR:
2641 case ABS_EXPR:
2642 case BIT_NOT_EXPR:
2643 CHECK_OP (0, "invalid operand to unary operator");
2644 break;
2646 case REALPART_EXPR:
2647 case IMAGPART_EXPR:
2648 case COMPONENT_REF:
2649 case ARRAY_REF:
2650 case ARRAY_RANGE_REF:
2651 case BIT_FIELD_REF:
2652 case VIEW_CONVERT_EXPR:
2653 /* We have a nest of references. Verify that each of the operands
2654 that determine where to reference is either a constant or a variable,
2655 verify that the base is valid, and then show we've already checked
2656 the subtrees. */
2657 while (handled_component_p (t))
2659 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
2660 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
2661 else if (TREE_CODE (t) == ARRAY_REF
2662 || TREE_CODE (t) == ARRAY_RANGE_REF)
2664 CHECK_OP (1, "invalid array index");
2665 if (TREE_OPERAND (t, 2))
2666 CHECK_OP (2, "invalid array lower bound");
2667 if (TREE_OPERAND (t, 3))
2668 CHECK_OP (3, "invalid array stride");
2670 else if (TREE_CODE (t) == BIT_FIELD_REF)
2672 if (!host_integerp (TREE_OPERAND (t, 1), 1)
2673 || !host_integerp (TREE_OPERAND (t, 2), 1))
2675 error ("invalid position or size operand to BIT_FIELD_REF");
2676 return t;
2678 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
2679 && (TYPE_PRECISION (TREE_TYPE (t))
2680 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
2682 error ("integral result type precision does not match "
2683 "field size of BIT_FIELD_REF");
2684 return t;
2686 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
2687 && !AGGREGATE_TYPE_P (TREE_TYPE (t))
2688 && TYPE_MODE (TREE_TYPE (t)) != BLKmode
2689 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
2690 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
2692 error ("mode precision of non-integral result does not "
2693 "match field size of BIT_FIELD_REF");
2694 return t;
2698 t = TREE_OPERAND (t, 0);
2701 if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
2703 error ("invalid reference prefix");
2704 return t;
2706 *walk_subtrees = 0;
2707 break;
2708 case PLUS_EXPR:
2709 case MINUS_EXPR:
2710 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
2711 POINTER_PLUS_EXPR. */
2712 if (POINTER_TYPE_P (TREE_TYPE (t)))
2714 error ("invalid operand to plus/minus, type is a pointer");
2715 return t;
2717 CHECK_OP (0, "invalid operand to binary operator");
2718 CHECK_OP (1, "invalid operand to binary operator");
2719 break;
2721 case POINTER_PLUS_EXPR:
2722 /* Check to make sure the first operand is a pointer or reference type. */
2723 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2725 error ("invalid operand to pointer plus, first operand is not a pointer");
2726 return t;
2728 /* Check to make sure the second operand is a ptrofftype. */
2729 if (!ptrofftype_p (TREE_TYPE (TREE_OPERAND (t, 1))))
2731 error ("invalid operand to pointer plus, second operand is not an "
2732 "integer type of appropriate width");
2733 return t;
2735 /* FALLTHROUGH */
2736 case LT_EXPR:
2737 case LE_EXPR:
2738 case GT_EXPR:
2739 case GE_EXPR:
2740 case EQ_EXPR:
2741 case NE_EXPR:
2742 case UNORDERED_EXPR:
2743 case ORDERED_EXPR:
2744 case UNLT_EXPR:
2745 case UNLE_EXPR:
2746 case UNGT_EXPR:
2747 case UNGE_EXPR:
2748 case UNEQ_EXPR:
2749 case LTGT_EXPR:
2750 case MULT_EXPR:
2751 case TRUNC_DIV_EXPR:
2752 case CEIL_DIV_EXPR:
2753 case FLOOR_DIV_EXPR:
2754 case ROUND_DIV_EXPR:
2755 case TRUNC_MOD_EXPR:
2756 case CEIL_MOD_EXPR:
2757 case FLOOR_MOD_EXPR:
2758 case ROUND_MOD_EXPR:
2759 case RDIV_EXPR:
2760 case EXACT_DIV_EXPR:
2761 case MIN_EXPR:
2762 case MAX_EXPR:
2763 case LSHIFT_EXPR:
2764 case RSHIFT_EXPR:
2765 case LROTATE_EXPR:
2766 case RROTATE_EXPR:
2767 case BIT_IOR_EXPR:
2768 case BIT_XOR_EXPR:
2769 case BIT_AND_EXPR:
2770 CHECK_OP (0, "invalid operand to binary operator");
2771 CHECK_OP (1, "invalid operand to binary operator");
2772 break;
2774 case CONSTRUCTOR:
2775 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2776 *walk_subtrees = 0;
2777 break;
2779 case CASE_LABEL_EXPR:
2780 if (CASE_CHAIN (t))
2782 error ("invalid CASE_CHAIN");
2783 return t;
2785 break;
2787 default:
2788 break;
2790 return NULL;
2792 #undef CHECK_OP
2796 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
2797 Returns true if there is an error, otherwise false. */
2799 static bool
2800 verify_types_in_gimple_min_lval (tree expr)
2802 tree op;
2804 if (is_gimple_id (expr))
2805 return false;
2807 if (TREE_CODE (expr) != TARGET_MEM_REF
2808 && TREE_CODE (expr) != MEM_REF)
2810 error ("invalid expression for min lvalue");
2811 return true;
2814 /* TARGET_MEM_REFs are strange beasts. */
2815 if (TREE_CODE (expr) == TARGET_MEM_REF)
2816 return false;
2818 op = TREE_OPERAND (expr, 0);
2819 if (!is_gimple_val (op))
2821 error ("invalid operand in indirect reference");
2822 debug_generic_stmt (op);
2823 return true;
2825 /* Memory references now generally can involve a value conversion. */
2827 return false;
2830 /* Verify if EXPR is a valid GIMPLE reference expression. If
2831 REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
2832 if there is an error, otherwise false. */
2834 static bool
2835 verify_types_in_gimple_reference (tree expr, bool require_lvalue)
2837 while (handled_component_p (expr))
2839 tree op = TREE_OPERAND (expr, 0);
2841 if (TREE_CODE (expr) == ARRAY_REF
2842 || TREE_CODE (expr) == ARRAY_RANGE_REF)
2844 if (!is_gimple_val (TREE_OPERAND (expr, 1))
2845 || (TREE_OPERAND (expr, 2)
2846 && !is_gimple_val (TREE_OPERAND (expr, 2)))
2847 || (TREE_OPERAND (expr, 3)
2848 && !is_gimple_val (TREE_OPERAND (expr, 3))))
2850 error ("invalid operands to array reference");
2851 debug_generic_stmt (expr);
2852 return true;
2856 /* Verify if the reference array element types are compatible. */
2857 if (TREE_CODE (expr) == ARRAY_REF
2858 && !useless_type_conversion_p (TREE_TYPE (expr),
2859 TREE_TYPE (TREE_TYPE (op))))
2861 error ("type mismatch in array reference");
2862 debug_generic_stmt (TREE_TYPE (expr));
2863 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
2864 return true;
2866 if (TREE_CODE (expr) == ARRAY_RANGE_REF
2867 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
2868 TREE_TYPE (TREE_TYPE (op))))
2870 error ("type mismatch in array range reference");
2871 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
2872 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
2873 return true;
2876 if ((TREE_CODE (expr) == REALPART_EXPR
2877 || TREE_CODE (expr) == IMAGPART_EXPR)
2878 && !useless_type_conversion_p (TREE_TYPE (expr),
2879 TREE_TYPE (TREE_TYPE (op))))
2881 error ("type mismatch in real/imagpart reference");
2882 debug_generic_stmt (TREE_TYPE (expr));
2883 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
2884 return true;
2887 if (TREE_CODE (expr) == COMPONENT_REF
2888 && !useless_type_conversion_p (TREE_TYPE (expr),
2889 TREE_TYPE (TREE_OPERAND (expr, 1))))
2891 error ("type mismatch in component reference");
2892 debug_generic_stmt (TREE_TYPE (expr));
2893 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
2894 return true;
2897 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
2899 /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
2900 that their operand is not an SSA name or an invariant when
2901 requiring an lvalue (this usually means there is a SRA or IPA-SRA
2902 bug). Otherwise there is nothing to verify, gross mismatches at
2903 most invoke undefined behavior. */
2904 if (require_lvalue
2905 && (TREE_CODE (op) == SSA_NAME
2906 || is_gimple_min_invariant (op)))
2908 error ("conversion of an SSA_NAME on the left hand side");
2909 debug_generic_stmt (expr);
2910 return true;
2912 else if (TREE_CODE (op) == SSA_NAME
2913 && TYPE_SIZE (TREE_TYPE (expr)) != TYPE_SIZE (TREE_TYPE (op)))
2915 error ("conversion of register to a different size");
2916 debug_generic_stmt (expr);
2917 return true;
2919 else if (!handled_component_p (op))
2920 return false;
2923 expr = op;
2926 if (TREE_CODE (expr) == MEM_REF)
2928 if (!is_gimple_mem_ref_addr (TREE_OPERAND (expr, 0)))
2930 error ("invalid address operand in MEM_REF");
2931 debug_generic_stmt (expr);
2932 return true;
2934 if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
2935 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
2937 error ("invalid offset operand in MEM_REF");
2938 debug_generic_stmt (expr);
2939 return true;
2942 else if (TREE_CODE (expr) == TARGET_MEM_REF)
2944 if (!TMR_BASE (expr)
2945 || !is_gimple_mem_ref_addr (TMR_BASE (expr)))
2947 error ("invalid address operand in TARGET_MEM_REF");
2948 return true;
2950 if (!TMR_OFFSET (expr)
2951 || TREE_CODE (TMR_OFFSET (expr)) != INTEGER_CST
2952 || !POINTER_TYPE_P (TREE_TYPE (TMR_OFFSET (expr))))
2954 error ("invalid offset operand in TARGET_MEM_REF");
2955 debug_generic_stmt (expr);
2956 return true;
2960 return ((require_lvalue || !is_gimple_min_invariant (expr))
2961 && verify_types_in_gimple_min_lval (expr));
2964 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
2965 list of pointer-to types that is trivially convertible to DEST. */
2967 static bool
2968 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
2970 tree src;
2972 if (!TYPE_POINTER_TO (src_obj))
2973 return true;
2975 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
2976 if (useless_type_conversion_p (dest, src))
2977 return true;
2979 return false;
2982 /* Return true if TYPE1 is a fixed-point type and if conversions to and
2983 from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
2985 static bool
2986 valid_fixed_convert_types_p (tree type1, tree type2)
2988 return (FIXED_POINT_TYPE_P (type1)
2989 && (INTEGRAL_TYPE_P (type2)
2990 || SCALAR_FLOAT_TYPE_P (type2)
2991 || FIXED_POINT_TYPE_P (type2)));
2994 /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
2995 is a problem, otherwise false. */
2997 static bool
2998 verify_gimple_call (gimple stmt)
3000 tree fn = gimple_call_fn (stmt);
3001 tree fntype, fndecl;
3002 unsigned i;
3004 if (gimple_call_internal_p (stmt))
3006 if (fn)
3008 error ("gimple call has two targets");
3009 debug_generic_stmt (fn);
3010 return true;
3013 else
3015 if (!fn)
3017 error ("gimple call has no target");
3018 return true;
3022 if (fn && !is_gimple_call_addr (fn))
3024 error ("invalid function in gimple call");
3025 debug_generic_stmt (fn);
3026 return true;
3029 if (fn
3030 && (!POINTER_TYPE_P (TREE_TYPE (fn))
3031 || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
3032 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE)))
3034 error ("non-function in gimple call");
3035 return true;
3038 fndecl = gimple_call_fndecl (stmt);
3039 if (fndecl
3040 && TREE_CODE (fndecl) == FUNCTION_DECL
3041 && DECL_LOOPING_CONST_OR_PURE_P (fndecl)
3042 && !DECL_PURE_P (fndecl)
3043 && !TREE_READONLY (fndecl))
3045 error ("invalid pure const state for function");
3046 return true;
3049 if (gimple_call_lhs (stmt)
3050 && (!is_gimple_lvalue (gimple_call_lhs (stmt))
3051 || verify_types_in_gimple_reference (gimple_call_lhs (stmt), true)))
3053 error ("invalid LHS in gimple call");
3054 return true;
3057 if (gimple_call_lhs (stmt) && gimple_call_noreturn_p (stmt))
3059 error ("LHS in noreturn call");
3060 return true;
3063 fntype = gimple_call_fntype (stmt);
3064 if (fntype
3065 && gimple_call_lhs (stmt)
3066 && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
3067 TREE_TYPE (fntype))
3068 /* ??? At least C++ misses conversions at assignments from
3069 void * call results.
3070 ??? Java is completely off. Especially with functions
3071 returning java.lang.Object.
3072 For now simply allow arbitrary pointer type conversions. */
3073 && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
3074 && POINTER_TYPE_P (TREE_TYPE (fntype))))
3076 error ("invalid conversion in gimple call");
3077 debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
3078 debug_generic_stmt (TREE_TYPE (fntype));
3079 return true;
3082 if (gimple_call_chain (stmt)
3083 && !is_gimple_val (gimple_call_chain (stmt)))
3085 error ("invalid static chain in gimple call");
3086 debug_generic_stmt (gimple_call_chain (stmt));
3087 return true;
3090 /* If there is a static chain argument, this should not be an indirect
3091 call, and the decl should have DECL_STATIC_CHAIN set. */
3092 if (gimple_call_chain (stmt))
3094 if (!gimple_call_fndecl (stmt))
3096 error ("static chain in indirect gimple call");
3097 return true;
3099 fn = TREE_OPERAND (fn, 0);
3101 if (!DECL_STATIC_CHAIN (fn))
3103 error ("static chain with function that doesn%'t use one");
3104 return true;
3108 /* ??? The C frontend passes unpromoted arguments in case it
3109 didn't see a function declaration before the call. So for now
3110 leave the call arguments mostly unverified. Once we gimplify
3111 unit-at-a-time we have a chance to fix this. */
3113 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3115 tree arg = gimple_call_arg (stmt, i);
3116 if ((is_gimple_reg_type (TREE_TYPE (arg))
3117 && !is_gimple_val (arg))
3118 || (!is_gimple_reg_type (TREE_TYPE (arg))
3119 && !is_gimple_lvalue (arg)))
3121 error ("invalid argument to gimple call");
3122 debug_generic_expr (arg);
3123 return true;
3127 return false;
3130 /* Verifies the gimple comparison with the result type TYPE and
3131 the operands OP0 and OP1. */
3133 static bool
3134 verify_gimple_comparison (tree type, tree op0, tree op1)
3136 tree op0_type = TREE_TYPE (op0);
3137 tree op1_type = TREE_TYPE (op1);
3139 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3141 error ("invalid operands in gimple comparison");
3142 return true;
3145 /* For comparisons we do not have the operations type as the
3146 effective type the comparison is carried out in. Instead
3147 we require that either the first operand is trivially
3148 convertible into the second, or the other way around.
3149 Because we special-case pointers to void we allow
3150 comparisons of pointers with the same mode as well. */
3151 if (!useless_type_conversion_p (op0_type, op1_type)
3152 && !useless_type_conversion_p (op1_type, op0_type)
3153 && (!POINTER_TYPE_P (op0_type)
3154 || !POINTER_TYPE_P (op1_type)
3155 || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
3157 error ("mismatching comparison operand types");
3158 debug_generic_expr (op0_type);
3159 debug_generic_expr (op1_type);
3160 return true;
3163 /* The resulting type of a comparison may be an effective boolean type. */
3164 if (INTEGRAL_TYPE_P (type)
3165 && (TREE_CODE (type) == BOOLEAN_TYPE
3166 || TYPE_PRECISION (type) == 1))
3168 if (TREE_CODE (op0_type) == VECTOR_TYPE
3169 || TREE_CODE (op1_type) == VECTOR_TYPE)
3171 error ("vector comparison returning a boolean");
3172 debug_generic_expr (op0_type);
3173 debug_generic_expr (op1_type);
3174 return true;
3177 /* Or an integer vector type with the same size and element count
3178 as the comparison operand types. */
3179 else if (TREE_CODE (type) == VECTOR_TYPE
3180 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
3182 if (TREE_CODE (op0_type) != VECTOR_TYPE
3183 || TREE_CODE (op1_type) != VECTOR_TYPE)
3185 error ("non-vector operands in vector comparison");
3186 debug_generic_expr (op0_type);
3187 debug_generic_expr (op1_type);
3188 return true;
3191 if (TYPE_VECTOR_SUBPARTS (type) != TYPE_VECTOR_SUBPARTS (op0_type)
3192 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)))
3193 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0_type)))))
3195 error ("invalid vector comparison resulting type");
3196 debug_generic_expr (type);
3197 return true;
3200 else
3202 error ("bogus comparison result type");
3203 debug_generic_expr (type);
3204 return true;
3207 return false;
3210 /* Verify a gimple assignment statement STMT with an unary rhs.
3211 Returns true if anything is wrong. */
3213 static bool
3214 verify_gimple_assign_unary (gimple stmt)
3216 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3217 tree lhs = gimple_assign_lhs (stmt);
3218 tree lhs_type = TREE_TYPE (lhs);
3219 tree rhs1 = gimple_assign_rhs1 (stmt);
3220 tree rhs1_type = TREE_TYPE (rhs1);
3222 if (!is_gimple_reg (lhs))
3224 error ("non-register as LHS of unary operation");
3225 return true;
3228 if (!is_gimple_val (rhs1))
3230 error ("invalid operand in unary operation");
3231 return true;
3234 /* First handle conversions. */
3235 switch (rhs_code)
3237 CASE_CONVERT:
3239 /* Allow conversions from pointer type to integral type only if
3240 there is no sign or zero extension involved.
3241 For targets were the precision of ptrofftype doesn't match that
3242 of pointers we need to allow arbitrary conversions to ptrofftype. */
3243 if ((POINTER_TYPE_P (lhs_type)
3244 && INTEGRAL_TYPE_P (rhs1_type))
3245 || (POINTER_TYPE_P (rhs1_type)
3246 && INTEGRAL_TYPE_P (lhs_type)
3247 && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
3248 || ptrofftype_p (sizetype))))
3249 return false;
3251 /* Allow conversion from integral to offset type and vice versa. */
3252 if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3253 && INTEGRAL_TYPE_P (rhs1_type))
3254 || (INTEGRAL_TYPE_P (lhs_type)
3255 && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3256 return false;
3258 /* Otherwise assert we are converting between types of the
3259 same kind. */
3260 if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3262 error ("invalid types in nop conversion");
3263 debug_generic_expr (lhs_type);
3264 debug_generic_expr (rhs1_type);
3265 return true;
3268 return false;
3271 case ADDR_SPACE_CONVERT_EXPR:
3273 if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
3274 || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
3275 == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
3277 error ("invalid types in address space conversion");
3278 debug_generic_expr (lhs_type);
3279 debug_generic_expr (rhs1_type);
3280 return true;
3283 return false;
3286 case FIXED_CONVERT_EXPR:
3288 if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3289 && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3291 error ("invalid types in fixed-point conversion");
3292 debug_generic_expr (lhs_type);
3293 debug_generic_expr (rhs1_type);
3294 return true;
3297 return false;
3300 case FLOAT_EXPR:
3302 if ((!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3303 && (!VECTOR_INTEGER_TYPE_P (rhs1_type)
3304 || !VECTOR_FLOAT_TYPE_P(lhs_type)))
3306 error ("invalid types in conversion to floating point");
3307 debug_generic_expr (lhs_type);
3308 debug_generic_expr (rhs1_type);
3309 return true;
3312 return false;
3315 case FIX_TRUNC_EXPR:
3317 if ((!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3318 && (!VECTOR_INTEGER_TYPE_P (lhs_type)
3319 || !VECTOR_FLOAT_TYPE_P(rhs1_type)))
3321 error ("invalid types in conversion to integer");
3322 debug_generic_expr (lhs_type);
3323 debug_generic_expr (rhs1_type);
3324 return true;
3327 return false;
3330 case VEC_UNPACK_HI_EXPR:
3331 case VEC_UNPACK_LO_EXPR:
3332 case REDUC_MAX_EXPR:
3333 case REDUC_MIN_EXPR:
3334 case REDUC_PLUS_EXPR:
3335 case VEC_UNPACK_FLOAT_HI_EXPR:
3336 case VEC_UNPACK_FLOAT_LO_EXPR:
3337 /* FIXME. */
3338 return false;
3340 case NEGATE_EXPR:
3341 case ABS_EXPR:
3342 case BIT_NOT_EXPR:
3343 case PAREN_EXPR:
3344 case NON_LVALUE_EXPR:
3345 case CONJ_EXPR:
3346 break;
3348 default:
3349 gcc_unreachable ();
3352 /* For the remaining codes assert there is no conversion involved. */
3353 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3355 error ("non-trivial conversion in unary operation");
3356 debug_generic_expr (lhs_type);
3357 debug_generic_expr (rhs1_type);
3358 return true;
3361 return false;
3364 /* Verify a gimple assignment statement STMT with a binary rhs.
3365 Returns true if anything is wrong. */
3367 static bool
3368 verify_gimple_assign_binary (gimple stmt)
3370 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3371 tree lhs = gimple_assign_lhs (stmt);
3372 tree lhs_type = TREE_TYPE (lhs);
3373 tree rhs1 = gimple_assign_rhs1 (stmt);
3374 tree rhs1_type = TREE_TYPE (rhs1);
3375 tree rhs2 = gimple_assign_rhs2 (stmt);
3376 tree rhs2_type = TREE_TYPE (rhs2);
3378 if (!is_gimple_reg (lhs))
3380 error ("non-register as LHS of binary operation");
3381 return true;
3384 if (!is_gimple_val (rhs1)
3385 || !is_gimple_val (rhs2))
3387 error ("invalid operands in binary operation");
3388 return true;
3391 /* First handle operations that involve different types. */
3392 switch (rhs_code)
3394 case COMPLEX_EXPR:
3396 if (TREE_CODE (lhs_type) != COMPLEX_TYPE
3397 || !(INTEGRAL_TYPE_P (rhs1_type)
3398 || SCALAR_FLOAT_TYPE_P (rhs1_type))
3399 || !(INTEGRAL_TYPE_P (rhs2_type)
3400 || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3402 error ("type mismatch in complex expression");
3403 debug_generic_expr (lhs_type);
3404 debug_generic_expr (rhs1_type);
3405 debug_generic_expr (rhs2_type);
3406 return true;
3409 return false;
3412 case LSHIFT_EXPR:
3413 case RSHIFT_EXPR:
3414 case LROTATE_EXPR:
3415 case RROTATE_EXPR:
3417 /* Shifts and rotates are ok on integral types, fixed point
3418 types and integer vector types. */
3419 if ((!INTEGRAL_TYPE_P (rhs1_type)
3420 && !FIXED_POINT_TYPE_P (rhs1_type)
3421 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3422 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
3423 || (!INTEGRAL_TYPE_P (rhs2_type)
3424 /* Vector shifts of vectors are also ok. */
3425 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3426 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3427 && TREE_CODE (rhs2_type) == VECTOR_TYPE
3428 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3429 || !useless_type_conversion_p (lhs_type, rhs1_type))
3431 error ("type mismatch in shift expression");
3432 debug_generic_expr (lhs_type);
3433 debug_generic_expr (rhs1_type);
3434 debug_generic_expr (rhs2_type);
3435 return true;
3438 return false;
3441 case VEC_LSHIFT_EXPR:
3442 case VEC_RSHIFT_EXPR:
3444 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3445 || !(INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3446 || POINTER_TYPE_P (TREE_TYPE (rhs1_type))
3447 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1_type))
3448 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
3449 || (!INTEGRAL_TYPE_P (rhs2_type)
3450 && (TREE_CODE (rhs2_type) != VECTOR_TYPE
3451 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3452 || !useless_type_conversion_p (lhs_type, rhs1_type))
3454 error ("type mismatch in vector shift expression");
3455 debug_generic_expr (lhs_type);
3456 debug_generic_expr (rhs1_type);
3457 debug_generic_expr (rhs2_type);
3458 return true;
3460 /* For shifting a vector of non-integral components we
3461 only allow shifting by a constant multiple of the element size. */
3462 if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3463 && (TREE_CODE (rhs2) != INTEGER_CST
3464 || !div_if_zero_remainder (EXACT_DIV_EXPR, rhs2,
3465 TYPE_SIZE (TREE_TYPE (rhs1_type)))))
3467 error ("non-element sized vector shift of floating point vector");
3468 return true;
3471 return false;
3474 case WIDEN_LSHIFT_EXPR:
3476 if (!INTEGRAL_TYPE_P (lhs_type)
3477 || !INTEGRAL_TYPE_P (rhs1_type)
3478 || TREE_CODE (rhs2) != INTEGER_CST
3479 || (2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)))
3481 error ("type mismatch in widening vector shift expression");
3482 debug_generic_expr (lhs_type);
3483 debug_generic_expr (rhs1_type);
3484 debug_generic_expr (rhs2_type);
3485 return true;
3488 return false;
3491 case VEC_WIDEN_LSHIFT_HI_EXPR:
3492 case VEC_WIDEN_LSHIFT_LO_EXPR:
3494 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3495 || TREE_CODE (lhs_type) != VECTOR_TYPE
3496 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3497 || !INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3498 || TREE_CODE (rhs2) != INTEGER_CST
3499 || (2 * TYPE_PRECISION (TREE_TYPE (rhs1_type))
3500 > TYPE_PRECISION (TREE_TYPE (lhs_type))))
3502 error ("type mismatch in widening vector shift expression");
3503 debug_generic_expr (lhs_type);
3504 debug_generic_expr (rhs1_type);
3505 debug_generic_expr (rhs2_type);
3506 return true;
3509 return false;
3512 case PLUS_EXPR:
3513 case MINUS_EXPR:
3515 tree lhs_etype = lhs_type;
3516 tree rhs1_etype = rhs1_type;
3517 tree rhs2_etype = rhs2_type;
3518 if (TREE_CODE (lhs_type) == VECTOR_TYPE)
3520 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3521 || TREE_CODE (rhs2_type) != VECTOR_TYPE)
3523 error ("invalid non-vector operands to vector valued plus");
3524 return true;
3526 lhs_etype = TREE_TYPE (lhs_type);
3527 rhs1_etype = TREE_TYPE (rhs1_type);
3528 rhs2_etype = TREE_TYPE (rhs2_type);
3530 if (POINTER_TYPE_P (lhs_etype)
3531 || POINTER_TYPE_P (rhs1_etype)
3532 || POINTER_TYPE_P (rhs2_etype))
3534 error ("invalid (pointer) operands to plus/minus");
3535 return true;
3538 /* Continue with generic binary expression handling. */
3539 break;
3542 case POINTER_PLUS_EXPR:
3544 if (!POINTER_TYPE_P (rhs1_type)
3545 || !useless_type_conversion_p (lhs_type, rhs1_type)
3546 || !ptrofftype_p (rhs2_type))
3548 error ("type mismatch in pointer plus expression");
3549 debug_generic_stmt (lhs_type);
3550 debug_generic_stmt (rhs1_type);
3551 debug_generic_stmt (rhs2_type);
3552 return true;
3555 return false;
3558 case TRUTH_ANDIF_EXPR:
3559 case TRUTH_ORIF_EXPR:
3560 case TRUTH_AND_EXPR:
3561 case TRUTH_OR_EXPR:
3562 case TRUTH_XOR_EXPR:
3564 gcc_unreachable ();
3566 case LT_EXPR:
3567 case LE_EXPR:
3568 case GT_EXPR:
3569 case GE_EXPR:
3570 case EQ_EXPR:
3571 case NE_EXPR:
3572 case UNORDERED_EXPR:
3573 case ORDERED_EXPR:
3574 case UNLT_EXPR:
3575 case UNLE_EXPR:
3576 case UNGT_EXPR:
3577 case UNGE_EXPR:
3578 case UNEQ_EXPR:
3579 case LTGT_EXPR:
3580 /* Comparisons are also binary, but the result type is not
3581 connected to the operand types. */
3582 return verify_gimple_comparison (lhs_type, rhs1, rhs2);
3584 case WIDEN_MULT_EXPR:
3585 if (TREE_CODE (lhs_type) != INTEGER_TYPE)
3586 return true;
3587 return ((2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type))
3588 || (TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type)));
3590 case WIDEN_SUM_EXPR:
3591 case VEC_WIDEN_MULT_HI_EXPR:
3592 case VEC_WIDEN_MULT_LO_EXPR:
3593 case VEC_WIDEN_MULT_EVEN_EXPR:
3594 case VEC_WIDEN_MULT_ODD_EXPR:
3595 case VEC_PACK_TRUNC_EXPR:
3596 case VEC_PACK_SAT_EXPR:
3597 case VEC_PACK_FIX_TRUNC_EXPR:
3598 /* FIXME. */
3599 return false;
3601 case MULT_EXPR:
3602 case MULT_HIGHPART_EXPR:
3603 case TRUNC_DIV_EXPR:
3604 case CEIL_DIV_EXPR:
3605 case FLOOR_DIV_EXPR:
3606 case ROUND_DIV_EXPR:
3607 case TRUNC_MOD_EXPR:
3608 case CEIL_MOD_EXPR:
3609 case FLOOR_MOD_EXPR:
3610 case ROUND_MOD_EXPR:
3611 case RDIV_EXPR:
3612 case EXACT_DIV_EXPR:
3613 case MIN_EXPR:
3614 case MAX_EXPR:
3615 case BIT_IOR_EXPR:
3616 case BIT_XOR_EXPR:
3617 case BIT_AND_EXPR:
3618 /* Continue with generic binary expression handling. */
3619 break;
3621 default:
3622 gcc_unreachable ();
3625 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3626 || !useless_type_conversion_p (lhs_type, rhs2_type))
3628 error ("type mismatch in binary expression");
3629 debug_generic_stmt (lhs_type);
3630 debug_generic_stmt (rhs1_type);
3631 debug_generic_stmt (rhs2_type);
3632 return true;
3635 return false;
3638 /* Verify a gimple assignment statement STMT with a ternary rhs.
3639 Returns true if anything is wrong. */
3641 static bool
3642 verify_gimple_assign_ternary (gimple stmt)
3644 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3645 tree lhs = gimple_assign_lhs (stmt);
3646 tree lhs_type = TREE_TYPE (lhs);
3647 tree rhs1 = gimple_assign_rhs1 (stmt);
3648 tree rhs1_type = TREE_TYPE (rhs1);
3649 tree rhs2 = gimple_assign_rhs2 (stmt);
3650 tree rhs2_type = TREE_TYPE (rhs2);
3651 tree rhs3 = gimple_assign_rhs3 (stmt);
3652 tree rhs3_type = TREE_TYPE (rhs3);
3654 if (!is_gimple_reg (lhs))
3656 error ("non-register as LHS of ternary operation");
3657 return true;
3660 if (((rhs_code == VEC_COND_EXPR || rhs_code == COND_EXPR)
3661 ? !is_gimple_condexpr (rhs1) : !is_gimple_val (rhs1))
3662 || !is_gimple_val (rhs2)
3663 || !is_gimple_val (rhs3))
3665 error ("invalid operands in ternary operation");
3666 return true;
3669 /* First handle operations that involve different types. */
3670 switch (rhs_code)
3672 case WIDEN_MULT_PLUS_EXPR:
3673 case WIDEN_MULT_MINUS_EXPR:
3674 if ((!INTEGRAL_TYPE_P (rhs1_type)
3675 && !FIXED_POINT_TYPE_P (rhs1_type))
3676 || !useless_type_conversion_p (rhs1_type, rhs2_type)
3677 || !useless_type_conversion_p (lhs_type, rhs3_type)
3678 || 2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)
3679 || TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type))
3681 error ("type mismatch in widening multiply-accumulate expression");
3682 debug_generic_expr (lhs_type);
3683 debug_generic_expr (rhs1_type);
3684 debug_generic_expr (rhs2_type);
3685 debug_generic_expr (rhs3_type);
3686 return true;
3688 break;
3690 case FMA_EXPR:
3691 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3692 || !useless_type_conversion_p (lhs_type, rhs2_type)
3693 || !useless_type_conversion_p (lhs_type, rhs3_type))
3695 error ("type mismatch in fused multiply-add expression");
3696 debug_generic_expr (lhs_type);
3697 debug_generic_expr (rhs1_type);
3698 debug_generic_expr (rhs2_type);
3699 debug_generic_expr (rhs3_type);
3700 return true;
3702 break;
3704 case COND_EXPR:
3705 case VEC_COND_EXPR:
3706 if (!useless_type_conversion_p (lhs_type, rhs2_type)
3707 || !useless_type_conversion_p (lhs_type, rhs3_type))
3709 error ("type mismatch in conditional expression");
3710 debug_generic_expr (lhs_type);
3711 debug_generic_expr (rhs2_type);
3712 debug_generic_expr (rhs3_type);
3713 return true;
3715 break;
3717 case VEC_PERM_EXPR:
3718 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3719 || !useless_type_conversion_p (lhs_type, rhs2_type))
3721 error ("type mismatch in vector permute expression");
3722 debug_generic_expr (lhs_type);
3723 debug_generic_expr (rhs1_type);
3724 debug_generic_expr (rhs2_type);
3725 debug_generic_expr (rhs3_type);
3726 return true;
3729 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3730 || TREE_CODE (rhs2_type) != VECTOR_TYPE
3731 || TREE_CODE (rhs3_type) != VECTOR_TYPE)
3733 error ("vector types expected in vector permute expression");
3734 debug_generic_expr (lhs_type);
3735 debug_generic_expr (rhs1_type);
3736 debug_generic_expr (rhs2_type);
3737 debug_generic_expr (rhs3_type);
3738 return true;
3741 if (TYPE_VECTOR_SUBPARTS (rhs1_type) != TYPE_VECTOR_SUBPARTS (rhs2_type)
3742 || TYPE_VECTOR_SUBPARTS (rhs2_type)
3743 != TYPE_VECTOR_SUBPARTS (rhs3_type)
3744 || TYPE_VECTOR_SUBPARTS (rhs3_type)
3745 != TYPE_VECTOR_SUBPARTS (lhs_type))
3747 error ("vectors with different element number found "
3748 "in vector permute expression");
3749 debug_generic_expr (lhs_type);
3750 debug_generic_expr (rhs1_type);
3751 debug_generic_expr (rhs2_type);
3752 debug_generic_expr (rhs3_type);
3753 return true;
3756 if (TREE_CODE (TREE_TYPE (rhs3_type)) != INTEGER_TYPE
3757 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs3_type)))
3758 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1_type))))
3760 error ("invalid mask type in vector permute expression");
3761 debug_generic_expr (lhs_type);
3762 debug_generic_expr (rhs1_type);
3763 debug_generic_expr (rhs2_type);
3764 debug_generic_expr (rhs3_type);
3765 return true;
3768 return false;
3770 case DOT_PROD_EXPR:
3771 case REALIGN_LOAD_EXPR:
3772 /* FIXME. */
3773 return false;
3775 default:
3776 gcc_unreachable ();
3778 return false;
3781 /* Verify a gimple assignment statement STMT with a single rhs.
3782 Returns true if anything is wrong. */
3784 static bool
3785 verify_gimple_assign_single (gimple stmt)
3787 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3788 tree lhs = gimple_assign_lhs (stmt);
3789 tree lhs_type = TREE_TYPE (lhs);
3790 tree rhs1 = gimple_assign_rhs1 (stmt);
3791 tree rhs1_type = TREE_TYPE (rhs1);
3792 bool res = false;
3794 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3796 error ("non-trivial conversion at assignment");
3797 debug_generic_expr (lhs_type);
3798 debug_generic_expr (rhs1_type);
3799 return true;
3802 if (gimple_clobber_p (stmt)
3803 && !DECL_P (lhs))
3805 error ("non-decl LHS in clobber statement");
3806 debug_generic_expr (lhs);
3807 return true;
3810 if (handled_component_p (lhs))
3811 res |= verify_types_in_gimple_reference (lhs, true);
3813 /* Special codes we cannot handle via their class. */
3814 switch (rhs_code)
3816 case ADDR_EXPR:
3818 tree op = TREE_OPERAND (rhs1, 0);
3819 if (!is_gimple_addressable (op))
3821 error ("invalid operand in unary expression");
3822 return true;
3825 /* Technically there is no longer a need for matching types, but
3826 gimple hygiene asks for this check. In LTO we can end up
3827 combining incompatible units and thus end up with addresses
3828 of globals that change their type to a common one. */
3829 if (!in_lto_p
3830 && !types_compatible_p (TREE_TYPE (op),
3831 TREE_TYPE (TREE_TYPE (rhs1)))
3832 && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
3833 TREE_TYPE (op)))
3835 error ("type mismatch in address expression");
3836 debug_generic_stmt (TREE_TYPE (rhs1));
3837 debug_generic_stmt (TREE_TYPE (op));
3838 return true;
3841 return verify_types_in_gimple_reference (op, true);
3844 /* tcc_reference */
3845 case INDIRECT_REF:
3846 error ("INDIRECT_REF in gimple IL");
3847 return true;
3849 case COMPONENT_REF:
3850 case BIT_FIELD_REF:
3851 case ARRAY_REF:
3852 case ARRAY_RANGE_REF:
3853 case VIEW_CONVERT_EXPR:
3854 case REALPART_EXPR:
3855 case IMAGPART_EXPR:
3856 case TARGET_MEM_REF:
3857 case MEM_REF:
3858 if (!is_gimple_reg (lhs)
3859 && is_gimple_reg_type (TREE_TYPE (lhs)))
3861 error ("invalid rhs for gimple memory store");
3862 debug_generic_stmt (lhs);
3863 debug_generic_stmt (rhs1);
3864 return true;
3866 return res || verify_types_in_gimple_reference (rhs1, false);
3868 /* tcc_constant */
3869 case SSA_NAME:
3870 case INTEGER_CST:
3871 case REAL_CST:
3872 case FIXED_CST:
3873 case COMPLEX_CST:
3874 case VECTOR_CST:
3875 case STRING_CST:
3876 return res;
3878 /* tcc_declaration */
3879 case CONST_DECL:
3880 return res;
3881 case VAR_DECL:
3882 case PARM_DECL:
3883 if (!is_gimple_reg (lhs)
3884 && !is_gimple_reg (rhs1)
3885 && is_gimple_reg_type (TREE_TYPE (lhs)))
3887 error ("invalid rhs for gimple memory store");
3888 debug_generic_stmt (lhs);
3889 debug_generic_stmt (rhs1);
3890 return true;
3892 return res;
3894 case CONSTRUCTOR:
3895 if (TREE_CODE (rhs1_type) == VECTOR_TYPE)
3897 unsigned int i;
3898 tree elt_i, elt_v, elt_t = NULL_TREE;
3900 if (CONSTRUCTOR_NELTS (rhs1) == 0)
3901 return res;
3902 /* For vector CONSTRUCTORs we require that either it is empty
3903 CONSTRUCTOR, or it is a CONSTRUCTOR of smaller vector elements
3904 (then the element count must be correct to cover the whole
3905 outer vector and index must be NULL on all elements, or it is
3906 a CONSTRUCTOR of scalar elements, where we as an exception allow
3907 smaller number of elements (assuming zero filling) and
3908 consecutive indexes as compared to NULL indexes (such
3909 CONSTRUCTORs can appear in the IL from FEs). */
3910 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), i, elt_i, elt_v)
3912 if (elt_t == NULL_TREE)
3914 elt_t = TREE_TYPE (elt_v);
3915 if (TREE_CODE (elt_t) == VECTOR_TYPE)
3917 tree elt_t = TREE_TYPE (elt_v);
3918 if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
3919 TREE_TYPE (elt_t)))
3921 error ("incorrect type of vector CONSTRUCTOR"
3922 " elements");
3923 debug_generic_stmt (rhs1);
3924 return true;
3926 else if (CONSTRUCTOR_NELTS (rhs1)
3927 * TYPE_VECTOR_SUBPARTS (elt_t)
3928 != TYPE_VECTOR_SUBPARTS (rhs1_type))
3930 error ("incorrect number of vector CONSTRUCTOR"
3931 " elements");
3932 debug_generic_stmt (rhs1);
3933 return true;
3936 else if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
3937 elt_t))
3939 error ("incorrect type of vector CONSTRUCTOR elements");
3940 debug_generic_stmt (rhs1);
3941 return true;
3943 else if (CONSTRUCTOR_NELTS (rhs1)
3944 > TYPE_VECTOR_SUBPARTS (rhs1_type))
3946 error ("incorrect number of vector CONSTRUCTOR elements");
3947 debug_generic_stmt (rhs1);
3948 return true;
3951 else if (!useless_type_conversion_p (elt_t, TREE_TYPE (elt_v)))
3953 error ("incorrect type of vector CONSTRUCTOR elements");
3954 debug_generic_stmt (rhs1);
3955 return true;
3957 if (elt_i != NULL_TREE
3958 && (TREE_CODE (elt_t) == VECTOR_TYPE
3959 || TREE_CODE (elt_i) != INTEGER_CST
3960 || compare_tree_int (elt_i, i) != 0))
3962 error ("vector CONSTRUCTOR with non-NULL element index");
3963 debug_generic_stmt (rhs1);
3964 return true;
3968 return res;
3969 case OBJ_TYPE_REF:
3970 case ASSERT_EXPR:
3971 case WITH_SIZE_EXPR:
3972 /* FIXME. */
3973 return res;
3975 default:;
3978 return res;
3981 /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
3982 is a problem, otherwise false. */
3984 static bool
3985 verify_gimple_assign (gimple stmt)
3987 switch (gimple_assign_rhs_class (stmt))
3989 case GIMPLE_SINGLE_RHS:
3990 return verify_gimple_assign_single (stmt);
3992 case GIMPLE_UNARY_RHS:
3993 return verify_gimple_assign_unary (stmt);
3995 case GIMPLE_BINARY_RHS:
3996 return verify_gimple_assign_binary (stmt);
3998 case GIMPLE_TERNARY_RHS:
3999 return verify_gimple_assign_ternary (stmt);
4001 default:
4002 gcc_unreachable ();
4006 /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
4007 is a problem, otherwise false. */
4009 static bool
4010 verify_gimple_return (gimple stmt)
4012 tree op = gimple_return_retval (stmt);
4013 tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
4015 /* We cannot test for present return values as we do not fix up missing
4016 return values from the original source. */
4017 if (op == NULL)
4018 return false;
4020 if (!is_gimple_val (op)
4021 && TREE_CODE (op) != RESULT_DECL)
4023 error ("invalid operand in return statement");
4024 debug_generic_stmt (op);
4025 return true;
4028 if ((TREE_CODE (op) == RESULT_DECL
4029 && DECL_BY_REFERENCE (op))
4030 || (TREE_CODE (op) == SSA_NAME
4031 && SSA_NAME_VAR (op)
4032 && TREE_CODE (SSA_NAME_VAR (op)) == RESULT_DECL
4033 && DECL_BY_REFERENCE (SSA_NAME_VAR (op))))
4034 op = TREE_TYPE (op);
4036 if (!useless_type_conversion_p (restype, TREE_TYPE (op)))
4038 error ("invalid conversion in return statement");
4039 debug_generic_stmt (restype);
4040 debug_generic_stmt (TREE_TYPE (op));
4041 return true;
4044 return false;
4048 /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
4049 is a problem, otherwise false. */
4051 static bool
4052 verify_gimple_goto (gimple stmt)
4054 tree dest = gimple_goto_dest (stmt);
4056 /* ??? We have two canonical forms of direct goto destinations, a
4057 bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
4058 if (TREE_CODE (dest) != LABEL_DECL
4059 && (!is_gimple_val (dest)
4060 || !POINTER_TYPE_P (TREE_TYPE (dest))))
4062 error ("goto destination is neither a label nor a pointer");
4063 return true;
4066 return false;
4069 /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
4070 is a problem, otherwise false. */
4072 static bool
4073 verify_gimple_switch (gimple stmt)
4075 unsigned int i, n;
4076 tree elt, prev_upper_bound = NULL_TREE;
4077 tree index_type, elt_type = NULL_TREE;
4079 if (!is_gimple_val (gimple_switch_index (stmt)))
4081 error ("invalid operand to switch statement");
4082 debug_generic_stmt (gimple_switch_index (stmt));
4083 return true;
4086 index_type = TREE_TYPE (gimple_switch_index (stmt));
4087 if (! INTEGRAL_TYPE_P (index_type))
4089 error ("non-integral type switch statement");
4090 debug_generic_expr (index_type);
4091 return true;
4094 elt = gimple_switch_label (stmt, 0);
4095 if (CASE_LOW (elt) != NULL_TREE || CASE_HIGH (elt) != NULL_TREE)
4097 error ("invalid default case label in switch statement");
4098 debug_generic_expr (elt);
4099 return true;
4102 n = gimple_switch_num_labels (stmt);
4103 for (i = 1; i < n; i++)
4105 elt = gimple_switch_label (stmt, i);
4107 if (! CASE_LOW (elt))
4109 error ("invalid case label in switch statement");
4110 debug_generic_expr (elt);
4111 return true;
4113 if (CASE_HIGH (elt)
4114 && ! tree_int_cst_lt (CASE_LOW (elt), CASE_HIGH (elt)))
4116 error ("invalid case range in switch statement");
4117 debug_generic_expr (elt);
4118 return true;
4121 if (elt_type)
4123 if (TREE_TYPE (CASE_LOW (elt)) != elt_type
4124 || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
4126 error ("type mismatch for case label in switch statement");
4127 debug_generic_expr (elt);
4128 return true;
4131 else
4133 elt_type = TREE_TYPE (CASE_LOW (elt));
4134 if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
4136 error ("type precision mismatch in switch statement");
4137 return true;
4141 if (prev_upper_bound)
4143 if (! tree_int_cst_lt (prev_upper_bound, CASE_LOW (elt)))
4145 error ("case labels not sorted in switch statement");
4146 return true;
4150 prev_upper_bound = CASE_HIGH (elt);
4151 if (! prev_upper_bound)
4152 prev_upper_bound = CASE_LOW (elt);
4155 return false;
4158 /* Verify a gimple debug statement STMT.
4159 Returns true if anything is wrong. */
4161 static bool
4162 verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
4164 /* There isn't much that could be wrong in a gimple debug stmt. A
4165 gimple debug bind stmt, for example, maps a tree, that's usually
4166 a VAR_DECL or a PARM_DECL, but that could also be some scalarized
4167 component or member of an aggregate type, to another tree, that
4168 can be an arbitrary expression. These stmts expand into debug
4169 insns, and are converted to debug notes by var-tracking.c. */
4170 return false;
4173 /* Verify a gimple label statement STMT.
4174 Returns true if anything is wrong. */
4176 static bool
4177 verify_gimple_label (gimple stmt)
4179 tree decl = gimple_label_label (stmt);
4180 int uid;
4181 bool err = false;
4183 if (TREE_CODE (decl) != LABEL_DECL)
4184 return true;
4186 uid = LABEL_DECL_UID (decl);
4187 if (cfun->cfg
4188 && (uid == -1 || (*label_to_block_map)[uid] != gimple_bb (stmt)))
4190 error ("incorrect entry in label_to_block_map");
4191 err |= true;
4194 uid = EH_LANDING_PAD_NR (decl);
4195 if (uid)
4197 eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
4198 if (decl != lp->post_landing_pad)
4200 error ("incorrect setting of landing pad number");
4201 err |= true;
4205 return err;
4208 /* Verify the GIMPLE statement STMT. Returns true if there is an
4209 error, otherwise false. */
4211 static bool
4212 verify_gimple_stmt (gimple stmt)
4214 switch (gimple_code (stmt))
4216 case GIMPLE_ASSIGN:
4217 return verify_gimple_assign (stmt);
4219 case GIMPLE_LABEL:
4220 return verify_gimple_label (stmt);
4222 case GIMPLE_CALL:
4223 return verify_gimple_call (stmt);
4225 case GIMPLE_COND:
4226 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
4228 error ("invalid comparison code in gimple cond");
4229 return true;
4231 if (!(!gimple_cond_true_label (stmt)
4232 || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
4233 || !(!gimple_cond_false_label (stmt)
4234 || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
4236 error ("invalid labels in gimple cond");
4237 return true;
4240 return verify_gimple_comparison (boolean_type_node,
4241 gimple_cond_lhs (stmt),
4242 gimple_cond_rhs (stmt));
4244 case GIMPLE_GOTO:
4245 return verify_gimple_goto (stmt);
4247 case GIMPLE_SWITCH:
4248 return verify_gimple_switch (stmt);
4250 case GIMPLE_RETURN:
4251 return verify_gimple_return (stmt);
4253 case GIMPLE_ASM:
4254 return false;
4256 case GIMPLE_TRANSACTION:
4257 return verify_gimple_transaction (stmt);
4259 /* Tuples that do not have tree operands. */
4260 case GIMPLE_NOP:
4261 case GIMPLE_PREDICT:
4262 case GIMPLE_RESX:
4263 case GIMPLE_EH_DISPATCH:
4264 case GIMPLE_EH_MUST_NOT_THROW:
4265 return false;
4267 CASE_GIMPLE_OMP:
4268 /* OpenMP directives are validated by the FE and never operated
4269 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
4270 non-gimple expressions when the main index variable has had
4271 its address taken. This does not affect the loop itself
4272 because the header of an GIMPLE_OMP_FOR is merely used to determine
4273 how to setup the parallel iteration. */
4274 return false;
4276 case GIMPLE_DEBUG:
4277 return verify_gimple_debug (stmt);
4279 default:
4280 gcc_unreachable ();
4284 /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
4285 and false otherwise. */
4287 static bool
4288 verify_gimple_phi (gimple phi)
4290 bool err = false;
4291 unsigned i;
4292 tree phi_result = gimple_phi_result (phi);
4293 bool virtual_p;
4295 if (!phi_result)
4297 error ("invalid PHI result");
4298 return true;
4301 virtual_p = virtual_operand_p (phi_result);
4302 if (TREE_CODE (phi_result) != SSA_NAME
4303 || (virtual_p
4304 && SSA_NAME_VAR (phi_result) != gimple_vop (cfun)))
4306 error ("invalid PHI result");
4307 err = true;
4310 for (i = 0; i < gimple_phi_num_args (phi); i++)
4312 tree t = gimple_phi_arg_def (phi, i);
4314 if (!t)
4316 error ("missing PHI def");
4317 err |= true;
4318 continue;
4320 /* Addressable variables do have SSA_NAMEs but they
4321 are not considered gimple values. */
4322 else if ((TREE_CODE (t) == SSA_NAME
4323 && virtual_p != virtual_operand_p (t))
4324 || (virtual_p
4325 && (TREE_CODE (t) != SSA_NAME
4326 || SSA_NAME_VAR (t) != gimple_vop (cfun)))
4327 || (!virtual_p
4328 && !is_gimple_val (t)))
4330 error ("invalid PHI argument");
4331 debug_generic_expr (t);
4332 err |= true;
4334 #ifdef ENABLE_TYPES_CHECKING
4335 if (!useless_type_conversion_p (TREE_TYPE (phi_result), TREE_TYPE (t)))
4337 error ("incompatible types in PHI argument %u", i);
4338 debug_generic_stmt (TREE_TYPE (phi_result));
4339 debug_generic_stmt (TREE_TYPE (t));
4340 err |= true;
4342 #endif
4345 return err;
4348 /* Verify the GIMPLE statements inside the sequence STMTS. */
4350 static bool
4351 verify_gimple_in_seq_2 (gimple_seq stmts)
4353 gimple_stmt_iterator ittr;
4354 bool err = false;
4356 for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
4358 gimple stmt = gsi_stmt (ittr);
4360 switch (gimple_code (stmt))
4362 case GIMPLE_BIND:
4363 err |= verify_gimple_in_seq_2 (gimple_bind_body (stmt));
4364 break;
4366 case GIMPLE_TRY:
4367 err |= verify_gimple_in_seq_2 (gimple_try_eval (stmt));
4368 err |= verify_gimple_in_seq_2 (gimple_try_cleanup (stmt));
4369 break;
4371 case GIMPLE_EH_FILTER:
4372 err |= verify_gimple_in_seq_2 (gimple_eh_filter_failure (stmt));
4373 break;
4375 case GIMPLE_EH_ELSE:
4376 err |= verify_gimple_in_seq_2 (gimple_eh_else_n_body (stmt));
4377 err |= verify_gimple_in_seq_2 (gimple_eh_else_e_body (stmt));
4378 break;
4380 case GIMPLE_CATCH:
4381 err |= verify_gimple_in_seq_2 (gimple_catch_handler (stmt));
4382 break;
4384 case GIMPLE_TRANSACTION:
4385 err |= verify_gimple_transaction (stmt);
4386 break;
4388 default:
4390 bool err2 = verify_gimple_stmt (stmt);
4391 if (err2)
4392 debug_gimple_stmt (stmt);
4393 err |= err2;
4398 return err;
4401 /* Verify the contents of a GIMPLE_TRANSACTION. Returns true if there
4402 is a problem, otherwise false. */
4404 static bool
4405 verify_gimple_transaction (gimple stmt)
4407 tree lab = gimple_transaction_label (stmt);
4408 if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
4409 return true;
4410 return verify_gimple_in_seq_2 (gimple_transaction_body (stmt));
4414 /* Verify the GIMPLE statements inside the statement list STMTS. */
4416 DEBUG_FUNCTION void
4417 verify_gimple_in_seq (gimple_seq stmts)
4419 timevar_push (TV_TREE_STMT_VERIFY);
4420 if (verify_gimple_in_seq_2 (stmts))
4421 internal_error ("verify_gimple failed");
4422 timevar_pop (TV_TREE_STMT_VERIFY);
4425 /* Return true when the T can be shared. */
4427 bool
4428 tree_node_can_be_shared (tree t)
4430 if (IS_TYPE_OR_DECL_P (t)
4431 || is_gimple_min_invariant (t)
4432 || TREE_CODE (t) == SSA_NAME
4433 || t == error_mark_node
4434 || TREE_CODE (t) == IDENTIFIER_NODE)
4435 return true;
4437 if (TREE_CODE (t) == CASE_LABEL_EXPR)
4438 return true;
4440 if (DECL_P (t))
4441 return true;
4443 return false;
4446 /* Called via walk_tree. Verify tree sharing. */
4448 static tree
4449 verify_node_sharing_1 (tree *tp, int *walk_subtrees, void *data)
4451 struct pointer_set_t *visited = (struct pointer_set_t *) data;
4453 if (tree_node_can_be_shared (*tp))
4455 *walk_subtrees = false;
4456 return NULL;
4459 if (pointer_set_insert (visited, *tp))
4460 return *tp;
4462 return NULL;
4465 /* Called via walk_gimple_stmt. Verify tree sharing. */
4467 static tree
4468 verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
4470 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4471 return verify_node_sharing_1 (tp, walk_subtrees, wi->info);
4474 static bool eh_error_found;
4475 static int
4476 verify_eh_throw_stmt_node (void **slot, void *data)
4478 struct throw_stmt_node *node = (struct throw_stmt_node *)*slot;
4479 struct pointer_set_t *visited = (struct pointer_set_t *) data;
4481 if (!pointer_set_contains (visited, node->stmt))
4483 error ("dead STMT in EH table");
4484 debug_gimple_stmt (node->stmt);
4485 eh_error_found = true;
4487 return 1;
4490 /* Verify if the location LOCs block is in BLOCKS. */
4492 static bool
4493 verify_location (pointer_set_t *blocks, location_t loc)
4495 tree block = LOCATION_BLOCK (loc);
4496 if (block != NULL_TREE
4497 && !pointer_set_contains (blocks, block))
4499 error ("location references block not in block tree");
4500 return true;
4502 if (block != NULL_TREE)
4503 return verify_location (blocks, BLOCK_SOURCE_LOCATION (block));
4504 return false;
4507 /* Called via walk_tree. Verify locations of expressions. */
4509 static tree
4510 verify_expr_location_1 (tree *tp, int *walk_subtrees, void *data)
4512 struct pointer_set_t *blocks = (struct pointer_set_t *) data;
4514 if (TREE_CODE (*tp) == VAR_DECL
4515 && DECL_DEBUG_EXPR_IS_FROM (*tp))
4517 tree t = DECL_DEBUG_EXPR (*tp);
4518 tree addr = walk_tree (&t, verify_expr_location_1, blocks, NULL);
4519 if (addr)
4520 return addr;
4523 if (!EXPR_P (*tp))
4525 *walk_subtrees = false;
4526 return NULL;
4529 location_t loc = EXPR_LOCATION (*tp);
4530 if (verify_location (blocks, loc))
4531 return *tp;
4533 return NULL;
4536 /* Called via walk_gimple_op. Verify locations of expressions. */
4538 static tree
4539 verify_expr_location (tree *tp, int *walk_subtrees, void *data)
4541 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4542 return verify_expr_location_1 (tp, walk_subtrees, wi->info);
4545 /* Insert all subblocks of BLOCK into BLOCKS and recurse. */
4547 static void
4548 collect_subblocks (pointer_set_t *blocks, tree block)
4550 tree t;
4551 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
4553 pointer_set_insert (blocks, t);
4554 collect_subblocks (blocks, t);
4558 /* Verify the GIMPLE statements in the CFG of FN. */
4560 DEBUG_FUNCTION void
4561 verify_gimple_in_cfg (struct function *fn)
4563 basic_block bb;
4564 bool err = false;
4565 struct pointer_set_t *visited, *visited_stmts, *blocks;
4567 timevar_push (TV_TREE_STMT_VERIFY);
4568 visited = pointer_set_create ();
4569 visited_stmts = pointer_set_create ();
4571 /* Collect all BLOCKs referenced by the BLOCK tree of FN. */
4572 blocks = pointer_set_create ();
4573 if (DECL_INITIAL (fn->decl))
4575 pointer_set_insert (blocks, DECL_INITIAL (fn->decl));
4576 collect_subblocks (blocks, DECL_INITIAL (fn->decl));
4579 FOR_EACH_BB_FN (bb, fn)
4581 gimple_stmt_iterator gsi;
4583 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4585 gimple phi = gsi_stmt (gsi);
4586 bool err2 = false;
4587 unsigned i;
4589 pointer_set_insert (visited_stmts, phi);
4591 if (gimple_bb (phi) != bb)
4593 error ("gimple_bb (phi) is set to a wrong basic block");
4594 err2 = true;
4597 err2 |= verify_gimple_phi (phi);
4599 /* Only PHI arguments have locations. */
4600 if (gimple_location (phi) != UNKNOWN_LOCATION)
4602 error ("PHI node with location");
4603 err2 = true;
4606 for (i = 0; i < gimple_phi_num_args (phi); i++)
4608 tree arg = gimple_phi_arg_def (phi, i);
4609 tree addr = walk_tree (&arg, verify_node_sharing_1,
4610 visited, NULL);
4611 if (addr)
4613 error ("incorrect sharing of tree nodes");
4614 debug_generic_expr (addr);
4615 err2 |= true;
4617 location_t loc = gimple_phi_arg_location (phi, i);
4618 if (virtual_operand_p (gimple_phi_result (phi))
4619 && loc != UNKNOWN_LOCATION)
4621 error ("virtual PHI with argument locations");
4622 err2 = true;
4624 addr = walk_tree (&arg, verify_expr_location_1, blocks, NULL);
4625 if (addr)
4627 debug_generic_expr (addr);
4628 err2 = true;
4630 err2 |= verify_location (blocks, loc);
4633 if (err2)
4634 debug_gimple_stmt (phi);
4635 err |= err2;
4638 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4640 gimple stmt = gsi_stmt (gsi);
4641 bool err2 = false;
4642 struct walk_stmt_info wi;
4643 tree addr;
4644 int lp_nr;
4646 pointer_set_insert (visited_stmts, stmt);
4648 if (gimple_bb (stmt) != bb)
4650 error ("gimple_bb (stmt) is set to a wrong basic block");
4651 err2 = true;
4654 err2 |= verify_gimple_stmt (stmt);
4655 err2 |= verify_location (blocks, gimple_location (stmt));
4657 memset (&wi, 0, sizeof (wi));
4658 wi.info = (void *) visited;
4659 addr = walk_gimple_op (stmt, verify_node_sharing, &wi);
4660 if (addr)
4662 error ("incorrect sharing of tree nodes");
4663 debug_generic_expr (addr);
4664 err2 |= true;
4667 memset (&wi, 0, sizeof (wi));
4668 wi.info = (void *) blocks;
4669 addr = walk_gimple_op (stmt, verify_expr_location, &wi);
4670 if (addr)
4672 debug_generic_expr (addr);
4673 err2 |= true;
4676 /* ??? Instead of not checking these stmts at all the walker
4677 should know its context via wi. */
4678 if (!is_gimple_debug (stmt)
4679 && !is_gimple_omp (stmt))
4681 memset (&wi, 0, sizeof (wi));
4682 addr = walk_gimple_op (stmt, verify_expr, &wi);
4683 if (addr)
4685 debug_generic_expr (addr);
4686 inform (gimple_location (stmt), "in statement");
4687 err2 |= true;
4691 /* If the statement is marked as part of an EH region, then it is
4692 expected that the statement could throw. Verify that when we
4693 have optimizations that simplify statements such that we prove
4694 that they cannot throw, that we update other data structures
4695 to match. */
4696 lp_nr = lookup_stmt_eh_lp (stmt);
4697 if (lp_nr != 0)
4699 if (!stmt_could_throw_p (stmt))
4701 error ("statement marked for throw, but doesn%'t");
4702 err2 |= true;
4704 else if (lp_nr > 0
4705 && !gsi_one_before_end_p (gsi)
4706 && stmt_can_throw_internal (stmt))
4708 error ("statement marked for throw in middle of block");
4709 err2 |= true;
4713 if (err2)
4714 debug_gimple_stmt (stmt);
4715 err |= err2;
4719 eh_error_found = false;
4720 if (get_eh_throw_stmt_table (cfun))
4721 htab_traverse (get_eh_throw_stmt_table (cfun),
4722 verify_eh_throw_stmt_node,
4723 visited_stmts);
4725 if (err || eh_error_found)
4726 internal_error ("verify_gimple failed");
4728 pointer_set_destroy (visited);
4729 pointer_set_destroy (visited_stmts);
4730 pointer_set_destroy (blocks);
4731 verify_histograms ();
4732 timevar_pop (TV_TREE_STMT_VERIFY);
4736 /* Verifies that the flow information is OK. */
4738 static int
4739 gimple_verify_flow_info (void)
4741 int err = 0;
4742 basic_block bb;
4743 gimple_stmt_iterator gsi;
4744 gimple stmt;
4745 edge e;
4746 edge_iterator ei;
4748 if (ENTRY_BLOCK_PTR->il.gimple.seq || ENTRY_BLOCK_PTR->il.gimple.phi_nodes)
4750 error ("ENTRY_BLOCK has IL associated with it");
4751 err = 1;
4754 if (EXIT_BLOCK_PTR->il.gimple.seq || EXIT_BLOCK_PTR->il.gimple.phi_nodes)
4756 error ("EXIT_BLOCK has IL associated with it");
4757 err = 1;
4760 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
4761 if (e->flags & EDGE_FALLTHRU)
4763 error ("fallthru to exit from bb %d", e->src->index);
4764 err = 1;
4767 FOR_EACH_BB (bb)
4769 bool found_ctrl_stmt = false;
4771 stmt = NULL;
4773 /* Skip labels on the start of basic block. */
4774 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4776 tree label;
4777 gimple prev_stmt = stmt;
4779 stmt = gsi_stmt (gsi);
4781 if (gimple_code (stmt) != GIMPLE_LABEL)
4782 break;
4784 label = gimple_label_label (stmt);
4785 if (prev_stmt && DECL_NONLOCAL (label))
4787 error ("nonlocal label ");
4788 print_generic_expr (stderr, label, 0);
4789 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4790 bb->index);
4791 err = 1;
4794 if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
4796 error ("EH landing pad label ");
4797 print_generic_expr (stderr, label, 0);
4798 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4799 bb->index);
4800 err = 1;
4803 if (label_to_block (label) != bb)
4805 error ("label ");
4806 print_generic_expr (stderr, label, 0);
4807 fprintf (stderr, " to block does not match in bb %d",
4808 bb->index);
4809 err = 1;
4812 if (decl_function_context (label) != current_function_decl)
4814 error ("label ");
4815 print_generic_expr (stderr, label, 0);
4816 fprintf (stderr, " has incorrect context in bb %d",
4817 bb->index);
4818 err = 1;
4822 /* Verify that body of basic block BB is free of control flow. */
4823 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4825 gimple stmt = gsi_stmt (gsi);
4827 if (found_ctrl_stmt)
4829 error ("control flow in the middle of basic block %d",
4830 bb->index);
4831 err = 1;
4834 if (stmt_ends_bb_p (stmt))
4835 found_ctrl_stmt = true;
4837 if (gimple_code (stmt) == GIMPLE_LABEL)
4839 error ("label ");
4840 print_generic_expr (stderr, gimple_label_label (stmt), 0);
4841 fprintf (stderr, " in the middle of basic block %d", bb->index);
4842 err = 1;
4846 gsi = gsi_last_bb (bb);
4847 if (gsi_end_p (gsi))
4848 continue;
4850 stmt = gsi_stmt (gsi);
4852 if (gimple_code (stmt) == GIMPLE_LABEL)
4853 continue;
4855 err |= verify_eh_edges (stmt);
4857 if (is_ctrl_stmt (stmt))
4859 FOR_EACH_EDGE (e, ei, bb->succs)
4860 if (e->flags & EDGE_FALLTHRU)
4862 error ("fallthru edge after a control statement in bb %d",
4863 bb->index);
4864 err = 1;
4868 if (gimple_code (stmt) != GIMPLE_COND)
4870 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
4871 after anything else but if statement. */
4872 FOR_EACH_EDGE (e, ei, bb->succs)
4873 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
4875 error ("true/false edge after a non-GIMPLE_COND in bb %d",
4876 bb->index);
4877 err = 1;
4881 switch (gimple_code (stmt))
4883 case GIMPLE_COND:
4885 edge true_edge;
4886 edge false_edge;
4888 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
4890 if (!true_edge
4891 || !false_edge
4892 || !(true_edge->flags & EDGE_TRUE_VALUE)
4893 || !(false_edge->flags & EDGE_FALSE_VALUE)
4894 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4895 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4896 || EDGE_COUNT (bb->succs) >= 3)
4898 error ("wrong outgoing edge flags at end of bb %d",
4899 bb->index);
4900 err = 1;
4903 break;
4905 case GIMPLE_GOTO:
4906 if (simple_goto_p (stmt))
4908 error ("explicit goto at end of bb %d", bb->index);
4909 err = 1;
4911 else
4913 /* FIXME. We should double check that the labels in the
4914 destination blocks have their address taken. */
4915 FOR_EACH_EDGE (e, ei, bb->succs)
4916 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
4917 | EDGE_FALSE_VALUE))
4918 || !(e->flags & EDGE_ABNORMAL))
4920 error ("wrong outgoing edge flags at end of bb %d",
4921 bb->index);
4922 err = 1;
4925 break;
4927 case GIMPLE_CALL:
4928 if (!gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
4929 break;
4930 /* ... fallthru ... */
4931 case GIMPLE_RETURN:
4932 if (!single_succ_p (bb)
4933 || (single_succ_edge (bb)->flags
4934 & (EDGE_FALLTHRU | EDGE_ABNORMAL
4935 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4937 error ("wrong outgoing edge flags at end of bb %d", bb->index);
4938 err = 1;
4940 if (single_succ (bb) != EXIT_BLOCK_PTR)
4942 error ("return edge does not point to exit in bb %d",
4943 bb->index);
4944 err = 1;
4946 break;
4948 case GIMPLE_SWITCH:
4950 tree prev;
4951 edge e;
4952 size_t i, n;
4954 n = gimple_switch_num_labels (stmt);
4956 /* Mark all the destination basic blocks. */
4957 for (i = 0; i < n; ++i)
4959 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4960 basic_block label_bb = label_to_block (lab);
4961 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
4962 label_bb->aux = (void *)1;
4965 /* Verify that the case labels are sorted. */
4966 prev = gimple_switch_label (stmt, 0);
4967 for (i = 1; i < n; ++i)
4969 tree c = gimple_switch_label (stmt, i);
4970 if (!CASE_LOW (c))
4972 error ("found default case not at the start of "
4973 "case vector");
4974 err = 1;
4975 continue;
4977 if (CASE_LOW (prev)
4978 && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
4980 error ("case labels not sorted: ");
4981 print_generic_expr (stderr, prev, 0);
4982 fprintf (stderr," is greater than ");
4983 print_generic_expr (stderr, c, 0);
4984 fprintf (stderr," but comes before it.\n");
4985 err = 1;
4987 prev = c;
4989 /* VRP will remove the default case if it can prove it will
4990 never be executed. So do not verify there always exists
4991 a default case here. */
4993 FOR_EACH_EDGE (e, ei, bb->succs)
4995 if (!e->dest->aux)
4997 error ("extra outgoing edge %d->%d",
4998 bb->index, e->dest->index);
4999 err = 1;
5002 e->dest->aux = (void *)2;
5003 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
5004 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
5006 error ("wrong outgoing edge flags at end of bb %d",
5007 bb->index);
5008 err = 1;
5012 /* Check that we have all of them. */
5013 for (i = 0; i < n; ++i)
5015 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
5016 basic_block label_bb = label_to_block (lab);
5018 if (label_bb->aux != (void *)2)
5020 error ("missing edge %i->%i", bb->index, label_bb->index);
5021 err = 1;
5025 FOR_EACH_EDGE (e, ei, bb->succs)
5026 e->dest->aux = (void *)0;
5028 break;
5030 case GIMPLE_EH_DISPATCH:
5031 err |= verify_eh_dispatch_edge (stmt);
5032 break;
5034 default:
5035 break;
5039 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
5040 verify_dominators (CDI_DOMINATORS);
5042 return err;
5046 /* Updates phi nodes after creating a forwarder block joined
5047 by edge FALLTHRU. */
5049 static void
5050 gimple_make_forwarder_block (edge fallthru)
5052 edge e;
5053 edge_iterator ei;
5054 basic_block dummy, bb;
5055 tree var;
5056 gimple_stmt_iterator gsi;
5058 dummy = fallthru->src;
5059 bb = fallthru->dest;
5061 if (single_pred_p (bb))
5062 return;
5064 /* If we redirected a branch we must create new PHI nodes at the
5065 start of BB. */
5066 for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
5068 gimple phi, new_phi;
5070 phi = gsi_stmt (gsi);
5071 var = gimple_phi_result (phi);
5072 new_phi = create_phi_node (var, bb);
5073 gimple_phi_set_result (phi, copy_ssa_name (var, phi));
5074 add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
5075 UNKNOWN_LOCATION);
5078 /* Add the arguments we have stored on edges. */
5079 FOR_EACH_EDGE (e, ei, bb->preds)
5081 if (e == fallthru)
5082 continue;
5084 flush_pending_stmts (e);
5089 /* Return a non-special label in the head of basic block BLOCK.
5090 Create one if it doesn't exist. */
5092 tree
5093 gimple_block_label (basic_block bb)
5095 gimple_stmt_iterator i, s = gsi_start_bb (bb);
5096 bool first = true;
5097 tree label;
5098 gimple stmt;
5100 for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
5102 stmt = gsi_stmt (i);
5103 if (gimple_code (stmt) != GIMPLE_LABEL)
5104 break;
5105 label = gimple_label_label (stmt);
5106 if (!DECL_NONLOCAL (label))
5108 if (!first)
5109 gsi_move_before (&i, &s);
5110 return label;
5114 label = create_artificial_label (UNKNOWN_LOCATION);
5115 stmt = gimple_build_label (label);
5116 gsi_insert_before (&s, stmt, GSI_NEW_STMT);
5117 return label;
5121 /* Attempt to perform edge redirection by replacing a possibly complex
5122 jump instruction by a goto or by removing the jump completely.
5123 This can apply only if all edges now point to the same block. The
5124 parameters and return values are equivalent to
5125 redirect_edge_and_branch. */
5127 static edge
5128 gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
5130 basic_block src = e->src;
5131 gimple_stmt_iterator i;
5132 gimple stmt;
5134 /* We can replace or remove a complex jump only when we have exactly
5135 two edges. */
5136 if (EDGE_COUNT (src->succs) != 2
5137 /* Verify that all targets will be TARGET. Specifically, the
5138 edge that is not E must also go to TARGET. */
5139 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
5140 return NULL;
5142 i = gsi_last_bb (src);
5143 if (gsi_end_p (i))
5144 return NULL;
5146 stmt = gsi_stmt (i);
5148 if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
5150 gsi_remove (&i, true);
5151 e = ssa_redirect_edge (e, target);
5152 e->flags = EDGE_FALLTHRU;
5153 return e;
5156 return NULL;
5160 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
5161 edge representing the redirected branch. */
5163 static edge
5164 gimple_redirect_edge_and_branch (edge e, basic_block dest)
5166 basic_block bb = e->src;
5167 gimple_stmt_iterator gsi;
5168 edge ret;
5169 gimple stmt;
5171 if (e->flags & EDGE_ABNORMAL)
5172 return NULL;
5174 if (e->dest == dest)
5175 return NULL;
5177 if (e->flags & EDGE_EH)
5178 return redirect_eh_edge (e, dest);
5180 if (e->src != ENTRY_BLOCK_PTR)
5182 ret = gimple_try_redirect_by_replacing_jump (e, dest);
5183 if (ret)
5184 return ret;
5187 gsi = gsi_last_bb (bb);
5188 stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
5190 switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
5192 case GIMPLE_COND:
5193 /* For COND_EXPR, we only need to redirect the edge. */
5194 break;
5196 case GIMPLE_GOTO:
5197 /* No non-abnormal edges should lead from a non-simple goto, and
5198 simple ones should be represented implicitly. */
5199 gcc_unreachable ();
5201 case GIMPLE_SWITCH:
5203 tree label = gimple_block_label (dest);
5204 tree cases = get_cases_for_edge (e, stmt);
5206 /* If we have a list of cases associated with E, then use it
5207 as it's a lot faster than walking the entire case vector. */
5208 if (cases)
5210 edge e2 = find_edge (e->src, dest);
5211 tree last, first;
5213 first = cases;
5214 while (cases)
5216 last = cases;
5217 CASE_LABEL (cases) = label;
5218 cases = CASE_CHAIN (cases);
5221 /* If there was already an edge in the CFG, then we need
5222 to move all the cases associated with E to E2. */
5223 if (e2)
5225 tree cases2 = get_cases_for_edge (e2, stmt);
5227 CASE_CHAIN (last) = CASE_CHAIN (cases2);
5228 CASE_CHAIN (cases2) = first;
5230 bitmap_set_bit (touched_switch_bbs, gimple_bb (stmt)->index);
5232 else
5234 size_t i, n = gimple_switch_num_labels (stmt);
5236 for (i = 0; i < n; i++)
5238 tree elt = gimple_switch_label (stmt, i);
5239 if (label_to_block (CASE_LABEL (elt)) == e->dest)
5240 CASE_LABEL (elt) = label;
5244 break;
5246 case GIMPLE_ASM:
5248 int i, n = gimple_asm_nlabels (stmt);
5249 tree label = NULL;
5251 for (i = 0; i < n; ++i)
5253 tree cons = gimple_asm_label_op (stmt, i);
5254 if (label_to_block (TREE_VALUE (cons)) == e->dest)
5256 if (!label)
5257 label = gimple_block_label (dest);
5258 TREE_VALUE (cons) = label;
5262 /* If we didn't find any label matching the former edge in the
5263 asm labels, we must be redirecting the fallthrough
5264 edge. */
5265 gcc_assert (label || (e->flags & EDGE_FALLTHRU));
5267 break;
5269 case GIMPLE_RETURN:
5270 gsi_remove (&gsi, true);
5271 e->flags |= EDGE_FALLTHRU;
5272 break;
5274 case GIMPLE_OMP_RETURN:
5275 case GIMPLE_OMP_CONTINUE:
5276 case GIMPLE_OMP_SECTIONS_SWITCH:
5277 case GIMPLE_OMP_FOR:
5278 /* The edges from OMP constructs can be simply redirected. */
5279 break;
5281 case GIMPLE_EH_DISPATCH:
5282 if (!(e->flags & EDGE_FALLTHRU))
5283 redirect_eh_dispatch_edge (stmt, e, dest);
5284 break;
5286 case GIMPLE_TRANSACTION:
5287 /* The ABORT edge has a stored label associated with it, otherwise
5288 the edges are simply redirectable. */
5289 if (e->flags == 0)
5290 gimple_transaction_set_label (stmt, gimple_block_label (dest));
5291 break;
5293 default:
5294 /* Otherwise it must be a fallthru edge, and we don't need to
5295 do anything besides redirecting it. */
5296 gcc_assert (e->flags & EDGE_FALLTHRU);
5297 break;
5300 /* Update/insert PHI nodes as necessary. */
5302 /* Now update the edges in the CFG. */
5303 e = ssa_redirect_edge (e, dest);
5305 return e;
5308 /* Returns true if it is possible to remove edge E by redirecting
5309 it to the destination of the other edge from E->src. */
5311 static bool
5312 gimple_can_remove_branch_p (const_edge e)
5314 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
5315 return false;
5317 return true;
5320 /* Simple wrapper, as we can always redirect fallthru edges. */
5322 static basic_block
5323 gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
5325 e = gimple_redirect_edge_and_branch (e, dest);
5326 gcc_assert (e);
5328 return NULL;
5332 /* Splits basic block BB after statement STMT (but at least after the
5333 labels). If STMT is NULL, BB is split just after the labels. */
5335 static basic_block
5336 gimple_split_block (basic_block bb, void *stmt)
5338 gimple_stmt_iterator gsi;
5339 gimple_stmt_iterator gsi_tgt;
5340 gimple act;
5341 gimple_seq list;
5342 basic_block new_bb;
5343 edge e;
5344 edge_iterator ei;
5346 new_bb = create_empty_bb (bb);
5348 /* Redirect the outgoing edges. */
5349 new_bb->succs = bb->succs;
5350 bb->succs = NULL;
5351 FOR_EACH_EDGE (e, ei, new_bb->succs)
5352 e->src = new_bb;
5354 if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
5355 stmt = NULL;
5357 /* Move everything from GSI to the new basic block. */
5358 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5360 act = gsi_stmt (gsi);
5361 if (gimple_code (act) == GIMPLE_LABEL)
5362 continue;
5364 if (!stmt)
5365 break;
5367 if (stmt == act)
5369 gsi_next (&gsi);
5370 break;
5374 if (gsi_end_p (gsi))
5375 return new_bb;
5377 /* Split the statement list - avoid re-creating new containers as this
5378 brings ugly quadratic memory consumption in the inliner.
5379 (We are still quadratic since we need to update stmt BB pointers,
5380 sadly.) */
5381 gsi_split_seq_before (&gsi, &list);
5382 set_bb_seq (new_bb, list);
5383 for (gsi_tgt = gsi_start (list);
5384 !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
5385 gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
5387 return new_bb;
5391 /* Moves basic block BB after block AFTER. */
5393 static bool
5394 gimple_move_block_after (basic_block bb, basic_block after)
5396 if (bb->prev_bb == after)
5397 return true;
5399 unlink_block (bb);
5400 link_block (bb, after);
5402 return true;
5406 /* Return TRUE if block BB has no executable statements, otherwise return
5407 FALSE. */
5409 bool
5410 gimple_empty_block_p (basic_block bb)
5412 /* BB must have no executable statements. */
5413 gimple_stmt_iterator gsi = gsi_after_labels (bb);
5414 if (phi_nodes (bb))
5415 return false;
5416 if (gsi_end_p (gsi))
5417 return true;
5418 if (is_gimple_debug (gsi_stmt (gsi)))
5419 gsi_next_nondebug (&gsi);
5420 return gsi_end_p (gsi);
5424 /* Split a basic block if it ends with a conditional branch and if the
5425 other part of the block is not empty. */
5427 static basic_block
5428 gimple_split_block_before_cond_jump (basic_block bb)
5430 gimple last, split_point;
5431 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
5432 if (gsi_end_p (gsi))
5433 return NULL;
5434 last = gsi_stmt (gsi);
5435 if (gimple_code (last) != GIMPLE_COND
5436 && gimple_code (last) != GIMPLE_SWITCH)
5437 return NULL;
5438 gsi_prev_nondebug (&gsi);
5439 split_point = gsi_stmt (gsi);
5440 return split_block (bb, split_point)->dest;
5444 /* Return true if basic_block can be duplicated. */
5446 static bool
5447 gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
5449 return true;
5452 /* Create a duplicate of the basic block BB. NOTE: This does not
5453 preserve SSA form. */
5455 static basic_block
5456 gimple_duplicate_bb (basic_block bb)
5458 basic_block new_bb;
5459 gimple_stmt_iterator gsi, gsi_tgt;
5460 gimple_seq phis = phi_nodes (bb);
5461 gimple phi, stmt, copy;
5463 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
5465 /* Copy the PHI nodes. We ignore PHI node arguments here because
5466 the incoming edges have not been setup yet. */
5467 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
5469 phi = gsi_stmt (gsi);
5470 copy = create_phi_node (NULL_TREE, new_bb);
5471 create_new_def_for (gimple_phi_result (phi), copy,
5472 gimple_phi_result_ptr (copy));
5475 gsi_tgt = gsi_start_bb (new_bb);
5476 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5478 def_operand_p def_p;
5479 ssa_op_iter op_iter;
5480 tree lhs;
5482 stmt = gsi_stmt (gsi);
5483 if (gimple_code (stmt) == GIMPLE_LABEL)
5484 continue;
5486 /* Don't duplicate label debug stmts. */
5487 if (gimple_debug_bind_p (stmt)
5488 && TREE_CODE (gimple_debug_bind_get_var (stmt))
5489 == LABEL_DECL)
5490 continue;
5492 /* Create a new copy of STMT and duplicate STMT's virtual
5493 operands. */
5494 copy = gimple_copy (stmt);
5495 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
5497 maybe_duplicate_eh_stmt (copy, stmt);
5498 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
5500 /* When copying around a stmt writing into a local non-user
5501 aggregate, make sure it won't share stack slot with other
5502 vars. */
5503 lhs = gimple_get_lhs (stmt);
5504 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5506 tree base = get_base_address (lhs);
5507 if (base
5508 && (TREE_CODE (base) == VAR_DECL
5509 || TREE_CODE (base) == RESULT_DECL)
5510 && DECL_IGNORED_P (base)
5511 && !TREE_STATIC (base)
5512 && !DECL_EXTERNAL (base)
5513 && (TREE_CODE (base) != VAR_DECL
5514 || !DECL_HAS_VALUE_EXPR_P (base)))
5515 DECL_NONSHAREABLE (base) = 1;
5518 /* Create new names for all the definitions created by COPY and
5519 add replacement mappings for each new name. */
5520 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
5521 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
5524 return new_bb;
5527 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
5529 static void
5530 add_phi_args_after_copy_edge (edge e_copy)
5532 basic_block bb, bb_copy = e_copy->src, dest;
5533 edge e;
5534 edge_iterator ei;
5535 gimple phi, phi_copy;
5536 tree def;
5537 gimple_stmt_iterator psi, psi_copy;
5539 if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
5540 return;
5542 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
5544 if (e_copy->dest->flags & BB_DUPLICATED)
5545 dest = get_bb_original (e_copy->dest);
5546 else
5547 dest = e_copy->dest;
5549 e = find_edge (bb, dest);
5550 if (!e)
5552 /* During loop unrolling the target of the latch edge is copied.
5553 In this case we are not looking for edge to dest, but to
5554 duplicated block whose original was dest. */
5555 FOR_EACH_EDGE (e, ei, bb->succs)
5557 if ((e->dest->flags & BB_DUPLICATED)
5558 && get_bb_original (e->dest) == dest)
5559 break;
5562 gcc_assert (e != NULL);
5565 for (psi = gsi_start_phis (e->dest),
5566 psi_copy = gsi_start_phis (e_copy->dest);
5567 !gsi_end_p (psi);
5568 gsi_next (&psi), gsi_next (&psi_copy))
5570 phi = gsi_stmt (psi);
5571 phi_copy = gsi_stmt (psi_copy);
5572 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5573 add_phi_arg (phi_copy, def, e_copy,
5574 gimple_phi_arg_location_from_edge (phi, e));
5579 /* Basic block BB_COPY was created by code duplication. Add phi node
5580 arguments for edges going out of BB_COPY. The blocks that were
5581 duplicated have BB_DUPLICATED set. */
5583 void
5584 add_phi_args_after_copy_bb (basic_block bb_copy)
5586 edge e_copy;
5587 edge_iterator ei;
5589 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
5591 add_phi_args_after_copy_edge (e_copy);
5595 /* Blocks in REGION_COPY array of length N_REGION were created by
5596 duplication of basic blocks. Add phi node arguments for edges
5597 going from these blocks. If E_COPY is not NULL, also add
5598 phi node arguments for its destination.*/
5600 void
5601 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
5602 edge e_copy)
5604 unsigned i;
5606 for (i = 0; i < n_region; i++)
5607 region_copy[i]->flags |= BB_DUPLICATED;
5609 for (i = 0; i < n_region; i++)
5610 add_phi_args_after_copy_bb (region_copy[i]);
5611 if (e_copy)
5612 add_phi_args_after_copy_edge (e_copy);
5614 for (i = 0; i < n_region; i++)
5615 region_copy[i]->flags &= ~BB_DUPLICATED;
5618 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5619 important exit edge EXIT. By important we mean that no SSA name defined
5620 inside region is live over the other exit edges of the region. All entry
5621 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5622 to the duplicate of the region. Dominance and loop information is
5623 updated, but not the SSA web. The new basic blocks are stored to
5624 REGION_COPY in the same order as they had in REGION, provided that
5625 REGION_COPY is not NULL.
5626 The function returns false if it is unable to copy the region,
5627 true otherwise. */
5629 bool
5630 gimple_duplicate_sese_region (edge entry, edge exit,
5631 basic_block *region, unsigned n_region,
5632 basic_block *region_copy)
5634 unsigned i;
5635 bool free_region_copy = false, copying_header = false;
5636 struct loop *loop = entry->dest->loop_father;
5637 edge exit_copy;
5638 vec<basic_block> doms;
5639 edge redirected;
5640 int total_freq = 0, entry_freq = 0;
5641 gcov_type total_count = 0, entry_count = 0;
5643 if (!can_copy_bbs_p (region, n_region))
5644 return false;
5646 /* Some sanity checking. Note that we do not check for all possible
5647 missuses of the functions. I.e. if you ask to copy something weird,
5648 it will work, but the state of structures probably will not be
5649 correct. */
5650 for (i = 0; i < n_region; i++)
5652 /* We do not handle subloops, i.e. all the blocks must belong to the
5653 same loop. */
5654 if (region[i]->loop_father != loop)
5655 return false;
5657 if (region[i] != entry->dest
5658 && region[i] == loop->header)
5659 return false;
5662 set_loop_copy (loop, loop);
5664 /* In case the function is used for loop header copying (which is the primary
5665 use), ensure that EXIT and its copy will be new latch and entry edges. */
5666 if (loop->header == entry->dest)
5668 copying_header = true;
5669 set_loop_copy (loop, loop_outer (loop));
5671 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5672 return false;
5674 for (i = 0; i < n_region; i++)
5675 if (region[i] != exit->src
5676 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5677 return false;
5680 if (!region_copy)
5682 region_copy = XNEWVEC (basic_block, n_region);
5683 free_region_copy = true;
5686 /* Record blocks outside the region that are dominated by something
5687 inside. */
5688 doms.create (0);
5689 initialize_original_copy_tables ();
5691 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5693 if (entry->dest->count)
5695 total_count = entry->dest->count;
5696 entry_count = entry->count;
5697 /* Fix up corner cases, to avoid division by zero or creation of negative
5698 frequencies. */
5699 if (entry_count > total_count)
5700 entry_count = total_count;
5702 else
5704 total_freq = entry->dest->frequency;
5705 entry_freq = EDGE_FREQUENCY (entry);
5706 /* Fix up corner cases, to avoid division by zero or creation of negative
5707 frequencies. */
5708 if (total_freq == 0)
5709 total_freq = 1;
5710 else if (entry_freq > total_freq)
5711 entry_freq = total_freq;
5714 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
5715 split_edge_bb_loc (entry));
5716 if (total_count)
5718 scale_bbs_frequencies_gcov_type (region, n_region,
5719 total_count - entry_count,
5720 total_count);
5721 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
5722 total_count);
5724 else
5726 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
5727 total_freq);
5728 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
5731 if (copying_header)
5733 loop->header = exit->dest;
5734 loop->latch = exit->src;
5737 /* Redirect the entry and add the phi node arguments. */
5738 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
5739 gcc_assert (redirected != NULL);
5740 flush_pending_stmts (entry);
5742 /* Concerning updating of dominators: We must recount dominators
5743 for entry block and its copy. Anything that is outside of the
5744 region, but was dominated by something inside needs recounting as
5745 well. */
5746 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5747 doms.safe_push (get_bb_original (entry->dest));
5748 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5749 doms.release ();
5751 /* Add the other PHI node arguments. */
5752 add_phi_args_after_copy (region_copy, n_region, NULL);
5754 if (free_region_copy)
5755 free (region_copy);
5757 free_original_copy_tables ();
5758 return true;
5761 /* Checks if BB is part of the region defined by N_REGION BBS. */
5762 static bool
5763 bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
5765 unsigned int n;
5767 for (n = 0; n < n_region; n++)
5769 if (bb == bbs[n])
5770 return true;
5772 return false;
5775 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
5776 are stored to REGION_COPY in the same order in that they appear
5777 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
5778 the region, EXIT an exit from it. The condition guarding EXIT
5779 is moved to ENTRY. Returns true if duplication succeeds, false
5780 otherwise.
5782 For example,
5784 some_code;
5785 if (cond)
5787 else
5790 is transformed to
5792 if (cond)
5794 some_code;
5797 else
5799 some_code;
5804 bool
5805 gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
5806 basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
5807 basic_block *region_copy ATTRIBUTE_UNUSED)
5809 unsigned i;
5810 bool free_region_copy = false;
5811 struct loop *loop = exit->dest->loop_father;
5812 struct loop *orig_loop = entry->dest->loop_father;
5813 basic_block switch_bb, entry_bb, nentry_bb;
5814 vec<basic_block> doms;
5815 int total_freq = 0, exit_freq = 0;
5816 gcov_type total_count = 0, exit_count = 0;
5817 edge exits[2], nexits[2], e;
5818 gimple_stmt_iterator gsi;
5819 gimple cond_stmt;
5820 edge sorig, snew;
5821 basic_block exit_bb;
5822 gimple_stmt_iterator psi;
5823 gimple phi;
5824 tree def;
5825 struct loop *target, *aloop, *cloop;
5827 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
5828 exits[0] = exit;
5829 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
5831 if (!can_copy_bbs_p (region, n_region))
5832 return false;
5834 initialize_original_copy_tables ();
5835 set_loop_copy (orig_loop, loop);
5837 target= loop;
5838 for (aloop = orig_loop->inner; aloop; aloop = aloop->next)
5840 if (bb_part_of_region_p (aloop->header, region, n_region))
5842 cloop = duplicate_loop (aloop, target);
5843 duplicate_subloops (aloop, cloop);
5847 if (!region_copy)
5849 region_copy = XNEWVEC (basic_block, n_region);
5850 free_region_copy = true;
5853 gcc_assert (!need_ssa_update_p (cfun));
5855 /* Record blocks outside the region that are dominated by something
5856 inside. */
5857 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5859 if (exit->src->count)
5861 total_count = exit->src->count;
5862 exit_count = exit->count;
5863 /* Fix up corner cases, to avoid division by zero or creation of negative
5864 frequencies. */
5865 if (exit_count > total_count)
5866 exit_count = total_count;
5868 else
5870 total_freq = exit->src->frequency;
5871 exit_freq = EDGE_FREQUENCY (exit);
5872 /* Fix up corner cases, to avoid division by zero or creation of negative
5873 frequencies. */
5874 if (total_freq == 0)
5875 total_freq = 1;
5876 if (exit_freq > total_freq)
5877 exit_freq = total_freq;
5880 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
5881 split_edge_bb_loc (exit));
5882 if (total_count)
5884 scale_bbs_frequencies_gcov_type (region, n_region,
5885 total_count - exit_count,
5886 total_count);
5887 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
5888 total_count);
5890 else
5892 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
5893 total_freq);
5894 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
5897 /* Create the switch block, and put the exit condition to it. */
5898 entry_bb = entry->dest;
5899 nentry_bb = get_bb_copy (entry_bb);
5900 if (!last_stmt (entry->src)
5901 || !stmt_ends_bb_p (last_stmt (entry->src)))
5902 switch_bb = entry->src;
5903 else
5904 switch_bb = split_edge (entry);
5905 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
5907 gsi = gsi_last_bb (switch_bb);
5908 cond_stmt = last_stmt (exit->src);
5909 gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
5910 cond_stmt = gimple_copy (cond_stmt);
5912 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
5914 sorig = single_succ_edge (switch_bb);
5915 sorig->flags = exits[1]->flags;
5916 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
5918 /* Register the new edge from SWITCH_BB in loop exit lists. */
5919 rescan_loop_exit (snew, true, false);
5921 /* Add the PHI node arguments. */
5922 add_phi_args_after_copy (region_copy, n_region, snew);
5924 /* Get rid of now superfluous conditions and associated edges (and phi node
5925 arguments). */
5926 exit_bb = exit->dest;
5928 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
5929 PENDING_STMT (e) = NULL;
5931 /* The latch of ORIG_LOOP was copied, and so was the backedge
5932 to the original header. We redirect this backedge to EXIT_BB. */
5933 for (i = 0; i < n_region; i++)
5934 if (get_bb_original (region_copy[i]) == orig_loop->latch)
5936 gcc_assert (single_succ_edge (region_copy[i]));
5937 e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
5938 PENDING_STMT (e) = NULL;
5939 for (psi = gsi_start_phis (exit_bb);
5940 !gsi_end_p (psi);
5941 gsi_next (&psi))
5943 phi = gsi_stmt (psi);
5944 def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
5945 add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
5948 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
5949 PENDING_STMT (e) = NULL;
5951 /* Anything that is outside of the region, but was dominated by something
5952 inside needs to update dominance info. */
5953 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5954 doms.release ();
5955 /* Update the SSA web. */
5956 update_ssa (TODO_update_ssa);
5958 if (free_region_copy)
5959 free (region_copy);
5961 free_original_copy_tables ();
5962 return true;
5965 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
5966 adding blocks when the dominator traversal reaches EXIT. This
5967 function silently assumes that ENTRY strictly dominates EXIT. */
5969 void
5970 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
5971 vec<basic_block> *bbs_p)
5973 basic_block son;
5975 for (son = first_dom_son (CDI_DOMINATORS, entry);
5976 son;
5977 son = next_dom_son (CDI_DOMINATORS, son))
5979 bbs_p->safe_push (son);
5980 if (son != exit)
5981 gather_blocks_in_sese_region (son, exit, bbs_p);
5985 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
5986 The duplicates are recorded in VARS_MAP. */
5988 static void
5989 replace_by_duplicate_decl (tree *tp, struct pointer_map_t *vars_map,
5990 tree to_context)
5992 tree t = *tp, new_t;
5993 struct function *f = DECL_STRUCT_FUNCTION (to_context);
5994 void **loc;
5996 if (DECL_CONTEXT (t) == to_context)
5997 return;
5999 loc = pointer_map_contains (vars_map, t);
6001 if (!loc)
6003 loc = pointer_map_insert (vars_map, t);
6005 if (SSA_VAR_P (t))
6007 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
6008 add_local_decl (f, new_t);
6010 else
6012 gcc_assert (TREE_CODE (t) == CONST_DECL);
6013 new_t = copy_node (t);
6015 DECL_CONTEXT (new_t) = to_context;
6017 *loc = new_t;
6019 else
6020 new_t = (tree) *loc;
6022 *tp = new_t;
6026 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
6027 VARS_MAP maps old ssa names and var_decls to the new ones. */
6029 static tree
6030 replace_ssa_name (tree name, struct pointer_map_t *vars_map,
6031 tree to_context)
6033 void **loc;
6034 tree new_name;
6036 gcc_assert (!virtual_operand_p (name));
6038 loc = pointer_map_contains (vars_map, name);
6040 if (!loc)
6042 tree decl = SSA_NAME_VAR (name);
6043 if (decl)
6045 replace_by_duplicate_decl (&decl, vars_map, to_context);
6046 new_name = make_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
6047 decl, SSA_NAME_DEF_STMT (name));
6048 if (SSA_NAME_IS_DEFAULT_DEF (name))
6049 set_ssa_default_def (DECL_STRUCT_FUNCTION (to_context),
6050 decl, new_name);
6052 else
6053 new_name = copy_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
6054 name, SSA_NAME_DEF_STMT (name));
6056 loc = pointer_map_insert (vars_map, name);
6057 *loc = new_name;
6059 else
6060 new_name = (tree) *loc;
6062 return new_name;
6065 struct move_stmt_d
6067 tree orig_block;
6068 tree new_block;
6069 tree from_context;
6070 tree to_context;
6071 struct pointer_map_t *vars_map;
6072 htab_t new_label_map;
6073 struct pointer_map_t *eh_map;
6074 bool remap_decls_p;
6077 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
6078 contained in *TP if it has been ORIG_BLOCK previously and change the
6079 DECL_CONTEXT of every local variable referenced in *TP. */
6081 static tree
6082 move_stmt_op (tree *tp, int *walk_subtrees, void *data)
6084 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6085 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6086 tree t = *tp;
6088 if (EXPR_P (t))
6090 tree block = TREE_BLOCK (t);
6091 if (block == p->orig_block
6092 || (p->orig_block == NULL_TREE
6093 && block != NULL_TREE))
6094 TREE_SET_BLOCK (t, p->new_block);
6095 #ifdef ENABLE_CHECKING
6096 else if (block != NULL_TREE)
6098 while (block && TREE_CODE (block) == BLOCK && block != p->orig_block)
6099 block = BLOCK_SUPERCONTEXT (block);
6100 gcc_assert (block == p->orig_block);
6102 #endif
6104 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
6106 if (TREE_CODE (t) == SSA_NAME)
6107 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
6108 else if (TREE_CODE (t) == LABEL_DECL)
6110 if (p->new_label_map)
6112 struct tree_map in, *out;
6113 in.base.from = t;
6114 out = (struct tree_map *)
6115 htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
6116 if (out)
6117 *tp = t = out->to;
6120 DECL_CONTEXT (t) = p->to_context;
6122 else if (p->remap_decls_p)
6124 /* Replace T with its duplicate. T should no longer appear in the
6125 parent function, so this looks wasteful; however, it may appear
6126 in referenced_vars, and more importantly, as virtual operands of
6127 statements, and in alias lists of other variables. It would be
6128 quite difficult to expunge it from all those places. ??? It might
6129 suffice to do this for addressable variables. */
6130 if ((TREE_CODE (t) == VAR_DECL
6131 && !is_global_var (t))
6132 || TREE_CODE (t) == CONST_DECL)
6133 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
6135 *walk_subtrees = 0;
6137 else if (TYPE_P (t))
6138 *walk_subtrees = 0;
6140 return NULL_TREE;
6143 /* Helper for move_stmt_r. Given an EH region number for the source
6144 function, map that to the duplicate EH regio number in the dest. */
6146 static int
6147 move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
6149 eh_region old_r, new_r;
6150 void **slot;
6152 old_r = get_eh_region_from_number (old_nr);
6153 slot = pointer_map_contains (p->eh_map, old_r);
6154 new_r = (eh_region) *slot;
6156 return new_r->index;
6159 /* Similar, but operate on INTEGER_CSTs. */
6161 static tree
6162 move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
6164 int old_nr, new_nr;
6166 old_nr = tree_low_cst (old_t_nr, 0);
6167 new_nr = move_stmt_eh_region_nr (old_nr, p);
6169 return build_int_cst (integer_type_node, new_nr);
6172 /* Like move_stmt_op, but for gimple statements.
6174 Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
6175 contained in the current statement in *GSI_P and change the
6176 DECL_CONTEXT of every local variable referenced in the current
6177 statement. */
6179 static tree
6180 move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
6181 struct walk_stmt_info *wi)
6183 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6184 gimple stmt = gsi_stmt (*gsi_p);
6185 tree block = gimple_block (stmt);
6187 if (block == p->orig_block
6188 || (p->orig_block == NULL_TREE
6189 && block != NULL_TREE))
6190 gimple_set_block (stmt, p->new_block);
6192 switch (gimple_code (stmt))
6194 case GIMPLE_CALL:
6195 /* Remap the region numbers for __builtin_eh_{pointer,filter}. */
6197 tree r, fndecl = gimple_call_fndecl (stmt);
6198 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
6199 switch (DECL_FUNCTION_CODE (fndecl))
6201 case BUILT_IN_EH_COPY_VALUES:
6202 r = gimple_call_arg (stmt, 1);
6203 r = move_stmt_eh_region_tree_nr (r, p);
6204 gimple_call_set_arg (stmt, 1, r);
6205 /* FALLTHRU */
6207 case BUILT_IN_EH_POINTER:
6208 case BUILT_IN_EH_FILTER:
6209 r = gimple_call_arg (stmt, 0);
6210 r = move_stmt_eh_region_tree_nr (r, p);
6211 gimple_call_set_arg (stmt, 0, r);
6212 break;
6214 default:
6215 break;
6218 break;
6220 case GIMPLE_RESX:
6222 int r = gimple_resx_region (stmt);
6223 r = move_stmt_eh_region_nr (r, p);
6224 gimple_resx_set_region (stmt, r);
6226 break;
6228 case GIMPLE_EH_DISPATCH:
6230 int r = gimple_eh_dispatch_region (stmt);
6231 r = move_stmt_eh_region_nr (r, p);
6232 gimple_eh_dispatch_set_region (stmt, r);
6234 break;
6236 case GIMPLE_OMP_RETURN:
6237 case GIMPLE_OMP_CONTINUE:
6238 break;
6239 default:
6240 if (is_gimple_omp (stmt))
6242 /* Do not remap variables inside OMP directives. Variables
6243 referenced in clauses and directive header belong to the
6244 parent function and should not be moved into the child
6245 function. */
6246 bool save_remap_decls_p = p->remap_decls_p;
6247 p->remap_decls_p = false;
6248 *handled_ops_p = true;
6250 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), move_stmt_r,
6251 move_stmt_op, wi);
6253 p->remap_decls_p = save_remap_decls_p;
6255 break;
6258 return NULL_TREE;
6261 /* Move basic block BB from function CFUN to function DEST_FN. The
6262 block is moved out of the original linked list and placed after
6263 block AFTER in the new list. Also, the block is removed from the
6264 original array of blocks and placed in DEST_FN's array of blocks.
6265 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
6266 updated to reflect the moved edges.
6268 The local variables are remapped to new instances, VARS_MAP is used
6269 to record the mapping. */
6271 static void
6272 move_block_to_fn (struct function *dest_cfun, basic_block bb,
6273 basic_block after, bool update_edge_count_p,
6274 struct move_stmt_d *d)
6276 struct control_flow_graph *cfg;
6277 edge_iterator ei;
6278 edge e;
6279 gimple_stmt_iterator si;
6280 unsigned old_len, new_len;
6282 /* Remove BB from dominance structures. */
6283 delete_from_dominance_info (CDI_DOMINATORS, bb);
6284 if (current_loops)
6285 remove_bb_from_loops (bb);
6287 /* Link BB to the new linked list. */
6288 move_block_after (bb, after);
6290 /* Update the edge count in the corresponding flowgraphs. */
6291 if (update_edge_count_p)
6292 FOR_EACH_EDGE (e, ei, bb->succs)
6294 cfun->cfg->x_n_edges--;
6295 dest_cfun->cfg->x_n_edges++;
6298 /* Remove BB from the original basic block array. */
6299 (*cfun->cfg->x_basic_block_info)[bb->index] = NULL;
6300 cfun->cfg->x_n_basic_blocks--;
6302 /* Grow DEST_CFUN's basic block array if needed. */
6303 cfg = dest_cfun->cfg;
6304 cfg->x_n_basic_blocks++;
6305 if (bb->index >= cfg->x_last_basic_block)
6306 cfg->x_last_basic_block = bb->index + 1;
6308 old_len = vec_safe_length (cfg->x_basic_block_info);
6309 if ((unsigned) cfg->x_last_basic_block >= old_len)
6311 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
6312 vec_safe_grow_cleared (cfg->x_basic_block_info, new_len);
6315 (*cfg->x_basic_block_info)[bb->index] = bb;
6317 /* Remap the variables in phi nodes. */
6318 for (si = gsi_start_phis (bb); !gsi_end_p (si); )
6320 gimple phi = gsi_stmt (si);
6321 use_operand_p use;
6322 tree op = PHI_RESULT (phi);
6323 ssa_op_iter oi;
6324 unsigned i;
6326 if (virtual_operand_p (op))
6328 /* Remove the phi nodes for virtual operands (alias analysis will be
6329 run for the new function, anyway). */
6330 remove_phi_node (&si, true);
6331 continue;
6334 SET_PHI_RESULT (phi,
6335 replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6336 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
6338 op = USE_FROM_PTR (use);
6339 if (TREE_CODE (op) == SSA_NAME)
6340 SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6343 for (i = 0; i < EDGE_COUNT (bb->preds); i++)
6345 location_t locus = gimple_phi_arg_location (phi, i);
6346 tree block = LOCATION_BLOCK (locus);
6348 if (locus == UNKNOWN_LOCATION)
6349 continue;
6350 if (d->orig_block == NULL_TREE || block == d->orig_block)
6352 if (d->new_block == NULL_TREE)
6353 locus = LOCATION_LOCUS (locus);
6354 else
6355 locus = COMBINE_LOCATION_DATA (line_table, locus, d->new_block);
6356 gimple_phi_arg_set_location (phi, i, locus);
6360 gsi_next (&si);
6363 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6365 gimple stmt = gsi_stmt (si);
6366 struct walk_stmt_info wi;
6368 memset (&wi, 0, sizeof (wi));
6369 wi.info = d;
6370 walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
6372 if (gimple_code (stmt) == GIMPLE_LABEL)
6374 tree label = gimple_label_label (stmt);
6375 int uid = LABEL_DECL_UID (label);
6377 gcc_assert (uid > -1);
6379 old_len = vec_safe_length (cfg->x_label_to_block_map);
6380 if (old_len <= (unsigned) uid)
6382 new_len = 3 * uid / 2 + 1;
6383 vec_safe_grow_cleared (cfg->x_label_to_block_map, new_len);
6386 (*cfg->x_label_to_block_map)[uid] = bb;
6387 (*cfun->cfg->x_label_to_block_map)[uid] = NULL;
6389 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
6391 if (uid >= dest_cfun->cfg->last_label_uid)
6392 dest_cfun->cfg->last_label_uid = uid + 1;
6395 maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
6396 remove_stmt_from_eh_lp_fn (cfun, stmt);
6398 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
6399 gimple_remove_stmt_histograms (cfun, stmt);
6401 /* We cannot leave any operands allocated from the operand caches of
6402 the current function. */
6403 free_stmt_operands (stmt);
6404 push_cfun (dest_cfun);
6405 update_stmt (stmt);
6406 pop_cfun ();
6409 FOR_EACH_EDGE (e, ei, bb->succs)
6410 if (e->goto_locus != UNKNOWN_LOCATION)
6412 tree block = LOCATION_BLOCK (e->goto_locus);
6413 if (d->orig_block == NULL_TREE
6414 || block == d->orig_block)
6415 e->goto_locus = d->new_block ?
6416 COMBINE_LOCATION_DATA (line_table, e->goto_locus, d->new_block) :
6417 LOCATION_LOCUS (e->goto_locus);
6421 /* Examine the statements in BB (which is in SRC_CFUN); find and return
6422 the outermost EH region. Use REGION as the incoming base EH region. */
6424 static eh_region
6425 find_outermost_region_in_block (struct function *src_cfun,
6426 basic_block bb, eh_region region)
6428 gimple_stmt_iterator si;
6430 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6432 gimple stmt = gsi_stmt (si);
6433 eh_region stmt_region;
6434 int lp_nr;
6436 lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
6437 stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
6438 if (stmt_region)
6440 if (region == NULL)
6441 region = stmt_region;
6442 else if (stmt_region != region)
6444 region = eh_region_outermost (src_cfun, stmt_region, region);
6445 gcc_assert (region != NULL);
6450 return region;
6453 static tree
6454 new_label_mapper (tree decl, void *data)
6456 htab_t hash = (htab_t) data;
6457 struct tree_map *m;
6458 void **slot;
6460 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
6462 m = XNEW (struct tree_map);
6463 m->hash = DECL_UID (decl);
6464 m->base.from = decl;
6465 m->to = create_artificial_label (UNKNOWN_LOCATION);
6466 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
6467 if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
6468 cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
6470 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
6471 gcc_assert (*slot == NULL);
6473 *slot = m;
6475 return m->to;
6478 /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
6479 subblocks. */
6481 static void
6482 replace_block_vars_by_duplicates (tree block, struct pointer_map_t *vars_map,
6483 tree to_context)
6485 tree *tp, t;
6487 for (tp = &BLOCK_VARS (block); *tp; tp = &DECL_CHAIN (*tp))
6489 t = *tp;
6490 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
6491 continue;
6492 replace_by_duplicate_decl (&t, vars_map, to_context);
6493 if (t != *tp)
6495 if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
6497 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
6498 DECL_HAS_VALUE_EXPR_P (t) = 1;
6500 DECL_CHAIN (t) = DECL_CHAIN (*tp);
6501 *tp = t;
6505 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
6506 replace_block_vars_by_duplicates (block, vars_map, to_context);
6509 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
6510 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
6511 single basic block in the original CFG and the new basic block is
6512 returned. DEST_CFUN must not have a CFG yet.
6514 Note that the region need not be a pure SESE region. Blocks inside
6515 the region may contain calls to abort/exit. The only restriction
6516 is that ENTRY_BB should be the only entry point and it must
6517 dominate EXIT_BB.
6519 Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
6520 functions outermost BLOCK, move all subblocks of ORIG_BLOCK
6521 to the new function.
6523 All local variables referenced in the region are assumed to be in
6524 the corresponding BLOCK_VARS and unexpanded variable lists
6525 associated with DEST_CFUN. */
6527 basic_block
6528 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
6529 basic_block exit_bb, tree orig_block)
6531 vec<basic_block> bbs, dom_bbs;
6532 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
6533 basic_block after, bb, *entry_pred, *exit_succ, abb;
6534 struct function *saved_cfun = cfun;
6535 int *entry_flag, *exit_flag;
6536 unsigned *entry_prob, *exit_prob;
6537 unsigned i, num_entry_edges, num_exit_edges;
6538 edge e;
6539 edge_iterator ei;
6540 htab_t new_label_map;
6541 struct pointer_map_t *vars_map, *eh_map;
6542 struct loop *loop = entry_bb->loop_father;
6543 struct move_stmt_d d;
6545 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
6546 region. */
6547 gcc_assert (entry_bb != exit_bb
6548 && (!exit_bb
6549 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
6551 /* Collect all the blocks in the region. Manually add ENTRY_BB
6552 because it won't be added by dfs_enumerate_from. */
6553 bbs.create (0);
6554 bbs.safe_push (entry_bb);
6555 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
6557 /* The blocks that used to be dominated by something in BBS will now be
6558 dominated by the new block. */
6559 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
6560 bbs.address (),
6561 bbs.length ());
6563 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
6564 the predecessor edges to ENTRY_BB and the successor edges to
6565 EXIT_BB so that we can re-attach them to the new basic block that
6566 will replace the region. */
6567 num_entry_edges = EDGE_COUNT (entry_bb->preds);
6568 entry_pred = XNEWVEC (basic_block, num_entry_edges);
6569 entry_flag = XNEWVEC (int, num_entry_edges);
6570 entry_prob = XNEWVEC (unsigned, num_entry_edges);
6571 i = 0;
6572 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
6574 entry_prob[i] = e->probability;
6575 entry_flag[i] = e->flags;
6576 entry_pred[i++] = e->src;
6577 remove_edge (e);
6580 if (exit_bb)
6582 num_exit_edges = EDGE_COUNT (exit_bb->succs);
6583 exit_succ = XNEWVEC (basic_block, num_exit_edges);
6584 exit_flag = XNEWVEC (int, num_exit_edges);
6585 exit_prob = XNEWVEC (unsigned, num_exit_edges);
6586 i = 0;
6587 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
6589 exit_prob[i] = e->probability;
6590 exit_flag[i] = e->flags;
6591 exit_succ[i++] = e->dest;
6592 remove_edge (e);
6595 else
6597 num_exit_edges = 0;
6598 exit_succ = NULL;
6599 exit_flag = NULL;
6600 exit_prob = NULL;
6603 /* Switch context to the child function to initialize DEST_FN's CFG. */
6604 gcc_assert (dest_cfun->cfg == NULL);
6605 push_cfun (dest_cfun);
6607 init_empty_tree_cfg ();
6609 /* Initialize EH information for the new function. */
6610 eh_map = NULL;
6611 new_label_map = NULL;
6612 if (saved_cfun->eh)
6614 eh_region region = NULL;
6616 FOR_EACH_VEC_ELT (bbs, i, bb)
6617 region = find_outermost_region_in_block (saved_cfun, bb, region);
6619 init_eh_for_function ();
6620 if (region != NULL)
6622 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
6623 eh_map = duplicate_eh_regions (saved_cfun, region, 0,
6624 new_label_mapper, new_label_map);
6628 pop_cfun ();
6630 /* Move blocks from BBS into DEST_CFUN. */
6631 gcc_assert (bbs.length () >= 2);
6632 after = dest_cfun->cfg->x_entry_block_ptr;
6633 vars_map = pointer_map_create ();
6635 memset (&d, 0, sizeof (d));
6636 d.orig_block = orig_block;
6637 d.new_block = DECL_INITIAL (dest_cfun->decl);
6638 d.from_context = cfun->decl;
6639 d.to_context = dest_cfun->decl;
6640 d.vars_map = vars_map;
6641 d.new_label_map = new_label_map;
6642 d.eh_map = eh_map;
6643 d.remap_decls_p = true;
6645 FOR_EACH_VEC_ELT (bbs, i, bb)
6647 /* No need to update edge counts on the last block. It has
6648 already been updated earlier when we detached the region from
6649 the original CFG. */
6650 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
6651 after = bb;
6654 /* Rewire BLOCK_SUBBLOCKS of orig_block. */
6655 if (orig_block)
6657 tree block;
6658 gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
6659 == NULL_TREE);
6660 BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
6661 = BLOCK_SUBBLOCKS (orig_block);
6662 for (block = BLOCK_SUBBLOCKS (orig_block);
6663 block; block = BLOCK_CHAIN (block))
6664 BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
6665 BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
6668 replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
6669 vars_map, dest_cfun->decl);
6671 if (new_label_map)
6672 htab_delete (new_label_map);
6673 if (eh_map)
6674 pointer_map_destroy (eh_map);
6675 pointer_map_destroy (vars_map);
6677 /* Rewire the entry and exit blocks. The successor to the entry
6678 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
6679 the child function. Similarly, the predecessor of DEST_FN's
6680 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
6681 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
6682 various CFG manipulation function get to the right CFG.
6684 FIXME, this is silly. The CFG ought to become a parameter to
6685 these helpers. */
6686 push_cfun (dest_cfun);
6687 make_edge (ENTRY_BLOCK_PTR, entry_bb, EDGE_FALLTHRU);
6688 if (exit_bb)
6689 make_edge (exit_bb, EXIT_BLOCK_PTR, 0);
6690 pop_cfun ();
6692 /* Back in the original function, the SESE region has disappeared,
6693 create a new basic block in its place. */
6694 bb = create_empty_bb (entry_pred[0]);
6695 if (current_loops)
6696 add_bb_to_loop (bb, loop);
6697 for (i = 0; i < num_entry_edges; i++)
6699 e = make_edge (entry_pred[i], bb, entry_flag[i]);
6700 e->probability = entry_prob[i];
6703 for (i = 0; i < num_exit_edges; i++)
6705 e = make_edge (bb, exit_succ[i], exit_flag[i]);
6706 e->probability = exit_prob[i];
6709 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
6710 FOR_EACH_VEC_ELT (dom_bbs, i, abb)
6711 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
6712 dom_bbs.release ();
6714 if (exit_bb)
6716 free (exit_prob);
6717 free (exit_flag);
6718 free (exit_succ);
6720 free (entry_prob);
6721 free (entry_flag);
6722 free (entry_pred);
6723 bbs.release ();
6725 return bb;
6729 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in dumpfile.h)
6732 void
6733 dump_function_to_file (tree fndecl, FILE *file, int flags)
6735 tree arg, var, old_current_fndecl = current_function_decl;
6736 struct function *dsf;
6737 bool ignore_topmost_bind = false, any_var = false;
6738 basic_block bb;
6739 tree chain;
6740 bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
6741 && decl_is_tm_clone (fndecl));
6742 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
6744 current_function_decl = fndecl;
6745 fprintf (file, "%s %s(", function_name (fun), tmclone ? "[tm-clone] " : "");
6747 arg = DECL_ARGUMENTS (fndecl);
6748 while (arg)
6750 print_generic_expr (file, TREE_TYPE (arg), dump_flags);
6751 fprintf (file, " ");
6752 print_generic_expr (file, arg, dump_flags);
6753 if (flags & TDF_VERBOSE)
6754 print_node (file, "", arg, 4);
6755 if (DECL_CHAIN (arg))
6756 fprintf (file, ", ");
6757 arg = DECL_CHAIN (arg);
6759 fprintf (file, ")\n");
6761 if (flags & TDF_VERBOSE)
6762 print_node (file, "", fndecl, 2);
6764 dsf = DECL_STRUCT_FUNCTION (fndecl);
6765 if (dsf && (flags & TDF_EH))
6766 dump_eh_tree (file, dsf);
6768 if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
6770 dump_node (fndecl, TDF_SLIM | flags, file);
6771 current_function_decl = old_current_fndecl;
6772 return;
6775 /* When GIMPLE is lowered, the variables are no longer available in
6776 BIND_EXPRs, so display them separately. */
6777 if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
6779 unsigned ix;
6780 ignore_topmost_bind = true;
6782 fprintf (file, "{\n");
6783 if (!vec_safe_is_empty (fun->local_decls))
6784 FOR_EACH_LOCAL_DECL (fun, ix, var)
6786 print_generic_decl (file, var, flags);
6787 if (flags & TDF_VERBOSE)
6788 print_node (file, "", var, 4);
6789 fprintf (file, "\n");
6791 any_var = true;
6793 if (gimple_in_ssa_p (cfun))
6794 for (ix = 1; ix < num_ssa_names; ++ix)
6796 tree name = ssa_name (ix);
6797 if (name && !SSA_NAME_VAR (name))
6799 fprintf (file, " ");
6800 print_generic_expr (file, TREE_TYPE (name), flags);
6801 fprintf (file, " ");
6802 print_generic_expr (file, name, flags);
6803 fprintf (file, ";\n");
6805 any_var = true;
6810 if (fun && fun->decl == fndecl
6811 && fun->cfg
6812 && basic_block_info_for_function (fun))
6814 /* If the CFG has been built, emit a CFG-based dump. */
6815 if (!ignore_topmost_bind)
6816 fprintf (file, "{\n");
6818 if (any_var && n_basic_blocks_for_function (fun))
6819 fprintf (file, "\n");
6821 FOR_EACH_BB_FN (bb, fun)
6822 dump_bb (file, bb, 2, flags | TDF_COMMENT);
6824 fprintf (file, "}\n");
6826 else if (DECL_SAVED_TREE (fndecl) == NULL)
6828 /* The function is now in GIMPLE form but the CFG has not been
6829 built yet. Emit the single sequence of GIMPLE statements
6830 that make up its body. */
6831 gimple_seq body = gimple_body (fndecl);
6833 if (gimple_seq_first_stmt (body)
6834 && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
6835 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
6836 print_gimple_seq (file, body, 0, flags);
6837 else
6839 if (!ignore_topmost_bind)
6840 fprintf (file, "{\n");
6842 if (any_var)
6843 fprintf (file, "\n");
6845 print_gimple_seq (file, body, 2, flags);
6846 fprintf (file, "}\n");
6849 else
6851 int indent;
6853 /* Make a tree based dump. */
6854 chain = DECL_SAVED_TREE (fndecl);
6855 if (chain && TREE_CODE (chain) == BIND_EXPR)
6857 if (ignore_topmost_bind)
6859 chain = BIND_EXPR_BODY (chain);
6860 indent = 2;
6862 else
6863 indent = 0;
6865 else
6867 if (!ignore_topmost_bind)
6868 fprintf (file, "{\n");
6869 indent = 2;
6872 if (any_var)
6873 fprintf (file, "\n");
6875 print_generic_stmt_indented (file, chain, flags, indent);
6876 if (ignore_topmost_bind)
6877 fprintf (file, "}\n");
6880 if (flags & TDF_ENUMERATE_LOCALS)
6881 dump_enumerated_decls (file, flags);
6882 fprintf (file, "\n\n");
6884 current_function_decl = old_current_fndecl;
6887 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
6889 DEBUG_FUNCTION void
6890 debug_function (tree fn, int flags)
6892 dump_function_to_file (fn, stderr, flags);
6896 /* Print on FILE the indexes for the predecessors of basic_block BB. */
6898 static void
6899 print_pred_bbs (FILE *file, basic_block bb)
6901 edge e;
6902 edge_iterator ei;
6904 FOR_EACH_EDGE (e, ei, bb->preds)
6905 fprintf (file, "bb_%d ", e->src->index);
6909 /* Print on FILE the indexes for the successors of basic_block BB. */
6911 static void
6912 print_succ_bbs (FILE *file, basic_block bb)
6914 edge e;
6915 edge_iterator ei;
6917 FOR_EACH_EDGE (e, ei, bb->succs)
6918 fprintf (file, "bb_%d ", e->dest->index);
6921 /* Print to FILE the basic block BB following the VERBOSITY level. */
6923 void
6924 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
6926 char *s_indent = (char *) alloca ((size_t) indent + 1);
6927 memset ((void *) s_indent, ' ', (size_t) indent);
6928 s_indent[indent] = '\0';
6930 /* Print basic_block's header. */
6931 if (verbosity >= 2)
6933 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
6934 print_pred_bbs (file, bb);
6935 fprintf (file, "}, succs = {");
6936 print_succ_bbs (file, bb);
6937 fprintf (file, "})\n");
6940 /* Print basic_block's body. */
6941 if (verbosity >= 3)
6943 fprintf (file, "%s {\n", s_indent);
6944 dump_bb (file, bb, indent + 4, TDF_VOPS|TDF_MEMSYMS);
6945 fprintf (file, "%s }\n", s_indent);
6949 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
6951 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
6952 VERBOSITY level this outputs the contents of the loop, or just its
6953 structure. */
6955 static void
6956 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
6958 char *s_indent;
6959 basic_block bb;
6961 if (loop == NULL)
6962 return;
6964 s_indent = (char *) alloca ((size_t) indent + 1);
6965 memset ((void *) s_indent, ' ', (size_t) indent);
6966 s_indent[indent] = '\0';
6968 /* Print loop's header. */
6969 fprintf (file, "%sloop_%d (", s_indent, loop->num);
6970 if (loop->header)
6971 fprintf (file, "header = %d", loop->header->index);
6972 else
6974 fprintf (file, "deleted)\n");
6975 return;
6977 if (loop->latch)
6978 fprintf (file, ", latch = %d", loop->latch->index);
6979 else
6980 fprintf (file, ", multiple latches");
6981 fprintf (file, ", niter = ");
6982 print_generic_expr (file, loop->nb_iterations, 0);
6984 if (loop->any_upper_bound)
6986 fprintf (file, ", upper_bound = ");
6987 dump_double_int (file, loop->nb_iterations_upper_bound, true);
6990 if (loop->any_estimate)
6992 fprintf (file, ", estimate = ");
6993 dump_double_int (file, loop->nb_iterations_estimate, true);
6995 fprintf (file, ")\n");
6997 /* Print loop's body. */
6998 if (verbosity >= 1)
7000 fprintf (file, "%s{\n", s_indent);
7001 FOR_EACH_BB (bb)
7002 if (bb->loop_father == loop)
7003 print_loops_bb (file, bb, indent, verbosity);
7005 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
7006 fprintf (file, "%s}\n", s_indent);
7010 /* Print the LOOP and its sibling loops on FILE, indented INDENT
7011 spaces. Following VERBOSITY level this outputs the contents of the
7012 loop, or just its structure. */
7014 static void
7015 print_loop_and_siblings (FILE *file, struct loop *loop, int indent, int verbosity)
7017 if (loop == NULL)
7018 return;
7020 print_loop (file, loop, indent, verbosity);
7021 print_loop_and_siblings (file, loop->next, indent, verbosity);
7024 /* Follow a CFG edge from the entry point of the program, and on entry
7025 of a loop, pretty print the loop structure on FILE. */
7027 void
7028 print_loops (FILE *file, int verbosity)
7030 basic_block bb;
7032 bb = ENTRY_BLOCK_PTR;
7033 if (bb && bb->loop_father)
7034 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
7038 /* Debugging loops structure at tree level, at some VERBOSITY level. */
7040 DEBUG_FUNCTION void
7041 debug_loops (int verbosity)
7043 print_loops (stderr, verbosity);
7046 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
7048 DEBUG_FUNCTION void
7049 debug_loop (struct loop *loop, int verbosity)
7051 print_loop (stderr, loop, 0, verbosity);
7054 /* Print on stderr the code of loop number NUM, at some VERBOSITY
7055 level. */
7057 DEBUG_FUNCTION void
7058 debug_loop_num (unsigned num, int verbosity)
7060 debug_loop (get_loop (num), verbosity);
7063 /* Return true if BB ends with a call, possibly followed by some
7064 instructions that must stay with the call. Return false,
7065 otherwise. */
7067 static bool
7068 gimple_block_ends_with_call_p (basic_block bb)
7070 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7071 return !gsi_end_p (gsi) && is_gimple_call (gsi_stmt (gsi));
7075 /* Return true if BB ends with a conditional branch. Return false,
7076 otherwise. */
7078 static bool
7079 gimple_block_ends_with_condjump_p (const_basic_block bb)
7081 gimple stmt = last_stmt (CONST_CAST_BB (bb));
7082 return (stmt && gimple_code (stmt) == GIMPLE_COND);
7086 /* Return true if we need to add fake edge to exit at statement T.
7087 Helper function for gimple_flow_call_edges_add. */
7089 static bool
7090 need_fake_edge_p (gimple t)
7092 tree fndecl = NULL_TREE;
7093 int call_flags = 0;
7095 /* NORETURN and LONGJMP calls already have an edge to exit.
7096 CONST and PURE calls do not need one.
7097 We don't currently check for CONST and PURE here, although
7098 it would be a good idea, because those attributes are
7099 figured out from the RTL in mark_constant_function, and
7100 the counter incrementation code from -fprofile-arcs
7101 leads to different results from -fbranch-probabilities. */
7102 if (is_gimple_call (t))
7104 fndecl = gimple_call_fndecl (t);
7105 call_flags = gimple_call_flags (t);
7108 if (is_gimple_call (t)
7109 && fndecl
7110 && DECL_BUILT_IN (fndecl)
7111 && (call_flags & ECF_NOTHROW)
7112 && !(call_flags & ECF_RETURNS_TWICE)
7113 /* fork() doesn't really return twice, but the effect of
7114 wrapping it in __gcov_fork() which calls __gcov_flush()
7115 and clears the counters before forking has the same
7116 effect as returning twice. Force a fake edge. */
7117 && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7118 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
7119 return false;
7121 if (is_gimple_call (t))
7123 edge_iterator ei;
7124 edge e;
7125 basic_block bb;
7127 if (!(call_flags & ECF_NORETURN))
7128 return true;
7130 bb = gimple_bb (t);
7131 FOR_EACH_EDGE (e, ei, bb->succs)
7132 if ((e->flags & EDGE_FAKE) == 0)
7133 return true;
7136 if (gimple_code (t) == GIMPLE_ASM
7137 && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
7138 return true;
7140 return false;
7144 /* Add fake edges to the function exit for any non constant and non
7145 noreturn calls (or noreturn calls with EH/abnormal edges),
7146 volatile inline assembly in the bitmap of blocks specified by BLOCKS
7147 or to the whole CFG if BLOCKS is zero. Return the number of blocks
7148 that were split.
7150 The goal is to expose cases in which entering a basic block does
7151 not imply that all subsequent instructions must be executed. */
7153 static int
7154 gimple_flow_call_edges_add (sbitmap blocks)
7156 int i;
7157 int blocks_split = 0;
7158 int last_bb = last_basic_block;
7159 bool check_last_block = false;
7161 if (n_basic_blocks == NUM_FIXED_BLOCKS)
7162 return 0;
7164 if (! blocks)
7165 check_last_block = true;
7166 else
7167 check_last_block = bitmap_bit_p (blocks, EXIT_BLOCK_PTR->prev_bb->index);
7169 /* In the last basic block, before epilogue generation, there will be
7170 a fallthru edge to EXIT. Special care is required if the last insn
7171 of the last basic block is a call because make_edge folds duplicate
7172 edges, which would result in the fallthru edge also being marked
7173 fake, which would result in the fallthru edge being removed by
7174 remove_fake_edges, which would result in an invalid CFG.
7176 Moreover, we can't elide the outgoing fake edge, since the block
7177 profiler needs to take this into account in order to solve the minimal
7178 spanning tree in the case that the call doesn't return.
7180 Handle this by adding a dummy instruction in a new last basic block. */
7181 if (check_last_block)
7183 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
7184 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7185 gimple t = NULL;
7187 if (!gsi_end_p (gsi))
7188 t = gsi_stmt (gsi);
7190 if (t && need_fake_edge_p (t))
7192 edge e;
7194 e = find_edge (bb, EXIT_BLOCK_PTR);
7195 if (e)
7197 gsi_insert_on_edge (e, gimple_build_nop ());
7198 gsi_commit_edge_inserts ();
7203 /* Now add fake edges to the function exit for any non constant
7204 calls since there is no way that we can determine if they will
7205 return or not... */
7206 for (i = 0; i < last_bb; i++)
7208 basic_block bb = BASIC_BLOCK (i);
7209 gimple_stmt_iterator gsi;
7210 gimple stmt, last_stmt;
7212 if (!bb)
7213 continue;
7215 if (blocks && !bitmap_bit_p (blocks, i))
7216 continue;
7218 gsi = gsi_last_nondebug_bb (bb);
7219 if (!gsi_end_p (gsi))
7221 last_stmt = gsi_stmt (gsi);
7224 stmt = gsi_stmt (gsi);
7225 if (need_fake_edge_p (stmt))
7227 edge e;
7229 /* The handling above of the final block before the
7230 epilogue should be enough to verify that there is
7231 no edge to the exit block in CFG already.
7232 Calling make_edge in such case would cause us to
7233 mark that edge as fake and remove it later. */
7234 #ifdef ENABLE_CHECKING
7235 if (stmt == last_stmt)
7237 e = find_edge (bb, EXIT_BLOCK_PTR);
7238 gcc_assert (e == NULL);
7240 #endif
7242 /* Note that the following may create a new basic block
7243 and renumber the existing basic blocks. */
7244 if (stmt != last_stmt)
7246 e = split_block (bb, stmt);
7247 if (e)
7248 blocks_split++;
7250 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
7252 gsi_prev (&gsi);
7254 while (!gsi_end_p (gsi));
7258 if (blocks_split)
7259 verify_flow_info ();
7261 return blocks_split;
7264 /* Removes edge E and all the blocks dominated by it, and updates dominance
7265 information. The IL in E->src needs to be updated separately.
7266 If dominance info is not available, only the edge E is removed.*/
7268 void
7269 remove_edge_and_dominated_blocks (edge e)
7271 vec<basic_block> bbs_to_remove = vNULL;
7272 vec<basic_block> bbs_to_fix_dom = vNULL;
7273 bitmap df, df_idom;
7274 edge f;
7275 edge_iterator ei;
7276 bool none_removed = false;
7277 unsigned i;
7278 basic_block bb, dbb;
7279 bitmap_iterator bi;
7281 if (!dom_info_available_p (CDI_DOMINATORS))
7283 remove_edge (e);
7284 return;
7287 /* No updating is needed for edges to exit. */
7288 if (e->dest == EXIT_BLOCK_PTR)
7290 if (cfgcleanup_altered_bbs)
7291 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7292 remove_edge (e);
7293 return;
7296 /* First, we find the basic blocks to remove. If E->dest has a predecessor
7297 that is not dominated by E->dest, then this set is empty. Otherwise,
7298 all the basic blocks dominated by E->dest are removed.
7300 Also, to DF_IDOM we store the immediate dominators of the blocks in
7301 the dominance frontier of E (i.e., of the successors of the
7302 removed blocks, if there are any, and of E->dest otherwise). */
7303 FOR_EACH_EDGE (f, ei, e->dest->preds)
7305 if (f == e)
7306 continue;
7308 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
7310 none_removed = true;
7311 break;
7315 df = BITMAP_ALLOC (NULL);
7316 df_idom = BITMAP_ALLOC (NULL);
7318 if (none_removed)
7319 bitmap_set_bit (df_idom,
7320 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
7321 else
7323 bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
7324 FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
7326 FOR_EACH_EDGE (f, ei, bb->succs)
7328 if (f->dest != EXIT_BLOCK_PTR)
7329 bitmap_set_bit (df, f->dest->index);
7332 FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
7333 bitmap_clear_bit (df, bb->index);
7335 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
7337 bb = BASIC_BLOCK (i);
7338 bitmap_set_bit (df_idom,
7339 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
7343 if (cfgcleanup_altered_bbs)
7345 /* Record the set of the altered basic blocks. */
7346 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7347 bitmap_ior_into (cfgcleanup_altered_bbs, df);
7350 /* Remove E and the cancelled blocks. */
7351 if (none_removed)
7352 remove_edge (e);
7353 else
7355 /* Walk backwards so as to get a chance to substitute all
7356 released DEFs into debug stmts. See
7357 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
7358 details. */
7359 for (i = bbs_to_remove.length (); i-- > 0; )
7360 delete_basic_block (bbs_to_remove[i]);
7363 /* Update the dominance information. The immediate dominator may change only
7364 for blocks whose immediate dominator belongs to DF_IDOM:
7366 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
7367 removal. Let Z the arbitrary block such that idom(Z) = Y and
7368 Z dominates X after the removal. Before removal, there exists a path P
7369 from Y to X that avoids Z. Let F be the last edge on P that is
7370 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
7371 dominates W, and because of P, Z does not dominate W), and W belongs to
7372 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
7373 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
7375 bb = BASIC_BLOCK (i);
7376 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
7377 dbb;
7378 dbb = next_dom_son (CDI_DOMINATORS, dbb))
7379 bbs_to_fix_dom.safe_push (dbb);
7382 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
7384 BITMAP_FREE (df);
7385 BITMAP_FREE (df_idom);
7386 bbs_to_remove.release ();
7387 bbs_to_fix_dom.release ();
7390 /* Purge dead EH edges from basic block BB. */
7392 bool
7393 gimple_purge_dead_eh_edges (basic_block bb)
7395 bool changed = false;
7396 edge e;
7397 edge_iterator ei;
7398 gimple stmt = last_stmt (bb);
7400 if (stmt && stmt_can_throw_internal (stmt))
7401 return false;
7403 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7405 if (e->flags & EDGE_EH)
7407 remove_edge_and_dominated_blocks (e);
7408 changed = true;
7410 else
7411 ei_next (&ei);
7414 return changed;
7417 /* Purge dead EH edges from basic block listed in BLOCKS. */
7419 bool
7420 gimple_purge_all_dead_eh_edges (const_bitmap blocks)
7422 bool changed = false;
7423 unsigned i;
7424 bitmap_iterator bi;
7426 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7428 basic_block bb = BASIC_BLOCK (i);
7430 /* Earlier gimple_purge_dead_eh_edges could have removed
7431 this basic block already. */
7432 gcc_assert (bb || changed);
7433 if (bb != NULL)
7434 changed |= gimple_purge_dead_eh_edges (bb);
7437 return changed;
7440 /* Purge dead abnormal call edges from basic block BB. */
7442 bool
7443 gimple_purge_dead_abnormal_call_edges (basic_block bb)
7445 bool changed = false;
7446 edge e;
7447 edge_iterator ei;
7448 gimple stmt = last_stmt (bb);
7450 if (!cfun->has_nonlocal_label)
7451 return false;
7453 if (stmt && stmt_can_make_abnormal_goto (stmt))
7454 return false;
7456 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7458 if (e->flags & EDGE_ABNORMAL)
7460 remove_edge_and_dominated_blocks (e);
7461 changed = true;
7463 else
7464 ei_next (&ei);
7467 return changed;
7470 /* Purge dead abnormal call edges from basic block listed in BLOCKS. */
7472 bool
7473 gimple_purge_all_dead_abnormal_call_edges (const_bitmap blocks)
7475 bool changed = false;
7476 unsigned i;
7477 bitmap_iterator bi;
7479 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7481 basic_block bb = BASIC_BLOCK (i);
7483 /* Earlier gimple_purge_dead_abnormal_call_edges could have removed
7484 this basic block already. */
7485 gcc_assert (bb || changed);
7486 if (bb != NULL)
7487 changed |= gimple_purge_dead_abnormal_call_edges (bb);
7490 return changed;
7493 /* This function is called whenever a new edge is created or
7494 redirected. */
7496 static void
7497 gimple_execute_on_growing_pred (edge e)
7499 basic_block bb = e->dest;
7501 if (!gimple_seq_empty_p (phi_nodes (bb)))
7502 reserve_phi_args_for_new_edge (bb);
7505 /* This function is called immediately before edge E is removed from
7506 the edge vector E->dest->preds. */
7508 static void
7509 gimple_execute_on_shrinking_pred (edge e)
7511 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
7512 remove_phi_args (e);
7515 /*---------------------------------------------------------------------------
7516 Helper functions for Loop versioning
7517 ---------------------------------------------------------------------------*/
7519 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
7520 of 'first'. Both of them are dominated by 'new_head' basic block. When
7521 'new_head' was created by 'second's incoming edge it received phi arguments
7522 on the edge by split_edge(). Later, additional edge 'e' was created to
7523 connect 'new_head' and 'first'. Now this routine adds phi args on this
7524 additional edge 'e' that new_head to second edge received as part of edge
7525 splitting. */
7527 static void
7528 gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
7529 basic_block new_head, edge e)
7531 gimple phi1, phi2;
7532 gimple_stmt_iterator psi1, psi2;
7533 tree def;
7534 edge e2 = find_edge (new_head, second);
7536 /* Because NEW_HEAD has been created by splitting SECOND's incoming
7537 edge, we should always have an edge from NEW_HEAD to SECOND. */
7538 gcc_assert (e2 != NULL);
7540 /* Browse all 'second' basic block phi nodes and add phi args to
7541 edge 'e' for 'first' head. PHI args are always in correct order. */
7543 for (psi2 = gsi_start_phis (second),
7544 psi1 = gsi_start_phis (first);
7545 !gsi_end_p (psi2) && !gsi_end_p (psi1);
7546 gsi_next (&psi2), gsi_next (&psi1))
7548 phi1 = gsi_stmt (psi1);
7549 phi2 = gsi_stmt (psi2);
7550 def = PHI_ARG_DEF (phi2, e2->dest_idx);
7551 add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
7556 /* Adds a if else statement to COND_BB with condition COND_EXPR.
7557 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
7558 the destination of the ELSE part. */
7560 static void
7561 gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
7562 basic_block second_head ATTRIBUTE_UNUSED,
7563 basic_block cond_bb, void *cond_e)
7565 gimple_stmt_iterator gsi;
7566 gimple new_cond_expr;
7567 tree cond_expr = (tree) cond_e;
7568 edge e0;
7570 /* Build new conditional expr */
7571 new_cond_expr = gimple_build_cond_from_tree (cond_expr,
7572 NULL_TREE, NULL_TREE);
7574 /* Add new cond in cond_bb. */
7575 gsi = gsi_last_bb (cond_bb);
7576 gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
7578 /* Adjust edges appropriately to connect new head with first head
7579 as well as second head. */
7580 e0 = single_succ_edge (cond_bb);
7581 e0->flags &= ~EDGE_FALLTHRU;
7582 e0->flags |= EDGE_FALSE_VALUE;
7586 /* Do book-keeping of basic block BB for the profile consistency checker.
7587 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
7588 then do post-pass accounting. Store the counting in RECORD. */
7589 static void
7590 gimple_account_profile_record (basic_block bb, int after_pass,
7591 struct profile_record *record)
7593 gimple_stmt_iterator i;
7594 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
7596 record->size[after_pass]
7597 += estimate_num_insns (gsi_stmt (i), &eni_size_weights);
7598 if (profile_status == PROFILE_READ)
7599 record->time[after_pass]
7600 += estimate_num_insns (gsi_stmt (i),
7601 &eni_time_weights) * bb->count;
7602 else if (profile_status == PROFILE_GUESSED)
7603 record->time[after_pass]
7604 += estimate_num_insns (gsi_stmt (i),
7605 &eni_time_weights) * bb->frequency;
7609 struct cfg_hooks gimple_cfg_hooks = {
7610 "gimple",
7611 gimple_verify_flow_info,
7612 gimple_dump_bb, /* dump_bb */
7613 gimple_dump_bb_for_graph, /* dump_bb_for_graph */
7614 create_bb, /* create_basic_block */
7615 gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
7616 gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
7617 gimple_can_remove_branch_p, /* can_remove_branch_p */
7618 remove_bb, /* delete_basic_block */
7619 gimple_split_block, /* split_block */
7620 gimple_move_block_after, /* move_block_after */
7621 gimple_can_merge_blocks_p, /* can_merge_blocks_p */
7622 gimple_merge_blocks, /* merge_blocks */
7623 gimple_predict_edge, /* predict_edge */
7624 gimple_predicted_by_p, /* predicted_by_p */
7625 gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
7626 gimple_duplicate_bb, /* duplicate_block */
7627 gimple_split_edge, /* split_edge */
7628 gimple_make_forwarder_block, /* make_forward_block */
7629 NULL, /* tidy_fallthru_edge */
7630 NULL, /* force_nonfallthru */
7631 gimple_block_ends_with_call_p,/* block_ends_with_call_p */
7632 gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
7633 gimple_flow_call_edges_add, /* flow_call_edges_add */
7634 gimple_execute_on_growing_pred, /* execute_on_growing_pred */
7635 gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
7636 gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
7637 gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
7638 gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
7639 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
7640 flush_pending_stmts, /* flush_pending_stmts */
7641 gimple_empty_block_p, /* block_empty_p */
7642 gimple_split_block_before_cond_jump, /* split_block_before_cond_jump */
7643 gimple_account_profile_record,
7647 /* Split all critical edges. */
7649 unsigned int
7650 split_critical_edges (void)
7652 basic_block bb;
7653 edge e;
7654 edge_iterator ei;
7656 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
7657 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
7658 mappings around the calls to split_edge. */
7659 start_recording_case_labels ();
7660 FOR_ALL_BB (bb)
7662 FOR_EACH_EDGE (e, ei, bb->succs)
7664 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
7665 split_edge (e);
7666 /* PRE inserts statements to edges and expects that
7667 since split_critical_edges was done beforehand, committing edge
7668 insertions will not split more edges. In addition to critical
7669 edges we must split edges that have multiple successors and
7670 end by control flow statements, such as RESX.
7671 Go ahead and split them too. This matches the logic in
7672 gimple_find_edge_insert_loc. */
7673 else if ((!single_pred_p (e->dest)
7674 || !gimple_seq_empty_p (phi_nodes (e->dest))
7675 || e->dest == EXIT_BLOCK_PTR)
7676 && e->src != ENTRY_BLOCK_PTR
7677 && !(e->flags & EDGE_ABNORMAL))
7679 gimple_stmt_iterator gsi;
7681 gsi = gsi_last_bb (e->src);
7682 if (!gsi_end_p (gsi)
7683 && stmt_ends_bb_p (gsi_stmt (gsi))
7684 && (gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN
7685 && !gimple_call_builtin_p (gsi_stmt (gsi),
7686 BUILT_IN_RETURN)))
7687 split_edge (e);
7691 end_recording_case_labels ();
7692 return 0;
7695 struct gimple_opt_pass pass_split_crit_edges =
7698 GIMPLE_PASS,
7699 "crited", /* name */
7700 OPTGROUP_NONE, /* optinfo_flags */
7701 NULL, /* gate */
7702 split_critical_edges, /* execute */
7703 NULL, /* sub */
7704 NULL, /* next */
7705 0, /* static_pass_number */
7706 TV_TREE_SPLIT_EDGES, /* tv_id */
7707 PROP_cfg, /* properties required */
7708 PROP_no_crit_edges, /* properties_provided */
7709 0, /* properties_destroyed */
7710 0, /* todo_flags_start */
7711 TODO_verify_flow /* todo_flags_finish */
7716 /* Build a ternary operation and gimplify it. Emit code before GSI.
7717 Return the gimple_val holding the result. */
7719 tree
7720 gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
7721 tree type, tree a, tree b, tree c)
7723 tree ret;
7724 location_t loc = gimple_location (gsi_stmt (*gsi));
7726 ret = fold_build3_loc (loc, code, type, a, b, c);
7727 STRIP_NOPS (ret);
7729 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7730 GSI_SAME_STMT);
7733 /* Build a binary operation and gimplify it. Emit code before GSI.
7734 Return the gimple_val holding the result. */
7736 tree
7737 gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
7738 tree type, tree a, tree b)
7740 tree ret;
7742 ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
7743 STRIP_NOPS (ret);
7745 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7746 GSI_SAME_STMT);
7749 /* Build a unary operation and gimplify it. Emit code before GSI.
7750 Return the gimple_val holding the result. */
7752 tree
7753 gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
7754 tree a)
7756 tree ret;
7758 ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
7759 STRIP_NOPS (ret);
7761 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7762 GSI_SAME_STMT);
7767 /* Emit return warnings. */
7769 static unsigned int
7770 execute_warn_function_return (void)
7772 source_location location;
7773 gimple last;
7774 edge e;
7775 edge_iterator ei;
7777 if (!targetm.warn_func_return (cfun->decl))
7778 return 0;
7780 /* If we have a path to EXIT, then we do return. */
7781 if (TREE_THIS_VOLATILE (cfun->decl)
7782 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
7784 location = UNKNOWN_LOCATION;
7785 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7787 last = last_stmt (e->src);
7788 if ((gimple_code (last) == GIMPLE_RETURN
7789 || gimple_call_builtin_p (last, BUILT_IN_RETURN))
7790 && (location = gimple_location (last)) != UNKNOWN_LOCATION)
7791 break;
7793 if (location == UNKNOWN_LOCATION)
7794 location = cfun->function_end_locus;
7795 warning_at (location, 0, "%<noreturn%> function does return");
7798 /* If we see "return;" in some basic block, then we do reach the end
7799 without returning a value. */
7800 else if (warn_return_type
7801 && !TREE_NO_WARNING (cfun->decl)
7802 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
7803 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
7805 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7807 gimple last = last_stmt (e->src);
7808 if (gimple_code (last) == GIMPLE_RETURN
7809 && gimple_return_retval (last) == NULL
7810 && !gimple_no_warning_p (last))
7812 location = gimple_location (last);
7813 if (location == UNKNOWN_LOCATION)
7814 location = cfun->function_end_locus;
7815 warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
7816 TREE_NO_WARNING (cfun->decl) = 1;
7817 break;
7821 return 0;
7825 /* Given a basic block B which ends with a conditional and has
7826 precisely two successors, determine which of the edges is taken if
7827 the conditional is true and which is taken if the conditional is
7828 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
7830 void
7831 extract_true_false_edges_from_block (basic_block b,
7832 edge *true_edge,
7833 edge *false_edge)
7835 edge e = EDGE_SUCC (b, 0);
7837 if (e->flags & EDGE_TRUE_VALUE)
7839 *true_edge = e;
7840 *false_edge = EDGE_SUCC (b, 1);
7842 else
7844 *false_edge = e;
7845 *true_edge = EDGE_SUCC (b, 1);
7849 struct gimple_opt_pass pass_warn_function_return =
7852 GIMPLE_PASS,
7853 "*warn_function_return", /* name */
7854 OPTGROUP_NONE, /* optinfo_flags */
7855 NULL, /* gate */
7856 execute_warn_function_return, /* execute */
7857 NULL, /* sub */
7858 NULL, /* next */
7859 0, /* static_pass_number */
7860 TV_NONE, /* tv_id */
7861 PROP_cfg, /* properties_required */
7862 0, /* properties_provided */
7863 0, /* properties_destroyed */
7864 0, /* todo_flags_start */
7865 0 /* todo_flags_finish */
7869 /* Emit noreturn warnings. */
7871 static unsigned int
7872 execute_warn_function_noreturn (void)
7874 if (!TREE_THIS_VOLATILE (current_function_decl)
7875 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0)
7876 warn_function_noreturn (current_function_decl);
7877 return 0;
7880 static bool
7881 gate_warn_function_noreturn (void)
7883 return warn_suggest_attribute_noreturn;
7886 struct gimple_opt_pass pass_warn_function_noreturn =
7889 GIMPLE_PASS,
7890 "*warn_function_noreturn", /* name */
7891 OPTGROUP_NONE, /* optinfo_flags */
7892 gate_warn_function_noreturn, /* gate */
7893 execute_warn_function_noreturn, /* execute */
7894 NULL, /* sub */
7895 NULL, /* next */
7896 0, /* static_pass_number */
7897 TV_NONE, /* tv_id */
7898 PROP_cfg, /* properties_required */
7899 0, /* properties_provided */
7900 0, /* properties_destroyed */
7901 0, /* todo_flags_start */
7902 0 /* todo_flags_finish */
7907 /* Walk a gimplified function and warn for functions whose return value is
7908 ignored and attribute((warn_unused_result)) is set. This is done before
7909 inlining, so we don't have to worry about that. */
7911 static void
7912 do_warn_unused_result (gimple_seq seq)
7914 tree fdecl, ftype;
7915 gimple_stmt_iterator i;
7917 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
7919 gimple g = gsi_stmt (i);
7921 switch (gimple_code (g))
7923 case GIMPLE_BIND:
7924 do_warn_unused_result (gimple_bind_body (g));
7925 break;
7926 case GIMPLE_TRY:
7927 do_warn_unused_result (gimple_try_eval (g));
7928 do_warn_unused_result (gimple_try_cleanup (g));
7929 break;
7930 case GIMPLE_CATCH:
7931 do_warn_unused_result (gimple_catch_handler (g));
7932 break;
7933 case GIMPLE_EH_FILTER:
7934 do_warn_unused_result (gimple_eh_filter_failure (g));
7935 break;
7937 case GIMPLE_CALL:
7938 if (gimple_call_lhs (g))
7939 break;
7940 if (gimple_call_internal_p (g))
7941 break;
7943 /* This is a naked call, as opposed to a GIMPLE_CALL with an
7944 LHS. All calls whose value is ignored should be
7945 represented like this. Look for the attribute. */
7946 fdecl = gimple_call_fndecl (g);
7947 ftype = gimple_call_fntype (g);
7949 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
7951 location_t loc = gimple_location (g);
7953 if (fdecl)
7954 warning_at (loc, OPT_Wunused_result,
7955 "ignoring return value of %qD, "
7956 "declared with attribute warn_unused_result",
7957 fdecl);
7958 else
7959 warning_at (loc, OPT_Wunused_result,
7960 "ignoring return value of function "
7961 "declared with attribute warn_unused_result");
7963 break;
7965 default:
7966 /* Not a container, not a call, or a call whose value is used. */
7967 break;
7972 static unsigned int
7973 run_warn_unused_result (void)
7975 do_warn_unused_result (gimple_body (current_function_decl));
7976 return 0;
7979 static bool
7980 gate_warn_unused_result (void)
7982 return flag_warn_unused_result;
7985 struct gimple_opt_pass pass_warn_unused_result =
7988 GIMPLE_PASS,
7989 "*warn_unused_result", /* name */
7990 OPTGROUP_NONE, /* optinfo_flags */
7991 gate_warn_unused_result, /* gate */
7992 run_warn_unused_result, /* execute */
7993 NULL, /* sub */
7994 NULL, /* next */
7995 0, /* static_pass_number */
7996 TV_NONE, /* tv_id */
7997 PROP_gimple_any, /* properties_required */
7998 0, /* properties_provided */
7999 0, /* properties_destroyed */
8000 0, /* todo_flags_start */
8001 0, /* todo_flags_finish */
8006 /* Garbage collection support for edge_def. */
8008 extern void gt_ggc_mx (tree&);
8009 extern void gt_ggc_mx (gimple&);
8010 extern void gt_ggc_mx (rtx&);
8011 extern void gt_ggc_mx (basic_block&);
8013 void
8014 gt_ggc_mx (edge_def *e)
8016 tree block = LOCATION_BLOCK (e->goto_locus);
8017 gt_ggc_mx (e->src);
8018 gt_ggc_mx (e->dest);
8019 if (current_ir_type () == IR_GIMPLE)
8020 gt_ggc_mx (e->insns.g);
8021 else
8022 gt_ggc_mx (e->insns.r);
8023 gt_ggc_mx (block);
8026 /* PCH support for edge_def. */
8028 extern void gt_pch_nx (tree&);
8029 extern void gt_pch_nx (gimple&);
8030 extern void gt_pch_nx (rtx&);
8031 extern void gt_pch_nx (basic_block&);
8033 void
8034 gt_pch_nx (edge_def *e)
8036 tree block = LOCATION_BLOCK (e->goto_locus);
8037 gt_pch_nx (e->src);
8038 gt_pch_nx (e->dest);
8039 if (current_ir_type () == IR_GIMPLE)
8040 gt_pch_nx (e->insns.g);
8041 else
8042 gt_pch_nx (e->insns.r);
8043 gt_pch_nx (block);
8046 void
8047 gt_pch_nx (edge_def *e, gt_pointer_operator op, void *cookie)
8049 tree block = LOCATION_BLOCK (e->goto_locus);
8050 op (&(e->src), cookie);
8051 op (&(e->dest), cookie);
8052 if (current_ir_type () == IR_GIMPLE)
8053 op (&(e->insns.g), cookie);
8054 else
8055 op (&(e->insns.r), cookie);
8056 op (&(block), cookie);