Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_COMPILER_GRAPH_REDUCER_H_ |
| 6 | #define V8_COMPILER_GRAPH_REDUCER_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/compiler/node-marker.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/zone-containers.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | namespace compiler { |
| 14 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | // Forward declarations. |
| 16 | class Graph; |
| 17 | class Node; |
| 18 | |
| 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 uint32_t NodeId; |
| 23 | |
| 24 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | // Represents the result of trying to reduce a node in the graph. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | class Reduction final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | explicit Reduction(Node* replacement = nullptr) : replacement_(replacement) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | |
| 30 | Node* replacement() const { return replacement_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | bool Changed() const { return replacement() != nullptr; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | Node* replacement_; |
| 35 | }; |
| 36 | |
| 37 | |
| 38 | // A reducer can reduce or simplify a given node based on its operator and |
| 39 | // inputs. This class functions as an extension point for the graph reducer for |
| 40 | // language-specific reductions (e.g. reduction based on types or constant |
| 41 | // folding of low-level operators) can be integrated into the graph reduction |
| 42 | // phase. |
| 43 | class Reducer { |
| 44 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | virtual ~Reducer() {} |
| 46 | |
| 47 | // Try to reduce a node if possible. |
| 48 | virtual Reduction Reduce(Node* node) = 0; |
| 49 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 50 | // Invoked by the {GraphReducer} when all nodes are done. Can be used to |
| 51 | // do additional reductions at the end, which in turn can cause a new round |
| 52 | // of reductions. |
| 53 | virtual void Finalize(); |
| 54 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | // Helper functions for subclasses to produce reductions for a node. |
| 56 | static Reduction NoChange() { return Reduction(); } |
| 57 | static Reduction Replace(Node* node) { return Reduction(node); } |
| 58 | static Reduction Changed(Node* node) { return Reduction(node); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | |
| 62 | // An advanced reducer can also edit the graphs by changing and replacing nodes |
| 63 | // other than the one currently being reduced. |
| 64 | class AdvancedReducer : public Reducer { |
| 65 | public: |
| 66 | // Observe the actions of this reducer. |
| 67 | class Editor { |
| 68 | public: |
| 69 | virtual ~Editor() {} |
| 70 | |
| 71 | // Replace {node} with {replacement}. |
| 72 | virtual void Replace(Node* node, Node* replacement) = 0; |
| 73 | // Revisit the {node} again later. |
| 74 | virtual void Revisit(Node* node) = 0; |
| 75 | // Replace value uses of {node} with {value} and effect uses of {node} with |
| 76 | // {effect}. If {effect == nullptr}, then use the effect input to {node}. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame^] | 77 | // All control uses will be relaxed assuming {node} cannot throw. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 78 | virtual void ReplaceWithValue(Node* node, Node* value, Node* effect, |
| 79 | Node* control) = 0; |
| 80 | }; |
| 81 | |
| 82 | explicit AdvancedReducer(Editor* editor) : editor_(editor) {} |
| 83 | |
| 84 | protected: |
| 85 | // Helper functions for subclasses to produce reductions for a node. |
| 86 | static Reduction Replace(Node* node) { return Reducer::Replace(node); } |
| 87 | |
| 88 | // Helper functions for subclasses to edit the graph. |
| 89 | void Replace(Node* node, Node* replacement) { |
| 90 | DCHECK_NOT_NULL(editor_); |
| 91 | editor_->Replace(node, replacement); |
| 92 | } |
| 93 | void Revisit(Node* node) { |
| 94 | DCHECK_NOT_NULL(editor_); |
| 95 | editor_->Revisit(node); |
| 96 | } |
| 97 | void ReplaceWithValue(Node* node, Node* value, Node* effect = nullptr, |
| 98 | Node* control = nullptr) { |
| 99 | DCHECK_NOT_NULL(editor_); |
| 100 | editor_->ReplaceWithValue(node, value, effect, control); |
| 101 | } |
| 102 | |
| 103 | // Relax the effects of {node} by immediately replacing effect and control |
| 104 | // uses of {node} with the effect and control input to {node}. |
| 105 | // TODO(turbofan): replace the effect input to {node} with {graph->start()}. |
| 106 | void RelaxEffectsAndControls(Node* node) { |
| 107 | ReplaceWithValue(node, node, nullptr, nullptr); |
| 108 | } |
| 109 | |
| 110 | // Relax the control uses of {node} by immediately replacing them with the |
| 111 | // control input to {node}. |
| 112 | void RelaxControls(Node* node) { |
| 113 | ReplaceWithValue(node, node, node, nullptr); |
| 114 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | Editor* const editor_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | |
| 121 | // Performs an iterative reduction of a node graph. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | class GraphReducer : public AdvancedReducer::Editor { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 124 | GraphReducer(Zone* zone, Graph* graph, Node* dead = nullptr); |
| 125 | ~GraphReducer(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | |
| 127 | Graph* graph() const { return graph_; } |
| 128 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 129 | void AddReducer(Reducer* reducer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 130 | |
| 131 | // Reduce a single node. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 132 | void ReduceNode(Node* const); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | // Reduce the whole graph. |
| 134 | void ReduceGraph(); |
| 135 | |
| 136 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 137 | enum class State : uint8_t; |
| 138 | struct NodeState { |
| 139 | Node* node; |
| 140 | int input_index; |
| 141 | }; |
| 142 | |
| 143 | // Reduce a single node. |
| 144 | Reduction Reduce(Node* const); |
| 145 | // Reduce the node on top of the stack. |
| 146 | void ReduceTop(); |
| 147 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 148 | // Replace {node} with {replacement}. |
| 149 | void Replace(Node* node, Node* replacement) final; |
| 150 | |
| 151 | // Replace value uses of {node} with {value} and effect uses of {node} with |
| 152 | // {effect}. If {effect == nullptr}, then use the effect input to {node}. All |
| 153 | // control uses will be relaxed assuming {node} cannot throw. |
| 154 | void ReplaceWithValue(Node* node, Node* value, Node* effect, |
| 155 | Node* control) final; |
| 156 | |
| 157 | // Replace all uses of {node} with {replacement} if the id of {replacement} is |
| 158 | // less than or equal to {max_id}. Otherwise, replace all uses of {node} whose |
| 159 | // id is less than or equal to {max_id} with the {replacement}. |
| 160 | void Replace(Node* node, Node* replacement, NodeId max_id); |
| 161 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 162 | // Node stack operations. |
| 163 | void Pop(); |
| 164 | void Push(Node* node); |
| 165 | |
| 166 | // Revisit queue operations. |
| 167 | bool Recurse(Node* node); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 168 | void Revisit(Node* node) final; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 169 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | Graph* const graph_; |
| 171 | Node* const dead_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 172 | NodeMarker<State> state_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 173 | ZoneVector<Reducer*> reducers_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 174 | ZoneStack<Node*> revisit_; |
| 175 | ZoneStack<NodeState> stack_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | |
| 177 | DISALLOW_COPY_AND_ASSIGN(GraphReducer); |
| 178 | }; |
| 179 | |
| 180 | } // namespace compiler |
| 181 | } // namespace internal |
| 182 | } // namespace v8 |
| 183 | |
| 184 | #endif // V8_COMPILER_GRAPH_REDUCER_H_ |