changeset 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 7cca8cf730de
children 0c8d6d345001
files gen/functions.cpp
diffstat 1 files changed, 20 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/gen/functions.cpp	Thu Jun 11 21:48:10 2009 +0200
+++ b/gen/functions.cpp	Fri Jun 12 16:41:38 2009 +0200
@@ -782,29 +782,33 @@
 
 //     std::cout << *func << std::endl;
 
-    // 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.
-    if (!gIR->scopereturned()) {
+    llvm::BasicBlock* bb = gIR->scopebb();
+    if (pred_begin(bb) == pred_end(bb) && bb != &bb->getParent()->getEntryBlock()) {
+        // 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
         if (global.params.symdebug) DtoDwarfFuncEnd(fd);
         if (func->getReturnType() == LLType::VoidTy) {
             llvm::ReturnInst::Create(gIR->scopebb());
         }
-        else {
-            if (!fd->isMain())
-            {
-                AsmBlockStatement* asmb = fd->fbody->endsWithAsm();
-                if (asmb) {
-                    assert(asmb->abiret);
-                    llvm::ReturnInst::Create(asmb->abiret, gIR->scopebb());
-                }
-                else {
-                    llvm::ReturnInst::Create(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb());
-                }
+        else if (!fd->isMain()) {
+            AsmBlockStatement* asmb = fd->fbody->endsWithAsm();
+            if (asmb) {
+                assert(asmb->abiret);
+                llvm::ReturnInst::Create(asmb->abiret, bb);
             }
-            else
-                llvm::ReturnInst::Create(llvm::Constant::getNullValue(func->getReturnType()), gIR->scopebb());
+            else {
+                llvm::ReturnInst::Create(llvm::UndefValue::get(func->getReturnType()), bb);
+            }
         }
+        else
+            llvm::ReturnInst::Create(llvm::Constant::getNullValue(func->getReturnType()), bb);
     }
 
 //     std::cout << *func << std::endl;