Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 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 |
Bill Wendling | d1aa77c | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 11 | // generation. Required if using dwarf exception handling. |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 16 | #include "llvm/ADT/BitVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 18 | #include "llvm/Analysis/CFG.h" |
| 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 20 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetLowering.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 27 | #include "llvm/Transforms/Utils/Local.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "dwarfehprepare" |
| 31 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 32 | STATISTIC(NumResumesLowered, "Number of resume calls lowered"); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 35 | class DwarfEHPrepare : public FunctionPass { |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 36 | const TargetMachine *TM; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 37 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 38 | // RewindFunction - _Unwind_Resume or the target equivalent. |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 39 | Constant *RewindFunction; |
| 40 | |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 41 | DominatorTree *DT; |
| 42 | const TargetLowering *TLI; |
| 43 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 44 | bool InsertUnwindResumeCalls(Function &Fn); |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 45 | Value *GetExceptionObject(ResumeInst *RI); |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 46 | size_t |
| 47 | pruneUnreachableResumes(Function &Fn, |
| 48 | SmallVectorImpl<ResumeInst *> &Resumes, |
| 49 | SmallVectorImpl<LandingPadInst *> &CleanupLPads); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 50 | |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 51 | public: |
| 52 | static char ID; // Pass identification, replacement for typeid. |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 53 | |
| 54 | // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in |
| 55 | // practice. |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 56 | DwarfEHPrepare() |
| 57 | : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr), DT(nullptr), |
| 58 | TLI(nullptr) {} |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 59 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 60 | DwarfEHPrepare(const TargetMachine *TM) |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 61 | : FunctionPass(ID), TM(TM), RewindFunction(nullptr), DT(nullptr), |
| 62 | TLI(nullptr) {} |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 63 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 64 | bool runOnFunction(Function &Fn) override; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 65 | |
Yaron Keren | 66b0ceb | 2014-09-14 20:36:28 +0000 | [diff] [blame] | 66 | bool doFinalization(Module &M) override { |
| 67 | RewindFunction = nullptr; |
| 68 | return false; |
| 69 | } |
| 70 | |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 71 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 72 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 73 | const char *getPassName() const override { |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 74 | return "Exception handling preparation"; |
| 75 | } |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 76 | }; |
| 77 | } // end anonymous namespace |
| 78 | |
| 79 | char DwarfEHPrepare::ID = 0; |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 80 | INITIALIZE_TM_PASS_BEGIN(DwarfEHPrepare, "dwarfehprepare", |
| 81 | "Prepare DWARF exceptions", false, false) |
| 82 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 83 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| 84 | INITIALIZE_TM_PASS_END(DwarfEHPrepare, "dwarfehprepare", |
| 85 | "Prepare DWARF exceptions", false, false) |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 86 | |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 87 | FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) { |
| 88 | return new DwarfEHPrepare(TM); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 91 | void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { |
| 92 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 93 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 94 | } |
| 95 | |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 96 | /// GetExceptionObject - Return the exception object from the value passed into |
| 97 | /// the 'resume' instruction (typically an aggregate). Clean up any dead |
| 98 | /// instructions, including the 'resume' instruction. |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 99 | Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 100 | Value *V = RI->getOperand(0); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 101 | Value *ExnObj = nullptr; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 102 | InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 103 | LoadInst *SelLoad = nullptr; |
| 104 | InsertValueInst *ExcIVI = nullptr; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 105 | bool EraseIVIs = false; |
| 106 | |
| 107 | if (SelIVI) { |
| 108 | if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { |
| 109 | ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); |
| 110 | if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && |
| 111 | ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 112 | ExnObj = ExcIVI->getOperand(1); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 113 | SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); |
| 114 | EraseIVIs = true; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (!ExnObj) |
| 120 | ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); |
| 121 | |
| 122 | RI->eraseFromParent(); |
| 123 | |
| 124 | if (EraseIVIs) { |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 125 | if (SelIVI->use_empty()) |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 126 | SelIVI->eraseFromParent(); |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 127 | if (ExcIVI->use_empty()) |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 128 | ExcIVI->eraseFromParent(); |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 129 | if (SelLoad && SelLoad->use_empty()) |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 130 | SelLoad->eraseFromParent(); |
| 131 | } |
| 132 | |
| 133 | return ExnObj; |
| 134 | } |
| 135 | |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 136 | /// Replace resumes that are not reachable from a cleanup landing pad with |
| 137 | /// unreachable and then simplify those blocks. |
| 138 | size_t DwarfEHPrepare::pruneUnreachableResumes( |
| 139 | Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes, |
| 140 | SmallVectorImpl<LandingPadInst *> &CleanupLPads) { |
| 141 | BitVector ResumeReachable(Resumes.size()); |
| 142 | size_t ResumeIndex = 0; |
| 143 | for (auto *RI : Resumes) { |
| 144 | for (auto *LP : CleanupLPads) { |
| 145 | if (isPotentiallyReachable(LP, RI, DT)) { |
| 146 | ResumeReachable.set(ResumeIndex); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | ++ResumeIndex; |
| 151 | } |
| 152 | |
| 153 | // If everything is reachable, there is no change. |
| 154 | if (ResumeReachable.all()) |
| 155 | return Resumes.size(); |
| 156 | |
| 157 | const TargetTransformInfo &TTI = |
| 158 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn); |
| 159 | LLVMContext &Ctx = Fn.getContext(); |
| 160 | |
| 161 | // Otherwise, insert unreachable instructions and call simplifycfg. |
| 162 | size_t ResumesLeft = 0; |
| 163 | for (size_t I = 0, E = Resumes.size(); I < E; ++I) { |
| 164 | ResumeInst *RI = Resumes[I]; |
| 165 | if (ResumeReachable[I]) { |
| 166 | Resumes[ResumesLeft++] = RI; |
| 167 | } else { |
| 168 | BasicBlock *BB = RI->getParent(); |
| 169 | new UnreachableInst(Ctx, RI); |
| 170 | RI->eraseFromParent(); |
| 171 | SimplifyCFG(BB, TTI, 1, TLI->getDataLayout()); |
| 172 | } |
| 173 | } |
| 174 | Resumes.resize(ResumesLeft); |
| 175 | return ResumesLeft; |
| 176 | } |
| 177 | |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 178 | /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present |
| 179 | /// into calls to the appropriate _Unwind_Resume function. |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 180 | bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 181 | SmallVector<ResumeInst*, 16> Resumes; |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 182 | SmallVector<LandingPadInst*, 16> CleanupLPads; |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 183 | for (BasicBlock &BB : Fn) { |
| 184 | if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) |
Bill Wendling | 8ac2041 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 185 | Resumes.push_back(RI); |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 186 | if (auto *LP = BB.getLandingPadInst()) |
| 187 | if (LP->isCleanup()) |
| 188 | CleanupLPads.push_back(LP); |
Bill Wendling | 8ac2041 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 189 | } |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 190 | |
| 191 | if (Resumes.empty()) |
Kai Nacke | e1823b6 | 2013-05-31 16:30:36 +0000 | [diff] [blame] | 192 | return false; |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 193 | |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame] | 194 | LLVMContext &Ctx = Fn.getContext(); |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 195 | |
| 196 | size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads); |
| 197 | if (ResumesLeft == 0) |
| 198 | return true; // We pruned them all. |
| 199 | |
| 200 | // Find the rewind function if we didn't already. |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 201 | if (!RewindFunction) { |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 202 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 203 | Type::getInt8PtrTy(Ctx), false); |
| 204 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 205 | RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Create the basic block where the _Unwind_Resume call will live. |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 209 | if (ResumesLeft == 1) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 210 | // Instead of creating a new BB and PHI node, just append the call to |
| 211 | // _Unwind_Resume to the end of the single resume block. |
| 212 | ResumeInst *RI = Resumes.front(); |
| 213 | BasicBlock *UnwindBB = RI->getParent(); |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 214 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 215 | |
| 216 | // Call the _Unwind_Resume function. |
| 217 | CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB); |
| 218 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 219 | |
| 220 | // We never expect _Unwind_Resume to return. |
| 221 | new UnreachableInst(Ctx, UnwindBB); |
| 222 | return true; |
| 223 | } |
| 224 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 225 | BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 226 | PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft, |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 227 | "exn.obj", UnwindBB); |
| 228 | |
| 229 | // Extract the exception object from the ResumeInst and add it to the PHI node |
| 230 | // that feeds the _Unwind_Resume call. |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 231 | for (ResumeInst *RI : Resumes) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 232 | BasicBlock *Parent = RI->getParent(); |
| 233 | BranchInst::Create(UnwindBB, Parent); |
Bill Wendling | 9249261 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 234 | |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 235 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 236 | PN->addIncoming(ExnObj, Parent); |
Bill Wendling | 9249261 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 237 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 238 | ++NumResumesLowered; |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Call the function. |
| 242 | CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); |
| 243 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 244 | |
| 245 | // We never expect _Unwind_Resume to return. |
| 246 | new UnreachableInst(Ctx, UnwindBB); |
| 247 | return true; |
| 248 | } |
| 249 | |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 250 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 251 | assert(TM && "DWARF EH preparation requires a target machine"); |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 252 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 253 | TLI = TM->getSubtargetImpl(Fn)->getTargetLowering(); |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 254 | bool Changed = InsertUnwindResumeCalls(Fn); |
Reid Kleckner | be0a050 | 2015-03-09 22:45:16 +0000 | [diff] [blame^] | 255 | DT = nullptr; |
| 256 | TLI = nullptr; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 257 | return Changed; |
| 258 | } |