AirStackAllocation.cpp [plain text]
#include "config.h"
#include "AirStackAllocation.h"
#if ENABLE(B3_JIT)
#include "AirArgInlines.h"
#include "AirCode.h"
#include "AirInstInlines.h"
#include "StackAlignment.h"
#include <wtf/ListDump.h>
namespace JSC { namespace B3 { namespace Air {
namespace {
const bool verbose = false;
template<typename Collection>
void updateFrameSizeBasedOnStackSlotsImpl(Code& code, const Collection& collection)
{
unsigned frameSize = 0;
for (StackSlot* slot : collection)
frameSize = std::max(frameSize, static_cast<unsigned>(-slot->offsetFromFP()));
code.setFrameSize(WTF::roundUpToMultipleOf(stackAlignmentBytes(), frameSize));
}
}
bool attemptAssignment(
StackSlot* slot, intptr_t offsetFromFP, const Vector<StackSlot*>& otherSlots)
{
if (verbose)
dataLog("Attempting to assign ", pointerDump(slot), " to ", offsetFromFP, " with interference ", pointerListDump(otherSlots), "\n");
offsetFromFP = -WTF::roundUpToMultipleOf(slot->alignment(), -offsetFromFP);
for (StackSlot* otherSlot : otherSlots) {
if (!otherSlot->offsetFromFP())
continue;
bool overlap = WTF::rangesOverlap(
offsetFromFP,
offsetFromFP + static_cast<intptr_t>(slot->byteSize()),
otherSlot->offsetFromFP(),
otherSlot->offsetFromFP() + static_cast<intptr_t>(otherSlot->byteSize()));
if (overlap)
return false;
}
if (verbose)
dataLog("Assigned ", pointerDump(slot), " to ", offsetFromFP, "\n");
slot->setOffsetFromFP(offsetFromFP);
return true;
}
void assign(StackSlot* slot, const Vector<StackSlot*>& otherSlots)
{
if (verbose)
dataLog("Attempting to assign ", pointerDump(slot), " with interference ", pointerListDump(otherSlots), "\n");
if (attemptAssignment(slot, -static_cast<intptr_t>(slot->byteSize()), otherSlots))
return;
for (StackSlot* otherSlot : otherSlots) {
if (!otherSlot->offsetFromFP())
continue;
bool didAssign = attemptAssignment(
slot, otherSlot->offsetFromFP() - static_cast<intptr_t>(slot->byteSize()), otherSlots);
if (didAssign)
return;
}
RELEASE_ASSERT_NOT_REACHED();
}
Vector<StackSlot*> allocateAndGetEscapedStackSlotsWithoutChangingFrameSize(Code& code)
{
RELEASE_ASSERT(!code.frameSize());
Vector<StackSlot*> assignedEscapedStackSlots;
Vector<StackSlot*> escapedStackSlotsWorklist;
for (StackSlot* slot : code.stackSlots()) {
if (slot->isLocked()) {
if (slot->offsetFromFP())
assignedEscapedStackSlots.append(slot);
else
escapedStackSlotsWorklist.append(slot);
} else {
ASSERT(!slot->offsetFromFP());
}
}
while (!escapedStackSlotsWorklist.isEmpty()) {
StackSlot* slot = escapedStackSlotsWorklist.takeLast();
assign(slot, assignedEscapedStackSlots);
assignedEscapedStackSlots.append(slot);
}
return assignedEscapedStackSlots;
}
void allocateEscapedStackSlots(Code& code)
{
updateFrameSizeBasedOnStackSlotsImpl(
code, allocateAndGetEscapedStackSlotsWithoutChangingFrameSize(code));
}
void updateFrameSizeBasedOnStackSlots(Code& code)
{
updateFrameSizeBasedOnStackSlotsImpl(code, code.stackSlots());
}
} } }
#endif // ENABLE(B3_JIT)