DFGStackLayoutPhase.cpp [plain text]
#include "config.h"
#include "DFGStackLayoutPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGGraph.h"
#include "DFGPhase.h"
#include "DFGValueSource.h"
#include "JSCJSValueInlines.h"
namespace JSC { namespace DFG {
class StackLayoutPhase : public Phase {
static constexpr bool verbose = false;
public:
StackLayoutPhase(Graph& graph)
: Phase(graph, "stack layout")
{
}
bool run()
{
Operands<bool> usedOperands(0, graph().m_localVars, graph().m_tmps, false);
bool hasNodesThatNeedFixup = false;
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);
switch (node->op()) {
case GetLocal:
case SetLocal:
case Flush:
case PhantomLocal: {
VariableAccessData* variable = node->variableAccessData();
if (variable->operand().isArgument())
break;
usedOperands.setOperand(variable->operand(), true);
break;
}
case LoadVarargs:
case ForwardVarargs: {
LoadVarargsData* data = node->loadVarargsData();
usedOperands.setOperand(data->count, true);
if (data->start.isLocal()) {
ASSERT(VirtualRegister(data->start.offset() + data->limit - 1).isLocal());
for (unsigned i = data->limit; i--;)
usedOperands.setOperand(VirtualRegister(data->start.offset() + i), true);
} hasNodesThatNeedFixup = true;
break;
}
case PutStack:
case GetStack: {
StackAccessData* stack = node->stackAccessData();
if (stack->operand.isArgument())
break;
usedOperands.setOperand(stack->operand, true);
break;
}
default:
break;
}
}
}
for (InlineCallFrameSet::iterator iter = m_graph.m_plan.inlineCallFrames()->begin(); !!iter; ++iter) {
InlineCallFrame* inlineCallFrame = *iter;
if (inlineCallFrame->isVarargs()) {
usedOperands.setOperand(VirtualRegister(
CallFrameSlot::argumentCountIncludingThis + inlineCallFrame->stackOffset), true);
}
for (unsigned argument = inlineCallFrame->argumentsWithFixup.size(); argument--;) {
usedOperands.setOperand(VirtualRegister(
virtualRegisterForArgumentIncludingThis(argument).offset() +
inlineCallFrame->stackOffset), true);
}
}
Vector<unsigned> allocation(usedOperands.size());
m_graph.m_nextMachineLocal = codeBlock()->calleeSaveSpaceAsVirtualRegisters();
for (unsigned i = 0; i < usedOperands.size(); ++i) {
if (!usedOperands.getForOperandIndex(i)) {
allocation[i] = UINT_MAX;
continue;
}
allocation[i] = m_graph.m_nextMachineLocal++;
}
for (unsigned i = m_graph.m_variableAccessData.size(); i--;) {
VariableAccessData* variable = &m_graph.m_variableAccessData[i];
if (!variable->isRoot())
continue;
if (variable->operand().isArgument()) {
variable->machineLocal() = variable->operand().virtualRegister();
continue;
}
Operand operand = variable->operand();
size_t index = usedOperands.operandIndex(operand);
if (index >= allocation.size())
continue;
if (allocation[index] == UINT_MAX)
continue;
variable->machineLocal() = assign(usedOperands, allocation, variable->operand());
}
for (StackAccessData* data : m_graph.m_stackAccessData) {
if (data->operand.isArgument()) {
data->machineLocal = data->operand.virtualRegister();
continue;
}
if (data->operand.isLocal()) {
if (static_cast<size_t>(data->operand.toLocal()) >= allocation.size())
continue;
if (allocation[data->operand.toLocal()] == UINT_MAX)
continue;
}
data->machineLocal = assign(usedOperands, allocation, data->operand);
}
if (!m_graph.needsScopeRegister())
codeBlock()->setScopeRegister(VirtualRegister());
else
codeBlock()->setScopeRegister(assign(usedOperands, allocation, codeBlock()->scopeRegister()));
for (unsigned i = m_graph.m_inlineVariableData.size(); i--;) {
InlineVariableData data = m_graph.m_inlineVariableData[i];
InlineCallFrame* inlineCallFrame = data.inlineCallFrame;
if (inlineCallFrame->isVarargs())
inlineCallFrame->argumentCountRegister = assign(usedOperands, allocation, VirtualRegister(inlineCallFrame->stackOffset + CallFrameSlot::argumentCountIncludingThis));
for (unsigned argument = inlineCallFrame->argumentsWithFixup.size(); argument--;) {
ArgumentPosition& position = m_graph.m_argumentPositions[
data.argumentPositionStart + argument];
VariableAccessData* variable = position.someVariable();
ValueSource source;
if (!variable)
source = ValueSource(SourceIsDead);
else {
source = ValueSource::forFlushFormat(
variable->machineLocal(), variable->flushFormat());
}
inlineCallFrame->argumentsWithFixup[argument] = source.valueRecovery();
}
RELEASE_ASSERT(inlineCallFrame->isClosureCall == !!data.calleeVariable);
if (inlineCallFrame->isClosureCall) {
VariableAccessData* variable = data.calleeVariable->find();
ValueSource source = ValueSource::forFlushFormat(
variable->machineLocal(),
variable->flushFormat());
inlineCallFrame->calleeRecovery = source.valueRecovery();
} else
RELEASE_ASSERT(inlineCallFrame->calleeRecovery.isConstant());
}
if (hasNodesThatNeedFixup) {
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);
switch (node->op()) {
case LoadVarargs:
case ForwardVarargs: {
LoadVarargsData* data = node->loadVarargsData();
data->machineCount = assign(usedOperands, allocation, data->count);
data->machineStart = assign(usedOperands, allocation, data->start);
break;
}
default:
break;
}
}
}
}
return true;
}
private:
VirtualRegister assign(const Operands<bool>& usedOperands, const Vector<unsigned>& allocation, Operand operand)
{
if (operand.isArgument())
return operand.virtualRegister();
size_t operandIndex = usedOperands.operandIndex(operand);
unsigned myAllocation = allocation[operandIndex];
if (myAllocation == UINT_MAX)
return VirtualRegister();
return virtualRegisterForLocal(myAllocation);
}
};
bool performStackLayout(Graph& graph)
{
return runPhase<StackLayoutPhase>(graph);
}
} }
#endif // ENABLE(DFG_JIT)