Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_COMPILER_NODE_CACHE_H_
6 : #define V8_COMPILER_NODE_CACHE_H_
7 :
8 : #include "src/base/export-template.h"
9 : #include "src/base/functional.h"
10 : #include "src/base/macros.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : // Forward declarations.
16 : class Zone;
17 : template <typename>
18 : class ZoneVector;
19 :
20 :
21 : namespace compiler {
22 :
23 : // Forward declarations.
24 : class Node;
25 :
26 :
27 : // A cache for nodes based on a key. Useful for implementing canonicalization of
28 : // nodes such as constants, parameters, etc.
29 : template <typename Key, typename Hash = base::hash<Key>,
30 : typename Pred = std::equal_to<Key> >
31 : class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) NodeCache final {
32 : public:
33 0 : explicit NodeCache(unsigned max = 256)
34 4289190 : : entries_(nullptr), size_(0), max_(max) {}
35 : ~NodeCache() = default;
36 :
37 : // Search for node associated with {key} and return a pointer to a memory
38 : // location in this cache that stores an entry for the key. If the location
39 : // returned by this method contains a non-nullptr node, the caller can use
40 : // that
41 : // node. Otherwise it is the responsibility of the caller to fill the entry
42 : // with a new node.
43 : // Note that a previous cache entry may be overwritten if the cache becomes
44 : // too full or encounters too many hash collisions.
45 : Node** Find(Zone* zone, Key key);
46 :
47 : // Appends all nodes from this cache to {nodes}.
48 : void GetCachedNodes(ZoneVector<Node*>* nodes);
49 :
50 : private:
51 : struct Entry;
52 :
53 : Entry* entries_; // lazily-allocated hash entries.
54 : size_t size_;
55 : size_t max_;
56 : Hash hash_;
57 : Pred pred_;
58 :
59 : bool Resize(Zone* zone);
60 :
61 : DISALLOW_COPY_AND_ASSIGN(NodeCache);
62 : };
63 :
64 : // Various default cache types.
65 : using Int32NodeCache = NodeCache<int32_t>;
66 : using Int64NodeCache = NodeCache<int64_t>;
67 :
68 : // All we want is the numeric value of the RelocInfo::Mode enum. We typedef
69 : // below to avoid pulling in assembler.h
70 : using RelocInfoMode = char;
71 : using RelocInt32Key = std::pair<int32_t, RelocInfoMode>;
72 : using RelocInt64Key = std::pair<int64_t, RelocInfoMode>;
73 : using RelocInt32NodeCache = NodeCache<RelocInt32Key>;
74 : using RelocInt64NodeCache = NodeCache<RelocInt64Key>;
75 : #if V8_HOST_ARCH_32_BIT
76 : using IntPtrNodeCache = Int32NodeCache;
77 : #else
78 : using IntPtrNodeCache = Int64NodeCache;
79 : #endif
80 :
81 : // Explicit instantiation declarations.
82 : extern template class EXPORT_TEMPLATE_DECLARE(
83 : V8_EXPORT_PRIVATE) NodeCache<int32_t>;
84 : extern template class EXPORT_TEMPLATE_DECLARE(
85 : V8_EXPORT_PRIVATE) NodeCache<int64_t>;
86 :
87 : extern template class EXPORT_TEMPLATE_DECLARE(
88 : V8_EXPORT_PRIVATE) NodeCache<RelocInt32Key>;
89 : extern template class EXPORT_TEMPLATE_DECLARE(
90 : V8_EXPORT_PRIVATE) NodeCache<RelocInt64Key>;
91 :
92 : } // namespace compiler
93 : } // namespace internal
94 : } // namespace v8
95 :
96 : #endif // V8_COMPILER_NODE_CACHE_H_
|