luau/CodeGen/src/IrAnalysis.cpp

121 lines
2.8 KiB
C++
Raw Normal View History

2023-01-27 21:28:45 +00:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2023-02-03 12:34:12 +00:00
#include "Luau/IrAnalysis.h"
2023-01-27 21:28:45 +00:00
2023-03-03 13:45:38 +00:00
#include "Luau/DenseHash.h"
2023-02-03 12:34:12 +00:00
#include "Luau/IrData.h"
#include "Luau/IrUtils.h"
2023-01-27 21:28:45 +00:00
#include <stddef.h>
namespace Luau
{
namespace CodeGen
{
2023-02-10 18:50:54 +00:00
void updateUseCounts(IrFunction& function)
2023-01-27 21:28:45 +00:00
{
2023-02-10 18:50:54 +00:00
std::vector<IrBlock>& blocks = function.blocks;
std::vector<IrInst>& instructions = function.instructions;
for (IrBlock& block : blocks)
block.useCount = 0;
for (IrInst& inst : instructions)
inst.useCount = 0;
2023-01-27 21:28:45 +00:00
2023-02-10 18:50:54 +00:00
auto checkOp = [&](IrOp op) {
if (op.kind == IrOpKind::Inst)
{
IrInst& target = instructions[op.index];
LUAU_ASSERT(target.useCount < 0xffff);
target.useCount++;
}
else if (op.kind == IrOpKind::Block)
{
IrBlock& target = blocks[op.index];
LUAU_ASSERT(target.useCount < 0xffff);
target.useCount++;
}
};
for (IrInst& inst : instructions)
{
checkOp(inst.a);
checkOp(inst.b);
checkOp(inst.c);
checkOp(inst.d);
checkOp(inst.e);
2023-02-24 18:24:22 +00:00
checkOp(inst.f);
2023-02-10 18:50:54 +00:00
}
2023-01-27 21:28:45 +00:00
}
2023-02-10 18:50:54 +00:00
void updateLastUseLocations(IrFunction& function)
2023-01-27 21:28:45 +00:00
{
std::vector<IrInst>& instructions = function.instructions;
for (IrInst& inst : instructions)
inst.lastUse = 0;
2023-02-10 18:50:54 +00:00
for (size_t instIdx = 0; instIdx < instructions.size(); ++instIdx)
2023-01-27 21:28:45 +00:00
{
2023-02-10 18:50:54 +00:00
IrInst& inst = instructions[instIdx];
2023-01-27 21:28:45 +00:00
2023-02-10 18:50:54 +00:00
auto checkOp = [&](IrOp op) {
2023-01-27 21:28:45 +00:00
if (op.kind == IrOpKind::Inst)
2023-02-10 18:50:54 +00:00
instructions[op.index].lastUse = uint32_t(instIdx);
2023-01-27 21:28:45 +00:00
};
checkOp(inst.a);
checkOp(inst.b);
checkOp(inst.c);
checkOp(inst.d);
checkOp(inst.e);
2023-02-24 18:24:22 +00:00
checkOp(inst.f);
2023-01-27 21:28:45 +00:00
}
}
2023-03-03 13:45:38 +00:00
std::pair<uint32_t, uint32_t> getLiveInOutValueCount(IrFunction& function, IrBlock& block)
{
uint32_t liveIns = 0;
uint32_t liveOuts = 0;
auto checkOp = [&](IrOp op) {
if (op.kind == IrOpKind::Inst)
{
if (op.index >= block.start && op.index <= block.finish)
liveOuts--;
else
liveIns++;
}
};
for (uint32_t instIdx = block.start; instIdx <= block.finish; instIdx++)
{
IrInst& inst = function.instructions[instIdx];
liveOuts += inst.useCount;
checkOp(inst.a);
checkOp(inst.b);
checkOp(inst.c);
checkOp(inst.d);
checkOp(inst.e);
checkOp(inst.f);
}
return std::make_pair(liveIns, liveOuts);
}
uint32_t getLiveInValueCount(IrFunction& function, IrBlock& block)
{
return getLiveInOutValueCount(function, block).first;
}
uint32_t getLiveOutValueCount(IrFunction& function, IrBlock& block)
{
return getLiveInOutValueCount(function, block).second;
}
2023-01-27 21:28:45 +00:00
} // namespace CodeGen
} // namespace Luau