blob: 611373f86547dbcdbc7a6dea9fd12b1baae4fb8d [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/ast.h"
8#include "src/code-stubs.h"
9#include "src/compiler.h"
10#include "src/ic/ic.h"
11#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/type-info.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010013
Steve Blocka7e24c12009-10-30 11:49:00 +000014namespace v8 {
15namespace internal {
16
Steve Block6ded16b2010-05-10 14:33:55 +010017
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018TypeFeedbackOracle::TypeFeedbackOracle(
19 Handle<Code> code, Handle<TypeFeedbackVector> feedback_vector,
20 Handle<Context> native_context, Zone* zone)
21 : native_context_(native_context), zone_(zone) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000022 BuildDictionary(code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023 DCHECK(dictionary_->IsDictionary());
24 // We make a copy of the feedback vector because a GC could clear
25 // the type feedback info contained therein.
26 // TODO(mvstanton): revisit the decision to copy when we weakly
27 // traverse the feedback vector at GC time.
28 feedback_vector_ = TypeFeedbackVector::Copy(isolate(), feedback_vector);
Ben Murdochb0fe1622011-05-05 13:52:32 +010029}
30
31
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032static uint32_t IdToKey(TypeFeedbackId ast_id) {
33 return static_cast<uint32_t>(ast_id.ToInt());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010034}
35
36
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037Handle<Object> TypeFeedbackOracle::GetInfo(TypeFeedbackId ast_id) {
38 int entry = dictionary_->FindEntry(IdToKey(ast_id));
39 if (entry != UnseededNumberDictionary::kNotFound) {
40 Object* value = dictionary_->ValueAt(entry);
41 if (value->IsCell()) {
42 Cell* cell = Cell::cast(value);
43 return Handle<Object>(cell->value(), isolate());
44 } else {
45 return Handle<Object>(value, isolate());
46 }
47 }
48 return Handle<Object>::cast(isolate()->factory()->undefined_value());
49}
50
51
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052Handle<Object> TypeFeedbackOracle::GetInfo(FeedbackVectorSlot slot) {
53 DCHECK(slot.ToInt() >= 0 && slot.ToInt() < feedback_vector_->length());
54 Object* obj = feedback_vector_->Get(slot);
55 if (!obj->IsJSFunction() ||
56 !CanRetainOtherContext(JSFunction::cast(obj), *native_context_)) {
57 return Handle<Object>(obj, isolate());
58 }
59 return Handle<Object>::cast(isolate()->factory()->undefined_value());
60}
61
62
63Handle<Object> TypeFeedbackOracle::GetInfo(FeedbackVectorICSlot slot) {
64 DCHECK(slot.ToInt() >= 0 && slot.ToInt() < feedback_vector_->length());
65 Object* obj = feedback_vector_->Get(slot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 if (!obj->IsJSFunction() ||
67 !CanRetainOtherContext(JSFunction::cast(obj), *native_context_)) {
68 return Handle<Object>(obj, isolate());
69 }
70 return Handle<Object>::cast(isolate()->factory()->undefined_value());
71}
72
73
74bool TypeFeedbackOracle::LoadIsUninitialized(TypeFeedbackId id) {
75 Handle<Object> maybe_code = GetInfo(id);
76 if (maybe_code->IsCode()) {
77 Handle<Code> code = Handle<Code>::cast(maybe_code);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010078 return code->is_inline_cache_stub() && code->ic_state() == UNINITIALIZED;
79 }
80 return false;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010081}
82
83
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084bool TypeFeedbackOracle::LoadIsUninitialized(FeedbackVectorICSlot slot) {
85 Code::Kind kind = feedback_vector_->GetKind(slot);
86 if (kind == Code::LOAD_IC) {
87 LoadICNexus nexus(feedback_vector_, slot);
88 return nexus.StateFromFeedback() == UNINITIALIZED;
89 } else if (kind == Code::KEYED_LOAD_IC) {
90 KeyedLoadICNexus nexus(feedback_vector_, slot);
91 return nexus.StateFromFeedback() == UNINITIALIZED;
92 } else if (kind == Code::NUMBER_OF_KINDS) {
93 // Code::NUMBER_OF_KINDS indicates a slot that was never even compiled
94 // in full code.
95 return true;
96 }
97
98 return false;
99}
100
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102bool TypeFeedbackOracle::StoreIsUninitialized(TypeFeedbackId ast_id) {
103 Handle<Object> maybe_code = GetInfo(ast_id);
104 if (!maybe_code->IsCode()) return false;
105 Handle<Code> code = Handle<Code>::cast(maybe_code);
106 return code->ic_state() == UNINITIALIZED;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100107}
108
109
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400110bool TypeFeedbackOracle::CallIsUninitialized(FeedbackVectorICSlot slot) {
111 Handle<Object> value = GetInfo(slot);
112 return value->IsUndefined() ||
113 value.is_identical_to(
114 TypeFeedbackVector::UninitializedSentinel(isolate()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000115}
116
117
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400118bool TypeFeedbackOracle::CallIsMonomorphic(FeedbackVectorICSlot slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 Handle<Object> value = GetInfo(slot);
120 return value->IsAllocationSite() || value->IsJSFunction();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100121}
122
123
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400124bool TypeFeedbackOracle::CallNewIsMonomorphic(FeedbackVectorSlot slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125 Handle<Object> info = GetInfo(slot);
126 return FLAG_pretenuring_call_new
127 ? info->IsJSFunction()
128 : info->IsAllocationSite() || info->IsJSFunction();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129}
130
131
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400132byte TypeFeedbackOracle::ForInType(FeedbackVectorSlot feedback_vector_slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 Handle<Object> value = GetInfo(feedback_vector_slot);
134 return value.is_identical_to(
135 TypeFeedbackVector::UninitializedSentinel(isolate()))
136 ? ForInStatement::FAST_FOR_IN
137 : ForInStatement::SLOW_FOR_IN;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100138}
139
140
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400141void TypeFeedbackOracle::GetStoreModeAndKeyType(
142 TypeFeedbackId ast_id, KeyedAccessStoreMode* store_mode,
143 IcCheckType* key_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 Handle<Object> maybe_code = GetInfo(ast_id);
145 if (maybe_code->IsCode()) {
146 Handle<Code> code = Handle<Code>::cast(maybe_code);
147 if (code->kind() == Code::KEYED_STORE_IC) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400148 ExtraICState extra_ic_state = code->extra_ic_state();
149 *store_mode = KeyedStoreIC::GetKeyedAccessStoreMode(extra_ic_state);
150 *key_type = KeyedStoreIC::GetKeyType(extra_ic_state);
151 return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100152 }
153 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400154 *store_mode = STANDARD_STORE;
155 *key_type = ELEMENT;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100156}
157
Ben Murdochb8e0da22011-05-16 14:20:40 +0100158
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400159void TypeFeedbackOracle::GetLoadKeyType(
160 TypeFeedbackId ast_id, IcCheckType* key_type) {
161 Handle<Object> maybe_code = GetInfo(ast_id);
162 if (maybe_code->IsCode()) {
163 Handle<Code> code = Handle<Code>::cast(maybe_code);
164 if (code->kind() == Code::KEYED_LOAD_IC) {
165 ExtraICState extra_ic_state = code->extra_ic_state();
166 *key_type = KeyedLoadIC::GetKeyType(extra_ic_state);
167 return;
168 }
169 }
170 *key_type = ELEMENT;
171}
172
173
174Handle<JSFunction> TypeFeedbackOracle::GetCallTarget(
175 FeedbackVectorICSlot slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 Handle<Object> info = GetInfo(slot);
177 if (info->IsAllocationSite()) {
178 return Handle<JSFunction>(isolate()->native_context()->array_function());
179 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100180
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 return Handle<JSFunction>::cast(info);
182}
183
184
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400185Handle<JSFunction> TypeFeedbackOracle::GetCallNewTarget(
186 FeedbackVectorSlot slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 Handle<Object> info = GetInfo(slot);
188 if (FLAG_pretenuring_call_new || info->IsJSFunction()) {
189 return Handle<JSFunction>::cast(info);
190 }
191
192 DCHECK(info->IsAllocationSite());
193 return Handle<JSFunction>(isolate()->native_context()->array_function());
194}
195
196
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400197Handle<AllocationSite> TypeFeedbackOracle::GetCallAllocationSite(
198 FeedbackVectorICSlot slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 Handle<Object> info = GetInfo(slot);
200 if (info->IsAllocationSite()) {
201 return Handle<AllocationSite>::cast(info);
202 }
203 return Handle<AllocationSite>::null();
204}
205
206
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400207Handle<AllocationSite> TypeFeedbackOracle::GetCallNewAllocationSite(
208 FeedbackVectorSlot slot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 Handle<Object> info = GetInfo(slot);
210 if (FLAG_pretenuring_call_new || info->IsAllocationSite()) {
211 return Handle<AllocationSite>::cast(info);
212 }
213 return Handle<AllocationSite>::null();
214}
215
216
217bool TypeFeedbackOracle::LoadIsBuiltin(
218 TypeFeedbackId id, Builtins::Name builtin) {
219 return *GetInfo(id) == isolate()->builtins()->builtin(builtin);
220}
221
222
223void TypeFeedbackOracle::CompareType(TypeFeedbackId id,
224 Type** left_type,
225 Type** right_type,
226 Type** combined_type) {
227 Handle<Object> info = GetInfo(id);
228 if (!info->IsCode()) {
229 // For some comparisons we don't have ICs, e.g. LiteralCompareTypeof.
230 *left_type = *right_type = *combined_type = Type::None(zone());
231 return;
232 }
233 Handle<Code> code = Handle<Code>::cast(info);
234
235 Handle<Map> map;
236 Map* raw_map = code->FindFirstMap();
237 if (raw_map != NULL) {
238 if (Map::TryUpdate(handle(raw_map)).ToHandle(&map) &&
239 CanRetainOtherContext(*map, *native_context_)) {
240 map = Handle<Map>::null();
241 }
242 }
243
244 if (code->is_compare_ic_stub()) {
245 CompareICStub stub(code->stub_key(), isolate());
246 *left_type = CompareICState::StateToType(zone(), stub.left());
247 *right_type = CompareICState::StateToType(zone(), stub.right());
248 *combined_type = CompareICState::StateToType(zone(), stub.state(), map);
249 } else if (code->is_compare_nil_ic_stub()) {
250 CompareNilICStub stub(isolate(), code->extra_ic_state());
251 *combined_type = stub.GetType(zone(), map);
252 *left_type = *right_type = stub.GetInputType(zone(), map);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100253 }
254}
255
256
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257void TypeFeedbackOracle::BinaryType(TypeFeedbackId id,
258 Type** left,
259 Type** right,
260 Type** result,
261 Maybe<int>* fixed_right_arg,
262 Handle<AllocationSite>* allocation_site,
263 Token::Value op) {
264 Handle<Object> object = GetInfo(id);
265 if (!object->IsCode()) {
266 // For some binary ops we don't have ICs, e.g. Token::COMMA, but for the
267 // operations covered by the BinaryOpIC we should always have them.
268 DCHECK(op < BinaryOpICState::FIRST_TOKEN ||
269 op > BinaryOpICState::LAST_TOKEN);
270 *left = *right = *result = Type::None(zone());
271 *fixed_right_arg = Maybe<int>();
272 *allocation_site = Handle<AllocationSite>::null();
273 return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000274 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 Handle<Code> code = Handle<Code>::cast(object);
276 DCHECK_EQ(Code::BINARY_OP_IC, code->kind());
277 BinaryOpICState state(isolate(), code->extra_ic_state());
278 DCHECK_EQ(op, state.op());
279
280 *left = state.GetLeftType(zone());
281 *right = state.GetRightType(zone());
282 *result = state.GetResultType(zone());
283 *fixed_right_arg = state.fixed_right_arg();
284
285 AllocationSite* first_allocation_site = code->FindFirstAllocationSite();
286 if (first_allocation_site != NULL) {
287 *allocation_site = handle(first_allocation_site);
288 } else {
289 *allocation_site = Handle<AllocationSite>::null();
290 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000291}
292
293
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294Type* TypeFeedbackOracle::CountType(TypeFeedbackId id) {
295 Handle<Object> object = GetInfo(id);
296 if (!object->IsCode()) return Type::None(zone());
297 Handle<Code> code = Handle<Code>::cast(object);
298 DCHECK_EQ(Code::BINARY_OP_IC, code->kind());
299 BinaryOpICState state(isolate(), code->extra_ic_state());
300 return state.GetLeftType(zone());
301}
302
303
304void TypeFeedbackOracle::PropertyReceiverTypes(TypeFeedbackId id,
305 Handle<String> name,
306 SmallMapList* receiver_types) {
307 receiver_types->Clear();
308 Code::Flags flags = Code::ComputeHandlerFlags(Code::LOAD_IC);
309 CollectReceiverTypes(id, name, flags, receiver_types);
310}
311
312
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400313bool TypeFeedbackOracle::HasOnlyStringMaps(SmallMapList* receiver_types) {
314 bool all_strings = receiver_types->length() > 0;
315 for (int i = 0; i < receiver_types->length(); i++) {
316 all_strings &= receiver_types->at(i)->IsStringMap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400318 return all_strings;
319}
320
321
322void TypeFeedbackOracle::KeyedPropertyReceiverTypes(
323 TypeFeedbackId id,
324 SmallMapList* receiver_types,
325 bool* is_string,
326 IcCheckType* key_type) {
327 receiver_types->Clear();
328 CollectReceiverTypes(id, receiver_types);
329 *is_string = HasOnlyStringMaps(receiver_types);
330 GetLoadKeyType(id, key_type);
331}
332
333
334void TypeFeedbackOracle::PropertyReceiverTypes(FeedbackVectorICSlot slot,
335 Handle<String> name,
336 SmallMapList* receiver_types) {
337 receiver_types->Clear();
338 LoadICNexus nexus(feedback_vector_, slot);
339 Code::Flags flags = Code::ComputeHandlerFlags(Code::LOAD_IC);
340 CollectReceiverTypes(&nexus, name, flags, receiver_types);
341}
342
343
344void TypeFeedbackOracle::KeyedPropertyReceiverTypes(
345 FeedbackVectorICSlot slot, SmallMapList* receiver_types, bool* is_string,
346 IcCheckType* key_type) {
347 receiver_types->Clear();
348 KeyedLoadICNexus nexus(feedback_vector_, slot);
349 CollectReceiverTypes<FeedbackNexus>(&nexus, receiver_types);
350 *is_string = HasOnlyStringMaps(receiver_types);
351 *key_type = nexus.FindFirstName() != NULL ? PROPERTY : ELEMENT;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352}
353
354
355void TypeFeedbackOracle::AssignmentReceiverTypes(
356 TypeFeedbackId id, Handle<String> name, SmallMapList* receiver_types) {
357 receiver_types->Clear();
358 Code::Flags flags = Code::ComputeHandlerFlags(Code::STORE_IC);
359 CollectReceiverTypes(id, name, flags, receiver_types);
360}
361
362
363void TypeFeedbackOracle::KeyedAssignmentReceiverTypes(
364 TypeFeedbackId id, SmallMapList* receiver_types,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400365 KeyedAccessStoreMode* store_mode, IcCheckType* key_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366 receiver_types->Clear();
367 CollectReceiverTypes(id, receiver_types);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400368 GetStoreModeAndKeyType(id, store_mode, key_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369}
370
371
372void TypeFeedbackOracle::CountReceiverTypes(TypeFeedbackId id,
373 SmallMapList* receiver_types) {
374 receiver_types->Clear();
375 CollectReceiverTypes(id, receiver_types);
376}
377
378
379void TypeFeedbackOracle::CollectReceiverTypes(TypeFeedbackId ast_id,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000380 Handle<String> name,
381 Code::Flags flags,
382 SmallMapList* types) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000383 Handle<Object> object = GetInfo(ast_id);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000384 if (object->IsUndefined() || object->IsSmi()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 DCHECK(object->IsCode());
387 Handle<Code> code(Handle<Code>::cast(object));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400388 CollectReceiverTypes<Code>(*code, name, flags, types);
389}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400391
392template <class T>
393void TypeFeedbackOracle::CollectReceiverTypes(T* obj, Handle<String> name,
394 Code::Flags flags,
395 SmallMapList* types) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396 if (FLAG_collect_megamorphic_maps_from_stub_cache &&
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400397 obj->ic_state() == MEGAMORPHIC) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 types->Reserve(4, zone());
399 isolate()->stub_cache()->CollectMatchingMaps(
400 types, name, flags, native_context_, zone());
401 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400402 CollectReceiverTypes<T>(obj, types);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100403 }
404}
405
406
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407// Check if a map originates from a given native context. We use this
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100408// information to filter out maps from different context to avoid
409// retaining objects from different tabs in Chrome via optimized code.
410bool TypeFeedbackOracle::CanRetainOtherContext(Map* map,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411 Context* native_context) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100412 Object* constructor = NULL;
413 while (!map->prototype()->IsNull()) {
414 constructor = map->constructor();
415 if (!constructor->IsNull()) {
416 // If the constructor is not null or a JSFunction, we have to
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 // conservatively assume that it may retain a native context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418 if (!constructor->IsJSFunction()) return true;
419 // Check if the constructor directly references a foreign context.
420 if (CanRetainOtherContext(JSFunction::cast(constructor),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 native_context)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100422 return true;
423 }
424 }
425 map = HeapObject::cast(map->prototype())->map();
426 }
427 constructor = map->constructor();
428 if (constructor->IsNull()) return false;
429 JSFunction* function = JSFunction::cast(constructor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 return CanRetainOtherContext(function, native_context);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100431}
432
433
434bool TypeFeedbackOracle::CanRetainOtherContext(JSFunction* function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435 Context* native_context) {
436 return function->context()->global_object() != native_context->global_object()
437 && function->context()->global_object() != native_context->builtins();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100438}
439
440
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441void TypeFeedbackOracle::CollectReceiverTypes(TypeFeedbackId ast_id,
442 SmallMapList* types) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000443 Handle<Object> object = GetInfo(ast_id);
444 if (!object->IsCode()) return;
445 Handle<Code> code = Handle<Code>::cast(object);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400446 CollectReceiverTypes<Code>(*code, types);
447}
448
449
450template <class T>
451void TypeFeedbackOracle::CollectReceiverTypes(T* obj, SmallMapList* types) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 MapHandleList maps;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400453 if (obj->ic_state() == MONOMORPHIC) {
454 Map* map = obj->FindFirstMap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 if (map != NULL) maps.Add(handle(map));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400456 } else if (obj->ic_state() == POLYMORPHIC) {
457 obj->FindAllMaps(&maps);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 } else {
459 return;
460 }
461 types->Reserve(maps.length(), zone());
462 for (int i = 0; i < maps.length(); i++) {
463 Handle<Map> map(maps.at(i));
464 if (!CanRetainOtherContext(*map, *native_context_)) {
465 types->AddMapIfMissing(map, zone());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000466 }
467 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100468}
469
470
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471byte TypeFeedbackOracle::ToBooleanTypes(TypeFeedbackId id) {
472 Handle<Object> object = GetInfo(id);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000473 return object->IsCode() ? Handle<Code>::cast(object)->to_boolean_state() : 0;
474}
475
476
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000477// Things are a bit tricky here: The iterator for the RelocInfos and the infos
478// themselves are not GC-safe, so we first get all infos, then we create the
479// dictionary (possibly triggering GC), and finally we relocate the collected
480// infos before we process them.
481void TypeFeedbackOracle::BuildDictionary(Handle<Code> code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482 DisallowHeapAllocation no_allocation;
483 ZoneList<RelocInfo> infos(16, zone());
484 HandleScope scope(isolate());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000485 GetRelocInfos(code, &infos);
486 CreateDictionary(code, &infos);
487 ProcessRelocInfos(&infos);
Steve Block44f0eee2011-05-26 01:26:41 +0100488 // Allocate handle in the parent scope.
489 dictionary_ = scope.CloseAndEscape(dictionary_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100490}
491
492
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000493void TypeFeedbackOracle::GetRelocInfos(Handle<Code> code,
494 ZoneList<RelocInfo>* infos) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000495 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000496 for (RelocIterator it(*code, mask); !it.done(); it.next()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497 infos->Add(*it.rinfo(), zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100498 }
499}
500
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000501
502void TypeFeedbackOracle::CreateDictionary(Handle<Code> code,
503 ZoneList<RelocInfo>* infos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 AllowHeapAllocation allocation_allowed;
505 Code* old_code = *code;
506 dictionary_ = UnseededNumberDictionary::New(isolate(), infos->length());
507 RelocateRelocInfos(infos, old_code, *code);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000508}
509
510
511void TypeFeedbackOracle::RelocateRelocInfos(ZoneList<RelocInfo>* infos,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 Code* old_code,
513 Code* new_code) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000514 for (int i = 0; i < infos->length(); i++) {
515 RelocInfo* info = &(*infos)[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 info->set_host(new_code);
517 info->set_pc(new_code->instruction_start() +
518 (info->pc() - old_code->instruction_start()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000519 }
520}
521
522
523void TypeFeedbackOracle::ProcessRelocInfos(ZoneList<RelocInfo>* infos) {
524 for (int i = 0; i < infos->length(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100525 RelocInfo reloc_entry = (*infos)[i];
526 Address target_address = reloc_entry.target_address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000527 TypeFeedbackId ast_id =
528 TypeFeedbackId(static_cast<unsigned>((*infos)[i].data()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100529 Code* target = Code::GetCodeFromTargetAddress(target_address);
530 switch (target->kind()) {
531 case Code::LOAD_IC:
532 case Code::STORE_IC:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100533 case Code::KEYED_LOAD_IC:
534 case Code::KEYED_STORE_IC:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100535 case Code::BINARY_OP_IC:
536 case Code::COMPARE_IC:
537 case Code::TO_BOOLEAN_IC:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 case Code::COMPARE_NIL_IC:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100539 SetInfo(ast_id, target);
540 break;
541
542 default:
543 break;
544 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000545 }
546}
547
548
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549void TypeFeedbackOracle::SetInfo(TypeFeedbackId ast_id, Object* target) {
550 DCHECK(dictionary_->FindEntry(IdToKey(ast_id)) ==
551 UnseededNumberDictionary::kNotFound);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000552 // Dictionary has been allocated with sufficient size for all elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 DisallowHeapAllocation no_need_to_resize_dictionary;
554 HandleScope scope(isolate());
555 USE(UnseededNumberDictionary::AtNumberPut(
556 dictionary_, IdToKey(ast_id), handle(target, isolate())));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000557}
558
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559
Steve Blocka7e24c12009-10-30 11:49:00 +0000560} } // namespace v8::internal