* MAINTAINERS: Add a note that maintainership also includes web
[official-gcc.git] / gcc / cfghooks.c
blob18dc49a035e604b153a36b093d582513d7d19564
1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003-2017 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 /* A pointer to one of the hooks containers. */
36 static struct cfg_hooks *cfg_hooks;
38 /* Initialization of functions specific to the rtl IR. */
39 void
40 rtl_register_cfg_hooks (void)
42 cfg_hooks = &rtl_cfg_hooks;
45 /* Initialization of functions specific to the rtl IR. */
46 void
47 cfg_layout_rtl_register_cfg_hooks (void)
49 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
52 /* Initialization of functions specific to the tree IR. */
54 void
55 gimple_register_cfg_hooks (void)
57 cfg_hooks = &gimple_cfg_hooks;
60 struct cfg_hooks
61 get_cfg_hooks (void)
63 return *cfg_hooks;
66 void
67 set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
69 *cfg_hooks = new_cfg_hooks;
72 /* Returns current ir type. */
74 enum ir_type
75 current_ir_type (void)
77 if (cfg_hooks == &gimple_cfg_hooks)
78 return IR_GIMPLE;
79 else if (cfg_hooks == &rtl_cfg_hooks)
80 return IR_RTL_CFGRTL;
81 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
82 return IR_RTL_CFGLAYOUT;
83 else
84 gcc_unreachable ();
87 /* Verify the CFG consistency.
89 Currently it does following: checks edge and basic block list correctness
90 and calls into IL dependent checking then. */
92 DEBUG_FUNCTION void
93 verify_flow_info (void)
95 size_t *edge_checksum;
96 int err = 0;
97 basic_block bb, last_bb_seen;
98 basic_block *last_visited;
100 timevar_push (TV_CFG_VERIFY);
101 last_visited = XCNEWVEC (basic_block, last_basic_block_for_fn (cfun));
102 edge_checksum = XCNEWVEC (size_t, last_basic_block_for_fn (cfun));
104 /* Check bb chain & numbers. */
105 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
106 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
108 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
109 && bb != BASIC_BLOCK_FOR_FN (cfun, bb->index))
111 error ("bb %d on wrong place", bb->index);
112 err = 1;
115 if (bb->prev_bb != last_bb_seen)
117 error ("prev_bb of %d should be %d, not %d",
118 bb->index, last_bb_seen->index, bb->prev_bb->index);
119 err = 1;
122 last_bb_seen = bb;
125 /* Now check the basic blocks (boundaries etc.) */
126 FOR_EACH_BB_REVERSE_FN (bb, cfun)
128 int n_fallthru = 0;
129 edge e;
130 edge_iterator ei;
132 if (bb->loop_father != NULL && current_loops == NULL)
134 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
135 bb->index);
136 err = 1;
138 if (bb->loop_father == NULL && current_loops != NULL)
140 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
141 err = 1;
144 if (!bb->count.verify ())
146 error ("verify_flow_info: Wrong count of block %i", bb->index);
147 err = 1;
149 if (bb->frequency < 0)
151 error ("verify_flow_info: Wrong frequency of block %i %i",
152 bb->index, bb->frequency);
153 err = 1;
155 FOR_EACH_EDGE (e, ei, bb->succs)
157 if (last_visited [e->dest->index] == bb)
159 error ("verify_flow_info: Duplicate edge %i->%i",
160 e->src->index, e->dest->index);
161 err = 1;
163 if (!e->probability.verify ())
165 error ("verify_flow_info: Wrong probability of edge %i->%i",
166 e->src->index, e->dest->index);
167 err = 1;
169 if (!e->count.verify ())
171 error ("verify_flow_info: Wrong count of edge %i->%i",
172 e->src->index, e->dest->index);
173 err = 1;
176 last_visited [e->dest->index] = bb;
178 if (e->flags & EDGE_FALLTHRU)
179 n_fallthru++;
181 if (e->src != bb)
183 error ("verify_flow_info: Basic block %d succ edge is corrupted",
184 bb->index);
185 fprintf (stderr, "Predecessor: ");
186 dump_edge_info (stderr, e, TDF_DETAILS, 0);
187 fprintf (stderr, "\nSuccessor: ");
188 dump_edge_info (stderr, e, TDF_DETAILS, 1);
189 fprintf (stderr, "\n");
190 err = 1;
193 edge_checksum[e->dest->index] += (size_t) e;
195 if (n_fallthru > 1)
197 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
198 err = 1;
201 FOR_EACH_EDGE (e, ei, bb->preds)
203 if (e->dest != bb)
205 error ("basic block %d pred edge is corrupted", bb->index);
206 fputs ("Predecessor: ", stderr);
207 dump_edge_info (stderr, e, TDF_DETAILS, 0);
208 fputs ("\nSuccessor: ", stderr);
209 dump_edge_info (stderr, e, TDF_DETAILS, 1);
210 fputc ('\n', stderr);
211 err = 1;
214 if (ei.index != e->dest_idx)
216 error ("basic block %d pred edge is corrupted", bb->index);
217 error ("its dest_idx should be %d, not %d",
218 ei.index, e->dest_idx);
219 fputs ("Predecessor: ", stderr);
220 dump_edge_info (stderr, e, TDF_DETAILS, 0);
221 fputs ("\nSuccessor: ", stderr);
222 dump_edge_info (stderr, e, TDF_DETAILS, 1);
223 fputc ('\n', stderr);
224 err = 1;
227 edge_checksum[e->dest->index] -= (size_t) e;
231 /* Complete edge checksumming for ENTRY and EXIT. */
233 edge e;
234 edge_iterator ei;
236 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
237 edge_checksum[e->dest->index] += (size_t) e;
239 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
240 edge_checksum[e->dest->index] -= (size_t) e;
243 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
244 if (edge_checksum[bb->index])
246 error ("basic block %i edge lists are corrupted", bb->index);
247 err = 1;
250 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
252 /* Clean up. */
253 free (last_visited);
254 free (edge_checksum);
256 if (cfg_hooks->verify_flow_info)
257 err |= cfg_hooks->verify_flow_info ();
258 if (err)
259 internal_error ("verify_flow_info failed");
260 timevar_pop (TV_CFG_VERIFY);
263 /* Print out one basic block BB to file OUTF. INDENT is printed at the
264 start of each new line. FLAGS are the TDF_* flags in dumpfile.h.
266 This function takes care of the purely graph related information.
267 The cfg hook for the active representation should dump
268 representation-specific information. */
270 void
271 dump_bb (FILE *outf, basic_block bb, int indent, dump_flags_t flags)
273 if (flags & TDF_BLOCKS)
274 dump_bb_info (outf, bb, indent, flags, true, false);
275 if (cfg_hooks->dump_bb)
276 cfg_hooks->dump_bb (outf, bb, indent, flags);
277 if (flags & TDF_BLOCKS)
278 dump_bb_info (outf, bb, indent, flags, false, true);
279 fputc ('\n', outf);
282 DEBUG_FUNCTION void
283 debug (basic_block_def &ref)
285 dump_bb (stderr, &ref, 0, 0);
288 DEBUG_FUNCTION void
289 debug (basic_block_def *ptr)
291 if (ptr)
292 debug (*ptr);
293 else
294 fprintf (stderr, "<nil>\n");
298 /* Dumps basic block BB to pretty-printer PP, for use as a label of
299 a DOT graph record-node. The implementation of this hook is
300 expected to write the label to the stream that is attached to PP.
301 Field separators between instructions are pipe characters printed
302 verbatim. Instructions should be written with some characters
303 escaped, using pp_write_text_as_dot_label_to_stream(). */
305 void
306 dump_bb_for_graph (pretty_printer *pp, basic_block bb)
308 if (!cfg_hooks->dump_bb_for_graph)
309 internal_error ("%s does not support dump_bb_for_graph",
310 cfg_hooks->name);
311 /* TODO: Add pretty printer for counter. */
312 if (bb->count.initialized_p ())
313 pp_printf (pp, "COUNT:" "%" PRId64, bb->count.to_gcov_type ());
314 pp_printf (pp, " FREQ:%i |", bb->frequency);
315 pp_write_text_to_stream (pp);
316 if (!(dump_flags & TDF_SLIM))
317 cfg_hooks->dump_bb_for_graph (pp, bb);
320 /* Dump the complete CFG to FILE. FLAGS are the TDF_* flags in dumpfile.h. */
321 void
322 dump_flow_info (FILE *file, dump_flags_t flags)
324 basic_block bb;
326 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks_for_fn (cfun),
327 n_edges_for_fn (cfun));
328 FOR_ALL_BB_FN (bb, cfun)
329 dump_bb (file, bb, 0, flags);
331 putc ('\n', file);
334 /* Like above, but dump to stderr. To be called from debuggers. */
335 void debug_flow_info (void);
336 DEBUG_FUNCTION void
337 debug_flow_info (void)
339 dump_flow_info (stderr, TDF_DETAILS);
342 /* Redirect edge E to the given basic block DEST and update underlying program
343 representation. Returns edge representing redirected branch (that may not
344 be equivalent to E in the case of duplicate edges being removed) or NULL
345 if edge is not easily redirectable for whatever reason. */
347 edge
348 redirect_edge_and_branch (edge e, basic_block dest)
350 edge ret;
352 if (!cfg_hooks->redirect_edge_and_branch)
353 internal_error ("%s does not support redirect_edge_and_branch",
354 cfg_hooks->name);
356 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
358 /* If RET != E, then either the redirection failed, or the edge E
359 was removed since RET already lead to the same destination. */
360 if (current_loops != NULL && ret == e)
361 rescan_loop_exit (e, false, false);
363 return ret;
366 /* Returns true if it is possible to remove the edge E by redirecting it
367 to the destination of the other edge going from its source. */
369 bool
370 can_remove_branch_p (const_edge e)
372 if (!cfg_hooks->can_remove_branch_p)
373 internal_error ("%s does not support can_remove_branch_p",
374 cfg_hooks->name);
376 if (EDGE_COUNT (e->src->succs) != 2)
377 return false;
379 return cfg_hooks->can_remove_branch_p (e);
382 /* Removes E, by redirecting it to the destination of the other edge going
383 from its source. Can_remove_branch_p must be true for E, hence this
384 operation cannot fail. */
386 void
387 remove_branch (edge e)
389 edge other;
390 basic_block src = e->src;
391 int irr;
393 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
395 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
396 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
398 e = redirect_edge_and_branch (e, other->dest);
399 gcc_assert (e != NULL);
401 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
402 e->flags |= irr;
405 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
407 void
408 remove_edge (edge e)
410 if (current_loops != NULL)
412 rescan_loop_exit (e, false, true);
414 /* Removal of an edge inside an irreducible region or which leads
415 to an irreducible region can turn the region into a natural loop.
416 In that case, ask for the loop structure fixups.
418 FIXME: Note that LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS is not always
419 set, so always ask for fixups when removing an edge in that case. */
420 if (!loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
421 || (e->flags & EDGE_IRREDUCIBLE_LOOP)
422 || (e->dest->flags & BB_IRREDUCIBLE_LOOP))
423 loops_state_set (LOOPS_NEED_FIXUP);
426 /* This is probably not needed, but it doesn't hurt. */
427 /* FIXME: This should be called via a remove_edge hook. */
428 if (current_ir_type () == IR_GIMPLE)
429 redirect_edge_var_map_clear (e);
431 remove_edge_raw (e);
434 /* Like redirect_edge_succ but avoid possible duplicate edge. */
436 edge
437 redirect_edge_succ_nodup (edge e, basic_block new_succ)
439 edge s;
441 s = find_edge (e->src, new_succ);
442 if (s && s != e)
444 s->flags |= e->flags;
445 s->probability += e->probability;
446 s->count += e->count;
447 /* FIXME: This should be called via a hook and only for IR_GIMPLE. */
448 redirect_edge_var_map_dup (s, e);
449 remove_edge (e);
450 e = s;
452 else
453 redirect_edge_succ (e, new_succ);
455 return e;
458 /* Redirect the edge E to basic block DEST even if it requires creating
459 of a new basic block; then it returns the newly created basic block.
460 Aborts when redirection is impossible. */
462 basic_block
463 redirect_edge_and_branch_force (edge e, basic_block dest)
465 basic_block ret, src = e->src;
467 if (!cfg_hooks->redirect_edge_and_branch_force)
468 internal_error ("%s does not support redirect_edge_and_branch_force",
469 cfg_hooks->name);
471 if (current_loops != NULL)
472 rescan_loop_exit (e, false, true);
474 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
476 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
477 set_immediate_dominator (CDI_DOMINATORS, ret, src);
479 if (current_loops != NULL)
481 if (ret != NULL)
483 struct loop *loop
484 = find_common_loop (single_pred (ret)->loop_father,
485 single_succ (ret)->loop_father);
486 add_bb_to_loop (ret, loop);
488 else if (find_edge (src, dest) == e)
489 rescan_loop_exit (e, true, false);
492 return ret;
495 /* Splits basic block BB after the specified instruction I (but at least after
496 the labels). If I is NULL, splits just after labels. The newly created edge
497 is returned. The new basic block is created just after the old one. */
499 static edge
500 split_block_1 (basic_block bb, void *i)
502 basic_block new_bb;
503 edge res;
505 if (!cfg_hooks->split_block)
506 internal_error ("%s does not support split_block", cfg_hooks->name);
508 new_bb = cfg_hooks->split_block (bb, i);
509 if (!new_bb)
510 return NULL;
512 new_bb->count = bb->count;
513 new_bb->frequency = bb->frequency;
514 new_bb->discriminator = bb->discriminator;
516 if (dom_info_available_p (CDI_DOMINATORS))
518 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
519 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
522 if (current_loops != NULL)
524 edge_iterator ei;
525 edge e;
526 add_bb_to_loop (new_bb, bb->loop_father);
527 /* Identify all loops bb may have been the latch of and adjust them. */
528 FOR_EACH_EDGE (e, ei, new_bb->succs)
529 if (e->dest->loop_father->latch == bb)
530 e->dest->loop_father->latch = new_bb;
533 res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
535 if (bb->flags & BB_IRREDUCIBLE_LOOP)
537 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
538 res->flags |= EDGE_IRREDUCIBLE_LOOP;
541 return res;
544 edge
545 split_block (basic_block bb, gimple *i)
547 return split_block_1 (bb, i);
550 edge
551 split_block (basic_block bb, rtx i)
553 return split_block_1 (bb, i);
556 /* Splits block BB just after labels. The newly created edge is returned. */
558 edge
559 split_block_after_labels (basic_block bb)
561 return split_block_1 (bb, NULL);
564 /* Moves block BB immediately after block AFTER. Returns false if the
565 movement was impossible. */
567 bool
568 move_block_after (basic_block bb, basic_block after)
570 bool ret;
572 if (!cfg_hooks->move_block_after)
573 internal_error ("%s does not support move_block_after", cfg_hooks->name);
575 ret = cfg_hooks->move_block_after (bb, after);
577 return ret;
580 /* Deletes the basic block BB. */
582 void
583 delete_basic_block (basic_block bb)
585 if (!cfg_hooks->delete_basic_block)
586 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
588 cfg_hooks->delete_basic_block (bb);
590 if (current_loops != NULL)
592 struct loop *loop = bb->loop_father;
594 /* If we remove the header or the latch of a loop, mark the loop for
595 removal. */
596 if (loop->latch == bb
597 || loop->header == bb)
598 mark_loop_for_removal (loop);
600 remove_bb_from_loops (bb);
603 /* Remove the edges into and out of this block. Note that there may
604 indeed be edges in, if we are removing an unreachable loop. */
605 while (EDGE_COUNT (bb->preds) != 0)
606 remove_edge (EDGE_PRED (bb, 0));
607 while (EDGE_COUNT (bb->succs) != 0)
608 remove_edge (EDGE_SUCC (bb, 0));
610 if (dom_info_available_p (CDI_DOMINATORS))
611 delete_from_dominance_info (CDI_DOMINATORS, bb);
612 if (dom_info_available_p (CDI_POST_DOMINATORS))
613 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
615 /* Remove the basic block from the array. */
616 expunge_block (bb);
619 /* Splits edge E and returns the newly created basic block. */
621 basic_block
622 split_edge (edge e)
624 basic_block ret;
625 profile_count count = e->count;
626 int freq = EDGE_FREQUENCY (e);
627 edge f;
628 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
629 struct loop *loop;
630 basic_block src = e->src, dest = e->dest;
632 if (!cfg_hooks->split_edge)
633 internal_error ("%s does not support split_edge", cfg_hooks->name);
635 if (current_loops != NULL)
636 rescan_loop_exit (e, false, true);
638 ret = cfg_hooks->split_edge (e);
639 ret->count = count;
640 ret->frequency = freq;
641 single_succ_edge (ret)->probability = profile_probability::always ();
642 single_succ_edge (ret)->count = count;
644 if (irr)
646 ret->flags |= BB_IRREDUCIBLE_LOOP;
647 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
648 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
651 if (dom_info_available_p (CDI_DOMINATORS))
652 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
654 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
656 /* There are two cases:
658 If the immediate dominator of e->dest is not e->src, it
659 remains unchanged.
661 If immediate dominator of e->dest is e->src, it may become
662 ret, provided that all other predecessors of e->dest are
663 dominated by e->dest. */
665 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
666 == single_pred (ret))
668 edge_iterator ei;
669 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
671 if (f == single_succ_edge (ret))
672 continue;
674 if (!dominated_by_p (CDI_DOMINATORS, f->src,
675 single_succ (ret)))
676 break;
679 if (!f)
680 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
684 if (current_loops != NULL)
686 loop = find_common_loop (src->loop_father, dest->loop_father);
687 add_bb_to_loop (ret, loop);
689 /* If we split the latch edge of loop adjust the latch block. */
690 if (loop->latch == src
691 && loop->header == dest)
692 loop->latch = ret;
695 return ret;
698 /* Creates a new basic block just after the basic block AFTER.
699 HEAD and END are the first and the last statement belonging
700 to the block. If both are NULL, an empty block is created. */
702 static basic_block
703 create_basic_block_1 (void *head, void *end, basic_block after)
705 basic_block ret;
707 if (!cfg_hooks->create_basic_block)
708 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
710 ret = cfg_hooks->create_basic_block (head, end, after);
712 if (dom_info_available_p (CDI_DOMINATORS))
713 add_to_dominance_info (CDI_DOMINATORS, ret);
714 if (dom_info_available_p (CDI_POST_DOMINATORS))
715 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
717 return ret;
720 basic_block
721 create_basic_block (gimple_seq seq, basic_block after)
723 return create_basic_block_1 (seq, NULL, after);
726 basic_block
727 create_basic_block (rtx head, rtx end, basic_block after)
729 return create_basic_block_1 (head, end, after);
733 /* Creates an empty basic block just after basic block AFTER. */
735 basic_block
736 create_empty_bb (basic_block after)
738 return create_basic_block_1 (NULL, NULL, after);
741 /* Checks whether we may merge blocks BB1 and BB2. */
743 bool
744 can_merge_blocks_p (basic_block bb1, basic_block bb2)
746 bool ret;
748 if (!cfg_hooks->can_merge_blocks_p)
749 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
751 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
753 return ret;
756 void
757 predict_edge (edge e, enum br_predictor predictor, int probability)
759 if (!cfg_hooks->predict_edge)
760 internal_error ("%s does not support predict_edge", cfg_hooks->name);
762 cfg_hooks->predict_edge (e, predictor, probability);
765 bool
766 predicted_by_p (const_basic_block bb, enum br_predictor predictor)
768 if (!cfg_hooks->predict_edge)
769 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
771 return cfg_hooks->predicted_by_p (bb, predictor);
774 /* Merges basic block B into basic block A. */
776 void
777 merge_blocks (basic_block a, basic_block b)
779 edge e;
780 edge_iterator ei;
782 if (!cfg_hooks->merge_blocks)
783 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
785 cfg_hooks->merge_blocks (a, b);
787 if (current_loops != NULL)
789 /* If the block we merge into is a loop header do nothing unless ... */
790 if (a->loop_father->header == a)
792 /* ... we merge two loop headers, in which case we kill
793 the inner loop. */
794 if (b->loop_father->header == b)
795 mark_loop_for_removal (b->loop_father);
797 /* If we merge a loop header into its predecessor, update the loop
798 structure. */
799 else if (b->loop_father->header == b)
801 remove_bb_from_loops (a);
802 add_bb_to_loop (a, b->loop_father);
803 a->loop_father->header = a;
805 /* If we merge a loop latch into its predecessor, update the loop
806 structure. */
807 if (b->loop_father->latch
808 && b->loop_father->latch == b)
809 b->loop_father->latch = a;
810 remove_bb_from_loops (b);
813 /* Normally there should only be one successor of A and that is B, but
814 partway though the merge of blocks for conditional_execution we'll
815 be merging a TEST block with THEN and ELSE successors. Free the
816 whole lot of them and hope the caller knows what they're doing. */
818 while (EDGE_COUNT (a->succs) != 0)
819 remove_edge (EDGE_SUCC (a, 0));
821 /* Adjust the edges out of B for the new owner. */
822 FOR_EACH_EDGE (e, ei, b->succs)
824 e->src = a;
825 if (current_loops != NULL)
827 /* If b was a latch, a now is. */
828 if (e->dest->loop_father->latch == b)
829 e->dest->loop_father->latch = a;
830 rescan_loop_exit (e, true, false);
833 a->succs = b->succs;
834 a->flags |= b->flags;
836 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
837 b->preds = b->succs = NULL;
839 if (dom_info_available_p (CDI_DOMINATORS))
840 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
842 if (dom_info_available_p (CDI_DOMINATORS))
843 delete_from_dominance_info (CDI_DOMINATORS, b);
844 if (dom_info_available_p (CDI_POST_DOMINATORS))
845 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
847 expunge_block (b);
850 /* Split BB into entry part and the rest (the rest is the newly created block).
851 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
852 part. Returns the edge connecting the entry part to the rest. */
854 edge
855 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
856 void (*new_bb_cbk) (basic_block))
858 edge e, fallthru;
859 edge_iterator ei;
860 basic_block dummy, jump;
861 struct loop *loop, *ploop, *cloop;
863 if (!cfg_hooks->make_forwarder_block)
864 internal_error ("%s does not support make_forwarder_block",
865 cfg_hooks->name);
867 fallthru = split_block_after_labels (bb);
868 dummy = fallthru->src;
869 dummy->count = profile_count::zero ();
870 dummy->frequency = 0;
871 fallthru->count = profile_count::zero ();
872 bb = fallthru->dest;
874 /* Redirect back edges we want to keep. */
875 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
877 basic_block e_src;
879 if (redirect_edge_p (e))
881 dummy->frequency += EDGE_FREQUENCY (e);
882 if (dummy->frequency > BB_FREQ_MAX)
883 dummy->frequency = BB_FREQ_MAX;
885 dummy->count += e->count;
886 fallthru->count += e->count;
887 ei_next (&ei);
888 continue;
891 e_src = e->src;
892 jump = redirect_edge_and_branch_force (e, bb);
893 if (jump != NULL)
895 /* If we redirected the loop latch edge, the JUMP block now acts like
896 the new latch of the loop. */
897 if (current_loops != NULL
898 && dummy->loop_father != NULL
899 && dummy->loop_father->header == dummy
900 && dummy->loop_father->latch == e_src)
901 dummy->loop_father->latch = jump;
903 if (new_bb_cbk != NULL)
904 new_bb_cbk (jump);
908 if (dom_info_available_p (CDI_DOMINATORS))
910 vec<basic_block> doms_to_fix;
911 doms_to_fix.create (2);
912 doms_to_fix.quick_push (dummy);
913 doms_to_fix.quick_push (bb);
914 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
915 doms_to_fix.release ();
918 if (current_loops != NULL)
920 /* If we do not split a loop header, then both blocks belong to the
921 same loop. In case we split loop header and do not redirect the
922 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
923 BB becomes the new header. If latch is not recorded for the loop,
924 we leave this updating on the caller (this may only happen during
925 loop analysis). */
926 loop = dummy->loop_father;
927 if (loop->header == dummy
928 && loop->latch != NULL
929 && find_edge (loop->latch, dummy) == NULL)
931 remove_bb_from_loops (dummy);
932 loop->header = bb;
934 cloop = loop;
935 FOR_EACH_EDGE (e, ei, dummy->preds)
937 cloop = find_common_loop (cloop, e->src->loop_father);
939 add_bb_to_loop (dummy, cloop);
942 /* In case we split loop latch, update it. */
943 for (ploop = loop; ploop; ploop = loop_outer (ploop))
944 if (ploop->latch == dummy)
945 ploop->latch = bb;
948 cfg_hooks->make_forwarder_block (fallthru);
950 return fallthru;
953 /* Try to make the edge fallthru. */
955 void
956 tidy_fallthru_edge (edge e)
958 if (cfg_hooks->tidy_fallthru_edge)
959 cfg_hooks->tidy_fallthru_edge (e);
962 /* Fix up edges that now fall through, or rather should now fall through
963 but previously required a jump around now deleted blocks. Simplify
964 the search by only examining blocks numerically adjacent, since this
965 is how they were created.
967 ??? This routine is currently RTL specific. */
969 void
970 tidy_fallthru_edges (void)
972 basic_block b, c;
974 if (!cfg_hooks->tidy_fallthru_edge)
975 return;
977 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
978 return;
980 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
981 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, next_bb)
983 edge s;
985 c = b->next_bb;
987 /* We care about simple conditional or unconditional jumps with
988 a single successor.
990 If we had a conditional branch to the next instruction when
991 CFG was built, then there will only be one out edge for the
992 block which ended with the conditional branch (since we do
993 not create duplicate edges).
995 Furthermore, the edge will be marked as a fallthru because we
996 merge the flags for the duplicate edges. So we do not want to
997 check that the edge is not a FALLTHRU edge. */
999 if (single_succ_p (b))
1001 s = single_succ_edge (b);
1002 if (! (s->flags & EDGE_COMPLEX)
1003 && s->dest == c
1004 && !(JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b))))
1005 tidy_fallthru_edge (s);
1010 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1011 (and possibly create new basic block) to make edge non-fallthru.
1012 Return newly created BB or NULL if none. */
1014 basic_block
1015 force_nonfallthru (edge e)
1017 basic_block ret, src = e->src;
1019 if (!cfg_hooks->force_nonfallthru)
1020 internal_error ("%s does not support force_nonfallthru",
1021 cfg_hooks->name);
1023 ret = cfg_hooks->force_nonfallthru (e);
1024 if (ret != NULL)
1026 if (dom_info_available_p (CDI_DOMINATORS))
1027 set_immediate_dominator (CDI_DOMINATORS, ret, src);
1029 if (current_loops != NULL)
1031 basic_block pred = single_pred (ret);
1032 basic_block succ = single_succ (ret);
1033 struct loop *loop
1034 = find_common_loop (pred->loop_father, succ->loop_father);
1035 rescan_loop_exit (e, false, true);
1036 add_bb_to_loop (ret, loop);
1038 /* If we split the latch edge of loop adjust the latch block. */
1039 if (loop->latch == pred
1040 && loop->header == succ)
1041 loop->latch = ret;
1045 return ret;
1048 /* Returns true if we can duplicate basic block BB. */
1050 bool
1051 can_duplicate_block_p (const_basic_block bb)
1053 if (!cfg_hooks->can_duplicate_block_p)
1054 internal_error ("%s does not support can_duplicate_block_p",
1055 cfg_hooks->name);
1057 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1058 return false;
1060 return cfg_hooks->can_duplicate_block_p (bb);
1063 /* Duplicates basic block BB and redirects edge E to it. Returns the
1064 new basic block. The new basic block is placed after the basic block
1065 AFTER. */
1067 basic_block
1068 duplicate_block (basic_block bb, edge e, basic_block after)
1070 edge s, n;
1071 basic_block new_bb;
1072 profile_count new_count = e ? e->count : profile_count::uninitialized ();
1073 edge_iterator ei;
1075 if (!cfg_hooks->duplicate_block)
1076 internal_error ("%s does not support duplicate_block",
1077 cfg_hooks->name);
1079 if (bb->count < new_count)
1080 new_count = bb->count;
1082 gcc_checking_assert (can_duplicate_block_p (bb));
1084 new_bb = cfg_hooks->duplicate_block (bb);
1085 if (after)
1086 move_block_after (new_bb, after);
1088 new_bb->flags = (bb->flags & ~BB_DUPLICATED);
1089 FOR_EACH_EDGE (s, ei, bb->succs)
1091 /* Since we are creating edges from a new block to successors
1092 of another block (which therefore are known to be disjoint), there
1093 is no need to actually check for duplicated edges. */
1094 n = unchecked_make_edge (new_bb, s->dest, s->flags);
1095 n->probability = s->probability;
1096 if (e && bb->count > profile_count::zero ())
1098 n->count = s->count.apply_scale (new_count, bb->count);
1099 s->count -= n->count;
1101 else
1102 n->count = s->count;
1103 n->aux = s->aux;
1106 if (e)
1108 new_bb->count = new_count;
1109 bb->count -= new_count;
1111 new_bb->frequency = EDGE_FREQUENCY (e);
1112 bb->frequency -= EDGE_FREQUENCY (e);
1114 redirect_edge_and_branch_force (e, new_bb);
1116 if (bb->frequency < 0)
1117 bb->frequency = 0;
1119 else
1121 new_bb->count = bb->count;
1122 new_bb->frequency = bb->frequency;
1125 set_bb_original (new_bb, bb);
1126 set_bb_copy (bb, new_bb);
1128 /* Add the new block to the copy of the loop of BB, or directly to the loop
1129 of BB if the loop is not being copied. */
1130 if (current_loops != NULL)
1132 struct loop *cloop = bb->loop_father;
1133 struct loop *copy = get_loop_copy (cloop);
1134 /* If we copied the loop header block but not the loop
1135 we have created a loop with multiple entries. Ditch the loop,
1136 add the new block to the outer loop and arrange for a fixup. */
1137 if (!copy
1138 && cloop->header == bb)
1140 add_bb_to_loop (new_bb, loop_outer (cloop));
1141 mark_loop_for_removal (cloop);
1143 else
1145 add_bb_to_loop (new_bb, copy ? copy : cloop);
1146 /* If we copied the loop latch block but not the loop, adjust
1147 loop state. */
1148 if (!copy
1149 && cloop->latch == bb)
1151 cloop->latch = NULL;
1152 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1157 return new_bb;
1160 /* Return 1 if BB ends with a call, possibly followed by some
1161 instructions that must stay with the call, 0 otherwise. */
1163 bool
1164 block_ends_with_call_p (basic_block bb)
1166 if (!cfg_hooks->block_ends_with_call_p)
1167 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
1169 return (cfg_hooks->block_ends_with_call_p) (bb);
1172 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1174 bool
1175 block_ends_with_condjump_p (const_basic_block bb)
1177 if (!cfg_hooks->block_ends_with_condjump_p)
1178 internal_error ("%s does not support block_ends_with_condjump_p",
1179 cfg_hooks->name);
1181 return (cfg_hooks->block_ends_with_condjump_p) (bb);
1184 /* Add fake edges to the function exit for any non constant and non noreturn
1185 calls, volatile inline assembly in the bitmap of blocks specified by
1186 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
1187 that were split.
1189 The goal is to expose cases in which entering a basic block does not imply
1190 that all subsequent instructions must be executed. */
1193 flow_call_edges_add (sbitmap blocks)
1195 if (!cfg_hooks->flow_call_edges_add)
1196 internal_error ("%s does not support flow_call_edges_add",
1197 cfg_hooks->name);
1199 return (cfg_hooks->flow_call_edges_add) (blocks);
1202 /* This function is called immediately after edge E is added to the
1203 edge vector E->dest->preds. */
1205 void
1206 execute_on_growing_pred (edge e)
1208 if (! (e->dest->flags & BB_DUPLICATED)
1209 && cfg_hooks->execute_on_growing_pred)
1210 cfg_hooks->execute_on_growing_pred (e);
1213 /* This function is called immediately before edge E is removed from
1214 the edge vector E->dest->preds. */
1216 void
1217 execute_on_shrinking_pred (edge e)
1219 if (! (e->dest->flags & BB_DUPLICATED)
1220 && cfg_hooks->execute_on_shrinking_pred)
1221 cfg_hooks->execute_on_shrinking_pred (e);
1224 /* This is used inside loop versioning when we want to insert
1225 stmts/insns on the edges, which have a different behavior
1226 in tree's and in RTL, so we made a CFG hook. */
1227 void
1228 lv_flush_pending_stmts (edge e)
1230 if (cfg_hooks->flush_pending_stmts)
1231 cfg_hooks->flush_pending_stmts (e);
1234 /* Loop versioning uses the duplicate_loop_to_header_edge to create
1235 a new version of the loop basic-blocks, the parameters here are
1236 exactly the same as in duplicate_loop_to_header_edge or
1237 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1238 additional work to maintain ssa information that's why there is
1239 a need to call the tree_duplicate_loop_to_header_edge rather
1240 than duplicate_loop_to_header_edge when we are in tree mode. */
1241 bool
1242 cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
1243 unsigned int ndupl,
1244 sbitmap wont_exit, edge orig,
1245 vec<edge> *to_remove,
1246 int flags)
1248 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
1249 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1250 ndupl, wont_exit,
1251 orig, to_remove,
1252 flags);
1255 /* Conditional jumps are represented differently in trees and RTL,
1256 this hook takes a basic block that is known to have a cond jump
1257 at its end and extracts the taken and not taken edges out of it
1258 and store it in E1 and E2 respectively. */
1259 void
1260 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1262 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1263 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1266 /* Responsible for updating the ssa info (PHI nodes) on the
1267 new condition basic block that guards the versioned loop. */
1268 void
1269 lv_adjust_loop_header_phi (basic_block first, basic_block second,
1270 basic_block new_block, edge e)
1272 if (cfg_hooks->lv_adjust_loop_header_phi)
1273 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1276 /* Conditions in trees and RTL are different so we need
1277 a different handling when we add the condition to the
1278 versioning code. */
1279 void
1280 lv_add_condition_to_bb (basic_block first, basic_block second,
1281 basic_block new_block, void *cond)
1283 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1284 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
1287 /* Checks whether all N blocks in BBS array can be copied. */
1288 bool
1289 can_copy_bbs_p (basic_block *bbs, unsigned n)
1291 unsigned i;
1292 edge e;
1293 int ret = true;
1295 for (i = 0; i < n; i++)
1296 bbs[i]->flags |= BB_DUPLICATED;
1298 for (i = 0; i < n; i++)
1300 /* In case we should redirect abnormal edge during duplication, fail. */
1301 edge_iterator ei;
1302 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1303 if ((e->flags & EDGE_ABNORMAL)
1304 && (e->dest->flags & BB_DUPLICATED))
1306 ret = false;
1307 goto end;
1310 if (!can_duplicate_block_p (bbs[i]))
1312 ret = false;
1313 break;
1317 end:
1318 for (i = 0; i < n; i++)
1319 bbs[i]->flags &= ~BB_DUPLICATED;
1321 return ret;
1324 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1325 are placed into array NEW_BBS in the same order. Edges from basic blocks
1326 in BBS are also duplicated and copies of those that lead into BBS are
1327 redirected to appropriate newly created block. The function assigns bbs
1328 into loops (copy of basic block bb is assigned to bb->loop_father->copy
1329 loop, so this must be set up correctly in advance)
1331 If UPDATE_DOMINANCE is true then this function updates dominators locally
1332 (LOOPS structure that contains the information about dominators is passed
1333 to enable this), otherwise it does not update the dominator information
1334 and it assumed that the caller will do this, perhaps by destroying and
1335 recreating it instead of trying to do an incremental update like this
1336 function does when update_dominance is true.
1338 BASE is the superloop to that basic block belongs; if its header or latch
1339 is copied, we do not set the new blocks as header or latch.
1341 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1342 also in the same order.
1344 Newly created basic blocks are put after the basic block AFTER in the
1345 instruction stream, and the order of the blocks in BBS array is preserved. */
1347 void
1348 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1349 edge *edges, unsigned num_edges, edge *new_edges,
1350 struct loop *base, basic_block after, bool update_dominance)
1352 unsigned i, j;
1353 basic_block bb, new_bb, dom_bb;
1354 edge e;
1356 /* Mark the blocks to be copied. This is used by edge creation hooks
1357 to decide whether to reallocate PHI nodes capacity to avoid reallocating
1358 PHIs in the set of source BBs. */
1359 for (i = 0; i < n; i++)
1360 bbs[i]->flags |= BB_DUPLICATED;
1362 /* Duplicate bbs, update dominators, assign bbs to loops. */
1363 for (i = 0; i < n; i++)
1365 /* Duplicate. */
1366 bb = bbs[i];
1367 new_bb = new_bbs[i] = duplicate_block (bb, NULL, after);
1368 after = new_bb;
1369 if (bb->loop_father)
1371 /* Possibly set loop header. */
1372 if (bb->loop_father->header == bb && bb->loop_father != base)
1373 new_bb->loop_father->header = new_bb;
1374 /* Or latch. */
1375 if (bb->loop_father->latch == bb && bb->loop_father != base)
1376 new_bb->loop_father->latch = new_bb;
1380 /* Set dominators. */
1381 if (update_dominance)
1383 for (i = 0; i < n; i++)
1385 bb = bbs[i];
1386 new_bb = new_bbs[i];
1388 dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1389 if (dom_bb->flags & BB_DUPLICATED)
1391 dom_bb = get_bb_copy (dom_bb);
1392 set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1397 /* Redirect edges. */
1398 for (j = 0; j < num_edges; j++)
1399 new_edges[j] = NULL;
1400 for (i = 0; i < n; i++)
1402 edge_iterator ei;
1403 new_bb = new_bbs[i];
1404 bb = bbs[i];
1406 FOR_EACH_EDGE (e, ei, new_bb->succs)
1408 for (j = 0; j < num_edges; j++)
1409 if (edges[j] && edges[j]->src == bb && edges[j]->dest == e->dest)
1410 new_edges[j] = e;
1412 if (!(e->dest->flags & BB_DUPLICATED))
1413 continue;
1414 redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1418 /* Clear information about duplicates. */
1419 for (i = 0; i < n; i++)
1420 bbs[i]->flags &= ~BB_DUPLICATED;
1423 /* Return true if BB contains only labels or non-executable
1424 instructions */
1425 bool
1426 empty_block_p (basic_block bb)
1428 gcc_assert (cfg_hooks->empty_block_p);
1429 return cfg_hooks->empty_block_p (bb);
1432 /* Split a basic block if it ends with a conditional branch and if
1433 the other part of the block is not empty. */
1434 basic_block
1435 split_block_before_cond_jump (basic_block bb)
1437 gcc_assert (cfg_hooks->split_block_before_cond_jump);
1438 return cfg_hooks->split_block_before_cond_jump (bb);
1441 /* Work-horse for passes.c:check_profile_consistency.
1442 Do book-keeping of the CFG for the profile consistency checker.
1443 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
1444 then do post-pass accounting. Store the counting in RECORD. */
1446 void
1447 account_profile_record (struct profile_record *record, int after_pass)
1449 basic_block bb;
1450 edge_iterator ei;
1451 edge e;
1453 FOR_ALL_BB_FN (bb, cfun)
1455 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
1456 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1458 profile_probability sum = profile_probability::never ();
1459 FOR_EACH_EDGE (e, ei, bb->succs)
1460 sum += e->probability;
1461 if (EDGE_COUNT (bb->succs)
1462 && sum.differs_from_p (profile_probability::always ()))
1463 record->num_mismatched_freq_out[after_pass]++;
1464 profile_count lsum = profile_count::zero ();
1465 FOR_EACH_EDGE (e, ei, bb->succs)
1466 lsum += e->count;
1467 if (EDGE_COUNT (bb->succs) && (lsum.differs_from_p (bb->count)))
1468 record->num_mismatched_count_out[after_pass]++;
1470 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1471 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1473 int sum = 0;
1474 FOR_EACH_EDGE (e, ei, bb->preds)
1475 sum += EDGE_FREQUENCY (e);
1476 if (abs (sum - bb->frequency) > 100
1477 || (MAX (sum, bb->frequency) > 10
1478 && abs ((sum - bb->frequency) * 100 / (MAX (sum, bb->frequency) + 1)) > 10))
1479 record->num_mismatched_freq_in[after_pass]++;
1480 profile_count lsum = profile_count::zero ();
1481 FOR_EACH_EDGE (e, ei, bb->preds)
1482 lsum += e->count;
1483 if (lsum.differs_from_p (bb->count))
1484 record->num_mismatched_count_in[after_pass]++;
1486 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1487 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1488 continue;
1489 gcc_assert (cfg_hooks->account_profile_record);
1490 cfg_hooks->account_profile_record (bb, after_pass, record);