luau/CodeGen/include/Luau/IrRegAllocX64.h

130 lines
3.5 KiB
C
Raw Normal View History

2023-02-24 18:24:22 +00:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
2023-04-07 20:56:27 +01:00
#include "Luau/AssemblyBuilderX64.h"
2023-02-24 18:24:22 +00:00
#include "Luau/IrData.h"
#include "Luau/RegisterX64.h"
#include <array>
#include <initializer_list>
namespace Luau
{
namespace CodeGen
{
2023-09-15 17:27:45 +01:00
struct LoweringStats;
2023-03-03 13:45:38 +00:00
namespace X64
{
2023-02-24 18:24:22 +00:00
2023-04-07 20:56:27 +01:00
constexpr uint8_t kNoStackSlot = 0xff;
struct IrSpillX64
{
uint32_t instIdx = 0;
2023-04-14 13:05:27 +01:00
IrValueKind valueKind = IrValueKind::Unknown;
unsigned spillId = 0;
2023-04-07 20:56:27 +01:00
// Spill location can be a stack location or be empty
// When it's empty, it means that instruction value can be rematerialized
uint8_t stackSlot = kNoStackSlot;
RegisterX64 originalLoc = noreg;
};
2023-02-24 18:24:22 +00:00
struct IrRegAllocX64
{
2023-09-15 17:27:45 +01:00
IrRegAllocX64(AssemblyBuilderX64& build, IrFunction& function, LoweringStats* stats);
2023-02-24 18:24:22 +00:00
2023-04-14 13:05:27 +01:00
RegisterX64 allocReg(SizeX64 size, uint32_t instIdx);
RegisterX64 allocRegOrReuse(SizeX64 size, uint32_t instIdx, std::initializer_list<IrOp> oprefs);
2023-04-07 20:56:27 +01:00
RegisterX64 takeReg(RegisterX64 reg, uint32_t instIdx);
2023-03-03 13:45:38 +00:00
2023-07-07 18:14:35 +01:00
bool canTakeReg(RegisterX64 reg) const;
2023-02-24 18:24:22 +00:00
void freeReg(RegisterX64 reg);
2023-04-07 20:56:27 +01:00
void freeLastUseReg(IrInst& target, uint32_t instIdx);
void freeLastUseRegs(const IrInst& inst, uint32_t instIdx);
2023-02-24 18:24:22 +00:00
2023-04-07 20:56:27 +01:00
bool isLastUseReg(const IrInst& target, uint32_t instIdx) const;
2023-03-31 13:21:14 +01:00
bool shouldFreeGpr(RegisterX64 reg) const;
2023-04-14 13:05:27 +01:00
unsigned findSpillStackSlot(IrValueKind valueKind);
IrOp getRestoreOp(const IrInst& inst) const;
bool hasRestoreOp(const IrInst& inst) const;
OperandX64 getRestoreAddress(const IrInst& inst, IrOp restoreOp);
2023-04-07 20:56:27 +01:00
// Register used by instruction is about to be freed, have to find a way to restore value later
void preserve(IrInst& inst);
void restore(IrInst& inst, bool intoOriginalLocation);
void preserveAndFreeInstValues();
uint32_t findInstructionWithFurthestNextUse(const std::array<uint32_t, 16>& regInstUsers) const;
2023-03-31 13:21:14 +01:00
void assertFree(RegisterX64 reg) const;
2023-03-03 13:45:38 +00:00
void assertAllFree() const;
2023-04-07 20:56:27 +01:00
void assertNoSpills() const;
2023-03-03 13:45:38 +00:00
2023-04-07 20:56:27 +01:00
AssemblyBuilderX64& build;
2023-02-24 18:24:22 +00:00
IrFunction& function;
2023-09-15 17:27:45 +01:00
LoweringStats* stats = nullptr;
2023-02-24 18:24:22 +00:00
2023-04-07 20:56:27 +01:00
uint32_t currInstIdx = ~0u;
2023-02-24 18:24:22 +00:00
std::array<bool, 16> freeGprMap;
2023-04-07 20:56:27 +01:00
std::array<uint32_t, 16> gprInstUsers;
2023-02-24 18:24:22 +00:00
std::array<bool, 16> freeXmmMap;
2023-04-07 20:56:27 +01:00
std::array<uint32_t, 16> xmmInstUsers;
2023-08-25 16:25:09 +01:00
uint8_t usableXmmRegCount = 0;
2023-04-07 20:56:27 +01:00
std::bitset<256> usedSpillSlots;
unsigned maxUsedSlot = 0;
2023-04-14 13:05:27 +01:00
unsigned nextSpillId = 1;
2023-04-07 20:56:27 +01:00
std::vector<IrSpillX64> spills;
2023-02-24 18:24:22 +00:00
};
struct ScopedRegX64
{
2023-03-10 19:20:04 +00:00
explicit ScopedRegX64(IrRegAllocX64& owner);
2023-02-24 18:24:22 +00:00
ScopedRegX64(IrRegAllocX64& owner, SizeX64 size);
ScopedRegX64(IrRegAllocX64& owner, RegisterX64 reg);
~ScopedRegX64();
ScopedRegX64(const ScopedRegX64&) = delete;
ScopedRegX64& operator=(const ScopedRegX64&) = delete;
2023-09-22 19:10:49 +01:00
void take(RegisterX64 reg);
2023-03-10 19:20:04 +00:00
void alloc(SizeX64 size);
2023-02-24 18:24:22 +00:00
void free();
2023-03-31 13:21:14 +01:00
RegisterX64 release();
2023-02-24 18:24:22 +00:00
IrRegAllocX64& owner;
RegisterX64 reg;
};
2023-04-07 20:56:27 +01:00
// When IR instruction makes a call under a condition that's not reflected as a real branch in IR,
// spilled values have to be restored to their exact original locations, so that both after a call
// and after the skip, values are found in the same place
struct ScopedSpills
{
explicit ScopedSpills(IrRegAllocX64& owner);
~ScopedSpills();
ScopedSpills(const ScopedSpills&) = delete;
ScopedSpills& operator=(const ScopedSpills&) = delete;
IrRegAllocX64& owner;
2023-04-14 13:05:27 +01:00
unsigned startSpillId = 0;
2023-04-07 20:56:27 +01:00
};
2023-03-03 13:45:38 +00:00
} // namespace X64
2023-02-24 18:24:22 +00:00
} // namespace CodeGen
} // namespace Luau