1756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
2756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//
3756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//                     The LLVM Compiler Infrastructure
4756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//
5756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson// This file is distributed under the University of Illinois Open Source
6756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson// License. See LICENSE.TXT for details.
7756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//
8756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//===----------------------------------------------------------------------===//
9756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//
10756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson// This contains code dealing with C++ exception related code generation.
11756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//
12756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson//===----------------------------------------------------------------------===//
13756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
14756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson#include "CodeGenFunction.h"
1536f893c1efe367f929d92c8b125f964c22ba189eJohn McCall#include "CGCleanup.h"
16af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer#include "CGObjCRuntime.h"
17204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall#include "TargetInfo.h"
18af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer#include "clang/AST/StmtCXX.h"
19b1ba0efc3d1dc1daa5d82c40bc504e1f368c4fa0Chandler Carruth#include "clang/AST/StmtObjC.h"
203b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h"
21af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer#include "llvm/Support/CallSite.h"
22f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
23756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlssonusing namespace clang;
24756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlssonusing namespace CodeGen;
25756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson
26629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
27d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson  // void *__cxa_allocate_exception(size_t thrown_size);
288755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
292acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
30629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
318755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
32629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
33d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson}
34d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson
35629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) {
3699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  // void __cxa_free_exception(void *thrown_exception);
378755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
382acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
39629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
408755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
41629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
4299533834ba8f3658559f334e68a518ebb6388ceaMike Stump}
4399533834ba8f3658559f334e68a518ebb6388ceaMike Stump
44629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getThrowFn(CodeGenModule &CGM) {
458755ec336108839b9621c3b18f0e175f8a3b671cMike Stump  // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
4699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  //                  void (*dest) (void *));
47d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson
48629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
492acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
50629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
518755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
52629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
53d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson}
54d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson
55629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getReThrowFn(CodeGenModule &CGM) {
5699533834ba8f3658559f334e68a518ebb6388ceaMike Stump  // void __cxa_rethrow();
57b4eea691866a3fa75722da9eba735c44f140398aMike Stump
582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
59629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
608755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
61629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
62b4eea691866a3fa75722da9eba735c44f140398aMike Stump}
63b4eea691866a3fa75722da9eba735c44f140398aMike Stump
64629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
65f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // void *__cxa_get_exception_ptr(void*);
66f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
672acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
68629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
69f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
70629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
71f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
72f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
73629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
74f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // void *__cxa_begin_catch(void*);
752bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
77629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
788755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
79629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
802bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump}
812bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
82629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
8399533834ba8f3658559f334e68a518ebb6388ceaMike Stump  // void __cxa_end_catch();
842bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
86629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
878755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
88629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
892bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump}
902bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
91629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) {
9214b0e4b4d55111e2b00db0072d20f4a8209fa5a4Richard Smith  // void __cxa_call_unexpected(void *thrown_exception);
93cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
95629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
968755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
97629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
98cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump}
99cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
10093c332a8ba2c193c435b293966d343dab15f555bJohn McCallllvm::Constant *CodeGenFunction::getUnwindResumeFn() {
1012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
102da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    llvm::FunctionType::get(VoidTy, Int8PtrTy, /*IsVarArgs=*/false);
10393c332a8ba2c193c435b293966d343dab15f555bJohn McCall
1044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().SjLjExceptions)
10593c332a8ba2c193c435b293966d343dab15f555bJohn McCall    return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
10693c332a8ba2c193c435b293966d343dab15f555bJohn McCall  return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume");
10793c332a8ba2c193c435b293966d343dab15f555bJohn McCall}
10893c332a8ba2c193c435b293966d343dab15f555bJohn McCall
10986a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregorllvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
1102acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
111da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    llvm::FunctionType::get(VoidTy, Int8PtrTy, /*IsVarArgs=*/false);
1128755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
1134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().SjLjExceptions)
114a5f2de2e49e25ece82dd9fd63d8f390a7cda6417John McCall    return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume_or_Rethrow");
11586a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregor  return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
1160f590be3808365e851352543faa6acbece50b686Mike Stump}
1170f590be3808365e851352543faa6acbece50b686Mike Stump
118629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getTerminateFn(CodeGenModule &CGM) {
11999533834ba8f3658559f334e68a518ebb6388ceaMike Stump  // void __terminate();
12099533834ba8f3658559f334e68a518ebb6388ceaMike Stump
1212acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
122629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
1238755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
1245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef name;
125256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall
126256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall  // In C++, use std::terminate().
127629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  if (CGM.getLangOpts().CPlusPlus)
128256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall    name = "_ZSt9terminatev"; // FIXME: mangling!
129629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  else if (CGM.getLangOpts().ObjC1 &&
130629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall           CGM.getLangOpts().ObjCRuntime.hasTerminate())
131256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall    name = "objc_terminate";
132256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall  else
133256a76e0b0e0c9e65a3122917d553ef10bc84d29John McCall    name = "abort";
134629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, name);
13579a9ad8e5eec3696989354be13a74a1106f64f72David Chisnall}
13679a9ad8e5eec3696989354be13a74a1106f64f72David Chisnall
137629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCallstatic llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM,
1385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                            StringRef Name) {
1392acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
140629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall    llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
1418262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall
142629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  return CGM.CreateRuntimeFunction(FTy, Name);
143f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
144f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
145af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramernamespace {
146af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  /// The exceptions personality for a function.
147af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  struct EHPersonality {
148af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    const char *PersonalityFn;
149af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer
150af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    // If this is non-null, this personality requires a non-standard
151af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    // function for rethrowing an exception after a catchall cleanup.
152af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    // This function must have prototype void(void*).
153af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    const char *CatchallRethrowFn;
154af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer
155af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality &get(const LangOptions &Lang);
156af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality GNU_C;
157af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality GNU_C_SJLJ;
158af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality GNU_ObjC;
15965bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall    static const EHPersonality GNUstep_ObjC;
160af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality GNU_ObjCXX;
161af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality NeXT_ObjC;
162af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality GNU_CPlusPlus;
163af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer    static const EHPersonality GNU_CPlusPlus_SJLJ;
164af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  };
165af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer}
166af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer
167af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", 0 };
168af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", 0 };
169af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality EHPersonality::NeXT_ObjC = { "__objc_personality_v0", 0 };
170af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", 0};
171af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality
172af2771b147f1a5934c6c91574f1c2df986034e74Benjamin KramerEHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", 0 };
173af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality
174af2771b147f1a5934c6c91574f1c2df986034e74Benjamin KramerEHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"};
175af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramerconst EHPersonality
176af2771b147f1a5934c6c91574f1c2df986034e74Benjamin KramerEHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", 0 };
17765bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnallconst EHPersonality
17865bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid ChisnallEHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", 0 };
1798262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall
1808262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCallstatic const EHPersonality &getCPersonality(const LangOptions &L) {
18144680786286f4f651603c6811f8412a3ee7fe975John McCall  if (L.SjLjExceptions)
18244680786286f4f651603c6811f8412a3ee7fe975John McCall    return EHPersonality::GNU_C_SJLJ;
1838262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  return EHPersonality::GNU_C;
1848262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall}
1858262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall
1868262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCallstatic const EHPersonality &getObjCPersonality(const LangOptions &L) {
187260611a32535c851237926bfcf78869b13c07d5bJohn McCall  switch (L.ObjCRuntime.getKind()) {
188260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::FragileMacOSX:
189260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return getCPersonality(L);
190260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::MacOSX:
191260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::iOS:
192260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return EHPersonality::NeXT_ObjC;
19311d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  case ObjCRuntime::GNUstep:
19465bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall    if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
19565bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall      return EHPersonality::GNUstep_ObjC;
19665bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall    // fallthrough
19711d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  case ObjCRuntime::GCC:
198f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall  case ObjCRuntime::ObjFW:
1998262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return EHPersonality::GNU_ObjC;
200f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
201260611a32535c851237926bfcf78869b13c07d5bJohn McCall  llvm_unreachable("bad runtime kind");
202f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
203f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2048262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCallstatic const EHPersonality &getCXXPersonality(const LangOptions &L) {
2058262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  if (L.SjLjExceptions)
2068262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return EHPersonality::GNU_CPlusPlus_SJLJ;
207f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  else
2088262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return EHPersonality::GNU_CPlusPlus;
209f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
210f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
211f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Determines the personality function to use when both C++
212f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// and Objective-C exceptions are being caught.
2138262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCallstatic const EHPersonality &getObjCXXPersonality(const LangOptions &L) {
214260611a32535c851237926bfcf78869b13c07d5bJohn McCall  switch (L.ObjCRuntime.getKind()) {
215f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // The ObjC personality defers to the C++ personality for non-ObjC
216f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // handlers.  Unlike the C++ case, we use the same personality
217f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // function on targets using (backend-driven) SJLJ EH.
218260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::MacOSX:
219260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::iOS:
220260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return EHPersonality::NeXT_ObjC;
221260611a32535c851237926bfcf78869b13c07d5bJohn McCall
222260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // In the fragile ABI, just use C++ exception handling and hope
223260611a32535c851237926bfcf78869b13c07d5bJohn McCall  // they're not doing crazy exception mixing.
224260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::FragileMacOSX:
225260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return getCXXPersonality(L);
22679a9ad8e5eec3696989354be13a74a1106f64f72David Chisnall
22711d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  // The GCC runtime's personality function inherently doesn't support
2288262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  // mixed EH.  Use the C++ personality just to avoid returning null.
22911d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  case ObjCRuntime::GCC:
230f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall  case ObjCRuntime::ObjFW: // XXX: this will change soon
23111d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall    return EHPersonality::GNU_ObjC;
23211d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  case ObjCRuntime::GNUstep:
233260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return EHPersonality::GNU_ObjCXX;
234260611a32535c851237926bfcf78869b13c07d5bJohn McCall  }
235260611a32535c851237926bfcf78869b13c07d5bJohn McCall  llvm_unreachable("bad runtime kind");
236f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
237f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2388262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCallconst EHPersonality &EHPersonality::get(const LangOptions &L) {
2398262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  if (L.CPlusPlus && L.ObjC1)
2408262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return getObjCXXPersonality(L);
2418262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  else if (L.CPlusPlus)
2428262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return getCXXPersonality(L);
2438262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  else if (L.ObjC1)
2448262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return getObjCPersonality(L);
245f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  else
2468262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall    return getCPersonality(L);
2478262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall}
2488262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall
249b25938303de0976b9f189363d43033e5788e3d36John McCallstatic llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
2508262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall                                        const EHPersonality &Personality) {
2518262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall  llvm::Constant *Fn =
2528b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
253af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer                              Personality.PersonalityFn);
254b25938303de0976b9f189363d43033e5788e3d36John McCall  return Fn;
255b25938303de0976b9f189363d43033e5788e3d36John McCall}
256b25938303de0976b9f189363d43033e5788e3d36John McCall
257b25938303de0976b9f189363d43033e5788e3d36John McCallstatic llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
258b25938303de0976b9f189363d43033e5788e3d36John McCall                                        const EHPersonality &Personality) {
259b25938303de0976b9f189363d43033e5788e3d36John McCall  llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
260d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
261b25938303de0976b9f189363d43033e5788e3d36John McCall}
262b25938303de0976b9f189363d43033e5788e3d36John McCall
263b25938303de0976b9f189363d43033e5788e3d36John McCall/// Check whether a personality function could reasonably be swapped
264b25938303de0976b9f189363d43033e5788e3d36John McCall/// for a C++ personality function.
265b25938303de0976b9f189363d43033e5788e3d36John McCallstatic bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
266b25938303de0976b9f189363d43033e5788e3d36John McCall  for (llvm::Constant::use_iterator
267b25938303de0976b9f189363d43033e5788e3d36John McCall         I = Fn->use_begin(), E = Fn->use_end(); I != E; ++I) {
268b25938303de0976b9f189363d43033e5788e3d36John McCall    llvm::User *User = *I;
269b25938303de0976b9f189363d43033e5788e3d36John McCall
270b25938303de0976b9f189363d43033e5788e3d36John McCall    // Conditionally white-list bitcasts.
271b25938303de0976b9f189363d43033e5788e3d36John McCall    if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(User)) {
272b25938303de0976b9f189363d43033e5788e3d36John McCall      if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
273b25938303de0976b9f189363d43033e5788e3d36John McCall      if (!PersonalityHasOnlyCXXUses(CE))
274b25938303de0976b9f189363d43033e5788e3d36John McCall        return false;
275b25938303de0976b9f189363d43033e5788e3d36John McCall      continue;
276b25938303de0976b9f189363d43033e5788e3d36John McCall    }
277b25938303de0976b9f189363d43033e5788e3d36John McCall
27840ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    // Otherwise, it has to be a landingpad instruction.
27940ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    llvm::LandingPadInst *LPI = dyn_cast<llvm::LandingPadInst>(User);
28040ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    if (!LPI) return false;
281b25938303de0976b9f189363d43033e5788e3d36John McCall
28240ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
283b25938303de0976b9f189363d43033e5788e3d36John McCall      // Look for something that would've been returned by the ObjC
284b25938303de0976b9f189363d43033e5788e3d36John McCall      // runtime's GetEHType() method.
28540ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling      llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
28640ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling      if (LPI->isCatch(I)) {
28740ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling        // Check if the catch value has the ObjC prefix.
288eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling        if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
289eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling          // ObjC EH selector entries are always global variables with
290eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling          // names starting like this.
291eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling          if (GV->getName().startswith("OBJC_EHTYPE"))
292eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling            return false;
29340ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling      } else {
29440ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling        // Check if any of the filter values have the ObjC prefix.
29540ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling        llvm::Constant *CVal = cast<llvm::Constant>(Val);
29640ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling        for (llvm::User::op_iterator
29740ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling               II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
298eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling          if (llvm::GlobalVariable *GV =
299eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling              cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
300eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling            // ObjC EH selector entries are always global variables with
301eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling            // names starting like this.
302eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling            if (GV->getName().startswith("OBJC_EHTYPE"))
303eecb6a1e8b95fcdb7d1a0d92b0a0311c041440cdBill Wendling              return false;
30440ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling        }
30540ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling      }
306b25938303de0976b9f189363d43033e5788e3d36John McCall    }
307b25938303de0976b9f189363d43033e5788e3d36John McCall  }
308b25938303de0976b9f189363d43033e5788e3d36John McCall
309b25938303de0976b9f189363d43033e5788e3d36John McCall  return true;
310b25938303de0976b9f189363d43033e5788e3d36John McCall}
311b25938303de0976b9f189363d43033e5788e3d36John McCall
312b25938303de0976b9f189363d43033e5788e3d36John McCall/// Try to use the C++ personality function in ObjC++.  Not doing this
313b25938303de0976b9f189363d43033e5788e3d36John McCall/// can cause some incompatibilities with gcc, which is more
314b25938303de0976b9f189363d43033e5788e3d36John McCall/// aggressive about only using the ObjC++ personality in a function
315b25938303de0976b9f189363d43033e5788e3d36John McCall/// when it really needs it.
316b25938303de0976b9f189363d43033e5788e3d36John McCallvoid CodeGenModule::SimplifyPersonality() {
317b25938303de0976b9f189363d43033e5788e3d36John McCall  // If we're not in ObjC++ -fexceptions, there's nothing to do.
3184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions)
319b25938303de0976b9f189363d43033e5788e3d36John McCall    return;
320b25938303de0976b9f189363d43033e5788e3d36John McCall
32170cd619e74306b1599d5579e6fd6a14bd9592281John McCall  // Both the problem this endeavors to fix and the way the logic
32270cd619e74306b1599d5579e6fd6a14bd9592281John McCall  // above works is specific to the NeXT runtime.
32370cd619e74306b1599d5579e6fd6a14bd9592281John McCall  if (!LangOpts.ObjCRuntime.isNeXTFamily())
32470cd619e74306b1599d5579e6fd6a14bd9592281John McCall    return;
32570cd619e74306b1599d5579e6fd6a14bd9592281John McCall
3264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const EHPersonality &ObjCXX = EHPersonality::get(LangOpts);
3274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const EHPersonality &CXX = getCXXPersonality(LangOpts);
328af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  if (&ObjCXX == &CXX)
329b25938303de0976b9f189363d43033e5788e3d36John McCall    return;
330b25938303de0976b9f189363d43033e5788e3d36John McCall
331af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 &&
332af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer         "Different EHPersonalities using the same personality function.");
333af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer
334af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn);
335b25938303de0976b9f189363d43033e5788e3d36John McCall
336b25938303de0976b9f189363d43033e5788e3d36John McCall  // Nothing to do if it's unused.
337b25938303de0976b9f189363d43033e5788e3d36John McCall  if (!Fn || Fn->use_empty()) return;
338b25938303de0976b9f189363d43033e5788e3d36John McCall
339b25938303de0976b9f189363d43033e5788e3d36John McCall  // Can't do the optimization if it has non-C++ uses.
340b25938303de0976b9f189363d43033e5788e3d36John McCall  if (!PersonalityHasOnlyCXXUses(Fn)) return;
341b25938303de0976b9f189363d43033e5788e3d36John McCall
342b25938303de0976b9f189363d43033e5788e3d36John McCall  // Create the C++ personality function and kill off the old
343b25938303de0976b9f189363d43033e5788e3d36John McCall  // function.
344b25938303de0976b9f189363d43033e5788e3d36John McCall  llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
345b25938303de0976b9f189363d43033e5788e3d36John McCall
346b25938303de0976b9f189363d43033e5788e3d36John McCall  // This can happen if the user is screwing with us.
347b25938303de0976b9f189363d43033e5788e3d36John McCall  if (Fn->getType() != CXXFn->getType()) return;
348b25938303de0976b9f189363d43033e5788e3d36John McCall
349b25938303de0976b9f189363d43033e5788e3d36John McCall  Fn->replaceAllUsesWith(CXXFn);
350b25938303de0976b9f189363d43033e5788e3d36John McCall  Fn->eraseFromParent();
351f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
352f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
353f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Returns the value to inject into a selector to indicate the
354f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// presence of a catch-all.
355f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
356f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Possibly we should use @llvm.eh.catch.all.value here.
357d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
358f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
359f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
36009faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCallnamespace {
36109faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCall  /// A cleanup to free the exception object if its initialization
36209faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCall  /// throws.
363c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall  struct FreeException : EHScopeStack::Cleanup {
364c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    llvm::Value *exn;
365c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    FreeException(llvm::Value *exn) : exn(exn) {}
366ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
367bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn);
36809faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCall    }
36909faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCall  };
37009faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCall}
37109faeabf39a6fab2e2beb6bf03da970c17d2049aJohn McCall
372ac418162692a951ca3796d6830496a85a2d12493John McCall// Emits an exception expression into the given location.  This
373ac418162692a951ca3796d6830496a85a2d12493John McCall// differs from EmitAnyExprToMem only in that, if a final copy-ctor
374ac418162692a951ca3796d6830496a85a2d12493John McCall// call is required, an exception within that copy ctor causes
375ac418162692a951ca3796d6830496a85a2d12493John McCall// std::terminate to be invoked.
3763ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCallstatic void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e,
3773ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall                             llvm::Value *addr) {
378f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Make sure the exception object is cleaned up if there's an
379f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // exception during initialization.
3803ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr);
3813ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin();
382ac418162692a951ca3796d6830496a85a2d12493John McCall
383ac418162692a951ca3796d6830496a85a2d12493John McCall  // __cxa_allocate_exception returns a void*;  we need to cast this
384ac418162692a951ca3796d6830496a85a2d12493John McCall  // to the appropriate type for the object.
3852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo();
3863ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty);
387ac418162692a951ca3796d6830496a85a2d12493John McCall
388ac418162692a951ca3796d6830496a85a2d12493John McCall  // FIXME: this isn't quite right!  If there's a final unelided call
389ac418162692a951ca3796d6830496a85a2d12493John McCall  // to a copy constructor, then according to [except.terminate]p1 we
390ac418162692a951ca3796d6830496a85a2d12493John McCall  // must call std::terminate() if that constructor throws, because
391ac418162692a951ca3796d6830496a85a2d12493John McCall  // technically that copy occurs after the exception expression is
392ac418162692a951ca3796d6830496a85a2d12493John McCall  // evaluated but before the exception is caught.  But the best way
393ac418162692a951ca3796d6830496a85a2d12493John McCall  // to handle that is to teach EmitAggExpr to do the final copy
394ac418162692a951ca3796d6830496a85a2d12493John McCall  // differently if it can't be elided.
395649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
396649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                       /*IsInit*/ true);
397ac418162692a951ca3796d6830496a85a2d12493John McCall
3983ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  // Deactivate the cleanup block.
3996f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  CGF.DeactivateCleanupBlock(cleanup, cast<llvm::Instruction>(typedAddr));
400f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
401f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
402f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::Value *CodeGenFunction::getExceptionSlot() {
40393c332a8ba2c193c435b293966d343dab15f555bJohn McCall  if (!ExceptionSlot)
40493c332a8ba2c193c435b293966d343dab15f555bJohn McCall    ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
405f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return ExceptionSlot;
4060f590be3808365e851352543faa6acbece50b686Mike Stump}
4070f590be3808365e851352543faa6acbece50b686Mike Stump
40893c332a8ba2c193c435b293966d343dab15f555bJohn McCallllvm::Value *CodeGenFunction::getEHSelectorSlot() {
40993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  if (!EHSelectorSlot)
41093c332a8ba2c193c435b293966d343dab15f555bJohn McCall    EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
41193c332a8ba2c193c435b293966d343dab15f555bJohn McCall  return EHSelectorSlot;
41293c332a8ba2c193c435b293966d343dab15f555bJohn McCall}
41393c332a8ba2c193c435b293966d343dab15f555bJohn McCall
414ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendlingllvm::Value *CodeGenFunction::getExceptionFromSlot() {
415ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  return Builder.CreateLoad(getExceptionSlot(), "exn");
416ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling}
417ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling
418ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendlingllvm::Value *CodeGenFunction::getSelectorFromSlot() {
419ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  return Builder.CreateLoad(getEHSelectorSlot(), "sel");
420ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling}
421ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling
4224c71b8cded575b0cfc133c5da4502ca613982094Richard Smithvoid CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E,
4234c71b8cded575b0cfc133c5da4502ca613982094Richard Smith                                       bool KeepInsertionPoint) {
424d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson  if (!E->getSubExpr()) {
425bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    EmitNoreturnRuntimeCallOrInvoke(getReThrowFn(CGM),
426bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                    ArrayRef<llvm::Value*>());
4271eb2e59338c4b9c0429fc39ca98662adc9e7a3f2Douglas Gregor
428cd5b22e12b6513163dd131589746c194090f14e6John McCall    // throw is an expression, and the expression emitters expect us
429cd5b22e12b6513163dd131589746c194090f14e6John McCall    // to leave ourselves at a valid insertion point.
4304c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    if (KeepInsertionPoint)
4314c71b8cded575b0cfc133c5da4502ca613982094Richard Smith      EmitBlock(createBasicBlock("throw.cont"));
432cd5b22e12b6513163dd131589746c194090f14e6John McCall
433d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson    return;
434d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson  }
4358755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
436d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson  QualType ThrowType = E->getSubExpr()->getType();
4378755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
4386a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian  if (ThrowType->isObjCObjectPointerType()) {
4396a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    const Stmt *ThrowStmt = E->getSubExpr();
4406a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    const ObjCAtThrowStmt S(E->getExprLoc(),
4416a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian                            const_cast<Stmt *>(ThrowStmt));
4426a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    CGM.getObjCRuntime().EmitThrowStmt(*this, S, false);
4436a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    // This will clear insertion point which was not cleared in
4446a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    // call to EmitThrowStmt.
4454c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    if (KeepInsertionPoint)
4464c71b8cded575b0cfc133c5da4502ca613982094Richard Smith      EmitBlock(createBasicBlock("throw.cont"));
4476a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    return;
4486a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian  }
4496a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian
450d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson  // Now allocate the exception object.
4512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
4523d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
4538755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
454629df0196eb305221d2c8aa9ab72293d05846f0bJohn McCall  llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
455f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::CallInst *ExceptionPtr =
456bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    EmitNounwindRuntimeCall(AllocExceptionFn,
457bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                            llvm::ConstantInt::get(SizeTy, TypeSize),
458bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                            "exception");
4598370c58b9295b32bee50443fe3ac43a47a2047e8Anders Carlsson
460ac418162692a951ca3796d6830496a85a2d12493John McCall  EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
4618755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
462d3379292f90e1381d3236c68891bb725b02464b6Anders Carlsson  // Now throw the exception.
46382a113adf8063baa70251dfa269d039ca22e2537Anders Carlsson  llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
46482a113adf8063baa70251dfa269d039ca22e2537Anders Carlsson                                                         /*ForEH=*/true);
465ac418162692a951ca3796d6830496a85a2d12493John McCall
466ac418162692a951ca3796d6830496a85a2d12493John McCall  // The address of the destructor.  If the exception type has a
467ac418162692a951ca3796d6830496a85a2d12493John McCall  // trivial destructor (or isn't a record), we just pass null.
468ac418162692a951ca3796d6830496a85a2d12493John McCall  llvm::Constant *Dtor = 0;
469ac418162692a951ca3796d6830496a85a2d12493John McCall  if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
470ac418162692a951ca3796d6830496a85a2d12493John McCall    CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
471ac418162692a951ca3796d6830496a85a2d12493John McCall    if (!Record->hasTrivialDestructor()) {
4721d110e05e0ff48c1c7a483d6b7fd094cdf28316aDouglas Gregor      CXXDestructorDecl *DtorD = Record->getDestructor();
473ac418162692a951ca3796d6830496a85a2d12493John McCall      Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
474ac418162692a951ca3796d6830496a85a2d12493John McCall      Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
475ac418162692a951ca3796d6830496a85a2d12493John McCall    }
476ac418162692a951ca3796d6830496a85a2d12493John McCall  }
477ac418162692a951ca3796d6830496a85a2d12493John McCall  if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
4788755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
479bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
480bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
4818755ec336108839b9621c3b18f0e175f8a3b671cMike Stump
482cd5b22e12b6513163dd131589746c194090f14e6John McCall  // throw is an expression, and the expression emitters expect us
483cd5b22e12b6513163dd131589746c194090f14e6John McCall  // to leave ourselves at a valid insertion point.
4844c71b8cded575b0cfc133c5da4502ca613982094Richard Smith  if (KeepInsertionPoint)
4854c71b8cded575b0cfc133c5da4502ca613982094Richard Smith    EmitBlock(createBasicBlock("throw.cont"));
486756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson}
4872bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
488cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stumpvoid CodeGenFunction::EmitStartEHSpec(const Decl *D) {
4894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!CGM.getLangOpts().CXXExceptions)
490a994ee4b197554282ae6b222c3284ccaa3a6484cAnders Carlsson    return;
491a994ee4b197554282ae6b222c3284ccaa3a6484cAnders Carlsson
492cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
493cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  if (FD == 0)
494cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump    return;
495cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
496cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  if (Proto == 0)
497cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump    return;
498cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
499a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  ExceptionSpecificationType EST = Proto->getExceptionSpecType();
500a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  if (isNoexceptExceptionSpec(EST)) {
501a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
502a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      // noexcept functions are simple terminate scopes.
503a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      EHStack.pushTerminate();
504a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    }
505a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
506a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    unsigned NumExceptions = Proto->getNumExceptions();
507a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
508a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl
509a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    for (unsigned I = 0; I != NumExceptions; ++I) {
510a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      QualType Ty = Proto->getExceptionType(I);
511a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
512a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
513a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl                                                        /*ForEH=*/true);
514a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      Filter->setFilter(I, EHType);
515a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    }
516cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  }
517cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump}
518cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
519777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall/// Emit the dispatch block for a filter scope if necessary.
520777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallstatic void emitFilterDispatchBlock(CodeGenFunction &CGF,
521777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall                                    EHFilterScope &filterScope) {
522777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
523777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (!dispatchBlock) return;
524777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (dispatchBlock->use_empty()) {
525777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    delete dispatchBlock;
526777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    return;
527777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
528777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
529777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  CGF.EmitBlockAfterUses(dispatchBlock);
530777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
531777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // If this isn't a catch-all filter, we need to check whether we got
532777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // here because the filter triggered.
533777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (filterScope.getNumFilters()) {
534777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // Load the selector value.
535ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling    llvm::Value *selector = CGF.getSelectorFromSlot();
536777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
537777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
538777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::Value *zero = CGF.Builder.getInt32(0);
539777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::Value *failsFilter =
540777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
541c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall    CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, CGF.getEHResumeBlock(false));
542777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
543777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    CGF.EmitBlock(unexpectedBB);
544777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
545777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
546777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Call __cxa_call_unexpected.  This doesn't need to be an invoke
547777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // because __cxa_call_unexpected magically filters exceptions
548777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // according to the last landing pad the exception was thrown
549777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // into.  Seriously.
550ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  llvm::Value *exn = CGF.getExceptionFromSlot();
551bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn)
552777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    ->setDoesNotReturn();
553777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  CGF.Builder.CreateUnreachable();
554777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
555777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
556cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stumpvoid CodeGenFunction::EmitEndEHSpec(const Decl *D) {
5574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!CGM.getLangOpts().CXXExceptions)
558a994ee4b197554282ae6b222c3284ccaa3a6484cAnders Carlsson    return;
559a994ee4b197554282ae6b222c3284ccaa3a6484cAnders Carlsson
560cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
561cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  if (FD == 0)
562cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump    return;
563cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
564cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  if (Proto == 0)
565cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump    return;
566cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
567a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  ExceptionSpecificationType EST = Proto->getExceptionSpecType();
568a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  if (isNoexceptExceptionSpec(EST)) {
569a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
570a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl      EHStack.popTerminate();
571a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    }
572a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
573777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
574777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    emitFilterDispatchBlock(*this, filterScope);
575a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl    EHStack.popFilter();
576a968e97947b1281c3bb3c4d47a952b3801d9bb02Sebastian Redl  }
577cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump}
578cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
5792bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stumpvoid CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
58059a7000a79118e4c140885ccbb2ac6a686a73092John McCall  EnterCXXTryStmt(S);
5819fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  EmitStmt(S.getTryBlock());
58259a7000a79118e4c140885ccbb2ac6a686a73092John McCall  ExitCXXTryStmt(S);
5839fc6a7774643a810c8501dae2323e863fefb623eJohn McCall}
5849fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
58559a7000a79118e4c140885ccbb2ac6a686a73092John McCallvoid CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
586f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  unsigned NumHandlers = S.getNumHandlers();
587f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
588f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
589f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  for (unsigned I = 0; I != NumHandlers; ++I) {
590f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    const CXXCatchStmt *C = S.getHandler(I);
591f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
592f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    llvm::BasicBlock *Handler = createBasicBlock("catch");
593f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (C->getExceptionDecl()) {
594f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // FIXME: Dropping the reference type on the type into makes it
595f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // impossible to correctly implement catch-by-reference
596f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // semantics for pointers.  Unfortunately, this is what all
597f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // existing compilers do, and it's not clear that the standard
598f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // personality routine is capable of doing this right.  See C++ DR 388:
599f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      //   http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
600f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      QualType CaughtType = C->getCaughtType();
601f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType();
6025a180397870944548aaadeaebf58e415885b9489John McCall
6035a180397870944548aaadeaebf58e415885b9489John McCall      llvm::Value *TypeInfo = 0;
6045a180397870944548aaadeaebf58e415885b9489John McCall      if (CaughtType->isObjCObjectPointerType())
605cf5abc7ba032bd35158e4d75b0bc92a482fc67e8Fariborz Jahanian        TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
6065a180397870944548aaadeaebf58e415885b9489John McCall      else
60782a113adf8063baa70251dfa269d039ca22e2537Anders Carlsson        TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true);
608f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CatchScope->setHandler(I, TypeInfo, Handler);
609f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    } else {
610f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // No exception decl indicates '...', a catch-all.
611f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CatchScope->setCatchAllHandler(I, Handler);
612f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
613f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
614f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
615f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
616777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallllvm::BasicBlock *
617777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallCodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
618777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // The dispatch block for the end of the scope chain is a block that
619777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // just resumes unwinding.
620777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (si == EHStack.stable_end())
621c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall    return getEHResumeBlock(true);
622777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
623777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Otherwise, we should look at the actual scope.
624777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EHScope &scope = *EHStack.find(si);
625777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
626777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
627777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (!dispatchBlock) {
628777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    switch (scope.getKind()) {
629777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    case EHScope::Catch: {
630777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      // Apply a special case to a single catch-all.
631777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      EHCatchScope &catchScope = cast<EHCatchScope>(scope);
632777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      if (catchScope.getNumHandlers() == 1 &&
633777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall          catchScope.getHandler(0).isCatchAll()) {
634777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall        dispatchBlock = catchScope.getHandler(0).Block;
635777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
636777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      // Otherwise, make a dispatch block.
637777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      } else {
638777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall        dispatchBlock = createBasicBlock("catch.dispatch");
639777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      }
640777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
641777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
642777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
643777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    case EHScope::Cleanup:
644777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      dispatchBlock = createBasicBlock("ehcleanup");
645777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
646777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
647777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    case EHScope::Filter:
648777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      dispatchBlock = createBasicBlock("filter.dispatch");
649777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
650777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
651777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    case EHScope::Terminate:
652777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      dispatchBlock = getTerminateHandler();
653777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      break;
654777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
655777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    scope.setCachedEHDispatchBlock(dispatchBlock);
656777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
657777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  return dispatchBlock;
658777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
659777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
660f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Check whether this is a non-EH scope, i.e. a scope which doesn't
661f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// affect exception handling.  Currently, the only non-EH scopes are
662f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// normal-only cleanup scopes.
663f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic bool isNonEHScope(const EHScope &S) {
664da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  switch (S.getKind()) {
6651f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  case EHScope::Cleanup:
6661f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    return !cast<EHCleanupScope>(S).isEHCleanup();
667da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  case EHScope::Filter:
668da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  case EHScope::Catch:
669da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  case EHScope::Terminate:
670da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    return false;
671da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
672da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
6733026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie  llvm_unreachable("Invalid EHScope Kind!");
674f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
6759fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
676f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
677f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.requiresLandingPad());
678f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(!EHStack.empty());
6799fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
6804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!CGM.getLangOpts().Exceptions)
681da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    return 0;
682da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
683f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Check the innermost scope for a cached landing pad.  If this is
684f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
685f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
686f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (LP) return LP;
687f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
688f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Build the landing pad for this scope.
689f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  LP = EmitLandingPad();
690f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(LP);
691f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
692f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Cache the landing pad on the innermost scope.  If this is a
693f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // non-EH scope, cache the landing pad on the enclosing scope, too.
694f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
695f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ir->setCachedLandingPad(LP);
696f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (!isNonEHScope(*ir)) break;
697f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
698f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
699f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return LP;
7009fc6a7774643a810c8501dae2323e863fefb623eJohn McCall}
7019fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
70293c332a8ba2c193c435b293966d343dab15f555bJohn McCall// This code contains a hack to work around a design flaw in
70393c332a8ba2c193c435b293966d343dab15f555bJohn McCall// LLVM's EH IR which breaks semantics after inlining.  This same
70493c332a8ba2c193c435b293966d343dab15f555bJohn McCall// hack is implemented in llvm-gcc.
70593c332a8ba2c193c435b293966d343dab15f555bJohn McCall//
70693c332a8ba2c193c435b293966d343dab15f555bJohn McCall// The LLVM EH abstraction is basically a thin veneer over the
70793c332a8ba2c193c435b293966d343dab15f555bJohn McCall// traditional GCC zero-cost design: for each range of instructions
70893c332a8ba2c193c435b293966d343dab15f555bJohn McCall// in the function, there is (at most) one "landing pad" with an
70993c332a8ba2c193c435b293966d343dab15f555bJohn McCall// associated chain of EH actions.  A language-specific personality
71093c332a8ba2c193c435b293966d343dab15f555bJohn McCall// function interprets this chain of actions and (1) decides whether
71193c332a8ba2c193c435b293966d343dab15f555bJohn McCall// or not to resume execution at the landing pad and (2) if so,
71293c332a8ba2c193c435b293966d343dab15f555bJohn McCall// provides an integer indicating why it's stopping.  In LLVM IR,
71393c332a8ba2c193c435b293966d343dab15f555bJohn McCall// the association of a landing pad with a range of instructions is
71493c332a8ba2c193c435b293966d343dab15f555bJohn McCall// achieved via an invoke instruction, the chain of actions becomes
71593c332a8ba2c193c435b293966d343dab15f555bJohn McCall// the arguments to the @llvm.eh.selector call, and the selector
71693c332a8ba2c193c435b293966d343dab15f555bJohn McCall// call returns the integer indicator.  Other than the required
71793c332a8ba2c193c435b293966d343dab15f555bJohn McCall// presence of two intrinsic function calls in the landing pad,
71893c332a8ba2c193c435b293966d343dab15f555bJohn McCall// the IR exactly describes the layout of the output code.
71993c332a8ba2c193c435b293966d343dab15f555bJohn McCall//
72093c332a8ba2c193c435b293966d343dab15f555bJohn McCall// A principal advantage of this design is that it is completely
72193c332a8ba2c193c435b293966d343dab15f555bJohn McCall// language-agnostic; in theory, the LLVM optimizers can treat
72293c332a8ba2c193c435b293966d343dab15f555bJohn McCall// landing pads neutrally, and targets need only know how to lower
72393c332a8ba2c193c435b293966d343dab15f555bJohn McCall// the intrinsics to have a functioning exceptions system (assuming
72493c332a8ba2c193c435b293966d343dab15f555bJohn McCall// that platform exceptions follow something approximately like the
72593c332a8ba2c193c435b293966d343dab15f555bJohn McCall// GCC design).  Unfortunately, landing pads cannot be combined in a
72693c332a8ba2c193c435b293966d343dab15f555bJohn McCall// language-agnostic way: given selectors A and B, there is no way
72793c332a8ba2c193c435b293966d343dab15f555bJohn McCall// to make a single landing pad which faithfully represents the
72893c332a8ba2c193c435b293966d343dab15f555bJohn McCall// semantics of propagating an exception first through A, then
72993c332a8ba2c193c435b293966d343dab15f555bJohn McCall// through B, without knowing how the personality will interpret the
73093c332a8ba2c193c435b293966d343dab15f555bJohn McCall// (lowered form of the) selectors.  This means that inlining has no
73193c332a8ba2c193c435b293966d343dab15f555bJohn McCall// choice but to crudely chain invokes (i.e., to ignore invokes in
73293c332a8ba2c193c435b293966d343dab15f555bJohn McCall// the inlined function, but to turn all unwindable calls into
73393c332a8ba2c193c435b293966d343dab15f555bJohn McCall// invokes), which is only semantically valid if every unwind stops
73493c332a8ba2c193c435b293966d343dab15f555bJohn McCall// at every landing pad.
73593c332a8ba2c193c435b293966d343dab15f555bJohn McCall//
73693c332a8ba2c193c435b293966d343dab15f555bJohn McCall// Therefore, the invoke-inline hack is to guarantee that every
73793c332a8ba2c193c435b293966d343dab15f555bJohn McCall// landing pad has a catch-all.
73893c332a8ba2c193c435b293966d343dab15f555bJohn McCallenum CleanupHackLevel_t {
73993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// A level of hack that requires that all landing pads have
74093c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// catch-alls.
74193c332a8ba2c193c435b293966d343dab15f555bJohn McCall  CHL_MandatoryCatchall,
74293c332a8ba2c193c435b293966d343dab15f555bJohn McCall
74393c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// A level of hack that requires that all landing pads handle
74493c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// cleanups.
74593c332a8ba2c193c435b293966d343dab15f555bJohn McCall  CHL_MandatoryCleanup,
74693c332a8ba2c193c435b293966d343dab15f555bJohn McCall
74793c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// No hacks at all;  ideal IR generation.
74893c332a8ba2c193c435b293966d343dab15f555bJohn McCall  CHL_Ideal
74993c332a8ba2c193c435b293966d343dab15f555bJohn McCall};
75093c332a8ba2c193c435b293966d343dab15f555bJohn McCallconst CleanupHackLevel_t CleanupHackLevel = CHL_MandatoryCleanup;
75193c332a8ba2c193c435b293966d343dab15f555bJohn McCall
752f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
753f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(EHStack.requiresLandingPad());
754f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
755777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
756777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  switch (innermostEHScope.getKind()) {
757777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  case EHScope::Terminate:
758777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    return getTerminateLandingPad();
759f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
760777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  case EHScope::Catch:
761777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  case EHScope::Cleanup:
762777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  case EHScope::Filter:
763777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
764777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      return lpad;
765f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
766f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
767f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Save the current IR generation state.
768777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
76959f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl  SourceLocation SavedLocation;
77059f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl  if (CGDebugInfo *DI = getDebugInfo()) {
77159f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl    SavedLocation = DI->getLocation();
77259f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl    DI->EmitLocation(Builder, CurEHLocation);
77359f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl  }
774f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
7754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const EHPersonality &personality = EHPersonality::get(getLangOpts());
7768262b6a44c98cf14e1d5f347a01e6bf44858198fJohn McCall
777f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Create and configure the landing pad.
778777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::BasicBlock *lpad = createBasicBlock("lpad");
779777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EmitBlock(lpad);
780f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
781285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  llvm::LandingPadInst *LPadInst =
782285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
783285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling                             getOpaquePersonalityFn(CGM, personality), 0);
784285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling
785285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
786285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  Builder.CreateStore(LPadExn, getExceptionSlot());
787285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
788285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  Builder.CreateStore(LPadSel, getEHSelectorSlot());
789285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling
790f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Save the exception pointer.  It's safe to use a single exception
791f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // pointer per function because EH cleanups can never have nested
792f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // try/catches.
793285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  // Build the landingpad instruction.
794f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
795f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Accumulate all the handlers in scope.
796777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  bool hasCatchAll = false;
797777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  bool hasCleanup = false;
798777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  bool hasFilter = false;
799777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  SmallVector<llvm::Value*, 4> filterTypes;
800777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
801f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end();
802f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         I != E; ++I) {
803f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
804f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    switch (I->getKind()) {
8051f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    case EHScope::Cleanup:
806777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      // If we have a cleanup, remember that.
807777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
808da65ea86482bc116906edfb9ba1d7124f76cc867John McCall      continue;
809da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
810f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    case EHScope::Filter: {
811f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
812777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      assert(!hasCatchAll && "EH filter reached after catch-all");
813f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
814285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      // Filter scopes get added to the landingpad in weird ways.
815777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      EHFilterScope &filter = cast<EHFilterScope>(*I);
816777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      hasFilter = true;
817f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
8188990daf237a48fa2eed3d0546687fc097a004db6Bill Wendling      // Add all the filter values.
8198990daf237a48fa2eed3d0546687fc097a004db6Bill Wendling      for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
8208990daf237a48fa2eed3d0546687fc097a004db6Bill Wendling        filterTypes.push_back(filter.getFilter(i));
821f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      goto done;
822f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
823f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
824f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    case EHScope::Terminate:
825f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // Terminate scopes are basically catch-alls.
826777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      assert(!hasCatchAll);
827777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      hasCatchAll = true;
828f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      goto done;
829f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
830f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    case EHScope::Catch:
831f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      break;
832f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
833f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
834777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    EHCatchScope &catchScope = cast<EHCatchScope>(*I);
835777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
836777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      EHCatchScope::Handler handler = catchScope.getHandler(hi);
837777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
838777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      // If this is a catch-all, register that and abort.
839777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      if (!handler.Type) {
840777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall        assert(!hasCatchAll);
841777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall        hasCatchAll = true;
842777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall        goto done;
843f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      }
844f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
845f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      // Check whether we already have a handler for this type.
846285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      if (catchTypes.insert(handler.Type))
847285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling        // If not, add it directly to the landingpad.
848285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling        LPadInst->addClause(handler.Type);
8492bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump    }
8502bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump  }
8512bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
852f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall done:
853285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  // If we have a catch-all, add null to the landingpad.
854777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  assert(!(hasCatchAll && hasFilter));
855777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (hasCatchAll) {
856285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    LPadInst->addClause(getCatchAllValue(*this));
857f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
858f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If we have an EH filter, we need to add those handlers in the
859285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  // right place in the landingpad, which is to say, at the end.
860777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  } else if (hasFilter) {
86140ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    // Create a filter expression: a constant array indicating which filter
86240ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    // types there are. The personality routine only lands here if the filter
86340ccaccd21a4377cd76d6adda2b192dcf9514ef6Bill Wendling    // doesn't match.
864cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko    SmallVector<llvm::Constant*, 8> Filters;
865285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    llvm::ArrayType *AType =
866285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      llvm::ArrayType::get(!filterTypes.empty() ?
867285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling                             filterTypes[0]->getType() : Int8PtrTy,
868285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling                           filterTypes.size());
869285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling
870285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
871285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
872285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
873285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    LPadInst->addClause(FilterArray);
874f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
875f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Also check whether we need a cleanup.
876285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    if (hasCleanup)
877285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      LPadInst->setCleanup(true);
878f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
879f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Otherwise, signal that we at least have cleanups.
880777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  } else if (CleanupHackLevel == CHL_MandatoryCatchall || hasCleanup) {
881285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    if (CleanupHackLevel == CHL_MandatoryCatchall)
882285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      LPadInst->addClause(getCatchAllValue(*this));
883285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    else
884285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      LPadInst->setCleanup(true);
8850f590be3808365e851352543faa6acbece50b686Mike Stump  }
8862bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
887285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
888285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling         "landingpad instruction has no clauses!");
889f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
890f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Tell the backend how to generate the landing pad.
891777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
892f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
893f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Restore the old IR generation state.
894777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  Builder.restoreIP(savedIP);
89559f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl  if (CGDebugInfo *DI = getDebugInfo())
89659f0a5a5481a6dcfafd092dd944e6cfb7d146d4cAdrian Prantl    DI->EmitLocation(Builder, SavedLocation);
897f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
898777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  return lpad;
899f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
9000f590be3808365e851352543faa6acbece50b686Mike Stump
9018e3f86193995c47ee0d229e4336c3382410f09f5John McCallnamespace {
9028e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// A cleanup to call __cxa_end_catch.  In many cases, the caught
9038e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// exception type lets us state definitively that the thrown exception
9048e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// type does not have a destructor.  In particular:
9058e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///   - Catch-alls tell us nothing, so we have to conservatively
9068e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///     assume that the thrown exception might have a destructor.
9078e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///   - Catches by reference behave according to their base types.
9088e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///   - Catches of non-record types will only trigger for exceptions
9098e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///     of non-record types, which never have destructors.
9108e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///   - Catches of record types can trigger for arbitrary subclasses
9118e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///     of the caught type, so we have to assume the actual thrown
9128e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///     exception type might have a throwing destructor, even if the
9138e3f86193995c47ee0d229e4336c3382410f09f5John McCall  ///     caught type's destructor is trivial or nothrow.
9141f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  struct CallEndCatch : EHScopeStack::Cleanup {
9158e3f86193995c47ee0d229e4336c3382410f09f5John McCall    CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
9168e3f86193995c47ee0d229e4336c3382410f09f5John McCall    bool MightThrow;
9178e3f86193995c47ee0d229e4336c3382410f09f5John McCall
918ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
9198e3f86193995c47ee0d229e4336c3382410f09f5John McCall      if (!MightThrow) {
920bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall        CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
9218e3f86193995c47ee0d229e4336c3382410f09f5John McCall        return;
9228e3f86193995c47ee0d229e4336c3382410f09f5John McCall      }
9238e3f86193995c47ee0d229e4336c3382410f09f5John McCall
924bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
9258e3f86193995c47ee0d229e4336c3382410f09f5John McCall    }
9268e3f86193995c47ee0d229e4336c3382410f09f5John McCall  };
9278e3f86193995c47ee0d229e4336c3382410f09f5John McCall}
9288e3f86193995c47ee0d229e4336c3382410f09f5John McCall
929f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call to __cxa_begin_catch and enters a cleanup to call
930f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// __cxa_end_catch.
9318e3f86193995c47ee0d229e4336c3382410f09f5John McCall///
9328e3f86193995c47ee0d229e4336c3382410f09f5John McCall/// \param EndMightThrow - true if __cxa_end_catch might throw
9338e3f86193995c47ee0d229e4336c3382410f09f5John McCallstatic llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
9348e3f86193995c47ee0d229e4336c3382410f09f5John McCall                                   llvm::Value *Exn,
9358e3f86193995c47ee0d229e4336c3382410f09f5John McCall                                   bool EndMightThrow) {
936bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *call =
937bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
9380f590be3808365e851352543faa6acbece50b686Mike Stump
9391f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
940f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
941bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return call;
942f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
943f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
944f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// A "special initializer" callback for initializing a catch
945f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// parameter during catch initialization.
946f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstatic void InitCatchParam(CodeGenFunction &CGF,
947f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                           const VarDecl &CatchParam,
948f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                           llvm::Value *ParamAddr) {
949f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Load the exception from where the landing pad saved it.
950ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  llvm::Value *Exn = CGF.getExceptionFromSlot();
951f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
952f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CanQualType CatchType =
953f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
9542acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
955f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
956f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // If we're catching by reference, we can just cast the object
957f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // pointer to the appropriate pointer.
958f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (isa<ReferenceType>(CatchType)) {
959204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
960204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    bool EndCatchMightThrow = CaughtType->isRecordType();
9618e3f86193995c47ee0d229e4336c3382410f09f5John McCall
962f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // __cxa_begin_catch returns the adjusted object pointer.
9638e3f86193995c47ee0d229e4336c3382410f09f5John McCall    llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
964204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
965204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    // We have no way to tell the personality function that we're
966204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    // catching by reference, so if we're catching a pointer,
967204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    // __cxa_begin_catch will actually return that pointer by value.
968204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
969204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      QualType PointeeType = PT->getPointeeType();
970204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
971204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // When catching by reference, generally we should just ignore
972204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // this by-value pointer and use the exception object instead.
973204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      if (!PointeeType->isRecordType()) {
974204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
975204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        // Exn points to the struct _Unwind_Exception header, which
976204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        // we have to skip past in order to reach the exception data.
977204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        unsigned HeaderSize =
978204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall          CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
979204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
980204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
981204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // However, if we're catching a pointer-to-record type that won't
982204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // work, because the personality function might have adjusted
983204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // the pointer.  There's actually no way for us to fully satisfy
984204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // the language/ABI contract here:  we can't use Exn because it
985204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // might have the wrong adjustment, but we can't use the by-value
986204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // pointer because it's off by a level of abstraction.
987204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      //
988204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // The current solution is to dump the adjusted pointer into an
989204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // alloca, which breaks language semantics (because changing the
990204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // pointer doesn't change the exception) but at least works.
991204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // The better solution would be to filter out non-exact matches
992204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // and rethrow them, but this is tricky because the rethrow
993204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // really needs to be catchable by other sites at this landing
994204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      // pad.  The best solution is to fix the personality function.
995204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      } else {
996204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        // Pull the pointer for the reference type off.
9972acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner        llvm::Type *PtrTy =
998204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall          cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
999204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
1000204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        // Create the temporary and write the adjusted pointer into it.
1001204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
1002204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
1003204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        CGF.Builder.CreateStore(Casted, ExnPtrTmp);
1004204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
1005204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        // Bind the reference to the temporary.
1006204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall        AdjustedExn = ExnPtrTmp;
1007204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall      }
1008204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall    }
1009204b075fcc47c3f2aa7276dfba9b42eb25840b53John McCall
1010f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    llvm::Value *ExnCast =
1011f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
1012f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    CGF.Builder.CreateStore(ExnCast, ParamAddr);
1013f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return;
1014a086783570f76062a345e761811296dc8df571c8Mike Stump  }
10152bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
10169d232c884ea9872d6555df0fd7359699819bc1f1John McCall  // Scalars and complexes.
10179d232c884ea9872d6555df0fd7359699819bc1f1John McCall  TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
10189d232c884ea9872d6555df0fd7359699819bc1f1John McCall  if (TEK != TEK_Aggregate) {
10198e3f86193995c47ee0d229e4336c3382410f09f5John McCall    llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
1020f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1021f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // If the catch type is a pointer type, __cxa_begin_catch returns
1022f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // the pointer by value.
1023f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (CatchType->hasPointerRepresentation()) {
1024f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      llvm::Value *CastExn =
1025f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
1026b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall
1027b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      switch (CatchType.getQualifiers().getObjCLifetime()) {
1028b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      case Qualifiers::OCL_Strong:
1029b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall        CastExn = CGF.EmitARCRetainNonBlock(CastExn);
1030b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall        // fallthrough
1031b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall
1032b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      case Qualifiers::OCL_None:
1033b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      case Qualifiers::OCL_ExplicitNone:
1034b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      case Qualifiers::OCL_Autoreleasing:
1035b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall        CGF.Builder.CreateStore(CastExn, ParamAddr);
1036b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall        return;
1037b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall
1038b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      case Qualifiers::OCL_Weak:
1039b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall        CGF.EmitARCInitWeak(ParamAddr, CastExn);
1040b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall        return;
1041b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      }
1042b29b12d494babf061201ffbcbcacddd21abec50eJohn McCall      llvm_unreachable("bad ownership qualifier!");
1043f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
10442bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
1045f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Otherwise, it returns a pointer into the exception object.
10462bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
10472acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
1048f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
10492bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
10509d232c884ea9872d6555df0fd7359699819bc1f1John McCall    LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
10519d232c884ea9872d6555df0fd7359699819bc1f1John McCall    LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType,
10529d232c884ea9872d6555df0fd7359699819bc1f1John McCall                                  CGF.getContext().getDeclAlign(&CatchParam));
10539d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (TEK) {
10549d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Complex:
10559d232c884ea9872d6555df0fd7359699819bc1f1John McCall      CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV), destLV,
10569d232c884ea9872d6555df0fd7359699819bc1f1John McCall                             /*init*/ true);
10579d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return;
10589d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Scalar: {
10599d232c884ea9872d6555df0fd7359699819bc1f1John McCall      llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV);
10609d232c884ea9872d6555df0fd7359699819bc1f1John McCall      CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
10619d232c884ea9872d6555df0fd7359699819bc1f1John McCall      return;
1062f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
10639d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Aggregate:
10649d232c884ea9872d6555df0fd7359699819bc1f1John McCall      llvm_unreachable("evaluation kind filtered out!");
10659d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
10669d232c884ea9872d6555df0fd7359699819bc1f1John McCall    llvm_unreachable("bad evaluation kind");
1067f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
10680f590be3808365e851352543faa6acbece50b686Mike Stump
1069acff696118d98c3acd09a16b96c95807057b5c99John McCall  assert(isa<RecordType>(CatchType) && "unexpected catch type!");
1070b2c9c0b1c5e25cfbee1403cde12b98f180e6b315Mike Stump
10712acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
1072f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1073acff696118d98c3acd09a16b96c95807057b5c99John McCall  // Check for a copy expression.  If we don't have a copy expression,
1074acff696118d98c3acd09a16b96c95807057b5c99John McCall  // that means a trivial copy is okay.
1075e996ffd240f20a1048179d7727a6ee3227261921John McCall  const Expr *copyExpr = CatchParam.getInit();
1076e996ffd240f20a1048179d7727a6ee3227261921John McCall  if (!copyExpr) {
1077acff696118d98c3acd09a16b96c95807057b5c99John McCall    llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
1078acff696118d98c3acd09a16b96c95807057b5c99John McCall    llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1079649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
1080f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return;
1081f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
1082f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1083f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // We have to call __cxa_get_exception_ptr to get the adjusted
1084f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // pointer before copying.
1085e996ffd240f20a1048179d7727a6ee3227261921John McCall  llvm::CallInst *rawAdjustedExn =
1086bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
1087f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1088e996ffd240f20a1048179d7727a6ee3227261921John McCall  // Cast that to the appropriate type.
1089e996ffd240f20a1048179d7727a6ee3227261921John McCall  llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1090f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1091e996ffd240f20a1048179d7727a6ee3227261921John McCall  // The copy expression is defined in terms of an OpaqueValueExpr.
1092e996ffd240f20a1048179d7727a6ee3227261921John McCall  // Find it and map it to the adjusted expression.
1093e996ffd240f20a1048179d7727a6ee3227261921John McCall  CodeGenFunction::OpaqueValueMapping
109456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
109556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall           CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
1096f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1097f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Call the copy ctor in a terminate scope.
1098f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGF.EHStack.pushTerminate();
1099e996ffd240f20a1048179d7727a6ee3227261921John McCall
1100e996ffd240f20a1048179d7727a6ee3227261921John McCall  // Perform the copy construction.
1101d7722d9d76a851e7897f4127626616d3b1b8e530Eli Friedman  CharUnits Alignment = CGF.getContext().getDeclAlign(&CatchParam);
1102f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman  CGF.EmitAggExpr(copyExpr,
1103f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman                  AggValueSlot::forAddr(ParamAddr, Alignment, Qualifiers(),
1104f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman                                        AggValueSlot::IsNotDestructed,
1105f394078fde147dcf27e9b6a7965517388d64dcb6Eli Friedman                                        AggValueSlot::DoesNotNeedGCBarriers,
1106649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                        AggValueSlot::IsNotAliased));
1107e996ffd240f20a1048179d7727a6ee3227261921John McCall
1108e996ffd240f20a1048179d7727a6ee3227261921John McCall  // Leave the terminate scope.
1109f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGF.EHStack.popTerminate();
1110f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1111e996ffd240f20a1048179d7727a6ee3227261921John McCall  // Undo the opaque value mapping.
1112e996ffd240f20a1048179d7727a6ee3227261921John McCall  opaque.pop();
1113e996ffd240f20a1048179d7727a6ee3227261921John McCall
1114f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Finally we can call __cxa_begin_catch.
11158e3f86193995c47ee0d229e4336c3382410f09f5John McCall  CallBeginCatch(CGF, Exn, true);
1116f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1117f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1118f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Begins a catch statement by initializing the catch variable and
1119f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// calling __cxa_begin_catch.
1120e996ffd240f20a1048179d7727a6ee3227261921John McCallstatic void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
1121f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // We have to be very careful with the ordering of cleanups here:
1122f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   C++ [except.throw]p4:
1123f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //     The destruction [of the exception temporary] occurs
1124f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //     immediately after the destruction of the object declared in
1125f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //     the exception-declaration in the handler.
1126f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //
1127f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // So the precise ordering is:
1128f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   1.  Construct catch variable.
1129f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   2.  __cxa_begin_catch
1130f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   3.  Enter __cxa_end_catch cleanup
1131f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   4.  Enter dtor cleanup
1132f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //
113334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  // We do this by using a slightly abnormal initialization process.
113434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  // Delegation sequence:
1135f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   - ExitCXXTryStmt opens a RunCleanupsScope
113634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  //     - EmitAutoVarAlloca creates the variable and debug info
1137f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //       - InitCatchParam initializes the variable from the exception
113834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  //       - CallBeginCatch calls __cxa_begin_catch
113934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  //       - CallBeginCatch enters the __cxa_end_catch cleanup
114034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  //     - EmitAutoVarCleanups enters the variable destructor cleanup
1141f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   - EmitCXXTryStmt emits the code for the catch body
1142f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   - EmitCXXTryStmt close the RunCleanupsScope
1143f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1144f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  VarDecl *CatchParam = S->getExceptionDecl();
1145f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!CatchParam) {
1146ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling    llvm::Value *Exn = CGF.getExceptionFromSlot();
11478e3f86193995c47ee0d229e4336c3382410f09f5John McCall    CallBeginCatch(CGF, Exn, true);
1148f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return;
1149f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
1150f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1151f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Emit the local.
115234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
115334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF));
115434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  CGF.EmitAutoVarCleanups(var);
1155f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1156f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1157777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall/// Emit the structure of the dispatch block for the given catch scope.
1158777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall/// It is an invariant that the dispatch block already exists.
1159777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallstatic void emitCatchDispatchBlock(CodeGenFunction &CGF,
1160777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall                                   EHCatchScope &catchScope) {
1161777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1162777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  assert(dispatchBlock);
1163777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1164777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // If there's only a single catch-all, getEHDispatchBlock returned
1165777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // that catch-all as the dispatch block.
1166777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (catchScope.getNumHandlers() == 1 &&
1167777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      catchScope.getHandler(0).isCatchAll()) {
1168777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    assert(dispatchBlock == catchScope.getHandler(0).Block);
1169777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    return;
1170777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
1171777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1172777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1173777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  CGF.EmitBlockAfterUses(dispatchBlock);
1174777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1175777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Select the right handler.
1176777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  llvm::Value *llvm_eh_typeid_for =
1177777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1178777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1179777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Load the selector value.
1180ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling  llvm::Value *selector = CGF.getSelectorFromSlot();
1181777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1182777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Test against each of the exception types we claim to catch.
1183777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1184777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    assert(i < e && "ran off end of handlers!");
1185777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1186777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1187777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::Value *typeValue = handler.Type;
1188777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    assert(typeValue && "fell into catch-all case!");
1189777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1190777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1191777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // Figure out the next block.
1192777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    bool nextIsEnd;
1193777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::BasicBlock *nextBlock;
1194777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1195777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // If this is the last handler, we're at the end, and the next
1196777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // block is the block for the enclosing EH scope.
1197777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    if (i + 1 == e) {
1198777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1199777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      nextIsEnd = true;
1200777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1201777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // If the next handler is a catch-all, we're at the end, and the
1202777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // next block is that handler.
1203777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    } else if (catchScope.getHandler(i+1).isCatchAll()) {
1204777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      nextBlock = catchScope.getHandler(i+1).Block;
1205777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      nextIsEnd = true;
1206777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1207777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // Otherwise, we're not at the end and we need a new block.
1208777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    } else {
1209777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      nextBlock = CGF.createBasicBlock("catch.fallthrough");
1210777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      nextIsEnd = false;
1211777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
1212777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1213777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // Figure out the catch type's index in the LSDA's type table.
1214777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::CallInst *typeIndex =
1215777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1216777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    typeIndex->setDoesNotThrow();
1217777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1218777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::Value *matchesTypeIndex =
1219777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1220777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1221777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1222777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    // If the next handler is a catch-all, we're completely done.
1223777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    if (nextIsEnd) {
1224777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      CGF.Builder.restoreIP(savedIP);
1225777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall      return;
1226777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    }
1227e8e92b9dccc362be33a7f9bb84a114b18db65b10Ahmed Charles    // Otherwise we need to emit and continue at that block.
1228e8e92b9dccc362be33a7f9bb84a114b18db65b10Ahmed Charles    CGF.EmitBlock(nextBlock);
1229777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
1230777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
1231777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1232777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCallvoid CodeGenFunction::popCatchScope() {
1233777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1234777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (catchScope.hasEHBranches())
1235777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    emitCatchDispatchBlock(*this, catchScope);
1236777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EHStack.popCatch();
1237777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall}
1238777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
123959a7000a79118e4c140885ccbb2ac6a686a73092John McCallvoid CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
1240f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  unsigned NumHandlers = S.getNumHandlers();
1241f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1242f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  assert(CatchScope.getNumHandlers() == NumHandlers);
1243f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1244777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // If the catch was not required, bail out now.
1245777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (!CatchScope.hasEHBranches()) {
1246777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    EHStack.popCatch();
1247777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    return;
1248777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  }
1249777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1250777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Emit the structure of the EH dispatch for this catch.
1251777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  emitCatchDispatchBlock(*this, CatchScope);
1252777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1253f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Copy the handler blocks off before we pop the EH stack.  Emitting
1254f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the handlers might scribble on this memory.
12555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers);
1256f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  memcpy(Handlers.data(), CatchScope.begin(),
1257f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         NumHandlers * sizeof(EHCatchScope::Handler));
1258777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1259f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHStack.popCatch();
1260f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1261f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // The fall-through block.
1262f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
1263f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1264f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // We just emitted the body of the try; jump to the continue block.
1265f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (HaveInsertPoint())
1266f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    Builder.CreateBr(ContBB);
1267f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1268f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall  // Determine if we need an implicit rethrow for all these catch handlers;
1269f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall  // see the comment below.
1270f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall  bool doImplicitRethrow = false;
127159a7000a79118e4c140885ccbb2ac6a686a73092John McCall  if (IsFnTryBlock)
1272f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1273f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall                        isa<CXXConstructorDecl>(CurCodeDecl);
127459a7000a79118e4c140885ccbb2ac6a686a73092John McCall
1275777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // Perversely, we emit the handlers backwards precisely because we
1276777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // want them to appear in source order.  In all of these cases, the
1277777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // catch block will have exactly one predecessor, which will be a
1278777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // particular block in the catch dispatch.  However, in the case of
1279777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // a catch-all, one of the dispatch blocks will branch to two
1280777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // different handlers, and EmitBlockAfterUses will cause the second
1281777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  // handler to be moved before the first.
1282777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  for (unsigned I = NumHandlers; I != 0; --I) {
1283777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1284777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    EmitBlockAfterUses(CatchBlock);
1285f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1286f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Catch the exception if this isn't a catch-all.
1287777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall    const CXXCatchStmt *C = S.getHandler(I-1);
1288f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1289f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Enter a cleanup scope, including the catch variable and the
1290f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // end-catch.
1291f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope CatchScope(*this);
1292f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1293f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Initialize the catch variable and set up the cleanups.
1294f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    BeginCatch(*this, C);
1295f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1296f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Perform the body of the catch.
1297f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EmitStmt(C->getHandlerBlock());
1298f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1299f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    // [except.handle]p11:
1300f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    //   The currently handled exception is rethrown if control
1301f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    //   reaches the end of a handler of the function-try-block of a
1302f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    //   constructor or destructor.
1303f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall
1304f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    // It is important that we only do this on fallthrough and not on
1305f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    // return.  Note that it's illegal to put a return in a
1306f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    // constructor function-try-block's catch handler (p14), so this
1307f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    // really only applies to destructors.
1308f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    if (doImplicitRethrow && HaveInsertPoint()) {
1309bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      EmitRuntimeCallOrInvoke(getReThrowFn(CGM));
1310f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall      Builder.CreateUnreachable();
1311f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall      Builder.ClearInsertionPoint();
1312f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall    }
1313f5533019fb70d62917fd080f6152b6469e2c6cd5John McCall
1314f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Fall out through the catch cleanups.
1315f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    CatchScope.ForceCleanup();
1316f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1317f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    // Branch out of the try.
1318f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (HaveInsertPoint())
1319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      Builder.CreateBr(ContBB);
1320f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
13212bf701ee4babb5c4a9ea99ca4675c5ef040bd402Mike Stump
1322f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBlock(ContBB);
1323f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1324f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
132555b20fc514678ff8ae1627cd9aef047d1f780119John McCallnamespace {
13261f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  struct CallEndCatchForFinally : EHScopeStack::Cleanup {
132755b20fc514678ff8ae1627cd9aef047d1f780119John McCall    llvm::Value *ForEHVar;
132855b20fc514678ff8ae1627cd9aef047d1f780119John McCall    llvm::Value *EndCatchFn;
132955b20fc514678ff8ae1627cd9aef047d1f780119John McCall    CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
133055b20fc514678ff8ae1627cd9aef047d1f780119John McCall      : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
133155b20fc514678ff8ae1627cd9aef047d1f780119John McCall
1332ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
133355b20fc514678ff8ae1627cd9aef047d1f780119John McCall      llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
133455b20fc514678ff8ae1627cd9aef047d1f780119John McCall      llvm::BasicBlock *CleanupContBB =
133555b20fc514678ff8ae1627cd9aef047d1f780119John McCall        CGF.createBasicBlock("finally.cleanup.cont");
133655b20fc514678ff8ae1627cd9aef047d1f780119John McCall
133755b20fc514678ff8ae1627cd9aef047d1f780119John McCall      llvm::Value *ShouldEndCatch =
133855b20fc514678ff8ae1627cd9aef047d1f780119John McCall        CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch");
133955b20fc514678ff8ae1627cd9aef047d1f780119John McCall      CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
134055b20fc514678ff8ae1627cd9aef047d1f780119John McCall      CGF.EmitBlock(EndCatchBB);
1341bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw
134255b20fc514678ff8ae1627cd9aef047d1f780119John McCall      CGF.EmitBlock(CleanupContBB);
134355b20fc514678ff8ae1627cd9aef047d1f780119John McCall    }
134455b20fc514678ff8ae1627cd9aef047d1f780119John McCall  };
134577199713ab56f87ffad9a535ff2a0877704eed87John McCall
13461f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  struct PerformFinally : EHScopeStack::Cleanup {
134777199713ab56f87ffad9a535ff2a0877704eed87John McCall    const Stmt *Body;
134877199713ab56f87ffad9a535ff2a0877704eed87John McCall    llvm::Value *ForEHVar;
134977199713ab56f87ffad9a535ff2a0877704eed87John McCall    llvm::Value *EndCatchFn;
135077199713ab56f87ffad9a535ff2a0877704eed87John McCall    llvm::Value *RethrowFn;
135177199713ab56f87ffad9a535ff2a0877704eed87John McCall    llvm::Value *SavedExnVar;
135277199713ab56f87ffad9a535ff2a0877704eed87John McCall
135377199713ab56f87ffad9a535ff2a0877704eed87John McCall    PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
135477199713ab56f87ffad9a535ff2a0877704eed87John McCall                   llvm::Value *EndCatchFn,
135577199713ab56f87ffad9a535ff2a0877704eed87John McCall                   llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
135677199713ab56f87ffad9a535ff2a0877704eed87John McCall      : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
135777199713ab56f87ffad9a535ff2a0877704eed87John McCall        RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
135877199713ab56f87ffad9a535ff2a0877704eed87John McCall
1359ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
136077199713ab56f87ffad9a535ff2a0877704eed87John McCall      // Enter a cleanup to call the end-catch function if one was provided.
136177199713ab56f87ffad9a535ff2a0877704eed87John McCall      if (EndCatchFn)
13621f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall        CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
13631f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall                                                        ForEHVar, EndCatchFn);
136477199713ab56f87ffad9a535ff2a0877704eed87John McCall
1365d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall      // Save the current cleanup destination in case there are
1366d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall      // cleanups in the finally block.
1367d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall      llvm::Value *SavedCleanupDest =
1368d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall        CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1369d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall                               "cleanup.dest.saved");
1370d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall
137177199713ab56f87ffad9a535ff2a0877704eed87John McCall      // Emit the finally block.
137277199713ab56f87ffad9a535ff2a0877704eed87John McCall      CGF.EmitStmt(Body);
137377199713ab56f87ffad9a535ff2a0877704eed87John McCall
137477199713ab56f87ffad9a535ff2a0877704eed87John McCall      // If the end of the finally is reachable, check whether this was
137577199713ab56f87ffad9a535ff2a0877704eed87John McCall      // for EH.  If so, rethrow.
137677199713ab56f87ffad9a535ff2a0877704eed87John McCall      if (CGF.HaveInsertPoint()) {
137777199713ab56f87ffad9a535ff2a0877704eed87John McCall        llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
137877199713ab56f87ffad9a535ff2a0877704eed87John McCall        llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
137977199713ab56f87ffad9a535ff2a0877704eed87John McCall
138077199713ab56f87ffad9a535ff2a0877704eed87John McCall        llvm::Value *ShouldRethrow =
138177199713ab56f87ffad9a535ff2a0877704eed87John McCall          CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow");
138277199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
138377199713ab56f87ffad9a535ff2a0877704eed87John McCall
138477199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGF.EmitBlock(RethrowBB);
138577199713ab56f87ffad9a535ff2a0877704eed87John McCall        if (SavedExnVar) {
1386bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall          CGF.EmitRuntimeCallOrInvoke(RethrowFn,
1387bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                      CGF.Builder.CreateLoad(SavedExnVar));
138877199713ab56f87ffad9a535ff2a0877704eed87John McCall        } else {
1389bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall          CGF.EmitRuntimeCallOrInvoke(RethrowFn);
139077199713ab56f87ffad9a535ff2a0877704eed87John McCall        }
139177199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGF.Builder.CreateUnreachable();
139277199713ab56f87ffad9a535ff2a0877704eed87John McCall
139377199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGF.EmitBlock(ContBB);
1394d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall
1395d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall        // Restore the cleanup destination.
1396d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall        CGF.Builder.CreateStore(SavedCleanupDest,
1397d96a8e771ca9f406f0fa1dd4639997335ae444a7John McCall                                CGF.getNormalCleanupDestSlot());
139877199713ab56f87ffad9a535ff2a0877704eed87John McCall      }
139977199713ab56f87ffad9a535ff2a0877704eed87John McCall
140077199713ab56f87ffad9a535ff2a0877704eed87John McCall      // Leave the end-catch cleanup.  As an optimization, pretend that
140177199713ab56f87ffad9a535ff2a0877704eed87John McCall      // the fallthrough path was inaccessible; we've dynamically proven
140277199713ab56f87ffad9a535ff2a0877704eed87John McCall      // that we're not in the EH case along that path.
140377199713ab56f87ffad9a535ff2a0877704eed87John McCall      if (EndCatchFn) {
140477199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
140577199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGF.PopCleanupBlock();
140677199713ab56f87ffad9a535ff2a0877704eed87John McCall        CGF.Builder.restoreIP(SavedIP);
140777199713ab56f87ffad9a535ff2a0877704eed87John McCall      }
140877199713ab56f87ffad9a535ff2a0877704eed87John McCall
140977199713ab56f87ffad9a535ff2a0877704eed87John McCall      // Now make sure we actually have an insertion point or the
141077199713ab56f87ffad9a535ff2a0877704eed87John McCall      // cleanup gods will hate us.
141177199713ab56f87ffad9a535ff2a0877704eed87John McCall      CGF.EnsureInsertPoint();
141277199713ab56f87ffad9a535ff2a0877704eed87John McCall    }
141377199713ab56f87ffad9a535ff2a0877704eed87John McCall  };
141455b20fc514678ff8ae1627cd9aef047d1f780119John McCall}
141555b20fc514678ff8ae1627cd9aef047d1f780119John McCall
1416f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Enters a finally block for an implementation using zero-cost
1417f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// exceptions.  This is mostly general, but hard-codes some
1418f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// language/ABI-specific behavior in the catch-all sections.
1419d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCallvoid CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1420d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall                                         const Stmt *body,
1421d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall                                         llvm::Constant *beginCatchFn,
1422d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall                                         llvm::Constant *endCatchFn,
1423d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall                                         llvm::Constant *rethrowFn) {
1424d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  assert((beginCatchFn != 0) == (endCatchFn != 0) &&
1425f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall         "begin/end catch functions not paired");
1426d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  assert(rethrowFn && "rethrow function is required");
1427d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
1428d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  BeginCatchFn = beginCatchFn;
1429f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1430f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // The rethrow function has one of the following two types:
1431f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   void (*)()
1432f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  //   void (*)(void*)
1433f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // In the latter case we need to pass it the exception object.
1434f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // But we can't use the exception slot because the @finally might
1435f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // have a landing pad (which would overwrite the exception slot).
14362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *rethrowFnTy =
1437f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    cast<llvm::FunctionType>(
1438d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall      cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1439d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  SavedExnVar = 0;
1440d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  if (rethrowFnTy->getNumParams())
1441d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
1442f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1443f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // A finally block is a statement which must be executed on any edge
1444f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // out of a given scope.  Unlike a cleanup, the finally block may
1445f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // contain arbitrary control flow leading out of itself.  In
1446f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // addition, finally blocks should always be executed, even if there
1447f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // are no catch handlers higher on the stack.  Therefore, we
1448f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // surround the protected scope with a combination of a normal
1449f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // cleanup (to catch attempts to break out of the block via normal
1450f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // control flow) and an EH catch-all (semantically "outside" any try
1451f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // statement to which the finally block might have been attached).
1452f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // The finally block itself is generated in the context of a cleanup
1453f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // which conditionally leaves the catch-all.
1454f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1455f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Jump destination for performing the finally block on an exception
1456f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // edge.  We'll never actually reach this block, so unreachable is
1457f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // fine.
1458d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
1459f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1460f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Whether the finally block is being executed for EH purposes.
1461d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1462d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar);
1463f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1464f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Enter a normal cleanup which will perform the @finally block.
1465d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1466d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall                                          ForEHVar, endCatchFn,
1467d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall                                          rethrowFn, SavedExnVar);
1468f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1469f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Enter a catch-all scope.
1470d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1471d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1472d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  catchScope->setCatchAllHandler(0, catchBB);
1473d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall}
1474f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1475d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCallvoid CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
1476d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  // Leave the finally catch-all.
1477d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1478d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
1479777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall
1480777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  CGF.popCatchScope();
1481f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1482d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  // If there are any references to the catch-all block, emit it.
1483d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  if (catchBB->use_empty()) {
1484d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    delete catchBB;
1485d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  } else {
1486d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1487d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    CGF.EmitBlock(catchBB);
1488f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1489d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::Value *exn = 0;
1490f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1491d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    // If there's a begin-catch function, call it.
1492d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    if (BeginCatchFn) {
1493ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling      exn = CGF.getExceptionFromSlot();
1494bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn);
1495d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    }
1496f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1497d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    // If we need to remember the exception pointer to rethrow later, do so.
1498d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    if (SavedExnVar) {
1499ae270598d5c7a9a283d4b3ddce53b151c6e2b182Bill Wendling      if (!exn) exn = CGF.getExceptionFromSlot();
1500d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall      CGF.Builder.CreateStore(exn, SavedExnVar);
1501d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    }
1502f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1503d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    // Tell the cleanups in the finally block that we're do this for EH.
1504d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar);
1505f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1506d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    // Thread a jump through the finally cleanup.
1507d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    CGF.EmitBranchThroughCleanup(RethrowDest);
1508f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1509d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    CGF.Builder.restoreIP(savedIP);
1510d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  }
1511f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1512d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  // Finally, leave the @finally cleanup.
1513d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  CGF.PopCleanupBlock();
1514f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1515f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
151666b22771fc0a1dba598e50469f2961048e7edd55John McCall/// In a terminate landing pad, should we use __clang__call_terminate
151766b22771fc0a1dba598e50469f2961048e7edd55John McCall/// or just a naked call to std::terminate?
151866b22771fc0a1dba598e50469f2961048e7edd55John McCall///
151966b22771fc0a1dba598e50469f2961048e7edd55John McCall/// __clang_call_terminate calls __cxa_begin_catch, which then allows
152066b22771fc0a1dba598e50469f2961048e7edd55John McCall/// std::terminate to usefully report something about the
152166b22771fc0a1dba598e50469f2961048e7edd55John McCall/// violating exception.
152266b22771fc0a1dba598e50469f2961048e7edd55John McCallstatic bool useClangCallTerminate(CodeGenModule &CGM) {
152366b22771fc0a1dba598e50469f2961048e7edd55John McCall  // Only do this for Itanium-family ABIs in C++ mode.
152466b22771fc0a1dba598e50469f2961048e7edd55John McCall  return (CGM.getLangOpts().CPlusPlus &&
152566b22771fc0a1dba598e50469f2961048e7edd55John McCall          CGM.getTarget().getCXXABI().isItaniumFamily());
152666b22771fc0a1dba598e50469f2961048e7edd55John McCall}
152766b22771fc0a1dba598e50469f2961048e7edd55John McCall
152866b22771fc0a1dba598e50469f2961048e7edd55John McCall/// Get or define the following function:
152966b22771fc0a1dba598e50469f2961048e7edd55John McCall///   void @__clang_call_terminate(i8* %exn) nounwind noreturn
153066b22771fc0a1dba598e50469f2961048e7edd55John McCall/// This code is used only in C++.
153166b22771fc0a1dba598e50469f2961048e7edd55John McCallstatic llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
153266b22771fc0a1dba598e50469f2961048e7edd55John McCall  llvm::FunctionType *fnTy =
153366b22771fc0a1dba598e50469f2961048e7edd55John McCall    llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
153466b22771fc0a1dba598e50469f2961048e7edd55John McCall  llvm::Constant *fnRef =
153566b22771fc0a1dba598e50469f2961048e7edd55John McCall    CGM.CreateRuntimeFunction(fnTy, "__clang_call_terminate");
153666b22771fc0a1dba598e50469f2961048e7edd55John McCall
153766b22771fc0a1dba598e50469f2961048e7edd55John McCall  llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
153866b22771fc0a1dba598e50469f2961048e7edd55John McCall  if (fn && fn->empty()) {
153966b22771fc0a1dba598e50469f2961048e7edd55John McCall    fn->setDoesNotThrow();
154066b22771fc0a1dba598e50469f2961048e7edd55John McCall    fn->setDoesNotReturn();
154166b22771fc0a1dba598e50469f2961048e7edd55John McCall
154266b22771fc0a1dba598e50469f2961048e7edd55John McCall    // What we really want is to massively penalize inlining without
154366b22771fc0a1dba598e50469f2961048e7edd55John McCall    // forbidding it completely.  The difference between that and
154466b22771fc0a1dba598e50469f2961048e7edd55John McCall    // 'noinline' is negligible.
154566b22771fc0a1dba598e50469f2961048e7edd55John McCall    fn->addFnAttr(llvm::Attribute::NoInline);
154666b22771fc0a1dba598e50469f2961048e7edd55John McCall
154766b22771fc0a1dba598e50469f2961048e7edd55John McCall    // Allow this function to be shared across translation units, but
154866b22771fc0a1dba598e50469f2961048e7edd55John McCall    // we don't want it to turn into an exported symbol.
154966b22771fc0a1dba598e50469f2961048e7edd55John McCall    fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
155066b22771fc0a1dba598e50469f2961048e7edd55John McCall    fn->setVisibility(llvm::Function::HiddenVisibility);
155166b22771fc0a1dba598e50469f2961048e7edd55John McCall
155266b22771fc0a1dba598e50469f2961048e7edd55John McCall    // Set up the function.
155366b22771fc0a1dba598e50469f2961048e7edd55John McCall    llvm::BasicBlock *entry =
155466b22771fc0a1dba598e50469f2961048e7edd55John McCall      llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
155566b22771fc0a1dba598e50469f2961048e7edd55John McCall    CGBuilderTy builder(entry);
155666b22771fc0a1dba598e50469f2961048e7edd55John McCall
155766b22771fc0a1dba598e50469f2961048e7edd55John McCall    // Pull the exception pointer out of the parameter list.
155866b22771fc0a1dba598e50469f2961048e7edd55John McCall    llvm::Value *exn = &*fn->arg_begin();
155966b22771fc0a1dba598e50469f2961048e7edd55John McCall
156066b22771fc0a1dba598e50469f2961048e7edd55John McCall    // Call __cxa_begin_catch(exn).
1561bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
1562bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    catchCall->setDoesNotThrow();
1563bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    catchCall->setCallingConv(CGM.getRuntimeCC());
156466b22771fc0a1dba598e50469f2961048e7edd55John McCall
156566b22771fc0a1dba598e50469f2961048e7edd55John McCall    // Call std::terminate().
156666b22771fc0a1dba598e50469f2961048e7edd55John McCall    llvm::CallInst *termCall = builder.CreateCall(getTerminateFn(CGM));
156766b22771fc0a1dba598e50469f2961048e7edd55John McCall    termCall->setDoesNotThrow();
156866b22771fc0a1dba598e50469f2961048e7edd55John McCall    termCall->setDoesNotReturn();
1569bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    termCall->setCallingConv(CGM.getRuntimeCC());
157066b22771fc0a1dba598e50469f2961048e7edd55John McCall
157166b22771fc0a1dba598e50469f2961048e7edd55John McCall    // std::terminate cannot return.
157266b22771fc0a1dba598e50469f2961048e7edd55John McCall    builder.CreateUnreachable();
157366b22771fc0a1dba598e50469f2961048e7edd55John McCall  }
157466b22771fc0a1dba598e50469f2961048e7edd55John McCall
157566b22771fc0a1dba598e50469f2961048e7edd55John McCall  return fnRef;
157666b22771fc0a1dba598e50469f2961048e7edd55John McCall}
157766b22771fc0a1dba598e50469f2961048e7edd55John McCall
1578f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1579f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (TerminateLandingPad)
1580f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return TerminateLandingPad;
1581f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1582f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1583f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1584f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // This will get inserted at the end of the function.
1585f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  TerminateLandingPad = createBasicBlock("terminate.lpad");
1586f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Builder.SetInsertPoint(TerminateLandingPad);
1587f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1588f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Tell the backend that this is a landing pad.
15894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const EHPersonality &Personality = EHPersonality::get(CGM.getLangOpts());
1590285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  llvm::LandingPadInst *LPadInst =
1591285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling    Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
1592285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling                             getOpaquePersonalityFn(CGM, Personality), 0);
1593285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling  LPadInst->addClause(getCatchAllValue(*this));
1594f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
159566b22771fc0a1dba598e50469f2961048e7edd55John McCall  llvm::CallInst *terminateCall;
159666b22771fc0a1dba598e50469f2961048e7edd55John McCall  if (useClangCallTerminate(CGM)) {
159766b22771fc0a1dba598e50469f2961048e7edd55John McCall    // Extract out the exception pointer.
159866b22771fc0a1dba598e50469f2961048e7edd55John McCall    llvm::Value *exn = Builder.CreateExtractValue(LPadInst, 0);
1599bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    terminateCall = EmitNounwindRuntimeCall(getClangCallTerminateFn(CGM), exn);
160066b22771fc0a1dba598e50469f2961048e7edd55John McCall  } else {
1601bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    terminateCall = EmitNounwindRuntimeCall(getTerminateFn(CGM));
160266b22771fc0a1dba598e50469f2961048e7edd55John McCall  }
160366b22771fc0a1dba598e50469f2961048e7edd55John McCall  terminateCall->setDoesNotReturn();
1604d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  Builder.CreateUnreachable();
1605d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
1606f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Restore the saved insertion state.
1607f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Builder.restoreIP(SavedIP);
1608891f80ec2fb2d1730b769467d602689e1080845bJohn McCall
1609f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return TerminateLandingPad;
1610d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump}
16119b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump
16129b39c51ae3c547568ac42325f94b4197618f6b18Mike Stumpllvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
1613182f383db1782af752ecaf607fdff72a8542088bMike Stump  if (TerminateHandler)
1614182f383db1782af752ecaf607fdff72a8542088bMike Stump    return TerminateHandler;
1615182f383db1782af752ecaf607fdff72a8542088bMike Stump
1616f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
16179b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump
1618f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Set up the terminate handler.  This block is inserted at the very
1619f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // end of the function by FinishFunction.
1620182f383db1782af752ecaf607fdff72a8542088bMike Stump  TerminateHandler = createBasicBlock("terminate.handler");
1621f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Builder.SetInsertPoint(TerminateHandler);
162245ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall  llvm::CallInst *terminateCall;
162345ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall  if (useClangCallTerminate(CGM)) {
162445ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall    // Load the exception pointer.
162545ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall    llvm::Value *exn = getExceptionFromSlot();
162645ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall    terminateCall = EmitNounwindRuntimeCall(getClangCallTerminateFn(CGM), exn);
162745ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall  } else {
162845ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall    terminateCall = EmitNounwindRuntimeCall(getTerminateFn(CGM));
162945ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall  }
163045ff3807ef06bca715afb9eb7e16c4c89880e40eJohn McCall  terminateCall->setDoesNotReturn();
16319b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump  Builder.CreateUnreachable();
16329b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump
16333d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  // Restore the saved insertion state.
1634f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  Builder.restoreIP(SavedIP);
163576958099828bac6ebd45abef9f76934b3e99e397Mike Stump
16369b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump  return TerminateHandler;
16379b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump}
1638f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1639c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnallllvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) {
1640777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  if (EHResumeBlock) return EHResumeBlock;
1641ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1642ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1643ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1644ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // We emit a jump to a notional label at the outermost unwind state.
1645777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  EHResumeBlock = createBasicBlock("eh.resume");
1646777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  Builder.SetInsertPoint(EHResumeBlock);
1647ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
16484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const EHPersonality &Personality = EHPersonality::get(CGM.getLangOpts());
1649ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1650ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // This can always be a call because we necessarily didn't find
1651ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  // anything on the EH stack which needs our help.
1652af2771b147f1a5934c6c91574f1c2df986034e74Benjamin Kramer  const char *RethrowName = Personality.CatchallRethrowFn;
1653c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall  if (RethrowName != 0 && !isCleanup) {
1654bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName),
1655bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                      getExceptionFromSlot())
165693c332a8ba2c193c435b293966d343dab15f555bJohn McCall      ->setDoesNotReturn();
165793c332a8ba2c193c435b293966d343dab15f555bJohn McCall  } else {
165893c332a8ba2c193c435b293966d343dab15f555bJohn McCall    switch (CleanupHackLevel) {
165993c332a8ba2c193c435b293966d343dab15f555bJohn McCall    case CHL_MandatoryCatchall:
166093c332a8ba2c193c435b293966d343dab15f555bJohn McCall      // In mandatory-catchall mode, we need to use
166193c332a8ba2c193c435b293966d343dab15f555bJohn McCall      // _Unwind_Resume_or_Rethrow, or whatever the personality's
166293c332a8ba2c193c435b293966d343dab15f555bJohn McCall      // equivalent is.
1663bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      EmitRuntimeCall(getUnwindResumeOrRethrowFn(),
1664bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                        getExceptionFromSlot())
166593c332a8ba2c193c435b293966d343dab15f555bJohn McCall        ->setDoesNotReturn();
166693c332a8ba2c193c435b293966d343dab15f555bJohn McCall      break;
166793c332a8ba2c193c435b293966d343dab15f555bJohn McCall    case CHL_MandatoryCleanup: {
1668285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      // In mandatory-cleanup mode, we should use 'resume'.
1669285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling
1670285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      // Recreate the landingpad's return value for the 'resume' instruction.
1671285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      llvm::Value *Exn = getExceptionFromSlot();
1672285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      llvm::Value *Sel = getSelectorFromSlot();
1673285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling
1674285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      llvm::Type *LPadType = llvm::StructType::get(Exn->getType(),
1675285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling                                                   Sel->getType(), NULL);
1676285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1677285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1678285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1679285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling
1680285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      Builder.CreateResume(LPadVal);
1681285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      Builder.restoreIP(SavedIP);
1682285cfd8953d4ca4da613a47a0d691f7234068f8cBill Wendling      return EHResumeBlock;
168393c332a8ba2c193c435b293966d343dab15f555bJohn McCall    }
168493c332a8ba2c193c435b293966d343dab15f555bJohn McCall    case CHL_Ideal:
168593c332a8ba2c193c435b293966d343dab15f555bJohn McCall      // In an idealized mode where we don't have to worry about the
168693c332a8ba2c193c435b293966d343dab15f555bJohn McCall      // optimizer combining landing pads, we should just use
168793c332a8ba2c193c435b293966d343dab15f555bJohn McCall      // _Unwind_Resume (or the personality's equivalent).
1688bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      EmitRuntimeCall(getUnwindResumeFn(), getExceptionFromSlot())
168993c332a8ba2c193c435b293966d343dab15f555bJohn McCall        ->setDoesNotReturn();
169093c332a8ba2c193c435b293966d343dab15f555bJohn McCall      break;
169193c332a8ba2c193c435b293966d343dab15f555bJohn McCall    }
169293c332a8ba2c193c435b293966d343dab15f555bJohn McCall  }
1693ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1694ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  Builder.CreateUnreachable();
1695ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1696ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  Builder.restoreIP(SavedIP);
1697ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1698777d6e56ad9b1fed9866daf3ee6486d85c5b7d32John McCall  return EHResumeBlock;
1699ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall}
1700