DFGOSREntry.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "DFGOSREntry.h"
  27. #if ENABLE(DFG_JIT)
  28. #include "CallFrame.h"
  29. #include "CodeBlock.h"
  30. #include "DFGNode.h"
  31. #include "JIT.h"
  32. #include "Operations.h"
  33. namespace JSC { namespace DFG {
  34. void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex)
  35. {
  36. #if DFG_ENABLE(OSR_ENTRY)
  37. ASSERT(codeBlock->getJITType() == JITCode::DFGJIT);
  38. ASSERT(codeBlock->alternative());
  39. ASSERT(codeBlock->alternative()->getJITType() == JITCode::BaselineJIT);
  40. ASSERT(!codeBlock->jitCodeMap());
  41. #if ENABLE(JIT_VERBOSE_OSR)
  42. dataLog("OSR in ", *codeBlock->alternative(), " -> ", *codeBlock, " from bc#", bytecodeIndex, "\n");
  43. #endif
  44. VM* vm = &exec->vm();
  45. OSREntryData* entry = codeBlock->dfgOSREntryDataForBytecodeIndex(bytecodeIndex);
  46. if (!entry) {
  47. #if ENABLE(JIT_VERBOSE_OSR)
  48. dataLogF(" OSR failed because the entrypoint was optimized out.\n");
  49. #endif
  50. return 0;
  51. }
  52. ASSERT(entry->m_bytecodeIndex == bytecodeIndex);
  53. // The code below checks if it is safe to perform OSR entry. It may find
  54. // that it is unsafe to do so, for any number of reasons, which are documented
  55. // below. If the code decides not to OSR then it returns 0, and it's the caller's
  56. // responsibility to patch up the state in such a way as to ensure that it's
  57. // both safe and efficient to continue executing baseline code for now. This
  58. // should almost certainly include calling either codeBlock->optimizeAfterWarmUp()
  59. // or codeBlock->dontOptimizeAnytimeSoon().
  60. // 1) Verify predictions. If the predictions are inconsistent with the actual
  61. // values, then OSR entry is not possible at this time. It's tempting to
  62. // assume that we could somehow avoid this case. We can certainly avoid it
  63. // for first-time loop OSR - that is, OSR into a CodeBlock that we have just
  64. // compiled. Then we are almost guaranteed that all of the predictions will
  65. // check out. It would be pretty easy to make that a hard guarantee. But
  66. // then there would still be the case where two call frames with the same
  67. // baseline CodeBlock are on the stack at the same time. The top one
  68. // triggers compilation and OSR. In that case, we may no longer have
  69. // accurate value profiles for the one deeper in the stack. Hence, when we
  70. // pop into the CodeBlock that is deeper on the stack, we might OSR and
  71. // realize that the predictions are wrong. Probably, in most cases, this is
  72. // just an anomaly in the sense that the older CodeBlock simply went off
  73. // into a less-likely path. So, the wisest course of action is to simply not
  74. // OSR at this time.
  75. for (size_t argument = 0; argument < entry->m_expectedValues.numberOfArguments(); ++argument) {
  76. if (argument >= exec->argumentCountIncludingThis()) {
  77. #if ENABLE(JIT_VERBOSE_OSR)
  78. dataLogF(" OSR failed because argument %zu was not passed, expected ", argument);
  79. entry->m_expectedValues.argument(argument).dump(WTF::dataFile());
  80. dataLogF(".\n");
  81. #endif
  82. return 0;
  83. }
  84. JSValue value;
  85. if (!argument)
  86. value = exec->hostThisValue();
  87. else
  88. value = exec->argument(argument - 1);
  89. if (!entry->m_expectedValues.argument(argument).validate(value)) {
  90. #if ENABLE(JIT_VERBOSE_OSR)
  91. dataLog(" OSR failed because argument ", argument, " is ", value, ", expected ", entry->m_expectedValues.argument(argument), ".\n");
  92. #endif
  93. return 0;
  94. }
  95. }
  96. for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
  97. if (entry->m_localsForcedDouble.get(local)) {
  98. if (!exec->registers()[local].jsValue().isNumber()) {
  99. #if ENABLE(JIT_VERBOSE_OSR)
  100. dataLog(" OSR failed because variable ", local, " is ", exec->registers()[local].jsValue(), ", expected number.\n");
  101. #endif
  102. return 0;
  103. }
  104. continue;
  105. }
  106. if (!entry->m_expectedValues.local(local).validate(exec->registers()[local].jsValue())) {
  107. #if ENABLE(JIT_VERBOSE_OSR)
  108. dataLog(" OSR failed because variable ", local, " is ", exec->registers()[local].jsValue(), ", expected ", entry->m_expectedValues.local(local), ".\n");
  109. #endif
  110. return 0;
  111. }
  112. }
  113. // 2) Check the stack height. The DFG JIT may require a taller stack than the
  114. // baseline JIT, in some cases. If we can't grow the stack, then don't do
  115. // OSR right now. That's the only option we have unless we want basic block
  116. // boundaries to start throwing RangeErrors. Although that would be possible,
  117. // it seems silly: you'd be diverting the program to error handling when it
  118. // would have otherwise just kept running albeit less quickly.
  119. if (!vm->interpreter->stack().grow(&exec->registers()[codeBlock->m_numCalleeRegisters])) {
  120. #if ENABLE(JIT_VERBOSE_OSR)
  121. dataLogF(" OSR failed because stack growth failed.\n");
  122. #endif
  123. return 0;
  124. }
  125. #if ENABLE(JIT_VERBOSE_OSR)
  126. dataLogF(" OSR should succeed.\n");
  127. #endif
  128. // 3) Perform data format conversions.
  129. for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
  130. if (entry->m_localsForcedDouble.get(local))
  131. *bitwise_cast<double*>(exec->registers() + local) = exec->registers()[local].jsValue().asNumber();
  132. }
  133. // 4) Fix the call frame.
  134. exec->setCodeBlock(codeBlock);
  135. // 5) Find and return the destination machine code address.
  136. void* result = codeBlock->getJITCode().executableAddressAtOffset(entry->m_machineCodeOffset);
  137. #if ENABLE(JIT_VERBOSE_OSR)
  138. dataLogF(" OSR returning machine code address %p.\n", result);
  139. #endif
  140. return result;
  141. #else // DFG_ENABLE(OSR_ENTRY)
  142. UNUSED_PARAM(exec);
  143. UNUSED_PARAM(codeBlock);
  144. UNUSED_PARAM(bytecodeIndex);
  145. return 0;
  146. #endif
  147. }
  148. } } // namespace JSC::DFG
  149. #endif // ENABLE(DFG_JIT)