34dc1bd8ac3a83cccce2888bfe2f3a4fe267bdcb
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGOSRExit.cpp
1 /*
2  * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
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 "DFGOSRExit.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "AssemblyHelpers.h"
32 #include "DFGGraph.h"
33 #include "DFGMayExit.h"
34 #include "DFGOSRExitCompilerCommon.h"
35 #include "DFGOSRExitPreparation.h"
36 #include "DFGOperations.h"
37 #include "DFGSpeculativeJIT.h"
38 #include "FrameTracers.h"
39 #include "JSCInlines.h"
40 #include "OperandsInlines.h"
41
42 namespace JSC { namespace DFG {
43
44 OSRExit::OSRExit(ExitKind kind, JSValueSource jsValueSource, MethodOfGettingAValueProfile valueProfile, SpeculativeJIT* jit, unsigned streamIndex, unsigned recoveryIndex)
45     : OSRExitBase(kind, jit->m_origin.forExit, jit->m_origin.semantic, jit->m_origin.wasHoisted)
46     , m_jsValueSource(jsValueSource)
47     , m_valueProfile(valueProfile)
48     , m_recoveryIndex(recoveryIndex)
49     , m_streamIndex(streamIndex)
50 {
51     bool canExit = jit->m_origin.exitOK;
52     if (!canExit && jit->m_currentNode) {
53         ExitMode exitMode = mayExit(jit->m_jit.graph(), jit->m_currentNode);
54         canExit = exitMode == ExitMode::Exits || exitMode == ExitMode::ExitsForExceptions;
55     }
56     DFG_ASSERT(jit->m_jit.graph(), jit->m_currentNode, canExit);
57 }
58
59 void OSRExit::setPatchableCodeOffset(MacroAssembler::PatchableJump check)
60 {
61     m_patchableCodeOffset = check.m_jump.m_label.m_offset;
62 }
63
64 MacroAssembler::Jump OSRExit::getPatchableCodeOffsetAsJump() const
65 {
66     return MacroAssembler::Jump(AssemblerLabel(m_patchableCodeOffset));
67 }
68
69 CodeLocationJump OSRExit::codeLocationForRepatch(CodeBlock* dfgCodeBlock) const
70 {
71     return CodeLocationJump(dfgCodeBlock->jitCode()->dataAddressAtOffset(m_patchableCodeOffset));
72 }
73
74 void OSRExit::correctJump(LinkBuffer& linkBuffer)
75 {
76     MacroAssembler::Label label;
77     label.m_label.m_offset = m_patchableCodeOffset;
78     m_patchableCodeOffset = linkBuffer.offsetOf(label);
79 }
80
81 void OSRExit::emitRestoreArguments(CCallHelpers& jit, const Operands<ValueRecovery>& operands)
82 {
83     HashMap<MinifiedID, int> alreadyAllocatedArguments; // Maps phantom arguments node ID to operand.
84     for (size_t index = 0; index < operands.size(); ++index) {
85         const ValueRecovery& recovery = operands[index];
86         int operand = operands.operandForIndex(index);
87
88         if (recovery.technique() != DirectArgumentsThatWereNotCreated
89             && recovery.technique() != ClonedArgumentsThatWereNotCreated)
90             continue;
91
92         MinifiedID id = recovery.nodeID();
93         auto iter = alreadyAllocatedArguments.find(id);
94         if (iter != alreadyAllocatedArguments.end()) {
95             JSValueRegs regs = JSValueRegs::withTwoAvailableRegs(GPRInfo::regT0, GPRInfo::regT1);
96             jit.loadValue(CCallHelpers::addressFor(iter->value), regs);
97             jit.storeValue(regs, CCallHelpers::addressFor(operand));
98             continue;
99         }
100
101         InlineCallFrame* inlineCallFrame =
102             jit.codeBlock()->jitCode()->dfg()->minifiedDFG.at(id)->inlineCallFrame();
103
104         int stackOffset;
105         if (inlineCallFrame)
106             stackOffset = inlineCallFrame->stackOffset;
107         else
108             stackOffset = 0;
109
110         if (!inlineCallFrame || inlineCallFrame->isClosureCall) {
111             jit.loadPtr(
112                 AssemblyHelpers::addressFor(stackOffset + CallFrameSlot::callee),
113                 GPRInfo::regT0);
114         } else {
115             jit.move(
116                 AssemblyHelpers::TrustedImmPtr(inlineCallFrame->calleeRecovery.constant().asCell()),
117                 GPRInfo::regT0);
118         }
119
120         if (!inlineCallFrame || inlineCallFrame->isVarargs()) {
121             jit.load32(
122                 AssemblyHelpers::payloadFor(stackOffset + CallFrameSlot::argumentCount),
123                 GPRInfo::regT1);
124         } else {
125             jit.move(
126                 AssemblyHelpers::TrustedImm32(inlineCallFrame->arguments.size()),
127                 GPRInfo::regT1);
128         }
129
130         jit.setupArgumentsWithExecState(
131             AssemblyHelpers::TrustedImmPtr(inlineCallFrame), GPRInfo::regT0, GPRInfo::regT1);
132         switch (recovery.technique()) {
133         case DirectArgumentsThatWereNotCreated:
134             jit.move(AssemblyHelpers::TrustedImmPtr(bitwise_cast<void*>(operationCreateDirectArgumentsDuringExit)), GPRInfo::nonArgGPR0);
135             break;
136         case ClonedArgumentsThatWereNotCreated:
137             jit.move(AssemblyHelpers::TrustedImmPtr(bitwise_cast<void*>(operationCreateClonedArgumentsDuringExit)), GPRInfo::nonArgGPR0);
138             break;
139         default:
140             RELEASE_ASSERT_NOT_REACHED();
141             break;
142         }
143         jit.call(GPRInfo::nonArgGPR0);
144         jit.storeCell(GPRInfo::returnValueGPR, AssemblyHelpers::addressFor(operand));
145
146         alreadyAllocatedArguments.add(id, operand);
147     }
148 }
149
150 void JIT_OPERATION OSRExit::compileOSRExit(ExecState* exec)
151 {
152     VM* vm = &exec->vm();
153     auto scope = DECLARE_THROW_SCOPE(*vm);
154
155     if (vm->callFrameForCatch)
156         RELEASE_ASSERT(vm->callFrameForCatch == exec);
157
158     CodeBlock* codeBlock = exec->codeBlock();
159     ASSERT(codeBlock);
160     ASSERT(codeBlock->jitType() == JITCode::DFGJIT);
161
162     // It's sort of preferable that we don't GC while in here. Anyways, doing so wouldn't
163     // really be profitable.
164     DeferGCForAWhile deferGC(vm->heap);
165
166     uint32_t exitIndex = vm->osrExitIndex;
167     OSRExit& exit = codeBlock->jitCode()->dfg()->osrExit[exitIndex];
168
169     if (vm->callFrameForCatch)
170         ASSERT(exit.m_kind == GenericUnwind);
171     if (exit.isExceptionHandler())
172         ASSERT_UNUSED(scope, !!scope.exception());
173     
174     prepareCodeOriginForOSRExit(exec, exit.m_codeOrigin);
175
176     // Compute the value recoveries.
177     Operands<ValueRecovery> operands;
178     codeBlock->jitCode()->dfg()->variableEventStream.reconstruct(codeBlock, exit.m_codeOrigin, codeBlock->jitCode()->dfg()->minifiedDFG, exit.m_streamIndex, operands);
179
180     SpeculationRecovery* recovery = 0;
181     if (exit.m_recoveryIndex != UINT_MAX)
182         recovery = &codeBlock->jitCode()->dfg()->speculationRecovery[exit.m_recoveryIndex];
183
184     {
185         CCallHelpers jit(codeBlock);
186
187         if (exit.m_kind == GenericUnwind) {
188             // We are acting as a defacto op_catch because we arrive here from genericUnwind().
189             // So, we must restore our call frame and stack pointer.
190             jit.restoreCalleeSavesFromVMEntryFrameCalleeSavesBuffer(*vm);
191             jit.loadPtr(vm->addressOfCallFrameForCatch(), GPRInfo::callFrameRegister);
192         }
193         jit.addPtr(
194             CCallHelpers::TrustedImm32(codeBlock->stackPointerOffset() * sizeof(Register)),
195             GPRInfo::callFrameRegister, CCallHelpers::stackPointerRegister);
196
197         jit.jitAssertHasValidCallFrame();
198
199         if (vm->m_perBytecodeProfiler && codeBlock->jitCode()->dfgCommon()->compilation) {
200             Profiler::Database& database = *vm->m_perBytecodeProfiler;
201             Profiler::Compilation* compilation = codeBlock->jitCode()->dfgCommon()->compilation.get();
202
203             Profiler::OSRExit* profilerExit = compilation->addOSRExit(
204                 exitIndex, Profiler::OriginStack(database, codeBlock, exit.m_codeOrigin),
205                 exit.m_kind, exit.m_kind == UncountableInvalidation);
206             jit.add64(CCallHelpers::TrustedImm32(1), CCallHelpers::AbsoluteAddress(profilerExit->counterAddress()));
207         }
208
209         compileExit(jit, *vm, exit, operands, recovery);
210
211         LinkBuffer patchBuffer(jit, codeBlock);
212         exit.m_code = FINALIZE_CODE_IF(
213             shouldDumpDisassembly() || Options::verboseOSR() || Options::verboseDFGOSRExit(),
214             patchBuffer,
215             ("DFG OSR exit #%u (%s, %s) from %s, with operands = %s",
216                 exitIndex, toCString(exit.m_codeOrigin).data(),
217                 exitKindToString(exit.m_kind), toCString(*codeBlock).data(),
218                 toCString(ignoringContext<DumpContext>(operands)).data()));
219     }
220
221     MacroAssembler::repatchJump(exit.codeLocationForRepatch(codeBlock), CodeLocationLabel(exit.m_code.code()));
222
223     vm->osrExitJumpDestination = exit.m_code.code().executableAddress();
224 }
225
226 #if USE(JSVALUE64)
227
228 void OSRExit::compileExit(CCallHelpers& jit, VM& vm, const OSRExit& exit, const Operands<ValueRecovery>& operands, SpeculationRecovery* recovery)
229 {
230     jit.jitAssertTagsInPlace();
231
232     // Pro-forma stuff.
233     if (Options::printEachOSRExit()) {
234         SpeculationFailureDebugInfo* debugInfo = new SpeculationFailureDebugInfo;
235         debugInfo->codeBlock = jit.codeBlock();
236         debugInfo->kind = exit.m_kind;
237         debugInfo->bytecodeOffset = exit.m_codeOrigin.bytecodeIndex;
238
239         jit.debugCall(vm, debugOperationPrintSpeculationFailure, debugInfo);
240     }
241
242     // Perform speculation recovery. This only comes into play when an operation
243     // starts mutating state before verifying the speculation it has already made.
244
245     if (recovery) {
246         switch (recovery->type()) {
247         case SpeculativeAdd:
248             jit.sub32(recovery->src(), recovery->dest());
249             jit.or64(GPRInfo::tagTypeNumberRegister, recovery->dest());
250             break;
251
252         case SpeculativeAddImmediate:
253             jit.sub32(AssemblyHelpers::Imm32(recovery->immediate()), recovery->dest());
254             jit.or64(GPRInfo::tagTypeNumberRegister, recovery->dest());
255             break;
256
257         case BooleanSpeculationCheck:
258             jit.xor64(AssemblyHelpers::TrustedImm32(static_cast<int32_t>(ValueFalse)), recovery->dest());
259             break;
260
261         default:
262             break;
263         }
264     }
265
266     // Refine some array and/or value profile, if appropriate.
267
268     if (!!exit.m_jsValueSource) {
269         if (exit.m_kind == BadCache || exit.m_kind == BadIndexingType) {
270             // If the instruction that this originated from has an array profile, then
271             // refine it. If it doesn't, then do nothing. The latter could happen for
272             // hoisted checks, or checks emitted for operations that didn't have array
273             // profiling - either ops that aren't array accesses at all, or weren't
274             // known to be array acceses in the bytecode. The latter case is a FIXME
275             // while the former case is an outcome of a CheckStructure not knowing why
276             // it was emitted (could be either due to an inline cache of a property
277             // property access, or due to an array profile).
278
279             CodeOrigin codeOrigin = exit.m_codeOriginForExitProfile;
280             if (ArrayProfile* arrayProfile = jit.baselineCodeBlockFor(codeOrigin)->getArrayProfile(codeOrigin.bytecodeIndex)) {
281                 GPRReg usedRegister;
282                 if (exit.m_jsValueSource.isAddress())
283                     usedRegister = exit.m_jsValueSource.base();
284                 else
285                     usedRegister = exit.m_jsValueSource.gpr();
286
287                 GPRReg scratch1;
288                 GPRReg scratch2;
289                 scratch1 = AssemblyHelpers::selectScratchGPR(usedRegister);
290                 scratch2 = AssemblyHelpers::selectScratchGPR(usedRegister, scratch1);
291
292                 if (isARM64()) {
293                     jit.pushToSave(scratch1);
294                     jit.pushToSave(scratch2);
295                 } else {
296                     jit.push(scratch1);
297                     jit.push(scratch2);
298                 }
299
300                 GPRReg value;
301                 if (exit.m_jsValueSource.isAddress()) {
302                     value = scratch1;
303                     jit.loadPtr(AssemblyHelpers::Address(exit.m_jsValueSource.asAddress()), value);
304                 } else
305                     value = exit.m_jsValueSource.gpr();
306
307                 jit.load32(AssemblyHelpers::Address(value, JSCell::structureIDOffset()), scratch1);
308                 jit.store32(scratch1, arrayProfile->addressOfLastSeenStructureID());
309                 jit.load8(AssemblyHelpers::Address(value, JSCell::indexingTypeAndMiscOffset()), scratch1);
310                 jit.move(AssemblyHelpers::TrustedImm32(1), scratch2);
311                 jit.lshift32(scratch1, scratch2);
312                 jit.or32(scratch2, AssemblyHelpers::AbsoluteAddress(arrayProfile->addressOfArrayModes()));
313
314                 if (isARM64()) {
315                     jit.popToRestore(scratch2);
316                     jit.popToRestore(scratch1);
317                 } else {
318                     jit.pop(scratch2);
319                     jit.pop(scratch1);
320                 }
321             }
322         }
323
324         if (MethodOfGettingAValueProfile profile = exit.m_valueProfile) {
325             if (exit.m_jsValueSource.isAddress()) {
326                 // We can't be sure that we have a spare register. So use the tagTypeNumberRegister,
327                 // since we know how to restore it.
328                 jit.load64(AssemblyHelpers::Address(exit.m_jsValueSource.asAddress()), GPRInfo::tagTypeNumberRegister);
329                 profile.emitReportValue(jit, JSValueRegs(GPRInfo::tagTypeNumberRegister));
330                 jit.move(AssemblyHelpers::TrustedImm64(TagTypeNumber), GPRInfo::tagTypeNumberRegister);
331             } else
332                 profile.emitReportValue(jit, JSValueRegs(exit.m_jsValueSource.gpr()));
333         }
334     }
335
336     // What follows is an intentionally simple OSR exit implementation that generates
337     // fairly poor code but is very easy to hack. In particular, it dumps all state that
338     // needs conversion into a scratch buffer so that in step 6, where we actually do the
339     // conversions, we know that all temp registers are free to use and the variable is
340     // definitely in a well-known spot in the scratch buffer regardless of whether it had
341     // originally been in a register or spilled. This allows us to decouple "where was
342     // the variable" from "how was it represented". Consider that the
343     // Int32DisplacedInJSStack recovery: it tells us that the value is in a
344     // particular place and that that place holds an unboxed int32. We have two different
345     // places that a value could be (displaced, register) and a bunch of different
346     // ways of representing a value. The number of recoveries is two * a bunch. The code
347     // below means that we have to have two + a bunch cases rather than two * a bunch.
348     // Once we have loaded the value from wherever it was, the reboxing is the same
349     // regardless of its location. Likewise, before we do the reboxing, the way we get to
350     // the value (i.e. where we load it from) is the same regardless of its type. Because
351     // the code below always dumps everything into a scratch buffer first, the two
352     // questions become orthogonal, which simplifies adding new types and adding new
353     // locations.
354     //
355     // This raises the question: does using such a suboptimal implementation of OSR exit,
356     // where we always emit code to dump all state into a scratch buffer only to then
357     // dump it right back into the stack, hurt us in any way? The asnwer is that OSR exits
358     // are rare. Our tiering strategy ensures this. This is because if an OSR exit is
359     // taken more than ~100 times, we jettison the DFG code block along with all of its
360     // exits. It is impossible for an OSR exit - i.e. the code we compile below - to
361     // execute frequently enough for the codegen to matter that much. It probably matters
362     // enough that we don't want to turn this into some super-slow function call, but so
363     // long as we're generating straight-line code, that code can be pretty bad. Also
364     // because we tend to exit only along one OSR exit from any DFG code block - that's an
365     // empirical result that we're extremely confident about - the code size of this
366     // doesn't matter much. Hence any attempt to optimize the codegen here is just purely
367     // harmful to the system: it probably won't reduce either net memory usage or net
368     // execution time. It will only prevent us from cleanly decoupling "where was the
369     // variable" from "how was it represented", which will make it more difficult to add
370     // features in the future and it will make it harder to reason about bugs.
371
372     // Save all state from GPRs into the scratch buffer.
373
374     ScratchBuffer* scratchBuffer = vm.scratchBufferForSize(sizeof(EncodedJSValue) * operands.size());
375     EncodedJSValue* scratch = scratchBuffer ? static_cast<EncodedJSValue*>(scratchBuffer->dataBuffer()) : 0;
376
377     for (size_t index = 0; index < operands.size(); ++index) {
378         const ValueRecovery& recovery = operands[index];
379
380         switch (recovery.technique()) {
381         case InGPR:
382         case UnboxedInt32InGPR:
383         case UnboxedInt52InGPR:
384         case UnboxedStrictInt52InGPR:
385         case UnboxedCellInGPR:
386             jit.store64(recovery.gpr(), scratch + index);
387             break;
388
389         default:
390             break;
391         }
392     }
393
394     // And voila, all GPRs are free to reuse.
395
396     // Save all state from FPRs into the scratch buffer.
397
398     for (size_t index = 0; index < operands.size(); ++index) {
399         const ValueRecovery& recovery = operands[index];
400
401         switch (recovery.technique()) {
402         case UnboxedDoubleInFPR:
403         case InFPR:
404             jit.move(AssemblyHelpers::TrustedImmPtr(scratch + index), GPRInfo::regT0);
405             jit.storeDouble(recovery.fpr(), MacroAssembler::Address(GPRInfo::regT0));
406             break;
407
408         default:
409             break;
410         }
411     }
412
413     // Now, all FPRs are also free.
414
415     // Save all state from the stack into the scratch buffer. For simplicity we
416     // do this even for state that's already in the right place on the stack.
417     // It makes things simpler later.
418
419     for (size_t index = 0; index < operands.size(); ++index) {
420         const ValueRecovery& recovery = operands[index];
421
422         switch (recovery.technique()) {
423         case DisplacedInJSStack:
424         case CellDisplacedInJSStack:
425         case BooleanDisplacedInJSStack:
426         case Int32DisplacedInJSStack:
427         case DoubleDisplacedInJSStack:
428         case Int52DisplacedInJSStack:
429         case StrictInt52DisplacedInJSStack:
430             jit.load64(AssemblyHelpers::addressFor(recovery.virtualRegister()), GPRInfo::regT0);
431             jit.store64(GPRInfo::regT0, scratch + index);
432             break;
433
434         default:
435             break;
436         }
437     }
438
439     // Need to ensure that the stack pointer accounts for the worst-case stack usage at exit. This
440     // could toast some stack that the DFG used. We need to do it before storing to stack offsets
441     // used by baseline.
442     jit.addPtr(
443         CCallHelpers::TrustedImm32(
444             -jit.codeBlock()->jitCode()->dfgCommon()->requiredRegisterCountForExit * sizeof(Register)),
445         CCallHelpers::framePointerRegister, CCallHelpers::stackPointerRegister);
446
447     // Restore the DFG callee saves and then save the ones the baseline JIT uses.
448     jit.emitRestoreCalleeSaves();
449     jit.emitSaveCalleeSavesFor(jit.baselineCodeBlock());
450
451     // The tag registers are needed to materialize recoveries below.
452     jit.emitMaterializeTagCheckRegisters();
453
454     if (exit.isExceptionHandler())
455         jit.copyCalleeSavesToVMEntryFrameCalleeSavesBuffer(vm);
456
457     // Do all data format conversions and store the results into the stack.
458
459     for (size_t index = 0; index < operands.size(); ++index) {
460         const ValueRecovery& recovery = operands[index];
461         VirtualRegister reg = operands.virtualRegisterForIndex(index);
462
463         if (reg.isLocal() && reg.toLocal() < static_cast<int>(jit.baselineCodeBlock()->calleeSaveSpaceAsVirtualRegisters()))
464             continue;
465
466         int operand = reg.offset();
467
468         switch (recovery.technique()) {
469         case InGPR:
470         case UnboxedCellInGPR:
471         case DisplacedInJSStack:
472         case CellDisplacedInJSStack:
473         case BooleanDisplacedInJSStack:
474         case InFPR:
475             jit.load64(scratch + index, GPRInfo::regT0);
476             jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(operand));
477             break;
478
479         case UnboxedInt32InGPR:
480         case Int32DisplacedInJSStack:
481             jit.load64(scratch + index, GPRInfo::regT0);
482             jit.zeroExtend32ToPtr(GPRInfo::regT0, GPRInfo::regT0);
483             jit.or64(GPRInfo::tagTypeNumberRegister, GPRInfo::regT0);
484             jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(operand));
485             break;
486
487         case UnboxedInt52InGPR:
488         case Int52DisplacedInJSStack:
489             jit.load64(scratch + index, GPRInfo::regT0);
490             jit.rshift64(
491                 AssemblyHelpers::TrustedImm32(JSValue::int52ShiftAmount), GPRInfo::regT0);
492             jit.boxInt52(GPRInfo::regT0, GPRInfo::regT0, GPRInfo::regT1, FPRInfo::fpRegT0);
493             jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(operand));
494             break;
495
496         case UnboxedStrictInt52InGPR:
497         case StrictInt52DisplacedInJSStack:
498             jit.load64(scratch + index, GPRInfo::regT0);
499             jit.boxInt52(GPRInfo::regT0, GPRInfo::regT0, GPRInfo::regT1, FPRInfo::fpRegT0);
500             jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(operand));
501             break;
502
503         case UnboxedDoubleInFPR:
504         case DoubleDisplacedInJSStack:
505             jit.move(AssemblyHelpers::TrustedImmPtr(scratch + index), GPRInfo::regT0);
506             jit.loadDouble(MacroAssembler::Address(GPRInfo::regT0), FPRInfo::fpRegT0);
507             jit.purifyNaN(FPRInfo::fpRegT0);
508             jit.boxDouble(FPRInfo::fpRegT0, GPRInfo::regT0);
509             jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(operand));
510             break;
511
512         case Constant:
513             jit.store64(
514                 AssemblyHelpers::TrustedImm64(JSValue::encode(recovery.constant())),
515                 AssemblyHelpers::addressFor(operand));
516             break;
517
518         case DirectArgumentsThatWereNotCreated:
519         case ClonedArgumentsThatWereNotCreated:
520             // Don't do this, yet.
521             break;
522
523         default:
524             RELEASE_ASSERT_NOT_REACHED();
525             break;
526         }
527     }
528
529     // Now that things on the stack are recovered, do the arguments recovery. We assume that arguments
530     // recoveries don't recursively refer to each other. But, we don't try to assume that they only
531     // refer to certain ranges of locals. Hence why we need to do this here, once the stack is sensible.
532     // Note that we also roughly assume that the arguments might still be materialized outside of its
533     // inline call frame scope - but for now the DFG wouldn't do that.
534
535     emitRestoreArguments(jit, operands);
536
537     // Adjust the old JIT's execute counter. Since we are exiting OSR, we know
538     // that all new calls into this code will go to the new JIT, so the execute
539     // counter only affects call frames that performed OSR exit and call frames
540     // that were still executing the old JIT at the time of another call frame's
541     // OSR exit. We want to ensure that the following is true:
542     //
543     // (a) Code the performs an OSR exit gets a chance to reenter optimized
544     //     code eventually, since optimized code is faster. But we don't
545     //     want to do such reentery too aggressively (see (c) below).
546     //
547     // (b) If there is code on the call stack that is still running the old
548     //     JIT's code and has never OSR'd, then it should get a chance to
549     //     perform OSR entry despite the fact that we've exited.
550     //
551     // (c) Code the performs an OSR exit should not immediately retry OSR
552     //     entry, since both forms of OSR are expensive. OSR entry is
553     //     particularly expensive.
554     //
555     // (d) Frequent OSR failures, even those that do not result in the code
556     //     running in a hot loop, result in recompilation getting triggered.
557     //
558     // To ensure (c), we'd like to set the execute counter to
559     // counterValueForOptimizeAfterWarmUp(). This seems like it would endanger
560     // (a) and (b), since then every OSR exit would delay the opportunity for
561     // every call frame to perform OSR entry. Essentially, if OSR exit happens
562     // frequently and the function has few loops, then the counter will never
563     // become non-negative and OSR entry will never be triggered. OSR entry
564     // will only happen if a loop gets hot in the old JIT, which does a pretty
565     // good job of ensuring (a) and (b). But that doesn't take care of (d),
566     // since each speculation failure would reset the execute counter.
567     // So we check here if the number of speculation failures is significantly
568     // larger than the number of successes (we want 90% success rate), and if
569     // there have been a large enough number of failures. If so, we set the
570     // counter to 0; otherwise we set the counter to
571     // counterValueForOptimizeAfterWarmUp().
572
573     handleExitCounts(jit, exit);
574
575     // Reify inlined call frames.
576
577     reifyInlinedCallFrames(jit, exit);
578
579     // And finish.
580     adjustAndJumpToTarget(vm, jit, exit);
581 }
582 #endif // USE(JSVALUE64)
583
584 void JIT_OPERATION OSRExit::debugOperationPrintSpeculationFailure(ExecState* exec, void* debugInfoRaw, void* scratch)
585 {
586     VM* vm = &exec->vm();
587     NativeCallFrameTracer tracer(vm, exec);
588
589     SpeculationFailureDebugInfo* debugInfo = static_cast<SpeculationFailureDebugInfo*>(debugInfoRaw);
590     CodeBlock* codeBlock = debugInfo->codeBlock;
591     CodeBlock* alternative = codeBlock->alternative();
592     dataLog("Speculation failure in ", *codeBlock);
593     dataLog(" @ exit #", vm->osrExitIndex, " (bc#", debugInfo->bytecodeOffset, ", ", exitKindToString(debugInfo->kind), ") with ");
594     if (alternative) {
595         dataLog(
596             "executeCounter = ", alternative->jitExecuteCounter(),
597             ", reoptimizationRetryCounter = ", alternative->reoptimizationRetryCounter(),
598             ", optimizationDelayCounter = ", alternative->optimizationDelayCounter());
599     } else
600         dataLog("no alternative code block (i.e. we've been jettisoned)");
601     dataLog(", osrExitCounter = ", codeBlock->osrExitCounter(), "\n");
602     dataLog("    GPRs at time of exit:");
603     char* scratchPointer = static_cast<char*>(scratch);
604     for (unsigned i = 0; i < GPRInfo::numberOfRegisters; ++i) {
605         GPRReg gpr = GPRInfo::toRegister(i);
606         dataLog(" ", GPRInfo::debugName(gpr), ":", RawPointer(*reinterpret_cast_ptr<void**>(scratchPointer)));
607         scratchPointer += sizeof(EncodedJSValue);
608     }
609     dataLog("\n");
610     dataLog("    FPRs at time of exit:");
611     for (unsigned i = 0; i < FPRInfo::numberOfRegisters; ++i) {
612         FPRReg fpr = FPRInfo::toRegister(i);
613         dataLog(" ", FPRInfo::debugName(fpr), ":");
614         uint64_t bits = *reinterpret_cast_ptr<uint64_t*>(scratchPointer);
615         double value = *reinterpret_cast_ptr<double*>(scratchPointer);
616         dataLogF("%llx:%lf", static_cast<long long>(bits), value);
617         scratchPointer += sizeof(EncodedJSValue);
618     }
619     dataLog("\n");
620 }
621
622 } } // namespace JSC::DFG
623
624 #endif // ENABLE(DFG_JIT)