1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_COMPILER_NODE_ORIGIN_TABLE_H_ 6 #define V8_COMPILER_NODE_ORIGIN_TABLE_H_ 7 8 #include <limits> 9 10 #include "src/base/compiler-specific.h" 11 #include "src/codegen/source-position.h" 12 #include "src/common/globals.h" 13 #include "src/compiler/node-aux-data.h" 14 15 namespace v8 { 16 namespace internal { 17 namespace compiler { 18 19 class NodeOrigin { 20 public: 21 enum OriginKind { kWasmBytecode, kGraphNode }; NodeOrigin(const char * phase_name,const char * reducer_name,NodeId created_from)22 NodeOrigin(const char* phase_name, const char* reducer_name, 23 NodeId created_from) 24 : phase_name_(phase_name), 25 reducer_name_(reducer_name), 26 origin_kind_(kGraphNode), 27 created_from_(created_from) {} 28 NodeOrigin(const char * phase_name,const char * reducer_name,OriginKind origin_kind,uint64_t created_from)29 NodeOrigin(const char* phase_name, const char* reducer_name, 30 OriginKind origin_kind, uint64_t created_from) 31 : phase_name_(phase_name), 32 reducer_name_(reducer_name), 33 origin_kind_(origin_kind), 34 created_from_(created_from) {} 35 36 NodeOrigin(const NodeOrigin& other) V8_NOEXCEPT = default; Unknown()37 static NodeOrigin Unknown() { return NodeOrigin(); } 38 IsKnown()39 bool IsKnown() { return created_from_ >= 0; } created_from()40 int64_t created_from() const { return created_from_; } reducer_name()41 const char* reducer_name() const { return reducer_name_; } phase_name()42 const char* phase_name() const { return phase_name_; } 43 origin_kind()44 OriginKind origin_kind() const { return origin_kind_; } 45 46 bool operator==(const NodeOrigin& o) const { 47 return reducer_name_ == o.reducer_name_ && created_from_ == o.created_from_; 48 } 49 50 void PrintJson(std::ostream& out) const; 51 52 private: NodeOrigin()53 NodeOrigin() 54 : phase_name_(""), 55 reducer_name_(""), 56 created_from_(std::numeric_limits<int64_t>::min()) {} 57 const char* phase_name_; 58 const char* reducer_name_; 59 OriginKind origin_kind_; 60 int64_t created_from_; 61 }; 62 63 inline bool operator!=(const NodeOrigin& lhs, const NodeOrigin& rhs) { 64 return !(lhs == rhs); 65 } 66 67 class V8_EXPORT_PRIVATE NodeOriginTable final NON_EXPORTED_BASE(ZoneObject)68 : public NON_EXPORTED_BASE(ZoneObject) { 69 public: 70 class Scope final { 71 public: 72 Scope(NodeOriginTable* origins, const char* reducer_name, Node* node) 73 : origins_(origins), prev_origin_(NodeOrigin::Unknown()) { 74 if (origins) { 75 prev_origin_ = origins->current_origin_; 76 origins->current_origin_ = 77 NodeOrigin(origins->current_phase_name_, reducer_name, node->id()); 78 } 79 } 80 81 ~Scope() { 82 if (origins_) origins_->current_origin_ = prev_origin_; 83 } 84 85 Scope(const Scope&) = delete; 86 Scope& operator=(const Scope&) = delete; 87 88 private: 89 NodeOriginTable* const origins_; 90 NodeOrigin prev_origin_; 91 }; 92 93 class PhaseScope final { 94 public: 95 PhaseScope(NodeOriginTable* origins, const char* phase_name) 96 : origins_(origins) { 97 if (origins != nullptr) { 98 prev_phase_name_ = origins->current_phase_name_; 99 origins->current_phase_name_ = 100 phase_name == nullptr ? "unnamed" : phase_name; 101 } 102 } 103 104 ~PhaseScope() { 105 if (origins_) origins_->current_phase_name_ = prev_phase_name_; 106 } 107 108 PhaseScope(const PhaseScope&) = delete; 109 PhaseScope& operator=(const PhaseScope&) = delete; 110 111 private: 112 NodeOriginTable* const origins_; 113 const char* prev_phase_name_; 114 }; 115 116 explicit NodeOriginTable(Graph* graph); 117 NodeOriginTable(const NodeOriginTable&) = delete; 118 NodeOriginTable& operator=(const NodeOriginTable&) = delete; 119 120 void AddDecorator(); 121 void RemoveDecorator(); 122 123 NodeOrigin GetNodeOrigin(Node* node) const; 124 void SetNodeOrigin(Node* node, const NodeOrigin& no); 125 126 void SetCurrentPosition(const NodeOrigin& no) { current_origin_ = no; } 127 128 void PrintJson(std::ostream& os) const; 129 130 private: 131 class Decorator; 132 133 Graph* const graph_; 134 Decorator* decorator_; 135 NodeOrigin current_origin_; 136 137 const char* current_phase_name_; 138 NodeAuxData<NodeOrigin, NodeOrigin::Unknown> table_; 139 }; 140 141 } // namespace compiler 142 } // namespace internal 143 } // namespace v8 144 145 #endif // V8_COMPILER_NODE_ORIGIN_TABLE_H_ 146