1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/feedback-vector.h"
6 #include "src/code-stubs.h"
7 #include "src/feedback-vector-inl.h"
8 #include "src/ic/ic-inl.h"
9 #include "src/objects.h"
10 #include "src/objects/hash-table-inl.h"
11 #include "src/objects/object-macros.h"
12 
13 namespace v8 {
14 namespace internal {
15 
AddSlot(FeedbackSlotKind kind)16 FeedbackSlot FeedbackVectorSpec::AddSlot(FeedbackSlotKind kind) {
17   int slot = slots();
18   int entries_per_slot = FeedbackMetadata::GetSlotSize(kind);
19   append(kind);
20   for (int i = 1; i < entries_per_slot; i++) {
21     append(FeedbackSlotKind::kInvalid);
22   }
23   return FeedbackSlot(slot);
24 }
25 
AddTypeProfileSlot()26 FeedbackSlot FeedbackVectorSpec::AddTypeProfileSlot() {
27   FeedbackSlot slot = AddSlot(FeedbackSlotKind::kTypeProfile);
28   CHECK_EQ(FeedbackVectorSpec::kTypeProfileSlotIndex,
29            FeedbackVector::GetIndex(slot));
30   return slot;
31 }
32 
HasTypeProfileSlot() const33 bool FeedbackVectorSpec::HasTypeProfileSlot() const {
34   FeedbackSlot slot =
35       FeedbackVector::ToSlot(FeedbackVectorSpec::kTypeProfileSlotIndex);
36   if (slots() <= slot.ToInt()) {
37     return false;
38   }
39   return GetKind(slot) == FeedbackSlotKind::kTypeProfile;
40 }
41 
IsPropertyNameFeedback(Object * feedback)42 static bool IsPropertyNameFeedback(Object* feedback) {
43   if (feedback->IsString()) return true;
44   if (!feedback->IsSymbol()) return false;
45   Symbol* symbol = Symbol::cast(feedback);
46   Heap* heap = symbol->GetHeap();
47   return symbol != heap->uninitialized_symbol() &&
48          symbol != heap->premonomorphic_symbol() &&
49          symbol != heap->megamorphic_symbol();
50 }
51 
operator <<(std::ostream & os,FeedbackSlotKind kind)52 std::ostream& operator<<(std::ostream& os, FeedbackSlotKind kind) {
53   return os << FeedbackMetadata::Kind2String(kind);
54 }
55 
GetKind(FeedbackSlot slot) const56 FeedbackSlotKind FeedbackMetadata::GetKind(FeedbackSlot slot) const {
57   int index = VectorICComputer::index(0, slot.ToInt());
58   int data = get(index);
59   return VectorICComputer::decode(data, slot.ToInt());
60 }
61 
SetKind(FeedbackSlot slot,FeedbackSlotKind kind)62 void FeedbackMetadata::SetKind(FeedbackSlot slot, FeedbackSlotKind kind) {
63   int index = VectorICComputer::index(0, slot.ToInt());
64   int data = get(index);
65   int new_data = VectorICComputer::encode(data, slot.ToInt(), kind);
66   set(index, new_data);
67 }
68 
69 // static
New(Isolate * isolate,const FeedbackVectorSpec * spec)70 Handle<FeedbackMetadata> FeedbackMetadata::New(Isolate* isolate,
71                                                const FeedbackVectorSpec* spec) {
72   Factory* factory = isolate->factory();
73 
74   const int slot_count = spec == nullptr ? 0 : spec->slots();
75   if (slot_count == 0) {
76     return factory->empty_feedback_metadata();
77   }
78 #ifdef DEBUG
79   for (int i = 0; i < slot_count;) {
80     DCHECK(spec);
81     FeedbackSlotKind kind = spec->GetKind(FeedbackSlot(i));
82     int entry_size = FeedbackMetadata::GetSlotSize(kind);
83     for (int j = 1; j < entry_size; j++) {
84       FeedbackSlotKind kind = spec->GetKind(FeedbackSlot(i + j));
85       DCHECK_EQ(FeedbackSlotKind::kInvalid, kind);
86     }
87     i += entry_size;
88   }
89 #endif
90 
91   Handle<FeedbackMetadata> metadata = factory->NewFeedbackMetadata(slot_count);
92 
93   // Initialize the slots. The raw data section has already been pre-zeroed in
94   // NewFeedbackMetadata.
95   for (int i = 0; i < slot_count; i++) {
96     DCHECK(spec);
97     FeedbackSlot slot(i);
98     FeedbackSlotKind kind = spec->GetKind(slot);
99     metadata->SetKind(slot, kind);
100   }
101 
102   return metadata;
103 }
104 
SpecDiffersFrom(const FeedbackVectorSpec * other_spec) const105 bool FeedbackMetadata::SpecDiffersFrom(
106     const FeedbackVectorSpec* other_spec) const {
107   if (other_spec->slots() != slot_count()) {
108     return true;
109   }
110 
111   int slots = slot_count();
112   for (int i = 0; i < slots;) {
113     FeedbackSlot slot(i);
114     FeedbackSlotKind kind = GetKind(slot);
115     int entry_size = FeedbackMetadata::GetSlotSize(kind);
116 
117     if (kind != other_spec->GetKind(slot)) {
118       return true;
119     }
120     i += entry_size;
121   }
122   return false;
123 }
124 
Kind2String(FeedbackSlotKind kind)125 const char* FeedbackMetadata::Kind2String(FeedbackSlotKind kind) {
126   switch (kind) {
127     case FeedbackSlotKind::kInvalid:
128       return "Invalid";
129     case FeedbackSlotKind::kCall:
130       return "Call";
131     case FeedbackSlotKind::kLoadProperty:
132       return "LoadProperty";
133     case FeedbackSlotKind::kLoadGlobalInsideTypeof:
134       return "LoadGlobalInsideTypeof";
135     case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
136       return "LoadGlobalNotInsideTypeof";
137     case FeedbackSlotKind::kLoadKeyed:
138       return "LoadKeyed";
139     case FeedbackSlotKind::kStoreNamedSloppy:
140       return "StoreNamedSloppy";
141     case FeedbackSlotKind::kStoreNamedStrict:
142       return "StoreNamedStrict";
143     case FeedbackSlotKind::kStoreOwnNamed:
144       return "StoreOwnNamed";
145     case FeedbackSlotKind::kStoreGlobalSloppy:
146       return "StoreGlobalSloppy";
147     case FeedbackSlotKind::kStoreGlobalStrict:
148       return "StoreGlobalStrict";
149     case FeedbackSlotKind::kStoreKeyedSloppy:
150       return "StoreKeyedSloppy";
151     case FeedbackSlotKind::kStoreKeyedStrict:
152       return "StoreKeyedStrict";
153     case FeedbackSlotKind::kStoreInArrayLiteral:
154       return "StoreInArrayLiteral";
155     case FeedbackSlotKind::kBinaryOp:
156       return "BinaryOp";
157     case FeedbackSlotKind::kCompareOp:
158       return "CompareOp";
159     case FeedbackSlotKind::kStoreDataPropertyInLiteral:
160       return "StoreDataPropertyInLiteral";
161     case FeedbackSlotKind::kCreateClosure:
162       return "kCreateClosure";
163     case FeedbackSlotKind::kLiteral:
164       return "Literal";
165     case FeedbackSlotKind::kTypeProfile:
166       return "TypeProfile";
167     case FeedbackSlotKind::kForIn:
168       return "ForIn";
169     case FeedbackSlotKind::kInstanceOf:
170       return "InstanceOf";
171     case FeedbackSlotKind::kKindsNumber:
172       break;
173   }
174   UNREACHABLE();
175 }
176 
HasTypeProfileSlot() const177 bool FeedbackMetadata::HasTypeProfileSlot() const {
178   FeedbackSlot slot =
179       FeedbackVector::ToSlot(FeedbackVectorSpec::kTypeProfileSlotIndex);
180   return slot.ToInt() < slot_count() &&
181          GetKind(slot) == FeedbackSlotKind::kTypeProfile;
182 }
183 
GetKind(FeedbackSlot slot) const184 FeedbackSlotKind FeedbackVector::GetKind(FeedbackSlot slot) const {
185   DCHECK(!is_empty());
186   return metadata()->GetKind(slot);
187 }
188 
GetTypeProfileSlot() const189 FeedbackSlot FeedbackVector::GetTypeProfileSlot() const {
190   DCHECK(metadata()->HasTypeProfileSlot());
191   FeedbackSlot slot =
192       FeedbackVector::ToSlot(FeedbackVectorSpec::kTypeProfileSlotIndex);
193   DCHECK_EQ(FeedbackSlotKind::kTypeProfile, GetKind(slot));
194   return slot;
195 }
196 
197 // static
New(Isolate * isolate,Handle<SharedFunctionInfo> shared)198 Handle<FeedbackVector> FeedbackVector::New(Isolate* isolate,
199                                            Handle<SharedFunctionInfo> shared) {
200   Factory* factory = isolate->factory();
201 
202   const int slot_count = shared->feedback_metadata()->slot_count();
203 
204   Handle<FeedbackVector> vector = factory->NewFeedbackVector(shared, TENURED);
205 
206   DCHECK_EQ(vector->length(), slot_count);
207 
208   DCHECK_EQ(vector->shared_function_info(), *shared);
209   DCHECK_EQ(
210       vector->optimized_code_weak_or_smi(),
211       MaybeObject::FromSmi(Smi::FromEnum(
212           FLAG_log_function_events ? OptimizationMarker::kLogFirstExecution
213                                    : OptimizationMarker::kNone)));
214   DCHECK_EQ(vector->invocation_count(), 0);
215   DCHECK_EQ(vector->profiler_ticks(), 0);
216   DCHECK_EQ(vector->deopt_count(), 0);
217 
218   // Ensure we can skip the write barrier
219   Handle<Object> uninitialized_sentinel = UninitializedSentinel(isolate);
220   DCHECK_EQ(isolate->heap()->uninitialized_symbol(), *uninitialized_sentinel);
221   Handle<Oddball> undefined_value = factory->undefined_value();
222   for (int i = 0; i < slot_count;) {
223     FeedbackSlot slot(i);
224     FeedbackSlotKind kind = shared->feedback_metadata()->GetKind(slot);
225     int index = FeedbackVector::GetIndex(slot);
226     int entry_size = FeedbackMetadata::GetSlotSize(kind);
227 
228     Object* extra_value = *uninitialized_sentinel;
229     switch (kind) {
230       case FeedbackSlotKind::kLoadGlobalInsideTypeof:
231       case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
232       case FeedbackSlotKind::kStoreGlobalSloppy:
233       case FeedbackSlotKind::kStoreGlobalStrict:
234         vector->set(index, isolate->heap()->empty_weak_cell(),
235                     SKIP_WRITE_BARRIER);
236         break;
237       case FeedbackSlotKind::kForIn:
238       case FeedbackSlotKind::kCompareOp:
239       case FeedbackSlotKind::kBinaryOp:
240         vector->set(index, Smi::kZero, SKIP_WRITE_BARRIER);
241         break;
242       case FeedbackSlotKind::kCreateClosure: {
243         Handle<FeedbackCell> cell = factory->NewNoClosuresCell(undefined_value);
244         vector->set(index, *cell);
245         break;
246       }
247       case FeedbackSlotKind::kLiteral:
248         vector->set(index, Smi::kZero, SKIP_WRITE_BARRIER);
249         break;
250       case FeedbackSlotKind::kCall:
251         vector->set(index, *uninitialized_sentinel, SKIP_WRITE_BARRIER);
252         extra_value = Smi::kZero;
253         break;
254       case FeedbackSlotKind::kLoadProperty:
255       case FeedbackSlotKind::kLoadKeyed:
256       case FeedbackSlotKind::kStoreNamedSloppy:
257       case FeedbackSlotKind::kStoreNamedStrict:
258       case FeedbackSlotKind::kStoreOwnNamed:
259       case FeedbackSlotKind::kStoreKeyedSloppy:
260       case FeedbackSlotKind::kStoreKeyedStrict:
261       case FeedbackSlotKind::kStoreInArrayLiteral:
262       case FeedbackSlotKind::kStoreDataPropertyInLiteral:
263       case FeedbackSlotKind::kTypeProfile:
264       case FeedbackSlotKind::kInstanceOf:
265         vector->set(index, *uninitialized_sentinel, SKIP_WRITE_BARRIER);
266         break;
267 
268       case FeedbackSlotKind::kInvalid:
269       case FeedbackSlotKind::kKindsNumber:
270         UNREACHABLE();
271         break;
272     }
273     for (int j = 1; j < entry_size; j++) {
274       vector->set(index + j, extra_value, SKIP_WRITE_BARRIER);
275     }
276     i += entry_size;
277   }
278 
279   Handle<FeedbackVector> result = Handle<FeedbackVector>::cast(vector);
280   if (!isolate->is_best_effort_code_coverage() ||
281       isolate->is_collecting_type_profile()) {
282     AddToVectorsForProfilingTools(isolate, result);
283   }
284   return result;
285 }
286 
287 // static
AddToVectorsForProfilingTools(Isolate * isolate,Handle<FeedbackVector> vector)288 void FeedbackVector::AddToVectorsForProfilingTools(
289     Isolate* isolate, Handle<FeedbackVector> vector) {
290   DCHECK(!isolate->is_best_effort_code_coverage() ||
291          isolate->is_collecting_type_profile());
292   if (!vector->shared_function_info()->IsSubjectToDebugging()) return;
293   Handle<ArrayList> list = Handle<ArrayList>::cast(
294       isolate->factory()->feedback_vectors_for_profiling_tools());
295   list = ArrayList::Add(list, vector);
296   isolate->SetFeedbackVectorsForProfilingTools(*list);
297 }
298 
299 // static
SetOptimizedCode(Handle<FeedbackVector> vector,Handle<Code> code)300 void FeedbackVector::SetOptimizedCode(Handle<FeedbackVector> vector,
301                                       Handle<Code> code) {
302   DCHECK_EQ(code->kind(), Code::OPTIMIZED_FUNCTION);
303   vector->set_optimized_code_weak_or_smi(HeapObjectReference::Weak(*code));
304 }
305 
ClearOptimizedCode()306 void FeedbackVector::ClearOptimizedCode() {
307   DCHECK(has_optimized_code());
308   SetOptimizationMarker(OptimizationMarker::kNone);
309 }
310 
ClearOptimizationMarker()311 void FeedbackVector::ClearOptimizationMarker() {
312   DCHECK(!has_optimized_code());
313   SetOptimizationMarker(OptimizationMarker::kNone);
314 }
315 
SetOptimizationMarker(OptimizationMarker marker)316 void FeedbackVector::SetOptimizationMarker(OptimizationMarker marker) {
317   set_optimized_code_weak_or_smi(MaybeObject::FromSmi(Smi::FromEnum(marker)));
318 }
319 
EvictOptimizedCodeMarkedForDeoptimization(SharedFunctionInfo * shared,const char * reason)320 void FeedbackVector::EvictOptimizedCodeMarkedForDeoptimization(
321     SharedFunctionInfo* shared, const char* reason) {
322   MaybeObject* slot = optimized_code_weak_or_smi();
323   if (slot->IsSmi()) {
324     return;
325   }
326 
327   if (slot->IsClearedWeakHeapObject()) {
328     ClearOptimizationMarker();
329     return;
330   }
331 
332   Code* code = Code::cast(slot->GetHeapObject());
333   if (code->marked_for_deoptimization()) {
334     if (FLAG_trace_deopt) {
335       PrintF("[evicting optimizing code marked for deoptimization (%s) for ",
336              reason);
337       shared->ShortPrint();
338       PrintF("]\n");
339     }
340     if (!code->deopt_already_counted()) {
341       increment_deopt_count();
342       code->set_deopt_already_counted(true);
343     }
344     ClearOptimizedCode();
345   }
346 }
347 
ClearSlots(Isolate * isolate)348 bool FeedbackVector::ClearSlots(Isolate* isolate) {
349   Object* uninitialized_sentinel =
350       FeedbackVector::RawUninitializedSentinel(isolate);
351 
352   bool feedback_updated = false;
353   FeedbackMetadataIterator iter(metadata());
354   while (iter.HasNext()) {
355     FeedbackSlot slot = iter.Next();
356 
357     Object* obj = Get(slot)->ToObject();
358     if (obj != uninitialized_sentinel) {
359       FeedbackNexus nexus(this, slot);
360       feedback_updated |= nexus.Clear();
361     }
362   }
363   return feedback_updated;
364 }
365 
AssertNoLegacyTypes(Object * object)366 void FeedbackVector::AssertNoLegacyTypes(Object* object) {
367   // Instead of FixedArray, the Feedback and the Extra should contain
368   // WeakFixedArrays. The only allowed FixedArray subtype is HashTable.
369   DCHECK_IMPLIES(object->IsFixedArray(), object->IsHashTable());
370 }
371 
EnsureArrayOfSize(int length)372 Handle<WeakFixedArray> FeedbackNexus::EnsureArrayOfSize(int length) {
373   Isolate* isolate = GetIsolate();
374   Handle<Object> feedback = handle(GetFeedback(), isolate);
375   if (!feedback->IsWeakFixedArray() ||
376       WeakFixedArray::cast(*feedback)->length() != length) {
377     Handle<WeakFixedArray> array =
378         isolate->factory()->NewWeakFixedArray(length);
379     SetFeedback(*array);
380     return array;
381   }
382   return Handle<WeakFixedArray>::cast(feedback);
383 }
384 
EnsureExtraArrayOfSize(int length)385 Handle<WeakFixedArray> FeedbackNexus::EnsureExtraArrayOfSize(int length) {
386   Isolate* isolate = GetIsolate();
387   HeapObject* heap_object;
388   if (GetFeedbackExtra()->ToStrongHeapObject(&heap_object) &&
389       heap_object->IsWeakFixedArray() &&
390       WeakFixedArray::cast(heap_object)->length() == length) {
391     return handle(WeakFixedArray::cast(heap_object));
392   }
393   Handle<WeakFixedArray> array = isolate->factory()->NewWeakFixedArray(length);
394   SetFeedbackExtra(*array);
395   return array;
396 }
397 
ConfigureUninitialized()398 void FeedbackNexus::ConfigureUninitialized() {
399   Isolate* isolate = GetIsolate();
400   switch (kind()) {
401     case FeedbackSlotKind::kStoreGlobalSloppy:
402     case FeedbackSlotKind::kStoreGlobalStrict:
403     case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
404     case FeedbackSlotKind::kLoadGlobalInsideTypeof: {
405       SetFeedback(isolate->heap()->empty_weak_cell(), SKIP_WRITE_BARRIER);
406       SetFeedbackExtra(*FeedbackVector::UninitializedSentinel(isolate),
407                        SKIP_WRITE_BARRIER);
408       break;
409     }
410     case FeedbackSlotKind::kCall: {
411       SetFeedback(*FeedbackVector::UninitializedSentinel(isolate),
412                   SKIP_WRITE_BARRIER);
413       SetFeedbackExtra(Smi::kZero, SKIP_WRITE_BARRIER);
414       break;
415     }
416     case FeedbackSlotKind::kInstanceOf: {
417       SetFeedback(*FeedbackVector::UninitializedSentinel(isolate),
418                   SKIP_WRITE_BARRIER);
419       break;
420     }
421     case FeedbackSlotKind::kStoreDataPropertyInLiteral: {
422       SetFeedback(*FeedbackVector::UninitializedSentinel(isolate),
423                   SKIP_WRITE_BARRIER);
424       SetFeedbackExtra(*FeedbackVector::UninitializedSentinel(isolate),
425                        SKIP_WRITE_BARRIER);
426       break;
427     }
428     default:
429       UNREACHABLE();
430   }
431 }
432 
Clear()433 bool FeedbackNexus::Clear() {
434   bool feedback_updated = false;
435 
436   switch (kind()) {
437     case FeedbackSlotKind::kCreateClosure:
438     case FeedbackSlotKind::kTypeProfile:
439       // We don't clear these kinds ever.
440       break;
441 
442     case FeedbackSlotKind::kCompareOp:
443     case FeedbackSlotKind::kForIn:
444     case FeedbackSlotKind::kBinaryOp:
445       // We don't clear these, either.
446       break;
447 
448     case FeedbackSlotKind::kLiteral:
449       SetFeedback(Smi::kZero, SKIP_WRITE_BARRIER);
450       feedback_updated = true;
451       break;
452 
453     case FeedbackSlotKind::kStoreNamedSloppy:
454     case FeedbackSlotKind::kStoreNamedStrict:
455     case FeedbackSlotKind::kStoreKeyedSloppy:
456     case FeedbackSlotKind::kStoreKeyedStrict:
457     case FeedbackSlotKind::kStoreInArrayLiteral:
458     case FeedbackSlotKind::kStoreOwnNamed:
459     case FeedbackSlotKind::kLoadProperty:
460     case FeedbackSlotKind::kLoadKeyed:
461       if (!IsCleared()) {
462         ConfigurePremonomorphic();
463         feedback_updated = true;
464       }
465       break;
466 
467     case FeedbackSlotKind::kStoreGlobalSloppy:
468     case FeedbackSlotKind::kStoreGlobalStrict:
469     case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
470     case FeedbackSlotKind::kLoadGlobalInsideTypeof:
471     case FeedbackSlotKind::kCall:
472     case FeedbackSlotKind::kInstanceOf:
473     case FeedbackSlotKind::kStoreDataPropertyInLiteral:
474       if (!IsCleared()) {
475         ConfigureUninitialized();
476         feedback_updated = true;
477       }
478       break;
479 
480     case FeedbackSlotKind::kInvalid:
481     case FeedbackSlotKind::kKindsNumber:
482       UNREACHABLE();
483       break;
484   }
485   return feedback_updated;
486 }
487 
ConfigurePremonomorphic()488 void FeedbackNexus::ConfigurePremonomorphic() {
489   SetFeedback(*FeedbackVector::PremonomorphicSentinel(GetIsolate()),
490               SKIP_WRITE_BARRIER);
491   SetFeedbackExtra(*FeedbackVector::UninitializedSentinel(GetIsolate()),
492                    SKIP_WRITE_BARRIER);
493 }
494 
ConfigureMegamorphic(IcCheckType property_type)495 bool FeedbackNexus::ConfigureMegamorphic(IcCheckType property_type) {
496   DisallowHeapAllocation no_gc;
497   Isolate* isolate = GetIsolate();
498   bool changed = false;
499   Symbol* sentinel = *FeedbackVector::MegamorphicSentinel(isolate);
500   if (GetFeedback() != sentinel) {
501     SetFeedback(sentinel, SKIP_WRITE_BARRIER);
502     changed = true;
503   }
504 
505   Smi* extra = Smi::FromInt(static_cast<int>(property_type));
506   if (changed || GetFeedbackExtra() != MaybeObject::FromSmi(extra)) {
507     SetFeedbackExtra(extra, SKIP_WRITE_BARRIER);
508     changed = true;
509   }
510   return changed;
511 }
512 
StateFromFeedback() const513 InlineCacheState FeedbackNexus::StateFromFeedback() const {
514   Isolate* isolate = GetIsolate();
515   Object* feedback = GetFeedback();
516 
517   switch (kind()) {
518     case FeedbackSlotKind::kCreateClosure:
519     case FeedbackSlotKind::kLiteral:
520       // CreateClosure and literal slots don't have a notion of state.
521       UNREACHABLE();
522       break;
523 
524     case FeedbackSlotKind::kStoreGlobalSloppy:
525     case FeedbackSlotKind::kStoreGlobalStrict:
526     case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
527     case FeedbackSlotKind::kLoadGlobalInsideTypeof: {
528       if (feedback->IsSmi()) return MONOMORPHIC;
529 
530       MaybeObject* extra = GetFeedbackExtra();
531       if (!WeakCell::cast(feedback)->cleared() ||
532           extra != MaybeObject::FromObject(
533                        *FeedbackVector::UninitializedSentinel(isolate))) {
534         return MONOMORPHIC;
535       }
536       return UNINITIALIZED;
537     }
538 
539     case FeedbackSlotKind::kStoreNamedSloppy:
540     case FeedbackSlotKind::kStoreNamedStrict:
541     case FeedbackSlotKind::kStoreKeyedSloppy:
542     case FeedbackSlotKind::kStoreKeyedStrict:
543     case FeedbackSlotKind::kStoreInArrayLiteral:
544     case FeedbackSlotKind::kStoreOwnNamed:
545     case FeedbackSlotKind::kLoadProperty:
546     case FeedbackSlotKind::kLoadKeyed: {
547       if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
548         return UNINITIALIZED;
549       }
550       if (feedback == *FeedbackVector::MegamorphicSentinel(isolate)) {
551         return MEGAMORPHIC;
552       }
553       if (feedback == *FeedbackVector::PremonomorphicSentinel(isolate)) {
554         return PREMONOMORPHIC;
555       }
556       if (feedback->IsWeakFixedArray()) {
557         // Determine state purely by our structure, don't check if the maps are
558         // cleared.
559         return POLYMORPHIC;
560       }
561       if (feedback->IsWeakCell()) {
562         // Don't check if the map is cleared.
563         return MONOMORPHIC;
564       }
565       if (feedback->IsName()) {
566         DCHECK(IsKeyedLoadICKind(kind()) || IsKeyedStoreICKind(kind()));
567         Object* extra = GetFeedbackExtra()->ToStrongHeapObject();
568         WeakFixedArray* extra_array = WeakFixedArray::cast(extra);
569         return extra_array->length() > 2 ? POLYMORPHIC : MONOMORPHIC;
570       }
571       UNREACHABLE();
572     }
573     case FeedbackSlotKind::kCall: {
574       if (feedback == *FeedbackVector::MegamorphicSentinel(isolate)) {
575         return GENERIC;
576       } else if (feedback->IsAllocationSite() || feedback->IsWeakCell()) {
577         return MONOMORPHIC;
578       }
579 
580       CHECK(feedback == *FeedbackVector::UninitializedSentinel(isolate));
581       return UNINITIALIZED;
582     }
583     case FeedbackSlotKind::kBinaryOp: {
584       BinaryOperationHint hint = GetBinaryOperationFeedback();
585       if (hint == BinaryOperationHint::kNone) {
586         return UNINITIALIZED;
587       } else if (hint == BinaryOperationHint::kAny) {
588         return GENERIC;
589       }
590 
591       return MONOMORPHIC;
592     }
593     case FeedbackSlotKind::kCompareOp: {
594       CompareOperationHint hint = GetCompareOperationFeedback();
595       if (hint == CompareOperationHint::kNone) {
596         return UNINITIALIZED;
597       } else if (hint == CompareOperationHint::kAny) {
598         return GENERIC;
599       }
600 
601       return MONOMORPHIC;
602     }
603     case FeedbackSlotKind::kForIn: {
604       ForInHint hint = GetForInFeedback();
605       if (hint == ForInHint::kNone) {
606         return UNINITIALIZED;
607       } else if (hint == ForInHint::kAny) {
608         return GENERIC;
609       }
610       return MONOMORPHIC;
611     }
612     case FeedbackSlotKind::kInstanceOf: {
613       if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
614         return UNINITIALIZED;
615       } else if (feedback == *FeedbackVector::MegamorphicSentinel(isolate)) {
616         return MEGAMORPHIC;
617       }
618       return MONOMORPHIC;
619     }
620     case FeedbackSlotKind::kStoreDataPropertyInLiteral: {
621       if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
622         return UNINITIALIZED;
623       } else if (feedback->IsWeakCell()) {
624         // Don't check if the map is cleared.
625         return MONOMORPHIC;
626       }
627 
628       return MEGAMORPHIC;
629     }
630     case FeedbackSlotKind::kTypeProfile: {
631       if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
632         return UNINITIALIZED;
633       }
634       return MONOMORPHIC;
635     }
636 
637     case FeedbackSlotKind::kInvalid:
638     case FeedbackSlotKind::kKindsNumber:
639       UNREACHABLE();
640       break;
641   }
642   return UNINITIALIZED;
643 }
644 
ConfigurePropertyCellMode(Handle<PropertyCell> cell)645 void FeedbackNexus::ConfigurePropertyCellMode(Handle<PropertyCell> cell) {
646   DCHECK(IsGlobalICKind(kind()));
647   Isolate* isolate = GetIsolate();
648   SetFeedback(*isolate->factory()->NewWeakCell(cell));
649   SetFeedbackExtra(*FeedbackVector::UninitializedSentinel(isolate),
650                    SKIP_WRITE_BARRIER);
651 }
652 
ConfigureLexicalVarMode(int script_context_index,int context_slot_index)653 bool FeedbackNexus::ConfigureLexicalVarMode(int script_context_index,
654                                             int context_slot_index) {
655   DCHECK(IsGlobalICKind(kind()));
656   DCHECK_LE(0, script_context_index);
657   DCHECK_LE(0, context_slot_index);
658   if (!ContextIndexBits::is_valid(script_context_index) ||
659       !SlotIndexBits::is_valid(context_slot_index)) {
660     return false;
661   }
662   int config = ContextIndexBits::encode(script_context_index) |
663                SlotIndexBits::encode(context_slot_index);
664 
665   SetFeedback(Smi::FromInt(config));
666   Isolate* isolate = GetIsolate();
667   SetFeedbackExtra(*FeedbackVector::UninitializedSentinel(isolate),
668                    SKIP_WRITE_BARRIER);
669   return true;
670 }
671 
ConfigureHandlerMode(const MaybeObjectHandle & handler)672 void FeedbackNexus::ConfigureHandlerMode(const MaybeObjectHandle& handler) {
673   DCHECK(IsGlobalICKind(kind()));
674   DCHECK(IC::IsHandler(*handler));
675   SetFeedback(GetIsolate()->heap()->empty_weak_cell());
676   SetFeedbackExtra(*handler);
677 }
678 
GetCallCount()679 int FeedbackNexus::GetCallCount() {
680   DCHECK(IsCallICKind(kind()));
681 
682   Object* call_count = GetFeedbackExtra()->ToObject();
683   CHECK(call_count->IsSmi());
684   uint32_t value = static_cast<uint32_t>(Smi::ToInt(call_count));
685   return CallCountField::decode(value);
686 }
687 
SetSpeculationMode(SpeculationMode mode)688 void FeedbackNexus::SetSpeculationMode(SpeculationMode mode) {
689   DCHECK(IsCallICKind(kind()));
690 
691   Object* call_count = GetFeedbackExtra()->ToObject();
692   CHECK(call_count->IsSmi());
693   uint32_t count = static_cast<uint32_t>(Smi::ToInt(call_count));
694   uint32_t value = CallCountField::encode(CallCountField::decode(count));
695   int result = static_cast<int>(value | SpeculationModeField::encode(mode));
696   SetFeedbackExtra(Smi::FromInt(result), SKIP_WRITE_BARRIER);
697 }
698 
GetSpeculationMode()699 SpeculationMode FeedbackNexus::GetSpeculationMode() {
700   DCHECK(IsCallICKind(kind()));
701 
702   Object* call_count = GetFeedbackExtra()->ToObject();
703   CHECK(call_count->IsSmi());
704   uint32_t value = static_cast<uint32_t>(Smi::ToInt(call_count));
705   return SpeculationModeField::decode(value);
706 }
707 
ComputeCallFrequency()708 float FeedbackNexus::ComputeCallFrequency() {
709   DCHECK(IsCallICKind(kind()));
710 
711   double const invocation_count = vector()->invocation_count();
712   double const call_count = GetCallCount();
713   if (invocation_count == 0) {
714     // Prevent division by 0.
715     return 0.0f;
716   }
717   return static_cast<float>(call_count / invocation_count);
718 }
719 
ConfigureMonomorphic(Handle<Name> name,Handle<Map> receiver_map,const MaybeObjectHandle & handler)720 void FeedbackNexus::ConfigureMonomorphic(Handle<Name> name,
721                                          Handle<Map> receiver_map,
722                                          const MaybeObjectHandle& handler) {
723   DCHECK(handler.is_null() || IC::IsHandler(*handler));
724   Handle<WeakCell> cell = Map::WeakCellForMap(receiver_map);
725   if (kind() == FeedbackSlotKind::kStoreDataPropertyInLiteral) {
726     SetFeedback(*cell);
727     SetFeedbackExtra(*name);
728   } else {
729     if (name.is_null()) {
730       SetFeedback(*cell);
731       SetFeedbackExtra(*handler);
732     } else {
733       Handle<WeakFixedArray> array = EnsureExtraArrayOfSize(2);
734       SetFeedback(*name);
735       array->Set(0, HeapObjectReference::Strong(*cell));
736       array->Set(1, *handler);
737     }
738   }
739 }
740 
ConfigurePolymorphic(Handle<Name> name,MapHandles const & maps,MaybeObjectHandles * handlers)741 void FeedbackNexus::ConfigurePolymorphic(Handle<Name> name,
742                                          MapHandles const& maps,
743                                          MaybeObjectHandles* handlers) {
744   DCHECK_EQ(handlers->size(), maps.size());
745   int receiver_count = static_cast<int>(maps.size());
746   DCHECK_GT(receiver_count, 1);
747   Handle<WeakFixedArray> array;
748   if (name.is_null()) {
749     array = EnsureArrayOfSize(receiver_count * 2);
750     SetFeedbackExtra(*FeedbackVector::UninitializedSentinel(GetIsolate()),
751                      SKIP_WRITE_BARRIER);
752   } else {
753     array = EnsureExtraArrayOfSize(receiver_count * 2);
754     SetFeedback(*name);
755   }
756 
757   for (int current = 0; current < receiver_count; ++current) {
758     Handle<Map> map = maps[current];
759     Handle<WeakCell> cell = Map::WeakCellForMap(map);
760     array->Set(current * 2, HeapObjectReference::Strong(*cell));
761     DCHECK(IC::IsHandler(*handlers->at(current)));
762     array->Set(current * 2 + 1, *handlers->at(current));
763   }
764 }
765 
ExtractMaps(MapHandles * maps) const766 int FeedbackNexus::ExtractMaps(MapHandles* maps) const {
767   DCHECK(IsLoadICKind(kind()) || IsStoreICKind(kind()) ||
768          IsKeyedLoadICKind(kind()) || IsKeyedStoreICKind(kind()) ||
769          IsStoreOwnICKind(kind()) || IsStoreDataPropertyInLiteralKind(kind()) ||
770          IsStoreInArrayLiteralICKind(kind()));
771 
772   Isolate* isolate = GetIsolate();
773   Object* feedback = GetFeedback();
774   bool is_named_feedback = IsPropertyNameFeedback(feedback);
775   if (feedback->IsWeakFixedArray() || is_named_feedback) {
776     int found = 0;
777     WeakFixedArray* array;
778     if (is_named_feedback) {
779       array = WeakFixedArray::cast(GetFeedbackExtra()->ToStrongHeapObject());
780     } else {
781       array = WeakFixedArray::cast(feedback);
782     }
783     const int increment = 2;
784     for (int i = 0; i < array->length(); i += increment) {
785       WeakCell* cell = WeakCell::cast(array->Get(i)->ToStrongHeapObject());
786       if (!cell->cleared()) {
787         Map* map = Map::cast(cell->value());
788         maps->push_back(handle(map, isolate));
789         found++;
790       }
791     }
792     return found;
793   } else if (feedback->IsWeakCell()) {
794     WeakCell* cell = WeakCell::cast(feedback);
795     if (!cell->cleared()) {
796       Map* map = Map::cast(cell->value());
797       maps->push_back(handle(map, isolate));
798       return 1;
799     }
800   }
801 
802   return 0;
803 }
804 
FindHandlerForMap(Handle<Map> map) const805 MaybeObjectHandle FeedbackNexus::FindHandlerForMap(Handle<Map> map) const {
806   DCHECK(IsLoadICKind(kind()) || IsStoreICKind(kind()) ||
807          IsKeyedLoadICKind(kind()) || IsKeyedStoreICKind(kind()) ||
808          IsStoreOwnICKind(kind()) || IsStoreDataPropertyInLiteralKind(kind()));
809 
810   Object* feedback = GetFeedback();
811   Isolate* isolate = GetIsolate();
812   bool is_named_feedback = IsPropertyNameFeedback(feedback);
813   if (feedback->IsWeakFixedArray() || is_named_feedback) {
814     WeakFixedArray* array;
815     if (is_named_feedback) {
816       array = WeakFixedArray::cast(GetFeedbackExtra()->ToStrongHeapObject());
817     } else {
818       array = WeakFixedArray::cast(feedback);
819     }
820     const int increment = 2;
821     for (int i = 0; i < array->length(); i += increment) {
822       WeakCell* cell = WeakCell::cast(array->Get(i)->ToStrongHeapObject());
823       if (!cell->cleared()) {
824         Map* array_map = Map::cast(cell->value());
825         if (array_map == *map &&
826             !array->Get(i + increment - 1)->IsClearedWeakHeapObject()) {
827           MaybeObject* handler = array->Get(i + increment - 1);
828           DCHECK(IC::IsHandler(handler));
829           return handle(handler, isolate);
830         }
831       }
832     }
833   } else if (feedback->IsWeakCell()) {
834     WeakCell* cell = WeakCell::cast(feedback);
835     if (!cell->cleared()) {
836       Map* cell_map = Map::cast(cell->value());
837       if (cell_map == *map && !GetFeedbackExtra()->IsClearedWeakHeapObject()) {
838         MaybeObject* handler = GetFeedbackExtra();
839         DCHECK(IC::IsHandler(handler));
840         return handle(handler, isolate);
841       }
842     }
843   }
844 
845   return MaybeObjectHandle();
846 }
847 
FindHandlers(MaybeObjectHandles * code_list,int length) const848 bool FeedbackNexus::FindHandlers(MaybeObjectHandles* code_list,
849                                  int length) const {
850   DCHECK(IsLoadICKind(kind()) || IsStoreICKind(kind()) ||
851          IsKeyedLoadICKind(kind()) || IsKeyedStoreICKind(kind()) ||
852          IsStoreOwnICKind(kind()) || IsStoreDataPropertyInLiteralKind(kind()) ||
853          IsStoreInArrayLiteralICKind(kind()));
854 
855   Object* feedback = GetFeedback();
856   Isolate* isolate = GetIsolate();
857   int count = 0;
858   bool is_named_feedback = IsPropertyNameFeedback(feedback);
859   if (feedback->IsWeakFixedArray() || is_named_feedback) {
860     WeakFixedArray* array;
861     if (is_named_feedback) {
862       array = WeakFixedArray::cast(GetFeedbackExtra()->ToStrongHeapObject());
863     } else {
864       array = WeakFixedArray::cast(feedback);
865     }
866     const int increment = 2;
867     for (int i = 0; i < array->length(); i += increment) {
868       WeakCell* cell = WeakCell::cast(array->Get(i)->ToStrongHeapObject());
869       // Be sure to skip handlers whose maps have been cleared.
870       if (!cell->cleared() &&
871           !array->Get(i + increment - 1)->IsClearedWeakHeapObject()) {
872         MaybeObject* handler = array->Get(i + increment - 1);
873         DCHECK(IC::IsHandler(handler));
874         code_list->push_back(handle(handler, isolate));
875         count++;
876       }
877     }
878   } else if (feedback->IsWeakCell()) {
879     WeakCell* cell = WeakCell::cast(feedback);
880     MaybeObject* extra = GetFeedbackExtra();
881     if (!cell->cleared() && !extra->IsClearedWeakHeapObject()) {
882       DCHECK(IC::IsHandler(extra));
883       code_list->push_back(handle(extra, isolate));
884       count++;
885     }
886   }
887   return count == length;
888 }
889 
FindFirstName() const890 Name* FeedbackNexus::FindFirstName() const {
891   if (IsKeyedStoreICKind(kind()) || IsKeyedLoadICKind(kind())) {
892     Object* feedback = GetFeedback();
893     if (IsPropertyNameFeedback(feedback)) {
894       return Name::cast(feedback);
895     }
896   }
897   return nullptr;
898 }
899 
GetKeyedAccessLoadMode() const900 KeyedAccessLoadMode FeedbackNexus::GetKeyedAccessLoadMode() const {
901   DCHECK(IsKeyedLoadICKind(kind()));
902   MapHandles maps;
903   MaybeObjectHandles handlers;
904 
905   if (GetKeyType() == PROPERTY) return STANDARD_LOAD;
906 
907   ExtractMaps(&maps);
908   FindHandlers(&handlers, static_cast<int>(maps.size()));
909   for (MaybeObjectHandle const& handler : handlers) {
910     KeyedAccessLoadMode mode = LoadHandler::GetKeyedAccessLoadMode(*handler);
911     if (mode != STANDARD_LOAD) return mode;
912   }
913 
914   return STANDARD_LOAD;
915 }
916 
GetKeyedAccessStoreMode() const917 KeyedAccessStoreMode FeedbackNexus::GetKeyedAccessStoreMode() const {
918   DCHECK(IsKeyedStoreICKind(kind()) || IsStoreInArrayLiteralICKind(kind()));
919   KeyedAccessStoreMode mode = STANDARD_STORE;
920   MapHandles maps;
921   MaybeObjectHandles handlers;
922 
923   if (GetKeyType() == PROPERTY) return mode;
924 
925   ExtractMaps(&maps);
926   FindHandlers(&handlers, static_cast<int>(maps.size()));
927   for (const MaybeObjectHandle& maybe_code_handler : handlers) {
928     // The first handler that isn't the slow handler will have the bits we need.
929     Handle<Code> handler;
930     if (maybe_code_handler.object()->IsStoreHandler()) {
931       Handle<StoreHandler> data_handler =
932           Handle<StoreHandler>::cast(maybe_code_handler.object());
933       handler = handle(Code::cast(data_handler->smi_handler()));
934     } else if (maybe_code_handler.object()->IsSmi()) {
935       // Skip proxy handlers.
936       DCHECK_EQ(*(maybe_code_handler.object()),
937                 *StoreHandler::StoreProxy(GetIsolate()));
938       continue;
939     } else {
940       // Element store without prototype chain check.
941       handler = Handle<Code>::cast(maybe_code_handler.object());
942       if (handler->is_builtin()) continue;
943     }
944     CodeStub::Major major_key = CodeStub::MajorKeyFromKey(handler->stub_key());
945     uint32_t minor_key = CodeStub::MinorKeyFromKey(handler->stub_key());
946     CHECK(major_key == CodeStub::KeyedStoreSloppyArguments ||
947           major_key == CodeStub::StoreFastElement ||
948           major_key == CodeStub::StoreSlowElement ||
949           major_key == CodeStub::StoreInArrayLiteralSlow ||
950           major_key == CodeStub::ElementsTransitionAndStore ||
951           major_key == CodeStub::NoCache);
952     if (major_key != CodeStub::NoCache) {
953       mode = CommonStoreModeBits::decode(minor_key);
954       break;
955     }
956   }
957 
958   return mode;
959 }
960 
GetKeyType() const961 IcCheckType FeedbackNexus::GetKeyType() const {
962   DCHECK(IsKeyedStoreICKind(kind()) || IsKeyedLoadICKind(kind()) ||
963          IsStoreInArrayLiteralICKind(kind()));
964   Object* feedback = GetFeedback();
965   if (feedback == *FeedbackVector::MegamorphicSentinel(GetIsolate())) {
966     return static_cast<IcCheckType>(Smi::ToInt(GetFeedbackExtra()->ToObject()));
967   }
968   return IsPropertyNameFeedback(feedback) ? PROPERTY : ELEMENT;
969 }
970 
GetBinaryOperationFeedback() const971 BinaryOperationHint FeedbackNexus::GetBinaryOperationFeedback() const {
972   DCHECK_EQ(kind(), FeedbackSlotKind::kBinaryOp);
973   int feedback = Smi::ToInt(GetFeedback());
974   return BinaryOperationHintFromFeedback(feedback);
975 }
976 
GetCompareOperationFeedback() const977 CompareOperationHint FeedbackNexus::GetCompareOperationFeedback() const {
978   DCHECK_EQ(kind(), FeedbackSlotKind::kCompareOp);
979   int feedback = Smi::ToInt(GetFeedback());
980   return CompareOperationHintFromFeedback(feedback);
981 }
982 
GetForInFeedback() const983 ForInHint FeedbackNexus::GetForInFeedback() const {
984   DCHECK_EQ(kind(), FeedbackSlotKind::kForIn);
985   int feedback = Smi::ToInt(GetFeedback());
986   return ForInHintFromFeedback(feedback);
987 }
988 
GetFeedbackCell() const989 Handle<FeedbackCell> FeedbackNexus::GetFeedbackCell() const {
990   DCHECK_EQ(FeedbackSlotKind::kCreateClosure, kind());
991   return handle(FeedbackCell::cast(GetFeedback()));
992 }
993 
GetConstructorFeedback() const994 MaybeHandle<JSObject> FeedbackNexus::GetConstructorFeedback() const {
995   DCHECK_EQ(kind(), FeedbackSlotKind::kInstanceOf);
996   Isolate* isolate = GetIsolate();
997   Object* feedback = GetFeedback();
998   if (feedback->IsWeakCell() && !WeakCell::cast(feedback)->cleared()) {
999     return handle(JSObject::cast(WeakCell::cast(feedback)->value()), isolate);
1000   }
1001   return MaybeHandle<JSObject>();
1002 }
1003 
1004 namespace {
1005 
InList(Handle<ArrayList> types,Handle<String> type)1006 bool InList(Handle<ArrayList> types, Handle<String> type) {
1007   for (int i = 0; i < types->Length(); i++) {
1008     Object* obj = types->Get(i);
1009     if (String::cast(obj)->Equals(*type)) {
1010       return true;
1011     }
1012   }
1013   return false;
1014 }
1015 }  // anonymous namespace
1016 
Collect(Handle<String> type,int position)1017 void FeedbackNexus::Collect(Handle<String> type, int position) {
1018   DCHECK(IsTypeProfileKind(kind()));
1019   DCHECK_GE(position, 0);
1020   Isolate* isolate = GetIsolate();
1021 
1022   Object* const feedback = GetFeedback();
1023 
1024   // Map source position to collection of types
1025   Handle<SimpleNumberDictionary> types;
1026 
1027   if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
1028     types = SimpleNumberDictionary::New(isolate, 1);
1029   } else {
1030     types = handle(SimpleNumberDictionary::cast(feedback));
1031   }
1032 
1033   Handle<ArrayList> position_specific_types;
1034 
1035   int entry = types->FindEntry(position);
1036   if (entry == SimpleNumberDictionary::kNotFound) {
1037     position_specific_types = ArrayList::New(isolate, 1);
1038     types = SimpleNumberDictionary::Set(
1039         types, position, ArrayList::Add(position_specific_types, type));
1040   } else {
1041     DCHECK(types->ValueAt(entry)->IsArrayList());
1042     position_specific_types = handle(ArrayList::cast(types->ValueAt(entry)));
1043     if (!InList(position_specific_types, type)) {  // Add type
1044       types = SimpleNumberDictionary::Set(
1045           types, position, ArrayList::Add(position_specific_types, type));
1046     }
1047   }
1048   SetFeedback(*types);
1049 }
1050 
GetSourcePositions() const1051 std::vector<int> FeedbackNexus::GetSourcePositions() const {
1052   DCHECK(IsTypeProfileKind(kind()));
1053   std::vector<int> source_positions;
1054   Isolate* isolate = GetIsolate();
1055 
1056   Object* const feedback = GetFeedback();
1057 
1058   if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
1059     return source_positions;
1060   }
1061 
1062   Handle<SimpleNumberDictionary> types = Handle<SimpleNumberDictionary>(
1063       SimpleNumberDictionary::cast(feedback), isolate);
1064 
1065   for (int index = SimpleNumberDictionary::kElementsStartIndex;
1066        index < types->length(); index += SimpleNumberDictionary::kEntrySize) {
1067     int key_index = index + SimpleNumberDictionary::kEntryKeyIndex;
1068     Object* key = types->get(key_index);
1069     if (key->IsSmi()) {
1070       int position = Smi::cast(key)->value();
1071       source_positions.push_back(position);
1072     }
1073   }
1074   return source_positions;
1075 }
1076 
GetTypesForSourcePositions(uint32_t position) const1077 std::vector<Handle<String>> FeedbackNexus::GetTypesForSourcePositions(
1078     uint32_t position) const {
1079   DCHECK(IsTypeProfileKind(kind()));
1080   Isolate* isolate = GetIsolate();
1081 
1082   Object* const feedback = GetFeedback();
1083   std::vector<Handle<String>> types_for_position;
1084   if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
1085     return types_for_position;
1086   }
1087 
1088   Handle<SimpleNumberDictionary> types = Handle<SimpleNumberDictionary>(
1089       SimpleNumberDictionary::cast(feedback), isolate);
1090 
1091   int entry = types->FindEntry(position);
1092   if (entry == SimpleNumberDictionary::kNotFound) {
1093     return types_for_position;
1094   }
1095   DCHECK(types->ValueAt(entry)->IsArrayList());
1096   Handle<ArrayList> position_specific_types =
1097       Handle<ArrayList>(ArrayList::cast(types->ValueAt(entry)));
1098   for (int i = 0; i < position_specific_types->Length(); i++) {
1099     Object* t = position_specific_types->Get(i);
1100     types_for_position.push_back(Handle<String>(String::cast(t), isolate));
1101   }
1102 
1103   return types_for_position;
1104 }
1105 
1106 namespace {
1107 
ConvertToJSObject(Isolate * isolate,Handle<SimpleNumberDictionary> feedback)1108 Handle<JSObject> ConvertToJSObject(Isolate* isolate,
1109                                    Handle<SimpleNumberDictionary> feedback) {
1110   Handle<JSObject> type_profile =
1111       isolate->factory()->NewJSObject(isolate->object_function());
1112 
1113   for (int index = SimpleNumberDictionary::kElementsStartIndex;
1114        index < feedback->length();
1115        index += SimpleNumberDictionary::kEntrySize) {
1116     int key_index = index + SimpleNumberDictionary::kEntryKeyIndex;
1117     Object* key = feedback->get(key_index);
1118     if (key->IsSmi()) {
1119       int value_index = index + SimpleNumberDictionary::kEntryValueIndex;
1120 
1121       Handle<ArrayList> position_specific_types(
1122           ArrayList::cast(feedback->get(value_index)));
1123 
1124       int position = Smi::ToInt(key);
1125       JSObject::AddDataElement(
1126           type_profile, position,
1127           isolate->factory()->NewJSArrayWithElements(
1128               ArrayList::Elements(position_specific_types)),
1129           PropertyAttributes::NONE)
1130           .ToHandleChecked();
1131     }
1132   }
1133   return type_profile;
1134 }
1135 }  // namespace
1136 
GetTypeProfile() const1137 JSObject* FeedbackNexus::GetTypeProfile() const {
1138   DCHECK(IsTypeProfileKind(kind()));
1139   Isolate* isolate = GetIsolate();
1140 
1141   Object* const feedback = GetFeedback();
1142 
1143   if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
1144     return *isolate->factory()->NewJSObject(isolate->object_function());
1145   }
1146 
1147   return *ConvertToJSObject(isolate,
1148                             handle(SimpleNumberDictionary::cast(feedback)));
1149 }
1150 
ResetTypeProfile()1151 void FeedbackNexus::ResetTypeProfile() {
1152   DCHECK(IsTypeProfileKind(kind()));
1153   SetFeedback(*FeedbackVector::UninitializedSentinel(GetIsolate()));
1154 }
1155 
1156 }  // namespace internal
1157 }  // namespace v8
1158