fix: parameterize caller-saved threshold for GP/FP in ColorGraph

Address code review feedback:
- Add caller_saved_threshold parameter to ColorGraph (GP: 19, FP: 16)
- Replace std::vector heap allocation with two-pass scan for zero overhead
- Fix semantic error: c<19 was incorrect for FP (s16-s18 are callee-saved)
master
黄熙哲 1 week ago
parent 4d95f33dc2
commit 26d89b2fbd

@ -599,7 +599,8 @@ namespace mir
static GraphColoringResult ColorGraph(
InterferenceGraph &graph,
const std::vector<int> &allocatable_regs,
MachineFunction & /*function*/)
MachineFunction & /*function*/,
int caller_saved_threshold = 19)
{
const int K = static_cast<int>(allocatable_regs.size());
GraphColoringResult result;
@ -726,24 +727,29 @@ namespace mir
}
int assigned_color = -1;
// 收集可用颜色区分caller-saved和callee-saved
std::vector<int> caller_saved_avail;
std::vector<int> callee_saved_avail;
// 第一遍:优先选 caller-saved 颜色 (c < caller_saved_threshold)
for (int c : allocatable_regs)
{
if (c >= caller_saved_threshold) break;
if (used_colors.find(c) == used_colors.end())
{
if (c < 19)
caller_saved_avail.push_back(c);
else
callee_saved_avail.push_back(c);
assigned_color = c;
break;
}
}
// 第二遍:若 caller-saved 无可用,选 callee-saved
if (assigned_color < 0)
{
for (int c : allocatable_regs)
{
if (c < caller_saved_threshold) continue;
if (used_colors.find(c) == used_colors.end())
{
assigned_color = c;
break;
}
}
}
// 优先选择caller-saved颜色
if (!caller_saved_avail.empty())
assigned_color = caller_saved_avail[0];
else if (!callee_saved_avail.empty())
assigned_color = callee_saved_avail[0];
if (assigned_color >= 0)
{
@ -996,8 +1002,8 @@ namespace mir
BuildInterferenceForGP(function, block_liveness, gp_alloc, gp_graph);
BuildInterferenceForFP(function, block_liveness, fp_alloc, fp_graph);
auto gp_result = ColorGraph(gp_graph, gp_alloc, function);
auto fp_result = ColorGraph(fp_graph, fp_alloc, function);
auto gp_result = ColorGraph(gp_graph, gp_alloc, function, 19);
auto fp_result = ColorGraph(fp_graph, fp_alloc, function, 16);
if (gp_result.spilled.empty() && fp_result.spilled.empty())
{

Loading…
Cancel
Save