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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 8 | #include "src/compiler/graph.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 | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | // Represents the result of trying to reduce a node in the graph. |
| 16 | class Reduction FINAL { |
| 17 | public: |
| 18 | explicit Reduction(Node* replacement = NULL) : replacement_(replacement) {} |
| 19 | |
| 20 | Node* replacement() const { return replacement_; } |
| 21 | bool Changed() const { return replacement() != NULL; } |
| 22 | |
| 23 | private: |
| 24 | Node* replacement_; |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | // A reducer can reduce or simplify a given node based on its operator and |
| 29 | // inputs. This class functions as an extension point for the graph reducer for |
| 30 | // language-specific reductions (e.g. reduction based on types or constant |
| 31 | // folding of low-level operators) can be integrated into the graph reduction |
| 32 | // phase. |
| 33 | class Reducer { |
| 34 | public: |
| 35 | Reducer() {} |
| 36 | virtual ~Reducer() {} |
| 37 | |
| 38 | // Try to reduce a node if possible. |
| 39 | virtual Reduction Reduce(Node* node) = 0; |
| 40 | |
| 41 | // Helper functions for subclasses to produce reductions for a node. |
| 42 | static Reduction NoChange() { return Reduction(); } |
| 43 | static Reduction Replace(Node* node) { return Reduction(node); } |
| 44 | static Reduction Changed(Node* node) { return Reduction(node); } |
| 45 | |
| 46 | private: |
| 47 | DISALLOW_COPY_AND_ASSIGN(Reducer); |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | // Performs an iterative reduction of a node graph. |
| 52 | class GraphReducer FINAL { |
| 53 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 54 | GraphReducer(Graph* graph, Zone* zone); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | |
| 56 | Graph* graph() const { return graph_; } |
| 57 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 58 | void AddReducer(Reducer* reducer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | |
| 60 | // Reduce a single node. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 61 | void ReduceNode(Node* const); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 62 | // Reduce the whole graph. |
| 63 | void ReduceGraph(); |
| 64 | |
| 65 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 66 | enum class State : uint8_t; |
| 67 | struct NodeState { |
| 68 | Node* node; |
| 69 | int input_index; |
| 70 | }; |
| 71 | |
| 72 | // Reduce a single node. |
| 73 | Reduction Reduce(Node* const); |
| 74 | // Reduce the node on top of the stack. |
| 75 | void ReduceTop(); |
| 76 | |
| 77 | // Node stack operations. |
| 78 | void Pop(); |
| 79 | void Push(Node* node); |
| 80 | |
| 81 | // Revisit queue operations. |
| 82 | bool Recurse(Node* node); |
| 83 | void Revisit(Node* node); |
| 84 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | Graph* graph_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 86 | NodeMarker<State> state_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | ZoneVector<Reducer*> reducers_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 88 | ZoneStack<Node*> revisit_; |
| 89 | ZoneStack<NodeState> stack_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(GraphReducer); |
| 92 | }; |
| 93 | |
| 94 | } // namespace compiler |
| 95 | } // namespace internal |
| 96 | } // namespace v8 |
| 97 | |
| 98 | #endif // V8_COMPILER_GRAPH_REDUCER_H_ |