1 //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===// 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 file contains code dealing with the IR generation for cleanups 11 // and related information. 12 // 13 // A "cleanup" is a piece of code which needs to be executed whenever 14 // control transfers out of a particular scope. This can be 15 // conditionalized to occur only on exceptional control flow, only on 16 // normal control flow, or both. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #include "CodeGenFunction.h" 21 #include "CGCleanup.h" 22 23 using namespace clang; 24 using namespace CodeGen; 25 26 bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) { 27 if (rv.isScalar()) 28 return DominatingLLVMValue::needsSaving(rv.getScalarVal()); 29 if (rv.isAggregate()) 30 return DominatingLLVMValue::needsSaving(rv.getAggregateAddr()); 31 return true; 32 } 33 34 DominatingValue<RValue>::saved_type 35 DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) { 36 if (rv.isScalar()) { 37 llvm::Value *V = rv.getScalarVal(); 38 39 // These automatically dominate and don't need to be saved. 40 if (!DominatingLLVMValue::needsSaving(V)) 41 return saved_type(V, ScalarLiteral); 42 43 // Everything else needs an alloca. 44 llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue"); 45 CGF.Builder.CreateStore(V, addr); 46 return saved_type(addr, ScalarAddress); 47 } 48 49 if (rv.isComplex()) { 50 CodeGenFunction::ComplexPairTy V = rv.getComplexVal(); 51 llvm::Type *ComplexTy = 52 llvm::StructType::get(V.first->getType(), V.second->getType(), 53 (void*) 0); 54 llvm::Value *addr = CGF.CreateTempAlloca(ComplexTy, "saved-complex"); 55 CGF.StoreComplexToAddr(V, addr, /*volatile*/ false); 56 return saved_type(addr, ComplexAddress); 57 } 58 59 assert(rv.isAggregate()); 60 llvm::Value *V = rv.getAggregateAddr(); // TODO: volatile? 61 if (!DominatingLLVMValue::needsSaving(V)) 62 return saved_type(V, AggregateLiteral); 63 64 llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue"); 65 CGF.Builder.CreateStore(V, addr); 66 return saved_type(addr, AggregateAddress); 67 } 68 69 /// Given a saved r-value produced by SaveRValue, perform the code 70 /// necessary to restore it to usability at the current insertion 71 /// point. 72 RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) { 73 switch (K) { 74 case ScalarLiteral: 75 return RValue::get(Value); 76 case ScalarAddress: 77 return RValue::get(CGF.Builder.CreateLoad(Value)); 78 case AggregateLiteral: 79 return RValue::getAggregate(Value); 80 case AggregateAddress: 81 return RValue::getAggregate(CGF.Builder.CreateLoad(Value)); 82 case ComplexAddress: 83 return RValue::getComplex(CGF.LoadComplexFromAddr(Value, false)); 84 } 85 86 llvm_unreachable("bad saved r-value kind"); 87 return RValue(); 88 } 89 90 /// Push an entry of the given size onto this protected-scope stack. 91 char *EHScopeStack::allocate(size_t Size) { 92 if (!StartOfBuffer) { 93 unsigned Capacity = 1024; 94 while (Capacity < Size) Capacity *= 2; 95 StartOfBuffer = new char[Capacity]; 96 StartOfData = EndOfBuffer = StartOfBuffer + Capacity; 97 } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) { 98 unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer; 99 unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer); 100 101 unsigned NewCapacity = CurrentCapacity; 102 do { 103 NewCapacity *= 2; 104 } while (NewCapacity < UsedCapacity + Size); 105 106 char *NewStartOfBuffer = new char[NewCapacity]; 107 char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity; 108 char *NewStartOfData = NewEndOfBuffer - UsedCapacity; 109 memcpy(NewStartOfData, StartOfData, UsedCapacity); 110 delete [] StartOfBuffer; 111 StartOfBuffer = NewStartOfBuffer; 112 EndOfBuffer = NewEndOfBuffer; 113 StartOfData = NewStartOfData; 114 } 115 116 assert(StartOfBuffer + Size <= StartOfData); 117 StartOfData -= Size; 118 return StartOfData; 119 } 120 121 EHScopeStack::stable_iterator 122 EHScopeStack::getInnermostActiveNormalCleanup() const { 123 for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end(); 124 si != se; ) { 125 EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si)); 126 if (cleanup.isActive()) return si; 127 si = cleanup.getEnclosingNormalCleanup(); 128 } 129 return stable_end(); 130 } 131 132 EHScopeStack::stable_iterator EHScopeStack::getInnermostActiveEHScope() const { 133 for (stable_iterator si = getInnermostEHScope(), se = stable_end(); 134 si != se; ) { 135 // Skip over inactive cleanups. 136 EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*find(si)); 137 if (cleanup && !cleanup->isActive()) { 138 si = cleanup->getEnclosingEHScope(); 139 continue; 140 } 141 142 // All other scopes are always active. 143 return si; 144 } 145 146 return stable_end(); 147 } 148 149 150 void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) { 151 assert(((Size % sizeof(void*)) == 0) && "cleanup type is misaligned"); 152 char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size)); 153 bool IsNormalCleanup = Kind & NormalCleanup; 154 bool IsEHCleanup = Kind & EHCleanup; 155 bool IsActive = !(Kind & InactiveCleanup); 156 EHCleanupScope *Scope = 157 new (Buffer) EHCleanupScope(IsNormalCleanup, 158 IsEHCleanup, 159 IsActive, 160 Size, 161 BranchFixups.size(), 162 InnermostNormalCleanup, 163 InnermostEHScope); 164 if (IsNormalCleanup) 165 InnermostNormalCleanup = stable_begin(); 166 if (IsEHCleanup) 167 InnermostEHScope = stable_begin(); 168 169 return Scope->getCleanupBuffer(); 170 } 171 172 void EHScopeStack::popCleanup() { 173 assert(!empty() && "popping exception stack when not empty"); 174 175 assert(isa<EHCleanupScope>(*begin())); 176 EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin()); 177 InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup(); 178 InnermostEHScope = Cleanup.getEnclosingEHScope(); 179 StartOfData += Cleanup.getAllocatedSize(); 180 181 // Destroy the cleanup. 182 Cleanup.~EHCleanupScope(); 183 184 // Check whether we can shrink the branch-fixups stack. 185 if (!BranchFixups.empty()) { 186 // If we no longer have any normal cleanups, all the fixups are 187 // complete. 188 if (!hasNormalCleanups()) 189 BranchFixups.clear(); 190 191 // Otherwise we can still trim out unnecessary nulls. 192 else 193 popNullFixups(); 194 } 195 } 196 197 EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) { 198 assert(getInnermostEHScope() == stable_end()); 199 char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters)); 200 EHFilterScope *filter = new (buffer) EHFilterScope(numFilters); 201 InnermostEHScope = stable_begin(); 202 return filter; 203 } 204 205 void EHScopeStack::popFilter() { 206 assert(!empty() && "popping exception stack when not empty"); 207 208 EHFilterScope &filter = cast<EHFilterScope>(*begin()); 209 StartOfData += EHFilterScope::getSizeForNumFilters(filter.getNumFilters()); 210 211 InnermostEHScope = filter.getEnclosingEHScope(); 212 } 213 214 EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) { 215 char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers)); 216 EHCatchScope *scope = 217 new (buffer) EHCatchScope(numHandlers, InnermostEHScope); 218 InnermostEHScope = stable_begin(); 219 return scope; 220 } 221 222 void EHScopeStack::pushTerminate() { 223 char *Buffer = allocate(EHTerminateScope::getSize()); 224 new (Buffer) EHTerminateScope(InnermostEHScope); 225 InnermostEHScope = stable_begin(); 226 } 227 228 /// Remove any 'null' fixups on the stack. However, we can't pop more 229 /// fixups than the fixup depth on the innermost normal cleanup, or 230 /// else fixups that we try to add to that cleanup will end up in the 231 /// wrong place. We *could* try to shrink fixup depths, but that's 232 /// actually a lot of work for little benefit. 233 void EHScopeStack::popNullFixups() { 234 // We expect this to only be called when there's still an innermost 235 // normal cleanup; otherwise there really shouldn't be any fixups. 236 assert(hasNormalCleanups()); 237 238 EHScopeStack::iterator it = find(InnermostNormalCleanup); 239 unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth(); 240 assert(BranchFixups.size() >= MinSize && "fixup stack out of order"); 241 242 while (BranchFixups.size() > MinSize && 243 BranchFixups.back().Destination == 0) 244 BranchFixups.pop_back(); 245 } 246 247 void CodeGenFunction::initFullExprCleanup() { 248 // Create a variable to decide whether the cleanup needs to be run. 249 llvm::AllocaInst *active 250 = CreateTempAlloca(Builder.getInt1Ty(), "cleanup.cond"); 251 252 // Initialize it to false at a site that's guaranteed to be run 253 // before each evaluation. 254 llvm::BasicBlock *block = OutermostConditional->getStartingBlock(); 255 new llvm::StoreInst(Builder.getFalse(), active, &block->back()); 256 257 // Initialize it to true at the current location. 258 Builder.CreateStore(Builder.getTrue(), active); 259 260 // Set that as the active flag in the cleanup. 261 EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin()); 262 assert(cleanup.getActiveFlag() == 0 && "cleanup already has active flag?"); 263 cleanup.setActiveFlag(active); 264 265 if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup(); 266 if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup(); 267 } 268 269 void EHScopeStack::Cleanup::anchor() {} 270 271 /// All the branch fixups on the EH stack have propagated out past the 272 /// outermost normal cleanup; resolve them all by adding cases to the 273 /// given switch instruction. 274 static void ResolveAllBranchFixups(CodeGenFunction &CGF, 275 llvm::SwitchInst *Switch, 276 llvm::BasicBlock *CleanupEntry) { 277 llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded; 278 279 for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) { 280 // Skip this fixup if its destination isn't set. 281 BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I); 282 if (Fixup.Destination == 0) continue; 283 284 // If there isn't an OptimisticBranchBlock, then InitialBranch is 285 // still pointing directly to its destination; forward it to the 286 // appropriate cleanup entry. This is required in the specific 287 // case of 288 // { std::string s; goto lbl; } 289 // lbl: 290 // i.e. where there's an unresolved fixup inside a single cleanup 291 // entry which we're currently popping. 292 if (Fixup.OptimisticBranchBlock == 0) { 293 new llvm::StoreInst(CGF.Builder.getInt32(Fixup.DestinationIndex), 294 CGF.getNormalCleanupDestSlot(), 295 Fixup.InitialBranch); 296 Fixup.InitialBranch->setSuccessor(0, CleanupEntry); 297 } 298 299 // Don't add this case to the switch statement twice. 300 if (!CasesAdded.insert(Fixup.Destination)) continue; 301 302 Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex), 303 Fixup.Destination); 304 } 305 306 CGF.EHStack.clearFixups(); 307 } 308 309 /// Transitions the terminator of the given exit-block of a cleanup to 310 /// be a cleanup switch. 311 static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF, 312 llvm::BasicBlock *Block) { 313 // If it's a branch, turn it into a switch whose default 314 // destination is its original target. 315 llvm::TerminatorInst *Term = Block->getTerminator(); 316 assert(Term && "can't transition block without terminator"); 317 318 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) { 319 assert(Br->isUnconditional()); 320 llvm::LoadInst *Load = 321 new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term); 322 llvm::SwitchInst *Switch = 323 llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block); 324 Br->eraseFromParent(); 325 return Switch; 326 } else { 327 return cast<llvm::SwitchInst>(Term); 328 } 329 } 330 331 void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) { 332 assert(Block && "resolving a null target block"); 333 if (!EHStack.getNumBranchFixups()) return; 334 335 assert(EHStack.hasNormalCleanups() && 336 "branch fixups exist with no normal cleanups on stack"); 337 338 llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks; 339 bool ResolvedAny = false; 340 341 for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) { 342 // Skip this fixup if its destination doesn't match. 343 BranchFixup &Fixup = EHStack.getBranchFixup(I); 344 if (Fixup.Destination != Block) continue; 345 346 Fixup.Destination = 0; 347 ResolvedAny = true; 348 349 // If it doesn't have an optimistic branch block, LatestBranch is 350 // already pointing to the right place. 351 llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock; 352 if (!BranchBB) 353 continue; 354 355 // Don't process the same optimistic branch block twice. 356 if (!ModifiedOptimisticBlocks.insert(BranchBB)) 357 continue; 358 359 llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB); 360 361 // Add a case to the switch. 362 Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block); 363 } 364 365 if (ResolvedAny) 366 EHStack.popNullFixups(); 367 } 368 369 /// Pops cleanup blocks until the given savepoint is reached. 370 void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) { 371 assert(Old.isValid()); 372 373 while (EHStack.stable_begin() != Old) { 374 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); 375 376 // As long as Old strictly encloses the scope's enclosing normal 377 // cleanup, we're going to emit another normal cleanup which 378 // fallthrough can propagate through. 379 bool FallThroughIsBranchThrough = 380 Old.strictlyEncloses(Scope.getEnclosingNormalCleanup()); 381 382 PopCleanupBlock(FallThroughIsBranchThrough); 383 } 384 } 385 386 static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF, 387 EHCleanupScope &Scope) { 388 assert(Scope.isNormalCleanup()); 389 llvm::BasicBlock *Entry = Scope.getNormalBlock(); 390 if (!Entry) { 391 Entry = CGF.createBasicBlock("cleanup"); 392 Scope.setNormalBlock(Entry); 393 } 394 return Entry; 395 } 396 397 /// Attempts to reduce a cleanup's entry block to a fallthrough. This 398 /// is basically llvm::MergeBlockIntoPredecessor, except 399 /// simplified/optimized for the tighter constraints on cleanup blocks. 400 /// 401 /// Returns the new block, whatever it is. 402 static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF, 403 llvm::BasicBlock *Entry) { 404 llvm::BasicBlock *Pred = Entry->getSinglePredecessor(); 405 if (!Pred) return Entry; 406 407 llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator()); 408 if (!Br || Br->isConditional()) return Entry; 409 assert(Br->getSuccessor(0) == Entry); 410 411 // If we were previously inserting at the end of the cleanup entry 412 // block, we'll need to continue inserting at the end of the 413 // predecessor. 414 bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry; 415 assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end()); 416 417 // Kill the branch. 418 Br->eraseFromParent(); 419 420 // Replace all uses of the entry with the predecessor, in case there 421 // are phis in the cleanup. 422 Entry->replaceAllUsesWith(Pred); 423 424 // Merge the blocks. 425 Pred->getInstList().splice(Pred->end(), Entry->getInstList()); 426 427 // Kill the entry block. 428 Entry->eraseFromParent(); 429 430 if (WasInsertBlock) 431 CGF.Builder.SetInsertPoint(Pred); 432 433 return Pred; 434 } 435 436 static void EmitCleanup(CodeGenFunction &CGF, 437 EHScopeStack::Cleanup *Fn, 438 EHScopeStack::Cleanup::Flags flags, 439 llvm::Value *ActiveFlag) { 440 // EH cleanups always occur within a terminate scope. 441 if (flags.isForEHCleanup()) CGF.EHStack.pushTerminate(); 442 443 // If there's an active flag, load it and skip the cleanup if it's 444 // false. 445 llvm::BasicBlock *ContBB = 0; 446 if (ActiveFlag) { 447 ContBB = CGF.createBasicBlock("cleanup.done"); 448 llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action"); 449 llvm::Value *IsActive 450 = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active"); 451 CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB); 452 CGF.EmitBlock(CleanupBB); 453 } 454 455 // Ask the cleanup to emit itself. 456 Fn->Emit(CGF, flags); 457 assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?"); 458 459 // Emit the continuation block if there was an active flag. 460 if (ActiveFlag) 461 CGF.EmitBlock(ContBB); 462 463 // Leave the terminate scope. 464 if (flags.isForEHCleanup()) CGF.EHStack.popTerminate(); 465 } 466 467 static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit, 468 llvm::BasicBlock *From, 469 llvm::BasicBlock *To) { 470 // Exit is the exit block of a cleanup, so it always terminates in 471 // an unconditional branch or a switch. 472 llvm::TerminatorInst *Term = Exit->getTerminator(); 473 474 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) { 475 assert(Br->isUnconditional() && Br->getSuccessor(0) == From); 476 Br->setSuccessor(0, To); 477 } else { 478 llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term); 479 for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I) 480 if (Switch->getSuccessor(I) == From) 481 Switch->setSuccessor(I, To); 482 } 483 } 484 485 /// We don't need a normal entry block for the given cleanup. 486 /// Optimistic fixup branches can cause these blocks to come into 487 /// existence anyway; if so, destroy it. 488 /// 489 /// The validity of this transformation is very much specific to the 490 /// exact ways in which we form branches to cleanup entries. 491 static void destroyOptimisticNormalEntry(CodeGenFunction &CGF, 492 EHCleanupScope &scope) { 493 llvm::BasicBlock *entry = scope.getNormalBlock(); 494 if (!entry) return; 495 496 // Replace all the uses with unreachable. 497 llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock(); 498 for (llvm::BasicBlock::use_iterator 499 i = entry->use_begin(), e = entry->use_end(); i != e; ) { 500 llvm::Use &use = i.getUse(); 501 ++i; 502 503 use.set(unreachableBB); 504 505 // The only uses should be fixup switches. 506 llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser()); 507 if (si->getNumCases() == 2 && si->getDefaultDest() == unreachableBB) { 508 // Replace the switch with a branch. 509 llvm::BranchInst::Create(si->getSuccessor(1), si); 510 511 // The switch operand is a load from the cleanup-dest alloca. 512 llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition()); 513 514 // Destroy the switch. 515 si->eraseFromParent(); 516 517 // Destroy the load. 518 assert(condition->getOperand(0) == CGF.NormalCleanupDest); 519 assert(condition->use_empty()); 520 condition->eraseFromParent(); 521 } 522 } 523 524 assert(entry->use_empty()); 525 delete entry; 526 } 527 528 /// Pops a cleanup block. If the block includes a normal cleanup, the 529 /// current insertion point is threaded through the cleanup, as are 530 /// any branch fixups on the cleanup. 531 void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) { 532 assert(!EHStack.empty() && "cleanup stack is empty!"); 533 assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!"); 534 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); 535 assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups()); 536 537 // Remember activation information. 538 bool IsActive = Scope.isActive(); 539 llvm::Value *NormalActiveFlag = 540 Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag() : 0; 541 llvm::Value *EHActiveFlag = 542 Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag() : 0; 543 544 // Check whether we need an EH cleanup. This is only true if we've 545 // generated a lazy EH cleanup block. 546 llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock(); 547 assert(Scope.hasEHBranches() == (EHEntry != 0)); 548 bool RequiresEHCleanup = (EHEntry != 0); 549 EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope(); 550 551 // Check the three conditions which might require a normal cleanup: 552 553 // - whether there are branch fix-ups through this cleanup 554 unsigned FixupDepth = Scope.getFixupDepth(); 555 bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth; 556 557 // - whether there are branch-throughs or branch-afters 558 bool HasExistingBranches = Scope.hasBranches(); 559 560 // - whether there's a fallthrough 561 llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock(); 562 bool HasFallthrough = (FallthroughSource != 0 && IsActive); 563 564 // Branch-through fall-throughs leave the insertion point set to the 565 // end of the last cleanup, which points to the current scope. The 566 // rest of IR gen doesn't need to worry about this; it only happens 567 // during the execution of PopCleanupBlocks(). 568 bool HasPrebranchedFallthrough = 569 (FallthroughSource && FallthroughSource->getTerminator()); 570 571 // If this is a normal cleanup, then having a prebranched 572 // fallthrough implies that the fallthrough source unconditionally 573 // jumps here. 574 assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough || 575 (Scope.getNormalBlock() && 576 FallthroughSource->getTerminator()->getSuccessor(0) 577 == Scope.getNormalBlock())); 578 579 bool RequiresNormalCleanup = false; 580 if (Scope.isNormalCleanup() && 581 (HasFixups || HasExistingBranches || HasFallthrough)) { 582 RequiresNormalCleanup = true; 583 } 584 585 // If we have a prebranched fallthrough into an inactive normal 586 // cleanup, rewrite it so that it leads to the appropriate place. 587 if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) { 588 llvm::BasicBlock *prebranchDest; 589 590 // If the prebranch is semantically branching through the next 591 // cleanup, just forward it to the next block, leaving the 592 // insertion point in the prebranched block. 593 if (FallthroughIsBranchThrough) { 594 EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup()); 595 prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing)); 596 597 // Otherwise, we need to make a new block. If the normal cleanup 598 // isn't being used at all, we could actually reuse the normal 599 // entry block, but this is simpler, and it avoids conflicts with 600 // dead optimistic fixup branches. 601 } else { 602 prebranchDest = createBasicBlock("forwarded-prebranch"); 603 EmitBlock(prebranchDest); 604 } 605 606 llvm::BasicBlock *normalEntry = Scope.getNormalBlock(); 607 assert(normalEntry && !normalEntry->use_empty()); 608 609 ForwardPrebranchedFallthrough(FallthroughSource, 610 normalEntry, prebranchDest); 611 } 612 613 // If we don't need the cleanup at all, we're done. 614 if (!RequiresNormalCleanup && !RequiresEHCleanup) { 615 destroyOptimisticNormalEntry(*this, Scope); 616 EHStack.popCleanup(); // safe because there are no fixups 617 assert(EHStack.getNumBranchFixups() == 0 || 618 EHStack.hasNormalCleanups()); 619 return; 620 } 621 622 // Copy the cleanup emission data out. Note that SmallVector 623 // guarantees maximal alignment for its buffer regardless of its 624 // type parameter. 625 SmallVector<char, 8*sizeof(void*)> CleanupBuffer; 626 CleanupBuffer.reserve(Scope.getCleanupSize()); 627 memcpy(CleanupBuffer.data(), 628 Scope.getCleanupBuffer(), Scope.getCleanupSize()); 629 CleanupBuffer.set_size(Scope.getCleanupSize()); 630 EHScopeStack::Cleanup *Fn = 631 reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data()); 632 633 EHScopeStack::Cleanup::Flags cleanupFlags; 634 if (Scope.isNormalCleanup()) 635 cleanupFlags.setIsNormalCleanupKind(); 636 if (Scope.isEHCleanup()) 637 cleanupFlags.setIsEHCleanupKind(); 638 639 if (!RequiresNormalCleanup) { 640 destroyOptimisticNormalEntry(*this, Scope); 641 EHStack.popCleanup(); 642 } else { 643 // If we have a fallthrough and no other need for the cleanup, 644 // emit it directly. 645 if (HasFallthrough && !HasPrebranchedFallthrough && 646 !HasFixups && !HasExistingBranches) { 647 648 destroyOptimisticNormalEntry(*this, Scope); 649 EHStack.popCleanup(); 650 651 EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag); 652 653 // Otherwise, the best approach is to thread everything through 654 // the cleanup block and then try to clean up after ourselves. 655 } else { 656 // Force the entry block to exist. 657 llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope); 658 659 // I. Set up the fallthrough edge in. 660 661 CGBuilderTy::InsertPoint savedInactiveFallthroughIP; 662 663 // If there's a fallthrough, we need to store the cleanup 664 // destination index. For fall-throughs this is always zero. 665 if (HasFallthrough) { 666 if (!HasPrebranchedFallthrough) 667 Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot()); 668 669 // Otherwise, save and clear the IP if we don't have fallthrough 670 // because the cleanup is inactive. 671 } else if (FallthroughSource) { 672 assert(!IsActive && "source without fallthrough for active cleanup"); 673 savedInactiveFallthroughIP = Builder.saveAndClearIP(); 674 } 675 676 // II. Emit the entry block. This implicitly branches to it if 677 // we have fallthrough. All the fixups and existing branches 678 // should already be branched to it. 679 EmitBlock(NormalEntry); 680 681 // III. Figure out where we're going and build the cleanup 682 // epilogue. 683 684 bool HasEnclosingCleanups = 685 (Scope.getEnclosingNormalCleanup() != EHStack.stable_end()); 686 687 // Compute the branch-through dest if we need it: 688 // - if there are branch-throughs threaded through the scope 689 // - if fall-through is a branch-through 690 // - if there are fixups that will be optimistically forwarded 691 // to the enclosing cleanup 692 llvm::BasicBlock *BranchThroughDest = 0; 693 if (Scope.hasBranchThroughs() || 694 (FallthroughSource && FallthroughIsBranchThrough) || 695 (HasFixups && HasEnclosingCleanups)) { 696 assert(HasEnclosingCleanups); 697 EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup()); 698 BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S)); 699 } 700 701 llvm::BasicBlock *FallthroughDest = 0; 702 SmallVector<llvm::Instruction*, 2> InstsToAppend; 703 704 // If there's exactly one branch-after and no other threads, 705 // we can route it without a switch. 706 if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough && 707 Scope.getNumBranchAfters() == 1) { 708 assert(!BranchThroughDest || !IsActive); 709 710 // TODO: clean up the possibly dead stores to the cleanup dest slot. 711 llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0); 712 InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter)); 713 714 // Build a switch-out if we need it: 715 // - if there are branch-afters threaded through the scope 716 // - if fall-through is a branch-after 717 // - if there are fixups that have nowhere left to go and 718 // so must be immediately resolved 719 } else if (Scope.getNumBranchAfters() || 720 (HasFallthrough && !FallthroughIsBranchThrough) || 721 (HasFixups && !HasEnclosingCleanups)) { 722 723 llvm::BasicBlock *Default = 724 (BranchThroughDest ? BranchThroughDest : getUnreachableBlock()); 725 726 // TODO: base this on the number of branch-afters and fixups 727 const unsigned SwitchCapacity = 10; 728 729 llvm::LoadInst *Load = 730 new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest"); 731 llvm::SwitchInst *Switch = 732 llvm::SwitchInst::Create(Load, Default, SwitchCapacity); 733 734 InstsToAppend.push_back(Load); 735 InstsToAppend.push_back(Switch); 736 737 // Branch-after fallthrough. 738 if (FallthroughSource && !FallthroughIsBranchThrough) { 739 FallthroughDest = createBasicBlock("cleanup.cont"); 740 if (HasFallthrough) 741 Switch->addCase(Builder.getInt32(0), FallthroughDest); 742 } 743 744 for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) { 745 Switch->addCase(Scope.getBranchAfterIndex(I), 746 Scope.getBranchAfterBlock(I)); 747 } 748 749 // If there aren't any enclosing cleanups, we can resolve all 750 // the fixups now. 751 if (HasFixups && !HasEnclosingCleanups) 752 ResolveAllBranchFixups(*this, Switch, NormalEntry); 753 } else { 754 // We should always have a branch-through destination in this case. 755 assert(BranchThroughDest); 756 InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest)); 757 } 758 759 // IV. Pop the cleanup and emit it. 760 EHStack.popCleanup(); 761 assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups); 762 763 EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag); 764 765 // Append the prepared cleanup prologue from above. 766 llvm::BasicBlock *NormalExit = Builder.GetInsertBlock(); 767 for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I) 768 NormalExit->getInstList().push_back(InstsToAppend[I]); 769 770 // Optimistically hope that any fixups will continue falling through. 771 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); 772 I < E; ++I) { 773 BranchFixup &Fixup = EHStack.getBranchFixup(I); 774 if (!Fixup.Destination) continue; 775 if (!Fixup.OptimisticBranchBlock) { 776 new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex), 777 getNormalCleanupDestSlot(), 778 Fixup.InitialBranch); 779 Fixup.InitialBranch->setSuccessor(0, NormalEntry); 780 } 781 Fixup.OptimisticBranchBlock = NormalExit; 782 } 783 784 // V. Set up the fallthrough edge out. 785 786 // Case 1: a fallthrough source exists but doesn't branch to the 787 // cleanup because the cleanup is inactive. 788 if (!HasFallthrough && FallthroughSource) { 789 // Prebranched fallthrough was forwarded earlier. 790 // Non-prebranched fallthrough doesn't need to be forwarded. 791 // Either way, all we need to do is restore the IP we cleared before. 792 assert(!IsActive); 793 Builder.restoreIP(savedInactiveFallthroughIP); 794 795 // Case 2: a fallthrough source exists and should branch to the 796 // cleanup, but we're not supposed to branch through to the next 797 // cleanup. 798 } else if (HasFallthrough && FallthroughDest) { 799 assert(!FallthroughIsBranchThrough); 800 EmitBlock(FallthroughDest); 801 802 // Case 3: a fallthrough source exists and should branch to the 803 // cleanup and then through to the next. 804 } else if (HasFallthrough) { 805 // Everything is already set up for this. 806 807 // Case 4: no fallthrough source exists. 808 } else { 809 Builder.ClearInsertionPoint(); 810 } 811 812 // VI. Assorted cleaning. 813 814 // Check whether we can merge NormalEntry into a single predecessor. 815 // This might invalidate (non-IR) pointers to NormalEntry. 816 llvm::BasicBlock *NewNormalEntry = 817 SimplifyCleanupEntry(*this, NormalEntry); 818 819 // If it did invalidate those pointers, and NormalEntry was the same 820 // as NormalExit, go back and patch up the fixups. 821 if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit) 822 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); 823 I < E; ++I) 824 EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry; 825 } 826 } 827 828 assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0); 829 830 // Emit the EH cleanup if required. 831 if (RequiresEHCleanup) { 832 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 833 834 EmitBlock(EHEntry); 835 836 cleanupFlags.setIsForEHCleanup(); 837 EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag); 838 839 Builder.CreateBr(getEHDispatchBlock(EHParent)); 840 841 Builder.restoreIP(SavedIP); 842 843 SimplifyCleanupEntry(*this, EHEntry); 844 } 845 } 846 847 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the 848 /// specified destination obviously has no cleanups to run. 'false' is always 849 /// a conservatively correct answer for this method. 850 bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const { 851 assert(Dest.getScopeDepth().encloses(EHStack.stable_begin()) 852 && "stale jump destination"); 853 854 // Calculate the innermost active normal cleanup. 855 EHScopeStack::stable_iterator TopCleanup = 856 EHStack.getInnermostActiveNormalCleanup(); 857 858 // If we're not in an active normal cleanup scope, or if the 859 // destination scope is within the innermost active normal cleanup 860 // scope, we don't need to worry about fixups. 861 if (TopCleanup == EHStack.stable_end() || 862 TopCleanup.encloses(Dest.getScopeDepth())) // works for invalid 863 return true; 864 865 // Otherwise, we might need some cleanups. 866 return false; 867 } 868 869 870 /// Terminate the current block by emitting a branch which might leave 871 /// the current cleanup-protected scope. The target scope may not yet 872 /// be known, in which case this will require a fixup. 873 /// 874 /// As a side-effect, this method clears the insertion point. 875 void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) { 876 assert(Dest.getScopeDepth().encloses(EHStack.stable_begin()) 877 && "stale jump destination"); 878 879 if (!HaveInsertPoint()) 880 return; 881 882 // Create the branch. 883 llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock()); 884 885 // Calculate the innermost active normal cleanup. 886 EHScopeStack::stable_iterator 887 TopCleanup = EHStack.getInnermostActiveNormalCleanup(); 888 889 // If we're not in an active normal cleanup scope, or if the 890 // destination scope is within the innermost active normal cleanup 891 // scope, we don't need to worry about fixups. 892 if (TopCleanup == EHStack.stable_end() || 893 TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid 894 Builder.ClearInsertionPoint(); 895 return; 896 } 897 898 // If we can't resolve the destination cleanup scope, just add this 899 // to the current cleanup scope as a branch fixup. 900 if (!Dest.getScopeDepth().isValid()) { 901 BranchFixup &Fixup = EHStack.addBranchFixup(); 902 Fixup.Destination = Dest.getBlock(); 903 Fixup.DestinationIndex = Dest.getDestIndex(); 904 Fixup.InitialBranch = BI; 905 Fixup.OptimisticBranchBlock = 0; 906 907 Builder.ClearInsertionPoint(); 908 return; 909 } 910 911 // Otherwise, thread through all the normal cleanups in scope. 912 913 // Store the index at the start. 914 llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex()); 915 new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI); 916 917 // Adjust BI to point to the first cleanup block. 918 { 919 EHCleanupScope &Scope = 920 cast<EHCleanupScope>(*EHStack.find(TopCleanup)); 921 BI->setSuccessor(0, CreateNormalEntry(*this, Scope)); 922 } 923 924 // Add this destination to all the scopes involved. 925 EHScopeStack::stable_iterator I = TopCleanup; 926 EHScopeStack::stable_iterator E = Dest.getScopeDepth(); 927 if (E.strictlyEncloses(I)) { 928 while (true) { 929 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I)); 930 assert(Scope.isNormalCleanup()); 931 I = Scope.getEnclosingNormalCleanup(); 932 933 // If this is the last cleanup we're propagating through, tell it 934 // that there's a resolved jump moving through it. 935 if (!E.strictlyEncloses(I)) { 936 Scope.addBranchAfter(Index, Dest.getBlock()); 937 break; 938 } 939 940 // Otherwise, tell the scope that there's a jump propoagating 941 // through it. If this isn't new information, all the rest of 942 // the work has been done before. 943 if (!Scope.addBranchThrough(Dest.getBlock())) 944 break; 945 } 946 } 947 948 Builder.ClearInsertionPoint(); 949 } 950 951 static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack, 952 EHScopeStack::stable_iterator C) { 953 // If we needed a normal block for any reason, that counts. 954 if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock()) 955 return true; 956 957 // Check whether any enclosed cleanups were needed. 958 for (EHScopeStack::stable_iterator 959 I = EHStack.getInnermostNormalCleanup(); 960 I != C; ) { 961 assert(C.strictlyEncloses(I)); 962 EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I)); 963 if (S.getNormalBlock()) return true; 964 I = S.getEnclosingNormalCleanup(); 965 } 966 967 return false; 968 } 969 970 static bool IsUsedAsEHCleanup(EHScopeStack &EHStack, 971 EHScopeStack::stable_iterator cleanup) { 972 // If we needed an EH block for any reason, that counts. 973 if (EHStack.find(cleanup)->hasEHBranches()) 974 return true; 975 976 // Check whether any enclosed cleanups were needed. 977 for (EHScopeStack::stable_iterator 978 i = EHStack.getInnermostEHScope(); i != cleanup; ) { 979 assert(cleanup.strictlyEncloses(i)); 980 981 EHScope &scope = *EHStack.find(i); 982 if (scope.hasEHBranches()) 983 return true; 984 985 i = scope.getEnclosingEHScope(); 986 } 987 988 return false; 989 } 990 991 enum ForActivation_t { 992 ForActivation, 993 ForDeactivation 994 }; 995 996 /// The given cleanup block is changing activation state. Configure a 997 /// cleanup variable if necessary. 998 /// 999 /// It would be good if we had some way of determining if there were 1000 /// extra uses *after* the change-over point. 1001 static void SetupCleanupBlockActivation(CodeGenFunction &CGF, 1002 EHScopeStack::stable_iterator C, 1003 ForActivation_t Kind) { 1004 EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C)); 1005 1006 // We always need the flag if we're activating the cleanup, because 1007 // we have to assume that the current location doesn't necessarily 1008 // dominate all future uses of the cleanup. 1009 bool NeedFlag = (Kind == ForActivation); 1010 1011 // Calculate whether the cleanup was used: 1012 1013 // - as a normal cleanup 1014 if (Scope.isNormalCleanup() && IsUsedAsNormalCleanup(CGF.EHStack, C)) { 1015 Scope.setTestFlagInNormalCleanup(); 1016 NeedFlag = true; 1017 } 1018 1019 // - as an EH cleanup 1020 if (Scope.isEHCleanup() && IsUsedAsEHCleanup(CGF.EHStack, C)) { 1021 Scope.setTestFlagInEHCleanup(); 1022 NeedFlag = true; 1023 } 1024 1025 // If it hasn't yet been used as either, we're done. 1026 if (!NeedFlag) return; 1027 1028 llvm::AllocaInst *Var = Scope.getActiveFlag(); 1029 if (!Var) { 1030 Var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "cleanup.isactive"); 1031 Scope.setActiveFlag(Var); 1032 1033 // Initialize to true or false depending on whether it was 1034 // active up to this point. 1035 CGF.InitTempAlloca(Var, CGF.Builder.getInt1(Kind == ForDeactivation)); 1036 } 1037 1038 CGF.Builder.CreateStore(CGF.Builder.getInt1(Kind == ForActivation), Var); 1039 } 1040 1041 /// Activate a cleanup that was created in an inactivated state. 1042 void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C) { 1043 assert(C != EHStack.stable_end() && "activating bottom of stack?"); 1044 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C)); 1045 assert(!Scope.isActive() && "double activation"); 1046 1047 SetupCleanupBlockActivation(*this, C, ForActivation); 1048 1049 Scope.setActive(true); 1050 } 1051 1052 /// Deactive a cleanup that was created in an active state. 1053 void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C) { 1054 assert(C != EHStack.stable_end() && "deactivating bottom of stack?"); 1055 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C)); 1056 assert(Scope.isActive() && "double deactivation"); 1057 1058 // If it's the top of the stack, just pop it. 1059 if (C == EHStack.stable_begin()) { 1060 // If it's a normal cleanup, we need to pretend that the 1061 // fallthrough is unreachable. 1062 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); 1063 PopCleanupBlock(); 1064 Builder.restoreIP(SavedIP); 1065 return; 1066 } 1067 1068 // Otherwise, follow the general case. 1069 SetupCleanupBlockActivation(*this, C, ForDeactivation); 1070 1071 Scope.setActive(false); 1072 } 1073 1074 llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() { 1075 if (!NormalCleanupDest) 1076 NormalCleanupDest = 1077 CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot"); 1078 return NormalCleanupDest; 1079 } 1080