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