blob: b12392239123e836a3e7699e81c63182112337f4 [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> >
30class 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
38 // returned by this method contains a non-NULL node, the caller can use that
39 // node. Otherwise it is the responsibility of the caller to fill the entry
40 // with a new node.
41 // Note that a previous cache entry may be overwritten if the cache becomes
42 // too full or encounters too many hash collisions.
43 Node** Find(Zone* zone, Key key);
44
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045 // Appends all nodes from this cache to {nodes}.
46 void GetCachedNodes(ZoneVector<Node*>* nodes);
47
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 struct Entry;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050
51 Entry* entries_; // lazily-allocated hash entries.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052 size_t size_;
53 size_t max_;
54 Hash hash_;
55 Pred pred_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056
57 bool Resize(Zone* zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040058
59 DISALLOW_COPY_AND_ASSIGN(NodeCache);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060};
61
62// Various default cache types.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063typedef NodeCache<int32_t> Int32NodeCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040064typedef NodeCache<int64_t> Int64NodeCache;
65#if V8_HOST_ARCH_32_BIT
66typedef Int32NodeCache IntPtrNodeCache;
67#else
68typedef Int64NodeCache IntPtrNodeCache;
69#endif
70
71} // namespace compiler
72} // namespace internal
73} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074
75#endif // V8_COMPILER_NODE_CACHE_H_