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" |
| 16 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
| 18 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetLowering.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetSubtargetInfo.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "dwarfehprepare" |
| 26 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 27 | STATISTIC(NumResumesLowered, "Number of resume calls lowered"); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 30 | class DwarfEHPrepare : public FunctionPass { |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 31 | const TargetMachine *TM; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 32 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 33 | // RewindFunction - _Unwind_Resume or the target equivalent. |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 34 | Constant *RewindFunction; |
| 35 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 36 | bool InsertUnwindResumeCalls(Function &Fn); |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 37 | Value *GetExceptionObject(ResumeInst *RI); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 38 | |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 39 | public: |
| 40 | static char ID; // Pass identification, replacement for typeid. |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 41 | |
| 42 | // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in |
| 43 | // practice. |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame^] | 44 | DwarfEHPrepare() : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr) {} |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 45 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 46 | DwarfEHPrepare(const TargetMachine *TM) |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame^] | 47 | : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {} |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 48 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &Fn) override; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 50 | |
Yaron Keren | 66b0ceb | 2014-09-14 20:36:28 +0000 | [diff] [blame] | 51 | bool doFinalization(Module &M) override { |
| 52 | RewindFunction = nullptr; |
| 53 | return false; |
| 54 | } |
| 55 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 56 | const char *getPassName() const override { |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 57 | return "Exception handling preparation"; |
| 58 | } |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 59 | }; |
| 60 | } // end anonymous namespace |
| 61 | |
| 62 | char DwarfEHPrepare::ID = 0; |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame^] | 63 | INITIALIZE_TM_PASS(DwarfEHPrepare, "dwarfehprepare", "Prepare DWARF exceptions", |
| 64 | false, false) |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 65 | |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 66 | FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) { |
| 67 | return new DwarfEHPrepare(TM); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 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. |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 73 | Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 74 | Value *V = RI->getOperand(0); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 75 | Value *ExnObj = nullptr; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 76 | InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 77 | LoadInst *SelLoad = nullptr; |
| 78 | InsertValueInst *ExcIVI = nullptr; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 79 | bool EraseIVIs = false; |
| 80 | |
| 81 | if (SelIVI) { |
| 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) { |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 86 | ExnObj = ExcIVI->getOperand(1); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 87 | SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); |
| 88 | EraseIVIs = true; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (!ExnObj) |
| 94 | ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); |
| 95 | |
| 96 | RI->eraseFromParent(); |
| 97 | |
| 98 | if (EraseIVIs) { |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 99 | if (SelIVI->use_empty()) |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 100 | SelIVI->eraseFromParent(); |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 101 | if (ExcIVI->use_empty()) |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 102 | ExcIVI->eraseFromParent(); |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 103 | if (SelLoad && SelLoad->use_empty()) |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 104 | SelLoad->eraseFromParent(); |
| 105 | } |
| 106 | |
| 107 | return ExnObj; |
| 108 | } |
| 109 | |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 110 | /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present |
| 111 | /// into calls to the appropriate _Unwind_Resume function. |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 112 | bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 113 | SmallVector<ResumeInst*, 16> Resumes; |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 114 | for (BasicBlock &BB : Fn) { |
| 115 | if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) |
Bill Wendling | 8ac2041 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 116 | Resumes.push_back(RI); |
Bill Wendling | 8ac2041 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 117 | } |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 118 | |
| 119 | if (Resumes.empty()) |
Kai Nacke | e1823b6 | 2013-05-31 16:30:36 +0000 | [diff] [blame] | 120 | return false; |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 121 | |
Reid Kleckner | 0b647e6 | 2015-02-20 01:00:19 +0000 | [diff] [blame] | 122 | // Find the rewind function if we didn't already. |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame^] | 123 | const TargetLowering *TLI = TM->getSubtargetImpl(Fn)->getTargetLowering(); |
| 124 | LLVMContext &Ctx = Fn.getContext(); |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 125 | if (!RewindFunction) { |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 126 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 127 | Type::getInt8PtrTy(Ctx), false); |
| 128 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 129 | RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Create the basic block where the _Unwind_Resume call will live. |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame^] | 133 | unsigned ResumesSize = Resumes.size(); |
| 134 | |
| 135 | if (ResumesSize == 1) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 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(); |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 140 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 141 | |
| 142 | // Call the _Unwind_Resume function. |
| 143 | CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB); |
| 144 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 145 | |
| 146 | // We never expect _Unwind_Resume to return. |
| 147 | new UnreachableInst(Ctx, UnwindBB); |
| 148 | return true; |
| 149 | } |
| 150 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 151 | BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); |
Chandler Carruth | 301ed0c | 2015-02-20 02:15:36 +0000 | [diff] [blame^] | 152 | PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize, |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 153 | "exn.obj", UnwindBB); |
| 154 | |
| 155 | // Extract the exception object from the ResumeInst and add it to the PHI node |
| 156 | // that feeds the _Unwind_Resume call. |
Benjamin Kramer | eb63e4d | 2015-01-29 13:26:50 +0000 | [diff] [blame] | 157 | for (ResumeInst *RI : Resumes) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 158 | BasicBlock *Parent = RI->getParent(); |
| 159 | BranchInst::Create(UnwindBB, Parent); |
Bill Wendling | 9249261 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 160 | |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 161 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 162 | PN->addIncoming(ExnObj, Parent); |
Bill Wendling | 9249261 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 163 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 164 | ++NumResumesLowered; |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Call the function. |
| 168 | CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); |
| 169 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 170 | |
| 171 | // We never expect _Unwind_Resume to return. |
| 172 | new UnreachableInst(Ctx, UnwindBB); |
| 173 | return true; |
| 174 | } |
| 175 | |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 176 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
Reid Kleckner | 7bb0738 | 2015-02-18 23:17:41 +0000 | [diff] [blame] | 177 | assert(TM && "DWARF EH preparation requires a target machine"); |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 178 | bool Changed = InsertUnwindResumeCalls(Fn); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 179 | return Changed; |
| 180 | } |