B3BlockInsertionSet.cpp [plain text]
#include "config.h"
#include "B3BlockInsertionSet.h"
#if ENABLE(B3_JIT)
#include "B3BasicBlockInlines.h"
#include "B3InsertionSet.h"
#include "B3ProcedureInlines.h"
#include <wtf/BubbleSort.h>
namespace JSC { namespace B3 {
BlockInsertionSet::BlockInsertionSet(Procedure &proc)
: m_proc(proc)
{
}
BlockInsertionSet::~BlockInsertionSet() { }
void BlockInsertionSet::insert(BlockInsertion&& insertion)
{
m_insertions.append(WTFMove(insertion));
}
BasicBlock* BlockInsertionSet::insert(unsigned index, double frequency)
{
std::unique_ptr<BasicBlock> block(new BasicBlock(UINT_MAX, frequency));
BasicBlock* result = block.get();
insert(BlockInsertion(index, WTFMove(block)));
return result;
}
BasicBlock* BlockInsertionSet::insertBefore(BasicBlock* before, double frequency)
{
return insert(before->index(), frequency == frequency ? frequency : before->frequency());
}
BasicBlock* BlockInsertionSet::insertAfter(BasicBlock* after, double frequency)
{
return insert(after->index() + 1, frequency == frequency ? frequency : after->frequency());
}
BasicBlock* BlockInsertionSet::splitForward(
BasicBlock* block, unsigned& valueIndex, InsertionSet* insertionSet, double frequency)
{
Value* value = block->at(valueIndex);
BasicBlock* result = insertBefore(block, frequency);
result->m_values.resize(valueIndex + 1);
for (unsigned i = valueIndex; i--;)
result->m_values[i] = block->m_values[i];
result->m_values[valueIndex] = m_proc.add<Value>(Jump, value->origin());
result->setSuccessors(FrequentedBlock(block));
if (insertionSet)
insertionSet->execute(result);
block->m_values.remove(0, valueIndex);
valueIndex = 0;
result->predecessors() = WTFMove(block->predecessors());
block->addPredecessor(result);
for (BasicBlock* predecessor : result->predecessors())
predecessor->replaceSuccessor(block, result);
return result;
}
bool BlockInsertionSet::execute()
{
if (m_insertions.isEmpty())
return false;
bubbleSort(m_insertions.begin(), m_insertions.end());
executeInsertions(m_proc.m_blocks, m_insertions);
m_proc.m_blocks.removeAllMatching(
[&] (std::unique_ptr<BasicBlock>& blockPtr) -> bool {
return !blockPtr;
});
for (unsigned i = 0; i < m_proc.m_blocks.size(); ++i)
m_proc.m_blocks[i]->m_index = i;
return true;
}
} }
#endif // ENABLE(B3_JIT)