• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/export-template.h"
9 #include "src/base/functional.h"
10 #include "src/base/macros.h"
11 #include "src/zone/zone-containers.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Forward declarations.
17 class Zone;
18 template <typename>
19 class ZoneVector;
20 
21 
22 namespace compiler {
23 
24 // Forward declarations.
25 class Node;
26 
27 
28 // A cache for nodes based on a key. Useful for implementing canonicalization of
29 // nodes such as constants, parameters, etc.
30 template <typename Key, typename Hash = base::hash<Key>,
31           typename Pred = std::equal_to<Key> >
EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)32 class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) NodeCache final {
33  public:
34   explicit NodeCache(Zone* zone) : map_(zone) {}
35   ~NodeCache() = default;
36   NodeCache(const NodeCache&) = delete;
37   NodeCache& operator=(const NodeCache&) = delete;
38 
39   // Search for node associated with {key} and return a pointer to a memory
40   // location in this cache that stores an entry for the key. If the location
41   // returned by this method contains a non-nullptr node, the caller can use
42   // that node. Otherwise it is the responsibility of the caller to fill the
43   // entry with a new node.
44   Node** Find(Key key) { return &(map_[key]); }
45 
46   // Appends all nodes from this cache to {nodes}.
47   void GetCachedNodes(ZoneVector<Node*>* nodes) {
48     for (const auto& entry : map_) {
49       if (entry.second) nodes->push_back(entry.second);
50     }
51   }
52 
53  private:
54   ZoneUnorderedMap<Key, Node*, Hash, Pred> map_;
55 };
56 
57 // Various default cache types.
58 using Int32NodeCache = NodeCache<int32_t>;
59 using Int64NodeCache = NodeCache<int64_t>;
60 
61 // All we want is the numeric value of the RelocInfo::Mode enum. We typedef
62 // below to avoid pulling in assembler.h
63 using RelocInfoMode = char;
64 using RelocInt32Key = std::pair<int32_t, RelocInfoMode>;
65 using RelocInt64Key = std::pair<int64_t, RelocInfoMode>;
66 using RelocInt32NodeCache = NodeCache<RelocInt32Key>;
67 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 }  // namespace compiler
75 }  // namespace internal
76 }  // namespace v8
77 
78 #endif  // V8_COMPILER_NODE_CACHE_H_
79