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" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/Instructions.h" |
| 18 | #include "llvm/IntrinsicInst.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Pass.h" |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/Analysis/Dominators.h" |
| 23 | #include "llvm/CodeGen/Passes.h" |
Jim Grosbach | 8d77cc8 | 2010-01-20 23:03:55 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmInfo.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 { |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 35 | const TargetMachine *TM; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 36 | const TargetLowering *TLI; |
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 | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 42 | Instruction *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. |
Duncan Sands | 22efc18 | 2010-08-31 09:05:06 +0000 | [diff] [blame] | 46 | DwarfEHPrepare(const TargetMachine *tm) : |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 47 | FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()), |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 48 | RewindFunction(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 49 | initializeDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 50 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 51 | |
| 52 | virtual bool runOnFunction(Function &Fn); |
| 53 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 54 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { } |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 55 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 56 | const char *getPassName() const { |
| 57 | return "Exception handling preparation"; |
| 58 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 59 | }; |
| 60 | } // end anonymous namespace |
| 61 | |
| 62 | char DwarfEHPrepare::ID = 0; |
| 63 | |
Duncan Sands | 22efc18 | 2010-08-31 09:05:06 +0000 | [diff] [blame] | 64 | FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm) { |
| 65 | return new DwarfEHPrepare(tm); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 68 | /// GetExceptionObject - Return the exception object from the value passed into |
| 69 | /// the 'resume' instruction (typically an aggregate). Clean up any dead |
| 70 | /// instructions, including the 'resume' instruction. |
| 71 | Instruction *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { |
| 72 | Value *V = RI->getOperand(0); |
| 73 | Instruction *ExnObj = 0; |
| 74 | InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); |
| 75 | LoadInst *SelLoad = 0; |
| 76 | InsertValueInst *ExcIVI = 0; |
| 77 | bool EraseIVIs = false; |
| 78 | |
| 79 | if (SelIVI) { |
| 80 | if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { |
| 81 | ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); |
| 82 | if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && |
| 83 | ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { |
| 84 | ExnObj = cast<Instruction>(ExcIVI->getOperand(1)); |
| 85 | SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); |
| 86 | EraseIVIs = true; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (!ExnObj) |
| 92 | ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); |
| 93 | |
| 94 | RI->eraseFromParent(); |
| 95 | |
| 96 | if (EraseIVIs) { |
| 97 | if (SelIVI->getNumUses() == 0) |
| 98 | SelIVI->eraseFromParent(); |
| 99 | if (ExcIVI->getNumUses() == 0) |
| 100 | ExcIVI->eraseFromParent(); |
| 101 | if (SelLoad && SelLoad->getNumUses() == 0) |
| 102 | SelLoad->eraseFromParent(); |
| 103 | } |
| 104 | |
| 105 | return ExnObj; |
| 106 | } |
| 107 | |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 108 | /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present |
| 109 | /// into calls to the appropriate _Unwind_Resume function. |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 110 | bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 111 | bool UsesNewEH = false; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 112 | SmallVector<ResumeInst*, 16> Resumes; |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 113 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 114 | TerminatorInst *TI = I->getTerminator(); |
| 115 | if (ResumeInst *RI = dyn_cast<ResumeInst>(TI)) |
| 116 | Resumes.push_back(RI); |
| 117 | else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) |
| 118 | UsesNewEH = II->getUnwindDest()->isLandingPad(); |
| 119 | } |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 120 | |
| 121 | if (Resumes.empty()) |
Bill Wendling | 09908c4 | 2011-08-25 23:48:11 +0000 | [diff] [blame] | 122 | return UsesNewEH; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 123 | |
| 124 | // Find the rewind function if we didn't already. |
| 125 | if (!RewindFunction) { |
| 126 | LLVMContext &Ctx = Resumes[0]->getContext(); |
| 127 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 128 | Type::getInt8PtrTy(Ctx), false); |
| 129 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 130 | RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Create the basic block where the _Unwind_Resume call will live. |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 134 | LLVMContext &Ctx = Fn.getContext(); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 135 | unsigned ResumesSize = Resumes.size(); |
| 136 | |
| 137 | if (ResumesSize == 1) { |
| 138 | // Instead of creating a new BB and PHI node, just append the call to |
| 139 | // _Unwind_Resume to the end of the single resume block. |
| 140 | ResumeInst *RI = Resumes.front(); |
| 141 | BasicBlock *UnwindBB = RI->getParent(); |
| 142 | Instruction *ExnObj = GetExceptionObject(RI); |
| 143 | |
| 144 | // Call the _Unwind_Resume function. |
| 145 | CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB); |
| 146 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 147 | |
| 148 | // We never expect _Unwind_Resume to return. |
| 149 | new UnreachableInst(Ctx, UnwindBB); |
| 150 | return true; |
| 151 | } |
| 152 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 153 | BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 154 | PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize, |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 155 | "exn.obj", UnwindBB); |
| 156 | |
| 157 | // Extract the exception object from the ResumeInst and add it to the PHI node |
| 158 | // that feeds the _Unwind_Resume call. |
| 159 | for (SmallVectorImpl<ResumeInst*>::iterator |
| 160 | I = Resumes.begin(), E = Resumes.end(); I != E; ++I) { |
| 161 | ResumeInst *RI = *I; |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 162 | BasicBlock *Parent = RI->getParent(); |
| 163 | BranchInst::Create(UnwindBB, Parent); |
Bill Wendling | b618ea5 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 164 | |
Bill Wendling | 0ae06de | 2012-01-28 01:17:56 +0000 | [diff] [blame] | 165 | Instruction *ExnObj = GetExceptionObject(RI); |
| 166 | PN->addIncoming(ExnObj, Parent); |
Bill Wendling | b618ea5 | 2012-01-20 00:53:28 +0000 | [diff] [blame] | 167 | |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 168 | ++NumResumesLowered; |
Bill Wendling | 35adbb3 | 2011-08-17 19:48:49 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Call the function. |
| 172 | CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); |
| 173 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
| 174 | |
| 175 | // We never expect _Unwind_Resume to return. |
| 176 | new UnreachableInst(Ctx, UnwindBB); |
| 177 | return true; |
| 178 | } |
| 179 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 180 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
Bill Wendling | e13eba2 | 2011-11-07 23:36:48 +0000 | [diff] [blame] | 181 | bool Changed = InsertUnwindResumeCalls(Fn); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 182 | return Changed; |
| 183 | } |