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