| 1 | /* |
| 2 | * Copyright (C) 2011-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "DFGDriver.h" |
| 28 | |
| 29 | #include "JSObject.h" |
| 30 | #include "JSString.h" |
| 31 | |
| 32 | #include "CodeBlock.h" |
| 33 | #include "DFGJITCode.h" |
| 34 | #include "DFGPlan.h" |
| 35 | #include "DFGThunks.h" |
| 36 | #include "DFGWorklist.h" |
| 37 | #include "FunctionWhitelist.h" |
| 38 | #include "JITCode.h" |
| 39 | #include "JSCInlines.h" |
| 40 | #include "Options.h" |
| 41 | #include "ThunkGenerators.h" |
| 42 | #include "TypeProfilerLog.h" |
| 43 | #include <wtf/Atomics.h> |
| 44 | #include <wtf/NeverDestroyed.h> |
| 45 | |
| 46 | #if ENABLE(FTL_JIT) |
| 47 | #include "FTLThunks.h" |
| 48 | #endif |
| 49 | |
| 50 | namespace JSC { namespace DFG { |
| 51 | |
| 52 | static unsigned numCompilations; |
| 53 | |
| 54 | unsigned getNumCompilations() |
| 55 | { |
| 56 | return numCompilations; |
| 57 | } |
| 58 | |
| 59 | #if ENABLE(DFG_JIT) |
| 60 | static FunctionWhitelist& ensureGlobalDFGWhitelist() |
| 61 | { |
| 62 | static LazyNeverDestroyed<FunctionWhitelist> dfgWhitelist; |
| 63 | static std::once_flag initializeWhitelistFlag; |
| 64 | std::call_once(initializeWhitelistFlag, [] { |
| 65 | const char* functionWhitelistFile = Options::dfgWhitelist(); |
| 66 | dfgWhitelist.construct(functionWhitelistFile); |
| 67 | }); |
| 68 | return dfgWhitelist; |
| 69 | } |
| 70 | |
| 71 | static CompilationResult compileImpl( |
| 72 | VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, CompilationMode mode, |
| 73 | unsigned osrEntryBytecodeIndex, const Operands<Optional<JSValue>>& mustHandleValues, |
| 74 | Ref<DeferredCompilationCallback>&& callback) |
| 75 | { |
| 76 | if (!Options::bytecodeRangeToDFGCompile().isInRange(codeBlock->instructionsSize()) |
| 77 | || !ensureGlobalDFGWhitelist().contains(codeBlock)) |
| 78 | return CompilationFailed; |
| 79 | |
| 80 | numCompilations++; |
| 81 | |
| 82 | ASSERT(codeBlock); |
| 83 | ASSERT(codeBlock->alternative()); |
| 84 | ASSERT(codeBlock->alternative()->jitType() == JITType::BaselineJIT); |
| 85 | ASSERT(!profiledDFGCodeBlock || profiledDFGCodeBlock->jitType() == JITType::DFGJIT); |
| 86 | |
| 87 | if (logCompilationChanges(mode)) |
| 88 | dataLog("DFG(Driver) compiling " , *codeBlock, " with " , mode, ", instructions size = " , codeBlock->instructionsSize(), "\n" ); |
| 89 | |
| 90 | // Make sure that any stubs that the DFG is going to use are initialized. We want to |
| 91 | // make sure that all JIT code generation does finalization on the main thread. |
| 92 | vm.getCTIStub(arityFixupGenerator); |
| 93 | vm.getCTIStub(osrExitThunkGenerator); |
| 94 | vm.getCTIStub(osrExitGenerationThunkGenerator); |
| 95 | vm.getCTIStub(throwExceptionFromCallSlowPathGenerator); |
| 96 | vm.getCTIStub(linkCallThunkGenerator); |
| 97 | vm.getCTIStub(linkPolymorphicCallThunkGenerator); |
| 98 | |
| 99 | if (vm.typeProfiler()) |
| 100 | vm.typeProfilerLog()->processLogEntries(vm, "Preparing for DFG compilation."_s ); |
| 101 | |
| 102 | Ref<Plan> plan = adoptRef( |
| 103 | *new Plan(codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues)); |
| 104 | |
| 105 | plan->setCallback(WTFMove(callback)); |
| 106 | if (Options::useConcurrentJIT()) { |
| 107 | Worklist& worklist = ensureGlobalWorklistFor(mode); |
| 108 | if (logCompilationChanges(mode)) |
| 109 | dataLog("Deferring DFG compilation of " , *codeBlock, " with queue length " , worklist.queueLength(), ".\n" ); |
| 110 | worklist.enqueue(WTFMove(plan)); |
| 111 | return CompilationDeferred; |
| 112 | } |
| 113 | |
| 114 | plan->compileInThread(nullptr); |
| 115 | return plan->finalizeWithoutNotifyingCallback(); |
| 116 | } |
| 117 | #else // ENABLE(DFG_JIT) |
| 118 | static CompilationResult compileImpl( |
| 119 | VM&, CodeBlock*, CodeBlock*, CompilationMode, unsigned, const Operands<Optional<JSValue>>&, |
| 120 | Ref<DeferredCompilationCallback>&&) |
| 121 | { |
| 122 | return CompilationFailed; |
| 123 | } |
| 124 | #endif // ENABLE(DFG_JIT) |
| 125 | |
| 126 | CompilationResult compile( |
| 127 | VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, CompilationMode mode, |
| 128 | unsigned osrEntryBytecodeIndex, const Operands<Optional<JSValue>>& mustHandleValues, |
| 129 | Ref<DeferredCompilationCallback>&& callback) |
| 130 | { |
| 131 | CompilationResult result = compileImpl( |
| 132 | vm, codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues, |
| 133 | callback.copyRef()); |
| 134 | if (result != CompilationDeferred) |
| 135 | callback->compilationDidComplete(codeBlock, profiledDFGCodeBlock, result); |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | } } // namespace JSC::DFG |
| 140 | |