From 439619c8274e0667776187bc90c896a120949862 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 16 Aug 2016 14:52:46 -0400 Subject: [PATCH] reg_alloc: Make GetRegLoc return by const reference Considering a HostLocInfo instance houses a std::vector, every time this function is called can cause a potential heap allocation. This can be somewhat unnecessary because this function is only used to query for information we already have. Considering this is used by several other internal query functions such as IsRegisterOccupied, IsRegisterAllocated, and IsLastUse, this will result in better codegen (returning an address is just 3 instructions excluding the ret instruction for returning, meanwhile heap alloc can be 60+). This also renames the function to have the same name as its non-const counterpart, since overloading will just select the correct function instead of putting that onus on the developer. --- src/backend_x64/reg_alloc.cpp | 8 +++++--- src/backend_x64/reg_alloc.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/backend_x64/reg_alloc.cpp b/src/backend_x64/reg_alloc.cpp index 9352fe15..765db4dd 100644 --- a/src/backend_x64/reg_alloc.cpp +++ b/src/backend_x64/reg_alloc.cpp @@ -351,17 +351,19 @@ boost::optional RegAlloc::ValueLocation(IR::Inst* value) const { } bool RegAlloc::IsRegisterOccupied(HostLoc loc) const { - return !GetLocInfo(loc).values.empty() || GetLocInfo(loc).def; + const auto& info = LocInfo(loc); + + return !info.values.empty() || info.def; } bool RegAlloc::IsRegisterAllocated(HostLoc loc) const { - return GetLocInfo(loc).is_being_used; + return LocInfo(loc).is_being_used; } bool RegAlloc::IsLastUse(IR::Inst* inst) const { if (inst->use_count > 1) return false; - return GetLocInfo(*ValueLocation(inst)).values.size() == 1; + return LocInfo(*ValueLocation(inst)).values.size() == 1; } void RegAlloc::SpillRegister(HostLoc loc) { diff --git a/src/backend_x64/reg_alloc.h b/src/backend_x64/reg_alloc.h index e3cb837c..1555b467 100644 --- a/src/backend_x64/reg_alloc.h +++ b/src/backend_x64/reg_alloc.h @@ -172,7 +172,7 @@ private: HostLocInfo& LocInfo(HostLoc loc) { return hostloc_info[static_cast(loc)]; } - HostLocInfo GetLocInfo(HostLoc loc) const { + const HostLocInfo& LocInfo(HostLoc loc) const { return hostloc_info[static_cast(loc)]; } };