Home | History | Annotate | Download | only in compiler
      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/compiler/node-aux-data.h"
     12 #include "src/globals.h"
     13 #include "src/source-position.h"
     14 
     15 namespace v8 {
     16 namespace internal {
     17 namespace compiler {
     18 
     19 class NodeOrigin {
     20  public:
     21   typedef enum { kWasmBytecode, kGraphNode } OriginKind;
     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 
     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) = default;
     37   static NodeOrigin Unknown() { return NodeOrigin(); }
     38 
     39   bool IsKnown() { return created_from_ >= 0; }
     40   int64_t created_from() const { return created_from_; }
     41   const char* reducer_name() const { return reducer_name_; }
     42   const char* phase_name() const { return phase_name_; }
     43 
     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:
     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
     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    private:
     86     NodeOriginTable* const origins_;
     87     NodeOrigin prev_origin_;
     88     DISALLOW_COPY_AND_ASSIGN(Scope);
     89   };
     90 
     91   class PhaseScope final {
     92    public:
     93     PhaseScope(NodeOriginTable* origins, const char* phase_name)
     94         : origins_(origins) {
     95       if (origins != nullptr) {
     96         prev_phase_name_ = origins->current_phase_name_;
     97         origins->current_phase_name_ =
     98             phase_name == nullptr ? "unnamed" : phase_name;
     99       }
    100     }
    101 
    102     ~PhaseScope() {
    103       if (origins_) origins_->current_phase_name_ = prev_phase_name_;
    104     }
    105 
    106    private:
    107     NodeOriginTable* const origins_;
    108     const char* prev_phase_name_;
    109     DISALLOW_COPY_AND_ASSIGN(PhaseScope);
    110   };
    111 
    112   explicit NodeOriginTable(Graph* graph);
    113 
    114   void AddDecorator();
    115   void RemoveDecorator();
    116 
    117   NodeOrigin GetNodeOrigin(Node* node) const;
    118   void SetNodeOrigin(Node* node, const NodeOrigin& no);
    119 
    120   void SetCurrentPosition(const NodeOrigin& no) { current_origin_ = no; }
    121 
    122   void PrintJson(std::ostream& os) const;
    123 
    124  private:
    125   class Decorator;
    126 
    127   Graph* const graph_;
    128   Decorator* decorator_;
    129   NodeOrigin current_origin_;
    130 
    131   const char* current_phase_name_;
    132   NodeAuxData<NodeOrigin, NodeOrigin::Unknown> table_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(NodeOriginTable);
    135 };
    136 
    137 }  // namespace compiler
    138 }  // namespace internal
    139 }  // namespace v8
    140 
    141 #endif  // V8_COMPILER_NODE_ORIGIN_TABLE_H_
    142