type-feedback-vector.cc 22.1 KB
Newer Older
1 2 3 4
// Copyright 2014 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.

5
#include "src/type-feedback-vector.h"
6

7
#include "src/code-stubs.h"
8
#include "src/ic/ic.h"
9
#include "src/ic/ic-state.h"
10
#include "src/objects.h"
11
#include "src/type-feedback-vector-inl.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17 18 19 20 21 22 23 24 25
// static
TypeFeedbackVector::VectorICKind TypeFeedbackVector::FromCodeKind(
    Code::Kind kind) {
  switch (kind) {
    case Code::CALL_IC:
      return KindCallIC;
    case Code::LOAD_IC:
      return KindLoadIC;
    case Code::KEYED_LOAD_IC:
      return KindKeyedLoadIC;
26 27 28 29
    case Code::STORE_IC:
      return KindStoreIC;
    case Code::KEYED_STORE_IC:
      return KindKeyedStoreIC;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    default:
      // Shouldn't get here.
      UNREACHABLE();
  }

  return KindUnused;
}


// static
Code::Kind TypeFeedbackVector::FromVectorICKind(VectorICKind kind) {
  switch (kind) {
    case KindCallIC:
      return Code::CALL_IC;
    case KindLoadIC:
      return Code::LOAD_IC;
    case KindKeyedLoadIC:
      return Code::KEYED_LOAD_IC;
48 49 50 51 52 53
    case KindStoreIC:
      DCHECK(FLAG_vector_stores);
      return Code::STORE_IC;
    case KindKeyedStoreIC:
      DCHECK(FLAG_vector_stores);
      return Code::KEYED_STORE_IC;
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
    case KindUnused:
      break;
  }
  // Sentinel for no information.
  return Code::NUMBER_OF_KINDS;
}


Code::Kind TypeFeedbackVector::GetKind(FeedbackVectorICSlot slot) const {
  int index = VectorICComputer::index(kReservedIndexCount, slot.ToInt());
  int data = Smi::cast(get(index))->value();
  VectorICKind b = VectorICComputer::decode(data, slot.ToInt());
  return FromVectorICKind(b);
}


void TypeFeedbackVector::SetKind(FeedbackVectorICSlot slot, Code::Kind kind) {
  VectorICKind b = FromCodeKind(kind);
  int index = VectorICComputer::index(kReservedIndexCount, slot.ToInt());
  int data = Smi::cast(get(index))->value();
  int new_data = VectorICComputer::encode(data, slot.ToInt(), b);
  set(index, Smi::FromInt(new_data));
}


79 80 81 82 83 84
template Handle<TypeFeedbackVector> TypeFeedbackVector::Allocate(
    Isolate* isolate, const FeedbackVectorSpec* spec);
template Handle<TypeFeedbackVector> TypeFeedbackVector::Allocate(
    Isolate* isolate, const ZoneFeedbackVectorSpec* spec);


