Revise -mdisable-fpregs option and add new -msoft-mult option
[official-gcc.git] / gcc / cfghooks.c
blob6446e16ca8c041390f3e3904d2b93480c48b2f90
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>
5 This file is part of GCC.
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.
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.
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/>. */
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"
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
41 /* A pointer to one of the hooks containers. */
42 static struct cfg_hooks *cfg_hooks;
44 /* Initialization of functions specific to the rtl IR. */
45 void
46 rtl_register_cfg_hooks (void)
48 cfg_hooks = &rtl_cfg_hooks;
51 /* Initialization of functions specific to the rtl IR. */
52 void
53 cfg_layout_rtl_register_cfg_hooks (void)
55 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
58 /* Initialization of functions specific to the tree IR. */
60 void
61 gimple_register_cfg_hooks (void)
63 cfg_hooks = &gimple_cfg_hooks;
66 struct cfg_hooks
67 get_cfg_hooks (void)
69 return *cfg_hooks;
72 void
73 set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
75 *cfg_hooks = new_cfg_hooks;
78 /* Returns current ir type. */
80 enum ir_type
81 current_ir_type (void)
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 ();
93 /* Verify the CFG consistency.
95 Currently it does following: checks edge and basic block list correctness
96 and calls into IL dependent checking then. */
98 DEBUG_FUNCTION void
99 verify_flow_info (void)
101 size_t *edge_checksum;
102 int err = 0;
103 basic_block bb, last_bb_seen;
104 basic_block *last_visited;
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));
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)
114 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
115 && bb != BASIC_BLOCK_FOR_FN (cfun, bb->index))
117 error ("bb %d on wrong place", bb->index);
118 err = 1;
121 if (bb->prev_bb != last_bb_seen)
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;
128 last_bb_seen = bb;
131 /* Now check the basic blocks (boundaries etc.) */
132 FOR_EACH_BB_REVERSE_FN (bb, cfun)
134 int n_fallthru = 0;
135 edge e;
136 edge_iterator ei;
138 if (bb->loop_father != NULL && current_loops == NULL)
140 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
141 bb->index);
142 err = 1;
144 if (bb->loop_father == NULL && current_loops != NULL)
146 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
147 err = 1;
150 if (!bb->count.verify ())
152 error ("verify_flow_info: Wrong count of block %i", bb->index);
153 err = 1;
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)
160 error ("verify_flow_info: Missing count of block %i", bb->index);
161 err = 1;
164 if (bb->flags & ~cfun->cfg->bb_flags_allocated)
166 error ("verify_flow_info: unallocated flag set on BB %d", bb->index);
167 err = 1;
170 FOR_EACH_EDGE (e, ei, bb->succs)
172 if (last_visited [e->dest->index] == bb)
174 error ("verify_flow_info: Duplicate edge %i->%i",
175 e->src->index, e->dest->index);
176 err = 1;
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)
183 error ("Uninitialized probability of edge %i->%i", e->src->index,
184 e->dest->index);
185 err = 1;
187 if (!e->probability.verify ())
189 error ("verify_flow_info: Wrong probability of edge %i->%i",
190 e->src->index, e->dest->index);
191 err = 1;
194 last_visited [e->dest->index] = bb;
196 if (e->flags & EDGE_FALLTHRU)
197 n_fallthru++;
199 if (e->src != bb)
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;
211 if (e->flags & ~cfun->cfg->edge_flags_allocated)
213 error ("verify_flow_info: unallocated edge flag set on %d -> %d",
214 e->src->index, e->dest->index);
215 err = 1;
218 edge_checksum[e->dest->index] += (size_t) e;
220 if (n_fallthru > 1)
222 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
223 err = 1;
226 FOR_EACH_EDGE (e, ei, bb->preds)
228 if (e->dest != bb)
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;
239 if (ei.index != e->dest_idx)
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;
252 edge_checksum[e->dest->index] -= (size_t) e;
256 /* Complete edge checksumming for ENTRY and EXIT. */
258 edge e;
259 edge_iterator ei;
261 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
262 edge_checksum[e->dest->index] += (size_t) e;
264 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
265 edge_checksum[e->dest->index] -= (size_t) e;
268 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
269 if (edge_checksum[bb->index])
271 error ("basic block %i edge lists are corrupted", bb->index);
272 err = 1;
275 /* Clean up. */
276 free (last_visited);
277 free (edge_checksum);
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);
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.
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. */
293 void
294 dump_bb (FILE *outf, basic_block bb, int indent, dump_flags_t flags)
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);
305 DEBUG_FUNCTION void
306 debug (basic_block_def &ref)
308 dump_bb (stderr, &ref, 0, TDF_NONE);
311 DEBUG_FUNCTION void
312 debug (basic_block_def *ptr)
314 if (ptr)
315 debug (*ptr);
316 else
317 fprintf (stderr, "<nil>\n");
320 static void
321 debug_slim (basic_block ptr)
323 fprintf (stderr, "<basic_block %p (%d)>", (void *) ptr, ptr->index);
326 DEFINE_DEBUG_VEC (basic_block_def *)
327 DEFINE_DEBUG_HASH_SET (basic_block_def *)
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(). */
336 void
337 dump_bb_for_graph (pretty_printer *pp, basic_block bb)
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);
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)
354 basic_block bb;
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);
361 putc ('\n', file);
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)
369 dump_flow_info (stderr, TDF_DETAILS);
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. */
377 edge
378 redirect_edge_and_branch (edge e, basic_block dest)
380 edge ret;
382 if (!cfg_hooks->redirect_edge_and_branch)
383 internal_error ("%s does not support redirect_edge_and_branch",
384 cfg_hooks->name);
386 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
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);
393 return ret;
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. */
399 bool
400 can_remove_branch_p (const_edge e)
402 if (!cfg_hooks->can_remove_branch_p)
403 internal_error ("%s does not support can_remove_branch_p",
404 cfg_hooks->name);
406 if (EDGE_COUNT (e->src->succs) != 2)
407 return false;
409 return cfg_hooks->can_remove_branch_p (e);
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. */
416 void
417 remove_branch (edge e)
419 edge other;
420 basic_block src = e->src;
421 int irr;
423 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
425 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
426 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
428 e = redirect_edge_and_branch (e, other->dest);
429 gcc_assert (e != NULL);
431 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
432 e->flags |= irr;
435 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
437 void
438 remove_edge (edge e)
440 if (current_loops != NULL)
442 rescan_loop_exit (e, false, true);
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.
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);
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);
461 remove_edge_raw (e);
464 /* Like redirect_edge_succ but avoid possible duplicate edge. */
466 edge
467 redirect_edge_succ_nodup (edge e, basic_block new_succ)
469 edge s;
471 s = find_edge (e->src, new_succ);
472 if (s && s != e)
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;
481 else
482 redirect_edge_succ (e, new_succ);
484 return e;
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. */
491 basic_block
492 redirect_edge_and_branch_force (edge e, basic_block dest)
494 basic_block ret, src = e->src;
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);
500 if (current_loops != NULL)
501 rescan_loop_exit (e, false, true);
503 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
505 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
506 set_immediate_dominator (CDI_DOMINATORS, ret, src);
508 if (current_loops != NULL)
510 if (ret != NULL)
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);
517 else if (find_edge (src, dest) == e)
518 rescan_loop_exit (e, true, false);
521 return ret;
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. */
528 static edge
529 split_block_1 (basic_block bb, void *i)
531 basic_block new_bb;
532 edge res;
534 if (!cfg_hooks->split_block)
535 internal_error ("%s does not support split_block", cfg_hooks->name);
537 new_bb = cfg_hooks->split_block (bb, i);
538 if (!new_bb)
539 return NULL;
541 new_bb->count = bb->count;
542 new_bb->discriminator = bb->discriminator;
544 if (dom_info_available_p (CDI_DOMINATORS))
546 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
547 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
550 if (current_loops != NULL)
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;
561 res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
563 if (bb->flags & BB_IRREDUCIBLE_LOOP)
565 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
566 res->flags |= EDGE_IRREDUCIBLE_LOOP;
569 return res;
572 edge
573 split_block (basic_block bb, gimple *i)
575 return split_block_1 (bb, i);
578 edge
579 split_block (basic_block bb, rtx i)
581 return split_block_1 (bb, i);
584 /* Splits block BB just after labels. The newly created edge is returned. */
586 edge
587 split_block_after_labels (basic_block bb)
589 return split_block_1 (bb, NULL);
592 /* Moves block BB immediately after block AFTER. Returns false if the
593 movement was impossible. */
595 bool
596 move_block_after (basic_block bb, basic_block after)
598 bool ret;
600 if (!cfg_hooks->move_block_after)
601 internal_error ("%s does not support move_block_after", cfg_hooks->name);
603 ret = cfg_hooks->move_block_after (bb, after);
605 return ret;
608 /* Deletes the basic block BB. */
610 void
611 delete_basic_block (basic_block bb)
613 if (!cfg_hooks->delete_basic_block)
614 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
616 cfg_hooks->delete_basic_block (bb);
618 if (current_loops != NULL)
620 class loop *loop = bb->loop_father;
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);
628 remove_bb_from_loops (bb);
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));
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);
643 /* Remove the basic block from the array. */
644 expunge_block (bb);
647 /* Splits edge E and returns the newly created basic block. */
649 basic_block
650 split_edge (edge e)
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;
660 if (!cfg_hooks->split_edge)
661 internal_error ("%s does not support split_edge", cfg_hooks->name);
663 if (current_loops != NULL)
664 rescan_loop_exit (e, false, true);
666 ret = cfg_hooks->split_edge (e);
667 ret->count = count;
668 single_succ_edge (ret)->probability = profile_probability::always ();
670 if (irr)
672 ret->flags |= BB_IRREDUCIBLE_LOOP;
673 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
674 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
676 if (back)
678 single_pred_edge (ret)->flags &= ~EDGE_DFS_BACK;
679 single_succ_edge (ret)->flags |= EDGE_DFS_BACK;
682 if (dom_info_available_p (CDI_DOMINATORS))
683 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
685 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
687 /* There are two cases:
689 If the immediate dominator of e->dest is not e->src, it
690 remains unchanged.
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. */
696 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
697 == single_pred (ret))
699 edge_iterator ei;
700 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
702 if (f == single_succ_edge (ret))
703 continue;
705 if (!dominated_by_p (CDI_DOMINATORS, f->src,
706 single_succ (ret)))
707 break;
710 if (!f)
711 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
715 if (current_loops != NULL)
717 loop = find_common_loop (src->loop_father, dest->loop_father);
718 add_bb_to_loop (ret, loop);
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;
726 return ret;
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. */
733 static basic_block
734 create_basic_block_1 (void *head, void *end, basic_block after)
736 basic_block ret;
738 if (!cfg_hooks->create_basic_block)
739 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
741 ret = cfg_hooks->create_basic_block (head, end, after);
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);
748 return ret;
751 basic_block
752 create_basic_block (gimple_seq seq, basic_block after)
754 return create_basic_block_1 (seq, NULL, after);
757 basic_block
758 create_basic_block (rtx head, rtx end, basic_block after)
760 return create_basic_block_1 (head, end, after);
764 /* Creates an empty basic block just after basic block AFTER. */
766 basic_block
767 create_empty_bb (basic_block after)
769 return create_basic_block_1 (NULL, NULL, after);
772 /* Checks whether we may merge blocks BB1 and BB2. */
774 bool
775 can_merge_blocks_p (basic_block bb1, basic_block bb2)
777 bool ret;
779 if (!cfg_hooks->can_merge_blocks_p)
780 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
782 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
784 return ret;
787 void
788 predict_edge (edge e, enum br_predictor predictor, int probability)
790 if (!cfg_hooks->predict_edge)
791 internal_error ("%s does not support predict_edge", cfg_hooks->name);
793 cfg_hooks->predict_edge (e, predictor, probability);
796 bool
797 predicted_by_p (const_basic_block bb, enum br_predictor predictor)
799 if (!cfg_hooks->predict_edge)
800 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
802 return cfg_hooks->predicted_by_p (bb, predictor);
805 /* Merges basic block B into basic block A. */
807 void
808 merge_blocks (basic_block a, basic_block b)
810 edge e;
811 edge_iterator ei;
813 if (!cfg_hooks->merge_blocks)
814 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
816 cfg_hooks->merge_blocks (a, b);
818 if (current_loops != NULL)
820 /* If the block we merge into is a loop header do nothing unless ... */
821 if (a->loop_father->header == a)
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);
828 /* If we merge a loop header into its predecessor, update the loop
829 structure. */
830 else if (b->loop_father->header == b)
832 remove_bb_from_loops (a);
833 add_bb_to_loop (a, b->loop_father);
834 a->loop_father->header = a;
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);
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. */
849 while (EDGE_COUNT (a->succs) != 0)
850 remove_edge (EDGE_SUCC (a, 0));
852 /* Adjust the edges out of B for the new owner. */
853 FOR_EACH_EDGE (e, ei, b->succs)
855 e->src = a;
856 if (current_loops != NULL)
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);
864 a->succs = b->succs;
865 a->flags |= b->flags;
867 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
868 b->preds = b->succs = NULL;
870 if (dom_info_available_p (CDI_DOMINATORS))
871 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
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);
878 expunge_block (b);
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. */
885 edge
886 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
887 void (*new_bb_cbk) (basic_block))
889 edge e, fallthru;
890 edge_iterator ei;
891 basic_block dummy, jump;
892 class loop *loop, *ploop, *cloop;
894 if (!cfg_hooks->make_forwarder_block)
895 internal_error ("%s does not support make_forwarder_block",
896 cfg_hooks->name);
898 fallthru = split_block_after_labels (bb);
899 dummy = fallthru->src;
900 dummy->count = profile_count::zero ();
901 bb = fallthru->dest;
903 /* Redirect back edges we want to keep. */
904 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
906 basic_block e_src;
908 if (redirect_edge_p (e))
910 dummy->count += e->count ();
911 ei_next (&ei);
912 continue;
915 e_src = e->src;
916 jump = redirect_edge_and_branch_force (e, bb);
917 if (jump != NULL)
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;
927 if (new_bb_cbk != NULL)
928 new_bb_cbk (jump);
932 if (dom_info_available_p (CDI_DOMINATORS))
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 ();
942 if (current_loops != NULL)
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)
955 remove_bb_from_loops (dummy);
956 loop->header = bb;
958 cloop = loop;
959 FOR_EACH_EDGE (e, ei, dummy->preds)
961 cloop = find_common_loop (cloop, e->src->loop_father);
963 add_bb_to_loop (dummy, cloop);
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;
972 cfg_hooks->make_forwarder_block (fallthru);
974 return fallthru;
977 /* Try to make the edge fallthru. */
979 void
980 tidy_fallthru_edge (edge e)
982 if (cfg_hooks->tidy_fallthru_edge)
983 cfg_hooks->tidy_fallthru_edge (e);
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.
991 ??? This routine is currently RTL specific. */
993 void
994 tidy_fallthru_edges (void)
996 basic_block b, c;
998 if (!cfg_hooks->tidy_fallthru_edge)
999 return;
1001 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1002 return;
1004 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
1005 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, next_bb)
1007 edge s;
1009 c = b->next_bb;
1011 /* We care about simple conditional or unconditional jumps with
1012 a single successor.
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).
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. */
1023 if (single_succ_p (b))
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);
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. */
1038 basic_block
1039 force_nonfallthru (edge e)
1041 basic_block ret, src = e->src;
1043 if (!cfg_hooks->force_nonfallthru)
1044 internal_error ("%s does not support force_nonfallthru",
1045 cfg_hooks->name);
1047 ret = cfg_hooks->force_nonfallthru (e);
1048 if (ret != NULL)
1050 if (dom_info_available_p (CDI_DOMINATORS))
1051 set_immediate_dominator (CDI_DOMINATORS, ret, src);
1053 if (current_loops != NULL)
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);
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;
1069 return ret;
1072 /* Returns true if we can duplicate basic block BB. */
1074 bool
1075 can_duplicate_block_p (const_basic_block bb)
1077 if (!cfg_hooks->can_duplicate_block_p)
1078 internal_error ("%s does not support can_duplicate_block_p",
1079 cfg_hooks->name);
1081 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1082 return false;
1084 return cfg_hooks->can_duplicate_block_p (bb);
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. */
1091 basic_block
1092 duplicate_block (basic_block bb, edge e, basic_block after, copy_bb_data *id)
1094 edge s, n;
1095 basic_block new_bb;
1096 profile_count new_count = e ? e->count (): profile_count::uninitialized ();
1097 edge_iterator ei;
1099 if (!cfg_hooks->duplicate_block)
1100 internal_error ("%s does not support duplicate_block",
1101 cfg_hooks->name);
1103 if (bb->count < new_count)
1104 new_count = bb->count;
1106 gcc_checking_assert (can_duplicate_block_p (bb));
1108 new_bb = cfg_hooks->duplicate_block (bb, id);
1109 if (after)
1110 move_block_after (new_bb, after);
1112 new_bb->flags = (bb->flags & ~BB_DUPLICATED);
1113 FOR_EACH_EDGE (s, ei, bb->succs)
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;
1123 if (e)
1125 new_bb->count = new_count;
1126 bb->count -= new_count;
1128 redirect_edge_and_branch_force (e, new_bb);
1130 else
1131 new_bb->count = bb->count;
1133 set_bb_original (new_bb, bb);
1134 set_bb_copy (bb, new_bb);
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)
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)
1148 add_bb_to_loop (new_bb, loop_outer (cloop));
1149 mark_loop_for_removal (cloop);
1151 else
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)
1159 cloop->latch = NULL;
1160 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1165 return new_bb;
1168 /* Return 1 if BB ends with a call, possibly followed by some
1169 instructions that must stay with the call, 0 otherwise. */
1171 bool
1172 block_ends_with_call_p (basic_block bb)
1174 if (!cfg_hooks->block_ends_with_call_p)
1175 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
1177 return (cfg_hooks->block_ends_with_call_p) (bb);
1180 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1182 bool
1183 block_ends_with_condjump_p (const_basic_block bb)
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);
1189 return (cfg_hooks->block_ends_with_condjump_p) (bb);
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.
1197 The goal is to expose cases in which entering a basic block does not imply
1198 that all subsequent instructions must be executed. */
1201 flow_call_edges_add (sbitmap blocks)
1203 if (!cfg_hooks->flow_call_edges_add)
1204 internal_error ("%s does not support flow_call_edges_add",
1205 cfg_hooks->name);
1207 return (cfg_hooks->flow_call_edges_add) (blocks);
1210 /* This function is called immediately after edge E is added to the
1211 edge vector E->dest->preds. */
1213 void
1214 execute_on_growing_pred (edge e)
1216 if (! (e->dest->flags & BB_DUPLICATED)
1217 && cfg_hooks->execute_on_growing_pred)
1218 cfg_hooks->execute_on_growing_pred (e);
1221 /* This function is called immediately before edge E is removed from
1222 the edge vector E->dest->preds. */
1224 void
1225 execute_on_shrinking_pred (edge e)
1227 if (! (e->dest->flags & BB_DUPLICATED)
1228 && cfg_hooks->execute_on_shrinking_pred)
1229 cfg_hooks->execute_on_shrinking_pred (e);
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)
1238 if (cfg_hooks->flush_pending_stmts)
1239 cfg_hooks->flush_pending_stmts (e);
1242 /* Loop versioning uses the duplicate_loop_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_to_header_edge or
1245 tree_duplicate_loop_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_to_header_edge rather
1248 than duplicate_loop_to_header_edge when we are in tree mode. */
1249 bool
1250 cfg_hook_duplicate_loop_to_header_edge (class loop *loop, edge e,
1251 unsigned int ndupl,
1252 sbitmap wont_exit, edge orig,
1253 vec<edge> *to_remove,
1254 int flags)
1256 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
1257 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1258 ndupl, wont_exit,
1259 orig, to_remove,
1260 flags);
1263 /* Conditional jumps are represented differently in trees and RTL,
1264 this hook takes a basic block that is known to have a cond jump
1265 at its end and extracts the taken and not taken edges out of it
1266 and store it in E1 and E2 respectively. */
1267 void
1268 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1270 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1271 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1274 /* Responsible for updating the ssa info (PHI nodes) on the
1275 new condition basic block that guards the versioned loop. */
1276 void
1277 lv_adjust_loop_header_phi (basic_block first, basic_block second,
1278 basic_block new_block, edge e)
1280 if (cfg_hooks->lv_adjust_loop_header_phi)
1281 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1284 /* Conditions in trees and RTL are different so we need
1285 a different handling when we add the condition to the
1286 versioning code. */
1287 void
1288 lv_add_condition_to_bb (basic_block first, basic_block second,
1289 basic_block new_block, void *cond)
1291 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1292 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
1295 /* Checks whether all N blocks in BBS array can be copied. */
1296 bool
1297 can_copy_bbs_p (basic_block *bbs, unsigned n)
1299 unsigned i;
1300 edge e;
1301 int ret = true;
1303 for (i = 0; i < n; i++)
1304 bbs[i]->flags |= BB_DUPLICATED;
1306 for (i = 0; i < n; i++)
1308 /* In case we should redirect abnormal edge during duplication, fail. */
1309 edge_iterator ei;
1310 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1311 if ((e->flags & EDGE_ABNORMAL)
1312 && (e->dest->flags & BB_DUPLICATED))
1314 ret = false;
1315 goto end;
1318 if (!can_duplicate_block_p (bbs[i]))
1320 ret = false;
1321 break;
1325 end:
1326 for (i = 0; i < n; i++)
1327 bbs[i]->flags &= ~BB_DUPLICATED;
1329 return ret;
1332 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1333 are placed into array NEW_BBS in the same order. Edges from basic blocks
1334 in BBS are also duplicated and copies of those that lead into BBS are
1335 redirected to appropriate newly created block. The function assigns bbs
1336 into loops (copy of basic block bb is assigned to bb->loop_father->copy
1337 loop, so this must be set up correctly in advance)
1339 If UPDATE_DOMINANCE is true then this function updates dominators locally
1340 (LOOPS structure that contains the information about dominators is passed
1341 to enable this), otherwise it does not update the dominator information
1342 and it assumed that the caller will do this, perhaps by destroying and
1343 recreating it instead of trying to do an incremental update like this
1344 function does when update_dominance is true.
1346 BASE is the superloop to that basic block belongs; if its header or latch
1347 is copied, we do not set the new blocks as header or latch.
1349 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1350 also in the same order.
1352 Newly created basic blocks are put after the basic block AFTER in the
1353 instruction stream, and the order of the blocks in BBS array is preserved. */
1355 void
1356 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1357 edge *edges, unsigned num_edges, edge *new_edges,
1358 class loop *base, basic_block after, bool update_dominance)
1360 unsigned i, j;
1361 basic_block bb, new_bb, dom_bb;
1362 edge e;
1363 copy_bb_data id;
1365 /* Mark the blocks to be copied. This is used by edge creation hooks
1366 to decide whether to reallocate PHI nodes capacity to avoid reallocating
1367 PHIs in the set of source BBs. */
1368 for (i = 0; i < n; i++)
1369 bbs[i]->flags |= BB_DUPLICATED;
1371 /* Duplicate bbs, update dominators, assign bbs to loops. */
1372 for (i = 0; i < n; i++)
1374 /* Duplicate. */
1375 bb = bbs[i];
1376 new_bb = new_bbs[i] = duplicate_block (bb, NULL, after, &id);
1377 after = new_bb;
1378 if (bb->loop_father)
1380 /* Possibly set loop header. */
1381 if (bb->loop_father->header == bb && bb->loop_father != base)
1382 new_bb->loop_father->header = new_bb;
1383 /* Or latch. */
1384 if (bb->loop_father->latch == bb && bb->loop_father != base)
1385 new_bb->loop_father->latch = new_bb;
1389 /* Set dominators. */
1390 if (update_dominance)
1392 for (i = 0; i < n; i++)
1394 bb = bbs[i];
1395 new_bb = new_bbs[i];
1397 dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1398 if (dom_bb->flags & BB_DUPLICATED)
1400 dom_bb = get_bb_copy (dom_bb);
1401 set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1406 /* Redirect edges. */
1407 for (i = 0; i < n; i++)
1409 edge_iterator ei;
1410 new_bb = new_bbs[i];
1411 bb = bbs[i];
1413 FOR_EACH_EDGE (e, ei, new_bb->succs)
1415 if (!(e->dest->flags & BB_DUPLICATED))
1416 continue;
1417 redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1420 for (j = 0; j < num_edges; j++)
1422 if (!edges[j])
1423 new_edges[j] = NULL;
1424 else
1426 basic_block src = edges[j]->src;
1427 basic_block dest = edges[j]->dest;
1428 if (src->flags & BB_DUPLICATED)
1429 src = get_bb_copy (src);
1430 if (dest->flags & BB_DUPLICATED)
1431 dest = get_bb_copy (dest);
1432 new_edges[j] = find_edge (src, dest);
1436 /* Clear information about duplicates. */
1437 for (i = 0; i < n; i++)
1438 bbs[i]->flags &= ~BB_DUPLICATED;
1441 /* Return true if BB contains only labels or non-executable
1442 instructions */
1443 bool
1444 empty_block_p (basic_block bb)
1446 gcc_assert (cfg_hooks->empty_block_p);
1447 return cfg_hooks->empty_block_p (bb);
1450 /* Split a basic block if it ends with a conditional branch and if
1451 the other part of the block is not empty. */
1452 basic_block
1453 split_block_before_cond_jump (basic_block bb)
1455 gcc_assert (cfg_hooks->split_block_before_cond_jump);
1456 return cfg_hooks->split_block_before_cond_jump (bb);
1459 /* Work-horse for passes.c:check_profile_consistency.
1460 Do book-keeping of the CFG for the profile consistency checker.
1461 Store the counting in RECORD. */
1463 void
1464 profile_record_check_consistency (profile_record *record)
1466 basic_block bb;
1467 edge_iterator ei;
1468 edge e;
1470 FOR_ALL_BB_FN (bb, cfun)
1472 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
1473 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1475 profile_probability sum = profile_probability::never ();
1476 FOR_EACH_EDGE (e, ei, bb->succs)
1477 sum += e->probability;
1478 if (EDGE_COUNT (bb->succs)
1479 && sum.differs_from_p (profile_probability::always ()))
1480 record->num_mismatched_freq_out++;
1481 profile_count lsum = profile_count::zero ();
1482 FOR_EACH_EDGE (e, ei, bb->succs)
1483 lsum += e->count ();
1484 if (EDGE_COUNT (bb->succs) && (lsum.differs_from_p (bb->count)))
1485 record->num_mismatched_count_out++;
1487 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1488 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1490 profile_probability sum = profile_probability::never ();
1491 profile_count lsum = profile_count::zero ();
1492 FOR_EACH_EDGE (e, ei, bb->preds)
1494 sum += e->probability;
1495 lsum += e->count ();
1497 if (EDGE_COUNT (bb->preds)
1498 && sum.differs_from_p (profile_probability::always ()))
1499 record->num_mismatched_freq_in++;
1500 if (lsum.differs_from_p (bb->count))
1501 record->num_mismatched_count_in++;
1503 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1504 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1505 continue;
1506 gcc_assert (cfg_hooks->account_profile_record);
1507 cfg_hooks->account_profile_record (bb, record);
1511 /* Work-horse for passes.c:acount_profile.
1512 Do book-keeping of the CFG for the profile accounting.
1513 Store the counting in RECORD. */
1515 void
1516 profile_record_account_profile (profile_record *record)
1518 basic_block bb;
1520 FOR_ALL_BB_FN (bb, cfun)
1522 gcc_assert (cfg_hooks->account_profile_record);
1523 cfg_hooks->account_profile_record (bb, record);
1527 #if __GNUC__ >= 10
1528 # pragma GCC diagnostic pop
1529 #endif