OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_GRAPH_REDUCER_H_ | 5 #ifndef V8_COMPILER_GRAPH_REDUCER_H_ |
6 #define V8_COMPILER_GRAPH_REDUCER_H_ | 6 #define V8_COMPILER_GRAPH_REDUCER_H_ |
7 | 7 |
8 #include "src/compiler/node-marker.h" | 8 #include "src/compiler/node-marker.h" |
9 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace compiler { | 13 namespace compiler { |
14 | 14 |
15 // Forward declarations. | 15 // Forward declarations. |
16 class Graph; | 16 class Graph; |
17 class Node; | 17 class Node; |
18 | 18 |
19 | 19 |
20 // NodeIds are identifying numbers for nodes that can be used to index auxiliary | |
21 // out-of-line data associated with each node. | |
22 typedef int32_t NodeId; | |
23 | |
24 | |
20 // Represents the result of trying to reduce a node in the graph. | 25 // Represents the result of trying to reduce a node in the graph. |
21 class Reduction final { | 26 class Reduction final { |
22 public: | 27 public: |
23 explicit Reduction(Node* replacement = NULL) : replacement_(replacement) {} | 28 explicit Reduction(Node* replacement = nullptr) : replacement_(replacement) {} |
24 | 29 |
25 Node* replacement() const { return replacement_; } | 30 Node* replacement() const { return replacement_; } |
26 bool Changed() const { return replacement() != NULL; } | 31 bool Changed() const { return replacement() != nullptr; } |
27 | 32 |
28 private: | 33 private: |
29 Node* replacement_; | 34 Node* replacement_; |
30 }; | 35 }; |
31 | 36 |
32 | 37 |
33 // A reducer can reduce or simplify a given node based on its operator and | 38 // A reducer can reduce or simplify a given node based on its operator and |
34 // inputs. This class functions as an extension point for the graph reducer for | 39 // inputs. This class functions as an extension point for the graph reducer for |
35 // language-specific reductions (e.g. reduction based on types or constant | 40 // language-specific reductions (e.g. reduction based on types or constant |
36 // folding of low-level operators) can be integrated into the graph reduction | 41 // folding of low-level operators) can be integrated into the graph reduction |
37 // phase. | 42 // phase. |
38 class Reducer { | 43 class Reducer { |
39 public: | 44 public: |
40 Reducer() {} | 45 class Observer { |
titzer
2015/05/06 08:57:40
As discussed, if we could move this out of the bas
Benedikt Meurer
2015/05/06 09:10:32
Done as discussed. However I kept the Finish metho
| |
46 public: | |
47 Observer() {} | |
48 virtual ~Observer() {} | |
49 | |
50 // Replace {node} with {replacement}. | |
51 virtual void Replace(Node* node, Node* replacement) = 0; | |
52 // Revisit the {node} again later. | |
53 virtual void Revisit(Node* node) = 0; | |
54 | |
55 private: | |
56 DISALLOW_COPY_AND_ASSIGN(Observer); | |
57 }; | |
58 | |
59 Reducer() : observer_(nullptr) {} | |
41 virtual ~Reducer() {} | 60 virtual ~Reducer() {} |
42 | 61 |
62 // Observe the actions of this reducer. | |
63 Observer* observer() const { return observer_; } | |
64 void set_observer(Observer* observer) { observer_ = observer; } | |
65 | |
43 // Try to reduce a node if possible. | 66 // Try to reduce a node if possible. |
44 virtual Reduction Reduce(Node* node) = 0; | 67 virtual Reduction Reduce(Node* node) = 0; |
45 | 68 |
69 // Ask this reducer to finish operation, returns {true} if the reducer is | |
70 // done, while {false} indicates that the graph might need to be reduced | |
71 // again. | |
72 virtual bool Finish(); | |
73 | |
46 // Helper functions for subclasses to produce reductions for a node. | 74 // Helper functions for subclasses to produce reductions for a node. |
47 static Reduction NoChange() { return Reduction(); } | 75 static Reduction NoChange() { return Reduction(); } |
48 static Reduction Replace(Node* node) { return Reduction(node); } | 76 static Reduction Replace(Node* node) { return Reduction(node); } |
49 static Reduction Changed(Node* node) { return Reduction(node); } | 77 static Reduction Changed(Node* node) { return Reduction(node); } |
50 | 78 |
79 protected: | |
80 void Replace(Node* node, Node* replacement) { | |
81 DCHECK_NOT_NULL(observer_); | |
82 observer_->Replace(node, replacement); | |
83 } | |
84 void Revisit(Node* node) { | |
85 DCHECK_NOT_NULL(observer_); | |
86 observer_->Revisit(node); | |
87 } | |
88 | |
51 private: | 89 private: |
90 Observer* observer_; | |
91 | |
52 DISALLOW_COPY_AND_ASSIGN(Reducer); | 92 DISALLOW_COPY_AND_ASSIGN(Reducer); |
53 }; | 93 }; |
54 | 94 |
55 | 95 |
56 // Performs an iterative reduction of a node graph. | 96 // Performs an iterative reduction of a node graph. |
57 class GraphReducer final { | 97 class GraphReducer final : public Reducer::Observer { |
58 public: | 98 public: |
59 GraphReducer(Graph* graph, Zone* zone); | 99 GraphReducer(Graph* graph, Zone* zone); |
100 ~GraphReducer() final; | |
60 | 101 |
61 Graph* graph() const { return graph_; } | 102 Graph* graph() const { return graph_; } |
62 | 103 |
63 void AddReducer(Reducer* reducer); | 104 void AddReducer(Reducer* reducer); |
64 | 105 |
65 // Reduce a single node. | 106 // Reduce a single node. |
66 void ReduceNode(Node* const); | 107 void ReduceNode(Node* const); |
67 // Reduce the whole graph. | 108 // Reduce the whole graph. |
68 void ReduceGraph(); | 109 void ReduceGraph(); |
69 | 110 |
70 private: | 111 private: |
71 enum class State : uint8_t; | 112 enum class State : uint8_t; |
72 struct NodeState { | 113 struct NodeState { |
73 Node* node; | 114 Node* node; |
74 int input_index; | 115 int input_index; |
75 }; | 116 }; |
76 | 117 |
77 // Reduce a single node. | 118 // Reduce a single node. |
78 Reduction Reduce(Node* const); | 119 Reduction Reduce(Node* const); |
79 // Reduce the node on top of the stack. | 120 // Reduce the node on top of the stack. |
80 void ReduceTop(); | 121 void ReduceTop(); |
81 | 122 |
123 // Replace {node} with {replacement}. | |
124 void Replace(Node* node, Node* replacement) final; | |
125 // Replace {node} during reduction with {replacement}. If the {replacement} | |
126 // is a node whose id is less than {max_id}, then replace all uses. | |
127 // Otherwise, only replace edges whose user id is less than {max_id}. | |
128 void Replace(Node* node, Node* replacement, NodeId max_id); | |
129 | |
82 // Node stack operations. | 130 // Node stack operations. |
83 void Pop(); | 131 void Pop(); |
84 void Push(Node* node); | 132 void Push(Node* node); |
85 | 133 |
86 // Revisit queue operations. | 134 // Revisit queue operations. |
87 bool Recurse(Node* node); | 135 bool Recurse(Node* node); |
88 void Revisit(Node* node); | 136 void Revisit(Node* node) final; |
89 | 137 |
90 Graph* graph_; | 138 // Finish the graph reduction. |
139 bool Finish() { | |
140 for (Reducer* const reducer : reducers_) { | |
141 if (!reducer->Finish()) return false; | |
142 } | |
143 return true; | |
144 } | |
145 | |
146 Graph* const graph_; | |
91 NodeMarker<State> state_; | 147 NodeMarker<State> state_; |
92 ZoneVector<Reducer*> reducers_; | 148 ZoneVector<Reducer*> reducers_; |
93 ZoneStack<Node*> revisit_; | 149 ZoneStack<Node*> revisit_; |
94 ZoneStack<NodeState> stack_; | 150 ZoneStack<NodeState> stack_; |
95 | 151 |
96 DISALLOW_COPY_AND_ASSIGN(GraphReducer); | 152 DISALLOW_COPY_AND_ASSIGN(GraphReducer); |
97 }; | 153 }; |
98 | 154 |
99 } // namespace compiler | 155 } // namespace compiler |
100 } // namespace internal | 156 } // namespace internal |
101 } // namespace v8 | 157 } // namespace v8 |
102 | 158 |
103 #endif // V8_COMPILER_GRAPH_REDUCER_H_ | 159 #endif // V8_COMPILER_GRAPH_REDUCER_H_ |
OLD | NEW |