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#include "src/zone/zone-containers.h" 12 13namespace v8 { 14namespace internal { 15 16// Forward declarations. 17class Zone; 18template <typename> 19class ZoneVector; 20 21 22namespace compiler { 23 24// Forward declarations. 25class Node; 26 27 28// A cache for nodes based on a key. Useful for implementing canonicalization of 29// nodes such as constants, parameters, etc. 30template <typename Key, typename Hash = base::hash<Key>, 31 typename Pred = std::equal_to<Key> > 32class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) NodeCache final { 33 public: 34 explicit NodeCache(Zone* zone) : map_(zone) {} 35 ~NodeCache() = default; 36 NodeCache(const NodeCache&) = delete; 37 NodeCache& operator=(const NodeCache&) = delete; 38 39 // Search for node associated with {key} and return a pointer to a memory 40 // location in this cache that stores an entry for the key. If the location 41 // returned by this method contains a non-nullptr node, the caller can use 42 // that node. Otherwise it is the responsibility of the caller to fill the 43 // entry with a new node. 44 Node** Find(Key key) { return &(map_[key]); } 45 46 // Appends all nodes from this cache to {nodes}. 47 void GetCachedNodes(ZoneVector<Node*>* nodes) { 48 for (const auto& entry : map_) { 49 if (entry.second) nodes->push_back(entry.second); 50 } 51 } 52 53 private: 54 ZoneUnorderedMap<Key, Node*, Hash, Pred> map_; 55}; 56 57// Various default cache types. 58using Int32NodeCache = NodeCache<int32_t>; 59using Int64NodeCache = NodeCache<int64_t>; 60 61// All we want is the numeric value of the RelocInfo::Mode enum. We typedef 62// below to avoid pulling in assembler.h 63using RelocInfoMode = char; 64using RelocInt32Key = std::pair<int32_t, RelocInfoMode>; 65using RelocInt64Key = std::pair<int64_t, RelocInfoMode>; 66using RelocInt32NodeCache = NodeCache<RelocInt32Key>; 67using RelocInt64NodeCache = NodeCache<RelocInt64Key>; 68#if V8_HOST_ARCH_32_BIT 69using IntPtrNodeCache = Int32NodeCache; 70#else 71using IntPtrNodeCache = Int64NodeCache; 72#endif 73 74} // namespace compiler 75} // namespace internal 76} // namespace v8 77 78#endif // V8_COMPILER_NODE_CACHE_H_ 79