* ggc-common.c (ggc_mark_roots): Don't iterate NULL hash tables.
[gcc.git] / gcc / ggc-common.c
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 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 GCC; 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 "hashtab.h"
30 #include "varray.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33
34 /* Statistics about the allocation. */
35 static ggc_statistics *ggc_stats;
36
37 static void ggc_mark_rtx_children_1 PARAMS ((rtx));
38 static int ggc_htab_delete PARAMS ((void **, void *));
39
40 /* Maintain global roots that are preserved during GC. */
41
42 /* Global roots that are preserved during calls to gc. */
43
44 struct ggc_root
45 {
46 struct ggc_root *next;
47 void *base;
48 int nelt;
49 int size;
50 void (*cb) PARAMS ((void *));
51 };
52
53 static struct ggc_root *roots;
54
55 /* Add BASE as a new garbage collection root. It is an array of
56 length NELT with each element SIZE bytes long. CB is a
57 function that will be called with a pointer to each element
58 of the array; it is the intention that CB call the appropriate
59 routine to mark gc-able memory for that element. */
60
61 void
62 ggc_add_root (base, nelt, size, cb)
63 void *base;
64 int nelt, size;
65 void (*cb) PARAMS ((void *));
66 {
67 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
68
69 x->next = roots;
70 x->base = base;
71 x->nelt = nelt;
72 x->size = size;
73 x->cb = cb;
74
75 roots = x;
76 }
77
78 /* Process a slot of an htab by deleting it if it has not been marked. */
79
80 static int
81 ggc_htab_delete (slot, info)
82 void **slot;
83 void *info;
84 {
85 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info;
86
87 if (! (*r->marked_p) (*slot))
88 htab_clear_slot (*r->base, slot);
89 else
90 (*r->cb) (*slot);
91
92 return 1;
93 }
94
95 /* Iterate through all registered roots and mark each element. */
96
97 void
98 ggc_mark_roots ()
99 {
100 struct ggc_root *x;
101 const struct ggc_root_tab *const *rt;
102 const struct ggc_root_tab *rti;
103 const struct ggc_cache_tab *const *ct;
104 const struct ggc_cache_tab *cti;
105 size_t i;
106
107 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
108 for (rti = *rt; rti->base != NULL; rti++)
109 memset (rti->base, 0, rti->stride);
110
111 for (rt = gt_ggc_rtab; *rt; rt++)
112 for (rti = *rt; rti->base != NULL; rti++)
113 for (i = 0; i < rti->nelt; i++)
114 (*rti->cb)(*(void **)((char *)rti->base + rti->stride * i));
115
116 for (x = roots; x != NULL; x = x->next)
117 {
118 char *elt = x->base;
119 int s = x->size, n = x->nelt;
120 void (*cb) PARAMS ((void *)) = x->cb;
121 int i;
122
123 for (i = 0; i < n; ++i, elt += s)
124 (*cb)(elt);
125 }
126
127 /* Now scan all hash tables that have objects which are to be deleted if
128 they are not already marked. */
129 for (ct = gt_ggc_cache_rtab; *ct; ct++)
130 for (cti = *ct; cti->base != NULL; cti++)
131 if (*cti->base)
132 htab_traverse (*cti->base, ggc_htab_delete, (PTR) cti);
133 }
134
135 /* R had not been previously marked, but has now been marked via
136 ggc_set_mark. Now recurse and process the children. */
137
138 void
139 ggc_mark_rtx_children (r)
140 rtx r;
141 {
142 rtx i, last;
143
144 /* Special case the instruction chain. This is a data structure whose
145 chain length is potentially unbounded, and which contain references
146 within the chain (e.g. label_ref and insn_list). If do nothing here,
147 we risk blowing the stack recursing through a long chain of insns.
148
149 Combat this by marking all of the instructions in the chain before
150 marking the contents of those instructions. */
151
152 switch (GET_CODE (r))
153 {
154 case INSN:
155 case JUMP_INSN:
156 case CALL_INSN:
157 case NOTE:
158 case CODE_LABEL:
159 case BARRIER:
160 for (i = NEXT_INSN (r); ; i = NEXT_INSN (i))
161 if (! ggc_test_and_set_mark (i))
162 break;
163 last = i;
164
165 for (i = NEXT_INSN (r); i != last; i = NEXT_INSN (i))
166 ggc_mark_rtx_children_1 (i);
167
168 default:
169 break;
170 }
171
172 ggc_mark_rtx_children_1 (r);
173 }
174
175 static void
176 ggc_mark_rtx_children_1 (r)
177 rtx r;
178 {
179 const char *fmt;
180 int i;
181 rtx next_rtx;
182
183 do
184 {
185 enum rtx_code code = GET_CODE (r);
186 /* This gets set to a child rtx to eliminate tail recursion. */
187 next_rtx = NULL;
188
189 /* Collect statistics, if appropriate. */
190 if (ggc_stats)
191 {
192 ++ggc_stats->num_rtxs[(int) code];
193 ggc_stats->size_rtxs[(int) code] += ggc_get_size (r);
194 }
195
196 /* ??? If (some of) these are really pass-dependent info, do we
197 have any right poking our noses in? */
198 switch (code)
199 {
200 case MEM:
201 gt_ggc_m_mem_attrs (MEM_ATTRS (r));
202 break;
203 case JUMP_INSN:
204 ggc_mark_rtx (JUMP_LABEL (r));
205 break;
206 case CODE_LABEL:
207 ggc_mark_rtx (LABEL_REFS (r));
208 break;
209 case LABEL_REF:
210 ggc_mark_rtx (LABEL_NEXTREF (r));
211 ggc_mark_rtx (CONTAINING_INSN (r));
212 break;
213 case ADDRESSOF:
214 ggc_mark_tree (ADDRESSOF_DECL (r));
215 break;
216 case NOTE:
217 switch (NOTE_LINE_NUMBER (r))
218 {
219 case NOTE_INSN_EXPECTED_VALUE:
220 ggc_mark_rtx (NOTE_EXPECTED_VALUE (r));
221 break;
222
223 case NOTE_INSN_BLOCK_BEG:
224 case NOTE_INSN_BLOCK_END:
225 ggc_mark_tree (NOTE_BLOCK (r));
226 break;
227
228 default:
229 break;
230 }
231 break;
232
233 default:
234 break;
235 }
236
237 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
238 {
239 rtx exp;
240 switch (*fmt)
241 {
242 case 'e': case 'u':
243 exp = XEXP (r, i);
244 if (ggc_test_and_set_mark (exp))
245 {
246 if (next_rtx == NULL)
247 next_rtx = exp;
248 else
249 ggc_mark_rtx_children (exp);
250 }
251 break;
252 case 'V': case 'E':
253 gt_ggc_m_rtvec_def (XVEC (r, i));
254 break;
255 }
256 }
257 }
258 while ((r = next_rtx) != NULL);
259 }
260
261 /* Various adaptor functions. */
262 void
263 gt_ggc_mx_rtx_def (x)
264 void *x;
265 {
266 ggc_mark_rtx((rtx)x);
267 }
268
269 /* Allocate a block of memory, then clear it. */
270 void *
271 ggc_alloc_cleared (size)
272 size_t size;
273 {
274 void *buf = ggc_alloc (size);
275 memset (buf, 0, size);
276 return buf;
277 }
278
279 /* Resize a block of memory, possibly re-allocating it. */
280 void *
281 ggc_realloc (x, size)
282 void *x;
283 size_t size;
284 {
285 void *r;
286 size_t old_size;
287
288 if (x == NULL)
289 return ggc_alloc (size);
290
291 old_size = ggc_get_size (x);
292 if (size <= old_size)
293 return x;
294
295 r = ggc_alloc (size);
296 memcpy (r, x, old_size);
297 return r;
298 }
299
300 /* Like ggc_alloc_cleared, but performs a multiplication. */
301 void *
302 ggc_calloc (s1, s2)
303 size_t s1, s2;
304 {
305 return ggc_alloc_cleared (s1 * s2);
306 }
307
308 /* Print statistics that are independent of the collector in use. */
309 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
310 ? (x) \
311 : ((x) < 1024*1024*10 \
312 ? (x) / 1024 \
313 : (x) / (1024*1024))))
314 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
315
316 void
317 ggc_print_common_statistics (stream, stats)
318 FILE *stream;
319 ggc_statistics *stats;
320 {
321 int code;
322
323 /* Set the pointer so that during collection we will actually gather
324 the statistics. */
325 ggc_stats = stats;
326
327 /* Then do one collection to fill in the statistics. */
328 ggc_collect ();
329
330 /* Total the statistics. */
331 for (code = 0; code < MAX_TREE_CODES; ++code)
332 {
333 stats->total_num_trees += stats->num_trees[code];
334 stats->total_size_trees += stats->size_trees[code];
335 }
336 for (code = 0; code < NUM_RTX_CODE; ++code)
337 {
338 stats->total_num_rtxs += stats->num_rtxs[code];
339 stats->total_size_rtxs += stats->size_rtxs[code];
340 }
341
342 /* Print the statistics for trees. */
343 fprintf (stream, "\n%-17s%10s %16s %10s\n", "Tree",
344 "Number", "Bytes", "% Total");
345 for (code = 0; code < MAX_TREE_CODES; ++code)
346 if (ggc_stats->num_trees[code])
347 {
348 fprintf (stream, "%-17s%10u%16ld%c %10.3f\n",
349 tree_code_name[code],
350 ggc_stats->num_trees[code],
351 SCALE (ggc_stats->size_trees[code]),
352 LABEL (ggc_stats->size_trees[code]),
353 (100 * ((double) ggc_stats->size_trees[code])
354 / ggc_stats->total_size_trees));
355 }
356 fprintf (stream,
357 "%-17s%10u%16ld%c\n", "Total",
358 ggc_stats->total_num_trees,
359 SCALE (ggc_stats->total_size_trees),
360 LABEL (ggc_stats->total_size_trees));
361
362 /* Print the statistics for RTL. */
363 fprintf (stream, "\n%-17s%10s %16s %10s\n", "RTX",
364 "Number", "Bytes", "% Total");
365 for (code = 0; code < NUM_RTX_CODE; ++code)
366 if (ggc_stats->num_rtxs[code])
367 {
368 fprintf (stream, "%-17s%10u%16ld%c %10.3f\n",
369 rtx_name[code],
370 ggc_stats->num_rtxs[code],
371 SCALE (ggc_stats->size_rtxs[code]),
372 LABEL (ggc_stats->size_rtxs[code]),
373 (100 * ((double) ggc_stats->size_rtxs[code])
374 / ggc_stats->total_size_rtxs));
375 }
376 fprintf (stream,
377 "%-17s%10u%16ld%c\n", "Total",
378 ggc_stats->total_num_rtxs,
379 SCALE (ggc_stats->total_size_rtxs),
380 LABEL (ggc_stats->total_size_rtxs));
381
382 /* Don't gather statistics any more. */
383 ggc_stats = NULL;
384 }