state-values-utils.cc 8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
// 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.

#include "src/compiler/state-values-utils.h"

namespace v8 {
namespace internal {
namespace compiler {

StateValuesCache::StateValuesCache(JSGraph* js_graph)
    : js_graph_(js_graph),
      hash_map_(AreKeysEqual, ZoneHashMap::kDefaultHashMapCapacity,
                ZoneAllocationPolicy(zone())),
      working_space_(zone()),
      empty_state_values_(nullptr) {}


// static
bool StateValuesCache::AreKeysEqual(void* key1, void* key2) {
  NodeKey* node_key1 = reinterpret_cast<NodeKey*>(key1);
  NodeKey* node_key2 = reinterpret_cast<NodeKey*>(key2);

  if (node_key1->node == nullptr) {
    if (node_key2->node == nullptr) {
      return AreValueKeysEqual(reinterpret_cast<StateValuesKey*>(key1),
                               reinterpret_cast<StateValuesKey*>(key2));
    } else {
      return IsKeysEqualToNode(reinterpret_cast<StateValuesKey*>(key1),
                               node_key2->node);
    }
  } else {
    if (node_key2->node == nullptr) {
      // If the nodes are already processed, they must be the same.
      return IsKeysEqualToNode(reinterpret_cast<StateValuesKey*>(key2),
                               node_key1->node);
    } else {
      return node_key1->node == node_key2->node;
    }
  }
  UNREACHABLE();
}


// static
bool StateValuesCache::IsKeysEqualToNode(StateValuesKey* key, Node* node) {
  if (key->count != static_cast<size_t>(node->InputCount())) {
    return false;
  }
  for (size_t i = 0; i < key->count; i++) {
    if (key->values[i] != node->InputAt(static_cast<int>(i))) {
      return false;
    }
  }
  return true;
}


// static
bool StateValuesCache::AreValueKeysEqual(StateValuesKey* key1,
                                         StateValuesKey* key2) {
  if (key1->count != key2->count) {
    return false;
  }
  for (size_t i = 0; i < key1->count; i++) {
    if (key1->values[i] != key2->values[i]) {
      return false;
    }
  }
  return true;
}


Node* StateValuesCache::GetEmptyStateValues() {
  if (empty_state_values_ == nullptr) {
    empty_state_values_ = graph()->NewNode(common()->StateValues(0));
  }
  return empty_state_values_;
}


NodeVector* StateValuesCache::GetWorkingSpace(size_t level) {
  while (working_space_.size() <= level) {
    void* space = zone()->New(sizeof(NodeVector));
    working_space_.push_back(new (space)
                                 NodeVector(kMaxInputCount, nullptr, zone()));
  }
  return working_space_[level];
}

namespace {

int StateValuesHashKey(Node** nodes, size_t count) {
  size_t hash = count;
  for (size_t i = 0; i < count; i++) {
    hash = hash * 23 + nodes[i]->id();
  }
  return static_cast<int>(hash & 0x7fffffff);
}

}  // namespace


Node* StateValuesCache::GetValuesNodeFromCache(Node** nodes, size_t count) {
  StateValuesKey key(count, nodes);
  int hash = StateValuesHashKey(nodes, count);
  ZoneHashMap::Entry* lookup =
108
      hash_map_.LookupOrInsert(&key, hash, ZoneAllocationPolicy(zone()));
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  DCHECK_NOT_NULL(lookup);
  Node* node;
  if (lookup->value == nullptr) {
    int input_count = static_cast<int>(count);
    node = graph()->NewNode(common()->StateValues(input_count), input_count,
                            nodes);
    NodeKey* new_key = new (zone()->New(sizeof(NodeKey))) NodeKey(node);
    lookup->key = new_key;
    lookup->value = node;
  } else {
    node = reinterpret_cast<Node*>(lookup->value);
  }
  return node;
}


class StateValuesCache::ValueArrayIterator {
 public:
  ValueArrayIterator(Node** values, size_t count)
      : values_(values), count_(count), current_(0) {}

  void Advance() {
    if (!done()) {
      current_++;
    }
  }

  bool done() { return current_ >= count_; }

  Node* node() {
    DCHECK(!done());
    return values_[current_];
  }

