blob: a8d7c5a78d29fa51e4c779928ef1e5e7544f5818 [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.
252 llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(Val);
John McCallb2593832010-09-16 06:16:50 +0000253
Bill Wendling40ccacc2011-09-19 22:08:36 +0000254 // ObjC EH selector entries are always global variables with
255 // names starting like this.
256 if (GV->getName().startswith("OBJC_EHTYPE"))
257 return false;
258 } else {
259 // Check if any of the filter values have the ObjC prefix.
260 llvm::Constant *CVal = cast<llvm::Constant>(Val);
261 for (llvm::User::op_iterator
262 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
263 llvm::GlobalVariable *GV =
264 cast<llvm::GlobalVariable>((*II)->stripPointerCasts());
265
266 // ObjC EH selector entries are always global variables with
267 // names starting like this.
268 if (GV->getName().startswith("OBJC_EHTYPE"))
269 return false;
270 }
271 }
John McCallb2593832010-09-16 06:16:50 +0000272 }
273 }
274
275 return true;
276}
277
278/// Try to use the C++ personality function in ObjC++. Not doing this
279/// can cause some incompatibilities with gcc, which is more
280/// aggressive about only using the ObjC++ personality in a function
281/// when it really needs it.
282void CodeGenModule::SimplifyPersonality() {
283 // For now, this is really a Darwin-specific operation.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000284 if (!Context.getTargetInfo().getTriple().isOSDarwin())
John McCallb2593832010-09-16 06:16:50 +0000285 return;
286
287 // If we're not in ObjC++ -fexceptions, there's nothing to do.
288 if (!Features.CPlusPlus || !Features.ObjC1 || !Features.Exceptions)
289 return;
290
291 const EHPersonality &ObjCXX = EHPersonality::get(Features);
292 const EHPersonality &CXX = getCXXPersonality(Features);
293 if (&ObjCXX == &CXX ||
294 ObjCXX.getPersonalityFnName() == CXX.getPersonalityFnName())
295 return;
296
297 llvm::Function *Fn =
298 getModule().getFunction(ObjCXX.getPersonalityFnName());
299
300 // Nothing to do if it's unused.
301 if (!Fn || Fn->use_empty()) return;
302
303 // Can't do the optimization if it has non-C++ uses.
304 if (!PersonalityHasOnlyCXXUses(Fn)) return;
305
306 // Create the C++ personality function and kill off the old
307 // function.
308 llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
309
310 // This can happen if the user is screwing with us.
311 if (Fn->getType() != CXXFn->getType()) return;
312
313 Fn->replaceAllUsesWith(CXXFn);
314 Fn->eraseFromParent();
John McCallf1549f62010-07-06 01:34:17 +0000315}
316
317/// Returns the value to inject into a selector to indicate the
318/// presence of a catch-all.
319static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
320 // Possibly we should use @llvm.eh.catch.all.value here.
John McCalld16c2cf2011-02-08 08:22:06 +0000321 return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
John McCallf1549f62010-07-06 01:34:17 +0000322}
323
John McCall09faeab2010-07-13 21:17:51 +0000324namespace {
325 /// A cleanup to free the exception object if its initialization
326 /// throws.
John McCallc4a1a842011-07-12 00:15:30 +0000327 struct FreeException : EHScopeStack::Cleanup {
328 llvm::Value *exn;
329 FreeException(llvm::Value *exn) : exn(exn) {}
John McCallad346f42011-07-12 20:27:29 +0000330 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall3ad32c82011-01-28 08:37:24 +0000331 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), exn)
John McCall09faeab2010-07-13 21:17:51 +0000332 ->setDoesNotThrow();
John McCall09faeab2010-07-13 21:17:51 +0000333 }
334 };
335}
336
John McCallac418162010-04-22 01:10:34 +0000337// Emits an exception expression into the given location. This
338// differs from EmitAnyExprToMem only in that, if a final copy-ctor
339// call is required, an exception within that copy ctor causes
340// std::terminate to be invoked.
John McCall3ad32c82011-01-28 08:37:24 +0000341static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e,
342 llvm::Value *addr) {
John McCallf1549f62010-07-06 01:34:17 +0000343 // Make sure the exception object is cleaned up if there's an
344 // exception during initialization.
John McCall3ad32c82011-01-28 08:37:24 +0000345 CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr);
346 EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin();
John McCallac418162010-04-22 01:10:34 +0000347
348 // __cxa_allocate_exception returns a void*; we need to cast this
349 // to the appropriate type for the object.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000350 llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo();
John McCall3ad32c82011-01-28 08:37:24 +0000351 llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty);
John McCallac418162010-04-22 01:10:34 +0000352
353 // FIXME: this isn't quite right! If there's a final unelided call
354 // to a copy constructor, then according to [except.terminate]p1 we
355 // must call std::terminate() if that constructor throws, because
356 // technically that copy occurs after the exception expression is
357 // evaluated but before the exception is caught. But the best way
358 // to handle that is to teach EmitAggExpr to do the final copy
359 // differently if it can't be elided.
John McCallf85e1932011-06-15 23:02:42 +0000360 CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
361 /*IsInit*/ true);
John McCallac418162010-04-22 01:10:34 +0000362
John McCall3ad32c82011-01-28 08:37:24 +0000363 // Deactivate the cleanup block.
364 CGF.DeactivateCleanupBlock(cleanup);
Mike Stump0f590be2009-12-01 03:41:18 +0000365}
366
John McCallf1549f62010-07-06 01:34:17 +0000367llvm::Value *CodeGenFunction::getExceptionSlot() {
John McCall93c332a2011-05-28 21:13:02 +0000368 if (!ExceptionSlot)
369 ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
John McCallf1549f62010-07-06 01:34:17 +0000370 return ExceptionSlot;
Mike Stump0f590be2009-12-01 03:41:18 +0000371}
372
John McCall93c332a2011-05-28 21:13:02 +0000373llvm::Value *CodeGenFunction::getEHSelectorSlot() {
374 if (!EHSelectorSlot)
375 EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
376 return EHSelectorSlot;
377}
378
Bill Wendlingae270592011-09-15 18:57:19 +0000379llvm::Value *CodeGenFunction::getExceptionFromSlot() {
380 return Builder.CreateLoad(getExceptionSlot(), "exn");
381}
382
383llvm::Value *CodeGenFunction::getSelectorFromSlot() {
384 return Builder.CreateLoad(getEHSelectorSlot(), "sel");
385}
386
Anders Carlsson756b5c42009-10-30 01:42:31 +0000387void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000388 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000389 if (getInvokeDest()) {
John McCallf1549f62010-07-06 01:34:17 +0000390 Builder.CreateInvoke(getReThrowFn(*this),
391 getUnreachableBlock(),
392 getInvokeDest())
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000393 ->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000394 } else {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000395 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000396 Builder.CreateUnreachable();
397 }
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000398
John McCallcd5b22e2011-01-12 03:41:02 +0000399 // throw is an expression, and the expression emitters expect us
400 // to leave ourselves at a valid insertion point.
401 EmitBlock(createBasicBlock("throw.cont"));
402
Anders Carlssond3379292009-10-30 02:27:02 +0000403 return;
404 }
Mike Stump8755ec32009-12-10 00:06:18 +0000405
Anders Carlssond3379292009-10-30 02:27:02 +0000406 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000407
Anders Carlssond3379292009-10-30 02:27:02 +0000408 // Now allocate the exception object.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000409 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000410 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000411
Anders Carlssond3379292009-10-30 02:27:02 +0000412 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
John McCallf1549f62010-07-06 01:34:17 +0000413 llvm::CallInst *ExceptionPtr =
Mike Stump8755ec32009-12-10 00:06:18 +0000414 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000415 llvm::ConstantInt::get(SizeTy, TypeSize),
416 "exception");
John McCallf1549f62010-07-06 01:34:17 +0000417 ExceptionPtr->setDoesNotThrow();
Anders Carlsson8370c582009-12-11 00:32:37 +0000418
John McCallac418162010-04-22 01:10:34 +0000419 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000420
Anders Carlssond3379292009-10-30 02:27:02 +0000421 // Now throw the exception.
Anders Carlsson82a113a2011-01-24 01:59:49 +0000422 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
423 /*ForEH=*/true);
John McCallac418162010-04-22 01:10:34 +0000424
425 // The address of the destructor. If the exception type has a
426 // trivial destructor (or isn't a record), we just pass null.
427 llvm::Constant *Dtor = 0;
428 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
429 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
430 if (!Record->hasTrivialDestructor()) {
Douglas Gregor1d110e02010-07-01 14:13:13 +0000431 CXXDestructorDecl *DtorD = Record->getDestructor();
John McCallac418162010-04-22 01:10:34 +0000432 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
433 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
434 }
435 }
436 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000437
Mike Stump0a3816e2009-12-04 01:51:45 +0000438 if (getInvokeDest()) {
Mike Stump8755ec32009-12-10 00:06:18 +0000439 llvm::InvokeInst *ThrowCall =
John McCallf1549f62010-07-06 01:34:17 +0000440 Builder.CreateInvoke3(getThrowFn(*this),
441 getUnreachableBlock(), getInvokeDest(),
Mike Stump0a3816e2009-12-04 01:51:45 +0000442 ExceptionPtr, TypeInfo, Dtor);
443 ThrowCall->setDoesNotReturn();
Mike Stump0a3816e2009-12-04 01:51:45 +0000444 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000445 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000446 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
447 ThrowCall->setDoesNotReturn();
John McCallf1549f62010-07-06 01:34:17 +0000448 Builder.CreateUnreachable();
Mike Stump0a3816e2009-12-04 01:51:45 +0000449 }
Mike Stump8755ec32009-12-10 00:06:18 +0000450
John McCallcd5b22e2011-01-12 03:41:02 +0000451 // throw is an expression, and the expression emitters expect us
452 // to leave ourselves at a valid insertion point.
453 EmitBlock(createBasicBlock("throw.cont"));
Anders Carlsson756b5c42009-10-30 01:42:31 +0000454}
Mike Stump2bf701e2009-11-20 23:44:51 +0000455
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000456void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlsson15348ae2011-02-28 02:27:16 +0000457 if (!CGM.getLangOptions().CXXExceptions)
Anders Carlssona994ee42010-02-06 23:59:05 +0000458 return;
459
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000460 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
461 if (FD == 0)
462 return;
463 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
464 if (Proto == 0)
465 return;
466
Sebastian Redla968e972011-03-15 18:42:48 +0000467 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
468 if (isNoexceptExceptionSpec(EST)) {
469 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
470 // noexcept functions are simple terminate scopes.
471 EHStack.pushTerminate();
472 }
473 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
474 unsigned NumExceptions = Proto->getNumExceptions();
475 EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000476
Sebastian Redla968e972011-03-15 18:42:48 +0000477 for (unsigned I = 0; I != NumExceptions; ++I) {
478 QualType Ty = Proto->getExceptionType(I);
479 QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
480 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
481 /*ForEH=*/true);
482 Filter->setFilter(I, EHType);
483 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000484 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000485}
486
John McCall777d6e52011-08-11 02:22:43 +0000487/// Emit the dispatch block for a filter scope if necessary.
488static void emitFilterDispatchBlock(CodeGenFunction &CGF,
489 EHFilterScope &filterScope) {
490 llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
491 if (!dispatchBlock) return;
492 if (dispatchBlock->use_empty()) {
493 delete dispatchBlock;
494 return;
495 }
496
John McCall777d6e52011-08-11 02:22:43 +0000497 CGF.EmitBlockAfterUses(dispatchBlock);
498
499 // If this isn't a catch-all filter, we need to check whether we got
500 // here because the filter triggered.
501 if (filterScope.getNumFilters()) {
502 // Load the selector value.
Bill Wendlingae270592011-09-15 18:57:19 +0000503 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall777d6e52011-08-11 02:22:43 +0000504 llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
505
506 llvm::Value *zero = CGF.Builder.getInt32(0);
507 llvm::Value *failsFilter =
508 CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
509 CGF.Builder.CreateCondBr(failsFilter, unexpectedBB, CGF.getEHResumeBlock());
510
511 CGF.EmitBlock(unexpectedBB);
512 }
513
514 // Call __cxa_call_unexpected. This doesn't need to be an invoke
515 // because __cxa_call_unexpected magically filters exceptions
516 // according to the last landing pad the exception was thrown
517 // into. Seriously.
Bill Wendlingae270592011-09-15 18:57:19 +0000518 llvm::Value *exn = CGF.getExceptionFromSlot();
John McCall777d6e52011-08-11 02:22:43 +0000519 CGF.Builder.CreateCall(getUnexpectedFn(CGF), exn)
520 ->setDoesNotReturn();
521 CGF.Builder.CreateUnreachable();
522}
523
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000524void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlsson15348ae2011-02-28 02:27:16 +0000525 if (!CGM.getLangOptions().CXXExceptions)
Anders Carlssona994ee42010-02-06 23:59:05 +0000526 return;
527
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000528 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
529 if (FD == 0)
530 return;
531 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
532 if (Proto == 0)
533 return;
534
Sebastian Redla968e972011-03-15 18:42:48 +0000535 ExceptionSpecificationType EST = Proto->getExceptionSpecType();
536 if (isNoexceptExceptionSpec(EST)) {
537 if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
538 EHStack.popTerminate();
539 }
540 } else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
John McCall777d6e52011-08-11 02:22:43 +0000541 EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
542 emitFilterDispatchBlock(*this, filterScope);
Sebastian Redla968e972011-03-15 18:42:48 +0000543 EHStack.popFilter();
544 }
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000545}
546
Mike Stump2bf701e2009-11-20 23:44:51 +0000547void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall59a70002010-07-07 06:56:46 +0000548 EnterCXXTryStmt(S);
John McCall9fc6a772010-02-19 09:25:03 +0000549 EmitStmt(S.getTryBlock());
John McCall59a70002010-07-07 06:56:46 +0000550 ExitCXXTryStmt(S);
John McCall9fc6a772010-02-19 09:25:03 +0000551}
552
John McCall59a70002010-07-07 06:56:46 +0000553void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallf1549f62010-07-06 01:34:17 +0000554 unsigned NumHandlers = S.getNumHandlers();
555 EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
John McCall9fc6a772010-02-19 09:25:03 +0000556
John McCallf1549f62010-07-06 01:34:17 +0000557 for (unsigned I = 0; I != NumHandlers; ++I) {
558 const CXXCatchStmt *C = S.getHandler(I);
John McCall9fc6a772010-02-19 09:25:03 +0000559
John McCallf1549f62010-07-06 01:34:17 +0000560 llvm::BasicBlock *Handler = createBasicBlock("catch");
561 if (C->getExceptionDecl()) {
562 // FIXME: Dropping the reference type on the type into makes it
563 // impossible to correctly implement catch-by-reference
564 // semantics for pointers. Unfortunately, this is what all
565 // existing compilers do, and it's not clear that the standard
566 // personality routine is capable of doing this right. See C++ DR 388:
567 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
568 QualType CaughtType = C->getCaughtType();
569 CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType();
John McCall5a180392010-07-24 00:37:23 +0000570
571 llvm::Value *TypeInfo = 0;
572 if (CaughtType->isObjCObjectPointerType())
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +0000573 TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
John McCall5a180392010-07-24 00:37:23 +0000574 else
Anders Carlsson82a113a2011-01-24 01:59:49 +0000575 TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true);
John McCallf1549f62010-07-06 01:34:17 +0000576 CatchScope->setHandler(I, TypeInfo, Handler);
577 } else {
578 // No exception decl indicates '...', a catch-all.
579 CatchScope->setCatchAllHandler(I, Handler);
580 }
581 }
John McCallf1549f62010-07-06 01:34:17 +0000582}
583
John McCall777d6e52011-08-11 02:22:43 +0000584llvm::BasicBlock *
585CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
586 // The dispatch block for the end of the scope chain is a block that
587 // just resumes unwinding.
588 if (si == EHStack.stable_end())
589 return getEHResumeBlock();
590
591 // Otherwise, we should look at the actual scope.
592 EHScope &scope = *EHStack.find(si);
593
594 llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
595 if (!dispatchBlock) {
596 switch (scope.getKind()) {
597 case EHScope::Catch: {
598 // Apply a special case to a single catch-all.
599 EHCatchScope &catchScope = cast<EHCatchScope>(scope);
600 if (catchScope.getNumHandlers() == 1 &&
601 catchScope.getHandler(0).isCatchAll()) {
602 dispatchBlock = catchScope.getHandler(0).Block;
603
604 // Otherwise, make a dispatch block.
605 } else {
606 dispatchBlock = createBasicBlock("catch.dispatch");
607 }
608 break;
609 }
610
611 case EHScope::Cleanup:
612 dispatchBlock = createBasicBlock("ehcleanup");
613 break;
614
615 case EHScope::Filter:
616 dispatchBlock = createBasicBlock("filter.dispatch");
617 break;
618
619 case EHScope::Terminate:
620 dispatchBlock = getTerminateHandler();
621 break;
622 }
623 scope.setCachedEHDispatchBlock(dispatchBlock);
624 }
625 return dispatchBlock;
626}
627
John McCallf1549f62010-07-06 01:34:17 +0000628/// Check whether this is a non-EH scope, i.e. a scope which doesn't
629/// affect exception handling. Currently, the only non-EH scopes are
630/// normal-only cleanup scopes.
631static bool isNonEHScope(const EHScope &S) {
John McCallda65ea82010-07-13 20:32:21 +0000632 switch (S.getKind()) {
John McCall1f0fca52010-07-21 07:22:38 +0000633 case EHScope::Cleanup:
634 return !cast<EHCleanupScope>(S).isEHCleanup();
John McCallda65ea82010-07-13 20:32:21 +0000635 case EHScope::Filter:
636 case EHScope::Catch:
637 case EHScope::Terminate:
638 return false;
639 }
640
641 // Suppress warning.
642 return false;
John McCallf1549f62010-07-06 01:34:17 +0000643}
644
645llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
646 assert(EHStack.requiresLandingPad());
647 assert(!EHStack.empty());
648
Anders Carlsson7a178512011-02-28 00:33:03 +0000649 if (!CGM.getLangOptions().Exceptions)
John McCallda65ea82010-07-13 20:32:21 +0000650 return 0;
651
John McCallf1549f62010-07-06 01:34:17 +0000652 // Check the innermost scope for a cached landing pad. If this is
653 // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
654 llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
655 if (LP) return LP;
656
657 // Build the landing pad for this scope.
658 LP = EmitLandingPad();
659 assert(LP);
660
661 // Cache the landing pad on the innermost scope. If this is a
662 // non-EH scope, cache the landing pad on the enclosing scope, too.
663 for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
664 ir->setCachedLandingPad(LP);
665 if (!isNonEHScope(*ir)) break;
666 }
667
668 return LP;
669}
670
John McCall93c332a2011-05-28 21:13:02 +0000671// This code contains a hack to work around a design flaw in
672// LLVM's EH IR which breaks semantics after inlining. This same
673// hack is implemented in llvm-gcc.
674//
675// The LLVM EH abstraction is basically a thin veneer over the
676// traditional GCC zero-cost design: for each range of instructions
677// in the function, there is (at most) one "landing pad" with an
678// associated chain of EH actions. A language-specific personality
679// function interprets this chain of actions and (1) decides whether
680// or not to resume execution at the landing pad and (2) if so,
681// provides an integer indicating why it's stopping. In LLVM IR,
682// the association of a landing pad with a range of instructions is
683// achieved via an invoke instruction, the chain of actions becomes
684// the arguments to the @llvm.eh.selector call, and the selector
685// call returns the integer indicator. Other than the required
686// presence of two intrinsic function calls in the landing pad,
687// the IR exactly describes the layout of the output code.
688//
689// A principal advantage of this design is that it is completely
690// language-agnostic; in theory, the LLVM optimizers can treat
691// landing pads neutrally, and targets need only know how to lower
692// the intrinsics to have a functioning exceptions system (assuming
693// that platform exceptions follow something approximately like the
694// GCC design). Unfortunately, landing pads cannot be combined in a
695// language-agnostic way: given selectors A and B, there is no way
696// to make a single landing pad which faithfully represents the
697// semantics of propagating an exception first through A, then
698// through B, without knowing how the personality will interpret the
699// (lowered form of the) selectors. This means that inlining has no
700// choice but to crudely chain invokes (i.e., to ignore invokes in
701// the inlined function, but to turn all unwindable calls into
702// invokes), which is only semantically valid if every unwind stops
703// at every landing pad.
704//
705// Therefore, the invoke-inline hack is to guarantee that every
706// landing pad has a catch-all.
707enum CleanupHackLevel_t {
708 /// A level of hack that requires that all landing pads have
709 /// catch-alls.
710 CHL_MandatoryCatchall,
711
712 /// A level of hack that requires that all landing pads handle
713 /// cleanups.
714 CHL_MandatoryCleanup,
715
716 /// No hacks at all; ideal IR generation.
717 CHL_Ideal
718};
719const CleanupHackLevel_t CleanupHackLevel = CHL_MandatoryCleanup;
720
John McCallf1549f62010-07-06 01:34:17 +0000721llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
722 assert(EHStack.requiresLandingPad());
723
John McCall777d6e52011-08-11 02:22:43 +0000724 EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
725 switch (innermostEHScope.getKind()) {
726 case EHScope::Terminate:
727 return getTerminateLandingPad();
John McCallf1549f62010-07-06 01:34:17 +0000728
John McCall777d6e52011-08-11 02:22:43 +0000729 case EHScope::Catch:
730 case EHScope::Cleanup:
731 case EHScope::Filter:
732 if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
733 return lpad;
John McCallf1549f62010-07-06 01:34:17 +0000734 }
735
736 // Save the current IR generation state.
John McCall777d6e52011-08-11 02:22:43 +0000737 CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
John McCallf1549f62010-07-06 01:34:17 +0000738
John McCall777d6e52011-08-11 02:22:43 +0000739 const EHPersonality &personality = EHPersonality::get(getLangOptions());
John McCall8262b6a2010-07-17 00:43:08 +0000740
John McCallf1549f62010-07-06 01:34:17 +0000741 // Create and configure the landing pad.
John McCall777d6e52011-08-11 02:22:43 +0000742 llvm::BasicBlock *lpad = createBasicBlock("lpad");
743 EmitBlock(lpad);
John McCallf1549f62010-07-06 01:34:17 +0000744
Bill Wendling285cfd82011-09-19 20:31:14 +0000745 llvm::LandingPadInst *LPadInst =
746 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
747 getOpaquePersonalityFn(CGM, personality), 0);
748
749 llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
750 Builder.CreateStore(LPadExn, getExceptionSlot());
751 llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
752 Builder.CreateStore(LPadSel, getEHSelectorSlot());
753
John McCallf1549f62010-07-06 01:34:17 +0000754 // Save the exception pointer. It's safe to use a single exception
755 // pointer per function because EH cleanups can never have nested
756 // try/catches.
Bill Wendling285cfd82011-09-19 20:31:14 +0000757 // Build the landingpad instruction.
John McCallf1549f62010-07-06 01:34:17 +0000758
759 // Accumulate all the handlers in scope.
John McCall777d6e52011-08-11 02:22:43 +0000760 bool hasCatchAll = false;
761 bool hasCleanup = false;
762 bool hasFilter = false;
763 SmallVector<llvm::Value*, 4> filterTypes;
764 llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
John McCallf1549f62010-07-06 01:34:17 +0000765 for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end();
766 I != E; ++I) {
767
768 switch (I->getKind()) {
John McCall1f0fca52010-07-21 07:22:38 +0000769 case EHScope::Cleanup:
John McCall777d6e52011-08-11 02:22:43 +0000770 // If we have a cleanup, remember that.
771 hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
John McCallda65ea82010-07-13 20:32:21 +0000772 continue;
773
John McCallf1549f62010-07-06 01:34:17 +0000774 case EHScope::Filter: {
775 assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
John McCall777d6e52011-08-11 02:22:43 +0000776 assert(!hasCatchAll && "EH filter reached after catch-all");
John McCallf1549f62010-07-06 01:34:17 +0000777
Bill Wendling285cfd82011-09-19 20:31:14 +0000778 // Filter scopes get added to the landingpad in weird ways.
John McCall777d6e52011-08-11 02:22:43 +0000779 EHFilterScope &filter = cast<EHFilterScope>(*I);
780 hasFilter = true;
John McCallf1549f62010-07-06 01:34:17 +0000781
782 // Add all the filter values which we aren't already explicitly
783 // catching.
John McCall777d6e52011-08-11 02:22:43 +0000784 for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i) {
785 llvm::Value *filterType = filter.getFilter(i);
786 if (!catchTypes.count(filterType))
787 filterTypes.push_back(filterType);
John McCallf1549f62010-07-06 01:34:17 +0000788 }
789 goto done;
790 }
791
792 case EHScope::Terminate:
793 // Terminate scopes are basically catch-alls.
John McCall777d6e52011-08-11 02:22:43 +0000794 assert(!hasCatchAll);
795 hasCatchAll = true;
John McCallf1549f62010-07-06 01:34:17 +0000796 goto done;
797
798 case EHScope::Catch:
799 break;
800 }
801
John McCall777d6e52011-08-11 02:22:43 +0000802 EHCatchScope &catchScope = cast<EHCatchScope>(*I);
803 for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
804 EHCatchScope::Handler handler = catchScope.getHandler(hi);
John McCallf1549f62010-07-06 01:34:17 +0000805
John McCall777d6e52011-08-11 02:22:43 +0000806 // If this is a catch-all, register that and abort.
807 if (!handler.Type) {
808 assert(!hasCatchAll);
809 hasCatchAll = true;
810 goto done;
John McCallf1549f62010-07-06 01:34:17 +0000811 }
812
813 // Check whether we already have a handler for this type.
Bill Wendling285cfd82011-09-19 20:31:14 +0000814 if (catchTypes.insert(handler.Type))
815 // If not, add it directly to the landingpad.
816 LPadInst->addClause(handler.Type);
John McCallf1549f62010-07-06 01:34:17 +0000817 }
John McCallf1549f62010-07-06 01:34:17 +0000818 }
819
820 done:
Bill Wendling285cfd82011-09-19 20:31:14 +0000821 // If we have a catch-all, add null to the landingpad.
John McCall777d6e52011-08-11 02:22:43 +0000822 assert(!(hasCatchAll && hasFilter));
823 if (hasCatchAll) {
Bill Wendling285cfd82011-09-19 20:31:14 +0000824 LPadInst->addClause(getCatchAllValue(*this));
John McCallf1549f62010-07-06 01:34:17 +0000825
826 // If we have an EH filter, we need to add those handlers in the
Bill Wendling285cfd82011-09-19 20:31:14 +0000827 // right place in the landingpad, which is to say, at the end.
John McCall777d6e52011-08-11 02:22:43 +0000828 } else if (hasFilter) {
Bill Wendling40ccacc2011-09-19 22:08:36 +0000829 // Create a filter expression: a constant array indicating which filter
830 // types there are. The personality routine only lands here if the filter
831 // doesn't match.
Bill Wendling285cfd82011-09-19 20:31:14 +0000832 llvm::SmallVector<llvm::Constant*, 8> Filters;
833 llvm::ArrayType *AType =
834 llvm::ArrayType::get(!filterTypes.empty() ?
835 filterTypes[0]->getType() : Int8PtrTy,
836 filterTypes.size());
837
838 for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
839 Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
840 llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
841 LPadInst->addClause(FilterArray);
John McCallf1549f62010-07-06 01:34:17 +0000842
843 // Also check whether we need a cleanup.
Bill Wendling285cfd82011-09-19 20:31:14 +0000844 if (hasCleanup)
845 LPadInst->setCleanup(true);
John McCallf1549f62010-07-06 01:34:17 +0000846
847 // Otherwise, signal that we at least have cleanups.
John McCall777d6e52011-08-11 02:22:43 +0000848 } else if (CleanupHackLevel == CHL_MandatoryCatchall || hasCleanup) {
Bill Wendling285cfd82011-09-19 20:31:14 +0000849 if (CleanupHackLevel == CHL_MandatoryCatchall)
850 LPadInst->addClause(getCatchAllValue(*this));
851 else
852 LPadInst->setCleanup(true);
John McCallf1549f62010-07-06 01:34:17 +0000853 }
854
Bill Wendling285cfd82011-09-19 20:31:14 +0000855 assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
856 "landingpad instruction has no clauses!");
John McCallf1549f62010-07-06 01:34:17 +0000857
858 // Tell the backend how to generate the landing pad.
John McCall777d6e52011-08-11 02:22:43 +0000859 Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
John McCallf1549f62010-07-06 01:34:17 +0000860
861 // Restore the old IR generation state.
John McCall777d6e52011-08-11 02:22:43 +0000862 Builder.restoreIP(savedIP);
John McCallf1549f62010-07-06 01:34:17 +0000863
John McCall777d6e52011-08-11 02:22:43 +0000864 return lpad;
John McCallf1549f62010-07-06 01:34:17 +0000865}
866
John McCall8e3f8612010-07-13 22:12:14 +0000867namespace {
868 /// A cleanup to call __cxa_end_catch. In many cases, the caught
869 /// exception type lets us state definitively that the thrown exception
870 /// type does not have a destructor. In particular:
871 /// - Catch-alls tell us nothing, so we have to conservatively
872 /// assume that the thrown exception might have a destructor.
873 /// - Catches by reference behave according to their base types.
874 /// - Catches of non-record types will only trigger for exceptions
875 /// of non-record types, which never have destructors.
876 /// - Catches of record types can trigger for arbitrary subclasses
877 /// of the caught type, so we have to assume the actual thrown
878 /// exception type might have a throwing destructor, even if the
879 /// caught type's destructor is trivial or nothrow.
John McCall1f0fca52010-07-21 07:22:38 +0000880 struct CallEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +0000881 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
882 bool MightThrow;
883
John McCallad346f42011-07-12 20:27:29 +0000884 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall8e3f8612010-07-13 22:12:14 +0000885 if (!MightThrow) {
886 CGF.Builder.CreateCall(getEndCatchFn(CGF))->setDoesNotThrow();
887 return;
888 }
889
Jay Foad4c7d9f12011-07-15 08:37:34 +0000890 CGF.EmitCallOrInvoke(getEndCatchFn(CGF));
John McCall8e3f8612010-07-13 22:12:14 +0000891 }
892 };
893}
894
John McCallf1549f62010-07-06 01:34:17 +0000895/// Emits a call to __cxa_begin_catch and enters a cleanup to call
896/// __cxa_end_catch.
John McCall8e3f8612010-07-13 22:12:14 +0000897///
898/// \param EndMightThrow - true if __cxa_end_catch might throw
899static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
900 llvm::Value *Exn,
901 bool EndMightThrow) {
John McCallf1549f62010-07-06 01:34:17 +0000902 llvm::CallInst *Call = CGF.Builder.CreateCall(getBeginCatchFn(CGF), Exn);
903 Call->setDoesNotThrow();
904
John McCall1f0fca52010-07-21 07:22:38 +0000905 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
John McCallf1549f62010-07-06 01:34:17 +0000906
907 return Call;
908}
909
910/// A "special initializer" callback for initializing a catch
911/// parameter during catch initialization.
912static void InitCatchParam(CodeGenFunction &CGF,
913 const VarDecl &CatchParam,
914 llvm::Value *ParamAddr) {
915 // Load the exception from where the landing pad saved it.
Bill Wendlingae270592011-09-15 18:57:19 +0000916 llvm::Value *Exn = CGF.getExceptionFromSlot();
John McCallf1549f62010-07-06 01:34:17 +0000917
918 CanQualType CatchType =
919 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000920 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
John McCallf1549f62010-07-06 01:34:17 +0000921
922 // If we're catching by reference, we can just cast the object
923 // pointer to the appropriate pointer.
924 if (isa<ReferenceType>(CatchType)) {
John McCall204b0752010-07-20 22:17:55 +0000925 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
926 bool EndCatchMightThrow = CaughtType->isRecordType();
John McCall8e3f8612010-07-13 22:12:14 +0000927
John McCallf1549f62010-07-06 01:34:17 +0000928 // __cxa_begin_catch returns the adjusted object pointer.
John McCall8e3f8612010-07-13 22:12:14 +0000929 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
John McCall204b0752010-07-20 22:17:55 +0000930
931 // We have no way to tell the personality function that we're
932 // catching by reference, so if we're catching a pointer,
933 // __cxa_begin_catch will actually return that pointer by value.
934 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
935 QualType PointeeType = PT->getPointeeType();
936
937 // When catching by reference, generally we should just ignore
938 // this by-value pointer and use the exception object instead.
939 if (!PointeeType->isRecordType()) {
940
941 // Exn points to the struct _Unwind_Exception header, which
942 // we have to skip past in order to reach the exception data.
943 unsigned HeaderSize =
944 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
945 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
946
947 // However, if we're catching a pointer-to-record type that won't
948 // work, because the personality function might have adjusted
949 // the pointer. There's actually no way for us to fully satisfy
950 // the language/ABI contract here: we can't use Exn because it
951 // might have the wrong adjustment, but we can't use the by-value
952 // pointer because it's off by a level of abstraction.
953 //
954 // The current solution is to dump the adjusted pointer into an
955 // alloca, which breaks language semantics (because changing the
956 // pointer doesn't change the exception) but at least works.
957 // The better solution would be to filter out non-exact matches
958 // and rethrow them, but this is tricky because the rethrow
959 // really needs to be catchable by other sites at this landing
960 // pad. The best solution is to fix the personality function.
961 } else {
962 // Pull the pointer for the reference type off.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000963 llvm::Type *PtrTy =
John McCall204b0752010-07-20 22:17:55 +0000964 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
965
966 // Create the temporary and write the adjusted pointer into it.
967 llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
968 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
969 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
970
971 // Bind the reference to the temporary.
972 AdjustedExn = ExnPtrTmp;
973 }
974 }
975
John McCallf1549f62010-07-06 01:34:17 +0000976 llvm::Value *ExnCast =
977 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
978 CGF.Builder.CreateStore(ExnCast, ParamAddr);
979 return;
980 }
981
982 // Non-aggregates (plus complexes).
983 bool IsComplex = false;
984 if (!CGF.hasAggregateLLVMType(CatchType) ||
985 (IsComplex = CatchType->isAnyComplexType())) {
John McCall8e3f8612010-07-13 22:12:14 +0000986 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
John McCallf1549f62010-07-06 01:34:17 +0000987
988 // If the catch type is a pointer type, __cxa_begin_catch returns
989 // the pointer by value.
990 if (CatchType->hasPointerRepresentation()) {
991 llvm::Value *CastExn =
992 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
993 CGF.Builder.CreateStore(CastExn, ParamAddr);
994 return;
995 }
996
997 // Otherwise, it returns a pointer into the exception object.
998
Chris Lattner2acc6e32011-07-18 04:24:23 +0000999 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
John McCallf1549f62010-07-06 01:34:17 +00001000 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
1001
1002 if (IsComplex) {
1003 CGF.StoreComplexToAddr(CGF.LoadComplexFromAddr(Cast, /*volatile*/ false),
1004 ParamAddr, /*volatile*/ false);
1005 } else {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001006 unsigned Alignment =
1007 CGF.getContext().getDeclAlign(&CatchParam).getQuantity();
John McCallf1549f62010-07-06 01:34:17 +00001008 llvm::Value *ExnLoad = CGF.Builder.CreateLoad(Cast, "exn.scalar");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001009 CGF.EmitStoreOfScalar(ExnLoad, ParamAddr, /*volatile*/ false, Alignment,
1010 CatchType);
John McCallf1549f62010-07-06 01:34:17 +00001011 }
1012 return;
1013 }
1014
John McCallacff6962011-02-16 08:39:19 +00001015 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
John McCallf1549f62010-07-06 01:34:17 +00001016
Chris Lattner2acc6e32011-07-18 04:24:23 +00001017 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
John McCallf1549f62010-07-06 01:34:17 +00001018
John McCallacff6962011-02-16 08:39:19 +00001019 // Check for a copy expression. If we don't have a copy expression,
1020 // that means a trivial copy is okay.
John McCalle996ffd2011-02-16 08:02:54 +00001021 const Expr *copyExpr = CatchParam.getInit();
1022 if (!copyExpr) {
John McCallacff6962011-02-16 08:39:19 +00001023 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
1024 llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
1025 CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
John McCallf1549f62010-07-06 01:34:17 +00001026 return;
1027 }
1028
1029 // We have to call __cxa_get_exception_ptr to get the adjusted
1030 // pointer before copying.
John McCalle996ffd2011-02-16 08:02:54 +00001031 llvm::CallInst *rawAdjustedExn =
John McCallf1549f62010-07-06 01:34:17 +00001032 CGF.Builder.CreateCall(getGetExceptionPtrFn(CGF), Exn);
John McCalle996ffd2011-02-16 08:02:54 +00001033 rawAdjustedExn->setDoesNotThrow();
John McCallf1549f62010-07-06 01:34:17 +00001034
John McCalle996ffd2011-02-16 08:02:54 +00001035 // Cast that to the appropriate type.
1036 llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
John McCallf1549f62010-07-06 01:34:17 +00001037
John McCalle996ffd2011-02-16 08:02:54 +00001038 // The copy expression is defined in terms of an OpaqueValueExpr.
1039 // Find it and map it to the adjusted expression.
1040 CodeGenFunction::OpaqueValueMapping
John McCall56ca35d2011-02-17 10:25:35 +00001041 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
1042 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
John McCallf1549f62010-07-06 01:34:17 +00001043
1044 // Call the copy ctor in a terminate scope.
1045 CGF.EHStack.pushTerminate();
John McCalle996ffd2011-02-16 08:02:54 +00001046
1047 // Perform the copy construction.
John McCall7c2349b2011-08-25 20:40:09 +00001048 CGF.EmitAggExpr(copyExpr, AggValueSlot::forAddr(ParamAddr, Qualifiers(),
1049 AggValueSlot::IsNotDestructed,
John McCall13668622011-08-26 00:46:38 +00001050 AggValueSlot::DoesNotNeedGCBarriers,
1051 AggValueSlot::IsNotAliased));
John McCalle996ffd2011-02-16 08:02:54 +00001052
1053 // Leave the terminate scope.
John McCallf1549f62010-07-06 01:34:17 +00001054 CGF.EHStack.popTerminate();
1055
John McCalle996ffd2011-02-16 08:02:54 +00001056 // Undo the opaque value mapping.
1057 opaque.pop();
1058
John McCallf1549f62010-07-06 01:34:17 +00001059 // Finally we can call __cxa_begin_catch.
John McCall8e3f8612010-07-13 22:12:14 +00001060 CallBeginCatch(CGF, Exn, true);
John McCallf1549f62010-07-06 01:34:17 +00001061}
1062
1063/// Begins a catch statement by initializing the catch variable and
1064/// calling __cxa_begin_catch.
John McCalle996ffd2011-02-16 08:02:54 +00001065static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
John McCallf1549f62010-07-06 01:34:17 +00001066 // We have to be very careful with the ordering of cleanups here:
1067 // C++ [except.throw]p4:
1068 // The destruction [of the exception temporary] occurs
1069 // immediately after the destruction of the object declared in
1070 // the exception-declaration in the handler.
1071 //
1072 // So the precise ordering is:
1073 // 1. Construct catch variable.
1074 // 2. __cxa_begin_catch
1075 // 3. Enter __cxa_end_catch cleanup
1076 // 4. Enter dtor cleanup
1077 //
John McCall34695852011-02-22 06:44:22 +00001078 // We do this by using a slightly abnormal initialization process.
1079 // Delegation sequence:
John McCallf1549f62010-07-06 01:34:17 +00001080 // - ExitCXXTryStmt opens a RunCleanupsScope
John McCall34695852011-02-22 06:44:22 +00001081 // - EmitAutoVarAlloca creates the variable and debug info
John McCallf1549f62010-07-06 01:34:17 +00001082 // - InitCatchParam initializes the variable from the exception
John McCall34695852011-02-22 06:44:22 +00001083 // - CallBeginCatch calls __cxa_begin_catch
1084 // - CallBeginCatch enters the __cxa_end_catch cleanup
1085 // - EmitAutoVarCleanups enters the variable destructor cleanup
John McCallf1549f62010-07-06 01:34:17 +00001086 // - EmitCXXTryStmt emits the code for the catch body
1087 // - EmitCXXTryStmt close the RunCleanupsScope
1088
1089 VarDecl *CatchParam = S->getExceptionDecl();
1090 if (!CatchParam) {
Bill Wendlingae270592011-09-15 18:57:19 +00001091 llvm::Value *Exn = CGF.getExceptionFromSlot();
John McCall8e3f8612010-07-13 22:12:14 +00001092 CallBeginCatch(CGF, Exn, true);
John McCallf1549f62010-07-06 01:34:17 +00001093 return;
1094 }
1095
1096 // Emit the local.
John McCall34695852011-02-22 06:44:22 +00001097 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
1098 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF));
1099 CGF.EmitAutoVarCleanups(var);
John McCall9fc6a772010-02-19 09:25:03 +00001100}
1101
John McCallfcd5c0c2010-07-13 22:24:23 +00001102namespace {
John McCall1f0fca52010-07-21 07:22:38 +00001103 struct CallRethrow : EHScopeStack::Cleanup {
John McCallad346f42011-07-12 20:27:29 +00001104 void Emit(CodeGenFunction &CGF, Flags flags) {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001105 CGF.EmitCallOrInvoke(getReThrowFn(CGF));
John McCallfcd5c0c2010-07-13 22:24:23 +00001106 }
1107 };
1108}
1109
John McCall777d6e52011-08-11 02:22:43 +00001110/// Emit the structure of the dispatch block for the given catch scope.
1111/// It is an invariant that the dispatch block already exists.
1112static void emitCatchDispatchBlock(CodeGenFunction &CGF,
1113 EHCatchScope &catchScope) {
1114 llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
1115 assert(dispatchBlock);
1116
1117 // If there's only a single catch-all, getEHDispatchBlock returned
1118 // that catch-all as the dispatch block.
1119 if (catchScope.getNumHandlers() == 1 &&
1120 catchScope.getHandler(0).isCatchAll()) {
1121 assert(dispatchBlock == catchScope.getHandler(0).Block);
1122 return;
1123 }
1124
1125 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
1126 CGF.EmitBlockAfterUses(dispatchBlock);
1127
1128 // Select the right handler.
1129 llvm::Value *llvm_eh_typeid_for =
1130 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
1131
1132 // Load the selector value.
Bill Wendlingae270592011-09-15 18:57:19 +00001133 llvm::Value *selector = CGF.getSelectorFromSlot();
John McCall777d6e52011-08-11 02:22:43 +00001134
1135 // Test against each of the exception types we claim to catch.
1136 for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
1137 assert(i < e && "ran off end of handlers!");
1138 const EHCatchScope::Handler &handler = catchScope.getHandler(i);
1139
1140 llvm::Value *typeValue = handler.Type;
1141 assert(typeValue && "fell into catch-all case!");
1142 typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
1143
1144 // Figure out the next block.
1145 bool nextIsEnd;
1146 llvm::BasicBlock *nextBlock;
1147
1148 // If this is the last handler, we're at the end, and the next
1149 // block is the block for the enclosing EH scope.
1150 if (i + 1 == e) {
1151 nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
1152 nextIsEnd = true;
1153
1154 // If the next handler is a catch-all, we're at the end, and the
1155 // next block is that handler.
1156 } else if (catchScope.getHandler(i+1).isCatchAll()) {
1157 nextBlock = catchScope.getHandler(i+1).Block;
1158 nextIsEnd = true;
1159
1160 // Otherwise, we're not at the end and we need a new block.
1161 } else {
1162 nextBlock = CGF.createBasicBlock("catch.fallthrough");
1163 nextIsEnd = false;
1164 }
1165
1166 // Figure out the catch type's index in the LSDA's type table.
1167 llvm::CallInst *typeIndex =
1168 CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
1169 typeIndex->setDoesNotThrow();
1170
1171 llvm::Value *matchesTypeIndex =
1172 CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
1173 CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
1174
1175 // If the next handler is a catch-all, we're completely done.
1176 if (nextIsEnd) {
1177 CGF.Builder.restoreIP(savedIP);
1178 return;
1179
1180 // Otherwise we need to emit and continue at that block.
1181 } else {
1182 CGF.EmitBlock(nextBlock);
1183 }
1184 }
1185
1186 llvm_unreachable("fell out of loop!");
1187}
1188
1189void CodeGenFunction::popCatchScope() {
1190 EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
1191 if (catchScope.hasEHBranches())
1192 emitCatchDispatchBlock(*this, catchScope);
1193 EHStack.popCatch();
1194}
1195
John McCall59a70002010-07-07 06:56:46 +00001196void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
John McCallf1549f62010-07-06 01:34:17 +00001197 unsigned NumHandlers = S.getNumHandlers();
1198 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
1199 assert(CatchScope.getNumHandlers() == NumHandlers);
Mike Stump2bf701e2009-11-20 23:44:51 +00001200
John McCall777d6e52011-08-11 02:22:43 +00001201 // If the catch was not required, bail out now.
1202 if (!CatchScope.hasEHBranches()) {
1203 EHStack.popCatch();
1204 return;
1205 }
1206
1207 // Emit the structure of the EH dispatch for this catch.
1208 emitCatchDispatchBlock(*this, CatchScope);
1209
John McCallf1549f62010-07-06 01:34:17 +00001210 // Copy the handler blocks off before we pop the EH stack. Emitting
1211 // the handlers might scribble on this memory.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001212 SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers);
John McCallf1549f62010-07-06 01:34:17 +00001213 memcpy(Handlers.data(), CatchScope.begin(),
1214 NumHandlers * sizeof(EHCatchScope::Handler));
John McCall777d6e52011-08-11 02:22:43 +00001215
John McCallf1549f62010-07-06 01:34:17 +00001216 EHStack.popCatch();
Mike Stump2bf701e2009-11-20 23:44:51 +00001217
John McCallf1549f62010-07-06 01:34:17 +00001218 // The fall-through block.
1219 llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
Mike Stump2bf701e2009-11-20 23:44:51 +00001220
John McCallf1549f62010-07-06 01:34:17 +00001221 // We just emitted the body of the try; jump to the continue block.
1222 if (HaveInsertPoint())
1223 Builder.CreateBr(ContBB);
Mike Stump639787c2009-12-02 19:53:57 +00001224
John McCall59a70002010-07-07 06:56:46 +00001225 // Determine if we need an implicit rethrow for all these catch handlers.
1226 bool ImplicitRethrow = false;
1227 if (IsFnTryBlock)
1228 ImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
1229 isa<CXXConstructorDecl>(CurCodeDecl);
1230
John McCall777d6e52011-08-11 02:22:43 +00001231 // Perversely, we emit the handlers backwards precisely because we
1232 // want them to appear in source order. In all of these cases, the
1233 // catch block will have exactly one predecessor, which will be a
1234 // particular block in the catch dispatch. However, in the case of
1235 // a catch-all, one of the dispatch blocks will branch to two
1236 // different handlers, and EmitBlockAfterUses will cause the second
1237 // handler to be moved before the first.
1238 for (unsigned I = NumHandlers; I != 0; --I) {
1239 llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
1240 EmitBlockAfterUses(CatchBlock);
Mike Stump8755ec32009-12-10 00:06:18 +00001241
John McCallf1549f62010-07-06 01:34:17 +00001242 // Catch the exception if this isn't a catch-all.
John McCall777d6e52011-08-11 02:22:43 +00001243 const CXXCatchStmt *C = S.getHandler(I-1);
Mike Stump2bf701e2009-11-20 23:44:51 +00001244
John McCallf1549f62010-07-06 01:34:17 +00001245 // Enter a cleanup scope, including the catch variable and the
1246 // end-catch.
1247 RunCleanupsScope CatchScope(*this);
Mike Stump2bf701e2009-11-20 23:44:51 +00001248
John McCallf1549f62010-07-06 01:34:17 +00001249 // Initialize the catch variable and set up the cleanups.
1250 BeginCatch(*this, C);
1251
John McCall59a70002010-07-07 06:56:46 +00001252 // If there's an implicit rethrow, push a normal "cleanup" to call
John McCallfcd5c0c2010-07-13 22:24:23 +00001253 // _cxa_rethrow. This needs to happen before __cxa_end_catch is
1254 // called, and so it is pushed after BeginCatch.
1255 if (ImplicitRethrow)
John McCall1f0fca52010-07-21 07:22:38 +00001256 EHStack.pushCleanup<CallRethrow>(NormalCleanup);
John McCall59a70002010-07-07 06:56:46 +00001257
John McCallf1549f62010-07-06 01:34:17 +00001258 // Perform the body of the catch.
1259 EmitStmt(C->getHandlerBlock());
1260
1261 // Fall out through the catch cleanups.
1262 CatchScope.ForceCleanup();
1263
1264 // Branch out of the try.
1265 if (HaveInsertPoint())
1266 Builder.CreateBr(ContBB);
Mike Stump2bf701e2009-11-20 23:44:51 +00001267 }
1268
John McCallf1549f62010-07-06 01:34:17 +00001269 EmitBlock(ContBB);
Mike Stump2bf701e2009-11-20 23:44:51 +00001270}
Mike Stumpd88ea562009-12-09 03:35:49 +00001271
John McCall55b20fc2010-07-21 00:52:03 +00001272namespace {
John McCall1f0fca52010-07-21 07:22:38 +00001273 struct CallEndCatchForFinally : EHScopeStack::Cleanup {
John McCall55b20fc2010-07-21 00:52:03 +00001274 llvm::Value *ForEHVar;
1275 llvm::Value *EndCatchFn;
1276 CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
1277 : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
1278
John McCallad346f42011-07-12 20:27:29 +00001279 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall55b20fc2010-07-21 00:52:03 +00001280 llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
1281 llvm::BasicBlock *CleanupContBB =
1282 CGF.createBasicBlock("finally.cleanup.cont");
1283
1284 llvm::Value *ShouldEndCatch =
1285 CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch");
1286 CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
1287 CGF.EmitBlock(EndCatchBB);
Jay Foad4c7d9f12011-07-15 08:37:34 +00001288 CGF.EmitCallOrInvoke(EndCatchFn); // catch-all, so might throw
John McCall55b20fc2010-07-21 00:52:03 +00001289 CGF.EmitBlock(CleanupContBB);
1290 }
1291 };
John McCall77199712010-07-21 05:47:49 +00001292
John McCall1f0fca52010-07-21 07:22:38 +00001293 struct PerformFinally : EHScopeStack::Cleanup {
John McCall77199712010-07-21 05:47:49 +00001294 const Stmt *Body;
1295 llvm::Value *ForEHVar;
1296 llvm::Value *EndCatchFn;
1297 llvm::Value *RethrowFn;
1298 llvm::Value *SavedExnVar;
1299
1300 PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
1301 llvm::Value *EndCatchFn,
1302 llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
1303 : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
1304 RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
1305
John McCallad346f42011-07-12 20:27:29 +00001306 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall77199712010-07-21 05:47:49 +00001307 // Enter a cleanup to call the end-catch function if one was provided.
1308 if (EndCatchFn)
John McCall1f0fca52010-07-21 07:22:38 +00001309 CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
1310 ForEHVar, EndCatchFn);
John McCall77199712010-07-21 05:47:49 +00001311
John McCalld96a8e72010-08-11 00:16:14 +00001312 // Save the current cleanup destination in case there are
1313 // cleanups in the finally block.
1314 llvm::Value *SavedCleanupDest =
1315 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
1316 "cleanup.dest.saved");
1317
John McCall77199712010-07-21 05:47:49 +00001318 // Emit the finally block.
1319 CGF.EmitStmt(Body);
1320
1321 // If the end of the finally is reachable, check whether this was
1322 // for EH. If so, rethrow.
1323 if (CGF.HaveInsertPoint()) {
1324 llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
1325 llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
1326
1327 llvm::Value *ShouldRethrow =
1328 CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow");
1329 CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
1330
1331 CGF.EmitBlock(RethrowBB);
1332 if (SavedExnVar) {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001333 CGF.EmitCallOrInvoke(RethrowFn, CGF.Builder.CreateLoad(SavedExnVar));
John McCall77199712010-07-21 05:47:49 +00001334 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00001335 CGF.EmitCallOrInvoke(RethrowFn);
John McCall77199712010-07-21 05:47:49 +00001336 }
1337 CGF.Builder.CreateUnreachable();
1338
1339 CGF.EmitBlock(ContBB);
John McCalld96a8e72010-08-11 00:16:14 +00001340
1341 // Restore the cleanup destination.
1342 CGF.Builder.CreateStore(SavedCleanupDest,
1343 CGF.getNormalCleanupDestSlot());
John McCall77199712010-07-21 05:47:49 +00001344 }
1345
1346 // Leave the end-catch cleanup. As an optimization, pretend that
1347 // the fallthrough path was inaccessible; we've dynamically proven
1348 // that we're not in the EH case along that path.
1349 if (EndCatchFn) {
1350 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
1351 CGF.PopCleanupBlock();
1352 CGF.Builder.restoreIP(SavedIP);
1353 }
1354
1355 // Now make sure we actually have an insertion point or the
1356 // cleanup gods will hate us.
1357 CGF.EnsureInsertPoint();
1358 }
1359 };
John McCall55b20fc2010-07-21 00:52:03 +00001360}
1361
John McCallf1549f62010-07-06 01:34:17 +00001362/// Enters a finally block for an implementation using zero-cost
1363/// exceptions. This is mostly general, but hard-codes some
1364/// language/ABI-specific behavior in the catch-all sections.
John McCalld768e9d2011-06-22 02:32:12 +00001365void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
1366 const Stmt *body,
1367 llvm::Constant *beginCatchFn,
1368 llvm::Constant *endCatchFn,
1369 llvm::Constant *rethrowFn) {
1370 assert((beginCatchFn != 0) == (endCatchFn != 0) &&
John McCallf1549f62010-07-06 01:34:17 +00001371 "begin/end catch functions not paired");
John McCalld768e9d2011-06-22 02:32:12 +00001372 assert(rethrowFn && "rethrow function is required");
1373
1374 BeginCatchFn = beginCatchFn;
Mike Stumpd88ea562009-12-09 03:35:49 +00001375
John McCallf1549f62010-07-06 01:34:17 +00001376 // The rethrow function has one of the following two types:
1377 // void (*)()
1378 // void (*)(void*)
1379 // In the latter case we need to pass it the exception object.
1380 // But we can't use the exception slot because the @finally might
1381 // have a landing pad (which would overwrite the exception slot).
Chris Lattner2acc6e32011-07-18 04:24:23 +00001382 llvm::FunctionType *rethrowFnTy =
John McCallf1549f62010-07-06 01:34:17 +00001383 cast<llvm::FunctionType>(
John McCalld768e9d2011-06-22 02:32:12 +00001384 cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
1385 SavedExnVar = 0;
1386 if (rethrowFnTy->getNumParams())
1387 SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
Mike Stumpd88ea562009-12-09 03:35:49 +00001388
John McCallf1549f62010-07-06 01:34:17 +00001389 // A finally block is a statement which must be executed on any edge
1390 // out of a given scope. Unlike a cleanup, the finally block may
1391 // contain arbitrary control flow leading out of itself. In
1392 // addition, finally blocks should always be executed, even if there
1393 // are no catch handlers higher on the stack. Therefore, we
1394 // surround the protected scope with a combination of a normal
1395 // cleanup (to catch attempts to break out of the block via normal
1396 // control flow) and an EH catch-all (semantically "outside" any try
1397 // statement to which the finally block might have been attached).
1398 // The finally block itself is generated in the context of a cleanup
1399 // which conditionally leaves the catch-all.
John McCall3d3ec1c2010-04-21 10:05:39 +00001400
John McCallf1549f62010-07-06 01:34:17 +00001401 // Jump destination for performing the finally block on an exception
1402 // edge. We'll never actually reach this block, so unreachable is
1403 // fine.
John McCalld768e9d2011-06-22 02:32:12 +00001404 RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
John McCall3d3ec1c2010-04-21 10:05:39 +00001405
John McCallf1549f62010-07-06 01:34:17 +00001406 // Whether the finally block is being executed for EH purposes.
John McCalld768e9d2011-06-22 02:32:12 +00001407 ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
1408 CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar);
Mike Stumpd88ea562009-12-09 03:35:49 +00001409
John McCallf1549f62010-07-06 01:34:17 +00001410 // Enter a normal cleanup which will perform the @finally block.
John McCalld768e9d2011-06-22 02:32:12 +00001411 CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
1412 ForEHVar, endCatchFn,
1413 rethrowFn, SavedExnVar);
John McCallf1549f62010-07-06 01:34:17 +00001414
1415 // Enter a catch-all scope.
John McCalld768e9d2011-06-22 02:32:12 +00001416 llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
1417 EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
1418 catchScope->setCatchAllHandler(0, catchBB);
John McCallf1549f62010-07-06 01:34:17 +00001419}
1420
John McCalld768e9d2011-06-22 02:32:12 +00001421void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
John McCallf1549f62010-07-06 01:34:17 +00001422 // Leave the finally catch-all.
John McCalld768e9d2011-06-22 02:32:12 +00001423 EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
1424 llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
John McCall777d6e52011-08-11 02:22:43 +00001425
1426 CGF.popCatchScope();
John McCallf1549f62010-07-06 01:34:17 +00001427
John McCalld768e9d2011-06-22 02:32:12 +00001428 // If there are any references to the catch-all block, emit it.
1429 if (catchBB->use_empty()) {
1430 delete catchBB;
1431 } else {
1432 CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
1433 CGF.EmitBlock(catchBB);
John McCallf1549f62010-07-06 01:34:17 +00001434
John McCalld768e9d2011-06-22 02:32:12 +00001435 llvm::Value *exn = 0;
John McCallf1549f62010-07-06 01:34:17 +00001436
John McCalld768e9d2011-06-22 02:32:12 +00001437 // If there's a begin-catch function, call it.
1438 if (BeginCatchFn) {
Bill Wendlingae270592011-09-15 18:57:19 +00001439 exn = CGF.getExceptionFromSlot();
John McCalld768e9d2011-06-22 02:32:12 +00001440 CGF.Builder.CreateCall(BeginCatchFn, exn)->setDoesNotThrow();
1441 }
1442
1443 // If we need to remember the exception pointer to rethrow later, do so.
1444 if (SavedExnVar) {
Bill Wendlingae270592011-09-15 18:57:19 +00001445 if (!exn) exn = CGF.getExceptionFromSlot();
John McCalld768e9d2011-06-22 02:32:12 +00001446 CGF.Builder.CreateStore(exn, SavedExnVar);
1447 }
1448
1449 // Tell the cleanups in the finally block that we're do this for EH.
1450 CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar);
1451
1452 // Thread a jump through the finally cleanup.
1453 CGF.EmitBranchThroughCleanup(RethrowDest);
1454
1455 CGF.Builder.restoreIP(savedIP);
1456 }
1457
1458 // Finally, leave the @finally cleanup.
1459 CGF.PopCleanupBlock();
John McCallf1549f62010-07-06 01:34:17 +00001460}
1461
1462llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
1463 if (TerminateLandingPad)
1464 return TerminateLandingPad;
1465
1466 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1467
1468 // This will get inserted at the end of the function.
1469 TerminateLandingPad = createBasicBlock("terminate.lpad");
1470 Builder.SetInsertPoint(TerminateLandingPad);
1471
1472 // Tell the backend that this is a landing pad.
John McCall8262b6a2010-07-17 00:43:08 +00001473 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions());
Bill Wendling285cfd82011-09-19 20:31:14 +00001474 llvm::LandingPadInst *LPadInst =
1475 Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, NULL),
1476 getOpaquePersonalityFn(CGM, Personality), 0);
1477 LPadInst->addClause(getCatchAllValue(*this));
John McCallf1549f62010-07-06 01:34:17 +00001478
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: {
Bill Wendling285cfd82011-09-19 20:31:14 +00001541 // In mandatory-cleanup mode, we should use 'resume'.
1542
1543 // Recreate the landingpad's return value for the 'resume' instruction.
1544 llvm::Value *Exn = getExceptionFromSlot();
1545 llvm::Value *Sel = getSelectorFromSlot();
1546
1547 llvm::Type *LPadType = llvm::StructType::get(Exn->getType(),
1548 Sel->getType(), NULL);
1549 llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
1550 LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
1551 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
1552
1553 Builder.CreateResume(LPadVal);
1554 Builder.restoreIP(SavedIP);
1555 return EHResumeBlock;
John McCall93c332a2011-05-28 21:13:02 +00001556 }
1557 case CHL_Ideal:
1558 // In an idealized mode where we don't have to worry about the
1559 // optimizer combining landing pads, we should just use
1560 // _Unwind_Resume (or the personality's equivalent).
1561 Builder.CreateCall(getUnwindResumeFn(), Exn)
1562 ->setDoesNotReturn();
1563 break;
1564 }
1565 }
1566
John McCallff8e1152010-07-23 21:56:41 +00001567 Builder.CreateUnreachable();
1568
1569 Builder.restoreIP(SavedIP);
1570
John McCall777d6e52011-08-11 02:22:43 +00001571 return EHResumeBlock;
John McCallff8e1152010-07-23 21:56:41 +00001572}