clang  3.8.0
CGException.cpp
Go to the documentation of this file.
1 //===--- CGException.cpp - Emit LLVM Code for C++ exceptions ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code dealing with C++ exception related code generation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CGCXXABI.h"
16 #include "CGCleanup.h"
17 #include "CGObjCRuntime.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"
24 #include "llvm/IR/CallSite.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/Support/SaveAndRestore.h"
28 
29 using namespace clang;
30 using namespace CodeGen;
31 
32 static llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) {
33  // void __cxa_free_exception(void *thrown_exception);
34 
35  llvm::FunctionType *FTy =
36  llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
37 
38  return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
39 }
40 
41 static llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) {
42  // void __cxa_call_unexpected(void *thrown_exception);
43 
44  llvm::FunctionType *FTy =
45  llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
46 
47  return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
48 }
49 
50 llvm::Constant *CodeGenModule::getTerminateFn() {
51  // void __terminate();
52 
53  llvm::FunctionType *FTy =
54  llvm::FunctionType::get(VoidTy, /*IsVarArgs=*/false);
55 
56  StringRef name;
57 
58  // In C++, use std::terminate().
59  if (getLangOpts().CPlusPlus &&
60  getTarget().getCXXABI().isItaniumFamily()) {
61  name = "_ZSt9terminatev";
62  } else if (getLangOpts().CPlusPlus &&
63  getTarget().getCXXABI().isMicrosoft()) {
64  if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015))
65  name = "__std_terminate";
66  else
67  name = "\01?terminate@@YAXXZ";
68  } else if (getLangOpts().ObjC1 &&
70  name = "objc_terminate";
71  else
72  name = "abort";
73  return CreateRuntimeFunction(FTy, name);
74 }
75 
76 static llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM,
77  StringRef Name) {
78  llvm::FunctionType *FTy =
79  llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
80 
81  return CGM.CreateRuntimeFunction(FTy, Name);
82 }
83 
84 const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr };
85 const EHPersonality
86 EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr };
87 const EHPersonality
88 EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr };
89 const EHPersonality
90 EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr };
91 const EHPersonality
92 EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr };
93 const EHPersonality
94 EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr };
95 const EHPersonality
96 EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr };
97 const EHPersonality
98 EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"};
99 const EHPersonality
100 EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr };
101 const EHPersonality
102 EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr };
103 const EHPersonality
104 EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr };
105 const EHPersonality
106 EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr };
107 const EHPersonality
108 EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr };
109 
110 /// On Win64, use libgcc's SEH personality function. We fall back to dwarf on
111 /// other platforms, unless the user asked for SjLj exceptions.
112 static bool useLibGCCSEHPersonality(const llvm::Triple &T) {
113  return T.isOSWindows() && T.getArch() == llvm::Triple::x86_64;
114 }
115 
116 static const EHPersonality &getCPersonality(const llvm::Triple &T,
117  const LangOptions &L) {
118  if (L.SjLjExceptions)
120  else if (useLibGCCSEHPersonality(T))
122  return EHPersonality::GNU_C;
123 }
124 
125 static const EHPersonality &getObjCPersonality(const llvm::Triple &T,
126  const LangOptions &L) {
127  switch (L.ObjCRuntime.getKind()) {
129  return getCPersonality(T, L);
130  case ObjCRuntime::MacOSX:
131  case ObjCRuntime::iOS:
135  if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
137  // fallthrough
138  case ObjCRuntime::GCC:
139  case ObjCRuntime::ObjFW:
141  }
142  llvm_unreachable("bad runtime kind");
143 }
144 
145 static const EHPersonality &getCXXPersonality(const llvm::Triple &T,
146  const LangOptions &L) {
147  if (L.SjLjExceptions)
149  else if (useLibGCCSEHPersonality(T))
152 }
153 
154 /// Determines the personality function to use when both C++
155 /// and Objective-C exceptions are being caught.
156 static const EHPersonality &getObjCXXPersonality(const llvm::Triple &T,
157  const LangOptions &L) {
158  switch (L.ObjCRuntime.getKind()) {
159  // The ObjC personality defers to the C++ personality for non-ObjC
160  // handlers. Unlike the C++ case, we use the same personality
161  // function on targets using (backend-driven) SJLJ EH.
162  case ObjCRuntime::MacOSX:
163  case ObjCRuntime::iOS:
166 
167  // In the fragile ABI, just use C++ exception handling and hope
168  // they're not doing crazy exception mixing.
170  return getCXXPersonality(T, L);
171 
172  // The GCC runtime's personality function inherently doesn't support
173  // mixed EH. Use the C++ personality just to avoid returning null.
174  case ObjCRuntime::GCC:
175  case ObjCRuntime::ObjFW: // XXX: this will change soon
179  }
180  llvm_unreachable("bad runtime kind");
181 }
182 
183 static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) {
184  if (T.getArch() == llvm::Triple::x86)
187 }
188 
190  const FunctionDecl *FD) {
191  const llvm::Triple &T = CGM.getTarget().getTriple();
192  const LangOptions &L = CGM.getLangOpts();
193 
194  // Functions using SEH get an SEH personality.
195  if (FD && FD->usesSEHTry())
196  return getSEHPersonalityMSVC(T);
197 
198  // Try to pick a personality function that is compatible with MSVC if we're
199  // not compiling Obj-C. Obj-C users better have an Obj-C runtime that supports
200  // the GCC-style personality function.
201  if (T.isWindowsMSVCEnvironment() && !L.ObjC1) {
202  if (L.SjLjExceptions)
204  else
206  }
207 
208  if (L.CPlusPlus && L.ObjC1)
209  return getObjCXXPersonality(T, L);
210  else if (L.CPlusPlus)
211  return getCXXPersonality(T, L);
212  else if (L.ObjC1)
213  return getObjCPersonality(T, L);
214  else
215  return getCPersonality(T, L);
216 }
217 
219  return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(CGF.CurCodeDecl));
220 }
221 
222 static llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
223  const EHPersonality &Personality) {
224  llvm::Constant *Fn =
225  CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
226  Personality.PersonalityFn);
227  return Fn;
228 }
229 
230 static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
231  const EHPersonality &Personality) {
232  llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
233  return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
234 }
235 
236 /// Check whether a landingpad instruction only uses C++ features.
237 static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI) {
238  for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
239  // Look for something that would've been returned by the ObjC
240  // runtime's GetEHType() method.
241  llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
242  if (LPI->isCatch(I)) {
243  // Check if the catch value has the ObjC prefix.
244  if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
245  // ObjC EH selector entries are always global variables with
246  // names starting like this.
247  if (GV->getName().startswith("OBJC_EHTYPE"))
248  return false;
249  } else {
250  // Check if any of the filter values have the ObjC prefix.
251  llvm::Constant *CVal = cast<llvm::Constant>(Val);
252  for (llvm::User::op_iterator
253  II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
254  if (llvm::GlobalVariable *GV =
255  cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
256  // ObjC EH selector entries are always global variables with
257  // names starting like this.
258  if (GV->getName().startswith("OBJC_EHTYPE"))
259  return false;
260  }
261  }
262  }
263  return true;
264 }
265 
266 /// Check whether a personality function could reasonably be swapped
267 /// for a C++ personality function.
268 static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
269  for (llvm::User *U : Fn->users()) {
270  // Conditionally white-list bitcasts.
271  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) {
272  if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
273  if (!PersonalityHasOnlyCXXUses(CE))
274  return false;
275  continue;
276  }
277 
278  // Otherwise it must be a function.
279  llvm::Function *F = dyn_cast<llvm::Function>(U);
280  if (!F) return false;
281 
282  for (auto BB = F->begin(), E = F->end(); BB != E; ++BB) {
283  if (BB->isLandingPad())
284  if (!LandingPadHasOnlyCXXUses(BB->getLandingPadInst()))
285  return false;
286  }
287  }
288 
289  return true;
290 }
291 
292 /// Try to use the C++ personality function in ObjC++. Not doing this
293 /// can cause some incompatibilities with gcc, which is more
294 /// aggressive about only using the ObjC++ personality in a function
295 /// when it really needs it.
296 void CodeGenModule::SimplifyPersonality() {
297  // If we're not in ObjC++ -fexceptions, there's nothing to do.
298  if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions)
299  return;
300 
301  // Both the problem this endeavors to fix and the way the logic
302  // above works is specific to the NeXT runtime.
303  if (!LangOpts.ObjCRuntime.isNeXTFamily())
304  return;
305 
306  const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr);
307  const EHPersonality &CXX =
308  getCXXPersonality(getTarget().getTriple(), LangOpts);
309  if (&ObjCXX == &CXX)
310  return;
311 
312  assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 &&
313  "Different EHPersonalities using the same personality function.");
314 
315  llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn);
316 
317  // Nothing to do if it's unused.
318  if (!Fn || Fn->use_empty()) return;
319 
320  // Can't do the optimization if it has non-C++ uses.
321  if (!PersonalityHasOnlyCXXUses(Fn)) return;
322 
323  // Create the C++ personality function and kill off the old
324  // function.
325  llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
326 
327  // This can happen if the user is screwing with us.
328  if (Fn->getType() != CXXFn->getType()) return;
329 
330  Fn->replaceAllUsesWith(CXXFn);
331  Fn->eraseFromParent();
332 }
333 
334 /// Returns the value to inject into a selector to indicate the
335 /// presence of a catch-all.
336 static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
337  // Possibly we should use @llvm.eh.catch.all.value here.
338  return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
339 }
340 
341 namespace {
342  /// A cleanup to free the exception object if its initialization
343  /// throws.
344  struct FreeException final : EHScopeStack::Cleanup {
345  llvm::Value *exn;
346  FreeException(llvm::Value *exn) : exn(exn) {}
347  void Emit(CodeGenFunction &CGF, Flags flags) override {
349  }
350  };
351 } // end anonymous namespace
352 
353 // Emits an exception expression into the given location. This
354 // differs from EmitAnyExprToMem only in that, if a final copy-ctor
355 // call is required, an exception within that copy ctor causes
356 // std::terminate to be invoked.
358  // Make sure the exception object is cleaned up if there's an
359  // exception during initialization.
360  pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer());
362 
363  // __cxa_allocate_exception returns a void*; we need to cast this
364  // to the appropriate type for the object.
365  llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo();
366  Address typedAddr = Builder.CreateBitCast(addr, ty);
367 
368  // FIXME: this isn't quite right! If there's a final unelided call
369  // to a copy constructor, then according to [except.terminate]p1 we
370  // must call std::terminate() if that constructor throws, because
371  // technically that copy occurs after the exception expression is
372  // evaluated but before the exception is caught. But the best way
373  // to handle that is to teach EmitAggExpr to do the final copy
374  // differently if it can't be elided.
375  EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
376  /*IsInit*/ true);
377 
378  // Deactivate the cleanup block.
379  DeactivateCleanupBlock(cleanup,
380  cast<llvm::Instruction>(typedAddr.getPointer()));
381 }
382 
384  if (!ExceptionSlot)
387 }
388 
390  if (!EHSelectorSlot)
391  EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
393 }
394 
396  return Builder.CreateLoad(getExceptionSlot(), "exn");
397 }
398 
400  return Builder.CreateLoad(getEHSelectorSlot(), "sel");
401 }
402 
404  bool KeepInsertionPoint) {
405  if (const Expr *SubExpr = E->getSubExpr()) {
406  QualType ThrowType = SubExpr->getType();
407  if (ThrowType->isObjCObjectPointerType()) {
408  const Stmt *ThrowStmt = E->getSubExpr();
409  const ObjCAtThrowStmt S(E->getExprLoc(), const_cast<Stmt *>(ThrowStmt));
410  CGM.getObjCRuntime().EmitThrowStmt(*this, S, false);
411  } else {
412  CGM.getCXXABI().emitThrow(*this, E);
413  }
414  } else {
415  CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true);
416  }
417 
418  // throw is an expression, and the expression emitters expect us
419  // to leave ourselves at a valid insertion point.
420  if (KeepInsertionPoint)
421  EmitBlock(createBasicBlock("throw.cont"));
422 }
423 
425  if (!CGM.getLangOpts().CXXExceptions)
426  return;
427 
428  const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
429  if (!FD) {
430  // Check if CapturedDecl is nothrow and create terminate scope for it.
431  if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
432  if (CD->isNothrow())
434  }
435  return;
436  }
437  const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
438  if (!Proto)
439  return;
440 
442  if (isNoexceptExceptionSpec(EST)) {
444  // noexcept functions are simple terminate scopes.
446  }
447  } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
448  // TODO: Revisit exception specifications for the MS ABI. There is a way to
449  // encode these in an object file but MSVC doesn't do anything with it.
450  if (getTarget().getCXXABI().isMicrosoft())
451  return;
452  unsigned NumExceptions = Proto->getNumExceptions();
453  EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
454 
455  for (unsigned I = 0; I != NumExceptions; ++I) {
456  QualType Ty = Proto->getExceptionType(I);
457  QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
458  llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
459  /*ForEH=*/true);
460  Filter->setFilter(I, EHType);
461  }
462  }
463 }
464 
465 /// Emit the dispatch block for a filter scope if necessary.
467  EHFilterScope &filterScope) {
468  llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
469  if (!dispatchBlock) return;
470  if (dispatchBlock->use_empty()) {
471  delete dispatchBlock;
472  return;
473  }
474 
475  CGF.EmitBlockAfterUses(dispatchBlock);
476 
477  // If this isn't a catch-all filter, we need to check whether we got
478  // here because the filter triggered.
479  if (filterScope.getNumFilters()) {
480  // Load the selector value.
481  llvm::Value *selector = CGF.getSelectorFromSlot();
482  llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
483 
484  llvm::Value *zero = CGF.Builder.getInt32(0);
485  llvm::Value *failsFilter =
486  CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
487  CGF.Builder.CreateCondBr(failsFilter, unexpectedBB,
488  CGF.getEHResumeBlock(false));
489 
490  CGF.EmitBlock(unexpectedBB);
491  }
492 
493  // Call __cxa_call_unexpected. This doesn't need to be an invoke
494  // because __cxa_call_unexpected magically filters exceptions
495  // according to the last landing pad the exception was thrown
496  // into. Seriously.
497  llvm::Value *exn = CGF.getExceptionFromSlot();
498  CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn)
499  ->setDoesNotReturn();
500  CGF.Builder.CreateUnreachable();
501 }
502 
504  if (!CGM.getLangOpts().CXXExceptions)
505  return;
506 
507  const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
508  if (!FD) {
509  // Check if CapturedDecl is nothrow and pop terminate scope for it.
510  if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
511  if (CD->isNothrow())
513  }
514  return;
515  }
516  const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
517  if (!Proto)
518  return;
519 
521  if (isNoexceptExceptionSpec(EST)) {
524  }
525  } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
526  // TODO: Revisit exception specifications for the MS ABI. There is a way to
527  // encode these in an object file but MSVC doesn't do anything with it.
528  if (getTarget().getCXXABI().isMicrosoft())
529  return;
530  EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
531  emitFilterDispatchBlock(*this, filterScope);
532  EHStack.popFilter();
533  }
534 }
535 
537  EnterCXXTryStmt(S);
538  EmitStmt(S.getTryBlock());
539  ExitCXXTryStmt(S);
540 }
541 
542 void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
543  unsigned NumHandlers = S.getNumHandlers();
544  EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
545 
546  for (unsigned I = 0; I != NumHandlers; ++I) {
547  const CXXCatchStmt *C = S.getHandler(I);
548 
549  llvm::BasicBlock *Handler = createBasicBlock("catch");
550  if (C->getExceptionDecl()) {
551  // FIXME: Dropping the reference type on the type into makes it
552  // impossible to correctly implement catch-by-reference
553  // semantics for pointers. Unfortunately, this is what all
554  // existing compilers do, and it's not clear that the standard
555  // personality routine is capable of doing this right. See C++ DR 388:
556  // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
557  Qualifiers CaughtTypeQuals;
559  C->getCaughtType().getNonReferenceType(), CaughtTypeQuals);
560 
561  CatchTypeInfo TypeInfo{nullptr, 0};
562  if (CaughtType->isObjCObjectPointerType())
563  TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType);
564  else
566  CaughtType, C->getCaughtType());
567  CatchScope->setHandler(I, TypeInfo, Handler);
568  } else {
569  // No exception decl indicates '...', a catch-all.
570  CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler);
571  }
572  }
573 }
574 
575 llvm::BasicBlock *
577  if (EHPersonality::get(*this).usesFuncletPads())
578  return getMSVCDispatchBlock(si);
579 
580  // The dispatch block for the end of the scope chain is a block that
581  // just resumes unwinding.
582  if (si == EHStack.stable_end())
583  return getEHResumeBlock(true);
584 
585  // Otherwise, we should look at the actual scope.
586  EHScope &scope = *EHStack.find(si);
587 
588  llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
589  if (!dispatchBlock) {
590  switch (scope.getKind()) {
591  case EHScope::Catch: {
592  // Apply a special case to a single catch-all.
593  EHCatchScope &catchScope = cast<EHCatchScope>(scope);
594  if (catchScope.getNumHandlers() == 1 &&
595  catchScope.getHandler(0).isCatchAll()) {
596  dispatchBlock = catchScope.getHandler(0).Block;
597 
598  // Otherwise, make a dispatch block.
599  } else {
600  dispatchBlock = createBasicBlock("catch.dispatch");
601  }
602  break;
603  }
604 
605  case EHScope::Cleanup:
606  dispatchBlock = createBasicBlock("ehcleanup");
607  break;
608 
609  case EHScope::Filter:
610  dispatchBlock = createBasicBlock("filter.dispatch");
611  break;
612 
613  case EHScope::Terminate:
614  dispatchBlock = getTerminateHandler();
615  break;
616 
617  case EHScope::PadEnd:
618  llvm_unreachable("PadEnd unnecessary for Itanium!");
619  }
620  scope.setCachedEHDispatchBlock(dispatchBlock);
621  }
622  return dispatchBlock;
623 }
624 
625 llvm::BasicBlock *
627  // Returning nullptr indicates that the previous dispatch block should unwind
628  // to caller.
629  if (SI == EHStack.stable_end())
630  return nullptr;
631 
632  // Otherwise, we should look at the actual scope.
633  EHScope &EHS = *EHStack.find(SI);
634 
635  llvm::BasicBlock *DispatchBlock = EHS.getCachedEHDispatchBlock();
636  if (DispatchBlock)
637  return DispatchBlock;
638 
639  if (EHS.getKind() == EHScope::Terminate)
640  DispatchBlock = getTerminateHandler();
641  else
642  DispatchBlock = createBasicBlock();
643  CGBuilderTy Builder(*this, DispatchBlock);
644 
645  switch (EHS.getKind()) {
646  case EHScope::Catch:
647  DispatchBlock->setName("catch.dispatch");
648  break;
649 
650  case EHScope::Cleanup:
651  DispatchBlock->setName("ehcleanup");
652  break;
653 
654  case EHScope::Filter:
655  llvm_unreachable("exception specifications not handled yet!");
656 
657  case EHScope::Terminate:
658  DispatchBlock->setName("terminate");
659  break;
660 
661  case EHScope::PadEnd:
662  llvm_unreachable("PadEnd dispatch block missing!");
663  }
664  EHS.setCachedEHDispatchBlock(DispatchBlock);
665  return DispatchBlock;
666 }
667 
668 /// Check whether this is a non-EH scope, i.e. a scope which doesn't
669 /// affect exception handling. Currently, the only non-EH scopes are
670 /// normal-only cleanup scopes.
671 static bool isNonEHScope(const EHScope &S) {
672  switch (S.getKind()) {
673  case EHScope::Cleanup:
674  return !cast<EHCleanupScope>(S).isEHCleanup();
675  case EHScope::Filter:
676  case EHScope::Catch:
677  case EHScope::Terminate:
678  case EHScope::PadEnd:
679  return false;
680  }
681 
682  llvm_unreachable("Invalid EHScope Kind!");
683 }
684 
686  assert(EHStack.requiresLandingPad());
687  assert(!EHStack.empty());
688 
689  // If exceptions are disabled, there are usually no landingpads. However, when
690  // SEH is enabled, functions using SEH still get landingpads.
691  const LangOptions &LO = CGM.getLangOpts();
692  if (!LO.Exceptions) {
693  if (!LO.Borland && !LO.MicrosoftExt)
694  return nullptr;
696  return nullptr;
697  }
698 
699  // Check the innermost scope for a cached landing pad. If this is
700  // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
701  llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
702  if (LP) return LP;
703 
704  const EHPersonality &Personality = EHPersonality::get(*this);
705 
706  if (!CurFn->hasPersonalityFn())
707  CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality));
708 
709  if (Personality.usesFuncletPads()) {
710  // We don't need separate landing pads in the funclet model.
712  } else {
713  // Build the landing pad for this scope.
714  LP = EmitLandingPad();
715  }
716 
717  assert(LP);
718 
719  // Cache the landing pad on the innermost scope. If this is a
720  // non-EH scope, cache the landing pad on the enclosing scope, too.
721  for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
722  ir->setCachedLandingPad(LP);
723  if (!isNonEHScope(*ir)) break;
724  }
725 
726  return LP;
727 }
728 
729 llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
730  assert(EHStack.requiresLandingPad());
731 
732  EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
733  switch (innermostEHScope.getKind()) {
734  case EHScope::Terminate:
735  return getTerminateLandingPad();
736 
737  case EHScope::PadEnd:
738  llvm_unreachable("PadEnd unnecessary for Itanium!");
739 
740  case EHScope::Catch:
741  case EHScope::Cleanup:
742  case EHScope::Filter:
743  if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
744  return lpad;
745  }
746 
747  // Save the current IR generation state.
748  CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
749  auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation);
750 
751  // Create and configure the landing pad.
752  llvm::BasicBlock *lpad = createBasicBlock("lpad");
753  EmitBlock(lpad);
754 
755  llvm::LandingPadInst *LPadInst = Builder.CreateLandingPad(
756  llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr), 0);
757 
758  llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
760  llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
762 
763  // Save the exception pointer. It's safe to use a single exception
764  // pointer per function because EH cleanups can never have nested
765  // try/catches.
766  // Build the landingpad instruction.
767 
768  // Accumulate all the handlers in scope.
769  bool hasCatchAll = false;
770  bool hasCleanup = false;
771  bool hasFilter = false;
772  SmallVector<llvm::Value*, 4> filterTypes;
773  llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
774  for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E;
775  ++I) {
776 
777  switch (I->getKind()) {
778  case EHScope::Cleanup:
779  // If we have a cleanup, remember that.
780  hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
781  continue;
782 
783  case EHScope::Filter: {
784  assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
785  assert(!hasCatchAll && "EH filter reached after catch-all");
786 
787  // Filter scopes get added to the landingpad in weird ways.
788  EHFilterScope &filter = cast<EHFilterScope>(*I);
789  hasFilter = true;
790 
791  // Add all the filter values.
792  for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
793  filterTypes.push_back(filter.getFilter(i));
794  goto done;
795  }
796 
797  case EHScope::Terminate:
798  // Terminate scopes are basically catch-alls.
799  assert(!hasCatchAll);
800  hasCatchAll = true;
801  goto done;
802 
803  case EHScope::Catch:
804  break;
805 
806  case EHScope::PadEnd:
807  llvm_unreachable("PadEnd unnecessary for Itanium!");
808  }
809 
810  EHCatchScope &catchScope = cast<EHCatchScope>(*I);
811  for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
812  EHCatchScope::Handler handler = catchScope.getHandler(hi);
813  assert(handler.Type.Flags == 0 &&
814  "landingpads do not support catch handler flags");
815 
816  // If this is a catch-all, register that and abort.
817  if (!handler.Type.RTTI) {
818  assert(!hasCatchAll);
819  hasCatchAll = true;
820  goto done;
821  }
822 
823  // Check whether we already have a handler for this type.
824  if (catchTypes.insert(handler.Type.RTTI).second)
825  // If not, add it directly to the landingpad.
826  LPadInst->addClause(handler.Type.RTTI);
827  }
828  }
829 
830  done:
831  // If we have a catch-all, add null to the landingpad.
832  assert(!(hasCatchAll && hasFilter));
833  if (hasCatchAll) {
834  LPadInst->addClause(getCatchAllValue(*this));
835 
836  // If we have an EH filter, we need to add those handlers in the
837  // right place in the landingpad, which is to say, at the end.
838  } else if (hasFilter) {
839  // Create a filter expression: a constant array indicating which filter
840  // types there are. The personality routine only lands here if the filter
841  // doesn't match.
843  llvm::ArrayType *AType =
844  llvm::ArrayType::get(!filterTypes.empty() ?
845  filterTypes[0]->getType() : Int8PtrTy,
846  filterTypes.size());
847 
848  for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
849  Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
850  llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
851  LPadInst->addClause(FilterArray);
852 
853  // Also check whether we need a cleanup.
854  if (hasCleanup)
855  LPadInst->setCleanup(true);
856 
857  // Otherwise, signal that we at least have cleanups.
858  } else if (hasCleanup) {
859  LPadInst->setCleanup(true);
860  }
861 
862  assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
863  "landingpad instruction has no clauses!");
864 
865  // Tell the backend how to generate the landing pad.
867 
868  // Restore the old IR generation state.
869  Builder.restoreIP(savedIP);
870 
871  return lpad;
872 }
873 
874 static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) {
875  llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
876  assert(DispatchBlock);
877 
878  CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP();
879  CGF.EmitBlockAfterUses(DispatchBlock);
880 
881  llvm::Value *ParentPad = CGF.CurrentFuncletPad;
882  if (!ParentPad)
883  ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext());
884  llvm::BasicBlock *UnwindBB =
885  CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope());
886 
887  unsigned NumHandlers = CatchScope.getNumHandlers();
888  llvm::CatchSwitchInst *CatchSwitch =
889  CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers);
890 
891  // Test against each of the exception types we claim to catch.
892  for (unsigned I = 0; I < NumHandlers; ++I) {
893  const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
894 
895  CatchTypeInfo TypeInfo = Handler.Type;
896  if (!TypeInfo.RTTI)
897  TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
898 
899  CGF.Builder.SetInsertPoint(Handler.Block);
900 
901  if (EHPersonality::get(CGF).isMSVCXXPersonality()) {
902  CGF.Builder.CreateCatchPad(
903  CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags),
904  llvm::Constant::getNullValue(CGF.VoidPtrTy)});
905  } else {
906  CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI});
907  }
908 
909  CatchSwitch->addHandler(Handler.Block);
910  }
911  CGF.Builder.restoreIP(SavedIP);
912 }
913 
914 /// Emit the structure of the dispatch block for the given catch scope.
915 /// It is an invariant that the dispatch block already exists.
917  EHCatchScope &catchScope) {
918  if (EHPersonality::get(CGF).usesFuncletPads())
919  return emitCatchPadBlock(CGF, catchScope);
920 
921  llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
922  assert(dispatchBlock);
923 
924  // If there's only a single catch-all, getEHDispatchBlock returned
925  // that catch-all as the dispatch block.
926  if (catchScope.getNumHandlers() == 1 &&
927  catchScope.getHandler(0).isCatchAll()) {
928  assert(dispatchBlock == catchScope.getHandler(0).Block);
929  return;
930  }
931 
932  CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
933  CGF.EmitBlockAfterUses(dispatchBlock);
934 
935  // Select the right handler.
936  llvm::Value *llvm_eh_typeid_for =
937  CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
938 
939  // Load the selector value.
940  llvm::Value *selector = CGF.getSelectorFromSlot();
941 
942  // Test against each of the exception types we claim to catch.
943  for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
944  assert(i < e && "ran off end of handlers!");
945  const EHCatchScope::Handler &handler = catchScope.getHandler(i);
946 
947  llvm::Value *typeValue = handler.Type.RTTI;
948  assert(handler.Type.Flags == 0 &&
949  "landingpads do not support catch handler flags");
950  assert(typeValue && "fell into catch-all case!");
951  typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
952 
953  // Figure out the next block.
954  bool nextIsEnd;
955  llvm::BasicBlock *nextBlock;
956 
957  // If this is the last handler, we're at the end, and the next
958  // block is the block for the enclosing EH scope.
959  if (i + 1 == e) {
960  nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
961  nextIsEnd = true;
962 
963  // If the next handler is a catch-all, we're at the end, and the
964  // next block is that handler.
965  } else if (catchScope.getHandler(i+1).isCatchAll()) {
966  nextBlock = catchScope.getHandler(i+1).Block;
967  nextIsEnd = true;
968 
969  // Otherwise, we're not at the end and we need a new block.
970  } else {
971  nextBlock = CGF.createBasicBlock("catch.fallthrough");
972  nextIsEnd = false;
973  }
974 
975  // Figure out the catch type's index in the LSDA's type table.
976  llvm::CallInst *typeIndex =
977  CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
978  typeIndex->setDoesNotThrow();
979 
980  llvm::Value *matchesTypeIndex =
981  CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
982  CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
983 
984  // If the next handler is a catch-all, we're completely done.
985  if (nextIsEnd) {
986  CGF.Builder.restoreIP(savedIP);
987  return;
988  }
989  // Otherwise we need to emit and continue at that block.
990  CGF.EmitBlock(nextBlock);
991  }
992 }
993 
995  EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
996  if (catchScope.hasEHBranches())
997  emitCatchDispatchBlock(*this, catchScope);
998  EHStack.popCatch();
999 }
1000 
1001 void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
1002  unsigned NumHandlers = S.getNumHandlers();
1003  EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1004  assert(CatchScope.getNumHandlers() == NumHandlers);
1005 
1006  // If the catch was not required, bail out now.
1007  if (!CatchScope.hasEHBranches()) {
1008  CatchScope.clearHandlerBlocks();
1009  EHStack.popCatch();
1010  return;
1011  }
1012 
1013  // Emit the structure of the EH dispatch for this catch.
1014  emitCatchDispatchBlock(*this, CatchScope);
1015 
1016  // Copy the handler blocks off before we pop the EH stack. Emitting
1017  // the handlers might scribble on this memory.
1019  CatchScope.begin(), CatchScope.begin() + NumHandlers);
1020 
1021  EHStack.popCatch();
1022 
1023  // The fall-through block.
1024  llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
1025 
1026  // We just emitted the body of the try; jump to the continue block.
1027  if (HaveInsertPoint())
1028  Builder.CreateBr(ContBB);
1029 
1030  // Determine if we need an implicit rethrow for all these catch handlers;
1031  // see the comment below.
1032  bool doImplicitRethrow = false;
1033  if (IsFnTryBlock)
1034  doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1035  isa<CXXConstructorDecl>(CurCodeDecl);
1036 
1037  // Perversely, we emit the handlers backwards precisely because we
1038  // want them to appear in source order. In all of these cases, the
1039  // catch block will have exactly one predecessor, which will be a
1040  // particular block in the catch dispatch. However, in the case of
1041  // a catch-all, one of the dispatch blocks will branch to two
1042  // different handlers, and EmitBlockAfterUses will cause the second
1043  // handler to be moved before the first.
1044  for (unsigned I = NumHandlers; I != 0; --I) {
1045  llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1046  EmitBlockAfterUses(CatchBlock);
1047 
1048  // Catch the exception if this isn't a catch-all.
1049  const CXXCatchStmt *C = S.getHandler(I-1);
1050 
1051  // Enter a cleanup scope, including the catch variable and the
1052  // end-catch.
1053  RunCleanupsScope CatchScope(*this);
1054 
1055  // Initialize the catch variable and set up the cleanups.
1056  SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1058  CGM.getCXXABI().emitBeginCatch(*this, C);
1059 
1060  // Emit the PGO counter increment.
1062 
1063  // Perform the body of the catch.
1064  EmitStmt(C->getHandlerBlock());
1065 
1066  // [except.handle]p11:
1067  // The currently handled exception is rethrown if control
1068  // reaches the end of a handler of the function-try-block of a
1069  // constructor or destructor.
1070 
1071  // It is important that we only do this on fallthrough and not on
1072  // return. Note that it's illegal to put a return in a
1073  // constructor function-try-block's catch handler (p14), so this
1074  // really only applies to destructors.
1075  if (doImplicitRethrow && HaveInsertPoint()) {
1076  CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false);
1077  Builder.CreateUnreachable();
1078  Builder.ClearInsertionPoint();
1079  }
1080 
1081  // Fall out through the catch cleanups.
1082  CatchScope.ForceCleanup();
1083 
1084  // Branch out of the try.
1085  if (HaveInsertPoint())
1086  Builder.CreateBr(ContBB);
1087  }
1088 
1089  EmitBlock(ContBB);
1091 }
1092 
1093 namespace {
1094  struct CallEndCatchForFinally final : EHScopeStack::Cleanup {
1095  llvm::Value *ForEHVar;
1096  llvm::Value *EndCatchFn;
1097  CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1098  : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1099 
1100  void Emit(CodeGenFunction &CGF, Flags flags) override {
1101  llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1102  llvm::BasicBlock *CleanupContBB =
1103  CGF.createBasicBlock("finally.cleanup.cont");
1104 
1105  llvm::Value *ShouldEndCatch =
1106  CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch");
1107  CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1108  CGF.EmitBlock(EndCatchBB);
1109  CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw
1110  CGF.EmitBlock(CleanupContBB);
1111  }
1112  };
1113 
1114  struct PerformFinally final : EHScopeStack::Cleanup {
1115  const Stmt *Body;
1116  llvm::Value *ForEHVar;
1117  llvm::Value *EndCatchFn;
1118  llvm::Value *RethrowFn;
1119  llvm::Value *SavedExnVar;
1120 
1121  PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1122  llvm::Value *EndCatchFn,
1123  llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1124  : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1125  RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1126 
1127  void Emit(CodeGenFunction &CGF, Flags flags) override {
1128  // Enter a cleanup to call the end-catch function if one was provided.
1129  if (EndCatchFn)
1130  CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1131  ForEHVar, EndCatchFn);
1132 
1133  // Save the current cleanup destination in case there are
1134  // cleanups in the finally block.
1135  llvm::Value *SavedCleanupDest =
1137  "cleanup.dest.saved");
1138 
1139  // Emit the finally block.
1140  CGF.EmitStmt(Body);
1141 
1142  // If the end of the finally is reachable, check whether this was
1143  // for EH. If so, rethrow.
1144  if (CGF.HaveInsertPoint()) {
1145  llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1146  llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1147 
1148  llvm::Value *ShouldRethrow =
1149  CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow");
1150  CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1151 
1152  CGF.EmitBlock(RethrowBB);
1153  if (SavedExnVar) {
1154  CGF.EmitRuntimeCallOrInvoke(RethrowFn,
1155  CGF.Builder.CreateAlignedLoad(SavedExnVar, CGF.getPointerAlign()));
1156  } else {
1157  CGF.EmitRuntimeCallOrInvoke(RethrowFn);
1158  }
1159  CGF.Builder.CreateUnreachable();
1160 
1161  CGF.EmitBlock(ContBB);
1162 
1163  // Restore the cleanup destination.
1164  CGF.Builder.CreateStore(SavedCleanupDest,
1165  CGF.getNormalCleanupDestSlot());
1166  }
1167 
1168  // Leave the end-catch cleanup. As an optimization, pretend that
1169  // the fallthrough path was inaccessible; we've dynamically proven
1170  // that we're not in the EH case along that path.
1171  if (EndCatchFn) {
1172  CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1173  CGF.PopCleanupBlock();
1174  CGF.Builder.restoreIP(SavedIP);
1175  }
1176 
1177  // Now make sure we actually have an insertion point or the
1178  // cleanup gods will hate us.
1179  CGF.EnsureInsertPoint();
1180  }
1181  };
1182 } // end anonymous namespace
1183 
1184 /// Enters a finally block for an implementation using zero-cost
1185 /// exceptions. This is mostly general, but hard-codes some
1186 /// language/ABI-specific behavior in the catch-all sections.
1188  const Stmt *body,
1189  llvm::Constant *beginCatchFn,
1190  llvm::Constant *endCatchFn,
1191  llvm::Constant *rethrowFn) {
1192  assert((beginCatchFn != nullptr) == (endCatchFn != nullptr) &&
1193  "begin/end catch functions not paired");
1194  assert(rethrowFn && "rethrow function is required");
1195 
1196  BeginCatchFn = beginCatchFn;
1197 
1198  // The rethrow function has one of the following two types:
1199  // void (*)()
1200  // void (*)(void*)
1201  // In the latter case we need to pass it the exception object.
1202  // But we can't use the exception slot because the @finally might
1203  // have a landing pad (which would overwrite the exception slot).
1204  llvm::FunctionType *rethrowFnTy =
1205  cast<llvm::FunctionType>(
1206  cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1207  SavedExnVar = nullptr;
1208  if (rethrowFnTy->getNumParams())
1209  SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
1210 
1211  // A finally block is a statement which must be executed on any edge
1212  // out of a given scope. Unlike a cleanup, the finally block may
1213  // contain arbitrary control flow leading out of itself. In
1214  // addition, finally blocks should always be executed, even if there
1215  // are no catch handlers higher on the stack. Therefore, we
1216  // surround the protected scope with a combination of a normal
1217  // cleanup (to catch attempts to break out of the block via normal
1218  // control flow) and an EH catch-all (semantically "outside" any try
1219  // statement to which the finally block might have been attached).
1220  // The finally block itself is generated in the context of a cleanup
1221  // which conditionally leaves the catch-all.
1222 
1223  // Jump destination for performing the finally block on an exception
1224  // edge. We'll never actually reach this block, so unreachable is
1225  // fine.
1226  RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
1227 
1228  // Whether the finally block is being executed for EH purposes.
1229  ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1230  CGF.Builder.CreateFlagStore(false, ForEHVar);
1231 
1232  // Enter a normal cleanup which will perform the @finally block.
1233  CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1234  ForEHVar, endCatchFn,
1235  rethrowFn, SavedExnVar);
1236 
1237  // Enter a catch-all scope.
1238  llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1239  EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1240  catchScope->setCatchAllHandler(0, catchBB);
1241 }
1242 
1244  // Leave the finally catch-all.
1245  EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1246  llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
1247 
1248  CGF.popCatchScope();
1249 
1250  // If there are any references to the catch-all block, emit it.
1251  if (catchBB->use_empty()) {
1252  delete catchBB;
1253  } else {
1254  CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1255  CGF.EmitBlock(catchBB);
1256 
1257  llvm::Value *exn = nullptr;
1258 
1259  // If there's a begin-catch function, call it.
1260  if (BeginCatchFn) {
1261  exn = CGF.getExceptionFromSlot();
1262  CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn);
1263  }
1264 
1265  // If we need to remember the exception pointer to rethrow later, do so.
1266  if (SavedExnVar) {
1267  if (!exn) exn = CGF.getExceptionFromSlot();
1268  CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign());
1269  }
1270 
1271  // Tell the cleanups in the finally block that we're do this for EH.
1272  CGF.Builder.CreateFlagStore(true, ForEHVar);
1273 
1274  // Thread a jump through the finally cleanup.
1275  CGF.EmitBranchThroughCleanup(RethrowDest);
1276 
1277  CGF.Builder.restoreIP(savedIP);
1278  }
1279 
1280  // Finally, leave the @finally cleanup.
1281  CGF.PopCleanupBlock();
1282 }
1283 
1285  if (TerminateLandingPad)
1286  return TerminateLandingPad;
1287 
1288  CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1289 
1290  // This will get inserted at the end of the function.
1291  TerminateLandingPad = createBasicBlock("terminate.lpad");
1292  Builder.SetInsertPoint(TerminateLandingPad);
1293 
1294  // Tell the backend that this is a landing pad.
1295  const EHPersonality &Personality = EHPersonality::get(*this);
1296 
1297  if (!CurFn->hasPersonalityFn())
1298  CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality));
1299 
1300  llvm::LandingPadInst *LPadInst = Builder.CreateLandingPad(
1301  llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr), 0);
1302  LPadInst->addClause(getCatchAllValue(*this));
1303 
1304  llvm::Value *Exn = nullptr;
1305  if (getLangOpts().CPlusPlus)
1306  Exn = Builder.CreateExtractValue(LPadInst, 0);
1307  llvm::CallInst *terminateCall =
1309  terminateCall->setDoesNotReturn();
1310  Builder.CreateUnreachable();
1311 
1312  // Restore the saved insertion state.
1313  Builder.restoreIP(SavedIP);
1314 
1315  return TerminateLandingPad;
1316 }
1317 
1319  if (TerminateHandler)
1320  return TerminateHandler;
1321 
1322  CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1323 
1324  // Set up the terminate handler. This block is inserted at the very
1325  // end of the function by FinishFunction.
1326  TerminateHandler = createBasicBlock("terminate.handler");
1327  Builder.SetInsertPoint(TerminateHandler);
1328  llvm::Value *Exn = nullptr;
1329  if (EHPersonality::get(*this).usesFuncletPads()) {
1330  llvm::Value *ParentPad = CurrentFuncletPad;
1331  if (!ParentPad)
1332  ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
1333  Builder.CreateCleanupPad(ParentPad);
1334  } else {
1335  if (getLangOpts().CPlusPlus)
1336  Exn = getExceptionFromSlot();
1337  }
1338  llvm::CallInst *terminateCall =
1340  terminateCall->setDoesNotReturn();
1341  Builder.CreateUnreachable();
1342 
1343  // Restore the saved insertion state.
1344  Builder.restoreIP(SavedIP);
1345 
1346  return TerminateHandler;
1347 }
1348 
1349 llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) {
1350  if (EHResumeBlock) return EHResumeBlock;
1351 
1352  CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1353 
1354  // We emit a jump to a notional label at the outermost unwind state.
1355  EHResumeBlock = createBasicBlock("eh.resume");
1356  Builder.SetInsertPoint(EHResumeBlock);
1357 
1358  const EHPersonality &Personality = EHPersonality::get(*this);
1359 
1360  // This can always be a call because we necessarily didn't find
1361  // anything on the EH stack which needs our help.
1362  const char *RethrowName = Personality.CatchallRethrowFn;
1363  if (RethrowName != nullptr && !isCleanup) {
1365  getExceptionFromSlot())->setDoesNotReturn();
1366  Builder.CreateUnreachable();
1367  Builder.restoreIP(SavedIP);
1368  return EHResumeBlock;
1369  }
1370 
1371  // Recreate the landingpad's return value for the 'resume' instruction.
1374 
1375  llvm::Type *LPadType = llvm::StructType::get(Exn->getType(),
1376  Sel->getType(), nullptr);
1377  llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1378  LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1379  LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1380 
1381  Builder.CreateResume(LPadVal);
1382  Builder.restoreIP(SavedIP);
1383  return EHResumeBlock;
1384 }
1385 
1387  EnterSEHTryStmt(S);
1388  {
1389  JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave");
1390 
1391  SEHTryEpilogueStack.push_back(&TryExit);
1392  EmitStmt(S.getTryBlock());
1393  SEHTryEpilogueStack.pop_back();
1394 
1395  if (!TryExit.getBlock()->use_empty())
1396  EmitBlock(TryExit.getBlock(), /*IsFinished=*/true);
1397  else
1398  delete TryExit.getBlock();
1399  }
1400  ExitSEHTryStmt(S);
1401 }
1402 
1403 namespace {
1404 struct PerformSEHFinally final : EHScopeStack::Cleanup {
1405  llvm::Function *OutlinedFinally;
1406  PerformSEHFinally(llvm::Function *OutlinedFinally)
1407  : OutlinedFinally(OutlinedFinally) {}
1408 
1409  void Emit(CodeGenFunction &CGF, Flags F) override {
1410  ASTContext &Context = CGF.getContext();
1411  CodeGenModule &CGM = CGF.CGM;
1412 
1413  CallArgList Args;
1414 
1415  // Compute the two argument values.
1416  QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy};
1417  llvm::Value *LocalAddrFn = CGM.getIntrinsic(llvm::Intrinsic::localaddress);
1418  llvm::Value *FP = CGF.Builder.CreateCall(LocalAddrFn);
1419  llvm::Value *IsForEH =
1420  llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup());
1421  Args.add(RValue::get(IsForEH), ArgTys[0]);
1422  Args.add(RValue::get(FP), ArgTys[1]);
1423 
1424  // Arrange a two-arg function info and type.
1426  const auto *FPT = cast<FunctionProtoType>(
1427  Context.getFunctionType(Context.VoidTy, ArgTys, EPI));
1428  const CGFunctionInfo &FnInfo =
1429  CGM.getTypes().arrangeFreeFunctionCall(Args, FPT,
1430  /*chainCall=*/false);
1431 
1432  CGF.EmitCall(FnInfo, OutlinedFinally, ReturnValueSlot(), Args);
1433  }
1434 };
1435 } // end anonymous namespace
1436 
1437 namespace {
1438 /// Find all local variable captures in the statement.
1439 struct CaptureFinder : ConstStmtVisitor<CaptureFinder> {
1440  CodeGenFunction &ParentCGF;
1441  const VarDecl *ParentThis;
1443  Address SEHCodeSlot = Address::invalid();
1444  CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis)
1445  : ParentCGF(ParentCGF), ParentThis(ParentThis) {}
1446 
1447  // Return true if we need to do any capturing work.
1448  bool foundCaptures() {
1449  return !Captures.empty() || SEHCodeSlot.isValid();
1450  }
1451 
1452  void Visit(const Stmt *S) {
1453  // See if this is a capture, then recurse.
1455  for (const Stmt *Child : S->children())
1456  if (Child)
1457  Visit(Child);
1458  }
1459 
1460  void VisitDeclRefExpr(const DeclRefExpr *E) {
1461  // If this is already a capture, just make sure we capture 'this'.
1463  Captures.insert(ParentThis);
1464  return;
1465  }
1466 
1467  const auto *D = dyn_cast<VarDecl>(E->getDecl());
1468  if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage())
1469  Captures.insert(D);
1470  }
1471 
1472  void VisitCXXThisExpr(const CXXThisExpr *E) {
1473  Captures.insert(ParentThis);
1474  }
1475 
1476  void VisitCallExpr(const CallExpr *E) {
1477  // We only need to add parent frame allocations for these builtins in x86.
1478  if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86)
1479  return;
1480 
1481  unsigned ID = E->getBuiltinCallee();
1482  switch (ID) {
1483  case Builtin::BI__exception_code:
1484  case Builtin::BI_exception_code:
1485  // This is the simple case where we are the outermost finally. All we
1486  // have to do here is make sure we escape this and recover it in the
1487  // outlined handler.
1488  if (!SEHCodeSlot.isValid())
1489  SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back();
1490  break;
1491  }
1492  }
1493 };
1494 } // end anonymous namespace
1495 
1497  Address ParentVar,
1498  llvm::Value *ParentFP) {
1499  llvm::CallInst *RecoverCall = nullptr;
1501  if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) {
1502  // Mark the variable escaped if nobody else referenced it and compute the
1503  // localescape index.
1504  auto InsertPair = ParentCGF.EscapedLocals.insert(
1505  std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size()));
1506  int FrameEscapeIdx = InsertPair.first->second;
1507  // call i8* @llvm.localrecover(i8* bitcast(@parentFn), i8* %fp, i32 N)
1508  llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration(
1509  &CGM.getModule(), llvm::Intrinsic::localrecover);
1510  llvm::Constant *ParentI8Fn =
1511  llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy);
1512  RecoverCall = Builder.CreateCall(
1513  FrameRecoverFn, {ParentI8Fn, ParentFP,
1514  llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)});
1515 
1516  } else {
1517  // If the parent didn't have an alloca, we're doing some nested outlining.
1518  // Just clone the existing localrecover call, but tweak the FP argument to
1519  // use our FP value. All other arguments are constants.
1520  auto *ParentRecover =
1521  cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts());
1522  assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover &&
1523  "expected alloca or localrecover in parent LocalDeclMap");
1524  RecoverCall = cast<llvm::CallInst>(ParentRecover->clone());
1525  RecoverCall->setArgOperand(1, ParentFP);
1526  RecoverCall->insertBefore(AllocaInsertPt);
1527  }
1528 
1529  // Bitcast the variable, rename it, and insert it in the local decl map.
1530  llvm::Value *ChildVar =
1531  Builder.CreateBitCast(RecoverCall, ParentVar.getType());
1532  ChildVar->setName(ParentVar.getName());
1533  return Address(ChildVar, ParentVar.getAlignment());
1534 }
1535 
1537  const Stmt *OutlinedStmt,
1538  bool IsFilter) {
1539  // Find all captures in the Stmt.
1540  CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl);
1541  Finder.Visit(OutlinedStmt);
1542 
1543  // We can exit early on x86_64 when there are no captures. We just have to
1544  // save the exception code in filters so that __exception_code() works.
1545  if (!Finder.foundCaptures() &&
1546  CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1547  if (IsFilter)
1548  EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr);
1549  return;
1550  }
1551 
1552  llvm::Value *EntryFP = nullptr;
1554  if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) {
1555  // 32-bit SEH filters need to be careful about FP recovery. The end of the
1556  // EH registration is passed in as the EBP physical register. We can
1557  // recover that with llvm.frameaddress(1).
1558  EntryFP = Builder.CreateCall(
1559  CGM.getIntrinsic(llvm::Intrinsic::frameaddress), {Builder.getInt32(1)});
1560  } else {
1561  // Otherwise, for x64 and 32-bit finally functions, the parent FP is the
1562  // second parameter.
1563  auto AI = CurFn->arg_begin();
1564  ++AI;
1565  EntryFP = &*AI;
1566  }
1567 
1568  llvm::Value *ParentFP = EntryFP;
1569  if (IsFilter) {
1570  // Given whatever FP the runtime provided us in EntryFP, recover the true
1571  // frame pointer of the parent function. We only need to do this in filters,
1572  // since finally funclets recover the parent FP for us.
1573  llvm::Function *RecoverFPIntrin =
1574  CGM.getIntrinsic(llvm::Intrinsic::x86_seh_recoverfp);
1575  llvm::Constant *ParentI8Fn =
1576  llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy);
1577  ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP});
1578  }
1579 
1580  // Create llvm.localrecover calls for all captures.
1581  for (const VarDecl *VD : Finder.Captures) {
1582  if (isa<ImplicitParamDecl>(VD)) {
1583  CGM.ErrorUnsupported(VD, "'this' captured by SEH");
1584  CXXThisValue = llvm::UndefValue::get(ConvertTypeForMem(VD->getType()));
1585  continue;
1586  }
1587  if (VD->getType()->isVariablyModifiedType()) {
1588  CGM.ErrorUnsupported(VD, "VLA captured by SEH");
1589  continue;
1590  }
1591  assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) &&
1592  "captured non-local variable");
1593 
1594  // If this decl hasn't been declared yet, it will be declared in the
1595  // OutlinedStmt.
1596  auto I = ParentCGF.LocalDeclMap.find(VD);
1597  if (I == ParentCGF.LocalDeclMap.end())
1598  continue;
1599 
1600  Address ParentVar = I->second;
1601  setAddrOfLocalVar(
1602  VD, recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP));
1603  }
1604 
1605  if (Finder.SEHCodeSlot.isValid()) {
1606  SEHCodeSlotStack.push_back(
1607  recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP));
1608  }
1609 
1610  if (IsFilter)
1611  EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP);
1612 }
1613 
1614 /// Arrange a function prototype that can be called by Windows exception
1615 /// handling personalities. On Win64, the prototype looks like:
1616 /// RetTy func(void *EHPtrs, void *ParentFP);
1618  bool IsFilter,
1619  const Stmt *OutlinedStmt) {
1620  SourceLocation StartLoc = OutlinedStmt->getLocStart();
1621 
1622  // Get the mangled function name.
1624  {
1625  llvm::raw_svector_ostream OS(Name);
1626  const Decl *ParentCodeDecl = ParentCGF.CurCodeDecl;
1627  const NamedDecl *Parent = dyn_cast_or_null<NamedDecl>(ParentCodeDecl);
1628  assert(Parent && "FIXME: handle unnamed decls (lambdas, blocks) with SEH");
1629  MangleContext &Mangler = CGM.getCXXABI().getMangleContext();
1630  if (IsFilter)
1631  Mangler.mangleSEHFilterExpression(Parent, OS);
1632  else
1633  Mangler.mangleSEHFinallyBlock(Parent, OS);
1634  }
1635 
1636  FunctionArgList Args;
1637  if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) {
1638  // All SEH finally functions take two parameters. Win64 filters take two
1639  // parameters. Win32 filters take no parameters.
1640  if (IsFilter) {
1641  Args.push_back(ImplicitParamDecl::Create(
1642  getContext(), nullptr, StartLoc,
1643  &getContext().Idents.get("exception_pointers"),
1644  getContext().VoidPtrTy));
1645  } else {
1646  Args.push_back(ImplicitParamDecl::Create(
1647  getContext(), nullptr, StartLoc,
1648  &getContext().Idents.get("abnormal_termination"),
1650  }
1651  Args.push_back(ImplicitParamDecl::Create(
1652  getContext(), nullptr, StartLoc,
1653  &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy));
1654  }
1655 
1656  QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy;
1657 
1658  llvm::Function *ParentFn = ParentCGF.CurFn;
1660  RetTy, Args, FunctionType::ExtInfo(), /*isVariadic=*/false);
1661 
1662  llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
1663  llvm::Function *Fn = llvm::Function::Create(
1664  FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule());
1665  // The filter is either in the same comdat as the function, or it's internal.
1666  if (llvm::Comdat *C = ParentFn->getComdat()) {
1667  Fn->setComdat(C);
1668  } else if (ParentFn->hasWeakLinkage() || ParentFn->hasLinkOnceLinkage()) {
1669  llvm::Comdat *C = CGM.getModule().getOrInsertComdat(ParentFn->getName());
1670  ParentFn->setComdat(C);
1671  Fn->setComdat(C);
1672  } else {
1673  Fn->setLinkage(llvm::GlobalValue::InternalLinkage);
1674  }
1675 
1676  IsOutlinedSEHHelper = true;
1677 
1678  StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
1679  OutlinedStmt->getLocStart(), OutlinedStmt->getLocStart());
1680 
1681  CGM.SetLLVMFunctionAttributes(nullptr, FnInfo, CurFn);
1682  EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter);
1683 }
1684 
1685 /// Create a stub filter function that will ultimately hold the code of the
1686 /// filter expression. The EH preparation passes in LLVM will outline the code
1687 /// from the main function body into this stub.
1688 llvm::Function *
1690  const SEHExceptStmt &Except) {
1691  const Expr *FilterExpr = Except.getFilterExpr();
1692  startOutlinedSEHHelper(ParentCGF, true, FilterExpr);
1693 
1694  // Emit the original filter expression, convert to i32, and return.
1695  llvm::Value *R = EmitScalarExpr(FilterExpr);
1696  R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy),
1697  FilterExpr->getType()->isSignedIntegerType());
1699 
1700  FinishFunction(FilterExpr->getLocEnd());
1701 
1702  return CurFn;
1703 }
1704 
1705 llvm::Function *
1707  const SEHFinallyStmt &Finally) {
1708  const Stmt *FinallyBlock = Finally.getBlock();
1709  startOutlinedSEHHelper(ParentCGF, false, FinallyBlock);
1710 
1711  // Mark finally block calls as nounwind and noinline to make LLVM's job a
1712  // little easier.
1713  // FIXME: Remove these restrictions in the future.
1714  CurFn->addFnAttr(llvm::Attribute::NoUnwind);
1715  CurFn->addFnAttr(llvm::Attribute::NoInline);
1716 
1717  // Emit the original filter expression, convert to i32, and return.
1718  EmitStmt(FinallyBlock);
1719 
1720  FinishFunction(FinallyBlock->getLocEnd());
1721 
1722  return CurFn;
1723 }
1724 
1726  llvm::Value *ParentFP,
1727  llvm::Value *EntryFP) {
1728  // Get the pointer to the EXCEPTION_POINTERS struct. This is returned by the
1729  // __exception_info intrinsic.
1730  if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1731  // On Win64, the info is passed as the first parameter to the filter.
1732  SEHInfo = &*CurFn->arg_begin();
1733  SEHCodeSlotStack.push_back(
1734  CreateMemTemp(getContext().IntTy, "__exception_code"));
1735  } else {
1736  // On Win32, the EBP on entry to the filter points to the end of an
1737  // exception registration object. It contains 6 32-bit fields, and the info
1738  // pointer is stored in the second field. So, GEP 20 bytes backwards and
1739  // load the pointer.
1740  SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20);
1741  SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo());
1744  ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP));
1745  }
1746 
1747  // Save the exception code in the exception slot to unify exception access in
1748  // the filter function and the landing pad.
1749  // struct EXCEPTION_POINTERS {
1750  // EXCEPTION_RECORD *ExceptionRecord;
1751  // CONTEXT *ContextRecord;
1752  // };
1753  // int exceptioncode = exception_pointers->ExceptionRecord->ExceptionCode;
1754  llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo();
1755  llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy, nullptr);
1756  llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo());
1757  llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0);
1760  assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except");
1761  Builder.CreateStore(Code, SEHCodeSlotStack.back());
1762 }
1763 
1765  // Sema should diagnose calling this builtin outside of a filter context, but
1766  // don't crash if we screw up.
1767  if (!SEHInfo)
1768  return llvm::UndefValue::get(Int8PtrTy);
1769  assert(SEHInfo->getType() == Int8PtrTy);
1770  return SEHInfo;
1771 }
1772 
1774  assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except");
1775  return Builder.CreateLoad(SEHCodeSlotStack.back());
1776 }
1777 
1779  // Abnormal termination is just the first parameter to the outlined finally
1780  // helper.
1781  auto AI = CurFn->arg_begin();
1782  return Builder.CreateZExt(&*AI, Int32Ty);
1783 }
1784 
1786  CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true);
1787  if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) {
1788  // Outline the finally block.
1789  llvm::Function *FinallyFunc =
1790  HelperCGF.GenerateSEHFinallyFunction(*this, *Finally);
1791 
1792  // Push a cleanup for __finally blocks.
1793  EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc);
1794  return;
1795  }
1796 
1797  // Otherwise, we must have an __except block.
1798  const SEHExceptStmt *Except = S.getExceptHandler();
1799  assert(Except);
1800  EHCatchScope *CatchScope = EHStack.pushCatch(1);
1801  SEHCodeSlotStack.push_back(
1802  CreateMemTemp(getContext().IntTy, "__exception_code"));
1803 
1804  // If the filter is known to evaluate to 1, then we can use the clause
1805  // "catch i8* null". We can't do this on x86 because the filter has to save
1806  // the exception code.
1807  llvm::Constant *C =
1808  CGM.EmitConstantExpr(Except->getFilterExpr(), getContext().IntTy, this);
1809  if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C &&
1810  C->isOneValue()) {
1811  CatchScope->setCatchAllHandler(0, createBasicBlock("__except"));
1812  return;
1813  }
1814 
1815  // In general, we have to emit an outlined filter function. Use the function
1816  // in place of the RTTI typeinfo global that C++ EH uses.
1817  llvm::Function *FilterFunc =
1818  HelperCGF.GenerateSEHFilterFunction(*this, *Except);
1819  llvm::Constant *OpaqueFunc =
1820  llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy);
1821  CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret"));
1822 }
1823 
1825  // Just pop the cleanup if it's a __finally block.
1826  if (S.getFinallyHandler()) {
1827  PopCleanupBlock();
1828  return;
1829  }
1830 
1831  // Otherwise, we must have an __except block.
1832  const SEHExceptStmt *Except = S.getExceptHandler();
1833  assert(Except && "__try must have __finally xor __except");
1834  EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1835 
1836  // Don't emit the __except block if the __try block lacked invokes.
1837  // TODO: Model unwind edges from instructions, either with iload / istore or
1838  // a try body function.
1839  if (!CatchScope.hasEHBranches()) {
1840  CatchScope.clearHandlerBlocks();
1841  EHStack.popCatch();
1842  SEHCodeSlotStack.pop_back();
1843  return;
1844  }
1845 
1846  // The fall-through block.
1847  llvm::BasicBlock *ContBB = createBasicBlock("__try.cont");
1848 
1849  // We just emitted the body of the __try; jump to the continue block.
1850  if (HaveInsertPoint())
1851  Builder.CreateBr(ContBB);
1852 
1853  // Check if our filter function returned true.
1854  emitCatchDispatchBlock(*this, CatchScope);
1855 
1856  // Grab the block before we pop the handler.
1857  llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block;
1858  EHStack.popCatch();
1859 
1860  EmitBlockAfterUses(CatchPadBB);
1861 
1862  // __except blocks don't get outlined into funclets, so immediately do a
1863  // catchret.
1864  llvm::CatchPadInst *CPI =
1865  cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI());
1866  llvm::BasicBlock *ExceptBB = createBasicBlock("__except");
1867  Builder.CreateCatchRet(CPI, ExceptBB);
1868  EmitBlock(ExceptBB);
1869 
1870  // On Win64, the exception code is returned in EAX. Copy it into the slot.
1871  if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1872  llvm::Function *SEHCodeIntrin =
1873  CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode);
1874  llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI});
1875  Builder.CreateStore(Code, SEHCodeSlotStack.back());
1876  }
1877 
1878  // Emit the __except body.
1879  EmitStmt(Except->getBlock());
1880 
1881  // End the lifetime of the exception code.
1882  SEHCodeSlotStack.pop_back();
1883 
1884  if (HaveInsertPoint())
1885  Builder.CreateBr(ContBB);
1886 
1887  EmitBlock(ContBB);
1888 }
1889 
1891  // If this code is reachable then emit a stop point (if generating
1892  // debug info). We have to do this ourselves because we are on the
1893  // "simple" statement path.
1894  if (HaveInsertPoint())
1895  EmitStopPoint(&S);
1896 
1897  // This must be a __leave from a __finally block, which we warn on and is UB.
1898  // Just emit unreachable.
1899  if (!isSEHTryScope()) {
1900  Builder.CreateUnreachable();
1901  Builder.ClearInsertionPoint();
1902  return;
1903  }
1904 
1906 }
virtual void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl, raw_ostream &Out)=0
void pushTerminate()
Push a terminate handler on the stack.
Definition: CGCleanup.cpp:243
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:151
QualType getExceptionType(unsigned i) const
Definition: Type.h:3221
iterator end() const
Returns an iterator pointing to the outermost EH scope.
Definition: CGCleanup.h:570
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1483
llvm::IntegerType * IntTy
int
virtual void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E)=0
void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block)
Definition: CGCleanup.h:197
CanQualType VoidPtrTy
Definition: ASTContext.h:895
A (possibly-)qualified type.
Definition: Type.h:575
void EmitSEHLeaveStmt(const SEHLeaveStmt &S)
llvm::Type * ConvertTypeForMem(QualType T)
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:187
llvm::Module & getModule() const
llvm::LLVMContext & getLLVMContext()
static const EHPersonality GNU_C_SJLJ
Definition: CGCleanup.h:615
void EmitCXXTryStmt(const CXXTryStmt &S)
CXXCatchStmt * getHandler(unsigned i)
Definition: StmtCXX.h:104
static const EHPersonality MSVC_C_specific_handler
Definition: CGCleanup.h:625
const TargetInfo & getTarget() const
llvm::Value * getFilter(unsigned i) const
Definition: CGCleanup.h:467
static const EHPersonality MSVC_CxxFrameHandler3
Definition: CGCleanup.h:626
static stable_iterator stable_end()
Create a stable reference to the bottom of the EH stack.
Definition: EHScopeStack.h:383
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateTempAlloca - This creates a alloca and inserts it into the entry block.
Definition: CGExpr.cpp:66
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:77
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
static const EHPersonality GNU_C
Definition: CGCleanup.h:614
virtual void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl, raw_ostream &Out)=0
Represents Objective-C's @throw statement.
Definition: StmtObjC.h:313
CanQualType LongTy
Definition: ASTContext.h:889
static llvm::Constant * getUnexpectedFn(CodeGenModule &CGM)
Definition: CGException.cpp:41
void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, llvm::Value *ParentFP, llvm::Value *EntryEBP)
static llvm::Constant * getPersonalityFn(CodeGenModule &CGM, const EHPersonality &Personality)
const LangOptions & getLangOpts() const
const CGFunctionInfo & arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args, const FunctionType::ExtInfo &Info, bool isVariadic)
Definition: CGCall.cpp:490
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
CompoundStmt * getBlock() const
Definition: Stmt.h:1888
'gcc' is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI ...
Definition: ObjCRuntime.h:50
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:699
Extra information about a function prototype.
Definition: Type.h:3067
llvm::BasicBlock * EmitLandingPad()
Emits a landing pad for the current EH stack.
A protected scope for zero-cost EH handling.
Definition: CGCleanup.h:44
Defines the Objective-C statement AST node classes.
llvm::BasicBlock * getCachedEHDispatchBlock() const
Definition: CGCleanup.h:129
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:900
static bool useLibGCCSEHPersonality(const llvm::Triple &T)
On Win64, use libgcc's SEH personality function.
A scope which attempts to handle some, possibly all, types of exceptions.
Definition: CGCleanup.h:153
The collection of all-type qualifiers we support.
Definition: Type.h:116
A jump destination is an abstract label, branching to which may require a jump out through normal cle...
bool isNoexceptExceptionSpec(ExceptionSpecificationType ESpecType)
void popCatchScope()
popCatchScope - Pops the catch scope at the top of the EHScope stack, emitting any required code (oth...
llvm::BasicBlock * getTerminateHandler()
getTerminateHandler - Return a handler (not a landing pad, just a catch handler) that just calls term...
'macosx-fragile' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
Definition: ObjCRuntime.h:37
const char * CatchallRethrowFn
Definition: CGCleanup.h:609
Kind getKind() const
Definition: CGCleanup.h:119
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4381
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:1766
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:91
SmallVector< Address, 1 > SEHCodeSlotStack
A stack of exception code slots.
static const EHPersonality GNU_CPlusPlus_SJLJ
Definition: CGCleanup.h:622
llvm::CallInst * EmitRuntimeCall(llvm::Value *callee, const Twine &name="")
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:81
llvm::Value * SEHInfo
Value returned by __exception_info intrinsic.
bool hasEHBranches() const
Definition: CGCleanup.h:137
static const EHPersonality & get(CodeGenModule &CGM, const FunctionDecl *FD)
RValue EmitCall(const CGFunctionInfo &FnInfo, llvm::Value *Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, CGCalleeInfo CalleeInfo=CGCalleeInfo(), llvm::Instruction **callOrInvoke=nullptr)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3159
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Address getExceptionSlot()
Returns a pointer to the function's exception object and selector slot, which is assigned in every la...
bool isEHCleanup() const
Definition: CGCleanup.h:315
Expr * getFilterExpr() const
Definition: Stmt.h:1848
void setFilter(unsigned i, llvm::Value *filterValue)
Definition: CGCleanup.h:462
class EHCatchScope * pushCatch(unsigned NumHandlers)
Push a set of catch handlers on the stack.
Definition: CGCleanup.cpp:235
const Handler & getHandler(unsigned I) const
Definition: CGCleanup.h:213
iterator begin() const
Returns an iterator pointing to the innermost EH scope.
Definition: CGCleanup.h:566
static llvm::Constant * getFreeExceptionFn(CodeGenModule &CGM)
Definition: CGException.cpp:32
bool isLocalVarDeclOrParm() const
Similar to isLocalVarDecl but also includes parameters.
Definition: Decl.h:964
static llvm::Constant * getCatchAllValue(CodeGenFunction &CGF)
Returns the value to inject into a selector to indicate the presence of a catch-all.
void ExitSEHTryStmt(const SEHTryStmt &S)
Stmt * getHandlerBlock() const
Definition: StmtCXX.h:52
static const EHPersonality GNUstep_ObjC
Definition: CGCleanup.h:618
bool IsOutlinedSEHHelper
True if the current function is an outlined SEH helper.
llvm::Constant * getTerminateFn()
Get the declaration of std::terminate for the platform.
Definition: CGException.cpp:50
void incrementProfileCounter(const Stmt *S)
Increment the profiler's counter for the given statement.
llvm::BasicBlock * getInvokeDestImpl()
llvm::Function * GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, const SEHFinallyStmt &Finally)
void EmitStmt(const Stmt *S)
EmitStmt - Emit the code for the statement.
Definition: CGStmt.cpp:48
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:85
void popFilter()
Pops an exceptions filter off the stack.
Definition: CGCleanup.cpp:226
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition: Expr.cpp:1245
This represents the body of a CapturedStmt, and serves as its DeclContext.
Definition: Decl.h:3560
detail::InMemoryDirectory::const_iterator I
llvm::AllocaInst * EHSelectorSlot
The selector slot.
CanQualType UnsignedCharTy
Definition: ASTContext.h:890
QualType getType() const
Definition: Decl.h:530
Represents the this expression in C++.
Definition: ExprCXX.h:860
iterator find(stable_iterator save) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack. ...
Definition: CGCleanup.h:590
llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
Definition: CGCall.cpp:3111
'watchos' is a variant of iOS for Apple's watchOS.
Definition: ObjCRuntime.h:46
bool hasTerminate() const
Does this runtime provide an objc_terminate function?
Definition: ObjCRuntime.h:257
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3041
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
Definition: Type.h:3193
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
void EmitAnyExprToMem(const Expr *E, Address Location, Qualifiers Quals, bool IsInitializer)
EmitAnyExprToMem - Emits the code necessary to evaluate an arbitrary expression into the given memory...
Definition: CGExpr.cpp:168
static const EHPersonality MSVC_except_handler
Definition: CGCleanup.h:624
static llvm::Constant * getOpaquePersonalityFn(CodeGenModule &CGM, const EHPersonality &Personality)
const TargetInfo & getTarget() const
ASTContext * Context
bool usesSEHTry() const
Indicates the function uses __try.
Definition: Decl.h:1798
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
iterator begin() const
Definition: CGCleanup.h:228
llvm::Value * ExceptionSlot
The exception slot.
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
void SetLLVMFunctionAttributes(const Decl *D, const CGFunctionInfo &Info, llvm::Function *F)
Set the LLVM function attributes (sext, zext, etc).
bool isNeXTFamily() const
Is this runtime basically of the NeXT family of runtimes?
Definition: ObjCRuntime.h:133
llvm::Value * getPointer() const
Definition: Address.h:38
llvm::BasicBlock * EHResumeBlock
EHResumeBlock - Unified block containing a call to llvm.eh.resume.
bool empty() const
Determines whether the exception-scopes stack is empty.
Definition: EHScopeStack.h:342
CatchTypeInfo Type
A type info value, or null (C++ null, not an LLVM null pointer) for a catch-all.
Definition: CGCleanup.h:163
void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt, bool IsFilter)
Scan the outlined statement for captures from the parent function.
Expr - This represents one expression.
Definition: Expr.h:104
void enter(CodeGenFunction &CGF, const Stmt *Finally, llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn, llvm::Constant *rethrowFn)
Enters a finally block for an implementation using zero-cost exceptions.
static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn)
Check whether a personality function could reasonably be swapped for a C++ personality function...
'macosx' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
Definition: ObjCRuntime.h:32
static Address invalid()
Definition: Address.h:35
CGCXXABI & getCXXABI() const
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
llvm::Function * GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, const SEHExceptStmt &Except)
Create a stub filter function that will ultimately hold the code of the filter expression.
virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, const ObjCAtThrowStmt &S, bool ClearInsertionPoint=true)=0
void EmitSEHTryStmt(const SEHTryStmt &S)
ASTContext & getContext() const
llvm::BasicBlock * getBlock() const
llvm::BasicBlock * Block
The catch handler for this type.
Definition: CGCleanup.h:166
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
CXXTryStmt - A C++ try block, including all handlers.
Definition: StmtCXX.h:65
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:81
llvm::Value * getExceptionFromSlot()
Returns the contents of the function's exception object and selector slots.
static void emitCatchDispatchBlock(CodeGenFunction &CGF, EHCatchScope &catchScope)
Emit the structure of the dispatch block for the given catch scope.
EHScopeStack::stable_iterator getEnclosingEHScope() const
Definition: CGCleanup.h:143
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Definition: EHScopeStack.h:378
llvm::BasicBlock * getCachedLandingPad() const
Definition: CGCleanup.h:121
llvm::LLVMContext & getLLVMContext()
static const EHPersonality & getObjCXXPersonality(const llvm::Triple &T, const LangOptions &L)
Determines the personality function to use when both C++ and Objective-C exceptions are being caught...
static const EHPersonality GNU_ObjCXX
Definition: CGCleanup.h:619
clang::ObjCRuntime ObjCRuntime
Definition: LangOptions.h:85
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr * > VL, ArrayRef< Expr * > PL, ArrayRef< Expr * > IL, Expr *Step, Expr *CalcStep)
Creates clause with a list of variables VL and a linear step Step.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
bool usesFuncletPads() const
Does this personality use landingpads or the family of pad instructions designed to form funclets...
Definition: CGCleanup.h:630
ValueDecl * getDecl()
Definition: Expr.h:1007
virtual CatchTypeInfo getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType)=0
'gnustep' is the modern non-fragile GNUstep runtime.
Definition: ObjCRuntime.h:53
llvm::StoreInst * CreateFlagStore(bool Value, llvm::Value *Addr)
Emit a store to an i1 flag variable.
Definition: CGBuilder.h:162
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:28
void popCatch()
Pops a catch scope off the stack. This is private to CGException.cpp.
Definition: CGCleanup.h:574
llvm::BasicBlock * getEHDispatchBlock(EHScopeStack::stable_iterator scope)
The l-value was considered opaque, so the alignment was determined from a type.
static void emitFilterDispatchBlock(CodeGenFunction &CGF, EHFilterScope &filterScope)
Emit the dispatch block for a filter scope if necessary.
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, Address ParentVar, llvm::Value *ParentFP)
Recovers the address of a local in a parent function.
ASTMatchFinder *const Finder
Enumerates target-specific builtins in their own namespaces within namespace clang.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:168
virtual void emitRethrow(CodeGenFunction &CGF, bool isNoReturn)=0
llvm::Constant * CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeSet ExtraAttrs=llvm::AttributeSet())
Create a new runtime function with the specified type and name.
static const EHPersonality & getObjCPersonality(const llvm::Triple &T, const LangOptions &L)
llvm::Constant * EmitConstantExpr(const Expr *E, QualType DestType, CodeGenFunction *CGF=nullptr)
Try to emit the given expression as a constant; returns 0 if the expression cannot be emitted as a co...
ASTContext & getContext() const
Encodes a location in the source.
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go...
A saved depth on the scope stack.
Definition: EHScopeStack.h:104
'objfw' is the Objective-C runtime included in ObjFW
Definition: ObjCRuntime.h:56
llvm::BasicBlock * getUnreachableBlock()
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
Definition: Expr.h:1127
void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
DeactivateCleanupBlock - Deactivates the given cleanup block.
Definition: CGCleanup.cpp:1176
CompoundStmt * getBlock() const
Definition: Stmt.h:1852
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:41
virtual CatchTypeInfo getCatchAllTypeInfo()
Definition: CGCXXABI.cpp:326
The noexcept specifier evaluates to true.
Definition: Type.h:3216
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
CanQualType VoidTy
Definition: ASTContext.h:881
An aligned address.
Definition: Address.h:25
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
const LangOptions & getLangOpts() const
llvm::BasicBlock * getEHResumeBlock(bool isCleanup)
SEHExceptStmt * getExceptHandler() const
Returns 0 if not defined.
Definition: Stmt.cpp:913
const VersionTuple & getVersion() const
Definition: ObjCRuntime.h:76
NoexceptResult getNoexceptSpec(const ASTContext &Ctx) const
Get the meaning of the noexcept spec on this function, if any.
Definition: Type.cpp:2760
void setCachedEHDispatchBlock(llvm::BasicBlock *block)
Definition: CGCleanup.h:133
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:95
llvm::Instruction * CurrentFuncletPad
JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target)
The given basic block lies in the current EH scope, but may be a target of a potentially scope-crossi...
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:62
void EmitStartEHSpec(const Decl *D)
EmitStartEHSpec - Emit the start of the exception spec.
unsigned getNumHandlers() const
Definition: CGCleanup.h:193
The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the type of a catch handler...
Definition: CGCleanup.h:38
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
static const EHPersonality & getCXXPersonality(const llvm::Triple &T, const LangOptions &L)
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:193
static const EHPersonality NeXT_ObjC
Definition: CGCleanup.h:620
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:146
QualType getType() const
Definition: Expr.h:125
llvm::BasicBlock * getMSVCDispatchBlock(EHScopeStack::stable_iterator scope)
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
CGFunctionInfo - Class to encapsulate the information about a function definition.
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:67
This class organizes the cross-function state that is used while generating LLVM code.
class EHFilterScope * pushFilter(unsigned NumFilters)
Push an exceptions filter on the stack.
Definition: CGCleanup.cpp:218
const Expr * getSubExpr() const
Definition: ExprCXX.h:920
static const Type * getElementType(const Expr *BaseExpr)
static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF, SourceLocation TemporaryLocation)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:569
static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI)
Check whether a landingpad instruction only uses C++ features.
static const EHPersonality GNU_CPlusPlus_SEH
Definition: CGCleanup.h:623
Address CreateMemTemp(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignment...
Definition: CGExpr.cpp:97
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:25
static const EHPersonality GNU_ObjC
Definition: CGCleanup.h:617
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset, const llvm::Twine &Name="")
Definition: CGBuilder.h:191
static bool isNonEHScope(const EHScope &S)
Check whether this is a non-EH scope, i.e.
void EmitAnyExprToExn(const Expr *E, Address Addr)
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:99
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:78
unsigned getNumHandlers() const
Definition: StmtCXX.h:103
QualType getCaughtType() const
Definition: StmtCXX.cpp:20
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
detail::InMemoryDirectory::const_iterator E
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:121
bool isSEHTryScope() const
Returns true inside SEH __try blocks.
const llvm::Triple & getTriple() const
Represents a __leave statement.
Definition: Stmt.h:1950
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:5255
'ios' is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
Definition: ObjCRuntime.h:42
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:44
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:5675
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id, QualType T)
Definition: Decl.cpp:3948
static const EHPersonality GNU_CPlusPlus
Definition: CGCleanup.h:621
llvm::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition: CGBuilder.h:155
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:33
ExceptionSpecificationType
The various types of exception specifications that exist in C++11.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:367
void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
void EmitStopPoint(const Stmt *S)
EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
Definition: CGStmt.cpp:38
Kind getKind() const
Definition: ObjCRuntime.h:75
static const EHPersonality & getSEHPersonalityMSVC(const llvm::Triple &T)
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5169
bool isObjCObjectPointerType() const
Definition: Type.h:5377
llvm::BasicBlock * getTerminateLandingPad()
getTerminateLandingPad - Return a landing pad that just calls terminate.
CXXCatchStmt - This represents a C++ catch block.
Definition: StmtCXX.h:29
llvm::Type * ConvertType(QualType T)
static const EHPersonality GNU_C_SEH
Definition: CGCleanup.h:616
static const EHPersonality & getCPersonality(const llvm::Triple &T, const LangOptions &L)
The exceptions personality for a function.
Definition: CGCleanup.h:603
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
void popTerminate()
Pops a terminate handler off the stack.
Definition: CGCleanup.h:582
llvm::Constant * RTTI
Definition: CGCleanup.h:39
CompoundStmt * getTryBlock()
Definition: StmtCXX.h:96
llvm::Value * EmitSEHAbnormalTermination()
virtual llvm::CallInst * emitTerminateForUnexpectedException(CodeGenFunction &CGF, llvm::Value *Exn)
Definition: CGCXXABI.cpp:320
void ForceCleanup()
Force the emission of cleanups now, instead of waiting until this object is destroyed.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2134
void EnterSEHTryStmt(const SEHTryStmt &S)
void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter, const Stmt *OutlinedStmt)
Arrange a function prototype that can be called by Windows exception handling personalities.
unsigned getNumFilters() const
Definition: CGCleanup.h:460
stable_iterator getInnermostEHScope() const
Definition: EHScopeStack.h:360
CanQualType IntTy
Definition: ASTContext.h:889
An exceptions scope which filters exceptions thrown through it.
Definition: CGCleanup.h:438
VarDecl * getExceptionDecl() const
Definition: StmtCXX.h:50
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:922
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments...
Definition: CGCall.cpp:444
void EmitBranchThroughCleanup(JumpDest Dest)
EmitBranchThroughCleanup - Emit a branch from the current insert block through the normal cleanup han...
Definition: CGCleanup.cpp:980
void EmitEndEHSpec(const Decl *D)
EmitEndEHSpec - Emit the end of the exception spec.
virtual void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C)=0
static llvm::Constant * getCatchallRethrowFn(CodeGenModule &CGM, StringRef Name)
Definition: CGException.cpp:76
llvm::StoreInst * CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr, CharUnits Align, bool IsVolatile=false)
Definition: CGBuilder.h:128
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:144
NamedDecl - This represents a decl with a name.
Definition: Decl.h:145
CompoundStmt * getTryBlock() const
Definition: Stmt.h:1929
A non-stable pointer into the scope stack.
Definition: CGCleanup.h:502
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], or an enum decl which has a signed representation.
Definition: Type.cpp:1700
void EmitBlockAfterUses(llvm::BasicBlock *BB)
EmitBlockAfterUses - Emit the given block somewhere hopefully near its uses, and leave the insertion ...
Definition: CGStmt.cpp:404
static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope)
virtual llvm::Constant * GetEHType(QualType T)=0
Get the type constant to catch for the given ObjC pointer type.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:56
bool requiresLandingPad() const
Definition: EHScopeStack.h:344
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:586
void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block)
Definition: CGCleanup.h:201
A class which abstracts out some details necessary for making a call.
Definition: Type.h:2872
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals)
Return this type as a completely-unqualified array type, capturing the qualifiers in Quals...
llvm::SmallVector< const JumpDest *, 2 > SEHTryEpilogueStack
SEHFinallyStmt * getFinallyHandler() const
Definition: Stmt.cpp:917
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:5116
unsigned getNumExceptions() const
Definition: Type.h:3220
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1293