blob: 4a2a857029a3d88d225385101d8017af5f0bb769 [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 Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/objects-inl.h" // TODO(mstarzinger): Temporary cycle breaker!
13#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,
78 FieldCheck field_check, MaybeHandle<JSObject> holder,
79 MaybeHandle<Map> transition_map) {
80 return PropertyAccessInfo(holder, transition_map, field_index, field_check,
81 field_type, receiver_type);
82}
83
84
85ElementAccessInfo::ElementAccessInfo() : receiver_type_(Type::None()) {}
86
87
88ElementAccessInfo::ElementAccessInfo(Type* receiver_type,
89 ElementsKind elements_kind,
90 MaybeHandle<JSObject> holder)
91 : elements_kind_(elements_kind),
92 holder_(holder),
93 receiver_type_(receiver_type) {}
94
95
96PropertyAccessInfo::PropertyAccessInfo()
97 : kind_(kInvalid), receiver_type_(Type::None()), field_type_(Type::Any()) {}
98
99
100PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
101 Type* receiver_type)
102 : kind_(kNotFound),
103 receiver_type_(receiver_type),
104 holder_(holder),
105 field_type_(Type::Any()) {}
106
107
108PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
109 Handle<Object> constant,
110 Type* receiver_type)
111 : kind_(kDataConstant),
112 receiver_type_(receiver_type),
113 constant_(constant),
114 holder_(holder),
115 field_type_(Type::Any()) {}
116
117
118PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
119 MaybeHandle<Map> transition_map,
120 FieldIndex field_index,
121 FieldCheck field_check, Type* field_type,
122 Type* receiver_type)
123 : kind_(kDataField),
124 receiver_type_(receiver_type),
125 transition_map_(transition_map),
126 holder_(holder),
127 field_index_(field_index),
128 field_check_(field_check),
129 field_type_(field_type) {}
130
131
132AccessInfoFactory::AccessInfoFactory(CompilationDependencies* dependencies,
133 Handle<Context> native_context, Zone* zone)
134 : dependencies_(dependencies),
135 native_context_(native_context),
136 isolate_(native_context->GetIsolate()),
137 type_cache_(TypeCache::Get()),
138 zone_(zone) {
139 DCHECK(native_context->IsNativeContext());
140}
141
142
143bool AccessInfoFactory::ComputeElementAccessInfo(
144 Handle<Map> map, AccessMode access_mode, ElementAccessInfo* access_info) {
145 // Check if it is safe to inline element access for the {map}.
146 if (!CanInlineElementAccess(map)) return false;
147
148 ElementsKind const elements_kind = map->elements_kind();
149
150 // Certain (monomorphic) stores need a prototype chain check because shape
151 // changes could allow callbacks on elements in the chain that are not
152 // compatible with monomorphic keyed stores.
153 MaybeHandle<JSObject> holder;
154 if (access_mode == AccessMode::kStore && map->prototype()->IsJSObject()) {
155 for (PrototypeIterator i(map); !i.IsAtEnd(); i.Advance()) {
156 Handle<JSReceiver> prototype =
157 PrototypeIterator::GetCurrent<JSReceiver>(i);
158 if (!prototype->IsJSObject()) return false;
159 // TODO(bmeurer): We do not currently support unstable prototypes.
160 // We might want to revisit the way we handle certain keyed stores
161 // because this whole prototype chain check is essential a hack,
162 // and I'm not sure that it is correct at all with dictionaries in
163 // the prototype chain.
164 if (!prototype->map()->is_stable()) return false;
165 holder = Handle<JSObject>::cast(prototype);
166 }
167 }
168
169 *access_info =
170 ElementAccessInfo(Type::Class(map, zone()), elements_kind, holder);
171 return true;
172}
173
174
175bool AccessInfoFactory::ComputeElementAccessInfos(
176 MapHandleList const& maps, AccessMode access_mode,
177 ZoneVector<ElementAccessInfo>* access_infos) {
178 // Collect possible transition targets.
179 MapHandleList possible_transition_targets(maps.length());
180 for (Handle<Map> map : maps) {
181 if (Map::TryUpdate(map).ToHandle(&map)) {
182 if (CanInlineElementAccess(map) &&
183 IsFastElementsKind(map->elements_kind()) &&
184 GetInitialFastElementsKind() != map->elements_kind()) {
185 possible_transition_targets.Add(map);
186 }
187 }
188 }
189
190 // Separate the actual receiver maps and the possible transition sources.
191 MapHandleList receiver_maps(maps.length());
192 MapTransitionList transitions(maps.length());
193 for (Handle<Map> map : maps) {
194 if (Map::TryUpdate(map).ToHandle(&map)) {
195 Handle<Map> transition_target =
196 Map::FindTransitionedMap(map, &possible_transition_targets);
197 if (transition_target.is_null()) {
198 receiver_maps.Add(map);
199 } else {
200 transitions.push_back(std::make_pair(map, transition_target));
201 }
202 }
203 }
204
205 for (Handle<Map> receiver_map : receiver_maps) {
206 // Compute the element access information.
207 ElementAccessInfo access_info;
208 if (!ComputeElementAccessInfo(receiver_map, access_mode, &access_info)) {
209 return false;
210 }
211
212 // Collect the possible transitions for the {receiver_map}.
213 for (auto transition : transitions) {
214 if (transition.second.is_identical_to(receiver_map)) {
215 access_info.transitions().push_back(transition);
216 }
217 }
218
219 // Schedule the access information.
220 access_infos->push_back(access_info);
221 }
222 return true;
223}
224
225
226bool AccessInfoFactory::ComputePropertyAccessInfo(
227 Handle<Map> map, Handle<Name> name, AccessMode access_mode,
228 PropertyAccessInfo* access_info) {
229 // Check if it is safe to inline property access for the {map}.
230 if (!CanInlinePropertyAccess(map)) return false;
231
232 // Compute the receiver type.
233 Handle<Map> receiver_map = map;
234
Ben Murdoch097c5b22016-05-18 11:27:45 +0100235 // Property lookups require the name to be internalized.
236 name = isolate()->factory()->InternalizeName(name);
237
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000238 // We support fast inline cases for certain JSObject getters.
239 if (access_mode == AccessMode::kLoad &&
240 LookupSpecialFieldAccessor(map, name, access_info)) {
241 return true;
242 }
243
244 MaybeHandle<JSObject> holder;
245 do {
246 // Lookup the named property on the {map}.
247 Handle<DescriptorArray> descriptors(map->instance_descriptors(), isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100248 int const number = descriptors->SearchWithCache(isolate(), *name, *map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 if (number != DescriptorArray::kNotFound) {
250 PropertyDetails const details = descriptors->GetDetails(number);
251 if (access_mode == AccessMode::kStore) {
252 // Don't bother optimizing stores to read-only properties.
253 if (details.IsReadOnly()) {
254 return false;
255 }
256 // Check for store to data property on a prototype.
257 if (details.kind() == kData && !holder.is_null()) {
258 // Store to property not found on the receiver but on a prototype, we
259 // need to transition to a new data property.
260 // Implemented according to ES6 section 9.1.9 [[Set]] (P, V, Receiver)
261 return LookupTransition(receiver_map, name, holder, access_info);
262 }
263 }
264 if (details.type() == DATA_CONSTANT) {
265 *access_info = PropertyAccessInfo::DataConstant(
266 Type::Class(receiver_map, zone()),
267 handle(descriptors->GetValue(number), isolate()), holder);
268 return true;
269 } else if (details.type() == DATA) {
270 int index = descriptors->GetFieldIndex(number);
271 Representation field_representation = details.representation();
272 FieldIndex field_index = FieldIndex::ForPropertyIndex(
273 *map, index, field_representation.IsDouble());
274 Type* field_type = Type::Tagged();
275 if (field_representation.IsSmi()) {
276 field_type = type_cache_.kSmi;
277 } else if (field_representation.IsDouble()) {
278 field_type = type_cache_.kFloat64;
279 } else if (field_representation.IsHeapObject()) {
280 // Extract the field type from the property details (make sure its
281 // representation is TaggedPointer to reflect the heap object case).
282 field_type = Type::Intersect(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100283 descriptors->GetFieldType(number)->Convert(zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 Type::TaggedPointer(), zone());
285 if (field_type->Is(Type::None())) {
286 // Store is not safe if the field type was cleared.
287 if (access_mode == AccessMode::kStore) return false;
288
289 // The field type was cleared by the GC, so we don't know anything
290 // about the contents now.
291 // TODO(bmeurer): It would be awesome to make this saner in the
292 // runtime/GC interaction.
293 field_type = Type::TaggedPointer();
294 } else if (!Type::Any()->Is(field_type)) {
295 // Add proper code dependencies in case of stable field map(s).
296 Handle<Map> field_owner_map(map->FindFieldOwner(number), isolate());
297 dependencies()->AssumeFieldType(field_owner_map);
298 }
299 DCHECK(field_type->Is(Type::TaggedPointer()));
300 }
301 *access_info = PropertyAccessInfo::DataField(
302 Type::Class(receiver_map, zone()), field_index, field_type,
303 FieldCheck::kNone, holder);
304 return true;
305 } else {
306 // TODO(bmeurer): Add support for accessors.
307 return false;
308 }
309 }
310
311 // Don't search on the prototype chain for special indices in case of
312 // integer indexed exotic objects (see ES6 section 9.4.5).
313 if (map->IsJSTypedArrayMap() && name->IsString() &&
314 IsSpecialIndex(isolate()->unicode_cache(), String::cast(*name))) {
315 return false;
316 }
317
318 // Don't lookup private symbols on the prototype chain.
319 if (name->IsPrivate()) return false;
320
321 // Walk up the prototype chain.
322 if (!map->prototype()->IsJSObject()) {
323 // Perform the implicit ToObject for primitives here.
324 // Implemented according to ES6 section 7.3.2 GetV (V, P).
325 Handle<JSFunction> constructor;
326 if (Map::GetConstructorFunction(map, native_context())
327 .ToHandle(&constructor)) {
328 map = handle(constructor->initial_map(), isolate());
329 DCHECK(map->prototype()->IsJSObject());
330 } else if (map->prototype()->IsNull()) {
331 // Store to property not found on the receiver or any prototype, we need
332 // to transition to a new data property.
333 // Implemented according to ES6 section 9.1.9 [[Set]] (P, V, Receiver)
334 if (access_mode == AccessMode::kStore) {
335 return LookupTransition(receiver_map, name, holder, access_info);
336 }
337 // The property was not found, return undefined or throw depending
338 // on the language mode of the load operation.
339 // Implemented according to ES6 section 9.1.8 [[Get]] (P, Receiver)
340 *access_info = PropertyAccessInfo::NotFound(
341 Type::Class(receiver_map, zone()), holder);
342 return true;
343 } else {
344 return false;
345 }
346 }
347 Handle<JSObject> map_prototype(JSObject::cast(map->prototype()), isolate());
348 if (map_prototype->map()->is_deprecated()) {
349 // Try to migrate the prototype object so we don't embed the deprecated
350 // map into the optimized code.
351 JSObject::TryMigrateInstance(map_prototype);
352 }
353 map = handle(map_prototype->map(), isolate());
354 holder = map_prototype;
355 } while (CanInlinePropertyAccess(map));
356 return false;
357}
358
359
360bool AccessInfoFactory::ComputePropertyAccessInfos(
361 MapHandleList const& maps, Handle<Name> name, AccessMode access_mode,
362 ZoneVector<PropertyAccessInfo>* access_infos) {
363 for (Handle<Map> map : maps) {
364 if (Map::TryUpdate(map).ToHandle(&map)) {
365 PropertyAccessInfo access_info;
366 if (!ComputePropertyAccessInfo(map, name, access_mode, &access_info)) {
367 return false;
368 }
369 access_infos->push_back(access_info);
370 }
371 }
372 return true;
373}
374
375
376bool AccessInfoFactory::LookupSpecialFieldAccessor(
377 Handle<Map> map, Handle<Name> name, PropertyAccessInfo* access_info) {
378 // Check for special JSObject field accessors.
379 int offset;
380 if (Accessors::IsJSObjectFieldAccessor(map, name, &offset)) {
381 FieldIndex field_index = FieldIndex::ForInObjectOffset(offset);
382 Type* field_type = Type::Tagged();
383 if (map->IsStringMap()) {
384 DCHECK(Name::Equals(factory()->length_string(), name));
385 // The String::length property is always a smi in the range
386 // [0, String::kMaxLength].
387 field_type = type_cache_.kStringLengthType;
388 } else if (map->IsJSArrayMap()) {
389 DCHECK(Name::Equals(factory()->length_string(), name));
390 // The JSArray::length property is a smi in the range
391 // [0, FixedDoubleArray::kMaxLength] in case of fast double
392 // elements, a smi in the range [0, FixedArray::kMaxLength]
393 // in case of other fast elements, and [0, kMaxUInt32] in
394 // case of other arrays.
395 if (IsFastDoubleElementsKind(map->elements_kind())) {
396 field_type = type_cache_.kFixedDoubleArrayLengthType;
397 } else if (IsFastElementsKind(map->elements_kind())) {
398 field_type = type_cache_.kFixedArrayLengthType;
399 } else {
400 field_type = type_cache_.kJSArrayLengthType;
401 }
402 }
403 *access_info = PropertyAccessInfo::DataField(Type::Class(map, zone()),
404 field_index, field_type);
405 return true;
406 }
407 // Check for special JSArrayBufferView field accessors.
408 if (Accessors::IsJSArrayBufferViewFieldAccessor(map, name, &offset)) {
409 FieldIndex field_index = FieldIndex::ForInObjectOffset(offset);
410 Type* field_type = Type::Tagged();
411 if (Name::Equals(factory()->byte_length_string(), name) ||
412 Name::Equals(factory()->byte_offset_string(), name)) {
413 // The JSArrayBufferView::byte_length and JSArrayBufferView::byte_offset
414 // properties are always numbers in the range [0, kMaxSafeInteger].
415 field_type = type_cache_.kPositiveSafeInteger;
416 } else if (map->IsJSTypedArrayMap()) {
417 DCHECK(Name::Equals(factory()->length_string(), name));
418 // The JSTypedArray::length property is always a number in the range
419 // [0, kMaxSafeInteger].
420 field_type = type_cache_.kPositiveSafeInteger;
421 }
422 *access_info = PropertyAccessInfo::DataField(
423 Type::Class(map, zone()), field_index, field_type,
424 FieldCheck::kJSArrayBufferViewBufferNotNeutered);
425 return true;
426 }
427 return false;
428}
429
430
431bool AccessInfoFactory::LookupTransition(Handle<Map> map, Handle<Name> name,
432 MaybeHandle<JSObject> holder,
433 PropertyAccessInfo* access_info) {
434 // Check if the {map} has a data transition with the given {name}.
435 if (map->unused_property_fields() == 0) return false;
436 Handle<Map> transition_map;
437 if (TransitionArray::SearchTransition(map, kData, name, NONE)
438 .ToHandle(&transition_map)) {
439 int const number = transition_map->LastAdded();
440 PropertyDetails const details =
441 transition_map->instance_descriptors()->GetDetails(number);
442 // Don't bother optimizing stores to read-only properties.
443 if (details.IsReadOnly()) return false;
444 // TODO(bmeurer): Handle transition to data constant?
445 if (details.type() != DATA) return false;
446 int const index = details.field_index();
447 Representation field_representation = details.representation();
448 FieldIndex field_index = FieldIndex::ForPropertyIndex(
449 *transition_map, index, field_representation.IsDouble());
450 Type* field_type = Type::Tagged();
451 if (field_representation.IsSmi()) {
452 field_type = type_cache_.kSmi;
453 } else if (field_representation.IsDouble()) {
454 field_type = type_cache_.kFloat64;
455 } else if (field_representation.IsHeapObject()) {
456 // Extract the field type from the property details (make sure its
457 // representation is TaggedPointer to reflect the heap object case).
458 field_type = Type::Intersect(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100459 transition_map->instance_descriptors()->GetFieldType(number)->Convert(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000460 zone()),
461 Type::TaggedPointer(), zone());
462 if (field_type->Is(Type::None())) {
463 // Store is not safe if the field type was cleared.
464 return false;
465 } else if (!Type::Any()->Is(field_type)) {
466 // Add proper code dependencies in case of stable field map(s).
467 Handle<Map> field_owner_map(transition_map->FindFieldOwner(number),
468 isolate());
469 dependencies()->AssumeFieldType(field_owner_map);
470 }
471 DCHECK(field_type->Is(Type::TaggedPointer()));
472 }
473 dependencies()->AssumeMapNotDeprecated(transition_map);
474 *access_info = PropertyAccessInfo::DataField(
475 Type::Class(map, zone()), field_index, field_type, FieldCheck::kNone,
476 holder, transition_map);
477 return true;
478 }
479 return false;
480}
481
482
483Factory* AccessInfoFactory::factory() const { return isolate()->factory(); }
484
485} // namespace compiler
486} // namespace internal
487} // namespace v8