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