Commit 112f1973 authored by Michael Starzinger's avatar Michael Starzinger

Simplify interface to optimized code map lookup.

This is one step torwards extracting an OptimizedCodeMap out from the
SharedFunctionInfo in order to have a more flexible implementation.

R=bmeurer@chromium.org, jarin@chromium.org

Review URL: https://codereview.chromium.org/1205783003.

Cr-Commit-Position: refs/heads/master@{#29278}
parent 5056c821
......@@ -691,23 +691,14 @@ MUST_USE_RESULT static MaybeHandle<Code> GetCodeFromOptimizedCodeMap(
if (FLAG_cache_optimized_code) {
Handle<SharedFunctionInfo> shared(function->shared());
DisallowHeapAllocation no_gc;
int index = shared->SearchOptimizedCodeMap(
CodeAndLiterals cached = shared->SearchOptimizedCodeMap(
function->context()->native_context(), osr_ast_id);
if (index > 0) {
if (FLAG_trace_opt) {
PrintF("[found optimized code for ");
function->ShortPrint();
if (!osr_ast_id.IsNone()) {
PrintF(" at OSR AST id %d", osr_ast_id.ToInt());
}
PrintF("]\n");
}
FixedArray* literals = shared->GetLiteralsFromOptimizedCodeMap(index);
if (literals != NULL) function->set_literals(literals);
Code* code = shared->GetCodeFromOptimizedCodeMap(index);
DCHECK(!code->marked_for_deoptimization());
if (cached.code != nullptr) {
// Caching of optimized code enabled and optimized code found.
if (cached.literals != nullptr) function->set_literals(cached.literals);
DCHECK(!cached.code->marked_for_deoptimization());
DCHECK(function->shared()->is_compiled());
return Handle<Code>(code);
return Handle<Code>(cached.code);
}
}
return MaybeHandle<Code>();
......@@ -1424,6 +1415,14 @@ MaybeHandle<Code> Compiler::GetOptimizedCode(Handle<JSFunction> function,
Handle<Code> cached_code;
if (GetCodeFromOptimizedCodeMap(
function, osr_ast_id).ToHandle(&cached_code)) {
if (FLAG_trace_opt) {
PrintF("[found optimized code for ");
function->ShortPrint();
if (!osr_ast_id.IsNone()) {
PrintF(" at OSR AST id %d", osr_ast_id.ToInt());
}
PrintF("]\n");
}
return cached_code;
}
......@@ -1503,8 +1502,8 @@ Handle<Code> Compiler::GetConcurrentlyOptimizedCode(OptimizedCompileJob* job) {
job->RetryOptimization(kDebuggerHasBreakPoints);
} else if (job->GenerateCode() == OptimizedCompileJob::SUCCEEDED) {
RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info.get(), shared);
if (info->shared_info()->SearchOptimizedCodeMap(
info->context()->native_context(), info->osr_ast_id()) == -1) {
if (shared->SearchOptimizedCodeMap(info->context()->native_context(),
info->osr_ast_id()).code == nullptr) {
InsertCodeIntoOptimizedCodeMap(info.get());
}
if (FLAG_trace_opt) {
......
......@@ -1377,24 +1377,22 @@ Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
result->MarkForOptimization();
}
int index = info->SearchOptimizedCodeMap(context->native_context(),
BailoutId::None());
if (!info->bound() && index < 0) {
CodeAndLiterals cached = info->SearchOptimizedCodeMap(
context->native_context(), BailoutId::None());
if (cached.code != nullptr) {
// Caching of optimized code enabled and optimized code found.
if (cached.literals != nullptr) result->set_literals(cached.literals);
DCHECK(!cached.code->marked_for_deoptimization());
DCHECK(result->shared()->is_compiled());
result->ReplaceCode(cached.code);
}
if (cached.literals == nullptr && !info->bound()) {
int number_of_literals = info->num_literals();
Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
result->set_literals(*literals);
}
if (index > 0) {
// Caching of optimized code enabled and optimized code found.
FixedArray* literals = info->GetLiteralsFromOptimizedCodeMap(index);
if (literals != NULL) result->set_literals(literals);
Code* code = info->GetCodeFromOptimizedCodeMap(index);
DCHECK(!code->marked_for_deoptimization());
DCHECK(result->shared()->is_compiled());
result->ReplaceCode(code);
}
return result;
}
......
......@@ -9590,7 +9590,7 @@ void SharedFunctionInfo::AddToOptimizedCodeMap(
} else {
// Copy old map and append one new entry.
Handle<FixedArray> old_code_map = Handle<FixedArray>::cast(value);
DCHECK_EQ(-1, shared->SearchOptimizedCodeMap(*native_context, osr_ast_id));
DCHECK(!shared->SearchOptimizedCodeMap(*native_context, osr_ast_id).code);
old_length = old_code_map->length();
new_code_map = FixedArray::CopySize(
old_code_map, old_length + kEntryLength);
......@@ -9620,24 +9620,6 @@ void SharedFunctionInfo::AddToOptimizedCodeMap(
}
FixedArray* SharedFunctionInfo::GetLiteralsFromOptimizedCodeMap(int index) {
DCHECK(index > kEntriesStart);
FixedArray* code_map = FixedArray::cast(optimized_code_map());
FixedArray* cached_literals = FixedArray::cast(code_map->get(index + 1));
DCHECK_NOT_NULL(cached_literals);
return cached_literals;
}
Code* SharedFunctionInfo::GetCodeFromOptimizedCodeMap(int index) {
DCHECK(index > kEntriesStart);
FixedArray* code_map = FixedArray::cast(optimized_code_map());
Code* code = Code::cast(code_map->get(index));
DCHECK_NOT_NULL(code);
return code;
}
void SharedFunctionInfo::ClearOptimizedCodeMap() {
FixedArray* code_map = FixedArray::cast(optimized_code_map());
......@@ -10612,11 +10594,11 @@ void SharedFunctionInfo::ResetForNewContext(int new_ic_age) {
}
int SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context,
BailoutId osr_ast_id) {
CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap(
Context* native_context, BailoutId osr_ast_id) {
DisallowHeapAllocation no_gc;
DCHECK(native_context->IsNativeContext());
if (!FLAG_cache_optimized_code) return -1;
if (!FLAG_cache_optimized_code) return {nullptr, nullptr};
Object* value = optimized_code_map();
if (!value->IsSmi()) {
FixedArray* optimized_code_map = FixedArray::cast(value);
......@@ -10625,7 +10607,8 @@ int SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context,
for (int i = kEntriesStart; i < length; i += kEntryLength) {
if (optimized_code_map->get(i + kContextOffset) == native_context &&
optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) {
return i + kCachedCodeOffset;
return {Code::cast(optimized_code_map->get(i + kCachedCodeOffset)),
FixedArray::cast(optimized_code_map->get(i + kLiteralsOffset))};
}
}
if (FLAG_trace_opt) {
......@@ -10634,7 +10617,7 @@ int SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context,
PrintF("]\n");
}
}
return -1;
return {nullptr, nullptr};
}
......
......@@ -6558,6 +6558,14 @@ enum BuiltinFunctionId {
};
// Result of searching in an optimized code map of a SharedFunctionInfo. Note
// that {code == nullptr} indicates that no entry has been found.
struct CodeAndLiterals {
Code* code; // Cached optimized code.
FixedArray* literals; // Cached literals array.
};
// SharedFunctionInfo describes the JSFunction information that can be
// shared by multiple instances of the function.
class SharedFunctionInfo: public HeapObject {
......@@ -6573,16 +6581,10 @@ class SharedFunctionInfo: public HeapObject {
// and a shared literals array or Smi(0) if none.
DECL_ACCESSORS(optimized_code_map, Object)
// Returns index i of the entry with the specified context and OSR entry.
// At position i - 1 is the context, position i the code, and i + 1 the
// literals array. Returns -1 when no matching entry is found.
int SearchOptimizedCodeMap(Context* native_context, BailoutId osr_ast_id);
// Installs optimized code from the code map on the given closure. The
// index has to be consistent with a search result as defined above.
FixedArray* GetLiteralsFromOptimizedCodeMap(int index);
Code* GetCodeFromOptimizedCodeMap(int index);
// Returns entry from optimized code map for specified context and OSR entry.
// Note that {code == nullptr} indicates no matching entry has been found.
CodeAndLiterals SearchOptimizedCodeMap(Context* native_context,
BailoutId osr_ast_id);
// Clear optimized code map.
void ClearOptimizedCodeMap();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment