]>
git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGOSREntry.cpp
2 * Copyright (C) 2011, 2013, 2014 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 "JSStackInlines.h"
37 #include "JSCInlines.h"
39 namespace JSC
{ namespace DFG
{
41 void* prepareOSREntry(ExecState
* exec
, CodeBlock
* codeBlock
, unsigned bytecodeIndex
)
43 ASSERT(JITCode::isOptimizingJIT(codeBlock
->jitType()));
44 ASSERT(codeBlock
->alternative());
45 ASSERT(codeBlock
->alternative()->jitType() == JITCode::BaselineJIT
);
46 ASSERT(!codeBlock
->jitCodeMap());
48 if (!Options::enableOSREntryToDFG())
51 if (Options::verboseOSR()) {
53 "DFG OSR in ", *codeBlock
->alternative(), " -> ", *codeBlock
,
54 " from bc#", bytecodeIndex
, "\n");
59 sanitizeStackForVM(vm
);
61 if (codeBlock
->jitType() != JITCode::DFGJIT
) {
62 RELEASE_ASSERT(codeBlock
->jitType() == JITCode::FTLJIT
);
64 // When will this happen? We could have:
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.
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
81 if (Options::verboseOSR())
82 dataLog(" OSR failed because the target code block is not DFG.\n");
86 JITCode
* jitCode
= codeBlock
->jitCode()->dfg();
87 OSREntryData
* entry
= jitCode
->osrEntryDataForBytecodeIndex(bytecodeIndex
);
90 if (Options::verboseOSR())
91 dataLogF(" OSR failed because the entrypoint was optimized out.\n");
95 ASSERT(entry
->m_bytecodeIndex
== bytecodeIndex
);
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().
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
121 for (size_t argument
= 0; argument
< entry
->m_expectedValues
.numberOfArguments(); ++argument
) {
122 if (argument
>= exec
->argumentCountIncludingThis()) {
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());
133 value
= exec
->thisValue();
135 value
= exec
->argument(argument
- 1);
137 if (!entry
->m_expectedValues
.argument(argument
).validate(value
)) {
138 if (Options::verboseOSR()) {
140 " OSR failed because argument ", argument
, " is ", value
,
141 ", expected ", entry
->m_expectedValues
.argument(argument
), ".\n");
147 for (size_t local
= 0; local
< entry
->m_expectedValues
.numberOfLocals(); ++local
) {
148 int localOffset
= virtualRegisterForLocal(local
).offset();
149 if (entry
->m_localsForcedDouble
.get(local
)) {
150 if (!exec
->registers()[localOffset
].jsValue().isNumber()) {
151 if (Options::verboseOSR()) {
153 " OSR failed because variable ", localOffset
, " is ",
154 exec
->registers()[localOffset
].jsValue(), ", expected number.\n");
160 if (entry
->m_localsForcedMachineInt
.get(local
)) {
161 if (!exec
->registers()[localOffset
].jsValue().isMachineInt()) {
162 if (Options::verboseOSR()) {
164 " OSR failed because variable ", localOffset
, " is ",
165 exec
->registers()[localOffset
].jsValue(), ", expected ",
172 if (!entry
->m_expectedValues
.local(local
).validate(exec
->registers()[localOffset
].jsValue())) {
173 if (Options::verboseOSR()) {
175 " OSR failed because variable ", localOffset
, " is ",
176 exec
->registers()[localOffset
].jsValue(), ", expected ",
177 entry
->m_expectedValues
.local(local
), ".\n");
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.
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");
197 if (Options::verboseOSR())
198 dataLogF(" OSR should succeed.\n");
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
205 // 3) Set up the data in the scratch buffer and perform data format conversions.
207 unsigned frameSize
= jitCode
->common
.frameRegisterCount
;
208 unsigned baselineFrameSize
= entry
->m_expectedValues
.numberOfLocals();
209 unsigned maxFrameSize
= std::max(frameSize
, baselineFrameSize
);
211 Register
* scratch
= bitwise_cast
<Register
*>(vm
->scratchBufferForSize(sizeof(Register
) * (2 + JSStack::CallFrameHeaderSize
+ maxFrameSize
))->dataBuffer());
213 *bitwise_cast
<size_t*>(scratch
+ 0) = frameSize
;
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
;
221 Register
* pivot
= scratch
+ 2 + JSStack::CallFrameHeaderSize
;
223 for (int index
= -JSStack::CallFrameHeaderSize
; index
< static_cast<int>(baselineFrameSize
); ++index
) {
224 VirtualRegister
reg(-1 - index
);
227 if (entry
->m_localsForcedDouble
.get(reg
.toLocal())) {
228 *bitwise_cast
<double*>(pivot
+ index
) = exec
->registers()[reg
.offset()].jsValue().asNumber();
232 if (entry
->m_localsForcedMachineInt
.get(reg
.toLocal())) {
233 *bitwise_cast
<int64_t*>(pivot
+ index
) = exec
->registers()[reg
.offset()].jsValue().asMachineInt() << JSValue::int52ShiftAmount
;
238 pivot
[index
] = exec
->registers()[reg
.offset()].jsValue();
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
];
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
))
253 pivot
[i
] = JSValue();
256 // 6) Fix the call frame to have the right code block.
258 *bitwise_cast
<CodeBlock
**>(pivot
- 1 - JSStack::CodeBlock
) = codeBlock
;
260 if (Options::verboseOSR())
261 dataLogF(" OSR returning data buffer %p.\n", scratch
);
265 } } // namespace JSC::DFG
267 #endif // ENABLE(DFG_JIT)