blob: 705e5d657c39da1c81cfbecc7ca86a8c216ea579 [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 Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000018#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-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 Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCAsmInfo.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"
Duncan Sandsd6fb6502009-05-22 20:36:31 +000027#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Duncan Sandsbc42c902010-09-03 08:31:48 +000028#include "llvm/Transforms/Utils/SSAUpdater.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
Bill Wendling478f58c2011-11-07 23:36:48 +000042 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling27489fe2012-05-17 17:59:51 +000043 Value *GetExceptionObject(ResumeInst *RI);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000044
Duncan Sandsd6fb6502009-05-22 20:36:31 +000045 public:
46 static char ID; // Pass identification, replacement for typeid.
Chandler Carruth73523022014-01-13 13:07:17 +000047 DwarfEHPrepare(const TargetMachine *TM)
Craig Topperc0196b12014-04-14 00:51:57 +000048 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {
Chandler Carruth73523022014-01-13 13:07:17 +000049 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
50 }
Duncan Sandsd6fb6502009-05-22 20:36:31 +000051
Craig Topper4584cd52014-03-07 09:26:03 +000052 bool runOnFunction(Function &Fn) override;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000053
Craig Topper4584cd52014-03-07 09:26:03 +000054 void getAnalysisUsage(AnalysisUsage &AU) const override { }
Bill Wendling3505c942009-10-29 00:37:35 +000055
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;
63
Bill Wendlingafc10362013-06-19 20:51:24 +000064FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
65 return new DwarfEHPrepare(TM);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000066}
67
Bill Wendling8c090402012-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.
Bill Wendling27489fe2012-05-17 17:59:51 +000071Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling8c090402012-01-28 01:17:56 +000072 Value *V = RI->getOperand(0);
Craig Topperc0196b12014-04-14 00:51:57 +000073 Value *ExnObj = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +000074 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
Craig Topperc0196b12014-04-14 00:51:57 +000075 LoadInst *SelLoad = nullptr;
76 InsertValueInst *ExcIVI = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +000077 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) {
Bill Wendling27489fe2012-05-17 17:59:51 +000084 ExnObj = ExcIVI->getOperand(1);
Bill Wendling8c090402012-01-28 01:17:56 +000085 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 Wendling1cdd7fd2011-08-17 19:48:49 +0000108/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
109/// into calls to the appropriate _Unwind_Resume function.
Bill Wendling478f58c2011-11-07 23:36:48 +0000110bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000111 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendling478f58c2011-11-07 23:36:48 +0000112 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Bill Wendling8ac20412011-08-25 23:48:11 +0000113 TerminatorInst *TI = I->getTerminator();
114 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
115 Resumes.push_back(RI);
Bill Wendling8ac20412011-08-25 23:48:11 +0000116 }
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000117
118 if (Resumes.empty())
Kai Nackee1823b62013-05-31 16:30:36 +0000119 return false;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000120
121 // Find the rewind function if we didn't already.
Eric Christopherd9134482014-08-04 21:25:23 +0000122 const TargetLowering *TLI = TM->getSubtargetImpl()->getTargetLowering();
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000123 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 Wendling478f58c2011-11-07 23:36:48 +0000128 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000129 }
130
131 // Create the basic block where the _Unwind_Resume call will live.
Bill Wendling478f58c2011-11-07 23:36:48 +0000132 LLVMContext &Ctx = Fn.getContext();
Bill Wendling8c090402012-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 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);
Bill Wendling8c090402012-01-28 01:17:56 +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.
157 for (SmallVectorImpl<ResumeInst*>::iterator
158 I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
159 ResumeInst *RI = *I;
Bill Wendling8c090402012-01-28 01:17:56 +0000160 BasicBlock *Parent = RI->getParent();
161 BranchInst::Create(UnwindBB, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000162
Bill Wendling27489fe2012-05-17 17:59:51 +0000163 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000164 PN->addIncoming(ExnObj, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000165
Bill Wendling478f58c2011-11-07 23:36:48 +0000166 ++NumResumesLowered;
Bill Wendling1cdd7fd2011-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 Sandsd6fb6502009-05-22 20:36:31 +0000178bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Bill Wendling478f58c2011-11-07 23:36:48 +0000179 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000180 return Changed;
181}