[midend-Loop-LICM][fix]检查load能否外提时其内存地址在循环中是否会被修改,需要判断函数调用对load内存地址的影响。
This commit is contained in:
@ -789,9 +789,10 @@ bool LoopCharacteristicsPass::isInvariantOperands(Instruction* inst, Loop* loop,
|
||||
|
||||
// 检查内存位置是否在循环中被修改
|
||||
bool LoopCharacteristicsPass::isMemoryLocationModifiedInLoop(Value* ptr, Loop* loop) {
|
||||
// 遍历循环中的所有Store指令,检查是否有对该内存位置的写入
|
||||
// 遍历循环中的所有指令,检查是否有对该内存位置的写入
|
||||
for (BasicBlock* bb : loop->getBlocks()) {
|
||||
for (auto& inst : bb->getInstructions()) {
|
||||
// 1. 检查直接的Store指令
|
||||
if (auto* storeInst = dynamic_cast<StoreInst*>(inst.get())) {
|
||||
Value* storeTar = storeInst->getPointer();
|
||||
|
||||
@ -812,6 +813,47 @@ bool LoopCharacteristicsPass::isMemoryLocationModifiedInLoop(Value* ptr, Loop* l
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 检查函数调用是否可能修改该内存位置
|
||||
else if (auto* callInst = dynamic_cast<CallInst*>(inst.get())) {
|
||||
Function* calledFunc = callInst->getCallee();
|
||||
|
||||
// 如果是纯函数,不会修改内存
|
||||
if (isPureFunction(calledFunc)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查函数参数中是否有该内存位置的指针
|
||||
for (size_t i = 1; i < callInst->getNumOperands(); ++i) { // 跳过函数指针
|
||||
Value* arg = callInst->getOperand(i);
|
||||
|
||||
// 检查参数是否是指针类型且可能指向该内存位置
|
||||
if (auto* ptrType = dynamic_cast<PointerType*>(arg->getType())) {
|
||||
// 使用别名分析检查
|
||||
if (aliasAnalysis) {
|
||||
auto aliasType = aliasAnalysis->queryAlias(ptr, arg);
|
||||
if (aliasType != AliasType::NO_ALIAS) {
|
||||
if (DEBUG) {
|
||||
std::cout << " Memory location " << ptr->getName()
|
||||
<< " may be modified by function call " << calledFunc->getName()
|
||||
<< " through parameter " << arg->getName() << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// 没有别名分析,检查精确匹配
|
||||
if (ptr == arg) {
|
||||
if (DEBUG) {
|
||||
std::cout << " Memory location " << ptr->getName()
|
||||
<< " may be modified by function call " << calledFunc->getName()
|
||||
<< " (exact match)" << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -161,14 +161,14 @@ void PassManager::runOptimizationPipeline(Module* moduleIR, IRBuilder* builderIR
|
||||
}
|
||||
|
||||
|
||||
// this->clearPasses();
|
||||
// this->addPass(&LICM::ID);
|
||||
// this->run();
|
||||
this->clearPasses();
|
||||
this->addPass(&LICM::ID);
|
||||
this->run();
|
||||
|
||||
// if(DEBUG) {
|
||||
// std::cout << "=== IR After LICM ===\n";
|
||||
// printPasses();
|
||||
// }
|
||||
if(DEBUG) {
|
||||
std::cout << "=== IR After LICM ===\n";
|
||||
printPasses();
|
||||
}
|
||||
|
||||
this->clearPasses();
|
||||
this->addPass(&LoopStrengthReduction::ID);
|
||||
|
||||
Reference in New Issue
Block a user