OSDN Git Service

bacbcddbd5d19f392ea4b9c39b1c95325a3c82cf
[pf3gnuchains/gcc-fork.git] / gcc / ggc-common.c
1 /* Simple garbage collection for the GNU compiler.
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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.  */
20
21 /* Generic garbage collection (GC) functions and data, not specific to
22    any particular GC implementation.  */
23
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"
32
33 /* Statistics about the allocation.  */
34 static ggc_statistics *ggc_stats;
35
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 *,
42                                                        hash_table_key));
43
44 /* Maintain global roots that are preserved during GC.  */
45
46 /* Global roots that are preserved during calls to gc.  */
47
48 struct ggc_root
49 {
50   struct ggc_root *next;
51   void *base;
52   int nelt;
53   int size;
54   void (*cb) PROTO ((void *));
55 };
56
57 static struct ggc_root *roots;
58
59 /* Type-correct function to pass to ggc_add_root.  It just forwards
60    *ELT (which is an rtx) to ggc_mark_tree_varray.  */
61
62 static void
63 ggc_mark_rtx_ptr (elt)
64      void *elt;
65 {
66   ggc_mark_rtx (*(rtx *) elt);
67 }
68
69 /* Type-correct function to pass to ggc_add_root.  It just forwards
70    *ELT (which is a tree) to ggc_mark_tree.  */
71
72 static void
73 ggc_mark_tree_ptr (elt)
74      void *elt;
75 {
76   ggc_mark_tree (*(tree *) elt);
77 }
78
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.  */
81
82 static void
83 ggc_mark_tree_varray_ptr (elt)
84      void *elt;
85 {
86   ggc_mark_tree_varray (*(varray_type *) elt);
87 }
88
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.  */
92
93 static void
94 ggc_mark_tree_hash_table_ptr (elt)
95      void *elt;
96 {
97   ggc_mark_tree_hash_table (*(struct hash_table **) elt);
98 }
99
100 /* Type-correct function to pass to ggc_add_root.  It just forwards
101    ELT (which is really a char **) to ggc_mark_string.  */
102
103 static void
104 ggc_mark_string_ptr (elt)
105      void *elt;
106 {
107   ggc_mark_string (*(char **) elt);
108 }
109
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.  */
115
116 void
117 ggc_add_root (base, nelt, size, cb)
118      void *base;
119      int nelt, size;
120      void (*cb) PROTO ((void *));
121 {
122   struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
123
124   x->next = roots;
125   x->base = base;
126   x->nelt = nelt;
127   x->size = size;
128   x->cb = cb;
129
130   roots = x;
131 }
132
133 /* Register an array of rtx as a GC root.  */
134
135 void
136 ggc_add_rtx_root (base, nelt)
137      rtx *base;
138      int nelt;
139 {
140   ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
141 }
142
143 /* Register an array of trees as a GC root.  */
144
145 void
146 ggc_add_tree_root (base, nelt)
147      tree *base;
148      int nelt;
149 {
150   ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
151 }
152
153 /* Register a varray of trees as a GC root.  */
154
155 void
156 ggc_add_tree_varray_root (base, nelt)
157      varray_type *base;
158      int nelt;
159 {
160   ggc_add_root (base, nelt, sizeof (varray_type), 
161                 ggc_mark_tree_varray_ptr);
162 }
163
164 /* Register a hash table of trees as a GC root.  */
165
166 void
167 ggc_add_tree_hash_table_root (base, nelt)
168      struct hash_table **base;
169      int nelt;
170 {
171   ggc_add_root (base, nelt, sizeof (struct hash_table *), 
172                 ggc_mark_tree_hash_table_ptr);
173 }
174
175 /* Register an array of strings as a GC root.  */
176
177 void
178 ggc_add_string_root (base, nelt)
179      char **base;
180      int nelt;
181 {
182   ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
183 }
184
185 /* Remove the previously registered GC root at BASE.  */
186
187 void
188 ggc_del_root (base)
189      void *base;
190 {
191   struct ggc_root *x, **p;
192
193   p = &roots, x = roots;
194   while (x)
195     {
196       if (x->base == base)
197         {
198           *p = x->next;
199           free (x);
200           return;
201         }
202       p = &x->next;
203       x = x->next;
204     }
205
206   abort();
207 }
208
209 /* Iterate through all registered roots and mark each element.  */
210
211 void
212 ggc_mark_roots ()
213 {
214   struct ggc_root* x;
215   
216   for (x = roots; x != NULL; x = x->next)
217     {
218       char *elt = x->base;
219       int s = x->size, n = x->nelt;
220       void (*cb) PROTO ((void *)) = x->cb;
221       int i;
222
223       for (i = 0; i < n; ++i, elt += s)
224         (*cb)(elt);
225     }
226 }
227
228 /* R had not been previously marked, but has now been marked via
229    ggc_set_mark.  Now recurse and process the children.  */
230
231 void
232 ggc_mark_rtx_children (r)
233      rtx r;
234 {
235   const char *fmt;
236   int i;
237   enum rtx_code code = GET_CODE (r);
238
239   /* Collect statistics, if appropriate.  */
240   if (ggc_stats)
241     {
242       ++ggc_stats->num_rtxs[(int) code];
243       ggc_stats->size_rtxs[(int) code] += ggc_get_size (r);
244     }
245
246   /* ??? If (some of) these are really pass-dependant info, do we have
247      any right poking our noses in?  */
248   switch (code)
249     {
250     case JUMP_INSN:
251       ggc_mark_rtx (JUMP_LABEL (r));
252       break;
253     case CODE_LABEL:
254       ggc_mark_rtx (LABEL_REFS (r));
255       ggc_mark_string (LABEL_ALTERNATE_NAME (r));
256       break;
257     case LABEL_REF:
258       ggc_mark_rtx (LABEL_NEXTREF (r));
259       ggc_mark_rtx (CONTAINING_INSN (r));
260       break;
261     case ADDRESSOF:
262       ggc_mark_tree (ADDRESSOF_DECL (r));
263       break;
264     case CONST_DOUBLE:
265       ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
266       break;
267     case NOTE:
268       switch (NOTE_LINE_NUMBER (r))
269         {
270         case NOTE_INSN_RANGE_START:
271         case NOTE_INSN_RANGE_END:
272         case NOTE_INSN_LIVE:
273           ggc_mark_rtx (NOTE_RANGE_INFO (r));
274           break;
275
276         case NOTE_INSN_BLOCK_BEG:
277         case NOTE_INSN_BLOCK_END:
278           ggc_mark_tree (NOTE_BLOCK (r));
279           break;
280
281         default:
282           if (NOTE_LINE_NUMBER (r) >= 0)
283             ggc_mark_string (NOTE_SOURCE_FILE (r));
284           break;
285         }
286       break;
287
288     default:
289       break;
290     }
291
292   for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
293     {
294       switch (*fmt)
295         {
296         case 'e': case 'u':
297           ggc_mark_rtx (XEXP (r, i));
298           break;
299         case 'V': case 'E':
300           ggc_mark_rtvec (XVEC (r, i));
301           break;
302         case 'S': case 's':
303           ggc_mark_if_gcable (XSTR (r, i));
304           break;
305         }
306     }
307 }
308
309 /* V had not been previously marked, but has now been marked via
310    ggc_set_mark.  Now recurse and process the children.  */
311
312 void
313 ggc_mark_rtvec_children (v)
314      rtvec v;
315 {
316   int i;
317
318   i = GET_NUM_ELEM (v);
319   while (--i >= 0)
320     ggc_mark_rtx (RTVEC_ELT (v, i));
321 }
322
323 /* T had not been previously marked, but has now been marked via
324    ggc_set_mark.  Now recurse and process the children.  */
325
326 void
327 ggc_mark_tree_children (t)
328      tree t;
329 {
330   enum tree_code code = TREE_CODE (t);
331
332   /* Collect statistics, if appropriate.  */
333   if (ggc_stats)
334     {
335       ++ggc_stats->num_trees[(int) code];
336       ggc_stats->size_trees[(int) code] += ggc_get_size (t);
337     }
338
339   /* Bits from common.  */
340   ggc_mark_tree (TREE_TYPE (t));
341   ggc_mark_tree (TREE_CHAIN (t));
342
343   /* Some nodes require special handling.  */
344   switch (code)
345     {
346     case TREE_LIST:
347       ggc_mark_tree (TREE_PURPOSE (t));
348       ggc_mark_tree (TREE_VALUE (t));
349       return;
350
351     case TREE_VEC:
352       {
353         int i = TREE_VEC_LENGTH (t);
354         while (--i >= 0)
355           ggc_mark_tree (TREE_VEC_ELT (t, i));
356         return;
357       }
358
359     case SAVE_EXPR:
360       ggc_mark_tree (TREE_OPERAND (t, 0));
361       ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
362       ggc_mark_rtx (SAVE_EXPR_RTL (t));
363       return;
364
365     case RTL_EXPR:
366       ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
367       ggc_mark_rtx (RTL_EXPR_RTL (t));
368       return;
369
370     case CALL_EXPR:
371       ggc_mark_tree (TREE_OPERAND (t, 0));
372       ggc_mark_tree (TREE_OPERAND (t, 1));
373       ggc_mark_rtx (CALL_EXPR_RTL (t));
374       return;
375
376     case COMPLEX_CST:
377       ggc_mark_tree (TREE_REALPART (t));
378       ggc_mark_tree (TREE_IMAGPART (t));
379       break;
380
381     case STRING_CST:
382       ggc_mark_string (TREE_STRING_POINTER (t));
383       break;
384
385     case PARM_DECL:
386       ggc_mark_rtx (DECL_INCOMING_RTL (t));
387       break;
388
389     case IDENTIFIER_NODE:
390       ggc_mark_string (IDENTIFIER_POINTER (t));
391       lang_mark_tree (t);
392       return;
393
394     default:
395       break;
396     }
397   
398   /* But in general we can handle them by class.  */
399   switch (TREE_CODE_CLASS (code))
400     {
401     case 'd': /* A decl node.  */
402       ggc_mark_string (DECL_SOURCE_FILE (t));
403       ggc_mark_tree (DECL_SIZE (t));
404       ggc_mark_tree (DECL_NAME (t));
405       ggc_mark_tree (DECL_CONTEXT (t));
406       ggc_mark_tree (DECL_ARGUMENTS (t));
407       ggc_mark_tree (DECL_RESULT (t));
408       ggc_mark_tree (DECL_INITIAL (t));
409       ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
410       ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
411       ggc_mark_tree (DECL_SECTION_NAME (t));
412       ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
413       ggc_mark_rtx (DECL_RTL (t));
414       ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t));
415       ggc_mark_tree (DECL_VINDEX (t));
416       lang_mark_tree (t);
417       break;
418
419     case 't': /* A type node.  */
420       ggc_mark_tree (TYPE_SIZE (t));
421       ggc_mark_tree (TYPE_SIZE_UNIT (t));
422       ggc_mark_tree (TYPE_ATTRIBUTES (t));
423       ggc_mark_tree (TYPE_VALUES (t));
424       ggc_mark_tree (TYPE_POINTER_TO (t));
425       ggc_mark_tree (TYPE_REFERENCE_TO (t));
426       ggc_mark_tree (TYPE_NAME (t));
427       ggc_mark_tree (TYPE_MIN_VALUE (t));
428       ggc_mark_tree (TYPE_MAX_VALUE (t));
429       ggc_mark_tree (TYPE_NEXT_VARIANT (t));
430       ggc_mark_tree (TYPE_MAIN_VARIANT (t));
431       ggc_mark_tree (TYPE_BINFO (t));
432       ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
433       ggc_mark_tree (TYPE_CONTEXT (t));
434       lang_mark_tree (t);
435       break;
436
437     case 'b': /* A lexical block.  */
438       ggc_mark_tree (BLOCK_VARS (t));
439       ggc_mark_tree (BLOCK_SUBBLOCKS (t));
440       ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
441       ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
442       break;
443
444     case 'c': /* A constant.  */
445       ggc_mark_rtx (TREE_CST_RTL (t));
446       break;
447
448     case 'r': case '<': case '1':
449     case '2': case 'e': case 's': /* Expressions.  */
450       {
451         int i = tree_code_length[TREE_CODE (t)];
452         while (--i >= 0)
453           ggc_mark_tree (TREE_OPERAND (t, i));
454         break;
455       }
456
457     case 'x':
458       lang_mark_tree (t);
459       break;
460     }
461 }
462
463 /* Mark all the elements of the varray V, which contains trees.  */
464
465 void
466 ggc_mark_tree_varray (v)
467      varray_type v;
468 {
469   int i;
470
471   if (v)
472     for (i = v->num_elements - 1; i >= 0; --i) 
473       ggc_mark_tree (VARRAY_TREE (v, i));
474 }
475
476 /* Mark the hash table-entry HE.  It's key field is really a tree.  */
477
478 static boolean
479 ggc_mark_tree_hash_table_entry (he, k)
480      struct hash_entry *he;
481      hash_table_key k ATTRIBUTE_UNUSED;
482 {
483   ggc_mark_tree ((tree) he->key);
484   return true;
485 }
486
487 /* Mark all the elements of the hash-table H, which contains trees.  */
488
489 void
490 ggc_mark_tree_hash_table (ht)
491      struct hash_table *ht;
492 {
493   hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
494 }
495
496 /* Allocate a gc-able string.  If CONTENTS is null, then the memory will
497    be uninitialized.  If LENGTH is -1, then CONTENTS is assumed to be a
498    null-terminated string and the memory sized accordingly.  Otherwise,
499    the memory is filled with LENGTH bytes from CONTENTS.  */
500
501 char *
502 ggc_alloc_string (contents, length)
503      const char *contents;
504      int length;
505 {
506   char *string;
507
508   if (length < 0)
509     {
510       if (contents == NULL)
511         return NULL;
512       length = strlen (contents);
513     }
514
515   string = (char *) ggc_alloc_obj (length + 1, 0);
516   if (contents != NULL)
517     memcpy (string, contents, length);
518   string[length] = 0;
519
520   return string;
521 }
522
523 /* Print statistics that are independent of the collector in use.  */
524
525 void
526 ggc_print_statistics (stream, stats)
527      FILE *stream;
528      ggc_statistics *stats;
529 {
530   int code;
531
532   /* Set the pointer so that during collection we will actually gather
533      the statistics.  */
534   ggc_stats = stats;
535
536   /* Then do one collection to fill in the statistics.  */
537   ggc_collect ();
538
539   /* Total the statistics.  */
540   for (code = 0; code < MAX_TREE_CODES; ++code)
541     {
542       stats->total_num_trees += stats->num_trees[code];
543       stats->total_size_trees += stats->size_trees[code];
544     }
545   for (code = 0; code < NUM_RTX_CODE; ++code)
546     {
547       stats->total_num_rtxs += stats->num_rtxs[code];
548       stats->total_size_rtxs += stats->size_rtxs[code];
549     }
550
551   /* Print the statistics for trees.  */
552   fprintf (stream, "%-22s%-16s%-16s%-7s\n", "Code", 
553            "Number", "Bytes", "% Total");
554   for (code = 0; code < MAX_TREE_CODES; ++code)
555     if (ggc_stats->num_trees[code]) 
556       {
557         fprintf (stream, "%s%*s%-15u %-15lu %7.3f\n", 
558                  tree_code_name[code],
559                  22 - (int) strlen (tree_code_name[code]), "",
560                  ggc_stats->num_trees[code],
561                  (unsigned long) ggc_stats->size_trees[code],
562                  (100 * ((double) ggc_stats->size_trees[code]) 
563                   / ggc_stats->total_size_trees));
564       }
565   fprintf (stream,
566            "%-22s%-15u %-15lu\n", "Total",
567            ggc_stats->total_num_trees,
568            (unsigned long) ggc_stats->total_size_trees);
569
570   /* Print the statistics for RTL.  */
571   fprintf (stream, "\n%-22s%-16s%-16s%-7s\n", "Code", 
572            "Number", "Bytes", "% Total");
573   for (code = 0; code < NUM_RTX_CODE; ++code)
574     if (ggc_stats->num_rtxs[code]) 
575       {
576         fprintf (stream, "%s%*s%-15u %-15lu %7.3f\n", 
577                  rtx_name[code],
578                  22 - (int) strlen (rtx_name[code]), "",
579                  ggc_stats->num_rtxs[code],
580                  (unsigned long) ggc_stats->size_rtxs[code],
581                  (100 * ((double) ggc_stats->size_rtxs[code]) 
582                   / ggc_stats->total_size_rtxs));
583       }
584   fprintf (stream,
585            "%-22s%-15u %-15lu\n", "Total",
586            ggc_stats->total_num_rtxs,
587            (unsigned long) ggc_stats->total_size_rtxs);
588
589
590   /* Don't gather statistics any more.  */
591   ggc_stats = NULL;
592 }