bool run() { ASSERT(m_graph.m_form == SSA); m_graph.m_dominators.computeIfNecessary(m_graph); m_graph.m_naturalLoops.computeIfNecessary(m_graph); m_data.resize(m_graph.m_naturalLoops.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; const NaturalLoop* loop = m_graph.m_naturalLoops.innerMostLoopOf(block); if (!loop) continue; LoopData& data = m_data[loop->index()]; for (unsigned nodeIndex = block->size(); nodeIndex--;) addWrites(m_graph, block->at(nodeIndex), data.writes); } // For each loop: // - Identify its pre-header. // - Make sure its outer loops know what it clobbers. for (unsigned loopIndex = m_graph.m_naturalLoops.numLoops(); loopIndex--;) { const NaturalLoop& loop = m_graph.m_naturalLoops.loop(loopIndex); LoopData& data = m_data[loop.index()]; for ( const NaturalLoop* outerLoop = m_graph.m_naturalLoops.innerMostOuterLoop(loop); outerLoop; outerLoop = m_graph.m_naturalLoops.innerMostOuterLoop(*outerLoop)) m_data[outerLoop->index()].writes.addAll(data.writes); BasicBlock* header = loop.header(); BasicBlock* preHeader = 0; for (unsigned i = header->predecessors.size(); i--;) { BasicBlock* predecessor = header->predecessors[i]; if (m_graph.m_dominators.dominates(header, predecessor)) continue; RELEASE_ASSERT(!preHeader || preHeader == predecessor); preHeader = predecessor; } RELEASE_ASSERT(preHeader->last()->op() == Jump); 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'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: // - Clear NodeExitsForward for any nodes we hoisted. // - 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<BasicBlock*> depthFirst; m_graph.getBlocksInDepthFirstOrder(depthFirst); Vector<const NaturalLoop*> loopStack; bool changed = false; for ( unsigned depthFirstIndex = 0; depthFirstIndex < depthFirst.size(); ++depthFirstIndex) { BasicBlock* block = depthFirst[depthFirstIndex]; const NaturalLoop* loop = m_graph.m_naturalLoops.innerMostLoopOf(block); if (!loop) continue; loopStack.resize(0); for ( const NaturalLoop* current = loop; current; current = m_graph.m_naturalLoops.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) { dataLog( "Attempting to hoist out of block ", *block, " in loops:\n"); for (unsigned stackIndex = loopStack.size(); stackIndex--;) { dataLog( " ", *loopStack[stackIndex], ", which writes ", m_data[loopStack[stackIndex]->index()].writes, "\n"); } } for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { Node*& nodeRef = block->at(nodeIndex); if (doesWrites(m_graph, nodeRef)) { if (verbose) dataLog(" Not hoisting ", nodeRef, " because it writes things.\n"); continue; } for (unsigned stackIndex = loopStack.size(); stackIndex--;) changed |= attemptHoist(block, nodeRef, loopStack[stackIndex]); } } return changed; }
bool run() { DFG_ASSERT(m_graph, nullptr, m_graph.m_form == SSA); m_graph.ensureDominators(); m_graph.ensureNaturalLoops(); m_graph.ensureControlEquivalenceAnalysis(); if (verbose) { dataLog("Graph before LICM:\n"); m_graph.dump(); } m_data.resize(m_graph.m_naturalLoops->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_naturalLoops->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_naturalLoops->numLoops(); loopIndex--;) { const NaturalLoop& loop = m_graph.m_naturalLoops->loop(loopIndex); LoopData& data = m_data[loop.index()]; for ( const NaturalLoop* outerLoop = m_graph.m_naturalLoops->innerMostOuterLoop(loop); outerLoop; outerLoop = m_graph.m_naturalLoops->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); for (unsigned i = header->predecessors.size(); i--;) { BasicBlock* predecessor = header->predecessors[i]; if (m_graph.m_dominators->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); 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; for (BasicBlock* block : m_graph.blocksInPreOrder()) { const NaturalLoop* loop = m_graph.m_naturalLoops->innerMostLoopOf(block); if (!loop) continue; loopStack.resize(0); for ( const NaturalLoop* current = loop; current; current = m_graph.m_naturalLoops->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) { dataLog( "Attempting to hoist out of block ", *block, " in loops:\n"); for (unsigned stackIndex = loopStack.size(); stackIndex--;) { dataLog( " ", *loopStack[stackIndex], ", which writes ", m_data[loopStack[stackIndex]->index()].writes, "\n"); } } for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { Node*& nodeRef = block->at(nodeIndex); if (doesWrites(m_graph, nodeRef)) { if (verbose) dataLog(" Not hoisting ", nodeRef, " because it writes things.\n"); continue; } for (unsigned stackIndex = loopStack.size(); stackIndex--;) changed |= attemptHoist(block, nodeRef, loopStack[stackIndex]); } } return changed; }
bool run() { DFG_ASSERT(m_graph, nullptr, m_graph.m_form == SSA); m_graph.m_dominators.computeIfNecessary(m_graph); m_graph.m_naturalLoops.computeIfNecessary(m_graph); m_data.resize(m_graph.m_naturalLoops.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_naturalLoops.innerMostLoopOf(block); if (!loop) continue; LoopData& data = m_data[loop->index()]; for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { Node* node = block->at(nodeIndex); // 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_naturalLoops.numLoops(); loopIndex--;) { const NaturalLoop& loop = m_graph.m_naturalLoops.loop(loopIndex); LoopData& data = m_data[loop.index()]; for ( const NaturalLoop* outerLoop = m_graph.m_naturalLoops.innerMostOuterLoop(loop); outerLoop; outerLoop = m_graph.m_naturalLoops.innerMostOuterLoop(*outerLoop)) m_data[outerLoop->index()].writes.addAll(data.writes); BasicBlock* header = loop.header(); BasicBlock* preHeader = 0; for (unsigned i = header->predecessors.size(); i--;) { BasicBlock* predecessor = header->predecessors[i]; if (m_graph.m_dominators.dominates(header, predecessor)) continue; DFG_ASSERT(m_graph, nullptr, !preHeader || preHeader == predecessor); preHeader = predecessor; } DFG_ASSERT(m_graph, preHeader->terminal(), preHeader->terminal()->op() == Jump); // We should validate the pre-header. If we placed forExit origins on nodes only if // at the top of that node it is legal to exit, then we would simply check if Jump // had a forExit. We should disable hoisting to pre-headers that don't validate. // Or, we could only allow hoisting of things that definitely don't exit. // FIXME: https://bugs.webkit.org/show_bug.cgi?id=145204 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'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; for (BasicBlock* block : m_graph.blocksInPreOrder()) { const NaturalLoop* loop = m_graph.m_naturalLoops.innerMostLoopOf(block); if (!loop) continue; loopStack.resize(0); for ( const NaturalLoop* current = loop; current; current = m_graph.m_naturalLoops.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) { dataLog( "Attempting to hoist out of block ", *block, " in loops:\n"); for (unsigned stackIndex = loopStack.size(); stackIndex--;) { dataLog( " ", *loopStack[stackIndex], ", which writes ", m_data[loopStack[stackIndex]->index()].writes, "\n"); } } for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { Node*& nodeRef = block->at(nodeIndex); if (doesWrites(m_graph, nodeRef)) { if (verbose) dataLog(" Not hoisting ", nodeRef, " because it writes things.\n"); continue; } for (unsigned stackIndex = loopStack.size(); stackIndex--;) changed |= attemptHoist(block, nodeRef, loopStack[stackIndex]); } } return changed; }