Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 1 | //===- DwarfEHPrepare - Prepare exception handling for code generation ----===// |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 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 Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 11 | // generation. Required if using dwarf exception handling. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CFG.h" |
David Majnemer | 1114aa2 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/EHPersonalities.h" |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
David Blaikie | 8325fb2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/Local.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/RuntimeLibcalls.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/TargetLowering.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/TargetPassConfig.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 26 | #include "llvm/IR/BasicBlock.h" |
| 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Type.h" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 34 | #include "llvm/Pass.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Casting.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 37 | #include <cstddef> |
| 38 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "dwarfehprepare" |
| 42 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 43 | STATISTIC(NumResumesLowered, "Number of resume calls lowered"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 46 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 47 | class DwarfEHPrepare : public FunctionPass { |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 48 | // RewindFunction - _Unwind_Resume or the target equivalent. |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 49 | Constant *RewindFunction = nullptr; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 50 | |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 51 | DominatorTree *DT = nullptr; |
| 52 | const TargetLowering *TLI = nullptr; |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 53 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 54 | bool InsertUnwindResumeCalls(Function &Fn); |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 55 | Value *GetExceptionObject(ResumeInst *RI); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 56 | size_t |
| 57 | pruneUnreachableResumes(Function &Fn, |
| 58 | SmallVectorImpl<ResumeInst *> &Resumes, |
| 59 | SmallVectorImpl<LandingPadInst *> &CleanupLPads); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 60 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 61 | public: |
| 62 | static char ID; // Pass identification, replacement for typeid. |
Reid Kleckner | f89d9b1 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 63 | |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 64 | DwarfEHPrepare() : FunctionPass(ID) {} |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 65 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 66 | bool runOnFunction(Function &Fn) override; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 67 | |
Yaron Keren | e1c77ca | 2014-09-14 20:36:28 +0000 | [diff] [blame] | 68 | bool doFinalization(Module &M) override { |
| 69 | RewindFunction = nullptr; |
| 70 | return false; |
| 71 | } |
| 72 | |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 73 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 74 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 75 | StringRef getPassName() const override { |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 76 | return "Exception handling preparation"; |
| 77 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 78 | }; |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 79 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 80 | } // end anonymous namespace |
| 81 | |
| 82 | char DwarfEHPrepare::ID = 0; |
Eugene Zelenko | 3840975 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 83 | |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 84 | INITIALIZE_PASS_BEGIN(DwarfEHPrepare, DEBUG_TYPE, |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 85 | "Prepare DWARF exceptions", false, false) |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 86 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 87 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 88 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 89 | INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE, |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 90 | "Prepare DWARF exceptions", false, false) |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 91 | |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 92 | FunctionPass *llvm::createDwarfEHPass() { return new DwarfEHPrepare(); } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 93 | |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 94 | void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 95 | AU.addRequired<TargetPassConfig>(); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 96 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 97 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 98 | } |
| 99 | |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 100 | /// GetExceptionObject - Return the exception object from the value passed into |
| 101 | /// the 'resume' instruction (typically an aggregate). Clean up any dead |
| 102 | /// instructions, including the 'resume' instruction. |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 103 | Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 104 | Value *V = RI->getOperand(0); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 105 | Value *ExnObj = nullptr; |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 106 | InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 107 | LoadInst *SelLoad = nullptr; |
| 108 | InsertValueInst *ExcIVI = nullptr; |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 109 | bool EraseIVIs = false; |
| 110 | |
| 111 | if (SelIVI) { |
| 112 | if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { |
| 113 | ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); |
| 114 | if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && |
| 115 | ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 116 | ExnObj = ExcIVI->getOperand(1); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 117 | SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); |
| 118 | EraseIVIs = true; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (!ExnObj) |
| 124 | ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); |
| 125 | |
| 126 | RI->eraseFromParent(); |
| 127 | |
| 128 | if (EraseIVIs) { |
Benjamin Kramer | 85ffc54 | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 129 | if (SelIVI->use_empty()) |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 130 | SelIVI->eraseFromParent(); |
Benjamin Kramer | 85ffc54 | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 131 | if (ExcIVI->use_empty()) |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 132 | ExcIVI->eraseFromParent(); |
Benjamin Kramer | 85ffc54 | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 133 | if (SelLoad && SelLoad->use_empty()) |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 134 | SelLoad->eraseFromParent(); |
| 135 | } |
| 136 | |
| 137 | return ExnObj; |
| 138 | } |
| 139 | |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 140 | /// Replace resumes that are not reachable from a cleanup landing pad with |
| 141 | /// unreachable and then simplify those blocks. |
| 142 | size_t DwarfEHPrepare::pruneUnreachableResumes( |
| 143 | Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes, |
| 144 | SmallVectorImpl<LandingPadInst *> &CleanupLPads) { |
| 145 | BitVector ResumeReachable(Resumes.size()); |
| 146 | size_t ResumeIndex = 0; |
| 147 | for (auto *RI : Resumes) { |
| 148 | for (auto *LP : CleanupLPads) { |
| 149 | if (isPotentiallyReachable(LP, RI, DT)) { |
| 150 | ResumeReachable.set(ResumeIndex); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | ++ResumeIndex; |
| 155 | } |
| 156 | |
| 157 | // If everything is reachable, there is no change. |
| 158 | if (ResumeReachable.all()) |
| 159 | return Resumes.size(); |
| 160 | |
| 161 | const TargetTransformInfo &TTI = |
| 162 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn); |
| 163 | LLVMContext &Ctx = Fn.getContext(); |
| 164 | |
| 165 | // Otherwise, insert unreachable instructions and call simplifycfg. |
| 166 | size_t ResumesLeft = 0; |
| 167 | for (size_t I = 0, E = Resumes.size(); I < E; ++I) { |
| 168 | ResumeInst *RI = Resumes[I]; |
| 169 | if (ResumeReachable[I]) { |
| 170 | Resumes[ResumesLeft++] = RI; |
| 171 | } else { |
| 172 | BasicBlock *BB = RI->getParent(); |
| 173 | new UnreachableInst(Ctx, RI); |
| 174 | RI->eraseFromParent(); |
Sanjay Patel | dfc9ea2 | 2017-10-04 20:26:25 +0000 | [diff] [blame] | 175 | simplifyCFG(BB, TTI); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | Resumes.resize(ResumesLeft); |
| 179 | return ResumesLeft; |
| 180 | } |
| 181 | |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 182 | /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present |
| 183 | /// into calls to the appropriate _Unwind_Resume function. |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 184 | bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 185 | SmallVector<ResumeInst*, 16> Resumes; |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 186 | SmallVector<LandingPadInst*, 16> CleanupLPads; |
Benjamin Kramer | 85ffc54 | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 187 | for (BasicBlock &BB : Fn) { |
| 188 | if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 189 | Resumes.push_back(RI); |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 190 | if (auto *LP = BB.getLandingPadInst()) |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 191 | if (LP->isCleanup()) |
| 192 | CleanupLPads.push_back(LP); |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 193 | } |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 194 | |
| 195 | if (Resumes.empty()) |
Kai Nacke | 52aaf6a | 2013-05-31 16:30:36 +0000 | [diff] [blame] | 196 | return false; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 197 | |
Heejin Ahn | 5b752cf | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 198 | // Check the personality, don't do anything if it's scope-based. |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 199 | EHPersonality Pers = classifyEHPersonality(Fn.getPersonalityFn()); |
Heejin Ahn | 5b752cf | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 200 | if (isScopedEHPersonality(Pers)) |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 201 | return false; |
| 202 | |
Chandler Carruth | 90b8e79 | 2015-02-20 02:15:36 +0000 | [diff] [blame] | 203 | LLVMContext &Ctx = Fn.getContext(); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 204 | |
| 205 | size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads); |
| 206 | if (ResumesLeft == 0) |
| 207 | return true; // We pruned them all. |
| 208 | |
| 209 | // Find the rewind function if we didn't already. |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 210 | if (!RewindFunction) { |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 211 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 212 | Type::getInt8PtrTy(Ctx), false); |
| 213 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 214 | RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Create the basic block where the _Unwind_Resume call will live. |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 218 | if (ResumesLeft == 1) { |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 219 | // Instead of creating a new BB and PHI node, just append the call to |
| 220 | // _Unwind_Resume to the end of the single resume block. |
| 221 | ResumeInst *RI = Resumes.front(); |
| 222 | BasicBlock *UnwindBB = RI->getParent(); |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 223 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 224 | |
| 225 | // Call the _Unwind_Resume function. |
| 226 | CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB); |
| 227 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 228 | |
| 229 | // We never expect _Unwind_Resume to return. |
| 230 | new UnreachableInst(Ctx, UnwindBB); |
| 231 | return true; |
| 232 | } |
| 233 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 234 | BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 235 | PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft, |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 236 | "exn.obj", UnwindBB); |
| 237 | |
| 238 | // Extract the exception object from the ResumeInst and add it to the PHI node |
| 239 | // that feeds the _Unwind_Resume call. |
Benjamin Kramer | 85ffc54 | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 240 | for (ResumeInst *RI : Resumes) { |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 241 | BasicBlock *Parent = RI->getParent(); |
| 242 | BranchInst::Create(UnwindBB, Parent); |
Bill Wendling | b618ea5 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 243 | |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 244 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 245 | PN->addIncoming(ExnObj, Parent); |
Bill Wendling | b618ea5 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 246 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 247 | ++NumResumesLowered; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Call the function. |
| 251 | CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); |
| 252 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 253 | |
| 254 | // We never expect _Unwind_Resume to return. |
| 255 | new UnreachableInst(Ctx, UnwindBB); |
| 256 | return true; |
| 257 | } |
| 258 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 259 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 260 | const TargetMachine &TM = |
| 261 | getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 262 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Francis Visoiu Mistrih | ae1c853 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 263 | TLI = TM.getSubtargetImpl(Fn)->getTargetLowering(); |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 264 | bool Changed = InsertUnwindResumeCalls(Fn); |
Reid Kleckner | 4c27f8d | 2015-03-09 22:45:16 +0000 | [diff] [blame] | 265 | DT = nullptr; |
| 266 | TLI = nullptr; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 267 | return Changed; |
| 268 | } |