static void registerClobberCheck(AssemblyHelpers& jit, RegisterSet dontClobber) { if (!Options::clobberAllRegsInFTLICSlowPath()) return; RegisterSet clobber = RegisterSet::allRegisters(); clobber.exclude(RegisterSet::reservedHardwareRegisters()); clobber.exclude(RegisterSet::stackRegisters()); clobber.exclude(RegisterSet::calleeSaveRegisters()); clobber.exclude(dontClobber); GPRReg someGPR; for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) { if (!clobber.get(reg) || !reg.isGPR()) continue; jit.move(AssemblyHelpers::TrustedImm32(0x1337beef), reg.gpr()); someGPR = reg.gpr(); } for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) { if (!clobber.get(reg) || !reg.isFPR()) continue; jit.move64ToDouble(someGPR, reg.fpr()); } }
void ValueRep::emitRestore(AssemblyHelpers& jit, Reg reg) const { if (reg.isGPR()) { switch (kind()) { case LateRegister: case Register: if (isGPR()) jit.move(gpr(), reg.gpr()); else jit.moveDoubleTo64(fpr(), reg.gpr()); break; case Stack: jit.load64(AssemblyHelpers::Address(GPRInfo::callFrameRegister, offsetFromFP()), reg.gpr()); break; case Constant: jit.move(AssemblyHelpers::TrustedImm64(value()), reg.gpr()); break; default: RELEASE_ASSERT_NOT_REACHED(); break; } return; } switch (kind()) { case LateRegister: case Register: if (isGPR()) jit.move64ToDouble(gpr(), reg.fpr()); else jit.moveDouble(fpr(), reg.fpr()); break; case Stack: jit.loadDouble(AssemblyHelpers::Address(GPRInfo::callFrameRegister, offsetFromFP()), reg.fpr()); break; case Constant: jit.move(AssemblyHelpers::TrustedImm64(value()), jit.scratchRegister()); jit.move64ToDouble(jit.scratchRegister(), reg.fpr()); break; default: RELEASE_ASSERT_NOT_REACHED(); break; } }
void CallFrameShuffler::emitDisplace(CachedRecovery& cachedRecovery) { Reg wantedReg; if (!(wantedReg = Reg { cachedRecovery.wantedJSValueRegs().gpr() })) wantedReg = Reg { cachedRecovery.wantedFPR() }; ASSERT(wantedReg); ASSERT(!m_lockedRegisters.get(wantedReg)); if (CachedRecovery* current = m_registers[wantedReg]) { if (current == &cachedRecovery) { if (verbose) dataLog(" + ", wantedReg, " is OK\n"); return; } // We could do a more complex thing by finding cycles // etc. in that case. // However, ending up in this situation will be super // rare, and should actually be outright impossible for // non-FTL tiers, since: // (a) All doubles have been converted into JSValues with // ValueRep nodes, so FPRs are initially free // // (b) The only recoveries with wanted registers are the // callee (which always starts out in a register) and // the callee-save registers // // (c) The callee-save registers are the first things we // load (after the return PC), and they are loaded as JSValues // // (d) We prefer loading JSValues into FPRs if their // wanted GPR is not available // // (e) If we end up spilling some registers with a // target, we won't load them again before the very // end of the algorithm // // Combined, this means that we will never load a recovery // with a wanted GPR into any GPR other than its wanted // GPR. The callee could however have been initially in // one of the callee-save registers - but since the wanted // GPR for the callee is always regT0, it will be the // first one to be displaced, and we won't see it when // handling any of the callee-save registers. // // Thus, the only way we could ever reach this path is in // the FTL, when there is so much pressure that we // absolutely need to load the callee-save registers into // different GPRs initially but not enough pressure to // then have to spill all of them. And even in that case, // depending on the order in which B3 saves the // callee-saves, we will probably still be safe. Anyway, // the couple extra move instructions compared to an // efficient cycle-based algorithm are not going to hurt // us. if (wantedReg.isFPR()) { FPRReg tempFPR = getFreeFPR(); if (verbose) dataLog(" * Moving ", wantedReg, " into ", tempFPR, "\n"); m_jit.moveDouble(wantedReg.fpr(), tempFPR); updateRecovery(*current, ValueRecovery::inFPR(tempFPR, current->recovery().dataFormat())); } else { GPRReg tempGPR = getFreeGPR(); if (verbose) dataLog(" * Moving ", wantedReg.gpr(), " into ", tempGPR, "\n"); m_jit.move(wantedReg.gpr(), tempGPR); updateRecovery(*current, ValueRecovery::inGPR(tempGPR, current->recovery().dataFormat())); } } ASSERT(!m_registers[wantedReg]); if (cachedRecovery.recovery().isConstant()) { // We only care about callee saves for wanted FPRs, and those are never constants ASSERT(wantedReg.isGPR()); if (verbose) dataLog(" * Loading ", cachedRecovery.recovery().constant(), " into ", wantedReg, "\n"); m_jit.moveTrustedValue(cachedRecovery.recovery().constant(), JSValueRegs { wantedReg.gpr() }); updateRecovery( cachedRecovery, ValueRecovery::inRegister(wantedReg, DataFormatJS)); } else if (cachedRecovery.recovery().isInGPR()) { if (verbose) dataLog(" * Moving ", cachedRecovery.recovery(), " into ", wantedReg, "\n"); if (wantedReg.isGPR()) m_jit.move(cachedRecovery.recovery().gpr(), wantedReg.gpr()); else m_jit.move64ToDouble(cachedRecovery.recovery().gpr(), wantedReg.fpr()); RELEASE_ASSERT(cachedRecovery.recovery().dataFormat() == DataFormatJS); updateRecovery(cachedRecovery, ValueRecovery::inRegister(wantedReg, DataFormatJS)); } else { ASSERT(cachedRecovery.recovery().isInFPR()); if (cachedRecovery.recovery().dataFormat() == DataFormatDouble) { // We only care about callee saves for wanted FPRs, and those are always DataFormatJS ASSERT(wantedReg.isGPR()); // This will automatically pick the wanted GPR emitBox(cachedRecovery); } else { if (verbose) dataLog(" * Moving ", cachedRecovery.recovery().fpr(), " into ", wantedReg, "\n"); if (wantedReg.isGPR()) m_jit.moveDoubleTo64(cachedRecovery.recovery().fpr(), wantedReg.gpr()); else m_jit.moveDouble(cachedRecovery.recovery().fpr(), wantedReg.fpr()); RELEASE_ASSERT(cachedRecovery.recovery().dataFormat() == DataFormatJS); updateRecovery(cachedRecovery, ValueRecovery::inRegister(wantedReg, DataFormatJS)); } } ASSERT(m_registers[wantedReg] == &cachedRecovery); }
static void compileStub( unsigned exitID, JITCode* jitCode, OSRExit& exit, VM* vm, CodeBlock* codeBlock) { StackMaps::Record* record = nullptr; for (unsigned i = jitCode->stackmaps.records.size(); i--;) { record = &jitCode->stackmaps.records[i]; if (record->patchpointID == exit.m_stackmapID) break; } RELEASE_ASSERT(record->patchpointID == exit.m_stackmapID); // This code requires framePointerRegister is the same as callFrameRegister static_assert(MacroAssembler::framePointerRegister == GPRInfo::callFrameRegister, "MacroAssembler::framePointerRegister and GPRInfo::callFrameRegister must be the same"); CCallHelpers jit(vm, codeBlock); // We need scratch space to save all registers, to build up the JS stack, to deal with unwind // fixup, pointers to all of the objects we materialize, and the elements inside those objects // that we materialize. // Figure out how much space we need for those object allocations. unsigned numMaterializations = 0; size_t maxMaterializationNumArguments = 0; for (ExitTimeObjectMaterialization* materialization : exit.m_materializations) { numMaterializations++; maxMaterializationNumArguments = std::max( maxMaterializationNumArguments, materialization->properties().size()); } ScratchBuffer* scratchBuffer = vm->scratchBufferForSize( sizeof(EncodedJSValue) * ( exit.m_values.size() + numMaterializations + maxMaterializationNumArguments) + requiredScratchMemorySizeInBytes() + codeBlock->calleeSaveRegisters()->size() * sizeof(uint64_t)); EncodedJSValue* scratch = scratchBuffer ? static_cast<EncodedJSValue*>(scratchBuffer->dataBuffer()) : 0; EncodedJSValue* materializationPointers = scratch + exit.m_values.size(); EncodedJSValue* materializationArguments = materializationPointers + numMaterializations; char* registerScratch = bitwise_cast<char*>(materializationArguments + maxMaterializationNumArguments); uint64_t* unwindScratch = bitwise_cast<uint64_t*>(registerScratch + requiredScratchMemorySizeInBytes()); HashMap<ExitTimeObjectMaterialization*, EncodedJSValue*> materializationToPointer; unsigned materializationCount = 0; for (ExitTimeObjectMaterialization* materialization : exit.m_materializations) { materializationToPointer.add( materialization, materializationPointers + materializationCount++); } // Note that we come in here, the stack used to be as LLVM left it except that someone called pushToSave(). // We don't care about the value they saved. But, we do appreciate the fact that they did it, because we use // that slot for saveAllRegisters(). saveAllRegisters(jit, registerScratch); // Bring the stack back into a sane form and assert that it's sane. jit.popToRestore(GPRInfo::regT0); jit.checkStackPointerAlignment(); if (vm->m_perBytecodeProfiler && codeBlock->jitCode()->dfgCommon()->compilation) { Profiler::Database& database = *vm->m_perBytecodeProfiler; Profiler::Compilation* compilation = codeBlock->jitCode()->dfgCommon()->compilation.get(); Profiler::OSRExit* profilerExit = compilation->addOSRExit( exitID, Profiler::OriginStack(database, codeBlock, exit.m_codeOrigin), exit.m_kind, exit.m_kind == UncountableInvalidation); jit.add64(CCallHelpers::TrustedImm32(1), CCallHelpers::AbsoluteAddress(profilerExit->counterAddress())); } // The remaining code assumes that SP/FP are in the same state that they were in the FTL's // call frame. // Get the call frame and tag thingies. // Restore the exiting function's callFrame value into a regT4 jit.move(MacroAssembler::TrustedImm64(TagTypeNumber), GPRInfo::tagTypeNumberRegister); jit.move(MacroAssembler::TrustedImm64(TagMask), GPRInfo::tagMaskRegister); // Do some value profiling. if (exit.m_profileDataFormat != DataFormatNone) { record->locations[0].restoreInto(jit, jitCode->stackmaps, registerScratch, GPRInfo::regT0); reboxAccordingToFormat( exit.m_profileDataFormat, jit, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT2); if (exit.m_kind == BadCache || exit.m_kind == BadIndexingType) { CodeOrigin codeOrigin = exit.m_codeOriginForExitProfile; if (ArrayProfile* arrayProfile = jit.baselineCodeBlockFor(codeOrigin)->getArrayProfile(codeOrigin.bytecodeIndex)) { jit.load32(MacroAssembler::Address(GPRInfo::regT0, JSCell::structureIDOffset()), GPRInfo::regT1); jit.store32(GPRInfo::regT1, arrayProfile->addressOfLastSeenStructureID()); jit.load8(MacroAssembler::Address(GPRInfo::regT0, JSCell::indexingTypeOffset()), GPRInfo::regT1); jit.move(MacroAssembler::TrustedImm32(1), GPRInfo::regT2); jit.lshift32(GPRInfo::regT1, GPRInfo::regT2); jit.or32(GPRInfo::regT2, MacroAssembler::AbsoluteAddress(arrayProfile->addressOfArrayModes())); } } if (!!exit.m_valueProfile) jit.store64(GPRInfo::regT0, exit.m_valueProfile.getSpecFailBucket(0)); } // Materialize all objects. Don't materialize an object until all // of the objects it needs have been materialized. We break cycles // by populating objects late - we only consider an object as // needing another object if the later is needed for the // allocation of the former. HashSet<ExitTimeObjectMaterialization*> toMaterialize; for (ExitTimeObjectMaterialization* materialization : exit.m_materializations) toMaterialize.add(materialization); while (!toMaterialize.isEmpty()) { unsigned previousToMaterializeSize = toMaterialize.size(); Vector<ExitTimeObjectMaterialization*> worklist; worklist.appendRange(toMaterialize.begin(), toMaterialize.end()); for (ExitTimeObjectMaterialization* materialization : worklist) { // Check if we can do anything about this right now. bool allGood = true; for (ExitPropertyValue value : materialization->properties()) { if (!value.value().isObjectMaterialization()) continue; if (!value.location().neededForMaterialization()) continue; if (toMaterialize.contains(value.value().objectMaterialization())) { // Gotta skip this one, since it needs a // materialization that hasn't been materialized. allGood = false; break; } } if (!allGood) continue; // All systems go for materializing the object. First we // recover the values of all of its fields and then we // call a function to actually allocate the beast. // We only recover the fields that are needed for the allocation. for (unsigned propertyIndex = materialization->properties().size(); propertyIndex--;) { const ExitPropertyValue& property = materialization->properties()[propertyIndex]; const ExitValue& value = property.value(); if (!property.location().neededForMaterialization()) continue; compileRecovery( jit, value, record, jitCode->stackmaps, registerScratch, materializationToPointer); jit.storePtr(GPRInfo::regT0, materializationArguments + propertyIndex); } // This call assumes that we don't pass arguments on the stack. jit.setupArgumentsWithExecState( CCallHelpers::TrustedImmPtr(materialization), CCallHelpers::TrustedImmPtr(materializationArguments)); jit.move(CCallHelpers::TrustedImmPtr(bitwise_cast<void*>(operationMaterializeObjectInOSR)), GPRInfo::nonArgGPR0); jit.call(GPRInfo::nonArgGPR0); jit.storePtr(GPRInfo::returnValueGPR, materializationToPointer.get(materialization)); // Let everyone know that we're done. toMaterialize.remove(materialization); } // We expect progress! This ensures that we crash rather than looping infinitely if there // is something broken about this fixpoint. Or, this could happen if we ever violate the // "materializations form a DAG" rule. RELEASE_ASSERT(toMaterialize.size() < previousToMaterializeSize); } // Now that all the objects have been allocated, we populate them // with the correct values. This time we can recover all the // fields, including those that are only needed for the allocation. for (ExitTimeObjectMaterialization* materialization : exit.m_materializations) { for (unsigned propertyIndex = materialization->properties().size(); propertyIndex--;) { const ExitValue& value = materialization->properties()[propertyIndex].value(); compileRecovery( jit, value, record, jitCode->stackmaps, registerScratch, materializationToPointer); jit.storePtr(GPRInfo::regT0, materializationArguments + propertyIndex); } // This call assumes that we don't pass arguments on the stack jit.setupArgumentsWithExecState( CCallHelpers::TrustedImmPtr(materialization), CCallHelpers::TrustedImmPtr(materializationToPointer.get(materialization)), CCallHelpers::TrustedImmPtr(materializationArguments)); jit.move(CCallHelpers::TrustedImmPtr(bitwise_cast<void*>(operationPopulateObjectInOSR)), GPRInfo::nonArgGPR0); jit.call(GPRInfo::nonArgGPR0); } // Save all state from wherever the exit data tells us it was, into the appropriate place in // the scratch buffer. This also does the reboxing. for (unsigned index = exit.m_values.size(); index--;) { compileRecovery( jit, exit.m_values[index], record, jitCode->stackmaps, registerScratch, materializationToPointer); jit.store64(GPRInfo::regT0, scratch + index); } // Henceforth we make it look like the exiting function was called through a register // preservation wrapper. This implies that FP must be nudged down by a certain amount. Then // we restore the various things according to either exit.m_values or by copying from the // old frame, and finally we save the various callee-save registers into where the // restoration thunk would restore them from. // Before we start messing with the frame, we need to set aside any registers that the // FTL code was preserving. for (unsigned i = codeBlock->calleeSaveRegisters()->size(); i--;) { RegisterAtOffset entry = codeBlock->calleeSaveRegisters()->at(i); jit.load64( MacroAssembler::Address(MacroAssembler::framePointerRegister, entry.offset()), GPRInfo::regT0); jit.store64(GPRInfo::regT0, unwindScratch + i); } jit.load32(CCallHelpers::payloadFor(JSStack::ArgumentCount), GPRInfo::regT2); // Let's say that the FTL function had failed its arity check. In that case, the stack will // contain some extra stuff. // // We compute the padded stack space: // // paddedStackSpace = roundUp(codeBlock->numParameters - regT2 + 1) // // The stack will have regT2 + CallFrameHeaderSize stuff. // We want to make the stack look like this, from higher addresses down: // // - argument padding // - actual arguments // - call frame header // This code assumes that we're dealing with FunctionCode. RELEASE_ASSERT(codeBlock->codeType() == FunctionCode); jit.add32( MacroAssembler::TrustedImm32(-codeBlock->numParameters()), GPRInfo::regT2, GPRInfo::regT3); MacroAssembler::Jump arityIntact = jit.branch32( MacroAssembler::GreaterThanOrEqual, GPRInfo::regT3, MacroAssembler::TrustedImm32(0)); jit.neg32(GPRInfo::regT3); jit.add32(MacroAssembler::TrustedImm32(1 + stackAlignmentRegisters() - 1), GPRInfo::regT3); jit.and32(MacroAssembler::TrustedImm32(-stackAlignmentRegisters()), GPRInfo::regT3); jit.add32(GPRInfo::regT3, GPRInfo::regT2); arityIntact.link(&jit); CodeBlock* baselineCodeBlock = jit.baselineCodeBlockFor(exit.m_codeOrigin); // First set up SP so that our data doesn't get clobbered by signals. unsigned conservativeStackDelta = (exit.m_values.numberOfLocals() + baselineCodeBlock->calleeSaveSpaceAsVirtualRegisters()) * sizeof(Register) + maxFrameExtentForSlowPathCall; conservativeStackDelta = WTF::roundUpToMultipleOf( stackAlignmentBytes(), conservativeStackDelta); jit.addPtr( MacroAssembler::TrustedImm32(-conservativeStackDelta), MacroAssembler::framePointerRegister, MacroAssembler::stackPointerRegister); jit.checkStackPointerAlignment(); RegisterSet allFTLCalleeSaves = RegisterSet::ftlCalleeSaveRegisters(); RegisterAtOffsetList* baselineCalleeSaves = baselineCodeBlock->calleeSaveRegisters(); for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) { if (!allFTLCalleeSaves.get(reg)) continue; unsigned unwindIndex = codeBlock->calleeSaveRegisters()->indexOf(reg); RegisterAtOffset* baselineRegisterOffset = baselineCalleeSaves->find(reg); if (reg.isGPR()) { GPRReg regToLoad = baselineRegisterOffset ? GPRInfo::regT0 : reg.gpr(); if (unwindIndex == UINT_MAX) { // The FTL compilation didn't preserve this register. This means that it also // didn't use the register. So its value at the beginning of OSR exit should be // preserved by the thunk. Luckily, we saved all registers into the register // scratch buffer, so we can restore them from there. jit.load64(registerScratch + offsetOfReg(reg), regToLoad); } else { // The FTL compilation preserved the register. Its new value is therefore // irrelevant, but we can get the value that was preserved by using the unwind // data. We've already copied all unwind-able preserved registers into the unwind // scratch buffer, so we can get it from there. jit.load64(unwindScratch + unwindIndex, regToLoad); } if (baselineRegisterOffset) jit.store64(regToLoad, MacroAssembler::Address(MacroAssembler::framePointerRegister, baselineRegisterOffset->offset())); } else { FPRReg fpRegToLoad = baselineRegisterOffset ? FPRInfo::fpRegT0 : reg.fpr(); if (unwindIndex == UINT_MAX) jit.loadDouble(MacroAssembler::TrustedImmPtr(registerScratch + offsetOfReg(reg)), fpRegToLoad); else jit.loadDouble(MacroAssembler::TrustedImmPtr(unwindScratch + unwindIndex), fpRegToLoad); if (baselineRegisterOffset) jit.storeDouble(fpRegToLoad, MacroAssembler::Address(MacroAssembler::framePointerRegister, baselineRegisterOffset->offset())); } } size_t baselineVirtualRegistersForCalleeSaves = baselineCodeBlock->calleeSaveSpaceAsVirtualRegisters(); // Now get state out of the scratch buffer and place it back into the stack. The values are // already reboxed so we just move them. for (unsigned index = exit.m_values.size(); index--;) { VirtualRegister reg = exit.m_values.virtualRegisterForIndex(index); if (reg.isLocal() && reg.toLocal() < static_cast<int>(baselineVirtualRegistersForCalleeSaves)) continue; jit.load64(scratch + index, GPRInfo::regT0); jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(reg)); } handleExitCounts(jit, exit); reifyInlinedCallFrames(jit, exit); adjustAndJumpToTarget(jit, exit, false); LinkBuffer patchBuffer(*vm, jit, codeBlock); exit.m_code = FINALIZE_CODE_IF( shouldDumpDisassembly() || Options::verboseOSR() || Options::verboseFTLOSRExit(), patchBuffer, ("FTL OSR exit #%u (%s, %s) from %s, with operands = %s, and record = %s", exitID, toCString(exit.m_codeOrigin).data(), exitKindToString(exit.m_kind), toCString(*codeBlock).data(), toCString(ignoringContext<DumpContext>(exit.m_values)).data(), toCString(*record).data())); }