85
// static
86 87 88 89 90
template <typename Spec>
Handle<TypeFeedbackVector> TypeFeedbackVector::Allocate(Isolate* isolate,
                                                        const Spec* spec) {
  const int slot_count = spec->slots();
  const int ic_slot_count = spec->ic_slots();
91
  const int index_count = VectorICComputer::word_count(ic_slot_count);
92 93
  const int length = slot_count + (ic_slot_count * elements_per_ic_slot()) +
                     index_count + kReservedIndexCount;
94 95 96 97 98 99 100 101
  if (length == kReservedIndexCount) {
    return Handle<TypeFeedbackVector>::cast(
        isolate->factory()->empty_fixed_array());
  }

  Handle<FixedArray> array = isolate->factory()->NewFixedArray(length, TENURED);
  if (ic_slot_count > 0) {
    array->set(kFirstICSlotIndex,
102
               Smi::FromInt(slot_count + index_count + kReservedIndexCount));
103 104 105 106 107
  } else {
    array->set(kFirstICSlotIndex, Smi::FromInt(length));
  }
  array->set(kWithTypesIndex, Smi::FromInt(0));
  array->set(kGenericCountIndex, Smi::FromInt(0));
108 109 110 111
  // Fill the indexes with zeros.
  for (int i = 0; i < index_count; i++) {
    array->set(kReservedIndexCount + i, Smi::FromInt(0));
  }
112 113 114 115

  // Ensure we can skip the write barrier
  Handle<Object> uninitialized_sentinel = UninitializedSentinel(isolate);
  DCHECK_EQ(isolate->heap()->uninitialized_symbol(), *uninitialized_sentinel);
116
  for (int i = kReservedIndexCount + index_count; i < length; i++) {
117 118
    array->set(i, *uninitialized_sentinel, SKIP_WRITE_BARRIER);
  }
119 120

  Handle<TypeFeedbackVector> vector = Handle<TypeFeedbackVector>::cast(array);
121 122
  for (int i = 0; i < ic_slot_count; i++) {
    vector->SetKind(FeedbackVectorICSlot(i), spec->GetKind(i));
123 124
  }
  return vector;
125 126 127
}


128 129 130 131 132 133 134 135
// static
Handle<TypeFeedbackVector> TypeFeedbackVector::Copy(
    Isolate* isolate, Handle<TypeFeedbackVector> vector) {
  Handle<TypeFeedbackVector> result;
  result = Handle<TypeFeedbackVector>::cast(
      isolate->factory()->CopyFixedArray(Handle<FixedArray>::cast(vector)));
  return result;
}
136 137


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
bool TypeFeedbackVector::SpecDiffersFrom(
    const ZoneFeedbackVectorSpec* other_spec) const {
  if (other_spec->slots() != Slots() || other_spec->ic_slots() != ICSlots()) {
    return true;
  }

  int ic_slots = ICSlots();
  for (int i = 0; i < ic_slots; i++) {
    if (GetKind(FeedbackVectorICSlot(i)) != other_spec->GetKind(i)) {
      return true;
    }
  }
  return false;
}


154 155
// This logic is copied from
// StaticMarkingVisitor<StaticVisitor>::VisitCodeTarget.
156
static bool ClearLogic(Heap* heap) {
157 158
  return FLAG_cleanup_code_caches_at_gc &&
         heap->isolate()->serializer_enabled();
159 160 161
}


162 163
void TypeFeedbackVector::ClearSlotsImpl(SharedFunctionInfo* shared,
                                        bool force_clear) {
164
  int slots = Slots();
165 166 167
  Heap* heap = GetIsolate()->heap();

  if (!force_clear && !ClearLogic(heap)) return;
168

169 170
  Object* uninitialized_sentinel =
      TypeFeedbackVector::RawUninitializedSentinel(heap);
171 172 173 174 175 176 177 178 179 180 181 182 183 184
  for (int i = 0; i < slots; i++) {
    FeedbackVectorSlot slot(i);
    Object* obj = Get(slot);
    if (obj->IsHeapObject()) {
      InstanceType instance_type =
          HeapObject::cast(obj)->map()->instance_type();
      // AllocationSites are exempt from clearing. They don't store Maps
      // or Code pointers which can cause memory leaks if not cleared
      // regularly.
      if (instance_type != ALLOCATION_SITE_TYPE) {
        Set(slot, uninitialized_sentinel, SKIP_WRITE_BARRIER);
      }
    }
  }
185
}
186 187


