blob: 7b47a48391c7bbcbcc029b4c0a37ed9c9abb913e [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"
16#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Function.h"
18#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000020#include "llvm/Pass.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000021#include "llvm/Target/TargetLowering.h"
Eric Christopherd9134482014-08-04 21:25:23 +000022#include "llvm/Target/TargetSubtargetInfo.h"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000023using namespace llvm;
24
Chandler Carruth1b9dde02014-04-22 02:02:50 +000025#define DEBUG_TYPE "dwarfehprepare"
26
Bill Wendling478f58c2011-11-07 23:36:48 +000027STATISTIC(NumResumesLowered, "Number of resume calls lowered");
Duncan Sandsd6fb6502009-05-22 20:36:31 +000028
29namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000030 class DwarfEHPrepare : public FunctionPass {
Bill Wendlingafc10362013-06-19 20:51:24 +000031 const TargetMachine *TM;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000032
Bill Wendling478f58c2011-11-07 23:36:48 +000033 // RewindFunction - _Unwind_Resume or the target equivalent.
Duncan Sandsd6fb6502009-05-22 20:36:31 +000034 Constant *RewindFunction;
35
Bill Wendling478f58c2011-11-07 23:36:48 +000036 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling27489fe2012-05-17 17:59:51 +000037 Value *GetExceptionObject(ResumeInst *RI);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000038
Duncan Sandsd6fb6502009-05-22 20:36:31 +000039 public:
40 static char ID; // Pass identification, replacement for typeid.
Reid Kleckner7bb07382015-02-18 23:17:41 +000041
42 // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in
43 // practice.
Chandler Carruth301ed0c2015-02-20 02:15:36 +000044 DwarfEHPrepare() : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr) {}
Reid Kleckner7bb07382015-02-18 23:17:41 +000045
Chandler Carruth73523022014-01-13 13:07:17 +000046 DwarfEHPrepare(const TargetMachine *TM)
Chandler Carruth301ed0c2015-02-20 02:15:36 +000047 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {}
Duncan Sandsd6fb6502009-05-22 20:36:31 +000048
Craig Topper4584cd52014-03-07 09:26:03 +000049 bool runOnFunction(Function &Fn) override;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000050
Yaron Keren66b0ceb2014-09-14 20:36:28 +000051 bool doFinalization(Module &M) override {
52 RewindFunction = nullptr;
53 return false;
54 }
55
Craig Topper4584cd52014-03-07 09:26:03 +000056 const char *getPassName() const override {
Duncan Sandsd6fb6502009-05-22 20:36:31 +000057 return "Exception handling preparation";
58 }
Duncan Sandsd6fb6502009-05-22 20:36:31 +000059 };
60} // end anonymous namespace
61
62char DwarfEHPrepare::ID = 0;
Chandler Carruth301ed0c2015-02-20 02:15:36 +000063INITIALIZE_TM_PASS(DwarfEHPrepare, "dwarfehprepare", "Prepare DWARF exceptions",
64 false, false)
Duncan Sandsd6fb6502009-05-22 20:36:31 +000065
Bill Wendlingafc10362013-06-19 20:51:24 +000066FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
67 return new DwarfEHPrepare(TM);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000068}
69
Bill Wendling8c090402012-01-28 01:17:56 +000070/// GetExceptionObject - Return the exception object from the value passed into
71/// the 'resume' instruction (typically an aggregate). Clean up any dead
72/// instructions, including the 'resume' instruction.
Bill Wendling27489fe2012-05-17 17:59:51 +000073Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling8c090402012-01-28 01:17:56 +000074 Value *V = RI->getOperand(0);
Craig Topperc0196b12014-04-14 00:51:57 +000075 Value *ExnObj = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +000076 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
Craig Topperc0196b12014-04-14 00:51:57 +000077 LoadInst *SelLoad = nullptr;
78 InsertValueInst *ExcIVI = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +000079 bool EraseIVIs = false;
80
81 if (SelIVI) {
82 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
83 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
84 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
85 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
Bill Wendling27489fe2012-05-17 17:59:51 +000086 ExnObj = ExcIVI->getOperand(1);
Bill Wendling8c090402012-01-28 01:17:56 +000087 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
88 EraseIVIs = true;
89 }
90 }
91 }
92
93 if (!ExnObj)
94 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
95
96 RI->eraseFromParent();
97
98 if (EraseIVIs) {
Benjamin Kramereb63e4d2015-01-29 13:26:50 +000099 if (SelIVI->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000100 SelIVI->eraseFromParent();
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000101 if (ExcIVI->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000102 ExcIVI->eraseFromParent();
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000103 if (SelLoad && SelLoad->use_empty())
Bill Wendling8c090402012-01-28 01:17:56 +0000104 SelLoad->eraseFromParent();
105 }
106
107 return ExnObj;
108}
109
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000110/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
111/// into calls to the appropriate _Unwind_Resume function.
Bill Wendling478f58c2011-11-07 23:36:48 +0000112bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000113 SmallVector<ResumeInst*, 16> Resumes;
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000114 for (BasicBlock &BB : Fn) {
115 if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
Bill Wendling8ac20412011-08-25 23:48:11 +0000116 Resumes.push_back(RI);
Bill Wendling8ac20412011-08-25 23:48:11 +0000117 }
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000118
119 if (Resumes.empty())
Kai Nackee1823b62013-05-31 16:30:36 +0000120 return false;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000121
Reid Kleckner0b647e62015-02-20 01:00:19 +0000122 // Find the rewind function if we didn't already.
Chandler Carruth301ed0c2015-02-20 02:15:36 +0000123 const TargetLowering *TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
124 LLVMContext &Ctx = Fn.getContext();
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000125 if (!RewindFunction) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000126 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
127 Type::getInt8PtrTy(Ctx), false);
128 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
Bill Wendling478f58c2011-11-07 23:36:48 +0000129 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000130 }
131
132 // Create the basic block where the _Unwind_Resume call will live.
Chandler Carruth301ed0c2015-02-20 02:15:36 +0000133 unsigned ResumesSize = Resumes.size();
134
135 if (ResumesSize == 1) {
Bill Wendling8c090402012-01-28 01:17:56 +0000136 // 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 Wendling27489fe2012-05-17 17:59:51 +0000140 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-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 Wendling478f58c2011-11-07 23:36:48 +0000151 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Chandler Carruth301ed0c2015-02-20 02:15:36 +0000152 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
Bill Wendling1cdd7fd2011-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.
Benjamin Kramereb63e4d2015-01-29 13:26:50 +0000157 for (ResumeInst *RI : Resumes) {
Bill Wendling8c090402012-01-28 01:17:56 +0000158 BasicBlock *Parent = RI->getParent();
159 BranchInst::Create(UnwindBB, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000160
Bill Wendling27489fe2012-05-17 17:59:51 +0000161 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000162 PN->addIncoming(ExnObj, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000163
Bill Wendling478f58c2011-11-07 23:36:48 +0000164 ++NumResumesLowered;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000165 }
166
167 // Call the function.
168 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
169 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
170
171 // We never expect _Unwind_Resume to return.
172 new UnreachableInst(Ctx, UnwindBB);
173 return true;
174}
175
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000176bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Reid Kleckner7bb07382015-02-18 23:17:41 +0000177 assert(TM && "DWARF EH preparation requires a target machine");
Bill Wendling478f58c2011-11-07 23:36:48 +0000178 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000179 return Changed;
180}