blob: 3a7b4d4107b78b5a72294a801ee86aab6d45c021 [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
Bill Wendling40ccacc2011-09-19 22:08:36 +0000242 // Otherwise, it has to be a landingpad instruction.
243 llvm::LandingPadInst *LPI = dyn_cast<llvm::LandingPadInst>(User);
244 if (!LPI) return false;
John McCallb2593832010-09-16 06:16:50 +0000245
Bill Wendling40ccacc2011-09-19 22:08:36 +0000246 for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
John McCallb2593832010-09-16 06:16:50 +0000247 // Look for something that would've been returned by the ObjC
248 // runtime's GetEHType() method.
Bill Wendling40ccacc2011-09-19 22:08:36 +0000249 llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
250 if (LPI->isCatch(I)) {
251 // Check if the catch value has the ObjC prefix.
Bill Wendlingeecb6a12011-09-20 00:40:19 +0000252 if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
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;
Bill Wendling40ccacc2011-09-19 22:08:36 +0000257 } else {
258 // Check if any of the filter values have the ObjC prefix.
259 llvm::Constant *CVal = cast<llvm::Constant>(Val);
260 for (llvm::User::op_iterator
261 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
Bill Wendlingeecb6a12011-09-20 00:40:19 +0000262 if (llvm::GlobalVariable *GV =
263 cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
264 // ObjC EH selector entries are always global variables with
265 // names starting like this.
266 if (GV->getName().startswith("OBJC_EHTYPE"))
267 return false;
Bill Wendling40ccacc2011-09-19 22:08:36 +0000268 }
269 }
John McCallb2593832010-09-16 06:16:50 +0000270 }
271 }
272
273 return true;
274}
275
276/// Try to use the C++ personality function in ObjC++. Not doing this
277/// can cause some incompatibilities with gcc, which is more
278/// aggressive about only using the ObjC++ personality in a function
279/// when it really needs it.
280void CodeGenModule::SimplifyPersonality() {
281 // For now, this is really a Darwin-specific operation.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000282 if (!Context.getTargetInfo().getTriple().isOSDarwin())
John McCallb2593832010-09-16 06:16:50 +0000283 return;
284
285 // If we're not in ObjC++ -fexceptions, there's nothing to do.
286 if (!Features.CPlusPlus || !Features.ObjC1 || !Features.Exceptions)
287 return;
288
289 const EHPersonality &ObjCXX = EHPersonality::get(Features);
290 const EHPersonality &CXX = getCXXPersonality(Features);
291 if (&ObjCXX == &CXX ||
292 ObjCXX.getPersonalityFnName() == CXX.getPersonalityFnName())
293 return;
294
295 llvm::Function *Fn =
296 getModule().getFunction(ObjCXX.getPersonalityFnName());
297
298 // Nothing to do if it's unused.
299 if (!Fn || Fn->use_empty()) return;
300
301 // Can't do the optimization if it has non-C++ uses.
302 if (!PersonalityHasOnlyCXXUses(Fn)) return;
303
304 // Create the C++ personality function and kill off the old
305 // function.
306 llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
307
308 // This can happen if the user is screwing with us.
309 if (Fn->getType() != CXXFn->getType()) return;
310
311 Fn->replaceAllUsesWith(CXXFn);
312 Fn->eraseFromParent();
John McCallf1549f62010-07-06 01:34:17 +0000313}
314
315/// Returns the value to inject into a selector to indicate the
316/// presence of a catch-all.
317static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
318 // Possibly we should use @llvm.eh.catch.all.value here.
John McCalld16c2cf2011-02-08 08:22:06 +0000319 return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
John McCallf1549f62010-07-06 01:34:17 +0000320}
321
John McCall09faeab2010-07-13 21:17:51 +0000322namespace {
323 /// A cleanup to free the exception object if its initialization
324 /// throws.
John McCallc4a1a842011-07-12 00:15:30 +0000325 struct FreeException : EHScopeStack::Cleanup {
326 llvm::Value *exn;
327 FreeException(llvm::Value *exn) : exn(exn) {}
John McCallad346f42011-07-12 20:27:29 +0000328 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall3ad32c82011-01-28 08:37:24 +0000329 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), exn)
John McCall09faeab2010-07-13 21:17:51 +0000330 ->setDoesNotThrow();
John McCall09faeab2010-07-13 21:17:51 +0000331 }
332 };
333}
334
John McCallac418162010-04-22 01:10:34 +0000335// Emits an exception expression into the given location. This
336// differs from EmitAnyExprToMem only in that, if a final copy-ctor
337// call is required, an exception within that copy ctor causes
338// std::terminate to be invoked.
John McCall3ad32c82011-01-28 08:37:24 +0000339static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e,
340 llvm::Value *addr) {
John McCallf1549f62010-07-06 01:34:17 +0000341 // Make sure the exception object is cleaned up if there's an
342 // exception during initialization.
John McCall3ad32c82011-01-28 08:37:24 +0000343 CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr);
344 EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin();
John McCallac418162010-04-22 01:10:34 +0000345
346 // __cxa_allocate_exception returns a void*; we need to cast this
347 // to the appropriate type for the object.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000348 llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo();
John McCall3ad32c82011-01-28 08:37:24 +0000349 llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty);
John McCallac418162010-04-22 01:10:34 +0000350
351 // FIXME: this isn't quite right! If there's a final unelided call
352 // to a copy constructor, then according to [except.terminate]p1 we
353 // must call std::terminate() if that constructor throws, because
354 // technically that copy occurs after the exception expression is
355 // evaluated but before the exception is caught. But the best way
356 // to handle that is to teach EmitAggExpr to do the final copy
357 // differently if it can't be elided.
John McCallf85e1932011-06-15 23:02:42 +0000358 CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
359 /*IsInit*/ true);
John McCallac418162010-04-22 01:10:34 +0000360
John McCall3ad32c82011-01-28 08:37:24 +0000361 // Deactivate the cleanup block.
John McCall6f103ba2011-11-10 10:43:54 +0000362 CGF.DeactivateCleanupBlock(cleanup, cast<llvm::Instruction>(typedAddr));
Mike Stump0f590be2009-12-01 03:41:18 +0000363}
364
John McCallf1549f62010-07-06 01:34:17 +0000365llvm::Value *CodeGenFunction::getExceptionSlot() {
John McCall93c332a2011-05-28 21:13:02 +0000366 if (!ExceptionSlot)
367 ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
John McCallf1549f62010-07-06 01:34:17 +0000368 return ExceptionSlot;
Mike Stump0f590be2009-12-01 03:41:18 +0000369}
370
John McCall93c332a2011-05-28 21:13:02 +0000371llvm::Value *CodeGenFunction::getEHSelectorSlot() {
372 if (!EHSelectorSlot)
373 EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
374 return EHSelectorSlot;
375}
376
Bill Wendlingae270592011-09-15 18:57:19 +0000377llvm::Value *CodeGenFunction::getExceptionFromSlot() {
378 return Builder.CreateLoad(getExceptionSlot(), "exn");
379}
380
381llvm::Value *CodeGenFunction::getSelectorFromSlot() {
382 return Builder.CreateLoad(getEHSelectorSlot(), "sel");
383}
384
Anders Carlsson756b5c42009-10-30 01:42:31 +0000385void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000386 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000387 if (getInvokeDest()) {
John McCallf1549f62010-07-06 01:34:17 +0000388 Builder.CreateInvoke(getReThrowFn(*this),
389 getUnreachableBlock(),
390 getInvokeDest())
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000391 ->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000392 } else {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000393 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000394 Builder.CreateUnreachable();
395 }
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000396
John McCallcd5b22e2011-01-12 03:41:02 +0000397 // throw is an expression, and the expression emitters expect us
398 // to leave ourselves at a valid insertion point.
399 EmitBlock(createBasicBlock("throw.cont"));
400
Anders Carlssond3379292009-10-30 02:27:02 +0000401 return;
402 }
Mike Stump8755ec32009-12-10 00:06:18 +0000403
Anders Carlssond3379292009-10-30 02:27:02 +0000404 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000405
Anders Carlssond3379292009-10-30 02:27:02 +0000406 // Now allocate the exception object.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000407 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000408 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000409
Anders Carlssond3379292009-10-30 02:27:02 +0000410 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
John McCallf1549f62010-07-06 01:34:17 +0000411 llvm::CallInst *ExceptionPtr =
Mike Stump8755ec32009-12-10 00:06:18 +0000412 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000413 llvm::ConstantInt::get(SizeTy, TypeSize),
414 "exception");
John McCallf1549f62010-07-06 01:34:17 +0000415 ExceptionPtr->setDoesNotThrow();
Anders Carlsson8370c582009-12-11 00:32:37 +0000416
John McCallac418162010-04-22 01:10:34 +0000417 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000418
Anders Carlssond3379292009-10-30 02:27:02 +0000419 // Now throw the exception.
Anders Carlsson82a113a2011-01-24 01:59:49 +0000420 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
421 /*ForEH=*/true);
John McCallac418162010-04-22 01:10:34 +0000422
423 // The address of the destructor. If the exception type has a
424 // trivial destructor (or isn't a record), we just pass null.
425 llvm::Constant *Dtor = 0;
426 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
427 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
428 if (!Record->hasTrivialDestructor()) {
Douglas Gregor1d110e02010-07-01 14:13:13 +0000429 CXXDestructorDecl *DtorD = Record->getDestructor();
John McCallac418162010-04-22 01:10:34 +0000430 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
431 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
432 }
433 }
434 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000435
Mike Stump0a3816e2009-12-04 01:51:45 +0000436 if (getInvokeDest()) {
Mike Stump8755ec32009-12-10 00:06:18 +0000437 llvm::InvokeInst *ThrowCall =
John McCallf1549f62010-07-06 01:34:17 +0000438 Builder.CreateInvoke3(getThrowFn(*this),
439 getUnreachableBlock(), getInvokeDest(),
Mike Stump0a3816e2009-12-04 01:51:45 +0000440 ExceptionPtr, TypeInfo, Dtor);
441 ThrowCall->setDoesNotReturn();
Mike Stump0a3816e2009-12-04 01:51:45 +0000442 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000443 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000444 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
445 ThrowCall->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000446 Builder.CreateUnreachable();
Mike Stump0a3816e2009-12-04 01:51:45 +0000447 }
Mike Stump8755ec32009-12-10 00:06:18 +0000448
John McCallcd5b22e2011-01-12 03:41:02 +0000449 // throw is an expression, and the expression emitters expect us
450 // to leave ourselves at a valid insertion point.
451 EmitBlock(createBasicBlock("throw.cont"));
Anders Carlsson756b5c42009-10-30 01:42:31 +0000452}
Mike Stump2bf701e2009-11-20 23:44:51 +0000453
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000454void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlsson15348ae2011-02-28 02:27:16 +0000455 if (!CGM.getLangOptions().CXXExceptions)
Anders Carlssona994ee42010-02-06 23:59:05 +0000456 return;
457
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000458 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
459 if (FD == 0)
460 return;
461 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
462 if (Proto == 0)
463 return;
464
Sebastian Redla968e972011-03-15 18:42:48 +0000465 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
466 if (isNoexceptExceptionSpec(EST)) {
467 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
468 // noexcept functions are simple terminate scopes.
469 EHStack.pushTerminate();
470 }
471 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
472 unsigned NumExceptions = Proto->getNumExceptions();
473 EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000474
Sebastian Redla968e972011-03-15 18:42:48 +0000475 for (unsigned I = 0; I != NumExceptions; ++I) {
476 QualType Ty = Proto->getExceptionType(I);
477 QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
478 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
479 /*ForEH=*/true);
480 Filter->setFilter(I, EHType);
481 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000482 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000483}
484
John McCall777d6e52011-08-11 02:22:43 +0000485/// Emit the dispatch block for a filter scope if necessary.
486static void emitFilterDispatchBlock(CodeGenFunction &CGF,
487 EHFilterScope &filterScope) {
488 llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
489 if (!dispatchBlock) return;
490 if (dispatchBlock->use_empty()) {
491 delete dispatchBlock;
492 return;
493 }
494
John McCall777d6e52011-08-11 02:22:43 +0000495 CGF.EmitBlockAfterUses(dispatchBlock);
496
497 // If this isn't a catch-all filter, we need to check whether we got
498 // here because the filter triggered.
499 if (filterScope.getNumFilters()) {
500 // Load the selector value.
Bill Wendlingae270592011-09-15 18:57:19 +0000501 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall777d6e52011-08-11 02:22:43 +0000502 llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
503
504 llvm::Value *zero = CGF.Builder.getInt32(0);
505 llvm::Value *failsFilter =
506 CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
507 CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, CGF.getEHResumeBlock());
508
509 CGF.EmitBlock(unexpectedBB);
510 }
511
512 // Call __cxa_call_unexpected. This doesn't need to be an invoke
513 // because __cxa_call_unexpected magically filters exceptions
514 // according to the last landing pad the exception was thrown
515 // into. Seriously.
Bill Wendlingae270592011-09-15 18:57:19 +0000516 llvm::Value *exn = CGF.getExceptionFromSlot();
John McCall777d6e52011-08-11 02:22:43 +0000517 CGF.Builder.CreateCall(getUnexpectedFn(CGF), exn)
518 ->setDoesNotReturn();
519 CGF.Builder.CreateUnreachable();
520}
521
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000522void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlsson15348ae2011-02-28 02:27:16 +0000523 if (!CGM.getLangOptions().CXXExceptions)
Anders Carlssona994ee42010-02-06 23:59:05 +0000524 return;
525
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000526 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
527 if (FD == 0)
528 return;
529 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
530 if (Proto == 0)
531 return;
532
Sebastian Redla968e972011-03-15 18:42:48 +0000533 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
534 if (isNoexceptExceptionSpec(EST)) {
535 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
536 EHStack.popTerminate();
537 }
538 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
John McCall777d6e52011-08-11 02:22:43 +0000539 EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
540 emitFilterDispatchBlock(*this, filterScope);
Sebastian Redla968e972011-03-15 18:42:48 +0000541 EHStack.popFilter();
542 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000543}
544
Mike Stump2bf701e2009-11-20 23:44:51 +0000545void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall59a70002010-07-07 06:56:46 +0000546 EnterCXXTryStmt(S);
John McCall9fc6a772010-02-19 09:25:03 +0000547 EmitStmt(S.getTryBlock());
John McCall59a70002010-07-07 06:56:46 +0000548 ExitCXXTryStmt(S);
John McCall9fc6a772010-02-19 09:25:03 +0000549}
550
John McCall59a70002010-07-07 06:56:46 +0000551void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallf1549f62010-07-06 01:34:17 +0000552 unsigned NumHandlers = S.getNumHandlers();
553 EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
John McCall9fc6a772010-02-19 09:25:03 +0000554
John McCallf1549f62010-07-06 01:34:17 +0000555 for (unsigned I = 0; I != NumHandlers; ++I) {
556 const CXXCatchStmt *C = S.getHandler(I);
John McCall9fc6a772010-02-19 09:25:03 +0000557
John McCallf1549f62010-07-06 01:34:17 +0000558 llvm::BasicBlock *Handler = createBasicBlock("catch");
559 if (C->getExceptionDecl()) {
560 // FIXME: Dropping the reference type on the type into makes it
561 // impossible to correctly implement catch-by-reference
562 // semantics for pointers. Unfortunately, this is what all
563 // existing compilers do, and it's not clear that the standard
564 // personality routine is capable of doing this right. See C++ DR 388:
565 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
566 QualType CaughtType = C->getCaughtType();
567 CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType();
John McCall5a180392010-07-24 00:37:23 +0000568
569 llvm::Value *TypeInfo = 0;
570 if (CaughtType->isObjCObjectPointerType())
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +0000571 TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
John McCall5a180392010-07-24 00:37:23 +0000572 else
Anders Carlsson82a113a2011-01-24 01:59:49 +0000573 TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true);
John McCallf1549f62010-07-06 01:34:17 +0000574 CatchScope->setHandler(I, TypeInfo, Handler);
575 } else {
576 // No exception decl indicates '...', a catch-all.
577 CatchScope->setCatchAllHandler(I, Handler);
578 }
579 }
John McCallf1549f62010-07-06 01:34:17 +0000580}
581
John McCall777d6e52011-08-11 02:22:43 +0000582llvm::BasicBlock *
583CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
584 // The dispatch block for the end of the scope chain is a block that
585 // just resumes unwinding.
586 if (si == EHStack.stable_end())
587 return getEHResumeBlock();
588
589 // Otherwise, we should look at the actual scope.
590 EHScope &scope = *EHStack.find(si);
591
592 llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
593 if (!dispatchBlock) {
594 switch (scope.getKind()) {
595 case EHScope::Catch: {
596 // Apply a special case to a single catch-all.
597 EHCatchScope &catchScope = cast<EHCatchScope>(scope);
598 if (catchScope.getNumHandlers() == 1 &&
599 catchScope.getHandler(0).isCatchAll()) {
600 dispatchBlock = catchScope.getHandler(0).Block;
601
602 // Otherwise, make a dispatch block.
603 } else {
604 dispatchBlock = createBasicBlock("catch.dispatch");
605 }
606 break;
607 }
608
609 case EHScope::Cleanup:
610 dispatchBlock = createBasicBlock("ehcleanup");
611 break;
612
613 case EHScope::Filter:
614 dispatchBlock = createBasicBlock("filter.dispatch");
615 break;
616
617 case EHScope::Terminate:
618 dispatchBlock = getTerminateHandler();
619 break;
620 }
621 scope.setCachedEHDispatchBlock(dispatchBlock);
622 }
623 return dispatchBlock;
624}
625
John McCallf1549f62010-07-06 01:34:17 +0000626/// Check whether this is a non-EH scope, i.e. a scope which doesn't
627/// affect exception handling. Currently, the only non-EH scopes are
628/// normal-only cleanup scopes.
629static bool isNonEHScope(const EHScope &S) {
John McCallda65ea82010-07-13 20:32:21 +0000630 switch (S.getKind()) {
John McCall1f0fca52010-07-21 07:22:38 +0000631 case EHScope::Cleanup:
632 return !cast<EHCleanupScope>(S).isEHCleanup();
John McCallda65ea82010-07-13 20:32:21 +0000633 case EHScope::Filter:
634 case EHScope::Catch:
635 case EHScope::Terminate:
636 return false;
637 }
638
639 // Suppress warning.
640 return false;
John McCallf1549f62010-07-06 01:34:17 +0000641}
642
643llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
644 assert(EHStack.requiresLandingPad());
645 assert(!EHStack.empty());
646
Anders Carlsson7a178512011-02-28 00:33:03 +0000647 if (!CGM.getLangOptions().Exceptions)
John McCallda65ea82010-07-13 20:32:21 +0000648 return 0;
649
John McCallf1549f62010-07-06 01:34:17 +0000650 // Check the innermost scope for a cached landing pad. If this is
651 // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
652 llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
653 if (LP) return LP;
654
655 // Build the landing pad for this scope.
656 LP = EmitLandingPad();
657 assert(LP);
658
659 // Cache the landing pad on the innermost scope. If this is a
660 // non-EH scope, cache the landing pad on the enclosing scope, too.
661 for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
662 ir->setCachedLandingPad(LP);
663 if (!isNonEHScope(*ir)) break;
664 }
665
666 return LP;
667}
668
John McCall93c332a2011-05-28 21:13:02 +0000669// This code contains a hack to work around a design flaw in
670// LLVM's EH IR which breaks semantics after inlining. This same
671// hack is implemented in llvm-gcc.
672//
673// The LLVM EH abstraction is basically a thin veneer over the
674// traditional GCC zero-cost design: for each range of instructions
675// in the function, there is (at most) one "landing pad" with an
676// associated chain of EH actions. A language-specific personality
677// function interprets this chain of actions and (1) decides whether
678// or not to resume execution at the landing pad and (2) if so,
679// provides an integer indicating why it's stopping. In LLVM IR,
680// the association of a landing pad with a range of instructions is
681// achieved via an invoke instruction, the chain of actions becomes
682// the arguments to the @llvm.eh.selector call, and the selector
683// call returns the integer indicator. Other than the required
684// presence of two intrinsic function calls in the landing pad,
685// the IR exactly describes the layout of the output code.
686//
687// A principal advantage of this design is that it is completely
688// language-agnostic; in theory, the LLVM optimizers can treat
689// landing pads neutrally, and targets need only know how to lower
690// the intrinsics to have a functioning exceptions system (assuming
691// that platform exceptions follow something approximately like the
692// GCC design). Unfortunately, landing pads cannot be combined in a
693// language-agnostic way: given selectors A and B, there is no way
694// to make a single landing pad which faithfully represents the
695// semantics of propagating an exception first through A, then
696// through B, without knowing how the personality will interpret the
697// (lowered form of the) selectors. This means that inlining has no
698// choice but to crudely chain invokes (i.e., to ignore invokes in
699// the inlined function, but to turn all unwindable calls into
700// invokes), which is only semantically valid if every unwind stops
701// at every landing pad.
702//
703// Therefore, the invoke-inline hack is to guarantee that every
704// landing pad has a catch-all.
705enum CleanupHackLevel_t {
706 /// A level of hack that requires that all landing pads have
707 /// catch-alls.
708 CHL_MandatoryCatchall,
709
710 /// A level of hack that requires that all landing pads handle
711 /// cleanups.
712 CHL_MandatoryCleanup,
713
714 /// No hacks at all; ideal IR generation.
715 CHL_Ideal
716};
717const CleanupHackLevel_t CleanupHackLevel = CHL_MandatoryCleanup;
718
John McCallf1549f62010-07-06 01:34:17 +0000719llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
720 assert(EHStack.requiresLandingPad());
721
John McCall777d6e52011-08-11 02:22:43 +0000722 EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
723 switch (innermostEHScope.getKind()) {
724 case EHScope::Terminate:
725 return getTerminateLandingPad();
John McCallf1549f62010-07-06 01:34:17 +0000726
John McCall777d6e52011-08-11 02:22:43 +0000727 case EHScope::Catch:
728 case EHScope::Cleanup:
729 case EHScope::Filter:
730 if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
731 return lpad;
John McCallf1549f62010-07-06 01:34:17 +0000732 }
733
734 // Save the current IR generation state.
John McCall777d6e52011-08-11 02:22:43 +0000735 CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
John McCallf1549f62010-07-06 01:34:17 +0000736
John McCall777d6e52011-08-11 02:22:43 +0000737 const EHPersonality &personality = EHPersonality::get(getLangOptions());
John McCall8262b6a2010-07-17 00:43:08 +0000738
John McCallf1549f62010-07-06 01:34:17 +0000739 // Create and configure the landing pad.
John McCall777d6e52011-08-11 02:22:43 +0000740 llvm::BasicBlock *lpad = createBasicBlock("lpad");
741 EmitBlock(lpad);
John McCallf1549f62010-07-06 01:34:17 +0000742
Bill Wendling285cfd82011-09-19 20:31:14 +0000743 llvm::LandingPadInst *LPadInst =
744 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
745 getOpaquePersonalityFn(CGM, personality), 0);
746
747 llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
748 Builder.CreateStore(LPadExn, getExceptionSlot());
749 llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
750 Builder.CreateStore(LPadSel, getEHSelectorSlot());
751
John McCallf1549f62010-07-06 01:34:17 +0000752 // Save the exception pointer. It's safe to use a single exception
753 // pointer per function because EH cleanups can never have nested
754 // try/catches.
Bill Wendling285cfd82011-09-19 20:31:14 +0000755 // Build the landingpad instruction.
John McCallf1549f62010-07-06 01:34:17 +0000756
757 // Accumulate all the handlers in scope.
John McCall777d6e52011-08-11 02:22:43 +0000758 bool hasCatchAll = false;
759 bool hasCleanup = false;
760 bool hasFilter = false;
761 SmallVector<llvm::Value*, 4> filterTypes;
762 llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
John McCallf1549f62010-07-06 01:34:17 +0000763 for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end();
764 I != E; ++I) {
765
766 switch (I->getKind()) {
John McCall1f0fca52010-07-21 07:22:38 +0000767 case EHScope::Cleanup:
John McCall777d6e52011-08-11 02:22:43 +0000768 // If we have a cleanup, remember that.
769 hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
John McCallda65ea82010-07-13 20:32:21 +0000770 continue;
771
John McCallf1549f62010-07-06 01:34:17 +0000772 case EHScope::Filter: {
773 assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
John McCall777d6e52011-08-11 02:22:43 +0000774 assert(!hasCatchAll && "EH filter reached after catch-all");
John McCallf1549f62010-07-06 01:34:17 +0000775
Bill Wendling285cfd82011-09-19 20:31:14 +0000776 // Filter scopes get added to the landingpad in weird ways.
John McCall777d6e52011-08-11 02:22:43 +0000777 EHFilterScope &filter = cast<EHFilterScope>(*I);
778 hasFilter = true;
John McCallf1549f62010-07-06 01:34:17 +0000779
Bill Wendling8990daf2011-09-22 20:32:54 +0000780 // Add all the filter values.
781 for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
782 filterTypes.push_back(filter.getFilter(i));
John McCallf1549f62010-07-06 01:34:17 +0000783 goto done;
784 }
785
786 case EHScope::Terminate:
787 // Terminate scopes are basically catch-alls.
John McCall777d6e52011-08-11 02:22:43 +0000788 assert(!hasCatchAll);
789 hasCatchAll = true;
John McCallf1549f62010-07-06 01:34:17 +0000790 goto done;
791
792 case EHScope::Catch:
793 break;
794 }
795
John McCall777d6e52011-08-11 02:22:43 +0000796 EHCatchScope &catchScope = cast<EHCatchScope>(*I);
797 for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
798 EHCatchScope::Handler handler = catchScope.getHandler(hi);
John McCallf1549f62010-07-06 01:34:17 +0000799
John McCall777d6e52011-08-11 02:22:43 +0000800 // If this is a catch-all, register that and abort.
801 if (!handler.Type) {
802 assert(!hasCatchAll);
803 hasCatchAll = true;
804 goto done;
John McCallf1549f62010-07-06 01:34:17 +0000805 }
806
807 // Check whether we already have a handler for this type.
Bill Wendling285cfd82011-09-19 20:31:14 +0000808 if (catchTypes.insert(handler.Type))
809 // If not, add it directly to the landingpad.
810 LPadInst->addClause(handler.Type);
John McCallf1549f62010-07-06 01:34:17 +0000811 }
John McCallf1549f62010-07-06 01:34:17 +0000812 }
813
814 done:
Bill Wendling285cfd82011-09-19 20:31:14 +0000815 // If we have a catch-all, add null to the landingpad.
John McCall777d6e52011-08-11 02:22:43 +0000816 assert(!(hasCatchAll && hasFilter));
817 if (hasCatchAll) {
Bill Wendling285cfd82011-09-19 20:31:14 +0000818 LPadInst->addClause(getCatchAllValue(*this));
John McCallf1549f62010-07-06 01:34:17 +0000819
820 // If we have an EH filter, we need to add those handlers in the
Bill Wendling285cfd82011-09-19 20:31:14 +0000821 // right place in the landingpad, which is to say, at the end.
John McCall777d6e52011-08-11 02:22:43 +0000822 } else if (hasFilter) {
Bill Wendling40ccacc2011-09-19 22:08:36 +0000823 // Create a filter expression: a constant array indicating which filter
824 // types there are. The personality routine only lands here if the filter
825 // doesn't match.
Bill Wendling285cfd82011-09-19 20:31:14 +0000826 llvm::SmallVector<llvm::Constant*, 8> Filters;
827 llvm::ArrayType *AType =
828 llvm::ArrayType::get(!filterTypes.empty() ?
829 filterTypes[0]->getType() : Int8PtrTy,
830 filterTypes.size());
831
832 for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
833 Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
834 llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
835 LPadInst->addClause(FilterArray);
John McCallf1549f62010-07-06 01:34:17 +0000836
837 // Also check whether we need a cleanup.
Bill Wendling285cfd82011-09-19 20:31:14 +0000838 if (hasCleanup)
839 LPadInst->setCleanup(true);
John McCallf1549f62010-07-06 01:34:17 +0000840
841 // Otherwise, signal that we at least have cleanups.
John McCall777d6e52011-08-11 02:22:43 +0000842 } else if (CleanupHackLevel == CHL_MandatoryCatchall || hasCleanup) {
Bill Wendling285cfd82011-09-19 20:31:14 +0000843 if (CleanupHackLevel == CHL_MandatoryCatchall)
844 LPadInst->addClause(getCatchAllValue(*this));
845 else
846 LPadInst->setCleanup(true);
John McCallf1549f62010-07-06 01:34:17 +0000847 }
848
Bill Wendling285cfd82011-09-19 20:31:14 +0000849 assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
850 "landingpad instruction has no clauses!");
John McCallf1549f62010-07-06 01:34:17 +0000851
852 // Tell the backend how to generate the landing pad.
John McCall777d6e52011-08-11 02:22:43 +0000853 Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
John McCallf1549f62010-07-06 01:34:17 +0000854
855 // Restore the old IR generation state.
John McCall777d6e52011-08-11 02:22:43 +0000856 Builder.restoreIP(savedIP);
John McCallf1549f62010-07-06 01:34:17 +0000857
John McCall777d6e52011-08-11 02:22:43 +0000858 return lpad;
John McCallf1549f62010-07-06 01:34:17 +0000859}
860
John McCall8e3f8612010-07-13 22:12:14 +0000861namespace {
862 /// A cleanup to call __cxa_end_catch. In many cases, the caught
863 /// exception type lets us state definitively that the thrown exception
864 /// type does not have a destructor. In particular:
865 /// - Catch-alls tell us nothing, so we have to conservatively
866 /// assume that the thrown exception might have a destructor.
867 /// - Catches by reference behave according to their base types.
868 /// - Catches of non-record types will only trigger for exceptions
869 /// of non-record types, which never have destructors.
870 /// - Catches of record types can trigger for arbitrary subclasses
871 /// of the caught type, so we have to assume the actual thrown
872 /// exception type might have a throwing destructor, even if the
873 /// caught type's destructor is trivial or nothrow.
John McCall1f0fca52010-07-21 07:22:38 +0000874 struct CallEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +0000875 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
876 bool MightThrow;
877
John McCallad346f42011-07-12 20:27:29 +0000878 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall8e3f8612010-07-13 22:12:14 +0000879 if (!MightThrow) {
880 CGF.Builder.CreateCall(getEndCatchFn(CGF))->setDoesNotThrow();
881 return;
882 }
883
Jay Foad4c7d9f12011-07-15 08:37:34 +0000884 CGF.EmitCallOrInvoke(getEndCatchFn(CGF));
John McCall8e3f8612010-07-13 22:12:14 +0000885 }
886 };
887}
888
John McCallf1549f62010-07-06 01:34:17 +0000889/// Emits a call to __cxa_begin_catch and enters a cleanup to call
890/// __cxa_end_catch.
John McCall8e3f8612010-07-13 22:12:14 +0000891///
892/// \param EndMightThrow - true if __cxa_end_catch might throw
893static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
894 llvm::Value *Exn,
895 bool EndMightThrow) {
John McCallf1549f62010-07-06 01:34:17 +0000896 llvm::CallInst *Call = CGF.Builder.CreateCall(getBeginCatchFn(CGF), Exn);
897 Call->setDoesNotThrow();
898
John McCall1f0fca52010-07-21 07:22:38 +0000899 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
John McCallf1549f62010-07-06 01:34:17 +0000900
901 return Call;
902}
903
904/// A "special initializer" callback for initializing a catch
905/// parameter during catch initialization.
906static void InitCatchParam(CodeGenFunction &CGF,
907 const VarDecl &CatchParam,
908 llvm::Value *ParamAddr) {
909 // Load the exception from where the landing pad saved it.
Bill Wendlingae270592011-09-15 18:57:19 +0000910 llvm::Value *Exn = CGF.getExceptionFromSlot();
John McCallf1549f62010-07-06 01:34:17 +0000911
912 CanQualType CatchType =
913 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000914 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
John McCallf1549f62010-07-06 01:34:17 +0000915
916 // If we're catching by reference, we can just cast the object
917 // pointer to the appropriate pointer.
918 if (isa<ReferenceType>(CatchType)) {
John McCall204b0752010-07-20 22:17:55 +0000919 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
920 bool EndCatchMightThrow = CaughtType->isRecordType();
John McCall8e3f8612010-07-13 22:12:14 +0000921
John McCallf1549f62010-07-06 01:34:17 +0000922 // __cxa_begin_catch returns the adjusted object pointer.
John McCall8e3f8612010-07-13 22:12:14 +0000923 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
John McCall204b0752010-07-20 22:17:55 +0000924
925 // We have no way to tell the personality function that we're
926 // catching by reference, so if we're catching a pointer,
927 // __cxa_begin_catch will actually return that pointer by value.
928 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
929 QualType PointeeType = PT->getPointeeType();
930
931 // When catching by reference, generally we should just ignore
932 // this by-value pointer and use the exception object instead.
933 if (!PointeeType->isRecordType()) {
934
935 // Exn points to the struct _Unwind_Exception header, which
936 // we have to skip past in order to reach the exception data.
937 unsigned HeaderSize =
938 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
939 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
940
941 // However, if we're catching a pointer-to-record type that won't
942 // work, because the personality function might have adjusted
943 // the pointer. There's actually no way for us to fully satisfy
944 // the language/ABI contract here: we can't use Exn because it
945 // might have the wrong adjustment, but we can't use the by-value
946 // pointer because it's off by a level of abstraction.
947 //
948 // The current solution is to dump the adjusted pointer into an
949 // alloca, which breaks language semantics (because changing the
950 // pointer doesn't change the exception) but at least works.
951 // The better solution would be to filter out non-exact matches
952 // and rethrow them, but this is tricky because the rethrow
953 // really needs to be catchable by other sites at this landing
954 // pad. The best solution is to fix the personality function.
955 } else {
956 // Pull the pointer for the reference type off.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000957 llvm::Type *PtrTy =
John McCall204b0752010-07-20 22:17:55 +0000958 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
959
960 // Create the temporary and write the adjusted pointer into it.
961 llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
962 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
963 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
964
965 // Bind the reference to the temporary.
966 AdjustedExn = ExnPtrTmp;
967 }
968 }
969
John McCallf1549f62010-07-06 01:34:17 +0000970 llvm::Value *ExnCast =
971 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
972 CGF.Builder.CreateStore(ExnCast, ParamAddr);
973 return;
974 }
975
976 // Non-aggregates (plus complexes).
977 bool IsComplex = false;
978 if (!CGF.hasAggregateLLVMType(CatchType) ||
979 (IsComplex = CatchType->isAnyComplexType())) {
John McCall8e3f8612010-07-13 22:12:14 +0000980 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
John McCallf1549f62010-07-06 01:34:17 +0000981
982 // If the catch type is a pointer type, __cxa_begin_catch returns
983 // the pointer by value.
984 if (CatchType->hasPointerRepresentation()) {
985 llvm::Value *CastExn =
986 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
987 CGF.Builder.CreateStore(CastExn, ParamAddr);
988 return;
989 }
990
991 // Otherwise, it returns a pointer into the exception object.
992
Chris Lattner2acc6e32011-07-18 04:24:23 +0000993 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
John McCallf1549f62010-07-06 01:34:17 +0000994 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
995
996 if (IsComplex) {
997 CGF.StoreComplexToAddr(CGF.LoadComplexFromAddr(Cast, /*volatile*/ false),
998 ParamAddr, /*volatile*/ false);
999 } else {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001000 unsigned Alignment =
1001 CGF.getContext().getDeclAlign(&CatchParam).getQuantity();
John McCallf1549f62010-07-06 01:34:17 +00001002 llvm::Value *ExnLoad = CGF.Builder.CreateLoad(Cast, "exn.scalar");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001003 CGF.EmitStoreOfScalar(ExnLoad, ParamAddr, /*volatile*/ false, Alignment,
1004 CatchType);
John McCallf1549f62010-07-06 01:34:17 +00001005 }
1006 return;
1007 }
1008
John McCallacff6962011-02-16 08:39:19 +00001009 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
John McCallf1549f62010-07-06 01:34:17 +00001010
Chris Lattner2acc6e32011-07-18 04:24:23 +00001011 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
John McCallf1549f62010-07-06 01:34:17 +00001012
John McCallacff6962011-02-16 08:39:19 +00001013 // Check for a copy expression. If we don't have a copy expression,
1014 // that means a trivial copy is okay.
John McCalle996ffd2011-02-16 08:02:54 +00001015 const Expr *copyExpr = CatchParam.getInit();
1016 if (!copyExpr) {
John McCallacff6962011-02-16 08:39:19 +00001017 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
1018 llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1019 CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
John McCallf1549f62010-07-06 01:34:17 +00001020 return;
1021 }
1022
1023 // We have to call __cxa_get_exception_ptr to get the adjusted
1024 // pointer before copying.
John McCalle996ffd2011-02-16 08:02:54 +00001025 llvm::CallInst *rawAdjustedExn =
John McCallf1549f62010-07-06 01:34:17 +00001026 CGF.Builder.CreateCall(getGetExceptionPtrFn(CGF), Exn);
John McCalle996ffd2011-02-16 08:02:54 +00001027 rawAdjustedExn->setDoesNotThrow();
John McCallf1549f62010-07-06 01:34:17 +00001028
John McCalle996ffd2011-02-16 08:02:54 +00001029 // Cast that to the appropriate type.
1030 llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
John McCallf1549f62010-07-06 01:34:17 +00001031
John McCalle996ffd2011-02-16 08:02:54 +00001032 // The copy expression is defined in terms of an OpaqueValueExpr.
1033 // Find it and map it to the adjusted expression.
1034 CodeGenFunction::OpaqueValueMapping
John McCall56ca35d2011-02-17 10:25:35 +00001035 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
1036 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
John McCallf1549f62010-07-06 01:34:17 +00001037
1038 // Call the copy ctor in a terminate scope.
1039 CGF.EHStack.pushTerminate();
John McCalle996ffd2011-02-16 08:02:54 +00001040
1041 // Perform the copy construction.
Eli Friedmanf3940782011-12-03 00:54:26 +00001042 unsigned Alignment = CGF.getContext().getDeclAlign(&CatchParam).getQuantity();
1043 CGF.EmitAggExpr(copyExpr,
1044 AggValueSlot::forAddr(ParamAddr, Alignment, Qualifiers(),
1045 AggValueSlot::IsNotDestructed,
1046 AggValueSlot::DoesNotNeedGCBarriers,
1047 AggValueSlot::IsNotAliased));
John McCalle996ffd2011-02-16 08:02:54 +00001048
1049 // Leave the terminate scope.
John McCallf1549f62010-07-06 01:34:17 +00001050 CGF.EHStack.popTerminate();
1051
John McCalle996ffd2011-02-16 08:02:54 +00001052 // Undo the opaque value mapping.
1053 opaque.pop();
1054
John McCallf1549f62010-07-06 01:34:17 +00001055 // Finally we can call __cxa_begin_catch.
John McCall8e3f8612010-07-13 22:12:14 +00001056 CallBeginCatch(CGF, Exn, true);
John McCallf1549f62010-07-06 01:34:17 +00001057}
1058
1059/// Begins a catch statement by initializing the catch variable and
1060/// calling __cxa_begin_catch.
John McCalle996ffd2011-02-16 08:02:54 +00001061static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
John McCallf1549f62010-07-06 01:34:17 +00001062 // We have to be very careful with the ordering of cleanups here:
1063 // C++ [except.throw]p4:
1064 // The destruction [of the exception temporary] occurs
1065 // immediately after the destruction of the object declared in
1066 // the exception-declaration in the handler.
1067 //
1068 // So the precise ordering is:
1069 // 1. Construct catch variable.
1070 // 2. __cxa_begin_catch
1071 // 3. Enter __cxa_end_catch cleanup
1072 // 4. Enter dtor cleanup
1073 //
John McCall34695852011-02-22 06:44:22 +00001074 // We do this by using a slightly abnormal initialization process.
1075 // Delegation sequence:
John McCallf1549f62010-07-06 01:34:17 +00001076 // - ExitCXXTryStmt opens a RunCleanupsScope
John McCall34695852011-02-22 06:44:22 +00001077 // - EmitAutoVarAlloca creates the variable and debug info
John McCallf1549f62010-07-06 01:34:17 +00001078 // - InitCatchParam initializes the variable from the exception
John McCall34695852011-02-22 06:44:22 +00001079 // - CallBeginCatch calls __cxa_begin_catch
1080 // - CallBeginCatch enters the __cxa_end_catch cleanup
1081 // - EmitAutoVarCleanups enters the variable destructor cleanup
John McCallf1549f62010-07-06 01:34:17 +00001082 // - EmitCXXTryStmt emits the code for the catch body
1083 // - EmitCXXTryStmt close the RunCleanupsScope
1084
1085 VarDecl *CatchParam = S->getExceptionDecl();
1086 if (!CatchParam) {
Bill Wendlingae270592011-09-15 18:57:19 +00001087 llvm::Value *Exn = CGF.getExceptionFromSlot();
John McCall8e3f8612010-07-13 22:12:14 +00001088 CallBeginCatch(CGF, Exn, true);
John McCallf1549f62010-07-06 01:34:17 +00001089 return;
1090 }
1091
1092 // Emit the local.
John McCall34695852011-02-22 06:44:22 +00001093 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
1094 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF));
1095 CGF.EmitAutoVarCleanups(var);
John McCall9fc6a772010-02-19 09:25:03 +00001096}
1097
John McCallfcd5c0c2010-07-13 22:24:23 +00001098namespace {
John McCall1f0fca52010-07-21 07:22:38 +00001099 struct CallRethrow : EHScopeStack::Cleanup {
John McCallad346f42011-07-12 20:27:29 +00001100 void Emit(CodeGenFunction &CGF, Flags flags) {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001101 CGF.EmitCallOrInvoke(getReThrowFn(CGF));
John McCallfcd5c0c2010-07-13 22:24:23 +00001102 }
1103 };
1104}
1105
John McCall777d6e52011-08-11 02:22:43 +00001106/// Emit the structure of the dispatch block for the given catch scope.
1107/// It is an invariant that the dispatch block already exists.
1108static void emitCatchDispatchBlock(CodeGenFunction &CGF,
1109 EHCatchScope &catchScope) {
1110 llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1111 assert(dispatchBlock);
1112
1113 // If there's only a single catch-all, getEHDispatchBlock returned
1114 // that catch-all as the dispatch block.
1115 if (catchScope.getNumHandlers() == 1 &&
1116 catchScope.getHandler(0).isCatchAll()) {
1117 assert(dispatchBlock == catchScope.getHandler(0).Block);
1118 return;
1119 }
1120
1121 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1122 CGF.EmitBlockAfterUses(dispatchBlock);
1123
1124 // Select the right handler.
1125 llvm::Value *llvm_eh_typeid_for =
1126 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1127
1128 // Load the selector value.
Bill Wendlingae270592011-09-15 18:57:19 +00001129 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall777d6e52011-08-11 02:22:43 +00001130
1131 // Test against each of the exception types we claim to catch.
1132 for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1133 assert(i < e && "ran off end of handlers!");
1134 const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1135
1136 llvm::Value *typeValue = handler.Type;
1137 assert(typeValue && "fell into catch-all case!");
1138 typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1139
1140 // Figure out the next block.
1141 bool nextIsEnd;
1142 llvm::BasicBlock *nextBlock;
1143
1144 // If this is the last handler, we're at the end, and the next
1145 // block is the block for the enclosing EH scope.
1146 if (i + 1 == e) {
1147 nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1148 nextIsEnd = true;
1149
1150 // If the next handler is a catch-all, we're at the end, and the
1151 // next block is that handler.
1152 } else if (catchScope.getHandler(i+1).isCatchAll()) {
1153 nextBlock = catchScope.getHandler(i+1).Block;
1154 nextIsEnd = true;
1155
1156 // Otherwise, we're not at the end and we need a new block.
1157 } else {
1158 nextBlock = CGF.createBasicBlock("catch.fallthrough");
1159 nextIsEnd = false;
1160 }
1161
1162 // Figure out the catch type's index in the LSDA's type table.
1163 llvm::CallInst *typeIndex =
1164 CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1165 typeIndex->setDoesNotThrow();
1166
1167 llvm::Value *matchesTypeIndex =
1168 CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1169 CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1170
1171 // If the next handler is a catch-all, we're completely done.
1172 if (nextIsEnd) {
1173 CGF.Builder.restoreIP(savedIP);
1174 return;
1175
1176 // Otherwise we need to emit and continue at that block.
1177 } else {
1178 CGF.EmitBlock(nextBlock);
1179 }
1180 }
1181
1182 llvm_unreachable("fell out of loop!");
1183}
1184
1185void CodeGenFunction::popCatchScope() {
1186 EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1187 if (catchScope.hasEHBranches())
1188 emitCatchDispatchBlock(*this, catchScope);
1189 EHStack.popCatch();
1190}
1191
John McCall59a70002010-07-07 06:56:46 +00001192void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallf1549f62010-07-06 01:34:17 +00001193 unsigned NumHandlers = S.getNumHandlers();
1194 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1195 assert(CatchScope.getNumHandlers() == NumHandlers);
Mike Stump2bf701e2009-11-20 23:44:51 +00001196
John McCall777d6e52011-08-11 02:22:43 +00001197 // If the catch was not required, bail out now.
1198 if (!CatchScope.hasEHBranches()) {
1199 EHStack.popCatch();
1200 return;
1201 }
1202
1203 // Emit the structure of the EH dispatch for this catch.
1204 emitCatchDispatchBlock(*this, CatchScope);
1205
John McCallf1549f62010-07-06 01:34:17 +00001206 // Copy the handler blocks off before we pop the EH stack. Emitting
1207 // the handlers might scribble on this memory.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001208 SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers);
John McCallf1549f62010-07-06 01:34:17 +00001209 memcpy(Handlers.data(), CatchScope.begin(),
1210 NumHandlers * sizeof(EHCatchScope::Handler));
John McCall777d6e52011-08-11 02:22:43 +00001211
John McCallf1549f62010-07-06 01:34:17 +00001212 EHStack.popCatch();
Mike Stump2bf701e2009-11-20 23:44:51 +00001213
John McCallf1549f62010-07-06 01:34:17 +00001214 // The fall-through block.
1215 llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
Mike Stump2bf701e2009-11-20 23:44:51 +00001216
John McCallf1549f62010-07-06 01:34:17 +00001217 // We just emitted the body of the try; jump to the continue block.
1218 if (HaveInsertPoint())
1219 Builder.CreateBr(ContBB);
Mike Stump639787c2009-12-02 19:53:57 +00001220
John McCall59a70002010-07-07 06:56:46 +00001221 // Determine if we need an implicit rethrow for all these catch handlers.
1222 bool ImplicitRethrow = false;
1223 if (IsFnTryBlock)
1224 ImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1225 isa<CXXConstructorDecl>(CurCodeDecl);
1226
John McCall777d6e52011-08-11 02:22:43 +00001227 // Perversely, we emit the handlers backwards precisely because we
1228 // want them to appear in source order. In all of these cases, the
1229 // catch block will have exactly one predecessor, which will be a
1230 // particular block in the catch dispatch. However, in the case of
1231 // a catch-all, one of the dispatch blocks will branch to two
1232 // different handlers, and EmitBlockAfterUses will cause the second
1233 // handler to be moved before the first.
1234 for (unsigned I = NumHandlers; I != 0; --I) {
1235 llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1236 EmitBlockAfterUses(CatchBlock);
Mike Stump8755ec32009-12-10 00:06:18 +00001237
John McCallf1549f62010-07-06 01:34:17 +00001238 // Catch the exception if this isn't a catch-all.
John McCall777d6e52011-08-11 02:22:43 +00001239 const CXXCatchStmt *C = S.getHandler(I-1);
Mike Stump2bf701e2009-11-20 23:44:51 +00001240
John McCallf1549f62010-07-06 01:34:17 +00001241 // Enter a cleanup scope, including the catch variable and the
1242 // end-catch.
1243 RunCleanupsScope CatchScope(*this);
Mike Stump2bf701e2009-11-20 23:44:51 +00001244
John McCallf1549f62010-07-06 01:34:17 +00001245 // Initialize the catch variable and set up the cleanups.
1246 BeginCatch(*this, C);
1247
John McCall59a70002010-07-07 06:56:46 +00001248 // If there's an implicit rethrow, push a normal "cleanup" to call
John McCallfcd5c0c2010-07-13 22:24:23 +00001249 // _cxa_rethrow. This needs to happen before __cxa_end_catch is
1250 // called, and so it is pushed after BeginCatch.
1251 if (ImplicitRethrow)
John McCall1f0fca52010-07-21 07:22:38 +00001252 EHStack.pushCleanup<CallRethrow>(NormalCleanup);
John McCall59a70002010-07-07 06:56:46 +00001253
John McCallf1549f62010-07-06 01:34:17 +00001254 // Perform the body of the catch.
1255 EmitStmt(C->getHandlerBlock());
1256
1257 // Fall out through the catch cleanups.
1258 CatchScope.ForceCleanup();
1259
1260 // Branch out of the try.
1261 if (HaveInsertPoint())
1262 Builder.CreateBr(ContBB);
Mike Stump2bf701e2009-11-20 23:44:51 +00001263 }
1264
John McCallf1549f62010-07-06 01:34:17 +00001265 EmitBlock(ContBB);
Mike Stump2bf701e2009-11-20 23:44:51 +00001266}
Mike Stumpd88ea562009-12-09 03:35:49 +00001267
John McCall55b20fc2010-07-21 00:52:03 +00001268namespace {
John McCall1f0fca52010-07-21 07:22:38 +00001269 struct CallEndCatchForFinally : EHScopeStack::Cleanup {
John McCall55b20fc2010-07-21 00:52:03 +00001270 llvm::Value *ForEHVar;
1271 llvm::Value *EndCatchFn;
1272 CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1273 : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1274
John McCallad346f42011-07-12 20:27:29 +00001275 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall55b20fc2010-07-21 00:52:03 +00001276 llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1277 llvm::BasicBlock *CleanupContBB =
1278 CGF.createBasicBlock("finally.cleanup.cont");
1279
1280 llvm::Value *ShouldEndCatch =
1281 CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch");
1282 CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1283 CGF.EmitBlock(EndCatchBB);
Jay Foad4c7d9f12011-07-15 08:37:34 +00001284 CGF.EmitCallOrInvoke(EndCatchFn); // catch-all, so might throw
John McCall55b20fc2010-07-21 00:52:03 +00001285 CGF.EmitBlock(CleanupContBB);
1286 }
1287 };
John McCall77199712010-07-21 05:47:49 +00001288
John McCall1f0fca52010-07-21 07:22:38 +00001289 struct PerformFinally : EHScopeStack::Cleanup {
John McCall77199712010-07-21 05:47:49 +00001290 const Stmt *Body;
1291 llvm::Value *ForEHVar;
1292 llvm::Value *EndCatchFn;
1293 llvm::Value *RethrowFn;
1294 llvm::Value *SavedExnVar;
1295
1296 PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1297 llvm::Value *EndCatchFn,
1298 llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1299 : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1300 RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1301
John McCallad346f42011-07-12 20:27:29 +00001302 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall77199712010-07-21 05:47:49 +00001303 // Enter a cleanup to call the end-catch function if one was provided.
1304 if (EndCatchFn)
John McCall1f0fca52010-07-21 07:22:38 +00001305 CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1306 ForEHVar, EndCatchFn);
John McCall77199712010-07-21 05:47:49 +00001307
John McCalld96a8e72010-08-11 00:16:14 +00001308 // Save the current cleanup destination in case there are
1309 // cleanups in the finally block.
1310 llvm::Value *SavedCleanupDest =
1311 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1312 "cleanup.dest.saved");
1313
John McCall77199712010-07-21 05:47:49 +00001314 // Emit the finally block.
1315 CGF.EmitStmt(Body);
1316
1317 // If the end of the finally is reachable, check whether this was
1318 // for EH. If so, rethrow.
1319 if (CGF.HaveInsertPoint()) {
1320 llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1321 llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1322
1323 llvm::Value *ShouldRethrow =
1324 CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow");
1325 CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1326
1327 CGF.EmitBlock(RethrowBB);
1328 if (SavedExnVar) {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001329 CGF.EmitCallOrInvoke(RethrowFn, CGF.Builder.CreateLoad(SavedExnVar));
John McCall77199712010-07-21 05:47:49 +00001330 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001331 CGF.EmitCallOrInvoke(RethrowFn);
John McCall77199712010-07-21 05:47:49 +00001332 }
1333 CGF.Builder.CreateUnreachable();
1334
1335 CGF.EmitBlock(ContBB);
John McCalld96a8e72010-08-11 00:16:14 +00001336
1337 // Restore the cleanup destination.
1338 CGF.Builder.CreateStore(SavedCleanupDest,
1339 CGF.getNormalCleanupDestSlot());
John McCall77199712010-07-21 05:47:49 +00001340 }
1341
1342 // Leave the end-catch cleanup. As an optimization, pretend that
1343 // the fallthrough path was inaccessible; we've dynamically proven
1344 // that we're not in the EH case along that path.
1345 if (EndCatchFn) {
1346 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1347 CGF.PopCleanupBlock();
1348 CGF.Builder.restoreIP(SavedIP);
1349 }
1350
1351 // Now make sure we actually have an insertion point or the
1352 // cleanup gods will hate us.
1353 CGF.EnsureInsertPoint();
1354 }
1355 };
John McCall55b20fc2010-07-21 00:52:03 +00001356}
1357
John McCallf1549f62010-07-06 01:34:17 +00001358/// Enters a finally block for an implementation using zero-cost
1359/// exceptions. This is mostly general, but hard-codes some
1360/// language/ABI-specific behavior in the catch-all sections.
John McCalld768e9d2011-06-22 02:32:12 +00001361void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1362 const Stmt *body,
1363 llvm::Constant *beginCatchFn,
1364 llvm::Constant *endCatchFn,
1365 llvm::Constant *rethrowFn) {
1366 assert((beginCatchFn != 0) == (endCatchFn != 0) &&
John McCallf1549f62010-07-06 01:34:17 +00001367 "begin/end catch functions not paired");
John McCalld768e9d2011-06-22 02:32:12 +00001368 assert(rethrowFn && "rethrow function is required");
1369
1370 BeginCatchFn = beginCatchFn;
Mike Stumpd88ea562009-12-09 03:35:49 +00001371
John McCallf1549f62010-07-06 01:34:17 +00001372 // The rethrow function has one of the following two types:
1373 // void (*)()
1374 // void (*)(void*)
1375 // In the latter case we need to pass it the exception object.
1376 // But we can't use the exception slot because the @finally might
1377 // have a landing pad (which would overwrite the exception slot).
Chris Lattner2acc6e32011-07-18 04:24:23 +00001378 llvm::FunctionType *rethrowFnTy =
John McCallf1549f62010-07-06 01:34:17 +00001379 cast<llvm::FunctionType>(
John McCalld768e9d2011-06-22 02:32:12 +00001380 cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1381 SavedExnVar = 0;
1382 if (rethrowFnTy->getNumParams())
1383 SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
Mike Stumpd88ea562009-12-09 03:35:49 +00001384
John McCallf1549f62010-07-06 01:34:17 +00001385 // A finally block is a statement which must be executed on any edge
1386 // out of a given scope. Unlike a cleanup, the finally block may
1387 // contain arbitrary control flow leading out of itself. In
1388 // addition, finally blocks should always be executed, even if there
1389 // are no catch handlers higher on the stack. Therefore, we
1390 // surround the protected scope with a combination of a normal
1391 // cleanup (to catch attempts to break out of the block via normal
1392 // control flow) and an EH catch-all (semantically "outside" any try
1393 // statement to which the finally block might have been attached).
1394 // The finally block itself is generated in the context of a cleanup
1395 // which conditionally leaves the catch-all.
John McCall3d3ec1c2010-04-21 10:05:39 +00001396
John McCallf1549f62010-07-06 01:34:17 +00001397 // Jump destination for performing the finally block on an exception
1398 // edge. We'll never actually reach this block, so unreachable is
1399 // fine.
John McCalld768e9d2011-06-22 02:32:12 +00001400 RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
John McCall3d3ec1c2010-04-21 10:05:39 +00001401
John McCallf1549f62010-07-06 01:34:17 +00001402 // Whether the finally block is being executed for EH purposes.
John McCalld768e9d2011-06-22 02:32:12 +00001403 ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1404 CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar);
Mike Stumpd88ea562009-12-09 03:35:49 +00001405
John McCallf1549f62010-07-06 01:34:17 +00001406 // Enter a normal cleanup which will perform the @finally block.
John McCalld768e9d2011-06-22 02:32:12 +00001407 CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1408 ForEHVar, endCatchFn,
1409 rethrowFn, SavedExnVar);
John McCallf1549f62010-07-06 01:34:17 +00001410
1411 // Enter a catch-all scope.
John McCalld768e9d2011-06-22 02:32:12 +00001412 llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1413 EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1414 catchScope->setCatchAllHandler(0, catchBB);
John McCallf1549f62010-07-06 01:34:17 +00001415}
1416
John McCalld768e9d2011-06-22 02:32:12 +00001417void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
John McCallf1549f62010-07-06 01:34:17 +00001418 // Leave the finally catch-all.
John McCalld768e9d2011-06-22 02:32:12 +00001419 EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1420 llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
John McCall777d6e52011-08-11 02:22:43 +00001421
1422 CGF.popCatchScope();
John McCallf1549f62010-07-06 01:34:17 +00001423
John McCalld768e9d2011-06-22 02:32:12 +00001424 // If there are any references to the catch-all block, emit it.
1425 if (catchBB->use_empty()) {
1426 delete catchBB;
1427 } else {
1428 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1429 CGF.EmitBlock(catchBB);
John McCallf1549f62010-07-06 01:34:17 +00001430
John McCalld768e9d2011-06-22 02:32:12 +00001431 llvm::Value *exn = 0;
John McCallf1549f62010-07-06 01:34:17 +00001432
John McCalld768e9d2011-06-22 02:32:12 +00001433 // If there's a begin-catch function, call it.
1434 if (BeginCatchFn) {
Bill Wendlingae270592011-09-15 18:57:19 +00001435 exn = CGF.getExceptionFromSlot();
John McCalld768e9d2011-06-22 02:32:12 +00001436 CGF.Builder.CreateCall(BeginCatchFn, exn)->setDoesNotThrow();
1437 }
1438
1439 // If we need to remember the exception pointer to rethrow later, do so.
1440 if (SavedExnVar) {
Bill Wendlingae270592011-09-15 18:57:19 +00001441 if (!exn) exn = CGF.getExceptionFromSlot();
John McCalld768e9d2011-06-22 02:32:12 +00001442 CGF.Builder.CreateStore(exn, SavedExnVar);
1443 }
1444
1445 // Tell the cleanups in the finally block that we're do this for EH.
1446 CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar);
1447
1448 // Thread a jump through the finally cleanup.
1449 CGF.EmitBranchThroughCleanup(RethrowDest);
1450
1451 CGF.Builder.restoreIP(savedIP);
1452 }
1453
1454 // Finally, leave the @finally cleanup.
1455 CGF.PopCleanupBlock();
John McCallf1549f62010-07-06 01:34:17 +00001456}
1457
1458llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1459 if (TerminateLandingPad)
1460 return TerminateLandingPad;
1461
1462 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1463
1464 // This will get inserted at the end of the function.
1465 TerminateLandingPad = createBasicBlock("terminate.lpad");
1466 Builder.SetInsertPoint(TerminateLandingPad);
1467
1468 // Tell the backend that this is a landing pad.
John McCall8262b6a2010-07-17 00:43:08 +00001469 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions());
Bill Wendling285cfd82011-09-19 20:31:14 +00001470 llvm::LandingPadInst *LPadInst =
1471 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
1472 getOpaquePersonalityFn(CGM, Personality), 0);
1473 LPadInst->addClause(getCatchAllValue(*this));
John McCallf1549f62010-07-06 01:34:17 +00001474
1475 llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this));
1476 TerminateCall->setDoesNotReturn();
1477 TerminateCall->setDoesNotThrow();
John McCalld16c2cf2011-02-08 08:22:06 +00001478 Builder.CreateUnreachable();
Mike Stumpd88ea562009-12-09 03:35:49 +00001479
John McCallf1549f62010-07-06 01:34:17 +00001480 // Restore the saved insertion state.
1481 Builder.restoreIP(SavedIP);
John McCall891f80e2010-04-30 00:06:43 +00001482
John McCallf1549f62010-07-06 01:34:17 +00001483 return TerminateLandingPad;
Mike Stumpd88ea562009-12-09 03:35:49 +00001484}
Mike Stump9b39c512009-12-09 22:59:31 +00001485
1486llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +00001487 if (TerminateHandler)
1488 return TerminateHandler;
1489
John McCallf1549f62010-07-06 01:34:17 +00001490 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
Mike Stump76958092009-12-09 23:31:35 +00001491
John McCallf1549f62010-07-06 01:34:17 +00001492 // Set up the terminate handler. This block is inserted at the very
1493 // end of the function by FinishFunction.
Mike Stump182f3832009-12-10 00:02:42 +00001494 TerminateHandler = createBasicBlock("terminate.handler");
John McCallf1549f62010-07-06 01:34:17 +00001495 Builder.SetInsertPoint(TerminateHandler);
1496 llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this));
Mike Stump9b39c512009-12-09 22:59:31 +00001497 TerminateCall->setDoesNotReturn();
1498 TerminateCall->setDoesNotThrow();
1499 Builder.CreateUnreachable();
1500
John McCall3d3ec1c2010-04-21 10:05:39 +00001501 // Restore the saved insertion state.
John McCallf1549f62010-07-06 01:34:17 +00001502 Builder.restoreIP(SavedIP);
Mike Stump76958092009-12-09 23:31:35 +00001503
Mike Stump9b39c512009-12-09 22:59:31 +00001504 return TerminateHandler;
1505}
John McCallf1549f62010-07-06 01:34:17 +00001506
John McCall777d6e52011-08-11 02:22:43 +00001507llvm::BasicBlock *CodeGenFunction::getEHResumeBlock() {
1508 if (EHResumeBlock) return EHResumeBlock;
John McCallff8e1152010-07-23 21:56:41 +00001509
1510 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
1511
1512 // We emit a jump to a notional label at the outermost unwind state.
John McCall777d6e52011-08-11 02:22:43 +00001513 EHResumeBlock = createBasicBlock("eh.resume");
1514 Builder.SetInsertPoint(EHResumeBlock);
John McCallff8e1152010-07-23 21:56:41 +00001515
1516 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions());
1517
1518 // This can always be a call because we necessarily didn't find
1519 // anything on the EH stack which needs our help.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001520 StringRef RethrowName = Personality.getCatchallRethrowFnName();
John McCall93c332a2011-05-28 21:13:02 +00001521 if (!RethrowName.empty()) {
1522 Builder.CreateCall(getCatchallRethrowFn(*this, RethrowName),
Bill Wendlingae270592011-09-15 18:57:19 +00001523 getExceptionFromSlot())
John McCall93c332a2011-05-28 21:13:02 +00001524 ->setDoesNotReturn();
1525 } else {
Bill Wendlingae270592011-09-15 18:57:19 +00001526 llvm::Value *Exn = getExceptionFromSlot();
John McCallff8e1152010-07-23 21:56:41 +00001527
John McCall93c332a2011-05-28 21:13:02 +00001528 switch (CleanupHackLevel) {
1529 case CHL_MandatoryCatchall:
1530 // In mandatory-catchall mode, we need to use
1531 // _Unwind_Resume_or_Rethrow, or whatever the personality's
1532 // equivalent is.
1533 Builder.CreateCall(getUnwindResumeOrRethrowFn(), Exn)
1534 ->setDoesNotReturn();
1535 break;
1536 case CHL_MandatoryCleanup: {
Bill Wendling285cfd82011-09-19 20:31:14 +00001537 // In mandatory-cleanup mode, we should use 'resume'.
1538
1539 // Recreate the landingpad's return value for the 'resume' instruction.
1540 llvm::Value *Exn = getExceptionFromSlot();
1541 llvm::Value *Sel = getSelectorFromSlot();
1542
1543 llvm::Type *LPadType = llvm::StructType::get(Exn->getType(),
1544 Sel->getType(), NULL);
1545 llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1546 LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1547 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1548
1549 Builder.CreateResume(LPadVal);
1550 Builder.restoreIP(SavedIP);
1551 return EHResumeBlock;
John McCall93c332a2011-05-28 21:13:02 +00001552 }
1553 case CHL_Ideal:
1554 // In an idealized mode where we don't have to worry about the
1555 // optimizer combining landing pads, we should just use
1556 // _Unwind_Resume (or the personality's equivalent).
1557 Builder.CreateCall(getUnwindResumeFn(), Exn)
1558 ->setDoesNotReturn();
1559 break;
1560 }
1561 }
1562
John McCallff8e1152010-07-23 21:56:41 +00001563 Builder.CreateUnreachable();
1564
1565 Builder.restoreIP(SavedIP);
1566
John McCall777d6e52011-08-11 02:22:43 +00001567 return EHResumeBlock;
John McCallff8e1152010-07-23 21:56:41 +00001568}