Coverage Report

Created: 2022-01-18 06:27

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