Line data Source code
1 : // Copyright 2013 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_H_
6 : #define V8_COMPILER_GRAPH_H_
7 :
8 : #include <array>
9 :
10 : #include "src/base/compiler-specific.h"
11 : #include "src/globals.h"
12 : #include "src/zone/zone-containers.h"
13 : #include "src/zone/zone.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 : namespace compiler {
18 :
19 : // Forward declarations.
20 : class GraphDecorator;
21 : class Node;
22 : class Operator;
23 :
24 :
25 : // Marks are used during traversal of the graph to distinguish states of nodes.
26 : // Each node has a mark which is a monotonically increasing integer, and a
27 : // {NodeMarker} has a range of values that indicate states of a node.
28 : using Mark = uint32_t;
29 :
30 : // NodeIds are identifying numbers for nodes that can be used to index auxiliary
31 : // out-of-line data associated with each node.
32 : using NodeId = uint32_t;
33 :
34 699838 : class V8_EXPORT_PRIVATE Graph final : public NON_EXPORTED_BASE(ZoneObject) {
35 : public:
36 : explicit Graph(Zone* zone);
37 :
38 : // Scope used when creating a subgraph for inlining. Automatically preserves
39 : // the original start and end nodes of the graph, and resets them when you
40 : // leave the scope.
41 : class SubgraphScope final {
42 : public:
43 : explicit SubgraphScope(Graph* graph)
44 : : graph_(graph), start_(graph->start()), end_(graph->end()) {}
45 : ~SubgraphScope() {
46 : graph_->SetStart(start_);
47 : graph_->SetEnd(end_);
48 : }
49 :
50 : private:
51 : Graph* const graph_;
52 : Node* const start_;
53 : Node* const end_;
54 :
55 : DISALLOW_COPY_AND_ASSIGN(SubgraphScope);
56 : };
57 :
58 : // Base implementation used by all factory methods.
59 : Node* NewNodeUnchecked(const Operator* op, int input_count,
60 : Node* const* inputs, bool incomplete = false);
61 :
62 : // Factory that checks the input count.
63 : Node* NewNode(const Operator* op, int input_count, Node* const* inputs,
64 : bool incomplete = false);
65 :
66 : // Factory template for nodes with static input counts.
67 : template <typename... Nodes>
68 : Node* NewNode(const Operator* op, Nodes*... nodes) {
69 56433379 : std::array<Node*, sizeof...(nodes)> nodes_arr{{nodes...}};
70 99972523 : return NewNode(op, nodes_arr.size(), nodes_arr.data());
71 : }
72 :
73 : // Clone the {node}, and assign a new node id to the copy.
74 : Node* CloneNode(const Node* node);
75 :
76 : Zone* zone() const { return zone_; }
77 : Node* start() const { return start_; }
78 : Node* end() const { return end_; }
79 :
80 2897258 : void SetStart(Node* start) { start_ = start; }
81 3051810 : void SetEnd(Node* end) { end_ = end; }
82 :
83 23220940 : size_t NodeCount() const { return next_node_id_; }
84 :
85 : void Decorate(Node* node);
86 : void AddDecorator(GraphDecorator* decorator);
87 : void RemoveDecorator(GraphDecorator* decorator);
88 :
89 : // Very simple print API usable in a debugger.
90 : void Print() const;
91 :
92 : private:
93 : friend class NodeMarkerBase;
94 :
95 : inline NodeId NextNodeId();
96 :
97 : Zone* const zone_;
98 : Node* start_;
99 : Node* end_;
100 : Mark mark_max_;
101 : NodeId next_node_id_;
102 : ZoneVector<GraphDecorator*> decorators_;
103 :
104 : DISALLOW_COPY_AND_ASSIGN(Graph);
105 : };
106 :
107 :
108 : // A graph decorator can be used to add behavior to the creation of nodes
109 : // in a graph.
110 1179896 : class GraphDecorator : public ZoneObject {
111 : public:
112 0 : virtual ~GraphDecorator() = default;
113 : virtual void Decorate(Node* node) = 0;
114 : };
115 :
116 : } // namespace compiler
117 : } // namespace internal
118 : } // namespace v8
119 :
120 : #endif // V8_COMPILER_GRAPH_H_
|