blob: e7119149557b1413ff7994da3cb26845ec016960 [file] [log] [blame]
Anders Carlsson756b5c42009-10-30 01:42:31 +00001//===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ exception related code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump2bf701e2009-11-20 23:44:51 +000014#include "clang/AST/StmtCXX.h"
15
16#include "llvm/Intrinsics.h"
John McCallb2593832010-09-16 06:16:50 +000017#include "llvm/IntrinsicInst.h"
John McCallf1549f62010-07-06 01:34:17 +000018#include "llvm/Support/CallSite.h"
Mike Stump2bf701e2009-11-20 23:44:51 +000019
John McCall5a180392010-07-24 00:37:23 +000020#include "CGObjCRuntime.h"
Anders Carlsson756b5c42009-10-30 01:42:31 +000021#include "CodeGenFunction.h"
John McCallf1549f62010-07-06 01:34:17 +000022#include "CGException.h"
John McCall36f893c2011-01-28 11:13:47 +000023#include "CGCleanup.h"
John McCall204b0752010-07-20 22:17:55 +000024#include "TargetInfo.h"
John McCallf1549f62010-07-06 01:34:17 +000025
Anders Carlsson756b5c42009-10-30 01:42:31 +000026using namespace clang;
27using namespace CodeGen;
28
Anders Carlssond3379292009-10-30 02:27:02 +000029static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
30 // void *__cxa_allocate_exception(size_t thrown_size);
Mike Stump8755ec32009-12-10 00:06:18 +000031
Chris Lattner2acc6e32011-07-18 04:24:23 +000032 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +000033 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.SizeTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000034
Anders Carlssond3379292009-10-30 02:27:02 +000035 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
36}
37
Mike Stump99533832009-12-02 07:41:41 +000038static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
39 // void __cxa_free_exception(void *thrown_exception);
Mike Stump8755ec32009-12-10 00:06:18 +000040
Chris Lattner2acc6e32011-07-18 04:24:23 +000041 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +000042 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000043
Mike Stump99533832009-12-02 07:41:41 +000044 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
45}
46
Anders Carlssond3379292009-10-30 02:27:02 +000047static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump8755ec32009-12-10 00:06:18 +000048 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
Mike Stump99533832009-12-02 07:41:41 +000049 // void (*dest) (void *));
Anders Carlssond3379292009-10-30 02:27:02 +000050
John McCall61c16012011-07-10 20:11:30 +000051 llvm::Type *Args[3] = { CGF.Int8PtrTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
Chris Lattner2acc6e32011-07-18 04:24:23 +000052 llvm::FunctionType *FTy =
John McCall61c16012011-07-10 20:11:30 +000053 llvm::FunctionType::get(CGF.VoidTy, Args, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000054
Anders Carlssond3379292009-10-30 02:27:02 +000055 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
56}
57
Mike Stumpb4eea692009-11-20 00:56:31 +000058static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000059 // void __cxa_rethrow();
Mike Stumpb4eea692009-11-20 00:56:31 +000060
Chris Lattner2acc6e32011-07-18 04:24:23 +000061 llvm::FunctionType *FTy =
John McCall61c16012011-07-10 20:11:30 +000062 llvm::FunctionType::get(CGF.VoidTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000063
Mike Stumpb4eea692009-11-20 00:56:31 +000064 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
65}
66
John McCallf1549f62010-07-06 01:34:17 +000067static llvm::Constant *getGetExceptionPtrFn(CodeGenFunction &CGF) {
68 // void *__cxa_get_exception_ptr(void*);
John McCallf1549f62010-07-06 01:34:17 +000069
Chris Lattner2acc6e32011-07-18 04:24:23 +000070 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +000071 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
John McCallf1549f62010-07-06 01:34:17 +000072
73 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
74}
75
Mike Stump2bf701e2009-11-20 23:44:51 +000076static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
John McCallf1549f62010-07-06 01:34:17 +000077 // void *__cxa_begin_catch(void*);
Mike Stump2bf701e2009-11-20 23:44:51 +000078
Chris Lattner2acc6e32011-07-18 04:24:23 +000079 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +000080 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000081
Mike Stump2bf701e2009-11-20 23:44:51 +000082 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
83}
84
85static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000086 // void __cxa_end_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000087
Chris Lattner2acc6e32011-07-18 04:24:23 +000088 llvm::FunctionType *FTy =
John McCall61c16012011-07-10 20:11:30 +000089 llvm::FunctionType::get(CGF.VoidTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000090
Mike Stump2bf701e2009-11-20 23:44:51 +000091 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
92}
93
Mike Stumpcce3d4f2009-12-07 23:38:24 +000094static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
95 // void __cxa_call_unexepcted(void *thrown_exception);
96
Chris Lattner2acc6e32011-07-18 04:24:23 +000097 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +000098 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +000099
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000100 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
101}
102
John McCall93c332a2011-05-28 21:13:02 +0000103llvm::Constant *CodeGenFunction::getUnwindResumeFn() {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000104 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +0000105 llvm::FunctionType::get(VoidTy, Int8PtrTy, /*IsVarArgs=*/false);
John McCall93c332a2011-05-28 21:13:02 +0000106
107 if (CGM.getLangOptions().SjLjExceptions)
108 return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
109 return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume");
110}
111
112llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000113 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +0000114 llvm::FunctionType::get(VoidTy, Int8PtrTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +0000115
Douglas Gregor86a3a032010-05-16 01:24:12 +0000116 if (CGM.getLangOptions().SjLjExceptions)
John McCalla5f2de22010-08-11 20:59:53 +0000117 return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume_or_Rethrow");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000118 return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
Mike Stump0f590be2009-12-01 03:41:18 +0000119}
120
Mike Stump99533832009-12-02 07:41:41 +0000121static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
122 // void __terminate();
123
Chris Lattner2acc6e32011-07-18 04:24:23 +0000124 llvm::FunctionType *FTy =
John McCall61c16012011-07-10 20:11:30 +0000125 llvm::FunctionType::get(CGF.VoidTy, /*IsVarArgs=*/false);
Mike Stump8755ec32009-12-10 00:06:18 +0000126
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 StringRef name;
John McCall256a76e2011-07-06 01:22:26 +0000128
129 // In C++, use std::terminate().
130 if (CGF.getLangOptions().CPlusPlus)
131 name = "_ZSt9terminatev"; // FIXME: mangling!
132 else if (CGF.getLangOptions().ObjC1 &&
133 CGF.CGM.getCodeGenOpts().ObjCRuntimeHasTerminate)
134 name = "objc_terminate";
135 else
136 name = "abort";
137 return CGF.CGM.CreateRuntimeFunction(FTy, name);
David Chisnall79a9ad82010-05-17 13:49:20 +0000138}
139
John McCall8262b6a2010-07-17 00:43:08 +0000140static llvm::Constant *getCatchallRethrowFn(CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 StringRef Name) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000142 llvm::FunctionType *FTy =
Jay Foadda549e82011-07-29 13:56:53 +0000143 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, /*IsVarArgs=*/false);
John McCall8262b6a2010-07-17 00:43:08 +0000144
145 return CGF.CGM.CreateRuntimeFunction(FTy, Name);
John McCallf1549f62010-07-06 01:34:17 +0000146}
147
John McCall8262b6a2010-07-17 00:43:08 +0000148const EHPersonality EHPersonality::GNU_C("__gcc_personality_v0");
John McCall44680782010-11-07 02:35:25 +0000149const EHPersonality EHPersonality::GNU_C_SJLJ("__gcc_personality_sj0");
John McCall8262b6a2010-07-17 00:43:08 +0000150const EHPersonality EHPersonality::NeXT_ObjC("__objc_personality_v0");
151const EHPersonality EHPersonality::GNU_CPlusPlus("__gxx_personality_v0");
152const EHPersonality EHPersonality::GNU_CPlusPlus_SJLJ("__gxx_personality_sj0");
153const EHPersonality EHPersonality::GNU_ObjC("__gnu_objc_personality_v0",
154 "objc_exception_throw");
David Chisnall80558d22011-03-20 21:35:39 +0000155const EHPersonality EHPersonality::GNU_ObjCXX("__gnustep_objcxx_personality_v0");
John McCall8262b6a2010-07-17 00:43:08 +0000156
157static const EHPersonality &getCPersonality(const LangOptions &L) {
John McCall44680782010-11-07 02:35:25 +0000158 if (L.SjLjExceptions)
159 return EHPersonality::GNU_C_SJLJ;
John McCall8262b6a2010-07-17 00:43:08 +0000160 return EHPersonality::GNU_C;
161}
162
163static const EHPersonality &getObjCPersonality(const LangOptions &L) {
164 if (L.NeXTRuntime) {
165 if (L.ObjCNonFragileABI) return EHPersonality::NeXT_ObjC;
166 else return getCPersonality(L);
John McCallf1549f62010-07-06 01:34:17 +0000167 } else {
John McCall8262b6a2010-07-17 00:43:08 +0000168 return EHPersonality::GNU_ObjC;
John McCallf1549f62010-07-06 01:34:17 +0000169 }
170}
171
John McCall8262b6a2010-07-17 00:43:08 +0000172static const EHPersonality &getCXXPersonality(const LangOptions &L) {
173 if (L.SjLjExceptions)
174 return EHPersonality::GNU_CPlusPlus_SJLJ;
John McCallf1549f62010-07-06 01:34:17 +0000175 else
John McCall8262b6a2010-07-17 00:43:08 +0000176 return EHPersonality::GNU_CPlusPlus;
John McCallf1549f62010-07-06 01:34:17 +0000177}
178
179/// Determines the personality function to use when both C++
180/// and Objective-C exceptions are being caught.
John McCall8262b6a2010-07-17 00:43:08 +0000181static const EHPersonality &getObjCXXPersonality(const LangOptions &L) {
John McCallf1549f62010-07-06 01:34:17 +0000182 // The ObjC personality defers to the C++ personality for non-ObjC
183 // handlers. Unlike the C++ case, we use the same personality
184 // function on targets using (backend-driven) SJLJ EH.
John McCall8262b6a2010-07-17 00:43:08 +0000185 if (L.NeXTRuntime) {
186 if (L.ObjCNonFragileABI)
187 return EHPersonality::NeXT_ObjC;
John McCallf1549f62010-07-06 01:34:17 +0000188
189 // In the fragile ABI, just use C++ exception handling and hope
190 // they're not doing crazy exception mixing.
191 else
John McCall8262b6a2010-07-17 00:43:08 +0000192 return getCXXPersonality(L);
Chandler Carruthdcf22ad2010-05-17 20:58:49 +0000193 }
David Chisnall79a9ad82010-05-17 13:49:20 +0000194
John McCall8262b6a2010-07-17 00:43:08 +0000195 // The GNU runtime's personality function inherently doesn't support
196 // mixed EH. Use the C++ personality just to avoid returning null.
David Chisnall80558d22011-03-20 21:35:39 +0000197 return EHPersonality::GNU_ObjCXX;
John McCallf1549f62010-07-06 01:34:17 +0000198}
199
John McCall8262b6a2010-07-17 00:43:08 +0000200const EHPersonality &EHPersonality::get(const LangOptions &L) {
201 if (L.CPlusPlus && L.ObjC1)
202 return getObjCXXPersonality(L);
203 else if (L.CPlusPlus)
204 return getCXXPersonality(L);
205 else if (L.ObjC1)
206 return getObjCPersonality(L);
John McCallf1549f62010-07-06 01:34:17 +0000207 else
John McCall8262b6a2010-07-17 00:43:08 +0000208 return getCPersonality(L);
209}
John McCallf1549f62010-07-06 01:34:17 +0000210
John McCallb2593832010-09-16 06:16:50 +0000211static llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
John McCall8262b6a2010-07-17 00:43:08 +0000212 const EHPersonality &Personality) {
John McCall8262b6a2010-07-17 00:43:08 +0000213 llvm::Constant *Fn =
John McCallb2593832010-09-16 06:16:50 +0000214 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
215 llvm::Type::getInt32Ty(CGM.getLLVMContext()),
216 true),
217 Personality.getPersonalityFnName());
218 return Fn;
219}
220
221static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
222 const EHPersonality &Personality) {
223 llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
John McCalld16c2cf2011-02-08 08:22:06 +0000224 return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
John McCallb2593832010-09-16 06:16:50 +0000225}
226
227/// Check whether a personality function could reasonably be swapped
228/// for a C++ personality function.
229static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
230 for (llvm::Constant::use_iterator
231 I = Fn->use_begin(), E = Fn->use_end(); I != E; ++I) {
232 llvm::User *User = *I;
233
234 // Conditionally white-list bitcasts.
235 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(User)) {
236 if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
237 if (!PersonalityHasOnlyCXXUses(CE))
238 return false;
239 continue;
240 }
241
242 // Otherwise, it has to be a selector call.
243 if (!isa<llvm::EHSelectorInst>(User)) return false;
244
245 llvm::EHSelectorInst *Selector = cast<llvm::EHSelectorInst>(User);
246 for (unsigned I = 2, E = Selector->getNumArgOperands(); I != E; ++I) {
247 // Look for something that would've been returned by the ObjC
248 // runtime's GetEHType() method.
249 llvm::GlobalVariable *GV
250 = dyn_cast<llvm::GlobalVariable>(Selector->getArgOperand(I));
251 if (!GV) continue;
252
253 // ObjC EH selector entries are always global variables with
254 // names starting like this.
255 if (GV->getName().startswith("OBJC_EHTYPE"))
256 return false;
257 }
258 }
259
260 return true;
261}
262
263/// Try to use the C++ personality function in ObjC++. Not doing this
264/// can cause some incompatibilities with gcc, which is more
265/// aggressive about only using the ObjC++ personality in a function
266/// when it really needs it.
267void CodeGenModule::SimplifyPersonality() {
268 // For now, this is really a Darwin-specific operation.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000269 if (!Context.getTargetInfo().getTriple().isOSDarwin())
John McCallb2593832010-09-16 06:16:50 +0000270 return;
271
272 // If we're not in ObjC++ -fexceptions, there's nothing to do.
273 if (!Features.CPlusPlus || !Features.ObjC1 || !Features.Exceptions)
274 return;
275
276 const EHPersonality &ObjCXX = EHPersonality::get(Features);
277 const EHPersonality &CXX = getCXXPersonality(Features);
278 if (&ObjCXX == &CXX ||
279 ObjCXX.getPersonalityFnName() == CXX.getPersonalityFnName())
280 return;
281
282 llvm::Function *Fn =
283 getModule().getFunction(ObjCXX.getPersonalityFnName());
284
285 // Nothing to do if it's unused.
286 if (!Fn || Fn->use_empty()) return;
287
288 // Can't do the optimization if it has non-C++ uses.
289 if (!PersonalityHasOnlyCXXUses(Fn)) return;
290
291 // Create the C++ personality function and kill off the old
292 // function.
293 llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
294
295 // This can happen if the user is screwing with us.
296 if (Fn->getType() != CXXFn->getType()) return;
297
298 Fn->replaceAllUsesWith(CXXFn);
299 Fn->eraseFromParent();
John McCallf1549f62010-07-06 01:34:17 +0000300}
301
302/// Returns the value to inject into a selector to indicate the
303/// presence of a catch-all.
304static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
305 // Possibly we should use @llvm.eh.catch.all.value here.
John McCalld16c2cf2011-02-08 08:22:06 +0000306 return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
John McCallf1549f62010-07-06 01:34:17 +0000307}
308
309/// Returns the value to inject into a selector to indicate the
310/// presence of a cleanup.
311static llvm::Constant *getCleanupValue(CodeGenFunction &CGF) {
312 return llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
Mike Stump99533832009-12-02 07:41:41 +0000313}
314
John McCall09faeab2010-07-13 21:17:51 +0000315namespace {
316 /// A cleanup to free the exception object if its initialization
317 /// throws.
John McCallc4a1a842011-07-12 00:15:30 +0000318 struct FreeException : EHScopeStack::Cleanup {
319 llvm::Value *exn;
320 FreeException(llvm::Value *exn) : exn(exn) {}
John McCallad346f42011-07-12 20:27:29 +0000321 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall3ad32c82011-01-28 08:37:24 +0000322 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), exn)
John McCall09faeab2010-07-13 21:17:51 +0000323 ->setDoesNotThrow();
John McCall09faeab2010-07-13 21:17:51 +0000324 }
325 };
326}
327
John McCallac418162010-04-22 01:10:34 +0000328// Emits an exception expression into the given location. This
329// differs from EmitAnyExprToMem only in that, if a final copy-ctor
330// call is required, an exception within that copy ctor causes
331// std::terminate to be invoked.
John McCall3ad32c82011-01-28 08:37:24 +0000332static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e,
333 llvm::Value *addr) {
John McCallf1549f62010-07-06 01:34:17 +0000334 // Make sure the exception object is cleaned up if there's an
335 // exception during initialization.
John McCall3ad32c82011-01-28 08:37:24 +0000336 CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr);
337 EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin();
John McCallac418162010-04-22 01:10:34 +0000338
339 // __cxa_allocate_exception returns a void*; we need to cast this
340 // to the appropriate type for the object.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000341 llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo();
John McCall3ad32c82011-01-28 08:37:24 +0000342 llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty);
John McCallac418162010-04-22 01:10:34 +0000343
344 // FIXME: this isn't quite right! If there's a final unelided call
345 // to a copy constructor, then according to [except.terminate]p1 we
346 // must call std::terminate() if that constructor throws, because
347 // technically that copy occurs after the exception expression is
348 // evaluated but before the exception is caught. But the best way
349 // to handle that is to teach EmitAggExpr to do the final copy
350 // differently if it can't be elided.
John McCallf85e1932011-06-15 23:02:42 +0000351 CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
352 /*IsInit*/ true);
John McCallac418162010-04-22 01:10:34 +0000353
John McCall3ad32c82011-01-28 08:37:24 +0000354 // Deactivate the cleanup block.
355 CGF.DeactivateCleanupBlock(cleanup);
Mike Stump0f590be2009-12-01 03:41:18 +0000356}
357
John McCallf1549f62010-07-06 01:34:17 +0000358llvm::Value *CodeGenFunction::getExceptionSlot() {
John McCall93c332a2011-05-28 21:13:02 +0000359 if (!ExceptionSlot)
360 ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
John McCallf1549f62010-07-06 01:34:17 +0000361 return ExceptionSlot;
Mike Stump0f590be2009-12-01 03:41:18 +0000362}
363
John McCall93c332a2011-05-28 21:13:02 +0000364llvm::Value *CodeGenFunction::getEHSelectorSlot() {
365 if (!EHSelectorSlot)
366 EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
367 return EHSelectorSlot;
368}
369
Bill Wendlingae270592011-09-15 18:57:19 +0000370llvm::Value *CodeGenFunction::getExceptionFromSlot() {
371 return Builder.CreateLoad(getExceptionSlot(), "exn");
372}
373
374llvm::Value *CodeGenFunction::getSelectorFromSlot() {
375 return Builder.CreateLoad(getEHSelectorSlot(), "sel");
376}
377
Anders Carlsson756b5c42009-10-30 01:42:31 +0000378void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000379 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000380 if (getInvokeDest()) {
John McCallf1549f62010-07-06 01:34:17 +0000381 Builder.CreateInvoke(getReThrowFn(*this),
382 getUnreachableBlock(),
383 getInvokeDest())
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000384 ->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000385 } else {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000386 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000387 Builder.CreateUnreachable();
388 }
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000389
John McCallcd5b22e2011-01-12 03:41:02 +0000390 // throw is an expression, and the expression emitters expect us
391 // to leave ourselves at a valid insertion point.
392 EmitBlock(createBasicBlock("throw.cont"));
393
Anders Carlssond3379292009-10-30 02:27:02 +0000394 return;
395 }
Mike Stump8755ec32009-12-10 00:06:18 +0000396
Anders Carlssond3379292009-10-30 02:27:02 +0000397 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000398
Anders Carlssond3379292009-10-30 02:27:02 +0000399 // Now allocate the exception object.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000400 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000401 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000402
Anders Carlssond3379292009-10-30 02:27:02 +0000403 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
John McCallf1549f62010-07-06 01:34:17 +0000404 llvm::CallInst *ExceptionPtr =
Mike Stump8755ec32009-12-10 00:06:18 +0000405 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000406 llvm::ConstantInt::get(SizeTy, TypeSize),
407 "exception");
John McCallf1549f62010-07-06 01:34:17 +0000408 ExceptionPtr->setDoesNotThrow();
Anders Carlsson8370c582009-12-11 00:32:37 +0000409
John McCallac418162010-04-22 01:10:34 +0000410 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000411
Anders Carlssond3379292009-10-30 02:27:02 +0000412 // Now throw the exception.
Anders Carlsson82a113a2011-01-24 01:59:49 +0000413 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
414 /*ForEH=*/true);
John McCallac418162010-04-22 01:10:34 +0000415
416 // The address of the destructor. If the exception type has a
417 // trivial destructor (or isn't a record), we just pass null.
418 llvm::Constant *Dtor = 0;
419 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
420 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
421 if (!Record->hasTrivialDestructor()) {
Douglas Gregor1d110e02010-07-01 14:13:13 +0000422 CXXDestructorDecl *DtorD = Record->getDestructor();
John McCallac418162010-04-22 01:10:34 +0000423 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
424 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
425 }
426 }
427 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000428
Mike Stump0a3816e2009-12-04 01:51:45 +0000429 if (getInvokeDest()) {
Mike Stump8755ec32009-12-10 00:06:18 +0000430 llvm::InvokeInst *ThrowCall =
John McCallf1549f62010-07-06 01:34:17 +0000431 Builder.CreateInvoke3(getThrowFn(*this),
432 getUnreachableBlock(), getInvokeDest(),
Mike Stump0a3816e2009-12-04 01:51:45 +0000433 ExceptionPtr, TypeInfo, Dtor);
434 ThrowCall->setDoesNotReturn();
Mike Stump0a3816e2009-12-04 01:51:45 +0000435 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000436 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000437 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
438 ThrowCall->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000439 Builder.CreateUnreachable();
Mike Stump0a3816e2009-12-04 01:51:45 +0000440 }
Mike Stump8755ec32009-12-10 00:06:18 +0000441
John McCallcd5b22e2011-01-12 03:41:02 +0000442 // throw is an expression, and the expression emitters expect us
443 // to leave ourselves at a valid insertion point.
444 EmitBlock(createBasicBlock("throw.cont"));
Anders Carlsson756b5c42009-10-30 01:42:31 +0000445}
Mike Stump2bf701e2009-11-20 23:44:51 +0000446
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000447void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlsson15348ae2011-02-28 02:27:16 +0000448 if (!CGM.getLangOptions().CXXExceptions)
Anders Carlssona994ee42010-02-06 23:59:05 +0000449 return;
450
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000451 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
452 if (FD == 0)
453 return;
454 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
455 if (Proto == 0)
456 return;
457
Sebastian Redla968e972011-03-15 18:42:48 +0000458 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
459 if (isNoexceptExceptionSpec(EST)) {
460 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
461 // noexcept functions are simple terminate scopes.
462 EHStack.pushTerminate();
463 }
464 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
465 unsigned NumExceptions = Proto->getNumExceptions();
466 EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000467
Sebastian Redla968e972011-03-15 18:42:48 +0000468 for (unsigned I = 0; I != NumExceptions; ++I) {
469 QualType Ty = Proto->getExceptionType(I);
470 QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
471 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
472 /*ForEH=*/true);
473 Filter->setFilter(I, EHType);
474 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000475 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000476}
477
John McCall777d6e52011-08-11 02:22:43 +0000478/// Emit the dispatch block for a filter scope if necessary.
479static void emitFilterDispatchBlock(CodeGenFunction &CGF,
480 EHFilterScope &filterScope) {
481 llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
482 if (!dispatchBlock) return;
483 if (dispatchBlock->use_empty()) {
484 delete dispatchBlock;
485 return;
486 }
487
John McCall777d6e52011-08-11 02:22:43 +0000488 CGF.EmitBlockAfterUses(dispatchBlock);
489
490 // If this isn't a catch-all filter, we need to check whether we got
491 // here because the filter triggered.
492 if (filterScope.getNumFilters()) {
493 // Load the selector value.
Bill Wendlingae270592011-09-15 18:57:19 +0000494 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall777d6e52011-08-11 02:22:43 +0000495 llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
496
497 llvm::Value *zero = CGF.Builder.getInt32(0);
498 llvm::Value *failsFilter =
499 CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
500 CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, CGF.getEHResumeBlock());
501
502 CGF.EmitBlock(unexpectedBB);
503 }
504
505 // Call __cxa_call_unexpected. This doesn't need to be an invoke
506 // because __cxa_call_unexpected magically filters exceptions
507 // according to the last landing pad the exception was thrown
508 // into. Seriously.
Bill Wendlingae270592011-09-15 18:57:19 +0000509 llvm::Value *exn = CGF.getExceptionFromSlot();
John McCall777d6e52011-08-11 02:22:43 +0000510 CGF.Builder.CreateCall(getUnexpectedFn(CGF), exn)
511 ->setDoesNotReturn();
512 CGF.Builder.CreateUnreachable();
513}
514
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000515void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlsson15348ae2011-02-28 02:27:16 +0000516 if (!CGM.getLangOptions().CXXExceptions)
Anders Carlssona994ee42010-02-06 23:59:05 +0000517 return;
518
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000519 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
520 if (FD == 0)
521 return;
522 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
523 if (Proto == 0)
524 return;
525
Sebastian Redla968e972011-03-15 18:42:48 +0000526 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
527 if (isNoexceptExceptionSpec(EST)) {
528 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
529 EHStack.popTerminate();
530 }
531 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
John McCall777d6e52011-08-11 02:22:43 +0000532 EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
533 emitFilterDispatchBlock(*this, filterScope);
Sebastian Redla968e972011-03-15 18:42:48 +0000534 EHStack.popFilter();
535 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000536}
537
Mike Stump2bf701e2009-11-20 23:44:51 +0000538void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall59a70002010-07-07 06:56:46 +0000539 EnterCXXTryStmt(S);
John McCall9fc6a772010-02-19 09:25:03 +0000540 EmitStmt(S.getTryBlock());
John McCall59a70002010-07-07 06:56:46 +0000541 ExitCXXTryStmt(S);
John McCall9fc6a772010-02-19 09:25:03 +0000542}
543
John McCall59a70002010-07-07 06:56:46 +0000544void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallf1549f62010-07-06 01:34:17 +0000545 unsigned NumHandlers = S.getNumHandlers();
546 EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
John McCall9fc6a772010-02-19 09:25:03 +0000547
John McCallf1549f62010-07-06 01:34:17 +0000548 for (unsigned I = 0; I != NumHandlers; ++I) {
549 const CXXCatchStmt *C = S.getHandler(I);
John McCall9fc6a772010-02-19 09:25:03 +0000550
John McCallf1549f62010-07-06 01:34:17 +0000551 llvm::BasicBlock *Handler = createBasicBlock("catch");
552 if (C->getExceptionDecl()) {
553 // FIXME: Dropping the reference type on the type into makes it
554 // impossible to correctly implement catch-by-reference
555 // semantics for pointers. Unfortunately, this is what all
556 // existing compilers do, and it's not clear that the standard
557 // personality routine is capable of doing this right. See C++ DR 388:
558 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
559 QualType CaughtType = C->getCaughtType();
560 CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType();
John McCall5a180392010-07-24 00:37:23 +0000561
562 llvm::Value *TypeInfo = 0;
563 if (CaughtType->isObjCObjectPointerType())
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +0000564 TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
John McCall5a180392010-07-24 00:37:23 +0000565 else
Anders Carlsson82a113a2011-01-24 01:59:49 +0000566 TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true);
John McCallf1549f62010-07-06 01:34:17 +0000567 CatchScope->setHandler(I, TypeInfo, Handler);
568 } else {
569 // No exception decl indicates '...', a catch-all.
570 CatchScope->setCatchAllHandler(I, Handler);
571 }
572 }
John McCallf1549f62010-07-06 01:34:17 +0000573}
574
John McCall777d6e52011-08-11 02:22:43 +0000575llvm::BasicBlock *
576CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
577 // The dispatch block for the end of the scope chain is a block that
578 // just resumes unwinding.
579 if (si == EHStack.stable_end())
580 return getEHResumeBlock();
581
582 // Otherwise, we should look at the actual scope.
583 EHScope &scope = *EHStack.find(si);
584
585 llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
586 if (!dispatchBlock) {
587 switch (scope.getKind()) {
588 case EHScope::Catch: {
589 // Apply a special case to a single catch-all.
590 EHCatchScope &catchScope = cast<EHCatchScope>(scope);
591 if (catchScope.getNumHandlers() == 1 &&
592 catchScope.getHandler(0).isCatchAll()) {
593 dispatchBlock = catchScope.getHandler(0).Block;
594
595 // Otherwise, make a dispatch block.
596 } else {
597 dispatchBlock = createBasicBlock("catch.dispatch");
598 }
599 break;
600 }
601
602 case EHScope::Cleanup:
603 dispatchBlock = createBasicBlock("ehcleanup");
604 break;
605
606 case EHScope::Filter:
607 dispatchBlock = createBasicBlock("filter.dispatch");
608 break;
609
610 case EHScope::Terminate:
611 dispatchBlock = getTerminateHandler();
612 break;
613 }
614 scope.setCachedEHDispatchBlock(dispatchBlock);
615 }
616 return dispatchBlock;
617}
618
John McCallf1549f62010-07-06 01:34:17 +0000619/// Check whether this is a non-EH scope, i.e. a scope which doesn't
620/// affect exception handling. Currently, the only non-EH scopes are
621/// normal-only cleanup scopes.
622static bool isNonEHScope(const EHScope &S) {
John McCallda65ea82010-07-13 20:32:21 +0000623 switch (S.getKind()) {
John McCall1f0fca52010-07-21 07:22:38 +0000624 case EHScope::Cleanup:
625 return !cast<EHCleanupScope>(S).isEHCleanup();
John McCallda65ea82010-07-13 20:32:21 +0000626 case EHScope::Filter:
627 case EHScope::Catch:
628 case EHScope::Terminate:
629 return false;
630 }
631
632 // Suppress warning.
633 return false;
John McCallf1549f62010-07-06 01:34:17 +0000634}
635
636llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
637 assert(EHStack.requiresLandingPad());
638 assert(!EHStack.empty());
639
Anders Carlsson7a178512011-02-28 00:33:03 +0000640 if (!CGM.getLangOptions().Exceptions)
John McCallda65ea82010-07-13 20:32:21 +0000641 return 0;
642
John McCallf1549f62010-07-06 01:34:17 +0000643 // Check the innermost scope for a cached landing pad. If this is
644 // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
645 llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
646 if (LP) return LP;
647
648 // Build the landing pad for this scope.
649 LP = EmitLandingPad();
650 assert(LP);
651
652 // Cache the landing pad on the innermost scope. If this is a
653 // non-EH scope, cache the landing pad on the enclosing scope, too.
654 for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
655 ir->setCachedLandingPad(LP);
656 if (!isNonEHScope(*ir)) break;
657 }
658
659 return LP;
660}
661
John McCall93c332a2011-05-28 21:13:02 +0000662// This code contains a hack to work around a design flaw in
663// LLVM's EH IR which breaks semantics after inlining. This same
664// hack is implemented in llvm-gcc.
665//
666// The LLVM EH abstraction is basically a thin veneer over the
667// traditional GCC zero-cost design: for each range of instructions
668// in the function, there is (at most) one "landing pad" with an
669// associated chain of EH actions. A language-specific personality
670// function interprets this chain of actions and (1) decides whether
671// or not to resume execution at the landing pad and (2) if so,
672// provides an integer indicating why it's stopping. In LLVM IR,
673// the association of a landing pad with a range of instructions is
674// achieved via an invoke instruction, the chain of actions becomes
675// the arguments to the @llvm.eh.selector call, and the selector
676// call returns the integer indicator. Other than the required
677// presence of two intrinsic function calls in the landing pad,
678// the IR exactly describes the layout of the output code.
679//
680// A principal advantage of this design is that it is completely
681// language-agnostic; in theory, the LLVM optimizers can treat
682// landing pads neutrally, and targets need only know how to lower
683// the intrinsics to have a functioning exceptions system (assuming
684// that platform exceptions follow something approximately like the
685// GCC design). Unfortunately, landing pads cannot be combined in a
686// language-agnostic way: given selectors A and B, there is no way
687// to make a single landing pad which faithfully represents the
688// semantics of propagating an exception first through A, then
689// through B, without knowing how the personality will interpret the
690// (lowered form of the) selectors. This means that inlining has no
691// choice but to crudely chain invokes (i.e., to ignore invokes in
692// the inlined function, but to turn all unwindable calls into
693// invokes), which is only semantically valid if every unwind stops
694// at every landing pad.
695//
696// Therefore, the invoke-inline hack is to guarantee that every
697// landing pad has a catch-all.
698enum CleanupHackLevel_t {
699 /// A level of hack that requires that all landing pads have
700 /// catch-alls.
701 CHL_MandatoryCatchall,
702
703 /// A level of hack that requires that all landing pads handle
704 /// cleanups.
705 CHL_MandatoryCleanup,
706
707 /// No hacks at all; ideal IR generation.
708 CHL_Ideal
709};
710const CleanupHackLevel_t CleanupHackLevel = CHL_MandatoryCleanup;
711
John McCallf1549f62010-07-06 01:34:17 +0000712llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
713 assert(EHStack.requiresLandingPad());
714
John McCall777d6e52011-08-11 02:22:43 +0000715 EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
716 switch (innermostEHScope.getKind()) {
717 case EHScope::Terminate:
718 return getTerminateLandingPad();
John McCallf1549f62010-07-06 01:34:17 +0000719
John McCall777d6e52011-08-11 02:22:43 +0000720 case EHScope::Catch:
721 case EHScope::Cleanup:
722 case EHScope::Filter:
723 if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
724 return lpad;
John McCallf1549f62010-07-06 01:34:17 +0000725 }
726
727 // Save the current IR generation state.
John McCall777d6e52011-08-11 02:22:43 +0000728 CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
John McCallf1549f62010-07-06 01:34:17 +0000729
John McCall777d6e52011-08-11 02:22:43 +0000730 const EHPersonality &personality = EHPersonality::get(getLangOptions());
John McCall8262b6a2010-07-17 00:43:08 +0000731
John McCallf1549f62010-07-06 01:34:17 +0000732 // Create and configure the landing pad.
John McCall777d6e52011-08-11 02:22:43 +0000733 llvm::BasicBlock *lpad = createBasicBlock("lpad");
734 EmitBlock(lpad);
John McCallf1549f62010-07-06 01:34:17 +0000735
736 // Save the exception pointer. It's safe to use a single exception
737 // pointer per function because EH cleanups can never have nested
738 // try/catches.
John McCall777d6e52011-08-11 02:22:43 +0000739 llvm::CallInst *exn =
John McCallf1549f62010-07-06 01:34:17 +0000740 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_exception), "exn");
John McCall777d6e52011-08-11 02:22:43 +0000741 exn->setDoesNotThrow();
John McCallf1549f62010-07-06 01:34:17 +0000742
743 // Build the selector arguments.
John McCall777d6e52011-08-11 02:22:43 +0000744 SmallVector<llvm::Value*, 8> selector;
745 selector.push_back(exn);
746 selector.push_back(getOpaquePersonalityFn(CGM, personality));
John McCallf1549f62010-07-06 01:34:17 +0000747
748 // Accumulate all the handlers in scope.
John McCall777d6e52011-08-11 02:22:43 +0000749 bool hasCatchAll = false;
750 bool hasCleanup = false;
751 bool hasFilter = false;
752 SmallVector<llvm::Value*, 4> filterTypes;
753 llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
John McCallf1549f62010-07-06 01:34:17 +0000754 for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end();
755 I != E; ++I) {
756
757 switch (I->getKind()) {
John McCall1f0fca52010-07-21 07:22:38 +0000758 case EHScope::Cleanup:
John McCall777d6e52011-08-11 02:22:43 +0000759 // If we have a cleanup, remember that.
760 hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
John McCallda65ea82010-07-13 20:32:21 +0000761 continue;
762
John McCallf1549f62010-07-06 01:34:17 +0000763 case EHScope::Filter: {
764 assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
John McCall777d6e52011-08-11 02:22:43 +0000765 assert(!hasCatchAll && "EH filter reached after catch-all");
John McCallf1549f62010-07-06 01:34:17 +0000766
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000767 // Filter scopes get added to the selector in weird ways.
John McCall777d6e52011-08-11 02:22:43 +0000768 EHFilterScope &filter = cast<EHFilterScope>(*I);
769 hasFilter = true;
John McCallf1549f62010-07-06 01:34:17 +0000770
771 // Add all the filter values which we aren't already explicitly
772 // catching.
John McCall777d6e52011-08-11 02:22:43 +0000773 for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) {
774 llvm::Value *filterType = filter.getFilter(i);
775 if (!catchTypes.count(filterType))
776 filterTypes.push_back(filterType);
John McCallf1549f62010-07-06 01:34:17 +0000777 }
778 goto done;
779 }
780
781 case EHScope::Terminate:
782 // Terminate scopes are basically catch-alls.
John McCall777d6e52011-08-11 02:22:43 +0000783 assert(!hasCatchAll);
784 hasCatchAll = true;
John McCallf1549f62010-07-06 01:34:17 +0000785 goto done;
786
787 case EHScope::Catch:
788 break;
789 }
790
John McCall777d6e52011-08-11 02:22:43 +0000791 EHCatchScope &catchScope = cast<EHCatchScope>(*I);
792 for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
793 EHCatchScope::Handler handler = catchScope.getHandler(hi);
John McCallf1549f62010-07-06 01:34:17 +0000794
John McCall777d6e52011-08-11 02:22:43 +0000795 // If this is a catch-all, register that and abort.
796 if (!handler.Type) {
797 assert(!hasCatchAll);
798 hasCatchAll = true;
799 goto done;
John McCallf1549f62010-07-06 01:34:17 +0000800 }
801
802 // Check whether we already have a handler for this type.
John McCall777d6e52011-08-11 02:22:43 +0000803 if (catchTypes.insert(handler.Type)) {
804 // If not, add it directly to the selector.
805 selector.push_back(handler.Type);
806 }
John McCallf1549f62010-07-06 01:34:17 +0000807 }
John McCallf1549f62010-07-06 01:34:17 +0000808 }
809
810 done:
John McCallf1549f62010-07-06 01:34:17 +0000811 // If we have a catch-all, add null to the selector.
John McCall777d6e52011-08-11 02:22:43 +0000812 assert(!(hasCatchAll && hasFilter));
813 if (hasCatchAll) {
814 selector.push_back(getCatchAllValue(*this));
John McCallf1549f62010-07-06 01:34:17 +0000815
816 // If we have an EH filter, we need to add those handlers in the
817 // right place in the selector, which is to say, at the end.
John McCall777d6e52011-08-11 02:22:43 +0000818 } else if (hasFilter) {
John McCallf1549f62010-07-06 01:34:17 +0000819 // Create a filter expression: an integer constant saying how many
820 // filters there are (+1 to avoid ambiguity with 0 for cleanup),
821 // followed by the filter types. The personality routine only
822 // lands here if the filter doesn't match.
John McCall777d6e52011-08-11 02:22:43 +0000823 selector.push_back(Builder.getInt32(filterTypes.size() + 1));
824 selector.append(filterTypes.begin(), filterTypes.end());
John McCallf1549f62010-07-06 01:34:17 +0000825
826 // Also check whether we need a cleanup.
John McCall777d6e52011-08-11 02:22:43 +0000827 if (CleanupHackLevel == CHL_MandatoryCatchall || hasCleanup)
828 selector.push_back(CleanupHackLevel == CHL_MandatoryCatchall
John McCalld16c2cf2011-02-08 08:22:06 +0000829 ? getCatchAllValue(*this)
830 : getCleanupValue(*this));
John McCallf1549f62010-07-06 01:34:17 +0000831
832 // Otherwise, signal that we at least have cleanups.
John McCall777d6e52011-08-11 02:22:43 +0000833 } else if (CleanupHackLevel == CHL_MandatoryCatchall || hasCleanup) {
834 selector.push_back(CleanupHackLevel == CHL_MandatoryCatchall
John McCalld16c2cf2011-02-08 08:22:06 +0000835 ? getCatchAllValue(*this)
836 : getCleanupValue(*this));
John McCallf1549f62010-07-06 01:34:17 +0000837 }
838
John McCall777d6e52011-08-11 02:22:43 +0000839 assert(selector.size() >= 3 && "selector call has only two arguments!");
John McCallf1549f62010-07-06 01:34:17 +0000840
841 // Tell the backend how to generate the landing pad.
John McCall777d6e52011-08-11 02:22:43 +0000842 llvm::CallInst *selectorCall =
John McCallf1549f62010-07-06 01:34:17 +0000843 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_selector),
John McCall777d6e52011-08-11 02:22:43 +0000844 selector, "eh.selector");
845 selectorCall->setDoesNotThrow();
John McCall93c332a2011-05-28 21:13:02 +0000846
John McCall777d6e52011-08-11 02:22:43 +0000847 // Save the selector and exception pointer.
848 Builder.CreateStore(exn, getExceptionSlot());
849 Builder.CreateStore(selectorCall, getEHSelectorSlot());
John McCallf1549f62010-07-06 01:34:17 +0000850
John McCall777d6e52011-08-11 02:22:43 +0000851 Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
John McCallf1549f62010-07-06 01:34:17 +0000852
853 // Restore the old IR generation state.
John McCall777d6e52011-08-11 02:22:43 +0000854 Builder.restoreIP(savedIP);
John McCallf1549f62010-07-06 01:34:17 +0000855
John McCall777d6e52011-08-11 02:22:43 +0000856 return lpad;
John McCallf1549f62010-07-06 01:34:17 +0000857}
858
John McCall8e3f8612010-07-13 22:12:14 +0000859namespace {
860 /// A cleanup to call __cxa_end_catch. In many cases, the caught
861 /// exception type lets us state definitively that the thrown exception
862 /// type does not have a destructor. In particular:
863 /// - Catch-alls tell us nothing, so we have to conservatively
864 /// assume that the thrown exception might have a destructor.
865 /// - Catches by reference behave according to their base types.
866 /// - Catches of non-record types will only trigger for exceptions
867 /// of non-record types, which never have destructors.
868 /// - Catches of record types can trigger for arbitrary subclasses
869 /// of the caught type, so we have to assume the actual thrown
870 /// exception type might have a throwing destructor, even if the
871 /// caught type's destructor is trivial or nothrow.
John McCall1f0fca52010-07-21 07:22:38 +0000872 struct CallEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +0000873 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
874 bool MightThrow;
875
John McCallad346f42011-07-12 20:27:29 +0000876 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall8e3f8612010-07-13 22:12:14 +0000877 if (!MightThrow) {
878 CGF.Builder.CreateCall(getEndCatchFn(CGF))->setDoesNotThrow();
879 return;
880 }
881
Jay Foad4c7d9f12011-07-15 08:37:34 +0000882 CGF.EmitCallOrInvoke(getEndCatchFn(CGF));
John McCall8e3f8612010-07-13 22:12:14 +0000883 }
884 };
885}
886
John McCallf1549f62010-07-06 01:34:17 +0000887/// Emits a call to __cxa_begin_catch and enters a cleanup to call
888/// __cxa_end_catch.
John McCall8e3f8612010-07-13 22:12:14 +0000889///
890/// \param EndMightThrow - true if __cxa_end_catch might throw
891static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
892 llvm::Value *Exn,
893 bool EndMightThrow) {
John McCallf1549f62010-07-06 01:34:17 +0000894 llvm::CallInst *Call = CGF.Builder.CreateCall(getBeginCatchFn(CGF), Exn);
895 Call->setDoesNotThrow();
896
John McCall1f0fca52010-07-21 07:22:38 +0000897 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
John McCallf1549f62010-07-06 01:34:17 +0000898
899 return Call;
900}
901
902/// A "special initializer" callback for initializing a catch
903/// parameter during catch initialization.
904static void InitCatchParam(CodeGenFunction &CGF,
905 const VarDecl &CatchParam,
906 llvm::Value *ParamAddr) {
907 // Load the exception from where the landing pad saved it.
Bill Wendlingae270592011-09-15 18:57:19 +0000908 llvm::Value *Exn = CGF.getExceptionFromSlot();
John McCallf1549f62010-07-06 01:34:17 +0000909
910 CanQualType CatchType =
911 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000912 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
John McCallf1549f62010-07-06 01:34:17 +0000913
914 // If we're catching by reference, we can just cast the object
915 // pointer to the appropriate pointer.
916 if (isa<ReferenceType>(CatchType)) {
John McCall204b0752010-07-20 22:17:55 +0000917 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
918 bool EndCatchMightThrow = CaughtType->isRecordType();
John McCall8e3f8612010-07-13 22:12:14 +0000919
John McCallf1549f62010-07-06 01:34:17 +0000920 // __cxa_begin_catch returns the adjusted object pointer.
John McCall8e3f8612010-07-13 22:12:14 +0000921 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
John McCall204b0752010-07-20 22:17:55 +0000922
923 // We have no way to tell the personality function that we're
924 // catching by reference, so if we're catching a pointer,
925 // __cxa_begin_catch will actually return that pointer by value.
926 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
927 QualType PointeeType = PT->getPointeeType();
928
929 // When catching by reference, generally we should just ignore
930 // this by-value pointer and use the exception object instead.
931 if (!PointeeType->isRecordType()) {
932
933 // Exn points to the struct _Unwind_Exception header, which
934 // we have to skip past in order to reach the exception data.
935 unsigned HeaderSize =
936 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
937 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
938
939 // However, if we're catching a pointer-to-record type that won't
940 // work, because the personality function might have adjusted
941 // the pointer. There's actually no way for us to fully satisfy
942 // the language/ABI contract here: we can't use Exn because it
943 // might have the wrong adjustment, but we can't use the by-value
944 // pointer because it's off by a level of abstraction.
945 //
946 // The current solution is to dump the adjusted pointer into an
947 // alloca, which breaks language semantics (because changing the
948 // pointer doesn't change the exception) but at least works.
949 // The better solution would be to filter out non-exact matches
950 // and rethrow them, but this is tricky because the rethrow
951 // really needs to be catchable by other sites at this landing
952 // pad. The best solution is to fix the personality function.
953 } else {
954 // Pull the pointer for the reference type off.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000955 llvm::Type *PtrTy =
John McCall204b0752010-07-20 22:17:55 +0000956 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
957
958 // Create the temporary and write the adjusted pointer into it.
959 llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
960 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
961 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
962
963 // Bind the reference to the temporary.
964 AdjustedExn = ExnPtrTmp;
965 }
966 }
967
John McCallf1549f62010-07-06 01:34:17 +0000968 llvm::Value *ExnCast =
969 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
970 CGF.Builder.CreateStore(ExnCast, ParamAddr);
971 return;
972 }
973
974 // Non-aggregates (plus complexes).
975 bool IsComplex = false;
976 if (!CGF.hasAggregateLLVMType(CatchType) ||
977 (IsComplex = CatchType->isAnyComplexType())) {
John McCall8e3f8612010-07-13 22:12:14 +0000978 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
John McCallf1549f62010-07-06 01:34:17 +0000979
980 // If the catch type is a pointer type, __cxa_begin_catch returns
981 // the pointer by value.
982 if (CatchType->hasPointerRepresentation()) {
983 llvm::Value *CastExn =
984 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
985 CGF.Builder.CreateStore(CastExn, ParamAddr);
986 return;
987 }
988
989 // Otherwise, it returns a pointer into the exception object.
990
Chris Lattner2acc6e32011-07-18 04:24:23 +0000991 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
John McCallf1549f62010-07-06 01:34:17 +0000992 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
993
994 if (IsComplex) {
995 CGF.StoreComplexToAddr(CGF.LoadComplexFromAddr(Cast, /*volatile*/ false),
996 ParamAddr, /*volatile*/ false);
997 } else {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000998 unsigned Alignment =
999 CGF.getContext().getDeclAlign(&CatchParam).getQuantity();
John McCallf1549f62010-07-06 01:34:17 +00001000 llvm::Value *ExnLoad = CGF.Builder.CreateLoad(Cast, "exn.scalar");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001001 CGF.EmitStoreOfScalar(ExnLoad, ParamAddr, /*volatile*/ false, Alignment,
1002 CatchType);
John McCallf1549f62010-07-06 01:34:17 +00001003 }
1004 return;
1005 }
1006
John McCallacff6962011-02-16 08:39:19 +00001007 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
John McCallf1549f62010-07-06 01:34:17 +00001008
Chris Lattner2acc6e32011-07-18 04:24:23 +00001009 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
John McCallf1549f62010-07-06 01:34:17 +00001010
John McCallacff6962011-02-16 08:39:19 +00001011 // Check for a copy expression. If we don't have a copy expression,
1012 // that means a trivial copy is okay.
John McCalle996ffd2011-02-16 08:02:54 +00001013 const Expr *copyExpr = CatchParam.getInit();
1014 if (!copyExpr) {
John McCallacff6962011-02-16 08:39:19 +00001015 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
1016 llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1017 CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
John McCallf1549f62010-07-06 01:34:17 +00001018 return;
1019 }
1020
1021 // We have to call __cxa_get_exception_ptr to get the adjusted
1022 // pointer before copying.
John McCalle996ffd2011-02-16 08:02:54 +00001023 llvm::CallInst *rawAdjustedExn =
John McCallf1549f62010-07-06 01:34:17 +00001024 CGF.Builder.CreateCall(getGetExceptionPtrFn(CGF), Exn);
John McCalle996ffd2011-02-16 08:02:54 +00001025 rawAdjustedExn->setDoesNotThrow();
John McCallf1549f62010-07-06 01:34:17 +00001026
John McCalle996ffd2011-02-16 08:02:54 +00001027 // Cast that to the appropriate type.
1028 llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
John McCallf1549f62010-07-06 01:34:17 +00001029
John McCalle996ffd2011-02-16 08:02:54 +00001030 // The copy expression is defined in terms of an OpaqueValueExpr.
1031 // Find it and map it to the adjusted expression.
1032 CodeGenFunction::OpaqueValueMapping
John McCall56ca35d2011-02-17 10:25:35 +00001033 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
1034 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
John McCallf1549f62010-07-06 01:34:17 +00001035
1036 // Call the copy ctor in a terminate scope.
1037 CGF.EHStack.pushTerminate();
John McCalle996ffd2011-02-16 08:02:54 +00001038
1039 // Perform the copy construction.
John McCall7c2349b2011-08-25 20:40:09 +00001040 CGF.EmitAggExpr(copyExpr, AggValueSlot::forAddr(ParamAddr, Qualifiers(),
1041 AggValueSlot::IsNotDestructed,
John McCall13668622011-08-26 00:46:38 +00001042 AggValueSlot::DoesNotNeedGCBarriers,
1043 AggValueSlot::IsNotAliased));
John McCalle996ffd2011-02-16 08:02:54 +00001044
1045 // Leave the terminate scope.
John McCallf1549f62010-07-06 01:34:17 +00001046 CGF.EHStack.popTerminate();
1047
John McCalle996ffd2011-02-16 08:02:54 +00001048 // Undo the opaque value mapping.
1049 opaque.pop();
1050
John McCallf1549f62010-07-06 01:34:17 +00001051 // Finally we can call __cxa_begin_catch.
John McCall8e3f8612010-07-13 22:12:14 +00001052 CallBeginCatch(CGF, Exn, true);
John McCallf1549f62010-07-06 01:34:17 +00001053}
1054
1055/// Begins a catch statement by initializing the catch variable and
1056/// calling __cxa_begin_catch.
John McCalle996ffd2011-02-16 08:02:54 +00001057static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
John McCallf1549f62010-07-06 01:34:17 +00001058 // We have to be very careful with the ordering of cleanups here:
1059 // C++ [except.throw]p4:
1060 // The destruction [of the exception temporary] occurs
1061 // immediately after the destruction of the object declared in
1062 // the exception-declaration in the handler.
1063 //
1064 // So the precise ordering is:
1065 // 1. Construct catch variable.
1066 // 2. __cxa_begin_catch
1067 // 3. Enter __cxa_end_catch cleanup
1068 // 4. Enter dtor cleanup
1069 //
John McCall34695852011-02-22 06:44:22 +00001070 // We do this by using a slightly abnormal initialization process.
1071 // Delegation sequence:
John McCallf1549f62010-07-06 01:34:17 +00001072 // - ExitCXXTryStmt opens a RunCleanupsScope
John McCall34695852011-02-22 06:44:22 +00001073 // - EmitAutoVarAlloca creates the variable and debug info
John McCallf1549f62010-07-06 01:34:17 +00001074 // - InitCatchParam initializes the variable from the exception
John McCall34695852011-02-22 06:44:22 +00001075 // - CallBeginCatch calls __cxa_begin_catch
1076 // - CallBeginCatch enters the __cxa_end_catch cleanup
1077 // - EmitAutoVarCleanups enters the variable destructor cleanup
John McCallf1549f62010-07-06 01:34:17 +00001078 // - EmitCXXTryStmt emits the code for the catch body
1079 // - EmitCXXTryStmt close the RunCleanupsScope
1080
1081 VarDecl *CatchParam = S->getExceptionDecl();
1082 if (!CatchParam) {
Bill Wendlingae270592011-09-15 18:57:19 +00001083 llvm::Value *Exn = CGF.getExceptionFromSlot();
John McCall8e3f8612010-07-13 22:12:14 +00001084 CallBeginCatch(CGF, Exn, true);
John McCallf1549f62010-07-06 01:34:17 +00001085 return;
1086 }
1087
1088 // Emit the local.
John McCall34695852011-02-22 06:44:22 +00001089 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
1090 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF));
1091 CGF.EmitAutoVarCleanups(var);
John McCall9fc6a772010-02-19 09:25:03 +00001092}
1093
John McCallfcd5c0c2010-07-13 22:24:23 +00001094namespace {
John McCall1f0fca52010-07-21 07:22:38 +00001095 struct CallRethrow : EHScopeStack::Cleanup {
John McCallad346f42011-07-12 20:27:29 +00001096 void Emit(CodeGenFunction &CGF, Flags flags) {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001097 CGF.EmitCallOrInvoke(getReThrowFn(CGF));
John McCallfcd5c0c2010-07-13 22:24:23 +00001098 }
1099 };
1100}
1101
John McCall777d6e52011-08-11 02:22:43 +00001102/// Emit the structure of the dispatch block for the given catch scope.
1103/// It is an invariant that the dispatch block already exists.
1104static void emitCatchDispatchBlock(CodeGenFunction &CGF,
1105 EHCatchScope &catchScope) {
1106 llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1107 assert(dispatchBlock);
1108
1109 // If there's only a single catch-all, getEHDispatchBlock returned
1110 // that catch-all as the dispatch block.
1111 if (catchScope.getNumHandlers() == 1 &&
1112 catchScope.getHandler(0).isCatchAll()) {
1113 assert(dispatchBlock == catchScope.getHandler(0).Block);
1114 return;
1115 }
1116
1117 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1118 CGF.EmitBlockAfterUses(dispatchBlock);
1119
1120 // Select the right handler.
1121 llvm::Value *llvm_eh_typeid_for =
1122 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1123
1124 // Load the selector value.
Bill Wendlingae270592011-09-15 18:57:19 +00001125 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall777d6e52011-08-11 02:22:43 +00001126
1127 // Test against each of the exception types we claim to catch.
1128 for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1129 assert(i < e && "ran off end of handlers!");
1130 const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1131
1132 llvm::Value *typeValue = handler.Type;
1133 assert(typeValue && "fell into catch-all case!");
1134 typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1135
1136 // Figure out the next block.
1137 bool nextIsEnd;
1138 llvm::BasicBlock *nextBlock;
1139
1140 // If this is the last handler, we're at the end, and the next
1141 // block is the block for the enclosing EH scope.
1142 if (i + 1 == e) {
1143 nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1144 nextIsEnd = true;
1145
1146 // If the next handler is a catch-all, we're at the end, and the
1147 // next block is that handler.
1148 } else if (catchScope.getHandler(i+1).isCatchAll()) {
1149 nextBlock = catchScope.getHandler(i+1).Block;
1150 nextIsEnd = true;
1151
1152 // Otherwise, we're not at the end and we need a new block.
1153 } else {
1154 nextBlock = CGF.createBasicBlock("catch.fallthrough");
1155 nextIsEnd = false;
1156 }
1157
1158 // Figure out the catch type's index in the LSDA's type table.
1159 llvm::CallInst *typeIndex =
1160 CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1161 typeIndex->setDoesNotThrow();
1162
1163 llvm::Value *matchesTypeIndex =
1164 CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1165 CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1166
1167 // If the next handler is a catch-all, we're completely done.
1168 if (nextIsEnd) {
1169 CGF.Builder.restoreIP(savedIP);
1170 return;
1171
1172 // Otherwise we need to emit and continue at that block.
1173 } else {
1174 CGF.EmitBlock(nextBlock);
1175 }
1176 }
1177
1178 llvm_unreachable("fell out of loop!");
1179}
1180
1181void CodeGenFunction::popCatchScope() {
1182 EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1183 if (catchScope.hasEHBranches())
1184 emitCatchDispatchBlock(*this, catchScope);
1185 EHStack.popCatch();
1186}
1187
John McCall59a70002010-07-07 06:56:46 +00001188void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallf1549f62010-07-06 01:34:17 +00001189 unsigned NumHandlers = S.getNumHandlers();
1190 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1191 assert(CatchScope.getNumHandlers() == NumHandlers);
Mike Stump2bf701e2009-11-20 23:44:51 +00001192
John McCall777d6e52011-08-11 02:22:43 +00001193 // If the catch was not required, bail out now.
1194 if (!CatchScope.hasEHBranches()) {
1195 EHStack.popCatch();
1196 return;
1197 }
1198
1199 // Emit the structure of the EH dispatch for this catch.
1200 emitCatchDispatchBlock(*this, CatchScope);
1201
John McCallf1549f62010-07-06 01:34:17 +00001202 // Copy the handler blocks off before we pop the EH stack. Emitting
1203 // the handlers might scribble on this memory.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001204 SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers);
John McCallf1549f62010-07-06 01:34:17 +00001205 memcpy(Handlers.data(), CatchScope.begin(),
1206 NumHandlers * sizeof(EHCatchScope::Handler));
John McCall777d6e52011-08-11 02:22:43 +00001207
John McCallf1549f62010-07-06 01:34:17 +00001208 EHStack.popCatch();
Mike Stump2bf701e2009-11-20 23:44:51 +00001209
John McCallf1549f62010-07-06 01:34:17 +00001210 // The fall-through block.
1211 llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
Mike Stump2bf701e2009-11-20 23:44:51 +00001212
John McCallf1549f62010-07-06 01:34:17 +00001213 // We just emitted the body of the try; jump to the continue block.
1214 if (HaveInsertPoint())
1215 Builder.CreateBr(ContBB);
Mike Stump639787c2009-12-02 19:53:57 +00001216
John McCall59a70002010-07-07 06:56:46 +00001217 // Determine if we need an implicit rethrow for all these catch handlers.
1218 bool ImplicitRethrow = false;
1219 if (IsFnTryBlock)
1220 ImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1221 isa<CXXConstructorDecl>(CurCodeDecl);
1222
John McCall777d6e52011-08-11 02:22:43 +00001223 // Perversely, we emit the handlers backwards precisely because we
1224 // want them to appear in source order. In all of these cases, the
1225 // catch block will have exactly one predecessor, which will be a
1226 // particular block in the catch dispatch. However, in the case of
1227 // a catch-all, one of the dispatch blocks will branch to two
1228 // different handlers, and EmitBlockAfterUses will cause the second
1229 // handler to be moved before the first.
1230 for (unsigned I = NumHandlers; I != 0; --I) {
1231 llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1232 EmitBlockAfterUses(CatchBlock);
Mike Stump8755ec32009-12-10 00:06:18 +00001233
John McCallf1549f62010-07-06 01:34:17 +00001234 // Catch the exception if this isn't a catch-all.
John McCall777d6e52011-08-11 02:22:43 +00001235 const CXXCatchStmt *C = S.getHandler(I-1);
Mike Stump2bf701e2009-11-20 23:44:51 +00001236
John McCallf1549f62010-07-06 01:34:17 +00001237 // Enter a cleanup scope, including the catch variable and the
1238 // end-catch.
1239 RunCleanupsScope CatchScope(*this);
Mike Stump2bf701e2009-11-20 23:44:51 +00001240
John McCallf1549f62010-07-06 01:34:17 +00001241 // Initialize the catch variable and set up the cleanups.
1242 BeginCatch(*this, C);
1243
John McCall59a70002010-07-07 06:56:46 +00001244 // If there's an implicit rethrow, push a normal "cleanup" to call
John McCallfcd5c0c2010-07-13 22:24:23 +00001245 // _cxa_rethrow. This needs to happen before __cxa_end_catch is
1246 // called, and so it is pushed after BeginCatch.
1247 if (ImplicitRethrow)
John McCall1f0fca52010-07-21 07:22:38 +00001248 EHStack.pushCleanup<CallRethrow>(NormalCleanup);
John McCall59a70002010-07-07 06:56:46 +00001249
John McCallf1549f62010-07-06 01:34:17 +00001250 // Perform the body of the catch.
1251 EmitStmt(C->getHandlerBlock());
1252
1253 // Fall out through the catch cleanups.
1254 CatchScope.ForceCleanup();
1255
1256 // Branch out of the try.
1257 if (HaveInsertPoint())
1258 Builder.CreateBr(ContBB);
Mike Stump2bf701e2009-11-20 23:44:51 +00001259 }
1260
John McCallf1549f62010-07-06 01:34:17 +00001261 EmitBlock(ContBB);
Mike Stump2bf701e2009-11-20 23:44:51 +00001262}
Mike Stumpd88ea562009-12-09 03:35:49 +00001263
John McCall55b20fc2010-07-21 00:52:03 +00001264namespace {
John McCall1f0fca52010-07-21 07:22:38 +00001265 struct CallEndCatchForFinally : EHScopeStack::Cleanup {
John McCall55b20fc2010-07-21 00:52:03 +00001266 llvm::Value *ForEHVar;
1267 llvm::Value *EndCatchFn;
1268 CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1269 : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1270
John McCallad346f42011-07-12 20:27:29 +00001271 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall55b20fc2010-07-21 00:52:03 +00001272 llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1273 llvm::BasicBlock *CleanupContBB =
1274 CGF.createBasicBlock("finally.cleanup.cont");
1275
1276 llvm::Value *ShouldEndCatch =
1277 CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch");
1278 CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1279 CGF.EmitBlock(EndCatchBB);
Jay Foad4c7d9f12011-07-15 08:37:34 +00001280 CGF.EmitCallOrInvoke(EndCatchFn); // catch-all, so might throw
John McCall55b20fc2010-07-21 00:52:03 +00001281 CGF.EmitBlock(CleanupContBB);
1282 }
1283 };
John McCall77199712010-07-21 05:47:49 +00001284
John McCall1f0fca52010-07-21 07:22:38 +00001285 struct PerformFinally : EHScopeStack::Cleanup {
John McCall77199712010-07-21 05:47:49 +00001286 const Stmt *Body;
1287 llvm::Value *ForEHVar;
1288 llvm::Value *EndCatchFn;
1289 llvm::Value *RethrowFn;
1290 llvm::Value *SavedExnVar;
1291
1292 PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1293 llvm::Value *EndCatchFn,
1294 llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1295 : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1296 RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1297
John McCallad346f42011-07-12 20:27:29 +00001298 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall77199712010-07-21 05:47:49 +00001299 // Enter a cleanup to call the end-catch function if one was provided.
1300 if (EndCatchFn)
John McCall1f0fca52010-07-21 07:22:38 +00001301 CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1302 ForEHVar, EndCatchFn);
John McCall77199712010-07-21 05:47:49 +00001303
John McCalld96a8e72010-08-11 00:16:14 +00001304 // Save the current cleanup destination in case there are
1305 // cleanups in the finally block.
1306 llvm::Value *SavedCleanupDest =
1307 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1308 "cleanup.dest.saved");
1309
John McCall77199712010-07-21 05:47:49 +00001310 // Emit the finally block.
1311 CGF.EmitStmt(Body);
1312
1313 // If the end of the finally is reachable, check whether this was
1314 // for EH. If so, rethrow.
1315 if (CGF.HaveInsertPoint()) {
1316 llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1317 llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1318
1319 llvm::Value *ShouldRethrow =
1320 CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow");
1321 CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1322
1323 CGF.EmitBlock(RethrowBB);
1324 if (SavedExnVar) {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001325 CGF.EmitCallOrInvoke(RethrowFn, CGF.Builder.CreateLoad(SavedExnVar));
John McCall77199712010-07-21 05:47:49 +00001326 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001327 CGF.EmitCallOrInvoke(RethrowFn);
John McCall77199712010-07-21 05:47:49 +00001328 }
1329 CGF.Builder.CreateUnreachable();
1330
1331 CGF.EmitBlock(ContBB);
John McCalld96a8e72010-08-11 00:16:14 +00001332
1333 // Restore the cleanup destination.
1334 CGF.Builder.CreateStore(SavedCleanupDest,
1335 CGF.getNormalCleanupDestSlot());
John McCall77199712010-07-21 05:47:49 +00001336 }
1337
1338 // Leave the end-catch cleanup. As an optimization, pretend that
1339 // the fallthrough path was inaccessible; we've dynamically proven
1340 // that we're not in the EH case along that path.
1341 if (EndCatchFn) {
1342 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1343 CGF.PopCleanupBlock();
1344 CGF.Builder.restoreIP(SavedIP);
1345 }
1346
1347 // Now make sure we actually have an insertion point or the
1348 // cleanup gods will hate us.
1349 CGF.EnsureInsertPoint();
1350 }
1351 };
John McCall55b20fc2010-07-21 00:52:03 +00001352}
1353
John McCallf1549f62010-07-06 01:34:17 +00001354/// Enters a finally block for an implementation using zero-cost
1355/// exceptions. This is mostly general, but hard-codes some
1356/// language/ABI-specific behavior in the catch-all sections.
John McCalld768e9d2011-06-22 02:32:12 +00001357void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1358 const Stmt *body,
1359 llvm::Constant *beginCatchFn,
1360 llvm::Constant *endCatchFn,
1361 llvm::Constant *rethrowFn) {
1362 assert((beginCatchFn != 0) == (endCatchFn != 0) &&
John McCallf1549f62010-07-06 01:34:17 +00001363 "begin/end catch functions not paired");
John McCalld768e9d2011-06-22 02:32:12 +00001364 assert(rethrowFn && "rethrow function is required");
1365
1366 BeginCatchFn = beginCatchFn;
Mike Stumpd88ea562009-12-09 03:35:49 +00001367
John McCallf1549f62010-07-06 01:34:17 +00001368 // The rethrow function has one of the following two types:
1369 // void (*)()
1370 // void (*)(void*)
1371 // In the latter case we need to pass it the exception object.
1372 // But we can't use the exception slot because the @finally might
1373 // have a landing pad (which would overwrite the exception slot).
Chris Lattner2acc6e32011-07-18 04:24:23 +00001374 llvm::FunctionType *rethrowFnTy =
John McCallf1549f62010-07-06 01:34:17 +00001375 cast<llvm::FunctionType>(
John McCalld768e9d2011-06-22 02:32:12 +00001376 cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1377 SavedExnVar = 0;
1378 if (rethrowFnTy->getNumParams())
1379 SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
Mike Stumpd88ea562009-12-09 03:35:49 +00001380
John McCallf1549f62010-07-06 01:34:17 +00001381 // A finally block is a statement which must be executed on any edge
1382 // out of a given scope. Unlike a cleanup, the finally block may
1383 // contain arbitrary control flow leading out of itself. In
1384 // addition, finally blocks should always be executed, even if there
1385 // are no catch handlers higher on the stack. Therefore, we
1386 // surround the protected scope with a combination of a normal
1387 // cleanup (to catch attempts to break out of the block via normal
1388 // control flow) and an EH catch-all (semantically "outside" any try
1389 // statement to which the finally block might have been attached).
1390 // The finally block itself is generated in the context of a cleanup
1391 // which conditionally leaves the catch-all.
John McCall3d3ec1c2010-04-21 10:05:39 +00001392
John McCallf1549f62010-07-06 01:34:17 +00001393 // Jump destination for performing the finally block on an exception
1394 // edge. We'll never actually reach this block, so unreachable is
1395 // fine.
John McCalld768e9d2011-06-22 02:32:12 +00001396 RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
John McCall3d3ec1c2010-04-21 10:05:39 +00001397
John McCallf1549f62010-07-06 01:34:17 +00001398 // Whether the finally block is being executed for EH purposes.
John McCalld768e9d2011-06-22 02:32:12 +00001399 ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1400 CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar);
Mike Stumpd88ea562009-12-09 03:35:49 +00001401
John McCallf1549f62010-07-06 01:34:17 +00001402 // Enter a normal cleanup which will perform the @finally block.
John McCalld768e9d2011-06-22 02:32:12 +00001403 CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1404 ForEHVar, endCatchFn,
1405 rethrowFn, SavedExnVar);
John McCallf1549f62010-07-06 01:34:17 +00001406
1407 // Enter a catch-all scope.
John McCalld768e9d2011-06-22 02:32:12 +00001408 llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1409 EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1410 catchScope->setCatchAllHandler(0, catchBB);
John McCallf1549f62010-07-06 01:34:17 +00001411}
1412
John McCalld768e9d2011-06-22 02:32:12 +00001413void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
John McCallf1549f62010-07-06 01:34:17 +00001414 // Leave the finally catch-all.
John McCalld768e9d2011-06-22 02:32:12 +00001415 EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1416 llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
John McCall777d6e52011-08-11 02:22:43 +00001417
1418 CGF.popCatchScope();
John McCallf1549f62010-07-06 01:34:17 +00001419
John McCalld768e9d2011-06-22 02:32:12 +00001420 // If there are any references to the catch-all block, emit it.
1421 if (catchBB->use_empty()) {
1422 delete catchBB;
1423 } else {
1424 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1425 CGF.EmitBlock(catchBB);
John McCallf1549f62010-07-06 01:34:17 +00001426
John McCalld768e9d2011-06-22 02:32:12 +00001427 llvm::Value *exn = 0;
John McCallf1549f62010-07-06 01:34:17 +00001428
John McCalld768e9d2011-06-22 02:32:12 +00001429 // If there's a begin-catch function, call it.
1430 if (BeginCatchFn) {
Bill Wendlingae270592011-09-15 18:57:19 +00001431 exn = CGF.getExceptionFromSlot();
John McCalld768e9d2011-06-22 02:32:12 +00001432 CGF.Builder.CreateCall(BeginCatchFn, exn)->setDoesNotThrow();
1433 }
1434
1435 // If we need to remember the exception pointer to rethrow later, do so.
1436 if (SavedExnVar) {
Bill Wendlingae270592011-09-15 18:57:19 +00001437 if (!exn) exn = CGF.getExceptionFromSlot();
John McCalld768e9d2011-06-22 02:32:12 +00001438 CGF.Builder.CreateStore(exn, SavedExnVar);
1439 }
1440
1441 // Tell the cleanups in the finally block that we're do this for EH.
1442 CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar);
1443
1444 // Thread a jump through the finally cleanup.
1445 CGF.EmitBranchThroughCleanup(RethrowDest);
1446
1447 CGF.Builder.restoreIP(savedIP);
1448 }
1449
1450 // Finally, leave the @finally cleanup.
1451 CGF.PopCleanupBlock();
John McCallf1549f62010-07-06 01:34:17 +00001452}
1453
1454llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1455 if (TerminateLandingPad)
1456 return TerminateLandingPad;
1457
1458 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1459
1460 // This will get inserted at the end of the function.
1461 TerminateLandingPad = createBasicBlock("terminate.lpad");
1462 Builder.SetInsertPoint(TerminateLandingPad);
1463
1464 // Tell the backend that this is a landing pad.
1465 llvm::CallInst *Exn =
1466 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_exception), "exn");
1467 Exn->setDoesNotThrow();
John McCall8262b6a2010-07-17 00:43:08 +00001468
1469 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions());
John McCallf1549f62010-07-06 01:34:17 +00001470
1471 // Tell the backend what the exception table should be:
1472 // nothing but a catch-all.
John McCallb2593832010-09-16 06:16:50 +00001473 llvm::Value *Args[3] = { Exn, getOpaquePersonalityFn(CGM, Personality),
John McCallf1549f62010-07-06 01:34:17 +00001474 getCatchAllValue(*this) };
1475 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_selector),
Jay Foad4c7d9f12011-07-15 08:37:34 +00001476 Args, "eh.selector")
John McCallf1549f62010-07-06 01:34:17 +00001477 ->setDoesNotThrow();
1478
1479 llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this));
1480 TerminateCall->setDoesNotReturn();
1481 TerminateCall->setDoesNotThrow();
John McCalld16c2cf2011-02-08 08:22:06 +00001482 Builder.CreateUnreachable();
Mike Stumpd88ea562009-12-09 03:35:49 +00001483
John McCallf1549f62010-07-06 01:34:17 +00001484 // Restore the saved insertion state.
1485 Builder.restoreIP(SavedIP);
John McCall891f80e2010-04-30 00:06:43 +00001486
John McCallf1549f62010-07-06 01:34:17 +00001487 return TerminateLandingPad;
Mike Stumpd88ea562009-12-09 03:35:49 +00001488}
Mike Stump9b39c512009-12-09 22:59:31 +00001489
1490llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +00001491 if (TerminateHandler)
1492 return TerminateHandler;
1493
John McCallf1549f62010-07-06 01:34:17 +00001494 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
Mike Stump76958092009-12-09 23:31:35 +00001495
John McCallf1549f62010-07-06 01:34:17 +00001496 // Set up the terminate handler. This block is inserted at the very
1497 // end of the function by FinishFunction.
Mike Stump182f3832009-12-10 00:02:42 +00001498 TerminateHandler = createBasicBlock("terminate.handler");
John McCallf1549f62010-07-06 01:34:17 +00001499 Builder.SetInsertPoint(TerminateHandler);
1500 llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this));
Mike Stump9b39c512009-12-09 22:59:31 +00001501 TerminateCall->setDoesNotReturn();
1502 TerminateCall->setDoesNotThrow();
1503 Builder.CreateUnreachable();
1504
John McCall3d3ec1c2010-04-21 10:05:39 +00001505 // Restore the saved insertion state.
John McCallf1549f62010-07-06 01:34:17 +00001506 Builder.restoreIP(SavedIP);
Mike Stump76958092009-12-09 23:31:35 +00001507
Mike Stump9b39c512009-12-09 22:59:31 +00001508 return TerminateHandler;
1509}
John McCallf1549f62010-07-06 01:34:17 +00001510
John McCall777d6e52011-08-11 02:22:43 +00001511llvm::BasicBlock *CodeGenFunction::getEHResumeBlock() {
1512 if (EHResumeBlock) return EHResumeBlock;
John McCallff8e1152010-07-23 21:56:41 +00001513
1514 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1515
1516 // We emit a jump to a notional label at the outermost unwind state.
John McCall777d6e52011-08-11 02:22:43 +00001517 EHResumeBlock = createBasicBlock("eh.resume");
1518 Builder.SetInsertPoint(EHResumeBlock);
John McCallff8e1152010-07-23 21:56:41 +00001519
1520 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions());
1521
1522 // This can always be a call because we necessarily didn't find
1523 // anything on the EH stack which needs our help.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001524 StringRef RethrowName = Personality.getCatchallRethrowFnName();
John McCall93c332a2011-05-28 21:13:02 +00001525 if (!RethrowName.empty()) {
1526 Builder.CreateCall(getCatchallRethrowFn(*this, RethrowName),
Bill Wendlingae270592011-09-15 18:57:19 +00001527 getExceptionFromSlot())
John McCall93c332a2011-05-28 21:13:02 +00001528 ->setDoesNotReturn();
1529 } else {
Bill Wendlingae270592011-09-15 18:57:19 +00001530 llvm::Value *Exn = getExceptionFromSlot();
John McCallff8e1152010-07-23 21:56:41 +00001531
John McCall93c332a2011-05-28 21:13:02 +00001532 switch (CleanupHackLevel) {
1533 case CHL_MandatoryCatchall:
1534 // In mandatory-catchall mode, we need to use
1535 // _Unwind_Resume_or_Rethrow, or whatever the personality's
1536 // equivalent is.
1537 Builder.CreateCall(getUnwindResumeOrRethrowFn(), Exn)
1538 ->setDoesNotReturn();
1539 break;
1540 case CHL_MandatoryCleanup: {
1541 // In mandatory-cleanup mode, we should use llvm.eh.resume.
Bill Wendlingae270592011-09-15 18:57:19 +00001542 llvm::Value *Selector = getSelectorFromSlot();
John McCall93c332a2011-05-28 21:13:02 +00001543 Builder.CreateCall2(CGM.getIntrinsic(llvm::Intrinsic::eh_resume),
1544 Exn, Selector)
1545 ->setDoesNotReturn();
1546 break;
1547 }
1548 case CHL_Ideal:
1549 // In an idealized mode where we don't have to worry about the
1550 // optimizer combining landing pads, we should just use
1551 // _Unwind_Resume (or the personality's equivalent).
1552 Builder.CreateCall(getUnwindResumeFn(), Exn)
1553 ->setDoesNotReturn();
1554 break;
1555 }
1556 }
1557
John McCallff8e1152010-07-23 21:56:41 +00001558 Builder.CreateUnreachable();
1559
1560 Builder.restoreIP(SavedIP);
1561
John McCall777d6e52011-08-11 02:22:43 +00001562 return EHResumeBlock;
John McCallff8e1152010-07-23 21:56:41 +00001563}