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