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 | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.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" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Duncan Sands | bc42c90 | 2010-09-03 08:31:48 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "dwarfehprepare" |
| 32 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 33 | STATISTIC(NumResumesLowered, "Number of resume calls lowered"); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 34 | |
| 35 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 36 | class DwarfEHPrepare : public FunctionPass { |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 37 | const TargetMachine *TM; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 38 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 39 | // RewindFunction - _Unwind_Resume or the target equivalent. |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 40 | Constant *RewindFunction; |
| 41 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 42 | bool InsertUnwindResumeCalls(Function &Fn); |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 43 | Value *GetExceptionObject(ResumeInst *RI); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 44 | |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 45 | public: |
| 46 | static char ID; // Pass identification, replacement for typeid. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 47 | DwarfEHPrepare(const TargetMachine *TM) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 48 | : FunctionPass(ID), TM(TM), RewindFunction(nullptr) { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 49 | initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 50 | } |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 51 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 52 | bool runOnFunction(Function &Fn) override; |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 53 | |
Yaron Keren | 66b0ceb | 2014-09-14 20:36:28 +0000 | [diff] [blame^] | 54 | bool doFinalization(Module &M) override { |
| 55 | RewindFunction = nullptr; |
| 56 | return false; |
| 57 | } |
| 58 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 59 | void getAnalysisUsage(AnalysisUsage &AU) const override { } |
Bill Wendling | 3505c94 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 60 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 61 | const char *getPassName() const override { |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 62 | return "Exception handling preparation"; |
| 63 | } |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 64 | }; |
| 65 | } // end anonymous namespace |
| 66 | |
| 67 | char DwarfEHPrepare::ID = 0; |
| 68 | |
Bill Wendling | afc1036 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 69 | FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) { |
| 70 | return new DwarfEHPrepare(TM); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 73 | /// GetExceptionObject - Return the exception object from the value passed into |
| 74 | /// the 'resume' instruction (typically an aggregate). Clean up any dead |
| 75 | /// instructions, including the 'resume' instruction. |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 76 | Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 77 | Value *V = RI->getOperand(0); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 78 | Value *ExnObj = nullptr; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 79 | InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 80 | LoadInst *SelLoad = nullptr; |
| 81 | InsertValueInst *ExcIVI = nullptr; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 82 | bool EraseIVIs = false; |
| 83 | |
| 84 | if (SelIVI) { |
| 85 | if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { |
| 86 | ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); |
| 87 | if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && |
| 88 | ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 89 | ExnObj = ExcIVI->getOperand(1); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 90 | SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); |
| 91 | EraseIVIs = true; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (!ExnObj) |
| 97 | ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); |
| 98 | |
| 99 | RI->eraseFromParent(); |
| 100 | |
| 101 | if (EraseIVIs) { |
| 102 | if (SelIVI->getNumUses() == 0) |
| 103 | SelIVI->eraseFromParent(); |
| 104 | if (ExcIVI->getNumUses() == 0) |
| 105 | ExcIVI->eraseFromParent(); |
| 106 | if (SelLoad && SelLoad->getNumUses() == 0) |
| 107 | SelLoad->eraseFromParent(); |
| 108 | } |
| 109 | |
| 110 | return ExnObj; |
| 111 | } |
| 112 | |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 113 | /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present |
| 114 | /// into calls to the appropriate _Unwind_Resume function. |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 115 | bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 116 | SmallVector<ResumeInst*, 16> Resumes; |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 117 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
Bill Wendling | 8ac2041 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 118 | TerminatorInst *TI = I->getTerminator(); |
| 119 | if (ResumeInst *RI = dyn_cast<ResumeInst>(TI)) |
| 120 | Resumes.push_back(RI); |
Bill Wendling | 8ac2041 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 121 | } |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 122 | |
| 123 | if (Resumes.empty()) |
Kai Nacke | e1823b6 | 2013-05-31 16:30:36 +0000 | [diff] [blame] | 124 | return false; |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 125 | |
| 126 | // Find the rewind function if we didn't already. |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 127 | const TargetLowering *TLI = TM->getSubtargetImpl()->getTargetLowering(); |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 128 | if (!RewindFunction) { |
| 129 | LLVMContext &Ctx = Resumes[0]->getContext(); |
| 130 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 131 | Type::getInt8PtrTy(Ctx), false); |
| 132 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 133 | RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Create the basic block where the _Unwind_Resume call will live. |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 137 | LLVMContext &Ctx = Fn.getContext(); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 138 | unsigned ResumesSize = Resumes.size(); |
| 139 | |
| 140 | if (ResumesSize == 1) { |
| 141 | // Instead of creating a new BB and PHI node, just append the call to |
| 142 | // _Unwind_Resume to the end of the single resume block. |
| 143 | ResumeInst *RI = Resumes.front(); |
| 144 | BasicBlock *UnwindBB = RI->getParent(); |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 145 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 146 | |
| 147 | // Call the _Unwind_Resume function. |
| 148 | CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB); |
| 149 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 150 | |
| 151 | // We never expect _Unwind_Resume to return. |
| 152 | new UnreachableInst(Ctx, UnwindBB); |
| 153 | return true; |
| 154 | } |
| 155 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 156 | BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 157 | PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize, |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 158 | "exn.obj", UnwindBB); |
| 159 | |
| 160 | // Extract the exception object from the ResumeInst and add it to the PHI node |
| 161 | // that feeds the _Unwind_Resume call. |
| 162 | for (SmallVectorImpl<ResumeInst*>::iterator |
| 163 | I = Resumes.begin(), E = Resumes.end(); I != E; ++I) { |
| 164 | ResumeInst *RI = *I; |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 165 | BasicBlock *Parent = RI->getParent(); |
| 166 | BranchInst::Create(UnwindBB, Parent); |
Bill Wendling | 9249261 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 167 | |
Bill Wendling | 27489fe | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 168 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 8c09040 | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 169 | PN->addIncoming(ExnObj, Parent); |
Bill Wendling | 9249261 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 170 | |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 171 | ++NumResumesLowered; |
Bill Wendling | 1cdd7fd | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Call the function. |
| 175 | CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); |
| 176 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 177 | |
| 178 | // We never expect _Unwind_Resume to return. |
| 179 | new UnreachableInst(Ctx, UnwindBB); |
| 180 | return true; |
| 181 | } |
| 182 | |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 183 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
Bill Wendling | 478f58c | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 184 | bool Changed = InsertUnwindResumeCalls(Fn); |
Duncan Sands | d6fb650 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 185 | return Changed; |
| 186 | } |