* go.test/go-test.exp: In +build lines, require whitespace around
[official-gcc.git] / gcc / tree-cfg.c
blobee10bc614b1c265b370efaa86b3b83f8900a952d
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 "predict.h"
33 #include "vec.h"
34 #include "hashtab.h"
35 #include "hash-set.h"
36 #include "machmode.h"
37 #include "hard-reg-set.h"
38 #include "input.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "cfganal.h"
43 #include "basic-block.h"
44 #include "flags.h"
45 #include "gimple-pretty-print.h"
46 #include "tree-ssa-alias.h"
47 #include "internal-fn.h"
48 #include "gimple-fold.h"
49 #include "tree-eh.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimple-iterator.h"
54 #include "gimplify-me.h"
55 #include "gimple-walk.h"
56 #include "gimple-ssa.h"
57 #include "plugin-api.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "tree-cfg.h"
61 #include "tree-phinodes.h"
62 #include "ssa-iterators.h"
63 #include "stringpool.h"
64 #include "tree-ssanames.h"
65 #include "tree-ssa-loop-manip.h"
66 #include "tree-ssa-loop-niter.h"
67 #include "tree-into-ssa.h"
68 #include "expr.h"
69 #include "tree-dfa.h"
70 #include "tree-ssa.h"
71 #include "tree-dump.h"
72 #include "tree-pass.h"
73 #include "diagnostic-core.h"
74 #include "except.h"
75 #include "cfgloop.h"
76 #include "tree-ssa-propagate.h"
77 #include "value-prof.h"
78 #include "tree-inline.h"
79 #include "target.h"
80 #include "tree-ssa-live.h"
81 #include "omp-low.h"
82 #include "tree-cfgcleanup.h"
83 #include "wide-int.h"
84 #include "wide-int-print.h"
86 /* This file contains functions for building the Control Flow Graph (CFG)
87 for a function tree. */
89 /* Local declarations. */
91 /* Initial capacity for the basic block array. */
92 static const int initial_cfg_capacity = 20;
94 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
95 which use a particular edge. The CASE_LABEL_EXPRs are chained together
96 via their CASE_CHAIN field, which we clear after we're done with the
97 hash table to prevent problems with duplication of GIMPLE_SWITCHes.
99 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
100 update the case vector in response to edge redirections.
102 Right now this table is set up and torn down at key points in the
103 compilation process. It would be nice if we could make the table
104 more persistent. The key is getting notification of changes to
105 the CFG (particularly edge removal, creation and redirection). */
107 static hash_map<edge, tree> *edge_to_cases;
109 /* If we record edge_to_cases, this bitmap will hold indexes
110 of basic blocks that end in a GIMPLE_SWITCH which we touched
111 due to edge manipulations. */
113 static bitmap touched_switch_bbs;
115 /* CFG statistics. */
116 struct cfg_stats_d
118 long num_merged_labels;
121 static struct cfg_stats_d cfg_stats;
123 /* Hash table to store last discriminator assigned for each locus. */
124 struct locus_discrim_map
126 location_t locus;
127 int discriminator;
130 /* Hashtable helpers. */
132 struct locus_discrim_hasher : typed_free_remove <locus_discrim_map>
134 typedef locus_discrim_map value_type;
135 typedef locus_discrim_map compare_type;
136 static inline hashval_t hash (const value_type *);
137 static inline bool equal (const value_type *, const compare_type *);
140 /* Trivial hash function for a location_t. ITEM is a pointer to
141 a hash table entry that maps a location_t to a discriminator. */
143 inline hashval_t
144 locus_discrim_hasher::hash (const value_type *item)
146 return LOCATION_LINE (item->locus);
149 /* Equality function for the locus-to-discriminator map. A and B
150 point to the two hash table entries to compare. */
152 inline bool
153 locus_discrim_hasher::equal (const value_type *a, const compare_type *b)
155 return LOCATION_LINE (a->locus) == LOCATION_LINE (b->locus);
158 static hash_table<locus_discrim_hasher> *discriminator_per_locus;
160 /* Basic blocks and flowgraphs. */
161 static void make_blocks (gimple_seq);
163 /* Edges. */
164 static void make_edges (void);
165 static void assign_discriminators (void);
166 static void make_cond_expr_edges (basic_block);
167 static void make_gimple_switch_edges (basic_block);
168 static bool make_goto_expr_edges (basic_block);
169 static void make_gimple_asm_edges (basic_block);
170 static edge gimple_redirect_edge_and_branch (edge, basic_block);
171 static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
173 /* Various helpers. */
174 static inline bool stmt_starts_bb_p (gimple, gimple);
175 static int gimple_verify_flow_info (void);
176 static void gimple_make_forwarder_block (edge);
177 static gimple first_non_label_stmt (basic_block);
178 static bool verify_gimple_transaction (gimple);
179 static bool call_can_make_abnormal_goto (gimple);
181 /* Flowgraph optimization and cleanup. */
182 static void gimple_merge_blocks (basic_block, basic_block);
183 static bool gimple_can_merge_blocks_p (basic_block, basic_block);
184 static void remove_bb (basic_block);
185 static edge find_taken_edge_computed_goto (basic_block, tree);
186 static edge find_taken_edge_cond_expr (basic_block, tree);
187 static edge find_taken_edge_switch_expr (basic_block, tree);
188 static tree find_case_label_for_value (gimple, tree);
190 void
191 init_empty_tree_cfg_for_function (struct function *fn)
193 /* Initialize the basic block array. */
194 init_flow (fn);
195 profile_status_for_fn (fn) = PROFILE_ABSENT;
196 n_basic_blocks_for_fn (fn) = NUM_FIXED_BLOCKS;
197 last_basic_block_for_fn (fn) = NUM_FIXED_BLOCKS;
198 vec_alloc (basic_block_info_for_fn (fn), initial_cfg_capacity);
199 vec_safe_grow_cleared (basic_block_info_for_fn (fn),
200 initial_cfg_capacity);
202 /* Build a mapping of labels to their associated blocks. */
203 vec_alloc (label_to_block_map_for_fn (fn), initial_cfg_capacity);
204 vec_safe_grow_cleared (label_to_block_map_for_fn (fn),
205 initial_cfg_capacity);
207 SET_BASIC_BLOCK_FOR_FN (fn, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (fn));
208 SET_BASIC_BLOCK_FOR_FN (fn, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (fn));
210 ENTRY_BLOCK_PTR_FOR_FN (fn)->next_bb
211 = EXIT_BLOCK_PTR_FOR_FN (fn);
212 EXIT_BLOCK_PTR_FOR_FN (fn)->prev_bb
213 = ENTRY_BLOCK_PTR_FOR_FN (fn);
216 void
217 init_empty_tree_cfg (void)
219 init_empty_tree_cfg_for_function (cfun);
222 /*---------------------------------------------------------------------------
223 Create basic blocks
224 ---------------------------------------------------------------------------*/
226 /* Entry point to the CFG builder for trees. SEQ is the sequence of
227 statements to be added to the flowgraph. */
229 static void
230 build_gimple_cfg (gimple_seq seq)
232 /* Register specific gimple functions. */
233 gimple_register_cfg_hooks ();
235 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
237 init_empty_tree_cfg ();
239 make_blocks (seq);
241 /* Make sure there is always at least one block, even if it's empty. */
242 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
243 create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
245 /* Adjust the size of the array. */
246 if (basic_block_info_for_fn (cfun)->length ()
247 < (size_t) n_basic_blocks_for_fn (cfun))
248 vec_safe_grow_cleared (basic_block_info_for_fn (cfun),
249 n_basic_blocks_for_fn (cfun));
251 /* To speed up statement iterator walks, we first purge dead labels. */
252 cleanup_dead_labels ();
254 /* Group case nodes to reduce the number of edges.
255 We do this after cleaning up dead labels because otherwise we miss
256 a lot of obvious case merging opportunities. */
257 group_case_labels ();
259 /* Create the edges of the flowgraph. */
260 discriminator_per_locus = new hash_table<locus_discrim_hasher> (13);
261 make_edges ();
262 assign_discriminators ();
263 cleanup_dead_labels ();
264 delete discriminator_per_locus;
265 discriminator_per_locus = NULL;
269 /* Look for ANNOTATE calls with loop annotation kind; if found, remove
270 them and propagate the information to the loop. We assume that the
271 annotations come immediately before the condition of the loop. */
273 static void
274 replace_loop_annotate ()
276 struct loop *loop;
277 basic_block bb;
278 gimple_stmt_iterator gsi;
279 gimple stmt;
281 FOR_EACH_LOOP (loop, 0)
283 gsi = gsi_last_bb (loop->header);
284 stmt = gsi_stmt (gsi);
285 if (!(stmt && gimple_code (stmt) == GIMPLE_COND))
286 continue;
287 for (gsi_prev_nondebug (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
289 stmt = gsi_stmt (gsi);
290 if (gimple_code (stmt) != GIMPLE_CALL)
291 break;
292 if (!gimple_call_internal_p (stmt)
293 || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
294 break;
295 switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
297 case annot_expr_ivdep_kind:
298 loop->safelen = INT_MAX;
299 break;
300 case annot_expr_no_vector_kind:
301 loop->dont_vectorize = true;
302 break;
303 case annot_expr_vector_kind:
304 loop->force_vectorize = true;
305 cfun->has_force_vectorize_loops = true;
306 break;
307 default:
308 gcc_unreachable ();
310 stmt = gimple_build_assign (gimple_call_lhs (stmt),
311 gimple_call_arg (stmt, 0));
312 gsi_replace (&gsi, stmt, true);
316 /* Remove IFN_ANNOTATE. Safeguard for the case loop->latch == NULL. */
317 FOR_EACH_BB_FN (bb, cfun)
319 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
321 stmt = gsi_stmt (gsi);
322 if (gimple_code (stmt) != GIMPLE_CALL)
323 break;
324 if (!gimple_call_internal_p (stmt)
325 || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
326 break;
327 switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
329 case annot_expr_ivdep_kind:
330 case annot_expr_no_vector_kind:
331 case annot_expr_vector_kind:
332 break;
333 default:
334 gcc_unreachable ();
336 warning_at (gimple_location (stmt), 0, "ignoring loop annotation");
337 stmt = gimple_build_assign (gimple_call_lhs (stmt),
338 gimple_call_arg (stmt, 0));
339 gsi_replace (&gsi, stmt, true);
345 static unsigned int
346 execute_build_cfg (void)
348 gimple_seq body = gimple_body (current_function_decl);
350 build_gimple_cfg (body);
351 gimple_set_body (current_function_decl, NULL);
352 if (dump_file && (dump_flags & TDF_DETAILS))
354 fprintf (dump_file, "Scope blocks:\n");
355 dump_scope_blocks (dump_file, dump_flags);
357 cleanup_tree_cfg ();
358 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
359 replace_loop_annotate ();
360 return 0;
363 namespace {
365 const pass_data pass_data_build_cfg =
367 GIMPLE_PASS, /* type */
368 "cfg", /* name */
369 OPTGROUP_NONE, /* optinfo_flags */
370 TV_TREE_CFG, /* tv_id */
371 PROP_gimple_leh, /* properties_required */
372 ( PROP_cfg | PROP_loops ), /* properties_provided */
373 0, /* properties_destroyed */
374 0, /* todo_flags_start */
375 0, /* todo_flags_finish */
378 class pass_build_cfg : public gimple_opt_pass
380 public:
381 pass_build_cfg (gcc::context *ctxt)
382 : gimple_opt_pass (pass_data_build_cfg, ctxt)
385 /* opt_pass methods: */
386 virtual unsigned int execute (function *) { return execute_build_cfg (); }
388 }; // class pass_build_cfg
390 } // anon namespace
392 gimple_opt_pass *
393 make_pass_build_cfg (gcc::context *ctxt)
395 return new pass_build_cfg (ctxt);
399 /* Return true if T is a computed goto. */
401 bool
402 computed_goto_p (gimple t)
404 return (gimple_code (t) == GIMPLE_GOTO
405 && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
408 /* Returns true for edge E where e->src ends with a GIMPLE_COND and
409 the other edge points to a bb with just __builtin_unreachable ().
410 I.e. return true for C->M edge in:
411 <bb C>:
413 if (something)
414 goto <bb N>;
415 else
416 goto <bb M>;
417 <bb N>:
418 __builtin_unreachable ();
419 <bb M>: */
421 bool
422 assert_unreachable_fallthru_edge_p (edge e)
424 basic_block pred_bb = e->src;
425 gimple last = last_stmt (pred_bb);
426 if (last && gimple_code (last) == GIMPLE_COND)
428 basic_block other_bb = EDGE_SUCC (pred_bb, 0)->dest;
429 if (other_bb == e->dest)
430 other_bb = EDGE_SUCC (pred_bb, 1)->dest;
431 if (EDGE_COUNT (other_bb->succs) == 0)
433 gimple_stmt_iterator gsi = gsi_after_labels (other_bb);
434 gimple stmt;
436 if (gsi_end_p (gsi))
437 return false;
438 stmt = gsi_stmt (gsi);
439 while (is_gimple_debug (stmt) || gimple_clobber_p (stmt))
441 gsi_next (&gsi);
442 if (gsi_end_p (gsi))
443 return false;
444 stmt = gsi_stmt (gsi);
446 return gimple_call_builtin_p (stmt, BUILT_IN_UNREACHABLE);
449 return false;
453 /* Initialize GF_CALL_CTRL_ALTERING flag, which indicates the call
454 could alter control flow except via eh. We initialize the flag at
455 CFG build time and only ever clear it later. */
457 static void
458 gimple_call_initialize_ctrl_altering (gimple stmt)
460 int flags = gimple_call_flags (stmt);
462 /* A call alters control flow if it can make an abnormal goto. */
463 if (call_can_make_abnormal_goto (stmt)
464 /* A call also alters control flow if it does not return. */
465 || flags & ECF_NORETURN
466 /* TM ending statements have backedges out of the transaction.
467 Return true so we split the basic block containing them.
468 Note that the TM_BUILTIN test is merely an optimization. */
469 || ((flags & ECF_TM_BUILTIN)
470 && is_tm_ending_fndecl (gimple_call_fndecl (stmt)))
471 /* BUILT_IN_RETURN call is same as return statement. */
472 || gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
473 gimple_call_set_ctrl_altering (stmt, true);
474 else
475 gimple_call_set_ctrl_altering (stmt, false);
479 /* Build a flowgraph for the sequence of stmts SEQ. */
481 static void
482 make_blocks (gimple_seq seq)
484 gimple_stmt_iterator i = gsi_start (seq);
485 gimple stmt = NULL;
486 bool start_new_block = true;
487 bool first_stmt_of_seq = true;
488 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
490 while (!gsi_end_p (i))
492 gimple prev_stmt;
494 prev_stmt = stmt;
495 stmt = gsi_stmt (i);
497 if (stmt && is_gimple_call (stmt))
498 gimple_call_initialize_ctrl_altering (stmt);
500 /* If the statement starts a new basic block or if we have determined
501 in a previous pass that we need to create a new block for STMT, do
502 so now. */
503 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
505 if (!first_stmt_of_seq)
506 gsi_split_seq_before (&i, &seq);
507 bb = create_basic_block (seq, NULL, bb);
508 start_new_block = false;
511 /* Now add STMT to BB and create the subgraphs for special statement
512 codes. */
513 gimple_set_bb (stmt, bb);
515 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
516 next iteration. */
517 if (stmt_ends_bb_p (stmt))
519 /* If the stmt can make abnormal goto use a new temporary
520 for the assignment to the LHS. This makes sure the old value
521 of the LHS is available on the abnormal edge. Otherwise
522 we will end up with overlapping life-ranges for abnormal
523 SSA names. */
524 if (gimple_has_lhs (stmt)
525 && stmt_can_make_abnormal_goto (stmt)
526 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
528 tree lhs = gimple_get_lhs (stmt);
529 tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
530 gimple s = gimple_build_assign (lhs, tmp);
531 gimple_set_location (s, gimple_location (stmt));
532 gimple_set_block (s, gimple_block (stmt));
533 gimple_set_lhs (stmt, tmp);
534 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
535 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
536 DECL_GIMPLE_REG_P (tmp) = 1;
537 gsi_insert_after (&i, s, GSI_SAME_STMT);
539 start_new_block = true;
542 gsi_next (&i);
543 first_stmt_of_seq = false;
548 /* Create and return a new empty basic block after bb AFTER. */
550 static basic_block
551 create_bb (void *h, void *e, basic_block after)
553 basic_block bb;
555 gcc_assert (!e);
557 /* Create and initialize a new basic block. Since alloc_block uses
558 GC allocation that clears memory to allocate a basic block, we do
559 not have to clear the newly allocated basic block here. */
560 bb = alloc_block ();
562 bb->index = last_basic_block_for_fn (cfun);
563 bb->flags = BB_NEW;
564 set_bb_seq (bb, h ? (gimple_seq) h : NULL);
566 /* Add the new block to the linked list of blocks. */
567 link_block (bb, after);
569 /* Grow the basic block array if needed. */
570 if ((size_t) last_basic_block_for_fn (cfun)
571 == basic_block_info_for_fn (cfun)->length ())
573 size_t new_size =
574 (last_basic_block_for_fn (cfun)
575 + (last_basic_block_for_fn (cfun) + 3) / 4);
576 vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size);
579 /* Add the newly created block to the array. */
580 SET_BASIC_BLOCK_FOR_FN (cfun, last_basic_block_for_fn (cfun), bb);
582 n_basic_blocks_for_fn (cfun)++;
583 last_basic_block_for_fn (cfun)++;
585 return bb;
589 /*---------------------------------------------------------------------------
590 Edge creation
591 ---------------------------------------------------------------------------*/
593 /* Fold COND_EXPR_COND of each COND_EXPR. */
595 void
596 fold_cond_expr_cond (void)
598 basic_block bb;
600 FOR_EACH_BB_FN (bb, cfun)
602 gimple stmt = last_stmt (bb);
604 if (stmt && gimple_code (stmt) == GIMPLE_COND)
606 location_t loc = gimple_location (stmt);
607 tree cond;
608 bool zerop, onep;
610 fold_defer_overflow_warnings ();
611 cond = fold_binary_loc (loc, gimple_cond_code (stmt), boolean_type_node,
612 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
613 if (cond)
615 zerop = integer_zerop (cond);
616 onep = integer_onep (cond);
618 else
619 zerop = onep = false;
621 fold_undefer_overflow_warnings (zerop || onep,
622 stmt,
623 WARN_STRICT_OVERFLOW_CONDITIONAL);
624 if (zerop)
625 gimple_cond_make_false (stmt);
626 else if (onep)
627 gimple_cond_make_true (stmt);
632 /* If basic block BB has an abnormal edge to a basic block
633 containing IFN_ABNORMAL_DISPATCHER internal call, return
634 that the dispatcher's basic block, otherwise return NULL. */
636 basic_block
637 get_abnormal_succ_dispatcher (basic_block bb)
639 edge e;
640 edge_iterator ei;
642 FOR_EACH_EDGE (e, ei, bb->succs)
643 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
645 gimple_stmt_iterator gsi
646 = gsi_start_nondebug_after_labels_bb (e->dest);
647 gimple g = gsi_stmt (gsi);
648 if (g
649 && is_gimple_call (g)
650 && gimple_call_internal_p (g)
651 && gimple_call_internal_fn (g) == IFN_ABNORMAL_DISPATCHER)
652 return e->dest;
654 return NULL;
657 /* Helper function for make_edges. Create a basic block with
658 with ABNORMAL_DISPATCHER internal call in it if needed, and
659 create abnormal edges from BBS to it and from it to FOR_BB
660 if COMPUTED_GOTO is false, otherwise factor the computed gotos. */
662 static void
663 handle_abnormal_edges (basic_block *dispatcher_bbs,
664 basic_block for_bb, int *bb_to_omp_idx,
665 auto_vec<basic_block> *bbs, bool computed_goto)
667 basic_block *dispatcher = dispatcher_bbs + (computed_goto ? 1 : 0);
668 unsigned int idx = 0;
669 basic_block bb;
670 bool inner = false;
672 if (bb_to_omp_idx)
674 dispatcher = dispatcher_bbs + 2 * bb_to_omp_idx[for_bb->index];
675 if (bb_to_omp_idx[for_bb->index] != 0)
676 inner = true;
679 /* If the dispatcher has been created already, then there are basic
680 blocks with abnormal edges to it, so just make a new edge to
681 for_bb. */
682 if (*dispatcher == NULL)
684 /* Check if there are any basic blocks that need to have
685 abnormal edges to this dispatcher. If there are none, return
686 early. */
687 if (bb_to_omp_idx == NULL)
689 if (bbs->is_empty ())
690 return;
692 else
694 FOR_EACH_VEC_ELT (*bbs, idx, bb)
695 if (bb_to_omp_idx[bb->index] == bb_to_omp_idx[for_bb->index])
696 break;
697 if (bb == NULL)
698 return;
701 /* Create the dispatcher bb. */
702 *dispatcher = create_basic_block (NULL, NULL, for_bb);
703 if (computed_goto)
705 /* Factor computed gotos into a common computed goto site. Also
706 record the location of that site so that we can un-factor the
707 gotos after we have converted back to normal form. */
708 gimple_stmt_iterator gsi = gsi_start_bb (*dispatcher);
710 /* Create the destination of the factored goto. Each original
711 computed goto will put its desired destination into this
712 variable and jump to the label we create immediately below. */
713 tree var = create_tmp_var (ptr_type_node, "gotovar");
715 /* Build a label for the new block which will contain the
716 factored computed goto. */
717 tree factored_label_decl
718 = create_artificial_label (UNKNOWN_LOCATION);
719 gimple factored_computed_goto_label
720 = gimple_build_label (factored_label_decl);
721 gsi_insert_after (&gsi, factored_computed_goto_label, GSI_NEW_STMT);
723 /* Build our new computed goto. */
724 gimple factored_computed_goto = gimple_build_goto (var);
725 gsi_insert_after (&gsi, factored_computed_goto, GSI_NEW_STMT);
727 FOR_EACH_VEC_ELT (*bbs, idx, bb)
729 if (bb_to_omp_idx
730 && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
731 continue;
733 gsi = gsi_last_bb (bb);
734 gimple last = gsi_stmt (gsi);
736 gcc_assert (computed_goto_p (last));
738 /* Copy the original computed goto's destination into VAR. */
739 gimple assignment
740 = gimple_build_assign (var, gimple_goto_dest (last));
741 gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
743 edge e = make_edge (bb, *dispatcher, EDGE_FALLTHRU);
744 e->goto_locus = gimple_location (last);
745 gsi_remove (&gsi, true);
748 else
750 tree arg = inner ? boolean_true_node : boolean_false_node;
751 gimple g = gimple_build_call_internal (IFN_ABNORMAL_DISPATCHER,
752 1, arg);
753 gimple_stmt_iterator gsi = gsi_after_labels (*dispatcher);
754 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
756 /* Create predecessor edges of the dispatcher. */
757 FOR_EACH_VEC_ELT (*bbs, idx, bb)
759 if (bb_to_omp_idx
760 && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
761 continue;
762 make_edge (bb, *dispatcher, EDGE_ABNORMAL);
767 make_edge (*dispatcher, for_bb, EDGE_ABNORMAL);
770 /* Join all the blocks in the flowgraph. */
772 static void
773 make_edges (void)
775 basic_block bb;
776 struct omp_region *cur_region = NULL;
777 auto_vec<basic_block> ab_edge_goto;
778 auto_vec<basic_block> ab_edge_call;
779 int *bb_to_omp_idx = NULL;
780 int cur_omp_region_idx = 0;
782 /* Create an edge from entry to the first block with executable
783 statements in it. */
784 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun),
785 BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS),
786 EDGE_FALLTHRU);
788 /* Traverse the basic block array placing edges. */
789 FOR_EACH_BB_FN (bb, cfun)
791 gimple last = last_stmt (bb);
792 bool fallthru;
794 if (bb_to_omp_idx)
795 bb_to_omp_idx[bb->index] = cur_omp_region_idx;
797 if (last)
799 enum gimple_code code = gimple_code (last);
800 switch (code)
802 case GIMPLE_GOTO:
803 if (make_goto_expr_edges (bb))
804 ab_edge_goto.safe_push (bb);
805 fallthru = false;
806 break;
807 case GIMPLE_RETURN:
809 edge e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
810 e->goto_locus = gimple_location (last);
811 fallthru = false;
813 break;
814 case GIMPLE_COND:
815 make_cond_expr_edges (bb);
816 fallthru = false;
817 break;
818 case GIMPLE_SWITCH:
819 make_gimple_switch_edges (bb);
820 fallthru = false;
821 break;
822 case GIMPLE_RESX:
823 make_eh_edges (last);
824 fallthru = false;
825 break;
826 case GIMPLE_EH_DISPATCH:
827 fallthru = make_eh_dispatch_edges (last);
828 break;
830 case GIMPLE_CALL:
831 /* If this function receives a nonlocal goto, then we need to
832 make edges from this call site to all the nonlocal goto
833 handlers. */
834 if (stmt_can_make_abnormal_goto (last))
835 ab_edge_call.safe_push (bb);
837 /* If this statement has reachable exception handlers, then
838 create abnormal edges to them. */
839 make_eh_edges (last);
841 /* BUILTIN_RETURN is really a return statement. */
842 if (gimple_call_builtin_p (last, BUILT_IN_RETURN))
844 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
845 fallthru = false;
847 /* Some calls are known not to return. */
848 else
849 fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
850 break;
852 case GIMPLE_ASSIGN:
853 /* A GIMPLE_ASSIGN may throw internally and thus be considered
854 control-altering. */
855 if (is_ctrl_altering_stmt (last))
856 make_eh_edges (last);
857 fallthru = true;
858 break;
860 case GIMPLE_ASM:
861 make_gimple_asm_edges (bb);
862 fallthru = true;
863 break;
865 CASE_GIMPLE_OMP:
866 fallthru = make_gimple_omp_edges (bb, &cur_region,
867 &cur_omp_region_idx);
868 if (cur_region && bb_to_omp_idx == NULL)
869 bb_to_omp_idx = XCNEWVEC (int, n_basic_blocks_for_fn (cfun));
870 break;
872 case GIMPLE_TRANSACTION:
874 tree abort_label = gimple_transaction_label (last);
875 if (abort_label)
876 make_edge (bb, label_to_block (abort_label), EDGE_TM_ABORT);
877 fallthru = true;
879 break;
881 default:
882 gcc_assert (!stmt_ends_bb_p (last));
883 fallthru = true;
886 else
887 fallthru = true;
889 if (fallthru)
890 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
893 /* Computed gotos are hell to deal with, especially if there are
894 lots of them with a large number of destinations. So we factor
895 them to a common computed goto location before we build the
896 edge list. After we convert back to normal form, we will un-factor
897 the computed gotos since factoring introduces an unwanted jump.
898 For non-local gotos and abnormal edges from calls to calls that return
899 twice or forced labels, factor the abnormal edges too, by having all
900 abnormal edges from the calls go to a common artificial basic block
901 with ABNORMAL_DISPATCHER internal call and abnormal edges from that
902 basic block to all forced labels and calls returning twice.
903 We do this per-OpenMP structured block, because those regions
904 are guaranteed to be single entry single exit by the standard,
905 so it is not allowed to enter or exit such regions abnormally this way,
906 thus all computed gotos, non-local gotos and setjmp/longjmp calls
907 must not transfer control across SESE region boundaries. */
908 if (!ab_edge_goto.is_empty () || !ab_edge_call.is_empty ())
910 gimple_stmt_iterator gsi;
911 basic_block dispatcher_bb_array[2] = { NULL, NULL };
912 basic_block *dispatcher_bbs = dispatcher_bb_array;
913 int count = n_basic_blocks_for_fn (cfun);
915 if (bb_to_omp_idx)
916 dispatcher_bbs = XCNEWVEC (basic_block, 2 * count);
918 FOR_EACH_BB_FN (bb, cfun)
920 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
922 gimple label_stmt = gsi_stmt (gsi);
923 tree target;
925 if (gimple_code (label_stmt) != GIMPLE_LABEL)
926 break;
928 target = gimple_label_label (label_stmt);
930 /* Make an edge to every label block that has been marked as a
931 potential target for a computed goto or a non-local goto. */
932 if (FORCED_LABEL (target))
933 handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
934 &ab_edge_goto, true);
935 if (DECL_NONLOCAL (target))
937 handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
938 &ab_edge_call, false);
939 break;
943 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
944 gsi_next_nondebug (&gsi);
945 if (!gsi_end_p (gsi))
947 /* Make an edge to every setjmp-like call. */
948 gimple call_stmt = gsi_stmt (gsi);
949 if (is_gimple_call (call_stmt)
950 && ((gimple_call_flags (call_stmt) & ECF_RETURNS_TWICE)
951 || gimple_call_builtin_p (call_stmt,
952 BUILT_IN_SETJMP_RECEIVER)))
953 handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
954 &ab_edge_call, false);
958 if (bb_to_omp_idx)
959 XDELETE (dispatcher_bbs);
962 XDELETE (bb_to_omp_idx);
964 free_omp_regions ();
966 /* Fold COND_EXPR_COND of each COND_EXPR. */
967 fold_cond_expr_cond ();
970 /* Find the next available discriminator value for LOCUS. The
971 discriminator distinguishes among several basic blocks that
972 share a common locus, allowing for more accurate sample-based
973 profiling. */
975 static int
976 next_discriminator_for_locus (location_t locus)
978 struct locus_discrim_map item;
979 struct locus_discrim_map **slot;
981 item.locus = locus;
982 item.discriminator = 0;
983 slot = discriminator_per_locus->find_slot_with_hash (
984 &item, LOCATION_LINE (locus), INSERT);
985 gcc_assert (slot);
986 if (*slot == HTAB_EMPTY_ENTRY)
988 *slot = XNEW (struct locus_discrim_map);
989 gcc_assert (*slot);
990 (*slot)->locus = locus;
991 (*slot)->discriminator = 0;
993 (*slot)->discriminator++;
994 return (*slot)->discriminator;
997 /* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line. */
999 static bool
1000 same_line_p (location_t locus1, location_t locus2)
1002 expanded_location from, to;
1004 if (locus1 == locus2)
1005 return true;
1007 from = expand_location (locus1);
1008 to = expand_location (locus2);
1010 if (from.line != to.line)
1011 return false;
1012 if (from.file == to.file)
1013 return true;
1014 return (from.file != NULL
1015 && to.file != NULL
1016 && filename_cmp (from.file, to.file) == 0);
1019 /* Assign discriminators to each basic block. */
1021 static void
1022 assign_discriminators (void)
1024 basic_block bb;
1026 FOR_EACH_BB_FN (bb, cfun)
1028 edge e;
1029 edge_iterator ei;
1030 gimple last = last_stmt (bb);
1031 location_t locus = last ? gimple_location (last) : UNKNOWN_LOCATION;
1033 if (locus == UNKNOWN_LOCATION)
1034 continue;
1036 FOR_EACH_EDGE (e, ei, bb->succs)
1038 gimple first = first_non_label_stmt (e->dest);
1039 gimple last = last_stmt (e->dest);
1040 if ((first && same_line_p (locus, gimple_location (first)))
1041 || (last && same_line_p (locus, gimple_location (last))))
1043 if (e->dest->discriminator != 0 && bb->discriminator == 0)
1044 bb->discriminator = next_discriminator_for_locus (locus);
1045 else
1046 e->dest->discriminator = next_discriminator_for_locus (locus);
1052 /* Create the edges for a GIMPLE_COND starting at block BB. */
1054 static void
1055 make_cond_expr_edges (basic_block bb)
1057 gimple entry = last_stmt (bb);
1058 gimple then_stmt, else_stmt;
1059 basic_block then_bb, else_bb;
1060 tree then_label, else_label;
1061 edge e;
1063 gcc_assert (entry);
1064 gcc_assert (gimple_code (entry) == GIMPLE_COND);
1066 /* Entry basic blocks for each component. */
1067 then_label = gimple_cond_true_label (entry);
1068 else_label = gimple_cond_false_label (entry);
1069 then_bb = label_to_block (then_label);
1070 else_bb = label_to_block (else_label);
1071 then_stmt = first_stmt (then_bb);
1072 else_stmt = first_stmt (else_bb);
1074 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1075 e->goto_locus = gimple_location (then_stmt);
1076 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1077 if (e)
1078 e->goto_locus = gimple_location (else_stmt);
1080 /* We do not need the labels anymore. */
1081 gimple_cond_set_true_label (entry, NULL_TREE);
1082 gimple_cond_set_false_label (entry, NULL_TREE);
1086 /* Called for each element in the hash table (P) as we delete the
1087 edge to cases hash table.
1089 Clear all the TREE_CHAINs to prevent problems with copying of
1090 SWITCH_EXPRs and structure sharing rules, then free the hash table
1091 element. */
1093 bool
1094 edge_to_cases_cleanup (edge const &, tree const &value, void *)
1096 tree t, next;
1098 for (t = value; t; t = next)
1100 next = CASE_CHAIN (t);
1101 CASE_CHAIN (t) = NULL;
1104 return true;
1107 /* Start recording information mapping edges to case labels. */
1109 void
1110 start_recording_case_labels (void)
1112 gcc_assert (edge_to_cases == NULL);
1113 edge_to_cases = new hash_map<edge, tree>;
1114 touched_switch_bbs = BITMAP_ALLOC (NULL);
1117 /* Return nonzero if we are recording information for case labels. */
1119 static bool
1120 recording_case_labels_p (void)
1122 return (edge_to_cases != NULL);
1125 /* Stop recording information mapping edges to case labels and
1126 remove any information we have recorded. */
1127 void
1128 end_recording_case_labels (void)
1130 bitmap_iterator bi;
1131 unsigned i;
1132 edge_to_cases->traverse<void *, edge_to_cases_cleanup> (NULL);
1133 delete edge_to_cases;
1134 edge_to_cases = NULL;
1135 EXECUTE_IF_SET_IN_BITMAP (touched_switch_bbs, 0, i, bi)
1137 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1138 if (bb)
1140 gimple stmt = last_stmt (bb);
1141 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1142 group_case_labels_stmt (stmt);
1145 BITMAP_FREE (touched_switch_bbs);
1148 /* If we are inside a {start,end}_recording_cases block, then return
1149 a chain of CASE_LABEL_EXPRs from T which reference E.
1151 Otherwise return NULL. */
1153 static tree
1154 get_cases_for_edge (edge e, gimple t)
1156 tree *slot;
1157 size_t i, n;
1159 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
1160 chains available. Return NULL so the caller can detect this case. */
1161 if (!recording_case_labels_p ())
1162 return NULL;
1164 slot = edge_to_cases->get (e);
1165 if (slot)
1166 return *slot;
1168 /* If we did not find E in the hash table, then this must be the first
1169 time we have been queried for information about E & T. Add all the
1170 elements from T to the hash table then perform the query again. */
1172 n = gimple_switch_num_labels (t);
1173 for (i = 0; i < n; i++)
1175 tree elt = gimple_switch_label (t, i);
1176 tree lab = CASE_LABEL (elt);
1177 basic_block label_bb = label_to_block (lab);
1178 edge this_edge = find_edge (e->src, label_bb);
1180 /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
1181 a new chain. */
1182 tree &s = edge_to_cases->get_or_insert (this_edge);
1183 CASE_CHAIN (elt) = s;
1184 s = elt;
1187 return *edge_to_cases->get (e);
1190 /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
1192 static void
1193 make_gimple_switch_edges (basic_block bb)
1195 gimple entry = last_stmt (bb);
1196 size_t i, n;
1198 n = gimple_switch_num_labels (entry);
1200 for (i = 0; i < n; ++i)
1202 tree lab = CASE_LABEL (gimple_switch_label (entry, i));
1203 basic_block label_bb = label_to_block (lab);
1204 make_edge (bb, label_bb, 0);
1209 /* Return the basic block holding label DEST. */
1211 basic_block
1212 label_to_block_fn (struct function *ifun, tree dest)
1214 int uid = LABEL_DECL_UID (dest);
1216 /* We would die hard when faced by an undefined label. Emit a label to
1217 the very first basic block. This will hopefully make even the dataflow
1218 and undefined variable warnings quite right. */
1219 if (seen_error () && uid < 0)
1221 gimple_stmt_iterator gsi =
1222 gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS));
1223 gimple stmt;
1225 stmt = gimple_build_label (dest);
1226 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1227 uid = LABEL_DECL_UID (dest);
1229 if (vec_safe_length (ifun->cfg->x_label_to_block_map) <= (unsigned int) uid)
1230 return NULL;
1231 return (*ifun->cfg->x_label_to_block_map)[uid];
1234 /* Create edges for a goto statement at block BB. Returns true
1235 if abnormal edges should be created. */
1237 static bool
1238 make_goto_expr_edges (basic_block bb)
1240 gimple_stmt_iterator last = gsi_last_bb (bb);
1241 gimple goto_t = gsi_stmt (last);
1243 /* A simple GOTO creates normal edges. */
1244 if (simple_goto_p (goto_t))
1246 tree dest = gimple_goto_dest (goto_t);
1247 basic_block label_bb = label_to_block (dest);
1248 edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
1249 e->goto_locus = gimple_location (goto_t);
1250 gsi_remove (&last, true);
1251 return false;
1254 /* A computed GOTO creates abnormal edges. */
1255 return true;
1258 /* Create edges for an asm statement with labels at block BB. */
1260 static void
1261 make_gimple_asm_edges (basic_block bb)
1263 gimple stmt = last_stmt (bb);
1264 int i, n = gimple_asm_nlabels (stmt);
1266 for (i = 0; i < n; ++i)
1268 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
1269 basic_block label_bb = label_to_block (label);
1270 make_edge (bb, label_bb, 0);
1274 /*---------------------------------------------------------------------------
1275 Flowgraph analysis
1276 ---------------------------------------------------------------------------*/
1278 /* Cleanup useless labels in basic blocks. This is something we wish
1279 to do early because it allows us to group case labels before creating
1280 the edges for the CFG, and it speeds up block statement iterators in
1281 all passes later on.
1282 We rerun this pass after CFG is created, to get rid of the labels that
1283 are no longer referenced. After then we do not run it any more, since
1284 (almost) no new labels should be created. */
1286 /* A map from basic block index to the leading label of that block. */
1287 static struct label_record
1289 /* The label. */
1290 tree label;
1292 /* True if the label is referenced from somewhere. */
1293 bool used;
1294 } *label_for_bb;
1296 /* Given LABEL return the first label in the same basic block. */
1298 static tree
1299 main_block_label (tree label)
1301 basic_block bb = label_to_block (label);
1302 tree main_label = label_for_bb[bb->index].label;
1304 /* label_to_block possibly inserted undefined label into the chain. */
1305 if (!main_label)
1307 label_for_bb[bb->index].label = label;
1308 main_label = label;
1311 label_for_bb[bb->index].used = true;
1312 return main_label;
1315 /* Clean up redundant labels within the exception tree. */
1317 static void
1318 cleanup_dead_labels_eh (void)
1320 eh_landing_pad lp;
1321 eh_region r;
1322 tree lab;
1323 int i;
1325 if (cfun->eh == NULL)
1326 return;
1328 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1329 if (lp && lp->post_landing_pad)
1331 lab = main_block_label (lp->post_landing_pad);
1332 if (lab != lp->post_landing_pad)
1334 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1335 EH_LANDING_PAD_NR (lab) = lp->index;
1339 FOR_ALL_EH_REGION (r)
1340 switch (r->type)
1342 case ERT_CLEANUP:
1343 case ERT_MUST_NOT_THROW:
1344 break;
1346 case ERT_TRY:
1348 eh_catch c;
1349 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
1351 lab = c->label;
1352 if (lab)
1353 c->label = main_block_label (lab);
1356 break;
1358 case ERT_ALLOWED_EXCEPTIONS:
1359 lab = r->u.allowed.label;
1360 if (lab)
1361 r->u.allowed.label = main_block_label (lab);
1362 break;
1367 /* Cleanup redundant labels. This is a three-step process:
1368 1) Find the leading label for each block.
1369 2) Redirect all references to labels to the leading labels.
1370 3) Cleanup all useless labels. */
1372 void
1373 cleanup_dead_labels (void)
1375 basic_block bb;
1376 label_for_bb = XCNEWVEC (struct label_record, last_basic_block_for_fn (cfun));
1378 /* Find a suitable label for each block. We use the first user-defined
1379 label if there is one, or otherwise just the first label we see. */
1380 FOR_EACH_BB_FN (bb, cfun)
1382 gimple_stmt_iterator i;
1384 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1386 tree label;
1387 gimple stmt = gsi_stmt (i);
1389 if (gimple_code (stmt) != GIMPLE_LABEL)
1390 break;
1392 label = gimple_label_label (stmt);
1394 /* If we have not yet seen a label for the current block,
1395 remember this one and see if there are more labels. */
1396 if (!label_for_bb[bb->index].label)
1398 label_for_bb[bb->index].label = label;
1399 continue;
1402 /* If we did see a label for the current block already, but it
1403 is an artificially created label, replace it if the current
1404 label is a user defined label. */
1405 if (!DECL_ARTIFICIAL (label)
1406 && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
1408 label_for_bb[bb->index].label = label;
1409 break;
1414 /* Now redirect all jumps/branches to the selected label.
1415 First do so for each block ending in a control statement. */
1416 FOR_EACH_BB_FN (bb, cfun)
1418 gimple stmt = last_stmt (bb);
1419 tree label, new_label;
1421 if (!stmt)
1422 continue;
1424 switch (gimple_code (stmt))
1426 case GIMPLE_COND:
1427 label = gimple_cond_true_label (stmt);
1428 if (label)
1430 new_label = main_block_label (label);
1431 if (new_label != label)
1432 gimple_cond_set_true_label (stmt, new_label);
1435 label = gimple_cond_false_label (stmt);
1436 if (label)
1438 new_label = main_block_label (label);
1439 if (new_label != label)
1440 gimple_cond_set_false_label (stmt, new_label);
1442 break;
1444 case GIMPLE_SWITCH:
1446 size_t i, n = gimple_switch_num_labels (stmt);
1448 /* Replace all destination labels. */
1449 for (i = 0; i < n; ++i)
1451 tree case_label = gimple_switch_label (stmt, i);
1452 label = CASE_LABEL (case_label);
1453 new_label = main_block_label (label);
1454 if (new_label != label)
1455 CASE_LABEL (case_label) = new_label;
1457 break;
1460 case GIMPLE_ASM:
1462 int i, n = gimple_asm_nlabels (stmt);
1464 for (i = 0; i < n; ++i)
1466 tree cons = gimple_asm_label_op (stmt, i);
1467 tree label = main_block_label (TREE_VALUE (cons));
1468 TREE_VALUE (cons) = label;
1470 break;
1473 /* We have to handle gotos until they're removed, and we don't
1474 remove them until after we've created the CFG edges. */
1475 case GIMPLE_GOTO:
1476 if (!computed_goto_p (stmt))
1478 label = gimple_goto_dest (stmt);
1479 new_label = main_block_label (label);
1480 if (new_label != label)
1481 gimple_goto_set_dest (stmt, new_label);
1483 break;
1485 case GIMPLE_TRANSACTION:
1487 tree label = gimple_transaction_label (stmt);
1488 if (label)
1490 tree new_label = main_block_label (label);
1491 if (new_label != label)
1492 gimple_transaction_set_label (stmt, new_label);
1495 break;
1497 default:
1498 break;
1502 /* Do the same for the exception region tree labels. */
1503 cleanup_dead_labels_eh ();
1505 /* Finally, purge dead labels. All user-defined labels and labels that
1506 can be the target of non-local gotos and labels which have their
1507 address taken are preserved. */
1508 FOR_EACH_BB_FN (bb, cfun)
1510 gimple_stmt_iterator i;
1511 tree label_for_this_bb = label_for_bb[bb->index].label;
1513 if (!label_for_this_bb)
1514 continue;
1516 /* If the main label of the block is unused, we may still remove it. */
1517 if (!label_for_bb[bb->index].used)
1518 label_for_this_bb = NULL;
1520 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1522 tree label;
1523 gimple stmt = gsi_stmt (i);
1525 if (gimple_code (stmt) != GIMPLE_LABEL)
1526 break;
1528 label = gimple_label_label (stmt);
1530 if (label == label_for_this_bb
1531 || !DECL_ARTIFICIAL (label)
1532 || DECL_NONLOCAL (label)
1533 || FORCED_LABEL (label))
1534 gsi_next (&i);
1535 else
1536 gsi_remove (&i, true);
1540 free (label_for_bb);
1543 /* Scan the sorted vector of cases in STMT (a GIMPLE_SWITCH) and combine
1544 the ones jumping to the same label.
1545 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1547 void
1548 group_case_labels_stmt (gimple stmt)
1550 int old_size = gimple_switch_num_labels (stmt);
1551 int i, j, new_size = old_size;
1552 basic_block default_bb = NULL;
1554 default_bb = label_to_block (CASE_LABEL (gimple_switch_default_label (stmt)));
1556 /* Look for possible opportunities to merge cases. */
1557 i = 1;
1558 while (i < old_size)
1560 tree base_case, base_high;
1561 basic_block base_bb;
1563 base_case = gimple_switch_label (stmt, i);
1565 gcc_assert (base_case);
1566 base_bb = label_to_block (CASE_LABEL (base_case));
1568 /* Discard cases that have the same destination as the
1569 default case. */
1570 if (base_bb == default_bb)
1572 gimple_switch_set_label (stmt, i, NULL_TREE);
1573 i++;
1574 new_size--;
1575 continue;
1578 base_high = CASE_HIGH (base_case)
1579 ? CASE_HIGH (base_case)
1580 : CASE_LOW (base_case);
1581 i++;
1583 /* Try to merge case labels. Break out when we reach the end
1584 of the label vector or when we cannot merge the next case
1585 label with the current one. */
1586 while (i < old_size)
1588 tree merge_case = gimple_switch_label (stmt, i);
1589 basic_block merge_bb = label_to_block (CASE_LABEL (merge_case));
1590 wide_int bhp1 = wi::add (base_high, 1);
1592 /* Merge the cases if they jump to the same place,
1593 and their ranges are consecutive. */
1594 if (merge_bb == base_bb
1595 && wi::eq_p (CASE_LOW (merge_case), bhp1))
1597 base_high = CASE_HIGH (merge_case) ?
1598 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1599 CASE_HIGH (base_case) = base_high;
1600 gimple_switch_set_label (stmt, i, NULL_TREE);
1601 new_size--;
1602 i++;
1604 else
1605 break;
1609 /* Compress the case labels in the label vector, and adjust the
1610 length of the vector. */
1611 for (i = 0, j = 0; i < new_size; i++)
1613 while (! gimple_switch_label (stmt, j))
1614 j++;
1615 gimple_switch_set_label (stmt, i,
1616 gimple_switch_label (stmt, j++));
1619 gcc_assert (new_size <= old_size);
1620 gimple_switch_set_num_labels (stmt, new_size);
1623 /* Look for blocks ending in a multiway branch (a GIMPLE_SWITCH),
1624 and scan the sorted vector of cases. Combine the ones jumping to the
1625 same label. */
1627 void
1628 group_case_labels (void)
1630 basic_block bb;
1632 FOR_EACH_BB_FN (bb, cfun)
1634 gimple stmt = last_stmt (bb);
1635 if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
1636 group_case_labels_stmt (stmt);
1640 /* Checks whether we can merge block B into block A. */
1642 static bool
1643 gimple_can_merge_blocks_p (basic_block a, basic_block b)
1645 gimple stmt;
1646 gimple_stmt_iterator gsi;
1648 if (!single_succ_p (a))
1649 return false;
1651 if (single_succ_edge (a)->flags & EDGE_COMPLEX)
1652 return false;
1654 if (single_succ (a) != b)
1655 return false;
1657 if (!single_pred_p (b))
1658 return false;
1660 if (b == EXIT_BLOCK_PTR_FOR_FN (cfun))
1661 return false;
1663 /* If A ends by a statement causing exceptions or something similar, we
1664 cannot merge the blocks. */
1665 stmt = last_stmt (a);
1666 if (stmt && stmt_ends_bb_p (stmt))
1667 return false;
1669 /* Do not allow a block with only a non-local label to be merged. */
1670 if (stmt
1671 && gimple_code (stmt) == GIMPLE_LABEL
1672 && DECL_NONLOCAL (gimple_label_label (stmt)))
1673 return false;
1675 /* Examine the labels at the beginning of B. */
1676 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
1678 tree lab;
1679 stmt = gsi_stmt (gsi);
1680 if (gimple_code (stmt) != GIMPLE_LABEL)
1681 break;
1682 lab = gimple_label_label (stmt);
1684 /* Do not remove user forced labels or for -O0 any user labels. */
1685 if (!DECL_ARTIFICIAL (lab) && (!optimize || FORCED_LABEL (lab)))
1686 return false;
1689 /* Protect simple loop latches. We only want to avoid merging
1690 the latch with the loop header in this case. */
1691 if (current_loops
1692 && b->loop_father->latch == b
1693 && loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES)
1694 && b->loop_father->header == a)
1695 return false;
1697 /* It must be possible to eliminate all phi nodes in B. If ssa form
1698 is not up-to-date and a name-mapping is registered, we cannot eliminate
1699 any phis. Symbols marked for renaming are never a problem though. */
1700 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi); gsi_next (&gsi))
1702 gimple phi = gsi_stmt (gsi);
1703 /* Technically only new names matter. */
1704 if (name_registered_for_update_p (PHI_RESULT (phi)))
1705 return false;
1708 /* When not optimizing, don't merge if we'd lose goto_locus. */
1709 if (!optimize
1710 && single_succ_edge (a)->goto_locus != UNKNOWN_LOCATION)
1712 location_t goto_locus = single_succ_edge (a)->goto_locus;
1713 gimple_stmt_iterator prev, next;
1714 prev = gsi_last_nondebug_bb (a);
1715 next = gsi_after_labels (b);
1716 if (!gsi_end_p (next) && is_gimple_debug (gsi_stmt (next)))
1717 gsi_next_nondebug (&next);
1718 if ((gsi_end_p (prev)
1719 || gimple_location (gsi_stmt (prev)) != goto_locus)
1720 && (gsi_end_p (next)
1721 || gimple_location (gsi_stmt (next)) != goto_locus))
1722 return false;
1725 return true;
1728 /* Replaces all uses of NAME by VAL. */
1730 void
1731 replace_uses_by (tree name, tree val)
1733 imm_use_iterator imm_iter;
1734 use_operand_p use;
1735 gimple stmt;
1736 edge e;
1738 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1740 /* Mark the block if we change the last stmt in it. */
1741 if (cfgcleanup_altered_bbs
1742 && stmt_ends_bb_p (stmt))
1743 bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1745 FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1747 replace_exp (use, val);
1749 if (gimple_code (stmt) == GIMPLE_PHI)
1751 e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
1752 if (e->flags & EDGE_ABNORMAL)
1754 /* This can only occur for virtual operands, since
1755 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1756 would prevent replacement. */
1757 gcc_checking_assert (virtual_operand_p (name));
1758 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1763 if (gimple_code (stmt) != GIMPLE_PHI)
1765 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1766 gimple orig_stmt = stmt;
1767 size_t i;
1769 /* FIXME. It shouldn't be required to keep TREE_CONSTANT
1770 on ADDR_EXPRs up-to-date on GIMPLE. Propagation will
1771 only change sth from non-invariant to invariant, and only
1772 when propagating constants. */
1773 if (is_gimple_min_invariant (val))
1774 for (i = 0; i < gimple_num_ops (stmt); i++)
1776 tree op = gimple_op (stmt, i);
1777 /* Operands may be empty here. For example, the labels
1778 of a GIMPLE_COND are nulled out following the creation
1779 of the corresponding CFG edges. */
1780 if (op && TREE_CODE (op) == ADDR_EXPR)
1781 recompute_tree_invariant_for_addr_expr (op);
1784 if (fold_stmt (&gsi))
1785 stmt = gsi_stmt (gsi);
1787 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1788 gimple_purge_dead_eh_edges (gimple_bb (stmt));
1790 update_stmt (stmt);
1794 gcc_checking_assert (has_zero_uses (name));
1796 /* Also update the trees stored in loop structures. */
1797 if (current_loops)
1799 struct loop *loop;
1801 FOR_EACH_LOOP (loop, 0)
1803 substitute_in_loop_info (loop, name, val);
1808 /* Merge block B into block A. */
1810 static void
1811 gimple_merge_blocks (basic_block a, basic_block b)
1813 gimple_stmt_iterator last, gsi, psi;
1815 if (dump_file)
1816 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1818 /* Remove all single-valued PHI nodes from block B of the form
1819 V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
1820 gsi = gsi_last_bb (a);
1821 for (psi = gsi_start_phis (b); !gsi_end_p (psi); )
1823 gimple phi = gsi_stmt (psi);
1824 tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
1825 gimple copy;
1826 bool may_replace_uses = (virtual_operand_p (def)
1827 || may_propagate_copy (def, use));
1829 /* In case we maintain loop closed ssa form, do not propagate arguments
1830 of loop exit phi nodes. */
1831 if (current_loops
1832 && loops_state_satisfies_p (LOOP_CLOSED_SSA)
1833 && !virtual_operand_p (def)
1834 && TREE_CODE (use) == SSA_NAME
1835 && a->loop_father != b->loop_father)
1836 may_replace_uses = false;
1838 if (!may_replace_uses)
1840 gcc_assert (!virtual_operand_p (def));
1842 /* Note that just emitting the copies is fine -- there is no problem
1843 with ordering of phi nodes. This is because A is the single
1844 predecessor of B, therefore results of the phi nodes cannot
1845 appear as arguments of the phi nodes. */
1846 copy = gimple_build_assign (def, use);
1847 gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
1848 remove_phi_node (&psi, false);
1850 else
1852 /* If we deal with a PHI for virtual operands, we can simply
1853 propagate these without fussing with folding or updating
1854 the stmt. */
1855 if (virtual_operand_p (def))
1857 imm_use_iterator iter;
1858 use_operand_p use_p;
1859 gimple stmt;
1861 FOR_EACH_IMM_USE_STMT (stmt, iter, def)
1862 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1863 SET_USE (use_p, use);
1865 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1866 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use) = 1;
1868 else
1869 replace_uses_by (def, use);
1871 remove_phi_node (&psi, true);
1875 /* Ensure that B follows A. */
1876 move_block_after (b, a);
1878 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1879 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1881 /* Remove labels from B and set gimple_bb to A for other statements. */
1882 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
1884 gimple stmt = gsi_stmt (gsi);
1885 if (gimple_code (stmt) == GIMPLE_LABEL)
1887 tree label = gimple_label_label (stmt);
1888 int lp_nr;
1890 gsi_remove (&gsi, false);
1892 /* Now that we can thread computed gotos, we might have
1893 a situation where we have a forced label in block B
1894 However, the label at the start of block B might still be
1895 used in other ways (think about the runtime checking for
1896 Fortran assigned gotos). So we can not just delete the
1897 label. Instead we move the label to the start of block A. */
1898 if (FORCED_LABEL (label))
1900 gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
1901 gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
1903 /* Other user labels keep around in a form of a debug stmt. */
1904 else if (!DECL_ARTIFICIAL (label) && MAY_HAVE_DEBUG_STMTS)
1906 gimple dbg = gimple_build_debug_bind (label,
1907 integer_zero_node,
1908 stmt);
1909 gimple_debug_bind_reset_value (dbg);
1910 gsi_insert_before (&gsi, dbg, GSI_SAME_STMT);
1913 lp_nr = EH_LANDING_PAD_NR (label);
1914 if (lp_nr)
1916 eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
1917 lp->post_landing_pad = NULL;
1920 else
1922 gimple_set_bb (stmt, a);
1923 gsi_next (&gsi);
1927 /* When merging two BBs, if their counts are different, the larger count
1928 is selected as the new bb count. This is to handle inconsistent
1929 profiles. */
1930 if (a->loop_father == b->loop_father)
1932 a->count = MAX (a->count, b->count);
1933 a->frequency = MAX (a->frequency, b->frequency);
1936 /* Merge the sequences. */
1937 last = gsi_last_bb (a);
1938 gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
1939 set_bb_seq (b, NULL);
1941 if (cfgcleanup_altered_bbs)
1942 bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
1946 /* Return the one of two successors of BB that is not reachable by a
1947 complex edge, if there is one. Else, return BB. We use
1948 this in optimizations that use post-dominators for their heuristics,
1949 to catch the cases in C++ where function calls are involved. */
1951 basic_block
1952 single_noncomplex_succ (basic_block bb)
1954 edge e0, e1;
1955 if (EDGE_COUNT (bb->succs) != 2)
1956 return bb;
1958 e0 = EDGE_SUCC (bb, 0);
1959 e1 = EDGE_SUCC (bb, 1);
1960 if (e0->flags & EDGE_COMPLEX)
1961 return e1->dest;
1962 if (e1->flags & EDGE_COMPLEX)
1963 return e0->dest;
1965 return bb;
1968 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1970 void
1971 notice_special_calls (gimple call)
1973 int flags = gimple_call_flags (call);
1975 if (flags & ECF_MAY_BE_ALLOCA)
1976 cfun->calls_alloca = true;
1977 if (flags & ECF_RETURNS_TWICE)
1978 cfun->calls_setjmp = true;
1982 /* Clear flags set by notice_special_calls. Used by dead code removal
1983 to update the flags. */
1985 void
1986 clear_special_calls (void)
1988 cfun->calls_alloca = false;
1989 cfun->calls_setjmp = false;
1992 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1994 static void
1995 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
1997 /* Since this block is no longer reachable, we can just delete all
1998 of its PHI nodes. */
1999 remove_phi_nodes (bb);
2001 /* Remove edges to BB's successors. */
2002 while (EDGE_COUNT (bb->succs) > 0)
2003 remove_edge (EDGE_SUCC (bb, 0));
2007 /* Remove statements of basic block BB. */
2009 static void
2010 remove_bb (basic_block bb)
2012 gimple_stmt_iterator i;
2014 if (dump_file)
2016 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2017 if (dump_flags & TDF_DETAILS)
2019 dump_bb (dump_file, bb, 0, TDF_BLOCKS);
2020 fprintf (dump_file, "\n");
2024 if (current_loops)
2026 struct loop *loop = bb->loop_father;
2028 /* If a loop gets removed, clean up the information associated
2029 with it. */
2030 if (loop->latch == bb
2031 || loop->header == bb)
2032 free_numbers_of_iterations_estimates_loop (loop);
2035 /* Remove all the instructions in the block. */
2036 if (bb_seq (bb) != NULL)
2038 /* Walk backwards so as to get a chance to substitute all
2039 released DEFs into debug stmts. See
2040 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
2041 details. */
2042 for (i = gsi_last_bb (bb); !gsi_end_p (i);)
2044 gimple stmt = gsi_stmt (i);
2045 if (gimple_code (stmt) == GIMPLE_LABEL
2046 && (FORCED_LABEL (gimple_label_label (stmt))
2047 || DECL_NONLOCAL (gimple_label_label (stmt))))
2049 basic_block new_bb;
2050 gimple_stmt_iterator new_gsi;
2052 /* A non-reachable non-local label may still be referenced.
2053 But it no longer needs to carry the extra semantics of
2054 non-locality. */
2055 if (DECL_NONLOCAL (gimple_label_label (stmt)))
2057 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
2058 FORCED_LABEL (gimple_label_label (stmt)) = 1;
2061 new_bb = bb->prev_bb;
2062 new_gsi = gsi_start_bb (new_bb);
2063 gsi_remove (&i, false);
2064 gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
2066 else
2068 /* Release SSA definitions if we are in SSA. Note that we
2069 may be called when not in SSA. For example,
2070 final_cleanup calls this function via
2071 cleanup_tree_cfg. */
2072 if (gimple_in_ssa_p (cfun))
2073 release_defs (stmt);
2075 gsi_remove (&i, true);
2078 if (gsi_end_p (i))
2079 i = gsi_last_bb (bb);
2080 else
2081 gsi_prev (&i);
2085 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2086 bb->il.gimple.seq = NULL;
2087 bb->il.gimple.phi_nodes = NULL;
2091 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2092 predicate VAL, return the edge that will be taken out of the block.
2093 If VAL does not match a unique edge, NULL is returned. */
2095 edge
2096 find_taken_edge (basic_block bb, tree val)
2098 gimple stmt;
2100 stmt = last_stmt (bb);
2102 gcc_assert (stmt);
2103 gcc_assert (is_ctrl_stmt (stmt));
2105 if (val == NULL)
2106 return NULL;
2108 if (!is_gimple_min_invariant (val))
2109 return NULL;
2111 if (gimple_code (stmt) == GIMPLE_COND)
2112 return find_taken_edge_cond_expr (bb, val);
2114 if (gimple_code (stmt) == GIMPLE_SWITCH)
2115 return find_taken_edge_switch_expr (bb, val);
2117 if (computed_goto_p (stmt))
2119 /* Only optimize if the argument is a label, if the argument is
2120 not a label then we can not construct a proper CFG.
2122 It may be the case that we only need to allow the LABEL_REF to
2123 appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
2124 appear inside a LABEL_EXPR just to be safe. */
2125 if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
2126 && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
2127 return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
2128 return NULL;
2131 gcc_unreachable ();
2134 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2135 statement, determine which of the outgoing edges will be taken out of the
2136 block. Return NULL if either edge may be taken. */
2138 static edge
2139 find_taken_edge_computed_goto (basic_block bb, tree val)
2141 basic_block dest;
2142 edge e = NULL;
2144 dest = label_to_block (val);
2145 if (dest)
2147 e = find_edge (bb, dest);
2148 gcc_assert (e != NULL);
2151 return e;
2154 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2155 statement, determine which of the two edges will be taken out of the
2156 block. Return NULL if either edge may be taken. */
2158 static edge
2159 find_taken_edge_cond_expr (basic_block bb, tree val)
2161 edge true_edge, false_edge;
2163 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2165 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2166 return (integer_zerop (val) ? false_edge : true_edge);
2169 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2170 statement, determine which edge will be taken out of the block. Return
2171 NULL if any edge may be taken. */
2173 static edge
2174 find_taken_edge_switch_expr (basic_block bb, tree val)
2176 basic_block dest_bb;
2177 edge e;
2178 gimple switch_stmt;
2179 tree taken_case;
2181 switch_stmt = last_stmt (bb);
2182 taken_case = find_case_label_for_value (switch_stmt, val);
2183 dest_bb = label_to_block (CASE_LABEL (taken_case));
2185 e = find_edge (bb, dest_bb);
2186 gcc_assert (e);
2187 return e;
2191 /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
2192 We can make optimal use here of the fact that the case labels are
2193 sorted: We can do a binary search for a case matching VAL. */
2195 static tree
2196 find_case_label_for_value (gimple switch_stmt, tree val)
2198 size_t low, high, n = gimple_switch_num_labels (switch_stmt);
2199 tree default_case = gimple_switch_default_label (switch_stmt);
2201 for (low = 0, high = n; high - low > 1; )
2203 size_t i = (high + low) / 2;
2204 tree t = gimple_switch_label (switch_stmt, i);
2205 int cmp;
2207 /* Cache the result of comparing CASE_LOW and val. */
2208 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2210 if (cmp > 0)
2211 high = i;
2212 else
2213 low = i;
2215 if (CASE_HIGH (t) == NULL)
2217 /* A singe-valued case label. */
2218 if (cmp == 0)
2219 return t;
2221 else
2223 /* A case range. We can only handle integer ranges. */
2224 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2225 return t;
2229 return default_case;
2233 /* Dump a basic block on stderr. */
2235 void
2236 gimple_debug_bb (basic_block bb)
2238 dump_bb (stderr, bb, 0, TDF_VOPS|TDF_MEMSYMS|TDF_BLOCKS);
2242 /* Dump basic block with index N on stderr. */
2244 basic_block
2245 gimple_debug_bb_n (int n)
2247 gimple_debug_bb (BASIC_BLOCK_FOR_FN (cfun, n));
2248 return BASIC_BLOCK_FOR_FN (cfun, n);
2252 /* Dump the CFG on stderr.
2254 FLAGS are the same used by the tree dumping functions
2255 (see TDF_* in dumpfile.h). */
2257 void
2258 gimple_debug_cfg (int flags)
2260 gimple_dump_cfg (stderr, flags);
2264 /* Dump the program showing basic block boundaries on the given FILE.
2266 FLAGS are the same used by the tree dumping functions (see TDF_* in
2267 tree.h). */
2269 void
2270 gimple_dump_cfg (FILE *file, int flags)
2272 if (flags & TDF_DETAILS)
2274 dump_function_header (file, current_function_decl, flags);
2275 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2276 n_basic_blocks_for_fn (cfun), n_edges_for_fn (cfun),
2277 last_basic_block_for_fn (cfun));
2279 brief_dump_cfg (file, flags | TDF_COMMENT);
2280 fprintf (file, "\n");
2283 if (flags & TDF_STATS)
2284 dump_cfg_stats (file);
2286 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2290 /* Dump CFG statistics on FILE. */
2292 void
2293 dump_cfg_stats (FILE *file)
2295 static long max_num_merged_labels = 0;
2296 unsigned long size, total = 0;
2297 long num_edges;
2298 basic_block bb;
2299 const char * const fmt_str = "%-30s%-13s%12s\n";
2300 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2301 const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
2302 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2303 const char *funcname = current_function_name ();
2305 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2307 fprintf (file, "---------------------------------------------------------\n");
2308 fprintf (file, fmt_str, "", " Number of ", "Memory");
2309 fprintf (file, fmt_str, "", " instances ", "used ");
2310 fprintf (file, "---------------------------------------------------------\n");
2312 size = n_basic_blocks_for_fn (cfun) * sizeof (struct basic_block_def);
2313 total += size;
2314 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks_for_fn (cfun),
2315 SCALE (size), LABEL (size));
2317 num_edges = 0;
2318 FOR_EACH_BB_FN (bb, cfun)
2319 num_edges += EDGE_COUNT (bb->succs);
2320 size = num_edges * sizeof (struct edge_def);
2321 total += size;
2322 fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
2324 fprintf (file, "---------------------------------------------------------\n");
2325 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2326 LABEL (total));
2327 fprintf (file, "---------------------------------------------------------\n");
2328 fprintf (file, "\n");
2330 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2331 max_num_merged_labels = cfg_stats.num_merged_labels;
2333 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2334 cfg_stats.num_merged_labels, max_num_merged_labels);
2336 fprintf (file, "\n");
2340 /* Dump CFG statistics on stderr. Keep extern so that it's always
2341 linked in the final executable. */
2343 DEBUG_FUNCTION void
2344 debug_cfg_stats (void)
2346 dump_cfg_stats (stderr);
2349 /*---------------------------------------------------------------------------
2350 Miscellaneous helpers
2351 ---------------------------------------------------------------------------*/
2353 /* Return true if T, a GIMPLE_CALL, can make an abnormal transfer of control
2354 flow. Transfers of control flow associated with EH are excluded. */
2356 static bool
2357 call_can_make_abnormal_goto (gimple t)
2359 /* If the function has no non-local labels, then a call cannot make an
2360 abnormal transfer of control. */
2361 if (!cfun->has_nonlocal_label
2362 && !cfun->calls_setjmp)
2363 return false;
2365 /* Likewise if the call has no side effects. */
2366 if (!gimple_has_side_effects (t))
2367 return false;
2369 /* Likewise if the called function is leaf. */
2370 if (gimple_call_flags (t) & ECF_LEAF)
2371 return false;
2373 return true;
2377 /* Return true if T can make an abnormal transfer of control flow.
2378 Transfers of control flow associated with EH are excluded. */
2380 bool
2381 stmt_can_make_abnormal_goto (gimple t)
2383 if (computed_goto_p (t))
2384 return true;
2385 if (is_gimple_call (t))
2386 return call_can_make_abnormal_goto (t);
2387 return false;
2391 /* Return true if T represents a stmt that always transfers control. */
2393 bool
2394 is_ctrl_stmt (gimple t)
2396 switch (gimple_code (t))
2398 case GIMPLE_COND:
2399 case GIMPLE_SWITCH:
2400 case GIMPLE_GOTO:
2401 case GIMPLE_RETURN:
2402 case GIMPLE_RESX:
2403 return true;
2404 default:
2405 return false;
2410 /* Return true if T is a statement that may alter the flow of control
2411 (e.g., a call to a non-returning function). */
2413 bool
2414 is_ctrl_altering_stmt (gimple t)
2416 gcc_assert (t);
2418 switch (gimple_code (t))
2420 case GIMPLE_CALL:
2421 /* Per stmt call flag indicates whether the call could alter
2422 controlflow. */
2423 if (gimple_call_ctrl_altering_p (t))
2424 return true;
2425 break;
2427 case GIMPLE_EH_DISPATCH:
2428 /* EH_DISPATCH branches to the individual catch handlers at
2429 this level of a try or allowed-exceptions region. It can
2430 fallthru to the next statement as well. */
2431 return true;
2433 case GIMPLE_ASM:
2434 if (gimple_asm_nlabels (t) > 0)
2435 return true;
2436 break;
2438 CASE_GIMPLE_OMP:
2439 /* OpenMP directives alter control flow. */
2440 return true;
2442 case GIMPLE_TRANSACTION:
2443 /* A transaction start alters control flow. */
2444 return true;
2446 default:
2447 break;
2450 /* If a statement can throw, it alters control flow. */
2451 return stmt_can_throw_internal (t);
2455 /* Return true if T is a simple local goto. */
2457 bool
2458 simple_goto_p (gimple t)
2460 return (gimple_code (t) == GIMPLE_GOTO
2461 && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2465 /* Return true if STMT should start a new basic block. PREV_STMT is
2466 the statement preceding STMT. It is used when STMT is a label or a
2467 case label. Labels should only start a new basic block if their
2468 previous statement wasn't a label. Otherwise, sequence of labels
2469 would generate unnecessary basic blocks that only contain a single
2470 label. */
2472 static inline bool
2473 stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
2475 if (stmt == NULL)
2476 return false;
2478 /* Labels start a new basic block only if the preceding statement
2479 wasn't a label of the same type. This prevents the creation of
2480 consecutive blocks that have nothing but a single label. */
2481 if (gimple_code (stmt) == GIMPLE_LABEL)
2483 /* Nonlocal and computed GOTO targets always start a new block. */
2484 if (DECL_NONLOCAL (gimple_label_label (stmt))
2485 || FORCED_LABEL (gimple_label_label (stmt)))
2486 return true;
2488 if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
2490 if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
2491 return true;
2493 cfg_stats.num_merged_labels++;
2494 return false;
2496 else
2497 return true;
2499 else if (gimple_code (stmt) == GIMPLE_CALL
2500 && gimple_call_flags (stmt) & ECF_RETURNS_TWICE)
2501 /* setjmp acts similar to a nonlocal GOTO target and thus should
2502 start a new block. */
2503 return true;
2505 return false;
2509 /* Return true if T should end a basic block. */
2511 bool
2512 stmt_ends_bb_p (gimple t)
2514 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2517 /* Remove block annotations and other data structures. */
2519 void
2520 delete_tree_cfg_annotations (void)
2522 vec_free (label_to_block_map_for_fn (cfun));
2526 /* Return the first statement in basic block BB. */
2528 gimple
2529 first_stmt (basic_block bb)
2531 gimple_stmt_iterator i = gsi_start_bb (bb);
2532 gimple stmt = NULL;
2534 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2536 gsi_next (&i);
2537 stmt = NULL;
2539 return stmt;
2542 /* Return the first non-label statement in basic block BB. */
2544 static gimple
2545 first_non_label_stmt (basic_block bb)
2547 gimple_stmt_iterator i = gsi_start_bb (bb);
2548 while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
2549 gsi_next (&i);
2550 return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
2553 /* Return the last statement in basic block BB. */
2555 gimple
2556 last_stmt (basic_block bb)
2558 gimple_stmt_iterator i = gsi_last_bb (bb);
2559 gimple stmt = NULL;
2561 while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2563 gsi_prev (&i);
2564 stmt = NULL;
2566 return stmt;
2569 /* Return the last statement of an otherwise empty block. Return NULL
2570 if the block is totally empty, or if it contains more than one
2571 statement. */
2573 gimple
2574 last_and_only_stmt (basic_block bb)
2576 gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
2577 gimple last, prev;
2579 if (gsi_end_p (i))
2580 return NULL;
2582 last = gsi_stmt (i);
2583 gsi_prev_nondebug (&i);
2584 if (gsi_end_p (i))
2585 return last;
2587 /* Empty statements should no longer appear in the instruction stream.
2588 Everything that might have appeared before should be deleted by
2589 remove_useless_stmts, and the optimizers should just gsi_remove
2590 instead of smashing with build_empty_stmt.
2592 Thus the only thing that should appear here in a block containing
2593 one executable statement is a label. */
2594 prev = gsi_stmt (i);
2595 if (gimple_code (prev) == GIMPLE_LABEL)
2596 return last;
2597 else
2598 return NULL;
2601 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
2603 static void
2604 reinstall_phi_args (edge new_edge, edge old_edge)
2606 edge_var_map *vm;
2607 int i;
2608 gimple_stmt_iterator phis;
2610 vec<edge_var_map> *v = redirect_edge_var_map_vector (old_edge);
2611 if (!v)
2612 return;
2614 for (i = 0, phis = gsi_start_phis (new_edge->dest);
2615 v->iterate (i, &vm) && !gsi_end_p (phis);
2616 i++, gsi_next (&phis))
2618 gimple phi = gsi_stmt (phis);
2619 tree result = redirect_edge_var_map_result (vm);
2620 tree arg = redirect_edge_var_map_def (vm);
2622 gcc_assert (result == gimple_phi_result (phi));
2624 add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
2627 redirect_edge_var_map_clear (old_edge);
2630 /* Returns the basic block after which the new basic block created
2631 by splitting edge EDGE_IN should be placed. Tries to keep the new block
2632 near its "logical" location. This is of most help to humans looking
2633 at debugging dumps. */
2635 static basic_block
2636 split_edge_bb_loc (edge edge_in)
2638 basic_block dest = edge_in->dest;
2639 basic_block dest_prev = dest->prev_bb;
2641 if (dest_prev)
2643 edge e = find_edge (dest_prev, dest);
2644 if (e && !(e->flags & EDGE_COMPLEX))
2645 return edge_in->src;
2647 return dest_prev;
2650 /* Split a (typically critical) edge EDGE_IN. Return the new block.
2651 Abort on abnormal edges. */
2653 static basic_block
2654 gimple_split_edge (edge edge_in)
2656 basic_block new_bb, after_bb, dest;
2657 edge new_edge, e;
2659 /* Abnormal edges cannot be split. */
2660 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2662 dest = edge_in->dest;
2664 after_bb = split_edge_bb_loc (edge_in);
2666 new_bb = create_empty_bb (after_bb);
2667 new_bb->frequency = EDGE_FREQUENCY (edge_in);
2668 new_bb->count = edge_in->count;
2669 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
2670 new_edge->probability = REG_BR_PROB_BASE;
2671 new_edge->count = edge_in->count;
2673 e = redirect_edge_and_branch (edge_in, new_bb);
2674 gcc_assert (e == edge_in);
2675 reinstall_phi_args (new_edge, e);
2677 return new_bb;
2681 /* Verify properties of the address expression T with base object BASE. */
2683 static tree
2684 verify_address (tree t, tree base)
2686 bool old_constant;
2687 bool old_side_effects;
2688 bool new_constant;
2689 bool new_side_effects;
2691 old_constant = TREE_CONSTANT (t);
2692 old_side_effects = TREE_SIDE_EFFECTS (t);
2694 recompute_tree_invariant_for_addr_expr (t);
2695 new_side_effects = TREE_SIDE_EFFECTS (t);
2696 new_constant = TREE_CONSTANT (t);
2698 if (old_constant != new_constant)
2700 error ("constant not recomputed when ADDR_EXPR changed");
2701 return t;
2703 if (old_side_effects != new_side_effects)
2705 error ("side effects not recomputed when ADDR_EXPR changed");
2706 return t;
2709 if (!(TREE_CODE (base) == VAR_DECL
2710 || TREE_CODE (base) == PARM_DECL
2711 || TREE_CODE (base) == RESULT_DECL))
2712 return NULL_TREE;
2714 if (DECL_GIMPLE_REG_P (base))
2716 error ("DECL_GIMPLE_REG_P set on a variable with address taken");
2717 return base;
2720 return NULL_TREE;
2723 /* Callback for walk_tree, check that all elements with address taken are
2724 properly noticed as such. The DATA is an int* that is 1 if TP was seen
2725 inside a PHI node. */
2727 static tree
2728 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2730 tree t = *tp, x;
2732 if (TYPE_P (t))
2733 *walk_subtrees = 0;
2735 /* Check operand N for being valid GIMPLE and give error MSG if not. */
2736 #define CHECK_OP(N, MSG) \
2737 do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
2738 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
2740 switch (TREE_CODE (t))
2742 case SSA_NAME:
2743 if (SSA_NAME_IN_FREE_LIST (t))
2745 error ("SSA name in freelist but still referenced");
2746 return *tp;
2748 break;
2750 case INDIRECT_REF:
2751 error ("INDIRECT_REF in gimple IL");
2752 return t;
2754 case MEM_REF:
2755 x = TREE_OPERAND (t, 0);
2756 if (!POINTER_TYPE_P (TREE_TYPE (x))
2757 || !is_gimple_mem_ref_addr (x))
2759 error ("invalid first operand of MEM_REF");
2760 return x;
2762 if (TREE_CODE (TREE_OPERAND (t, 1)) != INTEGER_CST
2763 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 1))))
2765 error ("invalid offset operand of MEM_REF");
2766 return TREE_OPERAND (t, 1);
2768 if (TREE_CODE (x) == ADDR_EXPR
2769 && (x = verify_address (x, TREE_OPERAND (x, 0))))
2770 return x;
2771 *walk_subtrees = 0;
2772 break;
2774 case ASSERT_EXPR:
2775 x = fold (ASSERT_EXPR_COND (t));
2776 if (x == boolean_false_node)
2778 error ("ASSERT_EXPR with an always-false condition");
2779 return *tp;
2781 break;
2783 case MODIFY_EXPR:
2784 error ("MODIFY_EXPR not expected while having tuples");
2785 return *tp;
2787 case ADDR_EXPR:
2789 tree tem;
2791 gcc_assert (is_gimple_address (t));
2793 /* Skip any references (they will be checked when we recurse down the
2794 tree) and ensure that any variable used as a prefix is marked
2795 addressable. */
2796 for (x = TREE_OPERAND (t, 0);
2797 handled_component_p (x);
2798 x = TREE_OPERAND (x, 0))
2801 if ((tem = verify_address (t, x)))
2802 return tem;
2804 if (!(TREE_CODE (x) == VAR_DECL
2805 || TREE_CODE (x) == PARM_DECL
2806 || TREE_CODE (x) == RESULT_DECL))
2807 return NULL;
2809 if (!TREE_ADDRESSABLE (x))
2811 error ("address taken, but ADDRESSABLE bit not set");
2812 return x;
2815 break;
2818 case COND_EXPR:
2819 x = COND_EXPR_COND (t);
2820 if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
2822 error ("non-integral used in condition");
2823 return x;
2825 if (!is_gimple_condexpr (x))
2827 error ("invalid conditional operand");
2828 return x;
2830 break;
2832 case NON_LVALUE_EXPR:
2833 case TRUTH_NOT_EXPR:
2834 gcc_unreachable ();
2836 CASE_CONVERT:
2837 case FIX_TRUNC_EXPR:
2838 case FLOAT_EXPR:
2839 case NEGATE_EXPR:
2840 case ABS_EXPR:
2841 case BIT_NOT_EXPR:
2842 CHECK_OP (0, "invalid operand to unary operator");
2843 break;
2845 case REALPART_EXPR:
2846 case IMAGPART_EXPR:
2847 case BIT_FIELD_REF:
2848 if (!is_gimple_reg_type (TREE_TYPE (t)))
2850 error ("non-scalar BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR");
2851 return t;
2854 if (TREE_CODE (t) == BIT_FIELD_REF)
2856 tree t0 = TREE_OPERAND (t, 0);
2857 tree t1 = TREE_OPERAND (t, 1);
2858 tree t2 = TREE_OPERAND (t, 2);
2859 if (!tree_fits_uhwi_p (t1)
2860 || !tree_fits_uhwi_p (t2))
2862 error ("invalid position or size operand to BIT_FIELD_REF");
2863 return t;
2865 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
2866 && (TYPE_PRECISION (TREE_TYPE (t))
2867 != tree_to_uhwi (t1)))
2869 error ("integral result type precision does not match "
2870 "field size of BIT_FIELD_REF");
2871 return t;
2873 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
2874 && TYPE_MODE (TREE_TYPE (t)) != BLKmode
2875 && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
2876 != tree_to_uhwi (t1)))
2878 error ("mode precision of non-integral result does not "
2879 "match field size of BIT_FIELD_REF");
2880 return t;
2882 if (!AGGREGATE_TYPE_P (TREE_TYPE (t0))
2883 && (tree_to_uhwi (t1) + tree_to_uhwi (t2)
2884 > tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t0)))))
2886 error ("position plus size exceeds size of referenced object in "
2887 "BIT_FIELD_REF");
2888 return t;
2891 t = TREE_OPERAND (t, 0);
2893 /* Fall-through. */
2894 case COMPONENT_REF:
2895 case ARRAY_REF:
2896 case ARRAY_RANGE_REF:
2897 case VIEW_CONVERT_EXPR:
2898 /* We have a nest of references. Verify that each of the operands
2899 that determine where to reference is either a constant or a variable,
2900 verify that the base is valid, and then show we've already checked
2901 the subtrees. */
2902 while (handled_component_p (t))
2904 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
2905 CHECK_OP (2, "invalid COMPONENT_REF offset operator");
2906 else if (TREE_CODE (t) == ARRAY_REF
2907 || TREE_CODE (t) == ARRAY_RANGE_REF)
2909 CHECK_OP (1, "invalid array index");
2910 if (TREE_OPERAND (t, 2))
2911 CHECK_OP (2, "invalid array lower bound");
2912 if (TREE_OPERAND (t, 3))
2913 CHECK_OP (3, "invalid array stride");
2915 else if (TREE_CODE (t) == BIT_FIELD_REF
2916 || TREE_CODE (t) == REALPART_EXPR
2917 || TREE_CODE (t) == IMAGPART_EXPR)
2919 error ("non-top-level BIT_FIELD_REF, IMAGPART_EXPR or "
2920 "REALPART_EXPR");
2921 return t;
2924 t = TREE_OPERAND (t, 0);
2927 if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
2929 error ("invalid reference prefix");
2930 return t;
2932 *walk_subtrees = 0;
2933 break;
2934 case PLUS_EXPR:
2935 case MINUS_EXPR:
2936 /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
2937 POINTER_PLUS_EXPR. */
2938 if (POINTER_TYPE_P (TREE_TYPE (t)))
2940 error ("invalid operand to plus/minus, type is a pointer");
2941 return t;
2943 CHECK_OP (0, "invalid operand to binary operator");
2944 CHECK_OP (1, "invalid operand to binary operator");
2945 break;
2947 case POINTER_PLUS_EXPR:
2948 /* Check to make sure the first operand is a pointer or reference type. */
2949 if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2951 error ("invalid operand to pointer plus, first operand is not a pointer");
2952 return t;
2954 /* Check to make sure the second operand is a ptrofftype. */
2955 if (!ptrofftype_p (TREE_TYPE (TREE_OPERAND (t, 1))))
2957 error ("invalid operand to pointer plus, second operand is not an "
2958 "integer type of appropriate width");
2959 return t;
2961 /* FALLTHROUGH */
2962 case LT_EXPR:
2963 case LE_EXPR:
2964 case GT_EXPR:
2965 case GE_EXPR:
2966 case EQ_EXPR:
2967 case NE_EXPR:
2968 case UNORDERED_EXPR:
2969 case ORDERED_EXPR:
2970 case UNLT_EXPR:
2971 case UNLE_EXPR:
2972 case UNGT_EXPR:
2973 case UNGE_EXPR:
2974 case UNEQ_EXPR:
2975 case LTGT_EXPR:
2976 case MULT_EXPR:
2977 case TRUNC_DIV_EXPR:
2978 case CEIL_DIV_EXPR:
2979 case FLOOR_DIV_EXPR:
2980 case ROUND_DIV_EXPR:
2981 case TRUNC_MOD_EXPR:
2982 case CEIL_MOD_EXPR:
2983 case FLOOR_MOD_EXPR:
2984 case ROUND_MOD_EXPR:
2985 case RDIV_EXPR:
2986 case EXACT_DIV_EXPR:
2987 case MIN_EXPR:
2988 case MAX_EXPR:
2989 case LSHIFT_EXPR:
2990 case RSHIFT_EXPR:
2991 case LROTATE_EXPR:
2992 case RROTATE_EXPR:
2993 case BIT_IOR_EXPR:
2994 case BIT_XOR_EXPR:
2995 case BIT_AND_EXPR:
2996 CHECK_OP (0, "invalid operand to binary operator");
2997 CHECK_OP (1, "invalid operand to binary operator");
2998 break;
3000 case CONSTRUCTOR:
3001 if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3002 *walk_subtrees = 0;
3003 break;
3005 case CASE_LABEL_EXPR:
3006 if (CASE_CHAIN (t))
3008 error ("invalid CASE_CHAIN");
3009 return t;
3011 break;
3013 default:
3014 break;
3016 return NULL;
3018 #undef CHECK_OP
3022 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
3023 Returns true if there is an error, otherwise false. */
3025 static bool
3026 verify_types_in_gimple_min_lval (tree expr)
3028 tree op;
3030 if (is_gimple_id (expr))
3031 return false;
3033 if (TREE_CODE (expr) != TARGET_MEM_REF
3034 && TREE_CODE (expr) != MEM_REF)
3036 error ("invalid expression for min lvalue");
3037 return true;
3040 /* TARGET_MEM_REFs are strange beasts. */
3041 if (TREE_CODE (expr) == TARGET_MEM_REF)
3042 return false;
3044 op = TREE_OPERAND (expr, 0);
3045 if (!is_gimple_val (op))
3047 error ("invalid operand in indirect reference");
3048 debug_generic_stmt (op);
3049 return true;
3051 /* Memory references now generally can involve a value conversion. */
3053 return false;
3056 /* Verify if EXPR is a valid GIMPLE reference expression. If
3057 REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
3058 if there is an error, otherwise false. */
3060 static bool
3061 verify_types_in_gimple_reference (tree expr, bool require_lvalue)
3063 while (handled_component_p (expr))
3065 tree op = TREE_OPERAND (expr, 0);
3067 if (TREE_CODE (expr) == ARRAY_REF
3068 || TREE_CODE (expr) == ARRAY_RANGE_REF)
3070 if (!is_gimple_val (TREE_OPERAND (expr, 1))
3071 || (TREE_OPERAND (expr, 2)
3072 && !is_gimple_val (TREE_OPERAND (expr, 2)))
3073 || (TREE_OPERAND (expr, 3)
3074 && !is_gimple_val (TREE_OPERAND (expr, 3))))
3076 error ("invalid operands to array reference");
3077 debug_generic_stmt (expr);
3078 return true;
3082 /* Verify if the reference array element types are compatible. */
3083 if (TREE_CODE (expr) == ARRAY_REF
3084 && !useless_type_conversion_p (TREE_TYPE (expr),
3085 TREE_TYPE (TREE_TYPE (op))))
3087 error ("type mismatch in array reference");
3088 debug_generic_stmt (TREE_TYPE (expr));
3089 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3090 return true;
3092 if (TREE_CODE (expr) == ARRAY_RANGE_REF
3093 && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
3094 TREE_TYPE (TREE_TYPE (op))))
3096 error ("type mismatch in array range reference");
3097 debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
3098 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3099 return true;
3102 if ((TREE_CODE (expr) == REALPART_EXPR
3103 || TREE_CODE (expr) == IMAGPART_EXPR)
3104 && !useless_type_conversion_p (TREE_TYPE (expr),
3105 TREE_TYPE (TREE_TYPE (op))))
3107 error ("type mismatch in real/imagpart reference");
3108 debug_generic_stmt (TREE_TYPE (expr));
3109 debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3110 return true;
3113 if (TREE_CODE (expr) == COMPONENT_REF
3114 && !useless_type_conversion_p (TREE_TYPE (expr),
3115 TREE_TYPE (TREE_OPERAND (expr, 1))))
3117 error ("type mismatch in component reference");
3118 debug_generic_stmt (TREE_TYPE (expr));
3119 debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3120 return true;
3123 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
3125 /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
3126 that their operand is not an SSA name or an invariant when
3127 requiring an lvalue (this usually means there is a SRA or IPA-SRA
3128 bug). Otherwise there is nothing to verify, gross mismatches at
3129 most invoke undefined behavior. */
3130 if (require_lvalue
3131 && (TREE_CODE (op) == SSA_NAME
3132 || is_gimple_min_invariant (op)))
3134 error ("conversion of an SSA_NAME on the left hand side");
3135 debug_generic_stmt (expr);
3136 return true;
3138 else if (TREE_CODE (op) == SSA_NAME
3139 && TYPE_SIZE (TREE_TYPE (expr)) != TYPE_SIZE (TREE_TYPE (op)))
3141 error ("conversion of register to a different size");
3142 debug_generic_stmt (expr);
3143 return true;
3145 else if (!handled_component_p (op))
3146 return false;
3149 expr = op;
3152 if (TREE_CODE (expr) == MEM_REF)
3154 if (!is_gimple_mem_ref_addr (TREE_OPERAND (expr, 0)))
3156 error ("invalid address operand in MEM_REF");
3157 debug_generic_stmt (expr);
3158 return true;
3160 if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
3161 || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
3163 error ("invalid offset operand in MEM_REF");
3164 debug_generic_stmt (expr);
3165 return true;
3168 else if (TREE_CODE (expr) == TARGET_MEM_REF)
3170 if (!TMR_BASE (expr)
3171 || !is_gimple_mem_ref_addr (TMR_BASE (expr)))
3173 error ("invalid address operand in TARGET_MEM_REF");
3174 return true;
3176 if (!TMR_OFFSET (expr)
3177 || TREE_CODE (TMR_OFFSET (expr)) != INTEGER_CST
3178 || !POINTER_TYPE_P (TREE_TYPE (TMR_OFFSET (expr))))
3180 error ("invalid offset operand in TARGET_MEM_REF");
3181 debug_generic_stmt (expr);
3182 return true;
3186 return ((require_lvalue || !is_gimple_min_invariant (expr))
3187 && verify_types_in_gimple_min_lval (expr));
3190 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3191 list of pointer-to types that is trivially convertible to DEST. */
3193 static bool
3194 one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3196 tree src;
3198 if (!TYPE_POINTER_TO (src_obj))
3199 return true;
3201 for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3202 if (useless_type_conversion_p (dest, src))
3203 return true;
3205 return false;
3208 /* Return true if TYPE1 is a fixed-point type and if conversions to and
3209 from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
3211 static bool
3212 valid_fixed_convert_types_p (tree type1, tree type2)
3214 return (FIXED_POINT_TYPE_P (type1)
3215 && (INTEGRAL_TYPE_P (type2)
3216 || SCALAR_FLOAT_TYPE_P (type2)
3217 || FIXED_POINT_TYPE_P (type2)));
3220 /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
3221 is a problem, otherwise false. */
3223 static bool
3224 verify_gimple_call (gimple stmt)
3226 tree fn = gimple_call_fn (stmt);
3227 tree fntype, fndecl;
3228 unsigned i;
3230 if (gimple_call_internal_p (stmt))
3232 if (fn)
3234 error ("gimple call has two targets");
3235 debug_generic_stmt (fn);
3236 return true;
3239 else
3241 if (!fn)
3243 error ("gimple call has no target");
3244 return true;
3248 if (fn && !is_gimple_call_addr (fn))
3250 error ("invalid function in gimple call");
3251 debug_generic_stmt (fn);
3252 return true;
3255 if (fn
3256 && (!POINTER_TYPE_P (TREE_TYPE (fn))
3257 || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
3258 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE)))
3260 error ("non-function in gimple call");
3261 return true;
3264 fndecl = gimple_call_fndecl (stmt);
3265 if (fndecl
3266 && TREE_CODE (fndecl) == FUNCTION_DECL
3267 && DECL_LOOPING_CONST_OR_PURE_P (fndecl)
3268 && !DECL_PURE_P (fndecl)
3269 && !TREE_READONLY (fndecl))
3271 error ("invalid pure const state for function");
3272 return true;
3275 if (gimple_call_lhs (stmt)
3276 && (!is_gimple_lvalue (gimple_call_lhs (stmt))
3277 || verify_types_in_gimple_reference (gimple_call_lhs (stmt), true)))
3279 error ("invalid LHS in gimple call");
3280 return true;
3283 if (gimple_call_lhs (stmt) && gimple_call_noreturn_p (stmt))
3285 error ("LHS in noreturn call");
3286 return true;
3289 fntype = gimple_call_fntype (stmt);
3290 if (fntype
3291 && gimple_call_lhs (stmt)
3292 && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
3293 TREE_TYPE (fntype))
3294 /* ??? At least C++ misses conversions at assignments from
3295 void * call results.
3296 ??? Java is completely off. Especially with functions
3297 returning java.lang.Object.
3298 For now simply allow arbitrary pointer type conversions. */
3299 && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
3300 && POINTER_TYPE_P (TREE_TYPE (fntype))))
3302 error ("invalid conversion in gimple call");
3303 debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
3304 debug_generic_stmt (TREE_TYPE (fntype));
3305 return true;
3308 if (gimple_call_chain (stmt)
3309 && !is_gimple_val (gimple_call_chain (stmt)))
3311 error ("invalid static chain in gimple call");
3312 debug_generic_stmt (gimple_call_chain (stmt));
3313 return true;
3316 /* If there is a static chain argument, this should not be an indirect
3317 call, and the decl should have DECL_STATIC_CHAIN set. */
3318 if (gimple_call_chain (stmt))
3320 if (!gimple_call_fndecl (stmt))
3322 error ("static chain in indirect gimple call");
3323 return true;
3325 fn = TREE_OPERAND (fn, 0);
3327 if (!DECL_STATIC_CHAIN (fn))
3329 error ("static chain with function that doesn%'t use one");
3330 return true;
3334 /* ??? The C frontend passes unpromoted arguments in case it
3335 didn't see a function declaration before the call. So for now
3336 leave the call arguments mostly unverified. Once we gimplify
3337 unit-at-a-time we have a chance to fix this. */
3339 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3341 tree arg = gimple_call_arg (stmt, i);
3342 if ((is_gimple_reg_type (TREE_TYPE (arg))
3343 && !is_gimple_val (arg))
3344 || (!is_gimple_reg_type (TREE_TYPE (arg))
3345 && !is_gimple_lvalue (arg)))
3347 error ("invalid argument to gimple call");
3348 debug_generic_expr (arg);
3349 return true;
3353 return false;
3356 /* Verifies the gimple comparison with the result type TYPE and
3357 the operands OP0 and OP1. */
3359 static bool
3360 verify_gimple_comparison (tree type, tree op0, tree op1)
3362 tree op0_type = TREE_TYPE (op0);
3363 tree op1_type = TREE_TYPE (op1);
3365 if (!is_gimple_val (op0) || !is_gimple_val (op1))
3367 error ("invalid operands in gimple comparison");
3368 return true;
3371 /* For comparisons we do not have the operations type as the
3372 effective type the comparison is carried out in. Instead
3373 we require that either the first operand is trivially
3374 convertible into the second, or the other way around.
3375 Because we special-case pointers to void we allow
3376 comparisons of pointers with the same mode as well. */
3377 if (!useless_type_conversion_p (op0_type, op1_type)
3378 && !useless_type_conversion_p (op1_type, op0_type)
3379 && (!POINTER_TYPE_P (op0_type)
3380 || !POINTER_TYPE_P (op1_type)
3381 || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
3383 error ("mismatching comparison operand types");
3384 debug_generic_expr (op0_type);
3385 debug_generic_expr (op1_type);
3386 return true;
3389 /* The resulting type of a comparison may be an effective boolean type. */
3390 if (INTEGRAL_TYPE_P (type)
3391 && (TREE_CODE (type) == BOOLEAN_TYPE
3392 || TYPE_PRECISION (type) == 1))
3394 if (TREE_CODE (op0_type) == VECTOR_TYPE
3395 || TREE_CODE (op1_type) == VECTOR_TYPE)
3397 error ("vector comparison returning a boolean");
3398 debug_generic_expr (op0_type);
3399 debug_generic_expr (op1_type);
3400 return true;
3403 /* Or an integer vector type with the same size and element count
3404 as the comparison operand types. */
3405 else if (TREE_CODE (type) == VECTOR_TYPE
3406 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
3408 if (TREE_CODE (op0_type) != VECTOR_TYPE
3409 || TREE_CODE (op1_type) != VECTOR_TYPE)
3411 error ("non-vector operands in vector comparison");
3412 debug_generic_expr (op0_type);
3413 debug_generic_expr (op1_type);
3414 return true;
3417 if (TYPE_VECTOR_SUBPARTS (type) != TYPE_VECTOR_SUBPARTS (op0_type)
3418 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)))
3419 != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0_type))))
3420 /* The result of a vector comparison is of signed
3421 integral type. */
3422 || TYPE_UNSIGNED (TREE_TYPE (type)))
3424 error ("invalid vector comparison resulting type");
3425 debug_generic_expr (type);
3426 return true;
3429 else
3431 error ("bogus comparison result type");
3432 debug_generic_expr (type);
3433 return true;
3436 return false;
3439 /* Verify a gimple assignment statement STMT with an unary rhs.
3440 Returns true if anything is wrong. */
3442 static bool
3443 verify_gimple_assign_unary (gimple stmt)
3445 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3446 tree lhs = gimple_assign_lhs (stmt);
3447 tree lhs_type = TREE_TYPE (lhs);
3448 tree rhs1 = gimple_assign_rhs1 (stmt);
3449 tree rhs1_type = TREE_TYPE (rhs1);
3451 if (!is_gimple_reg (lhs))
3453 error ("non-register as LHS of unary operation");
3454 return true;
3457 if (!is_gimple_val (rhs1))
3459 error ("invalid operand in unary operation");
3460 return true;
3463 /* First handle conversions. */
3464 switch (rhs_code)
3466 CASE_CONVERT:
3468 /* Allow conversions from pointer type to integral type only if
3469 there is no sign or zero extension involved.
3470 For targets were the precision of ptrofftype doesn't match that
3471 of pointers we need to allow arbitrary conversions to ptrofftype. */
3472 if ((POINTER_TYPE_P (lhs_type)
3473 && INTEGRAL_TYPE_P (rhs1_type))
3474 || (POINTER_TYPE_P (rhs1_type)
3475 && INTEGRAL_TYPE_P (lhs_type)
3476 && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
3477 || ptrofftype_p (sizetype))))
3478 return false;
3480 /* Allow conversion from integral to offset type and vice versa. */
3481 if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3482 && INTEGRAL_TYPE_P (rhs1_type))
3483 || (INTEGRAL_TYPE_P (lhs_type)
3484 && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3485 return false;
3487 /* Otherwise assert we are converting between types of the
3488 same kind. */
3489 if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3491 error ("invalid types in nop conversion");
3492 debug_generic_expr (lhs_type);
3493 debug_generic_expr (rhs1_type);
3494 return true;
3497 return false;
3500 case ADDR_SPACE_CONVERT_EXPR:
3502 if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
3503 || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
3504 == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
3506 error ("invalid types in address space conversion");
3507 debug_generic_expr (lhs_type);
3508 debug_generic_expr (rhs1_type);
3509 return true;
3512 return false;
3515 case FIXED_CONVERT_EXPR:
3517 if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3518 && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3520 error ("invalid types in fixed-point conversion");
3521 debug_generic_expr (lhs_type);
3522 debug_generic_expr (rhs1_type);
3523 return true;
3526 return false;
3529 case FLOAT_EXPR:
3531 if ((!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3532 && (!VECTOR_INTEGER_TYPE_P (rhs1_type)
3533 || !VECTOR_FLOAT_TYPE_P (lhs_type)))
3535 error ("invalid types in conversion to floating point");
3536 debug_generic_expr (lhs_type);
3537 debug_generic_expr (rhs1_type);
3538 return true;
3541 return false;
3544 case FIX_TRUNC_EXPR:
3546 if ((!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3547 && (!VECTOR_INTEGER_TYPE_P (lhs_type)
3548 || !VECTOR_FLOAT_TYPE_P (rhs1_type)))
3550 error ("invalid types in conversion to integer");
3551 debug_generic_expr (lhs_type);
3552 debug_generic_expr (rhs1_type);
3553 return true;
3556 return false;
3558 case REDUC_MAX_EXPR:
3559 case REDUC_MIN_EXPR:
3560 case REDUC_PLUS_EXPR:
3561 if (!VECTOR_TYPE_P (rhs1_type)
3562 || !useless_type_conversion_p (lhs_type, TREE_TYPE (rhs1_type)))
3564 error ("reduction should convert from vector to element type");
3565 debug_generic_expr (lhs_type);
3566 debug_generic_expr (rhs1_type);
3567 return true;
3569 return false;
3571 case VEC_UNPACK_HI_EXPR:
3572 case VEC_UNPACK_LO_EXPR:
3573 case VEC_UNPACK_FLOAT_HI_EXPR:
3574 case VEC_UNPACK_FLOAT_LO_EXPR:
3575 /* FIXME. */
3576 return false;
3578 case NEGATE_EXPR:
3579 case ABS_EXPR:
3580 case BIT_NOT_EXPR:
3581 case PAREN_EXPR:
3582 case CONJ_EXPR:
3583 break;
3585 default:
3586 gcc_unreachable ();
3589 /* For the remaining codes assert there is no conversion involved. */
3590 if (!useless_type_conversion_p (lhs_type, rhs1_type))
3592 error ("non-trivial conversion in unary operation");
3593 debug_generic_expr (lhs_type);
3594 debug_generic_expr (rhs1_type);
3595 return true;
3598 return false;
3601 /* Verify a gimple assignment statement STMT with a binary rhs.
3602 Returns true if anything is wrong. */
3604 static bool
3605 verify_gimple_assign_binary (gimple stmt)
3607 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3608 tree lhs = gimple_assign_lhs (stmt);
3609 tree lhs_type = TREE_TYPE (lhs);
3610 tree rhs1 = gimple_assign_rhs1 (stmt);
3611 tree rhs1_type = TREE_TYPE (rhs1);
3612 tree rhs2 = gimple_assign_rhs2 (stmt);
3613 tree rhs2_type = TREE_TYPE (rhs2);
3615 if (!is_gimple_reg (lhs))
3617 error ("non-register as LHS of binary operation");
3618 return true;
3621 if (!is_gimple_val (rhs1)
3622 || !is_gimple_val (rhs2))
3624 error ("invalid operands in binary operation");
3625 return true;
3628 /* First handle operations that involve different types. */
3629 switch (rhs_code)
3631 case COMPLEX_EXPR:
3633 if (TREE_CODE (lhs_type) != COMPLEX_TYPE
3634 || !(INTEGRAL_TYPE_P (rhs1_type)
3635 || SCALAR_FLOAT_TYPE_P (rhs1_type))
3636 || !(INTEGRAL_TYPE_P (rhs2_type)
3637 || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3639 error ("type mismatch in complex expression");
3640 debug_generic_expr (lhs_type);
3641 debug_generic_expr (rhs1_type);
3642 debug_generic_expr (rhs2_type);
3643 return true;
3646 return false;
3649 case LSHIFT_EXPR:
3650 case RSHIFT_EXPR:
3651 case LROTATE_EXPR:
3652 case RROTATE_EXPR:
3654 /* Shifts and rotates are ok on integral types, fixed point
3655 types and integer vector types. */
3656 if ((!INTEGRAL_TYPE_P (rhs1_type)
3657 && !FIXED_POINT_TYPE_P (rhs1_type)
3658 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3659 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
3660 || (!INTEGRAL_TYPE_P (rhs2_type)
3661 /* Vector shifts of vectors are also ok. */
3662 && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
3663 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3664 && TREE_CODE (rhs2_type) == VECTOR_TYPE
3665 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3666 || !useless_type_conversion_p (lhs_type, rhs1_type))
3668 error ("type mismatch in shift expression");
3669 debug_generic_expr (lhs_type);
3670 debug_generic_expr (rhs1_type);
3671 debug_generic_expr (rhs2_type);
3672 return true;
3675 return false;
3678 case VEC_RSHIFT_EXPR:
3680 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3681 || !(INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3682 || POINTER_TYPE_P (TREE_TYPE (rhs1_type))
3683 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1_type))
3684 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
3685 || (!INTEGRAL_TYPE_P (rhs2_type)
3686 && (TREE_CODE (rhs2_type) != VECTOR_TYPE
3687 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3688 || !useless_type_conversion_p (lhs_type, rhs1_type))
3690 error ("type mismatch in vector shift expression");
3691 debug_generic_expr (lhs_type);
3692 debug_generic_expr (rhs1_type);
3693 debug_generic_expr (rhs2_type);
3694 return true;
3696 /* For shifting a vector of non-integral components we
3697 only allow shifting by a constant multiple of the element size. */
3698 if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3699 && (TREE_CODE (rhs2) != INTEGER_CST
3700 || !div_if_zero_remainder (rhs2,
3701 TYPE_SIZE (TREE_TYPE (rhs1_type)))))
3703 error ("non-element sized vector shift of floating point vector");
3704 return true;
3707 return false;
3710 case WIDEN_LSHIFT_EXPR:
3712 if (!INTEGRAL_TYPE_P (lhs_type)
3713 || !INTEGRAL_TYPE_P (rhs1_type)
3714 || TREE_CODE (rhs2) != INTEGER_CST
3715 || (2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)))
3717 error ("type mismatch in widening vector shift expression");
3718 debug_generic_expr (lhs_type);
3719 debug_generic_expr (rhs1_type);
3720 debug_generic_expr (rhs2_type);
3721 return true;
3724 return false;
3727 case VEC_WIDEN_LSHIFT_HI_EXPR:
3728 case VEC_WIDEN_LSHIFT_LO_EXPR:
3730 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3731 || TREE_CODE (lhs_type) != VECTOR_TYPE
3732 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3733 || !INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3734 || TREE_CODE (rhs2) != INTEGER_CST
3735 || (2 * TYPE_PRECISION (TREE_TYPE (rhs1_type))
3736 > TYPE_PRECISION (TREE_TYPE (lhs_type))))
3738 error ("type mismatch in widening vector shift expression");
3739 debug_generic_expr (lhs_type);
3740 debug_generic_expr (rhs1_type);
3741 debug_generic_expr (rhs2_type);
3742 return true;
3745 return false;
3748 case PLUS_EXPR:
3749 case MINUS_EXPR:
3751 tree lhs_etype = lhs_type;
3752 tree rhs1_etype = rhs1_type;
3753 tree rhs2_etype = rhs2_type;
3754 if (TREE_CODE (lhs_type) == VECTOR_TYPE)
3756 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3757 || TREE_CODE (rhs2_type) != VECTOR_TYPE)
3759 error ("invalid non-vector operands to vector valued plus");
3760 return true;
3762 lhs_etype = TREE_TYPE (lhs_type);
3763 rhs1_etype = TREE_TYPE (rhs1_type);
3764 rhs2_etype = TREE_TYPE (rhs2_type);
3766 if (POINTER_TYPE_P (lhs_etype)
3767 || POINTER_TYPE_P (rhs1_etype)
3768 || POINTER_TYPE_P (rhs2_etype))
3770 error ("invalid (pointer) operands to plus/minus");
3771 return true;
3774 /* Continue with generic binary expression handling. */
3775 break;
3778 case POINTER_PLUS_EXPR:
3780 if (!POINTER_TYPE_P (rhs1_type)
3781 || !useless_type_conversion_p (lhs_type, rhs1_type)
3782 || !ptrofftype_p (rhs2_type))
3784 error ("type mismatch in pointer plus expression");
3785 debug_generic_stmt (lhs_type);
3786 debug_generic_stmt (rhs1_type);
3787 debug_generic_stmt (rhs2_type);
3788 return true;
3791 return false;
3794 case TRUTH_ANDIF_EXPR:
3795 case TRUTH_ORIF_EXPR:
3796 case TRUTH_AND_EXPR:
3797 case TRUTH_OR_EXPR:
3798 case TRUTH_XOR_EXPR:
3800 gcc_unreachable ();
3802 case LT_EXPR:
3803 case LE_EXPR:
3804 case GT_EXPR:
3805 case GE_EXPR:
3806 case EQ_EXPR:
3807 case NE_EXPR:
3808 case UNORDERED_EXPR:
3809 case ORDERED_EXPR:
3810 case UNLT_EXPR:
3811 case UNLE_EXPR:
3812 case UNGT_EXPR:
3813 case UNGE_EXPR:
3814 case UNEQ_EXPR:
3815 case LTGT_EXPR:
3816 /* Comparisons are also binary, but the result type is not
3817 connected to the operand types. */
3818 return verify_gimple_comparison (lhs_type, rhs1, rhs2);
3820 case WIDEN_MULT_EXPR:
3821 if (TREE_CODE (lhs_type) != INTEGER_TYPE)
3822 return true;
3823 return ((2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type))
3824 || (TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type)));
3826 case WIDEN_SUM_EXPR:
3827 case VEC_WIDEN_MULT_HI_EXPR:
3828 case VEC_WIDEN_MULT_LO_EXPR:
3829 case VEC_WIDEN_MULT_EVEN_EXPR:
3830 case VEC_WIDEN_MULT_ODD_EXPR:
3831 case VEC_PACK_TRUNC_EXPR:
3832 case VEC_PACK_SAT_EXPR:
3833 case VEC_PACK_FIX_TRUNC_EXPR:
3834 /* FIXME. */
3835 return false;
3837 case MULT_EXPR:
3838 case MULT_HIGHPART_EXPR:
3839 case TRUNC_DIV_EXPR:
3840 case CEIL_DIV_EXPR:
3841 case FLOOR_DIV_EXPR:
3842 case ROUND_DIV_EXPR:
3843 case TRUNC_MOD_EXPR:
3844 case CEIL_MOD_EXPR:
3845 case FLOOR_MOD_EXPR:
3846 case ROUND_MOD_EXPR:
3847 case RDIV_EXPR:
3848 case EXACT_DIV_EXPR:
3849 case MIN_EXPR:
3850 case MAX_EXPR:
3851 case BIT_IOR_EXPR:
3852 case BIT_XOR_EXPR:
3853 case BIT_AND_EXPR:
3854 /* Continue with generic binary expression handling. */
3855 break;
3857 default:
3858 gcc_unreachable ();
3861 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3862 || !useless_type_conversion_p (lhs_type, rhs2_type))
3864 error ("type mismatch in binary expression");
3865 debug_generic_stmt (lhs_type);
3866 debug_generic_stmt (rhs1_type);
3867 debug_generic_stmt (rhs2_type);
3868 return true;
3871 return false;
3874 /* Verify a gimple assignment statement STMT with a ternary rhs.
3875 Returns true if anything is wrong. */
3877 static bool
3878 verify_gimple_assign_ternary (gimple stmt)
3880 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3881 tree lhs = gimple_assign_lhs (stmt);
3882 tree lhs_type = TREE_TYPE (lhs);
3883 tree rhs1 = gimple_assign_rhs1 (stmt);
3884 tree rhs1_type = TREE_TYPE (rhs1);
3885 tree rhs2 = gimple_assign_rhs2 (stmt);
3886 tree rhs2_type = TREE_TYPE (rhs2);
3887 tree rhs3 = gimple_assign_rhs3 (stmt);
3888 tree rhs3_type = TREE_TYPE (rhs3);
3890 if (!is_gimple_reg (lhs))
3892 error ("non-register as LHS of ternary operation");
3893 return true;
3896 if (((rhs_code == VEC_COND_EXPR || rhs_code == COND_EXPR)
3897 ? !is_gimple_condexpr (rhs1) : !is_gimple_val (rhs1))
3898 || !is_gimple_val (rhs2)
3899 || !is_gimple_val (rhs3))
3901 error ("invalid operands in ternary operation");
3902 return true;
3905 /* First handle operations that involve different types. */
3906 switch (rhs_code)
3908 case WIDEN_MULT_PLUS_EXPR:
3909 case WIDEN_MULT_MINUS_EXPR:
3910 if ((!INTEGRAL_TYPE_P (rhs1_type)
3911 && !FIXED_POINT_TYPE_P (rhs1_type))
3912 || !useless_type_conversion_p (rhs1_type, rhs2_type)
3913 || !useless_type_conversion_p (lhs_type, rhs3_type)
3914 || 2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)
3915 || TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type))
3917 error ("type mismatch in widening multiply-accumulate expression");
3918 debug_generic_expr (lhs_type);
3919 debug_generic_expr (rhs1_type);
3920 debug_generic_expr (rhs2_type);
3921 debug_generic_expr (rhs3_type);
3922 return true;
3924 break;
3926 case FMA_EXPR:
3927 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3928 || !useless_type_conversion_p (lhs_type, rhs2_type)
3929 || !useless_type_conversion_p (lhs_type, rhs3_type))
3931 error ("type mismatch in fused multiply-add expression");
3932 debug_generic_expr (lhs_type);
3933 debug_generic_expr (rhs1_type);
3934 debug_generic_expr (rhs2_type);
3935 debug_generic_expr (rhs3_type);
3936 return true;
3938 break;
3940 case COND_EXPR:
3941 case VEC_COND_EXPR:
3942 if (!useless_type_conversion_p (lhs_type, rhs2_type)
3943 || !useless_type_conversion_p (lhs_type, rhs3_type))
3945 error ("type mismatch in conditional expression");
3946 debug_generic_expr (lhs_type);
3947 debug_generic_expr (rhs2_type);
3948 debug_generic_expr (rhs3_type);
3949 return true;
3951 break;
3953 case VEC_PERM_EXPR:
3954 if (!useless_type_conversion_p (lhs_type, rhs1_type)
3955 || !useless_type_conversion_p (lhs_type, rhs2_type))
3957 error ("type mismatch in vector permute expression");
3958 debug_generic_expr (lhs_type);
3959 debug_generic_expr (rhs1_type);
3960 debug_generic_expr (rhs2_type);
3961 debug_generic_expr (rhs3_type);
3962 return true;
3965 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3966 || TREE_CODE (rhs2_type) != VECTOR_TYPE
3967 || TREE_CODE (rhs3_type) != VECTOR_TYPE)
3969 error ("vector types expected in vector permute expression");
3970 debug_generic_expr (lhs_type);
3971 debug_generic_expr (rhs1_type);
3972 debug_generic_expr (rhs2_type);
3973 debug_generic_expr (rhs3_type);
3974 return true;
3977 if (TYPE_VECTOR_SUBPARTS (rhs1_type) != TYPE_VECTOR_SUBPARTS (rhs2_type)
3978 || TYPE_VECTOR_SUBPARTS (rhs2_type)
3979 != TYPE_VECTOR_SUBPARTS (rhs3_type)
3980 || TYPE_VECTOR_SUBPARTS (rhs3_type)
3981 != TYPE_VECTOR_SUBPARTS (lhs_type))
3983 error ("vectors with different element number found "
3984 "in vector permute expression");
3985 debug_generic_expr (lhs_type);
3986 debug_generic_expr (rhs1_type);
3987 debug_generic_expr (rhs2_type);
3988 debug_generic_expr (rhs3_type);
3989 return true;
3992 if (TREE_CODE (TREE_TYPE (rhs3_type)) != INTEGER_TYPE
3993 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs3_type)))
3994 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1_type))))
3996 error ("invalid mask type in vector permute expression");
3997 debug_generic_expr (lhs_type);
3998 debug_generic_expr (rhs1_type);
3999 debug_generic_expr (rhs2_type);
4000 debug_generic_expr (rhs3_type);
4001 return true;
4004 return false;
4006 case SAD_EXPR:
4007 if (!useless_type_conversion_p (rhs1_type, rhs2_type)
4008 || !useless_type_conversion_p (lhs_type, rhs3_type)
4009 || 2 * GET_MODE_BITSIZE (GET_MODE_INNER
4010 (TYPE_MODE (TREE_TYPE (rhs1_type))))
4011 > GET_MODE_BITSIZE (GET_MODE_INNER
4012 (TYPE_MODE (TREE_TYPE (lhs_type)))))
4014 error ("type mismatch in sad expression");
4015 debug_generic_expr (lhs_type);
4016 debug_generic_expr (rhs1_type);
4017 debug_generic_expr (rhs2_type);
4018 debug_generic_expr (rhs3_type);
4019 return true;
4022 if (TREE_CODE (rhs1_type) != VECTOR_TYPE
4023 || TREE_CODE (rhs2_type) != VECTOR_TYPE
4024 || TREE_CODE (rhs3_type) != VECTOR_TYPE)
4026 error ("vector types expected in sad expression");
4027 debug_generic_expr (lhs_type);
4028 debug_generic_expr (rhs1_type);
4029 debug_generic_expr (rhs2_type);
4030 debug_generic_expr (rhs3_type);
4031 return true;
4034 return false;
4036 case DOT_PROD_EXPR:
4037 case REALIGN_LOAD_EXPR:
4038 /* FIXME. */
4039 return false;
4041 default:
4042 gcc_unreachable ();
4044 return false;
4047 /* Verify a gimple assignment statement STMT with a single rhs.
4048 Returns true if anything is wrong. */
4050 static bool
4051 verify_gimple_assign_single (gimple stmt)
4053 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4054 tree lhs = gimple_assign_lhs (stmt);
4055 tree lhs_type = TREE_TYPE (lhs);
4056 tree rhs1 = gimple_assign_rhs1 (stmt);
4057 tree rhs1_type = TREE_TYPE (rhs1);
4058 bool res = false;
4060 if (!useless_type_conversion_p (lhs_type, rhs1_type))
4062 error ("non-trivial conversion at assignment");
4063 debug_generic_expr (lhs_type);
4064 debug_generic_expr (rhs1_type);
4065 return true;
4068 if (gimple_clobber_p (stmt)
4069 && !(DECL_P (lhs) || TREE_CODE (lhs) == MEM_REF))
4071 error ("non-decl/MEM_REF LHS in clobber statement");
4072 debug_generic_expr (lhs);
4073 return true;
4076 if (handled_component_p (lhs)
4077 || TREE_CODE (lhs) == MEM_REF
4078 || TREE_CODE (lhs) == TARGET_MEM_REF)
4079 res |= verify_types_in_gimple_reference (lhs, true);
4081 /* Special codes we cannot handle via their class. */
4082 switch (rhs_code)
4084 case ADDR_EXPR:
4086 tree op = TREE_OPERAND (rhs1, 0);
4087 if (!is_gimple_addressable (op))
4089 error ("invalid operand in unary expression");
4090 return true;
4093 /* Technically there is no longer a need for matching types, but
4094 gimple hygiene asks for this check. In LTO we can end up
4095 combining incompatible units and thus end up with addresses
4096 of globals that change their type to a common one. */
4097 if (!in_lto_p
4098 && !types_compatible_p (TREE_TYPE (op),
4099 TREE_TYPE (TREE_TYPE (rhs1)))
4100 && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
4101 TREE_TYPE (op)))
4103 error ("type mismatch in address expression");
4104 debug_generic_stmt (TREE_TYPE (rhs1));
4105 debug_generic_stmt (TREE_TYPE (op));
4106 return true;
4109 return verify_types_in_gimple_reference (op, true);
4112 /* tcc_reference */
4113 case INDIRECT_REF:
4114 error ("INDIRECT_REF in gimple IL");
4115 return true;
4117 case COMPONENT_REF:
4118 case BIT_FIELD_REF:
4119 case ARRAY_REF:
4120 case ARRAY_RANGE_REF:
4121 case VIEW_CONVERT_EXPR:
4122 case REALPART_EXPR:
4123 case IMAGPART_EXPR:
4124 case TARGET_MEM_REF:
4125 case MEM_REF:
4126 if (!is_gimple_reg (lhs)
4127 && is_gimple_reg_type (TREE_TYPE (lhs)))
4129 error ("invalid rhs for gimple memory store");
4130 debug_generic_stmt (lhs);
4131 debug_generic_stmt (rhs1);
4132 return true;
4134 return res || verify_types_in_gimple_reference (rhs1, false);
4136 /* tcc_constant */
4137 case SSA_NAME:
4138 case INTEGER_CST:
4139 case REAL_CST:
4140 case FIXED_CST:
4141 case COMPLEX_CST:
4142 case VECTOR_CST:
4143 case STRING_CST:
4144 return res;
4146 /* tcc_declaration */
4147 case CONST_DECL:
4148 return res;
4149 case VAR_DECL:
4150 case PARM_DECL:
4151 if (!is_gimple_reg (lhs)
4152 && !is_gimple_reg (rhs1)
4153 && is_gimple_reg_type (TREE_TYPE (lhs)))
4155 error ("invalid rhs for gimple memory store");
4156 debug_generic_stmt (lhs);
4157 debug_generic_stmt (rhs1);
4158 return true;
4160 return res;
4162 case CONSTRUCTOR:
4163 if (TREE_CODE (rhs1_type) == VECTOR_TYPE)
4165 unsigned int i;
4166 tree elt_i, elt_v, elt_t = NULL_TREE;
4168 if (CONSTRUCTOR_NELTS (rhs1) == 0)
4169 return res;
4170 /* For vector CONSTRUCTORs we require that either it is empty
4171 CONSTRUCTOR, or it is a CONSTRUCTOR of smaller vector elements
4172 (then the element count must be correct to cover the whole
4173 outer vector and index must be NULL on all elements, or it is
4174 a CONSTRUCTOR of scalar elements, where we as an exception allow
4175 smaller number of elements (assuming zero filling) and
4176 consecutive indexes as compared to NULL indexes (such
4177 CONSTRUCTORs can appear in the IL from FEs). */
4178 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), i, elt_i, elt_v)
4180 if (elt_t == NULL_TREE)
4182 elt_t = TREE_TYPE (elt_v);
4183 if (TREE_CODE (elt_t) == VECTOR_TYPE)
4185 tree elt_t = TREE_TYPE (elt_v);
4186 if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
4187 TREE_TYPE (elt_t)))
4189 error ("incorrect type of vector CONSTRUCTOR"
4190 " elements");
4191 debug_generic_stmt (rhs1);
4192 return true;
4194 else if (CONSTRUCTOR_NELTS (rhs1)
4195 * TYPE_VECTOR_SUBPARTS (elt_t)
4196 != TYPE_VECTOR_SUBPARTS (rhs1_type))
4198 error ("incorrect number of vector CONSTRUCTOR"
4199 " elements");
4200 debug_generic_stmt (rhs1);
4201 return true;
4204 else if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
4205 elt_t))
4207 error ("incorrect type of vector CONSTRUCTOR elements");
4208 debug_generic_stmt (rhs1);
4209 return true;
4211 else if (CONSTRUCTOR_NELTS (rhs1)
4212 > TYPE_VECTOR_SUBPARTS (rhs1_type))
4214 error ("incorrect number of vector CONSTRUCTOR elements");
4215 debug_generic_stmt (rhs1);
4216 return true;
4219 else if (!useless_type_conversion_p (elt_t, TREE_TYPE (elt_v)))
4221 error ("incorrect type of vector CONSTRUCTOR elements");
4222 debug_generic_stmt (rhs1);
4223 return true;
4225 if (elt_i != NULL_TREE
4226 && (TREE_CODE (elt_t) == VECTOR_TYPE
4227 || TREE_CODE (elt_i) != INTEGER_CST
4228 || compare_tree_int (elt_i, i) != 0))
4230 error ("vector CONSTRUCTOR with non-NULL element index");
4231 debug_generic_stmt (rhs1);
4232 return true;
4234 if (!is_gimple_val (elt_v))
4236 error ("vector CONSTRUCTOR element is not a GIMPLE value");
4237 debug_generic_stmt (rhs1);
4238 return true;
4242 else if (CONSTRUCTOR_NELTS (rhs1) != 0)
4244 error ("non-vector CONSTRUCTOR with elements");
4245 debug_generic_stmt (rhs1);
4246 return true;
4248 return res;
4249 case OBJ_TYPE_REF:
4250 case ASSERT_EXPR:
4251 case WITH_SIZE_EXPR:
4252 /* FIXME. */
4253 return res;
4255 default:;
4258 return res;
4261 /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
4262 is a problem, otherwise false. */
4264 static bool
4265 verify_gimple_assign (gimple stmt)
4267 switch (gimple_assign_rhs_class (stmt))
4269 case GIMPLE_SINGLE_RHS:
4270 return verify_gimple_assign_single (stmt);
4272 case GIMPLE_UNARY_RHS:
4273 return verify_gimple_assign_unary (stmt);
4275 case GIMPLE_BINARY_RHS:
4276 return verify_gimple_assign_binary (stmt);
4278 case GIMPLE_TERNARY_RHS:
4279 return verify_gimple_assign_ternary (stmt);
4281 default:
4282 gcc_unreachable ();
4286 /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
4287 is a problem, otherwise false. */
4289 static bool
4290 verify_gimple_return (gimple stmt)
4292 tree op = gimple_return_retval (stmt);
4293 tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
4295 /* We cannot test for present return values as we do not fix up missing
4296 return values from the original source. */
4297 if (op == NULL)
4298 return false;
4300 if (!is_gimple_val (op)
4301 && TREE_CODE (op) != RESULT_DECL)
4303 error ("invalid operand in return statement");
4304 debug_generic_stmt (op);
4305 return true;
4308 if ((TREE_CODE (op) == RESULT_DECL
4309 && DECL_BY_REFERENCE (op))
4310 || (TREE_CODE (op) == SSA_NAME
4311 && SSA_NAME_VAR (op)
4312 && TREE_CODE (SSA_NAME_VAR (op)) == RESULT_DECL
4313 && DECL_BY_REFERENCE (SSA_NAME_VAR (op))))
4314 op = TREE_TYPE (op);
4316 if (!useless_type_conversion_p (restype, TREE_TYPE (op)))
4318 error ("invalid conversion in return statement");
4319 debug_generic_stmt (restype);
4320 debug_generic_stmt (TREE_TYPE (op));
4321 return true;
4324 return false;
4328 /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
4329 is a problem, otherwise false. */
4331 static bool
4332 verify_gimple_goto (gimple stmt)
4334 tree dest = gimple_goto_dest (stmt);
4336 /* ??? We have two canonical forms of direct goto destinations, a
4337 bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
4338 if (TREE_CODE (dest) != LABEL_DECL
4339 && (!is_gimple_val (dest)
4340 || !POINTER_TYPE_P (TREE_TYPE (dest))))
4342 error ("goto destination is neither a label nor a pointer");
4343 return true;
4346 return false;
4349 /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
4350 is a problem, otherwise false. */
4352 static bool
4353 verify_gimple_switch (gimple stmt)
4355 unsigned int i, n;
4356 tree elt, prev_upper_bound = NULL_TREE;
4357 tree index_type, elt_type = NULL_TREE;
4359 if (!is_gimple_val (gimple_switch_index (stmt)))
4361 error ("invalid operand to switch statement");
4362 debug_generic_stmt (gimple_switch_index (stmt));
4363 return true;
4366 index_type = TREE_TYPE (gimple_switch_index (stmt));
4367 if (! INTEGRAL_TYPE_P (index_type))
4369 error ("non-integral type switch statement");
4370 debug_generic_expr (index_type);
4371 return true;
4374 elt = gimple_switch_label (stmt, 0);
4375 if (CASE_LOW (elt) != NULL_TREE || CASE_HIGH (elt) != NULL_TREE)
4377 error ("invalid default case label in switch statement");
4378 debug_generic_expr (elt);
4379 return true;
4382 n = gimple_switch_num_labels (stmt);
4383 for (i = 1; i < n; i++)
4385 elt = gimple_switch_label (stmt, i);
4387 if (! CASE_LOW (elt))
4389 error ("invalid case label in switch statement");
4390 debug_generic_expr (elt);
4391 return true;
4393 if (CASE_HIGH (elt)
4394 && ! tree_int_cst_lt (CASE_LOW (elt), CASE_HIGH (elt)))
4396 error ("invalid case range in switch statement");
4397 debug_generic_expr (elt);
4398 return true;
4401 if (elt_type)
4403 if (TREE_TYPE (CASE_LOW (elt)) != elt_type
4404 || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
4406 error ("type mismatch for case label in switch statement");
4407 debug_generic_expr (elt);
4408 return true;
4411 else
4413 elt_type = TREE_TYPE (CASE_LOW (elt));
4414 if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
4416 error ("type precision mismatch in switch statement");
4417 return true;
4421 if (prev_upper_bound)
4423 if (! tree_int_cst_lt (prev_upper_bound, CASE_LOW (elt)))
4425 error ("case labels not sorted in switch statement");
4426 return true;
4430 prev_upper_bound = CASE_HIGH (elt);
4431 if (! prev_upper_bound)
4432 prev_upper_bound = CASE_LOW (elt);
4435 return false;
4438 /* Verify a gimple debug statement STMT.
4439 Returns true if anything is wrong. */
4441 static bool
4442 verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
4444 /* There isn't much that could be wrong in a gimple debug stmt. A
4445 gimple debug bind stmt, for example, maps a tree, that's usually
4446 a VAR_DECL or a PARM_DECL, but that could also be some scalarized
4447 component or member of an aggregate type, to another tree, that
4448 can be an arbitrary expression. These stmts expand into debug
4449 insns, and are converted to debug notes by var-tracking.c. */
4450 return false;
4453 /* Verify a gimple label statement STMT.
4454 Returns true if anything is wrong. */
4456 static bool
4457 verify_gimple_label (gimple stmt)
4459 tree decl = gimple_label_label (stmt);
4460 int uid;
4461 bool err = false;
4463 if (TREE_CODE (decl) != LABEL_DECL)
4464 return true;
4465 if (!DECL_NONLOCAL (decl) && !FORCED_LABEL (decl)
4466 && DECL_CONTEXT (decl) != current_function_decl)
4468 error ("label's context is not the current function decl");
4469 err |= true;
4472 uid = LABEL_DECL_UID (decl);
4473 if (cfun->cfg
4474 && (uid == -1
4475 || (*label_to_block_map_for_fn (cfun))[uid] != gimple_bb (stmt)))
4477 error ("incorrect entry in label_to_block_map");
4478 err |= true;
4481 uid = EH_LANDING_PAD_NR (decl);
4482 if (uid)
4484 eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
4485 if (decl != lp->post_landing_pad)
4487 error ("incorrect setting of landing pad number");
4488 err |= true;
4492 return err;
4495 /* Verify the GIMPLE statement STMT. Returns true if there is an
4496 error, otherwise false. */
4498 static bool
4499 verify_gimple_stmt (gimple stmt)
4501 switch (gimple_code (stmt))
4503 case GIMPLE_ASSIGN:
4504 return verify_gimple_assign (stmt);
4506 case GIMPLE_LABEL:
4507 return verify_gimple_label (stmt);
4509 case GIMPLE_CALL:
4510 return verify_gimple_call (stmt);
4512 case GIMPLE_COND:
4513 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
4515 error ("invalid comparison code in gimple cond");
4516 return true;
4518 if (!(!gimple_cond_true_label (stmt)
4519 || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
4520 || !(!gimple_cond_false_label (stmt)
4521 || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
4523 error ("invalid labels in gimple cond");
4524 return true;
4527 return verify_gimple_comparison (boolean_type_node,
4528 gimple_cond_lhs (stmt),
4529 gimple_cond_rhs (stmt));
4531 case GIMPLE_GOTO:
4532 return verify_gimple_goto (stmt);
4534 case GIMPLE_SWITCH:
4535 return verify_gimple_switch (stmt);
4537 case GIMPLE_RETURN:
4538 return verify_gimple_return (stmt);
4540 case GIMPLE_ASM:
4541 return false;
4543 case GIMPLE_TRANSACTION:
4544 return verify_gimple_transaction (stmt);
4546 /* Tuples that do not have tree operands. */
4547 case GIMPLE_NOP:
4548 case GIMPLE_PREDICT:
4549 case GIMPLE_RESX:
4550 case GIMPLE_EH_DISPATCH:
4551 case GIMPLE_EH_MUST_NOT_THROW:
4552 return false;
4554 CASE_GIMPLE_OMP:
4555 /* OpenMP directives are validated by the FE and never operated
4556 on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
4557 non-gimple expressions when the main index variable has had
4558 its address taken. This does not affect the loop itself
4559 because the header of an GIMPLE_OMP_FOR is merely used to determine
4560 how to setup the parallel iteration. */
4561 return false;
4563 case GIMPLE_DEBUG:
4564 return verify_gimple_debug (stmt);
4566 default:
4567 gcc_unreachable ();
4571 /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
4572 and false otherwise. */
4574 static bool
4575 verify_gimple_phi (gimple phi)
4577 bool err = false;
4578 unsigned i;
4579 tree phi_result = gimple_phi_result (phi);
4580 bool virtual_p;
4582 if (!phi_result)
4584 error ("invalid PHI result");
4585 return true;
4588 virtual_p = virtual_operand_p (phi_result);
4589 if (TREE_CODE (phi_result) != SSA_NAME
4590 || (virtual_p
4591 && SSA_NAME_VAR (phi_result) != gimple_vop (cfun)))
4593 error ("invalid PHI result");
4594 err = true;
4597 for (i = 0; i < gimple_phi_num_args (phi); i++)
4599 tree t = gimple_phi_arg_def (phi, i);
4601 if (!t)
4603 error ("missing PHI def");
4604 err |= true;
4605 continue;
4607 /* Addressable variables do have SSA_NAMEs but they
4608 are not considered gimple values. */
4609 else if ((TREE_CODE (t) == SSA_NAME
4610 && virtual_p != virtual_operand_p (t))
4611 || (virtual_p
4612 && (TREE_CODE (t) != SSA_NAME
4613 || SSA_NAME_VAR (t) != gimple_vop (cfun)))
4614 || (!virtual_p
4615 && !is_gimple_val (t)))
4617 error ("invalid PHI argument");
4618 debug_generic_expr (t);
4619 err |= true;
4621 #ifdef ENABLE_TYPES_CHECKING
4622 if (!useless_type_conversion_p (TREE_TYPE (phi_result), TREE_TYPE (t)))
4624 error ("incompatible types in PHI argument %u", i);
4625 debug_generic_stmt (TREE_TYPE (phi_result));
4626 debug_generic_stmt (TREE_TYPE (t));
4627 err |= true;
4629 #endif
4632 return err;
4635 /* Verify the GIMPLE statements inside the sequence STMTS. */
4637 static bool
4638 verify_gimple_in_seq_2 (gimple_seq stmts)
4640 gimple_stmt_iterator ittr;
4641 bool err = false;
4643 for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
4645 gimple stmt = gsi_stmt (ittr);
4647 switch (gimple_code (stmt))
4649 case GIMPLE_BIND:
4650 err |= verify_gimple_in_seq_2 (gimple_bind_body (stmt));
4651 break;
4653 case GIMPLE_TRY:
4654 err |= verify_gimple_in_seq_2 (gimple_try_eval (stmt));
4655 err |= verify_gimple_in_seq_2 (gimple_try_cleanup (stmt));
4656 break;
4658 case GIMPLE_EH_FILTER:
4659 err |= verify_gimple_in_seq_2 (gimple_eh_filter_failure (stmt));
4660 break;
4662 case GIMPLE_EH_ELSE:
4663 err |= verify_gimple_in_seq_2 (gimple_eh_else_n_body (stmt));
4664 err |= verify_gimple_in_seq_2 (gimple_eh_else_e_body (stmt));
4665 break;
4667 case GIMPLE_CATCH:
4668 err |= verify_gimple_in_seq_2 (gimple_catch_handler (stmt));
4669 break;
4671 case GIMPLE_TRANSACTION:
4672 err |= verify_gimple_transaction (stmt);
4673 break;
4675 default:
4677 bool err2 = verify_gimple_stmt (stmt);
4678 if (err2)
4679 debug_gimple_stmt (stmt);
4680 err |= err2;
4685 return err;
4688 /* Verify the contents of a GIMPLE_TRANSACTION. Returns true if there
4689 is a problem, otherwise false. */
4691 static bool
4692 verify_gimple_transaction (gimple stmt)
4694 tree lab = gimple_transaction_label (stmt);
4695 if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
4696 return true;
4697 return verify_gimple_in_seq_2 (gimple_transaction_body (stmt));
4701 /* Verify the GIMPLE statements inside the statement list STMTS. */
4703 DEBUG_FUNCTION void
4704 verify_gimple_in_seq (gimple_seq stmts)
4706 timevar_push (TV_TREE_STMT_VERIFY);
4707 if (verify_gimple_in_seq_2 (stmts))
4708 internal_error ("verify_gimple failed");
4709 timevar_pop (TV_TREE_STMT_VERIFY);
4712 /* Return true when the T can be shared. */
4714 static bool
4715 tree_node_can_be_shared (tree t)
4717 if (IS_TYPE_OR_DECL_P (t)
4718 || is_gimple_min_invariant (t)
4719 || TREE_CODE (t) == SSA_NAME
4720 || t == error_mark_node
4721 || TREE_CODE (t) == IDENTIFIER_NODE)
4722 return true;
4724 if (TREE_CODE (t) == CASE_LABEL_EXPR)
4725 return true;
4727 if (DECL_P (t))
4728 return true;
4730 return false;
4733 /* Called via walk_tree. Verify tree sharing. */
4735 static tree
4736 verify_node_sharing_1 (tree *tp, int *walk_subtrees, void *data)
4738 hash_set<void *> *visited = (hash_set<void *> *) data;
4740 if (tree_node_can_be_shared (*tp))
4742 *walk_subtrees = false;
4743 return NULL;
4746 if (visited->add (*tp))
4747 return *tp;
4749 return NULL;
4752 /* Called via walk_gimple_stmt. Verify tree sharing. */
4754 static tree
4755 verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
4757 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4758 return verify_node_sharing_1 (tp, walk_subtrees, wi->info);
4761 static bool eh_error_found;
4762 bool
4763 verify_eh_throw_stmt_node (const gimple &stmt, const int &,
4764 hash_set<gimple> *visited)
4766 if (!visited->contains (stmt))
4768 error ("dead STMT in EH table");
4769 debug_gimple_stmt (stmt);
4770 eh_error_found = true;
4772 return true;
4775 /* Verify if the location LOCs block is in BLOCKS. */
4777 static bool
4778 verify_location (hash_set<tree> *blocks, location_t loc)
4780 tree block = LOCATION_BLOCK (loc);
4781 if (block != NULL_TREE
4782 && !blocks->contains (block))
4784 error ("location references block not in block tree");
4785 return true;
4787 if (block != NULL_TREE)
4788 return verify_location (blocks, BLOCK_SOURCE_LOCATION (block));
4789 return false;
4792 /* Called via walk_tree. Verify that expressions have no blocks. */
4794 static tree
4795 verify_expr_no_block (tree *tp, int *walk_subtrees, void *)
4797 if (!EXPR_P (*tp))
4799 *walk_subtrees = false;
4800 return NULL;
4803 location_t loc = EXPR_LOCATION (*tp);
4804 if (LOCATION_BLOCK (loc) != NULL)
4805 return *tp;
4807 return NULL;
4810 /* Called via walk_tree. Verify locations of expressions. */
4812 static tree
4813 verify_expr_location_1 (tree *tp, int *walk_subtrees, void *data)
4815 hash_set<tree> *blocks = (hash_set<tree> *) data;
4817 if (TREE_CODE (*tp) == VAR_DECL
4818 && DECL_HAS_DEBUG_EXPR_P (*tp))
4820 tree t = DECL_DEBUG_EXPR (*tp);
4821 tree addr = walk_tree (&t, verify_expr_no_block, NULL, NULL);
4822 if (addr)
4823 return addr;
4825 if ((TREE_CODE (*tp) == VAR_DECL
4826 || TREE_CODE (*tp) == PARM_DECL
4827 || TREE_CODE (*tp) == RESULT_DECL)
4828 && DECL_HAS_VALUE_EXPR_P (*tp))
4830 tree t = DECL_VALUE_EXPR (*tp);
4831 tree addr = walk_tree (&t, verify_expr_no_block, NULL, NULL);
4832 if (addr)
4833 return addr;
4836 if (!EXPR_P (*tp))
4838 *walk_subtrees = false;
4839 return NULL;
4842 location_t loc = EXPR_LOCATION (*tp);
4843 if (verify_location (blocks, loc))
4844 return *tp;
4846 return NULL;
4849 /* Called via walk_gimple_op. Verify locations of expressions. */
4851 static tree
4852 verify_expr_location (tree *tp, int *walk_subtrees, void *data)
4854 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4855 return verify_expr_location_1 (tp, walk_subtrees, wi->info);
4858 /* Insert all subblocks of BLOCK into BLOCKS and recurse. */
4860 static void
4861 collect_subblocks (hash_set<tree> *blocks, tree block)
4863 tree t;
4864 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
4866 blocks->add (t);
4867 collect_subblocks (blocks, t);
4871 /* Verify the GIMPLE statements in the CFG of FN. */
4873 DEBUG_FUNCTION void
4874 verify_gimple_in_cfg (struct function *fn, bool verify_nothrow)
4876 basic_block bb;
4877 bool err = false;
4879 timevar_push (TV_TREE_STMT_VERIFY);
4880 hash_set<void *> visited;
4881 hash_set<gimple> visited_stmts;
4883 /* Collect all BLOCKs referenced by the BLOCK tree of FN. */
4884 hash_set<tree> blocks;
4885 if (DECL_INITIAL (fn->decl))
4887 blocks.add (DECL_INITIAL (fn->decl));
4888 collect_subblocks (&blocks, DECL_INITIAL (fn->decl));
4891 FOR_EACH_BB_FN (bb, fn)
4893 gimple_stmt_iterator gsi;
4895 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4897 gimple phi = gsi_stmt (gsi);
4898 bool err2 = false;
4899 unsigned i;
4901 visited_stmts.add (phi);
4903 if (gimple_bb (phi) != bb)
4905 error ("gimple_bb (phi) is set to a wrong basic block");
4906 err2 = true;
4909 err2 |= verify_gimple_phi (phi);
4911 /* Only PHI arguments have locations. */
4912 if (gimple_location (phi) != UNKNOWN_LOCATION)
4914 error ("PHI node with location");
4915 err2 = true;
4918 for (i = 0; i < gimple_phi_num_args (phi); i++)
4920 tree arg = gimple_phi_arg_def (phi, i);
4921 tree addr = walk_tree (&arg, verify_node_sharing_1,
4922 &visited, NULL);
4923 if (addr)
4925 error ("incorrect sharing of tree nodes");
4926 debug_generic_expr (addr);
4927 err2 |= true;
4929 location_t loc = gimple_phi_arg_location (phi, i);
4930 if (virtual_operand_p (gimple_phi_result (phi))
4931 && loc != UNKNOWN_LOCATION)
4933 error ("virtual PHI with argument locations");
4934 err2 = true;
4936 addr = walk_tree (&arg, verify_expr_location_1, &blocks, NULL);
4937 if (addr)
4939 debug_generic_expr (addr);
4940 err2 = true;
4942 err2 |= verify_location (&blocks, loc);
4945 if (err2)
4946 debug_gimple_stmt (phi);
4947 err |= err2;
4950 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4952 gimple stmt = gsi_stmt (gsi);
4953 bool err2 = false;
4954 struct walk_stmt_info wi;
4955 tree addr;
4956 int lp_nr;
4958 visited_stmts.add (stmt);
4960 if (gimple_bb (stmt) != bb)
4962 error ("gimple_bb (stmt) is set to a wrong basic block");
4963 err2 = true;
4966 err2 |= verify_gimple_stmt (stmt);
4967 err2 |= verify_location (&blocks, gimple_location (stmt));
4969 memset (&wi, 0, sizeof (wi));
4970 wi.info = (void *) &visited;
4971 addr = walk_gimple_op (stmt, verify_node_sharing, &wi);
4972 if (addr)
4974 error ("incorrect sharing of tree nodes");
4975 debug_generic_expr (addr);
4976 err2 |= true;
4979 memset (&wi, 0, sizeof (wi));
4980 wi.info = (void *) &blocks;
4981 addr = walk_gimple_op (stmt, verify_expr_location, &wi);
4982 if (addr)
4984 debug_generic_expr (addr);
4985 err2 |= true;
4988 /* ??? Instead of not checking these stmts at all the walker
4989 should know its context via wi. */
4990 if (!is_gimple_debug (stmt)
4991 && !is_gimple_omp (stmt))
4993 memset (&wi, 0, sizeof (wi));
4994 addr = walk_gimple_op (stmt, verify_expr, &wi);
4995 if (addr)
4997 debug_generic_expr (addr);
4998 inform (gimple_location (stmt), "in statement");
4999 err2 |= true;
5003 /* If the statement is marked as part of an EH region, then it is
5004 expected that the statement could throw. Verify that when we
5005 have optimizations that simplify statements such that we prove
5006 that they cannot throw, that we update other data structures
5007 to match. */
5008 lp_nr = lookup_stmt_eh_lp (stmt);
5009 if (lp_nr > 0)
5011 if (!stmt_could_throw_p (stmt))
5013 if (verify_nothrow)
5015 error ("statement marked for throw, but doesn%'t");
5016 err2 |= true;
5019 else if (!gsi_one_before_end_p (gsi))
5021 error ("statement marked for throw in middle of block");
5022 err2 |= true;
5026 if (err2)
5027 debug_gimple_stmt (stmt);
5028 err |= err2;
5032 eh_error_found = false;
5033 hash_map<gimple, int> *eh_table = get_eh_throw_stmt_table (cfun);
5034 if (eh_table)
5035 eh_table->traverse<hash_set<gimple> *, verify_eh_throw_stmt_node>
5036 (&visited_stmts);
5038 if (err || eh_error_found)
5039 internal_error ("verify_gimple failed");
5041 verify_histograms ();
5042 timevar_pop (TV_TREE_STMT_VERIFY);
5046 /* Verifies that the flow information is OK. */
5048 static int
5049 gimple_verify_flow_info (void)
5051 int err = 0;
5052 basic_block bb;
5053 gimple_stmt_iterator gsi;
5054 gimple stmt;
5055 edge e;
5056 edge_iterator ei;
5058 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
5059 || ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
5061 error ("ENTRY_BLOCK has IL associated with it");
5062 err = 1;
5065 if (EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
5066 || EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
5068 error ("EXIT_BLOCK has IL associated with it");
5069 err = 1;
5072 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5073 if (e->flags & EDGE_FALLTHRU)
5075 error ("fallthru to exit from bb %d", e->src->index);
5076 err = 1;
5079 FOR_EACH_BB_FN (bb, cfun)
5081 bool found_ctrl_stmt = false;
5083 stmt = NULL;
5085 /* Skip labels on the start of basic block. */
5086 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5088 tree label;
5089 gimple prev_stmt = stmt;
5091 stmt = gsi_stmt (gsi);
5093 if (gimple_code (stmt) != GIMPLE_LABEL)
5094 break;
5096 label = gimple_label_label (stmt);
5097 if (prev_stmt && DECL_NONLOCAL (label))
5099 error ("nonlocal label ");
5100 print_generic_expr (stderr, label, 0);
5101 fprintf (stderr, " is not first in a sequence of labels in bb %d",
5102 bb->index);
5103 err = 1;
5106 if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
5108 error ("EH landing pad label ");
5109 print_generic_expr (stderr, label, 0);
5110 fprintf (stderr, " is not first in a sequence of labels in bb %d",
5111 bb->index);
5112 err = 1;
5115 if (label_to_block (label) != bb)
5117 error ("label ");
5118 print_generic_expr (stderr, label, 0);
5119 fprintf (stderr, " to block does not match in bb %d",
5120 bb->index);
5121 err = 1;
5124 if (decl_function_context (label) != current_function_decl)
5126 error ("label ");
5127 print_generic_expr (stderr, label, 0);
5128 fprintf (stderr, " has incorrect context in bb %d",
5129 bb->index);
5130 err = 1;
5134 /* Verify that body of basic block BB is free of control flow. */
5135 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5137 gimple stmt = gsi_stmt (gsi);
5139 if (found_ctrl_stmt)
5141 error ("control flow in the middle of basic block %d",
5142 bb->index);
5143 err = 1;
5146 if (stmt_ends_bb_p (stmt))
5147 found_ctrl_stmt = true;
5149 if (gimple_code (stmt) == GIMPLE_LABEL)
5151 error ("label ");
5152 print_generic_expr (stderr, gimple_label_label (stmt), 0);
5153 fprintf (stderr, " in the middle of basic block %d", bb->index);
5154 err = 1;
5158 gsi = gsi_last_bb (bb);
5159 if (gsi_end_p (gsi))
5160 continue;
5162 stmt = gsi_stmt (gsi);
5164 if (gimple_code (stmt) == GIMPLE_LABEL)
5165 continue;
5167 err |= verify_eh_edges (stmt);
5169 if (is_ctrl_stmt (stmt))
5171 FOR_EACH_EDGE (e, ei, bb->succs)
5172 if (e->flags & EDGE_FALLTHRU)
5174 error ("fallthru edge after a control statement in bb %d",
5175 bb->index);
5176 err = 1;
5180 if (gimple_code (stmt) != GIMPLE_COND)
5182 /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
5183 after anything else but if statement. */
5184 FOR_EACH_EDGE (e, ei, bb->succs)
5185 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
5187 error ("true/false edge after a non-GIMPLE_COND in bb %d",
5188 bb->index);
5189 err = 1;
5193 switch (gimple_code (stmt))
5195 case GIMPLE_COND:
5197 edge true_edge;
5198 edge false_edge;
5200 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
5202 if (!true_edge
5203 || !false_edge
5204 || !(true_edge->flags & EDGE_TRUE_VALUE)
5205 || !(false_edge->flags & EDGE_FALSE_VALUE)
5206 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
5207 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
5208 || EDGE_COUNT (bb->succs) >= 3)
5210 error ("wrong outgoing edge flags at end of bb %d",
5211 bb->index);
5212 err = 1;
5215 break;
5217 case GIMPLE_GOTO:
5218 if (simple_goto_p (stmt))
5220 error ("explicit goto at end of bb %d", bb->index);
5221 err = 1;
5223 else
5225 /* FIXME. We should double check that the labels in the
5226 destination blocks have their address taken. */
5227 FOR_EACH_EDGE (e, ei, bb->succs)
5228 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
5229 | EDGE_FALSE_VALUE))
5230 || !(e->flags & EDGE_ABNORMAL))
5232 error ("wrong outgoing edge flags at end of bb %d",
5233 bb->index);
5234 err = 1;
5237 break;
5239 case GIMPLE_CALL:
5240 if (!gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
5241 break;
5242 /* ... fallthru ... */
5243 case GIMPLE_RETURN:
5244 if (!single_succ_p (bb)
5245 || (single_succ_edge (bb)->flags
5246 & (EDGE_FALLTHRU | EDGE_ABNORMAL
5247 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
5249 error ("wrong outgoing edge flags at end of bb %d", bb->index);
5250 err = 1;
5252 if (single_succ (bb) != EXIT_BLOCK_PTR_FOR_FN (cfun))
5254 error ("return edge does not point to exit in bb %d",
5255 bb->index);
5256 err = 1;
5258 break;
5260 case GIMPLE_SWITCH:
5262 tree prev;
5263 edge e;
5264 size_t i, n;
5266 n = gimple_switch_num_labels (stmt);
5268 /* Mark all the destination basic blocks. */
5269 for (i = 0; i < n; ++i)
5271 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
5272 basic_block label_bb = label_to_block (lab);
5273 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
5274 label_bb->aux = (void *)1;
5277 /* Verify that the case labels are sorted. */
5278 prev = gimple_switch_label (stmt, 0);
5279 for (i = 1; i < n; ++i)
5281 tree c = gimple_switch_label (stmt, i);
5282 if (!CASE_LOW (c))
5284 error ("found default case not at the start of "
5285 "case vector");
5286 err = 1;
5287 continue;
5289 if (CASE_LOW (prev)
5290 && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
5292 error ("case labels not sorted: ");
5293 print_generic_expr (stderr, prev, 0);
5294 fprintf (stderr," is greater than ");
5295 print_generic_expr (stderr, c, 0);
5296 fprintf (stderr," but comes before it.\n");
5297 err = 1;
5299 prev = c;
5301 /* VRP will remove the default case if it can prove it will
5302 never be executed. So do not verify there always exists
5303 a default case here. */
5305 FOR_EACH_EDGE (e, ei, bb->succs)
5307 if (!e->dest->aux)
5309 error ("extra outgoing edge %d->%d",
5310 bb->index, e->dest->index);
5311 err = 1;
5314 e->dest->aux = (void *)2;
5315 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
5316 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
5318 error ("wrong outgoing edge flags at end of bb %d",
5319 bb->index);
5320 err = 1;
5324 /* Check that we have all of them. */
5325 for (i = 0; i < n; ++i)
5327 tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
5328 basic_block label_bb = label_to_block (lab);
5330 if (label_bb->aux != (void *)2)
5332 error ("missing edge %i->%i", bb->index, label_bb->index);
5333 err = 1;
5337 FOR_EACH_EDGE (e, ei, bb->succs)
5338 e->dest->aux = (void *)0;
5340 break;
5342 case GIMPLE_EH_DISPATCH:
5343 err |= verify_eh_dispatch_edge (stmt);
5344 break;
5346 default:
5347 break;
5351 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
5352 verify_dominators (CDI_DOMINATORS);
5354 return err;
5358 /* Updates phi nodes after creating a forwarder block joined
5359 by edge FALLTHRU. */
5361 static void
5362 gimple_make_forwarder_block (edge fallthru)
5364 edge e;
5365 edge_iterator ei;
5366 basic_block dummy, bb;
5367 tree var;
5368 gimple_stmt_iterator gsi;
5370 dummy = fallthru->src;
5371 bb = fallthru->dest;
5373 if (single_pred_p (bb))
5374 return;
5376 /* If we redirected a branch we must create new PHI nodes at the
5377 start of BB. */
5378 for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
5380 gimple phi, new_phi;
5382 phi = gsi_stmt (gsi);
5383 var = gimple_phi_result (phi);
5384 new_phi = create_phi_node (var, bb);
5385 gimple_phi_set_result (phi, copy_ssa_name (var, phi));
5386 add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
5387 UNKNOWN_LOCATION);
5390 /* Add the arguments we have stored on edges. */
5391 FOR_EACH_EDGE (e, ei, bb->preds)
5393 if (e == fallthru)
5394 continue;
5396 flush_pending_stmts (e);
5401 /* Return a non-special label in the head of basic block BLOCK.
5402 Create one if it doesn't exist. */
5404 tree
5405 gimple_block_label (basic_block bb)
5407 gimple_stmt_iterator i, s = gsi_start_bb (bb);
5408 bool first = true;
5409 tree label;
5410 gimple stmt;
5412 for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
5414 stmt = gsi_stmt (i);
5415 if (gimple_code (stmt) != GIMPLE_LABEL)
5416 break;
5417 label = gimple_label_label (stmt);
5418 if (!DECL_NONLOCAL (label))
5420 if (!first)
5421 gsi_move_before (&i, &s);
5422 return label;
5426 label = create_artificial_label (UNKNOWN_LOCATION);
5427 stmt = gimple_build_label (label);
5428 gsi_insert_before (&s, stmt, GSI_NEW_STMT);
5429 return label;
5433 /* Attempt to perform edge redirection by replacing a possibly complex
5434 jump instruction by a goto or by removing the jump completely.
5435 This can apply only if all edges now point to the same block. The
5436 parameters and return values are equivalent to
5437 redirect_edge_and_branch. */
5439 static edge
5440 gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
5442 basic_block src = e->src;
5443 gimple_stmt_iterator i;
5444 gimple stmt;
5446 /* We can replace or remove a complex jump only when we have exactly
5447 two edges. */
5448 if (EDGE_COUNT (src->succs) != 2
5449 /* Verify that all targets will be TARGET. Specifically, the
5450 edge that is not E must also go to TARGET. */
5451 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
5452 return NULL;
5454 i = gsi_last_bb (src);
5455 if (gsi_end_p (i))
5456 return NULL;
5458 stmt = gsi_stmt (i);
5460 if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
5462 gsi_remove (&i, true);
5463 e = ssa_redirect_edge (e, target);
5464 e->flags = EDGE_FALLTHRU;
5465 return e;
5468 return NULL;
5472 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
5473 edge representing the redirected branch. */
5475 static edge
5476 gimple_redirect_edge_and_branch (edge e, basic_block dest)
5478 basic_block bb = e->src;
5479 gimple_stmt_iterator gsi;
5480 edge ret;
5481 gimple stmt;
5483 if (e->flags & EDGE_ABNORMAL)
5484 return NULL;
5486 if (e->dest == dest)
5487 return NULL;
5489 if (e->flags & EDGE_EH)
5490 return redirect_eh_edge (e, dest);
5492 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5494 ret = gimple_try_redirect_by_replacing_jump (e, dest);
5495 if (ret)
5496 return ret;
5499 gsi = gsi_last_bb (bb);
5500 stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
5502 switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
5504 case GIMPLE_COND:
5505 /* For COND_EXPR, we only need to redirect the edge. */
5506 break;
5508 case GIMPLE_GOTO:
5509 /* No non-abnormal edges should lead from a non-simple goto, and
5510 simple ones should be represented implicitly. */
5511 gcc_unreachable ();
5513 case GIMPLE_SWITCH:
5515 tree label = gimple_block_label (dest);
5516 tree cases = get_cases_for_edge (e, stmt);
5518 /* If we have a list of cases associated with E, then use it
5519 as it's a lot faster than walking the entire case vector. */
5520 if (cases)
5522 edge e2 = find_edge (e->src, dest);
5523 tree last, first;
5525 first = cases;
5526 while (cases)
5528 last = cases;
5529 CASE_LABEL (cases) = label;
5530 cases = CASE_CHAIN (cases);
5533 /* If there was already an edge in the CFG, then we need
5534 to move all the cases associated with E to E2. */
5535 if (e2)
5537 tree cases2 = get_cases_for_edge (e2, stmt);
5539 CASE_CHAIN (last) = CASE_CHAIN (cases2);
5540 CASE_CHAIN (cases2) = first;
5542 bitmap_set_bit (touched_switch_bbs, gimple_bb (stmt)->index);
5544 else
5546 size_t i, n = gimple_switch_num_labels (stmt);
5548 for (i = 0; i < n; i++)
5550 tree elt = gimple_switch_label (stmt, i);
5551 if (label_to_block (CASE_LABEL (elt)) == e->dest)
5552 CASE_LABEL (elt) = label;
5556 break;
5558 case GIMPLE_ASM:
5560 int i, n = gimple_asm_nlabels (stmt);
5561 tree label = NULL;
5563 for (i = 0; i < n; ++i)
5565 tree cons = gimple_asm_label_op (stmt, i);
5566 if (label_to_block (TREE_VALUE (cons)) == e->dest)
5568 if (!label)
5569 label = gimple_block_label (dest);
5570 TREE_VALUE (cons) = label;
5574 /* If we didn't find any label matching the former edge in the
5575 asm labels, we must be redirecting the fallthrough
5576 edge. */
5577 gcc_assert (label || (e->flags & EDGE_FALLTHRU));
5579 break;
5581 case GIMPLE_RETURN:
5582 gsi_remove (&gsi, true);
5583 e->flags |= EDGE_FALLTHRU;
5584 break;
5586 case GIMPLE_OMP_RETURN:
5587 case GIMPLE_OMP_CONTINUE:
5588 case GIMPLE_OMP_SECTIONS_SWITCH:
5589 case GIMPLE_OMP_FOR:
5590 /* The edges from OMP constructs can be simply redirected. */
5591 break;
5593 case GIMPLE_EH_DISPATCH:
5594 if (!(e->flags & EDGE_FALLTHRU))
5595 redirect_eh_dispatch_edge (stmt, e, dest);
5596 break;
5598 case GIMPLE_TRANSACTION:
5599 /* The ABORT edge has a stored label associated with it, otherwise
5600 the edges are simply redirectable. */
5601 if (e->flags == 0)
5602 gimple_transaction_set_label (stmt, gimple_block_label (dest));
5603 break;
5605 default:
5606 /* Otherwise it must be a fallthru edge, and we don't need to
5607 do anything besides redirecting it. */
5608 gcc_assert (e->flags & EDGE_FALLTHRU);
5609 break;
5612 /* Update/insert PHI nodes as necessary. */
5614 /* Now update the edges in the CFG. */
5615 e = ssa_redirect_edge (e, dest);
5617 return e;
5620 /* Returns true if it is possible to remove edge E by redirecting
5621 it to the destination of the other edge from E->src. */
5623 static bool
5624 gimple_can_remove_branch_p (const_edge e)
5626 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
5627 return false;
5629 return true;
5632 /* Simple wrapper, as we can always redirect fallthru edges. */
5634 static basic_block
5635 gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
5637 e = gimple_redirect_edge_and_branch (e, dest);
5638 gcc_assert (e);
5640 return NULL;
5644 /* Splits basic block BB after statement STMT (but at least after the
5645 labels). If STMT is NULL, BB is split just after the labels. */
5647 static basic_block
5648 gimple_split_block (basic_block bb, void *stmt)
5650 gimple_stmt_iterator gsi;
5651 gimple_stmt_iterator gsi_tgt;
5652 gimple act;
5653 gimple_seq list;
5654 basic_block new_bb;
5655 edge e;
5656 edge_iterator ei;
5658 new_bb = create_empty_bb (bb);
5660 /* Redirect the outgoing edges. */
5661 new_bb->succs = bb->succs;
5662 bb->succs = NULL;
5663 FOR_EACH_EDGE (e, ei, new_bb->succs)
5664 e->src = new_bb;
5666 if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
5667 stmt = NULL;
5669 /* Move everything from GSI to the new basic block. */
5670 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5672 act = gsi_stmt (gsi);
5673 if (gimple_code (act) == GIMPLE_LABEL)
5674 continue;
5676 if (!stmt)
5677 break;
5679 if (stmt == act)
5681 gsi_next (&gsi);
5682 break;
5686 if (gsi_end_p (gsi))
5687 return new_bb;
5689 /* Split the statement list - avoid re-creating new containers as this
5690 brings ugly quadratic memory consumption in the inliner.
5691 (We are still quadratic since we need to update stmt BB pointers,
5692 sadly.) */
5693 gsi_split_seq_before (&gsi, &list);
5694 set_bb_seq (new_bb, list);
5695 for (gsi_tgt = gsi_start (list);
5696 !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
5697 gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
5699 return new_bb;
5703 /* Moves basic block BB after block AFTER. */
5705 static bool
5706 gimple_move_block_after (basic_block bb, basic_block after)
5708 if (bb->prev_bb == after)
5709 return true;
5711 unlink_block (bb);
5712 link_block (bb, after);
5714 return true;
5718 /* Return TRUE if block BB has no executable statements, otherwise return
5719 FALSE. */
5721 static bool
5722 gimple_empty_block_p (basic_block bb)
5724 /* BB must have no executable statements. */
5725 gimple_stmt_iterator gsi = gsi_after_labels (bb);
5726 if (phi_nodes (bb))
5727 return false;
5728 if (gsi_end_p (gsi))
5729 return true;
5730 if (is_gimple_debug (gsi_stmt (gsi)))
5731 gsi_next_nondebug (&gsi);
5732 return gsi_end_p (gsi);
5736 /* Split a basic block if it ends with a conditional branch and if the
5737 other part of the block is not empty. */
5739 static basic_block
5740 gimple_split_block_before_cond_jump (basic_block bb)
5742 gimple last, split_point;
5743 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
5744 if (gsi_end_p (gsi))
5745 return NULL;
5746 last = gsi_stmt (gsi);
5747 if (gimple_code (last) != GIMPLE_COND
5748 && gimple_code (last) != GIMPLE_SWITCH)
5749 return NULL;
5750 gsi_prev_nondebug (&gsi);
5751 split_point = gsi_stmt (gsi);
5752 return split_block (bb, split_point)->dest;
5756 /* Return true if basic_block can be duplicated. */
5758 static bool
5759 gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
5761 return true;
5764 /* Create a duplicate of the basic block BB. NOTE: This does not
5765 preserve SSA form. */
5767 static basic_block
5768 gimple_duplicate_bb (basic_block bb)
5770 basic_block new_bb;
5771 gimple_stmt_iterator gsi, gsi_tgt;
5772 gimple_seq phis = phi_nodes (bb);
5773 gimple phi, stmt, copy;
5775 new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
5777 /* Copy the PHI nodes. We ignore PHI node arguments here because
5778 the incoming edges have not been setup yet. */
5779 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
5781 phi = gsi_stmt (gsi);
5782 copy = create_phi_node (NULL_TREE, new_bb);
5783 create_new_def_for (gimple_phi_result (phi), copy,
5784 gimple_phi_result_ptr (copy));
5785 gimple_set_uid (copy, gimple_uid (phi));
5788 gsi_tgt = gsi_start_bb (new_bb);
5789 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5791 def_operand_p def_p;
5792 ssa_op_iter op_iter;
5793 tree lhs;
5795 stmt = gsi_stmt (gsi);
5796 if (gimple_code (stmt) == GIMPLE_LABEL)
5797 continue;
5799 /* Don't duplicate label debug stmts. */
5800 if (gimple_debug_bind_p (stmt)
5801 && TREE_CODE (gimple_debug_bind_get_var (stmt))
5802 == LABEL_DECL)
5803 continue;
5805 /* Create a new copy of STMT and duplicate STMT's virtual
5806 operands. */
5807 copy = gimple_copy (stmt);
5808 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
5810 maybe_duplicate_eh_stmt (copy, stmt);
5811 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
5813 /* When copying around a stmt writing into a local non-user
5814 aggregate, make sure it won't share stack slot with other
5815 vars. */
5816 lhs = gimple_get_lhs (stmt);
5817 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5819 tree base = get_base_address (lhs);
5820 if (base
5821 && (TREE_CODE (base) == VAR_DECL
5822 || TREE_CODE (base) == RESULT_DECL)
5823 && DECL_IGNORED_P (base)
5824 && !TREE_STATIC (base)
5825 && !DECL_EXTERNAL (base)
5826 && (TREE_CODE (base) != VAR_DECL
5827 || !DECL_HAS_VALUE_EXPR_P (base)))
5828 DECL_NONSHAREABLE (base) = 1;
5831 /* Create new names for all the definitions created by COPY and
5832 add replacement mappings for each new name. */
5833 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
5834 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
5837 return new_bb;
5840 /* Adds phi node arguments for edge E_COPY after basic block duplication. */
5842 static void
5843 add_phi_args_after_copy_edge (edge e_copy)
5845 basic_block bb, bb_copy = e_copy->src, dest;
5846 edge e;
5847 edge_iterator ei;
5848 gimple phi, phi_copy;
5849 tree def;
5850 gimple_stmt_iterator psi, psi_copy;
5852 if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
5853 return;
5855 bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
5857 if (e_copy->dest->flags & BB_DUPLICATED)
5858 dest = get_bb_original (e_copy->dest);
5859 else
5860 dest = e_copy->dest;
5862 e = find_edge (bb, dest);
5863 if (!e)
5865 /* During loop unrolling the target of the latch edge is copied.
5866 In this case we are not looking for edge to dest, but to
5867 duplicated block whose original was dest. */
5868 FOR_EACH_EDGE (e, ei, bb->succs)
5870 if ((e->dest->flags & BB_DUPLICATED)
5871 && get_bb_original (e->dest) == dest)
5872 break;
5875 gcc_assert (e != NULL);
5878 for (psi = gsi_start_phis (e->dest),
5879 psi_copy = gsi_start_phis (e_copy->dest);
5880 !gsi_end_p (psi);
5881 gsi_next (&psi), gsi_next (&psi_copy))
5883 phi = gsi_stmt (psi);
5884 phi_copy = gsi_stmt (psi_copy);
5885 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5886 add_phi_arg (phi_copy, def, e_copy,
5887 gimple_phi_arg_location_from_edge (phi, e));
5892 /* Basic block BB_COPY was created by code duplication. Add phi node
5893 arguments for edges going out of BB_COPY. The blocks that were
5894 duplicated have BB_DUPLICATED set. */
5896 void
5897 add_phi_args_after_copy_bb (basic_block bb_copy)
5899 edge e_copy;
5900 edge_iterator ei;
5902 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
5904 add_phi_args_after_copy_edge (e_copy);
5908 /* Blocks in REGION_COPY array of length N_REGION were created by
5909 duplication of basic blocks. Add phi node arguments for edges
5910 going from these blocks. If E_COPY is not NULL, also add
5911 phi node arguments for its destination.*/
5913 void
5914 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
5915 edge e_copy)
5917 unsigned i;
5919 for (i = 0; i < n_region; i++)
5920 region_copy[i]->flags |= BB_DUPLICATED;
5922 for (i = 0; i < n_region; i++)
5923 add_phi_args_after_copy_bb (region_copy[i]);
5924 if (e_copy)
5925 add_phi_args_after_copy_edge (e_copy);
5927 for (i = 0; i < n_region; i++)
5928 region_copy[i]->flags &= ~BB_DUPLICATED;
5931 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
5932 important exit edge EXIT. By important we mean that no SSA name defined
5933 inside region is live over the other exit edges of the region. All entry
5934 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
5935 to the duplicate of the region. Dominance and loop information is
5936 updated if UPDATE_DOMINANCE is true, but not the SSA web. If
5937 UPDATE_DOMINANCE is false then we assume that the caller will update the
5938 dominance information after calling this function. The new basic
5939 blocks are stored to REGION_COPY in the same order as they had in REGION,
5940 provided that REGION_COPY is not NULL.
5941 The function returns false if it is unable to copy the region,
5942 true otherwise. */
5944 bool
5945 gimple_duplicate_sese_region (edge entry, edge exit,
5946 basic_block *region, unsigned n_region,
5947 basic_block *region_copy,
5948 bool update_dominance)
5950 unsigned i;
5951 bool free_region_copy = false, copying_header = false;
5952 struct loop *loop = entry->dest->loop_father;
5953 edge exit_copy;
5954 vec<basic_block> doms;
5955 edge redirected;
5956 int total_freq = 0, entry_freq = 0;
5957 gcov_type total_count = 0, entry_count = 0;
5959 if (!can_copy_bbs_p (region, n_region))
5960 return false;
5962 /* Some sanity checking. Note that we do not check for all possible
5963 missuses of the functions. I.e. if you ask to copy something weird,
5964 it will work, but the state of structures probably will not be
5965 correct. */
5966 for (i = 0; i < n_region; i++)
5968 /* We do not handle subloops, i.e. all the blocks must belong to the
5969 same loop. */
5970 if (region[i]->loop_father != loop)
5971 return false;
5973 if (region[i] != entry->dest
5974 && region[i] == loop->header)
5975 return false;
5978 /* In case the function is used for loop header copying (which is the primary
5979 use), ensure that EXIT and its copy will be new latch and entry edges. */
5980 if (loop->header == entry->dest)
5982 copying_header = true;
5984 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5985 return false;
5987 for (i = 0; i < n_region; i++)
5988 if (region[i] != exit->src
5989 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5990 return false;
5993 initialize_original_copy_tables ();
5995 if (copying_header)
5996 set_loop_copy (loop, loop_outer (loop));
5997 else
5998 set_loop_copy (loop, loop);
6000 if (!region_copy)
6002 region_copy = XNEWVEC (basic_block, n_region);
6003 free_region_copy = true;
6006 /* Record blocks outside the region that are dominated by something
6007 inside. */
6008 if (update_dominance)
6010 doms.create (0);
6011 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
6014 if (entry->dest->count)
6016 total_count = entry->dest->count;
6017 entry_count = entry->count;
6018 /* Fix up corner cases, to avoid division by zero or creation of negative
6019 frequencies. */
6020 if (entry_count > total_count)
6021 entry_count = total_count;
6023 else
6025 total_freq = entry->dest->frequency;
6026 entry_freq = EDGE_FREQUENCY (entry);
6027 /* Fix up corner cases, to avoid division by zero or creation of negative
6028 frequencies. */
6029 if (total_freq == 0)
6030 total_freq = 1;
6031 else if (entry_freq > total_freq)
6032 entry_freq = total_freq;
6035 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
6036 split_edge_bb_loc (entry), update_dominance);
6037 if (total_count)
6039 scale_bbs_frequencies_gcov_type (region, n_region,
6040 total_count - entry_count,
6041 total_count);
6042 scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
6043 total_count);
6045 else
6047 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
6048 total_freq);
6049 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
6052 if (copying_header)
6054 loop->header = exit->dest;
6055 loop->latch = exit->src;
6058 /* Redirect the entry and add the phi node arguments. */
6059 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
6060 gcc_assert (redirected != NULL);
6061 flush_pending_stmts (entry);
6063 /* Concerning updating of dominators: We must recount dominators
6064 for entry block and its copy. Anything that is outside of the
6065 region, but was dominated by something inside needs recounting as
6066 well. */
6067 if (update_dominance)
6069 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
6070 doms.safe_push (get_bb_original (entry->dest));
6071 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
6072 doms.release ();
6075 /* Add the other PHI node arguments. */
6076 add_phi_args_after_copy (region_copy, n_region, NULL);
6078 if (free_region_copy)
6079 free (region_copy);
6081 free_original_copy_tables ();
6082 return true;
6085 /* Checks if BB is part of the region defined by N_REGION BBS. */
6086 static bool
6087 bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
6089 unsigned int n;
6091 for (n = 0; n < n_region; n++)
6093 if (bb == bbs[n])
6094 return true;
6096 return false;
6099 /* Duplicates REGION consisting of N_REGION blocks. The new blocks
6100 are stored to REGION_COPY in the same order in that they appear
6101 in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
6102 the region, EXIT an exit from it. The condition guarding EXIT
6103 is moved to ENTRY. Returns true if duplication succeeds, false
6104 otherwise.
6106 For example,
6108 some_code;
6109 if (cond)
6111 else
6114 is transformed to
6116 if (cond)
6118 some_code;
6121 else
6123 some_code;
6128 bool
6129 gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
6130 basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
6131 basic_block *region_copy ATTRIBUTE_UNUSED)
6133 unsigned i;
6134 bool free_region_copy = false;
6135 struct loop *loop = exit->dest->loop_father;
6136 struct loop *orig_loop = entry->dest->loop_father;
6137 basic_block switch_bb, entry_bb, nentry_bb;
6138 vec<basic_block> doms;
6139 int total_freq = 0, exit_freq = 0;
6140 gcov_type total_count = 0, exit_count = 0;
6141 edge exits[2], nexits[2], e;
6142 gimple_stmt_iterator gsi;
6143 gimple cond_stmt;
6144 edge sorig, snew;
6145 basic_block exit_bb;
6146 gimple_stmt_iterator psi;
6147 gimple phi;
6148 tree def;
6149 struct loop *target, *aloop, *cloop;
6151 gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
6152 exits[0] = exit;
6153 exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
6155 if (!can_copy_bbs_p (region, n_region))
6156 return false;
6158 initialize_original_copy_tables ();
6159 set_loop_copy (orig_loop, loop);
6161 target= loop;
6162 for (aloop = orig_loop->inner; aloop; aloop = aloop->next)
6164 if (bb_part_of_region_p (aloop->header, region, n_region))
6166 cloop = duplicate_loop (aloop, target);
6167 duplicate_subloops (aloop, cloop);
6171 if (!region_copy)
6173 region_copy = XNEWVEC (basic_block, n_region);
6174 free_region_copy = true;
6177 gcc_assert (!need_ssa_update_p (cfun));
6179 /* Record blocks outside the region that are dominated by something
6180 inside. */
6181 doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
6183 if (exit->src->count)
6185 total_count = exit->src->count;
6186 exit_count = exit->count;
6187 /* Fix up corner cases, to avoid division by zero or creation of negative
6188 frequencies. */
6189 if (exit_count > total_count)
6190 exit_count = total_count;
6192 else
6194 total_freq = exit->src->frequency;
6195 exit_freq = EDGE_FREQUENCY (exit);
6196 /* Fix up corner cases, to avoid division by zero or creation of negative
6197 frequencies. */
6198 if (total_freq == 0)
6199 total_freq = 1;
6200 if (exit_freq > total_freq)
6201 exit_freq = total_freq;
6204 copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
6205 split_edge_bb_loc (exit), true);
6206 if (total_count)
6208 scale_bbs_frequencies_gcov_type (region, n_region,
6209 total_count - exit_count,
6210 total_count);
6211 scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
6212 total_count);
6214 else
6216 scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
6217 total_freq);
6218 scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
6221 /* Create the switch block, and put the exit condition to it. */
6222 entry_bb = entry->dest;
6223 nentry_bb = get_bb_copy (entry_bb);
6224 if (!last_stmt (entry->src)
6225 || !stmt_ends_bb_p (last_stmt (entry->src)))
6226 switch_bb = entry->src;
6227 else
6228 switch_bb = split_edge (entry);
6229 set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
6231 gsi = gsi_last_bb (switch_bb);
6232 cond_stmt = last_stmt (exit->src);
6233 gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
6234 cond_stmt = gimple_copy (cond_stmt);
6236 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
6238 sorig = single_succ_edge (switch_bb);
6239 sorig->flags = exits[1]->flags;
6240 snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
6242 /* Register the new edge from SWITCH_BB in loop exit lists. */
6243 rescan_loop_exit (snew, true, false);
6245 /* Add the PHI node arguments. */
6246 add_phi_args_after_copy (region_copy, n_region, snew);
6248 /* Get rid of now superfluous conditions and associated edges (and phi node
6249 arguments). */
6250 exit_bb = exit->dest;
6252 e = redirect_edge_and_branch (exits[0], exits[1]->dest);
6253 PENDING_STMT (e) = NULL;
6255 /* The latch of ORIG_LOOP was copied, and so was the backedge
6256 to the original header. We redirect this backedge to EXIT_BB. */
6257 for (i = 0; i < n_region; i++)
6258 if (get_bb_original (region_copy[i]) == orig_loop->latch)
6260 gcc_assert (single_succ_edge (region_copy[i]));
6261 e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
6262 PENDING_STMT (e) = NULL;
6263 for (psi = gsi_start_phis (exit_bb);
6264 !gsi_end_p (psi);
6265 gsi_next (&psi))
6267 phi = gsi_stmt (psi);
6268 def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
6269 add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
6272 e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
6273 PENDING_STMT (e) = NULL;
6275 /* Anything that is outside of the region, but was dominated by something
6276 inside needs to update dominance info. */
6277 iterate_fix_dominators (CDI_DOMINATORS, doms, false);
6278 doms.release ();
6279 /* Update the SSA web. */
6280 update_ssa (TODO_update_ssa);
6282 if (free_region_copy)
6283 free (region_copy);
6285 free_original_copy_tables ();
6286 return true;
6289 /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
6290 adding blocks when the dominator traversal reaches EXIT. This
6291 function silently assumes that ENTRY strictly dominates EXIT. */
6293 void
6294 gather_blocks_in_sese_region (basic_block entry, basic_block exit,
6295 vec<basic_block> *bbs_p)
6297 basic_block son;
6299 for (son = first_dom_son (CDI_DOMINATORS, entry);
6300 son;
6301 son = next_dom_son (CDI_DOMINATORS, son))
6303 bbs_p->safe_push (son);
6304 if (son != exit)
6305 gather_blocks_in_sese_region (son, exit, bbs_p);
6309 /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
6310 The duplicates are recorded in VARS_MAP. */
6312 static void
6313 replace_by_duplicate_decl (tree *tp, hash_map<tree, tree> *vars_map,
6314 tree to_context)
6316 tree t = *tp, new_t;
6317 struct function *f = DECL_STRUCT_FUNCTION (to_context);
6319 if (DECL_CONTEXT (t) == to_context)
6320 return;
6322 bool existed;
6323 tree &loc = vars_map->get_or_insert (t, &existed);
6325 if (!existed)
6327 if (SSA_VAR_P (t))
6329 new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
6330 add_local_decl (f, new_t);
6332 else
6334 gcc_assert (TREE_CODE (t) == CONST_DECL);
6335 new_t = copy_node (t);
6337 DECL_CONTEXT (new_t) = to_context;
6339 loc = new_t;
6341 else
6342 new_t = loc;
6344 *tp = new_t;
6348 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
6349 VARS_MAP maps old ssa names and var_decls to the new ones. */
6351 static tree
6352 replace_ssa_name (tree name, hash_map<tree, tree> *vars_map,
6353 tree to_context)
6355 tree new_name;
6357 gcc_assert (!virtual_operand_p (name));
6359 tree *loc = vars_map->get (name);
6361 if (!loc)
6363 tree decl = SSA_NAME_VAR (name);
6364 if (decl)
6366 replace_by_duplicate_decl (&decl, vars_map, to_context);
6367 new_name = make_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
6368 decl, SSA_NAME_DEF_STMT (name));
6369 if (SSA_NAME_IS_DEFAULT_DEF (name))
6370 set_ssa_default_def (DECL_STRUCT_FUNCTION (to_context),
6371 decl, new_name);
6373 else
6374 new_name = copy_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
6375 name, SSA_NAME_DEF_STMT (name));
6377 vars_map->put (name, new_name);
6379 else
6380 new_name = *loc;
6382 return new_name;
6385 struct move_stmt_d
6387 tree orig_block;
6388 tree new_block;
6389 tree from_context;
6390 tree to_context;
6391 hash_map<tree, tree> *vars_map;
6392 htab_t new_label_map;
6393 hash_map<void *, void *> *eh_map;
6394 bool remap_decls_p;
6397 /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
6398 contained in *TP if it has been ORIG_BLOCK previously and change the
6399 DECL_CONTEXT of every local variable referenced in *TP. */
6401 static tree
6402 move_stmt_op (tree *tp, int *walk_subtrees, void *data)
6404 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6405 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6406 tree t = *tp;
6408 if (EXPR_P (t))
6410 tree block = TREE_BLOCK (t);
6411 if (block == p->orig_block
6412 || (p->orig_block == NULL_TREE
6413 && block != NULL_TREE))
6414 TREE_SET_BLOCK (t, p->new_block);
6415 #ifdef ENABLE_CHECKING
6416 else if (block != NULL_TREE)
6418 while (block && TREE_CODE (block) == BLOCK && block != p->orig_block)
6419 block = BLOCK_SUPERCONTEXT (block);
6420 gcc_assert (block == p->orig_block);
6422 #endif
6424 else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
6426 if (TREE_CODE (t) == SSA_NAME)
6427 *tp = replace_ssa_name (t, p->vars_map, p->to_context);
6428 else if (TREE_CODE (t) == LABEL_DECL)
6430 if (p->new_label_map)
6432 struct tree_map in, *out;
6433 in.base.from = t;
6434 out = (struct tree_map *)
6435 htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
6436 if (out)
6437 *tp = t = out->to;
6440 DECL_CONTEXT (t) = p->to_context;
6442 else if (p->remap_decls_p)
6444 /* Replace T with its duplicate. T should no longer appear in the
6445 parent function, so this looks wasteful; however, it may appear
6446 in referenced_vars, and more importantly, as virtual operands of
6447 statements, and in alias lists of other variables. It would be
6448 quite difficult to expunge it from all those places. ??? It might
6449 suffice to do this for addressable variables. */
6450 if ((TREE_CODE (t) == VAR_DECL
6451 && !is_global_var (t))
6452 || TREE_CODE (t) == CONST_DECL)
6453 replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
6455 *walk_subtrees = 0;
6457 else if (TYPE_P (t))
6458 *walk_subtrees = 0;
6460 return NULL_TREE;
6463 /* Helper for move_stmt_r. Given an EH region number for the source
6464 function, map that to the duplicate EH regio number in the dest. */
6466 static int
6467 move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
6469 eh_region old_r, new_r;
6471 old_r = get_eh_region_from_number (old_nr);
6472 new_r = static_cast<eh_region> (*p->eh_map->get (old_r));
6474 return new_r->index;
6477 /* Similar, but operate on INTEGER_CSTs. */
6479 static tree
6480 move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
6482 int old_nr, new_nr;
6484 old_nr = tree_to_shwi (old_t_nr);
6485 new_nr = move_stmt_eh_region_nr (old_nr, p);
6487 return build_int_cst (integer_type_node, new_nr);
6490 /* Like move_stmt_op, but for gimple statements.
6492 Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
6493 contained in the current statement in *GSI_P and change the
6494 DECL_CONTEXT of every local variable referenced in the current
6495 statement. */
6497 static tree
6498 move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
6499 struct walk_stmt_info *wi)
6501 struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
6502 gimple stmt = gsi_stmt (*gsi_p);
6503 tree block = gimple_block (stmt);
6505 if (block == p->orig_block
6506 || (p->orig_block == NULL_TREE
6507 && block != NULL_TREE))
6508 gimple_set_block (stmt, p->new_block);
6510 switch (gimple_code (stmt))
6512 case GIMPLE_CALL:
6513 /* Remap the region numbers for __builtin_eh_{pointer,filter}. */
6515 tree r, fndecl = gimple_call_fndecl (stmt);
6516 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
6517 switch (DECL_FUNCTION_CODE (fndecl))
6519 case BUILT_IN_EH_COPY_VALUES:
6520 r = gimple_call_arg (stmt, 1);
6521 r = move_stmt_eh_region_tree_nr (r, p);
6522 gimple_call_set_arg (stmt, 1, r);
6523 /* FALLTHRU */
6525 case BUILT_IN_EH_POINTER:
6526 case BUILT_IN_EH_FILTER:
6527 r = gimple_call_arg (stmt, 0);
6528 r = move_stmt_eh_region_tree_nr (r, p);
6529 gimple_call_set_arg (stmt, 0, r);
6530 break;
6532 default:
6533 break;
6536 break;
6538 case GIMPLE_RESX:
6540 int r = gimple_resx_region (stmt);
6541 r = move_stmt_eh_region_nr (r, p);
6542 gimple_resx_set_region (stmt, r);
6544 break;
6546 case GIMPLE_EH_DISPATCH:
6548 int r = gimple_eh_dispatch_region (stmt);
6549 r = move_stmt_eh_region_nr (r, p);
6550 gimple_eh_dispatch_set_region (stmt, r);
6552 break;
6554 case GIMPLE_OMP_RETURN:
6555 case GIMPLE_OMP_CONTINUE:
6556 break;
6557 default:
6558 if (is_gimple_omp (stmt))
6560 /* Do not remap variables inside OMP directives. Variables
6561 referenced in clauses and directive header belong to the
6562 parent function and should not be moved into the child
6563 function. */
6564 bool save_remap_decls_p = p->remap_decls_p;
6565 p->remap_decls_p = false;
6566 *handled_ops_p = true;
6568 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), move_stmt_r,
6569 move_stmt_op, wi);
6571 p->remap_decls_p = save_remap_decls_p;
6573 break;
6576 return NULL_TREE;
6579 /* Move basic block BB from function CFUN to function DEST_FN. The
6580 block is moved out of the original linked list and placed after
6581 block AFTER in the new list. Also, the block is removed from the
6582 original array of blocks and placed in DEST_FN's array of blocks.
6583 If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
6584 updated to reflect the moved edges.
6586 The local variables are remapped to new instances, VARS_MAP is used
6587 to record the mapping. */
6589 static void
6590 move_block_to_fn (struct function *dest_cfun, basic_block bb,
6591 basic_block after, bool update_edge_count_p,
6592 struct move_stmt_d *d)
6594 struct control_flow_graph *cfg;
6595 edge_iterator ei;
6596 edge e;
6597 gimple_stmt_iterator si;
6598 unsigned old_len, new_len;
6600 /* Remove BB from dominance structures. */
6601 delete_from_dominance_info (CDI_DOMINATORS, bb);
6603 /* Move BB from its current loop to the copy in the new function. */
6604 if (current_loops)
6606 struct loop *new_loop = (struct loop *)bb->loop_father->aux;
6607 if (new_loop)
6608 bb->loop_father = new_loop;
6611 /* Link BB to the new linked list. */
6612 move_block_after (bb, after);
6614 /* Update the edge count in the corresponding flowgraphs. */
6615 if (update_edge_count_p)
6616 FOR_EACH_EDGE (e, ei, bb->succs)
6618 cfun->cfg->x_n_edges--;
6619 dest_cfun->cfg->x_n_edges++;
6622 /* Remove BB from the original basic block array. */
6623 (*cfun->cfg->x_basic_block_info)[bb->index] = NULL;
6624 cfun->cfg->x_n_basic_blocks--;
6626 /* Grow DEST_CFUN's basic block array if needed. */
6627 cfg = dest_cfun->cfg;
6628 cfg->x_n_basic_blocks++;
6629 if (bb->index >= cfg->x_last_basic_block)
6630 cfg->x_last_basic_block = bb->index + 1;
6632 old_len = vec_safe_length (cfg->x_basic_block_info);
6633 if ((unsigned) cfg->x_last_basic_block >= old_len)
6635 new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
6636 vec_safe_grow_cleared (cfg->x_basic_block_info, new_len);
6639 (*cfg->x_basic_block_info)[bb->index] = bb;
6641 /* Remap the variables in phi nodes. */
6642 for (si = gsi_start_phis (bb); !gsi_end_p (si); )
6644 gimple phi = gsi_stmt (si);
6645 use_operand_p use;
6646 tree op = PHI_RESULT (phi);
6647 ssa_op_iter oi;
6648 unsigned i;
6650 if (virtual_operand_p (op))
6652 /* Remove the phi nodes for virtual operands (alias analysis will be
6653 run for the new function, anyway). */
6654 remove_phi_node (&si, true);
6655 continue;
6658 SET_PHI_RESULT (phi,
6659 replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6660 FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
6662 op = USE_FROM_PTR (use);
6663 if (TREE_CODE (op) == SSA_NAME)
6664 SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
6667 for (i = 0; i < EDGE_COUNT (bb->preds); i++)
6669 location_t locus = gimple_phi_arg_location (phi, i);
6670 tree block = LOCATION_BLOCK (locus);
6672 if (locus == UNKNOWN_LOCATION)
6673 continue;
6674 if (d->orig_block == NULL_TREE || block == d->orig_block)
6676 if (d->new_block == NULL_TREE)
6677 locus = LOCATION_LOCUS (locus);
6678 else
6679 locus = COMBINE_LOCATION_DATA (line_table, locus, d->new_block);
6680 gimple_phi_arg_set_location (phi, i, locus);
6684 gsi_next (&si);
6687 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6689 gimple stmt = gsi_stmt (si);
6690 struct walk_stmt_info wi;
6692 memset (&wi, 0, sizeof (wi));
6693 wi.info = d;
6694 walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
6696 if (gimple_code (stmt) == GIMPLE_LABEL)
6698 tree label = gimple_label_label (stmt);
6699 int uid = LABEL_DECL_UID (label);
6701 gcc_assert (uid > -1);
6703 old_len = vec_safe_length (cfg->x_label_to_block_map);
6704 if (old_len <= (unsigned) uid)
6706 new_len = 3 * uid / 2 + 1;
6707 vec_safe_grow_cleared (cfg->x_label_to_block_map, new_len);
6710 (*cfg->x_label_to_block_map)[uid] = bb;
6711 (*cfun->cfg->x_label_to_block_map)[uid] = NULL;
6713 gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
6715 if (uid >= dest_cfun->cfg->last_label_uid)
6716 dest_cfun->cfg->last_label_uid = uid + 1;
6719 maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
6720 remove_stmt_from_eh_lp_fn (cfun, stmt);
6722 gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
6723 gimple_remove_stmt_histograms (cfun, stmt);
6725 /* We cannot leave any operands allocated from the operand caches of
6726 the current function. */
6727 free_stmt_operands (cfun, stmt);
6728 push_cfun (dest_cfun);
6729 update_stmt (stmt);
6730 pop_cfun ();
6733 FOR_EACH_EDGE (e, ei, bb->succs)
6734 if (e->goto_locus != UNKNOWN_LOCATION)
6736 tree block = LOCATION_BLOCK (e->goto_locus);
6737 if (d->orig_block == NULL_TREE
6738 || block == d->orig_block)
6739 e->goto_locus = d->new_block ?
6740 COMBINE_LOCATION_DATA (line_table, e->goto_locus, d->new_block) :
6741 LOCATION_LOCUS (e->goto_locus);
6745 /* Examine the statements in BB (which is in SRC_CFUN); find and return
6746 the outermost EH region. Use REGION as the incoming base EH region. */
6748 static eh_region
6749 find_outermost_region_in_block (struct function *src_cfun,
6750 basic_block bb, eh_region region)
6752 gimple_stmt_iterator si;
6754 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6756 gimple stmt = gsi_stmt (si);
6757 eh_region stmt_region;
6758 int lp_nr;
6760 lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
6761 stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
6762 if (stmt_region)
6764 if (region == NULL)
6765 region = stmt_region;
6766 else if (stmt_region != region)
6768 region = eh_region_outermost (src_cfun, stmt_region, region);
6769 gcc_assert (region != NULL);
6774 return region;
6777 static tree
6778 new_label_mapper (tree decl, void *data)
6780 htab_t hash = (htab_t) data;
6781 struct tree_map *m;
6782 void **slot;
6784 gcc_assert (TREE_CODE (decl) == LABEL_DECL);
6786 m = XNEW (struct tree_map);
6787 m->hash = DECL_UID (decl);
6788 m->base.from = decl;
6789 m->to = create_artificial_label (UNKNOWN_LOCATION);
6790 LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
6791 if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
6792 cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
6794 slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
6795 gcc_assert (*slot == NULL);
6797 *slot = m;
6799 return m->to;
6802 /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
6803 subblocks. */
6805 static void
6806 replace_block_vars_by_duplicates (tree block, hash_map<tree, tree> *vars_map,
6807 tree to_context)
6809 tree *tp, t;
6811 for (tp = &BLOCK_VARS (block); *tp; tp = &DECL_CHAIN (*tp))
6813 t = *tp;
6814 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
6815 continue;
6816 replace_by_duplicate_decl (&t, vars_map, to_context);
6817 if (t != *tp)
6819 if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
6821 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
6822 DECL_HAS_VALUE_EXPR_P (t) = 1;
6824 DECL_CHAIN (t) = DECL_CHAIN (*tp);
6825 *tp = t;
6829 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
6830 replace_block_vars_by_duplicates (block, vars_map, to_context);
6833 /* Fixup the loop arrays and numbers after moving LOOP and its subloops
6834 from FN1 to FN2. */
6836 static void
6837 fixup_loop_arrays_after_move (struct function *fn1, struct function *fn2,
6838 struct loop *loop)
6840 /* Discard it from the old loop array. */
6841 (*get_loops (fn1))[loop->num] = NULL;
6843 /* Place it in the new loop array, assigning it a new number. */
6844 loop->num = number_of_loops (fn2);
6845 vec_safe_push (loops_for_fn (fn2)->larray, loop);
6847 /* Recurse to children. */
6848 for (loop = loop->inner; loop; loop = loop->next)
6849 fixup_loop_arrays_after_move (fn1, fn2, loop);
6852 /* Move a single-entry, single-exit region delimited by ENTRY_BB and
6853 EXIT_BB to function DEST_CFUN. The whole region is replaced by a
6854 single basic block in the original CFG and the new basic block is
6855 returned. DEST_CFUN must not have a CFG yet.
6857 Note that the region need not be a pure SESE region. Blocks inside
6858 the region may contain calls to abort/exit. The only restriction
6859 is that ENTRY_BB should be the only entry point and it must
6860 dominate EXIT_BB.
6862 Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
6863 functions outermost BLOCK, move all subblocks of ORIG_BLOCK
6864 to the new function.
6866 All local variables referenced in the region are assumed to be in
6867 the corresponding BLOCK_VARS and unexpanded variable lists
6868 associated with DEST_CFUN. */
6870 basic_block
6871 move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
6872 basic_block exit_bb, tree orig_block)
6874 vec<basic_block> bbs, dom_bbs;
6875 basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
6876 basic_block after, bb, *entry_pred, *exit_succ, abb;
6877 struct function *saved_cfun = cfun;
6878 int *entry_flag, *exit_flag;
6879 unsigned *entry_prob, *exit_prob;
6880 unsigned i, num_entry_edges, num_exit_edges, num_nodes;
6881 edge e;
6882 edge_iterator ei;
6883 htab_t new_label_map;
6884 hash_map<void *, void *> *eh_map;
6885 struct loop *loop = entry_bb->loop_father;
6886 struct loop *loop0 = get_loop (saved_cfun, 0);
6887 struct move_stmt_d d;
6889 /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
6890 region. */
6891 gcc_assert (entry_bb != exit_bb
6892 && (!exit_bb
6893 || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
6895 /* Collect all the blocks in the region. Manually add ENTRY_BB
6896 because it won't be added by dfs_enumerate_from. */
6897 bbs.create (0);
6898 bbs.safe_push (entry_bb);
6899 gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
6901 /* The blocks that used to be dominated by something in BBS will now be
6902 dominated by the new block. */
6903 dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
6904 bbs.address (),
6905 bbs.length ());
6907 /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
6908 the predecessor edges to ENTRY_BB and the successor edges to
6909 EXIT_BB so that we can re-attach them to the new basic block that
6910 will replace the region. */
6911 num_entry_edges = EDGE_COUNT (entry_bb->preds);
6912 entry_pred = XNEWVEC (basic_block, num_entry_edges);
6913 entry_flag = XNEWVEC (int, num_entry_edges);
6914 entry_prob = XNEWVEC (unsigned, num_entry_edges);
6915 i = 0;
6916 for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
6918 entry_prob[i] = e->probability;
6919 entry_flag[i] = e->flags;
6920 entry_pred[i++] = e->src;
6921 remove_edge (e);
6924 if (exit_bb)
6926 num_exit_edges = EDGE_COUNT (exit_bb->succs);
6927 exit_succ = XNEWVEC (basic_block, num_exit_edges);
6928 exit_flag = XNEWVEC (int, num_exit_edges);
6929 exit_prob = XNEWVEC (unsigned, num_exit_edges);
6930 i = 0;
6931 for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
6933 exit_prob[i] = e->probability;
6934 exit_flag[i] = e->flags;
6935 exit_succ[i++] = e->dest;
6936 remove_edge (e);
6939 else
6941 num_exit_edges = 0;
6942 exit_succ = NULL;
6943 exit_flag = NULL;
6944 exit_prob = NULL;
6947 /* Switch context to the child function to initialize DEST_FN's CFG. */
6948 gcc_assert (dest_cfun->cfg == NULL);
6949 push_cfun (dest_cfun);
6951 init_empty_tree_cfg ();
6953 /* Initialize EH information for the new function. */
6954 eh_map = NULL;
6955 new_label_map = NULL;
6956 if (saved_cfun->eh)
6958 eh_region region = NULL;
6960 FOR_EACH_VEC_ELT (bbs, i, bb)
6961 region = find_outermost_region_in_block (saved_cfun, bb, region);
6963 init_eh_for_function ();
6964 if (region != NULL)
6966 new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
6967 eh_map = duplicate_eh_regions (saved_cfun, region, 0,
6968 new_label_mapper, new_label_map);
6972 /* Initialize an empty loop tree. */
6973 struct loops *loops = ggc_cleared_alloc<struct loops> ();
6974 init_loops_structure (dest_cfun, loops, 1);
6975 loops->state = LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
6976 set_loops_for_fn (dest_cfun, loops);
6978 /* Move the outlined loop tree part. */
6979 num_nodes = bbs.length ();
6980 FOR_EACH_VEC_ELT (bbs, i, bb)
6982 if (bb->loop_father->header == bb)
6984 struct loop *this_loop = bb->loop_father;
6985 struct loop *outer = loop_outer (this_loop);
6986 if (outer == loop
6987 /* If the SESE region contains some bbs ending with
6988 a noreturn call, those are considered to belong
6989 to the outermost loop in saved_cfun, rather than
6990 the entry_bb's loop_father. */
6991 || outer == loop0)
6993 if (outer != loop)
6994 num_nodes -= this_loop->num_nodes;
6995 flow_loop_tree_node_remove (bb->loop_father);
6996 flow_loop_tree_node_add (get_loop (dest_cfun, 0), this_loop);
6997 fixup_loop_arrays_after_move (saved_cfun, cfun, this_loop);
7000 else if (bb->loop_father == loop0 && loop0 != loop)
7001 num_nodes--;
7003 /* Remove loop exits from the outlined region. */
7004 if (loops_for_fn (saved_cfun)->exits)
7005 FOR_EACH_EDGE (e, ei, bb->succs)
7007 struct loops *l = loops_for_fn (saved_cfun);
7008 loop_exit **slot
7009 = l->exits->find_slot_with_hash (e, htab_hash_pointer (e),
7010 NO_INSERT);
7011 if (slot)
7012 l->exits->clear_slot (slot);
7017 /* Adjust the number of blocks in the tree root of the outlined part. */
7018 get_loop (dest_cfun, 0)->num_nodes = bbs.length () + 2;
7020 /* Setup a mapping to be used by move_block_to_fn. */
7021 loop->aux = current_loops->tree_root;
7022 loop0->aux = current_loops->tree_root;
7024 pop_cfun ();
7026 /* Move blocks from BBS into DEST_CFUN. */
7027 gcc_assert (bbs.length () >= 2);
7028 after = dest_cfun->cfg->x_entry_block_ptr;
7029 hash_map<tree, tree> vars_map;
7031 memset (&d, 0, sizeof (d));
7032 d.orig_block = orig_block;
7033 d.new_block = DECL_INITIAL (dest_cfun->decl);
7034 d.from_context = cfun->decl;
7035 d.to_context = dest_cfun->decl;
7036 d.vars_map = &vars_map;
7037 d.new_label_map = new_label_map;
7038 d.eh_map = eh_map;
7039 d.remap_decls_p = true;
7041 FOR_EACH_VEC_ELT (bbs, i, bb)
7043 /* No need to update edge counts on the last block. It has
7044 already been updated earlier when we detached the region from
7045 the original CFG. */
7046 move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
7047 after = bb;
7050 loop->aux = NULL;
7051 loop0->aux = NULL;
7052 /* Loop sizes are no longer correct, fix them up. */
7053 loop->num_nodes -= num_nodes;
7054 for (struct loop *outer = loop_outer (loop);
7055 outer; outer = loop_outer (outer))
7056 outer->num_nodes -= num_nodes;
7057 loop0->num_nodes -= bbs.length () - num_nodes;
7059 if (saved_cfun->has_simduid_loops || saved_cfun->has_force_vectorize_loops)
7061 struct loop *aloop;
7062 for (i = 0; vec_safe_iterate (loops->larray, i, &aloop); i++)
7063 if (aloop != NULL)
7065 if (aloop->simduid)
7067 replace_by_duplicate_decl (&aloop->simduid, d.vars_map,
7068 d.to_context);
7069 dest_cfun->has_simduid_loops = true;
7071 if (aloop->force_vectorize)
7072 dest_cfun->has_force_vectorize_loops = true;
7076 /* Rewire BLOCK_SUBBLOCKS of orig_block. */
7077 if (orig_block)
7079 tree block;
7080 gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
7081 == NULL_TREE);
7082 BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
7083 = BLOCK_SUBBLOCKS (orig_block);
7084 for (block = BLOCK_SUBBLOCKS (orig_block);
7085 block; block = BLOCK_CHAIN (block))
7086 BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
7087 BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
7090 replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
7091 &vars_map, dest_cfun->decl);
7093 if (new_label_map)
7094 htab_delete (new_label_map);
7095 if (eh_map)
7096 delete eh_map;
7098 /* Rewire the entry and exit blocks. The successor to the entry
7099 block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
7100 the child function. Similarly, the predecessor of DEST_FN's
7101 EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
7102 need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
7103 various CFG manipulation function get to the right CFG.
7105 FIXME, this is silly. The CFG ought to become a parameter to
7106 these helpers. */
7107 push_cfun (dest_cfun);
7108 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), entry_bb, EDGE_FALLTHRU);
7109 if (exit_bb)
7110 make_edge (exit_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
7111 pop_cfun ();
7113 /* Back in the original function, the SESE region has disappeared,
7114 create a new basic block in its place. */
7115 bb = create_empty_bb (entry_pred[0]);
7116 if (current_loops)
7117 add_bb_to_loop (bb, loop);
7118 for (i = 0; i < num_entry_edges; i++)
7120 e = make_edge (entry_pred[i], bb, entry_flag[i]);
7121 e->probability = entry_prob[i];
7124 for (i = 0; i < num_exit_edges; i++)
7126 e = make_edge (bb, exit_succ[i], exit_flag[i]);
7127 e->probability = exit_prob[i];
7130 set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
7131 FOR_EACH_VEC_ELT (dom_bbs, i, abb)
7132 set_immediate_dominator (CDI_DOMINATORS, abb, bb);
7133 dom_bbs.release ();
7135 if (exit_bb)
7137 free (exit_prob);
7138 free (exit_flag);
7139 free (exit_succ);
7141 free (entry_prob);
7142 free (entry_flag);
7143 free (entry_pred);
7144 bbs.release ();
7146 return bb;
7150 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in dumpfile.h)
7153 void
7154 dump_function_to_file (tree fndecl, FILE *file, int flags)
7156 tree arg, var, old_current_fndecl = current_function_decl;
7157 struct function *dsf;
7158 bool ignore_topmost_bind = false, any_var = false;
7159 basic_block bb;
7160 tree chain;
7161 bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
7162 && decl_is_tm_clone (fndecl));
7163 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
7165 current_function_decl = fndecl;
7166 fprintf (file, "%s %s(", function_name (fun), tmclone ? "[tm-clone] " : "");
7168 arg = DECL_ARGUMENTS (fndecl);
7169 while (arg)
7171 print_generic_expr (file, TREE_TYPE (arg), dump_flags);
7172 fprintf (file, " ");
7173 print_generic_expr (file, arg, dump_flags);
7174 if (flags & TDF_VERBOSE)
7175 print_node (file, "", arg, 4);
7176 if (DECL_CHAIN (arg))
7177 fprintf (file, ", ");
7178 arg = DECL_CHAIN (arg);
7180 fprintf (file, ")\n");
7182 if (flags & TDF_VERBOSE)
7183 print_node (file, "", fndecl, 2);
7185 dsf = DECL_STRUCT_FUNCTION (fndecl);
7186 if (dsf && (flags & TDF_EH))
7187 dump_eh_tree (file, dsf);
7189 if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
7191 dump_node (fndecl, TDF_SLIM | flags, file);
7192 current_function_decl = old_current_fndecl;
7193 return;
7196 /* When GIMPLE is lowered, the variables are no longer available in
7197 BIND_EXPRs, so display them separately. */
7198 if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
7200 unsigned ix;
7201 ignore_topmost_bind = true;
7203 fprintf (file, "{\n");
7204 if (!vec_safe_is_empty (fun->local_decls))
7205 FOR_EACH_LOCAL_DECL (fun, ix, var)
7207 print_generic_decl (file, var, flags);
7208 if (flags & TDF_VERBOSE)
7209 print_node (file, "", var, 4);
7210 fprintf (file, "\n");
7212 any_var = true;
7214 if (gimple_in_ssa_p (cfun))
7215 for (ix = 1; ix < num_ssa_names; ++ix)
7217 tree name = ssa_name (ix);
7218 if (name && !SSA_NAME_VAR (name))
7220 fprintf (file, " ");
7221 print_generic_expr (file, TREE_TYPE (name), flags);
7222 fprintf (file, " ");
7223 print_generic_expr (file, name, flags);
7224 fprintf (file, ";\n");
7226 any_var = true;
7231 if (fun && fun->decl == fndecl
7232 && fun->cfg
7233 && basic_block_info_for_fn (fun))
7235 /* If the CFG has been built, emit a CFG-based dump. */
7236 if (!ignore_topmost_bind)
7237 fprintf (file, "{\n");
7239 if (any_var && n_basic_blocks_for_fn (fun))
7240 fprintf (file, "\n");
7242 FOR_EACH_BB_FN (bb, fun)
7243 dump_bb (file, bb, 2, flags | TDF_COMMENT);
7245 fprintf (file, "}\n");
7247 else if (DECL_SAVED_TREE (fndecl) == NULL)
7249 /* The function is now in GIMPLE form but the CFG has not been
7250 built yet. Emit the single sequence of GIMPLE statements
7251 that make up its body. */
7252 gimple_seq body = gimple_body (fndecl);
7254 if (gimple_seq_first_stmt (body)
7255 && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
7256 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
7257 print_gimple_seq (file, body, 0, flags);
7258 else
7260 if (!ignore_topmost_bind)
7261 fprintf (file, "{\n");
7263 if (any_var)
7264 fprintf (file, "\n");
7266 print_gimple_seq (file, body, 2, flags);
7267 fprintf (file, "}\n");
7270 else
7272 int indent;
7274 /* Make a tree based dump. */
7275 chain = DECL_SAVED_TREE (fndecl);
7276 if (chain && TREE_CODE (chain) == BIND_EXPR)
7278 if (ignore_topmost_bind)
7280 chain = BIND_EXPR_BODY (chain);
7281 indent = 2;
7283 else
7284 indent = 0;
7286 else
7288 if (!ignore_topmost_bind)
7289 fprintf (file, "{\n");
7290 indent = 2;
7293 if (any_var)
7294 fprintf (file, "\n");
7296 print_generic_stmt_indented (file, chain, flags, indent);
7297 if (ignore_topmost_bind)
7298 fprintf (file, "}\n");
7301 if (flags & TDF_ENUMERATE_LOCALS)
7302 dump_enumerated_decls (file, flags);
7303 fprintf (file, "\n\n");
7305 current_function_decl = old_current_fndecl;
7308 /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
7310 DEBUG_FUNCTION void
7311 debug_function (tree fn, int flags)
7313 dump_function_to_file (fn, stderr, flags);
7317 /* Print on FILE the indexes for the predecessors of basic_block BB. */
7319 static void
7320 print_pred_bbs (FILE *file, basic_block bb)
7322 edge e;
7323 edge_iterator ei;
7325 FOR_EACH_EDGE (e, ei, bb->preds)
7326 fprintf (file, "bb_%d ", e->src->index);
7330 /* Print on FILE the indexes for the successors of basic_block BB. */
7332 static void
7333 print_succ_bbs (FILE *file, basic_block bb)
7335 edge e;
7336 edge_iterator ei;
7338 FOR_EACH_EDGE (e, ei, bb->succs)
7339 fprintf (file, "bb_%d ", e->dest->index);
7342 /* Print to FILE the basic block BB following the VERBOSITY level. */
7344 void
7345 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
7347 char *s_indent = (char *) alloca ((size_t) indent + 1);
7348 memset ((void *) s_indent, ' ', (size_t) indent);
7349 s_indent[indent] = '\0';
7351 /* Print basic_block's header. */
7352 if (verbosity >= 2)
7354 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
7355 print_pred_bbs (file, bb);
7356 fprintf (file, "}, succs = {");
7357 print_succ_bbs (file, bb);
7358 fprintf (file, "})\n");
7361 /* Print basic_block's body. */
7362 if (verbosity >= 3)
7364 fprintf (file, "%s {\n", s_indent);
7365 dump_bb (file, bb, indent + 4, TDF_VOPS|TDF_MEMSYMS);
7366 fprintf (file, "%s }\n", s_indent);
7370 static void print_loop_and_siblings (FILE *, struct loop *, int, int);
7372 /* Pretty print LOOP on FILE, indented INDENT spaces. Following
7373 VERBOSITY level this outputs the contents of the loop, or just its
7374 structure. */
7376 static void
7377 print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
7379 char *s_indent;
7380 basic_block bb;
7382 if (loop == NULL)
7383 return;
7385 s_indent = (char *) alloca ((size_t) indent + 1);
7386 memset ((void *) s_indent, ' ', (size_t) indent);
7387 s_indent[indent] = '\0';
7389 /* Print loop's header. */
7390 fprintf (file, "%sloop_%d (", s_indent, loop->num);
7391 if (loop->header)
7392 fprintf (file, "header = %d", loop->header->index);
7393 else
7395 fprintf (file, "deleted)\n");
7396 return;
7398 if (loop->latch)
7399 fprintf (file, ", latch = %d", loop->latch->index);
7400 else
7401 fprintf (file, ", multiple latches");
7402 fprintf (file, ", niter = ");
7403 print_generic_expr (file, loop->nb_iterations, 0);
7405 if (loop->any_upper_bound)
7407 fprintf (file, ", upper_bound = ");
7408 print_decu (loop->nb_iterations_upper_bound, file);
7411 if (loop->any_estimate)
7413 fprintf (file, ", estimate = ");
7414 print_decu (loop->nb_iterations_estimate, file);
7416 fprintf (file, ")\n");
7418 /* Print loop's body. */
7419 if (verbosity >= 1)
7421 fprintf (file, "%s{\n", s_indent);
7422 FOR_EACH_BB_FN (bb, cfun)
7423 if (bb->loop_father == loop)
7424 print_loops_bb (file, bb, indent, verbosity);
7426 print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
7427 fprintf (file, "%s}\n", s_indent);
7431 /* Print the LOOP and its sibling loops on FILE, indented INDENT
7432 spaces. Following VERBOSITY level this outputs the contents of the
7433 loop, or just its structure. */
7435 static void
7436 print_loop_and_siblings (FILE *file, struct loop *loop, int indent,
7437 int verbosity)
7439 if (loop == NULL)
7440 return;
7442 print_loop (file, loop, indent, verbosity);
7443 print_loop_and_siblings (file, loop->next, indent, verbosity);
7446 /* Follow a CFG edge from the entry point of the program, and on entry
7447 of a loop, pretty print the loop structure on FILE. */
7449 void
7450 print_loops (FILE *file, int verbosity)
7452 basic_block bb;
7454 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
7455 if (bb && bb->loop_father)
7456 print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
7459 /* Dump a loop. */
7461 DEBUG_FUNCTION void
7462 debug (struct loop &ref)
7464 print_loop (stderr, &ref, 0, /*verbosity*/0);
7467 DEBUG_FUNCTION void
7468 debug (struct loop *ptr)
7470 if (ptr)
7471 debug (*ptr);
7472 else
7473 fprintf (stderr, "<nil>\n");
7476 /* Dump a loop verbosely. */
7478 DEBUG_FUNCTION void
7479 debug_verbose (struct loop &ref)
7481 print_loop (stderr, &ref, 0, /*verbosity*/3);
7484 DEBUG_FUNCTION void
7485 debug_verbose (struct loop *ptr)
7487 if (ptr)
7488 debug (*ptr);
7489 else
7490 fprintf (stderr, "<nil>\n");
7494 /* Debugging loops structure at tree level, at some VERBOSITY level. */
7496 DEBUG_FUNCTION void
7497 debug_loops (int verbosity)
7499 print_loops (stderr, verbosity);
7502 /* Print on stderr the code of LOOP, at some VERBOSITY level. */
7504 DEBUG_FUNCTION void
7505 debug_loop (struct loop *loop, int verbosity)
7507 print_loop (stderr, loop, 0, verbosity);
7510 /* Print on stderr the code of loop number NUM, at some VERBOSITY
7511 level. */
7513 DEBUG_FUNCTION void
7514 debug_loop_num (unsigned num, int verbosity)
7516 debug_loop (get_loop (cfun, num), verbosity);
7519 /* Return true if BB ends with a call, possibly followed by some
7520 instructions that must stay with the call. Return false,
7521 otherwise. */
7523 static bool
7524 gimple_block_ends_with_call_p (basic_block bb)
7526 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7527 return !gsi_end_p (gsi) && is_gimple_call (gsi_stmt (gsi));
7531 /* Return true if BB ends with a conditional branch. Return false,
7532 otherwise. */
7534 static bool
7535 gimple_block_ends_with_condjump_p (const_basic_block bb)
7537 gimple stmt = last_stmt (CONST_CAST_BB (bb));
7538 return (stmt && gimple_code (stmt) == GIMPLE_COND);
7542 /* Return true if we need to add fake edge to exit at statement T.
7543 Helper function for gimple_flow_call_edges_add. */
7545 static bool
7546 need_fake_edge_p (gimple t)
7548 tree fndecl = NULL_TREE;
7549 int call_flags = 0;
7551 /* NORETURN and LONGJMP calls already have an edge to exit.
7552 CONST and PURE calls do not need one.
7553 We don't currently check for CONST and PURE here, although
7554 it would be a good idea, because those attributes are
7555 figured out from the RTL in mark_constant_function, and
7556 the counter incrementation code from -fprofile-arcs
7557 leads to different results from -fbranch-probabilities. */
7558 if (is_gimple_call (t))
7560 fndecl = gimple_call_fndecl (t);
7561 call_flags = gimple_call_flags (t);
7564 if (is_gimple_call (t)
7565 && fndecl
7566 && DECL_BUILT_IN (fndecl)
7567 && (call_flags & ECF_NOTHROW)
7568 && !(call_flags & ECF_RETURNS_TWICE)
7569 /* fork() doesn't really return twice, but the effect of
7570 wrapping it in __gcov_fork() which calls __gcov_flush()
7571 and clears the counters before forking has the same
7572 effect as returning twice. Force a fake edge. */
7573 && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7574 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
7575 return false;
7577 if (is_gimple_call (t))
7579 edge_iterator ei;
7580 edge e;
7581 basic_block bb;
7583 if (!(call_flags & ECF_NORETURN))
7584 return true;
7586 bb = gimple_bb (t);
7587 FOR_EACH_EDGE (e, ei, bb->succs)
7588 if ((e->flags & EDGE_FAKE) == 0)
7589 return true;
7592 if (gimple_code (t) == GIMPLE_ASM
7593 && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
7594 return true;
7596 return false;
7600 /* Add fake edges to the function exit for any non constant and non
7601 noreturn calls (or noreturn calls with EH/abnormal edges),
7602 volatile inline assembly in the bitmap of blocks specified by BLOCKS
7603 or to the whole CFG if BLOCKS is zero. Return the number of blocks
7604 that were split.
7606 The goal is to expose cases in which entering a basic block does
7607 not imply that all subsequent instructions must be executed. */
7609 static int
7610 gimple_flow_call_edges_add (sbitmap blocks)
7612 int i;
7613 int blocks_split = 0;
7614 int last_bb = last_basic_block_for_fn (cfun);
7615 bool check_last_block = false;
7617 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
7618 return 0;
7620 if (! blocks)
7621 check_last_block = true;
7622 else
7623 check_last_block = bitmap_bit_p (blocks,
7624 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
7626 /* In the last basic block, before epilogue generation, there will be
7627 a fallthru edge to EXIT. Special care is required if the last insn
7628 of the last basic block is a call because make_edge folds duplicate
7629 edges, which would result in the fallthru edge also being marked
7630 fake, which would result in the fallthru edge being removed by
7631 remove_fake_edges, which would result in an invalid CFG.
7633 Moreover, we can't elide the outgoing fake edge, since the block
7634 profiler needs to take this into account in order to solve the minimal
7635 spanning tree in the case that the call doesn't return.
7637 Handle this by adding a dummy instruction in a new last basic block. */
7638 if (check_last_block)
7640 basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
7641 gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
7642 gimple t = NULL;
7644 if (!gsi_end_p (gsi))
7645 t = gsi_stmt (gsi);
7647 if (t && need_fake_edge_p (t))
7649 edge e;
7651 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
7652 if (e)
7654 gsi_insert_on_edge (e, gimple_build_nop ());
7655 gsi_commit_edge_inserts ();
7660 /* Now add fake edges to the function exit for any non constant
7661 calls since there is no way that we can determine if they will
7662 return or not... */
7663 for (i = 0; i < last_bb; i++)
7665 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
7666 gimple_stmt_iterator gsi;
7667 gimple stmt, last_stmt;
7669 if (!bb)
7670 continue;
7672 if (blocks && !bitmap_bit_p (blocks, i))
7673 continue;
7675 gsi = gsi_last_nondebug_bb (bb);
7676 if (!gsi_end_p (gsi))
7678 last_stmt = gsi_stmt (gsi);
7681 stmt = gsi_stmt (gsi);
7682 if (need_fake_edge_p (stmt))
7684 edge e;
7686 /* The handling above of the final block before the
7687 epilogue should be enough to verify that there is
7688 no edge to the exit block in CFG already.
7689 Calling make_edge in such case would cause us to
7690 mark that edge as fake and remove it later. */
7691 #ifdef ENABLE_CHECKING
7692 if (stmt == last_stmt)
7694 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
7695 gcc_assert (e == NULL);
7697 #endif
7699 /* Note that the following may create a new basic block
7700 and renumber the existing basic blocks. */
7701 if (stmt != last_stmt)
7703 e = split_block (bb, stmt);
7704 if (e)
7705 blocks_split++;
7707 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
7709 gsi_prev (&gsi);
7711 while (!gsi_end_p (gsi));
7715 if (blocks_split)
7716 verify_flow_info ();
7718 return blocks_split;
7721 /* Removes edge E and all the blocks dominated by it, and updates dominance
7722 information. The IL in E->src needs to be updated separately.
7723 If dominance info is not available, only the edge E is removed.*/
7725 void
7726 remove_edge_and_dominated_blocks (edge e)
7728 vec<basic_block> bbs_to_remove = vNULL;
7729 vec<basic_block> bbs_to_fix_dom = vNULL;
7730 bitmap df, df_idom;
7731 edge f;
7732 edge_iterator ei;
7733 bool none_removed = false;
7734 unsigned i;
7735 basic_block bb, dbb;
7736 bitmap_iterator bi;
7738 if (!dom_info_available_p (CDI_DOMINATORS))
7740 remove_edge (e);
7741 return;
7744 /* No updating is needed for edges to exit. */
7745 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
7747 if (cfgcleanup_altered_bbs)
7748 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7749 remove_edge (e);
7750 return;
7753 /* First, we find the basic blocks to remove. If E->dest has a predecessor
7754 that is not dominated by E->dest, then this set is empty. Otherwise,
7755 all the basic blocks dominated by E->dest are removed.
7757 Also, to DF_IDOM we store the immediate dominators of the blocks in
7758 the dominance frontier of E (i.e., of the successors of the
7759 removed blocks, if there are any, and of E->dest otherwise). */
7760 FOR_EACH_EDGE (f, ei, e->dest->preds)
7762 if (f == e)
7763 continue;
7765 if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
7767 none_removed = true;
7768 break;
7772 df = BITMAP_ALLOC (NULL);
7773 df_idom = BITMAP_ALLOC (NULL);
7775 if (none_removed)
7776 bitmap_set_bit (df_idom,
7777 get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
7778 else
7780 bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
7781 FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
7783 FOR_EACH_EDGE (f, ei, bb->succs)
7785 if (f->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
7786 bitmap_set_bit (df, f->dest->index);
7789 FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
7790 bitmap_clear_bit (df, bb->index);
7792 EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
7794 bb = BASIC_BLOCK_FOR_FN (cfun, i);
7795 bitmap_set_bit (df_idom,
7796 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
7800 if (cfgcleanup_altered_bbs)
7802 /* Record the set of the altered basic blocks. */
7803 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
7804 bitmap_ior_into (cfgcleanup_altered_bbs, df);
7807 /* Remove E and the cancelled blocks. */
7808 if (none_removed)
7809 remove_edge (e);
7810 else
7812 /* Walk backwards so as to get a chance to substitute all
7813 released DEFs into debug stmts. See
7814 eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
7815 details. */
7816 for (i = bbs_to_remove.length (); i-- > 0; )
7817 delete_basic_block (bbs_to_remove[i]);
7820 /* Update the dominance information. The immediate dominator may change only
7821 for blocks whose immediate dominator belongs to DF_IDOM:
7823 Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
7824 removal. Let Z the arbitrary block such that idom(Z) = Y and
7825 Z dominates X after the removal. Before removal, there exists a path P
7826 from Y to X that avoids Z. Let F be the last edge on P that is
7827 removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
7828 dominates W, and because of P, Z does not dominate W), and W belongs to
7829 the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
7830 EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
7832 bb = BASIC_BLOCK_FOR_FN (cfun, i);
7833 for (dbb = first_dom_son (CDI_DOMINATORS, bb);
7834 dbb;
7835 dbb = next_dom_son (CDI_DOMINATORS, dbb))
7836 bbs_to_fix_dom.safe_push (dbb);
7839 iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
7841 BITMAP_FREE (df);
7842 BITMAP_FREE (df_idom);
7843 bbs_to_remove.release ();
7844 bbs_to_fix_dom.release ();
7847 /* Purge dead EH edges from basic block BB. */
7849 bool
7850 gimple_purge_dead_eh_edges (basic_block bb)
7852 bool changed = false;
7853 edge e;
7854 edge_iterator ei;
7855 gimple stmt = last_stmt (bb);
7857 if (stmt && stmt_can_throw_internal (stmt))
7858 return false;
7860 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7862 if (e->flags & EDGE_EH)
7864 remove_edge_and_dominated_blocks (e);
7865 changed = true;
7867 else
7868 ei_next (&ei);
7871 return changed;
7874 /* Purge dead EH edges from basic block listed in BLOCKS. */
7876 bool
7877 gimple_purge_all_dead_eh_edges (const_bitmap blocks)
7879 bool changed = false;
7880 unsigned i;
7881 bitmap_iterator bi;
7883 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7885 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
7887 /* Earlier gimple_purge_dead_eh_edges could have removed
7888 this basic block already. */
7889 gcc_assert (bb || changed);
7890 if (bb != NULL)
7891 changed |= gimple_purge_dead_eh_edges (bb);
7894 return changed;
7897 /* Purge dead abnormal call edges from basic block BB. */
7899 bool
7900 gimple_purge_dead_abnormal_call_edges (basic_block bb)
7902 bool changed = false;
7903 edge e;
7904 edge_iterator ei;
7905 gimple stmt = last_stmt (bb);
7907 if (!cfun->has_nonlocal_label
7908 && !cfun->calls_setjmp)
7909 return false;
7911 if (stmt && stmt_can_make_abnormal_goto (stmt))
7912 return false;
7914 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
7916 if (e->flags & EDGE_ABNORMAL)
7918 if (e->flags & EDGE_FALLTHRU)
7919 e->flags &= ~EDGE_ABNORMAL;
7920 else
7921 remove_edge_and_dominated_blocks (e);
7922 changed = true;
7924 else
7925 ei_next (&ei);
7928 return changed;
7931 /* Purge dead abnormal call edges from basic block listed in BLOCKS. */
7933 bool
7934 gimple_purge_all_dead_abnormal_call_edges (const_bitmap blocks)
7936 bool changed = false;
7937 unsigned i;
7938 bitmap_iterator bi;
7940 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
7942 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
7944 /* Earlier gimple_purge_dead_abnormal_call_edges could have removed
7945 this basic block already. */
7946 gcc_assert (bb || changed);
7947 if (bb != NULL)
7948 changed |= gimple_purge_dead_abnormal_call_edges (bb);
7951 return changed;
7954 /* This function is called whenever a new edge is created or
7955 redirected. */
7957 static void
7958 gimple_execute_on_growing_pred (edge e)
7960 basic_block bb = e->dest;
7962 if (!gimple_seq_empty_p (phi_nodes (bb)))
7963 reserve_phi_args_for_new_edge (bb);
7966 /* This function is called immediately before edge E is removed from
7967 the edge vector E->dest->preds. */
7969 static void
7970 gimple_execute_on_shrinking_pred (edge e)
7972 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
7973 remove_phi_args (e);
7976 /*---------------------------------------------------------------------------
7977 Helper functions for Loop versioning
7978 ---------------------------------------------------------------------------*/
7980 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
7981 of 'first'. Both of them are dominated by 'new_head' basic block. When
7982 'new_head' was created by 'second's incoming edge it received phi arguments
7983 on the edge by split_edge(). Later, additional edge 'e' was created to
7984 connect 'new_head' and 'first'. Now this routine adds phi args on this
7985 additional edge 'e' that new_head to second edge received as part of edge
7986 splitting. */
7988 static void
7989 gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
7990 basic_block new_head, edge e)
7992 gimple phi1, phi2;
7993 gimple_stmt_iterator psi1, psi2;
7994 tree def;
7995 edge e2 = find_edge (new_head, second);
7997 /* Because NEW_HEAD has been created by splitting SECOND's incoming
7998 edge, we should always have an edge from NEW_HEAD to SECOND. */
7999 gcc_assert (e2 != NULL);
8001 /* Browse all 'second' basic block phi nodes and add phi args to
8002 edge 'e' for 'first' head. PHI args are always in correct order. */
8004 for (psi2 = gsi_start_phis (second),
8005 psi1 = gsi_start_phis (first);
8006 !gsi_end_p (psi2) && !gsi_end_p (psi1);
8007 gsi_next (&psi2), gsi_next (&psi1))
8009 phi1 = gsi_stmt (psi1);
8010 phi2 = gsi_stmt (psi2);
8011 def = PHI_ARG_DEF (phi2, e2->dest_idx);
8012 add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
8017 /* Adds a if else statement to COND_BB with condition COND_EXPR.
8018 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
8019 the destination of the ELSE part. */
8021 static void
8022 gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
8023 basic_block second_head ATTRIBUTE_UNUSED,
8024 basic_block cond_bb, void *cond_e)
8026 gimple_stmt_iterator gsi;
8027 gimple new_cond_expr;
8028 tree cond_expr = (tree) cond_e;
8029 edge e0;
8031 /* Build new conditional expr */
8032 new_cond_expr = gimple_build_cond_from_tree (cond_expr,
8033 NULL_TREE, NULL_TREE);
8035 /* Add new cond in cond_bb. */
8036 gsi = gsi_last_bb (cond_bb);
8037 gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
8039 /* Adjust edges appropriately to connect new head with first head
8040 as well as second head. */
8041 e0 = single_succ_edge (cond_bb);
8042 e0->flags &= ~EDGE_FALLTHRU;
8043 e0->flags |= EDGE_FALSE_VALUE;
8047 /* Do book-keeping of basic block BB for the profile consistency checker.
8048 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
8049 then do post-pass accounting. Store the counting in RECORD. */
8050 static void
8051 gimple_account_profile_record (basic_block bb, int after_pass,
8052 struct profile_record *record)
8054 gimple_stmt_iterator i;
8055 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
8057 record->size[after_pass]
8058 += estimate_num_insns (gsi_stmt (i), &eni_size_weights);
8059 if (profile_status_for_fn (cfun) == PROFILE_READ)
8060 record->time[after_pass]
8061 += estimate_num_insns (gsi_stmt (i),
8062 &eni_time_weights) * bb->count;
8063 else if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
8064 record->time[after_pass]
8065 += estimate_num_insns (gsi_stmt (i),
8066 &eni_time_weights) * bb->frequency;
8070 struct cfg_hooks gimple_cfg_hooks = {
8071 "gimple",
8072 gimple_verify_flow_info,
8073 gimple_dump_bb, /* dump_bb */
8074 gimple_dump_bb_for_graph, /* dump_bb_for_graph */
8075 create_bb, /* create_basic_block */
8076 gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
8077 gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
8078 gimple_can_remove_branch_p, /* can_remove_branch_p */
8079 remove_bb, /* delete_basic_block */
8080 gimple_split_block, /* split_block */
8081 gimple_move_block_after, /* move_block_after */
8082 gimple_can_merge_blocks_p, /* can_merge_blocks_p */
8083 gimple_merge_blocks, /* merge_blocks */
8084 gimple_predict_edge, /* predict_edge */
8085 gimple_predicted_by_p, /* predicted_by_p */
8086 gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
8087 gimple_duplicate_bb, /* duplicate_block */
8088 gimple_split_edge, /* split_edge */
8089 gimple_make_forwarder_block, /* make_forward_block */
8090 NULL, /* tidy_fallthru_edge */
8091 NULL, /* force_nonfallthru */
8092 gimple_block_ends_with_call_p,/* block_ends_with_call_p */
8093 gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
8094 gimple_flow_call_edges_add, /* flow_call_edges_add */
8095 gimple_execute_on_growing_pred, /* execute_on_growing_pred */
8096 gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
8097 gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
8098 gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
8099 gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
8100 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
8101 flush_pending_stmts, /* flush_pending_stmts */
8102 gimple_empty_block_p, /* block_empty_p */
8103 gimple_split_block_before_cond_jump, /* split_block_before_cond_jump */
8104 gimple_account_profile_record,
8108 /* Split all critical edges. */
8110 unsigned int
8111 split_critical_edges (void)
8113 basic_block bb;
8114 edge e;
8115 edge_iterator ei;
8117 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
8118 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
8119 mappings around the calls to split_edge. */
8120 start_recording_case_labels ();
8121 FOR_ALL_BB_FN (bb, cfun)
8123 FOR_EACH_EDGE (e, ei, bb->succs)
8125 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
8126 split_edge (e);
8127 /* PRE inserts statements to edges and expects that
8128 since split_critical_edges was done beforehand, committing edge
8129 insertions will not split more edges. In addition to critical
8130 edges we must split edges that have multiple successors and
8131 end by control flow statements, such as RESX.
8132 Go ahead and split them too. This matches the logic in
8133 gimple_find_edge_insert_loc. */
8134 else if ((!single_pred_p (e->dest)
8135 || !gimple_seq_empty_p (phi_nodes (e->dest))
8136 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
8137 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
8138 && !(e->flags & EDGE_ABNORMAL))
8140 gimple_stmt_iterator gsi;
8142 gsi = gsi_last_bb (e->src);
8143 if (!gsi_end_p (gsi)
8144 && stmt_ends_bb_p (gsi_stmt (gsi))
8145 && (gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN
8146 && !gimple_call_builtin_p (gsi_stmt (gsi),
8147 BUILT_IN_RETURN)))
8148 split_edge (e);
8152 end_recording_case_labels ();
8153 return 0;
8156 namespace {
8158 const pass_data pass_data_split_crit_edges =
8160 GIMPLE_PASS, /* type */
8161 "crited", /* name */
8162 OPTGROUP_NONE, /* optinfo_flags */
8163 TV_TREE_SPLIT_EDGES, /* tv_id */
8164 PROP_cfg, /* properties_required */
8165 PROP_no_crit_edges, /* properties_provided */
8166 0, /* properties_destroyed */
8167 0, /* todo_flags_start */
8168 0, /* todo_flags_finish */
8171 class pass_split_crit_edges : public gimple_opt_pass
8173 public:
8174 pass_split_crit_edges (gcc::context *ctxt)
8175 : gimple_opt_pass (pass_data_split_crit_edges, ctxt)
8178 /* opt_pass methods: */
8179 virtual unsigned int execute (function *) { return split_critical_edges (); }
8181 opt_pass * clone () { return new pass_split_crit_edges (m_ctxt); }
8182 }; // class pass_split_crit_edges
8184 } // anon namespace
8186 gimple_opt_pass *
8187 make_pass_split_crit_edges (gcc::context *ctxt)
8189 return new pass_split_crit_edges (ctxt);
8193 /* Build a ternary operation and gimplify it. Emit code before GSI.
8194 Return the gimple_val holding the result. */
8196 tree
8197 gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
8198 tree type, tree a, tree b, tree c)
8200 tree ret;
8201 location_t loc = gimple_location (gsi_stmt (*gsi));
8203 ret = fold_build3_loc (loc, code, type, a, b, c);
8204 STRIP_NOPS (ret);
8206 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
8207 GSI_SAME_STMT);
8210 /* Build a binary operation and gimplify it. Emit code before GSI.
8211 Return the gimple_val holding the result. */
8213 tree
8214 gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
8215 tree type, tree a, tree b)
8217 tree ret;
8219 ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
8220 STRIP_NOPS (ret);
8222 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
8223 GSI_SAME_STMT);
8226 /* Build a unary operation and gimplify it. Emit code before GSI.
8227 Return the gimple_val holding the result. */
8229 tree
8230 gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
8231 tree a)
8233 tree ret;
8235 ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
8236 STRIP_NOPS (ret);
8238 return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
8239 GSI_SAME_STMT);
8244 /* Given a basic block B which ends with a conditional and has
8245 precisely two successors, determine which of the edges is taken if
8246 the conditional is true and which is taken if the conditional is
8247 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
8249 void
8250 extract_true_false_edges_from_block (basic_block b,
8251 edge *true_edge,
8252 edge *false_edge)
8254 edge e = EDGE_SUCC (b, 0);
8256 if (e->flags & EDGE_TRUE_VALUE)
8258 *true_edge = e;
8259 *false_edge = EDGE_SUCC (b, 1);
8261 else
8263 *false_edge = e;
8264 *true_edge = EDGE_SUCC (b, 1);
8268 /* Emit return warnings. */
8270 namespace {
8272 const pass_data pass_data_warn_function_return =
8274 GIMPLE_PASS, /* type */
8275 "*warn_function_return", /* name */
8276 OPTGROUP_NONE, /* optinfo_flags */
8277 TV_NONE, /* tv_id */
8278 PROP_cfg, /* properties_required */
8279 0, /* properties_provided */
8280 0, /* properties_destroyed */
8281 0, /* todo_flags_start */
8282 0, /* todo_flags_finish */
8285 class pass_warn_function_return : public gimple_opt_pass
8287 public:
8288 pass_warn_function_return (gcc::context *ctxt)
8289 : gimple_opt_pass (pass_data_warn_function_return, ctxt)
8292 /* opt_pass methods: */
8293 virtual unsigned int execute (function *);
8295 }; // class pass_warn_function_return
8297 unsigned int
8298 pass_warn_function_return::execute (function *fun)
8300 source_location location;
8301 gimple last;
8302 edge e;
8303 edge_iterator ei;
8305 if (!targetm.warn_func_return (fun->decl))
8306 return 0;
8308 /* If we have a path to EXIT, then we do return. */
8309 if (TREE_THIS_VOLATILE (fun->decl)
8310 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0)
8312 location = UNKNOWN_LOCATION;
8313 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
8315 last = last_stmt (e->src);
8316 if ((gimple_code (last) == GIMPLE_RETURN
8317 || gimple_call_builtin_p (last, BUILT_IN_RETURN))
8318 && (location = gimple_location (last)) != UNKNOWN_LOCATION)
8319 break;
8321 if (location == UNKNOWN_LOCATION)
8322 location = cfun->function_end_locus;
8323 warning_at (location, 0, "%<noreturn%> function does return");
8326 /* If we see "return;" in some basic block, then we do reach the end
8327 without returning a value. */
8328 else if (warn_return_type
8329 && !TREE_NO_WARNING (fun->decl)
8330 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0
8331 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fun->decl))))
8333 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
8335 gimple last = last_stmt (e->src);
8336 if (gimple_code (last) == GIMPLE_RETURN
8337 && gimple_return_retval (last) == NULL
8338 && !gimple_no_warning_p (last))
8340 location = gimple_location (last);
8341 if (location == UNKNOWN_LOCATION)
8342 location = fun->function_end_locus;
8343 warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
8344 TREE_NO_WARNING (fun->decl) = 1;
8345 break;
8349 return 0;
8352 } // anon namespace
8354 gimple_opt_pass *
8355 make_pass_warn_function_return (gcc::context *ctxt)
8357 return new pass_warn_function_return (ctxt);
8360 /* Walk a gimplified function and warn for functions whose return value is
8361 ignored and attribute((warn_unused_result)) is set. This is done before
8362 inlining, so we don't have to worry about that. */
8364 static void
8365 do_warn_unused_result (gimple_seq seq)
8367 tree fdecl, ftype;
8368 gimple_stmt_iterator i;
8370 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
8372 gimple g = gsi_stmt (i);
8374 switch (gimple_code (g))
8376 case GIMPLE_BIND:
8377 do_warn_unused_result (gimple_bind_body (g));
8378 break;
8379 case GIMPLE_TRY:
8380 do_warn_unused_result (gimple_try_eval (g));
8381 do_warn_unused_result (gimple_try_cleanup (g));
8382 break;
8383 case GIMPLE_CATCH:
8384 do_warn_unused_result (gimple_catch_handler (g));
8385 break;
8386 case GIMPLE_EH_FILTER:
8387 do_warn_unused_result (gimple_eh_filter_failure (g));
8388 break;
8390 case GIMPLE_CALL:
8391 if (gimple_call_lhs (g))
8392 break;
8393 if (gimple_call_internal_p (g))
8394 break;
8396 /* This is a naked call, as opposed to a GIMPLE_CALL with an
8397 LHS. All calls whose value is ignored should be
8398 represented like this. Look for the attribute. */
8399 fdecl = gimple_call_fndecl (g);
8400 ftype = gimple_call_fntype (g);
8402 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
8404 location_t loc = gimple_location (g);
8406 if (fdecl)
8407 warning_at (loc, OPT_Wunused_result,
8408 "ignoring return value of %qD, "
8409 "declared with attribute warn_unused_result",
8410 fdecl);
8411 else
8412 warning_at (loc, OPT_Wunused_result,
8413 "ignoring return value of function "
8414 "declared with attribute warn_unused_result");
8416 break;
8418 default:
8419 /* Not a container, not a call, or a call whose value is used. */
8420 break;
8425 namespace {
8427 const pass_data pass_data_warn_unused_result =
8429 GIMPLE_PASS, /* type */
8430 "*warn_unused_result", /* name */
8431 OPTGROUP_NONE, /* optinfo_flags */
8432 TV_NONE, /* tv_id */
8433 PROP_gimple_any, /* properties_required */
8434 0, /* properties_provided */
8435 0, /* properties_destroyed */
8436 0, /* todo_flags_start */
8437 0, /* todo_flags_finish */
8440 class pass_warn_unused_result : public gimple_opt_pass
8442 public:
8443 pass_warn_unused_result (gcc::context *ctxt)
8444 : gimple_opt_pass (pass_data_warn_unused_result, ctxt)
8447 /* opt_pass methods: */
8448 virtual bool gate (function *) { return flag_warn_unused_result; }
8449 virtual unsigned int execute (function *)
8451 do_warn_unused_result (gimple_body (current_function_decl));
8452 return 0;
8455 }; // class pass_warn_unused_result
8457 } // anon namespace
8459 gimple_opt_pass *
8460 make_pass_warn_unused_result (gcc::context *ctxt)
8462 return new pass_warn_unused_result (ctxt);
8465 /* IPA passes, compilation of earlier functions or inlining
8466 might have changed some properties, such as marked functions nothrow,
8467 pure, const or noreturn.
8468 Remove redundant edges and basic blocks, and create new ones if necessary.
8470 This pass can't be executed as stand alone pass from pass manager, because
8471 in between inlining and this fixup the verify_flow_info would fail. */
8473 unsigned int
8474 execute_fixup_cfg (void)
8476 basic_block bb;
8477 gimple_stmt_iterator gsi;
8478 int todo = 0;
8479 gcov_type count_scale;
8480 edge e;
8481 edge_iterator ei;
8483 count_scale
8484 = GCOV_COMPUTE_SCALE (cgraph_node::get (current_function_decl)->count,
8485 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
8487 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
8488 cgraph_node::get (current_function_decl)->count;
8489 EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
8490 apply_scale (EXIT_BLOCK_PTR_FOR_FN (cfun)->count,
8491 count_scale);
8493 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
8494 e->count = apply_scale (e->count, count_scale);
8496 FOR_EACH_BB_FN (bb, cfun)
8498 bb->count = apply_scale (bb->count, count_scale);
8499 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
8501 gimple stmt = gsi_stmt (gsi);
8502 tree decl = is_gimple_call (stmt)
8503 ? gimple_call_fndecl (stmt)
8504 : NULL;
8505 if (decl)
8507 int flags = gimple_call_flags (stmt);
8508 if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
8510 if (gimple_purge_dead_abnormal_call_edges (bb))
8511 todo |= TODO_cleanup_cfg;
8513 if (gimple_in_ssa_p (cfun))
8515 todo |= TODO_update_ssa | TODO_cleanup_cfg;
8516 update_stmt (stmt);
8520 if (flags & ECF_NORETURN
8521 && fixup_noreturn_call (stmt))
8522 todo |= TODO_cleanup_cfg;
8525 /* Remove stores to variables we marked write-only.
8526 Keep access when store has side effect, i.e. in case when source
8527 is volatile. */
8528 if (gimple_store_p (stmt)
8529 && !gimple_has_side_effects (stmt))
8531 tree lhs = get_base_address (gimple_get_lhs (stmt));
8533 if (TREE_CODE (lhs) == VAR_DECL
8534 && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
8535 && varpool_node::get (lhs)->writeonly)
8537 unlink_stmt_vdef (stmt);
8538 gsi_remove (&gsi, true);
8539 release_defs (stmt);
8540 todo |= TODO_update_ssa | TODO_cleanup_cfg;
8541 continue;
8544 /* For calls we can simply remove LHS when it is known
8545 to be write-only. */
8546 if (is_gimple_call (stmt)
8547 && gimple_get_lhs (stmt))
8549 tree lhs = get_base_address (gimple_get_lhs (stmt));
8551 if (TREE_CODE (lhs) == VAR_DECL
8552 && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
8553 && varpool_node::get (lhs)->writeonly)
8555 gimple_call_set_lhs (stmt, NULL);
8556 update_stmt (stmt);
8557 todo |= TODO_update_ssa | TODO_cleanup_cfg;
8561 if (maybe_clean_eh_stmt (stmt)
8562 && gimple_purge_dead_eh_edges (bb))
8563 todo |= TODO_cleanup_cfg;
8564 gsi_next (&gsi);
8567 FOR_EACH_EDGE (e, ei, bb->succs)
8568 e->count = apply_scale (e->count, count_scale);
8570 /* If we have a basic block with no successors that does not
8571 end with a control statement or a noreturn call end it with
8572 a call to __builtin_unreachable. This situation can occur
8573 when inlining a noreturn call that does in fact return. */
8574 if (EDGE_COUNT (bb->succs) == 0)
8576 gimple stmt = last_stmt (bb);
8577 if (!stmt
8578 || (!is_ctrl_stmt (stmt)
8579 && (!is_gimple_call (stmt)
8580 || (gimple_call_flags (stmt) & ECF_NORETURN) == 0)))
8582 if (stmt && is_gimple_call (stmt))
8583 gimple_call_set_ctrl_altering (stmt, false);
8584 stmt = gimple_build_call
8585 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
8586 gimple_stmt_iterator gsi = gsi_last_bb (bb);
8587 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
8591 if (count_scale != REG_BR_PROB_BASE)
8592 compute_function_frequency ();
8594 /* Dump a textual representation of the flowgraph. */
8595 if (dump_file)
8596 gimple_dump_cfg (dump_file, dump_flags);
8598 if (current_loops
8599 && (todo & TODO_cleanup_cfg))
8600 loops_state_set (LOOPS_NEED_FIXUP);
8602 return todo;
8605 namespace {
8607 const pass_data pass_data_fixup_cfg =
8609 GIMPLE_PASS, /* type */
8610 "*free_cfg_annotations", /* name */
8611 OPTGROUP_NONE, /* optinfo_flags */
8612 TV_NONE, /* tv_id */
8613 PROP_cfg, /* properties_required */
8614 0, /* properties_provided */
8615 0, /* properties_destroyed */
8616 0, /* todo_flags_start */
8617 0, /* todo_flags_finish */
8620 class pass_fixup_cfg : public gimple_opt_pass
8622 public:
8623 pass_fixup_cfg (gcc::context *ctxt)
8624 : gimple_opt_pass (pass_data_fixup_cfg, ctxt)
8627 /* opt_pass methods: */
8628 opt_pass * clone () { return new pass_fixup_cfg (m_ctxt); }
8629 virtual unsigned int execute (function *) { return execute_fixup_cfg (); }
8631 }; // class pass_fixup_cfg
8633 } // anon namespace
8635 gimple_opt_pass *
8636 make_pass_fixup_cfg (gcc::context *ctxt)
8638 return new pass_fixup_cfg (ctxt);
8641 /* Garbage collection support for edge_def. */
8643 extern void gt_ggc_mx (tree&);
8644 extern void gt_ggc_mx (gimple&);
8645 extern void gt_ggc_mx (rtx&);
8646 extern void gt_ggc_mx (basic_block&);
8648 static void
8649 gt_ggc_mx (rtx_insn *& x)
8651 if (x)
8652 gt_ggc_mx_rtx_def ((void *) x);
8655 void
8656 gt_ggc_mx (edge_def *e)
8658 tree block = LOCATION_BLOCK (e->goto_locus);
8659 gt_ggc_mx (e->src);
8660 gt_ggc_mx (e->dest);
8661 if (current_ir_type () == IR_GIMPLE)
8662 gt_ggc_mx (e->insns.g);
8663 else
8664 gt_ggc_mx (e->insns.r);
8665 gt_ggc_mx (block);
8668 /* PCH support for edge_def. */
8670 extern void gt_pch_nx (tree&);
8671 extern void gt_pch_nx (gimple&);
8672 extern void gt_pch_nx (rtx&);
8673 extern void gt_pch_nx (basic_block&);
8675 static void
8676 gt_pch_nx (rtx_insn *& x)
8678 if (x)
8679 gt_pch_nx_rtx_def ((void *) x);
8682 void
8683 gt_pch_nx (edge_def *e)
8685 tree block = LOCATION_BLOCK (e->goto_locus);
8686 gt_pch_nx (e->src);
8687 gt_pch_nx (e->dest);
8688 if (current_ir_type () == IR_GIMPLE)
8689 gt_pch_nx (e->insns.g);
8690 else
8691 gt_pch_nx (e->insns.r);
8692 gt_pch_nx (block);
8695 void
8696 gt_pch_nx (edge_def *e, gt_pointer_operator op, void *cookie)
8698 tree block = LOCATION_BLOCK (e->goto_locus);
8699 op (&(e->src), cookie);
8700 op (&(e->dest), cookie);
8701 if (current_ir_type () == IR_GIMPLE)
8702 op (&(e->insns.g), cookie);
8703 else
8704 op (&(e->insns.r), cookie);
8705 op (&(block), cookie);