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 // Represents the result of trying to reduce a node in the graph. | 20 // Represents the result of trying to reduce a node in the graph. |
21 class Reduction final { | 21 class Reduction final { |
22 public: | 22 public: |
23 explicit Reduction(Node* replacement = NULL) : replacement_(replacement) {} | 23 explicit Reduction(Node* replacement = nullptr) : replacement_(replacement) {} |
24 | 24 |
25 Node* replacement() const { return replacement_; } | 25 Node* replacement() const { return replacement_; } |
26 bool Changed() const { return replacement() != NULL; } | 26 bool Changed() const { return replacement() != nullptr; } |
27 | 27 |
28 private: | 28 private: |
29 Node* replacement_; | 29 Node* replacement_; |
30 }; | 30 }; |
31 | 31 |
32 | 32 |
33 // A reducer can reduce or simplify a given node based on its operator and | 33 // 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 | 34 // 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 | 35 // language-specific reductions (e.g. reduction based on types or constant |
36 // folding of low-level operators) can be integrated into the graph reduction | 36 // folding of low-level operators) can be integrated into the graph reduction |
37 // phase. | 37 // phase. |
38 class Reducer { | 38 class Reducer { |
39 public: | 39 public: |
40 Reducer() {} | |
41 virtual ~Reducer() {} | 40 virtual ~Reducer() {} |
42 | 41 |
43 // Try to reduce a node if possible. | 42 // Try to reduce a node if possible. |
44 virtual Reduction Reduce(Node* node) = 0; | 43 virtual Reduction Reduce(Node* node) = 0; |
45 | 44 |
45 // Ask this reducer to finish operation, returns {true} if the reducer is | |
46 // done, while {false} indicates that the graph might need to be reduced | |
47 // again. | |
48 // TODO(turbofan): Remove this once the dead node trimming is in the | |
49 // GraphReducer. | |
50 virtual bool Finish(); | |
51 | |
46 // Helper functions for subclasses to produce reductions for a node. | 52 // Helper functions for subclasses to produce reductions for a node. |
47 static Reduction NoChange() { return Reduction(); } | 53 static Reduction NoChange() { return Reduction(); } |
48 static Reduction Replace(Node* node) { return Reduction(node); } | 54 static Reduction Replace(Node* node) { return Reduction(node); } |
49 static Reduction Changed(Node* node) { return Reduction(node); } | 55 static Reduction Changed(Node* node) { return Reduction(node); } |
56 }; | |
57 | |
58 | |
59 // An advanced reducer can also edit the graphs by changing and replacing nodes | |
60 // other than the one currently being reduced. | |
61 class AdvancedReducer : public Reducer { | |
62 public: | |
63 // Observe the actions of this reducer. | |
64 class Editor { | |
65 public: | |
66 virtual ~Editor() {} | |
67 | |
68 // Replace {node} with {replacement}. | |
69 virtual void Replace(Node* node, Node* replacement) = 0; | |
70 // Revisit the {node} again later. | |
71 virtual void Revisit(Node* node) = 0; | |
72 }; | |
73 | |
74 explicit AdvancedReducer(Editor* editor) : editor_(editor) {} | |
75 | |
76 protected: | |
77 // Helper functions for subclasses to produce reductions for a node. | |
78 static Reduction Replace(Node* node) { return Reducer::Replace(node); } | |
79 | |
80 // Helper functions for subclasses to edit the graph. | |
81 void Replace(Node* node, Node* replacement) { | |
82 DCHECK_NOT_NULL(editor_); | |
83 editor_->Replace(node, replacement); | |
84 } | |
85 void Revisit(Node* node) { | |
86 DCHECK_NOT_NULL(editor_); | |
87 editor_->Revisit(node); | |
88 } | |
50 | 89 |
51 private: | 90 private: |
52 DISALLOW_COPY_AND_ASSIGN(Reducer); | 91 Editor* const editor_; |
53 }; | 92 }; |
54 | 93 |
55 | 94 |
56 // Performs an iterative reduction of a node graph. | 95 // Performs an iterative reduction of a node graph. |
57 class GraphReducer final { | 96 class GraphReducer final : public AdvancedReducer::Editor { |
58 public: | 97 public: |
59 GraphReducer(Graph* graph, Zone* zone); | 98 GraphReducer(Graph* graph, Zone* zone); |
99 ~GraphReducer() final; | |
60 | 100 |
61 Graph* graph() const { return graph_; } | 101 Graph* graph() const { return graph_; } |
62 | 102 |
63 void AddReducer(Reducer* reducer); | 103 void AddReducer(Reducer* reducer); |
64 | 104 |
65 // Reduce a single node. | 105 // Reduce a single node. |
66 void ReduceNode(Node* const); | 106 void ReduceNode(Node* const); |
67 // Reduce the whole graph. | 107 // Reduce the whole graph. |
68 void ReduceGraph(); | 108 void ReduceGraph(); |
69 | 109 |
70 private: | 110 private: |
71 enum class State : uint8_t; | 111 enum class State : uint8_t; |
72 struct NodeState { | 112 struct NodeState { |
73 Node* node; | 113 Node* node; |
74 int input_index; | 114 int input_index; |
75 }; | 115 }; |
76 | 116 |
77 // Reduce a single node. | 117 // Reduce a single node. |
78 Reduction Reduce(Node* const); | 118 Reduction Reduce(Node* const); |
79 // Reduce the node on top of the stack. | 119 // Reduce the node on top of the stack. |
80 void ReduceTop(); | 120 void ReduceTop(); |
81 | 121 |
122 // Replace {node} with {replacement}. | |
123 void Replace(Node* node, Node* replacement) final; | |
124 | |
82 // Node stack operations. | 125 // Node stack operations. |
83 void Pop(); | 126 void Pop(); |
84 void Push(Node* node); | 127 void Push(Node* node); |
85 | 128 |
86 // Revisit queue operations. | 129 // Revisit queue operations. |
87 bool Recurse(Node* node); | 130 bool Recurse(Node* node); |
88 void Revisit(Node* node); | 131 void Revisit(Node* node) final; |
89 | 132 |
90 Graph* graph_; | 133 // Finish the graph reduction. |
134 bool Finish() { | |
titzer
2015/05/06 09:22:08
Why do we need this in the header?
Benedikt Meurer
2015/05/06 09:33:30
Done.
| |
135 for (Reducer* const reducer : reducers_) { | |
136 if (!reducer->Finish()) return false; | |
137 } | |
138 return true; | |
139 } | |
140 | |
141 Graph* const graph_; | |
91 NodeMarker<State> state_; | 142 NodeMarker<State> state_; |
92 ZoneVector<Reducer*> reducers_; | 143 ZoneVector<Reducer*> reducers_; |
93 ZoneStack<Node*> revisit_; | 144 ZoneStack<Node*> revisit_; |
94 ZoneStack<NodeState> stack_; | 145 ZoneStack<NodeState> stack_; |
95 | 146 |
96 DISALLOW_COPY_AND_ASSIGN(GraphReducer); | 147 DISALLOW_COPY_AND_ASSIGN(GraphReducer); |
97 }; | 148 }; |
98 | 149 |
99 } // namespace compiler | 150 } // namespace compiler |
100 } // namespace internal | 151 } // namespace internal |
101 } // namespace v8 | 152 } // namespace v8 |
102 | 153 |
103 #endif // V8_COMPILER_GRAPH_REDUCER_H_ | 154 #endif // V8_COMPILER_GRAPH_REDUCER_H_ |
OLD | NEW |