[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)); CurMBB->addInstruction(std::move(instr));
break; 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: { case Instruction::kDiv: {
auto instr = std::make_unique<MachineInstr>(RVOpcodes::DIVW); auto instr = std::make_unique<MachineInstr>(RVOpcodes::DIVW);
instr->addOperand(std::make_unique<RegOperand>(dest_vreg)); instr->addOperand(std::make_unique<RegOperand>(dest_vreg));

View File

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

View File

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