type-feedback-vector.cc 21.2 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
// static
Handle<TypeFeedbackVector> TypeFeedbackVector::DummyVector(Isolate* isolate) {
  return Handle<TypeFeedbackVector>::cast(isolate->factory()->dummy_vector());
}


232 233 234 235 236 237 238 239 240 241 242 243 244
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);
}


245
Handle<FixedArray> FeedbackNexus::EnsureExtraArrayOfSize(int length) {
246
  Isolate* isolate = GetIsolate();
247 248 249 250 251 252 253 254 255 256 257 258 259 260
  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) {
261
  int receiver_count = maps->length();
262
  for (int current = 0; current < receiver_count; ++current) {
263
    Handle<Map> map = maps->at(current);
264
    Handle<WeakCell> cell = Map::WeakCellForMap(map);
265 266
    array->set(current * 2, *cell);
    array->set(current * 2 + 1, *handlers->at(current));
267 268 269 270
  }
}


271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
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);
}


296 297 298
InlineCacheState LoadICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
299

300
  if (feedback == *TypeFeedbackVector::UninitializedSentinel(isolate)) {
301
    return UNINITIALIZED;
302
  } else if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
303
    return MEGAMORPHIC;
304
  } else if (feedback == *TypeFeedbackVector::PremonomorphicSentinel(isolate)) {
305 306
    return PREMONOMORPHIC;
  } else if (feedback->IsFixedArray()) {
307 308
    // Determine state purely by our structure, don't check if the maps are
    // cleared.
309 310 311 312
    return POLYMORPHIC;
  } else if (feedback->IsWeakCell()) {
    // Don't check if the map is cleared.
    return MONOMORPHIC;
313 314 315 316 317 318 319 320 321
  }

  return UNINITIALIZED;
}


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

323
  if (feedback == *TypeFeedbackVector::UninitializedSentinel(isolate)) {
324
    return UNINITIALIZED;
325
  } else if (feedback == *TypeFeedbackVector::PremonomorphicSentinel(isolate)) {
326
    return PREMONOMORPHIC;
327
  } else if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
328
    return MEGAMORPHIC;
329
  } else if (feedback->IsFixedArray()) {
330 331
    // Determine state purely by our structure, don't check if the maps are
    // cleared.
332 333 334 335 336 337 338 339
    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;
340 341 342 343 344 345
  }

  return UNINITIALIZED;
}


346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
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;
}


396 397 398
InlineCacheState CallICNexus::StateFromFeedback() const {
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
399 400 401
  DCHECK(GetFeedbackExtra() ==
             *TypeFeedbackVector::UninitializedSentinel(isolate) ||
         GetFeedbackExtra()->IsSmi());
402

403
  if (feedback == *TypeFeedbackVector::MegamorphicSentinel(isolate)) {
404
    return GENERIC;
405
  } else if (feedback->IsAllocationSite() || feedback->IsWeakCell()) {
406
    return MONOMORPHIC;
407 408
  }

409
  CHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate));
410
  return UNINITIALIZED;
411 412 413
}


414 415 416 417 418 419 420 421 422 423
int CallICNexus::ExtractCallCount() {
  Object* call_count = GetFeedbackExtra();
  if (call_count->IsSmi()) {
    int value = Smi::cast(call_count)->value() / 2;
    return value;
  }
  return -1;
}


424 425 426
void CallICNexus::Clear(Code* host) { CallIC::Clear(GetIsolate(), host, this); }


427 428 429 430 431 432 433
void CallICNexus::ConfigureMonomorphicArray() {
  Object* feedback = GetFeedback();
  if (!feedback->IsAllocationSite()) {
    Handle<AllocationSite> new_site =
        GetIsolate()->factory()->NewAllocationSite();
    SetFeedback(*new_site);
  }
434
  SetFeedbackExtra(Smi::FromInt(kCallCountIncrement), SKIP_WRITE_BARRIER);
435 436 437 438
}


void CallICNexus::ConfigureMonomorphic(Handle<JSFunction> function) {
439 440
  Handle<WeakCell> new_cell = GetIsolate()->factory()->NewWeakCell(function);
  SetFeedback(*new_cell);
441
  SetFeedbackExtra(Smi::FromInt(kCallCountIncrement), SKIP_WRITE_BARRIER);
442 443 444
}


445 446 447 448 449
void LoadICNexus::ConfigureMonomorphic(Handle<Map> receiver_map,
                                       Handle<Code> handler) {
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
  SetFeedback(*cell);
  SetFeedbackExtra(*handler);
450 451 452
}


453 454 455 456 457 458 459 460 461 462 463 464 465
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);
  }
466 467 468
}


