2016-07-01 15:01:06 +02: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>
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
#include <xbyak.h>
|
|
|
|
|
2016-08-26 19:43:50 +02:00
|
|
|
#include "backend_x64/abi.h"
|
2016-08-24 21:07:08 +02:00
|
|
|
#include "backend_x64/jitstate.h"
|
2016-07-01 15:01:06 +02:00
|
|
|
#include "backend_x64/reg_alloc.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace BackendX64 {
|
|
|
|
|
2016-12-03 12:29:50 +01:00
|
|
|
static u64 ImmediateToU64(const IR::Value& imm) {
|
2016-08-05 15:10:39 +02:00
|
|
|
switch (imm.GetType()) {
|
2016-08-23 00:40:30 +02:00
|
|
|
case IR::Type::U1:
|
2016-12-03 12:29:50 +01:00
|
|
|
return u64(imm.GetU1());
|
2016-08-23 00:40:30 +02:00
|
|
|
case IR::Type::U8:
|
2016-12-03 12:29:50 +01:00
|
|
|
return u64(imm.GetU8());
|
2016-08-23 00:40:30 +02:00
|
|
|
case IR::Type::U32:
|
2016-12-03 12:29:50 +01:00
|
|
|
return u64(imm.GetU32());
|
|
|
|
case IR::Type::U64:
|
|
|
|
return u64(imm.GetU64());
|
2016-08-23 00:40:30 +02:00
|
|
|
default:
|
|
|
|
ASSERT_MSG(false, "This should never happen.");
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
static Xbyak::Reg HostLocToX64(HostLoc hostloc) {
|
|
|
|
if (HostLocIsGPR(hostloc)) {
|
2016-11-30 20:42:41 +01:00
|
|
|
DEBUG_ASSERT(hostloc != HostLoc::RSP && hostloc != HostLoc::R15);
|
2016-08-24 21:07:08 +02:00
|
|
|
return HostLocToReg64(hostloc);
|
2016-08-06 18:11:22 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
if (HostLocIsXMM(hostloc)) {
|
|
|
|
return HostLocToXmm(hostloc);
|
2016-08-06 18:11:22 +02:00
|
|
|
}
|
|
|
|
ASSERT_MSG(false, "This should never happen.");
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2017-02-24 20:42:36 +01:00
|
|
|
static bool IsSameHostLocClass(HostLoc a, HostLoc b) {
|
|
|
|
return (HostLocIsGPR(a) && HostLocIsGPR(b))
|
|
|
|
|| (HostLocIsXMM(a) && HostLocIsXMM(b))
|
|
|
|
|| (HostLocIsSpill(a) && HostLocIsSpill(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitMove(BlockOfCode* code, HostLoc to, HostLoc from) {
|
|
|
|
if (HostLocIsXMM(to) && HostLocIsXMM(from)) {
|
|
|
|
code->movaps(HostLocToXmm(to), HostLocToXmm(from));
|
|
|
|
} else if (HostLocIsGPR(to) && HostLocIsGPR(from)) {
|
|
|
|
code->mov(HostLocToReg64(to), HostLocToReg64(from));
|
|
|
|
} else if (HostLocIsXMM(to) && HostLocIsGPR(from)) {
|
|
|
|
ASSERT_MSG(false, "TODO");
|
|
|
|
} else if (HostLocIsGPR(to) && HostLocIsXMM(from)) {
|
|
|
|
ASSERT_MSG(false, "TODO");
|
|
|
|
} else if (HostLocIsXMM(to) && HostLocIsSpill(from)) {
|
|
|
|
code->movsd(HostLocToXmm(to), SpillToOpArg(from));
|
|
|
|
} else if (HostLocIsSpill(to) && HostLocIsXMM(from)) {
|
|
|
|
code->movsd(SpillToOpArg(to), HostLocToXmm(from));
|
|
|
|
} else if (HostLocIsGPR(to) && HostLocIsSpill(from)) {
|
|
|
|
code->mov(HostLocToReg64(to), SpillToOpArg(from));
|
|
|
|
} else if (HostLocIsSpill(to) && HostLocIsGPR(from)) {
|
|
|
|
code->mov(SpillToOpArg(to), HostLocToReg64(from));
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid RegAlloc::EmitMove");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitExchange(BlockOfCode* code, HostLoc a, HostLoc b) {
|
|
|
|
if (HostLocIsGPR(a) && HostLocIsGPR(b)) {
|
|
|
|
code->xchg(HostLocToReg64(a), HostLocToReg64(b));
|
|
|
|
} else if (HostLocIsXMM(a) && HostLocIsXMM(b)) {
|
|
|
|
ASSERT_MSG(false, "Check your code: Exchanging XMM registers is unnecessary");
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid RegAlloc::EmitExchange");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 15:10:39 +02:00
|
|
|
void RegAlloc::RegisterAddDef(IR::Inst* def_inst, const IR::Value& use_inst) {
|
2016-08-05 19:40:28 +02:00
|
|
|
DEBUG_ASSERT_MSG(!ValueLocation(def_inst), "def_inst has already been defined");
|
2016-08-05 15:10:39 +02:00
|
|
|
|
|
|
|
if (use_inst.IsImmediate()) {
|
2017-02-24 20:08:58 +01:00
|
|
|
HostLoc location = ScratchHostLocReg(any_gpr);
|
|
|
|
DefineValue(def_inst, location);
|
|
|
|
LoadImmediateIntoHostLocReg(use_inst, location);
|
2016-08-05 15:10:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-24 20:08:58 +01:00
|
|
|
use_inst.GetInst()->DecrementRemainingUses();
|
2016-08-05 19:40:28 +02:00
|
|
|
DEBUG_ASSERT_MSG(ValueLocation(use_inst.GetInst()), "use_inst must already be defined");
|
2016-08-05 15:10:39 +02:00
|
|
|
HostLoc location = *ValueLocation(use_inst.GetInst());
|
2017-02-24 20:08:58 +01:00
|
|
|
DefineValue(def_inst, location);
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
std::tuple<OpArg, HostLoc> RegAlloc::UseDefOpArgHostLocReg(IR::Value use_value, IR::Inst* def_inst, HostLocList desired_locations) {
|
2016-08-05 15:10:39 +02:00
|
|
|
DEBUG_ASSERT(std::all_of(desired_locations.begin(), desired_locations.end(), HostLocIsRegister));
|
2016-08-05 19:40:28 +02: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 15:10:39 +02:00
|
|
|
|
|
|
|
if (!use_value.IsImmediate()) {
|
2016-09-02 18:30:01 +02:00
|
|
|
const IR::Inst* use_inst = use_value.GetInst();
|
2016-08-05 15:10:39 +02:00
|
|
|
|
|
|
|
if (IsLastUse(use_inst)) {
|
|
|
|
HostLoc current_location = *ValueLocation(use_inst);
|
|
|
|
auto& loc_info = LocInfo(current_location);
|
|
|
|
if (!loc_info.IsIdle()) {
|
2016-08-05 19:40:28 +02:00
|
|
|
if (HostLocIsSpill(current_location)) {
|
2017-02-24 19:42:59 +01:00
|
|
|
loc_info.Lock();
|
2016-08-05 19:40:28 +02:00
|
|
|
DEBUG_ASSERT(loc_info.IsUse());
|
2017-02-24 20:08:58 +01:00
|
|
|
HostLoc location = ScratchHostLocReg(desired_locations);
|
|
|
|
DefineValue(def_inst, location);
|
|
|
|
return std::make_tuple(SpillToOpArg(current_location), location);
|
2016-08-05 19:40:28 +02:00
|
|
|
} else {
|
2017-02-24 19:42:59 +01:00
|
|
|
loc_info.Lock();
|
2017-02-24 20:08:58 +01:00
|
|
|
DefineValue(def_inst, current_location);
|
2016-08-24 21:07:08 +02:00
|
|
|
return std::make_tuple(HostLocToX64(current_location), current_location);
|
2016-08-05 19:40:28 +02:00
|
|
|
}
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
OpArg use_oparg = UseOpArg(use_value, any_gpr);
|
2017-02-24 20:08:58 +01:00
|
|
|
HostLoc def_reg = ScratchHostLocReg(desired_locations);
|
|
|
|
DefineValue(def_inst, def_reg);
|
2016-08-05 15:10:39 +02:00
|
|
|
return std::make_tuple(use_oparg, def_reg);
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
HostLoc RegAlloc::UseHostLocReg(IR::Value use_value, HostLocList desired_locations) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (!use_value.IsImmediate()) {
|
2016-08-24 21:07:08 +02:00
|
|
|
return UseHostLocReg(use_value.GetInst(), desired_locations);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
return LoadImmediateIntoHostLocReg(use_value, ScratchHostLocReg(desired_locations));
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
HostLoc RegAlloc::UseHostLocReg(IR::Inst* use_inst, HostLocList desired_locations) {
|
2017-02-24 20:42:36 +01:00
|
|
|
use_inst->DecrementRemainingUses();
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2017-02-24 20:42:36 +01:00
|
|
|
const HostLoc current_location = *ValueLocation(use_inst);
|
|
|
|
|
|
|
|
const bool can_use_current_location = std::find(desired_locations.begin(), desired_locations.end(), current_location) != desired_locations.end();
|
|
|
|
if (can_use_current_location) {
|
|
|
|
LocInfo(current_location).Lock();
|
2016-08-24 21:07:08 +02:00
|
|
|
return current_location;
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2017-02-24 20:42:36 +01:00
|
|
|
if (LocInfo(current_location).IsLocked()) {
|
|
|
|
return UseScratchHostLocReg(use_inst, desired_locations);
|
|
|
|
}
|
|
|
|
|
|
|
|
const HostLoc destination_location = SelectARegister(desired_locations);
|
|
|
|
if (IsSameHostLocClass(destination_location, current_location)) {
|
|
|
|
Exchange(destination_location, current_location);
|
|
|
|
} else {
|
|
|
|
MoveOutOfTheWay(destination_location);
|
|
|
|
Move(destination_location, current_location);
|
|
|
|
}
|
|
|
|
LocInfo(destination_location).Lock();
|
|
|
|
return destination_location;
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
OpArg RegAlloc::UseOpArg(IR::Value use_value, HostLocList desired_locations) {
|
2016-08-05 15:10:39 +02:00
|
|
|
if (use_value.IsImmediate()) {
|
2016-08-26 16:23:38 +02:00
|
|
|
ASSERT_MSG(false, "UseOpArg does not support immediates");
|
|
|
|
return {}; // return a None
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2017-02-24 20:46:32 +01:00
|
|
|
// TODO: Reimplement properly
|
|
|
|
return HostLocToX64(UseHostLocReg(use_value.GetInst(), desired_locations));
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
HostLoc RegAlloc::UseScratchHostLocReg(IR::Value use_value, HostLocList desired_locations) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (!use_value.IsImmediate()) {
|
2016-08-24 21:07:08 +02:00
|
|
|
return UseScratchHostLocReg(use_value.GetInst(), desired_locations);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
return LoadImmediateIntoHostLocReg(use_value, ScratchHostLocReg(desired_locations));
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
HostLoc RegAlloc::UseScratchHostLocReg(IR::Inst* use_inst, HostLocList desired_locations) {
|
2017-02-24 20:58:16 +01:00
|
|
|
use_inst->DecrementRemainingUses();
|
2016-07-18 16:11:16 +02:00
|
|
|
|
2017-02-24 20:58:16 +01:00
|
|
|
const HostLoc current_location = *ValueLocation(use_inst);
|
2016-07-18 16:11:16 +02:00
|
|
|
|
2017-02-24 20:58:16 +01:00
|
|
|
const bool can_use_current_location = std::find(desired_locations.begin(), desired_locations.end(), current_location) != desired_locations.end();
|
|
|
|
if (can_use_current_location && !LocInfo(current_location).IsLocked()) {
|
|
|
|
MoveOutOfTheWay(current_location);
|
|
|
|
LocInfo(current_location).Lock();
|
|
|
|
return current_location;
|
2016-07-18 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:58:16 +01:00
|
|
|
const HostLoc destination_location = SelectARegister(desired_locations);
|
|
|
|
MoveOutOfTheWay(destination_location);
|
|
|
|
CopyToScratch(destination_location, current_location);
|
|
|
|
LocInfo(destination_location).Lock();
|
|
|
|
return destination_location;
|
2016-07-18 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
HostLoc RegAlloc::ScratchHostLocReg(HostLocList desired_locations) {
|
2016-07-01 15:01:06 +02:00
|
|
|
HostLoc location = SelectARegister(desired_locations);
|
2017-02-24 21:01:41 +01:00
|
|
|
MoveOutOfTheWay(location);
|
2017-02-24 19:42:59 +01:00
|
|
|
LocInfo(location).Lock();
|
2016-08-24 21:07:08 +02:00
|
|
|
return location;
|
2016-07-23 00:55:00 +02: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 19:43:50 +02: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 16:28:10 +02:00
|
|
|
|
2016-09-01 00:53:16 +02:00
|
|
|
const static std::vector<HostLoc> other_caller_save = [args_hostloc](){
|
2016-08-26 19:43:50 +02:00
|
|
|
std::vector<HostLoc> ret(ABI_ALL_CALLER_SAVE.begin(), ABI_ALL_CALLER_SAVE.end());
|
2016-07-11 16:28:10 +02:00
|
|
|
|
2016-09-01 00:53:16 +02:00
|
|
|
for (auto hostloc : args_hostloc)
|
2016-08-26 19:43:50 +02:00
|
|
|
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 16:28:10 +02:00
|
|
|
|
|
|
|
if (result_def) {
|
2017-02-24 20:08:58 +01:00
|
|
|
DefineValue(result_def, ScratchHostLocReg({ABI_RETURN}));
|
2016-07-11 16:28:10 +02:00
|
|
|
} else {
|
2016-08-26 19:43:50 +02:00
|
|
|
ScratchHostLocReg({ABI_RETURN});
|
2016-07-11 16:28:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 19:43:50 +02:00
|
|
|
for (size_t i = 0; i < args_count; i++) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (!args[i]->IsEmpty()) {
|
2016-08-26 19:43:50 +02:00
|
|
|
UseScratchHostLocReg(*args[i], {args_hostloc[i]});
|
2016-07-11 16:28:10 +02:00
|
|
|
} else {
|
2016-08-26 19:43:50 +02:00
|
|
|
ScratchHostLocReg({args_hostloc[i]});
|
2016-07-11 16:28:10 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 22:48:45 +02:00
|
|
|
|
2016-08-26 19:43:50 +02:00
|
|
|
for (HostLoc caller_saved : other_caller_save) {
|
|
|
|
ScratchHostLocReg({caller_saved});
|
2016-08-07 22:02:16 +02:00
|
|
|
}
|
2016-07-11 16:28:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 14:46:12 +02:00
|
|
|
HostLoc RegAlloc::SelectARegister(HostLocList desired_locations) const {
|
2016-07-01 15:01:06 +02: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 04:13:38 +02:00
|
|
|
boost::optional<HostLoc> RegAlloc::ValueLocation(const IR::Inst* value) const {
|
2016-07-23 00:55:00 +02:00
|
|
|
for (size_t i = 0; i < HostLocCount; i++)
|
2017-02-24 19:42:59 +01:00
|
|
|
if (hostloc_info[i].ContainsValue(value))
|
|
|
|
return boost::make_optional<HostLoc>(static_cast<HostLoc>(i));
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-08-05 19:40:28 +02:00
|
|
|
return boost::none;
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegAlloc::IsRegisterOccupied(HostLoc loc) const {
|
2016-08-16 20:52:46 +02:00
|
|
|
const auto& info = LocInfo(loc);
|
|
|
|
|
2017-02-24 19:42:59 +01:00
|
|
|
return !info.IsEmpty();
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegAlloc::IsRegisterAllocated(HostLoc loc) const {
|
2017-02-24 19:42:59 +01:00
|
|
|
return !LocInfo(loc).IsIdle();
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 19:42:59 +01:00
|
|
|
bool RegAlloc::IsLastUse(const IR::Inst*) const {
|
|
|
|
//if (inst->UseCount() > 1)
|
|
|
|
// return false;
|
|
|
|
//return LocInfo(*ValueLocation(inst)).values.size() == 1;
|
|
|
|
return false;
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:08:58 +01:00
|
|
|
void RegAlloc::DefineValue(IR::Inst* def_inst, HostLoc host_loc) {
|
|
|
|
DEBUG_ASSERT_MSG(!ValueLocation(def_inst), "def_inst has already been defined");
|
|
|
|
LocInfo(host_loc).AddValue(def_inst);
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:01:06 +02: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();
|
|
|
|
|
2017-02-24 20:42:36 +01:00
|
|
|
EmitMove(code, new_loc, loc);
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-08-05 15:10:39 +02:00
|
|
|
LocInfo(new_loc) = LocInfo(loc);
|
|
|
|
LocInfo(loc) = {};
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HostLoc RegAlloc::FindFreeSpill() const {
|
|
|
|
for (size_t i = 0; i < SpillCount; i++)
|
|
|
|
if (!IsRegisterOccupied(HostLocSpill(i)))
|
|
|
|
return HostLocSpill(i);
|
|
|
|
|
2016-08-26 17:43:51 +02:00
|
|
|
ASSERT_MSG(false, "All spill locations are full");
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::EndOfAllocScope() {
|
2016-08-02 14:46:12 +02:00
|
|
|
for (auto& iter : hostloc_info) {
|
2017-02-24 19:42:59 +01:00
|
|
|
iter.EndOfAllocScope();
|
2016-08-02 14:46:12 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 23:43:53 +02:00
|
|
|
void RegAlloc::AssertNoMoreUses() {
|
2017-02-24 19:42:59 +01:00
|
|
|
if (!std::all_of(hostloc_info.begin(), hostloc_info.end(), [](const auto& i){ return i.IsEmpty(); })) {
|
|
|
|
ASSERT_MSG(false, "bad");
|
|
|
|
}
|
2016-07-11 23:43:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 15:37:50 +02:00
|
|
|
void RegAlloc::Reset() {
|
2016-08-02 14:46:12 +02:00
|
|
|
hostloc_info.fill({});
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
HostLoc RegAlloc::LoadImmediateIntoHostLocReg(IR::Value imm, HostLoc host_loc) {
|
2016-08-05 15:10:39 +02:00
|
|
|
ASSERT_MSG(imm.IsImmediate(), "imm is not an immediate");
|
2016-08-24 21:07:08 +02:00
|
|
|
|
|
|
|
Xbyak::Reg64 reg = HostLocToReg64(host_loc);
|
|
|
|
|
2016-12-03 12:29:50 +01:00
|
|
|
u64 imm_value = ImmediateToU64(imm);
|
2016-08-24 21:07:08 +02:00
|
|
|
if (imm_value == 0)
|
2016-12-03 12:29:50 +01:00
|
|
|
code->xor_(reg.cvt32(), reg.cvt32());
|
2016-08-06 22:04:13 +02:00
|
|
|
else
|
2016-12-03 12:29:50 +01:00
|
|
|
code->mov(reg, imm_value);
|
2016-08-24 21:07:08 +02:00
|
|
|
return host_loc;
|
2016-08-05 15:10:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:42:36 +01:00
|
|
|
void RegAlloc::Move(HostLoc to, HostLoc from) {
|
|
|
|
ASSERT(LocInfo(to).IsEmpty() && !LocInfo(from).IsLocked());
|
|
|
|
|
|
|
|
if (LocInfo(from).IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocInfo(to) = LocInfo(from);
|
|
|
|
LocInfo(from) = {};
|
|
|
|
|
|
|
|
EmitMove(code, to, from);
|
|
|
|
}
|
|
|
|
|
2017-02-24 20:58:16 +01:00
|
|
|
void RegAlloc::CopyToScratch(HostLoc to, HostLoc from) {
|
|
|
|
ASSERT(LocInfo(to).IsEmpty() && !LocInfo(from).IsEmpty());
|
|
|
|
|
|
|
|
EmitMove(code, to, from);
|
|
|
|
}
|
|
|
|
|
2017-02-24 20:42:36 +01:00
|
|
|
void RegAlloc::Exchange(HostLoc a, HostLoc b) {
|
|
|
|
ASSERT(!LocInfo(a).IsLocked() && !LocInfo(b).IsLocked());
|
|
|
|
|
|
|
|
if (LocInfo(a).IsEmpty()) {
|
|
|
|
Move(a, b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LocInfo(b).IsEmpty()) {
|
|
|
|
Move(b, a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::swap(LocInfo(a), LocInfo(b));
|
|
|
|
|
|
|
|
EmitExchange(code, a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::MoveOutOfTheWay(HostLoc reg) {
|
|
|
|
ASSERT(!LocInfo(reg).IsLocked());
|
|
|
|
if (IsRegisterOccupied(reg)) {
|
|
|
|
SpillRegister(reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-01 15:01:06 +02:00
|
|
|
} // namespace BackendX64
|
|
|
|
} // namespace Dynarmic
|