* config/arm/netbsd.h (TARGET_OS_CPP_BUILTINS): Use
[official-gcc.git] / gcc / cfg.c
blob3e8c9488f27153792f7eee5fb3cb3a5f9278c5d9
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains low level functions to manipulate the CFG and
23 analyze it. All other modules should not transform the datastructure
24 directly and use abstraction instead. The file is supposed to be
25 ordered bottom-up and should not contain any code dependent on a
26 particular intermediate language (RTL or trees).
28 Available functionality:
29 - Initialization/deallocation
30 init_flow, clear_edges
31 - Low level basic block manipulation
32 alloc_block, expunge_block
33 - Edge manipulation
34 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
35 - Low level edge redirection (without updating instruction chain)
36 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
37 - Dumping and debugging
38 dump_flow_info, debug_flow_info, dump_edge_info
39 - Allocation of AUX fields for basic blocks
40 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
41 - clear_bb_flags
44 #include "config.h"
45 #include "system.h"
46 #include "tree.h"
47 #include "rtl.h"
48 #include "hard-reg-set.h"
49 #include "basic-block.h"
50 #include "regs.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "function.h"
54 #include "except.h"
55 #include "toplev.h"
56 #include "tm_p.h"
57 #include "obstack.h"
59 /* The obstack on which the flow graph components are allocated. */
61 struct obstack flow_obstack;
62 static char *flow_firstobj;
64 /* Number of basic blocks in the current function. */
66 int n_basic_blocks;
68 /* First free basic block number. */
70 int last_basic_block;
72 /* Number of edges in the current function. */
74 int n_edges;
76 /* First edge in the deleted edges chain. */
78 edge first_deleted_edge;
79 static basic_block first_deleted_block;
81 /* The basic block array. */
83 varray_type basic_block_info;
85 /* The special entry and exit blocks. */
87 struct basic_block_def entry_exit_blocks[2]
88 = {{NULL, /* head */
89 NULL, /* end */
90 NULL, /* head_tree */
91 NULL, /* end_tree */
92 NULL, /* pred */
93 NULL, /* succ */
94 NULL, /* local_set */
95 NULL, /* cond_local_set */
96 NULL, /* global_live_at_start */
97 NULL, /* global_live_at_end */
98 NULL, /* aux */
99 ENTRY_BLOCK, /* index */
100 NULL, /* prev_bb */
101 EXIT_BLOCK_PTR, /* next_bb */
102 0, /* loop_depth */
103 0, /* count */
104 0, /* frequency */
105 0 /* flags */
108 NULL, /* head */
109 NULL, /* end */
110 NULL, /* head_tree */
111 NULL, /* end_tree */
112 NULL, /* pred */
113 NULL, /* succ */
114 NULL, /* local_set */
115 NULL, /* cond_local_set */
116 NULL, /* global_live_at_start */
117 NULL, /* global_live_at_end */
118 NULL, /* aux */
119 EXIT_BLOCK, /* index */
120 ENTRY_BLOCK_PTR, /* prev_bb */
121 NULL, /* next_bb */
122 0, /* loop_depth */
123 0, /* count */
124 0, /* frequency */
125 0 /* flags */
129 void debug_flow_info PARAMS ((void));
130 static void free_edge PARAMS ((edge));
132 /* Called once at initialization time. */
134 void
135 init_flow ()
137 static int initialized;
139 first_deleted_edge = 0;
140 first_deleted_block = 0;
141 n_edges = 0;
143 if (!initialized)
145 gcc_obstack_init (&flow_obstack);
146 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
147 initialized = 1;
149 else
151 obstack_free (&flow_obstack, flow_firstobj);
152 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
156 /* Helper function for remove_edge and clear_edges. Frees edge structure
157 without actually unlinking it from the pred/succ lists. */
159 static void
160 free_edge (e)
161 edge e;
163 n_edges--;
164 memset (e, 0, sizeof *e);
165 e->succ_next = first_deleted_edge;
166 first_deleted_edge = e;
169 /* Free the memory associated with the edge structures. */
171 void
172 clear_edges ()
174 basic_block bb;
175 edge e;
177 FOR_EACH_BB (bb)
179 edge e = bb->succ;
181 while (e)
183 edge next = e->succ_next;
185 free_edge (e);
186 e = next;
189 bb->succ = NULL;
190 bb->pred = NULL;
193 e = ENTRY_BLOCK_PTR->succ;
194 while (e)
196 edge next = e->succ_next;
198 free_edge (e);
199 e = next;
202 EXIT_BLOCK_PTR->pred = NULL;
203 ENTRY_BLOCK_PTR->succ = NULL;
205 if (n_edges)
206 abort ();
209 /* Allocate memory for basic_block. */
211 basic_block
212 alloc_block ()
214 basic_block bb;
216 if (first_deleted_block)
218 bb = first_deleted_block;
219 first_deleted_block = (basic_block) bb->succ;
220 bb->succ = NULL;
222 else
224 bb = (basic_block) obstack_alloc (&flow_obstack, sizeof *bb);
225 memset (bb, 0, sizeof *bb);
227 return bb;
230 /* Link block B to chain after AFTER. */
231 void
232 link_block (b, after)
233 basic_block b, after;
235 b->next_bb = after->next_bb;
236 b->prev_bb = after;
237 after->next_bb = b;
238 b->next_bb->prev_bb = b;
241 /* Unlink block B from chain. */
242 void
243 unlink_block (b)
244 basic_block b;
246 b->next_bb->prev_bb = b->prev_bb;
247 b->prev_bb->next_bb = b->next_bb;
250 /* Sequentially order blocks and compact the arrays. */
251 void
252 compact_blocks ()
254 int i;
255 basic_block bb;
257 i = 0;
258 FOR_EACH_BB (bb)
260 BASIC_BLOCK (i) = bb;
261 bb->index = i;
262 i++;
265 if (i != n_basic_blocks)
266 abort ();
268 last_basic_block = n_basic_blocks;
272 /* Remove block B from the basic block array. */
274 void
275 expunge_block (b)
276 basic_block b;
278 unlink_block (b);
279 BASIC_BLOCK (b->index) = NULL;
280 n_basic_blocks--;
282 /* Invalidate data to make bughunting easier. */
283 memset (b, 0, sizeof *b);
284 b->index = -3;
285 b->succ = (edge) first_deleted_block;
286 first_deleted_block = (basic_block) b;
289 /* Create an edge connecting SRC and DST with FLAGS optionally using
290 edge cache CACHE. Return the new edge, NULL if already exist. */
292 edge
293 cached_make_edge (edge_cache, src, dst, flags)
294 sbitmap *edge_cache;
295 basic_block src, dst;
296 int flags;
298 int use_edge_cache;
299 edge e;
301 /* Don't bother with edge cache for ENTRY or EXIT, if there aren't that
302 many edges to them, or we didn't allocate memory for it. */
303 use_edge_cache = (edge_cache
304 && src != ENTRY_BLOCK_PTR && dst != EXIT_BLOCK_PTR);
306 /* Make sure we don't add duplicate edges. */
307 switch (use_edge_cache)
309 default:
310 /* Quick test for non-existence of the edge. */
311 if (! TEST_BIT (edge_cache[src->index], dst->index))
312 break;
314 /* The edge exists; early exit if no work to do. */
315 if (flags == 0)
316 return NULL;
318 /* FALLTHRU */
319 case 0:
320 for (e = src->succ; e; e = e->succ_next)
321 if (e->dest == dst)
323 e->flags |= flags;
324 return NULL;
326 break;
329 if (first_deleted_edge)
331 e = first_deleted_edge;
332 first_deleted_edge = e->succ_next;
334 else
336 e = (edge) obstack_alloc (&flow_obstack, sizeof *e);
337 memset (e, 0, sizeof *e);
339 n_edges++;
341 e->succ_next = src->succ;
342 e->pred_next = dst->pred;
343 e->src = src;
344 e->dest = dst;
345 e->flags = flags;
347 src->succ = e;
348 dst->pred = e;
350 if (use_edge_cache)
351 SET_BIT (edge_cache[src->index], dst->index);
353 return e;
356 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
357 created edge or NULL if already exist. */
359 edge
360 make_edge (src, dest, flags)
361 basic_block src, dest;
362 int flags;
364 return cached_make_edge (NULL, src, dest, flags);
367 /* Create an edge connecting SRC to DEST and set probability by knowing
368 that it is the single edge leaving SRC. */
370 edge
371 make_single_succ_edge (src, dest, flags)
372 basic_block src, dest;
373 int flags;
375 edge e = make_edge (src, dest, flags);
377 e->probability = REG_BR_PROB_BASE;
378 e->count = src->count;
379 return e;
382 /* This function will remove an edge from the flow graph. */
384 void
385 remove_edge (e)
386 edge e;
388 edge last_pred = NULL;
389 edge last_succ = NULL;
390 edge tmp;
391 basic_block src, dest;
393 src = e->src;
394 dest = e->dest;
395 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
396 last_succ = tmp;
398 if (!tmp)
399 abort ();
400 if (last_succ)
401 last_succ->succ_next = e->succ_next;
402 else
403 src->succ = e->succ_next;
405 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
406 last_pred = tmp;
408 if (!tmp)
409 abort ();
410 if (last_pred)
411 last_pred->pred_next = e->pred_next;
412 else
413 dest->pred = e->pred_next;
415 free_edge (e);
418 /* Redirect an edge's successor from one block to another. */
420 void
421 redirect_edge_succ (e, new_succ)
422 edge e;
423 basic_block new_succ;
425 edge *pe;
427 /* Disconnect the edge from the old successor block. */
428 for (pe = &e->dest->pred; *pe != e; pe = &(*pe)->pred_next)
429 continue;
430 *pe = (*pe)->pred_next;
432 /* Reconnect the edge to the new successor block. */
433 e->pred_next = new_succ->pred;
434 new_succ->pred = e;
435 e->dest = new_succ;
438 /* Like previous but avoid possible duplicate edge. */
440 edge
441 redirect_edge_succ_nodup (e, new_succ)
442 edge e;
443 basic_block new_succ;
445 edge s;
447 /* Check whether the edge is already present. */
448 for (s = e->src->succ; s; s = s->succ_next)
449 if (s->dest == new_succ && s != e)
450 break;
452 if (s)
454 s->flags |= e->flags;
455 s->probability += e->probability;
456 s->count += e->count;
457 remove_edge (e);
458 e = s;
460 else
461 redirect_edge_succ (e, new_succ);
463 return e;
466 /* Redirect an edge's predecessor from one block to another. */
468 void
469 redirect_edge_pred (e, new_pred)
470 edge e;
471 basic_block new_pred;
473 edge *pe;
475 /* Disconnect the edge from the old predecessor block. */
476 for (pe = &e->src->succ; *pe != e; pe = &(*pe)->succ_next)
477 continue;
479 *pe = (*pe)->succ_next;
481 /* Reconnect the edge to the new predecessor block. */
482 e->succ_next = new_pred->succ;
483 new_pred->succ = e;
484 e->src = new_pred;
487 void
488 clear_bb_flags ()
490 basic_block bb;
492 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
493 bb->flags = 0;
496 void
497 dump_flow_info (file)
498 FILE *file;
500 int i;
501 basic_block bb;
502 static const char * const reg_class_names[] = REG_CLASS_NAMES;
504 fprintf (file, "%d registers.\n", max_regno);
505 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
506 if (REG_N_REFS (i))
508 enum reg_class class, altclass;
510 fprintf (file, "\nRegister %d used %d times across %d insns",
511 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
512 if (REG_BASIC_BLOCK (i) >= 0)
513 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
514 if (REG_N_SETS (i))
515 fprintf (file, "; set %d time%s", REG_N_SETS (i),
516 (REG_N_SETS (i) == 1) ? "" : "s");
517 if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
518 fprintf (file, "; user var");
519 if (REG_N_DEATHS (i) != 1)
520 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
521 if (REG_N_CALLS_CROSSED (i) == 1)
522 fprintf (file, "; crosses 1 call");
523 else if (REG_N_CALLS_CROSSED (i))
524 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
525 if (regno_reg_rtx[i] != NULL
526 && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
527 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
529 class = reg_preferred_class (i);
530 altclass = reg_alternate_class (i);
531 if (class != GENERAL_REGS || altclass != ALL_REGS)
533 if (altclass == ALL_REGS || class == ALL_REGS)
534 fprintf (file, "; pref %s", reg_class_names[(int) class]);
535 else if (altclass == NO_REGS)
536 fprintf (file, "; %s or none", reg_class_names[(int) class]);
537 else
538 fprintf (file, "; pref %s, else %s",
539 reg_class_names[(int) class],
540 reg_class_names[(int) altclass]);
543 if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
544 fprintf (file, "; pointer");
545 fprintf (file, ".\n");
548 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
549 FOR_EACH_BB (bb)
551 edge e;
552 int sum;
553 gcov_type lsum;
555 fprintf (file, "\nBasic block %d: first insn %d, last %d, ",
556 bb->index, INSN_UID (bb->head), INSN_UID (bb->end));
557 fprintf (file, "prev %d, next %d, ",
558 bb->prev_bb->index, bb->next_bb->index);
559 fprintf (file, "loop_depth %d, count ", bb->loop_depth);
560 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
561 fprintf (file, ", freq %i", bb->frequency);
562 if (maybe_hot_bb_p (bb))
563 fprintf (file, ", maybe hot");
564 if (probably_never_executed_bb_p (bb))
565 fprintf (file, ", probably never executed");
566 fprintf (file, ".\n");
568 fprintf (file, "Predecessors: ");
569 for (e = bb->pred; e; e = e->pred_next)
570 dump_edge_info (file, e, 0);
572 fprintf (file, "\nSuccessors: ");
573 for (e = bb->succ; e; e = e->succ_next)
574 dump_edge_info (file, e, 1);
576 fprintf (file, "\nRegisters live at start:");
577 dump_regset (bb->global_live_at_start, file);
579 fprintf (file, "\nRegisters live at end:");
580 dump_regset (bb->global_live_at_end, file);
582 putc ('\n', file);
584 /* Check the consistency of profile information. We can't do that
585 in verify_flow_info, as the counts may get invalid for incompletely
586 solved graphs, later elliminating of conditionals or roundoff errors.
587 It is still practical to have them reported for debugging of simple
588 testcases. */
589 sum = 0;
590 for (e = bb->succ; e; e = e->succ_next)
591 sum += e->probability;
592 if (bb->succ && abs (sum - REG_BR_PROB_BASE) > 100)
593 fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
594 sum * 100.0 / REG_BR_PROB_BASE);
595 sum = 0;
596 for (e = bb->pred; e; e = e->pred_next)
597 sum += EDGE_FREQUENCY (e);
598 if (abs (sum - bb->frequency) > 100)
599 fprintf (file,
600 "Invalid sum of incomming frequencies %i, should be %i\n",
601 sum, bb->frequency);
602 lsum = 0;
603 for (e = bb->pred; e; e = e->pred_next)
604 lsum += e->count;
605 if (lsum - bb->count > 100 || lsum - bb->count < -100)
606 fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
607 (int)lsum, (int)bb->count);
608 lsum = 0;
609 for (e = bb->succ; e; e = e->succ_next)
610 lsum += e->count;
611 if (bb->succ && (lsum - bb->count > 100 || lsum - bb->count < -100))
612 fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
613 (int)lsum, (int)bb->count);
616 putc ('\n', file);
619 void
620 debug_flow_info ()
622 dump_flow_info (stderr);
625 void
626 dump_edge_info (file, e, do_succ)
627 FILE *file;
628 edge e;
629 int do_succ;
631 basic_block side = (do_succ ? e->dest : e->src);
633 if (side == ENTRY_BLOCK_PTR)
634 fputs (" ENTRY", file);
635 else if (side == EXIT_BLOCK_PTR)
636 fputs (" EXIT", file);
637 else
638 fprintf (file, " %d", side->index);
640 if (e->probability)
641 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
643 if (e->count)
645 fprintf (file, " count:");
646 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
649 if (e->flags)
651 static const char * const bitnames[]
652 = {"fallthru", "ab", "abcall", "eh", "fake", "dfs_back", "can_fallthru"};
653 int comma = 0;
654 int i, flags = e->flags;
656 fputs (" (", file);
657 for (i = 0; flags; i++)
658 if (flags & (1 << i))
660 flags &= ~(1 << i);
662 if (comma)
663 fputc (',', file);
664 if (i < (int) ARRAY_SIZE (bitnames))
665 fputs (bitnames[i], file);
666 else
667 fprintf (file, "%d", i);
668 comma = 1;
671 fputc (')', file);
675 /* Simple routines to easily allocate AUX fields of basic blocks. */
677 static struct obstack block_aux_obstack;
678 static void *first_block_aux_obj = 0;
679 static struct obstack edge_aux_obstack;
680 static void *first_edge_aux_obj = 0;
682 /* Allocate an memory block of SIZE as BB->aux. The obstack must
683 be first initialized by alloc_aux_for_blocks. */
685 inline void
686 alloc_aux_for_block (bb, size)
687 basic_block bb;
688 int size;
690 /* Verify that aux field is clear. */
691 if (bb->aux || !first_block_aux_obj)
692 abort ();
693 bb->aux = obstack_alloc (&block_aux_obstack, size);
694 memset (bb->aux, 0, size);
697 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
698 alloc_aux_for_block for each basic block. */
700 void
701 alloc_aux_for_blocks (size)
702 int size;
704 static int initialized;
706 if (!initialized)
708 gcc_obstack_init (&block_aux_obstack);
709 initialized = 1;
712 /* Check whether AUX data are still allocated. */
713 else if (first_block_aux_obj)
714 abort ();
715 first_block_aux_obj = (char *) obstack_alloc (&block_aux_obstack, 0);
716 if (size)
718 basic_block bb;
720 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
721 alloc_aux_for_block (bb, size);
725 /* Clear AUX pointers of all blocks. */
727 void
728 clear_aux_for_blocks ()
730 basic_block bb;
732 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
733 bb->aux = NULL;
736 /* Free data allocated in block_aux_obstack and clear AUX pointers
737 of all blocks. */
739 void
740 free_aux_for_blocks ()
742 if (!first_block_aux_obj)
743 abort ();
744 obstack_free (&block_aux_obstack, first_block_aux_obj);
745 first_block_aux_obj = NULL;
747 clear_aux_for_blocks ();
750 /* Allocate an memory edge of SIZE as BB->aux. The obstack must
751 be first initialized by alloc_aux_for_edges. */
753 inline void
754 alloc_aux_for_edge (e, size)
755 edge e;
756 int size;
758 /* Verify that aux field is clear. */
759 if (e->aux || !first_edge_aux_obj)
760 abort ();
761 e->aux = obstack_alloc (&edge_aux_obstack, size);
762 memset (e->aux, 0, size);
765 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
766 alloc_aux_for_edge for each basic edge. */
768 void
769 alloc_aux_for_edges (size)
770 int size;
772 static int initialized;
774 if (!initialized)
776 gcc_obstack_init (&edge_aux_obstack);
777 initialized = 1;
780 /* Check whether AUX data are still allocated. */
781 else if (first_edge_aux_obj)
782 abort ();
784 first_edge_aux_obj = (char *) obstack_alloc (&edge_aux_obstack, 0);
785 if (size)
787 basic_block bb;
789 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
791 edge e;
793 for (e = bb->succ; e; e = e->succ_next)
794 alloc_aux_for_edge (e, size);
799 /* Clear AUX pointers of all edges. */
801 void
802 clear_aux_for_edges ()
804 basic_block bb;
805 edge e;
807 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
809 for (e = bb->succ; e; e = e->succ_next)
810 e->aux = NULL;
814 /* Free data allocated in edge_aux_obstack and clear AUX pointers
815 of all edges. */
817 void
818 free_aux_for_edges ()
820 if (!first_edge_aux_obj)
821 abort ();
822 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
823 first_edge_aux_obj = NULL;
825 clear_aux_for_edges ();