blob: ecfe6f3f46938da411a88a297f91247c8ba38f60 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/CodeGen/Passes.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/Analysis/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Function.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/IntrinsicInst.h"
22#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/MC/MCAsmInfo.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000024#include "llvm/Pass.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 {
Benjamin Kramer69e42db2013-01-11 20:05:37 +000035 const TargetLoweringBase *TLI;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000036
Bill Wendlinge13eba22011-11-07 23:36:48 +000037 // RewindFunction - _Unwind_Resume or the target equivalent.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000038 Constant *RewindFunction;
39
Bill Wendlinge13eba22011-11-07 23:36:48 +000040 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling29424e82012-05-17 17:59:51 +000041 Value *GetExceptionObject(ResumeInst *RI);
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.
Bill Wendling3b1040c2013-05-20 21:54:18 +000045 DwarfEHPrepare(const TargetLoweringBase *TLI) :
46 FunctionPass(ID), TLI(TLI), RewindFunction(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000047 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
48 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000049
50 virtual bool runOnFunction(Function &Fn);
51
Bill Wendlinge13eba22011-11-07 23:36:48 +000052 virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
Bill Wendling8bedf972009-10-29 00:37:35 +000053
Duncan Sandsb0f1e172009-05-22 20:36:31 +000054 const char *getPassName() const {
55 return "Exception handling preparation";
56 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000057 };
58} // end anonymous namespace
59
60char DwarfEHPrepare::ID = 0;
61
Bill Wendling3b1040c2013-05-20 21:54:18 +000062FunctionPass *llvm::createDwarfEHPass(const TargetLoweringBase *TLI) {
63 return new DwarfEHPrepare(TLI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000064}
65
Bill Wendling0ae06de2012-01-28 01:17:56 +000066/// GetExceptionObject - Return the exception object from the value passed into
67/// the 'resume' instruction (typically an aggregate). Clean up any dead
68/// instructions, including the 'resume' instruction.
Bill Wendling29424e82012-05-17 17:59:51 +000069Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling0ae06de2012-01-28 01:17:56 +000070 Value *V = RI->getOperand(0);
Bill Wendling29424e82012-05-17 17:59:51 +000071 Value *ExnObj = 0;
Bill Wendling0ae06de2012-01-28 01:17:56 +000072 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
73 LoadInst *SelLoad = 0;
74 InsertValueInst *ExcIVI = 0;
75 bool EraseIVIs = false;
76
77 if (SelIVI) {
78 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
79 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
80 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
81 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
Bill Wendling29424e82012-05-17 17:59:51 +000082 ExnObj = ExcIVI->getOperand(1);
Bill Wendling0ae06de2012-01-28 01:17:56 +000083 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
84 EraseIVIs = true;
85 }
86 }
87 }
88
89 if (!ExnObj)
90 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
91
92 RI->eraseFromParent();
93
94 if (EraseIVIs) {
95 if (SelIVI->getNumUses() == 0)
96 SelIVI->eraseFromParent();
97 if (ExcIVI->getNumUses() == 0)
98 ExcIVI->eraseFromParent();
99 if (SelLoad && SelLoad->getNumUses() == 0)
100 SelLoad->eraseFromParent();
101 }
102
103 return ExnObj;
104}
105
Bill Wendling35adbb32011-08-17 19:48:49 +0000106/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
107/// into calls to the appropriate _Unwind_Resume function.
Bill Wendlinge13eba22011-11-07 23:36:48 +0000108bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling09908c42011-08-25 23:48:11 +0000109 bool UsesNewEH = false;
Bill Wendling35adbb32011-08-17 19:48:49 +0000110 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendlinge13eba22011-11-07 23:36:48 +0000111 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Bill Wendling09908c42011-08-25 23:48:11 +0000112 TerminatorInst *TI = I->getTerminator();
113 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
114 Resumes.push_back(RI);
115 else if (InvokeInst *II = dyn_cast<InvokeInst>(TI))
116 UsesNewEH = II->getUnwindDest()->isLandingPad();
117 }
Bill Wendling35adbb32011-08-17 19:48:49 +0000118
119 if (Resumes.empty())
Bill Wendling09908c42011-08-25 23:48:11 +0000120 return UsesNewEH;
Bill Wendling35adbb32011-08-17 19:48:49 +0000121
122 // Find the rewind function if we didn't already.
123 if (!RewindFunction) {
124 LLVMContext &Ctx = Resumes[0]->getContext();
125 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
126 Type::getInt8PtrTy(Ctx), false);
127 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
Bill Wendlinge13eba22011-11-07 23:36:48 +0000128 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling35adbb32011-08-17 19:48:49 +0000129 }
130
131 // Create the basic block where the _Unwind_Resume call will live.
Bill Wendlinge13eba22011-11-07 23:36:48 +0000132 LLVMContext &Ctx = Fn.getContext();
Bill Wendling0ae06de2012-01-28 01:17:56 +0000133 unsigned ResumesSize = Resumes.size();
134
135 if (ResumesSize == 1) {
136 // Instead of creating a new BB and PHI node, just append the call to
137 // _Unwind_Resume to the end of the single resume block.
138 ResumeInst *RI = Resumes.front();
139 BasicBlock *UnwindBB = RI->getParent();
Bill Wendling29424e82012-05-17 17:59:51 +0000140 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling0ae06de2012-01-28 01:17:56 +0000141
142 // Call the _Unwind_Resume function.
143 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
144 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
145
146 // We never expect _Unwind_Resume to return.
147 new UnreachableInst(Ctx, UnwindBB);
148 return true;
149 }
150
Bill Wendlinge13eba22011-11-07 23:36:48 +0000151 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Bill Wendling0ae06de2012-01-28 01:17:56 +0000152 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
Bill Wendling35adbb32011-08-17 19:48:49 +0000153 "exn.obj", UnwindBB);
154
155 // Extract the exception object from the ResumeInst and add it to the PHI node
156 // that feeds the _Unwind_Resume call.
157 for (SmallVectorImpl<ResumeInst*>::iterator
158 I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
159 ResumeInst *RI = *I;
Bill Wendling0ae06de2012-01-28 01:17:56 +0000160 BasicBlock *Parent = RI->getParent();
161 BranchInst::Create(UnwindBB, Parent);
Bill Wendlingb618ea52012-01-20 00:53:28 +0000162
Bill Wendling29424e82012-05-17 17:59:51 +0000163 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling0ae06de2012-01-28 01:17:56 +0000164 PN->addIncoming(ExnObj, Parent);
Bill Wendlingb618ea52012-01-20 00:53:28 +0000165
Bill Wendlinge13eba22011-11-07 23:36:48 +0000166 ++NumResumesLowered;
Bill Wendling35adbb32011-08-17 19:48:49 +0000167 }
168
169 // Call the function.
170 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
171 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
172
173 // We never expect _Unwind_Resume to return.
174 new UnreachableInst(Ctx, UnwindBB);
175 return true;
176}
177
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000178bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Bill Wendlinge13eba22011-11-07 23:36:48 +0000179 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000180 return Changed;
181}