1 //===-- DwarfEHPrepare - Prepare exception handling for code generation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass mulches exception handling code into a form adapted to code
11 // generation. Required if using dwarf exception handling.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetSubtargetInfo.h"
25 #define DEBUG_TYPE "dwarfehprepare"
27 STATISTIC(NumResumesLowered, "Number of resume calls lowered");
30 class DwarfEHPrepare : public FunctionPass {
31 const TargetMachine *TM;
33 // RewindFunction - _Unwind_Resume or the target equivalent.
34 Constant *RewindFunction;
36 bool InsertUnwindResumeCalls(Function &Fn);
37 Value *GetExceptionObject(ResumeInst *RI);
40 static char ID; // Pass identification, replacement for typeid.
41 DwarfEHPrepare(const TargetMachine *TM)
42 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {}
44 bool runOnFunction(Function &Fn) override;
46 bool doFinalization(Module &M) override {
47 RewindFunction = nullptr;
51 const char *getPassName() const override {
52 return "Exception handling preparation";
55 } // end anonymous namespace
57 char DwarfEHPrepare::ID = 0;
59 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
60 return new DwarfEHPrepare(TM);
63 /// GetExceptionObject - Return the exception object from the value passed into
64 /// the 'resume' instruction (typically an aggregate). Clean up any dead
65 /// instructions, including the 'resume' instruction.
66 Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
67 Value *V = RI->getOperand(0);
68 Value *ExnObj = nullptr;
69 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
70 LoadInst *SelLoad = nullptr;
71 InsertValueInst *ExcIVI = nullptr;
72 bool EraseIVIs = false;
75 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {
76 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
77 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&
78 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {
79 ExnObj = ExcIVI->getOperand(1);
80 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
87 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
89 RI->eraseFromParent();
92 if (SelIVI->use_empty())
93 SelIVI->eraseFromParent();
94 if (ExcIVI->use_empty())
95 ExcIVI->eraseFromParent();
96 if (SelLoad && SelLoad->use_empty())
97 SelLoad->eraseFromParent();
103 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
104 /// into calls to the appropriate _Unwind_Resume function.
105 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
106 SmallVector<ResumeInst*, 16> Resumes;
107 for (BasicBlock &BB : Fn) {
108 if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
109 Resumes.push_back(RI);
115 // Find the rewind function if we didn't already.
116 const TargetLowering *TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
117 LLVMContext &Ctx = Fn.getContext();
118 if (!RewindFunction) {
119 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
120 Type::getInt8PtrTy(Ctx), false);
121 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
122 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
125 // Create the basic block where the _Unwind_Resume call will live.
126 unsigned ResumesSize = Resumes.size();
128 if (ResumesSize == 1) {
129 // Instead of creating a new BB and PHI node, just append the call to
130 // _Unwind_Resume to the end of the single resume block.
131 ResumeInst *RI = Resumes.front();
132 BasicBlock *UnwindBB = RI->getParent();
133 Value *ExnObj = GetExceptionObject(RI);
135 // Call the _Unwind_Resume function.
136 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
137 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
139 // We never expect _Unwind_Resume to return.
140 new UnreachableInst(Ctx, UnwindBB);
144 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
145 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
146 "exn.obj", UnwindBB);
148 // Extract the exception object from the ResumeInst and add it to the PHI node
149 // that feeds the _Unwind_Resume call.
150 for (ResumeInst *RI : Resumes) {
151 BasicBlock *Parent = RI->getParent();
152 BranchInst::Create(UnwindBB, Parent);
154 Value *ExnObj = GetExceptionObject(RI);
155 PN->addIncoming(ExnObj, Parent);
160 // Call the function.
161 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
162 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
164 // We never expect _Unwind_Resume to return.
165 new UnreachableInst(Ctx, UnwindBB);
169 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
170 bool Changed = InsertUnwindResumeCalls(Fn);