]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - lib/CodeGen/DwarfEHPrepare.cpp
DebugInfo: Simplify solution to avoid DW_AT_artificial on inlined parameters.
[opencl/llvm.git] / lib / CodeGen / DwarfEHPrepare.cpp
1 //===-- 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
11 // generation. Required if using dwarf exception handling.
12 //
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/CallSite.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Target/TargetLowering.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #include "llvm/Transforms/Utils/SSAUpdater.h"
28 using namespace llvm;
30 #define DEBUG_TYPE "dwarfehprepare"
32 STATISTIC(NumResumesLowered, "Number of resume calls lowered");
34 namespace {
35   class DwarfEHPrepare : public FunctionPass {
36     const TargetMachine *TM;
38     // RewindFunction - _Unwind_Resume or the target equivalent.
39     Constant *RewindFunction;
41     bool InsertUnwindResumeCalls(Function &Fn);
42     Value *GetExceptionObject(ResumeInst *RI);
44   public:
45     static char ID; // Pass identification, replacement for typeid.
46     DwarfEHPrepare(const TargetMachine *TM)
47         : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {
48       initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
49     }
51     bool runOnFunction(Function &Fn) override;
53     void getAnalysisUsage(AnalysisUsage &AU) const override { }
55     const char *getPassName() const override {
56       return "Exception handling preparation";
57     }
58   };
59 } // end anonymous namespace
61 char DwarfEHPrepare::ID = 0;
63 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {
64   return new DwarfEHPrepare(TM);
65 }
67 /// 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.
70 Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
71   Value *V = RI->getOperand(0);
72   Value *ExnObj = nullptr;
73   InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
74   LoadInst *SelLoad = nullptr;
75   InsertValueInst *ExcIVI = nullptr;
76   bool EraseIVIs = false;
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) {
83         ExnObj = ExcIVI->getOperand(1);
84         SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
85         EraseIVIs = true;
86       }
87     }
88   }
90   if (!ExnObj)
91     ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
93   RI->eraseFromParent();
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   }
104   return ExnObj;
107 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
108 /// into calls to the appropriate _Unwind_Resume function.
109 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
110   SmallVector<ResumeInst*, 16> Resumes;
111   for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
112     TerminatorInst *TI = I->getTerminator();
113     if (ResumeInst *RI = dyn_cast<ResumeInst>(TI))
114       Resumes.push_back(RI);
115   }
117   if (Resumes.empty())
118     return false;
120   // Find the rewind function if we didn't already.
121   const TargetLowering *TLI = TM->getTargetLowering();
122   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);
127     RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
128   }
130   // Create the basic block where the _Unwind_Resume call will live.
131   LLVMContext &Ctx = Fn.getContext();
132   unsigned ResumesSize = Resumes.size();
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();
139     Value *ExnObj = GetExceptionObject(RI);
141     // Call the _Unwind_Resume function.
142     CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
143     CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
145     // We never expect _Unwind_Resume to return.
146     new UnreachableInst(Ctx, UnwindBB);
147     return true;
148   }
150   BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
151   PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,
152                                 "exn.obj", UnwindBB);
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;
159     BasicBlock *Parent = RI->getParent();
160     BranchInst::Create(UnwindBB, Parent);
162     Value *ExnObj = GetExceptionObject(RI);
163     PN->addIncoming(ExnObj, Parent);
165     ++NumResumesLowered;
166   }
168   // Call the function.
169   CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
170   CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
172   // We never expect _Unwind_Resume to return.
173   new UnreachableInst(Ctx, UnwindBB);
174   return true;
177 bool DwarfEHPrepare::runOnFunction(Function &Fn) {
178   bool Changed = InsertUnwindResumeCalls(Fn);
179   return Changed;