Coverage Report

Created: 2021-08-24 07:12

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