Avoid unnecessary dependencies on COND_EXEC insns.
[official-gcc.git] / gcc / ggc-simple.c
blobb5cab1bcb23b355f91df98199523ae5818354e51
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 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
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.
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 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "tm_p.h"
26 #include "flags.h"
27 #include "varray.h"
28 #include "ggc.h"
29 #include "timevar.h"
31 /* Debugging flags. */
33 /* Zap memory before freeing to catch dangling pointers. */
34 #define GGC_POISON
36 /* Collect statistics on how bushy the search tree is. */
37 #undef GGC_BALANCE
39 /* Perform collection every time ggc_collect is invoked. Otherwise,
40 collection is performed only when a significant amount of memory
41 has been allocated since the last collection. */
42 #undef GGC_ALWAYS_COLLECT
44 /* Always verify that the to-be-marked memory is collectable. */
45 #undef GGC_ALWAYS_VERIFY
47 #ifdef ENABLE_GC_CHECKING
48 #define GGC_POISON
49 #define GGC_ALWAYS_VERIFY
50 #endif
51 #ifdef ENABLE_GC_ALWAYS_COLLECT
52 #define GGC_ALWAYS_COLLECT
53 #endif
55 #ifndef HOST_BITS_PER_PTR
56 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
57 #endif
59 /* We'd like a balanced tree, but we don't really want to pay for the
60 cost of keeping the tree balanced. We'll settle for the next best
61 thing -- nearly balanced.
63 In this context, the most natural key is the node pointer itself,
64 but due to the way memory managers work, we'd be virtually certain
65 to wind up with a completely degenerate straight line. What's needed
66 is to make something more variable, and yet predictable, be more
67 significant in the comparison.
69 The handiest source of variability is the low bits of the pointer
70 value itself. Any sort of bit/byte swap would do, but such machine
71 specific operations are not handy, and we don't want to put that much
72 effort into it. */
74 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
75 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
76 | (size_t)p >> 16)
78 /* GC'able memory; a node in a binary search tree. */
80 struct ggc_mem
82 /* A combination of the standard left/right nodes, indexable by `<'. */
83 struct ggc_mem *sub[2];
85 unsigned int mark : 1;
86 unsigned int context : 7;
87 unsigned int size : 24;
89 /* Make sure the data is reasonably aligned. */
90 union {
91 HOST_WIDEST_INT i;
92 #ifdef HAVE_LONG_DOUBLE
93 long double d;
94 #else
95 double d;
96 #endif
97 } u;
100 static struct globals
102 /* Root of the object tree. */
103 struct ggc_mem *root;
105 /* Data bytes currently allocated. */
106 size_t allocated;
108 /* Data objects currently allocated. */
109 size_t objects;
111 /* Data bytes allocated at time of last GC. */
112 size_t allocated_last_gc;
114 /* Current context level. */
115 int context;
116 } G;
118 /* Skip garbage collection if the current allocation is not at least
119 this factor times the allocation at the end of the last collection.
120 In other words, total allocation must expand by (this factor minus
121 one) before collection is performed. */
122 #define GGC_MIN_EXPAND_FOR_GC (1.3)
124 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
125 test from triggering too often when the heap is small. */
126 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
128 /* Local function prototypes. */
130 static void tree_insert PARAMS ((struct ggc_mem *));
131 static int tree_lookup PARAMS ((struct ggc_mem *));
132 static void clear_marks PARAMS ((struct ggc_mem *));
133 static void sweep_objs PARAMS ((struct ggc_mem **));
134 static void ggc_pop_context_1 PARAMS ((struct ggc_mem *, int));
136 /* For use from debugger. */
137 extern void debug_ggc_tree PARAMS ((struct ggc_mem *, int));
139 #ifdef GGC_BALANCE
140 extern void debug_ggc_balance PARAMS ((void));
141 #endif
142 static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
144 /* Insert V into the search tree. */
146 static inline void
147 tree_insert (v)
148 struct ggc_mem *v;
150 size_t v_key = PTR_KEY (v);
151 struct ggc_mem *p, **pp;
153 for (pp = &G.root, p = *pp; p ; p = *pp)
155 size_t p_key = PTR_KEY (p);
156 pp = &p->sub[v_key < p_key];
158 *pp = v;
161 /* Return true if V is in the tree. */
163 static inline int
164 tree_lookup (v)
165 struct ggc_mem *v;
167 size_t v_key = PTR_KEY (v);
168 struct ggc_mem *p = G.root;
170 while (p)
172 size_t p_key = PTR_KEY (p);
173 if (p == v)
174 return 1;
175 p = p->sub[v_key < p_key];
178 return 0;
181 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
183 void *
184 ggc_alloc (size)
185 size_t size;
187 struct ggc_mem *x;
189 x = (struct ggc_mem *) xmalloc (offsetof (struct ggc_mem, u) + size);
190 x->sub[0] = NULL;
191 x->sub[1] = NULL;
192 x->mark = 0;
193 x->context = G.context;
194 x->size = size;
196 #ifdef GGC_POISON
197 memset (&x->u, 0xaf, size);
198 #endif
200 tree_insert (x);
201 G.allocated += size;
202 G.objects += 1;
204 return &x->u;
207 /* Mark a node. */
210 ggc_set_mark (p)
211 const void *p;
213 struct ggc_mem *x;
215 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
216 #ifdef GGC_ALWAYS_VERIFY
217 if (! tree_lookup (x))
218 abort ();
219 #endif
221 if (x->mark)
222 return 1;
224 x->mark = 1;
225 G.allocated += x->size;
226 G.objects += 1;
228 return 0;
231 /* Mark a node, but check first to see that it's really gc-able memory. */
233 void
234 ggc_mark_if_gcable (p)
235 const void *p;
237 struct ggc_mem *x;
239 if (p == NULL)
240 return;
242 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
243 if (! tree_lookup (x))
244 return;
246 if (x->mark)
247 return;
249 x->mark = 1;
250 G.allocated += x->size;
251 G.objects += 1;
254 /* Return the size of the gc-able object P. */
256 size_t
257 ggc_get_size (p)
258 const void *p;
260 struct ggc_mem *x
261 = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
262 return x->size;
265 /* Unmark all objects. */
267 static void
268 clear_marks (x)
269 struct ggc_mem *x;
271 x->mark = 0;
272 if (x->sub[0])
273 clear_marks (x->sub[0]);
274 if (x->sub[1])
275 clear_marks (x->sub[1]);
278 /* Free all objects in the current context that are not marked. */
280 static void
281 sweep_objs (root)
282 struct ggc_mem **root;
284 struct ggc_mem *x = *root;
285 if (!x)
286 return;
288 sweep_objs (&x->sub[0]);
289 sweep_objs (&x->sub[1]);
291 if (! x->mark && x->context >= G.context)
293 struct ggc_mem *l, *r;
295 l = x->sub[0];
296 r = x->sub[1];
297 if (!l)
298 *root = r;
299 else if (!r)
300 *root = l;
301 else if (!l->sub[1])
303 *root = l;
304 l->sub[1] = r;
306 else if (!r->sub[0])
308 *root = r;
309 r->sub[0] = l;
311 else
313 *root = l;
314 do {
315 root = &l->sub[1];
316 } while ((l = *root) != NULL);
317 *root = r;
320 #ifdef GGC_POISON
321 memset (&x->u, 0xA5, x->size);
322 #endif
324 free (x);
328 /* The top level mark-and-sweep routine. */
330 void
331 ggc_collect ()
333 #ifndef GGC_ALWAYS_COLLECT
334 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
335 return;
336 #endif
338 #ifdef GGC_BALANCE
339 debug_ggc_balance ();
340 #endif
342 timevar_push (TV_GC);
343 if (!quiet_flag)
344 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
346 G.allocated = 0;
347 G.objects = 0;
349 clear_marks (G.root);
350 ggc_mark_roots ();
351 sweep_objs (&G.root);
353 G.allocated_last_gc = G.allocated;
354 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
355 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
357 timevar_pop (TV_GC);
359 if (!quiet_flag)
360 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
362 #ifdef GGC_BALANCE
363 debug_ggc_balance ();
364 #endif
367 /* Called once to initialize the garbage collector. */
369 void
370 init_ggc ()
372 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
375 /* Start a new GGC context. Memory allocated in previous contexts
376 will not be collected while the new context is active. */
378 void
379 ggc_push_context ()
381 G.context++;
383 /* We only allocated 7 bits in the node for the context. This
384 should be more than enough. */
385 if (G.context >= 128)
386 abort ();
389 /* Finish a GC context. Any uncollected memory in the new context
390 will be merged with the old context. */
392 void
393 ggc_pop_context ()
395 G.context--;
396 if (G.root)
397 ggc_pop_context_1 (G.root, G.context);
400 static void
401 ggc_pop_context_1 (x, c)
402 struct ggc_mem *x;
403 int c;
405 if (x->context > c)
406 x->context = c;
407 if (x->sub[0])
408 ggc_pop_context_1 (x->sub[0], c);
409 if (x->sub[1])
410 ggc_pop_context_1 (x->sub[1], c);
413 /* Dump a tree. */
415 void
416 debug_ggc_tree (p, indent)
417 struct ggc_mem *p;
418 int indent;
420 int i;
422 if (!p)
424 fputs ("(nil)\n", stderr);
425 return;
428 if (p->sub[0])
429 debug_ggc_tree (p->sub[0], indent + 1);
431 for (i = 0; i < indent; ++i)
432 putc (' ', stderr);
433 fprintf (stderr, "%lx %p\n", (unsigned long)PTR_KEY (p), p);
435 if (p->sub[1])
436 debug_ggc_tree (p->sub[1], indent + 1);
439 #ifdef GGC_BALANCE
440 /* Collect tree balance metrics */
442 #include <math.h>
444 void
445 debug_ggc_balance ()
447 size_t nleaf, sumdepth;
449 nleaf = sumdepth = 0;
450 tally_leaves (G.root, 0, &nleaf, &sumdepth);
452 fprintf (stderr, " {B %.2f,%.1f,%.1f}",
453 /* In a balanced tree, leaf/node should approach 1/2. */
454 (float)nleaf / (float)G.objects,
455 /* In a balanced tree, average leaf depth should approach lg(n). */
456 (float)sumdepth / (float)nleaf,
457 log ((double) G.objects) / M_LN2);
459 #endif
461 /* Used by debug_ggc_balance, and also by ggc_print_statistics. */
462 static void
463 tally_leaves (x, depth, nleaf, sumdepth)
464 struct ggc_mem *x;
465 int depth;
466 size_t *nleaf;
467 size_t *sumdepth;
469 if (! x->sub[0] && !x->sub[1])
471 *nleaf += 1;
472 *sumdepth += depth;
474 else
476 if (x->sub[0])
477 tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
478 if (x->sub[1])
479 tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);
483 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
484 ? (x) \
485 : ((x) < 1024*1024*10 \
486 ? (x) / 1024 \
487 : (x) / (1024*1024))))
488 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
490 /* Report on GC memory usage. */
491 void
492 ggc_print_statistics ()
494 struct ggc_statistics stats;
495 size_t nleaf = 0, sumdepth = 0;
497 /* Clear the statistics. */
498 memset (&stats, 0, sizeof (stats));
500 /* Make sure collection will really occur. */
501 G.allocated_last_gc = 0;
503 /* Collect and print the statistics common across collectors. */
504 ggc_print_common_statistics (stderr, &stats);
506 /* Report on tree balancing. */
507 tally_leaves (G.root, 0, &nleaf, &sumdepth);
509 fprintf (stderr, "\n\
510 Total internal data (bytes)\t%ld%c\n\
511 Number of leaves in tree\t%d\n\
512 Average leaf depth\t\t%.1f\n",
513 SCALE(G.objects * offsetof (struct ggc_mem, u)),
514 LABEL(G.objects * offsetof (struct ggc_mem, u)),
515 nleaf, (double)sumdepth / (double)nleaf);
517 /* Report overall memory usage. */
518 fprintf (stderr, "\n\
519 Total objects allocated\t\t%d\n\
520 Total memory in GC arena\t%ld%c\n",
521 G.objects,
522 SCALE(G.allocated), LABEL(G.allocated));