Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. //===- llvm/ADT/DenseMapInfo.h - Type traits for DenseMap -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file defines DenseMapInfo traits for DenseMap.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13.  
  14. #ifndef LLVM_ADT_DENSEMAPINFO_H
  15. #define LLVM_ADT_DENSEMAPINFO_H
  16.  
  17. #include <cassert>
  18. #include <cstddef>
  19. #include <cstdint>
  20. #include <tuple>
  21. #include <type_traits>
  22. #include <utility>
  23. #include <variant>
  24.  
  25. namespace llvm {
  26.  
  27. namespace detail {
  28.  
  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.  
  43. } // end namespace detail
  44.  
  45. /// An information struct used to provide DenseMap with the various necessary
  46. /// components for a given value type `T`. `Enable` is an optional additional
  47. /// parameter that is used to support SFINAE (generally using std::enable_if_t)
  48. /// in derived DenseMapInfo specializations; in non-SFINAE use cases this should
  49. /// just be `void`.
  50. template<typename T, typename Enable = void>
  51. struct DenseMapInfo {
  52.   //static inline T getEmptyKey();
  53.   //static inline T getTombstoneKey();
  54.   //static unsigned getHashValue(const T &Val);
  55.   //static bool isEqual(const T &LHS, const T &RHS);
  56. };
  57.  
  58. // Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
  59. // that are aligned to alignof(T) bytes, but try to avoid requiring T to be
  60. // complete. This allows clients to instantiate DenseMap<T*, ...> with forward
  61. // declared key types. Assume that no pointer key type requires more than 4096
  62. // bytes of alignment.
  63. template<typename T>
  64. struct DenseMapInfo<T*> {
  65.   // The following should hold, but it would require T to be complete:
  66.   // static_assert(alignof(T) <= (1 << Log2MaxAlign),
  67.   //               "DenseMap does not support pointer keys requiring more than "
  68.   //               "Log2MaxAlign bits of alignment");
  69.   static constexpr uintptr_t Log2MaxAlign = 12;
  70.  
  71.   static inline T* getEmptyKey() {
  72.     uintptr_t Val = static_cast<uintptr_t>(-1);
  73.     Val <<= Log2MaxAlign;
  74.     return reinterpret_cast<T*>(Val);
  75.   }
  76.  
  77.   static inline T* getTombstoneKey() {
  78.     uintptr_t Val = static_cast<uintptr_t>(-2);
  79.     Val <<= Log2MaxAlign;
  80.     return reinterpret_cast<T*>(Val);
  81.   }
  82.  
  83.   static unsigned getHashValue(const T *PtrVal) {
  84.     return (unsigned((uintptr_t)PtrVal) >> 4) ^
  85.            (unsigned((uintptr_t)PtrVal) >> 9);
  86.   }
  87.  
  88.   static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
  89. };
  90.  
  91. // Provide DenseMapInfo for chars.
  92. template<> struct DenseMapInfo<char> {
  93.   static inline char getEmptyKey() { return ~0; }
  94.   static inline char getTombstoneKey() { return ~0 - 1; }
  95.   static unsigned getHashValue(const char& Val) { return Val * 37U; }
  96.  
  97.   static bool isEqual(const char &LHS, const char &RHS) {
  98.     return LHS == RHS;
  99.   }
  100. };
  101.  
  102. // Provide DenseMapInfo for unsigned chars.
  103. template <> struct DenseMapInfo<unsigned char> {
  104.   static inline unsigned char getEmptyKey() { return ~0; }
  105.   static inline unsigned char getTombstoneKey() { return ~0 - 1; }
  106.   static unsigned getHashValue(const unsigned char &Val) { return Val * 37U; }
  107.  
  108.   static bool isEqual(const unsigned char &LHS, const unsigned char &RHS) {
  109.     return LHS == RHS;
  110.   }
  111. };
  112.  
  113. // Provide DenseMapInfo for unsigned shorts.
  114. template <> struct DenseMapInfo<unsigned short> {
  115.   static inline unsigned short getEmptyKey() { return 0xFFFF; }
  116.   static inline unsigned short getTombstoneKey() { return 0xFFFF - 1; }
  117.   static unsigned getHashValue(const unsigned short &Val) { return Val * 37U; }
  118.  
  119.   static bool isEqual(const unsigned short &LHS, const unsigned short &RHS) {
  120.     return LHS == RHS;
  121.   }
  122. };
  123.  
  124. // Provide DenseMapInfo for unsigned ints.
  125. template<> struct DenseMapInfo<unsigned> {
  126.   static inline unsigned getEmptyKey() { return ~0U; }
  127.   static inline unsigned getTombstoneKey() { return ~0U - 1; }
  128.   static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  129.  
  130.   static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  131.     return LHS == RHS;
  132.   }
  133. };
  134.  
  135. // Provide DenseMapInfo for unsigned longs.
  136. template<> struct DenseMapInfo<unsigned long> {
  137.   static inline unsigned long getEmptyKey() { return ~0UL; }
  138.   static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  139.  
  140.   static unsigned getHashValue(const unsigned long& Val) {
  141.     return (unsigned)(Val * 37UL);
  142.   }
  143.  
  144.   static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  145.     return LHS == RHS;
  146.   }
  147. };
  148.  
  149. // Provide DenseMapInfo for unsigned long longs.
  150. template<> struct DenseMapInfo<unsigned long long> {
  151.   static inline unsigned long long getEmptyKey() { return ~0ULL; }
  152.   static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  153.  
  154.   static unsigned getHashValue(const unsigned long long& Val) {
  155.     return (unsigned)(Val * 37ULL);
  156.   }
  157.  
  158.   static bool isEqual(const unsigned long long& LHS,
  159.                       const unsigned long long& RHS) {
  160.     return LHS == RHS;
  161.   }
  162. };
  163.  
  164. // Provide DenseMapInfo for shorts.
  165. template <> struct DenseMapInfo<short> {
  166.   static inline short getEmptyKey() { return 0x7FFF; }
  167.   static inline short getTombstoneKey() { return -0x7FFF - 1; }
  168.   static unsigned getHashValue(const short &Val) { return Val * 37U; }
  169.   static bool isEqual(const short &LHS, const short &RHS) { return LHS == RHS; }
  170. };
  171.  
  172. // Provide DenseMapInfo for ints.
  173. template<> struct DenseMapInfo<int> {
  174.   static inline int getEmptyKey() { return 0x7fffffff; }
  175.   static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  176.   static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  177.  
  178.   static bool isEqual(const int& LHS, const int& RHS) {
  179.     return LHS == RHS;
  180.   }
  181. };
  182.  
  183. // Provide DenseMapInfo for longs.
  184. template<> struct DenseMapInfo<long> {
  185.   static inline long getEmptyKey() {
  186.     return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  187.   }
  188.  
  189.   static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  190.  
  191.   static unsigned getHashValue(const long& Val) {
  192.     return (unsigned)(Val * 37UL);
  193.   }
  194.  
  195.   static bool isEqual(const long& LHS, const long& RHS) {
  196.     return LHS == RHS;
  197.   }
  198. };
  199.  
  200. // Provide DenseMapInfo for long longs.
  201. template<> struct DenseMapInfo<long long> {
  202.   static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  203.   static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  204.  
  205.   static unsigned getHashValue(const long long& Val) {
  206.     return (unsigned)(Val * 37ULL);
  207.   }
  208.  
  209.   static bool isEqual(const long long& LHS,
  210.                       const long long& RHS) {
  211.     return LHS == RHS;
  212.   }
  213. };
  214.  
  215. // Provide DenseMapInfo for all pairs whose members have info.
  216. template<typename T, typename U>
  217. struct DenseMapInfo<std::pair<T, U>> {
  218.   using Pair = std::pair<T, U>;
  219.   using FirstInfo = DenseMapInfo<T>;
  220.   using SecondInfo = DenseMapInfo<U>;
  221.  
  222.   static inline Pair getEmptyKey() {
  223.     return std::make_pair(FirstInfo::getEmptyKey(),
  224.                           SecondInfo::getEmptyKey());
  225.   }
  226.  
  227.   static inline Pair getTombstoneKey() {
  228.     return std::make_pair(FirstInfo::getTombstoneKey(),
  229.                           SecondInfo::getTombstoneKey());
  230.   }
  231.  
  232.   static unsigned getHashValue(const Pair& PairVal) {
  233.     return detail::combineHashValue(FirstInfo::getHashValue(PairVal.first),
  234.                                     SecondInfo::getHashValue(PairVal.second));
  235.   }
  236.  
  237.   static bool isEqual(const Pair &LHS, const Pair &RHS) {
  238.     return FirstInfo::isEqual(LHS.first, RHS.first) &&
  239.            SecondInfo::isEqual(LHS.second, RHS.second);
  240.   }
  241. };
  242.  
  243. // Provide DenseMapInfo for all tuples whose members have info.
  244. template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
  245.   using Tuple = std::tuple<Ts...>;
  246.  
  247.   static inline Tuple getEmptyKey() {
  248.     return Tuple(DenseMapInfo<Ts>::getEmptyKey()...);
  249.   }
  250.  
  251.   static inline Tuple getTombstoneKey() {
  252.     return Tuple(DenseMapInfo<Ts>::getTombstoneKey()...);
  253.   }
  254.  
  255.   template <unsigned I>
  256.   static unsigned getHashValueImpl(const Tuple &values, std::false_type) {
  257.     using EltType = std::tuple_element_t<I, Tuple>;
  258.     std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  259.     return detail::combineHashValue(
  260.         DenseMapInfo<EltType>::getHashValue(std::get<I>(values)),
  261.         getHashValueImpl<I + 1>(values, atEnd));
  262.   }
  263.  
  264.   template <unsigned I>
  265.   static unsigned getHashValueImpl(const Tuple &, std::true_type) {
  266.     return 0;
  267.   }
  268.  
  269.   static unsigned getHashValue(const std::tuple<Ts...> &values) {
  270.     std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  271.     return getHashValueImpl<0>(values, atEnd);
  272.   }
  273.  
  274.   template <unsigned I>
  275.   static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::false_type) {
  276.     using EltType = std::tuple_element_t<I, Tuple>;
  277.     std::integral_constant<bool, I + 1 == sizeof...(Ts)> atEnd;
  278.     return DenseMapInfo<EltType>::isEqual(std::get<I>(lhs), std::get<I>(rhs)) &&
  279.            isEqualImpl<I + 1>(lhs, rhs, atEnd);
  280.   }
  281.  
  282.   template <unsigned I>
  283.   static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
  284.     return true;
  285.   }
  286.  
  287.   static bool isEqual(const Tuple &lhs, const Tuple &rhs) {
  288.     std::integral_constant<bool, 0 == sizeof...(Ts)> atEnd;
  289.     return isEqualImpl<0>(lhs, rhs, atEnd);
  290.   }
  291. };
  292.  
  293. // Provide DenseMapInfo for variants whose all alternatives have DenseMapInfo.
  294. template <typename... Ts> struct DenseMapInfo<std::variant<Ts...>> {
  295.   using Variant = std::variant<Ts...>;
  296.   using FirstT = std::variant_alternative_t<0, Variant>;
  297.  
  298.   static inline Variant getEmptyKey() {
  299.     return Variant(std::in_place_index<0>, DenseMapInfo<FirstT>::getEmptyKey());
  300.   }
  301.  
  302.   static inline Variant getTombstoneKey() {
  303.     return Variant(std::in_place_index<0>,
  304.                    DenseMapInfo<FirstT>::getTombstoneKey());
  305.   }
  306.  
  307.   static unsigned getHashValue(const Variant &Val) {
  308.     return std::visit(
  309.         [&Val](auto &&Alternative) {
  310.           using T = std::decay_t<decltype(Alternative)>;
  311.           // Include index in hash to make sure same value as different
  312.           // alternatives don't collide.
  313.           return detail::combineHashValue(
  314.               DenseMapInfo<size_t>::getHashValue(Val.index()),
  315.               DenseMapInfo<T>::getHashValue(Alternative));
  316.         },
  317.         Val);
  318.   }
  319.  
  320.   static bool isEqual(const Variant &LHS, const Variant &RHS) {
  321.     return LHS == RHS;
  322.   }
  323. };
  324. } // end namespace llvm
  325.  
  326. #endif // LLVM_ADT_DENSEMAPINFO_H
  327.