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