188 189 190 191
void TypeFeedbackVector::ClearICSlotsImpl(SharedFunctionInfo* shared,
                                          bool force_clear) {
  Heap* heap = GetIsolate()->heap();

192
  if (!force_clear && !ClearLogic(heap)) return;
193 194 195 196 197

  int slots = ICSlots();
  Code* host = shared->code();
  Object* uninitialized_sentinel =
      TypeFeedbackVector::RawUninitializedSentinel(heap);
198 199 200 201
  for (int i = 0; i < slots; i++) {
    FeedbackVectorICSlot slot(i);
    Object* obj = Get(slot);
    if (obj != uninitialized_sentinel) {
202 203 204
      Code::Kind kind = GetKind(slot);
      if (kind == Code::CALL_IC) {
        CallICNexus nexus(this, slot);
205
        nexus.Clear(host);
206 207
      } else if (kind == Code::LOAD_IC) {
        LoadICNexus nexus(this, slot);
208
        nexus.Clear(host);
209 210
      } else if (kind == Code::KEYED_LOAD_IC) {
        KeyedLoadICNexus nexus(this, slot);
211
        nexus.Clear(host);
212 213 214 215 216 217 218 219
      } else if (kind == Code::STORE_IC) {
        DCHECK(FLAG_vector_stores);
        StoreICNexus nexus(this, slot);
        nexus.Clear(host);
      } else if (kind == Code::KEYED_STORE_IC) {
        DCHECK(FLAG_vector_stores);
        KeyedStoreICNexus nexus(this, slot);
        nexus.Clear(host);
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 254 255 256 257 258 259
// static
void TypeFeedbackVector::ClearAllKeyedStoreICs(Isolate* isolate) {
  DCHECK(FLAG_vector_stores);
  SharedFunctionInfo::Iterator iterator(isolate);
  SharedFunctionInfo* shared;
  while ((shared = iterator.Next())) {
    TypeFeedbackVector* vector = shared->feedback_vector();
    vector->ClearKeyedStoreICs(shared);
  }
}


void TypeFeedbackVector::ClearKeyedStoreICs(SharedFunctionInfo* shared) {
  Heap* heap = GetIsolate()->heap();

  int slots = ICSlots();
  Code* host = shared->code();
  Object* uninitialized_sentinel =
      TypeFeedbackVector::RawUninitializedSentinel(heap);
  for (int i = 0; i < slots; i++) {
    FeedbackVectorICSlot slot(i);
    Object* obj = Get(slot);
    if (obj != uninitialized_sentinel) {
      Code::Kind kind = GetKind(slot);
      if (kind == Code::KEYED_STORE_IC) {
        DCHECK(FLAG_vector_stores);
        KeyedStoreICNexus nexus(this, slot);
        nexus.Clear(host);
      }
    }
  }
}


260 261 262 263 264 265
// static
Handle<TypeFeedbackVector> TypeFeedbackVector::DummyVector(Isolate* isolate) {
  return Handle<TypeFeedbackVector>::cast(isolate->factory()->dummy_vector());
}


266 267 268 269 270 271 272 273 274 275 276 277 278
Handle<FixedArray> FeedbackNexus::EnsureArrayOfSize(int length) {
  Isolate* isolate = GetIsolate();
  Handle<Object> feedback = handle(GetFeedback(), isolate);
  if (!feedback->IsFixedArray() ||
      FixedArray::cast(*feedback)->length() != length) {
    Handle<FixedArray> array = isolate->factory()->NewFixedArray(length);
    SetFeedback(*array);
    return array;
  }
  return Handle<FixedArray>::cast(feedback);
}


279
Handle<FixedArray> FeedbackNexus::EnsureExtraArrayOfSize(int length) {
280
  Isolate* isolate = GetIsolate();
281 282 283 284 285 286 287 288 289 290 291 292 293 294
  Handle<Object> feedback_extra = handle(GetFeedbackExtra(), isolate);
  if (!feedback_extra->IsFixedArray() ||
      FixedArray::cast(*feedback_extra)->length() != length) {
    Handle<FixedArray> array = isolate->factory()->NewFixedArray(length);
    SetFeedbackExtra(*array);
    return array;
  }
  return Handle<FixedArray>::cast(feedback_extra);
}


void FeedbackNexus::InstallHandlers(Handle<FixedArray> array,
                                    MapHandleList* maps,
                                    CodeHandleList* handlers) {
295
  int receiver_count = maps->length();
296
  for (int current = 0; current < receiver_count; ++current) {
297
    Handle<Map> map = maps->at(current);
298
    Handle<WeakCell> cell = Map::WeakCellForMap(map);
299 300
    array->set(current * 2, *cell);
    array->set(current * 2 + 1, *handlers->at(current));
301 302 303 304
  }
}


305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
void FeedbackNexus::ConfigureUninitialized() {
  SetFeedback(*TypeFeedbackVector::UninitializedSentinel(GetIsolate()),
              SKIP_WRITE_BARRIER);
  SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(GetIsolate()),
                   SKIP_WRITE_BARRIER);
}


void FeedbackNexus::ConfigurePremonomorphic() {
  SetFeedback(*TypeFeedbackVector::PremonomorphicSentinel(GetIsolate()),
              SKIP_WRITE_BARRIER);
  SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(GetIsolate()),
                   SKIP_WRITE_BARRIER);
}


void FeedbackNexus::ConfigureMegamorphic() {
  Isolate* isolate = GetIsolate();
  SetFeedback(*TypeFeedbackVector::MegamorphicSentinel(isolate),
              SKIP_WRITE_BARRIER);
  SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(isolate),
                   SKIP_WRITE_BARRIER);
}


330 331 332
InlineCacheState LoadICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
333

334
  if (feedback == *TypeFeedbackVector::UninitializedSentinel(isolate)) {
335
    return UNINITIALIZED;
336
  } else if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
337
    return MEGAMORPHIC;
338
  } else if (feedback == *TypeFeedbackVector::PremonomorphicSentinel(isolate)) {
339 340
    return PREMONOMORPHIC;
  } else if (feedback->IsFixedArray()) {
341 342
    // Determine state purely by our structure, don't check if the maps are
    // cleared.
343 344 345 346
    return POLYMORPHIC;
  } else if (feedback->IsWeakCell()) {
    // Don't check if the map is cleared.
    return MONOMORPHIC;
347 348 349 350 351 352 353 354 355
  }

  return UNINITIALIZED;
}


