DFGRedundantPhiEliminationPhase.cpp [plain text]
#include "config.h"
#include "DFGRedundantPhiEliminationPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGGraph.h"
namespace JSC { namespace DFG {
class RedundantPhiEliminationPhase : public Phase {
public:
RedundantPhiEliminationPhase(Graph& graph)
: Phase(graph, "redundant phi elimination")
{
}
void run()
{
bool changed = false;
do {
changed = fixupPhis();
} while (changed);
updateBlockVariableInformation();
for (NodeIndex index = 0; index < m_graph.size(); ++index) {
Node& node = m_graph[index];
if (!node.shouldGenerate())
continue;
switch (node.op()) {
case GetLocal:
replacePhiChild(node, 0);
break;
default:
break;
}
}
}
private:
NodeIndex getRedundantReplacement(NodeIndex phi)
{
NodeIndex child1 = m_graph[phi].child1().indexUnchecked();
NodeIndex candidate = child1 == phi ? NoNode : child1;
NodeIndex child2 = m_graph[phi].child2().indexUnchecked();
if (candidate != NoNode) {
if (child2 != NoNode && child2 != candidate && child2 != phi)
return NoNode;
} else if (child2 != phi)
candidate = child2;
NodeIndex child3 = m_graph[phi].child3().indexUnchecked();
if (candidate != NoNode) {
if (child3 != NoNode && child3 != candidate && child3 != phi)
return NoNode;
} else if (child3 != phi)
candidate = child3;
return candidate;
}
bool replacePhiChild(Node& node, unsigned childIndex)
{
ASSERT(childIndex < 3);
bool replaced = false;
NodeIndex child = node.children.child(childIndex).indexUnchecked();
if (child != NoNode && m_graph[child].op() == Phi) {
NodeIndex childReplacement = getRedundantReplacement(child);
if (childReplacement != NoNode) {
node.children.child(childIndex).setIndex(childReplacement);
replaced = true;
if (node.refCount()) {
m_graph[childReplacement].ref();
m_graph.deref(child);
}
}
}
return replaced;
}
bool fixupPhis()
{
bool changed = false;
for (BlockIndex block = 0; block < m_graph.m_blocks.size(); ++block) {
Vector<NodeIndex>& phis = m_graph.m_blocks[block]->phis;
for (size_t i = 0; i < phis.size(); ++i) {
NodeIndex phi = phis[i];
Node& phiNode = m_graph[phi];
changed |= (replacePhiChild(phiNode, 0) && phiNode.refCount());
changed |= (replacePhiChild(phiNode, 1) && phiNode.refCount());
changed |= (replacePhiChild(phiNode, 2) && phiNode.refCount());
}
}
return changed;
}
void updateBlockVariableInformation()
{
for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex) {
BasicBlock* basicBlock = m_graph.m_blocks[blockIndex].get();
for (size_t arg = 0; arg < basicBlock->variablesAtHead.numberOfArguments(); ++arg) {
NodeIndex nodeIndex = basicBlock->variablesAtHead.argument(arg);
if (nodeIndex != NoNode && m_graph[nodeIndex].op() == Phi && !m_graph[nodeIndex].refCount()) {
NodeIndex replacement = getRedundantReplacement(nodeIndex);
if (replacement != NoNode) {
ASSERT(basicBlock->variablesAtTail.argument(arg) == nodeIndex);
basicBlock->variablesAtHead.argument(arg) = replacement;
basicBlock->variablesAtTail.argument(arg) = replacement;
}
}
}
for (size_t local = 0; local < basicBlock->variablesAtHead.numberOfLocals(); ++local) {
NodeIndex nodeIndex = basicBlock->variablesAtHead.local(local);
if (nodeIndex != NoNode && m_graph[nodeIndex].op() == Phi && !m_graph[nodeIndex].refCount()) {
NodeIndex replacement = getRedundantReplacement(nodeIndex);
if (replacement != NoNode) {
ASSERT(basicBlock->variablesAtTail.local(local) == nodeIndex);
basicBlock->variablesAtHead.local(local) = replacement;
basicBlock->variablesAtTail.local(local) = replacement;
}
}
}
}
}
};
void performRedundantPhiElimination(Graph& graph)
{
runPhase<RedundantPhiEliminationPhase>(graph);
}
} }
#endif // ENABLE(DFG_JIT)