1 // Copyright 2015 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_STATE_VALUES_UTILS_H_
6 #define V8_COMPILER_STATE_VALUES_UTILS_H_
7 
8 #include "src/compiler/js-graph.h"
9 #include "src/globals.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 namespace compiler {
15 
16 class Graph;
17 
18 class V8_EXPORT_PRIVATE StateValuesCache {
19  public:
20   explicit StateValuesCache(JSGraph* js_graph);
21 
22   Node* GetNodeForValues(Node** values, size_t count);
23 
24  private:
25   static const size_t kMaxInputCount = 8;
26 
27   struct NodeKey {
28     Node* node;
29 
NodeKeyNodeKey30     explicit NodeKey(Node* node) : node(node) {}
31   };
32 
33   struct StateValuesKey : public NodeKey {
34     // ValueArray - array of nodes ({node} has to be nullptr).
35     size_t count;
36     Node** values;
37 
StateValuesKeyStateValuesKey38     StateValuesKey(size_t count, Node** values)
39         : NodeKey(nullptr), count(count), values(values) {}
40   };
41 
42   class ValueArrayIterator;
43 
44   static bool AreKeysEqual(void* key1, void* key2);
45   static bool IsKeysEqualToNode(StateValuesKey* key, Node* node);
46   static bool AreValueKeysEqual(StateValuesKey* key1, StateValuesKey* key2);
47 
48   Node* BuildTree(ValueArrayIterator* it, size_t max_height);
49   NodeVector* GetWorkingSpace(size_t level);
50   Node* GetEmptyStateValues();
51   Node* GetValuesNodeFromCache(Node** nodes, size_t count);
52 
graph()53   Graph* graph() { return js_graph_->graph(); }
common()54   CommonOperatorBuilder* common() { return js_graph_->common(); }
55 
zone()56   Zone* zone() { return graph()->zone(); }
57 
58   JSGraph* js_graph_;
59   CustomMatcherZoneHashMap hash_map_;
60   ZoneVector<NodeVector*> working_space_;  // One working space per level.
61   Node* empty_state_values_;
62 };
63 
64 class V8_EXPORT_PRIVATE StateValuesAccess {
65  public:
66   struct TypedNode {
67     Node* node;
68     MachineType type;
TypedNodeTypedNode69     TypedNode(Node* node, MachineType type) : node(node), type(type) {}
70   };
71 
72   class V8_EXPORT_PRIVATE iterator {
73    public:
74     // Bare minimum of operators needed for range iteration.
75     bool operator!=(iterator& other);
76     iterator& operator++();
77     TypedNode operator*();
78 
79    private:
80     friend class StateValuesAccess;
81 
iterator()82     iterator() : current_depth_(-1) {}
83     explicit iterator(Node* node);
84 
85     Node* node();
86     MachineType type();
87     bool done();
88     void Advance();
89 
90     struct StatePos {
91       Node* node;
92       int index;
93 
StatePosStatePos94       explicit StatePos(Node* node) : node(node), index(0) {}
StatePosStatePos95       StatePos() {}
96     };
97 
98     StatePos* Top();
99     void Push(Node* node);
100     void Pop();
101 
102     static const int kMaxInlineDepth = 8;
103     StatePos stack_[kMaxInlineDepth];
104     int current_depth_;
105   };
106 
StateValuesAccess(Node * node)107   explicit StateValuesAccess(Node* node) : node_(node) {}
108 
109   size_t size();
begin()110   iterator begin() { return iterator(node_); }
end()111   iterator end() { return iterator(); }
112 
113  private:
114   Node* node_;
115 };
116 
117 }  // namespace compiler
118 }  // namespace internal
119 }  // namespace v8
120 
121 #endif  // V8_COMPILER_STATE_VALUES_UTILS_H_
122