10b57cec5SDimitry Andric //===--- CGException.cpp - Emit LLVM Code for C++ exceptions ----*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This contains code dealing with C++ exception related code generation. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "CodeGenFunction.h" 140b57cec5SDimitry Andric #include "CGCXXABI.h" 150b57cec5SDimitry Andric #include "CGCleanup.h" 160b57cec5SDimitry Andric #include "CGObjCRuntime.h" 170b57cec5SDimitry Andric #include "ConstantEmitter.h" 180b57cec5SDimitry Andric #include "TargetInfo.h" 190b57cec5SDimitry Andric #include "clang/AST/Mangle.h" 200b57cec5SDimitry Andric #include "clang/AST/StmtCXX.h" 210b57cec5SDimitry Andric #include "clang/AST/StmtObjC.h" 220b57cec5SDimitry Andric #include "clang/AST/StmtVisitor.h" 230b57cec5SDimitry Andric #include "clang/Basic/TargetBuiltins.h" 240b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 250b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 260b57cec5SDimitry Andric #include "llvm/Support/SaveAndRestore.h" 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace clang; 290b57cec5SDimitry Andric using namespace CodeGen; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric static llvm::FunctionCallee getFreeExceptionFn(CodeGenModule &CGM) { 320b57cec5SDimitry Andric // void __cxa_free_exception(void *thrown_exception); 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric llvm::FunctionType *FTy = 350b57cec5SDimitry Andric llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric static llvm::FunctionCallee getUnexpectedFn(CodeGenModule &CGM) { 410b57cec5SDimitry Andric // void __cxa_call_unexpected(void *thrown_exception); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric llvm::FunctionType *FTy = 440b57cec5SDimitry Andric llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); 470b57cec5SDimitry Andric } 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric llvm::FunctionCallee CodeGenModule::getTerminateFn() { 500b57cec5SDimitry Andric // void __terminate(); 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric llvm::FunctionType *FTy = 530b57cec5SDimitry Andric llvm::FunctionType::get(VoidTy, /*isVarArg=*/false); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric StringRef name; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric // In C++, use std::terminate(). 580b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 590b57cec5SDimitry Andric getTarget().getCXXABI().isItaniumFamily()) { 600b57cec5SDimitry Andric name = "_ZSt9terminatev"; 610b57cec5SDimitry Andric } else if (getLangOpts().CPlusPlus && 620b57cec5SDimitry Andric getTarget().getCXXABI().isMicrosoft()) { 630b57cec5SDimitry Andric if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015)) 640b57cec5SDimitry Andric name = "__std_terminate"; 650b57cec5SDimitry Andric else 660b57cec5SDimitry Andric name = "?terminate@@YAXXZ"; 670b57cec5SDimitry Andric } else if (getLangOpts().ObjC && 680b57cec5SDimitry Andric getLangOpts().ObjCRuntime.hasTerminate()) 690b57cec5SDimitry Andric name = "objc_terminate"; 700b57cec5SDimitry Andric else 710b57cec5SDimitry Andric name = "abort"; 720b57cec5SDimitry Andric return CreateRuntimeFunction(FTy, name); 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric static llvm::FunctionCallee getCatchallRethrowFn(CodeGenModule &CGM, 760b57cec5SDimitry Andric StringRef Name) { 770b57cec5SDimitry Andric llvm::FunctionType *FTy = 780b57cec5SDimitry Andric llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric return CGM.CreateRuntimeFunction(FTy, Name); 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr }; 840b57cec5SDimitry Andric const EHPersonality 850b57cec5SDimitry Andric EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr }; 860b57cec5SDimitry Andric const EHPersonality 870b57cec5SDimitry Andric EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr }; 880b57cec5SDimitry Andric const EHPersonality 890b57cec5SDimitry Andric EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr }; 900b57cec5SDimitry Andric const EHPersonality 910b57cec5SDimitry Andric EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr }; 920b57cec5SDimitry Andric const EHPersonality 930b57cec5SDimitry Andric EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr }; 940b57cec5SDimitry Andric const EHPersonality 950b57cec5SDimitry Andric EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr }; 960b57cec5SDimitry Andric const EHPersonality 970b57cec5SDimitry Andric EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"}; 980b57cec5SDimitry Andric const EHPersonality 990b57cec5SDimitry Andric EHPersonality::GNU_ObjC_SJLJ = {"__gnu_objc_personality_sj0", "objc_exception_throw"}; 1000b57cec5SDimitry Andric const EHPersonality 1010b57cec5SDimitry Andric EHPersonality::GNU_ObjC_SEH = {"__gnu_objc_personality_seh0", "objc_exception_throw"}; 1020b57cec5SDimitry Andric const EHPersonality 1030b57cec5SDimitry Andric EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr }; 1040b57cec5SDimitry Andric const EHPersonality 1050b57cec5SDimitry Andric EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr }; 1060b57cec5SDimitry Andric const EHPersonality 1070b57cec5SDimitry Andric EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr }; 1080b57cec5SDimitry Andric const EHPersonality 1090b57cec5SDimitry Andric EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr }; 1100b57cec5SDimitry Andric const EHPersonality 1110b57cec5SDimitry Andric EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr }; 1120b57cec5SDimitry Andric const EHPersonality 1130b57cec5SDimitry Andric EHPersonality::GNU_Wasm_CPlusPlus = { "__gxx_wasm_personality_v0", nullptr }; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric static const EHPersonality &getCPersonality(const TargetInfo &Target, 1160b57cec5SDimitry Andric const LangOptions &L) { 1170b57cec5SDimitry Andric const llvm::Triple &T = Target.getTriple(); 1180b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment()) 1190b57cec5SDimitry Andric return EHPersonality::MSVC_CxxFrameHandler3; 1200b57cec5SDimitry Andric if (L.SjLjExceptions) 1210b57cec5SDimitry Andric return EHPersonality::GNU_C_SJLJ; 1220b57cec5SDimitry Andric if (L.DWARFExceptions) 1230b57cec5SDimitry Andric return EHPersonality::GNU_C; 1240b57cec5SDimitry Andric if (L.SEHExceptions) 1250b57cec5SDimitry Andric return EHPersonality::GNU_C_SEH; 1260b57cec5SDimitry Andric return EHPersonality::GNU_C; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric static const EHPersonality &getObjCPersonality(const TargetInfo &Target, 1300b57cec5SDimitry Andric const LangOptions &L) { 1310b57cec5SDimitry Andric const llvm::Triple &T = Target.getTriple(); 1320b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment()) 1330b57cec5SDimitry Andric return EHPersonality::MSVC_CxxFrameHandler3; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric switch (L.ObjCRuntime.getKind()) { 1360b57cec5SDimitry Andric case ObjCRuntime::FragileMacOSX: 1370b57cec5SDimitry Andric return getCPersonality(Target, L); 1380b57cec5SDimitry Andric case ObjCRuntime::MacOSX: 1390b57cec5SDimitry Andric case ObjCRuntime::iOS: 1400b57cec5SDimitry Andric case ObjCRuntime::WatchOS: 1410b57cec5SDimitry Andric return EHPersonality::NeXT_ObjC; 1420b57cec5SDimitry Andric case ObjCRuntime::GNUstep: 1430b57cec5SDimitry Andric if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7)) 1440b57cec5SDimitry Andric return EHPersonality::GNUstep_ObjC; 1450b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1460b57cec5SDimitry Andric case ObjCRuntime::GCC: 1470b57cec5SDimitry Andric case ObjCRuntime::ObjFW: 1480b57cec5SDimitry Andric if (L.SjLjExceptions) 1490b57cec5SDimitry Andric return EHPersonality::GNU_ObjC_SJLJ; 1500b57cec5SDimitry Andric if (L.SEHExceptions) 1510b57cec5SDimitry Andric return EHPersonality::GNU_ObjC_SEH; 1520b57cec5SDimitry Andric return EHPersonality::GNU_ObjC; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric llvm_unreachable("bad runtime kind"); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric static const EHPersonality &getCXXPersonality(const TargetInfo &Target, 1580b57cec5SDimitry Andric const LangOptions &L) { 1590b57cec5SDimitry Andric const llvm::Triple &T = Target.getTriple(); 1600b57cec5SDimitry Andric if (T.isWindowsMSVCEnvironment()) 1610b57cec5SDimitry Andric return EHPersonality::MSVC_CxxFrameHandler3; 1620b57cec5SDimitry Andric if (L.SjLjExceptions) 1630b57cec5SDimitry Andric return EHPersonality::GNU_CPlusPlus_SJLJ; 1640b57cec5SDimitry Andric if (L.DWARFExceptions) 1650b57cec5SDimitry Andric return EHPersonality::GNU_CPlusPlus; 1660b57cec5SDimitry Andric if (L.SEHExceptions) 1670b57cec5SDimitry Andric return EHPersonality::GNU_CPlusPlus_SEH; 168*a7dea167SDimitry Andric if (L.WasmExceptions) 1690b57cec5SDimitry Andric return EHPersonality::GNU_Wasm_CPlusPlus; 1700b57cec5SDimitry Andric return EHPersonality::GNU_CPlusPlus; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric /// Determines the personality function to use when both C++ 1740b57cec5SDimitry Andric /// and Objective-C exceptions are being caught. 1750b57cec5SDimitry Andric static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target, 1760b57cec5SDimitry Andric const LangOptions &L) { 1770b57cec5SDimitry Andric if (Target.getTriple().isWindowsMSVCEnvironment()) 1780b57cec5SDimitry Andric return EHPersonality::MSVC_CxxFrameHandler3; 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric switch (L.ObjCRuntime.getKind()) { 1810b57cec5SDimitry Andric // In the fragile ABI, just use C++ exception handling and hope 1820b57cec5SDimitry Andric // they're not doing crazy exception mixing. 1830b57cec5SDimitry Andric case ObjCRuntime::FragileMacOSX: 1840b57cec5SDimitry Andric return getCXXPersonality(Target, L); 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // The ObjC personality defers to the C++ personality for non-ObjC 1870b57cec5SDimitry Andric // handlers. Unlike the C++ case, we use the same personality 1880b57cec5SDimitry Andric // function on targets using (backend-driven) SJLJ EH. 1890b57cec5SDimitry Andric case ObjCRuntime::MacOSX: 1900b57cec5SDimitry Andric case ObjCRuntime::iOS: 1910b57cec5SDimitry Andric case ObjCRuntime::WatchOS: 1920b57cec5SDimitry Andric return getObjCPersonality(Target, L); 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric case ObjCRuntime::GNUstep: 1950b57cec5SDimitry Andric return EHPersonality::GNU_ObjCXX; 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric // The GCC runtime's personality function inherently doesn't support 1980b57cec5SDimitry Andric // mixed EH. Use the ObjC personality just to avoid returning null. 1990b57cec5SDimitry Andric case ObjCRuntime::GCC: 2000b57cec5SDimitry Andric case ObjCRuntime::ObjFW: 2010b57cec5SDimitry Andric return getObjCPersonality(Target, L); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric llvm_unreachable("bad runtime kind"); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) { 2070b57cec5SDimitry Andric if (T.getArch() == llvm::Triple::x86) 2080b57cec5SDimitry Andric return EHPersonality::MSVC_except_handler; 2090b57cec5SDimitry Andric return EHPersonality::MSVC_C_specific_handler; 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric const EHPersonality &EHPersonality::get(CodeGenModule &CGM, 2130b57cec5SDimitry Andric const FunctionDecl *FD) { 2140b57cec5SDimitry Andric const llvm::Triple &T = CGM.getTarget().getTriple(); 2150b57cec5SDimitry Andric const LangOptions &L = CGM.getLangOpts(); 2160b57cec5SDimitry Andric const TargetInfo &Target = CGM.getTarget(); 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric // Functions using SEH get an SEH personality. 2190b57cec5SDimitry Andric if (FD && FD->usesSEHTry()) 2200b57cec5SDimitry Andric return getSEHPersonalityMSVC(T); 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric if (L.ObjC) 2230b57cec5SDimitry Andric return L.CPlusPlus ? getObjCXXPersonality(Target, L) 2240b57cec5SDimitry Andric : getObjCPersonality(Target, L); 2250b57cec5SDimitry Andric return L.CPlusPlus ? getCXXPersonality(Target, L) 2260b57cec5SDimitry Andric : getCPersonality(Target, L); 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric const EHPersonality &EHPersonality::get(CodeGenFunction &CGF) { 2300b57cec5SDimitry Andric const auto *FD = CGF.CurCodeDecl; 2310b57cec5SDimitry Andric // For outlined finallys and filters, use the SEH personality in case they 2320b57cec5SDimitry Andric // contain more SEH. This mostly only affects finallys. Filters could 2330b57cec5SDimitry Andric // hypothetically use gnu statement expressions to sneak in nested SEH. 2340b57cec5SDimitry Andric FD = FD ? FD : CGF.CurSEHParent; 2350b57cec5SDimitry Andric return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(FD)); 2360b57cec5SDimitry Andric } 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric static llvm::FunctionCallee getPersonalityFn(CodeGenModule &CGM, 2390b57cec5SDimitry Andric const EHPersonality &Personality) { 2400b57cec5SDimitry Andric return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true), 2410b57cec5SDimitry Andric Personality.PersonalityFn, 2420b57cec5SDimitry Andric llvm::AttributeList(), /*Local=*/true); 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM, 2460b57cec5SDimitry Andric const EHPersonality &Personality) { 2470b57cec5SDimitry Andric llvm::FunctionCallee Fn = getPersonalityFn(CGM, Personality); 2480b57cec5SDimitry Andric llvm::PointerType* Int8PtrTy = llvm::PointerType::get( 2490b57cec5SDimitry Andric llvm::Type::getInt8Ty(CGM.getLLVMContext()), 2500b57cec5SDimitry Andric CGM.getDataLayout().getProgramAddressSpace()); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric return llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(Fn.getCallee()), 2530b57cec5SDimitry Andric Int8PtrTy); 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric /// Check whether a landingpad instruction only uses C++ features. 2570b57cec5SDimitry Andric static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI) { 2580b57cec5SDimitry Andric for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) { 2590b57cec5SDimitry Andric // Look for something that would've been returned by the ObjC 2600b57cec5SDimitry Andric // runtime's GetEHType() method. 2610b57cec5SDimitry Andric llvm::Value *Val = LPI->getClause(I)->stripPointerCasts(); 2620b57cec5SDimitry Andric if (LPI->isCatch(I)) { 2630b57cec5SDimitry Andric // Check if the catch value has the ObjC prefix. 2640b57cec5SDimitry Andric if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val)) 2650b57cec5SDimitry Andric // ObjC EH selector entries are always global variables with 2660b57cec5SDimitry Andric // names starting like this. 2670b57cec5SDimitry Andric if (GV->getName().startswith("OBJC_EHTYPE")) 2680b57cec5SDimitry Andric return false; 2690b57cec5SDimitry Andric } else { 2700b57cec5SDimitry Andric // Check if any of the filter values have the ObjC prefix. 2710b57cec5SDimitry Andric llvm::Constant *CVal = cast<llvm::Constant>(Val); 2720b57cec5SDimitry Andric for (llvm::User::op_iterator 2730b57cec5SDimitry Andric II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) { 2740b57cec5SDimitry Andric if (llvm::GlobalVariable *GV = 2750b57cec5SDimitry Andric cast<llvm::GlobalVariable>((*II)->stripPointerCasts())) 2760b57cec5SDimitry Andric // ObjC EH selector entries are always global variables with 2770b57cec5SDimitry Andric // names starting like this. 2780b57cec5SDimitry Andric if (GV->getName().startswith("OBJC_EHTYPE")) 2790b57cec5SDimitry Andric return false; 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric return true; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric /// Check whether a personality function could reasonably be swapped 2870b57cec5SDimitry Andric /// for a C++ personality function. 2880b57cec5SDimitry Andric static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) { 2890b57cec5SDimitry Andric for (llvm::User *U : Fn->users()) { 2900b57cec5SDimitry Andric // Conditionally white-list bitcasts. 2910b57cec5SDimitry Andric if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) { 2920b57cec5SDimitry Andric if (CE->getOpcode() != llvm::Instruction::BitCast) return false; 2930b57cec5SDimitry Andric if (!PersonalityHasOnlyCXXUses(CE)) 2940b57cec5SDimitry Andric return false; 2950b57cec5SDimitry Andric continue; 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric // Otherwise it must be a function. 2990b57cec5SDimitry Andric llvm::Function *F = dyn_cast<llvm::Function>(U); 3000b57cec5SDimitry Andric if (!F) return false; 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric for (auto BB = F->begin(), E = F->end(); BB != E; ++BB) { 3030b57cec5SDimitry Andric if (BB->isLandingPad()) 3040b57cec5SDimitry Andric if (!LandingPadHasOnlyCXXUses(BB->getLandingPadInst())) 3050b57cec5SDimitry Andric return false; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric return true; 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric /// Try to use the C++ personality function in ObjC++. Not doing this 3130b57cec5SDimitry Andric /// can cause some incompatibilities with gcc, which is more 3140b57cec5SDimitry Andric /// aggressive about only using the ObjC++ personality in a function 3150b57cec5SDimitry Andric /// when it really needs it. 3160b57cec5SDimitry Andric void CodeGenModule::SimplifyPersonality() { 3170b57cec5SDimitry Andric // If we're not in ObjC++ -fexceptions, there's nothing to do. 3180b57cec5SDimitry Andric if (!LangOpts.CPlusPlus || !LangOpts.ObjC || !LangOpts.Exceptions) 3190b57cec5SDimitry Andric return; 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Both the problem this endeavors to fix and the way the logic 3220b57cec5SDimitry Andric // above works is specific to the NeXT runtime. 3230b57cec5SDimitry Andric if (!LangOpts.ObjCRuntime.isNeXTFamily()) 3240b57cec5SDimitry Andric return; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr); 3270b57cec5SDimitry Andric const EHPersonality &CXX = getCXXPersonality(getTarget(), LangOpts); 3280b57cec5SDimitry Andric if (&ObjCXX == &CXX) 3290b57cec5SDimitry Andric return; 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 && 3320b57cec5SDimitry Andric "Different EHPersonalities using the same personality function."); 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn); 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // Nothing to do if it's unused. 3370b57cec5SDimitry Andric if (!Fn || Fn->use_empty()) return; 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric // Can't do the optimization if it has non-C++ uses. 3400b57cec5SDimitry Andric if (!PersonalityHasOnlyCXXUses(Fn)) return; 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric // Create the C++ personality function and kill off the old 3430b57cec5SDimitry Andric // function. 3440b57cec5SDimitry Andric llvm::FunctionCallee CXXFn = getPersonalityFn(*this, CXX); 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // This can happen if the user is screwing with us. 3470b57cec5SDimitry Andric if (Fn->getType() != CXXFn.getCallee()->getType()) 3480b57cec5SDimitry Andric return; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric Fn->replaceAllUsesWith(CXXFn.getCallee()); 3510b57cec5SDimitry Andric Fn->eraseFromParent(); 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric /// Returns the value to inject into a selector to indicate the 3550b57cec5SDimitry Andric /// presence of a catch-all. 3560b57cec5SDimitry Andric static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) { 3570b57cec5SDimitry Andric // Possibly we should use @llvm.eh.catch.all.value here. 3580b57cec5SDimitry Andric return llvm::ConstantPointerNull::get(CGF.Int8PtrTy); 3590b57cec5SDimitry Andric } 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric namespace { 3620b57cec5SDimitry Andric /// A cleanup to free the exception object if its initialization 3630b57cec5SDimitry Andric /// throws. 3640b57cec5SDimitry Andric struct FreeException final : EHScopeStack::Cleanup { 3650b57cec5SDimitry Andric llvm::Value *exn; 3660b57cec5SDimitry Andric FreeException(llvm::Value *exn) : exn(exn) {} 3670b57cec5SDimitry Andric void Emit(CodeGenFunction &CGF, Flags flags) override { 3680b57cec5SDimitry Andric CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric }; 3710b57cec5SDimitry Andric } // end anonymous namespace 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric // Emits an exception expression into the given location. This 3740b57cec5SDimitry Andric // differs from EmitAnyExprToMem only in that, if a final copy-ctor 3750b57cec5SDimitry Andric // call is required, an exception within that copy ctor causes 3760b57cec5SDimitry Andric // std::terminate to be invoked. 3770b57cec5SDimitry Andric void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) { 3780b57cec5SDimitry Andric // Make sure the exception object is cleaned up if there's an 3790b57cec5SDimitry Andric // exception during initialization. 3800b57cec5SDimitry Andric pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer()); 3810b57cec5SDimitry Andric EHScopeStack::stable_iterator cleanup = EHStack.stable_begin(); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric // __cxa_allocate_exception returns a void*; we need to cast this 3840b57cec5SDimitry Andric // to the appropriate type for the object. 3850b57cec5SDimitry Andric llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo(); 3860b57cec5SDimitry Andric Address typedAddr = Builder.CreateBitCast(addr, ty); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric // FIXME: this isn't quite right! If there's a final unelided call 3890b57cec5SDimitry Andric // to a copy constructor, then according to [except.terminate]p1 we 3900b57cec5SDimitry Andric // must call std::terminate() if that constructor throws, because 3910b57cec5SDimitry Andric // technically that copy occurs after the exception expression is 3920b57cec5SDimitry Andric // evaluated but before the exception is caught. But the best way 3930b57cec5SDimitry Andric // to handle that is to teach EmitAggExpr to do the final copy 3940b57cec5SDimitry Andric // differently if it can't be elided. 3950b57cec5SDimitry Andric EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(), 3960b57cec5SDimitry Andric /*IsInit*/ true); 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric // Deactivate the cleanup block. 3990b57cec5SDimitry Andric DeactivateCleanupBlock(cleanup, 4000b57cec5SDimitry Andric cast<llvm::Instruction>(typedAddr.getPointer())); 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric Address CodeGenFunction::getExceptionSlot() { 4040b57cec5SDimitry Andric if (!ExceptionSlot) 4050b57cec5SDimitry Andric ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot"); 4060b57cec5SDimitry Andric return Address(ExceptionSlot, getPointerAlign()); 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric Address CodeGenFunction::getEHSelectorSlot() { 4100b57cec5SDimitry Andric if (!EHSelectorSlot) 4110b57cec5SDimitry Andric EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot"); 4120b57cec5SDimitry Andric return Address(EHSelectorSlot, CharUnits::fromQuantity(4)); 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric 4150b57cec5SDimitry Andric llvm::Value *CodeGenFunction::getExceptionFromSlot() { 4160b57cec5SDimitry Andric return Builder.CreateLoad(getExceptionSlot(), "exn"); 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric llvm::Value *CodeGenFunction::getSelectorFromSlot() { 4200b57cec5SDimitry Andric return Builder.CreateLoad(getEHSelectorSlot(), "sel"); 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E, 4240b57cec5SDimitry Andric bool KeepInsertionPoint) { 4250b57cec5SDimitry Andric if (const Expr *SubExpr = E->getSubExpr()) { 4260b57cec5SDimitry Andric QualType ThrowType = SubExpr->getType(); 4270b57cec5SDimitry Andric if (ThrowType->isObjCObjectPointerType()) { 4280b57cec5SDimitry Andric const Stmt *ThrowStmt = E->getSubExpr(); 4290b57cec5SDimitry Andric const ObjCAtThrowStmt S(E->getExprLoc(), const_cast<Stmt *>(ThrowStmt)); 4300b57cec5SDimitry Andric CGM.getObjCRuntime().EmitThrowStmt(*this, S, false); 4310b57cec5SDimitry Andric } else { 4320b57cec5SDimitry Andric CGM.getCXXABI().emitThrow(*this, E); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric } else { 4350b57cec5SDimitry Andric CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric // throw is an expression, and the expression emitters expect us 4390b57cec5SDimitry Andric // to leave ourselves at a valid insertion point. 4400b57cec5SDimitry Andric if (KeepInsertionPoint) 4410b57cec5SDimitry Andric EmitBlock(createBasicBlock("throw.cont")); 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric void CodeGenFunction::EmitStartEHSpec(const Decl *D) { 4450b57cec5SDimitry Andric if (!CGM.getLangOpts().CXXExceptions) 4460b57cec5SDimitry Andric return; 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); 4490b57cec5SDimitry Andric if (!FD) { 4500b57cec5SDimitry Andric // Check if CapturedDecl is nothrow and create terminate scope for it. 4510b57cec5SDimitry Andric if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { 4520b57cec5SDimitry Andric if (CD->isNothrow()) 4530b57cec5SDimitry Andric EHStack.pushTerminate(); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric return; 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); 4580b57cec5SDimitry Andric if (!Proto) 4590b57cec5SDimitry Andric return; 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric ExceptionSpecificationType EST = Proto->getExceptionSpecType(); 4620b57cec5SDimitry Andric if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) { 4630b57cec5SDimitry Andric // noexcept functions are simple terminate scopes. 4640b57cec5SDimitry Andric EHStack.pushTerminate(); 4650b57cec5SDimitry Andric } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { 4660b57cec5SDimitry Andric // TODO: Revisit exception specifications for the MS ABI. There is a way to 4670b57cec5SDimitry Andric // encode these in an object file but MSVC doesn't do anything with it. 4680b57cec5SDimitry Andric if (getTarget().getCXXABI().isMicrosoft()) 4690b57cec5SDimitry Andric return; 4700b57cec5SDimitry Andric unsigned NumExceptions = Proto->getNumExceptions(); 4710b57cec5SDimitry Andric EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric for (unsigned I = 0; I != NumExceptions; ++I) { 4740b57cec5SDimitry Andric QualType Ty = Proto->getExceptionType(I); 4750b57cec5SDimitry Andric QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType(); 4760b57cec5SDimitry Andric llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, 4770b57cec5SDimitry Andric /*ForEH=*/true); 4780b57cec5SDimitry Andric Filter->setFilter(I, EHType); 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric } 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric /// Emit the dispatch block for a filter scope if necessary. 4840b57cec5SDimitry Andric static void emitFilterDispatchBlock(CodeGenFunction &CGF, 4850b57cec5SDimitry Andric EHFilterScope &filterScope) { 4860b57cec5SDimitry Andric llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock(); 4870b57cec5SDimitry Andric if (!dispatchBlock) return; 4880b57cec5SDimitry Andric if (dispatchBlock->use_empty()) { 4890b57cec5SDimitry Andric delete dispatchBlock; 4900b57cec5SDimitry Andric return; 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric CGF.EmitBlockAfterUses(dispatchBlock); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric // If this isn't a catch-all filter, we need to check whether we got 4960b57cec5SDimitry Andric // here because the filter triggered. 4970b57cec5SDimitry Andric if (filterScope.getNumFilters()) { 4980b57cec5SDimitry Andric // Load the selector value. 4990b57cec5SDimitry Andric llvm::Value *selector = CGF.getSelectorFromSlot(); 5000b57cec5SDimitry Andric llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected"); 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric llvm::Value *zero = CGF.Builder.getInt32(0); 5030b57cec5SDimitry Andric llvm::Value *failsFilter = 5040b57cec5SDimitry Andric CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails"); 5050b57cec5SDimitry Andric CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, 5060b57cec5SDimitry Andric CGF.getEHResumeBlock(false)); 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric CGF.EmitBlock(unexpectedBB); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric // Call __cxa_call_unexpected. This doesn't need to be an invoke 5120b57cec5SDimitry Andric // because __cxa_call_unexpected magically filters exceptions 5130b57cec5SDimitry Andric // according to the last landing pad the exception was thrown 5140b57cec5SDimitry Andric // into. Seriously. 5150b57cec5SDimitry Andric llvm::Value *exn = CGF.getExceptionFromSlot(); 5160b57cec5SDimitry Andric CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn) 5170b57cec5SDimitry Andric ->setDoesNotReturn(); 5180b57cec5SDimitry Andric CGF.Builder.CreateUnreachable(); 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric void CodeGenFunction::EmitEndEHSpec(const Decl *D) { 5220b57cec5SDimitry Andric if (!CGM.getLangOpts().CXXExceptions) 5230b57cec5SDimitry Andric return; 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); 5260b57cec5SDimitry Andric if (!FD) { 5270b57cec5SDimitry Andric // Check if CapturedDecl is nothrow and pop terminate scope for it. 5280b57cec5SDimitry Andric if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) { 5290b57cec5SDimitry Andric if (CD->isNothrow()) 5300b57cec5SDimitry Andric EHStack.popTerminate(); 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric return; 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); 5350b57cec5SDimitry Andric if (!Proto) 5360b57cec5SDimitry Andric return; 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric ExceptionSpecificationType EST = Proto->getExceptionSpecType(); 5390b57cec5SDimitry Andric if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) { 5400b57cec5SDimitry Andric EHStack.popTerminate(); 5410b57cec5SDimitry Andric } else if (EST == EST_Dynamic || EST == EST_DynamicNone) { 5420b57cec5SDimitry Andric // TODO: Revisit exception specifications for the MS ABI. There is a way to 5430b57cec5SDimitry Andric // encode these in an object file but MSVC doesn't do anything with it. 5440b57cec5SDimitry Andric if (getTarget().getCXXABI().isMicrosoft()) 5450b57cec5SDimitry Andric return; 5460b57cec5SDimitry Andric EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin()); 5470b57cec5SDimitry Andric emitFilterDispatchBlock(*this, filterScope); 5480b57cec5SDimitry Andric EHStack.popFilter(); 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { 5530b57cec5SDimitry Andric EnterCXXTryStmt(S); 5540b57cec5SDimitry Andric EmitStmt(S.getTryBlock()); 5550b57cec5SDimitry Andric ExitCXXTryStmt(S); 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { 5590b57cec5SDimitry Andric unsigned NumHandlers = S.getNumHandlers(); 5600b57cec5SDimitry Andric EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric for (unsigned I = 0; I != NumHandlers; ++I) { 5630b57cec5SDimitry Andric const CXXCatchStmt *C = S.getHandler(I); 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric llvm::BasicBlock *Handler = createBasicBlock("catch"); 5660b57cec5SDimitry Andric if (C->getExceptionDecl()) { 5670b57cec5SDimitry Andric // FIXME: Dropping the reference type on the type into makes it 5680b57cec5SDimitry Andric // impossible to correctly implement catch-by-reference 5690b57cec5SDimitry Andric // semantics for pointers. Unfortunately, this is what all 5700b57cec5SDimitry Andric // existing compilers do, and it's not clear that the standard 5710b57cec5SDimitry Andric // personality routine is capable of doing this right. See C++ DR 388: 5720b57cec5SDimitry Andric // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388 5730b57cec5SDimitry Andric Qualifiers CaughtTypeQuals; 5740b57cec5SDimitry Andric QualType CaughtType = CGM.getContext().getUnqualifiedArrayType( 5750b57cec5SDimitry Andric C->getCaughtType().getNonReferenceType(), CaughtTypeQuals); 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric CatchTypeInfo TypeInfo{nullptr, 0}; 5780b57cec5SDimitry Andric if (CaughtType->isObjCObjectPointerType()) 5790b57cec5SDimitry Andric TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType); 5800b57cec5SDimitry Andric else 5810b57cec5SDimitry Andric TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType( 5820b57cec5SDimitry Andric CaughtType, C->getCaughtType()); 5830b57cec5SDimitry Andric CatchScope->setHandler(I, TypeInfo, Handler); 5840b57cec5SDimitry Andric } else { 5850b57cec5SDimitry Andric // No exception decl indicates '...', a catch-all. 5860b57cec5SDimitry Andric CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler); 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric llvm::BasicBlock * 5920b57cec5SDimitry Andric CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) { 5930b57cec5SDimitry Andric if (EHPersonality::get(*this).usesFuncletPads()) 5940b57cec5SDimitry Andric return getFuncletEHDispatchBlock(si); 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric // The dispatch block for the end of the scope chain is a block that 5970b57cec5SDimitry Andric // just resumes unwinding. 5980b57cec5SDimitry Andric if (si == EHStack.stable_end()) 5990b57cec5SDimitry Andric return getEHResumeBlock(true); 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric // Otherwise, we should look at the actual scope. 6020b57cec5SDimitry Andric EHScope &scope = *EHStack.find(si); 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock(); 6050b57cec5SDimitry Andric if (!dispatchBlock) { 6060b57cec5SDimitry Andric switch (scope.getKind()) { 6070b57cec5SDimitry Andric case EHScope::Catch: { 6080b57cec5SDimitry Andric // Apply a special case to a single catch-all. 6090b57cec5SDimitry Andric EHCatchScope &catchScope = cast<EHCatchScope>(scope); 6100b57cec5SDimitry Andric if (catchScope.getNumHandlers() == 1 && 6110b57cec5SDimitry Andric catchScope.getHandler(0).isCatchAll()) { 6120b57cec5SDimitry Andric dispatchBlock = catchScope.getHandler(0).Block; 6130b57cec5SDimitry Andric 6140b57cec5SDimitry Andric // Otherwise, make a dispatch block. 6150b57cec5SDimitry Andric } else { 6160b57cec5SDimitry Andric dispatchBlock = createBasicBlock("catch.dispatch"); 6170b57cec5SDimitry Andric } 6180b57cec5SDimitry Andric break; 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric case EHScope::Cleanup: 6220b57cec5SDimitry Andric dispatchBlock = createBasicBlock("ehcleanup"); 6230b57cec5SDimitry Andric break; 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric case EHScope::Filter: 6260b57cec5SDimitry Andric dispatchBlock = createBasicBlock("filter.dispatch"); 6270b57cec5SDimitry Andric break; 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric case EHScope::Terminate: 6300b57cec5SDimitry Andric dispatchBlock = getTerminateHandler(); 6310b57cec5SDimitry Andric break; 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric case EHScope::PadEnd: 6340b57cec5SDimitry Andric llvm_unreachable("PadEnd unnecessary for Itanium!"); 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric scope.setCachedEHDispatchBlock(dispatchBlock); 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric return dispatchBlock; 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric llvm::BasicBlock * 6420b57cec5SDimitry Andric CodeGenFunction::getFuncletEHDispatchBlock(EHScopeStack::stable_iterator SI) { 6430b57cec5SDimitry Andric // Returning nullptr indicates that the previous dispatch block should unwind 6440b57cec5SDimitry Andric // to caller. 6450b57cec5SDimitry Andric if (SI == EHStack.stable_end()) 6460b57cec5SDimitry Andric return nullptr; 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric // Otherwise, we should look at the actual scope. 6490b57cec5SDimitry Andric EHScope &EHS = *EHStack.find(SI); 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric llvm::BasicBlock *DispatchBlock = EHS.getCachedEHDispatchBlock(); 6520b57cec5SDimitry Andric if (DispatchBlock) 6530b57cec5SDimitry Andric return DispatchBlock; 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andric if (EHS.getKind() == EHScope::Terminate) 6560b57cec5SDimitry Andric DispatchBlock = getTerminateFunclet(); 6570b57cec5SDimitry Andric else 6580b57cec5SDimitry Andric DispatchBlock = createBasicBlock(); 6590b57cec5SDimitry Andric CGBuilderTy Builder(*this, DispatchBlock); 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric switch (EHS.getKind()) { 6620b57cec5SDimitry Andric case EHScope::Catch: 6630b57cec5SDimitry Andric DispatchBlock->setName("catch.dispatch"); 6640b57cec5SDimitry Andric break; 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric case EHScope::Cleanup: 6670b57cec5SDimitry Andric DispatchBlock->setName("ehcleanup"); 6680b57cec5SDimitry Andric break; 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric case EHScope::Filter: 6710b57cec5SDimitry Andric llvm_unreachable("exception specifications not handled yet!"); 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric case EHScope::Terminate: 6740b57cec5SDimitry Andric DispatchBlock->setName("terminate"); 6750b57cec5SDimitry Andric break; 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric case EHScope::PadEnd: 6780b57cec5SDimitry Andric llvm_unreachable("PadEnd dispatch block missing!"); 6790b57cec5SDimitry Andric } 6800b57cec5SDimitry Andric EHS.setCachedEHDispatchBlock(DispatchBlock); 6810b57cec5SDimitry Andric return DispatchBlock; 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric /// Check whether this is a non-EH scope, i.e. a scope which doesn't 6850b57cec5SDimitry Andric /// affect exception handling. Currently, the only non-EH scopes are 6860b57cec5SDimitry Andric /// normal-only cleanup scopes. 6870b57cec5SDimitry Andric static bool isNonEHScope(const EHScope &S) { 6880b57cec5SDimitry Andric switch (S.getKind()) { 6890b57cec5SDimitry Andric case EHScope::Cleanup: 6900b57cec5SDimitry Andric return !cast<EHCleanupScope>(S).isEHCleanup(); 6910b57cec5SDimitry Andric case EHScope::Filter: 6920b57cec5SDimitry Andric case EHScope::Catch: 6930b57cec5SDimitry Andric case EHScope::Terminate: 6940b57cec5SDimitry Andric case EHScope::PadEnd: 6950b57cec5SDimitry Andric return false; 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric llvm_unreachable("Invalid EHScope Kind!"); 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() { 7020b57cec5SDimitry Andric assert(EHStack.requiresLandingPad()); 7030b57cec5SDimitry Andric assert(!EHStack.empty()); 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric // If exceptions are disabled and SEH is not in use, then there is no invoke 7060b57cec5SDimitry Andric // destination. SEH "works" even if exceptions are off. In practice, this 7070b57cec5SDimitry Andric // means that C++ destructors and other EH cleanups don't run, which is 7080b57cec5SDimitry Andric // consistent with MSVC's behavior. 7090b57cec5SDimitry Andric const LangOptions &LO = CGM.getLangOpts(); 7100b57cec5SDimitry Andric if (!LO.Exceptions) { 7110b57cec5SDimitry Andric if (!LO.Borland && !LO.MicrosoftExt) 7120b57cec5SDimitry Andric return nullptr; 7130b57cec5SDimitry Andric if (!currentFunctionUsesSEHTry()) 7140b57cec5SDimitry Andric return nullptr; 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric // CUDA device code doesn't have exceptions. 7180b57cec5SDimitry Andric if (LO.CUDA && LO.CUDAIsDevice) 7190b57cec5SDimitry Andric return nullptr; 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric // Check the innermost scope for a cached landing pad. If this is 7220b57cec5SDimitry Andric // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad. 7230b57cec5SDimitry Andric llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); 7240b57cec5SDimitry Andric if (LP) return LP; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric const EHPersonality &Personality = EHPersonality::get(*this); 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric if (!CurFn->hasPersonalityFn()) 7290b57cec5SDimitry Andric CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric if (Personality.usesFuncletPads()) { 7320b57cec5SDimitry Andric // We don't need separate landing pads in the funclet model. 7330b57cec5SDimitry Andric LP = getEHDispatchBlock(EHStack.getInnermostEHScope()); 7340b57cec5SDimitry Andric } else { 7350b57cec5SDimitry Andric // Build the landing pad for this scope. 7360b57cec5SDimitry Andric LP = EmitLandingPad(); 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric assert(LP); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // Cache the landing pad on the innermost scope. If this is a 7420b57cec5SDimitry Andric // non-EH scope, cache the landing pad on the enclosing scope, too. 7430b57cec5SDimitry Andric for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { 7440b57cec5SDimitry Andric ir->setCachedLandingPad(LP); 7450b57cec5SDimitry Andric if (!isNonEHScope(*ir)) break; 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric return LP; 7490b57cec5SDimitry Andric } 7500b57cec5SDimitry Andric 7510b57cec5SDimitry Andric llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { 7520b57cec5SDimitry Andric assert(EHStack.requiresLandingPad()); 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope()); 7550b57cec5SDimitry Andric switch (innermostEHScope.getKind()) { 7560b57cec5SDimitry Andric case EHScope::Terminate: 7570b57cec5SDimitry Andric return getTerminateLandingPad(); 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric case EHScope::PadEnd: 7600b57cec5SDimitry Andric llvm_unreachable("PadEnd unnecessary for Itanium!"); 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric case EHScope::Catch: 7630b57cec5SDimitry Andric case EHScope::Cleanup: 7640b57cec5SDimitry Andric case EHScope::Filter: 7650b57cec5SDimitry Andric if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad()) 7660b57cec5SDimitry Andric return lpad; 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric // Save the current IR generation state. 7700b57cec5SDimitry Andric CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP(); 7710b57cec5SDimitry Andric auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation); 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric // Create and configure the landing pad. 7740b57cec5SDimitry Andric llvm::BasicBlock *lpad = createBasicBlock("lpad"); 7750b57cec5SDimitry Andric EmitBlock(lpad); 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric llvm::LandingPadInst *LPadInst = 7780b57cec5SDimitry Andric Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0); 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0); 7810b57cec5SDimitry Andric Builder.CreateStore(LPadExn, getExceptionSlot()); 7820b57cec5SDimitry Andric llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1); 7830b57cec5SDimitry Andric Builder.CreateStore(LPadSel, getEHSelectorSlot()); 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric // Save the exception pointer. It's safe to use a single exception 7860b57cec5SDimitry Andric // pointer per function because EH cleanups can never have nested 7870b57cec5SDimitry Andric // try/catches. 7880b57cec5SDimitry Andric // Build the landingpad instruction. 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric // Accumulate all the handlers in scope. 7910b57cec5SDimitry Andric bool hasCatchAll = false; 7920b57cec5SDimitry Andric bool hasCleanup = false; 7930b57cec5SDimitry Andric bool hasFilter = false; 7940b57cec5SDimitry Andric SmallVector<llvm::Value*, 4> filterTypes; 7950b57cec5SDimitry Andric llvm::SmallPtrSet<llvm::Value*, 4> catchTypes; 7960b57cec5SDimitry Andric for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E; 7970b57cec5SDimitry Andric ++I) { 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric switch (I->getKind()) { 8000b57cec5SDimitry Andric case EHScope::Cleanup: 8010b57cec5SDimitry Andric // If we have a cleanup, remember that. 8020b57cec5SDimitry Andric hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup()); 8030b57cec5SDimitry Andric continue; 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric case EHScope::Filter: { 8060b57cec5SDimitry Andric assert(I.next() == EHStack.end() && "EH filter is not end of EH stack"); 8070b57cec5SDimitry Andric assert(!hasCatchAll && "EH filter reached after catch-all"); 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric // Filter scopes get added to the landingpad in weird ways. 8100b57cec5SDimitry Andric EHFilterScope &filter = cast<EHFilterScope>(*I); 8110b57cec5SDimitry Andric hasFilter = true; 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric // Add all the filter values. 8140b57cec5SDimitry Andric for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) 8150b57cec5SDimitry Andric filterTypes.push_back(filter.getFilter(i)); 8160b57cec5SDimitry Andric goto done; 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric case EHScope::Terminate: 8200b57cec5SDimitry Andric // Terminate scopes are basically catch-alls. 8210b57cec5SDimitry Andric assert(!hasCatchAll); 8220b57cec5SDimitry Andric hasCatchAll = true; 8230b57cec5SDimitry Andric goto done; 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric case EHScope::Catch: 8260b57cec5SDimitry Andric break; 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric case EHScope::PadEnd: 8290b57cec5SDimitry Andric llvm_unreachable("PadEnd unnecessary for Itanium!"); 8300b57cec5SDimitry Andric } 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric EHCatchScope &catchScope = cast<EHCatchScope>(*I); 8330b57cec5SDimitry Andric for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) { 8340b57cec5SDimitry Andric EHCatchScope::Handler handler = catchScope.getHandler(hi); 8350b57cec5SDimitry Andric assert(handler.Type.Flags == 0 && 8360b57cec5SDimitry Andric "landingpads do not support catch handler flags"); 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric // If this is a catch-all, register that and abort. 8390b57cec5SDimitry Andric if (!handler.Type.RTTI) { 8400b57cec5SDimitry Andric assert(!hasCatchAll); 8410b57cec5SDimitry Andric hasCatchAll = true; 8420b57cec5SDimitry Andric goto done; 8430b57cec5SDimitry Andric } 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric // Check whether we already have a handler for this type. 8460b57cec5SDimitry Andric if (catchTypes.insert(handler.Type.RTTI).second) 8470b57cec5SDimitry Andric // If not, add it directly to the landingpad. 8480b57cec5SDimitry Andric LPadInst->addClause(handler.Type.RTTI); 8490b57cec5SDimitry Andric } 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric done: 8530b57cec5SDimitry Andric // If we have a catch-all, add null to the landingpad. 8540b57cec5SDimitry Andric assert(!(hasCatchAll && hasFilter)); 8550b57cec5SDimitry Andric if (hasCatchAll) { 8560b57cec5SDimitry Andric LPadInst->addClause(getCatchAllValue(*this)); 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric // If we have an EH filter, we need to add those handlers in the 8590b57cec5SDimitry Andric // right place in the landingpad, which is to say, at the end. 8600b57cec5SDimitry Andric } else if (hasFilter) { 8610b57cec5SDimitry Andric // Create a filter expression: a constant array indicating which filter 8620b57cec5SDimitry Andric // types there are. The personality routine only lands here if the filter 8630b57cec5SDimitry Andric // doesn't match. 8640b57cec5SDimitry Andric SmallVector<llvm::Constant*, 8> Filters; 8650b57cec5SDimitry Andric llvm::ArrayType *AType = 8660b57cec5SDimitry Andric llvm::ArrayType::get(!filterTypes.empty() ? 8670b57cec5SDimitry Andric filterTypes[0]->getType() : Int8PtrTy, 8680b57cec5SDimitry Andric filterTypes.size()); 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric for (unsigned i = 0, e = filterTypes.size(); i != e; ++i) 8710b57cec5SDimitry Andric Filters.push_back(cast<llvm::Constant>(filterTypes[i])); 8720b57cec5SDimitry Andric llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters); 8730b57cec5SDimitry Andric LPadInst->addClause(FilterArray); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric // Also check whether we need a cleanup. 8760b57cec5SDimitry Andric if (hasCleanup) 8770b57cec5SDimitry Andric LPadInst->setCleanup(true); 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // Otherwise, signal that we at least have cleanups. 8800b57cec5SDimitry Andric } else if (hasCleanup) { 8810b57cec5SDimitry Andric LPadInst->setCleanup(true); 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) && 8850b57cec5SDimitry Andric "landingpad instruction has no clauses!"); 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric // Tell the backend how to generate the landing pad. 8880b57cec5SDimitry Andric Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope())); 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric // Restore the old IR generation state. 8910b57cec5SDimitry Andric Builder.restoreIP(savedIP); 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric return lpad; 8940b57cec5SDimitry Andric } 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) { 8970b57cec5SDimitry Andric llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); 8980b57cec5SDimitry Andric assert(DispatchBlock); 8990b57cec5SDimitry Andric 9000b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); 9010b57cec5SDimitry Andric CGF.EmitBlockAfterUses(DispatchBlock); 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric llvm::Value *ParentPad = CGF.CurrentFuncletPad; 9040b57cec5SDimitry Andric if (!ParentPad) 9050b57cec5SDimitry Andric ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); 9060b57cec5SDimitry Andric llvm::BasicBlock *UnwindBB = 9070b57cec5SDimitry Andric CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric unsigned NumHandlers = CatchScope.getNumHandlers(); 9100b57cec5SDimitry Andric llvm::CatchSwitchInst *CatchSwitch = 9110b57cec5SDimitry Andric CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric // Test against each of the exception types we claim to catch. 9140b57cec5SDimitry Andric for (unsigned I = 0; I < NumHandlers; ++I) { 9150b57cec5SDimitry Andric const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric CatchTypeInfo TypeInfo = Handler.Type; 9180b57cec5SDimitry Andric if (!TypeInfo.RTTI) 9190b57cec5SDimitry Andric TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric CGF.Builder.SetInsertPoint(Handler.Block); 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric if (EHPersonality::get(CGF).isMSVCXXPersonality()) { 9240b57cec5SDimitry Andric CGF.Builder.CreateCatchPad( 9250b57cec5SDimitry Andric CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags), 9260b57cec5SDimitry Andric llvm::Constant::getNullValue(CGF.VoidPtrTy)}); 9270b57cec5SDimitry Andric } else { 9280b57cec5SDimitry Andric CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI}); 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric CatchSwitch->addHandler(Handler.Block); 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric CGF.Builder.restoreIP(SavedIP); 9340b57cec5SDimitry Andric } 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric // Wasm uses Windows-style EH instructions, but it merges all catch clauses into 9370b57cec5SDimitry Andric // one big catchpad, within which we use Itanium's landingpad-style selector 9380b57cec5SDimitry Andric // comparison instructions. 9390b57cec5SDimitry Andric static void emitWasmCatchPadBlock(CodeGenFunction &CGF, 9400b57cec5SDimitry Andric EHCatchScope &CatchScope) { 9410b57cec5SDimitry Andric llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); 9420b57cec5SDimitry Andric assert(DispatchBlock); 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP(); 9450b57cec5SDimitry Andric CGF.EmitBlockAfterUses(DispatchBlock); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric llvm::Value *ParentPad = CGF.CurrentFuncletPad; 9480b57cec5SDimitry Andric if (!ParentPad) 9490b57cec5SDimitry Andric ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext()); 9500b57cec5SDimitry Andric llvm::BasicBlock *UnwindBB = 9510b57cec5SDimitry Andric CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope()); 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric unsigned NumHandlers = CatchScope.getNumHandlers(); 9540b57cec5SDimitry Andric llvm::CatchSwitchInst *CatchSwitch = 9550b57cec5SDimitry Andric CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers); 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric // We don't use a landingpad instruction, so generate intrinsic calls to 9580b57cec5SDimitry Andric // provide exception and selector values. 9590b57cec5SDimitry Andric llvm::BasicBlock *WasmCatchStartBlock = CGF.createBasicBlock("catch.start"); 9600b57cec5SDimitry Andric CatchSwitch->addHandler(WasmCatchStartBlock); 9610b57cec5SDimitry Andric CGF.EmitBlockAfterUses(WasmCatchStartBlock); 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric // Create a catchpad instruction. 9640b57cec5SDimitry Andric SmallVector<llvm::Value *, 4> CatchTypes; 9650b57cec5SDimitry Andric for (unsigned I = 0, E = NumHandlers; I < E; ++I) { 9660b57cec5SDimitry Andric const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); 9670b57cec5SDimitry Andric CatchTypeInfo TypeInfo = Handler.Type; 9680b57cec5SDimitry Andric if (!TypeInfo.RTTI) 9690b57cec5SDimitry Andric TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); 9700b57cec5SDimitry Andric CatchTypes.push_back(TypeInfo.RTTI); 9710b57cec5SDimitry Andric } 9720b57cec5SDimitry Andric auto *CPI = CGF.Builder.CreateCatchPad(CatchSwitch, CatchTypes); 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric // Create calls to wasm.get.exception and wasm.get.ehselector intrinsics. 9750b57cec5SDimitry Andric // Before they are lowered appropriately later, they provide values for the 9760b57cec5SDimitry Andric // exception and selector. 9770b57cec5SDimitry Andric llvm::Function *GetExnFn = 9780b57cec5SDimitry Andric CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception); 9790b57cec5SDimitry Andric llvm::Function *GetSelectorFn = 9800b57cec5SDimitry Andric CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_ehselector); 9810b57cec5SDimitry Andric llvm::CallInst *Exn = CGF.Builder.CreateCall(GetExnFn, CPI); 9820b57cec5SDimitry Andric CGF.Builder.CreateStore(Exn, CGF.getExceptionSlot()); 9830b57cec5SDimitry Andric llvm::CallInst *Selector = CGF.Builder.CreateCall(GetSelectorFn, CPI); 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric llvm::Function *TypeIDFn = CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric // If there's only a single catch-all, branch directly to its handler. 9880b57cec5SDimitry Andric if (CatchScope.getNumHandlers() == 1 && 9890b57cec5SDimitry Andric CatchScope.getHandler(0).isCatchAll()) { 9900b57cec5SDimitry Andric CGF.Builder.CreateBr(CatchScope.getHandler(0).Block); 9910b57cec5SDimitry Andric CGF.Builder.restoreIP(SavedIP); 9920b57cec5SDimitry Andric return; 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric // Test against each of the exception types we claim to catch. 9960b57cec5SDimitry Andric for (unsigned I = 0, E = NumHandlers;; ++I) { 9970b57cec5SDimitry Andric assert(I < E && "ran off end of handlers!"); 9980b57cec5SDimitry Andric const EHCatchScope::Handler &Handler = CatchScope.getHandler(I); 9990b57cec5SDimitry Andric CatchTypeInfo TypeInfo = Handler.Type; 10000b57cec5SDimitry Andric if (!TypeInfo.RTTI) 10010b57cec5SDimitry Andric TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric // Figure out the next block. 10040b57cec5SDimitry Andric llvm::BasicBlock *NextBlock; 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric bool EmitNextBlock = false, NextIsEnd = false; 10070b57cec5SDimitry Andric 10080b57cec5SDimitry Andric // If this is the last handler, we're at the end, and the next block is a 10090b57cec5SDimitry Andric // block that contains a call to the rethrow function, so we can unwind to 10100b57cec5SDimitry Andric // the enclosing EH scope. The call itself will be generated later. 10110b57cec5SDimitry Andric if (I + 1 == E) { 10120b57cec5SDimitry Andric NextBlock = CGF.createBasicBlock("rethrow"); 10130b57cec5SDimitry Andric EmitNextBlock = true; 10140b57cec5SDimitry Andric NextIsEnd = true; 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric // If the next handler is a catch-all, we're at the end, and the 10170b57cec5SDimitry Andric // next block is that handler. 10180b57cec5SDimitry Andric } else if (CatchScope.getHandler(I + 1).isCatchAll()) { 10190b57cec5SDimitry Andric NextBlock = CatchScope.getHandler(I + 1).Block; 10200b57cec5SDimitry Andric NextIsEnd = true; 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric // Otherwise, we're not at the end and we need a new block. 10230b57cec5SDimitry Andric } else { 10240b57cec5SDimitry Andric NextBlock = CGF.createBasicBlock("catch.fallthrough"); 10250b57cec5SDimitry Andric EmitNextBlock = true; 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric // Figure out the catch type's index in the LSDA's type table. 10290b57cec5SDimitry Andric llvm::CallInst *TypeIndex = CGF.Builder.CreateCall(TypeIDFn, TypeInfo.RTTI); 10300b57cec5SDimitry Andric TypeIndex->setDoesNotThrow(); 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric llvm::Value *MatchesTypeIndex = 10330b57cec5SDimitry Andric CGF.Builder.CreateICmpEQ(Selector, TypeIndex, "matches"); 10340b57cec5SDimitry Andric CGF.Builder.CreateCondBr(MatchesTypeIndex, Handler.Block, NextBlock); 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric if (EmitNextBlock) 10370b57cec5SDimitry Andric CGF.EmitBlock(NextBlock); 10380b57cec5SDimitry Andric if (NextIsEnd) 10390b57cec5SDimitry Andric break; 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric CGF.Builder.restoreIP(SavedIP); 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric 10450b57cec5SDimitry Andric /// Emit the structure of the dispatch block for the given catch scope. 10460b57cec5SDimitry Andric /// It is an invariant that the dispatch block already exists. 10470b57cec5SDimitry Andric static void emitCatchDispatchBlock(CodeGenFunction &CGF, 10480b57cec5SDimitry Andric EHCatchScope &catchScope) { 10490b57cec5SDimitry Andric if (EHPersonality::get(CGF).isWasmPersonality()) 10500b57cec5SDimitry Andric return emitWasmCatchPadBlock(CGF, catchScope); 10510b57cec5SDimitry Andric if (EHPersonality::get(CGF).usesFuncletPads()) 10520b57cec5SDimitry Andric return emitCatchPadBlock(CGF, catchScope); 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock(); 10550b57cec5SDimitry Andric assert(dispatchBlock); 10560b57cec5SDimitry Andric 10570b57cec5SDimitry Andric // If there's only a single catch-all, getEHDispatchBlock returned 10580b57cec5SDimitry Andric // that catch-all as the dispatch block. 10590b57cec5SDimitry Andric if (catchScope.getNumHandlers() == 1 && 10600b57cec5SDimitry Andric catchScope.getHandler(0).isCatchAll()) { 10610b57cec5SDimitry Andric assert(dispatchBlock == catchScope.getHandler(0).Block); 10620b57cec5SDimitry Andric return; 10630b57cec5SDimitry Andric } 10640b57cec5SDimitry Andric 10650b57cec5SDimitry Andric CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP(); 10660b57cec5SDimitry Andric CGF.EmitBlockAfterUses(dispatchBlock); 10670b57cec5SDimitry Andric 10680b57cec5SDimitry Andric // Select the right handler. 10690b57cec5SDimitry Andric llvm::Function *llvm_eh_typeid_for = 10700b57cec5SDimitry Andric CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric // Load the selector value. 10730b57cec5SDimitry Andric llvm::Value *selector = CGF.getSelectorFromSlot(); 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric // Test against each of the exception types we claim to catch. 10760b57cec5SDimitry Andric for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) { 10770b57cec5SDimitry Andric assert(i < e && "ran off end of handlers!"); 10780b57cec5SDimitry Andric const EHCatchScope::Handler &handler = catchScope.getHandler(i); 10790b57cec5SDimitry Andric 10800b57cec5SDimitry Andric llvm::Value *typeValue = handler.Type.RTTI; 10810b57cec5SDimitry Andric assert(handler.Type.Flags == 0 && 10820b57cec5SDimitry Andric "landingpads do not support catch handler flags"); 10830b57cec5SDimitry Andric assert(typeValue && "fell into catch-all case!"); 10840b57cec5SDimitry Andric typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy); 10850b57cec5SDimitry Andric 10860b57cec5SDimitry Andric // Figure out the next block. 10870b57cec5SDimitry Andric bool nextIsEnd; 10880b57cec5SDimitry Andric llvm::BasicBlock *nextBlock; 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric // If this is the last handler, we're at the end, and the next 10910b57cec5SDimitry Andric // block is the block for the enclosing EH scope. 10920b57cec5SDimitry Andric if (i + 1 == e) { 10930b57cec5SDimitry Andric nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope()); 10940b57cec5SDimitry Andric nextIsEnd = true; 10950b57cec5SDimitry Andric 10960b57cec5SDimitry Andric // If the next handler is a catch-all, we're at the end, and the 10970b57cec5SDimitry Andric // next block is that handler. 10980b57cec5SDimitry Andric } else if (catchScope.getHandler(i+1).isCatchAll()) { 10990b57cec5SDimitry Andric nextBlock = catchScope.getHandler(i+1).Block; 11000b57cec5SDimitry Andric nextIsEnd = true; 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric // Otherwise, we're not at the end and we need a new block. 11030b57cec5SDimitry Andric } else { 11040b57cec5SDimitry Andric nextBlock = CGF.createBasicBlock("catch.fallthrough"); 11050b57cec5SDimitry Andric nextIsEnd = false; 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric // Figure out the catch type's index in the LSDA's type table. 11090b57cec5SDimitry Andric llvm::CallInst *typeIndex = 11100b57cec5SDimitry Andric CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue); 11110b57cec5SDimitry Andric typeIndex->setDoesNotThrow(); 11120b57cec5SDimitry Andric 11130b57cec5SDimitry Andric llvm::Value *matchesTypeIndex = 11140b57cec5SDimitry Andric CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches"); 11150b57cec5SDimitry Andric CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock); 11160b57cec5SDimitry Andric 11170b57cec5SDimitry Andric // If the next handler is a catch-all, we're completely done. 11180b57cec5SDimitry Andric if (nextIsEnd) { 11190b57cec5SDimitry Andric CGF.Builder.restoreIP(savedIP); 11200b57cec5SDimitry Andric return; 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric // Otherwise we need to emit and continue at that block. 11230b57cec5SDimitry Andric CGF.EmitBlock(nextBlock); 11240b57cec5SDimitry Andric } 11250b57cec5SDimitry Andric } 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric void CodeGenFunction::popCatchScope() { 11280b57cec5SDimitry Andric EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin()); 11290b57cec5SDimitry Andric if (catchScope.hasEHBranches()) 11300b57cec5SDimitry Andric emitCatchDispatchBlock(*this, catchScope); 11310b57cec5SDimitry Andric EHStack.popCatch(); 11320b57cec5SDimitry Andric } 11330b57cec5SDimitry Andric 11340b57cec5SDimitry Andric void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { 11350b57cec5SDimitry Andric unsigned NumHandlers = S.getNumHandlers(); 11360b57cec5SDimitry Andric EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); 11370b57cec5SDimitry Andric assert(CatchScope.getNumHandlers() == NumHandlers); 11380b57cec5SDimitry Andric llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock(); 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric // If the catch was not required, bail out now. 11410b57cec5SDimitry Andric if (!CatchScope.hasEHBranches()) { 11420b57cec5SDimitry Andric CatchScope.clearHandlerBlocks(); 11430b57cec5SDimitry Andric EHStack.popCatch(); 11440b57cec5SDimitry Andric return; 11450b57cec5SDimitry Andric } 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andric // Emit the structure of the EH dispatch for this catch. 11480b57cec5SDimitry Andric emitCatchDispatchBlock(*this, CatchScope); 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric // Copy the handler blocks off before we pop the EH stack. Emitting 11510b57cec5SDimitry Andric // the handlers might scribble on this memory. 11520b57cec5SDimitry Andric SmallVector<EHCatchScope::Handler, 8> Handlers( 11530b57cec5SDimitry Andric CatchScope.begin(), CatchScope.begin() + NumHandlers); 11540b57cec5SDimitry Andric 11550b57cec5SDimitry Andric EHStack.popCatch(); 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andric // The fall-through block. 11580b57cec5SDimitry Andric llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric // We just emitted the body of the try; jump to the continue block. 11610b57cec5SDimitry Andric if (HaveInsertPoint()) 11620b57cec5SDimitry Andric Builder.CreateBr(ContBB); 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric // Determine if we need an implicit rethrow for all these catch handlers; 11650b57cec5SDimitry Andric // see the comment below. 11660b57cec5SDimitry Andric bool doImplicitRethrow = false; 11670b57cec5SDimitry Andric if (IsFnTryBlock) 11680b57cec5SDimitry Andric doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || 11690b57cec5SDimitry Andric isa<CXXConstructorDecl>(CurCodeDecl); 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric // Wasm uses Windows-style EH instructions, but merges all catch clauses into 11720b57cec5SDimitry Andric // one big catchpad. So we save the old funclet pad here before we traverse 11730b57cec5SDimitry Andric // each catch handler. 11740b57cec5SDimitry Andric SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( 11750b57cec5SDimitry Andric CurrentFuncletPad); 11760b57cec5SDimitry Andric llvm::BasicBlock *WasmCatchStartBlock = nullptr; 11770b57cec5SDimitry Andric if (EHPersonality::get(*this).isWasmPersonality()) { 11780b57cec5SDimitry Andric auto *CatchSwitch = 11790b57cec5SDimitry Andric cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHI()); 11800b57cec5SDimitry Andric WasmCatchStartBlock = CatchSwitch->hasUnwindDest() 11810b57cec5SDimitry Andric ? CatchSwitch->getSuccessor(1) 11820b57cec5SDimitry Andric : CatchSwitch->getSuccessor(0); 11830b57cec5SDimitry Andric auto *CPI = cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHI()); 11840b57cec5SDimitry Andric CurrentFuncletPad = CPI; 11850b57cec5SDimitry Andric } 11860b57cec5SDimitry Andric 11870b57cec5SDimitry Andric // Perversely, we emit the handlers backwards precisely because we 11880b57cec5SDimitry Andric // want them to appear in source order. In all of these cases, the 11890b57cec5SDimitry Andric // catch block will have exactly one predecessor, which will be a 11900b57cec5SDimitry Andric // particular block in the catch dispatch. However, in the case of 11910b57cec5SDimitry Andric // a catch-all, one of the dispatch blocks will branch to two 11920b57cec5SDimitry Andric // different handlers, and EmitBlockAfterUses will cause the second 11930b57cec5SDimitry Andric // handler to be moved before the first. 11940b57cec5SDimitry Andric bool HasCatchAll = false; 11950b57cec5SDimitry Andric for (unsigned I = NumHandlers; I != 0; --I) { 11960b57cec5SDimitry Andric HasCatchAll |= Handlers[I - 1].isCatchAll(); 11970b57cec5SDimitry Andric llvm::BasicBlock *CatchBlock = Handlers[I-1].Block; 11980b57cec5SDimitry Andric EmitBlockAfterUses(CatchBlock); 11990b57cec5SDimitry Andric 12000b57cec5SDimitry Andric // Catch the exception if this isn't a catch-all. 12010b57cec5SDimitry Andric const CXXCatchStmt *C = S.getHandler(I-1); 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric // Enter a cleanup scope, including the catch variable and the 12040b57cec5SDimitry Andric // end-catch. 12050b57cec5SDimitry Andric RunCleanupsScope CatchScope(*this); 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric // Initialize the catch variable and set up the cleanups. 12080b57cec5SDimitry Andric SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( 12090b57cec5SDimitry Andric CurrentFuncletPad); 12100b57cec5SDimitry Andric CGM.getCXXABI().emitBeginCatch(*this, C); 12110b57cec5SDimitry Andric 12120b57cec5SDimitry Andric // Emit the PGO counter increment. 12130b57cec5SDimitry Andric incrementProfileCounter(C); 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric // Perform the body of the catch. 12160b57cec5SDimitry Andric EmitStmt(C->getHandlerBlock()); 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric // [except.handle]p11: 12190b57cec5SDimitry Andric // The currently handled exception is rethrown if control 12200b57cec5SDimitry Andric // reaches the end of a handler of the function-try-block of a 12210b57cec5SDimitry Andric // constructor or destructor. 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric // It is important that we only do this on fallthrough and not on 12240b57cec5SDimitry Andric // return. Note that it's illegal to put a return in a 12250b57cec5SDimitry Andric // constructor function-try-block's catch handler (p14), so this 12260b57cec5SDimitry Andric // really only applies to destructors. 12270b57cec5SDimitry Andric if (doImplicitRethrow && HaveInsertPoint()) { 12280b57cec5SDimitry Andric CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false); 12290b57cec5SDimitry Andric Builder.CreateUnreachable(); 12300b57cec5SDimitry Andric Builder.ClearInsertionPoint(); 12310b57cec5SDimitry Andric } 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric // Fall out through the catch cleanups. 12340b57cec5SDimitry Andric CatchScope.ForceCleanup(); 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric // Branch out of the try. 12370b57cec5SDimitry Andric if (HaveInsertPoint()) 12380b57cec5SDimitry Andric Builder.CreateBr(ContBB); 12390b57cec5SDimitry Andric } 12400b57cec5SDimitry Andric 12410b57cec5SDimitry Andric // Because in wasm we merge all catch clauses into one big catchpad, in case 12420b57cec5SDimitry Andric // none of the types in catch handlers matches after we test against each of 12430b57cec5SDimitry Andric // them, we should unwind to the next EH enclosing scope. We generate a call 12440b57cec5SDimitry Andric // to rethrow function here to do that. 12450b57cec5SDimitry Andric if (EHPersonality::get(*this).isWasmPersonality() && !HasCatchAll) { 12460b57cec5SDimitry Andric assert(WasmCatchStartBlock); 12470b57cec5SDimitry Andric // Navigate for the "rethrow" block we created in emitWasmCatchPadBlock(). 12480b57cec5SDimitry Andric // Wasm uses landingpad-style conditional branches to compare selectors, so 12490b57cec5SDimitry Andric // we follow the false destination for each of the cond branches to reach 12500b57cec5SDimitry Andric // the rethrow block. 12510b57cec5SDimitry Andric llvm::BasicBlock *RethrowBlock = WasmCatchStartBlock; 12520b57cec5SDimitry Andric while (llvm::Instruction *TI = RethrowBlock->getTerminator()) { 12530b57cec5SDimitry Andric auto *BI = cast<llvm::BranchInst>(TI); 12540b57cec5SDimitry Andric assert(BI->isConditional()); 12550b57cec5SDimitry Andric RethrowBlock = BI->getSuccessor(1); 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric assert(RethrowBlock != WasmCatchStartBlock && RethrowBlock->empty()); 12580b57cec5SDimitry Andric Builder.SetInsertPoint(RethrowBlock); 12590b57cec5SDimitry Andric llvm::Function *RethrowInCatchFn = 12600b57cec5SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::wasm_rethrow_in_catch); 12610b57cec5SDimitry Andric EmitNoreturnRuntimeCallOrInvoke(RethrowInCatchFn, {}); 12620b57cec5SDimitry Andric } 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric EmitBlock(ContBB); 12650b57cec5SDimitry Andric incrementProfileCounter(&S); 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric namespace { 12690b57cec5SDimitry Andric struct CallEndCatchForFinally final : EHScopeStack::Cleanup { 12700b57cec5SDimitry Andric llvm::Value *ForEHVar; 12710b57cec5SDimitry Andric llvm::FunctionCallee EndCatchFn; 12720b57cec5SDimitry Andric CallEndCatchForFinally(llvm::Value *ForEHVar, 12730b57cec5SDimitry Andric llvm::FunctionCallee EndCatchFn) 12740b57cec5SDimitry Andric : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {} 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric void Emit(CodeGenFunction &CGF, Flags flags) override { 12770b57cec5SDimitry Andric llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch"); 12780b57cec5SDimitry Andric llvm::BasicBlock *CleanupContBB = 12790b57cec5SDimitry Andric CGF.createBasicBlock("finally.cleanup.cont"); 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric llvm::Value *ShouldEndCatch = 12820b57cec5SDimitry Andric CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch"); 12830b57cec5SDimitry Andric CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); 12840b57cec5SDimitry Andric CGF.EmitBlock(EndCatchBB); 12850b57cec5SDimitry Andric CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw 12860b57cec5SDimitry Andric CGF.EmitBlock(CleanupContBB); 12870b57cec5SDimitry Andric } 12880b57cec5SDimitry Andric }; 12890b57cec5SDimitry Andric 12900b57cec5SDimitry Andric struct PerformFinally final : EHScopeStack::Cleanup { 12910b57cec5SDimitry Andric const Stmt *Body; 12920b57cec5SDimitry Andric llvm::Value *ForEHVar; 12930b57cec5SDimitry Andric llvm::FunctionCallee EndCatchFn; 12940b57cec5SDimitry Andric llvm::FunctionCallee RethrowFn; 12950b57cec5SDimitry Andric llvm::Value *SavedExnVar; 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andric PerformFinally(const Stmt *Body, llvm::Value *ForEHVar, 12980b57cec5SDimitry Andric llvm::FunctionCallee EndCatchFn, 12990b57cec5SDimitry Andric llvm::FunctionCallee RethrowFn, llvm::Value *SavedExnVar) 13000b57cec5SDimitry Andric : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn), 13010b57cec5SDimitry Andric RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {} 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric void Emit(CodeGenFunction &CGF, Flags flags) override { 13040b57cec5SDimitry Andric // Enter a cleanup to call the end-catch function if one was provided. 13050b57cec5SDimitry Andric if (EndCatchFn) 13060b57cec5SDimitry Andric CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup, 13070b57cec5SDimitry Andric ForEHVar, EndCatchFn); 13080b57cec5SDimitry Andric 13090b57cec5SDimitry Andric // Save the current cleanup destination in case there are 13100b57cec5SDimitry Andric // cleanups in the finally block. 13110b57cec5SDimitry Andric llvm::Value *SavedCleanupDest = 13120b57cec5SDimitry Andric CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(), 13130b57cec5SDimitry Andric "cleanup.dest.saved"); 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric // Emit the finally block. 13160b57cec5SDimitry Andric CGF.EmitStmt(Body); 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric // If the end of the finally is reachable, check whether this was 13190b57cec5SDimitry Andric // for EH. If so, rethrow. 13200b57cec5SDimitry Andric if (CGF.HaveInsertPoint()) { 13210b57cec5SDimitry Andric llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow"); 13220b57cec5SDimitry Andric llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont"); 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric llvm::Value *ShouldRethrow = 13250b57cec5SDimitry Andric CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow"); 13260b57cec5SDimitry Andric CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric CGF.EmitBlock(RethrowBB); 13290b57cec5SDimitry Andric if (SavedExnVar) { 13300b57cec5SDimitry Andric CGF.EmitRuntimeCallOrInvoke(RethrowFn, 13310b57cec5SDimitry Andric CGF.Builder.CreateAlignedLoad(SavedExnVar, CGF.getPointerAlign())); 13320b57cec5SDimitry Andric } else { 13330b57cec5SDimitry Andric CGF.EmitRuntimeCallOrInvoke(RethrowFn); 13340b57cec5SDimitry Andric } 13350b57cec5SDimitry Andric CGF.Builder.CreateUnreachable(); 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric CGF.EmitBlock(ContBB); 13380b57cec5SDimitry Andric 13390b57cec5SDimitry Andric // Restore the cleanup destination. 13400b57cec5SDimitry Andric CGF.Builder.CreateStore(SavedCleanupDest, 13410b57cec5SDimitry Andric CGF.getNormalCleanupDestSlot()); 13420b57cec5SDimitry Andric } 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andric // Leave the end-catch cleanup. As an optimization, pretend that 13450b57cec5SDimitry Andric // the fallthrough path was inaccessible; we've dynamically proven 13460b57cec5SDimitry Andric // that we're not in the EH case along that path. 13470b57cec5SDimitry Andric if (EndCatchFn) { 13480b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); 13490b57cec5SDimitry Andric CGF.PopCleanupBlock(); 13500b57cec5SDimitry Andric CGF.Builder.restoreIP(SavedIP); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric // Now make sure we actually have an insertion point or the 13540b57cec5SDimitry Andric // cleanup gods will hate us. 13550b57cec5SDimitry Andric CGF.EnsureInsertPoint(); 13560b57cec5SDimitry Andric } 13570b57cec5SDimitry Andric }; 13580b57cec5SDimitry Andric } // end anonymous namespace 13590b57cec5SDimitry Andric 13600b57cec5SDimitry Andric /// Enters a finally block for an implementation using zero-cost 13610b57cec5SDimitry Andric /// exceptions. This is mostly general, but hard-codes some 13620b57cec5SDimitry Andric /// language/ABI-specific behavior in the catch-all sections. 13630b57cec5SDimitry Andric void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF, const Stmt *body, 13640b57cec5SDimitry Andric llvm::FunctionCallee beginCatchFn, 13650b57cec5SDimitry Andric llvm::FunctionCallee endCatchFn, 13660b57cec5SDimitry Andric llvm::FunctionCallee rethrowFn) { 13670b57cec5SDimitry Andric assert((!!beginCatchFn) == (!!endCatchFn) && 13680b57cec5SDimitry Andric "begin/end catch functions not paired"); 13690b57cec5SDimitry Andric assert(rethrowFn && "rethrow function is required"); 13700b57cec5SDimitry Andric 13710b57cec5SDimitry Andric BeginCatchFn = beginCatchFn; 13720b57cec5SDimitry Andric 13730b57cec5SDimitry Andric // The rethrow function has one of the following two types: 13740b57cec5SDimitry Andric // void (*)() 13750b57cec5SDimitry Andric // void (*)(void*) 13760b57cec5SDimitry Andric // In the latter case we need to pass it the exception object. 13770b57cec5SDimitry Andric // But we can't use the exception slot because the @finally might 13780b57cec5SDimitry Andric // have a landing pad (which would overwrite the exception slot). 13790b57cec5SDimitry Andric llvm::FunctionType *rethrowFnTy = rethrowFn.getFunctionType(); 13800b57cec5SDimitry Andric SavedExnVar = nullptr; 13810b57cec5SDimitry Andric if (rethrowFnTy->getNumParams()) 13820b57cec5SDimitry Andric SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn"); 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric // A finally block is a statement which must be executed on any edge 13850b57cec5SDimitry Andric // out of a given scope. Unlike a cleanup, the finally block may 13860b57cec5SDimitry Andric // contain arbitrary control flow leading out of itself. In 13870b57cec5SDimitry Andric // addition, finally blocks should always be executed, even if there 13880b57cec5SDimitry Andric // are no catch handlers higher on the stack. Therefore, we 13890b57cec5SDimitry Andric // surround the protected scope with a combination of a normal 13900b57cec5SDimitry Andric // cleanup (to catch attempts to break out of the block via normal 13910b57cec5SDimitry Andric // control flow) and an EH catch-all (semantically "outside" any try 13920b57cec5SDimitry Andric // statement to which the finally block might have been attached). 13930b57cec5SDimitry Andric // The finally block itself is generated in the context of a cleanup 13940b57cec5SDimitry Andric // which conditionally leaves the catch-all. 13950b57cec5SDimitry Andric 13960b57cec5SDimitry Andric // Jump destination for performing the finally block on an exception 13970b57cec5SDimitry Andric // edge. We'll never actually reach this block, so unreachable is 13980b57cec5SDimitry Andric // fine. 13990b57cec5SDimitry Andric RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock()); 14000b57cec5SDimitry Andric 14010b57cec5SDimitry Andric // Whether the finally block is being executed for EH purposes. 14020b57cec5SDimitry Andric ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh"); 14030b57cec5SDimitry Andric CGF.Builder.CreateFlagStore(false, ForEHVar); 14040b57cec5SDimitry Andric 14050b57cec5SDimitry Andric // Enter a normal cleanup which will perform the @finally block. 14060b57cec5SDimitry Andric CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body, 14070b57cec5SDimitry Andric ForEHVar, endCatchFn, 14080b57cec5SDimitry Andric rethrowFn, SavedExnVar); 14090b57cec5SDimitry Andric 14100b57cec5SDimitry Andric // Enter a catch-all scope. 14110b57cec5SDimitry Andric llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall"); 14120b57cec5SDimitry Andric EHCatchScope *catchScope = CGF.EHStack.pushCatch(1); 14130b57cec5SDimitry Andric catchScope->setCatchAllHandler(0, catchBB); 14140b57cec5SDimitry Andric } 14150b57cec5SDimitry Andric 14160b57cec5SDimitry Andric void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) { 14170b57cec5SDimitry Andric // Leave the finally catch-all. 14180b57cec5SDimitry Andric EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin()); 14190b57cec5SDimitry Andric llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block; 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andric CGF.popCatchScope(); 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric // If there are any references to the catch-all block, emit it. 14240b57cec5SDimitry Andric if (catchBB->use_empty()) { 14250b57cec5SDimitry Andric delete catchBB; 14260b57cec5SDimitry Andric } else { 14270b57cec5SDimitry Andric CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP(); 14280b57cec5SDimitry Andric CGF.EmitBlock(catchBB); 14290b57cec5SDimitry Andric 14300b57cec5SDimitry Andric llvm::Value *exn = nullptr; 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric // If there's a begin-catch function, call it. 14330b57cec5SDimitry Andric if (BeginCatchFn) { 14340b57cec5SDimitry Andric exn = CGF.getExceptionFromSlot(); 14350b57cec5SDimitry Andric CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn); 14360b57cec5SDimitry Andric } 14370b57cec5SDimitry Andric 14380b57cec5SDimitry Andric // If we need to remember the exception pointer to rethrow later, do so. 14390b57cec5SDimitry Andric if (SavedExnVar) { 14400b57cec5SDimitry Andric if (!exn) exn = CGF.getExceptionFromSlot(); 14410b57cec5SDimitry Andric CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign()); 14420b57cec5SDimitry Andric } 14430b57cec5SDimitry Andric 14440b57cec5SDimitry Andric // Tell the cleanups in the finally block that we're do this for EH. 14450b57cec5SDimitry Andric CGF.Builder.CreateFlagStore(true, ForEHVar); 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric // Thread a jump through the finally cleanup. 14480b57cec5SDimitry Andric CGF.EmitBranchThroughCleanup(RethrowDest); 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric CGF.Builder.restoreIP(savedIP); 14510b57cec5SDimitry Andric } 14520b57cec5SDimitry Andric 14530b57cec5SDimitry Andric // Finally, leave the @finally cleanup. 14540b57cec5SDimitry Andric CGF.PopCleanupBlock(); 14550b57cec5SDimitry Andric } 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { 14580b57cec5SDimitry Andric if (TerminateLandingPad) 14590b57cec5SDimitry Andric return TerminateLandingPad; 14600b57cec5SDimitry Andric 14610b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 14620b57cec5SDimitry Andric 14630b57cec5SDimitry Andric // This will get inserted at the end of the function. 14640b57cec5SDimitry Andric TerminateLandingPad = createBasicBlock("terminate.lpad"); 14650b57cec5SDimitry Andric Builder.SetInsertPoint(TerminateLandingPad); 14660b57cec5SDimitry Andric 14670b57cec5SDimitry Andric // Tell the backend that this is a landing pad. 14680b57cec5SDimitry Andric const EHPersonality &Personality = EHPersonality::get(*this); 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric if (!CurFn->hasPersonalityFn()) 14710b57cec5SDimitry Andric CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality)); 14720b57cec5SDimitry Andric 14730b57cec5SDimitry Andric llvm::LandingPadInst *LPadInst = 14740b57cec5SDimitry Andric Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0); 14750b57cec5SDimitry Andric LPadInst->addClause(getCatchAllValue(*this)); 14760b57cec5SDimitry Andric 14770b57cec5SDimitry Andric llvm::Value *Exn = nullptr; 14780b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 14790b57cec5SDimitry Andric Exn = Builder.CreateExtractValue(LPadInst, 0); 14800b57cec5SDimitry Andric llvm::CallInst *terminateCall = 14810b57cec5SDimitry Andric CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); 14820b57cec5SDimitry Andric terminateCall->setDoesNotReturn(); 14830b57cec5SDimitry Andric Builder.CreateUnreachable(); 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric // Restore the saved insertion state. 14860b57cec5SDimitry Andric Builder.restoreIP(SavedIP); 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric return TerminateLandingPad; 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric 14910b57cec5SDimitry Andric llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { 14920b57cec5SDimitry Andric if (TerminateHandler) 14930b57cec5SDimitry Andric return TerminateHandler; 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric // Set up the terminate handler. This block is inserted at the very 14960b57cec5SDimitry Andric // end of the function by FinishFunction. 14970b57cec5SDimitry Andric TerminateHandler = createBasicBlock("terminate.handler"); 14980b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 14990b57cec5SDimitry Andric Builder.SetInsertPoint(TerminateHandler); 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric llvm::Value *Exn = nullptr; 15020b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 15030b57cec5SDimitry Andric Exn = getExceptionFromSlot(); 15040b57cec5SDimitry Andric llvm::CallInst *terminateCall = 15050b57cec5SDimitry Andric CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); 15060b57cec5SDimitry Andric terminateCall->setDoesNotReturn(); 15070b57cec5SDimitry Andric Builder.CreateUnreachable(); 15080b57cec5SDimitry Andric 15090b57cec5SDimitry Andric // Restore the saved insertion state. 15100b57cec5SDimitry Andric Builder.restoreIP(SavedIP); 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric return TerminateHandler; 15130b57cec5SDimitry Andric } 15140b57cec5SDimitry Andric 15150b57cec5SDimitry Andric llvm::BasicBlock *CodeGenFunction::getTerminateFunclet() { 15160b57cec5SDimitry Andric assert(EHPersonality::get(*this).usesFuncletPads() && 15170b57cec5SDimitry Andric "use getTerminateLandingPad for non-funclet EH"); 15180b57cec5SDimitry Andric 15190b57cec5SDimitry Andric llvm::BasicBlock *&TerminateFunclet = TerminateFunclets[CurrentFuncletPad]; 15200b57cec5SDimitry Andric if (TerminateFunclet) 15210b57cec5SDimitry Andric return TerminateFunclet; 15220b57cec5SDimitry Andric 15230b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 15240b57cec5SDimitry Andric 15250b57cec5SDimitry Andric // Set up the terminate handler. This block is inserted at the very 15260b57cec5SDimitry Andric // end of the function by FinishFunction. 15270b57cec5SDimitry Andric TerminateFunclet = createBasicBlock("terminate.handler"); 15280b57cec5SDimitry Andric Builder.SetInsertPoint(TerminateFunclet); 15290b57cec5SDimitry Andric 15300b57cec5SDimitry Andric // Create the cleanuppad using the current parent pad as its token. Use 'none' 15310b57cec5SDimitry Andric // if this is a top-level terminate scope, which is the common case. 15320b57cec5SDimitry Andric SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad( 15330b57cec5SDimitry Andric CurrentFuncletPad); 15340b57cec5SDimitry Andric llvm::Value *ParentPad = CurrentFuncletPad; 15350b57cec5SDimitry Andric if (!ParentPad) 15360b57cec5SDimitry Andric ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext()); 15370b57cec5SDimitry Andric CurrentFuncletPad = Builder.CreateCleanupPad(ParentPad); 15380b57cec5SDimitry Andric 15390b57cec5SDimitry Andric // Emit the __std_terminate call. 15400b57cec5SDimitry Andric llvm::Value *Exn = nullptr; 15410b57cec5SDimitry Andric // In case of wasm personality, we need to pass the exception value to 15420b57cec5SDimitry Andric // __clang_call_terminate function. 15430b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 15440b57cec5SDimitry Andric EHPersonality::get(*this).isWasmPersonality()) { 15450b57cec5SDimitry Andric llvm::Function *GetExnFn = 15460b57cec5SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception); 15470b57cec5SDimitry Andric Exn = Builder.CreateCall(GetExnFn, CurrentFuncletPad); 15480b57cec5SDimitry Andric } 15490b57cec5SDimitry Andric llvm::CallInst *terminateCall = 15500b57cec5SDimitry Andric CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn); 15510b57cec5SDimitry Andric terminateCall->setDoesNotReturn(); 15520b57cec5SDimitry Andric Builder.CreateUnreachable(); 15530b57cec5SDimitry Andric 15540b57cec5SDimitry Andric // Restore the saved insertion state. 15550b57cec5SDimitry Andric Builder.restoreIP(SavedIP); 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric return TerminateFunclet; 15580b57cec5SDimitry Andric } 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) { 15610b57cec5SDimitry Andric if (EHResumeBlock) return EHResumeBlock; 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); 15640b57cec5SDimitry Andric 15650b57cec5SDimitry Andric // We emit a jump to a notional label at the outermost unwind state. 15660b57cec5SDimitry Andric EHResumeBlock = createBasicBlock("eh.resume"); 15670b57cec5SDimitry Andric Builder.SetInsertPoint(EHResumeBlock); 15680b57cec5SDimitry Andric 15690b57cec5SDimitry Andric const EHPersonality &Personality = EHPersonality::get(*this); 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric // This can always be a call because we necessarily didn't find 15720b57cec5SDimitry Andric // anything on the EH stack which needs our help. 15730b57cec5SDimitry Andric const char *RethrowName = Personality.CatchallRethrowFn; 15740b57cec5SDimitry Andric if (RethrowName != nullptr && !isCleanup) { 15750b57cec5SDimitry Andric EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName), 15760b57cec5SDimitry Andric getExceptionFromSlot())->setDoesNotReturn(); 15770b57cec5SDimitry Andric Builder.CreateUnreachable(); 15780b57cec5SDimitry Andric Builder.restoreIP(SavedIP); 15790b57cec5SDimitry Andric return EHResumeBlock; 15800b57cec5SDimitry Andric } 15810b57cec5SDimitry Andric 15820b57cec5SDimitry Andric // Recreate the landingpad's return value for the 'resume' instruction. 15830b57cec5SDimitry Andric llvm::Value *Exn = getExceptionFromSlot(); 15840b57cec5SDimitry Andric llvm::Value *Sel = getSelectorFromSlot(); 15850b57cec5SDimitry Andric 15860b57cec5SDimitry Andric llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), Sel->getType()); 15870b57cec5SDimitry Andric llvm::Value *LPadVal = llvm::UndefValue::get(LPadType); 15880b57cec5SDimitry Andric LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val"); 15890b57cec5SDimitry Andric LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); 15900b57cec5SDimitry Andric 15910b57cec5SDimitry Andric Builder.CreateResume(LPadVal); 15920b57cec5SDimitry Andric Builder.restoreIP(SavedIP); 15930b57cec5SDimitry Andric return EHResumeBlock; 15940b57cec5SDimitry Andric } 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andric void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) { 15970b57cec5SDimitry Andric EnterSEHTryStmt(S); 15980b57cec5SDimitry Andric { 15990b57cec5SDimitry Andric JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave"); 16000b57cec5SDimitry Andric 16010b57cec5SDimitry Andric SEHTryEpilogueStack.push_back(&TryExit); 16020b57cec5SDimitry Andric EmitStmt(S.getTryBlock()); 16030b57cec5SDimitry Andric SEHTryEpilogueStack.pop_back(); 16040b57cec5SDimitry Andric 16050b57cec5SDimitry Andric if (!TryExit.getBlock()->use_empty()) 16060b57cec5SDimitry Andric EmitBlock(TryExit.getBlock(), /*IsFinished=*/true); 16070b57cec5SDimitry Andric else 16080b57cec5SDimitry Andric delete TryExit.getBlock(); 16090b57cec5SDimitry Andric } 16100b57cec5SDimitry Andric ExitSEHTryStmt(S); 16110b57cec5SDimitry Andric } 16120b57cec5SDimitry Andric 16130b57cec5SDimitry Andric namespace { 16140b57cec5SDimitry Andric struct PerformSEHFinally final : EHScopeStack::Cleanup { 16150b57cec5SDimitry Andric llvm::Function *OutlinedFinally; 16160b57cec5SDimitry Andric PerformSEHFinally(llvm::Function *OutlinedFinally) 16170b57cec5SDimitry Andric : OutlinedFinally(OutlinedFinally) {} 16180b57cec5SDimitry Andric 16190b57cec5SDimitry Andric void Emit(CodeGenFunction &CGF, Flags F) override { 16200b57cec5SDimitry Andric ASTContext &Context = CGF.getContext(); 16210b57cec5SDimitry Andric CodeGenModule &CGM = CGF.CGM; 16220b57cec5SDimitry Andric 16230b57cec5SDimitry Andric CallArgList Args; 16240b57cec5SDimitry Andric 16250b57cec5SDimitry Andric // Compute the two argument values. 16260b57cec5SDimitry Andric QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy}; 16270b57cec5SDimitry Andric llvm::Value *FP = nullptr; 16280b57cec5SDimitry Andric // If CFG.IsOutlinedSEHHelper is true, then we are within a finally block. 16290b57cec5SDimitry Andric if (CGF.IsOutlinedSEHHelper) { 16300b57cec5SDimitry Andric FP = &CGF.CurFn->arg_begin()[1]; 16310b57cec5SDimitry Andric } else { 16320b57cec5SDimitry Andric llvm::Function *LocalAddrFn = 16330b57cec5SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::localaddress); 16340b57cec5SDimitry Andric FP = CGF.Builder.CreateCall(LocalAddrFn); 16350b57cec5SDimitry Andric } 16360b57cec5SDimitry Andric 16370b57cec5SDimitry Andric llvm::Value *IsForEH = 16380b57cec5SDimitry Andric llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup()); 16390b57cec5SDimitry Andric Args.add(RValue::get(IsForEH), ArgTys[0]); 16400b57cec5SDimitry Andric Args.add(RValue::get(FP), ArgTys[1]); 16410b57cec5SDimitry Andric 16420b57cec5SDimitry Andric // Arrange a two-arg function info and type. 16430b57cec5SDimitry Andric const CGFunctionInfo &FnInfo = 16440b57cec5SDimitry Andric CGM.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, Args); 16450b57cec5SDimitry Andric 16460b57cec5SDimitry Andric auto Callee = CGCallee::forDirect(OutlinedFinally); 16470b57cec5SDimitry Andric CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args); 16480b57cec5SDimitry Andric } 16490b57cec5SDimitry Andric }; 16500b57cec5SDimitry Andric } // end anonymous namespace 16510b57cec5SDimitry Andric 16520b57cec5SDimitry Andric namespace { 16530b57cec5SDimitry Andric /// Find all local variable captures in the statement. 16540b57cec5SDimitry Andric struct CaptureFinder : ConstStmtVisitor<CaptureFinder> { 16550b57cec5SDimitry Andric CodeGenFunction &ParentCGF; 16560b57cec5SDimitry Andric const VarDecl *ParentThis; 16570b57cec5SDimitry Andric llvm::SmallSetVector<const VarDecl *, 4> Captures; 16580b57cec5SDimitry Andric Address SEHCodeSlot = Address::invalid(); 16590b57cec5SDimitry Andric CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis) 16600b57cec5SDimitry Andric : ParentCGF(ParentCGF), ParentThis(ParentThis) {} 16610b57cec5SDimitry Andric 16620b57cec5SDimitry Andric // Return true if we need to do any capturing work. 16630b57cec5SDimitry Andric bool foundCaptures() { 16640b57cec5SDimitry Andric return !Captures.empty() || SEHCodeSlot.isValid(); 16650b57cec5SDimitry Andric } 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric void Visit(const Stmt *S) { 16680b57cec5SDimitry Andric // See if this is a capture, then recurse. 16690b57cec5SDimitry Andric ConstStmtVisitor<CaptureFinder>::Visit(S); 16700b57cec5SDimitry Andric for (const Stmt *Child : S->children()) 16710b57cec5SDimitry Andric if (Child) 16720b57cec5SDimitry Andric Visit(Child); 16730b57cec5SDimitry Andric } 16740b57cec5SDimitry Andric 16750b57cec5SDimitry Andric void VisitDeclRefExpr(const DeclRefExpr *E) { 16760b57cec5SDimitry Andric // If this is already a capture, just make sure we capture 'this'. 16770b57cec5SDimitry Andric if (E->refersToEnclosingVariableOrCapture()) { 16780b57cec5SDimitry Andric Captures.insert(ParentThis); 16790b57cec5SDimitry Andric return; 16800b57cec5SDimitry Andric } 16810b57cec5SDimitry Andric 16820b57cec5SDimitry Andric const auto *D = dyn_cast<VarDecl>(E->getDecl()); 16830b57cec5SDimitry Andric if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage()) 16840b57cec5SDimitry Andric Captures.insert(D); 16850b57cec5SDimitry Andric } 16860b57cec5SDimitry Andric 16870b57cec5SDimitry Andric void VisitCXXThisExpr(const CXXThisExpr *E) { 16880b57cec5SDimitry Andric Captures.insert(ParentThis); 16890b57cec5SDimitry Andric } 16900b57cec5SDimitry Andric 16910b57cec5SDimitry Andric void VisitCallExpr(const CallExpr *E) { 16920b57cec5SDimitry Andric // We only need to add parent frame allocations for these builtins in x86. 16930b57cec5SDimitry Andric if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86) 16940b57cec5SDimitry Andric return; 16950b57cec5SDimitry Andric 16960b57cec5SDimitry Andric unsigned ID = E->getBuiltinCallee(); 16970b57cec5SDimitry Andric switch (ID) { 16980b57cec5SDimitry Andric case Builtin::BI__exception_code: 16990b57cec5SDimitry Andric case Builtin::BI_exception_code: 17000b57cec5SDimitry Andric // This is the simple case where we are the outermost finally. All we 17010b57cec5SDimitry Andric // have to do here is make sure we escape this and recover it in the 17020b57cec5SDimitry Andric // outlined handler. 17030b57cec5SDimitry Andric if (!SEHCodeSlot.isValid()) 17040b57cec5SDimitry Andric SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back(); 17050b57cec5SDimitry Andric break; 17060b57cec5SDimitry Andric } 17070b57cec5SDimitry Andric } 17080b57cec5SDimitry Andric }; 17090b57cec5SDimitry Andric } // end anonymous namespace 17100b57cec5SDimitry Andric 17110b57cec5SDimitry Andric Address CodeGenFunction::recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, 17120b57cec5SDimitry Andric Address ParentVar, 17130b57cec5SDimitry Andric llvm::Value *ParentFP) { 17140b57cec5SDimitry Andric llvm::CallInst *RecoverCall = nullptr; 17150b57cec5SDimitry Andric CGBuilderTy Builder(*this, AllocaInsertPt); 17160b57cec5SDimitry Andric if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) { 17170b57cec5SDimitry Andric // Mark the variable escaped if nobody else referenced it and compute the 17180b57cec5SDimitry Andric // localescape index. 17190b57cec5SDimitry Andric auto InsertPair = ParentCGF.EscapedLocals.insert( 17200b57cec5SDimitry Andric std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size())); 17210b57cec5SDimitry Andric int FrameEscapeIdx = InsertPair.first->second; 17220b57cec5SDimitry Andric // call i8* @llvm.localrecover(i8* bitcast(@parentFn), i8* %fp, i32 N) 17230b57cec5SDimitry Andric llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration( 17240b57cec5SDimitry Andric &CGM.getModule(), llvm::Intrinsic::localrecover); 17250b57cec5SDimitry Andric llvm::Constant *ParentI8Fn = 17260b57cec5SDimitry Andric llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); 17270b57cec5SDimitry Andric RecoverCall = Builder.CreateCall( 17280b57cec5SDimitry Andric FrameRecoverFn, {ParentI8Fn, ParentFP, 17290b57cec5SDimitry Andric llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)}); 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric } else { 17320b57cec5SDimitry Andric // If the parent didn't have an alloca, we're doing some nested outlining. 17330b57cec5SDimitry Andric // Just clone the existing localrecover call, but tweak the FP argument to 17340b57cec5SDimitry Andric // use our FP value. All other arguments are constants. 17350b57cec5SDimitry Andric auto *ParentRecover = 17360b57cec5SDimitry Andric cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts()); 17370b57cec5SDimitry Andric assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover && 17380b57cec5SDimitry Andric "expected alloca or localrecover in parent LocalDeclMap"); 17390b57cec5SDimitry Andric RecoverCall = cast<llvm::CallInst>(ParentRecover->clone()); 17400b57cec5SDimitry Andric RecoverCall->setArgOperand(1, ParentFP); 17410b57cec5SDimitry Andric RecoverCall->insertBefore(AllocaInsertPt); 17420b57cec5SDimitry Andric } 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric // Bitcast the variable, rename it, and insert it in the local decl map. 17450b57cec5SDimitry Andric llvm::Value *ChildVar = 17460b57cec5SDimitry Andric Builder.CreateBitCast(RecoverCall, ParentVar.getType()); 17470b57cec5SDimitry Andric ChildVar->setName(ParentVar.getName()); 17480b57cec5SDimitry Andric return Address(ChildVar, ParentVar.getAlignment()); 17490b57cec5SDimitry Andric } 17500b57cec5SDimitry Andric 17510b57cec5SDimitry Andric void CodeGenFunction::EmitCapturedLocals(CodeGenFunction &ParentCGF, 17520b57cec5SDimitry Andric const Stmt *OutlinedStmt, 17530b57cec5SDimitry Andric bool IsFilter) { 17540b57cec5SDimitry Andric // Find all captures in the Stmt. 17550b57cec5SDimitry Andric CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl); 17560b57cec5SDimitry Andric Finder.Visit(OutlinedStmt); 17570b57cec5SDimitry Andric 17580b57cec5SDimitry Andric // We can exit early on x86_64 when there are no captures. We just have to 17590b57cec5SDimitry Andric // save the exception code in filters so that __exception_code() works. 17600b57cec5SDimitry Andric if (!Finder.foundCaptures() && 17610b57cec5SDimitry Andric CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { 17620b57cec5SDimitry Andric if (IsFilter) 17630b57cec5SDimitry Andric EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr); 17640b57cec5SDimitry Andric return; 17650b57cec5SDimitry Andric } 17660b57cec5SDimitry Andric 17670b57cec5SDimitry Andric llvm::Value *EntryFP = nullptr; 17680b57cec5SDimitry Andric CGBuilderTy Builder(CGM, AllocaInsertPt); 17690b57cec5SDimitry Andric if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) { 17700b57cec5SDimitry Andric // 32-bit SEH filters need to be careful about FP recovery. The end of the 17710b57cec5SDimitry Andric // EH registration is passed in as the EBP physical register. We can 17720b57cec5SDimitry Andric // recover that with llvm.frameaddress(1). 17730b57cec5SDimitry Andric EntryFP = Builder.CreateCall( 1774*a7dea167SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::frameaddress, AllocaInt8PtrTy), 1775*a7dea167SDimitry Andric {Builder.getInt32(1)}); 17760b57cec5SDimitry Andric } else { 17770b57cec5SDimitry Andric // Otherwise, for x64 and 32-bit finally functions, the parent FP is the 17780b57cec5SDimitry Andric // second parameter. 17790b57cec5SDimitry Andric auto AI = CurFn->arg_begin(); 17800b57cec5SDimitry Andric ++AI; 17810b57cec5SDimitry Andric EntryFP = &*AI; 17820b57cec5SDimitry Andric } 17830b57cec5SDimitry Andric 17840b57cec5SDimitry Andric llvm::Value *ParentFP = EntryFP; 17850b57cec5SDimitry Andric if (IsFilter) { 17860b57cec5SDimitry Andric // Given whatever FP the runtime provided us in EntryFP, recover the true 17870b57cec5SDimitry Andric // frame pointer of the parent function. We only need to do this in filters, 17880b57cec5SDimitry Andric // since finally funclets recover the parent FP for us. 17890b57cec5SDimitry Andric llvm::Function *RecoverFPIntrin = 17900b57cec5SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::eh_recoverfp); 17910b57cec5SDimitry Andric llvm::Constant *ParentI8Fn = 17920b57cec5SDimitry Andric llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy); 17930b57cec5SDimitry Andric ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP}); 17940b57cec5SDimitry Andric } 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric // Create llvm.localrecover calls for all captures. 17970b57cec5SDimitry Andric for (const VarDecl *VD : Finder.Captures) { 17980b57cec5SDimitry Andric if (isa<ImplicitParamDecl>(VD)) { 17990b57cec5SDimitry Andric CGM.ErrorUnsupported(VD, "'this' captured by SEH"); 18000b57cec5SDimitry Andric CXXThisValue = llvm::UndefValue::get(ConvertTypeForMem(VD->getType())); 18010b57cec5SDimitry Andric continue; 18020b57cec5SDimitry Andric } 18030b57cec5SDimitry Andric if (VD->getType()->isVariablyModifiedType()) { 18040b57cec5SDimitry Andric CGM.ErrorUnsupported(VD, "VLA captured by SEH"); 18050b57cec5SDimitry Andric continue; 18060b57cec5SDimitry Andric } 18070b57cec5SDimitry Andric assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) && 18080b57cec5SDimitry Andric "captured non-local variable"); 18090b57cec5SDimitry Andric 18100b57cec5SDimitry Andric // If this decl hasn't been declared yet, it will be declared in the 18110b57cec5SDimitry Andric // OutlinedStmt. 18120b57cec5SDimitry Andric auto I = ParentCGF.LocalDeclMap.find(VD); 18130b57cec5SDimitry Andric if (I == ParentCGF.LocalDeclMap.end()) 18140b57cec5SDimitry Andric continue; 18150b57cec5SDimitry Andric 18160b57cec5SDimitry Andric Address ParentVar = I->second; 18170b57cec5SDimitry Andric setAddrOfLocalVar( 18180b57cec5SDimitry Andric VD, recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP)); 18190b57cec5SDimitry Andric } 18200b57cec5SDimitry Andric 18210b57cec5SDimitry Andric if (Finder.SEHCodeSlot.isValid()) { 18220b57cec5SDimitry Andric SEHCodeSlotStack.push_back( 18230b57cec5SDimitry Andric recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP)); 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric 18260b57cec5SDimitry Andric if (IsFilter) 18270b57cec5SDimitry Andric EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP); 18280b57cec5SDimitry Andric } 18290b57cec5SDimitry Andric 18300b57cec5SDimitry Andric /// Arrange a function prototype that can be called by Windows exception 18310b57cec5SDimitry Andric /// handling personalities. On Win64, the prototype looks like: 18320b57cec5SDimitry Andric /// RetTy func(void *EHPtrs, void *ParentFP); 18330b57cec5SDimitry Andric void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF, 18340b57cec5SDimitry Andric bool IsFilter, 18350b57cec5SDimitry Andric const Stmt *OutlinedStmt) { 18360b57cec5SDimitry Andric SourceLocation StartLoc = OutlinedStmt->getBeginLoc(); 18370b57cec5SDimitry Andric 18380b57cec5SDimitry Andric // Get the mangled function name. 18390b57cec5SDimitry Andric SmallString<128> Name; 18400b57cec5SDimitry Andric { 18410b57cec5SDimitry Andric llvm::raw_svector_ostream OS(Name); 18420b57cec5SDimitry Andric const NamedDecl *ParentSEHFn = ParentCGF.CurSEHParent; 18430b57cec5SDimitry Andric assert(ParentSEHFn && "No CurSEHParent!"); 18440b57cec5SDimitry Andric MangleContext &Mangler = CGM.getCXXABI().getMangleContext(); 18450b57cec5SDimitry Andric if (IsFilter) 18460b57cec5SDimitry Andric Mangler.mangleSEHFilterExpression(ParentSEHFn, OS); 18470b57cec5SDimitry Andric else 18480b57cec5SDimitry Andric Mangler.mangleSEHFinallyBlock(ParentSEHFn, OS); 18490b57cec5SDimitry Andric } 18500b57cec5SDimitry Andric 18510b57cec5SDimitry Andric FunctionArgList Args; 18520b57cec5SDimitry Andric if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) { 18530b57cec5SDimitry Andric // All SEH finally functions take two parameters. Win64 filters take two 18540b57cec5SDimitry Andric // parameters. Win32 filters take no parameters. 18550b57cec5SDimitry Andric if (IsFilter) { 18560b57cec5SDimitry Andric Args.push_back(ImplicitParamDecl::Create( 18570b57cec5SDimitry Andric getContext(), /*DC=*/nullptr, StartLoc, 18580b57cec5SDimitry Andric &getContext().Idents.get("exception_pointers"), 18590b57cec5SDimitry Andric getContext().VoidPtrTy, ImplicitParamDecl::Other)); 18600b57cec5SDimitry Andric } else { 18610b57cec5SDimitry Andric Args.push_back(ImplicitParamDecl::Create( 18620b57cec5SDimitry Andric getContext(), /*DC=*/nullptr, StartLoc, 18630b57cec5SDimitry Andric &getContext().Idents.get("abnormal_termination"), 18640b57cec5SDimitry Andric getContext().UnsignedCharTy, ImplicitParamDecl::Other)); 18650b57cec5SDimitry Andric } 18660b57cec5SDimitry Andric Args.push_back(ImplicitParamDecl::Create( 18670b57cec5SDimitry Andric getContext(), /*DC=*/nullptr, StartLoc, 18680b57cec5SDimitry Andric &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy, 18690b57cec5SDimitry Andric ImplicitParamDecl::Other)); 18700b57cec5SDimitry Andric } 18710b57cec5SDimitry Andric 18720b57cec5SDimitry Andric QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy; 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric const CGFunctionInfo &FnInfo = 18750b57cec5SDimitry Andric CGM.getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args); 18760b57cec5SDimitry Andric 18770b57cec5SDimitry Andric llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); 18780b57cec5SDimitry Andric llvm::Function *Fn = llvm::Function::Create( 18790b57cec5SDimitry Andric FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule()); 18800b57cec5SDimitry Andric 18810b57cec5SDimitry Andric IsOutlinedSEHHelper = true; 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, 18840b57cec5SDimitry Andric OutlinedStmt->getBeginLoc(), OutlinedStmt->getBeginLoc()); 18850b57cec5SDimitry Andric CurSEHParent = ParentCGF.CurSEHParent; 18860b57cec5SDimitry Andric 18870b57cec5SDimitry Andric CGM.SetLLVMFunctionAttributes(GlobalDecl(), FnInfo, CurFn); 18880b57cec5SDimitry Andric EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter); 18890b57cec5SDimitry Andric } 18900b57cec5SDimitry Andric 18910b57cec5SDimitry Andric /// Create a stub filter function that will ultimately hold the code of the 18920b57cec5SDimitry Andric /// filter expression. The EH preparation passes in LLVM will outline the code 18930b57cec5SDimitry Andric /// from the main function body into this stub. 18940b57cec5SDimitry Andric llvm::Function * 18950b57cec5SDimitry Andric CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, 18960b57cec5SDimitry Andric const SEHExceptStmt &Except) { 18970b57cec5SDimitry Andric const Expr *FilterExpr = Except.getFilterExpr(); 18980b57cec5SDimitry Andric startOutlinedSEHHelper(ParentCGF, true, FilterExpr); 18990b57cec5SDimitry Andric 19000b57cec5SDimitry Andric // Emit the original filter expression, convert to i32, and return. 19010b57cec5SDimitry Andric llvm::Value *R = EmitScalarExpr(FilterExpr); 19020b57cec5SDimitry Andric R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy), 19030b57cec5SDimitry Andric FilterExpr->getType()->isSignedIntegerType()); 19040b57cec5SDimitry Andric Builder.CreateStore(R, ReturnValue); 19050b57cec5SDimitry Andric 19060b57cec5SDimitry Andric FinishFunction(FilterExpr->getEndLoc()); 19070b57cec5SDimitry Andric 19080b57cec5SDimitry Andric return CurFn; 19090b57cec5SDimitry Andric } 19100b57cec5SDimitry Andric 19110b57cec5SDimitry Andric llvm::Function * 19120b57cec5SDimitry Andric CodeGenFunction::GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, 19130b57cec5SDimitry Andric const SEHFinallyStmt &Finally) { 19140b57cec5SDimitry Andric const Stmt *FinallyBlock = Finally.getBlock(); 19150b57cec5SDimitry Andric startOutlinedSEHHelper(ParentCGF, false, FinallyBlock); 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric // Emit the original filter expression, convert to i32, and return. 19180b57cec5SDimitry Andric EmitStmt(FinallyBlock); 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric FinishFunction(FinallyBlock->getEndLoc()); 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric return CurFn; 19230b57cec5SDimitry Andric } 19240b57cec5SDimitry Andric 19250b57cec5SDimitry Andric void CodeGenFunction::EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, 19260b57cec5SDimitry Andric llvm::Value *ParentFP, 19270b57cec5SDimitry Andric llvm::Value *EntryFP) { 19280b57cec5SDimitry Andric // Get the pointer to the EXCEPTION_POINTERS struct. This is returned by the 19290b57cec5SDimitry Andric // __exception_info intrinsic. 19300b57cec5SDimitry Andric if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { 19310b57cec5SDimitry Andric // On Win64, the info is passed as the first parameter to the filter. 19320b57cec5SDimitry Andric SEHInfo = &*CurFn->arg_begin(); 19330b57cec5SDimitry Andric SEHCodeSlotStack.push_back( 19340b57cec5SDimitry Andric CreateMemTemp(getContext().IntTy, "__exception_code")); 19350b57cec5SDimitry Andric } else { 19360b57cec5SDimitry Andric // On Win32, the EBP on entry to the filter points to the end of an 19370b57cec5SDimitry Andric // exception registration object. It contains 6 32-bit fields, and the info 19380b57cec5SDimitry Andric // pointer is stored in the second field. So, GEP 20 bytes backwards and 19390b57cec5SDimitry Andric // load the pointer. 19400b57cec5SDimitry Andric SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20); 19410b57cec5SDimitry Andric SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo()); 19420b57cec5SDimitry Andric SEHInfo = Builder.CreateAlignedLoad(Int8PtrTy, SEHInfo, getPointerAlign()); 19430b57cec5SDimitry Andric SEHCodeSlotStack.push_back(recoverAddrOfEscapedLocal( 19440b57cec5SDimitry Andric ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP)); 19450b57cec5SDimitry Andric } 19460b57cec5SDimitry Andric 19470b57cec5SDimitry Andric // Save the exception code in the exception slot to unify exception access in 19480b57cec5SDimitry Andric // the filter function and the landing pad. 19490b57cec5SDimitry Andric // struct EXCEPTION_POINTERS { 19500b57cec5SDimitry Andric // EXCEPTION_RECORD *ExceptionRecord; 19510b57cec5SDimitry Andric // CONTEXT *ContextRecord; 19520b57cec5SDimitry Andric // }; 19530b57cec5SDimitry Andric // int exceptioncode = exception_pointers->ExceptionRecord->ExceptionCode; 19540b57cec5SDimitry Andric llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo(); 19550b57cec5SDimitry Andric llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy); 19560b57cec5SDimitry Andric llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo()); 19570b57cec5SDimitry Andric llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0); 19580b57cec5SDimitry Andric Rec = Builder.CreateAlignedLoad(Rec, getPointerAlign()); 19590b57cec5SDimitry Andric llvm::Value *Code = Builder.CreateAlignedLoad(Rec, getIntAlign()); 19600b57cec5SDimitry Andric assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); 19610b57cec5SDimitry Andric Builder.CreateStore(Code, SEHCodeSlotStack.back()); 19620b57cec5SDimitry Andric } 19630b57cec5SDimitry Andric 19640b57cec5SDimitry Andric llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() { 19650b57cec5SDimitry Andric // Sema should diagnose calling this builtin outside of a filter context, but 19660b57cec5SDimitry Andric // don't crash if we screw up. 19670b57cec5SDimitry Andric if (!SEHInfo) 19680b57cec5SDimitry Andric return llvm::UndefValue::get(Int8PtrTy); 19690b57cec5SDimitry Andric assert(SEHInfo->getType() == Int8PtrTy); 19700b57cec5SDimitry Andric return SEHInfo; 19710b57cec5SDimitry Andric } 19720b57cec5SDimitry Andric 19730b57cec5SDimitry Andric llvm::Value *CodeGenFunction::EmitSEHExceptionCode() { 19740b57cec5SDimitry Andric assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except"); 19750b57cec5SDimitry Andric return Builder.CreateLoad(SEHCodeSlotStack.back()); 19760b57cec5SDimitry Andric } 19770b57cec5SDimitry Andric 19780b57cec5SDimitry Andric llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() { 19790b57cec5SDimitry Andric // Abnormal termination is just the first parameter to the outlined finally 19800b57cec5SDimitry Andric // helper. 19810b57cec5SDimitry Andric auto AI = CurFn->arg_begin(); 19820b57cec5SDimitry Andric return Builder.CreateZExt(&*AI, Int32Ty); 19830b57cec5SDimitry Andric } 19840b57cec5SDimitry Andric 19850b57cec5SDimitry Andric void CodeGenFunction::pushSEHCleanup(CleanupKind Kind, 19860b57cec5SDimitry Andric llvm::Function *FinallyFunc) { 19870b57cec5SDimitry Andric EHStack.pushCleanup<PerformSEHFinally>(Kind, FinallyFunc); 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric 19900b57cec5SDimitry Andric void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) { 19910b57cec5SDimitry Andric CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true); 19920b57cec5SDimitry Andric if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) { 19930b57cec5SDimitry Andric // Outline the finally block. 19940b57cec5SDimitry Andric llvm::Function *FinallyFunc = 19950b57cec5SDimitry Andric HelperCGF.GenerateSEHFinallyFunction(*this, *Finally); 19960b57cec5SDimitry Andric 19970b57cec5SDimitry Andric // Push a cleanup for __finally blocks. 19980b57cec5SDimitry Andric EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc); 19990b57cec5SDimitry Andric return; 20000b57cec5SDimitry Andric } 20010b57cec5SDimitry Andric 20020b57cec5SDimitry Andric // Otherwise, we must have an __except block. 20030b57cec5SDimitry Andric const SEHExceptStmt *Except = S.getExceptHandler(); 20040b57cec5SDimitry Andric assert(Except); 20050b57cec5SDimitry Andric EHCatchScope *CatchScope = EHStack.pushCatch(1); 20060b57cec5SDimitry Andric SEHCodeSlotStack.push_back( 20070b57cec5SDimitry Andric CreateMemTemp(getContext().IntTy, "__exception_code")); 20080b57cec5SDimitry Andric 20090b57cec5SDimitry Andric // If the filter is known to evaluate to 1, then we can use the clause 20100b57cec5SDimitry Andric // "catch i8* null". We can't do this on x86 because the filter has to save 20110b57cec5SDimitry Andric // the exception code. 20120b57cec5SDimitry Andric llvm::Constant *C = 20130b57cec5SDimitry Andric ConstantEmitter(*this).tryEmitAbstract(Except->getFilterExpr(), 20140b57cec5SDimitry Andric getContext().IntTy); 20150b57cec5SDimitry Andric if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C && 20160b57cec5SDimitry Andric C->isOneValue()) { 20170b57cec5SDimitry Andric CatchScope->setCatchAllHandler(0, createBasicBlock("__except")); 20180b57cec5SDimitry Andric return; 20190b57cec5SDimitry Andric } 20200b57cec5SDimitry Andric 20210b57cec5SDimitry Andric // In general, we have to emit an outlined filter function. Use the function 20220b57cec5SDimitry Andric // in place of the RTTI typeinfo global that C++ EH uses. 20230b57cec5SDimitry Andric llvm::Function *FilterFunc = 20240b57cec5SDimitry Andric HelperCGF.GenerateSEHFilterFunction(*this, *Except); 20250b57cec5SDimitry Andric llvm::Constant *OpaqueFunc = 20260b57cec5SDimitry Andric llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy); 20270b57cec5SDimitry Andric CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret")); 20280b57cec5SDimitry Andric } 20290b57cec5SDimitry Andric 20300b57cec5SDimitry Andric void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) { 20310b57cec5SDimitry Andric // Just pop the cleanup if it's a __finally block. 20320b57cec5SDimitry Andric if (S.getFinallyHandler()) { 20330b57cec5SDimitry Andric PopCleanupBlock(); 20340b57cec5SDimitry Andric return; 20350b57cec5SDimitry Andric } 20360b57cec5SDimitry Andric 20370b57cec5SDimitry Andric // Otherwise, we must have an __except block. 20380b57cec5SDimitry Andric const SEHExceptStmt *Except = S.getExceptHandler(); 20390b57cec5SDimitry Andric assert(Except && "__try must have __finally xor __except"); 20400b57cec5SDimitry Andric EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); 20410b57cec5SDimitry Andric 20420b57cec5SDimitry Andric // Don't emit the __except block if the __try block lacked invokes. 20430b57cec5SDimitry Andric // TODO: Model unwind edges from instructions, either with iload / istore or 20440b57cec5SDimitry Andric // a try body function. 20450b57cec5SDimitry Andric if (!CatchScope.hasEHBranches()) { 20460b57cec5SDimitry Andric CatchScope.clearHandlerBlocks(); 20470b57cec5SDimitry Andric EHStack.popCatch(); 20480b57cec5SDimitry Andric SEHCodeSlotStack.pop_back(); 20490b57cec5SDimitry Andric return; 20500b57cec5SDimitry Andric } 20510b57cec5SDimitry Andric 20520b57cec5SDimitry Andric // The fall-through block. 20530b57cec5SDimitry Andric llvm::BasicBlock *ContBB = createBasicBlock("__try.cont"); 20540b57cec5SDimitry Andric 20550b57cec5SDimitry Andric // We just emitted the body of the __try; jump to the continue block. 20560b57cec5SDimitry Andric if (HaveInsertPoint()) 20570b57cec5SDimitry Andric Builder.CreateBr(ContBB); 20580b57cec5SDimitry Andric 20590b57cec5SDimitry Andric // Check if our filter function returned true. 20600b57cec5SDimitry Andric emitCatchDispatchBlock(*this, CatchScope); 20610b57cec5SDimitry Andric 20620b57cec5SDimitry Andric // Grab the block before we pop the handler. 20630b57cec5SDimitry Andric llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block; 20640b57cec5SDimitry Andric EHStack.popCatch(); 20650b57cec5SDimitry Andric 20660b57cec5SDimitry Andric EmitBlockAfterUses(CatchPadBB); 20670b57cec5SDimitry Andric 20680b57cec5SDimitry Andric // __except blocks don't get outlined into funclets, so immediately do a 20690b57cec5SDimitry Andric // catchret. 20700b57cec5SDimitry Andric llvm::CatchPadInst *CPI = 20710b57cec5SDimitry Andric cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI()); 20720b57cec5SDimitry Andric llvm::BasicBlock *ExceptBB = createBasicBlock("__except"); 20730b57cec5SDimitry Andric Builder.CreateCatchRet(CPI, ExceptBB); 20740b57cec5SDimitry Andric EmitBlock(ExceptBB); 20750b57cec5SDimitry Andric 20760b57cec5SDimitry Andric // On Win64, the exception code is returned in EAX. Copy it into the slot. 20770b57cec5SDimitry Andric if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) { 20780b57cec5SDimitry Andric llvm::Function *SEHCodeIntrin = 20790b57cec5SDimitry Andric CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode); 20800b57cec5SDimitry Andric llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI}); 20810b57cec5SDimitry Andric Builder.CreateStore(Code, SEHCodeSlotStack.back()); 20820b57cec5SDimitry Andric } 20830b57cec5SDimitry Andric 20840b57cec5SDimitry Andric // Emit the __except body. 20850b57cec5SDimitry Andric EmitStmt(Except->getBlock()); 20860b57cec5SDimitry Andric 20870b57cec5SDimitry Andric // End the lifetime of the exception code. 20880b57cec5SDimitry Andric SEHCodeSlotStack.pop_back(); 20890b57cec5SDimitry Andric 20900b57cec5SDimitry Andric if (HaveInsertPoint()) 20910b57cec5SDimitry Andric Builder.CreateBr(ContBB); 20920b57cec5SDimitry Andric 20930b57cec5SDimitry Andric EmitBlock(ContBB); 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric 20960b57cec5SDimitry Andric void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) { 20970b57cec5SDimitry Andric // If this code is reachable then emit a stop point (if generating 20980b57cec5SDimitry Andric // debug info). We have to do this ourselves because we are on the 20990b57cec5SDimitry Andric // "simple" statement path. 21000b57cec5SDimitry Andric if (HaveInsertPoint()) 21010b57cec5SDimitry Andric EmitStopPoint(&S); 21020b57cec5SDimitry Andric 21030b57cec5SDimitry Andric // This must be a __leave from a __finally block, which we warn on and is UB. 21040b57cec5SDimitry Andric // Just emit unreachable. 21050b57cec5SDimitry Andric if (!isSEHTryScope()) { 21060b57cec5SDimitry Andric Builder.CreateUnreachable(); 21070b57cec5SDimitry Andric Builder.ClearInsertionPoint(); 21080b57cec5SDimitry Andric return; 21090b57cec5SDimitry Andric } 21100b57cec5SDimitry Andric 21110b57cec5SDimitry Andric EmitBranchThroughCleanup(*SEHTryEpilogueStack.back()); 21120b57cec5SDimitry Andric } 2113