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.
42 // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in
44 DwarfEHPrepare() : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr) {}
46 DwarfEHPrepare(const TargetMachine *TM)
47 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {}
49 bool runOnFunction(Function &Fn) override;
51 bool doFinalization(Module &M) override {
52 RewindFunction = nullptr;
56 const char *getPassName() const override {
57 return "Exception handling preparation";
60 } // end anonymous namespace
62 char DwarfEHPrepare::ID = 0;
63 INITIALIZE_TM_PASS(DwarfEHPrepare, "dwarfehprepare", "Prepare DWARF exceptions",
66 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
67 return new DwarfEHPrepare(TM);
70 /// 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.
73 Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
74 Value *V = RI->getOperand(0);
75 Value *ExnObj = nullptr;
76 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
77 LoadInst *SelLoad = nullptr;
78 InsertValueInst *ExcIVI = nullptr;
79 bool EraseIVIs = false;
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) {
86 ExnObj = ExcIVI->getOperand(1);
87 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
94 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
96 RI->eraseFromParent();
99 if (SelIVI->use_empty())
100 SelIVI->eraseFromParent();
101 if (ExcIVI->use_empty())
102 ExcIVI->eraseFromParent();
103 if (SelLoad && SelLoad->use_empty())
104 SelLoad->eraseFromParent();
110 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
111 /// into calls to the appropriate _Unwind_Resume function.
112 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
113 SmallVector<ResumeInst*, 16> Resumes;
114 for (BasicBlock &BB : Fn) {
115 if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
116 Resumes.push_back(RI);
122 // Find the rewind function if we didn't already.
123 const TargetLowering *TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
124 LLVMContext &Ctx = Fn.getContext();
125 if (!RewindFunction) {
126 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
127 Type::getInt8PtrTy(Ctx), false);
128 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
129 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
132 // Create the basic block where the _Unwind_Resume call will live.
133 unsigned ResumesSize = Resumes.size();
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();
140 Value *ExnObj = GetExceptionObject(RI);
142 // Call the _Unwind_Resume function.
143 CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
144 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
146 // We never expect _Unwind_Resume to return.
147 new UnreachableInst(Ctx, UnwindBB);
151 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
152 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
153 "exn.obj", UnwindBB);
155 // Extract the exception object from the ResumeInst and add it to the PHI node
156 // that feeds the _Unwind_Resume call.
157 for (ResumeInst *RI : Resumes) {
158 BasicBlock *Parent = RI->getParent();
159 BranchInst::Create(UnwindBB, Parent);
161 Value *ExnObj = GetExceptionObject(RI);
162 PN->addIncoming(ExnObj, Parent);
167 // Call the function.
168 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
169 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
171 // We never expect _Unwind_Resume to return.
172 new UnreachableInst(Ctx, UnwindBB);
176 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
177 assert(TM && "DWARF EH preparation requires a target machine");
178 bool Changed = InsertUnwindResumeCalls(Fn);