graph.h 3.94 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2013 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_H_
#define V8_COMPILER_GRAPH_H_

8 9
#include "src/zone.h"
#include "src/zone-containers.h"
10 11 12 13 14

namespace v8 {
namespace internal {
namespace compiler {

15
// Forward declarations.
16
class GraphDecorator;
17 18 19 20 21 22 23 24 25 26 27 28
class Node;
class Operator;


// Marks are used during traversal of the graph to distinguish states of nodes.
// Each node has a mark which is a monotonically increasing integer, and a
// {NodeMarker} has a range of values that indicate states of a node.
typedef uint32_t Mark;


// NodeIds are identifying numbers for nodes that can be used to index auxiliary
// out-of-line data associated with each node.
29
typedef uint32_t NodeId;
30

31

32
class Graph : public ZoneObject {
33 34 35 36
 public:
  explicit Graph(Zone* zone);

  // Base implementation used by all factory methods.
37 38
  Node* NewNode(const Operator* op, int input_count, Node** inputs,
                bool incomplete = false);
39 40

  // Factories for nodes with static input counts.
41
  Node* NewNode(const Operator* op) {
42
    return NewNode(op, 0, static_cast<Node**>(nullptr));
43
  }
44 45
  Node* NewNode(const Operator* op, Node* n1) { return NewNode(op, 1, &n1); }
  Node* NewNode(const Operator* op, Node* n1, Node* n2) {
46
    Node* nodes[] = {n1, n2};
47
    return NewNode(op, arraysize(nodes), nodes);
48
  }
49
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3) {
50
    Node* nodes[] = {n1, n2, n3};
51
    return NewNode(op, arraysize(nodes), nodes);
52
  }
53
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4) {
54
    Node* nodes[] = {n1, n2, n3, n4};
55
    return NewNode(op, arraysize(nodes), nodes);
56
  }
57
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
58 59
                Node* n5) {
    Node* nodes[] = {n1, n2, n3, n4, n5};
60
    return NewNode(op, arraysize(nodes), nodes);
61
  }
62 63
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
                Node* n5, Node* n6) {
64
    Node* nodes[] = {n1, n2, n3, n4, n5, n6};
65
    return NewNode(op, arraysize(nodes), nodes);
66
  }
67 68 69 70 71
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
                Node* n5, Node* n6, Node* n7) {
    Node* nodes[] = {n1, n2, n3, n4, n5, n6, n7};
    return NewNode(op, arraysize(nodes), nodes);
  }
72 73 74 75 76
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
                Node* n5, Node* n6, Node* n7, Node* n8) {
    Node* nodes[] = {n1, n2, n3, n4, n5, n6, n7, n8};
    return NewNode(op, arraysize(nodes), nodes);
  }
77 78 79 80 81
  Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
                Node* n5, Node* n6, Node* n7, Node* n8, Node* n9) {
    Node* nodes[] = {n1, n2, n3, n4, n5, n6, n7, n8, n9};
    return NewNode(op, arraysize(nodes), nodes);
  }
82

83 84 85
  // Clone the {node}, and assign a new node id to the copy.
  Node* CloneNode(const Node* node);

86
  template <class Visitor>
87
  inline void VisitNodeInputsFromEnd(Visitor* visitor);
88

89 90 91 92 93 94 95
  Zone* zone() const { return zone_; }
  Node* start() const { return start_; }
  Node* end() const { return end_; }

  void SetStart(Node* start) { start_ = start; }
  void SetEnd(Node* end) { end_ = end; }

96
  size_t NodeCount() const { return next_node_id_; }
97

98
  void Decorate(Node* node);
99 100
  void AddDecorator(GraphDecorator* decorator);
  void RemoveDecorator(GraphDecorator* decorator);
101 102

 private:
103
  friend class NodeMarkerBase;
104

105 106 107
  inline NodeId NextNodeId();

  Zone* const zone_;
108 109
  Node* start_;
  Node* end_;
110
  Mark mark_max_;
111
  NodeId next_node_id_;
112
  ZoneVector<GraphDecorator*> decorators_;
113 114

  DISALLOW_COPY_AND_ASSIGN(Graph);
115 116 117
};


118 119
// A graph decorator can be used to add behavior to the creation of nodes
// in a graph.
120 121 122
class GraphDecorator : public ZoneObject {
 public:
  virtual ~GraphDecorator() {}
123
  virtual void Decorate(Node* node) = 0;
124
};
125 126 127 128

}  // namespace compiler
}  // namespace internal
}  // namespace v8
129 130

#endif  // V8_COMPILER_GRAPH_H_