1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000 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_rtx_varray_ptr
PARAMS ((void *));
39 static void ggc_mark_tree_varray_ptr
PARAMS ((void *));
40 static void ggc_mark_tree_hash_table_ptr
PARAMS ((void *));
41 static void ggc_mark_string_ptr
PARAMS ((void *));
42 static boolean ggc_mark_tree_hash_table_entry
PARAMS ((struct hash_entry
*,
45 /* Maintain global roots that are preserved during GC. */
47 /* Global roots that are preserved during calls to gc. */
51 struct ggc_root
*next
;
55 void (*cb
) PARAMS ((void *));
58 static struct ggc_root
*roots
;
60 /* Add BASE as a new garbage collection root. It is an array of
61 length NELT with each element SIZE bytes long. CB is a
62 function that will be called with a pointer to each element
63 of the array; it is the intention that CB call the appropriate
64 routine to mark gc-able memory for that element. */
67 ggc_add_root (base
, nelt
, size
, cb
)
70 void (*cb
) PARAMS ((void *));
72 struct ggc_root
*x
= (struct ggc_root
*) xmalloc (sizeof (*x
));
83 /* Register an array of rtx as a GC root. */
86 ggc_add_rtx_root (base
, nelt
)
90 ggc_add_root (base
, nelt
, sizeof(rtx
), ggc_mark_rtx_ptr
);
93 /* Register an array of trees as a GC root. */
96 ggc_add_tree_root (base
, nelt
)
100 ggc_add_root (base
, nelt
, sizeof(tree
), ggc_mark_tree_ptr
);
103 /* Register a varray of rtxs as a GC root. */
106 ggc_add_rtx_varray_root (base
, nelt
)
110 ggc_add_root (base
, nelt
, sizeof (varray_type
),
111 ggc_mark_rtx_varray_ptr
);
114 /* Register a varray of trees as a GC root. */
117 ggc_add_tree_varray_root (base
, nelt
)
121 ggc_add_root (base
, nelt
, sizeof (varray_type
),
122 ggc_mark_tree_varray_ptr
);
125 /* Register a hash table of trees as a GC root. */
128 ggc_add_tree_hash_table_root (base
, nelt
)
129 struct hash_table
**base
;
132 ggc_add_root (base
, nelt
, sizeof (struct hash_table
*),
133 ggc_mark_tree_hash_table_ptr
);
136 /* Register an array of strings as a GC root. */
139 ggc_add_string_root (base
, nelt
)
143 ggc_add_root (base
, nelt
, sizeof (char *), ggc_mark_string_ptr
);
146 /* Remove the previously registered GC root at BASE. */
152 struct ggc_root
*x
, **p
;
154 p
= &roots
, x
= roots
;
170 /* Iterate through all registered roots and mark each element. */
177 for (x
= roots
; x
!= NULL
; x
= x
->next
)
180 int s
= x
->size
, n
= x
->nelt
;
181 void (*cb
) PARAMS ((void *)) = x
->cb
;
184 for (i
= 0; i
< n
; ++i
, elt
+= s
)
189 /* R had not been previously marked, but has now been marked via
190 ggc_set_mark. Now recurse and process the children. */
193 ggc_mark_rtx_children (r
)
202 enum rtx_code code
= GET_CODE (r
);
203 /* This gets set to a child rtx to eliminate tail recursion. */
206 /* Collect statistics, if appropriate. */
209 ++ggc_stats
->num_rtxs
[(int) code
];
210 ggc_stats
->size_rtxs
[(int) code
] += ggc_get_size (r
);
213 /* ??? If (some of) these are really pass-dependant info, do we
214 have any right poking our noses in? */
218 ggc_mark_rtx (JUMP_LABEL (r
));
221 ggc_mark_rtx (LABEL_REFS (r
));
222 ggc_mark_string (LABEL_ALTERNATE_NAME (r
));
225 ggc_mark_rtx (LABEL_NEXTREF (r
));
226 ggc_mark_rtx (CONTAINING_INSN (r
));
229 ggc_mark_tree (ADDRESSOF_DECL (r
));
232 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r
));
235 switch (NOTE_LINE_NUMBER (r
))
237 case NOTE_INSN_RANGE_START
:
238 case NOTE_INSN_RANGE_END
:
240 ggc_mark_rtx (NOTE_RANGE_INFO (r
));
243 case NOTE_INSN_BLOCK_BEG
:
244 case NOTE_INSN_BLOCK_END
:
245 ggc_mark_tree (NOTE_BLOCK (r
));
249 if (NOTE_LINE_NUMBER (r
) >= 0)
250 ggc_mark_string (NOTE_SOURCE_FILE (r
));
259 for (fmt
= GET_RTX_FORMAT (GET_CODE (r
)), i
= 0; *fmt
; ++fmt
, ++i
)
266 if (ggc_test_and_set_mark (exp
))
268 if (next_rtx
== NULL
)
271 ggc_mark_rtx_children (exp
);
275 ggc_mark_rtvec (XVEC (r
, i
));
278 ggc_mark_if_gcable (XSTR (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 /* T had not been previously marked, but has now been marked via
301 ggc_set_mark. Now recurse and process the children. */
304 ggc_mark_tree_children (t
)
307 enum tree_code code
= TREE_CODE (t
);
309 /* Collect statistics, if appropriate. */
312 ++ggc_stats
->num_trees
[(int) code
];
313 ggc_stats
->size_trees
[(int) code
] += ggc_get_size (t
);
316 /* Bits from common. */
317 ggc_mark_tree (TREE_TYPE (t
));
318 ggc_mark_tree (TREE_CHAIN (t
));
320 /* Some nodes require special handling. */
324 ggc_mark_tree (TREE_PURPOSE (t
));
325 ggc_mark_tree (TREE_VALUE (t
));
330 int i
= TREE_VEC_LENGTH (t
);
332 ggc_mark_tree (TREE_VEC_ELT (t
, i
));
337 ggc_mark_tree (TREE_OPERAND (t
, 0));
338 ggc_mark_tree (SAVE_EXPR_CONTEXT (t
));
339 ggc_mark_rtx (SAVE_EXPR_RTL (t
));
343 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t
));
344 ggc_mark_rtx (RTL_EXPR_RTL (t
));
348 ggc_mark_tree (TREE_OPERAND (t
, 0));
349 ggc_mark_tree (TREE_OPERAND (t
, 1));
350 ggc_mark_rtx (CALL_EXPR_RTL (t
));
354 ggc_mark_tree (TREE_REALPART (t
));
355 ggc_mark_tree (TREE_IMAGPART (t
));
359 ggc_mark_string (TREE_STRING_POINTER (t
));
363 ggc_mark_rtx (DECL_INCOMING_RTL (t
));
366 case IDENTIFIER_NODE
:
367 ggc_mark_string (IDENTIFIER_POINTER (t
));
375 /* But in general we can handle them by class. */
376 switch (TREE_CODE_CLASS (code
))
378 case 'd': /* A decl node. */
379 ggc_mark_string (DECL_SOURCE_FILE (t
));
380 ggc_mark_tree (DECL_SIZE (t
));
381 ggc_mark_tree (DECL_NAME (t
));
382 ggc_mark_tree (DECL_CONTEXT (t
));
383 ggc_mark_tree (DECL_ARGUMENTS (t
));
384 ggc_mark_tree (DECL_RESULT (t
));
385 ggc_mark_tree (DECL_INITIAL (t
));
386 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t
));
387 ggc_mark_tree (DECL_ASSEMBLER_NAME (t
));
388 ggc_mark_tree (DECL_SECTION_NAME (t
));
389 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t
));
390 ggc_mark_rtx (DECL_RTL (t
));
391 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t
));
392 ggc_mark_tree (DECL_VINDEX (t
));
396 case 't': /* A type node. */
397 ggc_mark_tree (TYPE_SIZE (t
));
398 ggc_mark_tree (TYPE_SIZE_UNIT (t
));
399 ggc_mark_tree (TYPE_ATTRIBUTES (t
));
400 ggc_mark_tree (TYPE_VALUES (t
));
401 ggc_mark_tree (TYPE_POINTER_TO (t
));
402 ggc_mark_tree (TYPE_REFERENCE_TO (t
));
403 ggc_mark_tree (TYPE_NAME (t
));
404 ggc_mark_tree (TYPE_MIN_VALUE (t
));
405 ggc_mark_tree (TYPE_MAX_VALUE (t
));
406 ggc_mark_tree (TYPE_NEXT_VARIANT (t
));
407 ggc_mark_tree (TYPE_MAIN_VARIANT (t
));
408 ggc_mark_tree (TYPE_BINFO (t
));
409 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t
));
410 ggc_mark_tree (TYPE_CONTEXT (t
));
414 case 'b': /* A lexical block. */
415 ggc_mark_tree (BLOCK_VARS (t
));
416 ggc_mark_tree (BLOCK_SUBBLOCKS (t
));
417 ggc_mark_tree (BLOCK_SUPERCONTEXT (t
));
418 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t
));
421 case 'c': /* A constant. */
422 ggc_mark_rtx (TREE_CST_RTL (t
));
425 case 'r': case '<': case '1':
426 case '2': case 'e': case 's': /* Expressions. */
428 int i
= tree_code_length
[TREE_CODE (t
)];
430 ggc_mark_tree (TREE_OPERAND (t
, i
));
440 /* Mark all the elements of the varray V, which contains rtxs. */
443 ggc_mark_rtx_varray (v
)
449 for (i
= v
->num_elements
- 1; i
>= 0; --i
)
450 ggc_mark_rtx (VARRAY_RTX (v
, i
));
453 /* Mark all the elements of the varray V, which contains trees. */
456 ggc_mark_tree_varray (v
)
462 for (i
= v
->num_elements
- 1; i
>= 0; --i
)
463 ggc_mark_tree (VARRAY_TREE (v
, i
));
466 /* Mark the hash table-entry HE. It's key field is really a tree. */
469 ggc_mark_tree_hash_table_entry (he
, k
)
470 struct hash_entry
*he
;
471 hash_table_key k ATTRIBUTE_UNUSED
;
473 ggc_mark_tree ((tree
) he
->key
);
477 /* Mark all the elements of the hash-table H, which contains trees. */
480 ggc_mark_tree_hash_table (ht
)
481 struct hash_table
*ht
;
483 hash_traverse (ht
, ggc_mark_tree_hash_table_entry
, /*info=*/0);
486 /* Type-correct function to pass to ggc_add_root. It just forwards
487 *ELT (which is an rtx) to ggc_mark_rtx. */
490 ggc_mark_rtx_ptr (elt
)
493 ggc_mark_rtx (*(rtx
*) elt
);
496 /* Type-correct function to pass to ggc_add_root. It just forwards
497 *ELT (which is a tree) to ggc_mark_tree. */
500 ggc_mark_tree_ptr (elt
)
503 ggc_mark_tree (*(tree
*) elt
);
506 /* Type-correct function to pass to ggc_add_root. It just forwards
507 ELT (which is really a varray_type *) to ggc_mark_rtx_varray. */
510 ggc_mark_rtx_varray_ptr (elt
)
513 ggc_mark_rtx_varray (*(varray_type
*) elt
);
516 /* Type-correct function to pass to ggc_add_root. It just forwards
517 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
520 ggc_mark_tree_varray_ptr (elt
)
523 ggc_mark_tree_varray (*(varray_type
*) elt
);
526 /* Type-correct function to pass to ggc_add_root. It just forwards
527 ELT (which is really a struct hash_table **) to
528 ggc_mark_tree_hash_table. */
531 ggc_mark_tree_hash_table_ptr (elt
)
534 ggc_mark_tree_hash_table (*(struct hash_table
**) elt
);
537 /* Type-correct function to pass to ggc_add_root. It just forwards
538 ELT (which is really a char **) to ggc_mark_string. */
541 ggc_mark_string_ptr (elt
)
544 ggc_mark_string (*(char **) elt
);
547 /* Allocate a gc-able string. If CONTENTS is null, then the memory will
548 be uninitialized. If LENGTH is -1, then CONTENTS is assumed to be a
549 null-terminated string and the memory sized accordingly. Otherwise,
550 the memory is filled with LENGTH bytes from CONTENTS. */
553 ggc_alloc_string (contents
, length
)
554 const char *contents
;
561 if (contents
== NULL
)
563 length
= strlen (contents
);
566 string
= (char *) ggc_alloc_obj (length
+ 1, 0);
567 if (contents
!= NULL
)
568 memcpy (string
, contents
, length
);
574 /* Print statistics that are independent of the collector in use. */
577 ggc_print_statistics (stream
, stats
)
579 ggc_statistics
*stats
;
583 /* Set the pointer so that during collection we will actually gather
587 /* Then do one collection to fill in the statistics. */
590 /* Total the statistics. */
591 for (code
= 0; code
< MAX_TREE_CODES
; ++code
)
593 stats
->total_num_trees
+= stats
->num_trees
[code
];
594 stats
->total_size_trees
+= stats
->size_trees
[code
];
596 for (code
= 0; code
< NUM_RTX_CODE
; ++code
)
598 stats
->total_num_rtxs
+= stats
->num_rtxs
[code
];
599 stats
->total_size_rtxs
+= stats
->size_rtxs
[code
];
602 /* Print the statistics for trees. */
603 fprintf (stream
, "%-22s%-16s%-16s%-7s\n", "Code",
604 "Number", "Bytes", "% Total");
605 for (code
= 0; code
< MAX_TREE_CODES
; ++code
)
606 if (ggc_stats
->num_trees
[code
])
608 fprintf (stream
, "%s%*s%-15u %-15lu %7.3f\n",
609 tree_code_name
[code
],
610 22 - (int) strlen (tree_code_name
[code
]), "",
611 ggc_stats
->num_trees
[code
],
612 (unsigned long) ggc_stats
->size_trees
[code
],
613 (100 * ((double) ggc_stats
->size_trees
[code
])
614 / ggc_stats
->total_size_trees
));
617 "%-22s%-15u %-15lu\n", "Total",
618 ggc_stats
->total_num_trees
,
619 (unsigned long) ggc_stats
->total_size_trees
);
621 /* Print the statistics for RTL. */
622 fprintf (stream
, "\n%-22s%-16s%-16s%-7s\n", "Code",
623 "Number", "Bytes", "% Total");
624 for (code
= 0; code
< NUM_RTX_CODE
; ++code
)
625 if (ggc_stats
->num_rtxs
[code
])
627 fprintf (stream
, "%s%*s%-15u %-15lu %7.3f\n",
629 22 - (int) strlen (rtx_name
[code
]), "",
630 ggc_stats
->num_rtxs
[code
],
631 (unsigned long) ggc_stats
->size_rtxs
[code
],
632 (100 * ((double) ggc_stats
->size_rtxs
[code
])
633 / ggc_stats
->total_size_rtxs
));
636 "%-22s%-15u %-15lu\n", "Total",
637 ggc_stats
->total_num_rtxs
,
638 (unsigned long) ggc_stats
->total_size_rtxs
);
641 /* Don't gather statistics any more. */