2ce1c887b8e90977651e1fdb44b4d4563fe9b295
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGOSRExitCompiler.cpp
1 /*
2  * Copyright (C) 2011 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 "DFGOSRExitCompiler.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "CallFrame.h"
32 #include "DFGCommon.h"
33 #include "LinkBuffer.h"
34 #include "RepatchBuffer.h"
35
36 namespace JSC { namespace DFG {
37
38 extern "C" {
39
40 void compileOSRExit(ExecState* exec)
41 {
42     SamplingRegion samplingRegion("DFG OSR Exit Compilation");
43     
44     CodeBlock* codeBlock = exec->codeBlock();
45     
46     ASSERT(codeBlock);
47     ASSERT(codeBlock->getJITType() == JITCode::DFGJIT);
48     
49     JSGlobalData* globalData = &exec->globalData();
50     
51     uint32_t exitIndex = globalData->osrExitIndex;
52     OSRExit& exit = codeBlock->osrExit(exitIndex);
53     
54     // Make sure all code on our inline stack is JIT compiled. This is necessary since
55     // we may opt to inline a code block even before it had ever been compiled by the
56     // JIT, but our OSR exit infrastructure currently only works if the target of the
57     // OSR exit is JIT code. This could be changed since there is nothing particularly
58     // hard about doing an OSR exit into the interpreter, but for now this seems to make
59     // sense in that if we're OSR exiting from inlined code of a DFG code block, then
60     // probably it's a good sign that the thing we're exiting into is hot. Even more
61     // interestingly, since the code was inlined, it may never otherwise get JIT
62     // compiled since the act of inlining it may ensure that it otherwise never runs.
63     for (CodeOrigin codeOrigin = exit.m_codeOrigin; codeOrigin.inlineCallFrame; codeOrigin = codeOrigin.inlineCallFrame->caller) {
64         static_cast<FunctionExecutable*>(codeOrigin.inlineCallFrame->executable.get())
65             ->baselineCodeBlockFor(codeOrigin.inlineCallFrame->isCall ? CodeForCall : CodeForConstruct)
66             ->jitCompile(exec);
67     }
68     
69     // Compute the value recoveries.
70     Operands<ValueRecovery> operands;
71     codeBlock->variableEventStream().reconstruct(codeBlock, exit.m_codeOrigin, codeBlock->minifiedDFG(), exit.m_streamIndex, operands);
72     
73     // There may be an override, for forward speculations.
74     if (!!exit.m_valueRecoveryOverride) {
75         operands.setOperand(
76             exit.m_valueRecoveryOverride->operand, exit.m_valueRecoveryOverride->recovery);
77     }
78     
79     SpeculationRecovery* recovery = 0;
80     if (exit.m_recoveryIndex)
81         recovery = &codeBlock->speculationRecovery(exit.m_recoveryIndex - 1);
82
83 #if DFG_ENABLE(DEBUG_VERBOSE)
84     dataLog("Generating OSR exit #%u (seq#%u, bc#%u, @%u, %s) for code block %p.\n", exitIndex, exit.m_streamIndex, exit.m_codeOrigin.bytecodeIndex, exit.m_nodeIndex, exitKindToString(exit.m_kind), codeBlock);
85 #endif
86
87     {
88         CCallHelpers jit(globalData, codeBlock);
89         OSRExitCompiler exitCompiler(jit);
90
91         jit.jitAssertHasValidCallFrame();
92         exitCompiler.compileExit(exit, operands, recovery);
93         
94         LinkBuffer patchBuffer(*globalData, &jit, codeBlock);
95         exit.m_code = FINALIZE_CODE_IF(
96             shouldShowDisassembly(),
97             patchBuffer,
98             ("DFG OSR exit #%u (bc#%u, @%u, %s) from CodeBlock %p",
99              exitIndex, exit.m_codeOrigin.bytecodeIndex, exit.m_nodeIndex,
100              exitKindToString(exit.m_kind), codeBlock));
101     }
102     
103     {
104         RepatchBuffer repatchBuffer(codeBlock);
105         repatchBuffer.relink(exit.m_check.codeLocationForRepatch(codeBlock), CodeLocationLabel(exit.m_code.code()));
106     }
107     
108     globalData->osrExitJumpDestination = exit.m_code.code().executableAddress();
109 }
110
111 } // extern "C"
112
113 void OSRExitCompiler::handleExitCounts(const OSRExit& exit)
114 {
115     m_jit.add32(AssemblyHelpers::TrustedImm32(1), AssemblyHelpers::AbsoluteAddress(&exit.m_count));
116     
117     m_jit.move(AssemblyHelpers::TrustedImmPtr(m_jit.codeBlock()), GPRInfo::regT0);
118     
119     AssemblyHelpers::Jump tooFewFails;
120     
121     m_jit.load32(AssemblyHelpers::Address(GPRInfo::regT0, CodeBlock::offsetOfOSRExitCounter()), GPRInfo::regT2);
122     m_jit.add32(AssemblyHelpers::TrustedImm32(1), GPRInfo::regT2);
123     m_jit.store32(GPRInfo::regT2, AssemblyHelpers::Address(GPRInfo::regT0, CodeBlock::offsetOfOSRExitCounter()));
124     m_jit.move(AssemblyHelpers::TrustedImmPtr(m_jit.baselineCodeBlock()), GPRInfo::regT0);
125     tooFewFails = m_jit.branch32(AssemblyHelpers::BelowOrEqual, GPRInfo::regT2, AssemblyHelpers::TrustedImm32(m_jit.codeBlock()->exitCountThresholdForReoptimization()));
126     
127     // Reoptimize as soon as possible.
128 #if !NUMBER_OF_ARGUMENT_REGISTERS
129     m_jit.poke(GPRInfo::regT0);
130 #else
131     m_jit.move(GPRInfo::regT0, GPRInfo::argumentGPR0);
132     ASSERT(GPRInfo::argumentGPR0 != GPRInfo::regT1);
133 #endif
134     m_jit.move(AssemblyHelpers::TrustedImmPtr(bitwise_cast<void*>(triggerReoptimizationNow)), GPRInfo::regT1);
135     m_jit.call(GPRInfo::regT1);
136     AssemblyHelpers::Jump doneAdjusting = m_jit.jump();
137     
138     tooFewFails.link(&m_jit);
139     
140     // Adjust the execution counter such that the target is to only optimize after a while.
141     int32_t targetValue =
142         ExecutionCounter::applyMemoryUsageHeuristicsAndConvertToInt(
143             m_jit.baselineCodeBlock()->counterValueForOptimizeAfterLongWarmUp(),
144             m_jit.baselineCodeBlock());
145     m_jit.store32(AssemblyHelpers::TrustedImm32(-targetValue), AssemblyHelpers::Address(GPRInfo::regT0, CodeBlock::offsetOfJITExecuteCounter()));
146     targetValue = ExecutionCounter::clippedThreshold(m_jit.codeBlock()->globalObject(), targetValue);
147     m_jit.store32(AssemblyHelpers::TrustedImm32(targetValue), AssemblyHelpers::Address(GPRInfo::regT0, CodeBlock::offsetOfJITExecutionActiveThreshold()));
148     m_jit.store32(AssemblyHelpers::TrustedImm32(ExecutionCounter::formattedTotalCount(targetValue)), AssemblyHelpers::Address(GPRInfo::regT0, CodeBlock::offsetOfJITExecutionTotalCount()));
149     
150     doneAdjusting.link(&m_jit);
151 }
152
153 } } // namespace JSC::DFG
154
155 #endif // ENABLE(DFG_JIT)