blob: a2ff102e1ab4d79fc87fac8a394ada2ace5d8849 [file] [log] [blame]
Hans Wennborgdcfba332015-10-06 23:40:43 +00001//===--- CGException.cpp - Emit LLVM Code for C++ exceptions ----*- C++ -*-===//
Anders Carlsson4b08db72009-10-30 01:42:31 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ exception related code generation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
David Majnemer442d0a22014-11-25 07:20:20 +000015#include "CGCXXABI.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000016#include "CGCleanup.h"
Benjamin Kramer793bd552012-02-08 12:41:24 +000017#include "CGObjCRuntime.h"
John McCallde0fe072017-08-15 21:42:52 +000018#include "ConstantEmitter.h"
John McCall5add20c2010-07-20 22:17:55 +000019#include "TargetInfo.h"
Reid Kleckner1d59f992015-01-22 01:36:17 +000020#include "clang/AST/Mangle.h"
Benjamin Kramer793bd552012-02-08 12:41:24 +000021#include "clang/AST/StmtCXX.h"
Chandler Carruth4b417452013-01-19 08:09:44 +000022#include "clang/AST/StmtObjC.h"
Reid Kleckner31a1bb02015-04-08 22:23:48 +000023#include "clang/AST/StmtVisitor.h"
Reid Kleckner9fe7f232015-07-07 00:36:30 +000024#include "clang/Basic/TargetBuiltins.h"
Chandler Carruthc80ceea2014-03-04 11:02:08 +000025#include "llvm/IR/CallSite.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000026#include "llvm/IR/Intrinsics.h"
Reid Kleckner31a1bb02015-04-08 22:23:48 +000027#include "llvm/IR/IntrinsicInst.h"
Reid Klecknerebaf28d2015-04-14 20:59:00 +000028#include "llvm/Support/SaveAndRestore.h"
John McCallbd309292010-07-06 01:34:17 +000029
Anders Carlsson4b08db72009-10-30 01:42:31 +000030using namespace clang;
31using namespace CodeGen;
32
John McCall2c33ba82013-02-12 03:51:38 +000033static llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) {
Mike Stump33270212009-12-02 07:41:41 +000034 // void __cxa_free_exception(void *thrown_exception);
Mike Stump75546b82009-12-10 00:06:18 +000035
Chris Lattner2192fe52011-07-18 04:24:23 +000036 llvm::FunctionType *FTy =
John McCall2c33ba82013-02-12 03:51:38 +000037 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
Mike Stump75546b82009-12-10 00:06:18 +000038
John McCall2c33ba82013-02-12 03:51:38 +000039 return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
Mike Stump33270212009-12-02 07:41:41 +000040}
41
John McCall2c33ba82013-02-12 03:51:38 +000042static llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) {
Richard Smith2f7aa192013-06-20 23:03:35 +000043 // void __cxa_call_unexpected(void *thrown_exception);
Mike Stump1d849212009-12-07 23:38:24 +000044
Chris Lattner2192fe52011-07-18 04:24:23 +000045 llvm::FunctionType *FTy =
John McCall2c33ba82013-02-12 03:51:38 +000046 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
Mike Stump75546b82009-12-10 00:06:18 +000047
John McCall2c33ba82013-02-12 03:51:38 +000048 return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
Mike Stump1d849212009-12-07 23:38:24 +000049}
50
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000051llvm::Constant *CodeGenModule::getTerminateFn() {
Mike Stump33270212009-12-02 07:41:41 +000052 // void __terminate();
53
Chris Lattner2192fe52011-07-18 04:24:23 +000054 llvm::FunctionType *FTy =
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000055 llvm::FunctionType::get(VoidTy, /*IsVarArgs=*/false);
Mike Stump75546b82009-12-10 00:06:18 +000056
Chris Lattner0e62c1c2011-07-23 10:55:15 +000057 StringRef name;
John McCall9de19782011-07-06 01:22:26 +000058
59 // In C++, use std::terminate().
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000060 if (getLangOpts().CPlusPlus &&
61 getTarget().getCXXABI().isItaniumFamily()) {
Reid Kleckner1d59f992015-01-22 01:36:17 +000062 name = "_ZSt9terminatev";
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000063 } else if (getLangOpts().CPlusPlus &&
64 getTarget().getCXXABI().isMicrosoft()) {
David Majnemerb710a932015-05-11 03:57:49 +000065 if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015))
David Majnemerfb6ffca2015-05-10 21:38:26 +000066 name = "__std_terminate";
67 else
Reid Klecknerfb931542018-03-16 20:36:49 +000068 name = "?terminate@@YAXXZ";
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000069 } else if (getLangOpts().ObjC1 &&
70 getLangOpts().ObjCRuntime.hasTerminate())
John McCall9de19782011-07-06 01:22:26 +000071 name = "objc_terminate";
72 else
73 name = "abort";
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000074 return CreateRuntimeFunction(FTy, name);
David Chisnallf9c42252010-05-17 13:49:20 +000075}
76
John McCall2c33ba82013-02-12 03:51:38 +000077static llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000078 StringRef Name) {
Chris Lattner2192fe52011-07-18 04:24:23 +000079 llvm::FunctionType *FTy =
John McCall2c33ba82013-02-12 03:51:38 +000080 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
John McCall36ea3722010-07-17 00:43:08 +000081
John McCall2c33ba82013-02-12 03:51:38 +000082 return CGM.CreateRuntimeFunction(FTy, Name);
John McCallbd309292010-07-06 01:34:17 +000083}
84
Craig Topper8a13c412014-05-21 05:09:00 +000085const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr };
Benjamin Kramer793bd552012-02-08 12:41:24 +000086const EHPersonality
Craig Topper8a13c412014-05-21 05:09:00 +000087EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr };
88const EHPersonality
Reid Kleckner8f45c9c2014-09-15 17:19:16 +000089EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr };
90const EHPersonality
Craig Topper8a13c412014-05-21 05:09:00 +000091EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr };
92const EHPersonality
93EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr };
94const EHPersonality
95EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr };
Benjamin Kramer793bd552012-02-08 12:41:24 +000096const EHPersonality
Reid Kleckner8f45c9c2014-09-15 17:19:16 +000097EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr };
98const EHPersonality
Benjamin Kramer793bd552012-02-08 12:41:24 +000099EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"};
100const EHPersonality
Benjamin Kramer796c1d92017-01-08 22:58:07 +0000101EHPersonality::GNU_ObjC_SJLJ = {"__gnu_objc_personality_sj0", "objc_exception_throw"};
102const EHPersonality
103EHPersonality::GNU_ObjC_SEH = {"__gnu_objc_personality_seh0", "objc_exception_throw"};
104const EHPersonality
Craig Topper8a13c412014-05-21 05:09:00 +0000105EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr };
David Chisnall2ec1b10d2013-01-11 15:33:01 +0000106const EHPersonality
Craig Topper8a13c412014-05-21 05:09:00 +0000107EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr };
Reid Kleckner1d59f992015-01-22 01:36:17 +0000108const EHPersonality
109EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr };
110const EHPersonality
111EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr };
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000112const EHPersonality
113EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr };
Heejin Ahnc6479192018-05-31 22:18:13 +0000114const EHPersonality
115EHPersonality::GNU_Wasm_CPlusPlus = { "__gxx_wasm_personality_v0", nullptr };
John McCall36ea3722010-07-17 00:43:08 +0000116
Heejin Ahn00831792018-06-04 18:23:00 +0000117static const EHPersonality &getCPersonality(const TargetInfo &Target,
Reid Klecknere070b992014-11-14 02:01:10 +0000118 const LangOptions &L) {
Heejin Ahn00831792018-06-04 18:23:00 +0000119 const llvm::Triple &T = Target.getTriple();
Shoaib Meenaia5fc6032018-06-08 00:41:01 +0000120 if (T.isWindowsMSVCEnvironment())
121 return EHPersonality::MSVC_CxxFrameHandler3;
John McCall2faab302010-11-07 02:35:25 +0000122 if (L.SjLjExceptions)
123 return EHPersonality::GNU_C_SJLJ;
Saleem Abdulrasool3e701322018-03-09 07:06:42 +0000124 if (L.DWARFExceptions)
125 return EHPersonality::GNU_C;
Martell Malonec950c652017-11-29 07:25:12 +0000126 if (L.SEHExceptions)
Reid Kleckner8f45c9c2014-09-15 17:19:16 +0000127 return EHPersonality::GNU_C_SEH;
John McCall36ea3722010-07-17 00:43:08 +0000128 return EHPersonality::GNU_C;
129}
130
Heejin Ahn00831792018-06-04 18:23:00 +0000131static const EHPersonality &getObjCPersonality(const TargetInfo &Target,
Reid Klecknere070b992014-11-14 02:01:10 +0000132 const LangOptions &L) {
Heejin Ahn00831792018-06-04 18:23:00 +0000133 const llvm::Triple &T = Target.getTriple();
Shoaib Meenaia5fc6032018-06-08 00:41:01 +0000134 if (T.isWindowsMSVCEnvironment())
135 return EHPersonality::MSVC_CxxFrameHandler3;
136
John McCall5fb5df92012-06-20 06:18:46 +0000137 switch (L.ObjCRuntime.getKind()) {
138 case ObjCRuntime::FragileMacOSX:
Heejin Ahn00831792018-06-04 18:23:00 +0000139 return getCPersonality(Target, L);
John McCall5fb5df92012-06-20 06:18:46 +0000140 case ObjCRuntime::MacOSX:
141 case ObjCRuntime::iOS:
Tim Northover756447a2015-10-30 16:30:36 +0000142 case ObjCRuntime::WatchOS:
John McCall5fb5df92012-06-20 06:18:46 +0000143 return EHPersonality::NeXT_ObjC;
David Chisnallb601c962012-07-03 20:49:52 +0000144 case ObjCRuntime::GNUstep:
David Chisnall2ec1b10d2013-01-11 15:33:01 +0000145 if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
146 return EHPersonality::GNUstep_ObjC;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000147 LLVM_FALLTHROUGH;
David Chisnallb601c962012-07-03 20:49:52 +0000148 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +0000149 case ObjCRuntime::ObjFW:
Benjamin Kramer796c1d92017-01-08 22:58:07 +0000150 if (L.SjLjExceptions)
151 return EHPersonality::GNU_ObjC_SJLJ;
Martell Malonec950c652017-11-29 07:25:12 +0000152 if (L.SEHExceptions)
Benjamin Kramer796c1d92017-01-08 22:58:07 +0000153 return EHPersonality::GNU_ObjC_SEH;
John McCall36ea3722010-07-17 00:43:08 +0000154 return EHPersonality::GNU_ObjC;
John McCallbd309292010-07-06 01:34:17 +0000155 }
John McCall5fb5df92012-06-20 06:18:46 +0000156 llvm_unreachable("bad runtime kind");
John McCallbd309292010-07-06 01:34:17 +0000157}
158
Heejin Ahn00831792018-06-04 18:23:00 +0000159static const EHPersonality &getCXXPersonality(const TargetInfo &Target,
160 const LangOptions &L) {
161 const llvm::Triple &T = Target.getTriple();
Shoaib Meenaia5fc6032018-06-08 00:41:01 +0000162 if (T.isWindowsMSVCEnvironment())
163 return EHPersonality::MSVC_CxxFrameHandler3;
John McCall36ea3722010-07-17 00:43:08 +0000164 if (L.SjLjExceptions)
165 return EHPersonality::GNU_CPlusPlus_SJLJ;
Saleem Abdulrasool3e701322018-03-09 07:06:42 +0000166 if (L.DWARFExceptions)
167 return EHPersonality::GNU_CPlusPlus;
Martell Malonec950c652017-11-29 07:25:12 +0000168 if (L.SEHExceptions)
Reid Kleckner8f45c9c2014-09-15 17:19:16 +0000169 return EHPersonality::GNU_CPlusPlus_SEH;
Heejin Ahn1eb074d2018-06-01 01:01:37 +0000170 // Wasm EH is a non-MVP feature for now.
171 if (Target.hasFeature("exception-handling") &&
172 (T.getArch() == llvm::Triple::wasm32 ||
173 T.getArch() == llvm::Triple::wasm64))
Heejin Ahnc6479192018-05-31 22:18:13 +0000174 return EHPersonality::GNU_Wasm_CPlusPlus;
Reid Klecknere070b992014-11-14 02:01:10 +0000175 return EHPersonality::GNU_CPlusPlus;
John McCallbd309292010-07-06 01:34:17 +0000176}
177
178/// Determines the personality function to use when both C++
179/// and Objective-C exceptions are being caught.
Heejin Ahn00831792018-06-04 18:23:00 +0000180static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target,
181 const LangOptions &L) {
Shoaib Meenaia5fc6032018-06-08 00:41:01 +0000182 if (Target.getTriple().isWindowsMSVCEnvironment())
183 return EHPersonality::MSVC_CxxFrameHandler3;
184
John McCall5fb5df92012-06-20 06:18:46 +0000185 switch (L.ObjCRuntime.getKind()) {
Saleem Abdulrasool8f6d9442017-11-02 00:25:40 +0000186 // In the fragile ABI, just use C++ exception handling and hope
187 // they're not doing crazy exception mixing.
188 case ObjCRuntime::FragileMacOSX:
Heejin Ahn00831792018-06-04 18:23:00 +0000189 return getCXXPersonality(Target, L);
Saleem Abdulrasool8f6d9442017-11-02 00:25:40 +0000190
John McCallbd309292010-07-06 01:34:17 +0000191 // The ObjC personality defers to the C++ personality for non-ObjC
192 // handlers. Unlike the C++ case, we use the same personality
193 // function on targets using (backend-driven) SJLJ EH.
John McCall5fb5df92012-06-20 06:18:46 +0000194 case ObjCRuntime::MacOSX:
195 case ObjCRuntime::iOS:
Tim Northover756447a2015-10-30 16:30:36 +0000196 case ObjCRuntime::WatchOS:
Heejin Ahn00831792018-06-04 18:23:00 +0000197 return getObjCPersonality(Target, L);
John McCallbd309292010-07-06 01:34:17 +0000198
Saleem Abdulrasool8f6d9442017-11-02 00:25:40 +0000199 case ObjCRuntime::GNUstep:
200 return EHPersonality::GNU_ObjCXX;
David Chisnallf9c42252010-05-17 13:49:20 +0000201
David Chisnallb601c962012-07-03 20:49:52 +0000202 // The GCC runtime's personality function inherently doesn't support
Saleem Abdulrasool8f6d9442017-11-02 00:25:40 +0000203 // mixed EH. Use the ObjC personality just to avoid returning null.
David Chisnallb601c962012-07-03 20:49:52 +0000204 case ObjCRuntime::GCC:
Benjamin Kramer9851cb72017-04-01 17:59:01 +0000205 case ObjCRuntime::ObjFW:
Heejin Ahn00831792018-06-04 18:23:00 +0000206 return getObjCPersonality(Target, L);
John McCall5fb5df92012-06-20 06:18:46 +0000207 }
208 llvm_unreachable("bad runtime kind");
John McCallbd309292010-07-06 01:34:17 +0000209}
210
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000211static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) {
Reid Kleckner1d59f992015-01-22 01:36:17 +0000212 if (T.getArch() == llvm::Triple::x86)
213 return EHPersonality::MSVC_except_handler;
214 return EHPersonality::MSVC_C_specific_handler;
215}
216
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000217const EHPersonality &EHPersonality::get(CodeGenModule &CGM,
218 const FunctionDecl *FD) {
Reid Klecknere070b992014-11-14 02:01:10 +0000219 const llvm::Triple &T = CGM.getTarget().getTriple();
220 const LangOptions &L = CGM.getLangOpts();
Heejin Ahn1eb074d2018-06-01 01:01:37 +0000221 const TargetInfo &Target = CGM.getTarget();
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000222
Reid Kleckner01485652015-09-17 17:04:13 +0000223 // Functions using SEH get an SEH personality.
224 if (FD && FD->usesSEHTry())
225 return getSEHPersonalityMSVC(T);
226
Saleem Abdulrasool3e701322018-03-09 07:06:42 +0000227 if (L.ObjC1)
Heejin Ahn00831792018-06-04 18:23:00 +0000228 return L.CPlusPlus ? getObjCXXPersonality(Target, L)
229 : getObjCPersonality(Target, L);
230 return L.CPlusPlus ? getCXXPersonality(Target, L)
231 : getCPersonality(Target, L);
John McCall36ea3722010-07-17 00:43:08 +0000232}
John McCallbd309292010-07-06 01:34:17 +0000233
David Majnemerc28d46e2015-07-22 23:46:21 +0000234const EHPersonality &EHPersonality::get(CodeGenFunction &CGF) {
Reid Kleckner65fa8692017-10-13 16:55:14 +0000235 const auto *FD = CGF.CurCodeDecl;
236 // For outlined finallys and filters, use the SEH personality in case they
237 // contain more SEH. This mostly only affects finallys. Filters could
238 // hypothetically use gnu statement expressions to sneak in nested SEH.
239 FD = FD ? FD : CGF.CurSEHParent;
240 return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(FD));
David Majnemerc28d46e2015-07-22 23:46:21 +0000241}
242
John McCall0bdb1fd2010-09-16 06:16:50 +0000243static llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
John McCall36ea3722010-07-17 00:43:08 +0000244 const EHPersonality &Personality) {
Saleem Abdulrasool6cb07442016-12-15 06:59:05 +0000245 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
246 Personality.PersonalityFn,
Reid Klecknerde864822017-03-21 16:57:30 +0000247 llvm::AttributeList(), /*Local=*/true);
John McCall0bdb1fd2010-09-16 06:16:50 +0000248}
249
250static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
251 const EHPersonality &Personality) {
252 llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
John McCallad7c5c12011-02-08 08:22:06 +0000253 return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
John McCall0bdb1fd2010-09-16 06:16:50 +0000254}
255
Vedant Kumardb609472015-09-11 15:40:05 +0000256/// Check whether a landingpad instruction only uses C++ features.
257static bool LandingPadHasOnlyCXXUses(llvm::LandingPadInst *LPI) {
258 for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
259 // Look for something that would've been returned by the ObjC
260 // runtime's GetEHType() method.
261 llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
262 if (LPI->isCatch(I)) {
263 // Check if the catch value has the ObjC prefix.
264 if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
265 // ObjC EH selector entries are always global variables with
266 // names starting like this.
267 if (GV->getName().startswith("OBJC_EHTYPE"))
268 return false;
269 } else {
270 // Check if any of the filter values have the ObjC prefix.
271 llvm::Constant *CVal = cast<llvm::Constant>(Val);
272 for (llvm::User::op_iterator
273 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
274 if (llvm::GlobalVariable *GV =
275 cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
276 // ObjC EH selector entries are always global variables with
277 // names starting like this.
278 if (GV->getName().startswith("OBJC_EHTYPE"))
279 return false;
280 }
281 }
282 }
283 return true;
284}
285
John McCall0bdb1fd2010-09-16 06:16:50 +0000286/// Check whether a personality function could reasonably be swapped
287/// for a C++ personality function.
288static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
Chandler Carruth4d01fff2014-03-09 03:16:50 +0000289 for (llvm::User *U : Fn->users()) {
John McCall0bdb1fd2010-09-16 06:16:50 +0000290 // Conditionally white-list bitcasts.
Chandler Carruth4d01fff2014-03-09 03:16:50 +0000291 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) {
John McCall0bdb1fd2010-09-16 06:16:50 +0000292 if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
293 if (!PersonalityHasOnlyCXXUses(CE))
294 return false;
295 continue;
296 }
297
Vedant Kumardb609472015-09-11 15:40:05 +0000298 // Otherwise it must be a function.
299 llvm::Function *F = dyn_cast<llvm::Function>(U);
300 if (!F) return false;
John McCall0bdb1fd2010-09-16 06:16:50 +0000301
Vedant Kumardb609472015-09-11 15:40:05 +0000302 for (auto BB = F->begin(), E = F->end(); BB != E; ++BB) {
303 if (BB->isLandingPad())
304 if (!LandingPadHasOnlyCXXUses(BB->getLandingPadInst()))
305 return false;
John McCall0bdb1fd2010-09-16 06:16:50 +0000306 }
307 }
308
309 return true;
310}
311
312/// Try to use the C++ personality function in ObjC++. Not doing this
313/// can cause some incompatibilities with gcc, which is more
314/// aggressive about only using the ObjC++ personality in a function
315/// when it really needs it.
316void CodeGenModule::SimplifyPersonality() {
John McCall0bdb1fd2010-09-16 06:16:50 +0000317 // If we're not in ObjC++ -fexceptions, there's nothing to do.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000318 if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions)
John McCall0bdb1fd2010-09-16 06:16:50 +0000319 return;
320
John McCall3c223932012-11-14 17:48:31 +0000321 // Both the problem this endeavors to fix and the way the logic
322 // above works is specific to the NeXT runtime.
323 if (!LangOpts.ObjCRuntime.isNeXTFamily())
324 return;
325
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000326 const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr);
Heejin Ahn00831792018-06-04 18:23:00 +0000327 const EHPersonality &CXX = getCXXPersonality(getTarget(), LangOpts);
Benjamin Kramer793bd552012-02-08 12:41:24 +0000328 if (&ObjCXX == &CXX)
John McCall0bdb1fd2010-09-16 06:16:50 +0000329 return;
330
Benjamin Kramer793bd552012-02-08 12:41:24 +0000331 assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 &&
332 "Different EHPersonalities using the same personality function.");
333
334 llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn);
John McCall0bdb1fd2010-09-16 06:16:50 +0000335
336 // Nothing to do if it's unused.
337 if (!Fn || Fn->use_empty()) return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000338
John McCall0bdb1fd2010-09-16 06:16:50 +0000339 // Can't do the optimization if it has non-C++ uses.
340 if (!PersonalityHasOnlyCXXUses(Fn)) return;
341
342 // Create the C++ personality function and kill off the old
343 // function.
344 llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
345
346 // This can happen if the user is screwing with us.
347 if (Fn->getType() != CXXFn->getType()) return;
348
349 Fn->replaceAllUsesWith(CXXFn);
350 Fn->eraseFromParent();
John McCallbd309292010-07-06 01:34:17 +0000351}
352
353/// Returns the value to inject into a selector to indicate the
354/// presence of a catch-all.
355static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
356 // Possibly we should use @llvm.eh.catch.all.value here.
John McCallad7c5c12011-02-08 08:22:06 +0000357 return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
John McCallbd309292010-07-06 01:34:17 +0000358}
359
John McCallbb026012010-07-13 21:17:51 +0000360namespace {
361 /// A cleanup to free the exception object if its initialization
362 /// throws.
David Blaikie7e70d682015-08-18 22:40:54 +0000363 struct FreeException final : EHScopeStack::Cleanup {
John McCall5fcf8da2011-07-12 00:15:30 +0000364 llvm::Value *exn;
365 FreeException(llvm::Value *exn) : exn(exn) {}
Craig Topper4f12f102014-03-12 06:41:41 +0000366 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall882987f2013-02-28 19:01:20 +0000367 CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn);
John McCallbb026012010-07-13 21:17:51 +0000368 }
369 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000370} // end anonymous namespace
John McCallbb026012010-07-13 21:17:51 +0000371
John McCall2e6567a2010-04-22 01:10:34 +0000372// Emits an exception expression into the given location. This
373// differs from EmitAnyExprToMem only in that, if a final copy-ctor
374// call is required, an exception within that copy ctor causes
375// std::terminate to be invoked.
John McCall7f416cc2015-09-08 08:05:57 +0000376void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) {
John McCallbd309292010-07-06 01:34:17 +0000377 // Make sure the exception object is cleaned up if there's an
378 // exception during initialization.
John McCall7f416cc2015-09-08 08:05:57 +0000379 pushFullExprCleanup<FreeException>(EHCleanup, addr.getPointer());
David Majnemer7c237072015-03-05 00:46:22 +0000380 EHScopeStack::stable_iterator cleanup = EHStack.stable_begin();
John McCall2e6567a2010-04-22 01:10:34 +0000381
382 // __cxa_allocate_exception returns a void*; we need to cast this
383 // to the appropriate type for the object.
David Majnemer7c237072015-03-05 00:46:22 +0000384 llvm::Type *ty = ConvertTypeForMem(e->getType())->getPointerTo();
John McCall7f416cc2015-09-08 08:05:57 +0000385 Address typedAddr = Builder.CreateBitCast(addr, ty);
John McCall2e6567a2010-04-22 01:10:34 +0000386
387 // FIXME: this isn't quite right! If there's a final unelided call
388 // to a copy constructor, then according to [except.terminate]p1 we
389 // must call std::terminate() if that constructor throws, because
390 // technically that copy occurs after the exception expression is
391 // evaluated but before the exception is caught. But the best way
392 // to handle that is to teach EmitAggExpr to do the final copy
393 // differently if it can't be elided.
David Majnemer7c237072015-03-05 00:46:22 +0000394 EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
395 /*IsInit*/ true);
John McCall2e6567a2010-04-22 01:10:34 +0000396
John McCalle4df6c82011-01-28 08:37:24 +0000397 // Deactivate the cleanup block.
John McCall7f416cc2015-09-08 08:05:57 +0000398 DeactivateCleanupBlock(cleanup,
399 cast<llvm::Instruction>(typedAddr.getPointer()));
Mike Stump54066142009-12-01 03:41:18 +0000400}
401
John McCall7f416cc2015-09-08 08:05:57 +0000402Address CodeGenFunction::getExceptionSlot() {
John McCall9b382dd2011-05-28 21:13:02 +0000403 if (!ExceptionSlot)
404 ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
John McCall7f416cc2015-09-08 08:05:57 +0000405 return Address(ExceptionSlot, getPointerAlign());
Mike Stump54066142009-12-01 03:41:18 +0000406}
407
John McCall7f416cc2015-09-08 08:05:57 +0000408Address CodeGenFunction::getEHSelectorSlot() {
John McCall9b382dd2011-05-28 21:13:02 +0000409 if (!EHSelectorSlot)
410 EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
John McCall7f416cc2015-09-08 08:05:57 +0000411 return Address(EHSelectorSlot, CharUnits::fromQuantity(4));
John McCall9b382dd2011-05-28 21:13:02 +0000412}
413
Bill Wendling79a70e42011-09-15 18:57:19 +0000414llvm::Value *CodeGenFunction::getExceptionFromSlot() {
415 return Builder.CreateLoad(getExceptionSlot(), "exn");
416}
417
418llvm::Value *CodeGenFunction::getSelectorFromSlot() {
419 return Builder.CreateLoad(getEHSelectorSlot(), "sel");
420}
421
Richard Smithea852322013-05-07 21:53:22 +0000422void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E,
423 bool KeepInsertionPoint) {
David Majnemer7c237072015-03-05 00:46:22 +0000424 if (const Expr *SubExpr = E->getSubExpr()) {
425 QualType ThrowType = SubExpr->getType();
426 if (ThrowType->isObjCObjectPointerType()) {
427 const Stmt *ThrowStmt = E->getSubExpr();
428 const ObjCAtThrowStmt S(E->getExprLoc(), const_cast<Stmt *>(ThrowStmt));
429 CGM.getObjCRuntime().EmitThrowStmt(*this, S, false);
430 } else {
431 CGM.getCXXABI().emitThrow(*this, E);
John McCall2e6567a2010-04-22 01:10:34 +0000432 }
David Majnemer7c237072015-03-05 00:46:22 +0000433 } else {
434 CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true);
John McCall2e6567a2010-04-22 01:10:34 +0000435 }
Mike Stump75546b82009-12-10 00:06:18 +0000436
John McCall20f6ab82011-01-12 03:41:02 +0000437 // throw is an expression, and the expression emitters expect us
438 // to leave ourselves at a valid insertion point.
Richard Smithea852322013-05-07 21:53:22 +0000439 if (KeepInsertionPoint)
440 EmitBlock(createBasicBlock("throw.cont"));
Anders Carlsson4b08db72009-10-30 01:42:31 +0000441}
Mike Stump58ef18b2009-11-20 23:44:51 +0000442
Mike Stump1d849212009-12-07 23:38:24 +0000443void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000444 if (!CGM.getLangOpts().CXXExceptions)
Anders Carlsson9878f9f2010-02-06 23:59:05 +0000445 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000446
Mike Stump1d849212009-12-07 23:38:24 +0000447 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
Craig Topper8a13c412014-05-21 05:09:00 +0000448 if (!FD) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000449 // Check if CapturedDecl is nothrow and create terminate scope for it.
450 if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
451 if (CD->isNothrow())
452 EHStack.pushTerminate();
453 }
Mike Stump1d849212009-12-07 23:38:24 +0000454 return;
Alexey Bataev9959db52014-05-06 10:08:46 +0000455 }
Mike Stump1d849212009-12-07 23:38:24 +0000456 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
Craig Topper8a13c412014-05-21 05:09:00 +0000457 if (!Proto)
Mike Stump1d849212009-12-07 23:38:24 +0000458 return;
459
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000460 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
Richard Smitheaf11ad2018-05-03 03:58:32 +0000461 if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) {
462 // noexcept functions are simple terminate scopes.
463 EHStack.pushTerminate();
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000464 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
David Majnemer1f192e22015-04-01 04:45:52 +0000465 // TODO: Revisit exception specifications for the MS ABI. There is a way to
466 // encode these in an object file but MSVC doesn't do anything with it.
467 if (getTarget().getCXXABI().isMicrosoft())
468 return;
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000469 unsigned NumExceptions = Proto->getNumExceptions();
470 EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
Mike Stump1d849212009-12-07 23:38:24 +0000471
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000472 for (unsigned I = 0; I != NumExceptions; ++I) {
473 QualType Ty = Proto->getExceptionType(I);
474 QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
475 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
476 /*ForEH=*/true);
477 Filter->setFilter(I, EHType);
478 }
Mike Stump1d849212009-12-07 23:38:24 +0000479 }
Mike Stump1d849212009-12-07 23:38:24 +0000480}
481
John McCall8e4c74b2011-08-11 02:22:43 +0000482/// Emit the dispatch block for a filter scope if necessary.
483static void emitFilterDispatchBlock(CodeGenFunction &CGF,
484 EHFilterScope &filterScope) {
485 llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
486 if (!dispatchBlock) return;
487 if (dispatchBlock->use_empty()) {
488 delete dispatchBlock;
489 return;
490 }
491
John McCall8e4c74b2011-08-11 02:22:43 +0000492 CGF.EmitBlockAfterUses(dispatchBlock);
493
494 // If this isn't a catch-all filter, we need to check whether we got
495 // here because the filter triggered.
496 if (filterScope.getNumFilters()) {
497 // Load the selector value.
Bill Wendling79a70e42011-09-15 18:57:19 +0000498 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall8e4c74b2011-08-11 02:22:43 +0000499 llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
500
501 llvm::Value *zero = CGF.Builder.getInt32(0);
502 llvm::Value *failsFilter =
Nico Weber1bebad12015-02-11 22:33:32 +0000503 CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
504 CGF.Builder.CreateCondBr(failsFilter, unexpectedBB,
505 CGF.getEHResumeBlock(false));
John McCall8e4c74b2011-08-11 02:22:43 +0000506
507 CGF.EmitBlock(unexpectedBB);
508 }
509
510 // Call __cxa_call_unexpected. This doesn't need to be an invoke
511 // because __cxa_call_unexpected magically filters exceptions
512 // according to the last landing pad the exception was thrown
513 // into. Seriously.
Bill Wendling79a70e42011-09-15 18:57:19 +0000514 llvm::Value *exn = CGF.getExceptionFromSlot();
John McCall882987f2013-02-28 19:01:20 +0000515 CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn)
John McCall8e4c74b2011-08-11 02:22:43 +0000516 ->setDoesNotReturn();
517 CGF.Builder.CreateUnreachable();
518}
519
Mike Stump1d849212009-12-07 23:38:24 +0000520void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000521 if (!CGM.getLangOpts().CXXExceptions)
Anders Carlsson9878f9f2010-02-06 23:59:05 +0000522 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000523
Mike Stump1d849212009-12-07 23:38:24 +0000524 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
Craig Topper8a13c412014-05-21 05:09:00 +0000525 if (!FD) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000526 // Check if CapturedDecl is nothrow and pop terminate scope for it.
527 if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
528 if (CD->isNothrow())
529 EHStack.popTerminate();
530 }
Mike Stump1d849212009-12-07 23:38:24 +0000531 return;
Alexey Bataev9959db52014-05-06 10:08:46 +0000532 }
Mike Stump1d849212009-12-07 23:38:24 +0000533 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
Craig Topper8a13c412014-05-21 05:09:00 +0000534 if (!Proto)
Mike Stump1d849212009-12-07 23:38:24 +0000535 return;
536
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000537 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
Richard Smitheaf11ad2018-05-03 03:58:32 +0000538 if (isNoexceptExceptionSpec(EST) && Proto->canThrow() == CT_Cannot) {
539 EHStack.popTerminate();
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000540 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
David Majnemer1f192e22015-04-01 04:45:52 +0000541 // TODO: Revisit exception specifications for the MS ABI. There is a way to
542 // encode these in an object file but MSVC doesn't do anything with it.
543 if (getTarget().getCXXABI().isMicrosoft())
544 return;
John McCall8e4c74b2011-08-11 02:22:43 +0000545 EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
546 emitFilterDispatchBlock(*this, filterScope);
Sebastian Redl0b94c9f2011-03-15 18:42:48 +0000547 EHStack.popFilter();
548 }
Mike Stump1d849212009-12-07 23:38:24 +0000549}
550
Mike Stump58ef18b2009-11-20 23:44:51 +0000551void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCallb609d3f2010-07-07 06:56:46 +0000552 EnterCXXTryStmt(S);
John McCallb81884d2010-02-19 09:25:03 +0000553 EmitStmt(S.getTryBlock());
John McCallb609d3f2010-07-07 06:56:46 +0000554 ExitCXXTryStmt(S);
John McCallb81884d2010-02-19 09:25:03 +0000555}
556
John McCallb609d3f2010-07-07 06:56:46 +0000557void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallbd309292010-07-06 01:34:17 +0000558 unsigned NumHandlers = S.getNumHandlers();
559 EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
John McCallb81884d2010-02-19 09:25:03 +0000560
John McCallbd309292010-07-06 01:34:17 +0000561 for (unsigned I = 0; I != NumHandlers; ++I) {
562 const CXXCatchStmt *C = S.getHandler(I);
John McCallb81884d2010-02-19 09:25:03 +0000563
John McCallbd309292010-07-06 01:34:17 +0000564 llvm::BasicBlock *Handler = createBasicBlock("catch");
565 if (C->getExceptionDecl()) {
566 // FIXME: Dropping the reference type on the type into makes it
567 // impossible to correctly implement catch-by-reference
568 // semantics for pointers. Unfortunately, this is what all
569 // existing compilers do, and it's not clear that the standard
570 // personality routine is capable of doing this right. See C++ DR 388:
571 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
David Majnemer571162a2014-10-12 06:58:22 +0000572 Qualifiers CaughtTypeQuals;
573 QualType CaughtType = CGM.getContext().getUnqualifiedArrayType(
574 C->getCaughtType().getNonReferenceType(), CaughtTypeQuals);
John McCall2ca705e2010-07-24 00:37:23 +0000575
Reid Kleckner10aa7702015-09-16 20:15:55 +0000576 CatchTypeInfo TypeInfo{nullptr, 0};
John McCall2ca705e2010-07-24 00:37:23 +0000577 if (CaughtType->isObjCObjectPointerType())
Reid Kleckner10aa7702015-09-16 20:15:55 +0000578 TypeInfo.RTTI = CGM.getObjCRuntime().GetEHType(CaughtType);
John McCall2ca705e2010-07-24 00:37:23 +0000579 else
Reid Kleckner10aa7702015-09-16 20:15:55 +0000580 TypeInfo = CGM.getCXXABI().getAddrOfCXXCatchHandlerType(
581 CaughtType, C->getCaughtType());
John McCallbd309292010-07-06 01:34:17 +0000582 CatchScope->setHandler(I, TypeInfo, Handler);
583 } else {
584 // No exception decl indicates '...', a catch-all.
Reid Kleckner10aa7702015-09-16 20:15:55 +0000585 CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler);
John McCallbd309292010-07-06 01:34:17 +0000586 }
587 }
John McCallbd309292010-07-06 01:34:17 +0000588}
589
John McCall8e4c74b2011-08-11 02:22:43 +0000590llvm::BasicBlock *
591CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
Reid Kleckner129552b2015-10-08 01:13:52 +0000592 if (EHPersonality::get(*this).usesFuncletPads())
Heejin Ahnc6479192018-05-31 22:18:13 +0000593 return getFuncletEHDispatchBlock(si);
David Majnemerdbf10452015-07-31 17:58:45 +0000594
John McCall8e4c74b2011-08-11 02:22:43 +0000595 // The dispatch block for the end of the scope chain is a block that
596 // just resumes unwinding.
597 if (si == EHStack.stable_end())
David Chisnall9a837be2012-11-07 16:50:40 +0000598 return getEHResumeBlock(true);
John McCall8e4c74b2011-08-11 02:22:43 +0000599
600 // Otherwise, we should look at the actual scope.
601 EHScope &scope = *EHStack.find(si);
602
603 llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
604 if (!dispatchBlock) {
605 switch (scope.getKind()) {
606 case EHScope::Catch: {
607 // Apply a special case to a single catch-all.
608 EHCatchScope &catchScope = cast<EHCatchScope>(scope);
609 if (catchScope.getNumHandlers() == 1 &&
610 catchScope.getHandler(0).isCatchAll()) {
611 dispatchBlock = catchScope.getHandler(0).Block;
612
613 // Otherwise, make a dispatch block.
614 } else {
615 dispatchBlock = createBasicBlock("catch.dispatch");
616 }
617 break;
618 }
619
620 case EHScope::Cleanup:
621 dispatchBlock = createBasicBlock("ehcleanup");
622 break;
623
624 case EHScope::Filter:
625 dispatchBlock = createBasicBlock("filter.dispatch");
626 break;
627
628 case EHScope::Terminate:
629 dispatchBlock = getTerminateHandler();
630 break;
David Majnemerdbf10452015-07-31 17:58:45 +0000631
Reid Kleckner2586aac2015-09-10 22:11:13 +0000632 case EHScope::PadEnd:
633 llvm_unreachable("PadEnd unnecessary for Itanium!");
John McCall8e4c74b2011-08-11 02:22:43 +0000634 }
635 scope.setCachedEHDispatchBlock(dispatchBlock);
636 }
637 return dispatchBlock;
638}
639
David Majnemerdbf10452015-07-31 17:58:45 +0000640llvm::BasicBlock *
Heejin Ahnc6479192018-05-31 22:18:13 +0000641CodeGenFunction::getFuncletEHDispatchBlock(EHScopeStack::stable_iterator SI) {
David Majnemerdbf10452015-07-31 17:58:45 +0000642 // Returning nullptr indicates that the previous dispatch block should unwind
643 // to caller.
644 if (SI == EHStack.stable_end())
645 return nullptr;
646
647 // Otherwise, we should look at the actual scope.
648 EHScope &EHS = *EHStack.find(SI);
649
650 llvm::BasicBlock *DispatchBlock = EHS.getCachedEHDispatchBlock();
651 if (DispatchBlock)
652 return DispatchBlock;
653
654 if (EHS.getKind() == EHScope::Terminate)
Reid Kleckner06f19a02018-01-02 21:34:16 +0000655 DispatchBlock = getTerminateFunclet();
David Majnemerdbf10452015-07-31 17:58:45 +0000656 else
657 DispatchBlock = createBasicBlock();
John McCall7f416cc2015-09-08 08:05:57 +0000658 CGBuilderTy Builder(*this, DispatchBlock);
David Majnemerdbf10452015-07-31 17:58:45 +0000659
660 switch (EHS.getKind()) {
661 case EHScope::Catch:
662 DispatchBlock->setName("catch.dispatch");
663 break;
664
665 case EHScope::Cleanup:
666 DispatchBlock->setName("ehcleanup");
667 break;
668
669 case EHScope::Filter:
670 llvm_unreachable("exception specifications not handled yet!");
671
672 case EHScope::Terminate:
673 DispatchBlock->setName("terminate");
674 break;
675
Reid Kleckner2586aac2015-09-10 22:11:13 +0000676 case EHScope::PadEnd:
677 llvm_unreachable("PadEnd dispatch block missing!");
David Majnemerdbf10452015-07-31 17:58:45 +0000678 }
679 EHS.setCachedEHDispatchBlock(DispatchBlock);
680 return DispatchBlock;
681}
682
John McCallbd309292010-07-06 01:34:17 +0000683/// Check whether this is a non-EH scope, i.e. a scope which doesn't
684/// affect exception handling. Currently, the only non-EH scopes are
685/// normal-only cleanup scopes.
686static bool isNonEHScope(const EHScope &S) {
John McCall2b7fc382010-07-13 20:32:21 +0000687 switch (S.getKind()) {
John McCallcda666c2010-07-21 07:22:38 +0000688 case EHScope::Cleanup:
689 return !cast<EHCleanupScope>(S).isEHCleanup();
John McCall2b7fc382010-07-13 20:32:21 +0000690 case EHScope::Filter:
691 case EHScope::Catch:
692 case EHScope::Terminate:
Reid Kleckner2586aac2015-09-10 22:11:13 +0000693 case EHScope::PadEnd:
John McCall2b7fc382010-07-13 20:32:21 +0000694 return false;
695 }
696
David Blaikiee4d798f2012-01-20 21:50:17 +0000697 llvm_unreachable("Invalid EHScope Kind!");
John McCallbd309292010-07-06 01:34:17 +0000698}
699
700llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
701 assert(EHStack.requiresLandingPad());
702 assert(!EHStack.empty());
703
Reid Kleckner8f1b1f52016-03-01 19:51:48 +0000704 // If exceptions are disabled and SEH is not in use, then there is no invoke
705 // destination. SEH "works" even if exceptions are off. In practice, this
706 // means that C++ destructors and other EH cleanups don't run, which is
707 // consistent with MSVC's behavior.
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000708 const LangOptions &LO = CGM.getLangOpts();
709 if (!LO.Exceptions) {
710 if (!LO.Borland && !LO.MicrosoftExt)
711 return nullptr;
Reid Klecknere7b3f7c2015-02-11 00:00:21 +0000712 if (!currentFunctionUsesSEHTry())
Reid Klecknerdeeddec2015-02-05 18:56:03 +0000713 return nullptr;
714 }
John McCall2b7fc382010-07-13 20:32:21 +0000715
Justin Lebar3e6449b2016-10-04 23:41:49 +0000716 // CUDA device code doesn't have exceptions.
717 if (LO.CUDA && LO.CUDAIsDevice)
718 return nullptr;
719
John McCallbd309292010-07-06 01:34:17 +0000720 // Check the innermost scope for a cached landing pad. If this is
721 // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
722 llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
723 if (LP) return LP;
724
David Majnemerdbf10452015-07-31 17:58:45 +0000725 const EHPersonality &Personality = EHPersonality::get(*this);
726
727 if (!CurFn->hasPersonalityFn())
728 CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality));
729
Reid Kleckner129552b2015-10-08 01:13:52 +0000730 if (Personality.usesFuncletPads()) {
731 // We don't need separate landing pads in the funclet model.
David Majnemerdbf10452015-07-31 17:58:45 +0000732 LP = getEHDispatchBlock(EHStack.getInnermostEHScope());
733 } else {
734 // Build the landing pad for this scope.
735 LP = EmitLandingPad();
736 }
737
John McCallbd309292010-07-06 01:34:17 +0000738 assert(LP);
739
740 // Cache the landing pad on the innermost scope. If this is a
741 // non-EH scope, cache the landing pad on the enclosing scope, too.
742 for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
743 ir->setCachedLandingPad(LP);
744 if (!isNonEHScope(*ir)) break;
745 }
746
747 return LP;
748}
749
750llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
751 assert(EHStack.requiresLandingPad());
752
John McCall8e4c74b2011-08-11 02:22:43 +0000753 EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
754 switch (innermostEHScope.getKind()) {
755 case EHScope::Terminate:
756 return getTerminateLandingPad();
John McCallbd309292010-07-06 01:34:17 +0000757
Reid Kleckner2586aac2015-09-10 22:11:13 +0000758 case EHScope::PadEnd:
759 llvm_unreachable("PadEnd unnecessary for Itanium!");
David Majnemerdbf10452015-07-31 17:58:45 +0000760
John McCall8e4c74b2011-08-11 02:22:43 +0000761 case EHScope::Catch:
762 case EHScope::Cleanup:
763 case EHScope::Filter:
764 if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
765 return lpad;
John McCallbd309292010-07-06 01:34:17 +0000766 }
767
768 // Save the current IR generation state.
John McCall8e4c74b2011-08-11 02:22:43 +0000769 CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
Adrian Prantl95b24e92015-02-03 20:00:54 +0000770 auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation);
John McCallbd309292010-07-06 01:34:17 +0000771
772 // Create and configure the landing pad.
John McCall8e4c74b2011-08-11 02:22:43 +0000773 llvm::BasicBlock *lpad = createBasicBlock("lpad");
774 EmitBlock(lpad);
John McCallbd309292010-07-06 01:34:17 +0000775
Serge Guelton1d993272017-05-09 19:31:30 +0000776 llvm::LandingPadInst *LPadInst =
777 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0);
Bill Wendlingf0724e82011-09-19 20:31:14 +0000778
779 llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
780 Builder.CreateStore(LPadExn, getExceptionSlot());
781 llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
782 Builder.CreateStore(LPadSel, getEHSelectorSlot());
783
John McCallbd309292010-07-06 01:34:17 +0000784 // Save the exception pointer. It's safe to use a single exception
785 // pointer per function because EH cleanups can never have nested
786 // try/catches.
Bill Wendlingf0724e82011-09-19 20:31:14 +0000787 // Build the landingpad instruction.
John McCallbd309292010-07-06 01:34:17 +0000788
789 // Accumulate all the handlers in scope.
John McCall8e4c74b2011-08-11 02:22:43 +0000790 bool hasCatchAll = false;
791 bool hasCleanup = false;
792 bool hasFilter = false;
793 SmallVector<llvm::Value*, 4> filterTypes;
794 llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
Nico Webere68b9f32015-02-25 16:25:00 +0000795 for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); I != E;
796 ++I) {
John McCallbd309292010-07-06 01:34:17 +0000797
798 switch (I->getKind()) {
John McCallcda666c2010-07-21 07:22:38 +0000799 case EHScope::Cleanup:
John McCall8e4c74b2011-08-11 02:22:43 +0000800 // If we have a cleanup, remember that.
801 hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
John McCall2b7fc382010-07-13 20:32:21 +0000802 continue;
803
John McCallbd309292010-07-06 01:34:17 +0000804 case EHScope::Filter: {
805 assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
John McCall8e4c74b2011-08-11 02:22:43 +0000806 assert(!hasCatchAll && "EH filter reached after catch-all");
John McCallbd309292010-07-06 01:34:17 +0000807
Bill Wendlingf0724e82011-09-19 20:31:14 +0000808 // Filter scopes get added to the landingpad in weird ways.
John McCall8e4c74b2011-08-11 02:22:43 +0000809 EHFilterScope &filter = cast<EHFilterScope>(*I);
810 hasFilter = true;
John McCallbd309292010-07-06 01:34:17 +0000811
Bill Wendling8c4b7162011-09-22 20:32:54 +0000812 // Add all the filter values.
813 for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
814 filterTypes.push_back(filter.getFilter(i));
John McCallbd309292010-07-06 01:34:17 +0000815 goto done;
816 }
817
818 case EHScope::Terminate:
819 // Terminate scopes are basically catch-alls.
John McCall8e4c74b2011-08-11 02:22:43 +0000820 assert(!hasCatchAll);
821 hasCatchAll = true;
John McCallbd309292010-07-06 01:34:17 +0000822 goto done;
823
824 case EHScope::Catch:
825 break;
David Majnemerdbf10452015-07-31 17:58:45 +0000826
Reid Kleckner2586aac2015-09-10 22:11:13 +0000827 case EHScope::PadEnd:
828 llvm_unreachable("PadEnd unnecessary for Itanium!");
John McCallbd309292010-07-06 01:34:17 +0000829 }
830
John McCall8e4c74b2011-08-11 02:22:43 +0000831 EHCatchScope &catchScope = cast<EHCatchScope>(*I);
832 for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
833 EHCatchScope::Handler handler = catchScope.getHandler(hi);
Reid Kleckner10aa7702015-09-16 20:15:55 +0000834 assert(handler.Type.Flags == 0 &&
835 "landingpads do not support catch handler flags");
John McCallbd309292010-07-06 01:34:17 +0000836
John McCall8e4c74b2011-08-11 02:22:43 +0000837 // If this is a catch-all, register that and abort.
Reid Kleckner10aa7702015-09-16 20:15:55 +0000838 if (!handler.Type.RTTI) {
John McCall8e4c74b2011-08-11 02:22:43 +0000839 assert(!hasCatchAll);
840 hasCatchAll = true;
841 goto done;
John McCallbd309292010-07-06 01:34:17 +0000842 }
843
844 // Check whether we already have a handler for this type.
Reid Kleckner10aa7702015-09-16 20:15:55 +0000845 if (catchTypes.insert(handler.Type.RTTI).second)
Bill Wendlingf0724e82011-09-19 20:31:14 +0000846 // If not, add it directly to the landingpad.
Reid Kleckner10aa7702015-09-16 20:15:55 +0000847 LPadInst->addClause(handler.Type.RTTI);
John McCallbd309292010-07-06 01:34:17 +0000848 }
John McCallbd309292010-07-06 01:34:17 +0000849 }
850
851 done:
Bill Wendlingf0724e82011-09-19 20:31:14 +0000852 // If we have a catch-all, add null to the landingpad.
John McCall8e4c74b2011-08-11 02:22:43 +0000853 assert(!(hasCatchAll && hasFilter));
854 if (hasCatchAll) {
Bill Wendlingf0724e82011-09-19 20:31:14 +0000855 LPadInst->addClause(getCatchAllValue(*this));
John McCallbd309292010-07-06 01:34:17 +0000856
857 // If we have an EH filter, we need to add those handlers in the
Bill Wendlingf0724e82011-09-19 20:31:14 +0000858 // right place in the landingpad, which is to say, at the end.
John McCall8e4c74b2011-08-11 02:22:43 +0000859 } else if (hasFilter) {
Bill Wendling58e58fe2011-09-19 22:08:36 +0000860 // Create a filter expression: a constant array indicating which filter
861 // types there are. The personality routine only lands here if the filter
862 // doesn't match.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000863 SmallVector<llvm::Constant*, 8> Filters;
Bill Wendlingf0724e82011-09-19 20:31:14 +0000864 llvm::ArrayType *AType =
865 llvm::ArrayType::get(!filterTypes.empty() ?
866 filterTypes[0]->getType() : Int8PtrTy,
867 filterTypes.size());
868
869 for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
870 Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
871 llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
872 LPadInst->addClause(FilterArray);
John McCallbd309292010-07-06 01:34:17 +0000873
874 // Also check whether we need a cleanup.
Bill Wendlingf0724e82011-09-19 20:31:14 +0000875 if (hasCleanup)
876 LPadInst->setCleanup(true);
John McCallbd309292010-07-06 01:34:17 +0000877
878 // Otherwise, signal that we at least have cleanups.
Logan Chiene9c8ccb2014-07-01 11:47:10 +0000879 } else if (hasCleanup) {
880 LPadInst->setCleanup(true);
John McCallbd309292010-07-06 01:34:17 +0000881 }
882
Bill Wendlingf0724e82011-09-19 20:31:14 +0000883 assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
884 "landingpad instruction has no clauses!");
John McCallbd309292010-07-06 01:34:17 +0000885
886 // Tell the backend how to generate the landing pad.
John McCall8e4c74b2011-08-11 02:22:43 +0000887 Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
John McCallbd309292010-07-06 01:34:17 +0000888
889 // Restore the old IR generation state.
John McCall8e4c74b2011-08-11 02:22:43 +0000890 Builder.restoreIP(savedIP);
John McCallbd309292010-07-06 01:34:17 +0000891
John McCall8e4c74b2011-08-11 02:22:43 +0000892 return lpad;
John McCallbd309292010-07-06 01:34:17 +0000893}
894
David Majnemer4e52d6f2015-12-12 05:39:21 +0000895static void emitCatchPadBlock(CodeGenFunction &CGF, EHCatchScope &CatchScope) {
David Majnemerdbf10452015-07-31 17:58:45 +0000896 llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
897 assert(DispatchBlock);
898
899 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP();
900 CGF.EmitBlockAfterUses(DispatchBlock);
901
David Majnemer4e52d6f2015-12-12 05:39:21 +0000902 llvm::Value *ParentPad = CGF.CurrentFuncletPad;
903 if (!ParentPad)
904 ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext());
905 llvm::BasicBlock *UnwindBB =
906 CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope());
907
908 unsigned NumHandlers = CatchScope.getNumHandlers();
909 llvm::CatchSwitchInst *CatchSwitch =
910 CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers);
David Majnemerdbf10452015-07-31 17:58:45 +0000911
912 // Test against each of the exception types we claim to catch.
David Majnemer4e52d6f2015-12-12 05:39:21 +0000913 for (unsigned I = 0; I < NumHandlers; ++I) {
David Majnemerdbf10452015-07-31 17:58:45 +0000914 const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
915
Reid Kleckner10aa7702015-09-16 20:15:55 +0000916 CatchTypeInfo TypeInfo = Handler.Type;
917 if (!TypeInfo.RTTI)
918 TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
David Majnemerdbf10452015-07-31 17:58:45 +0000919
David Majnemer4e52d6f2015-12-12 05:39:21 +0000920 CGF.Builder.SetInsertPoint(Handler.Block);
David Majnemerdbf10452015-07-31 17:58:45 +0000921
922 if (EHPersonality::get(CGF).isMSVCXXPersonality()) {
David Majnemer4e52d6f2015-12-12 05:39:21 +0000923 CGF.Builder.CreateCatchPad(
924 CatchSwitch, {TypeInfo.RTTI, CGF.Builder.getInt32(TypeInfo.Flags),
925 llvm::Constant::getNullValue(CGF.VoidPtrTy)});
David Majnemerdbf10452015-07-31 17:58:45 +0000926 } else {
David Majnemer4e52d6f2015-12-12 05:39:21 +0000927 CGF.Builder.CreateCatchPad(CatchSwitch, {TypeInfo.RTTI});
David Majnemerdbf10452015-07-31 17:58:45 +0000928 }
929
David Majnemer4e52d6f2015-12-12 05:39:21 +0000930 CatchSwitch->addHandler(Handler.Block);
David Majnemerdbf10452015-07-31 17:58:45 +0000931 }
932 CGF.Builder.restoreIP(SavedIP);
David Majnemerdbf10452015-07-31 17:58:45 +0000933}
934
Heejin Ahnc6479192018-05-31 22:18:13 +0000935// Wasm uses Windows-style EH instructions, but it merges all catch clauses into
936// one big catchpad, within which we use Itanium's landingpad-style selector
937// comparison instructions.
938static void emitWasmCatchPadBlock(CodeGenFunction &CGF,
939 EHCatchScope &CatchScope) {
940 llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
941 assert(DispatchBlock);
942
943 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveIP();
944 CGF.EmitBlockAfterUses(DispatchBlock);
945
946 llvm::Value *ParentPad = CGF.CurrentFuncletPad;
947 if (!ParentPad)
948 ParentPad = llvm::ConstantTokenNone::get(CGF.getLLVMContext());
949 llvm::BasicBlock *UnwindBB =
950 CGF.getEHDispatchBlock(CatchScope.getEnclosingEHScope());
951
952 unsigned NumHandlers = CatchScope.getNumHandlers();
953 llvm::CatchSwitchInst *CatchSwitch =
954 CGF.Builder.CreateCatchSwitch(ParentPad, UnwindBB, NumHandlers);
955
956 // We don't use a landingpad instruction, so generate intrinsic calls to
957 // provide exception and selector values.
958 llvm::BasicBlock *WasmCatchStartBlock = CGF.createBasicBlock("catch.start");
959 CatchSwitch->addHandler(WasmCatchStartBlock);
960 CGF.EmitBlockAfterUses(WasmCatchStartBlock);
961
962 // Create a catchpad instruction.
963 SmallVector<llvm::Value *, 4> CatchTypes;
964 for (unsigned I = 0, E = NumHandlers; I < E; ++I) {
965 const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
966 CatchTypeInfo TypeInfo = Handler.Type;
967 if (!TypeInfo.RTTI)
968 TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
969 CatchTypes.push_back(TypeInfo.RTTI);
970 }
971 auto *CPI = CGF.Builder.CreateCatchPad(CatchSwitch, CatchTypes);
972
973 // Create calls to wasm.get.exception and wasm.get.ehselector intrinsics.
974 // Before they are lowered appropriately later, they provide values for the
975 // exception and selector.
976 llvm::Value *GetExnFn =
977 CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception);
978 llvm::Value *GetSelectorFn =
979 CGF.CGM.getIntrinsic(llvm::Intrinsic::wasm_get_ehselector);
980 llvm::CallInst *Exn = CGF.Builder.CreateCall(GetExnFn, CPI);
981 CGF.Builder.CreateStore(Exn, CGF.getExceptionSlot());
982 llvm::CallInst *Selector = CGF.Builder.CreateCall(GetSelectorFn, CPI);
983
984 llvm::Value *TypeIDFn = CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
985
986 // If there's only a single catch-all, branch directly to its handler.
987 if (CatchScope.getNumHandlers() == 1 &&
988 CatchScope.getHandler(0).isCatchAll()) {
989 CGF.Builder.CreateBr(CatchScope.getHandler(0).Block);
990 CGF.Builder.restoreIP(SavedIP);
991 return;
992 }
993
994 // Test against each of the exception types we claim to catch.
995 for (unsigned I = 0, E = NumHandlers;; ++I) {
996 assert(I < E && "ran off end of handlers!");
997 const EHCatchScope::Handler &Handler = CatchScope.getHandler(I);
998 CatchTypeInfo TypeInfo = Handler.Type;
999 if (!TypeInfo.RTTI)
1000 TypeInfo.RTTI = llvm::Constant::getNullValue(CGF.VoidPtrTy);
1001
1002 // Figure out the next block.
1003 llvm::BasicBlock *NextBlock;
1004
1005 bool EmitNextBlock = false, NextIsEnd = false;
1006
1007 // If this is the last handler, we're at the end, and the next block is a
1008 // block that contains a call to the rethrow function, so we can unwind to
1009 // the enclosing EH scope. The call itself will be generated later.
1010 if (I + 1 == E) {
1011 NextBlock = CGF.createBasicBlock("rethrow");
1012 EmitNextBlock = true;
1013 NextIsEnd = true;
1014
1015 // If the next handler is a catch-all, we're at the end, and the
1016 // next block is that handler.
1017 } else if (CatchScope.getHandler(I + 1).isCatchAll()) {
1018 NextBlock = CatchScope.getHandler(I + 1).Block;
1019 NextIsEnd = true;
1020
1021 // Otherwise, we're not at the end and we need a new block.
1022 } else {
1023 NextBlock = CGF.createBasicBlock("catch.fallthrough");
1024 EmitNextBlock = true;
1025 }
1026
1027 // Figure out the catch type's index in the LSDA's type table.
1028 llvm::CallInst *TypeIndex = CGF.Builder.CreateCall(TypeIDFn, TypeInfo.RTTI);
1029 TypeIndex->setDoesNotThrow();
1030
1031 llvm::Value *MatchesTypeIndex =
1032 CGF.Builder.CreateICmpEQ(Selector, TypeIndex, "matches");
1033 CGF.Builder.CreateCondBr(MatchesTypeIndex, Handler.Block, NextBlock);
1034
1035 if (EmitNextBlock)
1036 CGF.EmitBlock(NextBlock);
1037 if (NextIsEnd)
1038 break;
1039 }
1040
1041 CGF.Builder.restoreIP(SavedIP);
1042}
1043
John McCall8e4c74b2011-08-11 02:22:43 +00001044/// Emit the structure of the dispatch block for the given catch scope.
1045/// It is an invariant that the dispatch block already exists.
David Majnemer4e52d6f2015-12-12 05:39:21 +00001046static void emitCatchDispatchBlock(CodeGenFunction &CGF,
1047 EHCatchScope &catchScope) {
Heejin Ahnc6479192018-05-31 22:18:13 +00001048 if (EHPersonality::get(CGF).isWasmPersonality())
1049 return emitWasmCatchPadBlock(CGF, catchScope);
Reid Kleckner129552b2015-10-08 01:13:52 +00001050 if (EHPersonality::get(CGF).usesFuncletPads())
1051 return emitCatchPadBlock(CGF, catchScope);
David Majnemerdbf10452015-07-31 17:58:45 +00001052
John McCall8e4c74b2011-08-11 02:22:43 +00001053 llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1054 assert(dispatchBlock);
1055
1056 // If there's only a single catch-all, getEHDispatchBlock returned
1057 // that catch-all as the dispatch block.
1058 if (catchScope.getNumHandlers() == 1 &&
1059 catchScope.getHandler(0).isCatchAll()) {
1060 assert(dispatchBlock == catchScope.getHandler(0).Block);
David Majnemer4e52d6f2015-12-12 05:39:21 +00001061 return;
John McCall8e4c74b2011-08-11 02:22:43 +00001062 }
1063
1064 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1065 CGF.EmitBlockAfterUses(dispatchBlock);
1066
1067 // Select the right handler.
1068 llvm::Value *llvm_eh_typeid_for =
1069 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1070
1071 // Load the selector value.
Bill Wendling79a70e42011-09-15 18:57:19 +00001072 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall8e4c74b2011-08-11 02:22:43 +00001073
1074 // Test against each of the exception types we claim to catch.
1075 for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1076 assert(i < e && "ran off end of handlers!");
1077 const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1078
Reid Kleckner10aa7702015-09-16 20:15:55 +00001079 llvm::Value *typeValue = handler.Type.RTTI;
1080 assert(handler.Type.Flags == 0 &&
1081 "landingpads do not support catch handler flags");
John McCall8e4c74b2011-08-11 02:22:43 +00001082 assert(typeValue && "fell into catch-all case!");
1083 typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1084
1085 // Figure out the next block.
1086 bool nextIsEnd;
1087 llvm::BasicBlock *nextBlock;
1088
1089 // If this is the last handler, we're at the end, and the next
1090 // block is the block for the enclosing EH scope.
1091 if (i + 1 == e) {
1092 nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1093 nextIsEnd = true;
1094
1095 // If the next handler is a catch-all, we're at the end, and the
1096 // next block is that handler.
1097 } else if (catchScope.getHandler(i+1).isCatchAll()) {
1098 nextBlock = catchScope.getHandler(i+1).Block;
1099 nextIsEnd = true;
1100
1101 // Otherwise, we're not at the end and we need a new block.
1102 } else {
1103 nextBlock = CGF.createBasicBlock("catch.fallthrough");
1104 nextIsEnd = false;
1105 }
1106
1107 // Figure out the catch type's index in the LSDA's type table.
1108 llvm::CallInst *typeIndex =
1109 CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1110 typeIndex->setDoesNotThrow();
1111
1112 llvm::Value *matchesTypeIndex =
1113 CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1114 CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1115
1116 // If the next handler is a catch-all, we're completely done.
1117 if (nextIsEnd) {
1118 CGF.Builder.restoreIP(savedIP);
David Majnemer4e52d6f2015-12-12 05:39:21 +00001119 return;
John McCall8e4c74b2011-08-11 02:22:43 +00001120 }
Ahmed Charles289896d2012-02-19 11:57:29 +00001121 // Otherwise we need to emit and continue at that block.
1122 CGF.EmitBlock(nextBlock);
John McCall8e4c74b2011-08-11 02:22:43 +00001123 }
John McCall8e4c74b2011-08-11 02:22:43 +00001124}
1125
1126void CodeGenFunction::popCatchScope() {
1127 EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1128 if (catchScope.hasEHBranches())
1129 emitCatchDispatchBlock(*this, catchScope);
1130 EHStack.popCatch();
1131}
1132
John McCallb609d3f2010-07-07 06:56:46 +00001133void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallbd309292010-07-06 01:34:17 +00001134 unsigned NumHandlers = S.getNumHandlers();
1135 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1136 assert(CatchScope.getNumHandlers() == NumHandlers);
Heejin Ahnc6479192018-05-31 22:18:13 +00001137 llvm::BasicBlock *DispatchBlock = CatchScope.getCachedEHDispatchBlock();
Mike Stump58ef18b2009-11-20 23:44:51 +00001138
John McCall8e4c74b2011-08-11 02:22:43 +00001139 // If the catch was not required, bail out now.
1140 if (!CatchScope.hasEHBranches()) {
Kostya Serebryanyba4aced2014-01-09 09:22:32 +00001141 CatchScope.clearHandlerBlocks();
John McCall8e4c74b2011-08-11 02:22:43 +00001142 EHStack.popCatch();
1143 return;
1144 }
1145
1146 // Emit the structure of the EH dispatch for this catch.
David Majnemer4e52d6f2015-12-12 05:39:21 +00001147 emitCatchDispatchBlock(*this, CatchScope);
John McCall8e4c74b2011-08-11 02:22:43 +00001148
John McCallbd309292010-07-06 01:34:17 +00001149 // Copy the handler blocks off before we pop the EH stack. Emitting
1150 // the handlers might scribble on this memory.
Benjamin Kramerda32cf82015-08-04 15:38:49 +00001151 SmallVector<EHCatchScope::Handler, 8> Handlers(
1152 CatchScope.begin(), CatchScope.begin() + NumHandlers);
John McCall8e4c74b2011-08-11 02:22:43 +00001153
John McCallbd309292010-07-06 01:34:17 +00001154 EHStack.popCatch();
Mike Stump58ef18b2009-11-20 23:44:51 +00001155
John McCallbd309292010-07-06 01:34:17 +00001156 // The fall-through block.
1157 llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
Mike Stump58ef18b2009-11-20 23:44:51 +00001158
John McCallbd309292010-07-06 01:34:17 +00001159 // We just emitted the body of the try; jump to the continue block.
1160 if (HaveInsertPoint())
1161 Builder.CreateBr(ContBB);
Mike Stump97329152009-12-02 19:53:57 +00001162
John McCalld8d00be2012-06-15 05:27:05 +00001163 // Determine if we need an implicit rethrow for all these catch handlers;
1164 // see the comment below.
1165 bool doImplicitRethrow = false;
John McCallb609d3f2010-07-07 06:56:46 +00001166 if (IsFnTryBlock)
John McCalld8d00be2012-06-15 05:27:05 +00001167 doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1168 isa<CXXConstructorDecl>(CurCodeDecl);
John McCallb609d3f2010-07-07 06:56:46 +00001169
Heejin Ahnc6479192018-05-31 22:18:13 +00001170 // Wasm uses Windows-style EH instructions, but merges all catch clauses into
1171 // one big catchpad. So we save the old funclet pad here before we traverse
1172 // each catch handler.
1173 SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1174 CurrentFuncletPad);
1175 llvm::BasicBlock *WasmCatchStartBlock = nullptr;
1176 if (EHPersonality::get(*this).isWasmPersonality()) {
1177 auto *CatchSwitch =
1178 cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHI());
1179 WasmCatchStartBlock = CatchSwitch->hasUnwindDest()
1180 ? CatchSwitch->getSuccessor(1)
1181 : CatchSwitch->getSuccessor(0);
1182 auto *CPI = cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHI());
1183 CurrentFuncletPad = CPI;
1184 }
1185
John McCall8e4c74b2011-08-11 02:22:43 +00001186 // Perversely, we emit the handlers backwards precisely because we
1187 // want them to appear in source order. In all of these cases, the
1188 // catch block will have exactly one predecessor, which will be a
1189 // particular block in the catch dispatch. However, in the case of
1190 // a catch-all, one of the dispatch blocks will branch to two
1191 // different handlers, and EmitBlockAfterUses will cause the second
1192 // handler to be moved before the first.
Heejin Ahnc6479192018-05-31 22:18:13 +00001193 bool HasCatchAll = false;
John McCall8e4c74b2011-08-11 02:22:43 +00001194 for (unsigned I = NumHandlers; I != 0; --I) {
Heejin Ahnc6479192018-05-31 22:18:13 +00001195 HasCatchAll |= Handlers[I - 1].isCatchAll();
John McCall8e4c74b2011-08-11 02:22:43 +00001196 llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1197 EmitBlockAfterUses(CatchBlock);
Mike Stump75546b82009-12-10 00:06:18 +00001198
John McCallbd309292010-07-06 01:34:17 +00001199 // Catch the exception if this isn't a catch-all.
John McCall8e4c74b2011-08-11 02:22:43 +00001200 const CXXCatchStmt *C = S.getHandler(I-1);
Mike Stump58ef18b2009-11-20 23:44:51 +00001201
John McCallbd309292010-07-06 01:34:17 +00001202 // Enter a cleanup scope, including the catch variable and the
1203 // end-catch.
1204 RunCleanupsScope CatchScope(*this);
Mike Stump58ef18b2009-11-20 23:44:51 +00001205
John McCallbd309292010-07-06 01:34:17 +00001206 // Initialize the catch variable and set up the cleanups.
David Majnemer4e52d6f2015-12-12 05:39:21 +00001207 SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1208 CurrentFuncletPad);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00001209 CGM.getCXXABI().emitBeginCatch(*this, C);
John McCallbd309292010-07-06 01:34:17 +00001210
Justin Bognerea278c32014-01-07 00:20:28 +00001211 // Emit the PGO counter increment.
Justin Bogner66242d62015-04-23 23:06:47 +00001212 incrementProfileCounter(C);
Justin Bogneref512b92014-01-06 22:27:43 +00001213
John McCallbd309292010-07-06 01:34:17 +00001214 // Perform the body of the catch.
1215 EmitStmt(C->getHandlerBlock());
1216
John McCalld8d00be2012-06-15 05:27:05 +00001217 // [except.handle]p11:
1218 // The currently handled exception is rethrown if control
1219 // reaches the end of a handler of the function-try-block of a
1220 // constructor or destructor.
1221
1222 // It is important that we only do this on fallthrough and not on
1223 // return. Note that it's illegal to put a return in a
1224 // constructor function-try-block's catch handler (p14), so this
1225 // really only applies to destructors.
1226 if (doImplicitRethrow && HaveInsertPoint()) {
David Majnemer442d0a22014-11-25 07:20:20 +00001227 CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false);
John McCalld8d00be2012-06-15 05:27:05 +00001228 Builder.CreateUnreachable();
1229 Builder.ClearInsertionPoint();
1230 }
1231
John McCallbd309292010-07-06 01:34:17 +00001232 // Fall out through the catch cleanups.
1233 CatchScope.ForceCleanup();
1234
1235 // Branch out of the try.
1236 if (HaveInsertPoint())
1237 Builder.CreateBr(ContBB);
Mike Stump58ef18b2009-11-20 23:44:51 +00001238 }
1239
Heejin Ahnc6479192018-05-31 22:18:13 +00001240 // Because in wasm we merge all catch clauses into one big catchpad, in case
1241 // none of the types in catch handlers matches after we test against each of
1242 // them, we should unwind to the next EH enclosing scope. We generate a call
1243 // to rethrow function here to do that.
1244 if (EHPersonality::get(*this).isWasmPersonality() && !HasCatchAll) {
1245 assert(WasmCatchStartBlock);
1246 // Navigate for the "rethrow" block we created in emitWasmCatchPadBlock().
1247 // Wasm uses landingpad-style conditional branches to compare selectors, so
1248 // we follow the false destination for each of the cond branches to reach
1249 // the rethrow block.
1250 llvm::BasicBlock *RethrowBlock = WasmCatchStartBlock;
1251 while (llvm::TerminatorInst *TI = RethrowBlock->getTerminator()) {
1252 auto *BI = cast<llvm::BranchInst>(TI);
1253 assert(BI->isConditional());
1254 RethrowBlock = BI->getSuccessor(1);
1255 }
1256 assert(RethrowBlock != WasmCatchStartBlock && RethrowBlock->empty());
1257 Builder.SetInsertPoint(RethrowBlock);
1258 CGM.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true);
1259 }
1260
John McCallbd309292010-07-06 01:34:17 +00001261 EmitBlock(ContBB);
Justin Bogner66242d62015-04-23 23:06:47 +00001262 incrementProfileCounter(&S);
Mike Stump58ef18b2009-11-20 23:44:51 +00001263}
Mike Stumpaff69af2009-12-09 03:35:49 +00001264
John McCall1e670402010-07-21 00:52:03 +00001265namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00001266 struct CallEndCatchForFinally final : EHScopeStack::Cleanup {
John McCall1e670402010-07-21 00:52:03 +00001267 llvm::Value *ForEHVar;
1268 llvm::Value *EndCatchFn;
1269 CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1270 : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1271
Craig Topper4f12f102014-03-12 06:41:41 +00001272 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall1e670402010-07-21 00:52:03 +00001273 llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1274 llvm::BasicBlock *CleanupContBB =
1275 CGF.createBasicBlock("finally.cleanup.cont");
1276
1277 llvm::Value *ShouldEndCatch =
John McCall7f416cc2015-09-08 08:05:57 +00001278 CGF.Builder.CreateFlagLoad(ForEHVar, "finally.endcatch");
John McCall1e670402010-07-21 00:52:03 +00001279 CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1280 CGF.EmitBlock(EndCatchBB);
John McCall882987f2013-02-28 19:01:20 +00001281 CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw
John McCall1e670402010-07-21 00:52:03 +00001282 CGF.EmitBlock(CleanupContBB);
1283 }
1284 };
John McCall906da4b2010-07-21 05:47:49 +00001285
David Blaikie7e70d682015-08-18 22:40:54 +00001286 struct PerformFinally final : EHScopeStack::Cleanup {
John McCall906da4b2010-07-21 05:47:49 +00001287 const Stmt *Body;
1288 llvm::Value *ForEHVar;
1289 llvm::Value *EndCatchFn;
1290 llvm::Value *RethrowFn;
1291 llvm::Value *SavedExnVar;
1292
1293 PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1294 llvm::Value *EndCatchFn,
1295 llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1296 : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1297 RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1298
Craig Topper4f12f102014-03-12 06:41:41 +00001299 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall906da4b2010-07-21 05:47:49 +00001300 // Enter a cleanup to call the end-catch function if one was provided.
1301 if (EndCatchFn)
John McCallcda666c2010-07-21 07:22:38 +00001302 CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1303 ForEHVar, EndCatchFn);
John McCall906da4b2010-07-21 05:47:49 +00001304
John McCallcebe0ca2010-08-11 00:16:14 +00001305 // Save the current cleanup destination in case there are
1306 // cleanups in the finally block.
1307 llvm::Value *SavedCleanupDest =
1308 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1309 "cleanup.dest.saved");
1310
John McCall906da4b2010-07-21 05:47:49 +00001311 // Emit the finally block.
1312 CGF.EmitStmt(Body);
1313
1314 // If the end of the finally is reachable, check whether this was
1315 // for EH. If so, rethrow.
1316 if (CGF.HaveInsertPoint()) {
1317 llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1318 llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1319
1320 llvm::Value *ShouldRethrow =
John McCall7f416cc2015-09-08 08:05:57 +00001321 CGF.Builder.CreateFlagLoad(ForEHVar, "finally.shouldthrow");
John McCall906da4b2010-07-21 05:47:49 +00001322 CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1323
1324 CGF.EmitBlock(RethrowBB);
1325 if (SavedExnVar) {
John McCall882987f2013-02-28 19:01:20 +00001326 CGF.EmitRuntimeCallOrInvoke(RethrowFn,
John McCall7f416cc2015-09-08 08:05:57 +00001327 CGF.Builder.CreateAlignedLoad(SavedExnVar, CGF.getPointerAlign()));
John McCall906da4b2010-07-21 05:47:49 +00001328 } else {
John McCall882987f2013-02-28 19:01:20 +00001329 CGF.EmitRuntimeCallOrInvoke(RethrowFn);
John McCall906da4b2010-07-21 05:47:49 +00001330 }
1331 CGF.Builder.CreateUnreachable();
1332
1333 CGF.EmitBlock(ContBB);
John McCallcebe0ca2010-08-11 00:16:14 +00001334
1335 // Restore the cleanup destination.
1336 CGF.Builder.CreateStore(SavedCleanupDest,
1337 CGF.getNormalCleanupDestSlot());
John McCall906da4b2010-07-21 05:47:49 +00001338 }
1339
1340 // Leave the end-catch cleanup. As an optimization, pretend that
1341 // the fallthrough path was inaccessible; we've dynamically proven
1342 // that we're not in the EH case along that path.
1343 if (EndCatchFn) {
1344 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1345 CGF.PopCleanupBlock();
1346 CGF.Builder.restoreIP(SavedIP);
1347 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001348
John McCall906da4b2010-07-21 05:47:49 +00001349 // Now make sure we actually have an insertion point or the
1350 // cleanup gods will hate us.
1351 CGF.EnsureInsertPoint();
1352 }
1353 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00001354} // end anonymous namespace
John McCall1e670402010-07-21 00:52:03 +00001355
John McCallbd309292010-07-06 01:34:17 +00001356/// Enters a finally block for an implementation using zero-cost
1357/// exceptions. This is mostly general, but hard-codes some
1358/// language/ABI-specific behavior in the catch-all sections.
John McCall6b0feb72011-06-22 02:32:12 +00001359void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1360 const Stmt *body,
1361 llvm::Constant *beginCatchFn,
1362 llvm::Constant *endCatchFn,
1363 llvm::Constant *rethrowFn) {
Craig Topper8a13c412014-05-21 05:09:00 +00001364 assert((beginCatchFn != nullptr) == (endCatchFn != nullptr) &&
John McCallbd309292010-07-06 01:34:17 +00001365 "begin/end catch functions not paired");
John McCall6b0feb72011-06-22 02:32:12 +00001366 assert(rethrowFn && "rethrow function is required");
1367
1368 BeginCatchFn = beginCatchFn;
Mike Stumpaff69af2009-12-09 03:35:49 +00001369
John McCallbd309292010-07-06 01:34:17 +00001370 // The rethrow function has one of the following two types:
1371 // void (*)()
1372 // void (*)(void*)
1373 // In the latter case we need to pass it the exception object.
1374 // But we can't use the exception slot because the @finally might
1375 // have a landing pad (which would overwrite the exception slot).
Chris Lattner2192fe52011-07-18 04:24:23 +00001376 llvm::FunctionType *rethrowFnTy =
John McCallbd309292010-07-06 01:34:17 +00001377 cast<llvm::FunctionType>(
John McCall6b0feb72011-06-22 02:32:12 +00001378 cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
Craig Topper8a13c412014-05-21 05:09:00 +00001379 SavedExnVar = nullptr;
John McCall6b0feb72011-06-22 02:32:12 +00001380 if (rethrowFnTy->getNumParams())
1381 SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
Mike Stumpaff69af2009-12-09 03:35:49 +00001382
John McCallbd309292010-07-06 01:34:17 +00001383 // A finally block is a statement which must be executed on any edge
1384 // out of a given scope. Unlike a cleanup, the finally block may
1385 // contain arbitrary control flow leading out of itself. In
1386 // addition, finally blocks should always be executed, even if there
1387 // are no catch handlers higher on the stack. Therefore, we
1388 // surround the protected scope with a combination of a normal
1389 // cleanup (to catch attempts to break out of the block via normal
1390 // control flow) and an EH catch-all (semantically "outside" any try
1391 // statement to which the finally block might have been attached).
1392 // The finally block itself is generated in the context of a cleanup
1393 // which conditionally leaves the catch-all.
John McCall21886962010-04-21 10:05:39 +00001394
John McCallbd309292010-07-06 01:34:17 +00001395 // Jump destination for performing the finally block on an exception
1396 // edge. We'll never actually reach this block, so unreachable is
1397 // fine.
John McCall6b0feb72011-06-22 02:32:12 +00001398 RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
John McCall21886962010-04-21 10:05:39 +00001399
John McCallbd309292010-07-06 01:34:17 +00001400 // Whether the finally block is being executed for EH purposes.
John McCall6b0feb72011-06-22 02:32:12 +00001401 ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
John McCall7f416cc2015-09-08 08:05:57 +00001402 CGF.Builder.CreateFlagStore(false, ForEHVar);
Mike Stumpaff69af2009-12-09 03:35:49 +00001403
John McCallbd309292010-07-06 01:34:17 +00001404 // Enter a normal cleanup which will perform the @finally block.
John McCall6b0feb72011-06-22 02:32:12 +00001405 CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1406 ForEHVar, endCatchFn,
1407 rethrowFn, SavedExnVar);
John McCallbd309292010-07-06 01:34:17 +00001408
1409 // Enter a catch-all scope.
John McCall6b0feb72011-06-22 02:32:12 +00001410 llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1411 EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1412 catchScope->setCatchAllHandler(0, catchBB);
John McCallbd309292010-07-06 01:34:17 +00001413}
1414
John McCall6b0feb72011-06-22 02:32:12 +00001415void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
John McCallbd309292010-07-06 01:34:17 +00001416 // Leave the finally catch-all.
John McCall6b0feb72011-06-22 02:32:12 +00001417 EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1418 llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
John McCall8e4c74b2011-08-11 02:22:43 +00001419
1420 CGF.popCatchScope();
John McCallbd309292010-07-06 01:34:17 +00001421
John McCall6b0feb72011-06-22 02:32:12 +00001422 // If there are any references to the catch-all block, emit it.
1423 if (catchBB->use_empty()) {
1424 delete catchBB;
1425 } else {
1426 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1427 CGF.EmitBlock(catchBB);
John McCallbd309292010-07-06 01:34:17 +00001428
Craig Topper8a13c412014-05-21 05:09:00 +00001429 llvm::Value *exn = nullptr;
John McCallbd309292010-07-06 01:34:17 +00001430
John McCall6b0feb72011-06-22 02:32:12 +00001431 // If there's a begin-catch function, call it.
1432 if (BeginCatchFn) {
Bill Wendling79a70e42011-09-15 18:57:19 +00001433 exn = CGF.getExceptionFromSlot();
John McCall882987f2013-02-28 19:01:20 +00001434 CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn);
John McCall6b0feb72011-06-22 02:32:12 +00001435 }
1436
1437 // If we need to remember the exception pointer to rethrow later, do so.
1438 if (SavedExnVar) {
Bill Wendling79a70e42011-09-15 18:57:19 +00001439 if (!exn) exn = CGF.getExceptionFromSlot();
John McCall7f416cc2015-09-08 08:05:57 +00001440 CGF.Builder.CreateAlignedStore(exn, SavedExnVar, CGF.getPointerAlign());
John McCall6b0feb72011-06-22 02:32:12 +00001441 }
1442
1443 // Tell the cleanups in the finally block that we're do this for EH.
John McCall7f416cc2015-09-08 08:05:57 +00001444 CGF.Builder.CreateFlagStore(true, ForEHVar);
John McCall6b0feb72011-06-22 02:32:12 +00001445
1446 // Thread a jump through the finally cleanup.
1447 CGF.EmitBranchThroughCleanup(RethrowDest);
1448
1449 CGF.Builder.restoreIP(savedIP);
1450 }
1451
1452 // Finally, leave the @finally cleanup.
1453 CGF.PopCleanupBlock();
John McCallbd309292010-07-06 01:34:17 +00001454}
1455
1456llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1457 if (TerminateLandingPad)
1458 return TerminateLandingPad;
1459
1460 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1461
1462 // This will get inserted at the end of the function.
1463 TerminateLandingPad = createBasicBlock("terminate.lpad");
1464 Builder.SetInsertPoint(TerminateLandingPad);
1465
1466 // Tell the backend that this is a landing pad.
Reid Klecknerdeeddec2015-02-05 18:56:03 +00001467 const EHPersonality &Personality = EHPersonality::get(*this);
David Majnemerfcbdb6e2015-06-17 20:53:19 +00001468
1469 if (!CurFn->hasPersonalityFn())
1470 CurFn->setPersonalityFn(getOpaquePersonalityFn(CGM, Personality));
1471
Serge Guelton1d993272017-05-09 19:31:30 +00001472 llvm::LandingPadInst *LPadInst =
1473 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty), 0);
Bill Wendlingf0724e82011-09-19 20:31:14 +00001474 LPadInst->addClause(getCatchAllValue(*this));
John McCallbd309292010-07-06 01:34:17 +00001475
Hans Wennborgdcfba332015-10-06 23:40:43 +00001476 llvm::Value *Exn = nullptr;
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00001477 if (getLangOpts().CPlusPlus)
1478 Exn = Builder.CreateExtractValue(LPadInst, 0);
1479 llvm::CallInst *terminateCall =
1480 CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
John McCalle142ad52013-02-12 03:51:46 +00001481 terminateCall->setDoesNotReturn();
John McCallad7c5c12011-02-08 08:22:06 +00001482 Builder.CreateUnreachable();
Mike Stumpaff69af2009-12-09 03:35:49 +00001483
John McCallbd309292010-07-06 01:34:17 +00001484 // Restore the saved insertion state.
1485 Builder.restoreIP(SavedIP);
John McCalldac3ea62010-04-30 00:06:43 +00001486
John McCallbd309292010-07-06 01:34:17 +00001487 return TerminateLandingPad;
Mike Stumpaff69af2009-12-09 03:35:49 +00001488}
Mike Stump2b488872009-12-09 22:59:31 +00001489
1490llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stumpf5cbb082009-12-10 00:02:42 +00001491 if (TerminateHandler)
1492 return TerminateHandler;
1493
John McCallbd309292010-07-06 01:34:17 +00001494 // Set up the terminate handler. This block is inserted at the very
1495 // end of the function by FinishFunction.
Mike Stumpf5cbb082009-12-10 00:02:42 +00001496 TerminateHandler = createBasicBlock("terminate.handler");
Reid Kleckner06f19a02018-01-02 21:34:16 +00001497 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
John McCallbd309292010-07-06 01:34:17 +00001498 Builder.SetInsertPoint(TerminateHandler);
Reid Kleckner06f19a02018-01-02 21:34:16 +00001499
David Majnemerfeeefb22015-12-14 18:34:18 +00001500 llvm::Value *Exn = nullptr;
Reid Kleckner06f19a02018-01-02 21:34:16 +00001501 if (getLangOpts().CPlusPlus)
1502 Exn = getExceptionFromSlot();
David Majnemerfeeefb22015-12-14 18:34:18 +00001503 llvm::CallInst *terminateCall =
1504 CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
1505 terminateCall->setDoesNotReturn();
1506 Builder.CreateUnreachable();
Mike Stump2b488872009-12-09 22:59:31 +00001507
John McCall21886962010-04-21 10:05:39 +00001508 // Restore the saved insertion state.
John McCallbd309292010-07-06 01:34:17 +00001509 Builder.restoreIP(SavedIP);
Mike Stump25b20fc2009-12-09 23:31:35 +00001510
Mike Stump2b488872009-12-09 22:59:31 +00001511 return TerminateHandler;
1512}
John McCallbd309292010-07-06 01:34:17 +00001513
Reid Kleckner06f19a02018-01-02 21:34:16 +00001514llvm::BasicBlock *CodeGenFunction::getTerminateFunclet() {
1515 assert(EHPersonality::get(*this).usesFuncletPads() &&
1516 "use getTerminateLandingPad for non-funclet EH");
1517
1518 llvm::BasicBlock *&TerminateFunclet = TerminateFunclets[CurrentFuncletPad];
1519 if (TerminateFunclet)
1520 return TerminateFunclet;
1521
1522 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1523
1524 // Set up the terminate handler. This block is inserted at the very
1525 // end of the function by FinishFunction.
1526 TerminateFunclet = createBasicBlock("terminate.handler");
1527 Builder.SetInsertPoint(TerminateFunclet);
1528
1529 // Create the cleanuppad using the current parent pad as its token. Use 'none'
1530 // if this is a top-level terminate scope, which is the common case.
1531 SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
1532 CurrentFuncletPad);
1533 llvm::Value *ParentPad = CurrentFuncletPad;
1534 if (!ParentPad)
1535 ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
1536 CurrentFuncletPad = Builder.CreateCleanupPad(ParentPad);
1537
1538 // Emit the __std_terminate call.
Heejin Ahnc6479192018-05-31 22:18:13 +00001539 llvm::Value *Exn = nullptr;
1540 // In case of wasm personality, we need to pass the exception value to
1541 // __clang_call_terminate function.
1542 if (getLangOpts().CPlusPlus &&
1543 EHPersonality::get(*this).isWasmPersonality()) {
1544 llvm::Value *GetExnFn =
1545 CGM.getIntrinsic(llvm::Intrinsic::wasm_get_exception);
1546 Exn = Builder.CreateCall(GetExnFn, CurrentFuncletPad);
1547 }
Reid Kleckner06f19a02018-01-02 21:34:16 +00001548 llvm::CallInst *terminateCall =
Heejin Ahnc6479192018-05-31 22:18:13 +00001549 CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
Reid Kleckner06f19a02018-01-02 21:34:16 +00001550 terminateCall->setDoesNotReturn();
1551 Builder.CreateUnreachable();
1552
1553 // Restore the saved insertion state.
1554 Builder.restoreIP(SavedIP);
1555
1556 return TerminateFunclet;
1557}
1558
David Chisnall9a837be2012-11-07 16:50:40 +00001559llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) {
John McCall8e4c74b2011-08-11 02:22:43 +00001560 if (EHResumeBlock) return EHResumeBlock;
John McCallad5d61e2010-07-23 21:56:41 +00001561
1562 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1563
1564 // We emit a jump to a notional label at the outermost unwind state.
John McCall8e4c74b2011-08-11 02:22:43 +00001565 EHResumeBlock = createBasicBlock("eh.resume");
1566 Builder.SetInsertPoint(EHResumeBlock);
John McCallad5d61e2010-07-23 21:56:41 +00001567
Reid Klecknerdeeddec2015-02-05 18:56:03 +00001568 const EHPersonality &Personality = EHPersonality::get(*this);
John McCallad5d61e2010-07-23 21:56:41 +00001569
1570 // This can always be a call because we necessarily didn't find
1571 // anything on the EH stack which needs our help.
Benjamin Kramer793bd552012-02-08 12:41:24 +00001572 const char *RethrowName = Personality.CatchallRethrowFn;
Craig Topper8a13c412014-05-21 05:09:00 +00001573 if (RethrowName != nullptr && !isCleanup) {
John McCall882987f2013-02-28 19:01:20 +00001574 EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName),
Nico Weberff62a6a2015-02-26 22:34:33 +00001575 getExceptionFromSlot())->setDoesNotReturn();
Logan Chiene9c8ccb2014-07-01 11:47:10 +00001576 Builder.CreateUnreachable();
1577 Builder.restoreIP(SavedIP);
1578 return EHResumeBlock;
John McCall9b382dd2011-05-28 21:13:02 +00001579 }
1580
Logan Chiene9c8ccb2014-07-01 11:47:10 +00001581 // Recreate the landingpad's return value for the 'resume' instruction.
1582 llvm::Value *Exn = getExceptionFromSlot();
1583 llvm::Value *Sel = getSelectorFromSlot();
John McCallad5d61e2010-07-23 21:56:41 +00001584
Serge Guelton1d993272017-05-09 19:31:30 +00001585 llvm::Type *LPadType = llvm::StructType::get(Exn->getType(), Sel->getType());
Logan Chiene9c8ccb2014-07-01 11:47:10 +00001586 llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1587 LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1588 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1589
1590 Builder.CreateResume(LPadVal);
John McCallad5d61e2010-07-23 21:56:41 +00001591 Builder.restoreIP(SavedIP);
John McCall8e4c74b2011-08-11 02:22:43 +00001592 return EHResumeBlock;
John McCallad5d61e2010-07-23 21:56:41 +00001593}
Reid Kleckner543a16c2013-09-16 21:46:30 +00001594
1595void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) {
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001596 EnterSEHTryStmt(S);
Reid Klecknera5930002015-02-11 21:40:48 +00001597 {
Nico Weber5779f842015-02-12 23:16:11 +00001598 JumpDest TryExit = getJumpDestInCurrentScope("__try.__leave");
Nico Weber5779f842015-02-12 23:16:11 +00001599
Reid Kleckner11c033e2015-02-12 23:40:45 +00001600 SEHTryEpilogueStack.push_back(&TryExit);
Reid Klecknera5930002015-02-11 21:40:48 +00001601 EmitStmt(S.getTryBlock());
Reid Kleckner11c033e2015-02-12 23:40:45 +00001602 SEHTryEpilogueStack.pop_back();
Nico Weber5779f842015-02-12 23:16:11 +00001603
1604 if (!TryExit.getBlock()->use_empty())
1605 EmitBlock(TryExit.getBlock(), /*IsFinished=*/true);
1606 else
1607 delete TryExit.getBlock();
Reid Klecknera5930002015-02-11 21:40:48 +00001608 }
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001609 ExitSEHTryStmt(S);
Reid Kleckner1d59f992015-01-22 01:36:17 +00001610}
1611
1612namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00001613struct PerformSEHFinally final : EHScopeStack::Cleanup {
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001614 llvm::Function *OutlinedFinally;
Reid Kleckner55391522015-10-08 21:14:56 +00001615 PerformSEHFinally(llvm::Function *OutlinedFinally)
1616 : OutlinedFinally(OutlinedFinally) {}
Reid Kleckneraca01db2015-02-04 22:37:07 +00001617
Reid Kleckner1d59f992015-01-22 01:36:17 +00001618 void Emit(CodeGenFunction &CGF, Flags F) override {
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001619 ASTContext &Context = CGF.getContext();
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001620 CodeGenModule &CGM = CGF.CGM;
Reid Kleckner65870442015-06-09 17:47:50 +00001621
Reid Klecknerd0d9a1f2015-07-01 17:10:10 +00001622 CallArgList Args;
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001623
1624 // Compute the two argument values.
1625 QualType ArgTys[2] = {Context.UnsignedCharTy, Context.VoidPtrTy};
Reid Kleckner15d152d2015-07-07 23:23:31 +00001626 llvm::Value *LocalAddrFn = CGM.getIntrinsic(llvm::Intrinsic::localaddress);
David Blaikie4ba525b2015-07-14 17:27:39 +00001627 llvm::Value *FP = CGF.Builder.CreateCall(LocalAddrFn);
Reid Klecknereb11c412015-07-01 21:00:00 +00001628 llvm::Value *IsForEH =
1629 llvm::ConstantInt::get(CGF.ConvertType(ArgTys[0]), F.isForEHCleanup());
1630 Args.add(RValue::get(IsForEH), ArgTys[0]);
1631 Args.add(RValue::get(FP), ArgTys[1]);
Reid Klecknerd0d9a1f2015-07-01 17:10:10 +00001632
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001633 // Arrange a two-arg function info and type.
Reid Klecknereb11c412015-07-01 21:00:00 +00001634 const CGFunctionInfo &FnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00001635 CGM.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, Args);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001636
John McCallb92ab1a2016-10-26 23:46:34 +00001637 auto Callee = CGCallee::forDirect(OutlinedFinally);
1638 CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
Reid Kleckner1d59f992015-01-22 01:36:17 +00001639 }
1640};
Hans Wennborgdcfba332015-10-06 23:40:43 +00001641} // end anonymous namespace
Reid Kleckner1d59f992015-01-22 01:36:17 +00001642
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001643namespace {
1644/// Find all local variable captures in the statement.
1645struct CaptureFinder : ConstStmtVisitor<CaptureFinder> {
1646 CodeGenFunction &ParentCGF;
1647 const VarDecl *ParentThis;
John McCall0a490152015-09-08 21:15:22 +00001648 llvm::SmallSetVector<const VarDecl *, 4> Captures;
John McCall7f416cc2015-09-08 08:05:57 +00001649 Address SEHCodeSlot = Address::invalid();
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001650 CaptureFinder(CodeGenFunction &ParentCGF, const VarDecl *ParentThis)
1651 : ParentCGF(ParentCGF), ParentThis(ParentThis) {}
1652
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001653 // Return true if we need to do any capturing work.
1654 bool foundCaptures() {
John McCall7f416cc2015-09-08 08:05:57 +00001655 return !Captures.empty() || SEHCodeSlot.isValid();
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001656 }
1657
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001658 void Visit(const Stmt *S) {
1659 // See if this is a capture, then recurse.
1660 ConstStmtVisitor<CaptureFinder>::Visit(S);
1661 for (const Stmt *Child : S->children())
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001662 if (Child)
1663 Visit(Child);
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001664 }
1665
1666 void VisitDeclRefExpr(const DeclRefExpr *E) {
1667 // If this is already a capture, just make sure we capture 'this'.
1668 if (E->refersToEnclosingVariableOrCapture()) {
John McCall0a490152015-09-08 21:15:22 +00001669 Captures.insert(ParentThis);
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001670 return;
1671 }
1672
1673 const auto *D = dyn_cast<VarDecl>(E->getDecl());
1674 if (D && D->isLocalVarDeclOrParm() && D->hasLocalStorage())
John McCall0a490152015-09-08 21:15:22 +00001675 Captures.insert(D);
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001676 }
1677
1678 void VisitCXXThisExpr(const CXXThisExpr *E) {
John McCall0a490152015-09-08 21:15:22 +00001679 Captures.insert(ParentThis);
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001680 }
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001681
1682 void VisitCallExpr(const CallExpr *E) {
1683 // We only need to add parent frame allocations for these builtins in x86.
1684 if (ParentCGF.getTarget().getTriple().getArch() != llvm::Triple::x86)
1685 return;
1686
1687 unsigned ID = E->getBuiltinCallee();
1688 switch (ID) {
1689 case Builtin::BI__exception_code:
1690 case Builtin::BI_exception_code:
1691 // This is the simple case where we are the outermost finally. All we
1692 // have to do here is make sure we escape this and recover it in the
1693 // outlined handler.
John McCall7f416cc2015-09-08 08:05:57 +00001694 if (!SEHCodeSlot.isValid())
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001695 SEHCodeSlot = ParentCGF.SEHCodeSlotStack.back();
1696 break;
1697 }
1698 }
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001699};
Hans Wennborgdcfba332015-10-06 23:40:43 +00001700} // end anonymous namespace
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001701
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001702Address CodeGenFunction::recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF,
1703 Address ParentVar,
1704 llvm::Value *ParentFP) {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001705 llvm::CallInst *RecoverCall = nullptr;
John McCall7f416cc2015-09-08 08:05:57 +00001706 CGBuilderTy Builder(*this, AllocaInsertPt);
1707 if (auto *ParentAlloca = dyn_cast<llvm::AllocaInst>(ParentVar.getPointer())) {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001708 // Mark the variable escaped if nobody else referenced it and compute the
Reid Kleckner98cb8ba2015-07-07 22:26:07 +00001709 // localescape index.
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001710 auto InsertPair = ParentCGF.EscapedLocals.insert(
1711 std::make_pair(ParentAlloca, ParentCGF.EscapedLocals.size()));
1712 int FrameEscapeIdx = InsertPair.first->second;
Reid Kleckner98cb8ba2015-07-07 22:26:07 +00001713 // call i8* @llvm.localrecover(i8* bitcast(@parentFn), i8* %fp, i32 N)
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001714 llvm::Function *FrameRecoverFn = llvm::Intrinsic::getDeclaration(
Reid Kleckner98cb8ba2015-07-07 22:26:07 +00001715 &CGM.getModule(), llvm::Intrinsic::localrecover);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001716 llvm::Constant *ParentI8Fn =
1717 llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy);
1718 RecoverCall = Builder.CreateCall(
1719 FrameRecoverFn, {ParentI8Fn, ParentFP,
1720 llvm::ConstantInt::get(Int32Ty, FrameEscapeIdx)});
1721
1722 } else {
1723 // If the parent didn't have an alloca, we're doing some nested outlining.
Reid Kleckner98cb8ba2015-07-07 22:26:07 +00001724 // Just clone the existing localrecover call, but tweak the FP argument to
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001725 // use our FP value. All other arguments are constants.
1726 auto *ParentRecover =
John McCall7f416cc2015-09-08 08:05:57 +00001727 cast<llvm::IntrinsicInst>(ParentVar.getPointer()->stripPointerCasts());
Reid Kleckner98cb8ba2015-07-07 22:26:07 +00001728 assert(ParentRecover->getIntrinsicID() == llvm::Intrinsic::localrecover &&
1729 "expected alloca or localrecover in parent LocalDeclMap");
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001730 RecoverCall = cast<llvm::CallInst>(ParentRecover->clone());
1731 RecoverCall->setArgOperand(1, ParentFP);
1732 RecoverCall->insertBefore(AllocaInsertPt);
1733 }
1734
1735 // Bitcast the variable, rename it, and insert it in the local decl map.
1736 llvm::Value *ChildVar =
John McCall7f416cc2015-09-08 08:05:57 +00001737 Builder.CreateBitCast(RecoverCall, ParentVar.getType());
1738 ChildVar->setName(ParentVar.getName());
1739 return Address(ChildVar, ParentVar.getAlignment());
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001740}
1741
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001742void CodeGenFunction::EmitCapturedLocals(CodeGenFunction &ParentCGF,
Reid Kleckner0b9bbbf2015-06-09 17:49:42 +00001743 const Stmt *OutlinedStmt,
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001744 bool IsFilter) {
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001745 // Find all captures in the Stmt.
1746 CaptureFinder Finder(ParentCGF, ParentCGF.CXXABIThisDecl);
1747 Finder.Visit(OutlinedStmt);
1748
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001749 // We can exit early on x86_64 when there are no captures. We just have to
1750 // save the exception code in filters so that __exception_code() works.
1751 if (!Finder.foundCaptures() &&
1752 CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1753 if (IsFilter)
1754 EmitSEHExceptionCodeSave(ParentCGF, nullptr, nullptr);
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001755 return;
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001756 }
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001757
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001758 llvm::Value *EntryFP = nullptr;
1759 CGBuilderTy Builder(CGM, AllocaInsertPt);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001760 if (IsFilter && CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) {
1761 // 32-bit SEH filters need to be careful about FP recovery. The end of the
1762 // EH registration is passed in as the EBP physical register. We can
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001763 // recover that with llvm.frameaddress(1).
1764 EntryFP = Builder.CreateCall(
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001765 CGM.getIntrinsic(llvm::Intrinsic::frameaddress), {Builder.getInt32(1)});
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001766 } else {
1767 // Otherwise, for x64 and 32-bit finally functions, the parent FP is the
1768 // second parameter.
1769 auto AI = CurFn->arg_begin();
1770 ++AI;
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001771 EntryFP = &*AI;
1772 }
1773
1774 llvm::Value *ParentFP = EntryFP;
1775 if (IsFilter) {
1776 // Given whatever FP the runtime provided us in EntryFP, recover the true
1777 // frame pointer of the parent function. We only need to do this in filters,
1778 // since finally funclets recover the parent FP for us.
1779 llvm::Function *RecoverFPIntrin =
1780 CGM.getIntrinsic(llvm::Intrinsic::x86_seh_recoverfp);
1781 llvm::Constant *ParentI8Fn =
1782 llvm::ConstantExpr::getBitCast(ParentCGF.CurFn, Int8PtrTy);
1783 ParentFP = Builder.CreateCall(RecoverFPIntrin, {ParentI8Fn, EntryFP});
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001784 }
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001785
Reid Kleckner98cb8ba2015-07-07 22:26:07 +00001786 // Create llvm.localrecover calls for all captures.
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001787 for (const VarDecl *VD : Finder.Captures) {
1788 if (isa<ImplicitParamDecl>(VD)) {
1789 CGM.ErrorUnsupported(VD, "'this' captured by SEH");
1790 CXXThisValue = llvm::UndefValue::get(ConvertTypeForMem(VD->getType()));
1791 continue;
1792 }
1793 if (VD->getType()->isVariablyModifiedType()) {
1794 CGM.ErrorUnsupported(VD, "VLA captured by SEH");
1795 continue;
1796 }
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001797 assert((isa<ImplicitParamDecl>(VD) || VD->isLocalVarDeclOrParm()) &&
1798 "captured non-local variable");
1799
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001800 // If this decl hasn't been declared yet, it will be declared in the
1801 // OutlinedStmt.
1802 auto I = ParentCGF.LocalDeclMap.find(VD);
1803 if (I == ParentCGF.LocalDeclMap.end())
1804 continue;
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001805
John McCall7f416cc2015-09-08 08:05:57 +00001806 Address ParentVar = I->second;
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001807 setAddrOfLocalVar(
1808 VD, recoverAddrOfEscapedLocal(ParentCGF, ParentVar, ParentFP));
Nico Webere4f974c2015-07-02 06:10:53 +00001809 }
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001810
John McCall7f416cc2015-09-08 08:05:57 +00001811 if (Finder.SEHCodeSlot.isValid()) {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001812 SEHCodeSlotStack.push_back(
1813 recoverAddrOfEscapedLocal(ParentCGF, Finder.SEHCodeSlot, ParentFP));
1814 }
1815
1816 if (IsFilter)
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001817 EmitSEHExceptionCodeSave(ParentCGF, ParentFP, EntryFP);
Reid Kleckner31a1bb02015-04-08 22:23:48 +00001818}
1819
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001820/// Arrange a function prototype that can be called by Windows exception
1821/// handling personalities. On Win64, the prototype looks like:
1822/// RetTy func(void *EHPtrs, void *ParentFP);
1823void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF,
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001824 bool IsFilter,
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001825 const Stmt *OutlinedStmt) {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001826 SourceLocation StartLoc = OutlinedStmt->getLocStart();
1827
1828 // Get the mangled function name.
1829 SmallString<128> Name;
1830 {
1831 llvm::raw_svector_ostream OS(Name);
David Majnemer25eb1652016-03-01 19:42:53 +00001832 const FunctionDecl *ParentSEHFn = ParentCGF.CurSEHParent;
1833 assert(ParentSEHFn && "No CurSEHParent!");
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001834 MangleContext &Mangler = CGM.getCXXABI().getMangleContext();
1835 if (IsFilter)
David Majnemer25eb1652016-03-01 19:42:53 +00001836 Mangler.mangleSEHFilterExpression(ParentSEHFn, OS);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001837 else
David Majnemer25eb1652016-03-01 19:42:53 +00001838 Mangler.mangleSEHFinallyBlock(ParentSEHFn, OS);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001839 }
1840
1841 FunctionArgList Args;
1842 if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 || !IsFilter) {
1843 // All SEH finally functions take two parameters. Win64 filters take two
1844 // parameters. Win32 filters take no parameters.
1845 if (IsFilter) {
1846 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00001847 getContext(), /*DC=*/nullptr, StartLoc,
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001848 &getContext().Idents.get("exception_pointers"),
Alexey Bataev56223232017-06-09 13:40:18 +00001849 getContext().VoidPtrTy, ImplicitParamDecl::Other));
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001850 } else {
1851 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00001852 getContext(), /*DC=*/nullptr, StartLoc,
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001853 &getContext().Idents.get("abnormal_termination"),
Alexey Bataev56223232017-06-09 13:40:18 +00001854 getContext().UnsignedCharTy, ImplicitParamDecl::Other));
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001855 }
1856 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00001857 getContext(), /*DC=*/nullptr, StartLoc,
1858 &getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy,
1859 ImplicitParamDecl::Other));
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001860 }
1861
1862 QualType RetTy = IsFilter ? getContext().LongTy : getContext().VoidTy;
1863
John McCallc56a8b32016-03-11 04:30:31 +00001864 const CGFunctionInfo &FnInfo =
1865 CGM.getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args);
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001866
Reid Kleckner1d59f992015-01-22 01:36:17 +00001867 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001868 llvm::Function *Fn = llvm::Function::Create(
1869 FnTy, llvm::GlobalValue::InternalLinkage, Name.str(), &CGM.getModule());
Reid Kleckner1d59f992015-01-22 01:36:17 +00001870
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001871 IsOutlinedSEHHelper = true;
Nico Weberf2a39a72015-04-13 20:03:03 +00001872
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001873 StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
1874 OutlinedStmt->getLocStart(), OutlinedStmt->getLocStart());
David Majnemer25eb1652016-03-01 19:42:53 +00001875 CurSEHParent = ParentCGF.CurSEHParent;
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001876
1877 CGM.SetLLVMFunctionAttributes(nullptr, FnInfo, CurFn);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001878 EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter);
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001879}
1880
1881/// Create a stub filter function that will ultimately hold the code of the
1882/// filter expression. The EH preparation passes in LLVM will outline the code
1883/// from the main function body into this stub.
1884llvm::Function *
1885CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
1886 const SEHExceptStmt &Except) {
1887 const Expr *FilterExpr = Except.getFilterExpr();
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001888 startOutlinedSEHHelper(ParentCGF, true, FilterExpr);
Reid Kleckner1d59f992015-01-22 01:36:17 +00001889
1890 // Emit the original filter expression, convert to i32, and return.
1891 llvm::Value *R = EmitScalarExpr(FilterExpr);
David Majnemer2ccba832015-04-17 06:57:25 +00001892 R = Builder.CreateIntCast(R, ConvertType(getContext().LongTy),
Reid Kleckner1d59f992015-01-22 01:36:17 +00001893 FilterExpr->getType()->isSignedIntegerType());
1894 Builder.CreateStore(R, ReturnValue);
1895
1896 FinishFunction(FilterExpr->getLocEnd());
1897
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001898 return CurFn;
1899}
1900
1901llvm::Function *
1902CodeGenFunction::GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF,
1903 const SEHFinallyStmt &Finally) {
1904 const Stmt *FinallyBlock = Finally.getBlock();
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001905 startOutlinedSEHHelper(ParentCGF, false, FinallyBlock);
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001906
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001907 // Emit the original filter expression, convert to i32, and return.
1908 EmitStmt(FinallyBlock);
1909
1910 FinishFunction(FinallyBlock->getLocEnd());
1911
1912 return CurFn;
Reid Kleckner1d59f992015-01-22 01:36:17 +00001913}
1914
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001915void CodeGenFunction::EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF,
1916 llvm::Value *ParentFP,
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001917 llvm::Value *EntryFP) {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001918 // Get the pointer to the EXCEPTION_POINTERS struct. This is returned by the
1919 // __exception_info intrinsic.
1920 if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
1921 // On Win64, the info is passed as the first parameter to the filter.
Duncan P. N. Exon Smith9f5260a2015-11-06 23:00:41 +00001922 SEHInfo = &*CurFn->arg_begin();
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001923 SEHCodeSlotStack.push_back(
1924 CreateMemTemp(getContext().IntTy, "__exception_code"));
1925 } else {
1926 // On Win32, the EBP on entry to the filter points to the end of an
1927 // exception registration object. It contains 6 32-bit fields, and the info
1928 // pointer is stored in the second field. So, GEP 20 bytes backwards and
1929 // load the pointer.
Reid Kleckner39329d57b2015-12-16 00:26:37 +00001930 SEHInfo = Builder.CreateConstInBoundsGEP1_32(Int8Ty, EntryFP, -20);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001931 SEHInfo = Builder.CreateBitCast(SEHInfo, Int8PtrTy->getPointerTo());
John McCall7f416cc2015-09-08 08:05:57 +00001932 SEHInfo = Builder.CreateAlignedLoad(Int8PtrTy, SEHInfo, getPointerAlign());
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001933 SEHCodeSlotStack.push_back(recoverAddrOfEscapedLocal(
1934 ParentCGF, ParentCGF.SEHCodeSlotStack.back(), ParentFP));
1935 }
1936
Reid Kleckner1d59f992015-01-22 01:36:17 +00001937 // Save the exception code in the exception slot to unify exception access in
1938 // the filter function and the landing pad.
1939 // struct EXCEPTION_POINTERS {
1940 // EXCEPTION_RECORD *ExceptionRecord;
1941 // CONTEXT *ContextRecord;
1942 // };
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001943 // int exceptioncode = exception_pointers->ExceptionRecord->ExceptionCode;
Reid Kleckner1d59f992015-01-22 01:36:17 +00001944 llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo();
Serge Guelton1d993272017-05-09 19:31:30 +00001945 llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001946 llvm::Value *Ptrs = Builder.CreateBitCast(SEHInfo, PtrsTy->getPointerTo());
David Blaikie1ed728c2015-04-05 22:45:47 +00001947 llvm::Value *Rec = Builder.CreateStructGEP(PtrsTy, Ptrs, 0);
John McCall7f416cc2015-09-08 08:05:57 +00001948 Rec = Builder.CreateAlignedLoad(Rec, getPointerAlign());
1949 llvm::Value *Code = Builder.CreateAlignedLoad(Rec, getIntAlign());
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001950 assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except");
1951 Builder.CreateStore(Code, SEHCodeSlotStack.back());
Reid Kleckner1d59f992015-01-22 01:36:17 +00001952}
1953
1954llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() {
1955 // Sema should diagnose calling this builtin outside of a filter context, but
1956 // don't crash if we screw up.
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001957 if (!SEHInfo)
Reid Kleckner1d59f992015-01-22 01:36:17 +00001958 return llvm::UndefValue::get(Int8PtrTy);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001959 assert(SEHInfo->getType() == Int8PtrTy);
1960 return SEHInfo;
Reid Kleckner1d59f992015-01-22 01:36:17 +00001961}
1962
1963llvm::Value *CodeGenFunction::EmitSEHExceptionCode() {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001964 assert(!SEHCodeSlotStack.empty() && "emitting EH code outside of __except");
John McCall7f416cc2015-09-08 08:05:57 +00001965 return Builder.CreateLoad(SEHCodeSlotStack.back());
Reid Kleckner1d59f992015-01-22 01:36:17 +00001966}
1967
Reid Kleckneraca01db2015-02-04 22:37:07 +00001968llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() {
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001969 // Abnormal termination is just the first parameter to the outlined finally
1970 // helper.
1971 auto AI = CurFn->arg_begin();
1972 return Builder.CreateZExt(&*AI, Int32Ty);
Reid Kleckneraca01db2015-02-04 22:37:07 +00001973}
1974
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001975void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) {
1976 CodeGenFunction HelperCGF(CGM, /*suppressNewContext=*/true);
1977 if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) {
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001978 // Outline the finally block.
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001979 llvm::Function *FinallyFunc =
1980 HelperCGF.GenerateSEHFinallyFunction(*this, *Finally);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001981
1982 // Push a cleanup for __finally blocks.
Reid Kleckner55391522015-10-08 21:14:56 +00001983 EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, FinallyFunc);
Reid Kleckner1d59f992015-01-22 01:36:17 +00001984 return;
1985 }
1986
1987 // Otherwise, we must have an __except block.
Reid Klecknerebaf28d2015-04-14 20:59:00 +00001988 const SEHExceptStmt *Except = S.getExceptHandler();
Reid Kleckner1d59f992015-01-22 01:36:17 +00001989 assert(Except);
1990 EHCatchScope *CatchScope = EHStack.pushCatch(1);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001991 SEHCodeSlotStack.push_back(
1992 CreateMemTemp(getContext().IntTy, "__exception_code"));
Reid Kleckner2a2e1562015-01-22 02:25:56 +00001993
Reid Kleckner9fe7f232015-07-07 00:36:30 +00001994 // If the filter is known to evaluate to 1, then we can use the clause
1995 // "catch i8* null". We can't do this on x86 because the filter has to save
1996 // the exception code.
Reid Kleckner2a2e1562015-01-22 02:25:56 +00001997 llvm::Constant *C =
John McCallde0fe072017-08-15 21:42:52 +00001998 ConstantEmitter(*this).tryEmitAbstract(Except->getFilterExpr(),
1999 getContext().IntTy);
Reid Kleckner9fe7f232015-07-07 00:36:30 +00002000 if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86 && C &&
2001 C->isOneValue()) {
Reid Kleckner2a2e1562015-01-22 02:25:56 +00002002 CatchScope->setCatchAllHandler(0, createBasicBlock("__except"));
2003 return;
2004 }
2005
2006 // In general, we have to emit an outlined filter function. Use the function
2007 // in place of the RTTI typeinfo global that C++ EH uses.
Reid Kleckner1d59f992015-01-22 01:36:17 +00002008 llvm::Function *FilterFunc =
Reid Klecknerebaf28d2015-04-14 20:59:00 +00002009 HelperCGF.GenerateSEHFilterFunction(*this, *Except);
Reid Kleckner1d59f992015-01-22 01:36:17 +00002010 llvm::Constant *OpaqueFunc =
2011 llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy);
Reid Kleckner8be18472015-09-16 21:06:09 +00002012 CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except.ret"));
Reid Kleckner1d59f992015-01-22 01:36:17 +00002013}
2014
Reid Klecknerebaf28d2015-04-14 20:59:00 +00002015void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) {
Reid Kleckner1d59f992015-01-22 01:36:17 +00002016 // Just pop the cleanup if it's a __finally block.
Reid Klecknerebaf28d2015-04-14 20:59:00 +00002017 if (S.getFinallyHandler()) {
Reid Kleckner1d59f992015-01-22 01:36:17 +00002018 PopCleanupBlock();
2019 return;
2020 }
2021
2022 // Otherwise, we must have an __except block.
Reid Kleckneraca01db2015-02-04 22:37:07 +00002023 const SEHExceptStmt *Except = S.getExceptHandler();
Reid Kleckner1d59f992015-01-22 01:36:17 +00002024 assert(Except && "__try must have __finally xor __except");
2025 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
2026
2027 // Don't emit the __except block if the __try block lacked invokes.
2028 // TODO: Model unwind edges from instructions, either with iload / istore or
2029 // a try body function.
2030 if (!CatchScope.hasEHBranches()) {
2031 CatchScope.clearHandlerBlocks();
2032 EHStack.popCatch();
Reid Kleckner9fe7f232015-07-07 00:36:30 +00002033 SEHCodeSlotStack.pop_back();
Reid Kleckner1d59f992015-01-22 01:36:17 +00002034 return;
2035 }
2036
2037 // The fall-through block.
2038 llvm::BasicBlock *ContBB = createBasicBlock("__try.cont");
2039
2040 // We just emitted the body of the __try; jump to the continue block.
2041 if (HaveInsertPoint())
2042 Builder.CreateBr(ContBB);
2043
2044 // Check if our filter function returned true.
2045 emitCatchDispatchBlock(*this, CatchScope);
2046
2047 // Grab the block before we pop the handler.
David Majnemer4e52d6f2015-12-12 05:39:21 +00002048 llvm::BasicBlock *CatchPadBB = CatchScope.getHandler(0).Block;
Reid Kleckner1d59f992015-01-22 01:36:17 +00002049 EHStack.popCatch();
2050
David Majnemer4e52d6f2015-12-12 05:39:21 +00002051 EmitBlockAfterUses(CatchPadBB);
Reid Kleckner1d59f992015-01-22 01:36:17 +00002052
Reid Kleckner129552b2015-10-08 01:13:52 +00002053 // __except blocks don't get outlined into funclets, so immediately do a
2054 // catchret.
Reid Kleckner129552b2015-10-08 01:13:52 +00002055 llvm::CatchPadInst *CPI =
2056 cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI());
David Majnemer4e52d6f2015-12-12 05:39:21 +00002057 llvm::BasicBlock *ExceptBB = createBasicBlock("__except");
Reid Kleckner129552b2015-10-08 01:13:52 +00002058 Builder.CreateCatchRet(CPI, ExceptBB);
2059 EmitBlock(ExceptBB);
2060
2061 // On Win64, the exception code is returned in EAX. Copy it into the slot.
2062 if (CGM.getTarget().getTriple().getArch() != llvm::Triple::x86) {
2063 llvm::Function *SEHCodeIntrin =
2064 CGM.getIntrinsic(llvm::Intrinsic::eh_exceptioncode);
2065 llvm::Value *Code = Builder.CreateCall(SEHCodeIntrin, {CPI});
2066 Builder.CreateStore(Code, SEHCodeSlotStack.back());
Reid Kleckner9fe7f232015-07-07 00:36:30 +00002067 }
2068
Reid Kleckner1d59f992015-01-22 01:36:17 +00002069 // Emit the __except body.
2070 EmitStmt(Except->getBlock());
2071
Reid Kleckner9fe7f232015-07-07 00:36:30 +00002072 // End the lifetime of the exception code.
2073 SEHCodeSlotStack.pop_back();
2074
Reid Kleckner3a417c32015-01-30 22:16:45 +00002075 if (HaveInsertPoint())
2076 Builder.CreateBr(ContBB);
Reid Kleckner1d59f992015-01-22 01:36:17 +00002077
2078 EmitBlock(ContBB);
Reid Kleckner543a16c2013-09-16 21:46:30 +00002079}
Nico Weber9b982072014-07-07 00:12:30 +00002080
2081void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) {
Nico Weber5779f842015-02-12 23:16:11 +00002082 // If this code is reachable then emit a stop point (if generating
2083 // debug info). We have to do this ourselves because we are on the
2084 // "simple" statement path.
2085 if (HaveInsertPoint())
2086 EmitStopPoint(&S);
2087
Reid Klecknerebaf28d2015-04-14 20:59:00 +00002088 // This must be a __leave from a __finally block, which we warn on and is UB.
2089 // Just emit unreachable.
2090 if (!isSEHTryScope()) {
2091 Builder.CreateUnreachable();
2092 Builder.ClearInsertionPoint();
2093 return;
2094 }
2095
Nico Weber5779f842015-02-12 23:16:11 +00002096 EmitBranchThroughCleanup(*SEHTryEpilogueStack.back());
Nico Weber9b982072014-07-07 00:12:30 +00002097}