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