diff --git a/src/midend/Pass/Optimize/GVN.cpp b/src/midend/Pass/Optimize/GVN.cpp index 047ae52..2c9da1d 100644 --- a/src/midend/Pass/Optimize/GVN.cpp +++ b/src/midend/Pass/Optimize/GVN.cpp @@ -237,9 +237,30 @@ bool GVNContext::processInstruction(Instruction* inst) { return false; } + // 暂时完全禁用LoadInst的GVN优化,因为存在内存安全性问题 + if (dynamic_cast(inst)) { + if (DEBUG) { + std::cout << " Skipping LoadInst GVN optimization for safety: " << inst->getName() << std::endl; + } + getValueNumber(inst); + return false; + } + + // 暂时完全禁用GetElementPtrInst的GVN优化,因为可能存在内存安全性问题 + if (dynamic_cast(inst)) { + if (DEBUG) { + std::cout << " Skipping GetElementPtrInst GVN optimization for safety: " << inst->getName() << std::endl; + } + getValueNumber(inst); + return false; + } + if (DEBUG) { std::cout << " Processing optimizable instruction: " << inst->getName() << " (kind: " << static_cast(inst->getKind()) << ")" << std::endl; + if (auto loadInst = dynamic_cast(inst)) { + std::cout << " This is a LOAD instruction in block: " << inst->getParent()->getName() << std::endl; + } } // 构建表达式键 @@ -257,6 +278,20 @@ bool GVNContext::processInstruction(Instruction* inst) { // 查找已存在的等价值 Value* existing = findExistingValue(exprKey, inst); if (existing && existing != inst) { + if (DEBUG) { + std::cout << " Found potential replacement: " << existing->getName() + << " for " << inst->getName() << std::endl; + if (auto loadInst = dynamic_cast(inst)) { + std::cout << " Current instruction is LoadInst" << std::endl; + if (auto existingLoad = dynamic_cast(existing)) { + std::cout << " Existing value is also LoadInst in block: " + << existingLoad->getParent()->getName() << std::endl; + } else { + std::cout << " Existing value is NOT LoadInst, type: " + << typeid(*existing).name() << std::endl; + } + } + } // 检查支配关系 if (auto existingInst = dynamic_cast(existing)) { if (dominates(existingInst, inst)) { @@ -350,8 +385,17 @@ Value* GVNContext::findExistingValue(const std::string& exprKey, Instruction* in if (auto loadInst = dynamic_cast(inst)) { if (auto existingLoad = dynamic_cast(existing)) { if (!isMemorySafe(existingLoad, loadInst)) { + if (DEBUG) { + std::cout << " Memory safety check failed for load optimization" << std::endl; + } return nullptr; } + } else { + // existing不是load指令,但当前指令是load,不能替换 + if (DEBUG) { + std::cout << " Cannot replace load with non-load instruction" << std::endl; + } + return nullptr; } } @@ -409,6 +453,9 @@ bool GVNContext::isMemorySafe(LoadInst* earlierLoad, LoadInst* laterLoad) { if (earlierBB != laterBB) { // 跨基本块的情况需要更复杂的分析,暂时保守处理 + if (DEBUG) { + std::cout << " Memory safety check: Cross-block load optimization disabled" << std::endl; + } return false; } diff --git a/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp b/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp index 404cf7b..8ed02f5 100644 --- a/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp +++ b/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp @@ -337,13 +337,13 @@ bool GlobalStrengthReductionContext::optimizeDivision(BinaryInst *inst) { } // x / x = 1 (如果x != 0且没有副作用) - if (lhs == rhs && hasOnlyLocalUses(dynamic_cast(lhs))) { - if (DEBUG) { - std::cout << " Algebraic: " << inst->getName() << " = x / x -> 1" << std::endl; - } - replaceWithOptimized(inst, getConstantInt(1)); - return true; - } + // if (lhs == rhs && hasOnlyLocalUses(dynamic_cast(lhs))) { + // if (DEBUG) { + // std::cout << " Algebraic: " << inst->getName() << " = x / x -> 1" << std::endl; + // } + // replaceWithOptimized(inst, getConstantInt(1)); + // return true; + // } return false; } @@ -440,7 +440,7 @@ bool GlobalStrengthReductionContext::tryStrengthReduction(Instruction *inst) { case Instruction::kMul: return reduceMultiplication(binary); case Instruction::kDiv: - return reduceDivision(binary); + // return reduceDivision(binary); default: return false; } diff --git a/src/midend/Pass/Optimize/LoopStrengthReduction.cpp b/src/midend/Pass/Optimize/LoopStrengthReduction.cpp index 33751df..bdc80c0 100644 --- a/src/midend/Pass/Optimize/LoopStrengthReduction.cpp +++ b/src/midend/Pass/Optimize/LoopStrengthReduction.cpp @@ -262,9 +262,13 @@ std::unique_ptr StrengthReductionContext::isStrengthReductionCandidate(Instruction* inst, Loop* loop) { auto kind = inst->getKind(); - // 支持乘法、除法、取模指令 + // 禁用除法优化 - 只支持乘法和取模指令 + if (kind == Instruction::Kind::kDiv) { + return nullptr; // 禁用除法强度削弱 + } + + // 支持乘法、取模指令 if (kind != Instruction::Kind::kMul && - kind != Instruction::Kind::kDiv && kind != Instruction::Kind::kRem) { return nullptr; } @@ -293,9 +297,6 @@ StrengthReductionContext::isStrengthReductionCandidate(Instruction* inst, Loop* case Instruction::Kind::kMul: opType = StrengthReductionCandidate::MULTIPLY; break; - case Instruction::Kind::kDiv: - opType = StrengthReductionCandidate::DIVIDE; - break; case Instruction::Kind::kRem: opType = StrengthReductionCandidate::REMAINDER; break;