node-cache.h 1.96 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_NODE_CACHE_H_
#define V8_COMPILER_NODE_CACHE_H_

8 9
#include "src/base/functional.h"
#include "src/base/macros.h"
10 11 12

namespace v8 {
namespace internal {
13 14 15 16 17 18 19

// Forward declarations.
class Zone;
template <typename>
class ZoneVector;


20 21
namespace compiler {

22 23 24 25
// Forward declarations.
class Node;


26 27
// A cache for nodes based on a key. Useful for implementing canonicalization of
// nodes such as constants, parameters, etc.
28 29
template <typename Key, typename Hash = base::hash<Key>,
          typename Pred = std::equal_to<Key> >
30
class NodeCache final {
31
 public:
32
  explicit NodeCache(unsigned max = 256)
33
      : entries_(nullptr), size_(0), max_(max) {}
34
  ~NodeCache() {}
35 36 37 38 39 40 41 42 43 44

  // Search for node associated with {key} and return a pointer to a memory
  // location in this cache that stores an entry for the key. If the location
  // returned by this method contains a non-NULL node, the caller can use that
  // node. Otherwise it is the responsibility of the caller to fill the entry
  // with a new node.
  // Note that a previous cache entry may be overwritten if the cache becomes
  // too full or encounters too many hash collisions.
  Node** Find(Zone* zone, Key key);

45 46
  // Appends all nodes from this cache to {nodes}.
  void GetCachedNodes(ZoneVector<Node*>* nodes);
47

48
 private:
49
  struct Entry;
50 51

  Entry* entries_;  // lazily-allocated hash entries.
52 53 54 55
  size_t size_;
  size_t max_;
  Hash hash_;
  Pred pred_;
56 57

  bool Resize(Zone* zone);
58 59

  DISALLOW_COPY_AND_ASSIGN(NodeCache);
60 61 62
};

// Various default cache types.
63
typedef NodeCache<int32_t> Int32NodeCache;
64 65 66 67 68 69
typedef NodeCache<int64_t> Int64NodeCache;
#if V8_HOST_ARCH_32_BIT
typedef Int32NodeCache IntPtrNodeCache;
#else
typedef Int64NodeCache IntPtrNodeCache;
#endif
70 71 72 73

}  // namespace compiler
}  // namespace internal
}  // namespace v8
74 75

#endif  // V8_COMPILER_NODE_CACHE_H_