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 11 // generation. Required if using dwarf exception handling. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/Passes.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/IR/CallSite.h" 18 #include "llvm/IR/Dominators.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/IntrinsicInst.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/MC/MCAsmInfo.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Target/TargetLowering.h" 26 #include "llvm/Target/TargetSubtargetInfo.h" 27 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 28 #include "llvm/Transforms/Utils/SSAUpdater.h" 29 using namespace llvm; 30 31 #define DEBUG_TYPE "dwarfehprepare" 32 33 STATISTIC(NumResumesLowered, "Number of resume calls lowered"); 34 35 namespace { 36 class DwarfEHPrepare : public FunctionPass { 37 const TargetMachine *TM; 38 39 // RewindFunction - _Unwind_Resume or the target equivalent. 40 Constant *RewindFunction; 41 42 bool InsertUnwindResumeCalls(Function &Fn); 43 Value *GetExceptionObject(ResumeInst *RI); 44 45 public: 46 static char ID; // Pass identification, replacement for typeid. 47 DwarfEHPrepare(const TargetMachine *TM) 48 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) { 49 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry()); 50 } 51 52 bool runOnFunction(Function &Fn) override; 53 54 void getAnalysisUsage(AnalysisUsage &AU) const override { } 55 56 const char *getPassName() const override { 57 return "Exception handling preparation"; 58 } 59 }; 60 } // end anonymous namespace 61 62 char DwarfEHPrepare::ID = 0; 63 64 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) { 65 return new DwarfEHPrepare(TM); 66 } 67 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 Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { 72 Value *V = RI->getOperand(0); 73 Value *ExnObj = nullptr; 74 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); 75 LoadInst *SelLoad = nullptr; 76 InsertValueInst *ExcIVI = nullptr; 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 = 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 108 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present 109 /// into calls to the appropriate _Unwind_Resume function. 110 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { 111 SmallVector<ResumeInst*, 16> Resumes; 112 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { 113 TerminatorInst *TI = I->getTerminator(); 114 if (ResumeInst *RI = dyn_cast<ResumeInst>(TI)) 115 Resumes.push_back(RI); 116 } 117 118 if (Resumes.empty()) 119 return false; 120 121 // Find the rewind function if we didn't already. 122 const TargetLowering *TLI = TM->getSubtargetImpl()->getTargetLowering(); 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); 128 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); 129 } 130 131 // Create the basic block where the _Unwind_Resume call will live. 132 LLVMContext &Ctx = Fn.getContext(); 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(); 140 Value *ExnObj = GetExceptionObject(RI); 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 151 BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn); 152 PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize, 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; 160 BasicBlock *Parent = RI->getParent(); 161 BranchInst::Create(UnwindBB, Parent); 162 163 Value *ExnObj = GetExceptionObject(RI); 164 PN->addIncoming(ExnObj, Parent); 165 166 ++NumResumesLowered; 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 178 bool DwarfEHPrepare::runOnFunction(Function &Fn) { 179 bool Changed = InsertUnwindResumeCalls(Fn); 180 return Changed; 181 } 182