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