blob: d563e1dd8530c53abd987fbb3466c977e6683ea4 [file] [log] [blame]
Ben Murdoch014dc512016-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"
Ben Murdochf3b273f2017-01-17 12:11:28 +000010#include "src/compiler/type-cache.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000011#include "src/field-index-inl.h"
Ben Murdoch109988c2016-05-18 11:27:45 +010012#include "src/field-type.h"
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000013#include "src/ic/call-optimization.h"
Ben Murdoch13e2dad2016-09-16 13:49:30 +010014#include "src/objects-inl.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000015
16namespace v8 {
17namespace internal {
18namespace compiler {
19
20namespace {
21
22bool CanInlineElementAccess(Handle<Map> map) {
23 if (!map->IsJSObjectMap()) return false;
24 if (map->is_access_check_needed()) return false;
25 if (map->has_indexed_interceptor()) return false;
26 ElementsKind const elements_kind = map->elements_kind();
27 if (IsFastElementsKind(elements_kind)) return true;
Ben Murdochf91f0612016-11-29 16:50:11 +000028 if (IsFixedTypedArrayElementsKind(elements_kind)) return true;
Ben Murdoch014dc512016-03-22 12:00:34 +000029 return false;
30}
31
32
33bool CanInlinePropertyAccess(Handle<Map> map) {
34 // We can inline property access to prototypes of all primitives, except
35 // the special Oddball ones that have no wrapper counterparts (i.e. Null,
36 // Undefined and TheHole).
37 STATIC_ASSERT(ODDBALL_TYPE == LAST_PRIMITIVE_TYPE);
38 if (map->IsBooleanMap()) return true;
39 if (map->instance_type() < LAST_PRIMITIVE_TYPE) return true;
40 return map->IsJSObjectMap() && !map->is_dictionary_map() &&
41 !map->has_named_interceptor() &&
42 // TODO(verwaest): Whitelist contexts to which we have access.
43 !map->is_access_check_needed();
44}
45
46} // namespace
47
48
49std::ostream& operator<<(std::ostream& os, AccessMode access_mode) {
50 switch (access_mode) {
51 case AccessMode::kLoad:
52 return os << "Load";
53 case AccessMode::kStore:
54 return os << "Store";
Ben Murdoch62ed6312017-06-06 11:06:27 +010055 case AccessMode::kStoreInLiteral:
56 return os << "StoreInLiteral";
Ben Murdoch014dc512016-03-22 12:00:34 +000057 }
58 UNREACHABLE();
59 return os;
60}
61
Ben Murdochf91f0612016-11-29 16:50:11 +000062ElementAccessInfo::ElementAccessInfo() {}
63
64ElementAccessInfo::ElementAccessInfo(MapList const& receiver_maps,
65 ElementsKind elements_kind)
66 : elements_kind_(elements_kind), receiver_maps_(receiver_maps) {}
Ben Murdoch014dc512016-03-22 12:00:34 +000067
68// static
Ben Murdochf91f0612016-11-29 16:50:11 +000069PropertyAccessInfo PropertyAccessInfo::NotFound(MapList const& receiver_maps,
Ben Murdoch014dc512016-03-22 12:00:34 +000070 MaybeHandle<JSObject> holder) {
Ben Murdochf91f0612016-11-29 16:50:11 +000071 return PropertyAccessInfo(holder, receiver_maps);
Ben Murdoch014dc512016-03-22 12:00:34 +000072}
73
Ben Murdoch014dc512016-03-22 12:00:34 +000074// static
75PropertyAccessInfo PropertyAccessInfo::DataConstant(
Ben Murdochf91f0612016-11-29 16:50:11 +000076 MapList const& receiver_maps, Handle<Object> constant,
Ben Murdoch014dc512016-03-22 12:00:34 +000077 MaybeHandle<JSObject> holder) {
Ben Murdochf91f0612016-11-29 16:50:11 +000078 return PropertyAccessInfo(kDataConstant, holder, constant, receiver_maps);
Ben Murdoch014dc512016-03-22 12:00:34 +000079}
80
Ben Murdoch014dc512016-03-22 12:00:34 +000081// static
82PropertyAccessInfo PropertyAccessInfo::DataField(
Ben Murdoch62ed6312017-06-06 11:06:27 +010083 PropertyConstness constness, MapList const& receiver_maps,
84 FieldIndex field_index, MachineRepresentation field_representation,
85 Type* field_type, MaybeHandle<Map> field_map, MaybeHandle<JSObject> holder,
Ben Murdochf3b273f2017-01-17 12:11:28 +000086 MaybeHandle<Map> transition_map) {
Ben Murdoch62ed6312017-06-06 11:06:27 +010087 Kind kind = constness == kConst ? kDataConstantField : kDataField;
88 return PropertyAccessInfo(kind, holder, transition_map, field_index,
Ben Murdochf3b273f2017-01-17 12:11:28 +000089 field_representation, field_type, field_map,
Ben Murdochf91f0612016-11-29 16:50:11 +000090 receiver_maps);
Ben Murdoch014dc512016-03-22 12:00:34 +000091}
92
Ben Murdochf91f0612016-11-29 16:50:11 +000093// static
94PropertyAccessInfo PropertyAccessInfo::AccessorConstant(
95 MapList const& receiver_maps, Handle<Object> constant,
96 MaybeHandle<JSObject> holder) {
97 return PropertyAccessInfo(kAccessorConstant, holder, constant, receiver_maps);
98}
Ben Murdoch014dc512016-03-22 12:00:34 +000099
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000100// static
101PropertyAccessInfo PropertyAccessInfo::Generic(MapList const& receiver_maps) {
102 return PropertyAccessInfo(kGeneric, MaybeHandle<JSObject>(), Handle<Object>(),
103 receiver_maps);
104}
105
Ben Murdoch014dc512016-03-22 12:00:34 +0000106PropertyAccessInfo::PropertyAccessInfo()
Ben Murdochf3b273f2017-01-17 12:11:28 +0000107 : kind_(kInvalid),
108 field_representation_(MachineRepresentation::kNone),
109 field_type_(Type::None()) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000110
111PropertyAccessInfo::PropertyAccessInfo(MaybeHandle<JSObject> holder,
Ben Murdochf91f0612016-11-29 16:50:11 +0000112 MapList const& receiver_maps)
Ben Murdoch014dc512016-03-22 12:00:34 +0000113 : kind_(kNotFound),
Ben Murdochf91f0612016-11-29 16:50:11 +0000114 receiver_maps_(receiver_maps),
Ben Murdoch014dc512016-03-22 12:00:34 +0000115 holder_(holder),
Ben Murdochf3b273f2017-01-17 12:11:28 +0000116 field_representation_(MachineRepresentation::kNone),
Ben Murdochf91f0612016-11-29 16:50:11 +0000117 field_type_(Type::None()) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000118
Ben Murdochf91f0612016-11-29 16:50:11 +0000119PropertyAccessInfo::PropertyAccessInfo(Kind kind, MaybeHandle<JSObject> holder,
Ben Murdoch014dc512016-03-22 12:00:34 +0000120 Handle<Object> constant,
Ben Murdochf91f0612016-11-29 16:50:11 +0000121 MapList const& receiver_maps)
122 : kind_(kind),
123 receiver_maps_(receiver_maps),
Ben Murdoch014dc512016-03-22 12:00:34 +0000124 constant_(constant),
125 holder_(holder),
Ben Murdochf3b273f2017-01-17 12:11:28 +0000126 field_representation_(MachineRepresentation::kNone),
Ben Murdoch014dc512016-03-22 12:00:34 +0000127 field_type_(Type::Any()) {}
128
Ben Murdochf3b273f2017-01-17 12:11:28 +0000129PropertyAccessInfo::PropertyAccessInfo(
Ben Murdoch62ed6312017-06-06 11:06:27 +0100130 Kind kind, MaybeHandle<JSObject> holder, MaybeHandle<Map> transition_map,
Ben Murdochf3b273f2017-01-17 12:11:28 +0000131 FieldIndex field_index, MachineRepresentation field_representation,
132 Type* field_type, MaybeHandle<Map> field_map, MapList const& receiver_maps)
Ben Murdoch62ed6312017-06-06 11:06:27 +0100133 : kind_(kind),
Ben Murdochf91f0612016-11-29 16:50:11 +0000134 receiver_maps_(receiver_maps),
Ben Murdoch014dc512016-03-22 12:00:34 +0000135 transition_map_(transition_map),
136 holder_(holder),
137 field_index_(field_index),
Ben Murdochf3b273f2017-01-17 12:11:28 +0000138 field_representation_(field_representation),
139 field_type_(field_type),
140 field_map_(field_map) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000141
Ben Murdochf91f0612016-11-29 16:50:11 +0000142bool PropertyAccessInfo::Merge(PropertyAccessInfo const* that) {
143 if (this->kind_ != that->kind_) return false;
144 if (this->holder_.address() != that->holder_.address()) return false;
145
146 switch (this->kind_) {
147 case kInvalid:
148 break;
149
Ben Murdoch62ed6312017-06-06 11:06:27 +0100150 case kDataField:
151 case kDataConstantField: {
Ben Murdochf91f0612016-11-29 16:50:11 +0000152 // Check if we actually access the same field.
Ben Murdoch62ed6312017-06-06 11:06:27 +0100153 if (this->kind_ == that->kind_ &&
154 this->transition_map_.address() == that->transition_map_.address() &&
Ben Murdochf91f0612016-11-29 16:50:11 +0000155 this->field_index_ == that->field_index_ &&
Ben Murdoch62ed6312017-06-06 11:06:27 +0100156 this->field_map_.address() == that->field_map_.address() &&
Ben Murdochf91f0612016-11-29 16:50:11 +0000157 this->field_type_->Is(that->field_type_) &&
Ben Murdochf3b273f2017-01-17 12:11:28 +0000158 that->field_type_->Is(this->field_type_) &&
159 this->field_representation_ == that->field_representation_) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000160 this->receiver_maps_.insert(this->receiver_maps_.end(),
161 that->receiver_maps_.begin(),
162 that->receiver_maps_.end());
163 return true;
164 }
165 return false;
166 }
167
168 case kDataConstant:
169 case kAccessorConstant: {
170 // Check if we actually access the same constant.
171 if (this->constant_.address() == that->constant_.address()) {
172 this->receiver_maps_.insert(this->receiver_maps_.end(),
173 that->receiver_maps_.begin(),
174 that->receiver_maps_.end());
175 return true;
176 }
177 return false;
178 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100179
180 case kNotFound:
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000181 case kGeneric: {
182 this->receiver_maps_.insert(this->receiver_maps_.end(),
183 that->receiver_maps_.begin(),
184 that->receiver_maps_.end());
185 return true;
186 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000187 }
188
189 UNREACHABLE();
190 return false;
191}
192
Ben Murdoch014dc512016-03-22 12:00:34 +0000193AccessInfoFactory::AccessInfoFactory(CompilationDependencies* dependencies,
194 Handle<Context> native_context, Zone* zone)
195 : dependencies_(dependencies),
196 native_context_(native_context),
197 isolate_(native_context->GetIsolate()),
198 type_cache_(TypeCache::Get()),
199 zone_(zone) {
200 DCHECK(native_context->IsNativeContext());
201}
202
203
204bool AccessInfoFactory::ComputeElementAccessInfo(
205 Handle<Map> map, AccessMode access_mode, ElementAccessInfo* access_info) {
206 // Check if it is safe to inline element access for the {map}.
207 if (!CanInlineElementAccess(map)) return false;
Ben Murdoch014dc512016-03-22 12:00:34 +0000208 ElementsKind const elements_kind = map->elements_kind();
Ben Murdochf91f0612016-11-29 16:50:11 +0000209 *access_info = ElementAccessInfo(MapList{map}, elements_kind);
Ben Murdoch014dc512016-03-22 12:00:34 +0000210 return true;
211}
212
213
214bool AccessInfoFactory::ComputeElementAccessInfos(
215 MapHandleList const& maps, AccessMode access_mode,
216 ZoneVector<ElementAccessInfo>* access_infos) {
217 // Collect possible transition targets.
218 MapHandleList possible_transition_targets(maps.length());
219 for (Handle<Map> map : maps) {
220 if (Map::TryUpdate(map).ToHandle(&map)) {
221 if (CanInlineElementAccess(map) &&
222 IsFastElementsKind(map->elements_kind()) &&
223 GetInitialFastElementsKind() != map->elements_kind()) {
224 possible_transition_targets.Add(map);
225 }
226 }
227 }
228
229 // Separate the actual receiver maps and the possible transition sources.
230 MapHandleList receiver_maps(maps.length());
231 MapTransitionList transitions(maps.length());
232 for (Handle<Map> map : maps) {
233 if (Map::TryUpdate(map).ToHandle(&map)) {
Bryan Ferrisc0626e32019-03-05 17:18:50 -0800234 Map* transition_target = map->is_stable() ?
235 nullptr :
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100236 map->FindElementsKindTransitionedMap(&possible_transition_targets);
237 if (transition_target == nullptr) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000238 receiver_maps.Add(map);
239 } else {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100240 transitions.push_back(std::make_pair(map, handle(transition_target)));
Ben Murdoch014dc512016-03-22 12:00:34 +0000241 }
242 }
243 }
244
245 for (Handle<Map> receiver_map : receiver_maps) {
246 // Compute the element access information.
247 ElementAccessInfo access_info;
248 if (!ComputeElementAccessInfo(receiver_map, access_mode, &access_info)) {
249 return false;
250 }
251
252 // Collect the possible transitions for the {receiver_map}.
253 for (auto transition : transitions) {
254 if (transition.second.is_identical_to(receiver_map)) {
255 access_info.transitions().push_back(transition);
256 }
257 }
258
259 // Schedule the access information.
260 access_infos->push_back(access_info);
261 }
262 return true;
263}
264
265
266bool AccessInfoFactory::ComputePropertyAccessInfo(
267 Handle<Map> map, Handle<Name> name, AccessMode access_mode,
268 PropertyAccessInfo* access_info) {
269 // Check if it is safe to inline property access for the {map}.
270 if (!CanInlinePropertyAccess(map)) return false;
271
272 // Compute the receiver type.
273 Handle<Map> receiver_map = map;
274
Ben Murdoch109988c2016-05-18 11:27:45 +0100275 // Property lookups require the name to be internalized.
276 name = isolate()->factory()->InternalizeName(name);
277
Ben Murdoch014dc512016-03-22 12:00:34 +0000278 // We support fast inline cases for certain JSObject getters.
279 if (access_mode == AccessMode::kLoad &&
280 LookupSpecialFieldAccessor(map, name, access_info)) {
281 return true;
282 }
283
284 MaybeHandle<JSObject> holder;
285 do {
286 // Lookup the named property on the {map}.
287 Handle<DescriptorArray> descriptors(map->instance_descriptors(), isolate());
Ben Murdoch109988c2016-05-18 11:27:45 +0100288 int const number = descriptors->SearchWithCache(isolate(), *name, *map);
Ben Murdoch014dc512016-03-22 12:00:34 +0000289 if (number != DescriptorArray::kNotFound) {
290 PropertyDetails const details = descriptors->GetDetails(number);
Ben Murdoch62ed6312017-06-06 11:06:27 +0100291 if (access_mode == AccessMode::kStore ||
292 access_mode == AccessMode::kStoreInLiteral) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000293 // Don't bother optimizing stores to read-only properties.
294 if (details.IsReadOnly()) {
295 return false;
296 }
297 // Check for store to data property on a prototype.
298 if (details.kind() == kData && !holder.is_null()) {
299 // Store to property not found on the receiver but on a prototype, we
300 // need to transition to a new data property.
301 // Implemented according to ES6 section 9.1.9 [[Set]] (P, V, Receiver)
302 return LookupTransition(receiver_map, name, holder, access_info);
303 }
304 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100305 if (details.location() == kField) {
306 if (details.kind() == kData) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000307 int index = descriptors->GetFieldIndex(number);
Ben Murdochf3b273f2017-01-17 12:11:28 +0000308 Representation details_representation = details.representation();
Ben Murdochf91f0612016-11-29 16:50:11 +0000309 FieldIndex field_index = FieldIndex::ForPropertyIndex(
Ben Murdochf3b273f2017-01-17 12:11:28 +0000310 *map, index, details_representation.IsDouble());
311 Type* field_type = Type::NonInternal();
312 MachineRepresentation field_representation =
313 MachineRepresentation::kTagged;
314 MaybeHandle<Map> field_map;
315 if (details_representation.IsSmi()) {
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000316 field_type = Type::SignedSmall();
Ben Murdochf3b273f2017-01-17 12:11:28 +0000317 field_representation = MachineRepresentation::kTaggedSigned;
318 } else if (details_representation.IsDouble()) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000319 field_type = type_cache_.kFloat64;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000320 field_representation = MachineRepresentation::kFloat64;
321 } else if (details_representation.IsHeapObject()) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000322 // Extract the field type from the property details (make sure its
323 // representation is TaggedPointer to reflect the heap object case).
Ben Murdochf3b273f2017-01-17 12:11:28 +0000324 field_representation = MachineRepresentation::kTaggedPointer;
325 Handle<FieldType> descriptors_field_type(
326 descriptors->GetFieldType(number), isolate());
327 if (descriptors_field_type->IsNone()) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000328 // Store is not safe if the field type was cleared.
329 if (access_mode == AccessMode::kStore) return false;
330
331 // The field type was cleared by the GC, so we don't know anything
332 // about the contents now.
Ben Murdochf3b273f2017-01-17 12:11:28 +0000333 } else if (descriptors_field_type->IsClass()) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000334 // Add proper code dependencies in case of stable field map(s).
335 Handle<Map> field_owner_map(map->FindFieldOwner(number),
336 isolate());
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000337 dependencies()->AssumeFieldOwner(field_owner_map);
Ben Murdochf3b273f2017-01-17 12:11:28 +0000338
339 // Remember the field map, and try to infer a useful type.
340 field_type = Type::For(descriptors_field_type->AsClass());
341 field_map = descriptors_field_type->AsClass();
Ben Murdochf91f0612016-11-29 16:50:11 +0000342 }
343 }
344 *access_info = PropertyAccessInfo::DataField(
Ben Murdoch62ed6312017-06-06 11:06:27 +0100345 details.constness(), MapList{receiver_map}, field_index,
346 field_representation, field_type, field_map, holder);
Ben Murdochf91f0612016-11-29 16:50:11 +0000347 return true;
Ben Murdoch62ed6312017-06-06 11:06:27 +0100348 } else {
349 DCHECK_EQ(kAccessor, details.kind());
350 // TODO(turbofan): Add support for general accessors?
351 return false;
Ben Murdochf91f0612016-11-29 16:50:11 +0000352 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100353
354 } else {
355 DCHECK_EQ(kDescriptor, details.location());
356 if (details.kind() == kData) {
357 DCHECK(!FLAG_track_constant_fields);
358 *access_info = PropertyAccessInfo::DataConstant(
359 MapList{receiver_map},
360 handle(descriptors->GetValue(number), isolate()), holder);
361 return true;
362 } else {
363 DCHECK_EQ(kAccessor, details.kind());
Ben Murdochf91f0612016-11-29 16:50:11 +0000364 Handle<Object> accessors(descriptors->GetValue(number), isolate());
365 if (!accessors->IsAccessorPair()) return false;
366 Handle<Object> accessor(
367 access_mode == AccessMode::kLoad
368 ? Handle<AccessorPair>::cast(accessors)->getter()
369 : Handle<AccessorPair>::cast(accessors)->setter(),
370 isolate());
371 if (!accessor->IsJSFunction()) {
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000372 CallOptimization optimization(accessor);
373 if (!optimization.is_simple_api_call()) {
374 return false;
375 }
376 if (optimization.api_call_info()->fast_handler()->IsCode()) {
377 return false;
378 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100379 if (V8_UNLIKELY(FLAG_runtime_stats)) return false;
380 }
381 if (access_mode == AccessMode::kLoad) {
382 Handle<Name> cached_property_name;
383 if (FunctionTemplateInfo::TryGetCachedPropertyName(isolate(),
384 accessor)
385 .ToHandle(&cached_property_name)) {
386 if (ComputePropertyAccessInfo(map, cached_property_name,
387 access_mode, access_info)) {
388 return true;
389 }
390 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000391 }
392 *access_info = PropertyAccessInfo::AccessorConstant(
393 MapList{receiver_map}, accessor, holder);
394 return true;
395 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000396 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000397 UNREACHABLE();
398 return false;
Ben Murdoch014dc512016-03-22 12:00:34 +0000399 }
400
401 // Don't search on the prototype chain for special indices in case of
402 // integer indexed exotic objects (see ES6 section 9.4.5).
403 if (map->IsJSTypedArrayMap() && name->IsString() &&
404 IsSpecialIndex(isolate()->unicode_cache(), String::cast(*name))) {
405 return false;
406 }
407
Ben Murdoch62ed6312017-06-06 11:06:27 +0100408 // Don't search on the prototype when storing in literals
409 if (access_mode == AccessMode::kStoreInLiteral) {
410 return LookupTransition(receiver_map, name, holder, access_info);
411 }
412
Ben Murdoch014dc512016-03-22 12:00:34 +0000413 // Don't lookup private symbols on the prototype chain.
414 if (name->IsPrivate()) return false;
415
416 // Walk up the prototype chain.
417 if (!map->prototype()->IsJSObject()) {
418 // Perform the implicit ToObject for primitives here.
419 // Implemented according to ES6 section 7.3.2 GetV (V, P).
420 Handle<JSFunction> constructor;
421 if (Map::GetConstructorFunction(map, native_context())
422 .ToHandle(&constructor)) {
423 map = handle(constructor->initial_map(), isolate());
424 DCHECK(map->prototype()->IsJSObject());
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100425 } else if (map->prototype()->IsNull(isolate())) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000426 // Store to property not found on the receiver or any prototype, we need
427 // to transition to a new data property.
428 // Implemented according to ES6 section 9.1.9 [[Set]] (P, V, Receiver)
429 if (access_mode == AccessMode::kStore) {
430 return LookupTransition(receiver_map, name, holder, access_info);
431 }
432 // The property was not found, return undefined or throw depending
433 // on the language mode of the load operation.
434 // Implemented according to ES6 section 9.1.8 [[Get]] (P, Receiver)
Ben Murdochf91f0612016-11-29 16:50:11 +0000435 *access_info =
436 PropertyAccessInfo::NotFound(MapList{receiver_map}, holder);
Ben Murdoch014dc512016-03-22 12:00:34 +0000437 return true;
438 } else {
439 return false;
440 }
441 }
442 Handle<JSObject> map_prototype(JSObject::cast(map->prototype()), isolate());
443 if (map_prototype->map()->is_deprecated()) {
444 // Try to migrate the prototype object so we don't embed the deprecated
445 // map into the optimized code.
446 JSObject::TryMigrateInstance(map_prototype);
447 }
448 map = handle(map_prototype->map(), isolate());
449 holder = map_prototype;
450 } while (CanInlinePropertyAccess(map));
451 return false;
452}
453
Ben Murdoch014dc512016-03-22 12:00:34 +0000454bool AccessInfoFactory::ComputePropertyAccessInfos(
455 MapHandleList const& maps, Handle<Name> name, AccessMode access_mode,
456 ZoneVector<PropertyAccessInfo>* access_infos) {
457 for (Handle<Map> map : maps) {
458 if (Map::TryUpdate(map).ToHandle(&map)) {
459 PropertyAccessInfo access_info;
460 if (!ComputePropertyAccessInfo(map, name, access_mode, &access_info)) {
461 return false;
462 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000463 // Try to merge the {access_info} with an existing one.
464 bool merged = false;
465 for (PropertyAccessInfo& other_info : *access_infos) {
466 if (other_info.Merge(&access_info)) {
467 merged = true;
468 break;
469 }
470 }
471 if (!merged) access_infos->push_back(access_info);
Ben Murdoch014dc512016-03-22 12:00:34 +0000472 }
473 }
474 return true;
475}
476
477
478bool AccessInfoFactory::LookupSpecialFieldAccessor(
479 Handle<Map> map, Handle<Name> name, PropertyAccessInfo* access_info) {
480 // Check for special JSObject field accessors.
481 int offset;
482 if (Accessors::IsJSObjectFieldAccessor(map, name, &offset)) {
483 FieldIndex field_index = FieldIndex::ForInObjectOffset(offset);
Ben Murdochf3b273f2017-01-17 12:11:28 +0000484 Type* field_type = Type::NonInternal();
485 MachineRepresentation field_representation = MachineRepresentation::kTagged;
Ben Murdoch014dc512016-03-22 12:00:34 +0000486 if (map->IsStringMap()) {
487 DCHECK(Name::Equals(factory()->length_string(), name));
488 // The String::length property is always a smi in the range
489 // [0, String::kMaxLength].
490 field_type = type_cache_.kStringLengthType;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000491 field_representation = MachineRepresentation::kTaggedSigned;
Ben Murdoch014dc512016-03-22 12:00:34 +0000492 } else if (map->IsJSArrayMap()) {
493 DCHECK(Name::Equals(factory()->length_string(), name));
494 // The JSArray::length property is a smi in the range
495 // [0, FixedDoubleArray::kMaxLength] in case of fast double
496 // elements, a smi in the range [0, FixedArray::kMaxLength]
497 // in case of other fast elements, and [0, kMaxUInt32] in
498 // case of other arrays.
499 if (IsFastDoubleElementsKind(map->elements_kind())) {
500 field_type = type_cache_.kFixedDoubleArrayLengthType;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000501 field_representation = MachineRepresentation::kTaggedSigned;
Ben Murdoch014dc512016-03-22 12:00:34 +0000502 } else if (IsFastElementsKind(map->elements_kind())) {
503 field_type = type_cache_.kFixedArrayLengthType;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000504 field_representation = MachineRepresentation::kTaggedSigned;
Ben Murdoch014dc512016-03-22 12:00:34 +0000505 } else {
506 field_type = type_cache_.kJSArrayLengthType;
507 }
508 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100509 // Special fields are always mutable.
Ben Murdochf3b273f2017-01-17 12:11:28 +0000510 *access_info = PropertyAccessInfo::DataField(
Ben Murdoch62ed6312017-06-06 11:06:27 +0100511 kMutable, MapList{map}, field_index, field_representation, field_type);
Ben Murdoch014dc512016-03-22 12:00:34 +0000512 return true;
513 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000514 return false;
515}
516
517
518bool AccessInfoFactory::LookupTransition(Handle<Map> map, Handle<Name> name,
519 MaybeHandle<JSObject> holder,
520 PropertyAccessInfo* access_info) {
521 // Check if the {map} has a data transition with the given {name}.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000522 if (map->unused_property_fields() == 0) {
523 *access_info = PropertyAccessInfo::Generic(MapList{map});
524 return true;
525 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000526 Handle<Map> transition_map;
527 if (TransitionArray::SearchTransition(map, kData, name, NONE)
528 .ToHandle(&transition_map)) {
529 int const number = transition_map->LastAdded();
530 PropertyDetails const details =
531 transition_map->instance_descriptors()->GetDetails(number);
532 // Don't bother optimizing stores to read-only properties.
533 if (details.IsReadOnly()) return false;
534 // TODO(bmeurer): Handle transition to data constant?
Ben Murdoch62ed6312017-06-06 11:06:27 +0100535 if (details.location() != kField) return false;
Ben Murdoch014dc512016-03-22 12:00:34 +0000536 int const index = details.field_index();
Ben Murdochf3b273f2017-01-17 12:11:28 +0000537 Representation details_representation = details.representation();
Ben Murdoch014dc512016-03-22 12:00:34 +0000538 FieldIndex field_index = FieldIndex::ForPropertyIndex(
Ben Murdochf3b273f2017-01-17 12:11:28 +0000539 *transition_map, index, details_representation.IsDouble());
540 Type* field_type = Type::NonInternal();
541 MaybeHandle<Map> field_map;
542 MachineRepresentation field_representation = MachineRepresentation::kTagged;
543 if (details_representation.IsSmi()) {
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000544 field_type = Type::SignedSmall();
Ben Murdochf3b273f2017-01-17 12:11:28 +0000545 field_representation = MachineRepresentation::kTaggedSigned;
546 } else if (details_representation.IsDouble()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000547 field_type = type_cache_.kFloat64;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000548 field_representation = MachineRepresentation::kFloat64;
549 } else if (details_representation.IsHeapObject()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000550 // Extract the field type from the property details (make sure its
551 // representation is TaggedPointer to reflect the heap object case).
Ben Murdochf3b273f2017-01-17 12:11:28 +0000552 field_representation = MachineRepresentation::kTaggedPointer;
553 Handle<FieldType> descriptors_field_type(
554 transition_map->instance_descriptors()->GetFieldType(number),
555 isolate());
556 if (descriptors_field_type->IsNone()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000557 // Store is not safe if the field type was cleared.
558 return false;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000559 } else if (descriptors_field_type->IsClass()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000560 // Add proper code dependencies in case of stable field map(s).
561 Handle<Map> field_owner_map(transition_map->FindFieldOwner(number),
562 isolate());
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000563 dependencies()->AssumeFieldOwner(field_owner_map);
Ben Murdochf3b273f2017-01-17 12:11:28 +0000564
565 // Remember the field map, and try to infer a useful type.
566 field_type = Type::For(descriptors_field_type->AsClass());
567 field_map = descriptors_field_type->AsClass();
Ben Murdoch014dc512016-03-22 12:00:34 +0000568 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000569 }
570 dependencies()->AssumeMapNotDeprecated(transition_map);
Ben Murdoch62ed6312017-06-06 11:06:27 +0100571 // Transitioning stores are never stores to constant fields.
Ben Murdochf91f0612016-11-29 16:50:11 +0000572 *access_info = PropertyAccessInfo::DataField(
Ben Murdoch62ed6312017-06-06 11:06:27 +0100573 kMutable, MapList{map}, field_index, field_representation, field_type,
574 field_map, holder, transition_map);
Ben Murdoch014dc512016-03-22 12:00:34 +0000575 return true;
576 }
577 return false;
578}
579
580
581Factory* AccessInfoFactory::factory() const { return isolate()->factory(); }
582
583} // namespace compiler
584} // namespace internal
585} // namespace v8