node-origin-table.h 4.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 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.

#ifndef V8_COMPILER_NODE_ORIGIN_TABLE_H_
#define V8_COMPILER_NODE_ORIGIN_TABLE_H_

#include <limits>

#include "src/base/compiler-specific.h"
11
#include "src/codegen/source-position.h"
12
#include "src/common/globals.h"
13 14 15 16 17 18 19 20
#include "src/compiler/node-aux-data.h"

namespace v8 {
namespace internal {
namespace compiler {

class NodeOrigin {
 public:
21
  enum OriginKind { kWasmBytecode, kGraphNode };
22 23 24 25
  NodeOrigin(const char* phase_name, const char* reducer_name,
             NodeId created_from)
      : phase_name_(phase_name),
        reducer_name_(reducer_name),
26
        origin_kind_(kGraphNode),
27
        created_from_(created_from) {}
28 29 30 31 32 33 34 35

  NodeOrigin(const char* phase_name, const char* reducer_name,
             OriginKind origin_kind, uint64_t created_from)
      : phase_name_(phase_name),
        reducer_name_(reducer_name),
        origin_kind_(origin_kind),
        created_from_(created_from) {}

36
  NodeOrigin(const NodeOrigin& other) V8_NOEXCEPT = default;
37
  NodeOrigin& operator=(const NodeOrigin& other) V8_NOEXCEPT = default;
38 39 40 41 42
  static NodeOrigin Unknown() { return NodeOrigin(); }

  bool IsKnown() { return created_from_ >= 0; }
  int64_t created_from() const { return created_from_; }
  const char* reducer_name() const { return reducer_name_; }
43
  const char* phase_name() const { return phase_name_; }
44

45 46
  OriginKind origin_kind() const { return origin_kind_; }

47 48 49 50 51 52 53 54
  bool operator==(const NodeOrigin& o) const {
    return reducer_name_ == o.reducer_name_ && created_from_ == o.created_from_;
  }

  void PrintJson(std::ostream& out) const;

 private:
  NodeOrigin()
55 56 57 58
      : phase_name_(""),
        reducer_name_(""),
        created_from_(std::numeric_limits<int64_t>::min()) {}
  const char* phase_name_;
59
  const char* reducer_name_;
60
  OriginKind origin_kind_;
61 62 63 64 65 66 67 68 69 70
  int64_t created_from_;
};

inline bool operator!=(const NodeOrigin& lhs, const NodeOrigin& rhs) {
  return !(lhs == rhs);
}

class V8_EXPORT_PRIVATE NodeOriginTable final
    : public NON_EXPORTED_BASE(ZoneObject) {
 public:
71
  class V8_NODISCARD Scope final {
72 73 74 75 76
   public:
    Scope(NodeOriginTable* origins, const char* reducer_name, Node* node)
        : origins_(origins), prev_origin_(NodeOrigin::Unknown()) {
      if (origins) {
        prev_origin_ = origins->current_origin_;
77 78
        origins->current_origin_ =
            NodeOrigin(origins->current_phase_name_, reducer_name, node->id());
79 80 81 82 83 84 85
      }
    }

    ~Scope() {
      if (origins_) origins_->current_origin_ = prev_origin_;
    }

86 87 88
    Scope(const Scope&) = delete;
    Scope& operator=(const Scope&) = delete;

89 90 91 92 93
   private:
    NodeOriginTable* const origins_;
    NodeOrigin prev_origin_;
  };

94
  class V8_NODISCARD PhaseScope final {
95 96 97 98 99 100 101 102 103 104 105 106 107 108
   public:
    PhaseScope(NodeOriginTable* origins, const char* phase_name)
        : origins_(origins) {
      if (origins != nullptr) {
        prev_phase_name_ = origins->current_phase_name_;
        origins->current_phase_name_ =
            phase_name == nullptr ? "unnamed" : phase_name;
      }
    }

    ~PhaseScope() {
      if (origins_) origins_->current_phase_name_ = prev_phase_name_;
    }

109 110 111
    PhaseScope(const PhaseScope&) = delete;
    PhaseScope& operator=(const PhaseScope&) = delete;

112 113 114 115 116
   private:
    NodeOriginTable* const origins_;
    const char* prev_phase_name_;
  };

117
  explicit NodeOriginTable(Graph* graph);
118 119
  NodeOriginTable(const NodeOriginTable&) = delete;
  NodeOriginTable& operator=(const NodeOriginTable&) = delete;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

  void AddDecorator();
  void RemoveDecorator();

  NodeOrigin GetNodeOrigin(Node* node) const;
  void SetNodeOrigin(Node* node, const NodeOrigin& no);

  void SetCurrentPosition(const NodeOrigin& no) { current_origin_ = no; }

  void PrintJson(std::ostream& os) const;

 private:
  class Decorator;

  Graph* const graph_;
  Decorator* decorator_;
  NodeOrigin current_origin_;
137 138

  const char* current_phase_name_;
139 140 141 142
  static NodeOrigin UnknownNodeOrigin(Zone* zone) {
    return NodeOrigin::Unknown();
  }
  NodeAuxData<NodeOrigin, UnknownNodeOrigin> table_;
143 144 145 146 147 148 149
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_NODE_ORIGIN_TABLE_H_