CGException.cpp revision b6bbcc9995186799a60ce17d0c1acff31601653a
1//===--- 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 14#include "clang/AST/StmtCXX.h" 15 16#include "llvm/Intrinsics.h" 17#include "llvm/IntrinsicInst.h" 18#include "llvm/Support/CallSite.h" 19 20#include "CGObjCRuntime.h" 21#include "CodeGenFunction.h" 22#include "CGException.h" 23#include "TargetInfo.h" 24 25using namespace clang; 26using namespace CodeGen; 27 28/// Push an entry of the given size onto this protected-scope stack. 29char *EHScopeStack::allocate(size_t Size) { 30 if (!StartOfBuffer) { 31 unsigned Capacity = 1024; 32 while (Capacity < Size) Capacity *= 2; 33 StartOfBuffer = new char[Capacity]; 34 StartOfData = EndOfBuffer = StartOfBuffer + Capacity; 35 } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) { 36 unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer; 37 unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer); 38 39 unsigned NewCapacity = CurrentCapacity; 40 do { 41 NewCapacity *= 2; 42 } while (NewCapacity < UsedCapacity + Size); 43 44 char *NewStartOfBuffer = new char[NewCapacity]; 45 char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity; 46 char *NewStartOfData = NewEndOfBuffer - UsedCapacity; 47 memcpy(NewStartOfData, StartOfData, UsedCapacity); 48 delete [] StartOfBuffer; 49 StartOfBuffer = NewStartOfBuffer; 50 EndOfBuffer = NewEndOfBuffer; 51 StartOfData = NewStartOfData; 52 } 53 54 assert(StartOfBuffer + Size <= StartOfData); 55 StartOfData -= Size; 56 return StartOfData; 57} 58 59EHScopeStack::stable_iterator 60EHScopeStack::getEnclosingEHCleanup(iterator it) const { 61 assert(it != end()); 62 do { 63 if (isa<EHCleanupScope>(*it)) { 64 if (cast<EHCleanupScope>(*it).isEHCleanup()) 65 return stabilize(it); 66 return cast<EHCleanupScope>(*it).getEnclosingEHCleanup(); 67 } 68 ++it; 69 } while (it != end()); 70 return stable_end(); 71} 72 73 74void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) { 75 assert(((Size % sizeof(void*)) == 0) && "cleanup type is misaligned"); 76 char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size)); 77 bool IsNormalCleanup = Kind & NormalCleanup; 78 bool IsEHCleanup = Kind & EHCleanup; 79 bool IsActive = !(Kind & InactiveCleanup); 80 EHCleanupScope *Scope = 81 new (Buffer) EHCleanupScope(IsNormalCleanup, 82 IsEHCleanup, 83 IsActive, 84 Size, 85 BranchFixups.size(), 86 InnermostNormalCleanup, 87 InnermostEHCleanup); 88 if (IsNormalCleanup) 89 InnermostNormalCleanup = stable_begin(); 90 if (IsEHCleanup) 91 InnermostEHCleanup = stable_begin(); 92 93 return Scope->getCleanupBuffer(); 94} 95 96void EHScopeStack::popCleanup() { 97 assert(!empty() && "popping exception stack when not empty"); 98 99 assert(isa<EHCleanupScope>(*begin())); 100 EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin()); 101 InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup(); 102 InnermostEHCleanup = Cleanup.getEnclosingEHCleanup(); 103 StartOfData += Cleanup.getAllocatedSize(); 104 105 if (empty()) NextEHDestIndex = FirstEHDestIndex; 106 107 // Destroy the cleanup. 108 Cleanup.~EHCleanupScope(); 109 110 // Check whether we can shrink the branch-fixups stack. 111 if (!BranchFixups.empty()) { 112 // If we no longer have any normal cleanups, all the fixups are 113 // complete. 114 if (!hasNormalCleanups()) 115 BranchFixups.clear(); 116 117 // Otherwise we can still trim out unnecessary nulls. 118 else 119 popNullFixups(); 120 } 121} 122 123EHFilterScope *EHScopeStack::pushFilter(unsigned NumFilters) { 124 char *Buffer = allocate(EHFilterScope::getSizeForNumFilters(NumFilters)); 125 CatchDepth++; 126 return new (Buffer) EHFilterScope(NumFilters); 127} 128 129void EHScopeStack::popFilter() { 130 assert(!empty() && "popping exception stack when not empty"); 131 132 EHFilterScope &Filter = cast<EHFilterScope>(*begin()); 133 StartOfData += EHFilterScope::getSizeForNumFilters(Filter.getNumFilters()); 134 135 if (empty()) NextEHDestIndex = FirstEHDestIndex; 136 137 assert(CatchDepth > 0 && "mismatched filter push/pop"); 138 CatchDepth--; 139} 140 141EHCatchScope *EHScopeStack::pushCatch(unsigned NumHandlers) { 142 char *Buffer = allocate(EHCatchScope::getSizeForNumHandlers(NumHandlers)); 143 CatchDepth++; 144 EHCatchScope *Scope = new (Buffer) EHCatchScope(NumHandlers); 145 for (unsigned I = 0; I != NumHandlers; ++I) 146 Scope->getHandlers()[I].Index = getNextEHDestIndex(); 147 return Scope; 148} 149 150void EHScopeStack::pushTerminate() { 151 char *Buffer = allocate(EHTerminateScope::getSize()); 152 CatchDepth++; 153 new (Buffer) EHTerminateScope(getNextEHDestIndex()); 154} 155 156/// Remove any 'null' fixups on the stack. However, we can't pop more 157/// fixups than the fixup depth on the innermost normal cleanup, or 158/// else fixups that we try to add to that cleanup will end up in the 159/// wrong place. We *could* try to shrink fixup depths, but that's 160/// actually a lot of work for little benefit. 161void EHScopeStack::popNullFixups() { 162 // We expect this to only be called when there's still an innermost 163 // normal cleanup; otherwise there really shouldn't be any fixups. 164 assert(hasNormalCleanups()); 165 166 EHScopeStack::iterator it = find(InnermostNormalCleanup); 167 unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth(); 168 assert(BranchFixups.size() >= MinSize && "fixup stack out of order"); 169 170 while (BranchFixups.size() > MinSize && 171 BranchFixups.back().Destination == 0) 172 BranchFixups.pop_back(); 173} 174 175static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) { 176 // void *__cxa_allocate_exception(size_t thrown_size); 177 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 178 std::vector<const llvm::Type*> Args(1, SizeTy); 179 180 const llvm::FunctionType *FTy = 181 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()), 182 Args, false); 183 184 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); 185} 186 187static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) { 188 // void __cxa_free_exception(void *thrown_exception); 189 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 190 std::vector<const llvm::Type*> Args(1, Int8PtrTy); 191 192 const llvm::FunctionType *FTy = 193 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), 194 Args, false); 195 196 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception"); 197} 198 199static llvm::Constant *getThrowFn(CodeGenFunction &CGF) { 200 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo, 201 // void (*dest) (void *)); 202 203 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 204 std::vector<const llvm::Type*> Args(3, Int8PtrTy); 205 206 const llvm::FunctionType *FTy = 207 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), 208 Args, false); 209 210 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); 211} 212 213static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) { 214 // void __cxa_rethrow(); 215 216 const llvm::FunctionType *FTy = 217 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); 218 219 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); 220} 221 222static llvm::Constant *getGetExceptionPtrFn(CodeGenFunction &CGF) { 223 // void *__cxa_get_exception_ptr(void*); 224 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 225 std::vector<const llvm::Type*> Args(1, Int8PtrTy); 226 227 const llvm::FunctionType *FTy = 228 llvm::FunctionType::get(Int8PtrTy, Args, false); 229 230 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr"); 231} 232 233static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) { 234 // void *__cxa_begin_catch(void*); 235 236 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 237 std::vector<const llvm::Type*> Args(1, Int8PtrTy); 238 239 const llvm::FunctionType *FTy = 240 llvm::FunctionType::get(Int8PtrTy, Args, false); 241 242 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch"); 243} 244 245static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) { 246 // void __cxa_end_catch(); 247 248 const llvm::FunctionType *FTy = 249 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); 250 251 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch"); 252} 253 254static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) { 255 // void __cxa_call_unexepcted(void *thrown_exception); 256 257 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 258 std::vector<const llvm::Type*> Args(1, Int8PtrTy); 259 260 const llvm::FunctionType *FTy = 261 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), 262 Args, false); 263 264 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected"); 265} 266 267llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() { 268 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); 269 std::vector<const llvm::Type*> Args(1, Int8PtrTy); 270 271 const llvm::FunctionType *FTy = 272 llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), Args, 273 false); 274 275 if (CGM.getLangOptions().SjLjExceptions) 276 return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume_or_Rethrow"); 277 return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow"); 278} 279 280static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) { 281 // void __terminate(); 282 283 const llvm::FunctionType *FTy = 284 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false); 285 286 return CGF.CGM.CreateRuntimeFunction(FTy, 287 CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort"); 288} 289 290static llvm::Constant *getCatchallRethrowFn(CodeGenFunction &CGF, 291 llvm::StringRef Name) { 292 const llvm::Type *Int8PtrTy = 293 llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 294 std::vector<const llvm::Type*> Args(1, Int8PtrTy); 295 296 const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); 297 const llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, Args, false); 298 299 return CGF.CGM.CreateRuntimeFunction(FTy, Name); 300} 301 302const EHPersonality EHPersonality::GNU_C("__gcc_personality_v0"); 303const EHPersonality EHPersonality::NeXT_ObjC("__objc_personality_v0"); 304const EHPersonality EHPersonality::GNU_CPlusPlus("__gxx_personality_v0"); 305const EHPersonality EHPersonality::GNU_CPlusPlus_SJLJ("__gxx_personality_sj0"); 306const EHPersonality EHPersonality::GNU_ObjC("__gnu_objc_personality_v0", 307 "objc_exception_throw"); 308 309static const EHPersonality &getCPersonality(const LangOptions &L) { 310 return EHPersonality::GNU_C; 311} 312 313static const EHPersonality &getObjCPersonality(const LangOptions &L) { 314 if (L.NeXTRuntime) { 315 if (L.ObjCNonFragileABI) return EHPersonality::NeXT_ObjC; 316 else return getCPersonality(L); 317 } else { 318 return EHPersonality::GNU_ObjC; 319 } 320} 321 322static const EHPersonality &getCXXPersonality(const LangOptions &L) { 323 if (L.SjLjExceptions) 324 return EHPersonality::GNU_CPlusPlus_SJLJ; 325 else 326 return EHPersonality::GNU_CPlusPlus; 327} 328 329/// Determines the personality function to use when both C++ 330/// and Objective-C exceptions are being caught. 331static const EHPersonality &getObjCXXPersonality(const LangOptions &L) { 332 // The ObjC personality defers to the C++ personality for non-ObjC 333 // handlers. Unlike the C++ case, we use the same personality 334 // function on targets using (backend-driven) SJLJ EH. 335 if (L.NeXTRuntime) { 336 if (L.ObjCNonFragileABI) 337 return EHPersonality::NeXT_ObjC; 338 339 // In the fragile ABI, just use C++ exception handling and hope 340 // they're not doing crazy exception mixing. 341 else 342 return getCXXPersonality(L); 343 } 344 345 // The GNU runtime's personality function inherently doesn't support 346 // mixed EH. Use the C++ personality just to avoid returning null. 347 return getCXXPersonality(L); 348} 349 350const EHPersonality &EHPersonality::get(const LangOptions &L) { 351 if (L.CPlusPlus && L.ObjC1) 352 return getObjCXXPersonality(L); 353 else if (L.CPlusPlus) 354 return getCXXPersonality(L); 355 else if (L.ObjC1) 356 return getObjCPersonality(L); 357 else 358 return getCPersonality(L); 359} 360 361static llvm::Constant *getPersonalityFn(CodeGenModule &CGM, 362 const EHPersonality &Personality) { 363 llvm::Constant *Fn = 364 CGM.CreateRuntimeFunction(llvm::FunctionType::get( 365 llvm::Type::getInt32Ty(CGM.getLLVMContext()), 366 true), 367 Personality.getPersonalityFnName()); 368 return Fn; 369} 370 371static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM, 372 const EHPersonality &Personality) { 373 llvm::Constant *Fn = getPersonalityFn(CGM, Personality); 374 return llvm::ConstantExpr::getBitCast(Fn, CGM.PtrToInt8Ty); 375} 376 377/// Check whether a personality function could reasonably be swapped 378/// for a C++ personality function. 379static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) { 380 for (llvm::Constant::use_iterator 381 I = Fn->use_begin(), E = Fn->use_end(); I != E; ++I) { 382 llvm::User *User = *I; 383 384 // Conditionally white-list bitcasts. 385 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(User)) { 386 if (CE->getOpcode() != llvm::Instruction::BitCast) return false; 387 if (!PersonalityHasOnlyCXXUses(CE)) 388 return false; 389 continue; 390 } 391 392 // Otherwise, it has to be a selector call. 393 if (!isa<llvm::EHSelectorInst>(User)) return false; 394 395 llvm::EHSelectorInst *Selector = cast<llvm::EHSelectorInst>(User); 396 for (unsigned I = 2, E = Selector->getNumArgOperands(); I != E; ++I) { 397 // Look for something that would've been returned by the ObjC 398 // runtime's GetEHType() method. 399 llvm::GlobalVariable *GV 400 = dyn_cast<llvm::GlobalVariable>(Selector->getArgOperand(I)); 401 if (!GV) continue; 402 403 // ObjC EH selector entries are always global variables with 404 // names starting like this. 405 if (GV->getName().startswith("OBJC_EHTYPE")) 406 return false; 407 } 408 } 409 410 return true; 411} 412 413/// Try to use the C++ personality function in ObjC++. Not doing this 414/// can cause some incompatibilities with gcc, which is more 415/// aggressive about only using the ObjC++ personality in a function 416/// when it really needs it. 417void CodeGenModule::SimplifyPersonality() { 418 // For now, this is really a Darwin-specific operation. 419 if (Context.Target.getTriple().getOS() != llvm::Triple::Darwin) 420 return; 421 422 // If we're not in ObjC++ -fexceptions, there's nothing to do. 423 if (!Features.CPlusPlus || !Features.ObjC1 || !Features.Exceptions) 424 return; 425 426 const EHPersonality &ObjCXX = EHPersonality::get(Features); 427 const EHPersonality &CXX = getCXXPersonality(Features); 428 if (&ObjCXX == &CXX || 429 ObjCXX.getPersonalityFnName() == CXX.getPersonalityFnName()) 430 return; 431 432 llvm::Function *Fn = 433 getModule().getFunction(ObjCXX.getPersonalityFnName()); 434 435 // Nothing to do if it's unused. 436 if (!Fn || Fn->use_empty()) return; 437 438 // Can't do the optimization if it has non-C++ uses. 439 if (!PersonalityHasOnlyCXXUses(Fn)) return; 440 441 // Create the C++ personality function and kill off the old 442 // function. 443 llvm::Constant *CXXFn = getPersonalityFn(*this, CXX); 444 445 // This can happen if the user is screwing with us. 446 if (Fn->getType() != CXXFn->getType()) return; 447 448 Fn->replaceAllUsesWith(CXXFn); 449 Fn->eraseFromParent(); 450} 451 452/// Returns the value to inject into a selector to indicate the 453/// presence of a catch-all. 454static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) { 455 // Possibly we should use @llvm.eh.catch.all.value here. 456 return llvm::ConstantPointerNull::get(CGF.CGM.PtrToInt8Ty); 457} 458 459/// Returns the value to inject into a selector to indicate the 460/// presence of a cleanup. 461static llvm::Constant *getCleanupValue(CodeGenFunction &CGF) { 462 return llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); 463} 464 465namespace { 466 /// A cleanup to free the exception object if its initialization 467 /// throws. 468 struct FreeExceptionCleanup : EHScopeStack::Cleanup { 469 FreeExceptionCleanup(llvm::Value *ShouldFreeVar, 470 llvm::Value *ExnLocVar) 471 : ShouldFreeVar(ShouldFreeVar), ExnLocVar(ExnLocVar) {} 472 473 llvm::Value *ShouldFreeVar; 474 llvm::Value *ExnLocVar; 475 476 void Emit(CodeGenFunction &CGF, bool IsForEH) { 477 llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj"); 478 llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done"); 479 480 llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar, 481 "should-free-exnobj"); 482 CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB); 483 CGF.EmitBlock(FreeBB); 484 llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj"); 485 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal) 486 ->setDoesNotThrow(); 487 CGF.EmitBlock(DoneBB); 488 } 489 }; 490} 491 492// Emits an exception expression into the given location. This 493// differs from EmitAnyExprToMem only in that, if a final copy-ctor 494// call is required, an exception within that copy ctor causes 495// std::terminate to be invoked. 496static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E, 497 llvm::Value *ExnLoc) { 498 // We want to release the allocated exception object if this 499 // expression throws. We do this by pushing an EH-only cleanup 500 // block which, furthermore, deactivates itself after the expression 501 // is complete. 502 llvm::AllocaInst *ShouldFreeVar = 503 CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()), 504 "should-free-exnobj.var"); 505 CGF.InitTempAlloca(ShouldFreeVar, 506 llvm::ConstantInt::getFalse(CGF.getLLVMContext())); 507 508 // A variable holding the exception pointer. This is necessary 509 // because the throw expression does not necessarily dominate the 510 // cleanup, for example if it appears in a conditional expression. 511 llvm::AllocaInst *ExnLocVar = 512 CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var"); 513 514 // Make sure the exception object is cleaned up if there's an 515 // exception during initialization. 516 // FIXME: stmt expressions might require this to be a normal 517 // cleanup, too. 518 CGF.EHStack.pushCleanup<FreeExceptionCleanup>(EHCleanup, 519 ShouldFreeVar, 520 ExnLocVar); 521 EHScopeStack::stable_iterator Cleanup = CGF.EHStack.stable_begin(); 522 523 CGF.Builder.CreateStore(ExnLoc, ExnLocVar); 524 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()), 525 ShouldFreeVar); 526 527 // __cxa_allocate_exception returns a void*; we need to cast this 528 // to the appropriate type for the object. 529 const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo(); 530 llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty); 531 532 // FIXME: this isn't quite right! If there's a final unelided call 533 // to a copy constructor, then according to [except.terminate]p1 we 534 // must call std::terminate() if that constructor throws, because 535 // technically that copy occurs after the exception expression is 536 // evaluated but before the exception is caught. But the best way 537 // to handle that is to teach EmitAggExpr to do the final copy 538 // differently if it can't be elided. 539 CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false, /*IsInit*/ true); 540 541 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()), 542 ShouldFreeVar); 543 544 // Technically, the exception object is like a temporary; it has to 545 // be cleaned up when its full-expression is complete. 546 // Unfortunately, the AST represents full-expressions by creating a 547 // CXXExprWithTemporaries, which it only does when there are actually 548 // temporaries. 549 // 550 // If any cleanups have been added since we pushed ours, they must 551 // be from temporaries; this will get popped at the same time. 552 // Otherwise we need to pop ours off. FIXME: this is very brittle. 553 if (Cleanup == CGF.EHStack.stable_begin()) 554 CGF.PopCleanupBlock(); 555} 556 557llvm::Value *CodeGenFunction::getExceptionSlot() { 558 if (!ExceptionSlot) { 559 const llvm::Type *i8p = llvm::Type::getInt8PtrTy(getLLVMContext()); 560 ExceptionSlot = CreateTempAlloca(i8p, "exn.slot"); 561 } 562 return ExceptionSlot; 563} 564 565void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) { 566 if (!E->getSubExpr()) { 567 if (getInvokeDest()) { 568 Builder.CreateInvoke(getReThrowFn(*this), 569 getUnreachableBlock(), 570 getInvokeDest()) 571 ->setDoesNotReturn(); 572 } else { 573 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn(); 574 Builder.CreateUnreachable(); 575 } 576 577 // Clear the insertion point to indicate we are in unreachable code. 578 Builder.ClearInsertionPoint(); 579 return; 580 } 581 582 QualType ThrowType = E->getSubExpr()->getType(); 583 584 // Now allocate the exception object. 585 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 586 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity(); 587 588 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this); 589 llvm::CallInst *ExceptionPtr = 590 Builder.CreateCall(AllocExceptionFn, 591 llvm::ConstantInt::get(SizeTy, TypeSize), 592 "exception"); 593 ExceptionPtr->setDoesNotThrow(); 594 595 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr); 596 597 // Now throw the exception. 598 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); 599 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true); 600 601 // The address of the destructor. If the exception type has a 602 // trivial destructor (or isn't a record), we just pass null. 603 llvm::Constant *Dtor = 0; 604 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) { 605 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 606 if (!Record->hasTrivialDestructor()) { 607 CXXDestructorDecl *DtorD = Record->getDestructor(); 608 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete); 609 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy); 610 } 611 } 612 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy); 613 614 if (getInvokeDest()) { 615 llvm::InvokeInst *ThrowCall = 616 Builder.CreateInvoke3(getThrowFn(*this), 617 getUnreachableBlock(), getInvokeDest(), 618 ExceptionPtr, TypeInfo, Dtor); 619 ThrowCall->setDoesNotReturn(); 620 } else { 621 llvm::CallInst *ThrowCall = 622 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor); 623 ThrowCall->setDoesNotReturn(); 624 Builder.CreateUnreachable(); 625 } 626 627 // Clear the insertion point to indicate we are in unreachable code. 628 Builder.ClearInsertionPoint(); 629 630 // FIXME: For now, emit a dummy basic block because expr emitters in generally 631 // are not ready to handle emitting expressions at unreachable points. 632 EnsureInsertPoint(); 633} 634 635void CodeGenFunction::EmitStartEHSpec(const Decl *D) { 636 if (!Exceptions) 637 return; 638 639 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); 640 if (FD == 0) 641 return; 642 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); 643 if (Proto == 0) 644 return; 645 646 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack"); 647 648 if (!Proto->hasExceptionSpec()) 649 return; 650 651 unsigned NumExceptions = Proto->getNumExceptions(); 652 EHFilterScope *Filter = EHStack.pushFilter(NumExceptions); 653 654 for (unsigned I = 0; I != NumExceptions; ++I) { 655 QualType Ty = Proto->getExceptionType(I); 656 QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType(); 657 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true); 658 Filter->setFilter(I, EHType); 659 } 660} 661 662void CodeGenFunction::EmitEndEHSpec(const Decl *D) { 663 if (!Exceptions) 664 return; 665 666 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D); 667 if (FD == 0) 668 return; 669 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>(); 670 if (Proto == 0) 671 return; 672 673 if (!Proto->hasExceptionSpec()) 674 return; 675 676 EHStack.popFilter(); 677} 678 679void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { 680 EnterCXXTryStmt(S); 681 EmitStmt(S.getTryBlock()); 682 ExitCXXTryStmt(S); 683} 684 685void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { 686 unsigned NumHandlers = S.getNumHandlers(); 687 EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers); 688 689 for (unsigned I = 0; I != NumHandlers; ++I) { 690 const CXXCatchStmt *C = S.getHandler(I); 691 692 llvm::BasicBlock *Handler = createBasicBlock("catch"); 693 if (C->getExceptionDecl()) { 694 // FIXME: Dropping the reference type on the type into makes it 695 // impossible to correctly implement catch-by-reference 696 // semantics for pointers. Unfortunately, this is what all 697 // existing compilers do, and it's not clear that the standard 698 // personality routine is capable of doing this right. See C++ DR 388: 699 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388 700 QualType CaughtType = C->getCaughtType(); 701 CaughtType = CaughtType.getNonReferenceType().getUnqualifiedType(); 702 703 llvm::Value *TypeInfo = 0; 704 if (CaughtType->isObjCObjectPointerType()) 705 TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType); 706 else 707 TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, true); 708 CatchScope->setHandler(I, TypeInfo, Handler); 709 } else { 710 // No exception decl indicates '...', a catch-all. 711 CatchScope->setCatchAllHandler(I, Handler); 712 } 713 } 714} 715 716/// Check whether this is a non-EH scope, i.e. a scope which doesn't 717/// affect exception handling. Currently, the only non-EH scopes are 718/// normal-only cleanup scopes. 719static bool isNonEHScope(const EHScope &S) { 720 switch (S.getKind()) { 721 case EHScope::Cleanup: 722 return !cast<EHCleanupScope>(S).isEHCleanup(); 723 case EHScope::Filter: 724 case EHScope::Catch: 725 case EHScope::Terminate: 726 return false; 727 } 728 729 // Suppress warning. 730 return false; 731} 732 733llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() { 734 assert(EHStack.requiresLandingPad()); 735 assert(!EHStack.empty()); 736 737 if (!Exceptions) 738 return 0; 739 740 // Check the innermost scope for a cached landing pad. If this is 741 // a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad. 742 llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad(); 743 if (LP) return LP; 744 745 // Build the landing pad for this scope. 746 LP = EmitLandingPad(); 747 assert(LP); 748 749 // Cache the landing pad on the innermost scope. If this is a 750 // non-EH scope, cache the landing pad on the enclosing scope, too. 751 for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) { 752 ir->setCachedLandingPad(LP); 753 if (!isNonEHScope(*ir)) break; 754 } 755 756 return LP; 757} 758 759llvm::BasicBlock *CodeGenFunction::EmitLandingPad() { 760 assert(EHStack.requiresLandingPad()); 761 762 // This function contains a hack to work around a design flaw in 763 // LLVM's EH IR which breaks semantics after inlining. This same 764 // hack is implemented in llvm-gcc. 765 // 766 // The LLVM EH abstraction is basically a thin veneer over the 767 // traditional GCC zero-cost design: for each range of instructions 768 // in the function, there is (at most) one "landing pad" with an 769 // associated chain of EH actions. A language-specific personality 770 // function interprets this chain of actions and (1) decides whether 771 // or not to resume execution at the landing pad and (2) if so, 772 // provides an integer indicating why it's stopping. In LLVM IR, 773 // the association of a landing pad with a range of instructions is 774 // achieved via an invoke instruction, the chain of actions becomes 775 // the arguments to the @llvm.eh.selector call, and the selector 776 // call returns the integer indicator. Other than the required 777 // presence of two intrinsic function calls in the landing pad, 778 // the IR exactly describes the layout of the output code. 779 // 780 // A principal advantage of this design is that it is completely 781 // language-agnostic; in theory, the LLVM optimizers can treat 782 // landing pads neutrally, and targets need only know how to lower 783 // the intrinsics to have a functioning exceptions system (assuming 784 // that platform exceptions follow something approximately like the 785 // GCC design). Unfortunately, landing pads cannot be combined in a 786 // language-agnostic way: given selectors A and B, there is no way 787 // to make a single landing pad which faithfully represents the 788 // semantics of propagating an exception first through A, then 789 // through B, without knowing how the personality will interpret the 790 // (lowered form of the) selectors. This means that inlining has no 791 // choice but to crudely chain invokes (i.e., to ignore invokes in 792 // the inlined function, but to turn all unwindable calls into 793 // invokes), which is only semantically valid if every unwind stops 794 // at every landing pad. 795 // 796 // Therefore, the invoke-inline hack is to guarantee that every 797 // landing pad has a catch-all. 798 const bool UseInvokeInlineHack = true; 799 800 for (EHScopeStack::iterator ir = EHStack.begin(); ; ) { 801 assert(ir != EHStack.end() && 802 "stack requiring landing pad is nothing but non-EH scopes?"); 803 804 // If this is a terminate scope, just use the singleton terminate 805 // landing pad. 806 if (isa<EHTerminateScope>(*ir)) 807 return getTerminateLandingPad(); 808 809 // If this isn't an EH scope, iterate; otherwise break out. 810 if (!isNonEHScope(*ir)) break; 811 ++ir; 812 813 // We haven't checked this scope for a cached landing pad yet. 814 if (llvm::BasicBlock *LP = ir->getCachedLandingPad()) 815 return LP; 816 } 817 818 // Save the current IR generation state. 819 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 820 821 const EHPersonality &Personality = 822 EHPersonality::get(CGF.CGM.getLangOptions()); 823 824 // Create and configure the landing pad. 825 llvm::BasicBlock *LP = createBasicBlock("lpad"); 826 EmitBlock(LP); 827 828 // Save the exception pointer. It's safe to use a single exception 829 // pointer per function because EH cleanups can never have nested 830 // try/catches. 831 llvm::CallInst *Exn = 832 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_exception), "exn"); 833 Exn->setDoesNotThrow(); 834 Builder.CreateStore(Exn, getExceptionSlot()); 835 836 // Build the selector arguments. 837 llvm::SmallVector<llvm::Value*, 8> EHSelector; 838 EHSelector.push_back(Exn); 839 EHSelector.push_back(getOpaquePersonalityFn(CGM, Personality)); 840 841 // Accumulate all the handlers in scope. 842 llvm::DenseMap<llvm::Value*, UnwindDest> EHHandlers; 843 UnwindDest CatchAll; 844 bool HasEHCleanup = false; 845 bool HasEHFilter = false; 846 llvm::SmallVector<llvm::Value*, 8> EHFilters; 847 for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end(); 848 I != E; ++I) { 849 850 switch (I->getKind()) { 851 case EHScope::Cleanup: 852 if (!HasEHCleanup) 853 HasEHCleanup = cast<EHCleanupScope>(*I).isEHCleanup(); 854 // We otherwise don't care about cleanups. 855 continue; 856 857 case EHScope::Filter: { 858 assert(I.next() == EHStack.end() && "EH filter is not end of EH stack"); 859 assert(!CatchAll.isValid() && "EH filter reached after catch-all"); 860 861 // Filter scopes get added to the selector in wierd ways. 862 EHFilterScope &Filter = cast<EHFilterScope>(*I); 863 HasEHFilter = true; 864 865 // Add all the filter values which we aren't already explicitly 866 // catching. 867 for (unsigned I = 0, E = Filter.getNumFilters(); I != E; ++I) { 868 llvm::Value *FV = Filter.getFilter(I); 869 if (!EHHandlers.count(FV)) 870 EHFilters.push_back(FV); 871 } 872 goto done; 873 } 874 875 case EHScope::Terminate: 876 // Terminate scopes are basically catch-alls. 877 assert(!CatchAll.isValid()); 878 CatchAll = UnwindDest(getTerminateHandler(), 879 EHStack.getEnclosingEHCleanup(I), 880 cast<EHTerminateScope>(*I).getDestIndex()); 881 goto done; 882 883 case EHScope::Catch: 884 break; 885 } 886 887 EHCatchScope &Catch = cast<EHCatchScope>(*I); 888 for (unsigned HI = 0, HE = Catch.getNumHandlers(); HI != HE; ++HI) { 889 EHCatchScope::Handler Handler = Catch.getHandler(HI); 890 891 // Catch-all. We should only have one of these per catch. 892 if (!Handler.Type) { 893 assert(!CatchAll.isValid()); 894 CatchAll = UnwindDest(Handler.Block, 895 EHStack.getEnclosingEHCleanup(I), 896 Handler.Index); 897 continue; 898 } 899 900 // Check whether we already have a handler for this type. 901 UnwindDest &Dest = EHHandlers[Handler.Type]; 902 if (Dest.isValid()) continue; 903 904 EHSelector.push_back(Handler.Type); 905 Dest = UnwindDest(Handler.Block, 906 EHStack.getEnclosingEHCleanup(I), 907 Handler.Index); 908 } 909 910 // Stop if we found a catch-all. 911 if (CatchAll.isValid()) break; 912 } 913 914 done: 915 unsigned LastToEmitInLoop = EHSelector.size(); 916 917 // If we have a catch-all, add null to the selector. 918 if (CatchAll.isValid()) { 919 EHSelector.push_back(getCatchAllValue(CGF)); 920 921 // If we have an EH filter, we need to add those handlers in the 922 // right place in the selector, which is to say, at the end. 923 } else if (HasEHFilter) { 924 // Create a filter expression: an integer constant saying how many 925 // filters there are (+1 to avoid ambiguity with 0 for cleanup), 926 // followed by the filter types. The personality routine only 927 // lands here if the filter doesn't match. 928 EHSelector.push_back(llvm::ConstantInt::get(Builder.getInt32Ty(), 929 EHFilters.size() + 1)); 930 EHSelector.append(EHFilters.begin(), EHFilters.end()); 931 932 // Also check whether we need a cleanup. 933 if (UseInvokeInlineHack || HasEHCleanup) 934 EHSelector.push_back(UseInvokeInlineHack 935 ? getCatchAllValue(CGF) 936 : getCleanupValue(CGF)); 937 938 // Otherwise, signal that we at least have cleanups. 939 } else if (UseInvokeInlineHack || HasEHCleanup) { 940 EHSelector.push_back(UseInvokeInlineHack 941 ? getCatchAllValue(CGF) 942 : getCleanupValue(CGF)); 943 } else { 944 assert(LastToEmitInLoop > 2); 945 LastToEmitInLoop--; 946 } 947 948 assert(EHSelector.size() >= 3 && "selector call has only two arguments!"); 949 950 // Tell the backend how to generate the landing pad. 951 llvm::CallInst *Selection = 952 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_selector), 953 EHSelector.begin(), EHSelector.end(), "eh.selector"); 954 Selection->setDoesNotThrow(); 955 956 // Select the right handler. 957 llvm::Value *llvm_eh_typeid_for = 958 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); 959 960 // The results of llvm_eh_typeid_for aren't reliable --- at least 961 // not locally --- so we basically have to do this as an 'if' chain. 962 // We walk through the first N-1 catch clauses, testing and chaining, 963 // and then fall into the final clause (which is either a cleanup, a 964 // filter (possibly with a cleanup), a catch-all, or another catch). 965 for (unsigned I = 2; I != LastToEmitInLoop; ++I) { 966 llvm::Value *Type = EHSelector[I]; 967 UnwindDest Dest = EHHandlers[Type]; 968 assert(Dest.isValid() && "no handler entry for value in selector?"); 969 970 // Figure out where to branch on a match. As a debug code-size 971 // optimization, if the scope depth matches the innermost cleanup, 972 // we branch directly to the catch handler. 973 llvm::BasicBlock *Match = Dest.getBlock(); 974 bool MatchNeedsCleanup = 975 Dest.getScopeDepth() != EHStack.getInnermostEHCleanup(); 976 if (MatchNeedsCleanup) 977 Match = createBasicBlock("eh.match"); 978 979 llvm::BasicBlock *Next = createBasicBlock("eh.next"); 980 981 // Check whether the exception matches. 982 llvm::CallInst *Id 983 = Builder.CreateCall(llvm_eh_typeid_for, 984 Builder.CreateBitCast(Type, CGM.PtrToInt8Ty)); 985 Id->setDoesNotThrow(); 986 Builder.CreateCondBr(Builder.CreateICmpEQ(Selection, Id), 987 Match, Next); 988 989 // Emit match code if necessary. 990 if (MatchNeedsCleanup) { 991 EmitBlock(Match); 992 EmitBranchThroughEHCleanup(Dest); 993 } 994 995 // Continue to the next match. 996 EmitBlock(Next); 997 } 998 999 // Emit the final case in the selector. 1000 // This might be a catch-all.... 1001 if (CatchAll.isValid()) { 1002 assert(isa<llvm::ConstantPointerNull>(EHSelector.back())); 1003 EmitBranchThroughEHCleanup(CatchAll); 1004 1005 // ...or an EH filter... 1006 } else if (HasEHFilter) { 1007 llvm::Value *SavedSelection = Selection; 1008 1009 // First, unwind out to the outermost scope if necessary. 1010 if (EHStack.hasEHCleanups()) { 1011 // The end here might not dominate the beginning, so we might need to 1012 // save the selector if we need it. 1013 llvm::AllocaInst *SelectorVar = 0; 1014 if (HasEHCleanup) { 1015 SelectorVar = CreateTempAlloca(Builder.getInt32Ty(), "selector.var"); 1016 Builder.CreateStore(Selection, SelectorVar); 1017 } 1018 1019 llvm::BasicBlock *CleanupContBB = createBasicBlock("ehspec.cleanup.cont"); 1020 EmitBranchThroughEHCleanup(UnwindDest(CleanupContBB, EHStack.stable_end(), 1021 EHStack.getNextEHDestIndex())); 1022 EmitBlock(CleanupContBB); 1023 1024 if (HasEHCleanup) 1025 SavedSelection = Builder.CreateLoad(SelectorVar, "ehspec.saved-selector"); 1026 } 1027 1028 // If there was a cleanup, we'll need to actually check whether we 1029 // landed here because the filter triggered. 1030 if (UseInvokeInlineHack || HasEHCleanup) { 1031 llvm::BasicBlock *RethrowBB = createBasicBlock("cleanup"); 1032 llvm::BasicBlock *UnexpectedBB = createBasicBlock("ehspec.unexpected"); 1033 1034 llvm::Constant *Zero = llvm::ConstantInt::get(Builder.getInt32Ty(), 0); 1035 llvm::Value *FailsFilter = 1036 Builder.CreateICmpSLT(SavedSelection, Zero, "ehspec.fails"); 1037 Builder.CreateCondBr(FailsFilter, UnexpectedBB, RethrowBB); 1038 1039 // The rethrow block is where we land if this was a cleanup. 1040 // TODO: can this be _Unwind_Resume if the InvokeInlineHack is off? 1041 EmitBlock(RethrowBB); 1042 Builder.CreateCall(getUnwindResumeOrRethrowFn(), 1043 Builder.CreateLoad(getExceptionSlot())) 1044 ->setDoesNotReturn(); 1045 Builder.CreateUnreachable(); 1046 1047 EmitBlock(UnexpectedBB); 1048 } 1049 1050 // Call __cxa_call_unexpected. This doesn't need to be an invoke 1051 // because __cxa_call_unexpected magically filters exceptions 1052 // according to the last landing pad the exception was thrown 1053 // into. Seriously. 1054 Builder.CreateCall(getUnexpectedFn(*this), 1055 Builder.CreateLoad(getExceptionSlot())) 1056 ->setDoesNotReturn(); 1057 Builder.CreateUnreachable(); 1058 1059 // ...or a normal catch handler... 1060 } else if (!UseInvokeInlineHack && !HasEHCleanup) { 1061 llvm::Value *Type = EHSelector.back(); 1062 EmitBranchThroughEHCleanup(EHHandlers[Type]); 1063 1064 // ...or a cleanup. 1065 } else { 1066 EmitBranchThroughEHCleanup(getRethrowDest()); 1067 } 1068 1069 // Restore the old IR generation state. 1070 Builder.restoreIP(SavedIP); 1071 1072 return LP; 1073} 1074 1075namespace { 1076 /// A cleanup to call __cxa_end_catch. In many cases, the caught 1077 /// exception type lets us state definitively that the thrown exception 1078 /// type does not have a destructor. In particular: 1079 /// - Catch-alls tell us nothing, so we have to conservatively 1080 /// assume that the thrown exception might have a destructor. 1081 /// - Catches by reference behave according to their base types. 1082 /// - Catches of non-record types will only trigger for exceptions 1083 /// of non-record types, which never have destructors. 1084 /// - Catches of record types can trigger for arbitrary subclasses 1085 /// of the caught type, so we have to assume the actual thrown 1086 /// exception type might have a throwing destructor, even if the 1087 /// caught type's destructor is trivial or nothrow. 1088 struct CallEndCatch : EHScopeStack::Cleanup { 1089 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {} 1090 bool MightThrow; 1091 1092 void Emit(CodeGenFunction &CGF, bool IsForEH) { 1093 if (!MightThrow) { 1094 CGF.Builder.CreateCall(getEndCatchFn(CGF))->setDoesNotThrow(); 1095 return; 1096 } 1097 1098 CGF.EmitCallOrInvoke(getEndCatchFn(CGF), 0, 0); 1099 } 1100 }; 1101} 1102 1103/// Emits a call to __cxa_begin_catch and enters a cleanup to call 1104/// __cxa_end_catch. 1105/// 1106/// \param EndMightThrow - true if __cxa_end_catch might throw 1107static llvm::Value *CallBeginCatch(CodeGenFunction &CGF, 1108 llvm::Value *Exn, 1109 bool EndMightThrow) { 1110 llvm::CallInst *Call = CGF.Builder.CreateCall(getBeginCatchFn(CGF), Exn); 1111 Call->setDoesNotThrow(); 1112 1113 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow); 1114 1115 return Call; 1116} 1117 1118/// A "special initializer" callback for initializing a catch 1119/// parameter during catch initialization. 1120static void InitCatchParam(CodeGenFunction &CGF, 1121 const VarDecl &CatchParam, 1122 llvm::Value *ParamAddr) { 1123 // Load the exception from where the landing pad saved it. 1124 llvm::Value *Exn = CGF.Builder.CreateLoad(CGF.getExceptionSlot(), "exn"); 1125 1126 CanQualType CatchType = 1127 CGF.CGM.getContext().getCanonicalType(CatchParam.getType()); 1128 const llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType); 1129 1130 // If we're catching by reference, we can just cast the object 1131 // pointer to the appropriate pointer. 1132 if (isa<ReferenceType>(CatchType)) { 1133 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType(); 1134 bool EndCatchMightThrow = CaughtType->isRecordType(); 1135 1136 // __cxa_begin_catch returns the adjusted object pointer. 1137 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow); 1138 1139 // We have no way to tell the personality function that we're 1140 // catching by reference, so if we're catching a pointer, 1141 // __cxa_begin_catch will actually return that pointer by value. 1142 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) { 1143 QualType PointeeType = PT->getPointeeType(); 1144 1145 // When catching by reference, generally we should just ignore 1146 // this by-value pointer and use the exception object instead. 1147 if (!PointeeType->isRecordType()) { 1148 1149 // Exn points to the struct _Unwind_Exception header, which 1150 // we have to skip past in order to reach the exception data. 1151 unsigned HeaderSize = 1152 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException(); 1153 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize); 1154 1155 // However, if we're catching a pointer-to-record type that won't 1156 // work, because the personality function might have adjusted 1157 // the pointer. There's actually no way for us to fully satisfy 1158 // the language/ABI contract here: we can't use Exn because it 1159 // might have the wrong adjustment, but we can't use the by-value 1160 // pointer because it's off by a level of abstraction. 1161 // 1162 // The current solution is to dump the adjusted pointer into an 1163 // alloca, which breaks language semantics (because changing the 1164 // pointer doesn't change the exception) but at least works. 1165 // The better solution would be to filter out non-exact matches 1166 // and rethrow them, but this is tricky because the rethrow 1167 // really needs to be catchable by other sites at this landing 1168 // pad. The best solution is to fix the personality function. 1169 } else { 1170 // Pull the pointer for the reference type off. 1171 const llvm::Type *PtrTy = 1172 cast<llvm::PointerType>(LLVMCatchTy)->getElementType(); 1173 1174 // Create the temporary and write the adjusted pointer into it. 1175 llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp"); 1176 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); 1177 CGF.Builder.CreateStore(Casted, ExnPtrTmp); 1178 1179 // Bind the reference to the temporary. 1180 AdjustedExn = ExnPtrTmp; 1181 } 1182 } 1183 1184 llvm::Value *ExnCast = 1185 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref"); 1186 CGF.Builder.CreateStore(ExnCast, ParamAddr); 1187 return; 1188 } 1189 1190 // Non-aggregates (plus complexes). 1191 bool IsComplex = false; 1192 if (!CGF.hasAggregateLLVMType(CatchType) || 1193 (IsComplex = CatchType->isAnyComplexType())) { 1194 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false); 1195 1196 // If the catch type is a pointer type, __cxa_begin_catch returns 1197 // the pointer by value. 1198 if (CatchType->hasPointerRepresentation()) { 1199 llvm::Value *CastExn = 1200 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted"); 1201 CGF.Builder.CreateStore(CastExn, ParamAddr); 1202 return; 1203 } 1204 1205 // Otherwise, it returns a pointer into the exception object. 1206 1207 const llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok 1208 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); 1209 1210 if (IsComplex) { 1211 CGF.StoreComplexToAddr(CGF.LoadComplexFromAddr(Cast, /*volatile*/ false), 1212 ParamAddr, /*volatile*/ false); 1213 } else { 1214 unsigned Alignment = 1215 CGF.getContext().getDeclAlign(&CatchParam).getQuantity(); 1216 llvm::Value *ExnLoad = CGF.Builder.CreateLoad(Cast, "exn.scalar"); 1217 CGF.EmitStoreOfScalar(ExnLoad, ParamAddr, /*volatile*/ false, Alignment, 1218 CatchType); 1219 } 1220 return; 1221 } 1222 1223 // FIXME: this *really* needs to be done via a proper, Sema-emitted 1224 // initializer expression. 1225 1226 CXXRecordDecl *RD = CatchType.getTypePtr()->getAsCXXRecordDecl(); 1227 assert(RD && "aggregate catch type was not a record!"); 1228 1229 const llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok 1230 1231 if (RD->hasTrivialCopyConstructor()) { 1232 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, true); 1233 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); 1234 CGF.EmitAggregateCopy(ParamAddr, Cast, CatchType); 1235 return; 1236 } 1237 1238 // We have to call __cxa_get_exception_ptr to get the adjusted 1239 // pointer before copying. 1240 llvm::CallInst *AdjustedExn = 1241 CGF.Builder.CreateCall(getGetExceptionPtrFn(CGF), Exn); 1242 AdjustedExn->setDoesNotThrow(); 1243 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); 1244 1245 CXXConstructorDecl *CD = RD->getCopyConstructor(CGF.getContext(), 0); 1246 assert(CD && "record has no copy constructor!"); 1247 llvm::Value *CopyCtor = CGF.CGM.GetAddrOfCXXConstructor(CD, Ctor_Complete); 1248 1249 CallArgList CallArgs; 1250 CallArgs.push_back(std::make_pair(RValue::get(ParamAddr), 1251 CD->getThisType(CGF.getContext()))); 1252 CallArgs.push_back(std::make_pair(RValue::get(Cast), 1253 CD->getParamDecl(0)->getType())); 1254 1255 const FunctionProtoType *FPT 1256 = CD->getType()->getAs<FunctionProtoType>(); 1257 1258 // Call the copy ctor in a terminate scope. 1259 CGF.EHStack.pushTerminate(); 1260 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT), 1261 CopyCtor, ReturnValueSlot(), CallArgs, CD); 1262 CGF.EHStack.popTerminate(); 1263 1264 // Finally we can call __cxa_begin_catch. 1265 CallBeginCatch(CGF, Exn, true); 1266} 1267 1268/// Begins a catch statement by initializing the catch variable and 1269/// calling __cxa_begin_catch. 1270static void BeginCatch(CodeGenFunction &CGF, 1271 const CXXCatchStmt *S) { 1272 // We have to be very careful with the ordering of cleanups here: 1273 // C++ [except.throw]p4: 1274 // The destruction [of the exception temporary] occurs 1275 // immediately after the destruction of the object declared in 1276 // the exception-declaration in the handler. 1277 // 1278 // So the precise ordering is: 1279 // 1. Construct catch variable. 1280 // 2. __cxa_begin_catch 1281 // 3. Enter __cxa_end_catch cleanup 1282 // 4. Enter dtor cleanup 1283 // 1284 // We do this by initializing the exception variable with a 1285 // "special initializer", InitCatchParam. Delegation sequence: 1286 // - ExitCXXTryStmt opens a RunCleanupsScope 1287 // - EmitLocalBlockVarDecl creates the variable and debug info 1288 // - InitCatchParam initializes the variable from the exception 1289 // - CallBeginCatch calls __cxa_begin_catch 1290 // - CallBeginCatch enters the __cxa_end_catch cleanup 1291 // - EmitLocalBlockVarDecl enters the variable destructor cleanup 1292 // - EmitCXXTryStmt emits the code for the catch body 1293 // - EmitCXXTryStmt close the RunCleanupsScope 1294 1295 VarDecl *CatchParam = S->getExceptionDecl(); 1296 if (!CatchParam) { 1297 llvm::Value *Exn = CGF.Builder.CreateLoad(CGF.getExceptionSlot(), "exn"); 1298 CallBeginCatch(CGF, Exn, true); 1299 return; 1300 } 1301 1302 // Emit the local. 1303 CGF.EmitAutoVarDecl(*CatchParam, &InitCatchParam); 1304} 1305 1306namespace { 1307 struct CallRethrow : EHScopeStack::Cleanup { 1308 void Emit(CodeGenFunction &CGF, bool IsForEH) { 1309 CGF.EmitCallOrInvoke(getReThrowFn(CGF), 0, 0); 1310 } 1311 }; 1312} 1313 1314void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) { 1315 unsigned NumHandlers = S.getNumHandlers(); 1316 EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin()); 1317 assert(CatchScope.getNumHandlers() == NumHandlers); 1318 1319 // Copy the handler blocks off before we pop the EH stack. Emitting 1320 // the handlers might scribble on this memory. 1321 llvm::SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers); 1322 memcpy(Handlers.data(), CatchScope.begin(), 1323 NumHandlers * sizeof(EHCatchScope::Handler)); 1324 EHStack.popCatch(); 1325 1326 // The fall-through block. 1327 llvm::BasicBlock *ContBB = createBasicBlock("try.cont"); 1328 1329 // We just emitted the body of the try; jump to the continue block. 1330 if (HaveInsertPoint()) 1331 Builder.CreateBr(ContBB); 1332 1333 // Determine if we need an implicit rethrow for all these catch handlers. 1334 bool ImplicitRethrow = false; 1335 if (IsFnTryBlock) 1336 ImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) || 1337 isa<CXXConstructorDecl>(CurCodeDecl); 1338 1339 for (unsigned I = 0; I != NumHandlers; ++I) { 1340 llvm::BasicBlock *CatchBlock = Handlers[I].Block; 1341 EmitBlock(CatchBlock); 1342 1343 // Catch the exception if this isn't a catch-all. 1344 const CXXCatchStmt *C = S.getHandler(I); 1345 1346 // Enter a cleanup scope, including the catch variable and the 1347 // end-catch. 1348 RunCleanupsScope CatchScope(*this); 1349 1350 // Initialize the catch variable and set up the cleanups. 1351 BeginCatch(*this, C); 1352 1353 // If there's an implicit rethrow, push a normal "cleanup" to call 1354 // _cxa_rethrow. This needs to happen before __cxa_end_catch is 1355 // called, and so it is pushed after BeginCatch. 1356 if (ImplicitRethrow) 1357 EHStack.pushCleanup<CallRethrow>(NormalCleanup); 1358 1359 // Perform the body of the catch. 1360 EmitStmt(C->getHandlerBlock()); 1361 1362 // Fall out through the catch cleanups. 1363 CatchScope.ForceCleanup(); 1364 1365 // Branch out of the try. 1366 if (HaveInsertPoint()) 1367 Builder.CreateBr(ContBB); 1368 } 1369 1370 EmitBlock(ContBB); 1371} 1372 1373namespace { 1374 struct CallEndCatchForFinally : EHScopeStack::Cleanup { 1375 llvm::Value *ForEHVar; 1376 llvm::Value *EndCatchFn; 1377 CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn) 1378 : ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {} 1379 1380 void Emit(CodeGenFunction &CGF, bool IsForEH) { 1381 llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch"); 1382 llvm::BasicBlock *CleanupContBB = 1383 CGF.createBasicBlock("finally.cleanup.cont"); 1384 1385 llvm::Value *ShouldEndCatch = 1386 CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch"); 1387 CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB); 1388 CGF.EmitBlock(EndCatchBB); 1389 CGF.EmitCallOrInvoke(EndCatchFn, 0, 0); // catch-all, so might throw 1390 CGF.EmitBlock(CleanupContBB); 1391 } 1392 }; 1393 1394 struct PerformFinally : EHScopeStack::Cleanup { 1395 const Stmt *Body; 1396 llvm::Value *ForEHVar; 1397 llvm::Value *EndCatchFn; 1398 llvm::Value *RethrowFn; 1399 llvm::Value *SavedExnVar; 1400 1401 PerformFinally(const Stmt *Body, llvm::Value *ForEHVar, 1402 llvm::Value *EndCatchFn, 1403 llvm::Value *RethrowFn, llvm::Value *SavedExnVar) 1404 : Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn), 1405 RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {} 1406 1407 void Emit(CodeGenFunction &CGF, bool IsForEH) { 1408 // Enter a cleanup to call the end-catch function if one was provided. 1409 if (EndCatchFn) 1410 CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup, 1411 ForEHVar, EndCatchFn); 1412 1413 // Save the current cleanup destination in case there are 1414 // cleanups in the finally block. 1415 llvm::Value *SavedCleanupDest = 1416 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(), 1417 "cleanup.dest.saved"); 1418 1419 // Emit the finally block. 1420 CGF.EmitStmt(Body); 1421 1422 // If the end of the finally is reachable, check whether this was 1423 // for EH. If so, rethrow. 1424 if (CGF.HaveInsertPoint()) { 1425 llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow"); 1426 llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont"); 1427 1428 llvm::Value *ShouldRethrow = 1429 CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow"); 1430 CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB); 1431 1432 CGF.EmitBlock(RethrowBB); 1433 if (SavedExnVar) { 1434 llvm::Value *Args[] = { CGF.Builder.CreateLoad(SavedExnVar) }; 1435 CGF.EmitCallOrInvoke(RethrowFn, Args, Args+1); 1436 } else { 1437 CGF.EmitCallOrInvoke(RethrowFn, 0, 0); 1438 } 1439 CGF.Builder.CreateUnreachable(); 1440 1441 CGF.EmitBlock(ContBB); 1442 1443 // Restore the cleanup destination. 1444 CGF.Builder.CreateStore(SavedCleanupDest, 1445 CGF.getNormalCleanupDestSlot()); 1446 } 1447 1448 // Leave the end-catch cleanup. As an optimization, pretend that 1449 // the fallthrough path was inaccessible; we've dynamically proven 1450 // that we're not in the EH case along that path. 1451 if (EndCatchFn) { 1452 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); 1453 CGF.PopCleanupBlock(); 1454 CGF.Builder.restoreIP(SavedIP); 1455 } 1456 1457 // Now make sure we actually have an insertion point or the 1458 // cleanup gods will hate us. 1459 CGF.EnsureInsertPoint(); 1460 } 1461 }; 1462} 1463 1464/// Enters a finally block for an implementation using zero-cost 1465/// exceptions. This is mostly general, but hard-codes some 1466/// language/ABI-specific behavior in the catch-all sections. 1467CodeGenFunction::FinallyInfo 1468CodeGenFunction::EnterFinallyBlock(const Stmt *Body, 1469 llvm::Constant *BeginCatchFn, 1470 llvm::Constant *EndCatchFn, 1471 llvm::Constant *RethrowFn) { 1472 assert((BeginCatchFn != 0) == (EndCatchFn != 0) && 1473 "begin/end catch functions not paired"); 1474 assert(RethrowFn && "rethrow function is required"); 1475 1476 // The rethrow function has one of the following two types: 1477 // void (*)() 1478 // void (*)(void*) 1479 // In the latter case we need to pass it the exception object. 1480 // But we can't use the exception slot because the @finally might 1481 // have a landing pad (which would overwrite the exception slot). 1482 const llvm::FunctionType *RethrowFnTy = 1483 cast<llvm::FunctionType>( 1484 cast<llvm::PointerType>(RethrowFn->getType()) 1485 ->getElementType()); 1486 llvm::Value *SavedExnVar = 0; 1487 if (RethrowFnTy->getNumParams()) 1488 SavedExnVar = CreateTempAlloca(Builder.getInt8PtrTy(), "finally.exn"); 1489 1490 // A finally block is a statement which must be executed on any edge 1491 // out of a given scope. Unlike a cleanup, the finally block may 1492 // contain arbitrary control flow leading out of itself. In 1493 // addition, finally blocks should always be executed, even if there 1494 // are no catch handlers higher on the stack. Therefore, we 1495 // surround the protected scope with a combination of a normal 1496 // cleanup (to catch attempts to break out of the block via normal 1497 // control flow) and an EH catch-all (semantically "outside" any try 1498 // statement to which the finally block might have been attached). 1499 // The finally block itself is generated in the context of a cleanup 1500 // which conditionally leaves the catch-all. 1501 1502 FinallyInfo Info; 1503 1504 // Jump destination for performing the finally block on an exception 1505 // edge. We'll never actually reach this block, so unreachable is 1506 // fine. 1507 JumpDest RethrowDest = getJumpDestInCurrentScope(getUnreachableBlock()); 1508 1509 // Whether the finally block is being executed for EH purposes. 1510 llvm::AllocaInst *ForEHVar = CreateTempAlloca(CGF.Builder.getInt1Ty(), 1511 "finally.for-eh"); 1512 InitTempAlloca(ForEHVar, llvm::ConstantInt::getFalse(getLLVMContext())); 1513 1514 // Enter a normal cleanup which will perform the @finally block. 1515 EHStack.pushCleanup<PerformFinally>(NormalCleanup, Body, 1516 ForEHVar, EndCatchFn, 1517 RethrowFn, SavedExnVar); 1518 1519 // Enter a catch-all scope. 1520 llvm::BasicBlock *CatchAllBB = createBasicBlock("finally.catchall"); 1521 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); 1522 Builder.SetInsertPoint(CatchAllBB); 1523 1524 // If there's a begin-catch function, call it. 1525 if (BeginCatchFn) { 1526 Builder.CreateCall(BeginCatchFn, Builder.CreateLoad(getExceptionSlot())) 1527 ->setDoesNotThrow(); 1528 } 1529 1530 // If we need to remember the exception pointer to rethrow later, do so. 1531 if (SavedExnVar) { 1532 llvm::Value *SavedExn = Builder.CreateLoad(getExceptionSlot()); 1533 Builder.CreateStore(SavedExn, SavedExnVar); 1534 } 1535 1536 // Tell the finally block that we're in EH. 1537 Builder.CreateStore(llvm::ConstantInt::getTrue(getLLVMContext()), ForEHVar); 1538 1539 // Thread a jump through the finally cleanup. 1540 EmitBranchThroughCleanup(RethrowDest); 1541 1542 Builder.restoreIP(SavedIP); 1543 1544 EHCatchScope *CatchScope = EHStack.pushCatch(1); 1545 CatchScope->setCatchAllHandler(0, CatchAllBB); 1546 1547 return Info; 1548} 1549 1550void CodeGenFunction::ExitFinallyBlock(FinallyInfo &Info) { 1551 // Leave the finally catch-all. 1552 EHCatchScope &Catch = cast<EHCatchScope>(*EHStack.begin()); 1553 llvm::BasicBlock *CatchAllBB = Catch.getHandler(0).Block; 1554 EHStack.popCatch(); 1555 1556 // And leave the normal cleanup. 1557 PopCleanupBlock(); 1558 1559 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1560 EmitBlock(CatchAllBB, true); 1561 1562 Builder.restoreIP(SavedIP); 1563} 1564 1565llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() { 1566 if (TerminateLandingPad) 1567 return TerminateLandingPad; 1568 1569 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1570 1571 // This will get inserted at the end of the function. 1572 TerminateLandingPad = createBasicBlock("terminate.lpad"); 1573 Builder.SetInsertPoint(TerminateLandingPad); 1574 1575 // Tell the backend that this is a landing pad. 1576 llvm::CallInst *Exn = 1577 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_exception), "exn"); 1578 Exn->setDoesNotThrow(); 1579 1580 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions()); 1581 1582 // Tell the backend what the exception table should be: 1583 // nothing but a catch-all. 1584 llvm::Value *Args[3] = { Exn, getOpaquePersonalityFn(CGM, Personality), 1585 getCatchAllValue(*this) }; 1586 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::eh_selector), 1587 Args, Args+3, "eh.selector") 1588 ->setDoesNotThrow(); 1589 1590 llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this)); 1591 TerminateCall->setDoesNotReturn(); 1592 TerminateCall->setDoesNotThrow(); 1593 CGF.Builder.CreateUnreachable(); 1594 1595 // Restore the saved insertion state. 1596 Builder.restoreIP(SavedIP); 1597 1598 return TerminateLandingPad; 1599} 1600 1601llvm::BasicBlock *CodeGenFunction::getTerminateHandler() { 1602 if (TerminateHandler) 1603 return TerminateHandler; 1604 1605 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1606 1607 // Set up the terminate handler. This block is inserted at the very 1608 // end of the function by FinishFunction. 1609 TerminateHandler = createBasicBlock("terminate.handler"); 1610 Builder.SetInsertPoint(TerminateHandler); 1611 llvm::CallInst *TerminateCall = Builder.CreateCall(getTerminateFn(*this)); 1612 TerminateCall->setDoesNotReturn(); 1613 TerminateCall->setDoesNotThrow(); 1614 Builder.CreateUnreachable(); 1615 1616 // Restore the saved insertion state. 1617 Builder.restoreIP(SavedIP); 1618 1619 return TerminateHandler; 1620} 1621 1622CodeGenFunction::UnwindDest CodeGenFunction::getRethrowDest() { 1623 if (RethrowBlock.isValid()) return RethrowBlock; 1624 1625 CGBuilderTy::InsertPoint SavedIP = Builder.saveIP(); 1626 1627 // We emit a jump to a notional label at the outermost unwind state. 1628 llvm::BasicBlock *Unwind = createBasicBlock("eh.resume"); 1629 Builder.SetInsertPoint(Unwind); 1630 1631 const EHPersonality &Personality = EHPersonality::get(CGM.getLangOptions()); 1632 1633 // This can always be a call because we necessarily didn't find 1634 // anything on the EH stack which needs our help. 1635 llvm::StringRef RethrowName = Personality.getCatchallRethrowFnName(); 1636 llvm::Constant *RethrowFn; 1637 if (!RethrowName.empty()) 1638 RethrowFn = getCatchallRethrowFn(*this, RethrowName); 1639 else 1640 RethrowFn = getUnwindResumeOrRethrowFn(); 1641 1642 Builder.CreateCall(RethrowFn, Builder.CreateLoad(getExceptionSlot())) 1643 ->setDoesNotReturn(); 1644 Builder.CreateUnreachable(); 1645 1646 Builder.restoreIP(SavedIP); 1647 1648 RethrowBlock = UnwindDest(Unwind, EHStack.stable_end(), 0); 1649 return RethrowBlock; 1650} 1651 1652EHScopeStack::Cleanup::~Cleanup() { 1653 llvm_unreachable("Cleanup is indestructable"); 1654} 1655