#include "config.h"
#include "DFGPlan.h"
#if ENABLE(DFG_JIT)
#include "DFGArgumentsEliminationPhase.h"
#include "DFGBackwardsPropagationPhase.h"
#include "DFGByteCodeParser.h"
#include "DFGCFAPhase.h"
#include "DFGCFGSimplificationPhase.h"
#include "DFGCPSRethreadingPhase.h"
#include "DFGCSEPhase.h"
#include "DFGCleanUpPhase.h"
#include "DFGConstantFoldingPhase.h"
#include "DFGConstantHoistingPhase.h"
#include "DFGCriticalEdgeBreakingPhase.h"
#include "DFGDCEPhase.h"
#include "DFGFailedFinalizer.h"
#include "DFGFixupPhase.h"
#include "DFGGraphSafepoint.h"
#include "DFGIntegerCheckCombiningPhase.h"
#include "DFGIntegerRangeOptimizationPhase.h"
#include "DFGInvalidationPointInjectionPhase.h"
#include "DFGJITCompiler.h"
#include "DFGLICMPhase.h"
#include "DFGLivenessAnalysisPhase.h"
#include "DFGLoopPreHeaderCreationPhase.h"
#include "DFGMovHintRemovalPhase.h"
#include "DFGOSRAvailabilityAnalysisPhase.h"
#include "DFGOSREntrypointCreationPhase.h"
#include "DFGObjectAllocationSinkingPhase.h"
#include "DFGPhantomInsertionPhase.h"
#include "DFGPredictionInjectionPhase.h"
#include "DFGPredictionPropagationPhase.h"
#include "DFGPutStackSinkingPhase.h"
#include "DFGSSAConversionPhase.h"
#include "DFGSSALoweringPhase.h"
#include "DFGStackLayoutPhase.h"
#include "DFGStaticExecutionCountEstimationPhase.h"
#include "DFGStoreBarrierInsertionPhase.h"
#include "DFGStrengthReductionPhase.h"
#include "DFGStructureRegistrationPhase.h"
#include "DFGTierUpCheckInjectionPhase.h"
#include "DFGTypeCheckHoistingPhase.h"
#include "DFGUnificationPhase.h"
#include "DFGValidate.h"
#include "DFGVarargsForwardingPhase.h"
#include "DFGVirtualRegisterAllocationPhase.h"
#include "DFGWatchpointCollectionPhase.h"
#include "Debugger.h"
#include "JSCInlines.h"
#include "OperandsInlines.h"
#include "ProfilerDatabase.h"
#include "TrackedReferences.h"
#include <wtf/CurrentTime.h>
#if ENABLE(FTL_JIT)
#include "FTLCapabilities.h"
#include "FTLCompile.h"
#include "FTLFail.h"
#include "FTLLink.h"
#include "FTLLowerDFGToLLVM.h"
#include "FTLState.h"
#include "InitializeLLVM.h"
#endif
namespace JSC { namespace DFG {
static void dumpAndVerifyGraph(Graph& graph, const char* text, bool forceDump = false)
{
GraphDumpMode modeForFinalValidate = DumpGraph;
if (verboseCompilationEnabled(graph.m_plan.mode) || forceDump) {
dataLog(text, "\n");
graph.dump();
modeForFinalValidate = DontDumpGraph;
}
if (validationEnabled())
validate(graph, modeForFinalValidate);
}
static Profiler::CompilationKind profilerCompilationKindForMode(CompilationMode mode)
{
switch (mode) {
case InvalidCompilationMode:
RELEASE_ASSERT_NOT_REACHED();
return Profiler::DFG;
case DFGMode:
return Profiler::DFG;
case FTLMode:
return Profiler::FTL;
case FTLForOSREntryMode:
return Profiler::FTLForOSREntry;
}
RELEASE_ASSERT_NOT_REACHED();
return Profiler::DFG;
}
Plan::Plan(PassRefPtr<CodeBlock> passedCodeBlock, CodeBlock* profiledDFGCodeBlock,
CompilationMode mode, unsigned osrEntryBytecodeIndex,
const Operands<JSValue>& mustHandleValues)
: vm(*passedCodeBlock->vm())
, codeBlock(passedCodeBlock)
, profiledDFGCodeBlock(profiledDFGCodeBlock)
, mode(mode)
, osrEntryBytecodeIndex(osrEntryBytecodeIndex)
, mustHandleValues(mustHandleValues)
, compilation(codeBlock->vm()->m_perBytecodeProfiler ? adoptRef(new Profiler::Compilation(codeBlock->vm()->m_perBytecodeProfiler->ensureBytecodesFor(codeBlock.get()), profilerCompilationKindForMode(mode))) : 0)
, inlineCallFrames(adoptRef(new InlineCallFrameSet()))
, identifiers(codeBlock.get())
, weakReferences(codeBlock.get())
, willTryToTierUp(false)
, stage(Preparing)
{
}
Plan::~Plan()
{
}
bool Plan::reportCompileTimes() const
{
return Options::reportCompileTimes()
|| (Options::reportFTLCompileTimes() && isFTL(mode));
}
void Plan::compileInThread(LongLivedState& longLivedState, ThreadData* threadData)
{
this->threadData = threadData;
double before = 0;
CString codeBlockName;
if (reportCompileTimes()) {
before = monotonicallyIncreasingTimeMS();
codeBlockName = toCString(*codeBlock);
}
SamplingRegion samplingRegion("DFG Compilation (Plan)");
CompilationScope compilationScope;
if (logCompilationChanges(mode))
dataLog("DFG(Plan) compiling ", *codeBlock, " with ", mode, ", number of instructions = ", codeBlock->instructionCount(), "\n");
CompilationPath path = compileInThreadImpl(longLivedState);
RELEASE_ASSERT(path == CancelPath || finalizer);
RELEASE_ASSERT((path == CancelPath) == (stage == Cancelled));
if (reportCompileTimes()) {
const char* pathName;
switch (path) {
case FailPath:
pathName = "N/A (fail)";
break;
case DFGPath:
pathName = "DFG";
break;
case FTLPath:
pathName = "FTL";
break;
case CancelPath:
pathName = "Cancelled";
break;
default:
RELEASE_ASSERT_NOT_REACHED();
#if COMPILER_QUIRK(CONSIDERS_UNREACHABLE_CODE)
pathName = "";
#endif
break;
}
double now = monotonicallyIncreasingTimeMS();
dataLog("Optimized ", codeBlockName, " using ", mode, " with ", pathName, " into ", finalizer ? finalizer->codeSize() : 0, " bytes in ", now - before, " ms");
if (path == FTLPath)
dataLog(" (DFG: ", m_timeBeforeFTL - before, ", LLVM: ", now - m_timeBeforeFTL, ")");
dataLog(".\n");
}
}
Plan::CompilationPath Plan::compileInThreadImpl(LongLivedState& longLivedState)
{
if (verboseCompilationEnabled(mode) && osrEntryBytecodeIndex != UINT_MAX) {
dataLog("\n");
dataLog("Compiler must handle OSR entry from bc#", osrEntryBytecodeIndex, " with values: ", mustHandleValues, "\n");
dataLog("\n");
}
Graph dfg(vm, *this, longLivedState);
if (!parse(dfg)) {
finalizer = std::make_unique<FailedFinalizer>(*this);
return FailPath;
}
codeBlock->shrinkToFit(CodeBlock::EarlyShrink);
if (validationEnabled())
validate(dfg);
if (Options::dumpGraphAfterParsing()) {
dataLog("Graph after parsing:\n");
dfg.dump();
}
performCPSRethreading(dfg);
performUnification(dfg);
performPredictionInjection(dfg);
performStaticExecutionCountEstimation(dfg);
if (mode == FTLForOSREntryMode) {
bool result = performOSREntrypointCreation(dfg);
if (!result) {
finalizer = std::make_unique<FailedFinalizer>(*this);
return FailPath;
}
performCPSRethreading(dfg);
}
if (validationEnabled())
validate(dfg);
performBackwardsPropagation(dfg);
performPredictionPropagation(dfg);
performFixup(dfg);
performStructureRegistration(dfg);
performInvalidationPointInjection(dfg);
performTypeCheckHoisting(dfg);
dfg.m_fixpointState = FixpointNotConverged;
if (validationEnabled())
validate(dfg);
performStrengthReduction(dfg);
performLocalCSE(dfg);
performCPSRethreading(dfg);
performCFA(dfg);
performConstantFolding(dfg);
bool changed = false;
changed |= performCFGSimplification(dfg);
changed |= performLocalCSE(dfg);
if (validationEnabled())
validate(dfg);
performCPSRethreading(dfg);
if (!isFTL(mode)) {
changed |= performVarargsForwarding(dfg); }
if (changed) {
performCFA(dfg);
performConstantFolding(dfg);
}
if (validationEnabled()) {
dfg.m_dominators.computeIfNecessary(dfg);
dfg.m_naturalLoops.computeIfNecessary(dfg);
dfg.m_prePostNumbering.computeIfNecessary(dfg);
}
switch (mode) {
case DFGMode: {
dfg.m_fixpointState = FixpointConverged;
performTierUpCheckInjection(dfg);
performFastStoreBarrierInsertion(dfg);
performCleanUp(dfg);
performCPSRethreading(dfg);
performDCE(dfg);
performPhantomInsertion(dfg);
performStackLayout(dfg);
performVirtualRegisterAllocation(dfg);
performWatchpointCollection(dfg);
dumpAndVerifyGraph(dfg, "Graph after optimization:");
JITCompiler dataFlowJIT(dfg);
if (codeBlock->codeType() == FunctionCode)
dataFlowJIT.compileFunction();
else
dataFlowJIT.compile();
return DFGPath;
}
case FTLMode:
case FTLForOSREntryMode: {
#if ENABLE(FTL_JIT)
if (FTL::canCompile(dfg) == FTL::CannotCompile) {
finalizer = std::make_unique<FailedFinalizer>(*this);
return FailPath;
}
performCleanUp(dfg); performCriticalEdgeBreaking(dfg);
performLoopPreHeaderCreation(dfg);
performCPSRethreading(dfg);
performSSAConversion(dfg);
performSSALowering(dfg);
performArgumentsElimination(dfg);
if (Options::usePutStackSinking())
performPutStackSinking(dfg);
performConstantHoisting(dfg);
performGlobalCSE(dfg);
performLivenessAnalysis(dfg);
performIntegerRangeOptimization(dfg);
performLivenessAnalysis(dfg);
performCFA(dfg);
performConstantFolding(dfg);
performCleanUp(dfg); changed = false;
changed |= performStrengthReduction(dfg);
if (Options::enableObjectAllocationSinking()) {
changed |= performCriticalEdgeBreaking(dfg);
changed |= performObjectAllocationSinking(dfg);
}
if (changed) {
performLivenessAnalysis(dfg);
performCFA(dfg);
performConstantFolding(dfg);
}
performLICM(dfg);
performCleanUp(dfg);
performIntegerCheckCombining(dfg);
performGlobalCSE(dfg);
dfg.m_fixpointState = FixpointConverged;
performLivenessAnalysis(dfg);
performCFA(dfg);
performGlobalStoreBarrierInsertion(dfg);
if (Options::enableMovHintRemoval())
performMovHintRemoval(dfg);
performCleanUp(dfg);
performDCE(dfg); performStackLayout(dfg);
performLivenessAnalysis(dfg);
performOSRAvailabilityAnalysis(dfg);
performWatchpointCollection(dfg);
if (FTL::canCompile(dfg) == FTL::CannotCompile) {
finalizer = std::make_unique<FailedFinalizer>(*this);
return FailPath;
}
dumpAndVerifyGraph(dfg, "Graph just before FTL lowering:", shouldShowDisassembly(mode));
bool haveLLVM;
Safepoint::Result safepointResult;
{
GraphSafepoint safepoint(dfg, safepointResult);
haveLLVM = initializeLLVM();
}
if (safepointResult.didGetCancelled())
return CancelPath;
if (!haveLLVM) {
if (Options::ftlCrashesIfCantInitializeLLVM()) {
dataLog("LLVM can't be initialized.\n");
CRASH();
}
finalizer = std::make_unique<FailedFinalizer>(*this);
return FailPath;
}
FTL::State state(dfg);
FTL::lowerDFGToLLVM(state);
if (reportCompileTimes())
m_timeBeforeFTL = monotonicallyIncreasingTimeMS();
if (Options::llvmAlwaysFailsBeforeCompile()) {
FTL::fail(state);
return FTLPath;
}
FTL::compile(state, safepointResult);
if (safepointResult.didGetCancelled())
return CancelPath;
if (Options::llvmAlwaysFailsBeforeLink()) {
FTL::fail(state);
return FTLPath;
}
if (state.allocationFailed) {
FTL::fail(state);
return FTLPath;
}
if (state.jitCode->stackmaps.stackSize() > Options::llvmMaxStackSize()) {
FTL::fail(state);
return FTLPath;
}
FTL::link(state);
if (state.allocationFailed) {
FTL::fail(state);
return FTLPath;
}
return FTLPath;
#else
RELEASE_ASSERT_NOT_REACHED();
return FailPath;
#endif // ENABLE(FTL_JIT)
}
default:
RELEASE_ASSERT_NOT_REACHED();
return FailPath;
}
}
bool Plan::isStillValid()
{
CodeBlock* replacement = codeBlock->replacement();
if (!replacement)
return false;
if (codeBlock->alternative() != replacement->baselineVersion())
return false;
if (!watchpoints.areStillValid())
return false;
return true;
}
void Plan::reallyAdd(CommonData* commonData)
{
watchpoints.reallyAdd(codeBlock.get(), *commonData);
identifiers.reallyAdd(vm, commonData);
weakReferences.reallyAdd(vm, commonData);
transitions.reallyAdd(vm, commonData);
writeBarriers.trigger(vm);
}
void Plan::notifyCompiling()
{
stage = Compiling;
}
void Plan::notifyCompiled()
{
stage = Compiled;
}
void Plan::notifyReady()
{
callback->compilationDidBecomeReadyAsynchronously(codeBlock.get());
stage = Ready;
}
CompilationResult Plan::finalizeWithoutNotifyingCallback()
{
if (!isStillValid())
return CompilationInvalidated;
bool result;
if (codeBlock->codeType() == FunctionCode)
result = finalizer->finalizeFunction();
else
result = finalizer->finalize();
if (!result)
return CompilationFailed;
reallyAdd(codeBlock->jitCode()->dfgCommon());
if (validationEnabled()) {
TrackedReferences trackedReferences;
for (WriteBarrier<JSCell>& reference : codeBlock->jitCode()->dfgCommon()->weakReferences)
trackedReferences.add(reference.get());
for (WriteBarrier<Structure>& reference : codeBlock->jitCode()->dfgCommon()->weakStructureReferences)
trackedReferences.add(reference.get());
for (WriteBarrier<Unknown>& constant : codeBlock->constants())
trackedReferences.add(constant.get());
codeBlock->jitCode()->validateReferences(trackedReferences);
}
return CompilationSuccessful;
}
void Plan::finalizeAndNotifyCallback()
{
callback->compilationDidComplete(codeBlock.get(), finalizeWithoutNotifyingCallback());
}
CompilationKey Plan::key()
{
return CompilationKey(codeBlock->alternative(), mode);
}
void Plan::checkLivenessAndVisitChildren(SlotVisitor& visitor, CodeBlockSet& codeBlocks)
{
if (!isKnownToBeLiveDuringGC())
return;
for (unsigned i = mustHandleValues.size(); i--;)
visitor.appendUnbarrieredValue(&mustHandleValues[i]);
codeBlocks.mark(codeBlock->alternative());
codeBlocks.mark(codeBlock.get());
codeBlocks.mark(profiledDFGCodeBlock.get());
weakReferences.visitChildren(visitor);
writeBarriers.visitChildren(visitor);
transitions.visitChildren(visitor);
}
bool Plan::isKnownToBeLiveDuringGC()
{
if (stage == Cancelled)
return false;
if (!Heap::isMarked(codeBlock->ownerExecutable()))
return false;
if (!codeBlock->alternative()->isKnownToBeLiveDuringGC())
return false;
if (!!profiledDFGCodeBlock && !profiledDFGCodeBlock->isKnownToBeLiveDuringGC())
return false;
return true;
}
void Plan::cancel()
{
codeBlock = nullptr;
profiledDFGCodeBlock = nullptr;
mustHandleValues.clear();
compilation = nullptr;
finalizer = nullptr;
inlineCallFrames = nullptr;
watchpoints = DesiredWatchpoints();
identifiers = DesiredIdentifiers();
weakReferences = DesiredWeakReferences();
writeBarriers = DesiredWriteBarriers();
transitions = DesiredTransitions();
callback = nullptr;
stage = Cancelled;
}
} }
#endif // ENABLE(DFG_JIT)