graph.cc 1.96 KB
Newer Older
1 2 3 4 5 6
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/graph.h"

7 8 9
#include <algorithm>

#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 16 17 18

namespace v8 {
namespace internal {
namespace compiler {

19
Graph::Graph(Zone* zone)
20
    : zone_(zone),
21 22
      start_(nullptr),
      end_(nullptr),
23 24 25
      mark_max_(0),
      next_node_id_(0),
      decorators_(zone) {}
26 27


28
void Graph::Decorate(Node* node) {
29
  for (GraphDecorator* const decorator : decorators_) {
30
    decorator->Decorate(node);
31
  }
32 33 34 35 36 37 38 39 40 41 42 43
}


void Graph::AddDecorator(GraphDecorator* decorator) {
  decorators_.push_back(decorator);
}


void Graph::RemoveDecorator(GraphDecorator* decorator) {
  auto const it = std::find(decorators_.begin(), decorators_.end(), decorator);
  DCHECK(it != decorators_.end());
  decorators_.erase(it);
44 45
}

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 53
}

Node* Graph::NewNodeUnchecked(const Operator* op, int input_count,
54
                              Node* const* inputs, bool incomplete) {
55 56
  Node* const node =
      Node::New(zone(), NextNodeId(), op, input_count, inputs, incomplete);
57
  Decorate(node);
58 59 60 61
  return node;
}


62 63 64 65 66 67 68 69
Node* Graph::CloneNode(const Node* node) {
  DCHECK_NOT_NULL(node);
  Node* const clone = Node::Clone(zone(), NextNodeId(), node);
  Decorate(clone);
  return clone;
}


70 71
NodeId Graph::NextNodeId() {
  NodeId const id = next_node_id_;
72
  CHECK(!base::bits::UnsignedAddOverflow32(id, 1, &next_node_id_));
73
  return id;
74 75
}

76
void Graph::Print() const { StdoutStream{} << AsRPO(*this); }
77

78 79 80
}  // namespace compiler
}  // namespace internal
}  // namespace v8