[midend-CFGOpt]修复部分指令删除逻辑错误

This commit is contained in:
rain2133
2025-08-11 18:40:58 +08:00
parent 038552f58b
commit 46179e3866

View File

@ -42,7 +42,7 @@ bool SysYCFGOptUtils::SysYDelInstAfterBr(Function *func) {
++Branchiter; ++Branchiter;
while (Branchiter != instructions.end()) { while (Branchiter != instructions.end()) {
changed = true; changed = true;
Branchiter = instructions.erase(Branchiter); Branchiter = SysYIROptUtils::usedelete(Branchiter); // 删除指令
} }
if (Branch) { // 更新前驱后继关系 if (Branch) { // 更新前驱后继关系
@ -91,7 +91,7 @@ bool SysYCFGOptUtils::SysYBlockMerge(Function *func) {
BasicBlock *block = blockiter->get(); BasicBlock *block = blockiter->get();
BasicBlock *nextBlock = blockiter->get()->getSuccessors()[0]; BasicBlock *nextBlock = blockiter->get()->getSuccessors()[0];
// auto nextarguments = nextBlock->getArguments(); // auto nextarguments = nextBlock->getArguments();
// 删除br指令 // 删除block的br指令
if (block->getNumInstructions() != 0) { if (block->getNumInstructions() != 0) {
auto thelastinstinst = block->terminator(); auto thelastinstinst = block->terminator();
if (thelastinstinst->get()->isUnconditional()) { if (thelastinstinst->get()->isUnconditional()) {
@ -103,14 +103,20 @@ bool SysYCFGOptUtils::SysYBlockMerge(Function *func) {
if (brinst->getThenBlock() == brinst->getElseBlock()) { if (brinst->getThenBlock() == brinst->getElseBlock()) {
thelastinstinst = SysYIROptUtils::usedelete(thelastinstinst); thelastinstinst = SysYIROptUtils::usedelete(thelastinstinst);
} }
else{
assert(false && "SysYBlockMerge: unexpected conditional branch with different then and else blocks");
}
} }
} }
// 将后继块的指令移动到当前块 // 将后继块的指令移动到当前块
// 并将后继块的父指针改为当前块 // 并将后继块的父指针改为当前块
for (auto institer = nextBlock->begin(); institer != nextBlock->end();) { for (auto institer = nextBlock->begin(); institer != nextBlock->end();) {
institer->get()->setParent(block); // institer->get()->setParent(block);
block->getInstructions().emplace_back(institer->release()); // block->getInstructions().emplace_back(institer->release());
institer = nextBlock->getInstructions().erase(institer); // 用usedelete删除会导致use关系被删除我只希望移动指令到当前块
// institer = SysYIROptUtils::usedelete(institer);
// institer = nextBlock->getInstructions().erase(institer);
nextBlock->moveInst(institer, block->getInstructions().end(), block);
} }
// 更新前驱后继关系,类似树节点操作 // 更新前驱后继关系,类似树节点操作
block->removeSuccessor(nextBlock); block->removeSuccessor(nextBlock);