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