2 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "DFGOSREntry.h"
31 #include "CallFrame.h"
32 #include "CodeBlock.h"
33 #include "DFGJITCode.h"
36 #include "Operations.h"
38 namespace JSC { namespace DFG {
40 void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex)
42 #if DFG_ENABLE(OSR_ENTRY)
43 ASSERT(JITCode::isOptimizingJIT(codeBlock->jitType()));
44 ASSERT(codeBlock->alternative());
45 ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT);
46 ASSERT(!codeBlock->jitCodeMap());
48 if (Options::verboseOSR()) {
50 "DFG OSR in ", *codeBlock->alternative(), " -> ", *codeBlock,
51 " from bc#", bytecodeIndex, "\n");
55 if (codeBlock->jitType() != JITCode::DFGJIT) {
56 RELEASE_ASSERT(codeBlock->jitType() == JITCode::FTLJIT);
58 // When will this happen? We could have:
60 // - An exit from the FTL JIT into the baseline JIT followed by an attempt
61 // to reenter. We're fine with allowing this to fail. If it happens
62 // enough we'll just reoptimize. It basically means that the OSR exit cost
63 // us dearly and so reoptimizing is the right thing to do.
65 // - We have recursive code with hot loops. Consider that foo has a hot loop
66 // that calls itself. We have two foo's on the stack, lets call them foo1
67 // and foo2, with foo1 having called foo2 from foo's hot loop. foo2 gets
68 // optimized all the way into the FTL. Then it returns into foo1, and then
69 // foo1 wants to get optimized. It might reach this conclusion from its
70 // hot loop and attempt to OSR enter. And we'll tell it that it can't. It
71 // might be worth addressing this case, but I just think this case will
72 // be super rare. For now, if it does happen, it'll cause some compilation
75 if (Options::verboseOSR())
76 dataLog(" OSR failed because the target code block is not DFG.\n");
80 OSREntryData* entry = codeBlock->jitCode()->dfg()->osrEntryDataForBytecodeIndex(bytecodeIndex);
83 if (Options::verboseOSR())
84 dataLogF(" OSR failed because the entrypoint was optimized out.\n");
88 ASSERT(entry->m_bytecodeIndex == bytecodeIndex);
90 // The code below checks if it is safe to perform OSR entry. It may find
91 // that it is unsafe to do so, for any number of reasons, which are documented
92 // below. If the code decides not to OSR then it returns 0, and it's the caller's
93 // responsibility to patch up the state in such a way as to ensure that it's
94 // both safe and efficient to continue executing baseline code for now. This
95 // should almost certainly include calling either codeBlock->optimizeAfterWarmUp()
96 // or codeBlock->dontOptimizeAnytimeSoon().
98 // 1) Verify predictions. If the predictions are inconsistent with the actual
99 // values, then OSR entry is not possible at this time. It's tempting to
100 // assume that we could somehow avoid this case. We can certainly avoid it
101 // for first-time loop OSR - that is, OSR into a CodeBlock that we have just
102 // compiled. Then we are almost guaranteed that all of the predictions will
103 // check out. It would be pretty easy to make that a hard guarantee. But
104 // then there would still be the case where two call frames with the same
105 // baseline CodeBlock are on the stack at the same time. The top one
106 // triggers compilation and OSR. In that case, we may no longer have
107 // accurate value profiles for the one deeper in the stack. Hence, when we
108 // pop into the CodeBlock that is deeper on the stack, we might OSR and
109 // realize that the predictions are wrong. Probably, in most cases, this is
110 // just an anomaly in the sense that the older CodeBlock simply went off
111 // into a less-likely path. So, the wisest course of action is to simply not
114 for (size_t argument = 0; argument < entry->m_expectedValues.numberOfArguments(); ++argument) {
115 if (argument >= exec->argumentCountIncludingThis()) {
116 if (Options::verboseOSR()) {
117 dataLogF(" OSR failed because argument %zu was not passed, expected ", argument);
118 entry->m_expectedValues.argument(argument).dump(WTF::dataFile());
126 value = exec->hostThisValue();
128 value = exec->argument(argument - 1);
130 if (!entry->m_expectedValues.argument(argument).validate(value)) {
131 if (Options::verboseOSR()) {
133 " OSR failed because argument ", argument, " is ", value,
134 ", expected ", entry->m_expectedValues.argument(argument), ".\n");
140 for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
141 int localOffset = virtualRegisterForLocal(local).offset();
142 if (entry->m_localsForcedDouble.get(local)) {
143 if (!exec->registers()[localOffset].jsValue().isNumber()) {
144 if (Options::verboseOSR()) {
146 " OSR failed because variable ", localOffset, " is ",
147 exec->registers()[localOffset].jsValue(), ", expected number.\n");
153 if (entry->m_localsForcedMachineInt.get(local)) {
154 if (!exec->registers()[localOffset].jsValue().isMachineInt()) {
155 if (Options::verboseOSR()) {
157 " OSR failed because variable ", localOffset, " is ",
158 exec->registers()[localOffset].jsValue(), ", expected ",
165 if (!entry->m_expectedValues.local(local).validate(exec->registers()[localOffset].jsValue())) {
166 if (Options::verboseOSR()) {
168 " OSR failed because variable ", localOffset, " is ",
169 exec->registers()[localOffset].jsValue(), ", expected ",
170 entry->m_expectedValues.local(local), ".\n");
176 // 2) Check the stack height. The DFG JIT may require a taller stack than the
177 // baseline JIT, in some cases. If we can't grow the stack, then don't do
178 // OSR right now. That's the only option we have unless we want basic block
179 // boundaries to start throwing RangeErrors. Although that would be possible,
180 // it seems silly: you'd be diverting the program to error handling when it
181 // would have otherwise just kept running albeit less quickly.
183 if (!vm->interpreter->stack().grow(&exec->registers()[virtualRegisterForLocal(codeBlock->m_numCalleeRegisters).offset()])) {
184 if (Options::verboseOSR())
185 dataLogF(" OSR failed because stack growth failed.\n");
189 if (Options::verboseOSR())
190 dataLogF(" OSR should succeed.\n");
192 // 3) Perform data format conversions.
193 for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
194 if (entry->m_localsForcedDouble.get(local))
195 *bitwise_cast<double*>(exec->registers() + virtualRegisterForLocal(local).offset()) = exec->registers()[virtualRegisterForLocal(local).offset()].jsValue().asNumber();
196 if (entry->m_localsForcedMachineInt.get(local))
197 *bitwise_cast<int64_t*>(exec->registers() + virtualRegisterForLocal(local).offset()) = exec->registers()[virtualRegisterForLocal(local).offset()].jsValue().asMachineInt() << JSValue::int52ShiftAmount;
200 // 4) Fix the call frame.
202 exec->setCodeBlock(codeBlock);
204 // 5) Find and return the destination machine code address.
206 void* result = codeBlock->jitCode()->executableAddressAtOffset(entry->m_machineCodeOffset);
208 if (Options::verboseOSR())
209 dataLogF(" OSR returning machine code address %p.\n", result);
212 #else // DFG_ENABLE(OSR_ENTRY)
214 UNUSED_PARAM(codeBlock);
215 UNUSED_PARAM(bytecodeIndex);
220 } } // namespace JSC::DFG
222 #endif // ENABLE(DFG_JIT)