Add support for unreachable
authorChris Lattner <sabre@nondot.org>
Sat, 16 Oct 2004 18:21:33 +0000 (18:21 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 16 Oct 2004 18:21:33 +0000 (18:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17056 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/PostDominators.cpp
lib/ExecutionEngine/Interpreter/Execution.cpp
lib/ExecutionEngine/Interpreter/Interpreter.h
lib/Transforms/Utils/UnifyFunctionExitNodes.cpp

index 08822949c6292aa6b0b054b4db344dd71f389be8..4f9d1d160696f336866748311c1cdbf7642ad835 100644 (file)
@@ -40,8 +40,7 @@ bool PostDominatorSet::runOnFunction(Function &F) {
   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);
   }
 
index 589ac7f99dc62a9ec67fdb03e8f48ae063b04f6d..8616656360f45261b7819cf7f89d549bd13c62f9 100644 (file)
@@ -616,6 +616,11 @@ void Interpreter::visitUnwindInst(UnwindInst &I) {
   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;
index d6e590d735742c4a2f4f76240b0cfb4b8b2daf4b..e146135cf5f5866c962ae258f232c7b6704e1545 100644 (file)
@@ -145,6 +145,7 @@ public:
   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);
index 5c87a5b15968a410bc697126f13bc2f070257c9d..977616378d04bf80874350f2c3923775fb5a5fc5 100644 (file)
@@ -46,13 +46,16 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
   //
   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 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
     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