1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003, 2004, 2005 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 2, or (at your option)
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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "tree-flow.h"
33 /* A pointer to one of the hooks containers. */
34 static struct cfg_hooks *cfg_hooks;
36 /* Initialization of functions specific to the rtl IR. */
38 rtl_register_cfg_hooks (void)
40 cfg_hooks = &rtl_cfg_hooks;
43 /* Initialization of functions specific to the rtl IR. */
45 cfg_layout_rtl_register_cfg_hooks (void)
47 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
50 /* Initialization of functions specific to the tree IR. */
53 tree_register_cfg_hooks (void)
55 cfg_hooks = &tree_cfg_hooks;
58 /* Returns current ir type. */
61 current_ir_type (void)
63 if (cfg_hooks == &tree_cfg_hooks)
65 else if (cfg_hooks == &rtl_cfg_hooks)
67 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
68 return IR_RTL_CFGLAYOUT;
73 /* Verify the CFG consistency.
75 Currently it does following: checks edge and basic block list correctness
76 and calls into IL dependent checking then. */
79 verify_flow_info (void)
81 size_t *edge_checksum;
83 basic_block bb, last_bb_seen;
84 basic_block *last_visited;
86 timevar_push (TV_CFG_VERIFY);
87 last_visited = XCNEWVEC (basic_block, last_basic_block);
88 edge_checksum = XCNEWVEC (size_t, last_basic_block);
90 /* Check bb chain & numbers. */
91 last_bb_seen = ENTRY_BLOCK_PTR;
92 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
94 if (bb != EXIT_BLOCK_PTR
95 && bb != BASIC_BLOCK (bb->index))
97 error ("bb %d on wrong place", bb->index);
101 if (bb->prev_bb != last_bb_seen)
103 error ("prev_bb of %d should be %d, not %d",
104 bb->index, last_bb_seen->index, bb->prev_bb->index);
111 /* Now check the basic blocks (boundaries etc.) */
112 FOR_EACH_BB_REVERSE (bb)
120 error ("verify_flow_info: Wrong count of block %i %i",
121 bb->index, (int)bb->count);
124 if (bb->frequency < 0)
126 error ("verify_flow_info: Wrong frequency of block %i %i",
127 bb->index, bb->frequency);
130 FOR_EACH_EDGE (e, ei, bb->succs)
132 if (last_visited [e->dest->index] == bb)
134 error ("verify_flow_info: Duplicate edge %i->%i",
135 e->src->index, e->dest->index);
138 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
140 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
141 e->src->index, e->dest->index, e->probability);
146 error ("verify_flow_info: Wrong count of edge %i->%i %i",
147 e->src->index, e->dest->index, (int)e->count);
151 last_visited [e->dest->index] = bb;
153 if (e->flags & EDGE_FALLTHRU)
158 error ("verify_flow_info: Basic block %d succ edge is corrupted",
160 fprintf (stderr, "Predecessor: ");
161 dump_edge_info (stderr, e, 0);
162 fprintf (stderr, "\nSuccessor: ");
163 dump_edge_info (stderr, e, 1);
164 fprintf (stderr, "\n");
168 edge_checksum[e->dest->index] += (size_t) e;
172 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
176 FOR_EACH_EDGE (e, ei, bb->preds)
180 error ("basic block %d pred edge is corrupted", bb->index);
181 fputs ("Predecessor: ", stderr);
182 dump_edge_info (stderr, e, 0);
183 fputs ("\nSuccessor: ", stderr);
184 dump_edge_info (stderr, e, 1);
185 fputc ('\n', stderr);
189 if (ei.index != e->dest_idx)
191 error ("basic block %d pred edge is corrupted", bb->index);
192 error ("its dest_idx should be %d, not %d",
193 ei.index, e->dest_idx);
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 edge_checksum[e->dest->index] -= (size_t) e;
206 /* Complete edge checksumming for ENTRY and EXIT. */
211 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
212 edge_checksum[e->dest->index] += (size_t) e;
214 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
215 edge_checksum[e->dest->index] -= (size_t) e;
218 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
219 if (edge_checksum[bb->index])
221 error ("basic block %i edge lists are corrupted", bb->index);
225 last_bb_seen = ENTRY_BLOCK_PTR;
229 free (edge_checksum);
231 if (cfg_hooks->verify_flow_info)
232 err |= cfg_hooks->verify_flow_info ();
234 internal_error ("verify_flow_info failed");
235 timevar_pop (TV_CFG_VERIFY);
238 /* Print out one basic block. This function takes care of the purely
239 graph related information. The cfg hook for the active representation
240 should dump representation-specific information. */
243 dump_bb (basic_block bb, FILE *outf, int indent)
249 s_indent = alloca ((size_t) indent + 1);
250 memset (s_indent, ' ', (size_t) indent);
251 s_indent[indent] = '\0';
253 fprintf (outf, ";;%s basic block %d, loop depth %d, count ",
254 s_indent, bb->index, bb->loop_depth);
255 fprintf (outf, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
258 fprintf (outf, ";;%s prev block ", s_indent);
260 fprintf (outf, "%d, ", bb->prev_bb->index);
262 fprintf (outf, "(nil), ");
263 fprintf (outf, "next block ");
265 fprintf (outf, "%d", bb->next_bb->index);
267 fprintf (outf, "(nil)");
270 fprintf (outf, ";;%s pred: ", s_indent);
271 FOR_EACH_EDGE (e, ei, bb->preds)
272 dump_edge_info (outf, e, 0);
275 fprintf (outf, ";;%s succ: ", s_indent);
276 FOR_EACH_EDGE (e, ei, bb->succs)
277 dump_edge_info (outf, e, 1);
280 if (cfg_hooks->dump_bb)
281 cfg_hooks->dump_bb (bb, outf, indent);
284 /* Redirect edge E to the given basic block DEST and update underlying program
285 representation. Returns edge representing redirected branch (that may not
286 be equivalent to E in the case of duplicate edges being removed) or NULL
287 if edge is not easily redirectable for whatever reason. */
290 redirect_edge_and_branch (edge e, basic_block dest)
294 if (!cfg_hooks->redirect_edge_and_branch)
295 internal_error ("%s does not support redirect_edge_and_branch",
298 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
303 /* Redirect the edge E to basic block DEST even if it requires creating
304 of a new basic block; then it returns the newly created basic block.
305 Aborts when redirection is impossible. */
308 redirect_edge_and_branch_force (edge e, basic_block dest)
312 if (!cfg_hooks->redirect_edge_and_branch_force)
313 internal_error ("%s does not support redirect_edge_and_branch_force",
316 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
321 /* Splits basic block BB after the specified instruction I (but at least after
322 the labels). If I is NULL, splits just after labels. The newly created edge
323 is returned. The new basic block is created just after the old one. */
326 split_block (basic_block bb, void *i)
330 if (!cfg_hooks->split_block)
331 internal_error ("%s does not support split_block", cfg_hooks->name);
333 new_bb = cfg_hooks->split_block (bb, i);
337 new_bb->count = bb->count;
338 new_bb->frequency = bb->frequency;
339 new_bb->loop_depth = bb->loop_depth;
341 if (dom_info_available_p (CDI_DOMINATORS))
343 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
344 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
347 return make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
350 /* Splits block BB just after labels. The newly created edge is returned. */
353 split_block_after_labels (basic_block bb)
355 return split_block (bb, NULL);
358 /* Moves block BB immediately after block AFTER. Returns false if the
359 movement was impossible. */
362 move_block_after (basic_block bb, basic_block after)
366 if (!cfg_hooks->move_block_after)
367 internal_error ("%s does not support move_block_after", cfg_hooks->name);
369 ret = cfg_hooks->move_block_after (bb, after);
374 /* Deletes the basic block BB. */
377 delete_basic_block (basic_block bb)
379 if (!cfg_hooks->delete_basic_block)
380 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
382 cfg_hooks->delete_basic_block (bb);
384 /* Remove the edges into and out of this block. Note that there may
385 indeed be edges in, if we are removing an unreachable loop. */
386 while (EDGE_COUNT (bb->preds) != 0)
387 remove_edge (EDGE_PRED (bb, 0));
388 while (EDGE_COUNT (bb->succs) != 0)
389 remove_edge (EDGE_SUCC (bb, 0));
391 if (dom_computed[CDI_DOMINATORS])
392 delete_from_dominance_info (CDI_DOMINATORS, bb);
393 if (dom_computed[CDI_POST_DOMINATORS])
394 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
396 /* Remove the basic block from the array. */
400 /* Splits edge E and returns the newly created basic block. */
406 gcov_type count = e->count;
407 int freq = EDGE_FREQUENCY (e);
409 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
411 if (!cfg_hooks->split_edge)
412 internal_error ("%s does not support split_edge", cfg_hooks->name);
414 ret = cfg_hooks->split_edge (e);
416 ret->frequency = freq;
417 single_succ_edge (ret)->probability = REG_BR_PROB_BASE;
418 single_succ_edge (ret)->count = count;
422 ret->flags |= BB_IRREDUCIBLE_LOOP;
423 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
424 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
427 if (dom_computed[CDI_DOMINATORS])
428 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
430 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
432 /* There are two cases:
434 If the immediate dominator of e->dest is not e->src, it
437 If immediate dominator of e->dest is e->src, it may become
438 ret, provided that all other predecessors of e->dest are
439 dominated by e->dest. */
441 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
442 == single_pred (ret))
445 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
447 if (f == single_succ_edge (ret))
450 if (!dominated_by_p (CDI_DOMINATORS, f->src,
456 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
463 /* Creates a new basic block just after the basic block AFTER.
464 HEAD and END are the first and the last statement belonging
465 to the block. If both are NULL, an empty block is created. */
468 create_basic_block (void *head, void *end, basic_block after)
472 if (!cfg_hooks->create_basic_block)
473 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
475 ret = cfg_hooks->create_basic_block (head, end, after);
477 if (dom_computed[CDI_DOMINATORS])
478 add_to_dominance_info (CDI_DOMINATORS, ret);
479 if (dom_computed[CDI_POST_DOMINATORS])
480 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
485 /* Creates an empty basic block just after basic block AFTER. */
488 create_empty_bb (basic_block after)
490 return create_basic_block (NULL, NULL, after);
493 /* Checks whether we may merge blocks BB1 and BB2. */
496 can_merge_blocks_p (basic_block bb1, basic_block bb2)
500 if (!cfg_hooks->can_merge_blocks_p)
501 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
503 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
509 predict_edge (edge e, enum br_predictor predictor, int probability)
511 if (!cfg_hooks->predict_edge)
512 internal_error ("%s does not support predict_edge", cfg_hooks->name);
514 cfg_hooks->predict_edge (e, predictor, probability);
518 predicted_by_p (basic_block bb, enum br_predictor predictor)
520 if (!cfg_hooks->predict_edge)
521 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
523 return cfg_hooks->predicted_by_p (bb, predictor);
526 /* Merges basic block B into basic block A. */
529 merge_blocks (basic_block a, basic_block b)
534 if (!cfg_hooks->merge_blocks)
535 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
537 cfg_hooks->merge_blocks (a, b);
539 /* Normally there should only be one successor of A and that is B, but
540 partway though the merge of blocks for conditional_execution we'll
541 be merging a TEST block with THEN and ELSE successors. Free the
542 whole lot of them and hope the caller knows what they're doing. */
544 while (EDGE_COUNT (a->succs) != 0)
545 remove_edge (EDGE_SUCC (a, 0));
547 /* Adjust the edges out of B for the new owner. */
548 FOR_EACH_EDGE (e, ei, b->succs)
551 a->flags |= b->flags;
553 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
554 b->preds = b->succs = NULL;
556 if (dom_computed[CDI_DOMINATORS])
557 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
559 if (dom_computed[CDI_DOMINATORS])
560 delete_from_dominance_info (CDI_DOMINATORS, b);
561 if (dom_computed[CDI_POST_DOMINATORS])
562 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
567 /* Split BB into entry part and the rest (the rest is the newly created block).
568 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
569 part. Returns the edge connecting the entry part to the rest. */
572 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
573 void (*new_bb_cbk) (basic_block))
577 basic_block dummy, jump;
579 if (!cfg_hooks->make_forwarder_block)
580 internal_error ("%s does not support make_forwarder_block",
583 fallthru = split_block_after_labels (bb);
584 dummy = fallthru->src;
587 /* Redirect back edges we want to keep. */
588 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
590 if (redirect_edge_p (e))
596 dummy->frequency -= EDGE_FREQUENCY (e);
597 dummy->count -= e->count;
598 if (dummy->frequency < 0)
599 dummy->frequency = 0;
600 if (dummy->count < 0)
602 fallthru->count -= e->count;
603 if (fallthru->count < 0)
606 jump = redirect_edge_and_branch_force (e, bb);
611 if (dom_info_available_p (CDI_DOMINATORS))
613 basic_block doms_to_fix[2];
615 doms_to_fix[0] = dummy;
617 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, 2);
620 cfg_hooks->make_forwarder_block (fallthru);
626 tidy_fallthru_edge (edge e)
628 if (cfg_hooks->tidy_fallthru_edge)
629 cfg_hooks->tidy_fallthru_edge (e);
632 /* Fix up edges that now fall through, or rather should now fall through
633 but previously required a jump around now deleted blocks. Simplify
634 the search by only examining blocks numerically adjacent, since this
635 is how find_basic_blocks created them. */
638 tidy_fallthru_edges (void)
642 if (!cfg_hooks->tidy_fallthru_edge)
645 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
648 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, next_bb)
654 /* We care about simple conditional or unconditional jumps with
657 If we had a conditional branch to the next instruction when
658 find_basic_blocks was called, then there will only be one
659 out edge for the block which ended with the conditional
660 branch (since we do not create duplicate edges).
662 Furthermore, the edge will be marked as a fallthru because we
663 merge the flags for the duplicate edges. So we do not want to
664 check that the edge is not a FALLTHRU edge. */
666 if (single_succ_p (b))
668 s = single_succ_edge (b);
669 if (! (s->flags & EDGE_COMPLEX)
671 && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
672 tidy_fallthru_edge (s);
677 /* Returns true if we can duplicate basic block BB. */
680 can_duplicate_block_p (basic_block bb)
684 if (!cfg_hooks->can_duplicate_block_p)
685 internal_error ("%s does not support can_duplicate_block_p",
688 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR)
691 /* Duplicating fallthru block to exit would require adding a jump
692 and splitting the real last BB. */
693 e = find_edge (bb, EXIT_BLOCK_PTR);
694 if (e && (e->flags & EDGE_FALLTHRU))
697 return cfg_hooks->can_duplicate_block_p (bb);
700 /* Duplicates basic block BB and redirects edge E to it. Returns the
701 new basic block. The new basic block is placed after the basic block
705 duplicate_block (basic_block bb, edge e, basic_block after)
709 gcov_type new_count = e ? e->count : 0;
712 if (!cfg_hooks->duplicate_block)
713 internal_error ("%s does not support duplicate_block",
716 if (bb->count < new_count)
717 new_count = bb->count;
719 #ifdef ENABLE_CHECKING
720 gcc_assert (can_duplicate_block_p (bb));
723 new_bb = cfg_hooks->duplicate_block (bb);
725 move_block_after (new_bb, after);
727 new_bb->loop_depth = bb->loop_depth;
728 new_bb->flags = bb->flags;
729 FOR_EACH_EDGE (s, ei, bb->succs)
731 /* Since we are creating edges from a new block to successors
732 of another block (which therefore are known to be disjoint), there
733 is no need to actually check for duplicated edges. */
734 n = unchecked_make_edge (new_bb, s->dest, s->flags);
735 n->probability = s->probability;
738 /* Take care for overflows! */
739 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
740 s->count -= n->count;
749 new_bb->count = new_count;
750 bb->count -= new_count;
752 new_bb->frequency = EDGE_FREQUENCY (e);
753 bb->frequency -= EDGE_FREQUENCY (e);
755 redirect_edge_and_branch_force (e, new_bb);
759 if (bb->frequency < 0)
764 new_bb->count = bb->count;
765 new_bb->frequency = bb->frequency;
768 set_bb_original (new_bb, bb);
769 set_bb_copy (bb, new_bb);
774 /* Return 1 if BB ends with a call, possibly followed by some
775 instructions that must stay with the call, 0 otherwise. */
778 block_ends_with_call_p (basic_block bb)
780 if (!cfg_hooks->block_ends_with_call_p)
781 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
783 return (cfg_hooks->block_ends_with_call_p) (bb);
786 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
789 block_ends_with_condjump_p (basic_block bb)
791 if (!cfg_hooks->block_ends_with_condjump_p)
792 internal_error ("%s does not support block_ends_with_condjump_p",
795 return (cfg_hooks->block_ends_with_condjump_p) (bb);
798 /* Add fake edges to the function exit for any non constant and non noreturn
799 calls, volatile inline assembly in the bitmap of blocks specified by
800 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
803 The goal is to expose cases in which entering a basic block does not imply
804 that all subsequent instructions must be executed. */
807 flow_call_edges_add (sbitmap blocks)
809 if (!cfg_hooks->flow_call_edges_add)
810 internal_error ("%s does not support flow_call_edges_add",
813 return (cfg_hooks->flow_call_edges_add) (blocks);
816 /* This function is called immediately after edge E is added to the
817 edge vector E->dest->preds. */
820 execute_on_growing_pred (edge e)
822 if (cfg_hooks->execute_on_growing_pred)
823 cfg_hooks->execute_on_growing_pred (e);
826 /* This function is called immediately before edge E is removed from
827 the edge vector E->dest->preds. */
830 execute_on_shrinking_pred (edge e)
832 if (cfg_hooks->execute_on_shrinking_pred)
833 cfg_hooks->execute_on_shrinking_pred (e);
836 /* This is used inside loop versioning when we want to insert
837 stmts/insns on the edges, which have a different behavior
838 in tree's and in RTL, so we made a CFG hook. */
840 lv_flush_pending_stmts (edge e)
842 if (cfg_hooks->flush_pending_stmts)
843 cfg_hooks->flush_pending_stmts (e);
846 /* Loop versioning uses the duplicate_loop_to_header_edge to create
847 a new version of the loop basic-blocks, the parameters here are
848 exactly the same as in duplicate_loop_to_header_edge or
849 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
850 additional work to maintain ssa information that's why there is
851 a need to call the tree_duplicate_loop_to_header_edge rather
852 than duplicate_loop_to_header_edge when we are in tree mode. */
854 cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
855 struct loops *loops, unsigned int ndupl,
856 sbitmap wont_exit, edge orig,
858 unsigned int *n_to_remove, int flags)
860 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
861 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e, loops,
867 /* Conditional jumps are represented differently in trees and RTL,
868 this hook takes a basic block that is known to have a cond jump
869 at its end and extracts the taken and not taken eges out of it
870 and store it in E1 and E2 respectively. */
872 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
874 gcc_assert (cfg_hooks->extract_cond_bb_edges);
875 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
878 /* Responsible for updating the ssa info (PHI nodes) on the
879 new condition basic block that guards the versioned loop. */
881 lv_adjust_loop_header_phi (basic_block first, basic_block second,
882 basic_block new, edge e)
884 if (cfg_hooks->lv_adjust_loop_header_phi)
885 cfg_hooks->lv_adjust_loop_header_phi (first, second, new, e);
888 /* Conditions in trees and RTL are different so we need
889 a different handling when we add the condition to the
892 lv_add_condition_to_bb (basic_block first, basic_block second,
893 basic_block new, void *cond)
895 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
896 cfg_hooks->lv_add_condition_to_bb (first, second, new, cond);