[midend-phielimination]消除只有一个incomingvalue的phi指令

This commit is contained in:
rain2133
2025-08-19 08:27:18 +08:00
parent ce4d4b5f5b
commit db122cabbd
5 changed files with 13 additions and 6 deletions

View File

@@ -110,14 +110,18 @@ public:
// PHI指令消除相关方法 // PHI指令消除相关方法
static void eliminateRedundantPhisInFunction(Function* func){ static bool eliminateRedundantPhisInFunction(Function* func){
bool changed = false;
std::vector<Instruction *> toDelete; std::vector<Instruction *> toDelete;
for (auto &bb : func->getBasicBlocks()) { for (auto &bb : func->getBasicBlocks()) {
for (auto &inst : bb->getInstructions()) { for (auto &inst : bb->getInstructions()) {
if (auto phi = dynamic_cast<PhiInst *>(inst.get())) { if (auto phi = dynamic_cast<PhiInst *>(inst.get())) {
auto incoming = phi->getIncomingValues(); auto incoming = phi->getIncomingValues();
if(DEBUG){
std::cout << "Checking Phi: " << phi->getName() << " with " << incoming.size() << " incoming values." << std::endl;
}
if (incoming.size() == 1) { if (incoming.size() == 1) {
Value *singleVal = incoming[0].first; Value *singleVal = incoming[0].second;
inst->replaceAllUsesWith(singleVal); inst->replaceAllUsesWith(singleVal);
toDelete.push_back(inst.get()); toDelete.push_back(inst.get());
} }
@@ -128,7 +132,9 @@ public:
} }
for (auto *phi : toDelete) { for (auto *phi : toDelete) {
usedelete(phi); usedelete(phi);
changed = true; // 标记为已更改
} }
return changed; // 返回是否有删除发生
} }
//该实现参考了libdivide的算法 //该实现参考了libdivide的算法

View File

@@ -74,6 +74,7 @@ void DCEContext::run(Function *func, AnalysisManager *AM, bool &changed) {
} }
} }
} }
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func); // 如果有活跃指令,则标记为已更改
} }
// 判断指令是否是"天然活跃"的实现 // 判断指令是否是"天然活跃"的实现

View File

@@ -39,7 +39,7 @@ bool GVN::runOnFunction(Function *func, AnalysisManager &AM) {
} }
std::cout << "=== GVN completed for function: " << func->getName() << " ===" << std::endl; std::cout << "=== GVN completed for function: " << func->getName() << " ===" << std::endl;
} }
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func);
return changed; return changed;
} }

View File

@@ -133,6 +133,7 @@ bool InductionVariableEliminationContext::run(Function* F, AnalysisManager& AM)
printDebugInfo(); printDebugInfo();
} }
modified |= SysYIROptUtils::eliminateRedundantPhisInFunction(F);
return modified; return modified;
} }

View File

@@ -1357,9 +1357,8 @@ void SCCPContext::run(Function *func, AnalysisManager &AM) {
bool changed_control_flow = SimplifyControlFlow(func); bool changed_control_flow = SimplifyControlFlow(func);
// 如果任何一个阶段修改了 IR标记分析结果为失效 // 如果任何一个阶段修改了 IR标记分析结果为失效
if (changed_constant_propagation || changed_control_flow) { bool changed = changed_constant_propagation || changed_control_flow;
// AM.invalidate(); // 假设有这样的方法来使所有分析结果失效 changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func);
}
} }
// SCCP Pass methods // SCCP Pass methods