469 470
void StoreICNexus::ConfigureMonomorphic(Handle<Map> receiver_map,
                                        Handle<Code> handler) {
471
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
472 473
  SetFeedback(*cell);
  SetFeedbackExtra(*handler);
474 475 476
}


477 478 479
void KeyedStoreICNexus::ConfigureMonomorphic(Handle<Name> name,
                                             Handle<Map> receiver_map,
                                             Handle<Code> handler) {
480
  Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
481
  if (name.is_null()) {
482 483
    SetFeedback(*cell);
    SetFeedbackExtra(*handler);
484
  } else {
485 486 487 488
    SetFeedback(*name);
    Handle<FixedArray> array = EnsureExtraArrayOfSize(2);
    array->set(0, *cell);
    array->set(1, *handler);
489 490 491 492
  }
}


493
void LoadICNexus::ConfigurePolymorphic(MapHandleList* maps,
494
                                       CodeHandleList* handlers) {
495
  Isolate* isolate = GetIsolate();
496
  int receiver_count = maps->length();
497 498
  Handle<FixedArray> array = EnsureArrayOfSize(receiver_count * 2);
  InstallHandlers(array, maps, handlers);
499
  SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(isolate),
500
                   SKIP_WRITE_BARRIER);
501 502 503 504
}


void KeyedLoadICNexus::ConfigurePolymorphic(Handle<Name> name,
505
                                            MapHandleList* maps,
506
                                            CodeHandleList* handlers) {
507
  int receiver_count = maps->length();
508 509
  DCHECK(receiver_count > 1);
  Handle<FixedArray> array;
510
  if (name.is_null()) {
511
    array = EnsureArrayOfSize(receiver_count * 2);
512
    SetFeedbackExtra(*TypeFeedbackVector::UninitializedSentinel(GetIsolate()),
513
                     SKIP_WRITE_BARRIER);
514
  } else {
515 516
    SetFeedback(*name);
    array = EnsureExtraArrayOfSize(receiver_count * 2);
517
  }
518 519

  InstallHandlers(array, maps, handlers);
520 521 522
}


523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
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);
}


553
int FeedbackNexus::ExtractMaps(MapHandleList* maps) const {
554 555
  Isolate* isolate = GetIsolate();
  Object* feedback = GetFeedback();
556
  if (feedback->IsFixedArray() || feedback->IsString()) {
557
    int found = 0;
558 559 560
    if (feedback->IsString()) {
      feedback = GetFeedbackExtra();
    }
561 562 563
    FixedArray* array = FixedArray::cast(feedback);
    // The array should be of the form [<optional name>], then
    // [map, handler, map, handler, ... ]
564 565
    DCHECK(array->length() >= 2);
    for (int i = 0; i < array->length(); i += 2) {
566
      DCHECK(array->get(i)->IsWeakCell());
567 568 569 570 571 572
      WeakCell* cell = WeakCell::cast(array->get(i));
      if (!cell->cleared()) {
        Map* map = Map::cast(cell->value());
        maps->Add(handle(map, isolate));
        found++;
      }
573
    }
574
    return found;
575 576 577 578 579 580 581
  } 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;
    }
582 583 584 585 586 587
  }

  return 0;
}


588
MaybeHandle<Code> FeedbackNexus::FindHandlerForMap(Handle<Map> map) const {
589
  Object* feedback = GetFeedback();
590 591 592 593
  if (feedback->IsFixedArray() || feedback->IsString()) {
    if (feedback->IsString()) {
      feedback = GetFeedbackExtra();
    }
594
    FixedArray* array = FixedArray::cast(feedback);
595
    for (int i = 0; i < array->length(); i += 2) {
596
      DCHECK(array->get(i)->IsWeakCell());
597 598 599 600 601 602 603 604
      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);
        }
605 606
      }
    }
607 608 609 610 611 612 613 614 615 616
  } 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);
      }
    }
617 618 619 620 621 622
  }

  return MaybeHandle<Code>();
}


623
bool FeedbackNexus::FindHandlers(CodeHandleList* code_list, int length) const {
624 625
  Object* feedback = GetFeedback();
  int count = 0;
626 627 628 629
  if (feedback->IsFixedArray() || feedback->IsString()) {
    if (feedback->IsString()) {
      feedback = GetFeedbackExtra();
    }
630
    FixedArray* array = FixedArray::cast(feedback);
631 632 633 634
    // 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) {
635
      DCHECK(array->get(i)->IsWeakCell());
636 637 638 639 640 641 642
      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++;
      }
643
    }
644 645 646 647 648 649 650 651
  } 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++;
    }
652 653 654
  }
  return count == length;
}
655 656 657 658 659 660 661 662 663 664 665 666


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();
667 668
  if (feedback->IsString()) {
    return Name::cast(feedback);
669 670 671
  }
  return NULL;
}
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690


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);
}
691 692
}  // namespace internal
}  // namespace v8