llvm.org GIT mirror llvm / aa5abe8
[unwind removal] We no longer have 'unwind' instructions being generated, so remove the code that handles them. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149901 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
11 changed file(s) with 11 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
385385 }
386386
387387 TerminatorInst* terminator = currentNode->getBlock()->getTerminator();
388 if(isa(terminator) || isa(terminator)
389 || isa(terminator) || isa(terminator))
388 if(isa(terminator) || isa(terminator) ||
389 isa(terminator))
390390 addEdge(currentNode, getExit(),0);
391391
392392 currentNode->setColor(BallLarusNode::GRAY);
115115 // Branches and invokes do not escape, only unwind, resume, and return
116116 // do.
117117 TerminatorInst *TI = CurBB->getTerminator();
118 if (!isa(TI) && !isa(TI) &&
119 !isa(TI))
118 if (!isa(TI) && !isa(TI))
120119 continue;
121120
122121 Builder.SetInsertPoint(TI->getParent(), TI);
622622 }
623623
624624 popStackAndReturnValueToCaller(RetTy, Result);
625 }
626
627 void Interpreter::visitUnwindInst(UnwindInst &I) {
628 // Unwind stack
629 Instruction *Inst;
630 do {
631 ECStack.pop_back();
632 if (ECStack.empty())
633 report_fatal_error("Empty stack during unwind!");
634 Inst = ECStack.back().Caller.getInstruction();
635 } while (!(Inst && isa(Inst)));
636
637 // Return from invoke
638 ExecutionContext &InvokingSF = ECStack.back();
639 InvokingSF.Caller = CallSite();
640
641 // Go to exceptional destination BB of invoke instruction
642 SwitchToNewBasicBlock(cast(Inst)->getUnwindDest(), InvokingSF);
643625 }
644626
645627 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
170170 void visitCallSite(CallSite CS);
171171 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
172172 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
173 void visitUnwindInst(UnwindInst &I);
174173 void visitUnreachableInst(UnreachableInst &I);
175174
176175 void visitShl(BinaryOperator &I);
287287 void visitSwitchInst(SwitchInst &I);
288288 void visitIndirectBrInst(IndirectBrInst &I);
289289 void visitInvokeInst(InvokeInst &I) {
290 llvm_unreachable("Lowerinvoke pass didn't work!");
291 }
292 void visitUnwindInst(UnwindInst &I) {
293290 llvm_unreachable("Lowerinvoke pass didn't work!");
294291 }
295292 void visitResumeInst(ResumeInst &I) {
11411141 nl(Out);
11421142 break;
11431143 }
1144 case Instruction::Unwind: {
1145 Out << "new UnwindInst("
1146 << bbname << ");";
1147 break;
1148 }
11491144 case Instruction::Unreachable: {
11501145 Out << "new UnreachableInst("
11511146 << "mod->getContext(), "
100100 // Check to see if this function performs an unwind or calls an
101101 // unwinding function.
102102 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
103 if (CheckUnwind && (isa(BB->getTerminator()) ||
104 isa(BB->getTerminator()))) {
103 if (CheckUnwind && isa(BB->getTerminator())) {
105104 // Uses unwind / resume!
106105 SCCMightUnwind = true;
107106 } else if (CheckReturn && isa(BB->getTerminator())) {
736736 // If the stack restore is in a return, resume, or unwind block and if there
737737 // are no allocas or calls between the restore and the return, nuke the
738738 // restore.
739 if (!CannotRemove && (isa(TI) || isa(TI) ||
740 isaInst>(TI)))
739 if (!CannotRemove && (isa(TI) || isaInst>(TI)))
741740 return EraseInstFromFunction(CI);
742741 break;
743742 }
5353 using namespace llvm;
5454
5555 STATISTIC(NumInvokes, "Number of invokes replaced");
56 STATISTIC(NumUnwinds, "Number of unwinds replaced");
5756 STATISTIC(NumSpilled, "Number of registers live across unwind edges");
5857
5958 static cl::opt ExpensiveEHSupport("enable-correct-eh-support",
192191 BB->getInstList().erase(II);
193192
194193 ++NumInvokes; Changed = true;
195 } else if (UnwindInst *UI = dyn_cast(BB->getTerminator())) {
196 // Insert a call to abort()
197 CallInst::Create(AbortFn, "", UI)->setTailCall();
198
199 // Insert a return instruction. This really should be a "barrier", as it
200 // is unreachable.
201 ReturnInst::Create(F.getContext(),
202 F.getReturnType()->isVoidTy() ?
203 0 : Constant::getNullValue(F.getReturnType()), UI);
204
205 // Remove the unwind instruction now.
206 BB->getInstList().erase(UI);
207
208 ++NumUnwinds; Changed = true;
209194 }
210195 return Changed;
211196 }
403388
404389 bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
405390 SmallVector Returns;
406 SmallVector Unwinds;
407391 SmallVector Invokes;
408392 UnreachableInst* UnreachablePlaceholder = 0;
409393
414398 Returns.push_back(RI);
415399 } else if (InvokeInst *II = dyn_cast(BB->getTerminator())) {
416400 Invokes.push_back(II);
417 } else if (UnwindInst *UI = dyn_cast(BB->getTerminator())) {
418 Unwinds.push_back(UI);
419 }
420
421 if (Unwinds.empty() && Invokes.empty()) return false;
401 }
402
403 if (Invokes.empty()) return false;
422404
423405 NumInvokes += Invokes.size();
424 NumUnwinds += Unwinds.size();
425406
426407 // TODO: This is not an optimal way to do this. In particular, this always
427408 // inserts setjmp calls into the entries of functions with invoke instructions
571552 CallInst::Create(AbortFn, "",
572553 TermBlock->getTerminator())->setTailCall();
573554
574
575 // Replace all unwinds with a branch to the unwind handler.
576 for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
577 BranchInst::Create(UnwindHandler, Unwinds[i]);
578 Unwinds[i]->eraseFromParent();
579 }
580
581555 // Replace the inserted unreachable with a branch to the unwind handler.
582556 if (UnreachablePlaceholder) {
583557 BranchInst::Create(UnwindHandler, UnreachablePlaceholder);
6666 bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
6767 IRBuilder<> &Builder);
6868
69 bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
6970 bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
70 bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
71 bool SimplifyUnwind(UnwindInst *UI, IRBuilder<> &Builder);
7271 bool SimplifyUnreachable(UnreachableInst *UI);
7372 bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
7473 bool SimplifyIndirectBr(IndirectBrInst *IBI);
23502349 return true;
23512350 }
23522351 return false;
2353 }
2354
2355 bool SimplifyCFGOpt::SimplifyUnwind(UnwindInst *UI, IRBuilder<> &Builder) {
2356 // Check to see if the first instruction in this block is just an unwind.
2357 // If so, replace any invoke instructions which use this as an exception
2358 // destination with call instructions.
2359 BasicBlock *BB = UI->getParent();
2360 if (!BB->getFirstNonPHIOrDbg()->isTerminator()) return false;
2361
2362 bool Changed = false;
2363 SmallVector Preds(pred_begin(BB), pred_end(BB));
2364 while (!Preds.empty()) {
2365 BasicBlock *Pred = Preds.back();
2366 InvokeInst *II = dyn_cast(Pred->getTerminator());
2367 if (II && II->getUnwindDest() == BB) {
2368 // Insert a new branch instruction before the invoke, because this
2369 // is now a fall through.
2370 Builder.SetInsertPoint(II);
2371 BranchInst *BI = Builder.CreateBr(II->getNormalDest());
2372 Pred->getInstList().remove(II); // Take out of symbol table
2373
2374 // Insert the call now.
2375 SmallVector Args(II->op_begin(), II->op_end()-3);
2376 Builder.SetInsertPoint(BI);
2377 CallInst *CI = Builder.CreateCall(II->getCalledValue(),
2378 Args, II->getName());
2379 CI->setCallingConv(II->getCallingConv());
2380 CI->setAttributes(II->getAttributes());
2381 // If the invoke produced a value, the Call now does instead.
2382 II->replaceAllUsesWith(CI);
2383 delete II;
2384 Changed = true;
2385 }
2386
2387 Preds.pop_back();
2388 }
2389
2390 // If this block is now dead (and isn't the entry block), remove it.
2391 if (pred_begin(BB) == pred_end(BB) &&
2392 BB != &BB->getParent()->getEntryBlock()) {
2393 // We know there are no successors, so just nuke the block.
2394 BB->eraseFromParent();
2395 return true;
2396 }
2397
2398 return Changed;
23992352 }
24002353
24012354 bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) {
30022955 } else {
30032956 if (SimplifyCondBranch(BI, Builder)) return true;
30042957 }
2958 } else if (ReturnInst *RI = dyn_cast(BB->getTerminator())) {
2959 if (SimplifyReturn(RI, Builder)) return true;
30052960 } else if (ResumeInst *RI = dyn_cast(BB->getTerminator())) {
30062961 if (SimplifyResume(RI, Builder)) return true;
3007 } else if (ReturnInst *RI = dyn_cast(BB->getTerminator())) {
3008 if (SimplifyReturn(RI, Builder)) return true;
30092962 } else if (SwitchInst *SI = dyn_cast(BB->getTerminator())) {
30102963 if (SimplifySwitch(SI, Builder)) return true;
30112964 } else if (UnreachableInst *UI =
30122965 dyn_cast(BB->getTerminator())) {
30132966 if (SimplifyUnreachable(UI)) return true;
3014 } else if (UnwindInst *UI = dyn_cast(BB->getTerminator())) {
3015 if (SimplifyUnwind(UI, Builder)) return true;
30162967 } else if (IndirectBrInst *IBI =
30172968 dyn_cast(BB->getTerminator())) {
30182969 if (SimplifyIndirectBr(IBI)) return true;
4949 // return.
5050 //
5151 std::vector ReturningBlocks;
52 std::vector UnwindingBlocks;
5352 std::vector UnreachableBlocks;
5453 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
5554 if (isa(I->getTerminator()))
5655 ReturningBlocks.push_back(I);
57 else if (isa(I->getTerminator()))
58 UnwindingBlocks.push_back(I);
5956 else if (isa(I->getTerminator()))
6057 UnreachableBlocks.push_back(I);
61
62 // Handle unwinding blocks first.
63 if (UnwindingBlocks.empty()) {
64 UnwindBlock = 0;
65 } else if (UnwindingBlocks.size() == 1) {
66 UnwindBlock = UnwindingBlocks.front();
67 } else {
68 UnwindBlock = BasicBlock::Create(F.getContext(), "UnifiedUnwindBlock", &F);
69 new UnwindInst(F.getContext(), UnwindBlock);
70
71 for (std::vector::iterator I = UnwindingBlocks.begin(),
72 E = UnwindingBlocks.end(); I != E; ++I) {
73 BasicBlock *BB = *I;
74 BB->getInstList().pop_back(); // Remove the unwind insn
75 BranchInst::Create(UnwindBlock, BB);
76 }
77 }
7858
7959 // Then unreachable blocks.
8060 if (UnreachableBlocks.empty()) {