node-cache.h 2.28 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
#include "src/base/export-template.h"
9 10
#include "src/base/functional.h"
#include "src/base/macros.h"
11
#include "src/zone/zone-containers.h"
12 13 14

namespace v8 {
namespace internal {
15 16 17 18 19 20 21

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


22 23
namespace compiler {

24 25 26 27
// Forward declarations.
class Node;


28 29
// A cache for nodes based on a key. Useful for implementing canonicalization of
// nodes such as constants, parameters, etc.
30 31
template <typename Key, typename Hash = base::hash<Key>,
          typename Pred = std::equal_to<Key> >
32
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) NodeCache final {
33
 public:
34
  explicit NodeCache(Zone* zone) : map_(zone) {}
35
  ~NodeCache() = default;
36 37 38

  // 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
39
  // returned by this method contains a non-nullptr node, the caller can use
40 41 42
  // that node. Otherwise it is the responsibility of the caller to fill the
  // entry with a new node.
  Node** Find(Key key) { return &(map_[key]); }
43

44
  // Appends all nodes from this cache to {nodes}.
45 46 47 48 49
  void GetCachedNodes(ZoneVector<Node*>* nodes) {
    for (const auto& entry : map_) {
      if (entry.second) nodes->push_back(entry.second);
    }
  }
50

51
 private:
52
  ZoneUnorderedMap<Key, Node*, Hash, Pred> map_;
53 54

  DISALLOW_COPY_AND_ASSIGN(NodeCache);
55 56 57
};

// Various default cache types.
58 59
using Int32NodeCache = NodeCache<int32_t>;
using Int64NodeCache = NodeCache<int64_t>;
60 61 62

// All we want is the numeric value of the RelocInfo::Mode enum. We typedef
// below to avoid pulling in assembler.h
63 64 65 66 67
using RelocInfoMode = char;
using RelocInt32Key = std::pair<int32_t, RelocInfoMode>;
using RelocInt64Key = std::pair<int64_t, RelocInfoMode>;
using RelocInt32NodeCache = NodeCache<RelocInt32Key>;
using RelocInt64NodeCache = NodeCache<RelocInt64Key>;
68
#if V8_HOST_ARCH_32_BIT
69
using IntPtrNodeCache = Int32NodeCache;
70
#else
71
using IntPtrNodeCache = Int64NodeCache;
72
#endif
73 74 75 76

}  // namespace compiler
}  // namespace internal
}  // namespace v8
77 78

#endif  // V8_COMPILER_NODE_CACHE_H_