blob: a195586cf6247fd60f3bae5d8f2ba3b7d388bb68 [file] [log] [blame]
Duncan Sandsb0f1e172009-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 Wendling2ad4aca2010-03-26 23:41:30 +000011// generation. Required if using dwarf exception handling.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
16#include "llvm/ADT/Statistic.h"
Stephen Hines36b56882014-04-23 16:57:46 -070017#include "llvm/IR/CallSite.h"
18#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-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 Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/MC/MCAsmInfo.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000024#include "llvm/Pass.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000025#include "llvm/Target/TargetLowering.h"
26#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Duncan Sands850fcd42010-09-03 08:31:48 +000027#include "llvm/Transforms/Utils/SSAUpdater.h"
Duncan Sandsb0f1e172009-05-22 20:36:31 +000028using namespace llvm;
29
Stephen Hinesdce4a402014-05-29 02:49:00 -070030#define DEBUG_TYPE "dwarfehprepare"
31
Bill Wendlinge13eba22011-11-07 23:36:48 +000032STATISTIC(NumResumesLowered, "Number of resume calls lowered");
Duncan Sandsb0f1e172009-05-22 20:36:31 +000033
34namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000035 class DwarfEHPrepare : public FunctionPass {
Bill Wendlingea442812013-06-19 20:51:24 +000036 const TargetMachine *TM;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000037
Bill Wendlinge13eba22011-11-07 23:36:48 +000038 // RewindFunction - _Unwind_Resume or the target equivalent.
Duncan Sandsb0f1e172009-05-22 20:36:31 +000039 Constant *RewindFunction;
40
Bill Wendlinge13eba22011-11-07 23:36:48 +000041 bool InsertUnwindResumeCalls(Function &Fn);
Bill Wendling29424e82012-05-17 17:59:51 +000042 Value *GetExceptionObject(ResumeInst *RI);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000043
Duncan Sandsb0f1e172009-05-22 20:36:31 +000044 public:
45 static char ID; // Pass identification, replacement for typeid.
Stephen Hines36b56882014-04-23 16:57:46 -070046 DwarfEHPrepare(const TargetMachine *TM)
Stephen Hinesdce4a402014-05-29 02:49:00 -070047 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {
Stephen Hines36b56882014-04-23 16:57:46 -070048 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
49 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000050
Stephen Hines36b56882014-04-23 16:57:46 -070051 bool runOnFunction(Function &Fn) override;
Duncan Sandsb0f1e172009-05-22 20:36:31 +000052
Stephen Hines36b56882014-04-23 16:57:46 -070053 void getAnalysisUsage(AnalysisUsage &AU) const override { }
Bill Wendling8bedf972009-10-29 00:37:35 +000054
Stephen Hines36b56882014-04-23 16:57:46 -070055 const char *getPassName() const override {
Duncan Sandsb0f1e172009-05-22 20:36:31 +000056 return "Exception handling preparation";
57 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +000058 };
59} // end anonymous namespace
60
61char DwarfEHPrepare::ID = 0;
62
Bill Wendlingea442812013-06-19 20:51:24 +000063FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
64 return new DwarfEHPrepare(TM);
Duncan Sandsb0f1e172009-05-22 20:36:31 +000065}
66
Bill Wendling0ae06de2012-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 Wendling29424e82012-05-17 17:59:51 +000070Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
Bill Wendling0ae06de2012-01-28 01:17:56 +000071 Value *V = RI->getOperand(0);
Stephen Hinesdce4a402014-05-29 02:49:00 -070072 Value *ExnObj = nullptr;
Bill Wendling0ae06de2012-01-28 01:17:56 +000073 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
Stephen Hinesdce4a402014-05-29 02:49:00 -070074 LoadInst *SelLoad = nullptr;
75 InsertValueInst *ExcIVI = nullptr;
Bill Wendling0ae06de2012-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 Wendling29424e82012-05-17 17:59:51 +000083 ExnObj = ExcIVI->getOperand(1);
Bill Wendling0ae06de2012-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 Wendling35adbb32011-08-17 19:48:49 +0000107/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
108/// into calls to the appropriate _Unwind_Resume function.
Bill Wendlinge13eba22011-11-07 23:36:48 +0000109bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
Bill Wendling35adbb32011-08-17 19:48:49 +0000110 SmallVector<ResumeInst*, 16> Resumes;
Bill Wendlinge13eba22011-11-07 23:36:48 +0000111 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
Bill Wendling09908c42011-08-25 23:48:11 +0000112 TerminatorInst *TI = I->getTerminator();
113 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
114 Resumes.push_back(RI);
Bill Wendling09908c42011-08-25 23:48:11 +0000115 }
Bill Wendling35adbb32011-08-17 19:48:49 +0000116
117 if (Resumes.empty())
Kai Nacke52aaf6a2013-05-31 16:30:36 +0000118 return false;
Bill Wendling35adbb32011-08-17 19:48:49 +0000119
120 // Find the rewind function if we didn't already.
Bill Wendlingea442812013-06-19 20:51:24 +0000121 const TargetLowering *TLI = TM->getTargetLowering();
Bill Wendling35adbb32011-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 Wendlinge13eba22011-11-07 23:36:48 +0000127 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
Bill Wendling35adbb32011-08-17 19:48:49 +0000128 }
129
130 // Create the basic block where the _Unwind_Resume call will live.
Bill Wendlinge13eba22011-11-07 23:36:48 +0000131 LLVMContext &Ctx = Fn.getContext();
Bill Wendling0ae06de2012-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 Wendling29424e82012-05-17 17:59:51 +0000139 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling0ae06de2012-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 Wendlinge13eba22011-11-07 23:36:48 +0000150 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
Bill Wendling0ae06de2012-01-28 01:17:56 +0000151 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
Bill Wendling35adbb32011-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 Wendling0ae06de2012-01-28 01:17:56 +0000159 BasicBlock *Parent = RI->getParent();
160 BranchInst::Create(UnwindBB, Parent);
Bill Wendlingb618ea52012-01-20 00:53:28 +0000161
Bill Wendling29424e82012-05-17 17:59:51 +0000162 Value *ExnObj = GetExceptionObject(RI);
Bill Wendling0ae06de2012-01-28 01:17:56 +0000163 PN->addIncoming(ExnObj, Parent);
Bill Wendlingb618ea52012-01-20 00:53:28 +0000164
Bill Wendlinge13eba22011-11-07 23:36:48 +0000165 ++NumResumesLowered;
Bill Wendling35adbb32011-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 Sandsb0f1e172009-05-22 20:36:31 +0000177bool DwarfEHPrepare::runOnFunction(Function &Fn) {
Bill Wendlinge13eba22011-11-07 23:36:48 +0000178 bool Changed = InsertUnwindResumeCalls(Fn);
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000179 return Changed;
180}