2002-05-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / cfg.c
blob766c1b8ff3d7569655f325df80fd8e2e6b13ac11
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 /* Number of edges in the current function. */
70 int n_edges;
72 /* First edge in the deleted edges chain. */
74 edge first_deleted_edge;
75 static basic_block first_deleted_block;
77 /* The basic block array. */
79 varray_type basic_block_info;
81 /* The special entry and exit blocks. */
83 struct basic_block_def entry_exit_blocks[2]
84 = {{NULL, /* head */
85 NULL, /* end */
86 NULL, /* head_tree */
87 NULL, /* end_tree */
88 NULL, /* pred */
89 NULL, /* succ */
90 NULL, /* local_set */
91 NULL, /* cond_local_set */
92 NULL, /* global_live_at_start */
93 NULL, /* global_live_at_end */
94 NULL, /* aux */
95 ENTRY_BLOCK, /* index */
96 0, /* loop_depth */
97 0, /* count */
98 0, /* frequency */
99 0 /* flags */
102 NULL, /* head */
103 NULL, /* end */
104 NULL, /* head_tree */
105 NULL, /* end_tree */
106 NULL, /* pred */
107 NULL, /* succ */
108 NULL, /* local_set */
109 NULL, /* cond_local_set */
110 NULL, /* global_live_at_start */
111 NULL, /* global_live_at_end */
112 NULL, /* aux */
113 EXIT_BLOCK, /* index */
114 0, /* loop_depth */
115 0, /* count */
116 0, /* frequency */
117 0 /* flags */
121 void debug_flow_info PARAMS ((void));
122 static void free_edge PARAMS ((edge));
124 /* Called once at initialization time. */
126 void
127 init_flow ()
129 static int initialized;
131 first_deleted_edge = 0;
132 first_deleted_block = 0;
133 n_edges = 0;
135 if (!initialized)
137 gcc_obstack_init (&flow_obstack);
138 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
139 initialized = 1;
141 else
143 obstack_free (&flow_obstack, flow_firstobj);
144 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
148 /* Helper function for remove_edge and clear_edges. Frees edge structure
149 without actually unlinking it from the pred/succ lists. */
151 static void
152 free_edge (e)
153 edge e;
155 n_edges--;
156 memset (e, 0, sizeof *e);
157 e->succ_next = first_deleted_edge;
158 first_deleted_edge = e;
161 /* Free the memory associated with the edge structures. */
163 void
164 clear_edges ()
166 int i;
167 edge e;
169 for (i = 0; i < n_basic_blocks; ++i)
171 basic_block bb = BASIC_BLOCK (i);
172 edge e = bb->succ;
174 while (e)
176 edge next = e->succ_next;
178 free_edge (e);
179 e = next;
182 bb->succ = NULL;
183 bb->pred = NULL;
186 e = ENTRY_BLOCK_PTR->succ;
187 while (e)
189 edge next = e->succ_next;
191 free_edge (e);
192 e = next;
195 EXIT_BLOCK_PTR->pred = NULL;
196 ENTRY_BLOCK_PTR->succ = NULL;
198 if (n_edges)
199 abort ();
202 /* Allocate memory for basic_block. */
204 basic_block
205 alloc_block ()
207 basic_block bb;
209 if (first_deleted_block)
211 bb = first_deleted_block;
212 first_deleted_block = (basic_block) bb->succ;
213 bb->succ = NULL;
215 else
217 bb = (basic_block) obstack_alloc (&flow_obstack, sizeof *bb);
218 memset (bb, 0, sizeof *bb);
220 return bb;
223 /* Remove block B from the basic block array and compact behind it. */
225 void
226 expunge_block_nocompact (b)
227 basic_block b;
229 /* Invalidate data to make bughunting easier. */
230 memset (b, 0, sizeof *b);
231 b->index = -3;
232 b->succ = (edge) first_deleted_block;
233 first_deleted_block = (basic_block) b;
236 void
237 expunge_block (b)
238 basic_block b;
240 int i, n = n_basic_blocks;
242 for (i = b->index; i + 1 < n; ++i)
244 basic_block x = BASIC_BLOCK (i + 1);
245 BASIC_BLOCK (i) = x;
246 x->index = i;
249 n_basic_blocks--;
250 basic_block_info->num_elements--;
252 expunge_block_nocompact (b);
255 /* Create an edge connecting SRC and DST with FLAGS optionally using
256 edge cache CACHE. Return the new edge, NULL if already exist. */
258 edge
259 cached_make_edge (edge_cache, src, dst, flags)
260 sbitmap *edge_cache;
261 basic_block src, dst;
262 int flags;
264 int use_edge_cache;
265 edge e;
267 /* Don't bother with edge cache for ENTRY or EXIT, if there aren't that
268 many edges to them, or we didn't allocate memory for it. */
269 use_edge_cache = (edge_cache
270 && src != ENTRY_BLOCK_PTR && dst != EXIT_BLOCK_PTR);
272 /* Make sure we don't add duplicate edges. */
273 switch (use_edge_cache)
275 default:
276 /* Quick test for non-existence of the edge. */
277 if (! TEST_BIT (edge_cache[src->index], dst->index))
278 break;
280 /* The edge exists; early exit if no work to do. */
281 if (flags == 0)
282 return NULL;
284 /* FALLTHRU */
285 case 0:
286 for (e = src->succ; e; e = e->succ_next)
287 if (e->dest == dst)
289 e->flags |= flags;
290 return NULL;
292 break;
295 if (first_deleted_edge)
297 e = first_deleted_edge;
298 first_deleted_edge = e->succ_next;
300 else
302 e = (edge) obstack_alloc (&flow_obstack, sizeof *e);
303 memset (e, 0, sizeof *e);
305 n_edges++;
307 e->succ_next = src->succ;
308 e->pred_next = dst->pred;
309 e->src = src;
310 e->dest = dst;
311 e->flags = flags;
313 src->succ = e;
314 dst->pred = e;
316 if (use_edge_cache)
317 SET_BIT (edge_cache[src->index], dst->index);
319 return e;
322 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
323 created edge or NULL if already exist. */
325 edge
326 make_edge (src, dest, flags)
327 basic_block src, dest;
328 int flags;
330 return cached_make_edge (NULL, src, dest, flags);
333 /* Create an edge connecting SRC to DEST and set probability by knowing
334 that it is the single edge leaving SRC. */
336 edge
337 make_single_succ_edge (src, dest, flags)
338 basic_block src, dest;
339 int flags;
341 edge e = make_edge (src, dest, flags);
343 e->probability = REG_BR_PROB_BASE;
344 e->count = src->count;
345 return e;
348 /* This function will remove an edge from the flow graph. */
350 void
351 remove_edge (e)
352 edge e;
354 edge last_pred = NULL;
355 edge last_succ = NULL;
356 edge tmp;
357 basic_block src, dest;
359 src = e->src;
360 dest = e->dest;
361 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
362 last_succ = tmp;
364 if (!tmp)
365 abort ();
366 if (last_succ)
367 last_succ->succ_next = e->succ_next;
368 else
369 src->succ = e->succ_next;
371 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
372 last_pred = tmp;
374 if (!tmp)
375 abort ();
376 if (last_pred)
377 last_pred->pred_next = e->pred_next;
378 else
379 dest->pred = e->pred_next;
381 free_edge (e);
384 /* Redirect an edge's successor from one block to another. */
386 void
387 redirect_edge_succ (e, new_succ)
388 edge e;
389 basic_block new_succ;
391 edge *pe;
393 /* Disconnect the edge from the old successor block. */
394 for (pe = &e->dest->pred; *pe != e; pe = &(*pe)->pred_next)
395 continue;
396 *pe = (*pe)->pred_next;
398 /* Reconnect the edge to the new successor block. */
399 e->pred_next = new_succ->pred;
400 new_succ->pred = e;
401 e->dest = new_succ;
404 /* Like previous but avoid possible duplicate edge. */
406 edge
407 redirect_edge_succ_nodup (e, new_succ)
408 edge e;
409 basic_block new_succ;
411 edge s;
413 /* Check whether the edge is already present. */
414 for (s = e->src->succ; s; s = s->succ_next)
415 if (s->dest == new_succ && s != e)
416 break;
418 if (s)
420 s->flags |= e->flags;
421 s->probability += e->probability;
422 s->count += e->count;
423 remove_edge (e);
424 e = s;
426 else
427 redirect_edge_succ (e, new_succ);
429 return e;
432 /* Redirect an edge's predecessor from one block to another. */
434 void
435 redirect_edge_pred (e, new_pred)
436 edge e;
437 basic_block new_pred;
439 edge *pe;
441 /* Disconnect the edge from the old predecessor block. */
442 for (pe = &e->src->succ; *pe != e; pe = &(*pe)->succ_next)
443 continue;
445 *pe = (*pe)->succ_next;
447 /* Reconnect the edge to the new predecessor block. */
448 e->succ_next = new_pred->succ;
449 new_pred->succ = e;
450 e->src = new_pred;
453 void
454 clear_bb_flags ()
456 int i;
457 ENTRY_BLOCK_PTR->flags = 0;
458 EXIT_BLOCK_PTR->flags = 0;
459 for (i = 0; i < n_basic_blocks; i++)
460 BASIC_BLOCK (i)->flags = 0;
463 void
464 dump_flow_info (file)
465 FILE *file;
467 int i;
468 static const char * const reg_class_names[] = REG_CLASS_NAMES;
470 fprintf (file, "%d registers.\n", max_regno);
471 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
472 if (REG_N_REFS (i))
474 enum reg_class class, altclass;
476 fprintf (file, "\nRegister %d used %d times across %d insns",
477 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
478 if (REG_BASIC_BLOCK (i) >= 0)
479 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
480 if (REG_N_SETS (i))
481 fprintf (file, "; set %d time%s", REG_N_SETS (i),
482 (REG_N_SETS (i) == 1) ? "" : "s");
483 if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
484 fprintf (file, "; user var");
485 if (REG_N_DEATHS (i) != 1)
486 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
487 if (REG_N_CALLS_CROSSED (i) == 1)
488 fprintf (file, "; crosses 1 call");
489 else if (REG_N_CALLS_CROSSED (i))
490 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
491 if (regno_reg_rtx[i] != NULL
492 && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
493 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
495 class = reg_preferred_class (i);
496 altclass = reg_alternate_class (i);
497 if (class != GENERAL_REGS || altclass != ALL_REGS)
499 if (altclass == ALL_REGS || class == ALL_REGS)
500 fprintf (file, "; pref %s", reg_class_names[(int) class]);
501 else if (altclass == NO_REGS)
502 fprintf (file, "; %s or none", reg_class_names[(int) class]);
503 else
504 fprintf (file, "; pref %s, else %s",
505 reg_class_names[(int) class],
506 reg_class_names[(int) altclass]);
509 if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
510 fprintf (file, "; pointer");
511 fprintf (file, ".\n");
514 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
515 for (i = 0; i < n_basic_blocks; i++)
517 basic_block bb = BASIC_BLOCK (i);
518 edge e;
519 int sum;
520 gcov_type lsum;
522 fprintf (file, "\nBasic block %d: first insn %d, last %d, ",
523 i, INSN_UID (bb->head), INSN_UID (bb->end));
524 fprintf (file, "loop_depth %d, count ", bb->loop_depth);
525 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
526 fprintf (file, ", freq %i.\n", bb->frequency);
528 fprintf (file, "Predecessors: ");
529 for (e = bb->pred; e; e = e->pred_next)
530 dump_edge_info (file, e, 0);
532 fprintf (file, "\nSuccessors: ");
533 for (e = bb->succ; e; e = e->succ_next)
534 dump_edge_info (file, e, 1);
536 fprintf (file, "\nRegisters live at start:");
537 dump_regset (bb->global_live_at_start, file);
539 fprintf (file, "\nRegisters live at end:");
540 dump_regset (bb->global_live_at_end, file);
542 putc ('\n', file);
544 /* Check the consistency of profile information. We can't do that
545 in verify_flow_info, as the counts may get invalid for incompletely
546 solved graphs, later elliminating of conditionals or roundoff errors.
547 It is still practical to have them reported for debugging of simple
548 testcases. */
549 sum = 0;
550 for (e = bb->succ; e; e = e->succ_next)
551 sum += e->probability;
552 if (bb->succ && abs (sum - REG_BR_PROB_BASE) > 100)
553 fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
554 sum * 100.0 / REG_BR_PROB_BASE);
555 sum = 0;
556 for (e = bb->pred; e; e = e->pred_next)
557 sum += EDGE_FREQUENCY (e);
558 if (abs (sum - bb->frequency) > 100)
559 fprintf (file,
560 "Invalid sum of incomming frequencies %i, should be %i\n",
561 sum, bb->frequency);
562 lsum = 0;
563 for (e = bb->pred; e; e = e->pred_next)
564 lsum += e->count;
565 if (lsum - bb->count > 100 || lsum - bb->count < -100)
566 fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
567 (int)lsum, (int)bb->count);
568 lsum = 0;
569 for (e = bb->succ; e; e = e->succ_next)
570 lsum += e->count;
571 if (bb->succ && (lsum - bb->count > 100 || lsum - bb->count < -100))
572 fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
573 (int)lsum, (int)bb->count);
576 putc ('\n', file);
579 void
580 debug_flow_info ()
582 dump_flow_info (stderr);
585 void
586 dump_edge_info (file, e, do_succ)
587 FILE *file;
588 edge e;
589 int do_succ;
591 basic_block side = (do_succ ? e->dest : e->src);
593 if (side == ENTRY_BLOCK_PTR)
594 fputs (" ENTRY", file);
595 else if (side == EXIT_BLOCK_PTR)
596 fputs (" EXIT", file);
597 else
598 fprintf (file, " %d", side->index);
600 if (e->probability)
601 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
603 if (e->count)
605 fprintf (file, " count:");
606 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
609 if (e->flags)
611 static const char * const bitnames[]
612 = {"fallthru", "ab", "abcall", "eh", "fake", "dfs_back"};
613 int comma = 0;
614 int i, flags = e->flags;
616 fputs (" (", file);
617 for (i = 0; flags; i++)
618 if (flags & (1 << i))
620 flags &= ~(1 << i);
622 if (comma)
623 fputc (',', file);
624 if (i < (int) ARRAY_SIZE (bitnames))
625 fputs (bitnames[i], file);
626 else
627 fprintf (file, "%d", i);
628 comma = 1;
631 fputc (')', file);
635 /* Simple routines to easily allocate AUX fields of basic blocks. */
637 static struct obstack block_aux_obstack;
638 static void *first_block_aux_obj = 0;
639 static struct obstack edge_aux_obstack;
640 static void *first_edge_aux_obj = 0;
642 /* Allocate an memory block of SIZE as BB->aux. The obstack must
643 be first initialized by alloc_aux_for_blocks. */
645 inline void
646 alloc_aux_for_block (bb, size)
647 basic_block bb;
648 int size;
650 /* Verify that aux field is clear. */
651 if (bb->aux || !first_block_aux_obj)
652 abort ();
653 bb->aux = obstack_alloc (&block_aux_obstack, size);
654 memset (bb->aux, 0, size);
657 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
658 alloc_aux_for_block for each basic block. */
660 void
661 alloc_aux_for_blocks (size)
662 int size;
664 static int initialized;
666 if (!initialized)
668 gcc_obstack_init (&block_aux_obstack);
669 initialized = 1;
672 /* Check whether AUX data are still allocated. */
673 else if (first_block_aux_obj)
674 abort ();
675 first_block_aux_obj = (char *) obstack_alloc (&block_aux_obstack, 0);
676 if (size)
678 int i;
680 for (i = 0; i < n_basic_blocks; i++)
681 alloc_aux_for_block (BASIC_BLOCK (i), size);
683 alloc_aux_for_block (ENTRY_BLOCK_PTR, size);
684 alloc_aux_for_block (EXIT_BLOCK_PTR, size);
688 /* Clear AUX pointers of all blocks. */
690 void
691 clear_aux_for_blocks ()
693 int i;
695 for (i = 0; i < n_basic_blocks; i++)
696 BASIC_BLOCK (i)->aux = NULL;
698 ENTRY_BLOCK_PTR->aux = NULL;
699 EXIT_BLOCK_PTR->aux = NULL;
702 /* Free data allocated in block_aux_obstack and clear AUX pointers
703 of all blocks. */
705 void
706 free_aux_for_blocks ()
708 if (!first_block_aux_obj)
709 abort ();
710 obstack_free (&block_aux_obstack, first_block_aux_obj);
711 first_block_aux_obj = NULL;
713 clear_aux_for_blocks ();
716 /* Allocate an memory edge of SIZE as BB->aux. The obstack must
717 be first initialized by alloc_aux_for_edges. */
719 inline void
720 alloc_aux_for_edge (e, size)
721 edge e;
722 int size;
724 /* Verify that aux field is clear. */
725 if (e->aux || !first_edge_aux_obj)
726 abort ();
727 e->aux = obstack_alloc (&edge_aux_obstack, size);
728 memset (e->aux, 0, size);
731 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
732 alloc_aux_for_edge for each basic edge. */
734 void
735 alloc_aux_for_edges (size)
736 int size;
738 static int initialized;
740 if (!initialized)
742 gcc_obstack_init (&edge_aux_obstack);
743 initialized = 1;
746 /* Check whether AUX data are still allocated. */
747 else if (first_edge_aux_obj)
748 abort ();
750 first_edge_aux_obj = (char *) obstack_alloc (&edge_aux_obstack, 0);
751 if (size)
753 int i;
754 for (i = -1; i < n_basic_blocks; i++)
756 basic_block bb;
757 edge e;
759 if (i >= 0)
760 bb = BASIC_BLOCK (i);
761 else
762 bb = ENTRY_BLOCK_PTR;
764 for (e = bb->succ; e; e = e->succ_next)
765 alloc_aux_for_edge (e, size);
770 /* Clear AUX pointers of all edges. */
772 void
773 clear_aux_for_edges ()
775 int i;
777 for (i = -1; i < n_basic_blocks; i++)
779 basic_block bb;
780 edge e;
782 if (i >= 0)
783 bb = BASIC_BLOCK (i);
784 else
785 bb = ENTRY_BLOCK_PTR;
787 for (e = bb->succ; e; e = e->succ_next)
788 e->aux = NULL;
792 /* Free data allocated in edge_aux_obstack and clear AUX pointers
793 of all edges. */
795 void
796 free_aux_for_edges ()
798 if (!first_edge_aux_obj)
799 abort ();
800 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
801 first_edge_aux_obj = NULL;
803 clear_aux_for_edges ();