1 //===--- CGException.cpp - Emit LLVM Code for C++ exceptions ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code dealing with C++ exception related code generation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenFunction.h"
14 #include "CGCXXABI.h"
15 #include "CGCleanup.h"
16 #include "CGObjCRuntime.h"
17 #include "ConstantEmitter.h"
18 #include "TargetInfo.h"
19 #include "clang/AST/Mangle.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/AST/StmtObjC.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/TargetBuiltins.h"
24 #include "llvm/IR/Intrinsics.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/Support/SaveAndRestore.h"
27 
28 using namespace clang;
29 using namespace CodeGen;
30 
31 static llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) {
32   // void __cxa_free_exception(void *thrown_exception);
33 
34   llvm::FunctionType *FTy =
35     llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
36 
37   return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
38 }
39 
40 static llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) {
41   // void __cxa_call_unexpected(void *thrown_exception);
42 
43   llvm::FunctionType *FTy =
44     llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
45 
46   return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
47 }
48 
49 llvm::Constant *CodeGenModule::getTerminateFn() {
50   // void __terminate();
51 
52   llvm::FunctionType *FTy =
53     llvm::FunctionType::get(VoidTy, /*IsVarArgs=*/false);
54 
55   StringRef name;
56 
57   // In C++, use std::terminate().
58   if (getLangOpts().CPlusPlus &&
59       getTarget().getCXXABI().isItaniumFamily()) {
60     name = "_ZSt9terminatev";
61   } else if (getLangOpts().CPlusPlus &&
62              getTarget().getCXXABI().isMicrosoft()) {
63     if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015))
64       name = "__std_terminate";
65     else
66       name = "?terminate@@YAXXZ";
67   } else if (getLangOpts().ObjC &&
68              getLangOpts().ObjCRuntime.hasTerminate())
69     name = "objc_terminate";
70   else
71     name = "abort";
72   return CreateRuntimeFunction(FTy, name);
73 }
74 
75 static llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM,
76                                             StringRef Name) {
77   llvm::FunctionType *FTy =
78     llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
79 
80   return CGM.CreateRuntimeFunction(FTy, Name);
81 }
82 
83 const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr };
84 const EHPersonality
85 EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr };
86 const EHPersonality
87 EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr };
88 const EHPersonality
89 EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr };
90 const EHPersonality
91 EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr };
92 const EHPersonality
93 EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr };
94 const EHPersonality
95 EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr };
96 const EHPersonality
97 EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"};
98 const EHPersonality
99 EHPersonality::GNU_ObjC_SJLJ = {"__gnu_objc_personality_sj0", "objc_exception_throw"};
100 const EHPersonality
101 EHPersonality::GNU_ObjC_SEH = {"__gnu_objc_personality_seh0", "objc_exception_throw"};
102 const EHPersonality
103 EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr };
104 const EHPersonality
105 EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr };
106 const EHPersonality
107 EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr };
108 const EHPersonality
109 EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr };
110 const EHPersonality
111 EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr };
112 const EHPersonality
113 EHPersonality::GNU_Wasm_CPlusPlus = { "__gxx_wasm_personality_v0", nullptr };
114 
115 static const EHPersonality &getCPersonality(const TargetInfo &Target,
116                                             const LangOptions &L) {
117   const llvm::Triple &T = Target.getTriple();
118   if (T.isWindowsMSVCEnvironment())
119     return EHPersonality::MSVC_CxxFrameHandler3;
120   if (L.SjLjExceptions)
121     return EHPersonality::GNU_C_SJLJ;
122   if (L.DWARFExceptions)
123     return EHPersonality::GNU_C;
124   if (L.SEHExceptions)
125     return EHPersonality::GNU_C_SEH;
126   return EHPersonality::GNU_C;
127 }
128 
129 static const EHPersonality &getObjCPersonality(const TargetInfo &Target,
130                                                const LangOptions &L) {
131   const llvm::Triple &T = Target.getTriple();
132   if (T.isWindowsMSVCEnvironment())
133     return EHPersonality::MSVC_CxxFrameHandler3;
134 
135   switch (L.ObjCRuntime.getKind()) {
136   case ObjCRuntime::FragileMacOSX:
137     return getCPersonality(Target, L);
138   case ObjCRuntime::MacOSX:
139   case ObjCRuntime::iOS:
140   case ObjCRuntime::WatchOS:
141     return EHPersonality::NeXT_ObjC;
142   case ObjCRuntime::GNUstep:
143     if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
144       return EHPersonality::GNUstep_ObjC;
145     LLVM_FALLTHROUGH;
146   case ObjCRuntime::GCC:
147   case ObjCRuntime::ObjFW:
148     if (L.SjLjExceptions)
149       return EHPersonality::GNU_ObjC_SJLJ;
150     if (L.SEHExceptions)
151       return EHPersonality::GNU_ObjC_SEH;
152     return EHPersonality::GNU_ObjC;
153   }
154   llvm_unreachable("bad runtime kind");
155 }
156 
157 static const EHPersonality &getCXXPersonality(const TargetInfo &Target,
158                                               const LangOptions &L) {
159   const llvm::Triple &T = Target.getTriple();
160   if (T.isWindowsMSVCEnvironment())
161     return EHPersonality::MSVC_CxxFrameHandler3;
162   if (L.SjLjExceptions)
163     return EHPersonality::GNU_CPlusPlus_SJLJ;
164   if (L.DWARFExceptions)
165     return EHPersonality::GNU_CPlusPlus;
166   if (L.SEHExceptions)
167     return EHPersonality::GNU_CPlusPlus_SEH;
168   // Wasm EH is a non-MVP feature for now.
169   if (Target.hasFeature("exception-handling") &&
170       (T.getArch() == llvm::Triple::wasm32 ||
171        T.getArch() == llvm::Triple::wasm64))
172     return EHPersonality::GNU_Wasm_CPlusPlus;
173   return EHPersonality::GNU_CPlusPlus;
174 }
175 
176 /// Determines the personality function to use when both C++
177 /// and Objective-C exceptions are being caught.
178 static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target,
179                                                  const LangOptions &L) {
180   if (Target.getTriple().isWindowsMSVCEnvironment())
181     return EHPersonality::MSVC_CxxFrameHandler3;
182 
183   switch (L.ObjCRuntime.getKind()) {
184   // In the fragile ABI, just use C++ exception handling and hope
185   // they're not doing crazy exception mixing.
186   case ObjCRuntime::FragileMacOSX:
187     return getCXXPersonality(Target, L);
188 
189   // The ObjC personality defers to the C++ personality for non-ObjC
190   // handlers.  Unlike the C++ case, we use the same personality
191   // function on targets using (backend-driven) SJLJ EH.
192   case ObjCRuntime::MacOSX:
193   case ObjCRuntime::iOS:
194   case ObjCRuntime::WatchOS:
195     return getObjCPersonality(Target, L);
196 
197   case ObjCRuntime::GNUstep:
198     return EHPersonality::GNU_ObjCXX;
199 
200   // The GCC runtime's personality function inherently doesn't support
201   // mixed EH.  Use the ObjC personality just to avoid returning null.
202   case ObjCRuntime::GCC:
203   case ObjCRuntime::ObjFW:
204     return getObjCPersonality(Target, L);
205   }
206   llvm_unreachable("bad runtime kind");
207 }
208 
209 static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) {
210   if (T.getArch() == llvm::Triple::x86)
211     return EHPersonality::MSVC_except_handler;
212   return EHPersonality::MSVC_C_specific_handler;
213 }
214 
215 const EHPersonality &EHPersonality::get(CodeGenModule &CGM,
216                                         const FunctionDecl *FD) {
217   const llvm::Triple &T = CGM.getTarget().getTriple();
218   const LangOptions &L = CGM.getLangOpts();
219   const TargetInfo &Target = CGM.getTarget();
220 
221   // Functions using SEH get an SEH personality.
222   if (FD && FD->usesSEHTry())
223     return getSEHPersonalityMSVC(T);
224 
225   if (L.ObjC)
226     return L.CPlusPlus ? getObjCXXPersonality(Target, L)
227                        : getObjCPersonality(Target, L);
228   return L.CPlusPlus ? getCXXPersonality(Target, L)
229                      : getCPersonality(Target, L);
230 }
231 
232 const EHPersonality &EHPersonality::get(CodeGenFunction &CGF) {
233   const auto *FD = CGF.CurCodeDecl;
234   // For outlined finallys and filters, use the SEH personality in case they
235   // contain more SEH. This mostly only affects finallys. Filters could
236   // hypothetically use gnu statement expressions to sneak in nested SEH.
237   FD = FD ? FD : CGF.CurSEHParent;
238   return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(FD));
239 }
240 
241 static llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
242                                         const EHPersonality &Personality) {
243   return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
244                                    Personality.PersonalityFn,
245                                    llvm::AttributeList(), /*Local=*/true);
246 }
247 
248 static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
249                                         const EHPersonality &Personality) {
250   llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
251   llvm::PointerType* Int8PtrTy = llvm::PointerType::get(
252       llvm::Type::getInt8Ty(CGM.getLLVMContext()),
253       CGM.getDataLayout().getProgramAddressSpace());
254 
255   return llvm::ConstantExpr::getBitCast(Fn, Int8PtrTy);
256 }
257 
258 /// Check whether a landingpad instruction only uses C++ features.
259 static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI) {
260   for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
261     // Look for something that would've been returned by the ObjC
262     // runtime's GetEHType() method.
263     llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
264     if (LPI->isCatch(I)) {
265       // Check if the catch value has the ObjC prefix.
266       if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
267         // ObjC EH selector entries are always global variables with
268         // names starting like this.
269         if (GV->getName().startswith("OBJC_EHTYPE"))
270           return false;
271     } else {
272       // Check if any of the filter values have the ObjC prefix.
273       llvm::Constant *CVal = cast<llvm::Constant>(Val);
274       for (llvm::User::op_iterator
275               II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
276         if (llvm::GlobalVariable *GV =
277             cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
278           // ObjC EH selector entries are always global variables with
279           // names starting like this.
280           if (GV->getName().startswith("OBJC_EHTYPE"))
281             return false;
282       }
283     }
284   }
285   return true;
286 }
287 
288 /// Check whether a personality function could reasonably be swapped
289 /// for a C++ personality function.
290 static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
291   for (llvm::User *U : Fn->users()) {
292     // Conditionally white-list bitcasts.
293     if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) {
294       if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
295       if (!PersonalityHasOnlyCXXUses(CE))
296         return false;
297       continue;
298     }
299 
300     // Otherwise it must be a function.
301     llvm::Function *F = dyn_cast<llvm::Function>(U);
302     if (!F) return false;
303 
304     for (auto BB = F->begin(), E = F->end(); BB != E; ++BB) {
305       if (BB->isLandingPad())
306         if (!LandingPadHasOnlyCXXUses(BB->getLandingPadInst()))
307           return false;
308     }
309   }
310 
311   return true;
312 }
313 
314 /// Try to use the C++ personality function in ObjC++.  Not doing this
315 /// can cause some incompatibilities with gcc, which is more
316 /// aggressive about only using the ObjC++ personality in a function
317 /// when it really needs it.
318 void CodeGenModule::SimplifyPersonality() {
319   // If we're not in ObjC++ -fexceptions, there's nothing to do.
320   if (!LangOpts.CPlusPlus || !LangOpts.ObjC || !LangOpts.Exceptions)
321     return;
322 
323   // Both the problem this endeavors to fix and the way the logic
324   // above works is specific to the NeXT runtime.
325   if (!LangOpts.ObjCRuntime.isNeXTFamily())
326     return;
327 
328   const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr);
329   const EHPersonality &CXX = getCXXPersonality(getTarget(), LangOpts);
330   if (&ObjCXX == &CXX)
331     return;
332 
333   assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 &&
334          "Different EHPersonalities using the same personality function.");
335 
336   llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn);
337 
338   // Nothing to do if it's unused.
339   if (!Fn || Fn->use_empty()) return;
340 
341   // Can't do the optimization if it has non-C++ uses.
342   if (!PersonalityHasOnlyCXXUses(Fn)) return;
343 
344   // Create the C++ personality function and kill off the old
345   // function.
346   llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
347 
348   // This can happen if the user is screwing with us.
349   if (Fn->getType() != CXXFn->getType()) return;
350 
351   Fn->replaceAllUsesWith(CXXFn);
352   Fn->eraseFromParent();
353 }
354 
355 /// Returns the value to inject into a selector to indicate the
356 /// presence of a catch-all.
357 static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
358   // Possibly we should use @llvm.eh.catch.all.value here.
359   return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
360 }
361 
362 namespace {
363   /// A cleanup to free the exception object if its initialization
364   /// throws.
365   struct FreeException final : EHScopeStack::Cleanup {
366     llvm::Value *exn;
367     FreeException(llvm::Value *exn) : exn(exn) {}
368     void Emit(CodeGenFunction &CGF, Flags flags) override {
369       CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn);
370     }
371   };
372 } // end anonymous namespace
373 
374 // Emits an exception expression into the given location.  This
375 // differs from EmitAnyExprToMem only in that, if a final copy-ctor
376 // call is required, an exception within that copy ctor causes
377 // std::terminate to be invoked.
378 void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) {
379   // Make sure the exception object is cleaned up if there's an
380   // exception during initialization.
381   pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer());
382   EHScopeStack::stable_iterator cleanup = EHStack.stable_begin();
383 
384   // __cxa_allocate_exception returns a void*;  we need to cast this
385   // to the appropriate type for the object.
386   llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo();
387   Address typedAddr = Builder.CreateBitCast(addr, ty);
388 
389   // FIXME: this isn't quite right!  If there's a final unelided call
390   // to a copy constructor, then according to [except.terminate]p1 we
391   // must call std::terminate() if that constructor throws, because
392   // technically that copy occurs after the exception expression is
393   // evaluated but before the exception is caught.  But the best way
394   // to handle that is to teach EmitAggExpr to do the final copy
395   // differently if it can't be elided.
396   EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
397                    /*IsInit*/ true);
398 
399   // Deactivate the cleanup block.
400   DeactivateCleanupBlock(cleanup,
401                          cast<llvm::Instruction>(typedAddr.getPointer()));
402 }
403 
404 Address CodeGenFunction::getExceptionSlot() {
405   if (!ExceptionSlot)
406     ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
407   return Address(ExceptionSlot, getPointerAlign());
408 }
409 
410 Address CodeGenFunction::getEHSelectorSlot() {
411   if (!EHSelectorSlot)
412     EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
413   return Address(EHSelectorSlot, CharUnits::fromQuantity(4));
414 }
415 
416 llvm::Value *CodeGenFunction::getExceptionFromSlot() {
417   return Builder.CreateLoad(getExceptionSlot(), "exn");
418 }
419 
420 llvm::Value *CodeGenFunction::getSelectorFromSlot() {
421   return Builder.CreateLoad(getEHSelectorSlot(), "sel");
422 }
423 
424 void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E,
425                                        bool KeepInsertionPoint) {
426   if (const Expr *SubExpr = E->getSubExpr()) {
427     QualType ThrowType = SubExpr->getType();
428     if (ThrowType->isObjCObjectPointerType()) {
429       const Stmt *ThrowStmt = E->getSubExpr();
430       const ObjCAtThrowStmt S(E->getExprLoc(), const_cast<Stmt *>(ThrowStmt));
431       CGM.getObjCRuntime().EmitThrowStmt(*this, S, false);
432     } else {
433       CGM.getCXXABI().emitThrow(*this, E);
434     }
435   } else {
436     CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true);
437   }
438 
439   // throw is an expression, and the expression emitters expect us
440   // to leave ourselves at a valid insertion point.
441   if (KeepInsertionPoint)
442     EmitBlock(createBasicBlock("throw.cont"));
443 }
444 
445 void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
446   if (!CGM.getLangOpts().CXXExceptions)
447     return;
448 
449   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
450   if (!FD) {
451     // Check if CapturedDecl is nothrow and create terminate scope for it.
452     if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
453       if (CD->isNothrow())
454         EHStack.pushTerminate();
455     }
456     return;
457   }
458   const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
459   if (!Proto)
460     return;
461 
462   ExceptionSpecificationType EST = Proto->getExceptionSpecType();
463   if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) {
464     // noexcept functions are simple terminate scopes.
465     EHStack.pushTerminate();
466   } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
467     // TODO: Revisit exception specifications for the MS ABI.  There is a way to
468     // encode these in an object file but MSVC doesn't do anything with it.
469     if (getTarget().getCXXABI().isMicrosoft())
470       return;
471     unsigned NumExceptions = Proto->getNumExceptions();
472     EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
473 
474     for (unsigned I = 0; I != NumExceptions; ++I) {
475       QualType Ty = Proto->getExceptionType(I);
476       QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
477       llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
478                                                         /*ForEH=*/true);
479       Filter->setFilter(I, EHType);
480     }
481   }
482 }
483 
484 /// Emit the dispatch block for a filter scope if necessary.
485 static void emitFilterDispatchBlock(CodeGenFunction &CGF,
486                                     EHFilterScope &filterScope) {
487   llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
488   if (!dispatchBlock) return;
489   if (dispatchBlock->use_empty()) {
490     delete dispatchBlock;
491     return;
492   }
493 
494   CGF.EmitBlockAfterUses(dispatchBlock);
495 
496   // If this isn't a catch-all filter, we need to check whether we got
497   // here because the filter triggered.
498   if (filterScope.getNumFilters()) {
499     // Load the selector value.
500     llvm::Value *selector = CGF.getSelectorFromSlot();
501     llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
502 
503     llvm::Value *zero = CGF.Builder.getInt32(0);
504     llvm::Value *failsFilter =
505         CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
506     CGF.Builder.CreateCondBr(failsFilter, unexpectedBB,
507                              CGF.getEHResumeBlock(false));
508 
509     CGF.EmitBlock(unexpectedBB);
510   }
511 
512   // Call __cxa_call_unexpected.  This doesn't need to be an invoke
513   // because __cxa_call_unexpected magically filters exceptions
514   // according to the last landing pad the exception was thrown
515   // into.  Seriously.
516   llvm::Value *exn = CGF.getExceptionFromSlot();
517   CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn)
518     ->setDoesNotReturn();
519   CGF.Builder.CreateUnreachable();
520 }
521 
522 void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
523   if (!CGM.getLangOpts().CXXExceptions)
524     return;
525 
526   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
527   if (!FD) {
528     // Check if CapturedDecl is nothrow and pop terminate scope for it.
529     if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
530       if (CD->isNothrow())
531         EHStack.popTerminate();
532     }
533     return;
534   }
535   const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
536   if (!Proto)
537     return;
538 
539   ExceptionSpecificationType EST = Proto->getExceptionSpecType();
540   if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) {
541     EHStack.popTerminate();
542   } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
543     // TODO: Revisit exception specifications for the MS ABI.  There is a way to
544     // encode these in an object file but MSVC doesn't do anything with it.
545     if (getTarget().getCXXABI().isMicrosoft())
546       return;
547     EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
548     emitFilterDispatchBlock(*this, filterScope);
549     EHStack.popFilter();
550   }
551 }
552 
553 void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
554   EnterCXXTryStmt(S);
555   EmitStmt(S.getTryBlock());
556   ExitCXXTryStmt(S);
557 }
558 
559 void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
560   unsigned NumHandlers = S.getNumHandlers();
561   EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
562 
563   for (unsigned I = 0; I != NumHandlers; ++I) {
564     const CXXCatchStmt *C = S.getHandler(I);
565 
566     llvm::BasicBlock *Handler = createBasicBlock("catch");
567     if (C->getExceptionDecl()) {
568       // FIXME: Dropping the reference type on the type into makes it
569       // impossible to correctly implement catch-by-reference
570       // semantics for pointers.  Unfortunately, this is what all
571       // existing compilers do, and it's not clear that the standard
572       // personality routine is capable of doing this right.  See C++ DR 388:
573       //   http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
574       Qualifiers CaughtTypeQuals;
575       QualType CaughtType = CGM.getContext().getUnqualifiedArrayType(
576           C->getCaughtType().getNonReferenceType(), CaughtTypeQuals);
577 
578       CatchTypeInfo TypeInfo{nullptr, 0};
579       if (CaughtType->isObjCObjectPointerType())
580         TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType);
581       else
582         TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType(
583             CaughtType, C->getCaughtType());
584       CatchScope->setHandler(I, TypeInfo, Handler);
585     } else {
586       // No exception decl indicates '...', a catch-all.
587       CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler);
588     }
589   }
590 }
591 
592 llvm::BasicBlock *
593 CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
594   if (EHPersonality::get(*this).usesFuncletPads())
595     return getFuncletEHDispatchBlock(si);
596 
597   // The dispatch block for the end of the scope chain is a block that
598   // just resumes unwinding.
599   if (si == EHStack.stable_end())
600     return getEHResumeBlock(true);
601 
602   // Otherwise, we should look at the actual scope.
603   EHScope &scope = *EHStack.find(si);
604 
605   llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
606   if (!dispatchBlock) {
607     switch (scope.getKind()) {
608     case EHScope::Catch: {
609       // Apply a special case to a single catch-all.
610       EHCatchScope &catchScope = cast<EHCatchScope>(scope);
611       if (catchScope.getNumHandlers() == 1 &&
612           catchScope.getHandler(0).isCatchAll()) {
613         dispatchBlock = catchScope.getHandler(0).Block;
614 
615       // Otherwise, make a dispatch block.
616       } else {
617         dispatchBlock = createBasicBlock("catch.dispatch");
618       }
619       break;
620     }
621 
622     case EHScope::Cleanup:
623       dispatchBlock = createBasicBlock("ehcleanup");
624       break;
625 
626     case EHScope::Filter:
627       dispatchBlock = createBasicBlock("filter.dispatch");
628       break;
629 
630     case EHScope::Terminate:
631       dispatchBlock = getTerminateHandler();
632       break;
633 
634     case EHScope::PadEnd:
635       llvm_unreachable("PadEnd unnecessary for Itanium!");
636     }
637     scope.setCachedEHDispatchBlock(dispatchBlock);
638   }
639   return dispatchBlock;
640 }
641 
642 llvm::BasicBlock *
643 CodeGenFunction::getFuncletEHDispatchBlock(EHScopeStack::stable_iterator SI) {
644   // Returning nullptr indicates that the previous dispatch block should unwind
645   // to caller.
646   if (SI == EHStack.stable_end())
647     return nullptr;
648 
649   // Otherwise, we should look at the actual scope.
650   EHScope &EHS = *EHStack.find(SI);
651 
652   llvm::BasicBlock *DispatchBlock = EHS.getCachedEHDispatchBlock();
653   if (DispatchBlock)
654     return DispatchBlock;
655 
656   if (EHS.getKind() == EHScope::Terminate)
657     DispatchBlock = getTerminateFunclet();
658   else
659     DispatchBlock = createBasicBlock();
660   CGBuilderTy Builder(*this, DispatchBlock);
661 
662   switch (EHS.getKind()) {
663   case EHScope::Catch:
664     DispatchBlock->setName("catch.dispatch");
665     break;
666 
667   case EHScope::Cleanup:
668     DispatchBlock->setName("ehcleanup");
669     break;
670 
671   case EHScope::Filter:
672     llvm_unreachable("exception specifications not handled yet!");
673 
674   case EHScope::Terminate:
675     DispatchBlock->setName("terminate");
676     break;
677 
678   case EHScope::PadEnd:
679     llvm_unreachable("PadEnd dispatch block missing!");
680   }
681   EHS.setCachedEHDispatchBlock(DispatchBlock);
682   return DispatchBlock;
683 }
684 
685 /// Check whether this is a non-EH scope, i.e. a scope which doesn't
686 /// affect exception handling.  Currently, the only non-EH scopes are
687 /// normal-only cleanup scopes.
688 static bool isNonEHScope(const EHScope &S) {
689   switch (S.getKind()) {
690   case EHScope::Cleanup:
691     return !cast<EHCleanupScope>(S).isEHCleanup();
692   case EHScope::Filter:
693   case EHScope::Catch:
694   case EHScope::Terminate:
695   case EHScope::PadEnd:
696     return false;
697   }
698 
699   llvm_unreachable("Invalid EHScope Kind!");
700 }
701 
702 llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
703   assert(EHStack.requiresLandingPad());
704   assert(!EHStack.empty());
705 
706   // If exceptions are disabled and SEH is not in use, then there is no invoke
707   // destination. SEH "works" even if exceptions are off. In practice, this
708   // means that C++ destructors and other EH cleanups don't run, which is
709   // consistent with MSVC's behavior.
710   const LangOptions &LO = CGM.getLangOpts();
711   if (!LO.Exceptions) {
712     if (!LO.Borland && !LO.MicrosoftExt)
713       return nullptr;
714     if (!currentFunctionUsesSEHTry())
715       return nullptr;
716   }
717 
718   // CUDA device code doesn't have exceptions.
719   if (LO.CUDA && LO.CUDAIsDevice)
720     return nullptr;
721 
722   // Check the innermost scope for a cached landing pad.  If this is
723   // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
724   llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
725   if (LP) return LP;
726 
727   const EHPersonality &Personality = EHPersonality::get(*this);
728 
729   if (!CurFn->hasPersonalityFn())
730     CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality));
731 
732   if (Personality.usesFuncletPads()) {
733     // We don't need separate landing pads in the funclet model.
734     LP = getEHDispatchBlock(EHStack.getInnermostEHScope());
735   } else {
736     // Build the landing pad for this scope.
737     LP = EmitLandingPad();
738   }
739 
740   assert(LP);
741 
742   // Cache the landing pad on the innermost scope.  If this is a
743   // non-EH scope, cache the landing pad on the enclosing scope, too.
744   for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
745     ir->setCachedLandingPad(LP);
746     if (!isNonEHScope(*ir)) break;
747   }
748 
749   return LP;
750 }
751 
752 llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
753   assert(EHStack.requiresLandingPad());
754 
755   EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
756   switch (innermostEHScope.getKind()) {
757   case EHScope::Terminate:
758     return getTerminateLandingPad();
759 
760   case EHScope::PadEnd:
761     llvm_unreachable("PadEnd unnecessary for Itanium!");
762 
763   case EHScope::Catch:
764   case EHScope::Cleanup:
765   case EHScope::Filter:
766     if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
767       return lpad;
768   }
769 
770   // Save the current IR generation state.
771   CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
772   auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation);
773 
774   // Create and configure the landing pad.
775   llvm::BasicBlock *lpad = createBasicBlock("lpad");
776   EmitBlock(lpad);
777 
778   llvm::LandingPadInst *LPadInst =
779       Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0);
780 
781   llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
782   Builder.CreateStore(LPadExn, getExceptionSlot());
783   llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
784   Builder.CreateStore(LPadSel, getEHSelectorSlot());
785 
786   // Save the exception pointer.  It's safe to use a single exception
787   // pointer per function because EH cleanups can never have nested
788   // try/catches.
789   // Build the landingpad instruction.
790 
791   // Accumulate all the handlers in scope.
792   bool hasCatchAll = false;
793   bool hasCleanup = false;
794   bool hasFilter = false;
795   SmallVector<llvm::Value*, 4> filterTypes;
796   llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
797   for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E;
798        ++I) {
799 
800     switch (I->getKind()) {
801     case EHScope::Cleanup:
802       // If we have a cleanup, remember that.
803       hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
804       continue;
805 
806     case EHScope::Filter: {
807       assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
808       assert(!hasCatchAll && "EH filter reached after catch-all");
809 
810       // Filter scopes get added to the landingpad in weird ways.
811       EHFilterScope &filter = cast<EHFilterScope>(*I);
812       hasFilter = true;
813 
814       // Add all the filter values.
815       for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
816         filterTypes.push_back(filter.getFilter(i));
817       goto done;
818     }
819 
820     case EHScope::Terminate:
821       // Terminate scopes are basically catch-alls.
822       assert(!hasCatchAll);
823       hasCatchAll = true;
824       goto done;
825 
826     case EHScope::Catch:
827       break;
828 
829     case EHScope::PadEnd:
830       llvm_unreachable("PadEnd unnecessary for Itanium!");
831     }
832 
833     EHCatchScope &catchScope = cast<EHCatchScope>(*I);
834     for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
835       EHCatchScope::Handler handler = catchScope.getHandler(hi);
836       assert(handler.Type.Flags == 0 &&
837              "landingpads do not support catch handler flags");
838 
839       // If this is a catch-all, register that and abort.
840       if (!handler.Type.RTTI) {
841         assert(!hasCatchAll);
842         hasCatchAll = true;
843         goto done;
844       }
845 
846       // Check whether we already have a handler for this type.
847       if (catchTypes.insert(handler.Type.RTTI).second)
848         // If not, add it directly to the landingpad.
849         LPadInst->addClause(handler.Type.RTTI);
850     }
851   }
852 
853  done:
854   // If we have a catch-all, add null to the landingpad.
855   assert(!(hasCatchAll && hasFilter));
856   if (hasCatchAll) {
857     LPadInst->addClause(getCatchAllValue(*this));
858 
859   // If we have an EH filter, we need to add those handlers in the
860   // right place in the landingpad, which is to say, at the end.
861   } else if (hasFilter) {
862     // Create a filter expression: a constant array indicating which filter
863     // types there are. The personality routine only lands here if the filter
864     // doesn't match.
865     SmallVector<llvm::Constant*, 8> Filters;
866     llvm::ArrayType *AType =
867       llvm::ArrayType::get(!filterTypes.empty() ?
868                              filterTypes[0]->getType() : Int8PtrTy,
869                            filterTypes.size());
870 
871     for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
872       Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
873     llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
874     LPadInst->addClause(FilterArray);
875 
876     // Also check whether we need a cleanup.
877     if (hasCleanup)
878       LPadInst->setCleanup(true);
879 
880   // Otherwise, signal that we at least have cleanups.
881   } else if (hasCleanup) {
882     LPadInst->setCleanup(true);
883   }
884 
885   assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
886          "landingpad instruction has no clauses!");
887 
888   // Tell the backend how to generate the landing pad.
889   Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
890 
891   // Restore the old IR generation state.
892   Builder.restoreIP(savedIP);
893 
894   return lpad;
895 }
896 
897 static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) {
898   llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
899   assert(DispatchBlock);
900 
901   CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP();
902   CGF.EmitBlockAfterUses(DispatchBlock);
903 
904   llvm::Value *ParentPad = CGF.CurrentFuncletPad;
905   if (!ParentPad)
906     ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext());
907   llvm::BasicBlock *UnwindBB =
908       CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope());
909 
910   unsigned NumHandlers = CatchScope.getNumHandlers();
911   llvm::CatchSwitchInst *CatchSwitch =
912       CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers);
913 
914   // Test against each of the exception types we claim to catch.
915   for (unsigned I = 0; I < NumHandlers; ++I) {
916     const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
917 
918     CatchTypeInfo TypeInfo = Handler.Type;
919     if (!TypeInfo.RTTI)
920       TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
921 
922     CGF.Builder.SetInsertPoint(Handler.Block);
923 
924     if (EHPersonality::get(CGF).isMSVCXXPersonality()) {
925       CGF.Builder.CreateCatchPad(
926           CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags),
927                         llvm::Constant::getNullValue(CGF.VoidPtrTy)});
928     } else {
929       CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI});
930     }
931 
932     CatchSwitch->addHandler(Handler.Block);
933   }
934   CGF.Builder.restoreIP(SavedIP);
935 }
936 
937 // Wasm uses Windows-style EH instructions, but it merges all catch clauses into
938 // one big catchpad, within which we use Itanium's landingpad-style selector
939 // comparison instructions.
940 static void emitWasmCatchPadBlock(CodeGenFunction &CGF,
941                                   EHCatchScope &CatchScope) {
942   llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
943   assert(DispatchBlock);
944 
945   CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP();
946   CGF.EmitBlockAfterUses(DispatchBlock);
947 
948   llvm::Value *ParentPad = CGF.CurrentFuncletPad;
949   if (!ParentPad)
950     ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext());
951   llvm::BasicBlock *UnwindBB =
952       CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope());
953 
954   unsigned NumHandlers = CatchScope.getNumHandlers();
955   llvm::CatchSwitchInst *CatchSwitch =
956       CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers);
957 
958   // We don't use a landingpad instruction, so generate intrinsic calls to
959   // provide exception and selector values.
960   llvm::BasicBlock *WasmCatchStartBlock = CGF.createBasicBlock("catch.start");
961   CatchSwitch->addHandler(WasmCatchStartBlock);
962   CGF.EmitBlockAfterUses(WasmCatchStartBlock);
963 
964   // Create a catchpad instruction.
965   SmallVector<llvm::Value *, 4> CatchTypes;
966   for (unsigned I = 0, E = NumHandlers; I < E; ++I) {
967     const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
968     CatchTypeInfo TypeInfo = Handler.Type;
969     if (!TypeInfo.RTTI)
970       TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
971     CatchTypes.push_back(TypeInfo.RTTI);
972   }
973   auto *CPI = CGF.Builder.CreateCatchPad(CatchSwitch, CatchTypes);
974 
975   // Create calls to wasm.get.exception and wasm.get.ehselector intrinsics.
976   // Before they are lowered appropriately later, they provide values for the
977   // exception and selector.
978   llvm::Value *GetExnFn =
979       CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception);
980   llvm::Value *GetSelectorFn =
981       CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_ehselector);
982   llvm::CallInst *Exn = CGF.Builder.CreateCall(GetExnFn, CPI);
983   CGF.Builder.CreateStore(Exn, CGF.getExceptionSlot());
984   llvm::CallInst *Selector = CGF.Builder.CreateCall(GetSelectorFn, CPI);
985 
986   llvm::Value *TypeIDFn = CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
987 
988   // If there's only a single catch-all, branch directly to its handler.
989   if (CatchScope.getNumHandlers() == 1 &&
990       CatchScope.getHandler(0).isCatchAll()) {
991     CGF.Builder.CreateBr(CatchScope.getHandler(0).Block);
992     CGF.Builder.restoreIP(SavedIP);
993     return;
994   }
995 
996   // Test against each of the exception types we claim to catch.
997   for (unsigned I = 0, E = NumHandlers;; ++I) {
998     assert(I < E && "ran off end of handlers!");
999     const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
1000     CatchTypeInfo TypeInfo = Handler.Type;
1001     if (!TypeInfo.RTTI)
1002       TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
1003 
1004     // Figure out the next block.
1005     llvm::BasicBlock *NextBlock;
1006 
1007     bool EmitNextBlock = false, NextIsEnd = false;
1008 
1009     // If this is the last handler, we're at the end, and the next block is a
1010     // block that contains a call to the rethrow function, so we can unwind to
1011     // the enclosing EH scope. The call itself will be generated later.
1012     if (I + 1 == E) {
1013       NextBlock = CGF.createBasicBlock("rethrow");
1014       EmitNextBlock = true;
1015       NextIsEnd = true;
1016 
1017       // If the next handler is a catch-all, we're at the end, and the
1018       // next block is that handler.
1019     } else if (CatchScope.getHandler(I + 1).isCatchAll()) {
1020       NextBlock = CatchScope.getHandler(I + 1).Block;
1021       NextIsEnd = true;
1022 
1023       // Otherwise, we're not at the end and we need a new block.
1024     } else {
1025       NextBlock = CGF.createBasicBlock("catch.fallthrough");
1026       EmitNextBlock = true;
1027     }
1028 
1029     // Figure out the catch type's index in the LSDA's type table.
1030     llvm::CallInst *TypeIndex = CGF.Builder.CreateCall(TypeIDFn, TypeInfo.RTTI);
1031     TypeIndex->setDoesNotThrow();
1032 
1033     llvm::Value *MatchesTypeIndex =
1034         CGF.Builder.CreateICmpEQ(Selector, TypeIndex, "matches");
1035     CGF.Builder.CreateCondBr(MatchesTypeIndex, Handler.Block, NextBlock);
1036 
1037     if (EmitNextBlock)
1038       CGF.EmitBlock(NextBlock);
1039     if (NextIsEnd)
1040       break;
1041   }
1042 
1043   CGF.Builder.restoreIP(SavedIP);
1044 }
1045 
1046 /// Emit the structure of the dispatch block for the given catch scope.
1047 /// It is an invariant that the dispatch block already exists.
1048 static void emitCatchDispatchBlock(CodeGenFunction &CGF,
1049                                    EHCatchScope &catchScope) {
1050   if (EHPersonality::get(CGF).isWasmPersonality())
1051     return emitWasmCatchPadBlock(CGF, catchScope);
1052   if (EHPersonality::get(CGF).usesFuncletPads())
1053     return emitCatchPadBlock(CGF, catchScope);
1054 
1055   llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1056   assert(dispatchBlock);
1057 
1058   // If there's only a single catch-all, getEHDispatchBlock returned
1059   // that catch-all as the dispatch block.
1060   if (catchScope.getNumHandlers() == 1 &&
1061       catchScope.getHandler(0).isCatchAll()) {
1062     assert(dispatchBlock == catchScope.getHandler(0).Block);
1063     return;
1064   }
1065 
1066   CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1067   CGF.EmitBlockAfterUses(dispatchBlock);
1068 
1069   // Select the right handler.
1070   llvm::Value *llvm_eh_typeid_for =
1071     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1072 
1073   // Load the selector value.
1074   llvm::Value *selector = CGF.getSelectorFromSlot();
1075 
1076   // Test against each of the exception types we claim to catch.
1077   for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1078     assert(i < e && "ran off end of handlers!");
1079     const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1080 
1081     llvm::Value *typeValue = handler.Type.RTTI;
1082     assert(handler.Type.Flags == 0 &&
1083            "landingpads do not support catch handler flags");
1084     assert(typeValue && "fell into catch-all case!");
1085     typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1086 
1087     // Figure out the next block.
1088     bool nextIsEnd;
1089     llvm::BasicBlock *nextBlock;
1090 
1091     // If this is the last handler, we're at the end, and the next
1092     // block is the block for the enclosing EH scope.
1093     if (i + 1 == e) {
1094       nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1095       nextIsEnd = true;
1096 
1097     // If the next handler is a catch-all, we're at the end, and the
1098     // next block is that handler.
1099     } else if (catchScope.getHandler(i+1).isCatchAll()) {
1100       nextBlock = catchScope.getHandler(i+1).Block;
1101       nextIsEnd = true;
1102 
1103     // Otherwise, we're not at the end and we need a new block.
1104     } else {
1105       nextBlock = CGF.createBasicBlock("catch.fallthrough");
1106       nextIsEnd = false;
1107     }
1108 
1109     // Figure out the catch type's index in the LSDA's type table.
1110     llvm::CallInst *typeIndex =
1111       CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1112     typeIndex->setDoesNotThrow();
1113 
1114     llvm::Value *matchesTypeIndex =
1115       CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1116     CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1117 
1118     // If the next handler is a catch-all, we're completely done.
1119     if (nextIsEnd) {
1120       CGF.Builder.restoreIP(savedIP);
1121       return;
1122     }
1123     // Otherwise we need to emit and continue at that block.
1124     CGF.EmitBlock(nextBlock);
1125   }
1126 }
1127 
1128 void CodeGenFunction::popCatchScope() {
1129   EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1130   if (catchScope.hasEHBranches())
1131     emitCatchDispatchBlock(*this, catchScope);
1132   EHStack.popCatch();
1133 }
1134 
1135 void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
1136   unsigned NumHandlers = S.getNumHandlers();
1137   EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1138   assert(CatchScope.getNumHandlers() == NumHandlers);
1139   llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
1140 
1141   // If the catch was not required, bail out now.
1142   if (!CatchScope.hasEHBranches()) {
1143     CatchScope.clearHandlerBlocks();
1144     EHStack.popCatch();
1145     return;
1146   }
1147 
1148   // Emit the structure of the EH dispatch for this catch.
1149   emitCatchDispatchBlock(*this, CatchScope);
1150 
1151   // Copy the handler blocks off before we pop the EH stack.  Emitting
1152   // the handlers might scribble on this memory.
1153   SmallVector<EHCatchScope::Handler, 8> Handlers(
1154       CatchScope.begin(), CatchScope.begin() + NumHandlers);
1155 
1156   EHStack.popCatch();
1157 
1158   // The fall-through block.
1159   llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
1160 
1161   // We just emitted the body of the try; jump to the continue block.
1162   if (HaveInsertPoint())
1163     Builder.CreateBr(ContBB);
1164 
1165   // Determine if we need an implicit rethrow for all these catch handlers;
1166   // see the comment below.
1167   bool doImplicitRethrow = false;
1168   if (IsFnTryBlock)
1169     doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1170                         isa<CXXConstructorDecl>(CurCodeDecl);
1171 
1172   // Wasm uses Windows-style EH instructions, but merges all catch clauses into
1173   // one big catchpad. So we save the old funclet pad here before we traverse
1174   // each catch handler.
1175   SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1176       CurrentFuncletPad);
1177   llvm::BasicBlock *WasmCatchStartBlock = nullptr;
1178   if (EHPersonality::get(*this).isWasmPersonality()) {
1179     auto *CatchSwitch =
1180         cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHI());
1181     WasmCatchStartBlock = CatchSwitch->hasUnwindDest()
1182                               ? CatchSwitch->getSuccessor(1)
1183                               : CatchSwitch->getSuccessor(0);
1184     auto *CPI = cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHI());
1185     CurrentFuncletPad = CPI;
1186   }
1187 
1188   // Perversely, we emit the handlers backwards precisely because we
1189   // want them to appear in source order.  In all of these cases, the
1190   // catch block will have exactly one predecessor, which will be a
1191   // particular block in the catch dispatch.  However, in the case of
1192   // a catch-all, one of the dispatch blocks will branch to two
1193   // different handlers, and EmitBlockAfterUses will cause the second
1194   // handler to be moved before the first.
1195   bool HasCatchAll = false;
1196   for (unsigned I = NumHandlers; I != 0; --I) {
1197     HasCatchAll |= Handlers[I - 1].isCatchAll();
1198     llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1199     EmitBlockAfterUses(CatchBlock);
1200 
1201     // Catch the exception if this isn't a catch-all.
1202     const CXXCatchStmt *C = S.getHandler(I-1);
1203 
1204     // Enter a cleanup scope, including the catch variable and the
1205     // end-catch.
1206     RunCleanupsScope CatchScope(*this);
1207 
1208     // Initialize the catch variable and set up the cleanups.
1209     SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1210         CurrentFuncletPad);
1211     CGM.getCXXABI().emitBeginCatch(*this, C);
1212 
1213     // Emit the PGO counter increment.
1214     incrementProfileCounter(C);
1215 
1216     // Perform the body of the catch.
1217     EmitStmt(C->getHandlerBlock());
1218 
1219     // [except.handle]p11:
1220     //   The currently handled exception is rethrown if control
1221     //   reaches the end of a handler of the function-try-block of a
1222     //   constructor or destructor.
1223 
1224     // It is important that we only do this on fallthrough and not on
1225     // return.  Note that it's illegal to put a return in a
1226     // constructor function-try-block's catch handler (p14), so this
1227     // really only applies to destructors.
1228     if (doImplicitRethrow && HaveInsertPoint()) {
1229       CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false);
1230       Builder.CreateUnreachable();
1231       Builder.ClearInsertionPoint();
1232     }
1233 
1234     // Fall out through the catch cleanups.
1235     CatchScope.ForceCleanup();
1236 
1237     // Branch out of the try.
1238     if (HaveInsertPoint())
1239       Builder.CreateBr(ContBB);
1240   }
1241 
1242   // Because in wasm we merge all catch clauses into one big catchpad, in case
1243   // none of the types in catch handlers matches after we test against each of
1244   // them, we should unwind to the next EH enclosing scope. We generate a call
1245   // to rethrow function here to do that.
1246   if (EHPersonality::get(*this).isWasmPersonality() && !HasCatchAll) {
1247     assert(WasmCatchStartBlock);
1248     // Navigate for the "rethrow" block we created in emitWasmCatchPadBlock().
1249     // Wasm uses landingpad-style conditional branches to compare selectors, so
1250     // we follow the false destination for each of the cond branches to reach
1251     // the rethrow block.
1252     llvm::BasicBlock *RethrowBlock = WasmCatchStartBlock;
1253     while (llvm::Instruction *TI = RethrowBlock->getTerminator()) {
1254       auto *BI = cast<llvm::BranchInst>(TI);
1255       assert(BI->isConditional());
1256       RethrowBlock = BI->getSuccessor(1);
1257     }
1258     assert(RethrowBlock != WasmCatchStartBlock && RethrowBlock->empty());
1259     Builder.SetInsertPoint(RethrowBlock);
1260     CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true);
1261   }
1262 
1263   EmitBlock(ContBB);
1264   incrementProfileCounter(&S);
1265 }
1266 
1267 namespace {
1268   struct CallEndCatchForFinally final : EHScopeStack::Cleanup {
1269     llvm::Value *ForEHVar;
1270     llvm::Value *EndCatchFn;
1271     CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1272       : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1273 
1274     void Emit(CodeGenFunction &CGF, Flags flags) override {
1275       llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1276       llvm::BasicBlock *CleanupContBB =
1277         CGF.createBasicBlock("finally.cleanup.cont");
1278 
1279       llvm::Value *ShouldEndCatch =
1280         CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch");
1281       CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1282       CGF.EmitBlock(EndCatchBB);
1283       CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw
1284       CGF.EmitBlock(CleanupContBB);
1285     }
1286   };
1287 
1288   struct PerformFinally final : EHScopeStack::Cleanup {
1289     const Stmt *Body;
1290     llvm::Value *ForEHVar;
1291     llvm::Value *EndCatchFn;
1292     llvm::Value *RethrowFn;
1293     llvm::Value *SavedExnVar;
1294 
1295     PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1296                    llvm::Value *EndCatchFn,
1297                    llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1298       : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1299         RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1300 
1301     void Emit(CodeGenFunction &CGF, Flags flags) override {
1302       // Enter a cleanup to call the end-catch function if one was provided.
1303       if (EndCatchFn)
1304         CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1305                                                         ForEHVar, EndCatchFn);
1306 
1307       // Save the current cleanup destination in case there are
1308       // cleanups in the finally block.
1309       llvm::Value *SavedCleanupDest =
1310         CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1311                                "cleanup.dest.saved");
1312 
1313       // Emit the finally block.
1314       CGF.EmitStmt(Body);
1315 
1316       // If the end of the finally is reachable, check whether this was
1317       // for EH.  If so, rethrow.
1318       if (CGF.HaveInsertPoint()) {
1319         llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1320         llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1321 
1322         llvm::Value *ShouldRethrow =
1323           CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow");
1324         CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1325 
1326         CGF.EmitBlock(RethrowBB);
1327         if (SavedExnVar) {
1328           CGF.EmitRuntimeCallOrInvoke(RethrowFn,
1329             CGF.Builder.CreateAlignedLoad(SavedExnVar, CGF.getPointerAlign()));
1330         } else {
1331           CGF.EmitRuntimeCallOrInvoke(RethrowFn);
1332         }
1333         CGF.Builder.CreateUnreachable();
1334 
1335         CGF.EmitBlock(ContBB);
1336 
1337         // Restore the cleanup destination.
1338         CGF.Builder.CreateStore(SavedCleanupDest,
1339                                 CGF.getNormalCleanupDestSlot());
1340       }
1341 
1342       // Leave the end-catch cleanup.  As an optimization, pretend that
1343       // the fallthrough path was inaccessible; we've dynamically proven
1344       // that we're not in the EH case along that path.
1345       if (EndCatchFn) {
1346         CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1347         CGF.PopCleanupBlock();
1348         CGF.Builder.restoreIP(SavedIP);
1349       }
1350 
1351       // Now make sure we actually have an insertion point or the
1352       // cleanup gods will hate us.
1353       CGF.EnsureInsertPoint();
1354     }
1355   };
1356 } // end anonymous namespace
1357 
1358 /// Enters a finally block for an implementation using zero-cost
1359 /// exceptions.  This is mostly general, but hard-codes some
1360 /// language/ABI-specific behavior in the catch-all sections.
1361 void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1362                                          const Stmt *body,
1363                                          llvm::Constant *beginCatchFn,
1364                                          llvm::Constant *endCatchFn,
1365                                          llvm::Constant *rethrowFn) {
1366   assert((beginCatchFn != nullptr) == (endCatchFn != nullptr) &&
1367          "begin/end catch functions not paired");
1368   assert(rethrowFn && "rethrow function is required");
1369 
1370   BeginCatchFn = beginCatchFn;
1371 
1372   // The rethrow function has one of the following two types:
1373   //   void (*)()
1374   //   void (*)(void*)
1375   // In the latter case we need to pass it the exception object.
1376   // But we can't use the exception slot because the @finally might
1377   // have a landing pad (which would overwrite the exception slot).
1378   llvm::FunctionType *rethrowFnTy =
1379     cast<llvm::FunctionType>(
1380       cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1381   SavedExnVar = nullptr;
1382   if (rethrowFnTy->getNumParams())
1383     SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
1384 
1385   // A finally block is a statement which must be executed on any edge
1386   // out of a given scope.  Unlike a cleanup, the finally block may
1387   // contain arbitrary control flow leading out of itself.  In
1388   // addition, finally blocks should always be executed, even if there
1389   // are no catch handlers higher on the stack.  Therefore, we
1390   // surround the protected scope with a combination of a normal
1391   // cleanup (to catch attempts to break out of the block via normal
1392   // control flow) and an EH catch-all (semantically "outside" any try
1393   // statement to which the finally block might have been attached).
1394   // The finally block itself is generated in the context of a cleanup
1395   // which conditionally leaves the catch-all.
1396 
1397   // Jump destination for performing the finally block on an exception
1398   // edge.  We'll never actually reach this block, so unreachable is
1399   // fine.
1400   RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
1401 
1402   // Whether the finally block is being executed for EH purposes.
1403   ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1404   CGF.Builder.CreateFlagStore(false, ForEHVar);
1405 
1406   // Enter a normal cleanup which will perform the @finally block.
1407   CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1408                                           ForEHVar, endCatchFn,
1409                                           rethrowFn, SavedExnVar);
1410 
1411   // Enter a catch-all scope.
1412   llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1413   EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1414   catchScope->setCatchAllHandler(0, catchBB);
1415 }
1416 
1417 void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
1418   // Leave the finally catch-all.
1419   EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1420   llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
1421 
1422   CGF.popCatchScope();
1423 
1424   // If there are any references to the catch-all block, emit it.
1425   if (catchBB->use_empty()) {
1426     delete catchBB;
1427   } else {
1428     CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1429     CGF.EmitBlock(catchBB);
1430 
1431     llvm::Value *exn = nullptr;
1432 
1433     // If there's a begin-catch function, call it.
1434     if (BeginCatchFn) {
1435       exn = CGF.getExceptionFromSlot();
1436       CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn);
1437     }
1438 
1439     // If we need to remember the exception pointer to rethrow later, do so.
1440     if (SavedExnVar) {
1441       if (!exn) exn = CGF.getExceptionFromSlot();
1442       CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign());
1443     }
1444 
1445     // Tell the cleanups in the finally block that we're do this for EH.
1446     CGF.Builder.CreateFlagStore(true, ForEHVar);
1447 
1448     // Thread a jump through the finally cleanup.
1449     CGF.EmitBranchThroughCleanup(RethrowDest);
1450 
1451     CGF.Builder.restoreIP(savedIP);
1452   }
1453 
1454   // Finally, leave the @finally cleanup.
1455   CGF.PopCleanupBlock();
1456 }
1457 
1458 llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1459   if (TerminateLandingPad)
1460     return TerminateLandingPad;
1461 
1462   CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1463 
1464   // This will get inserted at the end of the function.
1465   TerminateLandingPad = createBasicBlock("terminate.lpad");
1466   Builder.SetInsertPoint(TerminateLandingPad);
1467 
1468   // Tell the backend that this is a landing pad.
1469   const EHPersonality &Personality = EHPersonality::get(*this);
1470 
1471   if (!CurFn->hasPersonalityFn())
1472     CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality));
1473 
1474   llvm::LandingPadInst *LPadInst =
1475       Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0);
1476   LPadInst->addClause(getCatchAllValue(*this));
1477 
1478   llvm::Value *Exn = nullptr;
1479   if (getLangOpts().CPlusPlus)
1480     Exn = Builder.CreateExtractValue(LPadInst, 0);
1481   llvm::CallInst *terminateCall =
1482       CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
1483   terminateCall->setDoesNotReturn();
1484   Builder.CreateUnreachable();
1485 
1486   // Restore the saved insertion state.
1487   Builder.restoreIP(SavedIP);
1488 
1489   return TerminateLandingPad;
1490 }
1491 
1492 llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
1493   if (TerminateHandler)
1494     return TerminateHandler;
1495 
1496   // Set up the terminate handler.  This block is inserted at the very
1497   // end of the function by FinishFunction.
1498   TerminateHandler = createBasicBlock("terminate.handler");
1499   CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1500   Builder.SetInsertPoint(TerminateHandler);
1501 
1502   llvm::Value *Exn = nullptr;
1503   if (getLangOpts().CPlusPlus)
1504     Exn = getExceptionFromSlot();
1505   llvm::CallInst *terminateCall =
1506       CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
1507   terminateCall->setDoesNotReturn();
1508   Builder.CreateUnreachable();
1509 
1510   // Restore the saved insertion state.
1511   Builder.restoreIP(SavedIP);
1512 
1513   return TerminateHandler;
1514 }
1515 
1516 llvm::BasicBlock *CodeGenFunction::getTerminateFunclet() {
1517   assert(EHPersonality::get(*this).usesFuncletPads() &&
1518          "use getTerminateLandingPad for non-funclet EH");
1519 
1520   llvm::BasicBlock *&TerminateFunclet = TerminateFunclets[CurrentFuncletPad];
1521   if (TerminateFunclet)
1522     return TerminateFunclet;
1523 
1524   CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1525 
1526   // Set up the terminate handler.  This block is inserted at the very
1527   // end of the function by FinishFunction.
1528   TerminateFunclet = createBasicBlock("terminate.handler");
1529   Builder.SetInsertPoint(TerminateFunclet);
1530 
1531   // Create the cleanuppad using the current parent pad as its token. Use 'none'
1532   // if this is a top-level terminate scope, which is the common case.
1533   SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1534       CurrentFuncletPad);
1535   llvm::Value *ParentPad = CurrentFuncletPad;
1536   if (!ParentPad)
1537     ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
1538   CurrentFuncletPad = Builder.CreateCleanupPad(ParentPad);
1539 
1540   // Emit the __std_terminate call.
1541   llvm::Value *Exn = nullptr;
1542   // In case of wasm personality, we need to pass the exception value to
1543   // __clang_call_terminate function.
1544   if (getLangOpts().CPlusPlus &&
1545       EHPersonality::get(*this).isWasmPersonality()) {
1546     llvm::Value *GetExnFn =
1547         CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception);
1548     Exn = Builder.CreateCall(GetExnFn, CurrentFuncletPad);
1549   }
1550   llvm::CallInst *terminateCall =
1551       CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
1552   terminateCall->setDoesNotReturn();
1553   Builder.CreateUnreachable();
1554 
1555   // Restore the saved insertion state.
1556   Builder.restoreIP(SavedIP);
1557 
1558   return TerminateFunclet;
1559 }
1560 
1561 llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) {
1562   if (EHResumeBlock) return EHResumeBlock;
1563 
1564   CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1565 
1566   // We emit a jump to a notional label at the outermost unwind state.
1567   EHResumeBlock = createBasicBlock("eh.resume");
1568   Builder.SetInsertPoint(EHResumeBlock);
1569 
1570   const EHPersonality &Personality = EHPersonality::get(*this);
1571 
1572   // This can always be a call because we necessarily didn't find
1573   // anything on the EH stack which needs our help.
1574   const char *RethrowName = Personality.CatchallRethrowFn;
1575   if (RethrowName != nullptr && !isCleanup) {
1576     EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName),
1577                     getExceptionFromSlot())->setDoesNotReturn();
1578     Builder.CreateUnreachable();
1579     Builder.restoreIP(SavedIP);
1580     return EHResumeBlock;
1581   }
1582 
1583   // Recreate the landingpad's return value for the 'resume' instruction.
1584   llvm::Value *Exn = getExceptionFromSlot();
1585   llvm::Value *Sel = getSelectorFromSlot();
1586 
1587   llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), Sel->getType());
1588   llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1589   LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1590   LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1591 
1592   Builder.CreateResume(LPadVal);
1593   Builder.restoreIP(SavedIP);
1594   return EHResumeBlock;
1595 }
1596 
1597 void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) {
1598   EnterSEHTryStmt(S);
1599   {
1600     JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave");
1601 
1602     SEHTryEpilogueStack.push_back(&TryExit);
1603     EmitStmt(S.getTryBlock());
1604     SEHTryEpilogueStack.pop_back();
1605 
1606     if (!TryExit.getBlock()->use_empty())
1607       EmitBlock(TryExit.getBlock(), /*IsFinished=*/true);
1608     else
1609       delete TryExit.getBlock();
1610   }
1611   ExitSEHTryStmt(S);
1612 }
1613 
1614 namespace {
1615 struct PerformSEHFinally final : EHScopeStack::Cleanup {
1616   llvm::Function *OutlinedFinally;
1617   PerformSEHFinally(llvm::Function *OutlinedFinally)
1618       : OutlinedFinally(OutlinedFinally) {}
1619 
1620   void Emit(CodeGenFunction &CGF, Flags F) override {
1621     ASTContext &Context = CGF.getContext();
1622     CodeGenModule &CGM = CGF.CGM;
1623 
1624     CallArgList Args;
1625 
1626     // Compute the two argument values.
1627     QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy};
1628     llvm::Value *FP = nullptr;
1629     // If CFG.IsOutlinedSEHHelper is true, then we are within a finally block.
1630     if (CGF.IsOutlinedSEHHelper) {
1631       FP = &CGF.CurFn->arg_begin()[1];
1632     } else {
1633       llvm::Value *LocalAddrFn =
1634           CGM.getIntrinsic(llvm::Intrinsic::localaddress);
1635       FP = CGF.Builder.CreateCall(LocalAddrFn);
1636     }
1637 
1638     llvm::Value *IsForEH =
1639         llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup());
1640     Args.add(RValue::get(IsForEH), ArgTys[0]);
1641     Args.add(RValue::get(FP), ArgTys[1]);
1642 
1643     // Arrange a two-arg function info and type.
1644     const CGFunctionInfo &FnInfo =
1645         CGM.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, Args);
1646 
1647     auto Callee = CGCallee::forDirect(OutlinedFinally);
1648     CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
1649   }
1650 };
1651 } // end anonymous namespace
1652 
1653 namespace {
1654 /// Find all local variable captures in the statement.
1655 struct CaptureFinder : ConstStmtVisitor<CaptureFinder> {
1656   CodeGenFunction &ParentCGF;
1657   const VarDecl *ParentThis;
1658   llvm::SmallSetVector<const VarDecl *, 4> Captures;
1659   Address SEHCodeSlot = Address::invalid();
1660   CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis)
1661       : ParentCGF(ParentCGF), ParentThis(ParentThis) {}
1662 
1663   // Return true if we need to do any capturing work.
1664   bool foundCaptures() {
1665     return !Captures.empty() || SEHCodeSlot.isValid();
1666   }
1667 
1668   void Visit(const Stmt *S) {
1669     // See if this is a capture, then recurse.
1670     ConstStmtVisitor<CaptureFinder>::Visit(S);
1671     for (const Stmt *Child : S->children())
1672       if (Child)
1673         Visit(Child);
1674   }
1675 
1676   void VisitDeclRefExpr(const DeclRefExpr *E) {
1677     // If this is already a capture, just make sure we capture 'this'.
1678     if (E->refersToEnclosingVariableOrCapture()) {
1679       Captures.insert(ParentThis);
1680       return;
1681     }
1682 
1683     const auto *D = dyn_cast<VarDecl>(E->getDecl());
1684     if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage())
1685       Captures.insert(D);
1686   }
1687 
1688   void VisitCXXThisExpr(const CXXThisExpr *E) {
1689     Captures.insert(ParentThis);
1690   }
1691 
1692   void VisitCallExpr(const CallExpr *E) {
1693     // We only need to add parent frame allocations for these builtins in x86.
1694     if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86)
1695       return;
1696 
1697     unsigned ID = E->getBuiltinCallee();
1698     switch (ID) {
1699     case Builtin::BI__exception_code:
1700     case Builtin::BI_exception_code:
1701       // This is the simple case where we are the outermost finally. All we
1702       // have to do here is make sure we escape this and recover it in the
1703       // outlined handler.
1704       if (!SEHCodeSlot.isValid())
1705         SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back();
1706       break;
1707     }
1708   }
1709 };
1710 } // end anonymous namespace
1711 
1712 Address CodeGenFunction::recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
1713                                                    Address ParentVar,
1714                                                    llvm::Value *ParentFP) {
1715   llvm::CallInst *RecoverCall = nullptr;
1716   CGBuilderTy Builder(*this, AllocaInsertPt);
1717   if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) {
1718     // Mark the variable escaped if nobody else referenced it and compute the
1719     // localescape index.
1720     auto InsertPair = ParentCGF.EscapedLocals.insert(
1721         std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size()));
1722     int FrameEscapeIdx = InsertPair.first->second;
1723     // call i8* @llvm.localrecover(i8* bitcast(@parentFn), i8* %fp, i32 N)
1724     llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration(
1725         &CGM.getModule(), llvm::Intrinsic::localrecover);
1726     llvm::Constant *ParentI8Fn =
1727         llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy);
1728     RecoverCall = Builder.CreateCall(
1729         FrameRecoverFn, {ParentI8Fn, ParentFP,
1730                          llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)});
1731 
1732   } else {
1733     // If the parent didn't have an alloca, we're doing some nested outlining.
1734     // Just clone the existing localrecover call, but tweak the FP argument to
1735     // use our FP value. All other arguments are constants.
1736     auto *ParentRecover =
1737         cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts());
1738     assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover &&
1739            "expected alloca or localrecover in parent LocalDeclMap");
1740     RecoverCall = cast<llvm::CallInst>(ParentRecover->clone());
1741     RecoverCall->setArgOperand(1, ParentFP);
1742     RecoverCall->insertBefore(AllocaInsertPt);
1743   }
1744 
1745   // Bitcast the variable, rename it, and insert it in the local decl map.
1746   llvm::Value *ChildVar =
1747       Builder.CreateBitCast(RecoverCall, ParentVar.getType());
1748   ChildVar->setName(ParentVar.getName());
1749   return Address(ChildVar, ParentVar.getAlignment());
1750 }
1751 
1752 void CodeGenFunction::EmitCapturedLocals(CodeGenFunction &ParentCGF,
1753                                          const Stmt *OutlinedStmt,
1754                                          bool IsFilter) {
1755   // Find all captures in the Stmt.
1756   CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl);
1757   Finder.Visit(OutlinedStmt);
1758 
1759   // We can exit early on x86_64 when there are no captures. We just have to
1760   // save the exception code in filters so that __exception_code() works.
1761   if (!Finder.foundCaptures() &&
1762       CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1763     if (IsFilter)
1764       EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr);
1765     return;
1766   }
1767 
1768   llvm::Value *EntryFP = nullptr;
1769   CGBuilderTy Builder(CGM, AllocaInsertPt);
1770   if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) {
1771     // 32-bit SEH filters need to be careful about FP recovery.  The end of the
1772     // EH registration is passed in as the EBP physical register.  We can
1773     // recover that with llvm.frameaddress(1).
1774     EntryFP = Builder.CreateCall(
1775         CGM.getIntrinsic(llvm::Intrinsic::frameaddress), {Builder.getInt32(1)});
1776   } else {
1777     // Otherwise, for x64 and 32-bit finally functions, the parent FP is the
1778     // second parameter.
1779     auto AI = CurFn->arg_begin();
1780     ++AI;
1781     EntryFP = &*AI;
1782   }
1783 
1784   llvm::Value *ParentFP = EntryFP;
1785   if (IsFilter) {
1786     // Given whatever FP the runtime provided us in EntryFP, recover the true
1787     // frame pointer of the parent function. We only need to do this in filters,
1788     // since finally funclets recover the parent FP for us.
1789     llvm::Function *RecoverFPIntrin =
1790         CGM.getIntrinsic(llvm::Intrinsic::eh_recoverfp);
1791     llvm::Constant *ParentI8Fn =
1792         llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy);
1793     ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP});
1794   }
1795 
1796   // Create llvm.localrecover calls for all captures.
1797   for (const VarDecl *VD : Finder.Captures) {
1798     if (isa<ImplicitParamDecl>(VD)) {
1799       CGM.ErrorUnsupported(VD, "'this' captured by SEH");
1800       CXXThisValue = llvm::UndefValue::get(ConvertTypeForMem(VD->getType()));
1801       continue;
1802     }
1803     if (VD->getType()->isVariablyModifiedType()) {
1804       CGM.ErrorUnsupported(VD, "VLA captured by SEH");
1805       continue;
1806     }
1807     assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) &&
1808            "captured non-local variable");
1809 
1810     // If this decl hasn't been declared yet, it will be declared in the
1811     // OutlinedStmt.
1812     auto I = ParentCGF.LocalDeclMap.find(VD);
1813     if (I == ParentCGF.LocalDeclMap.end())
1814       continue;
1815 
1816     Address ParentVar = I->second;
1817     setAddrOfLocalVar(
1818         VD, recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP));
1819   }
1820 
1821   if (Finder.SEHCodeSlot.isValid()) {
1822     SEHCodeSlotStack.push_back(
1823         recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP));
1824   }
1825 
1826   if (IsFilter)
1827     EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP);
1828 }
1829 
1830 /// Arrange a function prototype that can be called by Windows exception
1831 /// handling personalities. On Win64, the prototype looks like:
1832 /// RetTy func(void *EHPtrs, void *ParentFP);
1833 void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF,
1834                                              bool IsFilter,
1835                                              const Stmt *OutlinedStmt) {
1836   SourceLocation StartLoc = OutlinedStmt->getBeginLoc();
1837 
1838   // Get the mangled function name.
1839   SmallString<128> Name;
1840   {
1841     llvm::raw_svector_ostream OS(Name);
1842     const NamedDecl *ParentSEHFn = ParentCGF.CurSEHParent;
1843     assert(ParentSEHFn && "No CurSEHParent!");
1844     MangleContext &Mangler = CGM.getCXXABI().getMangleContext();
1845     if (IsFilter)
1846       Mangler.mangleSEHFilterExpression(ParentSEHFn, OS);
1847     else
1848       Mangler.mangleSEHFinallyBlock(ParentSEHFn, OS);
1849   }
1850 
1851   FunctionArgList Args;
1852   if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) {
1853     // All SEH finally functions take two parameters. Win64 filters take two
1854     // parameters. Win32 filters take no parameters.
1855     if (IsFilter) {
1856       Args.push_back(ImplicitParamDecl::Create(
1857           getContext(), /*DC=*/nullptr, StartLoc,
1858           &getContext().Idents.get("exception_pointers"),
1859           getContext().VoidPtrTy, ImplicitParamDecl::Other));
1860     } else {
1861       Args.push_back(ImplicitParamDecl::Create(
1862           getContext(), /*DC=*/nullptr, StartLoc,
1863           &getContext().Idents.get("abnormal_termination"),
1864           getContext().UnsignedCharTy, ImplicitParamDecl::Other));
1865     }
1866     Args.push_back(ImplicitParamDecl::Create(
1867         getContext(), /*DC=*/nullptr, StartLoc,
1868         &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy,
1869         ImplicitParamDecl::Other));
1870   }
1871 
1872   QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy;
1873 
1874   const CGFunctionInfo &FnInfo =
1875     CGM.getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args);
1876 
1877   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
1878   llvm::Function *Fn = llvm::Function::Create(
1879       FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule());
1880 
1881   IsOutlinedSEHHelper = true;
1882 
1883   StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
1884                 OutlinedStmt->getBeginLoc(), OutlinedStmt->getBeginLoc());
1885   CurSEHParent = ParentCGF.CurSEHParent;
1886 
1887   CGM.SetLLVMFunctionAttributes(GlobalDecl(), FnInfo, CurFn);
1888   EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter);
1889 }
1890 
1891 /// Create a stub filter function that will ultimately hold the code of the
1892 /// filter expression. The EH preparation passes in LLVM will outline the code
1893 /// from the main function body into this stub.
1894 llvm::Function *
1895 CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
1896                                            const SEHExceptStmt &Except) {
1897   const Expr *FilterExpr = Except.getFilterExpr();
1898   startOutlinedSEHHelper(ParentCGF, true, FilterExpr);
1899 
1900   // Emit the original filter expression, convert to i32, and return.
1901   llvm::Value *R = EmitScalarExpr(FilterExpr);
1902   R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy),
1903                             FilterExpr->getType()->isSignedIntegerType());
1904   Builder.CreateStore(R, ReturnValue);
1905 
1906   FinishFunction(FilterExpr->getEndLoc());
1907 
1908   return CurFn;
1909 }
1910 
1911 llvm::Function *
1912 CodeGenFunction::GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
1913                                             const SEHFinallyStmt &Finally) {
1914   const Stmt *FinallyBlock = Finally.getBlock();
1915   startOutlinedSEHHelper(ParentCGF, false, FinallyBlock);
1916 
1917   // Emit the original filter expression, convert to i32, and return.
1918   EmitStmt(FinallyBlock);
1919 
1920   FinishFunction(FinallyBlock->getEndLoc());
1921 
1922   return CurFn;
1923 }
1924 
1925 void CodeGenFunction::EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
1926                                                llvm::Value *ParentFP,
1927                                                llvm::Value *EntryFP) {
1928   // Get the pointer to the EXCEPTION_POINTERS struct. This is returned by the
1929   // __exception_info intrinsic.
1930   if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1931     // On Win64, the info is passed as the first parameter to the filter.
1932     SEHInfo = &*CurFn->arg_begin();
1933     SEHCodeSlotStack.push_back(
1934         CreateMemTemp(getContext().IntTy, "__exception_code"));
1935   } else {
1936     // On Win32, the EBP on entry to the filter points to the end of an
1937     // exception registration object. It contains 6 32-bit fields, and the info
1938     // pointer is stored in the second field. So, GEP 20 bytes backwards and
1939     // load the pointer.
1940     SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20);
1941     SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo());
1942     SEHInfo = Builder.CreateAlignedLoad(Int8PtrTy, SEHInfo, getPointerAlign());
1943     SEHCodeSlotStack.push_back(recoverAddrOfEscapedLocal(
1944         ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP));
1945   }
1946 
1947   // Save the exception code in the exception slot to unify exception access in
1948   // the filter function and the landing pad.
1949   // struct EXCEPTION_POINTERS {
1950   //   EXCEPTION_RECORD *ExceptionRecord;
1951   //   CONTEXT *ContextRecord;
1952   // };
1953   // int exceptioncode = exception_pointers->ExceptionRecord->ExceptionCode;
1954   llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo();
1955   llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy);
1956   llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo());
1957   llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0);
1958   Rec = Builder.CreateAlignedLoad(Rec, getPointerAlign());
1959   llvm::Value *Code = Builder.CreateAlignedLoad(Rec, getIntAlign());
1960   assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except");
1961   Builder.CreateStore(Code, SEHCodeSlotStack.back());
1962 }
1963 
1964 llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() {
1965   // Sema should diagnose calling this builtin outside of a filter context, but
1966   // don't crash if we screw up.
1967   if (!SEHInfo)
1968     return llvm::UndefValue::get(Int8PtrTy);
1969   assert(SEHInfo->getType() == Int8PtrTy);
1970   return SEHInfo;
1971 }
1972 
1973 llvm::Value *CodeGenFunction::EmitSEHExceptionCode() {
1974   assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except");
1975   return Builder.CreateLoad(SEHCodeSlotStack.back());
1976 }
1977 
1978 llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() {
1979   // Abnormal termination is just the first parameter to the outlined finally
1980   // helper.
1981   auto AI = CurFn->arg_begin();
1982   return Builder.CreateZExt(&*AI, Int32Ty);
1983 }
1984 
1985 void CodeGenFunction::pushSEHCleanup(CleanupKind Kind,
1986                                      llvm::Function *FinallyFunc) {
1987   EHStack.pushCleanup<PerformSEHFinally>(Kind, FinallyFunc);
1988 }
1989 
1990 void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) {
1991   CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true);
1992   if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) {
1993     // Outline the finally block.
1994     llvm::Function *FinallyFunc =
1995         HelperCGF.GenerateSEHFinallyFunction(*this, *Finally);
1996 
1997     // Push a cleanup for __finally blocks.
1998     EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc);
1999     return;
2000   }
2001 
2002   // Otherwise, we must have an __except block.
2003   const SEHExceptStmt *Except = S.getExceptHandler();
2004   assert(Except);
2005   EHCatchScope *CatchScope = EHStack.pushCatch(1);
2006   SEHCodeSlotStack.push_back(
2007       CreateMemTemp(getContext().IntTy, "__exception_code"));
2008 
2009   // If the filter is known to evaluate to 1, then we can use the clause
2010   // "catch i8* null". We can't do this on x86 because the filter has to save
2011   // the exception code.
2012   llvm::Constant *C =
2013     ConstantEmitter(*this).tryEmitAbstract(Except->getFilterExpr(),
2014                                            getContext().IntTy);
2015   if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C &&
2016       C->isOneValue()) {
2017     CatchScope->setCatchAllHandler(0, createBasicBlock("__except"));
2018     return;
2019   }
2020 
2021   // In general, we have to emit an outlined filter function. Use the function
2022   // in place of the RTTI typeinfo global that C++ EH uses.
2023   llvm::Function *FilterFunc =
2024       HelperCGF.GenerateSEHFilterFunction(*this, *Except);
2025   llvm::Constant *OpaqueFunc =
2026       llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy);
2027   CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret"));
2028 }
2029 
2030 void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) {
2031   // Just pop the cleanup if it's a __finally block.
2032   if (S.getFinallyHandler()) {
2033     PopCleanupBlock();
2034     return;
2035   }
2036 
2037   // Otherwise, we must have an __except block.
2038   const SEHExceptStmt *Except = S.getExceptHandler();
2039   assert(Except && "__try must have __finally xor __except");
2040   EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
2041 
2042   // Don't emit the __except block if the __try block lacked invokes.
2043   // TODO: Model unwind edges from instructions, either with iload / istore or
2044   // a try body function.
2045   if (!CatchScope.hasEHBranches()) {
2046     CatchScope.clearHandlerBlocks();
2047     EHStack.popCatch();
2048     SEHCodeSlotStack.pop_back();
2049     return;
2050   }
2051 
2052   // The fall-through block.
2053   llvm::BasicBlock *ContBB = createBasicBlock("__try.cont");
2054 
2055   // We just emitted the body of the __try; jump to the continue block.
2056   if (HaveInsertPoint())
2057     Builder.CreateBr(ContBB);
2058 
2059   // Check if our filter function returned true.
2060   emitCatchDispatchBlock(*this, CatchScope);
2061 
2062   // Grab the block before we pop the handler.
2063   llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block;
2064   EHStack.popCatch();
2065 
2066   EmitBlockAfterUses(CatchPadBB);
2067 
2068   // __except blocks don't get outlined into funclets, so immediately do a
2069   // catchret.
2070   llvm::CatchPadInst *CPI =
2071       cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI());
2072   llvm::BasicBlock *ExceptBB = createBasicBlock("__except");
2073   Builder.CreateCatchRet(CPI, ExceptBB);
2074   EmitBlock(ExceptBB);
2075 
2076   // On Win64, the exception code is returned in EAX. Copy it into the slot.
2077   if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
2078     llvm::Function *SEHCodeIntrin =
2079         CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode);
2080     llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI});
2081     Builder.CreateStore(Code, SEHCodeSlotStack.back());
2082   }
2083 
2084   // Emit the __except body.
2085   EmitStmt(Except->getBlock());
2086 
2087   // End the lifetime of the exception code.
2088   SEHCodeSlotStack.pop_back();
2089 
2090   if (HaveInsertPoint())
2091     Builder.CreateBr(ContBB);
2092 
2093   EmitBlock(ContBB);
2094 }
2095 
2096 void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) {
2097   // If this code is reachable then emit a stop point (if generating
2098   // debug info). We have to do this ourselves because we are on the
2099   // "simple" statement path.
2100   if (HaveInsertPoint())
2101     EmitStopPoint(&S);
2102 
2103   // This must be a __leave from a __finally block, which we warn on and is UB.
2104   // Just emit unreachable.
2105   if (!isSEHTryScope()) {
2106     Builder.CreateUnreachable();
2107     Builder.ClearInsertionPoint();
2108     return;
2109   }
2110 
2111   EmitBranchThroughCleanup(*SEHTryEpilogueStack.back());
2112 }
2113