InlineCacheState KeyedLoadICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
356

357
  if (feedback == *TypeFeedbackVector::UninitializedSentinel(isolate)) {
358
    return UNINITIALIZED;
359
  } else if (feedback == *TypeFeedbackVector::PremonomorphicSentinel(isolate)) {
360
    return PREMONOMORPHIC;
361
  } else if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
362
    return MEGAMORPHIC;
363
  } else if (feedback->IsFixedArray()) {
364 365
    // Determine state purely by our structure, don't check if the maps are
    // cleared.
366 367 368 369 370 371 372 373
    return POLYMORPHIC;
  } else if (feedback->IsWeakCell()) {
    // Don't check if the map is cleared.
    return MONOMORPHIC;
  } else if (feedback->IsName()) {
    Object* extra = GetFeedbackExtra();
    FixedArray* extra_array = FixedArray::cast(extra);
    return extra_array->length() > 2 ? POLYMORPHIC : MONOMORPHIC;
374 375 376 377 378 379
  }

  return UNINITIALIZED;
}


380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
InlineCacheState StoreICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();

  if (feedback == *TypeFeedbackVector::UninitializedSentinel(isolate)) {
    return UNINITIALIZED;
  } else if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
    return MEGAMORPHIC;
  } else if (feedback == *TypeFeedbackVector::PremonomorphicSentinel(isolate)) {
    return PREMONOMORPHIC;
  } else if (feedback->IsFixedArray()) {
    // Determine state purely by our structure, don't check if the maps are
    // cleared.
    return POLYMORPHIC;
  } else if (feedback->IsWeakCell()) {
    // Don't check if the map is cleared.
    return MONOMORPHIC;
  }

  return UNINITIALIZED;
}


