StackLayoutPhase should use CodeBlock::usesArguments rather than FunctionExecutable...
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGStackLayoutPhase.cpp
1 /*
2  * Copyright (C) 2013 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
26 #include "config.h"
27 #include "DFGStackLayoutPhase.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGGraph.h"
32 #include "DFGPhase.h"
33 #include "DFGValueSource.h"
34 #include "JSCInlines.h"
35
36 namespace JSC { namespace DFG {
37
38 class StackLayoutPhase : public Phase {
39     static const bool verbose = false;
40     
41 public:
42     StackLayoutPhase(Graph& graph)
43         : Phase(graph, "stack layout")
44     {
45     }
46     
47     bool run()
48     {
49         SymbolTable* symbolTable = codeBlock()->symbolTable();
50
51         // This enumerates the locals that we actually care about and packs them. So for example
52         // if we use local 1, 3, 4, 5, 7, then we remap them: 1->0, 3->1, 4->2, 5->3, 7->4. We
53         // treat a variable as being "used" if there exists an access to it (SetLocal, GetLocal,
54         // Flush, PhantomLocal).
55         
56         BitVector usedLocals;
57         
58         // Collect those variables that are used from IR.
59         bool hasGetLocalUnlinked = false;
60         for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
61             BasicBlock* block = m_graph.block(blockIndex);
62             if (!block)
63                 continue;
64             for (unsigned nodeIndex = block->size(); nodeIndex--;) {
65                 Node* node = block->at(nodeIndex);
66                 switch (node->op()) {
67                 case GetLocal:
68                 case SetLocal:
69                 case PutLocal:
70                 case Flush:
71                 case PhantomLocal: {
72                     VariableAccessData* variable = node->variableAccessData();
73                     if (variable->local().isArgument())
74                         break;
75                     usedLocals.set(variable->local().toLocal());
76                     break;
77                 }
78                     
79                 case GetLocalUnlinked: {
80                     VirtualRegister operand = node->unlinkedLocal();
81                     if (operand.isArgument())
82                         break;
83                     usedLocals.set(operand.toLocal());
84                     hasGetLocalUnlinked = true;
85                     break;
86                 }
87                     
88                 default:
89                     break;
90                 }
91             }
92         }
93         
94         // Ensure that captured variables and captured inline arguments are pinned down.
95         // They should have been because of flushes, except that the flushes can be optimized
96         // away.
97         if (symbolTable) {
98             for (int i = symbolTable->captureStart(); i > symbolTable->captureEnd(); i--)
99                 usedLocals.set(VirtualRegister(i).toLocal());
100         }
101         if (codeBlock()->usesArguments()) {
102             usedLocals.set(codeBlock()->argumentsRegister().toLocal());
103             usedLocals.set(unmodifiedArgumentsRegister(codeBlock()->argumentsRegister()).toLocal());
104         }
105         if (codeBlock()->uncheckedActivationRegister().isValid())
106             usedLocals.set(codeBlock()->activationRegister().toLocal());
107         for (InlineCallFrameSet::iterator iter = m_graph.m_plan.inlineCallFrames->begin(); !!iter; ++iter) {
108             InlineCallFrame* inlineCallFrame = *iter;
109             if (!m_graph.usesArguments(inlineCallFrame))
110                 continue;
111             
112             VirtualRegister argumentsRegister = m_graph.argumentsRegisterFor(inlineCallFrame);
113             usedLocals.set(argumentsRegister.toLocal());
114             usedLocals.set(unmodifiedArgumentsRegister(argumentsRegister).toLocal());
115             
116             for (unsigned argument = inlineCallFrame->arguments.size(); argument-- > 1;) {
117                 usedLocals.set(VirtualRegister(
118                     virtualRegisterForArgument(argument).offset() +
119                     inlineCallFrame->stackOffset).toLocal());
120             }
121         }
122         
123         Vector<unsigned> allocation(usedLocals.size());
124         m_graph.m_nextMachineLocal = 0;
125         for (unsigned i = 0; i < usedLocals.size(); ++i) {
126             if (!usedLocals.get(i)) {
127                 allocation[i] = UINT_MAX;
128                 continue;
129             }
130             
131             allocation[i] = m_graph.m_nextMachineLocal++;
132         }
133         
134         for (unsigned i = m_graph.m_variableAccessData.size(); i--;) {
135             VariableAccessData* variable = &m_graph.m_variableAccessData[i];
136             if (!variable->isRoot())
137                 continue;
138             
139             if (variable->local().isArgument()) {
140                 variable->machineLocal() = variable->local();
141                 continue;
142             }
143             
144             size_t local = variable->local().toLocal();
145             if (local >= allocation.size())
146                 continue;
147             
148             if (allocation[local] == UINT_MAX)
149                 continue;
150             
151             variable->machineLocal() = virtualRegisterForLocal(
152                 allocation[variable->local().toLocal()]);
153         }
154         
155         if (codeBlock()->usesArguments()) {
156             VirtualRegister argumentsRegister = virtualRegisterForLocal(
157                 allocation[codeBlock()->argumentsRegister().toLocal()]);
158             RELEASE_ASSERT(
159                 virtualRegisterForLocal(allocation[
160                     unmodifiedArgumentsRegister(
161                         codeBlock()->argumentsRegister()).toLocal()])
162                 == unmodifiedArgumentsRegister(argumentsRegister));
163             codeBlock()->setArgumentsRegister(argumentsRegister);
164         }
165         
166         if (codeBlock()->uncheckedActivationRegister().isValid()) {
167             codeBlock()->setActivationRegister(
168                 virtualRegisterForLocal(allocation[codeBlock()->activationRegister().toLocal()]));
169         }
170         
171         // This register is never valid for DFG code blocks.
172         codeBlock()->setScopeRegister(VirtualRegister());
173
174         for (unsigned i = m_graph.m_inlineVariableData.size(); i--;) {
175             InlineVariableData data = m_graph.m_inlineVariableData[i];
176             InlineCallFrame* inlineCallFrame = data.inlineCallFrame;
177             
178             if (m_graph.usesArguments(inlineCallFrame)) {
179                 inlineCallFrame->argumentsRegister = virtualRegisterForLocal(
180                     allocation[m_graph.argumentsRegisterFor(inlineCallFrame).toLocal()]);
181
182                 RELEASE_ASSERT(
183                     virtualRegisterForLocal(allocation[unmodifiedArgumentsRegister(
184                         m_graph.argumentsRegisterFor(inlineCallFrame)).toLocal()])
185                     == unmodifiedArgumentsRegister(inlineCallFrame->argumentsRegister));
186             }
187             
188             for (unsigned argument = inlineCallFrame->arguments.size(); argument-- > 1;) {
189                 ArgumentPosition& position = m_graph.m_argumentPositions[
190                     data.argumentPositionStart + argument];
191                 VariableAccessData* variable = position.someVariable();
192                 ValueSource source;
193                 if (!variable)
194                     source = ValueSource(SourceIsDead);
195                 else {
196                     source = ValueSource::forFlushFormat(
197                         variable->machineLocal(), variable->flushFormat());
198                 }
199                 inlineCallFrame->arguments[argument] = source.valueRecovery();
200             }
201             
202             RELEASE_ASSERT(inlineCallFrame->isClosureCall == !!data.calleeVariable);
203             if (inlineCallFrame->isClosureCall) {
204                 VariableAccessData* variable = data.calleeVariable->find();
205                 ValueSource source = ValueSource::forFlushFormat(
206                     variable->machineLocal(),
207                     variable->flushFormat());
208                 inlineCallFrame->calleeRecovery = source.valueRecovery();
209             } else
210                 RELEASE_ASSERT(inlineCallFrame->calleeRecovery.isConstant());
211         }
212         
213         if (symbolTable) {
214             if (symbolTable->captureCount()) {
215                 unsigned captureStartLocal = allocation[
216                     VirtualRegister(codeBlock()->symbolTable()->captureStart()).toLocal()];
217                 ASSERT(captureStartLocal != UINT_MAX);
218                 m_graph.m_machineCaptureStart = virtualRegisterForLocal(captureStartLocal).offset();
219             } else
220                 m_graph.m_machineCaptureStart = virtualRegisterForLocal(0).offset();
221         
222             // This is an abomination. If we had captured an argument then the argument ends
223             // up being "slow", meaning that loads of the argument go through an extra lookup
224             // table.
225             if (const SlowArgument* slowArguments = symbolTable->slowArguments()) {
226                 auto newSlowArguments = std::make_unique<SlowArgument[]>(
227                     symbolTable->parameterCount());
228                 for (size_t i = symbolTable->parameterCount(); i--;) {
229                     newSlowArguments[i] = slowArguments[i];
230                     VirtualRegister reg = VirtualRegister(slowArguments[i].index);
231                     if (reg.isLocal())
232                         newSlowArguments[i].index = virtualRegisterForLocal(allocation[reg.toLocal()]).offset();
233                 }
234             
235                 m_graph.m_slowArguments = WTF::move(newSlowArguments);
236             }
237         }
238         
239         // Fix GetLocalUnlinked's variable references.
240         if (hasGetLocalUnlinked) {
241             for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
242                 BasicBlock* block = m_graph.block(blockIndex);
243                 if (!block)
244                     continue;
245                 for (unsigned nodeIndex = block->size(); nodeIndex--;) {
246                     Node* node = block->at(nodeIndex);
247                     switch (node->op()) {
248                     case GetLocalUnlinked: {
249                         VirtualRegister operand = node->unlinkedLocal();
250                         if (operand.isLocal())
251                             operand = virtualRegisterForLocal(allocation[operand.toLocal()]);
252                         node->setUnlinkedMachineLocal(operand);
253                         break;
254                     }
255                         
256                     default:
257                         break;
258                     }
259                 }
260             }
261         }
262         
263         return true;
264     }
265 };
266
267 bool performStackLayout(Graph& graph)
268 {
269     SamplingRegion samplingRegion("DFG Stack Layout Phase");
270     return runPhase<StackLayoutPhase>(graph);
271 }
272
273 } } // namespace JSC::DFG
274
275 #endif // ENABLE(DFG_JIT)
276