DenseMapInfo.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/DenseMapInfo.h - Type traits for DenseMap -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines DenseMapInfo traits for DenseMap.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_DENSEMAPINFO_H
  19. #define LLVM_ADT_DENSEMAPINFO_H
  20. #include <cassert>
  21. #include <cstddef>
  22. #include <cstdint>
  23. #include <tuple>
  24. #include <utility>
  25. namespace llvm {
  26. namespace detail {
  27. /// Simplistic combination of 32-bit hash values into 32-bit hash values.
  28. static inline unsigned combineHashValue(unsigned a, unsigned b) {
  29. uint64_t key = (uint64_t)a << 32 | (uint64_t)b;
  30. key += ~(key << 32);
  31. key ^= (key >> 22);
  32. key += ~(key << 13);
  33. key ^= (key >> 8);
  34. key += (key << 3);
  35. key ^= (key >> 15);
  36. key += ~(key << 27);
  37. key ^= (key >> 31);
  38. return (unsigned)key;
  39. }
  40. } // end namespace detail
  41. /// An information struct used to provide DenseMap with the various necessary
  42. /// components for a given value type `T`. `Enable` is an optional additional
  43. /// parameter that is used to support SFINAE (generally using std::enable_if_t)
  44. /// in derived DenseMapInfo specializations; in non-SFINAE use cases this should
  45. /// just be `void`.
  46. template<typename T, typename Enable = void>
  47. struct DenseMapInfo {
  48. //static inline T getEmptyKey();
  49. //static inline T getTombstoneKey();
  50. //static unsigned getHashValue(const T &Val);
  51. //static bool isEqual(const T &LHS, const T &RHS);
  52. };
  53. // Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
  54. // that are aligned to alignof(T) bytes, but try to avoid requiring T to be
  55. // complete. This allows clients to instantiate DenseMap<T*, ...> with forward
  56. // declared key types. Assume that no pointer key type requires more than 4096
  57. // bytes of alignment.
  58. template<typename T>
  59. struct DenseMapInfo<T*> {
  60. // The following should hold, but it would require T to be complete:
  61. // static_assert(alignof(T) <= (1 << Log2MaxAlign),
  62. // "DenseMap does not support pointer keys requiring more than "
  63. // "Log2MaxAlign bits of alignment");
  64. static constexpr uintptr_t Log2MaxAlign = 12;
  65. static inline T* getEmptyKey() {
  66. uintptr_t Val = static_cast<uintptr_t>(-1);
  67. Val <<= Log2MaxAlign;
  68. return reinterpret_cast<T*>(Val);
  69. }
  70. static inline T* getTombstoneKey() {
  71. uintptr_t Val = static_cast<uintptr_t>(-2);
  72. Val <<= Log2MaxAlign;
  73. return reinterpret_cast<T*>(Val);
  74. }
  75. static unsigned getHashValue(const T *PtrVal) {
  76. return (unsigned((uintptr_t)PtrVal) >> 4) ^
  77. (unsigned((uintptr_t)PtrVal) >> 9);
  78. }
  79. static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
  80. };
  81. // Provide DenseMapInfo for chars.
  82. template<> struct DenseMapInfo<char> {
  83. static inline char getEmptyKey() { return ~0; }
  84. static inline char getTombstoneKey() { return ~0 - 1; }
  85. static unsigned getHashValue(const char& Val) { return Val * 37U; }
  86. static bool isEqual(const char &LHS, const char &RHS) {
  87. return LHS == RHS;
  88. }
  89. };
  90. // Provide DenseMapInfo for unsigned chars.
  91. template <> struct DenseMapInfo<unsigned char> {
  92. static inline unsigned char getEmptyKey() { return ~0; }
  93. static inline unsigned char getTombstoneKey() { return ~0 - 1; }
  94. static unsigned getHashValue(const unsigned char &Val) { return Val * 37U; }
  95. static bool isEqual(const unsigned char &LHS, const unsigned char &RHS) {
  96. return LHS == RHS;
  97. }
  98. };
  99. // Provide DenseMapInfo for unsigned shorts.
  100. template <> struct DenseMapInfo<unsigned short> {
  101. static inline unsigned short getEmptyKey() { return 0xFFFF; }
  102. static inline unsigned short getTombstoneKey() { return 0xFFFF - 1; }
  103. static unsigned getHashValue(const unsigned short &Val) { return Val * 37U; }
  104. static bool isEqual(const unsigned short &LHS, const unsigned short &RHS) {
  105. return LHS == RHS;
  106. }
  107. };
  108. // Provide DenseMapInfo for unsigned ints.
  109. template<> struct DenseMapInfo<unsigned> {
  110. static inline unsigned getEmptyKey() { return ~0U; }
  111. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  112. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  113. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  114. return LHS == RHS;
  115. }
  116. };
  117. // Provide DenseMapInfo for unsigned longs.
  118. template<> struct DenseMapInfo<unsigned long> {
  119. static inline unsigned long getEmptyKey() { return ~0UL; }
  120. static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  121. static unsigned getHashValue(const unsigned long& Val) {
  122. return (unsigned)(Val * 37UL);
  123. }
  124. static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  125. return LHS == RHS;
  126. }
  127. };
  128. // Provide DenseMapInfo for unsigned long longs.
  129. template<> struct DenseMapInfo<unsigned long long> {
  130. static inline unsigned long long getEmptyKey() { return ~0ULL; }
  131. static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  132. static unsigned getHashValue(const unsigned long long& Val) {
  133. return (unsigned)(Val * 37ULL);
  134. }
  135. static bool isEqual(const unsigned long long& LHS,
  136. const unsigned long long& RHS) {
  137. return LHS == RHS;
  138. }
  139. };
  140. // Provide DenseMapInfo for shorts.
  141. template <> struct DenseMapInfo<short> {
  142. static inline short getEmptyKey() { return 0x7FFF; }
  143. static inline short getTombstoneKey() { return -0x7FFF - 1; }
  144. static unsigned getHashValue(const short &Val) { return Val * 37U; }
  145. static bool isEqual(const short &LHS, const short &RHS) { return LHS == RHS; }
  146. };
  147. // Provide DenseMapInfo for ints.
  148. template<> struct DenseMapInfo<int> {
  149. static inline int getEmptyKey() { return 0x7fffffff; }
  150. static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  151. static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  152. static bool isEqual(const int& LHS, const int& RHS) {
  153. return LHS == RHS;
  154. }
  155. };
  156. // Provide DenseMapInfo for longs.
  157. template<> struct DenseMapInfo<long> {
  158. static inline long getEmptyKey() {
  159. return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  160. }
  161. static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  162. static unsigned getHashValue(const long& Val) {
  163. return (unsigned)(Val * 37UL);
  164. }
  165. static bool isEqual(const long& LHS, const long& RHS) {
  166. return LHS == RHS;
  167. }
  168. };
  169. // Provide DenseMapInfo for long longs.
  170. template<> struct DenseMapInfo<long long> {
  171. static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  172. static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  173. static unsigned getHashValue(const long long& Val) {
  174. return (unsigned)(Val * 37ULL);
  175. }
  176. static bool isEqual(const long long& LHS,
  177. const long long& RHS) {
  178. return LHS == RHS;
  179. }
  180. };
  181. // Provide DenseMapInfo for all pairs whose members have info.
  182. template<typename T, typename U>
  183. struct DenseMapInfo<std::pair<T, U>> {
  184. using Pair = std::pair<T, U>;
  185. using FirstInfo = DenseMapInfo<T>;
  186. using SecondInfo = DenseMapInfo<U>;
  187. static inline Pair getEmptyKey() {
  188. return std::make_pair(FirstInfo::getEmptyKey(),
  189. SecondInfo::getEmptyKey());
  190. }
  191. static inline Pair getTombstoneKey() {
  192. return std::make_pair(FirstInfo::getTombstoneKey(),
  193. SecondInfo::getTombstoneKey());
  194. }
  195. static unsigned getHashValue(const Pair& PairVal) {
  196. return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
  197. SecondInfo::getHashValue(PairVal.second));
  198. }
  199. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  200. return FirstInfo::isEqual(LHS.first, RHS.first) &&
  201. SecondInfo::isEqual(LHS.second, RHS.second);
  202. }
  203. };
  204. // Provide DenseMapInfo for all tuples whose members have info.
  205. template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
  206. using Tuple = std::tuple<Ts...>;
  207. static inline Tuple getEmptyKey() {
  208. return Tuple(DenseMapInfo<Ts>::getEmptyKey()...);
  209. }
  210. static inline Tuple getTombstoneKey() {
  211. return Tuple(DenseMapInfo<Ts>::getTombstoneKey()...);
  212. }
  213. template <unsigned I>
  214. static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
  215. using EltType = typename std::tuple_element<I, Tuple>::type;
  216. std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  217. return detail::combineHashValue(
  218. DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
  219. getHashValueImpl<I + 1>(values, atEnd));
  220. }
  221. template <unsigned I>
  222. static unsigned getHashValueImpl(const Tuple &, std::true_type) {
  223. return 0;
  224. }
  225. static unsigned getHashValue(const std::tuple<Ts...> &values) {
  226. std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  227. return getHashValueImpl<0>(values, atEnd);
  228. }
  229. template <unsigned I>
  230. static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
  231. using EltType = typename std::tuple_element<I, Tuple>::type;
  232. std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  233. return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
  234. isEqualImpl<I + 1>(lhs, rhs, atEnd);
  235. }
  236. template <unsigned I>
  237. static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
  238. return true;
  239. }
  240. static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
  241. std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  242. return isEqualImpl<0>(lhs, rhs, atEnd);
  243. }
  244. };
  245. } // end namespace llvm
  246. #endif // LLVM_ADT_DENSEMAPINFO_H
  247. #ifdef __GNUC__
  248. #pragma GCC diagnostic pop
  249. #endif