InlineCacheState KeyedStoreICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();

  if (feedback == *TypeFeedbackVector::UninitializedSentinel(isolate)) {
    return UNINITIALIZED;
  } else if (feedback == *TypeFeedbackVector::PremonomorphicSentinel(isolate)) {
    return PREMONOMORPHIC;
  } else if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
    return MEGAMORPHIC;
  } else if (feedback->IsFixedArray()) {
    // Determine state purely by our structure, don't check if the maps are
    // cleared.
    return POLYMORPHIC;
  } else if (feedback->IsWeakCell()) {
    // Don't check if the map is cleared.
    return MONOMORPHIC;
  } else if (feedback->IsName()) {
    Object* extra = GetFeedbackExtra();
    FixedArray* extra_array = FixedArray::cast(extra);
    return extra_array->length() > 2 ? POLYMORPHIC : MONOMORPHIC;
  }

  return UNINITIALIZED;
}


430 431 432
InlineCacheState CallICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
433 434 435
  DCHECK(GetFeedbackExtra() ==
             *TypeFeedbackVector::UninitializedSentinel(isolate) ||
         GetFeedbackExtra()->IsSmi());
436

437
  if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
438
    return GENERIC;
439
  } else if (feedback->IsAllocationSite() || feedback->IsWeakCell()) {
440
    return MONOMORPHIC;
441 442
  }

443
  CHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate));
444
  return UNINITIALIZED;
445 446 447
}


448 449 450 451 452 453 454 455 456 457
int CallICNexus::ExtractCallCount() {
  Object* call_count = GetFeedbackExtra();
  if (call_count->IsSmi()) {
    int value = Smi::cast(call_count)->value() / 2;
    return value;
  }
  return -1;
}


458 459 460
void CallICNexus::Clear(Code* host) { CallIC::Clear(GetIsolate(), host, this); }


461 462 463 464 465 466 467
void CallICNexus::ConfigureMonomorphicArray() {
  Object* feedback = GetFeedback();
  if (!feedback->IsAllocationSite()) {
    Handle<AllocationSite> new_site =
        GetIsolate()->factory()->NewAllocationSite();
    SetFeedback(*new_site);
  }
468
  SetFeedbackExtra(Smi::FromInt(kCallCountIncrement), SKIP_WRITE_BARRIER);
469 470 471 472
}


void CallICNexus::ConfigureMonomorphic(Handle<JSFunction> function) {
473 474
  Handle<WeakCell> new_cell = GetIsolate()->factory()->NewWeakCell(function);
  SetFeedback(*new_cell);
475
  SetFeedbackExtra(Smi::FromInt(kCallCountIncrement), SKIP_WRITE_BARRIER);
476 477 478
}


479 480 481 482 483
void LoadICNexus::ConfigureMonomorphic(Handle<Map> receiver_map,
                                       Handle<Code> handler) {
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
  SetFeedback(*cell);
  SetFeedbackExtra(*handler);
484 485 486
}


487 488 489 490 491 492 493 494 495 496 497 498 499
void KeyedLoadICNexus::ConfigureMonomorphic(Handle<Name> name,
                                            Handle<Map> receiver_map,
                                            Handle<Code> handler) {
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
  if (name.is_null()) {
    SetFeedback(*cell);
    SetFeedbackExtra(*handler);
  } else {
    SetFeedback(*name);
    Handle<FixedArray> array = EnsureExtraArrayOfSize(2);
    array->set(0, *cell);
    array->set(1, *handler);
  }
500 501 502
}


503 504
void StoreICNexus::ConfigureMonomorphic(Handle<Map> receiver_map,
                                        Handle<Code> handler) {
505
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
506 507
  SetFeedback(*cell);
  SetFeedbackExtra(*handler);
508 509 510
}


