#include "config.h"
#include "DFGDCEPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGBasicBlockInlines.h"
#include "DFGGraph.h"
#include "DFGInsertionSet.h"
#include "DFGPhase.h"
#include "JSCInlines.h"
namespace JSC { namespace DFG {
class DCEPhase : public Phase {
public:
DCEPhase(Graph& graph)
: Phase(graph, "dead code elimination")
, m_insertionSet(graph)
{
}
bool run()
{
ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
BasicBlock* block = m_graph.block(blockIndex);
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.numBlocks(); ++blockIndex) {
BasicBlock* block = m_graph.block(blockIndex);
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()) {
while (!m_worklist.isEmpty()) {
Node* node = m_worklist.last();
m_worklist.removeLast();
ASSERT(node->shouldGenerate()); DFG_NODE_DO_TO_CHILDREN(m_graph, node, countEdge);
}
if (m_graph.m_form == SSA) {
for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
for (unsigned nodeIndex = block->size(); nodeIndex--;) {
Node* node = block->at(nodeIndex);
if (node->op() != Upsilon)
continue;
if (node->shouldGenerate())
continue;
if (node->phi()->shouldGenerate())
countNode(node);
}
}
}
}
if (m_graph.m_form == SSA) {
Vector<BasicBlock*> depthFirst;
m_graph.getBlocksInDepthFirstOrder(depthFirst);
for (unsigned i = 0; i < depthFirst.size(); ++i)
fixupBlock(depthFirst[i]);
} else {
RELEASE_ASSERT(m_graph.m_form == ThreadedCPS);
for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
fixupBlock(m_graph.block(blockIndex));
cleanVariables(m_graph.m_arguments);
}
m_graph.m_refCountState = ExactRefCount;
return true;
}
private:
void findTypeCheckRoot(Node*, Edge edge)
{
if (edge.willNotHaveCheck())
return;
if (!edge->postfixRef())
m_worklist.append(edge.node());
}
void countNode(Node* node)
{
if (node->postfixRef())
return;
m_worklist.append(node);
}
void countEdge(Node*, Edge edge)
{
if (edge.willHaveCheck())
return;
countNode(edge.node());
}
void fixupBlock(BasicBlock* block)
{
if (!block)
return;
switch (m_graph.m_form) {
case SSA:
break;
case ThreadedCPS: {
for (unsigned phiIndex = 0; phiIndex < block->phis.size(); ++phiIndex) {
if (!block->phis[phiIndex]->shouldGenerate()) {
block->phis[phiIndex--] = block->phis.last();
block->phis.removeLast();
}
}
cleanVariables(block->variablesAtHead);
cleanVariables(block->variablesAtTail);
break;
}
default:
RELEASE_ASSERT_NOT_REACHED();
return;
}
for (unsigned indexInBlock = 0; indexInBlock < block->size(); ++indexInBlock) {
Node* node = block->at(indexInBlock);
if (node->shouldGenerate())
continue;
switch (node->op()) {
case MovHint: {
if (node->child1()->op() == Phantom) {
node->setOpAndDefaultFlags(ZombieHint);
node->child1() = Edge();
break;
}
break;
}
case ZombieHint: {
RELEASE_ASSERT_NOT_REACHED();
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.willNotHaveCheck())
continue;
m_insertionSet.insertNode(indexInBlock, SpecNone, Phantom, node->origin, edge);
}
node->convertToPhantomUnchecked();
node->children.reset();
node->setRefCount(1);
break;
}
node->convertToPhantom();
eliminateIrrelevantPhantomChildren(node);
node->setRefCount(1);
break;
} }
}
m_insertionSet.execute(block);
}
void eliminateIrrelevantPhantomChildren(Node* node)
{
for (unsigned i = 0; i < AdjacencyList::Size; ++i) {
Edge edge = node->children.child(i);
if (!edge)
continue;
if (edge.willNotHaveCheck())
node->children.removeEdge(i--);
}
}
template<typename VariablesVectorType>
void cleanVariables(VariablesVectorType& variables)
{
for (unsigned i = variables.size(); i--;) {
Node* node = variables[i];
if (!node)
continue;
if (node->op() != Phantom && node->shouldGenerate())
continue;
if (node->op() == GetLocal) {
node = node->child1().node();
ASSERT(
node->op() == Phi || node->op() == SetArgument
|| node->variableAccessData()->isCaptured());
if (node->shouldGenerate()) {
variables[i] = node;
continue;
}
}
variables[i] = 0;
}
}
Vector<Node*, 128> m_worklist;
InsertionSet m_insertionSet;
};
bool performDCE(Graph& graph)
{
SamplingRegion samplingRegion("DFG DCE Phase");
return runPhase<DCEPhase>(graph);
}
} }
#endif // ENABLE(DFG_JIT)