state-values-utils.h 3.91 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 9
#include <array>
#include "src/compiler/common-operator.h"
10
#include "src/compiler/js-graph.h"
11
#include "src/globals.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17
class BitVector;

18 19 20 21
namespace compiler {

class Graph;

22
class V8_EXPORT_PRIVATE StateValuesCache {
23 24 25
 public:
  explicit StateValuesCache(JSGraph* js_graph);

26
  Node* GetNodeForValues(Node** values, size_t count,
27 28
                         const BitVector* liveness = nullptr,
                         int liveness_offset = 0);
29 30 31

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

  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;
43
    SparseInputMask mask;
44 45
    Node** values;

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

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

54 55 56 57 58 59 60 61
  // 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.
  SparseInputMask::BitMaskType FillBufferWithValues(WorkingBuffer* node_buffer,
                                                    size_t* node_count,
                                                    size_t* values_idx,
                                                    Node** values, size_t count,
62 63
                                                    const BitVector* liveness,
                                                    int liveness_offset);
64 65

  Node* BuildTree(size_t* values_idx, Node** values, size_t count,
66
                  const BitVector* liveness, int liveness_offset, size_t level);
67 68

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

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

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

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

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

92
  class V8_EXPORT_PRIVATE iterator {
93 94 95 96
   public:
    // Bare minimum of operators needed for range iteration.
    bool operator!=(iterator& other);
    iterator& operator++();
97
    TypedNode operator*();
98 99 100 101 102 103 104 105

   private:
    friend class StateValuesAccess;

    iterator() : current_depth_(-1) {}
    explicit iterator(Node* node);

    Node* node();
106
    MachineType type();
107 108
    bool done();
    void Advance();
109
    void EnsureValid();
110

111
    SparseInputMask::InputIterator* Top();
112 113 114 115
    void Push(Node* node);
    void Pop();

    static const int kMaxInlineDepth = 8;
116
    SparseInputMask::InputIterator stack_[kMaxInlineDepth];
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    int current_depth_;
  };

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

  size_t size();
  iterator begin() { return iterator(node_); }
  iterator end() { return iterator(); }

 private:
  Node* node_;
};

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

#endif  // V8_COMPILER_STATE_VALUES_UTILS_H_