blob: 086d775d599035428e26f0eea98c9096f74db856 [file] [log] [blame]
Neil Booth2a967f32001-05-20 06:26:45 +00001/* Hash tables.
Jakub Jelinek748086b2009-04-09 17:00:19 +02002 Copyright (C) 2000, 2001, 2003, 2004, 2008, 2009
3 Free Software Foundation, Inc.
Neil Booth2a967f32001-05-20 06:26:45 +00004
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +02007Free Software Foundation; either version 3, or (at your option) any
Neil Booth2a967f32001-05-20 06:26:45 +00008later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020016along with this program; see the file COPYING3. If not see
17<http://www.gnu.org/licenses/>.
Neil Booth2a967f32001-05-20 06:26:45 +000018
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
22
23#include "config.h"
24#include "system.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000025#include "symtab.h"
Neil Booth2a967f32001-05-20 06:26:45 +000026
27/* The code below is a specialization of Vladimir Makarov's expandable
28 hash tables (see libiberty/hashtab.c). The abstraction penalty was
29 too high to continue using the generic form. This code knows
30 intrinsically how to calculate a hash value, and how to compare an
Tom Tromeydae41742008-05-21 15:00:59 +000031 existing entry with a potential new one. */
Neil Booth2a967f32001-05-20 06:26:45 +000032
Roger Sayle7bb3fbb2003-08-08 20:23:06 +000033static unsigned int calc_hash (const unsigned char *, size_t);
Andreas Jaeger1d088de2003-07-06 08:15:36 +020034static void ht_expand (hash_table *);
Zack Weinberga2f7be92003-07-22 16:24:53 +000035static double approx_sqrt (double);
Neil Booth2a967f32001-05-20 06:26:45 +000036
Tom Tromeydae41742008-05-21 15:00:59 +000037/* A deleted entry. */
38#define DELETED ((hashnode) -1)
39
Neil Booth2a967f32001-05-20 06:26:45 +000040/* Calculate the hash of the string STR of length LEN. */
41
42static unsigned int
Roger Sayle7bb3fbb2003-08-08 20:23:06 +000043calc_hash (const unsigned char *str, size_t len)
Neil Booth2a967f32001-05-20 06:26:45 +000044{
Roger Sayle7bb3fbb2003-08-08 20:23:06 +000045 size_t n = len;
Neil Booth2a967f32001-05-20 06:26:45 +000046 unsigned int r = 0;
Neil Booth2a967f32001-05-20 06:26:45 +000047
48 while (n--)
Zack Weinbergc6e83802004-06-05 20:58:06 +000049 r = HT_HASHSTEP (r, *str++);
Neil Booth2a967f32001-05-20 06:26:45 +000050
Zack Weinbergc6e83802004-06-05 20:58:06 +000051 return HT_HASHFINISH (r, len);
Neil Booth2a967f32001-05-20 06:26:45 +000052}
53
54/* Initialize an identifier hashtable. */
55
56hash_table *
Andreas Jaeger1d088de2003-07-06 08:15:36 +020057ht_create (unsigned int order)
Neil Booth2a967f32001-05-20 06:26:45 +000058{
59 unsigned int nslots = 1 << order;
60 hash_table *table;
61
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000062 table = XCNEW (hash_table);
Neil Booth2a967f32001-05-20 06:26:45 +000063
64 /* Strings need no alignment. */
Zack Weinberg43839642003-07-13 17:34:18 +000065 _obstack_begin (&table->stack, 0, 0,
66 (void *(*) (long)) xmalloc,
67 (void (*) (void *)) free);
68
Neil Booth2a967f32001-05-20 06:26:45 +000069 obstack_alignment_mask (&table->stack) = 0;
70
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000071 table->entries = XCNEWVEC (hashnode, nslots);
Geoffrey Keatingb453c952004-05-30 00:49:06 +000072 table->entries_owned = true;
Neil Booth2a967f32001-05-20 06:26:45 +000073 table->nslots = nslots;
74 return table;
75}
76
Neil Boothbef985f2001-08-11 12:37:19 +000077/* Frees all memory associated with a hash table. */
78
79void
Andreas Jaeger1d088de2003-07-06 08:15:36 +020080ht_destroy (hash_table *table)
Neil Boothbef985f2001-08-11 12:37:19 +000081{
82 obstack_free (&table->stack, NULL);
Geoffrey Keatingb453c952004-05-30 00:49:06 +000083 if (table->entries_owned)
84 free (table->entries);
Neil Boothbef985f2001-08-11 12:37:19 +000085 free (table);
86}
87
Neil Booth2a967f32001-05-20 06:26:45 +000088/* Returns the hash entry for the a STR of length LEN. If that string
Tom Tromeydae41742008-05-21 15:00:59 +000089 already exists in the table, returns the existing entry. If the
Neil Booth2a967f32001-05-20 06:26:45 +000090 identifier hasn't been seen before, and INSERT is CPP_NO_INSERT,
91 returns NULL. Otherwise insert and returns a new entry. A new
Tom Tromeydae41742008-05-21 15:00:59 +000092 string is allocated. */
Neil Booth2a967f32001-05-20 06:26:45 +000093hashnode
Roger Sayle7bb3fbb2003-08-08 20:23:06 +000094ht_lookup (hash_table *table, const unsigned char *str, size_t len,
Andreas Jaeger1d088de2003-07-06 08:15:36 +020095 enum ht_lookup_option insert)
Neil Booth2a967f32001-05-20 06:26:45 +000096{
Zack Weinbergc6e83802004-06-05 20:58:06 +000097 return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
98 insert);
99}
100
101hashnode
102ht_lookup_with_hash (hash_table *table, const unsigned char *str,
103 size_t len, unsigned int hash,
104 enum ht_lookup_option insert)
105{
Neil Booth2a967f32001-05-20 06:26:45 +0000106 unsigned int hash2;
107 unsigned int index;
Tom Tromeydae41742008-05-21 15:00:59 +0000108 unsigned int deleted_index = table->nslots;
Neil Booth2a967f32001-05-20 06:26:45 +0000109 size_t sizemask;
110 hashnode node;
111
112 sizemask = table->nslots - 1;
113 index = hash & sizemask;
Neil Booth2a967f32001-05-20 06:26:45 +0000114 table->searches++;
115
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000116 node = table->entries[index];
Tom Tromeydae41742008-05-21 15:00:59 +0000117
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000118 if (node != NULL)
Neil Booth2a967f32001-05-20 06:26:45 +0000119 {
Tom Tromeydae41742008-05-21 15:00:59 +0000120 if (node == DELETED)
121 deleted_index = index;
122 else if (node->hash_value == hash
123 && HT_LEN (node) == (unsigned int) len
124 && !memcmp (HT_STR (node), str, len))
125 return node;
Neil Booth2a967f32001-05-20 06:26:45 +0000126
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000127 /* hash2 must be odd, so we're guaranteed to visit every possible
128 location in the table during rehashing. */
129 hash2 = ((hash * 17) & sizemask) | 1;
130
131 for (;;)
132 {
133 table->collisions++;
134 index = (index + hash2) & sizemask;
135 node = table->entries[index];
136 if (node == NULL)
137 break;
138
Tom Tromeydae41742008-05-21 15:00:59 +0000139 if (node == DELETED)
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000140 {
Tom Tromeydae41742008-05-21 15:00:59 +0000141 if (deleted_index != table->nslots)
142 deleted_index = index;
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000143 }
Tom Tromeydae41742008-05-21 15:00:59 +0000144 else if (node->hash_value == hash
145 && HT_LEN (node) == (unsigned int) len
146 && !memcmp (HT_STR (node), str, len))
147 return node;
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000148 }
Neil Booth2a967f32001-05-20 06:26:45 +0000149 }
150
151 if (insert == HT_NO_INSERT)
152 return NULL;
153
Tom Tromeydae41742008-05-21 15:00:59 +0000154 /* We prefer to overwrite the first deleted slot we saw. */
155 if (deleted_index != table->nslots)
156 index = deleted_index;
157
Neil Booth2a967f32001-05-20 06:26:45 +0000158 node = (*table->alloc_node) (table);
159 table->entries[index] = node;
160
Roger Sayle7bb3fbb2003-08-08 20:23:06 +0000161 HT_LEN (node) = (unsigned int) len;
Gabriel Dos Reis5e0c54e2003-05-18 13:40:54 +0000162 node->hash_value = hash;
Tom Tromeydae41742008-05-21 15:00:59 +0000163
164 if (table->alloc_subobject)
165 {
166 char *chars = table->alloc_subobject (len + 1);
167 memcpy (chars, str, len);
168 chars[len] = '\0';
169 HT_STR (node) = (const unsigned char *) chars;
170 }
Neil Booth2a967f32001-05-20 06:26:45 +0000171 else
Tom Tromeydae41742008-05-21 15:00:59 +0000172 HT_STR (node) = (const unsigned char *) obstack_copy0 (&table->stack,
173 str, len);
Neil Booth2a967f32001-05-20 06:26:45 +0000174
175 if (++table->nelements * 4 >= table->nslots * 3)
176 /* Must expand the string table. */
177 ht_expand (table);
178
179 return node;
180}
181
182/* Double the size of a hash table, re-hashing existing entries. */
183
184static void
Andreas Jaeger1d088de2003-07-06 08:15:36 +0200185ht_expand (hash_table *table)
Neil Booth2a967f32001-05-20 06:26:45 +0000186{
187 hashnode *nentries, *p, *limit;
188 unsigned int size, sizemask;
189
190 size = table->nslots * 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000191 nentries = XCNEWVEC (hashnode, size);
Neil Booth2a967f32001-05-20 06:26:45 +0000192 sizemask = size - 1;
193
194 p = table->entries;
195 limit = p + table->nslots;
196 do
Tom Tromeydae41742008-05-21 15:00:59 +0000197 if (*p && *p != DELETED)
Neil Booth2a967f32001-05-20 06:26:45 +0000198 {
199 unsigned int index, hash, hash2;
200
Gabriel Dos Reis5e0c54e2003-05-18 13:40:54 +0000201 hash = (*p)->hash_value;
Neil Booth2a967f32001-05-20 06:26:45 +0000202 index = hash & sizemask;
203
Roger Sayle4ae2e3e2003-08-22 22:29:17 +0000204 if (nentries[index])
Neil Booth2a967f32001-05-20 06:26:45 +0000205 {
Roger Sayle4ae2e3e2003-08-22 22:29:17 +0000206 hash2 = ((hash * 17) & sizemask) | 1;
207 do
Neil Booth2a967f32001-05-20 06:26:45 +0000208 {
Roger Sayle4ae2e3e2003-08-22 22:29:17 +0000209 index = (index + hash2) & sizemask;
Neil Booth2a967f32001-05-20 06:26:45 +0000210 }
Roger Sayle4ae2e3e2003-08-22 22:29:17 +0000211 while (nentries[index]);
Neil Booth2a967f32001-05-20 06:26:45 +0000212 }
Roger Sayle4ae2e3e2003-08-22 22:29:17 +0000213 nentries[index] = *p;
Neil Booth2a967f32001-05-20 06:26:45 +0000214 }
215 while (++p < limit);
216
Geoffrey Keatingb453c952004-05-30 00:49:06 +0000217 if (table->entries_owned)
218 free (table->entries);
219 table->entries_owned = true;
Neil Booth2a967f32001-05-20 06:26:45 +0000220 table->entries = nentries;
221 table->nslots = size;
222}
223
224/* For all nodes in TABLE, callback CB with parameters TABLE->PFILE,
225 the node, and V. */
226void
Andreas Jaeger1d088de2003-07-06 08:15:36 +0200227ht_forall (hash_table *table, ht_cb cb, const void *v)
Neil Booth2a967f32001-05-20 06:26:45 +0000228{
229 hashnode *p, *limit;
230
231 p = table->entries;
232 limit = p + table->nslots;
233 do
Tom Tromeydae41742008-05-21 15:00:59 +0000234 if (*p && *p != DELETED)
Neil Booth2a967f32001-05-20 06:26:45 +0000235 {
236 if ((*cb) (table->pfile, *p, v) == 0)
237 break;
238 }
239 while (++p < limit);
240}
241
Tom Tromeydae41742008-05-21 15:00:59 +0000242/* Like ht_forall, but a nonzero return from the callback means that
243 the entry should be removed from the table. */
244void
245ht_purge (hash_table *table, ht_cb cb, const void *v)
246{
247 hashnode *p, *limit;
248
249 p = table->entries;
250 limit = p + table->nslots;
251 do
252 if (*p && *p != DELETED)
253 {
254 if ((*cb) (table->pfile, *p, v))
255 *p = DELETED;
256 }
257 while (++p < limit);
258}
259
Geoffrey Keatingb453c952004-05-30 00:49:06 +0000260/* Restore the hash table. */
261void
262ht_load (hash_table *ht, hashnode *entries,
263 unsigned int nslots, unsigned int nelements,
264 bool own)
265{
266 if (ht->entries_owned)
267 free (ht->entries);
268 ht->entries = entries;
269 ht->nslots = nslots;
270 ht->nelements = nelements;
271 ht->entries_owned = own;
272}
273
Neil Booth2a967f32001-05-20 06:26:45 +0000274/* Dump allocation statistics to stderr. */
275
276void
Andreas Jaeger1d088de2003-07-06 08:15:36 +0200277ht_dump_statistics (hash_table *table)
Neil Booth2a967f32001-05-20 06:26:45 +0000278{
279 size_t nelts, nids, overhead, headers;
Tom Tromeydae41742008-05-21 15:00:59 +0000280 size_t total_bytes, longest, deleted = 0;
Serge Belyshev0fd9e8d2004-09-06 13:22:48 +0000281 double sum_of_squares, exp_len, exp_len2, exp2_len;
Neil Booth2a967f32001-05-20 06:26:45 +0000282 hashnode *p, *limit;
283
284#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
285 ? (x) \
286 : ((x) < 1024*1024*10 \
287 ? (x) / 1024 \
288 : (x) / (1024*1024))))
289#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
290
291 total_bytes = longest = sum_of_squares = nids = 0;
292 p = table->entries;
293 limit = p + table->nslots;
294 do
Tom Tromeydae41742008-05-21 15:00:59 +0000295 if (*p == DELETED)
296 ++deleted;
297 else if (*p)
Neil Booth2a967f32001-05-20 06:26:45 +0000298 {
299 size_t n = HT_LEN (*p);
300
301 total_bytes += n;
Serge Belyshev0fd9e8d2004-09-06 13:22:48 +0000302 sum_of_squares += (double) n * n;
Neil Booth2a967f32001-05-20 06:26:45 +0000303 if (n > longest)
304 longest = n;
305 nids++;
306 }
307 while (++p < limit);
Andreas Jaeger1d088de2003-07-06 08:15:36 +0200308
Neil Booth2a967f32001-05-20 06:26:45 +0000309 nelts = table->nelements;
310 overhead = obstack_memory_used (&table->stack) - total_bytes;
311 headers = table->nslots * sizeof (hashnode);
312
313 fprintf (stderr, "\nString pool\nentries\t\t%lu\n",
314 (unsigned long) nelts);
315 fprintf (stderr, "identifiers\t%lu (%.2f%%)\n",
316 (unsigned long) nids, nids * 100.0 / nelts);
317 fprintf (stderr, "slots\t\t%lu\n",
318 (unsigned long) table->nslots);
Tom Tromeydae41742008-05-21 15:00:59 +0000319 fprintf (stderr, "deleted\t\t%lu\n",
320 (unsigned long) deleted);
Neil Booth2a967f32001-05-20 06:26:45 +0000321 fprintf (stderr, "bytes\t\t%lu%c (%lu%c overhead)\n",
322 SCALE (total_bytes), LABEL (total_bytes),
323 SCALE (overhead), LABEL (overhead));
324 fprintf (stderr, "table size\t%lu%c\n",
325 SCALE (headers), LABEL (headers));
326
327 exp_len = (double)total_bytes / (double)nelts;
328 exp2_len = exp_len * exp_len;
329 exp_len2 = (double) sum_of_squares / (double) nelts;
330
331 fprintf (stderr, "coll/search\t%.4f\n",
332 (double) table->collisions / (double) table->searches);
333 fprintf (stderr, "ins/search\t%.4f\n",
334 (double) nelts / (double) table->searches);
335 fprintf (stderr, "avg. entry\t%.2f bytes (+/- %.2f)\n",
336 exp_len, approx_sqrt (exp_len2 - exp2_len));
337 fprintf (stderr, "longest entry\t%lu\n",
338 (unsigned long) longest);
339#undef SCALE
340#undef LABEL
341}
342
343/* Return the approximate positive square root of a number N. This is for
344 statistical reports, not code generation. */
Zack Weinberga2f7be92003-07-22 16:24:53 +0000345static double
Andreas Jaeger1d088de2003-07-06 08:15:36 +0200346approx_sqrt (double x)
Neil Booth2a967f32001-05-20 06:26:45 +0000347{
348 double s, d;
349
350 if (x < 0)
351 abort ();
352 if (x == 0)
353 return 0;
354
355 s = x;
356 do
357 {
358 d = (s * s - x) / (2 * s);
359 s -= d;
360 }
361 while (d > .0001);
362 return s;
363}