2023-02-24 21:49:38 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
2023-03-31 19:42:49 +01:00
|
|
|
#include "Luau/IrRegAllocX64.h"
|
2023-02-24 21:49:38 +00:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
#include "Luau/IrUtils.h"
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
#include "EmitCommonX64.h"
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
2023-03-03 20:21:14 +00:00
|
|
|
namespace X64
|
|
|
|
{
|
2023-02-24 21:49:38 +00:00
|
|
|
|
|
|
|
static const RegisterX64 kGprAllocOrder[] = {rax, rdx, rcx, rbx, rsi, rdi, r8, r9, r10, r11};
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
IrRegAllocX64::IrRegAllocX64(AssemblyBuilderX64& build, IrFunction& function)
|
|
|
|
: build(build)
|
|
|
|
, function(function)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
|
|
|
freeGprMap.fill(true);
|
2023-04-07 22:01:29 +01:00
|
|
|
gprInstUsers.fill(kInvalidInstIdx);
|
2023-02-24 21:49:38 +00:00
|
|
|
freeXmmMap.fill(true);
|
2023-04-07 22:01:29 +01:00
|
|
|
xmmInstUsers.fill(kInvalidInstIdx);
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
RegisterX64 IrRegAllocX64::allocReg(SizeX64 size, uint32_t instIdx)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
if (size == SizeX64::xmmword)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
for (size_t i = 0; i < freeXmmMap.size(); ++i)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
if (freeXmmMap[i])
|
|
|
|
{
|
|
|
|
freeXmmMap[i] = false;
|
|
|
|
xmmInstUsers[i] = instIdx;
|
|
|
|
return RegisterX64{size, uint8_t(i)};
|
|
|
|
}
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 19:06:22 +01:00
|
|
|
else
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
for (RegisterX64 reg : kGprAllocOrder)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
if (freeGprMap[reg.index])
|
|
|
|
{
|
|
|
|
freeGprMap[reg.index] = false;
|
|
|
|
gprInstUsers[reg.index] = instIdx;
|
|
|
|
return RegisterX64{size, reg.index};
|
|
|
|
}
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
// Out of registers, spill the value with the furthest next use
|
2023-04-14 19:06:22 +01:00
|
|
|
const std::array<uint32_t, 16>& regInstUsers = size == SizeX64::xmmword ? xmmInstUsers : gprInstUsers;
|
|
|
|
if (uint32_t furthestUseTarget = findInstructionWithFurthestNextUse(regInstUsers); furthestUseTarget != kInvalidInstIdx)
|
2023-04-07 22:01:29 +01:00
|
|
|
return takeReg(function.instructions[furthestUseTarget].regX64, instIdx);
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
LUAU_ASSERT(!"Out of registers to allocate");
|
2023-02-24 21:49:38 +00:00
|
|
|
return noreg;
|
|
|
|
}
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
RegisterX64 IrRegAllocX64::allocRegOrReuse(SizeX64 size, uint32_t instIdx, std::initializer_list<IrOp> oprefs)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
|
|
|
for (IrOp op : oprefs)
|
|
|
|
{
|
|
|
|
if (op.kind != IrOpKind::Inst)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
IrInst& source = function.instructions[op.index];
|
|
|
|
|
2023-04-21 23:14:26 +01:00
|
|
|
if (source.lastUse == instIdx && !source.reusedReg && !source.spilled && !source.needsReload)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
// Not comparing size directly because we only need matching register set
|
|
|
|
if ((size == SizeX64::xmmword) != (source.regX64.size == SizeX64::xmmword))
|
|
|
|
continue;
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
LUAU_ASSERT(source.regX64 != noreg);
|
|
|
|
|
|
|
|
source.reusedReg = true;
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
if (size == SizeX64::xmmword)
|
|
|
|
xmmInstUsers[source.regX64.index] = instIdx;
|
|
|
|
else
|
|
|
|
gprInstUsers[source.regX64.index] = instIdx;
|
2023-02-24 21:49:38 +00:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
return RegisterX64{size, source.regX64.index};
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
return allocReg(size, instIdx);
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
RegisterX64 IrRegAllocX64::takeReg(RegisterX64 reg, uint32_t instIdx)
|
2023-03-03 20:21:14 +00:00
|
|
|
{
|
2023-03-31 19:42:49 +01:00
|
|
|
if (reg.size == SizeX64::xmmword)
|
|
|
|
{
|
2023-04-07 22:01:29 +01:00
|
|
|
if (!freeXmmMap[reg.index])
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(xmmInstUsers[reg.index] != kInvalidInstIdx);
|
|
|
|
preserve(function.instructions[xmmInstUsers[reg.index]]);
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:42:49 +01:00
|
|
|
LUAU_ASSERT(freeXmmMap[reg.index]);
|
|
|
|
freeXmmMap[reg.index] = false;
|
2023-04-07 22:01:29 +01:00
|
|
|
xmmInstUsers[reg.index] = instIdx;
|
2023-03-31 19:42:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-07 22:01:29 +01:00
|
|
|
if (!freeGprMap[reg.index])
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(gprInstUsers[reg.index] != kInvalidInstIdx);
|
|
|
|
preserve(function.instructions[gprInstUsers[reg.index]]);
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:42:49 +01:00
|
|
|
LUAU_ASSERT(freeGprMap[reg.index]);
|
|
|
|
freeGprMap[reg.index] = false;
|
2023-04-07 22:01:29 +01:00
|
|
|
gprInstUsers[reg.index] = instIdx;
|
2023-03-31 19:42:49 +01:00
|
|
|
}
|
2023-03-03 20:21:14 +00:00
|
|
|
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2023-07-07 21:10:48 +01:00
|
|
|
bool IrRegAllocX64::canTakeReg(RegisterX64 reg) const
|
|
|
|
{
|
|
|
|
const std::array<bool, 16>& freeMap = reg.size == SizeX64::xmmword ? freeXmmMap : freeGprMap;
|
|
|
|
const std::array<uint32_t, 16>& instUsers = reg.size == SizeX64::xmmword ? xmmInstUsers : gprInstUsers;
|
|
|
|
|
|
|
|
return freeMap[reg.index] || instUsers[reg.index] != kInvalidInstIdx;
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
void IrRegAllocX64::freeReg(RegisterX64 reg)
|
|
|
|
{
|
|
|
|
if (reg.size == SizeX64::xmmword)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!freeXmmMap[reg.index]);
|
|
|
|
freeXmmMap[reg.index] = true;
|
2023-04-07 22:01:29 +01:00
|
|
|
xmmInstUsers[reg.index] = kInvalidInstIdx;
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!freeGprMap[reg.index]);
|
|
|
|
freeGprMap[reg.index] = true;
|
2023-04-07 22:01:29 +01:00
|
|
|
gprInstUsers[reg.index] = kInvalidInstIdx;
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
void IrRegAllocX64::freeLastUseReg(IrInst& target, uint32_t instIdx)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-07 22:01:29 +01:00
|
|
|
if (isLastUseReg(target, instIdx))
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-21 23:14:26 +01:00
|
|
|
LUAU_ASSERT(!target.spilled && !target.needsReload);
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
// Register might have already been freed if it had multiple uses inside a single instruction
|
|
|
|
if (target.regX64 == noreg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
freeReg(target.regX64);
|
|
|
|
target.regX64 = noreg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
void IrRegAllocX64::freeLastUseRegs(const IrInst& inst, uint32_t instIdx)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-04-07 22:01:29 +01:00
|
|
|
auto checkOp = [this, instIdx](IrOp op) {
|
2023-02-24 21:49:38 +00:00
|
|
|
if (op.kind == IrOpKind::Inst)
|
2023-04-07 22:01:29 +01:00
|
|
|
freeLastUseReg(function.instructions[op.index], instIdx);
|
2023-02-24 21:49:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
checkOp(inst.a);
|
|
|
|
checkOp(inst.b);
|
|
|
|
checkOp(inst.c);
|
|
|
|
checkOp(inst.d);
|
|
|
|
checkOp(inst.e);
|
|
|
|
checkOp(inst.f);
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
bool IrRegAllocX64::isLastUseReg(const IrInst& target, uint32_t instIdx) const
|
|
|
|
{
|
|
|
|
return target.lastUse == instIdx && !target.reusedReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrRegAllocX64::preserve(IrInst& inst)
|
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
IrSpillX64 spill;
|
|
|
|
spill.instIdx = function.getInstIndex(inst);
|
|
|
|
spill.valueKind = getCmdValueKind(inst.cmd);
|
|
|
|
spill.spillId = nextSpillId++;
|
|
|
|
spill.originalLoc = inst.regX64;
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
// Loads from VmReg/VmConst don't have to be spilled, they can be restored from a register later
|
|
|
|
if (!hasRestoreOp(inst))
|
2023-04-07 22:01:29 +01:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
unsigned i = findSpillStackSlot(spill.valueKind);
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
if (spill.valueKind == IrValueKind::Tvalue)
|
2023-04-07 22:01:29 +01:00
|
|
|
build.vmovups(xmmword[sSpillArea + i * 8], inst.regX64);
|
2023-04-14 19:06:22 +01:00
|
|
|
else if (spill.valueKind == IrValueKind::Double)
|
2023-04-07 22:01:29 +01:00
|
|
|
build.vmovsd(qword[sSpillArea + i * 8], inst.regX64);
|
2023-04-14 19:06:22 +01:00
|
|
|
else if (spill.valueKind == IrValueKind::Pointer)
|
|
|
|
build.mov(qword[sSpillArea + i * 8], inst.regX64);
|
|
|
|
else if (spill.valueKind == IrValueKind::Tag || spill.valueKind == IrValueKind::Int)
|
|
|
|
build.mov(dword[sSpillArea + i * 8], inst.regX64);
|
2023-04-07 22:01:29 +01:00
|
|
|
else
|
2023-05-25 22:36:34 +01:00
|
|
|
LUAU_ASSERT(!"Unsupported value kind");
|
2023-04-07 22:01:29 +01:00
|
|
|
|
|
|
|
usedSpillSlots.set(i);
|
|
|
|
|
|
|
|
if (i + 1 > maxUsedSlot)
|
|
|
|
maxUsedSlot = i + 1;
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
if (spill.valueKind == IrValueKind::Tvalue)
|
2023-04-07 22:01:29 +01:00
|
|
|
{
|
|
|
|
usedSpillSlots.set(i + 1);
|
|
|
|
|
|
|
|
if (i + 2 > maxUsedSlot)
|
|
|
|
maxUsedSlot = i + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
spill.stackSlot = uint8_t(i);
|
2023-04-21 23:14:26 +01:00
|
|
|
inst.spilled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst.needsReload = true;
|
2023-04-14 19:06:22 +01:00
|
|
|
}
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
spills.push_back(spill);
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
freeReg(inst.regX64);
|
|
|
|
inst.regX64 = noreg;
|
2023-04-07 22:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrRegAllocX64::restore(IrInst& inst, bool intoOriginalLocation)
|
|
|
|
{
|
|
|
|
uint32_t instIdx = function.getInstIndex(inst);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < spills.size(); i++)
|
|
|
|
{
|
2023-04-21 23:14:26 +01:00
|
|
|
if (spills[i].instIdx == instIdx)
|
2023-04-07 22:01:29 +01:00
|
|
|
{
|
2023-04-21 23:14:26 +01:00
|
|
|
RegisterX64 reg = intoOriginalLocation ? takeReg(spills[i].originalLoc, instIdx) : allocReg(spills[i].originalLoc.size, instIdx);
|
2023-04-14 19:06:22 +01:00
|
|
|
OperandX64 restoreLocation = noreg;
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-21 23:14:26 +01:00
|
|
|
// Previous call might have relocated the spill vector, so this reference can't be taken earlier
|
|
|
|
const IrSpillX64& spill = spills[i];
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
if (spill.stackSlot != kNoStackSlot)
|
2023-04-07 22:01:29 +01:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
restoreLocation = addr[sSpillArea + spill.stackSlot * 8];
|
|
|
|
restoreLocation.memSize = reg.size;
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
usedSpillSlots.set(spill.stackSlot, false);
|
|
|
|
|
|
|
|
if (spill.valueKind == IrValueKind::Tvalue)
|
|
|
|
usedSpillSlots.set(spill.stackSlot + 1, false);
|
2023-04-07 22:01:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
restoreLocation = getRestoreAddress(inst, getRestoreOp(inst));
|
2023-04-07 22:01:29 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
if (spill.valueKind == IrValueKind::Tvalue)
|
|
|
|
build.vmovups(reg, restoreLocation);
|
|
|
|
else if (spill.valueKind == IrValueKind::Double)
|
|
|
|
build.vmovsd(reg, restoreLocation);
|
|
|
|
else
|
|
|
|
build.mov(reg, restoreLocation);
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
inst.regX64 = reg;
|
|
|
|
inst.spilled = false;
|
2023-04-21 23:14:26 +01:00
|
|
|
inst.needsReload = false;
|
2023-04-07 22:01:29 +01:00
|
|
|
|
|
|
|
spills[i] = spills.back();
|
|
|
|
spills.pop_back();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrRegAllocX64::preserveAndFreeInstValues()
|
2023-03-31 19:42:49 +01:00
|
|
|
{
|
2023-04-07 22:01:29 +01:00
|
|
|
for (uint32_t instIdx : gprInstUsers)
|
|
|
|
{
|
|
|
|
if (instIdx != kInvalidInstIdx)
|
|
|
|
preserve(function.instructions[instIdx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t instIdx : xmmInstUsers)
|
|
|
|
{
|
|
|
|
if (instIdx != kInvalidInstIdx)
|
|
|
|
preserve(function.instructions[instIdx]);
|
|
|
|
}
|
2023-03-31 19:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IrRegAllocX64::shouldFreeGpr(RegisterX64 reg) const
|
|
|
|
{
|
|
|
|
if (reg == noreg)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LUAU_ASSERT(reg.size != SizeX64::xmmword);
|
|
|
|
|
|
|
|
for (RegisterX64 gpr : kGprAllocOrder)
|
|
|
|
{
|
|
|
|
if (reg.index == gpr.index)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
unsigned IrRegAllocX64::findSpillStackSlot(IrValueKind valueKind)
|
|
|
|
{
|
|
|
|
// Find a free stack slot. Two consecutive slots might be required for 16 byte TValues, so '- 1' is used
|
|
|
|
for (unsigned i = 0; i < unsigned(usedSpillSlots.size() - 1); ++i)
|
|
|
|
{
|
|
|
|
if (usedSpillSlots.test(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (valueKind == IrValueKind::Tvalue && usedSpillSlots.test(i + 1))
|
|
|
|
{
|
|
|
|
++i; // No need to retest this double position
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
LUAU_ASSERT(!"Nowhere to spill");
|
2023-04-14 19:06:22 +01:00
|
|
|
return ~0u;
|
|
|
|
}
|
|
|
|
|
|
|
|
IrOp IrRegAllocX64::getRestoreOp(const IrInst& inst) const
|
|
|
|
{
|
2023-04-21 23:14:26 +01:00
|
|
|
if (IrOp location = function.findRestoreOp(inst); location.kind == IrOpKind::VmReg || location.kind == IrOpKind::VmConst)
|
|
|
|
return location;
|
2023-04-14 19:06:22 +01:00
|
|
|
|
|
|
|
return IrOp();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IrRegAllocX64::hasRestoreOp(const IrInst& inst) const
|
|
|
|
{
|
|
|
|
return getRestoreOp(inst).kind != IrOpKind::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperandX64 IrRegAllocX64::getRestoreAddress(const IrInst& inst, IrOp restoreOp)
|
|
|
|
{
|
2023-04-21 23:14:26 +01:00
|
|
|
switch (getCmdValueKind(inst.cmd))
|
2023-04-14 19:06:22 +01:00
|
|
|
{
|
2023-04-21 23:14:26 +01:00
|
|
|
case IrValueKind::Unknown:
|
|
|
|
case IrValueKind::None:
|
|
|
|
LUAU_ASSERT(!"Invalid operand restore value kind");
|
|
|
|
break;
|
|
|
|
case IrValueKind::Tag:
|
2023-04-14 19:06:22 +01:00
|
|
|
return restoreOp.kind == IrOpKind::VmReg ? luauRegTag(vmRegOp(restoreOp)) : luauConstantTag(vmConstOp(restoreOp));
|
2023-04-21 23:14:26 +01:00
|
|
|
case IrValueKind::Int:
|
2023-04-14 19:06:22 +01:00
|
|
|
LUAU_ASSERT(restoreOp.kind == IrOpKind::VmReg);
|
|
|
|
return luauRegValueInt(vmRegOp(restoreOp));
|
2023-04-21 23:14:26 +01:00
|
|
|
case IrValueKind::Pointer:
|
|
|
|
return restoreOp.kind == IrOpKind::VmReg ? luauRegValue(vmRegOp(restoreOp)) : luauConstantValue(vmConstOp(restoreOp));
|
|
|
|
case IrValueKind::Double:
|
|
|
|
return restoreOp.kind == IrOpKind::VmReg ? luauRegValue(vmRegOp(restoreOp)) : luauConstantValue(vmConstOp(restoreOp));
|
|
|
|
case IrValueKind::Tvalue:
|
2023-04-14 19:06:22 +01:00
|
|
|
return restoreOp.kind == IrOpKind::VmReg ? luauReg(vmRegOp(restoreOp)) : luauConstant(vmConstOp(restoreOp));
|
|
|
|
}
|
|
|
|
|
2023-04-21 23:14:26 +01:00
|
|
|
LUAU_ASSERT(!"Failed to find restore operand location");
|
2023-04-14 19:06:22 +01:00
|
|
|
return noreg;
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
uint32_t IrRegAllocX64::findInstructionWithFurthestNextUse(const std::array<uint32_t, 16>& regInstUsers) const
|
|
|
|
{
|
|
|
|
uint32_t furthestUseTarget = kInvalidInstIdx;
|
|
|
|
uint32_t furthestUseLocation = 0;
|
|
|
|
|
|
|
|
for (uint32_t regInstUser : regInstUsers)
|
|
|
|
{
|
|
|
|
// Cannot spill temporary registers or the register of the value that's defined in the current instruction
|
|
|
|
if (regInstUser == kInvalidInstIdx || regInstUser == currInstIdx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint32_t nextUse = getNextInstUse(function, regInstUser, currInstIdx);
|
|
|
|
|
|
|
|
// Cannot spill value that is about to be used in the current instruction
|
|
|
|
if (nextUse == currInstIdx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (furthestUseTarget == kInvalidInstIdx || nextUse > furthestUseLocation)
|
|
|
|
{
|
|
|
|
furthestUseLocation = nextUse;
|
|
|
|
furthestUseTarget = regInstUser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return furthestUseTarget;
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:42:49 +01:00
|
|
|
void IrRegAllocX64::assertFree(RegisterX64 reg) const
|
|
|
|
{
|
|
|
|
if (reg.size == SizeX64::xmmword)
|
|
|
|
LUAU_ASSERT(freeXmmMap[reg.index]);
|
|
|
|
else
|
|
|
|
LUAU_ASSERT(freeGprMap[reg.index]);
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
void IrRegAllocX64::assertAllFree() const
|
|
|
|
{
|
|
|
|
for (RegisterX64 reg : kGprAllocOrder)
|
|
|
|
LUAU_ASSERT(freeGprMap[reg.index]);
|
|
|
|
|
|
|
|
for (bool free : freeXmmMap)
|
|
|
|
LUAU_ASSERT(free);
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
void IrRegAllocX64::assertNoSpills() const
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(spills.empty());
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
ScopedRegX64::ScopedRegX64(IrRegAllocX64& owner)
|
|
|
|
: owner(owner)
|
|
|
|
, reg(noreg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
ScopedRegX64::ScopedRegX64(IrRegAllocX64& owner, SizeX64 size)
|
|
|
|
: owner(owner)
|
2023-03-10 20:21:07 +00:00
|
|
|
, reg(noreg)
|
2023-02-24 21:49:38 +00:00
|
|
|
{
|
2023-03-10 20:21:07 +00:00
|
|
|
alloc(size);
|
2023-02-24 21:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedRegX64::ScopedRegX64(IrRegAllocX64& owner, RegisterX64 reg)
|
|
|
|
: owner(owner)
|
|
|
|
, reg(reg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedRegX64::~ScopedRegX64()
|
|
|
|
{
|
|
|
|
if (reg != noreg)
|
|
|
|
owner.freeReg(reg);
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
void ScopedRegX64::alloc(SizeX64 size)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(reg == noreg);
|
2023-04-14 19:06:22 +01:00
|
|
|
reg = owner.allocReg(size, kInvalidInstIdx);
|
2023-03-10 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
void ScopedRegX64::free()
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(reg != noreg);
|
|
|
|
owner.freeReg(reg);
|
|
|
|
reg = noreg;
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:42:49 +01:00
|
|
|
RegisterX64 ScopedRegX64::release()
|
|
|
|
{
|
|
|
|
RegisterX64 tmp = reg;
|
|
|
|
reg = noreg;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
ScopedSpills::ScopedSpills(IrRegAllocX64& owner)
|
|
|
|
: owner(owner)
|
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
startSpillId = owner.nextSpillId;
|
2023-04-07 22:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedSpills::~ScopedSpills()
|
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
unsigned endSpillId = owner.nextSpillId;
|
2023-04-07 22:01:29 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
for (size_t i = 0; i < owner.spills.size();)
|
2023-04-07 22:01:29 +01:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
IrSpillX64& spill = owner.spills[i];
|
|
|
|
|
|
|
|
// Restoring spills inside this scope cannot create new spills
|
|
|
|
LUAU_ASSERT(spill.spillId < endSpillId);
|
|
|
|
|
|
|
|
// If spill was created inside current scope, it has to be restored
|
|
|
|
if (spill.spillId >= startSpillId)
|
2023-04-07 22:01:29 +01:00
|
|
|
{
|
2023-04-14 19:06:22 +01:00
|
|
|
IrInst& inst = owner.function.instructions[spill.instIdx];
|
2023-04-07 22:01:29 +01:00
|
|
|
|
|
|
|
owner.restore(inst, /*intoOriginalLocation*/ true);
|
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
// Spill restore removes the spill entry, so loop is repeated at the same 'i'
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
2023-04-07 22:01:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
} // namespace X64
|
2023-02-24 21:49:38 +00:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|