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