[midend-LICM]优化了特征分析中对循环不变量的识别,实现了LICM遍,格式化副作用分析代码

This commit is contained in:
rain2133
2025-08-12 15:53:57 +08:00
parent 70f6a25ebc
commit f634273852
7 changed files with 523 additions and 360 deletions

View File

@@ -0,0 +1,84 @@
#include "LICM.h"
#include "IR.h"
extern int DEBUG;
namespace sysy {
void *LICM::ID = (void *)&LICM::ID;
bool LICMContext::run() { return hoistInstructions(); }
bool LICMContext::hoistInstructions() {
bool changed = false;
BasicBlock *preheader = loop->getPreHeader();
if (!preheader || !chars){
if(DEBUG) {
std::cerr << "LICM: No preheader or loop characteristics found, skipping hoisting." << std::endl;
}
return false;
}
for (auto *inst : chars->invariantInsts) {
if (!inst) {
if(DEBUG) {
std::cerr << "LICM: Invalid instruction found, skipping." << std::endl;
}
continue; // 跳过无效指令
}
else{
if(DEBUG) {
std::cout << "LICM: Processing instruction " << inst->getName() << " for hoisting." << std::endl;
}
}
BasicBlock *parent = inst->getParent();
// 只外提当前还在循环体内的指令
if (parent && loop->contains(parent)) {
// 获取源和槽的迭代器并移动指令到前置块
if(DEBUG) {
std::cout << "LICM: Hoisting instruction " << inst->getName() << " from "
<< parent->getName() << " to preheader " << preheader->getName() << std::endl;
}
auto sourcePos = parent->findInstIterator(inst);
auto targetPos = preheader->terminator();
parent->moveInst(sourcePos, targetPos, preheader);
changed = true;
}
}
return changed;
}
// ---- LICM Pass Implementation ----
bool LICM::runOnFunction(Function *F, AnalysisManager &AM) {
auto *loopAnalysis = AM.getAnalysisResult<LoopAnalysisResult, LoopAnalysisPass>(F);
auto *loopCharsResult = AM.getAnalysisResult<LoopCharacteristicsResult, LoopCharacteristicsPass>(F);
if (!loopAnalysis || !loopCharsResult)
return false;
bool changed = false;
// 对每个函数内的所有循环做处理
for (const auto &loop_ptr : loopAnalysis->getAllLoops()) {
Loop *loop = loop_ptr.get();
if(DEBUG){
std::cout << "LICM: Processing loop in function " << F->getName() << ": " << loop->getName() << std::endl;
}
const LoopCharacteristics *chars = loopCharsResult->getCharacteristics(loop);
if (!chars || !loop->getPreHeader())
continue; // 没有分析结果或没有前置块则跳过
LICMContext ctx(F, loop, builder, chars);
changed |= ctx.run();
}
return changed;
}
void LICM::getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const {
analysisDependencies.insert(&LoopAnalysisPass::ID);
analysisDependencies.insert(&LoopCharacteristicsPass::ID);
analysisInvalidations.insert(&LoopCharacteristicsPass::ID);
analysisInvalidations.insert(&LivenessAnalysisPass::ID);
}
} // namespace sysy