]> git.saurik.com Git - apple/javascriptcore.git/blame - dfg/DFGOSREntry.cpp
JavaScriptCore-7600.1.4.9.tar.gz
[apple/javascriptcore.git] / dfg / DFGOSREntry.cpp
CommitLineData
6fe7ccc8 1/*
81345200 2 * Copyright (C) 2011, 2013, 2014 Apple Inc. All rights reserved.
6fe7ccc8
A
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 "DFGOSREntry.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "CallFrame.h"
32#include "CodeBlock.h"
81345200 33#include "DFGJITCode.h"
6fe7ccc8
A
34#include "DFGNode.h"
35#include "JIT.h"
81345200
A
36#include "JSStackInlines.h"
37#include "JSCInlines.h"
6fe7ccc8
A
38
39namespace JSC { namespace DFG {
40
41void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex)
42{
81345200 43 ASSERT(JITCode::isOptimizingJIT(codeBlock->jitType()));
6fe7ccc8 44 ASSERT(codeBlock->alternative());
81345200 45 ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT);
6fe7ccc8 46 ASSERT(!codeBlock->jitCodeMap());
6fe7ccc8 47
81345200
A
48 if (!Options::enableOSREntryToDFG())
49 return 0;
50
51 if (Options::verboseOSR()) {
52 dataLog(
53 "DFG OSR in ", *codeBlock->alternative(), " -> ", *codeBlock,
54 " from bc#", bytecodeIndex, "\n");
55 }
6fe7ccc8 56
93a37866 57 VM* vm = &exec->vm();
81345200
A
58
59 sanitizeStackForVM(vm);
60
61 if (codeBlock->jitType() != JITCode::DFGJIT) {
62 RELEASE_ASSERT(codeBlock->jitType() == JITCode::FTLJIT);
63
64 // When will this happen? We could have:
65 //
66 // - An exit from the FTL JIT into the baseline JIT followed by an attempt
67 // to reenter. We're fine with allowing this to fail. If it happens
68 // enough we'll just reoptimize. It basically means that the OSR exit cost
69 // us dearly and so reoptimizing is the right thing to do.
70 //
71 // - We have recursive code with hot loops. Consider that foo has a hot loop
72 // that calls itself. We have two foo's on the stack, lets call them foo1
73 // and foo2, with foo1 having called foo2 from foo's hot loop. foo2 gets
74 // optimized all the way into the FTL. Then it returns into foo1, and then
75 // foo1 wants to get optimized. It might reach this conclusion from its
76 // hot loop and attempt to OSR enter. And we'll tell it that it can't. It
77 // might be worth addressing this case, but I just think this case will
78 // be super rare. For now, if it does happen, it'll cause some compilation
79 // thrashing.
80
81 if (Options::verboseOSR())
82 dataLog(" OSR failed because the target code block is not DFG.\n");
83 return 0;
84 }
85
86 JITCode* jitCode = codeBlock->jitCode()->dfg();
87 OSREntryData* entry = jitCode->osrEntryDataForBytecodeIndex(bytecodeIndex);
6fe7ccc8 88
93a37866 89 if (!entry) {
81345200
A
90 if (Options::verboseOSR())
91 dataLogF(" OSR failed because the entrypoint was optimized out.\n");
93a37866
A
92 return 0;
93 }
94
6fe7ccc8
A
95 ASSERT(entry->m_bytecodeIndex == bytecodeIndex);
96
97 // The code below checks if it is safe to perform OSR entry. It may find
98 // that it is unsafe to do so, for any number of reasons, which are documented
99 // below. If the code decides not to OSR then it returns 0, and it's the caller's
100 // responsibility to patch up the state in such a way as to ensure that it's
101 // both safe and efficient to continue executing baseline code for now. This
102 // should almost certainly include calling either codeBlock->optimizeAfterWarmUp()
103 // or codeBlock->dontOptimizeAnytimeSoon().
104
105 // 1) Verify predictions. If the predictions are inconsistent with the actual
106 // values, then OSR entry is not possible at this time. It's tempting to
107 // assume that we could somehow avoid this case. We can certainly avoid it
108 // for first-time loop OSR - that is, OSR into a CodeBlock that we have just
109 // compiled. Then we are almost guaranteed that all of the predictions will
110 // check out. It would be pretty easy to make that a hard guarantee. But
111 // then there would still be the case where two call frames with the same
112 // baseline CodeBlock are on the stack at the same time. The top one
113 // triggers compilation and OSR. In that case, we may no longer have
114 // accurate value profiles for the one deeper in the stack. Hence, when we
115 // pop into the CodeBlock that is deeper on the stack, we might OSR and
116 // realize that the predictions are wrong. Probably, in most cases, this is
117 // just an anomaly in the sense that the older CodeBlock simply went off
118 // into a less-likely path. So, the wisest course of action is to simply not
119 // OSR at this time.
120
121 for (size_t argument = 0; argument < entry->m_expectedValues.numberOfArguments(); ++argument) {
122 if (argument >= exec->argumentCountIncludingThis()) {
81345200
A
123 if (Options::verboseOSR()) {
124 dataLogF(" OSR failed because argument %zu was not passed, expected ", argument);
125 entry->m_expectedValues.argument(argument).dump(WTF::dataFile());
126 dataLogF(".\n");
127 }
6fe7ccc8
A
128 return 0;
129 }
130
131 JSValue value;
132 if (!argument)
81345200 133 value = exec->thisValue();
6fe7ccc8
A
134 else
135 value = exec->argument(argument - 1);
136
137 if (!entry->m_expectedValues.argument(argument).validate(value)) {
81345200
A
138 if (Options::verboseOSR()) {
139 dataLog(
140 " OSR failed because argument ", argument, " is ", value,
141 ", expected ", entry->m_expectedValues.argument(argument), ".\n");
142 }
6fe7ccc8
A
143 return 0;
144 }
145 }
146
147 for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
81345200 148 int localOffset = virtualRegisterForLocal(local).offset();
6fe7ccc8 149 if (entry->m_localsForcedDouble.get(local)) {
81345200
A
150 if (!exec->registers()[localOffset].jsValue().isNumber()) {
151 if (Options::verboseOSR()) {
152 dataLog(
153 " OSR failed because variable ", localOffset, " is ",
154 exec->registers()[localOffset].jsValue(), ", expected number.\n");
155 }
156 return 0;
157 }
158 continue;
159 }
160 if (entry->m_localsForcedMachineInt.get(local)) {
161 if (!exec->registers()[localOffset].jsValue().isMachineInt()) {
162 if (Options::verboseOSR()) {
163 dataLog(
164 " OSR failed because variable ", localOffset, " is ",
165 exec->registers()[localOffset].jsValue(), ", expected ",
166 "machine int.\n");
167 }
6fe7ccc8
A
168 return 0;
169 }
170 continue;
171 }
81345200
A
172 if (!entry->m_expectedValues.local(local).validate(exec->registers()[localOffset].jsValue())) {
173 if (Options::verboseOSR()) {
174 dataLog(
175 " OSR failed because variable ", localOffset, " is ",
176 exec->registers()[localOffset].jsValue(), ", expected ",
177 entry->m_expectedValues.local(local), ".\n");
178 }
6fe7ccc8
A
179 return 0;
180 }
181 }
182
183 // 2) Check the stack height. The DFG JIT may require a taller stack than the
184 // baseline JIT, in some cases. If we can't grow the stack, then don't do
185 // OSR right now. That's the only option we have unless we want basic block
186 // boundaries to start throwing RangeErrors. Although that would be possible,
187 // it seems silly: you'd be diverting the program to error handling when it
188 // would have otherwise just kept running albeit less quickly.
189
81345200
A
190 unsigned frameSizeForCheck = jitCode->common.requiredRegisterCountForExecutionAndExit();
191 if (!vm->interpreter->stack().ensureCapacityFor(&exec->registers()[virtualRegisterForLocal(frameSizeForCheck - 1).offset()])) {
192 if (Options::verboseOSR())
193 dataLogF(" OSR failed because stack growth failed.\n");
6fe7ccc8
A
194 return 0;
195 }
196
81345200
A
197 if (Options::verboseOSR())
198 dataLogF(" OSR should succeed.\n");
199
200 // At this point we're committed to entering. We will do some work to set things up,
201 // but we also rely on our caller recognizing that when we return a non-null pointer,
202 // that means that we're already past the point of no return and we must succeed at
203 // entering.
6fe7ccc8 204
81345200
A
205 // 3) Set up the data in the scratch buffer and perform data format conversions.
206
207 unsigned frameSize = jitCode->common.frameRegisterCount;
208 unsigned baselineFrameSize = entry->m_expectedValues.numberOfLocals();
209 unsigned maxFrameSize = std::max(frameSize, baselineFrameSize);
210
211 Register* scratch = bitwise_cast<Register*>(vm->scratchBufferForSize(sizeof(Register) * (2 + JSStack::CallFrameHeaderSize + maxFrameSize))->dataBuffer());
212
213 *bitwise_cast<size_t*>(scratch + 0) = frameSize;
214
215 void* targetPC = codeBlock->jitCode()->executableAddressAtOffset(entry->m_machineCodeOffset);
216 if (Options::verboseOSR())
217 dataLogF(" OSR using target PC %p.\n", targetPC);
218 RELEASE_ASSERT(targetPC);
219 *bitwise_cast<void**>(scratch + 1) = targetPC;
220
221 Register* pivot = scratch + 2 + JSStack::CallFrameHeaderSize;
6fe7ccc8 222
81345200
A
223 for (int index = -JSStack::CallFrameHeaderSize; index < static_cast<int>(baselineFrameSize); ++index) {
224 VirtualRegister reg(-1 - index);
225
226 if (reg.isLocal()) {
227 if (entry->m_localsForcedDouble.get(reg.toLocal())) {
228 *bitwise_cast<double*>(pivot + index) = exec->registers()[reg.offset()].jsValue().asNumber();
229 continue;
230 }
231
232 if (entry->m_localsForcedMachineInt.get(reg.toLocal())) {
233 *bitwise_cast<int64_t*>(pivot + index) = exec->registers()[reg.offset()].jsValue().asMachineInt() << JSValue::int52ShiftAmount;
234 continue;
235 }
236 }
237
238 pivot[index] = exec->registers()[reg.offset()].jsValue();
239 }
6fe7ccc8 240
81345200
A
241 // 4) Reshuffle those registers that need reshuffling.
242 Vector<JSValue> temporaryLocals(entry->m_reshufflings.size());
243 for (unsigned i = entry->m_reshufflings.size(); i--;)
244 temporaryLocals[i] = pivot[VirtualRegister(entry->m_reshufflings[i].fromOffset).toLocal()].jsValue();
245 for (unsigned i = entry->m_reshufflings.size(); i--;)
246 pivot[VirtualRegister(entry->m_reshufflings[i].toOffset).toLocal()] = temporaryLocals[i];
6fe7ccc8 247
81345200
A
248 // 5) Clear those parts of the call frame that the DFG ain't using. This helps GC on
249 // some programs by eliminating some stale pointer pathologies.
250 for (unsigned i = frameSize; i--;) {
251 if (entry->m_machineStackUsed.get(i))
252 continue;
253 pivot[i] = JSValue();
254 }
6fe7ccc8 255
81345200 256 // 6) Fix the call frame to have the right code block.
6fe7ccc8 257
81345200 258 *bitwise_cast<CodeBlock**>(pivot - 1 - JSStack::CodeBlock) = codeBlock;
6fe7ccc8 259
81345200
A
260 if (Options::verboseOSR())
261 dataLogF(" OSR returning data buffer %p.\n", scratch);
262 return scratch;
6fe7ccc8
A
263}
264
265} } // namespace JSC::DFG
266
267#endif // ENABLE(DFG_JIT)