LLVM 22.0.0git
HashKeyMap.h
Go to the documentation of this file.
1//===--- HashKeyMap.h - Wrapper for maps using hash value key ---*- 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///
11/// Defines HashKeyMap template.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_PROFILEDATA_HASHKEYMAP_H
16#define LLVM_PROFILEDATA_HASHKEYMAP_H
17
18#include "llvm/ADT/Hashing.h"
19#include <utility>
20
21namespace llvm {
22
23namespace sampleprof {
24
25/// This class is a wrapper to associative container MapT<KeyT, ValueT> using
26/// the hash value of the original key as the new key. This greatly improves the
27/// performance of insert and query operations especially when hash values of
28/// keys are available a priori, and reduces memory usage if KeyT has a large
29/// size.
30/// All keys with the same hash value are considered equivalent (i.e. hash
31/// collision is silently ignored). Given such feature this class should only be
32/// used where it does not affect compilation correctness, for example, when
33/// loading a sample profile. The original key is not stored, so if the user
34/// needs to preserve it, it should be stored in the mapped type.
35/// Assuming the hashing algorithm is uniform, we use the formula
36/// 1 - Permute(n, k) / n ^ k where n is the universe size and k is number of
37/// elements chosen at random to calculate the probability of collision. With
38/// 1,000,000 entries the probability is negligible:
39/// 1 - (2^64)!/((2^64-1000000)!*(2^64)^1000000) ~= 3*10^-8.
40/// Source: https://en.wikipedia.org/wiki/Birthday_problem
41///
42/// \param MapT The underlying associative container type.
43/// \param KeyT The original key type, which requires the implementation of
44/// llvm::hash_value(KeyT).
45/// \param ValueT The original mapped type, which has the same requirement as
46/// the underlying container.
47/// \param MapTArgs Additional template parameters passed to the underlying
48/// container.
49template <template <typename, typename, typename...> typename MapT,
50 typename KeyT, typename ValueT, typename... MapTArgs>
52 public MapT<decltype(hash_value(KeyT())), ValueT, MapTArgs...> {
53public:
54 using base_type = MapT<decltype(hash_value(KeyT())), ValueT, MapTArgs...>;
55 using key_type = decltype(hash_value(KeyT()));
57 using mapped_type = ValueT;
58 using value_type = typename base_type::value_type;
59
60 using iterator = typename base_type::iterator;
61 using const_iterator = typename base_type::const_iterator;
62
63 template <typename... Ts>
64 std::pair<iterator, bool> try_emplace(const key_type &Hash,
66 Ts &&...Args) {
67 assert(Hash == hash_value(Key));
68 return base_type::try_emplace(Hash, std::forward<Ts>(Args)...);
69 }
70
71 template <typename... Ts>
72 std::pair<iterator, bool> try_emplace(const original_key_type &Key,
73 Ts &&...Args) {
74 return try_emplace(hash_value(Key), Key, std::forward<Ts>(Args)...);
75 }
76
77 template <typename... Ts> std::pair<iterator, bool> emplace(Ts &&...Args) {
78 return try_emplace(std::forward<Ts>(Args)...);
79 }
80
82 return try_emplace(Key, mapped_type()).first->second;
83 }
84
86 auto It = base_type::find(hash_value(Key));
87 if (It != base_type::end())
88 return It;
89 return base_type::end();
90 }
91
93 auto It = base_type::find(hash_value(Key));
94 if (It != base_type::end())
95 return It;
96 return base_type::end();
97 }
98
100 auto It = base_type::find(hash_value(Key));
101 if (It != base_type::end())
102 return It->second;
103 return mapped_type();
104 }
105
106 size_t count(const original_key_type &Key) const {
107 return base_type::count(hash_value(Key));
108 }
109
110 size_t erase(const original_key_type &Ctx) {
111 auto It = find(Ctx);
112 if (It != base_type::end()) {
113 base_type::erase(It);
114 return 1;
115 }
116 return 0;
117 }
118
120 return base_type::erase(It);
121 }
122};
123
124}
125
126}
127
128#endif // LLVM_PROFILEDATA_HASHKEYMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This class is a wrapper to associative container MapT<KeyT, ValueT> using the hash value of the origi...
Definition HashKeyMap.h:52
MapT< decltype(hash_value(KeyT())), ValueT, MapTArgs... > base_type
Definition HashKeyMap.h:54
std::pair< iterator, bool > emplace(Ts &&...Args)
Definition HashKeyMap.h:77
std::pair< iterator, bool > try_emplace(const key_type &Hash, const original_key_type &Key, Ts &&...Args)
Definition HashKeyMap.h:64
typename base_type::iterator iterator
Definition HashKeyMap.h:60
const_iterator find(const original_key_type &Key) const
Definition HashKeyMap.h:92
typename base_type::value_type value_type
Definition HashKeyMap.h:58
std::pair< iterator, bool > try_emplace(const original_key_type &Key, Ts &&...Args)
Definition HashKeyMap.h:72
decltype(hash_value(KeyT())) key_type
Definition HashKeyMap.h:55
size_t erase(const original_key_type &Ctx)
Definition HashKeyMap.h:110
mapped_type & operator[](const original_key_type &Key)
Definition HashKeyMap.h:81
size_t count(const original_key_type &Key) const
Definition HashKeyMap.h:106
iterator find(const original_key_type &Key)
Definition HashKeyMap.h:85
mapped_type lookup(const original_key_type &Key) const
Definition HashKeyMap.h:99
typename base_type::const_iterator const_iterator
Definition HashKeyMap.h:61
iterator erase(const_iterator It)
Definition HashKeyMap.h:119
uint64_t hash_value(const FunctionId &Obj)
Definition FunctionId.h:171
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key