diff --git a/src/midend/Pass/Analysis/LoopCharacteristics.cpp b/src/midend/Pass/Analysis/LoopCharacteristics.cpp index eb58af1..5d2f2aa 100644 --- a/src/midend/Pass/Analysis/LoopCharacteristics.cpp +++ b/src/midend/Pass/Analysis/LoopCharacteristics.cpp @@ -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(inst.get())) { Value* storeTar = storeInst->getPointer(); @@ -812,6 +813,47 @@ bool LoopCharacteristicsPass::isMemoryLocationModifiedInLoop(Value* ptr, Loop* l } } } + + // 2. 检查函数调用是否可能修改该内存位置 + else if (auto* callInst = dynamic_cast(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(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; diff --git a/src/midend/Pass/Pass.cpp b/src/midend/Pass/Pass.cpp index 69f9790..09de26e 100644 --- a/src/midend/Pass/Pass.cpp +++ b/src/midend/Pass/Pass.cpp @@ -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);