511 512 513
void KeyedStoreICNexus::ConfigureMonomorphic(Handle<Name> name,
                                             Handle<Map> receiver_map,
                                             Handle<Code> handler) {
514
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
515
  if (name.is_null()) {
516 517
    SetFeedback(*cell);
    SetFeedbackExtra(*handler);
518
  } else {
519 520 521 522
    SetFeedback(*name);
    Handle<FixedArray> array = EnsureExtraArrayOfSize(2);
    array->set(0, *cell);
    array->set(1, *handler);
523 524 525 526
  }
}


527
void LoadICNexus::ConfigurePolymorphic(MapHandleList* maps,
528
                                       CodeHandleList* handlers) {
529
  Isolate* isolate = GetIsolate();
530
  int receiver_count = maps->length();
531 532
  Handle<FixedArray> array = EnsureArrayOfSize(receiver_count * 2);
  InstallHandlers(array, maps, handlers);
533
  SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(isolate),
534
                   SKIP_WRITE_BARRIER);
535 536 537 538
}


void KeyedLoadICNexus::ConfigurePolymorphic(Handle<Name> name,
539
                                            MapHandleList* maps,
540
                                            CodeHandleList* handlers) {
541
  int receiver_count = maps->length();
542 543
  DCHECK(receiver_count > 1);
  Handle<FixedArray> array;
544
  if (name.is_null()) {
545
    array = EnsureArrayOfSize(receiver_count * 2);
546
    SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(GetIsolate()),
547
                     SKIP_WRITE_BARRIER);
548
  } else {
549 550
    SetFeedback(*name);
    array = EnsureExtraArrayOfSize(receiver_count * 2);
551
  }
552 553

  InstallHandlers(array, maps, handlers);
554 555 556
}


557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
void StoreICNexus::ConfigurePolymorphic(MapHandleList* maps,
                                        CodeHandleList* handlers) {
  Isolate* isolate = GetIsolate();
  int receiver_count = maps->length();
  Handle<FixedArray> array = EnsureArrayOfSize(receiver_count * 2);
  InstallHandlers(array, maps, handlers);
  SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(isolate),
                   SKIP_WRITE_BARRIER);
}


void KeyedStoreICNexus::ConfigurePolymorphic(Handle<Name> name,
                                             MapHandleList* maps,
                                             CodeHandleList* handlers) {
  int receiver_count = maps->length();
  DCHECK(receiver_count > 1);
  Handle<FixedArray> array;
  if (name.is_null()) {
    array = EnsureArrayOfSize(receiver_count * 2);
    SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(GetIsolate()),
                     SKIP_WRITE_BARRIER);
  } else {
    SetFeedback(*name);
    array = EnsureExtraArrayOfSize(receiver_count * 2);
  }

  InstallHandlers(array, maps, handlers);
}


587
int FeedbackNexus::ExtractMaps(MapHandleList* maps) const {
588 589
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
590
  if (feedback->IsFixedArray() || feedback->IsString()) {
591
    int found = 0;
592 593 594
    if (feedback->IsString()) {
      feedback = GetFeedbackExtra();
    }
595 596 597
    FixedArray* array = FixedArray::cast(feedback);
    // The array should be of the form [<optional name>], then
    // [map, handler, map, handler, ... ]
598 599
    DCHECK(array->length() >= 2);
    for (int i = 0; i < array->length(); i += 2) {
600
      DCHECK(array->get(i)->IsWeakCell());
601 602 603 604 605 606
      WeakCell* cell = WeakCell::cast(array->get(i));
      if (!cell->cleared()) {
        Map* map = Map::cast(cell->value());
        maps->Add(handle(map, isolate));
        found++;
      }
607
    }
608
    return found;
609 610 611 612 613 614 615
  } else if (feedback->IsWeakCell()) {
    WeakCell* cell = WeakCell::cast(feedback);
    if (!cell->cleared()) {
      Map* map = Map::cast(cell->value());
      maps->Add(handle(map, isolate));
      return 1;
    }
616 617 618 619 620 621
  }

  return 0;
}


