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 
27 
Decorate(Node * node)28 void Graph::Decorate(Node* node) {
29   for (GraphDecorator* const decorator : decorators_) {
30     decorator->Decorate(node);
31   }
32 }
33 
34 
AddDecorator(GraphDecorator * decorator)35 void Graph::AddDecorator(GraphDecorator* decorator) {
36   decorators_.push_back(decorator);
37 }
38 
39 
RemoveDecorator(GraphDecorator * decorator)40 void Graph::RemoveDecorator(GraphDecorator* decorator) {
41   auto const it = std::find(decorators_.begin(), decorators_.end(), decorator);
42   DCHECK(it != decorators_.end());
43   decorators_.erase(it);
44 }
45 
NewNode(const Operator * op,int input_count,Node * const * inputs,bool incomplete)46 Node* Graph::NewNode(const Operator* op, int input_count, Node* const* inputs,
47                      bool incomplete) {
48   Node* node = NewNodeUnchecked(op, input_count, inputs, incomplete);
49   Verifier::VerifyNode(node);
50   return node;
51 }
52 
NewNodeUnchecked(const Operator * op,int input_count,Node * const * inputs,bool incomplete)53 Node* Graph::NewNodeUnchecked(const Operator* op, int input_count,
54                               Node* const* inputs, bool incomplete) {
55   Node* const node =
56       Node::New(zone(), NextNodeId(), op, input_count, inputs, incomplete);
57   Decorate(node);
58   return node;
59 }
60 
61 
CloneNode(const Node * node)62 Node* Graph::CloneNode(const Node* node) {
63   DCHECK_NOT_NULL(node);
64   Node* const clone = Node::Clone(zone(), NextNodeId(), node);
65   Decorate(clone);
66   return clone;
67 }
68 
69 
NextNodeId()70 NodeId Graph::NextNodeId() {
71   NodeId const id = next_node_id_;
72   CHECK(!base::bits::UnsignedAddOverflow32(id, 1, &next_node_id_));
73   return id;
74 }
75 
Print() const76 void Graph::Print() const {
77   OFStream os(stdout);
78   os << AsRPO(*this);
79 }
80 
81 }  // namespace compiler
82 }  // namespace internal
83 }  // namespace v8
84