From f317010d76894b95ebd93035742f6d8d80a2fec9 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Sun, 17 Aug 2025 17:42:19 +0800 Subject: [PATCH] =?UTF-8?q?[midend-Loop-LICM][fix]=E6=A3=80=E6=9F=A5load?= =?UTF-8?q?=E8=83=BD=E5=90=A6=E5=A4=96=E6=8F=90=E6=97=B6=E5=85=B6=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=9C=B0=E5=9D=80=E5=9C=A8=E5=BE=AA=E7=8E=AF=E4=B8=AD?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E4=BC=9A=E8=A2=AB=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=88=A4=E6=96=AD=E5=87=BD=E6=95=B0=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=AF=B9load=E5=86=85=E5=AD=98=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E7=9A=84=E5=BD=B1=E5=93=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pass/Analysis/LoopCharacteristics.cpp | 44 ++++++++++++++++++- src/midend/Pass/Pass.cpp | 14 +++--- 2 files changed, 50 insertions(+), 8 deletions(-) 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);