1 //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code dealing with the IR generation for cleanups
11 // and related information.
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.
18 //===----------------------------------------------------------------------===//
20 #include "CodeGenFunction.h"
21 #include "CGCleanup.h"
23 using namespace clang;
24 using namespace CodeGen;
26 bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) {
28 return DominatingLLVMValue::needsSaving(rv.getScalarVal());
30 return DominatingLLVMValue::needsSaving(rv.getAggregateAddr());
34 DominatingValue<RValue>::saved_type
35 DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) {
37 llvm::Value *V = rv.getScalarVal();
39 // These automatically dominate and don't need to be saved.
40 if (!DominatingLLVMValue::needsSaving(V))
41 return saved_type(V, ScalarLiteral);
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);
50 CodeGenFunction::ComplexPairTy V = rv.getComplexVal();
51 llvm::Type *ComplexTy =
52 llvm::StructType::get(V.first->getType(), V.second->getType(),
54 llvm::Value *addr = CGF.CreateTempAlloca(ComplexTy, "saved-complex");
55 CGF.StoreComplexToAddr(V, addr, /*volatile*/ false);
56 return saved_type(addr, ComplexAddress);
59 assert(rv.isAggregate());
60 llvm::Value *V = rv.getAggregateAddr(); // TODO: volatile?
61 if (!DominatingLLVMValue::needsSaving(V))
62 return saved_type(V, AggregateLiteral);
64 llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
65 CGF.Builder.CreateStore(V, addr);
66 return saved_type(addr, AggregateAddress);
69 /// Given a saved r-value produced by SaveRValue, perform the code
70 /// necessary to restore it to usability at the current insertion
72 RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
75 return RValue::get(Value);
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));
83 return RValue::getComplex(CGF.LoadComplexFromAddr(Value, false));
86 llvm_unreachable("bad saved r-value kind");
90 /// Push an entry of the given size onto this protected-scope stack.
91 char *EHScopeStack::allocate(size_t Size) {
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);
101 unsigned NewCapacity = CurrentCapacity;
104 } while (NewCapacity < UsedCapacity + Size);
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;
116 assert(StartOfBuffer + Size <= StartOfData);
121 EHScopeStack::stable_iterator
122 EHScopeStack::getInnermostActiveNormalCleanup() const {
123 for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
125 EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
126 if (cleanup.isActive()) return si;
127 si = cleanup.getEnclosingNormalCleanup();
132 EHScopeStack::stable_iterator EHScopeStack::getInnermostActiveEHScope() const {
133 for (stable_iterator si = getInnermostEHScope(), se = stable_end();
135 // Skip over inactive cleanups.
136 EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*find(si));
137 if (cleanup && !cleanup->isActive()) {
138 si = cleanup->getEnclosingEHScope();
142 // All other scopes are always active.
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,
162 InnermostNormalCleanup,
165 InnermostNormalCleanup = stable_begin();
167 InnermostEHScope = stable_begin();
169 return Scope->getCleanupBuffer();
172 void EHScopeStack::popCleanup() {
173 assert(!empty() && "popping exception stack when not empty");
175 assert(isa<EHCleanupScope>(*begin()));
176 EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
177 InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
178 InnermostEHScope = Cleanup.getEnclosingEHScope();
179 StartOfData += Cleanup.getAllocatedSize();
181 // Destroy the cleanup.
182 Cleanup.~EHCleanupScope();
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
188 if (!hasNormalCleanups())
189 BranchFixups.clear();
191 // Otherwise we can still trim out unnecessary nulls.
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();
205 void EHScopeStack::popFilter() {
206 assert(!empty() && "popping exception stack when not empty");
208 EHFilterScope &filter = cast<EHFilterScope>(*begin());
209 StartOfData += EHFilterScope::getSizeForNumFilters(filter.getNumFilters());
211 InnermostEHScope = filter.getEnclosingEHScope();
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();
222 void EHScopeStack::pushTerminate() {
223 char *Buffer = allocate(EHTerminateScope::getSize());
224 new (Buffer) EHTerminateScope(InnermostEHScope);
225 InnermostEHScope = stable_begin();
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());
238 EHScopeStack::iterator it = find(InnermostNormalCleanup);
239 unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
240 assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
242 while (BranchFixups.size() > MinSize &&
243 BranchFixups.back().Destination == 0)
244 BranchFixups.pop_back();
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");
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());
257 // Initialize it to true at the current location.
258 Builder.CreateStore(Builder.getTrue(), active);
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);
265 if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup();
266 if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
269 void EHScopeStack::Cleanup::anchor() {}
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;
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;
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
288 // { std::string s; goto 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);
299 // Don't add this case to the switch statement twice.
300 if (!CasesAdded.insert(Fixup.Destination)) continue;
302 Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
306 CGF.EHStack.clearFixups();
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");
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();
327 return cast<llvm::SwitchInst>(Term);
331 void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
332 assert(Block && "resolving a null target block");
333 if (!EHStack.getNumBranchFixups()) return;
335 assert(EHStack.hasNormalCleanups() &&
336 "branch fixups exist with no normal cleanups on stack");
338 llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
339 bool ResolvedAny = false;
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;
346 Fixup.Destination = 0;
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;
355 // Don't process the same optimistic branch block twice.
356 if (!ModifiedOptimisticBlocks.insert(BranchBB))
359 llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
361 // Add a case to the switch.
362 Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
366 EHStack.popNullFixups();
369 /// Pops cleanup blocks until the given savepoint is reached.
370 void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) {
371 assert(Old.isValid());
373 while (EHStack.stable_begin() != Old) {
374 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
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());
382 PopCleanupBlock(FallThroughIsBranchThrough);
386 static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
387 EHCleanupScope &Scope) {
388 assert(Scope.isNormalCleanup());
389 llvm::BasicBlock *Entry = Scope.getNormalBlock();
391 Entry = CGF.createBasicBlock("cleanup");
392 Scope.setNormalBlock(Entry);
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.
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;
407 llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
408 if (!Br || Br->isConditional()) return Entry;
409 assert(Br->getSuccessor(0) == Entry);
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
414 bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
415 assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
418 Br->eraseFromParent();
420 // Replace all uses of the entry with the predecessor, in case there
421 // are phis in the cleanup.
422 Entry->replaceAllUsesWith(Pred);
425 Pred->getInstList().splice(Pred->end(), Entry->getInstList());
427 // Kill the entry block.
428 Entry->eraseFromParent();
431 CGF.Builder.SetInsertPoint(Pred);
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();
443 // If there's an active flag, load it and skip the cleanup if it's
445 llvm::BasicBlock *ContBB = 0;
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);
455 // Ask the cleanup to emit itself.
456 Fn->Emit(CGF, flags);
457 assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
459 // Emit the continuation block if there was an active flag.
461 CGF.EmitBlock(ContBB);
463 // Leave the terminate scope.
464 if (flags.isForEHCleanup()) CGF.EHStack.popTerminate();
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();
474 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
475 assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
476 Br->setSuccessor(0, To);
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);
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.
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();
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();
503 use.set(unreachableBB);
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);
511 // The switch operand is a load from the cleanup-dest alloca.
512 llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
514 // Destroy the switch.
515 si->eraseFromParent();
518 assert(condition->getOperand(0) == CGF.NormalCleanupDest);
519 assert(condition->use_empty());
520 condition->eraseFromParent();
524 assert(entry->use_empty());
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());
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;
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();
551 // Check the three conditions which might require a normal cleanup:
553 // - whether there are branch fix-ups through this cleanup
554 unsigned FixupDepth = Scope.getFixupDepth();
555 bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
557 // - whether there are branch-throughs or branch-afters
558 bool HasExistingBranches = Scope.hasBranches();
560 // - whether there's a fallthrough
561 llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
562 bool HasFallthrough = (FallthroughSource != 0 && IsActive);
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());
571 // If this is a normal cleanup, then having a prebranched
572 // fallthrough implies that the fallthrough source unconditionally
574 assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
575 (Scope.getNormalBlock() &&
576 FallthroughSource->getTerminator()->getSuccessor(0)
577 == Scope.getNormalBlock()));
579 bool RequiresNormalCleanup = false;
580 if (Scope.isNormalCleanup() &&
581 (HasFixups || HasExistingBranches || HasFallthrough)) {
582 RequiresNormalCleanup = true;
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;
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));
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.
602 prebranchDest = createBasicBlock("forwarded-prebranch");
603 EmitBlock(prebranchDest);
606 llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
607 assert(normalEntry && !normalEntry->use_empty());
609 ForwardPrebranchedFallthrough(FallthroughSource,
610 normalEntry, prebranchDest);
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());
622 // Copy the cleanup emission data out. Note that SmallVector
623 // guarantees maximal alignment for its buffer regardless of its
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());
633 EHScopeStack::Cleanup::Flags cleanupFlags;
634 if (Scope.isNormalCleanup())
635 cleanupFlags.setIsNormalCleanupKind();
636 if (Scope.isEHCleanup())
637 cleanupFlags.setIsEHCleanupKind();
639 if (!RequiresNormalCleanup) {
640 destroyOptimisticNormalEntry(*this, Scope);
641 EHStack.popCleanup();
643 // If we have a fallthrough and no other need for the cleanup,
645 if (HasFallthrough && !HasPrebranchedFallthrough &&
646 !HasFixups && !HasExistingBranches) {
648 destroyOptimisticNormalEntry(*this, Scope);
649 EHStack.popCleanup();
651 EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
653 // Otherwise, the best approach is to thread everything through
654 // the cleanup block and then try to clean up after ourselves.
656 // Force the entry block to exist.
657 llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
659 // I. Set up the fallthrough edge in.
661 CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
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());
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();
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);
681 // III. Figure out where we're going and build the cleanup
684 bool HasEnclosingCleanups =
685 (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
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));
701 llvm::BasicBlock *FallthroughDest = 0;
702 SmallVector<llvm::Instruction*, 2> InstsToAppend;
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);
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));
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)) {
723 llvm::BasicBlock *Default =
724 (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
726 // TODO: base this on the number of branch-afters and fixups
727 const unsigned SwitchCapacity = 10;
729 llvm::LoadInst *Load =
730 new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest");
731 llvm::SwitchInst *Switch =
732 llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
734 InstsToAppend.push_back(Load);
735 InstsToAppend.push_back(Switch);
737 // Branch-after fallthrough.
738 if (FallthroughSource && !FallthroughIsBranchThrough) {
739 FallthroughDest = createBasicBlock("cleanup.cont");
741 Switch->addCase(Builder.getInt32(0), FallthroughDest);
744 for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
745 Switch->addCase(Scope.getBranchAfterIndex(I),
746 Scope.getBranchAfterBlock(I));
749 // If there aren't any enclosing cleanups, we can resolve all
751 if (HasFixups && !HasEnclosingCleanups)
752 ResolveAllBranchFixups(*this, Switch, NormalEntry);
754 // We should always have a branch-through destination in this case.
755 assert(BranchThroughDest);
756 InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
759 // IV. Pop the cleanup and emit it.
760 EHStack.popCleanup();
761 assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
763 EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
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]);
770 // Optimistically hope that any fixups will continue falling through.
771 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
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);
781 Fixup.OptimisticBranchBlock = NormalExit;
784 // V. Set up the fallthrough edge out.
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.
793 Builder.restoreIP(savedInactiveFallthroughIP);
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
798 } else if (HasFallthrough && FallthroughDest) {
799 assert(!FallthroughIsBranchThrough);
800 EmitBlock(FallthroughDest);
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.
807 // Case 4: no fallthrough source exists.
809 Builder.ClearInsertionPoint();
812 // VI. Assorted cleaning.
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);
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();
824 EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
828 assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
830 // Emit the EH cleanup if required.
831 if (RequiresEHCleanup) {
832 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
836 cleanupFlags.setIsForEHCleanup();
837 EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
839 Builder.CreateBr(getEHDispatchBlock(EHParent));
841 Builder.restoreIP(SavedIP);
843 SimplifyCleanupEntry(*this, EHEntry);
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");
854 // Calculate the innermost active normal cleanup.
855 EHScopeStack::stable_iterator TopCleanup =
856 EHStack.getInnermostActiveNormalCleanup();
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
865 // Otherwise, we might need some cleanups.
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.
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");
879 if (!HaveInsertPoint())
882 // Create the branch.
883 llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
885 // Calculate the innermost active normal cleanup.
886 EHScopeStack::stable_iterator
887 TopCleanup = EHStack.getInnermostActiveNormalCleanup();
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();
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;
907 Builder.ClearInsertionPoint();
911 // Otherwise, thread through all the normal cleanups in scope.
913 // Store the index at the start.
914 llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
915 new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI);
917 // Adjust BI to point to the first cleanup block.
919 EHCleanupScope &Scope =
920 cast<EHCleanupScope>(*EHStack.find(TopCleanup));
921 BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
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)) {
929 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
930 assert(Scope.isNormalCleanup());
931 I = Scope.getEnclosingNormalCleanup();
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());
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()))
948 Builder.ClearInsertionPoint();
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())
957 // Check whether any enclosed cleanups were needed.
958 for (EHScopeStack::stable_iterator
959 I = EHStack.getInnermostNormalCleanup();
961 assert(C.strictlyEncloses(I));
962 EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
963 if (S.getNormalBlock()) return true;
964 I = S.getEnclosingNormalCleanup();
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())
976 // Check whether any enclosed cleanups were needed.
977 for (EHScopeStack::stable_iterator
978 i = EHStack.getInnermostEHScope(); i != cleanup; ) {
979 assert(cleanup.strictlyEncloses(i));
981 EHScope &scope = *EHStack.find(i);
982 if (scope.hasEHBranches())
985 i = scope.getEnclosingEHScope();
991 enum ForActivation_t {
996 /// The given cleanup block is changing activation state. Configure a
997 /// cleanup variable if necessary.
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));
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);
1011 // Calculate whether the cleanup was used:
1013 // - as a normal cleanup
1014 if (Scope.isNormalCleanup() && IsUsedAsNormalCleanup(CGF.EHStack, C)) {
1015 Scope.setTestFlagInNormalCleanup();
1019 // - as an EH cleanup
1020 if (Scope.isEHCleanup() && IsUsedAsEHCleanup(CGF.EHStack, C)) {
1021 Scope.setTestFlagInEHCleanup();
1025 // If it hasn't yet been used as either, we're done.
1026 if (!NeedFlag) return;
1028 llvm::AllocaInst *Var = Scope.getActiveFlag();
1030 Var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "cleanup.isactive");
1031 Scope.setActiveFlag(Var);
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));
1038 CGF.Builder.CreateStore(CGF.Builder.getInt1(Kind == ForActivation), Var);
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");
1047 SetupCleanupBlockActivation(*this, C, ForActivation);
1049 Scope.setActive(true);
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");
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();
1064 Builder.restoreIP(SavedIP);
1068 // Otherwise, follow the general case.
1069 SetupCleanupBlockActivation(*this, C, ForDeactivation);
1071 Scope.setActive(false);
1074 llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() {
1075 if (!NormalCleanupDest)
1077 CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
1078 return NormalCleanupDest;