blob: 944dd4fb41c8227b9b75a722d0b68be721d1c660 [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);
Bill Wendling0ae06de2012-01-28 01:17:56 +000042 Instruction *GetExceptionObject(ResumeInst *RI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000043
Duncan Sandsb0f1e172009-05-22 20:36:31 +000044 public:
45 static char ID; // Pass identification, replacement for typeid.
Duncan Sands22efc182010-08-31 09:05:06 +000046 DwarfEHPrepare(const TargetMachine *tm) :
Owen Anderson90c579d2010-08-06 18:33:48 +000047 FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()),
Bill Wendlinge13eba22011-11-07 23:36:48 +000048 RewindFunction(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000049 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
50 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000051
52 virtual bool runOnFunction(Function &Fn);
53
Bill Wendlinge13eba22011-11-07 23:36:48 +000054 virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
Bill Wendling8bedf972009-10-29 00:37:35 +000055
Duncan Sandsb0f1e172009-05-22 20:36:31 +000056 const char *getPassName() const {
57 return "Exception handling preparation";
58 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000059 };
60} // end anonymous namespace
61
62char DwarfEHPrepare::ID = 0;
63
Duncan Sands22efc182010-08-31 09:05:06 +000064FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) {
65 return new DwarfEHPrepare(tm);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000066}
67
Bill Wendling0ae06de2012-01-28 01:17:56 +000068/// GetExceptionObject - Return the exception object from the value passed into
69/// the 'resume' instruction (typically an aggregate). Clean up any dead
70/// instructions, including the 'resume' instruction.
71Instruction *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
72 Value *V = RI->getOperand(0);
73 Instruction *ExnObj = 0;
74 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
75 LoadInst *SelLoad = 0;
76 InsertValueInst *ExcIVI = 0;
77 bool EraseIVIs = false;
78
79 if (SelIVI) {
80 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
81 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
82 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
83 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
84 ExnObj = cast<Instruction>(ExcIVI->getOperand(1));
85 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
86 EraseIVIs = true;
87 }
88 }
89 }
90
91 if (!ExnObj)
92 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
93
94 RI->eraseFromParent();
95
96 if (EraseIVIs) {
97 if (SelIVI->getNumUses() == 0)
98 SelIVI->eraseFromParent();
99 if (ExcIVI->getNumUses() == 0)
100 ExcIVI->eraseFromParent();
101 if (SelLoad && SelLoad->getNumUses() == 0)
102 SelLoad->eraseFromParent();
103 }
104
105 return ExnObj;
106}
107
Bill Wendling35adbb32011-08-17 19:48:49 +0000108/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
109/// into calls to the appropriate _Unwind_Resume function.
Bill Wendlinge13eba22011-11-07 23:36:48 +0000110bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling09908c42011-08-25 23:48:11 +0000111 bool UsesNewEH = false;
Bill Wendling35adbb32011-08-17 19:48:49 +0000112 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendlinge13eba22011-11-07 23:36:48 +0000113 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Bill Wendling09908c42011-08-25 23:48:11 +0000114 TerminatorInst *TI = I->getTerminator();
115 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
116 Resumes.push_back(RI);
117 else if (InvokeInst *II = dyn_cast<InvokeInst>(TI))
118 UsesNewEH = II->getUnwindDest()->isLandingPad();
119 }
Bill Wendling35adbb32011-08-17 19:48:49 +0000120
121 if (Resumes.empty())
Bill Wendling09908c42011-08-25 23:48:11 +0000122 return UsesNewEH;
Bill Wendling35adbb32011-08-17 19:48:49 +0000123
124 // Find the rewind function if we didn't already.
125 if (!RewindFunction) {
126 LLVMContext &Ctx = Resumes[0]->getContext();
127 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
128 Type::getInt8PtrTy(Ctx), false);
129 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
Bill Wendlinge13eba22011-11-07 23:36:48 +0000130 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling35adbb32011-08-17 19:48:49 +0000131 }
132
133 // Create the basic block where the _Unwind_Resume call will live.
Bill Wendlinge13eba22011-11-07 23:36:48 +0000134 LLVMContext &Ctx = Fn.getContext();
Bill Wendling0ae06de2012-01-28 01:17:56 +0000135 unsigned ResumesSize = Resumes.size();
136
137 if (ResumesSize == 1) {
138 // Instead of creating a new BB and PHI node, just append the call to
139 // _Unwind_Resume to the end of the single resume block.
140 ResumeInst *RI = Resumes.front();
141 BasicBlock *UnwindBB = RI->getParent();
142 Instruction *ExnObj = GetExceptionObject(RI);
143
144 // Call the _Unwind_Resume function.
145 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
146 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
147
148 // We never expect _Unwind_Resume to return.
149 new UnreachableInst(Ctx, UnwindBB);
150 return true;
151 }
152
Bill Wendlinge13eba22011-11-07 23:36:48 +0000153 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Bill Wendling0ae06de2012-01-28 01:17:56 +0000154 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
Bill Wendling35adbb32011-08-17 19:48:49 +0000155 "exn.obj", UnwindBB);
156
157 // Extract the exception object from the ResumeInst and add it to the PHI node
158 // that feeds the _Unwind_Resume call.
159 for (SmallVectorImpl<ResumeInst*>::iterator
160 I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
161 ResumeInst *RI = *I;
Bill Wendling0ae06de2012-01-28 01:17:56 +0000162 BasicBlock *Parent = RI->getParent();
163 BranchInst::Create(UnwindBB, Parent);
Bill Wendlingb618ea52012-01-20 00:53:28 +0000164
Bill Wendling0ae06de2012-01-28 01:17:56 +0000165 Instruction *ExnObj = GetExceptionObject(RI);
166 PN->addIncoming(ExnObj, Parent);
Bill Wendlingb618ea52012-01-20 00:53:28 +0000167
Bill Wendlinge13eba22011-11-07 23:36:48 +0000168 ++NumResumesLowered;
Bill Wendling35adbb32011-08-17 19:48:49 +0000169 }
170
171 // Call the function.
172 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
173 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
174
175 // We never expect _Unwind_Resume to return.
176 new UnreachableInst(Ctx, UnwindBB);
177 return true;
178}
179
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000180bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Bill Wendlinge13eba22011-11-07 23:36:48 +0000181 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000182 return Changed;
183}