node-cache.h 2.33 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

  // 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
38 39
  // returned by this method contains a non-nullptr node, the caller can use
  // that
40 41 42 43 44 45
  // 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);

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

49
 private:
50
  struct Entry;
51 52

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

  bool Resize(Zone* zone);
59 60

  DISALLOW_COPY_AND_ASSIGN(NodeCache);
61 62 63
};

// Various default cache types.
64
typedef NodeCache<int32_t> Int32NodeCache;
65
typedef NodeCache<int64_t> Int64NodeCache;
66 67 68 69 70 71 72 73

// All we want is the numeric value of the RelocInfo::Mode enum. We typedef
// below to avoid pulling in assembler.h
typedef char RelocInfoMode;
typedef std::pair<int32_t, RelocInfoMode> RelocInt32Key;
typedef std::pair<int64_t, RelocInfoMode> RelocInt64Key;
typedef NodeCache<RelocInt32Key> RelocInt32NodeCache;
typedef NodeCache<RelocInt64Key> RelocInt64NodeCache;
74 75 76 77 78
#if V8_HOST_ARCH_32_BIT
typedef Int32NodeCache IntPtrNodeCache;
#else
typedef Int64NodeCache IntPtrNodeCache;
#endif
79 80 81 82

}  // namespace compiler
}  // namespace internal
}  // namespace v8
83 84

#endif  // V8_COMPILER_NODE_CACHE_H_