* Makefile.in (LIBGCOV_INTERFACE): Move _gcov_dump ...
[official-gcc.git] / gcc / tree-cfg.c
blobcfa65273efbe245b1a10c12454c9150fdd9e40bd
1 /* Control flow functions for trees.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "hash-table.h"
25 #include "hash-map.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "trans-mem.h"
29 #include "stor-layout.h"
30 #include "print-tree.h"
31 #include "tm_p.h"
32 #include "basic-block.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "gimple-pretty-print.h"
36 #include "pointer-set.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-fold.h"
40 #include "tree-eh.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimple-iterator.h"
45 #include "gimplify-me.h"
46 #include "gimple-walk.h"
47 #include "gimple-ssa.h"
48 #include "cgraph.h"
49 #include "tree-cfg.h"
50 #include "tree-phinodes.h"
51 #include "ssa-iterators.h"
52 #include "stringpool.h"
53 #include "tree-ssanames.h"
54 #include "tree-ssa-loop-manip.h"
55 #include "tree-ssa-loop-niter.h"
56 #include "tree-into-ssa.h"
57 #include "expr.h"
58 #include "tree-dfa.h"
59 #include "tree-ssa.h"
60 #include "tree-dump.h"
61 #include "tree-pass.h"
62 #include "diagnostic-core.h"
63 #include "except.h"
64 #include "cfgloop.h"
65 #include "tree-ssa-propagate.h"
66 #include "value-prof.h"
67 #include "tree-inline.h"
68 #include "target.h"
69 #include "tree-ssa-live.h"
70 #include "omp-low.h"
71 #include "tree-cfgcleanup.h"
72 #include "wide-int.h"
73 #include "wide-int-print.h"
75 /* This file contains functions for building the Control Flow Graph (CFG)
76 for a function tree. */
78 /* Local declarations. */
80 /* Initial capacity for the basic block array. */
81 static const int initial_cfg_capacity = 20;
83 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
84 which use a particular edge. The CASE_LABEL_EXPRs are chained together
85 via their CASE_CHAIN field, which we clear after we're done with the
86 hash table to prevent problems with duplication of GIMPLE_SWITCHes.
88 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
89 update the case vector in response to edge redirections.
91 Right now this table is set up and torn down at key points in the
92 compilation process. It would be nice if we could make the table
93 more persistent. The key is getting notification of changes to
94 the CFG (particularly edge removal, creation and redirection). */
96 static hash_map<edge, tree> *edge_to_cases;
98 /* If we record edge_to_cases, this bitmap will hold indexes
99 of basic blocks that end in a GIMPLE_SWITCH which we touched
100 due to edge manipulations. */
102 static bitmap touched_switch_bbs;
104 /* CFG statistics. */
105 struct cfg_stats_d
107 long num_merged_labels;
110 static struct cfg_stats_d cfg_stats;
112 /* Hash table to store last discriminator assigned for each locus. */
113 struct locus_discrim_map
115 location_t locus;
116 int discriminator;
119 /* Hashtable helpers. */
121 struct locus_discrim_hasher : typed_free_remove <locus_discrim_map>
123 typedef locus_discrim_map value_type;
124 typedef locus_discrim_map compare_type;
125 static inline hashval_t hash (const value_type *);
126 static inline bool equal (const value_type *, const compare_type *);
129 /* Trivial hash function for a location_t. ITEM is a pointer to
130 a hash table entry that maps a location_t to a discriminator. */
132 inline hashval_t
133 locus_discrim_hasher::hash (const value_type *item)
135 return LOCATION_LINE (item->locus);
138 /* Equality function for the locus-to-discriminator map. A and B
139 point to the two hash table entries to compare. */
141 inline bool
142 locus_discrim_hasher::equal (const value_type *a, const compare_type *b)
144 return LOCATION_LINE (a->locus) == LOCATION_LINE (b->locus);
147 static hash_table<locus_discrim_hasher> *discriminator_per_locus;
149 /* Basic blocks and flowgraphs. */
150 static void make_blocks (gimple_seq);
152 /* Edges. */
153 static void make_edges (void);
154 static void assign_discriminators (void);
155 static void make_cond_expr_edges (basic_block);
156 static void make_gimple_switch_edges (basic_block);
157 static bool make_goto_expr_edges (basic_block);
158 static void make_gimple_asm_edges (basic_block);
159 static edge gimple_redirect_edge_and_branch (edge, basic_block);
160 static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
162 /* Various helpers. */
163 static inline bool stmt_starts_bb_p (gimple, gimple);
164 static int gimple_verify_flow_info (void);
165 static void gimple_make_forwarder_block (edge);
166 static gimple first_non_label_stmt (basic_block);
167 static bool verify_gimple_transaction (gimple);
169 /* Flowgraph optimization and cleanup. */
170 static void gimple_merge_blocks (basic_block, basic_block);
171 static bool gimple_can_merge_blocks_p (basic_block, basic_block);
172 static void remove_bb (basic_block);
173 static edge find_taken_edge_computed_goto (basic_block, tree);
174 static edge find_taken_edge_cond_expr (basic_block, tree);
175 static edge find_taken_edge_switch_expr (basic_block, tree);
176 static tree find_case_label_for_value (gimple, tree);
178 void
179 init_empty_tree_cfg_for_function (struct function *fn)
181 /* Initialize the basic block array. */
182 init_flow (fn);
183 profile_status_for_fn (fn) = PROFILE_ABSENT;
184 n_basic_blocks_for_fn (fn) = NUM_FIXED_BLOCKS;
185 last_basic_block_for_fn (fn) = NUM_FIXED_BLOCKS;
186 vec_alloc (basic_block_info_for_fn (fn), initial_cfg_capacity);
187 vec_safe_grow_cleared (basic_block_info_for_fn (fn),
188 initial_cfg_capacity);
190 /* Build a mapping of labels to their associated blocks. */
191 vec_alloc (label_to_block_map_for_fn (fn), initial_cfg_capacity);
192 vec_safe_grow_cleared (label_to_block_map_for_fn (fn),
193 initial_cfg_capacity);
195 SET_BASIC_BLOCK_FOR_FN (fn, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (fn));
196 SET_BASIC_BLOCK_FOR_FN (fn, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (fn));
198 ENTRY_BLOCK_PTR_FOR_FN (fn)->next_bb
199 = EXIT_BLOCK_PTR_FOR_FN (fn);
200 EXIT_BLOCK_PTR_FOR_FN (fn)->prev_bb
201 = ENTRY_BLOCK_PTR_FOR_FN (fn);
204 void
205 init_empty_tree_cfg (void)
207 init_empty_tree_cfg_for_function (cfun);
210 /*---------------------------------------------------------------------------
211 Create basic blocks
212 ---------------------------------------------------------------------------*/
214 /* Entry point to the CFG builder for trees. SEQ is the sequence of
215 statements to be added to the flowgraph. */
217 static void
218 build_gimple_cfg (gimple_seq seq)
220 /* Register specific gimple functions. */
221 gimple_register_cfg_hooks ();
223 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
225 init_empty_tree_cfg ();
227 make_blocks (seq);
229 /* Make sure there is always at least one block, even if it's empty. */
230 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
231 create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
233 /* Adjust the size of the array. */
234 if (basic_block_info_for_fn (cfun)->length ()
235 < (size_t) n_basic_blocks_for_fn (cfun))
236 vec_safe_grow_cleared (basic_block_info_for_fn (cfun),
237 n_basic_blocks_for_fn (cfun));
239 /* To speed up statement iterator walks, we first purge dead labels. */
240 cleanup_dead_labels ();
242 /* Group case nodes to reduce the number of edges.
243 We do this after cleaning up dead labels because otherwise we miss
244 a lot of obvious case merging opportunities. */
245 group_case_labels ();
247 /* Create the edges of the flowgraph. */
248 discriminator_per_locus = new hash_table<locus_discrim_hasher> (13);
249 make_edges ();
250 assign_discriminators ();
251 cleanup_dead_labels ();
252 delete discriminator_per_locus;
253 discriminator_per_locus = NULL;
257 /* Look for ANNOTATE calls with loop annotation kind; if found, remove
258 them and propagate the information to the loop. We assume that the
259 annotations come immediately before the condition of the loop. */
261 static void
262 replace_loop_annotate ()
264 struct loop *loop;
265 basic_block bb;
266 gimple_stmt_iterator gsi;
267 gimple stmt;
269 FOR_EACH_LOOP (loop, 0)
271 gsi = gsi_last_bb (loop->header);
272 stmt = gsi_stmt (gsi);
273 if (!(stmt && gimple_code (stmt) == GIMPLE_COND))
274 continue;
275 for (gsi_prev_nondebug (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
277 stmt = gsi_stmt (gsi);
278 if (gimple_code (stmt) != GIMPLE_CALL)
279 break;
280 if (!gimple_call_internal_p (stmt)
281 || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
282 break;
283 switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
285 case annot_expr_ivdep_kind:
286 loop->safelen = INT_MAX;
287 break;
288 case annot_expr_no_vector_kind:
289 loop->dont_vectorize = true;
290 break;
291 case annot_expr_vector_kind:
292 loop->force_vectorize = true;
293 cfun->has_force_vectorize_loops = true;
294 break;
295 default:
296 gcc_unreachable ();
298 stmt = gimple_build_assign (gimple_call_lhs (stmt),
299 gimple_call_arg (stmt, 0));
300 gsi_replace (&gsi, stmt, true);
304 /* Remove IFN_ANNOTATE. Safeguard for the case loop->latch == NULL. */
305 FOR_EACH_BB_FN (bb, cfun)
307 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
309 stmt = gsi_stmt (gsi);
310 if (gimple_code (stmt) != GIMPLE_CALL)
311 break;
312 if (!gimple_call_internal_p (stmt)
313 || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
314 break;
315 switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
317 case annot_expr_ivdep_kind:
318 case annot_expr_no_vector_kind:
319 case annot_expr_vector_kind:
320 break;
321 default:
322 gcc_unreachable ();
324 warning_at (gimple_location (stmt), 0, "ignoring loop annotation");
325 stmt = gimple_build_assign (gimple_call_lhs (stmt),
326 gimple_call_arg (stmt, 0));
327 gsi_replace (&gsi, stmt, true);
333 static unsigned int
334 execute_build_cfg (void)
336 gimple_seq body = gimple_body (current_function_decl);
338 build_gimple_cfg (body);
339 gimple_set_body (current_function_decl, NULL);
340 if (dump_file && (dump_flags & TDF_DETAILS))
342 fprintf (dump_file, "Scope blocks:\n");
343 dump_scope_blocks (dump_file, dump_flags);
345 cleanup_tree_cfg ();
346 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
347 replace_loop_annotate ();
348 return 0;
351 namespace {
353 const pass_data pass_data_build_cfg =
355 GIMPLE_PASS, /* type */
356 "cfg", /* name */
357 OPTGROUP_NONE, /* optinfo_flags */
358 TV_TREE_CFG, /* tv_id */
359 PROP_gimple_leh, /* properties_required */
360 ( PROP_cfg | PROP_loops ), /* properties_provided */
361 0, /* properties_destroyed */
362 0, /* todo_flags_start */
363 0, /* todo_flags_finish */
366 class pass_build_cfg : public gimple_opt_pass
368 public:
369 pass_build_cfg (gcc::context *ctxt)
370 : gimple_opt_pass (pass_data_build_cfg, ctxt)
373 /* opt_pass methods: */
374 virtual unsigned int execute (function *) { return execute_build_cfg (); }
376 }; // class pass_build_cfg
378 } // anon namespace
380 gimple_opt_pass *
381 make_pass_build_cfg (gcc::context *ctxt)
383 return new pass_build_cfg (ctxt);
387 /* Return true if T is a computed goto. */
389 bool
390 computed_goto_p (gimple t)
392 return (gimple_code (t) == GIMPLE_GOTO
393 && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
396 /* Returns true for edge E where e->src ends with a GIMPLE_COND and
397 the other edge points to a bb with just __builtin_unreachable ().
398 I.e. return true for C->M edge in:
399 <bb C>:
401 if (something)
402 goto <bb N>;
403 else
404 goto <bb M>;
405 <bb N>:
406 __builtin_unreachable ();
407 <bb M>: */
409 bool
410 assert_unreachable_fallthru_edge_p (edge e)
412 basic_block pred_bb = e->src;
413 gimple last = last_stmt (pred_bb);
414 if (last && gimple_code (last) == GIMPLE_COND)
416 basic_block other_bb = EDGE_SUCC (pred_bb, 0)->dest;
417 if (other_bb == e->dest)
418 other_bb = EDGE_SUCC (pred_bb, 1)->dest;
419 if (EDGE_COUNT (other_bb->succs) == 0)
421 gimple_stmt_iterator gsi = gsi_after_labels (other_bb);
422 gimple stmt;
424 if (gsi_end_p (gsi))
425 return false;
426 stmt = gsi_stmt (gsi);
427 while (is_gimple_debug (stmt) || gimple_clobber_p (stmt))
429 gsi_next (&gsi);
430 if (gsi_end_p (gsi))
431 return false;
432 stmt = gsi_stmt (gsi);
434 return gimple_call_builtin_p (stmt, BUILT_IN_UNREACHABLE);
437 return false;
441 /* Build a flowgraph for the sequence of stmts SEQ. */
443 static void
444 make_blocks (gimple_seq seq)
446 gimple_stmt_iterator i = gsi_start (seq);
447 gimple stmt = NULL;
448 bool start_new_block = true;
449 bool first_stmt_of_seq = true;
450 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
452 while (!gsi_end_p (i))
454 gimple prev_stmt;
456 prev_stmt = stmt;
457 stmt = gsi_stmt (i);
459 /* If the statement starts a new basic block or if we have determined
460 in a previous pass that we need to create a new block for STMT, do
461 so now. */
462 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
464 if (!first_stmt_of_seq)
465 gsi_split_seq_before (&i, &seq);
466 bb = create_basic_block (seq, NULL, bb);
467 start_new_block = false;
470 /* Now add STMT to BB and create the subgraphs for special statement
471 codes. */
472 gimple_set_bb (stmt, bb);
474 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
475 next iteration. */
476 if (stmt_ends_bb_p (stmt))
478 /* If the stmt can make abnormal goto use a new temporary
479 for the assignment to the LHS. This makes sure the old value
480 of the LHS is available on the abnormal edge. Otherwise
481 we will end up with overlapping life-ranges for abnormal
482 SSA names. */
483 if (gimple_has_lhs (stmt)
484 && stmt_can_make_abnormal_goto (stmt)
485 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
487 tree lhs = gimple_get_lhs (stmt);
488 tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
489 gimple s = gimple_build_assign (lhs, tmp);
490 gimple_set_location (s, gimple_location (stmt));
491 gimple_set_block (s, gimple_block (stmt));
492 gimple_set_lhs (stmt, tmp);
493 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
494 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
495 DECL_GIMPLE_REG_P (tmp) = 1;
496 gsi_insert_after (&i, s, GSI_SAME_STMT);
498 start_new_block = true;
501 gsi_next (&i);
502 first_stmt_of_seq = false;
507 /* Create and return a new empty basic block after bb AFTER. */
509 static basic_block
510 create_bb (void *h, void *e, basic_block after)
512 basic_block bb;
514 gcc_assert (!e);
516 /* Create and initialize a new basic block. Since alloc_block uses
517 GC allocation that clears memory to allocate a basic block, we do
518 not have to clear the newly allocated basic block here. */
519 bb = alloc_block ();
521 bb->index = last_basic_block_for_fn (cfun);
522 bb->flags = BB_NEW;
523 set_bb_seq (bb, h ? (gimple_seq) h : NULL);
525 /* Add the new block to the linked list of blocks. */
526 link_block (bb, after);
528 /* Grow the basic block array if needed. */
529 if ((size_t) last_basic_block_for_fn (cfun)
530 == basic_block_info_for_fn (cfun)->length ())
532 size_t new_size =
533 (last_basic_block_for_fn (cfun)
534 + (last_basic_block_for_fn (cfun) + 3) / 4);
535 vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size);
538 /* Add the newly created block to the array. */
539 SET_BASIC_BLOCK_FOR_FN (cfun, last_basic_block_for_fn (cfun), bb);
541 n_basic_blocks_for_fn (cfun)++;
542 last_basic_block_for_fn (cfun)++;
544 return bb;
548 /*---------------------------------------------------------------------------
549 Edge creation
550 ---------------------------------------------------------------------------*/
552 /* Fold COND_EXPR_COND of each COND_EXPR. */
554 void
555 fold_cond_expr_cond (void)
557 basic_block bb;
559 FOR_EACH_BB_FN (bb, cfun)
561 gimple stmt = last_stmt (bb);
563 if (stmt && gimple_code (stmt) == GIMPLE_COND)
565 location_t loc = gimple_location (stmt);
566 tree cond;
567 bool zerop, onep;
569 fold_defer_overflow_warnings ();
570 cond = fold_binary_loc (loc, gimple_cond_code (stmt), boolean_type_node,
571 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
572 if (cond)
574 zerop = integer_zerop (cond);
575 onep = integer_onep (cond);
577 else
578 zerop = onep = false;
580 fold_undefer_overflow_warnings (zerop || onep,
581 stmt,
582 WARN_STRICT_OVERFLOW_CONDITIONAL);
583 if (zerop)
584 gimple_cond_make_false (stmt);
585 else if (onep)
586 gimple_cond_make_true (stmt);
591 /* If basic block BB has an abnormal edge to a basic block
592 containing IFN_ABNORMAL_DISPATCHER internal call, return
593 that the dispatcher's basic block, otherwise return NULL. */
595 basic_block
596 get_abnormal_succ_dispatcher (basic_block bb)
598 edge e;
599 edge_iterator ei;
601 FOR_EACH_EDGE (e, ei, bb->succs)
602 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
604 gimple_stmt_iterator gsi
605 = gsi_start_nondebug_after_labels_bb (e->dest);
606 gimple g = gsi_stmt (gsi);
607 if (g
608 && is_gimple_call (g)
609 && gimple_call_internal_p (g)
610 && gimple_call_internal_fn (g) == IFN_ABNORMAL_DISPATCHER)
611 return e->dest;
613 return NULL;
616 /* Helper function for make_edges. Create a basic block with
617 with ABNORMAL_DISPATCHER internal call in it if needed, and
618 create abnormal edges from BBS to it and from it to FOR_BB
619 if COMPUTED_GOTO is false, otherwise factor the computed gotos. */
621 static void
622 handle_abnormal_edges (basic_block *dispatcher_bbs,
623 basic_block for_bb, int *bb_to_omp_idx,
624 auto_vec<basic_block> *bbs, bool computed_goto)
626 basic_block *dispatcher = dispatcher_bbs + (computed_goto ? 1 : 0);
627 unsigned int idx = 0;
628 basic_block bb;
629 bool inner = false;
631 if (bb_to_omp_idx)
633 dispatcher = dispatcher_bbs + 2 * bb_to_omp_idx[for_bb->index];
634 if (bb_to_omp_idx[for_bb->index] != 0)
635 inner = true;
638 /* If the dispatcher has been created already, then there are basic
639 blocks with abnormal edges to it, so just make a new edge to
640 for_bb. */
641 if (*dispatcher == NULL)
643 /* Check if there are any basic blocks that need to have
644 abnormal edges to this dispatcher. If there are none, return
645 early. */
646 if (bb_to_omp_idx == NULL)
648 if (bbs->is_empty ())
649 return;
651 else
653 FOR_EACH_VEC_ELT (*bbs, idx, bb)
654 if (bb_to_omp_idx[bb->index] == bb_to_omp_idx[for_bb->index])
655 break;
656 if (bb == NULL)
657 return;
660 /* Create the dispatcher bb. */
661 *dispatcher = create_basic_block (NULL, NULL, for_bb);
662 if (computed_goto)
664 /* Factor computed gotos into a common computed goto site. Also
665 record the location of that site so that we can un-factor the
666 gotos after we have converted back to normal form. */
667 gimple_stmt_iterator gsi = gsi_start_bb (*dispatcher);
669 /* Create the destination of the factored goto. Each original
670 computed goto will put its desired destination into this
671 variable and jump to the label we create immediately below. */
672 tree var = create_tmp_var (ptr_type_node, "gotovar");
674 /* Build a label for the new block which will contain the
675 factored computed goto. */
676 tree factored_label_decl
677 = create_artificial_label (UNKNOWN_LOCATION);
678 gimple factored_computed_goto_label
679 = gimple_build_label (factored_label_decl);
680 gsi_insert_after (&gsi, factored_computed_goto_label, GSI_NEW_STMT);
682 /* Build our new computed goto. */
683 gimple factored_computed_goto = gimple_build_goto (var);
684 gsi_insert_after (&gsi, factored_computed_goto, GSI_NEW_STMT);
686 FOR_EACH_VEC_ELT (*bbs, idx, bb)
688 if (bb_to_omp_idx
689 && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
690 continue;
692 gsi = gsi_last_bb (bb);
693 gimple last = gsi_stmt (gsi);
695 gcc_assert (computed_goto_p (last));
697 /* Copy the original computed goto's destination into VAR. */
698 gimple assignment
699 = gimple_build_assign (var, gimple_goto_dest (last));
700 gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
702 edge e = make_edge (bb, *dispatcher, EDGE_FALLTHRU);
703 e->goto_locus = gimple_location (last);
704 gsi_remove (&gsi, true);
707 else
709 tree arg = inner ? boolean_true_node : boolean_false_node;
710 gimple g = gimple_build_call_internal (IFN_ABNORMAL_DISPATCHER,
711 1, arg);
712 gimple_stmt_iterator gsi = gsi_after_labels (*dispatcher);
713 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
715 /* Create predecessor edges of the dispatcher. */
716 FOR_EACH_VEC_ELT (*bbs, idx, bb)
718 if (bb_to_omp_idx
719 && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
720 continue;
721 make_edge (bb, *dispatcher, EDGE_ABNORMAL);
726 make_edge (*dispatcher, for_bb, EDGE_ABNORMAL);
729 /* Join all the blocks in the flowgraph. */
731 static void
732 make_edges (void)
734 basic_block bb;
735 struct omp_region *cur_region = NULL;
736 auto_vec<basic_block> ab_edge_goto;
737 auto_vec<basic_block> ab_edge_call;
738 int *bb_to_omp_idx = NULL;
739 int cur_omp_region_idx = 0;
741 /* Create an edge from entry to the first block with executable
742 statements in it. */
743 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun),
744 BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS),
745 EDGE_FALLTHRU);
747 /* Traverse the basic block array placing edges. */
748 FOR_EACH_BB_FN (bb, cfun)
750 gimple last = last_stmt (bb);
751 bool fallthru;
753 if (bb_to_omp_idx)
754 bb_to_omp_idx[bb->index] = cur_omp_region_idx;
756 if (last)
758 enum gimple_code code = gimple_code (last);
759 switch (code)
761 case GIMPLE_GOTO:
762 if (make_goto_expr_edges (bb))
763 ab_edge_goto.safe_push (bb);
764 fallthru = false;
765 break;
766 case GIMPLE_RETURN:
768 edge e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
769 e->goto_locus = gimple_location (last);
770 fallthru = false;
772 break;
773 case GIMPLE_COND:
774 make_cond_expr_edges (bb);
775 fallthru = false;
776 break;
777 case GIMPLE_SWITCH:
778 make_gimple_switch_edges (bb);
779 fallthru = false;
780 break;
781 case GIMPLE_RESX:
782 make_eh_edges (last);
783 fallthru = false;
784 break;
785 case GIMPLE_EH_DISPATCH:
786 fallthru = make_eh_dispatch_edges (last);
787 break;
789 case GIMPLE_CALL:
790 /* If this function receives a nonlocal goto, then we need to
791 make edges from this call site to all the nonlocal goto
792 handlers. */
793 if (stmt_can_make_abnormal_goto (last))
794 ab_edge_call.safe_push (bb);
796 /* If this statement has reachable exception handlers, then
797 create abnormal edges to them. */
798 make_eh_edges (last);
800 /* BUILTIN_RETURN is really a return statement. */
801 if (gimple_call_builtin_p (last, BUILT_IN_RETURN))
803 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
804 fallthru = false;
806 /* Some calls are known not to return. */
807 else
808 fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
809 break;
811 case GIMPLE_ASSIGN:
812 /* A GIMPLE_ASSIGN may throw internally and thus be considered
813 control-altering. */
814 if (is_ctrl_altering_stmt (last))
815 make_eh_edges (last);
816 fallthru = true;
817 break;
819 case GIMPLE_ASM:
820 make_gimple_asm_edges (bb);
821 fallthru = true;
822 break;
824 CASE_GIMPLE_OMP:
825 fallthru = make_gimple_omp_edges (bb, &cur_region,
826 &cur_omp_region_idx);
827 if (cur_region && bb_to_omp_idx == NULL)
828 bb_to_omp_idx = XCNEWVEC (int, n_basic_blocks_for_fn (cfun));
829 break;
831 case GIMPLE_TRANSACTION:
833 tree abort_label = gimple_transaction_label (last);
834 if (abort_label)
835 make_edge (bb, label_to_block (abort_label), EDGE_TM_ABORT);
836 fallthru = true;
838 break;
840 default:
841 gcc_assert (!stmt_ends_bb_p (last));
842 fallthru = true;
845 else
846 fallthru = true;
848 if (fallthru)
849 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
852 /* Computed gotos are hell to deal with, especially if there are
853 lots of them with a large number of destinations. So we factor
854 them to a common computed goto location before we build the
855 edge list. After we convert back to normal form, we will un-factor
856 the computed gotos since factoring introduces an unwanted jump.
857 For non-local gotos and abnormal edges from calls to calls that return
858 twice or forced labels, factor the abnormal edges too, by having all
859 abnormal edges from the calls go to a common artificial basic block
860 with ABNORMAL_DISPATCHER internal call and abnormal edges from that
861 basic block to all forced labels and calls returning twice.
862 We do this per-OpenMP structured block, because those regions
863 are guaranteed to be single entry single exit by the standard,
864 so it is not allowed to enter or exit such regions abnormally this way,
865 thus all computed gotos, non-local gotos and setjmp/longjmp calls
866 must not transfer control across SESE region boundaries. */
867 if (!ab_edge_goto.is_empty () || !ab_edge_call.is_empty ())
869 gimple_stmt_iterator gsi;
870 basic_block dispatcher_bb_array[2] = { NULL, NULL };
871 basic_block *dispatcher_bbs = dispatcher_bb_array;
872 int count = n_basic_blocks_for_fn (cfun);
874 if (bb_to_omp_idx)
875 dispatcher_bbs = XCNEWVEC (basic_block, 2 * count);
877 FOR_EACH_BB_FN (bb, cfun)
879 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
881 gimple label_stmt = gsi_stmt (gsi);
882 tree target;
884 if (gimple_code (label_stmt) != GIMPLE_LABEL)
885 break;
887 target = gimple_label_label (label_stmt);
889 /* Make an edge to every label block that has been marked as a
890 potential target for a computed goto or a non-local goto. */
891 if (FORCED_LABEL (target))
892 handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
893 &ab_edge_goto, true);
894 if (DECL_NONLOCAL (target))
896 handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
897 &ab_edge_call, false);
898 break;
902 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
903 gsi_next_nondebug (&gsi);
904 if (!gsi_end_p (gsi))
906 /* Make an edge to every setjmp-like call. */
907 gimple call_stmt = gsi_stmt (gsi);
908 if (is_gimple_call (call_stmt)
909 && ((gimple_call_flags (call_stmt) & ECF_RETURNS_TWICE)
910 || gimple_call_builtin_p (call_stmt,
911 BUILT_IN_SETJMP_RECEIVER)))
912 handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
913 &ab_edge_call, false);
917 if (bb_to_omp_idx)
918 XDELETE (dispatcher_bbs);
921 XDELETE (bb_to_omp_idx);
923 free_omp_regions ();
925 /* Fold COND_EXPR_COND of each COND_EXPR. */
926 fold_cond_expr_cond ();
929 /* Find the next available discriminator value for LOCUS. The
930 discriminator distinguishes among several basic blocks that
931 share a common locus, allowing for more accurate sample-based
932 profiling. */
934 static int
935 next_discriminator_for_locus (location_t locus)
937 struct locus_discrim_map item;
938 struct locus_discrim_map **slot;
940 item.locus = locus;
941 item.discriminator = 0;
942 slot = discriminator_per_locus->find_slot_with_hash (
943 &item, LOCATION_LINE (locus), INSERT);
944 gcc_assert (slot);
945 if (*slot == HTAB_EMPTY_ENTRY)
947 *slot = XNEW (struct locus_discrim_map);
948 gcc_assert (*slot);
949 (*slot)->locus = locus;
950 (*slot)->discriminator = 0;
952 (*slot)->discriminator++;
953 return (*slot)->discriminator;
956 /* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line. */
958 static bool
959 same_line_p (location_t locus1, location_t locus2)
961 expanded_location from, to;
963 if (locus1 == locus2)
964 return true;
966 from = expand_location (locus1);
967 to = expand_location (locus2);
969 if (from.line != to.line)
970 return false;
971 if (from.file == to.file)
972 return true;
973 return (from.file != NULL
974 && to.file != NULL
975 && filename_cmp (from.file, to.file) == 0);
978 /* Assign discriminators to each basic block. */
980 static void
981 assign_discriminators (void)
983 basic_block bb;
985 FOR_EACH_BB_FN (bb, cfun)
987 edge e;
988 edge_iterator ei;
989 gimple last = last_stmt (bb);
990 location_t locus = last ? gimple_location (last) : UNKNOWN_LOCATION;
992 if (locus == UNKNOWN_LOCATION)
993 continue;
995 FOR_EACH_EDGE (e, ei, bb->succs)
997 gimple first = first_non_label_stmt (e->dest);
998 gimple last = last_stmt (e->dest);
999 if ((first && same_line_p (locus, gimple_location (first)))
1000 || (last && same_line_p (locus, gimple_location (last))))
1002 if (e->dest->discriminator != 0 && bb->discriminator == 0)
1003 bb->discriminator = next_discriminator_for_locus (locus);
1004 else
1005 e->dest->discriminator = next_discriminator_for_locus (locus);
1011 /* Create the edges for a GIMPLE_COND starting at block BB. */
1013 static void
1014 make_cond_expr_edges (basic_block bb)
1016 gimple entry = last_stmt (bb);
1017 gimple then_stmt, else_stmt;
1018 basic_block then_bb, else_bb;
1019 tree then_label, else_label;
1020 edge e;
1022 gcc_assert (entry);
1023 gcc_assert (gimple_code (entry) == GIMPLE_COND);
1025 /* Entry basic blocks for each component. */
1026 then_label = gimple_cond_true_label (entry);
1027 else_label = gimple_cond_false_label (entry);
1028 then_bb = label_to_block (then_label);
1029 else_bb = label_to_block (else_label);
1030 then_stmt = first_stmt (then_bb);
1031 else_stmt = first_stmt (else_bb);
1033 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1034 e->goto_locus = gimple_location (then_stmt);
1035 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1036 if (e)
1037 e->goto_locus = gimple_location (else_stmt);
1039 /* We do not need the labels anymore. */
1040 gimple_cond_set_true_label (entry, NULL_TREE);
1041 gimple_cond_set_false_label (entry, NULL_TREE);
1045 /* Called for each element in the hash table (P) as we delete the
1046 edge to cases hash table.
1048 Clear all the TREE_CHAINs to prevent problems with copying of
1049 SWITCH_EXPRs and structure sharing rules, then free the hash table
1050 element. */
1052 bool
1053 edge_to_cases_cleanup (edge const &, tree const &value, void *)
1055 tree t, next;
1057 for (t = value; t; t = next)
1059 next = CASE_CHAIN (t);
1060 CASE_CHAIN (t) = NULL;
1063 return true;
1066 /* Start recording information mapping edges to case labels. */
1068 void
1069 start_recording_case_labels (void)
1071 gcc_assert (edge_to_cases == NULL);
1072 edge_to_cases = new hash_map<edge, tree>;
1073 touched_switch_bbs = BITMAP_ALLOC (NULL);
1076 /* Return nonzero if we are recording information for case labels. */
1078 static bool
1079 recording_case_labels_p (void)
1081 return (edge_to_cases != NULL);
1084 /* Stop recording information mapping edges to case labels and
1085 remove any information we have recorded. */
1086 void
1087 end_recording_case_labels (void)
1089 bitmap_iterator bi;
1090 unsigned i;
1091 edge_to_cases->traverse<void *, edge_to_cases_cleanup> (NULL);
1092 delete edge_to_cases;
1093 edge_to_cases = NULL;
1094 EXECUTE_IF_SET_IN_BITMAP (touched_switch_bbs, 0, i, bi)
1096 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1097 if (bb)
1099 gimple stmt = last_stmt (bb);
1100 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1101 group_case_labels_stmt (stmt);
1104 BITMAP_FREE (touched_switch_bbs);
1107 /* If we are inside a {start,end}_recording_cases block, then return
1108 a chain of CASE_LABEL_EXPRs from T which reference E.
1110 Otherwise return NULL. */
1112 static tree
1113 get_cases_for_edge (edge e, gimple t)
1115 tree *slot;
1116 size_t i, n;
1118 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
1119 chains available. Return NULL so the caller can detect this case. */
1120 if (!recording_case_labels_p ())
1121 return NULL;
1123 slot = edge_to_cases->get (e);
1124 if (slot)
1125 return *slot;
1127 /* If we did not find E in the hash table, then this must be the first
1128 time we have been queried for information about E & T. Add all the
1129 elements from T to the hash table then perform the query again. */
1131 n = gimple_switch_num_labels (t);
1132 for (i = 0; i < n; i++)
1134 tree elt = gimple_switch_label (t, i);
1135 tree lab = CASE_LABEL (elt);
1136 basic_block label_bb = label_to_block (lab);
1137 edge this_edge = find_edge (e->src, label_bb);
1139 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
1140 a new chain. */
1141 tree &s = edge_to_cases->get_or_insert (this_edge);
1142 CASE_CHAIN (elt) = s;
1143 s = elt;
1146 return *edge_to_cases->get (e);
1149 /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
1151 static void
1152 make_gimple_switch_edges (basic_block bb)
1154 gimple entry = last_stmt (bb);
1155 size_t i, n;
1157 n = gimple_switch_num_labels (entry);
1159 for (i = 0; i < n; ++i)
1161 tree lab = CASE_LABEL (gimple_switch_label (entry, i));
1162 basic_block label_bb = label_to_block (lab);
1163 make_edge (bb, label_bb, 0);
1168 /* Return the basic block holding label DEST. */
1170 basic_block
1171 label_to_block_fn (struct function *ifun, tree dest)
1173 int uid = LABEL_DECL_UID (dest);
1175 /* We would die hard when faced by an undefined label. Emit a label to
1176 the very first basic block. This will hopefully make even the dataflow
1177 and undefined variable warnings quite right. */
1178 if (seen_error () && uid < 0)
1180 gimple_stmt_iterator gsi =
1181 gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS));
1182 gimple stmt;
1184 stmt = gimple_build_label (dest);
1185 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1186 uid = LABEL_DECL_UID (dest);
1188 if (vec_safe_length (ifun->cfg->x_label_to_block_map) <= (unsigned int) uid)
1189 return NULL;
1190 return (*ifun->cfg->x_label_to_block_map)[uid];
1193 /* Create edges for a goto statement at block BB. Returns true
1194 if abnormal edges should be created. */
1196 static bool
1197 make_goto_expr_edges (basic_block bb)
1199 gimple_stmt_iterator last = gsi_last_bb (bb);
1200 gimple goto_t = gsi_stmt (last);
1202 /* A simple GOTO creates normal edges. */
1203 if (simple_goto_p (goto_t))
1205 tree dest = gimple_goto_dest (goto_t);
1206 basic_block label_bb = label_to_block (dest);
1207 edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
1208 e->goto_locus = gimple_location (goto_t);
1209 gsi_remove (&last, true);
1210 return false;
1213 /* A computed GOTO creates abnormal edges. */
1214 return true;
1217 /* Create edges for an asm statement with labels at block BB. */
1219 static void
1220 make_gimple_asm_edges (basic_block bb)
1222 gimple stmt = last_stmt (bb);
1223 int i, n = gimple_asm_nlabels (stmt);
1225 for (i = 0; i < n; ++i)
1227 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
1228 basic_block label_bb = label_to_block (label);
1229 make_edge (bb, label_bb, 0);
1233 /*---------------------------------------------------------------------------
1234 Flowgraph analysis
1235 ---------------------------------------------------------------------------*/
1237 /* Cleanup useless labels in basic blocks. This is something we wish
1238 to do early because it allows us to group case labels before creating
1239 the edges for the CFG, and it speeds up block statement iterators in
1240 all passes later on.
1241 We rerun this pass after CFG is created, to get rid of the labels that
1242 are no longer referenced. After then we do not run it any more, since
1243 (almost) no new labels should be created. */
1245 /* A map from basic block index to the leading label of that block. */
1246 static struct label_record
1248 /* The label. */
1249 tree label;
1251 /* True if the label is referenced from somewhere. */
1252 bool used;
1253 } *label_for_bb;
1255 /* Given LABEL return the first label in the same basic block. */
1257 static tree
1258 main_block_label (tree label)
1260 basic_block bb = label_to_block (label);
1261 tree main_label = label_for_bb[bb->index].label;
1263 /* label_to_block possibly inserted undefined label into the chain. */
1264 if (!main_label)
1266 label_for_bb[bb->index].label = label;
1267 main_label = label;
1270 label_for_bb[bb->index].used = true;
1271 return main_label;
1274 /* Clean up redundant labels within the exception tree. */
1276 static void
1277 cleanup_dead_labels_eh (void)
1279 eh_landing_pad lp;
1280 eh_region r;
1281 tree lab;
1282 int i;
1284 if (cfun->eh == NULL)
1285 return;
1287 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1288 if (lp && lp->post_landing_pad)
1290 lab = main_block_label (lp->post_landing_pad);
1291 if (lab != lp->post_landing_pad)
1293 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1294 EH_LANDING_PAD_NR (lab) = lp->index;
1298 FOR_ALL_EH_REGION (r)
1299 switch (r->type)
1301 case ERT_CLEANUP:
1302 case ERT_MUST_NOT_THROW:
1303 break;
1305 case ERT_TRY:
1307 eh_catch c;
1308 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
1310 lab = c->label;
1311 if (lab)
1312 c->label = main_block_label (lab);
1315 break;
1317 case ERT_ALLOWED_EXCEPTIONS:
1318 lab = r->u.allowed.label;
1319 if (lab)
1320 r->u.allowed.label = main_block_label (lab);
1321 break;
1326 /* Cleanup redundant labels. This is a three-step process:
1327 1) Find the leading label for each block.
1328 2) Redirect all references to labels to the leading labels.
1329 3) Cleanup all useless labels. */
1331 void
1332 cleanup_dead_labels (void)
1334 basic_block bb;
1335 label_for_bb = XCNEWVEC (struct label_record, last_basic_block_for_fn (cfun));
1337 /* Find a suitable label for each block. We use the first user-defined
1338 label if there is one, or otherwise just the first label we see. */
1339 FOR_EACH_BB_FN (bb, cfun)
1341 gimple_stmt_iterator i;
1343 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1345 tree label;
1346 gimple stmt = gsi_stmt (i);
1348 if (gimple_code (stmt) != GIMPLE_LABEL)
1349 break;
1351 label = gimple_label_label (stmt);
1353 /* If we have not yet seen a label for the current block,
1354 remember this one and see if there are more labels. */
1355 if (!label_for_bb[bb->index].label)
1357 label_for_bb[bb->index].label = label;
1358 continue;
1361 /* If we did see a label for the current block already, but it
1362 is an artificially created label, replace it if the current
1363 label is a user defined label. */
1364 if (!DECL_ARTIFICIAL (label)
1365 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
1367 label_for_bb[bb->index].label = label;
1368 break;
1373 /* Now redirect all jumps/branches to the selected label.
1374 First do so for each block ending in a control statement. */
1375 FOR_EACH_BB_FN (bb, cfun)
1377 gimple stmt = last_stmt (bb);
1378 tree label, new_label;
1380 if (!stmt)
1381 continue;
1383 switch (gimple_code (stmt))
1385 case GIMPLE_COND:
1386 label = gimple_cond_true_label (stmt);
1387 if (label)
1389 new_label = main_block_label (label);
1390 if (new_label != label)
1391 gimple_cond_set_true_label (stmt, new_label);
1394 label = gimple_cond_false_label (stmt);
1395 if (label)
1397 new_label = main_block_label (label);
1398 if (new_label != label)
1399 gimple_cond_set_false_label (stmt, new_label);
1401 break;
1403 case GIMPLE_SWITCH:
1405 size_t i, n = gimple_switch_num_labels (stmt);
1407 /* Replace all destination labels. */
1408 for (i = 0; i < n; ++i)
1410 tree case_label = gimple_switch_label (stmt, i);
1411 label = CASE_LABEL (case_label);
1412 new_label = main_block_label (label);
1413 if (new_label != label)
1414 CASE_LABEL (case_label) = new_label;
1416 break;
1419 case GIMPLE_ASM:
1421 int i, n = gimple_asm_nlabels (stmt);
1423 for (i = 0; i < n; ++i)
1425 tree cons = gimple_asm_label_op (stmt, i);
1426 tree label = main_block_label (TREE_VALUE (cons));
1427 TREE_VALUE (cons) = label;
1429 break;
1432 /* We have to handle gotos until they're removed, and we don't
1433 remove them until after we've created the CFG edges. */
1434 case GIMPLE_GOTO:
1435 if (!computed_goto_p (stmt))
1437 label = gimple_goto_dest (stmt);
1438 new_label = main_block_label (label);
1439 if (new_label != label)
1440 gimple_goto_set_dest (stmt, new_label);
1442 break;
1444 case GIMPLE_TRANSACTION:
1446 tree label = gimple_transaction_label (stmt);
1447 if (label)
1449 tree new_label = main_block_label (label);
1450 if (new_label != label)
1451 gimple_transaction_set_label (stmt, new_label);
1454 break;
1456 default:
1457 break;
1461 /* Do the same for the exception region tree labels. */
1462 cleanup_dead_labels_eh ();
1464 /* Finally, purge dead labels. All user-defined labels and labels that
1465 can be the target of non-local gotos and labels which have their
1466 address taken are preserved. */
1467 FOR_EACH_BB_FN (bb, cfun)
1469 gimple_stmt_iterator i;
1470 tree label_for_this_bb = label_for_bb[bb->index].label;
1472 if (!label_for_this_bb)
1473 continue;
1475 /* If the main label of the block is unused, we may still remove it. */
1476 if (!label_for_bb[bb->index].used)
1477 label_for_this_bb = NULL;
1479 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1481 tree label;
1482 gimple stmt = gsi_stmt (i);
1484 if (gimple_code (stmt) != GIMPLE_LABEL)
1485 break;
1487 label = gimple_label_label (stmt);
1489 if (label == label_for_this_bb
1490 || !DECL_ARTIFICIAL (label)
1491 || DECL_NONLOCAL (label)
1492 || FORCED_LABEL (label))
1493 gsi_next (&i);
1494 else
1495 gsi_remove (&i, true);
1499 free (label_for_bb);
1502 /* Scan the sorted vector of cases in STMT (a GIMPLE_SWITCH) and combine
1503 the ones jumping to the same label.
1504 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1506 void
1507 group_case_labels_stmt (gimple stmt)
1509 int old_size = gimple_switch_num_labels (stmt);
1510 int i, j, new_size = old_size;
1511 basic_block default_bb = NULL;
1513 default_bb = label_to_block (CASE_LABEL (gimple_switch_default_label (stmt)));
1515 /* Look for possible opportunities to merge cases. */
1516 i = 1;
1517 while (i < old_size)
1519 tree base_case, base_high;
1520 basic_block base_bb;
1522 base_case = gimple_switch_label (stmt, i);
1524 gcc_assert (base_case);
1525 base_bb = label_to_block (CASE_LABEL (base_case));
1527 /* Discard cases that have the same destination as the
1528 default case. */
1529 if (base_bb == default_bb)
1531 gimple_switch_set_label (stmt, i, NULL_TREE);
1532 i++;
1533 new_size--;
1534 continue;
1537 base_high = CASE_HIGH (base_case)
1538 ? CASE_HIGH (base_case)
1539 : CASE_LOW (base_case);
1540 i++;
1542 /* Try to merge case labels. Break out when we reach the end
1543 of the label vector or when we cannot merge the next case
1544 label with the current one. */
1545 while (i < old_size)
1547 tree merge_case = gimple_switch_label (stmt, i);
1548 basic_block merge_bb = label_to_block (CASE_LABEL (merge_case));
1549 wide_int bhp1 = wi::add (base_high, 1);
1551 /* Merge the cases if they jump to the same place,
1552 and their ranges are consecutive. */
1553 if (merge_bb == base_bb
1554 && wi::eq_p (CASE_LOW (merge_case), bhp1))
1556 base_high = CASE_HIGH (merge_case) ?
1557 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1558 CASE_HIGH (base_case) = base_high;
1559 gimple_switch_set_label (stmt, i, NULL_TREE);
1560 new_size--;
1561 i++;
1563 else
1564 break;
1568 /* Compress the case labels in the label vector, and adjust the
1569 length of the vector. */
1570 for (i = 0, j = 0; i < new_size; i++)
1572 while (! gimple_switch_label (stmt, j))
1573 j++;
1574 gimple_switch_set_label (stmt, i,
1575 gimple_switch_label (stmt, j++));
1578 gcc_assert (new_size <= old_size);
1579 gimple_switch_set_num_labels (stmt, new_size);
1582 /* Look for blocks ending in a multiway branch (a GIMPLE_SWITCH),
1583 and scan the sorted vector of cases. Combine the ones jumping to the
1584 same label. */
1586 void
1587 group_case_labels (void)
1589 basic_block bb;
1591 FOR_EACH_BB_FN (bb, cfun)
1593 gimple stmt = last_stmt (bb);
1594 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1595 group_case_labels_stmt (stmt);
1599 /* Checks whether we can merge block B into block A. */
1601 static bool
1602 gimple_can_merge_blocks_p (basic_block a, basic_block b)
1604 gimple stmt;
1605 gimple_stmt_iterator gsi;
1607 if (!single_succ_p (a))
1608 return false;
1610 if (single_succ_edge (a)->flags & EDGE_COMPLEX)
1611 return false;
1613 if (single_succ (a) != b)
1614 return false;
1616 if (!single_pred_p (b))
1617 return false;
1619 if (b == EXIT_BLOCK_PTR_FOR_FN (cfun))
1620 return false;
1622 /* If A ends by a statement causing exceptions or something similar, we
1623 cannot merge the blocks. */
1624 stmt = last_stmt (a);
1625 if (stmt && stmt_ends_bb_p (stmt))
1626 return false;
1628 /* Do not allow a block with only a non-local label to be merged. */
1629 if (stmt
1630 && gimple_code (stmt) == GIMPLE_LABEL
1631 && DECL_NONLOCAL (gimple_label_label (stmt)))
1632 return false;
1634 /* Examine the labels at the beginning of B. */
1635 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
1637 tree lab;
1638 stmt = gsi_stmt (gsi);
1639 if (gimple_code (stmt) != GIMPLE_LABEL)
1640 break;
1641 lab = gimple_label_label (stmt);
1643 /* Do not remove user forced labels or for -O0 any user labels. */
1644 if (!DECL_ARTIFICIAL (lab) && (!optimize || FORCED_LABEL (lab)))
1645 return false;
1648 /* Protect the loop latches. */
1649 if (current_loops && b->loop_father->latch == b)
1650 return false;
1652 /* It must be possible to eliminate all phi nodes in B. If ssa form
1653 is not up-to-date and a name-mapping is registered, we cannot eliminate
1654 any phis. Symbols marked for renaming are never a problem though. */
1655 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi); gsi_next (&gsi))
1657 gimple phi = gsi_stmt (gsi);
1658 /* Technically only new names matter. */
1659 if (name_registered_for_update_p (PHI_RESULT (phi)))
1660 return false;
1663 /* When not optimizing, don't merge if we'd lose goto_locus. */
1664 if (!optimize
1665 && single_succ_edge (a)->goto_locus != UNKNOWN_LOCATION)
1667 location_t goto_locus = single_succ_edge (a)->goto_locus;
1668 gimple_stmt_iterator prev, next;
1669 prev = gsi_last_nondebug_bb (a);
1670 next = gsi_after_labels (b);
1671 if (!gsi_end_p (next) && is_gimple_debug (gsi_stmt (next)))
1672 gsi_next_nondebug (&next);
1673 if ((gsi_end_p (prev)
1674 || gimple_location (gsi_stmt (prev)) != goto_locus)
1675 && (gsi_end_p (next)
1676 || gimple_location (gsi_stmt (next)) != goto_locus))
1677 return false;
1680 return true;
1683 /* Replaces all uses of NAME by VAL. */
1685 void
1686 replace_uses_by (tree name, tree val)
1688 imm_use_iterator imm_iter;
1689 use_operand_p use;
1690 gimple stmt;
1691 edge e;
1693 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1695 /* Mark the block if we change the last stmt in it. */
1696 if (cfgcleanup_altered_bbs
1697 && stmt_ends_bb_p (stmt))
1698 bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1700 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1702 replace_exp (use, val);
1704 if (gimple_code (stmt) == GIMPLE_PHI)
1706 e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
1707 if (e->flags & EDGE_ABNORMAL)
1709 /* This can only occur for virtual operands, since
1710 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1711 would prevent replacement. */
1712 gcc_checking_assert (virtual_operand_p (name));
1713 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1718 if (gimple_code (stmt) != GIMPLE_PHI)
1720 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1721 gimple orig_stmt = stmt;
1722 size_t i;
1724 /* FIXME. It shouldn't be required to keep TREE_CONSTANT
1725 on ADDR_EXPRs up-to-date on GIMPLE. Propagation will
1726 only change sth from non-invariant to invariant, and only
1727 when propagating constants. */
1728 if (is_gimple_min_invariant (val))
1729 for (i = 0; i < gimple_num_ops (stmt); i++)
1731 tree op = gimple_op (stmt, i);
1732 /* Operands may be empty here. For example, the labels
1733 of a GIMPLE_COND are nulled out following the creation
1734 of the corresponding CFG edges. */
1735 if (op && TREE_CODE (op) == ADDR_EXPR)
1736 recompute_tree_invariant_for_addr_expr (op);
1739 if (fold_stmt (&gsi))
1740 stmt = gsi_stmt (gsi);
1742 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1743 gimple_purge_dead_eh_edges (gimple_bb (stmt));
1745 update_stmt (stmt);
1749 gcc_checking_assert (has_zero_uses (name));
1751 /* Also update the trees stored in loop structures. */
1752 if (current_loops)
1754 struct loop *loop;
1756 FOR_EACH_LOOP (loop, 0)
1758 substitute_in_loop_info (loop, name, val);
1763 /* Merge block B into block A. */
1765 static void
1766 gimple_merge_blocks (basic_block a, basic_block b)
1768 gimple_stmt_iterator last, gsi, psi;
1770 if (dump_file)
1771 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1773 /* Remove all single-valued PHI nodes from block B of the form
1774 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1775 gsi = gsi_last_bb (a);
1776 for (psi = gsi_start_phis (b); !gsi_end_p (psi); )
1778 gimple phi = gsi_stmt (psi);
1779 tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
1780 gimple copy;
1781 bool may_replace_uses = (virtual_operand_p (def)
1782 || may_propagate_copy (def, use));
1784 /* In case we maintain loop closed ssa form, do not propagate arguments
1785 of loop exit phi nodes. */
1786 if (current_loops
1787 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1788 && !virtual_operand_p (def)
1789 && TREE_CODE (use) == SSA_NAME
1790 && a->loop_father != b->loop_father)
1791 may_replace_uses = false;
1793 if (!may_replace_uses)
1795 gcc_assert (!virtual_operand_p (def));
1797 /* Note that just emitting the copies is fine -- there is no problem
1798 with ordering of phi nodes. This is because A is the single
1799 predecessor of B, therefore results of the phi nodes cannot
1800 appear as arguments of the phi nodes. */
1801 copy = gimple_build_assign (def, use);
1802 gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
1803 remove_phi_node (&psi, false);
1805 else
1807 /* If we deal with a PHI for virtual operands, we can simply
1808 propagate these without fussing with folding or updating
1809 the stmt. */
1810 if (virtual_operand_p (def))
1812 imm_use_iterator iter;
1813 use_operand_p use_p;
1814 gimple stmt;
1816 FOR_EACH_IMM_USE_STMT (stmt, iter, def)
1817 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1818 SET_USE (use_p, use);
1820 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1821 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use) = 1;
1823 else
1824 replace_uses_by (def, use);
1826 remove_phi_node (&psi, true);
1830 /* Ensure that B follows A. */
1831 move_block_after (b, a);
1833 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1834 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1836 /* Remove labels from B and set gimple_bb to A for other statements. */
1837 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
1839 gimple stmt = gsi_stmt (gsi);
1840 if (gimple_code (stmt) == GIMPLE_LABEL)
1842 tree label = gimple_label_label (stmt);
1843 int lp_nr;
1845 gsi_remove (&gsi, false);
1847 /* Now that we can thread computed gotos, we might have
1848 a situation where we have a forced label in block B
1849 However, the label at the start of block B might still be
1850 used in other ways (think about the runtime checking for
1851 Fortran assigned gotos). So we can not just delete the
1852 label. Instead we move the label to the start of block A. */
1853 if (FORCED_LABEL (label))
1855 gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
1856 gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
1858 /* Other user labels keep around in a form of a debug stmt. */
1859 else if (!DECL_ARTIFICIAL (label) && MAY_HAVE_DEBUG_STMTS)
1861 gimple dbg = gimple_build_debug_bind (label,
1862 integer_zero_node,
1863 stmt);
1864 gimple_debug_bind_reset_value (dbg);
1865 gsi_insert_before (&gsi, dbg, GSI_SAME_STMT);
1868 lp_nr = EH_LANDING_PAD_NR (label);
1869 if (lp_nr)
1871 eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
1872 lp->post_landing_pad = NULL;
1875 else
1877 gimple_set_bb (stmt, a);
1878 gsi_next (&gsi);
1882 /* When merging two BBs, if their counts are different, the larger count
1883 is selected as the new bb count. This is to handle inconsistent
1884 profiles. */
1885 if (a->loop_father == b->loop_father)
1887 a->count = MAX (a->count, b->count);
1888 a->frequency = MAX (a->frequency, b->frequency);
1891 /* Merge the sequences. */
1892 last = gsi_last_bb (a);
1893 gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
1894 set_bb_seq (b, NULL);
1896 if (cfgcleanup_altered_bbs)
1897 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1901 /* Return the one of two successors of BB that is not reachable by a
1902 complex edge, if there is one. Else, return BB. We use
1903 this in optimizations that use post-dominators for their heuristics,
1904 to catch the cases in C++ where function calls are involved. */
1906 basic_block
1907 single_noncomplex_succ (basic_block bb)
1909 edge e0, e1;
1910 if (EDGE_COUNT (bb->succs) != 2)
1911 return bb;
1913 e0 = EDGE_SUCC (bb, 0);
1914 e1 = EDGE_SUCC (bb, 1);
1915 if (e0->flags & EDGE_COMPLEX)
1916 return e1->dest;
1917 if (e1->flags & EDGE_COMPLEX)
1918 return e0->dest;
1920 return bb;
1923 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1925 void
1926 notice_special_calls (gimple call)
1928 int flags = gimple_call_flags (call);
1930 if (flags & ECF_MAY_BE_ALLOCA)
1931 cfun->calls_alloca = true;
1932 if (flags & ECF_RETURNS_TWICE)
1933 cfun->calls_setjmp = true;
1937 /* Clear flags set by notice_special_calls. Used by dead code removal
1938 to update the flags. */
1940 void
1941 clear_special_calls (void)
1943 cfun->calls_alloca = false;
1944 cfun->calls_setjmp = false;
1947 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1949 static void
1950 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1952 /* Since this block is no longer reachable, we can just delete all
1953 of its PHI nodes. */
1954 remove_phi_nodes (bb);
1956 /* Remove edges to BB's successors. */
1957 while (EDGE_COUNT (bb->succs) > 0)
1958 remove_edge (EDGE_SUCC (bb, 0));
1962 /* Remove statements of basic block BB. */
1964 static void
1965 remove_bb (basic_block bb)
1967 gimple_stmt_iterator i;
1969 if (dump_file)
1971 fprintf (dump_file, "Removing basic block %d\n", bb->index);
1972 if (dump_flags & TDF_DETAILS)
1974 dump_bb (dump_file, bb, 0, TDF_BLOCKS);
1975 fprintf (dump_file, "\n");
1979 if (current_loops)
1981 struct loop *loop = bb->loop_father;
1983 /* If a loop gets removed, clean up the information associated
1984 with it. */
1985 if (loop->latch == bb
1986 || loop->header == bb)
1987 free_numbers_of_iterations_estimates_loop (loop);
1990 /* Remove all the instructions in the block. */
1991 if (bb_seq (bb) != NULL)
1993 /* Walk backwards so as to get a chance to substitute all
1994 released DEFs into debug stmts. See
1995 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
1996 details. */
1997 for (i = gsi_last_bb (bb); !gsi_end_p (i);)
1999 gimple stmt = gsi_stmt (i);
2000 if (gimple_code (stmt) == GIMPLE_LABEL
2001 && (FORCED_LABEL (gimple_label_label (stmt))
2002 || DECL_NONLOCAL (gimple_label_label (stmt))))
2004 basic_block new_bb;
2005 gimple_stmt_iterator new_gsi;
2007 /* A non-reachable non-local label may still be referenced.
2008 But it no longer needs to carry the extra semantics of
2009 non-locality. */
2010 if (DECL_NONLOCAL (gimple_label_label (stmt)))
2012 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
2013 FORCED_LABEL (gimple_label_label (stmt)) = 1;
2016 new_bb = bb->prev_bb;
2017 new_gsi = gsi_start_bb (new_bb);
2018 gsi_remove (&i, false);
2019 gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
2021 else
2023 /* Release SSA definitions if we are in SSA. Note that we
2024 may be called when not in SSA. For example,
2025 final_cleanup calls this function via
2026 cleanup_tree_cfg. */
2027 if (gimple_in_ssa_p (cfun))
2028 release_defs (stmt);
2030 gsi_remove (&i, true);
2033 if (gsi_end_p (i))
2034 i = gsi_last_bb (bb);
2035 else
2036 gsi_prev (&i);
2040 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2041 bb->il.gimple.seq = NULL;
2042 bb->il.gimple.phi_nodes = NULL;
2046 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2047 predicate VAL, return the edge that will be taken out of the block.
2048 If VAL does not match a unique edge, NULL is returned. */
2050 edge
2051 find_taken_edge (basic_block bb, tree val)
2053 gimple stmt;
2055 stmt = last_stmt (bb);
2057 gcc_assert (stmt);
2058 gcc_assert (is_ctrl_stmt (stmt));
2060 if (val == NULL)
2061 return NULL;
2063 if (!is_gimple_min_invariant (val))
2064 return NULL;
2066 if (gimple_code (stmt) == GIMPLE_COND)
2067 return find_taken_edge_cond_expr (bb, val);
2069 if (gimple_code (stmt) == GIMPLE_SWITCH)
2070 return find_taken_edge_switch_expr (bb, val);
2072 if (computed_goto_p (stmt))
2074 /* Only optimize if the argument is a label, if the argument is
2075 not a label then we can not construct a proper CFG.
2077 It may be the case that we only need to allow the LABEL_REF to
2078 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
2079 appear inside a LABEL_EXPR just to be safe. */
2080 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
2081 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
2082 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
2083 return NULL;
2086 gcc_unreachable ();
2089 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2090 statement, determine which of the outgoing edges will be taken out of the
2091 block. Return NULL if either edge may be taken. */
2093 static edge
2094 find_taken_edge_computed_goto (basic_block bb, tree val)
2096 basic_block dest;
2097 edge e = NULL;
2099 dest = label_to_block (val);
2100 if (dest)
2102 e = find_edge (bb, dest);
2103 gcc_assert (e != NULL);
2106 return e;
2109 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2110 statement, determine which of the two edges will be taken out of the
2111 block. Return NULL if either edge may be taken. */
2113 static edge
2114 find_taken_edge_cond_expr (basic_block bb, tree val)
2116 edge true_edge, false_edge;
2118 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2120 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2121 return (integer_zerop (val) ? false_edge : true_edge);
2124 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2125 statement, determine which edge will be taken out of the block. Return
2126 NULL if any edge may be taken. */
2128 static edge
2129 find_taken_edge_switch_expr (basic_block bb, tree val)
2131 basic_block dest_bb;
2132 edge e;
2133 gimple switch_stmt;
2134 tree taken_case;
2136 switch_stmt = last_stmt (bb);
2137 taken_case = find_case_label_for_value (switch_stmt, val);
2138 dest_bb = label_to_block (CASE_LABEL (taken_case));
2140 e = find_edge (bb, dest_bb);
2141 gcc_assert (e);
2142 return e;
2146 /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
2147 We can make optimal use here of the fact that the case labels are
2148 sorted: We can do a binary search for a case matching VAL. */
2150 static tree
2151 find_case_label_for_value (gimple switch_stmt, tree val)
2153 size_t low, high, n = gimple_switch_num_labels (switch_stmt);
2154 tree default_case = gimple_switch_default_label (switch_stmt);
2156 for (low = 0, high = n; high - low > 1; )
2158 size_t i = (high + low) / 2;
2159 tree t = gimple_switch_label (switch_stmt, i);
2160 int cmp;
2162 /* Cache the result of comparing CASE_LOW and val. */
2163 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2165 if (cmp > 0)
2166 high = i;
2167 else
2168 low = i;
2170 if (CASE_HIGH (t) == NULL)
2172 /* A singe-valued case label. */
2173 if (cmp == 0)
2174 return t;
2176 else
2178 /* A case range. We can only handle integer ranges. */
2179 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2180 return t;
2184 return default_case;
2188 /* Dump a basic block on stderr. */
2190 void
2191 gimple_debug_bb (basic_block bb)
2193 dump_bb (stderr, bb, 0, TDF_VOPS|TDF_MEMSYMS|TDF_BLOCKS);
2197 /* Dump basic block with index N on stderr. */
2199 basic_block
2200 gimple_debug_bb_n (int n)
2202 gimple_debug_bb (BASIC_BLOCK_FOR_FN (cfun, n));
2203 return BASIC_BLOCK_FOR_FN (cfun, n);
2207 /* Dump the CFG on stderr.
2209 FLAGS are the same used by the tree dumping functions
2210 (see TDF_* in dumpfile.h). */
2212 void
2213 gimple_debug_cfg (int flags)
2215 gimple_dump_cfg (stderr, flags);
2219 /* Dump the program showing basic block boundaries on the given FILE.
2221 FLAGS are the same used by the tree dumping functions (see TDF_* in
2222 tree.h). */
2224 void
2225 gimple_dump_cfg (FILE *file, int flags)
2227 if (flags & TDF_DETAILS)
2229 dump_function_header (file, current_function_decl, flags);
2230 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2231 n_basic_blocks_for_fn (cfun), n_edges_for_fn (cfun),
2232 last_basic_block_for_fn (cfun));
2234 brief_dump_cfg (file, flags | TDF_COMMENT);
2235 fprintf (file, "\n");
2238 if (flags & TDF_STATS)
2239 dump_cfg_stats (file);
2241 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2245 /* Dump CFG statistics on FILE. */
2247 void
2248 dump_cfg_stats (FILE *file)
2250 static long max_num_merged_labels = 0;
2251 unsigned long size, total = 0;
2252 long num_edges;
2253 basic_block bb;
2254 const char * const fmt_str = "%-30s%-13s%12s\n";
2255 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2256 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2257 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2258 const char *funcname = current_function_name ();
2260 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2262 fprintf (file, "---------------------------------------------------------\n");
2263 fprintf (file, fmt_str, "", " Number of ", "Memory");
2264 fprintf (file, fmt_str, "", " instances ", "used ");
2265 fprintf (file, "---------------------------------------------------------\n");
2267 size = n_basic_blocks_for_fn (cfun) * sizeof (struct basic_block_def);
2268 total += size;
2269 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks_for_fn (cfun),
2270 SCALE (size), LABEL (size));
2272 num_edges = 0;
2273 FOR_EACH_BB_FN (bb, cfun)
2274 num_edges += EDGE_COUNT (bb->succs);
2275 size = num_edges * sizeof (struct edge_def);
2276 total += size;
2277 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2279 fprintf (file, "---------------------------------------------------------\n");
2280 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2281 LABEL (total));
2282 fprintf (file, "---------------------------------------------------------\n");
2283 fprintf (file, "\n");
2285 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2286 max_num_merged_labels = cfg_stats.num_merged_labels;
2288 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2289 cfg_stats.num_merged_labels, max_num_merged_labels);
2291 fprintf (file, "\n");
2295 /* Dump CFG statistics on stderr. Keep extern so that it's always
2296 linked in the final executable. */
2298 DEBUG_FUNCTION void
2299 debug_cfg_stats (void)
2301 dump_cfg_stats (stderr);
2304 /*---------------------------------------------------------------------------
2305 Miscellaneous helpers
2306 ---------------------------------------------------------------------------*/
2308 /* Return true if T, a GIMPLE_CALL, can make an abnormal transfer of control
2309 flow. Transfers of control flow associated with EH are excluded. */
2311 static bool
2312 call_can_make_abnormal_goto (gimple t)
2314 /* If the function has no non-local labels, then a call cannot make an
2315 abnormal transfer of control. */
2316 if (!cfun->has_nonlocal_label
2317 && !cfun->calls_setjmp)
2318 return false;
2320 /* Likewise if the call has no side effects. */
2321 if (!gimple_has_side_effects (t))
2322 return false;
2324 /* Likewise if the called function is leaf. */
2325 if (gimple_call_flags (t) & ECF_LEAF)
2326 return false;
2328 return true;
2332 /* Return true if T can make an abnormal transfer of control flow.
2333 Transfers of control flow associated with EH are excluded. */
2335 bool
2336 stmt_can_make_abnormal_goto (gimple t)
2338 if (computed_goto_p (t))
2339 return true;
2340 if (is_gimple_call (t))
2341 return call_can_make_abnormal_goto (t);
2342 return false;
2346 /* Return true if T represents a stmt that always transfers control. */
2348 bool
2349 is_ctrl_stmt (gimple t)
2351 switch (gimple_code (t))
2353 case GIMPLE_COND:
2354 case GIMPLE_SWITCH:
2355 case GIMPLE_GOTO:
2356 case GIMPLE_RETURN:
2357 case GIMPLE_RESX:
2358 return true;
2359 default:
2360 return false;
2365 /* Return true if T is a statement that may alter the flow of control
2366 (e.g., a call to a non-returning function). */
2368 bool
2369 is_ctrl_altering_stmt (gimple t)
2371 gcc_assert (t);
2373 switch (gimple_code (t))
2375 case GIMPLE_CALL:
2377 int flags = gimple_call_flags (t);
2379 /* A call alters control flow if it can make an abnormal goto. */
2380 if (call_can_make_abnormal_goto (t))
2381 return true;
2383 /* A call also alters control flow if it does not return. */
2384 if (flags & ECF_NORETURN)
2385 return true;
2387 /* TM ending statements have backedges out of the transaction.
2388 Return true so we split the basic block containing them.
2389 Note that the TM_BUILTIN test is merely an optimization. */
2390 if ((flags & ECF_TM_BUILTIN)
2391 && is_tm_ending_fndecl (gimple_call_fndecl (t)))
2392 return true;
2394 /* BUILT_IN_RETURN call is same as return statement. */
2395 if (gimple_call_builtin_p (t, BUILT_IN_RETURN))
2396 return true;
2398 break;
2400 case GIMPLE_EH_DISPATCH:
2401 /* EH_DISPATCH branches to the individual catch handlers at
2402 this level of a try or allowed-exceptions region. It can
2403 fallthru to the next statement as well. */
2404 return true;
2406 case GIMPLE_ASM:
2407 if (gimple_asm_nlabels (t) > 0)
2408 return true;
2409 break;
2411 CASE_GIMPLE_OMP:
2412 /* OpenMP directives alter control flow. */
2413 return true;
2415 case GIMPLE_TRANSACTION:
2416 /* A transaction start alters control flow. */
2417 return true;
2419 default:
2420 break;
2423 /* If a statement can throw, it alters control flow. */
2424 return stmt_can_throw_internal (t);
2428 /* Return true if T is a simple local goto. */
2430 bool
2431 simple_goto_p (gimple t)
2433 return (gimple_code (t) == GIMPLE_GOTO
2434 && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2438 /* Return true if STMT should start a new basic block. PREV_STMT is
2439 the statement preceding STMT. It is used when STMT is a label or a
2440 case label. Labels should only start a new basic block if their
2441 previous statement wasn't a label. Otherwise, sequence of labels
2442 would generate unnecessary basic blocks that only contain a single
2443 label. */
2445 static inline bool
2446 stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
2448 if (stmt == NULL)
2449 return false;
2451 /* Labels start a new basic block only if the preceding statement
2452 wasn't a label of the same type. This prevents the creation of
2453 consecutive blocks that have nothing but a single label. */
2454 if (gimple_code (stmt) == GIMPLE_LABEL)
2456 /* Nonlocal and computed GOTO targets always start a new block. */
2457 if (DECL_NONLOCAL (gimple_label_label (stmt))
2458 || FORCED_LABEL (gimple_label_label (stmt)))
2459 return true;
2461 if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
2463 if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
2464 return true;
2466 cfg_stats.num_merged_labels++;
2467 return false;
2469 else
2470 return true;
2472 else if (gimple_code (stmt) == GIMPLE_CALL
2473 && gimple_call_flags (stmt) & ECF_RETURNS_TWICE)
2474 /* setjmp acts similar to a nonlocal GOTO target and thus should
2475 start a new block. */
2476 return true;
2478 return false;
2482 /* Return true if T should end a basic block. */
2484 bool
2485 stmt_ends_bb_p (gimple t)
2487 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2490 /* Remove block annotations and other data structures. */
2492 void
2493 delete_tree_cfg_annotations (void)
2495 vec_free (label_to_block_map_for_fn (cfun));
2499 /* Return the first statement in basic block BB. */
2501 gimple
2502 first_stmt (basic_block bb)
2504 gimple_stmt_iterator i = gsi_start_bb (bb);
2505 gimple stmt = NULL;
2507 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2509 gsi_next (&i);
2510 stmt = NULL;
2512 return stmt;
2515 /* Return the first non-label statement in basic block BB. */
2517 static gimple
2518 first_non_label_stmt (basic_block bb)
2520 gimple_stmt_iterator i = gsi_start_bb (bb);
2521 while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
2522 gsi_next (&i);
2523 return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
2526 /* Return the last statement in basic block BB. */
2528 gimple
2529 last_stmt (basic_block bb)
2531 gimple_stmt_iterator i = gsi_last_bb (bb);
2532 gimple stmt = NULL;
2534 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2536 gsi_prev (&i);
2537 stmt = NULL;
2539 return stmt;
2542 /* Return the last statement of an otherwise empty block. Return NULL
2543 if the block is totally empty, or if it contains more than one
2544 statement. */
2546 gimple
2547 last_and_only_stmt (basic_block bb)
2549 gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
2550 gimple last, prev;
2552 if (gsi_end_p (i))
2553 return NULL;
2555 last = gsi_stmt (i);
2556 gsi_prev_nondebug (&i);
2557 if (gsi_end_p (i))
2558 return last;
2560 /* Empty statements should no longer appear in the instruction stream.
2561 Everything that might have appeared before should be deleted by
2562 remove_useless_stmts, and the optimizers should just gsi_remove
2563 instead of smashing with build_empty_stmt.
2565 Thus the only thing that should appear here in a block containing
2566 one executable statement is a label. */
2567 prev = gsi_stmt (i);
2568 if (gimple_code (prev) == GIMPLE_LABEL)
2569 return last;
2570 else
2571 return NULL;
2574 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
2576 static void
2577 reinstall_phi_args (edge new_edge, edge old_edge)
2579 edge_var_map *vm;
2580 int i;
2581 gimple_stmt_iterator phis;
2583 vec<edge_var_map> *v = redirect_edge_var_map_vector (old_edge);
2584 if (!v)
2585 return;
2587 for (i = 0, phis = gsi_start_phis (new_edge->dest);
2588 v->iterate (i, &vm) && !gsi_end_p (phis);
2589 i++, gsi_next (&phis))
2591 gimple phi = gsi_stmt (phis);
2592 tree result = redirect_edge_var_map_result (vm);
2593 tree arg = redirect_edge_var_map_def (vm);
2595 gcc_assert (result == gimple_phi_result (phi));
2597 add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
2600 redirect_edge_var_map_clear (old_edge);
2603 /* Returns the basic block after which the new basic block created
2604 by splitting edge EDGE_IN should be placed. Tries to keep the new block
2605 near its "logical" location. This is of most help to humans looking
2606 at debugging dumps. */
2608 static basic_block
2609 split_edge_bb_loc (edge edge_in)
2611 basic_block dest = edge_in->dest;
2612 basic_block dest_prev = dest->prev_bb;
2614 if (dest_prev)
2616 edge e = find_edge (dest_prev, dest);
2617 if (e && !(e->flags & EDGE_COMPLEX))
2618 return edge_in->src;
2620 return dest_prev;
2623 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2624 Abort on abnormal edges. */
2626 static basic_block
2627 gimple_split_edge (edge edge_in)
2629 basic_block new_bb, after_bb, dest;
2630 edge new_edge, e;
2632 /* Abnormal edges cannot be split. */
2633 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2635 dest = edge_in->dest;
2637 after_bb = split_edge_bb_loc (edge_in);
2639 new_bb = create_empty_bb (after_bb);
2640 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2641 new_bb->count = edge_in->count;
2642 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2643 new_edge->probability = REG_BR_PROB_BASE;
2644 new_edge->count = edge_in->count;
2646 e = redirect_edge_and_branch (edge_in, new_bb);
2647 gcc_assert (e == edge_in);
2648 reinstall_phi_args (new_edge, e);
2650 return new_bb;
2654 /* Verify properties of the address expression T with base object BASE. */
2656 static tree
2657 verify_address (tree t, tree base)
2659 bool old_constant;
2660 bool old_side_effects;
2661 bool new_constant;
2662 bool new_side_effects;
2664 old_constant = TREE_CONSTANT (t);
2665 old_side_effects = TREE_SIDE_EFFECTS (t);
2667 recompute_tree_invariant_for_addr_expr (t);
2668 new_side_effects = TREE_SIDE_EFFECTS (t);
2669 new_constant = TREE_CONSTANT (t);
2671 if (old_constant != new_constant)
2673 error ("constant not recomputed when ADDR_EXPR changed");
2674 return t;
2676 if (old_side_effects != new_side_effects)
2678 error ("side effects not recomputed when ADDR_EXPR changed");
2679 return t;
2682 if (!(TREE_CODE (base) == VAR_DECL
2683 || TREE_CODE (base) == PARM_DECL
2684 || TREE_CODE (base) == RESULT_DECL))
2685 return NULL_TREE;
2687 if (DECL_GIMPLE_REG_P (base))
2689 error ("DECL_GIMPLE_REG_P set on a variable with address taken");
2690 return base;
2693 return NULL_TREE;
2696 /* Callback for walk_tree, check that all elements with address taken are
2697 properly noticed as such. The DATA is an int* that is 1 if TP was seen
2698 inside a PHI node. */
2700 static tree
2701 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2703 tree t = *tp, x;
2705 if (TYPE_P (t))
2706 *walk_subtrees = 0;
2708 /* Check operand N for being valid GIMPLE and give error MSG if not. */
2709 #define CHECK_OP(N, MSG) \
2710 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
2711 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
2713 switch (TREE_CODE (t))
2715 case SSA_NAME:
2716 if (SSA_NAME_IN_FREE_LIST (t))
2718 error ("SSA name in freelist but still referenced");
2719 return *tp;
2721 break;
2723 case INDIRECT_REF:
2724 error ("INDIRECT_REF in gimple IL");
2725 return t;
2727 case MEM_REF:
2728 x = TREE_OPERAND (t, 0);
2729 if (!POINTER_TYPE_P (TREE_TYPE (x))
2730 || !is_gimple_mem_ref_addr (x))
2732 error ("invalid first operand of MEM_REF");
2733 return x;
2735 if (TREE_CODE (TREE_OPERAND (t, 1)) != INTEGER_CST
2736 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 1))))
2738 error ("invalid offset operand of MEM_REF");
2739 return TREE_OPERAND (t, 1);
2741 if (TREE_CODE (x) == ADDR_EXPR
2742 && (x = verify_address (x, TREE_OPERAND (x, 0))))
2743 return x;
2744 *walk_subtrees = 0;
2745 break;
2747 case ASSERT_EXPR:
2748 x = fold (ASSERT_EXPR_COND (t));
2749 if (x == boolean_false_node)
2751 error ("ASSERT_EXPR with an always-false condition");
2752 return *tp;
2754 break;
2756 case MODIFY_EXPR:
2757 error ("MODIFY_EXPR not expected while having tuples");
2758 return *tp;
2760 case ADDR_EXPR:
2762 tree tem;
2764 gcc_assert (is_gimple_address (t));
2766 /* Skip any references (they will be checked when we recurse down the
2767 tree) and ensure that any variable used as a prefix is marked
2768 addressable. */
2769 for (x = TREE_OPERAND (t, 0);
2770 handled_component_p (x);
2771 x = TREE_OPERAND (x, 0))
2774 if ((tem = verify_address (t, x)))
2775 return tem;
2777 if (!(TREE_CODE (x) == VAR_DECL
2778 || TREE_CODE (x) == PARM_DECL
2779 || TREE_CODE (x) == RESULT_DECL))
2780 return NULL;
2782 if (!TREE_ADDRESSABLE (x))
2784 error ("address taken, but ADDRESSABLE bit not set");
2785 return x;
2788 break;
2791 case COND_EXPR:
2792 x = COND_EXPR_COND (t);
2793 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
2795 error ("non-integral used in condition");
2796 return x;
2798 if (!is_gimple_condexpr (x))
2800 error ("invalid conditional operand");
2801 return x;
2803 break;
2805 case NON_LVALUE_EXPR:
2806 case TRUTH_NOT_EXPR:
2807 gcc_unreachable ();
2809 CASE_CONVERT:
2810 case FIX_TRUNC_EXPR:
2811 case FLOAT_EXPR:
2812 case NEGATE_EXPR:
2813 case ABS_EXPR:
2814 case BIT_NOT_EXPR:
2815 CHECK_OP (0, "invalid operand to unary operator");
2816 break;
2818 case REALPART_EXPR:
2819 case IMAGPART_EXPR:
2820 case BIT_FIELD_REF:
2821 if (!is_gimple_reg_type (TREE_TYPE (t)))
2823 error ("non-scalar BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR");
2824 return t;
2827 if (TREE_CODE (t) == BIT_FIELD_REF)
2829 tree t0 = TREE_OPERAND (t, 0);
2830 tree t1 = TREE_OPERAND (t, 1);
2831 tree t2 = TREE_OPERAND (t, 2);
2832 if (!tree_fits_uhwi_p (t1)
2833 || !tree_fits_uhwi_p (t2))
2835 error ("invalid position or size operand to BIT_FIELD_REF");
2836 return t;
2838 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
2839 && (TYPE_PRECISION (TREE_TYPE (t))
2840 != tree_to_uhwi (t1)))
2842 error ("integral result type precision does not match "
2843 "field size of BIT_FIELD_REF");
2844 return t;
2846 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
2847 && TYPE_MODE (TREE_TYPE (t)) != BLKmode
2848 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
2849 != tree_to_uhwi (t1)))
2851 error ("mode precision of non-integral result does not "
2852 "match field size of BIT_FIELD_REF");
2853 return t;
2855 if (!AGGREGATE_TYPE_P (TREE_TYPE (t0))
2856 && (tree_to_uhwi (t1) + tree_to_uhwi (t2)
2857 > tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t0)))))
2859 error ("position plus size exceeds size of referenced object in "
2860 "BIT_FIELD_REF");
2861 return t;
2864 t = TREE_OPERAND (t, 0);
2866 /* Fall-through. */
2867 case COMPONENT_REF:
2868 case ARRAY_REF:
2869 case ARRAY_RANGE_REF:
2870 case VIEW_CONVERT_EXPR:
2871 /* We have a nest of references. Verify that each of the operands
2872 that determine where to reference is either a constant or a variable,
2873 verify that the base is valid, and then show we've already checked
2874 the subtrees. */
2875 while (handled_component_p (t))
2877 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
2878 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
2879 else if (TREE_CODE (t) == ARRAY_REF
2880 || TREE_CODE (t) == ARRAY_RANGE_REF)
2882 CHECK_OP (1, "invalid array index");
2883 if (TREE_OPERAND (t, 2))
2884 CHECK_OP (2, "invalid array lower bound");
2885 if (TREE_OPERAND (t, 3))
2886 CHECK_OP (3, "invalid array stride");
2888 else if (TREE_CODE (t) == BIT_FIELD_REF
2889 || TREE_CODE (t) == REALPART_EXPR
2890 || TREE_CODE (t) == IMAGPART_EXPR)
2892 error ("non-top-level BIT_FIELD_REF, IMAGPART_EXPR or "
2893 "REALPART_EXPR");
2894 return t;
2897 t = TREE_OPERAND (t, 0);
2900 if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
2902 error ("invalid reference prefix");
2903 return t;
2905 *walk_subtrees = 0;
2906 break;
2907 case PLUS_EXPR:
2908 case MINUS_EXPR:
2909 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
2910 POINTER_PLUS_EXPR. */
2911 if (POINTER_TYPE_P (TREE_TYPE (t)))
2913 error ("invalid operand to plus/minus, type is a pointer");
2914 return t;
2916 CHECK_OP (0, "invalid operand to binary operator");
2917 CHECK_OP (1, "invalid operand to binary operator");
2918 break;
2920 case POINTER_PLUS_EXPR:
2921 /* Check to make sure the first operand is a pointer or reference type. */
2922 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2924 error ("invalid operand to pointer plus, first operand is not a pointer");
2925 return t;
2927 /* Check to make sure the second operand is a ptrofftype. */
2928 if (!ptrofftype_p (TREE_TYPE (TREE_OPERAND (t, 1))))
2930 error ("invalid operand to pointer plus, second operand is not an "
2931 "integer type of appropriate width");
2932 return t;
2934 /* FALLTHROUGH */
2935 case LT_EXPR:
2936 case LE_EXPR:
2937 case GT_EXPR:
2938 case GE_EXPR:
2939 case EQ_EXPR:
2940 case NE_EXPR:
2941 case UNORDERED_EXPR:
2942 case ORDERED_EXPR:
2943 case UNLT_EXPR:
2944 case UNLE_EXPR:
2945 case UNGT_EXPR:
2946 case UNGE_EXPR:
2947 case UNEQ_EXPR:
2948 case LTGT_EXPR:
2949 case MULT_EXPR:
2950 case TRUNC_DIV_EXPR:
2951 case CEIL_DIV_EXPR:
2952 case FLOOR_DIV_EXPR:
2953 case ROUND_DIV_EXPR:
2954 case TRUNC_MOD_EXPR:
2955 case CEIL_MOD_EXPR:
2956 case FLOOR_MOD_EXPR:
2957 case ROUND_MOD_EXPR:
2958 case RDIV_EXPR:
2959 case EXACT_DIV_EXPR:
2960 case MIN_EXPR:
2961 case MAX_EXPR:
2962 case LSHIFT_EXPR:
2963 case RSHIFT_EXPR:
2964 case LROTATE_EXPR:
2965 case RROTATE_EXPR:
2966 case BIT_IOR_EXPR:
2967 case BIT_XOR_EXPR:
2968 case BIT_AND_EXPR:
2969 CHECK_OP (0, "invalid operand to binary operator");
2970 CHECK_OP (1, "invalid operand to binary operator");
2971 break;
2973 case CONSTRUCTOR:
2974 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2975 *walk_subtrees = 0;
2976 break;
2978 case CASE_LABEL_EXPR:
2979 if (CASE_CHAIN (t))
2981 error ("invalid CASE_CHAIN");
2982 return t;
2984 break;
2986 default:
2987 break;
2989 return NULL;
2991 #undef CHECK_OP
2995 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
2996 Returns true if there is an error, otherwise false. */
2998 static bool
2999 verify_types_in_gimple_min_lval (tree expr)
3001 tree op;
3003 if (is_gimple_id (expr))
3004 return false;
3006 if (TREE_CODE (expr) != TARGET_MEM_REF
3007 && TREE_CODE (expr) != MEM_REF)
3009 error ("invalid expression for min lvalue");
3010 return true;
3013 /* TARGET_MEM_REFs are strange beasts. */
3014 if (TREE_CODE (expr) == TARGET_MEM_REF)
3015 return false;
3017 op = TREE_OPERAND (expr, 0);
3018 if (!is_gimple_val (op))
3020 error ("invalid operand in indirect reference");
3021 debug_generic_stmt (op);
3022 return true;
3024 /* Memory references now generally can involve a value conversion. */
3026 return false;
3029 /* Verify if EXPR is a valid GIMPLE reference expression. If
3030 REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
3031 if there is an error, otherwise false. */
3033 static bool
3034 verify_types_in_gimple_reference (tree expr, bool require_lvalue)
3036 while (handled_component_p (expr))
3038 tree op = TREE_OPERAND (expr, 0);
3040 if (TREE_CODE (expr) == ARRAY_REF
3041 || TREE_CODE (expr) == ARRAY_RANGE_REF)
3043 if (!is_gimple_val (TREE_OPERAND (expr, 1))
3044 || (TREE_OPERAND (expr, 2)
3045 && !is_gimple_val (TREE_OPERAND (expr, 2)))
3046 || (TREE_OPERAND (expr, 3)
3047 && !is_gimple_val (TREE_OPERAND (expr, 3))))
3049 error ("invalid operands to array reference");
3050 debug_generic_stmt (expr);
3051 return true;
3055 /* Verify if the reference array element types are compatible. */
3056 if (TREE_CODE (expr) == ARRAY_REF
3057 && !useless_type_conversion_p (TREE_TYPE (expr),
3058 TREE_TYPE (TREE_TYPE (op))))
3060 error ("type mismatch in array reference");
3061 debug_generic_stmt (TREE_TYPE (expr));
3062 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3063 return true;
3065 if (TREE_CODE (expr) == ARRAY_RANGE_REF
3066 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
3067 TREE_TYPE (TREE_TYPE (op))))
3069 error ("type mismatch in array range reference");
3070 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
3071 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3072 return true;
3075 if ((TREE_CODE (expr) == REALPART_EXPR
3076 || TREE_CODE (expr) == IMAGPART_EXPR)
3077 && !useless_type_conversion_p (TREE_TYPE (expr),
3078 TREE_TYPE (TREE_TYPE (op))))
3080 error ("type mismatch in real/imagpart reference");
3081 debug_generic_stmt (TREE_TYPE (expr));
3082 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3083 return true;
3086 if (TREE_CODE (expr) == COMPONENT_REF
3087 && !useless_type_conversion_p (TREE_TYPE (expr),
3088 TREE_TYPE (TREE_OPERAND (expr, 1))))
3090 error ("type mismatch in component reference");
3091 debug_generic_stmt (TREE_TYPE (expr));
3092 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3093 return true;
3096 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
3098 /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
3099 that their operand is not an SSA name or an invariant when
3100 requiring an lvalue (this usually means there is a SRA or IPA-SRA
3101 bug). Otherwise there is nothing to verify, gross mismatches at
3102 most invoke undefined behavior. */
3103 if (require_lvalue
3104 && (TREE_CODE (op) == SSA_NAME
3105 || is_gimple_min_invariant (op)))
3107 error ("conversion of an SSA_NAME on the left hand side");
3108 debug_generic_stmt (expr);
3109 return true;
3111 else if (TREE_CODE (op) == SSA_NAME
3112 && TYPE_SIZE (TREE_TYPE (expr)) != TYPE_SIZE (TREE_TYPE (op)))
3114 error ("conversion of register to a different size");
3115 debug_generic_stmt (expr);
3116 return true;
3118 else if (!handled_component_p (op))
3119 return false;
3122 expr = op;
3125 if (TREE_CODE (expr) == MEM_REF)
3127 if (!is_gimple_mem_ref_addr (TREE_OPERAND (expr, 0)))
3129 error ("invalid address operand in MEM_REF");
3130 debug_generic_stmt (expr);
3131 return true;
3133 if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
3134 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
3136 error ("invalid offset operand in MEM_REF");
3137 debug_generic_stmt (expr);
3138 return true;
3141 else if (TREE_CODE (expr) == TARGET_MEM_REF)
3143 if (!TMR_BASE (expr)
3144 || !is_gimple_mem_ref_addr (TMR_BASE (expr)))
3146 error ("invalid address operand in TARGET_MEM_REF");
3147 return true;
3149 if (!TMR_OFFSET (expr)
3150 || TREE_CODE (TMR_OFFSET (expr)) != INTEGER_CST
3151 || !POINTER_TYPE_P (TREE_TYPE (TMR_OFFSET (expr))))
3153 error ("invalid offset operand in TARGET_MEM_REF");
3154 debug_generic_stmt (expr);
3155 return true;
3159 return ((require_lvalue || !is_gimple_min_invariant (expr))
3160 && verify_types_in_gimple_min_lval (expr));
3163 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3164 list of pointer-to types that is trivially convertible to DEST. */
3166 static bool
3167 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3169 tree src;
3171 if (!TYPE_POINTER_TO (src_obj))
3172 return true;
3174 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3175 if (useless_type_conversion_p (dest, src))
3176 return true;
3178 return false;
3181 /* Return true if TYPE1 is a fixed-point type and if conversions to and
3182 from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
3184 static bool
3185 valid_fixed_convert_types_p (tree type1, tree type2)
3187 return (FIXED_POINT_TYPE_P (type1)
3188 && (INTEGRAL_TYPE_P (type2)
3189 || SCALAR_FLOAT_TYPE_P (type2)
3190 || FIXED_POINT_TYPE_P (type2)));
3193 /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
3194 is a problem, otherwise false. */
3196 static bool
3197 verify_gimple_call (gimple stmt)
3199 tree fn = gimple_call_fn (stmt);
3200 tree fntype, fndecl;
3201 unsigned i;
3203 if (gimple_call_internal_p (stmt))
3205 if (fn)
3207 error ("gimple call has two targets");
3208 debug_generic_stmt (fn);
3209 return true;
3212 else
3214 if (!fn)
3216 error ("gimple call has no target");
3217 return true;
3221 if (fn && !is_gimple_call_addr (fn))
3223 error ("invalid function in gimple call");
3224 debug_generic_stmt (fn);
3225 return true;
3228 if (fn
3229 && (!POINTER_TYPE_P (TREE_TYPE (fn))
3230 || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
3231 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE)))
3233 error ("non-function in gimple call");
3234 return true;
3237 fndecl = gimple_call_fndecl (stmt);
3238 if (fndecl
3239 && TREE_CODE (fndecl) == FUNCTION_DECL
3240 && DECL_LOOPING_CONST_OR_PURE_P (fndecl)
3241 && !DECL_PURE_P (fndecl)
3242 && !TREE_READONLY (fndecl))
3244 error ("invalid pure const state for function");
3245 return true;
3248 if (gimple_call_lhs (stmt)
3249 && (!is_gimple_lvalue (gimple_call_lhs (stmt))
3250 || verify_types_in_gimple_reference (gimple_call_lhs (stmt), true)))
3252 error ("invalid LHS in gimple call");
3253 return true;
3256 if (gimple_call_lhs (stmt) && gimple_call_noreturn_p (stmt))
3258 error ("LHS in noreturn call");
3259 return true;
3262 fntype = gimple_call_fntype (stmt);
3263 if (fntype
3264 && gimple_call_lhs (stmt)
3265 && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
3266 TREE_TYPE (fntype))
3267 /* ??? At least C++ misses conversions at assignments from
3268 void * call results.
3269 ??? Java is completely off. Especially with functions
3270 returning java.lang.Object.
3271 For now simply allow arbitrary pointer type conversions. */
3272 && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
3273 && POINTER_TYPE_P (TREE_TYPE (fntype))))
3275 error ("invalid conversion in gimple call");
3276 debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
3277 debug_generic_stmt (TREE_TYPE (fntype));
3278 return true;
3281 if (gimple_call_chain (stmt)
3282 && !is_gimple_val (gimple_call_chain (stmt)))
3284 error ("invalid static chain in gimple call");
3285 debug_generic_stmt (gimple_call_chain (stmt));
3286 return true;
3289 /* If there is a static chain argument, this should not be an indirect
3290 call, and the decl should have DECL_STATIC_CHAIN set. */
3291 if (gimple_call_chain (stmt))
3293 if (!gimple_call_fndecl (stmt))
3295 error ("static chain in indirect gimple call");
3296 return true;
3298 fn = TREE_OPERAND (fn, 0);
3300 if (!DECL_STATIC_CHAIN (fn))
3302 error ("static chain with function that doesn%'t use one");
3303 return true;
3307 /* ??? The C frontend passes unpromoted arguments in case it
3308 didn't see a function declaration before the call. So for now
3309 leave the call arguments mostly unverified. Once we gimplify
3310 unit-at-a-time we have a chance to fix this. */
3312 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3314 tree arg = gimple_call_arg (stmt, i);
3315 if ((is_gimple_reg_type (TREE_TYPE (arg))
3316 && !is_gimple_val (arg))
3317 || (!is_gimple_reg_type (TREE_TYPE (arg))
3318 && !is_gimple_lvalue (arg)))
3320 error ("invalid argument to gimple call");
3321 debug_generic_expr (arg);
3322 return true;
3326 return false;
3329 /* Verifies the gimple comparison with the result type TYPE and
3330 the operands OP0 and OP1. */
3332 static bool
3333 verify_gimple_comparison (tree type, tree op0, tree op1)
3335 tree op0_type = TREE_TYPE (op0);
3336 tree op1_type = TREE_TYPE (op1);
3338 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3340 error ("invalid operands in gimple comparison");
3341 return true;
3344 /* For comparisons we do not have the operations type as the
3345 effective type the comparison is carried out in. Instead
3346 we require that either the first operand is trivially
3347 convertible into the second, or the other way around.
3348 Because we special-case pointers to void we allow
3349 comparisons of pointers with the same mode as well. */
3350 if (!useless_type_conversion_p (op0_type, op1_type)
3351 && !useless_type_conversion_p (op1_type, op0_type)
3352 && (!POINTER_TYPE_P (op0_type)
3353 || !POINTER_TYPE_P (op1_type)
3354 || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
3356 error ("mismatching comparison operand types");
3357 debug_generic_expr (op0_type);
3358 debug_generic_expr (op1_type);
3359 return true;
3362 /* The resulting type of a comparison may be an effective boolean type. */
3363 if (INTEGRAL_TYPE_P (type)
3364 && (TREE_CODE (type) == BOOLEAN_TYPE
3365 || TYPE_PRECISION (type) == 1))
3367 if (TREE_CODE (op0_type) == VECTOR_TYPE
3368 || TREE_CODE (op1_type) == VECTOR_TYPE)
3370 error ("vector comparison returning a boolean");
3371 debug_generic_expr (op0_type);
3372 debug_generic_expr (op1_type);
3373 return true;
3376 /* Or an integer vector type with the same size and element count
3377 as the comparison operand types. */
3378 else if (TREE_CODE (type) == VECTOR_TYPE
3379 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
3381 if (TREE_CODE (op0_type) != VECTOR_TYPE
3382 || TREE_CODE (op1_type) != VECTOR_TYPE)
3384 error ("non-vector operands in vector comparison");
3385 debug_generic_expr (op0_type);
3386 debug_generic_expr (op1_type);
3387 return true;
3390 if (TYPE_VECTOR_SUBPARTS (type) != TYPE_VECTOR_SUBPARTS (op0_type)
3391 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)))
3392 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0_type))))
3393 /* The result of a vector comparison is of signed
3394 integral type. */
3395 || TYPE_UNSIGNED (TREE_TYPE (type)))
3397 error ("invalid vector comparison resulting type");
3398 debug_generic_expr (type);
3399 return true;
3402 else
3404 error ("bogus comparison result type");
3405 debug_generic_expr (type);
3406 return true;
3409 return false;
3412 /* Verify a gimple assignment statement STMT with an unary rhs.
3413 Returns true if anything is wrong. */
3415 static bool
3416 verify_gimple_assign_unary (gimple stmt)
3418 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3419 tree lhs = gimple_assign_lhs (stmt);
3420 tree lhs_type = TREE_TYPE (lhs);
3421 tree rhs1 = gimple_assign_rhs1 (stmt);
3422 tree rhs1_type = TREE_TYPE (rhs1);
3424 if (!is_gimple_reg (lhs))
3426 error ("non-register as LHS of unary operation");
3427 return true;
3430 if (!is_gimple_val (rhs1))
3432 error ("invalid operand in unary operation");
3433 return true;
3436 /* First handle conversions. */
3437 switch (rhs_code)
3439 CASE_CONVERT:
3441 /* Allow conversions from pointer type to integral type only if
3442 there is no sign or zero extension involved.
3443 For targets were the precision of ptrofftype doesn't match that
3444 of pointers we need to allow arbitrary conversions to ptrofftype. */
3445 if ((POINTER_TYPE_P (lhs_type)
3446 && INTEGRAL_TYPE_P (rhs1_type))
3447 || (POINTER_TYPE_P (rhs1_type)
3448 && INTEGRAL_TYPE_P (lhs_type)
3449 && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
3450 || ptrofftype_p (sizetype))))
3451 return false;
3453 /* Allow conversion from integral to offset type and vice versa. */
3454 if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3455 && INTEGRAL_TYPE_P (rhs1_type))
3456 || (INTEGRAL_TYPE_P (lhs_type)
3457 && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3458 return false;
3460 /* Otherwise assert we are converting between types of the
3461 same kind. */
3462 if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3464 error ("invalid types in nop conversion");
3465 debug_generic_expr (lhs_type);
3466 debug_generic_expr (rhs1_type);
3467 return true;
3470 return false;
3473 case ADDR_SPACE_CONVERT_EXPR:
3475 if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
3476 || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
3477 == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
3479 error ("invalid types in address space conversion");
3480 debug_generic_expr (lhs_type);
3481 debug_generic_expr (rhs1_type);
3482 return true;
3485 return false;
3488 case FIXED_CONVERT_EXPR:
3490 if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3491 && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3493 error ("invalid types in fixed-point conversion");
3494 debug_generic_expr (lhs_type);
3495 debug_generic_expr (rhs1_type);
3496 return true;
3499 return false;
3502 case FLOAT_EXPR:
3504 if ((!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3505 && (!VECTOR_INTEGER_TYPE_P (rhs1_type)
3506 || !VECTOR_FLOAT_TYPE_P (lhs_type)))
3508 error ("invalid types in conversion to floating point");
3509 debug_generic_expr (lhs_type);
3510 debug_generic_expr (rhs1_type);
3511 return true;
3514 return false;
3517 case FIX_TRUNC_EXPR:
3519 if ((!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3520 && (!VECTOR_INTEGER_TYPE_P (lhs_type)
3521 || !VECTOR_FLOAT_TYPE_P (rhs1_type)))
3523 error ("invalid types in conversion to integer");
3524 debug_generic_expr (lhs_type);
3525 debug_generic_expr (rhs1_type);
3526 return true;
3529 return false;
3532 case VEC_UNPACK_HI_EXPR:
3533 case VEC_UNPACK_LO_EXPR:
3534 case REDUC_MAX_EXPR:
3535 case REDUC_MIN_EXPR:
3536 case REDUC_PLUS_EXPR:
3537 case VEC_UNPACK_FLOAT_HI_EXPR:
3538 case VEC_UNPACK_FLOAT_LO_EXPR:
3539 /* FIXME. */
3540 return false;
3542 case NEGATE_EXPR:
3543 case ABS_EXPR:
3544 case BIT_NOT_EXPR:
3545 case PAREN_EXPR:
3546 case NON_LVALUE_EXPR:
3547 case CONJ_EXPR:
3548 break;
3550 default:
3551 gcc_unreachable ();
3554 /* For the remaining codes assert there is no conversion involved. */
3555 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3557 error ("non-trivial conversion in unary operation");
3558 debug_generic_expr (lhs_type);
3559 debug_generic_expr (rhs1_type);
3560 return true;
3563 return false;
3566 /* Verify a gimple assignment statement STMT with a binary rhs.
3567 Returns true if anything is wrong. */
3569 static bool
3570 verify_gimple_assign_binary (gimple stmt)
3572 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3573 tree lhs = gimple_assign_lhs (stmt);
3574 tree lhs_type = TREE_TYPE (lhs);
3575 tree rhs1 = gimple_assign_rhs1 (stmt);
3576 tree rhs1_type = TREE_TYPE (rhs1);
3577 tree rhs2 = gimple_assign_rhs2 (stmt);
3578 tree rhs2_type = TREE_TYPE (rhs2);
3580 if (!is_gimple_reg (lhs))
3582 error ("non-register as LHS of binary operation");
3583 return true;
3586 if (!is_gimple_val (rhs1)
3587 || !is_gimple_val (rhs2))
3589 error ("invalid operands in binary operation");
3590 return true;
3593 /* First handle operations that involve different types. */
3594 switch (rhs_code)
3596 case COMPLEX_EXPR:
3598 if (TREE_CODE (lhs_type) != COMPLEX_TYPE
3599 || !(INTEGRAL_TYPE_P (rhs1_type)
3600 || SCALAR_FLOAT_TYPE_P (rhs1_type))
3601 || !(INTEGRAL_TYPE_P (rhs2_type)
3602 || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3604 error ("type mismatch in complex expression");
3605 debug_generic_expr (lhs_type);
3606 debug_generic_expr (rhs1_type);
3607 debug_generic_expr (rhs2_type);
3608 return true;
3611 return false;
3614 case LSHIFT_EXPR:
3615 case RSHIFT_EXPR:
3616 case LROTATE_EXPR:
3617 case RROTATE_EXPR:
3619 /* Shifts and rotates are ok on integral types, fixed point
3620 types and integer vector types. */
3621 if ((!INTEGRAL_TYPE_P (rhs1_type)
3622 && !FIXED_POINT_TYPE_P (rhs1_type)
3623 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3624 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
3625 || (!INTEGRAL_TYPE_P (rhs2_type)
3626 /* Vector shifts of vectors are also ok. */
3627 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3628 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3629 && TREE_CODE (rhs2_type) == VECTOR_TYPE
3630 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3631 || !useless_type_conversion_p (lhs_type, rhs1_type))
3633 error ("type mismatch in shift expression");
3634 debug_generic_expr (lhs_type);
3635 debug_generic_expr (rhs1_type);
3636 debug_generic_expr (rhs2_type);
3637 return true;
3640 return false;
3643 case VEC_LSHIFT_EXPR:
3644 case VEC_RSHIFT_EXPR:
3646 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3647 || !(INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3648 || POINTER_TYPE_P (TREE_TYPE (rhs1_type))
3649 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1_type))
3650 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
3651 || (!INTEGRAL_TYPE_P (rhs2_type)
3652 && (TREE_CODE (rhs2_type) != VECTOR_TYPE
3653 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3654 || !useless_type_conversion_p (lhs_type, rhs1_type))
3656 error ("type mismatch in vector shift expression");
3657 debug_generic_expr (lhs_type);
3658 debug_generic_expr (rhs1_type);
3659 debug_generic_expr (rhs2_type);
3660 return true;
3662 /* For shifting a vector of non-integral components we
3663 only allow shifting by a constant multiple of the element size. */
3664 if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3665 && (TREE_CODE (rhs2) != INTEGER_CST
3666 || !div_if_zero_remainder (rhs2,
3667 TYPE_SIZE (TREE_TYPE (rhs1_type)))))
3669 error ("non-element sized vector shift of floating point vector");
3670 return true;
3673 return false;
3676 case WIDEN_LSHIFT_EXPR:
3678 if (!INTEGRAL_TYPE_P (lhs_type)
3679 || !INTEGRAL_TYPE_P (rhs1_type)
3680 || TREE_CODE (rhs2) != INTEGER_CST
3681 || (2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)))
3683 error ("type mismatch in widening vector shift expression");
3684 debug_generic_expr (lhs_type);
3685 debug_generic_expr (rhs1_type);
3686 debug_generic_expr (rhs2_type);
3687 return true;
3690 return false;
3693 case VEC_WIDEN_LSHIFT_HI_EXPR:
3694 case VEC_WIDEN_LSHIFT_LO_EXPR:
3696 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3697 || TREE_CODE (lhs_type) != VECTOR_TYPE
3698 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3699 || !INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3700 || TREE_CODE (rhs2) != INTEGER_CST
3701 || (2 * TYPE_PRECISION (TREE_TYPE (rhs1_type))
3702 > TYPE_PRECISION (TREE_TYPE (lhs_type))))
3704 error ("type mismatch in widening vector shift expression");
3705 debug_generic_expr (lhs_type);
3706 debug_generic_expr (rhs1_type);
3707 debug_generic_expr (rhs2_type);
3708 return true;
3711 return false;
3714 case PLUS_EXPR:
3715 case MINUS_EXPR:
3717 tree lhs_etype = lhs_type;
3718 tree rhs1_etype = rhs1_type;
3719 tree rhs2_etype = rhs2_type;
3720 if (TREE_CODE (lhs_type) == VECTOR_TYPE)
3722 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3723 || TREE_CODE (rhs2_type) != VECTOR_TYPE)
3725 error ("invalid non-vector operands to vector valued plus");
3726 return true;
3728 lhs_etype = TREE_TYPE (lhs_type);
3729 rhs1_etype = TREE_TYPE (rhs1_type);
3730 rhs2_etype = TREE_TYPE (rhs2_type);
3732 if (POINTER_TYPE_P (lhs_etype)
3733 || POINTER_TYPE_P (rhs1_etype)
3734 || POINTER_TYPE_P (rhs2_etype))
3736 error ("invalid (pointer) operands to plus/minus");
3737 return true;
3740 /* Continue with generic binary expression handling. */
3741 break;
3744 case POINTER_PLUS_EXPR:
3746 if (!POINTER_TYPE_P (rhs1_type)
3747 || !useless_type_conversion_p (lhs_type, rhs1_type)
3748 || !ptrofftype_p (rhs2_type))
3750 error ("type mismatch in pointer plus expression");
3751 debug_generic_stmt (lhs_type);
3752 debug_generic_stmt (rhs1_type);
3753 debug_generic_stmt (rhs2_type);
3754 return true;
3757 return false;
3760 case TRUTH_ANDIF_EXPR:
3761 case TRUTH_ORIF_EXPR:
3762 case TRUTH_AND_EXPR:
3763 case TRUTH_OR_EXPR:
3764 case TRUTH_XOR_EXPR:
3766 gcc_unreachable ();
3768 case LT_EXPR:
3769 case LE_EXPR:
3770 case GT_EXPR:
3771 case GE_EXPR:
3772 case EQ_EXPR:
3773 case NE_EXPR:
3774 case UNORDERED_EXPR:
3775 case ORDERED_EXPR:
3776 case UNLT_EXPR:
3777 case UNLE_EXPR:
3778 case UNGT_EXPR:
3779 case UNGE_EXPR:
3780 case UNEQ_EXPR:
3781 case LTGT_EXPR:
3782 /* Comparisons are also binary, but the result type is not
3783 connected to the operand types. */
3784 return verify_gimple_comparison (lhs_type, rhs1, rhs2);
3786 case WIDEN_MULT_EXPR:
3787 if (TREE_CODE (lhs_type) != INTEGER_TYPE)
3788 return true;
3789 return ((2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type))
3790 || (TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type)));
3792 case WIDEN_SUM_EXPR:
3793 case VEC_WIDEN_MULT_HI_EXPR:
3794 case VEC_WIDEN_MULT_LO_EXPR:
3795 case VEC_WIDEN_MULT_EVEN_EXPR:
3796 case VEC_WIDEN_MULT_ODD_EXPR:
3797 case VEC_PACK_TRUNC_EXPR:
3798 case VEC_PACK_SAT_EXPR:
3799 case VEC_PACK_FIX_TRUNC_EXPR:
3800 /* FIXME. */
3801 return false;
3803 case MULT_EXPR:
3804 case MULT_HIGHPART_EXPR:
3805 case TRUNC_DIV_EXPR:
3806 case CEIL_DIV_EXPR:
3807 case FLOOR_DIV_EXPR:
3808 case ROUND_DIV_EXPR:
3809 case TRUNC_MOD_EXPR:
3810 case CEIL_MOD_EXPR:
3811 case FLOOR_MOD_EXPR:
3812 case ROUND_MOD_EXPR:
3813 case RDIV_EXPR:
3814 case EXACT_DIV_EXPR:
3815 case MIN_EXPR:
3816 case MAX_EXPR:
3817 case BIT_IOR_EXPR:
3818 case BIT_XOR_EXPR:
3819 case BIT_AND_EXPR:
3820 /* Continue with generic binary expression handling. */
3821 break;
3823 default:
3824 gcc_unreachable ();
3827 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3828 || !useless_type_conversion_p (lhs_type, rhs2_type))
3830 error ("type mismatch in binary expression");
3831 debug_generic_stmt (lhs_type);
3832 debug_generic_stmt (rhs1_type);
3833 debug_generic_stmt (rhs2_type);
3834 return true;
3837 return false;
3840 /* Verify a gimple assignment statement STMT with a ternary rhs.
3841 Returns true if anything is wrong. */
3843 static bool
3844 verify_gimple_assign_ternary (gimple stmt)
3846 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3847 tree lhs = gimple_assign_lhs (stmt);
3848 tree lhs_type = TREE_TYPE (lhs);
3849 tree rhs1 = gimple_assign_rhs1 (stmt);
3850 tree rhs1_type = TREE_TYPE (rhs1);
3851 tree rhs2 = gimple_assign_rhs2 (stmt);
3852 tree rhs2_type = TREE_TYPE (rhs2);
3853 tree rhs3 = gimple_assign_rhs3 (stmt);
3854 tree rhs3_type = TREE_TYPE (rhs3);
3856 if (!is_gimple_reg (lhs))
3858 error ("non-register as LHS of ternary operation");
3859 return true;
3862 if (((rhs_code == VEC_COND_EXPR || rhs_code == COND_EXPR)
3863 ? !is_gimple_condexpr (rhs1) : !is_gimple_val (rhs1))
3864 || !is_gimple_val (rhs2)
3865 || !is_gimple_val (rhs3))
3867 error ("invalid operands in ternary operation");
3868 return true;
3871 /* First handle operations that involve different types. */
3872 switch (rhs_code)
3874 case WIDEN_MULT_PLUS_EXPR:
3875 case WIDEN_MULT_MINUS_EXPR:
3876 if ((!INTEGRAL_TYPE_P (rhs1_type)
3877 && !FIXED_POINT_TYPE_P (rhs1_type))
3878 || !useless_type_conversion_p (rhs1_type, rhs2_type)
3879 || !useless_type_conversion_p (lhs_type, rhs3_type)
3880 || 2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)
3881 || TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type))
3883 error ("type mismatch in widening multiply-accumulate expression");
3884 debug_generic_expr (lhs_type);
3885 debug_generic_expr (rhs1_type);
3886 debug_generic_expr (rhs2_type);
3887 debug_generic_expr (rhs3_type);
3888 return true;
3890 break;
3892 case FMA_EXPR:
3893 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3894 || !useless_type_conversion_p (lhs_type, rhs2_type)
3895 || !useless_type_conversion_p (lhs_type, rhs3_type))
3897 error ("type mismatch in fused multiply-add expression");
3898 debug_generic_expr (lhs_type);
3899 debug_generic_expr (rhs1_type);
3900 debug_generic_expr (rhs2_type);
3901 debug_generic_expr (rhs3_type);
3902 return true;
3904 break;
3906 case COND_EXPR:
3907 case VEC_COND_EXPR:
3908 if (!useless_type_conversion_p (lhs_type, rhs2_type)
3909 || !useless_type_conversion_p (lhs_type, rhs3_type))
3911 error ("type mismatch in conditional expression");
3912 debug_generic_expr (lhs_type);
3913 debug_generic_expr (rhs2_type);
3914 debug_generic_expr (rhs3_type);
3915 return true;
3917 break;
3919 case VEC_PERM_EXPR:
3920 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3921 || !useless_type_conversion_p (lhs_type, rhs2_type))
3923 error ("type mismatch in vector permute expression");
3924 debug_generic_expr (lhs_type);
3925 debug_generic_expr (rhs1_type);
3926 debug_generic_expr (rhs2_type);
3927 debug_generic_expr (rhs3_type);
3928 return true;
3931 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3932 || TREE_CODE (rhs2_type) != VECTOR_TYPE
3933 || TREE_CODE (rhs3_type) != VECTOR_TYPE)
3935 error ("vector types expected in vector permute expression");
3936 debug_generic_expr (lhs_type);
3937 debug_generic_expr (rhs1_type);
3938 debug_generic_expr (rhs2_type);
3939 debug_generic_expr (rhs3_type);
3940 return true;
3943 if (TYPE_VECTOR_SUBPARTS (rhs1_type) != TYPE_VECTOR_SUBPARTS (rhs2_type)
3944 || TYPE_VECTOR_SUBPARTS (rhs2_type)
3945 != TYPE_VECTOR_SUBPARTS (rhs3_type)
3946 || TYPE_VECTOR_SUBPARTS (rhs3_type)
3947 != TYPE_VECTOR_SUBPARTS (lhs_type))
3949 error ("vectors with different element number found "
3950 "in vector permute expression");
3951 debug_generic_expr (lhs_type);
3952 debug_generic_expr (rhs1_type);
3953 debug_generic_expr (rhs2_type);
3954 debug_generic_expr (rhs3_type);
3955 return true;
3958 if (TREE_CODE (TREE_TYPE (rhs3_type)) != INTEGER_TYPE
3959 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs3_type)))
3960 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1_type))))
3962 error ("invalid mask type in vector permute expression");
3963 debug_generic_expr (lhs_type);
3964 debug_generic_expr (rhs1_type);
3965 debug_generic_expr (rhs2_type);
3966 debug_generic_expr (rhs3_type);
3967 return true;
3970 return false;
3972 case SAD_EXPR:
3973 if (!useless_type_conversion_p (rhs1_type, rhs2_type)
3974 || !useless_type_conversion_p (lhs_type, rhs3_type)
3975 || 2 * GET_MODE_BITSIZE (GET_MODE_INNER
3976 (TYPE_MODE (TREE_TYPE (rhs1_type))))
3977 > GET_MODE_BITSIZE (GET_MODE_INNER
3978 (TYPE_MODE (TREE_TYPE (lhs_type)))))
3980 error ("type mismatch in sad expression");
3981 debug_generic_expr (lhs_type);
3982 debug_generic_expr (rhs1_type);
3983 debug_generic_expr (rhs2_type);
3984 debug_generic_expr (rhs3_type);
3985 return true;
3988 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3989 || TREE_CODE (rhs2_type) != VECTOR_TYPE
3990 || TREE_CODE (rhs3_type) != VECTOR_TYPE)
3992 error ("vector types expected in sad expression");
3993 debug_generic_expr (lhs_type);
3994 debug_generic_expr (rhs1_type);
3995 debug_generic_expr (rhs2_type);
3996 debug_generic_expr (rhs3_type);
3997 return true;
4000 return false;
4002 case DOT_PROD_EXPR:
4003 case REALIGN_LOAD_EXPR:
4004 /* FIXME. */
4005 return false;
4007 default:
4008 gcc_unreachable ();
4010 return false;
4013 /* Verify a gimple assignment statement STMT with a single rhs.
4014 Returns true if anything is wrong. */
4016 static bool
4017 verify_gimple_assign_single (gimple stmt)
4019 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4020 tree lhs = gimple_assign_lhs (stmt);
4021 tree lhs_type = TREE_TYPE (lhs);
4022 tree rhs1 = gimple_assign_rhs1 (stmt);
4023 tree rhs1_type = TREE_TYPE (rhs1);
4024 bool res = false;
4026 if (!useless_type_conversion_p (lhs_type, rhs1_type))
4028 error ("non-trivial conversion at assignment");
4029 debug_generic_expr (lhs_type);
4030 debug_generic_expr (rhs1_type);
4031 return true;
4034 if (gimple_clobber_p (stmt)
4035 && !(DECL_P (lhs) || TREE_CODE (lhs) == MEM_REF))
4037 error ("non-decl/MEM_REF LHS in clobber statement");
4038 debug_generic_expr (lhs);
4039 return true;
4042 if (handled_component_p (lhs)
4043 || TREE_CODE (lhs) == MEM_REF
4044 || TREE_CODE (lhs) == TARGET_MEM_REF)
4045 res |= verify_types_in_gimple_reference (lhs, true);
4047 /* Special codes we cannot handle via their class. */
4048 switch (rhs_code)
4050 case ADDR_EXPR:
4052 tree op = TREE_OPERAND (rhs1, 0);
4053 if (!is_gimple_addressable (op))
4055 error ("invalid operand in unary expression");
4056 return true;
4059 /* Technically there is no longer a need for matching types, but
4060 gimple hygiene asks for this check. In LTO we can end up
4061 combining incompatible units and thus end up with addresses
4062 of globals that change their type to a common one. */
4063 if (!in_lto_p
4064 && !types_compatible_p (TREE_TYPE (op),
4065 TREE_TYPE (TREE_TYPE (rhs1)))
4066 && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
4067 TREE_TYPE (op)))
4069 error ("type mismatch in address expression");
4070 debug_generic_stmt (TREE_TYPE (rhs1));
4071 debug_generic_stmt (TREE_TYPE (op));
4072 return true;
4075 return verify_types_in_gimple_reference (op, true);
4078 /* tcc_reference */
4079 case INDIRECT_REF:
4080 error ("INDIRECT_REF in gimple IL");
4081 return true;
4083 case COMPONENT_REF:
4084 case BIT_FIELD_REF:
4085 case ARRAY_REF:
4086 case ARRAY_RANGE_REF:
4087 case VIEW_CONVERT_EXPR:
4088 case REALPART_EXPR:
4089 case IMAGPART_EXPR:
4090 case TARGET_MEM_REF:
4091 case MEM_REF:
4092 if (!is_gimple_reg (lhs)
4093 && is_gimple_reg_type (TREE_TYPE (lhs)))
4095 error ("invalid rhs for gimple memory store");
4096 debug_generic_stmt (lhs);
4097 debug_generic_stmt (rhs1);
4098 return true;
4100 return res || verify_types_in_gimple_reference (rhs1, false);
4102 /* tcc_constant */
4103 case SSA_NAME:
4104 case INTEGER_CST:
4105 case REAL_CST:
4106 case FIXED_CST:
4107 case COMPLEX_CST:
4108 case VECTOR_CST:
4109 case STRING_CST:
4110 return res;
4112 /* tcc_declaration */
4113 case CONST_DECL:
4114 return res;
4115 case VAR_DECL:
4116 case PARM_DECL:
4117 if (!is_gimple_reg (lhs)
4118 && !is_gimple_reg (rhs1)
4119 && is_gimple_reg_type (TREE_TYPE (lhs)))
4121 error ("invalid rhs for gimple memory store");
4122 debug_generic_stmt (lhs);
4123 debug_generic_stmt (rhs1);
4124 return true;
4126 return res;
4128 case CONSTRUCTOR:
4129 if (TREE_CODE (rhs1_type) == VECTOR_TYPE)
4131 unsigned int i;
4132 tree elt_i, elt_v, elt_t = NULL_TREE;
4134 if (CONSTRUCTOR_NELTS (rhs1) == 0)
4135 return res;
4136 /* For vector CONSTRUCTORs we require that either it is empty
4137 CONSTRUCTOR, or it is a CONSTRUCTOR of smaller vector elements
4138 (then the element count must be correct to cover the whole
4139 outer vector and index must be NULL on all elements, or it is
4140 a CONSTRUCTOR of scalar elements, where we as an exception allow
4141 smaller number of elements (assuming zero filling) and
4142 consecutive indexes as compared to NULL indexes (such
4143 CONSTRUCTORs can appear in the IL from FEs). */
4144 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), i, elt_i, elt_v)
4146 if (elt_t == NULL_TREE)
4148 elt_t = TREE_TYPE (elt_v);
4149 if (TREE_CODE (elt_t) == VECTOR_TYPE)
4151 tree elt_t = TREE_TYPE (elt_v);
4152 if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
4153 TREE_TYPE (elt_t)))
4155 error ("incorrect type of vector CONSTRUCTOR"
4156 " elements");
4157 debug_generic_stmt (rhs1);
4158 return true;
4160 else if (CONSTRUCTOR_NELTS (rhs1)
4161 * TYPE_VECTOR_SUBPARTS (elt_t)
4162 != TYPE_VECTOR_SUBPARTS (rhs1_type))
4164 error ("incorrect number of vector CONSTRUCTOR"
4165 " elements");
4166 debug_generic_stmt (rhs1);
4167 return true;
4170 else if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
4171 elt_t))
4173 error ("incorrect type of vector CONSTRUCTOR elements");
4174 debug_generic_stmt (rhs1);
4175 return true;
4177 else if (CONSTRUCTOR_NELTS (rhs1)
4178 > TYPE_VECTOR_SUBPARTS (rhs1_type))
4180 error ("incorrect number of vector CONSTRUCTOR elements");
4181 debug_generic_stmt (rhs1);
4182 return true;
4185 else if (!useless_type_conversion_p (elt_t, TREE_TYPE (elt_v)))
4187 error ("incorrect type of vector CONSTRUCTOR elements");
4188 debug_generic_stmt (rhs1);
4189 return true;
4191 if (elt_i != NULL_TREE
4192 && (TREE_CODE (elt_t) == VECTOR_TYPE
4193 || TREE_CODE (elt_i) != INTEGER_CST
4194 || compare_tree_int (elt_i, i) != 0))
4196 error ("vector CONSTRUCTOR with non-NULL element index");
4197 debug_generic_stmt (rhs1);
4198 return true;
4202 return res;
4203 case OBJ_TYPE_REF:
4204 case ASSERT_EXPR:
4205 case WITH_SIZE_EXPR:
4206 /* FIXME. */
4207 return res;
4209 default:;
4212 return res;
4215 /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
4216 is a problem, otherwise false. */
4218 static bool
4219 verify_gimple_assign (gimple stmt)
4221 switch (gimple_assign_rhs_class (stmt))
4223 case GIMPLE_SINGLE_RHS:
4224 return verify_gimple_assign_single (stmt);
4226 case GIMPLE_UNARY_RHS:
4227 return verify_gimple_assign_unary (stmt);
4229 case GIMPLE_BINARY_RHS:
4230 return verify_gimple_assign_binary (stmt);
4232 case GIMPLE_TERNARY_RHS:
4233 return verify_gimple_assign_ternary (stmt);
4235 default:
4236 gcc_unreachable ();
4240 /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
4241 is a problem, otherwise false. */
4243 static bool
4244 verify_gimple_return (gimple stmt)
4246 tree op = gimple_return_retval (stmt);
4247 tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
4249 /* We cannot test for present return values as we do not fix up missing
4250 return values from the original source. */
4251 if (op == NULL)
4252 return false;
4254 if (!is_gimple_val (op)
4255 && TREE_CODE (op) != RESULT_DECL)
4257 error ("invalid operand in return statement");
4258 debug_generic_stmt (op);
4259 return true;
4262 if ((TREE_CODE (op) == RESULT_DECL
4263 && DECL_BY_REFERENCE (op))
4264 || (TREE_CODE (op) == SSA_NAME
4265 && SSA_NAME_VAR (op)
4266 && TREE_CODE (SSA_NAME_VAR (op)) == RESULT_DECL
4267 && DECL_BY_REFERENCE (SSA_NAME_VAR (op))))
4268 op = TREE_TYPE (op);
4270 if (!useless_type_conversion_p (restype, TREE_TYPE (op)))
4272 error ("invalid conversion in return statement");
4273 debug_generic_stmt (restype);
4274 debug_generic_stmt (TREE_TYPE (op));
4275 return true;
4278 return false;
4282 /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
4283 is a problem, otherwise false. */
4285 static bool
4286 verify_gimple_goto (gimple stmt)
4288 tree dest = gimple_goto_dest (stmt);
4290 /* ??? We have two canonical forms of direct goto destinations, a
4291 bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
4292 if (TREE_CODE (dest) != LABEL_DECL
4293 && (!is_gimple_val (dest)
4294 || !POINTER_TYPE_P (TREE_TYPE (dest))))
4296 error ("goto destination is neither a label nor a pointer");
4297 return true;
4300 return false;
4303 /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
4304 is a problem, otherwise false. */
4306 static bool
4307 verify_gimple_switch (gimple stmt)
4309 unsigned int i, n;
4310 tree elt, prev_upper_bound = NULL_TREE;
4311 tree index_type, elt_type = NULL_TREE;
4313 if (!is_gimple_val (gimple_switch_index (stmt)))
4315 error ("invalid operand to switch statement");
4316 debug_generic_stmt (gimple_switch_index (stmt));
4317 return true;
4320 index_type = TREE_TYPE (gimple_switch_index (stmt));
4321 if (! INTEGRAL_TYPE_P (index_type))
4323 error ("non-integral type switch statement");
4324 debug_generic_expr (index_type);
4325 return true;
4328 elt = gimple_switch_label (stmt, 0);
4329 if (CASE_LOW (elt) != NULL_TREE || CASE_HIGH (elt) != NULL_TREE)
4331 error ("invalid default case label in switch statement");
4332 debug_generic_expr (elt);
4333 return true;
4336 n = gimple_switch_num_labels (stmt);
4337 for (i = 1; i < n; i++)
4339 elt = gimple_switch_label (stmt, i);
4341 if (! CASE_LOW (elt))
4343 error ("invalid case label in switch statement");
4344 debug_generic_expr (elt);
4345 return true;
4347 if (CASE_HIGH (elt)
4348 && ! tree_int_cst_lt (CASE_LOW (elt), CASE_HIGH (elt)))
4350 error ("invalid case range in switch statement");
4351 debug_generic_expr (elt);
4352 return true;
4355 if (elt_type)
4357 if (TREE_TYPE (CASE_LOW (elt)) != elt_type
4358 || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
4360 error ("type mismatch for case label in switch statement");
4361 debug_generic_expr (elt);
4362 return true;
4365 else
4367 elt_type = TREE_TYPE (CASE_LOW (elt));
4368 if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
4370 error ("type precision mismatch in switch statement");
4371 return true;
4375 if (prev_upper_bound)
4377 if (! tree_int_cst_lt (prev_upper_bound, CASE_LOW (elt)))
4379 error ("case labels not sorted in switch statement");
4380 return true;
4384 prev_upper_bound = CASE_HIGH (elt);
4385 if (! prev_upper_bound)
4386 prev_upper_bound = CASE_LOW (elt);
4389 return false;
4392 /* Verify a gimple debug statement STMT.
4393 Returns true if anything is wrong. */
4395 static bool
4396 verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
4398 /* There isn't much that could be wrong in a gimple debug stmt. A
4399 gimple debug bind stmt, for example, maps a tree, that's usually
4400 a VAR_DECL or a PARM_DECL, but that could also be some scalarized
4401 component or member of an aggregate type, to another tree, that
4402 can be an arbitrary expression. These stmts expand into debug
4403 insns, and are converted to debug notes by var-tracking.c. */
4404 return false;
4407 /* Verify a gimple label statement STMT.
4408 Returns true if anything is wrong. */
4410 static bool
4411 verify_gimple_label (gimple stmt)
4413 tree decl = gimple_label_label (stmt);
4414 int uid;
4415 bool err = false;
4417 if (TREE_CODE (decl) != LABEL_DECL)
4418 return true;
4419 if (!DECL_NONLOCAL (decl) && !FORCED_LABEL (decl)
4420 && DECL_CONTEXT (decl) != current_function_decl)
4422 error ("label's context is not the current function decl");
4423 err |= true;
4426 uid = LABEL_DECL_UID (decl);
4427 if (cfun->cfg
4428 && (uid == -1
4429 || (*label_to_block_map_for_fn (cfun))[uid] != gimple_bb (stmt)))
4431 error ("incorrect entry in label_to_block_map");
4432 err |= true;
4435 uid = EH_LANDING_PAD_NR (decl);
4436 if (uid)
4438 eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
4439 if (decl != lp->post_landing_pad)
4441 error ("incorrect setting of landing pad number");
4442 err |= true;
4446 return err;
4449 /* Verify the GIMPLE statement STMT. Returns true if there is an
4450 error, otherwise false. */
4452 static bool
4453 verify_gimple_stmt (gimple stmt)
4455 switch (gimple_code (stmt))
4457 case GIMPLE_ASSIGN:
4458 return verify_gimple_assign (stmt);
4460 case GIMPLE_LABEL:
4461 return verify_gimple_label (stmt);
4463 case GIMPLE_CALL:
4464 return verify_gimple_call (stmt);
4466 case GIMPLE_COND:
4467 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
4469 error ("invalid comparison code in gimple cond");
4470 return true;
4472 if (!(!gimple_cond_true_label (stmt)
4473 || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
4474 || !(!gimple_cond_false_label (stmt)
4475 || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
4477 error ("invalid labels in gimple cond");
4478 return true;
4481 return verify_gimple_comparison (boolean_type_node,
4482 gimple_cond_lhs (stmt),
4483 gimple_cond_rhs (stmt));
4485 case GIMPLE_GOTO:
4486 return verify_gimple_goto (stmt);
4488 case GIMPLE_SWITCH:
4489 return verify_gimple_switch (stmt);
4491 case GIMPLE_RETURN:
4492 return verify_gimple_return (stmt);
4494 case GIMPLE_ASM:
4495 return false;
4497 case GIMPLE_TRANSACTION:
4498 return verify_gimple_transaction (stmt);
4500 /* Tuples that do not have tree operands. */
4501 case GIMPLE_NOP:
4502 case GIMPLE_PREDICT:
4503 case GIMPLE_RESX:
4504 case GIMPLE_EH_DISPATCH:
4505 case GIMPLE_EH_MUST_NOT_THROW:
4506 return false;
4508 CASE_GIMPLE_OMP:
4509 /* OpenMP directives are validated by the FE and never operated
4510 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
4511 non-gimple expressions when the main index variable has had
4512 its address taken. This does not affect the loop itself
4513 because the header of an GIMPLE_OMP_FOR is merely used to determine
4514 how to setup the parallel iteration. */
4515 return false;
4517 case GIMPLE_DEBUG:
4518 return verify_gimple_debug (stmt);
4520 default:
4521 gcc_unreachable ();
4525 /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
4526 and false otherwise. */
4528 static bool
4529 verify_gimple_phi (gimple phi)
4531 bool err = false;
4532 unsigned i;
4533 tree phi_result = gimple_phi_result (phi);
4534 bool virtual_p;
4536 if (!phi_result)
4538 error ("invalid PHI result");
4539 return true;
4542 virtual_p = virtual_operand_p (phi_result);
4543 if (TREE_CODE (phi_result) != SSA_NAME
4544 || (virtual_p
4545 && SSA_NAME_VAR (phi_result) != gimple_vop (cfun)))
4547 error ("invalid PHI result");
4548 err = true;
4551 for (i = 0; i < gimple_phi_num_args (phi); i++)
4553 tree t = gimple_phi_arg_def (phi, i);
4555 if (!t)
4557 error ("missing PHI def");
4558 err |= true;
4559 continue;
4561 /* Addressable variables do have SSA_NAMEs but they
4562 are not considered gimple values. */
4563 else if ((TREE_CODE (t) == SSA_NAME
4564 && virtual_p != virtual_operand_p (t))
4565 || (virtual_p
4566 && (TREE_CODE (t) != SSA_NAME
4567 || SSA_NAME_VAR (t) != gimple_vop (cfun)))
4568 || (!virtual_p
4569 && !is_gimple_val (t)))
4571 error ("invalid PHI argument");
4572 debug_generic_expr (t);
4573 err |= true;
4575 #ifdef ENABLE_TYPES_CHECKING
4576 if (!useless_type_conversion_p (TREE_TYPE (phi_result), TREE_TYPE (t)))
4578 error ("incompatible types in PHI argument %u", i);
4579 debug_generic_stmt (TREE_TYPE (phi_result));
4580 debug_generic_stmt (TREE_TYPE (t));
4581 err |= true;
4583 #endif
4586 return err;
4589 /* Verify the GIMPLE statements inside the sequence STMTS. */
4591 static bool
4592 verify_gimple_in_seq_2 (gimple_seq stmts)
4594 gimple_stmt_iterator ittr;
4595 bool err = false;
4597 for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
4599 gimple stmt = gsi_stmt (ittr);
4601 switch (gimple_code (stmt))
4603 case GIMPLE_BIND:
4604 err |= verify_gimple_in_seq_2 (gimple_bind_body (stmt));
4605 break;
4607 case GIMPLE_TRY:
4608 err |= verify_gimple_in_seq_2 (gimple_try_eval (stmt));
4609 err |= verify_gimple_in_seq_2 (gimple_try_cleanup (stmt));
4610 break;
4612 case GIMPLE_EH_FILTER:
4613 err |= verify_gimple_in_seq_2 (gimple_eh_filter_failure (stmt));
4614 break;
4616 case GIMPLE_EH_ELSE:
4617 err |= verify_gimple_in_seq_2 (gimple_eh_else_n_body (stmt));
4618 err |= verify_gimple_in_seq_2 (gimple_eh_else_e_body (stmt));
4619 break;
4621 case GIMPLE_CATCH:
4622 err |= verify_gimple_in_seq_2 (gimple_catch_handler (stmt));
4623 break;
4625 case GIMPLE_TRANSACTION:
4626 err |= verify_gimple_transaction (stmt);
4627 break;
4629 default:
4631 bool err2 = verify_gimple_stmt (stmt);
4632 if (err2)
4633 debug_gimple_stmt (stmt);
4634 err |= err2;
4639 return err;
4642 /* Verify the contents of a GIMPLE_TRANSACTION. Returns true if there
4643 is a problem, otherwise false. */
4645 static bool
4646 verify_gimple_transaction (gimple stmt)
4648 tree lab = gimple_transaction_label (stmt);
4649 if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
4650 return true;
4651 return verify_gimple_in_seq_2 (gimple_transaction_body (stmt));
4655 /* Verify the GIMPLE statements inside the statement list STMTS. */
4657 DEBUG_FUNCTION void
4658 verify_gimple_in_seq (gimple_seq stmts)
4660 timevar_push (TV_TREE_STMT_VERIFY);
4661 if (verify_gimple_in_seq_2 (stmts))
4662 internal_error ("verify_gimple failed");
4663 timevar_pop (TV_TREE_STMT_VERIFY);
4666 /* Return true when the T can be shared. */
4668 static bool
4669 tree_node_can_be_shared (tree t)
4671 if (IS_TYPE_OR_DECL_P (t)
4672 || is_gimple_min_invariant (t)
4673 || TREE_CODE (t) == SSA_NAME
4674 || t == error_mark_node
4675 || TREE_CODE (t) == IDENTIFIER_NODE)
4676 return true;
4678 if (TREE_CODE (t) == CASE_LABEL_EXPR)
4679 return true;
4681 if (DECL_P (t))
4682 return true;
4684 return false;
4687 /* Called via walk_tree. Verify tree sharing. */
4689 static tree
4690 verify_node_sharing_1 (tree *tp, int *walk_subtrees, void *data)
4692 hash_set<void *> *visited = (hash_set<void *> *) data;
4694 if (tree_node_can_be_shared (*tp))
4696 *walk_subtrees = false;
4697 return NULL;
4700 if (visited->add (*tp))
4701 return *tp;
4703 return NULL;
4706 /* Called via walk_gimple_stmt. Verify tree sharing. */
4708 static tree
4709 verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
4711 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4712 return verify_node_sharing_1 (tp, walk_subtrees, wi->info);
4715 static bool eh_error_found;
4716 static int
4717 verify_eh_throw_stmt_node (void **slot, void *data)
4719 struct throw_stmt_node *node = (struct throw_stmt_node *)*slot;
4720 hash_set<void *> *visited = (hash_set<void *> *) data;
4722 if (!visited->contains (node->stmt))
4724 error ("dead STMT in EH table");
4725 debug_gimple_stmt (node->stmt);
4726 eh_error_found = true;
4728 return 1;
4731 /* Verify if the location LOCs block is in BLOCKS. */
4733 static bool
4734 verify_location (hash_set<tree> *blocks, location_t loc)
4736 tree block = LOCATION_BLOCK (loc);
4737 if (block != NULL_TREE
4738 && !blocks->contains (block))
4740 error ("location references block not in block tree");
4741 return true;
4743 if (block != NULL_TREE)
4744 return verify_location (blocks, BLOCK_SOURCE_LOCATION (block));
4745 return false;
4748 /* Called via walk_tree. Verify that expressions have no blocks. */
4750 static tree
4751 verify_expr_no_block (tree *tp, int *walk_subtrees, void *)
4753 if (!EXPR_P (*tp))
4755 *walk_subtrees = false;
4756 return NULL;
4759 location_t loc = EXPR_LOCATION (*tp);
4760 if (LOCATION_BLOCK (loc) != NULL)
4761 return *tp;
4763 return NULL;
4766 /* Called via walk_tree. Verify locations of expressions. */
4768 static tree
4769 verify_expr_location_1 (tree *tp, int *walk_subtrees, void *data)
4771 hash_set<tree> *blocks = (hash_set<tree> *) data;
4773 if (TREE_CODE (*tp) == VAR_DECL
4774 && DECL_HAS_DEBUG_EXPR_P (*tp))
4776 tree t = DECL_DEBUG_EXPR (*tp);
4777 tree addr = walk_tree (&t, verify_expr_no_block, NULL, NULL);
4778 if (addr)
4779 return addr;
4781 if ((TREE_CODE (*tp) == VAR_DECL
4782 || TREE_CODE (*tp) == PARM_DECL
4783 || TREE_CODE (*tp) == RESULT_DECL)
4784 && DECL_HAS_VALUE_EXPR_P (*tp))
4786 tree t = DECL_VALUE_EXPR (*tp);
4787 tree addr = walk_tree (&t, verify_expr_no_block, NULL, NULL);
4788 if (addr)
4789 return addr;
4792 if (!EXPR_P (*tp))
4794 *walk_subtrees = false;
4795 return NULL;
4798 location_t loc = EXPR_LOCATION (*tp);
4799 if (verify_location (blocks, loc))
4800 return *tp;
4802 return NULL;
4805 /* Called via walk_gimple_op. Verify locations of expressions. */
4807 static tree
4808 verify_expr_location (tree *tp, int *walk_subtrees, void *data)
4810 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4811 return verify_expr_location_1 (tp, walk_subtrees, wi->info);
4814 /* Insert all subblocks of BLOCK into BLOCKS and recurse. */
4816 static void
4817 collect_subblocks (hash_set<tree> *blocks, tree block)
4819 tree t;
4820 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
4822 blocks->add (t);
4823 collect_subblocks (blocks, t);
4827 /* Verify the GIMPLE statements in the CFG of FN. */
4829 DEBUG_FUNCTION void
4830 verify_gimple_in_cfg (struct function *fn, bool verify_nothrow)
4832 basic_block bb;
4833 bool err = false;
4835 timevar_push (TV_TREE_STMT_VERIFY);
4836 hash_set<void *> visited;
4837 hash_set<gimple> visited_stmts;
4839 /* Collect all BLOCKs referenced by the BLOCK tree of FN. */
4840 hash_set<tree> blocks;
4841 if (DECL_INITIAL (fn->decl))
4843 blocks.add (DECL_INITIAL (fn->decl));
4844 collect_subblocks (&blocks, DECL_INITIAL (fn->decl));
4847 FOR_EACH_BB_FN (bb, fn)
4849 gimple_stmt_iterator gsi;
4851 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4853 gimple phi = gsi_stmt (gsi);
4854 bool err2 = false;
4855 unsigned i;
4857 visited_stmts.add (phi);
4859 if (gimple_bb (phi) != bb)
4861 error ("gimple_bb (phi) is set to a wrong basic block");
4862 err2 = true;
4865 err2 |= verify_gimple_phi (phi);
4867 /* Only PHI arguments have locations. */
4868 if (gimple_location (phi) != UNKNOWN_LOCATION)
4870 error ("PHI node with location");
4871 err2 = true;
4874 for (i = 0; i < gimple_phi_num_args (phi); i++)
4876 tree arg = gimple_phi_arg_def (phi, i);
4877 tree addr = walk_tree (&arg, verify_node_sharing_1,
4878 &visited, NULL);
4879 if (addr)
4881 error ("incorrect sharing of tree nodes");
4882 debug_generic_expr (addr);
4883 err2 |= true;
4885 location_t loc = gimple_phi_arg_location (phi, i);
4886 if (virtual_operand_p (gimple_phi_result (phi))
4887 && loc != UNKNOWN_LOCATION)
4889 error ("virtual PHI with argument locations");
4890 err2 = true;
4892 addr = walk_tree (&arg, verify_expr_location_1, &blocks, NULL);
4893 if (addr)
4895 debug_generic_expr (addr);
4896 err2 = true;
4898 err2 |= verify_location (&blocks, loc);
4901 if (err2)
4902 debug_gimple_stmt (phi);
4903 err |= err2;
4906 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4908 gimple stmt = gsi_stmt (gsi);
4909 bool err2 = false;
4910 struct walk_stmt_info wi;
4911 tree addr;
4912 int lp_nr;
4914 visited_stmts.add (stmt);
4916 if (gimple_bb (stmt) != bb)
4918 error ("gimple_bb (stmt) is set to a wrong basic block");
4919 err2 = true;
4922 err2 |= verify_gimple_stmt (stmt);
4923 err2 |= verify_location (&blocks, gimple_location (stmt));
4925 memset (&wi, 0, sizeof (wi));
4926 wi.info = (void *) &visited;
4927 addr = walk_gimple_op (stmt, verify_node_sharing, &wi);
4928 if (addr)
4930 error ("incorrect sharing of tree nodes");
4931 debug_generic_expr (addr);
4932 err2 |= true;
4935 memset (&wi, 0, sizeof (wi));
4936 wi.info = (void *) &blocks;
4937 addr = walk_gimple_op (stmt, verify_expr_location, &wi);
4938 if (addr)
4940 debug_generic_expr (addr);
4941 err2 |= true;
4944 /* ??? Instead of not checking these stmts at all the walker
4945 should know its context via wi. */
4946 if (!is_gimple_debug (stmt)
4947 && !is_gimple_omp (stmt))
4949 memset (&wi, 0, sizeof (wi));
4950 addr = walk_gimple_op (stmt, verify_expr, &wi);
4951 if (addr)
4953 debug_generic_expr (addr);
4954 inform (gimple_location (stmt), "in statement");
4955 err2 |= true;
4959 /* If the statement is marked as part of an EH region, then it is
4960 expected that the statement could throw. Verify that when we
4961 have optimizations that simplify statements such that we prove
4962 that they cannot throw, that we update other data structures
4963 to match. */
4964 lp_nr = lookup_stmt_eh_lp (stmt);
4965 if (lp_nr > 0)
4967 if (!stmt_could_throw_p (stmt))
4969 if (verify_nothrow)
4971 error ("statement marked for throw, but doesn%'t");
4972 err2 |= true;
4975 else if (!gsi_one_before_end_p (gsi))
4977 error ("statement marked for throw in middle of block");
4978 err2 |= true;
4982 if (err2)
4983 debug_gimple_stmt (stmt);
4984 err |= err2;
4988 eh_error_found = false;
4989 if (get_eh_throw_stmt_table (cfun))
4990 htab_traverse (get_eh_throw_stmt_table (cfun),
4991 verify_eh_throw_stmt_node,
4992 &visited_stmts);
4994 if (err || eh_error_found)
4995 internal_error ("verify_gimple failed");
4997 verify_histograms ();
4998 timevar_pop (TV_TREE_STMT_VERIFY);
5002 /* Verifies that the flow information is OK. */
5004 static int
5005 gimple_verify_flow_info (void)
5007 int err = 0;
5008 basic_block bb;
5009 gimple_stmt_iterator gsi;
5010 gimple stmt;
5011 edge e;
5012 edge_iterator ei;
5014 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
5015 || ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
5017 error ("ENTRY_BLOCK has IL associated with it");
5018 err = 1;
5021 if (EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
5022 || EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
5024 error ("EXIT_BLOCK has IL associated with it");
5025 err = 1;
5028 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5029 if (e->flags & EDGE_FALLTHRU)
5031 error ("fallthru to exit from bb %d", e->src->index);
5032 err = 1;
5035 FOR_EACH_BB_FN (bb, cfun)
5037 bool found_ctrl_stmt = false;
5039 stmt = NULL;
5041 /* Skip labels on the start of basic block. */
5042 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5044 tree label;
5045 gimple prev_stmt = stmt;
5047 stmt = gsi_stmt (gsi);
5049 if (gimple_code (stmt) != GIMPLE_LABEL)
5050 break;
5052 label = gimple_label_label (stmt);
5053 if (prev_stmt && DECL_NONLOCAL (label))
5055 error ("nonlocal label ");
5056 print_generic_expr (stderr, label, 0);
5057 fprintf (stderr, " is not first in a sequence of labels in bb %d",
5058 bb->index);
5059 err = 1;
5062 if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
5064 error ("EH landing pad label ");
5065 print_generic_expr (stderr, label, 0);
5066 fprintf (stderr, " is not first in a sequence of labels in bb %d",
5067 bb->index);
5068 err = 1;
5071 if (label_to_block (label) != bb)
5073 error ("label ");
5074 print_generic_expr (stderr, label, 0);
5075 fprintf (stderr, " to block does not match in bb %d",
5076 bb->index);
5077 err = 1;
5080 if (decl_function_context (label) != current_function_decl)
5082 error ("label ");
5083 print_generic_expr (stderr, label, 0);
5084 fprintf (stderr, " has incorrect context in bb %d",
5085 bb->index);
5086 err = 1;
5090 /* Verify that body of basic block BB is free of control flow. */
5091 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5093 gimple stmt = gsi_stmt (gsi);
5095 if (found_ctrl_stmt)
5097 error ("control flow in the middle of basic block %d",
5098 bb->index);
5099 err = 1;
5102 if (stmt_ends_bb_p (stmt))
5103 found_ctrl_stmt = true;
5105 if (gimple_code (stmt) == GIMPLE_LABEL)
5107 error ("label ");
5108 print_generic_expr (stderr, gimple_label_label (stmt), 0);
5109 fprintf (stderr, " in the middle of basic block %d", bb->index);
5110 err = 1;
5114 gsi = gsi_last_bb (bb);
5115 if (gsi_end_p (gsi))
5116 continue;
5118 stmt = gsi_stmt (gsi);
5120 if (gimple_code (stmt) == GIMPLE_LABEL)
5121 continue;
5123 err |= verify_eh_edges (stmt);
5125 if (is_ctrl_stmt (stmt))
5127 FOR_EACH_EDGE (e, ei, bb->succs)
5128 if (e->flags & EDGE_FALLTHRU)
5130 error ("fallthru edge after a control statement in bb %d",
5131 bb->index);
5132 err = 1;
5136 if (gimple_code (stmt) != GIMPLE_COND)
5138 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
5139 after anything else but if statement. */
5140 FOR_EACH_EDGE (e, ei, bb->succs)
5141 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
5143 error ("true/false edge after a non-GIMPLE_COND in bb %d",
5144 bb->index);
5145 err = 1;
5149 switch (gimple_code (stmt))
5151 case GIMPLE_COND:
5153 edge true_edge;
5154 edge false_edge;
5156 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
5158 if (!true_edge
5159 || !false_edge
5160 || !(true_edge->flags & EDGE_TRUE_VALUE)
5161 || !(false_edge->flags & EDGE_FALSE_VALUE)
5162 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
5163 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
5164 || EDGE_COUNT (bb->succs) >= 3)
5166 error ("wrong outgoing edge flags at end of bb %d",
5167 bb->index);
5168 err = 1;
5171 break;
5173 case GIMPLE_GOTO:
5174 if (simple_goto_p (stmt))
5176 error ("explicit goto at end of bb %d", bb->index);
5177 err = 1;
5179 else
5181 /* FIXME. We should double check that the labels in the
5182 destination blocks have their address taken. */
5183 FOR_EACH_EDGE (e, ei, bb->succs)
5184 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
5185 | EDGE_FALSE_VALUE))
5186 || !(e->flags & EDGE_ABNORMAL))
5188 error ("wrong outgoing edge flags at end of bb %d",
5189 bb->index);
5190 err = 1;
5193 break;
5195 case GIMPLE_CALL:
5196 if (!gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
5197 break;
5198 /* ... fallthru ... */
5199 case GIMPLE_RETURN:
5200 if (!single_succ_p (bb)
5201 || (single_succ_edge (bb)->flags
5202 & (EDGE_FALLTHRU | EDGE_ABNORMAL
5203 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
5205 error ("wrong outgoing edge flags at end of bb %d", bb->index);
5206 err = 1;
5208 if (single_succ (bb) != EXIT_BLOCK_PTR_FOR_FN (cfun))
5210 error ("return edge does not point to exit in bb %d",
5211 bb->index);
5212 err = 1;
5214 break;
5216 case GIMPLE_SWITCH:
5218 tree prev;
5219 edge e;
5220 size_t i, n;
5222 n = gimple_switch_num_labels (stmt);
5224 /* Mark all the destination basic blocks. */
5225 for (i = 0; i < n; ++i)
5227 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
5228 basic_block label_bb = label_to_block (lab);
5229 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
5230 label_bb->aux = (void *)1;
5233 /* Verify that the case labels are sorted. */
5234 prev = gimple_switch_label (stmt, 0);
5235 for (i = 1; i < n; ++i)
5237 tree c = gimple_switch_label (stmt, i);
5238 if (!CASE_LOW (c))
5240 error ("found default case not at the start of "
5241 "case vector");
5242 err = 1;
5243 continue;
5245 if (CASE_LOW (prev)
5246 && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
5248 error ("case labels not sorted: ");
5249 print_generic_expr (stderr, prev, 0);
5250 fprintf (stderr," is greater than ");
5251 print_generic_expr (stderr, c, 0);
5252 fprintf (stderr," but comes before it.\n");
5253 err = 1;
5255 prev = c;
5257 /* VRP will remove the default case if it can prove it will
5258 never be executed. So do not verify there always exists
5259 a default case here. */
5261 FOR_EACH_EDGE (e, ei, bb->succs)
5263 if (!e->dest->aux)
5265 error ("extra outgoing edge %d->%d",
5266 bb->index, e->dest->index);
5267 err = 1;
5270 e->dest->aux = (void *)2;
5271 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
5272 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
5274 error ("wrong outgoing edge flags at end of bb %d",
5275 bb->index);
5276 err = 1;
5280 /* Check that we have all of them. */
5281 for (i = 0; i < n; ++i)
5283 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
5284 basic_block label_bb = label_to_block (lab);
5286 if (label_bb->aux != (void *)2)
5288 error ("missing edge %i->%i", bb->index, label_bb->index);
5289 err = 1;
5293 FOR_EACH_EDGE (e, ei, bb->succs)
5294 e->dest->aux = (void *)0;
5296 break;
5298 case GIMPLE_EH_DISPATCH:
5299 err |= verify_eh_dispatch_edge (stmt);
5300 break;
5302 default:
5303 break;
5307 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
5308 verify_dominators (CDI_DOMINATORS);
5310 return err;
5314 /* Updates phi nodes after creating a forwarder block joined
5315 by edge FALLTHRU. */
5317 static void
5318 gimple_make_forwarder_block (edge fallthru)
5320 edge e;
5321 edge_iterator ei;
5322 basic_block dummy, bb;
5323 tree var;
5324 gimple_stmt_iterator gsi;
5326 dummy = fallthru->src;
5327 bb = fallthru->dest;
5329 if (single_pred_p (bb))
5330 return;
5332 /* If we redirected a branch we must create new PHI nodes at the
5333 start of BB. */
5334 for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
5336 gimple phi, new_phi;
5338 phi = gsi_stmt (gsi);
5339 var = gimple_phi_result (phi);
5340 new_phi = create_phi_node (var, bb);
5341 gimple_phi_set_result (phi, copy_ssa_name (var, phi));
5342 add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
5343 UNKNOWN_LOCATION);
5346 /* Add the arguments we have stored on edges. */
5347 FOR_EACH_EDGE (e, ei, bb->preds)
5349 if (e == fallthru)
5350 continue;
5352 flush_pending_stmts (e);
5357 /* Return a non-special label in the head of basic block BLOCK.
5358 Create one if it doesn't exist. */
5360 tree
5361 gimple_block_label (basic_block bb)
5363 gimple_stmt_iterator i, s = gsi_start_bb (bb);
5364 bool first = true;
5365 tree label;
5366 gimple stmt;
5368 for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
5370 stmt = gsi_stmt (i);
5371 if (gimple_code (stmt) != GIMPLE_LABEL)
5372 break;
5373 label = gimple_label_label (stmt);
5374 if (!DECL_NONLOCAL (label))
5376 if (!first)
5377 gsi_move_before (&i, &s);
5378 return label;
5382 label = create_artificial_label (UNKNOWN_LOCATION);
5383 stmt = gimple_build_label (label);
5384 gsi_insert_before (&s, stmt, GSI_NEW_STMT);
5385 return label;
5389 /* Attempt to perform edge redirection by replacing a possibly complex
5390 jump instruction by a goto or by removing the jump completely.
5391 This can apply only if all edges now point to the same block. The
5392 parameters and return values are equivalent to
5393 redirect_edge_and_branch. */
5395 static edge
5396 gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
5398 basic_block src = e->src;
5399 gimple_stmt_iterator i;
5400 gimple stmt;
5402 /* We can replace or remove a complex jump only when we have exactly
5403 two edges. */
5404 if (EDGE_COUNT (src->succs) != 2
5405 /* Verify that all targets will be TARGET. Specifically, the
5406 edge that is not E must also go to TARGET. */
5407 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
5408 return NULL;
5410 i = gsi_last_bb (src);
5411 if (gsi_end_p (i))
5412 return NULL;
5414 stmt = gsi_stmt (i);
5416 if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
5418 gsi_remove (&i, true);
5419 e = ssa_redirect_edge (e, target);
5420 e->flags = EDGE_FALLTHRU;
5421 return e;
5424 return NULL;
5428 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
5429 edge representing the redirected branch. */
5431 static edge
5432 gimple_redirect_edge_and_branch (edge e, basic_block dest)
5434 basic_block bb = e->src;
5435 gimple_stmt_iterator gsi;
5436 edge ret;
5437 gimple stmt;
5439 if (e->flags & EDGE_ABNORMAL)
5440 return NULL;
5442 if (e->dest == dest)
5443 return NULL;
5445 if (e->flags & EDGE_EH)
5446 return redirect_eh_edge (e, dest);
5448 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5450 ret = gimple_try_redirect_by_replacing_jump (e, dest);
5451 if (ret)
5452 return ret;
5455 gsi = gsi_last_bb (bb);
5456 stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
5458 switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
5460 case GIMPLE_COND:
5461 /* For COND_EXPR, we only need to redirect the edge. */
5462 break;
5464 case GIMPLE_GOTO:
5465 /* No non-abnormal edges should lead from a non-simple goto, and
5466 simple ones should be represented implicitly. */
5467 gcc_unreachable ();
5469 case GIMPLE_SWITCH:
5471 tree label = gimple_block_label (dest);
5472 tree cases = get_cases_for_edge (e, stmt);
5474 /* If we have a list of cases associated with E, then use it
5475 as it's a lot faster than walking the entire case vector. */
5476 if (cases)
5478 edge e2 = find_edge (e->src, dest);
5479 tree last, first;
5481 first = cases;
5482 while (cases)
5484 last = cases;
5485 CASE_LABEL (cases) = label;
5486 cases = CASE_CHAIN (cases);
5489 /* If there was already an edge in the CFG, then we need
5490 to move all the cases associated with E to E2. */
5491 if (e2)
5493 tree cases2 = get_cases_for_edge (e2, stmt);
5495 CASE_CHAIN (last) = CASE_CHAIN (cases2);
5496 CASE_CHAIN (cases2) = first;
5498 bitmap_set_bit (touched_switch_bbs, gimple_bb (stmt)->index);
5500 else
5502 size_t i, n = gimple_switch_num_labels (stmt);
5504 for (i = 0; i < n; i++)
5506 tree elt = gimple_switch_label (stmt, i);
5507 if (label_to_block (CASE_LABEL (elt)) == e->dest)
5508 CASE_LABEL (elt) = label;
5512 break;
5514 case GIMPLE_ASM:
5516 int i, n = gimple_asm_nlabels (stmt);
5517 tree label = NULL;
5519 for (i = 0; i < n; ++i)
5521 tree cons = gimple_asm_label_op (stmt, i);
5522 if (label_to_block (TREE_VALUE (cons)) == e->dest)
5524 if (!label)
5525 label = gimple_block_label (dest);
5526 TREE_VALUE (cons) = label;
5530 /* If we didn't find any label matching the former edge in the
5531 asm labels, we must be redirecting the fallthrough
5532 edge. */
5533 gcc_assert (label || (e->flags & EDGE_FALLTHRU));
5535 break;
5537 case GIMPLE_RETURN:
5538 gsi_remove (&gsi, true);
5539 e->flags |= EDGE_FALLTHRU;
5540 break;
5542 case GIMPLE_OMP_RETURN:
5543 case GIMPLE_OMP_CONTINUE:
5544 case GIMPLE_OMP_SECTIONS_SWITCH:
5545 case GIMPLE_OMP_FOR:
5546 /* The edges from OMP constructs can be simply redirected. */
5547 break;
5549 case GIMPLE_EH_DISPATCH:
5550 if (!(e->flags & EDGE_FALLTHRU))
5551 redirect_eh_dispatch_edge (stmt, e, dest);
5552 break;
5554 case GIMPLE_TRANSACTION:
5555 /* The ABORT edge has a stored label associated with it, otherwise
5556 the edges are simply redirectable. */
5557 if (e->flags == 0)
5558 gimple_transaction_set_label (stmt, gimple_block_label (dest));
5559 break;
5561 default:
5562 /* Otherwise it must be a fallthru edge, and we don't need to
5563 do anything besides redirecting it. */
5564 gcc_assert (e->flags & EDGE_FALLTHRU);
5565 break;
5568 /* Update/insert PHI nodes as necessary. */
5570 /* Now update the edges in the CFG. */
5571 e = ssa_redirect_edge (e, dest);
5573 return e;
5576 /* Returns true if it is possible to remove edge E by redirecting
5577 it to the destination of the other edge from E->src. */
5579 static bool
5580 gimple_can_remove_branch_p (const_edge e)
5582 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
5583 return false;
5585 return true;
5588 /* Simple wrapper, as we can always redirect fallthru edges. */
5590 static basic_block
5591 gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
5593 e = gimple_redirect_edge_and_branch (e, dest);
5594 gcc_assert (e);
5596 return NULL;
5600 /* Splits basic block BB after statement STMT (but at least after the
5601 labels). If STMT is NULL, BB is split just after the labels. */
5603 static basic_block
5604 gimple_split_block (basic_block bb, void *stmt)
5606 gimple_stmt_iterator gsi;
5607 gimple_stmt_iterator gsi_tgt;
5608 gimple act;
5609 gimple_seq list;
5610 basic_block new_bb;
5611 edge e;
5612 edge_iterator ei;
5614 new_bb = create_empty_bb (bb);
5616 /* Redirect the outgoing edges. */
5617 new_bb->succs = bb->succs;
5618 bb->succs = NULL;
5619 FOR_EACH_EDGE (e, ei, new_bb->succs)
5620 e->src = new_bb;
5622 if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
5623 stmt = NULL;
5625 /* Move everything from GSI to the new basic block. */
5626 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5628 act = gsi_stmt (gsi);
5629 if (gimple_code (act) == GIMPLE_LABEL)
5630 continue;
5632 if (!stmt)
5633 break;
5635 if (stmt == act)
5637 gsi_next (&gsi);
5638 break;
5642 if (gsi_end_p (gsi))
5643 return new_bb;
5645 /* Split the statement list - avoid re-creating new containers as this
5646 brings ugly quadratic memory consumption in the inliner.
5647 (We are still quadratic since we need to update stmt BB pointers,
5648 sadly.) */
5649 gsi_split_seq_before (&gsi, &list);
5650 set_bb_seq (new_bb, list);
5651 for (gsi_tgt = gsi_start (list);
5652 !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
5653 gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
5655 return new_bb;
5659 /* Moves basic block BB after block AFTER. */
5661 static bool
5662 gimple_move_block_after (basic_block bb, basic_block after)
5664 if (bb->prev_bb == after)
5665 return true;
5667 unlink_block (bb);
5668 link_block (bb, after);
5670 return true;
5674 /* Return TRUE if block BB has no executable statements, otherwise return
5675 FALSE. */
5677 static bool
5678 gimple_empty_block_p (basic_block bb)
5680 /* BB must have no executable statements. */
5681 gimple_stmt_iterator gsi = gsi_after_labels (bb);
5682 if (phi_nodes (bb))
5683 return false;
5684 if (gsi_end_p (gsi))
5685 return true;
5686 if (is_gimple_debug (gsi_stmt (gsi)))
5687 gsi_next_nondebug (&gsi);
5688 return gsi_end_p (gsi);
5692 /* Split a basic block if it ends with a conditional branch and if the
5693 other part of the block is not empty. */
5695 static basic_block
5696 gimple_split_block_before_cond_jump (basic_block bb)
5698 gimple last, split_point;
5699 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
5700 if (gsi_end_p (gsi))
5701 return NULL;
5702 last = gsi_stmt (gsi);
5703 if (gimple_code (last) != GIMPLE_COND
5704 && gimple_code (last) != GIMPLE_SWITCH)
5705 return NULL;
5706 gsi_prev_nondebug (&gsi);
5707 split_point = gsi_stmt (gsi);
5708 return split_block (bb, split_point)->dest;
5712 /* Return true if basic_block can be duplicated. */
5714 static bool
5715 gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
5717 return true;
5720 /* Create a duplicate of the basic block BB. NOTE: This does not
5721 preserve SSA form. */
5723 static basic_block
5724 gimple_duplicate_bb (basic_block bb)
5726 basic_block new_bb;
5727 gimple_stmt_iterator gsi, gsi_tgt;
5728 gimple_seq phis = phi_nodes (bb);
5729 gimple phi, stmt, copy;
5731 new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
5733 /* Copy the PHI nodes. We ignore PHI node arguments here because
5734 the incoming edges have not been setup yet. */
5735 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
5737 phi = gsi_stmt (gsi);
5738 copy = create_phi_node (NULL_TREE, new_bb);
5739 create_new_def_for (gimple_phi_result (phi), copy,
5740 gimple_phi_result_ptr (copy));
5741 gimple_set_uid (copy, gimple_uid (phi));
5744 gsi_tgt = gsi_start_bb (new_bb);
5745 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5747 def_operand_p def_p;
5748 ssa_op_iter op_iter;
5749 tree lhs;
5751 stmt = gsi_stmt (gsi);
5752 if (gimple_code (stmt) == GIMPLE_LABEL)
5753 continue;
5755 /* Don't duplicate label debug stmts. */
5756 if (gimple_debug_bind_p (stmt)
5757 && TREE_CODE (gimple_debug_bind_get_var (stmt))
5758 == LABEL_DECL)
5759 continue;
5761 /* Create a new copy of STMT and duplicate STMT's virtual
5762 operands. */
5763 copy = gimple_copy (stmt);
5764 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
5766 maybe_duplicate_eh_stmt (copy, stmt);
5767 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
5769 /* When copying around a stmt writing into a local non-user
5770 aggregate, make sure it won't share stack slot with other
5771 vars. */
5772 lhs = gimple_get_lhs (stmt);
5773 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5775 tree base = get_base_address (lhs);
5776 if (base
5777 && (TREE_CODE (base) == VAR_DECL
5778 || TREE_CODE (base) == RESULT_DECL)
5779 && DECL_IGNORED_P (base)
5780 && !TREE_STATIC (base)
5781 && !DECL_EXTERNAL (base)
5782 && (TREE_CODE (base) != VAR_DECL
5783 || !DECL_HAS_VALUE_EXPR_P (base)))
5784 DECL_NONSHAREABLE (base) = 1;
5787 /* Create new names for all the definitions created by COPY and
5788 add replacement mappings for each new name. */
5789 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
5790 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
5793 return new_bb;
5796 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
5798 static void
5799 add_phi_args_after_copy_edge (edge e_copy)
5801 basic_block bb, bb_copy = e_copy->src, dest;
5802 edge e;
5803 edge_iterator ei;
5804 gimple phi, phi_copy;
5805 tree def;
5806 gimple_stmt_iterator psi, psi_copy;
5808 if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
5809 return;
5811 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
5813 if (e_copy->dest->flags & BB_DUPLICATED)
5814 dest = get_bb_original (e_copy->dest);
5815 else
5816 dest = e_copy->dest;
5818 e = find_edge (bb, dest);
5819 if (!e)
5821 /* During loop unrolling the target of the latch edge is copied.
5822 In this case we are not looking for edge to dest, but to
5823 duplicated block whose original was dest. */
5824 FOR_EACH_EDGE (e, ei, bb->succs)
5826 if ((e->dest->flags & BB_DUPLICATED)
5827 && get_bb_original (e->dest) == dest)
5828 break;
5831 gcc_assert (e != NULL);
5834 for (psi = gsi_start_phis (e->dest),
5835 psi_copy = gsi_start_phis (e_copy->dest);
5836 !gsi_end_p (psi);
5837 gsi_next (&psi), gsi_next (&psi_copy))
5839 phi = gsi_stmt (psi);
5840 phi_copy = gsi_stmt (psi_copy);
5841 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5842 add_phi_arg (phi_copy, def, e_copy,
5843 gimple_phi_arg_location_from_edge (phi, e));
5848 /* Basic block BB_COPY was created by code duplication. Add phi node
5849 arguments for edges going out of BB_COPY. The blocks that were
5850 duplicated have BB_DUPLICATED set. */
5852 void
5853 add_phi_args_after_copy_bb (basic_block bb_copy)
5855 edge e_copy;
5856 edge_iterator ei;
5858 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
5860 add_phi_args_after_copy_edge (e_copy);
5864 /* Blocks in REGION_COPY array of length N_REGION were created by
5865 duplication of basic blocks. Add phi node arguments for edges
5866 going from these blocks. If E_COPY is not NULL, also add
5867 phi node arguments for its destination.*/
5869 void
5870 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
5871 edge e_copy)
5873 unsigned i;
5875 for (i = 0; i < n_region; i++)
5876 region_copy[i]->flags |= BB_DUPLICATED;
5878 for (i = 0; i < n_region; i++)
5879 add_phi_args_after_copy_bb (region_copy[i]);
5880 if (e_copy)
5881 add_phi_args_after_copy_edge (e_copy);
5883 for (i = 0; i < n_region; i++)
5884 region_copy[i]->flags &= ~BB_DUPLICATED;
5887 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5888 important exit edge EXIT. By important we mean that no SSA name defined
5889 inside region is live over the other exit edges of the region. All entry
5890 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5891 to the duplicate of the region. Dominance and loop information is
5892 updated if UPDATE_DOMINANCE is true, but not the SSA web. If
5893 UPDATE_DOMINANCE is false then we assume that the caller will update the
5894 dominance information after calling this function. The new basic
5895 blocks are stored to REGION_COPY in the same order as they had in REGION,
5896 provided that REGION_COPY is not NULL.
5897 The function returns false if it is unable to copy the region,
5898 true otherwise. */
5900 bool
5901 gimple_duplicate_sese_region (edge entry, edge exit,
5902 basic_block *region, unsigned n_region,
5903 basic_block *region_copy,
5904 bool update_dominance)
5906 unsigned i;
5907 bool free_region_copy = false, copying_header = false;
5908 struct loop *loop = entry->dest->loop_father;
5909 edge exit_copy;
5910 vec<basic_block> doms;
5911 edge redirected;
5912 int total_freq = 0, entry_freq = 0;
5913 gcov_type total_count = 0, entry_count = 0;
5915 if (!can_copy_bbs_p (region, n_region))
5916 return false;
5918 /* Some sanity checking. Note that we do not check for all possible
5919 missuses of the functions. I.e. if you ask to copy something weird,
5920 it will work, but the state of structures probably will not be
5921 correct. */
5922 for (i = 0; i < n_region; i++)
5924 /* We do not handle subloops, i.e. all the blocks must belong to the
5925 same loop. */
5926 if (region[i]->loop_father != loop)
5927 return false;
5929 if (region[i] != entry->dest
5930 && region[i] == loop->header)
5931 return false;
5934 /* In case the function is used for loop header copying (which is the primary
5935 use), ensure that EXIT and its copy will be new latch and entry edges. */
5936 if (loop->header == entry->dest)
5938 copying_header = true;
5940 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5941 return false;
5943 for (i = 0; i < n_region; i++)
5944 if (region[i] != exit->src
5945 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5946 return false;
5949 initialize_original_copy_tables ();
5951 if (copying_header)
5952 set_loop_copy (loop, loop_outer (loop));
5953 else
5954 set_loop_copy (loop, loop);
5956 if (!region_copy)
5958 region_copy = XNEWVEC (basic_block, n_region);
5959 free_region_copy = true;
5962 /* Record blocks outside the region that are dominated by something
5963 inside. */
5964 if (update_dominance)
5966 doms.create (0);
5967 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
5970 if (entry->dest->count)
5972 total_count = entry->dest->count;
5973 entry_count = entry->count;
5974 /* Fix up corner cases, to avoid division by zero or creation of negative
5975 frequencies. */
5976 if (entry_count > total_count)
5977 entry_count = total_count;
5979 else
5981 total_freq = entry->dest->frequency;
5982 entry_freq = EDGE_FREQUENCY (entry);
5983 /* Fix up corner cases, to avoid division by zero or creation of negative
5984 frequencies. */
5985 if (total_freq == 0)
5986 total_freq = 1;
5987 else if (entry_freq > total_freq)
5988 entry_freq = total_freq;
5991 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
5992 split_edge_bb_loc (entry), update_dominance);
5993 if (total_count)
5995 scale_bbs_frequencies_gcov_type (region, n_region,
5996 total_count - entry_count,
5997 total_count);
5998 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
5999 total_count);
6001 else
6003 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
6004 total_freq);
6005 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
6008 if (copying_header)
6010 loop->header = exit->dest;
6011 loop->latch = exit->src;
6014 /* Redirect the entry and add the phi node arguments. */
6015 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
6016 gcc_assert (redirected != NULL);
6017 flush_pending_stmts (entry);
6019 /* Concerning updating of dominators: We must recount dominators
6020 for entry block and its copy. Anything that is outside of the
6021 region, but was dominated by something inside needs recounting as
6022 well. */
6023 if (update_dominance)
6025 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
6026 doms.safe_push (get_bb_original (entry->dest));
6027 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
6028 doms.release ();
6031 /* Add the other PHI node arguments. */
6032 add_phi_args_after_copy (region_copy, n_region, NULL);
6034 if (free_region_copy)
6035 free (region_copy);
6037 free_original_copy_tables ();
6038 return true;
6041 /* Checks if BB is part of the region defined by N_REGION BBS. */
6042 static bool
6043 bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
6045 unsigned int n;
6047 for (n = 0; n < n_region; n++)
6049 if (bb == bbs[n])
6050 return true;
6052 return false;
6055 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
6056 are stored to REGION_COPY in the same order in that they appear
6057 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
6058 the region, EXIT an exit from it. The condition guarding EXIT
6059 is moved to ENTRY. Returns true if duplication succeeds, false
6060 otherwise.
6062 For example,
6064 some_code;
6065 if (cond)
6067 else
6070 is transformed to
6072 if (cond)
6074 some_code;
6077 else
6079 some_code;
6084 bool
6085 gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
6086 basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
6087 basic_block *region_copy ATTRIBUTE_UNUSED)
6089 unsigned i;
6090 bool free_region_copy = false;
6091 struct loop *loop = exit->dest->loop_father;
6092 struct loop *orig_loop = entry->dest->loop_father;
6093 basic_block switch_bb, entry_bb, nentry_bb;
6094 vec<basic_block> doms;
6095 int total_freq = 0, exit_freq = 0;
6096 gcov_type total_count = 0, exit_count = 0;
6097 edge exits[2], nexits[2], e;
6098 gimple_stmt_iterator gsi;
6099 gimple cond_stmt;
6100 edge sorig, snew;
6101 basic_block exit_bb;
6102 gimple_stmt_iterator psi;
6103 gimple phi;
6104 tree def;
6105 struct loop *target, *aloop, *cloop;
6107 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
6108 exits[0] = exit;
6109 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
6111 if (!can_copy_bbs_p (region, n_region))
6112 return false;
6114 initialize_original_copy_tables ();
6115 set_loop_copy (orig_loop, loop);
6117 target= loop;
6118 for (aloop = orig_loop->inner; aloop; aloop = aloop->next)
6120 if (bb_part_of_region_p (aloop->header, region, n_region))
6122 cloop = duplicate_loop (aloop, target);
6123 duplicate_subloops (aloop, cloop);
6127 if (!region_copy)
6129 region_copy = XNEWVEC (basic_block, n_region);
6130 free_region_copy = true;
6133 gcc_assert (!need_ssa_update_p (cfun));
6135 /* Record blocks outside the region that are dominated by something
6136 inside. */
6137 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
6139 if (exit->src->count)
6141 total_count = exit->src->count;
6142 exit_count = exit->count;
6143 /* Fix up corner cases, to avoid division by zero or creation of negative
6144 frequencies. */
6145 if (exit_count > total_count)
6146 exit_count = total_count;
6148 else
6150 total_freq = exit->src->frequency;
6151 exit_freq = EDGE_FREQUENCY (exit);
6152 /* Fix up corner cases, to avoid division by zero or creation of negative
6153 frequencies. */
6154 if (total_freq == 0)
6155 total_freq = 1;
6156 if (exit_freq > total_freq)
6157 exit_freq = total_freq;
6160 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
6161 split_edge_bb_loc (exit), true);
6162 if (total_count)
6164 scale_bbs_frequencies_gcov_type (region, n_region,
6165 total_count - exit_count,
6166 total_count);
6167 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
6168 total_count);
6170 else
6172 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
6173 total_freq);
6174 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
6177 /* Create the switch block, and put the exit condition to it. */
6178 entry_bb = entry->dest;
6179 nentry_bb = get_bb_copy (entry_bb);
6180 if (!last_stmt (entry->src)
6181 || !stmt_ends_bb_p (last_stmt (entry->src)))
6182 switch_bb = entry->src;
6183 else
6184 switch_bb = split_edge (entry);
6185 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
6187 gsi = gsi_last_bb (switch_bb);
6188 cond_stmt = last_stmt (exit->src);
6189 gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
6190 cond_stmt = gimple_copy (cond_stmt);
6192 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
6194 sorig = single_succ_edge (switch_bb);
6195 sorig->flags = exits[1]->flags;
6196 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
6198 /* Register the new edge from SWITCH_BB in loop exit lists. */
6199 rescan_loop_exit (snew, true, false);
6201 /* Add the PHI node arguments. */
6202 add_phi_args_after_copy (region_copy, n_region, snew);
6204 /* Get rid of now superfluous conditions and associated edges (and phi node
6205 arguments). */
6206 exit_bb = exit->dest;
6208 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
6209 PENDING_STMT (e) = NULL;
6211 /* The latch of ORIG_LOOP was copied, and so was the backedge
6212 to the original header. We redirect this backedge to EXIT_BB. */
6213 for (i = 0; i < n_region; i++)
6214 if (get_bb_original (region_copy[i]) == orig_loop->latch)
6216 gcc_assert (single_succ_edge (region_copy[i]));
6217 e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
6218 PENDING_STMT (e) = NULL;
6219 for (psi = gsi_start_phis (exit_bb);
6220 !gsi_end_p (psi);
6221 gsi_next (&psi))
6223 phi = gsi_stmt (psi);
6224 def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
6225 add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
6228 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
6229 PENDING_STMT (e) = NULL;
6231 /* Anything that is outside of the region, but was dominated by something
6232 inside needs to update dominance info. */
6233 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
6234 doms.release ();
6235 /* Update the SSA web. */
6236 update_ssa (TODO_update_ssa);
6238 if (free_region_copy)
6239 free (region_copy);
6241 free_original_copy_tables ();
6242 return true;
6245 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
6246 adding blocks when the dominator traversal reaches EXIT. This
6247 function silently assumes that ENTRY strictly dominates EXIT. */
6249 void
6250 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
6251 vec<basic_block> *bbs_p)
6253 basic_block son;
6255 for (son = first_dom_son (CDI_DOMINATORS, entry);
6256 son;
6257 son = next_dom_son (CDI_DOMINATORS, son))
6259 bbs_p->safe_push (son);
6260 if (son != exit)
6261 gather_blocks_in_sese_region (son, exit, bbs_p);
6265 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
6266 The duplicates are recorded in VARS_MAP. */
6268 static void
6269 replace_by_duplicate_decl (tree *tp, hash_map<tree, tree> *vars_map,
6270 tree to_context)
6272 tree t = *tp, new_t;
6273 struct function *f = DECL_STRUCT_FUNCTION (to_context);
6275 if (DECL_CONTEXT (t) == to_context)
6276 return;
6278 bool existed;
6279 tree &loc = vars_map->get_or_insert (t, &existed);
6281 if (!existed)
6283 if (SSA_VAR_P (t))
6285 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
6286 add_local_decl (f, new_t);
6288 else
6290 gcc_assert (TREE_CODE (t) == CONST_DECL);
6291 new_t = copy_node (t);
6293 DECL_CONTEXT (new_t) = to_context;
6295 loc = new_t;
6297 else
6298 new_t = loc;
6300 *tp = new_t;
6304 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
6305 VARS_MAP maps old ssa names and var_decls to the new ones. */
6307 static tree
6308 replace_ssa_name (tree name, hash_map<tree, tree> *vars_map,
6309 tree to_context)
6311 tree new_name;
6313 gcc_assert (!virtual_operand_p (name));
6315 tree *loc = vars_map->get (name);
6317 if (!loc)
6319 tree decl = SSA_NAME_VAR (name);
6320 if (decl)
6322 replace_by_duplicate_decl (&decl, vars_map, to_context);
6323 new_name = make_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
6324 decl, SSA_NAME_DEF_STMT (name));
6325 if (SSA_NAME_IS_DEFAULT_DEF (name))
6326 set_ssa_default_def (DECL_STRUCT_FUNCTION (to_context),
6327 decl, new_name);
6329 else
6330 new_name = copy_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
6331 name, SSA_NAME_DEF_STMT (name));
6333 vars_map->put (name, new_name);
6335 else
6336 new_name = *loc;
6338 return new_name;
6341 struct move_stmt_d
6343 tree orig_block;
6344 tree new_block;
6345 tree from_context;
6346 tree to_context;
6347 hash_map<tree, tree> *vars_map;
6348 htab_t new_label_map;
6349 hash_map<void *, void *> *eh_map;
6350 bool remap_decls_p;
6353 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
6354 contained in *TP if it has been ORIG_BLOCK previously and change the
6355 DECL_CONTEXT of every local variable referenced in *TP. */
6357 static tree
6358 move_stmt_op (tree *tp, int *walk_subtrees, void *data)
6360 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6361 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6362 tree t = *tp;
6364 if (EXPR_P (t))
6366 tree block = TREE_BLOCK (t);
6367 if (block == p->orig_block
6368 || (p->orig_block == NULL_TREE
6369 && block != NULL_TREE))
6370 TREE_SET_BLOCK (t, p->new_block);
6371 #ifdef ENABLE_CHECKING
6372 else if (block != NULL_TREE)
6374 while (block && TREE_CODE (block) == BLOCK && block != p->orig_block)
6375 block = BLOCK_SUPERCONTEXT (block);
6376 gcc_assert (block == p->orig_block);
6378 #endif
6380 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
6382 if (TREE_CODE (t) == SSA_NAME)
6383 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
6384 else if (TREE_CODE (t) == LABEL_DECL)
6386 if (p->new_label_map)
6388 struct tree_map in, *out;
6389 in.base.from = t;
6390 out = (struct tree_map *)
6391 htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
6392 if (out)
6393 *tp = t = out->to;
6396 DECL_CONTEXT (t) = p->to_context;
6398 else if (p->remap_decls_p)
6400 /* Replace T with its duplicate. T should no longer appear in the
6401 parent function, so this looks wasteful; however, it may appear
6402 in referenced_vars, and more importantly, as virtual operands of
6403 statements, and in alias lists of other variables. It would be
6404 quite difficult to expunge it from all those places. ??? It might
6405 suffice to do this for addressable variables. */
6406 if ((TREE_CODE (t) == VAR_DECL
6407 && !is_global_var (t))
6408 || TREE_CODE (t) == CONST_DECL)
6409 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
6411 *walk_subtrees = 0;
6413 else if (TYPE_P (t))
6414 *walk_subtrees = 0;
6416 return NULL_TREE;
6419 /* Helper for move_stmt_r. Given an EH region number for the source
6420 function, map that to the duplicate EH regio number in the dest. */
6422 static int
6423 move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
6425 eh_region old_r, new_r;
6427 old_r = get_eh_region_from_number (old_nr);
6428 new_r = static_cast<eh_region> (*p->eh_map->get (old_r));
6430 return new_r->index;
6433 /* Similar, but operate on INTEGER_CSTs. */
6435 static tree
6436 move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
6438 int old_nr, new_nr;
6440 old_nr = tree_to_shwi (old_t_nr);
6441 new_nr = move_stmt_eh_region_nr (old_nr, p);
6443 return build_int_cst (integer_type_node, new_nr);
6446 /* Like move_stmt_op, but for gimple statements.
6448 Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
6449 contained in the current statement in *GSI_P and change the
6450 DECL_CONTEXT of every local variable referenced in the current
6451 statement. */
6453 static tree
6454 move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
6455 struct walk_stmt_info *wi)
6457 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6458 gimple stmt = gsi_stmt (*gsi_p);
6459 tree block = gimple_block (stmt);
6461 if (block == p->orig_block
6462 || (p->orig_block == NULL_TREE
6463 && block != NULL_TREE))
6464 gimple_set_block (stmt, p->new_block);
6466 switch (gimple_code (stmt))
6468 case GIMPLE_CALL:
6469 /* Remap the region numbers for __builtin_eh_{pointer,filter}. */
6471 tree r, fndecl = gimple_call_fndecl (stmt);
6472 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
6473 switch (DECL_FUNCTION_CODE (fndecl))
6475 case BUILT_IN_EH_COPY_VALUES:
6476 r = gimple_call_arg (stmt, 1);
6477 r = move_stmt_eh_region_tree_nr (r, p);
6478 gimple_call_set_arg (stmt, 1, r);
6479 /* FALLTHRU */
6481 case BUILT_IN_EH_POINTER:
6482 case BUILT_IN_EH_FILTER:
6483 r = gimple_call_arg (stmt, 0);
6484 r = move_stmt_eh_region_tree_nr (r, p);
6485 gimple_call_set_arg (stmt, 0, r);
6486 break;
6488 default:
6489 break;
6492 break;
6494 case GIMPLE_RESX:
6496 int r = gimple_resx_region (stmt);
6497 r = move_stmt_eh_region_nr (r, p);
6498 gimple_resx_set_region (stmt, r);
6500 break;
6502 case GIMPLE_EH_DISPATCH:
6504 int r = gimple_eh_dispatch_region (stmt);
6505 r = move_stmt_eh_region_nr (r, p);
6506 gimple_eh_dispatch_set_region (stmt, r);
6508 break;
6510 case GIMPLE_OMP_RETURN:
6511 case GIMPLE_OMP_CONTINUE:
6512 break;
6513 default:
6514 if (is_gimple_omp (stmt))
6516 /* Do not remap variables inside OMP directives. Variables
6517 referenced in clauses and directive header belong to the
6518 parent function and should not be moved into the child
6519 function. */
6520 bool save_remap_decls_p = p->remap_decls_p;
6521 p->remap_decls_p = false;
6522 *handled_ops_p = true;
6524 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), move_stmt_r,
6525 move_stmt_op, wi);
6527 p->remap_decls_p = save_remap_decls_p;
6529 break;
6532 return NULL_TREE;
6535 /* Move basic block BB from function CFUN to function DEST_FN. The
6536 block is moved out of the original linked list and placed after
6537 block AFTER in the new list. Also, the block is removed from the
6538 original array of blocks and placed in DEST_FN's array of blocks.
6539 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
6540 updated to reflect the moved edges.
6542 The local variables are remapped to new instances, VARS_MAP is used
6543 to record the mapping. */
6545 static void
6546 move_block_to_fn (struct function *dest_cfun, basic_block bb,
6547 basic_block after, bool update_edge_count_p,
6548 struct move_stmt_d *d)
6550 struct control_flow_graph *cfg;
6551 edge_iterator ei;
6552 edge e;
6553 gimple_stmt_iterator si;
6554 unsigned old_len, new_len;
6556 /* Remove BB from dominance structures. */
6557 delete_from_dominance_info (CDI_DOMINATORS, bb);
6559 /* Move BB from its current loop to the copy in the new function. */
6560 if (current_loops)
6562 struct loop *new_loop = (struct loop *)bb->loop_father->aux;
6563 if (new_loop)
6564 bb->loop_father = new_loop;
6567 /* Link BB to the new linked list. */
6568 move_block_after (bb, after);
6570 /* Update the edge count in the corresponding flowgraphs. */
6571 if (update_edge_count_p)
6572 FOR_EACH_EDGE (e, ei, bb->succs)
6574 cfun->cfg->x_n_edges--;
6575 dest_cfun->cfg->x_n_edges++;
6578 /* Remove BB from the original basic block array. */
6579 (*cfun->cfg->x_basic_block_info)[bb->index] = NULL;
6580 cfun->cfg->x_n_basic_blocks--;
6582 /* Grow DEST_CFUN's basic block array if needed. */
6583 cfg = dest_cfun->cfg;
6584 cfg->x_n_basic_blocks++;
6585 if (bb->index >= cfg->x_last_basic_block)
6586 cfg->x_last_basic_block = bb->index + 1;
6588 old_len = vec_safe_length (cfg->x_basic_block_info);
6589 if ((unsigned) cfg->x_last_basic_block >= old_len)
6591 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
6592 vec_safe_grow_cleared (cfg->x_basic_block_info, new_len);
6595 (*cfg->x_basic_block_info)[bb->index] = bb;
6597 /* Remap the variables in phi nodes. */
6598 for (si = gsi_start_phis (bb); !gsi_end_p (si); )
6600 gimple phi = gsi_stmt (si);
6601 use_operand_p use;
6602 tree op = PHI_RESULT (phi);
6603 ssa_op_iter oi;
6604 unsigned i;
6606 if (virtual_operand_p (op))
6608 /* Remove the phi nodes for virtual operands (alias analysis will be
6609 run for the new function, anyway). */
6610 remove_phi_node (&si, true);
6611 continue;
6614 SET_PHI_RESULT (phi,
6615 replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6616 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
6618 op = USE_FROM_PTR (use);
6619 if (TREE_CODE (op) == SSA_NAME)
6620 SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6623 for (i = 0; i < EDGE_COUNT (bb->preds); i++)
6625 location_t locus = gimple_phi_arg_location (phi, i);
6626 tree block = LOCATION_BLOCK (locus);
6628 if (locus == UNKNOWN_LOCATION)
6629 continue;
6630 if (d->orig_block == NULL_TREE || block == d->orig_block)
6632 if (d->new_block == NULL_TREE)
6633 locus = LOCATION_LOCUS (locus);
6634 else
6635 locus = COMBINE_LOCATION_DATA (line_table, locus, d->new_block);
6636 gimple_phi_arg_set_location (phi, i, locus);
6640 gsi_next (&si);
6643 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6645 gimple stmt = gsi_stmt (si);
6646 struct walk_stmt_info wi;
6648 memset (&wi, 0, sizeof (wi));
6649 wi.info = d;
6650 walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
6652 if (gimple_code (stmt) == GIMPLE_LABEL)
6654 tree label = gimple_label_label (stmt);
6655 int uid = LABEL_DECL_UID (label);
6657 gcc_assert (uid > -1);
6659 old_len = vec_safe_length (cfg->x_label_to_block_map);
6660 if (old_len <= (unsigned) uid)
6662 new_len = 3 * uid / 2 + 1;
6663 vec_safe_grow_cleared (cfg->x_label_to_block_map, new_len);
6666 (*cfg->x_label_to_block_map)[uid] = bb;
6667 (*cfun->cfg->x_label_to_block_map)[uid] = NULL;
6669 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
6671 if (uid >= dest_cfun->cfg->last_label_uid)
6672 dest_cfun->cfg->last_label_uid = uid + 1;
6675 maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
6676 remove_stmt_from_eh_lp_fn (cfun, stmt);
6678 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
6679 gimple_remove_stmt_histograms (cfun, stmt);
6681 /* We cannot leave any operands allocated from the operand caches of
6682 the current function. */
6683 free_stmt_operands (cfun, stmt);
6684 push_cfun (dest_cfun);
6685 update_stmt (stmt);
6686 pop_cfun ();
6689 FOR_EACH_EDGE (e, ei, bb->succs)
6690 if (e->goto_locus != UNKNOWN_LOCATION)
6692 tree block = LOCATION_BLOCK (e->goto_locus);
6693 if (d->orig_block == NULL_TREE
6694 || block == d->orig_block)
6695 e->goto_locus = d->new_block ?
6696 COMBINE_LOCATION_DATA (line_table, e->goto_locus, d->new_block) :
6697 LOCATION_LOCUS (e->goto_locus);
6701 /* Examine the statements in BB (which is in SRC_CFUN); find and return
6702 the outermost EH region. Use REGION as the incoming base EH region. */
6704 static eh_region
6705 find_outermost_region_in_block (struct function *src_cfun,
6706 basic_block bb, eh_region region)
6708 gimple_stmt_iterator si;
6710 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6712 gimple stmt = gsi_stmt (si);
6713 eh_region stmt_region;
6714 int lp_nr;
6716 lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
6717 stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
6718 if (stmt_region)
6720 if (region == NULL)
6721 region = stmt_region;
6722 else if (stmt_region != region)
6724 region = eh_region_outermost (src_cfun, stmt_region, region);
6725 gcc_assert (region != NULL);
6730 return region;
6733 static tree
6734 new_label_mapper (tree decl, void *data)
6736 htab_t hash = (htab_t) data;
6737 struct tree_map *m;
6738 void **slot;
6740 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
6742 m = XNEW (struct tree_map);
6743 m->hash = DECL_UID (decl);
6744 m->base.from = decl;
6745 m->to = create_artificial_label (UNKNOWN_LOCATION);
6746 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
6747 if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
6748 cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
6750 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
6751 gcc_assert (*slot == NULL);
6753 *slot = m;
6755 return m->to;
6758 /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
6759 subblocks. */
6761 static void
6762 replace_block_vars_by_duplicates (tree block, hash_map<tree, tree> *vars_map,
6763 tree to_context)
6765 tree *tp, t;
6767 for (tp = &BLOCK_VARS (block); *tp; tp = &DECL_CHAIN (*tp))
6769 t = *tp;
6770 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
6771 continue;
6772 replace_by_duplicate_decl (&t, vars_map, to_context);
6773 if (t != *tp)
6775 if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
6777 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
6778 DECL_HAS_VALUE_EXPR_P (t) = 1;
6780 DECL_CHAIN (t) = DECL_CHAIN (*tp);
6781 *tp = t;
6785 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
6786 replace_block_vars_by_duplicates (block, vars_map, to_context);
6789 /* Fixup the loop arrays and numbers after moving LOOP and its subloops
6790 from FN1 to FN2. */
6792 static void
6793 fixup_loop_arrays_after_move (struct function *fn1, struct function *fn2,
6794 struct loop *loop)
6796 /* Discard it from the old loop array. */
6797 (*get_loops (fn1))[loop->num] = NULL;
6799 /* Place it in the new loop array, assigning it a new number. */
6800 loop->num = number_of_loops (fn2);
6801 vec_safe_push (loops_for_fn (fn2)->larray, loop);
6803 /* Recurse to children. */
6804 for (loop = loop->inner; loop; loop = loop->next)
6805 fixup_loop_arrays_after_move (fn1, fn2, loop);
6808 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
6809 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
6810 single basic block in the original CFG and the new basic block is
6811 returned. DEST_CFUN must not have a CFG yet.
6813 Note that the region need not be a pure SESE region. Blocks inside
6814 the region may contain calls to abort/exit. The only restriction
6815 is that ENTRY_BB should be the only entry point and it must
6816 dominate EXIT_BB.
6818 Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
6819 functions outermost BLOCK, move all subblocks of ORIG_BLOCK
6820 to the new function.
6822 All local variables referenced in the region are assumed to be in
6823 the corresponding BLOCK_VARS and unexpanded variable lists
6824 associated with DEST_CFUN. */
6826 basic_block
6827 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
6828 basic_block exit_bb, tree orig_block)
6830 vec<basic_block> bbs, dom_bbs;
6831 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
6832 basic_block after, bb, *entry_pred, *exit_succ, abb;
6833 struct function *saved_cfun = cfun;
6834 int *entry_flag, *exit_flag;
6835 unsigned *entry_prob, *exit_prob;
6836 unsigned i, num_entry_edges, num_exit_edges, num_nodes;
6837 edge e;
6838 edge_iterator ei;
6839 htab_t new_label_map;
6840 hash_map<void *, void *> *eh_map;
6841 struct loop *loop = entry_bb->loop_father;
6842 struct loop *loop0 = get_loop (saved_cfun, 0);
6843 struct move_stmt_d d;
6845 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
6846 region. */
6847 gcc_assert (entry_bb != exit_bb
6848 && (!exit_bb
6849 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
6851 /* Collect all the blocks in the region. Manually add ENTRY_BB
6852 because it won't be added by dfs_enumerate_from. */
6853 bbs.create (0);
6854 bbs.safe_push (entry_bb);
6855 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
6857 /* The blocks that used to be dominated by something in BBS will now be
6858 dominated by the new block. */
6859 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
6860 bbs.address (),
6861 bbs.length ());
6863 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
6864 the predecessor edges to ENTRY_BB and the successor edges to
6865 EXIT_BB so that we can re-attach them to the new basic block that
6866 will replace the region. */
6867 num_entry_edges = EDGE_COUNT (entry_bb->preds);
6868 entry_pred = XNEWVEC (basic_block, num_entry_edges);
6869 entry_flag = XNEWVEC (int, num_entry_edges);
6870 entry_prob = XNEWVEC (unsigned, num_entry_edges);
6871 i = 0;
6872 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
6874 entry_prob[i] = e->probability;
6875 entry_flag[i] = e->flags;
6876 entry_pred[i++] = e->src;
6877 remove_edge (e);
6880 if (exit_bb)
6882 num_exit_edges = EDGE_COUNT (exit_bb->succs);
6883 exit_succ = XNEWVEC (basic_block, num_exit_edges);
6884 exit_flag = XNEWVEC (int, num_exit_edges);
6885 exit_prob = XNEWVEC (unsigned, num_exit_edges);
6886 i = 0;
6887 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
6889 exit_prob[i] = e->probability;
6890 exit_flag[i] = e->flags;
6891 exit_succ[i++] = e->dest;
6892 remove_edge (e);
6895 else
6897 num_exit_edges = 0;
6898 exit_succ = NULL;
6899 exit_flag = NULL;
6900 exit_prob = NULL;
6903 /* Switch context to the child function to initialize DEST_FN's CFG. */
6904 gcc_assert (dest_cfun->cfg == NULL);
6905 push_cfun (dest_cfun);
6907 init_empty_tree_cfg ();
6909 /* Initialize EH information for the new function. */
6910 eh_map = NULL;
6911 new_label_map = NULL;
6912 if (saved_cfun->eh)
6914 eh_region region = NULL;
6916 FOR_EACH_VEC_ELT (bbs, i, bb)
6917 region = find_outermost_region_in_block (saved_cfun, bb, region);
6919 init_eh_for_function ();
6920 if (region != NULL)
6922 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
6923 eh_map = duplicate_eh_regions (saved_cfun, region, 0,
6924 new_label_mapper, new_label_map);
6928 /* Initialize an empty loop tree. */
6929 struct loops *loops = ggc_cleared_alloc<struct loops> ();
6930 init_loops_structure (dest_cfun, loops, 1);
6931 loops->state = LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
6932 set_loops_for_fn (dest_cfun, loops);
6934 /* Move the outlined loop tree part. */
6935 num_nodes = bbs.length ();
6936 FOR_EACH_VEC_ELT (bbs, i, bb)
6938 if (bb->loop_father->header == bb)
6940 struct loop *this_loop = bb->loop_father;
6941 struct loop *outer = loop_outer (this_loop);
6942 if (outer == loop
6943 /* If the SESE region contains some bbs ending with
6944 a noreturn call, those are considered to belong
6945 to the outermost loop in saved_cfun, rather than
6946 the entry_bb's loop_father. */
6947 || outer == loop0)
6949 if (outer != loop)
6950 num_nodes -= this_loop->num_nodes;
6951 flow_loop_tree_node_remove (bb->loop_father);
6952 flow_loop_tree_node_add (get_loop (dest_cfun, 0), this_loop);
6953 fixup_loop_arrays_after_move (saved_cfun, cfun, this_loop);
6956 else if (bb->loop_father == loop0 && loop0 != loop)
6957 num_nodes--;
6959 /* Remove loop exits from the outlined region. */
6960 if (loops_for_fn (saved_cfun)->exits)
6961 FOR_EACH_EDGE (e, ei, bb->succs)
6963 void **slot = htab_find_slot_with_hash
6964 (loops_for_fn (saved_cfun)->exits, e,
6965 htab_hash_pointer (e), NO_INSERT);
6966 if (slot)
6967 htab_clear_slot (loops_for_fn (saved_cfun)->exits, slot);
6972 /* Adjust the number of blocks in the tree root of the outlined part. */
6973 get_loop (dest_cfun, 0)->num_nodes = bbs.length () + 2;
6975 /* Setup a mapping to be used by move_block_to_fn. */
6976 loop->aux = current_loops->tree_root;
6977 loop0->aux = current_loops->tree_root;
6979 pop_cfun ();
6981 /* Move blocks from BBS into DEST_CFUN. */
6982 gcc_assert (bbs.length () >= 2);
6983 after = dest_cfun->cfg->x_entry_block_ptr;
6984 hash_map<tree, tree> vars_map;
6986 memset (&d, 0, sizeof (d));
6987 d.orig_block = orig_block;
6988 d.new_block = DECL_INITIAL (dest_cfun->decl);
6989 d.from_context = cfun->decl;
6990 d.to_context = dest_cfun->decl;
6991 d.vars_map = &vars_map;
6992 d.new_label_map = new_label_map;
6993 d.eh_map = eh_map;
6994 d.remap_decls_p = true;
6996 FOR_EACH_VEC_ELT (bbs, i, bb)
6998 /* No need to update edge counts on the last block. It has
6999 already been updated earlier when we detached the region from
7000 the original CFG. */
7001 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
7002 after = bb;
7005 loop->aux = NULL;
7006 loop0->aux = NULL;
7007 /* Loop sizes are no longer correct, fix them up. */
7008 loop->num_nodes -= num_nodes;
7009 for (struct loop *outer = loop_outer (loop);
7010 outer; outer = loop_outer (outer))
7011 outer->num_nodes -= num_nodes;
7012 loop0->num_nodes -= bbs.length () - num_nodes;
7014 if (saved_cfun->has_simduid_loops || saved_cfun->has_force_vectorize_loops)
7016 struct loop *aloop;
7017 for (i = 0; vec_safe_iterate (loops->larray, i, &aloop); i++)
7018 if (aloop != NULL)
7020 if (aloop->simduid)
7022 replace_by_duplicate_decl (&aloop->simduid, d.vars_map,
7023 d.to_context);
7024 dest_cfun->has_simduid_loops = true;
7026 if (aloop->force_vectorize)
7027 dest_cfun->has_force_vectorize_loops = true;
7031 /* Rewire BLOCK_SUBBLOCKS of orig_block. */
7032 if (orig_block)
7034 tree block;
7035 gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
7036 == NULL_TREE);
7037 BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
7038 = BLOCK_SUBBLOCKS (orig_block);
7039 for (block = BLOCK_SUBBLOCKS (orig_block);
7040 block; block = BLOCK_CHAIN (block))
7041 BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
7042 BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
7045 replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
7046 &vars_map, dest_cfun->decl);
7048 if (new_label_map)
7049 htab_delete (new_label_map);
7050 if (eh_map)
7051 delete eh_map;
7053 /* Rewire the entry and exit blocks. The successor to the entry
7054 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
7055 the child function. Similarly, the predecessor of DEST_FN's
7056 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
7057 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
7058 various CFG manipulation function get to the right CFG.
7060 FIXME, this is silly. The CFG ought to become a parameter to
7061 these helpers. */
7062 push_cfun (dest_cfun);
7063 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), entry_bb, EDGE_FALLTHRU);
7064 if (exit_bb)
7065 make_edge (exit_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
7066 pop_cfun ();
7068 /* Back in the original function, the SESE region has disappeared,
7069 create a new basic block in its place. */
7070 bb = create_empty_bb (entry_pred[0]);
7071 if (current_loops)
7072 add_bb_to_loop (bb, loop);
7073 for (i = 0; i < num_entry_edges; i++)
7075 e = make_edge (entry_pred[i], bb, entry_flag[i]);
7076 e->probability = entry_prob[i];
7079 for (i = 0; i < num_exit_edges; i++)
7081 e = make_edge (bb, exit_succ[i], exit_flag[i]);
7082 e->probability = exit_prob[i];
7085 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
7086 FOR_EACH_VEC_ELT (dom_bbs, i, abb)
7087 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
7088 dom_bbs.release ();
7090 if (exit_bb)
7092 free (exit_prob);
7093 free (exit_flag);
7094 free (exit_succ);
7096 free (entry_prob);
7097 free (entry_flag);
7098 free (entry_pred);
7099 bbs.release ();
7101 return bb;
7105 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in dumpfile.h)
7108 void
7109 dump_function_to_file (tree fndecl, FILE *file, int flags)
7111 tree arg, var, old_current_fndecl = current_function_decl;
7112 struct function *dsf;
7113 bool ignore_topmost_bind = false, any_var = false;
7114 basic_block bb;
7115 tree chain;
7116 bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
7117 && decl_is_tm_clone (fndecl));
7118 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
7120 current_function_decl = fndecl;
7121 fprintf (file, "%s %s(", function_name (fun), tmclone ? "[tm-clone] " : "");
7123 arg = DECL_ARGUMENTS (fndecl);
7124 while (arg)
7126 print_generic_expr (file, TREE_TYPE (arg), dump_flags);
7127 fprintf (file, " ");
7128 print_generic_expr (file, arg, dump_flags);
7129 if (flags & TDF_VERBOSE)
7130 print_node (file, "", arg, 4);
7131 if (DECL_CHAIN (arg))
7132 fprintf (file, ", ");
7133 arg = DECL_CHAIN (arg);
7135 fprintf (file, ")\n");
7137 if (flags & TDF_VERBOSE)
7138 print_node (file, "", fndecl, 2);
7140 dsf = DECL_STRUCT_FUNCTION (fndecl);
7141 if (dsf && (flags & TDF_EH))
7142 dump_eh_tree (file, dsf);
7144 if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
7146 dump_node (fndecl, TDF_SLIM | flags, file);
7147 current_function_decl = old_current_fndecl;
7148 return;
7151 /* When GIMPLE is lowered, the variables are no longer available in
7152 BIND_EXPRs, so display them separately. */
7153 if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
7155 unsigned ix;
7156 ignore_topmost_bind = true;
7158 fprintf (file, "{\n");
7159 if (!vec_safe_is_empty (fun->local_decls))
7160 FOR_EACH_LOCAL_DECL (fun, ix, var)
7162 print_generic_decl (file, var, flags);
7163 if (flags & TDF_VERBOSE)
7164 print_node (file, "", var, 4);
7165 fprintf (file, "\n");
7167 any_var = true;
7169 if (gimple_in_ssa_p (cfun))
7170 for (ix = 1; ix < num_ssa_names; ++ix)
7172 tree name = ssa_name (ix);
7173 if (name && !SSA_NAME_VAR (name))
7175 fprintf (file, " ");
7176 print_generic_expr (file, TREE_TYPE (name), flags);
7177 fprintf (file, " ");
7178 print_generic_expr (file, name, flags);
7179 fprintf (file, ";\n");
7181 any_var = true;
7186 if (fun && fun->decl == fndecl
7187 && fun->cfg
7188 && basic_block_info_for_fn (fun))
7190 /* If the CFG has been built, emit a CFG-based dump. */
7191 if (!ignore_topmost_bind)
7192 fprintf (file, "{\n");
7194 if (any_var && n_basic_blocks_for_fn (fun))
7195 fprintf (file, "\n");
7197 FOR_EACH_BB_FN (bb, fun)
7198 dump_bb (file, bb, 2, flags | TDF_COMMENT);
7200 fprintf (file, "}\n");
7202 else if (DECL_SAVED_TREE (fndecl) == NULL)
7204 /* The function is now in GIMPLE form but the CFG has not been
7205 built yet. Emit the single sequence of GIMPLE statements
7206 that make up its body. */
7207 gimple_seq body = gimple_body (fndecl);
7209 if (gimple_seq_first_stmt (body)
7210 && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
7211 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
7212 print_gimple_seq (file, body, 0, flags);
7213 else
7215 if (!ignore_topmost_bind)
7216 fprintf (file, "{\n");
7218 if (any_var)
7219 fprintf (file, "\n");
7221 print_gimple_seq (file, body, 2, flags);
7222 fprintf (file, "}\n");
7225 else
7227 int indent;
7229 /* Make a tree based dump. */
7230 chain = DECL_SAVED_TREE (fndecl);
7231 if (chain && TREE_CODE (chain) == BIND_EXPR)
7233 if (ignore_topmost_bind)
7235 chain = BIND_EXPR_BODY (chain);
7236 indent = 2;
7238 else
7239 indent = 0;
7241 else
7243 if (!ignore_topmost_bind)
7244 fprintf (file, "{\n");
7245 indent = 2;
7248 if (any_var)
7249 fprintf (file, "\n");
7251 print_generic_stmt_indented (file, chain, flags, indent);
7252 if (ignore_topmost_bind)
7253 fprintf (file, "}\n");
7256 if (flags & TDF_ENUMERATE_LOCALS)
7257 dump_enumerated_decls (file, flags);
7258 fprintf (file, "\n\n");
7260 current_function_decl = old_current_fndecl;
7263 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
7265 DEBUG_FUNCTION void
7266 debug_function (tree fn, int flags)
7268 dump_function_to_file (fn, stderr, flags);
7272 /* Print on FILE the indexes for the predecessors of basic_block BB. */
7274 static void
7275 print_pred_bbs (FILE *file, basic_block bb)
7277 edge e;
7278 edge_iterator ei;
7280 FOR_EACH_EDGE (e, ei, bb->preds)
7281 fprintf (file, "bb_%d ", e->src->index);
7285 /* Print on FILE the indexes for the successors of basic_block BB. */
7287 static void
7288 print_succ_bbs (FILE *file, basic_block bb)
7290 edge e;
7291 edge_iterator ei;
7293 FOR_EACH_EDGE (e, ei, bb->succs)
7294 fprintf (file, "bb_%d ", e->dest->index);
7297 /* Print to FILE the basic block BB following the VERBOSITY level. */
7299 void
7300 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
7302 char *s_indent = (char *) alloca ((size_t) indent + 1);
7303 memset ((void *) s_indent, ' ', (size_t) indent);
7304 s_indent[indent] = '\0';
7306 /* Print basic_block's header. */
7307 if (verbosity >= 2)
7309 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
7310 print_pred_bbs (file, bb);
7311 fprintf (file, "}, succs = {");
7312 print_succ_bbs (file, bb);
7313 fprintf (file, "})\n");
7316 /* Print basic_block's body. */
7317 if (verbosity >= 3)
7319 fprintf (file, "%s {\n", s_indent);
7320 dump_bb (file, bb, indent + 4, TDF_VOPS|TDF_MEMSYMS);
7321 fprintf (file, "%s }\n", s_indent);
7325 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
7327 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
7328 VERBOSITY level this outputs the contents of the loop, or just its
7329 structure. */
7331 static void
7332 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
7334 char *s_indent;
7335 basic_block bb;
7337 if (loop == NULL)
7338 return;
7340 s_indent = (char *) alloca ((size_t) indent + 1);
7341 memset ((void *) s_indent, ' ', (size_t) indent);
7342 s_indent[indent] = '\0';
7344 /* Print loop's header. */
7345 fprintf (file, "%sloop_%d (", s_indent, loop->num);
7346 if (loop->header)
7347 fprintf (file, "header = %d", loop->header->index);
7348 else
7350 fprintf (file, "deleted)\n");
7351 return;
7353 if (loop->latch)
7354 fprintf (file, ", latch = %d", loop->latch->index);
7355 else
7356 fprintf (file, ", multiple latches");
7357 fprintf (file, ", niter = ");
7358 print_generic_expr (file, loop->nb_iterations, 0);
7360 if (loop->any_upper_bound)
7362 fprintf (file, ", upper_bound = ");
7363 print_decu (loop->nb_iterations_upper_bound, file);
7366 if (loop->any_estimate)
7368 fprintf (file, ", estimate = ");
7369 print_decu (loop->nb_iterations_estimate, file);
7371 fprintf (file, ")\n");
7373 /* Print loop's body. */
7374 if (verbosity >= 1)
7376 fprintf (file, "%s{\n", s_indent);
7377 FOR_EACH_BB_FN (bb, cfun)
7378 if (bb->loop_father == loop)
7379 print_loops_bb (file, bb, indent, verbosity);
7381 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
7382 fprintf (file, "%s}\n", s_indent);
7386 /* Print the LOOP and its sibling loops on FILE, indented INDENT
7387 spaces. Following VERBOSITY level this outputs the contents of the
7388 loop, or just its structure. */
7390 static void
7391 print_loop_and_siblings (FILE *file, struct loop *loop, int indent,
7392 int verbosity)
7394 if (loop == NULL)
7395 return;
7397 print_loop (file, loop, indent, verbosity);
7398 print_loop_and_siblings (file, loop->next, indent, verbosity);
7401 /* Follow a CFG edge from the entry point of the program, and on entry
7402 of a loop, pretty print the loop structure on FILE. */
7404 void
7405 print_loops (FILE *file, int verbosity)
7407 basic_block bb;
7409 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
7410 if (bb && bb->loop_father)
7411 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
7414 /* Dump a loop. */
7416 DEBUG_FUNCTION void
7417 debug (struct loop &ref)
7419 print_loop (stderr, &ref, 0, /*verbosity*/0);
7422 DEBUG_FUNCTION void
7423 debug (struct loop *ptr)
7425 if (ptr)
7426 debug (*ptr);
7427 else
7428 fprintf (stderr, "<nil>\n");
7431 /* Dump a loop verbosely. */
7433 DEBUG_FUNCTION void
7434 debug_verbose (struct loop &ref)
7436 print_loop (stderr, &ref, 0, /*verbosity*/3);
7439 DEBUG_FUNCTION void
7440 debug_verbose (struct loop *ptr)
7442 if (ptr)
7443 debug (*ptr);
7444 else
7445 fprintf (stderr, "<nil>\n");
7449 /* Debugging loops structure at tree level, at some VERBOSITY level. */
7451 DEBUG_FUNCTION void
7452 debug_loops (int verbosity)
7454 print_loops (stderr, verbosity);
7457 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
7459 DEBUG_FUNCTION void
7460 debug_loop (struct loop *loop, int verbosity)
7462 print_loop (stderr, loop, 0, verbosity);
7465 /* Print on stderr the code of loop number NUM, at some VERBOSITY
7466 level. */
7468 DEBUG_FUNCTION void
7469 debug_loop_num (unsigned num, int verbosity)
7471 debug_loop (get_loop (cfun, num), verbosity);
7474 /* Return true if BB ends with a call, possibly followed by some
7475 instructions that must stay with the call. Return false,
7476 otherwise. */
7478 static bool
7479 gimple_block_ends_with_call_p (basic_block bb)
7481 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7482 return !gsi_end_p (gsi) && is_gimple_call (gsi_stmt (gsi));
7486 /* Return true if BB ends with a conditional branch. Return false,
7487 otherwise. */
7489 static bool
7490 gimple_block_ends_with_condjump_p (const_basic_block bb)
7492 gimple stmt = last_stmt (CONST_CAST_BB (bb));
7493 return (stmt && gimple_code (stmt) == GIMPLE_COND);
7497 /* Return true if we need to add fake edge to exit at statement T.
7498 Helper function for gimple_flow_call_edges_add. */
7500 static bool
7501 need_fake_edge_p (gimple t)
7503 tree fndecl = NULL_TREE;
7504 int call_flags = 0;
7506 /* NORETURN and LONGJMP calls already have an edge to exit.
7507 CONST and PURE calls do not need one.
7508 We don't currently check for CONST and PURE here, although
7509 it would be a good idea, because those attributes are
7510 figured out from the RTL in mark_constant_function, and
7511 the counter incrementation code from -fprofile-arcs
7512 leads to different results from -fbranch-probabilities. */
7513 if (is_gimple_call (t))
7515 fndecl = gimple_call_fndecl (t);
7516 call_flags = gimple_call_flags (t);
7519 if (is_gimple_call (t)
7520 && fndecl
7521 && DECL_BUILT_IN (fndecl)
7522 && (call_flags & ECF_NOTHROW)
7523 && !(call_flags & ECF_RETURNS_TWICE)
7524 /* fork() doesn't really return twice, but the effect of
7525 wrapping it in __gcov_fork() which calls __gcov_flush()
7526 and clears the counters before forking has the same
7527 effect as returning twice. Force a fake edge. */
7528 && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7529 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
7530 return false;
7532 if (is_gimple_call (t))
7534 edge_iterator ei;
7535 edge e;
7536 basic_block bb;
7538 if (!(call_flags & ECF_NORETURN))
7539 return true;
7541 bb = gimple_bb (t);
7542 FOR_EACH_EDGE (e, ei, bb->succs)
7543 if ((e->flags & EDGE_FAKE) == 0)
7544 return true;
7547 if (gimple_code (t) == GIMPLE_ASM
7548 && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
7549 return true;
7551 return false;
7555 /* Add fake edges to the function exit for any non constant and non
7556 noreturn calls (or noreturn calls with EH/abnormal edges),
7557 volatile inline assembly in the bitmap of blocks specified by BLOCKS
7558 or to the whole CFG if BLOCKS is zero. Return the number of blocks
7559 that were split.
7561 The goal is to expose cases in which entering a basic block does
7562 not imply that all subsequent instructions must be executed. */
7564 static int
7565 gimple_flow_call_edges_add (sbitmap blocks)
7567 int i;
7568 int blocks_split = 0;
7569 int last_bb = last_basic_block_for_fn (cfun);
7570 bool check_last_block = false;
7572 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
7573 return 0;
7575 if (! blocks)
7576 check_last_block = true;
7577 else
7578 check_last_block = bitmap_bit_p (blocks,
7579 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
7581 /* In the last basic block, before epilogue generation, there will be
7582 a fallthru edge to EXIT. Special care is required if the last insn
7583 of the last basic block is a call because make_edge folds duplicate
7584 edges, which would result in the fallthru edge also being marked
7585 fake, which would result in the fallthru edge being removed by
7586 remove_fake_edges, which would result in an invalid CFG.
7588 Moreover, we can't elide the outgoing fake edge, since the block
7589 profiler needs to take this into account in order to solve the minimal
7590 spanning tree in the case that the call doesn't return.
7592 Handle this by adding a dummy instruction in a new last basic block. */
7593 if (check_last_block)
7595 basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
7596 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7597 gimple t = NULL;
7599 if (!gsi_end_p (gsi))
7600 t = gsi_stmt (gsi);
7602 if (t && need_fake_edge_p (t))
7604 edge e;
7606 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
7607 if (e)
7609 gsi_insert_on_edge (e, gimple_build_nop ());
7610 gsi_commit_edge_inserts ();
7615 /* Now add fake edges to the function exit for any non constant
7616 calls since there is no way that we can determine if they will
7617 return or not... */
7618 for (i = 0; i < last_bb; i++)
7620 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
7621 gimple_stmt_iterator gsi;
7622 gimple stmt, last_stmt;
7624 if (!bb)
7625 continue;
7627 if (blocks && !bitmap_bit_p (blocks, i))
7628 continue;
7630 gsi = gsi_last_nondebug_bb (bb);
7631 if (!gsi_end_p (gsi))
7633 last_stmt = gsi_stmt (gsi);
7636 stmt = gsi_stmt (gsi);
7637 if (need_fake_edge_p (stmt))
7639 edge e;
7641 /* The handling above of the final block before the
7642 epilogue should be enough to verify that there is
7643 no edge to the exit block in CFG already.
7644 Calling make_edge in such case would cause us to
7645 mark that edge as fake and remove it later. */
7646 #ifdef ENABLE_CHECKING
7647 if (stmt == last_stmt)
7649 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
7650 gcc_assert (e == NULL);
7652 #endif
7654 /* Note that the following may create a new basic block
7655 and renumber the existing basic blocks. */
7656 if (stmt != last_stmt)
7658 e = split_block (bb, stmt);
7659 if (e)
7660 blocks_split++;
7662 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
7664 gsi_prev (&gsi);
7666 while (!gsi_end_p (gsi));
7670 if (blocks_split)
7671 verify_flow_info ();
7673 return blocks_split;
7676 /* Removes edge E and all the blocks dominated by it, and updates dominance
7677 information. The IL in E->src needs to be updated separately.
7678 If dominance info is not available, only the edge E is removed.*/
7680 void
7681 remove_edge_and_dominated_blocks (edge e)
7683 vec<basic_block> bbs_to_remove = vNULL;
7684 vec<basic_block> bbs_to_fix_dom = vNULL;
7685 bitmap df, df_idom;
7686 edge f;
7687 edge_iterator ei;
7688 bool none_removed = false;
7689 unsigned i;
7690 basic_block bb, dbb;
7691 bitmap_iterator bi;
7693 if (!dom_info_available_p (CDI_DOMINATORS))
7695 remove_edge (e);
7696 return;
7699 /* No updating is needed for edges to exit. */
7700 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
7702 if (cfgcleanup_altered_bbs)
7703 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7704 remove_edge (e);
7705 return;
7708 /* First, we find the basic blocks to remove. If E->dest has a predecessor
7709 that is not dominated by E->dest, then this set is empty. Otherwise,
7710 all the basic blocks dominated by E->dest are removed.
7712 Also, to DF_IDOM we store the immediate dominators of the blocks in
7713 the dominance frontier of E (i.e., of the successors of the
7714 removed blocks, if there are any, and of E->dest otherwise). */
7715 FOR_EACH_EDGE (f, ei, e->dest->preds)
7717 if (f == e)
7718 continue;
7720 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
7722 none_removed = true;
7723 break;
7727 df = BITMAP_ALLOC (NULL);
7728 df_idom = BITMAP_ALLOC (NULL);
7730 if (none_removed)
7731 bitmap_set_bit (df_idom,
7732 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
7733 else
7735 bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
7736 FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
7738 FOR_EACH_EDGE (f, ei, bb->succs)
7740 if (f->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
7741 bitmap_set_bit (df, f->dest->index);
7744 FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
7745 bitmap_clear_bit (df, bb->index);
7747 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
7749 bb = BASIC_BLOCK_FOR_FN (cfun, i);
7750 bitmap_set_bit (df_idom,
7751 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
7755 if (cfgcleanup_altered_bbs)
7757 /* Record the set of the altered basic blocks. */
7758 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7759 bitmap_ior_into (cfgcleanup_altered_bbs, df);
7762 /* Remove E and the cancelled blocks. */
7763 if (none_removed)
7764 remove_edge (e);
7765 else
7767 /* Walk backwards so as to get a chance to substitute all
7768 released DEFs into debug stmts. See
7769 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
7770 details. */
7771 for (i = bbs_to_remove.length (); i-- > 0; )
7772 delete_basic_block (bbs_to_remove[i]);
7775 /* Update the dominance information. The immediate dominator may change only
7776 for blocks whose immediate dominator belongs to DF_IDOM:
7778 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
7779 removal. Let Z the arbitrary block such that idom(Z) = Y and
7780 Z dominates X after the removal. Before removal, there exists a path P
7781 from Y to X that avoids Z. Let F be the last edge on P that is
7782 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
7783 dominates W, and because of P, Z does not dominate W), and W belongs to
7784 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
7785 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
7787 bb = BASIC_BLOCK_FOR_FN (cfun, i);
7788 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
7789 dbb;
7790 dbb = next_dom_son (CDI_DOMINATORS, dbb))
7791 bbs_to_fix_dom.safe_push (dbb);
7794 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
7796 BITMAP_FREE (df);
7797 BITMAP_FREE (df_idom);
7798 bbs_to_remove.release ();
7799 bbs_to_fix_dom.release ();
7802 /* Purge dead EH edges from basic block BB. */
7804 bool
7805 gimple_purge_dead_eh_edges (basic_block bb)
7807 bool changed = false;
7808 edge e;
7809 edge_iterator ei;
7810 gimple stmt = last_stmt (bb);
7812 if (stmt && stmt_can_throw_internal (stmt))
7813 return false;
7815 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7817 if (e->flags & EDGE_EH)
7819 remove_edge_and_dominated_blocks (e);
7820 changed = true;
7822 else
7823 ei_next (&ei);
7826 return changed;
7829 /* Purge dead EH edges from basic block listed in BLOCKS. */
7831 bool
7832 gimple_purge_all_dead_eh_edges (const_bitmap blocks)
7834 bool changed = false;
7835 unsigned i;
7836 bitmap_iterator bi;
7838 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7840 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
7842 /* Earlier gimple_purge_dead_eh_edges could have removed
7843 this basic block already. */
7844 gcc_assert (bb || changed);
7845 if (bb != NULL)
7846 changed |= gimple_purge_dead_eh_edges (bb);
7849 return changed;
7852 /* Purge dead abnormal call edges from basic block BB. */
7854 bool
7855 gimple_purge_dead_abnormal_call_edges (basic_block bb)
7857 bool changed = false;
7858 edge e;
7859 edge_iterator ei;
7860 gimple stmt = last_stmt (bb);
7862 if (!cfun->has_nonlocal_label
7863 && !cfun->calls_setjmp)
7864 return false;
7866 if (stmt && stmt_can_make_abnormal_goto (stmt))
7867 return false;
7869 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7871 if (e->flags & EDGE_ABNORMAL)
7873 if (e->flags & EDGE_FALLTHRU)
7874 e->flags &= ~EDGE_ABNORMAL;
7875 else
7876 remove_edge_and_dominated_blocks (e);
7877 changed = true;
7879 else
7880 ei_next (&ei);
7883 return changed;
7886 /* Purge dead abnormal call edges from basic block listed in BLOCKS. */
7888 bool
7889 gimple_purge_all_dead_abnormal_call_edges (const_bitmap blocks)
7891 bool changed = false;
7892 unsigned i;
7893 bitmap_iterator bi;
7895 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7897 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
7899 /* Earlier gimple_purge_dead_abnormal_call_edges could have removed
7900 this basic block already. */
7901 gcc_assert (bb || changed);
7902 if (bb != NULL)
7903 changed |= gimple_purge_dead_abnormal_call_edges (bb);
7906 return changed;
7909 /* This function is called whenever a new edge is created or
7910 redirected. */
7912 static void
7913 gimple_execute_on_growing_pred (edge e)
7915 basic_block bb = e->dest;
7917 if (!gimple_seq_empty_p (phi_nodes (bb)))
7918 reserve_phi_args_for_new_edge (bb);
7921 /* This function is called immediately before edge E is removed from
7922 the edge vector E->dest->preds. */
7924 static void
7925 gimple_execute_on_shrinking_pred (edge e)
7927 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
7928 remove_phi_args (e);
7931 /*---------------------------------------------------------------------------
7932 Helper functions for Loop versioning
7933 ---------------------------------------------------------------------------*/
7935 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
7936 of 'first'. Both of them are dominated by 'new_head' basic block. When
7937 'new_head' was created by 'second's incoming edge it received phi arguments
7938 on the edge by split_edge(). Later, additional edge 'e' was created to
7939 connect 'new_head' and 'first'. Now this routine adds phi args on this
7940 additional edge 'e' that new_head to second edge received as part of edge
7941 splitting. */
7943 static void
7944 gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
7945 basic_block new_head, edge e)
7947 gimple phi1, phi2;
7948 gimple_stmt_iterator psi1, psi2;
7949 tree def;
7950 edge e2 = find_edge (new_head, second);
7952 /* Because NEW_HEAD has been created by splitting SECOND's incoming
7953 edge, we should always have an edge from NEW_HEAD to SECOND. */
7954 gcc_assert (e2 != NULL);
7956 /* Browse all 'second' basic block phi nodes and add phi args to
7957 edge 'e' for 'first' head. PHI args are always in correct order. */
7959 for (psi2 = gsi_start_phis (second),
7960 psi1 = gsi_start_phis (first);
7961 !gsi_end_p (psi2) && !gsi_end_p (psi1);
7962 gsi_next (&psi2), gsi_next (&psi1))
7964 phi1 = gsi_stmt (psi1);
7965 phi2 = gsi_stmt (psi2);
7966 def = PHI_ARG_DEF (phi2, e2->dest_idx);
7967 add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
7972 /* Adds a if else statement to COND_BB with condition COND_EXPR.
7973 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
7974 the destination of the ELSE part. */
7976 static void
7977 gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
7978 basic_block second_head ATTRIBUTE_UNUSED,
7979 basic_block cond_bb, void *cond_e)
7981 gimple_stmt_iterator gsi;
7982 gimple new_cond_expr;
7983 tree cond_expr = (tree) cond_e;
7984 edge e0;
7986 /* Build new conditional expr */
7987 new_cond_expr = gimple_build_cond_from_tree (cond_expr,
7988 NULL_TREE, NULL_TREE);
7990 /* Add new cond in cond_bb. */
7991 gsi = gsi_last_bb (cond_bb);
7992 gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
7994 /* Adjust edges appropriately to connect new head with first head
7995 as well as second head. */
7996 e0 = single_succ_edge (cond_bb);
7997 e0->flags &= ~EDGE_FALLTHRU;
7998 e0->flags |= EDGE_FALSE_VALUE;
8002 /* Do book-keeping of basic block BB for the profile consistency checker.
8003 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
8004 then do post-pass accounting. Store the counting in RECORD. */
8005 static void
8006 gimple_account_profile_record (basic_block bb, int after_pass,
8007 struct profile_record *record)
8009 gimple_stmt_iterator i;
8010 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
8012 record->size[after_pass]
8013 += estimate_num_insns (gsi_stmt (i), &eni_size_weights);
8014 if (profile_status_for_fn (cfun) == PROFILE_READ)
8015 record->time[after_pass]
8016 += estimate_num_insns (gsi_stmt (i),
8017 &eni_time_weights) * bb->count;
8018 else if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
8019 record->time[after_pass]
8020 += estimate_num_insns (gsi_stmt (i),
8021 &eni_time_weights) * bb->frequency;
8025 struct cfg_hooks gimple_cfg_hooks = {
8026 "gimple",
8027 gimple_verify_flow_info,
8028 gimple_dump_bb, /* dump_bb */
8029 gimple_dump_bb_for_graph, /* dump_bb_for_graph */
8030 create_bb, /* create_basic_block */
8031 gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
8032 gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
8033 gimple_can_remove_branch_p, /* can_remove_branch_p */
8034 remove_bb, /* delete_basic_block */
8035 gimple_split_block, /* split_block */
8036 gimple_move_block_after, /* move_block_after */
8037 gimple_can_merge_blocks_p, /* can_merge_blocks_p */
8038 gimple_merge_blocks, /* merge_blocks */
8039 gimple_predict_edge, /* predict_edge */
8040 gimple_predicted_by_p, /* predicted_by_p */
8041 gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
8042 gimple_duplicate_bb, /* duplicate_block */
8043 gimple_split_edge, /* split_edge */
8044 gimple_make_forwarder_block, /* make_forward_block */
8045 NULL, /* tidy_fallthru_edge */
8046 NULL, /* force_nonfallthru */
8047 gimple_block_ends_with_call_p,/* block_ends_with_call_p */
8048 gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
8049 gimple_flow_call_edges_add, /* flow_call_edges_add */
8050 gimple_execute_on_growing_pred, /* execute_on_growing_pred */
8051 gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
8052 gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
8053 gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
8054 gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
8055 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
8056 flush_pending_stmts, /* flush_pending_stmts */
8057 gimple_empty_block_p, /* block_empty_p */
8058 gimple_split_block_before_cond_jump, /* split_block_before_cond_jump */
8059 gimple_account_profile_record,
8063 /* Split all critical edges. */
8065 unsigned int
8066 split_critical_edges (void)
8068 basic_block bb;
8069 edge e;
8070 edge_iterator ei;
8072 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
8073 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
8074 mappings around the calls to split_edge. */
8075 start_recording_case_labels ();
8076 FOR_ALL_BB_FN (bb, cfun)
8078 FOR_EACH_EDGE (e, ei, bb->succs)
8080 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
8081 split_edge (e);
8082 /* PRE inserts statements to edges and expects that
8083 since split_critical_edges was done beforehand, committing edge
8084 insertions will not split more edges. In addition to critical
8085 edges we must split edges that have multiple successors and
8086 end by control flow statements, such as RESX.
8087 Go ahead and split them too. This matches the logic in
8088 gimple_find_edge_insert_loc. */
8089 else if ((!single_pred_p (e->dest)
8090 || !gimple_seq_empty_p (phi_nodes (e->dest))
8091 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
8092 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
8093 && !(e->flags & EDGE_ABNORMAL))
8095 gimple_stmt_iterator gsi;
8097 gsi = gsi_last_bb (e->src);
8098 if (!gsi_end_p (gsi)
8099 && stmt_ends_bb_p (gsi_stmt (gsi))
8100 && (gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN
8101 && !gimple_call_builtin_p (gsi_stmt (gsi),
8102 BUILT_IN_RETURN)))
8103 split_edge (e);
8107 end_recording_case_labels ();
8108 return 0;
8111 namespace {
8113 const pass_data pass_data_split_crit_edges =
8115 GIMPLE_PASS, /* type */
8116 "crited", /* name */
8117 OPTGROUP_NONE, /* optinfo_flags */
8118 TV_TREE_SPLIT_EDGES, /* tv_id */
8119 PROP_cfg, /* properties_required */
8120 PROP_no_crit_edges, /* properties_provided */
8121 0, /* properties_destroyed */
8122 0, /* todo_flags_start */
8123 0, /* todo_flags_finish */
8126 class pass_split_crit_edges : public gimple_opt_pass
8128 public:
8129 pass_split_crit_edges (gcc::context *ctxt)
8130 : gimple_opt_pass (pass_data_split_crit_edges, ctxt)
8133 /* opt_pass methods: */
8134 virtual unsigned int execute (function *) { return split_critical_edges (); }
8136 opt_pass * clone () { return new pass_split_crit_edges (m_ctxt); }
8137 }; // class pass_split_crit_edges
8139 } // anon namespace
8141 gimple_opt_pass *
8142 make_pass_split_crit_edges (gcc::context *ctxt)
8144 return new pass_split_crit_edges (ctxt);
8148 /* Build a ternary operation and gimplify it. Emit code before GSI.
8149 Return the gimple_val holding the result. */
8151 tree
8152 gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
8153 tree type, tree a, tree b, tree c)
8155 tree ret;
8156 location_t loc = gimple_location (gsi_stmt (*gsi));
8158 ret = fold_build3_loc (loc, code, type, a, b, c);
8159 STRIP_NOPS (ret);
8161 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
8162 GSI_SAME_STMT);
8165 /* Build a binary operation and gimplify it. Emit code before GSI.
8166 Return the gimple_val holding the result. */
8168 tree
8169 gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
8170 tree type, tree a, tree b)
8172 tree ret;
8174 ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
8175 STRIP_NOPS (ret);
8177 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
8178 GSI_SAME_STMT);
8181 /* Build a unary operation and gimplify it. Emit code before GSI.
8182 Return the gimple_val holding the result. */
8184 tree
8185 gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
8186 tree a)
8188 tree ret;
8190 ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
8191 STRIP_NOPS (ret);
8193 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
8194 GSI_SAME_STMT);
8199 /* Given a basic block B which ends with a conditional and has
8200 precisely two successors, determine which of the edges is taken if
8201 the conditional is true and which is taken if the conditional is
8202 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
8204 void
8205 extract_true_false_edges_from_block (basic_block b,
8206 edge *true_edge,
8207 edge *false_edge)
8209 edge e = EDGE_SUCC (b, 0);
8211 if (e->flags & EDGE_TRUE_VALUE)
8213 *true_edge = e;
8214 *false_edge = EDGE_SUCC (b, 1);
8216 else
8218 *false_edge = e;
8219 *true_edge = EDGE_SUCC (b, 1);
8223 /* Emit return warnings. */
8225 namespace {
8227 const pass_data pass_data_warn_function_return =
8229 GIMPLE_PASS, /* type */
8230 "*warn_function_return", /* name */
8231 OPTGROUP_NONE, /* optinfo_flags */
8232 TV_NONE, /* tv_id */
8233 PROP_cfg, /* properties_required */
8234 0, /* properties_provided */
8235 0, /* properties_destroyed */
8236 0, /* todo_flags_start */
8237 0, /* todo_flags_finish */
8240 class pass_warn_function_return : public gimple_opt_pass
8242 public:
8243 pass_warn_function_return (gcc::context *ctxt)
8244 : gimple_opt_pass (pass_data_warn_function_return, ctxt)
8247 /* opt_pass methods: */
8248 virtual unsigned int execute (function *);
8250 }; // class pass_warn_function_return
8252 unsigned int
8253 pass_warn_function_return::execute (function *fun)
8255 source_location location;
8256 gimple last;
8257 edge e;
8258 edge_iterator ei;
8260 if (!targetm.warn_func_return (fun->decl))
8261 return 0;
8263 /* If we have a path to EXIT, then we do return. */
8264 if (TREE_THIS_VOLATILE (fun->decl)
8265 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0)
8267 location = UNKNOWN_LOCATION;
8268 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
8270 last = last_stmt (e->src);
8271 if ((gimple_code (last) == GIMPLE_RETURN
8272 || gimple_call_builtin_p (last, BUILT_IN_RETURN))
8273 && (location = gimple_location (last)) != UNKNOWN_LOCATION)
8274 break;
8276 if (location == UNKNOWN_LOCATION)
8277 location = cfun->function_end_locus;
8278 warning_at (location, 0, "%<noreturn%> function does return");
8281 /* If we see "return;" in some basic block, then we do reach the end
8282 without returning a value. */
8283 else if (warn_return_type
8284 && !TREE_NO_WARNING (fun->decl)
8285 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0
8286 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fun->decl))))
8288 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
8290 gimple last = last_stmt (e->src);
8291 if (gimple_code (last) == GIMPLE_RETURN
8292 && gimple_return_retval (last) == NULL
8293 && !gimple_no_warning_p (last))
8295 location = gimple_location (last);
8296 if (location == UNKNOWN_LOCATION)
8297 location = fun->function_end_locus;
8298 warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
8299 TREE_NO_WARNING (fun->decl) = 1;
8300 break;
8304 return 0;
8307 } // anon namespace
8309 gimple_opt_pass *
8310 make_pass_warn_function_return (gcc::context *ctxt)
8312 return new pass_warn_function_return (ctxt);
8315 /* Walk a gimplified function and warn for functions whose return value is
8316 ignored and attribute((warn_unused_result)) is set. This is done before
8317 inlining, so we don't have to worry about that. */
8319 static void
8320 do_warn_unused_result (gimple_seq seq)
8322 tree fdecl, ftype;
8323 gimple_stmt_iterator i;
8325 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
8327 gimple g = gsi_stmt (i);
8329 switch (gimple_code (g))
8331 case GIMPLE_BIND:
8332 do_warn_unused_result (gimple_bind_body (g));
8333 break;
8334 case GIMPLE_TRY:
8335 do_warn_unused_result (gimple_try_eval (g));
8336 do_warn_unused_result (gimple_try_cleanup (g));
8337 break;
8338 case GIMPLE_CATCH:
8339 do_warn_unused_result (gimple_catch_handler (g));
8340 break;
8341 case GIMPLE_EH_FILTER:
8342 do_warn_unused_result (gimple_eh_filter_failure (g));
8343 break;
8345 case GIMPLE_CALL:
8346 if (gimple_call_lhs (g))
8347 break;
8348 if (gimple_call_internal_p (g))
8349 break;
8351 /* This is a naked call, as opposed to a GIMPLE_CALL with an
8352 LHS. All calls whose value is ignored should be
8353 represented like this. Look for the attribute. */
8354 fdecl = gimple_call_fndecl (g);
8355 ftype = gimple_call_fntype (g);
8357 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
8359 location_t loc = gimple_location (g);
8361 if (fdecl)
8362 warning_at (loc, OPT_Wunused_result,
8363 "ignoring return value of %qD, "
8364 "declared with attribute warn_unused_result",
8365 fdecl);
8366 else
8367 warning_at (loc, OPT_Wunused_result,
8368 "ignoring return value of function "
8369 "declared with attribute warn_unused_result");
8371 break;
8373 default:
8374 /* Not a container, not a call, or a call whose value is used. */
8375 break;
8380 namespace {
8382 const pass_data pass_data_warn_unused_result =
8384 GIMPLE_PASS, /* type */
8385 "*warn_unused_result", /* name */
8386 OPTGROUP_NONE, /* optinfo_flags */
8387 TV_NONE, /* tv_id */
8388 PROP_gimple_any, /* properties_required */
8389 0, /* properties_provided */
8390 0, /* properties_destroyed */
8391 0, /* todo_flags_start */
8392 0, /* todo_flags_finish */
8395 class pass_warn_unused_result : public gimple_opt_pass
8397 public:
8398 pass_warn_unused_result (gcc::context *ctxt)
8399 : gimple_opt_pass (pass_data_warn_unused_result, ctxt)
8402 /* opt_pass methods: */
8403 virtual bool gate (function *) { return flag_warn_unused_result; }
8404 virtual unsigned int execute (function *)
8406 do_warn_unused_result (gimple_body (current_function_decl));
8407 return 0;
8410 }; // class pass_warn_unused_result
8412 } // anon namespace
8414 gimple_opt_pass *
8415 make_pass_warn_unused_result (gcc::context *ctxt)
8417 return new pass_warn_unused_result (ctxt);
8420 /* IPA passes, compilation of earlier functions or inlining
8421 might have changed some properties, such as marked functions nothrow,
8422 pure, const or noreturn.
8423 Remove redundant edges and basic blocks, and create new ones if necessary.
8425 This pass can't be executed as stand alone pass from pass manager, because
8426 in between inlining and this fixup the verify_flow_info would fail. */
8428 unsigned int
8429 execute_fixup_cfg (void)
8431 basic_block bb;
8432 gimple_stmt_iterator gsi;
8433 int todo = 0;
8434 gcov_type count_scale;
8435 edge e;
8436 edge_iterator ei;
8438 count_scale
8439 = GCOV_COMPUTE_SCALE (cgraph_node::get (current_function_decl)->count,
8440 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
8442 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
8443 cgraph_node::get (current_function_decl)->count;
8444 EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
8445 apply_scale (EXIT_BLOCK_PTR_FOR_FN (cfun)->count,
8446 count_scale);
8448 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
8449 e->count = apply_scale (e->count, count_scale);
8451 FOR_EACH_BB_FN (bb, cfun)
8453 bb->count = apply_scale (bb->count, count_scale);
8454 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
8456 gimple stmt = gsi_stmt (gsi);
8457 tree decl = is_gimple_call (stmt)
8458 ? gimple_call_fndecl (stmt)
8459 : NULL;
8460 if (decl)
8462 int flags = gimple_call_flags (stmt);
8463 if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
8465 if (gimple_purge_dead_abnormal_call_edges (bb))
8466 todo |= TODO_cleanup_cfg;
8468 if (gimple_in_ssa_p (cfun))
8470 todo |= TODO_update_ssa | TODO_cleanup_cfg;
8471 update_stmt (stmt);
8475 if (flags & ECF_NORETURN
8476 && fixup_noreturn_call (stmt))
8477 todo |= TODO_cleanup_cfg;
8480 /* Remove stores to variables we marked write-only.
8481 Keep access when store has side effect, i.e. in case when source
8482 is volatile. */
8483 if (gimple_store_p (stmt)
8484 && !gimple_has_side_effects (stmt))
8486 tree lhs = get_base_address (gimple_get_lhs (stmt));
8488 if (TREE_CODE (lhs) == VAR_DECL
8489 && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
8490 && varpool_node::get (lhs)->writeonly)
8492 unlink_stmt_vdef (stmt);
8493 gsi_remove (&gsi, true);
8494 release_defs (stmt);
8495 todo |= TODO_update_ssa | TODO_cleanup_cfg;
8496 continue;
8499 /* For calls we can simply remove LHS when it is known
8500 to be write-only. */
8501 if (is_gimple_call (stmt)
8502 && gimple_get_lhs (stmt))
8504 tree lhs = get_base_address (gimple_get_lhs (stmt));
8506 if (TREE_CODE (lhs) == VAR_DECL
8507 && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
8508 && varpool_node::get (lhs)->writeonly)
8510 gimple_call_set_lhs (stmt, NULL);
8511 update_stmt (stmt);
8512 todo |= TODO_update_ssa | TODO_cleanup_cfg;
8516 if (maybe_clean_eh_stmt (stmt)
8517 && gimple_purge_dead_eh_edges (bb))
8518 todo |= TODO_cleanup_cfg;
8519 gsi_next (&gsi);
8522 FOR_EACH_EDGE (e, ei, bb->succs)
8523 e->count = apply_scale (e->count, count_scale);
8525 /* If we have a basic block with no successors that does not
8526 end with a control statement or a noreturn call end it with
8527 a call to __builtin_unreachable. This situation can occur
8528 when inlining a noreturn call that does in fact return. */
8529 if (EDGE_COUNT (bb->succs) == 0)
8531 gimple stmt = last_stmt (bb);
8532 if (!stmt
8533 || (!is_ctrl_stmt (stmt)
8534 && (!is_gimple_call (stmt)
8535 || (gimple_call_flags (stmt) & ECF_NORETURN) == 0)))
8537 stmt = gimple_build_call
8538 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
8539 gimple_stmt_iterator gsi = gsi_last_bb (bb);
8540 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
8544 if (count_scale != REG_BR_PROB_BASE)
8545 compute_function_frequency ();
8547 /* We just processed all calls. */
8548 if (cfun->gimple_df)
8549 vec_free (MODIFIED_NORETURN_CALLS (cfun));
8551 /* Dump a textual representation of the flowgraph. */
8552 if (dump_file)
8553 gimple_dump_cfg (dump_file, dump_flags);
8555 if (current_loops
8556 && (todo & TODO_cleanup_cfg))
8557 loops_state_set (LOOPS_NEED_FIXUP);
8559 return todo;
8562 namespace {
8564 const pass_data pass_data_fixup_cfg =
8566 GIMPLE_PASS, /* type */
8567 "*free_cfg_annotations", /* name */
8568 OPTGROUP_NONE, /* optinfo_flags */
8569 TV_NONE, /* tv_id */
8570 PROP_cfg, /* properties_required */
8571 0, /* properties_provided */
8572 0, /* properties_destroyed */
8573 0, /* todo_flags_start */
8574 0, /* todo_flags_finish */
8577 class pass_fixup_cfg : public gimple_opt_pass
8579 public:
8580 pass_fixup_cfg (gcc::context *ctxt)
8581 : gimple_opt_pass (pass_data_fixup_cfg, ctxt)
8584 /* opt_pass methods: */
8585 opt_pass * clone () { return new pass_fixup_cfg (m_ctxt); }
8586 virtual unsigned int execute (function *) { return execute_fixup_cfg (); }
8588 }; // class pass_fixup_cfg
8590 } // anon namespace
8592 gimple_opt_pass *
8593 make_pass_fixup_cfg (gcc::context *ctxt)
8595 return new pass_fixup_cfg (ctxt);
8598 /* Garbage collection support for edge_def. */
8600 extern void gt_ggc_mx (tree&);
8601 extern void gt_ggc_mx (gimple&);
8602 extern void gt_ggc_mx (rtx&);
8603 extern void gt_ggc_mx (basic_block&);
8605 void
8606 gt_ggc_mx (edge_def *e)
8608 tree block = LOCATION_BLOCK (e->goto_locus);
8609 gt_ggc_mx (e->src);
8610 gt_ggc_mx (e->dest);
8611 if (current_ir_type () == IR_GIMPLE)
8612 gt_ggc_mx (e->insns.g);
8613 else
8614 gt_ggc_mx (e->insns.r);
8615 gt_ggc_mx (block);
8618 /* PCH support for edge_def. */
8620 extern void gt_pch_nx (tree&);
8621 extern void gt_pch_nx (gimple&);
8622 extern void gt_pch_nx (rtx&);
8623 extern void gt_pch_nx (basic_block&);
8625 void
8626 gt_pch_nx (edge_def *e)
8628 tree block = LOCATION_BLOCK (e->goto_locus);
8629 gt_pch_nx (e->src);
8630 gt_pch_nx (e->dest);
8631 if (current_ir_type () == IR_GIMPLE)
8632 gt_pch_nx (e->insns.g);
8633 else
8634 gt_pch_nx (e->insns.r);
8635 gt_pch_nx (block);
8638 void
8639 gt_pch_nx (edge_def *e, gt_pointer_operator op, void *cookie)
8641 tree block = LOCATION_BLOCK (e->goto_locus);
8642 op (&(e->src), cookie);
8643 op (&(e->dest), cookie);
8644 if (current_ir_type () == IR_GIMPLE)
8645 op (&(e->insns.g), cookie);
8646 else
8647 op (&(e->insns.r), cookie);
8648 op (&(block), cookie);