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