| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/objects.h" | 5 #include "src/objects.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <iomanip> | 8 #include <iomanip> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <sstream> | 10 #include <sstream> |
| (...skipping 12521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12532 isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent)); | 12532 isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent)); |
| 12533 // No write barrier required, since the builtin is part of the root set. | 12533 // No write barrier required, since the builtin is part of the root set. |
| 12534 if (FLAG_mark_shared_functions_for_tier_up) { | 12534 if (FLAG_mark_shared_functions_for_tier_up) { |
| 12535 // TODO(leszeks): The compilation isn't concurrent if we trigger it using | 12535 // TODO(leszeks): The compilation isn't concurrent if we trigger it using |
| 12536 // this bit. | 12536 // this bit. |
| 12537 shared()->set_marked_for_tier_up(true); | 12537 shared()->set_marked_for_tier_up(true); |
| 12538 } | 12538 } |
| 12539 } | 12539 } |
| 12540 | 12540 |
| 12541 // static | 12541 // static |
| 12542 Handle<LiteralsArray> SharedFunctionInfo::FindOrCreateLiterals( | |
| 12543 Handle<SharedFunctionInfo> shared, Handle<Context> native_context) { | |
| 12544 Isolate* isolate = shared->GetIsolate(); | |
| 12545 CodeAndLiterals result = | |
| 12546 shared->SearchOptimizedCodeMap(*native_context, BailoutId::None()); | |
| 12547 if (result.literals != nullptr) { | |
| 12548 DCHECK(shared->feedback_metadata()->is_empty() || | |
| 12549 !result.literals->feedback_vector()->is_empty()); | |
| 12550 return handle(result.literals, isolate); | |
| 12551 } | |
| 12552 | |
| 12553 Handle<TypeFeedbackVector> feedback_vector = | |
| 12554 TypeFeedbackVector::New(isolate, handle(shared->feedback_metadata())); | |
| 12555 Handle<LiteralsArray> literals = | |
| 12556 LiteralsArray::New(isolate, feedback_vector, shared->num_literals()); | |
| 12557 Handle<Code> code; | |
| 12558 if (result.code != nullptr) { | |
| 12559 code = Handle<Code>(result.code, isolate); | |
| 12560 } | |
| 12561 AddToOptimizedCodeMap(shared, native_context, code, literals, | |
| 12562 BailoutId::None()); | |
| 12563 return literals; | |
| 12564 } | |
| 12565 | |
| 12566 // static | |
| 12567 void SharedFunctionInfo::AddToOptimizedCodeMap( | 12542 void SharedFunctionInfo::AddToOptimizedCodeMap( |
| 12568 Handle<SharedFunctionInfo> shared, Handle<Context> native_context, | 12543 Handle<SharedFunctionInfo> shared, Handle<Context> native_context, |
| 12569 MaybeHandle<Code> code, Handle<LiteralsArray> literals, | 12544 Handle<Code> code, BailoutId osr_ast_id) { |
| 12570 BailoutId osr_ast_id) { | |
| 12571 Isolate* isolate = shared->GetIsolate(); | 12545 Isolate* isolate = shared->GetIsolate(); |
| 12572 if (isolate->serializer_enabled()) return; | 12546 if (isolate->serializer_enabled()) return; |
| 12573 DCHECK(code.is_null() || | 12547 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 12574 code.ToHandleChecked()->kind() == Code::OPTIMIZED_FUNCTION); | |
| 12575 DCHECK(native_context->IsNativeContext()); | 12548 DCHECK(native_context->IsNativeContext()); |
| 12576 STATIC_ASSERT(kEntryLength == 3); | 12549 STATIC_ASSERT(kEntryLength == 2); |
| 12577 Handle<FixedArray> new_code_map; | 12550 Handle<FixedArray> new_code_map; |
| 12578 int entry; | 12551 int entry; |
| 12579 | 12552 |
| 12580 if (!osr_ast_id.IsNone()) { | 12553 if (!osr_ast_id.IsNone()) { |
| 12581 Context::AddToOptimizedCodeMap( | 12554 Context::AddToOptimizedCodeMap(native_context, shared, code, osr_ast_id); |
| 12582 native_context, shared, code.ToHandleChecked(), literals, osr_ast_id); | |
| 12583 return; | 12555 return; |
| 12584 } | 12556 } |
| 12585 | 12557 |
| 12586 DCHECK(osr_ast_id.IsNone()); | 12558 DCHECK(osr_ast_id.IsNone()); |
| 12587 if (shared->OptimizedCodeMapIsCleared()) { | 12559 if (shared->OptimizedCodeMapIsCleared()) { |
| 12588 new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED); | 12560 new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED); |
| 12589 entry = kEntriesStart; | 12561 entry = kEntriesStart; |
| 12590 } else { | 12562 } else { |
| 12591 Handle<FixedArray> old_code_map(shared->optimized_code_map(), isolate); | 12563 Handle<FixedArray> old_code_map(shared->optimized_code_map(), isolate); |
| 12592 entry = shared->SearchOptimizedCodeMapEntry(*native_context); | 12564 entry = shared->SearchOptimizedCodeMapEntry(*native_context); |
| 12593 if (entry >= kEntriesStart) { | 12565 if (entry >= kEntriesStart) { |
| 12594 // Just set the code and literals of the entry. | 12566 // Just set the code of the entry. |
| 12595 if (!code.is_null()) { | 12567 Handle<WeakCell> code_cell = isolate->factory()->NewWeakCell(code); |
| 12596 Handle<WeakCell> code_cell = | 12568 old_code_map->set(entry + kCachedCodeOffset, *code_cell); |
| 12597 isolate->factory()->NewWeakCell(code.ToHandleChecked()); | |
| 12598 old_code_map->set(entry + kCachedCodeOffset, *code_cell); | |
| 12599 } | |
| 12600 Handle<WeakCell> literals_cell = | |
| 12601 isolate->factory()->NewWeakCell(literals); | |
| 12602 old_code_map->set(entry + kLiteralsOffset, *literals_cell); | |
| 12603 return; | 12569 return; |
| 12604 } | 12570 } |
| 12605 | 12571 |
| 12606 // Can we reuse an entry? | 12572 // Can we reuse an entry? |
| 12607 DCHECK(entry < kEntriesStart); | 12573 DCHECK(entry < kEntriesStart); |
| 12608 int length = old_code_map->length(); | 12574 int length = old_code_map->length(); |
| 12609 for (int i = kEntriesStart; i < length; i += kEntryLength) { | 12575 for (int i = kEntriesStart; i < length; i += kEntryLength) { |
| 12610 if (WeakCell::cast(old_code_map->get(i + kContextOffset))->cleared()) { | 12576 if (WeakCell::cast(old_code_map->get(i + kContextOffset))->cleared()) { |
| 12611 new_code_map = old_code_map; | 12577 new_code_map = old_code_map; |
| 12612 entry = i; | 12578 entry = i; |
| 12613 break; | 12579 break; |
| 12614 } | 12580 } |
| 12615 } | 12581 } |
| 12616 | 12582 |
| 12617 if (entry < kEntriesStart) { | 12583 if (entry < kEntriesStart) { |
| 12618 // Copy old optimized code map and append one new entry. | 12584 // Copy old optimized code map and append one new entry. |
| 12619 new_code_map = isolate->factory()->CopyFixedArrayAndGrow( | 12585 new_code_map = isolate->factory()->CopyFixedArrayAndGrow( |
| 12620 old_code_map, kEntryLength, TENURED); | 12586 old_code_map, kEntryLength, TENURED); |
| 12621 // TODO(mstarzinger): Temporary workaround. The allocation above might | 12587 // TODO(mstarzinger): Temporary workaround. The allocation above might |
| 12622 // have flushed the optimized code map and the copy we created is full of | 12588 // have flushed the optimized code map and the copy we created is full of |
| 12623 // holes. For now we just give up on adding the entry and pretend it got | 12589 // holes. For now we just give up on adding the entry and pretend it got |
| 12624 // flushed. | 12590 // flushed. |
| 12625 if (shared->OptimizedCodeMapIsCleared()) return; | 12591 if (shared->OptimizedCodeMapIsCleared()) return; |
| 12626 entry = old_code_map->length(); | 12592 entry = old_code_map->length(); |
| 12627 } | 12593 } |
| 12628 } | 12594 } |
| 12629 | 12595 |
| 12630 Handle<WeakCell> code_cell = | 12596 Handle<WeakCell> code_cell = isolate->factory()->NewWeakCell(code); |
| 12631 code.is_null() ? isolate->factory()->empty_weak_cell() | |
| 12632 : isolate->factory()->NewWeakCell(code.ToHandleChecked()); | |
| 12633 Handle<WeakCell> literals_cell = isolate->factory()->NewWeakCell(literals); | |
| 12634 WeakCell* context_cell = native_context->self_weak_cell(); | 12597 WeakCell* context_cell = native_context->self_weak_cell(); |
| 12635 | 12598 |
| 12636 new_code_map->set(entry + kContextOffset, context_cell); | 12599 new_code_map->set(entry + kContextOffset, context_cell); |
| 12637 new_code_map->set(entry + kCachedCodeOffset, *code_cell); | 12600 new_code_map->set(entry + kCachedCodeOffset, *code_cell); |
| 12638 new_code_map->set(entry + kLiteralsOffset, *literals_cell); | |
| 12639 | 12601 |
| 12640 #ifdef DEBUG | 12602 #ifdef DEBUG |
| 12641 for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) { | 12603 for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) { |
| 12642 WeakCell* cell = WeakCell::cast(new_code_map->get(i + kContextOffset)); | 12604 WeakCell* cell = WeakCell::cast(new_code_map->get(i + kContextOffset)); |
| 12643 DCHECK(cell->cleared() || cell->value()->IsNativeContext()); | 12605 DCHECK(cell->cleared() || cell->value()->IsNativeContext()); |
| 12644 cell = WeakCell::cast(new_code_map->get(i + kCachedCodeOffset)); | 12606 cell = WeakCell::cast(new_code_map->get(i + kCachedCodeOffset)); |
| 12645 DCHECK(cell->cleared() || | 12607 DCHECK(cell->cleared() || |
| 12646 (cell->value()->IsCode() && | 12608 (cell->value()->IsCode() && |
| 12647 Code::cast(cell->value())->kind() == Code::OPTIMIZED_FUNCTION)); | 12609 Code::cast(cell->value())->kind() == Code::OPTIMIZED_FUNCTION)); |
| 12648 cell = WeakCell::cast(new_code_map->get(i + kLiteralsOffset)); | |
| 12649 DCHECK(cell->cleared() || cell->value()->IsFixedArray()); | |
| 12650 } | 12610 } |
| 12651 #endif | 12611 #endif |
| 12652 | 12612 |
| 12653 FixedArray* old_code_map = shared->optimized_code_map(); | 12613 FixedArray* old_code_map = shared->optimized_code_map(); |
| 12654 if (old_code_map != *new_code_map) { | 12614 if (old_code_map != *new_code_map) { |
| 12655 shared->set_optimized_code_map(*new_code_map); | 12615 shared->set_optimized_code_map(*new_code_map); |
| 12656 } | 12616 } |
| 12657 } | 12617 } |
| 12658 | 12618 |
| 12659 | 12619 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 12677 DCHECK(WeakCell::cast(code_map->get(src))->cleared() || | 12637 DCHECK(WeakCell::cast(code_map->get(src))->cleared() || |
| 12678 WeakCell::cast(code_map->get(src))->value()->IsNativeContext()); | 12638 WeakCell::cast(code_map->get(src))->value()->IsNativeContext()); |
| 12679 found = WeakCell::cast(code_map->get(src + kCachedCodeOffset))->value() == | 12639 found = WeakCell::cast(code_map->get(src + kCachedCodeOffset))->value() == |
| 12680 optimized_code; | 12640 optimized_code; |
| 12681 if (found) { | 12641 if (found) { |
| 12682 if (FLAG_trace_opt) { | 12642 if (FLAG_trace_opt) { |
| 12683 PrintF("[evicting entry from optimizing code map (%s) for ", reason); | 12643 PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
| 12684 ShortPrint(); | 12644 ShortPrint(); |
| 12685 PrintF("]\n"); | 12645 PrintF("]\n"); |
| 12686 } | 12646 } |
| 12687 // Just clear the code in order to continue sharing literals. | 12647 // Just clear the code. |
| 12688 code_map->set(src + kCachedCodeOffset, heap->empty_weak_cell(), | 12648 code_map->set(src + kCachedCodeOffset, heap->empty_weak_cell(), |
| 12689 SKIP_WRITE_BARRIER); | 12649 SKIP_WRITE_BARRIER); |
| 12690 } | 12650 } |
| 12691 } | 12651 } |
| 12692 } | 12652 } |
| 12693 | 12653 |
| 12694 if (!found) { | 12654 if (!found) { |
| 12695 // We didn't find the code in here. It must be osr'd code. | 12655 // We didn't find the code in here. It must be osr'd code. |
| 12696 isolate->EvictOSROptimizedCode(optimized_code, reason); | 12656 isolate->EvictOSROptimizedCode(optimized_code, reason); |
| 12697 } | 12657 } |
| 12698 } | 12658 } |
| 12699 | 12659 |
| 12700 // static | 12660 // static |
| 12701 void JSFunction::EnsureLiterals(Handle<JSFunction> function) { | 12661 void JSFunction::EnsureLiterals(Handle<JSFunction> function) { |
| 12702 Handle<SharedFunctionInfo> shared(function->shared()); | 12662 Handle<SharedFunctionInfo> shared(function->shared()); |
| 12703 Handle<Context> native_context(function->context()->native_context()); | 12663 Handle<Context> native_context(function->context()->native_context()); |
| 12704 if (function->literals() == | 12664 Isolate* isolate = shared->GetIsolate(); |
| 12705 function->GetIsolate()->heap()->empty_literals_array()) { | 12665 |
| 12706 Handle<LiteralsArray> literals = | 12666 if (!function->has_literals_array()) { |
| 12707 SharedFunctionInfo::FindOrCreateLiterals(shared, native_context); | 12667 if (FLAG_trace_strong_rooted_literals) { |
| 12708 function->set_literals(*literals); | 12668 PrintF("EnsureLiterals: Installing literals array in %s %p\n", |
| 12669 shared->DebugName()->ToCString().get(), |
| 12670 reinterpret_cast<void*>(*function)); |
| 12671 } |
| 12672 // Top level code didn't get it's literals installed. |
| 12673 Handle<TypeFeedbackVector> feedback_vector = |
| 12674 TypeFeedbackVector::New(isolate, handle(shared->feedback_metadata())); |
| 12675 Handle<LiteralsArray> new_literals = |
| 12676 LiteralsArray::New(isolate, feedback_vector, shared->num_literals()); |
| 12677 function->set_literals(*new_literals); |
| 12678 } else if (!function->literals()->has_feedback_vector()) { |
| 12679 if (FLAG_trace_strong_rooted_literals) { |
| 12680 PrintF("EnsureLiterals: Installing feedback vector in %s %p\n", |
| 12681 shared->DebugName()->ToCString().get(), |
| 12682 reinterpret_cast<void*>(*function)); |
| 12683 } |
| 12684 // If the feedback vector hasn't been installed, do that. |
| 12685 Handle<TypeFeedbackVector> feedback_vector = TypeFeedbackVector::New( |
| 12686 shared->GetIsolate(), handle(shared->feedback_metadata())); |
| 12687 function->literals()->set_feedback_vector(*feedback_vector); |
| 12688 } else { |
| 12689 if (FLAG_trace_strong_rooted_literals) { |
| 12690 PrintF("EnsureLiterals: did nothing for %s %p\n", |
| 12691 shared->DebugName()->ToCString().get(), |
| 12692 reinterpret_cast<void*>(*function)); |
| 12693 } |
| 12709 } | 12694 } |
| 12695 |
| 12696 // No matter what, ensure some post-conditions. |
| 12697 DCHECK(shared->feedback_metadata()->slot_count() != 0 || |
| 12698 function->feedback_vector() == |
| 12699 shared->GetIsolate()->heap()->empty_type_feedback_vector()); |
| 12700 DCHECK(shared->num_literals() == 0 || |
| 12701 function->literals() != |
| 12702 shared->GetIsolate()->heap()->empty_literals_array()); |
| 12710 } | 12703 } |
| 12711 | 12704 |
| 12712 static void GetMinInobjectSlack(Map* map, void* data) { | 12705 static void GetMinInobjectSlack(Map* map, void* data) { |
| 12713 int slack = map->unused_property_fields(); | 12706 int slack = map->unused_property_fields(); |
| 12714 if (*reinterpret_cast<int*>(data) > slack) { | 12707 if (*reinterpret_cast<int*>(data) > slack) { |
| 12715 *reinterpret_cast<int*>(data) = slack; | 12708 *reinterpret_cast<int*>(data) = slack; |
| 12716 } | 12709 } |
| 12717 } | 12710 } |
| 12718 | 12711 |
| 12719 | 12712 |
| (...skipping 1549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 14269 FixedArray* optimized_code_map = this->optimized_code_map(); | 14262 FixedArray* optimized_code_map = this->optimized_code_map(); |
| 14270 int length = optimized_code_map->length(); | 14263 int length = optimized_code_map->length(); |
| 14271 WeakCell* empty_weak_cell = GetHeap()->empty_weak_cell(); | 14264 WeakCell* empty_weak_cell = GetHeap()->empty_weak_cell(); |
| 14272 for (int i = kEntriesStart; i < length; i += kEntryLength) { | 14265 for (int i = kEntriesStart; i < length; i += kEntryLength) { |
| 14273 optimized_code_map->set(i + kCachedCodeOffset, empty_weak_cell, | 14266 optimized_code_map->set(i + kCachedCodeOffset, empty_weak_cell, |
| 14274 SKIP_WRITE_BARRIER); | 14267 SKIP_WRITE_BARRIER); |
| 14275 } | 14268 } |
| 14276 } | 14269 } |
| 14277 } | 14270 } |
| 14278 | 14271 |
| 14279 CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap( | 14272 Code* SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context, |
| 14280 Context* native_context, BailoutId osr_ast_id) { | 14273 BailoutId osr_ast_id) { |
| 14281 CodeAndLiterals result = {nullptr, nullptr}; | 14274 Code* result = nullptr; |
| 14282 if (!osr_ast_id.IsNone()) { | 14275 if (!osr_ast_id.IsNone()) { |
| 14283 Code* code; | 14276 return native_context->SearchOptimizedCodeMap(this, osr_ast_id); |
| 14284 LiteralsArray* literals; | |
| 14285 native_context->SearchOptimizedCodeMap(this, osr_ast_id, &code, &literals); | |
| 14286 result = {code, literals}; | |
| 14287 return result; | |
| 14288 } | 14277 } |
| 14289 | 14278 |
| 14290 DCHECK(osr_ast_id.IsNone()); | 14279 DCHECK(osr_ast_id.IsNone()); |
| 14291 int entry = SearchOptimizedCodeMapEntry(native_context); | 14280 int entry = SearchOptimizedCodeMapEntry(native_context); |
| 14292 if (entry != kNotFound) { | 14281 if (entry != kNotFound) { |
| 14293 FixedArray* code_map = optimized_code_map(); | 14282 FixedArray* code_map = optimized_code_map(); |
| 14294 DCHECK_LE(entry + kEntryLength, code_map->length()); | 14283 DCHECK_LE(entry + kEntryLength, code_map->length()); |
| 14295 WeakCell* cell = WeakCell::cast(code_map->get(entry + kCachedCodeOffset)); | 14284 WeakCell* cell = WeakCell::cast(code_map->get(entry + kCachedCodeOffset)); |
| 14296 WeakCell* literals_cell = | |
| 14297 WeakCell::cast(code_map->get(entry + kLiteralsOffset)); | |
| 14298 | 14285 |
| 14299 result = {cell->cleared() ? nullptr : Code::cast(cell->value()), | 14286 result = cell->cleared() ? nullptr : Code::cast(cell->value()); |
| 14300 literals_cell->cleared() ? nullptr : LiteralsArray::cast( | |
| 14301 literals_cell->value())}; | |
| 14302 } | 14287 } |
| 14303 return result; | 14288 return result; |
| 14304 } | 14289 } |
| 14305 | 14290 |
| 14306 | 14291 |
| 14307 #define DECLARE_TAG(ignore1, name, ignore2) name, | 14292 #define DECLARE_TAG(ignore1, name, ignore2) name, |
| 14308 const char* const VisitorSynchronization::kTags[ | 14293 const char* const VisitorSynchronization::kTags[ |
| 14309 VisitorSynchronization::kNumberOfSyncTags] = { | 14294 VisitorSynchronization::kNumberOfSyncTags] = { |
| 14310 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) | 14295 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 14311 }; | 14296 }; |
| (...skipping 6179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 20491 // depend on this. | 20476 // depend on this. |
| 20492 return DICTIONARY_ELEMENTS; | 20477 return DICTIONARY_ELEMENTS; |
| 20493 } | 20478 } |
| 20494 DCHECK_LE(kind, LAST_ELEMENTS_KIND); | 20479 DCHECK_LE(kind, LAST_ELEMENTS_KIND); |
| 20495 return kind; | 20480 return kind; |
| 20496 } | 20481 } |
| 20497 } | 20482 } |
| 20498 | 20483 |
| 20499 } // namespace internal | 20484 } // namespace internal |
| 20500 } // namespace v8 | 20485 } // namespace v8 |
| OLD | NEW |