| 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() {} | |
| 41 virtual ~Reducer() {} | 45 virtual ~Reducer() {} |
| 42 | 46 |
| 43 // Try to reduce a node if possible. | 47 // Try to reduce a node if possible. |
| 44 virtual Reduction Reduce(Node* node) = 0; | 48 virtual Reduction Reduce(Node* node) = 0; |
| 45 | 49 |
| 50 // Ask this reducer to finish operation, returns {true} if the reducer is |
| 51 // done, while {false} indicates that the graph might need to be reduced |
| 52 // again. |
| 53 // TODO(turbofan): Remove this once the dead node trimming is in the |
| 54 // GraphReducer. |
| 55 virtual bool Finish(); |
| 56 |
| 46 // Helper functions for subclasses to produce reductions for a node. | 57 // Helper functions for subclasses to produce reductions for a node. |
| 47 static Reduction NoChange() { return Reduction(); } | 58 static Reduction NoChange() { return Reduction(); } |
| 48 static Reduction Replace(Node* node) { return Reduction(node); } | 59 static Reduction Replace(Node* node) { return Reduction(node); } |
| 49 static Reduction Changed(Node* node) { return Reduction(node); } | 60 static Reduction Changed(Node* node) { return Reduction(node); } |
| 61 }; |
| 62 |
| 63 |
| 64 // An advanced reducer can also edit the graphs by changing and replacing nodes |
| 65 // other than the one currently being reduced. |
| 66 class AdvancedReducer : public Reducer { |
| 67 public: |
| 68 // Observe the actions of this reducer. |
| 69 class Editor { |
| 70 public: |
| 71 virtual ~Editor() {} |
| 72 |
| 73 // Replace {node} with {replacement}. |
| 74 virtual void Replace(Node* node, Node* replacement) = 0; |
| 75 // Revisit the {node} again later. |
| 76 virtual void Revisit(Node* node) = 0; |
| 77 }; |
| 78 |
| 79 explicit AdvancedReducer(Editor* editor) : editor_(editor) {} |
| 80 |
| 81 protected: |
| 82 // Helper functions for subclasses to produce reductions for a node. |
| 83 static Reduction Replace(Node* node) { return Reducer::Replace(node); } |
| 84 |
| 85 // Helper functions for subclasses to edit the graph. |
| 86 void Replace(Node* node, Node* replacement) { |
| 87 DCHECK_NOT_NULL(editor_); |
| 88 editor_->Replace(node, replacement); |
| 89 } |
| 90 void Revisit(Node* node) { |
| 91 DCHECK_NOT_NULL(editor_); |
| 92 editor_->Revisit(node); |
| 93 } |
| 50 | 94 |
| 51 private: | 95 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(Reducer); | 96 Editor* const editor_; |
| 53 }; | 97 }; |
| 54 | 98 |
| 55 | 99 |
| 56 // Performs an iterative reduction of a node graph. | 100 // Performs an iterative reduction of a node graph. |
| 57 class GraphReducer final { | 101 class GraphReducer final : public AdvancedReducer::Editor { |
| 58 public: | 102 public: |
| 59 GraphReducer(Graph* graph, Zone* zone); | 103 GraphReducer(Graph* graph, Zone* zone); |
| 104 ~GraphReducer() final; |
| 60 | 105 |
| 61 Graph* graph() const { return graph_; } | 106 Graph* graph() const { return graph_; } |
| 62 | 107 |
| 63 void AddReducer(Reducer* reducer); | 108 void AddReducer(Reducer* reducer); |
| 64 | 109 |
| 65 // Reduce a single node. | 110 // Reduce a single node. |
| 66 void ReduceNode(Node* const); | 111 void ReduceNode(Node* const); |
| 67 // Reduce the whole graph. | 112 // Reduce the whole graph. |
| 68 void ReduceGraph(); | 113 void ReduceGraph(); |
| 69 | 114 |
| 70 private: | 115 private: |
| 71 enum class State : uint8_t; | 116 enum class State : uint8_t; |
| 72 struct NodeState { | 117 struct NodeState { |
| 73 Node* node; | 118 Node* node; |
| 74 int input_index; | 119 int input_index; |
| 75 }; | 120 }; |
| 76 | 121 |
| 77 // Reduce a single node. | 122 // Reduce a single node. |
| 78 Reduction Reduce(Node* const); | 123 Reduction Reduce(Node* const); |
| 79 // Reduce the node on top of the stack. | 124 // Reduce the node on top of the stack. |
| 80 void ReduceTop(); | 125 void ReduceTop(); |
| 81 | 126 |
| 127 // Replace {node} with {replacement}. |
| 128 void Replace(Node* node, Node* replacement) final; |
| 129 // Replace all uses of {node} with {replacement} if the id of {replacement} is |
| 130 // less than or equal to {max_id}. Otherwise, replace all uses of {node} whose |
| 131 // id is less than or equal to {max_id} with the {replacement}. |
| 132 void Replace(Node* node, Node* replacement, NodeId max_id); |
| 133 |
| 82 // Node stack operations. | 134 // Node stack operations. |
| 83 void Pop(); | 135 void Pop(); |
| 84 void Push(Node* node); | 136 void Push(Node* node); |
| 85 | 137 |
| 86 // Revisit queue operations. | 138 // Revisit queue operations. |
| 87 bool Recurse(Node* node); | 139 bool Recurse(Node* node); |
| 88 void Revisit(Node* node); | 140 void Revisit(Node* node) final; |
| 89 | 141 |
| 90 Graph* graph_; | 142 Graph* const graph_; |
| 91 NodeMarker<State> state_; | 143 NodeMarker<State> state_; |
| 92 ZoneVector<Reducer*> reducers_; | 144 ZoneVector<Reducer*> reducers_; |
| 93 ZoneStack<Node*> revisit_; | 145 ZoneStack<Node*> revisit_; |
| 94 ZoneStack<NodeState> stack_; | 146 ZoneStack<NodeState> stack_; |
| 95 | 147 |
| 96 DISALLOW_COPY_AND_ASSIGN(GraphReducer); | 148 DISALLOW_COPY_AND_ASSIGN(GraphReducer); |
| 97 }; | 149 }; |
| 98 | 150 |
| 99 } // namespace compiler | 151 } // namespace compiler |
| 100 } // namespace internal | 152 } // namespace internal |
| 101 } // namespace v8 | 153 } // namespace v8 |
| 102 | 154 |
| 103 #endif // V8_COMPILER_GRAPH_REDUCER_H_ | 155 #endif // V8_COMPILER_GRAPH_REDUCER_H_ |
| OLD | NEW |