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 #include "src/compiler/graph.h"
6
7 #include <algorithm>
8
9 #include "src/base/bits.h"
10 #include "src/compiler/graph-visualizer.h"
11 #include "src/compiler/node-properties.h"
12 #include "src/compiler/node.h"
13 #include "src/compiler/verifier.h"
14
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18
Graph(Zone * zone)19 Graph::Graph(Zone* zone)
20 : zone_(zone),
21 start_(nullptr),
22 end_(nullptr),
23 mark_max_(0),
24 next_node_id_(0),
25 decorators_(zone) {
26 // Nodes use compressed pointers, so zone must support pointer compression.
27 // If the check fails, ensure the zone is created with kCompressGraphZone
28 // flag.
29 CHECK_IMPLIES(kCompressGraphZone, zone->supports_compression());
30 }
31
Decorate(Node * node)32 void Graph::Decorate(Node* node) {
33 for (GraphDecorator* const decorator : decorators_) {
34 decorator->Decorate(node);
35 }
36 }
37
38
AddDecorator(GraphDecorator * decorator)39 void Graph::AddDecorator(GraphDecorator* decorator) {
40 decorators_.push_back(decorator);
41 }
42
43
RemoveDecorator(GraphDecorator * decorator)44 void Graph::RemoveDecorator(GraphDecorator* decorator) {
45 auto const it = std::find(decorators_.begin(), decorators_.end(), decorator);
46 DCHECK(it != decorators_.end());
47 decorators_.erase(it);
48 }
49
NewNode(const Operator * op,int input_count,Node * const * inputs,bool incomplete)50 Node* Graph::NewNode(const Operator* op, int input_count, Node* const* inputs,
51 bool incomplete) {
52 Node* node = NewNodeUnchecked(op, input_count, inputs, incomplete);
53 Verifier::VerifyNode(node);
54 return node;
55 }
56
NewNodeUnchecked(const Operator * op,int input_count,Node * const * inputs,bool incomplete)57 Node* Graph::NewNodeUnchecked(const Operator* op, int input_count,
58 Node* const* inputs, bool incomplete) {
59 Node* const node =
60 Node::New(zone(), NextNodeId(), op, input_count, inputs, incomplete);
61 Decorate(node);
62 return node;
63 }
64
65
CloneNode(const Node * node)66 Node* Graph::CloneNode(const Node* node) {
67 DCHECK_NOT_NULL(node);
68 Node* const clone = Node::Clone(zone(), NextNodeId(), node);
69 Decorate(clone);
70 return clone;
71 }
72
73
NextNodeId()74 NodeId Graph::NextNodeId() {
75 // A node's id is internally stored in a bit field using fewer bits than
76 // NodeId (see Node::IdField). Hence the addition below won't ever overflow.
77 DCHECK_LT(next_node_id_, std::numeric_limits<NodeId>::max());
78 return next_node_id_++;
79 }
80
Print() const81 void Graph::Print() const { StdoutStream{} << AsRPO(*this); }
82
83 } // namespace compiler
84 } // namespace internal
85 } // namespace v8
86