Line data Source code
1 : /* Control flow functions for trees.
2 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
3 : Contributed by Diego Novillo <dnovillo@redhat.com>
4 :
5 : This file is part of GCC.
6 :
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.
11 :
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.
16 :
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/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "target.h"
26 : #include "rtl.h"
27 : #include "tree.h"
28 : #include "gimple.h"
29 : #include "cfghooks.h"
30 : #include "tree-pass.h"
31 : #include "ssa.h"
32 : #include "cgraph.h"
33 : #include "gimple-pretty-print.h"
34 : #include "diagnostic-core.h"
35 : #include "fold-const.h"
36 : #include "trans-mem.h"
37 : #include "stor-layout.h"
38 : #include "print-tree.h"
39 : #include "cfganal.h"
40 : #include "gimple-iterator.h"
41 : #include "gimple-fold.h"
42 : #include "tree-eh.h"
43 : #include "gimplify-me.h"
44 : #include "gimple-walk.h"
45 : #include "tree-cfg.h"
46 : #include "tree-ssa-loop-manip.h"
47 : #include "tree-ssa-loop-niter.h"
48 : #include "tree-into-ssa.h"
49 : #include "tree-dfa.h"
50 : #include "tree-ssa.h"
51 : #include "except.h"
52 : #include "cfgloop.h"
53 : #include "tree-ssa-propagate.h"
54 : #include "value-prof.h"
55 : #include "tree-inline.h"
56 : #include "tree-ssa-live.h"
57 : #include "tree-ssa-dce.h"
58 : #include "omp-general.h"
59 : #include "omp-expand.h"
60 : #include "tree-cfgcleanup.h"
61 : #include "gimplify.h"
62 : #include "internal-fn.h"
63 : #include "attribs.h"
64 : #include "selftest.h"
65 : #include "opts.h"
66 : #include "asan.h"
67 : #include "profile.h"
68 : #include "sreal.h"
69 : #include "gcc-urlifier.h"
70 :
71 : /* This file contains functions for building the Control Flow Graph (CFG)
72 : for a function tree. */
73 :
74 : /* Local declarations. */
75 :
76 : /* Initial capacity for the basic block array. */
77 : static const int initial_cfg_capacity = 20;
78 :
79 : /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
80 : which use a particular edge. The CASE_LABEL_EXPRs are chained together
81 : via their CASE_CHAIN field, which we clear after we're done with the
82 : hash table to prevent problems with duplication of GIMPLE_SWITCHes.
83 :
84 : Access to this list of CASE_LABEL_EXPRs allows us to efficiently
85 : update the case vector in response to edge redirections.
86 :
87 : Right now this table is set up and torn down at key points in the
88 : compilation process. It would be nice if we could make the table
89 : more persistent. The key is getting notification of changes to
90 : the CFG (particularly edge removal, creation and redirection). */
91 :
92 : static hash_map<edge, tree> *edge_to_cases;
93 :
94 : /* If we record edge_to_cases, this bitmap will hold indexes
95 : of basic blocks that end in a GIMPLE_SWITCH which we touched
96 : due to edge manipulations. */
97 :
98 : static bitmap touched_switch_bbs;
99 :
100 : /* OpenMP region idxs for blocks during cfg pass. */
101 : static vec<int> bb_to_omp_idx;
102 :
103 : /* CFG statistics. */
104 : struct cfg_stats_d
105 : {
106 : long num_merged_labels;
107 : };
108 :
109 : static struct cfg_stats_d cfg_stats;
110 :
111 : /* Data to pass to replace_block_vars_by_duplicates_1. */
112 : struct replace_decls_d
113 : {
114 : hash_map<tree, tree> *vars_map;
115 : tree to_context;
116 : };
117 :
118 : /* Basic blocks and flowgraphs. */
119 : static void make_blocks (gimple_seq);
120 :
121 : /* Edges. */
122 : static void make_edges (void);
123 : static void assign_discriminators (void);
124 : static void make_cond_expr_edges (basic_block);
125 : static void make_gimple_switch_edges (gswitch *, basic_block);
126 : static bool make_goto_expr_edges (basic_block);
127 : static void make_gimple_asm_edges (basic_block);
128 : static edge gimple_redirect_edge_and_branch (edge, basic_block);
129 : static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
130 :
131 : /* Various helpers. */
132 : static inline bool stmt_starts_bb_p (gimple *, gimple *);
133 : static bool gimple_verify_flow_info (void);
134 : static void gimple_make_forwarder_block (edge);
135 : static bool verify_gimple_transaction (gtransaction *);
136 : static bool call_can_make_abnormal_goto (gimple *);
137 : static bool gimple_empty_block_p (basic_block);
138 :
139 : /* Flowgraph optimization and cleanup. */
140 : static void gimple_merge_blocks (basic_block, basic_block);
141 : static bool gimple_can_merge_blocks_p (basic_block, basic_block);
142 : static void remove_bb (basic_block);
143 : static edge find_taken_edge_computed_goto (basic_block, tree);
144 : static edge find_taken_edge_cond_expr (const gcond *, tree);
145 :
146 : void
147 3427222 : init_empty_tree_cfg_for_function (struct function *fn)
148 : {
149 : /* Initialize the basic block array. */
150 3427222 : init_flow (fn);
151 3427222 : profile_status_for_fn (fn) = PROFILE_ABSENT;
152 3427222 : n_basic_blocks_for_fn (fn) = NUM_FIXED_BLOCKS;
153 3427222 : last_basic_block_for_fn (fn) = NUM_FIXED_BLOCKS;
154 3427222 : vec_safe_grow_cleared (basic_block_info_for_fn (fn),
155 : initial_cfg_capacity, true);
156 :
157 : /* Build a mapping of labels to their associated blocks. */
158 3427222 : vec_safe_grow_cleared (label_to_block_map_for_fn (fn),
159 : initial_cfg_capacity, true);
160 :
161 3427222 : SET_BASIC_BLOCK_FOR_FN (fn, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (fn));
162 3427222 : SET_BASIC_BLOCK_FOR_FN (fn, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (fn));
163 :
164 3427222 : ENTRY_BLOCK_PTR_FOR_FN (fn)->next_bb
165 3427222 : = EXIT_BLOCK_PTR_FOR_FN (fn);
166 3427222 : EXIT_BLOCK_PTR_FOR_FN (fn)->prev_bb
167 3427222 : = ENTRY_BLOCK_PTR_FOR_FN (fn);
168 3427222 : }
169 :
170 : void
171 3340232 : init_empty_tree_cfg (void)
172 : {
173 3340232 : init_empty_tree_cfg_for_function (cfun);
174 3340232 : }
175 :
176 : /*---------------------------------------------------------------------------
177 : Create basic blocks
178 : ---------------------------------------------------------------------------*/
179 :
180 : /* Entry point to the CFG builder for trees. SEQ is the sequence of
181 : statements to be added to the flowgraph. */
182 :
183 : static void
184 3019910 : build_gimple_cfg (gimple_seq seq)
185 : {
186 : /* Register specific gimple functions. */
187 3019910 : gimple_register_cfg_hooks ();
188 :
189 3019910 : memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
190 :
191 3019910 : init_empty_tree_cfg ();
192 :
193 3019910 : make_blocks (seq);
194 :
195 : /* Make sure there is always at least one block, even if it's empty. */
196 3019910 : if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
197 0 : create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
198 :
199 : /* Adjust the size of the array. */
200 3019910 : if (basic_block_info_for_fn (cfun)->length ()
201 3019910 : < (size_t) n_basic_blocks_for_fn (cfun))
202 0 : vec_safe_grow_cleared (basic_block_info_for_fn (cfun),
203 : n_basic_blocks_for_fn (cfun));
204 :
205 : /* To speed up statement iterator walks, we first purge dead labels. */
206 3019910 : cleanup_dead_labels ();
207 :
208 : /* Group case nodes to reduce the number of edges.
209 : We do this after cleaning up dead labels because otherwise we miss
210 : a lot of obvious case merging opportunities. */
211 3019910 : group_case_labels ();
212 :
213 : /* Create the edges of the flowgraph. */
214 3019910 : make_edges ();
215 3019910 : assign_discriminators ();
216 3019910 : cleanup_dead_labels ();
217 3019910 : }
218 :
219 : /* Look for ANNOTATE calls with loop annotation kind in BB; if found, remove
220 : them and propagate the information to LOOP. We assume that the annotations
221 : come immediately before the condition in BB, if any. */
222 :
223 : static void
224 1599315 : replace_loop_annotate_in_block (basic_block bb, class loop *loop)
225 : {
226 1599315 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
227 1599315 : gimple *stmt = gsi_stmt (gsi);
228 :
229 1599315 : if (!(stmt && gimple_code (stmt) == GIMPLE_COND))
230 583772 : return;
231 :
232 1021416 : for (gsi_prev_nondebug (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
233 : {
234 557837 : stmt = gsi_stmt (gsi);
235 557837 : if (gimple_code (stmt) != GIMPLE_CALL)
236 : break;
237 59966 : if (!gimple_call_internal_p (stmt)
238 59966 : || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
239 : break;
240 :
241 5818 : switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
242 : {
243 510 : case annot_expr_ivdep_kind:
244 510 : loop->safelen = INT_MAX;
245 510 : break;
246 2027 : case annot_expr_unroll_kind:
247 2027 : loop->unroll
248 2027 : = (unsigned short) tree_to_shwi (gimple_call_arg (stmt, 2));
249 2027 : cfun->has_unroll = true;
250 2027 : break;
251 3214 : case annot_expr_no_vector_kind:
252 3214 : loop->dont_vectorize = true;
253 3214 : break;
254 3 : case annot_expr_vector_kind:
255 3 : loop->force_vectorize = true;
256 3 : cfun->has_force_vectorize_loops = true;
257 3 : break;
258 0 : case annot_expr_parallel_kind:
259 0 : loop->can_be_parallel = true;
260 0 : loop->safelen = INT_MAX;
261 0 : break;
262 64 : case annot_expr_maybe_infinite_kind:
263 64 : loop->finite_p = false;
264 64 : break;
265 0 : default:
266 0 : gcc_unreachable ();
267 : }
268 :
269 5818 : stmt = gimple_build_assign (gimple_call_lhs (stmt),
270 : gimple_call_arg (stmt, 0));
271 5818 : gsi_replace (&gsi, stmt, true);
272 : }
273 : }
274 :
275 : /* Look for ANNOTATE calls with loop annotation kind; if found, remove
276 : them and propagate the information to the loop. We assume that the
277 : annotations come immediately before the condition of the loop. */
278 :
279 : static void
280 3019910 : replace_loop_annotate (void)
281 : {
282 3019910 : basic_block bb;
283 3019910 : gimple_stmt_iterator gsi;
284 3019910 : gimple *stmt;
285 :
286 9702020 : for (auto loop : loops_list (cfun, 0))
287 : {
288 : /* Push the global flag_finite_loops state down to individual loops. */
289 642290 : loop->finite_p = flag_finite_loops;
290 :
291 : /* Check all exit source blocks for annotations. */
292 3517157 : for (auto e : get_loop_exit_edges (loop))
293 2241605 : replace_loop_annotate_in_block (e->src, loop);
294 3019910 : }
295 :
296 : /* Remove IFN_ANNOTATE. Safeguard for the case loop->latch == NULL. */
297 21313345 : FOR_EACH_BB_FN (bb, cfun)
298 : {
299 123569747 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
300 : {
301 68780253 : stmt = gsi_stmt (gsi);
302 68780253 : if (gimple_code (stmt) != GIMPLE_CALL)
303 58001456 : continue;
304 10778797 : if (!gimple_call_internal_p (stmt)
305 10778797 : || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
306 10778797 : continue;
307 :
308 0 : switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
309 : {
310 0 : case annot_expr_ivdep_kind:
311 0 : case annot_expr_unroll_kind:
312 0 : case annot_expr_no_vector_kind:
313 0 : case annot_expr_vector_kind:
314 0 : case annot_expr_parallel_kind:
315 0 : case annot_expr_maybe_infinite_kind:
316 0 : break;
317 0 : default:
318 0 : gcc_unreachable ();
319 : }
320 :
321 0 : warning_at (gimple_location (stmt), 0, "ignoring loop annotation");
322 0 : stmt = gimple_build_assign (gimple_call_lhs (stmt),
323 : gimple_call_arg (stmt, 0));
324 0 : gsi_replace (&gsi, stmt, true);
325 : }
326 : }
327 3019910 : }
328 :
329 : static unsigned int
330 3019910 : execute_build_cfg (void)
331 : {
332 3019910 : gimple_seq body = gimple_body (current_function_decl);
333 :
334 3019910 : build_gimple_cfg (body);
335 3019910 : gimple_set_body (current_function_decl, NULL);
336 3019910 : if (dump_file && (dump_flags & TDF_DETAILS))
337 : {
338 5 : fprintf (dump_file, "Scope blocks:\n");
339 5 : dump_scope_blocks (dump_file, dump_flags);
340 : }
341 3019910 : cleanup_tree_cfg ();
342 :
343 3019910 : bb_to_omp_idx.release ();
344 :
345 3019910 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
346 3019910 : replace_loop_annotate ();
347 3019910 : return 0;
348 : }
349 :
350 : namespace {
351 :
352 : const pass_data pass_data_build_cfg =
353 : {
354 : GIMPLE_PASS, /* type */
355 : "cfg", /* name */
356 : OPTGROUP_NONE, /* optinfo_flags */
357 : TV_TREE_CFG, /* tv_id */
358 : PROP_gimple_leh, /* properties_required */
359 : ( PROP_cfg | PROP_loops ), /* properties_provided */
360 : 0, /* properties_destroyed */
361 : 0, /* todo_flags_start */
362 : 0, /* todo_flags_finish */
363 : };
364 :
365 : class pass_build_cfg : public gimple_opt_pass
366 : {
367 : public:
368 303001 : pass_build_cfg (gcc::context *ctxt)
369 606002 : : gimple_opt_pass (pass_data_build_cfg, ctxt)
370 : {}
371 :
372 : /* opt_pass methods: */
373 3019910 : unsigned int execute (function *) final override
374 : {
375 3019910 : return execute_build_cfg ();
376 : }
377 :
378 : }; // class pass_build_cfg
379 :
380 : } // anon namespace
381 :
382 : gimple_opt_pass *
383 303001 : make_pass_build_cfg (gcc::context *ctxt)
384 : {
385 303001 : return new pass_build_cfg (ctxt);
386 : }
387 :
388 :
389 : /* Return true if T is a computed goto. */
390 :
391 : bool
392 689208122 : computed_goto_p (gimple *t)
393 : {
394 689208122 : return (gimple_code (t) == GIMPLE_GOTO
395 689208122 : && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
396 : }
397 :
398 : /* Returns true if the sequence of statements STMTS only contains
399 : a call to __builtin_unreachable (). */
400 :
401 : bool
402 3776977 : gimple_seq_unreachable_p (gimple_seq stmts)
403 : {
404 3776977 : if (stmts == NULL
405 : /* Return false if -fsanitize=unreachable, we don't want to
406 : optimize away those calls, but rather turn them into
407 : __ubsan_handle_builtin_unreachable () or __builtin_trap ()
408 : later. */
409 3776977 : || sanitize_flags_p (SANITIZE_UNREACHABLE))
410 : return false;
411 :
412 3774296 : gimple_stmt_iterator gsi = gsi_last (stmts);
413 :
414 3774296 : if (!gimple_call_builtin_p (gsi_stmt (gsi), BUILT_IN_UNREACHABLE))
415 : return false;
416 :
417 1147943 : for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
418 : {
419 6405 : gimple *stmt = gsi_stmt (gsi);
420 6405 : if (gimple_code (stmt) != GIMPLE_LABEL
421 4503 : && !is_gimple_debug (stmt)
422 7495 : && !gimple_clobber_p (stmt))
423 : return false;
424 : }
425 : return true;
426 : }
427 :
428 : /* Returns true for edge E where e->src ends with a GIMPLE_COND and
429 : the other edge points to a bb with just __builtin_unreachable ().
430 : I.e. return true for C->M edge in:
431 : <bb C>:
432 : ...
433 : if (something)
434 : goto <bb N>;
435 : else
436 : goto <bb M>;
437 : <bb N>:
438 : __builtin_unreachable ();
439 : <bb M>: */
440 :
441 : bool
442 12310783 : assert_unreachable_fallthru_edge_p (edge e)
443 : {
444 12310783 : basic_block pred_bb = e->src;
445 24621566 : if (safe_is_a <gcond *> (*gsi_last_bb (pred_bb)))
446 : {
447 12310783 : basic_block other_bb = EDGE_SUCC (pred_bb, 0)->dest;
448 12310783 : if (other_bb == e->dest)
449 6606555 : other_bb = EDGE_SUCC (pred_bb, 1)->dest;
450 12310783 : if (EDGE_COUNT (other_bb->succs) == 0)
451 3787332 : return gimple_seq_unreachable_p (bb_seq (other_bb));
452 : }
453 : return false;
454 : }
455 :
456 :
457 : /* Initialize GF_CALL_CTRL_ALTERING flag, which indicates the call
458 : could alter control flow except via eh. We initialize the flag at
459 : CFG build time and only ever clear it later. */
460 :
461 : static void
462 11471378 : gimple_call_initialize_ctrl_altering (gimple *stmt)
463 : {
464 11471378 : int flags = gimple_call_flags (stmt);
465 :
466 : /* A call alters control flow if it can make an abnormal goto. */
467 11471378 : if (call_can_make_abnormal_goto (stmt)
468 : /* A call also alters control flow if it does not return. */
469 11463950 : || flags & ECF_NORETURN
470 : /* TM ending statements have backedges out of the transaction.
471 : Return true so we split the basic block containing them.
472 : Note that the TM_BUILTIN test is merely an optimization. */
473 9775468 : || ((flags & ECF_TM_BUILTIN)
474 985 : && is_tm_ending_fndecl (gimple_call_fndecl (stmt)))
475 : /* BUILT_IN_RETURN call is same as return statement. */
476 9774490 : || gimple_call_builtin_p (stmt, BUILT_IN_RETURN)
477 : /* IFN_UNIQUE should be the last insn, to make checking for it
478 : as cheap as possible. */
479 21245868 : || (gimple_call_internal_p (stmt)
480 533940 : && gimple_call_internal_unique_p (stmt)))
481 1782681 : gimple_call_set_ctrl_altering (stmt, true);
482 : else
483 9688697 : gimple_call_set_ctrl_altering (stmt, false);
484 11471378 : }
485 :
486 :
487 : /* Insert SEQ after BB and build a flowgraph. */
488 :
489 : static basic_block
490 3072312 : make_blocks_1 (gimple_seq seq, basic_block bb)
491 : {
492 3072312 : gimple_stmt_iterator i = gsi_start (seq);
493 3072312 : gimple *stmt = NULL;
494 3072312 : gimple *prev_stmt = NULL;
495 3072312 : bool start_new_block = true;
496 3072312 : bool first_stmt_of_seq = true;
497 3072312 : bool suppress_coverage_p = false;
498 :
499 97629472 : while (!gsi_end_p (i))
500 : {
501 : /* PREV_STMT should only be set to a debug stmt if the debug
502 : stmt is before nondebug stmts. Once stmt reaches a nondebug
503 : nonlabel, prev_stmt will be set to it, so that
504 : stmt_starts_bb_p will know to start a new block if a label is
505 : found. However, if stmt was a label after debug stmts only,
506 : keep the label in prev_stmt even if we find further debug
507 : stmts, for there may be other labels after them, and they
508 : should land in the same block. */
509 94557160 : if (!prev_stmt || !stmt || !is_gimple_debug (stmt))
510 : prev_stmt = stmt;
511 94557160 : stmt = gsi_stmt (i);
512 :
513 94557160 : if (stmt && is_gimple_call (stmt))
514 11471378 : gimple_call_initialize_ctrl_altering (stmt);
515 :
516 94557160 : if (!is_gimple_debug (stmt))
517 92073631 : suppress_coverage_p
518 92073631 : = in_pragma_suppress_coverage_p (stmt, suppress_coverage_p);
519 94557160 : if (suppress_coverage_p && !coverage_suppressed_p (bb))
520 : start_new_block = true;
521 94556792 : else if (!suppress_coverage_p && coverage_suppressed_p (bb))
522 : start_new_block = true;
523 :
524 : /* If the statement starts a new basic block or if we have determined
525 : in a previous pass that we need to create a new block for STMT, do
526 : so now. */
527 94556456 : if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
528 : {
529 22576825 : if (!first_stmt_of_seq)
530 19504513 : gsi_split_seq_before (&i, &seq);
531 22576825 : bb = create_basic_block (seq, bb);
532 :
533 22576825 : start_new_block = false;
534 22576825 : prev_stmt = NULL;
535 : }
536 :
537 : /* Now add STMT to BB and create the subgraphs for special statement
538 : codes. */
539 94557160 : gimple_set_bb (stmt, bb);
540 :
541 94557160 : if (suppress_coverage_p)
542 2103 : suppress_coverage (bb);
543 :
544 : /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
545 : next iteration. */
546 94557160 : if (stmt_ends_bb_p (stmt))
547 : {
548 : /* If the stmt can make abnormal goto use a new temporary
549 : for the assignment to the LHS. This makes sure the old value
550 : of the LHS is available on the abnormal edge. Otherwise
551 : we will end up with overlapping life-ranges for abnormal
552 : SSA names. */
553 20579476 : if (gimple_has_lhs (stmt)
554 1678818 : && stmt_can_make_abnormal_goto (stmt)
555 3632228 : && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
556 : {
557 2539 : tree lhs = gimple_get_lhs (stmt);
558 2539 : tree tmp = create_tmp_var (TREE_TYPE (lhs));
559 2539 : gimple *s = gimple_build_assign (lhs, tmp);
560 2539 : gimple_set_location (s, gimple_location (stmt));
561 2539 : gimple_set_block (s, gimple_block (stmt));
562 2539 : gimple_set_lhs (stmt, tmp);
563 2539 : gsi_insert_after (&i, s, GSI_SAME_STMT);
564 : }
565 : start_new_block = true;
566 : }
567 :
568 94557160 : gsi_next (&i);
569 94557160 : first_stmt_of_seq = false;
570 : }
571 3072312 : return bb;
572 : }
573 :
574 : /* Build a flowgraph for the sequence of stmts SEQ. */
575 :
576 : static void
577 3019910 : make_blocks (gimple_seq seq)
578 : {
579 : /* Look for debug markers right before labels, and move the debug
580 : stmts after the labels. Accepting labels among debug markers
581 : adds no value, just complexity; if we wanted to annotate labels
582 : with view numbers (so sequencing among markers would matter) or
583 : somesuch, we're probably better off still moving the labels, but
584 : adding other debug annotations in their original positions or
585 : emitting nonbind or bind markers associated with the labels in
586 : the original position of the labels.
587 :
588 : Moving labels would probably be simpler, but we can't do that:
589 : moving labels assigns label ids to them, and doing so because of
590 : debug markers makes for -fcompare-debug and possibly even codegen
591 : differences. So, we have to move the debug stmts instead. To
592 : that end, we scan SEQ backwards, marking the position of the
593 : latest (earliest we find) label, and moving debug stmts that are
594 : not separated from it by nondebug nonlabel stmts after the
595 : label. */
596 3019910 : if (MAY_HAVE_DEBUG_MARKER_STMTS)
597 : {
598 1844296 : gimple_stmt_iterator label = gsi_none ();
599 :
600 55881592 : for (gimple_stmt_iterator i = gsi_last (seq); !gsi_end_p (i); gsi_prev (&i))
601 : {
602 50348704 : gimple *stmt = gsi_stmt (i);
603 :
604 : /* If this is the first label we encounter (latest in SEQ)
605 : before nondebug stmts, record its position. */
606 50348704 : if (is_a <glabel *> (stmt))
607 : {
608 9461822 : if (gsi_end_p (label))
609 8792315 : label = i;
610 9461822 : continue;
611 : }
612 :
613 : /* Without a recorded label position to move debug stmts to,
614 : there's nothing to do. */
615 40886882 : if (gsi_end_p (label))
616 32077337 : continue;
617 :
618 : /* Move the debug stmt at I after LABEL. */
619 8809545 : if (is_gimple_debug (stmt))
620 : {
621 22561 : gcc_assert (gimple_debug_nonbind_marker_p (stmt));
622 : /* As STMT is removed, I advances to the stmt after
623 : STMT, so the gsi_prev in the for "increment"
624 : expression gets us to the stmt we're to visit after
625 : STMT. LABEL, however, would advance to the moved
626 : stmt if we passed it to gsi_move_after, so pass it a
627 : copy instead, so as to keep LABEL pointing to the
628 : LABEL. */
629 22561 : gimple_stmt_iterator copy = label;
630 22561 : gsi_move_after (&i, ©);
631 22561 : continue;
632 22561 : }
633 :
634 : /* There aren't any (more?) debug stmts before label, so
635 : there isn't anything else to move after it. */
636 : label = gsi_none ();
637 : }
638 : }
639 :
640 : /* If the function decl itself is in a #pragma GCC suppress_coverage block we
641 : insert a nop with the same location as the function so that we're
642 : guaranteed that the first block gets coverage suppressed and the function
643 : entry isn't counted.
644 :
645 : #pragma GCC suppress_coverage begin
646 : int foo (args) // count = suppressed
647 : {
648 : #pragma GCC suppress_coverage end
649 : ...
650 : }
651 :
652 : If it isn't and the first stmt is suppressed we insert a nop to ensure that
653 : the function entry is counted, but the first (real) stmt is not:
654 :
655 : int foo (args) // count = 1
656 : {
657 : #pragma GCC suppress_coverage begin
658 : bar (); // count = suppressed
659 : #pragma GCC suppress_coverage end
660 : ...
661 : }
662 :
663 : gimple_can_merge_blocks_p is aware of specific instruction. */
664 3019910 : gimple_stmt_iterator gsi = gsi_start (seq);
665 3019910 : const location_t decl_loc = DECL_SOURCE_LOCATION (cfun->decl);
666 3019910 : if (location_in_pragma_suppress_coverage_p (decl_loc))
667 : {
668 32 : gimple *nop = gimple_build_nop ();
669 32 : gimple_set_location (nop, decl_loc);
670 32 : gsi_insert_seq_before (&gsi, nop, GSI_NEW_STMT);
671 : }
672 3019878 : else if (*gsi && in_pragma_suppress_coverage_p (*gsi, false))
673 : {
674 103 : gimple *nop = gimple_build_nop ();
675 103 : gsi_insert_seq_before (&gsi, nop, GSI_NEW_STMT);
676 : }
677 :
678 3019910 : make_blocks_1 (seq, ENTRY_BLOCK_PTR_FOR_FN (cfun));
679 3019910 : }
680 :
681 : /* Create and return a new empty basic block after bb AFTER. */
682 :
683 : static basic_block
684 74782583 : create_bb (void *h, void *e, basic_block after)
685 : {
686 74782583 : basic_block bb;
687 :
688 74782583 : gcc_assert (!e);
689 :
690 : /* Create and initialize a new basic block. Since alloc_block uses
691 : GC allocation that clears memory to allocate a basic block, we do
692 : not have to clear the newly allocated basic block here. */
693 74782583 : bb = alloc_block ();
694 :
695 74782583 : bb->index = last_basic_block_for_fn (cfun);
696 74782583 : bb->flags = BB_NEW;
697 74782583 : set_bb_seq (bb, h ? (gimple_seq) h : NULL);
698 :
699 : /* Add the new block to the linked list of blocks. */
700 74782583 : link_block (bb, after);
701 :
702 : /* Grow the basic block array if needed. */
703 74782583 : if ((size_t) last_basic_block_for_fn (cfun)
704 74782583 : == basic_block_info_for_fn (cfun)->length ())
705 22487271 : vec_safe_grow_cleared (basic_block_info_for_fn (cfun),
706 22487271 : last_basic_block_for_fn (cfun) + 1);
707 :
708 : /* Add the newly created block to the array. */
709 74782583 : SET_BASIC_BLOCK_FOR_FN (cfun, last_basic_block_for_fn (cfun), bb);
710 :
711 74782583 : n_basic_blocks_for_fn (cfun)++;
712 74782583 : last_basic_block_for_fn (cfun)++;
713 :
714 74782583 : return bb;
715 : }
716 :
717 :
718 : /*---------------------------------------------------------------------------
719 : Edge creation
720 : ---------------------------------------------------------------------------*/
721 :
722 : /* If basic block BB has an abnormal edge to a basic block
723 : containing IFN_ABNORMAL_DISPATCHER internal call, return
724 : that the dispatcher's basic block, otherwise return NULL. */
725 :
726 : basic_block
727 665 : get_abnormal_succ_dispatcher (basic_block bb)
728 : {
729 665 : edge e;
730 665 : edge_iterator ei;
731 :
732 1409 : FOR_EACH_EDGE (e, ei, bb->succs)
733 1057 : if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
734 : {
735 313 : gimple_stmt_iterator gsi
736 313 : = gsi_start_nondebug_after_labels_bb (e->dest);
737 313 : gimple *g = gsi_stmt (gsi);
738 313 : if (g && gimple_call_internal_p (g, IFN_ABNORMAL_DISPATCHER))
739 313 : return e->dest;
740 : }
741 : return NULL;
742 : }
743 :
744 : /* Helper function for make_edges. Create a basic block with
745 : with ABNORMAL_DISPATCHER internal call in it if needed, and
746 : create abnormal edges from BBS to it and from it to FOR_BB
747 : if COMPUTED_GOTO is false, otherwise factor the computed gotos. */
748 :
749 : static void
750 5557 : handle_abnormal_edges (basic_block *dispatcher_bbs, basic_block for_bb,
751 : auto_vec<basic_block> *bbs, bool computed_goto)
752 : {
753 5557 : basic_block *dispatcher = dispatcher_bbs + (computed_goto ? 1 : 0);
754 5557 : unsigned int idx = 0;
755 5557 : basic_block bb;
756 5557 : bool inner = false;
757 :
758 5557 : if (!bb_to_omp_idx.is_empty ())
759 : {
760 11 : dispatcher = dispatcher_bbs + 2 * bb_to_omp_idx[for_bb->index];
761 11 : if (bb_to_omp_idx[for_bb->index] != 0)
762 6 : inner = true;
763 : }
764 :
765 : /* If the dispatcher has been created already, then there are basic
766 : blocks with abnormal edges to it, so just make a new edge to
767 : for_bb. */
768 5557 : if (*dispatcher == NULL)
769 : {
770 : /* Check if there are any basic blocks that need to have
771 : abnormal edges to this dispatcher. If there are none, return
772 : early. */
773 3593 : if (bb_to_omp_idx.is_empty ())
774 : {
775 3582 : if (bbs->is_empty ())
776 5557 : return;
777 : }
778 : else
779 : {
780 18 : FOR_EACH_VEC_ELT (*bbs, idx, bb)
781 18 : if (bb_to_omp_idx[bb->index] == bb_to_omp_idx[for_bb->index])
782 : break;
783 11 : if (bb == NULL)
784 : return;
785 : }
786 :
787 : /* Create the dispatcher bb. */
788 2766 : *dispatcher = create_basic_block (NULL, for_bb);
789 2766 : if (computed_goto)
790 : {
791 : /* Factor computed gotos into a common computed goto site. Also
792 : record the location of that site so that we can un-factor the
793 : gotos after we have converted back to normal form. */
794 602 : gimple_stmt_iterator gsi = gsi_start_bb (*dispatcher);
795 :
796 : /* Create the destination of the factored goto. Each original
797 : computed goto will put its desired destination into this
798 : variable and jump to the label we create immediately below. */
799 602 : tree var = create_tmp_var (ptr_type_node, "gotovar");
800 :
801 : /* Build a label for the new block which will contain the
802 : factored computed goto. */
803 602 : tree factored_label_decl
804 602 : = create_artificial_label (UNKNOWN_LOCATION);
805 602 : gimple *factored_computed_goto_label
806 602 : = gimple_build_label (factored_label_decl);
807 602 : gsi_insert_after (&gsi, factored_computed_goto_label, GSI_NEW_STMT);
808 :
809 : /* Build our new computed goto. */
810 602 : gimple *factored_computed_goto = gimple_build_goto (var);
811 602 : gsi_insert_after (&gsi, factored_computed_goto, GSI_NEW_STMT);
812 :
813 2978 : FOR_EACH_VEC_ELT (*bbs, idx, bb)
814 : {
815 1172 : if (!bb_to_omp_idx.is_empty ()
816 0 : && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
817 0 : continue;
818 :
819 1172 : gsi = gsi_last_bb (bb);
820 1172 : gimple *last = gsi_stmt (gsi);
821 :
822 1172 : gcc_assert (computed_goto_p (last));
823 :
824 : /* Copy the original computed goto's destination into VAR. */
825 1172 : gimple *assignment
826 1172 : = gimple_build_assign (var, gimple_goto_dest (last));
827 1172 : gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
828 :
829 1172 : edge e = make_edge (bb, *dispatcher, EDGE_FALLTHRU);
830 1172 : e->goto_locus = gimple_location (last);
831 1172 : gsi_remove (&gsi, true);
832 : }
833 : }
834 : else
835 : {
836 2164 : tree arg = inner ? boolean_true_node : boolean_false_node;
837 2164 : gcall *g = gimple_build_call_internal (IFN_ABNORMAL_DISPATCHER,
838 : 1, arg);
839 2164 : gimple_call_set_ctrl_altering (g, true);
840 2164 : gimple_stmt_iterator gsi = gsi_after_labels (*dispatcher);
841 2164 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
842 :
843 : /* Create predecessor edges of the dispatcher. */
844 13928 : FOR_EACH_VEC_ELT (*bbs, idx, bb)
845 : {
846 7436 : if (!bb_to_omp_idx.is_empty ()
847 44 : && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
848 20 : continue;
849 7416 : make_edge (bb, *dispatcher, EDGE_ABNORMAL);
850 : }
851 : }
852 : }
853 :
854 4730 : make_edge (*dispatcher, for_bb, EDGE_ABNORMAL);
855 : }
856 :
857 : /* Creates outgoing edges for BB. Returns 1 when it ends with an
858 : computed goto, returns 2 when it ends with a statement that
859 : might return to this function via an nonlocal goto, otherwise
860 : return 0. Updates *PCUR_REGION with the OMP region this BB is in. */
861 :
862 : static int
863 22576825 : make_edges_bb (basic_block bb, struct omp_region **pcur_region, int *pomp_index)
864 : {
865 22576825 : gimple *last = *gsi_last_bb (bb);
866 22576825 : bool fallthru = false;
867 22576825 : int ret = 0;
868 :
869 22576825 : if (!last)
870 : return ret;
871 :
872 22576825 : switch (gimple_code (last))
873 : {
874 6352894 : case GIMPLE_GOTO:
875 6352894 : if (make_goto_expr_edges (bb))
876 : ret = 1;
877 : fallthru = false;
878 : break;
879 3010041 : case GIMPLE_RETURN:
880 3010041 : {
881 3010041 : edge e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
882 3010041 : e->goto_locus = gimple_location (last);
883 3010041 : fallthru = false;
884 : }
885 3010041 : break;
886 5619383 : case GIMPLE_COND:
887 5619383 : make_cond_expr_edges (bb);
888 5619383 : fallthru = false;
889 5619383 : break;
890 54848 : case GIMPLE_SWITCH:
891 54848 : make_gimple_switch_edges (as_a <gswitch *> (last), bb);
892 54848 : fallthru = false;
893 54848 : break;
894 943284 : case GIMPLE_RESX:
895 943284 : make_eh_edge (last);
896 943284 : fallthru = false;
897 943284 : break;
898 44487 : case GIMPLE_EH_DISPATCH:
899 44487 : fallthru = make_eh_dispatch_edges (as_a <geh_dispatch *> (last));
900 44487 : break;
901 :
902 3971800 : case GIMPLE_CALL:
903 : /* If this function receives a nonlocal goto, then we need to
904 : make edges from this call site to all the nonlocal goto
905 : handlers. */
906 3971800 : if (stmt_can_make_abnormal_goto (last))
907 7428 : ret = 2;
908 :
909 : /* If this statement has reachable exception handlers, then
910 : create abnormal edges to them. */
911 3971800 : make_eh_edge (last);
912 :
913 : /* BUILTIN_RETURN is really a return statement. */
914 3971800 : if (gimple_call_builtin_p (last, BUILT_IN_RETURN))
915 : {
916 368 : make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
917 368 : fallthru = false;
918 : }
919 : /* Some calls are known not to return. */
920 : else
921 3971432 : fallthru = !gimple_call_noreturn_p (last);
922 : break;
923 :
924 2254873 : case GIMPLE_ASSIGN:
925 : /* A GIMPLE_ASSIGN may throw internally and thus be considered
926 : control-altering. */
927 2254873 : if (is_ctrl_altering_stmt (last))
928 618578 : make_eh_edge (last);
929 : fallthru = true;
930 : break;
931 :
932 1630 : case GIMPLE_ASM:
933 1630 : make_gimple_asm_edges (bb);
934 1630 : fallthru = true;
935 1630 : break;
936 :
937 305333 : CASE_GIMPLE_OMP:
938 305333 : fallthru = omp_make_gimple_edges (bb, pcur_region, pomp_index);
939 305333 : break;
940 :
941 411 : case GIMPLE_TRANSACTION:
942 411 : {
943 411 : gtransaction *txn = as_a <gtransaction *> (last);
944 411 : tree label1 = gimple_transaction_label_norm (txn);
945 411 : tree label2 = gimple_transaction_label_uninst (txn);
946 :
947 411 : if (label1)
948 401 : make_edge (bb, label_to_block (cfun, label1), EDGE_FALLTHRU);
949 411 : if (label2)
950 397 : make_edge (bb, label_to_block (cfun, label2),
951 397 : EDGE_TM_UNINSTRUMENTED | (label1 ? 0 : EDGE_FALLTHRU));
952 :
953 411 : tree label3 = gimple_transaction_label_over (txn);
954 411 : if (gimple_transaction_subcode (txn)
955 : & (GTMA_HAVE_ABORT | GTMA_IS_OUTER))
956 65 : make_edge (bb, label_to_block (cfun, label3), EDGE_TM_ABORT);
957 :
958 : fallthru = false;
959 : }
960 : break;
961 :
962 17841 : default:
963 17841 : gcc_assert (!stmt_ends_bb_p (last));
964 : fallthru = true;
965 : break;
966 : }
967 :
968 14569449 : if (fallthru)
969 4817026 : make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
970 :
971 : return ret;
972 : }
973 :
974 : /* Join all the blocks in the flowgraph. */
975 :
976 : static void
977 3019910 : make_edges (void)
978 : {
979 3019910 : basic_block bb;
980 3019910 : struct omp_region *cur_region = NULL;
981 3019910 : auto_vec<basic_block> ab_edge_goto;
982 3019910 : auto_vec<basic_block> ab_edge_call;
983 3019910 : int cur_omp_region_idx = 0;
984 :
985 : /* Create an edge from entry to the first block with executable
986 : statements in it. */
987 3019910 : make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun),
988 3019910 : BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS),
989 : EDGE_FALLTHRU);
990 :
991 : /* Traverse the basic block array placing edges. */
992 25457006 : FOR_EACH_BB_FN (bb, cfun)
993 : {
994 22437096 : int mer;
995 :
996 22437096 : if (!bb_to_omp_idx.is_empty ())
997 754216 : bb_to_omp_idx[bb->index] = cur_omp_region_idx;
998 :
999 22437096 : mer = make_edges_bb (bb, &cur_region, &cur_omp_region_idx);
1000 22437096 : if (mer == 1)
1001 1273 : ab_edge_goto.safe_push (bb);
1002 22435823 : else if (mer == 2)
1003 7428 : ab_edge_call.safe_push (bb);
1004 :
1005 22995115 : if (cur_region && bb_to_omp_idx.is_empty ())
1006 23579 : bb_to_omp_idx.safe_grow_cleared (n_basic_blocks_for_fn (cfun), true);
1007 : }
1008 :
1009 : /* Computed gotos are hell to deal with, especially if there are
1010 : lots of them with a large number of destinations. So we factor
1011 : them to a common computed goto location before we build the
1012 : edge list. After we convert back to normal form, we will un-factor
1013 : the computed gotos since factoring introduces an unwanted jump.
1014 : For non-local gotos and abnormal edges from calls to calls that return
1015 : twice or forced labels, factor the abnormal edges too, by having all
1016 : abnormal edges from the calls go to a common artificial basic block
1017 : with ABNORMAL_DISPATCHER internal call and abnormal edges from that
1018 : basic block to all forced labels and calls returning twice.
1019 : We do this per-OpenMP structured block, because those regions
1020 : are guaranteed to be single entry single exit by the standard,
1021 : so it is not allowed to enter or exit such regions abnormally this way,
1022 : thus all computed gotos, non-local gotos and setjmp/longjmp calls
1023 : must not transfer control across SESE region boundaries. */
1024 3019910 : if (!ab_edge_goto.is_empty () || !ab_edge_call.is_empty ())
1025 : {
1026 2818 : gimple_stmt_iterator gsi;
1027 2818 : basic_block dispatcher_bb_array[2] = { NULL, NULL };
1028 2818 : basic_block *dispatcher_bbs = dispatcher_bb_array;
1029 2818 : int count = n_basic_blocks_for_fn (cfun);
1030 :
1031 2818 : if (!bb_to_omp_idx.is_empty ())
1032 9 : dispatcher_bbs = XCNEWVEC (basic_block, 2 * count);
1033 :
1034 32773 : FOR_EACH_BB_FN (bb, cfun)
1035 : {
1036 76259 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1037 : {
1038 44498 : glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
1039 16847 : tree target;
1040 :
1041 16847 : if (!label_stmt)
1042 : break;
1043 :
1044 16847 : target = gimple_label_label (label_stmt);
1045 :
1046 : /* Make an edge to every label block that has been marked as a
1047 : potential target for a computed goto or a non-local goto. */
1048 16847 : if (FORCED_LABEL (target))
1049 2633 : handle_abnormal_edges (dispatcher_bbs, bb, &ab_edge_goto,
1050 : true);
1051 16847 : if (DECL_NONLOCAL (target))
1052 : {
1053 498 : handle_abnormal_edges (dispatcher_bbs, bb, &ab_edge_call,
1054 : false);
1055 498 : break;
1056 : }
1057 : }
1058 :
1059 29955 : if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
1060 2357 : gsi_next_nondebug (&gsi);
1061 29955 : if (!gsi_end_p (gsi))
1062 : {
1063 : /* Make an edge to every setjmp-like call. */
1064 28013 : gimple *call_stmt = gsi_stmt (gsi);
1065 28013 : if (is_gimple_call (call_stmt)
1066 28013 : && ((gimple_call_flags (call_stmt) & ECF_RETURNS_TWICE)
1067 8396 : || gimple_call_builtin_p (call_stmt,
1068 : BUILT_IN_SETJMP_RECEIVER)))
1069 2426 : handle_abnormal_edges (dispatcher_bbs, bb, &ab_edge_call,
1070 : false);
1071 : }
1072 : }
1073 :
1074 2827 : if (!bb_to_omp_idx.is_empty ())
1075 9 : XDELETE (dispatcher_bbs);
1076 : }
1077 :
1078 3019910 : omp_free_regions ();
1079 3019910 : }
1080 :
1081 : /* Add SEQ after GSI. Start new bb after GSI, and created further bbs as
1082 : needed. Returns true if new bbs were created.
1083 : Note: This is transitional code, and should not be used for new code. We
1084 : should be able to get rid of this by rewriting all target va-arg
1085 : gimplification hooks to use an interface gimple_build_cond_value as described
1086 : in https://gcc.gnu.org/ml/gcc-patches/2015-02/msg01194.html. */
1087 :
1088 : bool
1089 52402 : gimple_find_sub_bbs (gimple_seq seq, gimple_stmt_iterator *gsi)
1090 : {
1091 52402 : gimple *stmt = gsi_stmt (*gsi);
1092 52402 : basic_block bb = gimple_bb (stmt);
1093 52402 : basic_block lastbb, afterbb;
1094 52402 : int old_num_bbs = n_basic_blocks_for_fn (cfun);
1095 52402 : edge e;
1096 52402 : lastbb = make_blocks_1 (seq, bb);
1097 52402 : if (old_num_bbs == n_basic_blocks_for_fn (cfun))
1098 : return false;
1099 52402 : e = split_block (bb, stmt);
1100 : /* Move e->dest to come after the new basic blocks. */
1101 52402 : afterbb = e->dest;
1102 52402 : unlink_block (afterbb);
1103 52402 : link_block (afterbb, lastbb);
1104 52402 : redirect_edge_succ (e, bb->next_bb);
1105 52402 : bb = bb->next_bb;
1106 192131 : while (bb != afterbb)
1107 : {
1108 139729 : struct omp_region *cur_region = NULL;
1109 139729 : profile_count cnt = profile_count::zero ();
1110 139729 : bool all = true;
1111 :
1112 139729 : int cur_omp_region_idx = 0;
1113 139729 : int mer = make_edges_bb (bb, &cur_region, &cur_omp_region_idx);
1114 139729 : gcc_assert (!mer && !cur_region);
1115 139729 : add_bb_to_loop (bb, afterbb->loop_father);
1116 :
1117 139729 : edge e;
1118 139729 : edge_iterator ei;
1119 308826 : FOR_EACH_EDGE (e, ei, bb->preds)
1120 : {
1121 169097 : if (e->count ().initialized_p ())
1122 23565 : cnt += e->count ();
1123 : else
1124 : all = false;
1125 : }
1126 139729 : tree_guess_outgoing_edge_probabilities (bb);
1127 139729 : if (all || profile_status_for_fn (cfun) == PROFILE_READ)
1128 18915 : bb->count = cnt;
1129 :
1130 139729 : bb = bb->next_bb;
1131 : }
1132 : return true;
1133 : }
1134 :
1135 : /* Auto-profile needs discriminator to distinguish statements with same line
1136 : number (file name is ignored) which are in different basic block. This
1137 : map keeps track of current discriminator for a given line number. */
1138 : struct discrim_entry
1139 : {
1140 : /* ID of basic block we saw line number last time. */
1141 : unsigned int bb_id;
1142 : /* Discriminator we used. */
1143 : unsigned int discrim;
1144 : };
1145 :
1146 : /* Return updated LOC with discriminator for use in basic block BB_ID.
1147 : MAP keeps track of current values. */
1148 :
1149 : location_t
1150 75968143 : assign_discriminator (location_t loc, unsigned int bb_id,
1151 : hash_map<int_hash <unsigned, -1U, -2U>,
1152 : discrim_entry> &map)
1153 : {
1154 75968143 : bool existed;
1155 75968143 : if ((unsigned) LOCATION_LINE (loc) >= -2U)
1156 : return loc;
1157 75968141 : discrim_entry &e
1158 75968141 : = map.get_or_insert ((unsigned) LOCATION_LINE (loc), &existed);
1159 75968141 : gcc_checking_assert (!has_discriminator (loc));
1160 75968141 : if (!existed)
1161 : {
1162 14280495 : e.bb_id = bb_id;
1163 14280495 : e.discrim = 0;
1164 14280495 : return loc;
1165 : }
1166 61687646 : if (e.bb_id != bb_id)
1167 : {
1168 41455952 : e.bb_id = bb_id;
1169 41455952 : e.discrim++;
1170 : }
1171 61687646 : if (e.discrim)
1172 52298205 : return location_with_discriminator (loc, e.discrim);
1173 : return loc;
1174 : }
1175 :
1176 : /* Assign discriminators to statement locations. */
1177 :
1178 : static void
1179 3019910 : assign_discriminators (void)
1180 : {
1181 3019910 : hash_map<int_hash <unsigned, -1U, -2U>, discrim_entry> map (13);
1182 3019910 : unsigned int bb_id = 0;
1183 3019910 : basic_block bb;
1184 25459772 : FOR_EACH_BB_FN (bb, cfun)
1185 : {
1186 22439862 : location_t prev_loc = UNKNOWN_LOCATION, prev_replacement = UNKNOWN_LOCATION;
1187 : /* Traverse the basic block, if two function calls within a basic block
1188 : are mapped to the same line, assign a new discriminator because a call
1189 : stmt could be a split point of a basic block. */
1190 44879724 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1191 107678260 : !gsi_end_p (gsi); gsi_next (&gsi))
1192 : {
1193 85238398 : gimple *stmt = gsi_stmt (gsi);
1194 85238398 : location_t loc = gimple_location (stmt);
1195 85238398 : if (loc == UNKNOWN_LOCATION)
1196 4645203 : continue;
1197 80593195 : if (loc == prev_loc)
1198 24261586 : gimple_set_location (stmt, prev_replacement);
1199 : else
1200 : {
1201 56331609 : prev_loc = loc;
1202 56331609 : prev_replacement = assign_discriminator (loc, bb_id, map);
1203 112663218 : gimple_set_location (stmt, prev_replacement);
1204 : }
1205 : /* Break basic blocks after each call. This is required so each
1206 : call site has unique discriminator.
1207 : More correctly, we can break after each statement that can possibly
1208 : terinate execution of the basic block, but for auto-profile this
1209 : precision is probably not useful. */
1210 80593195 : if (gimple_code (stmt) == GIMPLE_CALL)
1211 : {
1212 11469831 : prev_loc = UNKNOWN_LOCATION;
1213 11469831 : bb_id++;
1214 : }
1215 : }
1216 : /* If basic block has multiple successors, consider every edge as a
1217 : separate block. */
1218 22439862 : if (!single_succ_p (bb))
1219 10060901 : bb_id++;
1220 92653031 : for (edge e : bb->succs)
1221 : {
1222 29008999 : if (e->goto_locus != UNKNOWN_LOCATION)
1223 19636534 : e->goto_locus = assign_discriminator (e->goto_locus, bb_id, map);
1224 29008999 : for (gphi_iterator gpi = gsi_start_phis (bb);
1225 29008999 : !gsi_end_p (gpi); gsi_next (&gpi))
1226 : {
1227 0 : gphi *phi = gpi.phi ();
1228 0 : location_t phi_loc
1229 0 : = gimple_phi_arg_location_from_edge (phi, e);
1230 0 : if (phi_loc == UNKNOWN_LOCATION)
1231 0 : continue;
1232 0 : gimple_phi_arg_set_location
1233 0 : (phi, e->dest_idx, assign_discriminator (phi_loc, bb_id, map));
1234 : }
1235 29008999 : bb_id++;
1236 : }
1237 22439862 : bb_id++;
1238 : }
1239 :
1240 3019910 : }
1241 :
1242 : /* Create the edges for a GIMPLE_COND starting at block BB. */
1243 :
1244 : static void
1245 5619383 : make_cond_expr_edges (basic_block bb)
1246 : {
1247 11238766 : gcond *entry = as_a <gcond *> (*gsi_last_bb (bb));
1248 5619383 : gimple *then_stmt, *else_stmt;
1249 5619383 : basic_block then_bb, else_bb;
1250 5619383 : tree then_label, else_label;
1251 5619383 : edge e;
1252 :
1253 5619383 : gcc_assert (entry);
1254 :
1255 : /* Entry basic blocks for each component. */
1256 5619383 : then_label = gimple_cond_true_label (entry);
1257 5619383 : else_label = gimple_cond_false_label (entry);
1258 5619383 : then_bb = label_to_block (cfun, then_label);
1259 5619383 : else_bb = label_to_block (cfun, else_label);
1260 5619383 : then_stmt = first_stmt (then_bb);
1261 5619383 : else_stmt = first_stmt (else_bb);
1262 :
1263 5619383 : e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1264 5619383 : e->goto_locus = gimple_location (then_stmt);
1265 5619383 : e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1266 5619383 : if (e)
1267 5613518 : e->goto_locus = gimple_location (else_stmt);
1268 :
1269 : /* We do not need the labels anymore. */
1270 5619383 : gimple_cond_set_true_label (entry, NULL_TREE);
1271 5619383 : gimple_cond_set_false_label (entry, NULL_TREE);
1272 5619383 : }
1273 :
1274 :
1275 : /* Called for each element in the hash table (P) as we delete the
1276 : edge to cases hash table.
1277 :
1278 : Clear all the CASE_CHAINs to prevent problems with copying of
1279 : SWITCH_EXPRs and structure sharing rules, then free the hash table
1280 : element. */
1281 :
1282 : bool
1283 1180323 : edge_to_cases_cleanup (edge const &, tree const &value, void *)
1284 : {
1285 1180323 : tree t, next;
1286 :
1287 2515816 : for (t = value; t; t = next)
1288 : {
1289 1335493 : next = CASE_CHAIN (t);
1290 1335493 : CASE_CHAIN (t) = NULL;
1291 : }
1292 :
1293 1180323 : return true;
1294 : }
1295 :
1296 : /* Start recording information mapping edges to case labels. */
1297 :
1298 : void
1299 31714619 : start_recording_case_labels (void)
1300 : {
1301 31714619 : gcc_assert (edge_to_cases == NULL);
1302 31714619 : edge_to_cases = new hash_map<edge, tree>;
1303 31714619 : touched_switch_bbs = BITMAP_ALLOC (NULL);
1304 31714619 : }
1305 :
1306 : /* Return nonzero if we are recording information for case labels. */
1307 :
1308 : static bool
1309 420128 : recording_case_labels_p (void)
1310 : {
1311 420128 : return (edge_to_cases != NULL);
1312 : }
1313 :
1314 : /* Stop recording information mapping edges to case labels and
1315 : remove any information we have recorded. */
1316 : void
1317 31714619 : end_recording_case_labels (void)
1318 : {
1319 31714619 : bitmap_iterator bi;
1320 31714619 : unsigned i;
1321 32894942 : edge_to_cases->traverse<void *, edge_to_cases_cleanup> (NULL);
1322 63429238 : delete edge_to_cases;
1323 31714619 : edge_to_cases = NULL;
1324 31890758 : EXECUTE_IF_SET_IN_BITMAP (touched_switch_bbs, 0, i, bi)
1325 : {
1326 176139 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1327 176139 : if (bb)
1328 : {
1329 524229 : if (gswitch *stmt = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)))
1330 173833 : group_case_labels_stmt (stmt);
1331 : }
1332 : }
1333 31714619 : BITMAP_FREE (touched_switch_bbs);
1334 31714619 : }
1335 :
1336 : /* If we are inside a {start,end}_recording_cases block, then return
1337 : a chain of CASE_LABEL_EXPRs from T which reference E.
1338 :
1339 : Otherwise return NULL. */
1340 :
1341 : tree
1342 420128 : get_cases_for_edge (edge e, gswitch *t)
1343 : {
1344 420128 : tree *slot;
1345 420128 : size_t i, n;
1346 :
1347 : /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
1348 : chains available. Return NULL so the caller can detect this case. */
1349 420128 : if (!recording_case_labels_p ())
1350 : return NULL;
1351 :
1352 357664 : slot = edge_to_cases->get (e);
1353 357664 : if (slot)
1354 181669 : return *slot;
1355 :
1356 : /* If we did not find E in the hash table, then this must be the first
1357 : time we have been queried for information about E & T. Add all the
1358 : elements from T to the hash table then perform the query again. */
1359 :
1360 175995 : n = gimple_switch_num_labels (t);
1361 1502425 : for (i = 0; i < n; i++)
1362 : {
1363 1326430 : tree elt = gimple_switch_label (t, i);
1364 1326430 : tree lab = CASE_LABEL (elt);
1365 1326430 : basic_block label_bb = label_to_block (cfun, lab);
1366 1326430 : edge this_edge = find_edge (e->src, label_bb);
1367 :
1368 : /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
1369 : a new chain. */
1370 1326430 : tree &s = edge_to_cases->get_or_insert (this_edge);
1371 1326430 : CASE_CHAIN (elt) = s;
1372 1326430 : s = elt;
1373 : }
1374 :
1375 175995 : return *edge_to_cases->get (e);
1376 : }
1377 :
1378 : /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
1379 :
1380 : static void
1381 54848 : make_gimple_switch_edges (gswitch *entry, basic_block bb)
1382 : {
1383 54848 : size_t i, n;
1384 :
1385 54848 : n = gimple_switch_num_labels (entry);
1386 :
1387 385105 : for (i = 0; i < n; ++i)
1388 : {
1389 330257 : basic_block label_bb = gimple_switch_label_bb (cfun, entry, i);
1390 330257 : make_edge (bb, label_bb, 0);
1391 : }
1392 54848 : }
1393 :
1394 :
1395 : /* Return the basic block holding label DEST. */
1396 :
1397 : basic_block
1398 482528752 : label_to_block (struct function *ifun, tree dest)
1399 : {
1400 482528752 : int uid = LABEL_DECL_UID (dest);
1401 :
1402 : /* We would die hard when faced by an undefined label. Emit a label to
1403 : the very first basic block. This will hopefully make even the dataflow
1404 : and undefined variable warnings quite right. */
1405 482528752 : if (seen_error () && uid < 0)
1406 : {
1407 141 : gimple_stmt_iterator gsi =
1408 141 : gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS));
1409 141 : gimple *stmt;
1410 :
1411 141 : stmt = gimple_build_label (dest);
1412 141 : gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1413 141 : uid = LABEL_DECL_UID (dest);
1414 : }
1415 482528752 : if (vec_safe_length (ifun->cfg->x_label_to_block_map) <= (unsigned int) uid)
1416 : return NULL;
1417 482528752 : return (*ifun->cfg->x_label_to_block_map)[uid];
1418 : }
1419 :
1420 : /* Create edges for a goto statement at block BB. Returns true
1421 : if abnormal edges should be created. */
1422 :
1423 : static bool
1424 6352894 : make_goto_expr_edges (basic_block bb)
1425 : {
1426 6352894 : gimple_stmt_iterator last = gsi_last_bb (bb);
1427 6352894 : gimple *goto_t = gsi_stmt (last);
1428 :
1429 : /* A simple GOTO creates normal edges. */
1430 6352894 : if (simple_goto_p (goto_t))
1431 : {
1432 6351621 : tree dest = gimple_goto_dest (goto_t);
1433 6351621 : basic_block label_bb = label_to_block (cfun, dest);
1434 6351621 : edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
1435 6351621 : e->goto_locus = gimple_location (goto_t);
1436 6351621 : gsi_remove (&last, true);
1437 6351621 : return false;
1438 : }
1439 :
1440 : /* A computed GOTO creates abnormal edges. */
1441 : return true;
1442 : }
1443 :
1444 : /* Create edges for an asm statement with labels at block BB. */
1445 :
1446 : static void
1447 1630 : make_gimple_asm_edges (basic_block bb)
1448 : {
1449 3260 : gasm *stmt = as_a <gasm *> (*gsi_last_bb (bb));
1450 1630 : int i, n = gimple_asm_nlabels (stmt);
1451 :
1452 2568 : for (i = 0; i < n; ++i)
1453 : {
1454 938 : tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
1455 938 : basic_block label_bb = label_to_block (cfun, label);
1456 938 : make_edge (bb, label_bb, 0);
1457 : }
1458 1630 : }
1459 :
1460 : /*---------------------------------------------------------------------------
1461 : Flowgraph analysis
1462 : ---------------------------------------------------------------------------*/
1463 :
1464 : /* Cleanup useless labels in basic blocks. This is something we wish
1465 : to do early because it allows us to group case labels before creating
1466 : the edges for the CFG, and it speeds up block statement iterators in
1467 : all passes later on.
1468 : We rerun this pass after CFG is created, to get rid of the labels that
1469 : are no longer referenced. After then we do not run it any more, since
1470 : (almost) no new labels should be created. */
1471 :
1472 : /* A map from basic block index to the leading label of that block. */
1473 : struct label_record
1474 : {
1475 : /* The label. */
1476 : tree label;
1477 :
1478 : /* True if the label is referenced from somewhere. */
1479 : bool used;
1480 : };
1481 :
1482 : /* Given LABEL return the first label in the same basic block. */
1483 :
1484 : static tree
1485 21194593 : main_block_label (tree label, label_record *label_for_bb)
1486 : {
1487 21194593 : basic_block bb = label_to_block (cfun, label);
1488 21194593 : tree main_label = label_for_bb[bb->index].label;
1489 :
1490 : /* label_to_block possibly inserted undefined label into the chain. */
1491 21194593 : if (!main_label)
1492 : {
1493 96 : label_for_bb[bb->index].label = label;
1494 96 : main_label = label;
1495 : }
1496 :
1497 21194593 : label_for_bb[bb->index].used = true;
1498 21194593 : return main_label;
1499 : }
1500 :
1501 : /* Clean up redundant labels within the exception tree. */
1502 :
1503 : static void
1504 7571691 : cleanup_dead_labels_eh (label_record *label_for_bb)
1505 : {
1506 7571691 : eh_landing_pad lp;
1507 7571691 : eh_region r;
1508 7571691 : tree lab;
1509 7571691 : int i;
1510 :
1511 7571691 : if (cfun->eh == NULL)
1512 7571691 : return;
1513 :
1514 10953703 : for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1515 3382012 : if (lp && lp->post_landing_pad)
1516 : {
1517 2100273 : lab = main_block_label (lp->post_landing_pad, label_for_bb);
1518 2100273 : if (lab != lp->post_landing_pad)
1519 : {
1520 0 : EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1521 0 : lp->post_landing_pad = lab;
1522 0 : EH_LANDING_PAD_NR (lab) = lp->index;
1523 : }
1524 : }
1525 :
1526 13245391 : FOR_ALL_EH_REGION (r)
1527 5673700 : switch (r->type)
1528 : {
1529 : case ERT_CLEANUP:
1530 : case ERT_MUST_NOT_THROW:
1531 : break;
1532 :
1533 142557 : case ERT_TRY:
1534 142557 : {
1535 142557 : eh_catch c;
1536 284392 : for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
1537 : {
1538 141835 : lab = c->label;
1539 141835 : if (lab)
1540 94224 : c->label = main_block_label (lab, label_for_bb);
1541 : }
1542 : }
1543 : break;
1544 :
1545 8988 : case ERT_ALLOWED_EXCEPTIONS:
1546 8988 : lab = r->u.allowed.label;
1547 8988 : if (lab)
1548 890 : r->u.allowed.label = main_block_label (lab, label_for_bb);
1549 : break;
1550 : }
1551 : }
1552 :
1553 :
1554 : /* Cleanup redundant labels. This is a three-step process:
1555 : 1) Find the leading label for each block.
1556 : 2) Redirect all references to labels to the leading labels.
1557 : 3) Cleanup all useless labels. */
1558 :
1559 : void
1560 7571691 : cleanup_dead_labels (void)
1561 : {
1562 7571691 : basic_block bb;
1563 7571691 : label_record *label_for_bb = XCNEWVEC (struct label_record,
1564 : last_basic_block_for_fn (cfun));
1565 :
1566 : /* Find a suitable label for each block. We use the first user-defined
1567 : label if there is one, or otherwise just the first label we see. */
1568 65332866 : FOR_EACH_BB_FN (bb, cfun)
1569 : {
1570 57761175 : gimple_stmt_iterator i;
1571 :
1572 151068600 : for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1573 : {
1574 91967554 : tree label;
1575 93318339 : glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (i));
1576 :
1577 35557164 : if (!label_stmt)
1578 : break;
1579 :
1580 35557164 : label = gimple_label_label (label_stmt);
1581 :
1582 : /* If we have not yet seen a label for the current block,
1583 : remember this one and see if there are more labels. */
1584 35557164 : if (!label_for_bb[bb->index].label)
1585 : {
1586 33267053 : label_for_bb[bb->index].label = label;
1587 33267053 : continue;
1588 : }
1589 :
1590 : /* If we did see a label for the current block already, but it
1591 : is an artificially created label, replace it if the current
1592 : label is a user defined label. */
1593 2290111 : if (!DECL_ARTIFICIAL (label)
1594 2290111 : && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
1595 : {
1596 10914 : label_for_bb[bb->index].label = label;
1597 10914 : break;
1598 : }
1599 : }
1600 : }
1601 :
1602 : /* Now redirect all jumps/branches to the selected label.
1603 : First do so for each block ending in a control statement. */
1604 65332866 : FOR_EACH_BB_FN (bb, cfun)
1605 : {
1606 57761175 : gimple *stmt = *gsi_last_bb (bb);
1607 57761175 : tree label, new_label;
1608 :
1609 57761175 : if (!stmt)
1610 431782 : continue;
1611 :
1612 57329393 : switch (gimple_code (stmt))
1613 : {
1614 17006991 : case GIMPLE_COND:
1615 17006991 : {
1616 17006991 : gcond *cond_stmt = as_a <gcond *> (stmt);
1617 17006991 : label = gimple_cond_true_label (cond_stmt);
1618 17006991 : if (label)
1619 : {
1620 5590015 : new_label = main_block_label (label, label_for_bb);
1621 5590015 : if (new_label != label)
1622 9979 : gimple_cond_set_true_label (cond_stmt, new_label);
1623 : }
1624 :
1625 17006991 : label = gimple_cond_false_label (cond_stmt);
1626 17006991 : if (label)
1627 : {
1628 5590015 : new_label = main_block_label (label, label_for_bb);
1629 5590015 : if (new_label != label)
1630 112575 : gimple_cond_set_false_label (cond_stmt, new_label);
1631 : }
1632 : }
1633 : break;
1634 :
1635 117344 : case GIMPLE_SWITCH:
1636 117344 : {
1637 117344 : gswitch *switch_stmt = as_a <gswitch *> (stmt);
1638 117344 : size_t i, n = gimple_switch_num_labels (switch_stmt);
1639 :
1640 : /* Replace all destination labels. */
1641 1608663 : for (i = 0; i < n; ++i)
1642 : {
1643 1491319 : tree case_label = gimple_switch_label (switch_stmt, i);
1644 1491319 : label = CASE_LABEL (case_label);
1645 1491319 : new_label = main_block_label (label, label_for_bb);
1646 1491319 : if (new_label != label)
1647 791958 : CASE_LABEL (case_label) = new_label;
1648 : }
1649 : break;
1650 : }
1651 :
1652 5639 : case GIMPLE_ASM:
1653 5639 : {
1654 5639 : gasm *asm_stmt = as_a <gasm *> (stmt);
1655 5639 : int i, n = gimple_asm_nlabels (asm_stmt);
1656 :
1657 8441 : for (i = 0; i < n; ++i)
1658 : {
1659 2802 : tree cons = gimple_asm_label_op (asm_stmt, i);
1660 2802 : tree label = main_block_label (TREE_VALUE (cons), label_for_bb);
1661 2802 : TREE_VALUE (cons) = label;
1662 : }
1663 : break;
1664 : }
1665 :
1666 : /* We have to handle gotos until they're removed, and we don't
1667 : remove them until after we've created the CFG edges. */
1668 6325065 : case GIMPLE_GOTO:
1669 6325065 : if (!computed_goto_p (stmt))
1670 : {
1671 6322645 : ggoto *goto_stmt = as_a <ggoto *> (stmt);
1672 6322645 : label = gimple_goto_dest (goto_stmt);
1673 6322645 : new_label = main_block_label (label, label_for_bb);
1674 6322645 : if (new_label != label)
1675 1127424 : gimple_goto_set_dest (goto_stmt, new_label);
1676 : }
1677 : break;
1678 :
1679 822 : case GIMPLE_TRANSACTION:
1680 822 : {
1681 822 : gtransaction *txn = as_a <gtransaction *> (stmt);
1682 :
1683 822 : label = gimple_transaction_label_norm (txn);
1684 822 : if (label)
1685 : {
1686 802 : new_label = main_block_label (label, label_for_bb);
1687 802 : if (new_label != label)
1688 0 : gimple_transaction_set_label_norm (txn, new_label);
1689 : }
1690 :
1691 822 : label = gimple_transaction_label_uninst (txn);
1692 822 : if (label)
1693 : {
1694 794 : new_label = main_block_label (label, label_for_bb);
1695 794 : if (new_label != label)
1696 0 : gimple_transaction_set_label_uninst (txn, new_label);
1697 : }
1698 :
1699 822 : label = gimple_transaction_label_over (txn);
1700 822 : if (label)
1701 : {
1702 814 : new_label = main_block_label (label, label_for_bb);
1703 814 : if (new_label != label)
1704 7 : gimple_transaction_set_label_over (txn, new_label);
1705 : }
1706 : }
1707 : break;
1708 :
1709 : default:
1710 : break;
1711 : }
1712 : }
1713 :
1714 : /* Do the same for the exception region tree labels. */
1715 7571691 : cleanup_dead_labels_eh (label_for_bb);
1716 :
1717 : /* Finally, purge dead labels. All user-defined labels and labels that
1718 : can be the target of non-local gotos and labels which have their
1719 : address taken are preserved. */
1720 65332866 : FOR_EACH_BB_FN (bb, cfun)
1721 : {
1722 57761175 : gimple_stmt_iterator i;
1723 57761175 : tree label_for_this_bb = label_for_bb[bb->index].label;
1724 :
1725 57761175 : if (!label_for_this_bb)
1726 24494026 : continue;
1727 :
1728 : /* If the main label of the block is unused, we may still remove it. */
1729 33267149 : if (!label_for_bb[bb->index].used)
1730 15552147 : label_for_this_bb = NULL;
1731 :
1732 102091606 : for (i = gsi_start_bb (bb); !gsi_end_p (i); )
1733 : {
1734 67915711 : tree label;
1735 68824457 : glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (i));
1736 :
1737 35557308 : if (!label_stmt)
1738 : break;
1739 :
1740 35557308 : label = gimple_label_label (label_stmt);
1741 :
1742 35557308 : if (label == label_for_this_bb
1743 17842306 : || !DECL_ARTIFICIAL (label)
1744 17094696 : || DECL_NONLOCAL (label)
1745 52650482 : || FORCED_LABEL (label))
1746 18491058 : gsi_next (&i);
1747 : else
1748 : {
1749 17066250 : gcc_checking_assert (EH_LANDING_PAD_NR (label) == 0);
1750 17066250 : gsi_remove (&i, true);
1751 : }
1752 : }
1753 : }
1754 :
1755 7571691 : free (label_for_bb);
1756 7571691 : }
1757 :
1758 : /* Scan the sorted vector of cases in STMT (a GIMPLE_SWITCH) and combine
1759 : the ones jumping to the same label.
1760 : Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1761 :
1762 : bool
1763 295768 : group_case_labels_stmt (gswitch *stmt)
1764 : {
1765 295768 : int old_size = gimple_switch_num_labels (stmt);
1766 295768 : int i, next_index, new_size;
1767 295768 : basic_block default_bb = NULL;
1768 :
1769 295768 : default_bb = gimple_switch_default_bb (cfun, stmt);
1770 :
1771 : /* Look for possible opportunities to merge cases. */
1772 295768 : new_size = i = 1;
1773 2404328 : while (i < old_size)
1774 : {
1775 1812792 : tree base_case, base_high;
1776 1812792 : basic_block base_bb;
1777 :
1778 1812792 : base_case = gimple_switch_label (stmt, i);
1779 :
1780 1812792 : gcc_assert (base_case);
1781 1812792 : base_bb = label_to_block (cfun, CASE_LABEL (base_case));
1782 :
1783 : /* Discard cases that have the same destination as the default case. */
1784 1820957 : if (base_bb == NULL
1785 1812792 : || base_bb == default_bb)
1786 : {
1787 8165 : i++;
1788 8165 : continue;
1789 : }
1790 :
1791 3609254 : base_high = CASE_HIGH (base_case)
1792 1804627 : ? CASE_HIGH (base_case)
1793 1722231 : : CASE_LOW (base_case);
1794 1804627 : next_index = i + 1;
1795 :
1796 : /* Try to merge case labels. Break out when we reach the end
1797 : of the label vector or when we cannot merge the next case
1798 : label with the current one. */
1799 2552988 : while (next_index < old_size)
1800 : {
1801 2259247 : tree merge_case = gimple_switch_label (stmt, next_index);
1802 2259247 : basic_block merge_bb = label_to_block (cfun, CASE_LABEL (merge_case));
1803 2259247 : wide_int bhp1 = wi::to_wide (base_high) + 1;
1804 :
1805 : /* Merge the cases if they jump to the same place,
1806 : and their ranges are consecutive. */
1807 2259247 : if (merge_bb == base_bb
1808 3109382 : && wi::to_wide (CASE_LOW (merge_case)) == bhp1)
1809 : {
1810 748361 : base_high
1811 1496722 : = (CASE_HIGH (merge_case)
1812 748361 : ? CASE_HIGH (merge_case) : CASE_LOW (merge_case));
1813 748361 : CASE_HIGH (base_case) = base_high;
1814 748361 : next_index++;
1815 : }
1816 : else
1817 : break;
1818 2259247 : }
1819 :
1820 1804627 : if (new_size < i)
1821 73324 : gimple_switch_set_label (stmt, new_size,
1822 : gimple_switch_label (stmt, i));
1823 1804627 : i = next_index;
1824 1804627 : new_size++;
1825 : }
1826 :
1827 295768 : gcc_assert (new_size <= old_size);
1828 :
1829 295768 : if (new_size < old_size)
1830 20271 : gimple_switch_set_num_labels (stmt, new_size);
1831 :
1832 295768 : return new_size < old_size;
1833 : }
1834 :
1835 : /* Look for blocks ending in a multiway branch (a GIMPLE_SWITCH),
1836 : and scan the sorted vector of cases. Combine the ones jumping to the
1837 : same label. */
1838 :
1839 : bool
1840 4551781 : group_case_labels (void)
1841 : {
1842 4551781 : basic_block bb;
1843 4551781 : bool changed = false;
1844 :
1845 39873094 : FOR_EACH_BB_FN (bb, cfun)
1846 : {
1847 105740178 : if (gswitch *stmt = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)))
1848 62496 : changed |= group_case_labels_stmt (stmt);
1849 : }
1850 :
1851 4551781 : return changed;
1852 : }
1853 :
1854 : /* Checks whether we can merge block B into block A. */
1855 :
1856 : static bool
1857 445403754 : gimple_can_merge_blocks_p (basic_block a, basic_block b)
1858 : {
1859 445403754 : gimple *stmt;
1860 :
1861 445403754 : if (!single_succ_p (a))
1862 : return false;
1863 :
1864 211439687 : if (single_succ_edge (a)->flags & EDGE_COMPLEX)
1865 : return false;
1866 :
1867 199097925 : if (single_succ (a) != b)
1868 : return false;
1869 :
1870 199097925 : if (!single_pred_p (b))
1871 : return false;
1872 :
1873 96885228 : if (a == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1874 62892764 : || b == EXIT_BLOCK_PTR_FOR_FN (cfun))
1875 : return false;
1876 :
1877 : /* If A ends by a statement causing exceptions or something similar, we
1878 : cannot merge the blocks. */
1879 34522200 : stmt = *gsi_last_nondebug_bb (a);
1880 34522200 : if (stmt && stmt_ends_bb_p (stmt))
1881 : return false;
1882 :
1883 : /* Check if the compiler-inserted nop should block merges. */
1884 26559839 : if (stmt && gimple_nop_p (stmt)
1885 9654 : && gimple_location (stmt) == DECL_SOURCE_LOCATION (cfun->decl)
1886 32611041 : && coverage_suppressed_p (a) && !coverage_suppressed_p (b))
1887 : return false;
1888 :
1889 : /* We cannot merge blocks if one has suppressed coverage and the other hasn't.
1890 : The exception is when the merge-into block is empty AND is the one with
1891 : coverage suppressed. This can happen for code like this:
1892 :
1893 : while (cond)
1894 : #pragma GCC suppress_coverage begin
1895 : foo ();
1896 : #pragma GCC suppress_coverage end
1897 : */
1898 32611005 : if (coverage_suppressed_p (a) != coverage_suppressed_p (b)
1899 32611005 : && !(coverage_suppressed_p (a) && gimple_empty_block_p (a)))
1900 : return false;
1901 :
1902 : /* Examine the labels at the beginning of B. */
1903 65414708 : for (gimple_stmt_iterator gsi = gsi_start_bb (b); !gsi_end_p (gsi);
1904 199566 : gsi_next (&gsi))
1905 : {
1906 29152291 : tree lab;
1907 29152291 : glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
1908 1925107 : if (!label_stmt)
1909 : break;
1910 1925107 : lab = gimple_label_label (label_stmt);
1911 :
1912 : /* Do not remove user forced labels or for -O0 any user labels. */
1913 1984871 : if (!DECL_ARTIFICIAL (lab) && (!optimize || FORCED_LABEL (lab)))
1914 445403754 : return false;
1915 : }
1916 :
1917 : /* Protect simple loop latches. We only want to avoid merging
1918 : the latch with the loop header or with a block in another
1919 : loop in this case. */
1920 30882030 : if (current_loops
1921 28046955 : && b->loop_father->latch == b
1922 447141 : && loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES)
1923 30919995 : && (b->loop_father->header == a
1924 26118 : || b->loop_father != a->loop_father))
1925 : return false;
1926 :
1927 : /* It must be possible to eliminate all phi nodes in B. If ssa form
1928 : is not up-to-date and a name-mapping is registered, we cannot eliminate
1929 : any phis. Symbols marked for renaming are never a problem though. */
1930 36987770 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);
1931 6117711 : gsi_next (&gsi))
1932 : {
1933 6117711 : gphi *phi = gsi.phi ();
1934 : /* Technically only new names matter. */
1935 6117711 : if (name_registered_for_update_p (PHI_RESULT (phi)))
1936 0 : return false;
1937 : }
1938 :
1939 : /* When not optimizing, don't merge if we'd lose goto_locus. */
1940 30870059 : if (!optimize
1941 30870059 : && single_succ_edge (a)->goto_locus != UNKNOWN_LOCATION)
1942 : {
1943 598696 : location_t goto_locus = single_succ_edge (a)->goto_locus;
1944 598696 : gimple_stmt_iterator prev, next;
1945 598696 : prev = gsi_last_nondebug_bb (a);
1946 598696 : next = gsi_after_labels (b);
1947 598696 : if (!gsi_end_p (next) && is_gimple_debug (gsi_stmt (next)))
1948 6 : gsi_next_nondebug (&next);
1949 598696 : if ((gsi_end_p (prev)
1950 451176 : || gimple_location (gsi_stmt (prev)) != goto_locus)
1951 1005402 : && (gsi_end_p (next)
1952 504047 : || gimple_location (gsi_stmt (next)) != goto_locus))
1953 554034 : return false;
1954 : }
1955 :
1956 : return true;
1957 : }
1958 :
1959 : /* Replaces all uses of NAME by VAL. */
1960 :
1961 : void
1962 4091718 : replace_uses_by (tree name, tree val)
1963 : {
1964 4091718 : imm_use_iterator imm_iter;
1965 4091718 : use_operand_p use;
1966 4091718 : gimple *stmt;
1967 4091718 : edge e;
1968 4091718 : auto_bitmap eh_cleanup_bbs;
1969 :
1970 11489010 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
1971 : {
1972 : /* Mark the block if we change the last stmt in it. */
1973 7397292 : if (cfgcleanup_altered_bbs
1974 7397292 : && stmt_ends_bb_p (stmt))
1975 809279 : bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
1976 :
1977 15110474 : FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1978 : {
1979 7555237 : replace_exp (use, val);
1980 :
1981 7555237 : if (gimple_code (stmt) == GIMPLE_PHI)
1982 : {
1983 2518856 : e = gimple_phi_arg_edge (as_a <gphi *> (stmt),
1984 2518856 : PHI_ARG_INDEX_FROM_USE (use));
1985 2518856 : if (e->flags & EDGE_ABNORMAL
1986 2518856 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
1987 : {
1988 : /* This can only occur for virtual operands, since
1989 : for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1990 : would prevent replacement. */
1991 0 : gcc_checking_assert (virtual_operand_p (name));
1992 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1993 : }
1994 : }
1995 : }
1996 :
1997 7397292 : if (gimple_code (stmt) != GIMPLE_PHI)
1998 : {
1999 5028418 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2000 5028418 : gimple *orig_stmt = stmt;
2001 5028418 : size_t i;
2002 :
2003 : /* FIXME. It shouldn't be required to keep TREE_CONSTANT
2004 : on ADDR_EXPRs up-to-date on GIMPLE. Propagation will
2005 : only change sth from non-invariant to invariant, and only
2006 : when propagating constants. */
2007 5028418 : if (is_gimple_min_invariant (val))
2008 1820146 : for (i = 0; i < gimple_num_ops (stmt); i++)
2009 : {
2010 1318172 : tree op = gimple_op (stmt, i);
2011 : /* Operands may be empty here. For example, the labels
2012 : of a GIMPLE_COND are nulled out following the creation
2013 : of the corresponding CFG edges. */
2014 1318172 : if (op && TREE_CODE (op) == ADDR_EXPR)
2015 66682 : recompute_tree_invariant_for_addr_expr (op);
2016 : }
2017 5028418 : update_stmt (stmt);
2018 :
2019 5028418 : if (fold_stmt (&gsi))
2020 : {
2021 538256 : stmt = gsi_stmt (gsi);
2022 538256 : update_stmt (stmt);
2023 : }
2024 :
2025 5028418 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
2026 971 : bitmap_set_bit (eh_cleanup_bbs, gimple_bb (stmt)->index);
2027 : }
2028 4091718 : }
2029 :
2030 4091718 : if (!bitmap_empty_p (eh_cleanup_bbs))
2031 932 : gimple_purge_all_dead_eh_edges (eh_cleanup_bbs);
2032 :
2033 4091718 : gcc_checking_assert (has_zero_uses (name));
2034 :
2035 : /* Also update the trees stored in loop structures. */
2036 4091718 : if (current_loops)
2037 : {
2038 140264008 : for (auto loop : loops_list (cfun, 0))
2039 132080572 : substitute_in_loop_info (loop, name, val);
2040 : }
2041 4091718 : }
2042 :
2043 : /* Merge block B into block A. */
2044 :
2045 : static void
2046 17924834 : gimple_merge_blocks (basic_block a, basic_block b)
2047 : {
2048 17924834 : gimple_stmt_iterator last, gsi;
2049 17924834 : gphi_iterator psi;
2050 :
2051 17925005 : if (coverage_suppressed_p (a) && !coverage_suppressed_p (b)
2052 17924857 : && gimple_empty_block_p (a))
2053 23 : suppress_coverage_unset (a);
2054 :
2055 17924834 : if (dump_file)
2056 21220 : fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
2057 :
2058 : /* Remove all single-valued PHI nodes from block B of the form
2059 : V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
2060 17924834 : gsi = gsi_last_bb (a);
2061 21995835 : for (psi = gsi_start_phis (b); !gsi_end_p (psi); )
2062 : {
2063 4071001 : gimple *phi = gsi_stmt (psi);
2064 4071001 : tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
2065 4071001 : gimple *copy;
2066 4071001 : bool may_replace_uses = (virtual_operand_p (def)
2067 4071001 : || may_propagate_copy (def, use));
2068 :
2069 : /* In case we maintain loop closed ssa form, do not propagate arguments
2070 : of loop exit phi nodes. */
2071 4071001 : if (current_loops
2072 4071001 : && loops_state_satisfies_p (LOOP_CLOSED_SSA)
2073 667865 : && !virtual_operand_p (def)
2074 486978 : && TREE_CODE (use) == SSA_NAME
2075 4435669 : && a->loop_father != b->loop_father)
2076 : may_replace_uses = false;
2077 :
2078 4070673 : if (!may_replace_uses)
2079 : {
2080 1296 : gcc_assert (!virtual_operand_p (def));
2081 :
2082 : /* Note that just emitting the copies is fine -- there is no problem
2083 : with ordering of phi nodes. This is because A is the single
2084 : predecessor of B, therefore results of the phi nodes cannot
2085 : appear as arguments of the phi nodes. */
2086 648 : copy = gimple_build_assign (def, use);
2087 648 : gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
2088 648 : remove_phi_node (&psi, false);
2089 : }
2090 : else
2091 : {
2092 : /* If we deal with a PHI for virtual operands, we can simply
2093 : propagate these without fussing with folding or updating
2094 : the stmt. */
2095 8140706 : if (virtual_operand_p (def))
2096 : {
2097 1568580 : imm_use_iterator iter;
2098 1568580 : use_operand_p use_p;
2099 1568580 : gimple *stmt;
2100 :
2101 4511411 : FOR_EACH_IMM_USE_STMT (stmt, iter, def)
2102 5917474 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2103 2958737 : SET_USE (use_p, use);
2104 :
2105 1568580 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2106 281 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use) = 1;
2107 : }
2108 : else
2109 2501773 : replace_uses_by (def, use);
2110 :
2111 4070353 : remove_phi_node (&psi, true);
2112 : }
2113 : }
2114 :
2115 : /* Ensure that B follows A. */
2116 17924834 : move_block_after (b, a);
2117 :
2118 17924834 : gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
2119 35849668 : gcc_assert (!*gsi_last_bb (a)
2120 : || !stmt_ends_bb_p (*gsi_last_bb (a)));
2121 :
2122 : /* Remove labels from B and set gimple_bb to A for other statements. */
2123 139713541 : for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
2124 : {
2125 103863873 : gimple *stmt = gsi_stmt (gsi);
2126 103863873 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
2127 : {
2128 116796 : tree label = gimple_label_label (label_stmt);
2129 116796 : int lp_nr;
2130 :
2131 116796 : gsi_remove (&gsi, false);
2132 :
2133 : /* Now that we can thread computed gotos, we might have
2134 : a situation where we have a forced label in block B
2135 : However, the label at the start of block B might still be
2136 : used in other ways (think about the runtime checking for
2137 : Fortran assigned gotos). So we cannot just delete the
2138 : label. Instead we move the label to the start of block A. */
2139 116796 : if (FORCED_LABEL (label))
2140 : {
2141 4709 : gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
2142 4709 : tree first_label = NULL_TREE;
2143 4709 : if (!gsi_end_p (dest_gsi))
2144 4687 : if (glabel *first_label_stmt
2145 4687 : = dyn_cast <glabel *> (gsi_stmt (dest_gsi)))
2146 4101 : first_label = gimple_label_label (first_label_stmt);
2147 4101 : if (first_label
2148 4101 : && (DECL_NONLOCAL (first_label)
2149 4101 : || EH_LANDING_PAD_NR (first_label) != 0))
2150 12 : gsi_insert_after (&dest_gsi, stmt, GSI_NEW_STMT);
2151 : else
2152 4697 : gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
2153 : }
2154 : /* Other user labels keep around in a form of a debug stmt. */
2155 112087 : else if (!DECL_ARTIFICIAL (label) && MAY_HAVE_DEBUG_BIND_STMTS)
2156 : {
2157 9108 : gimple *dbg = gimple_build_debug_bind (label,
2158 : integer_zero_node,
2159 : stmt);
2160 9108 : gimple_debug_bind_reset_value (dbg);
2161 9108 : gsi_insert_before (&gsi, dbg, GSI_SAME_STMT);
2162 : }
2163 :
2164 116796 : lp_nr = EH_LANDING_PAD_NR (label);
2165 116796 : if (lp_nr)
2166 : {
2167 9240 : eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
2168 9240 : lp->post_landing_pad = NULL;
2169 : }
2170 : }
2171 : else
2172 : {
2173 103747077 : gimple_set_bb (stmt, a);
2174 103747077 : gsi_next (&gsi);
2175 : }
2176 : }
2177 :
2178 : /* When merging two BBs, if their counts are different, the larger count
2179 : is selected as the new bb count. This is to handle inconsistent
2180 : profiles. */
2181 17924834 : if (a->loop_father == b->loop_father)
2182 : {
2183 17831945 : a->count = a->count.merge (b->count);
2184 : }
2185 :
2186 : /* Merge the sequences. */
2187 17924834 : last = gsi_last_bb (a);
2188 35849668 : gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
2189 17924834 : set_bb_seq (b, NULL);
2190 :
2191 17924834 : if (cfgcleanup_altered_bbs)
2192 17899086 : bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
2193 17924834 : }
2194 :
2195 :
2196 : /* Return the one of two successors of BB that is not reachable by a
2197 : complex edge, if there is one. Else, return BB. We use
2198 : this in optimizations that use post-dominators for their heuristics,
2199 : to catch the cases in C++ where function calls are involved. */
2200 :
2201 : basic_block
2202 6 : single_noncomplex_succ (basic_block bb)
2203 : {
2204 6 : edge e0, e1;
2205 6 : if (EDGE_COUNT (bb->succs) != 2)
2206 : return bb;
2207 :
2208 6 : e0 = EDGE_SUCC (bb, 0);
2209 6 : e1 = EDGE_SUCC (bb, 1);
2210 6 : if (e0->flags & EDGE_COMPLEX)
2211 6 : return e1->dest;
2212 0 : if (e1->flags & EDGE_COMPLEX)
2213 0 : return e0->dest;
2214 :
2215 : return bb;
2216 : }
2217 :
2218 : /* T is CALL_EXPR. Set current_function_calls_* flags. */
2219 :
2220 : void
2221 55369237 : notice_special_calls (gcall *call)
2222 : {
2223 55369237 : int flags = gimple_call_flags (call);
2224 :
2225 55369237 : if (flags & ECF_MAY_BE_ALLOCA)
2226 161485 : cfun->calls_alloca = true;
2227 55369237 : if (flags & ECF_RETURNS_TWICE)
2228 8263 : cfun->calls_setjmp = true;
2229 55369237 : if (gimple_call_must_tail_p (call))
2230 3631 : cfun->has_musttail = true;
2231 55369237 : }
2232 :
2233 :
2234 : /* Clear flags set by notice_special_calls. Used by dead code removal
2235 : to update the flags. */
2236 :
2237 : void
2238 8337506 : clear_special_calls (void)
2239 : {
2240 8337506 : cfun->calls_alloca = false;
2241 8337506 : cfun->calls_setjmp = false;
2242 8337506 : cfun->has_musttail = false;
2243 8337506 : }
2244 :
2245 : /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
2246 :
2247 : static void
2248 38199555 : remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
2249 : {
2250 : /* Since this block is no longer reachable, we can just delete all
2251 : of its PHI nodes. */
2252 38199555 : remove_phi_nodes (bb);
2253 :
2254 : /* Remove edges to BB's successors. */
2255 112535594 : while (EDGE_COUNT (bb->succs) > 0)
2256 36136484 : remove_edge (EDGE_SUCC (bb, 0));
2257 38199555 : }
2258 :
2259 :
2260 : /* Remove statements of basic block BB. */
2261 :
2262 : static void
2263 38199555 : remove_bb (basic_block bb)
2264 : {
2265 38199555 : gimple_stmt_iterator i;
2266 :
2267 38199555 : if (dump_file)
2268 : {
2269 50464 : fprintf (dump_file, "Removing basic block %d\n", bb->index);
2270 50464 : if (dump_flags & TDF_DETAILS)
2271 : {
2272 35605 : dump_bb (dump_file, bb, 0, TDF_BLOCKS);
2273 35605 : fprintf (dump_file, "\n");
2274 : }
2275 : }
2276 :
2277 38199555 : if (current_loops)
2278 : {
2279 36235576 : class loop *loop = bb->loop_father;
2280 :
2281 : /* If a loop gets removed, clean up the information associated
2282 : with it. */
2283 36235576 : if (loop->latch == bb
2284 36023325 : || loop->header == bb)
2285 256579 : free_numbers_of_iterations_estimates (loop);
2286 : }
2287 :
2288 : /* Remove all the instructions in the block. */
2289 38199555 : if (bb_seq (bb) != NULL)
2290 : {
2291 : /* Walk backwards so as to get a chance to substitute all
2292 : released DEFs into debug stmts. See
2293 : eliminate_unnecessary_stmts() in tree-ssa-dce.cc for more
2294 : details. */
2295 29735954 : for (i = gsi_last_bb (bb); !gsi_end_p (i);)
2296 : {
2297 22605256 : gimple *stmt = gsi_stmt (i);
2298 22605256 : glabel *label_stmt = dyn_cast <glabel *> (stmt);
2299 2038124 : if (label_stmt
2300 2038124 : && (FORCED_LABEL (gimple_label_label (label_stmt))
2301 2035143 : || DECL_NONLOCAL (gimple_label_label (label_stmt))))
2302 : {
2303 2983 : basic_block new_bb;
2304 2983 : gimple_stmt_iterator new_gsi;
2305 :
2306 : /* A non-reachable non-local label may still be referenced.
2307 : But it no longer needs to carry the extra semantics of
2308 : non-locality. */
2309 2983 : if (DECL_NONLOCAL (gimple_label_label (label_stmt)))
2310 : {
2311 2 : DECL_NONLOCAL (gimple_label_label (label_stmt)) = 0;
2312 2 : FORCED_LABEL (gimple_label_label (label_stmt)) = 1;
2313 : }
2314 :
2315 2983 : new_bb = bb->prev_bb;
2316 : /* Don't move any labels into ENTRY block. */
2317 2983 : if (new_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2318 : {
2319 0 : new_bb = single_succ (new_bb);
2320 0 : gcc_assert (new_bb != bb);
2321 : }
2322 2983 : if ((unsigned) bb->index < bb_to_omp_idx.length ()
2323 8 : && ((unsigned) new_bb->index >= bb_to_omp_idx.length ()
2324 8 : || (bb_to_omp_idx[bb->index]
2325 8 : != bb_to_omp_idx[new_bb->index])))
2326 : {
2327 : /* During cfg pass make sure to put orphaned labels
2328 : into the right OMP region. */
2329 : unsigned int i;
2330 : int idx;
2331 24 : new_bb = NULL;
2332 24 : FOR_EACH_VEC_ELT (bb_to_omp_idx, i, idx)
2333 24 : if (i >= NUM_FIXED_BLOCKS
2334 8 : && idx == bb_to_omp_idx[bb->index]
2335 32 : && i != (unsigned) bb->index)
2336 : {
2337 8 : new_bb = BASIC_BLOCK_FOR_FN (cfun, i);
2338 8 : break;
2339 : }
2340 8 : if (new_bb == NULL)
2341 : {
2342 0 : new_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2343 0 : gcc_assert (new_bb != bb);
2344 : }
2345 : }
2346 2983 : new_gsi = gsi_after_labels (new_bb);
2347 2983 : gsi_remove (&i, false);
2348 2983 : gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
2349 : }
2350 : else
2351 : {
2352 : /* Release SSA definitions. */
2353 22602273 : release_defs (stmt);
2354 22602273 : gsi_remove (&i, true);
2355 : }
2356 :
2357 22605256 : if (gsi_end_p (i))
2358 45210512 : i = gsi_last_bb (bb);
2359 : else
2360 0 : gsi_prev (&i);
2361 : }
2362 : }
2363 :
2364 38199555 : if ((unsigned) bb->index < bb_to_omp_idx.length ())
2365 13828 : bb_to_omp_idx[bb->index] = -1;
2366 38199555 : remove_phi_nodes_and_edges_for_unreachable_block (bb);
2367 38199555 : bb->il.gimple.seq = NULL;
2368 38199555 : bb->il.gimple.phi_nodes = NULL;
2369 38199555 : }
2370 :
2371 :
2372 : /* Given a basic block BB and a value VAL for use in the final statement
2373 : of the block (if a GIMPLE_COND, GIMPLE_SWITCH, or computed goto), return
2374 : the edge that will be taken out of the block.
2375 : If VAL is NULL_TREE, then the current value of the final statement's
2376 : predicate or index is used.
2377 : If the value does not match a unique edge, NULL is returned. */
2378 :
2379 : edge
2380 260672157 : find_taken_edge (basic_block bb, tree val)
2381 : {
2382 260672157 : gimple *stmt;
2383 :
2384 260672157 : stmt = *gsi_last_bb (bb);
2385 :
2386 : /* Handle ENTRY and EXIT. */
2387 260672157 : if (!stmt)
2388 : ;
2389 :
2390 256924143 : else if (gimple_code (stmt) == GIMPLE_COND)
2391 209880502 : return find_taken_edge_cond_expr (as_a <gcond *> (stmt), val);
2392 :
2393 47043641 : else if (gimple_code (stmt) == GIMPLE_SWITCH)
2394 1215269 : return find_taken_edge_switch_expr (as_a <gswitch *> (stmt), val);
2395 :
2396 45828372 : else if (computed_goto_p (stmt))
2397 : {
2398 : /* Only optimize if the argument is a label, if the argument is
2399 : not a label then we cannot construct a proper CFG.
2400 :
2401 : It may be the case that we only need to allow the LABEL_REF to
2402 : appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
2403 : appear inside a LABEL_EXPR just to be safe. */
2404 3329 : if (val
2405 1884 : && (TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
2406 3777 : && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
2407 359 : return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
2408 : }
2409 :
2410 : /* Otherwise we only know the taken successor edge if it's unique. */
2411 49576027 : return single_succ_p (bb) ? single_succ_edge (bb) : NULL;
2412 : }
2413 :
2414 : /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2415 : statement, determine which of the outgoing edges will be taken out of the
2416 : block. Return NULL if either edge may be taken. */
2417 :
2418 : static edge
2419 359 : find_taken_edge_computed_goto (basic_block bb, tree val)
2420 : {
2421 359 : basic_block dest;
2422 359 : edge e = NULL;
2423 :
2424 359 : dest = label_to_block (cfun, val);
2425 359 : if (dest)
2426 355 : e = find_edge (bb, dest);
2427 :
2428 : /* It's possible for find_edge to return NULL here on invalid code
2429 : that abuses the labels-as-values extension (e.g. code that attempts to
2430 : jump *between* functions via stored labels-as-values; PR 84136).
2431 : If so, then we simply return that NULL for the edge.
2432 : We don't currently have a way of detecting such invalid code, so we
2433 : can't assert that it was the case when a NULL edge occurs here. */
2434 :
2435 359 : return e;
2436 : }
2437 :
2438 : /* Given COND_STMT and a constant value VAL for use as the predicate,
2439 : determine which of the two edges will be taken out of
2440 : the statement's block. Return NULL if either edge may be taken.
2441 : If VAL is NULL_TREE, then the current value of COND_STMT's predicate
2442 : is used. */
2443 :
2444 : static edge
2445 209880502 : find_taken_edge_cond_expr (const gcond *cond_stmt, tree val)
2446 : {
2447 209880502 : edge true_edge, false_edge;
2448 :
2449 209880502 : if (val == NULL_TREE)
2450 : {
2451 : /* Use the current value of the predicate. */
2452 195274790 : if (gimple_cond_true_p (cond_stmt))
2453 77610 : val = integer_one_node;
2454 195197180 : else if (gimple_cond_false_p (cond_stmt))
2455 171924 : val = integer_zero_node;
2456 : else
2457 : return NULL;
2458 : }
2459 14605712 : else if (TREE_CODE (val) != INTEGER_CST)
2460 : return NULL;
2461 :
2462 13594425 : extract_true_false_edges_from_block (gimple_bb (cond_stmt),
2463 : &true_edge, &false_edge);
2464 :
2465 13594425 : return (integer_zerop (val) ? false_edge : true_edge);
2466 : }
2467 :
2468 : /* Given SWITCH_STMT and an INTEGER_CST VAL for use as the index, determine
2469 : which edge will be taken out of the statement's block. Return NULL if any
2470 : edge may be taken.
2471 : If VAL is NULL_TREE, then the current value of SWITCH_STMT's index
2472 : is used. */
2473 :
2474 : edge
2475 1215419 : find_taken_edge_switch_expr (const gswitch *switch_stmt, tree val)
2476 : {
2477 1215419 : basic_block dest_bb;
2478 1215419 : edge e;
2479 1215419 : tree taken_case;
2480 :
2481 1215419 : if (gimple_switch_num_labels (switch_stmt) == 1)
2482 0 : taken_case = gimple_switch_default_label (switch_stmt);
2483 : else
2484 : {
2485 1215419 : if (val == NULL_TREE)
2486 155661 : val = gimple_switch_index (switch_stmt);
2487 1215419 : if (TREE_CODE (val) != INTEGER_CST)
2488 : return NULL;
2489 : else
2490 24781 : taken_case = find_case_label_for_value (switch_stmt, val);
2491 : }
2492 24781 : dest_bb = label_to_block (cfun, CASE_LABEL (taken_case));
2493 :
2494 24781 : e = find_edge (gimple_bb (switch_stmt), dest_bb);
2495 24781 : gcc_assert (e);
2496 : return e;
2497 : }
2498 :
2499 :
2500 : /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
2501 : We can make optimal use here of the fact that the case labels are
2502 : sorted: We can do a binary search for a case matching VAL. */
2503 :
2504 : tree
2505 24781 : find_case_label_for_value (const gswitch *switch_stmt, tree val)
2506 : {
2507 24781 : size_t low, high, n = gimple_switch_num_labels (switch_stmt);
2508 24781 : tree default_case = gimple_switch_default_label (switch_stmt);
2509 :
2510 77531 : for (low = 0, high = n; high - low > 1; )
2511 : {
2512 48870 : size_t i = (high + low) / 2;
2513 48870 : tree t = gimple_switch_label (switch_stmt, i);
2514 48870 : int cmp;
2515 :
2516 : /* Cache the result of comparing CASE_LOW and val. */
2517 48870 : cmp = tree_int_cst_compare (CASE_LOW (t), val);
2518 :
2519 48870 : if (cmp > 0)
2520 : high = i;
2521 : else
2522 35433 : low = i;
2523 :
2524 48870 : if (CASE_HIGH (t) == NULL)
2525 : {
2526 : /* A single-valued case label. */
2527 47770 : if (cmp == 0)
2528 : return t;
2529 : }
2530 : else
2531 : {
2532 : /* A case range. We can only handle integer ranges. */
2533 1100 : if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2534 : return t;
2535 : }
2536 : }
2537 :
2538 : return default_case;
2539 : }
2540 :
2541 :
2542 : /* Dump a basic block on stderr. */
2543 :
2544 : void
2545 0 : gimple_debug_bb (basic_block bb)
2546 : {
2547 0 : dump_bb (stderr, bb, 0, TDF_VOPS|TDF_MEMSYMS|TDF_BLOCKS);
2548 0 : }
2549 :
2550 :
2551 : /* Dump basic block with index N on stderr. */
2552 :
2553 : basic_block
2554 0 : gimple_debug_bb_n (int n)
2555 : {
2556 0 : gimple_debug_bb (BASIC_BLOCK_FOR_FN (cfun, n));
2557 0 : return BASIC_BLOCK_FOR_FN (cfun, n);
2558 : }
2559 :
2560 :
2561 : /* Dump the CFG on stderr.
2562 :
2563 : FLAGS are the same used by the tree dumping functions
2564 : (see TDF_* in dumpfile.h). */
2565 :
2566 : void
2567 0 : gimple_debug_cfg (dump_flags_t flags)
2568 : {
2569 0 : gimple_dump_cfg (stderr, flags);
2570 0 : }
2571 :
2572 :
2573 : /* Dump the program showing basic block boundaries on the given FILE.
2574 :
2575 : FLAGS are the same used by the tree dumping functions (see TDF_* in
2576 : tree.h). */
2577 :
2578 : void
2579 201 : gimple_dump_cfg (FILE *file, dump_flags_t flags)
2580 : {
2581 201 : if (flags & TDF_DETAILS)
2582 : {
2583 2 : dump_function_header (file, current_function_decl, flags);
2584 2 : fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2585 : n_basic_blocks_for_fn (cfun), n_edges_for_fn (cfun),
2586 2 : last_basic_block_for_fn (cfun));
2587 :
2588 2 : brief_dump_cfg (file, flags);
2589 2 : fprintf (file, "\n");
2590 : }
2591 :
2592 201 : if (flags & TDF_STATS)
2593 44 : dump_cfg_stats (file);
2594 :
2595 201 : dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2596 201 : }
2597 :
2598 :
2599 : /* Dump CFG statistics on FILE. */
2600 :
2601 : void
2602 44 : dump_cfg_stats (FILE *file)
2603 : {
2604 44 : static long max_num_merged_labels = 0;
2605 44 : unsigned long size, total = 0;
2606 44 : long num_edges;
2607 44 : basic_block bb;
2608 44 : const char * const fmt_str = "%-30s%-13s%12s\n";
2609 44 : const char * const fmt_str_1 = "%-30s%13d" PRsa (11) "\n";
2610 44 : const char * const fmt_str_2 = "%-30s%13ld" PRsa (11) "\n";
2611 44 : const char * const fmt_str_3 = "%-43s" PRsa (11) "\n";
2612 44 : const char *funcname = current_function_name ();
2613 :
2614 44 : fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2615 :
2616 44 : fprintf (file, "---------------------------------------------------------\n");
2617 44 : fprintf (file, fmt_str, "", " Number of ", "Memory");
2618 44 : fprintf (file, fmt_str, "", " instances ", "used ");
2619 44 : fprintf (file, "---------------------------------------------------------\n");
2620 :
2621 44 : size = n_basic_blocks_for_fn (cfun) * sizeof (struct basic_block_def);
2622 44 : total += size;
2623 44 : fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks_for_fn (cfun),
2624 0 : SIZE_AMOUNT (size));
2625 :
2626 44 : num_edges = 0;
2627 122 : FOR_EACH_BB_FN (bb, cfun)
2628 156 : num_edges += EDGE_COUNT (bb->succs);
2629 44 : size = num_edges * sizeof (class edge_def);
2630 44 : total += size;
2631 44 : fprintf (file, fmt_str_2, "Edges", num_edges, SIZE_AMOUNT (size));
2632 :
2633 44 : fprintf (file, "---------------------------------------------------------\n");
2634 44 : fprintf (file, fmt_str_3, "Total memory used by CFG data",
2635 0 : SIZE_AMOUNT (total));
2636 44 : fprintf (file, "---------------------------------------------------------\n");
2637 44 : fprintf (file, "\n");
2638 :
2639 44 : if (cfg_stats.num_merged_labels > max_num_merged_labels)
2640 0 : max_num_merged_labels = cfg_stats.num_merged_labels;
2641 :
2642 44 : fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2643 : cfg_stats.num_merged_labels, max_num_merged_labels);
2644 :
2645 44 : fprintf (file, "\n");
2646 44 : }
2647 :
2648 :
2649 : /* Dump CFG statistics on stderr. Keep extern so that it's always
2650 : linked in the final executable. */
2651 :
2652 : DEBUG_FUNCTION void
2653 0 : debug_cfg_stats (void)
2654 : {
2655 0 : dump_cfg_stats (stderr);
2656 0 : }
2657 :
2658 : /*---------------------------------------------------------------------------
2659 : Miscellaneous helpers
2660 : ---------------------------------------------------------------------------*/
2661 :
2662 : /* Return true if T, a GIMPLE_CALL, can make an abnormal transfer of control
2663 : flow. Transfers of control flow associated with EH are excluded. */
2664 :
2665 : static bool
2666 149028338 : call_can_make_abnormal_goto (gimple *t)
2667 : {
2668 : /* If the function has no non-local labels, then a call cannot make an
2669 : abnormal transfer of control. */
2670 149028338 : if (!cfun->has_nonlocal_label
2671 148914117 : && !cfun->calls_setjmp)
2672 : return false;
2673 :
2674 : /* Likewise if the call has no side effects. */
2675 230056 : if (!gimple_has_side_effects (t))
2676 : return false;
2677 :
2678 : /* Likewise if the called function is leaf. */
2679 221866 : if (gimple_call_flags (t) & ECF_LEAF)
2680 51947 : return false;
2681 :
2682 : return true;
2683 : }
2684 :
2685 :
2686 : /* Return true if T can make an abnormal transfer of control flow.
2687 : Transfers of control flow associated with EH are excluded. */
2688 :
2689 : bool
2690 637053013 : stmt_can_make_abnormal_goto (gimple *t)
2691 : {
2692 637053013 : if (computed_goto_p (t))
2693 : return true;
2694 637048714 : if (is_gimple_call (t))
2695 137556960 : return call_can_make_abnormal_goto (t);
2696 : return false;
2697 : }
2698 :
2699 :
2700 : /* Return true if T represents a stmt that always transfers control. */
2701 :
2702 : bool
2703 17914287737 : is_ctrl_stmt (gimple *t)
2704 : {
2705 17914287737 : switch (gimple_code (t))
2706 : {
2707 : case GIMPLE_COND:
2708 : case GIMPLE_SWITCH:
2709 : case GIMPLE_GOTO:
2710 : case GIMPLE_RETURN:
2711 : case GIMPLE_RESX:
2712 : return true;
2713 15612335309 : default:
2714 15612335309 : return false;
2715 : }
2716 : }
2717 :
2718 :
2719 : /* Return true if T is a statement that may alter the flow of control
2720 : (e.g., a call to a non-returning function). */
2721 :
2722 : bool
2723 14873012158 : is_ctrl_altering_stmt (gimple *t)
2724 : {
2725 14873012158 : gcc_assert (t);
2726 :
2727 14873012158 : switch (gimple_code (t))
2728 : {
2729 1140638772 : case GIMPLE_CALL:
2730 : /* Per stmt call flag indicates whether the call could alter
2731 : controlflow. */
2732 1140638772 : if (gimple_call_ctrl_altering_p (t))
2733 : return true;
2734 : break;
2735 :
2736 : case GIMPLE_EH_DISPATCH:
2737 : /* EH_DISPATCH branches to the individual catch handlers at
2738 : this level of a try or allowed-exceptions region. It can
2739 : fallthru to the next statement as well. */
2740 : return true;
2741 :
2742 13073898 : case GIMPLE_ASM:
2743 13073898 : if (gimple_asm_nlabels (as_a <gasm *> (t)) > 0)
2744 : return true;
2745 : break;
2746 :
2747 : CASE_GIMPLE_OMP:
2748 : /* OpenMP directives alter control flow. */
2749 : return true;
2750 :
2751 : case GIMPLE_TRANSACTION:
2752 : /* A transaction start alters control flow. */
2753 : return true;
2754 :
2755 : default:
2756 : break;
2757 : }
2758 :
2759 : /* If a statement can throw, it alters control flow. */
2760 14696390246 : return stmt_can_throw_internal (cfun, t);
2761 : }
2762 :
2763 :
2764 : /* Return true if T is a simple local goto. */
2765 :
2766 : bool
2767 6410541 : simple_goto_p (gimple *t)
2768 : {
2769 6410541 : return (gimple_code (t) == GIMPLE_GOTO
2770 6410541 : && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
2771 : }
2772 :
2773 :
2774 : /* Return true if STMT should start a new basic block. PREV_STMT is
2775 : the statement preceding STMT. It is used when STMT is a label or a
2776 : case label. Labels should only start a new basic block if their
2777 : previous statement wasn't a label. Otherwise, sequence of labels
2778 : would generate unnecessary basic blocks that only contain a single
2779 : label. */
2780 :
2781 : static inline bool
2782 73924631 : stmt_starts_bb_p (gimple *stmt, gimple *prev_stmt)
2783 : {
2784 73924631 : if (stmt == NULL)
2785 : return false;
2786 :
2787 : /* PREV_STMT is only set to a debug stmt if the debug stmt is before
2788 : any nondebug stmts in the block. We don't want to start another
2789 : block in this case: the debug stmt will already have started the
2790 : one STMT would start if we weren't outputting debug stmts. */
2791 73924631 : if (prev_stmt && is_gimple_debug (prev_stmt))
2792 : return false;
2793 :
2794 : /* Labels start a new basic block only if the preceding statement
2795 : wasn't a label of the same type. This prevents the creation of
2796 : consecutive blocks that have nothing but a single label. */
2797 73478441 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
2798 : {
2799 : /* Nonlocal and computed GOTO targets always start a new block. */
2800 4210680 : if (DECL_NONLOCAL (gimple_label_label (label_stmt))
2801 4210680 : || FORCED_LABEL (gimple_label_label (label_stmt)))
2802 : return true;
2803 :
2804 4196854 : if (glabel *plabel = safe_dyn_cast <glabel *> (prev_stmt))
2805 : {
2806 2279435 : if (DECL_NONLOCAL (gimple_label_label (plabel))
2807 2279435 : || !DECL_ARTIFICIAL (gimple_label_label (plabel)))
2808 : return true;
2809 :
2810 2267602 : cfg_stats.num_merged_labels++;
2811 2267602 : return false;
2812 : }
2813 : else
2814 : return true;
2815 : }
2816 69267761 : else if (gimple_code (stmt) == GIMPLE_CALL)
2817 : {
2818 9674757 : if (gimple_call_flags (stmt) & ECF_RETURNS_TWICE)
2819 : /* setjmp acts similar to a nonlocal GOTO target and thus should
2820 : start a new block. */
2821 : return true;
2822 9673539 : if (gimple_call_internal_p (stmt, IFN_PHI)
2823 0 : && prev_stmt
2824 0 : && gimple_code (prev_stmt) != GIMPLE_LABEL
2825 9673539 : && (gimple_code (prev_stmt) != GIMPLE_CALL
2826 0 : || ! gimple_call_internal_p (prev_stmt, IFN_PHI)))
2827 : /* PHI nodes start a new block unless preceded by a label
2828 : or another PHI. */
2829 0 : return true;
2830 : }
2831 :
2832 : return false;
2833 : }
2834 :
2835 :
2836 : /* Return true if T should end a basic block. */
2837 :
2838 : bool
2839 15857930055 : stmt_ends_bb_p (gimple *t)
2840 : {
2841 15857930055 : return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2842 : }
2843 :
2844 : /* Remove block annotations and other data structures. */
2845 :
2846 : void
2847 3315950 : delete_tree_cfg_annotations (struct function *fn)
2848 : {
2849 3315950 : vec_free (label_to_block_map_for_fn (fn));
2850 3315950 : }
2851 :
2852 : /* Return the virtual phi in BB. */
2853 :
2854 : gphi *
2855 2881006151 : get_virtual_phi (basic_block bb)
2856 : {
2857 2881006151 : for (gphi_iterator gsi = gsi_start_phis (bb);
2858 3570504439 : !gsi_end_p (gsi);
2859 689498288 : gsi_next (&gsi))
2860 : {
2861 1541914934 : gphi *phi = gsi.phi ();
2862 :
2863 3083829868 : if (virtual_operand_p (PHI_RESULT (phi)))
2864 852416646 : return phi;
2865 : }
2866 :
2867 2028589505 : return NULL;
2868 : }
2869 :
2870 : /* Return the first statement in basic block BB. */
2871 :
2872 : gimple *
2873 158116688 : first_stmt (basic_block bb)
2874 : {
2875 158116688 : gimple_stmt_iterator i = gsi_start_bb (bb);
2876 158116688 : gimple *stmt = NULL;
2877 :
2878 575109861 : while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2879 : {
2880 416993173 : gsi_next (&i);
2881 416993173 : stmt = NULL;
2882 : }
2883 158116688 : return stmt;
2884 : }
2885 :
2886 : /* Return the last statement in basic block BB. */
2887 :
2888 : gimple *
2889 189647677 : last_nondebug_stmt (basic_block bb)
2890 : {
2891 189647677 : gimple_stmt_iterator i = gsi_last_bb (bb);
2892 189647677 : gimple *stmt = NULL;
2893 :
2894 239983901 : while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
2895 : {
2896 50336224 : gsi_prev (&i);
2897 50336224 : stmt = NULL;
2898 : }
2899 189647677 : return stmt;
2900 : }
2901 :
2902 : /* Return the last statement of an otherwise empty block. Return NULL
2903 : if the block is totally empty, or if it contains more than one
2904 : statement. */
2905 :
2906 : gimple *
2907 17220787 : last_and_only_stmt (basic_block bb)
2908 : {
2909 17220787 : gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
2910 17220787 : gimple *last, *prev;
2911 :
2912 17220787 : if (gsi_end_p (i))
2913 : return NULL;
2914 :
2915 16754286 : last = gsi_stmt (i);
2916 16754286 : gsi_prev_nondebug (&i);
2917 16754286 : if (gsi_end_p (i))
2918 : return last;
2919 :
2920 : /* Empty statements should no longer appear in the instruction stream.
2921 : Everything that might have appeared before should be deleted by
2922 : remove_useless_stmts, and the optimizers should just gsi_remove
2923 : instead of smashing with build_empty_stmt.
2924 :
2925 : Thus the only thing that should appear here in a block containing
2926 : one executable statement is a label. */
2927 12763307 : prev = gsi_stmt (i);
2928 12763307 : if (gimple_code (prev) == GIMPLE_LABEL)
2929 : return last;
2930 : else
2931 12427109 : return NULL;
2932 : }
2933 :
2934 : /* Returns the basic block after which the new basic block created
2935 : by splitting edge EDGE_IN should be placed. Tries to keep the new block
2936 : near its "logical" location. This is of most help to humans looking
2937 : at debugging dumps. */
2938 :
2939 : basic_block
2940 28959432 : split_edge_bb_loc (edge edge_in)
2941 : {
2942 28959432 : basic_block dest = edge_in->dest;
2943 28959432 : basic_block dest_prev = dest->prev_bb;
2944 :
2945 28959432 : if (dest_prev)
2946 : {
2947 28959432 : edge e = find_edge (dest_prev, dest);
2948 28959432 : if (e && !(e->flags & EDGE_COMPLEX))
2949 24503662 : return edge_in->src;
2950 : }
2951 : return dest_prev;
2952 : }
2953 :
2954 : /* Split a (typically critical) edge EDGE_IN. Return the new block.
2955 : Abort on abnormal edges. */
2956 :
2957 : static basic_block
2958 27061727 : gimple_split_edge (edge edge_in)
2959 : {
2960 27061727 : basic_block new_bb, after_bb, dest;
2961 27061727 : edge new_edge, e;
2962 :
2963 : /* Abnormal edges cannot be split. */
2964 27061727 : gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
2965 :
2966 27061727 : dest = edge_in->dest;
2967 :
2968 27061727 : after_bb = split_edge_bb_loc (edge_in);
2969 :
2970 27061727 : new_bb = create_empty_bb (after_bb);
2971 27061727 : new_bb->count = edge_in->count ();
2972 27061727 : if (coverage_suppressed_p (after_bb))
2973 73 : suppress_coverage (new_bb);
2974 :
2975 : /* We want to avoid re-allocating PHIs when we first
2976 : add the fallthru edge from new_bb to dest but we also
2977 : want to avoid changing PHI argument order when
2978 : first redirecting edge_in away from dest. The former
2979 : avoids changing PHI argument order by adding them
2980 : last and then the redirection swapping it back into
2981 : place by means of unordered remove.
2982 : So hack around things by temporarily removing all PHIs
2983 : from the destination during the edge redirection and then
2984 : making sure the edges stay in order. */
2985 27061727 : gimple_seq saved_phis = phi_nodes (dest);
2986 27061727 : unsigned old_dest_idx = edge_in->dest_idx;
2987 27061727 : set_phi_nodes (dest, NULL);
2988 27061727 : new_edge = make_single_succ_edge (new_bb, dest, EDGE_FALLTHRU);
2989 27061727 : e = redirect_edge_and_branch (edge_in, new_bb);
2990 27061727 : gcc_assert (e == edge_in && new_edge->dest_idx == old_dest_idx);
2991 : /* set_phi_nodes sets the BB of the PHI nodes, so do it manually here. */
2992 27061727 : dest->il.gimple.phi_nodes = saved_phis;
2993 :
2994 27061727 : return new_bb;
2995 : }
2996 :
2997 :
2998 : /* Verify properties of the address expression T whose base should be
2999 : TREE_ADDRESSABLE if VERIFY_ADDRESSABLE is true. */
3000 :
3001 : static bool
3002 633395511 : verify_address (tree t, bool verify_addressable)
3003 : {
3004 633395511 : bool old_constant;
3005 633395511 : bool old_side_effects;
3006 633395511 : bool new_constant;
3007 633395511 : bool new_side_effects;
3008 :
3009 633395511 : old_constant = TREE_CONSTANT (t);
3010 633395511 : old_side_effects = TREE_SIDE_EFFECTS (t);
3011 :
3012 633395511 : recompute_tree_invariant_for_addr_expr (t);
3013 633395511 : new_side_effects = TREE_SIDE_EFFECTS (t);
3014 633395511 : new_constant = TREE_CONSTANT (t);
3015 :
3016 633395511 : if (old_constant != new_constant)
3017 : {
3018 0 : error ("constant not recomputed when %<ADDR_EXPR%> changed");
3019 0 : return true;
3020 : }
3021 633395511 : if (old_side_effects != new_side_effects)
3022 : {
3023 0 : error ("side effects not recomputed when %<ADDR_EXPR%> changed");
3024 0 : return true;
3025 : }
3026 :
3027 633395511 : tree base = TREE_OPERAND (t, 0);
3028 783576204 : while (handled_component_p (base))
3029 150180693 : base = TREE_OPERAND (base, 0);
3030 :
3031 633395511 : if (!(VAR_P (base)
3032 : || TREE_CODE (base) == PARM_DECL
3033 : || TREE_CODE (base) == RESULT_DECL))
3034 : return false;
3035 :
3036 462604223 : if (verify_addressable && !TREE_ADDRESSABLE (base))
3037 : {
3038 0 : error ("address taken but %<TREE_ADDRESSABLE%> bit not set");
3039 0 : return true;
3040 : }
3041 :
3042 : return false;
3043 : }
3044 :
3045 :
3046 : /* Verify if EXPR is a valid GIMPLE reference expression. If
3047 : REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
3048 : if there is an error, otherwise false. */
3049 :
3050 : static bool
3051 3865613305 : verify_types_in_gimple_reference (tree expr, bool require_lvalue)
3052 : {
3053 3865613305 : const char *code_name = get_tree_code_name (TREE_CODE (expr));
3054 :
3055 3865613305 : if (TREE_CODE (expr) == REALPART_EXPR
3056 3865613305 : || TREE_CODE (expr) == IMAGPART_EXPR
3057 3826858217 : || TREE_CODE (expr) == BIT_FIELD_REF
3058 3811529894 : || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
3059 : {
3060 99416403 : tree op = TREE_OPERAND (expr, 0);
3061 99416403 : if (TREE_CODE (expr) != VIEW_CONVERT_EXPR
3062 99416403 : && !is_gimple_reg_type (TREE_TYPE (expr)))
3063 : {
3064 0 : error ("non-scalar %qs", code_name);
3065 0 : return true;
3066 : }
3067 :
3068 99416403 : if (TREE_CODE (expr) == BIT_FIELD_REF)
3069 : {
3070 15328323 : tree t1 = TREE_OPERAND (expr, 1);
3071 15328323 : tree t2 = TREE_OPERAND (expr, 2);
3072 15328323 : poly_uint64 size, bitpos;
3073 15328323 : if (!poly_int_tree_p (t1, &size)
3074 15328323 : || !poly_int_tree_p (t2, &bitpos)
3075 15328323 : || !types_compatible_p (bitsizetype, TREE_TYPE (t1))
3076 30656646 : || !types_compatible_p (bitsizetype, TREE_TYPE (t2)))
3077 : {
3078 0 : error ("invalid position or size operand to %qs", code_name);
3079 0 : return true;
3080 : }
3081 30617968 : if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
3082 28061171 : && maybe_ne (TYPE_PRECISION (TREE_TYPE (expr)), size))
3083 : {
3084 0 : error ("integral result type precision does not match "
3085 : "field size of %qs", code_name);
3086 0 : return true;
3087 : }
3088 15328323 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
3089 2556797 : && TYPE_MODE (TREE_TYPE (expr)) != BLKmode
3090 5110782 : && maybe_ne (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr))),
3091 : size))
3092 : {
3093 0 : error ("mode size of non-integral result does not "
3094 : "match field size of %qs",
3095 : code_name);
3096 0 : return true;
3097 : }
3098 30655940 : if (INTEGRAL_TYPE_P (TREE_TYPE (op))
3099 15561916 : && !type_has_mode_precision_p (TREE_TYPE (op)))
3100 : {
3101 0 : error ("%qs of non-mode-precision operand", code_name);
3102 0 : return true;
3103 : }
3104 15328323 : if (!AGGREGATE_TYPE_P (TREE_TYPE (op))
3105 15328323 : && known_gt (size + bitpos,
3106 : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (op)))))
3107 : {
3108 0 : error ("position plus size exceeds size of referenced object in "
3109 : "%qs", code_name);
3110 0 : return true;
3111 : }
3112 : }
3113 :
3114 99416403 : if ((TREE_CODE (expr) == REALPART_EXPR
3115 99416403 : || TREE_CODE (expr) == IMAGPART_EXPR)
3116 138171491 : && !useless_type_conversion_p (TREE_TYPE (expr),
3117 38755088 : TREE_TYPE (TREE_TYPE (op))))
3118 : {
3119 0 : error ("type mismatch in %qs reference", code_name);
3120 0 : debug_generic_stmt (TREE_TYPE (expr));
3121 0 : debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3122 0 : return true;
3123 : }
3124 :
3125 99416403 : if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
3126 : {
3127 : /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
3128 : that their operand is not a register an invariant when
3129 : requiring an lvalue (this usually means there is a SRA or IPA-SRA
3130 : bug). Otherwise there is nothing to verify, gross mismatches at
3131 : most invoke undefined behavior. */
3132 45332992 : if (require_lvalue
3133 45332992 : && (is_gimple_reg (op) || is_gimple_min_invariant (op)))
3134 : {
3135 0 : error ("conversion of %qs on the left hand side of %qs",
3136 0 : get_tree_code_name (TREE_CODE (op)), code_name);
3137 0 : debug_generic_stmt (expr);
3138 0 : return true;
3139 : }
3140 45332992 : else if (is_gimple_reg (op)
3141 45332992 : && TYPE_SIZE (TREE_TYPE (expr)) != TYPE_SIZE (TREE_TYPE (op)))
3142 : {
3143 0 : error ("conversion of register to a different size in %qs",
3144 : code_name);
3145 0 : debug_generic_stmt (expr);
3146 0 : return true;
3147 : }
3148 : }
3149 :
3150 : expr = op;
3151 : }
3152 :
3153 3865613305 : bool require_non_reg = false;
3154 6315165848 : while (handled_component_p (expr))
3155 : {
3156 2449552543 : require_non_reg = true;
3157 2449552543 : code_name = get_tree_code_name (TREE_CODE (expr));
3158 :
3159 2449552543 : if (TREE_CODE (expr) == REALPART_EXPR
3160 2449552543 : || TREE_CODE (expr) == IMAGPART_EXPR
3161 2449552543 : || TREE_CODE (expr) == BIT_FIELD_REF)
3162 : {
3163 0 : error ("non-top-level %qs", code_name);
3164 0 : return true;
3165 : }
3166 :
3167 2449552543 : tree op = TREE_OPERAND (expr, 0);
3168 :
3169 2449552543 : if (TREE_CODE (expr) == ARRAY_REF
3170 2449552543 : || TREE_CODE (expr) == ARRAY_RANGE_REF)
3171 : {
3172 463245241 : if (!is_gimple_val (TREE_OPERAND (expr, 1))
3173 463245241 : || (TREE_OPERAND (expr, 2)
3174 3733185 : && !is_gimple_val (TREE_OPERAND (expr, 2)))
3175 926490482 : || (TREE_OPERAND (expr, 3)
3176 692796 : && !is_gimple_val (TREE_OPERAND (expr, 3))))
3177 : {
3178 0 : error ("invalid operands to %qs", code_name);
3179 0 : debug_generic_stmt (expr);
3180 0 : return true;
3181 : }
3182 : }
3183 :
3184 : /* Verify if the reference array element types are compatible. */
3185 2449552543 : if (TREE_CODE (expr) == ARRAY_REF
3186 2912487374 : && !useless_type_conversion_p (TREE_TYPE (expr),
3187 462934831 : TREE_TYPE (TREE_TYPE (op))))
3188 : {
3189 0 : error ("type mismatch in %qs", code_name);
3190 0 : debug_generic_stmt (TREE_TYPE (expr));
3191 0 : debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3192 0 : return true;
3193 : }
3194 2449552543 : if (TREE_CODE (expr) == ARRAY_RANGE_REF
3195 2449862953 : && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
3196 310410 : TREE_TYPE (TREE_TYPE (op))))
3197 : {
3198 0 : error ("type mismatch in %qs", code_name);
3199 0 : debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
3200 0 : debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
3201 0 : return true;
3202 : }
3203 :
3204 2449552543 : if (TREE_CODE (expr) == COMPONENT_REF)
3205 : {
3206 1983431603 : if (TREE_OPERAND (expr, 2)
3207 1983431603 : && !is_gimple_val (TREE_OPERAND (expr, 2)))
3208 : {
3209 0 : error ("invalid %qs offset operator", code_name);
3210 0 : return true;
3211 : }
3212 1983431603 : if (!useless_type_conversion_p (TREE_TYPE (expr),
3213 1983431603 : TREE_TYPE (TREE_OPERAND (expr, 1))))
3214 : {
3215 0 : error ("type mismatch in %qs", code_name);
3216 0 : debug_generic_stmt (TREE_TYPE (expr));
3217 0 : debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
3218 0 : return true;
3219 : }
3220 : }
3221 :
3222 2449552543 : expr = op;
3223 : }
3224 :
3225 3865613305 : code_name = get_tree_code_name (TREE_CODE (expr));
3226 :
3227 3865613305 : if (TREE_CODE (expr) == MEM_REF)
3228 : {
3229 1306911200 : if (!is_gimple_mem_ref_addr (TREE_OPERAND (expr, 0))
3230 1306911200 : || (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
3231 356884042 : && verify_address (TREE_OPERAND (expr, 0), false)))
3232 : {
3233 0 : error ("invalid address operand in %qs", code_name);
3234 0 : debug_generic_stmt (expr);
3235 0 : return true;
3236 : }
3237 1306911200 : if (!poly_int_tree_p (TREE_OPERAND (expr, 1))
3238 1306911200 : || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
3239 : {
3240 0 : error ("invalid offset operand in %qs", code_name);
3241 0 : debug_generic_stmt (expr);
3242 0 : return true;
3243 : }
3244 1306911200 : if (MR_DEPENDENCE_CLIQUE (expr) != 0
3245 1306911200 : && MR_DEPENDENCE_CLIQUE (expr) > cfun->last_clique)
3246 : {
3247 0 : error ("invalid clique in %qs", code_name);
3248 0 : debug_generic_stmt (expr);
3249 0 : return true;
3250 : }
3251 : }
3252 2558702105 : else if (TREE_CODE (expr) == TARGET_MEM_REF)
3253 : {
3254 30657453 : if (!TMR_BASE (expr)
3255 30657453 : || !is_gimple_mem_ref_addr (TMR_BASE (expr))
3256 61314906 : || (TREE_CODE (TMR_BASE (expr)) == ADDR_EXPR
3257 6465780 : && verify_address (TMR_BASE (expr), false)))
3258 : {
3259 0 : error ("invalid address operand in %qs", code_name);
3260 0 : return true;
3261 : }
3262 30657453 : if (!TMR_OFFSET (expr)
3263 30657453 : || !poly_int_tree_p (TMR_OFFSET (expr))
3264 61314906 : || !POINTER_TYPE_P (TREE_TYPE (TMR_OFFSET (expr))))
3265 : {
3266 0 : error ("invalid offset operand in %qs", code_name);
3267 0 : debug_generic_stmt (expr);
3268 0 : return true;
3269 : }
3270 30657453 : if (MR_DEPENDENCE_CLIQUE (expr) != 0
3271 30657453 : && MR_DEPENDENCE_CLIQUE (expr) > cfun->last_clique)
3272 : {
3273 0 : error ("invalid clique in %qs", code_name);
3274 0 : debug_generic_stmt (expr);
3275 0 : return true;
3276 : }
3277 : }
3278 2528044652 : else if (INDIRECT_REF_P (expr))
3279 : {
3280 0 : error ("%qs in gimple IL", code_name);
3281 0 : debug_generic_stmt (expr);
3282 0 : return true;
3283 : }
3284 2528044652 : else if (require_non_reg
3285 2528044652 : && (is_gimple_reg (expr)
3286 939408821 : || (is_gimple_min_invariant (expr)
3287 : /* STRING_CSTs are representatives of the string table
3288 : entry which lives in memory. */
3289 9201293 : && TREE_CODE (expr) != STRING_CST)))
3290 : {
3291 0 : error ("%qs as base where non-register is required", code_name);
3292 0 : debug_generic_stmt (expr);
3293 0 : return true;
3294 : }
3295 :
3296 3865613305 : if (!require_lvalue
3297 3865613305 : && (is_gimple_reg (expr) || is_gimple_min_invariant (expr)))
3298 : return false;
3299 :
3300 2616558200 : if (TREE_CODE (expr) != SSA_NAME && is_gimple_id (expr))
3301 : return false;
3302 :
3303 1337568653 : if (TREE_CODE (expr) != TARGET_MEM_REF
3304 1337568653 : && TREE_CODE (expr) != MEM_REF)
3305 : {
3306 0 : error ("invalid expression for min lvalue");
3307 0 : return true;
3308 : }
3309 :
3310 : return false;
3311 : }
3312 :
3313 : /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
3314 : list of pointer-to types that is trivially convertible to DEST. */
3315 :
3316 : static bool
3317 475 : one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
3318 : {
3319 475 : tree src;
3320 :
3321 475 : if (!TYPE_POINTER_TO (src_obj))
3322 : return true;
3323 :
3324 475 : for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
3325 475 : if (useless_type_conversion_p (dest, src))
3326 : return true;
3327 :
3328 : return false;
3329 : }
3330 :
3331 : /* Return true if TYPE1 is a fixed-point type and if conversions to and
3332 : from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
3333 :
3334 : static bool
3335 0 : valid_fixed_convert_types_p (tree type1, tree type2)
3336 : {
3337 0 : return (FIXED_POINT_TYPE_P (type1)
3338 0 : && (INTEGRAL_TYPE_P (type2)
3339 0 : || SCALAR_FLOAT_TYPE_P (type2)
3340 0 : || FIXED_POINT_TYPE_P (type2)));
3341 : }
3342 :
3343 : /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
3344 : is a problem, otherwise false. */
3345 :
3346 : static bool
3347 1109502755 : verify_gimple_call (gcall *stmt)
3348 : {
3349 1109502755 : tree fn = gimple_call_fn (stmt);
3350 1109502755 : tree fntype, fndecl;
3351 1109502755 : unsigned i;
3352 :
3353 1109502755 : if (gimple_call_internal_p (stmt))
3354 : {
3355 42778903 : if (fn)
3356 : {
3357 0 : error ("gimple call has two targets");
3358 0 : debug_generic_stmt (fn);
3359 0 : return true;
3360 : }
3361 : }
3362 : else
3363 : {
3364 1066723852 : if (!fn)
3365 : {
3366 0 : error ("gimple call has no target");
3367 0 : return true;
3368 : }
3369 : }
3370 :
3371 1066723852 : if (fn && !is_gimple_call_addr (fn))
3372 : {
3373 0 : error ("invalid function in gimple call");
3374 0 : debug_generic_stmt (fn);
3375 0 : return true;
3376 : }
3377 :
3378 1109502755 : if (fn
3379 1109502755 : && (!POINTER_TYPE_P (TREE_TYPE (fn))
3380 1066723852 : || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
3381 177074065 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE)))
3382 : {
3383 0 : error ("non-function in gimple call");
3384 0 : return true;
3385 : }
3386 :
3387 1109502755 : fndecl = gimple_call_fndecl (stmt);
3388 1109502755 : if (fndecl
3389 1040078413 : && TREE_CODE (fndecl) == FUNCTION_DECL
3390 1040078413 : && DECL_LOOPING_CONST_OR_PURE_P (fndecl)
3391 6091874 : && !DECL_PURE_P (fndecl)
3392 1109877763 : && !TREE_READONLY (fndecl))
3393 : {
3394 0 : error ("invalid pure const state for function");
3395 0 : return true;
3396 : }
3397 :
3398 1109502755 : tree lhs = gimple_call_lhs (stmt);
3399 1109502755 : if (lhs
3400 1109502755 : && (!is_gimple_reg (lhs)
3401 73316248 : && (!is_gimple_lvalue (lhs)
3402 73316248 : || verify_types_in_gimple_reference
3403 73316248 : (TREE_CODE (lhs) == WITH_SIZE_EXPR
3404 0 : ? TREE_OPERAND (lhs, 0) : lhs, true))))
3405 : {
3406 0 : error ("invalid LHS in gimple call");
3407 0 : return true;
3408 : }
3409 :
3410 1109502755 : if (gimple_call_ctrl_altering_p (stmt)
3411 152994528 : && gimple_call_noreturn_p (stmt)
3412 1257376798 : && should_remove_lhs_p (lhs))
3413 : {
3414 0 : error ("LHS in %<noreturn%> call");
3415 0 : return true;
3416 : }
3417 :
3418 1109502755 : fntype = gimple_call_fntype (stmt);
3419 1109502755 : if (fntype
3420 1109502755 : && lhs
3421 404212838 : && !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (fntype))
3422 : /* ??? At least C++ misses conversions at assignments from
3423 : void * call results.
3424 : For now simply allow arbitrary pointer type conversions. */
3425 1109502755 : && !(POINTER_TYPE_P (TREE_TYPE (lhs))
3426 0 : && POINTER_TYPE_P (TREE_TYPE (fntype))))
3427 : {
3428 0 : error ("invalid conversion in gimple call");
3429 0 : debug_generic_stmt (TREE_TYPE (lhs));
3430 0 : debug_generic_stmt (TREE_TYPE (fntype));
3431 0 : return true;
3432 : }
3433 :
3434 1109502755 : if (gimple_call_chain (stmt)
3435 1109502755 : && !is_gimple_val (gimple_call_chain (stmt)))
3436 : {
3437 0 : error ("invalid static chain in gimple call");
3438 0 : debug_generic_stmt (gimple_call_chain (stmt));
3439 0 : return true;
3440 : }
3441 :
3442 : /* If there is a static chain argument, the call should either be
3443 : indirect, or the decl should have DECL_STATIC_CHAIN set. */
3444 1109502755 : if (gimple_call_chain (stmt)
3445 5052732 : && fndecl
3446 1111052018 : && !DECL_STATIC_CHAIN (fndecl))
3447 : {
3448 0 : error ("static chain with function that doesn%'t use one");
3449 0 : return true;
3450 : }
3451 :
3452 1109502755 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
3453 : {
3454 262456821 : switch (DECL_FUNCTION_CODE (fndecl))
3455 : {
3456 26494282 : case BUILT_IN_UNREACHABLE:
3457 26494282 : case BUILT_IN_UNREACHABLE_TRAP:
3458 26494282 : case BUILT_IN_TRAP:
3459 26494282 : if (gimple_call_num_args (stmt) > 0)
3460 : {
3461 : /* Built-in unreachable with parameters might not be caught by
3462 : undefined behavior sanitizer. Front-ends do check users do not
3463 : call them that way but we also produce calls to
3464 : __builtin_unreachable internally, for example when IPA figures
3465 : out a call cannot happen in a legal program. In such cases,
3466 : we must make sure arguments are stripped off. */
3467 0 : error ("%<__builtin_unreachable%> or %<__builtin_trap%> call "
3468 : "with arguments");
3469 0 : return true;
3470 : }
3471 : break;
3472 : default:
3473 : break;
3474 : }
3475 : }
3476 :
3477 : /* For a call to .DEFERRED_INIT,
3478 : LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL)
3479 : we should guarantee that when the 1st argument is a constant, it should
3480 : be the same as the size of the LHS. */
3481 :
3482 1109502755 : if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
3483 : {
3484 9966686 : tree size_of_arg0 = gimple_call_arg (stmt, 0);
3485 9966686 : tree size_of_lhs = TYPE_SIZE_UNIT (TREE_TYPE (lhs));
3486 :
3487 9966686 : if (TREE_CODE (lhs) == SSA_NAME)
3488 9966686 : lhs = SSA_NAME_VAR (lhs);
3489 :
3490 9966686 : poly_uint64 size_from_arg0, size_from_lhs;
3491 9966686 : bool is_constant_size_arg0 = poly_int_tree_p (size_of_arg0,
3492 : &size_from_arg0);
3493 9966686 : bool is_constant_size_lhs = poly_int_tree_p (size_of_lhs,
3494 : &size_from_lhs);
3495 9966686 : if (is_constant_size_arg0 && is_constant_size_lhs)
3496 9956093 : if (maybe_ne (size_from_arg0, size_from_lhs))
3497 : {
3498 0 : error ("%<DEFERRED_INIT%> calls should have same "
3499 : "constant size for the first argument and LHS");
3500 0 : return true;
3501 : }
3502 : }
3503 :
3504 : /* IFN_VARING is not allowed to be present after the completion of any pass
3505 : as it should have been replaced. */
3506 1109502755 : if (gimple_call_internal_p (stmt, IFN_VARYING))
3507 : {
3508 0 : error ("%<.VARYING%> calls should have been replaced and are not allowed "
3509 : "outside of the pass that introduced them");
3510 0 : return true;
3511 : }
3512 :
3513 1109502755 : if (gimple_call_internal_p (stmt))
3514 : {
3515 42778903 : unsigned nargs = gimple_call_num_args (stmt);
3516 85466137 : tree *args = nargs ? gimple_call_arg_ptr (stmt, 0) : NULL;
3517 42778903 : if (!check_internal_fn_args (gimple_location (stmt),
3518 : gimple_call_internal_fn (stmt), nargs, args))
3519 : return true;
3520 : }
3521 :
3522 : /* ??? The C frontend passes unpromoted arguments in case it
3523 : didn't see a function declaration before the call. So for now
3524 : leave the call arguments mostly unverified. Once we gimplify
3525 : unit-at-a-time we have a chance to fix this. */
3526 3313118728 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
3527 : {
3528 2203615974 : tree arg = gimple_call_arg (stmt, i);
3529 2203615974 : if ((is_gimple_reg_type (TREE_TYPE (arg))
3530 2082565336 : && !is_gimple_val (arg))
3531 4286181309 : || (!is_gimple_reg_type (TREE_TYPE (arg))
3532 121050638 : && !is_gimple_lvalue (arg)))
3533 : {
3534 1 : error ("invalid argument to gimple call");
3535 1 : debug_generic_expr (arg);
3536 1 : return true;
3537 : }
3538 2203615973 : if (!is_gimple_reg (arg))
3539 : {
3540 1306685567 : if (TREE_CODE (arg) == WITH_SIZE_EXPR)
3541 22447 : arg = TREE_OPERAND (arg, 0);
3542 1306685567 : if (verify_types_in_gimple_reference (arg, false))
3543 : return true;
3544 : }
3545 : }
3546 :
3547 : return false;
3548 : }
3549 :
3550 : /* Verifies the gimple comparison with the result type TYPE and
3551 : the operands OP0 and OP1, comparison code is CODE. */
3552 :
3553 : static bool
3554 871218760 : verify_gimple_comparison (tree type, tree op0, tree op1, enum tree_code code)
3555 : {
3556 871218760 : tree op0_type = TREE_TYPE (op0);
3557 871218760 : tree op1_type = TREE_TYPE (op1);
3558 :
3559 871218760 : if (!is_gimple_val (op0) || !is_gimple_val (op1))
3560 : {
3561 0 : error ("invalid operands in gimple comparison");
3562 0 : return true;
3563 : }
3564 :
3565 : /* For comparisons we do not have the operations type as the
3566 : effective type the comparison is carried out in. Instead
3567 : we require that either the first operand is trivially
3568 : convertible into the second, or the other way around. */
3569 871218760 : if (!useless_type_conversion_p (op0_type, op1_type)
3570 871218760 : && !useless_type_conversion_p (op1_type, op0_type))
3571 : {
3572 0 : error ("mismatching comparison operand types");
3573 0 : debug_generic_expr (op0_type);
3574 0 : debug_generic_expr (op1_type);
3575 0 : return true;
3576 : }
3577 :
3578 : /* The resulting type of a comparison may be an effective boolean type. */
3579 871218760 : if (INTEGRAL_TYPE_P (type)
3580 871218760 : && (TREE_CODE (type) == BOOLEAN_TYPE
3581 1856 : || TYPE_PRECISION (type) == 1))
3582 : {
3583 869179754 : if ((VECTOR_TYPE_P (op0_type)
3584 869069374 : || VECTOR_TYPE_P (op1_type))
3585 110380 : && code != EQ_EXPR && code != NE_EXPR
3586 0 : && !VECTOR_BOOLEAN_TYPE_P (op0_type)
3587 869179754 : && !VECTOR_INTEGER_TYPE_P (op0_type))
3588 : {
3589 0 : error ("unsupported operation or type for vector comparison"
3590 : " returning a boolean");
3591 0 : debug_generic_expr (op0_type);
3592 0 : debug_generic_expr (op1_type);
3593 0 : return true;
3594 : }
3595 : }
3596 : /* Or a boolean vector type with the same element count
3597 : as the comparison operand types. */
3598 2039006 : else if (VECTOR_TYPE_P (type)
3599 2039006 : && TREE_CODE (TREE_TYPE (type)) == BOOLEAN_TYPE)
3600 : {
3601 2039006 : if (TREE_CODE (op0_type) != VECTOR_TYPE
3602 2039006 : || TREE_CODE (op1_type) != VECTOR_TYPE)
3603 : {
3604 0 : error ("non-vector operands in vector comparison");
3605 0 : debug_generic_expr (op0_type);
3606 0 : debug_generic_expr (op1_type);
3607 0 : return true;
3608 : }
3609 :
3610 2039006 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type),
3611 4078012 : TYPE_VECTOR_SUBPARTS (op0_type)))
3612 : {
3613 0 : error ("invalid vector comparison resulting type");
3614 0 : debug_generic_expr (type);
3615 0 : return true;
3616 : }
3617 : }
3618 : else
3619 : {
3620 0 : error ("bogus comparison result type");
3621 0 : debug_generic_expr (type);
3622 0 : return true;
3623 : }
3624 :
3625 : return false;
3626 : }
3627 :
3628 : /* Verify a gimple assignment statement STMT with an unary rhs.
3629 : Returns true if anything is wrong. */
3630 :
3631 : static bool
3632 435187398 : verify_gimple_assign_unary (gassign *stmt)
3633 : {
3634 435187398 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3635 435187398 : tree lhs = gimple_assign_lhs (stmt);
3636 435187398 : tree lhs_type = TREE_TYPE (lhs);
3637 435187398 : tree rhs1 = gimple_assign_rhs1 (stmt);
3638 435187398 : tree rhs1_type = TREE_TYPE (rhs1);
3639 :
3640 435187398 : if (!is_gimple_reg (lhs))
3641 : {
3642 0 : error ("non-register as LHS of unary operation");
3643 0 : return true;
3644 : }
3645 :
3646 435187398 : if (!is_gimple_val (rhs1))
3647 : {
3648 0 : error ("invalid operand in unary operation");
3649 0 : return true;
3650 : }
3651 :
3652 435187398 : const char* const code_name = get_tree_code_name (rhs_code);
3653 :
3654 : /* First handle conversions. */
3655 435187398 : switch (rhs_code)
3656 : {
3657 385805077 : CASE_CONVERT:
3658 385805077 : {
3659 : /* Allow conversions between vectors with the same number of elements,
3660 : provided that the conversion is OK for the element types too. */
3661 385805077 : if (VECTOR_TYPE_P (lhs_type)
3662 148730 : && VECTOR_TYPE_P (rhs1_type)
3663 385953807 : && known_eq (TYPE_VECTOR_SUBPARTS (lhs_type),
3664 : TYPE_VECTOR_SUBPARTS (rhs1_type)))
3665 : {
3666 148730 : lhs_type = TREE_TYPE (lhs_type);
3667 148730 : rhs1_type = TREE_TYPE (rhs1_type);
3668 : }
3669 385656347 : else if (VECTOR_TYPE_P (lhs_type) || VECTOR_TYPE_P (rhs1_type))
3670 : {
3671 0 : error ("invalid vector types in nop conversion");
3672 0 : debug_generic_expr (lhs_type);
3673 0 : debug_generic_expr (rhs1_type);
3674 0 : return true;
3675 : }
3676 :
3677 : /* Allow conversions from pointer type to integral type only if
3678 : there is no sign or zero extension involved.
3679 : For targets were the precision of ptrofftype doesn't match that
3680 : of pointers we allow conversions to types where
3681 : POINTERS_EXTEND_UNSIGNED specifies how that works. */
3682 385805077 : if ((POINTER_TYPE_P (lhs_type)
3683 24189123 : && INTEGRAL_TYPE_P (rhs1_type))
3684 389947199 : || (POINTER_TYPE_P (rhs1_type)
3685 48763512 : && INTEGRAL_TYPE_P (lhs_type)
3686 44621390 : && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
3687 : #if defined(POINTERS_EXTEND_UNSIGNED)
3688 0 : || (TYPE_MODE (rhs1_type) == ptr_mode
3689 0 : && (TYPE_PRECISION (lhs_type)
3690 0 : == BITS_PER_WORD /* word_mode */
3691 0 : || (TYPE_PRECISION (lhs_type)
3692 0 : == GET_MODE_PRECISION (Pmode))))
3693 : #endif
3694 : )))
3695 : return false;
3696 :
3697 : /* Allow conversion from integral to offset type and vice versa. */
3698 321136686 : if ((TREE_CODE (lhs_type) == OFFSET_TYPE
3699 7600 : && INTEGRAL_TYPE_P (rhs1_type))
3700 321135519 : || (INTEGRAL_TYPE_P (lhs_type)
3701 296767117 : && TREE_CODE (rhs1_type) == OFFSET_TYPE))
3702 : return false;
3703 :
3704 : /* Otherwise assert we are converting between types of the
3705 : same kind. */
3706 321102863 : if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
3707 : {
3708 0 : error ("invalid types in nop conversion");
3709 0 : debug_generic_expr (lhs_type);
3710 0 : debug_generic_expr (rhs1_type);
3711 0 : return true;
3712 : }
3713 :
3714 : return false;
3715 : }
3716 :
3717 0 : case ADDR_SPACE_CONVERT_EXPR:
3718 0 : {
3719 0 : if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
3720 0 : || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
3721 0 : == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
3722 : {
3723 0 : error ("invalid types in address space conversion");
3724 0 : debug_generic_expr (lhs_type);
3725 0 : debug_generic_expr (rhs1_type);
3726 0 : return true;
3727 : }
3728 :
3729 : return false;
3730 : }
3731 :
3732 0 : case FIXED_CONVERT_EXPR:
3733 0 : {
3734 0 : if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
3735 435187398 : && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
3736 : {
3737 0 : error ("invalid types in fixed-point conversion");
3738 0 : debug_generic_expr (lhs_type);
3739 0 : debug_generic_expr (rhs1_type);
3740 0 : return true;
3741 : }
3742 :
3743 : return false;
3744 : }
3745 :
3746 16859048 : case FLOAT_EXPR:
3747 16859048 : {
3748 16757671 : if ((!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
3749 16859048 : && (!VECTOR_INTEGER_TYPE_P (rhs1_type)
3750 101377 : || !VECTOR_FLOAT_TYPE_P (lhs_type)))
3751 : {
3752 0 : error ("invalid types in conversion to floating-point");
3753 0 : debug_generic_expr (lhs_type);
3754 0 : debug_generic_expr (rhs1_type);
3755 0 : return true;
3756 : }
3757 :
3758 : return false;
3759 : }
3760 :
3761 5283476 : case FIX_TRUNC_EXPR:
3762 5283476 : {
3763 5256561 : if ((!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
3764 5283476 : && (!VECTOR_INTEGER_TYPE_P (lhs_type)
3765 26915 : || !VECTOR_FLOAT_TYPE_P (rhs1_type)))
3766 : {
3767 0 : error ("invalid types in conversion to integer");
3768 0 : debug_generic_expr (lhs_type);
3769 0 : debug_generic_expr (rhs1_type);
3770 0 : return true;
3771 : }
3772 :
3773 : return false;
3774 : }
3775 :
3776 892603 : case VEC_UNPACK_HI_EXPR:
3777 892603 : case VEC_UNPACK_LO_EXPR:
3778 892603 : case VEC_UNPACK_FLOAT_HI_EXPR:
3779 892603 : case VEC_UNPACK_FLOAT_LO_EXPR:
3780 892603 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
3781 892603 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
3782 892603 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3783 892603 : || TREE_CODE (lhs_type) != VECTOR_TYPE
3784 892603 : || (!INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3785 135765 : && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs_type)))
3786 892603 : || (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3787 42209 : && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
3788 892603 : || ((rhs_code == VEC_UNPACK_HI_EXPR
3789 892603 : || rhs_code == VEC_UNPACK_LO_EXPR)
3790 1592214 : && (INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3791 796107 : != INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
3792 892603 : || ((rhs_code == VEC_UNPACK_FLOAT_HI_EXPR
3793 892603 : || rhs_code == VEC_UNPACK_FLOAT_LO_EXPR)
3794 95026 : && (INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3795 95026 : || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type))))
3796 892603 : || ((rhs_code == VEC_UNPACK_FIX_TRUNC_HI_EXPR
3797 892603 : || rhs_code == VEC_UNPACK_FIX_TRUNC_LO_EXPR)
3798 1470 : && (INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3799 1470 : || SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs_type))))
3800 1785206 : || (maybe_ne (GET_MODE_SIZE (element_mode (lhs_type)),
3801 1785206 : 2 * GET_MODE_SIZE (element_mode (rhs1_type)))
3802 90802 : && (!VECTOR_BOOLEAN_TYPE_P (lhs_type)
3803 90802 : || !VECTOR_BOOLEAN_TYPE_P (rhs1_type)))
3804 1785206 : || maybe_ne (2 * TYPE_VECTOR_SUBPARTS (lhs_type),
3805 1785206 : TYPE_VECTOR_SUBPARTS (rhs1_type)))
3806 : {
3807 0 : error ("type mismatch in %qs expression", code_name);
3808 0 : debug_generic_expr (lhs_type);
3809 0 : debug_generic_expr (rhs1_type);
3810 0 : return true;
3811 : }
3812 :
3813 : return false;
3814 :
3815 143400 : case ABSU_EXPR:
3816 32556 : if (!ANY_INTEGRAL_TYPE_P (lhs_type)
3817 143400 : || !TYPE_UNSIGNED (lhs_type)
3818 143400 : || !ANY_INTEGRAL_TYPE_P (rhs1_type)
3819 143400 : || TYPE_UNSIGNED (rhs1_type)
3820 286800 : || element_precision (lhs_type) != element_precision (rhs1_type))
3821 : {
3822 0 : error ("invalid types for %qs", code_name);
3823 0 : debug_generic_expr (lhs_type);
3824 0 : debug_generic_expr (rhs1_type);
3825 0 : return true;
3826 : }
3827 : return false;
3828 :
3829 0 : case VEC_DUPLICATE_EXPR:
3830 0 : if (TREE_CODE (lhs_type) != VECTOR_TYPE
3831 0 : || !useless_type_conversion_p (TREE_TYPE (lhs_type), rhs1_type))
3832 : {
3833 0 : error ("%qs should be from a scalar to a like vector", code_name);
3834 0 : debug_generic_expr (lhs_type);
3835 0 : debug_generic_expr (rhs1_type);
3836 0 : return true;
3837 : }
3838 : return false;
3839 :
3840 39290 : case CONJ_EXPR:
3841 39290 : if (TREE_CODE (lhs_type) != COMPLEX_TYPE)
3842 : {
3843 0 : diagnose_unary_lhs:
3844 0 : error ("invalid type for %qs", code_name);
3845 0 : debug_generic_expr (lhs_type);
3846 0 : return true;
3847 : }
3848 : break;
3849 :
3850 25684580 : case NEGATE_EXPR:
3851 25684580 : case ABS_EXPR:
3852 25684580 : case BIT_NOT_EXPR:
3853 25684580 : if (POINTER_TYPE_P (lhs_type) || TREE_CODE (lhs_type) == OFFSET_TYPE)
3854 0 : goto diagnose_unary_lhs;
3855 : /* FALLTHRU */
3856 26164504 : case PAREN_EXPR:
3857 26164504 : if (AGGREGATE_TYPE_P (lhs_type))
3858 0 : goto diagnose_unary_lhs;
3859 : break;
3860 :
3861 0 : default:
3862 0 : gcc_unreachable ();
3863 : }
3864 :
3865 : /* For the remaining codes assert there is no conversion involved. */
3866 26203794 : if (!useless_type_conversion_p (lhs_type, rhs1_type))
3867 : {
3868 0 : error ("non-trivial conversion in unary operation");
3869 0 : debug_generic_expr (lhs_type);
3870 0 : debug_generic_expr (rhs1_type);
3871 0 : return true;
3872 : }
3873 :
3874 : return false;
3875 : }
3876 :
3877 : /* Verify a gimple assignment statement STMT with a binary rhs.
3878 : Returns true if anything is wrong. */
3879 :
3880 : static bool
3881 1079508165 : verify_gimple_assign_binary (gassign *stmt)
3882 : {
3883 1079508165 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3884 1079508165 : tree lhs = gimple_assign_lhs (stmt);
3885 1079508165 : tree lhs_type = TREE_TYPE (lhs);
3886 1079508165 : tree rhs1 = gimple_assign_rhs1 (stmt);
3887 1079508165 : tree rhs1_type = TREE_TYPE (rhs1);
3888 1079508165 : tree rhs2 = gimple_assign_rhs2 (stmt);
3889 1079508165 : tree rhs2_type = TREE_TYPE (rhs2);
3890 :
3891 1079508165 : if (!is_gimple_reg (lhs))
3892 : {
3893 0 : error ("non-register as LHS of binary operation");
3894 0 : return true;
3895 : }
3896 :
3897 1079508165 : if (!is_gimple_val (rhs1)
3898 1079508165 : || !is_gimple_val (rhs2))
3899 : {
3900 0 : error ("invalid operands in binary operation");
3901 0 : return true;
3902 : }
3903 :
3904 1079508165 : const char* const code_name = get_tree_code_name (rhs_code);
3905 :
3906 : /* First handle operations that involve different types. */
3907 1079508165 : switch (rhs_code)
3908 : {
3909 2476582 : case COMPLEX_EXPR:
3910 2476582 : {
3911 2476582 : if (TREE_CODE (lhs_type) != COMPLEX_TYPE
3912 2476582 : || !(INTEGRAL_TYPE_P (rhs1_type)
3913 : || SCALAR_FLOAT_TYPE_P (rhs1_type))
3914 2476582 : || !(INTEGRAL_TYPE_P (rhs2_type)
3915 : || SCALAR_FLOAT_TYPE_P (rhs2_type)))
3916 : {
3917 0 : error ("type mismatch in %qs", code_name);
3918 0 : debug_generic_expr (lhs_type);
3919 0 : debug_generic_expr (rhs1_type);
3920 0 : debug_generic_expr (rhs2_type);
3921 0 : return true;
3922 : }
3923 :
3924 : return false;
3925 : }
3926 :
3927 34509864 : case LSHIFT_EXPR:
3928 34509864 : case RSHIFT_EXPR:
3929 34509864 : case LROTATE_EXPR:
3930 34509864 : case RROTATE_EXPR:
3931 34509864 : {
3932 : /* Shifts and rotates are ok on integral types, fixed point
3933 : types and integer vector types. */
3934 34509864 : if ((!INTEGRAL_TYPE_P (rhs1_type)
3935 790651 : && !FIXED_POINT_TYPE_P (rhs1_type)
3936 790651 : && ! (VECTOR_TYPE_P (rhs1_type)
3937 790651 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
3938 34509864 : || (!INTEGRAL_TYPE_P (rhs2_type)
3939 : /* Vector shifts of vectors are also ok. */
3940 205993 : && ! (VECTOR_TYPE_P (rhs1_type)
3941 205993 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3942 205993 : && VECTOR_TYPE_P (rhs2_type)
3943 205993 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
3944 69019728 : || !useless_type_conversion_p (lhs_type, rhs1_type))
3945 : {
3946 0 : error ("type mismatch in %qs", code_name);
3947 0 : debug_generic_expr (lhs_type);
3948 0 : debug_generic_expr (rhs1_type);
3949 0 : debug_generic_expr (rhs2_type);
3950 0 : return true;
3951 : }
3952 :
3953 : return false;
3954 : }
3955 :
3956 0 : case WIDEN_LSHIFT_EXPR:
3957 0 : {
3958 0 : if (!INTEGRAL_TYPE_P (lhs_type)
3959 0 : || !INTEGRAL_TYPE_P (rhs1_type)
3960 0 : || TREE_CODE (rhs2) != INTEGER_CST
3961 0 : || (2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)))
3962 : {
3963 0 : error ("type mismatch in %qs", code_name);
3964 0 : debug_generic_expr (lhs_type);
3965 0 : debug_generic_expr (rhs1_type);
3966 0 : debug_generic_expr (rhs2_type);
3967 0 : return true;
3968 : }
3969 :
3970 : return false;
3971 : }
3972 :
3973 0 : case VEC_WIDEN_LSHIFT_HI_EXPR:
3974 0 : case VEC_WIDEN_LSHIFT_LO_EXPR:
3975 0 : {
3976 0 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
3977 0 : || TREE_CODE (lhs_type) != VECTOR_TYPE
3978 0 : || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
3979 0 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
3980 0 : || TREE_CODE (rhs2) != INTEGER_CST
3981 0 : || (2 * TYPE_PRECISION (TREE_TYPE (rhs1_type))
3982 0 : > TYPE_PRECISION (TREE_TYPE (lhs_type))))
3983 : {
3984 0 : error ("type mismatch in %qs", code_name);
3985 0 : debug_generic_expr (lhs_type);
3986 0 : debug_generic_expr (rhs1_type);
3987 0 : debug_generic_expr (rhs2_type);
3988 0 : return true;
3989 : }
3990 :
3991 : return false;
3992 : }
3993 :
3994 479613485 : case PLUS_EXPR:
3995 479613485 : case MINUS_EXPR:
3996 479613485 : {
3997 479613485 : tree lhs_etype = lhs_type;
3998 479613485 : tree rhs1_etype = rhs1_type;
3999 479613485 : tree rhs2_etype = rhs2_type;
4000 479613485 : if (VECTOR_TYPE_P (lhs_type))
4001 : {
4002 4956202 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
4003 4956202 : || TREE_CODE (rhs2_type) != VECTOR_TYPE)
4004 : {
4005 0 : error ("invalid non-vector operands to %qs", code_name);
4006 0 : return true;
4007 : }
4008 4956202 : lhs_etype = TREE_TYPE (lhs_type);
4009 4956202 : rhs1_etype = TREE_TYPE (rhs1_type);
4010 4956202 : rhs2_etype = TREE_TYPE (rhs2_type);
4011 : }
4012 479613485 : if (POINTER_TYPE_P (lhs_etype)
4013 479613485 : || POINTER_TYPE_P (rhs1_etype)
4014 479613485 : || POINTER_TYPE_P (rhs2_etype))
4015 : {
4016 0 : error ("invalid (pointer) operands %qs", code_name);
4017 0 : return true;
4018 : }
4019 :
4020 : /* Continue with generic binary expression handling. */
4021 : break;
4022 : }
4023 :
4024 159524512 : case POINTER_PLUS_EXPR:
4025 159524512 : {
4026 159524512 : if (!POINTER_TYPE_P (rhs1_type)
4027 159524512 : || !useless_type_conversion_p (lhs_type, rhs1_type)
4028 319049024 : || !ptrofftype_p (rhs2_type))
4029 : {
4030 0 : error ("type mismatch in %qs", code_name);
4031 0 : debug_generic_stmt (lhs_type);
4032 0 : debug_generic_stmt (rhs1_type);
4033 0 : debug_generic_stmt (rhs2_type);
4034 0 : return true;
4035 : }
4036 :
4037 : return false;
4038 : }
4039 :
4040 16503306 : case POINTER_DIFF_EXPR:
4041 16503306 : {
4042 16503306 : if (!POINTER_TYPE_P (rhs1_type)
4043 16503306 : || !POINTER_TYPE_P (rhs2_type)
4044 : /* Because we special-case pointers to void we allow difference
4045 : of arbitrary pointers with the same mode. */
4046 16503306 : || TYPE_MODE (rhs1_type) != TYPE_MODE (rhs2_type)
4047 16503306 : || !INTEGRAL_TYPE_P (lhs_type)
4048 16503306 : || TYPE_UNSIGNED (lhs_type)
4049 33006612 : || TYPE_PRECISION (lhs_type) != TYPE_PRECISION (rhs1_type))
4050 : {
4051 0 : error ("type mismatch in %qs", code_name);
4052 0 : debug_generic_stmt (lhs_type);
4053 0 : debug_generic_stmt (rhs1_type);
4054 0 : debug_generic_stmt (rhs2_type);
4055 0 : return true;
4056 : }
4057 :
4058 : return false;
4059 : }
4060 :
4061 0 : case TRUTH_ANDIF_EXPR:
4062 0 : case TRUTH_ORIF_EXPR:
4063 0 : case TRUTH_AND_EXPR:
4064 0 : case TRUTH_OR_EXPR:
4065 0 : case TRUTH_XOR_EXPR:
4066 :
4067 0 : gcc_unreachable ();
4068 :
4069 90594696 : case LT_EXPR:
4070 90594696 : case LE_EXPR:
4071 90594696 : case GT_EXPR:
4072 90594696 : case GE_EXPR:
4073 90594696 : case EQ_EXPR:
4074 90594696 : case NE_EXPR:
4075 90594696 : case UNORDERED_EXPR:
4076 90594696 : case ORDERED_EXPR:
4077 90594696 : case UNLT_EXPR:
4078 90594696 : case UNLE_EXPR:
4079 90594696 : case UNGT_EXPR:
4080 90594696 : case UNGE_EXPR:
4081 90594696 : case UNEQ_EXPR:
4082 90594696 : case LTGT_EXPR:
4083 : /* Comparisons are also binary, but the result type is not
4084 : connected to the operand types. */
4085 90594696 : return verify_gimple_comparison (lhs_type, rhs1, rhs2, rhs_code);
4086 :
4087 207451 : case WIDEN_MULT_EXPR:
4088 207451 : if (TREE_CODE (lhs_type) != INTEGER_TYPE)
4089 : return true;
4090 207451 : return ((2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type))
4091 207451 : || (TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type)));
4092 :
4093 0 : case WIDEN_SUM_EXPR:
4094 0 : {
4095 0 : if (((TREE_CODE (rhs1_type) != VECTOR_TYPE
4096 0 : || TREE_CODE (lhs_type) != VECTOR_TYPE)
4097 0 : && ((!INTEGRAL_TYPE_P (rhs1_type)
4098 0 : && !SCALAR_FLOAT_TYPE_P (rhs1_type))
4099 0 : || (!INTEGRAL_TYPE_P (lhs_type)
4100 0 : && !SCALAR_FLOAT_TYPE_P (lhs_type))))
4101 0 : || !useless_type_conversion_p (lhs_type, rhs2_type)
4102 0 : || maybe_lt (GET_MODE_SIZE (element_mode (rhs2_type)),
4103 0 : 2 * GET_MODE_SIZE (element_mode (rhs1_type))))
4104 : {
4105 0 : error ("type mismatch in %qs", code_name);
4106 0 : debug_generic_expr (lhs_type);
4107 0 : debug_generic_expr (rhs1_type);
4108 0 : debug_generic_expr (rhs2_type);
4109 0 : return true;
4110 : }
4111 : return false;
4112 : }
4113 :
4114 46418 : case VEC_WIDEN_MULT_HI_EXPR:
4115 46418 : case VEC_WIDEN_MULT_LO_EXPR:
4116 46418 : case VEC_WIDEN_MULT_EVEN_EXPR:
4117 46418 : case VEC_WIDEN_MULT_ODD_EXPR:
4118 46418 : {
4119 46418 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
4120 46418 : || TREE_CODE (lhs_type) != VECTOR_TYPE
4121 46418 : || !types_compatible_p (rhs1_type, rhs2_type)
4122 139254 : || maybe_ne (GET_MODE_SIZE (element_mode (lhs_type)),
4123 92836 : 2 * GET_MODE_SIZE (element_mode (rhs1_type))))
4124 : {
4125 0 : error ("type mismatch in %qs", code_name);
4126 0 : debug_generic_expr (lhs_type);
4127 0 : debug_generic_expr (rhs1_type);
4128 0 : debug_generic_expr (rhs2_type);
4129 0 : return true;
4130 : }
4131 : return false;
4132 : }
4133 :
4134 505070 : case VEC_PACK_TRUNC_EXPR:
4135 : /* ??? We currently use VEC_PACK_TRUNC_EXPR to simply concat
4136 : vector boolean types. */
4137 505070 : if (VECTOR_BOOLEAN_TYPE_P (lhs_type)
4138 121286 : && VECTOR_BOOLEAN_TYPE_P (rhs1_type)
4139 121286 : && types_compatible_p (rhs1_type, rhs2_type)
4140 1010140 : && known_eq (TYPE_VECTOR_SUBPARTS (lhs_type),
4141 : 2 * TYPE_VECTOR_SUBPARTS (rhs1_type)))
4142 121286 : return false;
4143 :
4144 : /* Fallthru. */
4145 398422 : case VEC_PACK_SAT_EXPR:
4146 398422 : case VEC_PACK_FIX_TRUNC_EXPR:
4147 398422 : {
4148 398422 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
4149 398422 : || TREE_CODE (lhs_type) != VECTOR_TYPE
4150 782206 : || !((rhs_code == VEC_PACK_FIX_TRUNC_EXPR
4151 14638 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type))
4152 14638 : && INTEGRAL_TYPE_P (TREE_TYPE (lhs_type)))
4153 383784 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
4154 383784 : == INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))))
4155 398422 : || !types_compatible_p (rhs1_type, rhs2_type)
4156 796844 : || maybe_ne (GET_MODE_SIZE (element_mode (rhs1_type)),
4157 796844 : 2 * GET_MODE_SIZE (element_mode (lhs_type)))
4158 796844 : || maybe_ne (2 * TYPE_VECTOR_SUBPARTS (rhs1_type),
4159 796844 : TYPE_VECTOR_SUBPARTS (lhs_type)))
4160 : {
4161 0 : error ("type mismatch in %qs", code_name);
4162 0 : debug_generic_expr (lhs_type);
4163 0 : debug_generic_expr (rhs1_type);
4164 0 : debug_generic_expr (rhs2_type);
4165 0 : return true;
4166 : }
4167 :
4168 : return false;
4169 : }
4170 :
4171 1213 : case VEC_PACK_FLOAT_EXPR:
4172 1213 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
4173 1213 : || TREE_CODE (lhs_type) != VECTOR_TYPE
4174 1213 : || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
4175 1213 : || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs_type))
4176 1213 : || !types_compatible_p (rhs1_type, rhs2_type)
4177 2426 : || maybe_ne (GET_MODE_SIZE (element_mode (rhs1_type)),
4178 2426 : 2 * GET_MODE_SIZE (element_mode (lhs_type)))
4179 2426 : || maybe_ne (2 * TYPE_VECTOR_SUBPARTS (rhs1_type),
4180 2426 : TYPE_VECTOR_SUBPARTS (lhs_type)))
4181 : {
4182 0 : error ("type mismatch in %qs", code_name);
4183 0 : debug_generic_expr (lhs_type);
4184 0 : debug_generic_expr (rhs1_type);
4185 0 : debug_generic_expr (rhs2_type);
4186 0 : return true;
4187 : }
4188 :
4189 : return false;
4190 :
4191 223655731 : case MULT_EXPR:
4192 223655731 : case MULT_HIGHPART_EXPR:
4193 223655731 : case TRUNC_DIV_EXPR:
4194 223655731 : case CEIL_DIV_EXPR:
4195 223655731 : case FLOOR_DIV_EXPR:
4196 223655731 : case ROUND_DIV_EXPR:
4197 223655731 : case TRUNC_MOD_EXPR:
4198 223655731 : case CEIL_MOD_EXPR:
4199 223655731 : case FLOOR_MOD_EXPR:
4200 223655731 : case ROUND_MOD_EXPR:
4201 223655731 : case RDIV_EXPR:
4202 223655731 : case EXACT_DIV_EXPR:
4203 223655731 : case BIT_IOR_EXPR:
4204 223655731 : case BIT_XOR_EXPR:
4205 : /* Disallow pointer and offset types for many of the binary gimple. */
4206 223655731 : if (POINTER_TYPE_P (lhs_type)
4207 223655731 : || TREE_CODE (lhs_type) == OFFSET_TYPE)
4208 : {
4209 0 : error ("invalid types for %qs", code_name);
4210 0 : debug_generic_expr (lhs_type);
4211 0 : debug_generic_expr (rhs1_type);
4212 0 : debug_generic_expr (rhs2_type);
4213 0 : return true;
4214 : }
4215 : /* Continue with generic binary expression handling. */
4216 : break;
4217 :
4218 : case MIN_EXPR:
4219 : case MAX_EXPR:
4220 : /* Continue with generic binary expression handling. */
4221 : break;
4222 :
4223 53329061 : case BIT_AND_EXPR:
4224 53329061 : if (POINTER_TYPE_P (lhs_type)
4225 175012 : && TREE_CODE (rhs2) == INTEGER_CST)
4226 : break;
4227 : /* Disallow pointer and offset types for many of the binary gimple. */
4228 53154049 : if (POINTER_TYPE_P (lhs_type)
4229 53154049 : || TREE_CODE (lhs_type) == OFFSET_TYPE)
4230 : {
4231 0 : error ("invalid types for %qs", code_name);
4232 0 : debug_generic_expr (lhs_type);
4233 0 : debug_generic_expr (rhs1_type);
4234 0 : debug_generic_expr (rhs2_type);
4235 0 : return true;
4236 : }
4237 : /* Continue with generic binary expression handling. */
4238 : break;
4239 :
4240 0 : case VEC_SERIES_EXPR:
4241 0 : if (!useless_type_conversion_p (rhs1_type, rhs2_type))
4242 : {
4243 0 : error ("type mismatch in %qs", code_name);
4244 0 : debug_generic_expr (rhs1_type);
4245 0 : debug_generic_expr (rhs2_type);
4246 0 : return true;
4247 : }
4248 0 : if (TREE_CODE (lhs_type) != VECTOR_TYPE
4249 0 : || !useless_type_conversion_p (TREE_TYPE (lhs_type), rhs1_type))
4250 : {
4251 0 : error ("vector type expected in %qs", code_name);
4252 0 : debug_generic_expr (lhs_type);
4253 0 : return true;
4254 : }
4255 : return false;
4256 :
4257 0 : default:
4258 0 : gcc_unreachable ();
4259 : }
4260 :
4261 775124415 : if (!useless_type_conversion_p (lhs_type, rhs1_type)
4262 775124415 : || !useless_type_conversion_p (lhs_type, rhs2_type))
4263 : {
4264 0 : error ("type mismatch in binary expression");
4265 0 : debug_generic_stmt (lhs_type);
4266 0 : debug_generic_stmt (rhs1_type);
4267 0 : debug_generic_stmt (rhs2_type);
4268 0 : return true;
4269 : }
4270 :
4271 : return false;
4272 : }
4273 :
4274 : /* Verify a gimple assignment statement STMT with a ternary rhs.
4275 : Returns true if anything is wrong. */
4276 :
4277 : static bool
4278 10239735 : verify_gimple_assign_ternary (gassign *stmt)
4279 : {
4280 10239735 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4281 10239735 : tree lhs = gimple_assign_lhs (stmt);
4282 10239735 : tree lhs_type = TREE_TYPE (lhs);
4283 10239735 : tree rhs1 = gimple_assign_rhs1 (stmt);
4284 10239735 : tree rhs1_type = TREE_TYPE (rhs1);
4285 10239735 : tree rhs2 = gimple_assign_rhs2 (stmt);
4286 10239735 : tree rhs2_type = TREE_TYPE (rhs2);
4287 10239735 : tree rhs3 = gimple_assign_rhs3 (stmt);
4288 10239735 : tree rhs3_type = TREE_TYPE (rhs3);
4289 :
4290 10239735 : if (!is_gimple_reg (lhs))
4291 : {
4292 0 : error ("non-register as LHS of ternary operation");
4293 0 : return true;
4294 : }
4295 :
4296 10239735 : if (!is_gimple_val (rhs1)
4297 10239735 : || !is_gimple_val (rhs2)
4298 20479470 : || !is_gimple_val (rhs3))
4299 : {
4300 0 : error ("invalid operands in ternary operation");
4301 0 : return true;
4302 : }
4303 :
4304 10239735 : const char* const code_name = get_tree_code_name (rhs_code);
4305 :
4306 : /* First handle operations that involve different types. */
4307 10239735 : switch (rhs_code)
4308 : {
4309 0 : case WIDEN_MULT_PLUS_EXPR:
4310 0 : case WIDEN_MULT_MINUS_EXPR:
4311 0 : if ((!INTEGRAL_TYPE_P (rhs1_type)
4312 0 : && !FIXED_POINT_TYPE_P (rhs1_type))
4313 0 : || !useless_type_conversion_p (rhs1_type, rhs2_type)
4314 0 : || !useless_type_conversion_p (lhs_type, rhs3_type)
4315 0 : || 2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)
4316 0 : || TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type))
4317 : {
4318 0 : error ("type mismatch in %qs", code_name);
4319 0 : debug_generic_expr (lhs_type);
4320 0 : debug_generic_expr (rhs1_type);
4321 0 : debug_generic_expr (rhs2_type);
4322 0 : debug_generic_expr (rhs3_type);
4323 0 : return true;
4324 : }
4325 : break;
4326 :
4327 1883113 : case VEC_COND_EXPR:
4328 1883113 : if (!VECTOR_BOOLEAN_TYPE_P (rhs1_type)
4329 3766226 : || maybe_ne (TYPE_VECTOR_SUBPARTS (rhs1_type),
4330 3766226 : TYPE_VECTOR_SUBPARTS (lhs_type)))
4331 : {
4332 0 : error ("the first argument of a %qs must be of a "
4333 : "boolean vector type of the same number of elements "
4334 : "as the result", code_name);
4335 0 : debug_generic_expr (lhs_type);
4336 0 : debug_generic_expr (rhs1_type);
4337 0 : return true;
4338 : }
4339 : /* Fallthrough. */
4340 3064286 : case COND_EXPR:
4341 3064286 : if (!useless_type_conversion_p (lhs_type, rhs2_type)
4342 3064286 : || !useless_type_conversion_p (lhs_type, rhs3_type))
4343 : {
4344 0 : error ("type mismatch in %qs", code_name);
4345 0 : debug_generic_expr (lhs_type);
4346 0 : debug_generic_expr (rhs2_type);
4347 0 : debug_generic_expr (rhs3_type);
4348 0 : return true;
4349 : }
4350 : break;
4351 :
4352 7066926 : case VEC_PERM_EXPR:
4353 : /* If permute is constant, then we allow for lhs and rhs
4354 : to have different vector types, provided:
4355 : (1) lhs, rhs1, rhs2 have same element type.
4356 : (2) rhs3 vector is constant and has integer element type.
4357 : (3) len(lhs) == len(rhs3) && len(rhs1) == len(rhs2). */
4358 :
4359 7066926 : if (TREE_CODE (lhs_type) != VECTOR_TYPE
4360 7066926 : || TREE_CODE (rhs1_type) != VECTOR_TYPE
4361 7066926 : || TREE_CODE (rhs2_type) != VECTOR_TYPE
4362 7066926 : || TREE_CODE (rhs3_type) != VECTOR_TYPE)
4363 : {
4364 0 : error ("vector types expected in %qs", code_name);
4365 0 : debug_generic_expr (lhs_type);
4366 0 : debug_generic_expr (rhs1_type);
4367 0 : debug_generic_expr (rhs2_type);
4368 0 : debug_generic_expr (rhs3_type);
4369 0 : return true;
4370 : }
4371 :
4372 : /* If rhs3 is constant, we allow lhs, rhs1 and rhs2 to be different vector types,
4373 : as long as lhs, rhs1 and rhs2 have same element type. */
4374 7066926 : if (TREE_CONSTANT (rhs3)
4375 7066926 : ? (!useless_type_conversion_p (TREE_TYPE (lhs_type), TREE_TYPE (rhs1_type))
4376 6945773 : || !useless_type_conversion_p (TREE_TYPE (lhs_type), TREE_TYPE (rhs2_type)))
4377 121153 : : (!useless_type_conversion_p (lhs_type, rhs1_type)
4378 121153 : || !useless_type_conversion_p (lhs_type, rhs2_type)))
4379 : {
4380 0 : error ("type mismatch in %qs", code_name);
4381 0 : debug_generic_expr (lhs_type);
4382 0 : debug_generic_expr (rhs1_type);
4383 0 : debug_generic_expr (rhs2_type);
4384 0 : debug_generic_expr (rhs3_type);
4385 0 : return true;
4386 : }
4387 :
4388 : /* If rhs3 is constant, relax the check len(rhs2) == len(rhs3). */
4389 7066926 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (rhs1_type),
4390 7066926 : TYPE_VECTOR_SUBPARTS (rhs2_type))
4391 7066926 : || (!TREE_CONSTANT(rhs3)
4392 121153 : && maybe_ne (TYPE_VECTOR_SUBPARTS (rhs2_type),
4393 121153 : TYPE_VECTOR_SUBPARTS (rhs3_type)))
4394 14133852 : || maybe_ne (TYPE_VECTOR_SUBPARTS (rhs3_type),
4395 7066926 : TYPE_VECTOR_SUBPARTS (lhs_type)))
4396 : {
4397 0 : error ("vectors with different element number found in %qs",
4398 : code_name);
4399 0 : debug_generic_expr (lhs_type);
4400 0 : debug_generic_expr (rhs1_type);
4401 0 : debug_generic_expr (rhs2_type);
4402 0 : debug_generic_expr (rhs3_type);
4403 0 : return true;
4404 : }
4405 :
4406 7066926 : if (TREE_CODE (TREE_TYPE (rhs3_type)) != INTEGER_TYPE
4407 7066926 : || (TREE_CODE (rhs3) != VECTOR_CST
4408 121153 : && (GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE
4409 : (TREE_TYPE (rhs3_type)))
4410 7188079 : != GET_MODE_BITSIZE (SCALAR_TYPE_MODE
4411 : (TREE_TYPE (rhs1_type))))))
4412 : {
4413 0 : error ("invalid mask type in %qs", code_name);
4414 0 : debug_generic_expr (lhs_type);
4415 0 : debug_generic_expr (rhs1_type);
4416 0 : debug_generic_expr (rhs2_type);
4417 0 : debug_generic_expr (rhs3_type);
4418 0 : return true;
4419 : }
4420 :
4421 : return false;
4422 :
4423 4929 : case SAD_EXPR:
4424 4929 : if (!useless_type_conversion_p (rhs1_type, rhs2_type)
4425 4929 : || !useless_type_conversion_p (lhs_type, rhs3_type)
4426 9858 : || 2 * GET_MODE_UNIT_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1_type)))
4427 9858 : > GET_MODE_UNIT_BITSIZE (TYPE_MODE (TREE_TYPE (lhs_type))))
4428 : {
4429 0 : error ("type mismatch in %qs", code_name);
4430 0 : debug_generic_expr (lhs_type);
4431 0 : debug_generic_expr (rhs1_type);
4432 0 : debug_generic_expr (rhs2_type);
4433 0 : debug_generic_expr (rhs3_type);
4434 0 : return true;
4435 : }
4436 :
4437 4929 : if (TREE_CODE (rhs1_type) != VECTOR_TYPE
4438 4929 : || TREE_CODE (rhs2_type) != VECTOR_TYPE
4439 4929 : || TREE_CODE (rhs3_type) != VECTOR_TYPE)
4440 : {
4441 0 : error ("vector types expected in %qs", code_name);
4442 0 : debug_generic_expr (lhs_type);
4443 0 : debug_generic_expr (rhs1_type);
4444 0 : debug_generic_expr (rhs2_type);
4445 0 : debug_generic_expr (rhs3_type);
4446 0 : return true;
4447 : }
4448 :
4449 : return false;
4450 :
4451 82681 : case BIT_INSERT_EXPR:
4452 82681 : if (! useless_type_conversion_p (lhs_type, rhs1_type))
4453 : {
4454 0 : error ("type mismatch in %qs", code_name);
4455 0 : debug_generic_expr (lhs_type);
4456 0 : debug_generic_expr (rhs1_type);
4457 0 : return true;
4458 : }
4459 82681 : if (! ((INTEGRAL_TYPE_P (rhs1_type)
4460 5554 : && INTEGRAL_TYPE_P (rhs2_type))
4461 : /* Vector element insert. */
4462 77127 : || (VECTOR_TYPE_P (rhs1_type)
4463 77127 : && types_compatible_p (TREE_TYPE (rhs1_type), rhs2_type))
4464 : /* Aligned sub-vector insert. */
4465 4322 : || (VECTOR_TYPE_P (rhs1_type)
4466 4322 : && VECTOR_TYPE_P (rhs2_type)
4467 4322 : && types_compatible_p (TREE_TYPE (rhs1_type),
4468 4322 : TREE_TYPE (rhs2_type))
4469 4322 : && multiple_p (TYPE_VECTOR_SUBPARTS (rhs1_type),
4470 4322 : TYPE_VECTOR_SUBPARTS (rhs2_type))
4471 4322 : && multiple_p (wi::to_poly_offset (rhs3),
4472 4322 : wi::to_poly_offset (TYPE_SIZE (rhs2_type))))))
4473 : {
4474 0 : error ("not allowed type combination in %qs", code_name);
4475 0 : debug_generic_expr (rhs1_type);
4476 0 : debug_generic_expr (rhs2_type);
4477 0 : return true;
4478 : }
4479 82681 : if (! tree_fits_uhwi_p (rhs3)
4480 82681 : || ! types_compatible_p (bitsizetype, TREE_TYPE (rhs3))
4481 165362 : || ! tree_fits_uhwi_p (TYPE_SIZE (rhs2_type)))
4482 : {
4483 0 : error ("invalid position or size in %qs", code_name);
4484 0 : return true;
4485 : }
4486 82681 : if (INTEGRAL_TYPE_P (rhs1_type)
4487 82681 : && !type_has_mode_precision_p (rhs1_type))
4488 : {
4489 0 : error ("%qs into non-mode-precision operand", code_name);
4490 0 : return true;
4491 : }
4492 82681 : if (INTEGRAL_TYPE_P (rhs1_type))
4493 : {
4494 5554 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (rhs3);
4495 5554 : if (bitpos >= TYPE_PRECISION (rhs1_type)
4496 5554 : || (bitpos + TYPE_PRECISION (rhs2_type)
4497 5554 : > TYPE_PRECISION (rhs1_type)))
4498 : {
4499 0 : error ("insertion out of range in %qs", code_name);
4500 0 : return true;
4501 : }
4502 : }
4503 77127 : else if (VECTOR_TYPE_P (rhs1_type))
4504 : {
4505 77127 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (rhs3);
4506 77127 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (TYPE_SIZE (rhs2_type));
4507 77127 : if (bitpos % bitsize != 0)
4508 : {
4509 0 : error ("%qs not at element boundary", code_name);
4510 0 : return true;
4511 : }
4512 : }
4513 : return false;
4514 :
4515 20913 : case DOT_PROD_EXPR:
4516 20913 : {
4517 20913 : if (((TREE_CODE (rhs1_type) != VECTOR_TYPE
4518 20913 : || TREE_CODE (lhs_type) != VECTOR_TYPE)
4519 0 : && ((!INTEGRAL_TYPE_P (rhs1_type)
4520 0 : && !SCALAR_FLOAT_TYPE_P (rhs1_type))
4521 0 : || (!INTEGRAL_TYPE_P (lhs_type)
4522 0 : && !SCALAR_FLOAT_TYPE_P (lhs_type))))
4523 : /* rhs1_type and rhs2_type may differ in sign. */
4524 20913 : || !tree_nop_conversion_p (rhs1_type, rhs2_type)
4525 20913 : || !useless_type_conversion_p (lhs_type, rhs3_type)
4526 62739 : || maybe_lt (GET_MODE_SIZE (element_mode (rhs3_type)),
4527 41826 : 2 * GET_MODE_SIZE (element_mode (rhs1_type))))
4528 : {
4529 0 : error ("type mismatch in %qs", code_name);
4530 0 : debug_generic_expr (lhs_type);
4531 0 : debug_generic_expr (rhs1_type);
4532 0 : debug_generic_expr (rhs2_type);
4533 0 : return true;
4534 : }
4535 : return false;
4536 : }
4537 :
4538 : case REALIGN_LOAD_EXPR:
4539 : /* FIXME. */
4540 : return false;
4541 :
4542 0 : default:
4543 0 : gcc_unreachable ();
4544 : }
4545 : return false;
4546 : }
4547 :
4548 : /* Verify a gimple assignment statement STMT with a single rhs.
4549 : Returns true if anything is wrong. */
4550 :
4551 : static bool
4552 3235681347 : verify_gimple_assign_single (gassign *stmt)
4553 : {
4554 3235681347 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4555 3235681347 : tree lhs = gimple_assign_lhs (stmt);
4556 3235681347 : tree lhs_type = TREE_TYPE (lhs);
4557 3235681347 : tree rhs1 = gimple_assign_rhs1 (stmt);
4558 3235681347 : tree rhs1_type = TREE_TYPE (rhs1);
4559 3235681347 : bool res = false;
4560 :
4561 3235681347 : const char* const code_name = get_tree_code_name (rhs_code);
4562 :
4563 3235681347 : if (!useless_type_conversion_p (lhs_type, rhs1_type))
4564 : {
4565 0 : error ("non-trivial conversion in %qs", code_name);
4566 0 : debug_generic_expr (lhs_type);
4567 0 : debug_generic_expr (rhs1_type);
4568 0 : return true;
4569 : }
4570 :
4571 : /* LHS can't be a constant or an address expression. */
4572 3235681347 : if (CONSTANT_CLASS_P (lhs)|| TREE_CODE (lhs) == ADDR_EXPR)
4573 : {
4574 0 : error ("invalid LHS (%qs) for assignment: %qs",
4575 : get_tree_code_name (TREE_CODE (lhs)), code_name);
4576 0 : return true;
4577 : }
4578 :
4579 3235681347 : if (gimple_clobber_p (stmt)
4580 3235681347 : && !(DECL_P (lhs) || TREE_CODE (lhs) == MEM_REF))
4581 : {
4582 0 : error ("%qs LHS in clobber statement",
4583 : get_tree_code_name (TREE_CODE (lhs)));
4584 0 : debug_generic_expr (lhs);
4585 0 : return true;
4586 : }
4587 :
4588 3235681347 : if (TREE_CODE (lhs) == WITH_SIZE_EXPR)
4589 : {
4590 0 : error ("%qs LHS in assignment statement",
4591 : get_tree_code_name (TREE_CODE (lhs)));
4592 0 : debug_generic_expr (lhs);
4593 0 : return true;
4594 : }
4595 :
4596 3235681347 : if (handled_component_p (lhs)
4597 2391888957 : || TREE_CODE (lhs) == MEM_REF
4598 2133525691 : || TREE_CODE (lhs) == TARGET_MEM_REF)
4599 1111712839 : res |= verify_types_in_gimple_reference (lhs, true);
4600 :
4601 : /* Special codes we cannot handle via their class. */
4602 3235681347 : switch (rhs_code)
4603 : {
4604 270045689 : case ADDR_EXPR:
4605 270045689 : {
4606 270045689 : tree op = TREE_OPERAND (rhs1, 0);
4607 270045689 : if (!is_gimple_addressable (op))
4608 : {
4609 0 : error ("invalid operand in %qs", code_name);
4610 0 : return true;
4611 : }
4612 :
4613 : /* Technically there is no longer a need for matching types, but
4614 : gimple hygiene asks for this check. In LTO we can end up
4615 : combining incompatible units and thus end up with addresses
4616 : of globals that change their type to a common one. */
4617 270045689 : if (!in_lto_p
4618 269528269 : && !types_compatible_p (TREE_TYPE (op),
4619 269528269 : TREE_TYPE (TREE_TYPE (rhs1)))
4620 270046164 : && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
4621 475 : TREE_TYPE (op)))
4622 : {
4623 0 : error ("type mismatch in %qs", code_name);
4624 0 : debug_generic_stmt (TREE_TYPE (rhs1));
4625 0 : debug_generic_stmt (TREE_TYPE (op));
4626 0 : return true;
4627 : }
4628 :
4629 270045689 : return (verify_address (rhs1, true)
4630 270045689 : || verify_types_in_gimple_reference (op, true));
4631 : }
4632 :
4633 : /* tcc_reference */
4634 0 : case INDIRECT_REF:
4635 0 : error ("%qs in gimple IL", code_name);
4636 0 : return true;
4637 :
4638 46 : case WITH_SIZE_EXPR:
4639 46 : if (!is_gimple_val (TREE_OPERAND (rhs1, 1)))
4640 : {
4641 0 : error ("invalid %qs size argument in load", code_name);
4642 0 : debug_generic_stmt (lhs);
4643 0 : debug_generic_stmt (rhs1);
4644 0 : return true;
4645 : }
4646 46 : rhs1 = TREE_OPERAND (rhs1, 0);
4647 : /* Fallthru. */
4648 1103852962 : case COMPONENT_REF:
4649 1103852962 : case BIT_FIELD_REF:
4650 1103852962 : case ARRAY_REF:
4651 1103852962 : case ARRAY_RANGE_REF:
4652 1103852962 : case VIEW_CONVERT_EXPR:
4653 1103852962 : case REALPART_EXPR:
4654 1103852962 : case IMAGPART_EXPR:
4655 1103852962 : case TARGET_MEM_REF:
4656 1103852962 : case MEM_REF:
4657 1103852962 : if (!is_gimple_reg (lhs)
4658 1103852962 : && is_gimple_reg_type (TREE_TYPE (lhs)))
4659 : {
4660 0 : error ("invalid RHS for gimple memory store: %qs", code_name);
4661 0 : debug_generic_stmt (lhs);
4662 0 : debug_generic_stmt (rhs1);
4663 0 : return true;
4664 : }
4665 1103852962 : return res || verify_types_in_gimple_reference (rhs1, false);
4666 :
4667 : /* tcc_constant */
4668 : case SSA_NAME:
4669 : case INTEGER_CST:
4670 : case REAL_CST:
4671 : case FIXED_CST:
4672 : case COMPLEX_CST:
4673 : case VECTOR_CST:
4674 : case STRING_CST:
4675 : return res;
4676 :
4677 : /* tcc_declaration */
4678 : case CONST_DECL:
4679 : return res;
4680 333118350 : case VAR_DECL:
4681 333118350 : case PARM_DECL:
4682 333118350 : if (!is_gimple_reg (lhs)
4683 90791784 : && !is_gimple_reg (rhs1)
4684 411749240 : && is_gimple_reg_type (TREE_TYPE (lhs)))
4685 : {
4686 0 : error ("invalid RHS for gimple memory store: %qs", code_name);
4687 0 : debug_generic_stmt (lhs);
4688 0 : debug_generic_stmt (rhs1);
4689 0 : return true;
4690 : }
4691 : return res;
4692 :
4693 334362218 : case CONSTRUCTOR:
4694 334362218 : if (VECTOR_TYPE_P (rhs1_type))
4695 : {
4696 7087490 : unsigned int i;
4697 7087490 : tree elt_i, elt_v, elt_t = NULL_TREE;
4698 :
4699 7087490 : if (CONSTRUCTOR_NELTS (rhs1) == 0)
4700 : return res;
4701 6358414 : if (!is_gimple_reg (lhs))
4702 : {
4703 0 : error ("non-register as LHS with vector constructor");
4704 0 : return true;
4705 : }
4706 : /* For vector CONSTRUCTORs we require that either it is empty
4707 : CONSTRUCTOR, or it is a CONSTRUCTOR of smaller vector elements
4708 : (then the element count must be correct to cover the whole
4709 : outer vector and index must be NULL on all elements, or it is
4710 : a CONSTRUCTOR of scalar elements, where we as an exception allow
4711 : smaller number of elements (assuming zero filling) and
4712 : consecutive indexes as compared to NULL indexes (such
4713 : CONSTRUCTORs can appear in the IL from FEs). */
4714 29731259 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), i, elt_i, elt_v)
4715 : {
4716 23372845 : if (elt_t == NULL_TREE)
4717 : {
4718 6358414 : elt_t = TREE_TYPE (elt_v);
4719 6358414 : if (VECTOR_TYPE_P (elt_t))
4720 : {
4721 207385 : tree elt_t = TREE_TYPE (elt_v);
4722 207385 : if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
4723 207385 : TREE_TYPE (elt_t)))
4724 : {
4725 0 : error ("incorrect type of vector %qs elements",
4726 : code_name);
4727 0 : debug_generic_stmt (rhs1);
4728 0 : return true;
4729 : }
4730 414770 : else if (maybe_ne (CONSTRUCTOR_NELTS (rhs1)
4731 414770 : * TYPE_VECTOR_SUBPARTS (elt_t),
4732 414770 : TYPE_VECTOR_SUBPARTS (rhs1_type)))
4733 : {
4734 0 : error ("incorrect number of vector %qs elements",
4735 : code_name);
4736 0 : debug_generic_stmt (rhs1);
4737 0 : return true;
4738 : }
4739 : }
4740 6151029 : else if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
4741 : elt_t))
4742 : {
4743 0 : error ("incorrect type of vector %qs elements",
4744 : code_name);
4745 0 : debug_generic_stmt (rhs1);
4746 0 : return true;
4747 : }
4748 12302058 : else if (maybe_gt (CONSTRUCTOR_NELTS (rhs1),
4749 : TYPE_VECTOR_SUBPARTS (rhs1_type)))
4750 : {
4751 0 : error ("incorrect number of vector %qs elements",
4752 : code_name);
4753 0 : debug_generic_stmt (rhs1);
4754 0 : return true;
4755 : }
4756 : }
4757 17014431 : else if (!useless_type_conversion_p (elt_t, TREE_TYPE (elt_v)))
4758 : {
4759 0 : error ("incorrect type of vector CONSTRUCTOR elements");
4760 0 : debug_generic_stmt (rhs1);
4761 0 : return true;
4762 : }
4763 23372845 : if (elt_i != NULL_TREE
4764 23372845 : && (VECTOR_TYPE_P (elt_t)
4765 8035288 : || TREE_CODE (elt_i) != INTEGER_CST
4766 8035288 : || compare_tree_int (elt_i, i) != 0))
4767 : {
4768 0 : error ("vector %qs with non-NULL element index",
4769 : code_name);
4770 0 : debug_generic_stmt (rhs1);
4771 0 : return true;
4772 : }
4773 23372845 : if (!is_gimple_val (elt_v))
4774 : {
4775 0 : error ("vector %qs element is not a GIMPLE value",
4776 : code_name);
4777 0 : debug_generic_stmt (rhs1);
4778 0 : return true;
4779 : }
4780 : }
4781 : }
4782 327274728 : else if (CONSTRUCTOR_NELTS (rhs1) != 0)
4783 : {
4784 0 : error ("non-vector %qs with elements", code_name);
4785 0 : debug_generic_stmt (rhs1);
4786 0 : return true;
4787 : }
4788 : return res;
4789 :
4790 : case OBJ_TYPE_REF:
4791 : /* FIXME. */
4792 : return res;
4793 :
4794 : default:;
4795 : }
4796 :
4797 : return res;
4798 : }
4799 :
4800 : /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
4801 : is a problem, otherwise false. */
4802 :
4803 : static bool
4804 4760616645 : verify_gimple_assign (gassign *stmt)
4805 : {
4806 4760616645 : if (gimple_assign_nontemporal_move_p (stmt))
4807 : {
4808 1188 : tree lhs = gimple_assign_lhs (stmt);
4809 1188 : if (is_gimple_reg (lhs))
4810 : {
4811 0 : error ("nontemporal store lhs cannot be a gimple register");
4812 0 : debug_generic_stmt (lhs);
4813 0 : return true;
4814 : }
4815 : }
4816 :
4817 4760616645 : switch (gimple_assign_rhs_class (stmt))
4818 : {
4819 3235681347 : case GIMPLE_SINGLE_RHS:
4820 3235681347 : return verify_gimple_assign_single (stmt);
4821 :
4822 435187398 : case GIMPLE_UNARY_RHS:
4823 435187398 : return verify_gimple_assign_unary (stmt);
4824 :
4825 1079508165 : case GIMPLE_BINARY_RHS:
4826 1079508165 : return verify_gimple_assign_binary (stmt);
4827 :
4828 10239735 : case GIMPLE_TERNARY_RHS:
4829 10239735 : return verify_gimple_assign_ternary (stmt);
4830 :
4831 0 : default:
4832 0 : gcc_unreachable ();
4833 : }
4834 : }
4835 :
4836 : /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
4837 : is a problem, otherwise false. */
4838 :
4839 : static bool
4840 265866164 : verify_gimple_return (greturn *stmt)
4841 : {
4842 265866164 : tree op = gimple_return_retval (stmt);
4843 265866164 : tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
4844 265866164 : tree resdecl = DECL_RESULT (cfun->decl);
4845 :
4846 : /* We cannot test for present return values as we do not fix up missing
4847 : return values from the original source. */
4848 265866164 : if (op == NULL)
4849 : return false;
4850 :
4851 148687114 : if (!is_gimple_val (op)
4852 148687114 : && TREE_CODE (op) != RESULT_DECL)
4853 : {
4854 0 : error ("invalid operand in return statement");
4855 0 : debug_generic_stmt (op);
4856 0 : return true;
4857 : }
4858 :
4859 148687114 : if (resdecl && DECL_BY_REFERENCE (resdecl))
4860 3377972 : op = TREE_TYPE (op);
4861 :
4862 148687114 : if (!useless_type_conversion_p (restype, TREE_TYPE (op)))
4863 : {
4864 0 : error ("invalid conversion in return statement");
4865 0 : debug_generic_stmt (restype);
4866 0 : debug_generic_stmt (TREE_TYPE (op));
4867 0 : return true;
4868 : }
4869 :
4870 : return false;
4871 : }
4872 :
4873 :
4874 : /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
4875 : is a problem, otherwise false. */
4876 :
4877 : static bool
4878 26392957 : verify_gimple_goto (ggoto *stmt)
4879 : {
4880 26392957 : tree dest = gimple_goto_dest (stmt);
4881 :
4882 : /* ??? We have two canonical forms of direct goto destinations, a
4883 : bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
4884 26392957 : if (TREE_CODE (dest) != LABEL_DECL
4885 26392957 : && (!is_gimple_val (dest)
4886 66248 : || !POINTER_TYPE_P (TREE_TYPE (dest))))
4887 : {
4888 0 : error ("goto destination is neither a label nor a pointer");
4889 0 : return true;
4890 : }
4891 :
4892 : return false;
4893 : }
4894 :
4895 : /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
4896 : is a problem, otherwise false. */
4897 :
4898 : static bool
4899 5030706 : verify_gimple_switch (gswitch *stmt)
4900 : {
4901 5030706 : unsigned int i, n;
4902 5030706 : tree elt, prev_upper_bound = NULL_TREE;
4903 5030706 : tree index_type, elt_type = NULL_TREE;
4904 :
4905 5030706 : if (!is_gimple_val (gimple_switch_index (stmt)))
4906 : {
4907 0 : error ("invalid operand to switch statement");
4908 0 : debug_generic_stmt (gimple_switch_index (stmt));
4909 0 : return true;
4910 : }
4911 :
4912 5030706 : index_type = TREE_TYPE (gimple_switch_index (stmt));
4913 5030706 : if (! INTEGRAL_TYPE_P (index_type))
4914 : {
4915 0 : error ("non-integral type switch statement");
4916 0 : debug_generic_expr (index_type);
4917 0 : return true;
4918 : }
4919 :
4920 5030706 : elt = gimple_switch_label (stmt, 0);
4921 5030706 : if (CASE_LOW (elt) != NULL_TREE
4922 5030706 : || CASE_HIGH (elt) != NULL_TREE
4923 10061412 : || CASE_CHAIN (elt) != NULL_TREE)
4924 : {
4925 0 : error ("invalid default case label in switch statement");
4926 0 : debug_generic_expr (elt);
4927 0 : return true;
4928 : }
4929 :
4930 5030706 : n = gimple_switch_num_labels (stmt);
4931 43046938 : for (i = 1; i < n; i++)
4932 : {
4933 32985526 : elt = gimple_switch_label (stmt, i);
4934 :
4935 32985526 : if (CASE_CHAIN (elt))
4936 : {
4937 0 : error ("invalid %<CASE_CHAIN%>");
4938 0 : debug_generic_expr (elt);
4939 0 : return true;
4940 : }
4941 32985526 : if (! CASE_LOW (elt))
4942 : {
4943 0 : error ("invalid case label in switch statement");
4944 0 : debug_generic_expr (elt);
4945 0 : return true;
4946 : }
4947 32985526 : if (CASE_HIGH (elt)
4948 32985526 : && ! tree_int_cst_lt (CASE_LOW (elt), CASE_HIGH (elt)))
4949 : {
4950 0 : error ("invalid case range in switch statement");
4951 0 : debug_generic_expr (elt);
4952 0 : return true;
4953 : }
4954 :
4955 32985526 : if (! elt_type)
4956 : {
4957 5027565 : elt_type = TREE_TYPE (CASE_LOW (elt));
4958 5027565 : if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
4959 : {
4960 0 : error ("type precision mismatch in switch statement");
4961 0 : return true;
4962 : }
4963 : }
4964 32985526 : if (TREE_TYPE (CASE_LOW (elt)) != elt_type
4965 32985526 : || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
4966 : {
4967 0 : error ("type mismatch for case label in switch statement");
4968 0 : debug_generic_expr (elt);
4969 0 : return true;
4970 : }
4971 :
4972 32985526 : if (prev_upper_bound)
4973 : {
4974 27957961 : if (! tree_int_cst_lt (prev_upper_bound, CASE_LOW (elt)))
4975 : {
4976 0 : error ("case labels not sorted in switch statement");
4977 0 : return true;
4978 : }
4979 : }
4980 :
4981 32985526 : prev_upper_bound = CASE_HIGH (elt);
4982 32985526 : if (! prev_upper_bound)
4983 30913842 : prev_upper_bound = CASE_LOW (elt);
4984 : }
4985 :
4986 : return false;
4987 : }
4988 :
4989 : /* Verify a gimple debug statement STMT.
4990 : Returns true if anything is wrong. */
4991 :
4992 : static bool
4993 0 : verify_gimple_debug (gimple *stmt ATTRIBUTE_UNUSED)
4994 : {
4995 : /* There isn't much that could be wrong in a gimple debug stmt. A
4996 : gimple debug bind stmt, for example, maps a tree, that's usually
4997 : a VAR_DECL or a PARM_DECL, but that could also be some scalarized
4998 : component or member of an aggregate type, to another tree, that
4999 : can be an arbitrary expression. These stmts expand into debug
5000 : insns, and are converted to debug notes by var-tracking.cc. */
5001 0 : return false;
5002 : }
5003 :
5004 : /* Verify a gimple label statement STMT.
5005 : Returns true if anything is wrong. */
5006 :
5007 : static bool
5008 234433172 : verify_gimple_label (glabel *stmt)
5009 : {
5010 234433172 : tree decl = gimple_label_label (stmt);
5011 234433172 : int uid;
5012 234433172 : bool err = false;
5013 :
5014 234433172 : if (TREE_CODE (decl) != LABEL_DECL)
5015 : return true;
5016 468806156 : if (!DECL_NONLOCAL (decl) && !FORCED_LABEL (decl)
5017 465423016 : && DECL_CONTEXT (decl) != current_function_decl)
5018 : {
5019 0 : error ("label context is not the current function declaration");
5020 0 : err |= true;
5021 : }
5022 :
5023 234433172 : uid = LABEL_DECL_UID (decl);
5024 234433172 : if (cfun->cfg
5025 234433172 : && (uid == -1
5026 146060129 : || (*label_to_block_map_for_fn (cfun))[uid] != gimple_bb (stmt)))
5027 : {
5028 0 : error ("incorrect entry in %<label_to_block_map%>");
5029 0 : err |= true;
5030 : }
5031 :
5032 234433172 : uid = EH_LANDING_PAD_NR (decl);
5033 234433172 : if (uid)
5034 : {
5035 80643881 : eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
5036 80643881 : if (decl != lp->post_landing_pad)
5037 : {
5038 0 : error ("incorrect setting of landing pad number");
5039 0 : err |= true;
5040 : }
5041 : }
5042 :
5043 : return err;
5044 : }
5045 :
5046 : /* Verify a gimple cond statement STMT.
5047 : Returns true if anything is wrong. */
5048 :
5049 : static bool
5050 780624064 : verify_gimple_cond (gcond *stmt)
5051 : {
5052 780624064 : if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
5053 : {
5054 0 : error ("invalid comparison code in gimple cond");
5055 0 : return true;
5056 : }
5057 780624064 : if (!(!gimple_cond_true_label (stmt)
5058 29797269 : || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
5059 810421333 : || !(!gimple_cond_false_label (stmt)
5060 29797269 : || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
5061 : {
5062 0 : error ("invalid labels in gimple cond");
5063 0 : return true;
5064 : }
5065 :
5066 780624064 : tree lhs = gimple_cond_lhs (stmt);
5067 :
5068 : /* GIMPLE_CONDs condition may not throw. */
5069 780624064 : if (flag_exceptions
5070 445306322 : && cfun->can_throw_non_call_exceptions
5071 1098916614 : && operation_could_trap_p (gimple_cond_code (stmt),
5072 318292550 : FLOAT_TYPE_P (TREE_TYPE (lhs)),
5073 : false, NULL_TREE))
5074 : {
5075 0 : error ("gimple cond condition cannot throw");
5076 0 : return true;
5077 : }
5078 :
5079 780624064 : return verify_gimple_comparison (boolean_type_node,
5080 : gimple_cond_lhs (stmt),
5081 : gimple_cond_rhs (stmt),
5082 780624064 : gimple_cond_code (stmt));
5083 : }
5084 :
5085 : /* Verify the GIMPLE statement STMT. Returns true if there is an
5086 : error, otherwise false. */
5087 :
5088 : static bool
5089 14055683269 : verify_gimple_stmt (gimple *stmt)
5090 : {
5091 14055683269 : switch (gimple_code (stmt))
5092 : {
5093 4760616645 : case GIMPLE_ASSIGN:
5094 4760616645 : return verify_gimple_assign (as_a <gassign *> (stmt));
5095 :
5096 234433172 : case GIMPLE_LABEL:
5097 234433172 : return verify_gimple_label (as_a <glabel *> (stmt));
5098 :
5099 1109502755 : case GIMPLE_CALL:
5100 1109502755 : return verify_gimple_call (as_a <gcall *> (stmt));
5101 :
5102 780624064 : case GIMPLE_COND:
5103 780624064 : return verify_gimple_cond (as_a <gcond *> (stmt));
5104 :
5105 26392957 : case GIMPLE_GOTO:
5106 26392957 : return verify_gimple_goto (as_a <ggoto *> (stmt));
5107 :
5108 5030706 : case GIMPLE_SWITCH:
5109 5030706 : return verify_gimple_switch (as_a <gswitch *> (stmt));
5110 :
5111 265866164 : case GIMPLE_RETURN:
5112 265866164 : return verify_gimple_return (as_a <greturn *> (stmt));
5113 :
5114 : case GIMPLE_ASM:
5115 : return false;
5116 :
5117 31303 : case GIMPLE_TRANSACTION:
5118 31303 : return verify_gimple_transaction (as_a <gtransaction *> (stmt));
5119 :
5120 : /* Tuples that do not have tree operands. */
5121 : case GIMPLE_NOP:
5122 : case GIMPLE_PREDICT:
5123 : case GIMPLE_RESX:
5124 : case GIMPLE_EH_DISPATCH:
5125 : case GIMPLE_EH_MUST_NOT_THROW:
5126 : return false;
5127 :
5128 : CASE_GIMPLE_OMP:
5129 : /* OpenMP directives are validated by the FE and never operated
5130 : on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
5131 : non-gimple expressions when the main index variable has had
5132 : its address taken. This does not affect the loop itself
5133 : because the header of an GIMPLE_OMP_FOR is merely used to determine
5134 : how to setup the parallel iteration. */
5135 : return false;
5136 :
5137 : case GIMPLE_ASSUME:
5138 : return false;
5139 :
5140 : case GIMPLE_DEBUG:
5141 : return verify_gimple_debug (stmt);
5142 :
5143 0 : default:
5144 0 : gcc_unreachable ();
5145 : }
5146 : }
5147 :
5148 : /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
5149 : and false otherwise. */
5150 :
5151 : static bool
5152 674203224 : verify_gimple_phi (gphi *phi)
5153 : {
5154 674203224 : bool err = false;
5155 674203224 : unsigned i;
5156 674203224 : tree phi_result = gimple_phi_result (phi);
5157 674203224 : bool virtual_p;
5158 :
5159 674203224 : if (!phi_result)
5160 : {
5161 0 : error ("invalid %<PHI%> result");
5162 0 : return true;
5163 : }
5164 :
5165 674203224 : virtual_p = virtual_operand_p (phi_result);
5166 674203224 : if (TREE_CODE (phi_result) != SSA_NAME
5167 674203224 : || (virtual_p
5168 314890114 : && SSA_NAME_VAR (phi_result) != gimple_vop (cfun)))
5169 : {
5170 0 : error ("invalid %<PHI%> result");
5171 0 : err = true;
5172 : }
5173 :
5174 2382036421 : for (i = 0; i < gimple_phi_num_args (phi); i++)
5175 : {
5176 1707833197 : tree t = gimple_phi_arg_def (phi, i);
5177 :
5178 1707833197 : if (!t)
5179 : {
5180 0 : error ("missing %<PHI%> def");
5181 0 : err |= true;
5182 0 : continue;
5183 : }
5184 : /* Addressable variables do have SSA_NAMEs but they
5185 : are not considered gimple values. */
5186 1707833197 : else if ((TREE_CODE (t) == SSA_NAME
5187 1472705061 : && virtual_p != virtual_operand_p (t))
5188 1707833197 : || (virtual_p
5189 836192133 : && (TREE_CODE (t) != SSA_NAME
5190 836192133 : || SSA_NAME_VAR (t) != gimple_vop (cfun)))
5191 1707833197 : || (!virtual_p
5192 871641064 : && !is_gimple_val (t)))
5193 : {
5194 0 : error ("invalid %<PHI%> argument");
5195 0 : debug_generic_expr (t);
5196 0 : err |= true;
5197 : }
5198 : #ifdef ENABLE_TYPES_CHECKING
5199 1707833197 : if (!useless_type_conversion_p (TREE_TYPE (phi_result), TREE_TYPE (t)))
5200 : {
5201 0 : error ("incompatible types in %<PHI%> argument %u", i);
5202 0 : debug_generic_stmt (TREE_TYPE (phi_result));
5203 0 : debug_generic_stmt (TREE_TYPE (t));
5204 0 : err |= true;
5205 : }
5206 : #endif
5207 : }
5208 :
5209 : return err;
5210 : }
5211 :
5212 : /* Verify the GIMPLE statements inside the sequence STMTS. */
5213 :
5214 : static bool
5215 57158262 : verify_gimple_in_seq_2 (gimple_seq stmts)
5216 : {
5217 57158262 : gimple_stmt_iterator ittr;
5218 57158262 : bool err = false;
5219 :
5220 553084776 : for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
5221 : {
5222 495926514 : gimple *stmt = gsi_stmt (ittr);
5223 :
5224 495926514 : switch (gimple_code (stmt))
5225 : {
5226 15246509 : case GIMPLE_BIND:
5227 15246509 : err |= verify_gimple_in_seq_2 (
5228 15246509 : gimple_bind_body (as_a <gbind *> (stmt)));
5229 15246509 : break;
5230 :
5231 11715500 : case GIMPLE_TRY:
5232 11715500 : err |= verify_gimple_in_seq_2 (gimple_try_eval (stmt));
5233 11715500 : err |= verify_gimple_in_seq_2 (gimple_try_cleanup (stmt));
5234 11715500 : break;
5235 :
5236 21382 : case GIMPLE_EH_FILTER:
5237 21382 : err |= verify_gimple_in_seq_2 (gimple_eh_filter_failure (stmt));
5238 21382 : break;
5239 :
5240 1878 : case GIMPLE_EH_ELSE:
5241 1878 : {
5242 1878 : geh_else *eh_else = as_a <geh_else *> (stmt);
5243 1878 : err |= verify_gimple_in_seq_2 (gimple_eh_else_n_body (eh_else));
5244 1878 : err |= verify_gimple_in_seq_2 (gimple_eh_else_e_body (eh_else));
5245 : }
5246 1878 : break;
5247 :
5248 192515 : case GIMPLE_CATCH:
5249 192515 : err |= verify_gimple_in_seq_2 (gimple_catch_handler (
5250 192515 : as_a <gcatch *> (stmt)));
5251 192515 : break;
5252 :
5253 618 : case GIMPLE_ASSUME:
5254 618 : err |= verify_gimple_in_seq_2 (gimple_assume_body (stmt));
5255 618 : break;
5256 :
5257 3076 : case GIMPLE_TRANSACTION:
5258 3076 : err |= verify_gimple_transaction (as_a <gtransaction *> (stmt));
5259 3076 : break;
5260 :
5261 468745036 : default:
5262 468745036 : {
5263 468745036 : bool err2 = verify_gimple_stmt (stmt);
5264 468745036 : if (err2)
5265 1 : debug_gimple_stmt (stmt);
5266 468745036 : err |= err2;
5267 : }
5268 : }
5269 : }
5270 :
5271 57158262 : return err;
5272 : }
5273 :
5274 : /* Verify the contents of a GIMPLE_TRANSACTION. Returns true if there
5275 : is a problem, otherwise false. */
5276 :
5277 : static bool
5278 34379 : verify_gimple_transaction (gtransaction *stmt)
5279 : {
5280 34379 : tree lab;
5281 :
5282 34379 : lab = gimple_transaction_label_norm (stmt);
5283 34379 : if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
5284 : return true;
5285 34379 : lab = gimple_transaction_label_uninst (stmt);
5286 34379 : if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
5287 : return true;
5288 34379 : lab = gimple_transaction_label_over (stmt);
5289 34379 : if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
5290 : return true;
5291 :
5292 34379 : return verify_gimple_in_seq_2 (gimple_transaction_body (stmt));
5293 : }
5294 :
5295 :
5296 : /* Verify the GIMPLE statements inside the statement list STMTS. */
5297 :
5298 : DEBUG_FUNCTION bool
5299 18228103 : verify_gimple_in_seq (gimple_seq stmts, bool ice)
5300 : {
5301 18228103 : timevar_push (TV_TREE_STMT_VERIFY);
5302 18228103 : bool res = verify_gimple_in_seq_2 (stmts);
5303 18228103 : if (res && ice)
5304 0 : internal_error ("%<verify_gimple%> failed");
5305 18228103 : timevar_pop (TV_TREE_STMT_VERIFY);
5306 18228103 : return res;
5307 : }
5308 :
5309 : /* Return true when the T can be shared. */
5310 :
5311 : static bool
5312 34177278629 : tree_node_can_be_shared (tree t)
5313 : {
5314 34177278629 : if (IS_TYPE_OR_DECL_P (t)
5315 : || TREE_CODE (t) == SSA_NAME
5316 : || TREE_CODE (t) == IDENTIFIER_NODE
5317 : || TREE_CODE (t) == CASE_LABEL_EXPR
5318 : || TREE_CODE (t) == OMP_NEXT_VARIANT
5319 46677563398 : || is_gimple_min_invariant (t))
5320 : return true;
5321 :
5322 4910984315 : if (t == error_mark_node)
5323 0 : return true;
5324 :
5325 : return false;
5326 : }
5327 :
5328 : /* Called via walk_tree. Verify tree sharing. */
5329 :
5330 : static tree
5331 34177278629 : verify_node_sharing_1 (tree *tp, int *walk_subtrees, void *data)
5332 : {
5333 34177278629 : hash_set<void *> *visited = (hash_set<void *> *) data;
5334 :
5335 34177278629 : if (tree_node_can_be_shared (*tp))
5336 : {
5337 29266294314 : *walk_subtrees = false;
5338 29266294314 : return NULL;
5339 : }
5340 :
5341 4910984315 : if (visited->add (*tp))
5342 0 : return *tp;
5343 :
5344 : return NULL;
5345 : }
5346 :
5347 : /* Called via walk_gimple_stmt. Verify tree sharing. */
5348 :
5349 : static tree
5350 32469445432 : verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
5351 : {
5352 32469445432 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5353 32469445432 : return verify_node_sharing_1 (tp, walk_subtrees, wi->info);
5354 : }
5355 :
5356 : static bool eh_error_found;
5357 : bool
5358 202053762 : verify_eh_throw_stmt_node (gimple *const &stmt, const int &,
5359 : hash_set<gimple *> *visited)
5360 : {
5361 202053762 : if (!visited->contains (stmt))
5362 : {
5363 0 : error ("dead statement in EH table");
5364 0 : debug_gimple_stmt (stmt);
5365 0 : eh_error_found = true;
5366 : }
5367 202053762 : return true;
5368 : }
5369 :
5370 : /* Verify if the location LOCs block is in BLOCKS. */
5371 :
5372 : static bool
5373 23886091565 : verify_location (hash_set<tree> *blocks, location_t loc)
5374 : {
5375 38715762063 : tree block = LOCATION_BLOCK (loc);
5376 38715762063 : if (block != NULL_TREE
5377 38715762063 : && !blocks->contains (block))
5378 : {
5379 0 : error ("location references block not in block tree");
5380 0 : return true;
5381 : }
5382 38715762063 : if (block != NULL_TREE)
5383 14829670498 : return verify_location (blocks, BLOCK_SOURCE_LOCATION (block));
5384 : return false;
5385 : }
5386 :
5387 : /* Called via walk_tree. Verify that expressions have no blocks. */
5388 :
5389 : static tree
5390 1680159327 : verify_expr_no_block (tree *tp, int *walk_subtrees, void *)
5391 : {
5392 1680159327 : if (!EXPR_P (*tp))
5393 : {
5394 1006398790 : *walk_subtrees = false;
5395 1006398790 : return NULL;
5396 : }
5397 :
5398 673760537 : location_t loc = EXPR_LOCATION (*tp);
5399 673760537 : if (LOCATION_BLOCK (loc) != NULL)
5400 0 : return *tp;
5401 :
5402 : return NULL;
5403 : }
5404 :
5405 : /* Called via walk_tree. Verify locations of expressions. */
5406 :
5407 : static tree
5408 38113631570 : verify_expr_location_1 (tree *tp, int *walk_subtrees, void *data)
5409 : {
5410 38113631570 : hash_set<tree> *blocks = (hash_set<tree> *) data;
5411 38113631570 : tree t = *tp;
5412 :
5413 : /* ??? This doesn't really belong here but there's no good place to
5414 : stick this remainder of old verify_expr. */
5415 : /* ??? This barfs on debug stmts which contain binds to vars with
5416 : different function context. */
5417 : #if 0
5418 : if (VAR_P (t)
5419 : || TREE_CODE (t) == PARM_DECL
5420 : || TREE_CODE (t) == RESULT_DECL)
5421 : {
5422 : tree context = decl_function_context (t);
5423 : if (context != cfun->decl
5424 : && !SCOPE_FILE_SCOPE_P (context)
5425 : && !TREE_STATIC (t)
5426 : && !DECL_EXTERNAL (t))
5427 : {
5428 : error ("local declaration from a different function");
5429 : return t;
5430 : }
5431 : }
5432 : #endif
5433 :
5434 38113631570 : if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
5435 : {
5436 440205292 : tree x = DECL_DEBUG_EXPR (t);
5437 440205292 : tree addr = walk_tree (&x, verify_expr_no_block, NULL, NULL);
5438 440205292 : if (addr)
5439 0 : return addr;
5440 : }
5441 38113631570 : if ((VAR_P (t)
5442 : || TREE_CODE (t) == PARM_DECL
5443 : || TREE_CODE (t) == RESULT_DECL)
5444 8526468705 : && DECL_HAS_VALUE_EXPR_P (t))
5445 : {
5446 2004 : tree x = DECL_VALUE_EXPR (t);
5447 2004 : tree addr = walk_tree (&x, verify_expr_no_block, NULL, NULL);
5448 2004 : if (addr)
5449 0 : return addr;
5450 : }
5451 :
5452 38113631570 : if (!EXPR_P (t))
5453 : {
5454 30065046846 : *walk_subtrees = false;
5455 30065046846 : return NULL;
5456 : }
5457 :
5458 8048584724 : location_t loc = EXPR_LOCATION (t);
5459 8048584724 : if (verify_location (blocks, loc))
5460 0 : return t;
5461 :
5462 : return NULL;
5463 : }
5464 :
5465 : /* Called via walk_gimple_op. Verify locations of expressions. */
5466 :
5467 : static tree
5468 36376543638 : verify_expr_location (tree *tp, int *walk_subtrees, void *data)
5469 : {
5470 36376543638 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5471 36376543638 : return verify_expr_location_1 (tp, walk_subtrees, wi->info);
5472 : }
5473 :
5474 : /* Insert all subblocks of BLOCK into BLOCKS and recurse. */
5475 :
5476 : static void
5477 2516425491 : collect_subblocks (hash_set<tree> *blocks, tree block)
5478 : {
5479 2516425491 : tree t;
5480 4778663979 : for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
5481 : {
5482 2262238488 : blocks->add (t);
5483 2262238488 : collect_subblocks (blocks, t);
5484 : }
5485 2516425491 : }
5486 :
5487 : /* Disable warnings about missing quoting in GCC diagnostics for
5488 : the verification errors. Their format strings don't follow
5489 : GCC diagnostic conventions and trigger an ICE in the end. */
5490 : #if __GNUC__ >= 10
5491 : # pragma GCC diagnostic push
5492 : # pragma GCC diagnostic ignored "-Wformat-diag"
5493 : #endif
5494 :
5495 : /* Verify the GIMPLE statements in the CFG of FN. */
5496 :
5497 : DEBUG_FUNCTION bool
5498 254187003 : verify_gimple_in_cfg (struct function *fn, bool verify_nothrow, bool ice)
5499 : {
5500 254187003 : basic_block bb;
5501 254187003 : bool err = false;
5502 :
5503 254187003 : timevar_push (TV_TREE_STMT_VERIFY);
5504 254187003 : hash_set<void *> visited;
5505 254187003 : hash_set<gimple *> visited_throwing_stmts;
5506 :
5507 : /* Collect all BLOCKs referenced by the BLOCK tree of FN. */
5508 254187003 : hash_set<tree> blocks;
5509 254187003 : if (DECL_INITIAL (fn->decl))
5510 : {
5511 254187003 : blocks.add (DECL_INITIAL (fn->decl));
5512 254187003 : collect_subblocks (&blocks, DECL_INITIAL (fn->decl));
5513 : }
5514 :
5515 2282060127 : FOR_EACH_BB_FN (bb, fn)
5516 : {
5517 2027873124 : gimple_stmt_iterator gsi;
5518 2027873124 : edge_iterator ei;
5519 2027873124 : edge e;
5520 :
5521 2027873124 : for (gphi_iterator gpi = gsi_start_phis (bb);
5522 2702076348 : !gsi_end_p (gpi);
5523 674203224 : gsi_next (&gpi))
5524 : {
5525 674203224 : gphi *phi = gpi.phi ();
5526 674203224 : bool err2 = false;
5527 674203224 : unsigned i;
5528 :
5529 674203224 : if (gimple_bb (phi) != bb)
5530 : {
5531 0 : error ("gimple_bb (phi) is set to a wrong basic block");
5532 0 : err2 = true;
5533 : }
5534 :
5535 674203224 : err2 |= verify_gimple_phi (phi);
5536 :
5537 : /* Only PHI arguments have locations. */
5538 674203224 : if (gimple_location (phi) != UNKNOWN_LOCATION)
5539 : {
5540 0 : error ("PHI node with location");
5541 0 : err2 = true;
5542 : }
5543 :
5544 2382036421 : for (i = 0; i < gimple_phi_num_args (phi); i++)
5545 : {
5546 1707833197 : tree arg = gimple_phi_arg_def (phi, i);
5547 1707833197 : tree addr = walk_tree (&arg, verify_node_sharing_1,
5548 : &visited, NULL);
5549 1707833197 : if (addr)
5550 : {
5551 0 : error ("incorrect sharing of tree nodes");
5552 0 : debug_generic_expr (addr);
5553 0 : err2 |= true;
5554 : }
5555 1707833197 : location_t loc = gimple_phi_arg_location (phi, i);
5556 1707833197 : if (virtual_operand_p (gimple_phi_result (phi))
5557 1707833197 : && loc != UNKNOWN_LOCATION)
5558 : {
5559 0 : error ("virtual PHI with argument locations");
5560 0 : err2 = true;
5561 : }
5562 1707833197 : addr = walk_tree (&arg, verify_expr_location_1, &blocks, NULL);
5563 1707833197 : if (addr)
5564 : {
5565 0 : debug_generic_expr (addr);
5566 0 : err2 = true;
5567 : }
5568 1707833197 : err2 |= verify_location (&blocks, loc);
5569 : }
5570 :
5571 674203224 : if (err2)
5572 0 : debug_gimple_stmt (phi);
5573 674203224 : err |= err2;
5574 : }
5575 :
5576 17642684481 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5577 : {
5578 13586938233 : gimple *stmt = gsi_stmt (gsi);
5579 13586938233 : bool err2 = false;
5580 13586938233 : struct walk_stmt_info wi;
5581 13586938233 : tree addr;
5582 13586938233 : int lp_nr;
5583 :
5584 13586938233 : if (gimple_bb (stmt) != bb)
5585 : {
5586 0 : error ("gimple_bb (stmt) is set to a wrong basic block");
5587 0 : err2 = true;
5588 : }
5589 :
5590 13586938233 : err2 |= verify_gimple_stmt (stmt);
5591 13586938233 : err2 |= verify_location (&blocks, gimple_location (stmt));
5592 :
5593 13586938233 : memset (&wi, 0, sizeof (wi));
5594 13586938233 : wi.info = (void *) &visited;
5595 13586938233 : addr = walk_gimple_op (stmt, verify_node_sharing, &wi);
5596 13586938233 : if (addr)
5597 : {
5598 0 : error ("incorrect sharing of tree nodes");
5599 0 : debug_generic_expr (addr);
5600 0 : err2 |= true;
5601 : }
5602 :
5603 13586938233 : memset (&wi, 0, sizeof (wi));
5604 13586938233 : wi.info = (void *) &blocks;
5605 13586938233 : addr = walk_gimple_op (stmt, verify_expr_location, &wi);
5606 13586938233 : if (addr)
5607 : {
5608 0 : debug_generic_expr (addr);
5609 0 : err2 |= true;
5610 : }
5611 :
5612 : /* If the statement is marked as part of an EH region, then it is
5613 : expected that the statement could throw. Verify that when we
5614 : have optimizations that simplify statements such that we prove
5615 : that they cannot throw, that we update other data structures
5616 : to match. */
5617 13586938233 : lp_nr = lookup_stmt_eh_lp (stmt);
5618 13586938233 : if (lp_nr != 0)
5619 202053762 : visited_throwing_stmts.add (stmt);
5620 202053762 : if (lp_nr > 0)
5621 : {
5622 194021610 : if (!stmt_could_throw_p (cfun, stmt))
5623 : {
5624 24 : if (verify_nothrow)
5625 : {
5626 0 : error ("statement marked for throw, but doesn%'t");
5627 0 : err2 |= true;
5628 : }
5629 : }
5630 194021586 : else if (!gsi_one_before_end_p (gsi))
5631 : {
5632 0 : error ("statement marked for throw in middle of block");
5633 0 : err2 |= true;
5634 : }
5635 : }
5636 :
5637 13586938233 : if (err2)
5638 0 : debug_gimple_stmt (stmt);
5639 13586938233 : err |= err2;
5640 : }
5641 :
5642 4831301760 : FOR_EACH_EDGE (e, ei, bb->succs)
5643 2803428636 : if (e->goto_locus != UNKNOWN_LOCATION)
5644 542735411 : err |= verify_location (&blocks, e->goto_locus);
5645 : }
5646 :
5647 254187003 : hash_map<gimple *, int> *eh_table = get_eh_throw_stmt_table (cfun);
5648 254187003 : eh_error_found = false;
5649 254187003 : if (eh_table)
5650 37881265 : eh_table->traverse<hash_set<gimple *> *, verify_eh_throw_stmt_node>
5651 239935027 : (&visited_throwing_stmts);
5652 :
5653 254187003 : if (ice && (err || eh_error_found))
5654 0 : internal_error ("verify_gimple failed");
5655 :
5656 254187003 : verify_histograms ();
5657 254187003 : timevar_pop (TV_TREE_STMT_VERIFY);
5658 :
5659 254187003 : return (err || eh_error_found);
5660 254187003 : }
5661 :
5662 :
5663 : /* Verifies that the flow information is OK. */
5664 :
5665 : static bool
5666 238935452 : gimple_verify_flow_info (void)
5667 : {
5668 238935452 : bool err = false;
5669 238935452 : basic_block bb;
5670 238935452 : gimple_stmt_iterator gsi;
5671 238935452 : gimple *stmt;
5672 238935452 : edge e;
5673 238935452 : edge_iterator ei;
5674 :
5675 238935452 : if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
5676 238935452 : || ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
5677 : {
5678 0 : error ("ENTRY_BLOCK has IL associated with it");
5679 0 : err = true;
5680 : }
5681 :
5682 238935452 : if (EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
5683 238935452 : || EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
5684 : {
5685 0 : error ("EXIT_BLOCK has IL associated with it");
5686 0 : err = true;
5687 : }
5688 :
5689 473790071 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5690 234854619 : if (e->flags & EDGE_FALLTHRU)
5691 : {
5692 0 : error ("fallthru to exit from bb %d", e->src->index);
5693 0 : err = true;
5694 : }
5695 238935452 : if (cfun->cfg->full_profile
5696 238935452 : && !ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ())
5697 : {
5698 0 : error ("entry block count not initialized");
5699 0 : err = true;
5700 : }
5701 238935452 : if (cfun->cfg->full_profile
5702 238935452 : && !EXIT_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ())
5703 : {
5704 0 : error ("exit block count not initialized");
5705 0 : err = true;
5706 : }
5707 238935452 : if (cfun->cfg->full_profile
5708 361200841 : && !single_succ_edge
5709 122265389 : (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability.initialized_p ())
5710 : {
5711 0 : error ("probability of edge from entry block not initialized");
5712 0 : err = true;
5713 : }
5714 238935452 : if (!EXIT_BLOCK_PTR_FOR_FN (cfun)
5715 238935452 : ->count.compatible_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count))
5716 : {
5717 0 : error ("exit block count is not compatible with entry block count");
5718 0 : err = true;
5719 : }
5720 :
5721 :
5722 2299794745 : FOR_EACH_BB_FN (bb, cfun)
5723 : {
5724 2060859293 : bool found_ctrl_stmt = false;
5725 :
5726 2060859293 : stmt = NULL;
5727 :
5728 2060859293 : if (cfun->cfg->full_profile)
5729 : {
5730 1357218697 : if (!bb->count.initialized_p ())
5731 : {
5732 0 : error ("count of bb %d not initialized", bb->index);
5733 0 : err = true;
5734 : }
5735 3271761157 : FOR_EACH_EDGE (e, ei, bb->succs)
5736 1914542460 : if (!e->probability.initialized_p ())
5737 : {
5738 0 : error ("probability of edge %d->%d not initialized",
5739 0 : bb->index, e->dest->index);
5740 0 : err = true;
5741 : }
5742 : }
5743 2060859293 : if (!bb->count.compatible_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count))
5744 : {
5745 0 : error ("count of bb %d is not compatible with entry block count",
5746 : bb->index);
5747 0 : err = true;
5748 : }
5749 :
5750 : /* Skip labels on the start of basic block. */
5751 4274335249 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5752 : {
5753 2050013617 : tree label;
5754 2050013617 : gimple *prev_stmt = stmt;
5755 :
5756 2050013617 : stmt = gsi_stmt (gsi);
5757 :
5758 2050013617 : if (gimple_code (stmt) != GIMPLE_LABEL)
5759 : break;
5760 :
5761 152616663 : label = gimple_label_label (as_a <glabel *> (stmt));
5762 152616663 : if (prev_stmt && DECL_NONLOCAL (label))
5763 : {
5764 0 : error ("nonlocal label %qD is not first in a sequence "
5765 : "of labels in bb %d", label, bb->index);
5766 0 : err = true;
5767 : }
5768 :
5769 155439254 : if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
5770 : {
5771 0 : error ("EH landing pad label %qD is not first in a sequence "
5772 : "of labels in bb %d", label, bb->index);
5773 0 : err = true;
5774 : }
5775 :
5776 152616663 : if (label_to_block (cfun, label) != bb)
5777 : {
5778 0 : error ("label %qD to block does not match in bb %d",
5779 : label, bb->index);
5780 0 : err = true;
5781 : }
5782 :
5783 152616663 : if (decl_function_context (label) != current_function_decl)
5784 : {
5785 0 : error ("label %qD has incorrect context in bb %d",
5786 : label, bb->index);
5787 0 : err = true;
5788 : }
5789 : }
5790 :
5791 : /* Verify that body of basic block BB is free of control flow. */
5792 2060859293 : bool seen_nondebug_stmt = false;
5793 16778370951 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
5794 : {
5795 14717511658 : gimple *stmt = gsi_stmt (gsi);
5796 :
5797 : /* Do NOT disregard debug stmts after found_ctrl_stmt. */
5798 14717511658 : if (found_ctrl_stmt)
5799 : {
5800 0 : error ("control flow in the middle of basic block %d",
5801 : bb->index);
5802 0 : err = true;
5803 : }
5804 :
5805 14717511658 : if (stmt_ends_bb_p (stmt))
5806 1390757916 : found_ctrl_stmt = true;
5807 :
5808 14717511658 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
5809 : {
5810 0 : error ("label %qD in the middle of basic block %d",
5811 : gimple_label_label (label_stmt), bb->index);
5812 0 : err = true;
5813 : }
5814 :
5815 : /* Check that no statements appear between a returns_twice call
5816 : and its associated abnormal edge. */
5817 14717511658 : if (gimple_code (stmt) == GIMPLE_CALL
5818 14717511658 : && gimple_call_flags (stmt) & ECF_RETURNS_TWICE)
5819 : {
5820 155880 : bool misplaced = false;
5821 : /* TM is an exception: it points abnormal edges just after the
5822 : call that starts a transaction, i.e. it must end the BB. */
5823 155880 : if (gimple_call_builtin_p (stmt, BUILT_IN_TM_START))
5824 : {
5825 3779 : if (single_succ_p (bb)
5826 2989 : && bb_has_abnormal_pred (single_succ (bb))
5827 6121 : && !gsi_one_nondebug_before_end_p (gsi))
5828 : {
5829 0 : error ("returns_twice call is not last in basic block "
5830 : "%d", bb->index);
5831 0 : misplaced = true;
5832 : }
5833 : }
5834 : else
5835 : {
5836 152101 : if (seen_nondebug_stmt && bb_has_abnormal_pred (bb))
5837 : {
5838 0 : error ("returns_twice call is not first in basic block "
5839 : "%d", bb->index);
5840 0 : misplaced = true;
5841 : }
5842 : }
5843 0 : if (misplaced)
5844 : {
5845 0 : print_gimple_stmt (stderr, stmt, 0, TDF_SLIM);
5846 0 : err = true;
5847 : }
5848 : }
5849 14717511658 : if (!is_gimple_debug (stmt))
5850 6805362843 : seen_nondebug_stmt = true;
5851 : }
5852 :
5853 2060859293 : gsi = gsi_last_nondebug_bb (bb);
5854 2060859293 : if (gsi_end_p (gsi))
5855 132381711 : continue;
5856 :
5857 1928477582 : stmt = gsi_stmt (gsi);
5858 :
5859 1928477582 : if (gimple_code (stmt) == GIMPLE_LABEL)
5860 43947227 : continue;
5861 :
5862 1884530355 : if (verify_eh_edges (stmt))
5863 0 : err = true;
5864 :
5865 1884530355 : if (is_ctrl_stmt (stmt))
5866 : {
5867 2937653417 : FOR_EACH_EDGE (e, ei, bb->succs)
5868 1863197034 : if (e->flags & EDGE_FALLTHRU)
5869 : {
5870 0 : error ("fallthru edge after a control statement in bb %d",
5871 : bb->index);
5872 0 : err = true;
5873 : }
5874 : }
5875 :
5876 1884530355 : if (gimple_code (stmt) != GIMPLE_COND)
5877 : {
5878 : /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
5879 : after anything else but if statement. */
5880 2228080986 : FOR_EACH_EDGE (e, ei, bb->succs)
5881 1129416692 : if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
5882 : {
5883 0 : error ("true/false edge after a non-GIMPLE_COND in bb %d",
5884 : bb->index);
5885 0 : err = true;
5886 : }
5887 : }
5888 :
5889 1884530355 : switch (gimple_code (stmt))
5890 : {
5891 785866061 : case GIMPLE_COND:
5892 785866061 : {
5893 785866061 : edge true_edge;
5894 785866061 : edge false_edge;
5895 :
5896 785866061 : extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
5897 :
5898 785866061 : if (!true_edge
5899 785866061 : || !false_edge
5900 785866061 : || !(true_edge->flags & EDGE_TRUE_VALUE)
5901 785866061 : || !(false_edge->flags & EDGE_FALSE_VALUE)
5902 785866061 : || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
5903 785866061 : || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
5904 1571732122 : || EDGE_COUNT (bb->succs) >= 3)
5905 : {
5906 0 : error ("wrong outgoing edge flags at end of bb %d",
5907 : bb->index);
5908 0 : err = true;
5909 : }
5910 : }
5911 785866061 : break;
5912 :
5913 55543 : case GIMPLE_GOTO:
5914 55543 : if (simple_goto_p (stmt))
5915 : {
5916 0 : error ("explicit goto at end of bb %d", bb->index);
5917 0 : err = true;
5918 : }
5919 : else
5920 : {
5921 : /* FIXME. We should double check that the labels in the
5922 : destination blocks have their address taken. */
5923 212909 : FOR_EACH_EDGE (e, ei, bb->succs)
5924 157366 : if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
5925 : | EDGE_FALSE_VALUE))
5926 157366 : || !(e->flags & EDGE_ABNORMAL))
5927 : {
5928 0 : error ("wrong outgoing edge flags at end of bb %d",
5929 : bb->index);
5930 0 : err = true;
5931 : }
5932 : }
5933 : break;
5934 :
5935 400657709 : case GIMPLE_CALL:
5936 400657709 : if (!gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
5937 : break;
5938 : /* fallthru */
5939 234850169 : case GIMPLE_RETURN:
5940 234850169 : if (!single_succ_p (bb)
5941 234850169 : || (single_succ_edge (bb)->flags
5942 234850169 : & (EDGE_FALLTHRU | EDGE_ABNORMAL
5943 : | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
5944 : {
5945 0 : error ("wrong outgoing edge flags at end of bb %d", bb->index);
5946 0 : err = true;
5947 : }
5948 234850169 : if (single_succ (bb) != EXIT_BLOCK_PTR_FOR_FN (cfun))
5949 : {
5950 0 : error ("return edge does not point to exit in bb %d",
5951 : bb->index);
5952 0 : err = true;
5953 : }
5954 : break;
5955 :
5956 4641127 : case GIMPLE_SWITCH:
5957 4641127 : {
5958 4641127 : gswitch *switch_stmt = as_a <gswitch *> (stmt);
5959 4641127 : tree prev;
5960 4641127 : edge e;
5961 4641127 : size_t i, n;
5962 :
5963 4641127 : n = gimple_switch_num_labels (switch_stmt);
5964 :
5965 : /* Mark all the destination basic blocks. */
5966 36411256 : for (i = 0; i < n; ++i)
5967 : {
5968 31770129 : basic_block label_bb = gimple_switch_label_bb (cfun, switch_stmt, i);
5969 31770129 : gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
5970 31770129 : label_bb->aux = (void *)1;
5971 : }
5972 :
5973 : /* Verify that the case labels are sorted. */
5974 4641127 : prev = gimple_switch_label (switch_stmt, 0);
5975 31770129 : for (i = 1; i < n; ++i)
5976 : {
5977 27129002 : tree c = gimple_switch_label (switch_stmt, i);
5978 27129002 : if (!CASE_LOW (c))
5979 : {
5980 0 : error ("found default case not at the start of "
5981 : "case vector");
5982 0 : err = true;
5983 0 : continue;
5984 : }
5985 27129002 : if (CASE_LOW (prev)
5986 27129002 : && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
5987 : {
5988 0 : error ("case labels not sorted: ");
5989 0 : print_generic_expr (stderr, prev);
5990 0 : fprintf (stderr," is greater than ");
5991 0 : print_generic_expr (stderr, c);
5992 0 : fprintf (stderr," but comes before it.\n");
5993 0 : err = true;
5994 : }
5995 : prev = c;
5996 : }
5997 : /* VRP will remove the default case if it can prove it will
5998 : never be executed. So do not verify there always exists
5999 : a default case here. */
6000 :
6001 32598420 : FOR_EACH_EDGE (e, ei, bb->succs)
6002 : {
6003 27957293 : if (!e->dest->aux)
6004 : {
6005 0 : error ("extra outgoing edge %d->%d",
6006 : bb->index, e->dest->index);
6007 0 : err = true;
6008 : }
6009 :
6010 27957293 : e->dest->aux = (void *)2;
6011 27957293 : if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
6012 : | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
6013 : {
6014 0 : error ("wrong outgoing edge flags at end of bb %d",
6015 : bb->index);
6016 0 : err = true;
6017 : }
6018 : }
6019 :
6020 : /* Check that we have all of them. */
6021 36411256 : for (i = 0; i < n; ++i)
6022 : {
6023 31770129 : basic_block label_bb = gimple_switch_label_bb (cfun,
6024 : switch_stmt, i);
6025 :
6026 31770129 : if (label_bb->aux != (void *)2)
6027 : {
6028 0 : error ("missing edge %i->%i", bb->index, label_bb->index);
6029 0 : err = true;
6030 : }
6031 : }
6032 :
6033 32598420 : FOR_EACH_EDGE (e, ei, bb->succs)
6034 27957293 : e->dest->aux = (void *)0;
6035 : }
6036 4641127 : break;
6037 :
6038 2017447 : case GIMPLE_EH_DISPATCH:
6039 2017447 : if (verify_eh_dispatch_edge (as_a <geh_dispatch *> (stmt)))
6040 0 : err = true;
6041 : break;
6042 :
6043 : default:
6044 : break;
6045 : }
6046 : }
6047 :
6048 238935452 : if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
6049 194817943 : verify_dominators (CDI_DOMINATORS);
6050 :
6051 238935452 : return err;
6052 : }
6053 :
6054 : #if __GNUC__ >= 10
6055 : # pragma GCC diagnostic pop
6056 : #endif
6057 :
6058 : /* Updates phi nodes after creating a forwarder block joined
6059 : by edge FALLTHRU. */
6060 :
6061 : static void
6062 98394 : gimple_make_forwarder_block (edge fallthru)
6063 : {
6064 98394 : edge e;
6065 98394 : edge_iterator ei;
6066 98394 : basic_block dummy, bb;
6067 98394 : tree var;
6068 98394 : gphi_iterator gsi;
6069 98394 : bool forward_location_p;
6070 :
6071 98394 : dummy = fallthru->src;
6072 98394 : bb = fallthru->dest;
6073 :
6074 98394 : if (single_pred_p (bb))
6075 148 : return;
6076 :
6077 : /* We can forward location info if we have only one predecessor. */
6078 98246 : forward_location_p = single_pred_p (dummy);
6079 :
6080 : /* If we redirected a branch we must create new PHI nodes at the
6081 : start of BB. */
6082 337560 : for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
6083 : {
6084 239314 : gphi *phi, *new_phi;
6085 :
6086 239314 : phi = gsi.phi ();
6087 239314 : var = gimple_phi_result (phi);
6088 239314 : new_phi = create_phi_node (var, bb);
6089 239314 : gimple_phi_set_result (phi, copy_ssa_name (var, phi));
6090 239318 : add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
6091 : forward_location_p
6092 4 : ? gimple_phi_arg_location (phi, 0) : UNKNOWN_LOCATION);
6093 : }
6094 :
6095 : /* Add the arguments we have stored on edges. */
6096 298051 : FOR_EACH_EDGE (e, ei, bb->preds)
6097 : {
6098 199805 : if (e == fallthru)
6099 98246 : continue;
6100 :
6101 101559 : flush_pending_stmts (e);
6102 : }
6103 : }
6104 :
6105 :
6106 : /* Return a non-special label in the head of basic block BLOCK.
6107 : Create one if it doesn't exist. */
6108 :
6109 : tree
6110 5969654 : gimple_block_label (basic_block bb)
6111 : {
6112 5969654 : gimple_stmt_iterator i, s = gsi_start_bb (bb);
6113 5969654 : bool first = true;
6114 5969654 : tree label;
6115 5969654 : glabel *stmt;
6116 :
6117 5969654 : for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
6118 : {
6119 4862805 : stmt = dyn_cast <glabel *> (gsi_stmt (i));
6120 4763378 : if (!stmt)
6121 : break;
6122 4763378 : label = gimple_label_label (stmt);
6123 4763378 : if (!DECL_NONLOCAL (label))
6124 : {
6125 4763378 : if (!first)
6126 0 : gsi_move_before (&i, &s);
6127 : return label;
6128 : }
6129 : }
6130 :
6131 1206276 : label = create_artificial_label (UNKNOWN_LOCATION);
6132 1206276 : stmt = gimple_build_label (label);
6133 1206276 : gsi_insert_before (&s, stmt, GSI_NEW_STMT);
6134 1206276 : return label;
6135 : }
6136 :
6137 :
6138 : /* Attempt to perform edge redirection by replacing a possibly complex
6139 : jump instruction by a goto or by removing the jump completely.
6140 : This can apply only if all edges now point to the same block. The
6141 : parameters and return values are equivalent to
6142 : redirect_edge_and_branch. */
6143 :
6144 : static edge
6145 66468300 : gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
6146 : {
6147 66468300 : basic_block src = e->src;
6148 66468300 : gimple_stmt_iterator i;
6149 66468300 : gimple *stmt;
6150 :
6151 : /* We can replace or remove a complex jump only when we have exactly
6152 : two edges. */
6153 66468300 : if (EDGE_COUNT (src->succs) != 2
6154 : /* Verify that all targets will be TARGET. Specifically, the
6155 : edge that is not E must also go to TARGET. */
6156 66468300 : || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
6157 : return NULL;
6158 :
6159 433136 : i = gsi_last_bb (src);
6160 433136 : if (gsi_end_p (i))
6161 : return NULL;
6162 :
6163 433136 : stmt = gsi_stmt (i);
6164 :
6165 433136 : if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
6166 : {
6167 433106 : gsi_remove (&i, true);
6168 433106 : e = ssa_redirect_edge (e, target);
6169 433106 : e->flags = EDGE_FALLTHRU;
6170 433106 : return e;
6171 : }
6172 :
6173 : return NULL;
6174 : }
6175 :
6176 :
6177 : /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
6178 : edge representing the redirected branch. */
6179 :
6180 : static edge
6181 67760962 : gimple_redirect_edge_and_branch (edge e, basic_block dest)
6182 : {
6183 67760962 : basic_block bb = e->src;
6184 67760962 : gimple_stmt_iterator gsi;
6185 67760962 : edge ret;
6186 67760962 : gimple *stmt;
6187 :
6188 67760962 : if (e->flags & EDGE_ABNORMAL)
6189 : return NULL;
6190 :
6191 67760962 : if (e->dest == dest)
6192 : return NULL;
6193 :
6194 67734420 : if (e->flags & EDGE_EH)
6195 1007251 : return redirect_eh_edge (e, dest);
6196 :
6197 66727169 : if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
6198 : {
6199 66468300 : ret = gimple_try_redirect_by_replacing_jump (e, dest);
6200 66468300 : if (ret)
6201 : return ret;
6202 : }
6203 :
6204 66294063 : gsi = gsi_last_nondebug_bb (bb);
6205 66294063 : stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
6206 :
6207 66294063 : switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
6208 : {
6209 : case GIMPLE_COND:
6210 : /* For COND_EXPR, we only need to redirect the edge. */
6211 : break;
6212 :
6213 0 : case GIMPLE_GOTO:
6214 : /* No non-abnormal edges should lead from a non-simple goto, and
6215 : simple ones should be represented implicitly. */
6216 0 : gcc_unreachable ();
6217 :
6218 410921 : case GIMPLE_SWITCH:
6219 410921 : {
6220 410921 : gswitch *switch_stmt = as_a <gswitch *> (stmt);
6221 410921 : tree label = gimple_block_label (dest);
6222 410921 : tree cases = get_cases_for_edge (e, switch_stmt);
6223 :
6224 : /* If we have a list of cases associated with E, then use it
6225 : as it's a lot faster than walking the entire case vector. */
6226 410921 : if (cases)
6227 : {
6228 348457 : edge e2 = find_edge (e->src, dest);
6229 348457 : tree last, first;
6230 :
6231 348457 : first = cases;
6232 1111829 : while (cases)
6233 : {
6234 414915 : last = cases;
6235 414915 : CASE_LABEL (cases) = label;
6236 414915 : cases = CASE_CHAIN (cases);
6237 : }
6238 :
6239 : /* If there was already an edge in the CFG, then we need
6240 : to move all the cases associated with E to E2. */
6241 348457 : if (e2)
6242 : {
6243 9063 : tree cases2 = get_cases_for_edge (e2, switch_stmt);
6244 :
6245 9063 : CASE_CHAIN (last) = CASE_CHAIN (cases2);
6246 9063 : CASE_CHAIN (cases2) = first;
6247 : }
6248 348457 : bitmap_set_bit (touched_switch_bbs, gimple_bb (stmt)->index);
6249 : }
6250 : else
6251 : {
6252 62464 : size_t i, n = gimple_switch_num_labels (switch_stmt);
6253 :
6254 4785585 : for (i = 0; i < n; i++)
6255 : {
6256 4723121 : tree elt = gimple_switch_label (switch_stmt, i);
6257 4723121 : if (label_to_block (cfun, CASE_LABEL (elt)) == e->dest)
6258 70502 : CASE_LABEL (elt) = label;
6259 : }
6260 : }
6261 : }
6262 : break;
6263 :
6264 10834 : case GIMPLE_ASM:
6265 10834 : {
6266 10834 : gasm *asm_stmt = as_a <gasm *> (stmt);
6267 10834 : int i, n = gimple_asm_nlabels (asm_stmt);
6268 10834 : tree label = NULL;
6269 :
6270 15619 : for (i = 0; i < n; ++i)
6271 : {
6272 4785 : tree cons = gimple_asm_label_op (asm_stmt, i);
6273 4785 : if (label_to_block (cfun, TREE_VALUE (cons)) == e->dest)
6274 : {
6275 2372 : if (!label)
6276 2372 : label = gimple_block_label (dest);
6277 2372 : TREE_VALUE (cons) = label;
6278 : }
6279 : }
6280 :
6281 : /* If we didn't find any label matching the former edge in the
6282 : asm labels, we must be redirecting the fallthrough
6283 : edge. */
6284 10834 : gcc_assert (label || (e->flags & EDGE_FALLTHRU));
6285 : }
6286 : break;
6287 :
6288 255 : case GIMPLE_RETURN:
6289 255 : gsi_remove (&gsi, true);
6290 255 : e->flags |= EDGE_FALLTHRU;
6291 255 : break;
6292 :
6293 : case GIMPLE_OMP_RETURN:
6294 : case GIMPLE_OMP_CONTINUE:
6295 : case GIMPLE_OMP_SECTIONS_SWITCH:
6296 : case GIMPLE_OMP_FOR:
6297 : /* The edges from OMP constructs can be simply redirected. */
6298 : break;
6299 :
6300 0 : case GIMPLE_EH_DISPATCH:
6301 0 : if (!(e->flags & EDGE_FALLTHRU))
6302 0 : redirect_eh_dispatch_edge (as_a <geh_dispatch *> (stmt), e, dest);
6303 : break;
6304 :
6305 346 : case GIMPLE_TRANSACTION:
6306 346 : if (e->flags & EDGE_TM_ABORT)
6307 81 : gimple_transaction_set_label_over (as_a <gtransaction *> (stmt),
6308 : gimple_block_label (dest));
6309 265 : else if (e->flags & EDGE_TM_UNINSTRUMENTED)
6310 128 : gimple_transaction_set_label_uninst (as_a <gtransaction *> (stmt),
6311 : gimple_block_label (dest));
6312 : else
6313 137 : gimple_transaction_set_label_norm (as_a <gtransaction *> (stmt),
6314 : gimple_block_label (dest));
6315 : break;
6316 :
6317 7849860 : default:
6318 : /* Otherwise it must be a fallthru edge, and we don't need to
6319 : do anything besides redirecting it. */
6320 7849860 : gcc_assert (e->flags & EDGE_FALLTHRU);
6321 : break;
6322 : }
6323 :
6324 : /* Update/insert PHI nodes as necessary. */
6325 :
6326 : /* Now update the edges in the CFG. */
6327 66294063 : e = ssa_redirect_edge (e, dest);
6328 :
6329 66294063 : return e;
6330 : }
6331 :
6332 : /* Returns true if it is possible to remove edge E by redirecting
6333 : it to the destination of the other edge from E->src. */
6334 :
6335 : static bool
6336 315734 : gimple_can_remove_branch_p (const_edge e)
6337 : {
6338 315734 : if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
6339 0 : return false;
6340 :
6341 : return true;
6342 : }
6343 :
6344 : /* Simple wrapper, as we can always redirect fallthru edges. */
6345 :
6346 : static basic_block
6347 4078683 : gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
6348 : {
6349 4078683 : e = gimple_redirect_edge_and_branch (e, dest);
6350 4078683 : gcc_assert (e);
6351 :
6352 4078683 : return NULL;
6353 : }
6354 :
6355 :
6356 : /* Splits basic block BB after statement STMT (but at least after the
6357 : labels). If STMT is NULL, BB is split just after the labels. */
6358 :
6359 : static basic_block
6360 5915686 : gimple_split_block (basic_block bb, void *stmt)
6361 : {
6362 5915686 : gimple_stmt_iterator gsi;
6363 5915686 : gimple_stmt_iterator gsi_tgt;
6364 5915686 : gimple_seq list;
6365 5915686 : basic_block new_bb;
6366 5915686 : edge e;
6367 5915686 : edge_iterator ei;
6368 :
6369 5915686 : new_bb = create_empty_bb (bb);
6370 5915686 : if (coverage_suppressed_p (bb))
6371 161 : suppress_coverage (new_bb);
6372 :
6373 : /* Redirect the outgoing edges. */
6374 5915686 : new_bb->succs = bb->succs;
6375 5915686 : bb->succs = NULL;
6376 13668271 : FOR_EACH_EDGE (e, ei, new_bb->succs)
6377 7752585 : e->src = new_bb;
6378 :
6379 : /* Get a stmt iterator pointing to the first stmt to move. */
6380 5915686 : if (!stmt || gimple_code ((gimple *) stmt) == GIMPLE_LABEL)
6381 1462681 : gsi = gsi_after_labels (bb);
6382 : else
6383 : {
6384 4453005 : gsi = gsi_for_stmt ((gimple *) stmt);
6385 4453005 : gsi_next (&gsi);
6386 : }
6387 :
6388 : /* Move everything from GSI to the new basic block. */
6389 5915686 : if (gsi_end_p (gsi))
6390 : return new_bb;
6391 :
6392 : /* Split the statement list - avoid re-creating new containers as this
6393 : brings ugly quadratic memory consumption in the inliner.
6394 : (We are still quadratic since we need to update stmt BB pointers,
6395 : sadly.) */
6396 5483234 : gsi_split_seq_before (&gsi, &list);
6397 5483234 : set_bb_seq (new_bb, list);
6398 5483234 : for (gsi_tgt = gsi_start (list);
6399 31298667 : !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
6400 25815433 : gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
6401 :
6402 : return new_bb;
6403 : }
6404 :
6405 :
6406 : /* Moves basic block BB after block AFTER. */
6407 :
6408 : static bool
6409 24183054 : gimple_move_block_after (basic_block bb, basic_block after)
6410 : {
6411 24183054 : if (bb->prev_bb == after)
6412 : return true;
6413 :
6414 5834427 : unlink_block (bb);
6415 5834427 : link_block (bb, after);
6416 :
6417 5834427 : return true;
6418 : }
6419 :
6420 :
6421 : /* Return TRUE if block BB has no executable statements, otherwise return
6422 : FALSE. */
6423 :
6424 : static bool
6425 15567312 : gimple_empty_block_p (basic_block bb)
6426 : {
6427 : /* BB must have no executable statements. */
6428 15567312 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
6429 15567312 : if (phi_nodes (bb))
6430 : return false;
6431 21754031 : while (!gsi_end_p (gsi))
6432 : {
6433 11567481 : gimple *stmt = gsi_stmt (gsi);
6434 11567481 : if (is_gimple_debug (stmt))
6435 : ;
6436 4217548 : else if (gimple_code (stmt) == GIMPLE_NOP
6437 4217548 : || gimple_code (stmt) == GIMPLE_PREDICT)
6438 : ;
6439 : else
6440 : return false;
6441 7389783 : gsi_next (&gsi);
6442 : }
6443 : return true;
6444 : }
6445 :
6446 :
6447 : /* Split a basic block if it ends with a conditional branch and if the
6448 : other part of the block is not empty. */
6449 :
6450 : static basic_block
6451 557 : gimple_split_block_before_cond_jump (basic_block bb)
6452 : {
6453 557 : gimple *last, *split_point;
6454 557 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
6455 557 : if (gsi_end_p (gsi))
6456 : return NULL;
6457 557 : last = gsi_stmt (gsi);
6458 557 : if (gimple_code (last) != GIMPLE_COND
6459 557 : && gimple_code (last) != GIMPLE_SWITCH)
6460 : return NULL;
6461 557 : gsi_prev (&gsi);
6462 557 : split_point = gsi_stmt (gsi);
6463 557 : return split_block (bb, split_point)->dest;
6464 : }
6465 :
6466 :
6467 : /* Return true if basic_block can be duplicated. */
6468 :
6469 : static bool
6470 16373675 : gimple_can_duplicate_bb_p (const_basic_block bb)
6471 : {
6472 16373675 : gimple *last = last_nondebug_stmt (const_cast<basic_block> (bb));
6473 :
6474 : /* Do checks that can only fail for the last stmt, to minimize the work in the
6475 : stmt loop. */
6476 16373675 : if (last) {
6477 : /* A transaction is a single entry multiple exit region. It
6478 : must be duplicated in its entirety or not at all. */
6479 14493811 : if (gimple_code (last) == GIMPLE_TRANSACTION)
6480 : return false;
6481 :
6482 : /* An IFN_UNIQUE call must be duplicated as part of its group,
6483 : or not at all. */
6484 14493809 : if (is_gimple_call (last)
6485 76960 : && gimple_call_internal_p (last)
6486 14494308 : && gimple_call_internal_unique_p (last))
6487 : return false;
6488 :
6489 : /* Prohibit duplication of returns_twice calls, otherwise associated
6490 : abnormal edges also need to be duplicated properly.
6491 : return_twice functions will always be the last statement. */
6492 14493809 : if (is_gimple_call (last)
6493 14493809 : && (gimple_call_flags (last) & ECF_RETURNS_TWICE))
6494 : return false;
6495 : }
6496 :
6497 32746922 : for (gimple_stmt_iterator gsi = gsi_start_bb (const_cast<basic_block> (bb));
6498 159715445 : !gsi_end_p (gsi); gsi_next (&gsi))
6499 : {
6500 143341984 : gimple *g = gsi_stmt (gsi);
6501 :
6502 : /* An IFN_GOMP_SIMT_ENTER_ALLOC/IFN_GOMP_SIMT_EXIT call must be
6503 : duplicated as part of its group, or not at all.
6504 : The IFN_GOMP_SIMT_VOTE_ANY and IFN_GOMP_SIMT_XCHG_* are part of such a
6505 : group, so the same holds there. */
6506 143341984 : if (is_gimple_call (g)
6507 143341984 : && (gimple_call_internal_p (g, IFN_GOMP_SIMT_ENTER_ALLOC)
6508 2226329 : || gimple_call_internal_p (g, IFN_GOMP_SIMT_EXIT)
6509 2226329 : || gimple_call_internal_p (g, IFN_GOMP_SIMT_VOTE_ANY)
6510 2226329 : || gimple_call_internal_p (g, IFN_GOMP_SIMT_XCHG_BFLY)
6511 2226329 : || gimple_call_internal_p (g, IFN_GOMP_SIMT_XCHG_IDX)))
6512 16373675 : return false;
6513 : }
6514 :
6515 : return true;
6516 : }
6517 :
6518 : /* Create a duplicate of the basic block BB. NOTE: This does not
6519 : preserve SSA form. */
6520 :
6521 : static basic_block
6522 4092515 : gimple_duplicate_bb (basic_block bb, copy_bb_data *id)
6523 : {
6524 4092515 : basic_block new_bb;
6525 4092515 : gimple_stmt_iterator gsi_tgt;
6526 :
6527 4092515 : new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
6528 4092515 : if (coverage_suppressed_p (bb))
6529 0 : suppress_coverage (new_bb);
6530 :
6531 : /* Copy the PHI nodes. We ignore PHI node arguments here because
6532 : the incoming edges have not been setup yet. */
6533 4092515 : for (gphi_iterator gpi = gsi_start_phis (bb);
6534 9655043 : !gsi_end_p (gpi);
6535 5562528 : gsi_next (&gpi))
6536 : {
6537 5562528 : gphi *phi, *copy;
6538 5562528 : phi = gpi.phi ();
6539 5562528 : copy = create_phi_node (NULL_TREE, new_bb);
6540 5562528 : create_new_def_for (gimple_phi_result (phi), copy,
6541 : gimple_phi_result_ptr (copy));
6542 5562528 : gimple_set_uid (copy, gimple_uid (phi));
6543 : }
6544 :
6545 4092515 : gsi_tgt = gsi_start_bb (new_bb);
6546 8185030 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
6547 28793949 : !gsi_end_p (gsi);
6548 24701434 : gsi_next (&gsi))
6549 : {
6550 24701434 : def_operand_p def_p;
6551 24701434 : ssa_op_iter op_iter;
6552 24701434 : tree lhs;
6553 24701434 : gimple *stmt, *copy;
6554 :
6555 24701434 : stmt = gsi_stmt (gsi);
6556 24701434 : if (gimple_code (stmt) == GIMPLE_LABEL)
6557 94436 : continue;
6558 :
6559 : /* Don't duplicate label debug stmts. */
6560 24609160 : if (gimple_debug_bind_p (stmt)
6561 15503069 : && TREE_CODE (gimple_debug_bind_get_var (stmt))
6562 : == LABEL_DECL)
6563 2162 : continue;
6564 :
6565 : /* Create a new copy of STMT and duplicate STMT's virtual
6566 : operands. */
6567 24606998 : copy = gimple_copy (stmt);
6568 24606998 : gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
6569 :
6570 24606998 : maybe_duplicate_eh_stmt (copy, stmt);
6571 24606998 : gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
6572 :
6573 : /* When copying around a stmt writing into a local non-user
6574 : aggregate, make sure it won't share stack slot with other
6575 : vars. */
6576 24606998 : lhs = gimple_get_lhs (stmt);
6577 24606998 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
6578 : {
6579 885067 : tree base = get_base_address (lhs);
6580 885067 : if (base
6581 885067 : && (VAR_P (base) || TREE_CODE (base) == RESULT_DECL)
6582 569737 : && DECL_IGNORED_P (base)
6583 214852 : && !TREE_STATIC (base)
6584 213579 : && !DECL_EXTERNAL (base)
6585 1098644 : && (!VAR_P (base) || !DECL_HAS_VALUE_EXPR_P (base)))
6586 213577 : DECL_NONSHAREABLE (base) = 1;
6587 : }
6588 :
6589 : /* If requested remap dependence info of cliques brought in
6590 : via inlining. */
6591 24606998 : if (id)
6592 66238330 : for (unsigned i = 0; i < gimple_num_ops (copy); ++i)
6593 : {
6594 45770940 : tree op = gimple_op (copy, i);
6595 45770940 : if (!op)
6596 11802139 : continue;
6597 33968801 : if (TREE_CODE (op) == ADDR_EXPR
6598 32645156 : || TREE_CODE (op) == WITH_SIZE_EXPR)
6599 1323645 : op = TREE_OPERAND (op, 0);
6600 36109489 : while (handled_component_p (op))
6601 2140688 : op = TREE_OPERAND (op, 0);
6602 33968801 : if ((TREE_CODE (op) == MEM_REF
6603 33968801 : || TREE_CODE (op) == TARGET_MEM_REF)
6604 1180029 : && MR_DEPENDENCE_CLIQUE (op) > 1
6605 34051389 : && MR_DEPENDENCE_CLIQUE (op) != bb->loop_father->owned_clique)
6606 : {
6607 28297 : if (!id->dependence_map)
6608 14370 : id->dependence_map = new hash_map<dependence_hash,
6609 : unsigned short>;
6610 28297 : bool existed;
6611 28297 : unsigned short &newc = id->dependence_map->get_or_insert
6612 28297 : (MR_DEPENDENCE_CLIQUE (op), &existed);
6613 28297 : if (!existed)
6614 : {
6615 18971 : gcc_assert (MR_DEPENDENCE_CLIQUE (op) <= cfun->last_clique);
6616 37942 : newc = get_new_clique (cfun);
6617 : }
6618 28297 : MR_DEPENDENCE_CLIQUE (op) = newc;
6619 : }
6620 : }
6621 :
6622 : /* Create new names for all the definitions created by COPY and
6623 : add replacement mappings for each new name. */
6624 30451539 : FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
6625 5844541 : create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
6626 : }
6627 :
6628 4092515 : return new_bb;
6629 : }
6630 :
6631 : /* Adds phi node arguments for edge E_COPY after basic block duplication. */
6632 :
6633 : static void
6634 5532309 : add_phi_args_after_copy_edge (edge e_copy)
6635 : {
6636 5532309 : basic_block bb, bb_copy = e_copy->src, dest;
6637 5532309 : edge e;
6638 5532309 : edge_iterator ei;
6639 5532309 : gphi *phi, *phi_copy;
6640 5532309 : tree def;
6641 5532309 : gphi_iterator psi, psi_copy;
6642 :
6643 5532309 : if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
6644 3525402 : return;
6645 :
6646 2006907 : bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
6647 :
6648 2006907 : if (e_copy->dest->flags & BB_DUPLICATED)
6649 823315 : dest = get_bb_original (e_copy->dest);
6650 : else
6651 : dest = e_copy->dest;
6652 :
6653 2006907 : e = find_edge (bb, dest);
6654 2006907 : if (!e)
6655 : {
6656 : /* During loop unrolling the target of the latch edge is copied.
6657 : In this case we are not looking for edge to dest, but to
6658 : duplicated block whose original was dest. */
6659 1253 : FOR_EACH_EDGE (e, ei, bb->succs)
6660 : {
6661 1253 : if ((e->dest->flags & BB_DUPLICATED)
6662 1253 : && get_bb_original (e->dest) == dest)
6663 : break;
6664 : }
6665 :
6666 1253 : gcc_assert (e != NULL);
6667 : }
6668 :
6669 2006907 : for (psi = gsi_start_phis (e->dest),
6670 2006907 : psi_copy = gsi_start_phis (e_copy->dest);
6671 5934363 : !gsi_end_p (psi);
6672 3927456 : gsi_next (&psi), gsi_next (&psi_copy))
6673 : {
6674 3927456 : phi = psi.phi ();
6675 3927456 : phi_copy = psi_copy.phi ();
6676 3927456 : def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6677 3927456 : add_phi_arg (phi_copy, def, e_copy,
6678 : gimple_phi_arg_location_from_edge (phi, e));
6679 : }
6680 : }
6681 :
6682 :
6683 : /* Basic block BB_COPY was created by code duplication. Add phi node
6684 : arguments for edges going out of BB_COPY. The blocks that were
6685 : duplicated have BB_DUPLICATED set. */
6686 :
6687 : void
6688 3816918 : add_phi_args_after_copy_bb (basic_block bb_copy)
6689 : {
6690 3816918 : edge e_copy;
6691 3816918 : edge_iterator ei;
6692 :
6693 9349205 : FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
6694 : {
6695 5532287 : add_phi_args_after_copy_edge (e_copy);
6696 : }
6697 3816918 : }
6698 :
6699 : /* Blocks in REGION_COPY array of length N_REGION were created by
6700 : duplication of basic blocks. Add phi node arguments for edges
6701 : going from these blocks. If E_COPY is not NULL, also add
6702 : phi node arguments for its destination.*/
6703 :
6704 : void
6705 2001957 : add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
6706 : edge e_copy)
6707 : {
6708 2001957 : unsigned i;
6709 :
6710 4571180 : for (i = 0; i < n_region; i++)
6711 2569223 : region_copy[i]->flags |= BB_DUPLICATED;
6712 :
6713 4571180 : for (i = 0; i < n_region; i++)
6714 2569223 : add_phi_args_after_copy_bb (region_copy[i]);
6715 2001957 : if (e_copy)
6716 22 : add_phi_args_after_copy_edge (e_copy);
6717 :
6718 4571180 : for (i = 0; i < n_region; i++)
6719 2569223 : region_copy[i]->flags &= ~BB_DUPLICATED;
6720 2001957 : }
6721 :
6722 : /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
6723 : important exit edge EXIT. By important we mean that no SSA name defined
6724 : inside region is live over the other exit edges of the region. All entry
6725 : edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
6726 : to the duplicate of the region. Dominance and loop information is
6727 : updated if UPDATE_DOMINANCE is true, but not the SSA web. If
6728 : UPDATE_DOMINANCE is false then we assume that the caller will update the
6729 : dominance information after calling this function. The new basic
6730 : blocks are stored to REGION_COPY in the same order as they had in REGION,
6731 : provided that REGION_COPY is not NULL.
6732 : The function returns false if it is unable to copy the region,
6733 : true otherwise.
6734 :
6735 : It is callers responsibility to update profile. */
6736 :
6737 : bool
6738 589542 : gimple_duplicate_seme_region (edge entry, edge exit,
6739 : basic_block *region, unsigned n_region,
6740 : basic_block *region_copy,
6741 : bool update_dominance)
6742 : {
6743 589542 : unsigned i;
6744 589542 : bool free_region_copy = false, copying_header = false;
6745 589542 : class loop *loop = entry->dest->loop_father;
6746 589542 : edge exit_copy;
6747 589542 : edge redirected;
6748 :
6749 589542 : if (!can_copy_bbs_p (region, n_region))
6750 : return false;
6751 :
6752 : /* Some sanity checking. Note that we do not check for all possible
6753 : missuses of the functions. I.e. if you ask to copy something weird,
6754 : it will work, but the state of structures probably will not be
6755 : correct. */
6756 1188801 : for (i = 0; i < n_region; i++)
6757 : {
6758 : /* We do not handle subloops, i.e. all the blocks must belong to the
6759 : same loop. */
6760 599259 : if (region[i]->loop_father != loop)
6761 : return false;
6762 :
6763 599259 : if (region[i] != entry->dest
6764 9717 : && region[i] == loop->header)
6765 : return false;
6766 : }
6767 :
6768 : /* In case the function is used for loop header copying (which is the primary
6769 : use), ensure that EXIT and its copy will be new latch and entry edges. */
6770 589542 : if (loop->header == entry->dest)
6771 : {
6772 589542 : copying_header = true;
6773 :
6774 589542 : if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
6775 : return false;
6776 :
6777 1188801 : for (i = 0; i < n_region; i++)
6778 599259 : if (region[i] != exit->src
6779 599259 : && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
6780 : return false;
6781 : }
6782 :
6783 589542 : initialize_original_copy_tables ();
6784 :
6785 589542 : if (copying_header)
6786 589542 : set_loop_copy (loop, loop_outer (loop));
6787 : else
6788 0 : set_loop_copy (loop, loop);
6789 :
6790 589542 : if (!region_copy)
6791 : {
6792 0 : region_copy = XNEWVEC (basic_block, n_region);
6793 0 : free_region_copy = true;
6794 : }
6795 :
6796 : /* Record blocks outside the region that are dominated by something
6797 : inside. */
6798 589542 : auto_vec<basic_block> doms;
6799 589542 : if (update_dominance)
6800 589542 : doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
6801 :
6802 589542 : copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
6803 : split_edge_bb_loc (entry), update_dominance);
6804 :
6805 589542 : if (copying_header)
6806 : {
6807 589542 : loop->header = exit->dest;
6808 589542 : loop->latch = exit->src;
6809 : }
6810 :
6811 : /* Redirect the entry and add the phi node arguments. */
6812 589542 : redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
6813 589542 : gcc_assert (redirected != NULL);
6814 589542 : flush_pending_stmts (entry);
6815 :
6816 : /* Concerning updating of dominators: We must recount dominators
6817 : for entry block and its copy. Anything that is outside of the
6818 : region, but was dominated by something inside needs recounting as
6819 : well. */
6820 589542 : if (update_dominance)
6821 : {
6822 589542 : set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
6823 589542 : doms.safe_push (get_bb_original (entry->dest));
6824 589542 : iterate_fix_dominators (CDI_DOMINATORS, doms, false);
6825 : }
6826 :
6827 : /* Add the other PHI node arguments. */
6828 589542 : add_phi_args_after_copy (region_copy, n_region, NULL);
6829 :
6830 589542 : if (free_region_copy)
6831 0 : free (region_copy);
6832 :
6833 589542 : free_original_copy_tables ();
6834 589542 : return true;
6835 589542 : }
6836 :
6837 : /* Checks if BB is part of the region defined by N_REGION BBS. */
6838 : static bool
6839 0 : bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
6840 : {
6841 0 : unsigned int n;
6842 :
6843 0 : for (n = 0; n < n_region; n++)
6844 : {
6845 0 : if (bb == bbs[n])
6846 : return true;
6847 : }
6848 : return false;
6849 : }
6850 :
6851 :
6852 : /* For each PHI in BB, copy the argument associated with SRC_E to TGT_E.
6853 : Assuming the argument exists, just does not have a value.
6854 : If USE_MAP is true, then use the redirect edge var map of TGT_E
6855 : for the new arguments; clearing the map afterwards. */
6856 :
6857 : void
6858 32461089 : copy_phi_arg_into_existing_phi (edge src_e, edge tgt_e, bool use_map)
6859 : {
6860 32461089 : int src_idx = src_e->dest_idx;
6861 32461089 : int tgt_idx = tgt_e->dest_idx;
6862 :
6863 : /* Iterate over each PHI in e->dest. */
6864 32461089 : for (gphi_iterator gsi = gsi_start_phis (src_e->dest),
6865 32461089 : gsi2 = gsi_start_phis (tgt_e->dest);
6866 83025488 : !gsi_end_p (gsi);
6867 50564399 : gsi_next (&gsi), gsi_next (&gsi2))
6868 : {
6869 50564399 : gphi *src_phi = gsi.phi ();
6870 50564399 : gphi *dest_phi = gsi2.phi ();
6871 50564399 : tree val = gimple_phi_arg_def (src_phi, src_idx);
6872 50564399 : location_t locus = gimple_phi_arg_location (src_phi, src_idx);
6873 :
6874 50564399 : if (use_map && TREE_CODE (val) == SSA_NAME)
6875 : {
6876 : /* If DEF is one of the results of PHI nodes removed during
6877 : redirection, replace it with the PHI argument that used
6878 : to be on E. */
6879 4843800 : vec<edge_var_map> *head = redirect_edge_var_map_vector (tgt_e);
6880 9687600 : size_t length = head ? head->length () : 0;
6881 8379874 : for (size_t i = 0; i < length; i++)
6882 : {
6883 6996805 : edge_var_map *vm = &(*head)[i];
6884 6996805 : tree old_arg = redirect_edge_var_map_result (vm);
6885 6996805 : tree new_arg = redirect_edge_var_map_def (vm);
6886 :
6887 6996805 : if (val == old_arg)
6888 : {
6889 3460731 : val = new_arg;
6890 3460731 : location_t locus1 = redirect_edge_var_map_location (vm);
6891 : /* Don't remove the location if we remap one does not have one. */
6892 3460731 : if (locus1 != UNKNOWN_LOCATION)
6893 534956 : locus = locus1;
6894 : break;
6895 : }
6896 : }
6897 : }
6898 :
6899 50564399 : SET_PHI_ARG_DEF (dest_phi, tgt_idx, val);
6900 50564399 : gimple_phi_arg_set_location (dest_phi, tgt_idx, locus);
6901 : }
6902 32461089 : if (use_map)
6903 2884280 : redirect_edge_var_map_clear (tgt_e);
6904 32461089 : }
6905 :
6906 : /* Duplicates REGION consisting of N_REGION blocks. The new blocks
6907 : are stored to REGION_COPY in the same order in that they appear
6908 : in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
6909 : the region, EXIT an exit from it. The condition guarding EXIT
6910 : is moved to ENTRY. Returns true if duplication succeeds, false
6911 : otherwise.
6912 :
6913 : For example,
6914 :
6915 : some_code;
6916 : if (cond)
6917 : A;
6918 : else
6919 : B;
6920 :
6921 : is transformed to
6922 :
6923 : if (cond)
6924 : {
6925 : some_code;
6926 : A;
6927 : }
6928 : else
6929 : {
6930 : some_code;
6931 : B;
6932 : }
6933 : */
6934 :
6935 : bool
6936 22 : gimple_duplicate_sese_tail (edge entry, edge exit,
6937 : basic_block *region, unsigned n_region,
6938 : basic_block *region_copy)
6939 : {
6940 22 : unsigned i;
6941 22 : bool free_region_copy = false;
6942 22 : class loop *loop = exit->dest->loop_father;
6943 22 : class loop *orig_loop = entry->dest->loop_father;
6944 22 : basic_block switch_bb, entry_bb, nentry_bb;
6945 22 : profile_count total_count = profile_count::uninitialized (),
6946 22 : exit_count = profile_count::uninitialized ();
6947 22 : edge exits[2], nexits[2], e;
6948 22 : gimple_stmt_iterator gsi;
6949 22 : edge sorig, snew;
6950 22 : basic_block exit_bb;
6951 22 : class loop *target, *aloop, *cloop;
6952 :
6953 22 : gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
6954 22 : exits[0] = exit;
6955 22 : exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
6956 :
6957 22 : if (!can_copy_bbs_p (region, n_region))
6958 : return false;
6959 :
6960 22 : initialize_original_copy_tables ();
6961 22 : set_loop_copy (orig_loop, loop);
6962 :
6963 22 : target= loop;
6964 22 : for (aloop = orig_loop->inner; aloop; aloop = aloop->next)
6965 : {
6966 0 : if (bb_part_of_region_p (aloop->header, region, n_region))
6967 : {
6968 0 : cloop = duplicate_loop (aloop, target);
6969 0 : duplicate_subloops (aloop, cloop);
6970 : }
6971 : }
6972 :
6973 22 : if (!region_copy)
6974 : {
6975 0 : region_copy = XNEWVEC (basic_block, n_region);
6976 0 : free_region_copy = true;
6977 : }
6978 :
6979 22 : gcc_assert (!need_ssa_update_p (cfun));
6980 :
6981 : /* Record blocks outside the region that are dominated by something
6982 : inside. */
6983 22 : auto_vec<basic_block> doms = get_dominated_by_region (CDI_DOMINATORS, region,
6984 22 : n_region);
6985 :
6986 22 : total_count = exit->src->count;
6987 22 : exit_count = exit->count ();
6988 : /* Fix up corner cases, to avoid division by zero or creation of negative
6989 : frequencies. */
6990 22 : if (exit_count > total_count)
6991 0 : exit_count = total_count;
6992 :
6993 22 : copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
6994 : split_edge_bb_loc (exit), true);
6995 22 : if (total_count.initialized_p () && exit_count.initialized_p ())
6996 : {
6997 22 : scale_bbs_frequencies_profile_count (region, n_region,
6998 : total_count - exit_count,
6999 : total_count);
7000 22 : scale_bbs_frequencies_profile_count (region_copy, n_region, exit_count,
7001 : total_count);
7002 : }
7003 :
7004 : /* Create the switch block, and put the exit condition to it. */
7005 22 : entry_bb = entry->dest;
7006 22 : nentry_bb = get_bb_copy (entry_bb);
7007 22 : if (!*gsi_last_bb (entry->src)
7008 22 : || !stmt_ends_bb_p (*gsi_last_bb (entry->src)))
7009 22 : switch_bb = entry->src;
7010 : else
7011 0 : switch_bb = split_edge (entry);
7012 22 : set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
7013 :
7014 44 : gcond *cond_stmt = as_a <gcond *> (*gsi_last_bb (exit->src));
7015 22 : cond_stmt = as_a <gcond *> (gimple_copy (cond_stmt));
7016 :
7017 22 : gsi = gsi_last_bb (switch_bb);
7018 22 : gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
7019 :
7020 22 : sorig = single_succ_edge (switch_bb);
7021 22 : sorig->flags = exits[1]->flags;
7022 22 : sorig->probability = exits[1]->probability;
7023 22 : snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
7024 22 : snew->probability = exits[0]->probability;
7025 :
7026 :
7027 : /* Register the new edge from SWITCH_BB in loop exit lists. */
7028 22 : rescan_loop_exit (snew, true, false);
7029 :
7030 : /* Add the PHI node arguments. */
7031 22 : add_phi_args_after_copy (region_copy, n_region, snew);
7032 :
7033 : /* Get rid of now superfluous conditions and associated edges (and phi node
7034 : arguments). */
7035 22 : exit_bb = exit->dest;
7036 :
7037 22 : e = redirect_edge_and_branch (exits[0], exits[1]->dest);
7038 22 : PENDING_STMT (e) = NULL;
7039 :
7040 : /* The latch of ORIG_LOOP was copied, and so was the backedge
7041 : to the original header. We redirect this backedge to EXIT_BB. */
7042 46 : for (i = 0; i < n_region; i++)
7043 24 : if (get_bb_original (region_copy[i]) == orig_loop->latch)
7044 : {
7045 0 : gcc_assert (single_succ_edge (region_copy[i]));
7046 0 : e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
7047 0 : PENDING_STMT (e) = NULL;
7048 0 : copy_phi_arg_into_existing_phi (nexits[0], e);
7049 : }
7050 22 : e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
7051 22 : PENDING_STMT (e) = NULL;
7052 :
7053 : /* Anything that is outside of the region, but was dominated by something
7054 : inside needs to update dominance info. */
7055 22 : iterate_fix_dominators (CDI_DOMINATORS, doms, false);
7056 :
7057 22 : if (free_region_copy)
7058 0 : free (region_copy);
7059 :
7060 22 : free_original_copy_tables ();
7061 22 : return true;
7062 22 : }
7063 :
7064 : /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
7065 : adding blocks when the dominator traversal reaches EXIT. This
7066 : function silently assumes that ENTRY strictly dominates EXIT. */
7067 :
7068 : void
7069 632374 : gather_blocks_in_sese_region (basic_block entry, basic_block exit,
7070 : vec<basic_block> *bbs_p)
7071 : {
7072 632374 : basic_block son;
7073 :
7074 632374 : for (son = first_dom_son (CDI_DOMINATORS, entry);
7075 1264627 : son;
7076 632253 : son = next_dom_son (CDI_DOMINATORS, son))
7077 : {
7078 632253 : bbs_p->safe_push (son);
7079 632253 : if (son != exit)
7080 586144 : gather_blocks_in_sese_region (son, exit, bbs_p);
7081 : }
7082 632374 : }
7083 :
7084 : /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
7085 : The duplicates are recorded in VARS_MAP. */
7086 :
7087 : static void
7088 4736406 : replace_by_duplicate_decl (tree *tp, hash_map<tree, tree> *vars_map,
7089 : tree to_context)
7090 : {
7091 4736406 : tree t = *tp, new_t;
7092 4736406 : struct function *f = DECL_STRUCT_FUNCTION (to_context);
7093 :
7094 4736406 : if (DECL_CONTEXT (t) == to_context)
7095 37201 : return;
7096 :
7097 4699205 : bool existed;
7098 4699205 : tree &loc = vars_map->get_or_insert (t, &existed);
7099 :
7100 4699205 : if (!existed)
7101 : {
7102 1396230 : if (SSA_VAR_P (t))
7103 : {
7104 1385635 : new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
7105 1385635 : add_local_decl (f, new_t);
7106 : }
7107 : else
7108 : {
7109 10595 : gcc_assert (TREE_CODE (t) == CONST_DECL);
7110 10595 : new_t = copy_node (t);
7111 : }
7112 1396230 : DECL_CONTEXT (new_t) = to_context;
7113 :
7114 1396230 : loc = new_t;
7115 : }
7116 : else
7117 3302975 : new_t = loc;
7118 :
7119 4699205 : *tp = new_t;
7120 : }
7121 :
7122 :
7123 : /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
7124 : VARS_MAP maps old ssa names and var_decls to the new ones. */
7125 :
7126 : static tree
7127 14556 : replace_ssa_name (tree name, hash_map<tree, tree> *vars_map,
7128 : tree to_context)
7129 : {
7130 14556 : tree new_name;
7131 :
7132 29112 : gcc_assert (!virtual_operand_p (name));
7133 :
7134 14556 : tree *loc = vars_map->get (name);
7135 :
7136 14556 : if (!loc)
7137 : {
7138 6033 : tree decl = SSA_NAME_VAR (name);
7139 6033 : if (decl)
7140 : {
7141 1186 : gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (name));
7142 1186 : replace_by_duplicate_decl (&decl, vars_map, to_context);
7143 1186 : new_name = make_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
7144 1186 : decl, SSA_NAME_DEF_STMT (name));
7145 : }
7146 : else
7147 4847 : new_name = copy_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
7148 4847 : name, SSA_NAME_DEF_STMT (name));
7149 :
7150 : /* Now that we've used the def stmt to define new_name, make sure it
7151 : doesn't define name anymore. */
7152 6033 : SSA_NAME_DEF_STMT (name) = NULL;
7153 :
7154 6033 : vars_map->put (name, new_name);
7155 : }
7156 : else
7157 8523 : new_name = *loc;
7158 :
7159 14556 : return new_name;
7160 : }
7161 :
7162 : struct move_stmt_d
7163 : {
7164 : tree orig_block;
7165 : tree new_block;
7166 : tree from_context;
7167 : tree to_context;
7168 : hash_map<tree, tree> *vars_map;
7169 : htab_t new_label_map;
7170 : hash_map<void *, void *> *eh_map;
7171 : bool remap_decls_p;
7172 : };
7173 :
7174 : /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
7175 : contained in *TP if it has been ORIG_BLOCK previously and change the
7176 : DECL_CONTEXT of every local variable referenced in *TP. */
7177 :
7178 : static tree
7179 7732832 : move_stmt_op (tree *tp, int *walk_subtrees, void *data)
7180 : {
7181 7732832 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
7182 7732832 : struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
7183 7732832 : tree t = *tp;
7184 :
7185 7732832 : if (EXPR_P (t))
7186 : {
7187 1217677 : tree block = TREE_BLOCK (t);
7188 1217677 : if (block == NULL_TREE)
7189 : ;
7190 31281 : else if (block == p->orig_block
7191 29278 : || p->orig_block == NULL_TREE)
7192 : {
7193 : /* tree_node_can_be_shared says we can share invariant
7194 : addresses but unshare_expr copies them anyways. Make sure
7195 : to unshare before adjusting the block in place - we do not
7196 : always see a copy here. */
7197 2026 : if (TREE_CODE (t) == ADDR_EXPR
7198 2026 : && is_gimple_min_invariant (t))
7199 2003 : *tp = t = unshare_expr (t);
7200 2026 : TREE_SET_BLOCK (t, p->new_block);
7201 : }
7202 29255 : else if (flag_checking)
7203 : {
7204 106120 : while (block && TREE_CODE (block) == BLOCK && block != p->orig_block)
7205 76865 : block = BLOCK_SUPERCONTEXT (block);
7206 29255 : gcc_assert (block == p->orig_block);
7207 : }
7208 : }
7209 6515155 : else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
7210 : {
7211 4770988 : if (TREE_CODE (t) == SSA_NAME)
7212 13022 : *tp = replace_ssa_name (t, p->vars_map, p->to_context);
7213 4757966 : else if (TREE_CODE (t) == PARM_DECL
7214 4757966 : && gimple_in_ssa_p (cfun))
7215 181 : *tp = *(p->vars_map->get (t));
7216 4757785 : else if (TREE_CODE (t) == LABEL_DECL)
7217 : {
7218 2820 : if (p->new_label_map)
7219 : {
7220 479 : struct tree_map in, *out;
7221 479 : in.base.from = t;
7222 479 : out = (struct tree_map *)
7223 479 : htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
7224 479 : if (out)
7225 13 : *tp = t = out->to;
7226 : }
7227 :
7228 : /* For FORCED_LABELs we can end up with references from other
7229 : functions if some SESE regions are outlined. It is UB to
7230 : jump in between them, but they could be used just for printing
7231 : addresses etc. In that case, DECL_CONTEXT on the label should
7232 : be the function containing the glabel stmt with that LABEL_DECL,
7233 : rather than whatever function a reference to the label was seen
7234 : last time. */
7235 2820 : if (!FORCED_LABEL (t) && !DECL_NONLOCAL (t))
7236 2813 : DECL_CONTEXT (t) = p->to_context;
7237 : }
7238 4754965 : else if (p->remap_decls_p)
7239 : {
7240 : /* Replace T with its duplicate. T should no longer appear in the
7241 : parent function, so this looks wasteful; however, it may appear
7242 : in referenced_vars, and more importantly, as virtual operands of
7243 : statements, and in alias lists of other variables. It would be
7244 : quite difficult to expunge it from all those places. ??? It might
7245 : suffice to do this for addressable variables. */
7246 4013845 : if ((VAR_P (t) && !is_global_var (t))
7247 4797080 : || TREE_CODE (t) == CONST_DECL)
7248 3982119 : replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
7249 : }
7250 4770988 : *walk_subtrees = 0;
7251 4770988 : }
7252 1744167 : else if (TYPE_P (t))
7253 0 : *walk_subtrees = 0;
7254 :
7255 7732832 : return NULL_TREE;
7256 : }
7257 :
7258 : /* Helper for move_stmt_r. Given an EH region number for the source
7259 : function, map that to the duplicate EH region number in the dest. */
7260 :
7261 : static int
7262 18 : move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
7263 : {
7264 18 : eh_region old_r, new_r;
7265 :
7266 18 : old_r = get_eh_region_from_number (old_nr);
7267 18 : new_r = static_cast<eh_region> (*p->eh_map->get (old_r));
7268 :
7269 18 : return new_r->index;
7270 : }
7271 :
7272 : /* Similar, but operate on INTEGER_CSTs. */
7273 :
7274 : static tree
7275 5 : move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
7276 : {
7277 5 : int old_nr, new_nr;
7278 :
7279 5 : old_nr = tree_to_shwi (old_t_nr);
7280 5 : new_nr = move_stmt_eh_region_nr (old_nr, p);
7281 :
7282 5 : return build_int_cst (integer_type_node, new_nr);
7283 : }
7284 :
7285 : /* Like move_stmt_op, but for gimple statements.
7286 :
7287 : Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
7288 : contained in the current statement in *GSI_P and change the
7289 : DECL_CONTEXT of every local variable referenced in the current
7290 : statement. */
7291 :
7292 : static tree
7293 2184529 : move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
7294 : struct walk_stmt_info *wi)
7295 : {
7296 2184529 : struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
7297 2184529 : gimple *stmt = gsi_stmt (*gsi_p);
7298 2184529 : tree block = gimple_block (stmt);
7299 :
7300 2184529 : if (block == p->orig_block
7301 1945257 : || (p->orig_block == NULL_TREE
7302 955 : && block != NULL_TREE))
7303 240227 : gimple_set_block (stmt, p->new_block);
7304 :
7305 2184529 : switch (gimple_code (stmt))
7306 : {
7307 282737 : case GIMPLE_CALL:
7308 : /* Remap the region numbers for __builtin_eh_{pointer,filter}. */
7309 282737 : {
7310 282737 : tree r, fndecl = gimple_call_fndecl (stmt);
7311 282737 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
7312 85260 : switch (DECL_FUNCTION_CODE (fndecl))
7313 : {
7314 0 : case BUILT_IN_EH_COPY_VALUES:
7315 0 : r = gimple_call_arg (stmt, 1);
7316 0 : r = move_stmt_eh_region_tree_nr (r, p);
7317 0 : gimple_call_set_arg (stmt, 1, r);
7318 : /* FALLTHRU */
7319 :
7320 5 : case BUILT_IN_EH_POINTER:
7321 5 : case BUILT_IN_EH_FILTER:
7322 5 : r = gimple_call_arg (stmt, 0);
7323 5 : r = move_stmt_eh_region_tree_nr (r, p);
7324 5 : gimple_call_set_arg (stmt, 0, r);
7325 5 : break;
7326 :
7327 : default:
7328 : break;
7329 : }
7330 : }
7331 : break;
7332 :
7333 8 : case GIMPLE_RESX:
7334 8 : {
7335 8 : gresx *resx_stmt = as_a <gresx *> (stmt);
7336 8 : int r = gimple_resx_region (resx_stmt);
7337 8 : r = move_stmt_eh_region_nr (r, p);
7338 8 : gimple_resx_set_region (resx_stmt, r);
7339 : }
7340 8 : break;
7341 :
7342 5 : case GIMPLE_EH_DISPATCH:
7343 5 : {
7344 5 : geh_dispatch *eh_dispatch_stmt = as_a <geh_dispatch *> (stmt);
7345 5 : int r = gimple_eh_dispatch_region (eh_dispatch_stmt);
7346 5 : r = move_stmt_eh_region_nr (r, p);
7347 5 : gimple_eh_dispatch_set_region (eh_dispatch_stmt, r);
7348 : }
7349 5 : break;
7350 :
7351 : case GIMPLE_OMP_RETURN:
7352 : case GIMPLE_OMP_CONTINUE:
7353 : break;
7354 :
7355 1418 : case GIMPLE_LABEL:
7356 1418 : {
7357 : /* For FORCED_LABEL, move_stmt_op doesn't adjust DECL_CONTEXT,
7358 : so that such labels can be referenced from other regions.
7359 : Make sure to update it when seeing a GIMPLE_LABEL though,
7360 : that is the owner of the label. */
7361 1418 : walk_gimple_op (stmt, move_stmt_op, wi);
7362 1418 : *handled_ops_p = true;
7363 1418 : tree label = gimple_label_label (as_a <glabel *> (stmt));
7364 1418 : if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
7365 5 : DECL_CONTEXT (label) = p->to_context;
7366 : }
7367 : break;
7368 :
7369 1900361 : default:
7370 1900361 : if (is_gimple_omp (stmt))
7371 : {
7372 : /* Do not remap variables inside OMP directives. Variables
7373 : referenced in clauses and directive header belong to the
7374 : parent function and should not be moved into the child
7375 : function. */
7376 0 : bool save_remap_decls_p = p->remap_decls_p;
7377 0 : p->remap_decls_p = false;
7378 0 : *handled_ops_p = true;
7379 :
7380 0 : walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), move_stmt_r,
7381 : move_stmt_op, wi);
7382 :
7383 0 : p->remap_decls_p = save_remap_decls_p;
7384 : }
7385 : break;
7386 : }
7387 :
7388 2184529 : return NULL_TREE;
7389 : }
7390 :
7391 : /* Move basic block BB from function CFUN to function DEST_FN. The
7392 : block is moved out of the original linked list and placed after
7393 : block AFTER in the new list. Also, the block is removed from the
7394 : original array of blocks and placed in DEST_FN's array of blocks.
7395 : If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
7396 : updated to reflect the moved edges.
7397 :
7398 : The local variables are remapped to new instances, VARS_MAP is used
7399 : to record the mapping. */
7400 :
7401 : static void
7402 676321 : move_block_to_fn (struct function *dest_cfun, basic_block bb,
7403 : basic_block after, bool update_edge_count_p,
7404 : struct move_stmt_d *d)
7405 : {
7406 676321 : struct control_flow_graph *cfg;
7407 676321 : edge_iterator ei;
7408 676321 : edge e;
7409 676321 : gimple_stmt_iterator si;
7410 676321 : unsigned old_len;
7411 :
7412 : /* Remove BB from dominance structures. */
7413 676321 : delete_from_dominance_info (CDI_DOMINATORS, bb);
7414 :
7415 : /* Move BB from its current loop to the copy in the new function. */
7416 676321 : if (current_loops)
7417 : {
7418 676321 : class loop *new_loop = (class loop *)bb->loop_father->aux;
7419 676321 : if (new_loop)
7420 381219 : bb->loop_father = new_loop;
7421 : }
7422 :
7423 : /* Link BB to the new linked list. */
7424 676321 : move_block_after (bb, after);
7425 :
7426 : /* Update the edge count in the corresponding flowgraphs. */
7427 676321 : if (update_edge_count_p)
7428 1448707 : FOR_EACH_EDGE (e, ei, bb->succs)
7429 : {
7430 818109 : cfun->cfg->x_n_edges--;
7431 818109 : dest_cfun->cfg->x_n_edges++;
7432 : }
7433 :
7434 : /* Remove BB from the original basic block array. */
7435 676321 : (*cfun->cfg->x_basic_block_info)[bb->index] = NULL;
7436 676321 : cfun->cfg->x_n_basic_blocks--;
7437 :
7438 : /* Grow DEST_CFUN's basic block array if needed. */
7439 676321 : cfg = dest_cfun->cfg;
7440 676321 : cfg->x_n_basic_blocks++;
7441 676321 : if (bb->index >= cfg->x_last_basic_block)
7442 45844 : cfg->x_last_basic_block = bb->index + 1;
7443 :
7444 676321 : old_len = vec_safe_length (cfg->x_basic_block_info);
7445 676321 : if ((unsigned) cfg->x_last_basic_block >= old_len)
7446 37149 : vec_safe_grow_cleared (cfg->x_basic_block_info,
7447 37149 : cfg->x_last_basic_block + 1);
7448 :
7449 676321 : (*cfg->x_basic_block_info)[bb->index] = bb;
7450 :
7451 : /* Remap the variables in phi nodes. */
7452 676321 : for (gphi_iterator psi = gsi_start_phis (bb);
7453 677487 : !gsi_end_p (psi); )
7454 : {
7455 1166 : gphi *phi = psi.phi ();
7456 1166 : use_operand_p use;
7457 1166 : tree op = PHI_RESULT (phi);
7458 1166 : ssa_op_iter oi;
7459 1166 : unsigned i;
7460 :
7461 2332 : if (virtual_operand_p (op))
7462 : {
7463 : /* Remove the phi nodes for virtual operands (alias analysis will be
7464 : run for the new function, anyway). But replace all uses that
7465 : might be outside of the region we move. */
7466 526 : use_operand_p use_p;
7467 526 : imm_use_iterator iter;
7468 526 : gimple *use_stmt;
7469 1045 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, op)
7470 1038 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
7471 519 : SET_USE (use_p, SSA_NAME_VAR (op));
7472 526 : remove_phi_node (&psi, true);
7473 526 : continue;
7474 526 : }
7475 :
7476 640 : SET_PHI_RESULT (phi,
7477 : replace_ssa_name (op, d->vars_map, dest_cfun->decl));
7478 1684 : FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
7479 : {
7480 1044 : op = USE_FROM_PTR (use);
7481 1044 : if (TREE_CODE (op) == SSA_NAME)
7482 894 : SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
7483 : }
7484 :
7485 1684 : for (i = 0; i < EDGE_COUNT (bb->preds); i++)
7486 : {
7487 1044 : location_t locus = gimple_phi_arg_location (phi, i);
7488 1044 : tree block = LOCATION_BLOCK (locus);
7489 :
7490 1044 : if (locus == UNKNOWN_LOCATION)
7491 884 : continue;
7492 160 : if (d->orig_block == NULL_TREE || block == d->orig_block)
7493 : {
7494 160 : locus = set_block (locus, d->new_block);
7495 160 : gimple_phi_arg_set_location (phi, i, locus);
7496 : }
7497 : }
7498 :
7499 640 : gsi_next (&psi);
7500 : }
7501 :
7502 3537171 : for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
7503 : {
7504 2184529 : gimple *stmt = gsi_stmt (si);
7505 2184529 : struct walk_stmt_info wi;
7506 :
7507 2184529 : memset (&wi, 0, sizeof (wi));
7508 2184529 : wi.info = d;
7509 2184529 : walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
7510 :
7511 2184529 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
7512 : {
7513 1418 : tree label = gimple_label_label (label_stmt);
7514 1418 : int uid = LABEL_DECL_UID (label);
7515 :
7516 1418 : gcc_assert (uid > -1);
7517 :
7518 1418 : old_len = vec_safe_length (cfg->x_label_to_block_map);
7519 1418 : if (old_len <= (unsigned) uid)
7520 171 : vec_safe_grow_cleared (cfg->x_label_to_block_map, uid + 1);
7521 :
7522 1418 : (*cfg->x_label_to_block_map)[uid] = bb;
7523 1418 : (*cfun->cfg->x_label_to_block_map)[uid] = NULL;
7524 :
7525 1418 : gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
7526 :
7527 1418 : if (uid >= dest_cfun->cfg->last_label_uid)
7528 320 : dest_cfun->cfg->last_label_uid = uid + 1;
7529 : }
7530 :
7531 2184529 : maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
7532 2184529 : remove_stmt_from_eh_lp_fn (cfun, stmt);
7533 :
7534 2184529 : gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
7535 2184529 : gimple_remove_stmt_histograms (cfun, stmt);
7536 :
7537 : /* We cannot leave any operands allocated from the operand caches of
7538 : the current function. */
7539 2184529 : free_stmt_operands (cfun, stmt);
7540 2184529 : push_cfun (dest_cfun);
7541 2184529 : update_stmt (stmt);
7542 2184529 : if (is_gimple_call (stmt))
7543 282737 : notice_special_calls (as_a <gcall *> (stmt));
7544 2184529 : pop_cfun ();
7545 : }
7546 :
7547 1494430 : FOR_EACH_EDGE (e, ei, bb->succs)
7548 818109 : if (e->goto_locus != UNKNOWN_LOCATION)
7549 : {
7550 35586 : tree block = LOCATION_BLOCK (e->goto_locus);
7551 35586 : if (d->orig_block == NULL_TREE
7552 35584 : || block == d->orig_block)
7553 2657 : e->goto_locus = set_block (e->goto_locus, d->new_block);
7554 : }
7555 676321 : }
7556 :
7557 : /* Examine the statements in BB (which is in SRC_CFUN); find and return
7558 : the outermost EH region. Use REGION as the incoming base EH region.
7559 : If there is no single outermost region, return NULL and set *ALL to
7560 : true. */
7561 :
7562 : static eh_region
7563 676316 : find_outermost_region_in_block (struct function *src_cfun,
7564 : basic_block bb, eh_region region,
7565 : bool *all)
7566 : {
7567 676316 : gimple_stmt_iterator si;
7568 :
7569 3537149 : for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
7570 : {
7571 2184518 : gimple *stmt = gsi_stmt (si);
7572 2184518 : eh_region stmt_region;
7573 2184518 : int lp_nr;
7574 :
7575 2184518 : lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
7576 2184518 : stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
7577 2184518 : if (stmt_region)
7578 : {
7579 3916 : if (region == NULL)
7580 : region = stmt_region;
7581 1862 : else if (stmt_region != region)
7582 : {
7583 249 : region = eh_region_outermost (src_cfun, stmt_region, region);
7584 249 : if (region == NULL)
7585 : {
7586 1 : *all = true;
7587 1 : return NULL;
7588 : }
7589 : }
7590 : }
7591 : }
7592 :
7593 : return region;
7594 : }
7595 :
7596 : static tree
7597 13 : new_label_mapper (tree decl, void *data)
7598 : {
7599 13 : htab_t hash = (htab_t) data;
7600 13 : struct tree_map *m;
7601 13 : void **slot;
7602 :
7603 13 : gcc_assert (TREE_CODE (decl) == LABEL_DECL);
7604 :
7605 13 : m = XNEW (struct tree_map);
7606 13 : m->hash = DECL_UID (decl);
7607 13 : m->base.from = decl;
7608 13 : m->to = create_artificial_label (UNKNOWN_LOCATION);
7609 13 : LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
7610 13 : if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
7611 5 : cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
7612 :
7613 13 : slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
7614 13 : gcc_assert (*slot == NULL);
7615 :
7616 13 : *slot = m;
7617 :
7618 13 : return m->to;
7619 : }
7620 :
7621 : /* Tree walker to replace the decls used inside value expressions by
7622 : duplicates. */
7623 :
7624 : static tree
7625 280676 : replace_block_vars_by_duplicates_1 (tree *tp, int *walk_subtrees, void *data)
7626 : {
7627 280676 : struct replace_decls_d *rd = (struct replace_decls_d *)data;
7628 :
7629 280676 : switch (TREE_CODE (*tp))
7630 : {
7631 54396 : case VAR_DECL:
7632 54396 : case PARM_DECL:
7633 54396 : case RESULT_DECL:
7634 54396 : replace_by_duplicate_decl (tp, rd->vars_map, rd->to_context);
7635 54396 : break;
7636 : default:
7637 : break;
7638 : }
7639 :
7640 280676 : if (IS_TYPE_OR_DECL_P (*tp))
7641 91634 : *walk_subtrees = false;
7642 :
7643 280676 : return NULL;
7644 : }
7645 :
7646 : /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
7647 : subblocks. */
7648 :
7649 : static void
7650 181146 : replace_block_vars_by_duplicates (tree block, hash_map<tree, tree> *vars_map,
7651 : tree to_context)
7652 : {
7653 181146 : tree *tp, t;
7654 :
7655 884777 : for (tp = &BLOCK_VARS (block); *tp; tp = &DECL_CHAIN (*tp))
7656 : {
7657 703631 : t = *tp;
7658 703631 : if (!VAR_P (t) && TREE_CODE (t) != CONST_DECL)
7659 7879 : continue;
7660 695752 : replace_by_duplicate_decl (&t, vars_map, to_context);
7661 695752 : if (t != *tp)
7662 : {
7663 695752 : if (VAR_P (*tp) && DECL_HAS_VALUE_EXPR_P (*tp))
7664 : {
7665 48440 : tree x = DECL_VALUE_EXPR (*tp);
7666 48440 : struct replace_decls_d rd = { vars_map, to_context };
7667 48440 : unshare_expr (x);
7668 48440 : walk_tree (&x, replace_block_vars_by_duplicates_1, &rd, NULL);
7669 48440 : SET_DECL_VALUE_EXPR (t, x);
7670 48440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
7671 : }
7672 695752 : DECL_CHAIN (t) = DECL_CHAIN (*tp);
7673 695752 : *tp = t;
7674 : }
7675 : }
7676 :
7677 316448 : for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
7678 135302 : replace_block_vars_by_duplicates (block, vars_map, to_context);
7679 181146 : }
7680 :
7681 : /* Fixup the loop arrays and numbers after moving LOOP and its subloops
7682 : from FN1 to FN2. */
7683 :
7684 : static void
7685 74129 : fixup_loop_arrays_after_move (struct function *fn1, struct function *fn2,
7686 : class loop *loop)
7687 : {
7688 : /* Discard it from the old loop array. */
7689 148258 : (*get_loops (fn1))[loop->num] = NULL;
7690 :
7691 : /* Place it in the new loop array, assigning it a new number. */
7692 74129 : loop->num = number_of_loops (fn2);
7693 74129 : vec_safe_push (loops_for_fn (fn2)->larray, loop);
7694 :
7695 : /* Recurse to children. */
7696 105063 : for (loop = loop->inner; loop; loop = loop->next)
7697 30934 : fixup_loop_arrays_after_move (fn1, fn2, loop);
7698 74129 : }
7699 :
7700 : /* Verify that the blocks in BBS_P are a single-entry, single-exit region
7701 : delimited by ENTRY_BB and EXIT_BB, possibly containing noreturn blocks. */
7702 :
7703 : DEBUG_FUNCTION void
7704 45844 : verify_sese (basic_block entry, basic_block exit, vec<basic_block> *bbs_p)
7705 : {
7706 45844 : basic_block bb;
7707 45844 : edge_iterator ei;
7708 45844 : edge e;
7709 45844 : bitmap bbs = BITMAP_ALLOC (NULL);
7710 45844 : int i;
7711 :
7712 45844 : gcc_assert (entry != NULL);
7713 45844 : gcc_assert (entry != exit);
7714 45844 : gcc_assert (bbs_p != NULL);
7715 :
7716 45844 : gcc_assert (bbs_p->length () > 0);
7717 :
7718 722165 : FOR_EACH_VEC_ELT (*bbs_p, i, bb)
7719 676321 : bitmap_set_bit (bbs, bb->index);
7720 :
7721 45844 : gcc_assert (bitmap_bit_p (bbs, entry->index));
7722 45844 : gcc_assert (exit == NULL || bitmap_bit_p (bbs, exit->index));
7723 :
7724 722165 : FOR_EACH_VEC_ELT (*bbs_p, i, bb)
7725 : {
7726 676321 : if (bb == entry)
7727 : {
7728 45844 : gcc_assert (single_pred_p (entry));
7729 45844 : gcc_assert (!bitmap_bit_p (bbs, single_pred (entry)->index));
7730 : }
7731 : else
7732 1448586 : for (ei = ei_start (bb->preds); !ei_end_p (ei); ei_next (&ei))
7733 : {
7734 818109 : e = ei_edge (ei);
7735 818109 : gcc_assert (bitmap_bit_p (bbs, e->src->index));
7736 : }
7737 :
7738 676321 : if (bb == exit)
7739 : {
7740 45723 : gcc_assert (single_succ_p (exit));
7741 45723 : gcc_assert (!bitmap_bit_p (bbs, single_succ (exit)->index));
7742 : }
7743 : else
7744 1448707 : for (ei = ei_start (bb->succs); !ei_end_p (ei); ei_next (&ei))
7745 : {
7746 818109 : e = ei_edge (ei);
7747 818109 : gcc_assert (bitmap_bit_p (bbs, e->dest->index));
7748 : }
7749 : }
7750 :
7751 45844 : BITMAP_FREE (bbs);
7752 45844 : }
7753 :
7754 : /* If FROM is an SSA_NAME, mark the version in bitmap DATA. */
7755 :
7756 : bool
7757 1402456 : gather_ssa_name_hash_map_from (tree const &from, tree const &, void *data)
7758 : {
7759 1402456 : bitmap release_names = (bitmap)data;
7760 :
7761 1402456 : if (TREE_CODE (from) != SSA_NAME)
7762 : return true;
7763 :
7764 6033 : bitmap_set_bit (release_names, SSA_NAME_VERSION (from));
7765 6033 : return true;
7766 : }
7767 :
7768 : /* Return LOOP_DIST_ALIAS call if present in BB. */
7769 :
7770 : static gimple *
7771 0 : find_loop_dist_alias (basic_block bb)
7772 : {
7773 0 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
7774 0 : if (!safe_is_a <gcond *> (*gsi))
7775 : return NULL;
7776 :
7777 0 : gsi_prev (&gsi);
7778 0 : if (gsi_end_p (gsi))
7779 : return NULL;
7780 :
7781 0 : gimple *g = gsi_stmt (gsi);
7782 0 : if (gimple_call_internal_p (g, IFN_LOOP_DIST_ALIAS))
7783 0 : return g;
7784 : return NULL;
7785 : }
7786 :
7787 : /* Fold loop internal call G like IFN_LOOP_VECTORIZED/IFN_LOOP_DIST_ALIAS
7788 : to VALUE and update any immediate uses of it's LHS. */
7789 :
7790 : void
7791 26056 : fold_loop_internal_call (gimple *g, tree value)
7792 : {
7793 26056 : tree lhs = gimple_call_lhs (g);
7794 26056 : use_operand_p use_p;
7795 26056 : imm_use_iterator iter;
7796 26056 : gimple *use_stmt;
7797 26056 : gimple_stmt_iterator gsi = gsi_for_stmt (g);
7798 :
7799 26056 : replace_call_with_value (&gsi, value);
7800 52010 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
7801 : {
7802 51908 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
7803 25954 : SET_USE (use_p, value);
7804 25954 : update_stmt (use_stmt);
7805 : /* If we turn conditional to constant, scale profile counts.
7806 : We know that the conditional was created by loop distribution
7807 : and all basic blocks dominated by the taken edge are part of
7808 : the loop distributed. */
7809 25954 : if (gimple_code (use_stmt) == GIMPLE_COND)
7810 : {
7811 25954 : edge true_edge, false_edge;
7812 25954 : extract_true_false_edges_from_block (gimple_bb (use_stmt),
7813 : &true_edge, &false_edge);
7814 25954 : edge taken_edge = NULL, other_edge = NULL;
7815 25954 : if (gimple_cond_true_p (as_a <gcond *>(use_stmt)))
7816 : {
7817 7105 : taken_edge = true_edge;
7818 7105 : other_edge = false_edge;
7819 : }
7820 18849 : else if (gimple_cond_false_p (as_a <gcond *>(use_stmt)))
7821 : {
7822 18846 : taken_edge = false_edge;
7823 18846 : other_edge = true_edge;
7824 : }
7825 25951 : if (taken_edge
7826 25954 : && !(taken_edge->probability == profile_probability::always ()))
7827 : {
7828 30 : profile_count old_count = taken_edge->count ();
7829 30 : profile_count new_count = taken_edge->src->count;
7830 30 : taken_edge->probability = profile_probability::always ();
7831 30 : other_edge->probability = profile_probability::never ();
7832 : /* If we have multiple predecessors, we can't use the dominance
7833 : test. This should not happen as the guarded code should
7834 : start with pre-header. */
7835 30 : gcc_assert (single_pred_edge (taken_edge->dest));
7836 60 : if (old_count.nonzero_p ())
7837 : {
7838 28 : taken_edge->dest->count
7839 28 : = taken_edge->dest->count.apply_scale (new_count,
7840 : old_count);
7841 28 : scale_strictly_dominated_blocks (taken_edge->dest,
7842 : new_count, old_count);
7843 : }
7844 : }
7845 : }
7846 26056 : }
7847 26056 : }
7848 :
7849 : /* Move a single-entry, single-exit region delimited by ENTRY_BB and
7850 : EXIT_BB to function DEST_CFUN. The whole region is replaced by a
7851 : single basic block in the original CFG and the new basic block is
7852 : returned. DEST_CFUN must not have a CFG yet.
7853 :
7854 : Note that the region need not be a pure SESE region. Blocks inside
7855 : the region may contain calls to abort/exit. The only restriction
7856 : is that ENTRY_BB should be the only entry point and it must
7857 : dominate EXIT_BB.
7858 :
7859 : Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
7860 : functions outermost BLOCK, move all subblocks of ORIG_BLOCK
7861 : to the new function.
7862 :
7863 : All local variables referenced in the region are assumed to be in
7864 : the corresponding BLOCK_VARS and unexpanded variable lists
7865 : associated with DEST_CFUN.
7866 :
7867 : TODO: investigate whether we can reuse gimple_duplicate_sese_region to
7868 : reimplement move_sese_region_to_fn by duplicating the region rather than
7869 : moving it. */
7870 :
7871 : basic_block
7872 45844 : move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
7873 : basic_block exit_bb, tree orig_block)
7874 : {
7875 45844 : vec<basic_block> bbs;
7876 45844 : basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
7877 45844 : basic_block after, bb, *entry_pred, *exit_succ, abb;
7878 45844 : struct function *saved_cfun = cfun;
7879 45844 : int *entry_flag, *exit_flag;
7880 45844 : profile_probability *entry_prob, *exit_prob;
7881 45844 : unsigned i, num_entry_edges, num_exit_edges, num_nodes;
7882 45844 : edge e;
7883 45844 : edge_iterator ei;
7884 45844 : htab_t new_label_map;
7885 45844 : hash_map<void *, void *> *eh_map;
7886 45844 : class loop *loop = entry_bb->loop_father;
7887 45844 : class loop *loop0 = get_loop (saved_cfun, 0);
7888 45844 : struct move_stmt_d d;
7889 :
7890 : /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
7891 : region. */
7892 45844 : gcc_assert (entry_bb != exit_bb
7893 : && (!exit_bb
7894 : || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
7895 :
7896 : /* Collect all the blocks in the region. Manually add ENTRY_BB
7897 : because it won't be added by dfs_enumerate_from. */
7898 45844 : bbs.create (0);
7899 45844 : bbs.safe_push (entry_bb);
7900 45844 : gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
7901 :
7902 45844 : if (flag_checking)
7903 45844 : verify_sese (entry_bb, exit_bb, &bbs);
7904 :
7905 : /* The blocks that used to be dominated by something in BBS will now be
7906 : dominated by the new block. */
7907 45844 : auto_vec<basic_block> dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
7908 : bbs.address (),
7909 91688 : bbs.length ());
7910 :
7911 : /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
7912 : the predecessor edges to ENTRY_BB and the successor edges to
7913 : EXIT_BB so that we can re-attach them to the new basic block that
7914 : will replace the region. */
7915 45844 : num_entry_edges = EDGE_COUNT (entry_bb->preds);
7916 45844 : entry_pred = XNEWVEC (basic_block, num_entry_edges);
7917 45844 : entry_flag = XNEWVEC (int, num_entry_edges);
7918 45844 : entry_prob = XNEWVEC (profile_probability, num_entry_edges);
7919 45844 : i = 0;
7920 91688 : for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
7921 : {
7922 45844 : entry_prob[i] = e->probability;
7923 45844 : entry_flag[i] = e->flags;
7924 45844 : entry_pred[i++] = e->src;
7925 45844 : remove_edge (e);
7926 : }
7927 :
7928 45844 : if (exit_bb)
7929 : {
7930 45723 : num_exit_edges = EDGE_COUNT (exit_bb->succs);
7931 45723 : exit_succ = XNEWVEC (basic_block, num_exit_edges);
7932 45723 : exit_flag = XNEWVEC (int, num_exit_edges);
7933 45723 : exit_prob = XNEWVEC (profile_probability, num_exit_edges);
7934 45723 : i = 0;
7935 91446 : for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
7936 : {
7937 45723 : exit_prob[i] = e->probability;
7938 45723 : exit_flag[i] = e->flags;
7939 45723 : exit_succ[i++] = e->dest;
7940 45723 : remove_edge (e);
7941 : }
7942 : }
7943 : else
7944 : {
7945 : num_exit_edges = 0;
7946 : exit_succ = NULL;
7947 : exit_flag = NULL;
7948 : exit_prob = NULL;
7949 : }
7950 :
7951 : /* Switch context to the child function to initialize DEST_FN's CFG. */
7952 45844 : gcc_assert (dest_cfun->cfg == NULL);
7953 45844 : push_cfun (dest_cfun);
7954 :
7955 45844 : init_empty_tree_cfg ();
7956 :
7957 : /* Initialize EH information for the new function. */
7958 45844 : eh_map = NULL;
7959 45844 : new_label_map = NULL;
7960 45844 : if (saved_cfun->eh)
7961 : {
7962 45844 : eh_region region = NULL;
7963 45844 : bool all = false;
7964 :
7965 722159 : FOR_EACH_VEC_ELT (bbs, i, bb)
7966 : {
7967 676316 : region = find_outermost_region_in_block (saved_cfun, bb, region, &all);
7968 676316 : if (all)
7969 : break;
7970 : }
7971 :
7972 45844 : init_eh_for_function ();
7973 45844 : if (region != NULL || all)
7974 : {
7975 2054 : new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
7976 2054 : eh_map = duplicate_eh_regions (saved_cfun, region, 0,
7977 : new_label_mapper, new_label_map);
7978 : }
7979 : }
7980 :
7981 : /* Initialize an empty loop tree. */
7982 45844 : struct loops *loops = ggc_cleared_alloc<struct loops> ();
7983 45844 : init_loops_structure (dest_cfun, loops, 1);
7984 45844 : loops->state = LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
7985 45844 : set_loops_for_fn (dest_cfun, loops);
7986 :
7987 91688 : vec<loop_p, va_gc> *larray = get_loops (saved_cfun)->copy ();
7988 :
7989 : /* Move the outlined loop tree part. */
7990 45844 : num_nodes = bbs.length ();
7991 722165 : FOR_EACH_VEC_ELT (bbs, i, bb)
7992 : {
7993 676321 : if (bb->loop_father->header == bb)
7994 : {
7995 74129 : class loop *this_loop = bb->loop_father;
7996 : /* Avoid the need to remap SSA names used in nb_iterations. */
7997 74129 : free_numbers_of_iterations_estimates (this_loop);
7998 74129 : class loop *outer = loop_outer (this_loop);
7999 74129 : if (outer == loop
8000 : /* If the SESE region contains some bbs ending with
8001 : a noreturn call, those are considered to belong
8002 : to the outermost loop in saved_cfun, rather than
8003 : the entry_bb's loop_father. */
8004 74129 : || outer == loop0)
8005 : {
8006 43195 : if (outer != loop)
8007 8 : num_nodes -= this_loop->num_nodes;
8008 43195 : flow_loop_tree_node_remove (bb->loop_father);
8009 43195 : flow_loop_tree_node_add (get_loop (dest_cfun, 0), this_loop);
8010 43195 : fixup_loop_arrays_after_move (saved_cfun, cfun, this_loop);
8011 : }
8012 : }
8013 602192 : else if (bb->loop_father == loop0 && loop0 != loop)
8014 1294 : num_nodes--;
8015 :
8016 : /* Remove loop exits from the outlined region. */
8017 676321 : if (loops_for_fn (saved_cfun)->exits)
8018 5594 : FOR_EACH_EDGE (e, ei, bb->succs)
8019 : {
8020 3036 : struct loops *l = loops_for_fn (saved_cfun);
8021 3036 : loop_exit **slot
8022 3036 : = l->exits->find_slot_with_hash (e, htab_hash_pointer (e),
8023 : NO_INSERT);
8024 3036 : if (slot)
8025 287 : l->exits->clear_slot (slot);
8026 : }
8027 : }
8028 :
8029 : /* Adjust the number of blocks in the tree root of the outlined part. */
8030 91688 : get_loop (dest_cfun, 0)->num_nodes = bbs.length () + 2;
8031 :
8032 : /* Setup a mapping to be used by move_block_to_fn. */
8033 45844 : loop->aux = current_loops->tree_root;
8034 45844 : loop0->aux = current_loops->tree_root;
8035 :
8036 : /* Fix up orig_loop_num. If the block referenced in it has been moved
8037 : to dest_cfun, update orig_loop_num field, otherwise clear it. */
8038 45844 : signed char *moved_orig_loop_num = NULL;
8039 211661 : for (auto dloop : loops_list (dest_cfun, 0))
8040 74129 : if (dloop->orig_loop_num)
8041 : {
8042 0 : if (moved_orig_loop_num == NULL)
8043 0 : moved_orig_loop_num
8044 0 : = XCNEWVEC (signed char, vec_safe_length (larray));
8045 0 : if ((*larray)[dloop->orig_loop_num] != NULL
8046 0 : && get_loop (saved_cfun, dloop->orig_loop_num) == NULL)
8047 : {
8048 0 : if (moved_orig_loop_num[dloop->orig_loop_num] >= 0
8049 0 : && moved_orig_loop_num[dloop->orig_loop_num] < 2)
8050 0 : moved_orig_loop_num[dloop->orig_loop_num]++;
8051 0 : dloop->orig_loop_num = (*larray)[dloop->orig_loop_num]->num;
8052 : }
8053 : else
8054 : {
8055 0 : moved_orig_loop_num[dloop->orig_loop_num] = -1;
8056 0 : dloop->orig_loop_num = 0;
8057 : }
8058 45844 : }
8059 45844 : pop_cfun ();
8060 :
8061 45844 : if (moved_orig_loop_num)
8062 : {
8063 0 : FOR_EACH_VEC_ELT (bbs, i, bb)
8064 : {
8065 0 : gimple *g = find_loop_dist_alias (bb);
8066 0 : if (g == NULL)
8067 0 : continue;
8068 :
8069 0 : int orig_loop_num = tree_to_shwi (gimple_call_arg (g, 0));
8070 0 : gcc_assert (orig_loop_num
8071 : && (unsigned) orig_loop_num < vec_safe_length (larray));
8072 0 : if (moved_orig_loop_num[orig_loop_num] == 2)
8073 : {
8074 : /* If we have moved both loops with this orig_loop_num into
8075 : dest_cfun and the LOOP_DIST_ALIAS call is being moved there
8076 : too, update the first argument. */
8077 0 : gcc_assert ((*larray)[orig_loop_num] != NULL
8078 : && (get_loop (saved_cfun, orig_loop_num) == NULL));
8079 0 : tree t = build_int_cst (integer_type_node,
8080 0 : (*larray)[orig_loop_num]->num);
8081 0 : gimple_call_set_arg (g, 0, t);
8082 0 : update_stmt (g);
8083 : /* Make sure the following loop will not update it. */
8084 0 : moved_orig_loop_num[orig_loop_num] = 0;
8085 : }
8086 : else
8087 : /* Otherwise at least one of the loops stayed in saved_cfun.
8088 : Remove the LOOP_DIST_ALIAS call. */
8089 0 : fold_loop_internal_call (g, gimple_call_arg (g, 1));
8090 : }
8091 0 : FOR_EACH_BB_FN (bb, saved_cfun)
8092 : {
8093 0 : gimple *g = find_loop_dist_alias (bb);
8094 0 : if (g == NULL)
8095 0 : continue;
8096 0 : int orig_loop_num = tree_to_shwi (gimple_call_arg (g, 0));
8097 0 : gcc_assert (orig_loop_num
8098 : && (unsigned) orig_loop_num < vec_safe_length (larray));
8099 0 : if (moved_orig_loop_num[orig_loop_num])
8100 : /* LOOP_DIST_ALIAS call remained in saved_cfun, if at least one
8101 : of the corresponding loops was moved, remove it. */
8102 0 : fold_loop_internal_call (g, gimple_call_arg (g, 1));
8103 : }
8104 0 : XDELETEVEC (moved_orig_loop_num);
8105 : }
8106 45844 : ggc_free (larray);
8107 :
8108 : /* Move blocks from BBS into DEST_CFUN. */
8109 45844 : gcc_assert (bbs.length () >= 2);
8110 45844 : after = dest_cfun->cfg->x_entry_block_ptr;
8111 45844 : hash_map<tree, tree> vars_map;
8112 :
8113 45844 : memset (&d, 0, sizeof (d));
8114 45844 : d.orig_block = orig_block;
8115 45844 : d.new_block = DECL_INITIAL (dest_cfun->decl);
8116 45844 : d.from_context = cfun->decl;
8117 45844 : d.to_context = dest_cfun->decl;
8118 45844 : d.vars_map = &vars_map;
8119 45844 : d.new_label_map = new_label_map;
8120 45844 : d.eh_map = eh_map;
8121 45844 : d.remap_decls_p = true;
8122 :
8123 45844 : if (gimple_in_ssa_p (cfun))
8124 386 : for (tree arg = DECL_ARGUMENTS (d.to_context); arg; arg = DECL_CHAIN (arg))
8125 : {
8126 193 : tree narg = make_ssa_name_fn (dest_cfun, arg, gimple_build_nop ());
8127 193 : set_ssa_default_def (dest_cfun, arg, narg);
8128 193 : vars_map.put (arg, narg);
8129 : }
8130 :
8131 722165 : FOR_EACH_VEC_ELT (bbs, i, bb)
8132 : {
8133 : /* No need to update edge counts on the last block. It has
8134 : already been updated earlier when we detached the region from
8135 : the original CFG. */
8136 676321 : move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
8137 676321 : after = bb;
8138 : }
8139 :
8140 : /* Adjust the maximum clique used. */
8141 45844 : dest_cfun->last_clique = saved_cfun->last_clique;
8142 :
8143 45844 : loop->aux = NULL;
8144 45844 : loop0->aux = NULL;
8145 : /* Loop sizes are no longer correct, fix them up. */
8146 45844 : loop->num_nodes -= num_nodes;
8147 45844 : for (class loop *outer = loop_outer (loop);
8148 50651 : outer; outer = loop_outer (outer))
8149 4807 : outer->num_nodes -= num_nodes;
8150 45844 : loop0->num_nodes -= bbs.length () - num_nodes;
8151 :
8152 45844 : if (saved_cfun->has_simduid_loops || saved_cfun->has_force_vectorize_loops)
8153 : {
8154 10951 : class loop *aloop;
8155 36978 : for (i = 0; vec_safe_iterate (loops->larray, i, &aloop); i++)
8156 26027 : if (aloop != NULL)
8157 : {
8158 26027 : if (aloop->simduid)
8159 : {
8160 2953 : replace_by_duplicate_decl (&aloop->simduid, d.vars_map,
8161 : d.to_context);
8162 2953 : dest_cfun->has_simduid_loops = true;
8163 : }
8164 26027 : if (aloop->force_vectorize)
8165 5235 : dest_cfun->has_force_vectorize_loops = true;
8166 : }
8167 : }
8168 :
8169 : /* Rewire BLOCK_SUBBLOCKS of orig_block. */
8170 45844 : if (orig_block)
8171 : {
8172 45651 : tree block;
8173 45651 : gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
8174 : == NULL_TREE);
8175 91302 : BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
8176 45651 : = BLOCK_SUBBLOCKS (orig_block);
8177 45651 : for (block = BLOCK_SUBBLOCKS (orig_block);
8178 87757 : block; block = BLOCK_CHAIN (block))
8179 42106 : BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
8180 45651 : BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
8181 : }
8182 :
8183 45844 : replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
8184 : &vars_map, dest_cfun->decl);
8185 :
8186 45844 : if (new_label_map)
8187 2054 : htab_delete (new_label_map);
8188 45844 : if (eh_map)
8189 2054 : delete eh_map;
8190 :
8191 : /* We need to release ssa-names in a defined order, so first find them,
8192 : and then iterate in ascending version order. */
8193 45844 : bitmap release_names = BITMAP_ALLOC (NULL);
8194 1448300 : vars_map.traverse<void *, gather_ssa_name_hash_map_from> (release_names);
8195 45844 : bitmap_iterator bi;
8196 51877 : EXECUTE_IF_SET_IN_BITMAP (release_names, 0, i, bi)
8197 6033 : release_ssa_name (ssa_name (i));
8198 45844 : BITMAP_FREE (release_names);
8199 :
8200 : /* Rewire the entry and exit blocks. The successor to the entry
8201 : block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
8202 : the child function. Similarly, the predecessor of DEST_FN's
8203 : EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
8204 : need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
8205 : various CFG manipulation function get to the right CFG.
8206 :
8207 : FIXME, this is silly. The CFG ought to become a parameter to
8208 : these helpers. */
8209 45844 : push_cfun (dest_cfun);
8210 45844 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = entry_bb->count;
8211 45844 : make_single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), entry_bb, EDGE_FALLTHRU);
8212 45844 : if (exit_bb)
8213 : {
8214 45723 : make_single_succ_edge (exit_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
8215 45723 : EXIT_BLOCK_PTR_FOR_FN (cfun)->count = exit_bb->count;
8216 : }
8217 : else
8218 121 : EXIT_BLOCK_PTR_FOR_FN (cfun)->count = profile_count::zero ();
8219 45844 : pop_cfun ();
8220 :
8221 : /* Back in the original function, the SESE region has disappeared,
8222 : create a new basic block in its place. */
8223 45844 : bb = create_empty_bb (entry_pred[0]);
8224 45844 : if (current_loops)
8225 45844 : add_bb_to_loop (bb, loop);
8226 45844 : profile_count count = profile_count::zero ();
8227 91688 : for (i = 0; i < num_entry_edges; i++)
8228 : {
8229 45844 : e = make_edge (entry_pred[i], bb, entry_flag[i]);
8230 45844 : e->probability = entry_prob[i];
8231 45844 : count += e->count ();
8232 : }
8233 45844 : bb->count = count;
8234 :
8235 91567 : for (i = 0; i < num_exit_edges; i++)
8236 : {
8237 45723 : e = make_edge (bb, exit_succ[i], exit_flag[i]);
8238 45723 : e->probability = exit_prob[i];
8239 : }
8240 :
8241 45844 : set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
8242 87711 : FOR_EACH_VEC_ELT (dom_bbs, i, abb)
8243 41867 : set_immediate_dominator (CDI_DOMINATORS, abb, bb);
8244 :
8245 45844 : if (exit_bb)
8246 : {
8247 45723 : free (exit_prob);
8248 45723 : free (exit_flag);
8249 45723 : free (exit_succ);
8250 : }
8251 45844 : free (entry_prob);
8252 45844 : free (entry_flag);
8253 45844 : free (entry_pred);
8254 45844 : bbs.release ();
8255 :
8256 45844 : return bb;
8257 45844 : }
8258 :
8259 : /* Dump default def DEF to file FILE using FLAGS and indentation
8260 : SPC. */
8261 :
8262 : static void
8263 554 : dump_default_def (FILE *file, tree def, int spc, dump_flags_t flags)
8264 : {
8265 1662 : for (int i = 0; i < spc; ++i)
8266 1108 : fprintf (file, " ");
8267 554 : dump_ssaname_info_to_file (file, def, spc);
8268 :
8269 554 : print_generic_expr (file, TREE_TYPE (def), flags);
8270 554 : fprintf (file, " ");
8271 554 : print_generic_expr (file, def, flags);
8272 554 : fprintf (file, " = ");
8273 554 : print_generic_expr (file, SSA_NAME_VAR (def), flags);
8274 554 : fprintf (file, ";\n");
8275 554 : }
8276 :
8277 : /* Print no_sanitize attribute to FILE for a given attribute VALUE. */
8278 :
8279 : static void
8280 69 : print_no_sanitize_attr_value (FILE *file, tree value)
8281 : {
8282 69 : sanitize_code_type flags = tree_to_sanitize_code_type (value);
8283 69 : bool first = true;
8284 2415 : for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
8285 : {
8286 2346 : if ((sanitizer_opts[i].flag & flags) == sanitizer_opts[i].flag)
8287 : {
8288 289 : if (!first)
8289 220 : fprintf (file, " | ");
8290 289 : fprintf (file, "%s", sanitizer_opts[i].name);
8291 289 : first = false;
8292 : }
8293 : }
8294 69 : }
8295 :
8296 : /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in dumpfile.h)
8297 : */
8298 :
8299 : void
8300 70387 : dump_function_to_file (tree fndecl, FILE *file, dump_flags_t flags)
8301 : {
8302 70387 : tree arg, var, old_current_fndecl = current_function_decl;
8303 70387 : struct function *dsf;
8304 70387 : bool ignore_topmost_bind = false, any_var = false;
8305 70387 : basic_block bb;
8306 70387 : tree chain;
8307 70387 : bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
8308 134783 : && decl_is_tm_clone (fndecl));
8309 70387 : struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
8310 :
8311 70387 : tree fntype = TREE_TYPE (fndecl);
8312 70387 : tree attrs[] = { DECL_ATTRIBUTES (fndecl), TYPE_ATTRIBUTES (fntype) };
8313 :
8314 211161 : for (int i = 0; i != 2; ++i)
8315 : {
8316 140774 : if (!attrs[i])
8317 115372 : continue;
8318 :
8319 25402 : fprintf (file, "__attribute__((");
8320 :
8321 25402 : bool first = true;
8322 25402 : tree chain;
8323 96846 : for (chain = attrs[i]; chain; first = false, chain = TREE_CHAIN (chain))
8324 : {
8325 46042 : if (!first)
8326 20640 : fprintf (file, ", ");
8327 :
8328 46042 : tree name = get_attribute_name (chain);
8329 46042 : print_generic_expr (file, name, flags);
8330 46042 : if (TREE_VALUE (chain) != NULL_TREE)
8331 : {
8332 11638 : fprintf (file, " (");
8333 :
8334 11638 : if (strstr (IDENTIFIER_POINTER (name), "no_sanitize"))
8335 69 : print_no_sanitize_attr_value (file, TREE_VALUE (chain));
8336 11569 : else if (!strcmp (IDENTIFIER_POINTER (name),
8337 : "omp declare variant base"))
8338 : {
8339 254 : tree a = TREE_VALUE (chain);
8340 254 : print_generic_expr (file, TREE_PURPOSE (a), flags);
8341 254 : fprintf (file, " match ");
8342 254 : print_omp_context_selector (file, TREE_VALUE (a),
8343 : flags);
8344 : }
8345 : else
8346 11315 : print_generic_expr (file, TREE_VALUE (chain), flags);
8347 11638 : fprintf (file, ")");
8348 : }
8349 : }
8350 :
8351 25402 : fprintf (file, "))\n");
8352 : }
8353 :
8354 70387 : current_function_decl = fndecl;
8355 70387 : if (flags & TDF_GIMPLE)
8356 : {
8357 44 : static bool hotness_bb_param_printed = false;
8358 44 : if (profile_info != NULL
8359 0 : && !hotness_bb_param_printed)
8360 : {
8361 0 : hotness_bb_param_printed = true;
8362 0 : fprintf (file,
8363 : "/* --param=gimple-fe-computed-hot-bb-threshold=%" PRId64
8364 : " */\n", get_hot_bb_threshold ());
8365 : }
8366 :
8367 44 : print_generic_expr (file, TREE_TYPE (TREE_TYPE (fndecl)),
8368 : flags | TDF_SLIM);
8369 44 : fprintf (file, " __GIMPLE (%s",
8370 44 : (fun->curr_properties & PROP_ssa) ? "ssa"
8371 9 : : (fun->curr_properties & PROP_cfg) ? "cfg"
8372 : : "");
8373 :
8374 44 : if (fun && fun->cfg)
8375 : {
8376 40 : basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (fun);
8377 40 : if (bb->count.initialized_p ())
8378 0 : fprintf (file, ",%s(%" PRIu64 ")",
8379 : profile_quality_as_string (bb->count.quality ()),
8380 : bb->count.value ());
8381 40 : if (flags & TDF_UID)
8382 0 : fprintf (file, ")\n%sD_%u (", function_name (fun),
8383 0 : DECL_UID (fndecl));
8384 : else
8385 40 : fprintf (file, ")\n%s (", function_name (fun));
8386 : }
8387 : }
8388 : else
8389 : {
8390 70343 : print_generic_expr (file, TREE_TYPE (fntype), flags);
8391 70343 : if (flags & TDF_UID)
8392 1294 : fprintf (file, " %sD.%u %s(", function_name (fun), DECL_UID (fndecl),
8393 : tmclone ? "[tm-clone] " : "");
8394 : else
8395 139355 : fprintf (file, " %s %s(", function_name (fun),
8396 : tmclone ? "[tm-clone] " : "");
8397 : }
8398 :
8399 70387 : arg = DECL_ARGUMENTS (fndecl);
8400 165830 : while (arg)
8401 : {
8402 95443 : print_generic_expr (file, TREE_TYPE (arg), flags);
8403 95443 : fprintf (file, " ");
8404 95443 : print_generic_expr (file, arg, flags);
8405 95443 : if (DECL_CHAIN (arg))
8406 48167 : fprintf (file, ", ");
8407 95443 : arg = DECL_CHAIN (arg);
8408 : }
8409 70387 : fprintf (file, ")\n");
8410 :
8411 70387 : dsf = DECL_STRUCT_FUNCTION (fndecl);
8412 70387 : if (dsf && (flags & TDF_EH))
8413 662 : dump_eh_tree (file, dsf);
8414 :
8415 70387 : if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
8416 : {
8417 2 : dump_node (fndecl, TDF_SLIM | flags, file);
8418 2 : current_function_decl = old_current_fndecl;
8419 2 : return;
8420 : }
8421 :
8422 : /* When GIMPLE is lowered, the variables are no longer available in
8423 : BIND_EXPRs, so display them separately. */
8424 70385 : if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
8425 : {
8426 57133 : unsigned ix;
8427 57133 : ignore_topmost_bind = true;
8428 :
8429 57133 : fprintf (file, "{\n");
8430 113039 : if (gimple_in_ssa_p (fun)
8431 55906 : && (flags & TDF_ALIAS))
8432 : {
8433 1234 : for (arg = DECL_ARGUMENTS (fndecl); arg != NULL;
8434 580 : arg = DECL_CHAIN (arg))
8435 : {
8436 580 : tree def = ssa_default_def (fun, arg);
8437 580 : if (def)
8438 554 : dump_default_def (file, def, 2, flags);
8439 : }
8440 :
8441 654 : tree res = DECL_RESULT (fun->decl);
8442 654 : if (res != NULL_TREE
8443 654 : && DECL_BY_REFERENCE (res))
8444 : {
8445 0 : tree def = ssa_default_def (fun, res);
8446 0 : if (def)
8447 0 : dump_default_def (file, def, 2, flags);
8448 : }
8449 :
8450 654 : tree static_chain = fun->static_chain_decl;
8451 654 : if (static_chain != NULL_TREE)
8452 : {
8453 0 : tree def = ssa_default_def (fun, static_chain);
8454 0 : if (def)
8455 0 : dump_default_def (file, def, 2, flags);
8456 : }
8457 : }
8458 :
8459 109315 : if (!vec_safe_is_empty (fun->local_decls))
8460 318627 : FOR_EACH_LOCAL_DECL (fun, ix, var)
8461 : {
8462 285517 : print_generic_decl (file, var, flags);
8463 285517 : fprintf (file, "\n");
8464 :
8465 285517 : any_var = true;
8466 : }
8467 :
8468 57133 : tree name;
8469 :
8470 57133 : if (gimple_in_ssa_p (fun))
8471 1861337 : FOR_EACH_SSA_NAME (ix, name, fun)
8472 : {
8473 1366367 : if (!SSA_NAME_VAR (name)
8474 : /* SSA name with decls without a name still get
8475 : dumped as _N, list those explicitly as well even
8476 : though we've dumped the decl declaration as D.xxx
8477 : above. */
8478 788848 : || !SSA_NAME_IDENTIFIER (name))
8479 : {
8480 580358 : fprintf (file, " ");
8481 580358 : print_generic_expr (file, TREE_TYPE (name), flags);
8482 580358 : fprintf (file, " ");
8483 580358 : print_generic_expr (file, name, flags);
8484 580358 : fprintf (file, ";\n");
8485 :
8486 580358 : any_var = true;
8487 : }
8488 : }
8489 : }
8490 :
8491 70385 : if (fun && fun->decl == fndecl
8492 70385 : && fun->cfg
8493 56849 : && basic_block_info_for_fn (fun))
8494 : {
8495 : /* If the CFG has been built, emit a CFG-based dump. */
8496 56849 : if (!ignore_topmost_bind)
8497 0 : fprintf (file, "{\n");
8498 :
8499 56849 : if (any_var && n_basic_blocks_for_fn (fun))
8500 49465 : fprintf (file, "\n");
8501 :
8502 340503 : FOR_EACH_BB_FN (bb, fun)
8503 283654 : dump_bb (file, bb, 2, flags);
8504 :
8505 56849 : fprintf (file, "}\n");
8506 : }
8507 13536 : else if (fun && (fun->curr_properties & PROP_gimple_any))
8508 : {
8509 : /* The function is now in GIMPLE form but the CFG has not been
8510 : built yet. Emit the single sequence of GIMPLE statements
8511 : that make up its body. */
8512 6961 : gimple_seq body = gimple_body (fndecl);
8513 :
8514 6961 : if (gimple_seq_first_stmt (body)
8515 6961 : && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
8516 13650 : && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
8517 6677 : print_gimple_seq (file, body, 0, flags);
8518 : else
8519 : {
8520 284 : if (!ignore_topmost_bind)
8521 0 : fprintf (file, "{\n");
8522 :
8523 284 : if (any_var)
8524 189 : fprintf (file, "\n");
8525 :
8526 284 : print_gimple_seq (file, body, 2, flags);
8527 284 : fprintf (file, "}\n");
8528 : }
8529 : }
8530 : else
8531 : {
8532 6575 : int indent;
8533 :
8534 : /* Make a tree based dump. */
8535 6575 : chain = DECL_SAVED_TREE (fndecl);
8536 6575 : if (chain && TREE_CODE (chain) == BIND_EXPR)
8537 : {
8538 6575 : if (ignore_topmost_bind)
8539 : {
8540 0 : chain = BIND_EXPR_BODY (chain);
8541 0 : indent = 2;
8542 : }
8543 : else
8544 : indent = 0;
8545 : }
8546 : else
8547 : {
8548 0 : if (!ignore_topmost_bind)
8549 : {
8550 0 : fprintf (file, "{\n");
8551 : /* No topmost bind, pretend it's ignored for later. */
8552 0 : ignore_topmost_bind = true;
8553 : }
8554 : indent = 2;
8555 : }
8556 :
8557 6575 : if (any_var)
8558 0 : fprintf (file, "\n");
8559 :
8560 6575 : print_generic_stmt_indented (file, chain, flags, indent);
8561 6575 : if (ignore_topmost_bind)
8562 0 : fprintf (file, "}\n");
8563 : }
8564 :
8565 70385 : if (flags & TDF_ENUMERATE_LOCALS)
8566 39 : dump_enumerated_decls (file, flags);
8567 70385 : fprintf (file, "\n\n");
8568 :
8569 70385 : current_function_decl = old_current_fndecl;
8570 : }
8571 :
8572 : /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
8573 :
8574 : DEBUG_FUNCTION void
8575 0 : debug_function (tree fn, dump_flags_t flags)
8576 : {
8577 0 : dump_function_to_file (fn, stderr, flags);
8578 0 : }
8579 :
8580 :
8581 : /* Print on FILE the indexes for the predecessors of basic_block BB. */
8582 :
8583 : static void
8584 5354 : print_pred_bbs (FILE *file, basic_block bb)
8585 : {
8586 5354 : edge e;
8587 5354 : edge_iterator ei;
8588 :
8589 12123 : FOR_EACH_EDGE (e, ei, bb->preds)
8590 6769 : fprintf (file, "bb_%d ", e->src->index);
8591 5354 : }
8592 :
8593 :
8594 : /* Print on FILE the indexes for the successors of basic_block BB. */
8595 :
8596 : static void
8597 5354 : print_succ_bbs (FILE *file, basic_block bb)
8598 : {
8599 5354 : edge e;
8600 5354 : edge_iterator ei;
8601 :
8602 12218 : FOR_EACH_EDGE (e, ei, bb->succs)
8603 6864 : fprintf (file, "bb_%d ", e->dest->index);
8604 5354 : }
8605 :
8606 : /* Print to FILE the basic block BB following the VERBOSITY level. */
8607 :
8608 : void
8609 5354 : print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
8610 : {
8611 5354 : char *s_indent = (char *) alloca ((size_t) indent + 1);
8612 5354 : memset ((void *) s_indent, ' ', (size_t) indent);
8613 5354 : s_indent[indent] = '\0';
8614 :
8615 : /* Print basic_block's header. */
8616 5354 : if (verbosity >= 2)
8617 : {
8618 5354 : fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
8619 5354 : print_pred_bbs (file, bb);
8620 5354 : fprintf (file, "}, succs = {");
8621 5354 : print_succ_bbs (file, bb);
8622 5354 : fprintf (file, "})\n");
8623 : }
8624 :
8625 : /* Print basic_block's body. */
8626 5354 : if (verbosity >= 3)
8627 : {
8628 2933 : fprintf (file, "%s {\n", s_indent);
8629 2933 : dump_bb (file, bb, indent + 4, TDF_VOPS|TDF_MEMSYMS);
8630 2933 : fprintf (file, "%s }\n", s_indent);
8631 : }
8632 5354 : }
8633 :
8634 : /* Print loop information. */
8635 :
8636 : void
8637 10853 : print_loop_info (FILE *file, const class loop *loop, const char *prefix)
8638 : {
8639 10853 : if (loop->can_be_parallel)
8640 0 : fprintf (file, ", can_be_parallel");
8641 10853 : if (loop->warned_aggressive_loop_optimizations)
8642 0 : fprintf (file, ", warned_aggressive_loop_optimizations");
8643 10853 : if (loop->dont_vectorize)
8644 7 : fprintf (file, ", dont_vectorize");
8645 10853 : if (loop->force_vectorize)
8646 0 : fprintf (file, ", force_vectorize");
8647 10853 : if (loop->in_oacc_kernels_region)
8648 181 : fprintf (file, ", in_oacc_kernels_region");
8649 10853 : if (loop->finite_p)
8650 2145 : fprintf (file, ", finite_p");
8651 10853 : if (loop->unroll)
8652 68 : fprintf (file, "\n%sunroll %d", prefix, loop->unroll);
8653 10853 : if (loop->nb_iterations)
8654 : {
8655 44 : fprintf (file, "\n%sniter ", prefix);
8656 44 : print_generic_expr (file, loop->nb_iterations);
8657 : }
8658 :
8659 10853 : if (loop->any_upper_bound)
8660 : {
8661 2096 : fprintf (file, "\n%supper_bound ", prefix);
8662 2096 : print_decu (loop->nb_iterations_upper_bound, file);
8663 : }
8664 10853 : if (loop->any_likely_upper_bound)
8665 : {
8666 2096 : fprintf (file, "\n%slikely_upper_bound ", prefix);
8667 2096 : print_decu (loop->nb_iterations_likely_upper_bound, file);
8668 : }
8669 :
8670 10853 : if (loop->any_estimate)
8671 : {
8672 949 : fprintf (file, "\n%sestimate ", prefix);
8673 949 : print_decu (loop->nb_iterations_estimate, file);
8674 : }
8675 10853 : bool reliable;
8676 10853 : sreal iterations;
8677 10853 : if (loop->num && expected_loop_iterations_by_profile (loop, &iterations, &reliable))
8678 : {
8679 5489 : fprintf (file, "\n%siterations by profile: %f (%s%s) entry count:", prefix,
8680 : iterations.to_double (), reliable ? "reliable" : "unreliable",
8681 2516 : maybe_flat_loop_profile (loop) ? ", maybe flat" : "");
8682 2516 : loop_count_in (loop).dump (file, cfun);
8683 : }
8684 :
8685 10853 : }
8686 :
8687 : static void print_loop_and_siblings (FILE *, class loop *, int, int);
8688 :
8689 : /* Pretty print LOOP on FILE, indented INDENT spaces. Following
8690 : VERBOSITY level this outputs the contents of the loop, or just its
8691 : structure. */
8692 :
8693 : static void
8694 1268 : print_loop (FILE *file, class loop *loop, int indent, int verbosity)
8695 : {
8696 1268 : char *s_indent;
8697 1268 : basic_block bb;
8698 :
8699 1268 : if (loop == NULL)
8700 : return;
8701 :
8702 1268 : s_indent = (char *) alloca ((size_t) indent + 1);
8703 1268 : memset ((void *) s_indent, ' ', (size_t) indent);
8704 1268 : s_indent[indent] = '\0';
8705 :
8706 : /* Print loop's header. */
8707 1268 : fprintf (file, "%sloop_%d (", s_indent, loop->num);
8708 1268 : if (loop->header)
8709 1268 : fprintf (file, "header = %d", loop->header->index);
8710 : else
8711 : {
8712 0 : fprintf (file, "deleted)\n");
8713 0 : return;
8714 : }
8715 1268 : if (loop->latch)
8716 1268 : fprintf (file, ", latch = %d", loop->latch->index);
8717 : else
8718 0 : fprintf (file, ", multiple latches");
8719 1268 : print_loop_info (file, loop, s_indent);
8720 1268 : fprintf (file, ")\n");
8721 :
8722 : /* Print loop's body. */
8723 1268 : if (verbosity >= 1)
8724 : {
8725 1268 : fprintf (file, "%s{\n", s_indent);
8726 21592 : FOR_EACH_BB_FN (bb, cfun)
8727 20324 : if (bb->loop_father == loop)
8728 4842 : print_loops_bb (file, bb, indent, verbosity);
8729 :
8730 1268 : print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
8731 1268 : fprintf (file, "%s}\n", s_indent);
8732 : }
8733 : }
8734 :
8735 : /* Print the LOOP and its sibling loops on FILE, indented INDENT
8736 : spaces. Following VERBOSITY level this outputs the contents of the
8737 : loop, or just its structure. */
8738 :
8739 : static void
8740 1268 : print_loop_and_siblings (FILE *file, class loop *loop, int indent,
8741 : int verbosity)
8742 : {
8743 2158 : if (loop == NULL)
8744 1268 : return;
8745 :
8746 1268 : print_loop (file, loop, indent, verbosity);
8747 378 : print_loop_and_siblings (file, loop->next, indent, verbosity);
8748 : }
8749 :
8750 : /* Follow a CFG edge from the entry point of the program, and on entry
8751 : of a loop, pretty print the loop structure on FILE. */
8752 :
8753 : void
8754 378 : print_loops (FILE *file, int verbosity)
8755 : {
8756 378 : basic_block bb;
8757 :
8758 378 : bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
8759 378 : fprintf (file, "\nLoops in function: %s\n", current_function_name ());
8760 378 : if (bb && bb->loop_father)
8761 756 : print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
8762 378 : }
8763 :
8764 : /* Dump a loop. */
8765 :
8766 : DEBUG_FUNCTION void
8767 0 : debug (class loop &ref)
8768 : {
8769 0 : print_loop (stderr, &ref, 0, /*verbosity*/0);
8770 0 : }
8771 :
8772 : DEBUG_FUNCTION void
8773 0 : debug (class loop *ptr)
8774 : {
8775 0 : if (ptr)
8776 0 : debug (*ptr);
8777 : else
8778 0 : fprintf (stderr, "<nil>\n");
8779 0 : }
8780 :
8781 : /* Dump a loop verbosely. */
8782 :
8783 : DEBUG_FUNCTION void
8784 0 : debug_verbose (class loop &ref)
8785 : {
8786 0 : print_loop (stderr, &ref, 0, /*verbosity*/3);
8787 0 : }
8788 :
8789 : DEBUG_FUNCTION void
8790 0 : debug_verbose (class loop *ptr)
8791 : {
8792 0 : if (ptr)
8793 0 : debug (*ptr);
8794 : else
8795 0 : fprintf (stderr, "<nil>\n");
8796 0 : }
8797 :
8798 :
8799 : /* Debugging loops structure at tree level, at some VERBOSITY level. */
8800 :
8801 : DEBUG_FUNCTION void
8802 0 : debug_loops (int verbosity)
8803 : {
8804 0 : print_loops (stderr, verbosity);
8805 0 : }
8806 :
8807 : /* Print on stderr the code of LOOP, at some VERBOSITY level. */
8808 :
8809 : DEBUG_FUNCTION void
8810 0 : debug_loop (class loop *loop, int verbosity)
8811 : {
8812 0 : print_loop (stderr, loop, 0, verbosity);
8813 0 : }
8814 :
8815 : /* Print on stderr the code of loop number NUM, at some VERBOSITY
8816 : level. */
8817 :
8818 : DEBUG_FUNCTION void
8819 0 : debug_loop_num (unsigned num, int verbosity)
8820 : {
8821 0 : debug_loop (get_loop (cfun, num), verbosity);
8822 0 : }
8823 :
8824 : /* Return true if BB ends with a call, possibly followed by some
8825 : instructions that must stay with the call. Return false,
8826 : otherwise. */
8827 :
8828 : static bool
8829 3 : gimple_block_ends_with_call_p (basic_block bb)
8830 : {
8831 3 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
8832 3 : return !gsi_end_p (gsi) && is_gimple_call (gsi_stmt (gsi));
8833 : }
8834 :
8835 :
8836 : /* Return true if BB ends with a conditional branch. Return false,
8837 : otherwise. */
8838 :
8839 : static bool
8840 1804 : gimple_block_ends_with_condjump_p (const_basic_block bb)
8841 : {
8842 3608 : return safe_is_a <gcond *> (*gsi_last_bb (const_cast <basic_block> (bb)));
8843 : }
8844 :
8845 :
8846 : /* Return true if statement T may terminate execution of BB in ways not
8847 : explicitly represtented in the CFG. */
8848 :
8849 : bool
8850 461280393 : stmt_can_terminate_bb_p (gimple *t)
8851 : {
8852 461280393 : tree fndecl = NULL_TREE;
8853 461280393 : int call_flags = 0;
8854 :
8855 : /* Eh exception not handled internally terminates execution of the whole
8856 : function. */
8857 461280393 : if (stmt_can_throw_external (cfun, t))
8858 : return true;
8859 :
8860 : /* NORETURN and LONGJMP calls already have an edge to exit.
8861 : CONST and PURE calls do not need one.
8862 : We don't currently check for CONST and PURE here, although
8863 : it would be a good idea, because those attributes are
8864 : figured out from the RTL in mark_constant_function, and
8865 : the counter incrementation code from -fprofile-arcs
8866 : leads to different results from -fbranch-probabilities. */
8867 453188755 : if (is_gimple_call (t))
8868 : {
8869 15010197 : fndecl = gimple_call_fndecl (t);
8870 15010197 : call_flags = gimple_call_flags (t);
8871 : }
8872 :
8873 453188755 : if (is_gimple_call (t)
8874 15010197 : && fndecl
8875 12988573 : && fndecl_built_in_p (fndecl)
8876 5320051 : && (call_flags & ECF_NOTHROW)
8877 4629089 : && !(call_flags & ECF_RETURNS_TWICE)
8878 : /* fork() doesn't really return twice, but the effect of
8879 : wrapping it in __gcov_fork() which calls __gcov_dump() and
8880 : __gcov_reset() and clears the counters before forking has the same
8881 : effect as returning twice. Force a fake edge. */
8882 457817477 : && !fndecl_built_in_p (fndecl, BUILT_IN_FORK))
8883 : return false;
8884 :
8885 448560248 : if (is_gimple_call (t))
8886 : {
8887 10381690 : edge_iterator ei;
8888 10381690 : edge e;
8889 10381690 : basic_block bb;
8890 :
8891 10381690 : if (call_flags & (ECF_PURE | ECF_CONST)
8892 2152389 : && !(call_flags & ECF_LOOPING_CONST_OR_PURE))
8893 9186783 : return false;
8894 :
8895 : /* Function call may do longjmp, terminate program or do other things.
8896 : Special case noreturn that have non-abnormal edges out as in this case
8897 : the fact is sufficiently represented by lack of edges out of T. */
8898 8361308 : if (!(call_flags & ECF_NORETURN))
8899 : return true;
8900 :
8901 1294025 : bb = gimple_bb (t);
8902 2140265 : FOR_EACH_EDGE (e, ei, bb->succs)
8903 945358 : if ((e->flags & EDGE_FAKE) == 0)
8904 : return true;
8905 : }
8906 :
8907 439373465 : if (gasm *asm_stmt = dyn_cast <gasm *> (t))
8908 315150 : if (gimple_asm_volatile_p (asm_stmt) || gimple_asm_basic_p (asm_stmt))
8909 : return true;
8910 :
8911 : return false;
8912 : }
8913 :
8914 :
8915 : /* Add fake edges to the function exit for any non constant and non
8916 : noreturn calls (or noreturn calls with EH/abnormal edges),
8917 : volatile inline assembly in the bitmap of blocks specified by BLOCKS
8918 : or to the whole CFG if BLOCKS is zero. Return the number of blocks
8919 : that were split.
8920 :
8921 : The goal is to expose cases in which entering a basic block does
8922 : not imply that all subsequent instructions must be executed. */
8923 :
8924 : static int
8925 3023 : gimple_flow_call_edges_add (sbitmap blocks)
8926 : {
8927 3023 : int i;
8928 3023 : int blocks_split = 0;
8929 3023 : int last_bb = last_basic_block_for_fn (cfun);
8930 3023 : bool check_last_block = false;
8931 :
8932 3023 : if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
8933 : return 0;
8934 :
8935 3023 : if (! blocks)
8936 : check_last_block = true;
8937 : else
8938 0 : check_last_block = bitmap_bit_p (blocks,
8939 0 : EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
8940 :
8941 : /* In the last basic block, before epilogue generation, there will be
8942 : a fallthru edge to EXIT. Special care is required if the last insn
8943 : of the last basic block is a call because make_edge folds duplicate
8944 : edges, which would result in the fallthru edge also being marked
8945 : fake, which would result in the fallthru edge being removed by
8946 : remove_fake_edges, which would result in an invalid CFG.
8947 :
8948 : Moreover, we can't elide the outgoing fake edge, since the block
8949 : profiler needs to take this into account in order to solve the minimal
8950 : spanning tree in the case that the call doesn't return.
8951 :
8952 : Handle this by adding a dummy instruction in a new last basic block. */
8953 0 : if (check_last_block)
8954 : {
8955 3023 : basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
8956 3023 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
8957 3023 : gimple *t = NULL;
8958 :
8959 3023 : if (!gsi_end_p (gsi))
8960 3011 : t = gsi_stmt (gsi);
8961 :
8962 3011 : if (t && stmt_can_terminate_bb_p (t))
8963 : {
8964 202 : edge e;
8965 :
8966 202 : e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
8967 202 : if (e)
8968 : {
8969 0 : gsi_insert_on_edge (e, gimple_build_nop ());
8970 0 : gsi_commit_edge_inserts ();
8971 : }
8972 : }
8973 : }
8974 :
8975 : /* Now add fake edges to the function exit for any non constant
8976 : calls since there is no way that we can determine if they will
8977 : return or not... */
8978 23158 : for (i = 0; i < last_bb; i++)
8979 : {
8980 20135 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
8981 20135 : gimple_stmt_iterator gsi;
8982 20135 : gimple *stmt, *last_stmt;
8983 :
8984 20135 : if (!bb)
8985 6 : continue;
8986 :
8987 20129 : if (blocks && !bitmap_bit_p (blocks, i))
8988 0 : continue;
8989 :
8990 20129 : gsi = gsi_last_nondebug_bb (bb);
8991 20129 : if (!gsi_end_p (gsi))
8992 : {
8993 : last_stmt = gsi_stmt (gsi);
8994 31048 : do
8995 : {
8996 31048 : stmt = gsi_stmt (gsi);
8997 31048 : if (stmt_can_terminate_bb_p (stmt))
8998 : {
8999 4969 : edge e;
9000 :
9001 : /* The handling above of the final block before the
9002 : epilogue should be enough to verify that there is
9003 : no edge to the exit block in CFG already.
9004 : Calling make_edge in such case would cause us to
9005 : mark that edge as fake and remove it later. */
9006 4969 : if (flag_checking && stmt == last_stmt)
9007 : {
9008 1647 : e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
9009 1647 : gcc_assert (e == NULL);
9010 : }
9011 :
9012 : /* Note that the following may create a new basic block
9013 : and renumber the existing basic blocks. */
9014 4969 : if (stmt != last_stmt)
9015 : {
9016 3322 : e = split_block (bb, stmt);
9017 3322 : if (e)
9018 3322 : blocks_split++;
9019 : }
9020 4969 : e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
9021 4969 : e->probability = profile_probability::guessed_never ();
9022 : }
9023 31048 : gsi_prev (&gsi);
9024 : }
9025 31048 : while (!gsi_end_p (gsi));
9026 : }
9027 : }
9028 :
9029 3023 : if (blocks_split)
9030 1252 : checking_verify_flow_info ();
9031 :
9032 : return blocks_split;
9033 : }
9034 :
9035 : /* Removes edge E and all the blocks dominated by it, and updates dominance
9036 : information. The IL in E->src needs to be updated separately.
9037 : If dominance info is not available, only the edge E is removed.*/
9038 :
9039 : void
9040 3860308 : remove_edge_and_dominated_blocks (edge e)
9041 : {
9042 3860308 : vec<basic_block> bbs_to_fix_dom = vNULL;
9043 3860308 : edge f;
9044 3860308 : edge_iterator ei;
9045 3860308 : bool none_removed = false;
9046 3860308 : unsigned i;
9047 3860308 : basic_block bb, dbb;
9048 3860308 : bitmap_iterator bi;
9049 :
9050 : /* If we are removing a path inside a non-root loop that may change
9051 : loop ownership of blocks or remove loops. Mark loops for fixup. */
9052 3860308 : class loop *src_loop = e->src->loop_father;
9053 3860308 : if (current_loops
9054 3596308 : && loop_outer (src_loop) != NULL
9055 4567958 : && src_loop == e->dest->loop_father)
9056 : {
9057 297197 : loops_state_set (LOOPS_NEED_FIXUP);
9058 : /* If we are removing a backedge clear the number of iterations
9059 : and estimates. */
9060 297197 : class loop *dest_loop = e->dest->loop_father;
9061 297197 : if (e->dest == src_loop->header
9062 297197 : || (e->dest == dest_loop->header
9063 0 : && flow_loop_nested_p (dest_loop, src_loop)))
9064 : {
9065 1846 : free_numbers_of_iterations_estimates (dest_loop);
9066 : /* If we removed the last backedge mark the loop for removal. */
9067 5521 : FOR_EACH_EDGE (f, ei, dest_loop->header->preds)
9068 3700 : if (f != e
9069 3700 : && (f->src->loop_father == dest_loop
9070 1846 : || flow_loop_nested_p (dest_loop, f->src->loop_father)))
9071 : break;
9072 1846 : if (!f)
9073 1821 : mark_loop_for_removal (dest_loop);
9074 : }
9075 : }
9076 :
9077 3860308 : if (!dom_info_available_p (CDI_DOMINATORS))
9078 : {
9079 3664227 : remove_edge (e);
9080 7328454 : return;
9081 : }
9082 :
9083 : /* No updating is needed for edges to exit. */
9084 196081 : if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
9085 : {
9086 0 : if (cfgcleanup_altered_bbs)
9087 0 : bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
9088 0 : remove_edge (e);
9089 0 : return;
9090 : }
9091 :
9092 : /* First, we find the basic blocks to remove. If E->dest has a predecessor
9093 : that is not dominated by E->dest, then this set is empty. Otherwise,
9094 : all the basic blocks dominated by E->dest are removed.
9095 :
9096 : Also, to DF_IDOM we store the immediate dominators of the blocks in
9097 : the dominance frontier of E (i.e., of the successors of the
9098 : removed blocks, if there are any, and of E->dest otherwise). */
9099 209520 : FOR_EACH_EDGE (f, ei, e->dest->preds)
9100 : {
9101 204425 : if (f == e)
9102 13388 : continue;
9103 :
9104 191037 : if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
9105 : {
9106 : none_removed = true;
9107 : break;
9108 : }
9109 : }
9110 :
9111 196081 : auto_bitmap df, df_idom;
9112 196081 : auto_vec<basic_block> bbs_to_remove;
9113 196081 : if (none_removed)
9114 190986 : bitmap_set_bit (df_idom,
9115 190986 : get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
9116 : else
9117 : {
9118 5095 : bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
9119 11547 : FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
9120 : {
9121 13044 : FOR_EACH_EDGE (f, ei, bb->succs)
9122 : {
9123 6592 : if (f->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
9124 6590 : bitmap_set_bit (df, f->dest->index);
9125 : }
9126 : }
9127 11547 : FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
9128 6452 : bitmap_clear_bit (df, bb->index);
9129 :
9130 9912 : EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
9131 : {
9132 4817 : bb = BASIC_BLOCK_FOR_FN (cfun, i);
9133 4817 : bitmap_set_bit (df_idom,
9134 4817 : get_immediate_dominator (CDI_DOMINATORS, bb)->index);
9135 : }
9136 : }
9137 :
9138 196081 : if (cfgcleanup_altered_bbs)
9139 : {
9140 : /* Record the set of the altered basic blocks. */
9141 13171 : bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
9142 13171 : bitmap_ior_into (cfgcleanup_altered_bbs, df);
9143 : }
9144 :
9145 : /* Remove E and the cancelled blocks. */
9146 196081 : if (none_removed)
9147 190986 : remove_edge (e);
9148 : else
9149 : {
9150 : /* Walk backwards so as to get a chance to substitute all
9151 : released DEFs into debug stmts. See
9152 : eliminate_unnecessary_stmts() in tree-ssa-dce.cc for more
9153 : details. */
9154 16642 : for (i = bbs_to_remove.length (); i-- > 0; )
9155 6452 : delete_basic_block (bbs_to_remove[i]);
9156 : }
9157 :
9158 : /* Update the dominance information. The immediate dominator may change only
9159 : for blocks whose immediate dominator belongs to DF_IDOM:
9160 :
9161 : Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
9162 : removal. Let Z the arbitrary block such that idom(Z) = Y and
9163 : Z dominates X after the removal. Before removal, there exists a path P
9164 : from Y to X that avoids Z. Let F be the last edge on P that is
9165 : removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
9166 : dominates W, and because of P, Z does not dominate W), and W belongs to
9167 : the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
9168 391211 : EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
9169 : {
9170 195130 : bb = BASIC_BLOCK_FOR_FN (cfun, i);
9171 195130 : for (dbb = first_dom_son (CDI_DOMINATORS, bb);
9172 689575 : dbb;
9173 494445 : dbb = next_dom_son (CDI_DOMINATORS, dbb))
9174 494445 : bbs_to_fix_dom.safe_push (dbb);
9175 : }
9176 :
9177 196081 : iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
9178 :
9179 196081 : bbs_to_fix_dom.release ();
9180 196081 : }
9181 :
9182 : /* Purge dead EH edges from basic block BB. */
9183 :
9184 : bool
9185 456912467 : gimple_purge_dead_eh_edges (basic_block bb)
9186 : {
9187 456912467 : bool changed = false;
9188 456912467 : edge e;
9189 456912467 : edge_iterator ei;
9190 456912467 : gimple *stmt = *gsi_last_bb (bb);
9191 :
9192 456912467 : if (stmt && stmt_can_throw_internal (cfun, stmt))
9193 : return false;
9194 :
9195 985629773 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
9196 : {
9197 573138461 : if (e->flags & EDGE_EH)
9198 : {
9199 956900 : remove_edge_and_dominated_blocks (e);
9200 956900 : changed = true;
9201 : }
9202 : else
9203 572181561 : ei_next (&ei);
9204 : }
9205 :
9206 : return changed;
9207 : }
9208 :
9209 : /* Purge dead EH edges from basic block listed in BLOCKS. */
9210 :
9211 : bool
9212 7459104 : gimple_purge_all_dead_eh_edges (const_bitmap blocks)
9213 : {
9214 7459104 : bool changed = false;
9215 7459104 : unsigned i;
9216 7459104 : bitmap_iterator bi;
9217 :
9218 7647582 : EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
9219 : {
9220 188478 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
9221 :
9222 : /* Earlier gimple_purge_dead_eh_edges could have removed
9223 : this basic block already. */
9224 188478 : gcc_assert (bb || changed);
9225 188478 : if (bb != NULL)
9226 188445 : changed |= gimple_purge_dead_eh_edges (bb);
9227 : }
9228 :
9229 7459104 : return changed;
9230 : }
9231 :
9232 : /* Purge dead abnormal call edges from basic block BB. */
9233 :
9234 : bool
9235 86652563 : gimple_purge_dead_abnormal_call_edges (basic_block bb)
9236 : {
9237 86652563 : bool changed = false;
9238 86652563 : edge e;
9239 86652563 : edge_iterator ei;
9240 86652563 : gimple *stmt = *gsi_last_bb (bb);
9241 :
9242 86652563 : if (stmt && stmt_can_make_abnormal_goto (stmt))
9243 : return false;
9244 :
9245 204519507 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
9246 : {
9247 117915331 : if (e->flags & EDGE_ABNORMAL)
9248 : {
9249 1420 : if (e->flags & EDGE_FALLTHRU)
9250 0 : e->flags &= ~EDGE_ABNORMAL;
9251 : else
9252 1420 : remove_edge_and_dominated_blocks (e);
9253 117915331 : changed = true;
9254 : }
9255 : else
9256 117913911 : ei_next (&ei);
9257 : }
9258 :
9259 : return changed;
9260 : }
9261 :
9262 : /* Purge dead abnormal call edges from basic block listed in BLOCKS. */
9263 :
9264 : bool
9265 7399757 : gimple_purge_all_dead_abnormal_call_edges (const_bitmap blocks)
9266 : {
9267 7399757 : bool changed = false;
9268 7399757 : unsigned i;
9269 7399757 : bitmap_iterator bi;
9270 :
9271 7399777 : EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
9272 : {
9273 20 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
9274 :
9275 : /* Earlier gimple_purge_dead_abnormal_call_edges could have removed
9276 : this basic block already. */
9277 20 : gcc_assert (bb || changed);
9278 20 : if (bb != NULL)
9279 20 : changed |= gimple_purge_dead_abnormal_call_edges (bb);
9280 : }
9281 :
9282 7399757 : return changed;
9283 : }
9284 :
9285 : /* This function is called whenever a new edge is created or
9286 : redirected. */
9287 :
9288 : static void
9289 168283369 : gimple_execute_on_growing_pred (edge e)
9290 : {
9291 168283369 : basic_block bb = e->dest;
9292 :
9293 168283369 : if (!gimple_seq_empty_p (phi_nodes (bb)))
9294 32832646 : reserve_phi_args_for_new_edge (bb);
9295 168283369 : }
9296 :
9297 : /* This function is called immediately before edge E is removed from
9298 : the edge vector E->dest->preds. */
9299 :
9300 : static void
9301 138283644 : gimple_execute_on_shrinking_pred (edge e)
9302 : {
9303 138283644 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
9304 37122435 : remove_phi_args (e);
9305 138283644 : }
9306 :
9307 : /*---------------------------------------------------------------------------
9308 : Helper functions for Loop versioning
9309 : ---------------------------------------------------------------------------*/
9310 :
9311 : /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
9312 : of 'first'. Both of them are dominated by 'new_head' basic block. When
9313 : 'new_head' was created by 'second's incoming edge it received phi arguments
9314 : on the edge by split_edge(). Later, additional edge 'e' was created to
9315 : connect 'new_head' and 'first'. Now this routine adds phi args on this
9316 : additional edge 'e' that new_head to second edge received as part of edge
9317 : splitting. */
9318 :
9319 : static void
9320 36421 : gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
9321 : basic_block new_head, edge e)
9322 : {
9323 36421 : gphi *phi1, *phi2;
9324 36421 : gphi_iterator psi1, psi2;
9325 36421 : tree def;
9326 36421 : edge e2 = find_edge (new_head, second);
9327 :
9328 : /* Because NEW_HEAD has been created by splitting SECOND's incoming
9329 : edge, we should always have an edge from NEW_HEAD to SECOND. */
9330 36421 : gcc_assert (e2 != NULL);
9331 :
9332 : /* Browse all 'second' basic block phi nodes and add phi args to
9333 : edge 'e' for 'first' head. PHI args are always in correct order. */
9334 :
9335 36421 : for (psi2 = gsi_start_phis (second),
9336 36421 : psi1 = gsi_start_phis (first);
9337 141341 : !gsi_end_p (psi2) && !gsi_end_p (psi1);
9338 104920 : gsi_next (&psi2), gsi_next (&psi1))
9339 : {
9340 104920 : phi1 = psi1.phi ();
9341 104920 : phi2 = psi2.phi ();
9342 104920 : def = PHI_ARG_DEF (phi2, e2->dest_idx);
9343 104920 : add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
9344 : }
9345 36421 : }
9346 :
9347 :
9348 : /* Adds a if else statement to COND_BB with condition COND_EXPR.
9349 : SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
9350 : the destination of the ELSE part. */
9351 :
9352 : static void
9353 36421 : gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
9354 : basic_block second_head ATTRIBUTE_UNUSED,
9355 : basic_block cond_bb, void *cond_e)
9356 : {
9357 36421 : gimple_stmt_iterator gsi;
9358 36421 : gimple *new_cond_expr;
9359 36421 : tree cond_expr = (tree) cond_e;
9360 36421 : edge e0;
9361 :
9362 : /* Build new conditional expr */
9363 36421 : gsi = gsi_last_bb (cond_bb);
9364 :
9365 36421 : cond_expr = force_gimple_operand_gsi_1 (&gsi, cond_expr,
9366 : is_gimple_condexpr_for_cond,
9367 : NULL_TREE, false,
9368 : GSI_CONTINUE_LINKING);
9369 36421 : new_cond_expr = gimple_build_cond_from_tree (cond_expr,
9370 : NULL_TREE, NULL_TREE);
9371 :
9372 : /* Add new cond in cond_bb. */
9373 36421 : gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
9374 :
9375 : /* Adjust edges appropriately to connect new head with first head
9376 : as well as second head. */
9377 36421 : e0 = single_succ_edge (cond_bb);
9378 36421 : e0->flags &= ~EDGE_FALLTHRU;
9379 36421 : e0->flags |= EDGE_FALSE_VALUE;
9380 36421 : }
9381 :
9382 :
9383 : /* Do book-keeping of basic block BB for the profile consistency checker.
9384 : Store the counting in RECORD. */
9385 : static void
9386 0 : gimple_account_profile_record (basic_block bb,
9387 : struct profile_record *record)
9388 : {
9389 0 : gimple_stmt_iterator i;
9390 0 : for (i = gsi_start_nondebug_after_labels_bb (bb); !gsi_end_p (i);
9391 0 : gsi_next_nondebug (&i))
9392 : {
9393 0 : record->size
9394 0 : += estimate_num_insns (gsi_stmt (i), &eni_size_weights);
9395 0 : if (profile_info)
9396 : {
9397 0 : if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ().initialized_p ()
9398 0 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ().nonzero_p ()
9399 0 : && bb->count.ipa ().initialized_p ())
9400 0 : record->time
9401 0 : += estimate_num_insns (gsi_stmt (i),
9402 : &eni_time_weights)
9403 0 : * bb->count.ipa ().to_gcov_type ();
9404 : }
9405 0 : else if (bb->count.initialized_p ()
9406 0 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ())
9407 0 : record->time
9408 0 : += estimate_num_insns
9409 0 : (gsi_stmt (i),
9410 : &eni_time_weights)
9411 0 : * bb->count.to_sreal_scale
9412 0 : (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count).to_double ();
9413 : else
9414 0 : record->time
9415 0 : += estimate_num_insns (gsi_stmt (i), &eni_time_weights);
9416 : }
9417 0 : }
9418 :
9419 : const struct cfg_hooks gimple_cfg_hooks = {
9420 : IR_GIMPLE,
9421 : gimple_verify_flow_info,
9422 : gimple_dump_bb, /* dump_bb */
9423 : gimple_dump_bb_for_graph, /* dump_bb_for_graph */
9424 : gimple_dump_bb_as_sarif_properties,
9425 : create_bb, /* create_basic_block */
9426 : gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
9427 : gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
9428 : gimple_can_remove_branch_p, /* can_remove_branch_p */
9429 : remove_bb, /* delete_basic_block */
9430 : gimple_split_block, /* split_block */
9431 : gimple_move_block_after, /* move_block_after */
9432 : gimple_can_merge_blocks_p, /* can_merge_blocks_p */
9433 : gimple_merge_blocks, /* merge_blocks */
9434 : gimple_predict_edge, /* predict_edge */
9435 : gimple_predicted_by_p, /* predicted_by_p */
9436 : gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
9437 : gimple_duplicate_bb, /* duplicate_block */
9438 : gimple_split_edge, /* split_edge */
9439 : gimple_make_forwarder_block, /* make_forward_block */
9440 : NULL, /* tidy_fallthru_edge */
9441 : NULL, /* force_nonfallthru */
9442 : gimple_block_ends_with_call_p,/* block_ends_with_call_p */
9443 : gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
9444 : gimple_flow_call_edges_add, /* flow_call_edges_add */
9445 : gimple_execute_on_growing_pred, /* execute_on_growing_pred */
9446 : gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
9447 : gimple_duplicate_loop_body_to_header_edge, /* duplicate loop for trees */
9448 : gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
9449 : gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
9450 : extract_true_false_edges_from_block, /* extract_cond_bb_edges */
9451 : flush_pending_stmts, /* flush_pending_stmts */
9452 : gimple_empty_block_p, /* block_empty_p */
9453 : gimple_split_block_before_cond_jump, /* split_block_before_cond_jump */
9454 : gimple_account_profile_record,
9455 : };
9456 :
9457 :
9458 : /* Split all critical edges. Split some extra (not necessarily critical) edges
9459 : if FOR_EDGE_INSERTION_P is true. */
9460 :
9461 : unsigned int
9462 4336338 : split_critical_edges (bool for_edge_insertion_p /* = false */)
9463 : {
9464 4336338 : basic_block bb;
9465 4336338 : edge e;
9466 4336338 : edge_iterator ei;
9467 :
9468 : /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
9469 : expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
9470 : mappings around the calls to split_edge. */
9471 4336338 : start_recording_case_labels ();
9472 76306115 : FOR_ALL_BB_FN (bb, cfun)
9473 : {
9474 159758264 : FOR_EACH_EDGE (e, ei, bb->succs)
9475 : {
9476 87788487 : if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
9477 15108904 : split_edge (e);
9478 : /* PRE inserts statements to edges and expects that
9479 : since split_critical_edges was done beforehand, committing edge
9480 : insertions will not split more edges. In addition to critical
9481 : edges we must split edges that have multiple successors and
9482 : end by control flow statements, such as RESX.
9483 : Go ahead and split them too. This matches the logic in
9484 : gimple_find_edge_insert_loc. */
9485 72679583 : else if (for_edge_insertion_p
9486 56783558 : && (!single_pred_p (e->dest)
9487 31680057 : || !gimple_seq_empty_p (phi_nodes (e->dest))
9488 31429709 : || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
9489 28530495 : && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
9490 101209669 : && !(e->flags & EDGE_ABNORMAL))
9491 : {
9492 28513813 : gimple_stmt_iterator gsi;
9493 :
9494 28513813 : gsi = gsi_last_bb (e->src);
9495 28513813 : if (!gsi_end_p (gsi)
9496 14067547 : && stmt_ends_bb_p (gsi_stmt (gsi))
9497 31912149 : && (gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN
9498 213814 : && !gimple_call_builtin_p (gsi_stmt (gsi),
9499 : BUILT_IN_RETURN)))
9500 212763 : split_edge (e);
9501 : }
9502 : }
9503 : }
9504 4336338 : end_recording_case_labels ();
9505 4336338 : return 0;
9506 : }
9507 :
9508 : namespace {
9509 :
9510 : const pass_data pass_data_split_crit_edges =
9511 : {
9512 : GIMPLE_PASS, /* type */
9513 : "crited", /* name */
9514 : OPTGROUP_NONE, /* optinfo_flags */
9515 : TV_TREE_SPLIT_EDGES, /* tv_id */
9516 : PROP_cfg, /* properties_required */
9517 : PROP_no_crit_edges, /* properties_provided */
9518 : 0, /* properties_destroyed */
9519 : 0, /* todo_flags_start */
9520 : 0, /* todo_flags_finish */
9521 : };
9522 :
9523 : class pass_split_crit_edges : public gimple_opt_pass
9524 : {
9525 : public:
9526 605970 : pass_split_crit_edges (gcc::context *ctxt)
9527 1211940 : : gimple_opt_pass (pass_data_split_crit_edges, ctxt)
9528 : {}
9529 :
9530 : /* opt_pass methods: */
9531 1070725 : unsigned int execute (function *) final override
9532 : {
9533 1070725 : return split_critical_edges ();
9534 : }
9535 :
9536 302985 : opt_pass * clone () final override
9537 : {
9538 302985 : return new pass_split_crit_edges (m_ctxt);
9539 : }
9540 : }; // class pass_split_crit_edges
9541 :
9542 : } // anon namespace
9543 :
9544 : gimple_opt_pass *
9545 302985 : make_pass_split_crit_edges (gcc::context *ctxt)
9546 : {
9547 302985 : return new pass_split_crit_edges (ctxt);
9548 : }
9549 :
9550 :
9551 : /* Insert COND expression which is GIMPLE_COND after STMT
9552 : in basic block BB with appropriate basic block split
9553 : and creation of a new conditionally executed basic block.
9554 : Update profile so the new bb is visited with probability PROB.
9555 : Return created basic block. */
9556 : basic_block
9557 2225 : insert_cond_bb (basic_block bb, gimple *stmt, gimple *cond,
9558 : profile_probability prob)
9559 : {
9560 2225 : edge fall = split_block (bb, stmt);
9561 2225 : gimple_stmt_iterator iter = gsi_last_bb (bb);
9562 2225 : basic_block new_bb;
9563 :
9564 : /* Insert cond statement. */
9565 2225 : gcc_assert (gimple_code (cond) == GIMPLE_COND);
9566 2225 : if (gsi_end_p (iter))
9567 0 : gsi_insert_before (&iter, cond, GSI_CONTINUE_LINKING);
9568 : else
9569 2225 : gsi_insert_after (&iter, cond, GSI_CONTINUE_LINKING);
9570 :
9571 : /* Create conditionally executed block. */
9572 2225 : new_bb = create_empty_bb (bb);
9573 2225 : if (coverage_suppressed_p (bb))
9574 0 : suppress_coverage (new_bb);
9575 2225 : edge e = make_edge (bb, new_bb, EDGE_TRUE_VALUE);
9576 2225 : e->probability = prob;
9577 2225 : new_bb->count = e->count ();
9578 2225 : make_single_succ_edge (new_bb, fall->dest, EDGE_FALLTHRU);
9579 :
9580 : /* Fix edge for split bb. */
9581 2225 : fall->flags = EDGE_FALSE_VALUE;
9582 2225 : fall->probability -= e->probability;
9583 :
9584 : /* Update dominance info. */
9585 2225 : if (dom_info_available_p (CDI_DOMINATORS))
9586 : {
9587 2225 : set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
9588 2225 : set_immediate_dominator (CDI_DOMINATORS, fall->dest, bb);
9589 : }
9590 :
9591 : /* Update loop info. */
9592 2225 : if (current_loops)
9593 2225 : add_bb_to_loop (new_bb, bb->loop_father);
9594 :
9595 2225 : return new_bb;
9596 : }
9597 :
9598 :
9599 :
9600 : /* Given a basic block B which ends with a conditional and has
9601 : precisely two successors, determine which of the edges is taken if
9602 : the conditional is true and which is taken if the conditional is
9603 : false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
9604 :
9605 : void
9606 869145952 : extract_true_false_edges_from_block (basic_block b,
9607 : edge *true_edge,
9608 : edge *false_edge)
9609 : {
9610 869145952 : edge e = EDGE_SUCC (b, 0);
9611 :
9612 869145952 : if (e->flags & EDGE_TRUE_VALUE)
9613 : {
9614 827499406 : *true_edge = e;
9615 827499406 : *false_edge = EDGE_SUCC (b, 1);
9616 : }
9617 : else
9618 : {
9619 41646546 : *false_edge = e;
9620 41646546 : *true_edge = EDGE_SUCC (b, 1);
9621 : }
9622 869145952 : }
9623 :
9624 :
9625 : /* From a controlling predicate in the immediate dominator DOM of
9626 : PHIBLOCK determine the edges into PHIBLOCK that are chosen if the
9627 : predicate evaluates to true and false and store them to
9628 : *TRUE_CONTROLLED_EDGE and *FALSE_CONTROLLED_EDGE if
9629 : they are non-NULL. Returns true if the edges can be determined,
9630 : else return false. */
9631 :
9632 : bool
9633 142561 : extract_true_false_controlled_edges (basic_block dom, basic_block phiblock,
9634 : edge *true_controlled_edge,
9635 : edge *false_controlled_edge)
9636 : {
9637 142561 : basic_block bb = phiblock;
9638 142561 : edge true_edge, false_edge, tem;
9639 142561 : edge e0 = NULL, e1 = NULL;
9640 :
9641 : /* We have to verify that one edge into the PHI node is dominated
9642 : by the true edge of the predicate block and the other edge
9643 : dominated by the false edge. This ensures that the PHI argument
9644 : we are going to take is completely determined by the path we
9645 : take from the predicate block.
9646 : We can only use BB dominance checks below if the destination of
9647 : the true/false edges are dominated by their edge, thus only
9648 : have a single predecessor. */
9649 142561 : extract_true_false_edges_from_block (dom, &true_edge, &false_edge);
9650 142561 : tem = EDGE_PRED (bb, 0);
9651 142561 : if (tem == true_edge
9652 142561 : || (single_pred_p (true_edge->dest)
9653 103303 : && (tem->src == true_edge->dest
9654 63312 : || dominated_by_p (CDI_DOMINATORS,
9655 : tem->src, true_edge->dest))))
9656 : e0 = tem;
9657 58668 : else if (tem == false_edge
9658 58668 : || (single_pred_p (false_edge->dest)
9659 47720 : && (tem->src == false_edge->dest
9660 35058 : || dominated_by_p (CDI_DOMINATORS,
9661 : tem->src, false_edge->dest))))
9662 : e1 = tem;
9663 : else
9664 : return false;
9665 117939 : tem = EDGE_PRED (bb, 1);
9666 117939 : if (tem == true_edge
9667 117939 : || (single_pred_p (true_edge->dest)
9668 83045 : && (tem->src == true_edge->dest
9669 65400 : || dominated_by_p (CDI_DOMINATORS,
9670 : tem->src, true_edge->dest))))
9671 : e0 = tem;
9672 87183 : else if (tem == false_edge
9673 87183 : || (single_pred_p (false_edge->dest)
9674 69243 : && (tem->src == false_edge->dest
9675 29342 : || dominated_by_p (CDI_DOMINATORS,
9676 : tem->src, false_edge->dest))))
9677 : e1 = tem;
9678 : else
9679 : return false;
9680 106919 : if (!e0 || !e1)
9681 : return false;
9682 :
9683 106919 : if (true_controlled_edge)
9684 106919 : *true_controlled_edge = e0;
9685 106919 : if (false_controlled_edge)
9686 106919 : *false_controlled_edge = e1;
9687 :
9688 : return true;
9689 : }
9690 :
9691 : /* Generate a range test LHS CODE RHS that determines whether INDEX is in the
9692 : range [low, high]. Place associated stmts before *GSI. */
9693 :
9694 : void
9695 4837 : generate_range_test (basic_block bb, tree index, tree low, tree high,
9696 : tree *lhs, tree *rhs)
9697 : {
9698 4837 : tree type = TREE_TYPE (index);
9699 4837 : tree utype = range_check_type (type);
9700 :
9701 4837 : low = fold_convert (utype, low);
9702 4837 : high = fold_convert (utype, high);
9703 :
9704 4837 : gimple_seq seq = NULL;
9705 4837 : index = gimple_convert (&seq, utype, index);
9706 4837 : *lhs = gimple_build (&seq, MINUS_EXPR, utype, index, low);
9707 4837 : *rhs = const_binop (MINUS_EXPR, utype, high, low);
9708 :
9709 4837 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
9710 4837 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
9711 4837 : }
9712 :
9713 : /* Return the basic block that belongs to label numbered INDEX
9714 : of a switch statement. */
9715 :
9716 : basic_block
9717 65878464 : gimple_switch_label_bb (function *ifun, gswitch *gs, unsigned index)
9718 : {
9719 65878464 : return label_to_block (ifun, CASE_LABEL (gimple_switch_label (gs, index)));
9720 : }
9721 :
9722 : /* Return the default basic block of a switch statement. */
9723 :
9724 : basic_block
9725 357116 : gimple_switch_default_bb (function *ifun, gswitch *gs)
9726 : {
9727 357116 : return gimple_switch_label_bb (ifun, gs, 0);
9728 : }
9729 :
9730 : /* Return the edge that belongs to label numbered INDEX
9731 : of a switch statement. */
9732 :
9733 : edge
9734 1650721 : gimple_switch_edge (function *ifun, gswitch *gs, unsigned index)
9735 : {
9736 1650721 : return find_edge (gimple_bb (gs), gimple_switch_label_bb (ifun, gs, index));
9737 : }
9738 :
9739 : /* Return the default edge of a switch statement. */
9740 :
9741 : edge
9742 210890 : gimple_switch_default_edge (function *ifun, gswitch *gs)
9743 : {
9744 210890 : return gimple_switch_edge (ifun, gs, 0);
9745 : }
9746 :
9747 : /* Return true if the only executable statement in BB is a GIMPLE_COND. */
9748 :
9749 : bool
9750 15584 : cond_only_block_p (basic_block bb)
9751 : {
9752 : /* BB must have no executable statements. */
9753 15584 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
9754 15584 : if (phi_nodes (bb))
9755 : return false;
9756 35044 : while (!gsi_end_p (gsi))
9757 : {
9758 20265 : gimple *stmt = gsi_stmt (gsi);
9759 20265 : if (is_gimple_debug (stmt))
9760 : ;
9761 15584 : else if (gimple_code (stmt) == GIMPLE_NOP
9762 : || gimple_code (stmt) == GIMPLE_PREDICT
9763 : || gimple_code (stmt) == GIMPLE_COND)
9764 : ;
9765 : else
9766 : return false;
9767 19460 : gsi_next (&gsi);
9768 : }
9769 : return true;
9770 : }
9771 :
9772 :
9773 : /* Emit return warnings. */
9774 :
9775 : namespace {
9776 :
9777 : const pass_data pass_data_warn_function_return =
9778 : {
9779 : GIMPLE_PASS, /* type */
9780 : "*warn_function_return", /* name */
9781 : OPTGROUP_NONE, /* optinfo_flags */
9782 : TV_NONE, /* tv_id */
9783 : PROP_cfg, /* properties_required */
9784 : 0, /* properties_provided */
9785 : 0, /* properties_destroyed */
9786 : 0, /* todo_flags_start */
9787 : 0, /* todo_flags_finish */
9788 : };
9789 :
9790 : class pass_warn_function_return : public gimple_opt_pass
9791 : {
9792 : public:
9793 302985 : pass_warn_function_return (gcc::context *ctxt)
9794 605970 : : gimple_opt_pass (pass_data_warn_function_return, ctxt)
9795 : {}
9796 :
9797 : /* opt_pass methods: */
9798 : unsigned int execute (function *) final override;
9799 :
9800 : }; // class pass_warn_function_return
9801 :
9802 : unsigned int
9803 3019888 : pass_warn_function_return::execute (function *fun)
9804 : {
9805 3019888 : location_t location;
9806 3019888 : gimple *last;
9807 3019888 : edge e;
9808 3019888 : edge_iterator ei;
9809 :
9810 3019888 : if (!targetm.warn_func_return (fun->decl))
9811 : return 0;
9812 :
9813 : /* If we have a path to EXIT, then we do return. */
9814 3019814 : if (TREE_THIS_VOLATILE (fun->decl)
9815 3019814 : && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0)
9816 : {
9817 90 : location = UNKNOWN_LOCATION;
9818 90 : for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (fun)->preds);
9819 180 : (e = ei_safe_edge (ei)); )
9820 : {
9821 90 : last = *gsi_last_bb (e->src);
9822 : /* Warn about __builtin_return .*/
9823 90 : if (gimple_call_builtin_p (last, BUILT_IN_RETURN)
9824 90 : && location == UNKNOWN_LOCATION)
9825 : {
9826 1 : location = LOCATION_LOCUS (gimple_location (last));
9827 1 : ei_next (&ei);
9828 : }
9829 : /* Replace return stmts in noreturn functions
9830 : with __builtin_unreachable () call. */
9831 89 : else if (gimple_code (last) == GIMPLE_RETURN)
9832 : {
9833 89 : location_t loc = gimple_location (last);
9834 89 : if (location == UNKNOWN_LOCATION)
9835 89 : location = LOCATION_LOCUS (loc);
9836 89 : gimple *new_stmt = gimple_build_builtin_unreachable (loc);
9837 89 : gimple_stmt_iterator gsi = gsi_for_stmt (last);
9838 89 : gsi_replace (&gsi, new_stmt, true);
9839 89 : remove_edge (e);
9840 : }
9841 : else
9842 0 : ei_next (&ei);
9843 : }
9844 90 : if (location == UNKNOWN_LOCATION)
9845 2 : location = cfun->function_end_locus;
9846 90 : warning_at (location, 0, "%<noreturn%> function does return");
9847 : }
9848 :
9849 : /* If we see "return;" in some basic block, then we do reach the end
9850 : without returning a value. */
9851 3019724 : else if (warn_return_type > 0
9852 2019996 : && !warning_suppressed_p (fun->decl, OPT_Wreturn_type)
9853 5036491 : && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fun->decl))))
9854 : {
9855 2135916 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
9856 : {
9857 3196447 : greturn *return_stmt = dyn_cast <greturn *> (*gsi_last_bb (e->src));
9858 1065397 : if (return_stmt
9859 1065397 : && gimple_return_retval (return_stmt) == NULL
9860 37 : && !warning_suppressed_p (return_stmt, OPT_Wreturn_type))
9861 : {
9862 16 : location = gimple_location (return_stmt);
9863 16 : if (LOCATION_LOCUS (location) == UNKNOWN_LOCATION)
9864 0 : location = fun->function_end_locus;
9865 16 : if (warning_at (location, OPT_Wreturn_type,
9866 : "control reaches end of non-void function"))
9867 14 : suppress_warning (fun->decl, OPT_Wreturn_type);
9868 : break;
9869 : }
9870 : }
9871 : /* The C++ FE turns fallthrough from the end of non-void function
9872 : into __builtin_unreachable () call with BUILTINS_LOCATION.
9873 : Recognize those as well as calls from ubsan_instrument_return. */
9874 1070399 : basic_block bb;
9875 1070399 : if (!warning_suppressed_p (fun->decl, OPT_Wreturn_type))
9876 5052794 : FOR_EACH_BB_FN (bb, fun)
9877 3982943 : if (EDGE_COUNT (bb->succs) == 0)
9878 : {
9879 193418 : gimple *last = *gsi_last_bb (bb);
9880 193418 : const enum built_in_function ubsan_missing_ret
9881 : = BUILT_IN_UBSAN_HANDLE_MISSING_RETURN;
9882 193418 : if (last
9883 193418 : && ((LOCATION_LOCUS (gimple_location (last))
9884 : == BUILTINS_LOCATION
9885 519 : && (gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE)
9886 190 : || gimple_call_builtin_p (last,
9887 : BUILT_IN_UNREACHABLE_TRAP)
9888 3 : || gimple_call_builtin_p (last, BUILT_IN_TRAP)))
9889 192899 : || gimple_call_builtin_p (last, ubsan_missing_ret)))
9890 : {
9891 534 : gimple_stmt_iterator gsi = gsi_for_stmt (last);
9892 534 : gsi_prev_nondebug (&gsi);
9893 534 : gimple *prev = gsi_stmt (gsi);
9894 534 : if (prev == NULL)
9895 : location = UNKNOWN_LOCATION;
9896 : else
9897 186 : location = gimple_location (prev);
9898 534 : if (LOCATION_LOCUS (location) == UNKNOWN_LOCATION)
9899 374 : location = fun->function_end_locus;
9900 534 : if (warning_at (location, OPT_Wreturn_type,
9901 : "control reaches end of non-void function"))
9902 123 : suppress_warning (fun->decl, OPT_Wreturn_type);
9903 534 : break;
9904 : }
9905 : }
9906 : }
9907 : return 0;
9908 : }
9909 :
9910 : } // anon namespace
9911 :
9912 : gimple_opt_pass *
9913 302985 : make_pass_warn_function_return (gcc::context *ctxt)
9914 : {
9915 302985 : return new pass_warn_function_return (ctxt);
9916 : }
9917 :
9918 : /* Walk a gimplified function and warn for functions whose return value is
9919 : ignored and attribute((warn_unused_result)) is set. This is done before
9920 : inlining, so we don't have to worry about that. */
9921 :
9922 : static void
9923 10328454 : do_warn_unused_result (gimple_seq seq)
9924 : {
9925 10328454 : tree fdecl, ftype;
9926 10328454 : gimple_stmt_iterator i;
9927 :
9928 64242850 : for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
9929 : {
9930 53914396 : gimple *g = gsi_stmt (i);
9931 :
9932 53914396 : switch (gimple_code (g))
9933 : {
9934 3653523 : case GIMPLE_BIND:
9935 3653523 : do_warn_unused_result (gimple_bind_body (as_a <gbind *>(g)));
9936 3653523 : break;
9937 2082489 : case GIMPLE_TRY:
9938 2082489 : do_warn_unused_result (gimple_try_eval (g));
9939 2082489 : do_warn_unused_result (gimple_try_cleanup (g));
9940 2082489 : break;
9941 23286 : case GIMPLE_CATCH:
9942 46572 : do_warn_unused_result (gimple_catch_handler (
9943 23286 : as_a <gcatch *> (g)));
9944 23286 : break;
9945 4351 : case GIMPLE_EH_FILTER:
9946 4351 : do_warn_unused_result (gimple_eh_filter_failure (g));
9947 4351 : break;
9948 :
9949 8144588 : case GIMPLE_CALL:
9950 8144588 : if (gimple_call_lhs (g))
9951 : break;
9952 4493252 : if (gimple_call_internal_p (g))
9953 : break;
9954 4470416 : if (warning_suppressed_p (g, OPT_Wunused_result))
9955 : break;
9956 :
9957 : /* This is a naked call, as opposed to a GIMPLE_CALL with an
9958 : LHS. All calls whose value is ignored should be
9959 : represented like this. Look for the attribute. */
9960 4427999 : fdecl = gimple_call_fndecl (g);
9961 4427999 : ftype = gimple_call_fntype (g);
9962 :
9963 4427999 : if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
9964 : {
9965 186 : auto_urlify_attributes sentinel;
9966 :
9967 186 : location_t loc = gimple_location (g);
9968 :
9969 186 : if (fdecl)
9970 114 : warning_at (loc, OPT_Wunused_result,
9971 : "ignoring return value of %qD "
9972 : "declared with attribute %<warn_unused_result%>",
9973 : fdecl);
9974 : else
9975 72 : warning_at (loc, OPT_Wunused_result,
9976 : "ignoring return value of function "
9977 : "declared with attribute %<warn_unused_result%>");
9978 186 : }
9979 : break;
9980 :
9981 : default:
9982 : /* Not a container, not a call, or a call whose value is used. */
9983 : break;
9984 : }
9985 : }
9986 10328454 : }
9987 :
9988 : namespace {
9989 :
9990 : const pass_data pass_data_warn_unused_result =
9991 : {
9992 : GIMPLE_PASS, /* type */
9993 : "*warn_unused_result", /* name */
9994 : OPTGROUP_NONE, /* optinfo_flags */
9995 : TV_NONE, /* tv_id */
9996 : PROP_gimple_any, /* properties_required */
9997 : 0, /* properties_provided */
9998 : 0, /* properties_destroyed */
9999 : 0, /* todo_flags_start */
10000 : 0, /* todo_flags_finish */
10001 : };
10002 :
10003 : class pass_warn_unused_result : public gimple_opt_pass
10004 : {
10005 : public:
10006 302985 : pass_warn_unused_result (gcc::context *ctxt)
10007 605970 : : gimple_opt_pass (pass_data_warn_unused_result, ctxt)
10008 : {}
10009 :
10010 : /* opt_pass methods: */
10011 3019912 : bool gate (function *) final override { return flag_warn_unused_result; }
10012 2482316 : unsigned int execute (function *) final override
10013 : {
10014 2482316 : do_warn_unused_result (gimple_body (current_function_decl));
10015 2482316 : return 0;
10016 : }
10017 :
10018 : }; // class pass_warn_unused_result
10019 :
10020 : } // anon namespace
10021 :
10022 : gimple_opt_pass *
10023 302985 : make_pass_warn_unused_result (gcc::context *ctxt)
10024 : {
10025 302985 : return new pass_warn_unused_result (ctxt);
10026 : }
10027 :
10028 : /* Maybe Remove stores to variables we marked write-only.
10029 : Return true if a store was removed. */
10030 : static bool
10031 474436938 : maybe_remove_writeonly_store (gimple_stmt_iterator &gsi, gimple *stmt,
10032 : bitmap dce_ssa_names)
10033 : {
10034 : /* Keep access when store has side effect, i.e. in case when source
10035 : is volatile. */
10036 474436938 : if (!gimple_store_p (stmt)
10037 65019684 : || gimple_has_side_effects (stmt)
10038 526777594 : || optimize_debug)
10039 : return false;
10040 :
10041 52289224 : tree lhs = get_base_address (gimple_get_lhs (stmt));
10042 :
10043 52289224 : if (!VAR_P (lhs)
10044 41083573 : || (!TREE_STATIC (lhs) && !DECL_EXTERNAL (lhs))
10045 59445843 : || !varpool_node::get (lhs)->writeonly)
10046 : return false;
10047 :
10048 31698 : if (dump_file && (dump_flags & TDF_DETAILS))
10049 : {
10050 0 : fprintf (dump_file, "Removing statement, writes"
10051 : " to write only var:\n");
10052 0 : print_gimple_stmt (dump_file, stmt, 0,
10053 : TDF_VOPS|TDF_MEMSYMS);
10054 : }
10055 :
10056 : /* Mark ssa name defining to be checked for simple dce. */
10057 31698 : if (gimple_assign_single_p (stmt))
10058 : {
10059 31698 : tree rhs = gimple_assign_rhs1 (stmt);
10060 31698 : if (TREE_CODE (rhs) == SSA_NAME
10061 31698 : && !SSA_NAME_IS_DEFAULT_DEF (rhs))
10062 9840 : bitmap_set_bit (dce_ssa_names, SSA_NAME_VERSION (rhs));
10063 : }
10064 31698 : unlink_stmt_vdef (stmt);
10065 31698 : gsi_remove (&gsi, true);
10066 31698 : release_defs (stmt);
10067 31698 : return true;
10068 : }
10069 :
10070 : /* IPA passes, compilation of earlier functions or inlining
10071 : might have changed some properties, such as marked functions nothrow,
10072 : pure, const or noreturn.
10073 : Remove redundant edges and basic blocks, and create new ones if necessary. */
10074 :
10075 : unsigned int
10076 11517148 : execute_fixup_cfg (void)
10077 : {
10078 11517148 : basic_block bb;
10079 11517148 : gimple_stmt_iterator gsi;
10080 11517148 : int todo = 0;
10081 11517148 : cgraph_node *node = cgraph_node::get (current_function_decl);
10082 : /* Same scaling is also done by ipa_merge_profiles. */
10083 11517148 : profile_count num = node->count;
10084 11517148 : profile_count den = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
10085 11517148 : bool scale = num.initialized_p () && !(num == den);
10086 11517148 : auto_bitmap dce_ssa_names;
10087 :
10088 11517148 : if (scale)
10089 : {
10090 30337 : profile_count::adjust_for_ipa_scaling (&num, &den);
10091 30337 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = node->count;
10092 30337 : EXIT_BLOCK_PTR_FOR_FN (cfun)->count
10093 30337 : = EXIT_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale (num, den);
10094 : }
10095 :
10096 103271957 : FOR_EACH_BB_FN (bb, cfun)
10097 : {
10098 91754809 : if (scale)
10099 1506249 : bb->count = bb->count.apply_scale (num, den);
10100 657946556 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
10101 : {
10102 474436938 : gimple *stmt = gsi_stmt (gsi);
10103 474436938 : tree decl = is_gimple_call (stmt)
10104 474436938 : ? gimple_call_fndecl (stmt)
10105 : : NULL;
10106 45779173 : if (decl)
10107 : {
10108 42610895 : int flags = gimple_call_flags (stmt);
10109 42610895 : if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
10110 : {
10111 6945217 : if (gimple_in_ssa_p (cfun))
10112 : {
10113 6239118 : todo |= TODO_update_ssa | TODO_cleanup_cfg;
10114 6239118 : update_stmt (stmt);
10115 : }
10116 : }
10117 42610895 : if (flags & ECF_NORETURN
10118 42610895 : && fixup_noreturn_call (stmt))
10119 98951 : todo |= TODO_cleanup_cfg;
10120 : }
10121 :
10122 : /* Remove stores to variables we marked write-only. */
10123 474436938 : if (maybe_remove_writeonly_store (gsi, stmt, dce_ssa_names))
10124 : {
10125 31698 : todo |= TODO_update_ssa | TODO_cleanup_cfg;
10126 31698 : continue;
10127 : }
10128 :
10129 : /* For calls we can simply remove LHS when it is known
10130 : to be write-only. */
10131 474405240 : if (is_gimple_call (stmt)
10132 474405240 : && gimple_get_lhs (stmt))
10133 : {
10134 18809416 : tree lhs = get_base_address (gimple_get_lhs (stmt));
10135 :
10136 18809416 : if (VAR_P (lhs)
10137 4917056 : && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
10138 18820003 : && varpool_node::get (lhs)->writeonly)
10139 : {
10140 2 : gimple_call_set_lhs (stmt, NULL);
10141 2 : update_stmt (stmt);
10142 2 : todo |= TODO_update_ssa | TODO_cleanup_cfg;
10143 : }
10144 : }
10145 :
10146 474405240 : gsi_next (&gsi);
10147 : }
10148 183509618 : if (gimple *last = *gsi_last_bb (bb))
10149 : {
10150 85522929 : if (maybe_clean_eh_stmt (last)
10151 85522929 : && gimple_purge_dead_eh_edges (bb))
10152 233589 : todo |= TODO_cleanup_cfg;
10153 85522929 : if (gimple_purge_dead_abnormal_call_edges (bb))
10154 1069 : todo |= TODO_cleanup_cfg;
10155 : }
10156 :
10157 : /* If we have a basic block with no successors that does not
10158 : end with a control statement or a noreturn call end it with
10159 : a call to __builtin_unreachable. This situation can occur
10160 : when inlining a noreturn call that does in fact return. */
10161 91754809 : if (EDGE_COUNT (bb->succs) == 0)
10162 : {
10163 6701946 : gimple *stmt = last_nondebug_stmt (bb);
10164 6701946 : if (!stmt
10165 6701946 : || (!is_ctrl_stmt (stmt)
10166 5820454 : && (!is_gimple_call (stmt)
10167 5820436 : || !gimple_call_noreturn_p (stmt))))
10168 : {
10169 133 : if (stmt && is_gimple_call (stmt))
10170 104 : gimple_call_set_ctrl_altering (stmt, false);
10171 133 : stmt = gimple_build_builtin_unreachable (UNKNOWN_LOCATION);
10172 133 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
10173 133 : gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
10174 133 : if (!cfun->after_inlining)
10175 109 : if (tree fndecl = gimple_call_fndecl (stmt))
10176 : {
10177 109 : gcall *call_stmt = dyn_cast <gcall *> (stmt);
10178 109 : node->create_edge (cgraph_node::get_create (fndecl),
10179 : call_stmt, bb->count);
10180 : }
10181 : }
10182 : }
10183 : }
10184 11517148 : if (scale)
10185 : {
10186 30337 : update_max_bb_count ();
10187 30337 : compute_function_frequency ();
10188 : }
10189 :
10190 11517148 : if (current_loops
10191 11517148 : && (todo & TODO_cleanup_cfg))
10192 2013419 : loops_state_set (LOOPS_NEED_FIXUP);
10193 :
10194 11517148 : simple_dce_from_worklist (dce_ssa_names);
10195 :
10196 11517148 : return todo;
10197 11517148 : }
10198 :
10199 : namespace {
10200 :
10201 : const pass_data pass_data_fixup_cfg =
10202 : {
10203 : GIMPLE_PASS, /* type */
10204 : "fixup_cfg", /* name */
10205 : OPTGROUP_NONE, /* optinfo_flags */
10206 : TV_NONE, /* tv_id */
10207 : PROP_cfg, /* properties_required */
10208 : 0, /* properties_provided */
10209 : 0, /* properties_destroyed */
10210 : 0, /* todo_flags_start */
10211 : 0, /* todo_flags_finish */
10212 : };
10213 :
10214 : class pass_fixup_cfg : public gimple_opt_pass
10215 : {
10216 : public:
10217 908955 : pass_fixup_cfg (gcc::context *ctxt)
10218 1817910 : : gimple_opt_pass (pass_data_fixup_cfg, ctxt)
10219 : {}
10220 :
10221 : /* opt_pass methods: */
10222 605970 : opt_pass * clone () final override { return new pass_fixup_cfg (m_ctxt); }
10223 7527582 : unsigned int execute (function *) final override
10224 : {
10225 7527582 : return execute_fixup_cfg ();
10226 : }
10227 :
10228 : }; // class pass_fixup_cfg
10229 :
10230 : } // anon namespace
10231 :
10232 : gimple_opt_pass *
10233 302985 : make_pass_fixup_cfg (gcc::context *ctxt)
10234 : {
10235 302985 : return new pass_fixup_cfg (ctxt);
10236 : }
10237 :
10238 :
10239 : /* Sort PHI argument values for make_forwarders_with_degenerate_phis. */
10240 :
10241 : static int
10242 46293990 : sort_phi_args (const void *a_, const void *b_)
10243 : {
10244 46293990 : auto *a = (const std::pair<edge, hashval_t> *) a_;
10245 46293990 : auto *b = (const std::pair<edge, hashval_t> *) b_;
10246 46293990 : hashval_t ha = a->second;
10247 46293990 : hashval_t hb = b->second;
10248 46293990 : if (ha < hb)
10249 : return -1;
10250 28830481 : else if (ha > hb)
10251 : return 1;
10252 14071603 : else if (a->first->dest_idx < b->first->dest_idx)
10253 : return -1;
10254 7336945 : else if (a->first->dest_idx > b->first->dest_idx)
10255 : return 1;
10256 : else
10257 0 : return 0;
10258 : }
10259 :
10260 : /* Returns true if edge E comes from a possible ifconvertable branch.
10261 : That is:
10262 : ```
10263 : BB0:
10264 : if (a) goto BB1; else goto BB2;
10265 : BB1:
10266 : BB2:
10267 : ```
10268 : Returns true for the edge from BB0->BB2 or BB1->BB2.
10269 : This function assumes we only have one middle block.
10270 : And the middle block is empty. */
10271 :
10272 : static bool
10273 1235134 : ifconvertable_edge (edge e)
10274 : {
10275 1235134 : basic_block bb2 = e->dest;
10276 1235134 : basic_block bb0 = e->src;
10277 1235134 : basic_block bb1 = nullptr, rbb1;
10278 1235134 : if (e->src == e->dest)
10279 : return false;
10280 1235134 : if (EDGE_COUNT (bb0->succs) > 2)
10281 : return false;
10282 1229629 : if (single_succ_p (bb0))
10283 : {
10284 488026 : if (!single_pred_p (bb0))
10285 : return false;
10286 : /* The middle block can only be empty,
10287 : otherwise the phis will be
10288 : different anyways. */
10289 395069 : if (!empty_block_p (bb0))
10290 : return false;
10291 53291 : bb1 = bb0;
10292 53291 : bb0 = single_pred (bb0);
10293 53291 : if (EDGE_COUNT (bb0->succs) != 2)
10294 : return false;
10295 : }
10296 :
10297 : /* If convertibles are only for conditionals. */
10298 768487 : if (!is_a<gcond*>(*gsi_last_nondebug_bb (bb0)))
10299 : return false;
10300 :
10301 : /* Find the other basic block. */
10302 716227 : if (EDGE_SUCC (bb0, 0)->dest == bb2)
10303 343493 : rbb1 = EDGE_SUCC (bb0, 1)->dest;
10304 372734 : else if (EDGE_SUCC (bb0, 1)->dest == bb2)
10305 : rbb1 = EDGE_SUCC (bb0, 0)->dest;
10306 : else
10307 : return false;
10308 :
10309 : /* If we already know bb1, then just test it. */
10310 716156 : if (bb1)
10311 26812 : return rbb1 == bb1;
10312 :
10313 902217 : if (!single_succ_p (rbb1) || !single_pred_p (rbb1))
10314 : return false;
10315 : /* The middle block can only be empty,
10316 : otherwise the phis will be
10317 : different anyways. */
10318 143350 : if (!empty_block_p (rbb1))
10319 : return false;
10320 :
10321 26812 : return single_succ (rbb1) == bb2;
10322 : }
10323 :
10324 : /* Look for a non-virtual PHIs and make a forwarder block when all PHIs
10325 : have the same argument on a set of edges. This is to not consider
10326 : control dependences of individual edges for same values but only for
10327 : the common set. Returns true if changed the CFG. */
10328 :
10329 : bool
10330 4708421 : make_forwarders_with_degenerate_phis (function *fn, bool skip_ifcvtable)
10331 : {
10332 4708421 : bool didsomething = false;
10333 :
10334 4708421 : basic_block bb;
10335 45102230 : FOR_EACH_BB_FN (bb, fn)
10336 : {
10337 : /* Only PHIs with three or more arguments have opportunities. */
10338 40393809 : if (EDGE_COUNT (bb->preds) < 3)
10339 39914163 : continue;
10340 : /* Do not touch loop headers or blocks with abnormal predecessors.
10341 : ??? This is to avoid creating valid loops here, see PR103458.
10342 : We might want to improve things to either explicitly add those
10343 : loops or at least consider blocks with no backedges. */
10344 1882504 : if (bb->loop_father->header == bb
10345 1879059 : || bb_has_abnormal_pred (bb))
10346 3445 : continue;
10347 :
10348 : /* Take one PHI node as template to look for identical
10349 : arguments. Build a vector of candidates forming sets
10350 : of argument edges with equal values. Note optimality
10351 : depends on the particular choice of the template PHI
10352 : since equal arguments are unordered leaving other PHIs
10353 : with more than one set of equal arguments within this
10354 : argument range unsorted. We'd have to break ties by
10355 : looking at other PHI nodes. */
10356 1875614 : gphi_iterator gsi = gsi_start_nonvirtual_phis (bb);
10357 1875614 : if (gsi_end_p (gsi))
10358 1086814 : continue;
10359 788800 : gphi *phi = gsi.phi ();
10360 788800 : auto_vec<std::pair<edge, hashval_t>, 8> args;
10361 788800 : bool need_resort = false;
10362 4745906 : for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
10363 : {
10364 3957106 : edge e = gimple_phi_arg_edge (phi, i);
10365 : /* Skip abnormal edges since we cannot redirect them. */
10366 3957106 : if (e->flags & EDGE_ABNORMAL)
10367 3957106 : continue;
10368 : /* Skip loop exit edges when we are in loop-closed SSA form
10369 : since the forwarder we'd create does not have a PHI node. */
10370 3957106 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
10371 3957106 : && loop_exit_edge_p (e->src->loop_father, e))
10372 25141 : continue;
10373 :
10374 : /* Skip ifconvertable edges when asked as we want the
10375 : copy/constant on that edge still when going out of ssa.
10376 : FIXME: phiopt should produce COND_EXPR but there
10377 : are regressions with that. */
10378 3931965 : if (skip_ifcvtable && ifconvertable_edge (e))
10379 53624 : continue;
10380 :
10381 3878341 : tree arg = gimple_phi_arg_def (phi, i);
10382 3878341 : if (!CONSTANT_CLASS_P (arg) && TREE_CODE (arg) != SSA_NAME)
10383 3878341 : need_resort = true;
10384 3878341 : args.safe_push (std::make_pair (e, iterative_hash_expr (arg, 0)));
10385 : }
10386 788800 : if (args.length () < 2)
10387 13238 : continue;
10388 775562 : args.qsort (sort_phi_args);
10389 : /* The above sorting can be different between -g and -g0, as e.g. decls
10390 : can have different uids (-g could have bigger gaps in between them).
10391 : So, only use that to determine which args are equal, then change
10392 : second from hash value to smallest dest_idx of the edges which have
10393 : equal argument and sort again. If all the phi arguments are
10394 : constants or SSA_NAME, there is no need for the second sort, the hash
10395 : values are stable in that case. */
10396 775562 : hashval_t hash = args[0].second;
10397 775562 : args[0].second = args[0].first->dest_idx;
10398 775562 : bool any_equal = false;
10399 3867787 : for (unsigned i = 1; i < args.length (); ++i)
10400 3092225 : if (hash == args[i].second
10401 4339358 : && operand_equal_p (PHI_ARG_DEF_FROM_EDGE (phi, args[i - 1].first),
10402 1247133 : PHI_ARG_DEF_FROM_EDGE (phi, args[i].first)))
10403 : {
10404 1245932 : args[i].second = args[i - 1].second;
10405 1245932 : any_equal = true;
10406 : }
10407 : else
10408 : {
10409 1846293 : hash = args[i].second;
10410 1846293 : args[i].second = args[i].first->dest_idx;
10411 : }
10412 775562 : if (!any_equal)
10413 295916 : continue;
10414 479646 : if (need_resort)
10415 25198 : args.qsort (sort_phi_args);
10416 :
10417 : /* From the candidates vector now verify true candidates for
10418 : forwarders and create them. */
10419 479646 : gphi *vphi = get_virtual_phi (bb);
10420 479646 : unsigned start = 0;
10421 3931974 : while (start < args.length () - 1)
10422 : {
10423 1248590 : unsigned i;
10424 3610796 : for (i = start + 1; i < args.length (); ++i)
10425 3359615 : if (args[start].second != args[i].second)
10426 : break;
10427 : /* args[start]..args[i-1] are equal. */
10428 1248590 : if (start != i - 1)
10429 : {
10430 : /* Check all PHI nodes for argument equality
10431 : except for vops. */
10432 725077 : bool equal = true;
10433 725077 : gphi_iterator gsi2 = gsi;
10434 725077 : gsi_next (&gsi2);
10435 1551443 : for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
10436 : {
10437 1060843 : gphi *phi2 = gsi2.phi ();
10438 2121686 : if (virtual_operand_p (gimple_phi_result (phi2)))
10439 257542 : continue;
10440 803301 : tree start_arg
10441 803301 : = PHI_ARG_DEF_FROM_EDGE (phi2, args[start].first);
10442 4075011 : for (unsigned j = start + 1; j < i; ++j)
10443 : {
10444 5512964 : if (!operand_equal_p (start_arg,
10445 2756482 : PHI_ARG_DEF_FROM_EDGE
10446 : (phi2, args[j].first)))
10447 : {
10448 : /* Another PHI might have a shorter set of
10449 : equivalent args. Go for that. */
10450 288073 : i = j;
10451 288073 : if (j == start + 1)
10452 : equal = false;
10453 : break;
10454 : }
10455 : }
10456 : if (!equal)
10457 : break;
10458 : }
10459 725077 : if (equal)
10460 : {
10461 : /* If we are asked to forward all edges the block
10462 : has all degenerate PHIs. Do nothing in that case. */
10463 490600 : if (start == 0
10464 230905 : && i == args.length ()
10465 494461 : && args.length () == gimple_phi_num_args (phi))
10466 : break;
10467 : /* Instead of using make_forwarder_block we are
10468 : rolling our own variant knowing that the forwarder
10469 : does not need PHI nodes apart from eventually
10470 : a virtual one. */
10471 488528 : auto_vec<tree, 8> vphi_args;
10472 488528 : if (vphi)
10473 : {
10474 329021 : vphi_args.reserve_exact (i - start);
10475 1655959 : for (unsigned j = start; j < i; ++j)
10476 997917 : vphi_args.quick_push
10477 997917 : (PHI_ARG_DEF_FROM_EDGE (vphi, args[j].first));
10478 : }
10479 488528 : free_dominance_info (fn, CDI_DOMINATORS);
10480 488528 : if (dump_file && (dump_flags & TDF_DETAILS))
10481 : {
10482 1 : fprintf (dump_file, "New forwarder block for edge ");
10483 1 : fprintf (dump_file, "%d -> %d.\n",
10484 1 : args[start].first->src->index,
10485 1 : args[start].first->dest->index);
10486 : }
10487 488528 : basic_block forwarder = split_edge (args[start].first);
10488 488528 : profile_count count = forwarder->count;
10489 488528 : bool irr = false;
10490 1482858 : for (unsigned j = start + 1; j < i; ++j)
10491 : {
10492 994330 : edge e = args[j].first;
10493 994330 : if (e->flags & EDGE_IRREDUCIBLE_LOOP)
10494 4536 : irr = true;
10495 994330 : redirect_edge_and_branch_force (e, forwarder);
10496 994330 : redirect_edge_var_map_clear (e);
10497 994330 : count += e->count ();
10498 : }
10499 488528 : forwarder->count = count;
10500 488528 : if (irr)
10501 : {
10502 2947 : forwarder->flags |= BB_IRREDUCIBLE_LOOP;
10503 2947 : single_succ_edge (forwarder)->flags
10504 2947 : |= EDGE_IRREDUCIBLE_LOOP;
10505 : }
10506 :
10507 488528 : if (vphi)
10508 : {
10509 329021 : tree def = copy_ssa_name (vphi_args[0]);
10510 329021 : gphi *vphi_copy = create_phi_node (def, forwarder);
10511 1655959 : for (unsigned j = start; j < i; ++j)
10512 1995834 : add_phi_arg (vphi_copy, vphi_args[j - start],
10513 997917 : args[j].first, UNKNOWN_LOCATION);
10514 329021 : SET_PHI_ARG_DEF
10515 : (vphi, single_succ_edge (forwarder)->dest_idx, def);
10516 : }
10517 488528 : didsomething = true;
10518 488528 : }
10519 : }
10520 : /* Continue searching for more opportunities. */
10521 1246518 : start = i;
10522 : }
10523 788800 : }
10524 4708421 : return didsomething;
10525 : }
10526 :
10527 : /* Garbage collection support for edge_def. */
10528 :
10529 : extern void gt_ggc_mx (tree&);
10530 : extern void gt_ggc_mx (gimple *&);
10531 : extern void gt_ggc_mx (rtx&);
10532 : extern void gt_ggc_mx (basic_block&);
10533 :
10534 : static void
10535 51589730 : gt_ggc_mx (rtx_insn *& x)
10536 : {
10537 0 : if (x)
10538 0 : gt_ggc_mx_rtx_def ((void *) x);
10539 0 : }
10540 :
10541 : void
10542 171699092 : gt_ggc_mx (edge_def *e)
10543 : {
10544 171699092 : tree block = LOCATION_BLOCK (e->goto_locus);
10545 171699092 : gt_ggc_mx (e->src);
10546 171699092 : gt_ggc_mx (e->dest);
10547 171699092 : if (current_ir_type () == IR_GIMPLE)
10548 120109362 : gt_ggc_mx (e->insns.g);
10549 : else
10550 51589730 : gt_ggc_mx (e->insns.r);
10551 171699092 : gt_ggc_mx (block);
10552 171699092 : }
10553 :
10554 : /* PCH support for edge_def. */
10555 :
10556 : extern void gt_pch_nx (tree&);
10557 : extern void gt_pch_nx (gimple *&);
10558 : extern void gt_pch_nx (rtx&);
10559 : extern void gt_pch_nx (basic_block&);
10560 :
10561 : static void
10562 0 : gt_pch_nx (rtx_insn *& x)
10563 : {
10564 0 : if (x)
10565 0 : gt_pch_nx_rtx_def ((void *) x);
10566 0 : }
10567 :
10568 : void
10569 0 : gt_pch_nx (edge_def *e)
10570 : {
10571 0 : tree block = LOCATION_BLOCK (e->goto_locus);
10572 0 : gt_pch_nx (e->src);
10573 0 : gt_pch_nx (e->dest);
10574 0 : if (current_ir_type () == IR_GIMPLE)
10575 0 : gt_pch_nx (e->insns.g);
10576 : else
10577 0 : gt_pch_nx (e->insns.r);
10578 0 : gt_pch_nx (block);
10579 0 : }
10580 :
10581 : void
10582 0 : gt_pch_nx (edge_def *e, gt_pointer_operator op, void *cookie)
10583 : {
10584 0 : tree block = LOCATION_BLOCK (e->goto_locus);
10585 0 : op (&(e->src), NULL, cookie);
10586 0 : op (&(e->dest), NULL, cookie);
10587 0 : if (current_ir_type () == IR_GIMPLE)
10588 0 : op (&(e->insns.g), NULL, cookie);
10589 : else
10590 0 : op (&(e->insns.r), NULL, cookie);
10591 0 : op (&(block), &(block), cookie);
10592 0 : }
10593 :
10594 : #if CHECKING_P
10595 :
10596 : namespace selftest {
10597 :
10598 : /* Helper function for CFG selftests: create a dummy function decl
10599 : and push it as cfun. */
10600 :
10601 : static tree
10602 12 : push_fndecl (const char *name)
10603 : {
10604 12 : tree fn_type = build_function_type_array (integer_type_node, 0, NULL);
10605 : /* FIXME: this uses input_location: */
10606 12 : tree fndecl = build_fn_decl (name, fn_type);
10607 12 : tree retval = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
10608 : NULL_TREE, integer_type_node);
10609 12 : DECL_RESULT (fndecl) = retval;
10610 12 : push_struct_function (fndecl);
10611 12 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
10612 12 : ASSERT_TRUE (fun != NULL);
10613 12 : init_empty_tree_cfg_for_function (fun);
10614 12 : ASSERT_EQ (2, n_basic_blocks_for_fn (fun));
10615 12 : ASSERT_EQ (0, n_edges_for_fn (fun));
10616 12 : return fndecl;
10617 : }
10618 :
10619 : /* These tests directly create CFGs.
10620 : Compare with the static fns within tree-cfg.cc:
10621 : - build_gimple_cfg
10622 : - make_blocks: calls create_basic_block (seq, bb);
10623 : - make_edges. */
10624 :
10625 : /* Verify a simple cfg of the form:
10626 : ENTRY -> A -> B -> C -> EXIT. */
10627 :
10628 : static void
10629 4 : test_linear_chain ()
10630 : {
10631 4 : gimple_register_cfg_hooks ();
10632 :
10633 4 : tree fndecl = push_fndecl ("cfg_test_linear_chain");
10634 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
10635 :
10636 : /* Create some empty blocks. */
10637 4 : basic_block bb_a = create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
10638 4 : basic_block bb_b = create_empty_bb (bb_a);
10639 4 : basic_block bb_c = create_empty_bb (bb_b);
10640 :
10641 4 : ASSERT_EQ (5, n_basic_blocks_for_fn (fun));
10642 4 : ASSERT_EQ (0, n_edges_for_fn (fun));
10643 :
10644 : /* Create some edges: a simple linear chain of BBs. */
10645 4 : make_edge (ENTRY_BLOCK_PTR_FOR_FN (fun), bb_a, EDGE_FALLTHRU);
10646 4 : make_edge (bb_a, bb_b, 0);
10647 4 : make_edge (bb_b, bb_c, 0);
10648 4 : make_edge (bb_c, EXIT_BLOCK_PTR_FOR_FN (fun), 0);
10649 :
10650 : /* Verify the edges. */
10651 4 : ASSERT_EQ (4, n_edges_for_fn (fun));
10652 4 : ASSERT_EQ (NULL, ENTRY_BLOCK_PTR_FOR_FN (fun)->preds);
10653 4 : ASSERT_EQ (1, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs->length ());
10654 4 : ASSERT_EQ (1, bb_a->preds->length ());
10655 4 : ASSERT_EQ (1, bb_a->succs->length ());
10656 4 : ASSERT_EQ (1, bb_b->preds->length ());
10657 4 : ASSERT_EQ (1, bb_b->succs->length ());
10658 4 : ASSERT_EQ (1, bb_c->preds->length ());
10659 4 : ASSERT_EQ (1, bb_c->succs->length ());
10660 4 : ASSERT_EQ (1, EXIT_BLOCK_PTR_FOR_FN (fun)->preds->length ());
10661 4 : ASSERT_EQ (NULL, EXIT_BLOCK_PTR_FOR_FN (fun)->succs);
10662 :
10663 : /* Verify the dominance information
10664 : Each BB in our simple chain should be dominated by the one before
10665 : it. */
10666 4 : calculate_dominance_info (CDI_DOMINATORS);
10667 4 : ASSERT_EQ (bb_a, get_immediate_dominator (CDI_DOMINATORS, bb_b));
10668 4 : ASSERT_EQ (bb_b, get_immediate_dominator (CDI_DOMINATORS, bb_c));
10669 4 : auto_vec<basic_block> dom_by_b = get_dominated_by (CDI_DOMINATORS, bb_b);
10670 4 : ASSERT_EQ (1, dom_by_b.length ());
10671 4 : ASSERT_EQ (bb_c, dom_by_b[0]);
10672 4 : free_dominance_info (CDI_DOMINATORS);
10673 :
10674 : /* Similarly for post-dominance: each BB in our chain is post-dominated
10675 : by the one after it. */
10676 4 : calculate_dominance_info (CDI_POST_DOMINATORS);
10677 4 : ASSERT_EQ (bb_b, get_immediate_dominator (CDI_POST_DOMINATORS, bb_a));
10678 4 : ASSERT_EQ (bb_c, get_immediate_dominator (CDI_POST_DOMINATORS, bb_b));
10679 4 : auto_vec<basic_block> postdom_by_b = get_dominated_by (CDI_POST_DOMINATORS, bb_b);
10680 4 : ASSERT_EQ (1, postdom_by_b.length ());
10681 4 : ASSERT_EQ (bb_a, postdom_by_b[0]);
10682 4 : free_dominance_info (CDI_POST_DOMINATORS);
10683 :
10684 4 : pop_cfun ();
10685 4 : }
10686 :
10687 : /* Verify a simple CFG of the form:
10688 : ENTRY
10689 : |
10690 : A
10691 : / \
10692 : /t \f
10693 : B C
10694 : \ /
10695 : \ /
10696 : D
10697 : |
10698 : EXIT. */
10699 :
10700 : static void
10701 4 : test_diamond ()
10702 : {
10703 4 : gimple_register_cfg_hooks ();
10704 :
10705 4 : tree fndecl = push_fndecl ("cfg_test_diamond");
10706 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
10707 :
10708 : /* Create some empty blocks. */
10709 4 : basic_block bb_a = create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
10710 4 : basic_block bb_b = create_empty_bb (bb_a);
10711 4 : basic_block bb_c = create_empty_bb (bb_a);
10712 4 : basic_block bb_d = create_empty_bb (bb_b);
10713 :
10714 4 : ASSERT_EQ (6, n_basic_blocks_for_fn (fun));
10715 4 : ASSERT_EQ (0, n_edges_for_fn (fun));
10716 :
10717 : /* Create the edges. */
10718 4 : make_edge (ENTRY_BLOCK_PTR_FOR_FN (fun), bb_a, EDGE_FALLTHRU);
10719 4 : make_edge (bb_a, bb_b, EDGE_TRUE_VALUE);
10720 4 : make_edge (bb_a, bb_c, EDGE_FALSE_VALUE);
10721 4 : make_edge (bb_b, bb_d, 0);
10722 4 : make_edge (bb_c, bb_d, 0);
10723 4 : make_edge (bb_d, EXIT_BLOCK_PTR_FOR_FN (fun), 0);
10724 :
10725 : /* Verify the edges. */
10726 4 : ASSERT_EQ (6, n_edges_for_fn (fun));
10727 4 : ASSERT_EQ (1, bb_a->preds->length ());
10728 4 : ASSERT_EQ (2, bb_a->succs->length ());
10729 4 : ASSERT_EQ (1, bb_b->preds->length ());
10730 4 : ASSERT_EQ (1, bb_b->succs->length ());
10731 4 : ASSERT_EQ (1, bb_c->preds->length ());
10732 4 : ASSERT_EQ (1, bb_c->succs->length ());
10733 4 : ASSERT_EQ (2, bb_d->preds->length ());
10734 4 : ASSERT_EQ (1, bb_d->succs->length ());
10735 :
10736 : /* Verify the dominance information. */
10737 4 : calculate_dominance_info (CDI_DOMINATORS);
10738 4 : ASSERT_EQ (bb_a, get_immediate_dominator (CDI_DOMINATORS, bb_b));
10739 4 : ASSERT_EQ (bb_a, get_immediate_dominator (CDI_DOMINATORS, bb_c));
10740 4 : ASSERT_EQ (bb_a, get_immediate_dominator (CDI_DOMINATORS, bb_d));
10741 4 : auto_vec<basic_block> dom_by_a = get_dominated_by (CDI_DOMINATORS, bb_a);
10742 4 : ASSERT_EQ (3, dom_by_a.length ()); /* B, C, D, in some order. */
10743 4 : dom_by_a.release ();
10744 4 : auto_vec<basic_block> dom_by_b = get_dominated_by (CDI_DOMINATORS, bb_b);
10745 4 : ASSERT_EQ (0, dom_by_b.length ());
10746 4 : dom_by_b.release ();
10747 4 : free_dominance_info (CDI_DOMINATORS);
10748 :
10749 : /* Similarly for post-dominance. */
10750 4 : calculate_dominance_info (CDI_POST_DOMINATORS);
10751 4 : ASSERT_EQ (bb_d, get_immediate_dominator (CDI_POST_DOMINATORS, bb_a));
10752 4 : ASSERT_EQ (bb_d, get_immediate_dominator (CDI_POST_DOMINATORS, bb_b));
10753 4 : ASSERT_EQ (bb_d, get_immediate_dominator (CDI_POST_DOMINATORS, bb_c));
10754 4 : auto_vec<basic_block> postdom_by_d = get_dominated_by (CDI_POST_DOMINATORS, bb_d);
10755 4 : ASSERT_EQ (3, postdom_by_d.length ()); /* A, B, C in some order. */
10756 4 : postdom_by_d.release ();
10757 4 : auto_vec<basic_block> postdom_by_b = get_dominated_by (CDI_POST_DOMINATORS, bb_b);
10758 4 : ASSERT_EQ (0, postdom_by_b.length ());
10759 4 : postdom_by_b.release ();
10760 4 : free_dominance_info (CDI_POST_DOMINATORS);
10761 :
10762 4 : pop_cfun ();
10763 4 : }
10764 :
10765 : /* Verify that we can handle a CFG containing a "complete" aka
10766 : fully-connected subgraph (where A B C D below all have edges
10767 : pointing to each other node, also to themselves).
10768 : e.g.:
10769 : ENTRY EXIT
10770 : | ^
10771 : | /
10772 : | /
10773 : | /
10774 : V/
10775 : A<--->B
10776 : ^^ ^^
10777 : | \ / |
10778 : | X |
10779 : | / \ |
10780 : VV VV
10781 : C<--->D
10782 : */
10783 :
10784 : static void
10785 4 : test_fully_connected ()
10786 : {
10787 4 : gimple_register_cfg_hooks ();
10788 :
10789 4 : tree fndecl = push_fndecl ("cfg_fully_connected");
10790 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
10791 :
10792 4 : const int n = 4;
10793 :
10794 : /* Create some empty blocks. */
10795 4 : auto_vec <basic_block> subgraph_nodes;
10796 20 : for (int i = 0; i < n; i++)
10797 16 : subgraph_nodes.safe_push (create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (fun)));
10798 :
10799 4 : ASSERT_EQ (n + 2, n_basic_blocks_for_fn (fun));
10800 4 : ASSERT_EQ (0, n_edges_for_fn (fun));
10801 :
10802 : /* Create the edges. */
10803 4 : make_edge (ENTRY_BLOCK_PTR_FOR_FN (fun), subgraph_nodes[0], EDGE_FALLTHRU);
10804 4 : make_edge (subgraph_nodes[0], EXIT_BLOCK_PTR_FOR_FN (fun), 0);
10805 24 : for (int i = 0; i < n; i++)
10806 80 : for (int j = 0; j < n; j++)
10807 64 : make_edge (subgraph_nodes[i], subgraph_nodes[j], 0);
10808 :
10809 : /* Verify the edges. */
10810 4 : ASSERT_EQ (2 + (n * n), n_edges_for_fn (fun));
10811 : /* The first one is linked to ENTRY/EXIT as well as itself and
10812 : everything else. */
10813 4 : ASSERT_EQ (n + 1, subgraph_nodes[0]->preds->length ());
10814 4 : ASSERT_EQ (n + 1, subgraph_nodes[0]->succs->length ());
10815 : /* The other ones in the subgraph are linked to everything in
10816 : the subgraph (including themselves). */
10817 16 : for (int i = 1; i < n; i++)
10818 : {
10819 12 : ASSERT_EQ (n, subgraph_nodes[i]->preds->length ());
10820 12 : ASSERT_EQ (n, subgraph_nodes[i]->succs->length ());
10821 : }
10822 :
10823 : /* Verify the dominance information. */
10824 4 : calculate_dominance_info (CDI_DOMINATORS);
10825 : /* The initial block in the subgraph should be dominated by ENTRY. */
10826 4 : ASSERT_EQ (ENTRY_BLOCK_PTR_FOR_FN (fun),
10827 : get_immediate_dominator (CDI_DOMINATORS,
10828 : subgraph_nodes[0]));
10829 : /* Every other block in the subgraph should be dominated by the
10830 : initial block. */
10831 16 : for (int i = 1; i < n; i++)
10832 12 : ASSERT_EQ (subgraph_nodes[0],
10833 : get_immediate_dominator (CDI_DOMINATORS,
10834 : subgraph_nodes[i]));
10835 4 : free_dominance_info (CDI_DOMINATORS);
10836 :
10837 : /* Similarly for post-dominance. */
10838 4 : calculate_dominance_info (CDI_POST_DOMINATORS);
10839 : /* The initial block in the subgraph should be postdominated by EXIT. */
10840 4 : ASSERT_EQ (EXIT_BLOCK_PTR_FOR_FN (fun),
10841 : get_immediate_dominator (CDI_POST_DOMINATORS,
10842 : subgraph_nodes[0]));
10843 : /* Every other block in the subgraph should be postdominated by the
10844 : initial block, since that leads to EXIT. */
10845 16 : for (int i = 1; i < n; i++)
10846 12 : ASSERT_EQ (subgraph_nodes[0],
10847 : get_immediate_dominator (CDI_POST_DOMINATORS,
10848 : subgraph_nodes[i]));
10849 4 : free_dominance_info (CDI_POST_DOMINATORS);
10850 :
10851 4 : pop_cfun ();
10852 4 : }
10853 :
10854 : /* Run all of the selftests within this file. */
10855 :
10856 : void
10857 4 : tree_cfg_cc_tests ()
10858 : {
10859 4 : test_linear_chain ();
10860 4 : test_diamond ();
10861 4 : test_fully_connected ();
10862 4 : }
10863 :
10864 : } // namespace selftest
10865 :
10866 : /* TODO: test the dominator/postdominator logic with various graphs/nodes:
10867 : - loop
10868 : - nested loops
10869 : - switch statement (a block with many out-edges)
10870 : - something that jumps to itself
10871 : - etc */
10872 :
10873 : #endif /* CHECKING_P */
|