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