DenseMapInfo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 <type_traits>
  25. #include <utility>
  26. #include <variant>
  27. namespace llvm {
  28. namespace detail {
  29. /// Simplistic combination of 32-bit hash values into 32-bit hash values.
  30. static inline unsigned combineHashValue(unsigned a, unsigned b) {
  31. uint64_t key = (uint64_t)a << 32 | (uint64_t)b;
  32. key += ~(key << 32);
  33. key ^= (key >> 22);
  34. key += ~(key << 13);
  35. key ^= (key >> 8);
  36. key += (key << 3);
  37. key ^= (key >> 15);
  38. key += ~(key << 27);
  39. key ^= (key >> 31);
  40. return (unsigned)key;
  41. }
  42. } // end namespace detail
  43. /// An information struct used to provide DenseMap with the various necessary
  44. /// components for a given value type `T`. `Enable` is an optional additional
  45. /// parameter that is used to support SFINAE (generally using std::enable_if_t)
  46. /// in derived DenseMapInfo specializations; in non-SFINAE use cases this should
  47. /// just be `void`.
  48. template<typename T, typename Enable = void>
  49. struct DenseMapInfo {
  50. //static inline T getEmptyKey();
  51. //static inline T getTombstoneKey();
  52. //static unsigned getHashValue(const T &Val);
  53. //static bool isEqual(const T &LHS, const T &RHS);
  54. };
  55. // Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
  56. // that are aligned to alignof(T) bytes, but try to avoid requiring T to be
  57. // complete. This allows clients to instantiate DenseMap<T*, ...> with forward
  58. // declared key types. Assume that no pointer key type requires more than 4096
  59. // bytes of alignment.
  60. template<typename T>
  61. struct DenseMapInfo<T*> {
  62. // The following should hold, but it would require T to be complete:
  63. // static_assert(alignof(T) <= (1 << Log2MaxAlign),
  64. // "DenseMap does not support pointer keys requiring more than "
  65. // "Log2MaxAlign bits of alignment");
  66. static constexpr uintptr_t Log2MaxAlign = 12;
  67. static inline T* getEmptyKey() {
  68. uintptr_t Val = static_cast<uintptr_t>(-1);
  69. Val <<= Log2MaxAlign;
  70. return reinterpret_cast<T*>(Val);
  71. }
  72. static inline T* getTombstoneKey() {
  73. uintptr_t Val = static_cast<uintptr_t>(-2);
  74. Val <<= Log2MaxAlign;
  75. return reinterpret_cast<T*>(Val);
  76. }
  77. static unsigned getHashValue(const T *PtrVal) {
  78. return (unsigned((uintptr_t)PtrVal) >> 4) ^
  79. (unsigned((uintptr_t)PtrVal) >> 9);
  80. }
  81. static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
  82. };
  83. // Provide DenseMapInfo for chars.
  84. template<> struct DenseMapInfo<char> {
  85. static inline char getEmptyKey() { return ~0; }
  86. static inline char getTombstoneKey() { return ~0 - 1; }
  87. static unsigned getHashValue(const char& Val) { return Val * 37U; }
  88. static bool isEqual(const char &LHS, const char &RHS) {
  89. return LHS == RHS;
  90. }
  91. };
  92. // Provide DenseMapInfo for unsigned chars.
  93. template <> struct DenseMapInfo<unsigned char> {
  94. static inline unsigned char getEmptyKey() { return ~0; }
  95. static inline unsigned char getTombstoneKey() { return ~0 - 1; }
  96. static unsigned getHashValue(const unsigned char &Val) { return Val * 37U; }
  97. static bool isEqual(const unsigned char &LHS, const unsigned char &RHS) {
  98. return LHS == RHS;
  99. }
  100. };
  101. // Provide DenseMapInfo for unsigned shorts.
  102. template <> struct DenseMapInfo<unsigned short> {
  103. static inline unsigned short getEmptyKey() { return 0xFFFF; }
  104. static inline unsigned short getTombstoneKey() { return 0xFFFF - 1; }
  105. static unsigned getHashValue(const unsigned short &Val) { return Val * 37U; }
  106. static bool isEqual(const unsigned short &LHS, const unsigned short &RHS) {
  107. return LHS == RHS;
  108. }
  109. };
  110. // Provide DenseMapInfo for unsigned ints.
  111. template<> struct DenseMapInfo<unsigned> {
  112. static inline unsigned getEmptyKey() { return ~0U; }
  113. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  114. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  115. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  116. return LHS == RHS;
  117. }
  118. };
  119. // Provide DenseMapInfo for unsigned longs.
  120. template<> struct DenseMapInfo<unsigned long> {
  121. static inline unsigned long getEmptyKey() { return ~0UL; }
  122. static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  123. static unsigned getHashValue(const unsigned long& Val) {
  124. return (unsigned)(Val * 37UL);
  125. }
  126. static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  127. return LHS == RHS;
  128. }
  129. };
  130. // Provide DenseMapInfo for unsigned long longs.
  131. template<> struct DenseMapInfo<unsigned long long> {
  132. static inline unsigned long long getEmptyKey() { return ~0ULL; }
  133. static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  134. static unsigned getHashValue(const unsigned long long& Val) {
  135. return (unsigned)(Val * 37ULL);
  136. }
  137. static bool isEqual(const unsigned long long& LHS,
  138. const unsigned long long& RHS) {
  139. return LHS == RHS;
  140. }
  141. };
  142. // Provide DenseMapInfo for shorts.
  143. template <> struct DenseMapInfo<short> {
  144. static inline short getEmptyKey() { return 0x7FFF; }
  145. static inline short getTombstoneKey() { return -0x7FFF - 1; }
  146. static unsigned getHashValue(const short &Val) { return Val * 37U; }
  147. static bool isEqual(const short &LHS, const short &RHS) { return LHS == RHS; }
  148. };
  149. // Provide DenseMapInfo for ints.
  150. template<> struct DenseMapInfo<int> {
  151. static inline int getEmptyKey() { return 0x7fffffff; }
  152. static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  153. static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  154. static bool isEqual(const int& LHS, const int& RHS) {
  155. return LHS == RHS;
  156. }
  157. };
  158. // Provide DenseMapInfo for longs.
  159. template<> struct DenseMapInfo<long> {
  160. static inline long getEmptyKey() {
  161. return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  162. }
  163. static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  164. static unsigned getHashValue(const long& Val) {
  165. return (unsigned)(Val * 37UL);
  166. }
  167. static bool isEqual(const long& LHS, const long& RHS) {
  168. return LHS == RHS;
  169. }
  170. };
  171. // Provide DenseMapInfo for long longs.
  172. template<> struct DenseMapInfo<long long> {
  173. static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  174. static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  175. static unsigned getHashValue(const long long& Val) {
  176. return (unsigned)(Val * 37ULL);
  177. }
  178. static bool isEqual(const long long& LHS,
  179. const long long& RHS) {
  180. return LHS == RHS;
  181. }
  182. };
  183. // Provide DenseMapInfo for all pairs whose members have info.
  184. template<typename T, typename U>
  185. struct DenseMapInfo<std::pair<T, U>> {
  186. using Pair = std::pair<T, U>;
  187. using FirstInfo = DenseMapInfo<T>;
  188. using SecondInfo = DenseMapInfo<U>;
  189. static inline Pair getEmptyKey() {
  190. return std::make_pair(FirstInfo::getEmptyKey(),
  191. SecondInfo::getEmptyKey());
  192. }
  193. static inline Pair getTombstoneKey() {
  194. return std::make_pair(FirstInfo::getTombstoneKey(),
  195. SecondInfo::getTombstoneKey());
  196. }
  197. static unsigned getHashValue(const Pair& PairVal) {
  198. return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
  199. SecondInfo::getHashValue(PairVal.second));
  200. }
  201. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  202. return FirstInfo::isEqual(LHS.first, RHS.first) &&
  203. SecondInfo::isEqual(LHS.second, RHS.second);
  204. }
  205. };
  206. // Provide DenseMapInfo for all tuples whose members have info.
  207. template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
  208. using Tuple = std::tuple<Ts...>;
  209. static inline Tuple getEmptyKey() {
  210. return Tuple(DenseMapInfo<Ts>::getEmptyKey()...);
  211. }
  212. static inline Tuple getTombstoneKey() {
  213. return Tuple(DenseMapInfo<Ts>::getTombstoneKey()...);
  214. }
  215. template <unsigned I>
  216. static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
  217. using EltType = std::tuple_element_t<I, Tuple>;
  218. std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  219. return detail::combineHashValue(
  220. DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
  221. getHashValueImpl<I + 1>(values, atEnd));
  222. }
  223. template <unsigned I>
  224. static unsigned getHashValueImpl(const Tuple &, std::true_type) {
  225. return 0;
  226. }
  227. static unsigned getHashValue(const std::tuple<Ts...> &values) {
  228. std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  229. return getHashValueImpl<0>(values, atEnd);
  230. }
  231. template <unsigned I>
  232. static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
  233. using EltType = std::tuple_element_t<I, Tuple>;
  234. std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  235. return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
  236. isEqualImpl<I + 1>(lhs, rhs, atEnd);
  237. }
  238. template <unsigned I>
  239. static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
  240. return true;
  241. }
  242. static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
  243. std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  244. return isEqualImpl<0>(lhs, rhs, atEnd);
  245. }
  246. };
  247. // Provide DenseMapInfo for variants whose all alternatives have DenseMapInfo.
  248. template <typename... Ts> struct DenseMapInfo<std::variant<Ts...>> {
  249. using Variant = std::variant<Ts...>;
  250. using FirstT = std::variant_alternative_t<0, Variant>;
  251. static inline Variant getEmptyKey() {
  252. return Variant(std::in_place_index<0>, DenseMapInfo<FirstT>::getEmptyKey());
  253. }
  254. static inline Variant getTombstoneKey() {
  255. return Variant(std::in_place_index<0>,
  256. DenseMapInfo<FirstT>::getTombstoneKey());
  257. }
  258. static unsigned getHashValue(const Variant &Val) {
  259. return std::visit(
  260. [&Val](auto &&Alternative) {
  261. using T = std::decay_t<decltype(Alternative)>;
  262. // Include index in hash to make sure same value as different
  263. // alternatives don't collide.
  264. return detail::combineHashValue(
  265. DenseMapInfo<size_t>::getHashValue(Val.index()),
  266. DenseMapInfo<T>::getHashValue(Alternative));
  267. },
  268. Val);
  269. }
  270. static bool isEqual(const Variant &LHS, const Variant &RHS) {
  271. return LHS == RHS;
  272. }
  273. };
  274. } // end namespace llvm
  275. #endif // LLVM_ADT_DENSEMAPINFO_H
  276. #ifdef __GNUC__
  277. #pragma GCC diagnostic pop
  278. #endif