622
MaybeHandle<Code> FeedbackNexus::FindHandlerForMap(Handle<Map> map) const {
623
  Object* feedback = GetFeedback();
624 625 626 627
  if (feedback->IsFixedArray() || feedback->IsString()) {
    if (feedback->IsString()) {
      feedback = GetFeedbackExtra();
    }
628
    FixedArray* array = FixedArray::cast(feedback);
629
    for (int i = 0; i < array->length(); i += 2) {
630
      DCHECK(array->get(i)->IsWeakCell());
631 632 633 634 635 636 637 638
      WeakCell* cell = WeakCell::cast(array->get(i));
      if (!cell->cleared()) {
        Map* array_map = Map::cast(cell->value());
        if (array_map == *map) {
          Code* code = Code::cast(array->get(i + 1));
          DCHECK(code->kind() == Code::HANDLER);
          return handle(code);
        }
639 640
      }
    }
641 642 643 644 645 646 647 648 649 650
  } else if (feedback->IsWeakCell()) {
    WeakCell* cell = WeakCell::cast(feedback);
    if (!cell->cleared()) {
      Map* cell_map = Map::cast(cell->value());
      if (cell_map == *map) {
        Code* code = Code::cast(GetFeedbackExtra());
        DCHECK(code->kind() == Code::HANDLER);
        return handle(code);
      }
    }
651 652 653 654 655 656
  }

  return MaybeHandle<Code>();
}


657
bool FeedbackNexus::FindHandlers(CodeHandleList* code_list, int length) const {
658 659
  Object* feedback = GetFeedback();
  int count = 0;
660 661 662 663
  if (feedback->IsFixedArray() || feedback->IsString()) {
    if (feedback->IsString()) {
      feedback = GetFeedbackExtra();
    }
664
    FixedArray* array = FixedArray::cast(feedback);
665 666 667 668
    // The array should be of the form [map, handler, map, handler, ... ].
    // Be sure to skip handlers whose maps have been cleared.
    DCHECK(array->length() >= 2);
    for (int i = 0; i < array->length(); i += 2) {
669
      DCHECK(array->get(i)->IsWeakCell());
670 671 672 673 674 675 676
      WeakCell* cell = WeakCell::cast(array->get(i));
      if (!cell->cleared()) {
        Code* code = Code::cast(array->get(i + 1));
        DCHECK(code->kind() == Code::HANDLER);
        code_list->Add(handle(code));
        count++;
      }
677
    }
678 679 680 681 682 683 684 685
  } else if (feedback->IsWeakCell()) {
    WeakCell* cell = WeakCell::cast(feedback);
    if (!cell->cleared()) {
      Code* code = Code::cast(GetFeedbackExtra());
      DCHECK(code->kind() == Code::HANDLER);
      code_list->Add(handle(code));
      count++;
    }
686 687 688
  }
  return count == length;
}
689 690 691 692 693 694 695 696 697 698 699 700


void LoadICNexus::Clear(Code* host) { LoadIC::Clear(GetIsolate(), host, this); }


void KeyedLoadICNexus::Clear(Code* host) {
  KeyedLoadIC::Clear(GetIsolate(), host, this);
}


Name* KeyedLoadICNexus::FindFirstName() const {
  Object* feedback = GetFeedback();
701 702
  if (feedback->IsString()) {
    return Name::cast(feedback);
703 704 705
  }
  return NULL;
}
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724


Name* KeyedStoreICNexus::FindFirstName() const {
  Object* feedback = GetFeedback();
  if (feedback->IsString()) {
    return Name::cast(feedback);
  }
  return NULL;
}


void StoreICNexus::Clear(Code* host) {
  StoreIC::Clear(GetIsolate(), host, this);
}


void KeyedStoreICNexus::Clear(Code* host) {
  KeyedStoreIC::Clear(GetIsolate(), host, this);
}
725 726
}  // namespace internal
}  // namespace v8