Duncan Sands | b0f1e17 | 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 | 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 | |
| 15 | #define DEBUG_TYPE "dwarfehprepare" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/Analysis/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 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 | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Gabor Greif | 2bf4b3b | 2010-06-25 11:25:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CallSite.h" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetLowering.h" |
| 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Duncan Sands | 850fcd4 | 2010-09-03 08:31:48 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 31 | STATISTIC(NumResumesLowered, "Number of resume calls lowered"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 32 | |
| 33 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 34 | class DwarfEHPrepare : public FunctionPass { |
Benjamin Kramer | 69e42db | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 35 | const TargetLoweringBase *TLI; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 36 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 37 | // RewindFunction - _Unwind_Resume or the target equivalent. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 38 | Constant *RewindFunction; |
| 39 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 40 | bool InsertUnwindResumeCalls(Function &Fn); |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 41 | Value *GetExceptionObject(ResumeInst *RI); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 42 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 43 | public: |
| 44 | static char ID; // Pass identification, replacement for typeid. |
Bill Wendling | 3b1040c | 2013-05-20 21:54:18 +0000 | [diff] [blame] | 45 | DwarfEHPrepare(const TargetLoweringBase *TLI) : |
| 46 | FunctionPass(ID), TLI(TLI), RewindFunction(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 47 | initializeDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 48 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 49 | |
| 50 | virtual bool runOnFunction(Function &Fn); |
| 51 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 52 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { } |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 53 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 54 | const char *getPassName() const { |
| 55 | return "Exception handling preparation"; |
| 56 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 57 | }; |
| 58 | } // end anonymous namespace |
| 59 | |
| 60 | char DwarfEHPrepare::ID = 0; |
| 61 | |
Bill Wendling | 3b1040c | 2013-05-20 21:54:18 +0000 | [diff] [blame] | 62 | FunctionPass *llvm::createDwarfEHPass(const TargetLoweringBase *TLI) { |
| 63 | return new DwarfEHPrepare(TLI); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 66 | /// GetExceptionObject - Return the exception object from the value passed into |
| 67 | /// the 'resume' instruction (typically an aggregate). Clean up any dead |
| 68 | /// instructions, including the 'resume' instruction. |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 69 | Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 70 | Value *V = RI->getOperand(0); |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 71 | Value *ExnObj = 0; |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 72 | InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); |
| 73 | LoadInst *SelLoad = 0; |
| 74 | InsertValueInst *ExcIVI = 0; |
| 75 | bool EraseIVIs = false; |
| 76 | |
| 77 | if (SelIVI) { |
| 78 | if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { |
| 79 | ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); |
| 80 | if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && |
| 81 | ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 82 | ExnObj = ExcIVI->getOperand(1); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 83 | SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); |
| 84 | EraseIVIs = true; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (!ExnObj) |
| 90 | ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); |
| 91 | |
| 92 | RI->eraseFromParent(); |
| 93 | |
| 94 | if (EraseIVIs) { |
| 95 | if (SelIVI->getNumUses() == 0) |
| 96 | SelIVI->eraseFromParent(); |
| 97 | if (ExcIVI->getNumUses() == 0) |
| 98 | ExcIVI->eraseFromParent(); |
| 99 | if (SelLoad && SelLoad->getNumUses() == 0) |
| 100 | SelLoad->eraseFromParent(); |
| 101 | } |
| 102 | |
| 103 | return ExnObj; |
| 104 | } |
| 105 | |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 106 | /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present |
| 107 | /// into calls to the appropriate _Unwind_Resume function. |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 108 | bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 109 | bool UsesNewEH = false; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 110 | SmallVector<ResumeInst*, 16> Resumes; |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 111 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 112 | TerminatorInst *TI = I->getTerminator(); |
| 113 | if (ResumeInst *RI = dyn_cast<ResumeInst>(TI)) |
| 114 | Resumes.push_back(RI); |
| 115 | else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) |
| 116 | UsesNewEH = II->getUnwindDest()->isLandingPad(); |
| 117 | } |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 118 | |
| 119 | if (Resumes.empty()) |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 120 | return UsesNewEH; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 121 | |
| 122 | // Find the rewind function if we didn't already. |
| 123 | if (!RewindFunction) { |
| 124 | LLVMContext &Ctx = Resumes[0]->getContext(); |
| 125 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 126 | Type::getInt8PtrTy(Ctx), false); |
| 127 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 128 | RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Create the basic block where the _Unwind_Resume call will live. |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 132 | LLVMContext &Ctx = Fn.getContext(); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 133 | unsigned ResumesSize = Resumes.size(); |
| 134 | |
| 135 | if (ResumesSize == 1) { |
| 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 | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 140 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 0ae06de | 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 | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 151 | BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 152 | PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize, |
Bill Wendling | 35adbb3 | 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. |
| 157 | for (SmallVectorImpl<ResumeInst*>::iterator |
| 158 | I = Resumes.begin(), E = Resumes.end(); I != E; ++I) { |
| 159 | ResumeInst *RI = *I; |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 160 | BasicBlock *Parent = RI->getParent(); |
| 161 | BranchInst::Create(UnwindBB, Parent); |
Bill Wendling | b618ea5 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 162 | |
Bill Wendling | 29424e8 | 2012-05-17 17:59:51 +0000 | [diff] [blame] | 163 | Value *ExnObj = GetExceptionObject(RI); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 164 | PN->addIncoming(ExnObj, Parent); |
Bill Wendling | b618ea5 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 165 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 166 | ++NumResumesLowered; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Call the function. |
| 170 | CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); |
| 171 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 172 | |
| 173 | // We never expect _Unwind_Resume to return. |
| 174 | new UnreachableInst(Ctx, UnwindBB); |
| 175 | return true; |
| 176 | } |
| 177 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 178 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 179 | bool Changed = InsertUnwindResumeCalls(Fn); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 180 | return Changed; |
| 181 | } |