blob: ed392bd0d345ce4e4fc47e7885e665d1687d7c26 [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"
17
Anders Carlsson756b5c42009-10-30 01:42:31 +000018#include "CodeGenFunction.h"
19using namespace clang;
20using namespace CodeGen;
21
Anders Carlssond3379292009-10-30 02:27:02 +000022static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
23 // void *__cxa_allocate_exception(size_t thrown_size);
24 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
25 std::vector<const llvm::Type*> Args(1, SizeTy);
Mike Stump8755ec32009-12-10 00:06:18 +000026
27 const llvm::FunctionType *FTy =
Anders Carlssond3379292009-10-30 02:27:02 +000028 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000030
Anders Carlssond3379292009-10-30 02:27:02 +000031 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
Mike Stump99533832009-12-02 07:41:41 +000034static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
35 // void __cxa_free_exception(void *thrown_exception);
36 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
37 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000038
39 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000042
Mike Stump99533832009-12-02 07:41:41 +000043 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44}
45
Anders Carlssond3379292009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump8755ec32009-12-10 00:06:18 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
Mike Stump99533832009-12-02 07:41:41 +000048 // void (*dest) (void *));
Anders Carlssond3379292009-10-30 02:27:02 +000049
50 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
51 std::vector<const llvm::Type*> Args(3, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000052
53 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000056
Anders Carlssond3379292009-10-30 02:27:02 +000057 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58}
59
Mike Stumpb4eea692009-11-20 00:56:31 +000060static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000061 // void __cxa_rethrow();
Mike Stumpb4eea692009-11-20 00:56:31 +000062
Mike Stump8755ec32009-12-10 00:06:18 +000063 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000064 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +000065
Mike Stumpb4eea692009-11-20 00:56:31 +000066 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67}
68
Mike Stump2bf701e2009-11-20 23:44:51 +000069static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000070 // void* __cxa_begin_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000071
72 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
73 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000074
75 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000077
Mike Stump2bf701e2009-11-20 23:44:51 +000078 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000082 // void __cxa_end_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000083
Mike Stump8755ec32009-12-10 00:06:18 +000084 const llvm::FunctionType *FTy =
Mike Stump2bf701e2009-11-20 23:44:51 +000085 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +000086
Mike Stump2bf701e2009-11-20 23:44:51 +000087 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
Mike Stumpcce3d4f2009-12-07 23:38:24 +000090static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
91 // void __cxa_call_unexepcted(void *thrown_exception);
92
93 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
94 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000095
96 const llvm::FunctionType *FTy =
Mike Stumpcce3d4f2009-12-07 23:38:24 +000097 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98 Args, 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
Douglas Gregor86a3a032010-05-16 01:24:12 +0000103llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
104 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stump0f590be2009-12-01 03:41:18 +0000105 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000106
107 const llvm::FunctionType *FTy =
Douglas Gregor86a3a032010-05-16 01:24:12 +0000108 llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), Args,
Mike Stump0f590be2009-12-01 03:41:18 +0000109 false);
Mike Stump8755ec32009-12-10 00:06:18 +0000110
Douglas Gregor86a3a032010-05-16 01:24:12 +0000111 if (CGM.getLangOptions().SjLjExceptions)
112 return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
113 return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
Mike Stump0f590be2009-12-01 03:41:18 +0000114}
115
Mike Stump99533832009-12-02 07:41:41 +0000116static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
117 // void __terminate();
118
Mike Stump8755ec32009-12-10 00:06:18 +0000119 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +0000120 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +0000121
David Chisnall79a9ad82010-05-17 13:49:20 +0000122 return CGF.CGM.CreateRuntimeFunction(FTy,
123 CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort");
124}
125
126static llvm::Constant *getPersonalityFn(CodeGenModule &CGM) {
127 const char *PersonalityFnName = "__gcc_personality_v0";
128 LangOptions Opts = CGM.getLangOptions();
129 if (Opts.CPlusPlus)
130 PersonalityFnName = "__gxx_personality_v0";
131 else if (Opts.ObjC1)
132 if (Opts.NeXTRuntime) {
133 if (Opts.ObjCNonFragileABI)
134 PersonalityFnName = "__gcc_personality_v0";
135 } else
136 PersonalityFnName = "__gnu_objc_personality_v0";
137
138 llvm::Constant *Personality =
139 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(
140 CGM.getLLVMContext()),
141 true),
142 PersonalityFnName);
143 return llvm::ConstantExpr::getBitCast(Personality, CGM.PtrToInt8Ty);
Mike Stump99533832009-12-02 07:41:41 +0000144}
145
John McCallac418162010-04-22 01:10:34 +0000146// Emits an exception expression into the given location. This
147// differs from EmitAnyExprToMem only in that, if a final copy-ctor
148// call is required, an exception within that copy ctor causes
149// std::terminate to be invoked.
150static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E,
151 llvm::Value *ExnLoc) {
152 // We want to release the allocated exception object if this
153 // expression throws. We do this by pushing an EH-only cleanup
154 // block which, furthermore, deactivates itself after the expression
155 // is complete.
156 llvm::AllocaInst *ShouldFreeVar =
157 CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
158 "should-free-exnobj.var");
159 CGF.InitTempAlloca(ShouldFreeVar,
160 llvm::ConstantInt::getFalse(CGF.getLLVMContext()));
Mike Stump0f590be2009-12-01 03:41:18 +0000161
John McCallac418162010-04-22 01:10:34 +0000162 // A variable holding the exception pointer. This is necessary
163 // because the throw expression does not necessarily dominate the
164 // cleanup, for example if it appears in a conditional expression.
165 llvm::AllocaInst *ExnLocVar =
166 CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var");
Mike Stump8755ec32009-12-10 00:06:18 +0000167
John McCallac418162010-04-22 01:10:34 +0000168 llvm::BasicBlock *SavedInvokeDest = CGF.getInvokeDest();
169 {
170 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
171 llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj");
172 llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done");
Mike Stumpf2945c02009-12-17 06:08:47 +0000173
John McCallac418162010-04-22 01:10:34 +0000174 llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar,
175 "should-free-exnobj");
176 CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB);
177 CGF.EmitBlock(FreeBB);
178 llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj");
179 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal);
180 CGF.EmitBlock(DoneBB);
Mike Stump0f590be2009-12-01 03:41:18 +0000181 }
John McCallac418162010-04-22 01:10:34 +0000182 llvm::BasicBlock *Cleanup = CGF.getInvokeDest();
183
184 CGF.Builder.CreateStore(ExnLoc, ExnLocVar);
185 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
186 ShouldFreeVar);
187
188 // __cxa_allocate_exception returns a void*; we need to cast this
189 // to the appropriate type for the object.
190 const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo();
191 llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty);
192
193 // FIXME: this isn't quite right! If there's a final unelided call
194 // to a copy constructor, then according to [except.terminate]p1 we
195 // must call std::terminate() if that constructor throws, because
196 // technically that copy occurs after the exception expression is
197 // evaluated but before the exception is caught. But the best way
198 // to handle that is to teach EmitAggExpr to do the final copy
199 // differently if it can't be elided.
200 CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false);
201
202 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
203 ShouldFreeVar);
204
205 // Pop the cleanup block if it's still the top of the cleanup stack.
206 // Otherwise, temporaries have been created and our cleanup will get
207 // properly removed in time.
208 // TODO: this is not very resilient.
209 if (CGF.getInvokeDest() == Cleanup)
210 CGF.setInvokeDest(SavedInvokeDest);
Mike Stump0f590be2009-12-01 03:41:18 +0000211}
212
213// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
214// N is casted to the right type.
215static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump13b2f922010-01-01 03:20:32 +0000216 bool WasPointer, bool WasPointerReference,
217 llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000218 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000219 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000220 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000221 if (!WasPointer)
222 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000223 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump13b2f922010-01-01 03:20:32 +0000224 if (WasPointerReference) {
Mike Stumpb606c382010-01-01 02:51:52 +0000225 llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
226 CGF.Builder.CreateStore(Value, Tmp);
227 Value = Tmp;
Mike Stump13b2f922010-01-01 03:20:32 +0000228 ValuePtrTy = Value->getType()->getPointerTo(0);
229 }
230 N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
Mike Stumpb606c382010-01-01 02:51:52 +0000231 CGF.Builder.CreateStore(Value, N);
Mike Stump0f590be2009-12-01 03:41:18 +0000232 } else {
233 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
234 const CXXRecordDecl *RD;
235 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
236 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
237 if (RD->hasTrivialCopyConstructor()) {
238 CGF.EmitAggregateCopy(This, E, ObjectType);
239 } else if (CXXConstructorDecl *CopyCtor
240 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000241 llvm::Value *Src = E;
242
243 // Stolen from EmitClassAggrMemberwiseCopy
244 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
245 Ctor_Complete);
246 CallArgList CallArgs;
247 CallArgs.push_back(std::make_pair(RValue::get(This),
248 CopyCtor->getThisType(CGF.getContext())));
249
250 // Push the Src ptr.
251 CallArgs.push_back(std::make_pair(RValue::get(Src),
252 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000253
254 const FunctionProtoType *FPT
255 = CopyCtor->getType()->getAs<FunctionProtoType>();
256 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000257 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000258 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000259 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000260 }
261}
262
Anders Carlsson756b5c42009-10-30 01:42:31 +0000263void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000264 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000265 if (getInvokeDest()) {
266 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
267 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
268 ->setDoesNotReturn();
269 EmitBlock(Cont);
270 } else
271 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
272 Builder.CreateUnreachable();
273
274 // Clear the insertion point to indicate we are in unreachable code.
275 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000276 return;
277 }
Mike Stump8755ec32009-12-10 00:06:18 +0000278
Anders Carlssond3379292009-10-30 02:27:02 +0000279 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000280
Anders Carlssond3379292009-10-30 02:27:02 +0000281 // Now allocate the exception object.
282 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000283 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000284
Anders Carlssond3379292009-10-30 02:27:02 +0000285 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000286 llvm::Value *ExceptionPtr =
287 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000288 llvm::ConstantInt::get(SizeTy, TypeSize),
289 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000290
John McCallac418162010-04-22 01:10:34 +0000291 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000292
Anders Carlssond3379292009-10-30 02:27:02 +0000293 // Now throw the exception.
294 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
John McCall9dffe6f2010-04-30 01:15:21 +0000295 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true);
John McCallac418162010-04-22 01:10:34 +0000296
297 // The address of the destructor. If the exception type has a
298 // trivial destructor (or isn't a record), we just pass null.
299 llvm::Constant *Dtor = 0;
300 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
301 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
302 if (!Record->hasTrivialDestructor()) {
303 CXXDestructorDecl *DtorD = Record->getDestructor(getContext());
304 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
305 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
306 }
307 }
308 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000309
Mike Stump0a3816e2009-12-04 01:51:45 +0000310 if (getInvokeDest()) {
311 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000312 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000313 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
314 ExceptionPtr, TypeInfo, Dtor);
315 ThrowCall->setDoesNotReturn();
316 EmitBlock(Cont);
317 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000318 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000319 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
320 ThrowCall->setDoesNotReturn();
321 }
Anders Carlssond3379292009-10-30 02:27:02 +0000322 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000323
Anders Carlssond3379292009-10-30 02:27:02 +0000324 // Clear the insertion point to indicate we are in unreachable code.
325 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000326
327 // FIXME: For now, emit a dummy basic block because expr emitters in generally
328 // are not ready to handle emitting expressions at unreachable points.
329 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000330}
Mike Stump2bf701e2009-11-20 23:44:51 +0000331
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000332void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000333 if (!Exceptions)
334 return;
335
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000336 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
337 if (FD == 0)
338 return;
339 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
340 if (Proto == 0)
341 return;
342
343 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
344
345 if (!Proto->hasExceptionSpec())
346 return;
347
David Chisnall79a9ad82010-05-17 13:49:20 +0000348 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000349 llvm::Value *llvm_eh_exception =
350 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
351 llvm::Value *llvm_eh_selector =
352 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
353 const llvm::IntegerType *Int8Ty;
354 const llvm::PointerType *PtrToInt8Ty;
355 Int8Ty = llvm::Type::getInt8Ty(VMContext);
356 // C string type. Used in lots of places.
357 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
358 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
359 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
360
361 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
362 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
363 llvm::BasicBlock *Match = createBasicBlock("match");
364 llvm::BasicBlock *Unwind = 0;
365
366 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000367 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000368
369 llvm::BasicBlock *Cont = createBasicBlock("cont");
370
371 EmitBranchThroughCleanup(Cont);
372
373 // Emit the statements in the try {} block
374 setInvokeDest(EHSpecHandler);
375
376 EmitBlock(EHSpecHandler);
377 // Exception object
378 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
379 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
380
381 SelectorArgs.push_back(Exc);
382 SelectorArgs.push_back(Personality);
383 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
384 Proto->getNumExceptions()+1));
385
386 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
387 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000388 QualType ExceptType
389 = Ty.getNonReferenceType().getUnqualifiedType();
John McCall9dffe6f2010-04-30 01:15:21 +0000390 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000391 SelectorArgs.push_back(EHType);
392 }
393 if (Proto->getNumExceptions())
394 SelectorArgs.push_back(Null);
395
396 // Find which handler was matched.
397 llvm::Value *Selector
398 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
399 SelectorArgs.end(), "selector");
400 if (Proto->getNumExceptions()) {
401 Unwind = createBasicBlock("Unwind");
402
403 Builder.CreateStore(Exc, RethrowPtr);
404 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
405 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
406 0)),
407 Match, Unwind);
408
409 EmitBlock(Match);
410 }
411 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
412 Builder.CreateUnreachable();
413
414 if (Proto->getNumExceptions()) {
415 EmitBlock(Unwind);
Douglas Gregor86a3a032010-05-16 01:24:12 +0000416 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000417 Builder.CreateLoad(RethrowPtr));
418 Builder.CreateUnreachable();
419 }
420
421 EmitBlock(Cont);
422}
423
424void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000425 if (!Exceptions)
426 return;
427
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000428 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
429 if (FD == 0)
430 return;
431 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
432 if (Proto == 0)
433 return;
434
435 if (!Proto->hasExceptionSpec())
436 return;
437
438 setInvokeDest(0);
439}
440
Mike Stump2bf701e2009-11-20 23:44:51 +0000441void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall9fc6a772010-02-19 09:25:03 +0000442 CXXTryStmtInfo Info = EnterCXXTryStmt(S);
443 EmitStmt(S.getTryBlock());
444 ExitCXXTryStmt(S, Info);
445}
446
447CodeGenFunction::CXXTryStmtInfo
448CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
449 CXXTryStmtInfo Info;
450 Info.SavedLandingPad = getInvokeDest();
451 Info.HandlerBlock = createBasicBlock("try.handler");
452 Info.FinallyBlock = createBasicBlock("finally");
453
454 PushCleanupBlock(Info.FinallyBlock);
455 setInvokeDest(Info.HandlerBlock);
456
457 return Info;
458}
459
460void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
461 CXXTryStmtInfo TryInfo) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000462 // Pointer to the personality function
David Chisnall79a9ad82010-05-17 13:49:20 +0000463 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stump639787c2009-12-02 19:53:57 +0000464 llvm::Value *llvm_eh_exception =
465 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
466 llvm::Value *llvm_eh_selector =
467 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000468
John McCall9fc6a772010-02-19 09:25:03 +0000469 llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
470 llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
471 llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
Mike Stump0f590be2009-12-01 03:41:18 +0000472 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000473 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
474
Mike Stump2bf701e2009-11-20 23:44:51 +0000475 // Jump to end if there is no exception
476 EmitBranchThroughCleanup(FinallyEnd);
477
Mike Stump9b39c512009-12-09 22:59:31 +0000478 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000479
Mike Stump2bf701e2009-11-20 23:44:51 +0000480 // Emit the handlers
481 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000482
Mike Stump2bf701e2009-11-20 23:44:51 +0000483 const llvm::IntegerType *Int8Ty;
484 const llvm::PointerType *PtrToInt8Ty;
485 Int8Ty = llvm::Type::getInt8Ty(VMContext);
486 // C string type. Used in lots of places.
487 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000488 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
489 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000490 llvm::Value *llvm_eh_typeid_for =
491 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000492 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000493 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000494 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000495
Mike Stump0f590be2009-12-01 03:41:18 +0000496 SelectorArgs.push_back(Exc);
497 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000498
Mike Stump0f590be2009-12-01 03:41:18 +0000499 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000500 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
501 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000502 VarDecl *CatchParam = C->getExceptionDecl();
503 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000504 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
505 // are ignored.
506 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000507 llvm::Value *EHTypeInfo
John McCall9dffe6f2010-04-30 01:15:21 +0000508 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType(), true);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000509 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000510 } else {
511 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000512 SelectorArgs.push_back(Null);
513 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000514 }
515 }
516
Mike Stump0f590be2009-12-01 03:41:18 +0000517 // We use a cleanup unless there was already a catch all.
518 if (!HasCatchAll) {
519 SelectorArgs.push_back(Null);
520 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000521
Mike Stump0f590be2009-12-01 03:41:18 +0000522 // Find which handler was matched.
523 llvm::Value *Selector
524 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
525 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000526 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
527 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000528 VarDecl *CatchParam = C->getExceptionDecl();
529 Stmt *CatchBody = C->getHandlerBlock();
530
531 llvm::BasicBlock *Next = 0;
532
533 if (SelectorArgs[i+2] != Null) {
534 llvm::BasicBlock *Match = createBasicBlock("match");
535 Next = createBasicBlock("catch.next");
536 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
537 llvm::Value *Id
538 = Builder.CreateCall(llvm_eh_typeid_for,
539 Builder.CreateBitCast(SelectorArgs[i+2],
540 Int8PtrTy));
541 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
542 Match, Next);
543 EmitBlock(Match);
544 }
545
546 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
547 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
548
549 PushCleanupBlock(MatchEnd);
550 setInvokeDest(MatchHandler);
551
552 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
553
Mike Stumpf7f74672009-12-02 23:37:16 +0000554 {
555 CleanupScope CatchScope(*this);
556 // Bind the catch parameter if it exists.
557 if (CatchParam) {
558 QualType CatchType = CatchParam->getType().getNonReferenceType();
559 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000560 bool WasPointer = true;
Mike Stump13b2f922010-01-01 03:20:32 +0000561 bool WasPointerReference = false;
Mike Stumpb606c382010-01-01 02:51:52 +0000562 CatchType = CGM.getContext().getCanonicalType(CatchType);
Mike Stump13b2f922010-01-01 03:20:32 +0000563 if (CatchType.getTypePtr()->isPointerType()) {
564 if (isa<ReferenceType>(CatchParam->getType()))
565 WasPointerReference = true;
566 } else {
Mike Stump0a3816e2009-12-04 01:51:45 +0000567 if (!isa<ReferenceType>(CatchParam->getType()))
568 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000569 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000570 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000571 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000572 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000573 // FIXME: we need to do this sooner so that the EH region for the
574 // cleanup doesn't start until after the ctor completes, use a decl
575 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000576 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump13b2f922010-01-01 03:20:32 +0000577 WasPointer, WasPointerReference, ExcObject,
Mike Stumpb606c382010-01-01 02:51:52 +0000578 GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000579 setInvokeDest(MatchHandler);
580 }
581
582 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000583 }
584
Mike Stump0f590be2009-12-01 03:41:18 +0000585 EmitBranchThroughCleanup(FinallyEnd);
586
587 EmitBlock(MatchHandler);
588
589 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
590 // We are required to emit this call to satisfy LLVM, even
591 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000592 llvm::Value *Args[] = {
593 Exc, Personality,
594 llvm::ConstantInt::getNullValue(llvm::Type::getInt32Ty(VMContext))
595 };
596 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump0f590be2009-12-01 03:41:18 +0000597 Builder.CreateStore(Exc, RethrowPtr);
598 EmitBranchThroughCleanup(FinallyRethrow);
599
600 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
601
602 EmitBlock(MatchEnd);
603
Mike Stump99533832009-12-02 07:41:41 +0000604 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000605 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000606 Cont, TerminateHandler,
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000607 &Args[0], &Args[0]);
Mike Stump0f590be2009-12-01 03:41:18 +0000608 EmitBlock(Cont);
609 if (Info.SwitchBlock)
610 EmitBlock(Info.SwitchBlock);
611 if (Info.EndBlock)
612 EmitBlock(Info.EndBlock);
613
Mike Stump0f590be2009-12-01 03:41:18 +0000614 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000615 Builder.CreateStore(Exc, RethrowPtr);
616 EmitBranchThroughCleanup(FinallyRethrow);
617
618 if (Next)
619 EmitBlock(Next);
620 }
Mike Stumpa0867832009-12-04 19:03:47 +0000621 if (!HasCatchAll) {
622 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000623 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000624 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000625
626 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
627
628 setInvokeDest(PrevLandingPad);
629
630 EmitBlock(FinallyBlock);
631
Mike Stump0f590be2009-12-01 03:41:18 +0000632 if (Info.SwitchBlock)
633 EmitBlock(Info.SwitchBlock);
634 if (Info.EndBlock)
635 EmitBlock(Info.EndBlock);
636
Mike Stump2bf701e2009-11-20 23:44:51 +0000637 // Branch around the rethrow code.
638 EmitBranch(FinallyEnd);
639
640 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000641 // FIXME: Eventually we can chain the handlers together and just do a call
642 // here.
643 if (getInvokeDest()) {
644 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000645 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000646 getInvokeDest(),
647 Builder.CreateLoad(RethrowPtr));
648 EmitBlock(Cont);
649 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000650 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000651 Builder.CreateLoad(RethrowPtr));
652
Mike Stump2bf701e2009-11-20 23:44:51 +0000653 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000654
655 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000656}
Mike Stumpd88ea562009-12-09 03:35:49 +0000657
658CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
Mike Stumpd88ea562009-12-09 03:35:49 +0000659 CGF.setInvokeDest(PreviousInvokeDest);
660
John McCall3d3ec1c2010-04-21 10:05:39 +0000661 llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock();
Mike Stumpd88ea562009-12-09 03:35:49 +0000662
John McCall3d3ec1c2010-04-21 10:05:39 +0000663 // Jump to the beginning of the cleanup.
664 CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin());
665
666 // The libstdc++ personality function.
667 // TODO: generalize to work with other libraries.
David Chisnall79a9ad82010-05-17 13:49:20 +0000668 llvm::Constant *Personality = getPersonalityFn(CGF.CGM);
John McCall3d3ec1c2010-04-21 10:05:39 +0000669
670 // %exception = call i8* @llvm.eh.exception()
671 // Magic intrinsic which tells gives us a handle to the caught
672 // exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000673 llvm::Value *llvm_eh_exception =
674 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
John McCall3d3ec1c2010-04-21 10:05:39 +0000675 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
676
677 llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty);
678
679 // %ignored = call i32 @llvm.eh.selector(i8* %exception,
680 // i8* @__gxx_personality_v0,
681 // i8* null)
682 // Magic intrinsic which tells LLVM that this invoke landing pad is
683 // just a cleanup block.
684 llvm::Value *Args[] = { Exc, Personality, Null };
Mike Stumpd88ea562009-12-09 03:35:49 +0000685 llvm::Value *llvm_eh_selector =
686 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000687 CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stumpd88ea562009-12-09 03:35:49 +0000688
John McCall3d3ec1c2010-04-21 10:05:39 +0000689 // And then we fall through into the code that the user put there.
690 // Jump back to the end of the cleanup.
691 CGF.Builder.SetInsertPoint(EndOfCleanup);
Mike Stumpd88ea562009-12-09 03:35:49 +0000692
John McCall3d3ec1c2010-04-21 10:05:39 +0000693 // Rethrow the exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000694 if (CGF.getInvokeDest()) {
695 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000696 CGF.Builder.CreateInvoke(CGF.getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpd88ea562009-12-09 03:35:49 +0000697 CGF.getInvokeDest(), Exc);
698 CGF.EmitBlock(Cont);
699 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000700 CGF.Builder.CreateCall(CGF.getUnwindResumeOrRethrowFn(), Exc);
Mike Stumpd88ea562009-12-09 03:35:49 +0000701 CGF.Builder.CreateUnreachable();
702
John McCall3d3ec1c2010-04-21 10:05:39 +0000703 // Resume inserting where we started, but put the new cleanup
704 // handler in place.
John McCall891f80e2010-04-30 00:06:43 +0000705 if (PreviousInsertionBlock)
706 CGF.Builder.SetInsertPoint(PreviousInsertionBlock);
707 else
708 CGF.Builder.ClearInsertionPoint();
709
Mike Stumpd88ea562009-12-09 03:35:49 +0000710 if (CGF.Exceptions)
711 CGF.setInvokeDest(CleanupHandler);
712}
Mike Stump9b39c512009-12-09 22:59:31 +0000713
714llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000715 if (TerminateHandler)
716 return TerminateHandler;
717
John McCall3d3ec1c2010-04-21 10:05:39 +0000718 // We don't want to change anything at the current location, so
719 // save it aside and clear the insert point.
720 llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock();
721 llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint();
722 Builder.ClearInsertionPoint();
Mike Stump76958092009-12-09 23:31:35 +0000723
David Chisnall79a9ad82010-05-17 13:49:20 +0000724 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stump9b39c512009-12-09 22:59:31 +0000725 llvm::Value *llvm_eh_exception =
726 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
727 llvm::Value *llvm_eh_selector =
728 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
729
730 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000731 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000732 EmitBlock(TerminateHandler);
733 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
734 // We are required to emit this call to satisfy LLVM, even
735 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000736 llvm::Value *Args[] = {
737 Exc, Personality,
738 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)
739 };
740 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump8755ec32009-12-10 00:06:18 +0000741 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000742 Builder.CreateCall(getTerminateFn(*this));
743 TerminateCall->setDoesNotReturn();
744 TerminateCall->setDoesNotThrow();
745 Builder.CreateUnreachable();
746
John McCall3d3ec1c2010-04-21 10:05:39 +0000747 // Restore the saved insertion state.
748 Builder.SetInsertPoint(SavedInsertBlock, SavedInsertPoint);
Mike Stump76958092009-12-09 23:31:35 +0000749
Mike Stump9b39c512009-12-09 22:59:31 +0000750 return TerminateHandler;
751}