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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/*
* Copyright (C) 2014-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 "DFGPutStackSinkingPhase.h"
#if ENABLE(DFG_JIT)
#include "ButterflyInlines.h"
#include "DFGBlockMapInlines.h"
#include "DFGGraph.h"
#include "DFGInsertionSet.h"
#include "DFGPhase.h"
#include "DFGPreciseLocalClobberize.h"
#include "DFGSSACalculator.h"
#include "OperandsInlines.h"
namespace JSC { namespace DFG {
namespace {
class PutStackSinkingPhase : public Phase {
static constexpr bool verbose = false;
public:
PutStackSinkingPhase(Graph& graph)
: Phase(graph, "PutStack sinking"_s)
{
}
bool run()
{
// FIXME: One of the problems of this approach is that it will create a duplicate Phi graph
// for sunken PutStacks in the presence of interesting control flow merges, and where the
// value being PutStack'd is also otherwise live in the DFG code. We could work around this
// by doing the sinking over CPS, or maybe just by doing really smart hoisting. It's also
// possible that the duplicate Phi graph can be deduplicated by B3. It would be best if we
// could observe that there is already a Phi graph in place that does what we want. In
// principle if we have a request to place a Phi at a particular place, we could just check
// if there is already a Phi that does what we want. Because PutStackSinkingPhase runs just
// after SSA conversion, we have almost a guarantee that the Phi graph we produce here would
// be trivially redundant to the one we already have.
// FIXME: This phase doesn't adequately use KillStacks. KillStack can be viewed as a def.
// This is mostly inconsequential; it would be a bug to have a local live at a KillStack.
// More important is that KillStack should swallow any deferral. After a KillStack, the
// local should behave like a TOP deferral because it would be invalid for anyone to trust
// the stack. It's not clear to me if this is important or not.
// https://bugs.webkit.org/show_bug.cgi?id=145296
dataLogIf(verbose, "Graph before PutStack sinking:\n", m_graph);
m_graph.ensureSSADominators();
SSACalculator ssaCalculator(m_graph);
InsertionSet insertionSet(m_graph);
// First figure out where various locals are live.
BlockMap<Operands<bool>> liveAtHead(m_graph);
BlockMap<Operands<bool>> liveAtTail(m_graph);
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
liveAtHead[block] = Operands<bool>(OperandsLike, block->variablesAtHead, false);
liveAtTail[block] = Operands<bool>(OperandsLike, block->variablesAtHead, false);
}
bool changed;
do {
changed = false;
for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
dataLogLnIf(verbose, "Starting liveness for block: ", *block);
Operands<bool> live = liveAtTail[block];
for (unsigned nodeIndex = block->size(); nodeIndex--;) {
Node* node = block->at(nodeIndex);
dataLogLnIf(verbose, "Live at ", node, ": ", live);
Vector<Operand, 4> reads;
Vector<Operand, 4> writes;
auto escapeHandler = [&] (Operand operand) {
if (operand.isHeader())
return;
dataLogLnIf(verbose, " ", operand, " is live at ", node);
reads.append(operand);
};
auto writeHandler = [&] (Operand operand) {
if (operand.isHeader())
return;
auto op = node->op();
RELEASE_ASSERT(op == PutStack || op == LoadVarargs || op == ForwardVarargs || op == KillStack);
writes.append(operand);
};
preciseLocalClobberize(
m_graph, node, escapeHandler, writeHandler,
[&] (Operand, LazyNode) { });
for (Operand operand : writes)
live.operand(operand) = false;
for (Operand operand : reads)
live.operand(operand) = true;
}
if (live == liveAtHead[block])
continue;
liveAtHead[block] = live;
changed = true;
for (BasicBlock* predecessor : block->predecessors) {
for (size_t i = live.size(); i--;)
liveAtTail[predecessor][i] |= live[i];
}
}
} while (changed);
// All of the arguments should be live at head of root. Note that we may find that some
// locals are live at head of root. This seems wrong but isn't. This will happen for example
// if the function accesses closure variable #42 for some other function and we either don't
// have variable #42 at all or we haven't set it at root, for whatever reason. Basically this
// arises since our aliasing for closure variables is conservatively based on variable number
// and ignores the owning symbol table. We should probably fix this eventually and make our
// aliasing more precise.
//
// For our purposes here, the imprecision in the aliasing is harmless. It just means that we
// may not do as much Phi pruning as we wanted.
for (size_t i = liveAtHead.atIndex(0).numberOfArguments(); i--;)
DFG_ASSERT(m_graph, nullptr, liveAtHead.atIndex(0).argument(i));
// Next identify where we would want to sink PutStacks to. We say that there is a deferred
// flush if we had a PutStack with a given FlushFormat but it hasn't been materialized yet.
// Deferrals have the following lattice; but it's worth noting that the TOP part of the
// lattice serves an entirely different purpose than the rest of the lattice: it just means
// that we're in a region of code where nobody should have been relying on the value. The
// rest of the lattice means that we either have a PutStack that is deferred (i.e. still
// needs to be executed) or there isn't one (because we've alraedy executed it).
//
// Bottom:
// Represented as DeadFlush.
// Means that all previous PutStacks have been executed so there is nothing deferred.
// During merging this is subordinate to the other kinds of deferrals, because it
// represents the fact that we've already executed all necessary PutStacks. This implies
// that there *had* been some PutStacks that we should have executed.
//
// Top:
// Represented as ConflictingFlush.
// Represents the fact that we know, via forward flow, that there isn't any value in the
// given local that anyone should have been relying on. This comes into play at the
// prologue (because in SSA form at the prologue no local has any value) or when we merge
// deferrals for different formats's. A lexical scope in which a local had some semantic
// meaning will by this point share the same format; if we had stores from different
// lexical scopes that got merged together then we may have a conflicting format. Hence
// a conflicting format proves that we're no longer in an area in which the variable was
// in scope. Note that this is all approximate and only precise enough to later answer
// questions pertinent to sinking. For example, this doesn't always detect when a local
// is no longer semantically relevant - we may well have a deferral from inside some
// inlined call survive outside of that inlined code, and this is generally OK. In the
// worst case it means that we might think that a deferral that is actually dead must
// still be executed. But we usually catch that with liveness. Liveness usually catches
// such cases, but that's not guaranteed since liveness is conservative.
//
// What Top does give us is detects situations where we both don't need to care about a
// deferral and there is no way that we could reason about it anyway. If we merged
// deferrals for different formats then we wouldn't know the format to use. So, we use
// Top in that case because that's also a case where we know that we can ignore the
// deferral.
//
// Deferral with a concrete format:
// Represented by format values other than DeadFlush or ConflictingFlush.
// Represents the fact that the original code would have done a PutStack but we haven't
// identified an operation that would have observed that PutStack.
//
// We need to be precise about liveness in this phase because not doing so
// could cause us to insert a PutStack before a node we thought may escape a
// value that it doesn't really escape. Sinking this PutStack above such a node may
// cause us to insert a GetStack that we forward to the Phi we're feeding into the
// sunken PutStack. Inserting such a GetStack could cause us to load garbage and
// can confuse the AI to claim untrue things (like that the program will exit when
// it really won't).
BlockMap<Operands<FlushFormat>> deferredAtHead(m_graph);
BlockMap<Operands<FlushFormat>> deferredAtTail(m_graph);
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
deferredAtHead[block] =
Operands<FlushFormat>(OperandsLike, block->variablesAtHead);
deferredAtTail[block] =
Operands<FlushFormat>(OperandsLike, block->variablesAtHead);
}
for (unsigned local = deferredAtHead.atIndex(0).numberOfLocals(); local--;)
deferredAtHead.atIndex(0).local(local) = ConflictingFlush;
do {
changed = false;
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
Operands<FlushFormat> deferred = deferredAtHead[block];
dataLogLnIf(verbose, "Looking for deferred PutStacks in block: ", *block);
for (Node* node : *block) {
dataLogLnIf(verbose, "Deferred at ", node, ":", deferred);
if (node->op() == GetStack) {
// Handle the case that the input doesn't match our requirements. This is
// really a bug, but it's a benign one if we simply don't run this phase.
// It usually arises because of patterns like:
//
// if (thing)
// PutStack()
// ...
// if (thing)
// GetStack()
//
// Or:
//
// if (never happens)
// GetStack()
//
// Because this phase runs early in SSA, it should be sensible to enforce
// that no such code pattern has arisen yet. So, when validation is
// enabled, we assert that we aren't seeing this. But with validation
// disabled we silently let this fly and we just abort this phase.
// FIXME: Get rid of all remaining cases of conflicting GetStacks.
// https://bugs.webkit.org/show_bug.cgi?id=150398
bool isConflicting =
deferred.operand(node->stackAccessData()->operand) == ConflictingFlush;
if (validationEnabled())
DFG_ASSERT(m_graph, node, !isConflicting);
if (isConflicting) {
// Oh noes! Abort!!
return false;
}
// A GetStack doesn't affect anything, since we know which local we are reading
// from.
continue;
} else if (node->op() == PutStack) {
Operand operand = node->stackAccessData()->operand;
dataLogLnIf(verbose, "Setting flush format for ", node, " at operand ", operand);
deferred.operand(operand) = node->stackAccessData()->format;
continue;
} else if (node->op() == KillStack) {
// We don't want to sink a PutStack past a KillStack.
dataLogLnIf(verbose, "Killing stack for ", node->unlinkedOperand());
deferred.operand(node->unlinkedOperand()) = ConflictingFlush;
continue;
}
auto escapeHandler = [&] (Operand operand) {
dataLogLnIf(verbose, "For ", node, " escaping ", operand);
if (operand.isHeader())
return;
// We will materialize just before any reads.
deferred.operand(operand) = DeadFlush;
};
auto writeHandler = [&] (Operand operand) {
ASSERT(!operand.isTmp());
if (operand.isHeader())
return;
RELEASE_ASSERT(node->op() == VarargsLength || node->op() == LoadVarargs || node->op() == ForwardVarargs);
dataLogLnIf(verbose, "Writing dead flush for ", node, " at operand ", operand);
deferred.operand(operand) = DeadFlush;
};
preciseLocalClobberize(
m_graph, node, escapeHandler, writeHandler,
[&] (Operand, LazyNode) { });
}
if (deferred == deferredAtTail[block])
continue;
deferredAtTail[block] = deferred;
changed = true;
for (BasicBlock* successor : block->successors()) {
for (size_t i = deferred.size(); i--;) {
dataLogIf(verbose, "Considering ", deferred.operandForIndex(i), " at ", pointerDump(block), "->", pointerDump(successor), ": ", deferred[i], " and ", deferredAtHead[successor][i], " merges to ");
deferredAtHead[successor][i] = merge(deferredAtHead[successor][i], deferred[i]);
dataLogLnIf(verbose, deferredAtHead[successor][i]);
}
}
}
} while (changed);
// We wish to insert PutStacks at all of the materialization points, which are defined
// implicitly as the places where we set deferred to Dead while it was previously not Dead.
// To do this, we may need to build some Phi functions to handle stuff like this:
//
// Before:
//
// if (p)
// PutStack(r42, @x)
// else
// PutStack(r42, @y)
//
// After:
//
// if (p)
// Upsilon(@x, ^z)
// else
// Upsilon(@y, ^z)
// z: Phi()
// PutStack(r42, @z)
//
// This means that we have an SSACalculator::Variable for each local, and a Def is any
// PutStack in the original program. The original PutStacks will simply vanish.
Operands<SSACalculator::Variable*> operandToVariable(
OperandsLike, m_graph.block(0)->variablesAtHead);
Vector<Operand> indexToOperand;
for (size_t i = m_graph.block(0)->variablesAtHead.size(); i--;) {
Operand operand = m_graph.block(0)->variablesAtHead.operandForIndex(i);
SSACalculator::Variable* variable = ssaCalculator.newVariable();
operandToVariable.operand(operand) = variable;
ASSERT(indexToOperand.size() == variable->index());
indexToOperand.append(operand);
}
UncheckedKeyHashSet<Node*> putStacksToSink;
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
for (Node* node : *block) {
switch (node->op()) {
case PutStack:
putStacksToSink.add(node);
ssaCalculator.newDef(
operandToVariable.operand(node->stackAccessData()->operand),
block, node->child1().node());
break;
case GetStack:
ssaCalculator.newDef(
operandToVariable.operand(node->stackAccessData()->operand),
block, node);
break;
default:
break;
}
}
}
ssaCalculator.computePhis(
[&] (SSACalculator::Variable* variable, BasicBlock* block) -> Node* {
Operand operand = indexToOperand[variable->index()];
if (!liveAtHead[block].operand(operand))
return nullptr;
FlushFormat format = deferredAtHead[block].operand(operand);
// We could have an invalid deferral because liveness is imprecise.
if (!isConcrete(format))
return nullptr;
dataLogLnIf(verbose, "Adding Phi for ", operand, " at ", pointerDump(block));
Node* phiNode = m_graph.addNode(SpecHeapTop, Phi, block->at(0)->origin.withInvalidExit());
phiNode->mergeFlags(resultFor(format));
return phiNode;
});
Operands<Node*> mapping(OperandsLike, m_graph.block(0)->variablesAtHead);
Operands<FlushFormat> deferred;
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
mapping.fill(nullptr);
for (size_t i = mapping.size(); i--;) {
Operand operand(mapping.operandForIndex(i));
SSACalculator::Variable* variable = operandToVariable.operand(operand);
SSACalculator::Def* def = ssaCalculator.reachingDefAtHead(block, variable);
if (!def)
continue;
mapping.operand(operand) = def->value();
}
dataLogLnIf(verbose, "Mapping at top of ", pointerDump(block), ": ", mapping);
for (SSACalculator::Def* phiDef : ssaCalculator.phisForBlock(block)) {
Operand operand = indexToOperand[phiDef->variable()->index()];
insertionSet.insert(0, phiDef->value());
dataLogLnIf(verbose, " Mapping ", operand, " to ", phiDef->value());
mapping.operand(operand) = phiDef->value();
}
deferred = deferredAtHead[block];
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
Node* node = block->at(nodeIndex);
dataLogLnIf(verbose, "Deferred at ", node, ":", deferred);
switch (node->op()) {
case PutStack: {
StackAccessData* data = node->stackAccessData();
Operand operand = data->operand;
deferred.operand(operand) = data->format;
dataLogLnIf(verbose, " Mapping ", operand, " to ", node->child1().node(), " at ", node);
mapping.operand(operand) = node->child1().node();
break;
}
case GetStack: {
StackAccessData* data = node->stackAccessData();
FlushFormat format = deferred.operand(data->operand);
if (!isConcrete(format)) {
DFG_ASSERT(
m_graph, node,
deferred.operand(data->operand) != ConflictingFlush, deferred.operand(data->operand));
// This means there is no deferral. No deferral means that the most
// authoritative value for this stack slot is what is stored in the stack. So,
// keep the GetStack.
mapping.operand(data->operand) = node;
break;
}
// We have a concrete deferral, which means a PutStack that hasn't executed yet. It
// would have stored a value with a certain format. That format must match our
// format. But more importantly, we can simply use the value that the PutStack would
// have stored and get rid of the GetStack.
DFG_ASSERT(m_graph, node, format == data->format, format, data->format);
Node* incoming = mapping.operand(data->operand);
node->child1() = incoming->defaultEdge();
node->convertToIdentity();
break;
}
case KillStack: {
deferred.operand(node->unlinkedOperand()) = ConflictingFlush;
break;
}
default: {
auto escapeHandler = [&] (Operand operand) {
dataLogLnIf(verbose, "For ", node, " escaping ", operand);
if (operand.isHeader())
return;
FlushFormat format = deferred.operand(operand);
if (!isConcrete(format)) {
// It's dead now, rather than conflicting.
deferred.operand(operand) = DeadFlush;
return;
}
// Gotta insert a PutStack.
dataLogLnIf(verbose, "Inserting a PutStack for ", operand, " at ", node);
Node* incoming = mapping.operand(operand);
DFG_ASSERT(m_graph, node, incoming);
// If we are sinking a PutStack to before an ExitOK, it
// should be ExitInvalid like the other preceding nodes.
auto origin = node->op() == ExitOK ? node->origin.withInvalidExit() : node->origin;
insertionSet.insertNode(
nodeIndex, SpecNone, PutStack, origin,
OpInfo(m_graph.m_stackAccessData.add(operand, format)),
Edge(incoming, uncheckedUseKindFor(format)));
deferred.operand(operand) = DeadFlush;
};
auto writeHandler = [&] (Operand operand) {
if (operand.isHeader())
return;
// LoadVarargs and ForwardVarargs are unconditional writes to the stack
// locations they claim to write to. They do not read from the stack
// locations they write to. This makes those stack locations dead right
// before a LoadVarargs/ForwardVarargs. This means we should never sink
// PutStacks right to this point.
RELEASE_ASSERT(node->op() == VarargsLength || node->op() == LoadVarargs || node->op() == ForwardVarargs);
deferred.operand(operand) = DeadFlush;
};
preciseLocalClobberize(
m_graph, node, escapeHandler, writeHandler,
[&] (Operand, LazyNode) { });
break;
} }
}
NodeAndIndex terminal = block->findTerminal();
size_t upsilonInsertionPoint = terminal.index;
NodeOrigin upsilonOrigin = terminal.node->origin;
for (BasicBlock* successorBlock : block->successors()) {
for (SSACalculator::Def* phiDef : ssaCalculator.phisForBlock(successorBlock)) {
Node* phiNode = phiDef->value();
SSACalculator::Variable* variable = phiDef->variable();
Operand operand = indexToOperand[variable->index()];
dataLogLnIf(verbose, "Creating Upsilon for ", operand, " at ", pointerDump(block), "->", pointerDump(successorBlock));
FlushFormat format = deferredAtHead[successorBlock].operand(operand);
DFG_ASSERT(m_graph, nullptr, isConcrete(format), format);
UseKind useKind = uncheckedUseKindFor(format);
// We need to get a value for the stack slot. This phase doesn't really have a
// good way of determining if a stack location got clobbered. It just knows if
// there is a deferral. The lack of a deferral might mean that a PutStack or
// GetStack had never happened, or it might mean that the value was read, or
// that it was written. It's OK for us to make some bad decisions here, since
// GCSE will clean it up anyway.
Node* incoming;
if (isConcrete(deferred.operand(operand))) {
incoming = mapping.operand(operand);
DFG_ASSERT(m_graph, phiNode, incoming);
} else {
// Issue a GetStack to get the value. This might introduce some redundancy
// into the code, but if it's bad enough, GCSE will clean it up.
incoming = insertionSet.insertNode(
upsilonInsertionPoint, SpecNone, GetStack, upsilonOrigin,
OpInfo(m_graph.m_stackAccessData.add(operand, format)));
incoming->setResult(resultFor(format));
}
insertionSet.insertNode(
upsilonInsertionPoint, SpecNone, Upsilon, upsilonOrigin,
OpInfo(phiNode), Edge(incoming, useKind));
}
}
insertionSet.execute(block);
}
// Finally eliminate the sunken PutStacks by turning them into Checks. This keeps whatever
// type check they were doing.
for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
for (auto* node : *block) {
if (!putStacksToSink.contains(node))
continue;
node->remove(m_graph);
}
}
dataLogIf(verbose, "Graph after PutStack sinking:\n", m_graph);
return true;
}
};
} // anonymous namespace
bool performPutStackSinking(Graph& graph)
{
return runPhase<PutStackSinkingPhase>(graph);
}
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)
|