]>
Commit | Line | Data |
---|---|---|
848205e6 | 1 | /* Virtual array support. |
d9221e01 | 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
2e1eedd6 | 3 | Free Software Foundation, Inc. |
848205e6 MM |
4 | Contributed by Cygnus Solutions. |
5 | ||
1322177d | 6 | This file is part of GCC. |
848205e6 | 7 | |
1322177d | 8 | GCC is free software; you can redistribute it and/or modify it |
848205e6 MM |
9 | under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
1322177d LB |
13 | GCC is distributed in the hope that it will be useful, but WITHOUT |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
15 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
16 | License for more details. | |
848205e6 MM |
17 | |
18 | You should have received a copy of the GNU General Public License | |
1322177d LB |
19 | along with GCC; see the file COPYING. If not, write to the Free |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, | |
21 | MA 02111-1307, USA. */ | |
848205e6 MM |
22 | |
23 | #include "config.h" | |
3d7c1012 | 24 | #include "errors.h" |
848205e6 | 25 | #include "system.h" |
4977bab6 ZW |
26 | #include "coretypes.h" |
27 | #include "tm.h" | |
848205e6 | 28 | #include "varray.h" |
e2500fed | 29 | #include "ggc.h" |
9b57b627 | 30 | #include "hashtab.h" |
848205e6 MM |
31 | |
32 | #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data)) | |
33 | ||
9b57b627 JH |
34 | #ifdef GATHER_STATISTICS |
35 | ||
1f52178b | 36 | /* Store information about each particular varray. */ |
9b57b627 JH |
37 | struct varray_descriptor |
38 | { | |
39 | const char *name; | |
40 | int allocated; | |
41 | int created; | |
42 | int resized; | |
43 | int copied; | |
44 | }; | |
45 | ||
46 | /* Hashtable mapping varray names to descriptors. */ | |
47 | static htab_t varray_hash; | |
48 | ||
49 | /* Hashtable helpers. */ | |
50 | static hashval_t | |
51 | hash_descriptor (const void *p) | |
52 | { | |
53 | const struct varray_descriptor *d = p; | |
54 | return htab_hash_pointer (d->name); | |
55 | } | |
56 | static int | |
57 | eq_descriptor (const void *p1, const void *p2) | |
58 | { | |
59 | const struct varray_descriptor *d = p1; | |
60 | return d->name == p2; | |
61 | } | |
62 | ||
63 | /* For given name, return descriptor, create new if needed. */ | |
64 | static struct varray_descriptor * | |
65 | varray_descriptor (const char *name) | |
66 | { | |
67 | struct varray_descriptor **slot; | |
68 | ||
69 | if (!varray_hash) | |
70 | varray_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL); | |
71 | ||
72 | slot = (struct varray_descriptor **) | |
73 | htab_find_slot_with_hash (varray_hash, name, | |
74 | htab_hash_pointer (name), | |
75 | 1); | |
76 | if (*slot) | |
77 | return *slot; | |
78 | *slot = xcalloc (sizeof (**slot), 1); | |
79 | (*slot)->name = name; | |
80 | return *slot; | |
81 | } | |
82 | #endif | |
83 | ||
b604074c GK |
84 | /* Do not add any more non-GC items here. Please either remove or GC |
85 | those items that are not GCed. */ | |
19a7414e MS |
86 | |
87 | static const struct { | |
88 | unsigned char size; | |
89 | bool uses_ggc; | |
90 | } element[NUM_VARRAY_DATA] = { | |
91 | { sizeof (char), 1 }, | |
92 | { sizeof (unsigned char), 1 }, | |
93 | { sizeof (short), 1 }, | |
94 | { sizeof (unsigned short), 1 }, | |
95 | { sizeof (int), 1 }, | |
96 | { sizeof (unsigned int), 1 }, | |
97 | { sizeof (long), 1 }, | |
98 | { sizeof (unsigned long), 1 }, | |
99 | { sizeof (HOST_WIDE_INT), 1 }, | |
100 | { sizeof (unsigned HOST_WIDE_INT), 1 }, | |
b604074c | 101 | { sizeof (void *), 1 }, |
6de9cd9a | 102 | { sizeof (void *), 0 }, |
19a7414e MS |
103 | { sizeof (char *), 1 }, |
104 | { sizeof (struct rtx_def *), 1 }, | |
105 | { sizeof (struct rtvec_def *), 1 }, | |
106 | { sizeof (union tree_node *), 1 }, | |
107 | { sizeof (struct bitmap_head_def *), 1 }, | |
108 | { sizeof (struct reg_info_def *), 0 }, | |
109 | { sizeof (struct const_equiv_data), 0 }, | |
6de9cd9a | 110 | { sizeof (struct basic_block_def *), 1 }, |
19a7414e | 111 | { sizeof (struct elt_list *), 1 }, |
6de9cd9a DN |
112 | { sizeof (struct edge_def *), 1 }, |
113 | { sizeof (tree *), 1 }, | |
e2500fed GK |
114 | }; |
115 | ||
848205e6 MM |
116 | /* Allocate a virtual array with NUM_ELEMENT elements, each of which is |
117 | ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */ | |
118 | varray_type | |
2e1eedd6 AJ |
119 | varray_init (size_t num_elements, enum varray_data_enum element_kind, |
120 | const char *name) | |
848205e6 | 121 | { |
19a7414e | 122 | size_t data_size = num_elements * element[element_kind].size; |
e2500fed | 123 | varray_type ptr; |
9b57b627 JH |
124 | #ifdef GATHER_STATISTICS |
125 | struct varray_descriptor *desc = varray_descriptor (name); | |
126 | ||
127 | desc->created++; | |
128 | desc->allocated += data_size + VARRAY_HDR_SIZE; | |
129 | #endif | |
19a7414e | 130 | if (element[element_kind].uses_ggc) |
703ad42b | 131 | ptr = ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size); |
e2500fed | 132 | else |
703ad42b | 133 | ptr = xcalloc (VARRAY_HDR_SIZE + data_size, 1); |
848205e6 MM |
134 | |
135 | ptr->num_elements = num_elements; | |
a6873608 | 136 | ptr->elements_used = 0; |
e2500fed | 137 | ptr->type = element_kind; |
a6873608 | 138 | ptr->name = name; |
848205e6 MM |
139 | return ptr; |
140 | } | |
141 | ||
142 | /* Grow/shrink the virtual array VA to N elements. Zero any new elements | |
143 | allocated. */ | |
144 | varray_type | |
2e1eedd6 | 145 | varray_grow (varray_type va, size_t n) |
848205e6 MM |
146 | { |
147 | size_t old_elements = va->num_elements; | |
848205e6 MM |
148 | if (n != old_elements) |
149 | { | |
19a7414e | 150 | size_t elem_size = element[va->type].size; |
e2500fed GK |
151 | size_t old_data_size = old_elements * elem_size; |
152 | size_t data_size = n * elem_size; | |
9b57b627 JH |
153 | #ifdef GATHER_STATISTICS |
154 | struct varray_descriptor *desc = varray_descriptor (va->name); | |
155 | varray_type oldva = va; | |
156 | ||
157 | if (data_size > old_data_size) | |
158 | desc->allocated += data_size - old_data_size; | |
159 | desc->resized ++; | |
160 | #endif | |
161 | ||
e2500fed | 162 | |
19a7414e | 163 | if (element[va->type].uses_ggc) |
703ad42b | 164 | va = ggc_realloc (va, VARRAY_HDR_SIZE + data_size); |
e2500fed | 165 | else |
703ad42b | 166 | va = xrealloc (va, VARRAY_HDR_SIZE + data_size); |
848205e6 MM |
167 | va->num_elements = n; |
168 | if (n > old_elements) | |
961192e1 | 169 | memset (&va->data.c[old_data_size], 0, data_size - old_data_size); |
9b57b627 JH |
170 | #ifdef GATHER_STATISTICS |
171 | if (oldva != va) | |
172 | desc->copied++; | |
173 | #endif | |
848205e6 MM |
174 | } |
175 | ||
176 | return va; | |
177 | } | |
987009bf | 178 | |
e2500fed GK |
179 | /* Reset a varray to its original state. */ |
180 | void | |
2e1eedd6 | 181 | varray_clear (varray_type va) |
e2500fed | 182 | { |
19a7414e | 183 | size_t data_size = element[va->type].size * va->num_elements; |
e2500fed GK |
184 | |
185 | memset (va->data.c, 0, data_size); | |
186 | va->elements_used = 0; | |
187 | } | |
188 | ||
987009bf ZW |
189 | /* Check the bounds of a varray access. */ |
190 | ||
6c9821b7 | 191 | #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007) |
987009bf | 192 | |
987009bf | 193 | void |
2e1eedd6 AJ |
194 | varray_check_failed (varray_type va, size_t n, const char *file, int line, |
195 | const char *function) | |
987009bf | 196 | { |
12a08b40 ZW |
197 | internal_error ("virtual array %s[%lu]: element %lu out of bounds " |
198 | "in %s, at %s:%d", | |
9d99ca5b | 199 | va->name, (unsigned long) va->num_elements, (unsigned long) n, |
11dc5cc6 | 200 | function, trim_filename (file), line); |
987009bf ZW |
201 | } |
202 | ||
12a08b40 ZW |
203 | void |
204 | varray_underflow (varray_type va, const char *file, int line, | |
205 | const char *function) | |
206 | { | |
207 | internal_error ("underflowed virtual array %s in %s, at %s:%d", | |
208 | va->name, function, trim_filename (file), line); | |
209 | } | |
210 | ||
987009bf | 211 | #endif |
9b57b627 | 212 | |
6de9cd9a DN |
213 | |
214 | /* Copy varray V2 into varray V1. Both arrays must be the same size | |
215 | and type. */ | |
216 | ||
217 | void | |
218 | varray_copy (varray_type v1, varray_type v2) | |
219 | { | |
220 | size_t data_size; | |
221 | ||
222 | if (v1->type != v2->type) | |
223 | abort (); | |
224 | ||
225 | if (v1->num_elements != v2->num_elements) | |
226 | abort (); | |
227 | ||
228 | data_size = element[v2->type].size * v2->num_elements; | |
229 | memcpy (v1->data.c, v2->data.c, data_size); | |
230 | v1->elements_used = v2->elements_used; | |
231 | } | |
232 | ||
9b57b627 JH |
233 | /* Output per-varray statistics. */ |
234 | #ifdef GATHER_STATISTICS | |
235 | ||
236 | /* Used to accumulate statistics about varray sizes. */ | |
237 | struct output_info | |
238 | { | |
239 | int count; | |
240 | int size; | |
241 | }; | |
242 | ||
243 | /* Called via htab_traverse. Output varray descriptor pointed out by SLOT | |
244 | and update statistics. */ | |
245 | static int | |
246 | print_statistics (void **slot, void *b) | |
247 | { | |
248 | struct varray_descriptor *d = (struct varray_descriptor *) *slot; | |
249 | struct output_info *i = (struct output_info *) b; | |
250 | ||
251 | if (d->allocated) | |
252 | { | |
253 | fprintf (stderr, "%-21s %6d %10d %7d %7d\n", d->name, | |
254 | d->created, d->allocated, d->resized, d->copied); | |
255 | i->size += d->allocated; | |
256 | i->count += d->created; | |
257 | } | |
258 | return 1; | |
259 | } | |
260 | #endif | |
261 | ||
262 | /* Output per-varray memory usage statistics. */ | |
263 | void dump_varray_statistics (void) | |
264 | { | |
265 | #ifdef GATHER_STATISTICS | |
266 | struct output_info info; | |
267 | ||
268 | fprintf (stderr, "\nVARRAY Kind Count Bytes Resized copied\n"); | |
269 | fprintf (stderr, "-------------------------------------------------------\n"); | |
270 | info.count = 0; | |
271 | info.size = 0; | |
272 | htab_traverse (varray_hash, print_statistics, &info); | |
273 | fprintf (stderr, "-------------------------------------------------------\n"); | |
274 | fprintf (stderr, "%-20s %7d %10d\n", | |
275 | "Total", info.count, info.size); | |
276 | fprintf (stderr, "-------------------------------------------------------\n"); | |
277 | #endif | |
278 | } |