2 * Copyright (C) 2011, 2013, 2014, 2015 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"
38 #include <wtf/CommaPrinter.h>
40 namespace JSC
{ namespace DFG
{
42 void OSREntryData::dumpInContext(PrintStream
& out
, DumpContext
* context
) const
44 out
.print("bc#", m_bytecodeIndex
, ", machine code offset = ", m_machineCodeOffset
);
45 out
.print(", stack rules = [");
47 auto printOperand
= [&] (VirtualRegister reg
) {
48 out
.print(inContext(m_expectedValues
.operand(reg
), context
), " (");
49 VirtualRegister toReg
;
50 bool overwritten
= false;
51 for (OSREntryReshuffling reshuffling
: m_reshufflings
) {
52 if (reg
== VirtualRegister(reshuffling
.fromOffset
)) {
53 toReg
= VirtualRegister(reshuffling
.toOffset
);
56 if (reg
== VirtualRegister(reshuffling
.toOffset
))
59 if (!overwritten
&& !toReg
.isValid())
61 if (toReg
.isValid()) {
62 if (toReg
.isLocal() && !m_machineStackUsed
.get(toReg
.toLocal()))
65 out
.print("maps to ", toReg
);
67 out
.print("overwritten");
68 if (reg
.isLocal() && m_localsForcedDouble
.get(reg
.toLocal()))
69 out
.print(", forced double");
70 if (reg
.isLocal() && m_localsForcedMachineInt
.get(reg
.toLocal()))
71 out
.print(", forced machine int");
76 for (size_t argumentIndex
= m_expectedValues
.numberOfArguments(); argumentIndex
--;) {
77 out
.print(comma
, "arg", argumentIndex
, ":");
78 printOperand(virtualRegisterForArgument(argumentIndex
));
80 for (size_t localIndex
= 0; localIndex
< m_expectedValues
.numberOfLocals(); ++localIndex
) {
81 out
.print(comma
, "loc", localIndex
, ":");
82 printOperand(virtualRegisterForLocal(localIndex
));
85 out
.print("], machine stack used = ", m_machineStackUsed
);
88 void OSREntryData::dump(PrintStream
& out
) const
90 dumpInContext(out
, nullptr);
93 void* prepareOSREntry(ExecState
* exec
, CodeBlock
* codeBlock
, unsigned bytecodeIndex
)
95 ASSERT(JITCode::isOptimizingJIT(codeBlock
->jitType()));
96 ASSERT(codeBlock
->alternative());
97 ASSERT(codeBlock
->alternative()->jitType() == JITCode::BaselineJIT
);
98 ASSERT(!codeBlock
->jitCodeMap());
100 if (!Options::enableOSREntryToDFG())
103 if (Options::verboseOSR()) {
105 "DFG OSR in ", *codeBlock
->alternative(), " -> ", *codeBlock
,
106 " from bc#", bytecodeIndex
, "\n");
109 VM
* vm
= &exec
->vm();
111 sanitizeStackForVM(vm
);
114 codeBlock
->ownerExecutable()->setDidTryToEnterInLoop(true);
116 if (codeBlock
->jitType() != JITCode::DFGJIT
) {
117 RELEASE_ASSERT(codeBlock
->jitType() == JITCode::FTLJIT
);
119 // When will this happen? We could have:
121 // - An exit from the FTL JIT into the baseline JIT followed by an attempt
122 // to reenter. We're fine with allowing this to fail. If it happens
123 // enough we'll just reoptimize. It basically means that the OSR exit cost
124 // us dearly and so reoptimizing is the right thing to do.
126 // - We have recursive code with hot loops. Consider that foo has a hot loop
127 // that calls itself. We have two foo's on the stack, lets call them foo1
128 // and foo2, with foo1 having called foo2 from foo's hot loop. foo2 gets
129 // optimized all the way into the FTL. Then it returns into foo1, and then
130 // foo1 wants to get optimized. It might reach this conclusion from its
131 // hot loop and attempt to OSR enter. And we'll tell it that it can't. It
132 // might be worth addressing this case, but I just think this case will
133 // be super rare. For now, if it does happen, it'll cause some compilation
136 if (Options::verboseOSR())
137 dataLog(" OSR failed because the target code block is not DFG.\n");
141 JITCode
* jitCode
= codeBlock
->jitCode()->dfg();
142 OSREntryData
* entry
= jitCode
->osrEntryDataForBytecodeIndex(bytecodeIndex
);
145 if (Options::verboseOSR())
146 dataLogF(" OSR failed because the entrypoint was optimized out.\n");
150 ASSERT(entry
->m_bytecodeIndex
== bytecodeIndex
);
152 // The code below checks if it is safe to perform OSR entry. It may find
153 // that it is unsafe to do so, for any number of reasons, which are documented
154 // below. If the code decides not to OSR then it returns 0, and it's the caller's
155 // responsibility to patch up the state in such a way as to ensure that it's
156 // both safe and efficient to continue executing baseline code for now. This
157 // should almost certainly include calling either codeBlock->optimizeAfterWarmUp()
158 // or codeBlock->dontOptimizeAnytimeSoon().
160 // 1) Verify predictions. If the predictions are inconsistent with the actual
161 // values, then OSR entry is not possible at this time. It's tempting to
162 // assume that we could somehow avoid this case. We can certainly avoid it
163 // for first-time loop OSR - that is, OSR into a CodeBlock that we have just
164 // compiled. Then we are almost guaranteed that all of the predictions will
165 // check out. It would be pretty easy to make that a hard guarantee. But
166 // then there would still be the case where two call frames with the same
167 // baseline CodeBlock are on the stack at the same time. The top one
168 // triggers compilation and OSR. In that case, we may no longer have
169 // accurate value profiles for the one deeper in the stack. Hence, when we
170 // pop into the CodeBlock that is deeper on the stack, we might OSR and
171 // realize that the predictions are wrong. Probably, in most cases, this is
172 // just an anomaly in the sense that the older CodeBlock simply went off
173 // into a less-likely path. So, the wisest course of action is to simply not
176 for (size_t argument
= 0; argument
< entry
->m_expectedValues
.numberOfArguments(); ++argument
) {
177 if (argument
>= exec
->argumentCountIncludingThis()) {
178 if (Options::verboseOSR()) {
179 dataLogF(" OSR failed because argument %zu was not passed, expected ", argument
);
180 entry
->m_expectedValues
.argument(argument
).dump(WTF::dataFile());
188 value
= exec
->thisValue();
190 value
= exec
->argument(argument
- 1);
192 if (!entry
->m_expectedValues
.argument(argument
).validate(value
)) {
193 if (Options::verboseOSR()) {
195 " OSR failed because argument ", argument
, " is ", value
,
196 ", expected ", entry
->m_expectedValues
.argument(argument
), ".\n");
202 for (size_t local
= 0; local
< entry
->m_expectedValues
.numberOfLocals(); ++local
) {
203 int localOffset
= virtualRegisterForLocal(local
).offset();
204 if (entry
->m_localsForcedDouble
.get(local
)) {
205 if (!exec
->registers()[localOffset
].jsValue().isNumber()) {
206 if (Options::verboseOSR()) {
208 " OSR failed because variable ", localOffset
, " is ",
209 exec
->registers()[localOffset
].jsValue(), ", expected number.\n");
215 if (entry
->m_localsForcedMachineInt
.get(local
)) {
216 if (!exec
->registers()[localOffset
].jsValue().isMachineInt()) {
217 if (Options::verboseOSR()) {
219 " OSR failed because variable ", localOffset
, " is ",
220 exec
->registers()[localOffset
].jsValue(), ", expected ",
227 if (!entry
->m_expectedValues
.local(local
).validate(exec
->registers()[localOffset
].jsValue())) {
228 if (Options::verboseOSR()) {
230 " OSR failed because variable ", localOffset
, " is ",
231 exec
->registers()[localOffset
].jsValue(), ", expected ",
232 entry
->m_expectedValues
.local(local
), ".\n");
238 // 2) Check the stack height. The DFG JIT may require a taller stack than the
239 // baseline JIT, in some cases. If we can't grow the stack, then don't do
240 // OSR right now. That's the only option we have unless we want basic block
241 // boundaries to start throwing RangeErrors. Although that would be possible,
242 // it seems silly: you'd be diverting the program to error handling when it
243 // would have otherwise just kept running albeit less quickly.
245 unsigned frameSizeForCheck
= jitCode
->common
.requiredRegisterCountForExecutionAndExit();
246 if (!vm
->interpreter
->stack().ensureCapacityFor(&exec
->registers()[virtualRegisterForLocal(frameSizeForCheck
- 1).offset()])) {
247 if (Options::verboseOSR())
248 dataLogF(" OSR failed because stack growth failed.\n");
252 if (Options::verboseOSR())
253 dataLogF(" OSR should succeed.\n");
255 // At this point we're committed to entering. We will do some work to set things up,
256 // but we also rely on our caller recognizing that when we return a non-null pointer,
257 // that means that we're already past the point of no return and we must succeed at
260 // 3) Set up the data in the scratch buffer and perform data format conversions.
262 unsigned frameSize
= jitCode
->common
.frameRegisterCount
;
263 unsigned baselineFrameSize
= entry
->m_expectedValues
.numberOfLocals();
264 unsigned maxFrameSize
= std::max(frameSize
, baselineFrameSize
);
266 Register
* scratch
= bitwise_cast
<Register
*>(vm
->scratchBufferForSize(sizeof(Register
) * (2 + JSStack::CallFrameHeaderSize
+ maxFrameSize
))->dataBuffer());
268 *bitwise_cast
<size_t*>(scratch
+ 0) = frameSize
;
270 void* targetPC
= codeBlock
->jitCode()->executableAddressAtOffset(entry
->m_machineCodeOffset
);
271 if (Options::verboseOSR())
272 dataLogF(" OSR using target PC %p.\n", targetPC
);
273 RELEASE_ASSERT(targetPC
);
274 *bitwise_cast
<void**>(scratch
+ 1) = targetPC
;
276 Register
* pivot
= scratch
+ 2 + JSStack::CallFrameHeaderSize
;
278 for (int index
= -JSStack::CallFrameHeaderSize
; index
< static_cast<int>(baselineFrameSize
); ++index
) {
279 VirtualRegister
reg(-1 - index
);
282 if (entry
->m_localsForcedDouble
.get(reg
.toLocal())) {
283 *bitwise_cast
<double*>(pivot
+ index
) = exec
->registers()[reg
.offset()].jsValue().asNumber();
287 if (entry
->m_localsForcedMachineInt
.get(reg
.toLocal())) {
288 *bitwise_cast
<int64_t*>(pivot
+ index
) = exec
->registers()[reg
.offset()].jsValue().asMachineInt() << JSValue::int52ShiftAmount
;
293 pivot
[index
] = exec
->registers()[reg
.offset()].jsValue();
296 // 4) Reshuffle those registers that need reshuffling.
297 Vector
<JSValue
> temporaryLocals(entry
->m_reshufflings
.size());
298 for (unsigned i
= entry
->m_reshufflings
.size(); i
--;)
299 temporaryLocals
[i
] = pivot
[VirtualRegister(entry
->m_reshufflings
[i
].fromOffset
).toLocal()].jsValue();
300 for (unsigned i
= entry
->m_reshufflings
.size(); i
--;)
301 pivot
[VirtualRegister(entry
->m_reshufflings
[i
].toOffset
).toLocal()] = temporaryLocals
[i
];
303 // 5) Clear those parts of the call frame that the DFG ain't using. This helps GC on
304 // some programs by eliminating some stale pointer pathologies.
305 for (unsigned i
= frameSize
; i
--;) {
306 if (entry
->m_machineStackUsed
.get(i
))
308 pivot
[i
] = JSValue();
311 // 6) Fix the call frame to have the right code block.
313 *bitwise_cast
<CodeBlock
**>(pivot
- 1 - JSStack::CodeBlock
) = codeBlock
;
315 if (Options::verboseOSR())
316 dataLogF(" OSR returning data buffer %p.\n", scratch
);
320 } } // namespace JSC::DFG
322 #endif // ENABLE(DFG_JIT)