* ggc-common.c (ggc_print_statistics): Make arguments to fprintf
[official-gcc.git] / gcc / ggc-common.c
blobab8c3a1c196ebe953c075d3a6f2059731cca1a67
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
24 #include "config.h"
25 #include "system.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "hash.h"
30 #include "varray.h"
31 #include "ggc.h"
33 /* Statistics about the allocation. */
34 static ggc_statistics *ggc_stats;
36 static void ggc_mark_rtx_ptr PARAMS ((void *));
37 static void ggc_mark_tree_ptr PARAMS ((void *));
38 static void ggc_mark_tree_varray_ptr PARAMS ((void *));
39 static void ggc_mark_tree_hash_table_ptr PARAMS ((void *));
40 static void ggc_mark_string_ptr PARAMS ((void *));
41 static boolean ggc_mark_tree_hash_table_entry PARAMS ((struct hash_entry *,
42 hash_table_key));
44 /* Maintain global roots that are preserved during GC. */
46 /* Global roots that are preserved during calls to gc. */
48 struct ggc_root
50 struct ggc_root *next;
51 void *base;
52 int nelt;
53 int size;
54 void (*cb) PROTO ((void *));
57 static struct ggc_root *roots;
59 /* Type-correct function to pass to ggc_add_root. It just forwards
60 *ELT (which is an rtx) to ggc_mark_tree_varray. */
62 static void
63 ggc_mark_rtx_ptr (elt)
64 void *elt;
66 ggc_mark_rtx (*(rtx *)elt);
69 /* Type-correct function to pass to ggc_add_root. It just forwards
70 *ELT (which is a tree) to ggc_mark_tree. */
72 static void
73 ggc_mark_tree_ptr (elt)
74 void *elt;
76 ggc_mark_tree (*(tree *)elt);
79 /* Type-correct function to pass to ggc_add_root. It just forwards
80 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
82 static void
83 ggc_mark_tree_varray_ptr (elt)
84 void *elt;
86 ggc_mark_tree_varray (*(varray_type *)elt);
89 /* Type-correct function to pass to ggc_add_root. It just forwards
90 ELT (which is really a struct hash_table **) to
91 ggc_mark_tree_hash_table. */
93 static void
94 ggc_mark_tree_hash_table_ptr (elt)
95 void *elt;
97 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
100 static void
101 ggc_mark_string_ptr (elt)
102 void *elt;
104 ggc_mark_string (*(char **)elt);
107 void
108 ggc_add_root (base, nelt, size, cb)
109 void *base;
110 int nelt, size;
111 void (*cb) PROTO ((void *));
113 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
115 x->next = roots;
116 x->base = base;
117 x->nelt = nelt;
118 x->size = size;
119 x->cb = cb;
121 roots = x;
124 void
125 ggc_add_rtx_root (base, nelt)
126 rtx *base;
127 int nelt;
129 ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
132 void
133 ggc_add_tree_root (base, nelt)
134 tree *base;
135 int nelt;
137 ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
140 /* Add V (a varray full of trees) to the list of GC roots. */
142 void
143 ggc_add_tree_varray_root (base, nelt)
144 varray_type *base;
145 int nelt;
147 ggc_add_root (base, nelt, sizeof (varray_type),
148 ggc_mark_tree_varray_ptr);
151 /* Add HT (a hash-table where ever key is a tree) to the list of GC
152 roots. */
154 void
155 ggc_add_tree_hash_table_root (base, nelt)
156 struct hash_table **base;
157 int nelt;
159 ggc_add_root (base, nelt, sizeof (struct hash_table *),
160 ggc_mark_tree_hash_table_ptr);
163 void
164 ggc_add_string_root (base, nelt)
165 char **base;
166 int nelt;
168 ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
172 void
173 ggc_del_root (base)
174 void *base;
176 struct ggc_root *x, **p;
178 p = &roots, x = roots;
179 while (x)
181 if (x->base == base)
183 *p = x->next;
184 free (x);
185 return;
187 p = &x->next;
188 x = x->next;
191 abort();
194 void
195 ggc_mark_roots ()
197 struct ggc_root* x;
199 for (x = roots; x != NULL; x = x->next)
201 char *elt = x->base;
202 int s = x->size, n = x->nelt;
203 void (*cb) PROTO ((void *)) = x->cb;
204 int i;
206 for (i = 0; i < n; ++i, elt += s)
207 (*cb)(elt);
211 void
212 ggc_mark_rtx_children (r)
213 rtx r;
215 const char *fmt;
216 int i;
217 enum rtx_code code = GET_CODE (r);
219 /* Collect statistics, if appropriate. */
220 if (ggc_stats)
222 ++ggc_stats->num_rtxs[(int) code];
223 ggc_stats->size_rtxs[(int) code] += ggc_get_size (r);
226 /* ??? If (some of) these are really pass-dependant info, do we have
227 any right poking our noses in? */
228 switch (code)
230 case JUMP_INSN:
231 ggc_mark_rtx (JUMP_LABEL (r));
232 break;
233 case CODE_LABEL:
234 ggc_mark_rtx (LABEL_REFS (r));
235 break;
236 case LABEL_REF:
237 ggc_mark_rtx (LABEL_NEXTREF (r));
238 ggc_mark_rtx (CONTAINING_INSN (r));
239 break;
240 case ADDRESSOF:
241 ggc_mark_tree (ADDRESSOF_DECL (r));
242 break;
243 case CONST_DOUBLE:
244 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
245 break;
246 case NOTE:
247 switch (NOTE_LINE_NUMBER (r))
249 case NOTE_INSN_RANGE_START:
250 case NOTE_INSN_RANGE_END:
251 case NOTE_INSN_LIVE:
252 ggc_mark_rtx (NOTE_RANGE_INFO (r));
253 break;
255 case NOTE_INSN_BLOCK_BEG:
256 case NOTE_INSN_BLOCK_END:
257 ggc_mark_tree (NOTE_BLOCK (r));
258 break;
260 default:
261 if (NOTE_LINE_NUMBER (r) >= 0)
262 ggc_mark_string (NOTE_SOURCE_FILE (r));
263 break;
265 break;
267 default:
268 break;
271 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
273 switch (*fmt)
275 case 'e': case 'u':
276 ggc_mark_rtx (XEXP (r, i));
277 break;
278 case 'V': case 'E':
279 ggc_mark_rtvec (XVEC (r, i));
280 break;
281 case 'S': case 's':
282 ggc_mark_if_gcable (XSTR (r, i));
283 break;
288 void
289 ggc_mark_rtvec_children (v)
290 rtvec v;
292 int i;
294 i = GET_NUM_ELEM (v);
295 while (--i >= 0)
296 ggc_mark_rtx (RTVEC_ELT (v, i));
299 void
300 ggc_mark_tree_children (t)
301 tree t;
303 enum tree_code code = TREE_CODE (t);
305 /* Collect statistics, if appropriate. */
306 if (ggc_stats)
308 ++ggc_stats->num_trees[(int) code];
309 ggc_stats->size_trees[(int) code] += ggc_get_size (t);
312 /* Bits from common. */
313 ggc_mark_tree (TREE_TYPE (t));
314 ggc_mark_tree (TREE_CHAIN (t));
316 /* Some nodes require special handling. */
317 switch (code)
319 case TREE_LIST:
320 ggc_mark_tree (TREE_PURPOSE (t));
321 ggc_mark_tree (TREE_VALUE (t));
322 return;
324 case TREE_VEC:
326 int i = TREE_VEC_LENGTH (t);
327 while (--i >= 0)
328 ggc_mark_tree (TREE_VEC_ELT (t, i));
329 return;
332 case SAVE_EXPR:
333 ggc_mark_tree (TREE_OPERAND (t, 0));
334 ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
335 ggc_mark_rtx (SAVE_EXPR_RTL (t));
336 return;
338 case RTL_EXPR:
339 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
340 ggc_mark_rtx (RTL_EXPR_RTL (t));
341 return;
343 case CALL_EXPR:
344 ggc_mark_tree (TREE_OPERAND (t, 0));
345 ggc_mark_tree (TREE_OPERAND (t, 1));
346 ggc_mark_rtx (CALL_EXPR_RTL (t));
347 return;
349 case COMPLEX_CST:
350 ggc_mark_tree (TREE_REALPART (t));
351 ggc_mark_tree (TREE_IMAGPART (t));
352 break;
354 case STRING_CST:
355 ggc_mark_string (TREE_STRING_POINTER (t));
356 break;
358 case PARM_DECL:
359 ggc_mark_rtx (DECL_INCOMING_RTL (t));
360 break;
362 case IDENTIFIER_NODE:
363 ggc_mark_string (IDENTIFIER_POINTER (t));
364 lang_mark_tree (t);
365 return;
367 default:
368 break;
371 /* But in general we can handle them by class. */
372 switch (TREE_CODE_CLASS (code))
374 case 'd': /* A decl node. */
375 ggc_mark_string (DECL_SOURCE_FILE (t));
376 ggc_mark_tree (DECL_SIZE (t));
377 ggc_mark_tree (DECL_NAME (t));
378 ggc_mark_tree (DECL_CONTEXT (t));
379 ggc_mark_tree (DECL_ARGUMENTS (t));
380 ggc_mark_tree (DECL_RESULT (t));
381 ggc_mark_tree (DECL_INITIAL (t));
382 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
383 ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
384 ggc_mark_tree (DECL_SECTION_NAME (t));
385 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
386 ggc_mark_rtx (DECL_RTL (t));
387 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t));
388 ggc_mark_tree (DECL_VINDEX (t));
389 lang_mark_tree (t);
390 break;
392 case 't': /* A type node. */
393 ggc_mark_tree (TYPE_SIZE (t));
394 ggc_mark_tree (TYPE_SIZE_UNIT (t));
395 ggc_mark_tree (TYPE_ATTRIBUTES (t));
396 ggc_mark_tree (TYPE_VALUES (t));
397 ggc_mark_tree (TYPE_POINTER_TO (t));
398 ggc_mark_tree (TYPE_REFERENCE_TO (t));
399 ggc_mark_tree (TYPE_NAME (t));
400 ggc_mark_tree (TYPE_MIN_VALUE (t));
401 ggc_mark_tree (TYPE_MAX_VALUE (t));
402 ggc_mark_tree (TYPE_NEXT_VARIANT (t));
403 ggc_mark_tree (TYPE_MAIN_VARIANT (t));
404 ggc_mark_tree (TYPE_BINFO (t));
405 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
406 ggc_mark_tree (TYPE_CONTEXT (t));
407 lang_mark_tree (t);
408 break;
410 case 'b': /* A lexical block. */
411 ggc_mark_tree (BLOCK_VARS (t));
412 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
413 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
414 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
415 break;
417 case 'c': /* A constant. */
418 ggc_mark_rtx (TREE_CST_RTL (t));
419 break;
421 case 'r': case '<': case '1':
422 case '2': case 'e': case 's': /* Expressions. */
424 int i = tree_code_length[TREE_CODE (t)];
425 while (--i >= 0)
426 ggc_mark_tree (TREE_OPERAND (t, i));
427 break;
430 case 'x':
431 lang_mark_tree (t);
432 break;
436 /* Mark all the elements of the varray V, which contains trees. */
438 void
439 ggc_mark_tree_varray (v)
440 varray_type v;
442 int i;
444 if (v)
445 for (i = v->num_elements - 1; i >= 0; --i)
446 ggc_mark_tree (VARRAY_TREE (v, i));
449 /* Mark the hash table-entry HE. It's key field is really a tree. */
451 static boolean
452 ggc_mark_tree_hash_table_entry (he, k)
453 struct hash_entry *he;
454 hash_table_key k ATTRIBUTE_UNUSED;
456 ggc_mark_tree ((tree) he->key);
457 return true;
460 /* Mark all the elements of the hash-table H, which contains trees. */
462 void
463 ggc_mark_tree_hash_table (ht)
464 struct hash_table *ht;
466 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
469 /* Allocation wrappers. */
471 char *
472 ggc_alloc_string (contents, length)
473 const char *contents;
474 int length;
476 char *string;
478 if (length < 0)
480 if (contents == NULL)
481 return NULL;
482 length = strlen (contents);
485 string = (char *) ggc_alloc_obj (length + 1, 0);
486 if (contents != NULL)
487 memcpy (string, contents, length);
488 string[length] = 0;
490 return string;
493 /* Print statistics that are independent of the collector in use. */
495 void
496 ggc_print_statistics (stream, stats)
497 FILE *stream;
498 ggc_statistics *stats;
500 int code;
502 /* Set the pointer so that during collection we will actually gather
503 the statistics. */
504 ggc_stats = stats;
506 /* Then do one collection to fill in the statistics. */
507 ggc_collect ();
509 /* Total the statistics. */
510 for (code = 0; code < MAX_TREE_CODES; ++code)
512 stats->total_num_trees += stats->num_trees[code];
513 stats->total_size_trees += stats->size_trees[code];
515 for (code = 0; code < NUM_RTX_CODE; ++code)
517 stats->total_num_rtxs += stats->num_rtxs[code];
518 stats->total_size_rtxs += stats->size_rtxs[code];
521 /* Print the statistics for trees. */
522 fprintf (stream, "%-22s%-16s%-16s%-7s\n", "Code",
523 "Number", "Bytes", "% Total");
524 for (code = 0; code < MAX_TREE_CODES; ++code)
525 if (ggc_stats->num_trees[code])
527 fprintf (stream, "%s%*s%-15u %-15lu %7.3f\n",
528 tree_code_name[code],
529 22 - (int) strlen (tree_code_name[code]), "",
530 ggc_stats->num_trees[code],
531 (unsigned long) ggc_stats->size_trees[code],
532 (100 * ((double) ggc_stats->size_trees[code])
533 / ggc_stats->total_size_trees));
535 fprintf (stream,
536 "%-22s%-15u %-15u\n", "Total",
537 ggc_stats->total_num_trees,
538 ggc_stats->total_size_trees);
540 /* Print the statistics for RTL. */
541 fprintf (stream, "\n%-22s%-16s%-16s%-7s\n", "Code",
542 "Number", "Bytes", "% Total");
543 for (code = 0; code < NUM_RTX_CODE; ++code)
544 if (ggc_stats->num_rtxs[code])
546 fprintf (stream, "%s%*s%-15u %-15lu %7.3f\n",
547 rtx_name[code],
548 22 - (int) strlen (rtx_name[code]), "",
549 ggc_stats->num_rtxs[code],
550 (unsigned long) ggc_stats->size_rtxs[code],
551 (100 * ((double) ggc_stats->size_rtxs[code])
552 / ggc_stats->total_size_rtxs));
554 fprintf (stream,
555 "%-22s%-15u %-15u\n", "Total",
556 ggc_stats->total_num_rtxs,
557 ggc_stats->total_size_rtxs);
560 /* Don't gather statistics any more. */
561 ggc_stats = NULL;