mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-04-30 23:20:40 +03:00
Generate less dead code by deleting unreachable blocks at the end of functions
(which would otherwise appear if a function ends with a return, for example).
This commit is contained in:
parent
8bcc2d9b3f
commit
25ebb3dbcd
1 changed files with 20 additions and 16 deletions
|
@ -782,29 +782,33 @@ void DtoDefineFunction(FuncDeclaration* fd)
|
||||||
|
|
||||||
// std::cout << *func << std::endl;
|
// std::cout << *func << std::endl;
|
||||||
|
|
||||||
// llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement
|
llvm::BasicBlock* bb = gIR->scopebb();
|
||||||
// in automatically, so we do it here.
|
if (pred_begin(bb) == pred_end(bb) && bb != &bb->getParent()->getEntryBlock()) {
|
||||||
if (!gIR->scopereturned()) {
|
// This block is trivially unreachable, so just delete it.
|
||||||
|
// (This is a common case because it happens when 'return'
|
||||||
|
// is the last statement in a function)
|
||||||
|
bb->eraseFromParent();
|
||||||
|
} else if (!gIR->scopereturned()) {
|
||||||
|
// llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement
|
||||||
|
// in automatically, so we do it here.
|
||||||
|
|
||||||
// pass the previous block into this block
|
// pass the previous block into this block
|
||||||
if (global.params.symdebug) DtoDwarfFuncEnd(fd);
|
if (global.params.symdebug) DtoDwarfFuncEnd(fd);
|
||||||
if (func->getReturnType() == LLType::VoidTy) {
|
if (func->getReturnType() == LLType::VoidTy) {
|
||||||
llvm::ReturnInst::Create(gIR->scopebb());
|
llvm::ReturnInst::Create(gIR->scopebb());
|
||||||
}
|
}
|
||||||
else {
|
else if (!fd->isMain()) {
|
||||||
if (!fd->isMain())
|
AsmBlockStatement* asmb = fd->fbody->endsWithAsm();
|
||||||
{
|
if (asmb) {
|
||||||
AsmBlockStatement* asmb = fd->fbody->endsWithAsm();
|
assert(asmb->abiret);
|
||||||
if (asmb) {
|
llvm::ReturnInst::Create(asmb->abiret, bb);
|
||||||
assert(asmb->abiret);
|
}
|
||||||
llvm::ReturnInst::Create(asmb->abiret, gIR->scopebb());
|
else {
|
||||||
}
|
llvm::ReturnInst::Create(llvm::UndefValue::get(func->getReturnType()), bb);
|
||||||
else {
|
|
||||||
llvm::ReturnInst::Create(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
llvm::ReturnInst::Create(llvm::Constant::getNullValue(func->getReturnType()), gIR->scopebb());
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
llvm::ReturnInst::Create(llvm::Constant::getNullValue(func->getReturnType()), bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
// std::cout << *func << std::endl;
|
// std::cout << *func << std::endl;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue