blob: a195586cf6247fd60f3bae5d8f2ba3b7d388bb68 [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"
26#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Duncan Sandsbc42c902010-09-03 08:31:48 +000027#include "llvm/Transforms/Utils/SSAUpdater.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
Bill Wendling478f58c2011-11-07 23:36:48 +000041 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling27489fe2012-05-17 17:59:51 +000042 Value *GetExceptionObject(ResumeInst *RI);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000043
Duncan Sandsd6fb6502009-05-22 20:36:31 +000044 public:
45 static char ID; // Pass identification, replacement for typeid.
Chandler Carruth73523022014-01-13 13:07:17 +000046 DwarfEHPrepare(const TargetMachine *TM)
Craig Topperc0196b12014-04-14 00:51:57 +000047 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {
Chandler Carruth73523022014-01-13 13:07:17 +000048 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
49 }
Duncan Sandsd6fb6502009-05-22 20:36:31 +000050
Craig Topper4584cd52014-03-07 09:26:03 +000051 bool runOnFunction(Function &Fn) override;
Duncan Sandsd6fb6502009-05-22 20:36:31 +000052
Craig Topper4584cd52014-03-07 09:26:03 +000053 void getAnalysisUsage(AnalysisUsage &AU) const override { }
Bill Wendling3505c942009-10-29 00:37:35 +000054
Craig Topper4584cd52014-03-07 09:26:03 +000055 const char *getPassName() const override {
Duncan Sandsd6fb6502009-05-22 20:36:31 +000056 return "Exception handling preparation";
57 }
Duncan Sandsd6fb6502009-05-22 20:36:31 +000058 };
59} // end anonymous namespace
60
61char DwarfEHPrepare::ID = 0;
62
Bill Wendlingafc10362013-06-19 20:51:24 +000063FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
64 return new DwarfEHPrepare(TM);
Duncan Sandsd6fb6502009-05-22 20:36:31 +000065}
66
Bill Wendling8c090402012-01-28 01:17:56 +000067/// GetExceptionObject - Return the exception object from the value passed into
68/// the 'resume' instruction (typically an aggregate). Clean up any dead
69/// instructions, including the 'resume' instruction.
Bill Wendling27489fe2012-05-17 17:59:51 +000070Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling8c090402012-01-28 01:17:56 +000071 Value *V = RI->getOperand(0);
Craig Topperc0196b12014-04-14 00:51:57 +000072 Value *ExnObj = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +000073 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
Craig Topperc0196b12014-04-14 00:51:57 +000074 LoadInst *SelLoad = nullptr;
75 InsertValueInst *ExcIVI = nullptr;
Bill Wendling8c090402012-01-28 01:17:56 +000076 bool EraseIVIs = false;
77
78 if (SelIVI) {
79 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
80 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
81 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
82 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
Bill Wendling27489fe2012-05-17 17:59:51 +000083 ExnObj = ExcIVI->getOperand(1);
Bill Wendling8c090402012-01-28 01:17:56 +000084 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
85 EraseIVIs = true;
86 }
87 }
88 }
89
90 if (!ExnObj)
91 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
92
93 RI->eraseFromParent();
94
95 if (EraseIVIs) {
96 if (SelIVI->getNumUses() == 0)
97 SelIVI->eraseFromParent();
98 if (ExcIVI->getNumUses() == 0)
99 ExcIVI->eraseFromParent();
100 if (SelLoad && SelLoad->getNumUses() == 0)
101 SelLoad->eraseFromParent();
102 }
103
104 return ExnObj;
105}
106
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000107/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
108/// into calls to the appropriate _Unwind_Resume function.
Bill Wendling478f58c2011-11-07 23:36:48 +0000109bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000110 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendling478f58c2011-11-07 23:36:48 +0000111 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Bill Wendling8ac20412011-08-25 23:48:11 +0000112 TerminatorInst *TI = I->getTerminator();
113 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
114 Resumes.push_back(RI);
Bill Wendling8ac20412011-08-25 23:48:11 +0000115 }
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000116
117 if (Resumes.empty())
Kai Nackee1823b62013-05-31 16:30:36 +0000118 return false;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000119
120 // Find the rewind function if we didn't already.
Bill Wendlingafc10362013-06-19 20:51:24 +0000121 const TargetLowering *TLI = TM->getTargetLowering();
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000122 if (!RewindFunction) {
123 LLVMContext &Ctx = Resumes[0]->getContext();
124 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
125 Type::getInt8PtrTy(Ctx), false);
126 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
Bill Wendling478f58c2011-11-07 23:36:48 +0000127 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000128 }
129
130 // Create the basic block where the _Unwind_Resume call will live.
Bill Wendling478f58c2011-11-07 23:36:48 +0000131 LLVMContext &Ctx = Fn.getContext();
Bill Wendling8c090402012-01-28 01:17:56 +0000132 unsigned ResumesSize = Resumes.size();
133
134 if (ResumesSize == 1) {
135 // Instead of creating a new BB and PHI node, just append the call to
136 // _Unwind_Resume to the end of the single resume block.
137 ResumeInst *RI = Resumes.front();
138 BasicBlock *UnwindBB = RI->getParent();
Bill Wendling27489fe2012-05-17 17:59:51 +0000139 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000140
141 // Call the _Unwind_Resume function.
142 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
143 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
144
145 // We never expect _Unwind_Resume to return.
146 new UnreachableInst(Ctx, UnwindBB);
147 return true;
148 }
149
Bill Wendling478f58c2011-11-07 23:36:48 +0000150 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Bill Wendling8c090402012-01-28 01:17:56 +0000151 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000152 "exn.obj", UnwindBB);
153
154 // Extract the exception object from the ResumeInst and add it to the PHI node
155 // that feeds the _Unwind_Resume call.
156 for (SmallVectorImpl<ResumeInst*>::iterator
157 I = Resumes.begin(), E = Resumes.end(); I != E; ++I) {
158 ResumeInst *RI = *I;
Bill Wendling8c090402012-01-28 01:17:56 +0000159 BasicBlock *Parent = RI->getParent();
160 BranchInst::Create(UnwindBB, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000161
Bill Wendling27489fe2012-05-17 17:59:51 +0000162 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling8c090402012-01-28 01:17:56 +0000163 PN->addIncoming(ExnObj, Parent);
Bill Wendling92492612012-01-20 00:53:28 +0000164
Bill Wendling478f58c2011-11-07 23:36:48 +0000165 ++NumResumesLowered;
Bill Wendling1cdd7fd2011-08-17 19:48:49 +0000166 }
167
168 // Call the function.
169 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
170 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
171
172 // We never expect _Unwind_Resume to return.
173 new UnreachableInst(Ctx, UnwindBB);
174 return true;
175}
176
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000177bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Bill Wendling478f58c2011-11-07 23:36:48 +0000178 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000179 return Changed;
180}