* config/bfin/bfin.h (TARGET_CPU_CPP_BUILTINS): Define
[official-gcc/alias-decl.git] / gcc / cfghooks.c
blobe224ad81ca24f1bda63556e1ea85f35240eaefe9
1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003, 2004, 2005, 2007 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 "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "basic-block.h"
28 #include "tree-flow.h"
29 #include "timevar.h"
30 #include "toplev.h"
31 #include "cfgloop.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. */
37 void
38 rtl_register_cfg_hooks (void)
40 cfg_hooks = &rtl_cfg_hooks;
43 /* Initialization of functions specific to the rtl IR. */
44 void
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. */
52 void
53 tree_register_cfg_hooks (void)
55 cfg_hooks = &tree_cfg_hooks;
58 /* Returns current ir type. */
60 enum ir_type
61 current_ir_type (void)
63 if (cfg_hooks == &tree_cfg_hooks)
64 return IR_GIMPLE;
65 else if (cfg_hooks == &rtl_cfg_hooks)
66 return IR_RTL_CFGRTL;
67 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
68 return IR_RTL_CFGLAYOUT;
69 else
70 gcc_unreachable ();
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. */
78 void
79 verify_flow_info (void)
81 size_t *edge_checksum;
82 int err = 0;
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);
98 err = 1;
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);
105 err = 1;
108 last_bb_seen = bb;
111 /* Now check the basic blocks (boundaries etc.) */
112 FOR_EACH_BB_REVERSE (bb)
114 int n_fallthru = 0;
115 edge e;
116 edge_iterator ei;
118 if (bb->loop_father != NULL && current_loops == NULL)
120 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
121 bb->index);
122 err = 1;
124 if (bb->loop_father == NULL && current_loops != NULL)
126 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
127 err = 1;
130 if (bb->count < 0)
132 error ("verify_flow_info: Wrong count of block %i %i",
133 bb->index, (int)bb->count);
134 err = 1;
136 if (bb->frequency < 0)
138 error ("verify_flow_info: Wrong frequency of block %i %i",
139 bb->index, bb->frequency);
140 err = 1;
142 FOR_EACH_EDGE (e, ei, bb->succs)
144 if (last_visited [e->dest->index] == bb)
146 error ("verify_flow_info: Duplicate edge %i->%i",
147 e->src->index, e->dest->index);
148 err = 1;
150 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
152 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
153 e->src->index, e->dest->index, e->probability);
154 err = 1;
156 if (e->count < 0)
158 error ("verify_flow_info: Wrong count of edge %i->%i %i",
159 e->src->index, e->dest->index, (int)e->count);
160 err = 1;
163 last_visited [e->dest->index] = bb;
165 if (e->flags & EDGE_FALLTHRU)
166 n_fallthru++;
168 if (e->src != bb)
170 error ("verify_flow_info: Basic block %d succ edge is corrupted",
171 bb->index);
172 fprintf (stderr, "Predecessor: ");
173 dump_edge_info (stderr, e, 0);
174 fprintf (stderr, "\nSuccessor: ");
175 dump_edge_info (stderr, e, 1);
176 fprintf (stderr, "\n");
177 err = 1;
180 edge_checksum[e->dest->index] += (size_t) e;
182 if (n_fallthru > 1)
184 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
185 err = 1;
188 FOR_EACH_EDGE (e, ei, bb->preds)
190 if (e->dest != bb)
192 error ("basic block %d pred edge is corrupted", bb->index);
193 fputs ("Predecessor: ", stderr);
194 dump_edge_info (stderr, e, 0);
195 fputs ("\nSuccessor: ", stderr);
196 dump_edge_info (stderr, e, 1);
197 fputc ('\n', stderr);
198 err = 1;
201 if (ei.index != e->dest_idx)
203 error ("basic block %d pred edge is corrupted", bb->index);
204 error ("its dest_idx should be %d, not %d",
205 ei.index, e->dest_idx);
206 fputs ("Predecessor: ", stderr);
207 dump_edge_info (stderr, e, 0);
208 fputs ("\nSuccessor: ", stderr);
209 dump_edge_info (stderr, e, 1);
210 fputc ('\n', stderr);
211 err = 1;
214 edge_checksum[e->dest->index] -= (size_t) e;
218 /* Complete edge checksumming for ENTRY and EXIT. */
220 edge e;
221 edge_iterator ei;
223 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
224 edge_checksum[e->dest->index] += (size_t) e;
226 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
227 edge_checksum[e->dest->index] -= (size_t) e;
230 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
231 if (edge_checksum[bb->index])
233 error ("basic block %i edge lists are corrupted", bb->index);
234 err = 1;
237 last_bb_seen = ENTRY_BLOCK_PTR;
239 /* Clean up. */
240 free (last_visited);
241 free (edge_checksum);
243 if (cfg_hooks->verify_flow_info)
244 err |= cfg_hooks->verify_flow_info ();
245 if (err)
246 internal_error ("verify_flow_info failed");
247 timevar_pop (TV_CFG_VERIFY);
250 /* Print out one basic block. This function takes care of the purely
251 graph related information. The cfg hook for the active representation
252 should dump representation-specific information. */
254 void
255 dump_bb (basic_block bb, FILE *outf, int indent)
257 edge e;
258 edge_iterator ei;
259 char *s_indent;
261 s_indent = (char *) alloca ((size_t) indent + 1);
262 memset (s_indent, ' ', (size_t) indent);
263 s_indent[indent] = '\0';
265 fprintf (outf, ";;%s basic block %d, loop depth %d, count ",
266 s_indent, bb->index, bb->loop_depth);
267 fprintf (outf, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
268 putc ('\n', outf);
270 fprintf (outf, ";;%s prev block ", s_indent);
271 if (bb->prev_bb)
272 fprintf (outf, "%d, ", bb->prev_bb->index);
273 else
274 fprintf (outf, "(nil), ");
275 fprintf (outf, "next block ");
276 if (bb->next_bb)
277 fprintf (outf, "%d", bb->next_bb->index);
278 else
279 fprintf (outf, "(nil)");
280 putc ('\n', outf);
282 fprintf (outf, ";;%s pred: ", s_indent);
283 FOR_EACH_EDGE (e, ei, bb->preds)
284 dump_edge_info (outf, e, 0);
285 putc ('\n', outf);
287 fprintf (outf, ";;%s succ: ", s_indent);
288 FOR_EACH_EDGE (e, ei, bb->succs)
289 dump_edge_info (outf, e, 1);
290 putc ('\n', outf);
292 if (cfg_hooks->dump_bb)
293 cfg_hooks->dump_bb (bb, outf, indent);
296 /* Redirect edge E to the given basic block DEST and update underlying program
297 representation. Returns edge representing redirected branch (that may not
298 be equivalent to E in the case of duplicate edges being removed) or NULL
299 if edge is not easily redirectable for whatever reason. */
301 edge
302 redirect_edge_and_branch (edge e, basic_block dest)
304 edge ret;
306 if (!cfg_hooks->redirect_edge_and_branch)
307 internal_error ("%s does not support redirect_edge_and_branch",
308 cfg_hooks->name);
310 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
312 /* If RET != E, then either the redirection failed, or the edge E
313 was removed since RET already lead to the same destination. */
314 if (current_loops != NULL && ret == e)
315 rescan_loop_exit (e, false, false);
317 return ret;
320 /* Returns true if it is possible to remove the edge E by redirecting it
321 to the destination of the other edge going from its source. */
323 bool
324 can_remove_branch_p (edge e)
326 if (!cfg_hooks->can_remove_branch_p)
327 internal_error ("%s does not support can_remove_branch_p",
328 cfg_hooks->name);
330 if (EDGE_COUNT (e->src->succs) != 2)
331 return false;
333 return cfg_hooks->can_remove_branch_p (e);
336 /* Removes E, by redirecting it to the destination of the other edge going
337 from its source. Can_remove_branch_p must be true for E, hence this
338 operation cannot fail. */
340 void
341 remove_branch (edge e)
343 edge other;
344 basic_block src = e->src;
345 int irr;
347 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
349 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
350 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
352 e = redirect_edge_and_branch (e, other->dest);
353 gcc_assert (e != NULL);
355 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
356 e->flags |= irr;
359 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
361 void
362 remove_edge (edge e)
364 if (current_loops != NULL)
365 rescan_loop_exit (e, false, true);
367 remove_edge_raw (e);
370 /* Redirect the edge E to basic block DEST even if it requires creating
371 of a new basic block; then it returns the newly created basic block.
372 Aborts when redirection is impossible. */
374 basic_block
375 redirect_edge_and_branch_force (edge e, basic_block dest)
377 basic_block ret, src = e->src;
378 struct loop *loop;
380 if (!cfg_hooks->redirect_edge_and_branch_force)
381 internal_error ("%s does not support redirect_edge_and_branch_force",
382 cfg_hooks->name);
384 if (current_loops != NULL)
385 rescan_loop_exit (e, false, true);
387 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
388 if (ret != NULL
389 && dom_info_available_p (CDI_DOMINATORS))
390 set_immediate_dominator (CDI_DOMINATORS, ret, src);
392 if (current_loops != NULL)
394 if (ret != NULL)
396 loop = find_common_loop (single_pred (ret)->loop_father,
397 single_succ (ret)->loop_father);
398 add_bb_to_loop (ret, loop);
400 else if (find_edge (src, dest) == e)
401 rescan_loop_exit (e, true, false);
404 return ret;
407 /* Splits basic block BB after the specified instruction I (but at least after
408 the labels). If I is NULL, splits just after labels. The newly created edge
409 is returned. The new basic block is created just after the old one. */
411 edge
412 split_block (basic_block bb, void *i)
414 basic_block new_bb;
416 if (!cfg_hooks->split_block)
417 internal_error ("%s does not support split_block", cfg_hooks->name);
419 new_bb = cfg_hooks->split_block (bb, i);
420 if (!new_bb)
421 return NULL;
423 new_bb->count = bb->count;
424 new_bb->frequency = bb->frequency;
425 new_bb->loop_depth = bb->loop_depth;
427 if (dom_info_available_p (CDI_DOMINATORS))
429 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
430 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
433 if (current_loops != NULL)
435 add_bb_to_loop (new_bb, bb->loop_father);
436 if (bb->loop_father->latch == bb)
437 bb->loop_father->latch = new_bb;
440 return make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
443 /* Splits block BB just after labels. The newly created edge is returned. */
445 edge
446 split_block_after_labels (basic_block bb)
448 return split_block (bb, NULL);
451 /* Moves block BB immediately after block AFTER. Returns false if the
452 movement was impossible. */
454 bool
455 move_block_after (basic_block bb, basic_block after)
457 bool ret;
459 if (!cfg_hooks->move_block_after)
460 internal_error ("%s does not support move_block_after", cfg_hooks->name);
462 ret = cfg_hooks->move_block_after (bb, after);
464 return ret;
467 /* Deletes the basic block BB. */
469 void
470 delete_basic_block (basic_block bb)
472 if (!cfg_hooks->delete_basic_block)
473 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
475 cfg_hooks->delete_basic_block (bb);
477 if (current_loops != NULL)
479 struct loop *loop = bb->loop_father;
481 /* If we remove the header or the latch of a loop, mark the loop for
482 removal by setting its header and latch to NULL. */
483 if (loop->latch == bb
484 || loop->header == bb)
486 loop->header = NULL;
487 loop->latch = NULL;
490 remove_bb_from_loops (bb);
493 /* Remove the edges into and out of this block. Note that there may
494 indeed be edges in, if we are removing an unreachable loop. */
495 while (EDGE_COUNT (bb->preds) != 0)
496 remove_edge (EDGE_PRED (bb, 0));
497 while (EDGE_COUNT (bb->succs) != 0)
498 remove_edge (EDGE_SUCC (bb, 0));
500 if (dom_info_available_p (CDI_DOMINATORS))
501 delete_from_dominance_info (CDI_DOMINATORS, bb);
502 if (dom_info_available_p (CDI_POST_DOMINATORS))
503 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
505 /* Remove the basic block from the array. */
506 expunge_block (bb);
509 /* Splits edge E and returns the newly created basic block. */
511 basic_block
512 split_edge (edge e)
514 basic_block ret;
515 gcov_type count = e->count;
516 int freq = EDGE_FREQUENCY (e);
517 edge f;
518 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
519 struct loop *loop;
520 basic_block src = e->src, dest = e->dest;
522 if (!cfg_hooks->split_edge)
523 internal_error ("%s does not support split_edge", cfg_hooks->name);
525 if (current_loops != NULL)
526 rescan_loop_exit (e, false, true);
528 ret = cfg_hooks->split_edge (e);
529 ret->count = count;
530 ret->frequency = freq;
531 single_succ_edge (ret)->probability = REG_BR_PROB_BASE;
532 single_succ_edge (ret)->count = count;
534 if (irr)
536 ret->flags |= BB_IRREDUCIBLE_LOOP;
537 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
538 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
541 if (dom_info_available_p (CDI_DOMINATORS))
542 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
544 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
546 /* There are two cases:
548 If the immediate dominator of e->dest is not e->src, it
549 remains unchanged.
551 If immediate dominator of e->dest is e->src, it may become
552 ret, provided that all other predecessors of e->dest are
553 dominated by e->dest. */
555 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
556 == single_pred (ret))
558 edge_iterator ei;
559 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
561 if (f == single_succ_edge (ret))
562 continue;
564 if (!dominated_by_p (CDI_DOMINATORS, f->src,
565 single_succ (ret)))
566 break;
569 if (!f)
570 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
574 if (current_loops != NULL)
576 loop = find_common_loop (src->loop_father, dest->loop_father);
577 add_bb_to_loop (ret, loop);
579 if (loop->latch == src)
580 loop->latch = ret;
583 return ret;
586 /* Creates a new basic block just after the basic block AFTER.
587 HEAD and END are the first and the last statement belonging
588 to the block. If both are NULL, an empty block is created. */
590 basic_block
591 create_basic_block (void *head, void *end, basic_block after)
593 basic_block ret;
595 if (!cfg_hooks->create_basic_block)
596 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
598 ret = cfg_hooks->create_basic_block (head, end, after);
600 if (dom_info_available_p (CDI_DOMINATORS))
601 add_to_dominance_info (CDI_DOMINATORS, ret);
602 if (dom_info_available_p (CDI_POST_DOMINATORS))
603 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
605 return ret;
608 /* Creates an empty basic block just after basic block AFTER. */
610 basic_block
611 create_empty_bb (basic_block after)
613 return create_basic_block (NULL, NULL, after);
616 /* Checks whether we may merge blocks BB1 and BB2. */
618 bool
619 can_merge_blocks_p (basic_block bb1, basic_block bb2)
621 bool ret;
623 if (!cfg_hooks->can_merge_blocks_p)
624 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
626 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
628 return ret;
631 void
632 predict_edge (edge e, enum br_predictor predictor, int probability)
634 if (!cfg_hooks->predict_edge)
635 internal_error ("%s does not support predict_edge", cfg_hooks->name);
637 cfg_hooks->predict_edge (e, predictor, probability);
640 bool
641 predicted_by_p (basic_block bb, enum br_predictor predictor)
643 if (!cfg_hooks->predict_edge)
644 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
646 return cfg_hooks->predicted_by_p (bb, predictor);
649 /* Merges basic block B into basic block A. */
651 void
652 merge_blocks (basic_block a, basic_block b)
654 edge e;
655 edge_iterator ei;
657 if (!cfg_hooks->merge_blocks)
658 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
660 cfg_hooks->merge_blocks (a, b);
662 if (current_loops != NULL)
663 remove_bb_from_loops (b);
665 /* Normally there should only be one successor of A and that is B, but
666 partway though the merge of blocks for conditional_execution we'll
667 be merging a TEST block with THEN and ELSE successors. Free the
668 whole lot of them and hope the caller knows what they're doing. */
670 while (EDGE_COUNT (a->succs) != 0)
671 remove_edge (EDGE_SUCC (a, 0));
673 /* Adjust the edges out of B for the new owner. */
674 FOR_EACH_EDGE (e, ei, b->succs)
676 e->src = a;
677 if (current_loops != NULL)
678 rescan_loop_exit (e, true, false);
680 a->succs = b->succs;
681 a->flags |= b->flags;
683 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
684 b->preds = b->succs = NULL;
686 if (dom_info_available_p (CDI_DOMINATORS))
687 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
689 if (dom_info_available_p (CDI_DOMINATORS))
690 delete_from_dominance_info (CDI_DOMINATORS, b);
691 if (dom_info_available_p (CDI_POST_DOMINATORS))
692 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
694 expunge_block (b);
697 /* Split BB into entry part and the rest (the rest is the newly created block).
698 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
699 part. Returns the edge connecting the entry part to the rest. */
701 edge
702 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
703 void (*new_bb_cbk) (basic_block))
705 edge e, fallthru;
706 edge_iterator ei;
707 basic_block dummy, jump;
708 struct loop *loop, *ploop, *cloop;
710 if (!cfg_hooks->make_forwarder_block)
711 internal_error ("%s does not support make_forwarder_block",
712 cfg_hooks->name);
714 fallthru = split_block_after_labels (bb);
715 dummy = fallthru->src;
716 bb = fallthru->dest;
718 /* Redirect back edges we want to keep. */
719 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
721 if (redirect_edge_p (e))
723 ei_next (&ei);
724 continue;
727 dummy->frequency -= EDGE_FREQUENCY (e);
728 dummy->count -= e->count;
729 if (dummy->frequency < 0)
730 dummy->frequency = 0;
731 if (dummy->count < 0)
732 dummy->count = 0;
733 fallthru->count -= e->count;
734 if (fallthru->count < 0)
735 fallthru->count = 0;
737 jump = redirect_edge_and_branch_force (e, bb);
738 if (jump != NULL
739 && new_bb_cbk != NULL)
740 new_bb_cbk (jump);
743 if (dom_info_available_p (CDI_DOMINATORS))
745 VEC (basic_block, heap) *doms_to_fix = VEC_alloc (basic_block, heap, 2);
746 VEC_quick_push (basic_block, doms_to_fix, dummy);
747 VEC_quick_push (basic_block, doms_to_fix, bb);
748 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
749 VEC_free (basic_block, heap, doms_to_fix);
752 if (current_loops != NULL)
754 /* If we do not split a loop header, then both blocks belong to the
755 same loop. In case we split loop header and do not redirect the
756 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
757 BB becomes the new header. If latch is not recorded for the loop,
758 we leave this updating on the caller (this may only happen during
759 loop analysis). */
760 loop = dummy->loop_father;
761 if (loop->header == dummy
762 && loop->latch != NULL
763 && find_edge (loop->latch, dummy) == NULL)
765 remove_bb_from_loops (dummy);
766 loop->header = bb;
768 cloop = loop;
769 FOR_EACH_EDGE (e, ei, dummy->preds)
771 cloop = find_common_loop (cloop, e->src->loop_father);
773 add_bb_to_loop (dummy, cloop);
776 /* In case we split loop latch, update it. */
777 for (ploop = loop; ploop; ploop = loop_outer (ploop))
778 if (ploop->latch == dummy)
779 ploop->latch = bb;
782 cfg_hooks->make_forwarder_block (fallthru);
784 return fallthru;
787 void
788 tidy_fallthru_edge (edge e)
790 if (cfg_hooks->tidy_fallthru_edge)
791 cfg_hooks->tidy_fallthru_edge (e);
794 /* Fix up edges that now fall through, or rather should now fall through
795 but previously required a jump around now deleted blocks. Simplify
796 the search by only examining blocks numerically adjacent, since this
797 is how find_basic_blocks created them. */
799 void
800 tidy_fallthru_edges (void)
802 basic_block b, c;
804 if (!cfg_hooks->tidy_fallthru_edge)
805 return;
807 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
808 return;
810 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, next_bb)
812 edge s;
814 c = b->next_bb;
816 /* We care about simple conditional or unconditional jumps with
817 a single successor.
819 If we had a conditional branch to the next instruction when
820 find_basic_blocks was called, then there will only be one
821 out edge for the block which ended with the conditional
822 branch (since we do not create duplicate edges).
824 Furthermore, the edge will be marked as a fallthru because we
825 merge the flags for the duplicate edges. So we do not want to
826 check that the edge is not a FALLTHRU edge. */
828 if (single_succ_p (b))
830 s = single_succ_edge (b);
831 if (! (s->flags & EDGE_COMPLEX)
832 && s->dest == c
833 && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
834 tidy_fallthru_edge (s);
839 /* Returns true if we can duplicate basic block BB. */
841 bool
842 can_duplicate_block_p (basic_block bb)
844 if (!cfg_hooks->can_duplicate_block_p)
845 internal_error ("%s does not support can_duplicate_block_p",
846 cfg_hooks->name);
848 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR)
849 return false;
851 return cfg_hooks->can_duplicate_block_p (bb);
854 /* Duplicates basic block BB and redirects edge E to it. Returns the
855 new basic block. The new basic block is placed after the basic block
856 AFTER. */
858 basic_block
859 duplicate_block (basic_block bb, edge e, basic_block after)
861 edge s, n;
862 basic_block new_bb;
863 gcov_type new_count = e ? e->count : 0;
864 edge_iterator ei;
866 if (!cfg_hooks->duplicate_block)
867 internal_error ("%s does not support duplicate_block",
868 cfg_hooks->name);
870 if (bb->count < new_count)
871 new_count = bb->count;
873 #ifdef ENABLE_CHECKING
874 gcc_assert (can_duplicate_block_p (bb));
875 #endif
877 new_bb = cfg_hooks->duplicate_block (bb);
878 if (after)
879 move_block_after (new_bb, after);
881 new_bb->loop_depth = bb->loop_depth;
882 new_bb->flags = bb->flags;
883 FOR_EACH_EDGE (s, ei, bb->succs)
885 /* Since we are creating edges from a new block to successors
886 of another block (which therefore are known to be disjoint), there
887 is no need to actually check for duplicated edges. */
888 n = unchecked_make_edge (new_bb, s->dest, s->flags);
889 n->probability = s->probability;
890 if (e && bb->count)
892 /* Take care for overflows! */
893 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
894 s->count -= n->count;
896 else
897 n->count = s->count;
898 n->aux = s->aux;
901 if (e)
903 new_bb->count = new_count;
904 bb->count -= new_count;
906 new_bb->frequency = EDGE_FREQUENCY (e);
907 bb->frequency -= EDGE_FREQUENCY (e);
909 redirect_edge_and_branch_force (e, new_bb);
911 if (bb->count < 0)
912 bb->count = 0;
913 if (bb->frequency < 0)
914 bb->frequency = 0;
916 else
918 new_bb->count = bb->count;
919 new_bb->frequency = bb->frequency;
922 set_bb_original (new_bb, bb);
923 set_bb_copy (bb, new_bb);
925 /* Add the new block to the copy of the loop of BB, or directly to the loop
926 of BB if the loop is not being copied. */
927 if (current_loops != NULL)
929 struct loop *cloop = bb->loop_father;
930 struct loop *copy = get_loop_copy (cloop);
931 add_bb_to_loop (new_bb, copy ? copy : cloop);
934 return new_bb;
937 /* Return 1 if BB ends with a call, possibly followed by some
938 instructions that must stay with the call, 0 otherwise. */
940 bool
941 block_ends_with_call_p (basic_block bb)
943 if (!cfg_hooks->block_ends_with_call_p)
944 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
946 return (cfg_hooks->block_ends_with_call_p) (bb);
949 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
951 bool
952 block_ends_with_condjump_p (basic_block bb)
954 if (!cfg_hooks->block_ends_with_condjump_p)
955 internal_error ("%s does not support block_ends_with_condjump_p",
956 cfg_hooks->name);
958 return (cfg_hooks->block_ends_with_condjump_p) (bb);
961 /* Add fake edges to the function exit for any non constant and non noreturn
962 calls, volatile inline assembly in the bitmap of blocks specified by
963 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
964 that were split.
966 The goal is to expose cases in which entering a basic block does not imply
967 that all subsequent instructions must be executed. */
970 flow_call_edges_add (sbitmap blocks)
972 if (!cfg_hooks->flow_call_edges_add)
973 internal_error ("%s does not support flow_call_edges_add",
974 cfg_hooks->name);
976 return (cfg_hooks->flow_call_edges_add) (blocks);
979 /* This function is called immediately after edge E is added to the
980 edge vector E->dest->preds. */
982 void
983 execute_on_growing_pred (edge e)
985 if (cfg_hooks->execute_on_growing_pred)
986 cfg_hooks->execute_on_growing_pred (e);
989 /* This function is called immediately before edge E is removed from
990 the edge vector E->dest->preds. */
992 void
993 execute_on_shrinking_pred (edge e)
995 if (cfg_hooks->execute_on_shrinking_pred)
996 cfg_hooks->execute_on_shrinking_pred (e);
999 /* This is used inside loop versioning when we want to insert
1000 stmts/insns on the edges, which have a different behavior
1001 in tree's and in RTL, so we made a CFG hook. */
1002 void
1003 lv_flush_pending_stmts (edge e)
1005 if (cfg_hooks->flush_pending_stmts)
1006 cfg_hooks->flush_pending_stmts (e);
1009 /* Loop versioning uses the duplicate_loop_to_header_edge to create
1010 a new version of the loop basic-blocks, the parameters here are
1011 exactly the same as in duplicate_loop_to_header_edge or
1012 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1013 additional work to maintain ssa information that's why there is
1014 a need to call the tree_duplicate_loop_to_header_edge rather
1015 than duplicate_loop_to_header_edge when we are in tree mode. */
1016 bool
1017 cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
1018 unsigned int ndupl,
1019 sbitmap wont_exit, edge orig,
1020 VEC (edge, heap) **to_remove,
1021 int flags)
1023 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
1024 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1025 ndupl, wont_exit,
1026 orig, to_remove,
1027 flags);
1030 /* Conditional jumps are represented differently in trees and RTL,
1031 this hook takes a basic block that is known to have a cond jump
1032 at its end and extracts the taken and not taken eges out of it
1033 and store it in E1 and E2 respectively. */
1034 void
1035 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1037 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1038 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1041 /* Responsible for updating the ssa info (PHI nodes) on the
1042 new condition basic block that guards the versioned loop. */
1043 void
1044 lv_adjust_loop_header_phi (basic_block first, basic_block second,
1045 basic_block new_block, edge e)
1047 if (cfg_hooks->lv_adjust_loop_header_phi)
1048 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1051 /* Conditions in trees and RTL are different so we need
1052 a different handling when we add the condition to the
1053 versioning code. */
1054 void
1055 lv_add_condition_to_bb (basic_block first, basic_block second,
1056 basic_block new_block, void *cond)
1058 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1059 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);