[backend] fix the logical error of constants in interference graph construction

This commit is contained in:
Lixuanwang
2025-06-25 14:35:20 +08:00
parent c8587a6d0b
commit 15a80bd5cd

View File

@ -1121,6 +1121,35 @@ std::map<std::string, std::set<std::string>> RISCv64CodeGen::build_interference_
}
}
}
// --- 新增修复逻辑:处理指令内部操作数之间的干扰 ---
// 对于 store 指令,要存储的值和目标地址指针是同时活跃的,必须互相干扰。
if (auto store = dynamic_cast<StoreInst*>(inst)) {
Value* val_operand = store->getValue();
Value* ptr_operand = store->getPointer();
if (value_vreg_map.count(val_operand) && value_vreg_map.count(ptr_operand)) {
const std::string& val_vreg = value_vreg_map.at(val_operand);
const std::string& ptr_vreg = value_vreg_map.at(ptr_operand);
if (val_vreg != ptr_vreg) {
graph[val_vreg].insert(ptr_vreg);
graph[ptr_vreg].insert(val_vreg);
}
}
}
// 可选:为其他有两个或以上源操作数的指令(如 add添加类似逻辑
// 确保它们的操作数虚拟寄存器互相干扰。
else if (auto bin = dynamic_cast<BinaryInst*>(inst)) {
Value* lhs_operand = bin->getLhs();
Value* rhs_operand = bin->getRhs();
if (value_vreg_map.count(lhs_operand) && value_vreg_map.count(rhs_operand)) {
const std::string& lhs_vreg = value_vreg_map.at(lhs_operand);
const std::string& rhs_vreg = value_vreg_map.at(rhs_operand);
if (lhs_vreg != rhs_vreg) {
graph[lhs_vreg].insert(rhs_vreg);
graph[rhs_vreg].insert(rhs_vreg);
}
}
}
}
return graph;
}