blob: 0f6e1463f10f699d400eef1b1c70999345b4c33b [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"
Reid Kleckner47c8e7a2015-03-12 00:36:20 +000019#include "llvm/Analysis/LibCallSemantics.h"
Reid Klecknerbe0a0502015-03-09 22:45:16 +000020#include "llvm/Analysis/TargetTransformInfo.h"
21#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Function.h"
23#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000025#include "llvm/Pass.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000026#include "llvm/Target/TargetLowering.h"
Eric Christopherd9134482014-08-04 21:25:23 +000027#include "llvm/Target/TargetSubtargetInfo.h"
Reid Klecknerbe0a0502015-03-09 22:45:16 +000028#include "llvm/Transforms/Utils/Local.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000029using namespace llvm;
30
Chandler Carruth1b9dde02014-04-22 02:02:50 +000031#define DEBUG_TYPE "dwarfehprepare"
32
Bill Wendling478f58c2011-11-07 23:36:48 +000033STATISTIC(NumResumesLowered, "Number of resume calls lowered");
Duncan Sandsd6fb6502009-05-22 20:36:31 +000034
35namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000036 class DwarfEHPrepare : public FunctionPass {
Bill Wendlingafc10362013-06-19 20:51:24 +000037 const TargetMachine *TM;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000038
Bill Wendling478f58c2011-11-07 23:36:48 +000039 // RewindFunction - _Unwind_Resume or the target equivalent.
Duncan Sandsd6fb6502009-05-22 20:36:31 +000040 Constant *RewindFunction;
41
Reid Klecknerbe0a0502015-03-09 22:45:16 +000042 DominatorTree *DT;
43 const TargetLowering *TLI;
44
Bill Wendling478f58c2011-11-07 23:36:48 +000045 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling27489fe2012-05-17 17:59:51 +000046 Value *GetExceptionObject(ResumeInst *RI);
Reid Klecknerbe0a0502015-03-09 22:45:16 +000047 size_t
48 pruneUnreachableResumes(Function &Fn,
49 SmallVectorImpl<ResumeInst *> &Resumes,
50 SmallVectorImpl<LandingPadInst *> &CleanupLPads);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000051
Duncan Sandsd6fb6502009-05-22 20:36:31 +000052 public:
53 static char ID; // Pass identification, replacement for typeid.
Reid Kleckner7bb07382015-02-18 23:17:41 +000054
55 // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in
56 // practice.
Reid Klecknerbe0a0502015-03-09 22:45:16 +000057 DwarfEHPrepare()
58 : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr), DT(nullptr),
59 TLI(nullptr) {}
Reid Kleckner7bb07382015-02-18 23:17:41 +000060
Chandler Carruth73523022014-01-13 13:07:17 +000061 DwarfEHPrepare(const TargetMachine *TM)
Reid Klecknerbe0a0502015-03-09 22:45:16 +000062 : FunctionPass(ID), TM(TM), RewindFunction(nullptr), DT(nullptr),
63 TLI(nullptr) {}
Duncan Sandsd6fb6502009-05-22 20:36:31 +000064
Craig Topper4584cd52014-03-07 09:26:03 +000065 bool runOnFunction(Function &Fn) override;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000066
Yaron Keren66b0ceb2014-09-14 20:36:28 +000067 bool doFinalization(Module &M) override {
68 RewindFunction = nullptr;
69 return false;
70 }
71
Reid Klecknerbe0a0502015-03-09 22:45:16 +000072 void getAnalysisUsage(AnalysisUsage &AU) const override;
73
Craig Topper4584cd52014-03-07 09:26:03 +000074 const char *getPassName() const override {
Duncan Sandsd6fb6502009-05-22 20:36:31 +000075 return "Exception handling preparation";
76 }
Duncan Sandsd6fb6502009-05-22 20:36:31 +000077 };
78} // end anonymous namespace
79
80char DwarfEHPrepare::ID = 0;
Reid Klecknerbe0a0502015-03-09 22:45:16 +000081INITIALIZE_TM_PASS_BEGIN(DwarfEHPrepare, "dwarfehprepare",
82 "Prepare DWARF exceptions", false, false)
83INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
84INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
85INITIALIZE_TM_PASS_END(DwarfEHPrepare, "dwarfehprepare",
86 "Prepare DWARF exceptions", false, false)
Duncan Sandsd6fb6502009-05-22 20:36:31 +000087
Bill Wendlingafc10362013-06-19 20:51:24 +000088FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
89 return new DwarfEHPrepare(TM);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000090}
91
Reid Klecknerbe0a0502015-03-09 22:45:16 +000092void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.addRequired<TargetTransformInfoWrapperPass>();
94 AU.addRequired<DominatorTreeWrapperPass>();
95}
96
Bill Wendling8c090402012-01-28 01:17:56 +000097/// GetExceptionObject - Return the exception object from the value passed into
98/// the 'resume' instruction (typically an aggregate). Clean up any dead
99/// instructions, including the 'resume' instruction.
Bill Wendling27489fe2012-05-17 17:59:51 +0000100Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling8c090402012-01-28 01:17:56 +0000101 Value *V = RI->getOperand(0);
Craig Topperc0196b12014-04-14 00:51:57 +0000102 Value *ExnObj = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +0000103 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
Craig Topperc0196b12014-04-14 00:51:57 +0000104 LoadInst *SelLoad = nullptr;
105 InsertValueInst *ExcIVI = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +0000106 bool EraseIVIs = false;
107
108 if (SelIVI) {
109 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
110 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
111 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
112 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
Bill Wendling27489fe2012-05-17 17:59:51 +0000113 ExnObj = ExcIVI->getOperand(1);
Bill Wendling8c090402012-01-28 01:17:56 +0000114 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
115 EraseIVIs = true;
116 }
117 }
118 }
119
120 if (!ExnObj)
121 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
122
123 RI->eraseFromParent();
124
125 if (EraseIVIs) {
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000126 if (SelIVI->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000127 SelIVI->eraseFromParent();
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000128 if (ExcIVI->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000129 ExcIVI->eraseFromParent();
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000130 if (SelLoad && SelLoad->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000131 SelLoad->eraseFromParent();
132 }
133
134 return ExnObj;
135}
136
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000137/// Replace resumes that are not reachable from a cleanup landing pad with
138/// unreachable and then simplify those blocks.
139size_t DwarfEHPrepare::pruneUnreachableResumes(
140 Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes,
141 SmallVectorImpl<LandingPadInst *> &CleanupLPads) {
142 BitVector ResumeReachable(Resumes.size());
143 size_t ResumeIndex = 0;
144 for (auto *RI : Resumes) {
145 for (auto *LP : CleanupLPads) {
146 if (isPotentiallyReachable(LP, RI, DT)) {
147 ResumeReachable.set(ResumeIndex);
148 break;
149 }
150 }
151 ++ResumeIndex;
152 }
153
154 // If everything is reachable, there is no change.
155 if (ResumeReachable.all())
156 return Resumes.size();
157
158 const TargetTransformInfo &TTI =
159 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
160 LLVMContext &Ctx = Fn.getContext();
161
162 // Otherwise, insert unreachable instructions and call simplifycfg.
163 size_t ResumesLeft = 0;
164 for (size_t I = 0, E = Resumes.size(); I < E; ++I) {
165 ResumeInst *RI = Resumes[I];
166 if (ResumeReachable[I]) {
167 Resumes[ResumesLeft++] = RI;
168 } else {
169 BasicBlock *BB = RI->getParent();
170 new UnreachableInst(Ctx, RI);
171 RI->eraseFromParent();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000172 SimplifyCFG(BB, TTI, 1);
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000173 }
174 }
175 Resumes.resize(ResumesLeft);
176 return ResumesLeft;
177}
178
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000179/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
180/// into calls to the appropriate _Unwind_Resume function.
Bill Wendling478f58c2011-11-07 23:36:48 +0000181bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000182 SmallVector<ResumeInst*, 16> Resumes;
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000183 SmallVector<LandingPadInst*, 16> CleanupLPads;
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000184 for (BasicBlock &BB : Fn) {
185 if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
Bill Wendling8ac20412011-08-25 23:48:11 +0000186 Resumes.push_back(RI);
David Majnemer7fddecc2015-06-17 20:52:32 +0000187 if (auto *LP = BB.getLandingPadInst())
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000188 if (LP->isCleanup())
189 CleanupLPads.push_back(LP);
Bill Wendling8ac20412011-08-25 23:48:11 +0000190 }
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000191
192 if (Resumes.empty())
Kai Nackee1823b62013-05-31 16:30:36 +0000193 return false;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000194
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000195 // Check the personality, don't do anything if it's funclet-based.
David Majnemer7fddecc2015-06-17 20:52:32 +0000196 EHPersonality Pers = classifyEHPersonality(Fn.getPersonalityFn());
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000197 if (isFuncletEHPersonality(Pers))
David Majnemer7fddecc2015-06-17 20:52:32 +0000198 return false;
199
Chandler Carruth301ed0c2015-02-20 02:15:36 +0000200 LLVMContext &Ctx = Fn.getContext();
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000201
202 size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads);
203 if (ResumesLeft == 0)
204 return true; // We pruned them all.
205
206 // Find the rewind function if we didn't already.
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000207 if (!RewindFunction) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000208 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
209 Type::getInt8PtrTy(Ctx), false);
210 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
Bill Wendling478f58c2011-11-07 23:36:48 +0000211 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000212 }
213
214 // Create the basic block where the _Unwind_Resume call will live.
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000215 if (ResumesLeft == 1) {
Bill Wendling8c090402012-01-28 01:17:56 +0000216 // Instead of creating a new BB and PHI node, just append the call to
217 // _Unwind_Resume to the end of the single resume block.
218 ResumeInst *RI = Resumes.front();
219 BasicBlock *UnwindBB = RI->getParent();
Bill Wendling27489fe2012-05-17 17:59:51 +0000220 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000221
222 // Call the _Unwind_Resume function.
223 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
224 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
225
226 // We never expect _Unwind_Resume to return.
227 new UnreachableInst(Ctx, UnwindBB);
228 return true;
229 }
230
Bill Wendling478f58c2011-11-07 23:36:48 +0000231 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000232 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft,
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000233 "exn.obj", UnwindBB);
234
235 // Extract the exception object from the ResumeInst and add it to the PHI node
236 // that feeds the _Unwind_Resume call.
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000237 for (ResumeInst *RI : Resumes) {
Bill Wendling8c090402012-01-28 01:17:56 +0000238 BasicBlock *Parent = RI->getParent();
239 BranchInst::Create(UnwindBB, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000240
Bill Wendling27489fe2012-05-17 17:59:51 +0000241 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000242 PN->addIncoming(ExnObj, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000243
Bill Wendling478f58c2011-11-07 23:36:48 +0000244 ++NumResumesLowered;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000245 }
246
247 // Call the function.
248 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
249 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
250
251 // We never expect _Unwind_Resume to return.
252 new UnreachableInst(Ctx, UnwindBB);
253 return true;
254}
255
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000256bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Reid Kleckner7bb07382015-02-18 23:17:41 +0000257 assert(TM && "DWARF EH preparation requires a target machine");
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000258 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
259 TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
Bill Wendling478f58c2011-11-07 23:36:48 +0000260 bool Changed = InsertUnwindResumeCalls(Fn);
Reid Klecknerbe0a0502015-03-09 22:45:16 +0000261 DT = nullptr;
262 TLI = nullptr;
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000263 return Changed;
264}