luau/CodeGen/src/IrAnalysis.cpp

78 lines
1.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-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
}
}
} // namespace CodeGen
} // namespace Luau