]> gcc.gnu.org Git - gcc.git/blame - gcc/hash-traits.h
OpenACC: Fix reduction tree-sharing issue [PR106982]
[gcc.git] / gcc / hash-traits.h
CommitLineData
f11c3779 1/* Traits for hashable types.
7adcbafe 2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
f11c3779
RS
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>
26struct typed_free_remove
27{
28 static inline void remove (Type *p);
29};
30
bc84b61b
MNW
31template <typename Type>
32struct typed_const_free_remove
33{
34 static inline void remove (const Type *p);
35};
f11c3779
RS
36
37/* Remove with free. */
38
39template <typename Type>
40inline void
41typed_free_remove <Type>::remove (Type *p)
42{
43 free (p);
44}
45
bc84b61b
MNW
46template <typename Type>
47inline void
48typed_const_free_remove <Type>::remove (const Type *p)
49{
50 free (const_cast <Type *> (p));
51}
52
74fbae92
ML
53/* Helpful type for removing with delete. */
54
55template <typename Type>
56struct typed_delete_remove
57{
58 static inline void remove (Type *p);
59};
60
61
62/* Remove with delete. */
63
64template <typename Type>
65inline void
66typed_delete_remove <Type>::remove (Type *p)
67{
68 delete p;
69}
f11c3779
RS
70
71/* Helpful type for a no-op remove. */
72
73template <typename Type>
74struct typed_noop_remove
75{
fc17926a 76 static inline void remove (Type &);
f11c3779
RS
77};
78
79
80/* Remove doing nothing. */
81
82template <typename Type>
83inline void
fc17926a 84typed_noop_remove <Type>::remove (Type &)
f11c3779
RS
85{
86}
87
88
e0702244
RS
89/* Hasher for integer type Type in which Empty is a spare value that can be
90 used to mark empty slots. If Deleted != Empty then Deleted is another
91 spare value that can be used for deleted slots; if Deleted == Empty then
92 hash table entries cannot be deleted. */
93
94template <typename Type, Type Empty, Type Deleted = Empty>
95struct int_hash : typed_noop_remove <Type>
96{
97 typedef Type value_type;
98 typedef Type compare_type;
99
100 static inline hashval_t hash (value_type);
101 static inline bool equal (value_type existing, value_type candidate);
102 static inline void mark_deleted (Type &);
7ca50de0 103 static const bool empty_zero_p = Empty == 0;
e0702244
RS
104 static inline void mark_empty (Type &);
105 static inline bool is_deleted (Type);
106 static inline bool is_empty (Type);
107};
108
109template <typename Type, Type Empty, Type Deleted>
110inline hashval_t
111int_hash <Type, Empty, Deleted>::hash (value_type x)
112{
113 return x;
114}
115
116template <typename Type, Type Empty, Type Deleted>
117inline bool
118int_hash <Type, Empty, Deleted>::equal (value_type x, value_type y)
119{
120 return x == y;
121}
122
123template <typename Type, Type Empty, Type Deleted>
124inline void
125int_hash <Type, Empty, Deleted>::mark_deleted (Type &x)
126{
127 gcc_assert (Empty != Deleted);
128 x = Deleted;
129}
130
131template <typename Type, Type Empty, Type Deleted>
132inline void
133int_hash <Type, Empty, Deleted>::mark_empty (Type &x)
134{
135 x = Empty;
136}
137
138template <typename Type, Type Empty, Type Deleted>
139inline bool
140int_hash <Type, Empty, Deleted>::is_deleted (Type x)
141{
142 return Empty != Deleted && x == Deleted;
143}
144
145template <typename Type, Type Empty, Type Deleted>
146inline bool
147int_hash <Type, Empty, Deleted>::is_empty (Type x)
148{
149 return x == Empty;
150}
151
8d67ee55
RS
152/* Pointer hasher based on pointer equality. Other types of pointer hash
153 can inherit this and override the hash and equal functions with some
154 other form of equality (such as string equality). */
f11c3779
RS
155
156template <typename Type>
8d67ee55 157struct pointer_hash
f11c3779
RS
158{
159 typedef Type *value_type;
160 typedef Type *compare_type;
161
162 static inline hashval_t hash (const value_type &);
f11c3779
RS
163 static inline bool equal (const value_type &existing,
164 const compare_type &candidate);
843adf88 165 static inline void mark_deleted (Type *&);
7ca50de0 166 static const bool empty_zero_p = true;
843adf88
RS
167 static inline void mark_empty (Type *&);
168 static inline bool is_deleted (Type *);
169 static inline bool is_empty (Type *);
f11c3779
RS
170};
171
172template <typename Type>
173inline hashval_t
174pointer_hash <Type>::hash (const value_type &candidate)
175{
176 /* This is a really poor hash function, but it is what the current code uses,
177 so I am reusing it to avoid an additional axis in testing. */
178 return (hashval_t) ((intptr_t)candidate >> 3);
179}
180
181template <typename Type>
182inline bool
183pointer_hash <Type>::equal (const value_type &existing,
184 const compare_type &candidate)
185{
186 return existing == candidate;
187}
188
843adf88
RS
189template <typename Type>
190inline void
191pointer_hash <Type>::mark_deleted (Type *&e)
192{
193 e = reinterpret_cast<Type *> (1);
194}
195
196template <typename Type>
197inline void
198pointer_hash <Type>::mark_empty (Type *&e)
199{
200 e = NULL;
201}
202
203template <typename Type>
204inline bool
205pointer_hash <Type>::is_deleted (Type *e)
206{
207 return e == reinterpret_cast<Type *> (1);
208}
209
210template <typename Type>
211inline bool
212pointer_hash <Type>::is_empty (Type *e)
213{
214 return e == NULL;
215}
216
20d2c372
RS
217/* Hasher for "const char *" strings, using string rather than pointer
218 equality. */
219
220struct string_hash : pointer_hash <const char>
221{
222 static inline hashval_t hash (const char *);
223 static inline bool equal (const char *, const char *);
224};
225
226inline hashval_t
227string_hash::hash (const char *id)
228{
229 return htab_hash_string (id);
230}
231
232inline bool
233string_hash::equal (const char *id1, const char *id2)
234{
235 return strcmp (id1, id2) == 0;
236}
237
ca752f39 238/* Remover and marker for entries in gc memory. */
f11c3779
RS
239
240template<typename T>
ca752f39 241struct ggc_remove
f11c3779 242{
5ac6389b 243 static void remove (T &) {}
f11c3779
RS
244
245 static void
5ac6389b 246 ggc_mx (T &p)
f11c3779
RS
247 {
248 extern void gt_ggc_mx (T &);
249 gt_ggc_mx (p);
250 }
251
21faa101
JM
252 /* Overridden in ggc_cache_remove. */
253 static void
254 ggc_maybe_mx (T &p)
255 {
256 ggc_mx (p);
257 }
258
f11c3779
RS
259 static void
260 pch_nx (T &p)
261 {
262 extern void gt_pch_nx (T &);
263 gt_pch_nx (p);
264 }
265
266 static void
267 pch_nx (T &p, gt_pointer_operator op, void *cookie)
268 {
747380f4 269 op (&p, NULL, cookie);
f11c3779
RS
270 }
271};
272
6c907cff
RS
273/* Remover and marker for "cache" entries in gc memory. These entries can
274 be deleted if there are no non-cache references to the data. */
f11c3779
RS
275
276template<typename T>
6c907cff 277struct ggc_cache_remove : ggc_remove<T>
f11c3779 278{
f11c3779 279 /* Entries are weakly held because this is for caches. */
21faa101 280 static void ggc_maybe_mx (T &) {}
f11c3779 281
08ec2754
RS
282 static int
283 keep_cache_entry (T &e)
f11c3779 284 {
08ec2754 285 return ggc_marked_p (e) ? -1 : 0;
f11c3779
RS
286 }
287};
288
8d67ee55
RS
289/* Traits for pointer elements that should not be freed when an element
290 is deleted. */
291
292template <typename T>
fc17926a 293struct nofree_ptr_hash : pointer_hash <T>, typed_noop_remove <T *> {};
8d67ee55 294
95fbe13e
RS
295/* Traits for pointer elements that should be freed via free() when an
296 element is deleted. */
297
298template <typename T>
299struct free_ptr_hash : pointer_hash <T>, typed_free_remove <T> {};
300
74fbae92
ML
301/* Traits for pointer elements that should be freed via delete operand when an
302 element is deleted. */
303
304template <typename T>
305struct delete_ptr_hash : pointer_hash <T>, typed_delete_remove <T> {};
306
ca752f39
RS
307/* Traits for elements that point to gc memory. The pointed-to data
308 must be kept across collections. */
309
310template <typename T>
311struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
312
6c907cff
RS
313/* Traits for elements that point to gc memory. The elements don't
314 in themselves keep the pointed-to data alive and they can be deleted
315 if the pointed-to data is going to be collected. */
316
317template <typename T>
318struct ggc_cache_ptr_hash : pointer_hash <T>, ggc_cache_remove <T *> {};
319
bc84b61b
MNW
320/* Traits for string elements that should be freed when an element is
321 deleted. */
322
323struct free_string_hash : string_hash, typed_const_free_remove <char> {};
324
20d2c372
RS
325/* Traits for string elements that should not be freed when an element
326 is deleted. */
327
328struct nofree_string_hash : string_hash, typed_noop_remove <const char *> {};
329
9adee305
RS
330/* Traits for pairs of values, using the first to record empty and
331 deleted slots. */
332
333template <typename T1, typename T2>
334struct pair_hash
335{
336 typedef std::pair <typename T1::value_type,
337 typename T2::value_type> value_type;
338 typedef std::pair <typename T1::compare_type,
339 typename T2::compare_type> compare_type;
340
341 static inline hashval_t hash (const value_type &);
342 static inline bool equal (const value_type &, const compare_type &);
343 static inline void remove (value_type &);
344 static inline void mark_deleted (value_type &);
7ca50de0 345 static const bool empty_zero_p = T1::empty_zero_p;
9adee305
RS
346 static inline void mark_empty (value_type &);
347 static inline bool is_deleted (const value_type &);
348 static inline bool is_empty (const value_type &);
349};
350
351template <typename T1, typename T2>
352inline hashval_t
353pair_hash <T1, T2>::hash (const value_type &x)
354{
355 return iterative_hash_hashval_t (T1::hash (x.first), T2::hash (x.second));
356}
357
358template <typename T1, typename T2>
359inline bool
360pair_hash <T1, T2>::equal (const value_type &x, const compare_type &y)
361{
362 return T1::equal (x.first, y.first) && T2::equal (x.second, y.second);
363}
364
365template <typename T1, typename T2>
366inline void
367pair_hash <T1, T2>::remove (value_type &x)
368{
369 T1::remove (x.first);
370 T2::remove (x.second);
371}
372
373template <typename T1, typename T2>
374inline void
375pair_hash <T1, T2>::mark_deleted (value_type &x)
376{
377 T1::mark_deleted (x.first);
378}
379
380template <typename T1, typename T2>
381inline void
382pair_hash <T1, T2>::mark_empty (value_type &x)
383{
384 T1::mark_empty (x.first);
385}
386
387template <typename T1, typename T2>
388inline bool
389pair_hash <T1, T2>::is_deleted (const value_type &x)
390{
391 return T1::is_deleted (x.first);
392}
393
394template <typename T1, typename T2>
395inline bool
396pair_hash <T1, T2>::is_empty (const value_type &x)
397{
398 return T1::is_empty (x.first);
399}
400
fb5c464a 401template <typename T> struct default_hash_traits : T {};
b32ca1df
RS
402
403template <typename T>
404struct default_hash_traits <T *> : ggc_ptr_hash <T> {};
405
f11c3779 406#endif
This page took 3.681491 seconds and 5 git commands to generate.