9a304a7f8479f7687cbc8b282460bd810aa82dac
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGDriver.cpp
1 /*
2  * Copyright (C) 2011-2014, 2016 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 "TypeProfilerLog.h"
42 #include <wtf/Atomics.h>
43 #include <wtf/NeverDestroyed.h>
44
45 #if ENABLE(FTL_JIT)
46 #include "FTLThunks.h"
47 #endif
48
49 namespace JSC { namespace DFG {
50
51 static unsigned numCompilations;
52
53 unsigned getNumCompilations()
54 {
55     return numCompilations;
56 }
57
58 #if ENABLE(DFG_JIT)
59 static FunctionWhitelist& ensureGlobalDFGWhitelist()
60 {
61     static LazyNeverDestroyed<FunctionWhitelist> dfgWhitelist;
62     static std::once_flag initializeWhitelistFlag;
63     std::call_once(initializeWhitelistFlag, [] {
64         const char* functionWhitelistFile = Options::dfgWhitelist();
65         dfgWhitelist.construct(functionWhitelistFile);
66     });
67     return dfgWhitelist;
68 }
69
70 static CompilationResult compileImpl(
71     VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, CompilationMode mode,
72     unsigned osrEntryBytecodeIndex, const Operands<JSValue>& mustHandleValues,
73     Ref<DeferredCompilationCallback>&& callback)
74 {
75     if (!Options::bytecodeRangeToDFGCompile().isInRange(codeBlock->instructionCount())
76         || !ensureGlobalDFGWhitelist().contains(codeBlock))
77         return CompilationFailed;
78     
79     numCompilations++;
80     
81     ASSERT(codeBlock);
82     ASSERT(codeBlock->alternative());
83     ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT);
84     ASSERT(!profiledDFGCodeBlock || profiledDFGCodeBlock->jitType() == JITCode::DFGJIT);
85     
86     if (logCompilationChanges(mode))
87         dataLog("DFG(Driver) compiling ", *codeBlock, " with ", mode, ", number of instructions = ", codeBlock->instructionCount(), "\n");
88     
89     // Make sure that any stubs that the DFG is going to use are initialized. We want to
90     // make sure that all JIT code generation does finalization on the main thread.
91     vm.getCTIStub(osrExitGenerationThunkGenerator);
92     vm.getCTIStub(throwExceptionFromCallSlowPathGenerator);
93     vm.getCTIStub(linkCallThunkGenerator);
94     vm.getCTIStub(linkPolymorphicCallThunkGenerator);
95     
96     if (vm.typeProfiler())
97         vm.typeProfilerLog()->processLogEntries(ASCIILiteral("Preparing for DFG compilation."));
98     
99     Ref<Plan> plan = adoptRef(
100         *new Plan(codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues));
101     
102     plan->callback = WTFMove(callback);
103     if (Options::useConcurrentJIT()) {
104         Worklist& worklist = ensureGlobalWorklistFor(mode);
105         if (logCompilationChanges(mode))
106             dataLog("Deferring DFG compilation of ", *codeBlock, " with queue length ", worklist.queueLength(), ".\n");
107         worklist.enqueue(WTFMove(plan));
108         return CompilationDeferred;
109     }
110     
111     plan->compileInThread(nullptr);
112     return plan->finalizeWithoutNotifyingCallback();
113 }
114 #else // ENABLE(DFG_JIT)
115 static CompilationResult compileImpl(
116     VM&, CodeBlock*, CodeBlock*, CompilationMode, unsigned, const Operands<JSValue>&,
117     Ref<DeferredCompilationCallback>&&)
118 {
119     return CompilationFailed;
120 }
121 #endif // ENABLE(DFG_JIT)
122
123 CompilationResult compile(
124     VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, CompilationMode mode,
125     unsigned osrEntryBytecodeIndex, const Operands<JSValue>& mustHandleValues,
126     Ref<DeferredCompilationCallback>&& callback)
127 {
128     CompilationResult result = compileImpl(
129         vm, codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues,
130         callback.copyRef());
131     if (result != CompilationDeferred)
132         callback->compilationDidComplete(codeBlock, profiledDFGCodeBlock, result);
133     return result;
134 }
135
136 } } // namespace JSC::DFG