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