blob: 09a650cce8fbb015f798465ee1f245ae8623c2f9 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/compiler/graph.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/zone-containers.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015// Represents the result of trying to reduce a node in the graph.
16class 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.
33class 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.
52class GraphReducer FINAL {
53 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054 GraphReducer(Graph* graph, Zone* zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055
56 Graph* graph() const { return graph_; }
57
Emily Bernierd0a1eb72015-03-24 16:35:39 -040058 void AddReducer(Reducer* reducer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059
60 // Reduce a single node.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061 void ReduceNode(Node* const);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 // Reduce the whole graph.
63 void ReduceGraph();
64
65 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066 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 Murdochb8a8cc12014-11-26 15:28:44 +000085 Graph* graph_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040086 NodeMarker<State> state_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 ZoneVector<Reducer*> reducers_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040088 ZoneStack<Node*> revisit_;
89 ZoneStack<NodeState> stack_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090
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_