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> > |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 | // returned by this method contains a non-nullptr node, the caller can use |
| 39 | // that |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | // node. Otherwise it is the responsibility of the caller to fill the entry |
| 41 | // with a new node. |
| 42 | // Note that a previous cache entry may be overwritten if the cache becomes |
| 43 | // too full or encounters too many hash collisions. |
| 44 | Node** Find(Zone* zone, Key key); |
| 45 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 46 | // Appends all nodes from this cache to {nodes}. |
| 47 | void GetCachedNodes(ZoneVector<Node*>* nodes); |
| 48 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 50 | struct Entry; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | |
| 52 | Entry* entries_; // lazily-allocated hash entries. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 53 | size_t size_; |
| 54 | size_t max_; |
| 55 | Hash hash_; |
| 56 | Pred pred_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | |
| 58 | bool Resize(Zone* zone); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(NodeCache); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // Various default cache types. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | typedef NodeCache<int32_t> Int32NodeCache; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 65 | typedef NodeCache<int64_t> Int64NodeCache; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 66 | |
| 67 | // All we want is the numeric value of the RelocInfo::Mode enum. We typedef |
| 68 | // below to avoid pulling in assembler.h |
| 69 | typedef char RelocInfoMode; |
| 70 | typedef std::pair<int32_t, RelocInfoMode> RelocInt32Key; |
| 71 | typedef std::pair<int64_t, RelocInfoMode> RelocInt64Key; |
| 72 | typedef NodeCache<RelocInt32Key> RelocInt32NodeCache; |
| 73 | typedef NodeCache<RelocInt64Key> RelocInt64NodeCache; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 74 | #if V8_HOST_ARCH_32_BIT |
| 75 | typedef Int32NodeCache IntPtrNodeCache; |
| 76 | #else |
| 77 | typedef Int64NodeCache IntPtrNodeCache; |
| 78 | #endif |
| 79 | |
| 80 | } // namespace compiler |
| 81 | } // namespace internal |
| 82 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | |
| 84 | #endif // V8_COMPILER_NODE_CACHE_H_ |