graph.cc 2.24 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
#include <algorithm>

9 10
#include "src/compiler/graph-visualizer.h"
#include "src/compiler/node.h"
11
#include "src/compiler/verifier.h"
12 13 14 15 16

namespace v8 {
namespace internal {
namespace compiler {

17
Graph::Graph(Zone* zone)
18
    : zone_(zone),
19 20
      start_(nullptr),
      end_(nullptr),
21 22
      mark_max_(0),
      next_node_id_(0),
23 24 25 26 27 28
      decorators_(zone) {
  // Nodes use compressed pointers, so zone must support pointer compression.
  // If the check fails, ensure the zone is created with kCompressGraphZone
  // flag.
  CHECK_IMPLIES(kCompressGraphZone, zone->supports_compression());
}
29

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


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);
46 47
}

48
Node* Graph::NewNode(const Operator* op, int input_count, Node* const* inputs,
49
                     bool incomplete) {
50
  Node* node = NewNodeUnchecked(op, input_count, inputs, incomplete);
51
  Verifier::VerifyNode(node);
52
  return node;
53 54 55
}

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


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


72
NodeId Graph::NextNodeId() {
73 74 75 76
  // A node's id is internally stored in a bit field using fewer bits than
  // NodeId (see Node::IdField). Hence the addition below won't ever overflow.
  DCHECK_LT(next_node_id_, std::numeric_limits<NodeId>::max());
  return next_node_id_++;
77 78
}

79
void Graph::Print() const { StdoutStream{} << AsRPO(*this); }
80

81 82 83
}  // namespace compiler
}  // namespace internal
}  // namespace v8