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 inline bool predictionIsValid(JSGlobalData* globalData, JSValue value, PredictedType type)
40 // this takes into account only local variable predictions that get enforced
43 if (isInt32Prediction(type))
44 return value.isInt32();
46 if (isArrayPrediction(type))
47 return isJSArray(globalData, value);
49 if (isBooleanPrediction(type))
50 return value.isBoolean();
55 void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex)
57 #if ENABLE(DFG_OSR_ENTRY)
58 ASSERT(codeBlock->getJITType() == JITCode::DFGJIT);
59 ASSERT(codeBlock->alternative());
60 ASSERT(codeBlock->alternative()->getJITType() == JITCode::BaselineJIT);
62 #if ENABLE(JIT_VERBOSE_OSR)
63 printf("OSR in %p(%p) from bc#%u\n", codeBlock, codeBlock->alternative(), bytecodeIndex);
66 JSGlobalData* globalData = &exec->globalData();
67 CodeBlock* baselineCodeBlock = codeBlock->alternative();
69 // The code below checks if it is safe to perform OSR entry. It may find
70 // that it is unsafe to do so, for any number of reasons, which are documented
71 // below. If the code decides not to OSR then it returns 0, and it's the caller's
72 // responsibility to patch up the state in such a way as to ensure that it's
73 // both safe and efficient to continue executing baseline code for now. This
74 // should almost certainly include calling either codeBlock->optimizeAfterWarmUp()
75 // or codeBlock->dontOptimizeAnytimeSoon().
77 // 1) Check if the DFG code set a code map. If it didn't, it means that it
78 // cannot handle OSR entry. This currently only happens if we disable
79 // dynamic speculation termination and end up with a DFG code block that
80 // was compiled entirely with the non-speculative JIT. The non-speculative
81 // JIT does not support OSR entry and probably never will, since it is
82 // kind of a deprecated compiler right now.
84 #if ENABLE(DYNAMIC_TERMINATE_SPECULATION)
85 ASSERT(codeBlock->jitCodeMap());
87 if (!codeBlock->jitCodeMap()) {
88 #if ENABLE(JIT_VERBOSE_OSR)
89 printf(" OSR failed because of a missing JIT code map.\n");
95 // 2) Verify predictions. If the predictions are inconsistent with the actual
96 // values, then OSR entry is not possible at this time. It's tempting to
97 // assume that we could somehow avoid this case. We can certainly avoid it
98 // for first-time loop OSR - that is, OSR into a CodeBlock that we have just
99 // compiled. Then we are almost guaranteed that all of the predictions will
100 // check out. It would be pretty easy to make that a hard guarantee. But
101 // then there would still be the case where two call frames with the same
102 // baseline CodeBlock are on the stack at the same time. The top one
103 // triggers compilation and OSR. In that case, we may no longer have
104 // accurate value profiles for the one deeper in the stack. Hence, when we
105 // pop into the CodeBlock that is deeper on the stack, we might OSR and
106 // realize that the predictions are wrong. Probably, in most cases, this is
107 // just an anomaly in the sense that the older CodeBlock simply went off
108 // into a less-likely path. So, the wisest course of action is to simply not
111 PredictionTracker* predictions = baselineCodeBlock->predictions();
113 if (predictions->numberOfArguments() > exec->argumentCountIncludingThis())
116 for (unsigned i = 1; i < predictions->numberOfArguments(); ++i) {
117 if (!predictionIsValid(globalData, exec->argument(i - 1), predictions->getArgumentPrediction(i))) {
118 #if ENABLE(JIT_VERBOSE_OSR)
119 printf(" OSR failed because argument %u is %s, expected %s.\n", i, exec->argument(i - 1).description(), predictionToString(predictions->getArgumentPrediction(i)));
125 // FIXME: we need to know if at an OSR entry, a variable is live. If it isn't
126 // then we shouldn't try to verify its prediction.
128 for (unsigned i = 0; i < predictions->numberOfVariables(); ++i) {
129 if (!predictionIsValid(globalData, exec->registers()[i].jsValue(), predictions->getPrediction(i))) {
130 #if ENABLE(JIT_VERBOSE_OSR)
131 printf(" OSR failed because variable %u is %s, expected %s.\n", i, exec->registers()[i].jsValue().description(), predictionToString(predictions->getPrediction(i)));
137 // 3) Check the stack height. The DFG JIT may require a taller stack than the
138 // baseline JIT, in some cases. If we can't grow the stack, then don't do
139 // OSR right now. That's the only option we have unless we want basic block
140 // boundaries to start throwing RangeErrors. Although that would be possible,
141 // it seems silly: you'd be diverting the program to error handling when it
142 // would have otherwise just kept running albeit less quickly.
144 if (!globalData->interpreter->registerFile().grow(&exec->registers()[codeBlock->m_numCalleeRegisters])) {
145 #if ENABLE(JIT_VERBOSE_OSR)
146 printf(" OSR failed because stack growth failed..\n");
151 #if ENABLE(JIT_VERBOSE_OSR)
152 printf(" OSR should succeed.\n");
155 // 4) Fix the call frame.
157 exec->setCodeBlock(codeBlock);
159 // 5) Find and return the destination machine code address. The DFG stores
160 // the machine code offsets of OSR targets in a CompactJITCodeMap.
161 // Decoding it is not super efficient, but we expect that OSR entry
162 // happens sufficiently rarely, and that OSR entrypoints are sufficiently
163 // few, that this won't hurt throughput. Note that the only real
164 // reason why we use a CompactJITCodeMap is to avoid having to introduce
165 // yet another data structure for mapping between bytecode indices and
166 // machine code offsets.
168 CompactJITCodeMap::Decoder decoder(codeBlock->jitCodeMap());
169 unsigned machineCodeOffset = std::numeric_limits<unsigned>::max();
170 while (decoder.numberOfEntriesRemaining()) {
171 unsigned currentBytecodeIndex;
172 unsigned currentMachineCodeOffset;
173 decoder.read(currentBytecodeIndex, currentMachineCodeOffset);
174 if (currentBytecodeIndex == bytecodeIndex) {
175 machineCodeOffset = currentMachineCodeOffset;
180 ASSERT(machineCodeOffset != std::numeric_limits<unsigned>::max());
182 void* result = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(codeBlock->getJITCode().start()) + machineCodeOffset);
184 #if ENABLE(JIT_VERBOSE_OSR)
185 printf(" OSR returning machine code address %p.\n", result);
189 #else // ENABLE(DFG_OSR_ENTRY)
191 UNUSED_PARAM(codeBlock);
192 UNUSED_PARAM(bytecodeIndex);
197 } } // namespace JSC::DFG
199 #endif // ENABLE(DFG_JIT)