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.
5 #ifndef V8_COMPILER_GRAPH_REDUCER_H_
6 #define V8_COMPILER_GRAPH_REDUCER_H_
8 #include "src/zone-containers.h"
14 // Forward declarations.
19 // Represents the result of trying to reduce a node in the graph.
20 class Reduction FINAL {
22 explicit Reduction(Node* replacement = NULL) : replacement_(replacement) {}
24 Node* replacement() const { return replacement_; }
25 bool Changed() const { return replacement() != NULL; }
32 // A reducer can reduce or simplify a given node based on its operator and
33 // inputs. This class functions as an extension point for the graph reducer for
34 // language-specific reductions (e.g. reduction based on types or constant
35 // folding of low-level operators) can be integrated into the graph reduction
42 // Try to reduce a node if possible.
43 virtual Reduction Reduce(Node* node) = 0;
45 // Helper functions for subclasses to produce reductions for a node.
46 static Reduction NoChange() { return Reduction(); }
47 static Reduction Replace(Node* node) { return Reduction(node); }
48 static Reduction Changed(Node* node) { return Reduction(node); }
51 DISALLOW_COPY_AND_ASSIGN(Reducer);
55 // Performs an iterative reduction of a node graph.
56 class GraphReducer FINAL {
58 explicit GraphReducer(Graph* graph);
60 Graph* graph() const { return graph_; }
62 void AddReducer(Reducer* reducer) { reducers_.push_back(reducer); }
64 // Reduce a single node.
65 void ReduceNode(Node* node);
66 // Reduce the whole graph.
71 ZoneVector<Reducer*> reducers_;
73 DISALLOW_COPY_AND_ASSIGN(GraphReducer);
76 } // namespace compiler
77 } // namespace internal
80 #endif // V8_COMPILER_GRAPH_REDUCER_H_