graph-reducer.h 2.01 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_GRAPH_REDUCER_H_
#define V8_COMPILER_GRAPH_REDUCER_H_

8
#include "src/zone-containers.h"
9 10 11 12 13 14 15 16 17 18 19

namespace v8 {
namespace internal {
namespace compiler {

// Forward declarations.
class Graph;
class Node;


// Represents the result of trying to reduce a node in the graph.
20
class Reduction FINAL {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 public:
  explicit Reduction(Node* replacement = NULL) : replacement_(replacement) {}

  Node* replacement() const { return replacement_; }
  bool Changed() const { return replacement() != NULL; }

 private:
  Node* replacement_;
};


// A reducer can reduce or simplify a given node based on its operator and
// inputs. This class functions as an extension point for the graph reducer for
// language-specific reductions (e.g. reduction based on types or constant
// folding of low-level operators) can be integrated into the graph reduction
// phase.
class Reducer {
 public:
39
  Reducer() {}
40 41 42 43 44 45 46 47 48
  virtual ~Reducer() {}

  // Try to reduce a node if possible.
  virtual Reduction Reduce(Node* node) = 0;

  // Helper functions for subclasses to produce reductions for a node.
  static Reduction NoChange() { return Reduction(); }
  static Reduction Replace(Node* node) { return Reduction(node); }
  static Reduction Changed(Node* node) { return Reduction(node); }
49 50 51

 private:
  DISALLOW_COPY_AND_ASSIGN(Reducer);
52 53 54 55
};


// Performs an iterative reduction of a node graph.
56
class GraphReducer FINAL {
57 58 59 60 61 62 63 64 65 66 67 68 69 70
 public:
  explicit GraphReducer(Graph* graph);

  Graph* graph() const { return graph_; }

  void AddReducer(Reducer* reducer) { reducers_.push_back(reducer); }

  // Reduce a single node.
  void ReduceNode(Node* node);
  // Reduce the whole graph.
  void ReduceGraph();

 private:
  Graph* graph_;
71
  ZoneVector<Reducer*> reducers_;
72 73

  DISALLOW_COPY_AND_ASSIGN(GraphReducer);
74
};
75 76 77 78

}  // namespace compiler
}  // namespace internal
}  // namespace v8
79 80

#endif  // V8_COMPILER_GRAPH_REDUCER_H_