[deploy]禁用除法强度削弱优化,禁用GVN对load和GEP指令进行优化

This commit is contained in:
rain2133
2025-08-20 10:34:06 +08:00
parent 8ba2e8397c
commit bfc4071c36
3 changed files with 61 additions and 13 deletions

View File

@@ -237,9 +237,30 @@ bool GVNContext::processInstruction(Instruction* inst) {
return false; return false;
} }
// 暂时完全禁用LoadInst的GVN优化因为存在内存安全性问题
if (dynamic_cast<LoadInst*>(inst)) {
if (DEBUG) {
std::cout << " Skipping LoadInst GVN optimization for safety: " << inst->getName() << std::endl;
}
getValueNumber(inst);
return false;
}
// 暂时完全禁用GetElementPtrInst的GVN优化因为可能存在内存安全性问题
if (dynamic_cast<GetElementPtrInst*>(inst)) {
if (DEBUG) {
std::cout << " Skipping GetElementPtrInst GVN optimization for safety: " << inst->getName() << std::endl;
}
getValueNumber(inst);
return false;
}
if (DEBUG) { if (DEBUG) {
std::cout << " Processing optimizable instruction: " << inst->getName() std::cout << " Processing optimizable instruction: " << inst->getName()
<< " (kind: " << static_cast<int>(inst->getKind()) << ")" << std::endl; << " (kind: " << static_cast<int>(inst->getKind()) << ")" << std::endl;
if (auto loadInst = dynamic_cast<LoadInst*>(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); Value* existing = findExistingValue(exprKey, inst);
if (existing && existing != inst) { if (existing && existing != inst) {
if (DEBUG) {
std::cout << " Found potential replacement: " << existing->getName()
<< " for " << inst->getName() << std::endl;
if (auto loadInst = dynamic_cast<LoadInst*>(inst)) {
std::cout << " Current instruction is LoadInst" << std::endl;
if (auto existingLoad = dynamic_cast<LoadInst*>(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<Instruction*>(existing)) { if (auto existingInst = dynamic_cast<Instruction*>(existing)) {
if (dominates(existingInst, inst)) { if (dominates(existingInst, inst)) {
@@ -350,8 +385,17 @@ Value* GVNContext::findExistingValue(const std::string& exprKey, Instruction* in
if (auto loadInst = dynamic_cast<LoadInst*>(inst)) { if (auto loadInst = dynamic_cast<LoadInst*>(inst)) {
if (auto existingLoad = dynamic_cast<LoadInst*>(existing)) { if (auto existingLoad = dynamic_cast<LoadInst*>(existing)) {
if (!isMemorySafe(existingLoad, loadInst)) { if (!isMemorySafe(existingLoad, loadInst)) {
if (DEBUG) {
std::cout << " Memory safety check failed for load optimization" << std::endl;
}
return nullptr; 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 (earlierBB != laterBB) {
// 跨基本块的情况需要更复杂的分析,暂时保守处理 // 跨基本块的情况需要更复杂的分析,暂时保守处理
if (DEBUG) {
std::cout << " Memory safety check: Cross-block load optimization disabled" << std::endl;
}
return false; return false;
} }

View File

@@ -337,13 +337,13 @@ bool GlobalStrengthReductionContext::optimizeDivision(BinaryInst *inst) {
} }
// x / x = 1 (如果x != 0且没有副作用) // x / x = 1 (如果x != 0且没有副作用)
if (lhs == rhs && hasOnlyLocalUses(dynamic_cast<Instruction*>(lhs))) { // if (lhs == rhs && hasOnlyLocalUses(dynamic_cast<Instruction*>(lhs))) {
if (DEBUG) { // if (DEBUG) {
std::cout << " Algebraic: " << inst->getName() << " = x / x -> 1" << std::endl; // std::cout << " Algebraic: " << inst->getName() << " = x / x -> 1" << std::endl;
} // }
replaceWithOptimized(inst, getConstantInt(1)); // replaceWithOptimized(inst, getConstantInt(1));
return true; // return true;
} // }
return false; return false;
} }
@@ -440,7 +440,7 @@ bool GlobalStrengthReductionContext::tryStrengthReduction(Instruction *inst) {
case Instruction::kMul: case Instruction::kMul:
return reduceMultiplication(binary); return reduceMultiplication(binary);
case Instruction::kDiv: case Instruction::kDiv:
return reduceDivision(binary); // return reduceDivision(binary);
default: default:
return false; return false;
} }

View File

@@ -262,9 +262,13 @@ std::unique_ptr<StrengthReductionCandidate>
StrengthReductionContext::isStrengthReductionCandidate(Instruction* inst, Loop* loop) { StrengthReductionContext::isStrengthReductionCandidate(Instruction* inst, Loop* loop) {
auto kind = inst->getKind(); auto kind = inst->getKind();
// 支持乘法、除法、取模指令 // 禁用除法优化 - 只支持乘法和取模指令
if (kind == Instruction::Kind::kDiv) {
return nullptr; // 禁用除法强度削弱
}
// 支持乘法、取模指令
if (kind != Instruction::Kind::kMul && if (kind != Instruction::Kind::kMul &&
kind != Instruction::Kind::kDiv &&
kind != Instruction::Kind::kRem) { kind != Instruction::Kind::kRem) {
return nullptr; return nullptr;
} }
@@ -293,9 +297,6 @@ StrengthReductionContext::isStrengthReductionCandidate(Instruction* inst, Loop*
case Instruction::Kind::kMul: case Instruction::Kind::kMul:
opType = StrengthReductionCandidate::MULTIPLY; opType = StrengthReductionCandidate::MULTIPLY;
break; break;
case Instruction::Kind::kDiv:
opType = StrengthReductionCandidate::DIVIDE;
break;
case Instruction::Kind::kRem: case Instruction::Kind::kRem:
opType = StrengthReductionCandidate::REMAINDER; opType = StrengthReductionCandidate::REMAINDER;
break; break;