OSDN Git Service

* ggc.h (struct ggc_statistics): New type.
[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
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)
9    any later version.
10
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.
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
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 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 static void
101 ggc_mark_string_ptr (elt)
102      void *elt;
103 {
104   ggc_mark_string (*(char **)elt);
105 }
106
107 void
108 ggc_add_root (base, nelt, size, cb)
109      void *base;
110      int nelt, size;
111      void (*cb) PROTO ((void *));
112 {
113   struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
114
115   x->next = roots;
116   x->base = base;
117   x->nelt = nelt;
118   x->size = size;
119   x->cb = cb;
120
121   roots = x;
122 }
123
124 void
125 ggc_add_rtx_root (base, nelt)
126      rtx *base;
127      int nelt;
128 {
129   ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
130 }
131
132 void
133 ggc_add_tree_root (base, nelt)
134      tree *base;
135      int nelt;
136 {
137   ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
138 }
139
140 /* Add V (a varray full of trees) to the list of GC roots.  */
141
142 void
143 ggc_add_tree_varray_root (base, nelt)
144      varray_type *base;
145      int nelt;
146 {
147   ggc_add_root (base, nelt, sizeof (varray_type), 
148                 ggc_mark_tree_varray_ptr);
149 }
150
151 /* Add HT (a hash-table where ever key is a tree) to the list of GC
152    roots.  */
153
154 void
155 ggc_add_tree_hash_table_root (base, nelt)
156      struct hash_table **base;
157      int nelt;
158 {
159   ggc_add_root (base, nelt, sizeof (struct hash_table *), 
160                 ggc_mark_tree_hash_table_ptr);
161 }
162
163 void
164 ggc_add_string_root (base, nelt)
165      char **base;
166      int nelt;
167 {
168   ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
169 }
170
171
172 void
173 ggc_del_root (base)
174      void *base;
175 {
176   struct ggc_root *x, **p;
177
178   p = &roots, x = roots;
179   while (x)
180     {
181       if (x->base == base)
182         {
183           *p = x->next;
184           free (x);
185           return;
186         }
187       p = &x->next;
188       x = x->next;
189     }
190
191   abort();
192 }
193
194 void
195 ggc_mark_roots ()
196 {
197   struct ggc_root* x;
198   
199   for (x = roots; x != NULL; x = x->next)
200     {
201       char *elt = x->base;
202       int s = x->size, n = x->nelt;
203       void (*cb) PROTO ((void *)) = x->cb;
204       int i;
205
206       for (i = 0; i < n; ++i, elt += s)
207         (*cb)(elt);
208     }
209 }
210
211 void
212 ggc_mark_rtx_children (r)
213      rtx r;
214 {
215   const char *fmt;
216   int i;
217   enum rtx_code code = GET_CODE (r);
218
219   /* Collect statistics, if appropriate.  */
220   if (ggc_stats)
221     {
222       ++ggc_stats->num_rtxs[(int) code];
223       ggc_stats->size_rtxs[(int) code] += ggc_get_size (r);
224     }
225
226   /* ??? If (some of) these are really pass-dependant info, do we have
227      any right poking our noses in?  */
228   switch (code)
229     {
230     case JUMP_INSN:
231       ggc_mark_rtx (JUMP_LABEL (r));
232       break;
233     case CODE_LABEL:
234       ggc_mark_rtx (LABEL_REFS (r));
235       break;
236     case LABEL_REF:
237       ggc_mark_rtx (LABEL_NEXTREF (r));
238       ggc_mark_rtx (CONTAINING_INSN (r));
239       break;
240     case ADDRESSOF:
241       ggc_mark_tree (ADDRESSOF_DECL (r));
242       break;
243     case CONST_DOUBLE:
244       ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
245       break;
246     case NOTE:
247       switch (NOTE_LINE_NUMBER (r))
248         {
249         case NOTE_INSN_RANGE_START:
250         case NOTE_INSN_RANGE_END:
251         case NOTE_INSN_LIVE:
252           ggc_mark_rtx (NOTE_RANGE_INFO (r));
253           break;
254
255         case NOTE_INSN_BLOCK_BEG:
256         case NOTE_INSN_BLOCK_END:
257           ggc_mark_tree (NOTE_BLOCK (r));
258           break;
259
260         default:
261           if (NOTE_LINE_NUMBER (r) >= 0)
262             ggc_mark_string (NOTE_SOURCE_FILE (r));
263           break;
264         }
265       break;
266
267     default:
268       break;
269     }
270
271   for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
272     {
273       switch (*fmt)
274         {
275         case 'e': case 'u':
276           ggc_mark_rtx (XEXP (r, i));
277           break;
278         case 'V': case 'E':
279           ggc_mark_rtvec (XVEC (r, i));
280           break;
281         case 'S': case 's':
282           ggc_mark_if_gcable (XSTR (r, i));
283           break;
284         }
285     }
286 }
287
288 void
289 ggc_mark_rtvec_children (v)
290      rtvec v;
291 {
292   int i;
293
294   i = GET_NUM_ELEM (v);
295   while (--i >= 0)
296     ggc_mark_rtx (RTVEC_ELT (v, i));
297 }
298
299 void
300 ggc_mark_tree_children (t)
301      tree t;
302 {
303   enum tree_code code = TREE_CODE (t);
304
305   /* Collect statistics, if appropriate.  */
306   if (ggc_stats)
307     {
308       ++ggc_stats->num_trees[(int) code];
309       ggc_stats->size_trees[(int) code] += ggc_get_size (t);
310     }
311
312   /* Bits from common.  */
313   ggc_mark_tree (TREE_TYPE (t));
314   ggc_mark_tree (TREE_CHAIN (t));
315
316   /* Some nodes require special handling.  */
317   switch (code)
318     {
319     case TREE_LIST:
320       ggc_mark_tree (TREE_PURPOSE (t));
321       ggc_mark_tree (TREE_VALUE (t));
322       return;
323
324     case TREE_VEC:
325       {
326         int i = TREE_VEC_LENGTH (t);
327         while (--i >= 0)
328           ggc_mark_tree (TREE_VEC_ELT (t, i));
329         return;
330       }
331
332     case SAVE_EXPR:
333       ggc_mark_tree (TREE_OPERAND (t, 0));
334       ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
335       ggc_mark_rtx (SAVE_EXPR_RTL (t));
336       return;
337
338     case RTL_EXPR:
339       ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
340       ggc_mark_rtx (RTL_EXPR_RTL (t));
341       return;
342
343     case CALL_EXPR:
344       ggc_mark_tree (TREE_OPERAND (t, 0));
345       ggc_mark_tree (TREE_OPERAND (t, 1));
346       ggc_mark_rtx (CALL_EXPR_RTL (t));
347       return;
348
349     case COMPLEX_CST:
350       ggc_mark_tree (TREE_REALPART (t));
351       ggc_mark_tree (TREE_IMAGPART (t));
352       break;
353
354     case STRING_CST:
355       ggc_mark_string (TREE_STRING_POINTER (t));
356       break;
357
358     case PARM_DECL:
359       ggc_mark_rtx (DECL_INCOMING_RTL (t));
360       break;
361
362     case IDENTIFIER_NODE:
363       ggc_mark_string (IDENTIFIER_POINTER (t));
364       lang_mark_tree (t);
365       return;
366
367     default:
368       break;
369     }
370   
371   /* But in general we can handle them by class.  */
372   switch (TREE_CODE_CLASS (code))
373     {
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));
389       lang_mark_tree (t);
390       break;
391
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));
407       lang_mark_tree (t);
408       break;
409
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));
415       break;
416
417     case 'c': /* A constant.  */
418       ggc_mark_rtx (TREE_CST_RTL (t));
419       break;
420
421     case 'r': case '<': case '1':
422     case '2': case 'e': case 's': /* Expressions.  */
423       {
424         int i = tree_code_length[TREE_CODE (t)];
425         while (--i >= 0)
426           ggc_mark_tree (TREE_OPERAND (t, i));
427         break;
428       }
429
430     case 'x':
431       lang_mark_tree (t);
432       break;
433     }
434 }
435
436 /* Mark all the elements of the varray V, which contains trees.  */
437
438 void
439 ggc_mark_tree_varray (v)
440      varray_type v;
441 {
442   int i;
443
444   if (v)
445     for (i = v->num_elements - 1; i >= 0; --i) 
446       ggc_mark_tree (VARRAY_TREE (v, i));
447 }
448
449 /* Mark the hash table-entry HE.  It's key field is really a tree.  */
450
451 static boolean
452 ggc_mark_tree_hash_table_entry (he, k)
453      struct hash_entry *he;
454      hash_table_key k ATTRIBUTE_UNUSED;
455 {
456   ggc_mark_tree ((tree) he->key);
457   return true;
458 }
459
460 /* Mark all the elements of the hash-table H, which contains trees.  */
461
462 void
463 ggc_mark_tree_hash_table (ht)
464      struct hash_table *ht;
465 {
466   hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
467 }
468
469 /* Allocation wrappers.  */
470
471 char *
472 ggc_alloc_string (contents, length)
473      const char *contents;
474      int length;
475 {
476   char *string;
477
478   if (length < 0)
479     {
480       if (contents == NULL)
481         return NULL;
482       length = strlen (contents);
483     }
484
485   string = (char *) ggc_alloc_obj (length + 1, 0);
486   if (contents != NULL)
487     memcpy (string, contents, length);
488   string[length] = 0;
489
490   return string;
491 }
492
493 /* Print statistics that are independent of the collector in use.  */
494
495 void
496 ggc_print_statistics (stream, stats)
497      FILE *stream;
498      ggc_statistics *stats;
499 {
500   int code;
501
502   /* Set the pointer so that during collection we will actually gather
503      the statistics.  */
504   ggc_stats = stats;
505
506   /* Then do one collection to fill in the statistics.  */
507   ggc_collect ();
508
509   /* Total the statistics.  */
510   for (code = 0; code < MAX_TREE_CODES; ++code)
511     {
512       stats->total_num_trees += stats->num_trees[code];
513       stats->total_size_trees += stats->size_trees[code];
514     }
515   for (code = 0; code < NUM_RTX_CODE; ++code)
516     {
517       stats->total_num_rtxs += stats->num_rtxs[code];
518       stats->total_size_rtxs += stats->size_rtxs[code];
519     }
520
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]) 
526       {
527         fprintf (stream, "%s%*s%-15u %-15u %7.3f\n", 
528                  tree_code_name[code],
529                  22 - strlen (tree_code_name[code]), "",
530                  ggc_stats->num_trees[code],
531                  ggc_stats->size_trees[code],
532                  (100 * ((double) ggc_stats->size_trees[code]) 
533                   / ggc_stats->total_size_trees));
534       }
535   fprintf (stream,
536            "%-22s%-15u %-15u\n", "Total",
537            ggc_stats->total_num_trees,
538            ggc_stats->total_size_trees);
539
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]) 
545       {
546         fprintf (stream, "%s%*s%-15u %-15u %7.3f\n", 
547                  rtx_name[code],
548                  22 - strlen (rtx_name[code]), "",
549                  ggc_stats->num_rtxs[code],
550                  ggc_stats->size_rtxs[code],
551                  (100 * ((double) ggc_stats->size_rtxs[code]) 
552                   / ggc_stats->total_size_rtxs));
553       }
554   fprintf (stream,
555            "%-22s%-15u %-15u\n", "Total",
556            ggc_stats->total_num_rtxs,
557            ggc_stats->total_size_rtxs);
558
559
560   /* Don't gather statistics any more.  */
561   ggc_stats = NULL;
562 }