Mercurial > projects > ldc
comparison gen/functions.cpp @ 1494:b243e28f63d4
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).
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Fri, 12 Jun 2009 16:41:38 +0200 |
parents | 93644700a0b3 |
children | 2292878925f4 |
comparison
equal
deleted
inserted
replaced
1493:7cca8cf730de | 1494:b243e28f63d4 |
---|---|
780 | 780 |
781 // TODO: clean up this mess | 781 // TODO: clean up this mess |
782 | 782 |
783 // std::cout << *func << std::endl; | 783 // std::cout << *func << std::endl; |
784 | 784 |
785 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | 785 llvm::BasicBlock* bb = gIR->scopebb(); |
786 // in automatically, so we do it here. | 786 if (pred_begin(bb) == pred_end(bb) && bb != &bb->getParent()->getEntryBlock()) { |
787 if (!gIR->scopereturned()) { | 787 // This block is trivially unreachable, so just delete it. |
788 // (This is a common case because it happens when 'return' | |
789 // is the last statement in a function) | |
790 bb->eraseFromParent(); | |
791 } else if (!gIR->scopereturned()) { | |
792 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
793 // in automatically, so we do it here. | |
794 | |
788 // pass the previous block into this block | 795 // pass the previous block into this block |
789 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | 796 if (global.params.symdebug) DtoDwarfFuncEnd(fd); |
790 if (func->getReturnType() == LLType::VoidTy) { | 797 if (func->getReturnType() == LLType::VoidTy) { |
791 llvm::ReturnInst::Create(gIR->scopebb()); | 798 llvm::ReturnInst::Create(gIR->scopebb()); |
792 } | 799 } |
793 else { | 800 else if (!fd->isMain()) { |
794 if (!fd->isMain()) | 801 AsmBlockStatement* asmb = fd->fbody->endsWithAsm(); |
795 { | 802 if (asmb) { |
796 AsmBlockStatement* asmb = fd->fbody->endsWithAsm(); | 803 assert(asmb->abiret); |
797 if (asmb) { | 804 llvm::ReturnInst::Create(asmb->abiret, bb); |
798 assert(asmb->abiret); | |
799 llvm::ReturnInst::Create(asmb->abiret, gIR->scopebb()); | |
800 } | |
801 else { | |
802 llvm::ReturnInst::Create(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
803 } | |
804 } | 805 } |
805 else | 806 else { |
806 llvm::ReturnInst::Create(llvm::Constant::getNullValue(func->getReturnType()), gIR->scopebb()); | 807 llvm::ReturnInst::Create(llvm::UndefValue::get(func->getReturnType()), bb); |
807 } | 808 } |
809 } | |
810 else | |
811 llvm::ReturnInst::Create(llvm::Constant::getNullValue(func->getReturnType()), bb); | |
808 } | 812 } |
809 | 813 |
810 // std::cout << *func << std::endl; | 814 // std::cout << *func << std::endl; |
811 | 815 |
812 // erase alloca point | 816 // erase alloca point |