11cb0ef41Sopenharmony_ci// Copyright 2014 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#ifndef V8_COMPILER_NODE_ORIGIN_TABLE_H_
61cb0ef41Sopenharmony_ci#define V8_COMPILER_NODE_ORIGIN_TABLE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <limits>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "src/base/compiler-specific.h"
111cb0ef41Sopenharmony_ci#include "src/codegen/source-position.h"
121cb0ef41Sopenharmony_ci#include "src/common/globals.h"
131cb0ef41Sopenharmony_ci#include "src/compiler/node-aux-data.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace internal {
171cb0ef41Sopenharmony_cinamespace compiler {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciclass NodeOrigin {
201cb0ef41Sopenharmony_ci public:
211cb0ef41Sopenharmony_ci  enum OriginKind { kWasmBytecode, kGraphNode };
221cb0ef41Sopenharmony_ci  NodeOrigin(const char* phase_name, const char* reducer_name,
231cb0ef41Sopenharmony_ci             NodeId created_from)
241cb0ef41Sopenharmony_ci      : phase_name_(phase_name),
251cb0ef41Sopenharmony_ci        reducer_name_(reducer_name),
261cb0ef41Sopenharmony_ci        origin_kind_(kGraphNode),
271cb0ef41Sopenharmony_ci        created_from_(created_from) {}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  NodeOrigin(const char* phase_name, const char* reducer_name,
301cb0ef41Sopenharmony_ci             OriginKind origin_kind, uint64_t created_from)
311cb0ef41Sopenharmony_ci      : phase_name_(phase_name),
321cb0ef41Sopenharmony_ci        reducer_name_(reducer_name),
331cb0ef41Sopenharmony_ci        origin_kind_(origin_kind),
341cb0ef41Sopenharmony_ci        created_from_(created_from) {}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  NodeOrigin(const NodeOrigin& other) V8_NOEXCEPT = default;
371cb0ef41Sopenharmony_ci  NodeOrigin& operator=(const NodeOrigin& other) V8_NOEXCEPT = default;
381cb0ef41Sopenharmony_ci  static NodeOrigin Unknown() { return NodeOrigin(); }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  bool IsKnown() { return created_from_ >= 0; }
411cb0ef41Sopenharmony_ci  int64_t created_from() const { return created_from_; }
421cb0ef41Sopenharmony_ci  const char* reducer_name() const { return reducer_name_; }
431cb0ef41Sopenharmony_ci  const char* phase_name() const { return phase_name_; }
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  OriginKind origin_kind() const { return origin_kind_; }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  bool operator==(const NodeOrigin& o) const {
481cb0ef41Sopenharmony_ci    return reducer_name_ == o.reducer_name_ && created_from_ == o.created_from_;
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  void PrintJson(std::ostream& out) const;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci private:
541cb0ef41Sopenharmony_ci  NodeOrigin()
551cb0ef41Sopenharmony_ci      : phase_name_(""),
561cb0ef41Sopenharmony_ci        reducer_name_(""),
571cb0ef41Sopenharmony_ci        created_from_(std::numeric_limits<int64_t>::min()) {}
581cb0ef41Sopenharmony_ci  const char* phase_name_;
591cb0ef41Sopenharmony_ci  const char* reducer_name_;
601cb0ef41Sopenharmony_ci  OriginKind origin_kind_;
611cb0ef41Sopenharmony_ci  int64_t created_from_;
621cb0ef41Sopenharmony_ci};
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciinline bool operator!=(const NodeOrigin& lhs, const NodeOrigin& rhs) {
651cb0ef41Sopenharmony_ci  return !(lhs == rhs);
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE NodeOriginTable final
691cb0ef41Sopenharmony_ci    : public NON_EXPORTED_BASE(ZoneObject) {
701cb0ef41Sopenharmony_ci public:
711cb0ef41Sopenharmony_ci  class V8_NODISCARD Scope final {
721cb0ef41Sopenharmony_ci   public:
731cb0ef41Sopenharmony_ci    Scope(NodeOriginTable* origins, const char* reducer_name, Node* node)
741cb0ef41Sopenharmony_ci        : origins_(origins), prev_origin_(NodeOrigin::Unknown()) {
751cb0ef41Sopenharmony_ci      if (origins) {
761cb0ef41Sopenharmony_ci        prev_origin_ = origins->current_origin_;
771cb0ef41Sopenharmony_ci        origins->current_origin_ =
781cb0ef41Sopenharmony_ci            NodeOrigin(origins->current_phase_name_, reducer_name, node->id());
791cb0ef41Sopenharmony_ci      }
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    ~Scope() {
831cb0ef41Sopenharmony_ci      if (origins_) origins_->current_origin_ = prev_origin_;
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    Scope(const Scope&) = delete;
871cb0ef41Sopenharmony_ci    Scope& operator=(const Scope&) = delete;
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci   private:
901cb0ef41Sopenharmony_ci    NodeOriginTable* const origins_;
911cb0ef41Sopenharmony_ci    NodeOrigin prev_origin_;
921cb0ef41Sopenharmony_ci  };
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  class V8_NODISCARD PhaseScope final {
951cb0ef41Sopenharmony_ci   public:
961cb0ef41Sopenharmony_ci    PhaseScope(NodeOriginTable* origins, const char* phase_name)
971cb0ef41Sopenharmony_ci        : origins_(origins) {
981cb0ef41Sopenharmony_ci      if (origins != nullptr) {
991cb0ef41Sopenharmony_ci        prev_phase_name_ = origins->current_phase_name_;
1001cb0ef41Sopenharmony_ci        origins->current_phase_name_ =
1011cb0ef41Sopenharmony_ci            phase_name == nullptr ? "unnamed" : phase_name;
1021cb0ef41Sopenharmony_ci      }
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci    ~PhaseScope() {
1061cb0ef41Sopenharmony_ci      if (origins_) origins_->current_phase_name_ = prev_phase_name_;
1071cb0ef41Sopenharmony_ci    }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci    PhaseScope(const PhaseScope&) = delete;
1101cb0ef41Sopenharmony_ci    PhaseScope& operator=(const PhaseScope&) = delete;
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci   private:
1131cb0ef41Sopenharmony_ci    NodeOriginTable* const origins_;
1141cb0ef41Sopenharmony_ci    const char* prev_phase_name_;
1151cb0ef41Sopenharmony_ci  };
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  explicit NodeOriginTable(Graph* graph);
1181cb0ef41Sopenharmony_ci  NodeOriginTable(const NodeOriginTable&) = delete;
1191cb0ef41Sopenharmony_ci  NodeOriginTable& operator=(const NodeOriginTable&) = delete;
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  void AddDecorator();
1221cb0ef41Sopenharmony_ci  void RemoveDecorator();
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  NodeOrigin GetNodeOrigin(Node* node) const;
1251cb0ef41Sopenharmony_ci  void SetNodeOrigin(Node* node, const NodeOrigin& no);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  void SetCurrentPosition(const NodeOrigin& no) { current_origin_ = no; }
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  void PrintJson(std::ostream& os) const;
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci private:
1321cb0ef41Sopenharmony_ci  class Decorator;
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  Graph* const graph_;
1351cb0ef41Sopenharmony_ci  Decorator* decorator_;
1361cb0ef41Sopenharmony_ci  NodeOrigin current_origin_;
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  const char* current_phase_name_;
1391cb0ef41Sopenharmony_ci  static NodeOrigin UnknownNodeOrigin(Zone* zone) {
1401cb0ef41Sopenharmony_ci    return NodeOrigin::Unknown();
1411cb0ef41Sopenharmony_ci  }
1421cb0ef41Sopenharmony_ci  NodeAuxData<NodeOrigin, UnknownNodeOrigin> table_;
1431cb0ef41Sopenharmony_ci};
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci}  // namespace compiler
1461cb0ef41Sopenharmony_ci}  // namespace internal
1471cb0ef41Sopenharmony_ci}  // namespace v8
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci#endif  // V8_COMPILER_NODE_ORIGIN_TABLE_H_
150