基本完成CFG优化(IR修复)
This commit is contained in:
@ -17,14 +17,6 @@
|
|||||||
|
|
||||||
namespace sysy {
|
namespace sysy {
|
||||||
|
|
||||||
void SysYOptPre::SysYOptimizateAll() -> void {
|
|
||||||
SysYDelAfterBr();
|
|
||||||
SysYBlockMerge();
|
|
||||||
SysYDelNoPreBLock();
|
|
||||||
SysYDelEmptyBlock();
|
|
||||||
SysYAddReturn();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* use删除operand,以免扰乱后续分析
|
* use删除operand,以免扰乱后续分析
|
||||||
* instr: 要删除的指令
|
* instr: 要删除的指令
|
||||||
@ -61,20 +53,20 @@ void SysYOptPre::SysYDelInstAfterBr() {
|
|||||||
Branchiter = instructions.erase(Branchiter);
|
Branchiter = instructions.erase(Branchiter);
|
||||||
|
|
||||||
if (Branch) { // 更新前驱后继关系
|
if (Branch) { // 更新前驱后继关系
|
||||||
auto thelastinst = basicBlock->getInstructions().end();
|
auto thelastinstinst = basicBlock->getInstructions().end();
|
||||||
--thelastinst;
|
--thelastinstinst;
|
||||||
auto &Successors = basicBlock->getSuccessors();
|
auto &Successors = basicBlock->getSuccessors();
|
||||||
for (auto iterSucc = Successors.begin(); iterSucc != Successors.end();) {
|
for (auto iterSucc = Successors.begin(); iterSucc != Successors.end();) {
|
||||||
(*iterSucc)->removePredecessor(basicBlock.get());
|
(*iterSucc)->removePredecessor(basicBlock.get());
|
||||||
basicBlock->removeSuccessor(*iterSucc);
|
basicBlock->removeSuccessor(*iterSucc);
|
||||||
}
|
}
|
||||||
if (thelastinst->get()->isUnconditional()) {
|
if (thelastinstinst->get()->isUnconditional()) {
|
||||||
BasicBlock* branchBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
BasicBlock* branchBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(0));
|
||||||
basicBlock->addSuccessor(branchBlock);
|
basicBlock->addSuccessor(branchBlock);
|
||||||
branchBlock->addPredecessor(basicBlock.get());
|
branchBlock->addPredecessor(basicBlock.get());
|
||||||
} else if (thelastinst->get()->isConditional()) {
|
} else if (thelastinstinst->get()->isConditional()) {
|
||||||
BasicBlock* thenBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
BasicBlock* thenBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(1));
|
||||||
BasicBlock* elseBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
BasicBlock* elseBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(2));
|
||||||
basicBlock->addSuccessor(thenBlock);
|
basicBlock->addSuccessor(thenBlock);
|
||||||
basicBlock->addSuccessor(elseBlock);
|
basicBlock->addSuccessor(elseBlock);
|
||||||
thenBlock->addPredecessor(basicBlock.get());
|
thenBlock->addPredecessor(basicBlock.get());
|
||||||
@ -90,7 +82,7 @@ void SysYOptPre::SysYBlockMerge() {
|
|||||||
auto &functions = pModule->getFunctions(); //std::map<std::string, std::unique_ptr<Function>>
|
auto &functions = pModule->getFunctions(); //std::map<std::string, std::unique_ptr<Function>>
|
||||||
for (auto &function : functions) {
|
for (auto &function : functions) {
|
||||||
// auto basicBlocks = function.second->getBasicBlocks();
|
// auto basicBlocks = function.second->getBasicBlocks();
|
||||||
Function* func = function.second.get();
|
auto &func = function.second;
|
||||||
for (auto blockiter = func->getBasicBlocks().begin();
|
for (auto blockiter = func->getBasicBlocks().begin();
|
||||||
blockiter != func->getBasicBlocks().end();) {
|
blockiter != func->getBasicBlocks().end();) {
|
||||||
if (blockiter->get()->getNumSuccessors() == 1) {
|
if (blockiter->get()->getNumSuccessors() == 1) {
|
||||||
@ -104,16 +96,16 @@ void SysYOptPre::SysYBlockMerge() {
|
|||||||
auto nextarguments = nextBlock->getArguments();
|
auto nextarguments = nextBlock->getArguments();
|
||||||
// 删除br指令
|
// 删除br指令
|
||||||
if (block->getNumInstructions() != 0) {
|
if (block->getNumInstructions() != 0) {
|
||||||
auto thelastinst = block->end();
|
auto thelastinstinst = block->end();
|
||||||
(--thelastinst);
|
(--thelastinstinst);
|
||||||
if (thelastinst->get()->isUnconditional()) {
|
if (thelastinstinst->get()->isUnconditional()) {
|
||||||
usedelete(thelastinst->get());
|
usedelete(thelastinstinst->get());
|
||||||
block->getInstructions().erase(thelastinst);
|
block->getInstructions().erase(thelastinstinst);
|
||||||
} else if (thelastinst->get()->isConditional()) {
|
} else if (thelastinstinst->get()->isConditional()) {
|
||||||
// 如果是条件分支,判断条件是否相同,主要优化相同布尔表达式
|
// 如果是条件分支,判断条件是否相同,主要优化相同布尔表达式
|
||||||
if (thelastinst->get()->getOperand(1)->getName() == thelastinst->get()->getOperand(1)->getName()) {
|
if (thelastinstinst->get()->getOperand(1)->getName() == thelastinstinst->get()->getOperand(1)->getName()) {
|
||||||
usedelete(thelastinst->get());
|
usedelete(thelastinstinst->get());
|
||||||
block->getInstructions().erase(thelastinst);
|
block->getInstructions().erase(thelastinstinst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,11 +117,12 @@ void SysYOptPre::SysYBlockMerge() {
|
|||||||
institer = nextBlock->getInstructions().erase(institer);
|
institer = nextBlock->getInstructions().erase(institer);
|
||||||
}
|
}
|
||||||
// 合并参数
|
// 合并参数
|
||||||
|
// TODO:是否需要去重?
|
||||||
for (auto &argm : nextarguments) {
|
for (auto &argm : nextarguments) {
|
||||||
argm->setParent(block);
|
argm->setParent(block);
|
||||||
block->insertArgument(argm);
|
block->insertArgument(argm);
|
||||||
}
|
}
|
||||||
// // // 改变block 和 next 之间的关系
|
// 更新前驱后继关系,类似树节点操作
|
||||||
block->removeSuccessor(nextBlock);
|
block->removeSuccessor(nextBlock);
|
||||||
nextBlock->removePredecessor(block);
|
nextBlock->removePredecessor(block);
|
||||||
std::list<BasicBlock *> succshoulddel;
|
std::list<BasicBlock *> succshoulddel;
|
||||||
@ -142,16 +135,8 @@ void SysYOptPre::SysYBlockMerge() {
|
|||||||
nextBlock->removeSuccessor(del);
|
nextBlock->removeSuccessor(del);
|
||||||
}
|
}
|
||||||
|
|
||||||
// // // 添加block 和 next 的后继 之间的关系
|
func->removeBasicBlock(nextBlock);
|
||||||
// block->addSuccessor(nextBlock->getSuccessors()[0]);
|
|
||||||
// nextBlock->getSuccessors()[0]->addPredecessor(blockiter->get());
|
|
||||||
// // // 删除next 和 next后继的关系
|
|
||||||
// nextBlock->getSuccessors()[0]->removePredecessor(nextBlock);
|
|
||||||
// nextBlock->removeSuccessor(nextBlock->getSuccessors()[0]);
|
|
||||||
// std::cout << nextBlock->getName() << std::endl;
|
|
||||||
|
|
||||||
function.second->removeBasicBlock(nextBlock);
|
|
||||||
// ++blockiter;
|
|
||||||
} else {
|
} else {
|
||||||
blockiter++;
|
blockiter++;
|
||||||
}
|
}
|
||||||
@ -162,12 +147,344 @@ void SysYOptPre::SysYBlockMerge() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 删除无前驱块,兼容SSA后的处理
|
||||||
void SysYOptPre::SysYDelNoPreBLock() {
|
void SysYOptPre::SysYDelNoPreBLock() {
|
||||||
|
|
||||||
|
auto &functions = pModule->getFunctions(); // std::map<std::string, std::unique_ptr<sysy::Function>>
|
||||||
|
for (auto &function : functions) {
|
||||||
|
auto &func = function.second;
|
||||||
|
|
||||||
|
for (auto &block : func->getBasicBlocks()) {
|
||||||
|
block->setreachableFalse();
|
||||||
|
}
|
||||||
|
// 对函数基本块做一个拓扑排序,排查不可达基本块
|
||||||
|
auto entryBlock = func->getEntryBlock();
|
||||||
|
entryBlock->setreachableTrue();
|
||||||
|
std::queue<BasicBlock *> blockqueue;
|
||||||
|
blockqueue.push(entryBlock);
|
||||||
|
while (!blockqueue.empty()) {
|
||||||
|
auto block = blockqueue.front();
|
||||||
|
blockqueue.pop();
|
||||||
|
for (auto &succ : block->getSuccessors()) {
|
||||||
|
if (!succ->getreachable()) {
|
||||||
|
succ->setreachableTrue();
|
||||||
|
blockqueue.push(succ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除不可达基本块指令
|
||||||
|
for (auto blockIter = func->getBasicBlocks().begin(); blockIter != func->getBasicBlocks().end();blockIter++) {
|
||||||
|
|
||||||
|
if (!blockIter->get()->getreachable())
|
||||||
|
for (auto &iterInst : blockIter->get()->getInstructions())
|
||||||
|
usedelete(iterInst.get());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (auto blockIter = func->getBasicBlocks().begin(); blockIter != func->getBasicBlocks().end();blockIter++) {
|
||||||
|
if (!blockIter->get()->getreachable()) {
|
||||||
|
//// 处理phi指令
|
||||||
|
// 删除不可达基本块的phi指令的操作数
|
||||||
|
for (auto succblock : blockIter->get()->getSuccessors()) {
|
||||||
|
int indexphi = 1;
|
||||||
|
for (auto pred : succblock->getPredecessors()) {
|
||||||
|
if (pred == blockIter->get()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
indexphi++;
|
||||||
|
}
|
||||||
|
for (auto &phiinst : succblock->getInstructions()) {
|
||||||
|
if (phiinst->getKind() != Instruction::kPhi) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
phiinst->removeOperand(indexphi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 删除不可达基本块
|
||||||
|
func->removeBasicBlock(blockIter->get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SysyOptimization::SysyDelEmptyBlock() {
|
||||||
|
auto &functions = pModule->getFunctions();
|
||||||
|
for (auto &function : functions) {
|
||||||
|
// 收集不可达基本块
|
||||||
|
// 这里的不可达基本块是指没有实际指令的基本块
|
||||||
|
// 当一个基本块没有实际指令例如只有phi指令和一个uncondbr指令时,也会被视作不可达
|
||||||
|
auto basicBlocks = function.second->getBasicBlocks();
|
||||||
|
std::map<sysy::BasicBlock *, BasicBlock *> EmptyBlocks;
|
||||||
|
// 空块儿和后继的基本块的映射
|
||||||
|
for (auto &basicBlock : basicBlocks) {
|
||||||
|
if (basicBlock->getNumInstructions() == 0) {
|
||||||
|
if (basicBlock->getNumSuccessors() == 1) {
|
||||||
|
EmptyBlocks[basicBlock.get()] = basicBlock->getSuccessors().front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// 如果只有phi指令和一个uncondbr。(phi)*(uncondbr)?
|
||||||
|
// 判断除了最后一个指令之外是不是只有phi指令
|
||||||
|
bool onlyPhi = true;
|
||||||
|
for (auto &inst : basicBlock->getInstructions()) {
|
||||||
|
if (!inst->isPhi() && !inst->isUnconditional()) {
|
||||||
|
onlyPhi = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(onlyPhi)
|
||||||
|
EmptyBlocks[basicBlock.get()] = basicBlock->getSuccessors().front();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
// 更新基本块信息,增加必要指令
|
||||||
|
for (auto &basicBlock : basicBlocks) {
|
||||||
|
// 把空块转换成只有跳转指令的不可达块
|
||||||
|
if (distance(basicBlock->begin(), basicBlock->end()) == 0) {
|
||||||
|
if (basicBlock->getNumSuccessors() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (basicBlock->getNumSuccessors() > 1) {
|
||||||
|
assert("");
|
||||||
|
}
|
||||||
|
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||||
|
pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto thelastinst = basicBlock->getInstructions().end();
|
||||||
|
--thelastinst;
|
||||||
|
|
||||||
|
// 根据br指令传递的后继块信息,跳过空块链
|
||||||
|
if (thelastinst->get()->getKind()->isUnconditional()) {
|
||||||
|
BasicBlock* OldBrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||||
|
BasicBlock *thelastBlockOld = nullptr;
|
||||||
|
// 如果空块链表为多个块
|
||||||
|
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))) !=
|
||||||
|
EmptyBlocks.end()) {
|
||||||
|
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||||
|
thelastinst->get()->replaceOperand(0, EmptyBlocks[thelastBlockOld]);
|
||||||
|
}
|
||||||
|
|
||||||
|
basicBlock->removeSuccessor(OldBrBlock);
|
||||||
|
OldBrBlock->removePredecessor(basicBlock.get());
|
||||||
|
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)));
|
||||||
|
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->addPredecessor(basicBlock.get());
|
||||||
|
|
||||||
|
if (thelastBlockOld != nullptr) {
|
||||||
|
int indexphi = 0;
|
||||||
|
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors()) {
|
||||||
|
if (pred == thelastBlockOld) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
indexphi++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新phi指令的操作数
|
||||||
|
// 移除thelastBlockOld对应的phi操作数
|
||||||
|
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->getInstructions()) {
|
||||||
|
if (InstInNew->isPhi()) {
|
||||||
|
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (thelastinst->get()->getKind() == Instruction::kCondBr) {
|
||||||
|
auto OldThenBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||||
|
auto OldElseBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
||||||
|
|
||||||
|
BasicBlock *thelastBlockOld = nullptr;
|
||||||
|
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))) !=
|
||||||
|
EmptyBlocks.end()) {
|
||||||
|
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||||
|
thelastinst->get()->replaceOperand(
|
||||||
|
1, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))]);
|
||||||
|
}
|
||||||
|
basicBlock->removeSuccessor(OldThenBlock);
|
||||||
|
OldThenBlock->removePredecessor(basicBlock.get());
|
||||||
|
// 处理 then 和 else 分支合并的情况
|
||||||
|
if (dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)) ==
|
||||||
|
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) {
|
||||||
|
auto thebrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||||
|
usedelete(thelastinst->get());
|
||||||
|
thelastinst = basicBlock->getInstructions().erase(thelastinst);
|
||||||
|
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||||
|
pBuilder->createUncondBrInst(thebrBlock, {});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)));
|
||||||
|
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->addPredecessor(basicBlock.get());
|
||||||
|
// auto indexInNew = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors().
|
||||||
|
|
||||||
|
if (thelastBlockOld != nullptr) {
|
||||||
|
int index = 0;
|
||||||
|
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->getPredecessors()) {
|
||||||
|
if (pred == thelastBlockOld) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->getInstructions()) {
|
||||||
|
if (InstInNew->isPhi()) {
|
||||||
|
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
thelastBlockOld = nullptr;
|
||||||
|
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) !=
|
||||||
|
EmptyBlocks.end()) {
|
||||||
|
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
||||||
|
thelastinst->get()->replaceOperand(
|
||||||
|
2, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))]);
|
||||||
|
}
|
||||||
|
basicBlock->removeSuccessor(OldElseBlock);
|
||||||
|
OldElseBlock->removePredecessor(basicBlock.get());
|
||||||
|
// 处理 then 和 else 分支合并的情况
|
||||||
|
if (dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)) ==
|
||||||
|
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) {
|
||||||
|
auto thebrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||||
|
usedelete(thelastinst->get());
|
||||||
|
thelastinst = basicBlock->getInstructions().erase(thelastinst);
|
||||||
|
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||||
|
pBuilder->createUncondBrInst(thebrBlock, {});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2)));
|
||||||
|
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->addPredecessor(basicBlock.get());
|
||||||
|
|
||||||
|
if (thelastBlockOld != nullptr) {
|
||||||
|
int index = 0;
|
||||||
|
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->getPredecessors()) {
|
||||||
|
if (pred == thelastBlockOld) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->getInstructions()) {
|
||||||
|
if (InstInNew->isPhi()) {
|
||||||
|
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (basicBlock->getNumSuccessors() == 1) {
|
||||||
|
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||||
|
pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||||
|
auto thelastinst = basicBlock->getInstructions().end();
|
||||||
|
(--thelastinst);
|
||||||
|
auto OldBrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||||
|
sysy::BasicBlock *thelastBlockOld = nullptr;
|
||||||
|
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))) !=
|
||||||
|
EmptyBlocks.end()) {
|
||||||
|
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||||
|
|
||||||
|
thelastinst->get()->replaceOperand(
|
||||||
|
0, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))]);
|
||||||
|
}
|
||||||
|
|
||||||
|
basicBlock->removeSuccessor(OldBrBlock);
|
||||||
|
OldBrBlock->removePredecessor(basicBlock.get());
|
||||||
|
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)));
|
||||||
|
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->addPredecessor(basicBlock.get());
|
||||||
|
if (thelastBlockOld != nullptr) {
|
||||||
|
int index = 0;
|
||||||
|
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors()) {
|
||||||
|
if (pred == thelastBlockOld) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getInstructions()) {
|
||||||
|
if (InstInNew->isPhi()) {
|
||||||
|
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto iter = function.second->getBasicBlocks().begin(); iter != function.second->getBasicBlocks().end();) {
|
||||||
|
|
||||||
|
if (EmptyBlocks.find(iter->get()) != EmptyBlocks.end()) {
|
||||||
|
// EntryBlock跳过
|
||||||
|
if (iter->get() == function.second->getEntryBlock()) {
|
||||||
|
++iter;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &iterInst : iter->get()->getInstructions())
|
||||||
|
usedelete(iterInst.get());
|
||||||
|
// 删除不可达基本块的phi指令的操作数
|
||||||
|
for (auto &succ : iter->get()->getSuccessors()) {
|
||||||
|
int index = 0;
|
||||||
|
for (auto &pred : succ->getPredecessors()) {
|
||||||
|
if (pred == iter->get()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &instinsucc : succ->getInstructions()) {
|
||||||
|
if (instinsucc->isPhi()) {
|
||||||
|
dynamic_cast<PhiInst *>(instinsucc.get())->removeOperand(index);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function.second->removeBasicBlock((iter++)->get());
|
||||||
|
} else {
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果函数没有返回指令,则添加一个默认返回指令(主要解决void函数没有返回指令的问题)
|
||||||
void SysYOptPre::SysYAddReturn() {
|
void SysYOptPre::SysYAddReturn() {
|
||||||
|
auto &functions = pModule->getFunctions();
|
||||||
|
for (auto &function : functions) {
|
||||||
|
auto &func = function.second;
|
||||||
|
auto &basicBlocks = func->getBasicBlocks();
|
||||||
|
for (auto &block : basicBlocks) {
|
||||||
|
if (block->getNumSuccessors() == 0) {
|
||||||
|
// 如果基本块没有后继块,则添加一个返回指令
|
||||||
|
if (block->getNumInstructions() == 0) {
|
||||||
|
pBuilder->setPosition(block.get(), block->end());
|
||||||
|
pBuilder->createReturnInst({});
|
||||||
|
}
|
||||||
|
auto thelastinst = block->getInstructions().end();
|
||||||
|
--thelastinst;
|
||||||
|
if (thelastinst->get()->getKind() != Instruction::kReturn) {
|
||||||
|
pBuilder->setPosition(block.get(), block->end());
|
||||||
|
// TODO: 如果int float函数缺少返回值是否需要报错
|
||||||
|
if (func->getReturnType()->isInt()) {
|
||||||
|
pBuilder->createReturnInst(ConstantValue::get(0));
|
||||||
|
} else if (func->getReturnType()->isFloat()) {
|
||||||
|
pBuilder->createReturnInst(ConstantValue::get(0.0F));
|
||||||
|
} else {
|
||||||
|
pBuilder->createReturnInst({});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sysy
|
} // namespace sysy
|
||||||
|
|||||||
@ -7,6 +7,9 @@ namespace sysy {
|
|||||||
|
|
||||||
// 优化前对SysY IR的预处理,也可以视作部分CFG优化
|
// 优化前对SysY IR的预处理,也可以视作部分CFG优化
|
||||||
// 主要包括删除无用指令、合并基本块、删除空块等
|
// 主要包括删除无用指令、合并基本块、删除空块等
|
||||||
|
// 这些操作可以在SysY IR生成时就完成,但为了简化IR生成过程,
|
||||||
|
// 这里将其放在SysY IR生成后进行预处理
|
||||||
|
// 同时兼容phi节点的处理,可以再mem2reg后再次调用优化
|
||||||
class SysYOptPre {
|
class SysYOptPre {
|
||||||
private:
|
private:
|
||||||
Module *pModule;
|
Module *pModule;
|
||||||
@ -15,6 +18,13 @@ class SysYOptPre {
|
|||||||
public:
|
public:
|
||||||
SysYOptPre(Module *pMoudle, IRBuilder *pBuilder) : pModule(pMoudle), pBuilder(pBuilder) {}
|
SysYOptPre(Module *pMoudle, IRBuilder *pBuilder) : pModule(pMoudle), pBuilder(pBuilder) {}
|
||||||
|
|
||||||
|
void SysYOptimizateAfterIR(){
|
||||||
|
SysYDelAfterBr();
|
||||||
|
SysYBlockMerge();
|
||||||
|
SysYDelNoPreBLock();
|
||||||
|
SysYDelEmptyBlock();
|
||||||
|
SysYAddReturn();
|
||||||
|
}
|
||||||
void SysYDelInstAfterBr(); // 删除br后面的指令
|
void SysYDelInstAfterBr(); // 删除br后面的指令
|
||||||
void SysYDelEmptyBlock(); // 空块删除
|
void SysYDelEmptyBlock(); // 空块删除
|
||||||
void SysYDelNoPreBLock(); // 删除无前驱块
|
void SysYDelNoPreBLock(); // 删除无前驱块
|
||||||
|
|||||||
Reference in New Issue
Block a user