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 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 public: | 70 public: |
71 virtual ~Editor() {} | 71 virtual ~Editor() {} |
72 | 72 |
73 // Replace {node} with {replacement}. | 73 // Replace {node} with {replacement}. |
74 virtual void Replace(Node* node, Node* replacement) = 0; | 74 virtual void Replace(Node* node, Node* replacement) = 0; |
75 // Revisit the {node} again later. | 75 // Revisit the {node} again later. |
76 virtual void Revisit(Node* node) = 0; | 76 virtual void Revisit(Node* node) = 0; |
77 // Replace value uses of {node} with {value} and effect uses of {node} with | 77 // Replace value uses of {node} with {value} and effect uses of {node} with |
78 // {effect}. If {effect == NULL}, then use the effect input to {node}. All | 78 // {effect}. If {effect == NULL}, then use the effect input to {node}. All |
79 // control uses will be relaxed assuming {node} cannot throw. | 79 // control uses will be relaxed assuming {node} cannot throw. |
80 virtual void ReplaceWithValue(Node* node, Node* value, | 80 virtual void ReplaceWithValue(Node* node, Node* value, Node* effect, |
81 Node* effect = nullptr, | 81 Node* control) = 0; |
82 Node* control = nullptr) = 0; | |
83 }; | 82 }; |
84 | 83 |
85 explicit AdvancedReducer(Editor* editor) : editor_(editor) {} | 84 explicit AdvancedReducer(Editor* editor) : editor_(editor) {} |
86 | 85 |
87 protected: | 86 protected: |
88 // Helper functions for subclasses to produce reductions for a node. | 87 // Helper functions for subclasses to produce reductions for a node. |
89 static Reduction Replace(Node* node) { return Reducer::Replace(node); } | 88 static Reduction Replace(Node* node) { return Reducer::Replace(node); } |
90 | 89 |
91 // Helper functions for subclasses to edit the graph. | 90 // Helper functions for subclasses to edit the graph. |
92 void Replace(Node* node, Node* replacement) { | 91 void Replace(Node* node, Node* replacement) { |
93 DCHECK_NOT_NULL(editor_); | 92 DCHECK_NOT_NULL(editor_); |
94 editor_->Replace(node, replacement); | 93 editor_->Replace(node, replacement); |
95 } | 94 } |
96 void Revisit(Node* node) { | 95 void Revisit(Node* node) { |
97 DCHECK_NOT_NULL(editor_); | 96 DCHECK_NOT_NULL(editor_); |
98 editor_->Revisit(node); | 97 editor_->Revisit(node); |
99 } | 98 } |
100 void ReplaceWithValue(Node* node, Node* value, Node* effect = nullptr, | 99 void ReplaceWithValue(Node* node, Node* value, Node* effect = nullptr, |
101 Node* control = nullptr) { | 100 Node* control = nullptr) { |
102 DCHECK_NOT_NULL(editor_); | 101 DCHECK_NOT_NULL(editor_); |
103 editor_->ReplaceWithValue(node, value, effect, control); | 102 editor_->ReplaceWithValue(node, value, effect, control); |
104 } | 103 } |
105 | 104 |
106 // Relax the effects of {node} by immediately replacing effect and control | 105 // Relax the effects of {node} by immediately replacing effect and control |
107 // uses of {node} with the effect and control input to {node}. | 106 // uses of {node} with the effect and control input to {node}. |
108 // TODO(turbofan): replace the effect input to {node} with {graph->start()}. | 107 // TODO(turbofan): replace the effect input to {node} with {graph->start()}. |
109 void RelaxEffectsAndControls(Node* node) { | 108 void RelaxEffectsAndControls(Node* node) { |
110 DCHECK_NOT_NULL(editor_); | 109 ReplaceWithValue(node, node, nullptr, nullptr); |
111 editor_->ReplaceWithValue(node, node, nullptr, nullptr); | |
112 } | 110 } |
113 | 111 |
114 // Relax the control uses of {node} by immediately replacing them with the | 112 // Relax the control uses of {node} by immediately replacing them with the |
115 // control input to {node}. | 113 // control input to {node}. |
116 void RelaxControls(Node* node) { | 114 void RelaxControls(Node* node) { |
117 DCHECK_NOT_NULL(editor_); | 115 ReplaceWithValue(node, node, node, nullptr); |
118 editor_->ReplaceWithValue(node, node, node, nullptr); | |
119 } | 116 } |
120 | 117 |
121 private: | 118 private: |
122 Editor* const editor_; | 119 Editor* const editor_; |
123 }; | 120 }; |
124 | 121 |
125 | 122 |
126 // Performs an iterative reduction of a node graph. | 123 // Performs an iterative reduction of a node graph. |
127 class GraphReducer final : public AdvancedReducer::Editor { | 124 class GraphReducer : public AdvancedReducer::Editor { |
128 public: | 125 public: |
129 GraphReducer(Graph* graph, Zone* zone); | 126 GraphReducer(Zone* zone, Graph* graph, Node* dead_value = nullptr, |
130 ~GraphReducer() final; | 127 Node* dead_control = nullptr); |
128 ~GraphReducer(); | |
131 | 129 |
132 Graph* graph() const { return graph_; } | 130 Graph* graph() const { return graph_; } |
133 | 131 |
134 void AddReducer(Reducer* reducer); | 132 void AddReducer(Reducer* reducer); |
135 | 133 |
136 // Reduce a single node. | 134 // Reduce a single node. |
137 void ReduceNode(Node* const); | 135 void ReduceNode(Node* const); |
138 // Reduce the whole graph. | 136 // Reduce the whole graph. |
139 void ReduceGraph(); | 137 void ReduceGraph(); |
140 | 138 |
141 private: | 139 private: |
142 enum class State : uint8_t; | 140 enum class State : uint8_t; |
143 struct NodeState { | 141 struct NodeState { |
144 Node* node; | 142 Node* node; |
145 int input_index; | 143 int input_index; |
146 }; | 144 }; |
147 | 145 |
148 // Reduce a single node. | 146 // Reduce a single node. |
149 Reduction Reduce(Node* const); | 147 Reduction Reduce(Node* const); |
150 // Reduce the node on top of the stack. | 148 // Reduce the node on top of the stack. |
151 void ReduceTop(); | 149 void ReduceTop(); |
152 | 150 |
153 // Replace {node} with {replacement}. | 151 // Replace {node} with {replacement}. |
154 void Replace(Node* node, Node* replacement) final; | 152 void Replace(Node* node, Node* replacement) final; |
155 | 153 |
156 // Replace value uses of {node} with {value} and effect uses of {node} with | 154 // Replace value uses of {node} with {value} and effect uses of {node} with |
157 // {effect}. If {effect == NULL}, then use the effect input to {node}. All | 155 // {effect}. If {effect == NULL}, then use the effect input to {node}. All |
158 // control uses will be relaxed assuming {node} cannot throw. | 156 // control uses will be relaxed assuming {node} cannot throw. |
159 void ReplaceWithValue(Node* node, Node* value, Node* effect = nullptr, | 157 void ReplaceWithValue(Node* node, Node* value, Node* effect, |
160 Node* control = nullptr) final; | 158 Node* control) final; |
161 | 159 |
162 // Replace all uses of {node} with {replacement} if the id of {replacement} is | 160 // Replace all uses of {node} with {replacement} if the id of {replacement} is |
163 // less than or equal to {max_id}. Otherwise, replace all uses of {node} whose | 161 // less than or equal to {max_id}. Otherwise, replace all uses of {node} whose |
164 // id is less than or equal to {max_id} with the {replacement}. | 162 // id is less than or equal to {max_id} with the {replacement}. |
165 void Replace(Node* node, Node* replacement, NodeId max_id); | 163 void Replace(Node* node, Node* replacement, NodeId max_id); |
166 | 164 |
167 // Node stack operations. | 165 // Node stack operations. |
168 void Pop(); | 166 void Pop(); |
169 void Push(Node* node); | 167 void Push(Node* node); |
170 | 168 |
171 // Revisit queue operations. | 169 // Revisit queue operations. |
172 bool Recurse(Node* node); | 170 bool Recurse(Node* node); |
173 void Revisit(Node* node) final; | 171 void Revisit(Node* node) final; |
174 | 172 |
175 Graph* const graph_; | 173 Graph* const graph_; |
174 Node* const dead_value_; | |
titzer
2015/06/05 11:30:50
Const is like, just your opinion, man.
Michael Starzinger
2015/06/05 11:34:12
Done. Well, your slight preference is my command.
| |
175 Node* const dead_control_; | |
176 NodeMarker<State> state_; | 176 NodeMarker<State> state_; |
177 ZoneVector<Reducer*> reducers_; | 177 ZoneVector<Reducer*> reducers_; |
178 ZoneStack<Node*> revisit_; | 178 ZoneStack<Node*> revisit_; |
179 ZoneStack<NodeState> stack_; | 179 ZoneStack<NodeState> stack_; |
180 | 180 |
181 DISALLOW_COPY_AND_ASSIGN(GraphReducer); | 181 DISALLOW_COPY_AND_ASSIGN(GraphReducer); |
182 }; | 182 }; |
183 | 183 |
184 } // namespace compiler | 184 } // namespace compiler |
185 } // namespace internal | 185 } // namespace internal |
186 } // namespace v8 | 186 } // namespace v8 |
187 | 187 |
188 #endif // V8_COMPILER_GRAPH_REDUCER_H_ | 188 #endif // V8_COMPILER_GRAPH_REDUCER_H_ |
OLD | NEW |