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
|
/*
* Copyright (C) 2013 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 "DFGNaturalLoops.h"
#if ENABLE(DFG_JIT)
#include "DFGGraph.h"
#include "JSCInlines.h"
#include <wtf/CommaPrinter.h>
namespace JSC { namespace DFG {
void NaturalLoop::dump(PrintStream& out) const
{
out.print("[Header: ", *header(), ", Body:");
for (unsigned i = 0; i < m_body.size(); ++i)
out.print(" ", *m_body[i]);
out.print("]");
}
NaturalLoops::NaturalLoops() { }
NaturalLoops::~NaturalLoops() { }
void NaturalLoops::computeDependencies(Graph& graph)
{
graph.m_dominators.computeIfNecessary(graph);
}
void NaturalLoops::compute(Graph& graph)
{
// Implement the classic dominator-based natural loop finder. The first
// step is to find all control flow edges A -> B where B dominates A.
// Then B is a loop header and A is a backward branching block. We will
// then accumulate, for each loop header, multiple backward branching
// blocks. Then we backwards graph search from the backward branching
// blocks to their loop headers, which gives us all of the blocks in the
// loop body.
static const bool verbose = false;
if (verbose) {
dataLog("Dominators:\n");
graph.m_dominators.dump(WTF::dataFile());
}
m_loops.resize(0);
for (BlockIndex blockIndex = graph.numBlocks(); blockIndex--;) {
BasicBlock* block = graph.block(blockIndex);
if (!block)
continue;
for (unsigned i = block->numSuccessors(); i--;) {
BasicBlock* successor = block->successor(i);
if (!graph.m_dominators.dominates(successor, block))
continue;
bool found = false;
for (unsigned j = m_loops.size(); j--;) {
if (m_loops[j].header() == successor) {
m_loops[j].addBlock(block);
found = true;
break;
}
}
if (found)
continue;
NaturalLoop loop(successor, m_loops.size());
loop.addBlock(block);
m_loops.append(loop);
}
}
if (verbose)
dataLog("After bootstrap: ", *this, "\n");
FastBitVector seenBlocks;
Vector<BasicBlock*, 4> blockWorklist;
seenBlocks.resize(graph.numBlocks());
for (unsigned i = m_loops.size(); i--;) {
NaturalLoop& loop = m_loops[i];
seenBlocks.clearAll();
ASSERT(blockWorklist.isEmpty());
if (verbose)
dataLog("Dealing with loop ", loop, "\n");
for (unsigned j = loop.size(); j--;) {
seenBlocks.set(loop[j]->index);
blockWorklist.append(loop[j]);
}
while (!blockWorklist.isEmpty()) {
BasicBlock* block = blockWorklist.takeLast();
if (verbose)
dataLog(" Dealing with ", *block, "\n");
if (block == loop.header())
continue;
for (unsigned j = block->predecessors.size(); j--;) {
BasicBlock* predecessor = block->predecessors[j];
if (seenBlocks.get(predecessor->index))
continue;
loop.addBlock(predecessor);
blockWorklist.append(predecessor);
seenBlocks.set(predecessor->index);
}
}
}
// Figure out reverse mapping from blocks to loops.
for (BlockIndex blockIndex = graph.numBlocks(); blockIndex--;) {
BasicBlock* block = graph.block(blockIndex);
if (!block)
continue;
for (unsigned i = BasicBlock::numberOfInnerMostLoopIndices; i--;)
block->innerMostLoopIndices[i] = UINT_MAX;
}
for (unsigned loopIndex = m_loops.size(); loopIndex--;) {
NaturalLoop& loop = m_loops[loopIndex];
for (unsigned blockIndexInLoop = loop.size(); blockIndexInLoop--;) {
BasicBlock* block = loop[blockIndexInLoop];
for (unsigned i = 0; i < BasicBlock::numberOfInnerMostLoopIndices; ++i) {
unsigned thisIndex = block->innerMostLoopIndices[i];
if (thisIndex == UINT_MAX || loop.size() < m_loops[thisIndex].size()) {
insertIntoBoundedVector(
block->innerMostLoopIndices, BasicBlock::numberOfInnerMostLoopIndices,
loopIndex, i);
break;
}
}
}
}
// Now each block knows its inner-most loop and its next-to-inner-most loop. Use
// this to figure out loop parenting.
for (unsigned i = m_loops.size(); i--;) {
NaturalLoop& loop = m_loops[i];
RELEASE_ASSERT(loop.header()->innerMostLoopIndices[0] == i);
loop.m_outerLoopIndex = loop.header()->innerMostLoopIndices[1];
}
if (validationEnabled()) {
// Do some self-verification that we've done some of this correctly.
for (BlockIndex blockIndex = graph.numBlocks(); blockIndex--;) {
BasicBlock* block = graph.block(blockIndex);
if (!block)
continue;
Vector<const NaturalLoop*> simpleLoopsOf;
for (unsigned i = m_loops.size(); i--;) {
if (m_loops[i].contains(block))
simpleLoopsOf.append(&m_loops[i]);
}
Vector<const NaturalLoop*> fancyLoopsOf = loopsOf(block);
std::sort(simpleLoopsOf.begin(), simpleLoopsOf.end());
std::sort(fancyLoopsOf.begin(), fancyLoopsOf.end());
RELEASE_ASSERT(simpleLoopsOf == fancyLoopsOf);
}
}
if (verbose)
dataLog("Results: ", *this, "\n");
}
Vector<const NaturalLoop*> NaturalLoops::loopsOf(BasicBlock* block) const
{
Vector<const NaturalLoop*> result;
for (const NaturalLoop* loop = innerMostLoopOf(block); loop; loop = innerMostOuterLoop(*loop))
result.append(loop);
return result;
}
void NaturalLoops::dump(PrintStream& out) const
{
out.print("NaturalLoops:{");
CommaPrinter comma;
for (unsigned i = 0; i < m_loops.size(); ++i)
out.print(comma, m_loops[i]);
out.print("}");
}
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)
|