blob: 864f336f90cb733f3e2ce28d875383bc25782231 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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#ifndef V8_TYPE_FEEDBACK_VECTOR_H_
6#define V8_TYPE_FEEDBACK_VECTOR_H_
7
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <vector>
9
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#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"
15
16namespace v8 {
17namespace internal {
18
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019class FeedbackVectorSpec {
20 public:
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);
25 }
26
27 int slots() const { return slots_; }
28 void increase_slots(int count) { slots_ += count; }
29
30 int ic_slots() const { return ic_slots_; }
31 void increase_ic_slots(int count) {
32 ic_slots_ += count;
33 if (FLAG_vector_ics) ic_slot_kinds_.resize(ic_slots_);
34 }
35
36 void SetKind(int ic_slot, Code::Kind kind) {
37 DCHECK(FLAG_vector_ics);
38 ic_slot_kinds_[ic_slot] = kind;
39 }
40
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));
44 }
45
46 private:
47 int slots_;
48 int ic_slots_;
49 std::vector<unsigned char> ic_slot_kinds_;
50};
51
52
53// The shape of the TypeFeedbackVector is an array with:
54// 0: first_ic_slot_index (== length() if no ic slots are present)
55// 1: ics_with_types
56// 2: ics_with_generic_info
57// 3: type information for ic slots, if any
58// ...
59// N: first feedback slot (N >= 3)
60// ...
61// [<first_ic_slot_index>: feedback slot]
62// ...to length() - 1
63//
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064class TypeFeedbackVector : public FixedArray {
65 public:
66 // Casting.
67 static TypeFeedbackVector* cast(Object* obj) {
68 DCHECK(obj->IsTypeFeedbackVector());
69 return reinterpret_cast<TypeFeedbackVector*>(obj);
70 }
71
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072 static const int kReservedIndexCount = 3;
73 static const int kFirstICSlotIndex = 0;
74 static const int kWithTypesIndex = 1;
75 static const int kGenericCountIndex = 2;
76
77 int first_ic_slot_index() const {
78 DCHECK(length() >= kReservedIndexCount);
79 return Smi::cast(get(kFirstICSlotIndex))->value();
80 }
81
82 int ic_with_type_info_count() {
83 return length() > 0 ? Smi::cast(get(kWithTypesIndex))->value() : 0;
84 }
85
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.
90 if (value >= 0) {
91 set(kWithTypesIndex, Smi::FromInt(value));
92 }
93 }
94
95 int ic_generic_count() {
96 return length() > 0 ? Smi::cast(get(kGenericCountIndex))->value() : 0;
97 }
98
99 void change_ic_generic_count(int delta) {
100 if (delta == 0) return;
101 int value = ic_generic_count() + delta;
102 if (value >= 0) {
103 set(kGenericCountIndex, Smi::FromInt(value));
104 }
105 }
106
107 inline int ic_metadata_length() const;
108
109 int Slots() const {
110 if (length() == 0) return 0;
111 return Max(
112 0, first_ic_slot_index() - ic_metadata_length() - kReservedIndexCount);
113 }
114
115 int ICSlots() const {
116 if (length() == 0) return 0;
117 return length() - first_ic_slot_index();
118 }
119
120 // Conversion from a slot or ic slot to an integer index to the underlying
121 // array.
122 int GetIndex(FeedbackVectorSlot slot) const {
123 return kReservedIndexCount + ic_metadata_length() + slot.ToInt();
124 }
125
126 int GetIndex(FeedbackVectorICSlot slot) const {
127 int first_ic_slot = first_ic_slot_index();
128 DCHECK(slot.ToInt() < ICSlots());
129 return first_ic_slot + slot.ToInt();
130 }
131
132 // Conversion from an integer index to either a slot or an ic slot. The caller
133 // should know what kind she expects.
134 FeedbackVectorSlot ToSlot(int index) const {
135 DCHECK(index >= kReservedIndexCount && index < first_ic_slot_index());
136 return FeedbackVectorSlot(index - ic_metadata_length() -
137 kReservedIndexCount);
138 }
139
140 FeedbackVectorICSlot ToICSlot(int index) const {
141 DCHECK(index >= first_ic_slot_index() && index < length());
142 return FeedbackVectorICSlot(index - first_ic_slot_index());
143 }
144
145 Object* Get(FeedbackVectorSlot slot) const { return get(GetIndex(slot)); }
146 void Set(FeedbackVectorSlot slot, Object* value,
147 WriteBarrierMode mode = UPDATE_WRITE_BARRIER) {
148 set(GetIndex(slot), value, mode);
149 }
150
151 Object* Get(FeedbackVectorICSlot slot) const { return get(GetIndex(slot)); }
152 void Set(FeedbackVectorICSlot slot, Object* value,
153 WriteBarrierMode mode = UPDATE_WRITE_BARRIER) {
154 set(GetIndex(slot), value, mode);
155 }
156
157 // IC slots need metadata to recognize the type of IC.
158 Code::Kind GetKind(FeedbackVectorICSlot slot) const;
159
160 static Handle<TypeFeedbackVector> Allocate(Isolate* isolate,
161 const FeedbackVectorSpec& spec);
162
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 static Handle<TypeFeedbackVector> Copy(Isolate* isolate,
164 Handle<TypeFeedbackVector> vector);
165
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400166 // Clears the vector slots and the vector ic slots.
167 void ClearSlots(SharedFunctionInfo* shared);
168
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 // The object that indicates an uninitialized cache.
170 static inline Handle<Object> UninitializedSentinel(Isolate* isolate);
171
172 // The object that indicates a megamorphic state.
173 static inline Handle<Object> MegamorphicSentinel(Isolate* isolate);
174
175 // The object that indicates a premonomorphic state.
176 static inline Handle<Object> PremonomorphicSentinel(Isolate* isolate);
177
178 // The object that indicates a generic state.
179 static inline Handle<Object> GenericSentinel(Isolate* isolate);
180
181 // The object that indicates a monomorphic state of Array with
182 // ElementsKind
183 static inline Handle<Object> MonomorphicArraySentinel(
184 Isolate* isolate, ElementsKind elements_kind);
185
186 // A raw version of the uninitialized sentinel that's safe to read during
187 // garbage collection (e.g., for patching the cache).
188 static inline Object* RawUninitializedSentinel(Heap* heap);
189
190 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400191 enum VectorICKind {
192 KindUnused = 0x0,
193 KindCallIC = 0x1,
194 KindLoadIC = 0x2,
195 KindKeyedLoadIC = 0x3
196 };
197
198 static const int kVectorICKindBits = 2;
199 static VectorICKind FromCodeKind(Code::Kind kind);
200 static Code::Kind FromVectorICKind(VectorICKind kind);
201 void SetKind(FeedbackVectorICSlot slot, Code::Kind kind);
202
203 typedef BitSetComputer<VectorICKind, kVectorICKindBits, kSmiValueSize,
204 uint32_t> VectorICComputer;
205
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 DISALLOW_IMPLICIT_CONSTRUCTORS(TypeFeedbackVector);
207};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400208
209
210// A FeedbackNexus is the combination of a TypeFeedbackVector and a slot.
211// Derived classes customize the update and retrieval of feedback.
212class FeedbackNexus {
213 public:
214 FeedbackNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
215 : vector_handle_(vector), vector_(NULL), slot_(slot) {}
216 FeedbackNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
217 : vector_(vector), slot_(slot) {}
218 virtual ~FeedbackNexus() {}
219
220 Handle<TypeFeedbackVector> vector_handle() const {
221 DCHECK(vector_ == NULL);
222 return vector_handle_;
223 }
224 TypeFeedbackVector* vector() const {
225 return vector_handle_.is_null() ? vector_ : *vector_handle_;
226 }
227 FeedbackVectorICSlot slot() const { return slot_; }
228
229 InlineCacheState ic_state() const { return StateFromFeedback(); }
230 Map* FindFirstMap() const {
231 MapHandleList maps;
232 ExtractMaps(&maps);
233 if (maps.length() > 0) return *maps.at(0);
234 return NULL;
235 }
236
237 // TODO(mvstanton): remove FindAllMaps, it didn't survive a code review.
238 void FindAllMaps(MapHandleList* maps) const { ExtractMaps(maps); }
239
240 virtual InlineCacheState StateFromFeedback() const = 0;
241 virtual int ExtractMaps(MapHandleList* maps) const = 0;
242 virtual MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const = 0;
243 virtual bool FindHandlers(CodeHandleList* code_list, int length = -1) const {
244 return length == 0;
245 }
246 virtual Name* FindFirstName() const { return NULL; }
247
248 Object* GetFeedback() const { return vector()->Get(slot()); }
249
250 protected:
251 Isolate* GetIsolate() const { return vector()->GetIsolate(); }
252
253 void SetFeedback(Object* feedback,
254 WriteBarrierMode mode = UPDATE_WRITE_BARRIER) {
255 vector()->Set(slot(), feedback, mode);
256 }
257
258 Handle<FixedArray> EnsureArrayOfSize(int length);
259 void InstallHandlers(int start_index, TypeHandleList* types,
260 CodeHandleList* handlers);
261 int ExtractMaps(int start_index, MapHandleList* maps) const;
262 MaybeHandle<Code> FindHandlerForMap(int start_index, Handle<Map> map) const;
263 bool FindHandlers(int start_index, CodeHandleList* code_list,
264 int length) const;
265
266 private:
267 // The reason for having a vector handle and a raw pointer is that we can and
268 // should use handles during IC miss, but not during GC when we clear ICs. If
269 // you have a handle to the vector that is better because more operations can
270 // be done, like allocation.
271 Handle<TypeFeedbackVector> vector_handle_;
272 TypeFeedbackVector* vector_;
273 FeedbackVectorICSlot slot_;
274};
275
276
277class CallICNexus : public FeedbackNexus {
278 public:
279 CallICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
280 : FeedbackNexus(vector, slot) {
281 DCHECK(vector->GetKind(slot) == Code::CALL_IC);
282 }
283 CallICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
284 : FeedbackNexus(vector, slot) {
285 DCHECK(vector->GetKind(slot) == Code::CALL_IC);
286 }
287
288 void Clear(Code* host);
289
290 void ConfigureUninitialized();
291 void ConfigureGeneric();
292 void ConfigureMonomorphicArray();
293 void ConfigureMonomorphic(Handle<JSFunction> function);
294
295 InlineCacheState StateFromFeedback() const OVERRIDE;
296
297 int ExtractMaps(MapHandleList* maps) const OVERRIDE {
298 // CallICs don't record map feedback.
299 return 0;
300 }
301 MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const OVERRIDE {
302 return MaybeHandle<Code>();
303 }
304 virtual bool FindHandlers(CodeHandleList* code_list,
305 int length = -1) const OVERRIDE {
306 return length == 0;
307 }
308};
309
310
311class LoadICNexus : public FeedbackNexus {
312 public:
313 LoadICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
314 : FeedbackNexus(vector, slot) {
315 DCHECK(vector->GetKind(slot) == Code::LOAD_IC);
316 }
317 LoadICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
318 : FeedbackNexus(vector, slot) {
319 DCHECK(vector->GetKind(slot) == Code::LOAD_IC);
320 }
321
322 void Clear(Code* host);
323
324 void ConfigureMegamorphic();
325 void ConfigurePremonomorphic();
326 void ConfigureMonomorphic(Handle<HeapType> type, Handle<Code> handler);
327
328 void ConfigurePolymorphic(TypeHandleList* types, CodeHandleList* handlers);
329
330 InlineCacheState StateFromFeedback() const OVERRIDE;
331 int ExtractMaps(MapHandleList* maps) const OVERRIDE;
332 MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const OVERRIDE;
333 virtual bool FindHandlers(CodeHandleList* code_list,
334 int length = -1) const OVERRIDE;
335};
336
337
338class KeyedLoadICNexus : public FeedbackNexus {
339 public:
340 KeyedLoadICNexus(Handle<TypeFeedbackVector> vector, FeedbackVectorICSlot slot)
341 : FeedbackNexus(vector, slot) {
342 DCHECK(vector->GetKind(slot) == Code::KEYED_LOAD_IC);
343 }
344 KeyedLoadICNexus(TypeFeedbackVector* vector, FeedbackVectorICSlot slot)
345 : FeedbackNexus(vector, slot) {
346 DCHECK(vector->GetKind(slot) == Code::KEYED_LOAD_IC);
347 }
348
349 void Clear(Code* host);
350
351 void ConfigureGeneric();
352 void ConfigurePremonomorphic();
353 // name can be a null handle for element loads.
354 void ConfigureMonomorphic(Handle<Name> name, Handle<HeapType> type,
355 Handle<Code> handler);
356 // name can be null.
357 void ConfigurePolymorphic(Handle<Name> name, TypeHandleList* types,
358 CodeHandleList* handlers);
359
360 InlineCacheState StateFromFeedback() const OVERRIDE;
361 int ExtractMaps(MapHandleList* maps) const OVERRIDE;
362 MaybeHandle<Code> FindHandlerForMap(Handle<Map> map) const OVERRIDE;
363 virtual bool FindHandlers(CodeHandleList* code_list,
364 int length = -1) const OVERRIDE;
365 Name* FindFirstName() const OVERRIDE;
366};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367}
368} // namespace v8::internal
369
370#endif // V8_TRANSITIONS_H_