2023-02-25 05:49:38 +08: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-08 05:01:29 +08:00
|
|
|
#include "Luau/AssemblyBuilderX64.h"
|
2023-02-25 05:49:38 +08:00
|
|
|
#include "Luau/IrData.h"
|
|
|
|
#include "Luau/RegisterX64.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <initializer_list>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
2023-09-16 01:26:59 +08:00
|
|
|
|
|
|
|
struct LoweringStats;
|
|
|
|
|
2023-03-04 04:21:14 +08:00
|
|
|
namespace X64
|
|
|
|
{
|
2023-02-25 05:49:38 +08:00
|
|
|
|
2023-04-08 05:01:29 +08:00
|
|
|
constexpr uint8_t kNoStackSlot = 0xff;
|
|
|
|
|
|
|
|
struct IrSpillX64
|
|
|
|
{
|
|
|
|
uint32_t instIdx = 0;
|
2023-04-15 02:06:22 +08:00
|
|
|
IrValueKind valueKind = IrValueKind::Unknown;
|
|
|
|
|
|
|
|
unsigned spillId = 0;
|
2023-04-08 05:01:29 +08: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-25 05:49:38 +08:00
|
|
|
struct IrRegAllocX64
|
|
|
|
{
|
2023-09-16 01:26:59 +08:00
|
|
|
IrRegAllocX64(AssemblyBuilderX64& build, IrFunction& function, LoweringStats* stats);
|
2023-02-25 05:49:38 +08:00
|
|
|
|
2023-04-15 02:06:22 +08:00
|
|
|
RegisterX64 allocReg(SizeX64 size, uint32_t instIdx);
|
|
|
|
RegisterX64 allocRegOrReuse(SizeX64 size, uint32_t instIdx, std::initializer_list<IrOp> oprefs);
|
2023-04-08 05:01:29 +08:00
|
|
|
RegisterX64 takeReg(RegisterX64 reg, uint32_t instIdx);
|
2023-03-04 04:21:14 +08:00
|
|
|
|
2023-07-08 04:10:48 +08:00
|
|
|
bool canTakeReg(RegisterX64 reg) const;
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
void freeReg(RegisterX64 reg);
|
2023-04-08 05:01:29 +08:00
|
|
|
void freeLastUseReg(IrInst& target, uint32_t instIdx);
|
|
|
|
void freeLastUseRegs(const IrInst& inst, uint32_t instIdx);
|
2023-02-25 05:49:38 +08:00
|
|
|
|
2023-04-08 05:01:29 +08:00
|
|
|
bool isLastUseReg(const IrInst& target, uint32_t instIdx) const;
|
2023-04-01 02:42:49 +08:00
|
|
|
|
|
|
|
bool shouldFreeGpr(RegisterX64 reg) const;
|
|
|
|
|
2023-04-15 02:06:22 +08: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-08 05:01:29 +08: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-04-01 02:42:49 +08:00
|
|
|
void assertFree(RegisterX64 reg) const;
|
2023-03-04 04:21:14 +08:00
|
|
|
void assertAllFree() const;
|
2023-04-08 05:01:29 +08:00
|
|
|
void assertNoSpills() const;
|
2023-03-04 04:21:14 +08:00
|
|
|
|
2023-04-08 05:01:29 +08:00
|
|
|
AssemblyBuilderX64& build;
|
2023-02-25 05:49:38 +08:00
|
|
|
IrFunction& function;
|
2023-09-16 01:26:59 +08:00
|
|
|
LoweringStats* stats = nullptr;
|
2023-02-25 05:49:38 +08:00
|
|
|
|
2023-04-08 05:01:29 +08:00
|
|
|
uint32_t currInstIdx = ~0u;
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
std::array<bool, 16> freeGprMap;
|
2023-04-08 05:01:29 +08:00
|
|
|
std::array<uint32_t, 16> gprInstUsers;
|
2023-02-25 05:49:38 +08:00
|
|
|
std::array<bool, 16> freeXmmMap;
|
2023-04-08 05:01:29 +08:00
|
|
|
std::array<uint32_t, 16> xmmInstUsers;
|
2023-08-26 01:23:55 +08:00
|
|
|
uint8_t usableXmmRegCount = 0;
|
2023-04-08 05:01:29 +08:00
|
|
|
|
|
|
|
std::bitset<256> usedSpillSlots;
|
|
|
|
unsigned maxUsedSlot = 0;
|
2023-04-15 02:06:22 +08:00
|
|
|
unsigned nextSpillId = 1;
|
2023-04-08 05:01:29 +08:00
|
|
|
std::vector<IrSpillX64> spills;
|
2023-02-25 05:49:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ScopedRegX64
|
|
|
|
{
|
2023-03-11 04:21:07 +08:00
|
|
|
explicit ScopedRegX64(IrRegAllocX64& owner);
|
2023-02-25 05:49:38 +08:00
|
|
|
ScopedRegX64(IrRegAllocX64& owner, SizeX64 size);
|
|
|
|
ScopedRegX64(IrRegAllocX64& owner, RegisterX64 reg);
|
|
|
|
~ScopedRegX64();
|
|
|
|
|
|
|
|
ScopedRegX64(const ScopedRegX64&) = delete;
|
|
|
|
ScopedRegX64& operator=(const ScopedRegX64&) = delete;
|
|
|
|
|
2023-09-23 03:12:15 +08:00
|
|
|
void take(RegisterX64 reg);
|
2023-03-11 04:21:07 +08:00
|
|
|
void alloc(SizeX64 size);
|
2023-02-25 05:49:38 +08:00
|
|
|
void free();
|
|
|
|
|
2023-04-01 02:42:49 +08:00
|
|
|
RegisterX64 release();
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
IrRegAllocX64& owner;
|
|
|
|
RegisterX64 reg;
|
|
|
|
};
|
|
|
|
|
2023-04-08 05:01:29 +08: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-15 02:06:22 +08:00
|
|
|
unsigned startSpillId = 0;
|
2023-04-08 05:01:29 +08:00
|
|
|
};
|
|
|
|
|
2023-03-04 04:21:14 +08:00
|
|
|
} // namespace X64
|
2023-02-25 05:49:38 +08:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|