1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001 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 it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU CC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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 the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
33 /* Statistics about the allocation. */
34 static ggc_statistics *ggc_stats;
36 /* The FALSE_LABEL_STACK, declared in except.h, has language-dependent
37 semantics. If a front-end needs to mark the false label stack, it
38 should set this pointer to a non-NULL value. Otherwise, no marking
40 void (*lang_mark_false_label_stack) PARAMS ((struct label_node *));
42 /* Trees that have been marked, but whose children still need marking. */
43 varray_type ggc_pending_trees;
45 static void ggc_mark_rtx_ptr PARAMS ((void *));
46 static void ggc_mark_tree_ptr PARAMS ((void *));
47 static void ggc_mark_rtx_varray_ptr PARAMS ((void *));
48 static void ggc_mark_tree_varray_ptr PARAMS ((void *));
49 static void ggc_mark_tree_hash_table_ptr PARAMS ((void *));
50 static void ggc_mark_trees PARAMS ((void));
51 static bool ggc_mark_tree_hash_table_entry PARAMS ((struct hash_entry *,
54 /* Maintain global roots that are preserved during GC. */
56 /* Global roots that are preserved during calls to gc. */
60 struct ggc_root *next;
64 void (*cb) PARAMS ((void *));
67 static struct ggc_root *roots;
69 /* Add BASE as a new garbage collection root. It is an array of
70 length NELT with each element SIZE bytes long. CB is a
71 function that will be called with a pointer to each element
72 of the array; it is the intention that CB call the appropriate
73 routine to mark gc-able memory for that element. */
76 ggc_add_root (base, nelt, size, cb)
79 void (*cb) PARAMS ((void *));
81 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
92 /* Register an array of rtx as a GC root. */
95 ggc_add_rtx_root (base, nelt)
99 ggc_add_root (base, nelt, sizeof (rtx), ggc_mark_rtx_ptr);
102 /* Register an array of trees as a GC root. */
105 ggc_add_tree_root (base, nelt)
109 ggc_add_root (base, nelt, sizeof (tree), ggc_mark_tree_ptr);
112 /* Register a varray of rtxs as a GC root. */
115 ggc_add_rtx_varray_root (base, nelt)
119 ggc_add_root (base, nelt, sizeof (varray_type),
120 ggc_mark_rtx_varray_ptr);
123 /* Register a varray of trees as a GC root. */
126 ggc_add_tree_varray_root (base, nelt)
130 ggc_add_root (base, nelt, sizeof (varray_type),
131 ggc_mark_tree_varray_ptr);
134 /* Register a hash table of trees as a GC root. */
137 ggc_add_tree_hash_table_root (base, nelt)
138 struct hash_table **base;
141 ggc_add_root (base, nelt, sizeof (struct hash_table *),
142 ggc_mark_tree_hash_table_ptr);
145 /* Remove the previously registered GC root at BASE. */
151 struct ggc_root *x, **p;
153 p = &roots, x = roots;
169 /* Iterate through all registered roots and mark each element. */
176 VARRAY_TREE_INIT (ggc_pending_trees, 4096, "ggc_pending_trees");
178 for (x = roots; x != NULL; x = x->next)
181 int s = x->size, n = x->nelt;
182 void (*cb) PARAMS ((void *)) = x->cb;
185 for (i = 0; i < n; ++i, elt += s)
189 /* Mark all the queued up trees, and their children. */
191 VARRAY_FREE (ggc_pending_trees);
194 /* R had not been previously marked, but has now been marked via
195 ggc_set_mark. Now recurse and process the children. */
198 ggc_mark_rtx_children (r)
207 enum rtx_code code = GET_CODE (r);
208 /* This gets set to a child rtx to eliminate tail recursion. */
211 /* Collect statistics, if appropriate. */
214 ++ggc_stats->num_rtxs[(int) code];
215 ggc_stats->size_rtxs[(int) code] += ggc_get_size (r);
218 /* ??? If (some of) these are really pass-dependent info, do we
219 have any right poking our noses in? */
223 ggc_mark_rtx (JUMP_LABEL (r));
226 ggc_mark_rtx (LABEL_REFS (r));
229 ggc_mark_rtx (LABEL_NEXTREF (r));
230 ggc_mark_rtx (CONTAINING_INSN (r));
233 ggc_mark_tree (ADDRESSOF_DECL (r));
236 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
239 switch (NOTE_LINE_NUMBER (r))
241 case NOTE_INSN_RANGE_BEG:
242 case NOTE_INSN_RANGE_END:
244 case NOTE_INSN_EXPECTED_VALUE:
245 ggc_mark_rtx (NOTE_RANGE_INFO (r));
248 case NOTE_INSN_BLOCK_BEG:
249 case NOTE_INSN_BLOCK_END:
250 ggc_mark_tree (NOTE_BLOCK (r));
262 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
269 if (ggc_test_and_set_mark (exp))
271 if (next_rtx == NULL)
274 ggc_mark_rtx_children (exp);
278 ggc_mark_rtvec (XVEC (r, i));
283 while ((r = next_rtx) != NULL);
286 /* V had not been previously marked, but has now been marked via
287 ggc_set_mark. Now recurse and process the children. */
290 ggc_mark_rtvec_children (v)
295 i = GET_NUM_ELEM (v);
297 ggc_mark_rtx (RTVEC_ELT (v, i));
300 /* Recursively set marks on all of the children of the
301 GCC_PENDING_TREES. */
306 while (ggc_pending_trees->elements_used)
311 t = VARRAY_TOP_TREE (ggc_pending_trees);
312 VARRAY_POP (ggc_pending_trees);
313 code = TREE_CODE (t);
315 /* Collect statistics, if appropriate. */
318 ++ggc_stats->num_trees[(int) code];
319 ggc_stats->size_trees[(int) code] += ggc_get_size (t);
322 /* Bits from common. */
323 ggc_mark_tree (TREE_TYPE (t));
324 ggc_mark_tree (TREE_CHAIN (t));
326 /* Some nodes require special handling. */
330 ggc_mark_tree (TREE_PURPOSE (t));
331 ggc_mark_tree (TREE_VALUE (t));
336 int i = TREE_VEC_LENGTH (t);
339 ggc_mark_tree (TREE_VEC_ELT (t, i));
344 ggc_mark_tree (TREE_REALPART (t));
345 ggc_mark_tree (TREE_IMAGPART (t));
349 ggc_mark_rtx (DECL_INCOMING_RTL (t));
353 ggc_mark_tree (DECL_FIELD_BIT_OFFSET (t));
356 case IDENTIFIER_NODE:
364 /* But in general we can handle them by class. */
365 switch (TREE_CODE_CLASS (code))
367 case 'd': /* A decl node. */
368 ggc_mark_tree (DECL_SIZE (t));
369 ggc_mark_tree (DECL_SIZE_UNIT (t));
370 ggc_mark_tree (DECL_NAME (t));
371 ggc_mark_tree (DECL_CONTEXT (t));
372 ggc_mark_tree (DECL_ARGUMENTS (t));
373 ggc_mark_tree (DECL_RESULT_FLD (t));
374 ggc_mark_tree (DECL_INITIAL (t));
375 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
376 ggc_mark_tree (DECL_SECTION_NAME (t));
377 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
378 if (DECL_RTL_SET_P (t))
379 ggc_mark_rtx (DECL_RTL (t));
380 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t));
381 ggc_mark_tree (DECL_VINDEX (t));
382 if (DECL_ASSEMBLER_NAME_SET_P (t))
383 ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
387 case 't': /* A type node. */
388 ggc_mark_tree (TYPE_SIZE (t));
389 ggc_mark_tree (TYPE_SIZE_UNIT (t));
390 ggc_mark_tree (TYPE_ATTRIBUTES (t));
391 ggc_mark_tree (TYPE_VALUES (t));
392 ggc_mark_tree (TYPE_POINTER_TO (t));
393 ggc_mark_tree (TYPE_REFERENCE_TO (t));
394 ggc_mark_tree (TYPE_NAME (t));
395 ggc_mark_tree (TYPE_MIN_VALUE (t));
396 ggc_mark_tree (TYPE_MAX_VALUE (t));
397 ggc_mark_tree (TYPE_NEXT_VARIANT (t));
398 ggc_mark_tree (TYPE_MAIN_VARIANT (t));
399 ggc_mark_tree (TYPE_BINFO (t));
400 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
401 ggc_mark_tree (TYPE_CONTEXT (t));
405 case 'b': /* A lexical block. */
406 ggc_mark_tree (BLOCK_VARS (t));
407 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
408 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
409 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
412 case 'c': /* A constant. */
413 ggc_mark_rtx (TREE_CST_RTL (t));
416 case 'r': case '<': case '1':
417 case '2': case 'e': case 's': /* Expressions. */
419 int i = TREE_CODE_LENGTH (TREE_CODE (t));
420 int first_rtl = first_rtl_op (TREE_CODE (t));
425 ggc_mark_rtx ((rtx) TREE_OPERAND (t, i));
427 ggc_mark_tree (TREE_OPERAND (t, i));
439 /* Mark all the elements of the varray V, which contains rtxs. */
442 ggc_mark_rtx_varray (v)
448 for (i = v->num_elements - 1; i >= 0; --i)
449 ggc_mark_rtx (VARRAY_RTX (v, i));
452 /* Mark all the elements of the varray V, which contains trees. */
455 ggc_mark_tree_varray (v)
461 for (i = v->num_elements - 1; i >= 0; --i)
462 ggc_mark_tree (VARRAY_TREE (v, i));
465 /* Mark the hash table-entry HE. It's key field is really a tree. */
468 ggc_mark_tree_hash_table_entry (he, k)
469 struct hash_entry *he;
470 hash_table_key k ATTRIBUTE_UNUSED;
472 ggc_mark_tree ((tree) he->key);
476 /* Mark all the elements of the hash-table H, which contains trees. */
479 ggc_mark_tree_hash_table (ht)
480 struct hash_table *ht;
482 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
485 /* Type-correct function to pass to ggc_add_root. It just forwards
486 *ELT (which is an rtx) to ggc_mark_rtx. */
489 ggc_mark_rtx_ptr (elt)
492 ggc_mark_rtx (*(rtx *) elt);
495 /* Type-correct function to pass to ggc_add_root. It just forwards
496 *ELT (which is a tree) to ggc_mark_tree. */
499 ggc_mark_tree_ptr (elt)
502 ggc_mark_tree (*(tree *) elt);
505 /* Type-correct function to pass to ggc_add_root. It just forwards
506 ELT (which is really a varray_type *) to ggc_mark_rtx_varray. */
509 ggc_mark_rtx_varray_ptr (elt)
512 ggc_mark_rtx_varray (*(varray_type *) elt);
515 /* Type-correct function to pass to ggc_add_root. It just forwards
516 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
519 ggc_mark_tree_varray_ptr (elt)
522 ggc_mark_tree_varray (*(varray_type *) elt);
525 /* Type-correct function to pass to ggc_add_root. It just forwards
526 ELT (which is really a struct hash_table **) to
527 ggc_mark_tree_hash_table. */
530 ggc_mark_tree_hash_table_ptr (elt)
533 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
536 /* Allocate a block of memory, then clear it. */
538 ggc_alloc_cleared (size)
541 void *buf = ggc_alloc (size);
542 memset (buf, 0, size);
546 /* Print statistics that are independent of the collector in use. */
547 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
549 : ((x) < 1024*1024*10 \
551 : (x) / (1024*1024))))
552 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
555 ggc_print_common_statistics (stream, stats)
557 ggc_statistics *stats;
561 /* Set the pointer so that during collection we will actually gather
565 /* Then do one collection to fill in the statistics. */
568 /* Total the statistics. */
569 for (code = 0; code < MAX_TREE_CODES; ++code)
571 stats->total_num_trees += stats->num_trees[code];
572 stats->total_size_trees += stats->size_trees[code];
574 for (code = 0; code < NUM_RTX_CODE; ++code)
576 stats->total_num_rtxs += stats->num_rtxs[code];
577 stats->total_size_rtxs += stats->size_rtxs[code];
580 /* Print the statistics for trees. */
581 fprintf (stream, "\n%-17s%10s %16s %10s\n", "Tree",
582 "Number", "Bytes", "% Total");
583 for (code = 0; code < MAX_TREE_CODES; ++code)
584 if (ggc_stats->num_trees[code])
586 fprintf (stream, "%-17s%10u%16ld%c %10.3f\n",
587 tree_code_name[code],
588 ggc_stats->num_trees[code],
589 SCALE (ggc_stats->size_trees[code]),
590 LABEL (ggc_stats->size_trees[code]),
591 (100 * ((double) ggc_stats->size_trees[code])
592 / ggc_stats->total_size_trees));
595 "%-17s%10u%16ld%c\n", "Total",
596 ggc_stats->total_num_trees,
597 SCALE (ggc_stats->total_size_trees),
598 LABEL (ggc_stats->total_size_trees));
600 /* Print the statistics for RTL. */
601 fprintf (stream, "\n%-17s%10s %16s %10s\n", "RTX",
602 "Number", "Bytes", "% Total");
603 for (code = 0; code < NUM_RTX_CODE; ++code)
604 if (ggc_stats->num_rtxs[code])
606 fprintf (stream, "%-17s%10u%16ld%c %10.3f\n",
608 ggc_stats->num_rtxs[code],
609 SCALE (ggc_stats->size_rtxs[code]),
610 LABEL (ggc_stats->size_rtxs[code]),
611 (100 * ((double) ggc_stats->size_rtxs[code])
612 / ggc_stats->total_size_rtxs));
615 "%-17s%10u%16ld%c\n", "Total",
616 ggc_stats->total_num_rtxs,
617 SCALE (ggc_stats->total_size_rtxs),
618 LABEL (ggc_stats->total_size_rtxs));
620 /* Don't gather statistics any more. */