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/node.h"
11#include "src/compiler/node-properties.h"
12#include "src/compiler/verifier.h"
13
14namespace v8 {
15namespace internal {
16namespace compiler {
17
18Graph::Graph(Zone* zone)
19    : zone_(zone),
20      start_(nullptr),
21      end_(nullptr),
22      mark_max_(0),
23      next_node_id_(0),
24      decorators_(zone) {}
25
26
27void Graph::Decorate(Node* node) {
28  for (auto const decorator : decorators_) {
29    decorator->Decorate(node);
30  }
31}
32
33
34void Graph::AddDecorator(GraphDecorator* decorator) {
35  decorators_.push_back(decorator);
36}
37
38
39void Graph::RemoveDecorator(GraphDecorator* decorator) {
40  auto const it = std::find(decorators_.begin(), decorators_.end(), decorator);
41  DCHECK(it != decorators_.end());
42  decorators_.erase(it);
43}
44
45
46Node* Graph::NewNode(const Operator* op, int input_count, Node** inputs,
47                     bool incomplete) {
48  Node* node = NewNodeUnchecked(op, input_count, inputs, incomplete);
49  Verifier::VerifyNode(node);
50  return node;
51}
52
53
54Node* Graph::NewNodeUnchecked(const Operator* op, int input_count,
55                              Node** inputs, bool incomplete) {
56  Node* const node =
57      Node::New(zone(), NextNodeId(), op, input_count, inputs, incomplete);
58  Decorate(node);
59  return node;
60}
61
62
63Node* Graph::CloneNode(const Node* node) {
64  DCHECK_NOT_NULL(node);
65  Node* const clone = Node::Clone(zone(), NextNodeId(), node);
66  Decorate(clone);
67  return clone;
68}
69
70
71NodeId Graph::NextNodeId() {
72  NodeId const id = next_node_id_;
73  CHECK(!base::bits::UnsignedAddOverflow32(id, 1, &next_node_id_));
74  return id;
75}
76
77}  // namespace compiler
78}  // namespace internal
79}  // namespace v8
80