* c-decl.c (start_function): Set DECL_ARTIFICIAL and DECL_IGNORED_P
[official-gcc.git] / gcc / cfghooks.c
blobbd67a21dc93d641cac1715ed706f669d2cc77215
1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003, 2004 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)
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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "basic-block.h"
29 #include "tree-flow.h"
30 #include "timevar.h"
31 #include "toplev.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 (rtl = 0, trees = 1). */
60 int
61 ir_type (void)
63 return cfg_hooks == &tree_cfg_hooks ? 1 : 0;
66 /* Verify the CFG consistency.
68 Currently it does following: checks edge and basic block list correctness
69 and calls into IL dependent checking then. */
71 void
72 verify_flow_info (void)
74 size_t *edge_checksum;
75 int num_bb_notes, err = 0;
76 basic_block bb, last_bb_seen;
77 basic_block *last_visited;
79 timevar_push (TV_CFG_VERIFY);
80 last_visited = xcalloc (last_basic_block + 2, sizeof (basic_block));
81 edge_checksum = xcalloc (last_basic_block + 2, sizeof (size_t));
83 /* Check bb chain & numbers. */
84 last_bb_seen = ENTRY_BLOCK_PTR;
85 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
87 if (bb != EXIT_BLOCK_PTR
88 && bb != BASIC_BLOCK (bb->index))
90 error ("bb %d on wrong place", bb->index);
91 err = 1;
94 if (bb->prev_bb != last_bb_seen)
96 error ("prev_bb of %d should be %d, not %d",
97 bb->index, last_bb_seen->index, bb->prev_bb->index);
98 err = 1;
101 last_bb_seen = bb;
104 /* Now check the basic blocks (boundaries etc.) */
105 FOR_EACH_BB_REVERSE (bb)
107 int n_fallthru = 0;
108 edge e;
110 if (bb->count < 0)
112 error ("verify_flow_info: Wrong count of block %i %i",
113 bb->index, (int)bb->count);
114 err = 1;
116 if (bb->frequency < 0)
118 error ("verify_flow_info: Wrong frequency of block %i %i",
119 bb->index, bb->frequency);
120 err = 1;
122 for (e = bb->succ; e; e = e->succ_next)
124 if (last_visited [e->dest->index + 2] == bb)
126 error ("verify_flow_info: Duplicate edge %i->%i",
127 e->src->index, e->dest->index);
128 err = 1;
130 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
132 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
133 e->src->index, e->dest->index, e->probability);
134 err = 1;
136 if (e->count < 0)
138 error ("verify_flow_info: Wrong count of edge %i->%i %i",
139 e->src->index, e->dest->index, (int)e->count);
140 err = 1;
143 last_visited [e->dest->index + 2] = bb;
145 if (e->flags & EDGE_FALLTHRU)
146 n_fallthru++;
148 if (e->src != bb)
150 error ("verify_flow_info: Basic block %d succ edge is corrupted",
151 bb->index);
152 fprintf (stderr, "Predecessor: ");
153 dump_edge_info (stderr, e, 0);
154 fprintf (stderr, "\nSuccessor: ");
155 dump_edge_info (stderr, e, 1);
156 fprintf (stderr, "\n");
157 err = 1;
160 edge_checksum[e->dest->index + 2] += (size_t) e;
162 if (n_fallthru > 1)
164 error ("Wrong amount of branch edges after unconditional jump %i", bb->index);
165 err = 1;
168 for (e = bb->pred; e; e = e->pred_next)
170 if (e->dest != bb)
172 error ("basic block %d pred edge is corrupted", bb->index);
173 fputs ("Predecessor: ", stderr);
174 dump_edge_info (stderr, e, 0);
175 fputs ("\nSuccessor: ", stderr);
176 dump_edge_info (stderr, e, 1);
177 fputc ('\n', stderr);
178 err = 1;
180 edge_checksum[e->dest->index + 2] -= (size_t) e;
184 /* Complete edge checksumming for ENTRY and EXIT. */
186 edge e;
188 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
189 edge_checksum[e->dest->index + 2] += (size_t) e;
191 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
192 edge_checksum[e->dest->index + 2] -= (size_t) e;
195 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
196 if (edge_checksum[bb->index + 2])
198 error ("basic block %i edge lists are corrupted", bb->index);
199 err = 1;
202 num_bb_notes = 0;
203 last_bb_seen = ENTRY_BLOCK_PTR;
205 /* Clean up. */
206 free (last_visited);
207 free (edge_checksum);
209 if (cfg_hooks->verify_flow_info)
210 err |= cfg_hooks->verify_flow_info ();
211 if (err)
212 internal_error ("verify_flow_info failed");
213 timevar_pop (TV_CFG_VERIFY);
216 /* Print out one basic block. This function takes care of the purely
217 graph related information. The cfg hook for the active representation
218 should dump representation-specific information. */
220 void
221 dump_bb (basic_block bb, FILE *outf, int indent)
223 edge e;
224 char *s_indent;
226 s_indent = alloca ((size_t) indent + 1);
227 memset (s_indent, ' ', (size_t) indent);
228 s_indent[indent] = '\0';
230 fprintf (outf, ";;%s basic block %d, loop depth %d, count ",
231 s_indent, bb->index, bb->loop_depth);
232 fprintf (outf, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
233 putc ('\n', outf);
235 fprintf (outf, ";;%s prev block ", s_indent);
236 if (bb->prev_bb)
237 fprintf (outf, "%d, ", bb->prev_bb->index);
238 else
239 fprintf (outf, "(nil), ");
240 fprintf (outf, "next block ");
241 if (bb->next_bb)
242 fprintf (outf, "%d", bb->next_bb->index);
243 else
244 fprintf (outf, "(nil)");
245 putc ('\n', outf);
247 fprintf (outf, ";;%s pred: ", s_indent);
248 for (e = bb->pred; e; e = e->pred_next)
249 dump_edge_info (outf, e, 0);
250 putc ('\n', outf);
252 fprintf (outf, ";;%s succ: ", s_indent);
253 for (e = bb->succ; e; e = e->succ_next)
254 dump_edge_info (outf, e, 1);
255 putc ('\n', outf);
257 if (cfg_hooks->dump_bb)
258 cfg_hooks->dump_bb (bb, outf, indent);
261 /* Redirect edge E to the given basic block DEST and update underlying program
262 representation. Returns edge representing redirected branch (that may not
263 be equivalent to E in the case of duplicate edges being removed) or NULL
264 if edge is not easily redirectable for whatever reason. */
266 edge
267 redirect_edge_and_branch (edge e, basic_block dest)
269 edge ret;
271 if (!cfg_hooks->redirect_edge_and_branch)
272 internal_error ("%s does not support redirect_edge_and_branch.",
273 cfg_hooks->name);
275 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
277 return ret;
280 /* Redirect the edge E to basic block DEST even if it requires creating
281 of a new basic block; then it returns the newly created basic block.
282 Aborts when redirection is impossible. */
284 basic_block
285 redirect_edge_and_branch_force (edge e, basic_block dest)
287 basic_block ret;
289 if (!cfg_hooks->redirect_edge_and_branch_force)
290 internal_error ("%s does not support redirect_edge_and_branch_force.",
291 cfg_hooks->name);
293 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
295 return ret;
298 /* Splits basic block BB after the specified instruction I (but at least after
299 the labels). If I is NULL, splits just after labels. The newly created edge
300 is returned. The new basic block is created just after the old one. */
302 edge
303 split_block (basic_block bb, void *i)
305 basic_block new_bb;
307 if (!cfg_hooks->split_block)
308 internal_error ("%s does not support split_block.", cfg_hooks->name);
310 new_bb = cfg_hooks->split_block (bb, i);
311 if (!new_bb)
312 return NULL;
314 new_bb->count = bb->count;
315 new_bb->frequency = bb->frequency;
316 new_bb->loop_depth = bb->loop_depth;
318 if (dom_computed[CDI_DOMINATORS] >= DOM_CONS_OK)
320 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
321 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
324 return make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
327 /* Splits block BB just after labels. The newly created edge is returned. */
329 edge
330 split_block_after_labels (basic_block bb)
332 return split_block (bb, NULL);
335 /* Moves block BB immediately after block AFTER. Returns false if the
336 movement was impossible. */
338 bool
339 move_block_after (basic_block bb, basic_block after)
341 bool ret;
343 if (!cfg_hooks->move_block_after)
344 internal_error ("%s does not support move_block_after.", cfg_hooks->name);
346 ret = cfg_hooks->move_block_after (bb, after);
348 return ret;
351 /* Deletes the basic block BB. */
353 void
354 delete_basic_block (basic_block bb)
356 if (!cfg_hooks->delete_basic_block)
357 internal_error ("%s does not support delete_basic_block.", cfg_hooks->name);
359 cfg_hooks->delete_basic_block (bb);
361 /* Remove the edges into and out of this block. Note that there may
362 indeed be edges in, if we are removing an unreachable loop. */
363 while (bb->pred != NULL)
364 remove_edge (bb->pred);
365 while (bb->succ != NULL)
366 remove_edge (bb->succ);
368 bb->pred = NULL;
369 bb->succ = NULL;
371 if (dom_computed[CDI_DOMINATORS])
372 delete_from_dominance_info (CDI_DOMINATORS, bb);
373 if (dom_computed[CDI_POST_DOMINATORS])
374 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
376 /* Remove the basic block from the array. */
377 expunge_block (bb);
380 /* Splits edge E and returns the newly created basic block. */
382 basic_block
383 split_edge (edge e)
385 basic_block ret;
386 gcov_type count = e->count;
387 int freq = EDGE_FREQUENCY (e);
388 edge f;
390 if (!cfg_hooks->split_edge)
391 internal_error ("%s does not support split_edge.", cfg_hooks->name);
393 ret = cfg_hooks->split_edge (e);
394 ret->count = count;
395 ret->frequency = freq;
396 ret->succ->probability = REG_BR_PROB_BASE;
397 ret->succ->count = count;
399 if (dom_computed[CDI_DOMINATORS])
400 set_immediate_dominator (CDI_DOMINATORS, ret, ret->pred->src);
402 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
404 /* There are two cases:
406 If the immediate dominator of e->dest is not e->src, it
407 remains unchanged.
409 If immediate dominator of e->dest is e->src, it may become
410 ret, provided that all other predecessors of e->dest are
411 dominated by e->dest. */
413 if (get_immediate_dominator (CDI_DOMINATORS, ret->succ->dest)
414 == ret->pred->src)
416 for (f = ret->succ->dest->pred; f; f = f->pred_next)
418 if (f == ret->succ)
419 continue;
421 if (!dominated_by_p (CDI_DOMINATORS, f->src,
422 ret->succ->dest))
423 break;
426 if (!f)
427 set_immediate_dominator (CDI_DOMINATORS, ret->succ->dest, ret);
431 return ret;
434 /* Creates a new basic block just after the basic block AFTER.
435 HEAD and END are the first and the last statement belonging
436 to the block. If both are NULL, an empty block is created. */
438 basic_block
439 create_basic_block (void *head, void *end, basic_block after)
441 basic_block ret;
443 if (!cfg_hooks->create_basic_block)
444 internal_error ("%s does not support create_basic_block.", cfg_hooks->name);
446 ret = cfg_hooks->create_basic_block (head, end, after);
448 if (dom_computed[CDI_DOMINATORS])
449 add_to_dominance_info (CDI_DOMINATORS, ret);
450 if (dom_computed[CDI_POST_DOMINATORS])
451 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
453 return ret;
456 /* Creates an empty basic block just after basic block AFTER. */
458 basic_block
459 create_empty_bb (basic_block after)
461 return create_basic_block (NULL, NULL, after);
464 /* Checks whether we may merge blocks BB1 and BB2. */
466 bool
467 can_merge_blocks_p (basic_block bb1, basic_block bb2)
469 bool ret;
471 if (!cfg_hooks->can_merge_blocks_p)
472 internal_error ("%s does not support can_merge_blocks_p.", cfg_hooks->name);
474 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
476 return ret;
479 void
480 predict_edge (edge e, enum br_predictor predictor, int probability)
482 if (!cfg_hooks->predict_edge)
483 internal_error ("%s does not support predict_edge.", cfg_hooks->name);
485 cfg_hooks->predict_edge (e, predictor, probability);
488 bool
489 predicted_by_p (basic_block bb, enum br_predictor predictor)
491 if (!cfg_hooks->predict_edge)
492 internal_error ("%s does not support predicted_by_p.", cfg_hooks->name);
494 return cfg_hooks->predicted_by_p (bb, predictor);
497 /* Merges basic block B into basic block A. */
499 void
500 merge_blocks (basic_block a, basic_block b)
502 edge e;
504 if (!cfg_hooks->merge_blocks)
505 internal_error ("%s does not support merge_blocks.", cfg_hooks->name);
507 cfg_hooks->merge_blocks (a, b);
509 /* Normally there should only be one successor of A and that is B, but
510 partway though the merge of blocks for conditional_execution we'll
511 be merging a TEST block with THEN and ELSE successors. Free the
512 whole lot of them and hope the caller knows what they're doing. */
513 while (a->succ)
514 remove_edge (a->succ);
516 /* Adjust the edges out of B for the new owner. */
517 for (e = b->succ; e; e = e->succ_next)
518 e->src = a;
519 a->succ = b->succ;
520 a->flags |= b->flags;
522 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
523 b->pred = b->succ = NULL;
524 a->global_live_at_end = b->global_live_at_end;
526 if (dom_computed[CDI_DOMINATORS])
527 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
529 if (dom_computed[CDI_DOMINATORS])
530 delete_from_dominance_info (CDI_DOMINATORS, b);
531 if (dom_computed[CDI_POST_DOMINATORS])
532 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
534 expunge_block (b);
537 /* Split BB into entry part and the rest (the rest is the newly created block).
538 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
539 part. Returns the edge connecting the entry part to the rest. */
541 edge
542 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
543 void (*new_bb_cbk) (basic_block))
545 edge e, next_e, fallthru;
546 basic_block dummy, jump;
548 if (!cfg_hooks->make_forwarder_block)
549 internal_error ("%s does not support make_forwarder_block.",
550 cfg_hooks->name);
552 fallthru = split_block_after_labels (bb);
553 dummy = fallthru->src;
554 bb = fallthru->dest;
556 /* Redirect back edges we want to keep. */
557 for (e = dummy->pred; e; e = next_e)
559 next_e = e->pred_next;
560 if (redirect_edge_p (e))
561 continue;
563 dummy->frequency -= EDGE_FREQUENCY (e);
564 dummy->count -= e->count;
565 if (dummy->frequency < 0)
566 dummy->frequency = 0;
567 if (dummy->count < 0)
568 dummy->count = 0;
569 fallthru->count -= e->count;
570 if (fallthru->count < 0)
571 fallthru->count = 0;
573 jump = redirect_edge_and_branch_force (e, bb);
574 if (jump)
575 new_bb_cbk (jump);
578 if (dom_computed[CDI_DOMINATORS] >= DOM_CONS_OK)
580 basic_block doms_to_fix[2];
582 doms_to_fix[0] = dummy;
583 doms_to_fix[1] = bb;
584 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, 2);
587 cfg_hooks->make_forwarder_block (fallthru);
589 return fallthru;
592 void
593 tidy_fallthru_edge (edge e)
595 if (cfg_hooks->tidy_fallthru_edge)
596 cfg_hooks->tidy_fallthru_edge (e);
599 /* Fix up edges that now fall through, or rather should now fall through
600 but previously required a jump around now deleted blocks. Simplify
601 the search by only examining blocks numerically adjacent, since this
602 is how find_basic_blocks created them. */
604 void
605 tidy_fallthru_edges (void)
607 basic_block b, c;
609 if (!cfg_hooks->tidy_fallthru_edge)
610 return;
612 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
613 return;
615 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, next_bb)
617 edge s;
619 c = b->next_bb;
621 /* We care about simple conditional or unconditional jumps with
622 a single successor.
624 If we had a conditional branch to the next instruction when
625 find_basic_blocks was called, then there will only be one
626 out edge for the block which ended with the conditional
627 branch (since we do not create duplicate edges).
629 Furthermore, the edge will be marked as a fallthru because we
630 merge the flags for the duplicate edges. So we do not want to
631 check that the edge is not a FALLTHRU edge. */
633 if ((s = b->succ) != NULL
634 && ! (s->flags & EDGE_COMPLEX)
635 && s->succ_next == NULL
636 && s->dest == c
637 && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
638 tidy_fallthru_edge (s);
642 /* Returns true if we can duplicate basic block BB. */
644 bool
645 can_duplicate_block_p (basic_block bb)
647 edge e;
649 if (!cfg_hooks->can_duplicate_block_p)
650 internal_error ("%s does not support can_duplicate_block_p.",
651 cfg_hooks->name);
653 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR)
654 return false;
656 /* Duplicating fallthru block to exit would require adding a jump
657 and splitting the real last BB. */
658 for (e = bb->succ; e; e = e->succ_next)
659 if (e->dest == EXIT_BLOCK_PTR && e->flags & EDGE_FALLTHRU)
660 return false;
662 return cfg_hooks->can_duplicate_block_p (bb);
665 /* Duplicates basic block BB and redirects edge E to it. Returns the
666 new basic block. */
668 basic_block
669 duplicate_block (basic_block bb, edge e)
671 edge s, n;
672 basic_block new_bb;
673 gcov_type new_count = e ? e->count : 0;
675 if (!cfg_hooks->duplicate_block)
676 internal_error ("%s does not support duplicate_block.",
677 cfg_hooks->name);
679 if (bb->count < new_count)
680 new_count = bb->count;
681 if (!bb->pred)
682 abort ();
683 #ifdef ENABLE_CHECKING
684 if (!can_duplicate_block_p (bb))
685 abort ();
686 #endif
688 new_bb = cfg_hooks->duplicate_block (bb);
690 new_bb->loop_depth = bb->loop_depth;
691 new_bb->flags = bb->flags;
692 for (s = bb->succ; s; s = s->succ_next)
694 /* Since we are creating edges from a new block to successors
695 of another block (which therefore are known to be disjoint), there
696 is no need to actually check for duplicated edges. */
697 n = unchecked_make_edge (new_bb, s->dest, s->flags);
698 n->probability = s->probability;
699 if (e && bb->count)
701 /* Take care for overflows! */
702 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
703 s->count -= n->count;
705 else
706 n->count = s->count;
707 n->aux = s->aux;
710 if (e)
712 new_bb->count = new_count;
713 bb->count -= new_count;
715 new_bb->frequency = EDGE_FREQUENCY (e);
716 bb->frequency -= EDGE_FREQUENCY (e);
718 redirect_edge_and_branch_force (e, new_bb);
720 if (bb->count < 0)
721 bb->count = 0;
722 if (bb->frequency < 0)
723 bb->frequency = 0;
725 else
727 new_bb->count = bb->count;
728 new_bb->frequency = bb->frequency;
731 new_bb->rbi->original = bb;
732 bb->rbi->copy = new_bb;
734 return new_bb;
737 /* Return 1 if BB ends with a call, possibly followed by some
738 instructions that must stay with the call, 0 otherwise. */
740 bool
741 block_ends_with_call_p (basic_block bb)
743 if (!cfg_hooks->block_ends_with_call_p)
744 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
746 return (cfg_hooks->block_ends_with_call_p) (bb);
749 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
751 bool
752 block_ends_with_condjump_p (basic_block bb)
754 if (!cfg_hooks->block_ends_with_condjump_p)
755 internal_error ("%s does not support block_ends_with_condjump_p",
756 cfg_hooks->name);
758 return (cfg_hooks->block_ends_with_condjump_p) (bb);
761 /* Add fake edges to the function exit for any non constant and non noreturn
762 calls, volatile inline assembly in the bitmap of blocks specified by
763 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
764 that were split.
766 The goal is to expose cases in which entering a basic block does not imply
767 that all subsequent instructions must be executed. */
770 flow_call_edges_add (sbitmap blocks)
772 if (!cfg_hooks->flow_call_edges_add)
773 internal_error ("%s does not support flow_call_edges_add",
774 cfg_hooks->name);
776 return (cfg_hooks->flow_call_edges_add) (blocks);