2016-07-01 14:01:06 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
#include <xbyak.h>
|
|
|
|
|
2016-08-26 18:43:50 +01:00
|
|
|
#include "backend_x64/abi.h"
|
2016-08-24 20:07:08 +01:00
|
|
|
#include "backend_x64/jitstate.h"
|
2016-07-01 14:01:06 +01:00
|
|
|
#include "backend_x64/reg_alloc.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace BackendX64 {
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
static u32 ImmediateToU32(const IR::Value& imm) {
|
2016-08-05 14:10:39 +01:00
|
|
|
switch (imm.GetType()) {
|
2016-08-22 23:40:30 +01:00
|
|
|
case IR::Type::U1:
|
2016-08-24 20:07:08 +01:00
|
|
|
return u32(imm.GetU1());
|
2016-08-22 23:40:30 +01:00
|
|
|
break;
|
|
|
|
case IR::Type::U8:
|
2016-08-24 20:07:08 +01:00
|
|
|
return u32(imm.GetU8());
|
2016-08-22 23:40:30 +01:00
|
|
|
break;
|
|
|
|
case IR::Type::U32:
|
2016-08-24 20:07:08 +01:00
|
|
|
return u32(imm.GetU32());
|
2016-08-22 23:40:30 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_MSG(false, "This should never happen.");
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
static Xbyak::Reg HostLocToX64(HostLoc hostloc) {
|
|
|
|
if (HostLocIsGPR(hostloc)) {
|
|
|
|
return HostLocToReg64(hostloc);
|
2016-08-06 17:11:22 +01:00
|
|
|
}
|
2016-08-24 20:07:08 +01:00
|
|
|
if (HostLocIsXMM(hostloc)) {
|
|
|
|
return HostLocToXmm(hostloc);
|
2016-08-06 17:11:22 +01:00
|
|
|
}
|
|
|
|
ASSERT_MSG(false, "This should never happen.");
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::DefHostLocReg(IR::Inst* def_inst, HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
2016-08-05 18:40:28 +01:00
|
|
|
DEBUG_ASSERT_MSG(!ValueLocation(def_inst), "def_inst has already been defined");
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
HostLoc location = SelectARegister(desired_locations);
|
|
|
|
|
|
|
|
if (IsRegisterOccupied(location)) {
|
|
|
|
SpillRegister(location);
|
|
|
|
}
|
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
LocInfo(location).is_being_used = true;
|
|
|
|
LocInfo(location).def = def_inst;
|
|
|
|
|
|
|
|
DEBUG_ASSERT(LocInfo(location).IsDef());
|
2016-08-24 20:07:08 +01:00
|
|
|
return location;
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
void RegAlloc::RegisterAddDef(IR::Inst* def_inst, const IR::Value& use_inst) {
|
2016-08-05 18:40:28 +01:00
|
|
|
DEBUG_ASSERT_MSG(!ValueLocation(def_inst), "def_inst has already been defined");
|
2016-08-05 14:10:39 +01:00
|
|
|
|
|
|
|
if (use_inst.IsImmediate()) {
|
2016-08-24 20:07:08 +01:00
|
|
|
LoadImmediateIntoHostLocReg(use_inst, DefHostLocReg(def_inst, any_gpr));
|
2016-08-05 14:10:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:40:28 +01:00
|
|
|
DEBUG_ASSERT_MSG(ValueLocation(use_inst.GetInst()), "use_inst must already be defined");
|
2016-08-05 14:10:39 +01:00
|
|
|
HostLoc location = *ValueLocation(use_inst.GetInst());
|
|
|
|
LocInfo(location).values.emplace_back(def_inst);
|
|
|
|
DecrementRemainingUses(use_inst.GetInst());
|
|
|
|
DEBUG_ASSERT(LocInfo(location).IsIdle());
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::UseDefHostLocReg(IR::Value use_value, IR::Inst* def_inst, HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
if (!use_value.IsImmediate()) {
|
2016-08-24 20:07:08 +01:00
|
|
|
return UseDefHostLocReg(use_value.GetInst(), def_inst, desired_locations);
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
return LoadImmediateIntoHostLocReg(use_value, DefHostLocReg(def_inst, desired_locations));
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::UseDefHostLocReg(IR::Inst* use_inst, IR::Inst* def_inst, HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
2016-08-05 18:40:28 +01:00
|
|
|
DEBUG_ASSERT_MSG(!ValueLocation(def_inst), "def_inst has already been defined");
|
|
|
|
DEBUG_ASSERT_MSG(ValueLocation(use_inst), "use_inst has not been defined");
|
2016-08-05 14:10:39 +01:00
|
|
|
|
|
|
|
if (IsLastUse(use_inst)) {
|
|
|
|
HostLoc current_location = *ValueLocation(use_inst);
|
|
|
|
auto& loc_info = LocInfo(current_location);
|
2016-08-05 15:24:25 +01:00
|
|
|
if (loc_info.IsIdle()) {
|
2016-08-05 14:10:39 +01:00
|
|
|
loc_info.is_being_used = true;
|
|
|
|
loc_info.def = def_inst;
|
|
|
|
DEBUG_ASSERT(loc_info.IsUseDef());
|
2016-08-05 18:40:28 +01:00
|
|
|
if (HostLocIsSpill(current_location)) {
|
|
|
|
HostLoc new_location = SelectARegister(desired_locations);
|
|
|
|
if (IsRegisterOccupied(new_location)) {
|
|
|
|
SpillRegister(new_location);
|
|
|
|
}
|
|
|
|
EmitMove(new_location, current_location);
|
|
|
|
LocInfo(new_location) = LocInfo(current_location);
|
|
|
|
LocInfo(current_location) = {};
|
2016-08-24 20:07:08 +01:00
|
|
|
return new_location;
|
2016-08-05 18:40:28 +01:00
|
|
|
} else {
|
2016-08-24 20:07:08 +01:00
|
|
|
return current_location;
|
2016-08-05 18:40:28 +01:00
|
|
|
}
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-23 14:51:46 +01:00
|
|
|
bool is_floating_point = HostLocIsXMM(*desired_locations.begin());
|
2016-08-24 01:31:34 +01:00
|
|
|
if (is_floating_point) {
|
2016-08-23 14:51:46 +01:00
|
|
|
DEBUG_ASSERT(use_inst->GetType() == IR::Type::F32 || use_inst->GetType() == IR::Type::F64);
|
2016-08-24 01:31:34 +01:00
|
|
|
}
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc use_reg = UseHostLocReg(use_inst, is_floating_point ? any_xmm : any_gpr);
|
|
|
|
HostLoc def_reg = DefHostLocReg(def_inst, desired_locations);
|
2016-08-05 18:54:19 +01:00
|
|
|
if (is_floating_point) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->movapd(HostLocToXmm(def_reg), HostLocToXmm(use_reg));
|
2016-08-05 18:54:19 +01:00
|
|
|
} else {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->mov(HostLocToReg64(def_reg), HostLocToReg64(use_reg));
|
2016-08-05 18:54:19 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
return def_reg;
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
std::tuple<OpArg, HostLoc> RegAlloc::UseDefOpArgHostLocReg(IR::Value use_value, IR::Inst* def_inst, HostLocList desired_locations) {
|
2016-08-05 14:10:39 +01:00
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
2016-08-05 18:40:28 +01:00
|
|
|
DEBUG_ASSERT_MSG(!ValueLocation(def_inst), "def_inst has already been defined");
|
|
|
|
DEBUG_ASSERT_MSG(use_value.IsImmediate() || ValueLocation(use_value.GetInst()), "use_inst has not been defined");
|
2016-08-05 14:10:39 +01:00
|
|
|
|
|
|
|
if (!use_value.IsImmediate()) {
|
|
|
|
IR::Inst* use_inst = use_value.GetInst();
|
|
|
|
|
|
|
|
if (IsLastUse(use_inst)) {
|
|
|
|
HostLoc current_location = *ValueLocation(use_inst);
|
|
|
|
auto& loc_info = LocInfo(current_location);
|
|
|
|
if (!loc_info.IsIdle()) {
|
2016-08-05 18:40:28 +01:00
|
|
|
if (HostLocIsSpill(current_location)) {
|
|
|
|
loc_info.is_being_used = true;
|
|
|
|
DEBUG_ASSERT(loc_info.IsUse());
|
2016-08-24 20:07:08 +01:00
|
|
|
return std::make_tuple(SpillToOpArg(current_location), DefHostLocReg(def_inst, desired_locations));
|
2016-08-05 18:40:28 +01:00
|
|
|
} else {
|
|
|
|
loc_info.is_being_used = true;
|
|
|
|
loc_info.def = def_inst;
|
|
|
|
DEBUG_ASSERT(loc_info.IsUseDef());
|
2016-08-24 20:07:08 +01:00
|
|
|
return std::make_tuple(HostLocToX64(current_location), current_location);
|
2016-08-05 18:40:28 +01:00
|
|
|
}
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
OpArg use_oparg = UseOpArg(use_value, any_gpr);
|
|
|
|
HostLoc def_reg = DefHostLocReg(def_inst, desired_locations);
|
2016-08-05 14:10:39 +01:00
|
|
|
return std::make_tuple(use_oparg, def_reg);
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::UseHostLocReg(IR::Value use_value, HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
if (!use_value.IsImmediate()) {
|
2016-08-24 20:07:08 +01:00
|
|
|
return UseHostLocReg(use_value.GetInst(), desired_locations);
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
return LoadImmediateIntoHostLocReg(use_value, ScratchHostLocReg(desired_locations));
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::UseHostLocReg(IR::Inst* use_inst, HostLocList desired_locations) {
|
2016-08-05 14:10:39 +01:00
|
|
|
HostLoc current_location;
|
|
|
|
bool was_being_used;
|
|
|
|
std::tie(current_location, was_being_used) = UseHostLoc(use_inst, desired_locations);
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
if (HostLocIsRegister(current_location)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
return current_location;
|
2016-08-05 14:10:39 +01:00
|
|
|
} else if (HostLocIsSpill(current_location)) {
|
|
|
|
HostLoc new_location = SelectARegister(desired_locations);
|
2016-07-01 14:01:06 +01:00
|
|
|
if (IsRegisterOccupied(new_location)) {
|
|
|
|
SpillRegister(new_location);
|
|
|
|
}
|
2016-08-02 13:46:12 +01:00
|
|
|
EmitMove(new_location, current_location);
|
2016-08-05 14:10:39 +01:00
|
|
|
if (!was_being_used) {
|
|
|
|
LocInfo(new_location) = LocInfo(current_location);
|
|
|
|
LocInfo(current_location) = {};
|
|
|
|
DEBUG_ASSERT(LocInfo(new_location).IsUse());
|
|
|
|
} else {
|
|
|
|
LocInfo(new_location).is_being_used = true;
|
|
|
|
DEBUG_ASSERT(LocInfo(new_location).IsScratch());
|
|
|
|
}
|
2016-08-24 20:07:08 +01:00
|
|
|
return new_location;
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
ASSERT_MSG(false, "Unknown current_location type");
|
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
OpArg RegAlloc::UseOpArg(IR::Value use_value, HostLocList desired_locations) {
|
2016-08-05 14:10:39 +01:00
|
|
|
if (use_value.IsImmediate()) {
|
2016-08-26 15:23:38 +01:00
|
|
|
ASSERT_MSG(false, "UseOpArg does not support immediates");
|
|
|
|
return {}; // return a None
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
IR::Inst* use_inst = use_value.GetInst();
|
|
|
|
|
|
|
|
HostLoc current_location;
|
|
|
|
bool was_being_used;
|
|
|
|
std::tie(current_location, was_being_used) = UseHostLoc(use_inst, desired_locations);
|
|
|
|
|
|
|
|
if (HostLocIsRegister(current_location)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
return HostLocToX64(current_location);
|
2016-08-05 14:10:39 +01:00
|
|
|
} else if (HostLocIsSpill(current_location)) {
|
|
|
|
return SpillToOpArg(current_location);
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
ASSERT_MSG(false, "Unknown current_location type");
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::UseScratchHostLocReg(IR::Value use_value, HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
if (!use_value.IsImmediate()) {
|
2016-08-24 20:07:08 +01:00
|
|
|
return UseScratchHostLocReg(use_value.GetInst(), desired_locations);
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
return LoadImmediateIntoHostLocReg(use_value, ScratchHostLocReg(desired_locations));
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::UseScratchHostLocReg(IR::Inst* use_inst, HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
2016-08-05 14:10:39 +01:00
|
|
|
DEBUG_ASSERT_MSG(ValueLocation(use_inst), "use_inst has not been defined");
|
2016-08-19 03:08:01 +01:00
|
|
|
ASSERT_MSG(use_inst->HasUses(), "use_inst ran out of uses. (Use-d an IR::Inst* too many times)");
|
2016-07-18 15:11:16 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
HostLoc current_location = *ValueLocation(use_inst);
|
2016-07-18 15:11:16 +01:00
|
|
|
HostLoc new_location = SelectARegister(desired_locations);
|
2016-08-05 14:10:39 +01:00
|
|
|
if (IsRegisterOccupied(new_location)) {
|
|
|
|
SpillRegister(new_location);
|
|
|
|
}
|
2016-07-18 15:11:16 +01:00
|
|
|
|
|
|
|
if (HostLocIsSpill(current_location)) {
|
2016-08-02 13:46:12 +01:00
|
|
|
EmitMove(new_location, current_location);
|
2016-08-05 14:10:39 +01:00
|
|
|
LocInfo(new_location).is_being_used = true;
|
2016-07-22 23:55:00 +01:00
|
|
|
DecrementRemainingUses(use_inst);
|
2016-08-05 14:10:39 +01:00
|
|
|
DEBUG_ASSERT(LocInfo(new_location).IsScratch());
|
2016-08-24 20:07:08 +01:00
|
|
|
return new_location;
|
2016-07-18 15:11:16 +01:00
|
|
|
} else if (HostLocIsRegister(current_location)) {
|
2016-08-07 19:25:12 +01:00
|
|
|
ASSERT(LocInfo(current_location).IsIdle()
|
|
|
|
|| LocInfo(current_location).IsUse()
|
|
|
|
|| LocInfo(current_location).IsUseDef());
|
2016-07-18 15:11:16 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
if (current_location != new_location) {
|
2016-08-02 13:46:12 +01:00
|
|
|
EmitMove(new_location, current_location);
|
2016-08-07 19:25:12 +01:00
|
|
|
} else {
|
|
|
|
ASSERT(LocInfo(current_location).IsIdle());
|
2016-07-18 15:11:16 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
LocInfo(new_location).is_being_used = true;
|
|
|
|
LocInfo(new_location).values.clear();
|
2016-07-22 23:55:00 +01:00
|
|
|
DecrementRemainingUses(use_inst);
|
2016-08-05 14:10:39 +01:00
|
|
|
DEBUG_ASSERT(LocInfo(new_location).IsScratch());
|
2016-08-24 20:07:08 +01:00
|
|
|
return new_location;
|
2016-07-18 15:11:16 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
ASSERT_MSG(false, "Invalid current_location");
|
2016-07-18 15:11:16 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::ScratchHostLocReg(HostLocList desired_locations) {
|
2016-07-22 23:55:00 +01:00
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
HostLoc location = SelectARegister(desired_locations);
|
|
|
|
|
|
|
|
if (IsRegisterOccupied(location)) {
|
|
|
|
SpillRegister(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update state
|
2016-08-05 14:10:39 +01:00
|
|
|
LocInfo(location).is_being_used = true;
|
2016-07-22 23:55:00 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
DEBUG_ASSERT(LocInfo(location).IsScratch());
|
2016-08-24 20:07:08 +01:00
|
|
|
return location;
|
2016-07-22 23:55:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::HostCall(IR::Inst* result_def, IR::Value arg0_use, IR::Value arg1_use, IR::Value arg2_use, IR::Value arg3_use) {
|
2016-08-26 18:43:50 +01:00
|
|
|
constexpr size_t args_count = 4;
|
|
|
|
constexpr std::array<HostLoc, args_count> args_hostloc = { ABI_PARAM1, ABI_PARAM2, ABI_PARAM3, ABI_PARAM4 };
|
|
|
|
const std::array<IR::Value*, args_count> args = {&arg0_use, &arg1_use, &arg2_use, &arg3_use};
|
2016-07-11 15:28:10 +01:00
|
|
|
|
2016-08-26 18:43:50 +01:00
|
|
|
const static std::vector<HostLoc> other_caller_save = [](){
|
|
|
|
std::vector<HostLoc> ret(ABI_ALL_CALLER_SAVE.begin(), ABI_ALL_CALLER_SAVE.end());
|
2016-07-11 15:28:10 +01:00
|
|
|
|
2016-08-26 18:43:50 +01:00
|
|
|
for (auto hostloc : {ABI_RETURN, ABI_PARAM1, ABI_PARAM2, ABI_PARAM3, ABI_PARAM4})
|
|
|
|
ret.erase(std::find(ret.begin(), ret.end(), hostloc));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}();
|
|
|
|
|
|
|
|
// TODO: This works but almost certainly leads to suboptimal generated code.
|
2016-07-11 15:28:10 +01:00
|
|
|
|
|
|
|
if (result_def) {
|
2016-08-26 18:43:50 +01:00
|
|
|
DefHostLocReg(result_def, {ABI_RETURN});
|
2016-07-11 15:28:10 +01:00
|
|
|
} else {
|
2016-08-26 18:43:50 +01:00
|
|
|
ScratchHostLocReg({ABI_RETURN});
|
2016-07-11 15:28:10 +01:00
|
|
|
}
|
|
|
|
|
2016-08-26 18:43:50 +01:00
|
|
|
for (size_t i = 0; i < args_count; i++) {
|
2016-07-22 23:55:00 +01:00
|
|
|
if (!args[i]->IsEmpty()) {
|
2016-08-26 18:43:50 +01:00
|
|
|
UseScratchHostLocReg(*args[i], {args_hostloc[i]});
|
2016-07-11 15:28:10 +01:00
|
|
|
} else {
|
2016-08-26 18:43:50 +01:00
|
|
|
ScratchHostLocReg({args_hostloc[i]});
|
2016-07-11 15:28:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 21:48:45 +01:00
|
|
|
|
2016-08-26 18:43:50 +01:00
|
|
|
for (HostLoc caller_saved : other_caller_save) {
|
|
|
|
ScratchHostLocReg({caller_saved});
|
2016-08-07 21:02:16 +01:00
|
|
|
}
|
2016-07-11 15:28:10 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 13:46:12 +01:00
|
|
|
HostLoc RegAlloc::SelectARegister(HostLocList desired_locations) const {
|
2016-07-01 14:01:06 +01:00
|
|
|
std::vector<HostLoc> candidates = desired_locations;
|
|
|
|
|
|
|
|
// Find all locations that have not been allocated..
|
|
|
|
auto allocated_locs = std::partition(candidates.begin(), candidates.end(), [this](auto loc){
|
|
|
|
return !this->IsRegisterAllocated(loc);
|
|
|
|
});
|
|
|
|
candidates.erase(allocated_locs, candidates.end());
|
|
|
|
ASSERT_MSG(!candidates.empty(), "All candidate registers have already been allocated");
|
|
|
|
|
|
|
|
// Selects the best location out of the available locations.
|
|
|
|
// TODO: Actually do LRU or something. Currently we just try to pick something without a value if possible.
|
|
|
|
|
|
|
|
std::partition(candidates.begin(), candidates.end(), [this](auto loc){
|
|
|
|
return !this->IsRegisterOccupied(loc);
|
|
|
|
});
|
|
|
|
|
|
|
|
return candidates.front();
|
|
|
|
}
|
|
|
|
|
2016-08-19 03:13:38 +01:00
|
|
|
boost::optional<HostLoc> RegAlloc::ValueLocation(const IR::Inst* value) const {
|
2016-07-22 23:55:00 +01:00
|
|
|
for (size_t i = 0; i < HostLocCount; i++)
|
2016-08-19 03:13:38 +01:00
|
|
|
for (const IR::Inst* v : hostloc_info[i].values)
|
2016-08-05 14:10:39 +01:00
|
|
|
if (v == value)
|
2016-08-05 18:40:28 +01:00
|
|
|
return boost::make_optional<HostLoc>(static_cast<HostLoc>(i));
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-05 18:40:28 +01:00
|
|
|
return boost::none;
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegAlloc::IsRegisterOccupied(HostLoc loc) const {
|
2016-08-16 19:52:46 +01:00
|
|
|
const auto& info = LocInfo(loc);
|
|
|
|
|
|
|
|
return !info.values.empty() || info.def;
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegAlloc::IsRegisterAllocated(HostLoc loc) const {
|
2016-08-16 19:52:46 +01:00
|
|
|
return LocInfo(loc).is_being_used;
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegAlloc::IsLastUse(IR::Inst* inst) const {
|
|
|
|
if (inst->use_count > 1)
|
|
|
|
return false;
|
2016-08-16 19:52:46 +01:00
|
|
|
return LocInfo(*ValueLocation(inst)).values.size() == 1;
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::SpillRegister(HostLoc loc) {
|
|
|
|
ASSERT_MSG(HostLocIsRegister(loc), "Only registers can 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();
|
|
|
|
|
2016-08-02 13:46:12 +01:00
|
|
|
EmitMove(new_loc, loc);
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
LocInfo(new_loc) = LocInfo(loc);
|
|
|
|
LocInfo(loc) = {};
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HostLoc RegAlloc::FindFreeSpill() const {
|
|
|
|
for (size_t i = 0; i < SpillCount; i++)
|
|
|
|
if (!IsRegisterOccupied(HostLocSpill(i)))
|
|
|
|
return HostLocSpill(i);
|
|
|
|
|
|
|
|
ASSERT_MSG(0, "All spill locations are full");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::EndOfAllocScope() {
|
2016-08-02 13:46:12 +01:00
|
|
|
for (auto& iter : hostloc_info) {
|
2016-08-05 14:10:39 +01:00
|
|
|
iter.is_being_used = false;
|
|
|
|
if (iter.def) {
|
|
|
|
iter.values.clear();
|
|
|
|
iter.values.emplace_back(iter.def);
|
|
|
|
iter.def = nullptr;
|
2016-08-13 01:55:03 +01:00
|
|
|
}
|
|
|
|
if (!iter.values.empty()) {
|
2016-08-05 14:10:39 +01:00
|
|
|
auto to_erase = std::remove_if(iter.values.begin(), iter.values.end(),
|
2016-08-19 03:08:01 +01:00
|
|
|
[](const auto& inst){ return !inst->HasUses(); });
|
2016-08-05 14:10:39 +01:00
|
|
|
iter.values.erase(to_erase, iter.values.end());
|
|
|
|
}
|
2016-08-02 13:46:12 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-07-22 23:55:00 +01:00
|
|
|
void RegAlloc::DecrementRemainingUses(IR::Inst* value) {
|
2016-08-19 03:08:01 +01:00
|
|
|
ASSERT_MSG(value->HasUses(), "value doesn't have any remaining uses");
|
2016-07-22 23:55:00 +01:00
|
|
|
value->use_count--;
|
2016-07-11 22:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::AssertNoMoreUses() {
|
2016-08-05 14:10:39 +01:00
|
|
|
ASSERT(std::all_of(hostloc_info.begin(), hostloc_info.end(), [](const auto& i){ return i.values.empty(); }));
|
2016-07-11 22:43:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-04 14:37:50 +01:00
|
|
|
void RegAlloc::Reset() {
|
2016-08-02 13:46:12 +01:00
|
|
|
hostloc_info.fill({});
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::EmitMove(HostLoc to, HostLoc from) {
|
|
|
|
if (HostLocIsXMM(to) && HostLocIsSpill(from)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->movsd(HostLocToXmm(to), SpillToOpArg(from));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else if (HostLocIsSpill(to) && HostLocIsXMM(from)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->movsd(SpillToOpArg(to), HostLocToXmm(from));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else if (HostLocIsXMM(to) && HostLocIsXMM(from)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->movaps(HostLocToXmm(to), HostLocToXmm(from));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else if (HostLocIsGPR(to) && HostLocIsSpill(from)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->mov(HostLocToReg64(to), SpillToOpArg(from));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else if (HostLocIsSpill(to) && HostLocIsGPR(from)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->mov(SpillToOpArg(to), HostLocToReg64(from));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else if (HostLocIsGPR(to) && HostLocIsGPR(from)){
|
2016-08-24 20:07:08 +01:00
|
|
|
code->mov(HostLocToReg64(to), HostLocToReg64(from));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid RegAlloc::EmitMove");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::EmitExchange(HostLoc a, HostLoc b) {
|
|
|
|
if (HostLocIsGPR(a) && HostLocIsGPR(b)) {
|
2016-08-24 20:07:08 +01:00
|
|
|
code->xchg(HostLocToReg64(a), HostLocToReg64(b));
|
2016-08-02 13:46:12 +01:00
|
|
|
} else if (HostLocIsXMM(a) && HostLocIsXMM(b)) {
|
|
|
|
ASSERT_MSG(false, "Exchange is unnecessary for XMM registers");
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid RegAlloc::EmitExchange");
|
|
|
|
}
|
2016-07-04 14:37:50 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 14:10:39 +01:00
|
|
|
std::tuple<HostLoc, bool> RegAlloc::UseHostLoc(IR::Inst* use_inst, HostLocList desired_locations) {
|
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
|
|
|
DEBUG_ASSERT_MSG(ValueLocation(use_inst), "use_inst has not been defined");
|
|
|
|
|
|
|
|
HostLoc current_location = *ValueLocation(use_inst);
|
|
|
|
auto iter = std::find(desired_locations.begin(), desired_locations.end(), current_location);
|
|
|
|
if (iter != desired_locations.end()) {
|
|
|
|
if (LocInfo(current_location).IsDef()) {
|
|
|
|
HostLoc new_location = SelectARegister(desired_locations);
|
|
|
|
if (IsRegisterOccupied(new_location)) {
|
|
|
|
SpillRegister(new_location);
|
|
|
|
}
|
|
|
|
EmitMove(new_location, current_location);
|
|
|
|
LocInfo(new_location).is_being_used = true;
|
|
|
|
LocInfo(new_location).values.emplace_back(use_inst);
|
|
|
|
DecrementRemainingUses(use_inst);
|
|
|
|
DEBUG_ASSERT(LocInfo(new_location).IsUse());
|
|
|
|
return std::make_tuple(new_location, false);
|
|
|
|
} else {
|
|
|
|
bool was_being_used = LocInfo(current_location).is_being_used;
|
|
|
|
ASSERT(LocInfo(current_location).IsUse() || LocInfo(current_location).IsIdle());
|
|
|
|
LocInfo(current_location).is_being_used = true;
|
|
|
|
DecrementRemainingUses(use_inst);
|
2016-08-05 18:40:28 +01:00
|
|
|
DEBUG_ASSERT(LocInfo(current_location).IsUse());
|
2016-08-05 14:10:39 +01:00
|
|
|
return std::make_tuple(current_location, was_being_used);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HostLocIsSpill(current_location)) {
|
|
|
|
bool was_being_used = LocInfo(current_location).is_being_used;
|
|
|
|
LocInfo(current_location).is_being_used = true;
|
|
|
|
DecrementRemainingUses(use_inst);
|
|
|
|
DEBUG_ASSERT(LocInfo(current_location).IsUse());
|
|
|
|
return std::make_tuple(current_location, was_being_used);
|
|
|
|
} else if (HostLocIsRegister(current_location)) {
|
|
|
|
HostLoc new_location = SelectARegister(desired_locations);
|
|
|
|
ASSERT(LocInfo(current_location).IsIdle());
|
|
|
|
EmitExchange(new_location, current_location);
|
|
|
|
std::swap(LocInfo(new_location), LocInfo(current_location));
|
|
|
|
LocInfo(new_location).is_being_used = true;
|
|
|
|
DecrementRemainingUses(use_inst);
|
|
|
|
DEBUG_ASSERT(LocInfo(new_location).IsUse());
|
|
|
|
return std::make_tuple(new_location, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_MSG(0, "Invalid current_location");
|
|
|
|
return std::make_tuple(static_cast<HostLoc>(-1), false);
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
HostLoc RegAlloc::LoadImmediateIntoHostLocReg(IR::Value imm, HostLoc host_loc) {
|
2016-08-05 14:10:39 +01:00
|
|
|
ASSERT_MSG(imm.IsImmediate(), "imm is not an immediate");
|
2016-08-24 20:07:08 +01:00
|
|
|
|
|
|
|
Xbyak::Reg64 reg = HostLocToReg64(host_loc);
|
|
|
|
|
|
|
|
u32 imm_value = ImmediateToU32(imm);
|
|
|
|
if (imm_value == 0)
|
|
|
|
code->xor_(reg, reg);
|
2016-08-06 21:04:13 +01:00
|
|
|
else
|
2016-08-24 20:07:08 +01:00
|
|
|
code->mov(reg.cvt32(), imm_value);
|
|
|
|
return host_loc;
|
2016-08-05 14:10:39 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 14:01:06 +01:00
|
|
|
} // namespace BackendX64
|
|
|
|
} // namespace Dynarmic
|