Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / compiler / node-cache.h
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 #include "src/compiler/node.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
16 // A cache for nodes based on a key. Useful for implementing canonicalization of
17 // nodes such as constants, parameters, etc.
18 template <typename Key, typename Hash = base::hash<Key>,
19           typename Pred = std::equal_to<Key> >
20 class NodeCache FINAL {
21  public:
22   explicit NodeCache(size_t max = 256)
23       : entries_(nullptr), size_(0), max_(max) {}
24
25   // Search for node associated with {key} and return a pointer to a memory
26   // location in this cache that stores an entry for the key. If the location
27   // returned by this method contains a non-NULL node, the caller can use that
28   // node. Otherwise it is the responsibility of the caller to fill the entry
29   // with a new node.
30   // Note that a previous cache entry may be overwritten if the cache becomes
31   // too full or encounters too many hash collisions.
32   Node** Find(Zone* zone, Key key);
33
34   void GetCachedNodes(NodeVector* nodes);
35
36  private:
37   enum { kInitialSize = 16u, kLinearProbe = 5u };
38
39   struct Entry;
40
41   Entry* entries_;  // lazily-allocated hash entries.
42   size_t size_;
43   size_t max_;
44   Hash hash_;
45   Pred pred_;
46
47   bool Resize(Zone* zone);
48 };
49
50 // Various default cache types.
51 typedef NodeCache<int64_t> Int64NodeCache;
52 typedef NodeCache<int32_t> Int32NodeCache;
53 typedef NodeCache<void*> PtrNodeCache;
54
55 }  // namespace compiler
56 }  // namespace internal
57 }  // namespace v8
58
59 #endif  // V8_COMPILER_NODE_CACHE_H_