DenseMapInfo.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // This file defines DenseMapInfo traits for DenseMap.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ADT_DENSEMAPINFO_H
  18. #define LLVM_ADT_DENSEMAPINFO_H
  19. #include "llvm/ADT/APInt.h"
  20. #include "llvm/ADT/APSInt.h"
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/ADT/Hashing.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include <cassert>
  25. #include <cstddef>
  26. #include <cstdint>
  27. #include <utility>
  28. namespace llvm {
  29. namespace detail {
  30. /// Simplistic combination of 32-bit hash values into 32-bit hash values.
  31. static inline unsigned combineHashValue(unsigned a, unsigned b) {
  32. uint64_t key = (uint64_t)a << 32 | (uint64_t)b;
  33. key += ~(key << 32);
  34. key ^= (key >> 22);
  35. key += ~(key << 13);
  36. key ^= (key >> 8);
  37. key += (key << 3);
  38. key ^= (key >> 15);
  39. key += ~(key << 27);
  40. key ^= (key >> 31);
  41. return (unsigned)key;
  42. }
  43. } // end namespace detail
  44. template<typename T>
  45. struct DenseMapInfo {
  46. //static inline T getEmptyKey();
  47. //static inline T getTombstoneKey();
  48. //static unsigned getHashValue(const T &Val);
  49. //static bool isEqual(const T &LHS, const T &RHS);
  50. };
  51. // Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
  52. // that are aligned to alignof(T) bytes, but try to avoid requiring T to be
  53. // complete. This allows clients to instantiate DenseMap<T*, ...> with forward
  54. // declared key types. Assume that no pointer key type requires more than 4096
  55. // bytes of alignment.
  56. template<typename T>
  57. struct DenseMapInfo<T*> {
  58. // The following should hold, but it would require T to be complete:
  59. // static_assert(alignof(T) <= (1 << Log2MaxAlign),
  60. // "DenseMap does not support pointer keys requiring more than "
  61. // "Log2MaxAlign bits of alignment");
  62. static constexpr uintptr_t Log2MaxAlign = 12;
  63. static inline T* getEmptyKey() {
  64. uintptr_t Val = static_cast<uintptr_t>(-1);
  65. Val <<= Log2MaxAlign;
  66. return reinterpret_cast<T*>(Val);
  67. }
  68. static inline T* getTombstoneKey() {
  69. uintptr_t Val = static_cast<uintptr_t>(-2);
  70. Val <<= Log2MaxAlign;
  71. return reinterpret_cast<T*>(Val);
  72. }
  73. static unsigned getHashValue(const T *PtrVal) {
  74. return (unsigned((uintptr_t)PtrVal) >> 4) ^
  75. (unsigned((uintptr_t)PtrVal) >> 9);
  76. }
  77. static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
  78. };
  79. // Provide DenseMapInfo for chars.
  80. template<> struct DenseMapInfo<char> {
  81. static inline char getEmptyKey() { return ~0; }
  82. static inline char getTombstoneKey() { return ~0 - 1; }
  83. static unsigned getHashValue(const char& Val) { return Val * 37U; }
  84. static bool isEqual(const char &LHS, const char &RHS) {
  85. return LHS == RHS;
  86. }
  87. };
  88. // Provide DenseMapInfo for unsigned chars.
  89. template <> struct DenseMapInfo<unsigned char> {
  90. static inline unsigned char getEmptyKey() { return ~0; }
  91. static inline unsigned char getTombstoneKey() { return ~0 - 1; }
  92. static unsigned getHashValue(const unsigned char &Val) { return Val * 37U; }
  93. static bool isEqual(const unsigned char &LHS, const unsigned char &RHS) {
  94. return LHS == RHS;
  95. }
  96. };
  97. // Provide DenseMapInfo for unsigned shorts.
  98. template <> struct DenseMapInfo<unsigned short> {
  99. static inline unsigned short getEmptyKey() { return 0xFFFF; }
  100. static inline unsigned short getTombstoneKey() { return 0xFFFF - 1; }
  101. static unsigned getHashValue(const unsigned short &Val) { return Val * 37U; }
  102. static bool isEqual(const unsigned short &LHS, const unsigned short &RHS) {
  103. return LHS == RHS;
  104. }
  105. };
  106. // Provide DenseMapInfo for unsigned ints.
  107. template<> struct DenseMapInfo<unsigned> {
  108. static inline unsigned getEmptyKey() { return ~0U; }
  109. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  110. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  111. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  112. return LHS == RHS;
  113. }
  114. };
  115. // Provide DenseMapInfo for unsigned longs.
  116. template<> struct DenseMapInfo<unsigned long> {
  117. static inline unsigned long getEmptyKey() { return ~0UL; }
  118. static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  119. static unsigned getHashValue(const unsigned long& Val) {
  120. return (unsigned)(Val * 37UL);
  121. }
  122. static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  123. return LHS == RHS;
  124. }
  125. };
  126. // Provide DenseMapInfo for unsigned long longs.
  127. template<> struct DenseMapInfo<unsigned long long> {
  128. static inline unsigned long long getEmptyKey() { return ~0ULL; }
  129. static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  130. static unsigned getHashValue(const unsigned long long& Val) {
  131. return (unsigned)(Val * 37ULL);
  132. }
  133. static bool isEqual(const unsigned long long& LHS,
  134. const unsigned long long& RHS) {
  135. return LHS == RHS;
  136. }
  137. };
  138. // Provide DenseMapInfo for shorts.
  139. template <> struct DenseMapInfo<short> {
  140. static inline short getEmptyKey() { return 0x7FFF; }
  141. static inline short getTombstoneKey() { return -0x7FFF - 1; }
  142. static unsigned getHashValue(const short &Val) { return Val * 37U; }
  143. static bool isEqual(const short &LHS, const short &RHS) { return LHS == RHS; }
  144. };
  145. // Provide DenseMapInfo for ints.
  146. template<> struct DenseMapInfo<int> {
  147. static inline int getEmptyKey() { return 0x7fffffff; }
  148. static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  149. static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  150. static bool isEqual(const int& LHS, const int& RHS) {
  151. return LHS == RHS;
  152. }
  153. };
  154. // Provide DenseMapInfo for longs.
  155. template<> struct DenseMapInfo<long> {
  156. static inline long getEmptyKey() {
  157. return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  158. }
  159. static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  160. static unsigned getHashValue(const long& Val) {
  161. return (unsigned)(Val * 37UL);
  162. }
  163. static bool isEqual(const long& LHS, const long& RHS) {
  164. return LHS == RHS;
  165. }
  166. };
  167. // Provide DenseMapInfo for long longs.
  168. template<> struct DenseMapInfo<long long> {
  169. static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  170. static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  171. static unsigned getHashValue(const long long& Val) {
  172. return (unsigned)(Val * 37ULL);
  173. }
  174. static bool isEqual(const long long& LHS,
  175. const long long& RHS) {
  176. return LHS == RHS;
  177. }
  178. };
  179. // Provide DenseMapInfo for all pairs whose members have info.
  180. template<typename T, typename U>
  181. struct DenseMapInfo<std::pair<T, U>> {
  182. using Pair = std::pair<T, U>;
  183. using FirstInfo = DenseMapInfo<T>;
  184. using SecondInfo = DenseMapInfo<U>;
  185. static inline Pair getEmptyKey() {
  186. return std::make_pair(FirstInfo::getEmptyKey(),
  187. SecondInfo::getEmptyKey());
  188. }
  189. static inline Pair getTombstoneKey() {
  190. return std::make_pair(FirstInfo::getTombstoneKey(),
  191. SecondInfo::getTombstoneKey());
  192. }
  193. static unsigned getHashValue(const Pair& PairVal) {
  194. return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
  195. SecondInfo::getHashValue(PairVal.second));
  196. }
  197. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  198. return FirstInfo::isEqual(LHS.first, RHS.first) &&
  199. SecondInfo::isEqual(LHS.second, RHS.second);
  200. }
  201. };
  202. // Provide DenseMapInfo for all tuples whose members have info.
  203. template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
  204. using Tuple = std::tuple<Ts...>;
  205. static inline Tuple getEmptyKey() {
  206. return Tuple(DenseMapInfo<Ts>::getEmptyKey()...);
  207. }
  208. static inline Tuple getTombstoneKey() {
  209. return Tuple(DenseMapInfo<Ts>::getTombstoneKey()...);
  210. }
  211. template <unsigned I>
  212. static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
  213. using EltType = typename std::tuple_element<I, Tuple>::type;
  214. std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  215. return detail::combineHashValue(
  216. DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
  217. getHashValueImpl<I + 1>(values, atEnd));
  218. }
  219. template <unsigned I>
  220. static unsigned getHashValueImpl(const Tuple &values, std::true_type) {
  221. return 0;
  222. }
  223. static unsigned getHashValue(const std::tuple<Ts...> &values) {
  224. std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  225. return getHashValueImpl<0>(values, atEnd);
  226. }
  227. template <unsigned I>
  228. static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
  229. using EltType = typename std::tuple_element<I, Tuple>::type;
  230. std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  231. return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
  232. isEqualImpl<I + 1>(lhs, rhs, atEnd);
  233. }
  234. template <unsigned I>
  235. static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::true_type) {
  236. return true;
  237. }
  238. static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
  239. std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  240. return isEqualImpl<0>(lhs, rhs, atEnd);
  241. }
  242. };
  243. // Provide DenseMapInfo for StringRefs.
  244. template <> struct DenseMapInfo<StringRef> {
  245. static inline StringRef getEmptyKey() {
  246. return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)),
  247. 0);
  248. }
  249. static inline StringRef getTombstoneKey() {
  250. return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)),
  251. 0);
  252. }
  253. static unsigned getHashValue(StringRef Val) {
  254. assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
  255. assert(Val.data() != getTombstoneKey().data() &&
  256. "Cannot hash the tombstone key!");
  257. return (unsigned)(hash_value(Val));
  258. }
  259. static bool isEqual(StringRef LHS, StringRef RHS) {
  260. if (RHS.data() == getEmptyKey().data())
  261. return LHS.data() == getEmptyKey().data();
  262. if (RHS.data() == getTombstoneKey().data())
  263. return LHS.data() == getTombstoneKey().data();
  264. return LHS == RHS;
  265. }
  266. };
  267. // Provide DenseMapInfo for ArrayRefs.
  268. template <typename T> struct DenseMapInfo<ArrayRef<T>> {
  269. static inline ArrayRef<T> getEmptyKey() {
  270. return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)),
  271. size_t(0));
  272. }
  273. static inline ArrayRef<T> getTombstoneKey() {
  274. return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)),
  275. size_t(0));
  276. }
  277. static unsigned getHashValue(ArrayRef<T> Val) {
  278. assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
  279. assert(Val.data() != getTombstoneKey().data() &&
  280. "Cannot hash the tombstone key!");
  281. return (unsigned)(hash_value(Val));
  282. }
  283. static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
  284. if (RHS.data() == getEmptyKey().data())
  285. return LHS.data() == getEmptyKey().data();
  286. if (RHS.data() == getTombstoneKey().data())
  287. return LHS.data() == getTombstoneKey().data();
  288. return LHS == RHS;
  289. }
  290. };
  291. template <> struct DenseMapInfo<hash_code> {
  292. static inline hash_code getEmptyKey() { return hash_code(-1); }
  293. static inline hash_code getTombstoneKey() { return hash_code(-2); }
  294. static unsigned getHashValue(hash_code val) { return val; }
  295. static bool isEqual(hash_code LHS, hash_code RHS) { return LHS == RHS; }
  296. };
  297. /// Provide DenseMapInfo for APInt.
  298. template <> struct DenseMapInfo<APInt> {
  299. static inline APInt getEmptyKey() {
  300. APInt V(nullptr, 0);
  301. V.U.VAL = 0;
  302. return V;
  303. }
  304. static inline APInt getTombstoneKey() {
  305. APInt V(nullptr, 0);
  306. V.U.VAL = 1;
  307. return V;
  308. }
  309. static unsigned getHashValue(const APInt &Key) {
  310. return static_cast<unsigned>(hash_value(Key));
  311. }
  312. static bool isEqual(const APInt &LHS, const APInt &RHS) {
  313. return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
  314. }
  315. };
  316. /// Provide DenseMapInfo for APSInt, using the DenseMapInfo for APInt.
  317. template <> struct DenseMapInfo<APSInt> {
  318. static inline APSInt getEmptyKey() {
  319. return APSInt(DenseMapInfo<APInt>::getEmptyKey());
  320. }
  321. static inline APSInt getTombstoneKey() {
  322. return APSInt(DenseMapInfo<APInt>::getTombstoneKey());
  323. }
  324. static unsigned getHashValue(const APSInt &Key) {
  325. return static_cast<unsigned>(hash_value(Key));
  326. }
  327. static bool isEqual(const APSInt &LHS, const APSInt &RHS) {
  328. return LHS.getBitWidth() == RHS.getBitWidth() &&
  329. LHS.isUnsigned() == RHS.isUnsigned() && LHS == RHS;
  330. }
  331. };
  332. } // end namespace llvm
  333. #endif // LLVM_ADT_DENSEMAPINFO_H
  334. #ifdef __GNUC__
  335. #pragma GCC diagnostic pop
  336. #endif