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.
This commit is contained in:
Lioncash 2016-08-16 14:52:46 -04:00 committed by MerryMage
parent 0ebb572e2d
commit 439619c827
2 changed files with 6 additions and 4 deletions

View file

@ -351,17 +351,19 @@ boost::optional<HostLoc> 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) {

View file

@ -172,7 +172,7 @@ private:
HostLocInfo& LocInfo(HostLoc loc) {
return hostloc_info[static_cast<size_t>(loc)];
}
HostLocInfo GetLocInfo(HostLoc loc) const {
const HostLocInfo& LocInfo(HostLoc loc) const {
return hostloc_info[static_cast<size_t>(loc)];
}
};