Merge branch 'midend' into backend-O1-1

This commit is contained in:
Lixuanwang
2025-08-20 02:14:53 +08:00
10 changed files with 60 additions and 28 deletions

2
.gitignore vendored
View File

@@ -36,7 +36,7 @@ doxygen
!/testdata/functional/*.out !/testdata/functional/*.out
!/testdata/h_functional/*.out !/testdata/h_functional/*.out
!/testdata/performance/*.out testdata/performance/
build/ build/
.antlr .antlr
.vscode/ .vscode/

View File

@@ -1007,6 +1007,7 @@ class PhiInst : public Instruction {
void replaceIncomingBlock(BasicBlock *oldBlock, BasicBlock *newBlock, Value *newValue); void replaceIncomingBlock(BasicBlock *oldBlock, BasicBlock *newBlock, Value *newValue);
void refreshMap() { void refreshMap() {
blk2val.clear(); blk2val.clear();
vsize = getNumOperands() / 2;
for (unsigned i = 0; i < vsize; ++i) { for (unsigned i = 0; i < vsize; ++i) {
blk2val[getIncomingBlock(i)] = getIncomingValue(i); blk2val[getIncomingBlock(i)] = getIncomingValue(i);
} }

View File

@@ -109,6 +109,34 @@ public:
} }
// PHI指令消除相关方法
static bool eliminateRedundantPhisInFunction(Function* func){
bool changed = false;
std::vector<Instruction *> toDelete;
for (auto &bb : func->getBasicBlocks()) {
for (auto &inst : bb->getInstructions()) {
if (auto phi = dynamic_cast<PhiInst *>(inst.get())) {
auto incoming = phi->getIncomingValues();
if(DEBUG){
std::cout << "Checking Phi: " << phi->getName() << " with " << incoming.size() << " incoming values." << std::endl;
}
if (incoming.size() == 1) {
Value *singleVal = incoming[0].second;
inst->replaceAllUsesWith(singleVal);
toDelete.push_back(inst.get());
}
}
else
break; // 只处理Phi指令
}
}
for (auto *phi : toDelete) {
usedelete(phi);
changed = true; // 标记为已更改
}
return changed; // 返回是否有删除发生
}
//该实现参考了libdivide的算法 //该实现参考了libdivide的算法
static std::pair<int, int> computeMulhMagicNumbers(int divisor) { static std::pair<int, int> computeMulhMagicNumbers(int divisor) {

View File

@@ -757,7 +757,7 @@ void BinaryInst::print(std::ostream &os) const {
auto lhs_hash = std::hash<const void*>{}(static_cast<const void*>(getLhs())); auto lhs_hash = std::hash<const void*>{}(static_cast<const void*>(getLhs()));
auto rhs_hash = std::hash<const void*>{}(static_cast<const void*>(getRhs())); auto rhs_hash = std::hash<const void*>{}(static_cast<const void*>(getRhs()));
size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2); size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2);
std::string tmpName = "tmp_icmp_" + std::to_string(combined_hash % 1000000); std::string tmpName = "tmp_icmp_" + std::to_string(combined_hash % 1000000007);
os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " "; os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " ";
printOperand(os, getLhs()); printOperand(os, getLhs());
os << ", "; os << ", ";
@@ -772,7 +772,7 @@ void BinaryInst::print(std::ostream &os) const {
auto lhs_hash = std::hash<const void*>{}(static_cast<const void*>(getLhs())); auto lhs_hash = std::hash<const void*>{}(static_cast<const void*>(getLhs()));
auto rhs_hash = std::hash<const void*>{}(static_cast<const void*>(getRhs())); auto rhs_hash = std::hash<const void*>{}(static_cast<const void*>(getRhs()));
size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2); size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2);
std::string tmpName = "tmp_fcmp_" + std::to_string(combined_hash % 1000000); std::string tmpName = "tmp_fcmp_" + std::to_string(combined_hash % 1000000007);
os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " "; os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " ";
printOperand(os, getLhs()); printOperand(os, getLhs());
os << ", "; os << ", ";
@@ -834,7 +834,7 @@ void CondBrInst::print(std::ostream &os) const {
if (condName.empty()) { if (condName.empty()) {
// 使用条件值地址的哈希值作为唯一标识 // 使用条件值地址的哈希值作为唯一标识
auto ptr_hash = std::hash<const void*>{}(static_cast<const void*>(condition)); auto ptr_hash = std::hash<const void*>{}(static_cast<const void*>(condition));
condName = "const_" + std::to_string(ptr_hash % 100000); condName = "const_" + std::to_string(ptr_hash % 1000000007);
} }
// 组合指令地址、条件地址和目标块地址的哈希来确保唯一性 // 组合指令地址、条件地址和目标块地址的哈希来确保唯一性
@@ -843,7 +843,7 @@ void CondBrInst::print(std::ostream &os) const {
auto then_hash = std::hash<const void*>{}(static_cast<const void*>(getThenBlock())); auto then_hash = std::hash<const void*>{}(static_cast<const void*>(getThenBlock()));
auto else_hash = std::hash<const void*>{}(static_cast<const void*>(getElseBlock())); auto else_hash = std::hash<const void*>{}(static_cast<const void*>(getElseBlock()));
size_t combined_hash = inst_hash ^ (cond_hash << 1) ^ (then_hash << 2) ^ (else_hash << 3); size_t combined_hash = inst_hash ^ (cond_hash << 1) ^ (then_hash << 2) ^ (else_hash << 3);
std::string uniqueSuffix = std::to_string(combined_hash % 1000000); std::string uniqueSuffix = std::to_string(combined_hash % 1000000007);
os << "%tmp_cond_" << condName << "_" << uniqueSuffix << " = icmp ne i32 "; os << "%tmp_cond_" << condName << "_" << uniqueSuffix << " = icmp ne i32 ";
printOperand(os, condition); printOperand(os, condition);

View File

@@ -74,6 +74,7 @@ void DCEContext::run(Function *func, AnalysisManager *AM, bool &changed) {
} }
} }
} }
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func); // 如果有活跃指令,则标记为已更改
} }
// 判断指令是否是"天然活跃"的实现 // 判断指令是否是"天然活跃"的实现

View File

@@ -39,7 +39,7 @@ bool GVN::runOnFunction(Function *func, AnalysisManager &AM) {
} }
std::cout << "=== GVN completed for function: " << func->getName() << " ===" << std::endl; std::cout << "=== GVN completed for function: " << func->getName() << " ===" << std::endl;
} }
changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func);
return changed; return changed;
} }

View File

@@ -671,13 +671,13 @@ bool GlobalStrengthReductionContext::reduceDivision(BinaryInst *inst) {
} }
// x / c = x * magic_number (魔数乘法优化 - 使用libdivide算法) // x / c = x * magic_number (魔数乘法优化 - 使用libdivide算法)
if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) { // if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) {
// auto magicPair = computeMulhMagicNumbers(static_cast<int>(constVal)); // // auto magicPair = computeMulhMagicNumbers(static_cast<int>(constVal));
Value* magicResult = createMagicDivisionLibdivide(inst, static_cast<int>(constVal)); // Value* magicResult = createMagicDivisionLibdivide(inst, static_cast<int>(constVal));
replaceWithOptimized(inst, magicResult); // replaceWithOptimized(inst, magicResult);
divisionOptCount++; // divisionOptCount++;
return true; // return true;
} // }
return false; return false;
} }

View File

@@ -133,6 +133,7 @@ bool InductionVariableEliminationContext::run(Function* F, AnalysisManager& AM)
printDebugInfo(); printDebugInfo();
} }
modified |= SysYIROptUtils::eliminateRedundantPhisInFunction(F);
return modified; return modified;
} }

View File

@@ -661,9 +661,9 @@ bool StrengthReductionContext::replaceOriginalInstruction(StrengthReductionCandi
case StrengthReductionCandidate::DIVIDE_CONST: { case StrengthReductionCandidate::DIVIDE_CONST: {
// 任意常数除法 // 任意常数除法
builder->setPosition(candidate->containingBlock, // builder->setPosition(candidate->containingBlock,
candidate->containingBlock->findInstIterator(candidate->originalInst)); // candidate->containingBlock->findInstIterator(candidate->originalInst));
replacementValue = generateConstantDivisionReplacement(candidate, builder); // replacementValue = generateConstantDivisionReplacement(candidate, builder);
break; break;
} }
@@ -683,17 +683,19 @@ bool StrengthReductionContext::replaceOriginalInstruction(StrengthReductionCandi
); );
// 检查原值是否为负数 // 检查原值是否为负数
Value* zero = ConstantInteger::get(0); Value* shift31condidata = builder->createBinaryInst(
Value* isNegative = builder->createICmpLTInst(candidate->inductionVar, zero); Instruction::Kind::kSra, candidate->inductionVar->getType(),
candidate->inductionVar, ConstantInteger::get(31)
);
// 如果为负数,需要调整结果 // 如果为负数,需要调整结果
Value* adjustment = ConstantInteger::get(candidate->multiplier); Value* adjustment = builder->createAndInst(shift31condidata, maskConstant);
Value* adjustedTemp = builder->createAddInst(temp, adjustment); Value* adjustedTemp = builder->createAddInst(candidate->inductionVar, adjustment);
Value* adjustedResult = builder->createBinaryInst(
// 使用条件分支来模拟select操作 Instruction::Kind::kAnd, candidate->inductionVar->getType(),
// 为简化起见,这里先用一个更复杂但可工作的方式 adjustedTemp, maskConstant
// 实际应该创建条件分支,但这里先简化处理 );
replacementValue = temp; // 简化版本,假设大多数情况下不是负数 replacementValue = adjustedResult;
} else { } else {
// 非负数的取模,直接使用位与 // 非负数的取模,直接使用位与
replacementValue = builder->createBinaryInst( replacementValue = builder->createBinaryInst(

View File

@@ -1357,9 +1357,8 @@ void SCCPContext::run(Function *func, AnalysisManager &AM) {
bool changed_control_flow = SimplifyControlFlow(func); bool changed_control_flow = SimplifyControlFlow(func);
// 如果任何一个阶段修改了 IR标记分析结果为失效 // 如果任何一个阶段修改了 IR标记分析结果为失效
if (changed_constant_propagation || changed_control_flow) { bool changed = changed_constant_propagation || changed_control_flow;
// AM.invalidate(); // 假设有这样的方法来使所有分析结果失效 changed |= SysYIROptUtils::eliminateRedundantPhisInFunction(func);
}
} }
// SCCP Pass methods // SCCP Pass methods