Add support for unreachable


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17056 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/PostDominators.cpp b/lib/Analysis/PostDominators.cpp
index 0882294..4f9d1d1 100644
--- a/lib/Analysis/PostDominators.cpp
+++ b/lib/Analysis/PostDominators.cpp
@@ -40,8 +40,7 @@
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
     Doms[I];  // Initialize to empty
 
-    if (isa<ReturnInst>(I->getTerminator()) ||
-        isa<UnwindInst>(I->getTerminator()))
+    if (succ_begin(I) == succ_end(I))
       Roots.push_back(I);
   }
 
diff --git a/lib/ExecutionEngine/Interpreter/Execution.cpp b/lib/ExecutionEngine/Interpreter/Execution.cpp
index 589ac7f..8616656 100644
--- a/lib/ExecutionEngine/Interpreter/Execution.cpp
+++ b/lib/ExecutionEngine/Interpreter/Execution.cpp
@@ -616,6 +616,11 @@
   SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
 }
 
+void Interpreter::visitUnreachableInst(UnreachableInst &I) {
+  std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
+  abort();
+}
+
 void Interpreter::visitBranchInst(BranchInst &I) {
   ExecutionContext &SF = ECStack.back();
   BasicBlock *Dest;
diff --git a/lib/ExecutionEngine/Interpreter/Interpreter.h b/lib/ExecutionEngine/Interpreter/Interpreter.h
index d6e590d..e146135 100644
--- a/lib/ExecutionEngine/Interpreter/Interpreter.h
+++ b/lib/ExecutionEngine/Interpreter/Interpreter.h
@@ -145,6 +145,7 @@
   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
   void visitUnwindInst(UnwindInst &I);
+  void visitUnreachableInst(UnreachableInst &I);
 
   void visitShl(ShiftInst &I);
   void visitShr(ShiftInst &I);
diff --git a/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp b/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
index 5c87a5b..9776163 100644
--- a/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
+++ b/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
@@ -46,13 +46,16 @@
   //
   std::vector<BasicBlock*> ReturningBlocks;
   std::vector<BasicBlock*> UnwindingBlocks;
+  std::vector<BasicBlock*> UnreachableBlocks;
   for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
     if (isa<ReturnInst>(I->getTerminator()))
       ReturningBlocks.push_back(I);
     else if (isa<UnwindInst>(I->getTerminator()))
       UnwindingBlocks.push_back(I);
+    else if (isa<UnreachableInst>(I->getTerminator()))
+      UnreachableBlocks.push_back(I);
 
-  // Handle unwinding blocks first...
+  // Handle unwinding blocks first.
   if (UnwindingBlocks.empty()) {
     UnwindBlock = 0;
   } else if (UnwindingBlocks.size() == 1) {
@@ -64,12 +67,29 @@
     for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), 
            E = UnwindingBlocks.end(); I != E; ++I) {
       BasicBlock *BB = *I;
-      BB->getInstList().pop_back();  // Remove the return insn
+      BB->getInstList().pop_back();  // Remove the unwind insn
       new BranchInst(UnwindBlock, BB);
     }
   }
 
-  // Now handle return blocks...
+  // Then unreachable blocks.
+  if (UnreachableBlocks.empty()) {
+    UnreachableBlock = 0;
+  } else if (UnreachableBlocks.size() == 1) {
+    UnreachableBlock = UnreachableBlocks.front();
+  } else {
+    UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
+    new UnreachableInst(UnreachableBlock);
+
+    for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(), 
+           E = UnreachableBlocks.end(); I != E; ++I) {
+      BasicBlock *BB = *I;
+      BB->getInstList().pop_back();  // Remove the unreachable inst.
+      new BranchInst(UnreachableBlock, BB);
+    }
+  }
+
+  // Now handle return blocks.
   if (ReturningBlocks.empty()) {
     ReturnBlock = 0;
     return false;                          // No blocks return