• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
37   NodeOrigin& operator=(const NodeOrigin& other) V8_NOEXCEPT = default;
Unknown()38   static NodeOrigin Unknown() { return NodeOrigin(); }
39 
IsKnown()40   bool IsKnown() { return created_from_ >= 0; }
created_from()41   int64_t created_from() const { return created_from_; }
reducer_name()42   const char* reducer_name() const { return reducer_name_; }
phase_name()43   const char* phase_name() const { return phase_name_; }
44 
origin_kind()45   OriginKind origin_kind() const { return origin_kind_; }
46 
47   bool operator==(const NodeOrigin& o) const {
48     return reducer_name_ == o.reducer_name_ && created_from_ == o.created_from_;
49   }
50 
51   void PrintJson(std::ostream& out) const;
52 
53  private:
NodeOrigin()54   NodeOrigin()
55       : phase_name_(""),
56         reducer_name_(""),
57         created_from_(std::numeric_limits<int64_t>::min()) {}
58   const char* phase_name_;
59   const char* reducer_name_;
60   OriginKind origin_kind_;
61   int64_t created_from_;
62 };
63 
64 inline bool operator!=(const NodeOrigin& lhs, const NodeOrigin& rhs) {
65   return !(lhs == rhs);
66 }
67 
68 class V8_EXPORT_PRIVATE NodeOriginTable final
NON_EXPORTED_BASE(ZoneObject)69     : public NON_EXPORTED_BASE(ZoneObject) {
70  public:
71   class V8_NODISCARD Scope final {
72    public:
73     Scope(NodeOriginTable* origins, const char* reducer_name, Node* node)
74         : origins_(origins), prev_origin_(NodeOrigin::Unknown()) {
75       if (origins) {
76         prev_origin_ = origins->current_origin_;
77         origins->current_origin_ =
78             NodeOrigin(origins->current_phase_name_, reducer_name, node->id());
79       }
80     }
81 
82     ~Scope() {
83       if (origins_) origins_->current_origin_ = prev_origin_;
84     }
85 
86     Scope(const Scope&) = delete;
87     Scope& operator=(const Scope&) = delete;
88 
89    private:
90     NodeOriginTable* const origins_;
91     NodeOrigin prev_origin_;
92   };
93 
94   class V8_NODISCARD PhaseScope final {
95    public:
96     PhaseScope(NodeOriginTable* origins, const char* phase_name)
97         : origins_(origins) {
98       if (origins != nullptr) {
99         prev_phase_name_ = origins->current_phase_name_;
100         origins->current_phase_name_ =
101             phase_name == nullptr ? "unnamed" : phase_name;
102       }
103     }
104 
105     ~PhaseScope() {
106       if (origins_) origins_->current_phase_name_ = prev_phase_name_;
107     }
108 
109     PhaseScope(const PhaseScope&) = delete;
110     PhaseScope& operator=(const PhaseScope&) = delete;
111 
112    private:
113     NodeOriginTable* const origins_;
114     const char* prev_phase_name_;
115   };
116 
117   explicit NodeOriginTable(Graph* graph);
118   NodeOriginTable(const NodeOriginTable&) = delete;
119   NodeOriginTable& operator=(const NodeOriginTable&) = delete;
120 
121   void AddDecorator();
122   void RemoveDecorator();
123 
124   NodeOrigin GetNodeOrigin(Node* node) const;
125   void SetNodeOrigin(Node* node, const NodeOrigin& no);
126 
127   void SetCurrentPosition(const NodeOrigin& no) { current_origin_ = no; }
128 
129   void PrintJson(std::ostream& os) const;
130 
131  private:
132   class Decorator;
133 
134   Graph* const graph_;
135   Decorator* decorator_;
136   NodeOrigin current_origin_;
137 
138   const char* current_phase_name_;
139   static NodeOrigin UnknownNodeOrigin(Zone* zone) {
140     return NodeOrigin::Unknown();
141   }
142   NodeAuxData<NodeOrigin, UnknownNodeOrigin> table_;
143 };
144 
145 }  // namespace compiler
146 }  // namespace internal
147 }  // namespace v8
148 
149 #endif  // V8_COMPILER_NODE_ORIGIN_TABLE_H_
150