blob: 81f8c1b127d76458616d511c65d711a4f2ef718d [file] [log] [blame]
Duncan Sandsd6fb6502009-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 Wendlingd1aa77c2010-03-26 23:41:30 +000011// generation. Required if using dwarf exception handling.
Duncan Sandsd6fb6502009-05-22 20:36:31 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
Reid Klecknerbe0a0502015-03-09 22:45:16 +000016#include "llvm/ADT/BitVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.h"
Reid Klecknerbe0a0502015-03-09 22:45:16 +000018#include "llvm/Analysis/CFG.h"
19#include "llvm/Analysis/TargetTransformInfo.h"
20#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Function.h"
22#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000024#include "llvm/Pass.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000025#include "llvm/Target/TargetLowering.h"
Eric Christopherd9134482014-08-04 21:25:23 +000026#include "llvm/Target/TargetSubtargetInfo.h"
Reid Klecknerbe0a0502015-03-09 22:45:16 +000027#include "llvm/Transforms/Utils/Local.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "dwarfehprepare"
31
Bill Wendling478f58c2011-11-07 23:36:48 +000032STATISTIC(NumResumesLowered, "Number of resume calls lowered");
Duncan Sandsd6fb6502009-05-22 20:36:31 +000033
34namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000035 class DwarfEHPrepare : public FunctionPass {
Bill Wendlingafc10362013-06-19 20:51:24 +000036 const TargetMachine *TM;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000037
Bill Wendling478f58c2011-11-07 23:36:48 +000038 // RewindFunction - _Unwind_Resume or the target equivalent.
Duncan Sandsd6fb6502009-05-22 20:36:31 +000039 Constant *RewindFunction;
40
Reid Klecknerbe0a0502015-03-09 22:45:16 +000041 DominatorTree *DT;
42 const TargetLowering *TLI;
43
Bill Wendling478f58c2011-11-07 23:36:48 +000044 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling27489fe2012-05-17 17:59:51 +000045 Value *GetExceptionObject(ResumeInst *RI);
Reid Klecknerbe0a0502015-03-09 22:45:16 +000046 size_t
47 pruneUnreachableResumes(Function &Fn,
48 SmallVectorImpl<ResumeInst *> &Resumes,
49 SmallVectorImpl<LandingPadInst *> &CleanupLPads);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000050
Duncan Sandsd6fb6502009-05-22 20:36:31 +000051 public:
52 static char ID; // Pass identification, replacement for typeid.
Reid Kleckner7bb07382015-02-18 23:17:41 +000053
54 // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in
55 // practice.
Reid Klecknerbe0a0502015-03-09 22:45:16 +000056 DwarfEHPrepare()
57 : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr), DT(nullptr),
58 TLI(nullptr) {}
Reid Kleckner7bb07382015-02-18 23:17:41 +000059
Chandler Carruth73523022014-01-13 13:07:17 +000060 DwarfEHPrepare(const TargetMachine *TM)
Reid Klecknerbe0a0502015-03-09 22:45:16 +000061 : FunctionPass(ID), TM(TM), RewindFunction(nullptr), DT(nullptr),
62 TLI(nullptr) {}
Duncan Sandsd6fb6502009-05-22 20:36:31 +000063
Craig Topper4584cd52014-03-07 09:26:03 +000064 bool runOnFunction(Function &Fn) override;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000065
Yaron Keren66b0ceb2014-09-14 20:36:28 +000066 bool doFinalization(Module &M) override {
67 RewindFunction = nullptr;
68 return false;
69 }
70
Reid Klecknerbe0a0502015-03-09 22:45:16 +000071 void getAnalysisUsage(AnalysisUsage &AU) const override;
72
Craig Topper4584cd52014-03-07 09:26:03 +000073 const char *getPassName() const override {
Duncan Sandsd6fb6502009-05-22 20:36:31 +000074 return "Exception handling preparation";
75 }
Duncan Sandsd6fb6502009-05-22 20:36:31 +000076 };
77} // end anonymous namespace
78
79char DwarfEHPrepare::ID = 0;
Reid Klecknerbe0a0502015-03-09 22:45:16 +000080INITIALIZE_TM_PASS_BEGIN(DwarfEHPrepare, "dwarfehprepare",
81 "Prepare DWARF exceptions", false, false)
82INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
83INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
84INITIALIZE_TM_PASS_END(DwarfEHPrepare, "dwarfehprepare",
85 "Prepare DWARF exceptions", false, false)
Duncan Sandsd6fb6502009-05-22 20:36:31 +000086
Bill Wendlingafc10362013-06-19 20:51:24 +000087FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
88 return new DwarfEHPrepare(TM);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000089}
90
Reid Klecknerbe0a0502015-03-09 22:45:16 +000091void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
92 AU.addRequired<TargetTransformInfoWrapperPass>();
93 AU.addRequired<DominatorTreeWrapperPass>();
94}
95
Bill Wendling8c090402012-01-28 01:17:56 +000096/// GetExceptionObject - Return the exception object from the value passed into
97/// the 'resume' instruction (typically an aggregate). Clean up any dead
98/// instructions, including the 'resume' instruction.
Bill Wendling27489fe2012-05-17 17:59:51 +000099Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling8c090402012-01-28 01:17:56 +0000100 Value *V = RI->getOperand(0);
Craig Topperc0196b12014-04-14 00:51:57 +0000101 Value *ExnObj = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +0000102 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
Craig Topperc0196b12014-04-14 00:51:57 +0000103 LoadInst *SelLoad = nullptr;
104 InsertValueInst *ExcIVI = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +0000105 bool EraseIVIs = false;
106
107 if (SelIVI) {
108 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
109 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
110 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
111 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
Bill Wendling27489fe2012-05-17 17:59:51 +0000112 ExnObj = ExcIVI->getOperand(1);
Bill Wendling8c090402012-01-28 01:17:56 +0000113 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
114 EraseIVIs = true;
115 }
116 }
117 }
118
119 if (!ExnObj)
120 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
121
122 RI->eraseFromParent();
123
124 if (EraseIVIs) {
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000125 if (SelIVI->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000126 SelIVI->eraseFromParent();
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000127 if (ExcIVI->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000128 ExcIVI->eraseFromParent();
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000129 if (SelLoad && SelLoad->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000130 SelLoad->eraseFromParent();
131 }
132
133 return ExnObj;
134}
135
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000136/// Replace resumes that are not reachable from a cleanup landing pad with
137/// unreachable and then simplify those blocks.
138size_t DwarfEHPrepare::pruneUnreachableResumes(
139 Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes,
140 SmallVectorImpl<LandingPadInst *> &CleanupLPads) {
141 BitVector ResumeReachable(Resumes.size());
142 size_t ResumeIndex = 0;
143 for (auto *RI : Resumes) {
144 for (auto *LP : CleanupLPads) {
145 if (isPotentiallyReachable(LP, RI, DT)) {
146 ResumeReachable.set(ResumeIndex);
147 break;
148 }
149 }
150 ++ResumeIndex;
151 }
152
153 // If everything is reachable, there is no change.
154 if (ResumeReachable.all())
155 return Resumes.size();
156
157 const TargetTransformInfo &TTI =
158 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
159 LLVMContext &Ctx = Fn.getContext();
160
161 // Otherwise, insert unreachable instructions and call simplifycfg.
162 size_t ResumesLeft = 0;
163 for (size_t I = 0, E = Resumes.size(); I < E; ++I) {
164 ResumeInst *RI = Resumes[I];
165 if (ResumeReachable[I]) {
166 Resumes[ResumesLeft++] = RI;
167 } else {
168 BasicBlock *BB = RI->getParent();
169 new UnreachableInst(Ctx, RI);
170 RI->eraseFromParent();
171 SimplifyCFG(BB, TTI, 1, TLI->getDataLayout());
172 }
173 }
174 Resumes.resize(ResumesLeft);
175 return ResumesLeft;
176}
177
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000178/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
179/// into calls to the appropriate _Unwind_Resume function.
Bill Wendling478f58c2011-11-07 23:36:48 +0000180bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000181 SmallVector<ResumeInst*, 16> Resumes;
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000182 SmallVector<LandingPadInst*, 16> CleanupLPads;
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000183 for (BasicBlock &BB : Fn) {
184 if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
Bill Wendling8ac20412011-08-25 23:48:11 +0000185 Resumes.push_back(RI);
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000186 if (auto *LP = BB.getLandingPadInst())
187 if (LP->isCleanup())
188 CleanupLPads.push_back(LP);
Bill Wendling8ac20412011-08-25 23:48:11 +0000189 }
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000190
191 if (Resumes.empty())
Kai Nackee1823b62013-05-31 16:30:36 +0000192 return false;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000193
Chandler Carruth301ed0c2015-02-20 02:15:36 +0000194 LLVMContext &Ctx = Fn.getContext();
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000195
196 size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads);
197 if (ResumesLeft == 0)
198 return true; // We pruned them all.
199
200 // Find the rewind function if we didn't already.
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000201 if (!RewindFunction) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000202 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
203 Type::getInt8PtrTy(Ctx), false);
204 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
Bill Wendling478f58c2011-11-07 23:36:48 +0000205 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000206 }
207
208 // Create the basic block where the _Unwind_Resume call will live.
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000209 if (ResumesLeft == 1) {
Bill Wendling8c090402012-01-28 01:17:56 +0000210 // Instead of creating a new BB and PHI node, just append the call to
211 // _Unwind_Resume to the end of the single resume block.
212 ResumeInst *RI = Resumes.front();
213 BasicBlock *UnwindBB = RI->getParent();
Bill Wendling27489fe2012-05-17 17:59:51 +0000214 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000215
216 // Call the _Unwind_Resume function.
217 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
218 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
219
220 // We never expect _Unwind_Resume to return.
221 new UnreachableInst(Ctx, UnwindBB);
222 return true;
223 }
224
Bill Wendling478f58c2011-11-07 23:36:48 +0000225 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000226 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft,
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000227 "exn.obj", UnwindBB);
228
229 // Extract the exception object from the ResumeInst and add it to the PHI node
230 // that feeds the _Unwind_Resume call.
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000231 for (ResumeInst *RI : Resumes) {
Bill Wendling8c090402012-01-28 01:17:56 +0000232 BasicBlock *Parent = RI->getParent();
233 BranchInst::Create(UnwindBB, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000234
Bill Wendling27489fe2012-05-17 17:59:51 +0000235 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000236 PN->addIncoming(ExnObj, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000237
Bill Wendling478f58c2011-11-07 23:36:48 +0000238 ++NumResumesLowered;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000239 }
240
241 // Call the function.
242 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
243 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
244
245 // We never expect _Unwind_Resume to return.
246 new UnreachableInst(Ctx, UnwindBB);
247 return true;
248}
249
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000250bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Reid Kleckner7bb07382015-02-18 23:17:41 +0000251 assert(TM && "DWARF EH preparation requires a target machine");
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000252 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
253 TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
Bill Wendling478f58c2011-11-07 23:36:48 +0000254 bool Changed = InsertUnwindResumeCalls(Fn);
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000255 DT = nullptr;
256 TLI = nullptr;
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000257 return Changed;
258}