[midend-Loop-IVE]修复循环的死IV消除逻辑

This commit is contained in:
rain2133
2025-08-15 01:19:45 +08:00
parent a3435e7c26
commit fa33bf5134
2 changed files with 54 additions and 15 deletions

View File

@@ -255,6 +255,18 @@ bool InductionVariableEliminationContext::isInstructionUseChainDeadRecursively(
return false; // 有副作用的指令是逃逸点
}
// 1.5. 特殊检查:控制流指令永远不是死代码
auto instKind = inst->getKind();
if (instKind == Instruction::Kind::kCondBr ||
instKind == Instruction::Kind::kBr ||
instKind == Instruction::Kind::kReturn) {
if (DEBUG && visited.size() < 10) {
std::cout << " 控制流指令,是逃逸点" << std::endl;
}
currentPath.erase(inst);
return false; // 控制流指令是逃逸点
}
// 2. 检查指令的所有使用
bool allUsesAreDead = true;
for (auto use : inst->getUses()) {
@@ -280,22 +292,15 @@ bool InductionVariableEliminationContext::isInstructionUseChainDeadRecursively(
}
// 特殊检查:如果使用者是循环的退出条件,需要进一步分析
// 只有当循环有副作用时,才将用于退出条件的归纳变量视为逃逸点
// 对于用于退出条件的归纳变量,需要更谨慎的处理
if (isUsedInLoopExitCondition(userInst, loop)) {
// 检查循环是否有副作用
if (loopHasSideEffects(loop)) {
if (DEBUG && visited.size() < 10) {
std::cout << " 被用于循环退出条件且循环有副作用,是逃逸点" << std::endl;
}
allUsesAreDead = false;
break;
} else {
if (DEBUG && visited.size() < 10) {
std::cout << " 被用于循环退出条件但循环无副作用,继续分析" << std::endl;
}
// 对于纯循环,即使用于退出条件也不是逃逸点,继续分析其他使用
continue;
// 修复逻辑:用于循环退出条件的归纳变量通常不应该被消除
// 除非整个循环都可以被证明是完全无用的(这需要更复杂的分析)
if (DEBUG && visited.size() < 10) {
std::cout << " 被用于循环退出条件,是逃逸点(避免破坏循环语义)" << std::endl;
}
allUsesAreDead = false;
break;
}
// 递归分析使用者的使用链
@@ -345,6 +350,24 @@ bool InductionVariableEliminationContext::loopHasSideEffects(Loop* loop) {
}
}
// 重要修复:检查是否为嵌套循环的外层循环
// 如果当前循环包含其他循环,那么它有潜在的副作用
for (const auto& loop_ptr : loopAnalysis->getAllLoops()) {
Loop* otherLoop = loop_ptr.get();
if(loopAnalysis->getLowestCommonAncestor(otherLoop, loop) == loop) {
if (DEBUG) {
std::cout << " 循环 " << loop->getName() << " 是其他循环的外层循环,视为有副作用" << std::endl;
}
return true; // 外层循环被视为有副作用
}
// if (otherLoop != loop && loop->contains(otherLoop->getHeader())) {
// if (DEBUG) {
// std::cout << " 循环 " << loop->getName() << " 包含子循环 " << otherLoop->getName() << ",视为有副作用" << std::endl;
// }
// return true; // 包含子循环的外层循环被视为有副作用
// }
}
if (DEBUG) {
std::cout << " 循环 " << loop->getName() << " 无副作用" << std::endl;
}