Coverage Report

Created: 2019-07-24 05:18

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