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