[backend-O1-1]调整寄存器预着色逻辑

This commit is contained in:
Lixuanwang
2025-08-20 02:13:23 +08:00
parent dd2725796a
commit 9ff1ace10e
3 changed files with 38 additions and 30 deletions

View File

@@ -554,6 +554,14 @@ void RISCv64ISel::selectNode(DAGNode* node) {
CurMBB->addInstruction(std::move(instr));
break;
}
case BinaryInst::kMulh: {
auto instr = std::make_unique<MachineInstr>(RVOpcodes::MULH);
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
instr->addOperand(std::make_unique<RegOperand>(lhs_vreg));
instr->addOperand(std::make_unique<RegOperand>(rhs_vreg));
CurMBB->addInstruction(std::move(instr));
break;
}
case Instruction::kDiv: {
auto instr = std::make_unique<MachineInstr>(RVOpcodes::DIVW);
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));

View File

@@ -193,30 +193,30 @@ void RISCv64RegAlloc::protectCrossCallVRegs() {
return; // 如果没有需要保护的vreg直接返回
}
VRegSet live_across_call_vregs;
// 遍历所有指令找出哪些被标记的vreg其生命周期确实跨越了call指令
for (const auto& mbb_ptr : MFunc->getBlocks()) {
for (const auto& instr_ptr : mbb_ptr->getInstructions()) {
if (instr_ptr->getOpcode() == RVOpcodes::CALL) {
const VRegSet& live_out_after_call = live_out_map.at(instr_ptr.get());
for (unsigned vreg : vregs_to_protect_potentially) {
if (live_out_after_call.count(vreg)) {
live_across_call_vregs.insert(vreg);
}
}
}
}
}
// VRegSet live_across_call_vregs;
// // 遍历所有指令找出哪些被标记的vreg其生命周期确实跨越了call指令
// for (const auto& mbb_ptr : MFunc->getBlocks()) {
// for (const auto& instr_ptr : mbb_ptr->getInstructions()) {
// if (instr_ptr->getOpcode() == RVOpcodes::CALL) {
// const VRegSet& live_out_after_call = live_out_map.at(instr_ptr.get());
// for (unsigned vreg : vregs_to_protect_potentially) {
// if (live_out_after_call.count(vreg)) {
// live_across_call_vregs.insert(vreg);
// }
// }
// }
// }
// }
if (live_across_call_vregs.empty()) {
return; // 如果被标记的vreg没有一个跨越call也无需操作
}
// if (live_across_call_vregs.empty()) {
// return; // 如果被标记的vreg没有一个跨越call也无需操作
// }
if (DEEPDEBUG) {
std::cerr << "--- [FIX] Applying protection for argument vregs that live across calls: ";
for(unsigned v : live_across_call_vregs) std::cerr << regIdToString(v) << " ";
std::cerr << "\n";
}
// if (DEEPDEBUG) {
// std::cerr << "--- [FIX] Applying protection for argument vregs that live across calls: ";
// for(unsigned v : live_across_call_vregs) std::cerr << regIdToString(v) << " ";
// std::cerr << "\n";
// }
// 获取所有调用者保存寄存器
const auto& caller_saved_int = getCallerSavedIntRegs();
@@ -224,7 +224,7 @@ void RISCv64RegAlloc::protectCrossCallVRegs() {
const unsigned offset = static_cast<unsigned>(PhysicalReg::PHYS_REG_START_ID);
// 为每个确认跨越call的vreg添加与所有调用者保存寄存器的冲突
for (unsigned vreg : live_across_call_vregs) {
for (unsigned vreg : vregs_to_protect_potentially) {
if (isFPVReg(vreg)) { // 如果是浮点vreg
for (auto preg : caller_saved_fp) {
addEdge(vreg, offset + static_cast<unsigned>(preg));

View File

@@ -181,19 +181,19 @@ void PassManager::runOptimizationPipeline(Module* moduleIR, IRBuilder* builderIR
printPasses();
}
// this->clearPasses();
// this->addPass(&LoopStrengthReduction::ID);
// this->run();
this->clearPasses();
this->addPass(&LoopStrengthReduction::ID);
this->run();
if(DEBUG) {
std::cout << "=== IR After Loop Normalization, and Strength Reduction Optimizations ===\n";
printPasses();
}
// // 全局强度削弱优化,包括代数优化和魔数除法
// this->clearPasses();
// this->addPass(&GlobalStrengthReduction::ID);
// this->run();
// 全局强度削弱优化,包括代数优化和魔数除法
this->clearPasses();
this->addPass(&GlobalStrengthReduction::ID);
this->run();
if(DEBUG) {
std::cout << "=== IR After Global Strength Reduction Optimizations ===\n";