Coverage Report

Created: 2021-01-23 06:44

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