blob: 9fa195235ff5df7313b230d8353d91a8fb29d772 [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
Mike Stump99533832009-12-02 07:41:41 +0000122 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
123}
124
John McCallac418162010-04-22 01:10:34 +0000125// Emits an exception expression into the given location. This
126// differs from EmitAnyExprToMem only in that, if a final copy-ctor
127// call is required, an exception within that copy ctor causes
128// std::terminate to be invoked.
129static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E,
130 llvm::Value *ExnLoc) {
131 // We want to release the allocated exception object if this
132 // expression throws. We do this by pushing an EH-only cleanup
133 // block which, furthermore, deactivates itself after the expression
134 // is complete.
135 llvm::AllocaInst *ShouldFreeVar =
136 CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
137 "should-free-exnobj.var");
138 CGF.InitTempAlloca(ShouldFreeVar,
139 llvm::ConstantInt::getFalse(CGF.getLLVMContext()));
Mike Stump0f590be2009-12-01 03:41:18 +0000140
John McCallac418162010-04-22 01:10:34 +0000141 // A variable holding the exception pointer. This is necessary
142 // because the throw expression does not necessarily dominate the
143 // cleanup, for example if it appears in a conditional expression.
144 llvm::AllocaInst *ExnLocVar =
145 CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var");
Mike Stump8755ec32009-12-10 00:06:18 +0000146
John McCallac418162010-04-22 01:10:34 +0000147 llvm::BasicBlock *SavedInvokeDest = CGF.getInvokeDest();
148 {
149 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
150 llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj");
151 llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done");
Mike Stumpf2945c02009-12-17 06:08:47 +0000152
John McCallac418162010-04-22 01:10:34 +0000153 llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar,
154 "should-free-exnobj");
155 CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB);
156 CGF.EmitBlock(FreeBB);
157 llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj");
158 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal);
159 CGF.EmitBlock(DoneBB);
Mike Stump0f590be2009-12-01 03:41:18 +0000160 }
John McCallac418162010-04-22 01:10:34 +0000161 llvm::BasicBlock *Cleanup = CGF.getInvokeDest();
162
163 CGF.Builder.CreateStore(ExnLoc, ExnLocVar);
164 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
165 ShouldFreeVar);
166
167 // __cxa_allocate_exception returns a void*; we need to cast this
168 // to the appropriate type for the object.
169 const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo();
170 llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty);
171
172 // FIXME: this isn't quite right! If there's a final unelided call
173 // to a copy constructor, then according to [except.terminate]p1 we
174 // must call std::terminate() if that constructor throws, because
175 // technically that copy occurs after the exception expression is
176 // evaluated but before the exception is caught. But the best way
177 // to handle that is to teach EmitAggExpr to do the final copy
178 // differently if it can't be elided.
179 CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false);
180
181 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
182 ShouldFreeVar);
183
184 // Pop the cleanup block if it's still the top of the cleanup stack.
185 // Otherwise, temporaries have been created and our cleanup will get
186 // properly removed in time.
187 // TODO: this is not very resilient.
188 if (CGF.getInvokeDest() == Cleanup)
189 CGF.setInvokeDest(SavedInvokeDest);
Mike Stump0f590be2009-12-01 03:41:18 +0000190}
191
192// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
193// N is casted to the right type.
194static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump13b2f922010-01-01 03:20:32 +0000195 bool WasPointer, bool WasPointerReference,
196 llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000197 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000198 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000199 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000200 if (!WasPointer)
201 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000202 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump13b2f922010-01-01 03:20:32 +0000203 if (WasPointerReference) {
Mike Stumpb606c382010-01-01 02:51:52 +0000204 llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
205 CGF.Builder.CreateStore(Value, Tmp);
206 Value = Tmp;
Mike Stump13b2f922010-01-01 03:20:32 +0000207 ValuePtrTy = Value->getType()->getPointerTo(0);
208 }
209 N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
Mike Stumpb606c382010-01-01 02:51:52 +0000210 CGF.Builder.CreateStore(Value, N);
Mike Stump0f590be2009-12-01 03:41:18 +0000211 } else {
212 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
213 const CXXRecordDecl *RD;
214 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
215 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
216 if (RD->hasTrivialCopyConstructor()) {
217 CGF.EmitAggregateCopy(This, E, ObjectType);
218 } else if (CXXConstructorDecl *CopyCtor
219 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000220 llvm::Value *Src = E;
221
222 // Stolen from EmitClassAggrMemberwiseCopy
223 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
224 Ctor_Complete);
225 CallArgList CallArgs;
226 CallArgs.push_back(std::make_pair(RValue::get(This),
227 CopyCtor->getThisType(CGF.getContext())));
228
229 // Push the Src ptr.
230 CallArgs.push_back(std::make_pair(RValue::get(Src),
231 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000232
233 const FunctionProtoType *FPT
234 = CopyCtor->getType()->getAs<FunctionProtoType>();
235 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000236 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000237 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000238 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000239 }
240}
241
Anders Carlsson756b5c42009-10-30 01:42:31 +0000242void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000243 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000244 if (getInvokeDest()) {
245 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
246 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
247 ->setDoesNotReturn();
248 EmitBlock(Cont);
249 } else
250 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
251 Builder.CreateUnreachable();
252
253 // Clear the insertion point to indicate we are in unreachable code.
254 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000255 return;
256 }
Mike Stump8755ec32009-12-10 00:06:18 +0000257
Anders Carlssond3379292009-10-30 02:27:02 +0000258 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000259
Anders Carlssond3379292009-10-30 02:27:02 +0000260 // Now allocate the exception object.
261 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000262 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000263
Anders Carlssond3379292009-10-30 02:27:02 +0000264 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000265 llvm::Value *ExceptionPtr =
266 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000267 llvm::ConstantInt::get(SizeTy, TypeSize),
268 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000269
John McCallac418162010-04-22 01:10:34 +0000270 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000271
Anders Carlssond3379292009-10-30 02:27:02 +0000272 // Now throw the exception.
273 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
John McCall9dffe6f2010-04-30 01:15:21 +0000274 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true);
John McCallac418162010-04-22 01:10:34 +0000275
276 // The address of the destructor. If the exception type has a
277 // trivial destructor (or isn't a record), we just pass null.
278 llvm::Constant *Dtor = 0;
279 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
280 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
281 if (!Record->hasTrivialDestructor()) {
282 CXXDestructorDecl *DtorD = Record->getDestructor(getContext());
283 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
284 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
285 }
286 }
287 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000288
Mike Stump0a3816e2009-12-04 01:51:45 +0000289 if (getInvokeDest()) {
290 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000291 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000292 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
293 ExceptionPtr, TypeInfo, Dtor);
294 ThrowCall->setDoesNotReturn();
295 EmitBlock(Cont);
296 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000297 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000298 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
299 ThrowCall->setDoesNotReturn();
300 }
Anders Carlssond3379292009-10-30 02:27:02 +0000301 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000302
Anders Carlssond3379292009-10-30 02:27:02 +0000303 // Clear the insertion point to indicate we are in unreachable code.
304 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000305
306 // FIXME: For now, emit a dummy basic block because expr emitters in generally
307 // are not ready to handle emitting expressions at unreachable points.
308 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000309}
Mike Stump2bf701e2009-11-20 23:44:51 +0000310
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000311void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000312 if (!Exceptions)
313 return;
314
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000315 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
316 if (FD == 0)
317 return;
318 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
319 if (Proto == 0)
320 return;
321
322 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
323
324 if (!Proto->hasExceptionSpec())
325 return;
326
327 llvm::Constant *Personality =
328 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
329 (VMContext),
330 true),
331 "__gxx_personality_v0");
332 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
333 llvm::Value *llvm_eh_exception =
334 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
335 llvm::Value *llvm_eh_selector =
336 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
337 const llvm::IntegerType *Int8Ty;
338 const llvm::PointerType *PtrToInt8Ty;
339 Int8Ty = llvm::Type::getInt8Ty(VMContext);
340 // C string type. Used in lots of places.
341 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
342 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
343 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
344
345 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
346 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
347 llvm::BasicBlock *Match = createBasicBlock("match");
348 llvm::BasicBlock *Unwind = 0;
349
350 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000351 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000352
353 llvm::BasicBlock *Cont = createBasicBlock("cont");
354
355 EmitBranchThroughCleanup(Cont);
356
357 // Emit the statements in the try {} block
358 setInvokeDest(EHSpecHandler);
359
360 EmitBlock(EHSpecHandler);
361 // Exception object
362 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
363 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
364
365 SelectorArgs.push_back(Exc);
366 SelectorArgs.push_back(Personality);
367 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
368 Proto->getNumExceptions()+1));
369
370 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
371 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000372 QualType ExceptType
373 = Ty.getNonReferenceType().getUnqualifiedType();
John McCall9dffe6f2010-04-30 01:15:21 +0000374 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000375 SelectorArgs.push_back(EHType);
376 }
377 if (Proto->getNumExceptions())
378 SelectorArgs.push_back(Null);
379
380 // Find which handler was matched.
381 llvm::Value *Selector
382 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
383 SelectorArgs.end(), "selector");
384 if (Proto->getNumExceptions()) {
385 Unwind = createBasicBlock("Unwind");
386
387 Builder.CreateStore(Exc, RethrowPtr);
388 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
389 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
390 0)),
391 Match, Unwind);
392
393 EmitBlock(Match);
394 }
395 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
396 Builder.CreateUnreachable();
397
398 if (Proto->getNumExceptions()) {
399 EmitBlock(Unwind);
Douglas Gregor86a3a032010-05-16 01:24:12 +0000400 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000401 Builder.CreateLoad(RethrowPtr));
402 Builder.CreateUnreachable();
403 }
404
405 EmitBlock(Cont);
406}
407
408void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000409 if (!Exceptions)
410 return;
411
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000412 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
413 if (FD == 0)
414 return;
415 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
416 if (Proto == 0)
417 return;
418
419 if (!Proto->hasExceptionSpec())
420 return;
421
422 setInvokeDest(0);
423}
424
Mike Stump2bf701e2009-11-20 23:44:51 +0000425void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall9fc6a772010-02-19 09:25:03 +0000426 CXXTryStmtInfo Info = EnterCXXTryStmt(S);
427 EmitStmt(S.getTryBlock());
428 ExitCXXTryStmt(S, Info);
429}
430
431CodeGenFunction::CXXTryStmtInfo
432CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
433 CXXTryStmtInfo Info;
434 Info.SavedLandingPad = getInvokeDest();
435 Info.HandlerBlock = createBasicBlock("try.handler");
436 Info.FinallyBlock = createBasicBlock("finally");
437
438 PushCleanupBlock(Info.FinallyBlock);
439 setInvokeDest(Info.HandlerBlock);
440
441 return Info;
442}
443
444void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
445 CXXTryStmtInfo TryInfo) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000446 // Pointer to the personality function
447 llvm::Constant *Personality =
448 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
449 (VMContext),
450 true),
451 "__gxx_personality_v0");
452 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000453 llvm::Value *llvm_eh_exception =
454 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
455 llvm::Value *llvm_eh_selector =
456 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000457
John McCall9fc6a772010-02-19 09:25:03 +0000458 llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
459 llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
460 llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
Mike Stump0f590be2009-12-01 03:41:18 +0000461 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000462 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
463
Mike Stump2bf701e2009-11-20 23:44:51 +0000464 // Jump to end if there is no exception
465 EmitBranchThroughCleanup(FinallyEnd);
466
Mike Stump9b39c512009-12-09 22:59:31 +0000467 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000468
Mike Stump2bf701e2009-11-20 23:44:51 +0000469 // Emit the handlers
470 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000471
Mike Stump2bf701e2009-11-20 23:44:51 +0000472 const llvm::IntegerType *Int8Ty;
473 const llvm::PointerType *PtrToInt8Ty;
474 Int8Ty = llvm::Type::getInt8Ty(VMContext);
475 // C string type. Used in lots of places.
476 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000477 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
478 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000479 llvm::Value *llvm_eh_typeid_for =
480 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000481 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000482 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000483 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000484
Mike Stump0f590be2009-12-01 03:41:18 +0000485 SelectorArgs.push_back(Exc);
486 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000487
Mike Stump0f590be2009-12-01 03:41:18 +0000488 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000489 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
490 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000491 VarDecl *CatchParam = C->getExceptionDecl();
492 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000493 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
494 // are ignored.
495 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000496 llvm::Value *EHTypeInfo
John McCall9dffe6f2010-04-30 01:15:21 +0000497 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType(), true);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000498 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000499 } else {
500 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000501 SelectorArgs.push_back(Null);
502 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000503 }
504 }
505
Mike Stump0f590be2009-12-01 03:41:18 +0000506 // We use a cleanup unless there was already a catch all.
507 if (!HasCatchAll) {
508 SelectorArgs.push_back(Null);
509 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000510
Mike Stump0f590be2009-12-01 03:41:18 +0000511 // Find which handler was matched.
512 llvm::Value *Selector
513 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
514 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000515 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
516 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000517 VarDecl *CatchParam = C->getExceptionDecl();
518 Stmt *CatchBody = C->getHandlerBlock();
519
520 llvm::BasicBlock *Next = 0;
521
522 if (SelectorArgs[i+2] != Null) {
523 llvm::BasicBlock *Match = createBasicBlock("match");
524 Next = createBasicBlock("catch.next");
525 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
526 llvm::Value *Id
527 = Builder.CreateCall(llvm_eh_typeid_for,
528 Builder.CreateBitCast(SelectorArgs[i+2],
529 Int8PtrTy));
530 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
531 Match, Next);
532 EmitBlock(Match);
533 }
534
535 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
536 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
537
538 PushCleanupBlock(MatchEnd);
539 setInvokeDest(MatchHandler);
540
541 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
542
Mike Stumpf7f74672009-12-02 23:37:16 +0000543 {
544 CleanupScope CatchScope(*this);
545 // Bind the catch parameter if it exists.
546 if (CatchParam) {
547 QualType CatchType = CatchParam->getType().getNonReferenceType();
548 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000549 bool WasPointer = true;
Mike Stump13b2f922010-01-01 03:20:32 +0000550 bool WasPointerReference = false;
Mike Stumpb606c382010-01-01 02:51:52 +0000551 CatchType = CGM.getContext().getCanonicalType(CatchType);
Mike Stump13b2f922010-01-01 03:20:32 +0000552 if (CatchType.getTypePtr()->isPointerType()) {
553 if (isa<ReferenceType>(CatchParam->getType()))
554 WasPointerReference = true;
555 } else {
Mike Stump0a3816e2009-12-04 01:51:45 +0000556 if (!isa<ReferenceType>(CatchParam->getType()))
557 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000558 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000559 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000560 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000561 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000562 // FIXME: we need to do this sooner so that the EH region for the
563 // cleanup doesn't start until after the ctor completes, use a decl
564 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000565 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump13b2f922010-01-01 03:20:32 +0000566 WasPointer, WasPointerReference, ExcObject,
Mike Stumpb606c382010-01-01 02:51:52 +0000567 GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000568 setInvokeDest(MatchHandler);
569 }
570
571 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000572 }
573
Mike Stump0f590be2009-12-01 03:41:18 +0000574 EmitBranchThroughCleanup(FinallyEnd);
575
576 EmitBlock(MatchHandler);
577
578 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
579 // We are required to emit this call to satisfy LLVM, even
580 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000581 llvm::Value *Args[] = {
582 Exc, Personality,
583 llvm::ConstantInt::getNullValue(llvm::Type::getInt32Ty(VMContext))
584 };
585 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump0f590be2009-12-01 03:41:18 +0000586 Builder.CreateStore(Exc, RethrowPtr);
587 EmitBranchThroughCleanup(FinallyRethrow);
588
589 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
590
591 EmitBlock(MatchEnd);
592
Mike Stump99533832009-12-02 07:41:41 +0000593 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000594 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000595 Cont, TerminateHandler,
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000596 &Args[0], &Args[0]);
Mike Stump0f590be2009-12-01 03:41:18 +0000597 EmitBlock(Cont);
598 if (Info.SwitchBlock)
599 EmitBlock(Info.SwitchBlock);
600 if (Info.EndBlock)
601 EmitBlock(Info.EndBlock);
602
Mike Stump0f590be2009-12-01 03:41:18 +0000603 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000604 Builder.CreateStore(Exc, RethrowPtr);
605 EmitBranchThroughCleanup(FinallyRethrow);
606
607 if (Next)
608 EmitBlock(Next);
609 }
Mike Stumpa0867832009-12-04 19:03:47 +0000610 if (!HasCatchAll) {
611 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000612 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000613 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000614
615 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
616
617 setInvokeDest(PrevLandingPad);
618
619 EmitBlock(FinallyBlock);
620
Mike Stump0f590be2009-12-01 03:41:18 +0000621 if (Info.SwitchBlock)
622 EmitBlock(Info.SwitchBlock);
623 if (Info.EndBlock)
624 EmitBlock(Info.EndBlock);
625
Mike Stump2bf701e2009-11-20 23:44:51 +0000626 // Branch around the rethrow code.
627 EmitBranch(FinallyEnd);
628
629 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000630 // FIXME: Eventually we can chain the handlers together and just do a call
631 // here.
632 if (getInvokeDest()) {
633 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000634 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000635 getInvokeDest(),
636 Builder.CreateLoad(RethrowPtr));
637 EmitBlock(Cont);
638 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000639 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000640 Builder.CreateLoad(RethrowPtr));
641
Mike Stump2bf701e2009-11-20 23:44:51 +0000642 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000643
644 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000645}
Mike Stumpd88ea562009-12-09 03:35:49 +0000646
647CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
Mike Stumpd88ea562009-12-09 03:35:49 +0000648 CGF.setInvokeDest(PreviousInvokeDest);
649
John McCall3d3ec1c2010-04-21 10:05:39 +0000650 llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock();
Mike Stumpd88ea562009-12-09 03:35:49 +0000651
John McCall3d3ec1c2010-04-21 10:05:39 +0000652 // Jump to the beginning of the cleanup.
653 CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin());
654
655 // The libstdc++ personality function.
656 // TODO: generalize to work with other libraries.
Mike Stumpd88ea562009-12-09 03:35:49 +0000657 llvm::Constant *Personality =
658 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
659 (CGF.VMContext),
660 true),
661 "__gxx_personality_v0");
662 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
John McCall3d3ec1c2010-04-21 10:05:39 +0000663
664 // %exception = call i8* @llvm.eh.exception()
665 // Magic intrinsic which tells gives us a handle to the caught
666 // exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000667 llvm::Value *llvm_eh_exception =
668 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
John McCall3d3ec1c2010-04-21 10:05:39 +0000669 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
670
671 llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty);
672
673 // %ignored = call i32 @llvm.eh.selector(i8* %exception,
674 // i8* @__gxx_personality_v0,
675 // i8* null)
676 // Magic intrinsic which tells LLVM that this invoke landing pad is
677 // just a cleanup block.
678 llvm::Value *Args[] = { Exc, Personality, Null };
Mike Stumpd88ea562009-12-09 03:35:49 +0000679 llvm::Value *llvm_eh_selector =
680 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000681 CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stumpd88ea562009-12-09 03:35:49 +0000682
John McCall3d3ec1c2010-04-21 10:05:39 +0000683 // And then we fall through into the code that the user put there.
684 // Jump back to the end of the cleanup.
685 CGF.Builder.SetInsertPoint(EndOfCleanup);
Mike Stumpd88ea562009-12-09 03:35:49 +0000686
John McCall3d3ec1c2010-04-21 10:05:39 +0000687 // Rethrow the exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000688 if (CGF.getInvokeDest()) {
689 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000690 CGF.Builder.CreateInvoke(CGF.getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpd88ea562009-12-09 03:35:49 +0000691 CGF.getInvokeDest(), Exc);
692 CGF.EmitBlock(Cont);
693 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000694 CGF.Builder.CreateCall(CGF.getUnwindResumeOrRethrowFn(), Exc);
Mike Stumpd88ea562009-12-09 03:35:49 +0000695 CGF.Builder.CreateUnreachable();
696
John McCall3d3ec1c2010-04-21 10:05:39 +0000697 // Resume inserting where we started, but put the new cleanup
698 // handler in place.
John McCall891f80e2010-04-30 00:06:43 +0000699 if (PreviousInsertionBlock)
700 CGF.Builder.SetInsertPoint(PreviousInsertionBlock);
701 else
702 CGF.Builder.ClearInsertionPoint();
703
Mike Stumpd88ea562009-12-09 03:35:49 +0000704 if (CGF.Exceptions)
705 CGF.setInvokeDest(CleanupHandler);
706}
Mike Stump9b39c512009-12-09 22:59:31 +0000707
708llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000709 if (TerminateHandler)
710 return TerminateHandler;
711
John McCall3d3ec1c2010-04-21 10:05:39 +0000712 // We don't want to change anything at the current location, so
713 // save it aside and clear the insert point.
714 llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock();
715 llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint();
716 Builder.ClearInsertionPoint();
Mike Stump76958092009-12-09 23:31:35 +0000717
Mike Stump9b39c512009-12-09 22:59:31 +0000718 llvm::Constant *Personality =
719 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
720 (VMContext),
721 true),
722 "__gxx_personality_v0");
723 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
724 llvm::Value *llvm_eh_exception =
725 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
726 llvm::Value *llvm_eh_selector =
727 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
728
729 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000730 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000731 EmitBlock(TerminateHandler);
732 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
733 // We are required to emit this call to satisfy LLVM, even
734 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000735 llvm::Value *Args[] = {
736 Exc, Personality,
737 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)
738 };
739 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump8755ec32009-12-10 00:06:18 +0000740 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000741 Builder.CreateCall(getTerminateFn(*this));
742 TerminateCall->setDoesNotReturn();
743 TerminateCall->setDoesNotThrow();
744 Builder.CreateUnreachable();
745
John McCall3d3ec1c2010-04-21 10:05:39 +0000746 // Restore the saved insertion state.
747 Builder.SetInsertPoint(SavedInsertBlock, SavedInsertPoint);
Mike Stump76958092009-12-09 23:31:35 +0000748
Mike Stump9b39c512009-12-09 22:59:31 +0000749 return TerminateHandler;
750}