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 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 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
*,
44 /* Maintain global roots that are preserved during GC. */
46 /* Global roots that are preserved during calls to gc. */
50 struct ggc_root
*next
;
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. */
63 ggc_mark_rtx_ptr (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. */
73 ggc_mark_tree_ptr (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. */
83 ggc_mark_tree_varray_ptr (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. */
94 ggc_mark_tree_hash_table_ptr (elt
)
97 ggc_mark_tree_hash_table (*(struct hash_table
**) elt
);
100 /* Type-correct function to pass to ggc_add_root. It just forwards
101 ELT (which is really a char **) to ggc_mark_string. */
104 ggc_mark_string_ptr (elt
)
107 ggc_mark_string (*(char **) elt
);
110 /* Add BASE as a new garbage collection root. It is an array of
111 length NELT with each element SIZE bytes long. CB is a
112 function that will be called with a pointer to each element
113 of the array; it is the intention that CB call the appropriate
114 routine to mark gc-able memory for that element. */
117 ggc_add_root (base
, nelt
, size
, cb
)
120 void (*cb
) PROTO ((void *));
122 struct ggc_root
*x
= (struct ggc_root
*) xmalloc (sizeof (*x
));
133 /* Register an array of rtx as a GC root. */
136 ggc_add_rtx_root (base
, nelt
)
140 ggc_add_root (base
, nelt
, sizeof(rtx
), ggc_mark_rtx_ptr
);
143 /* Register an array of trees as a GC root. */
146 ggc_add_tree_root (base
, nelt
)
150 ggc_add_root (base
, nelt
, sizeof(tree
), ggc_mark_tree_ptr
);
153 /* Register a varray of trees as a GC root. */
156 ggc_add_tree_varray_root (base
, nelt
)
160 ggc_add_root (base
, nelt
, sizeof (varray_type
),
161 ggc_mark_tree_varray_ptr
);
164 /* Register a hash table of trees as a GC root. */
167 ggc_add_tree_hash_table_root (base
, nelt
)
168 struct hash_table
**base
;
171 ggc_add_root (base
, nelt
, sizeof (struct hash_table
*),
172 ggc_mark_tree_hash_table_ptr
);
175 /* Register an array of strings as a GC root. */
178 ggc_add_string_root (base
, nelt
)
182 ggc_add_root (base
, nelt
, sizeof (char *), ggc_mark_string_ptr
);
185 /* Remove the previously registered GC root at BASE. */
191 struct ggc_root
*x
, **p
;
193 p
= &roots
, x
= roots
;
209 /* Iterate through all registered roots and mark each element. */
216 for (x
= roots
; x
!= NULL
; x
= x
->next
)
219 int s
= x
->size
, n
= x
->nelt
;
220 void (*cb
) PROTO ((void *)) = x
->cb
;
223 for (i
= 0; i
< n
; ++i
, elt
+= s
)
228 /* R had not been previously marked, but has now been marked via
229 ggc_set_mark. Now recurse and process the children. */
232 ggc_mark_rtx_children (r
)
237 enum rtx_code code
= GET_CODE (r
);
239 /* Collect statistics, if appropriate. */
242 ++ggc_stats
->num_rtxs
[(int) code
];
243 ggc_stats
->size_rtxs
[(int) code
] += ggc_get_size (r
);
246 /* ??? If (some of) these are really pass-dependant info, do we have
247 any right poking our noses in? */
251 ggc_mark_rtx (JUMP_LABEL (r
));
254 ggc_mark_rtx (LABEL_REFS (r
));
257 ggc_mark_rtx (LABEL_NEXTREF (r
));
258 ggc_mark_rtx (CONTAINING_INSN (r
));
261 ggc_mark_tree (ADDRESSOF_DECL (r
));
264 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r
));
267 switch (NOTE_LINE_NUMBER (r
))
269 case NOTE_INSN_RANGE_START
:
270 case NOTE_INSN_RANGE_END
:
272 ggc_mark_rtx (NOTE_RANGE_INFO (r
));
275 case NOTE_INSN_BLOCK_BEG
:
276 case NOTE_INSN_BLOCK_END
:
277 ggc_mark_tree (NOTE_BLOCK (r
));
281 if (NOTE_LINE_NUMBER (r
) >= 0)
282 ggc_mark_string (NOTE_SOURCE_FILE (r
));
291 for (fmt
= GET_RTX_FORMAT (GET_CODE (r
)), i
= 0; *fmt
; ++fmt
, ++i
)
296 ggc_mark_rtx (XEXP (r
, i
));
299 ggc_mark_rtvec (XVEC (r
, i
));
302 ggc_mark_if_gcable (XSTR (r
, i
));
308 /* V had not been previously marked, but has now been marked via
309 ggc_set_mark. Now recurse and process the children. */
312 ggc_mark_rtvec_children (v
)
317 i
= GET_NUM_ELEM (v
);
319 ggc_mark_rtx (RTVEC_ELT (v
, i
));
322 /* T had not been previously marked, but has now been marked via
323 ggc_set_mark. Now recurse and process the children. */
326 ggc_mark_tree_children (t
)
329 enum tree_code code
= TREE_CODE (t
);
331 /* Collect statistics, if appropriate. */
334 ++ggc_stats
->num_trees
[(int) code
];
335 ggc_stats
->size_trees
[(int) code
] += ggc_get_size (t
);
338 /* Bits from common. */
339 ggc_mark_tree (TREE_TYPE (t
));
340 ggc_mark_tree (TREE_CHAIN (t
));
342 /* Some nodes require special handling. */
346 ggc_mark_tree (TREE_PURPOSE (t
));
347 ggc_mark_tree (TREE_VALUE (t
));
352 int i
= TREE_VEC_LENGTH (t
);
354 ggc_mark_tree (TREE_VEC_ELT (t
, i
));
359 ggc_mark_tree (TREE_OPERAND (t
, 0));
360 ggc_mark_tree (SAVE_EXPR_CONTEXT (t
));
361 ggc_mark_rtx (SAVE_EXPR_RTL (t
));
365 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t
));
366 ggc_mark_rtx (RTL_EXPR_RTL (t
));
370 ggc_mark_tree (TREE_OPERAND (t
, 0));
371 ggc_mark_tree (TREE_OPERAND (t
, 1));
372 ggc_mark_rtx (CALL_EXPR_RTL (t
));
376 ggc_mark_tree (TREE_REALPART (t
));
377 ggc_mark_tree (TREE_IMAGPART (t
));
381 ggc_mark_string (TREE_STRING_POINTER (t
));
385 ggc_mark_rtx (DECL_INCOMING_RTL (t
));
388 case IDENTIFIER_NODE
:
389 ggc_mark_string (IDENTIFIER_POINTER (t
));
397 /* But in general we can handle them by class. */
398 switch (TREE_CODE_CLASS (code
))
400 case 'd': /* A decl node. */
401 ggc_mark_string (DECL_SOURCE_FILE (t
));
402 ggc_mark_tree (DECL_SIZE (t
));
403 ggc_mark_tree (DECL_NAME (t
));
404 ggc_mark_tree (DECL_CONTEXT (t
));
405 ggc_mark_tree (DECL_ARGUMENTS (t
));
406 ggc_mark_tree (DECL_RESULT (t
));
407 ggc_mark_tree (DECL_INITIAL (t
));
408 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t
));
409 ggc_mark_tree (DECL_ASSEMBLER_NAME (t
));
410 ggc_mark_tree (DECL_SECTION_NAME (t
));
411 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t
));
412 ggc_mark_rtx (DECL_RTL (t
));
413 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t
));
414 ggc_mark_tree (DECL_VINDEX (t
));
418 case 't': /* A type node. */
419 ggc_mark_tree (TYPE_SIZE (t
));
420 ggc_mark_tree (TYPE_SIZE_UNIT (t
));
421 ggc_mark_tree (TYPE_ATTRIBUTES (t
));
422 ggc_mark_tree (TYPE_VALUES (t
));
423 ggc_mark_tree (TYPE_POINTER_TO (t
));
424 ggc_mark_tree (TYPE_REFERENCE_TO (t
));
425 ggc_mark_tree (TYPE_NAME (t
));
426 ggc_mark_tree (TYPE_MIN_VALUE (t
));
427 ggc_mark_tree (TYPE_MAX_VALUE (t
));
428 ggc_mark_tree (TYPE_NEXT_VARIANT (t
));
429 ggc_mark_tree (TYPE_MAIN_VARIANT (t
));
430 ggc_mark_tree (TYPE_BINFO (t
));
431 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t
));
432 ggc_mark_tree (TYPE_CONTEXT (t
));
436 case 'b': /* A lexical block. */
437 ggc_mark_tree (BLOCK_VARS (t
));
438 ggc_mark_tree (BLOCK_SUBBLOCKS (t
));
439 ggc_mark_tree (BLOCK_SUPERCONTEXT (t
));
440 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t
));
443 case 'c': /* A constant. */
444 ggc_mark_rtx (TREE_CST_RTL (t
));
447 case 'r': case '<': case '1':
448 case '2': case 'e': case 's': /* Expressions. */
450 int i
= tree_code_length
[TREE_CODE (t
)];
452 ggc_mark_tree (TREE_OPERAND (t
, i
));
462 /* Mark all the elements of the varray V, which contains trees. */
465 ggc_mark_tree_varray (v
)
471 for (i
= v
->num_elements
- 1; i
>= 0; --i
)
472 ggc_mark_tree (VARRAY_TREE (v
, i
));
475 /* Mark the hash table-entry HE. It's key field is really a tree. */
478 ggc_mark_tree_hash_table_entry (he
, k
)
479 struct hash_entry
*he
;
480 hash_table_key k ATTRIBUTE_UNUSED
;
482 ggc_mark_tree ((tree
) he
->key
);
486 /* Mark all the elements of the hash-table H, which contains trees. */
489 ggc_mark_tree_hash_table (ht
)
490 struct hash_table
*ht
;
492 hash_traverse (ht
, ggc_mark_tree_hash_table_entry
, /*info=*/0);
495 /* Allocate a gc-able string. If CONTENTS is null, then the memory will
496 be uninitialized. If LENGTH is -1, then CONTENTS is assumed to be a
497 null-terminated string and the memory sized accordingly. Otherwise,
498 the memory is filled with LENGTH bytes from CONTENTS. */
501 ggc_alloc_string (contents
, length
)
502 const char *contents
;
509 if (contents
== NULL
)
511 length
= strlen (contents
);
514 string
= (char *) ggc_alloc_obj (length
+ 1, 0);
515 if (contents
!= NULL
)
516 memcpy (string
, contents
, length
);
522 /* Print statistics that are independent of the collector in use. */
525 ggc_print_statistics (stream
, stats
)
527 ggc_statistics
*stats
;
531 /* Set the pointer so that during collection we will actually gather
535 /* Then do one collection to fill in the statistics. */
538 /* Total the statistics. */
539 for (code
= 0; code
< MAX_TREE_CODES
; ++code
)
541 stats
->total_num_trees
+= stats
->num_trees
[code
];
542 stats
->total_size_trees
+= stats
->size_trees
[code
];
544 for (code
= 0; code
< NUM_RTX_CODE
; ++code
)
546 stats
->total_num_rtxs
+= stats
->num_rtxs
[code
];
547 stats
->total_size_rtxs
+= stats
->size_rtxs
[code
];
550 /* Print the statistics for trees. */
551 fprintf (stream
, "%-22s%-16s%-16s%-7s\n", "Code",
552 "Number", "Bytes", "% Total");
553 for (code
= 0; code
< MAX_TREE_CODES
; ++code
)
554 if (ggc_stats
->num_trees
[code
])
556 fprintf (stream
, "%s%*s%-15u %-15lu %7.3f\n",
557 tree_code_name
[code
],
558 22 - (int) strlen (tree_code_name
[code
]), "",
559 ggc_stats
->num_trees
[code
],
560 (unsigned long) ggc_stats
->size_trees
[code
],
561 (100 * ((double) ggc_stats
->size_trees
[code
])
562 / ggc_stats
->total_size_trees
));
565 "%-22s%-15u %-15u\n", "Total",
566 ggc_stats
->total_num_trees
,
567 ggc_stats
->total_size_trees
);
569 /* Print the statistics for RTL. */
570 fprintf (stream
, "\n%-22s%-16s%-16s%-7s\n", "Code",
571 "Number", "Bytes", "% Total");
572 for (code
= 0; code
< NUM_RTX_CODE
; ++code
)
573 if (ggc_stats
->num_rtxs
[code
])
575 fprintf (stream
, "%s%*s%-15u %-15lu %7.3f\n",
577 22 - (int) strlen (rtx_name
[code
]), "",
578 ggc_stats
->num_rtxs
[code
],
579 (unsigned long) ggc_stats
->size_rtxs
[code
],
580 (100 * ((double) ggc_stats
->size_rtxs
[code
])
581 / ggc_stats
->total_size_rtxs
));
584 "%-22s%-15u %-15u\n", "Total",
585 ggc_stats
->total_num_rtxs
,
586 ggc_stats
->total_size_rtxs
);
589 /* Don't gather statistics any more. */