blob: 7063a3b0b49b2e882dac9f612eec7058b019718f [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/base/functional.h"
9#include "src/base/macros.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010
11namespace v8 {
12namespace internal {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013
14// Forward declarations.
15class Zone;
16template <typename>
17class ZoneVector;
18
19
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020namespace compiler {
21
Emily Bernierd0a1eb72015-03-24 16:35:39 -040022// Forward declarations.
23class Node;
24
25
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026// A cache for nodes based on a key. Useful for implementing canonicalization of
27// nodes such as constants, parameters, etc.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028template <typename Key, typename Hash = base::hash<Key>,
29 typename Pred = std::equal_to<Key> >
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030class NodeCache final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040032 explicit NodeCache(unsigned max = 256)
33 : entries_(nullptr), size_(0), max_(max) {}
34 ~NodeCache() {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035
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 Murdoch4a90d5f2016-03-22 12:00:34 +000038 // returned by this method contains a non-nullptr node, the caller can use
39 // that
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040 // 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 Bernierd0a1eb72015-03-24 16:35:39 -040046 // Appends all nodes from this cache to {nodes}.
47 void GetCachedNodes(ZoneVector<Node*>* nodes);
48
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040050 struct Entry;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051
52 Entry* entries_; // lazily-allocated hash entries.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040053 size_t size_;
54 size_t max_;
55 Hash hash_;
56 Pred pred_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057
58 bool Resize(Zone* zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059
60 DISALLOW_COPY_AND_ASSIGN(NodeCache);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061};
62
63// Various default cache types.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064typedef NodeCache<int32_t> Int32NodeCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065typedef NodeCache<int64_t> Int64NodeCache;
Ben Murdoch61f157c2016-09-16 13:49:30 +010066
67// All we want is the numeric value of the RelocInfo::Mode enum. We typedef
68// below to avoid pulling in assembler.h
69typedef char RelocInfoMode;
70typedef std::pair<int32_t, RelocInfoMode> RelocInt32Key;
71typedef std::pair<int64_t, RelocInfoMode> RelocInt64Key;
72typedef NodeCache<RelocInt32Key> RelocInt32NodeCache;
73typedef NodeCache<RelocInt64Key> RelocInt64NodeCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074#if V8_HOST_ARCH_32_BIT
75typedef Int32NodeCache IntPtrNodeCache;
76#else
77typedef Int64NodeCache IntPtrNodeCache;
78#endif
79
80} // namespace compiler
81} // namespace internal
82} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083
84#endif // V8_COMPILER_NODE_CACHE_H_