Merge branch 'midend-phielimination' into midend
This commit is contained in:
@@ -109,6 +109,34 @@ public:
|
||||
}
|
||||
|
||||
|
||||
// PHI指令消除相关方法
|
||||
static bool eliminateRedundantPhisInFunction(Function* func){
|
||||
bool changed = false;
|
||||
std::vector<Instruction *> toDelete;
|
||||
for (auto &bb : func->getBasicBlocks()) {
|
||||
for (auto &inst : bb->getInstructions()) {
|
||||
if (auto phi = dynamic_cast<PhiInst *>(inst.get())) {
|
||||
auto incoming = phi->getIncomingValues();
|
||||
if(DEBUG){
|
||||
std::cout << "Checking Phi: " << phi->getName() << " with " << incoming.size() << " incoming values." << std::endl;
|
||||
}
|
||||
if (incoming.size() == 1) {
|
||||
Value *singleVal = incoming[0].second;
|
||||
inst->replaceAllUsesWith(singleVal);
|
||||
toDelete.push_back(inst.get());
|
||||
}
|
||||
}
|
||||
else
|
||||
break; // 只处理Phi指令
|
||||
}
|
||||
}
|
||||
for (auto *phi : toDelete) {
|
||||
usedelete(phi);
|
||||
changed = true; // 标记为已更改
|
||||
}
|
||||
return changed; // 返回是否有删除发生
|
||||
}
|
||||
|
||||
//该实现参考了libdivide的算法
|
||||
static std::pair<int, int> computeMulhMagicNumbers(int divisor) {
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ void DCEContext::run(Function *func, AnalysisManager *AM, bool &changed) {
|
||||
}
|
||||
}
|
||||
}
|
||||
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func); // 如果有活跃指令,则标记为已更改
|
||||
}
|
||||
|
||||
// 判断指令是否是"天然活跃"的实现
|
||||
|
||||
@@ -39,7 +39,7 @@ bool GVN::runOnFunction(Function *func, AnalysisManager &AM) {
|
||||
}
|
||||
std::cout << "=== GVN completed for function: " << func->getName() << " ===" << std::endl;
|
||||
}
|
||||
|
||||
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func);
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
@@ -133,6 +133,7 @@ bool InductionVariableEliminationContext::run(Function* F, AnalysisManager& AM)
|
||||
printDebugInfo();
|
||||
}
|
||||
|
||||
modified |= SysYIROptUtils::eliminateRedundantPhisInFunction(F);
|
||||
return modified;
|
||||
}
|
||||
|
||||
|
||||
@@ -1357,9 +1357,8 @@ void SCCPContext::run(Function *func, AnalysisManager &AM) {
|
||||
bool changed_control_flow = SimplifyControlFlow(func);
|
||||
|
||||
// 如果任何一个阶段修改了 IR,标记分析结果为失效
|
||||
if (changed_constant_propagation || changed_control_flow) {
|
||||
// AM.invalidate(); // 假设有这样的方法来使所有分析结果失效
|
||||
}
|
||||
bool changed = changed_constant_propagation || changed_control_flow;
|
||||
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func);
|
||||
}
|
||||
|
||||
// SCCP Pass methods
|
||||
|
||||
Reference in New Issue
Block a user