* vi.po: Update.
[official-gcc.git] / gcc / tree-cfg.c
blob1a166cc4994006eb300951b884ea76ce1cd2e2d3
1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "flags.h"
30 #include "function.h"
31 #include "ggc.h"
32 #include "gimple-pretty-print.h"
33 #include "tree-flow.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "diagnostic-core.h"
37 #include "except.h"
38 #include "cfgloop.h"
39 #include "tree-ssa-propagate.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
42 #include "tree-inline.h"
43 #include "target.h"
45 /* This file contains functions for building the Control Flow Graph (CFG)
46 for a function tree. */
48 /* Local declarations. */
50 /* Initial capacity for the basic block array. */
51 static const int initial_cfg_capacity = 20;
53 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
54 which use a particular edge. The CASE_LABEL_EXPRs are chained together
55 via their CASE_CHAIN field, which we clear after we're done with the
56 hash table to prevent problems with duplication of GIMPLE_SWITCHes.
58 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
59 update the case vector in response to edge redirections.
61 Right now this table is set up and torn down at key points in the
62 compilation process. It would be nice if we could make the table
63 more persistent. The key is getting notification of changes to
64 the CFG (particularly edge removal, creation and redirection). */
66 static struct pointer_map_t *edge_to_cases;
68 /* If we record edge_to_cases, this bitmap will hold indexes
69 of basic blocks that end in a GIMPLE_SWITCH which we touched
70 due to edge manipulations. */
72 static bitmap touched_switch_bbs;
74 /* CFG statistics. */
75 struct cfg_stats_d
77 long num_merged_labels;
80 static struct cfg_stats_d cfg_stats;
82 /* Nonzero if we found a computed goto while building basic blocks. */
83 static bool found_computed_goto;
85 /* Hash table to store last discriminator assigned for each locus. */
86 struct locus_discrim_map
88 location_t locus;
89 int discriminator;
91 static htab_t discriminator_per_locus;
93 /* Basic blocks and flowgraphs. */
94 static void make_blocks (gimple_seq);
95 static void factor_computed_gotos (void);
97 /* Edges. */
98 static void make_edges (void);
99 static void make_cond_expr_edges (basic_block);
100 static void make_gimple_switch_edges (basic_block);
101 static void make_goto_expr_edges (basic_block);
102 static void make_gimple_asm_edges (basic_block);
103 static unsigned int locus_map_hash (const void *);
104 static int locus_map_eq (const void *, const void *);
105 static void assign_discriminator (location_t, basic_block);
106 static edge gimple_redirect_edge_and_branch (edge, basic_block);
107 static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
108 static unsigned int split_critical_edges (void);
110 /* Various helpers. */
111 static inline bool stmt_starts_bb_p (gimple, gimple);
112 static int gimple_verify_flow_info (void);
113 static void gimple_make_forwarder_block (edge);
114 static void gimple_cfg2vcg (FILE *);
115 static gimple first_non_label_stmt (basic_block);
116 static bool verify_gimple_transaction (gimple);
118 /* Flowgraph optimization and cleanup. */
119 static void gimple_merge_blocks (basic_block, basic_block);
120 static bool gimple_can_merge_blocks_p (basic_block, basic_block);
121 static void remove_bb (basic_block);
122 static edge find_taken_edge_computed_goto (basic_block, tree);
123 static edge find_taken_edge_cond_expr (basic_block, tree);
124 static edge find_taken_edge_switch_expr (basic_block, tree);
125 static tree find_case_label_for_value (gimple, tree);
127 void
128 init_empty_tree_cfg_for_function (struct function *fn)
130 /* Initialize the basic block array. */
131 init_flow (fn);
132 profile_status_for_function (fn) = PROFILE_ABSENT;
133 n_basic_blocks_for_function (fn) = NUM_FIXED_BLOCKS;
134 last_basic_block_for_function (fn) = NUM_FIXED_BLOCKS;
135 basic_block_info_for_function (fn)
136 = VEC_alloc (basic_block, gc, initial_cfg_capacity);
137 VEC_safe_grow_cleared (basic_block, gc,
138 basic_block_info_for_function (fn),
139 initial_cfg_capacity);
141 /* Build a mapping of labels to their associated blocks. */
142 label_to_block_map_for_function (fn)
143 = VEC_alloc (basic_block, gc, initial_cfg_capacity);
144 VEC_safe_grow_cleared (basic_block, gc,
145 label_to_block_map_for_function (fn),
146 initial_cfg_capacity);
148 SET_BASIC_BLOCK_FOR_FUNCTION (fn, ENTRY_BLOCK,
149 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn));
150 SET_BASIC_BLOCK_FOR_FUNCTION (fn, EXIT_BLOCK,
151 EXIT_BLOCK_PTR_FOR_FUNCTION (fn));
153 ENTRY_BLOCK_PTR_FOR_FUNCTION (fn)->next_bb
154 = EXIT_BLOCK_PTR_FOR_FUNCTION (fn);
155 EXIT_BLOCK_PTR_FOR_FUNCTION (fn)->prev_bb
156 = ENTRY_BLOCK_PTR_FOR_FUNCTION (fn);
159 void
160 init_empty_tree_cfg (void)
162 init_empty_tree_cfg_for_function (cfun);
165 /*---------------------------------------------------------------------------
166 Create basic blocks
167 ---------------------------------------------------------------------------*/
169 /* Entry point to the CFG builder for trees. SEQ is the sequence of
170 statements to be added to the flowgraph. */
172 static void
173 build_gimple_cfg (gimple_seq seq)
175 /* Register specific gimple functions. */
176 gimple_register_cfg_hooks ();
178 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
180 init_empty_tree_cfg ();
182 found_computed_goto = 0;
183 make_blocks (seq);
185 /* Computed gotos are hell to deal with, especially if there are
186 lots of them with a large number of destinations. So we factor
187 them to a common computed goto location before we build the
188 edge list. After we convert back to normal form, we will un-factor
189 the computed gotos since factoring introduces an unwanted jump. */
190 if (found_computed_goto)
191 factor_computed_gotos ();
193 /* Make sure there is always at least one block, even if it's empty. */
194 if (n_basic_blocks == NUM_FIXED_BLOCKS)
195 create_empty_bb (ENTRY_BLOCK_PTR);
197 /* Adjust the size of the array. */
198 if (VEC_length (basic_block, basic_block_info) < (size_t) n_basic_blocks)
199 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, n_basic_blocks);
201 /* To speed up statement iterator walks, we first purge dead labels. */
202 cleanup_dead_labels ();
204 /* Group case nodes to reduce the number of edges.
205 We do this after cleaning up dead labels because otherwise we miss
206 a lot of obvious case merging opportunities. */
207 group_case_labels ();
209 /* Create the edges of the flowgraph. */
210 discriminator_per_locus = htab_create (13, locus_map_hash, locus_map_eq,
211 free);
212 make_edges ();
213 cleanup_dead_labels ();
214 htab_delete (discriminator_per_locus);
216 /* Debugging dumps. */
218 /* Write the flowgraph to a VCG file. */
220 int local_dump_flags;
221 FILE *vcg_file = dump_begin (TDI_vcg, &local_dump_flags);
222 if (vcg_file)
224 gimple_cfg2vcg (vcg_file);
225 dump_end (TDI_vcg, vcg_file);
230 static unsigned int
231 execute_build_cfg (void)
233 gimple_seq body = gimple_body (current_function_decl);
235 build_gimple_cfg (body);
236 gimple_set_body (current_function_decl, NULL);
237 if (dump_file && (dump_flags & TDF_DETAILS))
239 fprintf (dump_file, "Scope blocks:\n");
240 dump_scope_blocks (dump_file, dump_flags);
242 return 0;
245 struct gimple_opt_pass pass_build_cfg =
248 GIMPLE_PASS,
249 "cfg", /* name */
250 NULL, /* gate */
251 execute_build_cfg, /* execute */
252 NULL, /* sub */
253 NULL, /* next */
254 0, /* static_pass_number */
255 TV_TREE_CFG, /* tv_id */
256 PROP_gimple_leh, /* properties_required */
257 PROP_cfg, /* properties_provided */
258 0, /* properties_destroyed */
259 0, /* todo_flags_start */
260 TODO_verify_stmts | TODO_cleanup_cfg /* todo_flags_finish */
265 /* Return true if T is a computed goto. */
267 static bool
268 computed_goto_p (gimple t)
270 return (gimple_code (t) == GIMPLE_GOTO
271 && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
275 /* Search the CFG for any computed gotos. If found, factor them to a
276 common computed goto site. Also record the location of that site so
277 that we can un-factor the gotos after we have converted back to
278 normal form. */
280 static void
281 factor_computed_gotos (void)
283 basic_block bb;
284 tree factored_label_decl = NULL;
285 tree var = NULL;
286 gimple factored_computed_goto_label = NULL;
287 gimple factored_computed_goto = NULL;
289 /* We know there are one or more computed gotos in this function.
290 Examine the last statement in each basic block to see if the block
291 ends with a computed goto. */
293 FOR_EACH_BB (bb)
295 gimple_stmt_iterator gsi = gsi_last_bb (bb);
296 gimple last;
298 if (gsi_end_p (gsi))
299 continue;
301 last = gsi_stmt (gsi);
303 /* Ignore the computed goto we create when we factor the original
304 computed gotos. */
305 if (last == factored_computed_goto)
306 continue;
308 /* If the last statement is a computed goto, factor it. */
309 if (computed_goto_p (last))
311 gimple assignment;
313 /* The first time we find a computed goto we need to create
314 the factored goto block and the variable each original
315 computed goto will use for their goto destination. */
316 if (!factored_computed_goto)
318 basic_block new_bb = create_empty_bb (bb);
319 gimple_stmt_iterator new_gsi = gsi_start_bb (new_bb);
321 /* Create the destination of the factored goto. Each original
322 computed goto will put its desired destination into this
323 variable and jump to the label we create immediately
324 below. */
325 var = create_tmp_var (ptr_type_node, "gotovar");
327 /* Build a label for the new block which will contain the
328 factored computed goto. */
329 factored_label_decl = create_artificial_label (UNKNOWN_LOCATION);
330 factored_computed_goto_label
331 = gimple_build_label (factored_label_decl);
332 gsi_insert_after (&new_gsi, factored_computed_goto_label,
333 GSI_NEW_STMT);
335 /* Build our new computed goto. */
336 factored_computed_goto = gimple_build_goto (var);
337 gsi_insert_after (&new_gsi, factored_computed_goto, GSI_NEW_STMT);
340 /* Copy the original computed goto's destination into VAR. */
341 assignment = gimple_build_assign (var, gimple_goto_dest (last));
342 gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
344 /* And re-vector the computed goto to the new destination. */
345 gimple_goto_set_dest (last, factored_label_decl);
351 /* Build a flowgraph for the sequence of stmts SEQ. */
353 static void
354 make_blocks (gimple_seq seq)
356 gimple_stmt_iterator i = gsi_start (seq);
357 gimple stmt = NULL;
358 bool start_new_block = true;
359 bool first_stmt_of_seq = true;
360 basic_block bb = ENTRY_BLOCK_PTR;
362 while (!gsi_end_p (i))
364 gimple prev_stmt;
366 prev_stmt = stmt;
367 stmt = gsi_stmt (i);
369 /* If the statement starts a new basic block or if we have determined
370 in a previous pass that we need to create a new block for STMT, do
371 so now. */
372 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
374 if (!first_stmt_of_seq)
375 gsi_split_seq_before (&i, &seq);
376 bb = create_basic_block (seq, NULL, bb);
377 start_new_block = false;
380 /* Now add STMT to BB and create the subgraphs for special statement
381 codes. */
382 gimple_set_bb (stmt, bb);
384 if (computed_goto_p (stmt))
385 found_computed_goto = true;
387 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
388 next iteration. */
389 if (stmt_ends_bb_p (stmt))
391 /* If the stmt can make abnormal goto use a new temporary
392 for the assignment to the LHS. This makes sure the old value
393 of the LHS is available on the abnormal edge. Otherwise
394 we will end up with overlapping life-ranges for abnormal
395 SSA names. */
396 if (gimple_has_lhs (stmt)
397 && stmt_can_make_abnormal_goto (stmt)
398 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
400 tree lhs = gimple_get_lhs (stmt);
401 tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
402 gimple s = gimple_build_assign (lhs, tmp);
403 gimple_set_location (s, gimple_location (stmt));
404 gimple_set_block (s, gimple_block (stmt));
405 gimple_set_lhs (stmt, tmp);
406 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
407 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
408 DECL_GIMPLE_REG_P (tmp) = 1;
409 gsi_insert_after (&i, s, GSI_SAME_STMT);
411 start_new_block = true;
414 gsi_next (&i);
415 first_stmt_of_seq = false;
420 /* Create and return a new empty basic block after bb AFTER. */
422 static basic_block
423 create_bb (void *h, void *e, basic_block after)
425 basic_block bb;
427 gcc_assert (!e);
429 /* Create and initialize a new basic block. Since alloc_block uses
430 GC allocation that clears memory to allocate a basic block, we do
431 not have to clear the newly allocated basic block here. */
432 bb = alloc_block ();
434 bb->index = last_basic_block;
435 bb->flags = BB_NEW;
436 set_bb_seq (bb, h ? (gimple_seq) h : NULL);
438 /* Add the new block to the linked list of blocks. */
439 link_block (bb, after);
441 /* Grow the basic block array if needed. */
442 if ((size_t) last_basic_block == VEC_length (basic_block, basic_block_info))
444 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
445 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
448 /* Add the newly created block to the array. */
449 SET_BASIC_BLOCK (last_basic_block, bb);
451 n_basic_blocks++;
452 last_basic_block++;
454 return bb;
458 /*---------------------------------------------------------------------------
459 Edge creation
460 ---------------------------------------------------------------------------*/
462 /* Fold COND_EXPR_COND of each COND_EXPR. */
464 void
465 fold_cond_expr_cond (void)
467 basic_block bb;
469 FOR_EACH_BB (bb)
471 gimple stmt = last_stmt (bb);
473 if (stmt && gimple_code (stmt) == GIMPLE_COND)
475 location_t loc = gimple_location (stmt);
476 tree cond;
477 bool zerop, onep;
479 fold_defer_overflow_warnings ();
480 cond = fold_binary_loc (loc, gimple_cond_code (stmt), boolean_type_node,
481 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
482 if (cond)
484 zerop = integer_zerop (cond);
485 onep = integer_onep (cond);
487 else
488 zerop = onep = false;
490 fold_undefer_overflow_warnings (zerop || onep,
491 stmt,
492 WARN_STRICT_OVERFLOW_CONDITIONAL);
493 if (zerop)
494 gimple_cond_make_false (stmt);
495 else if (onep)
496 gimple_cond_make_true (stmt);
501 /* Join all the blocks in the flowgraph. */
503 static void
504 make_edges (void)
506 basic_block bb;
507 struct omp_region *cur_region = NULL;
509 /* Create an edge from entry to the first block with executable
510 statements in it. */
511 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (NUM_FIXED_BLOCKS), EDGE_FALLTHRU);
513 /* Traverse the basic block array placing edges. */
514 FOR_EACH_BB (bb)
516 gimple last = last_stmt (bb);
517 bool fallthru;
519 if (last)
521 enum gimple_code code = gimple_code (last);
522 switch (code)
524 case GIMPLE_GOTO:
525 make_goto_expr_edges (bb);
526 fallthru = false;
527 break;
528 case GIMPLE_RETURN:
529 make_edge (bb, EXIT_BLOCK_PTR, 0);
530 fallthru = false;
531 break;
532 case GIMPLE_COND:
533 make_cond_expr_edges (bb);
534 fallthru = false;
535 break;
536 case GIMPLE_SWITCH:
537 make_gimple_switch_edges (bb);
538 fallthru = false;
539 break;
540 case GIMPLE_RESX:
541 make_eh_edges (last);
542 fallthru = false;
543 break;
544 case GIMPLE_EH_DISPATCH:
545 fallthru = make_eh_dispatch_edges (last);
546 break;
548 case GIMPLE_CALL:
549 /* If this function receives a nonlocal goto, then we need to
550 make edges from this call site to all the nonlocal goto
551 handlers. */
552 if (stmt_can_make_abnormal_goto (last))
553 make_abnormal_goto_edges (bb, true);
555 /* If this statement has reachable exception handlers, then
556 create abnormal edges to them. */
557 make_eh_edges (last);
559 /* BUILTIN_RETURN is really a return statement. */
560 if (gimple_call_builtin_p (last, BUILT_IN_RETURN))
561 make_edge (bb, EXIT_BLOCK_PTR, 0), fallthru = false;
562 /* Some calls are known not to return. */
563 else
564 fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
565 break;
567 case GIMPLE_ASSIGN:
568 /* A GIMPLE_ASSIGN may throw internally and thus be considered
569 control-altering. */
570 if (is_ctrl_altering_stmt (last))
571 make_eh_edges (last);
572 fallthru = true;
573 break;
575 case GIMPLE_ASM:
576 make_gimple_asm_edges (bb);
577 fallthru = true;
578 break;
580 case GIMPLE_OMP_PARALLEL:
581 case GIMPLE_OMP_TASK:
582 case GIMPLE_OMP_FOR:
583 case GIMPLE_OMP_SINGLE:
584 case GIMPLE_OMP_MASTER:
585 case GIMPLE_OMP_ORDERED:
586 case GIMPLE_OMP_CRITICAL:
587 case GIMPLE_OMP_SECTION:
588 cur_region = new_omp_region (bb, code, cur_region);
589 fallthru = true;
590 break;
592 case GIMPLE_OMP_SECTIONS:
593 cur_region = new_omp_region (bb, code, cur_region);
594 fallthru = true;
595 break;
597 case GIMPLE_OMP_SECTIONS_SWITCH:
598 fallthru = false;
599 break;
601 case GIMPLE_OMP_ATOMIC_LOAD:
602 case GIMPLE_OMP_ATOMIC_STORE:
603 fallthru = true;
604 break;
606 case GIMPLE_OMP_RETURN:
607 /* In the case of a GIMPLE_OMP_SECTION, the edge will go
608 somewhere other than the next block. This will be
609 created later. */
610 cur_region->exit = bb;
611 fallthru = cur_region->type != GIMPLE_OMP_SECTION;
612 cur_region = cur_region->outer;
613 break;
615 case GIMPLE_OMP_CONTINUE:
616 cur_region->cont = bb;
617 switch (cur_region->type)
619 case GIMPLE_OMP_FOR:
620 /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
621 succs edges as abnormal to prevent splitting
622 them. */
623 single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
624 /* Make the loopback edge. */
625 make_edge (bb, single_succ (cur_region->entry),
626 EDGE_ABNORMAL);
628 /* Create an edge from GIMPLE_OMP_FOR to exit, which
629 corresponds to the case that the body of the loop
630 is not executed at all. */
631 make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
632 make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
633 fallthru = false;
634 break;
636 case GIMPLE_OMP_SECTIONS:
637 /* Wire up the edges into and out of the nested sections. */
639 basic_block switch_bb = single_succ (cur_region->entry);
641 struct omp_region *i;
642 for (i = cur_region->inner; i ; i = i->next)
644 gcc_assert (i->type == GIMPLE_OMP_SECTION);
645 make_edge (switch_bb, i->entry, 0);
646 make_edge (i->exit, bb, EDGE_FALLTHRU);
649 /* Make the loopback edge to the block with
650 GIMPLE_OMP_SECTIONS_SWITCH. */
651 make_edge (bb, switch_bb, 0);
653 /* Make the edge from the switch to exit. */
654 make_edge (switch_bb, bb->next_bb, 0);
655 fallthru = false;
657 break;
659 default:
660 gcc_unreachable ();
662 break;
664 case GIMPLE_TRANSACTION:
666 tree abort_label = gimple_transaction_label (last);
667 if (abort_label)
668 make_edge (bb, label_to_block (abort_label), 0);
669 fallthru = true;
671 break;
673 default:
674 gcc_assert (!stmt_ends_bb_p (last));
675 fallthru = true;
678 else
679 fallthru = true;
681 if (fallthru)
683 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
684 if (last)
685 assign_discriminator (gimple_location (last), bb->next_bb);
689 if (root_omp_region)
690 free_omp_regions ();
692 /* Fold COND_EXPR_COND of each COND_EXPR. */
693 fold_cond_expr_cond ();
696 /* Trivial hash function for a location_t. ITEM is a pointer to
697 a hash table entry that maps a location_t to a discriminator. */
699 static unsigned int
700 locus_map_hash (const void *item)
702 return ((const struct locus_discrim_map *) item)->locus;
705 /* Equality function for the locus-to-discriminator map. VA and VB
706 point to the two hash table entries to compare. */
708 static int
709 locus_map_eq (const void *va, const void *vb)
711 const struct locus_discrim_map *a = (const struct locus_discrim_map *) va;
712 const struct locus_discrim_map *b = (const struct locus_discrim_map *) vb;
713 return a->locus == b->locus;
716 /* Find the next available discriminator value for LOCUS. The
717 discriminator distinguishes among several basic blocks that
718 share a common locus, allowing for more accurate sample-based
719 profiling. */
721 static int
722 next_discriminator_for_locus (location_t locus)
724 struct locus_discrim_map item;
725 struct locus_discrim_map **slot;
727 item.locus = locus;
728 item.discriminator = 0;
729 slot = (struct locus_discrim_map **)
730 htab_find_slot_with_hash (discriminator_per_locus, (void *) &item,
731 (hashval_t) locus, INSERT);
732 gcc_assert (slot);
733 if (*slot == HTAB_EMPTY_ENTRY)
735 *slot = XNEW (struct locus_discrim_map);
736 gcc_assert (*slot);
737 (*slot)->locus = locus;
738 (*slot)->discriminator = 0;
740 (*slot)->discriminator++;
741 return (*slot)->discriminator;
744 /* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line. */
746 static bool
747 same_line_p (location_t locus1, location_t locus2)
749 expanded_location from, to;
751 if (locus1 == locus2)
752 return true;
754 from = expand_location (locus1);
755 to = expand_location (locus2);
757 if (from.line != to.line)
758 return false;
759 if (from.file == to.file)
760 return true;
761 return (from.file != NULL
762 && to.file != NULL
763 && filename_cmp (from.file, to.file) == 0);
766 /* Assign a unique discriminator value to block BB if it begins at the same
767 LOCUS as its predecessor block. */
769 static void
770 assign_discriminator (location_t locus, basic_block bb)
772 gimple first_in_to_bb, last_in_to_bb;
774 if (locus == 0 || bb->discriminator != 0)
775 return;
777 first_in_to_bb = first_non_label_stmt (bb);
778 last_in_to_bb = last_stmt (bb);
779 if ((first_in_to_bb && same_line_p (locus, gimple_location (first_in_to_bb)))
780 || (last_in_to_bb && same_line_p (locus, gimple_location (last_in_to_bb))))
781 bb->discriminator = next_discriminator_for_locus (locus);
784 /* Create the edges for a GIMPLE_COND starting at block BB. */
786 static void
787 make_cond_expr_edges (basic_block bb)
789 gimple entry = last_stmt (bb);
790 gimple then_stmt, else_stmt;
791 basic_block then_bb, else_bb;
792 tree then_label, else_label;
793 edge e;
794 location_t entry_locus;
796 gcc_assert (entry);
797 gcc_assert (gimple_code (entry) == GIMPLE_COND);
799 entry_locus = gimple_location (entry);
801 /* Entry basic blocks for each component. */
802 then_label = gimple_cond_true_label (entry);
803 else_label = gimple_cond_false_label (entry);
804 then_bb = label_to_block (then_label);
805 else_bb = label_to_block (else_label);
806 then_stmt = first_stmt (then_bb);
807 else_stmt = first_stmt (else_bb);
809 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
810 assign_discriminator (entry_locus, then_bb);
811 e->goto_locus = gimple_location (then_stmt);
812 if (e->goto_locus)
813 e->goto_block = gimple_block (then_stmt);
814 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
815 if (e)
817 assign_discriminator (entry_locus, else_bb);
818 e->goto_locus = gimple_location (else_stmt);
819 if (e->goto_locus)
820 e->goto_block = gimple_block (else_stmt);
823 /* We do not need the labels anymore. */
824 gimple_cond_set_true_label (entry, NULL_TREE);
825 gimple_cond_set_false_label (entry, NULL_TREE);
829 /* Called for each element in the hash table (P) as we delete the
830 edge to cases hash table.
832 Clear all the TREE_CHAINs to prevent problems with copying of
833 SWITCH_EXPRs and structure sharing rules, then free the hash table
834 element. */
836 static bool
837 edge_to_cases_cleanup (const void *key ATTRIBUTE_UNUSED, void **value,
838 void *data ATTRIBUTE_UNUSED)
840 tree t, next;
842 for (t = (tree) *value; t; t = next)
844 next = CASE_CHAIN (t);
845 CASE_CHAIN (t) = NULL;
848 *value = NULL;
849 return true;
852 /* Start recording information mapping edges to case labels. */
854 void
855 start_recording_case_labels (void)
857 gcc_assert (edge_to_cases == NULL);
858 edge_to_cases = pointer_map_create ();
859 touched_switch_bbs = BITMAP_ALLOC (NULL);
862 /* Return nonzero if we are recording information for case labels. */
864 static bool
865 recording_case_labels_p (void)
867 return (edge_to_cases != NULL);
870 /* Stop recording information mapping edges to case labels and
871 remove any information we have recorded. */
872 void
873 end_recording_case_labels (void)
875 bitmap_iterator bi;
876 unsigned i;
877 pointer_map_traverse (edge_to_cases, edge_to_cases_cleanup, NULL);
878 pointer_map_destroy (edge_to_cases);
879 edge_to_cases = NULL;
880 EXECUTE_IF_SET_IN_BITMAP (touched_switch_bbs, 0, i, bi)
882 basic_block bb = BASIC_BLOCK (i);
883 if (bb)
885 gimple stmt = last_stmt (bb);
886 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
887 group_case_labels_stmt (stmt);
890 BITMAP_FREE (touched_switch_bbs);
893 /* If we are inside a {start,end}_recording_cases block, then return
894 a chain of CASE_LABEL_EXPRs from T which reference E.
896 Otherwise return NULL. */
898 static tree
899 get_cases_for_edge (edge e, gimple t)
901 void **slot;
902 size_t i, n;
904 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
905 chains available. Return NULL so the caller can detect this case. */
906 if (!recording_case_labels_p ())
907 return NULL;
909 slot = pointer_map_contains (edge_to_cases, e);
910 if (slot)
911 return (tree) *slot;
913 /* If we did not find E in the hash table, then this must be the first
914 time we have been queried for information about E & T. Add all the
915 elements from T to the hash table then perform the query again. */
917 n = gimple_switch_num_labels (t);
918 for (i = 0; i < n; i++)
920 tree elt = gimple_switch_label (t, i);
921 tree lab = CASE_LABEL (elt);
922 basic_block label_bb = label_to_block (lab);
923 edge this_edge = find_edge (e->src, label_bb);
925 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
926 a new chain. */
927 slot = pointer_map_insert (edge_to_cases, this_edge);
928 CASE_CHAIN (elt) = (tree) *slot;
929 *slot = elt;
932 return (tree) *pointer_map_contains (edge_to_cases, e);
935 /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
937 static void
938 make_gimple_switch_edges (basic_block bb)
940 gimple entry = last_stmt (bb);
941 location_t entry_locus;
942 size_t i, n;
944 entry_locus = gimple_location (entry);
946 n = gimple_switch_num_labels (entry);
948 for (i = 0; i < n; ++i)
950 tree lab = CASE_LABEL (gimple_switch_label (entry, i));
951 basic_block label_bb = label_to_block (lab);
952 make_edge (bb, label_bb, 0);
953 assign_discriminator (entry_locus, label_bb);
958 /* Return the basic block holding label DEST. */
960 basic_block
961 label_to_block_fn (struct function *ifun, tree dest)
963 int uid = LABEL_DECL_UID (dest);
965 /* We would die hard when faced by an undefined label. Emit a label to
966 the very first basic block. This will hopefully make even the dataflow
967 and undefined variable warnings quite right. */
968 if (seen_error () && uid < 0)
970 gimple_stmt_iterator gsi = gsi_start_bb (BASIC_BLOCK (NUM_FIXED_BLOCKS));
971 gimple stmt;
973 stmt = gimple_build_label (dest);
974 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
975 uid = LABEL_DECL_UID (dest);
977 if (VEC_length (basic_block, ifun->cfg->x_label_to_block_map)
978 <= (unsigned int) uid)
979 return NULL;
980 return VEC_index (basic_block, ifun->cfg->x_label_to_block_map, uid);
983 /* Create edges for an abnormal goto statement at block BB. If FOR_CALL
984 is true, the source statement is a CALL_EXPR instead of a GOTO_EXPR. */
986 void
987 make_abnormal_goto_edges (basic_block bb, bool for_call)
989 basic_block target_bb;
990 gimple_stmt_iterator gsi;
992 FOR_EACH_BB (target_bb)
993 for (gsi = gsi_start_bb (target_bb); !gsi_end_p (gsi); gsi_next (&gsi))
995 gimple label_stmt = gsi_stmt (gsi);
996 tree target;
998 if (gimple_code (label_stmt) != GIMPLE_LABEL)
999 break;
1001 target = gimple_label_label (label_stmt);
1003 /* Make an edge to every label block that has been marked as a
1004 potential target for a computed goto or a non-local goto. */
1005 if ((FORCED_LABEL (target) && !for_call)
1006 || (DECL_NONLOCAL (target) && for_call))
1008 make_edge (bb, target_bb, EDGE_ABNORMAL);
1009 break;
1014 /* Create edges for a goto statement at block BB. */
1016 static void
1017 make_goto_expr_edges (basic_block bb)
1019 gimple_stmt_iterator last = gsi_last_bb (bb);
1020 gimple goto_t = gsi_stmt (last);
1022 /* A simple GOTO creates normal edges. */
1023 if (simple_goto_p (goto_t))
1025 tree dest = gimple_goto_dest (goto_t);
1026 basic_block label_bb = label_to_block (dest);
1027 edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
1028 e->goto_locus = gimple_location (goto_t);
1029 assign_discriminator (e->goto_locus, label_bb);
1030 if (e->goto_locus)
1031 e->goto_block = gimple_block (goto_t);
1032 gsi_remove (&last, true);
1033 return;
1036 /* A computed GOTO creates abnormal edges. */
1037 make_abnormal_goto_edges (bb, false);
1040 /* Create edges for an asm statement with labels at block BB. */
1042 static void
1043 make_gimple_asm_edges (basic_block bb)
1045 gimple stmt = last_stmt (bb);
1046 location_t stmt_loc = gimple_location (stmt);
1047 int i, n = gimple_asm_nlabels (stmt);
1049 for (i = 0; i < n; ++i)
1051 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
1052 basic_block label_bb = label_to_block (label);
1053 make_edge (bb, label_bb, 0);
1054 assign_discriminator (stmt_loc, label_bb);
1058 /*---------------------------------------------------------------------------
1059 Flowgraph analysis
1060 ---------------------------------------------------------------------------*/
1062 /* Cleanup useless labels in basic blocks. This is something we wish
1063 to do early because it allows us to group case labels before creating
1064 the edges for the CFG, and it speeds up block statement iterators in
1065 all passes later on.
1066 We rerun this pass after CFG is created, to get rid of the labels that
1067 are no longer referenced. After then we do not run it any more, since
1068 (almost) no new labels should be created. */
1070 /* A map from basic block index to the leading label of that block. */
1071 static struct label_record
1073 /* The label. */
1074 tree label;
1076 /* True if the label is referenced from somewhere. */
1077 bool used;
1078 } *label_for_bb;
1080 /* Given LABEL return the first label in the same basic block. */
1082 static tree
1083 main_block_label (tree label)
1085 basic_block bb = label_to_block (label);
1086 tree main_label = label_for_bb[bb->index].label;
1088 /* label_to_block possibly inserted undefined label into the chain. */
1089 if (!main_label)
1091 label_for_bb[bb->index].label = label;
1092 main_label = label;
1095 label_for_bb[bb->index].used = true;
1096 return main_label;
1099 /* Clean up redundant labels within the exception tree. */
1101 static void
1102 cleanup_dead_labels_eh (void)
1104 eh_landing_pad lp;
1105 eh_region r;
1106 tree lab;
1107 int i;
1109 if (cfun->eh == NULL)
1110 return;
1112 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1113 if (lp && lp->post_landing_pad)
1115 lab = main_block_label (lp->post_landing_pad);
1116 if (lab != lp->post_landing_pad)
1118 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1119 EH_LANDING_PAD_NR (lab) = lp->index;
1123 FOR_ALL_EH_REGION (r)
1124 switch (r->type)
1126 case ERT_CLEANUP:
1127 case ERT_MUST_NOT_THROW:
1128 break;
1130 case ERT_TRY:
1132 eh_catch c;
1133 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
1135 lab = c->label;
1136 if (lab)
1137 c->label = main_block_label (lab);
1140 break;
1142 case ERT_ALLOWED_EXCEPTIONS:
1143 lab = r->u.allowed.label;
1144 if (lab)
1145 r->u.allowed.label = main_block_label (lab);
1146 break;
1151 /* Cleanup redundant labels. This is a three-step process:
1152 1) Find the leading label for each block.
1153 2) Redirect all references to labels to the leading labels.
1154 3) Cleanup all useless labels. */
1156 void
1157 cleanup_dead_labels (void)
1159 basic_block bb;
1160 label_for_bb = XCNEWVEC (struct label_record, last_basic_block);
1162 /* Find a suitable label for each block. We use the first user-defined
1163 label if there is one, or otherwise just the first label we see. */
1164 FOR_EACH_BB (bb)
1166 gimple_stmt_iterator i;
1168 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1170 tree label;
1171 gimple stmt = gsi_stmt (i);
1173 if (gimple_code (stmt) != GIMPLE_LABEL)
1174 break;
1176 label = gimple_label_label (stmt);
1178 /* If we have not yet seen a label for the current block,
1179 remember this one and see if there are more labels. */
1180 if (!label_for_bb[bb->index].label)
1182 label_for_bb[bb->index].label = label;
1183 continue;
1186 /* If we did see a label for the current block already, but it
1187 is an artificially created label, replace it if the current
1188 label is a user defined label. */
1189 if (!DECL_ARTIFICIAL (label)
1190 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
1192 label_for_bb[bb->index].label = label;
1193 break;
1198 /* Now redirect all jumps/branches to the selected label.
1199 First do so for each block ending in a control statement. */
1200 FOR_EACH_BB (bb)
1202 gimple stmt = last_stmt (bb);
1203 tree label, new_label;
1205 if (!stmt)
1206 continue;
1208 switch (gimple_code (stmt))
1210 case GIMPLE_COND:
1211 label = gimple_cond_true_label (stmt);
1212 if (label)
1214 new_label = main_block_label (label);
1215 if (new_label != label)
1216 gimple_cond_set_true_label (stmt, new_label);
1219 label = gimple_cond_false_label (stmt);
1220 if (label)
1222 new_label = main_block_label (label);
1223 if (new_label != label)
1224 gimple_cond_set_false_label (stmt, new_label);
1226 break;
1228 case GIMPLE_SWITCH:
1230 size_t i, n = gimple_switch_num_labels (stmt);
1232 /* Replace all destination labels. */
1233 for (i = 0; i < n; ++i)
1235 tree case_label = gimple_switch_label (stmt, i);
1236 label = CASE_LABEL (case_label);
1237 new_label = main_block_label (label);
1238 if (new_label != label)
1239 CASE_LABEL (case_label) = new_label;
1241 break;
1244 case GIMPLE_ASM:
1246 int i, n = gimple_asm_nlabels (stmt);
1248 for (i = 0; i < n; ++i)
1250 tree cons = gimple_asm_label_op (stmt, i);
1251 tree label = main_block_label (TREE_VALUE (cons));
1252 TREE_VALUE (cons) = label;
1254 break;
1257 /* We have to handle gotos until they're removed, and we don't
1258 remove them until after we've created the CFG edges. */
1259 case GIMPLE_GOTO:
1260 if (!computed_goto_p (stmt))
1262 label = gimple_goto_dest (stmt);
1263 new_label = main_block_label (label);
1264 if (new_label != label)
1265 gimple_goto_set_dest (stmt, new_label);
1267 break;
1269 case GIMPLE_TRANSACTION:
1271 tree label = gimple_transaction_label (stmt);
1272 if (label)
1274 tree new_label = main_block_label (label);
1275 if (new_label != label)
1276 gimple_transaction_set_label (stmt, new_label);
1279 break;
1281 default:
1282 break;
1286 /* Do the same for the exception region tree labels. */
1287 cleanup_dead_labels_eh ();
1289 /* Finally, purge dead labels. All user-defined labels and labels that
1290 can be the target of non-local gotos and labels which have their
1291 address taken are preserved. */
1292 FOR_EACH_BB (bb)
1294 gimple_stmt_iterator i;
1295 tree label_for_this_bb = label_for_bb[bb->index].label;
1297 if (!label_for_this_bb)
1298 continue;
1300 /* If the main label of the block is unused, we may still remove it. */
1301 if (!label_for_bb[bb->index].used)
1302 label_for_this_bb = NULL;
1304 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1306 tree label;
1307 gimple stmt = gsi_stmt (i);
1309 if (gimple_code (stmt) != GIMPLE_LABEL)
1310 break;
1312 label = gimple_label_label (stmt);
1314 if (label == label_for_this_bb
1315 || !DECL_ARTIFICIAL (label)
1316 || DECL_NONLOCAL (label)
1317 || FORCED_LABEL (label))
1318 gsi_next (&i);
1319 else
1320 gsi_remove (&i, true);
1324 free (label_for_bb);
1327 /* Scan the sorted vector of cases in STMT (a GIMPLE_SWITCH) and combine
1328 the ones jumping to the same label.
1329 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1331 void
1332 group_case_labels_stmt (gimple stmt)
1334 int old_size = gimple_switch_num_labels (stmt);
1335 int i, j, new_size = old_size;
1336 basic_block default_bb = NULL;
1338 default_bb = label_to_block (CASE_LABEL (gimple_switch_default_label (stmt)));
1340 /* Look for possible opportunities to merge cases. */
1341 i = 1;
1342 while (i < old_size)
1344 tree base_case, base_high;
1345 basic_block base_bb;
1347 base_case = gimple_switch_label (stmt, i);
1349 gcc_assert (base_case);
1350 base_bb = label_to_block (CASE_LABEL (base_case));
1352 /* Discard cases that have the same destination as the
1353 default case. */
1354 if (base_bb == default_bb)
1356 gimple_switch_set_label (stmt, i, NULL_TREE);
1357 i++;
1358 new_size--;
1359 continue;
1362 base_high = CASE_HIGH (base_case)
1363 ? CASE_HIGH (base_case)
1364 : CASE_LOW (base_case);
1365 i++;
1367 /* Try to merge case labels. Break out when we reach the end
1368 of the label vector or when we cannot merge the next case
1369 label with the current one. */
1370 while (i < old_size)
1372 tree merge_case = gimple_switch_label (stmt, i);
1373 basic_block merge_bb = label_to_block (CASE_LABEL (merge_case));
1374 double_int bhp1 = tree_to_double_int (base_high) + double_int_one;
1376 /* Merge the cases if they jump to the same place,
1377 and their ranges are consecutive. */
1378 if (merge_bb == base_bb
1379 && tree_to_double_int (CASE_LOW (merge_case)) == bhp1)
1381 base_high = CASE_HIGH (merge_case) ?
1382 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1383 CASE_HIGH (base_case) = base_high;
1384 gimple_switch_set_label (stmt, i, NULL_TREE);
1385 new_size--;
1386 i++;
1388 else
1389 break;
1393 /* Compress the case labels in the label vector, and adjust the
1394 length of the vector. */
1395 for (i = 0, j = 0; i < new_size; i++)
1397 while (! gimple_switch_label (stmt, j))
1398 j++;
1399 gimple_switch_set_label (stmt, i,
1400 gimple_switch_label (stmt, j++));
1403 gcc_assert (new_size <= old_size);
1404 gimple_switch_set_num_labels (stmt, new_size);
1407 /* Look for blocks ending in a multiway branch (a GIMPLE_SWITCH),
1408 and scan the sorted vector of cases. Combine the ones jumping to the
1409 same label. */
1411 void
1412 group_case_labels (void)
1414 basic_block bb;
1416 FOR_EACH_BB (bb)
1418 gimple stmt = last_stmt (bb);
1419 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1420 group_case_labels_stmt (stmt);
1424 /* Checks whether we can merge block B into block A. */
1426 static bool
1427 gimple_can_merge_blocks_p (basic_block a, basic_block b)
1429 gimple stmt;
1430 gimple_stmt_iterator gsi;
1432 if (!single_succ_p (a))
1433 return false;
1435 if (single_succ_edge (a)->flags & EDGE_COMPLEX)
1436 return false;
1438 if (single_succ (a) != b)
1439 return false;
1441 if (!single_pred_p (b))
1442 return false;
1444 if (b == EXIT_BLOCK_PTR)
1445 return false;
1447 /* If A ends by a statement causing exceptions or something similar, we
1448 cannot merge the blocks. */
1449 stmt = last_stmt (a);
1450 if (stmt && stmt_ends_bb_p (stmt))
1451 return false;
1453 /* Do not allow a block with only a non-local label to be merged. */
1454 if (stmt
1455 && gimple_code (stmt) == GIMPLE_LABEL
1456 && DECL_NONLOCAL (gimple_label_label (stmt)))
1457 return false;
1459 /* Examine the labels at the beginning of B. */
1460 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
1462 tree lab;
1463 stmt = gsi_stmt (gsi);
1464 if (gimple_code (stmt) != GIMPLE_LABEL)
1465 break;
1466 lab = gimple_label_label (stmt);
1468 /* Do not remove user forced labels or for -O0 any user labels. */
1469 if (!DECL_ARTIFICIAL (lab) && (!optimize || FORCED_LABEL (lab)))
1470 return false;
1473 /* Protect the loop latches. */
1474 if (current_loops && b->loop_father->latch == b)
1475 return false;
1477 /* It must be possible to eliminate all phi nodes in B. If ssa form
1478 is not up-to-date and a name-mapping is registered, we cannot eliminate
1479 any phis. Symbols marked for renaming are never a problem though. */
1480 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi); gsi_next (&gsi))
1482 gimple phi = gsi_stmt (gsi);
1483 /* Technically only new names matter. */
1484 if (name_registered_for_update_p (PHI_RESULT (phi)))
1485 return false;
1488 /* When not optimizing, don't merge if we'd lose goto_locus. */
1489 if (!optimize
1490 && single_succ_edge (a)->goto_locus != UNKNOWN_LOCATION)
1492 location_t goto_locus = single_succ_edge (a)->goto_locus;
1493 gimple_stmt_iterator prev, next;
1494 prev = gsi_last_nondebug_bb (a);
1495 next = gsi_after_labels (b);
1496 if (!gsi_end_p (next) && is_gimple_debug (gsi_stmt (next)))
1497 gsi_next_nondebug (&next);
1498 if ((gsi_end_p (prev)
1499 || gimple_location (gsi_stmt (prev)) != goto_locus)
1500 && (gsi_end_p (next)
1501 || gimple_location (gsi_stmt (next)) != goto_locus))
1502 return false;
1505 return true;
1508 /* Return true if the var whose chain of uses starts at PTR has no
1509 nondebug uses. */
1510 bool
1511 has_zero_uses_1 (const ssa_use_operand_t *head)
1513 const ssa_use_operand_t *ptr;
1515 for (ptr = head->next; ptr != head; ptr = ptr->next)
1516 if (!is_gimple_debug (USE_STMT (ptr)))
1517 return false;
1519 return true;
1522 /* Return true if the var whose chain of uses starts at PTR has a
1523 single nondebug use. Set USE_P and STMT to that single nondebug
1524 use, if so, or to NULL otherwise. */
1525 bool
1526 single_imm_use_1 (const ssa_use_operand_t *head,
1527 use_operand_p *use_p, gimple *stmt)
1529 ssa_use_operand_t *ptr, *single_use = 0;
1531 for (ptr = head->next; ptr != head; ptr = ptr->next)
1532 if (!is_gimple_debug (USE_STMT (ptr)))
1534 if (single_use)
1536 single_use = NULL;
1537 break;
1539 single_use = ptr;
1542 if (use_p)
1543 *use_p = single_use;
1545 if (stmt)
1546 *stmt = single_use ? single_use->loc.stmt : NULL;
1548 return !!single_use;
1551 /* Replaces all uses of NAME by VAL. */
1553 void
1554 replace_uses_by (tree name, tree val)
1556 imm_use_iterator imm_iter;
1557 use_operand_p use;
1558 gimple stmt;
1559 edge e;
1561 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1563 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1565 replace_exp (use, val);
1567 if (gimple_code (stmt) == GIMPLE_PHI)
1569 e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
1570 if (e->flags & EDGE_ABNORMAL)
1572 /* This can only occur for virtual operands, since
1573 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1574 would prevent replacement. */
1575 gcc_checking_assert (virtual_operand_p (name));
1576 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1581 if (gimple_code (stmt) != GIMPLE_PHI)
1583 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1584 gimple orig_stmt = stmt;
1585 size_t i;
1587 /* Mark the block if we changed the last stmt in it. */
1588 if (cfgcleanup_altered_bbs
1589 && stmt_ends_bb_p (stmt))
1590 bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1592 /* FIXME. It shouldn't be required to keep TREE_CONSTANT
1593 on ADDR_EXPRs up-to-date on GIMPLE. Propagation will
1594 only change sth from non-invariant to invariant, and only
1595 when propagating constants. */
1596 if (is_gimple_min_invariant (val))
1597 for (i = 0; i < gimple_num_ops (stmt); i++)
1599 tree op = gimple_op (stmt, i);
1600 /* Operands may be empty here. For example, the labels
1601 of a GIMPLE_COND are nulled out following the creation
1602 of the corresponding CFG edges. */
1603 if (op && TREE_CODE (op) == ADDR_EXPR)
1604 recompute_tree_invariant_for_addr_expr (op);
1607 if (fold_stmt (&gsi))
1608 stmt = gsi_stmt (gsi);
1610 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1611 gimple_purge_dead_eh_edges (gimple_bb (stmt));
1613 update_stmt (stmt);
1617 gcc_checking_assert (has_zero_uses (name));
1619 /* Also update the trees stored in loop structures. */
1620 if (current_loops)
1622 struct loop *loop;
1623 loop_iterator li;
1625 FOR_EACH_LOOP (li, loop, 0)
1627 substitute_in_loop_info (loop, name, val);
1632 /* Merge block B into block A. */
1634 static void
1635 gimple_merge_blocks (basic_block a, basic_block b)
1637 gimple_stmt_iterator last, gsi, psi;
1639 if (dump_file)
1640 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1642 /* Remove all single-valued PHI nodes from block B of the form
1643 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1644 gsi = gsi_last_bb (a);
1645 for (psi = gsi_start_phis (b); !gsi_end_p (psi); )
1647 gimple phi = gsi_stmt (psi);
1648 tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
1649 gimple copy;
1650 bool may_replace_uses = (virtual_operand_p (def)
1651 || may_propagate_copy (def, use));
1653 /* In case we maintain loop closed ssa form, do not propagate arguments
1654 of loop exit phi nodes. */
1655 if (current_loops
1656 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1657 && !virtual_operand_p (def)
1658 && TREE_CODE (use) == SSA_NAME
1659 && a->loop_father != b->loop_father)
1660 may_replace_uses = false;
1662 if (!may_replace_uses)
1664 gcc_assert (!virtual_operand_p (def));
1666 /* Note that just emitting the copies is fine -- there is no problem
1667 with ordering of phi nodes. This is because A is the single
1668 predecessor of B, therefore results of the phi nodes cannot
1669 appear as arguments of the phi nodes. */
1670 copy = gimple_build_assign (def, use);
1671 gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
1672 remove_phi_node (&psi, false);
1674 else
1676 /* If we deal with a PHI for virtual operands, we can simply
1677 propagate these without fussing with folding or updating
1678 the stmt. */
1679 if (virtual_operand_p (def))
1681 imm_use_iterator iter;
1682 use_operand_p use_p;
1683 gimple stmt;
1685 FOR_EACH_IMM_USE_STMT (stmt, iter, def)
1686 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1687 SET_USE (use_p, use);
1689 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1690 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use) = 1;
1692 else
1693 replace_uses_by (def, use);
1695 remove_phi_node (&psi, true);
1699 /* Ensure that B follows A. */
1700 move_block_after (b, a);
1702 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1703 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1705 /* Remove labels from B and set gimple_bb to A for other statements. */
1706 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
1708 gimple stmt = gsi_stmt (gsi);
1709 if (gimple_code (stmt) == GIMPLE_LABEL)
1711 tree label = gimple_label_label (stmt);
1712 int lp_nr;
1714 gsi_remove (&gsi, false);
1716 /* Now that we can thread computed gotos, we might have
1717 a situation where we have a forced label in block B
1718 However, the label at the start of block B might still be
1719 used in other ways (think about the runtime checking for
1720 Fortran assigned gotos). So we can not just delete the
1721 label. Instead we move the label to the start of block A. */
1722 if (FORCED_LABEL (label))
1724 gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
1725 gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
1727 /* Other user labels keep around in a form of a debug stmt. */
1728 else if (!DECL_ARTIFICIAL (label) && MAY_HAVE_DEBUG_STMTS)
1730 gimple dbg = gimple_build_debug_bind (label,
1731 integer_zero_node,
1732 stmt);
1733 gimple_debug_bind_reset_value (dbg);
1734 gsi_insert_before (&gsi, dbg, GSI_SAME_STMT);
1737 lp_nr = EH_LANDING_PAD_NR (label);
1738 if (lp_nr)
1740 eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
1741 lp->post_landing_pad = NULL;
1744 else
1746 gimple_set_bb (stmt, a);
1747 gsi_next (&gsi);
1751 /* Merge the sequences. */
1752 last = gsi_last_bb (a);
1753 gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
1754 set_bb_seq (b, NULL);
1756 if (cfgcleanup_altered_bbs)
1757 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1761 /* Return the one of two successors of BB that is not reachable by a
1762 complex edge, if there is one. Else, return BB. We use
1763 this in optimizations that use post-dominators for their heuristics,
1764 to catch the cases in C++ where function calls are involved. */
1766 basic_block
1767 single_noncomplex_succ (basic_block bb)
1769 edge e0, e1;
1770 if (EDGE_COUNT (bb->succs) != 2)
1771 return bb;
1773 e0 = EDGE_SUCC (bb, 0);
1774 e1 = EDGE_SUCC (bb, 1);
1775 if (e0->flags & EDGE_COMPLEX)
1776 return e1->dest;
1777 if (e1->flags & EDGE_COMPLEX)
1778 return e0->dest;
1780 return bb;
1783 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1785 void
1786 notice_special_calls (gimple call)
1788 int flags = gimple_call_flags (call);
1790 if (flags & ECF_MAY_BE_ALLOCA)
1791 cfun->calls_alloca = true;
1792 if (flags & ECF_RETURNS_TWICE)
1793 cfun->calls_setjmp = true;
1797 /* Clear flags set by notice_special_calls. Used by dead code removal
1798 to update the flags. */
1800 void
1801 clear_special_calls (void)
1803 cfun->calls_alloca = false;
1804 cfun->calls_setjmp = false;
1807 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1809 static void
1810 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1812 /* Since this block is no longer reachable, we can just delete all
1813 of its PHI nodes. */
1814 remove_phi_nodes (bb);
1816 /* Remove edges to BB's successors. */
1817 while (EDGE_COUNT (bb->succs) > 0)
1818 remove_edge (EDGE_SUCC (bb, 0));
1822 /* Remove statements of basic block BB. */
1824 static void
1825 remove_bb (basic_block bb)
1827 gimple_stmt_iterator i;
1829 if (dump_file)
1831 fprintf (dump_file, "Removing basic block %d\n", bb->index);
1832 if (dump_flags & TDF_DETAILS)
1834 dump_bb (dump_file, bb, 0, dump_flags);
1835 fprintf (dump_file, "\n");
1839 if (current_loops)
1841 struct loop *loop = bb->loop_father;
1843 /* If a loop gets removed, clean up the information associated
1844 with it. */
1845 if (loop->latch == bb
1846 || loop->header == bb)
1847 free_numbers_of_iterations_estimates_loop (loop);
1850 /* Remove all the instructions in the block. */
1851 if (bb_seq (bb) != NULL)
1853 /* Walk backwards so as to get a chance to substitute all
1854 released DEFs into debug stmts. See
1855 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
1856 details. */
1857 for (i = gsi_last_bb (bb); !gsi_end_p (i);)
1859 gimple stmt = gsi_stmt (i);
1860 if (gimple_code (stmt) == GIMPLE_LABEL
1861 && (FORCED_LABEL (gimple_label_label (stmt))
1862 || DECL_NONLOCAL (gimple_label_label (stmt))))
1864 basic_block new_bb;
1865 gimple_stmt_iterator new_gsi;
1867 /* A non-reachable non-local label may still be referenced.
1868 But it no longer needs to carry the extra semantics of
1869 non-locality. */
1870 if (DECL_NONLOCAL (gimple_label_label (stmt)))
1872 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
1873 FORCED_LABEL (gimple_label_label (stmt)) = 1;
1876 new_bb = bb->prev_bb;
1877 new_gsi = gsi_start_bb (new_bb);
1878 gsi_remove (&i, false);
1879 gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
1881 else
1883 /* Release SSA definitions if we are in SSA. Note that we
1884 may be called when not in SSA. For example,
1885 final_cleanup calls this function via
1886 cleanup_tree_cfg. */
1887 if (gimple_in_ssa_p (cfun))
1888 release_defs (stmt);
1890 gsi_remove (&i, true);
1893 if (gsi_end_p (i))
1894 i = gsi_last_bb (bb);
1895 else
1896 gsi_prev (&i);
1900 remove_phi_nodes_and_edges_for_unreachable_block (bb);
1901 bb->il.gimple.seq = NULL;
1902 bb->il.gimple.phi_nodes = NULL;
1906 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
1907 predicate VAL, return the edge that will be taken out of the block.
1908 If VAL does not match a unique edge, NULL is returned. */
1910 edge
1911 find_taken_edge (basic_block bb, tree val)
1913 gimple stmt;
1915 stmt = last_stmt (bb);
1917 gcc_assert (stmt);
1918 gcc_assert (is_ctrl_stmt (stmt));
1920 if (val == NULL)
1921 return NULL;
1923 if (!is_gimple_min_invariant (val))
1924 return NULL;
1926 if (gimple_code (stmt) == GIMPLE_COND)
1927 return find_taken_edge_cond_expr (bb, val);
1929 if (gimple_code (stmt) == GIMPLE_SWITCH)
1930 return find_taken_edge_switch_expr (bb, val);
1932 if (computed_goto_p (stmt))
1934 /* Only optimize if the argument is a label, if the argument is
1935 not a label then we can not construct a proper CFG.
1937 It may be the case that we only need to allow the LABEL_REF to
1938 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
1939 appear inside a LABEL_EXPR just to be safe. */
1940 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
1941 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
1942 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
1943 return NULL;
1946 gcc_unreachable ();
1949 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
1950 statement, determine which of the outgoing edges will be taken out of the
1951 block. Return NULL if either edge may be taken. */
1953 static edge
1954 find_taken_edge_computed_goto (basic_block bb, tree val)
1956 basic_block dest;
1957 edge e = NULL;
1959 dest = label_to_block (val);
1960 if (dest)
1962 e = find_edge (bb, dest);
1963 gcc_assert (e != NULL);
1966 return e;
1969 /* Given a constant value VAL and the entry block BB to a COND_EXPR
1970 statement, determine which of the two edges will be taken out of the
1971 block. Return NULL if either edge may be taken. */
1973 static edge
1974 find_taken_edge_cond_expr (basic_block bb, tree val)
1976 edge true_edge, false_edge;
1978 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1980 gcc_assert (TREE_CODE (val) == INTEGER_CST);
1981 return (integer_zerop (val) ? false_edge : true_edge);
1984 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
1985 statement, determine which edge will be taken out of the block. Return
1986 NULL if any edge may be taken. */
1988 static edge
1989 find_taken_edge_switch_expr (basic_block bb, tree val)
1991 basic_block dest_bb;
1992 edge e;
1993 gimple switch_stmt;
1994 tree taken_case;
1996 switch_stmt = last_stmt (bb);
1997 taken_case = find_case_label_for_value (switch_stmt, val);
1998 dest_bb = label_to_block (CASE_LABEL (taken_case));
2000 e = find_edge (bb, dest_bb);
2001 gcc_assert (e);
2002 return e;
2006 /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
2007 We can make optimal use here of the fact that the case labels are
2008 sorted: We can do a binary search for a case matching VAL. */
2010 static tree
2011 find_case_label_for_value (gimple switch_stmt, tree val)
2013 size_t low, high, n = gimple_switch_num_labels (switch_stmt);
2014 tree default_case = gimple_switch_default_label (switch_stmt);
2016 for (low = 0, high = n; high - low > 1; )
2018 size_t i = (high + low) / 2;
2019 tree t = gimple_switch_label (switch_stmt, i);
2020 int cmp;
2022 /* Cache the result of comparing CASE_LOW and val. */
2023 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2025 if (cmp > 0)
2026 high = i;
2027 else
2028 low = i;
2030 if (CASE_HIGH (t) == NULL)
2032 /* A singe-valued case label. */
2033 if (cmp == 0)
2034 return t;
2036 else
2038 /* A case range. We can only handle integer ranges. */
2039 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2040 return t;
2044 return default_case;
2048 /* Dump a basic block on stderr. */
2050 void
2051 gimple_debug_bb (basic_block bb)
2053 dump_bb (stderr, bb, 0, TDF_VOPS|TDF_MEMSYMS|TDF_BLOCKS);
2057 /* Dump basic block with index N on stderr. */
2059 basic_block
2060 gimple_debug_bb_n (int n)
2062 gimple_debug_bb (BASIC_BLOCK (n));
2063 return BASIC_BLOCK (n);
2067 /* Dump the CFG on stderr.
2069 FLAGS are the same used by the tree dumping functions
2070 (see TDF_* in tree-pass.h). */
2072 void
2073 gimple_debug_cfg (int flags)
2075 gimple_dump_cfg (stderr, flags);
2079 /* Dump the program showing basic block boundaries on the given FILE.
2081 FLAGS are the same used by the tree dumping functions (see TDF_* in
2082 tree.h). */
2084 void
2085 gimple_dump_cfg (FILE *file, int flags)
2087 if (flags & TDF_DETAILS)
2089 dump_function_header (file, current_function_decl, flags);
2090 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2091 n_basic_blocks, n_edges, last_basic_block);
2093 brief_dump_cfg (file, flags | TDF_COMMENT);
2094 fprintf (file, "\n");
2097 if (flags & TDF_STATS)
2098 dump_cfg_stats (file);
2100 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2104 /* Dump CFG statistics on FILE. */
2106 void
2107 dump_cfg_stats (FILE *file)
2109 static long max_num_merged_labels = 0;
2110 unsigned long size, total = 0;
2111 long num_edges;
2112 basic_block bb;
2113 const char * const fmt_str = "%-30s%-13s%12s\n";
2114 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2115 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2116 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2117 const char *funcname = current_function_name ();
2119 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2121 fprintf (file, "---------------------------------------------------------\n");
2122 fprintf (file, fmt_str, "", " Number of ", "Memory");
2123 fprintf (file, fmt_str, "", " instances ", "used ");
2124 fprintf (file, "---------------------------------------------------------\n");
2126 size = n_basic_blocks * sizeof (struct basic_block_def);
2127 total += size;
2128 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2129 SCALE (size), LABEL (size));
2131 num_edges = 0;
2132 FOR_EACH_BB (bb)
2133 num_edges += EDGE_COUNT (bb->succs);
2134 size = num_edges * sizeof (struct edge_def);
2135 total += size;
2136 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2138 fprintf (file, "---------------------------------------------------------\n");
2139 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2140 LABEL (total));
2141 fprintf (file, "---------------------------------------------------------\n");
2142 fprintf (file, "\n");
2144 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2145 max_num_merged_labels = cfg_stats.num_merged_labels;
2147 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2148 cfg_stats.num_merged_labels, max_num_merged_labels);
2150 fprintf (file, "\n");
2154 /* Dump CFG statistics on stderr. Keep extern so that it's always
2155 linked in the final executable. */
2157 DEBUG_FUNCTION void
2158 debug_cfg_stats (void)
2160 dump_cfg_stats (stderr);
2164 /* Dump the flowgraph to a .vcg FILE. */
2166 static void
2167 gimple_cfg2vcg (FILE *file)
2169 edge e;
2170 edge_iterator ei;
2171 basic_block bb;
2172 const char *funcname = current_function_name ();
2174 /* Write the file header. */
2175 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2176 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2177 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2179 /* Write blocks and edges. */
2180 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2182 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2183 e->dest->index);
2185 if (e->flags & EDGE_FAKE)
2186 fprintf (file, " linestyle: dotted priority: 10");
2187 else
2188 fprintf (file, " linestyle: solid priority: 100");
2190 fprintf (file, " }\n");
2192 fputc ('\n', file);
2194 FOR_EACH_BB (bb)
2196 enum gimple_code head_code, end_code;
2197 const char *head_name, *end_name;
2198 int head_line = 0;
2199 int end_line = 0;
2200 gimple first = first_stmt (bb);
2201 gimple last = last_stmt (bb);
2203 if (first)
2205 head_code = gimple_code (first);
2206 head_name = gimple_code_name[head_code];
2207 head_line = get_lineno (first);
2209 else
2210 head_name = "no-statement";
2212 if (last)
2214 end_code = gimple_code (last);
2215 end_name = gimple_code_name[end_code];
2216 end_line = get_lineno (last);
2218 else
2219 end_name = "no-statement";
2221 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2222 bb->index, bb->index, head_name, head_line, end_name,
2223 end_line);
2225 FOR_EACH_EDGE (e, ei, bb->succs)
2227 if (e->dest == EXIT_BLOCK_PTR)
2228 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2229 else
2230 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2232 if (e->flags & EDGE_FAKE)
2233 fprintf (file, " priority: 10 linestyle: dotted");
2234 else
2235 fprintf (file, " priority: 100 linestyle: solid");
2237 fprintf (file, " }\n");
2240 if (bb->next_bb != EXIT_BLOCK_PTR)
2241 fputc ('\n', file);
2244 fputs ("}\n\n", file);
2249 /*---------------------------------------------------------------------------
2250 Miscellaneous helpers
2251 ---------------------------------------------------------------------------*/
2253 /* Return true if T, a GIMPLE_CALL, can make an abnormal transfer of control
2254 flow. Transfers of control flow associated with EH are excluded. */
2256 static bool
2257 call_can_make_abnormal_goto (gimple t)
2259 /* If the function has no non-local labels, then a call cannot make an
2260 abnormal transfer of control. */
2261 if (!cfun->has_nonlocal_label)
2262 return false;
2264 /* Likewise if the call has no side effects. */
2265 if (!gimple_has_side_effects (t))
2266 return false;
2268 /* Likewise if the called function is leaf. */
2269 if (gimple_call_flags (t) & ECF_LEAF)
2270 return false;
2272 return true;
2276 /* Return true if T can make an abnormal transfer of control flow.
2277 Transfers of control flow associated with EH are excluded. */
2279 bool
2280 stmt_can_make_abnormal_goto (gimple t)
2282 if (computed_goto_p (t))
2283 return true;
2284 if (is_gimple_call (t))
2285 return call_can_make_abnormal_goto (t);
2286 return false;
2290 /* Return true if T represents a stmt that always transfers control. */
2292 bool
2293 is_ctrl_stmt (gimple t)
2295 switch (gimple_code (t))
2297 case GIMPLE_COND:
2298 case GIMPLE_SWITCH:
2299 case GIMPLE_GOTO:
2300 case GIMPLE_RETURN:
2301 case GIMPLE_RESX:
2302 return true;
2303 default:
2304 return false;
2309 /* Return true if T is a statement that may alter the flow of control
2310 (e.g., a call to a non-returning function). */
2312 bool
2313 is_ctrl_altering_stmt (gimple t)
2315 gcc_assert (t);
2317 switch (gimple_code (t))
2319 case GIMPLE_CALL:
2321 int flags = gimple_call_flags (t);
2323 /* A call alters control flow if it can make an abnormal goto. */
2324 if (call_can_make_abnormal_goto (t))
2325 return true;
2327 /* A call also alters control flow if it does not return. */
2328 if (flags & ECF_NORETURN)
2329 return true;
2331 /* TM ending statements have backedges out of the transaction.
2332 Return true so we split the basic block containing them.
2333 Note that the TM_BUILTIN test is merely an optimization. */
2334 if ((flags & ECF_TM_BUILTIN)
2335 && is_tm_ending_fndecl (gimple_call_fndecl (t)))
2336 return true;
2338 /* BUILT_IN_RETURN call is same as return statement. */
2339 if (gimple_call_builtin_p (t, BUILT_IN_RETURN))
2340 return true;
2342 break;
2344 case GIMPLE_EH_DISPATCH:
2345 /* EH_DISPATCH branches to the individual catch handlers at
2346 this level of a try or allowed-exceptions region. It can
2347 fallthru to the next statement as well. */
2348 return true;
2350 case GIMPLE_ASM:
2351 if (gimple_asm_nlabels (t) > 0)
2352 return true;
2353 break;
2355 CASE_GIMPLE_OMP:
2356 /* OpenMP directives alter control flow. */
2357 return true;
2359 case GIMPLE_TRANSACTION:
2360 /* A transaction start alters control flow. */
2361 return true;
2363 default:
2364 break;
2367 /* If a statement can throw, it alters control flow. */
2368 return stmt_can_throw_internal (t);
2372 /* Return true if T is a simple local goto. */
2374 bool
2375 simple_goto_p (gimple t)
2377 return (gimple_code (t) == GIMPLE_GOTO
2378 && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2382 /* Return true if STMT should start a new basic block. PREV_STMT is
2383 the statement preceding STMT. It is used when STMT is a label or a
2384 case label. Labels should only start a new basic block if their
2385 previous statement wasn't a label. Otherwise, sequence of labels
2386 would generate unnecessary basic blocks that only contain a single
2387 label. */
2389 static inline bool
2390 stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
2392 if (stmt == NULL)
2393 return false;
2395 /* Labels start a new basic block only if the preceding statement
2396 wasn't a label of the same type. This prevents the creation of
2397 consecutive blocks that have nothing but a single label. */
2398 if (gimple_code (stmt) == GIMPLE_LABEL)
2400 /* Nonlocal and computed GOTO targets always start a new block. */
2401 if (DECL_NONLOCAL (gimple_label_label (stmt))
2402 || FORCED_LABEL (gimple_label_label (stmt)))
2403 return true;
2405 if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
2407 if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
2408 return true;
2410 cfg_stats.num_merged_labels++;
2411 return false;
2413 else
2414 return true;
2417 return false;
2421 /* Return true if T should end a basic block. */
2423 bool
2424 stmt_ends_bb_p (gimple t)
2426 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2429 /* Remove block annotations and other data structures. */
2431 void
2432 delete_tree_cfg_annotations (void)
2434 label_to_block_map = NULL;
2438 /* Return the first statement in basic block BB. */
2440 gimple
2441 first_stmt (basic_block bb)
2443 gimple_stmt_iterator i = gsi_start_bb (bb);
2444 gimple stmt = NULL;
2446 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2448 gsi_next (&i);
2449 stmt = NULL;
2451 return stmt;
2454 /* Return the first non-label statement in basic block BB. */
2456 static gimple
2457 first_non_label_stmt (basic_block bb)
2459 gimple_stmt_iterator i = gsi_start_bb (bb);
2460 while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
2461 gsi_next (&i);
2462 return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
2465 /* Return the last statement in basic block BB. */
2467 gimple
2468 last_stmt (basic_block bb)
2470 gimple_stmt_iterator i = gsi_last_bb (bb);
2471 gimple stmt = NULL;
2473 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2475 gsi_prev (&i);
2476 stmt = NULL;
2478 return stmt;
2481 /* Return the last statement of an otherwise empty block. Return NULL
2482 if the block is totally empty, or if it contains more than one
2483 statement. */
2485 gimple
2486 last_and_only_stmt (basic_block bb)
2488 gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
2489 gimple last, prev;
2491 if (gsi_end_p (i))
2492 return NULL;
2494 last = gsi_stmt (i);
2495 gsi_prev_nondebug (&i);
2496 if (gsi_end_p (i))
2497 return last;
2499 /* Empty statements should no longer appear in the instruction stream.
2500 Everything that might have appeared before should be deleted by
2501 remove_useless_stmts, and the optimizers should just gsi_remove
2502 instead of smashing with build_empty_stmt.
2504 Thus the only thing that should appear here in a block containing
2505 one executable statement is a label. */
2506 prev = gsi_stmt (i);
2507 if (gimple_code (prev) == GIMPLE_LABEL)
2508 return last;
2509 else
2510 return NULL;
2513 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
2515 static void
2516 reinstall_phi_args (edge new_edge, edge old_edge)
2518 edge_var_map_vector v;
2519 edge_var_map *vm;
2520 int i;
2521 gimple_stmt_iterator phis;
2523 v = redirect_edge_var_map_vector (old_edge);
2524 if (!v)
2525 return;
2527 for (i = 0, phis = gsi_start_phis (new_edge->dest);
2528 VEC_iterate (edge_var_map, v, i, vm) && !gsi_end_p (phis);
2529 i++, gsi_next (&phis))
2531 gimple phi = gsi_stmt (phis);
2532 tree result = redirect_edge_var_map_result (vm);
2533 tree arg = redirect_edge_var_map_def (vm);
2535 gcc_assert (result == gimple_phi_result (phi));
2537 add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
2540 redirect_edge_var_map_clear (old_edge);
2543 /* Returns the basic block after which the new basic block created
2544 by splitting edge EDGE_IN should be placed. Tries to keep the new block
2545 near its "logical" location. This is of most help to humans looking
2546 at debugging dumps. */
2548 static basic_block
2549 split_edge_bb_loc (edge edge_in)
2551 basic_block dest = edge_in->dest;
2552 basic_block dest_prev = dest->prev_bb;
2554 if (dest_prev)
2556 edge e = find_edge (dest_prev, dest);
2557 if (e && !(e->flags & EDGE_COMPLEX))
2558 return edge_in->src;
2560 return dest_prev;
2563 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2564 Abort on abnormal edges. */
2566 static basic_block
2567 gimple_split_edge (edge edge_in)
2569 basic_block new_bb, after_bb, dest;
2570 edge new_edge, e;
2572 /* Abnormal edges cannot be split. */
2573 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2575 dest = edge_in->dest;
2577 after_bb = split_edge_bb_loc (edge_in);
2579 new_bb = create_empty_bb (after_bb);
2580 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2581 new_bb->count = edge_in->count;
2582 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2583 new_edge->probability = REG_BR_PROB_BASE;
2584 new_edge->count = edge_in->count;
2586 e = redirect_edge_and_branch (edge_in, new_bb);
2587 gcc_assert (e == edge_in);
2588 reinstall_phi_args (new_edge, e);
2590 return new_bb;
2594 /* Verify properties of the address expression T with base object BASE. */
2596 static tree
2597 verify_address (tree t, tree base)
2599 bool old_constant;
2600 bool old_side_effects;
2601 bool new_constant;
2602 bool new_side_effects;
2604 old_constant = TREE_CONSTANT (t);
2605 old_side_effects = TREE_SIDE_EFFECTS (t);
2607 recompute_tree_invariant_for_addr_expr (t);
2608 new_side_effects = TREE_SIDE_EFFECTS (t);
2609 new_constant = TREE_CONSTANT (t);
2611 if (old_constant != new_constant)
2613 error ("constant not recomputed when ADDR_EXPR changed");
2614 return t;
2616 if (old_side_effects != new_side_effects)
2618 error ("side effects not recomputed when ADDR_EXPR changed");
2619 return t;
2622 if (!(TREE_CODE (base) == VAR_DECL
2623 || TREE_CODE (base) == PARM_DECL
2624 || TREE_CODE (base) == RESULT_DECL))
2625 return NULL_TREE;
2627 if (DECL_GIMPLE_REG_P (base))
2629 error ("DECL_GIMPLE_REG_P set on a variable with address taken");
2630 return base;
2633 return NULL_TREE;
2636 /* Callback for walk_tree, check that all elements with address taken are
2637 properly noticed as such. The DATA is an int* that is 1 if TP was seen
2638 inside a PHI node. */
2640 static tree
2641 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2643 tree t = *tp, x;
2645 if (TYPE_P (t))
2646 *walk_subtrees = 0;
2648 /* Check operand N for being valid GIMPLE and give error MSG if not. */
2649 #define CHECK_OP(N, MSG) \
2650 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
2651 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
2653 switch (TREE_CODE (t))
2655 case SSA_NAME:
2656 if (SSA_NAME_IN_FREE_LIST (t))
2658 error ("SSA name in freelist but still referenced");
2659 return *tp;
2661 break;
2663 case INDIRECT_REF:
2664 error ("INDIRECT_REF in gimple IL");
2665 return t;
2667 case MEM_REF:
2668 x = TREE_OPERAND (t, 0);
2669 if (!POINTER_TYPE_P (TREE_TYPE (x))
2670 || !is_gimple_mem_ref_addr (x))
2672 error ("invalid first operand of MEM_REF");
2673 return x;
2675 if (TREE_CODE (TREE_OPERAND (t, 1)) != INTEGER_CST
2676 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 1))))
2678 error ("invalid offset operand of MEM_REF");
2679 return TREE_OPERAND (t, 1);
2681 if (TREE_CODE (x) == ADDR_EXPR
2682 && (x = verify_address (x, TREE_OPERAND (x, 0))))
2683 return x;
2684 *walk_subtrees = 0;
2685 break;
2687 case ASSERT_EXPR:
2688 x = fold (ASSERT_EXPR_COND (t));
2689 if (x == boolean_false_node)
2691 error ("ASSERT_EXPR with an always-false condition");
2692 return *tp;
2694 break;
2696 case MODIFY_EXPR:
2697 error ("MODIFY_EXPR not expected while having tuples");
2698 return *tp;
2700 case ADDR_EXPR:
2702 tree tem;
2704 gcc_assert (is_gimple_address (t));
2706 /* Skip any references (they will be checked when we recurse down the
2707 tree) and ensure that any variable used as a prefix is marked
2708 addressable. */
2709 for (x = TREE_OPERAND (t, 0);
2710 handled_component_p (x);
2711 x = TREE_OPERAND (x, 0))
2714 if ((tem = verify_address (t, x)))
2715 return tem;
2717 if (!(TREE_CODE (x) == VAR_DECL
2718 || TREE_CODE (x) == PARM_DECL
2719 || TREE_CODE (x) == RESULT_DECL))
2720 return NULL;
2722 if (!TREE_ADDRESSABLE (x))
2724 error ("address taken, but ADDRESSABLE bit not set");
2725 return x;
2728 break;
2731 case COND_EXPR:
2732 x = COND_EXPR_COND (t);
2733 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
2735 error ("non-integral used in condition");
2736 return x;
2738 if (!is_gimple_condexpr (x))
2740 error ("invalid conditional operand");
2741 return x;
2743 break;
2745 case NON_LVALUE_EXPR:
2746 case TRUTH_NOT_EXPR:
2747 gcc_unreachable ();
2749 CASE_CONVERT:
2750 case FIX_TRUNC_EXPR:
2751 case FLOAT_EXPR:
2752 case NEGATE_EXPR:
2753 case ABS_EXPR:
2754 case BIT_NOT_EXPR:
2755 CHECK_OP (0, "invalid operand to unary operator");
2756 break;
2758 case REALPART_EXPR:
2759 case IMAGPART_EXPR:
2760 case COMPONENT_REF:
2761 case ARRAY_REF:
2762 case ARRAY_RANGE_REF:
2763 case BIT_FIELD_REF:
2764 case VIEW_CONVERT_EXPR:
2765 /* We have a nest of references. Verify that each of the operands
2766 that determine where to reference is either a constant or a variable,
2767 verify that the base is valid, and then show we've already checked
2768 the subtrees. */
2769 while (handled_component_p (t))
2771 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
2772 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
2773 else if (TREE_CODE (t) == ARRAY_REF
2774 || TREE_CODE (t) == ARRAY_RANGE_REF)
2776 CHECK_OP (1, "invalid array index");
2777 if (TREE_OPERAND (t, 2))
2778 CHECK_OP (2, "invalid array lower bound");
2779 if (TREE_OPERAND (t, 3))
2780 CHECK_OP (3, "invalid array stride");
2782 else if (TREE_CODE (t) == BIT_FIELD_REF)
2784 if (!host_integerp (TREE_OPERAND (t, 1), 1)
2785 || !host_integerp (TREE_OPERAND (t, 2), 1))
2787 error ("invalid position or size operand to BIT_FIELD_REF");
2788 return t;
2790 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
2791 && (TYPE_PRECISION (TREE_TYPE (t))
2792 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
2794 error ("integral result type precision does not match "
2795 "field size of BIT_FIELD_REF");
2796 return t;
2798 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
2799 && !AGGREGATE_TYPE_P (TREE_TYPE (t))
2800 && TYPE_MODE (TREE_TYPE (t)) != BLKmode
2801 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
2802 != TREE_INT_CST_LOW (TREE_OPERAND (t, 1))))
2804 error ("mode precision of non-integral result does not "
2805 "match field size of BIT_FIELD_REF");
2806 return t;
2810 t = TREE_OPERAND (t, 0);
2813 if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
2815 error ("invalid reference prefix");
2816 return t;
2818 *walk_subtrees = 0;
2819 break;
2820 case PLUS_EXPR:
2821 case MINUS_EXPR:
2822 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
2823 POINTER_PLUS_EXPR. */
2824 if (POINTER_TYPE_P (TREE_TYPE (t)))
2826 error ("invalid operand to plus/minus, type is a pointer");
2827 return t;
2829 CHECK_OP (0, "invalid operand to binary operator");
2830 CHECK_OP (1, "invalid operand to binary operator");
2831 break;
2833 case POINTER_PLUS_EXPR:
2834 /* Check to make sure the first operand is a pointer or reference type. */
2835 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2837 error ("invalid operand to pointer plus, first operand is not a pointer");
2838 return t;
2840 /* Check to make sure the second operand is a ptrofftype. */
2841 if (!ptrofftype_p (TREE_TYPE (TREE_OPERAND (t, 1))))
2843 error ("invalid operand to pointer plus, second operand is not an "
2844 "integer type of appropriate width");
2845 return t;
2847 /* FALLTHROUGH */
2848 case LT_EXPR:
2849 case LE_EXPR:
2850 case GT_EXPR:
2851 case GE_EXPR:
2852 case EQ_EXPR:
2853 case NE_EXPR:
2854 case UNORDERED_EXPR:
2855 case ORDERED_EXPR:
2856 case UNLT_EXPR:
2857 case UNLE_EXPR:
2858 case UNGT_EXPR:
2859 case UNGE_EXPR:
2860 case UNEQ_EXPR:
2861 case LTGT_EXPR:
2862 case MULT_EXPR:
2863 case TRUNC_DIV_EXPR:
2864 case CEIL_DIV_EXPR:
2865 case FLOOR_DIV_EXPR:
2866 case ROUND_DIV_EXPR:
2867 case TRUNC_MOD_EXPR:
2868 case CEIL_MOD_EXPR:
2869 case FLOOR_MOD_EXPR:
2870 case ROUND_MOD_EXPR:
2871 case RDIV_EXPR:
2872 case EXACT_DIV_EXPR:
2873 case MIN_EXPR:
2874 case MAX_EXPR:
2875 case LSHIFT_EXPR:
2876 case RSHIFT_EXPR:
2877 case LROTATE_EXPR:
2878 case RROTATE_EXPR:
2879 case BIT_IOR_EXPR:
2880 case BIT_XOR_EXPR:
2881 case BIT_AND_EXPR:
2882 CHECK_OP (0, "invalid operand to binary operator");
2883 CHECK_OP (1, "invalid operand to binary operator");
2884 break;
2886 case CONSTRUCTOR:
2887 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2888 *walk_subtrees = 0;
2889 break;
2891 case CASE_LABEL_EXPR:
2892 if (CASE_CHAIN (t))
2894 error ("invalid CASE_CHAIN");
2895 return t;
2897 break;
2899 default:
2900 break;
2902 return NULL;
2904 #undef CHECK_OP
2908 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
2909 Returns true if there is an error, otherwise false. */
2911 static bool
2912 verify_types_in_gimple_min_lval (tree expr)
2914 tree op;
2916 if (is_gimple_id (expr))
2917 return false;
2919 if (TREE_CODE (expr) != TARGET_MEM_REF
2920 && TREE_CODE (expr) != MEM_REF)
2922 error ("invalid expression for min lvalue");
2923 return true;
2926 /* TARGET_MEM_REFs are strange beasts. */
2927 if (TREE_CODE (expr) == TARGET_MEM_REF)
2928 return false;
2930 op = TREE_OPERAND (expr, 0);
2931 if (!is_gimple_val (op))
2933 error ("invalid operand in indirect reference");
2934 debug_generic_stmt (op);
2935 return true;
2937 /* Memory references now generally can involve a value conversion. */
2939 return false;
2942 /* Verify if EXPR is a valid GIMPLE reference expression. If
2943 REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
2944 if there is an error, otherwise false. */
2946 static bool
2947 verify_types_in_gimple_reference (tree expr, bool require_lvalue)
2949 while (handled_component_p (expr))
2951 tree op = TREE_OPERAND (expr, 0);
2953 if (TREE_CODE (expr) == ARRAY_REF
2954 || TREE_CODE (expr) == ARRAY_RANGE_REF)
2956 if (!is_gimple_val (TREE_OPERAND (expr, 1))
2957 || (TREE_OPERAND (expr, 2)
2958 && !is_gimple_val (TREE_OPERAND (expr, 2)))
2959 || (TREE_OPERAND (expr, 3)
2960 && !is_gimple_val (TREE_OPERAND (expr, 3))))
2962 error ("invalid operands to array reference");
2963 debug_generic_stmt (expr);
2964 return true;
2968 /* Verify if the reference array element types are compatible. */
2969 if (TREE_CODE (expr) == ARRAY_REF
2970 && !useless_type_conversion_p (TREE_TYPE (expr),
2971 TREE_TYPE (TREE_TYPE (op))))
2973 error ("type mismatch in array reference");
2974 debug_generic_stmt (TREE_TYPE (expr));
2975 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
2976 return true;
2978 if (TREE_CODE (expr) == ARRAY_RANGE_REF
2979 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
2980 TREE_TYPE (TREE_TYPE (op))))
2982 error ("type mismatch in array range reference");
2983 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
2984 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
2985 return true;
2988 if ((TREE_CODE (expr) == REALPART_EXPR
2989 || TREE_CODE (expr) == IMAGPART_EXPR)
2990 && !useless_type_conversion_p (TREE_TYPE (expr),
2991 TREE_TYPE (TREE_TYPE (op))))
2993 error ("type mismatch in real/imagpart reference");
2994 debug_generic_stmt (TREE_TYPE (expr));
2995 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
2996 return true;
2999 if (TREE_CODE (expr) == COMPONENT_REF
3000 && !useless_type_conversion_p (TREE_TYPE (expr),
3001 TREE_TYPE (TREE_OPERAND (expr, 1))))
3003 error ("type mismatch in component reference");
3004 debug_generic_stmt (TREE_TYPE (expr));
3005 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3006 return true;
3009 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
3011 /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
3012 that their operand is not an SSA name or an invariant when
3013 requiring an lvalue (this usually means there is a SRA or IPA-SRA
3014 bug). Otherwise there is nothing to verify, gross mismatches at
3015 most invoke undefined behavior. */
3016 if (require_lvalue
3017 && (TREE_CODE (op) == SSA_NAME
3018 || is_gimple_min_invariant (op)))
3020 error ("conversion of an SSA_NAME on the left hand side");
3021 debug_generic_stmt (expr);
3022 return true;
3024 else if (TREE_CODE (op) == SSA_NAME
3025 && TYPE_SIZE (TREE_TYPE (expr)) != TYPE_SIZE (TREE_TYPE (op)))
3027 error ("conversion of register to a different size");
3028 debug_generic_stmt (expr);
3029 return true;
3031 else if (!handled_component_p (op))
3032 return false;
3035 expr = op;
3038 if (TREE_CODE (expr) == MEM_REF)
3040 if (!is_gimple_mem_ref_addr (TREE_OPERAND (expr, 0)))
3042 error ("invalid address operand in MEM_REF");
3043 debug_generic_stmt (expr);
3044 return true;
3046 if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
3047 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
3049 error ("invalid offset operand in MEM_REF");
3050 debug_generic_stmt (expr);
3051 return true;
3054 else if (TREE_CODE (expr) == TARGET_MEM_REF)
3056 if (!TMR_BASE (expr)
3057 || !is_gimple_mem_ref_addr (TMR_BASE (expr)))
3059 error ("invalid address operand in TARGET_MEM_REF");
3060 return true;
3062 if (!TMR_OFFSET (expr)
3063 || TREE_CODE (TMR_OFFSET (expr)) != INTEGER_CST
3064 || !POINTER_TYPE_P (TREE_TYPE (TMR_OFFSET (expr))))
3066 error ("invalid offset operand in TARGET_MEM_REF");
3067 debug_generic_stmt (expr);
3068 return true;
3072 return ((require_lvalue || !is_gimple_min_invariant (expr))
3073 && verify_types_in_gimple_min_lval (expr));
3076 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3077 list of pointer-to types that is trivially convertible to DEST. */
3079 static bool
3080 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3082 tree src;
3084 if (!TYPE_POINTER_TO (src_obj))
3085 return true;
3087 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3088 if (useless_type_conversion_p (dest, src))
3089 return true;
3091 return false;
3094 /* Return true if TYPE1 is a fixed-point type and if conversions to and
3095 from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
3097 static bool
3098 valid_fixed_convert_types_p (tree type1, tree type2)
3100 return (FIXED_POINT_TYPE_P (type1)
3101 && (INTEGRAL_TYPE_P (type2)
3102 || SCALAR_FLOAT_TYPE_P (type2)
3103 || FIXED_POINT_TYPE_P (type2)));
3106 /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
3107 is a problem, otherwise false. */
3109 static bool
3110 verify_gimple_call (gimple stmt)
3112 tree fn = gimple_call_fn (stmt);
3113 tree fntype, fndecl;
3114 unsigned i;
3116 if (gimple_call_internal_p (stmt))
3118 if (fn)
3120 error ("gimple call has two targets");
3121 debug_generic_stmt (fn);
3122 return true;
3125 else
3127 if (!fn)
3129 error ("gimple call has no target");
3130 return true;
3134 if (fn && !is_gimple_call_addr (fn))
3136 error ("invalid function in gimple call");
3137 debug_generic_stmt (fn);
3138 return true;
3141 if (fn
3142 && (!POINTER_TYPE_P (TREE_TYPE (fn))
3143 || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
3144 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE)))
3146 error ("non-function in gimple call");
3147 return true;
3150 fndecl = gimple_call_fndecl (stmt);
3151 if (fndecl
3152 && TREE_CODE (fndecl) == FUNCTION_DECL
3153 && DECL_LOOPING_CONST_OR_PURE_P (fndecl)
3154 && !DECL_PURE_P (fndecl)
3155 && !TREE_READONLY (fndecl))
3157 error ("invalid pure const state for function");
3158 return true;
3161 if (gimple_call_lhs (stmt)
3162 && (!is_gimple_lvalue (gimple_call_lhs (stmt))
3163 || verify_types_in_gimple_reference (gimple_call_lhs (stmt), true)))
3165 error ("invalid LHS in gimple call");
3166 return true;
3169 if (gimple_call_lhs (stmt) && gimple_call_noreturn_p (stmt))
3171 error ("LHS in noreturn call");
3172 return true;
3175 fntype = gimple_call_fntype (stmt);
3176 if (fntype
3177 && gimple_call_lhs (stmt)
3178 && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
3179 TREE_TYPE (fntype))
3180 /* ??? At least C++ misses conversions at assignments from
3181 void * call results.
3182 ??? Java is completely off. Especially with functions
3183 returning java.lang.Object.
3184 For now simply allow arbitrary pointer type conversions. */
3185 && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
3186 && POINTER_TYPE_P (TREE_TYPE (fntype))))
3188 error ("invalid conversion in gimple call");
3189 debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
3190 debug_generic_stmt (TREE_TYPE (fntype));
3191 return true;
3194 if (gimple_call_chain (stmt)
3195 && !is_gimple_val (gimple_call_chain (stmt)))
3197 error ("invalid static chain in gimple call");
3198 debug_generic_stmt (gimple_call_chain (stmt));
3199 return true;
3202 /* If there is a static chain argument, this should not be an indirect
3203 call, and the decl should have DECL_STATIC_CHAIN set. */
3204 if (gimple_call_chain (stmt))
3206 if (!gimple_call_fndecl (stmt))
3208 error ("static chain in indirect gimple call");
3209 return true;
3211 fn = TREE_OPERAND (fn, 0);
3213 if (!DECL_STATIC_CHAIN (fn))
3215 error ("static chain with function that doesn%'t use one");
3216 return true;
3220 /* ??? The C frontend passes unpromoted arguments in case it
3221 didn't see a function declaration before the call. So for now
3222 leave the call arguments mostly unverified. Once we gimplify
3223 unit-at-a-time we have a chance to fix this. */
3225 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3227 tree arg = gimple_call_arg (stmt, i);
3228 if ((is_gimple_reg_type (TREE_TYPE (arg))
3229 && !is_gimple_val (arg))
3230 || (!is_gimple_reg_type (TREE_TYPE (arg))
3231 && !is_gimple_lvalue (arg)))
3233 error ("invalid argument to gimple call");
3234 debug_generic_expr (arg);
3235 return true;
3239 return false;
3242 /* Verifies the gimple comparison with the result type TYPE and
3243 the operands OP0 and OP1. */
3245 static bool
3246 verify_gimple_comparison (tree type, tree op0, tree op1)
3248 tree op0_type = TREE_TYPE (op0);
3249 tree op1_type = TREE_TYPE (op1);
3251 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3253 error ("invalid operands in gimple comparison");
3254 return true;
3257 /* For comparisons we do not have the operations type as the
3258 effective type the comparison is carried out in. Instead
3259 we require that either the first operand is trivially
3260 convertible into the second, or the other way around.
3261 Because we special-case pointers to void we allow
3262 comparisons of pointers with the same mode as well. */
3263 if (!useless_type_conversion_p (op0_type, op1_type)
3264 && !useless_type_conversion_p (op1_type, op0_type)
3265 && (!POINTER_TYPE_P (op0_type)
3266 || !POINTER_TYPE_P (op1_type)
3267 || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
3269 error ("mismatching comparison operand types");
3270 debug_generic_expr (op0_type);
3271 debug_generic_expr (op1_type);
3272 return true;
3275 /* The resulting type of a comparison may be an effective boolean type. */
3276 if (INTEGRAL_TYPE_P (type)
3277 && (TREE_CODE (type) == BOOLEAN_TYPE
3278 || TYPE_PRECISION (type) == 1))
3280 /* Or an integer vector type with the same size and element count
3281 as the comparison operand types. */
3282 else if (TREE_CODE (type) == VECTOR_TYPE
3283 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
3285 if (TREE_CODE (op0_type) != VECTOR_TYPE
3286 || TREE_CODE (op1_type) != VECTOR_TYPE)
3288 error ("non-vector operands in vector comparison");
3289 debug_generic_expr (op0_type);
3290 debug_generic_expr (op1_type);
3291 return true;
3294 if (TYPE_VECTOR_SUBPARTS (type) != TYPE_VECTOR_SUBPARTS (op0_type)
3295 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)))
3296 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0_type)))))
3298 error ("invalid vector comparison resulting type");
3299 debug_generic_expr (type);
3300 return true;
3303 else
3305 error ("bogus comparison result type");
3306 debug_generic_expr (type);
3307 return true;
3310 return false;
3313 /* Verify a gimple assignment statement STMT with an unary rhs.
3314 Returns true if anything is wrong. */
3316 static bool
3317 verify_gimple_assign_unary (gimple stmt)
3319 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3320 tree lhs = gimple_assign_lhs (stmt);
3321 tree lhs_type = TREE_TYPE (lhs);
3322 tree rhs1 = gimple_assign_rhs1 (stmt);
3323 tree rhs1_type = TREE_TYPE (rhs1);
3325 if (!is_gimple_reg (lhs))
3327 error ("non-register as LHS of unary operation");
3328 return true;
3331 if (!is_gimple_val (rhs1))
3333 error ("invalid operand in unary operation");
3334 return true;
3337 /* First handle conversions. */
3338 switch (rhs_code)
3340 CASE_CONVERT:
3342 /* Allow conversions from pointer type to integral type only if
3343 there is no sign or zero extension involved.
3344 For targets were the precision of ptrofftype doesn't match that
3345 of pointers we need to allow arbitrary conversions to ptrofftype. */
3346 if ((POINTER_TYPE_P (lhs_type)
3347 && INTEGRAL_TYPE_P (rhs1_type))
3348 || (POINTER_TYPE_P (rhs1_type)
3349 && INTEGRAL_TYPE_P (lhs_type)
3350 && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
3351 || ptrofftype_p (sizetype))))
3352 return false;
3354 /* Allow conversion from integral to offset type and vice versa. */
3355 if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3356 && INTEGRAL_TYPE_P (rhs1_type))
3357 || (INTEGRAL_TYPE_P (lhs_type)
3358 && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3359 return false;
3361 /* Otherwise assert we are converting between types of the
3362 same kind. */
3363 if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3365 error ("invalid types in nop conversion");
3366 debug_generic_expr (lhs_type);
3367 debug_generic_expr (rhs1_type);
3368 return true;
3371 return false;
3374 case ADDR_SPACE_CONVERT_EXPR:
3376 if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
3377 || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
3378 == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
3380 error ("invalid types in address space conversion");
3381 debug_generic_expr (lhs_type);
3382 debug_generic_expr (rhs1_type);
3383 return true;
3386 return false;
3389 case FIXED_CONVERT_EXPR:
3391 if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3392 && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3394 error ("invalid types in fixed-point conversion");
3395 debug_generic_expr (lhs_type);
3396 debug_generic_expr (rhs1_type);
3397 return true;
3400 return false;
3403 case FLOAT_EXPR:
3405 if ((!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3406 && (!VECTOR_INTEGER_TYPE_P (rhs1_type)
3407 || !VECTOR_FLOAT_TYPE_P(lhs_type)))
3409 error ("invalid types in conversion to floating point");
3410 debug_generic_expr (lhs_type);
3411 debug_generic_expr (rhs1_type);
3412 return true;
3415 return false;
3418 case FIX_TRUNC_EXPR:
3420 if ((!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3421 && (!VECTOR_INTEGER_TYPE_P (lhs_type)
3422 || !VECTOR_FLOAT_TYPE_P(rhs1_type)))
3424 error ("invalid types in conversion to integer");
3425 debug_generic_expr (lhs_type);
3426 debug_generic_expr (rhs1_type);
3427 return true;
3430 return false;
3433 case VEC_UNPACK_HI_EXPR:
3434 case VEC_UNPACK_LO_EXPR:
3435 case REDUC_MAX_EXPR:
3436 case REDUC_MIN_EXPR:
3437 case REDUC_PLUS_EXPR:
3438 case VEC_UNPACK_FLOAT_HI_EXPR:
3439 case VEC_UNPACK_FLOAT_LO_EXPR:
3440 /* FIXME. */
3441 return false;
3443 case NEGATE_EXPR:
3444 case ABS_EXPR:
3445 case BIT_NOT_EXPR:
3446 case PAREN_EXPR:
3447 case NON_LVALUE_EXPR:
3448 case CONJ_EXPR:
3449 break;
3451 default:
3452 gcc_unreachable ();
3455 /* For the remaining codes assert there is no conversion involved. */
3456 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3458 error ("non-trivial conversion in unary operation");
3459 debug_generic_expr (lhs_type);
3460 debug_generic_expr (rhs1_type);
3461 return true;
3464 return false;
3467 /* Verify a gimple assignment statement STMT with a binary rhs.
3468 Returns true if anything is wrong. */
3470 static bool
3471 verify_gimple_assign_binary (gimple stmt)
3473 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3474 tree lhs = gimple_assign_lhs (stmt);
3475 tree lhs_type = TREE_TYPE (lhs);
3476 tree rhs1 = gimple_assign_rhs1 (stmt);
3477 tree rhs1_type = TREE_TYPE (rhs1);
3478 tree rhs2 = gimple_assign_rhs2 (stmt);
3479 tree rhs2_type = TREE_TYPE (rhs2);
3481 if (!is_gimple_reg (lhs))
3483 error ("non-register as LHS of binary operation");
3484 return true;
3487 if (!is_gimple_val (rhs1)
3488 || !is_gimple_val (rhs2))
3490 error ("invalid operands in binary operation");
3491 return true;
3494 /* First handle operations that involve different types. */
3495 switch (rhs_code)
3497 case COMPLEX_EXPR:
3499 if (TREE_CODE (lhs_type) != COMPLEX_TYPE
3500 || !(INTEGRAL_TYPE_P (rhs1_type)
3501 || SCALAR_FLOAT_TYPE_P (rhs1_type))
3502 || !(INTEGRAL_TYPE_P (rhs2_type)
3503 || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3505 error ("type mismatch in complex expression");
3506 debug_generic_expr (lhs_type);
3507 debug_generic_expr (rhs1_type);
3508 debug_generic_expr (rhs2_type);
3509 return true;
3512 return false;
3515 case LSHIFT_EXPR:
3516 case RSHIFT_EXPR:
3517 case LROTATE_EXPR:
3518 case RROTATE_EXPR:
3520 /* Shifts and rotates are ok on integral types, fixed point
3521 types and integer vector types. */
3522 if ((!INTEGRAL_TYPE_P (rhs1_type)
3523 && !FIXED_POINT_TYPE_P (rhs1_type)
3524 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3525 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
3526 || (!INTEGRAL_TYPE_P (rhs2_type)
3527 /* Vector shifts of vectors are also ok. */
3528 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3529 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3530 && TREE_CODE (rhs2_type) == VECTOR_TYPE
3531 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3532 || !useless_type_conversion_p (lhs_type, rhs1_type))
3534 error ("type mismatch in shift expression");
3535 debug_generic_expr (lhs_type);
3536 debug_generic_expr (rhs1_type);
3537 debug_generic_expr (rhs2_type);
3538 return true;
3541 return false;
3544 case VEC_LSHIFT_EXPR:
3545 case VEC_RSHIFT_EXPR:
3547 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3548 || !(INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3549 || POINTER_TYPE_P (TREE_TYPE (rhs1_type))
3550 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1_type))
3551 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
3552 || (!INTEGRAL_TYPE_P (rhs2_type)
3553 && (TREE_CODE (rhs2_type) != VECTOR_TYPE
3554 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3555 || !useless_type_conversion_p (lhs_type, rhs1_type))
3557 error ("type mismatch in vector shift expression");
3558 debug_generic_expr (lhs_type);
3559 debug_generic_expr (rhs1_type);
3560 debug_generic_expr (rhs2_type);
3561 return true;
3563 /* For shifting a vector of non-integral components we
3564 only allow shifting by a constant multiple of the element size. */
3565 if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3566 && (TREE_CODE (rhs2) != INTEGER_CST
3567 || !div_if_zero_remainder (EXACT_DIV_EXPR, rhs2,
3568 TYPE_SIZE (TREE_TYPE (rhs1_type)))))
3570 error ("non-element sized vector shift of floating point vector");
3571 return true;
3574 return false;
3577 case WIDEN_LSHIFT_EXPR:
3579 if (!INTEGRAL_TYPE_P (lhs_type)
3580 || !INTEGRAL_TYPE_P (rhs1_type)
3581 || TREE_CODE (rhs2) != INTEGER_CST
3582 || (2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)))
3584 error ("type mismatch in widening vector shift expression");
3585 debug_generic_expr (lhs_type);
3586 debug_generic_expr (rhs1_type);
3587 debug_generic_expr (rhs2_type);
3588 return true;
3591 return false;
3594 case VEC_WIDEN_LSHIFT_HI_EXPR:
3595 case VEC_WIDEN_LSHIFT_LO_EXPR:
3597 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3598 || TREE_CODE (lhs_type) != VECTOR_TYPE
3599 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3600 || !INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3601 || TREE_CODE (rhs2) != INTEGER_CST
3602 || (2 * TYPE_PRECISION (TREE_TYPE (rhs1_type))
3603 > TYPE_PRECISION (TREE_TYPE (lhs_type))))
3605 error ("type mismatch in widening vector shift expression");
3606 debug_generic_expr (lhs_type);
3607 debug_generic_expr (rhs1_type);
3608 debug_generic_expr (rhs2_type);
3609 return true;
3612 return false;
3615 case PLUS_EXPR:
3616 case MINUS_EXPR:
3618 /* We use regular PLUS_EXPR and MINUS_EXPR for vectors.
3619 ??? This just makes the checker happy and may not be what is
3620 intended. */
3621 if (TREE_CODE (lhs_type) == VECTOR_TYPE
3622 && POINTER_TYPE_P (TREE_TYPE (lhs_type)))
3624 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3625 || TREE_CODE (rhs2_type) != VECTOR_TYPE)
3627 error ("invalid non-vector operands to vector valued plus");
3628 return true;
3630 lhs_type = TREE_TYPE (lhs_type);
3631 rhs1_type = TREE_TYPE (rhs1_type);
3632 rhs2_type = TREE_TYPE (rhs2_type);
3633 /* PLUS_EXPR is commutative, so we might end up canonicalizing
3634 the pointer to 2nd place. */
3635 if (POINTER_TYPE_P (rhs2_type))
3637 tree tem = rhs1_type;
3638 rhs1_type = rhs2_type;
3639 rhs2_type = tem;
3641 goto do_pointer_plus_expr_check;
3643 if (POINTER_TYPE_P (lhs_type)
3644 || POINTER_TYPE_P (rhs1_type)
3645 || POINTER_TYPE_P (rhs2_type))
3647 error ("invalid (pointer) operands to plus/minus");
3648 return true;
3651 /* Continue with generic binary expression handling. */
3652 break;
3655 case POINTER_PLUS_EXPR:
3657 do_pointer_plus_expr_check:
3658 if (!POINTER_TYPE_P (rhs1_type)
3659 || !useless_type_conversion_p (lhs_type, rhs1_type)
3660 || !ptrofftype_p (rhs2_type))
3662 error ("type mismatch in pointer plus expression");
3663 debug_generic_stmt (lhs_type);
3664 debug_generic_stmt (rhs1_type);
3665 debug_generic_stmt (rhs2_type);
3666 return true;
3669 return false;
3672 case TRUTH_ANDIF_EXPR:
3673 case TRUTH_ORIF_EXPR:
3674 case TRUTH_AND_EXPR:
3675 case TRUTH_OR_EXPR:
3676 case TRUTH_XOR_EXPR:
3678 gcc_unreachable ();
3680 case LT_EXPR:
3681 case LE_EXPR:
3682 case GT_EXPR:
3683 case GE_EXPR:
3684 case EQ_EXPR:
3685 case NE_EXPR:
3686 case UNORDERED_EXPR:
3687 case ORDERED_EXPR:
3688 case UNLT_EXPR:
3689 case UNLE_EXPR:
3690 case UNGT_EXPR:
3691 case UNGE_EXPR:
3692 case UNEQ_EXPR:
3693 case LTGT_EXPR:
3694 /* Comparisons are also binary, but the result type is not
3695 connected to the operand types. */
3696 return verify_gimple_comparison (lhs_type, rhs1, rhs2);
3698 case WIDEN_MULT_EXPR:
3699 if (TREE_CODE (lhs_type) != INTEGER_TYPE)
3700 return true;
3701 return ((2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type))
3702 || (TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type)));
3704 case WIDEN_SUM_EXPR:
3705 case VEC_WIDEN_MULT_HI_EXPR:
3706 case VEC_WIDEN_MULT_LO_EXPR:
3707 case VEC_WIDEN_MULT_EVEN_EXPR:
3708 case VEC_WIDEN_MULT_ODD_EXPR:
3709 case VEC_PACK_TRUNC_EXPR:
3710 case VEC_PACK_SAT_EXPR:
3711 case VEC_PACK_FIX_TRUNC_EXPR:
3712 /* FIXME. */
3713 return false;
3715 case MULT_EXPR:
3716 case MULT_HIGHPART_EXPR:
3717 case TRUNC_DIV_EXPR:
3718 case CEIL_DIV_EXPR:
3719 case FLOOR_DIV_EXPR:
3720 case ROUND_DIV_EXPR:
3721 case TRUNC_MOD_EXPR:
3722 case CEIL_MOD_EXPR:
3723 case FLOOR_MOD_EXPR:
3724 case ROUND_MOD_EXPR:
3725 case RDIV_EXPR:
3726 case EXACT_DIV_EXPR:
3727 case MIN_EXPR:
3728 case MAX_EXPR:
3729 case BIT_IOR_EXPR:
3730 case BIT_XOR_EXPR:
3731 case BIT_AND_EXPR:
3732 /* Continue with generic binary expression handling. */
3733 break;
3735 default:
3736 gcc_unreachable ();
3739 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3740 || !useless_type_conversion_p (lhs_type, rhs2_type))
3742 error ("type mismatch in binary expression");
3743 debug_generic_stmt (lhs_type);
3744 debug_generic_stmt (rhs1_type);
3745 debug_generic_stmt (rhs2_type);
3746 return true;
3749 return false;
3752 /* Verify a gimple assignment statement STMT with a ternary rhs.
3753 Returns true if anything is wrong. */
3755 static bool
3756 verify_gimple_assign_ternary (gimple stmt)
3758 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3759 tree lhs = gimple_assign_lhs (stmt);
3760 tree lhs_type = TREE_TYPE (lhs);
3761 tree rhs1 = gimple_assign_rhs1 (stmt);
3762 tree rhs1_type = TREE_TYPE (rhs1);
3763 tree rhs2 = gimple_assign_rhs2 (stmt);
3764 tree rhs2_type = TREE_TYPE (rhs2);
3765 tree rhs3 = gimple_assign_rhs3 (stmt);
3766 tree rhs3_type = TREE_TYPE (rhs3);
3768 if (!is_gimple_reg (lhs))
3770 error ("non-register as LHS of ternary operation");
3771 return true;
3774 if (((rhs_code == VEC_COND_EXPR || rhs_code == COND_EXPR)
3775 ? !is_gimple_condexpr (rhs1) : !is_gimple_val (rhs1))
3776 || !is_gimple_val (rhs2)
3777 || !is_gimple_val (rhs3))
3779 error ("invalid operands in ternary operation");
3780 return true;
3783 /* First handle operations that involve different types. */
3784 switch (rhs_code)
3786 case WIDEN_MULT_PLUS_EXPR:
3787 case WIDEN_MULT_MINUS_EXPR:
3788 if ((!INTEGRAL_TYPE_P (rhs1_type)
3789 && !FIXED_POINT_TYPE_P (rhs1_type))
3790 || !useless_type_conversion_p (rhs1_type, rhs2_type)
3791 || !useless_type_conversion_p (lhs_type, rhs3_type)
3792 || 2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)
3793 || TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type))
3795 error ("type mismatch in widening multiply-accumulate expression");
3796 debug_generic_expr (lhs_type);
3797 debug_generic_expr (rhs1_type);
3798 debug_generic_expr (rhs2_type);
3799 debug_generic_expr (rhs3_type);
3800 return true;
3802 break;
3804 case FMA_EXPR:
3805 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3806 || !useless_type_conversion_p (lhs_type, rhs2_type)
3807 || !useless_type_conversion_p (lhs_type, rhs3_type))
3809 error ("type mismatch in fused multiply-add expression");
3810 debug_generic_expr (lhs_type);
3811 debug_generic_expr (rhs1_type);
3812 debug_generic_expr (rhs2_type);
3813 debug_generic_expr (rhs3_type);
3814 return true;
3816 break;
3818 case COND_EXPR:
3819 case VEC_COND_EXPR:
3820 if (!useless_type_conversion_p (lhs_type, rhs2_type)
3821 || !useless_type_conversion_p (lhs_type, rhs3_type))
3823 error ("type mismatch in conditional expression");
3824 debug_generic_expr (lhs_type);
3825 debug_generic_expr (rhs2_type);
3826 debug_generic_expr (rhs3_type);
3827 return true;
3829 break;
3831 case VEC_PERM_EXPR:
3832 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3833 || !useless_type_conversion_p (lhs_type, rhs2_type))
3835 error ("type mismatch in vector permute expression");
3836 debug_generic_expr (lhs_type);
3837 debug_generic_expr (rhs1_type);
3838 debug_generic_expr (rhs2_type);
3839 debug_generic_expr (rhs3_type);
3840 return true;
3843 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3844 || TREE_CODE (rhs2_type) != VECTOR_TYPE
3845 || TREE_CODE (rhs3_type) != VECTOR_TYPE)
3847 error ("vector types expected in vector permute expression");
3848 debug_generic_expr (lhs_type);
3849 debug_generic_expr (rhs1_type);
3850 debug_generic_expr (rhs2_type);
3851 debug_generic_expr (rhs3_type);
3852 return true;
3855 if (TYPE_VECTOR_SUBPARTS (rhs1_type) != TYPE_VECTOR_SUBPARTS (rhs2_type)
3856 || TYPE_VECTOR_SUBPARTS (rhs2_type)
3857 != TYPE_VECTOR_SUBPARTS (rhs3_type)
3858 || TYPE_VECTOR_SUBPARTS (rhs3_type)
3859 != TYPE_VECTOR_SUBPARTS (lhs_type))
3861 error ("vectors with different element number found "
3862 "in vector permute expression");
3863 debug_generic_expr (lhs_type);
3864 debug_generic_expr (rhs1_type);
3865 debug_generic_expr (rhs2_type);
3866 debug_generic_expr (rhs3_type);
3867 return true;
3870 if (TREE_CODE (TREE_TYPE (rhs3_type)) != INTEGER_TYPE
3871 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs3_type)))
3872 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1_type))))
3874 error ("invalid mask type in vector permute expression");
3875 debug_generic_expr (lhs_type);
3876 debug_generic_expr (rhs1_type);
3877 debug_generic_expr (rhs2_type);
3878 debug_generic_expr (rhs3_type);
3879 return true;
3882 return false;
3884 case DOT_PROD_EXPR:
3885 case REALIGN_LOAD_EXPR:
3886 /* FIXME. */
3887 return false;
3889 default:
3890 gcc_unreachable ();
3892 return false;
3895 /* Verify a gimple assignment statement STMT with a single rhs.
3896 Returns true if anything is wrong. */
3898 static bool
3899 verify_gimple_assign_single (gimple stmt)
3901 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3902 tree lhs = gimple_assign_lhs (stmt);
3903 tree lhs_type = TREE_TYPE (lhs);
3904 tree rhs1 = gimple_assign_rhs1 (stmt);
3905 tree rhs1_type = TREE_TYPE (rhs1);
3906 bool res = false;
3908 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3910 error ("non-trivial conversion at assignment");
3911 debug_generic_expr (lhs_type);
3912 debug_generic_expr (rhs1_type);
3913 return true;
3916 if (gimple_clobber_p (stmt)
3917 && !DECL_P (lhs))
3919 error ("non-decl LHS in clobber statement");
3920 debug_generic_expr (lhs);
3921 return true;
3924 if (handled_component_p (lhs))
3925 res |= verify_types_in_gimple_reference (lhs, true);
3927 /* Special codes we cannot handle via their class. */
3928 switch (rhs_code)
3930 case ADDR_EXPR:
3932 tree op = TREE_OPERAND (rhs1, 0);
3933 if (!is_gimple_addressable (op))
3935 error ("invalid operand in unary expression");
3936 return true;
3939 /* Technically there is no longer a need for matching types, but
3940 gimple hygiene asks for this check. In LTO we can end up
3941 combining incompatible units and thus end up with addresses
3942 of globals that change their type to a common one. */
3943 if (!in_lto_p
3944 && !types_compatible_p (TREE_TYPE (op),
3945 TREE_TYPE (TREE_TYPE (rhs1)))
3946 && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
3947 TREE_TYPE (op)))
3949 error ("type mismatch in address expression");
3950 debug_generic_stmt (TREE_TYPE (rhs1));
3951 debug_generic_stmt (TREE_TYPE (op));
3952 return true;
3955 return verify_types_in_gimple_reference (op, true);
3958 /* tcc_reference */
3959 case INDIRECT_REF:
3960 error ("INDIRECT_REF in gimple IL");
3961 return true;
3963 case COMPONENT_REF:
3964 case BIT_FIELD_REF:
3965 case ARRAY_REF:
3966 case ARRAY_RANGE_REF:
3967 case VIEW_CONVERT_EXPR:
3968 case REALPART_EXPR:
3969 case IMAGPART_EXPR:
3970 case TARGET_MEM_REF:
3971 case MEM_REF:
3972 if (!is_gimple_reg (lhs)
3973 && is_gimple_reg_type (TREE_TYPE (lhs)))
3975 error ("invalid rhs for gimple memory store");
3976 debug_generic_stmt (lhs);
3977 debug_generic_stmt (rhs1);
3978 return true;
3980 return res || verify_types_in_gimple_reference (rhs1, false);
3982 /* tcc_constant */
3983 case SSA_NAME:
3984 case INTEGER_CST:
3985 case REAL_CST:
3986 case FIXED_CST:
3987 case COMPLEX_CST:
3988 case VECTOR_CST:
3989 case STRING_CST:
3990 return res;
3992 /* tcc_declaration */
3993 case CONST_DECL:
3994 return res;
3995 case VAR_DECL:
3996 case PARM_DECL:
3997 if (!is_gimple_reg (lhs)
3998 && !is_gimple_reg (rhs1)
3999 && is_gimple_reg_type (TREE_TYPE (lhs)))
4001 error ("invalid rhs for gimple memory store");
4002 debug_generic_stmt (lhs);
4003 debug_generic_stmt (rhs1);
4004 return true;
4006 return res;
4008 case CONSTRUCTOR:
4009 case OBJ_TYPE_REF:
4010 case ASSERT_EXPR:
4011 case WITH_SIZE_EXPR:
4012 /* FIXME. */
4013 return res;
4015 default:;
4018 return res;
4021 /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
4022 is a problem, otherwise false. */
4024 static bool
4025 verify_gimple_assign (gimple stmt)
4027 switch (gimple_assign_rhs_class (stmt))
4029 case GIMPLE_SINGLE_RHS:
4030 return verify_gimple_assign_single (stmt);
4032 case GIMPLE_UNARY_RHS:
4033 return verify_gimple_assign_unary (stmt);
4035 case GIMPLE_BINARY_RHS:
4036 return verify_gimple_assign_binary (stmt);
4038 case GIMPLE_TERNARY_RHS:
4039 return verify_gimple_assign_ternary (stmt);
4041 default:
4042 gcc_unreachable ();
4046 /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
4047 is a problem, otherwise false. */
4049 static bool
4050 verify_gimple_return (gimple stmt)
4052 tree op = gimple_return_retval (stmt);
4053 tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
4055 /* We cannot test for present return values as we do not fix up missing
4056 return values from the original source. */
4057 if (op == NULL)
4058 return false;
4060 if (!is_gimple_val (op)
4061 && TREE_CODE (op) != RESULT_DECL)
4063 error ("invalid operand in return statement");
4064 debug_generic_stmt (op);
4065 return true;
4068 if ((TREE_CODE (op) == RESULT_DECL
4069 && DECL_BY_REFERENCE (op))
4070 || (TREE_CODE (op) == SSA_NAME
4071 && SSA_NAME_VAR (op)
4072 && TREE_CODE (SSA_NAME_VAR (op)) == RESULT_DECL
4073 && DECL_BY_REFERENCE (SSA_NAME_VAR (op))))
4074 op = TREE_TYPE (op);
4076 if (!useless_type_conversion_p (restype, TREE_TYPE (op)))
4078 error ("invalid conversion in return statement");
4079 debug_generic_stmt (restype);
4080 debug_generic_stmt (TREE_TYPE (op));
4081 return true;
4084 return false;
4088 /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
4089 is a problem, otherwise false. */
4091 static bool
4092 verify_gimple_goto (gimple stmt)
4094 tree dest = gimple_goto_dest (stmt);
4096 /* ??? We have two canonical forms of direct goto destinations, a
4097 bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
4098 if (TREE_CODE (dest) != LABEL_DECL
4099 && (!is_gimple_val (dest)
4100 || !POINTER_TYPE_P (TREE_TYPE (dest))))
4102 error ("goto destination is neither a label nor a pointer");
4103 return true;
4106 return false;
4109 /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
4110 is a problem, otherwise false. */
4112 static bool
4113 verify_gimple_switch (gimple stmt)
4115 unsigned int i, n;
4116 tree elt, prev_upper_bound = NULL_TREE;
4117 tree index_type, elt_type = NULL_TREE;
4119 if (!is_gimple_val (gimple_switch_index (stmt)))
4121 error ("invalid operand to switch statement");
4122 debug_generic_stmt (gimple_switch_index (stmt));
4123 return true;
4126 index_type = TREE_TYPE (gimple_switch_index (stmt));
4127 if (! INTEGRAL_TYPE_P (index_type))
4129 error ("non-integral type switch statement");
4130 debug_generic_expr (index_type);
4131 return true;
4134 elt = gimple_switch_label (stmt, 0);
4135 if (CASE_LOW (elt) != NULL_TREE || CASE_HIGH (elt) != NULL_TREE)
4137 error ("invalid default case label in switch statement");
4138 debug_generic_expr (elt);
4139 return true;
4142 n = gimple_switch_num_labels (stmt);
4143 for (i = 1; i < n; i++)
4145 elt = gimple_switch_label (stmt, i);
4147 if (! CASE_LOW (elt))
4149 error ("invalid case label in switch statement");
4150 debug_generic_expr (elt);
4151 return true;
4153 if (CASE_HIGH (elt)
4154 && ! tree_int_cst_lt (CASE_LOW (elt), CASE_HIGH (elt)))
4156 error ("invalid case range in switch statement");
4157 debug_generic_expr (elt);
4158 return true;
4161 if (elt_type)
4163 if (TREE_TYPE (CASE_LOW (elt)) != elt_type
4164 || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
4166 error ("type mismatch for case label in switch statement");
4167 debug_generic_expr (elt);
4168 return true;
4171 else
4173 elt_type = TREE_TYPE (CASE_LOW (elt));
4174 if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
4176 error ("type precision mismatch in switch statement");
4177 return true;
4181 if (prev_upper_bound)
4183 if (! tree_int_cst_lt (prev_upper_bound, CASE_LOW (elt)))
4185 error ("case labels not sorted in switch statement");
4186 return true;
4190 prev_upper_bound = CASE_HIGH (elt);
4191 if (! prev_upper_bound)
4192 prev_upper_bound = CASE_LOW (elt);
4195 return false;
4198 /* Verify a gimple debug statement STMT.
4199 Returns true if anything is wrong. */
4201 static bool
4202 verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
4204 /* There isn't much that could be wrong in a gimple debug stmt. A
4205 gimple debug bind stmt, for example, maps a tree, that's usually
4206 a VAR_DECL or a PARM_DECL, but that could also be some scalarized
4207 component or member of an aggregate type, to another tree, that
4208 can be an arbitrary expression. These stmts expand into debug
4209 insns, and are converted to debug notes by var-tracking.c. */
4210 return false;
4213 /* Verify a gimple label statement STMT.
4214 Returns true if anything is wrong. */
4216 static bool
4217 verify_gimple_label (gimple stmt)
4219 tree decl = gimple_label_label (stmt);
4220 int uid;
4221 bool err = false;
4223 if (TREE_CODE (decl) != LABEL_DECL)
4224 return true;
4226 uid = LABEL_DECL_UID (decl);
4227 if (cfun->cfg
4228 && (uid == -1
4229 || VEC_index (basic_block,
4230 label_to_block_map, uid) != gimple_bb (stmt)))
4232 error ("incorrect entry in label_to_block_map");
4233 err |= true;
4236 uid = EH_LANDING_PAD_NR (decl);
4237 if (uid)
4239 eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
4240 if (decl != lp->post_landing_pad)
4242 error ("incorrect setting of landing pad number");
4243 err |= true;
4247 return err;
4250 /* Verify the GIMPLE statement STMT. Returns true if there is an
4251 error, otherwise false. */
4253 static bool
4254 verify_gimple_stmt (gimple stmt)
4256 switch (gimple_code (stmt))
4258 case GIMPLE_ASSIGN:
4259 return verify_gimple_assign (stmt);
4261 case GIMPLE_LABEL:
4262 return verify_gimple_label (stmt);
4264 case GIMPLE_CALL:
4265 return verify_gimple_call (stmt);
4267 case GIMPLE_COND:
4268 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
4270 error ("invalid comparison code in gimple cond");
4271 return true;
4273 if (!(!gimple_cond_true_label (stmt)
4274 || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
4275 || !(!gimple_cond_false_label (stmt)
4276 || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
4278 error ("invalid labels in gimple cond");
4279 return true;
4282 return verify_gimple_comparison (boolean_type_node,
4283 gimple_cond_lhs (stmt),
4284 gimple_cond_rhs (stmt));
4286 case GIMPLE_GOTO:
4287 return verify_gimple_goto (stmt);
4289 case GIMPLE_SWITCH:
4290 return verify_gimple_switch (stmt);
4292 case GIMPLE_RETURN:
4293 return verify_gimple_return (stmt);
4295 case GIMPLE_ASM:
4296 return false;
4298 case GIMPLE_TRANSACTION:
4299 return verify_gimple_transaction (stmt);
4301 /* Tuples that do not have tree operands. */
4302 case GIMPLE_NOP:
4303 case GIMPLE_PREDICT:
4304 case GIMPLE_RESX:
4305 case GIMPLE_EH_DISPATCH:
4306 case GIMPLE_EH_MUST_NOT_THROW:
4307 return false;
4309 CASE_GIMPLE_OMP:
4310 /* OpenMP directives are validated by the FE and never operated
4311 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
4312 non-gimple expressions when the main index variable has had
4313 its address taken. This does not affect the loop itself
4314 because the header of an GIMPLE_OMP_FOR is merely used to determine
4315 how to setup the parallel iteration. */
4316 return false;
4318 case GIMPLE_DEBUG:
4319 return verify_gimple_debug (stmt);
4321 default:
4322 gcc_unreachable ();
4326 /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
4327 and false otherwise. */
4329 static bool
4330 verify_gimple_phi (gimple phi)
4332 bool err = false;
4333 unsigned i;
4334 tree phi_result = gimple_phi_result (phi);
4335 bool virtual_p;
4337 if (!phi_result)
4339 error ("invalid PHI result");
4340 return true;
4343 virtual_p = virtual_operand_p (phi_result);
4344 if (TREE_CODE (phi_result) != SSA_NAME
4345 || (virtual_p
4346 && SSA_NAME_VAR (phi_result) != gimple_vop (cfun)))
4348 error ("invalid PHI result");
4349 err = true;
4352 for (i = 0; i < gimple_phi_num_args (phi); i++)
4354 tree t = gimple_phi_arg_def (phi, i);
4356 if (!t)
4358 error ("missing PHI def");
4359 err |= true;
4360 continue;
4362 /* Addressable variables do have SSA_NAMEs but they
4363 are not considered gimple values. */
4364 else if ((TREE_CODE (t) == SSA_NAME
4365 && virtual_p != virtual_operand_p (t))
4366 || (virtual_p
4367 && (TREE_CODE (t) != SSA_NAME
4368 || SSA_NAME_VAR (t) != gimple_vop (cfun)))
4369 || (!virtual_p
4370 && !is_gimple_val (t)))
4372 error ("invalid PHI argument");
4373 debug_generic_expr (t);
4374 err |= true;
4376 #ifdef ENABLE_TYPES_CHECKING
4377 if (!useless_type_conversion_p (TREE_TYPE (phi_result), TREE_TYPE (t)))
4379 error ("incompatible types in PHI argument %u", i);
4380 debug_generic_stmt (TREE_TYPE (phi_result));
4381 debug_generic_stmt (TREE_TYPE (t));
4382 err |= true;
4384 #endif
4387 return err;
4390 /* Verify the GIMPLE statements inside the sequence STMTS. */
4392 static bool
4393 verify_gimple_in_seq_2 (gimple_seq stmts)
4395 gimple_stmt_iterator ittr;
4396 bool err = false;
4398 for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
4400 gimple stmt = gsi_stmt (ittr);
4402 switch (gimple_code (stmt))
4404 case GIMPLE_BIND:
4405 err |= verify_gimple_in_seq_2 (gimple_bind_body (stmt));
4406 break;
4408 case GIMPLE_TRY:
4409 err |= verify_gimple_in_seq_2 (gimple_try_eval (stmt));
4410 err |= verify_gimple_in_seq_2 (gimple_try_cleanup (stmt));
4411 break;
4413 case GIMPLE_EH_FILTER:
4414 err |= verify_gimple_in_seq_2 (gimple_eh_filter_failure (stmt));
4415 break;
4417 case GIMPLE_EH_ELSE:
4418 err |= verify_gimple_in_seq_2 (gimple_eh_else_n_body (stmt));
4419 err |= verify_gimple_in_seq_2 (gimple_eh_else_e_body (stmt));
4420 break;
4422 case GIMPLE_CATCH:
4423 err |= verify_gimple_in_seq_2 (gimple_catch_handler (stmt));
4424 break;
4426 case GIMPLE_TRANSACTION:
4427 err |= verify_gimple_transaction (stmt);
4428 break;
4430 default:
4432 bool err2 = verify_gimple_stmt (stmt);
4433 if (err2)
4434 debug_gimple_stmt (stmt);
4435 err |= err2;
4440 return err;
4443 /* Verify the contents of a GIMPLE_TRANSACTION. Returns true if there
4444 is a problem, otherwise false. */
4446 static bool
4447 verify_gimple_transaction (gimple stmt)
4449 tree lab = gimple_transaction_label (stmt);
4450 if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
4451 return true;
4452 return verify_gimple_in_seq_2 (gimple_transaction_body (stmt));
4456 /* Verify the GIMPLE statements inside the statement list STMTS. */
4458 DEBUG_FUNCTION void
4459 verify_gimple_in_seq (gimple_seq stmts)
4461 timevar_push (TV_TREE_STMT_VERIFY);
4462 if (verify_gimple_in_seq_2 (stmts))
4463 internal_error ("verify_gimple failed");
4464 timevar_pop (TV_TREE_STMT_VERIFY);
4467 /* Return true when the T can be shared. */
4469 bool
4470 tree_node_can_be_shared (tree t)
4472 if (IS_TYPE_OR_DECL_P (t)
4473 || is_gimple_min_invariant (t)
4474 || TREE_CODE (t) == SSA_NAME
4475 || t == error_mark_node
4476 || TREE_CODE (t) == IDENTIFIER_NODE)
4477 return true;
4479 if (TREE_CODE (t) == CASE_LABEL_EXPR)
4480 return true;
4482 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
4483 && is_gimple_min_invariant (TREE_OPERAND (t, 1)))
4484 || TREE_CODE (t) == COMPONENT_REF
4485 || TREE_CODE (t) == REALPART_EXPR
4486 || TREE_CODE (t) == IMAGPART_EXPR)
4487 t = TREE_OPERAND (t, 0);
4489 if (DECL_P (t))
4490 return true;
4492 return false;
4495 /* Called via walk_gimple_stmt. Verify tree sharing. */
4497 static tree
4498 verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
4500 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4501 struct pointer_set_t *visited = (struct pointer_set_t *) wi->info;
4503 if (tree_node_can_be_shared (*tp))
4505 *walk_subtrees = false;
4506 return NULL;
4509 if (pointer_set_insert (visited, *tp))
4510 return *tp;
4512 return NULL;
4515 static bool eh_error_found;
4516 static int
4517 verify_eh_throw_stmt_node (void **slot, void *data)
4519 struct throw_stmt_node *node = (struct throw_stmt_node *)*slot;
4520 struct pointer_set_t *visited = (struct pointer_set_t *) data;
4522 if (!pointer_set_contains (visited, node->stmt))
4524 error ("dead STMT in EH table");
4525 debug_gimple_stmt (node->stmt);
4526 eh_error_found = true;
4528 return 1;
4531 /* Verify the GIMPLE statements in the CFG of FN. */
4533 DEBUG_FUNCTION void
4534 verify_gimple_in_cfg (struct function *fn)
4536 basic_block bb;
4537 bool err = false;
4538 struct pointer_set_t *visited, *visited_stmts;
4540 timevar_push (TV_TREE_STMT_VERIFY);
4541 visited = pointer_set_create ();
4542 visited_stmts = pointer_set_create ();
4544 FOR_EACH_BB_FN (bb, fn)
4546 gimple_stmt_iterator gsi;
4548 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4550 gimple phi = gsi_stmt (gsi);
4551 bool err2 = false;
4552 unsigned i;
4554 pointer_set_insert (visited_stmts, phi);
4556 if (gimple_bb (phi) != bb)
4558 error ("gimple_bb (phi) is set to a wrong basic block");
4559 err2 = true;
4562 err2 |= verify_gimple_phi (phi);
4564 for (i = 0; i < gimple_phi_num_args (phi); i++)
4566 tree arg = gimple_phi_arg_def (phi, i);
4567 tree addr = walk_tree (&arg, verify_node_sharing, visited, NULL);
4568 if (addr)
4570 error ("incorrect sharing of tree nodes");
4571 debug_generic_expr (addr);
4572 err2 |= true;
4576 if (err2)
4577 debug_gimple_stmt (phi);
4578 err |= err2;
4581 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4583 gimple stmt = gsi_stmt (gsi);
4584 bool err2 = false;
4585 struct walk_stmt_info wi;
4586 tree addr;
4587 int lp_nr;
4589 pointer_set_insert (visited_stmts, stmt);
4591 if (gimple_bb (stmt) != bb)
4593 error ("gimple_bb (stmt) is set to a wrong basic block");
4594 err2 = true;
4597 err2 |= verify_gimple_stmt (stmt);
4599 memset (&wi, 0, sizeof (wi));
4600 wi.info = (void *) visited;
4601 addr = walk_gimple_op (stmt, verify_node_sharing, &wi);
4602 if (addr)
4604 error ("incorrect sharing of tree nodes");
4605 debug_generic_expr (addr);
4606 err2 |= true;
4609 /* ??? Instead of not checking these stmts at all the walker
4610 should know its context via wi. */
4611 if (!is_gimple_debug (stmt)
4612 && !is_gimple_omp (stmt))
4614 memset (&wi, 0, sizeof (wi));
4615 addr = walk_gimple_op (stmt, verify_expr, &wi);
4616 if (addr)
4618 debug_generic_expr (addr);
4619 inform (gimple_location (stmt), "in statement");
4620 err2 |= true;
4624 /* If the statement is marked as part of an EH region, then it is
4625 expected that the statement could throw. Verify that when we
4626 have optimizations that simplify statements such that we prove
4627 that they cannot throw, that we update other data structures
4628 to match. */
4629 lp_nr = lookup_stmt_eh_lp (stmt);
4630 if (lp_nr != 0)
4632 if (!stmt_could_throw_p (stmt))
4634 error ("statement marked for throw, but doesn%'t");
4635 err2 |= true;
4637 else if (lp_nr > 0
4638 && !gsi_one_before_end_p (gsi)
4639 && stmt_can_throw_internal (stmt))
4641 error ("statement marked for throw in middle of block");
4642 err2 |= true;
4646 if (err2)
4647 debug_gimple_stmt (stmt);
4648 err |= err2;
4652 eh_error_found = false;
4653 if (get_eh_throw_stmt_table (cfun))
4654 htab_traverse (get_eh_throw_stmt_table (cfun),
4655 verify_eh_throw_stmt_node,
4656 visited_stmts);
4658 if (err || eh_error_found)
4659 internal_error ("verify_gimple failed");
4661 pointer_set_destroy (visited);
4662 pointer_set_destroy (visited_stmts);
4663 verify_histograms ();
4664 timevar_pop (TV_TREE_STMT_VERIFY);
4668 /* Verifies that the flow information is OK. */
4670 static int
4671 gimple_verify_flow_info (void)
4673 int err = 0;
4674 basic_block bb;
4675 gimple_stmt_iterator gsi;
4676 gimple stmt;
4677 edge e;
4678 edge_iterator ei;
4680 if (ENTRY_BLOCK_PTR->il.gimple.seq || ENTRY_BLOCK_PTR->il.gimple.phi_nodes)
4682 error ("ENTRY_BLOCK has IL associated with it");
4683 err = 1;
4686 if (EXIT_BLOCK_PTR->il.gimple.seq || EXIT_BLOCK_PTR->il.gimple.phi_nodes)
4688 error ("EXIT_BLOCK has IL associated with it");
4689 err = 1;
4692 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
4693 if (e->flags & EDGE_FALLTHRU)
4695 error ("fallthru to exit from bb %d", e->src->index);
4696 err = 1;
4699 FOR_EACH_BB (bb)
4701 bool found_ctrl_stmt = false;
4703 stmt = NULL;
4705 /* Skip labels on the start of basic block. */
4706 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4708 tree label;
4709 gimple prev_stmt = stmt;
4711 stmt = gsi_stmt (gsi);
4713 if (gimple_code (stmt) != GIMPLE_LABEL)
4714 break;
4716 label = gimple_label_label (stmt);
4717 if (prev_stmt && DECL_NONLOCAL (label))
4719 error ("nonlocal label ");
4720 print_generic_expr (stderr, label, 0);
4721 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4722 bb->index);
4723 err = 1;
4726 if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
4728 error ("EH landing pad label ");
4729 print_generic_expr (stderr, label, 0);
4730 fprintf (stderr, " is not first in a sequence of labels in bb %d",
4731 bb->index);
4732 err = 1;
4735 if (label_to_block (label) != bb)
4737 error ("label ");
4738 print_generic_expr (stderr, label, 0);
4739 fprintf (stderr, " to block does not match in bb %d",
4740 bb->index);
4741 err = 1;
4744 if (decl_function_context (label) != current_function_decl)
4746 error ("label ");
4747 print_generic_expr (stderr, label, 0);
4748 fprintf (stderr, " has incorrect context in bb %d",
4749 bb->index);
4750 err = 1;
4754 /* Verify that body of basic block BB is free of control flow. */
4755 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4757 gimple stmt = gsi_stmt (gsi);
4759 if (found_ctrl_stmt)
4761 error ("control flow in the middle of basic block %d",
4762 bb->index);
4763 err = 1;
4766 if (stmt_ends_bb_p (stmt))
4767 found_ctrl_stmt = true;
4769 if (gimple_code (stmt) == GIMPLE_LABEL)
4771 error ("label ");
4772 print_generic_expr (stderr, gimple_label_label (stmt), 0);
4773 fprintf (stderr, " in the middle of basic block %d", bb->index);
4774 err = 1;
4778 gsi = gsi_last_bb (bb);
4779 if (gsi_end_p (gsi))
4780 continue;
4782 stmt = gsi_stmt (gsi);
4784 if (gimple_code (stmt) == GIMPLE_LABEL)
4785 continue;
4787 err |= verify_eh_edges (stmt);
4789 if (is_ctrl_stmt (stmt))
4791 FOR_EACH_EDGE (e, ei, bb->succs)
4792 if (e->flags & EDGE_FALLTHRU)
4794 error ("fallthru edge after a control statement in bb %d",
4795 bb->index);
4796 err = 1;
4800 if (gimple_code (stmt) != GIMPLE_COND)
4802 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
4803 after anything else but if statement. */
4804 FOR_EACH_EDGE (e, ei, bb->succs)
4805 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
4807 error ("true/false edge after a non-GIMPLE_COND in bb %d",
4808 bb->index);
4809 err = 1;
4813 switch (gimple_code (stmt))
4815 case GIMPLE_COND:
4817 edge true_edge;
4818 edge false_edge;
4820 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
4822 if (!true_edge
4823 || !false_edge
4824 || !(true_edge->flags & EDGE_TRUE_VALUE)
4825 || !(false_edge->flags & EDGE_FALSE_VALUE)
4826 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4827 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
4828 || EDGE_COUNT (bb->succs) >= 3)
4830 error ("wrong outgoing edge flags at end of bb %d",
4831 bb->index);
4832 err = 1;
4835 break;
4837 case GIMPLE_GOTO:
4838 if (simple_goto_p (stmt))
4840 error ("explicit goto at end of bb %d", bb->index);
4841 err = 1;
4843 else
4845 /* FIXME. We should double check that the labels in the
4846 destination blocks have their address taken. */
4847 FOR_EACH_EDGE (e, ei, bb->succs)
4848 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
4849 | EDGE_FALSE_VALUE))
4850 || !(e->flags & EDGE_ABNORMAL))
4852 error ("wrong outgoing edge flags at end of bb %d",
4853 bb->index);
4854 err = 1;
4857 break;
4859 case GIMPLE_CALL:
4860 if (!gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
4861 break;
4862 /* ... fallthru ... */
4863 case GIMPLE_RETURN:
4864 if (!single_succ_p (bb)
4865 || (single_succ_edge (bb)->flags
4866 & (EDGE_FALLTHRU | EDGE_ABNORMAL
4867 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4869 error ("wrong outgoing edge flags at end of bb %d", bb->index);
4870 err = 1;
4872 if (single_succ (bb) != EXIT_BLOCK_PTR)
4874 error ("return edge does not point to exit in bb %d",
4875 bb->index);
4876 err = 1;
4878 break;
4880 case GIMPLE_SWITCH:
4882 tree prev;
4883 edge e;
4884 size_t i, n;
4886 n = gimple_switch_num_labels (stmt);
4888 /* Mark all the destination basic blocks. */
4889 for (i = 0; i < n; ++i)
4891 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4892 basic_block label_bb = label_to_block (lab);
4893 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
4894 label_bb->aux = (void *)1;
4897 /* Verify that the case labels are sorted. */
4898 prev = gimple_switch_label (stmt, 0);
4899 for (i = 1; i < n; ++i)
4901 tree c = gimple_switch_label (stmt, i);
4902 if (!CASE_LOW (c))
4904 error ("found default case not at the start of "
4905 "case vector");
4906 err = 1;
4907 continue;
4909 if (CASE_LOW (prev)
4910 && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
4912 error ("case labels not sorted: ");
4913 print_generic_expr (stderr, prev, 0);
4914 fprintf (stderr," is greater than ");
4915 print_generic_expr (stderr, c, 0);
4916 fprintf (stderr," but comes before it.\n");
4917 err = 1;
4919 prev = c;
4921 /* VRP will remove the default case if it can prove it will
4922 never be executed. So do not verify there always exists
4923 a default case here. */
4925 FOR_EACH_EDGE (e, ei, bb->succs)
4927 if (!e->dest->aux)
4929 error ("extra outgoing edge %d->%d",
4930 bb->index, e->dest->index);
4931 err = 1;
4934 e->dest->aux = (void *)2;
4935 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
4936 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4938 error ("wrong outgoing edge flags at end of bb %d",
4939 bb->index);
4940 err = 1;
4944 /* Check that we have all of them. */
4945 for (i = 0; i < n; ++i)
4947 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
4948 basic_block label_bb = label_to_block (lab);
4950 if (label_bb->aux != (void *)2)
4952 error ("missing edge %i->%i", bb->index, label_bb->index);
4953 err = 1;
4957 FOR_EACH_EDGE (e, ei, bb->succs)
4958 e->dest->aux = (void *)0;
4960 break;
4962 case GIMPLE_EH_DISPATCH:
4963 err |= verify_eh_dispatch_edge (stmt);
4964 break;
4966 default:
4967 break;
4971 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
4972 verify_dominators (CDI_DOMINATORS);
4974 return err;
4978 /* Updates phi nodes after creating a forwarder block joined
4979 by edge FALLTHRU. */
4981 static void
4982 gimple_make_forwarder_block (edge fallthru)
4984 edge e;
4985 edge_iterator ei;
4986 basic_block dummy, bb;
4987 tree var;
4988 gimple_stmt_iterator gsi;
4990 dummy = fallthru->src;
4991 bb = fallthru->dest;
4993 if (single_pred_p (bb))
4994 return;
4996 /* If we redirected a branch we must create new PHI nodes at the
4997 start of BB. */
4998 for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
5000 gimple phi, new_phi;
5002 phi = gsi_stmt (gsi);
5003 var = gimple_phi_result (phi);
5004 new_phi = create_phi_node (var, bb);
5005 gimple_phi_set_result (phi, copy_ssa_name (var, phi));
5006 add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
5007 UNKNOWN_LOCATION);
5010 /* Add the arguments we have stored on edges. */
5011 FOR_EACH_EDGE (e, ei, bb->preds)
5013 if (e == fallthru)
5014 continue;
5016 flush_pending_stmts (e);
5021 /* Return a non-special label in the head of basic block BLOCK.
5022 Create one if it doesn't exist. */
5024 tree
5025 gimple_block_label (basic_block bb)
5027 gimple_stmt_iterator i, s = gsi_start_bb (bb);
5028 bool first = true;
5029 tree label;
5030 gimple stmt;
5032 for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
5034 stmt = gsi_stmt (i);
5035 if (gimple_code (stmt) != GIMPLE_LABEL)
5036 break;
5037 label = gimple_label_label (stmt);
5038 if (!DECL_NONLOCAL (label))
5040 if (!first)
5041 gsi_move_before (&i, &s);
5042 return label;
5046 label = create_artificial_label (UNKNOWN_LOCATION);
5047 stmt = gimple_build_label (label);
5048 gsi_insert_before (&s, stmt, GSI_NEW_STMT);
5049 return label;
5053 /* Attempt to perform edge redirection by replacing a possibly complex
5054 jump instruction by a goto or by removing the jump completely.
5055 This can apply only if all edges now point to the same block. The
5056 parameters and return values are equivalent to
5057 redirect_edge_and_branch. */
5059 static edge
5060 gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
5062 basic_block src = e->src;
5063 gimple_stmt_iterator i;
5064 gimple stmt;
5066 /* We can replace or remove a complex jump only when we have exactly
5067 two edges. */
5068 if (EDGE_COUNT (src->succs) != 2
5069 /* Verify that all targets will be TARGET. Specifically, the
5070 edge that is not E must also go to TARGET. */
5071 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
5072 return NULL;
5074 i = gsi_last_bb (src);
5075 if (gsi_end_p (i))
5076 return NULL;
5078 stmt = gsi_stmt (i);
5080 if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
5082 gsi_remove (&i, true);
5083 e = ssa_redirect_edge (e, target);
5084 e->flags = EDGE_FALLTHRU;
5085 return e;
5088 return NULL;
5092 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
5093 edge representing the redirected branch. */
5095 static edge
5096 gimple_redirect_edge_and_branch (edge e, basic_block dest)
5098 basic_block bb = e->src;
5099 gimple_stmt_iterator gsi;
5100 edge ret;
5101 gimple stmt;
5103 if (e->flags & EDGE_ABNORMAL)
5104 return NULL;
5106 if (e->dest == dest)
5107 return NULL;
5109 if (e->flags & EDGE_EH)
5110 return redirect_eh_edge (e, dest);
5112 if (e->src != ENTRY_BLOCK_PTR)
5114 ret = gimple_try_redirect_by_replacing_jump (e, dest);
5115 if (ret)
5116 return ret;
5119 gsi = gsi_last_bb (bb);
5120 stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
5122 switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
5124 case GIMPLE_COND:
5125 /* For COND_EXPR, we only need to redirect the edge. */
5126 break;
5128 case GIMPLE_GOTO:
5129 /* No non-abnormal edges should lead from a non-simple goto, and
5130 simple ones should be represented implicitly. */
5131 gcc_unreachable ();
5133 case GIMPLE_SWITCH:
5135 tree label = gimple_block_label (dest);
5136 tree cases = get_cases_for_edge (e, stmt);
5138 /* If we have a list of cases associated with E, then use it
5139 as it's a lot faster than walking the entire case vector. */
5140 if (cases)
5142 edge e2 = find_edge (e->src, dest);
5143 tree last, first;
5145 first = cases;
5146 while (cases)
5148 last = cases;
5149 CASE_LABEL (cases) = label;
5150 cases = CASE_CHAIN (cases);
5153 /* If there was already an edge in the CFG, then we need
5154 to move all the cases associated with E to E2. */
5155 if (e2)
5157 tree cases2 = get_cases_for_edge (e2, stmt);
5159 CASE_CHAIN (last) = CASE_CHAIN (cases2);
5160 CASE_CHAIN (cases2) = first;
5162 bitmap_set_bit (touched_switch_bbs, gimple_bb (stmt)->index);
5164 else
5166 size_t i, n = gimple_switch_num_labels (stmt);
5168 for (i = 0; i < n; i++)
5170 tree elt = gimple_switch_label (stmt, i);
5171 if (label_to_block (CASE_LABEL (elt)) == e->dest)
5172 CASE_LABEL (elt) = label;
5176 break;
5178 case GIMPLE_ASM:
5180 int i, n = gimple_asm_nlabels (stmt);
5181 tree label = NULL;
5183 for (i = 0; i < n; ++i)
5185 tree cons = gimple_asm_label_op (stmt, i);
5186 if (label_to_block (TREE_VALUE (cons)) == e->dest)
5188 if (!label)
5189 label = gimple_block_label (dest);
5190 TREE_VALUE (cons) = label;
5194 /* If we didn't find any label matching the former edge in the
5195 asm labels, we must be redirecting the fallthrough
5196 edge. */
5197 gcc_assert (label || (e->flags & EDGE_FALLTHRU));
5199 break;
5201 case GIMPLE_RETURN:
5202 gsi_remove (&gsi, true);
5203 e->flags |= EDGE_FALLTHRU;
5204 break;
5206 case GIMPLE_OMP_RETURN:
5207 case GIMPLE_OMP_CONTINUE:
5208 case GIMPLE_OMP_SECTIONS_SWITCH:
5209 case GIMPLE_OMP_FOR:
5210 /* The edges from OMP constructs can be simply redirected. */
5211 break;
5213 case GIMPLE_EH_DISPATCH:
5214 if (!(e->flags & EDGE_FALLTHRU))
5215 redirect_eh_dispatch_edge (stmt, e, dest);
5216 break;
5218 case GIMPLE_TRANSACTION:
5219 /* The ABORT edge has a stored label associated with it, otherwise
5220 the edges are simply redirectable. */
5221 if (e->flags == 0)
5222 gimple_transaction_set_label (stmt, gimple_block_label (dest));
5223 break;
5225 default:
5226 /* Otherwise it must be a fallthru edge, and we don't need to
5227 do anything besides redirecting it. */
5228 gcc_assert (e->flags & EDGE_FALLTHRU);
5229 break;
5232 /* Update/insert PHI nodes as necessary. */
5234 /* Now update the edges in the CFG. */
5235 e = ssa_redirect_edge (e, dest);
5237 return e;
5240 /* Returns true if it is possible to remove edge E by redirecting
5241 it to the destination of the other edge from E->src. */
5243 static bool
5244 gimple_can_remove_branch_p (const_edge e)
5246 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
5247 return false;
5249 return true;
5252 /* Simple wrapper, as we can always redirect fallthru edges. */
5254 static basic_block
5255 gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
5257 e = gimple_redirect_edge_and_branch (e, dest);
5258 gcc_assert (e);
5260 return NULL;
5264 /* Splits basic block BB after statement STMT (but at least after the
5265 labels). If STMT is NULL, BB is split just after the labels. */
5267 static basic_block
5268 gimple_split_block (basic_block bb, void *stmt)
5270 gimple_stmt_iterator gsi;
5271 gimple_stmt_iterator gsi_tgt;
5272 gimple act;
5273 gimple_seq list;
5274 basic_block new_bb;
5275 edge e;
5276 edge_iterator ei;
5278 new_bb = create_empty_bb (bb);
5280 /* Redirect the outgoing edges. */
5281 new_bb->succs = bb->succs;
5282 bb->succs = NULL;
5283 FOR_EACH_EDGE (e, ei, new_bb->succs)
5284 e->src = new_bb;
5286 if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
5287 stmt = NULL;
5289 /* Move everything from GSI to the new basic block. */
5290 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5292 act = gsi_stmt (gsi);
5293 if (gimple_code (act) == GIMPLE_LABEL)
5294 continue;
5296 if (!stmt)
5297 break;
5299 if (stmt == act)
5301 gsi_next (&gsi);
5302 break;
5306 if (gsi_end_p (gsi))
5307 return new_bb;
5309 /* Split the statement list - avoid re-creating new containers as this
5310 brings ugly quadratic memory consumption in the inliner.
5311 (We are still quadratic since we need to update stmt BB pointers,
5312 sadly.) */
5313 gsi_split_seq_before (&gsi, &list);
5314 set_bb_seq (new_bb, list);
5315 for (gsi_tgt = gsi_start (list);
5316 !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
5317 gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
5319 return new_bb;
5323 /* Moves basic block BB after block AFTER. */
5325 static bool
5326 gimple_move_block_after (basic_block bb, basic_block after)
5328 if (bb->prev_bb == after)
5329 return true;
5331 unlink_block (bb);
5332 link_block (bb, after);
5334 return true;
5338 /* Return TRUE if block BB has no executable statements, otherwise return
5339 FALSE. */
5341 bool
5342 gimple_empty_block_p (basic_block bb)
5344 /* BB must have no executable statements. */
5345 gimple_stmt_iterator gsi = gsi_after_labels (bb);
5346 if (phi_nodes (bb))
5347 return false;
5348 if (gsi_end_p (gsi))
5349 return true;
5350 if (is_gimple_debug (gsi_stmt (gsi)))
5351 gsi_next_nondebug (&gsi);
5352 return gsi_end_p (gsi);
5356 /* Split a basic block if it ends with a conditional branch and if the
5357 other part of the block is not empty. */
5359 static basic_block
5360 gimple_split_block_before_cond_jump (basic_block bb)
5362 gimple last, split_point;
5363 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
5364 if (gsi_end_p (gsi))
5365 return NULL;
5366 last = gsi_stmt (gsi);
5367 if (gimple_code (last) != GIMPLE_COND
5368 && gimple_code (last) != GIMPLE_SWITCH)
5369 return NULL;
5370 gsi_prev_nondebug (&gsi);
5371 split_point = gsi_stmt (gsi);
5372 return split_block (bb, split_point)->dest;
5376 /* Return true if basic_block can be duplicated. */
5378 static bool
5379 gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
5381 return true;
5384 /* Create a duplicate of the basic block BB. NOTE: This does not
5385 preserve SSA form. */
5387 static basic_block
5388 gimple_duplicate_bb (basic_block bb)
5390 basic_block new_bb;
5391 gimple_stmt_iterator gsi, gsi_tgt;
5392 gimple_seq phis = phi_nodes (bb);
5393 gimple phi, stmt, copy;
5395 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
5397 /* Copy the PHI nodes. We ignore PHI node arguments here because
5398 the incoming edges have not been setup yet. */
5399 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
5401 phi = gsi_stmt (gsi);
5402 copy = create_phi_node (NULL_TREE, new_bb);
5403 create_new_def_for (gimple_phi_result (phi), copy,
5404 gimple_phi_result_ptr (copy));
5407 gsi_tgt = gsi_start_bb (new_bb);
5408 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5410 def_operand_p def_p;
5411 ssa_op_iter op_iter;
5412 tree lhs;
5414 stmt = gsi_stmt (gsi);
5415 if (gimple_code (stmt) == GIMPLE_LABEL)
5416 continue;
5418 /* Don't duplicate label debug stmts. */
5419 if (gimple_debug_bind_p (stmt)
5420 && TREE_CODE (gimple_debug_bind_get_var (stmt))
5421 == LABEL_DECL)
5422 continue;
5424 /* Create a new copy of STMT and duplicate STMT's virtual
5425 operands. */
5426 copy = gimple_copy (stmt);
5427 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
5429 maybe_duplicate_eh_stmt (copy, stmt);
5430 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
5432 /* When copying around a stmt writing into a local non-user
5433 aggregate, make sure it won't share stack slot with other
5434 vars. */
5435 lhs = gimple_get_lhs (stmt);
5436 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5438 tree base = get_base_address (lhs);
5439 if (base
5440 && (TREE_CODE (base) == VAR_DECL
5441 || TREE_CODE (base) == RESULT_DECL)
5442 && DECL_IGNORED_P (base)
5443 && !TREE_STATIC (base)
5444 && !DECL_EXTERNAL (base)
5445 && (TREE_CODE (base) != VAR_DECL
5446 || !DECL_HAS_VALUE_EXPR_P (base)))
5447 DECL_NONSHAREABLE (base) = 1;
5450 /* Create new names for all the definitions created by COPY and
5451 add replacement mappings for each new name. */
5452 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
5453 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
5456 return new_bb;
5459 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
5461 static void
5462 add_phi_args_after_copy_edge (edge e_copy)
5464 basic_block bb, bb_copy = e_copy->src, dest;
5465 edge e;
5466 edge_iterator ei;
5467 gimple phi, phi_copy;
5468 tree def;
5469 gimple_stmt_iterator psi, psi_copy;
5471 if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
5472 return;
5474 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
5476 if (e_copy->dest->flags & BB_DUPLICATED)
5477 dest = get_bb_original (e_copy->dest);
5478 else
5479 dest = e_copy->dest;
5481 e = find_edge (bb, dest);
5482 if (!e)
5484 /* During loop unrolling the target of the latch edge is copied.
5485 In this case we are not looking for edge to dest, but to
5486 duplicated block whose original was dest. */
5487 FOR_EACH_EDGE (e, ei, bb->succs)
5489 if ((e->dest->flags & BB_DUPLICATED)
5490 && get_bb_original (e->dest) == dest)
5491 break;
5494 gcc_assert (e != NULL);
5497 for (psi = gsi_start_phis (e->dest),
5498 psi_copy = gsi_start_phis (e_copy->dest);
5499 !gsi_end_p (psi);
5500 gsi_next (&psi), gsi_next (&psi_copy))
5502 phi = gsi_stmt (psi);
5503 phi_copy = gsi_stmt (psi_copy);
5504 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5505 add_phi_arg (phi_copy, def, e_copy,
5506 gimple_phi_arg_location_from_edge (phi, e));
5511 /* Basic block BB_COPY was created by code duplication. Add phi node
5512 arguments for edges going out of BB_COPY. The blocks that were
5513 duplicated have BB_DUPLICATED set. */
5515 void
5516 add_phi_args_after_copy_bb (basic_block bb_copy)
5518 edge e_copy;
5519 edge_iterator ei;
5521 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
5523 add_phi_args_after_copy_edge (e_copy);
5527 /* Blocks in REGION_COPY array of length N_REGION were created by
5528 duplication of basic blocks. Add phi node arguments for edges
5529 going from these blocks. If E_COPY is not NULL, also add
5530 phi node arguments for its destination.*/
5532 void
5533 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
5534 edge e_copy)
5536 unsigned i;
5538 for (i = 0; i < n_region; i++)
5539 region_copy[i]->flags |= BB_DUPLICATED;
5541 for (i = 0; i < n_region; i++)
5542 add_phi_args_after_copy_bb (region_copy[i]);
5543 if (e_copy)
5544 add_phi_args_after_copy_edge (e_copy);
5546 for (i = 0; i < n_region; i++)
5547 region_copy[i]->flags &= ~BB_DUPLICATED;
5550 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5551 important exit edge EXIT. By important we mean that no SSA name defined
5552 inside region is live over the other exit edges of the region. All entry
5553 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5554 to the duplicate of the region. Dominance and loop information is
5555 updated, but not the SSA web. The new basic blocks are stored to
5556 REGION_COPY in the same order as they had in REGION, provided that
5557 REGION_COPY is not NULL.
5558 The function returns false if it is unable to copy the region,
5559 true otherwise. */
5561 bool
5562 gimple_duplicate_sese_region (edge entry, edge exit,
5563 basic_block *region, unsigned n_region,
5564 basic_block *region_copy)
5566 unsigned i;
5567 bool free_region_copy = false, copying_header = false;
5568 struct loop *loop = entry->dest->loop_father;
5569 edge exit_copy;
5570 VEC (basic_block, heap) *doms;
5571 edge redirected;
5572 int total_freq = 0, entry_freq = 0;
5573 gcov_type total_count = 0, entry_count = 0;
5575 if (!can_copy_bbs_p (region, n_region))
5576 return false;
5578 /* Some sanity checking. Note that we do not check for all possible
5579 missuses of the functions. I.e. if you ask to copy something weird,
5580 it will work, but the state of structures probably will not be
5581 correct. */
5582 for (i = 0; i < n_region; i++)
5584 /* We do not handle subloops, i.e. all the blocks must belong to the
5585 same loop. */
5586 if (region[i]->loop_father != loop)
5587 return false;
5589 if (region[i] != entry->dest
5590 && region[i] == loop->header)
5591 return false;
5594 set_loop_copy (loop, loop);
5596 /* In case the function is used for loop header copying (which is the primary
5597 use), ensure that EXIT and its copy will be new latch and entry edges. */
5598 if (loop->header == entry->dest)
5600 copying_header = true;
5601 set_loop_copy (loop, loop_outer (loop));
5603 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5604 return false;
5606 for (i = 0; i < n_region; i++)
5607 if (region[i] != exit->src
5608 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5609 return false;
5612 if (!region_copy)
5614 region_copy = XNEWVEC (basic_block, n_region);
5615 free_region_copy = true;
5618 /* Record blocks outside the region that are dominated by something
5619 inside. */
5620 doms = NULL;
5621 initialize_original_copy_tables ();
5623 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5625 if (entry->dest->count)
5627 total_count = entry->dest->count;
5628 entry_count = entry->count;
5629 /* Fix up corner cases, to avoid division by zero or creation of negative
5630 frequencies. */
5631 if (entry_count > total_count)
5632 entry_count = total_count;
5634 else
5636 total_freq = entry->dest->frequency;
5637 entry_freq = EDGE_FREQUENCY (entry);
5638 /* Fix up corner cases, to avoid division by zero or creation of negative
5639 frequencies. */
5640 if (total_freq == 0)
5641 total_freq = 1;
5642 else if (entry_freq > total_freq)
5643 entry_freq = total_freq;
5646 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
5647 split_edge_bb_loc (entry));
5648 if (total_count)
5650 scale_bbs_frequencies_gcov_type (region, n_region,
5651 total_count - entry_count,
5652 total_count);
5653 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
5654 total_count);
5656 else
5658 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
5659 total_freq);
5660 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
5663 if (copying_header)
5665 loop->header = exit->dest;
5666 loop->latch = exit->src;
5669 /* Redirect the entry and add the phi node arguments. */
5670 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
5671 gcc_assert (redirected != NULL);
5672 flush_pending_stmts (entry);
5674 /* Concerning updating of dominators: We must recount dominators
5675 for entry block and its copy. Anything that is outside of the
5676 region, but was dominated by something inside needs recounting as
5677 well. */
5678 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5679 VEC_safe_push (basic_block, heap, doms, get_bb_original (entry->dest));
5680 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5681 VEC_free (basic_block, heap, doms);
5683 /* Add the other PHI node arguments. */
5684 add_phi_args_after_copy (region_copy, n_region, NULL);
5686 if (free_region_copy)
5687 free (region_copy);
5689 free_original_copy_tables ();
5690 return true;
5693 /* Checks if BB is part of the region defined by N_REGION BBS. */
5694 static bool
5695 bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
5697 unsigned int n;
5699 for (n = 0; n < n_region; n++)
5701 if (bb == bbs[n])
5702 return true;
5704 return false;
5707 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
5708 are stored to REGION_COPY in the same order in that they appear
5709 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
5710 the region, EXIT an exit from it. The condition guarding EXIT
5711 is moved to ENTRY. Returns true if duplication succeeds, false
5712 otherwise.
5714 For example,
5716 some_code;
5717 if (cond)
5719 else
5722 is transformed to
5724 if (cond)
5726 some_code;
5729 else
5731 some_code;
5736 bool
5737 gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
5738 basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
5739 basic_block *region_copy ATTRIBUTE_UNUSED)
5741 unsigned i;
5742 bool free_region_copy = false;
5743 struct loop *loop = exit->dest->loop_father;
5744 struct loop *orig_loop = entry->dest->loop_father;
5745 basic_block switch_bb, entry_bb, nentry_bb;
5746 VEC (basic_block, heap) *doms;
5747 int total_freq = 0, exit_freq = 0;
5748 gcov_type total_count = 0, exit_count = 0;
5749 edge exits[2], nexits[2], e;
5750 gimple_stmt_iterator gsi;
5751 gimple cond_stmt;
5752 edge sorig, snew;
5753 basic_block exit_bb;
5754 gimple_stmt_iterator psi;
5755 gimple phi;
5756 tree def;
5757 struct loop *target, *aloop, *cloop;
5759 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
5760 exits[0] = exit;
5761 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
5763 if (!can_copy_bbs_p (region, n_region))
5764 return false;
5766 initialize_original_copy_tables ();
5767 set_loop_copy (orig_loop, loop);
5769 target= loop;
5770 for (aloop = orig_loop->inner; aloop; aloop = aloop->next)
5772 if (bb_part_of_region_p (aloop->header, region, n_region))
5774 cloop = duplicate_loop (aloop, target);
5775 duplicate_subloops (aloop, cloop);
5779 if (!region_copy)
5781 region_copy = XNEWVEC (basic_block, n_region);
5782 free_region_copy = true;
5785 gcc_assert (!need_ssa_update_p (cfun));
5787 /* Record blocks outside the region that are dominated by something
5788 inside. */
5789 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5791 if (exit->src->count)
5793 total_count = exit->src->count;
5794 exit_count = exit->count;
5795 /* Fix up corner cases, to avoid division by zero or creation of negative
5796 frequencies. */
5797 if (exit_count > total_count)
5798 exit_count = total_count;
5800 else
5802 total_freq = exit->src->frequency;
5803 exit_freq = EDGE_FREQUENCY (exit);
5804 /* Fix up corner cases, to avoid division by zero or creation of negative
5805 frequencies. */
5806 if (total_freq == 0)
5807 total_freq = 1;
5808 if (exit_freq > total_freq)
5809 exit_freq = total_freq;
5812 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
5813 split_edge_bb_loc (exit));
5814 if (total_count)
5816 scale_bbs_frequencies_gcov_type (region, n_region,
5817 total_count - exit_count,
5818 total_count);
5819 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
5820 total_count);
5822 else
5824 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
5825 total_freq);
5826 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
5829 /* Create the switch block, and put the exit condition to it. */
5830 entry_bb = entry->dest;
5831 nentry_bb = get_bb_copy (entry_bb);
5832 if (!last_stmt (entry->src)
5833 || !stmt_ends_bb_p (last_stmt (entry->src)))
5834 switch_bb = entry->src;
5835 else
5836 switch_bb = split_edge (entry);
5837 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
5839 gsi = gsi_last_bb (switch_bb);
5840 cond_stmt = last_stmt (exit->src);
5841 gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
5842 cond_stmt = gimple_copy (cond_stmt);
5844 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
5846 sorig = single_succ_edge (switch_bb);
5847 sorig->flags = exits[1]->flags;
5848 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
5850 /* Register the new edge from SWITCH_BB in loop exit lists. */
5851 rescan_loop_exit (snew, true, false);
5853 /* Add the PHI node arguments. */
5854 add_phi_args_after_copy (region_copy, n_region, snew);
5856 /* Get rid of now superfluous conditions and associated edges (and phi node
5857 arguments). */
5858 exit_bb = exit->dest;
5860 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
5861 PENDING_STMT (e) = NULL;
5863 /* The latch of ORIG_LOOP was copied, and so was the backedge
5864 to the original header. We redirect this backedge to EXIT_BB. */
5865 for (i = 0; i < n_region; i++)
5866 if (get_bb_original (region_copy[i]) == orig_loop->latch)
5868 gcc_assert (single_succ_edge (region_copy[i]));
5869 e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
5870 PENDING_STMT (e) = NULL;
5871 for (psi = gsi_start_phis (exit_bb);
5872 !gsi_end_p (psi);
5873 gsi_next (&psi))
5875 phi = gsi_stmt (psi);
5876 def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
5877 add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
5880 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
5881 PENDING_STMT (e) = NULL;
5883 /* Anything that is outside of the region, but was dominated by something
5884 inside needs to update dominance info. */
5885 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
5886 VEC_free (basic_block, heap, doms);
5887 /* Update the SSA web. */
5888 update_ssa (TODO_update_ssa);
5890 if (free_region_copy)
5891 free (region_copy);
5893 free_original_copy_tables ();
5894 return true;
5897 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
5898 adding blocks when the dominator traversal reaches EXIT. This
5899 function silently assumes that ENTRY strictly dominates EXIT. */
5901 void
5902 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
5903 VEC(basic_block,heap) **bbs_p)
5905 basic_block son;
5907 for (son = first_dom_son (CDI_DOMINATORS, entry);
5908 son;
5909 son = next_dom_son (CDI_DOMINATORS, son))
5911 VEC_safe_push (basic_block, heap, *bbs_p, son);
5912 if (son != exit)
5913 gather_blocks_in_sese_region (son, exit, bbs_p);
5917 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
5918 The duplicates are recorded in VARS_MAP. */
5920 static void
5921 replace_by_duplicate_decl (tree *tp, struct pointer_map_t *vars_map,
5922 tree to_context)
5924 tree t = *tp, new_t;
5925 struct function *f = DECL_STRUCT_FUNCTION (to_context);
5926 void **loc;
5928 if (DECL_CONTEXT (t) == to_context)
5929 return;
5931 loc = pointer_map_contains (vars_map, t);
5933 if (!loc)
5935 loc = pointer_map_insert (vars_map, t);
5937 if (SSA_VAR_P (t))
5939 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
5940 add_local_decl (f, new_t);
5942 else
5944 gcc_assert (TREE_CODE (t) == CONST_DECL);
5945 new_t = copy_node (t);
5947 DECL_CONTEXT (new_t) = to_context;
5949 *loc = new_t;
5951 else
5952 new_t = (tree) *loc;
5954 *tp = new_t;
5958 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
5959 VARS_MAP maps old ssa names and var_decls to the new ones. */
5961 static tree
5962 replace_ssa_name (tree name, struct pointer_map_t *vars_map,
5963 tree to_context)
5965 void **loc;
5966 tree new_name;
5968 gcc_assert (!virtual_operand_p (name));
5970 loc = pointer_map_contains (vars_map, name);
5972 if (!loc)
5974 tree decl = SSA_NAME_VAR (name);
5975 if (decl)
5977 replace_by_duplicate_decl (&decl, vars_map, to_context);
5978 new_name = make_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
5979 decl, SSA_NAME_DEF_STMT (name));
5980 if (SSA_NAME_IS_DEFAULT_DEF (name))
5981 set_ssa_default_def (DECL_STRUCT_FUNCTION (to_context),
5982 decl, new_name);
5984 else
5985 new_name = copy_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
5986 name, SSA_NAME_DEF_STMT (name));
5988 loc = pointer_map_insert (vars_map, name);
5989 *loc = new_name;
5991 else
5992 new_name = (tree) *loc;
5994 return new_name;
5997 struct move_stmt_d
5999 tree orig_block;
6000 tree new_block;
6001 tree from_context;
6002 tree to_context;
6003 struct pointer_map_t *vars_map;
6004 htab_t new_label_map;
6005 struct pointer_map_t *eh_map;
6006 bool remap_decls_p;
6009 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
6010 contained in *TP if it has been ORIG_BLOCK previously and change the
6011 DECL_CONTEXT of every local variable referenced in *TP. */
6013 static tree
6014 move_stmt_op (tree *tp, int *walk_subtrees, void *data)
6016 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6017 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6018 tree t = *tp;
6020 if (EXPR_P (t))
6021 /* We should never have TREE_BLOCK set on non-statements. */
6022 gcc_assert (!TREE_BLOCK (t));
6024 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
6026 if (TREE_CODE (t) == SSA_NAME)
6027 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
6028 else if (TREE_CODE (t) == LABEL_DECL)
6030 if (p->new_label_map)
6032 struct tree_map in, *out;
6033 in.base.from = t;
6034 out = (struct tree_map *)
6035 htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
6036 if (out)
6037 *tp = t = out->to;
6040 DECL_CONTEXT (t) = p->to_context;
6042 else if (p->remap_decls_p)
6044 /* Replace T with its duplicate. T should no longer appear in the
6045 parent function, so this looks wasteful; however, it may appear
6046 in referenced_vars, and more importantly, as virtual operands of
6047 statements, and in alias lists of other variables. It would be
6048 quite difficult to expunge it from all those places. ??? It might
6049 suffice to do this for addressable variables. */
6050 if ((TREE_CODE (t) == VAR_DECL
6051 && !is_global_var (t))
6052 || TREE_CODE (t) == CONST_DECL)
6053 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
6055 *walk_subtrees = 0;
6057 else if (TYPE_P (t))
6058 *walk_subtrees = 0;
6060 return NULL_TREE;
6063 /* Helper for move_stmt_r. Given an EH region number for the source
6064 function, map that to the duplicate EH regio number in the dest. */
6066 static int
6067 move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
6069 eh_region old_r, new_r;
6070 void **slot;
6072 old_r = get_eh_region_from_number (old_nr);
6073 slot = pointer_map_contains (p->eh_map, old_r);
6074 new_r = (eh_region) *slot;
6076 return new_r->index;
6079 /* Similar, but operate on INTEGER_CSTs. */
6081 static tree
6082 move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
6084 int old_nr, new_nr;
6086 old_nr = tree_low_cst (old_t_nr, 0);
6087 new_nr = move_stmt_eh_region_nr (old_nr, p);
6089 return build_int_cst (integer_type_node, new_nr);
6092 /* Like move_stmt_op, but for gimple statements.
6094 Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
6095 contained in the current statement in *GSI_P and change the
6096 DECL_CONTEXT of every local variable referenced in the current
6097 statement. */
6099 static tree
6100 move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
6101 struct walk_stmt_info *wi)
6103 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6104 gimple stmt = gsi_stmt (*gsi_p);
6105 tree block = gimple_block (stmt);
6107 if (p->orig_block == NULL_TREE
6108 || block == p->orig_block
6109 || block == NULL_TREE)
6110 gimple_set_block (stmt, p->new_block);
6111 #ifdef ENABLE_CHECKING
6112 else if (block != p->new_block)
6114 while (block && block != p->orig_block)
6115 block = BLOCK_SUPERCONTEXT (block);
6116 gcc_assert (block);
6118 #endif
6120 switch (gimple_code (stmt))
6122 case GIMPLE_CALL:
6123 /* Remap the region numbers for __builtin_eh_{pointer,filter}. */
6125 tree r, fndecl = gimple_call_fndecl (stmt);
6126 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
6127 switch (DECL_FUNCTION_CODE (fndecl))
6129 case BUILT_IN_EH_COPY_VALUES:
6130 r = gimple_call_arg (stmt, 1);
6131 r = move_stmt_eh_region_tree_nr (r, p);
6132 gimple_call_set_arg (stmt, 1, r);
6133 /* FALLTHRU */
6135 case BUILT_IN_EH_POINTER:
6136 case BUILT_IN_EH_FILTER:
6137 r = gimple_call_arg (stmt, 0);
6138 r = move_stmt_eh_region_tree_nr (r, p);
6139 gimple_call_set_arg (stmt, 0, r);
6140 break;
6142 default:
6143 break;
6146 break;
6148 case GIMPLE_RESX:
6150 int r = gimple_resx_region (stmt);
6151 r = move_stmt_eh_region_nr (r, p);
6152 gimple_resx_set_region (stmt, r);
6154 break;
6156 case GIMPLE_EH_DISPATCH:
6158 int r = gimple_eh_dispatch_region (stmt);
6159 r = move_stmt_eh_region_nr (r, p);
6160 gimple_eh_dispatch_set_region (stmt, r);
6162 break;
6164 case GIMPLE_OMP_RETURN:
6165 case GIMPLE_OMP_CONTINUE:
6166 break;
6167 default:
6168 if (is_gimple_omp (stmt))
6170 /* Do not remap variables inside OMP directives. Variables
6171 referenced in clauses and directive header belong to the
6172 parent function and should not be moved into the child
6173 function. */
6174 bool save_remap_decls_p = p->remap_decls_p;
6175 p->remap_decls_p = false;
6176 *handled_ops_p = true;
6178 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), move_stmt_r,
6179 move_stmt_op, wi);
6181 p->remap_decls_p = save_remap_decls_p;
6183 break;
6186 return NULL_TREE;
6189 /* Move basic block BB from function CFUN to function DEST_FN. The
6190 block is moved out of the original linked list and placed after
6191 block AFTER in the new list. Also, the block is removed from the
6192 original array of blocks and placed in DEST_FN's array of blocks.
6193 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
6194 updated to reflect the moved edges.
6196 The local variables are remapped to new instances, VARS_MAP is used
6197 to record the mapping. */
6199 static void
6200 move_block_to_fn (struct function *dest_cfun, basic_block bb,
6201 basic_block after, bool update_edge_count_p,
6202 struct move_stmt_d *d)
6204 struct control_flow_graph *cfg;
6205 edge_iterator ei;
6206 edge e;
6207 gimple_stmt_iterator si;
6208 unsigned old_len, new_len;
6210 /* Remove BB from dominance structures. */
6211 delete_from_dominance_info (CDI_DOMINATORS, bb);
6212 if (current_loops)
6213 remove_bb_from_loops (bb);
6215 /* Link BB to the new linked list. */
6216 move_block_after (bb, after);
6218 /* Update the edge count in the corresponding flowgraphs. */
6219 if (update_edge_count_p)
6220 FOR_EACH_EDGE (e, ei, bb->succs)
6222 cfun->cfg->x_n_edges--;
6223 dest_cfun->cfg->x_n_edges++;
6226 /* Remove BB from the original basic block array. */
6227 VEC_replace (basic_block, cfun->cfg->x_basic_block_info, bb->index, NULL);
6228 cfun->cfg->x_n_basic_blocks--;
6230 /* Grow DEST_CFUN's basic block array if needed. */
6231 cfg = dest_cfun->cfg;
6232 cfg->x_n_basic_blocks++;
6233 if (bb->index >= cfg->x_last_basic_block)
6234 cfg->x_last_basic_block = bb->index + 1;
6236 old_len = VEC_length (basic_block, cfg->x_basic_block_info);
6237 if ((unsigned) cfg->x_last_basic_block >= old_len)
6239 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
6240 VEC_safe_grow_cleared (basic_block, gc, cfg->x_basic_block_info,
6241 new_len);
6244 VEC_replace (basic_block, cfg->x_basic_block_info,
6245 bb->index, bb);
6247 /* Remap the variables in phi nodes. */
6248 for (si = gsi_start_phis (bb); !gsi_end_p (si); )
6250 gimple phi = gsi_stmt (si);
6251 use_operand_p use;
6252 tree op = PHI_RESULT (phi);
6253 ssa_op_iter oi;
6255 if (virtual_operand_p (op))
6257 /* Remove the phi nodes for virtual operands (alias analysis will be
6258 run for the new function, anyway). */
6259 remove_phi_node (&si, true);
6260 continue;
6263 SET_PHI_RESULT (phi,
6264 replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6265 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
6267 op = USE_FROM_PTR (use);
6268 if (TREE_CODE (op) == SSA_NAME)
6269 SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6272 gsi_next (&si);
6275 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6277 gimple stmt = gsi_stmt (si);
6278 struct walk_stmt_info wi;
6280 memset (&wi, 0, sizeof (wi));
6281 wi.info = d;
6282 walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
6284 if (gimple_code (stmt) == GIMPLE_LABEL)
6286 tree label = gimple_label_label (stmt);
6287 int uid = LABEL_DECL_UID (label);
6289 gcc_assert (uid > -1);
6291 old_len = VEC_length (basic_block, cfg->x_label_to_block_map);
6292 if (old_len <= (unsigned) uid)
6294 new_len = 3 * uid / 2 + 1;
6295 VEC_safe_grow_cleared (basic_block, gc,
6296 cfg->x_label_to_block_map, new_len);
6299 VEC_replace (basic_block, cfg->x_label_to_block_map, uid, bb);
6300 VEC_replace (basic_block, cfun->cfg->x_label_to_block_map, uid, NULL);
6302 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
6304 if (uid >= dest_cfun->cfg->last_label_uid)
6305 dest_cfun->cfg->last_label_uid = uid + 1;
6308 maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
6309 remove_stmt_from_eh_lp_fn (cfun, stmt);
6311 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
6312 gimple_remove_stmt_histograms (cfun, stmt);
6314 /* We cannot leave any operands allocated from the operand caches of
6315 the current function. */
6316 free_stmt_operands (stmt);
6317 push_cfun (dest_cfun);
6318 update_stmt (stmt);
6319 pop_cfun ();
6322 FOR_EACH_EDGE (e, ei, bb->succs)
6323 if (e->goto_locus)
6325 tree block = e->goto_block;
6326 if (d->orig_block == NULL_TREE
6327 || block == d->orig_block)
6328 e->goto_block = d->new_block;
6329 #ifdef ENABLE_CHECKING
6330 else if (block != d->new_block)
6332 while (block && block != d->orig_block)
6333 block = BLOCK_SUPERCONTEXT (block);
6334 gcc_assert (block);
6336 #endif
6340 /* Examine the statements in BB (which is in SRC_CFUN); find and return
6341 the outermost EH region. Use REGION as the incoming base EH region. */
6343 static eh_region
6344 find_outermost_region_in_block (struct function *src_cfun,
6345 basic_block bb, eh_region region)
6347 gimple_stmt_iterator si;
6349 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6351 gimple stmt = gsi_stmt (si);
6352 eh_region stmt_region;
6353 int lp_nr;
6355 lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
6356 stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
6357 if (stmt_region)
6359 if (region == NULL)
6360 region = stmt_region;
6361 else if (stmt_region != region)
6363 region = eh_region_outermost (src_cfun, stmt_region, region);
6364 gcc_assert (region != NULL);
6369 return region;
6372 static tree
6373 new_label_mapper (tree decl, void *data)
6375 htab_t hash = (htab_t) data;
6376 struct tree_map *m;
6377 void **slot;
6379 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
6381 m = XNEW (struct tree_map);
6382 m->hash = DECL_UID (decl);
6383 m->base.from = decl;
6384 m->to = create_artificial_label (UNKNOWN_LOCATION);
6385 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
6386 if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
6387 cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
6389 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
6390 gcc_assert (*slot == NULL);
6392 *slot = m;
6394 return m->to;
6397 /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
6398 subblocks. */
6400 static void
6401 replace_block_vars_by_duplicates (tree block, struct pointer_map_t *vars_map,
6402 tree to_context)
6404 tree *tp, t;
6406 for (tp = &BLOCK_VARS (block); *tp; tp = &DECL_CHAIN (*tp))
6408 t = *tp;
6409 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
6410 continue;
6411 replace_by_duplicate_decl (&t, vars_map, to_context);
6412 if (t != *tp)
6414 if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
6416 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
6417 DECL_HAS_VALUE_EXPR_P (t) = 1;
6419 DECL_CHAIN (t) = DECL_CHAIN (*tp);
6420 *tp = t;
6424 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
6425 replace_block_vars_by_duplicates (block, vars_map, to_context);
6428 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
6429 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
6430 single basic block in the original CFG and the new basic block is
6431 returned. DEST_CFUN must not have a CFG yet.
6433 Note that the region need not be a pure SESE region. Blocks inside
6434 the region may contain calls to abort/exit. The only restriction
6435 is that ENTRY_BB should be the only entry point and it must
6436 dominate EXIT_BB.
6438 Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
6439 functions outermost BLOCK, move all subblocks of ORIG_BLOCK
6440 to the new function.
6442 All local variables referenced in the region are assumed to be in
6443 the corresponding BLOCK_VARS and unexpanded variable lists
6444 associated with DEST_CFUN. */
6446 basic_block
6447 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
6448 basic_block exit_bb, tree orig_block)
6450 VEC(basic_block,heap) *bbs, *dom_bbs;
6451 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
6452 basic_block after, bb, *entry_pred, *exit_succ, abb;
6453 struct function *saved_cfun = cfun;
6454 int *entry_flag, *exit_flag;
6455 unsigned *entry_prob, *exit_prob;
6456 unsigned i, num_entry_edges, num_exit_edges;
6457 edge e;
6458 edge_iterator ei;
6459 htab_t new_label_map;
6460 struct pointer_map_t *vars_map, *eh_map;
6461 struct loop *loop = entry_bb->loop_father;
6462 struct move_stmt_d d;
6464 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
6465 region. */
6466 gcc_assert (entry_bb != exit_bb
6467 && (!exit_bb
6468 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
6470 /* Collect all the blocks in the region. Manually add ENTRY_BB
6471 because it won't be added by dfs_enumerate_from. */
6472 bbs = NULL;
6473 VEC_safe_push (basic_block, heap, bbs, entry_bb);
6474 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
6476 /* The blocks that used to be dominated by something in BBS will now be
6477 dominated by the new block. */
6478 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
6479 VEC_address (basic_block, bbs),
6480 VEC_length (basic_block, bbs));
6482 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
6483 the predecessor edges to ENTRY_BB and the successor edges to
6484 EXIT_BB so that we can re-attach them to the new basic block that
6485 will replace the region. */
6486 num_entry_edges = EDGE_COUNT (entry_bb->preds);
6487 entry_pred = XNEWVEC (basic_block, num_entry_edges);
6488 entry_flag = XNEWVEC (int, num_entry_edges);
6489 entry_prob = XNEWVEC (unsigned, num_entry_edges);
6490 i = 0;
6491 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
6493 entry_prob[i] = e->probability;
6494 entry_flag[i] = e->flags;
6495 entry_pred[i++] = e->src;
6496 remove_edge (e);
6499 if (exit_bb)
6501 num_exit_edges = EDGE_COUNT (exit_bb->succs);
6502 exit_succ = XNEWVEC (basic_block, num_exit_edges);
6503 exit_flag = XNEWVEC (int, num_exit_edges);
6504 exit_prob = XNEWVEC (unsigned, num_exit_edges);
6505 i = 0;
6506 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
6508 exit_prob[i] = e->probability;
6509 exit_flag[i] = e->flags;
6510 exit_succ[i++] = e->dest;
6511 remove_edge (e);
6514 else
6516 num_exit_edges = 0;
6517 exit_succ = NULL;
6518 exit_flag = NULL;
6519 exit_prob = NULL;
6522 /* Switch context to the child function to initialize DEST_FN's CFG. */
6523 gcc_assert (dest_cfun->cfg == NULL);
6524 push_cfun (dest_cfun);
6526 init_empty_tree_cfg ();
6528 /* Initialize EH information for the new function. */
6529 eh_map = NULL;
6530 new_label_map = NULL;
6531 if (saved_cfun->eh)
6533 eh_region region = NULL;
6535 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
6536 region = find_outermost_region_in_block (saved_cfun, bb, region);
6538 init_eh_for_function ();
6539 if (region != NULL)
6541 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
6542 eh_map = duplicate_eh_regions (saved_cfun, region, 0,
6543 new_label_mapper, new_label_map);
6547 pop_cfun ();
6549 /* Move blocks from BBS into DEST_CFUN. */
6550 gcc_assert (VEC_length (basic_block, bbs) >= 2);
6551 after = dest_cfun->cfg->x_entry_block_ptr;
6552 vars_map = pointer_map_create ();
6554 memset (&d, 0, sizeof (d));
6555 d.orig_block = orig_block;
6556 d.new_block = DECL_INITIAL (dest_cfun->decl);
6557 d.from_context = cfun->decl;
6558 d.to_context = dest_cfun->decl;
6559 d.vars_map = vars_map;
6560 d.new_label_map = new_label_map;
6561 d.eh_map = eh_map;
6562 d.remap_decls_p = true;
6564 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
6566 /* No need to update edge counts on the last block. It has
6567 already been updated earlier when we detached the region from
6568 the original CFG. */
6569 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
6570 after = bb;
6573 /* Rewire BLOCK_SUBBLOCKS of orig_block. */
6574 if (orig_block)
6576 tree block;
6577 gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
6578 == NULL_TREE);
6579 BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
6580 = BLOCK_SUBBLOCKS (orig_block);
6581 for (block = BLOCK_SUBBLOCKS (orig_block);
6582 block; block = BLOCK_CHAIN (block))
6583 BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
6584 BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
6587 replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
6588 vars_map, dest_cfun->decl);
6590 if (new_label_map)
6591 htab_delete (new_label_map);
6592 if (eh_map)
6593 pointer_map_destroy (eh_map);
6594 pointer_map_destroy (vars_map);
6596 /* Rewire the entry and exit blocks. The successor to the entry
6597 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
6598 the child function. Similarly, the predecessor of DEST_FN's
6599 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
6600 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
6601 various CFG manipulation function get to the right CFG.
6603 FIXME, this is silly. The CFG ought to become a parameter to
6604 these helpers. */
6605 push_cfun (dest_cfun);
6606 make_edge (ENTRY_BLOCK_PTR, entry_bb, EDGE_FALLTHRU);
6607 if (exit_bb)
6608 make_edge (exit_bb, EXIT_BLOCK_PTR, 0);
6609 pop_cfun ();
6611 /* Back in the original function, the SESE region has disappeared,
6612 create a new basic block in its place. */
6613 bb = create_empty_bb (entry_pred[0]);
6614 if (current_loops)
6615 add_bb_to_loop (bb, loop);
6616 for (i = 0; i < num_entry_edges; i++)
6618 e = make_edge (entry_pred[i], bb, entry_flag[i]);
6619 e->probability = entry_prob[i];
6622 for (i = 0; i < num_exit_edges; i++)
6624 e = make_edge (bb, exit_succ[i], exit_flag[i]);
6625 e->probability = exit_prob[i];
6628 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
6629 FOR_EACH_VEC_ELT (basic_block, dom_bbs, i, abb)
6630 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
6631 VEC_free (basic_block, heap, dom_bbs);
6633 if (exit_bb)
6635 free (exit_prob);
6636 free (exit_flag);
6637 free (exit_succ);
6639 free (entry_prob);
6640 free (entry_flag);
6641 free (entry_pred);
6642 VEC_free (basic_block, heap, bbs);
6644 return bb;
6648 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree-pass.h)
6651 void
6652 dump_function_to_file (tree fndecl, FILE *file, int flags)
6654 tree arg, var, old_current_fndecl = current_function_decl;
6655 struct function *dsf;
6656 bool ignore_topmost_bind = false, any_var = false;
6657 basic_block bb;
6658 tree chain;
6659 bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
6660 && decl_is_tm_clone (fndecl));
6661 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
6663 current_function_decl = fndecl;
6664 fprintf (file, "%s %s(", function_name (fun), tmclone ? "[tm-clone] " : "");
6666 arg = DECL_ARGUMENTS (fndecl);
6667 while (arg)
6669 print_generic_expr (file, TREE_TYPE (arg), dump_flags);
6670 fprintf (file, " ");
6671 print_generic_expr (file, arg, dump_flags);
6672 if (flags & TDF_VERBOSE)
6673 print_node (file, "", arg, 4);
6674 if (DECL_CHAIN (arg))
6675 fprintf (file, ", ");
6676 arg = DECL_CHAIN (arg);
6678 fprintf (file, ")\n");
6680 if (flags & TDF_VERBOSE)
6681 print_node (file, "", fndecl, 2);
6683 dsf = DECL_STRUCT_FUNCTION (fndecl);
6684 if (dsf && (flags & TDF_EH))
6685 dump_eh_tree (file, dsf);
6687 if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
6689 dump_node (fndecl, TDF_SLIM | flags, file);
6690 current_function_decl = old_current_fndecl;
6691 return;
6694 /* When GIMPLE is lowered, the variables are no longer available in
6695 BIND_EXPRs, so display them separately. */
6696 if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
6698 unsigned ix;
6699 ignore_topmost_bind = true;
6701 fprintf (file, "{\n");
6702 if (!VEC_empty (tree, fun->local_decls))
6703 FOR_EACH_LOCAL_DECL (fun, ix, var)
6705 print_generic_decl (file, var, flags);
6706 if (flags & TDF_VERBOSE)
6707 print_node (file, "", var, 4);
6708 fprintf (file, "\n");
6710 any_var = true;
6712 if (gimple_in_ssa_p (cfun))
6713 for (ix = 1; ix < num_ssa_names; ++ix)
6715 tree name = ssa_name (ix);
6716 if (name && !SSA_NAME_VAR (name))
6718 fprintf (file, " ");
6719 print_generic_expr (file, TREE_TYPE (name), flags);
6720 fprintf (file, " ");
6721 print_generic_expr (file, name, flags);
6722 fprintf (file, ";\n");
6724 any_var = true;
6729 if (fun && fun->decl == fndecl && fun->cfg
6730 && basic_block_info_for_function (fun))
6732 /* If the CFG has been built, emit a CFG-based dump. */
6733 if (!ignore_topmost_bind)
6734 fprintf (file, "{\n");
6736 if (any_var && n_basic_blocks_for_function (fun))
6737 fprintf (file, "\n");
6739 FOR_EACH_BB_FN (bb, fun)
6740 dump_bb (file, bb, 2, flags | TDF_COMMENT);
6742 fprintf (file, "}\n");
6744 else if (DECL_SAVED_TREE (fndecl) == NULL)
6746 /* The function is now in GIMPLE form but the CFG has not been
6747 built yet. Emit the single sequence of GIMPLE statements
6748 that make up its body. */
6749 gimple_seq body = gimple_body (fndecl);
6751 if (gimple_seq_first_stmt (body)
6752 && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
6753 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
6754 print_gimple_seq (file, body, 0, flags);
6755 else
6757 if (!ignore_topmost_bind)
6758 fprintf (file, "{\n");
6760 if (any_var)
6761 fprintf (file, "\n");
6763 print_gimple_seq (file, body, 2, flags);
6764 fprintf (file, "}\n");
6767 else
6769 int indent;
6771 /* Make a tree based dump. */
6772 chain = DECL_SAVED_TREE (fndecl);
6773 if (chain && TREE_CODE (chain) == BIND_EXPR)
6775 if (ignore_topmost_bind)
6777 chain = BIND_EXPR_BODY (chain);
6778 indent = 2;
6780 else
6781 indent = 0;
6783 else
6785 if (!ignore_topmost_bind)
6786 fprintf (file, "{\n");
6787 indent = 2;
6790 if (any_var)
6791 fprintf (file, "\n");
6793 print_generic_stmt_indented (file, chain, flags, indent);
6794 if (ignore_topmost_bind)
6795 fprintf (file, "}\n");
6798 if (flags & TDF_ENUMERATE_LOCALS)
6799 dump_enumerated_decls (file, flags);
6800 fprintf (file, "\n\n");
6802 current_function_decl = old_current_fndecl;
6805 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
6807 DEBUG_FUNCTION void
6808 debug_function (tree fn, int flags)
6810 dump_function_to_file (fn, stderr, flags);
6814 /* Print on FILE the indexes for the predecessors of basic_block BB. */
6816 static void
6817 print_pred_bbs (FILE *file, basic_block bb)
6819 edge e;
6820 edge_iterator ei;
6822 FOR_EACH_EDGE (e, ei, bb->preds)
6823 fprintf (file, "bb_%d ", e->src->index);
6827 /* Print on FILE the indexes for the successors of basic_block BB. */
6829 static void
6830 print_succ_bbs (FILE *file, basic_block bb)
6832 edge e;
6833 edge_iterator ei;
6835 FOR_EACH_EDGE (e, ei, bb->succs)
6836 fprintf (file, "bb_%d ", e->dest->index);
6839 /* Print to FILE the basic block BB following the VERBOSITY level. */
6841 void
6842 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
6844 char *s_indent = (char *) alloca ((size_t) indent + 1);
6845 memset ((void *) s_indent, ' ', (size_t) indent);
6846 s_indent[indent] = '\0';
6848 /* Print basic_block's header. */
6849 if (verbosity >= 2)
6851 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
6852 print_pred_bbs (file, bb);
6853 fprintf (file, "}, succs = {");
6854 print_succ_bbs (file, bb);
6855 fprintf (file, "})\n");
6858 /* Print basic_block's body. */
6859 if (verbosity >= 3)
6861 fprintf (file, "%s {\n", s_indent);
6862 dump_bb (file, bb, indent + 4, TDF_VOPS|TDF_MEMSYMS);
6863 fprintf (file, "%s }\n", s_indent);
6867 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
6869 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
6870 VERBOSITY level this outputs the contents of the loop, or just its
6871 structure. */
6873 static void
6874 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
6876 char *s_indent;
6877 basic_block bb;
6879 if (loop == NULL)
6880 return;
6882 s_indent = (char *) alloca ((size_t) indent + 1);
6883 memset ((void *) s_indent, ' ', (size_t) indent);
6884 s_indent[indent] = '\0';
6886 /* Print loop's header. */
6887 fprintf (file, "%sloop_%d (", s_indent, loop->num);
6888 if (loop->header)
6889 fprintf (file, "header = %d", loop->header->index);
6890 else
6892 fprintf (file, "deleted)\n");
6893 return;
6895 if (loop->latch)
6896 fprintf (file, ", latch = %d", loop->latch->index);
6897 else
6898 fprintf (file, ", multiple latches");
6899 fprintf (file, ", niter = ");
6900 print_generic_expr (file, loop->nb_iterations, 0);
6902 if (loop->any_upper_bound)
6904 fprintf (file, ", upper_bound = ");
6905 dump_double_int (file, loop->nb_iterations_upper_bound, true);
6908 if (loop->any_estimate)
6910 fprintf (file, ", estimate = ");
6911 dump_double_int (file, loop->nb_iterations_estimate, true);
6913 fprintf (file, ")\n");
6915 /* Print loop's body. */
6916 if (verbosity >= 1)
6918 fprintf (file, "%s{\n", s_indent);
6919 FOR_EACH_BB (bb)
6920 if (bb->loop_father == loop)
6921 print_loops_bb (file, bb, indent, verbosity);
6923 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
6924 fprintf (file, "%s}\n", s_indent);
6928 /* Print the LOOP and its sibling loops on FILE, indented INDENT
6929 spaces. Following VERBOSITY level this outputs the contents of the
6930 loop, or just its structure. */
6932 static void
6933 print_loop_and_siblings (FILE *file, struct loop *loop, int indent, int verbosity)
6935 if (loop == NULL)
6936 return;
6938 print_loop (file, loop, indent, verbosity);
6939 print_loop_and_siblings (file, loop->next, indent, verbosity);
6942 /* Follow a CFG edge from the entry point of the program, and on entry
6943 of a loop, pretty print the loop structure on FILE. */
6945 void
6946 print_loops (FILE *file, int verbosity)
6948 basic_block bb;
6950 bb = ENTRY_BLOCK_PTR;
6951 if (bb && bb->loop_father)
6952 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
6956 /* Debugging loops structure at tree level, at some VERBOSITY level. */
6958 DEBUG_FUNCTION void
6959 debug_loops (int verbosity)
6961 print_loops (stderr, verbosity);
6964 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
6966 DEBUG_FUNCTION void
6967 debug_loop (struct loop *loop, int verbosity)
6969 print_loop (stderr, loop, 0, verbosity);
6972 /* Print on stderr the code of loop number NUM, at some VERBOSITY
6973 level. */
6975 DEBUG_FUNCTION void
6976 debug_loop_num (unsigned num, int verbosity)
6978 debug_loop (get_loop (num), verbosity);
6981 /* Return true if BB ends with a call, possibly followed by some
6982 instructions that must stay with the call. Return false,
6983 otherwise. */
6985 static bool
6986 gimple_block_ends_with_call_p (basic_block bb)
6988 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
6989 return !gsi_end_p (gsi) && is_gimple_call (gsi_stmt (gsi));
6993 /* Return true if BB ends with a conditional branch. Return false,
6994 otherwise. */
6996 static bool
6997 gimple_block_ends_with_condjump_p (const_basic_block bb)
6999 gimple stmt = last_stmt (CONST_CAST_BB (bb));
7000 return (stmt && gimple_code (stmt) == GIMPLE_COND);
7004 /* Return true if we need to add fake edge to exit at statement T.
7005 Helper function for gimple_flow_call_edges_add. */
7007 static bool
7008 need_fake_edge_p (gimple t)
7010 tree fndecl = NULL_TREE;
7011 int call_flags = 0;
7013 /* NORETURN and LONGJMP calls already have an edge to exit.
7014 CONST and PURE calls do not need one.
7015 We don't currently check for CONST and PURE here, although
7016 it would be a good idea, because those attributes are
7017 figured out from the RTL in mark_constant_function, and
7018 the counter incrementation code from -fprofile-arcs
7019 leads to different results from -fbranch-probabilities. */
7020 if (is_gimple_call (t))
7022 fndecl = gimple_call_fndecl (t);
7023 call_flags = gimple_call_flags (t);
7026 if (is_gimple_call (t)
7027 && fndecl
7028 && DECL_BUILT_IN (fndecl)
7029 && (call_flags & ECF_NOTHROW)
7030 && !(call_flags & ECF_RETURNS_TWICE)
7031 /* fork() doesn't really return twice, but the effect of
7032 wrapping it in __gcov_fork() which calls __gcov_flush()
7033 and clears the counters before forking has the same
7034 effect as returning twice. Force a fake edge. */
7035 && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7036 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
7037 return false;
7039 if (is_gimple_call (t))
7041 edge_iterator ei;
7042 edge e;
7043 basic_block bb;
7045 if (!(call_flags & ECF_NORETURN))
7046 return true;
7048 bb = gimple_bb (t);
7049 FOR_EACH_EDGE (e, ei, bb->succs)
7050 if ((e->flags & EDGE_FAKE) == 0)
7051 return true;
7054 if (gimple_code (t) == GIMPLE_ASM
7055 && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
7056 return true;
7058 return false;
7062 /* Add fake edges to the function exit for any non constant and non
7063 noreturn calls (or noreturn calls with EH/abnormal edges),
7064 volatile inline assembly in the bitmap of blocks specified by BLOCKS
7065 or to the whole CFG if BLOCKS is zero. Return the number of blocks
7066 that were split.
7068 The goal is to expose cases in which entering a basic block does
7069 not imply that all subsequent instructions must be executed. */
7071 static int
7072 gimple_flow_call_edges_add (sbitmap blocks)
7074 int i;
7075 int blocks_split = 0;
7076 int last_bb = last_basic_block;
7077 bool check_last_block = false;
7079 if (n_basic_blocks == NUM_FIXED_BLOCKS)
7080 return 0;
7082 if (! blocks)
7083 check_last_block = true;
7084 else
7085 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
7087 /* In the last basic block, before epilogue generation, there will be
7088 a fallthru edge to EXIT. Special care is required if the last insn
7089 of the last basic block is a call because make_edge folds duplicate
7090 edges, which would result in the fallthru edge also being marked
7091 fake, which would result in the fallthru edge being removed by
7092 remove_fake_edges, which would result in an invalid CFG.
7094 Moreover, we can't elide the outgoing fake edge, since the block
7095 profiler needs to take this into account in order to solve the minimal
7096 spanning tree in the case that the call doesn't return.
7098 Handle this by adding a dummy instruction in a new last basic block. */
7099 if (check_last_block)
7101 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
7102 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7103 gimple t = NULL;
7105 if (!gsi_end_p (gsi))
7106 t = gsi_stmt (gsi);
7108 if (t && need_fake_edge_p (t))
7110 edge e;
7112 e = find_edge (bb, EXIT_BLOCK_PTR);
7113 if (e)
7115 gsi_insert_on_edge (e, gimple_build_nop ());
7116 gsi_commit_edge_inserts ();
7121 /* Now add fake edges to the function exit for any non constant
7122 calls since there is no way that we can determine if they will
7123 return or not... */
7124 for (i = 0; i < last_bb; i++)
7126 basic_block bb = BASIC_BLOCK (i);
7127 gimple_stmt_iterator gsi;
7128 gimple stmt, last_stmt;
7130 if (!bb)
7131 continue;
7133 if (blocks && !TEST_BIT (blocks, i))
7134 continue;
7136 gsi = gsi_last_nondebug_bb (bb);
7137 if (!gsi_end_p (gsi))
7139 last_stmt = gsi_stmt (gsi);
7142 stmt = gsi_stmt (gsi);
7143 if (need_fake_edge_p (stmt))
7145 edge e;
7147 /* The handling above of the final block before the
7148 epilogue should be enough to verify that there is
7149 no edge to the exit block in CFG already.
7150 Calling make_edge in such case would cause us to
7151 mark that edge as fake and remove it later. */
7152 #ifdef ENABLE_CHECKING
7153 if (stmt == last_stmt)
7155 e = find_edge (bb, EXIT_BLOCK_PTR);
7156 gcc_assert (e == NULL);
7158 #endif
7160 /* Note that the following may create a new basic block
7161 and renumber the existing basic blocks. */
7162 if (stmt != last_stmt)
7164 e = split_block (bb, stmt);
7165 if (e)
7166 blocks_split++;
7168 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
7170 gsi_prev (&gsi);
7172 while (!gsi_end_p (gsi));
7176 if (blocks_split)
7177 verify_flow_info ();
7179 return blocks_split;
7182 /* Removes edge E and all the blocks dominated by it, and updates dominance
7183 information. The IL in E->src needs to be updated separately.
7184 If dominance info is not available, only the edge E is removed.*/
7186 void
7187 remove_edge_and_dominated_blocks (edge e)
7189 VEC (basic_block, heap) *bbs_to_remove = NULL;
7190 VEC (basic_block, heap) *bbs_to_fix_dom = NULL;
7191 bitmap df, df_idom;
7192 edge f;
7193 edge_iterator ei;
7194 bool none_removed = false;
7195 unsigned i;
7196 basic_block bb, dbb;
7197 bitmap_iterator bi;
7199 if (!dom_info_available_p (CDI_DOMINATORS))
7201 remove_edge (e);
7202 return;
7205 /* No updating is needed for edges to exit. */
7206 if (e->dest == EXIT_BLOCK_PTR)
7208 if (cfgcleanup_altered_bbs)
7209 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7210 remove_edge (e);
7211 return;
7214 /* First, we find the basic blocks to remove. If E->dest has a predecessor
7215 that is not dominated by E->dest, then this set is empty. Otherwise,
7216 all the basic blocks dominated by E->dest are removed.
7218 Also, to DF_IDOM we store the immediate dominators of the blocks in
7219 the dominance frontier of E (i.e., of the successors of the
7220 removed blocks, if there are any, and of E->dest otherwise). */
7221 FOR_EACH_EDGE (f, ei, e->dest->preds)
7223 if (f == e)
7224 continue;
7226 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
7228 none_removed = true;
7229 break;
7233 df = BITMAP_ALLOC (NULL);
7234 df_idom = BITMAP_ALLOC (NULL);
7236 if (none_removed)
7237 bitmap_set_bit (df_idom,
7238 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
7239 else
7241 bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
7242 FOR_EACH_VEC_ELT (basic_block, bbs_to_remove, i, bb)
7244 FOR_EACH_EDGE (f, ei, bb->succs)
7246 if (f->dest != EXIT_BLOCK_PTR)
7247 bitmap_set_bit (df, f->dest->index);
7250 FOR_EACH_VEC_ELT (basic_block, bbs_to_remove, i, bb)
7251 bitmap_clear_bit (df, bb->index);
7253 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
7255 bb = BASIC_BLOCK (i);
7256 bitmap_set_bit (df_idom,
7257 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
7261 if (cfgcleanup_altered_bbs)
7263 /* Record the set of the altered basic blocks. */
7264 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7265 bitmap_ior_into (cfgcleanup_altered_bbs, df);
7268 /* Remove E and the cancelled blocks. */
7269 if (none_removed)
7270 remove_edge (e);
7271 else
7273 /* Walk backwards so as to get a chance to substitute all
7274 released DEFs into debug stmts. See
7275 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
7276 details. */
7277 for (i = VEC_length (basic_block, bbs_to_remove); i-- > 0; )
7278 delete_basic_block (VEC_index (basic_block, bbs_to_remove, i));
7281 /* Update the dominance information. The immediate dominator may change only
7282 for blocks whose immediate dominator belongs to DF_IDOM:
7284 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
7285 removal. Let Z the arbitrary block such that idom(Z) = Y and
7286 Z dominates X after the removal. Before removal, there exists a path P
7287 from Y to X that avoids Z. Let F be the last edge on P that is
7288 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
7289 dominates W, and because of P, Z does not dominate W), and W belongs to
7290 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
7291 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
7293 bb = BASIC_BLOCK (i);
7294 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
7295 dbb;
7296 dbb = next_dom_son (CDI_DOMINATORS, dbb))
7297 VEC_safe_push (basic_block, heap, bbs_to_fix_dom, dbb);
7300 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
7302 BITMAP_FREE (df);
7303 BITMAP_FREE (df_idom);
7304 VEC_free (basic_block, heap, bbs_to_remove);
7305 VEC_free (basic_block, heap, bbs_to_fix_dom);
7308 /* Purge dead EH edges from basic block BB. */
7310 bool
7311 gimple_purge_dead_eh_edges (basic_block bb)
7313 bool changed = false;
7314 edge e;
7315 edge_iterator ei;
7316 gimple stmt = last_stmt (bb);
7318 if (stmt && stmt_can_throw_internal (stmt))
7319 return false;
7321 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7323 if (e->flags & EDGE_EH)
7325 remove_edge_and_dominated_blocks (e);
7326 changed = true;
7328 else
7329 ei_next (&ei);
7332 return changed;
7335 /* Purge dead EH edges from basic block listed in BLOCKS. */
7337 bool
7338 gimple_purge_all_dead_eh_edges (const_bitmap blocks)
7340 bool changed = false;
7341 unsigned i;
7342 bitmap_iterator bi;
7344 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7346 basic_block bb = BASIC_BLOCK (i);
7348 /* Earlier gimple_purge_dead_eh_edges could have removed
7349 this basic block already. */
7350 gcc_assert (bb || changed);
7351 if (bb != NULL)
7352 changed |= gimple_purge_dead_eh_edges (bb);
7355 return changed;
7358 /* Purge dead abnormal call edges from basic block BB. */
7360 bool
7361 gimple_purge_dead_abnormal_call_edges (basic_block bb)
7363 bool changed = false;
7364 edge e;
7365 edge_iterator ei;
7366 gimple stmt = last_stmt (bb);
7368 if (!cfun->has_nonlocal_label)
7369 return false;
7371 if (stmt && stmt_can_make_abnormal_goto (stmt))
7372 return false;
7374 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7376 if (e->flags & EDGE_ABNORMAL)
7378 remove_edge_and_dominated_blocks (e);
7379 changed = true;
7381 else
7382 ei_next (&ei);
7385 return changed;
7388 /* Purge dead abnormal call edges from basic block listed in BLOCKS. */
7390 bool
7391 gimple_purge_all_dead_abnormal_call_edges (const_bitmap blocks)
7393 bool changed = false;
7394 unsigned i;
7395 bitmap_iterator bi;
7397 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7399 basic_block bb = BASIC_BLOCK (i);
7401 /* Earlier gimple_purge_dead_abnormal_call_edges could have removed
7402 this basic block already. */
7403 gcc_assert (bb || changed);
7404 if (bb != NULL)
7405 changed |= gimple_purge_dead_abnormal_call_edges (bb);
7408 return changed;
7411 /* This function is called whenever a new edge is created or
7412 redirected. */
7414 static void
7415 gimple_execute_on_growing_pred (edge e)
7417 basic_block bb = e->dest;
7419 if (!gimple_seq_empty_p (phi_nodes (bb)))
7420 reserve_phi_args_for_new_edge (bb);
7423 /* This function is called immediately before edge E is removed from
7424 the edge vector E->dest->preds. */
7426 static void
7427 gimple_execute_on_shrinking_pred (edge e)
7429 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
7430 remove_phi_args (e);
7433 /*---------------------------------------------------------------------------
7434 Helper functions for Loop versioning
7435 ---------------------------------------------------------------------------*/
7437 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
7438 of 'first'. Both of them are dominated by 'new_head' basic block. When
7439 'new_head' was created by 'second's incoming edge it received phi arguments
7440 on the edge by split_edge(). Later, additional edge 'e' was created to
7441 connect 'new_head' and 'first'. Now this routine adds phi args on this
7442 additional edge 'e' that new_head to second edge received as part of edge
7443 splitting. */
7445 static void
7446 gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
7447 basic_block new_head, edge e)
7449 gimple phi1, phi2;
7450 gimple_stmt_iterator psi1, psi2;
7451 tree def;
7452 edge e2 = find_edge (new_head, second);
7454 /* Because NEW_HEAD has been created by splitting SECOND's incoming
7455 edge, we should always have an edge from NEW_HEAD to SECOND. */
7456 gcc_assert (e2 != NULL);
7458 /* Browse all 'second' basic block phi nodes and add phi args to
7459 edge 'e' for 'first' head. PHI args are always in correct order. */
7461 for (psi2 = gsi_start_phis (second),
7462 psi1 = gsi_start_phis (first);
7463 !gsi_end_p (psi2) && !gsi_end_p (psi1);
7464 gsi_next (&psi2), gsi_next (&psi1))
7466 phi1 = gsi_stmt (psi1);
7467 phi2 = gsi_stmt (psi2);
7468 def = PHI_ARG_DEF (phi2, e2->dest_idx);
7469 add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
7474 /* Adds a if else statement to COND_BB with condition COND_EXPR.
7475 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
7476 the destination of the ELSE part. */
7478 static void
7479 gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
7480 basic_block second_head ATTRIBUTE_UNUSED,
7481 basic_block cond_bb, void *cond_e)
7483 gimple_stmt_iterator gsi;
7484 gimple new_cond_expr;
7485 tree cond_expr = (tree) cond_e;
7486 edge e0;
7488 /* Build new conditional expr */
7489 new_cond_expr = gimple_build_cond_from_tree (cond_expr,
7490 NULL_TREE, NULL_TREE);
7492 /* Add new cond in cond_bb. */
7493 gsi = gsi_last_bb (cond_bb);
7494 gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
7496 /* Adjust edges appropriately to connect new head with first head
7497 as well as second head. */
7498 e0 = single_succ_edge (cond_bb);
7499 e0->flags &= ~EDGE_FALLTHRU;
7500 e0->flags |= EDGE_FALSE_VALUE;
7503 struct cfg_hooks gimple_cfg_hooks = {
7504 "gimple",
7505 gimple_verify_flow_info,
7506 gimple_dump_bb, /* dump_bb */
7507 create_bb, /* create_basic_block */
7508 gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
7509 gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
7510 gimple_can_remove_branch_p, /* can_remove_branch_p */
7511 remove_bb, /* delete_basic_block */
7512 gimple_split_block, /* split_block */
7513 gimple_move_block_after, /* move_block_after */
7514 gimple_can_merge_blocks_p, /* can_merge_blocks_p */
7515 gimple_merge_blocks, /* merge_blocks */
7516 gimple_predict_edge, /* predict_edge */
7517 gimple_predicted_by_p, /* predicted_by_p */
7518 gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
7519 gimple_duplicate_bb, /* duplicate_block */
7520 gimple_split_edge, /* split_edge */
7521 gimple_make_forwarder_block, /* make_forward_block */
7522 NULL, /* tidy_fallthru_edge */
7523 NULL, /* force_nonfallthru */
7524 gimple_block_ends_with_call_p,/* block_ends_with_call_p */
7525 gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
7526 gimple_flow_call_edges_add, /* flow_call_edges_add */
7527 gimple_execute_on_growing_pred, /* execute_on_growing_pred */
7528 gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
7529 gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
7530 gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
7531 gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
7532 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
7533 flush_pending_stmts, /* flush_pending_stmts */
7534 gimple_empty_block_p, /* block_empty_p */
7535 gimple_split_block_before_cond_jump, /* split_block_before_cond_jump */
7539 /* Split all critical edges. */
7541 static unsigned int
7542 split_critical_edges (void)
7544 basic_block bb;
7545 edge e;
7546 edge_iterator ei;
7548 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
7549 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
7550 mappings around the calls to split_edge. */
7551 start_recording_case_labels ();
7552 FOR_ALL_BB (bb)
7554 FOR_EACH_EDGE (e, ei, bb->succs)
7556 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
7557 split_edge (e);
7558 /* PRE inserts statements to edges and expects that
7559 since split_critical_edges was done beforehand, committing edge
7560 insertions will not split more edges. In addition to critical
7561 edges we must split edges that have multiple successors and
7562 end by control flow statements, such as RESX.
7563 Go ahead and split them too. This matches the logic in
7564 gimple_find_edge_insert_loc. */
7565 else if ((!single_pred_p (e->dest)
7566 || !gimple_seq_empty_p (phi_nodes (e->dest))
7567 || e->dest == EXIT_BLOCK_PTR)
7568 && e->src != ENTRY_BLOCK_PTR
7569 && !(e->flags & EDGE_ABNORMAL))
7571 gimple_stmt_iterator gsi;
7573 gsi = gsi_last_bb (e->src);
7574 if (!gsi_end_p (gsi)
7575 && stmt_ends_bb_p (gsi_stmt (gsi))
7576 && (gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN
7577 && !gimple_call_builtin_p (gsi_stmt (gsi),
7578 BUILT_IN_RETURN)))
7579 split_edge (e);
7583 end_recording_case_labels ();
7584 return 0;
7587 struct gimple_opt_pass pass_split_crit_edges =
7590 GIMPLE_PASS,
7591 "crited", /* name */
7592 NULL, /* gate */
7593 split_critical_edges, /* execute */
7594 NULL, /* sub */
7595 NULL, /* next */
7596 0, /* static_pass_number */
7597 TV_TREE_SPLIT_EDGES, /* tv_id */
7598 PROP_cfg, /* properties required */
7599 PROP_no_crit_edges, /* properties_provided */
7600 0, /* properties_destroyed */
7601 0, /* todo_flags_start */
7602 TODO_verify_flow /* todo_flags_finish */
7607 /* Build a ternary operation and gimplify it. Emit code before GSI.
7608 Return the gimple_val holding the result. */
7610 tree
7611 gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
7612 tree type, tree a, tree b, tree c)
7614 tree ret;
7615 location_t loc = gimple_location (gsi_stmt (*gsi));
7617 ret = fold_build3_loc (loc, code, type, a, b, c);
7618 STRIP_NOPS (ret);
7620 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7621 GSI_SAME_STMT);
7624 /* Build a binary operation and gimplify it. Emit code before GSI.
7625 Return the gimple_val holding the result. */
7627 tree
7628 gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
7629 tree type, tree a, tree b)
7631 tree ret;
7633 ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
7634 STRIP_NOPS (ret);
7636 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7637 GSI_SAME_STMT);
7640 /* Build a unary operation and gimplify it. Emit code before GSI.
7641 Return the gimple_val holding the result. */
7643 tree
7644 gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
7645 tree a)
7647 tree ret;
7649 ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
7650 STRIP_NOPS (ret);
7652 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
7653 GSI_SAME_STMT);
7658 /* Emit return warnings. */
7660 static unsigned int
7661 execute_warn_function_return (void)
7663 source_location location;
7664 gimple last;
7665 edge e;
7666 edge_iterator ei;
7668 if (!targetm.warn_func_return (cfun->decl))
7669 return 0;
7671 /* If we have a path to EXIT, then we do return. */
7672 if (TREE_THIS_VOLATILE (cfun->decl)
7673 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
7675 location = UNKNOWN_LOCATION;
7676 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7678 last = last_stmt (e->src);
7679 if ((gimple_code (last) == GIMPLE_RETURN
7680 || gimple_call_builtin_p (last, BUILT_IN_RETURN))
7681 && (location = gimple_location (last)) != UNKNOWN_LOCATION)
7682 break;
7684 if (location == UNKNOWN_LOCATION)
7685 location = cfun->function_end_locus;
7686 warning_at (location, 0, "%<noreturn%> function does return");
7689 /* If we see "return;" in some basic block, then we do reach the end
7690 without returning a value. */
7691 else if (warn_return_type
7692 && !TREE_NO_WARNING (cfun->decl)
7693 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
7694 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
7696 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
7698 gimple last = last_stmt (e->src);
7699 if (gimple_code (last) == GIMPLE_RETURN
7700 && gimple_return_retval (last) == NULL
7701 && !gimple_no_warning_p (last))
7703 location = gimple_location (last);
7704 if (location == UNKNOWN_LOCATION)
7705 location = cfun->function_end_locus;
7706 warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
7707 TREE_NO_WARNING (cfun->decl) = 1;
7708 break;
7712 return 0;
7716 /* Given a basic block B which ends with a conditional and has
7717 precisely two successors, determine which of the edges is taken if
7718 the conditional is true and which is taken if the conditional is
7719 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
7721 void
7722 extract_true_false_edges_from_block (basic_block b,
7723 edge *true_edge,
7724 edge *false_edge)
7726 edge e = EDGE_SUCC (b, 0);
7728 if (e->flags & EDGE_TRUE_VALUE)
7730 *true_edge = e;
7731 *false_edge = EDGE_SUCC (b, 1);
7733 else
7735 *false_edge = e;
7736 *true_edge = EDGE_SUCC (b, 1);
7740 struct gimple_opt_pass pass_warn_function_return =
7743 GIMPLE_PASS,
7744 "*warn_function_return", /* name */
7745 NULL, /* gate */
7746 execute_warn_function_return, /* execute */
7747 NULL, /* sub */
7748 NULL, /* next */
7749 0, /* static_pass_number */
7750 TV_NONE, /* tv_id */
7751 PROP_cfg, /* properties_required */
7752 0, /* properties_provided */
7753 0, /* properties_destroyed */
7754 0, /* todo_flags_start */
7755 0 /* todo_flags_finish */
7759 /* Emit noreturn warnings. */
7761 static unsigned int
7762 execute_warn_function_noreturn (void)
7764 if (!TREE_THIS_VOLATILE (current_function_decl)
7765 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0)
7766 warn_function_noreturn (current_function_decl);
7767 return 0;
7770 static bool
7771 gate_warn_function_noreturn (void)
7773 return warn_suggest_attribute_noreturn;
7776 struct gimple_opt_pass pass_warn_function_noreturn =
7779 GIMPLE_PASS,
7780 "*warn_function_noreturn", /* name */
7781 gate_warn_function_noreturn, /* gate */
7782 execute_warn_function_noreturn, /* execute */
7783 NULL, /* sub */
7784 NULL, /* next */
7785 0, /* static_pass_number */
7786 TV_NONE, /* tv_id */
7787 PROP_cfg, /* properties_required */
7788 0, /* properties_provided */
7789 0, /* properties_destroyed */
7790 0, /* todo_flags_start */
7791 0 /* todo_flags_finish */
7796 /* Walk a gimplified function and warn for functions whose return value is
7797 ignored and attribute((warn_unused_result)) is set. This is done before
7798 inlining, so we don't have to worry about that. */
7800 static void
7801 do_warn_unused_result (gimple_seq seq)
7803 tree fdecl, ftype;
7804 gimple_stmt_iterator i;
7806 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
7808 gimple g = gsi_stmt (i);
7810 switch (gimple_code (g))
7812 case GIMPLE_BIND:
7813 do_warn_unused_result (gimple_bind_body (g));
7814 break;
7815 case GIMPLE_TRY:
7816 do_warn_unused_result (gimple_try_eval (g));
7817 do_warn_unused_result (gimple_try_cleanup (g));
7818 break;
7819 case GIMPLE_CATCH:
7820 do_warn_unused_result (gimple_catch_handler (g));
7821 break;
7822 case GIMPLE_EH_FILTER:
7823 do_warn_unused_result (gimple_eh_filter_failure (g));
7824 break;
7826 case GIMPLE_CALL:
7827 if (gimple_call_lhs (g))
7828 break;
7829 if (gimple_call_internal_p (g))
7830 break;
7832 /* This is a naked call, as opposed to a GIMPLE_CALL with an
7833 LHS. All calls whose value is ignored should be
7834 represented like this. Look for the attribute. */
7835 fdecl = gimple_call_fndecl (g);
7836 ftype = gimple_call_fntype (g);
7838 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
7840 location_t loc = gimple_location (g);
7842 if (fdecl)
7843 warning_at (loc, OPT_Wunused_result,
7844 "ignoring return value of %qD, "
7845 "declared with attribute warn_unused_result",
7846 fdecl);
7847 else
7848 warning_at (loc, OPT_Wunused_result,
7849 "ignoring return value of function "
7850 "declared with attribute warn_unused_result");
7852 break;
7854 default:
7855 /* Not a container, not a call, or a call whose value is used. */
7856 break;
7861 static unsigned int
7862 run_warn_unused_result (void)
7864 do_warn_unused_result (gimple_body (current_function_decl));
7865 return 0;
7868 static bool
7869 gate_warn_unused_result (void)
7871 return flag_warn_unused_result;
7874 struct gimple_opt_pass pass_warn_unused_result =
7877 GIMPLE_PASS,
7878 "*warn_unused_result", /* name */
7879 gate_warn_unused_result, /* gate */
7880 run_warn_unused_result, /* execute */
7881 NULL, /* sub */
7882 NULL, /* next */
7883 0, /* static_pass_number */
7884 TV_NONE, /* tv_id */
7885 PROP_gimple_any, /* properties_required */
7886 0, /* properties_provided */
7887 0, /* properties_destroyed */
7888 0, /* todo_flags_start */
7889 0, /* todo_flags_finish */
7894 /* Garbage collection support for edge_def. */
7896 extern void gt_ggc_mx (tree&);
7897 extern void gt_ggc_mx (gimple&);
7898 extern void gt_ggc_mx (rtx&);
7899 extern void gt_ggc_mx (basic_block&);
7901 void
7902 gt_ggc_mx (edge_def *e)
7904 gt_ggc_mx (e->src);
7905 gt_ggc_mx (e->dest);
7906 if (current_ir_type () == IR_GIMPLE)
7907 gt_ggc_mx (e->insns.g);
7908 else
7909 gt_ggc_mx (e->insns.r);
7910 gt_ggc_mx (e->goto_block);
7913 /* PCH support for edge_def. */
7915 extern void gt_pch_nx (tree&);
7916 extern void gt_pch_nx (gimple&);
7917 extern void gt_pch_nx (rtx&);
7918 extern void gt_pch_nx (basic_block&);
7920 void
7921 gt_pch_nx (edge_def *e)
7923 gt_pch_nx (e->src);
7924 gt_pch_nx (e->dest);
7925 if (current_ir_type () == IR_GIMPLE)
7926 gt_pch_nx (e->insns.g);
7927 else
7928 gt_pch_nx (e->insns.r);
7929 gt_pch_nx (e->goto_block);
7932 void
7933 gt_pch_nx (edge_def *e, gt_pointer_operator op, void *cookie)
7935 op (&(e->src), cookie);
7936 op (&(e->dest), cookie);
7937 if (current_ir_type () == IR_GIMPLE)
7938 op (&(e->insns.g), cookie);
7939 else
7940 op (&(e->insns.r), cookie);
7941 op (&(e->goto_block), cookie);