11cb0ef41Sopenharmony_ci// Copyright 2013 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/compiler/graph.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <algorithm>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/base/bits.h"
101cb0ef41Sopenharmony_ci#include "src/compiler/graph-visualizer.h"
111cb0ef41Sopenharmony_ci#include "src/compiler/node-properties.h"
121cb0ef41Sopenharmony_ci#include "src/compiler/node.h"
131cb0ef41Sopenharmony_ci#include "src/compiler/verifier.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace internal {
171cb0ef41Sopenharmony_cinamespace compiler {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciGraph::Graph(Zone* zone)
201cb0ef41Sopenharmony_ci    : zone_(zone),
211cb0ef41Sopenharmony_ci      start_(nullptr),
221cb0ef41Sopenharmony_ci      end_(nullptr),
231cb0ef41Sopenharmony_ci      mark_max_(0),
241cb0ef41Sopenharmony_ci      next_node_id_(0),
251cb0ef41Sopenharmony_ci      decorators_(zone) {
261cb0ef41Sopenharmony_ci  // Nodes use compressed pointers, so zone must support pointer compression.
271cb0ef41Sopenharmony_ci  // If the check fails, ensure the zone is created with kCompressGraphZone
281cb0ef41Sopenharmony_ci  // flag.
291cb0ef41Sopenharmony_ci  CHECK_IMPLIES(kCompressGraphZone, zone->supports_compression());
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_civoid Graph::Decorate(Node* node) {
331cb0ef41Sopenharmony_ci  for (GraphDecorator* const decorator : decorators_) {
341cb0ef41Sopenharmony_ci    decorator->Decorate(node);
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_civoid Graph::AddDecorator(GraphDecorator* decorator) {
401cb0ef41Sopenharmony_ci  decorators_.push_back(decorator);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_civoid Graph::RemoveDecorator(GraphDecorator* decorator) {
451cb0ef41Sopenharmony_ci  auto const it = std::find(decorators_.begin(), decorators_.end(), decorator);
461cb0ef41Sopenharmony_ci  DCHECK(it != decorators_.end());
471cb0ef41Sopenharmony_ci  decorators_.erase(it);
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciNode* Graph::NewNode(const Operator* op, int input_count, Node* const* inputs,
511cb0ef41Sopenharmony_ci                     bool incomplete) {
521cb0ef41Sopenharmony_ci  Node* node = NewNodeUnchecked(op, input_count, inputs, incomplete);
531cb0ef41Sopenharmony_ci  Verifier::VerifyNode(node);
541cb0ef41Sopenharmony_ci  return node;
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciNode* Graph::NewNodeUnchecked(const Operator* op, int input_count,
581cb0ef41Sopenharmony_ci                              Node* const* inputs, bool incomplete) {
591cb0ef41Sopenharmony_ci  Node* const node =
601cb0ef41Sopenharmony_ci      Node::New(zone(), NextNodeId(), op, input_count, inputs, incomplete);
611cb0ef41Sopenharmony_ci  Decorate(node);
621cb0ef41Sopenharmony_ci  return node;
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciNode* Graph::CloneNode(const Node* node) {
671cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(node);
681cb0ef41Sopenharmony_ci  Node* const clone = Node::Clone(zone(), NextNodeId(), node);
691cb0ef41Sopenharmony_ci  Decorate(clone);
701cb0ef41Sopenharmony_ci  return clone;
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciNodeId Graph::NextNodeId() {
751cb0ef41Sopenharmony_ci  // A node's id is internally stored in a bit field using fewer bits than
761cb0ef41Sopenharmony_ci  // NodeId (see Node::IdField). Hence the addition below won't ever overflow.
771cb0ef41Sopenharmony_ci  DCHECK_LT(next_node_id_, std::numeric_limits<NodeId>::max());
781cb0ef41Sopenharmony_ci  return next_node_id_++;
791cb0ef41Sopenharmony_ci}
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_civoid Graph::Print() const { StdoutStream{} << AsRPO(*this); }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci}  // namespace compiler
841cb0ef41Sopenharmony_ci}  // namespace internal
851cb0ef41Sopenharmony_ci}  // namespace v8
86