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