11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_COMPILER_NODE_CACHE_H_ 61cb0ef41Sopenharmony_ci#define V8_COMPILER_NODE_CACHE_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "src/base/export-template.h" 91cb0ef41Sopenharmony_ci#include "src/base/functional.h" 101cb0ef41Sopenharmony_ci#include "src/base/macros.h" 111cb0ef41Sopenharmony_ci#include "src/zone/zone-containers.h" 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cinamespace v8 { 141cb0ef41Sopenharmony_cinamespace internal { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci// Forward declarations. 171cb0ef41Sopenharmony_ciclass Zone; 181cb0ef41Sopenharmony_citemplate <typename> 191cb0ef41Sopenharmony_ciclass ZoneVector; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cinamespace compiler { 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci// Forward declarations. 251cb0ef41Sopenharmony_ciclass Node; 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci// A cache for nodes based on a key. Useful for implementing canonicalization of 291cb0ef41Sopenharmony_ci// nodes such as constants, parameters, etc. 301cb0ef41Sopenharmony_citemplate <typename Key, typename Hash = base::hash<Key>, 311cb0ef41Sopenharmony_ci typename Pred = std::equal_to<Key> > 321cb0ef41Sopenharmony_ciclass EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) NodeCache final { 331cb0ef41Sopenharmony_ci public: 341cb0ef41Sopenharmony_ci explicit NodeCache(Zone* zone) : map_(zone) {} 351cb0ef41Sopenharmony_ci ~NodeCache() = default; 361cb0ef41Sopenharmony_ci NodeCache(const NodeCache&) = delete; 371cb0ef41Sopenharmony_ci NodeCache& operator=(const NodeCache&) = delete; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci // Search for node associated with {key} and return a pointer to a memory 401cb0ef41Sopenharmony_ci // location in this cache that stores an entry for the key. If the location 411cb0ef41Sopenharmony_ci // returned by this method contains a non-nullptr node, the caller can use 421cb0ef41Sopenharmony_ci // that node. Otherwise it is the responsibility of the caller to fill the 431cb0ef41Sopenharmony_ci // entry with a new node. 441cb0ef41Sopenharmony_ci Node** Find(Key key) { return &(map_[key]); } 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci // Appends all nodes from this cache to {nodes}. 471cb0ef41Sopenharmony_ci void GetCachedNodes(ZoneVector<Node*>* nodes) { 481cb0ef41Sopenharmony_ci for (const auto& entry : map_) { 491cb0ef41Sopenharmony_ci if (entry.second) nodes->push_back(entry.second); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci private: 541cb0ef41Sopenharmony_ci ZoneUnorderedMap<Key, Node*, Hash, Pred> map_; 551cb0ef41Sopenharmony_ci}; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci// Various default cache types. 581cb0ef41Sopenharmony_ciusing Int32NodeCache = NodeCache<int32_t>; 591cb0ef41Sopenharmony_ciusing Int64NodeCache = NodeCache<int64_t>; 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci// All we want is the numeric value of the RelocInfo::Mode enum. We typedef 621cb0ef41Sopenharmony_ci// below to avoid pulling in assembler.h 631cb0ef41Sopenharmony_ciusing RelocInfoMode = char; 641cb0ef41Sopenharmony_ciusing RelocInt32Key = std::pair<int32_t, RelocInfoMode>; 651cb0ef41Sopenharmony_ciusing RelocInt64Key = std::pair<int64_t, RelocInfoMode>; 661cb0ef41Sopenharmony_ciusing RelocInt32NodeCache = NodeCache<RelocInt32Key>; 671cb0ef41Sopenharmony_ciusing RelocInt64NodeCache = NodeCache<RelocInt64Key>; 681cb0ef41Sopenharmony_ci#if V8_HOST_ARCH_32_BIT 691cb0ef41Sopenharmony_ciusing IntPtrNodeCache = Int32NodeCache; 701cb0ef41Sopenharmony_ci#else 711cb0ef41Sopenharmony_ciusing IntPtrNodeCache = Int64NodeCache; 721cb0ef41Sopenharmony_ci#endif 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci} // namespace compiler 751cb0ef41Sopenharmony_ci} // namespace internal 761cb0ef41Sopenharmony_ci} // namespace v8 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci#endif // V8_COMPILER_NODE_CACHE_H_ 79