blob: 3d4d6da89c2dfb1ff480f3daf74359565abce2b5 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include <algorithm>
8
9#include "src/base/bits.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/compiler/node.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/compiler/node-properties.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/compiler/verifier.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14namespace v8 {
15namespace internal {
16namespace compiler {
17
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018Graph::Graph(Zone* zone)
19 : zone_(zone),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020 start_(nullptr),
21 end_(nullptr),
Emily Bernierd0a1eb72015-03-24 16:35:39 -040022 mark_max_(0),
23 next_node_id_(0),
24 decorators_(zone) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025
26
Emily Bernierd0a1eb72015-03-24 16:35:39 -040027void Graph::Decorate(Node* node) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028 for (auto const decorator : decorators_) {
29 decorator->Decorate(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040030 }
31}
32
33
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034void 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040046Node* Graph::NewNode(const Operator* op, int input_count, Node** inputs,
47 bool incomplete) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 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;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075}
76
77} // namespace compiler
78} // namespace internal
79} // namespace v8