 private:
  Node** values_;
  size_t count_;
  size_t current_;
};


Node* StateValuesCache::BuildTree(ValueArrayIterator* it, size_t max_height) {
  if (max_height == 0) {
    Node* node = it->node();
    it->Advance();
    return node;
  }
  DCHECK(!it->done());

  NodeVector* buffer = GetWorkingSpace(max_height);
  size_t count = 0;
  for (; count < kMaxInputCount; count++) {
    if (it->done()) break;
    (*buffer)[count] = BuildTree(it, max_height - 1);
  }
  if (count == 1) {
    return (*buffer)[0];
  } else {
    return GetValuesNodeFromCache(&(buffer->front()), count);
  }
}


Node* StateValuesCache::GetNodeForValues(Node** values, size_t count) {
173 174 175
#if DEBUG
  for (size_t i = 0; i < count; i++) {
    DCHECK_NE(values[i]->opcode(), IrOpcode::kStateValues);
176
    DCHECK_NE(values[i]->opcode(), IrOpcode::kTypedStateValues);
177 178
  }
#endif
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  if (count == 0) {
    return GetEmptyStateValues();
  }
  size_t height = 0;
  size_t max_nodes = 1;
  while (count > max_nodes) {
    height++;
    max_nodes *= kMaxInputCount;
  }

  ValueArrayIterator it(values, count);

  Node* tree = BuildTree(&it, height);

  // If the 'tree' is a single node, equip it with a StateValues wrapper.
194 195
  if (tree->opcode() != IrOpcode::kStateValues &&
      tree->opcode() != IrOpcode::kTypedStateValues) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    tree = GetValuesNodeFromCache(&tree, 1);
  }

  return tree;
}


StateValuesAccess::iterator::iterator(Node* node) : current_depth_(0) {
  // A hacky way initialize - just set the index before the node we want
  // to process and then advance to it.
  stack_[current_depth_].node = node;
  stack_[current_depth_].index = -1;
  Advance();
}


StateValuesAccess::iterator::StatePos* StateValuesAccess::iterator::Top() {
  DCHECK(current_depth_ >= 0);
  DCHECK(current_depth_ < kMaxInlineDepth);
  return &(stack_[current_depth_]);
}


void StateValuesAccess::iterator::Push(Node* node) {
  current_depth_++;
  CHECK(current_depth_ < kMaxInlineDepth);
  stack_[current_depth_].node = node;
  stack_[current_depth_].index = 0;
}


void StateValuesAccess::iterator::Pop() {
  DCHECK(current_depth_ >= 0);
  current_depth_--;
}


bool StateValuesAccess::iterator::done() { return current_depth_ < 0; }


void StateValuesAccess::iterator::Advance() {
  // Advance the current index.
  Top()->index++;

  // Fix up the position to point to a valid node.
  while (true) {
    // TODO(jarin): Factor to a separate method.
    Node* node = Top()->node;
    int index = Top()->index;

    if (index >= node->InputCount()) {
      // Pop stack and move to the next sibling.
      Pop();
      if (done()) {
        // Stack is exhausted, we have reached the end.
        return;
      }
      Top()->index++;
254 255
    } else if (node->InputAt(index)->opcode() == IrOpcode::kStateValues ||
               node->InputAt(index)->opcode() == IrOpcode::kTypedStateValues) {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
      // Nested state, we need to push to the stack.
      Push(node->InputAt(index));
    } else {
      // We are on a valid node, we can stop the iteration.
      return;
    }
  }
}


Node* StateValuesAccess::iterator::node() {
  return Top()->node->InputAt(Top()->index);
}


271 272 273
MachineType StateValuesAccess::iterator::type() {
  Node* state = Top()->node;
  if (state->opcode() == IrOpcode::kStateValues) {
274
    return MachineType::AnyTagged();
275 276 277 278 279 280 281 282 283
  } else {
    DCHECK_EQ(IrOpcode::kTypedStateValues, state->opcode());
    const ZoneVector<MachineType>* types =
        OpParameter<const ZoneVector<MachineType>*>(state);
    return (*types)[Top()->index];
  }
}


284 285 286 287 288 289 290 291 292 293 294 295 296
bool StateValuesAccess::iterator::operator!=(iterator& other) {
  // We only allow comparison with end().
  CHECK(other.done());
  return !done();
}


StateValuesAccess::iterator& StateValuesAccess::iterator::operator++() {
  Advance();
  return *this;
}


297 298 299
StateValuesAccess::TypedNode StateValuesAccess::iterator::operator*() {
  return TypedNode(node(), type());
}
300 301 302 303 304


size_t StateValuesAccess::size() {
  size_t count = 0;
  for (int i = 0; i < node_->InputCount(); i++) {
305 306
    if (node_->InputAt(i)->opcode() == IrOpcode::kStateValues ||
        node_->InputAt(i)->opcode() == IrOpcode::kTypedStateValues) {
307 308 309 310 311 312 313 314 315 316 317
      count += StateValuesAccess(node_->InputAt(i)).size();
    } else {
      count++;
    }
  }
  return count;
}

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