GCC Middle and Back End API Reference
hash-traits.h
Go to the documentation of this file.
1/* Traits for hashable types.
2 Copyright (C) 2014-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef hash_traits_h
21#define hash_traits_h
22
23/* Helpful type for removing with free. */
24
25template <typename Type>
27{
28 static inline void remove (Type *p);
29};
30
31template <typename Type>
33{
34 static inline void remove (const Type *p);
35};
36
37/* Remove with free. */
38
39template <typename Type>
40inline void
45
46template <typename Type>
47inline void
49{
50 free (const_cast <Type *> (p));
51}
52
53/* Helpful type for removing with delete. */
54
55template <typename Type>
57{
58 static inline void remove (Type *p);
59};
60
61
62/* Remove with delete. */
63
64template <typename Type>
65inline void
67{
68 delete p;
69}
70
71/* Helpful type for a no-op remove. */
72
73template <typename Type>
75{
76 static inline void remove (Type &);
77};
78
79
80/* Remove doing nothing. */
81
82template <typename Type>
83inline void
87
88/* Base traits for integer type Type, providing just the hash and
89 comparison functionality. */
90
91template <typename Type>
93{
96
97 static inline hashval_t hash (value_type);
99};
100
101template <typename Type>
102inline hashval_t
104{
105 return x;
106}
107
108template <typename Type>
109inline bool
111{
112 return x == y;
113}
114
115/* Hasher for integer type Type in which Empty is a spare value that can be
116 used to mark empty slots. If Deleted != Empty then Deleted is another
117 spare value that can be used for deleted slots; if Deleted == Empty then
118 hash table entries cannot be deleted. */
119
120template <typename Type, Type Empty, Type Deleted = Empty>
121struct int_hash : int_hash_base <Type>
122{
125
126 static inline void mark_deleted (Type &);
127 static const bool empty_zero_p = Empty == 0;
128 static inline void mark_empty (Type &);
129 static inline bool is_deleted (Type);
130 static inline bool is_empty (Type);
131};
132
133template <typename Type, Type Empty, Type Deleted>
134inline void
140
141template <typename Type, Type Empty, Type Deleted>
142inline void
147
148template <typename Type, Type Empty, Type Deleted>
149inline bool
154
155template <typename Type, Type Empty, Type Deleted>
156inline bool
161
162/* Pointer hasher based on pointer equality. Other types of pointer hash
163 can inherit this and override the hash and equal functions with some
164 other form of equality (such as string equality). */
165
166template <typename Type>
168{
169 typedef Type *value_type;
171
172 static inline hashval_t hash (const value_type &);
173 static inline bool equal (const value_type &existing,
174 const compare_type &candidate);
175 static inline void mark_deleted (Type *&);
176 static const bool empty_zero_p = true;
177 static inline void mark_empty (Type *&);
178 static inline bool is_deleted (Type *);
179 static inline bool is_empty (Type *);
180};
181
182template <typename Type>
183inline hashval_t
185{
186 /* This is a really poor hash function, but it is what the current code uses,
187 so I am reusing it to avoid an additional axis in testing. */
188 return (hashval_t) ((intptr_t)candidate >> 3);
189}
190
191template <typename Type>
192inline bool
198
199template <typename Type>
200inline void
202{
203 e = reinterpret_cast<Type *> (1);
204}
205
206template <typename Type>
207inline void
212
213template <typename Type>
214inline bool
216{
217 return e == reinterpret_cast<Type *> (1);
218}
219
220template <typename Type>
221inline bool
223{
224 return e == NULL;
225}
226
227/* Hasher for "const char *" strings, using string rather than pointer
228 equality. */
229
230struct string_hash : pointer_hash <const char>
231{
232 static inline hashval_t hash (const char *);
233 static inline bool equal (const char *, const char *);
234};
235
236inline hashval_t
237string_hash::hash (const char *id)
238{
239 return htab_hash_string (id);
240}
241
242inline bool
243string_hash::equal (const char *id1, const char *id2)
244{
245 return strcmp (id1, id2) == 0;
246}
247
248/* Remover and marker for entries in gc memory. */
249
250template<typename T>
252{
253 static void remove (T &) {}
254
255 static void
256 ggc_mx (T &p)
257 {
258 extern void gt_ggc_mx (T &);
259 gt_ggc_mx (p);
260 }
261
262 /* Overridden in ggc_cache_remove. */
263 static void
265 {
266 ggc_mx (p);
267 }
268
269 static void
270 pch_nx (T &p)
271 {
272 extern void gt_pch_nx (T &);
273 gt_pch_nx (p);
274 }
275
276 static void
278 {
279 op (&p, NULL, cookie);
280 }
281};
282
283/* Remover and marker for "cache" entries in gc memory. These entries can
284 be deleted if there are no non-cache references to the data. */
285
286template<typename T>
288{
289 /* Entries are weakly held because this is for caches. */
290 static void ggc_maybe_mx (T &) {}
291
292 static int
294 {
295 return ggc_marked_p (e) ? -1 : 0;
296 }
297};
298
299/* Traits for pointer elements that should not be freed when an element
300 is deleted. */
301
302template <typename T>
304
305/* Traits for pointer elements that should be freed via free() when an
306 element is deleted. */
307
308template <typename T>
310
311/* Traits for pointer elements that should be freed via delete operand when an
312 element is deleted. */
313
314template <typename T>
316
317/* Traits for elements that point to gc memory. The pointed-to data
318 must be kept across collections. */
319
320template <typename T>
321struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
322
323/* Traits for elements that point to gc memory. The elements don't
324 in themselves keep the pointed-to data alive and they can be deleted
325 if the pointed-to data is going to be collected. */
326
327template <typename T>
329
330/* Traits for string elements that should be freed when an element is
331 deleted. */
332
334
335/* Traits for string elements that should not be freed when an element
336 is deleted. */
337
339
340/* Traits for pairs of values, using the first to record empty and
341 deleted slots. */
342
343template <typename T1, typename T2>
345{
346 typedef std::pair <typename T1::value_type,
347 typename T2::value_type> value_type;
348 typedef std::pair <typename T1::compare_type,
349 typename T2::compare_type> compare_type;
350
351 static inline hashval_t hash (const value_type &);
352 static inline bool equal (const value_type &, const compare_type &);
353 static inline void remove (value_type &);
354 static inline void mark_deleted (value_type &);
355 static const bool empty_zero_p = T1::empty_zero_p;
356 static inline void mark_empty (value_type &);
357 static inline bool is_deleted (const value_type &);
358 static inline bool is_empty (const value_type &);
359};
360
361template <typename T1, typename T2>
362inline hashval_t
364{
365 return iterative_hash_hashval_t (T1::hash (x.first), T2::hash (x.second));
366}
367
368template <typename T1, typename T2>
369inline bool
371{
372 return T1::equal (x.first, y.first) && T2::equal (x.second, y.second);
373}
374
375template <typename T1, typename T2>
376inline void
378{
379 T1::remove (x.first);
380 T2::remove (x.second);
381}
382
383template <typename T1, typename T2>
384inline void
386{
387 T1::mark_deleted (x.first);
388}
389
390template <typename T1, typename T2>
391inline void
393{
394 T1::mark_empty (x.first);
395}
396
397template <typename T1, typename T2>
398inline bool
400{
401 return T1::is_deleted (x.first);
402}
403
404template <typename T1, typename T2>
405inline bool
407{
408 return T1::is_empty (x.first);
409}
410
411/* Base traits for vectors, providing just the hash and comparison
412 functionality. Type gives the corresponding traits for the element
413 type. */
414
415template <typename Type>
424
425template <typename Type>
426inline hashval_t
428{
430 hstate.add_int (x.length ());
431 for (auto &value : x)
432 hstate.merge_hash (Type::hash (value));
433 return hstate.end ();
434}
435
436template <typename Type>
437inline bool
439{
440 if (x.length () != y.length ())
441 return false;
442 for (unsigned int i = 0; i < x.length (); ++i)
443 if (!Type::equal (x[i], y[i]))
444 return false;
445 return true;
446}
447
448/* Traits for vectors whose contents should be freed normally. */
449
450template <typename Type>
452{
453 static void remove (typename vec_hash_base <Type>::value_type &);
454};
455
456template <typename Type>
457void
460{
461 for (auto &value : x)
462 Type::remove (x);
463 x.release ();
464}
465
466template <typename T> struct default_hash_traits : T {};
467
468template <typename T>
470
471#endif
Definition inchash.h:38
void(* gt_pointer_operator)(void *, void *, void *)
Definition coretypes.h:462
free(str)
bool ggc_marked_p(const void *p)
Definition ggc-page.cc:1581
void gt_ggc_mx(const char *&x)
Definition ggc-page.cc:1518
void gt_pch_nx(const char *)
Definition ggc.h:341
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
hashval_t iterative_hash_hashval_t(hashval_t, hashval_t)
Definition inchash.h:178
i
Definition poly-int.h:772
Definition except.cc:183
Definition hash-traits.h:466
Definition hash-traits.h:315
Definition hash-traits.h:309
Definition hash-traits.h:333
Definition hash-traits.h:328
Definition hash-traits.h:288
static int keep_cache_entry(T &e)
Definition hash-traits.h:293
static void ggc_maybe_mx(T &)
Definition hash-traits.h:290
Definition hash-traits.h:321
Definition hash-traits.h:252
static void ggc_maybe_mx(T &p)
Definition hash-traits.h:264
static void pch_nx(T &p, gt_pointer_operator op, void *cookie)
Definition hash-traits.h:277
static void ggc_mx(T &p)
Definition hash-traits.h:256
static void pch_nx(T &p)
Definition hash-traits.h:270
static void remove(T &)
Definition hash-traits.h:253
Definition hash-traits.h:93
static hashval_t hash(value_type)
Definition hash-traits.h:103
Type value_type
Definition hash-traits.h:94
static bool equal(value_type existing, value_type candidate)
Definition hash-traits.h:110
Type compare_type
Definition hash-traits.h:95
Definition hash-traits.h:122
static bool is_deleted(Type)
Definition hash-traits.h:150
Type value_type
Definition hash-traits.h:123
static const bool empty_zero_p
Definition hash-traits.h:127
static bool is_empty(Type)
Definition hash-traits.h:157
static void mark_empty(Type &)
Definition hash-traits.h:143
Type compare_type
Definition hash-traits.h:124
static void mark_deleted(Type &)
Definition hash-traits.h:135
Definition hash-traits.h:303
Definition hash-traits.h:338
Definition hash-traits.h:345
std::pair< typename T1::value_type, typename T2::value_type > value_type
Definition hash-traits.h:347
static void mark_empty(value_type &)
Definition hash-traits.h:392
static void mark_deleted(value_type &)
Definition hash-traits.h:385
static bool is_empty(const value_type &)
Definition hash-traits.h:406
std::pair< typename T1::compare_type, typename T2::compare_type > compare_type
Definition hash-traits.h:349
static bool is_deleted(const value_type &)
Definition hash-traits.h:399
static const bool empty_zero_p
Definition hash-traits.h:355
static bool equal(const value_type &, const compare_type &)
Definition hash-traits.h:370
static void remove(value_type &)
Definition hash-traits.h:377
static hashval_t hash(const value_type &)
Definition hash-traits.h:363
Definition hash-traits.h:168
static bool equal(const value_type &existing, const compare_type &candidate)
Definition hash-traits.h:193
static bool is_empty(Type *)
Definition hash-traits.h:222
static void mark_empty(Type *&)
Definition hash-traits.h:208
static bool is_deleted(Type *)
Definition hash-traits.h:215
static void mark_deleted(Type *&)
Definition hash-traits.h:201
static const bool empty_zero_p
Definition hash-traits.h:176
Type * value_type
Definition hash-traits.h:169
Type * compare_type
Definition hash-traits.h:170
static hashval_t hash(const value_type &)
Definition hash-traits.h:184
Definition hash-traits.h:231
static hashval_t hash(const char *)
Definition hash-traits.h:237
static bool equal(const char *, const char *)
Definition hash-traits.h:243
Definition hash-traits.h:33
static void remove(const Type *p)
Definition hash-traits.h:48
Definition hash-traits.h:57
static void remove(Type *p)
Definition hash-traits.h:66
Definition hash-traits.h:27
static void remove(Type *p)
Definition hash-traits.h:41
Definition hash-traits.h:75
static void remove(Type &)
Definition hash-traits.h:84
Definition hash-traits.h:452
static void remove(typename vec_hash_base< Type >::value_type &)
Definition hash-traits.h:459
Definition hash-traits.h:417
static bool equal(value_type, compare_type)
Definition hash-traits.h:438
vec< typename Type::value_type > value_type
Definition hash-traits.h:418
static hashval_t hash(value_type)
Definition hash-traits.h:427
vec< typename Type::compare_type > compare_type
Definition hash-traits.h:419
Definition vec.h:450
#define NULL
Definition system.h:50
#define gcc_assert(EXPR)
Definition system.h:821
static tree candidate(unsigned uid)
Definition tree-sra.cc:325
const T2 & y
Definition wide-int.h:3870