mirror of
https://git.suyu.dev/suyu/dynarmic.git
synced 2025-12-25 16:54:40 +01:00
RegAlloc: Allow allocation of XMM registers
This commit is contained in:
parent
864081d1a0
commit
4414ec5bc8
5 changed files with 218 additions and 131 deletions
|
|
@ -21,13 +21,14 @@ static Gen::X64Reg HostLocToX64(HostLoc loc) {
|
|||
}
|
||||
|
||||
static Gen::OpArg SpillToOpArg(HostLoc loc) {
|
||||
static_assert(std::is_same<decltype(JitState{}.Spill[0]), u64&>::value, "Spill must be u64");
|
||||
DEBUG_ASSERT(HostLocIsSpill(loc));
|
||||
|
||||
size_t i = static_cast<size_t>(loc) - static_cast<size_t>(HostLoc::FirstSpill);
|
||||
return Gen::MDisp(Gen::R15, static_cast<int>(offsetof(JitState, Spill) + i * sizeof(u32)));
|
||||
return Gen::MDisp(Gen::R15, static_cast<int>(offsetof(JitState, Spill) + i * sizeof(u64)));
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::DefRegister(IR::Inst* def_inst, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::DefRegister(IR::Inst* def_inst, HostLocList desired_locations) {
|
||||
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
||||
DEBUG_ASSERT_MSG(ValueLocations(def_inst).empty(), "def_inst has already been defined");
|
||||
|
||||
|
|
@ -38,13 +39,11 @@ Gen::X64Reg RegAlloc::DefRegister(IR::Inst* def_inst, std::initializer_list<Host
|
|||
}
|
||||
|
||||
// Update state
|
||||
hostloc_state[static_cast<size_t>(location)] = HostLocState::Def;
|
||||
hostloc_to_inst[static_cast<size_t>(location)] = def_inst;
|
||||
|
||||
LocInfo(location) = {def_inst, HostLocState::Def};
|
||||
return HostLocToX64(location);
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::UseDefRegister(IR::Value use_value, IR::Inst* def_inst, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::UseDefRegister(IR::Value use_value, IR::Inst* def_inst, HostLocList desired_locations) {
|
||||
if (!use_value.IsImmediate()) {
|
||||
return UseDefRegister(use_value.GetInst(), def_inst, desired_locations);
|
||||
}
|
||||
|
|
@ -52,19 +51,19 @@ Gen::X64Reg RegAlloc::UseDefRegister(IR::Value use_value, IR::Inst* def_inst, st
|
|||
return LoadImmediateIntoRegister(use_value, DefRegister(def_inst, desired_locations));
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::UseDefRegister(IR::Inst* use_inst, IR::Inst* def_inst, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::UseDefRegister(IR::Inst* use_inst, IR::Inst* def_inst, HostLocList desired_locations) {
|
||||
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
||||
DEBUG_ASSERT_MSG(ValueLocations(def_inst).empty(), "def_inst has already been defined");
|
||||
DEBUG_ASSERT_MSG(!ValueLocations(use_inst).empty(), "use_inst has not been defined");
|
||||
|
||||
// TODO: Optimize the case when this is the last use_inst use.
|
||||
Gen::X64Reg use_reg = UseRegister(use_inst);
|
||||
Gen::X64Reg use_reg = UseRegister(use_inst, any_gpr);
|
||||
Gen::X64Reg def_reg = DefRegister(def_inst, desired_locations);
|
||||
code->MOV(32, Gen::R(def_reg), Gen::R(use_reg));
|
||||
code->MOV(64, Gen::R(def_reg), Gen::R(use_reg));
|
||||
return def_reg;
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::UseRegister(IR::Value use_value, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::UseRegister(IR::Value use_value, HostLocList desired_locations) {
|
||||
if (!use_value.IsImmediate()) {
|
||||
return UseRegister(use_value.GetInst(), desired_locations);
|
||||
}
|
||||
|
|
@ -72,17 +71,18 @@ Gen::X64Reg RegAlloc::UseRegister(IR::Value use_value, std::initializer_list<Hos
|
|||
return LoadImmediateIntoRegister(use_value, ScratchRegister(desired_locations));
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::UseRegister(IR::Inst* use_inst, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::UseRegister(IR::Inst* use_inst, HostLocList desired_locations) {
|
||||
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
||||
DEBUG_ASSERT_MSG(!ValueLocations(use_inst).empty(), "use_inst has not been defined");
|
||||
|
||||
HostLoc current_location = ValueLocations(use_inst).front();
|
||||
auto iter = std::find(desired_locations.begin(), desired_locations.end(), current_location);
|
||||
if (iter != desired_locations.end()) {
|
||||
ASSERT(hostloc_state[static_cast<size_t>(current_location)] == HostLocState::Idle || hostloc_state[static_cast<size_t>(current_location)] == HostLocState::Use);
|
||||
ASSERT(LocInfo(current_location).state == HostLocState::Idle ||
|
||||
LocInfo(current_location).state == HostLocState::Use);
|
||||
|
||||
// Update state
|
||||
hostloc_state[static_cast<size_t>(current_location)] = HostLocState::Use;
|
||||
LocInfo(current_location).state = HostLocState::Use;
|
||||
DecrementRemainingUses(use_inst);
|
||||
|
||||
return HostLocToX64(current_location);
|
||||
|
|
@ -95,18 +95,19 @@ Gen::X64Reg RegAlloc::UseRegister(IR::Inst* use_inst, std::initializer_list<Host
|
|||
SpillRegister(new_location);
|
||||
}
|
||||
|
||||
code->MOV(32, Gen::R(HostLocToX64(new_location)), SpillToOpArg(current_location));
|
||||
EmitMove(new_location, current_location);
|
||||
|
||||
hostloc_state[static_cast<size_t>(new_location)] = HostLocState::Use;
|
||||
std::swap(hostloc_to_inst[static_cast<size_t>(new_location)], hostloc_to_inst[static_cast<size_t>(current_location)]);
|
||||
LocInfo(new_location) = LocInfo(current_location);
|
||||
LocInfo(new_location).state = HostLocState::Use;
|
||||
LocInfo(current_location) = {};
|
||||
DecrementRemainingUses(use_inst);
|
||||
} else if (HostLocIsRegister(current_location)) {
|
||||
ASSERT(hostloc_state[static_cast<size_t>(current_location)] == HostLocState::Idle);
|
||||
ASSERT(LocInfo(current_location).state == HostLocState::Idle);
|
||||
|
||||
code->XCHG(32, Gen::R(HostLocToX64(new_location)), Gen::R(HostLocToX64(current_location)));
|
||||
EmitExchange(new_location, current_location);
|
||||
|
||||
hostloc_state[static_cast<size_t>(new_location)] = HostLocState::Use;
|
||||
std::swap(hostloc_to_inst[static_cast<size_t>(new_location)], hostloc_to_inst[static_cast<size_t>(current_location)]);
|
||||
std::swap(LocInfo(new_location), LocInfo(current_location));
|
||||
LocInfo(new_location).state = HostLocState::Use;
|
||||
DecrementRemainingUses(use_inst);
|
||||
} else {
|
||||
ASSERT_MSG(0, "Invalid current_location");
|
||||
|
|
@ -115,7 +116,7 @@ Gen::X64Reg RegAlloc::UseRegister(IR::Inst* use_inst, std::initializer_list<Host
|
|||
return HostLocToX64(new_location);
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::UseScratchRegister(IR::Value use_value, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::UseScratchRegister(IR::Value use_value, HostLocList desired_locations) {
|
||||
if (!use_value.IsImmediate()) {
|
||||
return UseScratchRegister(use_value.GetInst(), desired_locations);
|
||||
}
|
||||
|
|
@ -123,7 +124,7 @@ Gen::X64Reg RegAlloc::UseScratchRegister(IR::Value use_value, std::initializer_l
|
|||
return LoadImmediateIntoRegister(use_value, ScratchRegister(desired_locations));
|
||||
}
|
||||
|
||||
Gen::X64Reg RegAlloc::UseScratchRegister(IR::Inst* use_inst, std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::UseScratchRegister(IR::Inst* use_inst, HostLocList desired_locations) {
|
||||
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
||||
DEBUG_ASSERT_MSG(!ValueLocations(use_inst).empty(), "use_inst has not been defined");
|
||||
ASSERT_MSG(use_inst->use_count != 0, "use_inst ran out of uses. (Use-d an IR::Inst* too many times)");
|
||||
|
|
@ -136,23 +137,23 @@ Gen::X64Reg RegAlloc::UseScratchRegister(IR::Inst* use_inst, std::initializer_li
|
|||
SpillRegister(new_location);
|
||||
}
|
||||
|
||||
code->MOV(32, Gen::R(HostLocToX64(new_location)), SpillToOpArg(current_location));
|
||||
EmitMove(new_location, current_location);
|
||||
|
||||
hostloc_state[static_cast<size_t>(new_location)] = HostLocState::Scratch;
|
||||
LocInfo(new_location).state = HostLocState::Scratch;
|
||||
DecrementRemainingUses(use_inst);
|
||||
} else if (HostLocIsRegister(current_location)) {
|
||||
ASSERT(hostloc_state[static_cast<size_t>(current_location)] == HostLocState::Idle);
|
||||
ASSERT(LocInfo(current_location).state == HostLocState::Idle);
|
||||
|
||||
if (IsRegisterOccupied(new_location)) {
|
||||
SpillRegister(new_location);
|
||||
if (current_location != new_location) {
|
||||
code->MOV(32, Gen::R(HostLocToX64(new_location)), Gen::R(HostLocToX64(current_location)));
|
||||
EmitMove(new_location, current_location);
|
||||
}
|
||||
} else {
|
||||
code->MOV(32, Gen::R(HostLocToX64(new_location)), Gen::R(HostLocToX64(current_location)));
|
||||
EmitMove(new_location, current_location);
|
||||
}
|
||||
|
||||
hostloc_state[static_cast<size_t>(new_location)] = HostLocState::Scratch;
|
||||
LocInfo(new_location).state = HostLocState::Scratch;
|
||||
DecrementRemainingUses(use_inst);
|
||||
} else {
|
||||
ASSERT_MSG(0, "Invalid current_location");
|
||||
|
|
@ -161,8 +162,7 @@ Gen::X64Reg RegAlloc::UseScratchRegister(IR::Inst* use_inst, std::initializer_li
|
|||
return HostLocToX64(new_location);
|
||||
}
|
||||
|
||||
|
||||
Gen::X64Reg RegAlloc::ScratchRegister(std::initializer_list<HostLoc> desired_locations) {
|
||||
Gen::X64Reg RegAlloc::ScratchRegister(HostLocList desired_locations) {
|
||||
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
||||
|
||||
HostLoc location = SelectARegister(desired_locations);
|
||||
|
|
@ -172,7 +172,7 @@ Gen::X64Reg RegAlloc::ScratchRegister(std::initializer_list<HostLoc> desired_loc
|
|||
}
|
||||
|
||||
// Update state
|
||||
hostloc_state[static_cast<size_t>(location)] = HostLocState::Scratch;
|
||||
LocInfo(location).state = HostLocState::Scratch;
|
||||
|
||||
return HostLocToX64(location);
|
||||
}
|
||||
|
|
@ -235,7 +235,7 @@ void RegAlloc::HostCall(IR::Inst* result_def, IR::Value arg0_use, IR::Value arg1
|
|||
code->MOV(64, Gen::R(Gen::RSP), Gen::MDisp(Gen::R15, offsetof(JitState, save_host_RSP)));
|
||||
}
|
||||
|
||||
HostLoc RegAlloc::SelectARegister(std::initializer_list<HostLoc> desired_locations) const {
|
||||
HostLoc RegAlloc::SelectARegister(HostLocList desired_locations) const {
|
||||
std::vector<HostLoc> candidates = desired_locations;
|
||||
|
||||
// Find all locations that have not been allocated..
|
||||
|
|
@ -259,32 +259,32 @@ std::vector<HostLoc> RegAlloc::ValueLocations(IR::Inst* value) const {
|
|||
std::vector<HostLoc> locations;
|
||||
|
||||
for (size_t i = 0; i < HostLocCount; i++)
|
||||
if (hostloc_to_inst[i] == value)
|
||||
if (hostloc_info[i].value == value)
|
||||
locations.emplace_back(static_cast<HostLoc>(i));
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
bool RegAlloc::IsRegisterOccupied(HostLoc loc) const {
|
||||
return hostloc_to_inst.at(static_cast<size_t>(loc)) != nullptr;
|
||||
return GetLocInfo(loc).value != nullptr;
|
||||
}
|
||||
|
||||
bool RegAlloc::IsRegisterAllocated(HostLoc loc) const {
|
||||
return hostloc_state.at(static_cast<size_t>(loc)) != HostLocState::Idle;
|
||||
return GetLocInfo(loc).state != HostLocState::Idle;
|
||||
}
|
||||
|
||||
void RegAlloc::SpillRegister(HostLoc loc) {
|
||||
ASSERT_MSG(HostLocIsRegister(loc), "Only registers can be spilled");
|
||||
ASSERT_MSG(hostloc_state[static_cast<size_t>(loc)] == HostLocState::Idle, "Allocated registers cannot be spilled");
|
||||
ASSERT_MSG(LocInfo(loc).state == HostLocState::Idle, "Allocated registers cannot be spilled");
|
||||
ASSERT_MSG(IsRegisterOccupied(loc), "There is no need to spill unoccupied registers");
|
||||
ASSERT_MSG(!IsRegisterAllocated(loc), "Registers that have been allocated must not be spilt");
|
||||
|
||||
HostLoc new_loc = FindFreeSpill();
|
||||
|
||||
code->MOV(32, SpillToOpArg(new_loc), Gen::R(HostLocToX64(loc)));
|
||||
EmitMove(new_loc, loc);
|
||||
|
||||
hostloc_to_inst[static_cast<size_t>(new_loc)] = hostloc_to_inst[static_cast<size_t>(loc)];
|
||||
hostloc_to_inst[static_cast<size_t>(loc)] = nullptr;
|
||||
LocInfo(new_loc).value = LocInfo(loc).value;
|
||||
LocInfo(loc).value = nullptr;
|
||||
}
|
||||
|
||||
HostLoc RegAlloc::FindFreeSpill() const {
|
||||
|
|
@ -296,11 +296,11 @@ HostLoc RegAlloc::FindFreeSpill() const {
|
|||
}
|
||||
|
||||
void RegAlloc::EndOfAllocScope() {
|
||||
hostloc_state.fill(HostLocState::Idle);
|
||||
|
||||
for (auto& iter : hostloc_to_inst)
|
||||
if (iter && iter->use_count == 0)
|
||||
iter = nullptr;
|
||||
for (auto& iter : hostloc_info) {
|
||||
iter.state = HostLocState::Idle;
|
||||
if (iter.value && iter.value->use_count == 0)
|
||||
iter.value = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void RegAlloc::DecrementRemainingUses(IR::Inst* value) {
|
||||
|
|
@ -309,12 +309,53 @@ void RegAlloc::DecrementRemainingUses(IR::Inst* value) {
|
|||
}
|
||||
|
||||
void RegAlloc::AssertNoMoreUses() {
|
||||
ASSERT(std::all_of(hostloc_to_inst.begin(), hostloc_to_inst.end(), [](const auto& inst){ return !inst; }));
|
||||
ASSERT(std::all_of(hostloc_info.begin(), hostloc_info.end(), [](const auto& i){ return !i.value; }));
|
||||
}
|
||||
|
||||
void RegAlloc::Reset() {
|
||||
hostloc_to_inst.fill(nullptr);
|
||||
hostloc_state.fill(HostLocState::Idle);
|
||||
hostloc_info.fill({});
|
||||
}
|
||||
|
||||
void RegAlloc::EmitMove(HostLoc to, HostLoc from) {
|
||||
const auto& from_info = LocInfo(from);
|
||||
|
||||
if (HostLocIsXMM(to) && HostLocIsSpill(from)) {
|
||||
if (from_info.GetType() == IR::Type::F64) {
|
||||
code->MOVSD(HostLocToX64(to), SpillToOpArg(from));
|
||||
} else if (from_info.GetType() == IR::Type::F32) {
|
||||
code->MOVSS(HostLocToX64(to), SpillToOpArg(from));
|
||||
} else {
|
||||
ASSERT_MSG(false, "Tried to move a non-fp value into an XMM register");
|
||||
}
|
||||
} else if (HostLocIsSpill(to) && HostLocIsXMM(from)) {
|
||||
if (from_info.GetType() == IR::Type::F64) {
|
||||
code->MOVSD(SpillToOpArg(to), HostLocToX64(from));
|
||||
} else if (from_info.GetType() == IR::Type::F32) {
|
||||
code->MOVSS(SpillToOpArg(to), HostLocToX64(from));
|
||||
} else {
|
||||
ASSERT_MSG(false, "Tried to move a non-fp value into an XMM register");
|
||||
}
|
||||
} else if (HostLocIsXMM(to) && HostLocIsXMM(from)) {
|
||||
code->MOVAPS(HostLocToX64(to), Gen::R(HostLocToX64(from)));
|
||||
} else if (HostLocIsGPR(to) && HostLocIsSpill(from)) {
|
||||
code->MOV(64, Gen::R(HostLocToX64(to)), SpillToOpArg(from));
|
||||
} else if (HostLocIsSpill(to) && HostLocIsGPR(from)) {
|
||||
code->MOV(64, SpillToOpArg(to), Gen::R(HostLocToX64(from)));
|
||||
} else if (HostLocIsGPR(to) && HostLocIsGPR(from)){
|
||||
code->MOV(64, Gen::R(HostLocToX64(to)), Gen::R(HostLocToX64(from)));
|
||||
} else {
|
||||
ASSERT_MSG(false, "Invalid RegAlloc::EmitMove");
|
||||
}
|
||||
}
|
||||
|
||||
void RegAlloc::EmitExchange(HostLoc a, HostLoc b) {
|
||||
if (HostLocIsGPR(a) && HostLocIsGPR(b)) {
|
||||
code->XCHG(64, Gen::R(HostLocToX64(a)), Gen::R(HostLocToX64(b)));
|
||||
} else if (HostLocIsXMM(a) && HostLocIsXMM(b)) {
|
||||
ASSERT_MSG(false, "Exchange is unnecessary for XMM registers");
|
||||
} else {
|
||||
ASSERT_MSG(false, "Invalid RegAlloc::EmitExchange");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BackendX64
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue