1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation,
4 Contributed by Sebastian Pop <s.pop@laposte.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "tree-flow.h"
34 /* A pointer to one of the hooks containers. */
35 static struct cfg_hooks
*cfg_hooks
;
37 /* Initialization of functions specific to the rtl IR. */
39 rtl_register_cfg_hooks (void)
41 cfg_hooks
= &rtl_cfg_hooks
;
44 /* Initialization of functions specific to the rtl IR. */
46 cfg_layout_rtl_register_cfg_hooks (void)
48 cfg_hooks
= &cfg_layout_rtl_cfg_hooks
;
51 /* Initialization of functions specific to the tree IR. */
54 tree_register_cfg_hooks (void)
56 cfg_hooks
= &tree_cfg_hooks
;
59 /* Returns current ir type. */
62 current_ir_type (void)
64 if (cfg_hooks
== &tree_cfg_hooks
)
66 else if (cfg_hooks
== &rtl_cfg_hooks
)
68 else if (cfg_hooks
== &cfg_layout_rtl_cfg_hooks
)
69 return IR_RTL_CFGLAYOUT
;
74 /* Verify the CFG consistency.
76 Currently it does following: checks edge and basic block list correctness
77 and calls into IL dependent checking then. */
80 verify_flow_info (void)
82 size_t *edge_checksum
;
84 basic_block bb
, last_bb_seen
;
85 basic_block
*last_visited
;
87 timevar_push (TV_CFG_VERIFY
);
88 last_visited
= XCNEWVEC (basic_block
, last_basic_block
);
89 edge_checksum
= XCNEWVEC (size_t, last_basic_block
);
91 /* Check bb chain & numbers. */
92 last_bb_seen
= ENTRY_BLOCK_PTR
;
93 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
95 if (bb
!= EXIT_BLOCK_PTR
96 && bb
!= BASIC_BLOCK (bb
->index
))
98 error ("bb %d on wrong place", bb
->index
);
102 if (bb
->prev_bb
!= last_bb_seen
)
104 error ("prev_bb of %d should be %d, not %d",
105 bb
->index
, last_bb_seen
->index
, bb
->prev_bb
->index
);
112 /* Now check the basic blocks (boundaries etc.) */
113 FOR_EACH_BB_REVERSE (bb
)
119 if (bb
->loop_father
!= NULL
&& current_loops
== NULL
)
121 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
125 if (bb
->loop_father
== NULL
&& current_loops
!= NULL
)
127 error ("verify_flow_info: Block %i lacks loop_father", bb
->index
);
133 error ("verify_flow_info: Wrong count of block %i %i",
134 bb
->index
, (int)bb
->count
);
137 if (bb
->frequency
< 0)
139 error ("verify_flow_info: Wrong frequency of block %i %i",
140 bb
->index
, bb
->frequency
);
143 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
145 if (last_visited
[e
->dest
->index
] == bb
)
147 error ("verify_flow_info: Duplicate edge %i->%i",
148 e
->src
->index
, e
->dest
->index
);
151 if (e
->probability
< 0 || e
->probability
> REG_BR_PROB_BASE
)
153 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
154 e
->src
->index
, e
->dest
->index
, e
->probability
);
159 error ("verify_flow_info: Wrong count of edge %i->%i %i",
160 e
->src
->index
, e
->dest
->index
, (int)e
->count
);
164 last_visited
[e
->dest
->index
] = bb
;
166 if (e
->flags
& EDGE_FALLTHRU
)
171 error ("verify_flow_info: Basic block %d succ edge is corrupted",
173 fprintf (stderr
, "Predecessor: ");
174 dump_edge_info (stderr
, e
, 0);
175 fprintf (stderr
, "\nSuccessor: ");
176 dump_edge_info (stderr
, e
, 1);
177 fprintf (stderr
, "\n");
181 edge_checksum
[e
->dest
->index
] += (size_t) e
;
185 error ("wrong amount of branch edges after unconditional jump %i", bb
->index
);
189 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
193 error ("basic block %d pred edge is corrupted", bb
->index
);
194 fputs ("Predecessor: ", stderr
);
195 dump_edge_info (stderr
, e
, 0);
196 fputs ("\nSuccessor: ", stderr
);
197 dump_edge_info (stderr
, e
, 1);
198 fputc ('\n', stderr
);
202 if (ei
.index
!= e
->dest_idx
)
204 error ("basic block %d pred edge is corrupted", bb
->index
);
205 error ("its dest_idx should be %d, not %d",
206 ei
.index
, e
->dest_idx
);
207 fputs ("Predecessor: ", stderr
);
208 dump_edge_info (stderr
, e
, 0);
209 fputs ("\nSuccessor: ", stderr
);
210 dump_edge_info (stderr
, e
, 1);
211 fputc ('\n', stderr
);
215 edge_checksum
[e
->dest
->index
] -= (size_t) e
;
219 /* Complete edge checksumming for ENTRY and EXIT. */
224 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
225 edge_checksum
[e
->dest
->index
] += (size_t) e
;
227 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
228 edge_checksum
[e
->dest
->index
] -= (size_t) e
;
231 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
232 if (edge_checksum
[bb
->index
])
234 error ("basic block %i edge lists are corrupted", bb
->index
);
238 last_bb_seen
= ENTRY_BLOCK_PTR
;
242 free (edge_checksum
);
244 if (cfg_hooks
->verify_flow_info
)
245 err
|= cfg_hooks
->verify_flow_info ();
247 internal_error ("verify_flow_info failed");
248 timevar_pop (TV_CFG_VERIFY
);
251 /* Print out one basic block. This function takes care of the purely
252 graph related information. The cfg hook for the active representation
253 should dump representation-specific information. */
256 dump_bb (basic_block bb
, FILE *outf
, int indent
)
262 s_indent
= (char *) alloca ((size_t) indent
+ 1);
263 memset (s_indent
, ' ', (size_t) indent
);
264 s_indent
[indent
] = '\0';
266 fprintf (outf
, ";;%s basic block %d, loop depth %d, count ",
267 s_indent
, bb
->index
, bb
->loop_depth
);
268 fprintf (outf
, HOST_WIDEST_INT_PRINT_DEC
, (HOST_WIDEST_INT
) bb
->count
);
271 fprintf (outf
, ";;%s prev block ", s_indent
);
273 fprintf (outf
, "%d, ", bb
->prev_bb
->index
);
275 fprintf (outf
, "(nil), ");
276 fprintf (outf
, "next block ");
278 fprintf (outf
, "%d", bb
->next_bb
->index
);
280 fprintf (outf
, "(nil)");
283 fprintf (outf
, ";;%s pred: ", s_indent
);
284 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
285 dump_edge_info (outf
, e
, 0);
288 fprintf (outf
, ";;%s succ: ", s_indent
);
289 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
290 dump_edge_info (outf
, e
, 1);
293 if (cfg_hooks
->dump_bb
)
294 cfg_hooks
->dump_bb (bb
, outf
, indent
);
297 /* Redirect edge E to the given basic block DEST and update underlying program
298 representation. Returns edge representing redirected branch (that may not
299 be equivalent to E in the case of duplicate edges being removed) or NULL
300 if edge is not easily redirectable for whatever reason. */
303 redirect_edge_and_branch (edge e
, basic_block dest
)
307 if (!cfg_hooks
->redirect_edge_and_branch
)
308 internal_error ("%s does not support redirect_edge_and_branch",
311 ret
= cfg_hooks
->redirect_edge_and_branch (e
, dest
);
313 /* If RET != E, then either the redirection failed, or the edge E
314 was removed since RET already lead to the same destination. */
315 if (current_loops
!= NULL
&& ret
== e
)
316 rescan_loop_exit (e
, false, false);
321 /* Returns true if it is possible to remove the edge E by redirecting it
322 to the destination of the other edge going from its source. */
325 can_remove_branch_p (const_edge e
)
327 if (!cfg_hooks
->can_remove_branch_p
)
328 internal_error ("%s does not support can_remove_branch_p",
331 if (EDGE_COUNT (e
->src
->succs
) != 2)
334 return cfg_hooks
->can_remove_branch_p (e
);
337 /* Removes E, by redirecting it to the destination of the other edge going
338 from its source. Can_remove_branch_p must be true for E, hence this
339 operation cannot fail. */
342 remove_branch (edge e
)
345 basic_block src
= e
->src
;
348 gcc_assert (EDGE_COUNT (e
->src
->succs
) == 2);
350 other
= EDGE_SUCC (src
, EDGE_SUCC (src
, 0) == e
);
351 irr
= other
->flags
& EDGE_IRREDUCIBLE_LOOP
;
353 e
= redirect_edge_and_branch (e
, other
->dest
);
354 gcc_assert (e
!= NULL
);
356 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
360 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
365 if (current_loops
!= NULL
)
366 rescan_loop_exit (e
, false, true);
371 /* Redirect the edge E to basic block DEST even if it requires creating
372 of a new basic block; then it returns the newly created basic block.
373 Aborts when redirection is impossible. */
376 redirect_edge_and_branch_force (edge e
, basic_block dest
)
378 basic_block ret
, src
= e
->src
;
381 if (!cfg_hooks
->redirect_edge_and_branch_force
)
382 internal_error ("%s does not support redirect_edge_and_branch_force",
385 if (current_loops
!= NULL
)
386 rescan_loop_exit (e
, false, true);
388 ret
= cfg_hooks
->redirect_edge_and_branch_force (e
, dest
);
390 && dom_info_available_p (CDI_DOMINATORS
))
391 set_immediate_dominator (CDI_DOMINATORS
, ret
, src
);
393 if (current_loops
!= NULL
)
397 loop
= find_common_loop (single_pred (ret
)->loop_father
,
398 single_succ (ret
)->loop_father
);
399 add_bb_to_loop (ret
, loop
);
401 else if (find_edge (src
, dest
) == e
)
402 rescan_loop_exit (e
, true, false);
408 /* Splits basic block BB after the specified instruction I (but at least after
409 the labels). If I is NULL, splits just after labels. The newly created edge
410 is returned. The new basic block is created just after the old one. */
413 split_block (basic_block bb
, void *i
)
417 if (!cfg_hooks
->split_block
)
418 internal_error ("%s does not support split_block", cfg_hooks
->name
);
420 new_bb
= cfg_hooks
->split_block (bb
, i
);
424 new_bb
->count
= bb
->count
;
425 new_bb
->frequency
= bb
->frequency
;
426 new_bb
->loop_depth
= bb
->loop_depth
;
428 if (dom_info_available_p (CDI_DOMINATORS
))
430 redirect_immediate_dominators (CDI_DOMINATORS
, bb
, new_bb
);
431 set_immediate_dominator (CDI_DOMINATORS
, new_bb
, bb
);
434 if (current_loops
!= NULL
)
436 add_bb_to_loop (new_bb
, bb
->loop_father
);
437 if (bb
->loop_father
->latch
== bb
)
438 bb
->loop_father
->latch
= new_bb
;
441 return make_single_succ_edge (bb
, new_bb
, EDGE_FALLTHRU
);
444 /* Splits block BB just after labels. The newly created edge is returned. */
447 split_block_after_labels (basic_block bb
)
449 return split_block (bb
, NULL
);
452 /* Moves block BB immediately after block AFTER. Returns false if the
453 movement was impossible. */
456 move_block_after (basic_block bb
, basic_block after
)
460 if (!cfg_hooks
->move_block_after
)
461 internal_error ("%s does not support move_block_after", cfg_hooks
->name
);
463 ret
= cfg_hooks
->move_block_after (bb
, after
);
468 /* Deletes the basic block BB. */
471 delete_basic_block (basic_block bb
)
473 if (!cfg_hooks
->delete_basic_block
)
474 internal_error ("%s does not support delete_basic_block", cfg_hooks
->name
);
476 cfg_hooks
->delete_basic_block (bb
);
478 if (current_loops
!= NULL
)
480 struct loop
*loop
= bb
->loop_father
;
482 /* If we remove the header or the latch of a loop, mark the loop for
483 removal by setting its header and latch to NULL. */
484 if (loop
->latch
== bb
485 || loop
->header
== bb
)
491 remove_bb_from_loops (bb
);
494 /* Remove the edges into and out of this block. Note that there may
495 indeed be edges in, if we are removing an unreachable loop. */
496 while (EDGE_COUNT (bb
->preds
) != 0)
497 remove_edge (EDGE_PRED (bb
, 0));
498 while (EDGE_COUNT (bb
->succs
) != 0)
499 remove_edge (EDGE_SUCC (bb
, 0));
501 if (dom_info_available_p (CDI_DOMINATORS
))
502 delete_from_dominance_info (CDI_DOMINATORS
, bb
);
503 if (dom_info_available_p (CDI_POST_DOMINATORS
))
504 delete_from_dominance_info (CDI_POST_DOMINATORS
, bb
);
506 /* Remove the basic block from the array. */
510 /* Splits edge E and returns the newly created basic block. */
516 gcov_type count
= e
->count
;
517 int freq
= EDGE_FREQUENCY (e
);
519 bool irr
= (e
->flags
& EDGE_IRREDUCIBLE_LOOP
) != 0;
521 basic_block src
= e
->src
, dest
= e
->dest
;
523 if (!cfg_hooks
->split_edge
)
524 internal_error ("%s does not support split_edge", cfg_hooks
->name
);
526 if (current_loops
!= NULL
)
527 rescan_loop_exit (e
, false, true);
529 ret
= cfg_hooks
->split_edge (e
);
531 ret
->frequency
= freq
;
532 single_succ_edge (ret
)->probability
= REG_BR_PROB_BASE
;
533 single_succ_edge (ret
)->count
= count
;
537 ret
->flags
|= BB_IRREDUCIBLE_LOOP
;
538 single_pred_edge (ret
)->flags
|= EDGE_IRREDUCIBLE_LOOP
;
539 single_succ_edge (ret
)->flags
|= EDGE_IRREDUCIBLE_LOOP
;
542 if (dom_info_available_p (CDI_DOMINATORS
))
543 set_immediate_dominator (CDI_DOMINATORS
, ret
, single_pred (ret
));
545 if (dom_info_state (CDI_DOMINATORS
) >= DOM_NO_FAST_QUERY
)
547 /* There are two cases:
549 If the immediate dominator of e->dest is not e->src, it
552 If immediate dominator of e->dest is e->src, it may become
553 ret, provided that all other predecessors of e->dest are
554 dominated by e->dest. */
556 if (get_immediate_dominator (CDI_DOMINATORS
, single_succ (ret
))
557 == single_pred (ret
))
560 FOR_EACH_EDGE (f
, ei
, single_succ (ret
)->preds
)
562 if (f
== single_succ_edge (ret
))
565 if (!dominated_by_p (CDI_DOMINATORS
, f
->src
,
571 set_immediate_dominator (CDI_DOMINATORS
, single_succ (ret
), ret
);
575 if (current_loops
!= NULL
)
577 loop
= find_common_loop (src
->loop_father
, dest
->loop_father
);
578 add_bb_to_loop (ret
, loop
);
580 if (loop
->latch
== src
)
587 /* Creates a new basic block just after the basic block AFTER.
588 HEAD and END are the first and the last statement belonging
589 to the block. If both are NULL, an empty block is created. */
592 create_basic_block (void *head
, void *end
, basic_block after
)
596 if (!cfg_hooks
->create_basic_block
)
597 internal_error ("%s does not support create_basic_block", cfg_hooks
->name
);
599 ret
= cfg_hooks
->create_basic_block (head
, end
, after
);
601 if (dom_info_available_p (CDI_DOMINATORS
))
602 add_to_dominance_info (CDI_DOMINATORS
, ret
);
603 if (dom_info_available_p (CDI_POST_DOMINATORS
))
604 add_to_dominance_info (CDI_POST_DOMINATORS
, ret
);
609 /* Creates an empty basic block just after basic block AFTER. */
612 create_empty_bb (basic_block after
)
614 return create_basic_block (NULL
, NULL
, after
);
617 /* Checks whether we may merge blocks BB1 and BB2. */
620 can_merge_blocks_p (basic_block bb1
, basic_block bb2
)
624 if (!cfg_hooks
->can_merge_blocks_p
)
625 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks
->name
);
627 ret
= cfg_hooks
->can_merge_blocks_p (bb1
, bb2
);
633 predict_edge (edge e
, enum br_predictor predictor
, int probability
)
635 if (!cfg_hooks
->predict_edge
)
636 internal_error ("%s does not support predict_edge", cfg_hooks
->name
);
638 cfg_hooks
->predict_edge (e
, predictor
, probability
);
642 predicted_by_p (const_basic_block bb
, enum br_predictor predictor
)
644 if (!cfg_hooks
->predict_edge
)
645 internal_error ("%s does not support predicted_by_p", cfg_hooks
->name
);
647 return cfg_hooks
->predicted_by_p (bb
, predictor
);
650 /* Merges basic block B into basic block A. */
653 merge_blocks (basic_block a
, basic_block b
)
658 if (!cfg_hooks
->merge_blocks
)
659 internal_error ("%s does not support merge_blocks", cfg_hooks
->name
);
661 cfg_hooks
->merge_blocks (a
, b
);
663 if (current_loops
!= NULL
)
664 remove_bb_from_loops (b
);
666 /* Normally there should only be one successor of A and that is B, but
667 partway though the merge of blocks for conditional_execution we'll
668 be merging a TEST block with THEN and ELSE successors. Free the
669 whole lot of them and hope the caller knows what they're doing. */
671 while (EDGE_COUNT (a
->succs
) != 0)
672 remove_edge (EDGE_SUCC (a
, 0));
674 /* Adjust the edges out of B for the new owner. */
675 FOR_EACH_EDGE (e
, ei
, b
->succs
)
678 if (current_loops
!= NULL
)
679 rescan_loop_exit (e
, true, false);
682 a
->flags
|= b
->flags
;
684 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
685 b
->preds
= b
->succs
= NULL
;
687 if (dom_info_available_p (CDI_DOMINATORS
))
688 redirect_immediate_dominators (CDI_DOMINATORS
, b
, a
);
690 if (dom_info_available_p (CDI_DOMINATORS
))
691 delete_from_dominance_info (CDI_DOMINATORS
, b
);
692 if (dom_info_available_p (CDI_POST_DOMINATORS
))
693 delete_from_dominance_info (CDI_POST_DOMINATORS
, b
);
698 /* Split BB into entry part and the rest (the rest is the newly created block).
699 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
700 part. Returns the edge connecting the entry part to the rest. */
703 make_forwarder_block (basic_block bb
, bool (*redirect_edge_p
) (edge
),
704 void (*new_bb_cbk
) (basic_block
))
708 basic_block dummy
, jump
;
709 struct loop
*loop
, *ploop
, *cloop
;
711 if (!cfg_hooks
->make_forwarder_block
)
712 internal_error ("%s does not support make_forwarder_block",
715 fallthru
= split_block_after_labels (bb
);
716 dummy
= fallthru
->src
;
719 /* Redirect back edges we want to keep. */
720 for (ei
= ei_start (dummy
->preds
); (e
= ei_safe_edge (ei
)); )
722 if (redirect_edge_p (e
))
728 dummy
->frequency
-= EDGE_FREQUENCY (e
);
729 dummy
->count
-= e
->count
;
730 if (dummy
->frequency
< 0)
731 dummy
->frequency
= 0;
732 if (dummy
->count
< 0)
734 fallthru
->count
-= e
->count
;
735 if (fallthru
->count
< 0)
738 jump
= redirect_edge_and_branch_force (e
, bb
);
740 && new_bb_cbk
!= NULL
)
744 if (dom_info_available_p (CDI_DOMINATORS
))
746 VEC (basic_block
, heap
) *doms_to_fix
= VEC_alloc (basic_block
, heap
, 2);
747 VEC_quick_push (basic_block
, doms_to_fix
, dummy
);
748 VEC_quick_push (basic_block
, doms_to_fix
, bb
);
749 iterate_fix_dominators (CDI_DOMINATORS
, doms_to_fix
, false);
750 VEC_free (basic_block
, heap
, doms_to_fix
);
753 if (current_loops
!= NULL
)
755 /* If we do not split a loop header, then both blocks belong to the
756 same loop. In case we split loop header and do not redirect the
757 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
758 BB becomes the new header. If latch is not recorded for the loop,
759 we leave this updating on the caller (this may only happen during
761 loop
= dummy
->loop_father
;
762 if (loop
->header
== dummy
763 && loop
->latch
!= NULL
764 && find_edge (loop
->latch
, dummy
) == NULL
)
766 remove_bb_from_loops (dummy
);
770 FOR_EACH_EDGE (e
, ei
, dummy
->preds
)
772 cloop
= find_common_loop (cloop
, e
->src
->loop_father
);
774 add_bb_to_loop (dummy
, cloop
);
777 /* In case we split loop latch, update it. */
778 for (ploop
= loop
; ploop
; ploop
= loop_outer (ploop
))
779 if (ploop
->latch
== dummy
)
783 cfg_hooks
->make_forwarder_block (fallthru
);
789 tidy_fallthru_edge (edge e
)
791 if (cfg_hooks
->tidy_fallthru_edge
)
792 cfg_hooks
->tidy_fallthru_edge (e
);
795 /* Fix up edges that now fall through, or rather should now fall through
796 but previously required a jump around now deleted blocks. Simplify
797 the search by only examining blocks numerically adjacent, since this
798 is how find_basic_blocks created them. */
801 tidy_fallthru_edges (void)
805 if (!cfg_hooks
->tidy_fallthru_edge
)
808 if (ENTRY_BLOCK_PTR
->next_bb
== EXIT_BLOCK_PTR
)
811 FOR_BB_BETWEEN (b
, ENTRY_BLOCK_PTR
->next_bb
, EXIT_BLOCK_PTR
->prev_bb
, next_bb
)
817 /* We care about simple conditional or unconditional jumps with
820 If we had a conditional branch to the next instruction when
821 find_basic_blocks was called, then there will only be one
822 out edge for the block which ended with the conditional
823 branch (since we do not create duplicate edges).
825 Furthermore, the edge will be marked as a fallthru because we
826 merge the flags for the duplicate edges. So we do not want to
827 check that the edge is not a FALLTHRU edge. */
829 if (single_succ_p (b
))
831 s
= single_succ_edge (b
);
832 if (! (s
->flags
& EDGE_COMPLEX
)
834 && !find_reg_note (BB_END (b
), REG_CROSSING_JUMP
, NULL_RTX
))
835 tidy_fallthru_edge (s
);
840 /* Returns true if we can duplicate basic block BB. */
843 can_duplicate_block_p (const_basic_block bb
)
845 if (!cfg_hooks
->can_duplicate_block_p
)
846 internal_error ("%s does not support can_duplicate_block_p",
849 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
)
852 return cfg_hooks
->can_duplicate_block_p (bb
);
855 /* Duplicates basic block BB and redirects edge E to it. Returns the
856 new basic block. The new basic block is placed after the basic block
860 duplicate_block (basic_block bb
, edge e
, basic_block after
)
864 gcov_type new_count
= e
? e
->count
: 0;
867 if (!cfg_hooks
->duplicate_block
)
868 internal_error ("%s does not support duplicate_block",
871 if (bb
->count
< new_count
)
872 new_count
= bb
->count
;
874 #ifdef ENABLE_CHECKING
875 gcc_assert (can_duplicate_block_p (bb
));
878 new_bb
= cfg_hooks
->duplicate_block (bb
);
880 move_block_after (new_bb
, after
);
882 new_bb
->loop_depth
= bb
->loop_depth
;
883 new_bb
->flags
= bb
->flags
;
884 FOR_EACH_EDGE (s
, ei
, bb
->succs
)
886 /* Since we are creating edges from a new block to successors
887 of another block (which therefore are known to be disjoint), there
888 is no need to actually check for duplicated edges. */
889 n
= unchecked_make_edge (new_bb
, s
->dest
, s
->flags
);
890 n
->probability
= s
->probability
;
893 /* Take care for overflows! */
894 n
->count
= s
->count
* (new_count
* 10000 / bb
->count
) / 10000;
895 s
->count
-= n
->count
;
904 new_bb
->count
= new_count
;
905 bb
->count
-= new_count
;
907 new_bb
->frequency
= EDGE_FREQUENCY (e
);
908 bb
->frequency
-= EDGE_FREQUENCY (e
);
910 redirect_edge_and_branch_force (e
, new_bb
);
914 if (bb
->frequency
< 0)
919 new_bb
->count
= bb
->count
;
920 new_bb
->frequency
= bb
->frequency
;
923 set_bb_original (new_bb
, bb
);
924 set_bb_copy (bb
, new_bb
);
926 /* Add the new block to the copy of the loop of BB, or directly to the loop
927 of BB if the loop is not being copied. */
928 if (current_loops
!= NULL
)
930 struct loop
*cloop
= bb
->loop_father
;
931 struct loop
*copy
= get_loop_copy (cloop
);
932 add_bb_to_loop (new_bb
, copy
? copy
: cloop
);
938 /* Return 1 if BB ends with a call, possibly followed by some
939 instructions that must stay with the call, 0 otherwise. */
942 block_ends_with_call_p (basic_block bb
)
944 if (!cfg_hooks
->block_ends_with_call_p
)
945 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks
->name
);
947 return (cfg_hooks
->block_ends_with_call_p
) (bb
);
950 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
953 block_ends_with_condjump_p (const_basic_block bb
)
955 if (!cfg_hooks
->block_ends_with_condjump_p
)
956 internal_error ("%s does not support block_ends_with_condjump_p",
959 return (cfg_hooks
->block_ends_with_condjump_p
) (bb
);
962 /* Add fake edges to the function exit for any non constant and non noreturn
963 calls, volatile inline assembly in the bitmap of blocks specified by
964 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
967 The goal is to expose cases in which entering a basic block does not imply
968 that all subsequent instructions must be executed. */
971 flow_call_edges_add (sbitmap blocks
)
973 if (!cfg_hooks
->flow_call_edges_add
)
974 internal_error ("%s does not support flow_call_edges_add",
977 return (cfg_hooks
->flow_call_edges_add
) (blocks
);
980 /* This function is called immediately after edge E is added to the
981 edge vector E->dest->preds. */
984 execute_on_growing_pred (edge e
)
986 if (cfg_hooks
->execute_on_growing_pred
)
987 cfg_hooks
->execute_on_growing_pred (e
);
990 /* This function is called immediately before edge E is removed from
991 the edge vector E->dest->preds. */
994 execute_on_shrinking_pred (edge e
)
996 if (cfg_hooks
->execute_on_shrinking_pred
)
997 cfg_hooks
->execute_on_shrinking_pred (e
);
1000 /* This is used inside loop versioning when we want to insert
1001 stmts/insns on the edges, which have a different behavior
1002 in tree's and in RTL, so we made a CFG hook. */
1004 lv_flush_pending_stmts (edge e
)
1006 if (cfg_hooks
->flush_pending_stmts
)
1007 cfg_hooks
->flush_pending_stmts (e
);
1010 /* Loop versioning uses the duplicate_loop_to_header_edge to create
1011 a new version of the loop basic-blocks, the parameters here are
1012 exactly the same as in duplicate_loop_to_header_edge or
1013 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1014 additional work to maintain ssa information that's why there is
1015 a need to call the tree_duplicate_loop_to_header_edge rather
1016 than duplicate_loop_to_header_edge when we are in tree mode. */
1018 cfg_hook_duplicate_loop_to_header_edge (struct loop
*loop
, edge e
,
1020 sbitmap wont_exit
, edge orig
,
1021 VEC (edge
, heap
) **to_remove
,
1024 gcc_assert (cfg_hooks
->cfg_hook_duplicate_loop_to_header_edge
);
1025 return cfg_hooks
->cfg_hook_duplicate_loop_to_header_edge (loop
, e
,
1031 /* Conditional jumps are represented differently in trees and RTL,
1032 this hook takes a basic block that is known to have a cond jump
1033 at its end and extracts the taken and not taken edges out of it
1034 and store it in E1 and E2 respectively. */
1036 extract_cond_bb_edges (basic_block b
, edge
*e1
, edge
*e2
)
1038 gcc_assert (cfg_hooks
->extract_cond_bb_edges
);
1039 cfg_hooks
->extract_cond_bb_edges (b
, e1
, e2
);
1042 /* Responsible for updating the ssa info (PHI nodes) on the
1043 new condition basic block that guards the versioned loop. */
1045 lv_adjust_loop_header_phi (basic_block first
, basic_block second
,
1046 basic_block new_block
, edge e
)
1048 if (cfg_hooks
->lv_adjust_loop_header_phi
)
1049 cfg_hooks
->lv_adjust_loop_header_phi (first
, second
, new_block
, e
);
1052 /* Conditions in trees and RTL are different so we need
1053 a different handling when we add the condition to the
1056 lv_add_condition_to_bb (basic_block first
, basic_block second
,
1057 basic_block new_block
, void *cond
)
1059 gcc_assert (cfg_hooks
->lv_add_condition_to_bb
);
1060 cfg_hooks
->lv_add_condition_to_bb (first
, second
, new_block
, cond
);