fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 6921b29 | 2013-09-18 17:14:02 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011, 2013 Apple Inc. All rights reserved. |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 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 "DFGOSREntry.h" |
| 28 | |
| 29 | #if ENABLE(DFG_JIT) |
| 30 | |
| 31 | #include "CallFrame.h" |
| 32 | #include "CodeBlock.h" |
oliver@apple.com | 02b179b | 2013-07-25 03:58:20 +0000 | [diff] [blame] | 33 | #include "DFGJITCode.h" |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 34 | #include "DFGNode.h" |
| 35 | #include "JIT.h" |
fpizlo@apple.com | a4b4cbe | 2013-01-12 04:47:03 +0000 | [diff] [blame] | 36 | #include "Operations.h" |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 37 | |
| 38 | namespace JSC { namespace DFG { |
| 39 | |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 40 | void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex) |
| 41 | { |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 42 | ASSERT(JITCode::isOptimizingJIT(codeBlock->jitType())); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 43 | ASSERT(codeBlock->alternative()); |
oliver@apple.com | 5a24fdd | 2013-07-25 04:00:54 +0000 | [diff] [blame] | 44 | ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT); |
fpizlo@apple.com | 706f5f3 | 2011-09-21 23:36:35 +0000 | [diff] [blame] | 45 | ASSERT(!codeBlock->jitCodeMap()); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 46 | |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 47 | if (Options::verboseOSR()) { |
| 48 | dataLog( |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 49 | "DFG OSR in ", *codeBlock->alternative(), " -> ", *codeBlock, |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 50 | " from bc#", bytecodeIndex, "\n"); |
| 51 | } |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 52 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 53 | VM* vm = &exec->vm(); |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 54 | if (codeBlock->jitType() != JITCode::DFGJIT) { |
| 55 | RELEASE_ASSERT(codeBlock->jitType() == JITCode::FTLJIT); |
| 56 | |
| 57 | // When will this happen? We could have: |
| 58 | // |
| 59 | // - An exit from the FTL JIT into the baseline JIT followed by an attempt |
| 60 | // to reenter. We're fine with allowing this to fail. If it happens |
| 61 | // enough we'll just reoptimize. It basically means that the OSR exit cost |
| 62 | // us dearly and so reoptimizing is the right thing to do. |
| 63 | // |
| 64 | // - We have recursive code with hot loops. Consider that foo has a hot loop |
| 65 | // that calls itself. We have two foo's on the stack, lets call them foo1 |
| 66 | // and foo2, with foo1 having called foo2 from foo's hot loop. foo2 gets |
| 67 | // optimized all the way into the FTL. Then it returns into foo1, and then |
| 68 | // foo1 wants to get optimized. It might reach this conclusion from its |
| 69 | // hot loop and attempt to OSR enter. And we'll tell it that it can't. It |
| 70 | // might be worth addressing this case, but I just think this case will |
| 71 | // be super rare. For now, if it does happen, it'll cause some compilation |
| 72 | // thrashing. |
| 73 | |
| 74 | if (Options::verboseOSR()) |
| 75 | dataLog(" OSR failed because the target code block is not DFG.\n"); |
| 76 | return 0; |
| 77 | } |
| 78 | |
oliver@apple.com | 5a24fdd | 2013-07-25 04:00:54 +0000 | [diff] [blame] | 79 | OSREntryData* entry = codeBlock->jitCode()->dfg()->osrEntryDataForBytecodeIndex(bytecodeIndex); |
fpizlo@apple.com | 706f5f3 | 2011-09-21 23:36:35 +0000 | [diff] [blame] | 80 | |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 81 | if (!entry) { |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 82 | if (Options::verboseOSR()) |
| 83 | dataLogF(" OSR failed because the entrypoint was optimized out.\n"); |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
fpizlo@apple.com | 706f5f3 | 2011-09-21 23:36:35 +0000 | [diff] [blame] | 87 | ASSERT(entry->m_bytecodeIndex == bytecodeIndex); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 88 | |
| 89 | // The code below checks if it is safe to perform OSR entry. It may find |
| 90 | // that it is unsafe to do so, for any number of reasons, which are documented |
| 91 | // below. If the code decides not to OSR then it returns 0, and it's the caller's |
| 92 | // responsibility to patch up the state in such a way as to ensure that it's |
| 93 | // both safe and efficient to continue executing baseline code for now. This |
| 94 | // should almost certainly include calling either codeBlock->optimizeAfterWarmUp() |
| 95 | // or codeBlock->dontOptimizeAnytimeSoon(). |
| 96 | |
fpizlo@apple.com | 903c378 | 2011-09-15 23:24:27 +0000 | [diff] [blame] | 97 | // 1) Verify predictions. If the predictions are inconsistent with the actual |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 98 | // values, then OSR entry is not possible at this time. It's tempting to |
| 99 | // assume that we could somehow avoid this case. We can certainly avoid it |
| 100 | // for first-time loop OSR - that is, OSR into a CodeBlock that we have just |
| 101 | // compiled. Then we are almost guaranteed that all of the predictions will |
| 102 | // check out. It would be pretty easy to make that a hard guarantee. But |
| 103 | // then there would still be the case where two call frames with the same |
| 104 | // baseline CodeBlock are on the stack at the same time. The top one |
| 105 | // triggers compilation and OSR. In that case, we may no longer have |
| 106 | // accurate value profiles for the one deeper in the stack. Hence, when we |
| 107 | // pop into the CodeBlock that is deeper on the stack, we might OSR and |
| 108 | // realize that the predictions are wrong. Probably, in most cases, this is |
| 109 | // just an anomaly in the sense that the older CodeBlock simply went off |
| 110 | // into a less-likely path. So, the wisest course of action is to simply not |
| 111 | // OSR at this time. |
| 112 | |
fpizlo@apple.com | 4ffd395 | 2011-10-12 02:05:53 +0000 | [diff] [blame] | 113 | for (size_t argument = 0; argument < entry->m_expectedValues.numberOfArguments(); ++argument) { |
| 114 | if (argument >= exec->argumentCountIncludingThis()) { |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 115 | if (Options::verboseOSR()) { |
| 116 | dataLogF(" OSR failed because argument %zu was not passed, expected ", argument); |
| 117 | entry->m_expectedValues.argument(argument).dump(WTF::dataFile()); |
| 118 | dataLogF(".\n"); |
| 119 | } |
fpizlo@apple.com | dc4cae7 | 2011-09-29 23:17:19 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
fpizlo@apple.com | 4ffd395 | 2011-10-12 02:05:53 +0000 | [diff] [blame] | 123 | JSValue value; |
| 124 | if (!argument) |
| 125 | value = exec->hostThisValue(); |
| 126 | else |
| 127 | value = exec->argument(argument - 1); |
| 128 | |
fpizlo@apple.com | eb3323d | 2012-08-20 06:11:24 +0000 | [diff] [blame] | 129 | if (!entry->m_expectedValues.argument(argument).validate(value)) { |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 130 | if (Options::verboseOSR()) { |
| 131 | dataLog( |
| 132 | " OSR failed because argument ", argument, " is ", value, |
| 133 | ", expected ", entry->m_expectedValues.argument(argument), ".\n"); |
| 134 | } |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | } |
| 138 | |
fpizlo@apple.com | 4ffd395 | 2011-10-12 02:05:53 +0000 | [diff] [blame] | 139 | for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) { |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 140 | int localOffset = virtualRegisterForLocal(local).offset(); |
fpizlo@apple.com | 10f22fc | 2011-12-14 07:16:36 +0000 | [diff] [blame] | 141 | if (entry->m_localsForcedDouble.get(local)) { |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 142 | if (!exec->registers()[localOffset].jsValue().isNumber()) { |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 143 | if (Options::verboseOSR()) { |
| 144 | dataLog( |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 145 | " OSR failed because variable ", localOffset, " is ", |
| 146 | exec->registers()[localOffset].jsValue(), ", expected number.\n"); |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 147 | } |
fpizlo@apple.com | 10f22fc | 2011-12-14 07:16:36 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | continue; |
| 151 | } |
fpizlo@apple.com | 6921b29 | 2013-09-18 17:14:02 +0000 | [diff] [blame] | 152 | if (entry->m_localsForcedMachineInt.get(local)) { |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 153 | if (!exec->registers()[localOffset].jsValue().isMachineInt()) { |
fpizlo@apple.com | 6921b29 | 2013-09-18 17:14:02 +0000 | [diff] [blame] | 154 | if (Options::verboseOSR()) { |
| 155 | dataLog( |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 156 | " OSR failed because variable ", localOffset, " is ", |
| 157 | exec->registers()[localOffset].jsValue(), ", expected ", |
fpizlo@apple.com | 6921b29 | 2013-09-18 17:14:02 +0000 | [diff] [blame] | 158 | "machine int.\n"); |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | continue; |
| 163 | } |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 164 | if (!entry->m_expectedValues.local(local).validate(exec->registers()[localOffset].jsValue())) { |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 165 | if (Options::verboseOSR()) { |
| 166 | dataLog( |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 167 | " OSR failed because variable ", localOffset, " is ", |
| 168 | exec->registers()[localOffset].jsValue(), ", expected ", |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 169 | entry->m_expectedValues.local(local), ".\n"); |
| 170 | } |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | } |
fpizlo@apple.com | 10f22fc | 2011-12-14 07:16:36 +0000 | [diff] [blame] | 174 | |
fpizlo@apple.com | 903c378 | 2011-09-15 23:24:27 +0000 | [diff] [blame] | 175 | // 2) Check the stack height. The DFG JIT may require a taller stack than the |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 176 | // baseline JIT, in some cases. If we can't grow the stack, then don't do |
| 177 | // OSR right now. That's the only option we have unless we want basic block |
| 178 | // boundaries to start throwing RangeErrors. Although that would be possible, |
| 179 | // it seems silly: you'd be diverting the program to error handling when it |
| 180 | // would have otherwise just kept running albeit less quickly. |
| 181 | |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 182 | if (!vm->interpreter->stack().grow(&exec->registers()[virtualRegisterForLocal(codeBlock->m_numCalleeRegisters).offset()])) { |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 183 | if (Options::verboseOSR()) |
| 184 | dataLogF(" OSR failed because stack growth failed.\n"); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 188 | if (Options::verboseOSR()) |
| 189 | dataLogF(" OSR should succeed.\n"); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 190 | |
fpizlo@apple.com | 10f22fc | 2011-12-14 07:16:36 +0000 | [diff] [blame] | 191 | // 3) Perform data format conversions. |
| 192 | for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) { |
| 193 | if (entry->m_localsForcedDouble.get(local)) |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 194 | *bitwise_cast<double*>(exec->registers() + virtualRegisterForLocal(local).offset()) = exec->registers()[virtualRegisterForLocal(local).offset()].jsValue().asNumber(); |
fpizlo@apple.com | 6921b29 | 2013-09-18 17:14:02 +0000 | [diff] [blame] | 195 | if (entry->m_localsForcedMachineInt.get(local)) |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 196 | *bitwise_cast<int64_t*>(exec->registers() + virtualRegisterForLocal(local).offset()) = exec->registers()[virtualRegisterForLocal(local).offset()].jsValue().asMachineInt() << JSValue::int52ShiftAmount; |
fpizlo@apple.com | 10f22fc | 2011-12-14 07:16:36 +0000 | [diff] [blame] | 197 | } |
fpizlo@apple.com | 10f22fc | 2011-12-14 07:16:36 +0000 | [diff] [blame] | 198 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 199 | // 4) Reshuffle those registers that need reshuffling. |
| 200 | |
| 201 | Vector<EncodedJSValue> temporaryLocals(entry->m_reshufflings.size()); |
| 202 | EncodedJSValue* registers = bitwise_cast<EncodedJSValue*>(exec->registers()); |
| 203 | for (unsigned i = entry->m_reshufflings.size(); i--;) |
| 204 | temporaryLocals[i] = registers[entry->m_reshufflings[i].fromOffset]; |
| 205 | for (unsigned i = entry->m_reshufflings.size(); i--;) |
| 206 | registers[entry->m_reshufflings[i].toOffset] = temporaryLocals[i]; |
| 207 | |
| 208 | // 5) Fix the call frame. |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 209 | |
| 210 | exec->setCodeBlock(codeBlock); |
| 211 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 212 | // 6) Find and return the destination machine code address. |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 213 | |
oliver@apple.com | 5a24fdd | 2013-07-25 04:00:54 +0000 | [diff] [blame] | 214 | void* result = codeBlock->jitCode()->executableAddressAtOffset(entry->m_machineCodeOffset); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 215 | |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 216 | if (Options::verboseOSR()) |
| 217 | dataLogF(" OSR returning machine code address %p.\n", result); |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 218 | |
| 219 | return result; |
fpizlo@apple.com | a71ab05 | 2011-09-13 05:35:53 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | } } // namespace JSC::DFG |
| 223 | |
| 224 | #endif // ENABLE(DFG_JIT) |