blob: 768b98502e871de5985755d70bb8b412ecdcfe7f [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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 <ostream>
6
7#include "src/accessors.h"
8#include "src/compilation-dependencies.h"
9#include "src/compiler/access-info.h"
10#include "src/field-index-inl.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010011#include "src/field-type.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010012#include "src/objects-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/type-cache.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014
15namespace v8 {
16namespace internal {
17namespace compiler {
18
19namespace {
20
21bool CanInlineElementAccess(Handle<Map> map) {
22 if (!map->IsJSObjectMap()) return false;
23 if (map->is_access_check_needed()) return false;
24 if (map->has_indexed_interceptor()) return false;
25 ElementsKind const elements_kind = map->elements_kind();
26 if (IsFastElementsKind(elements_kind)) return true;
27 // TODO(bmeurer): Add support for other elements kind.
28 return false;
29}
30
31
32bool CanInlinePropertyAccess(Handle<Map> map) {
33 // We can inline property access to prototypes of all primitives, except
34 // the special Oddball ones that have no wrapper counterparts (i.e. Null,
35 // Undefined and TheHole).
36 STATIC_ASSERT(ODDBALL_TYPE == LAST_PRIMITIVE_TYPE);
37 if (map->IsBooleanMap()) return true;
38 if (map->instance_type() < LAST_PRIMITIVE_TYPE) return true;
39 return map->IsJSObjectMap() && !map->is_dictionary_map() &&
40 !map->has_named_interceptor() &&
41 // TODO(verwaest): Whitelist contexts to which we have access.
42 !map->is_access_check_needed();
43}
44
45} // namespace
46
47
48std::ostream& operator<<(std::ostream& os, AccessMode access_mode) {
49 switch (access_mode) {
50 case AccessMode::kLoad:
51 return os << "Load";
52 case AccessMode::kStore:
53 return os << "Store";
54 }
55 UNREACHABLE();
56 return os;
57}
58
59
60// static
61PropertyAccessInfo PropertyAccessInfo::NotFound(Type* receiver_type,
62 MaybeHandle<JSObject> holder) {
63 return PropertyAccessInfo(holder, receiver_type);
64}
65
66
67// static
68PropertyAccessInfo PropertyAccessInfo::DataConstant(
69 Type* receiver_type, Handle<Object> constant,
70 MaybeHandle<JSObject> holder) {
71 return PropertyAccessInfo(holder, constant, receiver_type);
72}
73
74
75// static
76PropertyAccessInfo PropertyAccessInfo::DataField(
77 Type* receiver_type, FieldIndex field_index, Type* field_type,
Ben Murdoch61f157c2016-09-16 13:49:30 +010078 MaybeHandle<JSObject> holder, MaybeHandle<Map> transition_map) {
79 return PropertyAccessInfo(holder, transition_map, field_index, field_type,
80 receiver_type);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081}
82
83
84ElementAccessInfo::ElementAccessInfo() : receiver_type_(Type::None()) {}
85
86
87ElementAccessInfo::ElementAccessInfo(Type* receiver_type,
88 ElementsKind elements_kind,
89 MaybeHandle<JSObject> holder)
90 : elements_kind_(elements_kind),
91 holder_(holder),
92 receiver_type_(receiver_type) {}
93
94
95PropertyAccessInfo::PropertyAccessInfo()
96 : kind_(kInvalid), receiver_type_(Type::None()), field_type_(Type::Any()) {}
97
98
99PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
100 Type* receiver_type)
101 : kind_(kNotFound),
102 receiver_type_(receiver_type),
103 holder_(holder),
104 field_type_(Type::Any()) {}
105
106
107PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
108 Handle<Object> constant,
109 Type* receiver_type)
110 : kind_(kDataConstant),
111 receiver_type_(receiver_type),
112 constant_(constant),
113 holder_(holder),
114 field_type_(Type::Any()) {}
115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
117 MaybeHandle<Map> transition_map,
Ben Murdoch61f157c2016-09-16 13:49:30 +0100118 FieldIndex field_index, Type* field_type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 Type* receiver_type)
120 : kind_(kDataField),
121 receiver_type_(receiver_type),
122 transition_map_(transition_map),
123 holder_(holder),
124 field_index_(field_index),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000125 field_type_(field_type) {}
126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127AccessInfoFactory::AccessInfoFactory(CompilationDependencies* dependencies,
128 Handle<Context> native_context, Zone* zone)
129 : dependencies_(dependencies),
130 native_context_(native_context),
131 isolate_(native_context->GetIsolate()),
132 type_cache_(TypeCache::Get()),
133 zone_(zone) {
134 DCHECK(native_context->IsNativeContext());
135}
136
137
138bool AccessInfoFactory::ComputeElementAccessInfo(
139 Handle<Map> map, AccessMode access_mode, ElementAccessInfo* access_info) {
140 // Check if it is safe to inline element access for the {map}.
141 if (!CanInlineElementAccess(map)) return false;
142
143 ElementsKind const elements_kind = map->elements_kind();
144
145 // Certain (monomorphic) stores need a prototype chain check because shape
146 // changes could allow callbacks on elements in the chain that are not
147 // compatible with monomorphic keyed stores.
148 MaybeHandle<JSObject> holder;
149 if (access_mode == AccessMode::kStore && map->prototype()->IsJSObject()) {
150 for (PrototypeIterator i(map); !i.IsAtEnd(); i.Advance()) {
151 Handle<JSReceiver> prototype =
152 PrototypeIterator::GetCurrent<JSReceiver>(i);
153 if (!prototype->IsJSObject()) return false;
154 // TODO(bmeurer): We do not currently support unstable prototypes.
155 // We might want to revisit the way we handle certain keyed stores
156 // because this whole prototype chain check is essential a hack,
157 // and I'm not sure that it is correct at all with dictionaries in
158 // the prototype chain.
159 if (!prototype->map()->is_stable()) return false;
160 holder = Handle<JSObject>::cast(prototype);
161 }
162 }
163
164 *access_info =
165 ElementAccessInfo(Type::Class(map, zone()), elements_kind, holder);
166 return true;
167}
168
169
170bool AccessInfoFactory::ComputeElementAccessInfos(
171 MapHandleList const& maps, AccessMode access_mode,
172 ZoneVector<ElementAccessInfo>* access_infos) {
173 // Collect possible transition targets.
174 MapHandleList possible_transition_targets(maps.length());
175 for (Handle<Map> map : maps) {
176 if (Map::TryUpdate(map).ToHandle(&map)) {
177 if (CanInlineElementAccess(map) &&
178 IsFastElementsKind(map->elements_kind()) &&
179 GetInitialFastElementsKind() != map->elements_kind()) {
180 possible_transition_targets.Add(map);
181 }
182 }
183 }
184
185 // Separate the actual receiver maps and the possible transition sources.
186 MapHandleList receiver_maps(maps.length());
187 MapTransitionList transitions(maps.length());
188 for (Handle<Map> map : maps) {
189 if (Map::TryUpdate(map).ToHandle(&map)) {
Ben Murdochc5610432016-08-08 18:44:38 +0100190 Map* transition_target =
191 map->FindElementsKindTransitionedMap(&possible_transition_targets);
192 if (transition_target == nullptr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 receiver_maps.Add(map);
194 } else {
Ben Murdochc5610432016-08-08 18:44:38 +0100195 transitions.push_back(std::make_pair(map, handle(transition_target)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 }
197 }
198 }
199
200 for (Handle<Map> receiver_map : receiver_maps) {
201 // Compute the element access information.
202 ElementAccessInfo access_info;
203 if (!ComputeElementAccessInfo(receiver_map, access_mode, &access_info)) {
204 return false;
205 }
206
207 // Collect the possible transitions for the {receiver_map}.
208 for (auto transition : transitions) {
209 if (transition.second.is_identical_to(receiver_map)) {
210 access_info.transitions().push_back(transition);
211 }
212 }
213
214 // Schedule the access information.
215 access_infos->push_back(access_info);
216 }
217 return true;
218}
219
220
221bool AccessInfoFactory::ComputePropertyAccessInfo(
222 Handle<Map> map, Handle<Name> name, AccessMode access_mode,
223 PropertyAccessInfo* access_info) {
224 // Check if it is safe to inline property access for the {map}.
225 if (!CanInlinePropertyAccess(map)) return false;
226
227 // Compute the receiver type.
228 Handle<Map> receiver_map = map;
229
Ben Murdoch097c5b22016-05-18 11:27:45 +0100230 // Property lookups require the name to be internalized.
231 name = isolate()->factory()->InternalizeName(name);
232
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000233 // We support fast inline cases for certain JSObject getters.
234 if (access_mode == AccessMode::kLoad &&
235 LookupSpecialFieldAccessor(map, name, access_info)) {
236 return true;
237 }
238
239 MaybeHandle<JSObject> holder;
240 do {
241 // Lookup the named property on the {map}.
242 Handle<DescriptorArray> descriptors(map->instance_descriptors(), isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100243 int const number = descriptors->SearchWithCache(isolate(), *name, *map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 if (number != DescriptorArray::kNotFound) {
245 PropertyDetails const details = descriptors->GetDetails(number);
246 if (access_mode == AccessMode::kStore) {
247 // Don't bother optimizing stores to read-only properties.
248 if (details.IsReadOnly()) {
249 return false;
250 }
251 // Check for store to data property on a prototype.
252 if (details.kind() == kData && !holder.is_null()) {
253 // Store to property not found on the receiver but on a prototype, we
254 // need to transition to a new data property.
255 // Implemented according to ES6 section 9.1.9 [[Set]] (P, V, Receiver)
256 return LookupTransition(receiver_map, name, holder, access_info);
257 }
258 }
259 if (details.type() == DATA_CONSTANT) {
260 *access_info = PropertyAccessInfo::DataConstant(
261 Type::Class(receiver_map, zone()),
262 handle(descriptors->GetValue(number), isolate()), holder);
263 return true;
264 } else if (details.type() == DATA) {
265 int index = descriptors->GetFieldIndex(number);
266 Representation field_representation = details.representation();
267 FieldIndex field_index = FieldIndex::ForPropertyIndex(
268 *map, index, field_representation.IsDouble());
269 Type* field_type = Type::Tagged();
270 if (field_representation.IsSmi()) {
271 field_type = type_cache_.kSmi;
272 } else if (field_representation.IsDouble()) {
273 field_type = type_cache_.kFloat64;
274 } else if (field_representation.IsHeapObject()) {
275 // Extract the field type from the property details (make sure its
276 // representation is TaggedPointer to reflect the heap object case).
277 field_type = Type::Intersect(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100278 descriptors->GetFieldType(number)->Convert(zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279 Type::TaggedPointer(), zone());
280 if (field_type->Is(Type::None())) {
281 // Store is not safe if the field type was cleared.
282 if (access_mode == AccessMode::kStore) return false;
283
284 // The field type was cleared by the GC, so we don't know anything
285 // about the contents now.
286 // TODO(bmeurer): It would be awesome to make this saner in the
287 // runtime/GC interaction.
288 field_type = Type::TaggedPointer();
289 } else if (!Type::Any()->Is(field_type)) {
290 // Add proper code dependencies in case of stable field map(s).
291 Handle<Map> field_owner_map(map->FindFieldOwner(number), isolate());
292 dependencies()->AssumeFieldType(field_owner_map);
293 }
294 DCHECK(field_type->Is(Type::TaggedPointer()));
295 }
296 *access_info = PropertyAccessInfo::DataField(
Ben Murdoch61f157c2016-09-16 13:49:30 +0100297 Type::Class(receiver_map, zone()), field_index, field_type, holder);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 return true;
299 } else {
300 // TODO(bmeurer): Add support for accessors.
301 return false;
302 }
303 }
304
305 // Don't search on the prototype chain for special indices in case of
306 // integer indexed exotic objects (see ES6 section 9.4.5).
307 if (map->IsJSTypedArrayMap() && name->IsString() &&
308 IsSpecialIndex(isolate()->unicode_cache(), String::cast(*name))) {
309 return false;
310 }
311
312 // Don't lookup private symbols on the prototype chain.
313 if (name->IsPrivate()) return false;
314
315 // Walk up the prototype chain.
316 if (!map->prototype()->IsJSObject()) {
317 // Perform the implicit ToObject for primitives here.
318 // Implemented according to ES6 section 7.3.2 GetV (V, P).
319 Handle<JSFunction> constructor;
320 if (Map::GetConstructorFunction(map, native_context())
321 .ToHandle(&constructor)) {
322 map = handle(constructor->initial_map(), isolate());
323 DCHECK(map->prototype()->IsJSObject());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100324 } else if (map->prototype()->IsNull(isolate())) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000325 // Store to property not found on the receiver or any prototype, we need
326 // to transition to a new data property.
327 // Implemented according to ES6 section 9.1.9 [[Set]] (P, V, Receiver)
328 if (access_mode == AccessMode::kStore) {
329 return LookupTransition(receiver_map, name, holder, access_info);
330 }
331 // The property was not found, return undefined or throw depending
332 // on the language mode of the load operation.
333 // Implemented according to ES6 section 9.1.8 [[Get]] (P, Receiver)
334 *access_info = PropertyAccessInfo::NotFound(
335 Type::Class(receiver_map, zone()), holder);
336 return true;
337 } else {
338 return false;
339 }
340 }
341 Handle<JSObject> map_prototype(JSObject::cast(map->prototype()), isolate());
342 if (map_prototype->map()->is_deprecated()) {
343 // Try to migrate the prototype object so we don't embed the deprecated
344 // map into the optimized code.
345 JSObject::TryMigrateInstance(map_prototype);
346 }
347 map = handle(map_prototype->map(), isolate());
348 holder = map_prototype;
349 } while (CanInlinePropertyAccess(map));
350 return false;
351}
352
353
354bool AccessInfoFactory::ComputePropertyAccessInfos(
355 MapHandleList const& maps, Handle<Name> name, AccessMode access_mode,
356 ZoneVector<PropertyAccessInfo>* access_infos) {
357 for (Handle<Map> map : maps) {
358 if (Map::TryUpdate(map).ToHandle(&map)) {
359 PropertyAccessInfo access_info;
360 if (!ComputePropertyAccessInfo(map, name, access_mode, &access_info)) {
361 return false;
362 }
363 access_infos->push_back(access_info);
364 }
365 }
366 return true;
367}
368
369
370bool AccessInfoFactory::LookupSpecialFieldAccessor(
371 Handle<Map> map, Handle<Name> name, PropertyAccessInfo* access_info) {
372 // Check for special JSObject field accessors.
373 int offset;
374 if (Accessors::IsJSObjectFieldAccessor(map, name, &offset)) {
375 FieldIndex field_index = FieldIndex::ForInObjectOffset(offset);
376 Type* field_type = Type::Tagged();
377 if (map->IsStringMap()) {
378 DCHECK(Name::Equals(factory()->length_string(), name));
379 // The String::length property is always a smi in the range
380 // [0, String::kMaxLength].
381 field_type = type_cache_.kStringLengthType;
382 } else if (map->IsJSArrayMap()) {
383 DCHECK(Name::Equals(factory()->length_string(), name));
384 // The JSArray::length property is a smi in the range
385 // [0, FixedDoubleArray::kMaxLength] in case of fast double
386 // elements, a smi in the range [0, FixedArray::kMaxLength]
387 // in case of other fast elements, and [0, kMaxUInt32] in
388 // case of other arrays.
389 if (IsFastDoubleElementsKind(map->elements_kind())) {
390 field_type = type_cache_.kFixedDoubleArrayLengthType;
391 } else if (IsFastElementsKind(map->elements_kind())) {
392 field_type = type_cache_.kFixedArrayLengthType;
393 } else {
394 field_type = type_cache_.kJSArrayLengthType;
395 }
396 }
397 *access_info = PropertyAccessInfo::DataField(Type::Class(map, zone()),
398 field_index, field_type);
399 return true;
400 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 return false;
402}
403
404
405bool AccessInfoFactory::LookupTransition(Handle<Map> map, Handle<Name> name,
406 MaybeHandle<JSObject> holder,
407 PropertyAccessInfo* access_info) {
408 // Check if the {map} has a data transition with the given {name}.
409 if (map->unused_property_fields() == 0) return false;
410 Handle<Map> transition_map;
411 if (TransitionArray::SearchTransition(map, kData, name, NONE)
412 .ToHandle(&transition_map)) {
413 int const number = transition_map->LastAdded();
414 PropertyDetails const details =
415 transition_map->instance_descriptors()->GetDetails(number);
416 // Don't bother optimizing stores to read-only properties.
417 if (details.IsReadOnly()) return false;
418 // TODO(bmeurer): Handle transition to data constant?
419 if (details.type() != DATA) return false;
420 int const index = details.field_index();
421 Representation field_representation = details.representation();
422 FieldIndex field_index = FieldIndex::ForPropertyIndex(
423 *transition_map, index, field_representation.IsDouble());
424 Type* field_type = Type::Tagged();
425 if (field_representation.IsSmi()) {
426 field_type = type_cache_.kSmi;
427 } else if (field_representation.IsDouble()) {
428 field_type = type_cache_.kFloat64;
429 } else if (field_representation.IsHeapObject()) {
430 // Extract the field type from the property details (make sure its
431 // representation is TaggedPointer to reflect the heap object case).
432 field_type = Type::Intersect(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100433 transition_map->instance_descriptors()->GetFieldType(number)->Convert(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434 zone()),
435 Type::TaggedPointer(), zone());
436 if (field_type->Is(Type::None())) {
437 // Store is not safe if the field type was cleared.
438 return false;
439 } else if (!Type::Any()->Is(field_type)) {
440 // Add proper code dependencies in case of stable field map(s).
441 Handle<Map> field_owner_map(transition_map->FindFieldOwner(number),
442 isolate());
443 dependencies()->AssumeFieldType(field_owner_map);
444 }
445 DCHECK(field_type->Is(Type::TaggedPointer()));
446 }
447 dependencies()->AssumeMapNotDeprecated(transition_map);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100448 *access_info =
449 PropertyAccessInfo::DataField(Type::Class(map, zone()), field_index,
450 field_type, holder, transition_map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000451 return true;
452 }
453 return false;
454}
455
456
457Factory* AccessInfoFactory::factory() const { return isolate()->factory(); }
458
459} // namespace compiler
460} // namespace internal
461} // namespace v8