B3EliminateDeadCode.cpp [plain text]
#include "config.h"
#include "B3EliminateDeadCode.h"
#if ENABLE(B3_JIT)
#include "B3PhaseScope.h"
#include "B3ProcedureInlines.h"
#include "B3ValueInlines.h"
#include "B3Variable.h"
#include "B3VariableValue.h"
#include <wtf/GraphNodeWorklist.h>
#include <wtf/IndexSet.h>
#include <wtf/Vector.h>
namespace JSC { namespace B3 {
bool eliminateDeadCodeImpl(Procedure& proc)
{
bool changed = false;
GraphNodeWorklist<Value*, IndexSet<Value*>> worklist;
Vector<UpsilonValue*, 64> upsilons;
for (BasicBlock* block : proc) {
for (Value* value : *block) {
Effects effects;
if (value->opcode() != Phi && value->opcode() != Upsilon)
effects = value->effects();
if (effects.mustExecute())
worklist.push(value);
if (UpsilonValue* upsilon = value->as<UpsilonValue>())
upsilons.append(upsilon);
}
}
for (;;) {
while (Value* value = worklist.pop()) {
for (Value* child : value->children())
worklist.push(child);
}
bool didPush = false;
for (size_t upsilonIndex = 0; upsilonIndex < upsilons.size(); ++upsilonIndex) {
UpsilonValue* upsilon = upsilons[upsilonIndex];
if (worklist.saw(upsilon->phi())) {
worklist.push(upsilon);
upsilons[upsilonIndex--] = upsilons.last();
upsilons.takeLast();
didPush = true;
}
}
if (!didPush)
break;
}
IndexSet<Variable*> liveVariables;
for (BasicBlock* block : proc) {
size_t sourceIndex = 0;
size_t targetIndex = 0;
while (sourceIndex < block->size()) {
Value* value = block->at(sourceIndex++);
if (worklist.saw(value)) {
if (VariableValue* variableValue = value->as<VariableValue>())
liveVariables.add(variableValue->variable());
block->at(targetIndex++) = value;
} else {
proc.deleteValue(value);
changed = true;
}
}
block->values().resize(targetIndex);
}
for (Variable* variable : proc.variables()) {
if (!liveVariables.contains(variable))
proc.deleteVariable(variable);
}
return changed;
}
bool eliminateDeadCode(Procedure& proc)
{
PhaseScope phaseScope(proc, "eliminateDeadCode");
return eliminateDeadCodeImpl(proc);
}
} }
#endif // ENABLE(B3_JIT)