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
|
/*
* Copyright (C) 2013-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 "DFGLICMPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGAbstractInterpreterInlines.h"
#include "DFGAtTailAbstractState.h"
#include "DFGClobberSet.h"
#include "DFGClobberize.h"
#include "DFGControlEquivalenceAnalysis.h"
#include "DFGEdgeDominates.h"
#include "DFGGraph.h"
#include "DFGMayExit.h"
#include "DFGNaturalLoops.h"
#include "DFGPhase.h"
#include "DFGSafeToExecute.h"
#include "JSCInlines.h"
namespace JSC { namespace DFG {
class LICMPhase : public Phase {
static constexpr bool verbose = false;
using NaturalLoop = SSANaturalLoop;
struct LoopData {
ClobberSet writes;
BasicBlock* preHeader { nullptr };
};
public:
LICMPhase(Graph& graph)
: Phase(graph, "LICM"_s)
, m_state(graph)
, m_interpreter(graph, m_state)
{
}
bool run()
{
ASSERT(m_graph.m_planStage <= PlanStage::LICMAndLater);
m_graph.m_planStage = PlanStage::LICMAndLater;
DFG_ASSERT(m_graph, nullptr, m_graph.m_form == SSA);
m_graph.ensureSSADominators();
m_graph.ensureSSANaturalLoops();
m_graph.ensureControlEquivalenceAnalysis();
dataLogIf(verbose, "Graph before LICM:\n", m_graph);
m_data.resize(m_graph.m_ssaNaturalLoops->numLoops());
// Figure out the set of things each loop writes to, not including blocks that
// belong to inner loops. We fix this later.
for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
// Skip blocks that are proved to not execute.
// FIXME: This shouldn't be needed.
// https://bugs.webkit.org/show_bug.cgi?id=128584
if (!block->cfaHasVisited)
continue;
const NaturalLoop* loop = m_graph.m_ssaNaturalLoops->innerMostLoopOf(block);
if (!loop)
continue;
LoopData& data = m_data[loop->index()];
for (auto* node : *block) {
// Don't look beyond parts of the code that definitely always exit.
// FIXME: This shouldn't be needed.
// https://bugs.webkit.org/show_bug.cgi?id=128584
if (node->op() == ForceOSRExit)
break;
addWrites(m_graph, node, data.writes);
}
}
// For each loop:
// - Identify its pre-header.
// - Make sure its outer loops know what it clobbers.
for (unsigned loopIndex = m_graph.m_ssaNaturalLoops->numLoops(); loopIndex--;) {
const NaturalLoop& loop = m_graph.m_ssaNaturalLoops->loop(loopIndex);
LoopData& data = m_data[loop.index()];
for (
const NaturalLoop* outerLoop = m_graph.m_ssaNaturalLoops->innerMostOuterLoop(loop);
outerLoop;
outerLoop = m_graph.m_ssaNaturalLoops->innerMostOuterLoop(*outerLoop))
m_data[outerLoop->index()].writes.addAll(data.writes);
BasicBlock* header = loop.header();
BasicBlock* preHeader = nullptr;
unsigned numberOfPreHeaders = 0; // We're cool if this is 1.
// This is guaranteed because we expect the CFG not to have unreachable code. Therefore, a
// loop header must have a predecessor. (Also, we don't allow the root block to be a loop,
// which cuts out the one other way of having a loop header with only one predecessor.)
DFG_ASSERT(m_graph, header->at(0), header->predecessors.size() > 1, header->predecessors.size());
for (unsigned i = header->predecessors.size(); i--;) {
BasicBlock* predecessor = header->predecessors[i];
if (m_graph.m_ssaDominators->dominates(header, predecessor))
continue;
preHeader = predecessor;
++numberOfPreHeaders;
}
// We need to validate the pre-header. There are a bunch of things that could be wrong
// about it:
//
// - There might be more than one. This means that pre-header creation either did not run,
// or some CFG transformation destroyed the pre-headers.
//
// - It may not be legal to exit at the pre-header. That would be a real bummer. Currently,
// LICM assumes that it can always hoist checks. See
// https://bugs.webkit.org/show_bug.cgi?id=148545. Though even with that fixed, we anyway
// would need to check if it's OK to exit at the pre-header since if we can't then we
// would have to restrict hoisting to non-exiting nodes.
if (numberOfPreHeaders != 1)
continue;
// This is guaranteed because the header has multiple predecessors and critical edges are
// broken. Therefore the predecessors must all have one successor, which implies that they
// must end in a Jump.
DFG_ASSERT(m_graph, preHeader->terminal(), preHeader->terminal()->op() == Jump, preHeader->terminal()->op());
if (!preHeader->terminal()->origin.exitOK)
continue;
data.preHeader = preHeader;
}
m_graph.initializeNodeOwners();
// Walk all basic blocks that belong to loops, looking for hoisting opportunities.
// We try to hoist to the outer-most loop that permits it. Hoisting is valid if:
// - The node doesn't write anything.
// - The node doesn't read anything that the loop writes.
// - The preHeader is valid (i.e. it passed the validation above).
// - The preHeader's state at tail makes the node safe to execute.
// - The loop's children all belong to nodes that strictly dominate the loop header.
// - The preHeader's state at tail is still valid. This is mostly to save compile
// time and preserve some kind of sanity, if we hoist something that must exit.
//
// Also, we need to remember to:
// - Update the state-at-tail with the node we hoisted, so future hoist candidates
// know about any type checks we hoisted.
//
// For maximum profit, we walk blocks in DFS order to ensure that we generally
// tend to hoist dominators before dominatees.
Vector<const NaturalLoop*> loopStack;
bool changed = false;
WeakRandom random { Options::seedForLICMFuzzer() };
for (BasicBlock* block : m_graph.blocksInPreOrder()) {
if (!block->cfaHasVisited)
continue;
const NaturalLoop* loop = m_graph.m_ssaNaturalLoops->innerMostLoopOf(block);
if (!loop)
continue;
loopStack.shrink(0);
for (
const NaturalLoop* current = loop;
current;
current = m_graph.m_ssaNaturalLoops->innerMostOuterLoop(*current))
loopStack.append(current);
// Remember: the loop stack has the inner-most loop at index 0, so if we want
// to bias hoisting to outer loops then we need to use a reverse loop.
if (verbose) {
WTF::dataFile().atomically([&](auto&) {
dataLogLn("Attempting to hoist out of block ", *block, " in loops:");
for (unsigned stackIndex = loopStack.size(); stackIndex--;) {
dataLogLn(
" ", *loopStack[stackIndex], ", which writes ",
m_data[loopStack[stackIndex]->index()].writes);
}
});
}
for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
Node*& nodeRef = block->at(nodeIndex);
if (nodeRef->op() == ForceOSRExit)
break;
for (unsigned stackIndex = loopStack.size(); stackIndex--;) {
if (UNLIKELY(Options::useLICMFuzzing())) {
bool shouldAttemptHoist = random.returnTrueWithProbability(Options::allowHoistingLICMProbability());
if (!shouldAttemptHoist && !nodeRef->isCheckNode())
continue;
}
changed |= attemptHoist(block, nodeRef, loopStack[stackIndex]);
}
}
}
return changed;
}
private:
bool attemptHoist(BasicBlock* fromBlock, Node*& nodeRef, const NaturalLoop* loop)
{
Node* node = nodeRef;
LoopData& data = m_data[loop->index()];
if (!data.preHeader) {
dataLogLnIf(verbose, " Not hoisting ", node, " because the pre-header is invalid.");
return false;
}
if (!data.preHeader->cfaDidFinish) {
dataLogLnIf(verbose, " Not hoisting ", node, " because CFA is invalid.");
return false;
}
m_state.initializeTo(data.preHeader);
ASSERT(m_state.isValid());
NodeOrigin originalOrigin = node->origin;
bool canSpeculateBlindly = !m_graph.hasGlobalExitSite(originalOrigin.semantic, HoistingFailed);
// NOTE: We could just use BackwardsDominators here directly, since we already know that the
// preHeader dominates fromBlock. But we wouldn't get anything from being so clever, since
// dominance checks are O(1) and only a few integer compares.
bool isControlEquivalent = m_graph.m_controlEquivalenceAnalysis->dominatesEquivalently(data.preHeader, fromBlock);
bool addsBlindSpeculation = !isControlEquivalent;
NodeOrigin terminalOrigin = data.preHeader->terminal()->origin;
Vector<Node*, 2> hoistedNodes; // This is sorted in the program order they will appear in the basic block we're hoisting to.
auto insertHoistedNode = [&] (Node* node) {
data.preHeader->insertBeforeTerminal(node);
node->owner = data.preHeader;
node->origin = terminalOrigin.withSemantic(node->origin.semantic);
node->origin.wasHoisted |= addsBlindSpeculation;
hoistedNodes.append(node);
};
auto updateAbstractState = [&] {
auto invalidate = [&] (const NaturalLoop* loop) {
LoopData& data = m_data[loop->index()];
data.preHeader->cfaDidFinish = false;
for (unsigned bodyIndex = loop->size(); bodyIndex--;) {
BasicBlock* block = loop->at(bodyIndex);
if (block != data.preHeader)
block->cfaHasVisited = false;
block->cfaDidFinish = false;
}
};
// We can trust what AI proves about edge proof statuses when hoisting to the preheader.
m_state.trustEdgeProofs();
for (unsigned i = 0; i < hoistedNodes.size(); ++i) {
if (!m_interpreter.execute(hoistedNodes[i])) {
invalidate(loop);
return;
}
}
// However, when walking various inner loops below, the proof status of
// an edge may be trivially true, even if it's not true in the preheader
// we hoist to. We don't allow the below node executions to change the
// state of edge proofs. An example of where a proof is trivially true
// is if we have two loops, L1 and L2, where L2 is nested inside L1. The
// header for L1 dominates L2. We hoist a Check from L1's header into L1's
// preheader. However, inside L2's preheader, we can't trust that AI will
// tell us this edge is proven. It's proven in L2's preheader because L2
// is dominated by L1's header. However, the edge is not guaranteed to be
// proven inside L1's preheader.
m_state.dontTrustEdgeProofs();
// Modify the states at the end of the preHeader of the loop we hoisted to,
// and all pre-headers inside the loop. This isn't a stability bottleneck right now
// because most loops are small and most blocks belong to few loops.
for (unsigned bodyIndex = loop->size(); bodyIndex--;) {
BasicBlock* subBlock = loop->at(bodyIndex);
const NaturalLoop* subLoop = m_graph.m_ssaNaturalLoops->headerOf(subBlock);
if (!subLoop)
continue;
BasicBlock* subPreHeader = m_data[subLoop->index()].preHeader;
// We may not have given this loop a pre-header because either it didn't have exitOK
// or the header had multiple predecessors that it did not dominate. In that case the
// loop wouldn't be a hoisting candidate anyway, so we don't have to do anything.
if (!subPreHeader)
continue;
// The pre-header's tail may be unreachable, in which case we have nothing to do.
if (!subPreHeader->cfaDidFinish)
continue;
// We handled this above.
if (subPreHeader == data.preHeader)
continue;
m_state.initializeTo(subPreHeader);
for (unsigned i = 0; i < hoistedNodes.size(); ++i) {
if (!m_interpreter.execute(hoistedNodes[i])) {
invalidate(subLoop);
break;
}
}
}
};
auto tryHoistChecks = [&] {
if (addsBlindSpeculation && !canSpeculateBlindly)
return false;
ASSERT(hoistedNodes.isEmpty());
Vector<Edge, 3> checks;
m_graph.doToChildren(node, [&] (Edge edge) {
if (!m_graph.m_ssaDominators->dominates(edge.node()->owner, data.preHeader))
return;
if (!edge.willHaveCheck())
return;
if ((m_state.forNode(edge).m_type & SpecEmpty) && checkMayCrashIfInputIsEmpty(edge.useKind())) {
if (!canSpeculateBlindly)
return;
Node* checkNotEmpty = m_graph.addNode(CheckNotEmpty, originalOrigin, Edge(edge.node(), UntypedUse));
insertHoistedNode(checkNotEmpty);
}
checks.append(edge);
});
if (checks.isEmpty())
return false;
AdjacencyList children;
NodeType checkOp = Check;
if (checks.size() <= AdjacencyList::Size) {
children = AdjacencyList(AdjacencyList::Fixed);
for (unsigned i = 0; i < checks.size(); ++i)
children.setChild(i, checks[i]);
} else {
checkOp = CheckVarargs;
unsigned firstChild = m_graph.m_varArgChildren.size();
for (Edge edge : checks)
m_graph.m_varArgChildren.append(edge);
children = AdjacencyList(AdjacencyList::Variable, firstChild, checks.size());
}
Node* check = m_graph.addNode(checkOp, originalOrigin, children);
insertHoistedNode(check);
updateAbstractState();
if (verbose)
dataLogLn(" Hoisted some checks from ", node, " and created a new Check ", check, ". Hoisted from ", *fromBlock, " to ", *data.preHeader);
return true;
};
if (!edgesDominate(m_graph, node, data.preHeader)) {
dataLogLnIf(verbose, " Not hoisting ", node, " because it isn't loop invariant.");
return tryHoistChecks();
}
if (doesWrites(m_graph, node)) {
dataLogLnIf(verbose, " Not hoisting ", node, " because it writes things.");
return tryHoistChecks();
}
// It's not safe to consult the AbstractState inside mayExit until we prove all edges
// dominate the pre-header we're hoisting to. We are more conservative above when assigning
// to this variable since we hadn't yet proven all edges dominate the pre-header. Above, we
// just assume mayExit is true. We refine that here since we can now consult the AbstractState.
addsBlindSpeculation = mayExit(m_graph, node, m_state) && !isControlEquivalent;
if (readsOverlap(m_graph, node, data.writes)) {
dataLogLnIf(verbose,
" Not hoisting ", node,
" because it reads things that the loop writes.");
return tryHoistChecks();
}
if (addsBlindSpeculation && !canSpeculateBlindly) {
dataLogLnIf(verbose,
" Not hoisting ", node, " because it may exit and the pre-header (",
*data.preHeader, ") is not control equivalent to the node's original block (",
*fromBlock, ") and hoisting had previously failed.");
return tryHoistChecks();
}
if (!safeToExecute(m_state, m_graph, node)) {
// See if we can rescue the situation by inserting blind speculations.
bool ignoreEmptyChildren = true;
if (canSpeculateBlindly
&& safeToExecute(m_state, m_graph, node, ignoreEmptyChildren)) {
dataLogLnIf(verbose, " Rescuing hoisting by inserting empty checks.");
m_graph.doToChildren(
node,
[&] (Edge& edge) {
if (!(m_state.forNode(edge).m_type & SpecEmpty))
return;
Node* check = m_graph.addNode(CheckNotEmpty, originalOrigin, Edge(edge.node(), UntypedUse));
insertHoistedNode(check);
});
} else {
dataLogLnIf(verbose, " Not hoisting ", node, " because it isn't safe to execute.");
return tryHoistChecks();
}
}
dataLogLnIf(verbose, " Hoisting ", node, " from ", *fromBlock, " to ", *data.preHeader);
insertHoistedNode(node);
updateAbstractState();
if (node->flags() & NodeHasVarArgs)
nodeRef = m_graph.addNode(CheckVarargs, originalOrigin, m_graph.copyVarargChildren(node));
else
nodeRef = m_graph.addNode(Check, originalOrigin, node->children);
return true;
}
AtTailAbstractState m_state;
AbstractInterpreter<AtTailAbstractState> m_interpreter;
Vector<LoopData> m_data;
};
bool performLICM(Graph& graph)
{
return runPhase<LICMPhase>(graph);
}
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)
|