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.
5 #ifndef V8_TYPE_FEEDBACK_VECTOR_H_
6 #define V8_TYPE_FEEDBACK_VECTOR_H_
10 #include "src/checks.h"
11 #include "src/elements-kind.h"
12 #include "src/heap/heap.h"
13 #include "src/isolate.h"
14 #include "src/objects.h"
19 class FeedbackVectorSpec {
21 FeedbackVectorSpec() : slots_(0), ic_slots_(0) {}
22 FeedbackVectorSpec(int slots, int ic_slots)
23 : slots_(slots), ic_slots_(ic_slots) {
24 if (FLAG_vector_ics) ic_slot_kinds_.resize(ic_slots);
27 int slots() const { return slots_; }
28 void increase_slots(int count) { slots_ += count; }
30 int ic_slots() const { return ic_slots_; }
31 void increase_ic_slots(int count) {
33 if (FLAG_vector_ics) ic_slot_kinds_.resize(ic_slots_);
36 void SetKind(int ic_slot, Code::Kind kind) {
37 DCHECK(FLAG_vector_ics);
38 ic_slot_kinds_[ic_slot] = kind;
41 Code::Kind GetKind(int ic_slot) const {
42 DCHECK(FLAG_vector_ics);
43 return static_cast<Code::Kind>(ic_slot_kinds_.at(ic_slot));
49 std::vector<unsigned char> ic_slot_kinds_;
53 // The shape of the TypeFeedbackVector is an array with:
54 // 0: first_ic_slot_index (== length() if no ic slots are present)
56 // 2: ics_with_generic_info
57 // 3: type information for ic slots, if any
59 // N: first feedback slot (N >= 3)
61 // [<first_ic_slot_index>: feedback slot]
64 class TypeFeedbackVector : public FixedArray {
67 static TypeFeedbackVector* cast(Object* obj) {
68 DCHECK(obj->IsTypeFeedbackVector());
69 return reinterpret_cast<TypeFeedbackVector*>(obj);
72 static const int kReservedIndexCount = 3;
73 static const int kFirstICSlotIndex = 0;
74 static const int kWithTypesIndex = 1;
75 static const int kGenericCountIndex = 2;
77 int first_ic_slot_index() const {
78 DCHECK(length() >= kReservedIndexCount);
79 return Smi::cast(get(kFirstICSlotIndex))->value();
82 int ic_with_type_info_count() {
83 return length() > 0 ? Smi::cast(get(kWithTypesIndex))->value() : 0;
86 void change_ic_with_type_info_count(int delta) {
87 if (delta == 0) return;
88 int value = ic_with_type_info_count() + delta;
89 // Could go negative because of the debugger.
91 set(kWithTypesIndex, Smi::FromInt(value));
95 int ic_generic_count() {
96 return length() > 0 ? Smi::cast(get(kGenericCountIndex))->value() : 0;
99 void change_ic_generic_count(int delta) {
100 if (delta == 0) return;
101 int value = ic_generic_count() + delta;
103 set(kGenericCountIndex, Smi::FromInt(value));
107 inline int ic_metadata_length() const;
110 if (length() == 0) return 0;
112 0, first_ic_slot_index() - ic_metadata_length() - kReservedIndexCount);
115 int ICSlots() const {
116 if (length() == 0) return 0;
117 return length() - first_ic_slot_index();
120 // Conversion from a slot or ic slot to an integer index to the underlying
122 int GetIndex(FeedbackVectorSlot slot) const {
123 DCHECK(slot.ToInt() < first_ic_slot_index());
124 return kReservedIndexCount + ic_metadata_length() + slot.ToInt();
127 int GetIndex(FeedbackVectorICSlot slot) const {
128 int first_ic_slot = first_ic_slot_index();
129 DCHECK(slot.ToInt() < ICSlots());
130 return first_ic_slot + slot.ToInt();
133 // Conversion from an integer index to either a slot or an ic slot. The caller
134 // should know what kind she expects.
135 FeedbackVectorSlot ToSlot(int index) const {
136 DCHECK(index >= kReservedIndexCount && index < first_ic_slot_index());
137 return FeedbackVectorSlot(index - ic_metadata_length() -
138 kReservedIndexCount);
141 FeedbackVectorICSlot ToICSlot(int index) const {
142 DCHECK(index >= first_ic_slot_index() && index < length());
143 return FeedbackVectorICSlot(index - first_ic_slot_index());
146 Object* Get(FeedbackVectorSlot slot) const { return get(GetIndex(slot)); }
147 void Set(FeedbackVectorSlot slot, Object* value,
148 WriteBarrierMode mode = UPDATE_WRITE_BARRIER) {
149 set(GetIndex(slot), value, mode);
152 Object* Get(FeedbackVectorICSlot slot) const { return get(GetIndex(slot)); }
153 void Set(FeedbackVectorICSlot slot, Object* value,
154 WriteBarrierMode mode = UPDATE_WRITE_BARRIER) {
155 set(GetIndex(slot), value, mode);
158 // IC slots need metadata to recognize the type of IC.
159 Code::Kind GetKind(FeedbackVectorICSlot slot) const;
161 static Handle<TypeFeedbackVector> Allocate(Isolate* isolate,
162 const FeedbackVectorSpec& spec);
164 static Handle<TypeFeedbackVector> Copy(Isolate* isolate,
165 Handle<TypeFeedbackVector> vector);
167 // Clears the vector slots and the vector ic slots.
168 void ClearSlots(SharedFunctionInfo* shared);
169 void ClearICSlots(SharedFunctionInfo* shared) {
170 ClearICSlotsImpl(shared, true);
172 void ClearICSlotsAtGCTime(SharedFunctionInfo* shared) {
173 ClearICSlotsImpl(shared, false);
176 // The object that indicates an uninitialized cache.
177 static inline Handle<Object> UninitializedSentinel(Isolate* isolate);
179 // The object that indicates a megamorphic state.
180 static inline Handle<Object> MegamorphicSentinel(Isolate* isolate);
182 // The object that indicates a premonomorphic state.
183 static inline Handle<Object> PremonomorphicSentinel(Isolate* isolate);
185 // The object that indicates a monomorphic state of Array with
187 static inline Handle<Object> MonomorphicArraySentinel(
188 Isolate* isolate, ElementsKind elements_kind);
190 // A raw version of the uninitialized sentinel that's safe to read during
191 // garbage collection (e.g., for patching the cache).
192 static inline Object* RawUninitializedSentinel(Heap* heap);
199 KindKeyedLoadIC = 0x3
202 static const int kVectorICKindBits = 2;
203 static VectorICKind FromCodeKind(Code::Kind kind);
204 static Code::Kind FromVectorICKind(VectorICKind kind);
205 void SetKind(FeedbackVectorICSlot slot, Code::Kind kind);
207 typedef BitSetComputer<VectorICKind, kVectorICKindBits, kSmiValueSize,
208 uint32_t> VectorICComputer;
210 void ClearICSlotsImpl(SharedFunctionInfo* shared, bool force_clear);
212 DISALLOW_IMPLICIT_CONSTRUCTORS(TypeFeedbackVector);
216 // A FeedbackNexus is the combination of a TypeFeedbackVector and a slot.
217 // Derived classes customize the update and retrieval of feedback.
218 class FeedbackNexus {
220 FeedbackNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
221 : vector_handle_(vector), vector_(NULL), slot_(slot) {}
222 FeedbackNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
223 : vector_(vector), slot_(slot) {}
224 virtual ~FeedbackNexus() {}
226 Handle<TypeFeedbackVector> vector_handle() const {
227 DCHECK(vector_ == NULL);
228 return vector_handle_;
230 TypeFeedbackVector* vector() const {
231 return vector_handle_.is_null() ? vector_ : *vector_handle_;
233 FeedbackVectorICSlot slot() const { return slot_; }
235 InlineCacheState ic_state() const { return StateFromFeedback(); }
236 Map* FindFirstMap() const {
239 if (maps.length() > 0) return *maps.at(0);
243 // TODO(mvstanton): remove FindAllMaps, it didn't survive a code review.
244 void FindAllMaps(MapHandleList* maps) const { ExtractMaps(maps); }
246 virtual InlineCacheState StateFromFeedback() const = 0;
247 virtual int ExtractMaps(MapHandleList* maps) const = 0;
248 virtual MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const = 0;
249 virtual bool FindHandlers(CodeHandleList* code_list, int length = -1) const {
252 virtual Name* FindFirstName() const { return NULL; }
254 Object* GetFeedback() const { return vector()->Get(slot()); }
257 Isolate* GetIsolate() const { return vector()->GetIsolate(); }
259 void SetFeedback(Object* feedback,
260 WriteBarrierMode mode = UPDATE_WRITE_BARRIER) {
261 vector()->Set(slot(), feedback, mode);
264 Handle<FixedArray> EnsureArrayOfSize(int length);
265 void InstallHandlers(int start_index, MapHandleList* maps,
266 CodeHandleList* handlers);
267 int ExtractMaps(int start_index, MapHandleList* maps) const;
268 MaybeHandle<Code> FindHandlerForMap(int start_index, Handle<Map> map) const;
269 bool FindHandlers(int start_index, CodeHandleList* code_list,
273 // The reason for having a vector handle and a raw pointer is that we can and
274 // should use handles during IC miss, but not during GC when we clear ICs. If
275 // you have a handle to the vector that is better because more operations can
276 // be done, like allocation.
277 Handle<TypeFeedbackVector> vector_handle_;
278 TypeFeedbackVector* vector_;
279 FeedbackVectorICSlot slot_;
283 class CallICNexus : public FeedbackNexus {
285 CallICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
286 : FeedbackNexus(vector, slot) {
287 DCHECK(vector->GetKind(slot) == Code::CALL_IC);
289 CallICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
290 : FeedbackNexus(vector, slot) {
291 DCHECK(vector->GetKind(slot) == Code::CALL_IC);
294 void Clear(Code* host);
296 void ConfigureUninitialized();
297 void ConfigureGeneric();
298 void ConfigureMonomorphicArray();
299 void ConfigureMonomorphic(Handle<JSFunction> function);
301 InlineCacheState StateFromFeedback() const OVERRIDE;
303 int ExtractMaps(MapHandleList* maps) const OVERRIDE {
304 // CallICs don't record map feedback.
307 MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const OVERRIDE {
308 return MaybeHandle<Code>();
310 virtual bool FindHandlers(CodeHandleList* code_list,
311 int length = -1) const OVERRIDE {
317 class LoadICNexus : public FeedbackNexus {
319 LoadICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
320 : FeedbackNexus(vector, slot) {
321 DCHECK(vector->GetKind(slot) == Code::LOAD_IC);
323 LoadICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
324 : FeedbackNexus(vector, slot) {
325 DCHECK(vector->GetKind(slot) == Code::LOAD_IC);
328 void Clear(Code* host);
330 void ConfigureMegamorphic();
331 void ConfigurePremonomorphic();
332 void ConfigureMonomorphic(Handle<Map> receiver_map, Handle<Code> handler);
334 void ConfigurePolymorphic(MapHandleList* maps, CodeHandleList* handlers);
336 InlineCacheState StateFromFeedback() const OVERRIDE;
337 int ExtractMaps(MapHandleList* maps) const OVERRIDE;
338 MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const OVERRIDE;
339 virtual bool FindHandlers(CodeHandleList* code_list,
340 int length = -1) const OVERRIDE;
344 class KeyedLoadICNexus : public FeedbackNexus {
346 KeyedLoadICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
347 : FeedbackNexus(vector, slot) {
348 DCHECK(vector->GetKind(slot) == Code::KEYED_LOAD_IC);
350 KeyedLoadICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
351 : FeedbackNexus(vector, slot) {
352 DCHECK(vector->GetKind(slot) == Code::KEYED_LOAD_IC);
355 void Clear(Code* host);
357 void ConfigureMegamorphic();
358 void ConfigurePremonomorphic();
359 // name can be a null handle for element loads.
360 void ConfigureMonomorphic(Handle<Name> name, Handle<Map> receiver_map,
361 Handle<Code> handler);
363 void ConfigurePolymorphic(Handle<Name> name, MapHandleList* maps,
364 CodeHandleList* handlers);
366 InlineCacheState StateFromFeedback() const OVERRIDE;
367 int ExtractMaps(MapHandleList* maps) const OVERRIDE;
368 MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const OVERRIDE;
369 virtual bool FindHandlers(CodeHandleList* code_list,
370 int length = -1) const OVERRIDE;
371 Name* FindFirstName() const OVERRIDE;
374 } // namespace v8::internal
376 #endif // V8_TRANSITIONS_H_