Add D30V options
[official-gcc.git] / gcc / ggc-common.c
blob2fa3d101493a13d5fd2ec15c617218685e365f3b
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
9 later version.
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
14 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 the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 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 /* Trees that have been marked, but whose children still need marking. */
37 varray_type ggc_pending_trees;
39 static void ggc_mark_rtx_ptr PARAMS ((void *));
40 static void ggc_mark_tree_ptr PARAMS ((void *));
41 static void ggc_mark_rtx_varray_ptr PARAMS ((void *));
42 static void ggc_mark_tree_varray_ptr PARAMS ((void *));
43 static void ggc_mark_tree_hash_table_ptr PARAMS ((void *));
44 static void ggc_mark_string_ptr PARAMS ((void *));
45 static void ggc_mark_trees PARAMS ((void));
46 static boolean ggc_mark_tree_hash_table_entry PARAMS ((struct hash_entry *,
47 hash_table_key));
49 /* Maintain global roots that are preserved during GC. */
51 /* Global roots that are preserved during calls to gc. */
53 struct ggc_root
55 struct ggc_root *next;
56 void *base;
57 int nelt;
58 int size;
59 void (*cb) PARAMS ((void *));
62 static struct ggc_root *roots;
64 /* Add BASE as a new garbage collection root. It is an array of
65 length NELT with each element SIZE bytes long. CB is a
66 function that will be called with a pointer to each element
67 of the array; it is the intention that CB call the appropriate
68 routine to mark gc-able memory for that element. */
70 void
71 ggc_add_root (base, nelt, size, cb)
72 void *base;
73 int nelt, size;
74 void (*cb) PARAMS ((void *));
76 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
78 x->next = roots;
79 x->base = base;
80 x->nelt = nelt;
81 x->size = size;
82 x->cb = cb;
84 roots = x;
87 /* Register an array of rtx as a GC root. */
89 void
90 ggc_add_rtx_root (base, nelt)
91 rtx *base;
92 int nelt;
94 ggc_add_root (base, nelt, sizeof (rtx), ggc_mark_rtx_ptr);
97 /* Register an array of trees as a GC root. */
99 void
100 ggc_add_tree_root (base, nelt)
101 tree *base;
102 int nelt;
104 ggc_add_root (base, nelt, sizeof (tree), ggc_mark_tree_ptr);
107 /* Register a varray of rtxs as a GC root. */
109 void
110 ggc_add_rtx_varray_root (base, nelt)
111 varray_type *base;
112 int nelt;
114 ggc_add_root (base, nelt, sizeof (varray_type),
115 ggc_mark_rtx_varray_ptr);
118 /* Register a varray of trees as a GC root. */
120 void
121 ggc_add_tree_varray_root (base, nelt)
122 varray_type *base;
123 int nelt;
125 ggc_add_root (base, nelt, sizeof (varray_type),
126 ggc_mark_tree_varray_ptr);
129 /* Register a hash table of trees as a GC root. */
131 void
132 ggc_add_tree_hash_table_root (base, nelt)
133 struct hash_table **base;
134 int nelt;
136 ggc_add_root (base, nelt, sizeof (struct hash_table *),
137 ggc_mark_tree_hash_table_ptr);
140 /* Register an array of strings as a GC root. */
142 void
143 ggc_add_string_root (base, nelt)
144 char **base;
145 int nelt;
147 ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
150 /* Remove the previously registered GC root at BASE. */
152 void
153 ggc_del_root (base)
154 void *base;
156 struct ggc_root *x, **p;
158 p = &roots, x = roots;
159 while (x)
161 if (x->base == base)
163 *p = x->next;
164 free (x);
165 return;
167 p = &x->next;
168 x = x->next;
171 abort();
174 /* Iterate through all registered roots and mark each element. */
176 void
177 ggc_mark_roots ()
179 struct ggc_root* x;
181 VARRAY_TREE_INIT (ggc_pending_trees, 4096, "ggc_pending_trees");
183 for (x = roots; x != NULL; x = x->next)
185 char *elt = x->base;
186 int s = x->size, n = x->nelt;
187 void (*cb) PARAMS ((void *)) = x->cb;
188 int i;
190 for (i = 0; i < n; ++i, elt += s)
191 (*cb)(elt);
194 /* Mark all the queued up trees, and their children. */
195 ggc_mark_trees ();
196 VARRAY_FREE (ggc_pending_trees);
199 /* R had not been previously marked, but has now been marked via
200 ggc_set_mark. Now recurse and process the children. */
202 void
203 ggc_mark_rtx_children (r)
204 rtx r;
206 const char *fmt;
207 int i;
208 rtx next_rtx;
212 enum rtx_code code = GET_CODE (r);
213 /* This gets set to a child rtx to eliminate tail recursion. */
214 next_rtx = NULL;
216 /* Collect statistics, if appropriate. */
217 if (ggc_stats)
219 ++ggc_stats->num_rtxs[(int) code];
220 ggc_stats->size_rtxs[(int) code] += ggc_get_size (r);
223 /* ??? If (some of) these are really pass-dependent info, do we
224 have any right poking our noses in? */
225 switch (code)
227 case JUMP_INSN:
228 ggc_mark_rtx (JUMP_LABEL (r));
229 break;
230 case CODE_LABEL:
231 ggc_mark_rtx (LABEL_REFS (r));
232 ggc_mark_string (LABEL_ALTERNATE_NAME (r));
233 break;
234 case LABEL_REF:
235 ggc_mark_rtx (LABEL_NEXTREF (r));
236 ggc_mark_rtx (CONTAINING_INSN (r));
237 break;
238 case ADDRESSOF:
239 ggc_mark_tree (ADDRESSOF_DECL (r));
240 break;
241 case CONST_DOUBLE:
242 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
243 break;
244 case NOTE:
245 switch (NOTE_LINE_NUMBER (r))
247 case NOTE_INSN_RANGE_BEG:
248 case NOTE_INSN_RANGE_END:
249 case NOTE_INSN_LIVE:
250 case NOTE_INSN_EXPECTED_VALUE:
251 ggc_mark_rtx (NOTE_RANGE_INFO (r));
252 break;
254 case NOTE_INSN_BLOCK_BEG:
255 case NOTE_INSN_BLOCK_END:
256 ggc_mark_tree (NOTE_BLOCK (r));
257 break;
259 default:
260 if (NOTE_LINE_NUMBER (r) >= 0)
262 case NOTE_INSN_DELETED_LABEL:
263 ggc_mark_string (NOTE_SOURCE_FILE (r));
265 break;
267 break;
269 default:
270 break;
273 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
275 rtx exp;
276 switch (*fmt)
278 case 'e': case 'u':
279 exp = XEXP (r, i);
280 if (ggc_test_and_set_mark (exp))
282 if (next_rtx == NULL)
283 next_rtx = exp;
284 else
285 ggc_mark_rtx_children (exp);
287 break;
288 case 'V': case 'E':
289 ggc_mark_rtvec (XVEC (r, i));
290 break;
291 case 'S': case 's':
292 ggc_mark_if_gcable (XSTR (r, i));
293 break;
297 while ((r = next_rtx) != NULL);
300 /* V had not been previously marked, but has now been marked via
301 ggc_set_mark. Now recurse and process the children. */
303 void
304 ggc_mark_rtvec_children (v)
305 rtvec v;
307 int i;
309 i = GET_NUM_ELEM (v);
310 while (--i >= 0)
311 ggc_mark_rtx (RTVEC_ELT (v, i));
314 /* Recursively set marks on all of the children of the
315 GCC_PENDING_TREES. */
317 static void
318 ggc_mark_trees ()
320 while (ggc_pending_trees->elements_used)
322 tree t;
323 enum tree_code code;
325 t = VARRAY_TOP_TREE (ggc_pending_trees);
326 VARRAY_POP (ggc_pending_trees);
327 code = TREE_CODE (t);
329 /* Collect statistics, if appropriate. */
330 if (ggc_stats)
332 ++ggc_stats->num_trees[(int) code];
333 ggc_stats->size_trees[(int) code] += ggc_get_size (t);
336 /* Bits from common. */
337 ggc_mark_tree (TREE_TYPE (t));
338 ggc_mark_tree (TREE_CHAIN (t));
340 /* Some nodes require special handling. */
341 switch (code)
343 case TREE_LIST:
344 ggc_mark_tree (TREE_PURPOSE (t));
345 ggc_mark_tree (TREE_VALUE (t));
346 continue;
348 case TREE_VEC:
350 int i = TREE_VEC_LENGTH (t);
352 while (--i >= 0)
353 ggc_mark_tree (TREE_VEC_ELT (t, i));
354 continue;
357 case COMPLEX_CST:
358 ggc_mark_tree (TREE_REALPART (t));
359 ggc_mark_tree (TREE_IMAGPART (t));
360 break;
362 case STRING_CST:
363 ggc_mark_string (TREE_STRING_POINTER (t));
364 break;
366 case PARM_DECL:
367 ggc_mark_rtx (DECL_INCOMING_RTL (t));
368 break;
370 case FIELD_DECL:
371 ggc_mark_tree (DECL_FIELD_BIT_OFFSET (t));
372 break;
374 case IDENTIFIER_NODE:
375 ggc_mark_string (IDENTIFIER_POINTER (t));
376 lang_mark_tree (t);
377 continue;
379 default:
380 break;
383 /* But in general we can handle them by class. */
384 switch (TREE_CODE_CLASS (code))
386 case 'd': /* A decl node. */
387 ggc_mark_string (DECL_SOURCE_FILE (t));
388 ggc_mark_tree (DECL_SIZE (t));
389 ggc_mark_tree (DECL_SIZE_UNIT (t));
390 ggc_mark_tree (DECL_NAME (t));
391 ggc_mark_tree (DECL_CONTEXT (t));
392 ggc_mark_tree (DECL_ARGUMENTS (t));
393 ggc_mark_tree (DECL_RESULT_FLD (t));
394 ggc_mark_tree (DECL_INITIAL (t));
395 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
396 ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
397 ggc_mark_tree (DECL_SECTION_NAME (t));
398 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
399 ggc_mark_rtx (DECL_RTL (t));
400 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t));
401 ggc_mark_tree (DECL_VINDEX (t));
402 lang_mark_tree (t);
403 break;
405 case 't': /* A type node. */
406 ggc_mark_tree (TYPE_SIZE (t));
407 ggc_mark_tree (TYPE_SIZE_UNIT (t));
408 ggc_mark_tree (TYPE_ATTRIBUTES (t));
409 ggc_mark_tree (TYPE_VALUES (t));
410 ggc_mark_tree (TYPE_POINTER_TO (t));
411 ggc_mark_tree (TYPE_REFERENCE_TO (t));
412 ggc_mark_tree (TYPE_NAME (t));
413 ggc_mark_tree (TYPE_MIN_VALUE (t));
414 ggc_mark_tree (TYPE_MAX_VALUE (t));
415 ggc_mark_tree (TYPE_NEXT_VARIANT (t));
416 ggc_mark_tree (TYPE_MAIN_VARIANT (t));
417 ggc_mark_tree (TYPE_BINFO (t));
418 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
419 ggc_mark_tree (TYPE_CONTEXT (t));
420 lang_mark_tree (t);
421 break;
423 case 'b': /* A lexical block. */
424 ggc_mark_tree (BLOCK_VARS (t));
425 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
426 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
427 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
428 break;
430 case 'c': /* A constant. */
431 ggc_mark_rtx (TREE_CST_RTL (t));
432 break;
434 case 'r': case '<': case '1':
435 case '2': case 'e': case 's': /* Expressions. */
437 int i = TREE_CODE_LENGTH (TREE_CODE (t));
438 int first_rtl = first_rtl_op (TREE_CODE (t));
440 while (--i >= 0)
442 if (i >= first_rtl)
443 ggc_mark_rtx ((rtx) TREE_OPERAND (t, i));
444 else
445 ggc_mark_tree (TREE_OPERAND (t, i));
447 break;
450 case 'x':
451 lang_mark_tree (t);
452 break;
457 /* Mark all the elements of the varray V, which contains rtxs. */
459 void
460 ggc_mark_rtx_varray (v)
461 varray_type v;
463 int i;
465 if (v)
466 for (i = v->num_elements - 1; i >= 0; --i)
467 ggc_mark_rtx (VARRAY_RTX (v, i));
470 /* Mark all the elements of the varray V, which contains trees. */
472 void
473 ggc_mark_tree_varray (v)
474 varray_type v;
476 int i;
478 if (v)
479 for (i = v->num_elements - 1; i >= 0; --i)
480 ggc_mark_tree (VARRAY_TREE (v, i));
483 /* Mark the hash table-entry HE. It's key field is really a tree. */
485 static boolean
486 ggc_mark_tree_hash_table_entry (he, k)
487 struct hash_entry *he;
488 hash_table_key k ATTRIBUTE_UNUSED;
490 ggc_mark_tree ((tree) he->key);
491 return true;
494 /* Mark all the elements of the hash-table H, which contains trees. */
496 void
497 ggc_mark_tree_hash_table (ht)
498 struct hash_table *ht;
500 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
503 /* Type-correct function to pass to ggc_add_root. It just forwards
504 *ELT (which is an rtx) to ggc_mark_rtx. */
506 static void
507 ggc_mark_rtx_ptr (elt)
508 void *elt;
510 ggc_mark_rtx (*(rtx *) elt);
513 /* Type-correct function to pass to ggc_add_root. It just forwards
514 *ELT (which is a tree) to ggc_mark_tree. */
516 static void
517 ggc_mark_tree_ptr (elt)
518 void *elt;
520 ggc_mark_tree (*(tree *) elt);
523 /* Type-correct function to pass to ggc_add_root. It just forwards
524 ELT (which is really a varray_type *) to ggc_mark_rtx_varray. */
526 static void
527 ggc_mark_rtx_varray_ptr (elt)
528 void *elt;
530 ggc_mark_rtx_varray (*(varray_type *) elt);
533 /* Type-correct function to pass to ggc_add_root. It just forwards
534 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
536 static void
537 ggc_mark_tree_varray_ptr (elt)
538 void *elt;
540 ggc_mark_tree_varray (*(varray_type *) elt);
543 /* Type-correct function to pass to ggc_add_root. It just forwards
544 ELT (which is really a struct hash_table **) to
545 ggc_mark_tree_hash_table. */
547 static void
548 ggc_mark_tree_hash_table_ptr (elt)
549 void *elt;
551 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
554 /* Type-correct function to pass to ggc_add_root. It just forwards
555 ELT (which is really a char **) to ggc_mark_string. */
557 static void
558 ggc_mark_string_ptr (elt)
559 void *elt;
561 ggc_mark_string (*(char **) elt);
564 /* Allocate a gc-able string. If CONTENTS is null, then the memory will
565 be uninitialized. If LENGTH is -1, then CONTENTS is assumed to be a
566 null-terminated string and the memory sized accordingly. Otherwise,
567 the memory is filled with LENGTH bytes from CONTENTS. */
569 char *
570 ggc_alloc_string (contents, length)
571 const char *contents;
572 int length;
574 char *string;
576 if (length < 0)
578 if (contents == NULL)
579 return NULL;
580 length = strlen (contents);
583 string = (char *) ggc_alloc (length + 1);
584 if (contents != NULL)
585 memcpy (string, contents, length);
586 string[length] = 0;
588 return string;
591 /* Allocate a block of memory, then clear it. */
592 void *
593 ggc_alloc_cleared (size)
594 size_t size;
596 void *buf = ggc_alloc (size);
597 memset (buf, 0, size);
598 return buf;
601 /* Print statistics that are independent of the collector in use. */
603 void
604 ggc_print_statistics (stream, stats)
605 FILE *stream;
606 ggc_statistics *stats;
608 int code;
610 /* Set the pointer so that during collection we will actually gather
611 the statistics. */
612 ggc_stats = stats;
614 /* Then do one collection to fill in the statistics. */
615 ggc_collect ();
617 /* Total the statistics. */
618 for (code = 0; code < MAX_TREE_CODES; ++code)
620 stats->total_num_trees += stats->num_trees[code];
621 stats->total_size_trees += stats->size_trees[code];
623 for (code = 0; code < NUM_RTX_CODE; ++code)
625 stats->total_num_rtxs += stats->num_rtxs[code];
626 stats->total_size_rtxs += stats->size_rtxs[code];
629 /* Print the statistics for trees. */
630 fprintf (stream, "%-22s%-16s%-16s%-7s\n", "Code",
631 "Number", "Bytes", "% Total");
632 for (code = 0; code < MAX_TREE_CODES; ++code)
633 if (ggc_stats->num_trees[code])
635 fprintf (stream, "%s%*s%-15u %-15lu %7.3f\n",
636 tree_code_name[code],
637 22 - (int) strlen (tree_code_name[code]), "",
638 ggc_stats->num_trees[code],
639 (unsigned long) ggc_stats->size_trees[code],
640 (100 * ((double) ggc_stats->size_trees[code])
641 / ggc_stats->total_size_trees));
643 fprintf (stream,
644 "%-22s%-15u %-15lu\n", "Total",
645 ggc_stats->total_num_trees,
646 (unsigned long) ggc_stats->total_size_trees);
648 /* Print the statistics for RTL. */
649 fprintf (stream, "\n%-22s%-16s%-16s%-7s\n", "Code",
650 "Number", "Bytes", "% Total");
651 for (code = 0; code < NUM_RTX_CODE; ++code)
652 if (ggc_stats->num_rtxs[code])
654 fprintf (stream, "%s%*s%-15u %-15lu %7.3f\n",
655 rtx_name[code],
656 22 - (int) strlen (rtx_name[code]), "",
657 ggc_stats->num_rtxs[code],
658 (unsigned long) ggc_stats->size_rtxs[code],
659 (100 * ((double) ggc_stats->size_rtxs[code])
660 / ggc_stats->total_size_rtxs));
662 fprintf (stream,
663 "%-22s%-15u %-15lu\n", "Total",
664 ggc_stats->total_num_rtxs,
665 (unsigned long) ggc_stats->total_size_rtxs);
668 /* Don't gather statistics any more. */
669 ggc_stats = NULL;