Chromium Code Reviews| Index: src/compiler/graph-reducer.h |
| diff --git a/src/compiler/graph-reducer.h b/src/compiler/graph-reducer.h |
| index a6cc1f2d94b0b14974907cd37b83ac7d8d81a448..eb3256d0624980d69d21f76dd2c1f0432f3a2696 100644 |
| --- a/src/compiler/graph-reducer.h |
| +++ b/src/compiler/graph-reducer.h |
| @@ -20,10 +20,10 @@ class Node; |
| // Represents the result of trying to reduce a node in the graph. |
| class Reduction final { |
| public: |
| - explicit Reduction(Node* replacement = NULL) : replacement_(replacement) {} |
| + explicit Reduction(Node* replacement = nullptr) : replacement_(replacement) {} |
| Node* replacement() const { return replacement_; } |
| - bool Changed() const { return replacement() != NULL; } |
| + bool Changed() const { return replacement() != nullptr; } |
| private: |
| Node* replacement_; |
| @@ -37,26 +37,66 @@ class Reduction final { |
| // phase. |
| class Reducer { |
| public: |
| - Reducer() {} |
| virtual ~Reducer() {} |
| // Try to reduce a node if possible. |
| virtual Reduction Reduce(Node* node) = 0; |
| + // Ask this reducer to finish operation, returns {true} if the reducer is |
| + // done, while {false} indicates that the graph might need to be reduced |
| + // again. |
| + // TODO(turbofan): Remove this once the dead node trimming is in the |
| + // GraphReducer. |
| + virtual bool Finish(); |
| + |
| // Helper functions for subclasses to produce reductions for a node. |
| static Reduction NoChange() { return Reduction(); } |
| static Reduction Replace(Node* node) { return Reduction(node); } |
| static Reduction Changed(Node* node) { return Reduction(node); } |
| +}; |
| + |
| + |
| +// An advanced reducer can also edit the graphs by changing and replacing nodes |
| +// other than the one currently being reduced. |
| +class AdvancedReducer : public Reducer { |
| + public: |
| + // Observe the actions of this reducer. |
| + class Editor { |
| + public: |
| + virtual ~Editor() {} |
| + |
| + // Replace {node} with {replacement}. |
| + virtual void Replace(Node* node, Node* replacement) = 0; |
| + // Revisit the {node} again later. |
| + virtual void Revisit(Node* node) = 0; |
| + }; |
| + |
| + explicit AdvancedReducer(Editor* editor) : editor_(editor) {} |
| + |
| + protected: |
| + // Helper functions for subclasses to produce reductions for a node. |
| + static Reduction Replace(Node* node) { return Reducer::Replace(node); } |
| + |
| + // Helper functions for subclasses to edit the graph. |
| + void Replace(Node* node, Node* replacement) { |
| + DCHECK_NOT_NULL(editor_); |
| + editor_->Replace(node, replacement); |
| + } |
| + void Revisit(Node* node) { |
| + DCHECK_NOT_NULL(editor_); |
| + editor_->Revisit(node); |
| + } |
| private: |
| - DISALLOW_COPY_AND_ASSIGN(Reducer); |
| + Editor* const editor_; |
| }; |
| // Performs an iterative reduction of a node graph. |
| -class GraphReducer final { |
| +class GraphReducer final : public AdvancedReducer::Editor { |
| public: |
| GraphReducer(Graph* graph, Zone* zone); |
| + ~GraphReducer() final; |
| Graph* graph() const { return graph_; } |
| @@ -79,15 +119,26 @@ class GraphReducer final { |
| // Reduce the node on top of the stack. |
| void ReduceTop(); |
| + // Replace {node} with {replacement}. |
| + void Replace(Node* node, Node* replacement) final; |
| + |
| // Node stack operations. |
| void Pop(); |
| void Push(Node* node); |
| // Revisit queue operations. |
| bool Recurse(Node* node); |
| - void Revisit(Node* node); |
| + void Revisit(Node* node) final; |
| + |
| + // Finish the graph reduction. |
| + 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.
|
| + for (Reducer* const reducer : reducers_) { |
| + if (!reducer->Finish()) return false; |
| + } |
| + return true; |
| + } |
| - Graph* graph_; |
| + Graph* const graph_; |
| NodeMarker<State> state_; |
| ZoneVector<Reducer*> reducers_; |
| ZoneStack<Node*> revisit_; |