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/Function.h" 18 #include "llvm/IR/Instructions.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/Pass.h" 21 #include "llvm/Target/TargetLowering.h" 22 #include "llvm/Target/TargetSubtargetInfo.h" 23 using namespace llvm; 24 25 #define DEBUG_TYPE "dwarfehprepare" 26 27 STATISTIC(NumResumesLowered, "Number of resume calls lowered"); 28 29 namespace { 30 class DwarfEHPrepare : public FunctionPass { 31 const TargetMachine *TM; 32 33 // RewindFunction - _Unwind_Resume or the target equivalent. 34 Constant *RewindFunction; 35 36 bool InsertUnwindResumeCalls(Function &Fn); 37 Value *GetExceptionObject(ResumeInst *RI); 38 39 public: 40 static char ID; // Pass identification, replacement for typeid. 41 42 // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in 43 // practice. 44 DwarfEHPrepare() : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr) {} 45 46 DwarfEHPrepare(const TargetMachine *TM) 47 : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {} 48 49 bool runOnFunction(Function &Fn) override; 50 51 bool doFinalization(Module &M) override { 52 RewindFunction = nullptr; 53 return false; 54 } 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 INITIALIZE_TM_PASS(DwarfEHPrepare, "dwarfehprepare", "Prepare DWARF exceptions", 64 false, false) 65 66 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) { 67 return new DwarfEHPrepare(TM); 68 } 69 70 /// GetExceptionObject - Return the exception object from the value passed into 71 /// the 'resume' instruction (typically an aggregate). Clean up any dead 72 /// instructions, including the 'resume' instruction. 73 Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) { 74 Value *V = RI->getOperand(0); 75 Value *ExnObj = nullptr; 76 InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V); 77 LoadInst *SelLoad = nullptr; 78 InsertValueInst *ExcIVI = nullptr; 79 bool EraseIVIs = false; 80 81 if (SelIVI) { 82 if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) { 83 ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0)); 84 if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) && 85 ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) { 86 ExnObj = ExcIVI->getOperand(1); 87 SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1)); 88 EraseIVIs = true; 89 } 90 } 91 } 92 93 if (!ExnObj) 94 ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI); 95 96 RI->eraseFromParent(); 97 98 if (EraseIVIs) { 99 if (SelIVI->use_empty()) 100 SelIVI->eraseFromParent(); 101 if (ExcIVI->use_empty()) 102 ExcIVI->eraseFromParent(); 103 if (SelLoad && SelLoad->use_empty()) 104 SelLoad->eraseFromParent(); 105 } 106 107 return ExnObj; 108 } 109 110 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present 111 /// into calls to the appropriate _Unwind_Resume function. 112 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) { 113 SmallVector<ResumeInst*, 16> Resumes; 114 for (BasicBlock &BB : Fn) { 115 if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator())) 116 Resumes.push_back(RI); 117 } 118 119 if (Resumes.empty()) 120 return false; 121 122 // Find the rewind function if we didn't already. 123 const TargetLowering *TLI = TM->getSubtargetImpl(Fn)->getTargetLowering(); 124 LLVMContext &Ctx = Fn.getContext(); 125 if (!RewindFunction) { 126 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), 127 Type::getInt8PtrTy(Ctx), false); 128 const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); 129 RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy); 130 } 131 132 // Create the basic block where the _Unwind_Resume call will live. 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 (ResumeInst *RI : Resumes) { 158 BasicBlock *Parent = RI->getParent(); 159 BranchInst::Create(UnwindBB, Parent); 160 161 Value *ExnObj = GetExceptionObject(RI); 162 PN->addIncoming(ExnObj, Parent); 163 164 ++NumResumesLowered; 165 } 166 167 // Call the function. 168 CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB); 169 CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); 170 171 // We never expect _Unwind_Resume to return. 172 new UnreachableInst(Ctx, UnwindBB); 173 return true; 174 } 175 176 bool DwarfEHPrepare::runOnFunction(Function &Fn) { 177 assert(TM && "DWARF EH preparation requires a target machine"); 178 bool Changed = InsertUnwindResumeCalls(Fn); 179 return Changed; 180 } 181