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/functional.h" 9 #include "src/base/macros.h" 10 11 namespace v8 { 12 namespace internal { 13 14 // Forward declarations. 15 class Zone; 16 template <typename> 17 class ZoneVector; 18 19 20 namespace compiler { 21 22 // Forward declarations. 23 class Node; 24 25 26 // A cache for nodes based on a key. Useful for implementing canonicalization of 27 // nodes such as constants, parameters, etc. 28 template <typename Key, typename Hash = base::hash<Key>, 29 typename Pred = std::equal_to<Key> > 30 class NodeCache final { 31 public: 32 explicit NodeCache(unsigned max = 256) entries_(nullptr)33 : entries_(nullptr), size_(0), max_(max) {} ~NodeCache()34 ~NodeCache() {} 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-nullptr node, the caller can use 39 // that 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 46 // Appends all nodes from this cache to {nodes}. 47 void GetCachedNodes(ZoneVector<Node*>* nodes); 48 49 private: 50 struct Entry; 51 52 Entry* entries_; // lazily-allocated hash entries. 53 size_t size_; 54 size_t max_; 55 Hash hash_; 56 Pred pred_; 57 58 bool Resize(Zone* zone); 59 60 DISALLOW_COPY_AND_ASSIGN(NodeCache); 61 }; 62 63 // Various default cache types. 64 typedef NodeCache<int32_t> Int32NodeCache; 65 typedef NodeCache<int64_t> Int64NodeCache; 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; 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 83 84 #endif // V8_COMPILER_NODE_CACHE_H_ 85