Add support for unreachable

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17056 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2004-10-16 18:21:33 +00:00
parent bd1d382cc4
commit ec7c1ab1da
4 changed files with 30 additions and 5 deletions

View File

@ -40,8 +40,7 @@ bool PostDominatorSet::runOnFunction(Function &F) {
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Doms[I]; // Initialize to empty Doms[I]; // Initialize to empty
if (isa<ReturnInst>(I->getTerminator()) || if (succ_begin(I) == succ_end(I))
isa<UnwindInst>(I->getTerminator()))
Roots.push_back(I); Roots.push_back(I);
} }

View File

@ -616,6 +616,11 @@ void Interpreter::visitUnwindInst(UnwindInst &I) {
SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF); 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) { void Interpreter::visitBranchInst(BranchInst &I) {
ExecutionContext &SF = ECStack.back(); ExecutionContext &SF = ECStack.back();
BasicBlock *Dest; BasicBlock *Dest;

View File

@ -145,6 +145,7 @@ public:
void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
void visitUnwindInst(UnwindInst &I); void visitUnwindInst(UnwindInst &I);
void visitUnreachableInst(UnreachableInst &I);
void visitShl(ShiftInst &I); void visitShl(ShiftInst &I);
void visitShr(ShiftInst &I); void visitShr(ShiftInst &I);

View File

@ -46,13 +46,16 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
// //
std::vector<BasicBlock*> ReturningBlocks; std::vector<BasicBlock*> ReturningBlocks;
std::vector<BasicBlock*> UnwindingBlocks; std::vector<BasicBlock*> UnwindingBlocks;
std::vector<BasicBlock*> UnreachableBlocks;
for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I) for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
if (isa<ReturnInst>(I->getTerminator())) if (isa<ReturnInst>(I->getTerminator()))
ReturningBlocks.push_back(I); ReturningBlocks.push_back(I);
else if (isa<UnwindInst>(I->getTerminator())) else if (isa<UnwindInst>(I->getTerminator()))
UnwindingBlocks.push_back(I); 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()) { if (UnwindingBlocks.empty()) {
UnwindBlock = 0; UnwindBlock = 0;
} else if (UnwindingBlocks.size() == 1) { } else if (UnwindingBlocks.size() == 1) {
@ -64,12 +67,29 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(), for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
E = UnwindingBlocks.end(); I != E; ++I) { E = UnwindingBlocks.end(); I != E; ++I) {
BasicBlock *BB = *I; BasicBlock *BB = *I;
BB->getInstList().pop_back(); // Remove the return insn BB->getInstList().pop_back(); // Remove the unwind insn
new BranchInst(UnwindBlock, BB); 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()) { if (ReturningBlocks.empty()) {
ReturnBlock = 0; ReturnBlock = 0;
return false; // No blocks return return false; // No blocks return