OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_NODE_CACHE_H_ | 5 #ifndef V8_COMPILER_NODE_CACHE_H_ |
6 #define V8_COMPILER_NODE_CACHE_H_ | 6 #define V8_COMPILER_NODE_CACHE_H_ |
7 | 7 |
8 #include "src/base/functional.h" | 8 #include "src/base/functional.h" |
9 #include "src/base/macros.h" | 9 #include "src/base/macros.h" |
10 #include "src/compiler/node.h" | |
11 | 10 |
12 namespace v8 { | 11 namespace v8 { |
13 namespace internal { | 12 namespace internal { |
| 13 |
| 14 // Forward declarations. |
| 15 class Zone; |
| 16 template <typename> |
| 17 class ZoneVector; |
| 18 |
| 19 |
14 namespace compiler { | 20 namespace compiler { |
15 | 21 |
| 22 // Forward declarations. |
| 23 class Node; |
| 24 |
| 25 |
16 // A cache for nodes based on a key. Useful for implementing canonicalization of | 26 // A cache for nodes based on a key. Useful for implementing canonicalization of |
17 // nodes such as constants, parameters, etc. | 27 // nodes such as constants, parameters, etc. |
18 template <typename Key, typename Hash = base::hash<Key>, | 28 template <typename Key, typename Hash = base::hash<Key>, |
19 typename Pred = std::equal_to<Key> > | 29 typename Pred = std::equal_to<Key> > |
20 class NodeCache FINAL { | 30 class NodeCache FINAL { |
21 public: | 31 public: |
22 explicit NodeCache(size_t max = 256) | 32 explicit NodeCache(unsigned max = 256) |
23 : entries_(nullptr), size_(0), max_(max) {} | 33 : entries_(nullptr), size_(0), max_(max) {} |
| 34 ~NodeCache() {} |
24 | 35 |
25 // Search for node associated with {key} and return a pointer to a memory | 36 // Search for node associated with {key} and return a pointer to a memory |
26 // location in this cache that stores an entry for the key. If the location | 37 // location in this cache that stores an entry for the key. If the location |
27 // returned by this method contains a non-NULL node, the caller can use that | 38 // returned by this method contains a non-NULL node, the caller can use that |
28 // node. Otherwise it is the responsibility of the caller to fill the entry | 39 // node. Otherwise it is the responsibility of the caller to fill the entry |
29 // with a new node. | 40 // with a new node. |
30 // Note that a previous cache entry may be overwritten if the cache becomes | 41 // Note that a previous cache entry may be overwritten if the cache becomes |
31 // too full or encounters too many hash collisions. | 42 // too full or encounters too many hash collisions. |
32 Node** Find(Zone* zone, Key key); | 43 Node** Find(Zone* zone, Key key); |
33 | 44 |
34 void GetCachedNodes(NodeVector* nodes); | 45 // Appends all nodes from this cache to {nodes}. |
| 46 void GetCachedNodes(ZoneVector<Node*>* nodes); |
35 | 47 |
36 private: | 48 private: |
37 enum { kInitialSize = 16u, kLinearProbe = 5u }; | |
38 | |
39 struct Entry; | 49 struct Entry; |
40 | 50 |
41 Entry* entries_; // lazily-allocated hash entries. | 51 Entry* entries_; // lazily-allocated hash entries. |
42 size_t size_; | 52 size_t size_; |
43 size_t max_; | 53 size_t max_; |
44 Hash hash_; | 54 Hash hash_; |
45 Pred pred_; | 55 Pred pred_; |
46 | 56 |
47 bool Resize(Zone* zone); | 57 bool Resize(Zone* zone); |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(NodeCache); |
48 }; | 60 }; |
49 | 61 |
50 // Various default cache types. | 62 // Various default cache types. |
| 63 typedef NodeCache<int32_t> Int32NodeCache; |
51 typedef NodeCache<int64_t> Int64NodeCache; | 64 typedef NodeCache<int64_t> Int64NodeCache; |
52 typedef NodeCache<int32_t> Int32NodeCache; | 65 #if V8_HOST_ARCH_32_BIT |
53 typedef NodeCache<void*> PtrNodeCache; | 66 typedef Int32NodeCache IntPtrNodeCache; |
| 67 #else |
| 68 typedef Int64NodeCache IntPtrNodeCache; |
| 69 #endif |
54 | 70 |
55 } // namespace compiler | 71 } // namespace compiler |
56 } // namespace internal | 72 } // namespace internal |
57 } // namespace v8 | 73 } // namespace v8 |
58 | 74 |
59 #endif // V8_COMPILER_NODE_CACHE_H_ | 75 #endif // V8_COMPILER_NODE_CACHE_H_ |
OLD | NEW |