blob: a82ac35eaaaa3b8bf1e16822a814a4400860ab2b [file] [log] [blame]
fpizlo@apple.coma71ab052011-09-13 05:35:53 +00001/*
fpizlo@apple.com6921b292013-09-18 17:14:02 +00002 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
fpizlo@apple.coma71ab052011-09-13 05:35:53 +00003 *
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.com02b179b2013-07-25 03:58:20 +000033#include "DFGJITCode.h"
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000034#include "DFGNode.h"
35#include "JIT.h"
fpizlo@apple.coma4b4cbe2013-01-12 04:47:03 +000036#include "Operations.h"
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000037
38namespace JSC { namespace DFG {
39
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000040void* prepareOSREntry(ExecState* exec, CodeBlock* codeBlock, unsigned bytecodeIndex)
41{
fpizlo@apple.com532f1e52013-09-04 06:26:04 +000042 ASSERT(JITCode::isOptimizingJIT(codeBlock->jitType()));
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000043 ASSERT(codeBlock->alternative());
oliver@apple.com5a24fdd2013-07-25 04:00:54 +000044 ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT);
fpizlo@apple.com706f5f32011-09-21 23:36:35 +000045 ASSERT(!codeBlock->jitCodeMap());
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000046
oliver@apple.com284cc3d2013-07-25 04:00:33 +000047 if (Options::verboseOSR()) {
48 dataLog(
fpizlo@apple.com532f1e52013-09-04 06:26:04 +000049 "DFG OSR in ", *codeBlock->alternative(), " -> ", *codeBlock,
oliver@apple.com284cc3d2013-07-25 04:00:33 +000050 " from bc#", bytecodeIndex, "\n");
51 }
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000052
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000053 VM* vm = &exec->vm();
fpizlo@apple.com532f1e52013-09-04 06:26:04 +000054 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.com5a24fdd2013-07-25 04:00:54 +000079 OSREntryData* entry = codeBlock->jitCode()->dfg()->osrEntryDataForBytecodeIndex(bytecodeIndex);
fpizlo@apple.com706f5f32011-09-21 23:36:35 +000080
fpizlo@apple.com79c51ee2012-05-18 22:30:24 +000081 if (!entry) {
oliver@apple.com284cc3d2013-07-25 04:00:33 +000082 if (Options::verboseOSR())
83 dataLogF(" OSR failed because the entrypoint was optimized out.\n");
fpizlo@apple.com79c51ee2012-05-18 22:30:24 +000084 return 0;
85 }
86
fpizlo@apple.com706f5f32011-09-21 23:36:35 +000087 ASSERT(entry->m_bytecodeIndex == bytecodeIndex);
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000088
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.com903c3782011-09-15 23:24:27 +000097 // 1) Verify predictions. If the predictions are inconsistent with the actual
fpizlo@apple.coma71ab052011-09-13 05:35:53 +000098 // 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.com4ffd3952011-10-12 02:05:53 +0000113 for (size_t argument = 0; argument < entry->m_expectedValues.numberOfArguments(); ++argument) {
114 if (argument >= exec->argumentCountIncludingThis()) {
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000115 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.comdc4cae72011-09-29 23:17:19 +0000120 return 0;
121 }
122
fpizlo@apple.com4ffd3952011-10-12 02:05:53 +0000123 JSValue value;
124 if (!argument)
125 value = exec->hostThisValue();
126 else
127 value = exec->argument(argument - 1);
128
fpizlo@apple.comeb3323d2012-08-20 06:11:24 +0000129 if (!entry->m_expectedValues.argument(argument).validate(value)) {
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000130 if (Options::verboseOSR()) {
131 dataLog(
132 " OSR failed because argument ", argument, " is ", value,
133 ", expected ", entry->m_expectedValues.argument(argument), ".\n");
134 }
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000135 return 0;
136 }
137 }
138
fpizlo@apple.com4ffd3952011-10-12 02:05:53 +0000139 for (size_t local = 0; local < entry->m_expectedValues.numberOfLocals(); ++local) {
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000140 int localOffset = virtualRegisterForLocal(local).offset();
fpizlo@apple.com10f22fc2011-12-14 07:16:36 +0000141 if (entry->m_localsForcedDouble.get(local)) {
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000142 if (!exec->registers()[localOffset].jsValue().isNumber()) {
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000143 if (Options::verboseOSR()) {
144 dataLog(
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000145 " OSR failed because variable ", localOffset, " is ",
146 exec->registers()[localOffset].jsValue(), ", expected number.\n");
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000147 }
fpizlo@apple.com10f22fc2011-12-14 07:16:36 +0000148 return 0;
149 }
150 continue;
151 }
fpizlo@apple.com6921b292013-09-18 17:14:02 +0000152 if (entry->m_localsForcedMachineInt.get(local)) {
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000153 if (!exec->registers()[localOffset].jsValue().isMachineInt()) {
fpizlo@apple.com6921b292013-09-18 17:14:02 +0000154 if (Options::verboseOSR()) {
155 dataLog(
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000156 " OSR failed because variable ", localOffset, " is ",
157 exec->registers()[localOffset].jsValue(), ", expected ",
fpizlo@apple.com6921b292013-09-18 17:14:02 +0000158 "machine int.\n");
159 }
160 return 0;
161 }
162 continue;
163 }
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000164 if (!entry->m_expectedValues.local(local).validate(exec->registers()[localOffset].jsValue())) {
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000165 if (Options::verboseOSR()) {
166 dataLog(
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000167 " OSR failed because variable ", localOffset, " is ",
168 exec->registers()[localOffset].jsValue(), ", expected ",
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000169 entry->m_expectedValues.local(local), ".\n");
170 }
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000171 return 0;
172 }
173 }
fpizlo@apple.com10f22fc2011-12-14 07:16:36 +0000174
fpizlo@apple.com903c3782011-09-15 23:24:27 +0000175 // 2) Check the stack height. The DFG JIT may require a taller stack than the
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000176 // 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.com62aa8b72013-09-26 22:53:54 +0000182 if (!vm->interpreter->stack().grow(&exec->registers()[virtualRegisterForLocal(codeBlock->m_numCalleeRegisters).offset()])) {
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000183 if (Options::verboseOSR())
184 dataLogF(" OSR failed because stack growth failed.\n");
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000185 return 0;
186 }
187
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000188 if (Options::verboseOSR())
189 dataLogF(" OSR should succeed.\n");
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000190
fpizlo@apple.com10f22fc2011-12-14 07:16:36 +0000191 // 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.com62aa8b72013-09-26 22:53:54 +0000194 *bitwise_cast<double*>(exec->registers() + virtualRegisterForLocal(local).offset()) = exec->registers()[virtualRegisterForLocal(local).offset()].jsValue().asNumber();
fpizlo@apple.com6921b292013-09-18 17:14:02 +0000195 if (entry->m_localsForcedMachineInt.get(local))
msaboff@apple.com62aa8b72013-09-26 22:53:54 +0000196 *bitwise_cast<int64_t*>(exec->registers() + virtualRegisterForLocal(local).offset()) = exec->registers()[virtualRegisterForLocal(local).offset()].jsValue().asMachineInt() << JSValue::int52ShiftAmount;
fpizlo@apple.com10f22fc2011-12-14 07:16:36 +0000197 }
fpizlo@apple.com10f22fc2011-12-14 07:16:36 +0000198
fpizlo@apple.coma62d4822013-10-06 04:22:43 +0000199 // 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.coma71ab052011-09-13 05:35:53 +0000209
210 exec->setCodeBlock(codeBlock);
211
fpizlo@apple.coma62d4822013-10-06 04:22:43 +0000212 // 6) Find and return the destination machine code address.
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000213
oliver@apple.com5a24fdd2013-07-25 04:00:54 +0000214 void* result = codeBlock->jitCode()->executableAddressAtOffset(entry->m_machineCodeOffset);
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000215
oliver@apple.com284cc3d2013-07-25 04:00:33 +0000216 if (Options::verboseOSR())
217 dataLogF(" OSR returning machine code address %p.\n", result);
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000218
219 return result;
fpizlo@apple.coma71ab052011-09-13 05:35:53 +0000220}
221
222} } // namespace JSC::DFG
223
224#endif // ENABLE(DFG_JIT)