-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
DFGVarargsForwardingPhase.cpp
458 lines (397 loc) · 16.8 KB
/
DFGVarargsForwardingPhase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Copyright (C) 2015-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DFGVarargsForwardingPhase.h"
#if ENABLE(DFG_JIT)
#include "ButterflyInlines.h"
#include "ClonedArguments.h"
#include "DFGArgumentsUtilities.h"
#include "DFGClobberize.h"
#include "DFGForAllKills.h"
#include "DFGGraph.h"
#include "DFGPhase.h"
#include <wtf/ListDump.h>
namespace JSC { namespace DFG {
namespace {
class VarargsForwardingPhase : public Phase {
static constexpr bool verbose = false;
public:
VarargsForwardingPhase(Graph& graph)
: Phase(graph, "varargs forwarding")
{
}
bool run()
{
DFG_ASSERT(m_graph, nullptr, m_graph.m_form != SSA);
if (verbose) {
dataLog("Graph before varargs forwarding:\n");
m_graph.dump();
}
m_changed = false;
for (BasicBlock* block : m_graph.blocksInNaturalOrder())
handleBlock(block);
return m_changed;
}
private:
void handleBlock(BasicBlock* block)
{
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
Node* node = block->at(nodeIndex);
switch (node->op()) {
case CreateDirectArguments:
case CreateClonedArguments:
handleCandidate(block, nodeIndex);
break;
default:
break;
}
}
}
void handleCandidate(BasicBlock* block, unsigned candidateNodeIndex)
{
// We expect calls into this function to be rare. So, this is written in a simple O(n) manner.
Node* candidate = block->at(candidateNodeIndex);
if (verbose)
dataLog("Handling candidate ", candidate, "\n");
// We eliminate GetButterfly over CreateClonedArguments if the butterfly is only
// used by a GetByOffset that loads the CreateClonedArguments's length. We also
// eliminate it if the GetButterfly node is totally unused.
Vector<Node*, 1> candidateButterflies;
// Find the index of the last node in this block to use the candidate, and look for escaping
// sites.
unsigned lastUserIndex = candidateNodeIndex;
Vector<Operand, 2> relevantLocals; // This is a set. We expect it to be a small set.
for (unsigned nodeIndex = candidateNodeIndex + 1; nodeIndex < block->size(); ++nodeIndex) {
Node* node = block->at(nodeIndex);
auto defaultEscape = [&] {
if (m_graph.uses(node, candidate)) {
if (verbose)
dataLog(" Escape at ", node, "\n");
return true;
}
return false;
};
bool validGetByOffset = false;
switch (node->op()) {
case MovHint:
if (node->child1() != candidate)
break;
lastUserIndex = nodeIndex;
if (!relevantLocals.contains(node->unlinkedOperand()))
relevantLocals.append(node->unlinkedOperand());
break;
case CheckVarargs:
case Check: {
bool sawEscape = false;
m_graph.doToChildren(
node,
[&] (Edge edge) {
if (edge == candidate) {
lastUserIndex = nodeIndex;
if (edge.willNotHaveCheck())
return;
if (alreadyChecked(edge.useKind(), SpecObject))
return;
sawEscape = true;
}
});
if (sawEscape) {
if (verbose)
dataLog(" Escape at ", node, "\n");
return;
}
break;
}
case VarargsLength:
case LoadVarargs:
if (m_graph.uses(node, candidate))
lastUserIndex = nodeIndex;
break;
case CallVarargs:
case ConstructVarargs:
case TailCallVarargs:
case TailCallVarargsInlinedCaller:
if (node->child1() == candidate || node->child2() == candidate) {
if (verbose)
dataLog(" Escape at ", node, "\n");
return;
}
if (node->child2() == candidate)
lastUserIndex = nodeIndex;
break;
case SetLocal:
if (node->child1() == candidate && node->variableAccessData()->isLoadedFrom()) {
if (verbose)
dataLog(" Escape at ", node, "\n");
return;
}
break;
case GetArrayLength: {
if (node->arrayMode().type() == Array::DirectArguments && node->child1() == candidate && node->child1()->op() == CreateDirectArguments) {
lastUserIndex = nodeIndex;
break;
}
if (defaultEscape())
return;
break;
}
case GetButterfly: {
if (node->child1() == candidate && candidate->op() == CreateClonedArguments) {
lastUserIndex = nodeIndex;
candidateButterflies.append(node);
break;
}
if (defaultEscape())
return;
break;
}
case FilterGetByStatus:
case FilterPutByIdStatus:
case FilterCallLinkStatus:
case FilterInByIdStatus:
case FilterDeleteByStatus:
case FilterCheckPrivateBrandStatus:
case FilterSetPrivateBrandStatus:
break;
case GetByOffset: {
if (node->child1()->op() == GetButterfly
&& candidateButterflies.contains(node->child1().node())
&& node->child2() == candidate
&& node->storageAccessData().offset == clonedArgumentsLengthPropertyOffset) {
ASSERT(node->child1()->child1() == candidate);
ASSERT(isOutOfLineOffset(clonedArgumentsLengthPropertyOffset));
// We're good to go. This is getting the length of the arguments.
lastUserIndex = nodeIndex;
validGetByOffset = true;
break;
}
if (defaultEscape())
return;
break;
}
default:
if (defaultEscape())
return;
break;
}
if (!validGetByOffset) {
for (Node* butterfly : candidateButterflies) {
if (m_graph.uses(node, butterfly)) {
if (verbose)
dataLog(" Butterfly escaped at ", node, "\n");
return;
}
}
}
forAllKilledOperands(
m_graph, node, block->tryAt(nodeIndex + 1),
[&] (Operand operand) {
if (verbose)
dataLog(" Killing ", operand, " while we are interested in ", listDump(relevantLocals), "\n");
for (unsigned i = 0; i < relevantLocals.size(); ++i) {
if (operand == relevantLocals[i]) {
relevantLocals[i--] = relevantLocals.last();
relevantLocals.removeLast();
lastUserIndex = nodeIndex;
ASSERT(!relevantLocals.contains(operand));
break;
}
}
});
}
if (verbose)
dataLog("Selected lastUserIndex = ", lastUserIndex, ", ", block->at(lastUserIndex), "\n");
// We're still in business. Determine if between the candidate and the last user there is any
// effect that could interfere with sinking.
for (unsigned nodeIndex = candidateNodeIndex + 1; nodeIndex <= lastUserIndex; ++nodeIndex) {
Node* node = block->at(nodeIndex);
// We have our own custom switch to detect some interferences that clobberize() wouldn't know
// about, and also some of the common ones, too. In particular, clobberize() doesn't know
// that Flush, MovHint, ZombieHint, and KillStack are bad because it's not worried about
// what gets read on OSR exit.
switch (node->op()) {
case MovHint:
case KillStack:
if (argumentsInvolveStackSlot(candidate, node->unlinkedOperand())) {
if (verbose)
dataLog(" Interference at ", node, "\n");
return;
}
break;
case PutStack:
if (argumentsInvolveStackSlot(candidate, node->stackAccessData()->operand)) {
if (verbose)
dataLog(" Interference at ", node, "\n");
return;
}
break;
case SetLocal:
case Flush:
if (argumentsInvolveStackSlot(candidate, node->operand())) {
if (verbose)
dataLog(" Interference at ", node, "\n");
return;
}
break;
default: {
bool doesInterfere = false;
clobberize(
m_graph, node, NoOpClobberize(),
[&] (AbstractHeap heap) {
if (heap.kind() != Stack) {
ASSERT(!heap.overlaps(Stack));
return;
}
ASSERT(!heap.payload().isTop());
if (argumentsInvolveStackSlot(candidate, heap.operand()))
doesInterfere = true;
},
NoOpClobberize());
if (doesInterfere) {
if (verbose)
dataLog(" Interference at ", node, "\n");
return;
}
} }
}
// We can make this work.
if (verbose)
dataLog(" Will do forwarding!\n");
m_changed = true;
// Transform the program.
switch (candidate->op()) {
case CreateDirectArguments:
candidate->setOpAndDefaultFlags(PhantomDirectArguments);
break;
case CreateClonedArguments:
candidate->setOpAndDefaultFlags(PhantomClonedArguments);
break;
default:
DFG_CRASH(m_graph, candidate, "bad node type");
break;
}
InsertionSet insertionSet(m_graph);
for (unsigned nodeIndex = candidateNodeIndex + 1; nodeIndex <= lastUserIndex; ++nodeIndex) {
Node* node = block->at(nodeIndex);
switch (node->op()) {
case Check:
case CheckVarargs:
case MovHint:
case PutHint:
// We don't need to change anything with these.
break;
case VarargsLength: {
if (node->argumentsChild() != candidate)
break;
node->convertToIdentityOn(emitCodeToGetArgumentsArrayLength(insertionSet, candidate, nodeIndex, node->origin, /* addThis = */ true));
break;
}
case LoadVarargs:
if (node->argumentsChild() != candidate)
break;
node->setOpAndDefaultFlags(ForwardVarargs);
break;
case CallVarargs:
if (node->child3() != candidate)
break;
node->setOpAndDefaultFlags(CallForwardVarargs);
break;
case ConstructVarargs:
if (node->child3() != candidate)
break;
node->setOpAndDefaultFlags(ConstructForwardVarargs);
break;
case TailCallVarargs:
if (node->child3() != candidate)
break;
node->setOpAndDefaultFlags(TailCallForwardVarargs);
break;
case TailCallVarargsInlinedCaller:
if (node->child3() != candidate)
break;
node->setOpAndDefaultFlags(TailCallForwardVarargsInlinedCaller);
break;
case SetLocal:
// This is super odd. We don't have to do anything here, since in DFG IR, the phantom
// arguments nodes do produce a JSValue. Also, we know that if this SetLocal referenecs a
// candidate then the SetLocal - along with all of its references - will die off pretty
// soon, since it has no real users. DCE will surely kill it. If we make it to SSA, then
// SSA conversion will kill it.
break;
case GetButterfly: {
if (node->child1().node() == candidate) {
ASSERT(candidateButterflies.contains(node));
node->child1() = Edge();
node->remove(m_graph);
}
break;
}
case FilterGetByStatus:
case FilterPutByIdStatus:
case FilterCallLinkStatus:
case FilterInByIdStatus:
case FilterDeleteByStatus:
case FilterCheckPrivateBrandStatus:
case FilterSetPrivateBrandStatus:
if (node->child1().node() == candidate)
node->remove(m_graph);
break;
case GetByOffset: {
if (node->child2() == candidate) {
ASSERT(candidateButterflies.contains(node->child1().node())); // It's no longer a GetButterfly node, but it should've been a candidate butterfly.
ASSERT(node->storageAccessData().offset == clonedArgumentsLengthPropertyOffset);
node->convertToIdentityOn(
emitCodeToGetArgumentsArrayLength(insertionSet, candidate, nodeIndex, node->origin));
}
break;
}
case GetArrayLength:
if (node->arrayMode().type() == Array::DirectArguments && node->child1() == candidate) {
node->convertToIdentityOn(
emitCodeToGetArgumentsArrayLength(insertionSet, candidate, nodeIndex, node->origin));
}
break;
default:
if (!ASSERT_ENABLED)
break;
m_graph.doToChildren(
node,
[&] (Edge edge) {
DFG_ASSERT(m_graph, node, edge != candidate);
});
break;
}
}
insertionSet.execute(block);
}
bool m_changed;
};
} // anonymous namespace
bool performVarargsForwarding(Graph& graph)
{
return runPhase<VarargsForwardingPhase>(graph);
}
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)