2 * Copyright (C) 2011 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"
36 namespace JSC { namespace DFG {
38 void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex)
40 #if DFG_ENABLE(OSR_ENTRY)
41 ASSERT(codeBlock->getJITType() == JITCode::DFGJIT);
42 ASSERT(codeBlock->alternative());
43 ASSERT(codeBlock->alternative()->getJITType() == JITCode::BaselineJIT);
44 ASSERT(!codeBlock->jitCodeMap());
46 #if ENABLE(JIT_VERBOSE_OSR)
47 dataLogF("OSR in %p(%p) from bc#%u\n", codeBlock, codeBlock->alternative(), bytecodeIndex);
50 JSGlobalData* globalData = &exec->globalData();
51 OSREntryData* entry = codeBlock->dfgOSREntryDataForBytecodeIndex(bytecodeIndex);
54 #if ENABLE(JIT_VERBOSE_OSR)
55 dataLogF(" OSR failed because the entrypoint was optimized out.\n");
60 ASSERT(entry->m_bytecodeIndex == bytecodeIndex);
62 // The code below checks if it is safe to perform OSR entry. It may find
63 // that it is unsafe to do so, for any number of reasons, which are documented
64 // below. If the code decides not to OSR then it returns 0, and it's the caller's
65 // responsibility to patch up the state in such a way as to ensure that it's
66 // both safe and efficient to continue executing baseline code for now. This
67 // should almost certainly include calling either codeBlock->optimizeAfterWarmUp()
68 // or codeBlock->dontOptimizeAnytimeSoon().
70 // 1) Verify predictions. If the predictions are inconsistent with the actual
71 // values, then OSR entry is not possible at this time. It's tempting to
72 // assume that we could somehow avoid this case. We can certainly avoid it
73 // for first-time loop OSR - that is, OSR into a CodeBlock that we have just
74 // compiled. Then we are almost guaranteed that all of the predictions will
75 // check out. It would be pretty easy to make that a hard guarantee. But
76 // then there would still be the case where two call frames with the same
77 // baseline CodeBlock are on the stack at the same time. The top one
78 // triggers compilation and OSR. In that case, we may no longer have
79 // accurate value profiles for the one deeper in the stack. Hence, when we
80 // pop into the CodeBlock that is deeper on the stack, we might OSR and
81 // realize that the predictions are wrong. Probably, in most cases, this is
82 // just an anomaly in the sense that the older CodeBlock simply went off
83 // into a less-likely path. So, the wisest course of action is to simply not
86 for (size_t argument = 0; argument < entry->m_expectedValues.numberOfArguments(); ++argument) {
87 if (argument >= exec->argumentCountIncludingThis()) {
88 #if ENABLE(JIT_VERBOSE_OSR)
89 dataLogF(" OSR failed because argument %zu was not passed, expected ", argument);
90 entry->m_expectedValues.argument(argument).dump(WTF::dataFile());
98 value = exec->hostThisValue();
100 value = exec->argument(argument - 1);
102 if (!entry->m_expectedValues.argument(argument).validate(value)) {
103 #if ENABLE(JIT_VERBOSE_OSR)
104 dataLogF(" OSR failed because argument %zu is %s, expected ", argument, value.description());
105 entry->m_expectedValues.argument(argument).dump(WTF::dataFile());
112 for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
113 if (entry->m_localsForcedDouble.get(local)) {
114 if (!exec->registers()[local].jsValue().isNumber()) {
115 #if ENABLE(JIT_VERBOSE_OSR)
116 dataLogF(" OSR failed because variable %zu is %s, expected number.\n", local, exec->registers()[local].jsValue().description());
122 if (!entry->m_expectedValues.local(local).validate(exec->registers()[local].jsValue())) {
123 #if ENABLE(JIT_VERBOSE_OSR)
124 dataLogF(" OSR failed because variable %zu is %s, expected ", local, exec->registers()[local].jsValue().description());
125 entry->m_expectedValues.local(local).dump(WTF::dataFile());
132 // 2) Check the stack height. The DFG JIT may require a taller stack than the
133 // baseline JIT, in some cases. If we can't grow the stack, then don't do
134 // OSR right now. That's the only option we have unless we want basic block
135 // boundaries to start throwing RangeErrors. Although that would be possible,
136 // it seems silly: you'd be diverting the program to error handling when it
137 // would have otherwise just kept running albeit less quickly.
139 if (!globalData->interpreter->stack().grow(&exec->registers()[codeBlock->m_numCalleeRegisters])) {
140 #if ENABLE(JIT_VERBOSE_OSR)
141 dataLogF(" OSR failed because stack growth failed.\n");
146 #if ENABLE(JIT_VERBOSE_OSR)
147 dataLogF(" OSR should succeed.\n");
150 // 3) Perform data format conversions.
151 for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
152 if (entry->m_localsForcedDouble.get(local))
153 *bitwise_cast<double*>(exec->registers() + local) = exec->registers()[local].jsValue().asNumber();
156 // 4) Fix the call frame.
158 exec->setCodeBlock(codeBlock);
160 // 5) Find and return the destination machine code address.
162 void* result = codeBlock->getJITCode().executableAddressAtOffset(entry->m_machineCodeOffset);
164 #if ENABLE(JIT_VERBOSE_OSR)
165 dataLogF(" OSR returning machine code address %p.\n", result);
169 #else // DFG_ENABLE(OSR_ENTRY)
171 UNUSED_PARAM(codeBlock);
172 UNUSED_PARAM(bytecodeIndex);
177 } } // namespace JSC::DFG
179 #endif // ENABLE(DFG_JIT)