#include "config.h"
#include "B3PureCSE.h"
#if ENABLE(B3_JIT)
#include "B3Dominators.h"
#include "B3PhaseScope.h"
#include "B3Value.h"
namespace JSC { namespace B3 {
PureCSE::PureCSE()
{
}
PureCSE::~PureCSE()
{
}
void PureCSE::clear()
{
m_map.clear();
}
Value* PureCSE::findMatch(const ValueKey& key, BasicBlock* block, Dominators& dominators)
{
if (!key)
return nullptr;
auto iter = m_map.find(key);
if (iter == m_map.end())
return nullptr;
for (Value* match : iter->value) {
if (!match->owner)
continue;
if (dominators.dominates(match->owner, block))
return match;
}
return nullptr;
}
bool PureCSE::process(Value* value, Dominators& dominators)
{
if (value->opcode() == Identity)
return false;
ValueKey key = value->key();
if (!key)
return false;
Matches& matches = m_map.add(key, Matches()).iterator->value;
for (Value* match : matches) {
if (!match->owner)
continue;
if (dominators.dominates(match->owner, value->owner)) {
value->replaceWithIdentity(match);
return true;
}
}
matches.append(value);
return false;
}
bool pureCSE(Procedure& proc)
{
PhaseScope phaseScope(proc, "pureCSE");
Dominators& dominators = proc.dominators();
PureCSE pureCSE;
bool result = false;
for (BasicBlock* block : proc.blocksInPreOrder()) {
for (Value* value : *block) {
result |= value->performSubstitution();
result |= pureCSE.process(value, dominators);
}
}
return result;
}
} }
#endif // ENABLE(B3_JIT)