Line data Source code
1 : /* Hooks for cfg representation specific functions.
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 : Contributed by Sebastian Pop <s.pop@laposte.net>
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 : #define INCLUDE_VECTOR
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "rtl.h"
27 : #include "cfghooks.h"
28 : #include "timevar.h"
29 : #include "pretty-print.h"
30 : #include "diagnostic-core.h"
31 : #include "dumpfile.h"
32 : #include "cfganal.h"
33 : #include "tree.h"
34 : #include "tree-ssa.h"
35 : #include "tree-cfg.h"
36 : #include "cfgloop.h"
37 : #include "sreal.h"
38 : #include "profile.h"
39 : #include "diagnostics/sarif-sink.h"
40 : #include "custom-sarif-properties/cfg.h"
41 : #include "hierarchical_discriminator.h"
42 :
43 : /* Location used to allocate a copyid for a duplicated BB. */
44 :
45 : static location_t
46 3304487 : copyid_location_from_bb (basic_block bb)
47 : {
48 3304487 : if (!bb)
49 : return UNKNOWN_LOCATION;
50 :
51 3304487 : if (current_ir_type () == IR_GIMPLE)
52 : {
53 2820083 : gimple *last = last_nondebug_stmt (bb);
54 2820083 : return last ? gimple_location (last) : UNKNOWN_LOCATION;
55 : }
56 :
57 484404 : rtx_insn *insn;
58 690055 : FOR_BB_INSNS_REVERSE (bb, insn)
59 668261 : if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
60 462610 : return INSN_LOCATION (insn);
61 :
62 : return UNKNOWN_LOCATION;
63 : }
64 :
65 : /* Disable warnings about missing quoting in GCC diagnostics. */
66 : #if __GNUC__ >= 10
67 : # pragma GCC diagnostic push
68 : # pragma GCC diagnostic ignored "-Wformat-diag"
69 : #endif
70 :
71 : /* A pointer to one of the hooks containers. */
72 : static const struct cfg_hooks *cfg_hooks;
73 :
74 : /* Initialization of functions specific to the rtl IR. */
75 : void
76 4100417 : rtl_register_cfg_hooks (void)
77 : {
78 4100417 : cfg_hooks = &rtl_cfg_hooks;
79 4100417 : }
80 :
81 : /* Initialization of functions specific to the rtl IR. */
82 : void
83 2582405 : cfg_layout_rtl_register_cfg_hooks (void)
84 : {
85 2582405 : cfg_hooks = &cfg_layout_rtl_cfg_hooks;
86 2582405 : }
87 :
88 : /* Initialization of functions specific to the tree IR. */
89 :
90 : void
91 15118705 : gimple_register_cfg_hooks (void)
92 : {
93 15118705 : cfg_hooks = &gimple_cfg_hooks;
94 15118705 : }
95 :
96 : const struct cfg_hooks *
97 715 : get_cfg_hooks (void)
98 : {
99 715 : return cfg_hooks;
100 : }
101 :
102 : void
103 1430 : set_cfg_hooks (const struct cfg_hooks *new_cfg_hooks)
104 : {
105 1430 : cfg_hooks = new_cfg_hooks;
106 1430 : }
107 :
108 : /* Returns current ir type. */
109 :
110 : enum ir_type
111 340639332 : current_ir_type (void)
112 : {
113 340639332 : return cfg_hooks->ir;
114 : }
115 :
116 : static const char *
117 0 : current_ir_name (void)
118 : {
119 0 : enum ir_type ir = cfg_hooks->ir;
120 0 : switch (ir)
121 : {
122 : case IR_GIMPLE:
123 : return "gimple";
124 0 : case IR_RTL_CFGRTL:
125 0 : return "rtl";
126 0 : case IR_RTL_CFGLAYOUT:
127 0 : return "cfglayout mode";
128 0 : default:
129 0 : gcc_unreachable();
130 : }
131 : }
132 :
133 : /* Verify the CFG consistency.
134 :
135 : Currently it does following: checks edge and basic block list correctness
136 : and calls into IL dependent checking then. */
137 :
138 : DEBUG_FUNCTION void
139 337334001 : verify_flow_info (void)
140 : {
141 337334001 : size_t *edge_checksum;
142 337334001 : bool err = false;
143 337334001 : basic_block bb, last_bb_seen;
144 337334001 : basic_block *last_visited;
145 :
146 337334001 : timevar_push (TV_CFG_VERIFY);
147 337334001 : last_visited = XCNEWVEC (basic_block, last_basic_block_for_fn (cfun));
148 337334001 : edge_checksum = XCNEWVEC (size_t, last_basic_block_for_fn (cfun));
149 :
150 : /* Check bb chain & numbers. */
151 337334001 : last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
152 3808207500 : FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
153 : {
154 3470873499 : if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
155 3470873499 : && bb != BASIC_BLOCK_FOR_FN (cfun, bb->index))
156 : {
157 0 : error ("bb %d on wrong place", bb->index);
158 0 : err = true;
159 : }
160 :
161 3470873499 : if (bb->prev_bb != last_bb_seen)
162 : {
163 0 : error ("prev_bb of %d should be %d, not %d",
164 : bb->index, last_bb_seen->index, bb->prev_bb->index);
165 0 : err = true;
166 : }
167 :
168 3470873499 : last_bb_seen = bb;
169 : }
170 :
171 : /* Now check the basic blocks (boundaries etc.) */
172 3470873499 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
173 : {
174 3133539498 : int n_fallthru = 0;
175 3133539498 : edge e;
176 3133539498 : edge_iterator ei;
177 :
178 3133539498 : if (bb->loop_father != NULL && current_loops == NULL)
179 : {
180 0 : error ("verify_flow_info: Block %i has loop_father, but there are no loops",
181 : bb->index);
182 0 : err = true;
183 : }
184 3133539498 : if (bb->loop_father == NULL && current_loops != NULL)
185 : {
186 0 : error ("verify_flow_info: Block %i lacks loop_father", bb->index);
187 0 : err = true;
188 : }
189 :
190 3133539498 : if (!bb->count.verify ())
191 : {
192 0 : error ("verify_flow_info: Wrong count of block %i", bb->index);
193 0 : err = true;
194 : }
195 : /* FIXME: Graphite and SLJL and target code still tends to produce
196 : edges with no probability. */
197 3133539498 : if (profile_status_for_fn (cfun) >= PROFILE_GUESSED
198 : && !bb->count.initialized_p () && !flag_graphite && 0)
199 : {
200 : error ("verify_flow_info: Missing count of block %i", bb->index);
201 : err = true;
202 : }
203 :
204 3133539498 : if (bb->flags & ~cfun->cfg->bb_flags_allocated)
205 : {
206 0 : error ("verify_flow_info: unallocated flag set on BB %d", bb->index);
207 0 : err = true;
208 : }
209 :
210 7573993048 : FOR_EACH_EDGE (e, ei, bb->succs)
211 : {
212 4440453550 : if (last_visited [e->dest->index] == bb)
213 : {
214 0 : error ("verify_flow_info: Duplicate edge %i->%i",
215 0 : e->src->index, e->dest->index);
216 0 : err = true;
217 : }
218 : /* FIXME: Graphite and SLJL and target code still tends to produce
219 : edges with no probability. */
220 4440453550 : if (profile_status_for_fn (cfun) >= PROFILE_GUESSED
221 : && !e->probability.initialized_p () && !flag_graphite && 0)
222 : {
223 : error ("Uninitialized probability of edge %i->%i", e->src->index,
224 : e->dest->index);
225 : err = true;
226 : }
227 4440453550 : if (!e->probability.verify ())
228 : {
229 0 : error ("verify_flow_info: Wrong probability of edge %i->%i",
230 0 : e->src->index, e->dest->index);
231 0 : err = true;
232 : }
233 :
234 4440453550 : last_visited [e->dest->index] = bb;
235 :
236 4440453550 : if (e->flags & EDGE_FALLTHRU)
237 1699401340 : n_fallthru++;
238 :
239 4440453550 : if (e->src != bb)
240 : {
241 0 : error ("verify_flow_info: Basic block %d succ edge is corrupted",
242 : bb->index);
243 0 : fprintf (stderr, "Predecessor: ");
244 0 : dump_edge_info (stderr, e, TDF_DETAILS, 0);
245 0 : fprintf (stderr, "\nSuccessor: ");
246 0 : dump_edge_info (stderr, e, TDF_DETAILS, 1);
247 0 : fprintf (stderr, "\n");
248 0 : err = true;
249 : }
250 :
251 4440453550 : if (e->flags & ~cfun->cfg->edge_flags_allocated)
252 : {
253 0 : error ("verify_flow_info: unallocated edge flag set on %d -> %d",
254 0 : e->src->index, e->dest->index);
255 0 : err = true;
256 : }
257 :
258 4440453550 : edge_checksum[e->dest->index] += (size_t) e;
259 : }
260 3133539498 : if (n_fallthru > 1)
261 : {
262 0 : error ("wrong amount of branch edges after unconditional jump %i", bb->index);
263 0 : err = true;
264 : }
265 :
266 7571475082 : FOR_EACH_EDGE (e, ei, bb->preds)
267 : {
268 4437935584 : if (e->dest != bb)
269 : {
270 0 : error ("basic block %d pred edge is corrupted", bb->index);
271 0 : fputs ("Predecessor: ", stderr);
272 0 : dump_edge_info (stderr, e, TDF_DETAILS, 0);
273 0 : fputs ("\nSuccessor: ", stderr);
274 0 : dump_edge_info (stderr, e, TDF_DETAILS, 1);
275 0 : fputc ('\n', stderr);
276 0 : err = true;
277 : }
278 :
279 4437935584 : if (ei.index != e->dest_idx)
280 : {
281 0 : error ("basic block %d pred edge is corrupted", bb->index);
282 0 : error ("its dest_idx should be %d, not %d",
283 : ei.index, e->dest_idx);
284 0 : fputs ("Predecessor: ", stderr);
285 0 : dump_edge_info (stderr, e, TDF_DETAILS, 0);
286 0 : fputs ("\nSuccessor: ", stderr);
287 0 : dump_edge_info (stderr, e, TDF_DETAILS, 1);
288 0 : fputc ('\n', stderr);
289 0 : err = true;
290 : }
291 :
292 4437935584 : edge_checksum[e->dest->index] -= (size_t) e;
293 : }
294 : }
295 :
296 : /* Complete edge checksumming for ENTRY and EXIT. */
297 337334001 : {
298 337334001 : edge e;
299 337334001 : edge_iterator ei;
300 :
301 674668002 : FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
302 337334001 : edge_checksum[e->dest->index] += (size_t) e;
303 :
304 677185968 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
305 339851967 : edge_checksum[e->dest->index] -= (size_t) e;
306 : }
307 :
308 4145541501 : FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
309 3808207500 : if (edge_checksum[bb->index])
310 : {
311 0 : error ("basic block %i edge lists are corrupted", bb->index);
312 0 : err = true;
313 : }
314 :
315 : /* Clean up. */
316 337334001 : free (last_visited);
317 337334001 : free (edge_checksum);
318 :
319 337334001 : if (cfg_hooks->verify_flow_info)
320 337334001 : if (cfg_hooks->verify_flow_info ())
321 : err = true;
322 :
323 337334001 : if (err)
324 0 : internal_error ("verify_flow_info failed");
325 337334001 : timevar_pop (TV_CFG_VERIFY);
326 337334001 : }
327 :
328 : /* Print out one basic block BB to file OUTF. INDENT is printed at the
329 : start of each new line. FLAGS are the TDF_* flags in dumpfile.h.
330 :
331 : This function takes care of the purely graph related information.
332 : The cfg hook for the active representation should dump
333 : representation-specific information. */
334 :
335 : void
336 318359 : dump_bb (FILE *outf, basic_block bb, int indent, dump_flags_t flags)
337 : {
338 318359 : if (flags & TDF_BLOCKS)
339 48517 : dump_bb_info (outf, bb, indent, flags, true, false);
340 318359 : if (cfg_hooks->dump_bb)
341 318359 : cfg_hooks->dump_bb (outf, bb, indent, flags);
342 318359 : if (flags & TDF_BLOCKS)
343 48517 : dump_bb_info (outf, bb, indent, flags, false, true);
344 318359 : fputc ('\n', outf);
345 318359 : }
346 :
347 : DEBUG_FUNCTION void
348 0 : debug (basic_block_def &ref)
349 : {
350 0 : dump_bb (stderr, &ref, 0, TDF_NONE);
351 0 : }
352 :
353 : DEBUG_FUNCTION void
354 0 : debug (basic_block_def *ptr)
355 : {
356 0 : if (ptr)
357 0 : debug (*ptr);
358 : else
359 0 : fprintf (stderr, "<nil>\n");
360 0 : }
361 :
362 : static void
363 0 : debug_slim (basic_block ptr)
364 : {
365 0 : fprintf (stderr, "<basic_block %p (%d)>", (void *) ptr, ptr->index);
366 0 : }
367 :
368 0 : DEFINE_DEBUG_VEC (basic_block_def *)
369 0 : DEFINE_DEBUG_HASH_SET (basic_block_def *)
370 :
371 : /* Dumps basic block BB to pretty-printer PP, for use as a label of
372 : a DOT graph record-node. The implementation of this hook is
373 : expected to write the label to the stream that is attached to PP.
374 : Field separators between instructions are pipe characters printed
375 : verbatim. Instructions should be written with some characters
376 : escaped, using pp_write_text_as_dot_label_to_stream(). */
377 :
378 : void
379 450 : dump_bb_for_graph (pretty_printer *pp, basic_block bb)
380 : {
381 450 : if (!cfg_hooks->dump_bb_for_graph)
382 0 : internal_error ("%s does not support dump_bb_for_graph",
383 : current_ir_name ());
384 : /* TODO: Add pretty printer for counter. */
385 450 : if (bb->count.initialized_p ())
386 351 : pp_printf (pp, "COUNT:" "%" PRId64, bb->count.to_gcov_type ());
387 450 : pp_write_text_to_stream (pp);
388 450 : if (!(dump_flags & TDF_SLIM))
389 450 : cfg_hooks->dump_bb_for_graph (pp, bb);
390 450 : }
391 :
392 : void
393 540 : dump_bb_as_sarif_properties (diagnostics::sarif_builder *builder,
394 : json::object &output_bag,
395 : basic_block bb)
396 : {
397 540 : if (!cfg_hooks->dump_bb_for_graph)
398 0 : internal_error ("%s does not support dump_bb_as_sarif_properties",
399 : current_ir_name ());
400 540 : namespace bb_property_names = custom_sarif_properties::cfg::basic_block;
401 540 : if (bb->index == ENTRY_BLOCK)
402 90 : output_bag.set_string (bb_property_names::kind, "entry");
403 450 : else if (bb->index == EXIT_BLOCK)
404 90 : output_bag.set_string (bb_property_names::kind, "exit");
405 360 : else if (BB_PARTITION (bb) == BB_HOT_PARTITION)
406 0 : output_bag.set_string (bb_property_names::kind, "hot");
407 360 : else if (BB_PARTITION (bb) == BB_COLD_PARTITION)
408 0 : output_bag.set_string (bb_property_names::kind, "cold");
409 540 : if (bb->count.initialized_p ())
410 : {
411 0 : pretty_printer pp;
412 0 : pp_printf (&pp, "%" PRId64, bb->count.to_gcov_type ());
413 0 : output_bag.set_string (bb_property_names::count,
414 : pp_formatted_text (&pp));
415 0 : }
416 540 : cfg_hooks->dump_bb_as_sarif_properties (builder, output_bag, bb);
417 540 : }
418 :
419 : /* Dump the complete CFG to FILE. FLAGS are the TDF_* flags in dumpfile.h. */
420 : void
421 291 : dump_flow_info (FILE *file, dump_flags_t flags)
422 : {
423 291 : basic_block bb;
424 :
425 291 : fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks_for_fn (cfun),
426 291 : n_edges_for_fn (cfun));
427 2612 : FOR_ALL_BB_FN (bb, cfun)
428 2321 : dump_bb (file, bb, 0, flags);
429 :
430 291 : putc ('\n', file);
431 291 : }
432 :
433 : /* Like above, but dump to stderr. To be called from debuggers. */
434 : void debug_flow_info (void);
435 : DEBUG_FUNCTION void
436 0 : debug_flow_info (void)
437 : {
438 0 : dump_flow_info (stderr, TDF_DETAILS);
439 0 : }
440 :
441 : /* Redirect edge E to the given basic block DEST and update underlying program
442 : representation. Returns edge representing redirected branch (that may not
443 : be equivalent to E in the case of duplicate edges being removed) or NULL
444 : if edge is not easily redirectable for whatever reason. */
445 :
446 : edge
447 77651970 : redirect_edge_and_branch (edge e, basic_block dest)
448 : {
449 77651970 : edge ret;
450 :
451 77651970 : if (!cfg_hooks->redirect_edge_and_branch)
452 0 : internal_error ("%s does not support redirect_edge_and_branch",
453 : current_ir_name ());
454 :
455 77651970 : ret = cfg_hooks->redirect_edge_and_branch (e, dest);
456 :
457 : /* If RET != E, then either the redirection failed, or the edge E
458 : was removed since RET already lead to the same destination. */
459 77651970 : if (current_loops != NULL && ret == e)
460 64822638 : rescan_loop_exit (e, false, false);
461 :
462 77651970 : return ret;
463 : }
464 :
465 : /* Returns true if it is possible to remove the edge E by redirecting it
466 : to the destination of the other edge going from its source. */
467 :
468 : bool
469 463542 : can_remove_branch_p (const_edge e)
470 : {
471 463542 : if (!cfg_hooks->can_remove_branch_p)
472 0 : internal_error ("%s does not support can_remove_branch_p",
473 : current_ir_name ());
474 :
475 463542 : if (EDGE_COUNT (e->src->succs) != 2)
476 : return false;
477 :
478 463542 : return cfg_hooks->can_remove_branch_p (e);
479 : }
480 :
481 : /* Removes E, by redirecting it to the destination of the other edge going
482 : from its source. Can_remove_branch_p must be true for E, hence this
483 : operation cannot fail. */
484 :
485 : void
486 463602 : remove_branch (edge e)
487 : {
488 463602 : edge other;
489 463602 : basic_block src = e->src;
490 463602 : int irr;
491 :
492 463602 : gcc_assert (EDGE_COUNT (e->src->succs) == 2);
493 :
494 463602 : other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
495 463602 : irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
496 :
497 463602 : e = redirect_edge_and_branch (e, other->dest);
498 463602 : gcc_assert (e != NULL);
499 :
500 463602 : e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
501 463602 : e->flags |= irr;
502 463602 : }
503 :
504 : /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
505 :
506 : void
507 87673226 : remove_edge (edge e)
508 : {
509 87673226 : if (current_loops != NULL)
510 : {
511 78194238 : rescan_loop_exit (e, false, true);
512 :
513 : /* Removal of an edge inside an irreducible region or which leads
514 : to an irreducible region can turn the region into a natural loop.
515 : In that case, ask for the loop structure fixups.
516 :
517 : FIXME: Note that LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS is not always
518 : set, so always ask for fixups when removing an edge in that case. */
519 78194238 : if (!loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
520 8229706 : || (e->flags & EDGE_IRREDUCIBLE_LOOP)
521 86420556 : || (e->dest->flags & BB_IRREDUCIBLE_LOOP))
522 69968737 : loops_state_set (LOOPS_NEED_FIXUP);
523 : }
524 :
525 : /* This is probably not needed, but it doesn't hurt. */
526 : /* FIXME: This should be called via a remove_edge hook. */
527 87673226 : if (current_ir_type () == IR_GIMPLE)
528 71163012 : redirect_edge_var_map_clear (e);
529 :
530 87673226 : remove_edge_raw (e);
531 87673226 : }
532 :
533 : /* Like redirect_edge_succ but avoid possible duplicate edge. */
534 :
535 : edge
536 83413599 : redirect_edge_succ_nodup (edge e, basic_block new_succ)
537 : {
538 83413599 : edge s;
539 :
540 83413599 : s = find_edge (e->src, new_succ);
541 83413599 : if (s && s != e)
542 : {
543 449191 : s->flags |= e->flags;
544 449191 : s->probability += e->probability;
545 : /* FIXME: This should be called via a hook and only for IR_GIMPLE. */
546 449191 : redirect_edge_var_map_dup (s, e);
547 449191 : remove_edge (e);
548 449191 : e = s;
549 : }
550 : else
551 82964408 : redirect_edge_succ (e, new_succ);
552 :
553 83413599 : return e;
554 : }
555 :
556 : /* Redirect the edge E to basic block DEST even if it requires creating
557 : of a new basic block; then it returns the newly created basic block.
558 : Aborts when redirection is impossible. */
559 :
560 : basic_block
561 9492979 : redirect_edge_and_branch_force (edge e, basic_block dest)
562 : {
563 9492979 : basic_block ret, src = e->src;
564 :
565 9492979 : if (!cfg_hooks->redirect_edge_and_branch_force)
566 0 : internal_error ("%s does not support redirect_edge_and_branch_force",
567 : current_ir_name ());
568 :
569 9492979 : if (current_loops != NULL)
570 7777508 : rescan_loop_exit (e, false, true);
571 :
572 9492979 : ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
573 :
574 9492979 : if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
575 43927 : set_immediate_dominator (CDI_DOMINATORS, ret, src);
576 :
577 9492979 : if (current_loops != NULL)
578 : {
579 7777508 : if (ret != NULL)
580 : {
581 4 : class loop *loop
582 4 : = find_common_loop (single_pred (ret)->loop_father,
583 4 : single_succ (ret)->loop_father);
584 4 : add_bb_to_loop (ret, loop);
585 : }
586 7777504 : else if (find_edge (src, dest) == e)
587 7777502 : rescan_loop_exit (e, true, false);
588 : }
589 :
590 9492979 : return ret;
591 : }
592 :
593 : /* Splits basic block BB after the specified instruction I (but at least after
594 : the labels). If I is NULL, splits just after labels. The newly created edge
595 : is returned. The new basic block is created just after the old one. */
596 :
597 : static edge
598 8943287 : split_block_1 (basic_block bb, void *i)
599 : {
600 8943287 : basic_block new_bb;
601 8943287 : edge res;
602 :
603 8943287 : if (!cfg_hooks->split_block)
604 0 : internal_error ("%s does not support split_block", current_ir_name ());
605 :
606 8943287 : new_bb = cfg_hooks->split_block (bb, i);
607 8943287 : if (!new_bb)
608 : return NULL;
609 :
610 8943287 : new_bb->count = bb->count;
611 :
612 8943287 : if (dom_info_available_p (CDI_DOMINATORS))
613 : {
614 698184 : redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
615 698184 : set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
616 : }
617 :
618 8943287 : if (current_loops != NULL)
619 : {
620 7975050 : edge_iterator ei;
621 7975050 : edge e;
622 7975050 : add_bb_to_loop (new_bb, bb->loop_father);
623 : /* Identify all loops bb may have been the latch of and adjust them. */
624 19699952 : FOR_EACH_EDGE (e, ei, new_bb->succs)
625 11724902 : if (e->dest->loop_father->latch == bb)
626 128874 : e->dest->loop_father->latch = new_bb;
627 : }
628 :
629 8943287 : res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
630 :
631 8943287 : if (bb->flags & BB_IRREDUCIBLE_LOOP)
632 : {
633 6949 : new_bb->flags |= BB_IRREDUCIBLE_LOOP;
634 6949 : res->flags |= EDGE_IRREDUCIBLE_LOOP;
635 : }
636 :
637 : return res;
638 : }
639 :
640 : edge
641 5785809 : split_block (basic_block bb, gimple *i)
642 : {
643 5785809 : return split_block_1 (bb, i);
644 : }
645 :
646 : edge
647 3033058 : split_block (basic_block bb, rtx i)
648 : {
649 3033058 : return split_block_1 (bb, i);
650 : }
651 :
652 : /* Splits block BB just after labels. The newly created edge is returned. */
653 :
654 : edge
655 124420 : split_block_after_labels (basic_block bb)
656 : {
657 124420 : return split_block_1 (bb, NULL);
658 : }
659 :
660 : /* Moves block BB immediately after block AFTER. Returns false if the
661 : movement was impossible. */
662 :
663 : bool
664 24771797 : move_block_after (basic_block bb, basic_block after)
665 : {
666 24771797 : bool ret;
667 :
668 24771797 : if (!cfg_hooks->move_block_after)
669 0 : internal_error ("%s does not support move_block_after",
670 : current_ir_name ());
671 :
672 24771797 : ret = cfg_hooks->move_block_after (bb, after);
673 :
674 24771797 : return ret;
675 : }
676 :
677 : /* Deletes the basic block BB. */
678 :
679 : void
680 44382957 : delete_basic_block (basic_block bb)
681 : {
682 44382957 : if (!cfg_hooks->delete_basic_block)
683 0 : internal_error ("%s does not support delete_basic_block",
684 : current_ir_name ());
685 :
686 44382957 : cfg_hooks->delete_basic_block (bb);
687 :
688 44382957 : if (current_loops != NULL)
689 : {
690 39209877 : class loop *loop = bb->loop_father;
691 :
692 : /* If we remove the header or the latch of a loop, mark the loop for
693 : removal. */
694 39209877 : if (loop->latch == bb
695 38996935 : || loop->header == bb)
696 256938 : mark_loop_for_removal (loop);
697 :
698 39209877 : remove_bb_from_loops (bb);
699 : }
700 :
701 : /* Remove the edges into and out of this block. Note that there may
702 : indeed be edges in, if we are removing an unreachable loop. */
703 89469944 : while (EDGE_COUNT (bb->preds) != 0)
704 689526 : remove_edge (EDGE_PRED (bb, 0));
705 51298077 : while (EDGE_COUNT (bb->succs) != 0)
706 6915120 : remove_edge (EDGE_SUCC (bb, 0));
707 :
708 44382957 : if (dom_info_available_p (CDI_DOMINATORS))
709 31148565 : delete_from_dominance_info (CDI_DOMINATORS, bb);
710 44382957 : if (dom_info_available_p (CDI_POST_DOMINATORS))
711 443912 : delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
712 :
713 : /* Remove the basic block from the array. */
714 44382957 : expunge_block (bb);
715 44382957 : }
716 :
717 : /* Splits edge E and returns the newly created basic block. */
718 :
719 : basic_block
720 31102668 : split_edge (edge e)
721 : {
722 31102668 : basic_block ret;
723 31102668 : profile_count count = e->count ();
724 31102668 : edge f;
725 31102668 : bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
726 31102668 : bool back = (e->flags & EDGE_DFS_BACK) != 0;
727 31102668 : class loop *loop;
728 31102668 : basic_block src = e->src, dest = e->dest;
729 :
730 31102668 : if (!cfg_hooks->split_edge)
731 0 : internal_error ("%s does not support split_edge", current_ir_name ());
732 :
733 31102668 : if (current_loops != NULL)
734 29605010 : rescan_loop_exit (e, false, true);
735 :
736 31102668 : ret = cfg_hooks->split_edge (e);
737 31102668 : ret->count = count;
738 31102668 : single_succ_edge (ret)->probability = profile_probability::always ();
739 :
740 31102668 : if (irr)
741 : {
742 79098 : ret->flags |= BB_IRREDUCIBLE_LOOP;
743 79098 : single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
744 79098 : single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
745 : }
746 31102668 : if (back)
747 : {
748 1835185 : single_pred_edge (ret)->flags &= ~EDGE_DFS_BACK;
749 1835185 : single_succ_edge (ret)->flags |= EDGE_DFS_BACK;
750 : }
751 :
752 31102668 : if (dom_info_available_p (CDI_DOMINATORS))
753 26848339 : set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
754 :
755 31102668 : if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
756 : {
757 : /* There are two cases:
758 :
759 : If the immediate dominator of e->dest is not e->src, it
760 : remains unchanged.
761 :
762 : If immediate dominator of e->dest is e->src, it may become
763 : ret, provided that all other predecessors of e->dest are
764 : dominated by e->dest. */
765 :
766 26848339 : if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
767 26848339 : == single_pred (ret))
768 : {
769 9107547 : edge_iterator ei;
770 19999565 : FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
771 : {
772 15730896 : if (f == single_succ_edge (ret))
773 6886060 : continue;
774 :
775 8844836 : if (!dominated_by_p (CDI_DOMINATORS, f->src,
776 8844836 : single_succ (ret)))
777 : break;
778 : }
779 :
780 9107547 : if (!f)
781 4268669 : set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
782 : }
783 : }
784 :
785 31102668 : if (current_loops != NULL)
786 : {
787 29605010 : loop = find_common_loop (src->loop_father, dest->loop_father);
788 29605010 : add_bb_to_loop (ret, loop);
789 :
790 : /* If we split the latch edge of loop adjust the latch block. */
791 29605010 : if (loop->latch == src
792 7109085 : && loop->header == dest)
793 7108379 : loop->latch = ret;
794 : }
795 :
796 31102668 : return ret;
797 : }
798 :
799 : /* Creates a new basic block just after the basic block AFTER.
800 : HEAD and END are the first and the last statement belonging
801 : to the block. If both are NULL, an empty block is created. */
802 :
803 : static basic_block
804 87179468 : create_basic_block_1 (void *head, void *end, basic_block after)
805 : {
806 87179468 : basic_block ret;
807 :
808 87179468 : if (!cfg_hooks->create_basic_block)
809 0 : internal_error ("%s does not support create_basic_block",
810 : current_ir_name ());
811 :
812 87179468 : ret = cfg_hooks->create_basic_block (head, end, after);
813 :
814 87179468 : if (dom_info_available_p (CDI_DOMINATORS))
815 32759482 : add_to_dominance_info (CDI_DOMINATORS, ret);
816 87179468 : if (dom_info_available_p (CDI_POST_DOMINATORS))
817 169828 : add_to_dominance_info (CDI_POST_DOMINATORS, ret);
818 :
819 87179468 : return ret;
820 : }
821 :
822 : basic_block
823 37328045 : create_basic_block (gimple_seq seq, basic_block after)
824 : {
825 37328045 : return create_basic_block_1 (seq, NULL, after);
826 : }
827 :
828 : basic_block
829 13415588 : create_basic_block (rtx head, rtx end, basic_block after)
830 : {
831 13415588 : return create_basic_block_1 (head, end, after);
832 : }
833 :
834 :
835 : /* Creates an empty basic block just after basic block AFTER. */
836 :
837 : basic_block
838 36435835 : create_empty_bb (basic_block after)
839 : {
840 36435835 : return create_basic_block_1 (NULL, NULL, after);
841 : }
842 :
843 : /* Checks whether we may merge blocks BB1 and BB2. */
844 :
845 : bool
846 434227899 : can_merge_blocks_p (basic_block bb1, basic_block bb2)
847 : {
848 434227899 : bool ret;
849 :
850 434227899 : if (!cfg_hooks->can_merge_blocks_p)
851 0 : internal_error ("%s does not support can_merge_blocks_p",
852 : current_ir_name ());
853 :
854 434227899 : ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
855 :
856 434227899 : return ret;
857 : }
858 :
859 : void
860 6435004 : predict_edge (edge e, enum br_predictor predictor, int probability)
861 : {
862 6435004 : if (!cfg_hooks->predict_edge)
863 0 : internal_error ("%s does not support predict_edge",
864 : current_ir_name ());
865 :
866 6435004 : cfg_hooks->predict_edge (e, predictor, probability);
867 6435004 : }
868 :
869 : bool
870 3273617 : predicted_by_p (const_basic_block bb, enum br_predictor predictor)
871 : {
872 3273617 : if (!cfg_hooks->predict_edge)
873 0 : internal_error ("%s does not support predicted_by_p",
874 : current_ir_name ());
875 :
876 3273617 : return cfg_hooks->predicted_by_p (bb, predictor);
877 : }
878 :
879 : /* Merges basic block B into basic block A. */
880 :
881 : void
882 22612942 : merge_blocks (basic_block a, basic_block b)
883 : {
884 22612942 : edge e;
885 22612942 : edge_iterator ei;
886 :
887 22612942 : if (!cfg_hooks->merge_blocks)
888 0 : internal_error ("%s does not support merge_blocks",
889 : current_ir_name ());
890 :
891 : /* Pick the more reliable count. If both qualities agrees, pick the larger
892 : one since turning mistakely hot code to cold is more harmful. */
893 22612942 : if (!a->count.initialized_p ())
894 12273099 : a->count = b->count;
895 10339843 : else if (a->count.quality () < b->count.quality ())
896 5270 : a->count = b->count;
897 10334573 : else if (a->count.quality () == b->count.quality ())
898 10328551 : a->count = profile_count::max_prefer_initialized (a->count, b->count);
899 :
900 22612942 : cfg_hooks->merge_blocks (a, b);
901 :
902 22612942 : if (current_loops != NULL)
903 : {
904 : /* If the block we merge into is a loop header do nothing unless ... */
905 19303698 : if (a->loop_father->header == a)
906 : {
907 : /* ... we merge two loop headers, in which case we kill
908 : the inner loop. */
909 662197 : if (b->loop_father->header == b)
910 423 : mark_loop_for_removal (b->loop_father);
911 : }
912 : /* If we merge a loop header into its predecessor, update the loop
913 : structure. */
914 18641501 : else if (b->loop_father->header == b)
915 : {
916 84 : remove_bb_from_loops (a);
917 84 : add_bb_to_loop (a, b->loop_father);
918 84 : a->loop_father->header = a;
919 : }
920 : /* If we merge a loop latch into its predecessor, update the loop
921 : structure. */
922 19303698 : if (b->loop_father->latch
923 19164634 : && b->loop_father->latch == b)
924 266058 : b->loop_father->latch = a;
925 19303698 : remove_bb_from_loops (b);
926 : }
927 :
928 : /* Normally there should only be one successor of A and that is B, but
929 : partway though the merge of blocks for conditional_execution we'll
930 : be merging a TEST block with THEN and ELSE successors. Free the
931 : whole lot of them and hope the caller knows what they're doing. */
932 :
933 45225884 : while (EDGE_COUNT (a->succs) != 0)
934 22612942 : remove_edge (EDGE_SUCC (a, 0));
935 :
936 : /* Adjust the edges out of B for the new owner. */
937 51349471 : FOR_EACH_EDGE (e, ei, b->succs)
938 : {
939 28736529 : e->src = a;
940 28736529 : if (current_loops != NULL)
941 : {
942 : /* If b was a latch, a now is. */
943 25013949 : if (e->dest->loop_father->latch == b)
944 711 : e->dest->loop_father->latch = a;
945 25013949 : rescan_loop_exit (e, true, false);
946 : }
947 : }
948 22612942 : a->succs = b->succs;
949 22612942 : a->flags |= b->flags;
950 :
951 : /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
952 22612942 : b->preds = b->succs = NULL;
953 :
954 22612942 : if (dom_info_available_p (CDI_DOMINATORS))
955 17881929 : redirect_immediate_dominators (CDI_DOMINATORS, b, a);
956 :
957 22612942 : if (dom_info_available_p (CDI_DOMINATORS))
958 17881929 : delete_from_dominance_info (CDI_DOMINATORS, b);
959 22612942 : if (dom_info_available_p (CDI_POST_DOMINATORS))
960 134685 : delete_from_dominance_info (CDI_POST_DOMINATORS, b);
961 :
962 22612942 : expunge_block (b);
963 22612942 : }
964 :
965 : /* Split BB into entry part and the rest (the rest is the newly created block).
966 : Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
967 : part. Returns the edge connecting the entry part to the rest.
968 : DATA gets passed on to REDIRECT_EDGE_P. */
969 :
970 : edge
971 99530 : make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge, void*), void *data)
972 : {
973 99530 : edge e, fallthru;
974 99530 : edge_iterator ei;
975 99530 : basic_block dummy, jump;
976 99530 : class loop *loop, *ploop, *cloop;
977 :
978 99530 : if (!cfg_hooks->make_forwarder_block)
979 0 : internal_error ("%s does not support make_forwarder_block",
980 : current_ir_name ());
981 :
982 99530 : fallthru = split_block_after_labels (bb);
983 99530 : dummy = fallthru->src;
984 99530 : dummy->count = profile_count::zero ();
985 99530 : bb = fallthru->dest;
986 :
987 : /* Redirect back edges we want to keep. */
988 431407 : for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
989 : {
990 331877 : basic_block e_src;
991 :
992 331877 : if (redirect_edge_p (e, data))
993 : {
994 229165 : dummy->count += e->count ();
995 229165 : ei_next (&ei);
996 229165 : continue;
997 : }
998 :
999 102712 : e_src = e->src;
1000 102712 : jump = redirect_edge_and_branch_force (e, bb);
1001 102712 : if (jump != NULL)
1002 : {
1003 : /* If we redirected the loop latch edge, the JUMP block now acts like
1004 : the new latch of the loop. */
1005 3 : if (current_loops != NULL
1006 3 : && dummy->loop_father != NULL
1007 3 : && dummy->loop_father->header == dummy
1008 3 : && dummy->loop_father->latch == e_src)
1009 0 : dummy->loop_father->latch = jump;
1010 : }
1011 : }
1012 :
1013 99530 : if (dom_info_available_p (CDI_DOMINATORS))
1014 : {
1015 57670 : vec<basic_block> doms_to_fix;
1016 57670 : doms_to_fix.create (2);
1017 57670 : doms_to_fix.quick_push (dummy);
1018 57670 : doms_to_fix.quick_push (bb);
1019 57670 : iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
1020 57670 : doms_to_fix.release ();
1021 : }
1022 :
1023 99530 : if (current_loops != NULL)
1024 : {
1025 : /* If we do not split a loop header, then both blocks belong to the
1026 : same loop. In case we split loop header and do not redirect the
1027 : latch edge to DUMMY, then DUMMY belongs to the outer loop, and
1028 : BB becomes the new header. If latch is not recorded for the loop,
1029 : we leave this updating on the caller (this may only happen during
1030 : loop analysis). */
1031 99530 : loop = dummy->loop_father;
1032 99530 : if (loop->header == dummy
1033 99378 : && loop->latch != NULL
1034 167377 : && find_edge (loop->latch, dummy) == NULL)
1035 : {
1036 67802 : remove_bb_from_loops (dummy);
1037 67802 : loop->header = bb;
1038 :
1039 67802 : cloop = loop;
1040 220610 : FOR_EACH_EDGE (e, ei, dummy->preds)
1041 : {
1042 152808 : cloop = find_common_loop (cloop, e->src->loop_father);
1043 : }
1044 67802 : add_bb_to_loop (dummy, cloop);
1045 : }
1046 :
1047 : /* In case we split loop latch, update it. */
1048 333018 : for (ploop = loop; ploop; ploop = loop_outer (ploop))
1049 233488 : if (ploop->latch == dummy)
1050 0 : ploop->latch = bb;
1051 : }
1052 :
1053 99530 : cfg_hooks->make_forwarder_block (fallthru);
1054 :
1055 99530 : return fallthru;
1056 : }
1057 :
1058 : /* Try to make the edge fallthru. */
1059 :
1060 : void
1061 832937 : tidy_fallthru_edge (edge e)
1062 : {
1063 832937 : if (cfg_hooks->tidy_fallthru_edge)
1064 832937 : cfg_hooks->tidy_fallthru_edge (e);
1065 832937 : }
1066 :
1067 : /* Fix up edges that now fall through, or rather should now fall through
1068 : but previously required a jump around now deleted blocks. Simplify
1069 : the search by only examining blocks numerically adjacent, since this
1070 : is how they were created.
1071 :
1072 : ??? This routine is currently RTL specific. */
1073 :
1074 : void
1075 476645 : tidy_fallthru_edges (void)
1076 : {
1077 476645 : basic_block b, c;
1078 :
1079 476645 : if (!cfg_hooks->tidy_fallthru_edge)
1080 : return;
1081 :
1082 130466 : if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1083 : return;
1084 :
1085 4337621 : FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
1086 : EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, next_bb)
1087 : {
1088 4207155 : edge s;
1089 :
1090 4207155 : c = b->next_bb;
1091 :
1092 : /* We care about simple conditional or unconditional jumps with
1093 : a single successor.
1094 :
1095 : If we had a conditional branch to the next instruction when
1096 : CFG was built, then there will only be one out edge for the
1097 : block which ended with the conditional branch (since we do
1098 : not create duplicate edges).
1099 :
1100 : Furthermore, the edge will be marked as a fallthru because we
1101 : merge the flags for the duplicate edges. So we do not want to
1102 : check that the edge is not a FALLTHRU edge. */
1103 :
1104 6194846 : if (single_succ_p (b))
1105 : {
1106 1987691 : s = single_succ_edge (b);
1107 1987691 : if (! (s->flags & EDGE_COMPLEX)
1108 1906009 : && s->dest == c
1109 2832865 : && !(JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b))))
1110 825427 : tidy_fallthru_edge (s);
1111 : }
1112 : }
1113 : }
1114 :
1115 : /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1116 : (and possibly create new basic block) to make edge non-fallthru.
1117 : Return newly created BB or NULL if none. */
1118 :
1119 : basic_block
1120 769265 : force_nonfallthru (edge e)
1121 : {
1122 769265 : basic_block ret, src = e->src;
1123 :
1124 769265 : if (!cfg_hooks->force_nonfallthru)
1125 0 : internal_error ("%s does not support force_nonfallthru",
1126 : current_ir_name ());
1127 :
1128 769265 : ret = cfg_hooks->force_nonfallthru (e);
1129 769265 : if (ret != NULL)
1130 : {
1131 178239 : if (dom_info_available_p (CDI_DOMINATORS))
1132 9757 : set_immediate_dominator (CDI_DOMINATORS, ret, src);
1133 :
1134 178239 : if (current_loops != NULL)
1135 : {
1136 65662 : basic_block pred = single_pred (ret);
1137 65662 : basic_block succ = single_succ (ret);
1138 65662 : class loop *loop
1139 65662 : = find_common_loop (pred->loop_father, succ->loop_father);
1140 65662 : rescan_loop_exit (e, false, true);
1141 65662 : add_bb_to_loop (ret, loop);
1142 :
1143 : /* If we split the latch edge of loop adjust the latch block. */
1144 65662 : if (loop->latch == pred
1145 4933 : && loop->header == succ)
1146 4933 : loop->latch = ret;
1147 : }
1148 : }
1149 :
1150 769265 : return ret;
1151 : }
1152 :
1153 : /* Returns true if we can duplicate basic block BB. */
1154 :
1155 : bool
1156 24727851 : can_duplicate_block_p (const_basic_block bb)
1157 : {
1158 24727851 : if (!cfg_hooks->can_duplicate_block_p)
1159 0 : internal_error ("%s does not support can_duplicate_block_p",
1160 : current_ir_name ());
1161 :
1162 24727851 : if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1163 : return false;
1164 :
1165 24727851 : return cfg_hooks->can_duplicate_block_p (bb);
1166 : }
1167 :
1168 : /* Returns true if we can duplicate E's destination with E redirected to the
1169 : copy. */
1170 :
1171 : bool
1172 6350170 : can_duplicate_block_on_edge_p (edge e)
1173 : {
1174 6350170 : basic_block bb = e->dest;
1175 :
1176 6350170 : if (!can_duplicate_block_p (bb))
1177 : return false;
1178 :
1179 6350170 : if (e->flags & EDGE_COMPLEX)
1180 : return false;
1181 :
1182 6350170 : edge s;
1183 6350170 : edge_iterator ei;
1184 18855285 : FOR_EACH_EDGE (s, ei, bb->succs)
1185 12505530 : if (s->flags & EDGE_COMPLEX)
1186 : return false;
1187 :
1188 : return true;
1189 : }
1190 :
1191 : /* Duplicate basic block BB, place it after AFTER (if non-null) and redirect
1192 : edge E to it (if non-null). Return the new basic block.
1193 :
1194 : If BB contains a returns_twice call, the caller is responsible for recreating
1195 : incoming abnormal edges corresponding to the "second return" for the copy.
1196 : gimple_can_duplicate_bb_p rejects such blocks, while RTL likes to live
1197 : dangerously.
1198 :
1199 : If BB has incoming abnormal edges for some other reason, their destinations
1200 : should be tied to label(s) of the original BB and not the copy. */
1201 :
1202 : basic_block
1203 4801733 : duplicate_block (basic_block bb, edge e, basic_block after, copy_bb_data *id)
1204 : {
1205 4801733 : edge s, n;
1206 4801733 : basic_block new_bb;
1207 5131342 : profile_count new_count = e ? e->count (): profile_count::uninitialized ();
1208 4801733 : edge_iterator ei;
1209 :
1210 4801733 : if (!cfg_hooks->duplicate_block)
1211 0 : internal_error ("%s does not support duplicate_block",
1212 : current_ir_name ());
1213 :
1214 4801733 : if (bb->count < new_count)
1215 1995 : new_count = bb->count;
1216 :
1217 4801733 : gcc_checking_assert (can_duplicate_block_p (bb));
1218 :
1219 4801733 : new_bb = cfg_hooks->duplicate_block (bb, id);
1220 4801733 : if (after)
1221 4480964 : move_block_after (new_bb, after);
1222 :
1223 4801733 : new_bb->flags = (bb->flags & ~BB_DUPLICATED);
1224 13256984 : FOR_EACH_EDGE (s, ei, bb->succs)
1225 : {
1226 : /* Since we are creating edges from a new block to successors
1227 : of another block (which therefore are known to be disjoint), there
1228 : is no need to actually check for duplicated edges. */
1229 8455251 : n = unchecked_make_edge (new_bb, s->dest, s->flags);
1230 8455251 : n->probability = s->probability;
1231 8455251 : n->aux = s->aux;
1232 : }
1233 :
1234 4801733 : if (e)
1235 : {
1236 329609 : new_bb->count = new_count;
1237 329609 : bb->count -= new_count;
1238 :
1239 329609 : redirect_edge_and_branch_force (e, new_bb);
1240 : }
1241 : else
1242 4472124 : new_bb->count = bb->count;
1243 :
1244 4801733 : set_bb_original (new_bb, bb);
1245 4801733 : set_bb_copy (bb, new_bb);
1246 :
1247 : /* Add the new block to the copy of the loop of BB, or directly to the loop
1248 : of BB if the loop is not being copied. */
1249 4801733 : if (current_loops != NULL)
1250 : {
1251 4495865 : class loop *cloop = bb->loop_father;
1252 4495865 : class loop *copy = get_loop_copy (cloop);
1253 : /* If we copied the loop header block but not the loop
1254 : we have created a loop with multiple entries. Ditch the loop,
1255 : add the new block to the outer loop and arrange for a fixup. */
1256 4495865 : if (!copy
1257 380223 : && cloop->header == bb)
1258 : {
1259 2733 : add_bb_to_loop (new_bb, loop_outer (cloop));
1260 2733 : mark_loop_for_removal (cloop);
1261 : }
1262 : else
1263 : {
1264 4493132 : add_bb_to_loop (new_bb, copy ? copy : cloop);
1265 : /* If we copied the loop latch block but not the loop, adjust
1266 : loop state. */
1267 4493132 : if (!copy
1268 377490 : && cloop->latch == bb)
1269 : {
1270 1696 : cloop->latch = NULL;
1271 1696 : loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1272 : }
1273 : }
1274 : }
1275 :
1276 : /* Skip if copy_bbs assigns one copyid for the whole region. */
1277 4801733 : if (!id || !id->skip_copyid)
1278 : {
1279 648416 : location_t loc = copyid_location_from_bb (bb);
1280 648416 : if (loc != UNKNOWN_LOCATION)
1281 620865 : assign_discriminators_to_bb (new_bb, 0,
1282 : allocate_copyid_base (loc, 1));
1283 : }
1284 :
1285 4801733 : return new_bb;
1286 : }
1287 :
1288 : /* Return 1 if BB ends with a call, possibly followed by some
1289 : instructions that must stay with the call, 0 otherwise. */
1290 :
1291 : bool
1292 9982796 : block_ends_with_call_p (basic_block bb)
1293 : {
1294 9982796 : if (!cfg_hooks->block_ends_with_call_p)
1295 0 : internal_error ("%s does not support block_ends_with_call_p",
1296 : current_ir_name ());
1297 :
1298 9982796 : return (cfg_hooks->block_ends_with_call_p) (bb);
1299 : }
1300 :
1301 : /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1302 :
1303 : bool
1304 1805 : block_ends_with_condjump_p (const_basic_block bb)
1305 : {
1306 1805 : if (!cfg_hooks->block_ends_with_condjump_p)
1307 0 : internal_error ("%s does not support block_ends_with_condjump_p",
1308 : current_ir_name ());
1309 :
1310 1805 : return (cfg_hooks->block_ends_with_condjump_p) (bb);
1311 : }
1312 :
1313 : /* Add fake edges to the function exit for any non constant and non noreturn
1314 : calls, volatile inline assembly in the bitmap of blocks specified by
1315 : BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
1316 : that were split.
1317 :
1318 : The goal is to expose cases in which entering a basic block does not imply
1319 : that all subsequent instructions must be executed. */
1320 :
1321 : int
1322 3024 : flow_call_edges_add (sbitmap blocks)
1323 : {
1324 3024 : if (!cfg_hooks->flow_call_edges_add)
1325 0 : internal_error ("%s does not support flow_call_edges_add",
1326 : current_ir_name ());
1327 :
1328 3024 : return (cfg_hooks->flow_call_edges_add) (blocks);
1329 : }
1330 :
1331 : /* This function is called immediately after edge E is added to the
1332 : edge vector E->dest->preds. */
1333 :
1334 : void
1335 209527537 : execute_on_growing_pred (edge e)
1336 : {
1337 209527537 : if (! (e->dest->flags & BB_DUPLICATED)
1338 207152366 : && cfg_hooks->execute_on_growing_pred)
1339 165691434 : cfg_hooks->execute_on_growing_pred (e);
1340 209527537 : }
1341 :
1342 : /* This function is called immediately before edge E is removed from
1343 : the edge vector E->dest->preds. */
1344 :
1345 : void
1346 177147089 : execute_on_shrinking_pred (edge e)
1347 : {
1348 177147089 : if (! (e->dest->flags & BB_DUPLICATED)
1349 174771918 : && cfg_hooks->execute_on_shrinking_pred)
1350 135849482 : cfg_hooks->execute_on_shrinking_pred (e);
1351 177147089 : }
1352 :
1353 : /* This is used inside loop versioning when we want to insert
1354 : stmts/insns on the edges, which have a different behavior
1355 : in tree's and in RTL, so we made a CFG hook. */
1356 : void
1357 36166 : lv_flush_pending_stmts (edge e)
1358 : {
1359 36166 : if (cfg_hooks->flush_pending_stmts)
1360 36166 : cfg_hooks->flush_pending_stmts (e);
1361 36166 : }
1362 :
1363 : /* Loop versioning uses the duplicate_loop_body_to_header_edge to create
1364 : a new version of the loop basic-blocks, the parameters here are
1365 : exactly the same as in duplicate_loop_body_to_header_edge or
1366 : tree_duplicate_loop_body_to_header_edge; while in tree-ssa there is
1367 : additional work to maintain ssa information that's why there is
1368 : a need to call the tree_duplicate_loop_body_to_header_edge rather
1369 : than duplicate_loop_body_to_header_edge when we are in tree mode. */
1370 : bool
1371 36176 : cfg_hook_duplicate_loop_body_to_header_edge (class loop *loop, edge e,
1372 : unsigned int ndupl,
1373 : sbitmap wont_exit, edge orig,
1374 : vec<edge> *to_remove, int flags)
1375 : {
1376 36176 : gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_body_to_header_edge);
1377 36176 : return cfg_hooks->cfg_hook_duplicate_loop_body_to_header_edge (
1378 36176 : loop, e, ndupl, wont_exit, orig, to_remove, flags);
1379 : }
1380 :
1381 : /* Conditional jumps are represented differently in trees and RTL,
1382 : this hook takes a basic block that is known to have a cond jump
1383 : at its end and extracts the taken and not taken edges out of it
1384 : and store it in E1 and E2 respectively. */
1385 : void
1386 0 : extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1387 : {
1388 0 : gcc_assert (cfg_hooks->extract_cond_bb_edges);
1389 0 : cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1390 0 : }
1391 :
1392 : /* Responsible for updating the ssa info (PHI nodes) on the
1393 : new condition basic block that guards the versioned loop. */
1394 : void
1395 36166 : lv_adjust_loop_header_phi (basic_block first, basic_block second,
1396 : basic_block new_block, edge e)
1397 : {
1398 36166 : if (cfg_hooks->lv_adjust_loop_header_phi)
1399 36166 : cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1400 36166 : }
1401 :
1402 : /* Conditions in trees and RTL are different so we need
1403 : a different handling when we add the condition to the
1404 : versioning code. */
1405 : void
1406 36166 : lv_add_condition_to_bb (basic_block first, basic_block second,
1407 : basic_block new_block, void *cond)
1408 : {
1409 36166 : gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1410 36166 : cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
1411 36166 : }
1412 :
1413 : /* Checks whether all N blocks in BBS array can be copied.
1414 :
1415 : PREVAILING_EXIT is as in copy_bbs. If non-NULL, the copy of its source
1416 : keeps only the prevailing edge, so the abnormal successor edges of that
1417 : block do not require redirection. */
1418 : bool
1419 3450815 : can_copy_bbs_p (basic_block *bbs, unsigned n, edge prevailing_exit)
1420 : {
1421 3450815 : unsigned i;
1422 3450815 : edge e;
1423 3450815 : int ret = true;
1424 :
1425 12333638 : for (i = 0; i < n; i++)
1426 8882823 : bbs[i]->flags |= BB_DUPLICATED;
1427 :
1428 : /* A prevailing edge jumping back into the region is not supported:
1429 : its copy would have to keep targeting the original block. */
1430 3450815 : if (prevailing_exit && (prevailing_exit->dest->flags & BB_DUPLICATED))
1431 : {
1432 1 : ret = false;
1433 1 : goto end;
1434 : }
1435 :
1436 12145156 : for (i = 0; i < n; i++)
1437 : {
1438 : /* In case we should redirect abnormal edge during duplication, fail.
1439 : However, the copy of PREVAILING_EXIT->src is exempt as its outgoing
1440 : edges are removed or left in place rather than redirected. */
1441 8706855 : edge_iterator ei;
1442 8706855 : if (!(prevailing_exit && bbs[i] == prevailing_exit->src))
1443 23184756 : FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1444 14487228 : if ((e->flags & EDGE_ABNORMAL)
1445 112501 : && (e->dest->flags & BB_DUPLICATED))
1446 : {
1447 9237 : ret = false;
1448 9237 : goto end;
1449 : }
1450 :
1451 8697618 : if (!can_duplicate_block_p (bbs[i]))
1452 : {
1453 3276 : ret = false;
1454 3276 : break;
1455 : }
1456 : }
1457 :
1458 3438301 : end:
1459 12333638 : for (i = 0; i < n; i++)
1460 8882823 : bbs[i]->flags &= ~BB_DUPLICATED;
1461 :
1462 3450815 : return ret;
1463 : }
1464 :
1465 : /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1466 : are placed into array NEW_BBS in the same order. Edges from basic blocks
1467 : in BBS are also duplicated and copies of those that lead into BBS are
1468 : redirected to appropriate newly created block. The function assigns bbs
1469 : into loops (copy of basic block bb is assigned to bb->loop_father->copy
1470 : loop, so this must be set up correctly in advance)
1471 :
1472 : If UPDATE_DOMINANCE is true then this function updates dominators locally
1473 : (LOOPS structure that contains the information about dominators is passed
1474 : to enable this), otherwise it does not update the dominator information
1475 : and it assumed that the caller will do this, perhaps by destroying and
1476 : recreating it instead of trying to do an incremental update like this
1477 : function does when update_dominance is true.
1478 :
1479 : BASE is the superloop to that basic block belongs; if its header or latch
1480 : is copied, we do not set the new blocks as header or latch.
1481 :
1482 : Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1483 : also in the same order.
1484 :
1485 : Newly created basic blocks are put after the basic block AFTER in the
1486 : instruction stream, and the order of the blocks in BBS array is preserved.
1487 :
1488 : If PREVAILING_EXIT is non-NULL, its source must be in BBS, and its
1489 : destination must not be: the copy of that block keeps only its edges to
1490 : PREVAILING_EXIT->dest, and the rest of its outgoing edges are removed. None
1491 : of its edges are redirected, which allows copying a region whose exit block
1492 : has abnormal successor edges into the region. It is the caller's
1493 : responsibility to remove or rewrite the copied block's control
1494 : statement. */
1495 :
1496 : void
1497 2515913 : copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1498 : edge *edges, unsigned num_edges, edge *new_edges,
1499 : class loop *base, basic_block after, bool update_dominance,
1500 : edge prevailing_exit)
1501 : {
1502 2515913 : unsigned i, j;
1503 2515913 : basic_block bb, new_bb, dom_bb;
1504 2515913 : edge e;
1505 2515913 : copy_bb_data id;
1506 :
1507 : /* One copyid for the region (not per BB). */
1508 2515913 : id.skip_copyid = true;
1509 :
1510 : /* Mark the blocks to be copied. This is used by edge creation hooks
1511 : to decide whether to reallocate PHI nodes capacity to avoid reallocating
1512 : PHIs in the set of source BBs. */
1513 6669230 : for (i = 0; i < n; i++)
1514 4153317 : bbs[i]->flags |= BB_DUPLICATED;
1515 :
1516 : /* A prevailing edge into the region would be redirected like any
1517 : other. We refuse this in can_copy_bbs_p. */
1518 2515913 : gcc_checking_assert (!prevailing_exit
1519 : || !(prevailing_exit->dest->flags & BB_DUPLICATED));
1520 :
1521 : /* Duplicate bbs, update dominators, assign bbs to loops. */
1522 6669230 : for (i = 0; i < n; i++)
1523 : {
1524 : /* Duplicate. */
1525 4153317 : bb = bbs[i];
1526 4153317 : new_bb = new_bbs[i] = duplicate_block (bb, NULL, after, &id);
1527 4153317 : after = new_bb;
1528 4153317 : if (bb->loop_father)
1529 : {
1530 : /* Possibly set loop header. */
1531 4153317 : if (bb->loop_father->header == bb && bb->loop_father != base)
1532 41981 : new_bb->loop_father->header = new_bb;
1533 : /* Or latch. */
1534 4153317 : if (bb->loop_father->latch == bb && bb->loop_father != base)
1535 41981 : new_bb->loop_father->latch = new_bb;
1536 : }
1537 : }
1538 :
1539 : /* Set dominators. */
1540 2515913 : if (update_dominance)
1541 : {
1542 3564525 : for (i = 0; i < n; i++)
1543 : {
1544 2361240 : bb = bbs[i];
1545 2361240 : new_bb = new_bbs[i];
1546 :
1547 2361240 : dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1548 2361240 : if (dom_bb->flags & BB_DUPLICATED)
1549 : {
1550 1157955 : dom_bb = get_bb_copy (dom_bb);
1551 1157955 : set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1552 : }
1553 : }
1554 : }
1555 :
1556 : /* Redirect edges. */
1557 6669230 : for (i = 0; i < n; i++)
1558 : {
1559 4153317 : edge_iterator ei;
1560 4153317 : new_bb = new_bbs[i];
1561 4153317 : bb = bbs[i];
1562 :
1563 11517691 : for (ei = ei_start (new_bb->succs); (e = ei_safe_edge (ei)); )
1564 : {
1565 : /* Remove the edges that do not prevail instead of
1566 : redirecting them. */
1567 7364374 : if (prevailing_exit && bb == prevailing_exit->src
1568 265 : && e->dest != prevailing_exit->dest)
1569 : {
1570 177 : remove_edge (e);
1571 177 : continue;
1572 : }
1573 7364197 : if (e->dest->flags & BB_DUPLICATED)
1574 2375167 : redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1575 7364197 : ei_next (&ei);
1576 : }
1577 : }
1578 4302369 : for (j = 0; j < num_edges; j++)
1579 : {
1580 1786456 : if (!edges[j])
1581 95453 : new_edges[j] = NULL;
1582 : else
1583 : {
1584 1691003 : basic_block src = edges[j]->src;
1585 1691003 : basic_block dest = edges[j]->dest;
1586 1691003 : if (src->flags & BB_DUPLICATED)
1587 1691003 : src = get_bb_copy (src);
1588 1691003 : if (dest->flags & BB_DUPLICATED)
1589 527858 : dest = get_bb_copy (dest);
1590 1691003 : new_edges[j] = find_edge (src, dest);
1591 : }
1592 : }
1593 :
1594 : /* Clear information about duplicates. */
1595 6669230 : for (i = 0; i < n; i++)
1596 4153317 : bbs[i]->flags &= ~BB_DUPLICATED;
1597 :
1598 : /* One copyid for the whole region, keyed off the first source block that
1599 : has a location. */
1600 : location_t loc = UNKNOWN_LOCATION;
1601 5171984 : for (i = 0; i < n && loc == UNKNOWN_LOCATION; i++)
1602 2656071 : loc = copyid_location_from_bb (bbs[i]);
1603 :
1604 2515913 : if (loc != UNKNOWN_LOCATION)
1605 : {
1606 2430549 : unsigned int copyid = allocate_copyid_base (loc, 1);
1607 8856648 : for (i = 0; i < n; i++)
1608 3995550 : assign_discriminators_to_bb (new_bbs[i], 0, copyid);
1609 : }
1610 2515913 : }
1611 :
1612 : /* Return true if BB contains only labels or non-executable
1613 : instructions */
1614 : bool
1615 15428558 : empty_block_p (basic_block bb)
1616 : {
1617 15428558 : gcc_assert (cfg_hooks->empty_block_p);
1618 15428558 : return cfg_hooks->empty_block_p (bb);
1619 : }
1620 :
1621 : /* Split a basic block if it ends with a conditional branch and if
1622 : the other part of the block is not empty. */
1623 : basic_block
1624 557 : split_block_before_cond_jump (basic_block bb)
1625 : {
1626 557 : gcc_assert (cfg_hooks->split_block_before_cond_jump);
1627 557 : return cfg_hooks->split_block_before_cond_jump (bb);
1628 : }
1629 :
1630 : /* Work-horse for passes.cc:check_profile_consistency.
1631 : Do book-keeping of the CFG for the profile consistency checker.
1632 : Store the counting in RECORD. */
1633 :
1634 : void
1635 0 : profile_record_check_consistency (profile_record *record)
1636 : {
1637 0 : basic_block bb;
1638 0 : edge_iterator ei;
1639 0 : edge e;
1640 :
1641 0 : FOR_ALL_BB_FN (bb, cfun)
1642 : {
1643 0 : if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
1644 0 : && profile_status_for_fn (cfun) != PROFILE_ABSENT
1645 0 : && EDGE_COUNT (bb->succs))
1646 : {
1647 0 : sreal sum = 0;
1648 0 : bool found = false;
1649 0 : FOR_EACH_EDGE (e, ei, bb->succs)
1650 : {
1651 0 : if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
1652 0 : found = true;
1653 0 : if (e->probability.initialized_p ())
1654 0 : sum += e->probability.to_sreal ();
1655 : }
1656 0 : double dsum = sum.to_double ();
1657 0 : if (found && (dsum < 0.9 || dsum > 1.1)
1658 0 : && !(bb->count == profile_count::zero ()))
1659 : {
1660 0 : record->num_mismatched_prob_out++;
1661 0 : dsum = dsum > 1 ? dsum - 1 : 1 - dsum;
1662 0 : if (profile_info)
1663 : {
1664 0 : if (ENTRY_BLOCK_PTR_FOR_FN
1665 0 : (cfun)->count.ipa ().initialized_p ()
1666 0 : && ENTRY_BLOCK_PTR_FOR_FN
1667 0 : (cfun)->count.ipa ().nonzero_p ()
1668 0 : && bb->count.ipa ().initialized_p ())
1669 0 : record->dyn_mismatched_prob_out
1670 0 : += dsum * bb->count.ipa ().to_gcov_type ();
1671 : }
1672 0 : else if (bb->count.initialized_p ())
1673 0 : record->dyn_mismatched_prob_out
1674 0 : += dsum * bb->count.to_sreal_scale
1675 0 : (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count).to_double ();
1676 : }
1677 : }
1678 0 : if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1679 0 : && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1680 : {
1681 0 : profile_count lsum = profile_count::zero ();
1682 0 : FOR_EACH_EDGE (e, ei, bb->preds)
1683 0 : lsum += e->count ();
1684 0 : if (lsum.differs_from_p (bb->count))
1685 : {
1686 0 : record->num_mismatched_count_in++;
1687 0 : profile_count max;
1688 0 : if (lsum < bb->count)
1689 0 : max = bb->count;
1690 : else
1691 0 : max = lsum;
1692 0 : if (profile_info)
1693 : {
1694 0 : if (ENTRY_BLOCK_PTR_FOR_FN
1695 0 : (cfun)->count.ipa ().initialized_p ()
1696 0 : && ENTRY_BLOCK_PTR_FOR_FN
1697 0 : (cfun)->count.ipa ().nonzero_p ()
1698 0 : && max.ipa ().initialized_p ())
1699 0 : record->dyn_mismatched_count_in
1700 0 : += max.ipa ().to_gcov_type ();
1701 : }
1702 0 : else if (bb->count.initialized_p ())
1703 0 : record->dyn_mismatched_prob_out
1704 0 : += max.to_sreal_scale
1705 0 : (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count).to_double ();
1706 : }
1707 : }
1708 0 : if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1709 : || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1710 0 : continue;
1711 : }
1712 0 : }
1713 :
1714 : /* Work-horse for passes.cc:acount_profile.
1715 : Do book-keeping of the CFG for the profile accounting.
1716 : Store the counting in RECORD. */
1717 :
1718 : void
1719 0 : profile_record_account_profile (profile_record *record)
1720 : {
1721 0 : basic_block bb;
1722 :
1723 0 : FOR_ALL_BB_FN (bb, cfun)
1724 : {
1725 0 : gcc_assert (cfg_hooks->account_profile_record);
1726 0 : cfg_hooks->account_profile_record (bb, record);
1727 : }
1728 0 : }
1729 :
1730 : #if __GNUC__ >= 10
1731 : # pragma GCC diagnostic pop
1732 : #endif
|