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)
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. */
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
);
101 ggc_mark_string_ptr (elt
)
104 ggc_mark_string (*(char **)elt
);
108 ggc_add_root (base
, nelt
, size
, cb
)
111 void (*cb
) PROTO ((void *));
113 struct ggc_root
*x
= (struct ggc_root
*) xmalloc (sizeof (*x
));
125 ggc_add_rtx_root (base
, nelt
)
129 ggc_add_root (base
, nelt
, sizeof(rtx
), ggc_mark_rtx_ptr
);
133 ggc_add_tree_root (base
, 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. */
143 ggc_add_tree_varray_root (base
, 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
155 ggc_add_tree_hash_table_root (base
, nelt
)
156 struct hash_table
**base
;
159 ggc_add_root (base
, nelt
, sizeof (struct hash_table
*),
160 ggc_mark_tree_hash_table_ptr
);
164 ggc_add_string_root (base
, nelt
)
168 ggc_add_root (base
, nelt
, sizeof (char *), ggc_mark_string_ptr
);
176 struct ggc_root
*x
, **p
;
178 p
= &roots
, x
= roots
;
199 for (x
= roots
; x
!= NULL
; x
= x
->next
)
202 int s
= x
->size
, n
= x
->nelt
;
203 void (*cb
) PROTO ((void *)) = x
->cb
;
206 for (i
= 0; i
< n
; ++i
, elt
+= s
)
212 ggc_mark_rtx_children (r
)
217 enum rtx_code code
= GET_CODE (r
);
219 /* Collect statistics, if appropriate. */
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? */
231 ggc_mark_rtx (JUMP_LABEL (r
));
234 ggc_mark_rtx (LABEL_REFS (r
));
237 ggc_mark_rtx (LABEL_NEXTREF (r
));
238 ggc_mark_rtx (CONTAINING_INSN (r
));
241 ggc_mark_tree (ADDRESSOF_DECL (r
));
244 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r
));
247 switch (NOTE_LINE_NUMBER (r
))
249 case NOTE_INSN_RANGE_START
:
250 case NOTE_INSN_RANGE_END
:
252 ggc_mark_rtx (NOTE_RANGE_INFO (r
));
255 case NOTE_INSN_BLOCK_BEG
:
256 case NOTE_INSN_BLOCK_END
:
257 ggc_mark_tree (NOTE_BLOCK (r
));
261 if (NOTE_LINE_NUMBER (r
) >= 0)
262 ggc_mark_string (NOTE_SOURCE_FILE (r
));
271 for (fmt
= GET_RTX_FORMAT (GET_CODE (r
)), i
= 0; *fmt
; ++fmt
, ++i
)
276 ggc_mark_rtx (XEXP (r
, i
));
279 ggc_mark_rtvec (XVEC (r
, i
));
282 ggc_mark_if_gcable (XSTR (r
, i
));
289 ggc_mark_rtvec_children (v
)
294 i
= GET_NUM_ELEM (v
);
296 ggc_mark_rtx (RTVEC_ELT (v
, i
));
300 ggc_mark_tree_children (t
)
303 enum tree_code code
= TREE_CODE (t
);
305 /* Collect statistics, if appropriate. */
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. */
320 ggc_mark_tree (TREE_PURPOSE (t
));
321 ggc_mark_tree (TREE_VALUE (t
));
326 int i
= TREE_VEC_LENGTH (t
);
328 ggc_mark_tree (TREE_VEC_ELT (t
, i
));
333 ggc_mark_tree (TREE_OPERAND (t
, 0));
334 ggc_mark_tree (SAVE_EXPR_CONTEXT (t
));
335 ggc_mark_rtx (SAVE_EXPR_RTL (t
));
339 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t
));
340 ggc_mark_rtx (RTL_EXPR_RTL (t
));
344 ggc_mark_tree (TREE_OPERAND (t
, 0));
345 ggc_mark_tree (TREE_OPERAND (t
, 1));
346 ggc_mark_rtx (CALL_EXPR_RTL (t
));
350 ggc_mark_tree (TREE_REALPART (t
));
351 ggc_mark_tree (TREE_IMAGPART (t
));
355 ggc_mark_string (TREE_STRING_POINTER (t
));
359 ggc_mark_rtx (DECL_INCOMING_RTL (t
));
362 case IDENTIFIER_NODE
:
363 ggc_mark_string (IDENTIFIER_POINTER (t
));
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
));
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
));
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
));
417 case 'c': /* A constant. */
418 ggc_mark_rtx (TREE_CST_RTL (t
));
421 case 'r': case '<': case '1':
422 case '2': case 'e': case 's': /* Expressions. */
424 int i
= tree_code_length
[TREE_CODE (t
)];
426 ggc_mark_tree (TREE_OPERAND (t
, i
));
436 /* Mark all the elements of the varray V, which contains trees. */
439 ggc_mark_tree_varray (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. */
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
);
460 /* Mark all the elements of the hash-table H, which contains trees. */
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. */
472 ggc_alloc_string (contents
, length
)
473 const char *contents
;
480 if (contents
== NULL
)
482 length
= strlen (contents
);
485 string
= (char *) ggc_alloc_obj (length
+ 1, 0);
486 if (contents
!= NULL
)
487 memcpy (string
, contents
, length
);
493 /* Print statistics that are independent of the collector in use. */
496 ggc_print_statistics (stream
, stats
)
498 ggc_statistics
*stats
;
502 /* Set the pointer so that during collection we will actually gather
506 /* Then do one collection to fill in the statistics. */
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
));
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",
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
));
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. */