1 //===-- DwarfEHPrepare - Prepare exception handling for code generation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass mulches exception handling code into a form adapted to code
11 // generation. Required if using dwarf exception handling.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "dwarfehprepare"
16 #include "llvm/Function.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/IntrinsicInst.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/Support/CallSite.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/SSAUpdater.h"
31 STATISTIC(NumResumesLowered, "Number of resume calls lowered");
34 class DwarfEHPrepare : public FunctionPass {
35 const TargetMachine *TM;
36 const TargetLowering *TLI;
38 // RewindFunction - _Unwind_Resume or the target equivalent.
39 Constant *RewindFunction;
41 bool InsertUnwindResumeCalls(Function &Fn);
44 static char ID; // Pass identification, replacement for typeid.
45 DwarfEHPrepare(const TargetMachine *tm) :
46 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
48 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
51 virtual bool runOnFunction(Function &Fn);
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
55 const char *getPassName() const {
56 return "Exception handling preparation";
59 } // end anonymous namespace
61 char DwarfEHPrepare::ID = 0;
63 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
64 return new DwarfEHPrepare(tm);
67 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
68 /// into calls to the appropriate _Unwind_Resume function.
69 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
70 bool UsesNewEH = false;
71 SmallVector<ResumeInst*, 16> Resumes;
72 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
73 TerminatorInst *TI = I->getTerminator();
74 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
75 Resumes.push_back(RI);
76 else if (InvokeInst *II = dyn_cast<InvokeInst>(TI))
77 UsesNewEH = II->getUnwindDest()->isLandingPad();
83 // Find the rewind function if we didn't already.
84 if (!RewindFunction) {
85 LLVMContext &Ctx = Resumes[0]->getContext();
86 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
87 Type::getInt8PtrTy(Ctx), false);
88 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
89 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
92 // Create the basic block where the _Unwind_Resume call will live.
93 LLVMContext &Ctx = Fn.getContext();
94 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
95 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), Resumes.size(),
98 // Extract the exception object from the ResumeInst and add it to the PHI node
99 // that feeds the _Unwind_Resume call.
100 for (SmallVectorImpl<ResumeInst*>::iterator
101 I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
103 BranchInst::Create(UnwindBB, RI->getParent());
104 ExtractValueInst *ExnObj = ExtractValueInst::Create(RI->getOperand(0),
106 PN->addIncoming(ExnObj, RI->getParent());
107 RI->eraseFromParent();
111 // Call the function.
112 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
113 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
115 // We never expect _Unwind_Resume to return.
116 new UnreachableInst(Ctx, UnwindBB);
120 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
121 bool Changed = InsertUnwindResumeCalls(Fn);