blob: aa442233e5c4ef27688a59b69be21f528783e74d [file] [log] [blame]
Duncan Sandsb0f1e172009-05-22 20:36:31 +00001//===-- DwarfEHPrepare - Prepare exception handling for code generation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass mulches exception handling code into a form adapted to code
Bill Wendling2ad4aca2010-03-26 23:41:30 +000011// generation. Required if using dwarf exception handling.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dwarfehprepare"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000016#include "llvm/Function.h"
17#include "llvm/Instructions.h"
18#include "llvm/IntrinsicInst.h"
19#include "llvm/Module.h"
20#include "llvm/Pass.h"
Bill Wendling2ad4aca2010-03-26 23:41:30 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/Dominators.h"
23#include "llvm/CodeGen/Passes.h"
Jim Grosbach8d77cc82010-01-20 23:03:55 +000024#include "llvm/MC/MCAsmInfo.h"
Gabor Greif2bf4b3b2010-06-25 11:25:30 +000025#include "llvm/Support/CallSite.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000026#include "llvm/Target/TargetLowering.h"
27#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Duncan Sands850fcd42010-09-03 08:31:48 +000028#include "llvm/Transforms/Utils/SSAUpdater.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000029using namespace llvm;
30
Bill Wendlinge13eba22011-11-07 23:36:48 +000031STATISTIC(NumResumesLowered, "Number of resume calls lowered");
Duncan Sandsb0f1e172009-05-22 20:36:31 +000032
33namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000034 class DwarfEHPrepare : public FunctionPass {
Dan Gohman55e59c12010-04-19 19:05:59 +000035 const TargetMachine *TM;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000036 const TargetLowering *TLI;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000037
Bill Wendlinge13eba22011-11-07 23:36:48 +000038 // RewindFunction - _Unwind_Resume or the target equivalent.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000039 Constant *RewindFunction;
40
Bill Wendlinge13eba22011-11-07 23:36:48 +000041 bool InsertUnwindResumeCalls(Function &Fn);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000042
Duncan Sandsb0f1e172009-05-22 20:36:31 +000043 public:
44 static char ID; // Pass identification, replacement for typeid.
Duncan Sands22efc182010-08-31 09:05:06 +000045 DwarfEHPrepare(const TargetMachine *tm) :
Owen Anderson90c579d2010-08-06 18:33:48 +000046 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
Bill Wendlinge13eba22011-11-07 23:36:48 +000047 RewindFunction(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000048 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
49 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000050
51 virtual bool runOnFunction(Function &Fn);
52
Bill Wendlinge13eba22011-11-07 23:36:48 +000053 virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
Bill Wendling8bedf972009-10-29 00:37:35 +000054
Duncan Sandsb0f1e172009-05-22 20:36:31 +000055 const char *getPassName() const {
56 return "Exception handling preparation";
57 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000058 };
59} // end anonymous namespace
60
61char DwarfEHPrepare::ID = 0;
62
Duncan Sands22efc182010-08-31 09:05:06 +000063FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
64 return new DwarfEHPrepare(tm);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000065}
66
Bill Wendling35adbb32011-08-17 19:48:49 +000067/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
68/// into calls to the appropriate _Unwind_Resume function.
Bill Wendlinge13eba22011-11-07 23:36:48 +000069bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling09908c42011-08-25 23:48:11 +000070 bool UsesNewEH = false;
Bill Wendling35adbb32011-08-17 19:48:49 +000071 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendlinge13eba22011-11-07 23:36:48 +000072 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Bill Wendling09908c42011-08-25 23:48:11 +000073 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();
78 }
Bill Wendling35adbb32011-08-17 19:48:49 +000079
80 if (Resumes.empty())
Bill Wendling09908c42011-08-25 23:48:11 +000081 return UsesNewEH;
Bill Wendling35adbb32011-08-17 19:48:49 +000082
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);
Bill Wendlinge13eba22011-11-07 23:36:48 +000089 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling35adbb32011-08-17 19:48:49 +000090 }
91
92 // Create the basic block where the _Unwind_Resume call will live.
Bill Wendlinge13eba22011-11-07 23:36:48 +000093 LLVMContext &Ctx = Fn.getContext();
94 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Bill Wendling35adbb32011-08-17 19:48:49 +000095 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), Resumes.size(),
96 "exn.obj", UnwindBB);
97
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) {
102 ResumeInst *RI = *I;
103 BranchInst::Create(UnwindBB, RI->getParent());
104 ExtractValueInst *ExnObj = ExtractValueInst::Create(RI->getOperand(0),
105 0, "exn.obj", RI);
106 PN->addIncoming(ExnObj, RI->getParent());
107 RI->eraseFromParent();
Bill Wendlinge13eba22011-11-07 23:36:48 +0000108 ++NumResumesLowered;
Bill Wendling35adbb32011-08-17 19:48:49 +0000109 }
110
111 // Call the function.
112 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
113 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
114
115 // We never expect _Unwind_Resume to return.
116 new UnreachableInst(Ctx, UnwindBB);
117 return true;
118}
119
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000120bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Bill Wendlinge13eba22011-11-07 23:36:48 +0000121 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000122 return Changed;
123}