state-values-utils.h 4.07 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 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_STATE_VALUES_UTILS_H_
#define V8_COMPILER_STATE_VALUES_UTILS_H_

8
#include <array>
9

10
#include "src/common/globals.h"
11
#include "src/compiler/common-operator.h"
12
#include "src/compiler/js-graph.h"
13
#include "src/zone/zone-hashmap.h"
14 15 16 17

namespace v8 {
namespace internal {

18 19
class BitVector;

20 21 22
namespace compiler {

class Graph;
23
class BytecodeLivenessState;
24

25
class V8_EXPORT_PRIVATE StateValuesCache {
26 27 28
 public:
  explicit StateValuesCache(JSGraph* js_graph);

29
  Node* GetNodeForValues(Node** values, size_t count,
30
                         const BytecodeLivenessState* liveness = nullptr);
31 32 33

 private:
  static const size_t kMaxInputCount = 8;
34
  using WorkingBuffer = std::array<Node*, kMaxInputCount>;
35 36 37 38 39 40 41 42 43 44

  struct NodeKey {
    Node* node;

    explicit NodeKey(Node* node) : node(node) {}
  };

  struct StateValuesKey : public NodeKey {
    // ValueArray - array of nodes ({node} has to be nullptr).
    size_t count;
45
    SparseInputMask mask;
46 47
    Node** values;

48 49
    StateValuesKey(size_t count, SparseInputMask mask, Node** values)
        : NodeKey(nullptr), count(count), mask(mask), values(values) {}
50 51 52 53 54 55
  };

  static bool AreKeysEqual(void* key1, void* key2);
  static bool IsKeysEqualToNode(StateValuesKey* key, Node* node);
  static bool AreValueKeysEqual(StateValuesKey* key1, StateValuesKey* key2);

56 57 58 59
  // Fills {node_buffer}, starting from {node_count}, with {values}, starting
  // at {values_idx}, sparsely encoding according to {liveness}. {node_count} is
  // updated with the new number of inputs in {node_buffer}, and a bitmask of
  // the sparse encoding is returned.
60 61 62
  SparseInputMask::BitMaskType FillBufferWithValues(
      WorkingBuffer* node_buffer, size_t* node_count, size_t* values_idx,
      Node** values, size_t count, const BytecodeLivenessState* liveness);
63 64

  Node* BuildTree(size_t* values_idx, Node** values, size_t count,
65
                  const BytecodeLivenessState* liveness, size_t level);
66 67

  WorkingBuffer* GetWorkingSpace(size_t level);
68
  Node* GetEmptyStateValues();
69 70
  Node* GetValuesNodeFromCache(Node** nodes, size_t count,
                               SparseInputMask mask);
71 72 73 74 75 76 77

  Graph* graph() { return js_graph_->graph(); }
  CommonOperatorBuilder* common() { return js_graph_->common(); }

  Zone* zone() { return graph()->zone(); }

  JSGraph* js_graph_;
78
  CustomMatcherZoneHashMap hash_map_;
79
  ZoneVector<WorkingBuffer> working_space_;  // One working space per level.
80 81 82
  Node* empty_state_values_;
};

83
class V8_EXPORT_PRIVATE StateValuesAccess {
84
 public:
85 86 87 88 89 90
  struct TypedNode {
    Node* node;
    MachineType type;
    TypedNode(Node* node, MachineType type) : node(node), type(type) {}
  };

91
  class V8_EXPORT_PRIVATE iterator {
92
   public:
93
    bool operator!=(iterator const& other) const;
94 95
    iterator& operator++();
    TypedNode operator*();
96

97 98 99 100 101 102
    Node* node();
    bool done() const { return current_depth_ < 0; }

    // Returns the number of empty nodes that were skipped over.
    size_t AdvanceTillNotEmpty();

103 104 105 106
   private:
    friend class StateValuesAccess;

    iterator() : current_depth_(-1) {}
107 108 109 110 111 112 113 114 115
    explicit iterator(Node* node);

    MachineType type();
    void Advance();
    void EnsureValid();

    SparseInputMask::InputIterator* Top();
    void Push(Node* node);
    void Pop();
116 117

    static const int kMaxInlineDepth = 8;
118
    SparseInputMask::InputIterator stack_[kMaxInlineDepth];
119 120 121 122 123
    int current_depth_;
  };

  explicit StateValuesAccess(Node* node) : node_(node) {}

124 125
  size_t size() const;
  iterator begin() const { return iterator(node_); }
126 127 128 129 130 131 132 133 134 135 136
  iterator begin_without_receiver() const {
    return ++begin();  // Skip the receiver.
  }
  iterator begin_without_receiver_and_skip(int n_skips) {
    iterator it = begin_without_receiver();
    while (n_skips > 0 && !it.done()) {
      ++it;
      --n_skips;
    }
    return it;
  }
137
  iterator end() const { return iterator(); }
138 139 140 141 142 143 144 145 146 147

 private:
  Node* node_;
};

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

#endif  // V8_COMPILER_STATE_VALUES_UTILS_H_