node-origin-table.h 3.82 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 38 39 40 41
  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_; }
42
  const char* phase_name() const { return phase_name_; }
43

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

46 47 48 49 50 51 52 53
  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()
54 55 56 57
      : phase_name_(""),
        reducer_name_(""),
        created_from_(std::numeric_limits<int64_t>::min()) {}
  const char* phase_name_;
58
  const char* reducer_name_;
59
  OriginKind origin_kind_;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  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:
  class Scope final {
   public:
    Scope(NodeOriginTable* origins, const char* reducer_name, Node* node)
        : origins_(origins), prev_origin_(NodeOrigin::Unknown()) {
      if (origins) {
        prev_origin_ = origins->current_origin_;
76 77
        origins->current_origin_ =
            NodeOrigin(origins->current_phase_name_, reducer_name, node->id());
78 79 80 81 82 83 84 85 86 87 88 89 90
      }
    }

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

   private:
    NodeOriginTable* const origins_;
    NodeOrigin prev_origin_;
    DISALLOW_COPY_AND_ASSIGN(Scope);
  };

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  class PhaseScope final {
   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_;
    }

   private:
    NodeOriginTable* const origins_;
    const char* prev_phase_name_;
    DISALLOW_COPY_AND_ASSIGN(PhaseScope);
  };

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  explicit NodeOriginTable(Graph* graph);

  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_;
130 131

  const char* current_phase_name_;
132 133 134 135 136 137 138 139 140 141
  NodeAuxData<NodeOrigin, NodeOrigin::Unknown> table_;

  DISALLOW_COPY_AND_ASSIGN(NodeOriginTable);
};

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

#endif  // V8_COMPILER_NODE_ORIGIN_TABLE_H_