• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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 #ifndef V8_OBJECTS_FEEDBACK_VECTOR_INL_H_
6 #define V8_OBJECTS_FEEDBACK_VECTOR_INL_H_
7 
8 #include "src/common/globals.h"
9 #include "src/heap/factory-inl.h"
10 #include "src/heap/heap-write-barrier-inl.h"
11 #include "src/objects/code-inl.h"
12 #include "src/objects/feedback-cell-inl.h"
13 #include "src/objects/feedback-vector.h"
14 #include "src/objects/maybe-object-inl.h"
15 #include "src/objects/shared-function-info.h"
16 #include "src/objects/smi.h"
17 
18 // Has to be the last include (doesn't have include guards):
19 #include "src/objects/object-macros.h"
20 
21 namespace v8 {
22 namespace internal {
23 
24 #include "torque-generated/src/objects/feedback-vector-tq-inl.inc"
25 
26 TQ_OBJECT_CONSTRUCTORS_IMPL(FeedbackVector)
OBJECT_CONSTRUCTORS_IMPL(FeedbackMetadata,HeapObject)27 OBJECT_CONSTRUCTORS_IMPL(FeedbackMetadata, HeapObject)
28 OBJECT_CONSTRUCTORS_IMPL(ClosureFeedbackCellArray, FixedArray)
29 
30 NEVER_READ_ONLY_SPACE_IMPL(FeedbackVector)
31 NEVER_READ_ONLY_SPACE_IMPL(ClosureFeedbackCellArray)
32 
33 CAST_ACCESSOR(FeedbackMetadata)
34 CAST_ACCESSOR(ClosureFeedbackCellArray)
35 
36 INT32_ACCESSORS(FeedbackMetadata, slot_count, kSlotCountOffset)
37 
38 INT32_ACCESSORS(FeedbackMetadata, create_closure_slot_count,
39                 kCreateClosureSlotCountOffset)
40 
41 int32_t FeedbackMetadata::synchronized_slot_count() const {
42   return base::Acquire_Load(
43       reinterpret_cast<const base::Atomic32*>(field_address(kSlotCountOffset)));
44 }
45 
get(int index)46 int32_t FeedbackMetadata::get(int index) const {
47   DCHECK(index >= 0 && index < length());
48   int offset = kHeaderSize + index * kInt32Size;
49   return ReadField<int32_t>(offset);
50 }
51 
set(int index,int32_t value)52 void FeedbackMetadata::set(int index, int32_t value) {
53   DCHECK(index >= 0 && index < length());
54   int offset = kHeaderSize + index * kInt32Size;
55   WriteField<int32_t>(offset, value);
56 }
57 
is_empty()58 bool FeedbackMetadata::is_empty() const { return slot_count() == 0; }
59 
length()60 int FeedbackMetadata::length() const {
61   return FeedbackMetadata::length(slot_count());
62 }
63 
GetSlotSize(FeedbackSlotKind kind)64 int FeedbackMetadata::GetSlotSize(FeedbackSlotKind kind) {
65   switch (kind) {
66     case FeedbackSlotKind::kForIn:
67     case FeedbackSlotKind::kInstanceOf:
68     case FeedbackSlotKind::kCompareOp:
69     case FeedbackSlotKind::kBinaryOp:
70     case FeedbackSlotKind::kLiteral:
71     case FeedbackSlotKind::kTypeProfile:
72       return 1;
73 
74     case FeedbackSlotKind::kCall:
75     case FeedbackSlotKind::kCloneObject:
76     case FeedbackSlotKind::kLoadProperty:
77     case FeedbackSlotKind::kLoadGlobalInsideTypeof:
78     case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
79     case FeedbackSlotKind::kLoadKeyed:
80     case FeedbackSlotKind::kHasKeyed:
81     case FeedbackSlotKind::kStoreNamedSloppy:
82     case FeedbackSlotKind::kStoreNamedStrict:
83     case FeedbackSlotKind::kStoreOwnNamed:
84     case FeedbackSlotKind::kStoreGlobalSloppy:
85     case FeedbackSlotKind::kStoreGlobalStrict:
86     case FeedbackSlotKind::kStoreKeyedSloppy:
87     case FeedbackSlotKind::kStoreKeyedStrict:
88     case FeedbackSlotKind::kStoreInArrayLiteral:
89     case FeedbackSlotKind::kStoreDataPropertyInLiteral:
90       return 2;
91 
92     case FeedbackSlotKind::kInvalid:
93     case FeedbackSlotKind::kKindsNumber:
94       UNREACHABLE();
95   }
96   return 1;
97 }
98 
GetFeedbackCell(int index)99 Handle<FeedbackCell> ClosureFeedbackCellArray::GetFeedbackCell(int index) {
100   return handle(FeedbackCell::cast(get(index)), GetIsolate());
101 }
102 
is_empty()103 bool FeedbackVector::is_empty() const { return length() == 0; }
104 
metadata()105 FeedbackMetadata FeedbackVector::metadata() const {
106   return shared_function_info().feedback_metadata();
107 }
108 
clear_invocation_count()109 void FeedbackVector::clear_invocation_count() { set_invocation_count(0); }
110 
optimized_code()111 Code FeedbackVector::optimized_code() const {
112   MaybeObject slot = maybe_optimized_code();
113   DCHECK(slot->IsWeakOrCleared());
114   HeapObject heap_object;
115   Code code =
116       slot->GetHeapObject(&heap_object) ? Code::cast(heap_object) : Code();
117   // It is possible that the maybe_optimized_code slot is cleared but the
118   // optimization tier hasn't been updated yet. We update the tier when we
119   // execute the function next time / when we create new closure.
120   DCHECK_IMPLIES(!code.is_null(), OptimizationTierBits::decode(flags()) ==
121                                       GetTierForCodeKind(code.kind()));
122   return code;
123 }
124 
optimization_marker()125 OptimizationMarker FeedbackVector::optimization_marker() const {
126   return OptimizationMarkerBits::decode(flags());
127 }
128 
optimization_tier()129 OptimizationTier FeedbackVector::optimization_tier() const {
130   OptimizationTier tier = OptimizationTierBits::decode(flags());
131   // It is possible that the optimization tier bits aren't updated when the code
132   // was cleared due to a GC.
133   DCHECK_IMPLIES(tier == OptimizationTier::kNone,
134                  maybe_optimized_code()->IsCleared());
135   return tier;
136 }
137 
has_optimized_code()138 bool FeedbackVector::has_optimized_code() const {
139   return !optimized_code().is_null();
140 }
141 
has_optimization_marker()142 bool FeedbackVector::has_optimization_marker() const {
143   return optimization_marker() != OptimizationMarker::kLogFirstExecution &&
144          optimization_marker() != OptimizationMarker::kNone;
145 }
146 
147 // Conversion from an integer index to either a slot or an ic slot.
148 // static
ToSlot(intptr_t index)149 FeedbackSlot FeedbackVector::ToSlot(intptr_t index) {
150   DCHECK_LE(static_cast<uintptr_t>(index),
151             static_cast<uintptr_t>(std::numeric_limits<int>::max()));
152   return FeedbackSlot(static_cast<int>(index));
153 }
154 
155 #ifdef DEBUG
156 // Instead of FixedArray, the Feedback and the Extra should contain
157 // WeakFixedArrays. The only allowed FixedArray subtype is HashTable.
IsOfLegacyType(MaybeObject value)158 bool FeedbackVector::IsOfLegacyType(MaybeObject value) {
159   HeapObject heap_object;
160   if (value->GetHeapObject(&heap_object)) {
161     return heap_object.IsFixedArray() && !heap_object.IsHashTable();
162   }
163   return false;
164 }
165 #endif  // DEBUG
166 
Get(FeedbackSlot slot)167 MaybeObject FeedbackVector::Get(FeedbackSlot slot) const {
168   MaybeObject value = raw_feedback_slots(GetIndex(slot));
169   DCHECK(!IsOfLegacyType(value));
170   return value;
171 }
172 
Get(IsolateRoot isolate,FeedbackSlot slot)173 MaybeObject FeedbackVector::Get(IsolateRoot isolate, FeedbackSlot slot) const {
174   MaybeObject value = raw_feedback_slots(isolate, GetIndex(slot));
175   DCHECK(!IsOfLegacyType(value));
176   return value;
177 }
178 
GetClosureFeedbackCell(int index)179 Handle<FeedbackCell> FeedbackVector::GetClosureFeedbackCell(int index) const {
180   DCHECK_GE(index, 0);
181   ClosureFeedbackCellArray cell_array =
182       ClosureFeedbackCellArray::cast(closure_feedback_cell_array());
183   return cell_array.GetFeedbackCell(index);
184 }
185 
SynchronizedGet(FeedbackSlot slot)186 MaybeObject FeedbackVector::SynchronizedGet(FeedbackSlot slot) const {
187   const int i = slot.ToInt();
188   DCHECK_LT(static_cast<unsigned>(i), static_cast<unsigned>(this->length()));
189   const int offset = kRawFeedbackSlotsOffset + i * kTaggedSize;
190   MaybeObject value = TaggedField<MaybeObject>::Acquire_Load(*this, offset);
191   DCHECK(!IsOfLegacyType(value));
192   return value;
193 }
194 
SynchronizedSet(FeedbackSlot slot,MaybeObject value,WriteBarrierMode mode)195 void FeedbackVector::SynchronizedSet(FeedbackSlot slot, MaybeObject value,
196                                      WriteBarrierMode mode) {
197   DCHECK(!IsOfLegacyType(value));
198   const int i = slot.ToInt();
199   DCHECK_LT(static_cast<unsigned>(i), static_cast<unsigned>(this->length()));
200   const int offset = kRawFeedbackSlotsOffset + i * kTaggedSize;
201   TaggedField<MaybeObject>::Release_Store(*this, offset, value);
202   CONDITIONAL_WEAK_WRITE_BARRIER(*this, offset, value, mode);
203 }
204 
SynchronizedSet(FeedbackSlot slot,Object value,WriteBarrierMode mode)205 void FeedbackVector::SynchronizedSet(FeedbackSlot slot, Object value,
206                                      WriteBarrierMode mode) {
207   SynchronizedSet(slot, MaybeObject::FromObject(value), mode);
208 }
209 
Set(FeedbackSlot slot,MaybeObject value,WriteBarrierMode mode)210 void FeedbackVector::Set(FeedbackSlot slot, MaybeObject value,
211                          WriteBarrierMode mode) {
212   DCHECK(!IsOfLegacyType(value));
213   set_raw_feedback_slots(GetIndex(slot), value, mode);
214 }
215 
Set(FeedbackSlot slot,Object value,WriteBarrierMode mode)216 void FeedbackVector::Set(FeedbackSlot slot, Object value,
217                          WriteBarrierMode mode) {
218   MaybeObject maybe_value = MaybeObject::FromObject(value);
219   DCHECK(!IsOfLegacyType(maybe_value));
220   set_raw_feedback_slots(GetIndex(slot), maybe_value, mode);
221 }
222 
slots_start()223 inline MaybeObjectSlot FeedbackVector::slots_start() {
224   return RawMaybeWeakField(OffsetOfElementAt(0));
225 }
226 
227 // Helper function to transform the feedback to BinaryOperationHint.
BinaryOperationHintFromFeedback(int type_feedback)228 BinaryOperationHint BinaryOperationHintFromFeedback(int type_feedback) {
229   switch (type_feedback) {
230     case BinaryOperationFeedback::kNone:
231       return BinaryOperationHint::kNone;
232     case BinaryOperationFeedback::kSignedSmall:
233       return BinaryOperationHint::kSignedSmall;
234     case BinaryOperationFeedback::kSignedSmallInputs:
235       return BinaryOperationHint::kSignedSmallInputs;
236     case BinaryOperationFeedback::kNumber:
237       return BinaryOperationHint::kNumber;
238     case BinaryOperationFeedback::kNumberOrOddball:
239       return BinaryOperationHint::kNumberOrOddball;
240     case BinaryOperationFeedback::kString:
241       return BinaryOperationHint::kString;
242     case BinaryOperationFeedback::kBigInt:
243       return BinaryOperationHint::kBigInt;
244     default:
245       return BinaryOperationHint::kAny;
246   }
247   UNREACHABLE();
248 }
249 
250 // Helper function to transform the feedback to CompareOperationHint.
251 template <CompareOperationFeedback::Type Feedback>
Is(int type_feedback)252 bool Is(int type_feedback) {
253   return !(type_feedback & ~Feedback);
254 }
255 
CompareOperationHintFromFeedback(int type_feedback)256 CompareOperationHint CompareOperationHintFromFeedback(int type_feedback) {
257   if (Is<CompareOperationFeedback::kNone>(type_feedback)) {
258     return CompareOperationHint::kNone;
259   }
260 
261   if (Is<CompareOperationFeedback::kSignedSmall>(type_feedback)) {
262     return CompareOperationHint::kSignedSmall;
263   } else if (Is<CompareOperationFeedback::kNumber>(type_feedback)) {
264     return CompareOperationHint::kNumber;
265   } else if (Is<CompareOperationFeedback::kNumberOrBoolean>(type_feedback)) {
266     return CompareOperationHint::kNumberOrBoolean;
267   }
268 
269   if (Is<CompareOperationFeedback::kInternalizedString>(type_feedback)) {
270     return CompareOperationHint::kInternalizedString;
271   } else if (Is<CompareOperationFeedback::kString>(type_feedback)) {
272     return CompareOperationHint::kString;
273   }
274 
275   if (Is<CompareOperationFeedback::kReceiver>(type_feedback)) {
276     return CompareOperationHint::kReceiver;
277   } else if (Is<CompareOperationFeedback::kReceiverOrNullOrUndefined>(
278                  type_feedback)) {
279     return CompareOperationHint::kReceiverOrNullOrUndefined;
280   }
281 
282   if (Is<CompareOperationFeedback::kBigInt>(type_feedback)) {
283     return CompareOperationHint::kBigInt;
284   }
285 
286   if (Is<CompareOperationFeedback::kSymbol>(type_feedback)) {
287     return CompareOperationHint::kSymbol;
288   }
289 
290   DCHECK(Is<CompareOperationFeedback::kAny>(type_feedback));
291   return CompareOperationHint::kAny;
292 }
293 
294 // Helper function to transform the feedback to ForInHint.
ForInHintFromFeedback(ForInFeedback type_feedback)295 ForInHint ForInHintFromFeedback(ForInFeedback type_feedback) {
296   switch (type_feedback) {
297     case ForInFeedback::kNone:
298       return ForInHint::kNone;
299     case ForInFeedback::kEnumCacheKeys:
300       return ForInHint::kEnumCacheKeys;
301     case ForInFeedback::kEnumCacheKeysAndIndices:
302       return ForInHint::kEnumCacheKeysAndIndices;
303     default:
304       return ForInHint::kAny;
305   }
306   UNREACHABLE();
307 }
308 
UninitializedSentinel(Isolate * isolate)309 Handle<Symbol> FeedbackVector::UninitializedSentinel(Isolate* isolate) {
310   return isolate->factory()->uninitialized_symbol();
311 }
312 
MegamorphicSentinel(Isolate * isolate)313 Handle<Symbol> FeedbackVector::MegamorphicSentinel(Isolate* isolate) {
314   return isolate->factory()->megamorphic_symbol();
315 }
316 
RawUninitializedSentinel(Isolate * isolate)317 Symbol FeedbackVector::RawUninitializedSentinel(Isolate* isolate) {
318   return ReadOnlyRoots(isolate).uninitialized_symbol();
319 }
320 
HasNext()321 bool FeedbackMetadataIterator::HasNext() const {
322   return next_slot_.ToInt() < metadata().slot_count();
323 }
324 
Next()325 FeedbackSlot FeedbackMetadataIterator::Next() {
326   DCHECK(HasNext());
327   cur_slot_ = next_slot_;
328   slot_kind_ = metadata().GetKind(cur_slot_);
329   next_slot_ = FeedbackSlot(next_slot_.ToInt() + entry_size());
330   return cur_slot_;
331 }
332 
entry_size()333 int FeedbackMetadataIterator::entry_size() const {
334   return FeedbackMetadata::GetSlotSize(kind());
335 }
336 
GetFeedback(FeedbackVector vector,FeedbackSlot slot)337 MaybeObject NexusConfig::GetFeedback(FeedbackVector vector,
338                                      FeedbackSlot slot) const {
339   return vector.SynchronizedGet(slot);
340 }
341 
SetFeedback(FeedbackVector vector,FeedbackSlot slot,MaybeObject feedback,WriteBarrierMode mode)342 void NexusConfig::SetFeedback(FeedbackVector vector, FeedbackSlot slot,
343                               MaybeObject feedback,
344                               WriteBarrierMode mode) const {
345   DCHECK(can_write());
346   vector.SynchronizedSet(slot, feedback, mode);
347 }
348 
UninitializedSentinel()349 MaybeObject FeedbackNexus::UninitializedSentinel() const {
350   return MaybeObject::FromObject(
351       *FeedbackVector::UninitializedSentinel(GetIsolate()));
352 }
353 
MegamorphicSentinel()354 MaybeObject FeedbackNexus::MegamorphicSentinel() const {
355   return MaybeObject::FromObject(
356       *FeedbackVector::MegamorphicSentinel(GetIsolate()));
357 }
358 
FromHandle(MaybeObjectHandle slot)359 MaybeObject FeedbackNexus::FromHandle(MaybeObjectHandle slot) const {
360   return slot.is_null() ? HeapObjectReference::ClearedValue(config()->isolate())
361                         : *slot;
362 }
363 
ToHandle(MaybeObject value)364 MaybeObjectHandle FeedbackNexus::ToHandle(MaybeObject value) const {
365   return value.IsCleared() ? MaybeObjectHandle()
366                            : MaybeObjectHandle(config()->NewHandle(value));
367 }
368 
GetFeedback()369 MaybeObject FeedbackNexus::GetFeedback() const {
370   auto pair = GetFeedbackPair();
371   return pair.first;
372 }
373 
GetFeedbackExtra()374 MaybeObject FeedbackNexus::GetFeedbackExtra() const {
375   auto pair = GetFeedbackPair();
376   return pair.second;
377 }
378 
GetFeedbackPair()379 std::pair<MaybeObject, MaybeObject> FeedbackNexus::GetFeedbackPair() const {
380   if (config()->mode() == NexusConfig::BackgroundThread &&
381       feedback_cache_.has_value()) {
382     return std::make_pair(FromHandle(feedback_cache_->first),
383                           FromHandle(feedback_cache_->second));
384   }
385   auto pair = FeedbackMetadata::GetSlotSize(kind()) == 2
386                   ? config()->GetFeedbackPair(vector(), slot())
387                   : std::make_pair(config()->GetFeedback(vector(), slot()),
388                                    MaybeObject());
389   if (config()->mode() == NexusConfig::BackgroundThread &&
390       !feedback_cache_.has_value()) {
391     feedback_cache_ =
392         std::make_pair(ToHandle(pair.first), ToHandle(pair.second));
393   }
394   return pair;
395 }
396 
397 template <typename T>
398 struct IsValidFeedbackType
399     : public std::integral_constant<bool,
400                                     std::is_base_of<MaybeObject, T>::value ||
401                                         std::is_base_of<Object, T>::value> {};
402 
403 template <typename FeedbackType>
SetFeedback(FeedbackType feedback,WriteBarrierMode mode)404 void FeedbackNexus::SetFeedback(FeedbackType feedback, WriteBarrierMode mode) {
405   static_assert(IsValidFeedbackType<FeedbackType>(),
406                 "feedbacks need to be Smi, Object or MaybeObject");
407   MaybeObject fmo = MaybeObject::Create(feedback);
408   config()->SetFeedback(vector(), slot(), fmo, mode);
409 }
410 
411 template <typename FeedbackType, typename FeedbackExtraType>
SetFeedback(FeedbackType feedback,WriteBarrierMode mode,FeedbackExtraType feedback_extra,WriteBarrierMode mode_extra)412 void FeedbackNexus::SetFeedback(FeedbackType feedback, WriteBarrierMode mode,
413                                 FeedbackExtraType feedback_extra,
414                                 WriteBarrierMode mode_extra) {
415   static_assert(IsValidFeedbackType<FeedbackType>(),
416                 "feedbacks need to be Smi, Object or MaybeObject");
417   static_assert(IsValidFeedbackType<FeedbackExtraType>(),
418                 "feedbacks need to be Smi, Object or MaybeObject");
419   MaybeObject fmo = MaybeObject::Create(feedback);
420   MaybeObject fmo_extra = MaybeObject::Create(feedback_extra);
421   config()->SetFeedbackPair(vector(), slot(), fmo, mode, fmo_extra, mode_extra);
422 }
423 
GetIsolate()424 Isolate* FeedbackNexus::GetIsolate() const { return vector().GetIsolate(); }
425 }  // namespace internal
426 }  // namespace v8
427 
428 #include "src/objects/object-macros-undef.h"
429 
430 #endif  // V8_OBJECTS_FEEDBACK_VECTOR_INL_H_
431