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