#include "config.h"
#include "DFGDCEPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGBasicBlockInlines.h"
#include "DFGGraph.h"
#include "DFGInsertionSet.h"
#include "DFGPhase.h"
#include "Operations.h"
namespace JSC { namespace DFG {
class DCEPhase : public Phase {
public:
DCEPhase(Graph& graph)
: Phase(graph, "dead code elimination")
{
}
bool run()
{
for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex) {
BasicBlock* block = m_graph.m_blocks[blockIndex].get();
if (!block)
continue;
for (unsigned indexInBlock = block->size(); indexInBlock--;)
block->at(indexInBlock)->setRefCount(0);
for (unsigned phiIndex = block->phis.size(); phiIndex--;)
block->phis[phiIndex]->setRefCount(0);
}
for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex) {
BasicBlock* block = m_graph.m_blocks[blockIndex].get();
if (!block)
continue;
for (unsigned indexInBlock = block->size(); indexInBlock--;) {
Node* node = block->at(indexInBlock);
DFG_NODE_DO_TO_CHILDREN(m_graph, node, findTypeCheckRoot);
if (!(node->flags() & NodeMustGenerate))
continue;
if (!node->postfixRef())
m_worklist.append(node);
}
}
while (!m_worklist.isEmpty()) {
Node* node = m_worklist.last();
m_worklist.removeLast();
ASSERT(node->shouldGenerate()); DFG_NODE_DO_TO_CHILDREN(m_graph, node, countEdge);
}
for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex) {
BasicBlock* block = m_graph.m_blocks[blockIndex].get();
if (!block)
continue;
InsertionSet insertionSet(m_graph);
for (unsigned indexInBlock = block->size(); indexInBlock--;) {
Node* node = block->at(indexInBlock);
if (node->shouldGenerate())
continue;
switch (node->op()) {
case SetLocal: {
if (node->child1().isProved() || node->child1().useKind() == UntypedUse) {
if (!node->child1()->shouldGenerate()
&& node->child1()->op() == UInt32ToNumber)
node->child1() = node->child1()->child1();
if (!node->child1()->shouldGenerate()) {
node->setOpAndDefaultFlags(ZombieHint);
node->child1() = Edge();
break;
}
node->setOpAndDefaultFlags(MovHint);
break;
}
node->setOpAndDefaultFlags(MovHintAndCheck);
node->setRefCount(1);
break;
}
case GetLocal:
case SetArgument: {
break;
}
default: {
if (node->flags() & NodeHasVarArgs) {
for (unsigned childIdx = node->firstChild(); childIdx < node->firstChild() + node->numChildren(); childIdx++) {
Edge edge = m_graph.m_varArgChildren[childIdx];
if (!edge || edge.isProved() || edge.useKind() == UntypedUse)
continue;
insertionSet.insertNode(indexInBlock, SpecNone, Phantom, node->codeOrigin, edge);
}
node->convertToPhantomUnchecked();
node->children.reset();
node->setRefCount(1);
break;
}
node->convertToPhantom();
eliminateIrrelevantPhantomChildren(node);
node->setRefCount(1);
break;
} }
}
insertionSet.execute(block);
}
m_graph.m_refCountState = ExactRefCount;
return true;
}
private:
void findTypeCheckRoot(Node*, Edge edge)
{
if (edge.isProved() || edge.useKind() == UntypedUse)
return;
if (!edge->postfixRef())
m_worklist.append(edge.node());
}
void countEdge(Node*, Edge edge)
{
if (!(edge.isProved() || edge.useKind() == UntypedUse))
return;
if (edge->postfixRef())
return;
m_worklist.append(edge.node());
}
void eliminateIrrelevantPhantomChildren(Node* node)
{
for (unsigned i = 0; i < AdjacencyList::Size; ++i) {
Edge edge = node->children.child(i);
if (!edge)
continue;
if (edge.isProved() || edge.useKind() == UntypedUse)
node->children.removeEdge(i--);
}
}
Vector<Node*, 128> m_worklist;
};
bool performDCE(Graph& graph)
{
SamplingRegion samplingRegion("DFG DCE Phase");
return runPhase<DCEPhase>(graph);
}
} }
#endif // ENABLE(DFG_JIT)