DFGOSRExitCompiler32_64.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Copyright (C) 2011 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. #include "config.h"
  26. #include "DFGOSRExitCompiler.h"
  27. #if ENABLE(DFG_JIT) && USE(JSVALUE32_64)
  28. #include "DFGOperations.h"
  29. #include "Operations.h"
  30. #include <wtf/DataLog.h>
  31. namespace JSC { namespace DFG {
  32. void OSRExitCompiler::compileExit(const OSRExit& exit, const Operands<ValueRecovery>& operands, SpeculationRecovery* recovery)
  33. {
  34. // 1) Pro-forma stuff.
  35. #if DFG_ENABLE(DEBUG_VERBOSE)
  36. dataLogF("OSR exit (");
  37. for (CodeOrigin codeOrigin = exit.m_codeOrigin; ; codeOrigin = codeOrigin.inlineCallFrame->caller) {
  38. dataLogF("bc#%u", codeOrigin.bytecodeIndex);
  39. if (!codeOrigin.inlineCallFrame)
  40. break;
  41. dataLogF(" -> %p ", codeOrigin.inlineCallFrame->executable.get());
  42. }
  43. dataLogF(") at JIT offset 0x%x ", m_jit.debugOffset());
  44. dumpOperands(operands, WTF::dataFile());
  45. #endif
  46. if (Options::printEachOSRExit()) {
  47. SpeculationFailureDebugInfo* debugInfo = new SpeculationFailureDebugInfo;
  48. debugInfo->codeBlock = m_jit.codeBlock();
  49. m_jit.debugCall(debugOperationPrintSpeculationFailure, debugInfo);
  50. }
  51. #if DFG_ENABLE(JIT_BREAK_ON_SPECULATION_FAILURE)
  52. m_jit.breakpoint();
  53. #endif
  54. #if DFG_ENABLE(SUCCESS_STATS)
  55. static SamplingCounter counter("SpeculationFailure");
  56. m_jit.emitCount(counter);
  57. #endif
  58. // 2) Perform speculation recovery. This only comes into play when an operation
  59. // starts mutating state before verifying the speculation it has already made.
  60. if (recovery) {
  61. switch (recovery->type()) {
  62. case SpeculativeAdd:
  63. m_jit.sub32(recovery->src(), recovery->dest());
  64. break;
  65. case BooleanSpeculationCheck:
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. // 3) Refine some value profile, if appropriate.
  72. if (!!exit.m_jsValueSource) {
  73. if (exit.m_kind == BadCache || exit.m_kind == BadIndexingType) {
  74. // If the instruction that this originated from has an array profile, then
  75. // refine it. If it doesn't, then do nothing. The latter could happen for
  76. // hoisted checks, or checks emitted for operations that didn't have array
  77. // profiling - either ops that aren't array accesses at all, or weren't
  78. // known to be array acceses in the bytecode. The latter case is a FIXME
  79. // while the former case is an outcome of a CheckStructure not knowing why
  80. // it was emitted (could be either due to an inline cache of a property
  81. // property access, or due to an array profile).
  82. // Note: We are free to assume that the jsValueSource is already known to
  83. // be a cell since both BadCache and BadIndexingType exits occur after
  84. // the cell check would have already happened.
  85. CodeOrigin codeOrigin = exit.m_codeOriginForExitProfile;
  86. if (ArrayProfile* arrayProfile = m_jit.baselineCodeBlockFor(codeOrigin)->getArrayProfile(codeOrigin.bytecodeIndex)) {
  87. GPRReg usedRegister1;
  88. GPRReg usedRegister2;
  89. if (exit.m_jsValueSource.isAddress()) {
  90. usedRegister1 = exit.m_jsValueSource.base();
  91. usedRegister2 = InvalidGPRReg;
  92. } else {
  93. usedRegister1 = exit.m_jsValueSource.payloadGPR();
  94. if (exit.m_jsValueSource.hasKnownTag())
  95. usedRegister2 = InvalidGPRReg;
  96. else
  97. usedRegister2 = exit.m_jsValueSource.tagGPR();
  98. }
  99. GPRReg scratch1;
  100. GPRReg scratch2;
  101. scratch1 = AssemblyHelpers::selectScratchGPR(usedRegister1, usedRegister2);
  102. scratch2 = AssemblyHelpers::selectScratchGPR(usedRegister1, usedRegister2, scratch1);
  103. m_jit.push(scratch1);
  104. m_jit.push(scratch2);
  105. GPRReg value;
  106. if (exit.m_jsValueSource.isAddress()) {
  107. value = scratch1;
  108. m_jit.loadPtr(AssemblyHelpers::Address(exit.m_jsValueSource.asAddress()), value);
  109. } else
  110. value = exit.m_jsValueSource.payloadGPR();
  111. m_jit.loadPtr(AssemblyHelpers::Address(value, JSCell::structureOffset()), scratch1);
  112. m_jit.storePtr(scratch1, arrayProfile->addressOfLastSeenStructure());
  113. m_jit.load8(AssemblyHelpers::Address(scratch1, Structure::indexingTypeOffset()), scratch1);
  114. m_jit.move(AssemblyHelpers::TrustedImm32(1), scratch2);
  115. m_jit.lshift32(scratch1, scratch2);
  116. m_jit.or32(scratch2, AssemblyHelpers::AbsoluteAddress(arrayProfile->addressOfArrayModes()));
  117. m_jit.pop(scratch2);
  118. m_jit.pop(scratch1);
  119. }
  120. }
  121. if (!!exit.m_valueProfile) {
  122. EncodedJSValue* bucket = exit.m_valueProfile.getSpecFailBucket(0);
  123. if (exit.m_jsValueSource.isAddress()) {
  124. // Save a register so we can use it.
  125. GPRReg scratch = AssemblyHelpers::selectScratchGPR(exit.m_jsValueSource.base());
  126. m_jit.push(scratch);
  127. m_jit.load32(exit.m_jsValueSource.asAddress(OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag)), scratch);
  128. m_jit.store32(scratch, &bitwise_cast<EncodedValueDescriptor*>(bucket)->asBits.tag);
  129. m_jit.load32(exit.m_jsValueSource.asAddress(OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)), scratch);
  130. m_jit.store32(scratch, &bitwise_cast<EncodedValueDescriptor*>(bucket)->asBits.payload);
  131. m_jit.pop(scratch);
  132. } else if (exit.m_jsValueSource.hasKnownTag()) {
  133. m_jit.store32(AssemblyHelpers::TrustedImm32(exit.m_jsValueSource.tag()), &bitwise_cast<EncodedValueDescriptor*>(bucket)->asBits.tag);
  134. m_jit.store32(exit.m_jsValueSource.payloadGPR(), &bitwise_cast<EncodedValueDescriptor*>(bucket)->asBits.payload);
  135. } else {
  136. m_jit.store32(exit.m_jsValueSource.tagGPR(), &bitwise_cast<EncodedValueDescriptor*>(bucket)->asBits.tag);
  137. m_jit.store32(exit.m_jsValueSource.payloadGPR(), &bitwise_cast<EncodedValueDescriptor*>(bucket)->asBits.payload);
  138. }
  139. }
  140. }
  141. // 4) Figure out how many scratch slots we'll need. We need one for every GPR/FPR
  142. // whose destination is now occupied by a DFG virtual register, and we need
  143. // one for every displaced virtual register if there are more than
  144. // GPRInfo::numberOfRegisters of them. Also see if there are any constants,
  145. // any undefined slots, any FPR slots, and any unboxed ints.
  146. Vector<bool> poisonedVirtualRegisters(operands.numberOfLocals());
  147. for (unsigned i = 0; i < poisonedVirtualRegisters.size(); ++i)
  148. poisonedVirtualRegisters[i] = false;
  149. unsigned numberOfPoisonedVirtualRegisters = 0;
  150. unsigned numberOfDisplacedVirtualRegisters = 0;
  151. // Booleans for fast checks. We expect that most OSR exits do not have to rebox
  152. // Int32s, have no FPRs, and have no constants. If there are constants, we
  153. // expect most of them to be jsUndefined(); if that's true then we handle that
  154. // specially to minimize code size and execution time.
  155. bool haveUnboxedInt32InJSStack = false;
  156. bool haveUnboxedCellInJSStack = false;
  157. bool haveUnboxedBooleanInJSStack = false;
  158. bool haveUInt32s = false;
  159. bool haveFPRs = false;
  160. bool haveConstants = false;
  161. bool haveUndefined = false;
  162. bool haveArguments = false;
  163. for (size_t index = 0; index < operands.size(); ++index) {
  164. const ValueRecovery& recovery = operands[index];
  165. switch (recovery.technique()) {
  166. case DisplacedInJSStack:
  167. case Int32DisplacedInJSStack:
  168. case CellDisplacedInJSStack:
  169. case BooleanDisplacedInJSStack:
  170. numberOfDisplacedVirtualRegisters++;
  171. ASSERT((int)recovery.virtualRegister() >= 0);
  172. // See if we might like to store to this virtual register before doing
  173. // virtual register shuffling. If so, we say that the virtual register
  174. // is poisoned: it cannot be stored to until after displaced virtual
  175. // registers are handled. We track poisoned virtual register carefully
  176. // to ensure this happens efficiently. Note that we expect this case
  177. // to be rare, so the handling of it is optimized for the cases in
  178. // which it does not happen.
  179. if (recovery.virtualRegister() < (int)operands.numberOfLocals()) {
  180. switch (operands.local(recovery.virtualRegister()).technique()) {
  181. case InGPR:
  182. case UnboxedInt32InGPR:
  183. case UnboxedBooleanInGPR:
  184. case UInt32InGPR:
  185. case InPair:
  186. case InFPR:
  187. if (!poisonedVirtualRegisters[recovery.virtualRegister()]) {
  188. poisonedVirtualRegisters[recovery.virtualRegister()] = true;
  189. numberOfPoisonedVirtualRegisters++;
  190. }
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. break;
  197. case UInt32InGPR:
  198. haveUInt32s = true;
  199. break;
  200. case AlreadyInJSStackAsUnboxedInt32:
  201. haveUnboxedInt32InJSStack = true;
  202. break;
  203. case AlreadyInJSStackAsUnboxedCell:
  204. haveUnboxedCellInJSStack = true;
  205. break;
  206. case AlreadyInJSStackAsUnboxedBoolean:
  207. haveUnboxedBooleanInJSStack = true;
  208. break;
  209. case InFPR:
  210. haveFPRs = true;
  211. break;
  212. case Constant:
  213. haveConstants = true;
  214. if (recovery.constant().isUndefined())
  215. haveUndefined = true;
  216. break;
  217. case ArgumentsThatWereNotCreated:
  218. haveArguments = true;
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. unsigned scratchBufferLengthBeforeUInt32s = numberOfPoisonedVirtualRegisters + ((numberOfDisplacedVirtualRegisters * 2) <= GPRInfo::numberOfRegisters ? 0 : numberOfDisplacedVirtualRegisters);
  225. ScratchBuffer* scratchBuffer = m_jit.vm()->scratchBufferForSize(sizeof(EncodedJSValue) * (scratchBufferLengthBeforeUInt32s + (haveUInt32s ? 2 : 0)));
  226. EncodedJSValue* scratchDataBuffer = scratchBuffer ? static_cast<EncodedJSValue*>(scratchBuffer->dataBuffer()) : 0;
  227. // From here on, the code assumes that it is profitable to maximize the distance
  228. // between when something is computed and when it is stored.
  229. // 5) Perform all reboxing of integers and cells, except for those in registers.
  230. if (haveUnboxedInt32InJSStack || haveUnboxedCellInJSStack || haveUnboxedBooleanInJSStack) {
  231. for (size_t index = 0; index < operands.size(); ++index) {
  232. const ValueRecovery& recovery = operands[index];
  233. switch (recovery.technique()) {
  234. case AlreadyInJSStackAsUnboxedInt32:
  235. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::Int32Tag), AssemblyHelpers::tagFor(static_cast<VirtualRegister>(operands.operandForIndex(index))));
  236. break;
  237. case AlreadyInJSStackAsUnboxedCell:
  238. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::CellTag), AssemblyHelpers::tagFor(static_cast<VirtualRegister>(operands.operandForIndex(index))));
  239. break;
  240. case AlreadyInJSStackAsUnboxedBoolean:
  241. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::BooleanTag), AssemblyHelpers::tagFor(static_cast<VirtualRegister>(operands.operandForIndex(index))));
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. }
  248. // 6) Dump all non-poisoned GPRs. For poisoned GPRs, save them into the scratch storage.
  249. // Note that GPRs do not have a fast change (like haveFPRs) because we expect that
  250. // most OSR failure points will have at least one GPR that needs to be dumped.
  251. initializePoisoned(operands.numberOfLocals());
  252. unsigned currentPoisonIndex = 0;
  253. for (size_t index = 0; index < operands.size(); ++index) {
  254. const ValueRecovery& recovery = operands[index];
  255. int operand = operands.operandForIndex(index);
  256. switch (recovery.technique()) {
  257. case InGPR:
  258. case UnboxedInt32InGPR:
  259. case UnboxedBooleanInGPR:
  260. if (operands.isVariable(index) && poisonedVirtualRegisters[operands.variableForIndex(index)]) {
  261. m_jit.store32(recovery.gpr(), reinterpret_cast<char*>(scratchDataBuffer + currentPoisonIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
  262. m_poisonScratchIndices[operands.variableForIndex(index)] = currentPoisonIndex;
  263. currentPoisonIndex++;
  264. } else {
  265. uint32_t tag = JSValue::EmptyValueTag;
  266. if (recovery.technique() == InGPR)
  267. tag = JSValue::CellTag;
  268. else if (recovery.technique() == UnboxedInt32InGPR)
  269. tag = JSValue::Int32Tag;
  270. else
  271. tag = JSValue::BooleanTag;
  272. m_jit.store32(AssemblyHelpers::TrustedImm32(tag), AssemblyHelpers::tagFor((VirtualRegister)operand));
  273. m_jit.store32(recovery.gpr(), AssemblyHelpers::payloadFor((VirtualRegister)operand));
  274. }
  275. break;
  276. case InPair:
  277. if (operands.isVariable(index) && poisonedVirtualRegisters[operands.variableForIndex(index)]) {
  278. m_jit.store32(recovery.tagGPR(), reinterpret_cast<char*>(scratchDataBuffer + currentPoisonIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag));
  279. m_jit.store32(recovery.payloadGPR(), reinterpret_cast<char*>(scratchDataBuffer + currentPoisonIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
  280. m_poisonScratchIndices[operands.variableForIndex(index)] = currentPoisonIndex;
  281. currentPoisonIndex++;
  282. } else {
  283. m_jit.store32(recovery.tagGPR(), AssemblyHelpers::tagFor((VirtualRegister)operand));
  284. m_jit.store32(recovery.payloadGPR(), AssemblyHelpers::payloadFor((VirtualRegister)operand));
  285. }
  286. break;
  287. case UInt32InGPR: {
  288. EncodedJSValue* myScratch = scratchDataBuffer + scratchBufferLengthBeforeUInt32s;
  289. GPRReg addressGPR = GPRInfo::regT0;
  290. if (addressGPR == recovery.gpr())
  291. addressGPR = GPRInfo::regT1;
  292. m_jit.storePtr(addressGPR, myScratch);
  293. m_jit.move(AssemblyHelpers::TrustedImmPtr(myScratch + 1), addressGPR);
  294. m_jit.storeDouble(FPRInfo::fpRegT0, addressGPR);
  295. AssemblyHelpers::Jump positive = m_jit.branch32(AssemblyHelpers::GreaterThanOrEqual, recovery.gpr(), AssemblyHelpers::TrustedImm32(0));
  296. m_jit.convertInt32ToDouble(recovery.gpr(), FPRInfo::fpRegT0);
  297. m_jit.addDouble(AssemblyHelpers::AbsoluteAddress(&AssemblyHelpers::twoToThe32), FPRInfo::fpRegT0);
  298. if (operands.isVariable(index) && poisonedVirtualRegisters[operands.variableForIndex(index)]) {
  299. m_jit.move(AssemblyHelpers::TrustedImmPtr(scratchDataBuffer + currentPoisonIndex), addressGPR);
  300. m_jit.storeDouble(FPRInfo::fpRegT0, addressGPR);
  301. } else
  302. m_jit.storeDouble(FPRInfo::fpRegT0, AssemblyHelpers::addressFor((VirtualRegister)operand));
  303. AssemblyHelpers::Jump done = m_jit.jump();
  304. positive.link(&m_jit);
  305. if (operands.isVariable(index) && poisonedVirtualRegisters[operands.variableForIndex(index)]) {
  306. m_jit.store32(recovery.gpr(), reinterpret_cast<char*>(scratchDataBuffer + currentPoisonIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
  307. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::Int32Tag), reinterpret_cast<char*>(scratchDataBuffer + currentPoisonIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag));
  308. } else {
  309. m_jit.store32(recovery.gpr(), AssemblyHelpers::payloadFor((VirtualRegister)operand));
  310. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::Int32Tag), AssemblyHelpers::tagFor((VirtualRegister)operand));
  311. }
  312. done.link(&m_jit);
  313. m_jit.move(AssemblyHelpers::TrustedImmPtr(myScratch + 1), addressGPR);
  314. m_jit.loadDouble(addressGPR, FPRInfo::fpRegT0);
  315. m_jit.loadPtr(myScratch, addressGPR);
  316. if (operands.isVariable(index) && poisonedVirtualRegisters[operands.variableForIndex(index)]) {
  317. m_poisonScratchIndices[operands.variableForIndex(index)] = currentPoisonIndex;
  318. currentPoisonIndex++;
  319. }
  320. break;
  321. }
  322. default:
  323. break;
  324. }
  325. }
  326. // 7) Dump all doubles into the stack, or to the scratch storage if the
  327. // destination virtual register is poisoned.
  328. if (haveFPRs) {
  329. for (size_t index = 0; index < operands.size(); ++index) {
  330. const ValueRecovery& recovery = operands[index];
  331. if (recovery.technique() != InFPR)
  332. continue;
  333. if (operands.isVariable(index) && poisonedVirtualRegisters[operands.variableForIndex(index)]) {
  334. m_jit.storeDouble(recovery.fpr(), scratchDataBuffer + currentPoisonIndex);
  335. m_poisonScratchIndices[operands.variableForIndex(index)] = currentPoisonIndex;
  336. currentPoisonIndex++;
  337. } else
  338. m_jit.storeDouble(recovery.fpr(), AssemblyHelpers::addressFor((VirtualRegister)operands.operandForIndex(index)));
  339. }
  340. }
  341. // At this point all GPRs are available for scratch use.
  342. ASSERT(currentPoisonIndex == numberOfPoisonedVirtualRegisters);
  343. // 8) Reshuffle displaced virtual registers. Optimize for the case that
  344. // the number of displaced virtual registers is not more than the number
  345. // of available physical registers.
  346. if (numberOfDisplacedVirtualRegisters) {
  347. if (numberOfDisplacedVirtualRegisters * 2 <= GPRInfo::numberOfRegisters) {
  348. // So far this appears to be the case that triggers all the time, but
  349. // that is far from guaranteed.
  350. unsigned displacementIndex = 0;
  351. for (size_t index = 0; index < operands.size(); ++index) {
  352. const ValueRecovery& recovery = operands[index];
  353. switch (recovery.technique()) {
  354. case DisplacedInJSStack:
  355. m_jit.load32(AssemblyHelpers::payloadFor(recovery.virtualRegister()), GPRInfo::toRegister(displacementIndex++));
  356. m_jit.load32(AssemblyHelpers::tagFor(recovery.virtualRegister()), GPRInfo::toRegister(displacementIndex++));
  357. break;
  358. case Int32DisplacedInJSStack:
  359. m_jit.load32(AssemblyHelpers::payloadFor(recovery.virtualRegister()), GPRInfo::toRegister(displacementIndex++));
  360. m_jit.move(AssemblyHelpers::TrustedImm32(JSValue::Int32Tag), GPRInfo::toRegister(displacementIndex++));
  361. break;
  362. case CellDisplacedInJSStack:
  363. m_jit.load32(AssemblyHelpers::payloadFor(recovery.virtualRegister()), GPRInfo::toRegister(displacementIndex++));
  364. m_jit.move(AssemblyHelpers::TrustedImm32(JSValue::CellTag), GPRInfo::toRegister(displacementIndex++));
  365. break;
  366. case BooleanDisplacedInJSStack:
  367. m_jit.load32(AssemblyHelpers::payloadFor(recovery.virtualRegister()), GPRInfo::toRegister(displacementIndex++));
  368. m_jit.move(AssemblyHelpers::TrustedImm32(JSValue::BooleanTag), GPRInfo::toRegister(displacementIndex++));
  369. break;
  370. default:
  371. break;
  372. }
  373. }
  374. displacementIndex = 0;
  375. for (size_t index = 0; index < operands.size(); ++index) {
  376. const ValueRecovery& recovery = operands[index];
  377. switch (recovery.technique()) {
  378. case DisplacedInJSStack:
  379. case Int32DisplacedInJSStack:
  380. case CellDisplacedInJSStack:
  381. case BooleanDisplacedInJSStack:
  382. m_jit.store32(GPRInfo::toRegister(displacementIndex++), AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  383. m_jit.store32(GPRInfo::toRegister(displacementIndex++), AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. } else {
  390. // FIXME: This should use the shuffling algorithm that we use
  391. // for speculative->non-speculative jumps, if we ever discover that
  392. // some hot code with lots of live values that get displaced and
  393. // spilled really enjoys frequently failing speculation.
  394. // For now this code is engineered to be correct but probably not
  395. // super. In particular, it correctly handles cases where for example
  396. // the displacements are a permutation of the destination values, like
  397. //
  398. // 1 -> 2
  399. // 2 -> 1
  400. //
  401. // It accomplishes this by simply lifting all of the virtual registers
  402. // from their old (DFG JIT) locations and dropping them in a scratch
  403. // location in memory, and then transferring from that scratch location
  404. // to their new (old JIT) locations.
  405. unsigned scratchIndex = numberOfPoisonedVirtualRegisters;
  406. for (size_t index = 0; index < operands.size(); ++index) {
  407. const ValueRecovery& recovery = operands[index];
  408. switch (recovery.technique()) {
  409. case DisplacedInJSStack:
  410. m_jit.load32(AssemblyHelpers::payloadFor(recovery.virtualRegister()), GPRInfo::regT0);
  411. m_jit.load32(AssemblyHelpers::tagFor(recovery.virtualRegister()), GPRInfo::regT1);
  412. m_jit.store32(GPRInfo::regT0, reinterpret_cast<char*>(scratchDataBuffer + scratchIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
  413. m_jit.store32(GPRInfo::regT1, reinterpret_cast<char*>(scratchDataBuffer + scratchIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag));
  414. scratchIndex++;
  415. break;
  416. case Int32DisplacedInJSStack:
  417. case CellDisplacedInJSStack:
  418. case BooleanDisplacedInJSStack:
  419. m_jit.load32(AssemblyHelpers::payloadFor(recovery.virtualRegister()), GPRInfo::regT0);
  420. m_jit.store32(GPRInfo::regT0, reinterpret_cast<char*>(scratchDataBuffer + scratchIndex++) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload));
  421. break;
  422. default:
  423. break;
  424. }
  425. }
  426. scratchIndex = numberOfPoisonedVirtualRegisters;
  427. for (size_t index = 0; index < operands.size(); ++index) {
  428. const ValueRecovery& recovery = operands[index];
  429. switch (recovery.technique()) {
  430. case DisplacedInJSStack:
  431. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + scratchIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload), GPRInfo::regT0);
  432. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + scratchIndex) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag), GPRInfo::regT1);
  433. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  434. m_jit.store32(GPRInfo::regT1, AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  435. scratchIndex++;
  436. break;
  437. case Int32DisplacedInJSStack:
  438. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + scratchIndex++) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload), GPRInfo::regT0);
  439. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::Int32Tag), AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  440. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  441. break;
  442. case CellDisplacedInJSStack:
  443. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + scratchIndex++) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload), GPRInfo::regT0);
  444. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::CellTag), AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  445. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  446. break;
  447. case BooleanDisplacedInJSStack:
  448. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + scratchIndex++) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload), GPRInfo::regT0);
  449. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::BooleanTag), AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  450. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  451. break;
  452. default:
  453. break;
  454. }
  455. }
  456. ASSERT(scratchIndex == numberOfPoisonedVirtualRegisters + numberOfDisplacedVirtualRegisters);
  457. }
  458. }
  459. // 9) Dump all poisoned virtual registers.
  460. if (numberOfPoisonedVirtualRegisters) {
  461. for (int virtualRegister = 0; virtualRegister < (int)operands.numberOfLocals(); ++virtualRegister) {
  462. if (!poisonedVirtualRegisters[virtualRegister])
  463. continue;
  464. const ValueRecovery& recovery = operands.local(virtualRegister);
  465. switch (recovery.technique()) {
  466. case InGPR:
  467. case UnboxedInt32InGPR:
  468. case UnboxedBooleanInGPR: {
  469. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + poisonIndex(virtualRegister)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload), GPRInfo::regT0);
  470. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)virtualRegister));
  471. uint32_t tag = JSValue::EmptyValueTag;
  472. if (recovery.technique() == InGPR)
  473. tag = JSValue::CellTag;
  474. else if (recovery.technique() == UnboxedInt32InGPR)
  475. tag = JSValue::Int32Tag;
  476. else
  477. tag = JSValue::BooleanTag;
  478. m_jit.store32(AssemblyHelpers::TrustedImm32(tag), AssemblyHelpers::tagFor((VirtualRegister)virtualRegister));
  479. break;
  480. }
  481. case InFPR:
  482. case InPair:
  483. case UInt32InGPR:
  484. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + poisonIndex(virtualRegister)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload), GPRInfo::regT0);
  485. m_jit.load32(reinterpret_cast<char*>(scratchDataBuffer + poisonIndex(virtualRegister)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag), GPRInfo::regT1);
  486. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)virtualRegister));
  487. m_jit.store32(GPRInfo::regT1, AssemblyHelpers::tagFor((VirtualRegister)virtualRegister));
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. }
  494. // 10) Dump all constants. Optimize for Undefined, since that's a constant we see
  495. // often.
  496. if (haveConstants) {
  497. if (haveUndefined) {
  498. m_jit.move(AssemblyHelpers::TrustedImm32(jsUndefined().payload()), GPRInfo::regT0);
  499. m_jit.move(AssemblyHelpers::TrustedImm32(jsUndefined().tag()), GPRInfo::regT1);
  500. }
  501. for (size_t index = 0; index < operands.size(); ++index) {
  502. const ValueRecovery& recovery = operands[index];
  503. if (recovery.technique() != Constant)
  504. continue;
  505. if (recovery.constant().isUndefined()) {
  506. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  507. m_jit.store32(GPRInfo::regT1, AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  508. } else {
  509. m_jit.store32(AssemblyHelpers::TrustedImm32(recovery.constant().payload()), AssemblyHelpers::payloadFor((VirtualRegister)operands.operandForIndex(index)));
  510. m_jit.store32(AssemblyHelpers::TrustedImm32(recovery.constant().tag()), AssemblyHelpers::tagFor((VirtualRegister)operands.operandForIndex(index)));
  511. }
  512. }
  513. }
  514. // 12) Adjust the old JIT's execute counter. Since we are exiting OSR, we know
  515. // that all new calls into this code will go to the new JIT, so the execute
  516. // counter only affects call frames that performed OSR exit and call frames
  517. // that were still executing the old JIT at the time of another call frame's
  518. // OSR exit. We want to ensure that the following is true:
  519. //
  520. // (a) Code the performs an OSR exit gets a chance to reenter optimized
  521. // code eventually, since optimized code is faster. But we don't
  522. // want to do such reentery too aggressively (see (c) below).
  523. //
  524. // (b) If there is code on the call stack that is still running the old
  525. // JIT's code and has never OSR'd, then it should get a chance to
  526. // perform OSR entry despite the fact that we've exited.
  527. //
  528. // (c) Code the performs an OSR exit should not immediately retry OSR
  529. // entry, since both forms of OSR are expensive. OSR entry is
  530. // particularly expensive.
  531. //
  532. // (d) Frequent OSR failures, even those that do not result in the code
  533. // running in a hot loop, result in recompilation getting triggered.
  534. //
  535. // To ensure (c), we'd like to set the execute counter to
  536. // counterValueForOptimizeAfterWarmUp(). This seems like it would endanger
  537. // (a) and (b), since then every OSR exit would delay the opportunity for
  538. // every call frame to perform OSR entry. Essentially, if OSR exit happens
  539. // frequently and the function has few loops, then the counter will never
  540. // become non-negative and OSR entry will never be triggered. OSR entry
  541. // will only happen if a loop gets hot in the old JIT, which does a pretty
  542. // good job of ensuring (a) and (b). But that doesn't take care of (d),
  543. // since each speculation failure would reset the execute counter.
  544. // So we check here if the number of speculation failures is significantly
  545. // larger than the number of successes (we want 90% success rate), and if
  546. // there have been a large enough number of failures. If so, we set the
  547. // counter to 0; otherwise we set the counter to
  548. // counterValueForOptimizeAfterWarmUp().
  549. handleExitCounts(exit);
  550. // 13) Reify inlined call frames.
  551. ASSERT(m_jit.baselineCodeBlock()->getJITType() == JITCode::BaselineJIT);
  552. m_jit.storePtr(AssemblyHelpers::TrustedImmPtr(m_jit.baselineCodeBlock()), AssemblyHelpers::addressFor((VirtualRegister)JSStack::CodeBlock));
  553. for (CodeOrigin codeOrigin = exit.m_codeOrigin; codeOrigin.inlineCallFrame; codeOrigin = codeOrigin.inlineCallFrame->caller) {
  554. InlineCallFrame* inlineCallFrame = codeOrigin.inlineCallFrame;
  555. CodeBlock* baselineCodeBlock = m_jit.baselineCodeBlockFor(codeOrigin);
  556. CodeBlock* baselineCodeBlockForCaller = m_jit.baselineCodeBlockFor(inlineCallFrame->caller);
  557. Vector<BytecodeAndMachineOffset>& decodedCodeMap = m_jit.decodedCodeMapFor(baselineCodeBlockForCaller);
  558. unsigned returnBytecodeIndex = inlineCallFrame->caller.bytecodeIndex + OPCODE_LENGTH(op_call);
  559. BytecodeAndMachineOffset* mapping = binarySearch<BytecodeAndMachineOffset, unsigned>(decodedCodeMap, decodedCodeMap.size(), returnBytecodeIndex, BytecodeAndMachineOffset::getBytecodeIndex);
  560. ASSERT(mapping);
  561. ASSERT(mapping->m_bytecodeIndex == returnBytecodeIndex);
  562. void* jumpTarget = baselineCodeBlockForCaller->getJITCode().executableAddressAtOffset(mapping->m_machineCodeOffset);
  563. GPRReg callerFrameGPR;
  564. if (inlineCallFrame->caller.inlineCallFrame) {
  565. m_jit.add32(AssemblyHelpers::TrustedImm32(inlineCallFrame->caller.inlineCallFrame->stackOffset * sizeof(EncodedJSValue)), GPRInfo::callFrameRegister, GPRInfo::regT3);
  566. callerFrameGPR = GPRInfo::regT3;
  567. } else
  568. callerFrameGPR = GPRInfo::callFrameRegister;
  569. m_jit.storePtr(AssemblyHelpers::TrustedImmPtr(baselineCodeBlock), AssemblyHelpers::addressFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::CodeBlock)));
  570. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::CellTag), AssemblyHelpers::tagFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::ScopeChain)));
  571. if (!inlineCallFrame->isClosureCall())
  572. m_jit.storePtr(AssemblyHelpers::TrustedImmPtr(inlineCallFrame->callee->scope()), AssemblyHelpers::payloadFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::ScopeChain)));
  573. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::CellTag), AssemblyHelpers::tagFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::CallerFrame)));
  574. m_jit.storePtr(callerFrameGPR, AssemblyHelpers::payloadFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::CallerFrame)));
  575. m_jit.storePtr(AssemblyHelpers::TrustedImmPtr(jumpTarget), AssemblyHelpers::payloadFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::ReturnPC)));
  576. m_jit.store32(AssemblyHelpers::TrustedImm32(inlineCallFrame->arguments.size()), AssemblyHelpers::payloadFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::ArgumentCount)));
  577. m_jit.store32(AssemblyHelpers::TrustedImm32(JSValue::CellTag), AssemblyHelpers::tagFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::Callee)));
  578. if (!inlineCallFrame->isClosureCall())
  579. m_jit.storePtr(AssemblyHelpers::TrustedImmPtr(inlineCallFrame->callee.get()), AssemblyHelpers::payloadFor((VirtualRegister)(inlineCallFrame->stackOffset + JSStack::Callee)));
  580. }
  581. // 14) Create arguments if necessary and place them into the appropriate aliased
  582. // registers.
  583. if (haveArguments) {
  584. HashSet<InlineCallFrame*, DefaultHash<InlineCallFrame*>::Hash,
  585. NullableHashTraits<InlineCallFrame*> > didCreateArgumentsObject;
  586. for (size_t index = 0; index < operands.size(); ++index) {
  587. const ValueRecovery& recovery = operands[index];
  588. if (recovery.technique() != ArgumentsThatWereNotCreated)
  589. continue;
  590. int operand = operands.operandForIndex(index);
  591. // Find the right inline call frame.
  592. InlineCallFrame* inlineCallFrame = 0;
  593. for (InlineCallFrame* current = exit.m_codeOrigin.inlineCallFrame;
  594. current;
  595. current = current->caller.inlineCallFrame) {
  596. if (current->stackOffset <= operand) {
  597. inlineCallFrame = current;
  598. break;
  599. }
  600. }
  601. if (!m_jit.baselineCodeBlockFor(inlineCallFrame)->usesArguments())
  602. continue;
  603. int argumentsRegister = m_jit.argumentsRegisterFor(inlineCallFrame);
  604. if (didCreateArgumentsObject.add(inlineCallFrame).isNewEntry) {
  605. // We know this call frame optimized out an arguments object that
  606. // the baseline JIT would have created. Do that creation now.
  607. if (inlineCallFrame) {
  608. m_jit.setupArgumentsWithExecState(
  609. AssemblyHelpers::TrustedImmPtr(inlineCallFrame));
  610. m_jit.move(
  611. AssemblyHelpers::TrustedImmPtr(
  612. bitwise_cast<void*>(operationCreateInlinedArguments)),
  613. GPRInfo::nonArgGPR0);
  614. } else {
  615. m_jit.setupArgumentsExecState();
  616. m_jit.move(
  617. AssemblyHelpers::TrustedImmPtr(
  618. bitwise_cast<void*>(operationCreateArguments)),
  619. GPRInfo::nonArgGPR0);
  620. }
  621. m_jit.call(GPRInfo::nonArgGPR0);
  622. m_jit.store32(
  623. AssemblyHelpers::TrustedImm32(JSValue::CellTag),
  624. AssemblyHelpers::tagFor(argumentsRegister));
  625. m_jit.store32(
  626. GPRInfo::returnValueGPR,
  627. AssemblyHelpers::payloadFor(argumentsRegister));
  628. m_jit.store32(
  629. AssemblyHelpers::TrustedImm32(JSValue::CellTag),
  630. AssemblyHelpers::tagFor(unmodifiedArgumentsRegister(argumentsRegister)));
  631. m_jit.store32(
  632. GPRInfo::returnValueGPR,
  633. AssemblyHelpers::payloadFor(unmodifiedArgumentsRegister(argumentsRegister)));
  634. m_jit.move(GPRInfo::returnValueGPR, GPRInfo::regT0); // no-op move on almost all platforms.
  635. }
  636. m_jit.load32(AssemblyHelpers::payloadFor(argumentsRegister), GPRInfo::regT0);
  637. m_jit.store32(
  638. AssemblyHelpers::TrustedImm32(JSValue::CellTag),
  639. AssemblyHelpers::tagFor(operand));
  640. m_jit.store32(GPRInfo::regT0, AssemblyHelpers::payloadFor(operand));
  641. }
  642. }
  643. // 15) Load the result of the last bytecode operation into regT0.
  644. if (exit.m_lastSetOperand != std::numeric_limits<int>::max()) {
  645. m_jit.load32(AssemblyHelpers::payloadFor((VirtualRegister)exit.m_lastSetOperand), GPRInfo::cachedResultRegister);
  646. m_jit.load32(AssemblyHelpers::tagFor((VirtualRegister)exit.m_lastSetOperand), GPRInfo::cachedResultRegister2);
  647. }
  648. // 16) Adjust the call frame pointer.
  649. if (exit.m_codeOrigin.inlineCallFrame)
  650. m_jit.addPtr(AssemblyHelpers::TrustedImm32(exit.m_codeOrigin.inlineCallFrame->stackOffset * sizeof(EncodedJSValue)), GPRInfo::callFrameRegister);
  651. // 17) Jump into the corresponding baseline JIT code.
  652. CodeBlock* baselineCodeBlock = m_jit.baselineCodeBlockFor(exit.m_codeOrigin);
  653. Vector<BytecodeAndMachineOffset>& decodedCodeMap = m_jit.decodedCodeMapFor(baselineCodeBlock);
  654. BytecodeAndMachineOffset* mapping = binarySearch<BytecodeAndMachineOffset, unsigned>(decodedCodeMap, decodedCodeMap.size(), exit.m_codeOrigin.bytecodeIndex, BytecodeAndMachineOffset::getBytecodeIndex);
  655. ASSERT(mapping);
  656. ASSERT(mapping->m_bytecodeIndex == exit.m_codeOrigin.bytecodeIndex);
  657. void* jumpTarget = baselineCodeBlock->getJITCode().executableAddressAtOffset(mapping->m_machineCodeOffset);
  658. ASSERT(GPRInfo::regT2 != GPRInfo::cachedResultRegister && GPRInfo::regT2 != GPRInfo::cachedResultRegister2);
  659. m_jit.move(AssemblyHelpers::TrustedImmPtr(jumpTarget), GPRInfo::regT2);
  660. m_jit.jump(GPRInfo::regT2);
  661. #if DFG_ENABLE(DEBUG_VERBOSE)
  662. dataLogF(" -> %p\n", jumpTarget);
  663. #endif
  664. }
  665. } } // namespace JSC::DFG
  666. #endif // ENABLE(DFG_JIT) && USE(JSVALUE32_64)