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