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