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