2 * Copyright (C) 2008-2017 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "CodeBlock.h"
33 #include "ArithProfile.h"
34 #include "BasicBlockLocation.h"
35 #include "BytecodeDumper.h"
36 #include "BytecodeGenerator.h"
37 #include "BytecodeLivenessAnalysis.h"
38 #include "BytecodeUseDef.h"
39 #include "CallLinkStatus.h"
40 #include "CodeBlockSet.h"
41 #include "DFGCapabilities.h"
42 #include "DFGCommon.h"
43 #include "DFGDriver.h"
44 #include "DFGJITCode.h"
45 #include "DFGWorklist.h"
47 #include "EvalCodeBlock.h"
48 #include "FullCodeOrigin.h"
49 #include "FunctionCodeBlock.h"
50 #include "FunctionExecutableDump.h"
51 #include "GetPutInfo.h"
52 #include "InlineCallFrame.h"
53 #include "InterpreterInlines.h"
55 #include "JITMathIC.h"
56 #include "JSCInlines.h"
57 #include "JSCJSValue.h"
58 #include "JSFunction.h"
59 #include "JSLexicalEnvironment.h"
60 #include "JSModuleEnvironment.h"
63 #include "JSTemplateRegistryKey.h"
64 #include "LLIntData.h"
65 #include "LLIntEntrypoint.h"
66 #include "LLIntPrototypeLoadAdaptiveStructureWatchpoint.h"
67 #include "LowLevelInterpreter.h"
68 #include "ModuleProgramCodeBlock.h"
69 #include "PCToCodeOriginMap.h"
70 #include "PolymorphicAccess.h"
71 #include "ProfilerDatabase.h"
72 #include "ProgramCodeBlock.h"
73 #include "ReduceWhitespace.h"
75 #include "SlotVisitorInlines.h"
76 #include "StackVisitor.h"
77 #include "StructureStubInfo.h"
78 #include "TypeLocationCache.h"
79 #include "TypeProfiler.h"
80 #include "UnlinkedInstructionStream.h"
81 #include "VMInlines.h"
82 #include <wtf/BagToHashMap.h>
83 #include <wtf/CommaPrinter.h>
84 #include <wtf/SimpleStats.h>
85 #include <wtf/StringExtras.h>
86 #include <wtf/StringPrintStream.h>
87 #include <wtf/text/UniquedStringImpl.h>
90 #include "RegisterAtOffsetList.h"
94 #include "DFGOperations.h"
98 #include "FTLJITCode.h"
103 const ClassInfo CodeBlock::s_info = {
105 CREATE_METHOD_TABLE(CodeBlock)
108 CString CodeBlock::inferredName() const
110 switch (codeType()) {
116 return jsCast<FunctionExecutable*>(ownerExecutable())->inferredName().utf8();
121 return CString("", 0);
125 bool CodeBlock::hasHash() const
130 bool CodeBlock::isSafeToComputeHash() const
132 return !isCompilationThread();
135 CodeBlockHash CodeBlock::hash() const
138 RELEASE_ASSERT(isSafeToComputeHash());
139 m_hash = CodeBlockHash(ownerScriptExecutable()->source(), specializationKind());
144 CString CodeBlock::sourceCodeForTools() const
146 if (codeType() != FunctionCode)
147 return ownerScriptExecutable()->source().toUTF8();
149 SourceProvider* provider = source();
150 FunctionExecutable* executable = jsCast<FunctionExecutable*>(ownerExecutable());
151 UnlinkedFunctionExecutable* unlinked = executable->unlinkedExecutable();
152 unsigned unlinkedStartOffset = unlinked->startOffset();
153 unsigned linkedStartOffset = executable->source().startOffset();
154 int delta = linkedStartOffset - unlinkedStartOffset;
155 unsigned rangeStart = delta + unlinked->unlinkedFunctionNameStart();
156 unsigned rangeEnd = delta + unlinked->startOffset() + unlinked->sourceLength();
159 provider->source().substring(rangeStart, rangeEnd - rangeStart).utf8());
162 CString CodeBlock::sourceCodeOnOneLine() const
164 return reduceWhitespace(sourceCodeForTools());
167 CString CodeBlock::hashAsStringIfPossible() const
169 if (hasHash() || isSafeToComputeHash())
170 return toCString(hash());
174 void CodeBlock::dumpAssumingJITType(PrintStream& out, JITCode::JITType jitType) const
176 out.print(inferredName(), "#", hashAsStringIfPossible());
177 out.print(":[", RawPointer(this), "->");
179 out.print(RawPointer(alternative()), "->");
180 out.print(RawPointer(ownerExecutable()), ", ", jitType, codeType());
182 if (codeType() == FunctionCode)
183 out.print(specializationKind());
184 out.print(", ", instructionCount());
185 if (this->jitType() == JITCode::BaselineJIT && m_shouldAlwaysBeInlined)
186 out.print(" (ShouldAlwaysBeInlined)");
187 if (ownerScriptExecutable()->neverInline())
188 out.print(" (NeverInline)");
189 if (ownerScriptExecutable()->neverOptimize())
190 out.print(" (NeverOptimize)");
191 else if (ownerScriptExecutable()->neverFTLOptimize())
192 out.print(" (NeverFTLOptimize)");
193 if (ownerScriptExecutable()->didTryToEnterInLoop())
194 out.print(" (DidTryToEnterInLoop)");
195 if (ownerScriptExecutable()->isStrictMode())
196 out.print(" (StrictMode)");
197 if (m_didFailJITCompilation)
198 out.print(" (JITFail)");
199 if (this->jitType() == JITCode::BaselineJIT && m_didFailFTLCompilation)
200 out.print(" (FTLFail)");
201 if (this->jitType() == JITCode::BaselineJIT && m_hasBeenCompiledWithFTL)
202 out.print(" (HadFTLReplacement)");
206 void CodeBlock::dump(PrintStream& out) const
208 dumpAssumingJITType(out, jitType());
211 void CodeBlock::dumpSource()
213 dumpSource(WTF::dataFile());
216 void CodeBlock::dumpSource(PrintStream& out)
218 ScriptExecutable* executable = ownerScriptExecutable();
219 if (executable->isFunctionExecutable()) {
220 FunctionExecutable* functionExecutable = reinterpret_cast<FunctionExecutable*>(executable);
221 StringView source = functionExecutable->source().provider()->getRange(
222 functionExecutable->parametersStartOffset(),
223 functionExecutable->typeProfilingEndOffset() + 1); // Type profiling end offset is the character before the '}'.
225 out.print("function ", inferredName(), source);
228 out.print(executable->source().view());
231 void CodeBlock::dumpBytecode()
233 dumpBytecode(WTF::dataFile());
236 void CodeBlock::dumpBytecode(PrintStream& out)
238 StubInfoMap stubInfos;
239 CallLinkInfoMap callLinkInfos;
240 getStubInfoMap(stubInfos);
241 getCallLinkInfoMap(callLinkInfos);
242 BytecodeDumper<CodeBlock>::dumpBlock(this, instructions(), out, stubInfos, callLinkInfos);
245 void CodeBlock::dumpBytecode(PrintStream& out, const Instruction* begin, const Instruction*& it, const StubInfoMap& stubInfos, const CallLinkInfoMap& callLinkInfos)
247 BytecodeDumper<CodeBlock>::dumpBytecode(this, out, begin, it, stubInfos, callLinkInfos);
250 void CodeBlock::dumpBytecode(
251 PrintStream& out, unsigned bytecodeOffset,
252 const StubInfoMap& stubInfos, const CallLinkInfoMap& callLinkInfos)
254 const Instruction* it = instructions().begin() + bytecodeOffset;
255 dumpBytecode(out, instructions().begin(), it, stubInfos, callLinkInfos);
258 #define FOR_EACH_MEMBER_VECTOR(macro) \
259 macro(instructions) \
260 macro(callLinkInfos) \
261 macro(linkedCallerList) \
263 macro(functionExpressions) \
264 macro(constantRegisters)
267 static size_t sizeInBytes(const Vector<T>& vector)
269 return vector.capacity() * sizeof(T);
274 class PutToScopeFireDetail : public FireDetail {
276 PutToScopeFireDetail(CodeBlock* codeBlock, const Identifier& ident)
277 : m_codeBlock(codeBlock)
282 void dump(PrintStream& out) const override
284 out.print("Linking put_to_scope in ", FunctionExecutableDump(jsCast<FunctionExecutable*>(m_codeBlock->ownerExecutable())), " for ", m_ident);
288 CodeBlock* m_codeBlock;
289 const Identifier& m_ident;
292 } // anonymous namespace
294 CodeBlock::CodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, CodeBlock& other)
295 : JSCell(*vm, structure)
296 , m_globalObject(other.m_globalObject)
297 , m_numCalleeLocals(other.m_numCalleeLocals)
298 , m_numVars(other.m_numVars)
299 , m_shouldAlwaysBeInlined(true)
301 , m_capabilityLevelState(DFG::CapabilityLevelNotSet)
303 , m_didFailJITCompilation(false)
304 , m_didFailFTLCompilation(false)
305 , m_hasBeenCompiledWithFTL(false)
306 , m_isConstructor(other.m_isConstructor)
307 , m_isStrictMode(other.m_isStrictMode)
308 , m_codeType(other.m_codeType)
309 , m_unlinkedCode(*other.m_vm, this, other.m_unlinkedCode.get())
310 , m_numberOfArgumentsToSkip(other.m_numberOfArgumentsToSkip)
311 , m_hasDebuggerStatement(false)
312 , m_steppingMode(SteppingModeDisabled)
313 , m_numBreakpoints(0)
314 , m_ownerExecutable(*other.m_vm, this, other.m_ownerExecutable.get())
316 , m_instructions(other.m_instructions)
317 , m_thisRegister(other.m_thisRegister)
318 , m_scopeRegister(other.m_scopeRegister)
319 , m_hash(other.m_hash)
320 , m_source(other.m_source)
321 , m_sourceOffset(other.m_sourceOffset)
322 , m_firstLineColumnOffset(other.m_firstLineColumnOffset)
323 , m_constantRegisters(other.m_constantRegisters)
324 , m_constantsSourceCodeRepresentation(other.m_constantsSourceCodeRepresentation)
325 , m_functionDecls(other.m_functionDecls)
326 , m_functionExprs(other.m_functionExprs)
327 , m_osrExitCounter(0)
328 , m_optimizationDelayCounter(0)
329 , m_reoptimizationRetryCounter(0)
330 , m_creationTime(std::chrono::steady_clock::now())
332 m_visitWeaklyHasBeenCalled = false;
334 ASSERT(heap()->isDeferred());
335 ASSERT(m_scopeRegister.isLocal());
337 setNumParameters(other.numParameters());
340 void CodeBlock::finishCreation(VM& vm, CopyParsedBlockTag, CodeBlock& other)
342 Base::finishCreation(vm);
344 optimizeAfterWarmUp();
347 if (other.m_rareData) {
348 createRareDataIfNecessary();
350 m_rareData->m_exceptionHandlers = other.m_rareData->m_exceptionHandlers;
351 m_rareData->m_constantBuffers = other.m_rareData->m_constantBuffers;
352 m_rareData->m_switchJumpTables = other.m_rareData->m_switchJumpTables;
353 m_rareData->m_stringSwitchJumpTables = other.m_rareData->m_stringSwitchJumpTables;
356 heap()->m_codeBlocks->add(this);
359 CodeBlock::CodeBlock(VM* vm, Structure* structure, ScriptExecutable* ownerExecutable, UnlinkedCodeBlock* unlinkedCodeBlock,
360 JSScope* scope, RefPtr<SourceProvider>&& sourceProvider, unsigned sourceOffset, unsigned firstLineColumnOffset)
361 : JSCell(*vm, structure)
362 , m_globalObject(scope->globalObject()->vm(), this, scope->globalObject())
363 , m_numCalleeLocals(unlinkedCodeBlock->m_numCalleeLocals)
364 , m_numVars(unlinkedCodeBlock->m_numVars)
365 , m_shouldAlwaysBeInlined(true)
367 , m_capabilityLevelState(DFG::CapabilityLevelNotSet)
369 , m_didFailJITCompilation(false)
370 , m_didFailFTLCompilation(false)
371 , m_hasBeenCompiledWithFTL(false)
372 , m_isConstructor(unlinkedCodeBlock->isConstructor())
373 , m_isStrictMode(unlinkedCodeBlock->isStrictMode())
374 , m_codeType(unlinkedCodeBlock->codeType())
375 , m_unlinkedCode(m_globalObject->vm(), this, unlinkedCodeBlock)
376 , m_hasDebuggerStatement(false)
377 , m_steppingMode(SteppingModeDisabled)
378 , m_numBreakpoints(0)
379 , m_ownerExecutable(m_globalObject->vm(), this, ownerExecutable)
380 , m_vm(unlinkedCodeBlock->vm())
381 , m_thisRegister(unlinkedCodeBlock->thisRegister())
382 , m_scopeRegister(unlinkedCodeBlock->scopeRegister())
383 , m_source(WTFMove(sourceProvider))
384 , m_sourceOffset(sourceOffset)
385 , m_firstLineColumnOffset(firstLineColumnOffset)
386 , m_osrExitCounter(0)
387 , m_optimizationDelayCounter(0)
388 , m_reoptimizationRetryCounter(0)
389 , m_creationTime(std::chrono::steady_clock::now())
391 m_visitWeaklyHasBeenCalled = false;
393 ASSERT(heap()->isDeferred());
394 ASSERT(m_scopeRegister.isLocal());
397 setNumParameters(unlinkedCodeBlock->numParameters());
400 bool CodeBlock::finishCreation(VM& vm, ScriptExecutable* ownerExecutable, UnlinkedCodeBlock* unlinkedCodeBlock,
403 Base::finishCreation(vm);
405 if (vm.typeProfiler() || vm.controlFlowProfiler())
406 vm.functionHasExecutedCache()->removeUnexecutedRange(ownerExecutable->sourceID(), ownerExecutable->typeProfilingStartOffset(), ownerExecutable->typeProfilingEndOffset());
408 if (!setConstantRegisters(unlinkedCodeBlock->constantRegisters(), unlinkedCodeBlock->constantsSourceCodeRepresentation()))
410 if (!setConstantIdentifierSetRegisters(vm, unlinkedCodeBlock->constantIdentifierSets()))
412 if (unlinkedCodeBlock->usesGlobalObject())
413 m_constantRegisters[unlinkedCodeBlock->globalObjectRegister().toConstantIndex()].set(*m_vm, this, m_globalObject.get());
415 for (unsigned i = 0; i < LinkTimeConstantCount; i++) {
416 LinkTimeConstant type = static_cast<LinkTimeConstant>(i);
417 if (unsigned registerIndex = unlinkedCodeBlock->registerIndexForLinkTimeConstant(type))
418 m_constantRegisters[registerIndex].set(*m_vm, this, m_globalObject->jsCellForLinkTimeConstant(type));
421 // We already have the cloned symbol table for the module environment since we need to instantiate
422 // the module environments before linking the code block. We replace the stored symbol table with the already cloned one.
423 if (UnlinkedModuleProgramCodeBlock* unlinkedModuleProgramCodeBlock = jsDynamicCast<UnlinkedModuleProgramCodeBlock*>(vm, unlinkedCodeBlock)) {
424 SymbolTable* clonedSymbolTable = jsCast<ModuleProgramExecutable*>(ownerExecutable)->moduleEnvironmentSymbolTable();
425 if (m_vm->typeProfiler()) {
426 ConcurrentJSLocker locker(clonedSymbolTable->m_lock);
427 clonedSymbolTable->prepareForTypeProfiling(locker);
429 replaceConstant(unlinkedModuleProgramCodeBlock->moduleEnvironmentSymbolTableConstantRegisterOffset(), clonedSymbolTable);
432 bool shouldUpdateFunctionHasExecutedCache = vm.typeProfiler() || vm.controlFlowProfiler();
433 m_functionDecls = RefCountedArray<WriteBarrier<FunctionExecutable>>(unlinkedCodeBlock->numberOfFunctionDecls());
434 for (size_t count = unlinkedCodeBlock->numberOfFunctionDecls(), i = 0; i < count; ++i) {
435 UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionDecl(i);
436 if (shouldUpdateFunctionHasExecutedCache)
437 vm.functionHasExecutedCache()->insertUnexecutedRange(ownerExecutable->sourceID(), unlinkedExecutable->typeProfilingStartOffset(), unlinkedExecutable->typeProfilingEndOffset());
438 m_functionDecls[i].set(*m_vm, this, unlinkedExecutable->link(*m_vm, ownerExecutable->source()));
441 m_functionExprs = RefCountedArray<WriteBarrier<FunctionExecutable>>(unlinkedCodeBlock->numberOfFunctionExprs());
442 for (size_t count = unlinkedCodeBlock->numberOfFunctionExprs(), i = 0; i < count; ++i) {
443 UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionExpr(i);
444 if (shouldUpdateFunctionHasExecutedCache)
445 vm.functionHasExecutedCache()->insertUnexecutedRange(ownerExecutable->sourceID(), unlinkedExecutable->typeProfilingStartOffset(), unlinkedExecutable->typeProfilingEndOffset());
446 m_functionExprs[i].set(*m_vm, this, unlinkedExecutable->link(*m_vm, ownerExecutable->source()));
449 if (unlinkedCodeBlock->hasRareData()) {
450 createRareDataIfNecessary();
451 if (size_t count = unlinkedCodeBlock->constantBufferCount()) {
452 m_rareData->m_constantBuffers.grow(count);
453 for (size_t i = 0; i < count; i++) {
454 const UnlinkedCodeBlock::ConstantBuffer& buffer = unlinkedCodeBlock->constantBuffer(i);
455 m_rareData->m_constantBuffers[i] = buffer;
458 if (size_t count = unlinkedCodeBlock->numberOfExceptionHandlers()) {
459 m_rareData->m_exceptionHandlers.resizeToFit(count);
460 for (size_t i = 0; i < count; i++) {
461 const UnlinkedHandlerInfo& unlinkedHandler = unlinkedCodeBlock->exceptionHandler(i);
462 HandlerInfo& handler = m_rareData->m_exceptionHandlers[i];
464 handler.initialize(unlinkedHandler, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(LLInt::getCodePtr(op_catch))));
466 handler.initialize(unlinkedHandler);
471 if (size_t count = unlinkedCodeBlock->numberOfStringSwitchJumpTables()) {
472 m_rareData->m_stringSwitchJumpTables.grow(count);
473 for (size_t i = 0; i < count; i++) {
474 UnlinkedStringJumpTable::StringOffsetTable::iterator ptr = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.begin();
475 UnlinkedStringJumpTable::StringOffsetTable::iterator end = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.end();
476 for (; ptr != end; ++ptr) {
477 OffsetLocation offset;
478 offset.branchOffset = ptr->value.branchOffset;
479 m_rareData->m_stringSwitchJumpTables[i].offsetTable.add(ptr->key, offset);
484 if (size_t count = unlinkedCodeBlock->numberOfSwitchJumpTables()) {
485 m_rareData->m_switchJumpTables.grow(count);
486 for (size_t i = 0; i < count; i++) {
487 UnlinkedSimpleJumpTable& sourceTable = unlinkedCodeBlock->switchJumpTable(i);
488 SimpleJumpTable& destTable = m_rareData->m_switchJumpTables[i];
489 destTable.branchOffsets = sourceTable.branchOffsets;
490 destTable.min = sourceTable.min;
495 // Allocate metadata buffers for the bytecode
496 if (size_t size = unlinkedCodeBlock->numberOfLLintCallLinkInfos())
497 m_llintCallLinkInfos = RefCountedArray<LLIntCallLinkInfo>(size);
498 if (size_t size = unlinkedCodeBlock->numberOfArrayProfiles())
499 m_arrayProfiles.grow(size);
500 if (size_t size = unlinkedCodeBlock->numberOfArrayAllocationProfiles())
501 m_arrayAllocationProfiles = RefCountedArray<ArrayAllocationProfile>(size);
502 if (size_t size = unlinkedCodeBlock->numberOfValueProfiles())
503 m_valueProfiles = RefCountedArray<ValueProfile>(size);
504 if (size_t size = unlinkedCodeBlock->numberOfObjectAllocationProfiles())
505 m_objectAllocationProfiles = RefCountedArray<ObjectAllocationProfile>(size);
508 setCalleeSaveRegisters(RegisterSet::llintBaselineCalleeSaveRegisters());
511 // Copy and translate the UnlinkedInstructions
512 unsigned instructionCount = unlinkedCodeBlock->instructions().count();
513 UnlinkedInstructionStream::Reader instructionReader(unlinkedCodeBlock->instructions());
515 // Bookkeep the strongly referenced module environments.
516 HashSet<JSModuleEnvironment*> stronglyReferencedModuleEnvironments;
518 RefCountedArray<Instruction> instructions(instructionCount);
520 unsigned valueProfileCount = 0;
521 auto linkValueProfile = [&](unsigned bytecodeOffset, unsigned opLength) {
522 unsigned valueProfileIndex = valueProfileCount++;
523 ValueProfile* profile = &m_valueProfiles[valueProfileIndex];
524 ASSERT(profile->m_bytecodeOffset == -1);
525 profile->m_bytecodeOffset = bytecodeOffset;
526 instructions[bytecodeOffset + opLength - 1] = profile;
529 for (unsigned i = 0; !instructionReader.atEnd(); ) {
530 const UnlinkedInstruction* pc = instructionReader.next();
532 unsigned opLength = opcodeLength(pc[0].u.opcode);
534 instructions[i] = vm.interpreter->getOpcode(pc[0].u.opcode);
535 for (size_t j = 1; j < opLength; ++j) {
536 if (sizeof(int32_t) != sizeof(intptr_t))
537 instructions[i + j].u.pointer = 0;
538 instructions[i + j].u.operand = pc[j].u.operand;
540 switch (pc[0].u.opcode) {
541 case op_has_indexed_property: {
542 int arrayProfileIndex = pc[opLength - 1].u.operand;
543 m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
545 instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
548 case op_call_varargs:
549 case op_tail_call_varargs:
550 case op_tail_call_forward_arguments:
551 case op_construct_varargs:
552 case op_get_by_val: {
553 int arrayProfileIndex = pc[opLength - 2].u.operand;
554 m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
556 instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex];
559 case op_get_direct_pname:
561 case op_get_by_id_with_this:
562 case op_try_get_by_id:
563 case op_get_by_val_with_this:
564 case op_get_from_arguments:
566 case op_get_argument: {
567 linkValueProfile(i, opLength);
573 case op_put_by_val_direct: {
574 int arrayProfileIndex = pc[opLength - 1].u.operand;
575 m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
576 instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
581 case op_new_array_buffer:
582 case op_new_array_with_size: {
583 int arrayAllocationProfileIndex = pc[opLength - 1].u.operand;
584 instructions[i + opLength - 1] = &m_arrayAllocationProfiles[arrayAllocationProfileIndex];
587 case op_new_object: {
588 int objectAllocationProfileIndex = pc[opLength - 1].u.operand;
589 ObjectAllocationProfile* objectAllocationProfile = &m_objectAllocationProfiles[objectAllocationProfileIndex];
590 int inferredInlineCapacity = pc[opLength - 2].u.operand;
592 instructions[i + opLength - 1] = objectAllocationProfile;
593 objectAllocationProfile->initialize(vm,
594 m_globalObject.get(), this, m_globalObject->objectPrototype(), inferredInlineCapacity);
601 linkValueProfile(i, opLength);
602 int arrayProfileIndex = pc[opLength - 2].u.operand;
603 m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
604 instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex];
605 instructions[i + 5] = &m_llintCallLinkInfos[pc[5].u.operand];
609 instructions[i + 5] = &m_llintCallLinkInfos[pc[5].u.operand];
610 linkValueProfile(i, opLength);
613 case op_get_array_length:
616 case op_resolve_scope: {
617 const Identifier& ident = identifier(pc[3].u.operand);
618 ResolveType type = static_cast<ResolveType>(pc[4].u.operand);
619 RELEASE_ASSERT(type != LocalClosureVar);
620 int localScopeDepth = pc[5].u.operand;
622 ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Get, type, InitializationMode::NotInitialization);
623 instructions[i + 4].u.operand = op.type;
624 instructions[i + 5].u.operand = op.depth;
625 if (op.lexicalEnvironment) {
626 if (op.type == ModuleVar) {
627 // Keep the linked module environment strongly referenced.
628 if (stronglyReferencedModuleEnvironments.add(jsCast<JSModuleEnvironment*>(op.lexicalEnvironment)).isNewEntry)
629 addConstant(op.lexicalEnvironment);
630 instructions[i + 6].u.jsCell.set(vm, this, op.lexicalEnvironment);
632 instructions[i + 6].u.symbolTable.set(vm, this, op.lexicalEnvironment->symbolTable());
633 } else if (JSScope* constantScope = JSScope::constantScopeForCodeBlock(op.type, this))
634 instructions[i + 6].u.jsCell.set(vm, this, constantScope);
636 instructions[i + 6].u.pointer = nullptr;
640 case op_get_from_scope: {
641 linkValueProfile(i, opLength);
643 // get_from_scope dst, scope, id, GetPutInfo, Structure, Operand
645 int localScopeDepth = pc[5].u.operand;
646 instructions[i + 5].u.pointer = nullptr;
648 GetPutInfo getPutInfo = GetPutInfo(pc[4].u.operand);
649 ASSERT(!isInitialization(getPutInfo.initializationMode()));
650 if (getPutInfo.resolveType() == LocalClosureVar) {
651 instructions[i + 4] = GetPutInfo(getPutInfo.resolveMode(), ClosureVar, getPutInfo.initializationMode()).operand();
655 const Identifier& ident = identifier(pc[3].u.operand);
656 ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Get, getPutInfo.resolveType(), InitializationMode::NotInitialization);
658 instructions[i + 4].u.operand = GetPutInfo(getPutInfo.resolveMode(), op.type, getPutInfo.initializationMode()).operand();
659 if (op.type == ModuleVar)
660 instructions[i + 4].u.operand = GetPutInfo(getPutInfo.resolveMode(), ClosureVar, getPutInfo.initializationMode()).operand();
661 if (op.type == GlobalVar || op.type == GlobalVarWithVarInjectionChecks || op.type == GlobalLexicalVar || op.type == GlobalLexicalVarWithVarInjectionChecks)
662 instructions[i + 5].u.watchpointSet = op.watchpointSet;
663 else if (op.structure)
664 instructions[i + 5].u.structure.set(vm, this, op.structure);
665 instructions[i + 6].u.pointer = reinterpret_cast<void*>(op.operand);
669 case op_put_to_scope: {
670 // put_to_scope scope, id, value, GetPutInfo, Structure, Operand
671 GetPutInfo getPutInfo = GetPutInfo(pc[4].u.operand);
672 if (getPutInfo.resolveType() == LocalClosureVar) {
673 // Only do watching if the property we're putting to is not anonymous.
674 if (static_cast<unsigned>(pc[2].u.operand) != UINT_MAX) {
675 int symbolTableIndex = pc[5].u.operand;
676 SymbolTable* symbolTable = jsCast<SymbolTable*>(getConstant(symbolTableIndex));
677 const Identifier& ident = identifier(pc[2].u.operand);
678 ConcurrentJSLocker locker(symbolTable->m_lock);
679 auto iter = symbolTable->find(locker, ident.impl());
680 ASSERT(iter != symbolTable->end(locker));
681 iter->value.prepareToWatch();
682 instructions[i + 5].u.watchpointSet = iter->value.watchpointSet();
684 instructions[i + 5].u.watchpointSet = nullptr;
688 const Identifier& ident = identifier(pc[2].u.operand);
689 int localScopeDepth = pc[5].u.operand;
690 instructions[i + 5].u.pointer = nullptr;
691 ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Put, getPutInfo.resolveType(), getPutInfo.initializationMode());
693 instructions[i + 4].u.operand = GetPutInfo(getPutInfo.resolveMode(), op.type, getPutInfo.initializationMode()).operand();
694 if (op.type == GlobalVar || op.type == GlobalVarWithVarInjectionChecks || op.type == GlobalLexicalVar || op.type == GlobalLexicalVarWithVarInjectionChecks)
695 instructions[i + 5].u.watchpointSet = op.watchpointSet;
696 else if (op.type == ClosureVar || op.type == ClosureVarWithVarInjectionChecks) {
697 if (op.watchpointSet)
698 op.watchpointSet->invalidate(vm, PutToScopeFireDetail(this, ident));
699 } else if (op.structure)
700 instructions[i + 5].u.structure.set(vm, this, op.structure);
701 instructions[i + 6].u.pointer = reinterpret_cast<void*>(op.operand);
706 case op_profile_type: {
707 RELEASE_ASSERT(vm.typeProfiler());
708 // The format of this instruction is: op_profile_type regToProfile, TypeLocation*, flag, identifier?, resolveType?
709 size_t instructionOffset = i + opLength - 1;
710 unsigned divotStart, divotEnd;
711 GlobalVariableID globalVariableID = 0;
712 RefPtr<TypeSet> globalTypeSet;
713 bool shouldAnalyze = m_unlinkedCode->typeProfilerExpressionInfoForBytecodeOffset(instructionOffset, divotStart, divotEnd);
714 VirtualRegister profileRegister(pc[1].u.operand);
715 ProfileTypeBytecodeFlag flag = static_cast<ProfileTypeBytecodeFlag>(pc[3].u.operand);
716 SymbolTable* symbolTable = nullptr;
719 case ProfileTypeBytecodeClosureVar: {
720 const Identifier& ident = identifier(pc[4].u.operand);
721 int localScopeDepth = pc[2].u.operand;
722 ResolveType type = static_cast<ResolveType>(pc[5].u.operand);
723 // Even though type profiling may be profiling either a Get or a Put, we can always claim a Get because
724 // we're abstractly "read"ing from a JSScope.
725 ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Get, type, InitializationMode::NotInitialization);
727 if (op.type == ClosureVar || op.type == ModuleVar)
728 symbolTable = op.lexicalEnvironment->symbolTable();
729 else if (op.type == GlobalVar)
730 symbolTable = m_globalObject.get()->symbolTable();
732 UniquedStringImpl* impl = (op.type == ModuleVar) ? op.importedName.get() : ident.impl();
734 ConcurrentJSLocker locker(symbolTable->m_lock);
735 // If our parent scope was created while profiling was disabled, it will not have prepared for profiling yet.
736 symbolTable->prepareForTypeProfiling(locker);
737 globalVariableID = symbolTable->uniqueIDForVariable(locker, impl, vm);
738 globalTypeSet = symbolTable->globalTypeSetForVariable(locker, impl, vm);
740 globalVariableID = TypeProfilerNoGlobalIDExists;
744 case ProfileTypeBytecodeLocallyResolved: {
745 int symbolTableIndex = pc[2].u.operand;
746 SymbolTable* symbolTable = jsCast<SymbolTable*>(getConstant(symbolTableIndex));
747 const Identifier& ident = identifier(pc[4].u.operand);
748 ConcurrentJSLocker locker(symbolTable->m_lock);
749 // If our parent scope was created while profiling was disabled, it will not have prepared for profiling yet.
750 globalVariableID = symbolTable->uniqueIDForVariable(locker, ident.impl(), vm);
751 globalTypeSet = symbolTable->globalTypeSetForVariable(locker, ident.impl(), vm);
755 case ProfileTypeBytecodeDoesNotHaveGlobalID:
756 case ProfileTypeBytecodeFunctionArgument: {
757 globalVariableID = TypeProfilerNoGlobalIDExists;
760 case ProfileTypeBytecodeFunctionReturnStatement: {
761 RELEASE_ASSERT(ownerExecutable->isFunctionExecutable());
762 globalTypeSet = jsCast<FunctionExecutable*>(ownerExecutable)->returnStatementTypeSet();
763 globalVariableID = TypeProfilerReturnStatement;
764 if (!shouldAnalyze) {
765 // Because a return statement can be added implicitly to return undefined at the end of a function,
766 // and these nodes don't emit expression ranges because they aren't in the actual source text of
767 // the user's program, give the type profiler some range to identify these return statements.
768 // Currently, the text offset that is used as identification is "f" in the function keyword
769 // and is stored on TypeLocation's m_divotForFunctionOffsetIfReturnStatement member variable.
770 divotStart = divotEnd = ownerExecutable->typeProfilingStartOffset();
771 shouldAnalyze = true;
777 std::pair<TypeLocation*, bool> locationPair = vm.typeProfiler()->typeLocationCache()->getTypeLocation(globalVariableID,
778 ownerExecutable->sourceID(), divotStart, divotEnd, WTFMove(globalTypeSet), &vm);
779 TypeLocation* location = locationPair.first;
780 bool isNewLocation = locationPair.second;
782 if (flag == ProfileTypeBytecodeFunctionReturnStatement)
783 location->m_divotForFunctionOffsetIfReturnStatement = ownerExecutable->typeProfilingStartOffset();
785 if (shouldAnalyze && isNewLocation)
786 vm.typeProfiler()->insertNewLocation(location);
788 instructions[i + 2].u.location = location;
793 if (pc[1].u.unsignedValue == DidReachBreakpoint)
794 m_hasDebuggerStatement = true;
798 case op_create_rest: {
799 int numberOfArgumentsToSkip = instructions[i + 3].u.operand;
800 ASSERT_UNUSED(numberOfArgumentsToSkip, numberOfArgumentsToSkip >= 0);
801 // This is used when rematerializing the rest parameter during OSR exit in the FTL JIT.");
802 m_numberOfArgumentsToSkip = numberOfArgumentsToSkip;
812 if (vm.controlFlowProfiler())
813 insertBasicBlockBoundariesForControlFlowProfiler(instructions);
815 m_instructions = WTFMove(instructions);
817 // Set optimization thresholds only after m_instructions is initialized, since these
818 // rely on the instruction count (and are in theory permitted to also inspect the
819 // instruction stream to more accurate assess the cost of tier-up).
820 optimizeAfterWarmUp();
823 // If the concurrent thread will want the code block's hash, then compute it here
825 if (Options::alwaysComputeHash())
828 if (Options::dumpGeneratedBytecodes())
831 heap()->m_codeBlocks->add(this);
832 heap()->reportExtraMemoryAllocated(m_instructions.size() * sizeof(Instruction));
837 CodeBlock::~CodeBlock()
839 if (m_vm->m_perBytecodeProfiler)
840 m_vm->m_perBytecodeProfiler->notifyDestruction(this);
842 if (unlinkedCodeBlock()->didOptimize() == MixedTriState)
843 unlinkedCodeBlock()->setDidOptimize(FalseTriState);
845 #if ENABLE(VERBOSE_VALUE_PROFILE)
849 // We may be destroyed before any CodeBlocks that refer to us are destroyed.
850 // Consider that two CodeBlocks become unreachable at the same time. There
851 // is no guarantee about the order in which the CodeBlocks are destroyed.
852 // So, if we don't remove incoming calls, and get destroyed before the
853 // CodeBlock(s) that have calls into us, then the CallLinkInfo vector's
854 // destructor will try to remove nodes from our (no longer valid) linked list.
855 unlinkIncomingCalls();
857 // Note that our outgoing calls will be removed from other CodeBlocks'
858 // m_incomingCalls linked lists through the execution of the ~CallLinkInfo
862 for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter) {
863 StructureStubInfo* stub = *iter;
867 #endif // ENABLE(JIT)
870 bool CodeBlock::setConstantIdentifierSetRegisters(VM& vm, const Vector<ConstantIndentifierSetEntry>& constants)
872 auto scope = DECLARE_THROW_SCOPE(vm);
873 JSGlobalObject* globalObject = m_globalObject.get();
874 ExecState* exec = globalObject->globalExec();
876 for (const auto& entry : constants) {
877 Structure* setStructure = globalObject->setStructure();
878 RETURN_IF_EXCEPTION(scope, false);
879 JSSet* jsSet = JSSet::create(exec, vm, setStructure);
880 RETURN_IF_EXCEPTION(scope, false);
882 const IdentifierSet& set = entry.first;
883 for (auto& setEntry : set) {
884 JSString* jsString = jsOwnedString(&vm, setEntry.get());
885 jsSet->add(exec, JSValue(jsString));
886 RETURN_IF_EXCEPTION(scope, false);
888 m_constantRegisters[entry.second].set(vm, this, JSValue(jsSet));
893 bool CodeBlock::setConstantRegisters(const Vector<WriteBarrier<Unknown>>& constants, const Vector<SourceCodeRepresentation>& constantsSourceCodeRepresentation)
895 auto scope = DECLARE_THROW_SCOPE(*m_vm);
896 JSGlobalObject* globalObject = m_globalObject.get();
897 ExecState* exec = globalObject->globalExec();
899 ASSERT(constants.size() == constantsSourceCodeRepresentation.size());
900 size_t count = constants.size();
901 m_constantRegisters.resizeToFit(count);
902 bool hasTypeProfiler = !!m_vm->typeProfiler();
903 for (size_t i = 0; i < count; i++) {
904 JSValue constant = constants[i].get();
906 if (!constant.isEmpty()) {
907 if (SymbolTable* symbolTable = jsDynamicCast<SymbolTable*>(*m_vm, constant)) {
908 if (hasTypeProfiler) {
909 ConcurrentJSLocker locker(symbolTable->m_lock);
910 symbolTable->prepareForTypeProfiling(locker);
913 SymbolTable* clone = symbolTable->cloneScopePart(*m_vm);
914 if (wasCompiledWithDebuggingOpcodes())
915 clone->setRareDataCodeBlock(this);
918 } else if (isTemplateRegistryKey(*m_vm, constant)) {
919 auto* templateObject = globalObject->templateRegistry().getTemplateObject(exec, jsCast<JSTemplateRegistryKey*>(constant));
920 RETURN_IF_EXCEPTION(scope, false);
921 constant = templateObject;
925 m_constantRegisters[i].set(*m_vm, this, constant);
928 m_constantsSourceCodeRepresentation = constantsSourceCodeRepresentation;
933 void CodeBlock::setAlternative(VM& vm, CodeBlock* alternative)
935 m_alternative.set(vm, this, alternative);
938 void CodeBlock::setNumParameters(int newValue)
940 m_numParameters = newValue;
942 m_argumentValueProfiles = RefCountedArray<ValueProfile>(newValue);
945 CodeBlock* CodeBlock::specialOSREntryBlockOrNull()
948 if (jitType() != JITCode::DFGJIT)
950 DFG::JITCode* jitCode = m_jitCode->dfg();
951 return jitCode->osrEntryBlock();
952 #else // ENABLE(FTL_JIT)
954 #endif // ENABLE(FTL_JIT)
957 void CodeBlock::visitWeakly(SlotVisitor& visitor)
959 ConcurrentJSLocker locker(m_lock);
960 if (m_visitWeaklyHasBeenCalled)
963 m_visitWeaklyHasBeenCalled = true;
965 if (Heap::isMarkedConcurrently(this))
968 if (shouldVisitStrongly(locker)) {
969 visitor.appendUnbarriered(this);
973 // There are two things that may use unconditional finalizers: inline cache clearing
974 // and jettisoning. The probability of us wanting to do at least one of those things
975 // is probably quite close to 1. So we add one no matter what and when it runs, it
976 // figures out whether it has any work to do.
977 visitor.addUnconditionalFinalizer(&m_unconditionalFinalizer);
979 if (!JITCode::isOptimizingJIT(jitType()))
982 // If we jettison ourselves we'll install our alternative, so make sure that it
983 // survives GC even if we don't.
984 visitor.append(m_alternative);
986 // There are two things that we use weak reference harvesters for: DFG fixpoint for
987 // jettisoning, and trying to find structures that would be live based on some
988 // inline cache. So it makes sense to register them regardless.
989 visitor.addWeakReferenceHarvester(&m_weakReferenceHarvester);
992 // We get here if we're live in the sense that our owner executable is live,
993 // but we're not yet live for sure in another sense: we may yet decide that this
994 // code block should be jettisoned based on its outgoing weak references being
995 // stale. Set a flag to indicate that we're still assuming that we're dead, and
996 // perform one round of determining if we're live. The GC may determine, based on
997 // either us marking additional objects, or by other objects being marked for
998 // other reasons, that this iteration should run again; it will notify us of this
999 // decision by calling harvestWeakReferences().
1001 m_allTransitionsHaveBeenMarked = false;
1002 propagateTransitions(locker, visitor);
1004 m_jitCode->dfgCommon()->livenessHasBeenProved = false;
1005 determineLiveness(locker, visitor);
1006 #endif // ENABLE(DFG_JIT)
1009 size_t CodeBlock::estimatedSize(JSCell* cell)
1011 CodeBlock* thisObject = jsCast<CodeBlock*>(cell);
1012 size_t extraMemoryAllocated = thisObject->m_instructions.size() * sizeof(Instruction);
1013 if (thisObject->m_jitCode)
1014 extraMemoryAllocated += thisObject->m_jitCode->size();
1015 return Base::estimatedSize(cell) + extraMemoryAllocated;
1018 void CodeBlock::visitChildren(JSCell* cell, SlotVisitor& visitor)
1020 CodeBlock* thisObject = jsCast<CodeBlock*>(cell);
1021 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
1022 JSCell::visitChildren(thisObject, visitor);
1023 thisObject->visitChildren(visitor);
1026 void CodeBlock::visitChildren(SlotVisitor& visitor)
1028 ConcurrentJSLocker locker(m_lock);
1029 // There are two things that may use unconditional finalizers: inline cache clearing
1030 // and jettisoning. The probability of us wanting to do at least one of those things
1031 // is probably quite close to 1. So we add one no matter what and when it runs, it
1032 // figures out whether it has any work to do.
1033 visitor.addUnconditionalFinalizer(&m_unconditionalFinalizer);
1035 if (CodeBlock* otherBlock = specialOSREntryBlockOrNull())
1036 visitor.appendUnbarriered(otherBlock);
1039 visitor.reportExtraMemoryVisited(m_jitCode->size());
1040 if (m_instructions.size()) {
1041 unsigned refCount = m_instructions.refCount();
1043 dataLog("CodeBlock: ", RawPointer(this), "\n");
1044 dataLog("m_instructions.data(): ", RawPointer(m_instructions.data()), "\n");
1045 dataLog("refCount: ", refCount, "\n");
1046 RELEASE_ASSERT_NOT_REACHED();
1048 visitor.reportExtraMemoryVisited(m_instructions.size() * sizeof(Instruction) / refCount);
1051 stronglyVisitStrongReferences(locker, visitor);
1052 stronglyVisitWeakReferences(locker, visitor);
1054 m_allTransitionsHaveBeenMarked = false;
1055 propagateTransitions(locker, visitor);
1058 bool CodeBlock::shouldVisitStrongly(const ConcurrentJSLocker& locker)
1060 if (Options::forceCodeBlockLiveness())
1063 if (shouldJettisonDueToOldAge(locker))
1066 // Interpreter and Baseline JIT CodeBlocks don't need to be jettisoned when
1067 // their weak references go stale. So if a basline JIT CodeBlock gets
1068 // scanned, we can assume that this means that it's live.
1069 if (!JITCode::isOptimizingJIT(jitType()))
1075 bool CodeBlock::shouldJettisonDueToWeakReference()
1077 if (!JITCode::isOptimizingJIT(jitType()))
1079 return !Heap::isMarked(this);
1082 static std::chrono::milliseconds timeToLive(JITCode::JITType jitType)
1084 if (UNLIKELY(Options::useEagerCodeBlockJettisonTiming())) {
1086 case JITCode::InterpreterThunk:
1087 return std::chrono::milliseconds(10);
1088 case JITCode::BaselineJIT:
1089 return std::chrono::milliseconds(10 + 20);
1090 case JITCode::DFGJIT:
1091 return std::chrono::milliseconds(40);
1092 case JITCode::FTLJIT:
1093 return std::chrono::milliseconds(120);
1095 return std::chrono::milliseconds::max();
1100 case JITCode::InterpreterThunk:
1101 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(5));
1102 case JITCode::BaselineJIT:
1103 // Effectively 10 additional seconds, since BaselineJIT and
1104 // InterpreterThunk share a CodeBlock.
1105 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(5 + 10));
1106 case JITCode::DFGJIT:
1107 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(20));
1108 case JITCode::FTLJIT:
1109 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(60));
1111 return std::chrono::milliseconds::max();
1115 bool CodeBlock::shouldJettisonDueToOldAge(const ConcurrentJSLocker&)
1117 if (Heap::isMarkedConcurrently(this))
1120 if (UNLIKELY(Options::forceCodeBlockToJettisonDueToOldAge()))
1123 if (timeSinceCreation() < timeToLive(jitType()))
1130 static bool shouldMarkTransition(DFG::WeakReferenceTransition& transition)
1132 if (transition.m_codeOrigin && !Heap::isMarkedConcurrently(transition.m_codeOrigin.get()))
1135 if (!Heap::isMarkedConcurrently(transition.m_from.get()))
1140 #endif // ENABLE(DFG_JIT)
1142 void CodeBlock::propagateTransitions(const ConcurrentJSLocker&, SlotVisitor& visitor)
1144 UNUSED_PARAM(visitor);
1146 if (m_allTransitionsHaveBeenMarked)
1149 bool allAreMarkedSoFar = true;
1151 Interpreter* interpreter = m_vm->interpreter;
1152 if (jitType() == JITCode::InterpreterThunk) {
1153 const Vector<unsigned>& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions();
1154 for (size_t i = 0; i < propertyAccessInstructions.size(); ++i) {
1155 Instruction* instruction = &instructions()[propertyAccessInstructions[i]];
1156 switch (interpreter->getOpcodeID(instruction[0])) {
1157 case op_put_by_id: {
1158 StructureID oldStructureID = instruction[4].u.structureID;
1159 StructureID newStructureID = instruction[6].u.structureID;
1160 if (!oldStructureID || !newStructureID)
1162 Structure* oldStructure =
1163 m_vm->heap.structureIDTable().get(oldStructureID);
1164 Structure* newStructure =
1165 m_vm->heap.structureIDTable().get(newStructureID);
1166 if (Heap::isMarkedConcurrently(oldStructure))
1167 visitor.appendUnbarriered(newStructure);
1169 allAreMarkedSoFar = false;
1179 if (JITCode::isJIT(jitType())) {
1180 for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter)
1181 allAreMarkedSoFar &= (*iter)->propagateTransitions(visitor);
1183 #endif // ENABLE(JIT)
1186 if (JITCode::isOptimizingJIT(jitType())) {
1187 DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
1188 for (auto& weakReference : dfgCommon->weakStructureReferences)
1189 allAreMarkedSoFar &= weakReference->markIfCheap(visitor);
1191 for (auto& transition : dfgCommon->transitions) {
1192 if (shouldMarkTransition(transition)) {
1193 // If the following three things are live, then the target of the
1194 // transition is also live:
1196 // - This code block. We know it's live already because otherwise
1197 // we wouldn't be scanning ourselves.
1199 // - The code origin of the transition. Transitions may arise from
1200 // code that was inlined. They are not relevant if the user's
1201 // object that is required for the inlinee to run is no longer
1204 // - The source of the transition. The transition checks if some
1205 // heap location holds the source, and if so, stores the target.
1206 // Hence the source must be live for the transition to be live.
1208 // We also short-circuit the liveness if the structure is harmless
1209 // to mark (i.e. its global object and prototype are both already
1212 visitor.append(transition.m_to);
1214 allAreMarkedSoFar = false;
1217 #endif // ENABLE(DFG_JIT)
1219 if (allAreMarkedSoFar)
1220 m_allTransitionsHaveBeenMarked = true;
1223 void CodeBlock::determineLiveness(const ConcurrentJSLocker&, SlotVisitor& visitor)
1225 UNUSED_PARAM(visitor);
1228 // Check if we have any remaining work to do.
1229 DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
1230 if (dfgCommon->livenessHasBeenProved)
1233 // Now check all of our weak references. If all of them are live, then we
1234 // have proved liveness and so we scan our strong references. If at end of
1235 // GC we still have not proved liveness, then this code block is toast.
1236 bool allAreLiveSoFar = true;
1237 for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) {
1238 JSCell* reference = dfgCommon->weakReferences[i].get();
1239 ASSERT(!jsDynamicCast<CodeBlock*>(*reference->vm(), reference));
1240 if (!Heap::isMarkedConcurrently(reference)) {
1241 allAreLiveSoFar = false;
1245 if (allAreLiveSoFar) {
1246 for (unsigned i = 0; i < dfgCommon->weakStructureReferences.size(); ++i) {
1247 if (!Heap::isMarkedConcurrently(dfgCommon->weakStructureReferences[i].get())) {
1248 allAreLiveSoFar = false;
1254 // If some weak references are dead, then this fixpoint iteration was
1256 if (!allAreLiveSoFar)
1259 // All weak references are live. Record this information so we don't
1260 // come back here again, and scan the strong references.
1261 dfgCommon->livenessHasBeenProved = true;
1262 visitor.appendUnbarriered(this);
1263 #endif // ENABLE(DFG_JIT)
1266 void CodeBlock::WeakReferenceHarvester::visitWeakReferences(SlotVisitor& visitor)
1268 CodeBlock* codeBlock =
1269 bitwise_cast<CodeBlock*>(
1270 bitwise_cast<char*>(this) - OBJECT_OFFSETOF(CodeBlock, m_weakReferenceHarvester));
1272 codeBlock->propagateTransitions(NoLockingNecessary, visitor);
1273 codeBlock->determineLiveness(NoLockingNecessary, visitor);
1276 void CodeBlock::clearLLIntGetByIdCache(Instruction* instruction)
1278 instruction[0].u.opcode = LLInt::getOpcode(op_get_by_id);
1279 instruction[4].u.pointer = nullptr;
1280 instruction[5].u.pointer = nullptr;
1281 instruction[6].u.pointer = nullptr;
1284 void CodeBlock::finalizeLLIntInlineCaches()
1286 Interpreter* interpreter = m_vm->interpreter;
1287 const Vector<unsigned>& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions();
1288 for (size_t size = propertyAccessInstructions.size(), i = 0; i < size; ++i) {
1289 Instruction* curInstruction = &instructions()[propertyAccessInstructions[i]];
1290 switch (interpreter->getOpcodeID(curInstruction[0])) {
1292 case op_get_by_id_proto_load:
1293 case op_get_by_id_unset: {
1294 StructureID oldStructureID = curInstruction[4].u.structureID;
1295 if (!oldStructureID || Heap::isMarked(m_vm->heap.structureIDTable().get(oldStructureID)))
1297 if (Options::verboseOSR())
1298 dataLogF("Clearing LLInt property access.\n");
1299 clearLLIntGetByIdCache(curInstruction);
1302 case op_put_by_id: {
1303 StructureID oldStructureID = curInstruction[4].u.structureID;
1304 StructureID newStructureID = curInstruction[6].u.structureID;
1305 StructureChain* chain = curInstruction[7].u.structureChain.get();
1306 if ((!oldStructureID || Heap::isMarked(m_vm->heap.structureIDTable().get(oldStructureID))) &&
1307 (!newStructureID || Heap::isMarked(m_vm->heap.structureIDTable().get(newStructureID))) &&
1308 (!chain || Heap::isMarked(chain)))
1310 if (Options::verboseOSR())
1311 dataLogF("Clearing LLInt put transition.\n");
1312 curInstruction[4].u.structureID = 0;
1313 curInstruction[5].u.operand = 0;
1314 curInstruction[6].u.structureID = 0;
1315 curInstruction[7].u.structureChain.clear();
1318 case op_get_array_length:
1321 if (!curInstruction[2].u.structure || Heap::isMarked(curInstruction[2].u.structure.get()))
1323 if (Options::verboseOSR())
1324 dataLogF("Clearing LLInt to_this with structure %p.\n", curInstruction[2].u.structure.get());
1325 curInstruction[2].u.structure.clear();
1326 curInstruction[3].u.toThisStatus = merge(
1327 curInstruction[3].u.toThisStatus, ToThisClearedByGC);
1329 case op_create_this: {
1330 auto& cacheWriteBarrier = curInstruction[4].u.jsCell;
1331 if (!cacheWriteBarrier || cacheWriteBarrier.unvalidatedGet() == JSCell::seenMultipleCalleeObjects())
1333 JSCell* cachedFunction = cacheWriteBarrier.get();
1334 if (Heap::isMarked(cachedFunction))
1336 if (Options::verboseOSR())
1337 dataLogF("Clearing LLInt create_this with cached callee %p.\n", cachedFunction);
1338 cacheWriteBarrier.clear();
1341 case op_resolve_scope: {
1342 // Right now this isn't strictly necessary. Any symbol tables that this will refer to
1343 // are for outer functions, and we refer to those functions strongly, and they refer
1344 // to the symbol table strongly. But it's nice to be on the safe side.
1345 WriteBarrierBase<SymbolTable>& symbolTable = curInstruction[6].u.symbolTable;
1346 if (!symbolTable || Heap::isMarked(symbolTable.get()))
1348 if (Options::verboseOSR())
1349 dataLogF("Clearing dead symbolTable %p.\n", symbolTable.get());
1350 symbolTable.clear();
1353 case op_get_from_scope:
1354 case op_put_to_scope: {
1355 GetPutInfo getPutInfo = GetPutInfo(curInstruction[4].u.operand);
1356 if (getPutInfo.resolveType() == GlobalVar || getPutInfo.resolveType() == GlobalVarWithVarInjectionChecks
1357 || getPutInfo.resolveType() == LocalClosureVar || getPutInfo.resolveType() == GlobalLexicalVar || getPutInfo.resolveType() == GlobalLexicalVarWithVarInjectionChecks)
1359 WriteBarrierBase<Structure>& structure = curInstruction[5].u.structure;
1360 if (!structure || Heap::isMarked(structure.get()))
1362 if (Options::verboseOSR())
1363 dataLogF("Clearing scope access with structure %p.\n", structure.get());
1368 OpcodeID opcodeID = interpreter->getOpcodeID(curInstruction[0]);
1369 ASSERT_WITH_MESSAGE_UNUSED(opcodeID, false, "Unhandled opcode in CodeBlock::finalizeUnconditionally, %s(%d) at bc %u", opcodeNames[opcodeID], opcodeID, propertyAccessInstructions[i]);
1373 // We can't just remove all the sets when we clear the caches since we might have created a watchpoint set
1374 // then cleared the cache without GCing in between.
1375 m_llintGetByIdWatchpointMap.removeIf([](const StructureWatchpointMap::KeyValuePairType& pair) -> bool {
1376 return !Heap::isMarked(pair.key);
1379 for (unsigned i = 0; i < m_llintCallLinkInfos.size(); ++i) {
1380 if (m_llintCallLinkInfos[i].isLinked() && !Heap::isMarked(m_llintCallLinkInfos[i].callee.get())) {
1381 if (Options::verboseOSR())
1382 dataLog("Clearing LLInt call from ", *this, "\n");
1383 m_llintCallLinkInfos[i].unlink();
1385 if (!!m_llintCallLinkInfos[i].lastSeenCallee && !Heap::isMarked(m_llintCallLinkInfos[i].lastSeenCallee.get()))
1386 m_llintCallLinkInfos[i].lastSeenCallee.clear();
1390 void CodeBlock::finalizeBaselineJITInlineCaches()
1393 for (auto iter = callLinkInfosBegin(); !!iter; ++iter)
1394 (*iter)->visitWeak(*vm());
1396 for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter) {
1397 StructureStubInfo& stubInfo = **iter;
1398 stubInfo.visitWeakReferences(this);
1403 void CodeBlock::UnconditionalFinalizer::finalizeUnconditionally()
1405 CodeBlock* codeBlock = bitwise_cast<CodeBlock*>(
1406 bitwise_cast<char*>(this) - OBJECT_OFFSETOF(CodeBlock, m_unconditionalFinalizer));
1408 codeBlock->updateAllPredictions();
1410 if (!Heap::isMarked(codeBlock)) {
1411 if (codeBlock->shouldJettisonDueToWeakReference())
1412 codeBlock->jettison(Profiler::JettisonDueToWeakReference);
1414 codeBlock->jettison(Profiler::JettisonDueToOldAge);
1418 if (JITCode::couldBeInterpreted(codeBlock->jitType()))
1419 codeBlock->finalizeLLIntInlineCaches();
1422 if (!!codeBlock->jitCode())
1423 codeBlock->finalizeBaselineJITInlineCaches();
1427 void CodeBlock::getStubInfoMap(const ConcurrentJSLocker&, StubInfoMap& result)
1430 if (JITCode::isJIT(jitType()))
1431 toHashMap(m_stubInfos, getStructureStubInfoCodeOrigin, result);
1433 UNUSED_PARAM(result);
1437 void CodeBlock::getStubInfoMap(StubInfoMap& result)
1439 ConcurrentJSLocker locker(m_lock);
1440 getStubInfoMap(locker, result);
1443 void CodeBlock::getCallLinkInfoMap(const ConcurrentJSLocker&, CallLinkInfoMap& result)
1446 if (JITCode::isJIT(jitType()))
1447 toHashMap(m_callLinkInfos, getCallLinkInfoCodeOrigin, result);
1449 UNUSED_PARAM(result);
1453 void CodeBlock::getCallLinkInfoMap(CallLinkInfoMap& result)
1455 ConcurrentJSLocker locker(m_lock);
1456 getCallLinkInfoMap(locker, result);
1459 void CodeBlock::getByValInfoMap(const ConcurrentJSLocker&, ByValInfoMap& result)
1462 if (JITCode::isJIT(jitType())) {
1463 for (auto* byValInfo : m_byValInfos)
1464 result.add(CodeOrigin(byValInfo->bytecodeIndex), byValInfo);
1467 UNUSED_PARAM(result);
1471 void CodeBlock::getByValInfoMap(ByValInfoMap& result)
1473 ConcurrentJSLocker locker(m_lock);
1474 getByValInfoMap(locker, result);
1478 StructureStubInfo* CodeBlock::addStubInfo(AccessType accessType)
1480 ConcurrentJSLocker locker(m_lock);
1481 return m_stubInfos.add(accessType);
1484 JITAddIC* CodeBlock::addJITAddIC(ArithProfile* arithProfile)
1486 return m_addICs.add(arithProfile);
1489 JITMulIC* CodeBlock::addJITMulIC(ArithProfile* arithProfile)
1491 return m_mulICs.add(arithProfile);
1494 JITSubIC* CodeBlock::addJITSubIC(ArithProfile* arithProfile)
1496 return m_subICs.add(arithProfile);
1499 JITNegIC* CodeBlock::addJITNegIC(ArithProfile* arithProfile)
1501 return m_negICs.add(arithProfile);
1504 StructureStubInfo* CodeBlock::findStubInfo(CodeOrigin codeOrigin)
1506 for (StructureStubInfo* stubInfo : m_stubInfos) {
1507 if (stubInfo->codeOrigin == codeOrigin)
1513 ByValInfo* CodeBlock::addByValInfo()
1515 ConcurrentJSLocker locker(m_lock);
1516 return m_byValInfos.add();
1519 CallLinkInfo* CodeBlock::addCallLinkInfo()
1521 ConcurrentJSLocker locker(m_lock);
1522 return m_callLinkInfos.add();
1525 CallLinkInfo* CodeBlock::getCallLinkInfoForBytecodeIndex(unsigned index)
1527 for (auto iter = m_callLinkInfos.begin(); !!iter; ++iter) {
1528 if ((*iter)->codeOrigin() == CodeOrigin(index))
1534 void CodeBlock::resetJITData()
1536 RELEASE_ASSERT(!JITCode::isJIT(jitType()));
1537 ConcurrentJSLocker locker(m_lock);
1539 // We can clear these because no other thread will have references to any stub infos, call
1540 // link infos, or by val infos if we don't have JIT code. Attempts to query these data
1541 // structures using the concurrent API (getStubInfoMap and friends) will return nothing if we
1542 // don't have JIT code.
1543 m_stubInfos.clear();
1544 m_callLinkInfos.clear();
1545 m_byValInfos.clear();
1547 // We can clear this because the DFG's queries to these data structures are guarded by whether
1548 // there is JIT code.
1549 m_rareCaseProfiles.clear();
1553 void CodeBlock::visitOSRExitTargets(const ConcurrentJSLocker&, SlotVisitor& visitor)
1555 // We strongly visit OSR exits targets because we don't want to deal with
1556 // the complexity of generating an exit target CodeBlock on demand and
1557 // guaranteeing that it matches the details of the CodeBlock we compiled
1558 // the OSR exit against.
1560 visitor.append(m_alternative);
1563 DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
1564 if (dfgCommon->inlineCallFrames) {
1565 for (auto* inlineCallFrame : *dfgCommon->inlineCallFrames) {
1566 ASSERT(inlineCallFrame->baselineCodeBlock);
1567 visitor.append(inlineCallFrame->baselineCodeBlock);
1573 void CodeBlock::stronglyVisitStrongReferences(const ConcurrentJSLocker& locker, SlotVisitor& visitor)
1575 UNUSED_PARAM(locker);
1577 visitor.append(m_globalObject);
1578 visitor.append(m_ownerExecutable);
1579 visitor.append(m_unlinkedCode);
1581 m_rareData->m_directEvalCodeCache.visitAggregate(visitor);
1582 visitor.appendValues(m_constantRegisters.data(), m_constantRegisters.size());
1583 for (auto& functionExpr : m_functionExprs)
1584 visitor.append(functionExpr);
1585 for (auto& functionDecl : m_functionDecls)
1586 visitor.append(functionDecl);
1587 for (auto& objectAllocationProfile : m_objectAllocationProfiles)
1588 objectAllocationProfile.visitAggregate(visitor);
1591 for (ByValInfo* byValInfo : m_byValInfos)
1592 visitor.append(byValInfo->cachedSymbol);
1596 if (JITCode::isOptimizingJIT(jitType()))
1597 visitOSRExitTargets(locker, visitor);
1601 void CodeBlock::stronglyVisitWeakReferences(const ConcurrentJSLocker&, SlotVisitor& visitor)
1603 UNUSED_PARAM(visitor);
1606 if (!JITCode::isOptimizingJIT(jitType()))
1609 DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
1611 for (auto& transition : dfgCommon->transitions) {
1612 if (!!transition.m_codeOrigin)
1613 visitor.append(transition.m_codeOrigin); // Almost certainly not necessary, since the code origin should also be a weak reference. Better to be safe, though.
1614 visitor.append(transition.m_from);
1615 visitor.append(transition.m_to);
1618 for (auto& weakReference : dfgCommon->weakReferences)
1619 visitor.append(weakReference);
1621 for (auto& weakStructureReference : dfgCommon->weakStructureReferences)
1622 visitor.append(weakStructureReference);
1624 dfgCommon->livenessHasBeenProved = true;
1628 CodeBlock* CodeBlock::baselineAlternative()
1631 CodeBlock* result = this;
1632 while (result->alternative())
1633 result = result->alternative();
1634 RELEASE_ASSERT(result);
1635 RELEASE_ASSERT(JITCode::isBaselineCode(result->jitType()) || result->jitType() == JITCode::None);
1642 CodeBlock* CodeBlock::baselineVersion()
1645 if (JITCode::isBaselineCode(jitType()))
1647 CodeBlock* result = replacement();
1649 // This can happen if we're creating the original CodeBlock for an executable.
1650 // Assume that we're the baseline CodeBlock.
1651 RELEASE_ASSERT(jitType() == JITCode::None);
1654 result = result->baselineAlternative();
1662 bool CodeBlock::hasOptimizedReplacement(JITCode::JITType typeToReplace)
1664 return JITCode::isHigherTier(replacement()->jitType(), typeToReplace);
1667 bool CodeBlock::hasOptimizedReplacement()
1669 return hasOptimizedReplacement(jitType());
1673 HandlerInfo* CodeBlock::handlerForBytecodeOffset(unsigned bytecodeOffset, RequiredHandler requiredHandler)
1675 RELEASE_ASSERT(bytecodeOffset < instructions().size());
1676 return handlerForIndex(bytecodeOffset, requiredHandler);
1679 HandlerInfo* CodeBlock::handlerForIndex(unsigned index, RequiredHandler requiredHandler)
1683 return HandlerInfo::handlerForIndex(m_rareData->m_exceptionHandlers, index, requiredHandler);
1686 CallSiteIndex CodeBlock::newExceptionHandlingCallSiteIndex(CallSiteIndex originalCallSite)
1689 RELEASE_ASSERT(JITCode::isOptimizingJIT(jitType()));
1690 RELEASE_ASSERT(canGetCodeOrigin(originalCallSite));
1691 ASSERT(!!handlerForIndex(originalCallSite.bits()));
1692 CodeOrigin originalOrigin = codeOrigin(originalCallSite);
1693 return m_jitCode->dfgCommon()->addUniqueCallSiteIndex(originalOrigin);
1695 // We never create new on-the-fly exception handling
1696 // call sites outside the DFG/FTL inline caches.
1697 UNUSED_PARAM(originalCallSite);
1698 RELEASE_ASSERT_NOT_REACHED();
1699 return CallSiteIndex(0u);
1703 void CodeBlock::removeExceptionHandlerForCallSite(CallSiteIndex callSiteIndex)
1705 RELEASE_ASSERT(m_rareData);
1706 Vector<HandlerInfo>& exceptionHandlers = m_rareData->m_exceptionHandlers;
1707 unsigned index = callSiteIndex.bits();
1708 for (size_t i = 0; i < exceptionHandlers.size(); ++i) {
1709 HandlerInfo& handler = exceptionHandlers[i];
1710 if (handler.start <= index && handler.end > index) {
1711 exceptionHandlers.remove(i);
1716 RELEASE_ASSERT_NOT_REACHED();
1719 unsigned CodeBlock::lineNumberForBytecodeOffset(unsigned bytecodeOffset)
1721 RELEASE_ASSERT(bytecodeOffset < instructions().size());
1722 return ownerScriptExecutable()->firstLine() + m_unlinkedCode->lineNumberForBytecodeOffset(bytecodeOffset);
1725 unsigned CodeBlock::columnNumberForBytecodeOffset(unsigned bytecodeOffset)
1732 expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column);
1736 void CodeBlock::expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column) const
1738 m_unlinkedCode->expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column);
1739 divot += m_sourceOffset;
1740 column += line ? 1 : firstLineColumnOffset();
1741 line += ownerScriptExecutable()->firstLine();
1744 bool CodeBlock::hasOpDebugForLineAndColumn(unsigned line, unsigned column)
1746 Interpreter* interpreter = vm()->interpreter;
1747 const Instruction* begin = instructions().begin();
1748 const Instruction* end = instructions().end();
1749 for (const Instruction* it = begin; it != end;) {
1750 OpcodeID opcodeID = interpreter->getOpcodeID(*it);
1751 if (opcodeID == op_debug) {
1752 unsigned bytecodeOffset = it - begin;
1754 unsigned opDebugLine;
1755 unsigned opDebugColumn;
1756 expressionRangeForBytecodeOffset(bytecodeOffset, unused, unused, unused, opDebugLine, opDebugColumn);
1757 if (line == opDebugLine && (column == Breakpoint::unspecifiedColumn || column == opDebugColumn))
1760 it += opcodeLengths[opcodeID];
1765 void CodeBlock::shrinkToFit(ShrinkMode shrinkMode)
1767 ConcurrentJSLocker locker(m_lock);
1769 m_rareCaseProfiles.shrinkToFit();
1771 if (shrinkMode == EarlyShrink) {
1772 m_constantRegisters.shrinkToFit();
1773 m_constantsSourceCodeRepresentation.shrinkToFit();
1776 m_rareData->m_switchJumpTables.shrinkToFit();
1777 m_rareData->m_stringSwitchJumpTables.shrinkToFit();
1779 } // else don't shrink these, because we would have already pointed pointers into these tables.
1783 void CodeBlock::linkIncomingCall(ExecState* callerFrame, CallLinkInfo* incoming)
1785 noticeIncomingCall(callerFrame);
1786 m_incomingCalls.push(incoming);
1789 void CodeBlock::linkIncomingPolymorphicCall(ExecState* callerFrame, PolymorphicCallNode* incoming)
1791 noticeIncomingCall(callerFrame);
1792 m_incomingPolymorphicCalls.push(incoming);
1794 #endif // ENABLE(JIT)
1796 void CodeBlock::unlinkIncomingCalls()
1798 while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end())
1799 m_incomingLLIntCalls.begin()->unlink();
1801 while (m_incomingCalls.begin() != m_incomingCalls.end())
1802 m_incomingCalls.begin()->unlink(*vm());
1803 while (m_incomingPolymorphicCalls.begin() != m_incomingPolymorphicCalls.end())
1804 m_incomingPolymorphicCalls.begin()->unlink(*vm());
1805 #endif // ENABLE(JIT)
1808 void CodeBlock::linkIncomingCall(ExecState* callerFrame, LLIntCallLinkInfo* incoming)
1810 noticeIncomingCall(callerFrame);
1811 m_incomingLLIntCalls.push(incoming);
1814 CodeBlock* CodeBlock::newReplacement()
1816 return ownerScriptExecutable()->newReplacementCodeBlockFor(specializationKind());
1820 CodeBlock* CodeBlock::replacement()
1822 const ClassInfo* classInfo = this->classInfo(*vm());
1824 if (classInfo == FunctionCodeBlock::info())
1825 return jsCast<FunctionExecutable*>(ownerExecutable())->codeBlockFor(m_isConstructor ? CodeForConstruct : CodeForCall);
1827 if (classInfo == EvalCodeBlock::info())
1828 return jsCast<EvalExecutable*>(ownerExecutable())->codeBlock();
1830 if (classInfo == ProgramCodeBlock::info())
1831 return jsCast<ProgramExecutable*>(ownerExecutable())->codeBlock();
1833 if (classInfo == ModuleProgramCodeBlock::info())
1834 return jsCast<ModuleProgramExecutable*>(ownerExecutable())->codeBlock();
1836 RELEASE_ASSERT_NOT_REACHED();
1840 DFG::CapabilityLevel CodeBlock::computeCapabilityLevel()
1842 const ClassInfo* classInfo = this->classInfo(*vm());
1844 if (classInfo == FunctionCodeBlock::info()) {
1845 if (m_isConstructor)
1846 return DFG::functionForConstructCapabilityLevel(this);
1847 return DFG::functionForCallCapabilityLevel(this);
1850 if (classInfo == EvalCodeBlock::info())
1851 return DFG::evalCapabilityLevel(this);
1853 if (classInfo == ProgramCodeBlock::info())
1854 return DFG::programCapabilityLevel(this);
1856 if (classInfo == ModuleProgramCodeBlock::info())
1857 return DFG::programCapabilityLevel(this);
1859 RELEASE_ASSERT_NOT_REACHED();
1860 return DFG::CannotCompile;
1863 #endif // ENABLE(JIT)
1865 void CodeBlock::jettison(Profiler::JettisonReason reason, ReoptimizationMode mode, const FireDetail* detail)
1867 #if !ENABLE(DFG_JIT)
1869 UNUSED_PARAM(detail);
1872 CODEBLOCK_LOG_EVENT(this, "jettison", ("due to ", reason, ", counting = ", mode == CountReoptimization, ", detail = ", pointerDump(detail)));
1874 RELEASE_ASSERT(reason != Profiler::NotJettisoned);
1877 if (DFG::shouldDumpDisassembly()) {
1878 dataLog("Jettisoning ", *this);
1879 if (mode == CountReoptimization)
1880 dataLog(" and counting reoptimization");
1881 dataLog(" due to ", reason);
1883 dataLog(", ", *detail);
1887 if (reason == Profiler::JettisonDueToWeakReference) {
1888 if (DFG::shouldDumpDisassembly()) {
1889 dataLog(*this, " will be jettisoned because of the following dead references:\n");
1890 DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
1891 for (auto& transition : dfgCommon->transitions) {
1892 JSCell* origin = transition.m_codeOrigin.get();
1893 JSCell* from = transition.m_from.get();
1894 JSCell* to = transition.m_to.get();
1895 if ((!origin || Heap::isMarked(origin)) && Heap::isMarked(from))
1897 dataLog(" Transition under ", RawPointer(origin), ", ", RawPointer(from), " -> ", RawPointer(to), ".\n");
1899 for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) {
1900 JSCell* weak = dfgCommon->weakReferences[i].get();
1901 if (Heap::isMarked(weak))
1903 dataLog(" Weak reference ", RawPointer(weak), ".\n");
1907 #endif // ENABLE(DFG_JIT)
1909 DeferGCForAWhile deferGC(*heap());
1911 // We want to accomplish two things here:
1912 // 1) Make sure that if this CodeBlock is on the stack right now, then if we return to it
1913 // we should OSR exit at the top of the next bytecode instruction after the return.
1914 // 2) Make sure that if we call the owner executable, then we shouldn't call this CodeBlock.
1917 if (reason != Profiler::JettisonDueToOldAge) {
1918 if (Profiler::Compilation* compilation = jitCode()->dfgCommon()->compilation.get())
1919 compilation->setJettisonReason(reason, detail);
1921 // This accomplishes (1), and does its own book-keeping about whether it has already happened.
1922 if (!jitCode()->dfgCommon()->invalidate()) {
1923 // We've already been invalidated.
1924 RELEASE_ASSERT(this != replacement() || (m_vm->heap.isCurrentThreadBusy() && !Heap::isMarked(ownerScriptExecutable())));
1929 if (DFG::shouldDumpDisassembly())
1930 dataLog(" Did invalidate ", *this, "\n");
1932 // Count the reoptimization if that's what the user wanted.
1933 if (mode == CountReoptimization) {
1934 // FIXME: Maybe this should call alternative().
1935 // https://bugs.webkit.org/show_bug.cgi?id=123677
1936 baselineAlternative()->countReoptimization();
1937 if (DFG::shouldDumpDisassembly())
1938 dataLog(" Did count reoptimization for ", *this, "\n");
1941 if (this != replacement()) {
1942 // This means that we were never the entrypoint. This can happen for OSR entry code
1948 alternative()->optimizeAfterWarmUp();
1950 if (reason != Profiler::JettisonDueToOldAge && reason != Profiler::JettisonDueToVMTraps)
1951 tallyFrequentExitSites();
1952 #endif // ENABLE(DFG_JIT)
1954 // Jettison can happen during GC. We don't want to install code to a dead executable
1955 // because that would add a dead object to the remembered set.
1956 if (m_vm->heap.isCurrentThreadBusy() && !Heap::isMarked(ownerScriptExecutable()))
1959 // This accomplishes (2).
1960 ownerScriptExecutable()->installCode(
1961 m_globalObject->vm(), alternative(), codeType(), specializationKind());
1964 if (DFG::shouldDumpDisassembly())
1965 dataLog(" Did install baseline version of ", *this, "\n");
1966 #endif // ENABLE(DFG_JIT)
1969 JSGlobalObject* CodeBlock::globalObjectFor(CodeOrigin codeOrigin)
1971 if (!codeOrigin.inlineCallFrame)
1972 return globalObject();
1973 return codeOrigin.inlineCallFrame->baselineCodeBlock->globalObject();
1976 class RecursionCheckFunctor {
1978 RecursionCheckFunctor(CallFrame* startCallFrame, CodeBlock* codeBlock, unsigned depthToCheck)
1979 : m_startCallFrame(startCallFrame)
1980 , m_codeBlock(codeBlock)
1981 , m_depthToCheck(depthToCheck)
1982 , m_foundStartCallFrame(false)
1983 , m_didRecurse(false)
1986 StackVisitor::Status operator()(StackVisitor& visitor) const
1988 CallFrame* currentCallFrame = visitor->callFrame();
1990 if (currentCallFrame == m_startCallFrame)
1991 m_foundStartCallFrame = true;
1993 if (m_foundStartCallFrame) {
1994 if (visitor->callFrame()->codeBlock() == m_codeBlock) {
1995 m_didRecurse = true;
1996 return StackVisitor::Done;
1999 if (!m_depthToCheck--)
2000 return StackVisitor::Done;
2003 return StackVisitor::Continue;
2006 bool didRecurse() const { return m_didRecurse; }
2009 CallFrame* m_startCallFrame;
2010 CodeBlock* m_codeBlock;
2011 mutable unsigned m_depthToCheck;
2012 mutable bool m_foundStartCallFrame;
2013 mutable bool m_didRecurse;
2016 void CodeBlock::noticeIncomingCall(ExecState* callerFrame)
2018 CodeBlock* callerCodeBlock = callerFrame->codeBlock();
2020 if (Options::verboseCallLink())
2021 dataLog("Noticing call link from ", pointerDump(callerCodeBlock), " to ", *this, "\n");
2024 if (!m_shouldAlwaysBeInlined)
2027 if (!callerCodeBlock) {
2028 m_shouldAlwaysBeInlined = false;
2029 if (Options::verboseCallLink())
2030 dataLog(" Clearing SABI because caller is native.\n");
2034 if (!hasBaselineJITProfiling())
2037 if (!DFG::mightInlineFunction(this))
2040 if (!canInline(capabilityLevelState()))
2043 if (!DFG::isSmallEnoughToInlineCodeInto(callerCodeBlock)) {
2044 m_shouldAlwaysBeInlined = false;
2045 if (Options::verboseCallLink())
2046 dataLog(" Clearing SABI because caller is too large.\n");
2050 if (callerCodeBlock->jitType() == JITCode::InterpreterThunk) {
2051 // If the caller is still in the interpreter, then we can't expect inlining to
2052 // happen anytime soon. Assume it's profitable to optimize it separately. This
2053 // ensures that a function is SABI only if it is called no more frequently than
2054 // any of its callers.
2055 m_shouldAlwaysBeInlined = false;
2056 if (Options::verboseCallLink())
2057 dataLog(" Clearing SABI because caller is in LLInt.\n");
2061 if (JITCode::isOptimizingJIT(callerCodeBlock->jitType())) {
2062 m_shouldAlwaysBeInlined = false;
2063 if (Options::verboseCallLink())
2064 dataLog(" Clearing SABI bcause caller was already optimized.\n");
2068 if (callerCodeBlock->codeType() != FunctionCode) {
2069 // If the caller is either eval or global code, assume that that won't be
2070 // optimized anytime soon. For eval code this is particularly true since we
2071 // delay eval optimization by a *lot*.
2072 m_shouldAlwaysBeInlined = false;
2073 if (Options::verboseCallLink())
2074 dataLog(" Clearing SABI because caller is not a function.\n");
2078 // Recursive calls won't be inlined.
2079 RecursionCheckFunctor functor(callerFrame, this, Options::maximumInliningDepth());
2080 vm()->topCallFrame->iterate(functor);
2082 if (functor.didRecurse()) {
2083 if (Options::verboseCallLink())
2084 dataLog(" Clearing SABI because recursion was detected.\n");
2085 m_shouldAlwaysBeInlined = false;
2089 if (callerCodeBlock->capabilityLevelState() == DFG::CapabilityLevelNotSet) {
2090 dataLog("In call from ", FullCodeOrigin(callerCodeBlock, callerFrame->codeOrigin()), " to ", *this, ": caller's DFG capability level is not set.\n");
2094 if (canCompile(callerCodeBlock->capabilityLevelState()))
2097 if (Options::verboseCallLink())
2098 dataLog(" Clearing SABI because the caller is not a DFG candidate.\n");
2100 m_shouldAlwaysBeInlined = false;
2104 unsigned CodeBlock::reoptimizationRetryCounter() const
2107 ASSERT(m_reoptimizationRetryCounter <= Options::reoptimizationRetryCounterMax());
2108 return m_reoptimizationRetryCounter;
2111 #endif // ENABLE(JIT)
2115 void CodeBlock::setCalleeSaveRegisters(RegisterSet calleeSaveRegisters)
2117 m_calleeSaveRegisters = std::make_unique<RegisterAtOffsetList>(calleeSaveRegisters);
2120 void CodeBlock::setCalleeSaveRegisters(std::unique_ptr<RegisterAtOffsetList> registerAtOffsetList)
2122 m_calleeSaveRegisters = WTFMove(registerAtOffsetList);
2125 static size_t roundCalleeSaveSpaceAsVirtualRegisters(size_t calleeSaveRegisters)
2127 static const unsigned cpuRegisterSize = sizeof(void*);
2128 return (WTF::roundUpToMultipleOf(sizeof(Register), calleeSaveRegisters * cpuRegisterSize) / sizeof(Register));
2132 size_t CodeBlock::llintBaselineCalleeSaveSpaceAsVirtualRegisters()
2134 return roundCalleeSaveSpaceAsVirtualRegisters(numberOfLLIntBaselineCalleeSaveRegisters());
2137 size_t CodeBlock::calleeSaveSpaceAsVirtualRegisters()
2139 return roundCalleeSaveSpaceAsVirtualRegisters(m_calleeSaveRegisters->size());
2142 void CodeBlock::countReoptimization()
2144 m_reoptimizationRetryCounter++;
2145 if (m_reoptimizationRetryCounter > Options::reoptimizationRetryCounterMax())
2146 m_reoptimizationRetryCounter = Options::reoptimizationRetryCounterMax();
2149 unsigned CodeBlock::numberOfDFGCompiles()
2151 ASSERT(JITCode::isBaselineCode(jitType()));
2152 if (Options::testTheFTL()) {
2153 if (m_didFailFTLCompilation)
2155 return (m_hasBeenCompiledWithFTL ? 1 : 0) + m_reoptimizationRetryCounter;
2157 return (JITCode::isOptimizingJIT(replacement()->jitType()) ? 1 : 0) + m_reoptimizationRetryCounter;
2160 int32_t CodeBlock::codeTypeThresholdMultiplier() const
2162 if (codeType() == EvalCode)
2163 return Options::evalThresholdMultiplier();
2168 double CodeBlock::optimizationThresholdScalingFactor()
2170 // This expression arises from doing a least-squares fit of
2172 // F[x_] =: a * Sqrt[x + b] + Abs[c * x] + d
2174 // against the data points:
2177 // 10 0.9 (smallest reasonable code block)
2178 // 200 1.0 (typical small-ish code block)
2179 // 320 1.2 (something I saw in 3d-cube that I wanted to optimize)
2180 // 1268 5.0 (something I saw in 3d-cube that I didn't want to optimize)
2181 // 4000 5.5 (random large size, used to cause the function to converge to a shallow curve of some sort)
2182 // 10000 6.0 (similar to above)
2184 // I achieve the minimization using the following Mathematica code:
2186 // MyFunctionTemplate[x_, a_, b_, c_, d_] := a*Sqrt[x + b] + Abs[c*x] + d
2188 // samples = {{10, 0.9}, {200, 1}, {320, 1.2}, {1268, 5}, {4000, 5.5}, {10000, 6}}
2191 // Minimize[Plus @@ ((MyFunctionTemplate[#[[1]], a, b, c, d] - #[[2]])^2 & /@ samples),
2192 // {a, b, c, d}][[2]]
2194 // And the code below (to initialize a, b, c, d) is generated by:
2196 // Print["const double " <> ToString[#[[1]]] <> " = " <>
2197 // If[#[[2]] < 0.00001, "0.0", ToString[#[[2]]]] <> ";"] & /@ solution
2199 // We've long known the following to be true:
2200 // - Small code blocks are cheap to optimize and so we should do it sooner rather
2202 // - Large code blocks are expensive to optimize and so we should postpone doing so,
2203 // and sometimes have a large enough threshold that we never optimize them.
2204 // - The difference in cost is not totally linear because (a) just invoking the
2205 // DFG incurs some base cost and (b) for large code blocks there is enough slop
2206 // in the correlation between instruction count and the actual compilation cost
2207 // that for those large blocks, the instruction count should not have a strong
2208 // influence on our threshold.
2210 // I knew the goals but I didn't know how to achieve them; so I picked an interesting
2211 // example where the heuristics were right (code block in 3d-cube with instruction
2212 // count 320, which got compiled early as it should have been) and one where they were
2213 // totally wrong (code block in 3d-cube with instruction count 1268, which was expensive
2214 // to compile and didn't run often enough to warrant compilation in my opinion), and
2215 // then threw in additional data points that represented my own guess of what our
2216 // heuristics should do for some round-numbered examples.
2218 // The expression to which I decided to fit the data arose because I started with an
2219 // affine function, and then did two things: put the linear part in an Abs to ensure
2220 // that the fit didn't end up choosing a negative value of c (which would result in
2221 // the function turning over and going negative for large x) and I threw in a Sqrt
2222 // term because Sqrt represents my intution that the function should be more sensitive
2223 // to small changes in small values of x, but less sensitive when x gets large.
2225 // Note that the current fit essentially eliminates the linear portion of the
2226 // expression (c == 0.0).
2227 const double a = 0.061504;
2228 const double b = 1.02406;
2229 const double c = 0.0;
2230 const double d = 0.825914;
2232 double instructionCount = this->instructionCount();
2234 ASSERT(instructionCount); // Make sure this is called only after we have an instruction stream; otherwise it'll just return the value of d, which makes no sense.
2236 double result = d + a * sqrt(instructionCount + b) + c * instructionCount;
2238 result *= codeTypeThresholdMultiplier();
2240 if (Options::verboseOSR()) {
2242 *this, ": instruction count is ", instructionCount,
2243 ", scaling execution counter by ", result, " * ", codeTypeThresholdMultiplier(),
2249 static int32_t clipThreshold(double threshold)
2251 if (threshold < 1.0)
2254 if (threshold > static_cast<double>(std::numeric_limits<int32_t>::max()))
2255 return std::numeric_limits<int32_t>::max();
2257 return static_cast<int32_t>(threshold);
2260 int32_t CodeBlock::adjustedCounterValue(int32_t desiredThreshold)
2262 return clipThreshold(
2263 static_cast<double>(desiredThreshold) *
2264 optimizationThresholdScalingFactor() *
2265 (1 << reoptimizationRetryCounter()));
2268 bool CodeBlock::checkIfOptimizationThresholdReached()
2271 if (DFG::Worklist* worklist = DFG::existingGlobalDFGWorklistOrNull()) {
2272 if (worklist->compilationState(DFG::CompilationKey(this, DFG::DFGMode))
2273 == DFG::Worklist::Compiled) {
2274 optimizeNextInvocation();
2280 return m_jitExecuteCounter.checkIfThresholdCrossedAndSet(this);
2283 void CodeBlock::optimizeNextInvocation()
2285 if (Options::verboseOSR())
2286 dataLog(*this, ": Optimizing next invocation.\n");
2287 m_jitExecuteCounter.setNewThreshold(0, this);
2290 void CodeBlock::dontOptimizeAnytimeSoon()
2292 if (Options::verboseOSR())
2293 dataLog(*this, ": Not optimizing anytime soon.\n");
2294 m_jitExecuteCounter.deferIndefinitely();
2297 void CodeBlock::optimizeAfterWarmUp()
2299 if (Options::verboseOSR())
2300 dataLog(*this, ": Optimizing after warm-up.\n");
2302 m_jitExecuteCounter.setNewThreshold(
2303 adjustedCounterValue(Options::thresholdForOptimizeAfterWarmUp()), this);
2307 void CodeBlock::optimizeAfterLongWarmUp()
2309 if (Options::verboseOSR())
2310 dataLog(*this, ": Optimizing after long warm-up.\n");
2312 m_jitExecuteCounter.setNewThreshold(
2313 adjustedCounterValue(Options::thresholdForOptimizeAfterLongWarmUp()), this);
2317 void CodeBlock::optimizeSoon()
2319 if (Options::verboseOSR())
2320 dataLog(*this, ": Optimizing soon.\n");
2322 m_jitExecuteCounter.setNewThreshold(
2323 adjustedCounterValue(Options::thresholdForOptimizeSoon()), this);
2327 void CodeBlock::forceOptimizationSlowPathConcurrently()
2329 if (Options::verboseOSR())
2330 dataLog(*this, ": Forcing slow path concurrently.\n");
2331 m_jitExecuteCounter.forceSlowPathConcurrently();
2335 void CodeBlock::setOptimizationThresholdBasedOnCompilationResult(CompilationResult result)
2337 JITCode::JITType type = jitType();
2338 if (type != JITCode::BaselineJIT) {
2339 dataLog(*this, ": expected to have baseline code but have ", type, "\n");
2340 RELEASE_ASSERT_NOT_REACHED();
2343 CodeBlock* theReplacement = replacement();
2344 if ((result == CompilationSuccessful) != (theReplacement != this)) {
2345 dataLog(*this, ": we have result = ", result, " but ");
2346 if (theReplacement == this)
2347 dataLog("we are our own replacement.\n");
2349 dataLog("our replacement is ", pointerDump(theReplacement), "\n");
2350 RELEASE_ASSERT_NOT_REACHED();
2354 case CompilationSuccessful:
2355 RELEASE_ASSERT(JITCode::isOptimizingJIT(replacement()->jitType()));
2356 optimizeNextInvocation();
2358 case CompilationFailed:
2359 dontOptimizeAnytimeSoon();
2361 case CompilationDeferred:
2362 // We'd like to do dontOptimizeAnytimeSoon() but we cannot because
2363 // forceOptimizationSlowPathConcurrently() is inherently racy. It won't
2364 // necessarily guarantee anything. So, we make sure that even if that
2365 // function ends up being a no-op, we still eventually retry and realize
2366 // that we have optimized code ready.
2367 optimizeAfterWarmUp();
2369 case CompilationInvalidated:
2370 // Retry with exponential backoff.
2371 countReoptimization();
2372 optimizeAfterWarmUp();
2376 dataLog("Unrecognized result: ", static_cast<int>(result), "\n");
2377 RELEASE_ASSERT_NOT_REACHED();
2382 uint32_t CodeBlock::adjustedExitCountThreshold(uint32_t desiredThreshold)
2384 ASSERT(JITCode::isOptimizingJIT(jitType()));
2385 // Compute this the lame way so we don't saturate. This is called infrequently
2386 // enough that this loop won't hurt us.
2387 unsigned result = desiredThreshold;
2388 for (unsigned n = baselineVersion()->reoptimizationRetryCounter(); n--;) {
2389 unsigned newResult = result << 1;
2390 if (newResult < result)
2391 return std::numeric_limits<uint32_t>::max();
2397 uint32_t CodeBlock::exitCountThresholdForReoptimization()
2399 return adjustedExitCountThreshold(Options::osrExitCountForReoptimization() * codeTypeThresholdMultiplier());
2402 uint32_t CodeBlock::exitCountThresholdForReoptimizationFromLoop()
2404 return adjustedExitCountThreshold(Options::osrExitCountForReoptimizationFromLoop() * codeTypeThresholdMultiplier());
2407 bool CodeBlock::shouldReoptimizeNow()
2409 return osrExitCounter() >= exitCountThresholdForReoptimization();
2412 bool CodeBlock::shouldReoptimizeFromLoopNow()
2414 return osrExitCounter() >= exitCountThresholdForReoptimizationFromLoop();
2418 ArrayProfile* CodeBlock::getArrayProfile(const ConcurrentJSLocker&, unsigned bytecodeOffset)
2420 for (auto& m_arrayProfile : m_arrayProfiles) {
2421 if (m_arrayProfile.bytecodeOffset() == bytecodeOffset)
2422 return &m_arrayProfile;
2427 ArrayProfile* CodeBlock::getArrayProfile(unsigned bytecodeOffset)
2429 ConcurrentJSLocker locker(m_lock);
2430 return getArrayProfile(locker, bytecodeOffset);
2433 ArrayProfile* CodeBlock::addArrayProfile(const ConcurrentJSLocker&, unsigned bytecodeOffset)
2435 m_arrayProfiles.append(ArrayProfile(bytecodeOffset));
2436 return &m_arrayProfiles.last();
2439 ArrayProfile* CodeBlock::addArrayProfile(unsigned bytecodeOffset)
2441 ConcurrentJSLocker locker(m_lock);
2442 return addArrayProfile(locker, bytecodeOffset);
2445 ArrayProfile* CodeBlock::getOrAddArrayProfile(const ConcurrentJSLocker& locker, unsigned bytecodeOffset)
2447 ArrayProfile* result = getArrayProfile(locker, bytecodeOffset);
2450 return addArrayProfile(locker, bytecodeOffset);
2453 ArrayProfile* CodeBlock::getOrAddArrayProfile(unsigned bytecodeOffset)
2455 ConcurrentJSLocker locker(m_lock);
2456 return getOrAddArrayProfile(locker, bytecodeOffset);
2460 Vector<CodeOrigin, 0, UnsafeVectorOverflow>& CodeBlock::codeOrigins()
2462 return m_jitCode->dfgCommon()->codeOrigins;
2465 size_t CodeBlock::numberOfDFGIdentifiers() const
2467 if (!JITCode::isOptimizingJIT(jitType()))
2470 return m_jitCode->dfgCommon()->dfgIdentifiers.size();
2473 const Identifier& CodeBlock::identifier(int index) const
2475 size_t unlinkedIdentifiers = m_unlinkedCode->numberOfIdentifiers();
2476 if (static_cast<unsigned>(index) < unlinkedIdentifiers)
2477 return m_unlinkedCode->identifier(index);
2478 ASSERT(JITCode::isOptimizingJIT(jitType()));
2479 return m_jitCode->dfgCommon()->dfgIdentifiers[index - unlinkedIdentifiers];
2481 #endif // ENABLE(DFG_JIT)
2483 void CodeBlock::updateAllPredictionsAndCountLiveness(unsigned& numberOfLiveNonArgumentValueProfiles, unsigned& numberOfSamplesInProfiles)
2485 ConcurrentJSLocker locker(m_lock);
2487 numberOfLiveNonArgumentValueProfiles = 0;
2488 numberOfSamplesInProfiles = 0; // If this divided by ValueProfile::numberOfBuckets equals numberOfValueProfiles() then value profiles are full.
2489 for (unsigned i = 0; i < totalNumberOfValueProfiles(); ++i) {
2490 ValueProfile* profile = getFromAllValueProfiles(i);
2491 unsigned numSamples = profile->totalNumberOfSamples();
2492 if (numSamples > ValueProfile::numberOfBuckets)
2493 numSamples = ValueProfile::numberOfBuckets; // We don't want profiles that are extremely hot to be given more weight.
2494 numberOfSamplesInProfiles += numSamples;
2495 if (profile->m_bytecodeOffset < 0) {
2496 profile->computeUpdatedPrediction(locker);
2499 if (profile->numberOfSamples() || profile->m_prediction != SpecNone)
2500 numberOfLiveNonArgumentValueProfiles++;
2501 profile->computeUpdatedPrediction(locker);
2505 m_lazyOperandValueProfiles.computeUpdatedPredictions(locker);
2509 void CodeBlock::updateAllValueProfilePredictions()
2511 unsigned ignoredValue1, ignoredValue2;
2512 updateAllPredictionsAndCountLiveness(ignoredValue1, ignoredValue2);
2515 void CodeBlock::updateAllArrayPredictions()
2517 ConcurrentJSLocker locker(m_lock);
2519 for (unsigned i = m_arrayProfiles.size(); i--;)
2520 m_arrayProfiles[i].computeUpdatedPrediction(locker, this);
2522 // Don't count these either, for similar reasons.
2523 for (unsigned i = m_arrayAllocationProfiles.size(); i--;)
2524 m_arrayAllocationProfiles[i].updateIndexingType();
2527 void CodeBlock::updateAllPredictions()
2529 updateAllValueProfilePredictions();
2530 updateAllArrayPredictions();
2533 bool CodeBlock::shouldOptimizeNow()
2535 if (Options::verboseOSR())
2536 dataLog("Considering optimizing ", *this, "...\n");
2538 if (m_optimizationDelayCounter >= Options::maximumOptimizationDelay())
2541 updateAllArrayPredictions();
2543 unsigned numberOfLiveNonArgumentValueProfiles;
2544 unsigned numberOfSamplesInProfiles;
2545 updateAllPredictionsAndCountLiveness(numberOfLiveNonArgumentValueProfiles, numberOfSamplesInProfiles);
2547 if (Options::verboseOSR()) {
2549 "Profile hotness: %lf (%u / %u), %lf (%u / %u)\n",
2550 (double)numberOfLiveNonArgumentValueProfiles / numberOfValueProfiles(),
2551 numberOfLiveNonArgumentValueProfiles, numberOfValueProfiles(),
2552 (double)numberOfSamplesInProfiles / ValueProfile::numberOfBuckets / numberOfValueProfiles(),
2553 numberOfSamplesInProfiles, ValueProfile::numberOfBuckets * numberOfValueProfiles());
2556 if ((!numberOfValueProfiles() || (double)numberOfLiveNonArgumentValueProfiles / numberOfValueProfiles() >= Options::desiredProfileLivenessRate())
2557 && (!totalNumberOfValueProfiles() || (double)numberOfSamplesInProfiles / ValueProfile::numberOfBuckets / totalNumberOfValueProfiles() >= Options::desiredProfileFullnessRate())
2558 && static_cast<unsigned>(m_optimizationDelayCounter) + 1 >= Options::minimumOptimizationDelay())
2561 ASSERT(m_optimizationDelayCounter < std::numeric_limits<uint8_t>::max());
2562 m_optimizationDelayCounter++;
2563 optimizeAfterWarmUp();
2568 void CodeBlock::tallyFrequentExitSites()
2570 ASSERT(JITCode::isOptimizingJIT(jitType()));
2571 ASSERT(alternative()->jitType() == JITCode::BaselineJIT);
2573 CodeBlock* profiledBlock = alternative();
2575 switch (jitType()) {
2576 case JITCode::DFGJIT: {
2577 DFG::JITCode* jitCode = m_jitCode->dfg();
2578 for (auto& exit : jitCode->osrExit)
2579 exit.considerAddingAsFrequentExitSite(profiledBlock);
2584 case JITCode::FTLJIT: {
2585 // There is no easy way to avoid duplicating this code since the FTL::JITCode::osrExit
2586 // vector contains a totally different type, that just so happens to behave like
2587 // DFG::JITCode::osrExit.
2588 FTL::JITCode* jitCode = m_jitCode->ftl();
2589 for (unsigned i = 0; i < jitCode->osrExit.size(); ++i) {
2590 FTL::OSRExit& exit = jitCode->osrExit[i];
2591 exit.considerAddingAsFrequentExitSite(profiledBlock);
2598 RELEASE_ASSERT_NOT_REACHED();
2602 #endif // ENABLE(DFG_JIT)
2604 #if ENABLE(VERBOSE_VALUE_PROFILE)
2605 void CodeBlock::dumpValueProfiles()
2607 dataLog("ValueProfile for ", *this, ":\n");
2608 for (unsigned i = 0; i < totalNumberOfValueProfiles(); ++i) {
2609 ValueProfile* profile = getFromAllValueProfiles(i);
2610 if (profile->m_bytecodeOffset < 0) {
2611 ASSERT(profile->m_bytecodeOffset == -1);
2612 dataLogF(" arg = %u: ", i);
2614 dataLogF(" bc = %d: ", profile->m_bytecodeOffset);
2615 if (!profile->numberOfSamples() && profile->m_prediction == SpecNone) {
2616 dataLogF("<empty>\n");
2619 profile->dump(WTF::dataFile());
2622 dataLog("RareCaseProfile for ", *this, ":\n");
2623 for (unsigned i = 0; i < numberOfRareCaseProfiles(); ++i) {
2624 RareCaseProfile* profile = rareCaseProfile(i);
2625 dataLogF(" bc = %d: %u\n", profile->m_bytecodeOffset, profile->m_counter);
2628 #endif // ENABLE(VERBOSE_VALUE_PROFILE)
2630 unsigned CodeBlock::frameRegisterCount()
2632 switch (jitType()) {
2633 case JITCode::InterpreterThunk:
2634 return LLInt::frameRegisterCountFor(this);
2637 case JITCode::BaselineJIT:
2638 return JIT::frameRegisterCountFor(this);
2639 #endif // ENABLE(JIT)
2642 case JITCode::DFGJIT:
2643 case JITCode::FTLJIT:
2644 return jitCode()->dfgCommon()->frameRegisterCount;
2645 #endif // ENABLE(DFG_JIT)
2648 RELEASE_ASSERT_NOT_REACHED();
2653 int CodeBlock::stackPointerOffset()
2655 return virtualRegisterForLocal(frameRegisterCount() - 1).offset();
2658 size_t CodeBlock::predictedMachineCodeSize()
2660 // This will be called from CodeBlock::CodeBlock before either m_vm or the
2661 // instructions have been initialized. It's OK to return 0 because what will really
2662 // matter is the recomputation of this value when the slow path is triggered.
2666 if (!*m_vm->machineCodeBytesPerBytecodeWordForBaselineJIT)
2667 return 0; // It's as good of a prediction as we'll get.
2669 // Be conservative: return a size that will be an overestimation 84% of the time.
2670 double multiplier = m_vm->machineCodeBytesPerBytecodeWordForBaselineJIT->mean() +
2671 m_vm->machineCodeBytesPerBytecodeWordForBaselineJIT->standardDeviation();
2673 // Be paranoid: silently reject bogus multipiers. Silently doing the "wrong" thing
2674 // here is OK, since this whole method is just a heuristic.
2675 if (multiplier < 0 || multiplier > 1000)
2678 double doubleResult = multiplier * m_instructions.size();
2680 // Be even more paranoid: silently reject values that won't fit into a size_t. If
2681 // the function is so huge that we can't even fit it into virtual memory then we
2682 // should probably have some other guards in place to prevent us from even getting
2684 if (doubleResult > std::numeric_limits<size_t>::max())
2687 return static_cast<size_t>(doubleResult);
2690 bool CodeBlock::usesOpcode(OpcodeID opcodeID)
2692 Interpreter* interpreter = vm()->interpreter;
2693 Instruction* instructionsBegin = instructions().begin();
2694 unsigned instructionCount = instructions().size();
2696 for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount; ) {
2697 switch (interpreter->getOpcodeID(instructionsBegin[bytecodeOffset])) {
2698 #define DEFINE_OP(curOpcode, length) \
2700 if (curOpcode == opcodeID) \
2702 bytecodeOffset += length; \
2704 FOR_EACH_OPCODE_ID(DEFINE_OP)
2707 RELEASE_ASSERT_NOT_REACHED();
2715 String CodeBlock::nameForRegister(VirtualRegister virtualRegister)
2717 for (auto& constantRegister : m_constantRegisters) {
2718 if (constantRegister.get().isEmpty())
2720 if (SymbolTable* symbolTable = jsDynamicCast<SymbolTable*>(*vm(), constantRegister.get())) {
2721 ConcurrentJSLocker locker(symbolTable->m_lock);
2722 auto end = symbolTable->end(locker);
2723 for (auto ptr = symbolTable->begin(locker); ptr != end; ++ptr) {
2724 if (ptr->value.varOffset() == VarOffset(virtualRegister)) {
2725 // FIXME: This won't work from the compilation thread.
2726 // https://bugs.webkit.org/show_bug.cgi?id=115300
2727 return ptr->key.get();
2732 if (virtualRegister == thisRegister())
2733 return ASCIILiteral("this");
2734 if (virtualRegister.isArgument())
2735 return String::format("arguments[%3d]", virtualRegister.toArgument());
2740 ValueProfile* CodeBlock::valueProfileForBytecodeOffset(int bytecodeOffset)
2742 OpcodeID opcodeID = m_vm->interpreter->getOpcodeID(instructions()[bytecodeOffset]);
2743 unsigned length = opcodeLength(opcodeID);
2744 return instructions()[bytecodeOffset + length - 1].u.profile;
2747 void CodeBlock::validate()
2749 BytecodeLivenessAnalysis liveness(this); // Compute directly from scratch so it doesn't effect CodeBlock footprint.
2751 FastBitVector liveAtHead = liveness.getLivenessInfoAtBytecodeOffset(0);
2753 if (liveAtHead.numBits() != static_cast<size_t>(m_numCalleeLocals)) {
2754 beginValidationDidFail();
2755 dataLog(" Wrong number of bits in result!\n");
2756 dataLog(" Result: ", liveAtHead, "\n");
2757 dataLog(" Bit count: ", liveAtHead.numBits(), "\n");
2758 endValidationDidFail();
2761 for (unsigned i = m_numCalleeLocals; i--;) {
2762 VirtualRegister reg = virtualRegisterForLocal(i);
2764 if (liveAtHead[i]) {
2765 beginValidationDidFail();
2766 dataLog(" Variable ", reg, " is expected to be dead.\n");
2767 dataLog(" Result: ", liveAtHead, "\n");
2768 endValidationDidFail();
2773 void CodeBlock::beginValidationDidFail()
2775 dataLog("Validation failure in ", *this, ":\n");
2779 void CodeBlock::endValidationDidFail()
2784 dataLog("Validation failure.\n");
2785 RELEASE_ASSERT_NOT_REACHED();
2788 void CodeBlock::addBreakpoint(unsigned numBreakpoints)
2790 m_numBreakpoints += numBreakpoints;
2791 ASSERT(m_numBreakpoints);
2792 if (JITCode::isOptimizingJIT(jitType()))
2793 jettison(Profiler::JettisonDueToDebuggerBreakpoint);
2796 void CodeBlock::setSteppingMode(CodeBlock::SteppingMode mode)
2798 m_steppingMode = mode;
2799 if (mode == SteppingModeEnabled && JITCode::isOptimizingJIT(jitType()))
2800 jettison(Profiler::JettisonDueToDebuggerStepping);
2803 RareCaseProfile* CodeBlock::addRareCaseProfile(int bytecodeOffset)
2805 m_rareCaseProfiles.append(RareCaseProfile(bytecodeOffset));
2806 return &m_rareCaseProfiles.last();
2809 RareCaseProfile* CodeBlock::rareCaseProfileForBytecodeOffset(int bytecodeOffset)
2811 return tryBinarySearch<RareCaseProfile, int>(
2812 m_rareCaseProfiles, m_rareCaseProfiles.size(), bytecodeOffset,
2813 getRareCaseProfileBytecodeOffset);
2816 unsigned CodeBlock::rareCaseProfileCountForBytecodeOffset(int bytecodeOffset)
2818 RareCaseProfile* profile = rareCaseProfileForBytecodeOffset(bytecodeOffset);
2820 return profile->m_counter;
2824 ArithProfile* CodeBlock::arithProfileForBytecodeOffset(int bytecodeOffset)
2826 return arithProfileForPC(instructions().begin() + bytecodeOffset);
2829 ArithProfile* CodeBlock::arithProfileForPC(Instruction* pc)
2831 auto opcodeID = vm()->interpreter->getOpcodeID(pc[0]);
2834 return bitwise_cast<ArithProfile*>(&pc[3].u.operand);
2842 return bitwise_cast<ArithProfile*>(&pc[4].u.operand);
2850 bool CodeBlock::couldTakeSpecialFastCase(int bytecodeOffset)
2852 if (!hasBaselineJITProfiling())
2854 ArithProfile* profile = arithProfileForBytecodeOffset(bytecodeOffset);
2857 return profile->tookSpecialFastPath();
2861 DFG::CapabilityLevel CodeBlock::capabilityLevel()
2863 DFG::CapabilityLevel result = computeCapabilityLevel();
2864 m_capabilityLevelState = result;
2869 void CodeBlock::insertBasicBlockBoundariesForControlFlowProfiler(RefCountedArray<Instruction>& instructions)
2871 if (!unlinkedCodeBlock()->hasOpProfileControlFlowBytecodeOffsets())
2873 const Vector<size_t>& bytecodeOffsets = unlinkedCodeBlock()->opProfileControlFlowBytecodeOffsets();
2874 for (size_t i = 0, offsetsLength = bytecodeOffsets.size(); i < offsetsLength; i++) {
2875 // Because op_profile_control_flow is emitted at the beginning of every basic block, finding
2876 // the next op_profile_control_flow will give us the text range of a single basic block.
2877 size_t startIdx = bytecodeOffsets[i];
2878 RELEASE_ASSERT(vm()->interpreter->getOpcodeID(instructions[startIdx]) == op_profile_control_flow);
2879 int basicBlockStartOffset = instructions[startIdx + 1].u.operand;
2880 int basicBlockEndOffset;
2881 if (i + 1 < offsetsLength) {
2882 size_t endIdx = bytecodeOffsets[i + 1];
2883 RELEASE_ASSERT(vm()->interpreter->getOpcodeID(instructions[endIdx]) == op_profile_control_flow);
2884 basicBlockEndOffset = instructions[endIdx + 1].u.operand - 1;
2886 basicBlockEndOffset = m_sourceOffset + ownerScriptExecutable()->source().length() - 1; // Offset before the closing brace.
2887 basicBlockStartOffset = std::min(basicBlockStartOffset, basicBlockEndOffset); // Some start offsets may be at the closing brace, ensure it is the offset before.
2890 // The following check allows for the same textual JavaScript basic block to have its bytecode emitted more
2891 // than once and still play nice with the control flow profiler. When basicBlockStartOffset is larger than
2892 // basicBlockEndOffset, it indicates that the bytecode generator has emitted code for the same AST node
2893 // more than once (for example: ForInNode, Finally blocks in TryNode, etc). Though these are different
2894 // basic blocks at the bytecode level, they are generated from the same textual basic block in the JavaScript
2895 // program. The condition:
2896 // (basicBlockEndOffset < basicBlockStartOffset)
2897 // is encountered when op_profile_control_flow lies across the boundary of these duplicated bytecode basic
2898 // blocks and the textual offset goes from the end of the duplicated block back to the beginning. These
2899 // ranges are dummy ranges and are ignored. The duplicated bytecode basic blocks point to the same
2900 // internal data structure, so if any of them execute, it will record the same textual basic block in the
2901 // JavaScript program as executing.
2902 // At the bytecode level, this situation looks like:
2903 // j: op_profile_control_flow (from j->k, we have basicBlockEndOffset < basicBlockStartOffset)
2905 // k: op_profile_control_flow (we want to skip over the j->k block and start fresh at offset k as the start of a new basic block k->m).
2907 // m: op_profile_control_flow
2908 if (basicBlockEndOffset < basicBlockStartOffset) {
2909 RELEASE_ASSERT(i + 1 < offsetsLength); // We should never encounter dummy blocks at the end of a CodeBlock.
2910 instructions[startIdx + 1].u.basicBlockLocation = vm()->controlFlowProfiler()->dummyBasicBlock();
2914 BasicBlockLocation* basicBlockLocation = vm()->controlFlowProfiler()->getBasicBlockLocation(ownerScriptExecutable()->sourceID(), basicBlockStartOffset, basicBlockEndOffset);
2916 // Find all functions that are enclosed within the range: [basicBlockStartOffset, basicBlockEndOffset]
2917 // and insert these functions' start/end offsets as gaps in the current BasicBlockLocation.
2918 // This is necessary because in the original source text of a JavaScript program,
2919 // function literals form new basic blocks boundaries, but they aren't represented
2920 // inside the CodeBlock's instruction stream.
2921 auto insertFunctionGaps = [basicBlockLocation, basicBlockStartOffset, basicBlockEndOffset] (const WriteBarrier<FunctionExecutable>& functionExecutable) {
2922 const UnlinkedFunctionExecutable* executable = functionExecutable->unlinkedExecutable();
2923 int functionStart = executable->typeProfilingStartOffset();
2924 int functionEnd = executable->typeProfilingEndOffset();
2925 if (functionStart >= basicBlockStartOffset && functionEnd <= basicBlockEndOffset)
2926 basicBlockLocation->insertGap(functionStart, functionEnd);
2929 for (const WriteBarrier<FunctionExecutable>& executable : m_functionDecls)
2930 insertFunctionGaps(executable);
2931 for (const WriteBarrier<FunctionExecutable>& executable : m_functionExprs)
2932 insertFunctionGaps(executable);
2934 instructions[startIdx + 1].u.basicBlockLocation = basicBlockLocation;
2939 void CodeBlock::setPCToCodeOriginMap(std::unique_ptr<PCToCodeOriginMap>&& map)
2941 m_pcToCodeOriginMap = WTFMove(map);
2944 std::optional<CodeOrigin> CodeBlock::findPC(void* pc)
2946 if (m_pcToCodeOriginMap) {
2947 if (std::optional<CodeOrigin> codeOrigin = m_pcToCodeOriginMap->findPC(pc))
2951 for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter) {
2952 StructureStubInfo* stub = *iter;
2953 if (stub->containsPC(pc))
2954 return std::optional<CodeOrigin>(stub->codeOrigin);
2957 if (std::optional<CodeOrigin> codeOrigin = m_jitCode->findPC(this, pc))
2960 return std::nullopt;
2962 #endif // ENABLE(JIT)
2964 std::optional<unsigned> CodeBlock::bytecodeOffsetFromCallSiteIndex(CallSiteIndex callSiteIndex)
2966 std::optional<unsigned> bytecodeOffset;
2967 JITCode::JITType jitType = this->jitType();
2968 if (jitType == JITCode::InterpreterThunk || jitType == JITCode::BaselineJIT) {
2970 bytecodeOffset = callSiteIndex.bits();
2972 Instruction* instruction = bitwise_cast<Instruction*>(callSiteIndex.bits());
2973 bytecodeOffset = instruction - instructions().begin();
2975 } else if (jitType == JITCode::DFGJIT || jitType == JITCode::FTLJIT) {
2977 RELEASE_ASSERT(canGetCodeOrigin(callSiteIndex));
2978 CodeOrigin origin = codeOrigin(callSiteIndex);
2979 bytecodeOffset = origin.bytecodeIndex;
2981 RELEASE_ASSERT_NOT_REACHED();
2985 return bytecodeOffset;
2988 int32_t CodeBlock::thresholdForJIT(int32_t threshold)
2990 switch (unlinkedCodeBlock()->didOptimize()) {
2994 return threshold * 4;
2996 return threshold / 2;
2998 ASSERT_NOT_REACHED();
3002 void CodeBlock::jitAfterWarmUp()
3004 m_llintExecuteCounter.setNewThreshold(thresholdForJIT(Options::thresholdForJITAfterWarmUp()), this);
3007 void CodeBlock::jitSoon()
3009 m_llintExecuteCounter.setNewThreshold(thresholdForJIT(Options::thresholdForJITSoon()), this);
3012 bool CodeBlock::hasInstalledVMTrapBreakpoints() const
3014 #if ENABLE(SIGNAL_BASED_VM_TRAPS)
3016 // This function may be called from a signal handler. We need to be
3017 // careful to not call anything that is not signal handler safe, e.g.
3018 // we should not perturb the refCount of m_jitCode.
3019 if (!JITCode::isOptimizingJIT(jitType()))
3021 return m_jitCode->dfgCommon()->hasInstalledVMTrapsBreakpoints();
3027 bool CodeBlock::installVMTrapBreakpoints()
3029 #if ENABLE(SIGNAL_BASED_VM_TRAPS)
3030 // This function may be called from a signal handler. We need to be
3031 // careful to not call anything that is not signal handler safe, e.g.
3032 // we should not perturb the refCount of m_jitCode.
3033 if (!JITCode::isOptimizingJIT(jitType()))
3035 m_jitCode->dfgCommon()->installVMTrapBreakpoints();
3042 void CodeBlock::dumpMathICStats()
3044 #if ENABLE(MATH_IC_STATS)
3045 double numAdds = 0.0;
3046 double totalAddSize = 0.0;
3047 double numMuls = 0.0;
3048 double totalMulSize = 0.0;
3049 double numNegs = 0.0;
3050 double totalNegSize = 0.0;
3051 double numSubs = 0.0;
3052 double totalSubSize = 0.0;
3054 auto countICs = [&] (CodeBlock* codeBlock) {
3055 for (JITAddIC* addIC : codeBlock->m_addICs) {
3057 totalAddSize += addIC->codeSize();
3060 for (JITMulIC* mulIC : codeBlock->m_mulICs) {
3062 totalMulSize += mulIC->codeSize();
3065 for (JITNegIC* negIC : codeBlock->m_negICs) {
3067 totalNegSize += negIC->codeSize();
3070 for (JITSubIC* subIC : codeBlock->m_subICs) {
3072 totalSubSize += subIC->codeSize();
3077 heap()->forEachCodeBlock(countICs);
3079 dataLog("Num Adds: ", numAdds, "\n");
3080 dataLog("Total Add size in bytes: ", totalAddSize, "\n");
3081 dataLog("Average Add size: ", totalAddSize / numAdds, "\n");
3083 dataLog("Num Muls: ", numMuls, "\n");
3084 dataLog("Total Mul size in bytes: ", totalMulSize, "\n");
3085 dataLog("Average Mul size: ", totalMulSize / numMuls, "\n");
3087 dataLog("Num Negs: ", numNegs, "\n");
3088 dataLog("Total Neg size in bytes: ", totalNegSize, "\n");
3089 dataLog("Average Neg size: ", totalNegSize / numNegs, "\n");
3091 dataLog("Num Subs: ", numSubs, "\n");
3092 dataLog("Total Sub size in bytes: ", totalSubSize, "\n");
3093 dataLog("Average Sub size: ", totalSubSize / numSubs, "\n");
3095 dataLog("-----------------------\n");
3099 BytecodeLivenessAnalysis& CodeBlock::livenessAnalysisSlow()
3101 std::unique_ptr<BytecodeLivenessAnalysis> analysis = std::make_unique<BytecodeLivenessAnalysis>(this);
3103 ConcurrentJSLocker locker(m_lock);
3104 if (!m_livenessAnalysis)
3105 m_livenessAnalysis = WTFMove(analysis);
3106 return *m_livenessAnalysis;