1 //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains code dealing with the IR generation for cleanups
10 // and related information.
12 // A "cleanup" is a piece of code which needs to be executed whenever
13 // control transfers out of a particular scope. This can be
14 // conditionalized to occur only on exceptional control flow, only on
15 // normal control flow, or both.
17 //===----------------------------------------------------------------------===//
19 #include "CGCleanup.h"
20 #include "CodeGenFunction.h"
21 #include "llvm/Support/SaveAndRestore.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.getAggregatePointer());
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.
45 CGF.CreateDefaultAlignTempAlloca(V->getType(), "saved-rvalue");
46 CGF.Builder.CreateStore(V, addr);
47 return saved_type(addr.getPointer(), ScalarAddress);
51 CodeGenFunction::ComplexPairTy V = rv.getComplexVal();
52 llvm::Type *ComplexTy =
53 llvm::StructType::get(V.first->getType(), V.second->getType());
54 Address addr = CGF.CreateDefaultAlignTempAlloca(ComplexTy, "saved-complex");
55 CGF.Builder.CreateStore(V.first, CGF.Builder.CreateStructGEP(addr, 0));
56 CGF.Builder.CreateStore(V.second, CGF.Builder.CreateStructGEP(addr, 1));
57 return saved_type(addr.getPointer(), ComplexAddress);
60 assert(rv.isAggregate());
61 Address V = rv.getAggregateAddress(); // TODO: volatile?
62 if (!DominatingLLVMValue::needsSaving(V.getPointer()))
63 return saved_type(V.getPointer(), AggregateLiteral,
64 V.getAlignment().getQuantity());
67 CGF.CreateTempAlloca(V.getType(), CGF.getPointerAlign(), "saved-rvalue");
68 CGF.Builder.CreateStore(V.getPointer(), addr);
69 return saved_type(addr.getPointer(), AggregateAddress,
70 V.getAlignment().getQuantity());
73 /// Given a saved r-value produced by SaveRValue, perform the code
74 /// necessary to restore it to usability at the current insertion
76 RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
77 auto getSavingAddress = [&](llvm::Value *value) {
78 auto alignment = cast<llvm::AllocaInst>(value)->getAlignment();
79 return Address(value, CharUnits::fromQuantity(alignment));
83 return RValue::get(Value);
85 return RValue::get(CGF.Builder.CreateLoad(getSavingAddress(Value)));
86 case AggregateLiteral:
87 return RValue::getAggregate(Address(Value, CharUnits::fromQuantity(Align)));
88 case AggregateAddress: {
89 auto addr = CGF.Builder.CreateLoad(getSavingAddress(Value));
90 return RValue::getAggregate(Address(addr, CharUnits::fromQuantity(Align)));
92 case ComplexAddress: {
93 Address address = getSavingAddress(Value);
95 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 0));
97 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 1));
98 return RValue::getComplex(real, imag);
102 llvm_unreachable("bad saved r-value kind");
105 /// Push an entry of the given size onto this protected-scope stack.
106 char *EHScopeStack::allocate(size_t Size) {
107 Size = llvm::alignTo(Size, ScopeStackAlignment);
108 if (!StartOfBuffer) {
109 unsigned Capacity = 1024;
110 while (Capacity < Size) Capacity *= 2;
111 StartOfBuffer = new char[Capacity];
112 StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
113 } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) {
114 unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
115 unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
117 unsigned NewCapacity = CurrentCapacity;
120 } while (NewCapacity < UsedCapacity + Size);
122 char *NewStartOfBuffer = new char[NewCapacity];
123 char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
124 char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
125 memcpy(NewStartOfData, StartOfData, UsedCapacity);
126 delete [] StartOfBuffer;
127 StartOfBuffer = NewStartOfBuffer;
128 EndOfBuffer = NewEndOfBuffer;
129 StartOfData = NewStartOfData;
132 assert(StartOfBuffer + Size <= StartOfData);
137 void EHScopeStack::deallocate(size_t Size) {
138 StartOfData += llvm::alignTo(Size, ScopeStackAlignment);
141 bool EHScopeStack::containsOnlyLifetimeMarkers(
142 EHScopeStack::stable_iterator Old) const {
143 for (EHScopeStack::iterator it = begin(); stabilize(it) != Old; it++) {
144 EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*it);
145 if (!cleanup || !cleanup->isLifetimeMarker())
152 bool EHScopeStack::requiresLandingPad() const {
153 for (stable_iterator si = getInnermostEHScope(); si != stable_end(); ) {
154 // Skip lifetime markers.
155 if (auto *cleanup = dyn_cast<EHCleanupScope>(&*find(si)))
156 if (cleanup->isLifetimeMarker()) {
157 si = cleanup->getEnclosingEHScope();
166 EHScopeStack::stable_iterator
167 EHScopeStack::getInnermostActiveNormalCleanup() const {
168 for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
170 EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
171 if (cleanup.isActive()) return si;
172 si = cleanup.getEnclosingNormalCleanup();
178 void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
179 char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
180 bool IsNormalCleanup = Kind & NormalCleanup;
181 bool IsEHCleanup = Kind & EHCleanup;
182 bool IsLifetimeMarker = Kind & LifetimeMarker;
183 EHCleanupScope *Scope =
184 new (Buffer) EHCleanupScope(IsNormalCleanup,
188 InnermostNormalCleanup,
191 InnermostNormalCleanup = stable_begin();
193 InnermostEHScope = stable_begin();
194 if (IsLifetimeMarker)
195 Scope->setLifetimeMarker();
197 return Scope->getCleanupBuffer();
200 void EHScopeStack::popCleanup() {
201 assert(!empty() && "popping exception stack when not empty");
203 assert(isa<EHCleanupScope>(*begin()));
204 EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
205 InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
206 InnermostEHScope = Cleanup.getEnclosingEHScope();
207 deallocate(Cleanup.getAllocatedSize());
209 // Destroy the cleanup.
212 // Check whether we can shrink the branch-fixups stack.
213 if (!BranchFixups.empty()) {
214 // If we no longer have any normal cleanups, all the fixups are
216 if (!hasNormalCleanups())
217 BranchFixups.clear();
219 // Otherwise we can still trim out unnecessary nulls.
225 EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) {
226 assert(getInnermostEHScope() == stable_end());
227 char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters));
228 EHFilterScope *filter = new (buffer) EHFilterScope(numFilters);
229 InnermostEHScope = stable_begin();
233 void EHScopeStack::popFilter() {
234 assert(!empty() && "popping exception stack when not empty");
236 EHFilterScope &filter = cast<EHFilterScope>(*begin());
237 deallocate(EHFilterScope::getSizeForNumFilters(filter.getNumFilters()));
239 InnermostEHScope = filter.getEnclosingEHScope();
242 EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) {
243 char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers));
244 EHCatchScope *scope =
245 new (buffer) EHCatchScope(numHandlers, InnermostEHScope);
246 InnermostEHScope = stable_begin();
250 void EHScopeStack::pushTerminate() {
251 char *Buffer = allocate(EHTerminateScope::getSize());
252 new (Buffer) EHTerminateScope(InnermostEHScope);
253 InnermostEHScope = stable_begin();
256 /// Remove any 'null' fixups on the stack. However, we can't pop more
257 /// fixups than the fixup depth on the innermost normal cleanup, or
258 /// else fixups that we try to add to that cleanup will end up in the
259 /// wrong place. We *could* try to shrink fixup depths, but that's
260 /// actually a lot of work for little benefit.
261 void EHScopeStack::popNullFixups() {
262 // We expect this to only be called when there's still an innermost
263 // normal cleanup; otherwise there really shouldn't be any fixups.
264 assert(hasNormalCleanups());
266 EHScopeStack::iterator it = find(InnermostNormalCleanup);
267 unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
268 assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
270 while (BranchFixups.size() > MinSize &&
271 BranchFixups.back().Destination == nullptr)
272 BranchFixups.pop_back();
275 Address CodeGenFunction::createCleanupActiveFlag() {
276 // Create a variable to decide whether the cleanup needs to be run.
277 Address active = CreateTempAllocaWithoutCast(
278 Builder.getInt1Ty(), CharUnits::One(), "cleanup.cond");
280 // Initialize it to false at a site that's guaranteed to be run
281 // before each evaluation.
282 setBeforeOutermostConditional(Builder.getFalse(), active);
284 // Initialize it to true at the current location.
285 Builder.CreateStore(Builder.getTrue(), active);
290 void CodeGenFunction::initFullExprCleanupWithFlag(Address ActiveFlag) {
291 // Set that as the active flag in the cleanup.
292 EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
293 assert(!cleanup.hasActiveFlag() && "cleanup already has active flag?");
294 cleanup.setActiveFlag(ActiveFlag);
296 if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup();
297 if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
300 void EHScopeStack::Cleanup::anchor() {}
302 static void createStoreInstBefore(llvm::Value *value, Address addr,
303 llvm::Instruction *beforeInst) {
304 auto store = new llvm::StoreInst(value, addr.getPointer(), beforeInst);
305 store->setAlignment(addr.getAlignment().getAsAlign());
308 static llvm::LoadInst *createLoadInstBefore(Address addr, const Twine &name,
309 llvm::Instruction *beforeInst) {
310 return new llvm::LoadInst(addr.getElementType(), addr.getPointer(), name,
311 false, addr.getAlignment().getAsAlign(),
315 /// All the branch fixups on the EH stack have propagated out past the
316 /// outermost normal cleanup; resolve them all by adding cases to the
317 /// given switch instruction.
318 static void ResolveAllBranchFixups(CodeGenFunction &CGF,
319 llvm::SwitchInst *Switch,
320 llvm::BasicBlock *CleanupEntry) {
321 llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
323 for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) {
324 // Skip this fixup if its destination isn't set.
325 BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
326 if (Fixup.Destination == nullptr) continue;
328 // If there isn't an OptimisticBranchBlock, then InitialBranch is
329 // still pointing directly to its destination; forward it to the
330 // appropriate cleanup entry. This is required in the specific
332 // { std::string s; goto lbl; }
334 // i.e. where there's an unresolved fixup inside a single cleanup
335 // entry which we're currently popping.
336 if (Fixup.OptimisticBranchBlock == nullptr) {
337 createStoreInstBefore(CGF.Builder.getInt32(Fixup.DestinationIndex),
338 CGF.getNormalCleanupDestSlot(),
339 Fixup.InitialBranch);
340 Fixup.InitialBranch->setSuccessor(0, CleanupEntry);
343 // Don't add this case to the switch statement twice.
344 if (!CasesAdded.insert(Fixup.Destination).second)
347 Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
351 CGF.EHStack.clearFixups();
354 /// Transitions the terminator of the given exit-block of a cleanup to
355 /// be a cleanup switch.
356 static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
357 llvm::BasicBlock *Block) {
358 // If it's a branch, turn it into a switch whose default
359 // destination is its original target.
360 llvm::Instruction *Term = Block->getTerminator();
361 assert(Term && "can't transition block without terminator");
363 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
364 assert(Br->isUnconditional());
365 auto Load = createLoadInstBefore(CGF.getNormalCleanupDestSlot(),
366 "cleanup.dest", Term);
367 llvm::SwitchInst *Switch =
368 llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
369 Br->eraseFromParent();
372 return cast<llvm::SwitchInst>(Term);
376 void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
377 assert(Block && "resolving a null target block");
378 if (!EHStack.getNumBranchFixups()) return;
380 assert(EHStack.hasNormalCleanups() &&
381 "branch fixups exist with no normal cleanups on stack");
383 llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
384 bool ResolvedAny = false;
386 for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
387 // Skip this fixup if its destination doesn't match.
388 BranchFixup &Fixup = EHStack.getBranchFixup(I);
389 if (Fixup.Destination != Block) continue;
391 Fixup.Destination = nullptr;
394 // If it doesn't have an optimistic branch block, LatestBranch is
395 // already pointing to the right place.
396 llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
400 // Don't process the same optimistic branch block twice.
401 if (!ModifiedOptimisticBlocks.insert(BranchBB).second)
404 llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
406 // Add a case to the switch.
407 Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
411 EHStack.popNullFixups();
414 /// Pops cleanup blocks until the given savepoint is reached.
415 void CodeGenFunction::PopCleanupBlocks(
416 EHScopeStack::stable_iterator Old,
417 std::initializer_list<llvm::Value **> ValuesToReload) {
418 assert(Old.isValid());
420 bool HadBranches = false;
421 while (EHStack.stable_begin() != Old) {
422 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
423 HadBranches |= Scope.hasBranches();
425 // As long as Old strictly encloses the scope's enclosing normal
426 // cleanup, we're going to emit another normal cleanup which
427 // fallthrough can propagate through.
428 bool FallThroughIsBranchThrough =
429 Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
431 PopCleanupBlock(FallThroughIsBranchThrough);
434 // If we didn't have any branches, the insertion point before cleanups must
435 // dominate the current insertion point and we don't need to reload any
440 // Spill and reload all values that the caller wants to be live at the current
442 for (llvm::Value **ReloadedValue : ValuesToReload) {
443 auto *Inst = dyn_cast_or_null<llvm::Instruction>(*ReloadedValue);
447 // Don't spill static allocas, they dominate all cleanups. These are created
448 // by binding a reference to a local variable or temporary.
449 auto *AI = dyn_cast<llvm::AllocaInst>(Inst);
450 if (AI && AI->isStaticAlloca())
454 CreateDefaultAlignTempAlloca(Inst->getType(), "tmp.exprcleanup");
456 // Find an insertion point after Inst and spill it to the temporary.
457 llvm::BasicBlock::iterator InsertBefore;
458 if (auto *Invoke = dyn_cast<llvm::InvokeInst>(Inst))
459 InsertBefore = Invoke->getNormalDest()->getFirstInsertionPt();
461 InsertBefore = std::next(Inst->getIterator());
462 CGBuilderTy(CGM, &*InsertBefore).CreateStore(Inst, Tmp);
464 // Reload the value at the current insertion point.
465 *ReloadedValue = Builder.CreateLoad(Tmp);
469 /// Pops cleanup blocks until the given savepoint is reached, then add the
470 /// cleanups from the given savepoint in the lifetime-extended cleanups stack.
471 void CodeGenFunction::PopCleanupBlocks(
472 EHScopeStack::stable_iterator Old, size_t OldLifetimeExtendedSize,
473 std::initializer_list<llvm::Value **> ValuesToReload) {
474 PopCleanupBlocks(Old, ValuesToReload);
476 // Move our deferred cleanups onto the EH stack.
477 for (size_t I = OldLifetimeExtendedSize,
478 E = LifetimeExtendedCleanupStack.size(); I != E; /**/) {
479 // Alignment should be guaranteed by the vptrs in the individual cleanups.
480 assert((I % alignof(LifetimeExtendedCleanupHeader) == 0) &&
481 "misaligned cleanup stack entry");
483 LifetimeExtendedCleanupHeader &Header =
484 reinterpret_cast<LifetimeExtendedCleanupHeader&>(
485 LifetimeExtendedCleanupStack[I]);
488 EHStack.pushCopyOfCleanup(Header.getKind(),
489 &LifetimeExtendedCleanupStack[I],
491 I += Header.getSize();
493 if (Header.isConditional()) {
495 reinterpret_cast<Address &>(LifetimeExtendedCleanupStack[I]);
496 initFullExprCleanupWithFlag(ActiveFlag);
497 I += sizeof(ActiveFlag);
500 LifetimeExtendedCleanupStack.resize(OldLifetimeExtendedSize);
503 static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
504 EHCleanupScope &Scope) {
505 assert(Scope.isNormalCleanup());
506 llvm::BasicBlock *Entry = Scope.getNormalBlock();
508 Entry = CGF.createBasicBlock("cleanup");
509 Scope.setNormalBlock(Entry);
514 /// Attempts to reduce a cleanup's entry block to a fallthrough. This
515 /// is basically llvm::MergeBlockIntoPredecessor, except
516 /// simplified/optimized for the tighter constraints on cleanup blocks.
518 /// Returns the new block, whatever it is.
519 static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
520 llvm::BasicBlock *Entry) {
521 llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
522 if (!Pred) return Entry;
524 llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
525 if (!Br || Br->isConditional()) return Entry;
526 assert(Br->getSuccessor(0) == Entry);
528 // If we were previously inserting at the end of the cleanup entry
529 // block, we'll need to continue inserting at the end of the
531 bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
532 assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
535 Br->eraseFromParent();
537 // Replace all uses of the entry with the predecessor, in case there
538 // are phis in the cleanup.
539 Entry->replaceAllUsesWith(Pred);
542 Pred->getInstList().splice(Pred->end(), Entry->getInstList());
544 // Kill the entry block.
545 Entry->eraseFromParent();
548 CGF.Builder.SetInsertPoint(Pred);
553 static void EmitCleanup(CodeGenFunction &CGF,
554 EHScopeStack::Cleanup *Fn,
555 EHScopeStack::Cleanup::Flags flags,
556 Address ActiveFlag) {
557 // If there's an active flag, load it and skip the cleanup if it's
559 llvm::BasicBlock *ContBB = nullptr;
560 if (ActiveFlag.isValid()) {
561 ContBB = CGF.createBasicBlock("cleanup.done");
562 llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
563 llvm::Value *IsActive
564 = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
565 CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
566 CGF.EmitBlock(CleanupBB);
569 // Ask the cleanup to emit itself.
570 Fn->Emit(CGF, flags);
571 assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
573 // Emit the continuation block if there was an active flag.
574 if (ActiveFlag.isValid())
575 CGF.EmitBlock(ContBB);
578 static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
579 llvm::BasicBlock *From,
580 llvm::BasicBlock *To) {
581 // Exit is the exit block of a cleanup, so it always terminates in
582 // an unconditional branch or a switch.
583 llvm::Instruction *Term = Exit->getTerminator();
585 if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
586 assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
587 Br->setSuccessor(0, To);
589 llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
590 for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
591 if (Switch->getSuccessor(I) == From)
592 Switch->setSuccessor(I, To);
596 /// We don't need a normal entry block for the given cleanup.
597 /// Optimistic fixup branches can cause these blocks to come into
598 /// existence anyway; if so, destroy it.
600 /// The validity of this transformation is very much specific to the
601 /// exact ways in which we form branches to cleanup entries.
602 static void destroyOptimisticNormalEntry(CodeGenFunction &CGF,
603 EHCleanupScope &scope) {
604 llvm::BasicBlock *entry = scope.getNormalBlock();
607 // Replace all the uses with unreachable.
608 llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock();
609 for (llvm::BasicBlock::use_iterator
610 i = entry->use_begin(), e = entry->use_end(); i != e; ) {
614 use.set(unreachableBB);
616 // The only uses should be fixup switches.
617 llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
618 if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
619 // Replace the switch with a branch.
620 llvm::BranchInst::Create(si->case_begin()->getCaseSuccessor(), si);
622 // The switch operand is a load from the cleanup-dest alloca.
623 llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
625 // Destroy the switch.
626 si->eraseFromParent();
629 assert(condition->getOperand(0) == CGF.NormalCleanupDest.getPointer());
630 assert(condition->use_empty());
631 condition->eraseFromParent();
635 assert(entry->use_empty());
639 /// Pops a cleanup block. If the block includes a normal cleanup, the
640 /// current insertion point is threaded through the cleanup, as are
641 /// any branch fixups on the cleanup.
642 void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
643 assert(!EHStack.empty() && "cleanup stack is empty!");
644 assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
645 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
646 assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
648 // Remember activation information.
649 bool IsActive = Scope.isActive();
650 Address NormalActiveFlag =
651 Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag()
652 : Address::invalid();
653 Address EHActiveFlag =
654 Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag()
655 : Address::invalid();
657 // Check whether we need an EH cleanup. This is only true if we've
658 // generated a lazy EH cleanup block.
659 llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock();
660 assert(Scope.hasEHBranches() == (EHEntry != nullptr));
661 bool RequiresEHCleanup = (EHEntry != nullptr);
662 EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope();
664 // Check the three conditions which might require a normal cleanup:
666 // - whether there are branch fix-ups through this cleanup
667 unsigned FixupDepth = Scope.getFixupDepth();
668 bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
670 // - whether there are branch-throughs or branch-afters
671 bool HasExistingBranches = Scope.hasBranches();
673 // - whether there's a fallthrough
674 llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
675 bool HasFallthrough = (FallthroughSource != nullptr && IsActive);
677 // Branch-through fall-throughs leave the insertion point set to the
678 // end of the last cleanup, which points to the current scope. The
679 // rest of IR gen doesn't need to worry about this; it only happens
680 // during the execution of PopCleanupBlocks().
681 bool HasPrebranchedFallthrough =
682 (FallthroughSource && FallthroughSource->getTerminator());
684 // If this is a normal cleanup, then having a prebranched
685 // fallthrough implies that the fallthrough source unconditionally
687 assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
688 (Scope.getNormalBlock() &&
689 FallthroughSource->getTerminator()->getSuccessor(0)
690 == Scope.getNormalBlock()));
692 bool RequiresNormalCleanup = false;
693 if (Scope.isNormalCleanup() &&
694 (HasFixups || HasExistingBranches || HasFallthrough)) {
695 RequiresNormalCleanup = true;
698 // If we have a prebranched fallthrough into an inactive normal
699 // cleanup, rewrite it so that it leads to the appropriate place.
700 if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) {
701 llvm::BasicBlock *prebranchDest;
703 // If the prebranch is semantically branching through the next
704 // cleanup, just forward it to the next block, leaving the
705 // insertion point in the prebranched block.
706 if (FallthroughIsBranchThrough) {
707 EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup());
708 prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing));
710 // Otherwise, we need to make a new block. If the normal cleanup
711 // isn't being used at all, we could actually reuse the normal
712 // entry block, but this is simpler, and it avoids conflicts with
713 // dead optimistic fixup branches.
715 prebranchDest = createBasicBlock("forwarded-prebranch");
716 EmitBlock(prebranchDest);
719 llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
720 assert(normalEntry && !normalEntry->use_empty());
722 ForwardPrebranchedFallthrough(FallthroughSource,
723 normalEntry, prebranchDest);
726 // If we don't need the cleanup at all, we're done.
727 if (!RequiresNormalCleanup && !RequiresEHCleanup) {
728 destroyOptimisticNormalEntry(*this, Scope);
729 EHStack.popCleanup(); // safe because there are no fixups
730 assert(EHStack.getNumBranchFixups() == 0 ||
731 EHStack.hasNormalCleanups());
735 // Copy the cleanup emission data out. This uses either a stack
736 // array or malloc'd memory, depending on the size, which is
737 // behavior that SmallVector would provide, if we could use it
738 // here. Unfortunately, if you ask for a SmallVector<char>, the
739 // alignment isn't sufficient.
740 auto *CleanupSource = reinterpret_cast<char *>(Scope.getCleanupBuffer());
741 alignas(EHScopeStack::ScopeStackAlignment) char
742 CleanupBufferStack[8 * sizeof(void *)];
743 std::unique_ptr<char[]> CleanupBufferHeap;
744 size_t CleanupSize = Scope.getCleanupSize();
745 EHScopeStack::Cleanup *Fn;
747 if (CleanupSize <= sizeof(CleanupBufferStack)) {
748 memcpy(CleanupBufferStack, CleanupSource, CleanupSize);
749 Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferStack);
751 CleanupBufferHeap.reset(new char[CleanupSize]);
752 memcpy(CleanupBufferHeap.get(), CleanupSource, CleanupSize);
753 Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferHeap.get());
756 EHScopeStack::Cleanup::Flags cleanupFlags;
757 if (Scope.isNormalCleanup())
758 cleanupFlags.setIsNormalCleanupKind();
759 if (Scope.isEHCleanup())
760 cleanupFlags.setIsEHCleanupKind();
762 if (!RequiresNormalCleanup) {
763 destroyOptimisticNormalEntry(*this, Scope);
764 EHStack.popCleanup();
766 // If we have a fallthrough and no other need for the cleanup,
768 if (HasFallthrough && !HasPrebranchedFallthrough &&
769 !HasFixups && !HasExistingBranches) {
771 destroyOptimisticNormalEntry(*this, Scope);
772 EHStack.popCleanup();
774 EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
776 // Otherwise, the best approach is to thread everything through
777 // the cleanup block and then try to clean up after ourselves.
779 // Force the entry block to exist.
780 llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
782 // I. Set up the fallthrough edge in.
784 CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
786 // If there's a fallthrough, we need to store the cleanup
787 // destination index. For fall-throughs this is always zero.
788 if (HasFallthrough) {
789 if (!HasPrebranchedFallthrough)
790 Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
792 // Otherwise, save and clear the IP if we don't have fallthrough
793 // because the cleanup is inactive.
794 } else if (FallthroughSource) {
795 assert(!IsActive && "source without fallthrough for active cleanup");
796 savedInactiveFallthroughIP = Builder.saveAndClearIP();
799 // II. Emit the entry block. This implicitly branches to it if
800 // we have fallthrough. All the fixups and existing branches
801 // should already be branched to it.
802 EmitBlock(NormalEntry);
804 // III. Figure out where we're going and build the cleanup
807 bool HasEnclosingCleanups =
808 (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
810 // Compute the branch-through dest if we need it:
811 // - if there are branch-throughs threaded through the scope
812 // - if fall-through is a branch-through
813 // - if there are fixups that will be optimistically forwarded
814 // to the enclosing cleanup
815 llvm::BasicBlock *BranchThroughDest = nullptr;
816 if (Scope.hasBranchThroughs() ||
817 (FallthroughSource && FallthroughIsBranchThrough) ||
818 (HasFixups && HasEnclosingCleanups)) {
819 assert(HasEnclosingCleanups);
820 EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
821 BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
824 llvm::BasicBlock *FallthroughDest = nullptr;
825 SmallVector<llvm::Instruction*, 2> InstsToAppend;
827 // If there's exactly one branch-after and no other threads,
828 // we can route it without a switch.
829 if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
830 Scope.getNumBranchAfters() == 1) {
831 assert(!BranchThroughDest || !IsActive);
833 // Clean up the possibly dead store to the cleanup dest slot.
834 llvm::Instruction *NormalCleanupDestSlot =
835 cast<llvm::Instruction>(getNormalCleanupDestSlot().getPointer());
836 if (NormalCleanupDestSlot->hasOneUse()) {
837 NormalCleanupDestSlot->user_back()->eraseFromParent();
838 NormalCleanupDestSlot->eraseFromParent();
839 NormalCleanupDest = Address::invalid();
842 llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
843 InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
845 // Build a switch-out if we need it:
846 // - if there are branch-afters threaded through the scope
847 // - if fall-through is a branch-after
848 // - if there are fixups that have nowhere left to go and
849 // so must be immediately resolved
850 } else if (Scope.getNumBranchAfters() ||
851 (HasFallthrough && !FallthroughIsBranchThrough) ||
852 (HasFixups && !HasEnclosingCleanups)) {
854 llvm::BasicBlock *Default =
855 (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
857 // TODO: base this on the number of branch-afters and fixups
858 const unsigned SwitchCapacity = 10;
860 // pass the abnormal exit flag to Fn (SEH cleanup)
861 cleanupFlags.setHasExitSwitch();
863 llvm::LoadInst *Load =
864 createLoadInstBefore(getNormalCleanupDestSlot(), "cleanup.dest",
866 llvm::SwitchInst *Switch =
867 llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
869 InstsToAppend.push_back(Load);
870 InstsToAppend.push_back(Switch);
872 // Branch-after fallthrough.
873 if (FallthroughSource && !FallthroughIsBranchThrough) {
874 FallthroughDest = createBasicBlock("cleanup.cont");
876 Switch->addCase(Builder.getInt32(0), FallthroughDest);
879 for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
880 Switch->addCase(Scope.getBranchAfterIndex(I),
881 Scope.getBranchAfterBlock(I));
884 // If there aren't any enclosing cleanups, we can resolve all
886 if (HasFixups && !HasEnclosingCleanups)
887 ResolveAllBranchFixups(*this, Switch, NormalEntry);
889 // We should always have a branch-through destination in this case.
890 assert(BranchThroughDest);
891 InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
894 // IV. Pop the cleanup and emit it.
895 EHStack.popCleanup();
896 assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
898 EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
900 // Append the prepared cleanup prologue from above.
901 llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
902 for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
903 NormalExit->getInstList().push_back(InstsToAppend[I]);
905 // Optimistically hope that any fixups will continue falling through.
906 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
908 BranchFixup &Fixup = EHStack.getBranchFixup(I);
909 if (!Fixup.Destination) continue;
910 if (!Fixup.OptimisticBranchBlock) {
911 createStoreInstBefore(Builder.getInt32(Fixup.DestinationIndex),
912 getNormalCleanupDestSlot(),
913 Fixup.InitialBranch);
914 Fixup.InitialBranch->setSuccessor(0, NormalEntry);
916 Fixup.OptimisticBranchBlock = NormalExit;
919 // V. Set up the fallthrough edge out.
921 // Case 1: a fallthrough source exists but doesn't branch to the
922 // cleanup because the cleanup is inactive.
923 if (!HasFallthrough && FallthroughSource) {
924 // Prebranched fallthrough was forwarded earlier.
925 // Non-prebranched fallthrough doesn't need to be forwarded.
926 // Either way, all we need to do is restore the IP we cleared before.
928 Builder.restoreIP(savedInactiveFallthroughIP);
930 // Case 2: a fallthrough source exists and should branch to the
931 // cleanup, but we're not supposed to branch through to the next
933 } else if (HasFallthrough && FallthroughDest) {
934 assert(!FallthroughIsBranchThrough);
935 EmitBlock(FallthroughDest);
937 // Case 3: a fallthrough source exists and should branch to the
938 // cleanup and then through to the next.
939 } else if (HasFallthrough) {
940 // Everything is already set up for this.
942 // Case 4: no fallthrough source exists.
944 Builder.ClearInsertionPoint();
947 // VI. Assorted cleaning.
949 // Check whether we can merge NormalEntry into a single predecessor.
950 // This might invalidate (non-IR) pointers to NormalEntry.
951 llvm::BasicBlock *NewNormalEntry =
952 SimplifyCleanupEntry(*this, NormalEntry);
954 // If it did invalidate those pointers, and NormalEntry was the same
955 // as NormalExit, go back and patch up the fixups.
956 if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
957 for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
959 EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
963 assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
965 // Emit the EH cleanup if required.
966 if (RequiresEHCleanup) {
967 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
971 llvm::BasicBlock *NextAction = getEHDispatchBlock(EHParent);
973 // Push a terminate scope or cleanupendpad scope around the potentially
974 // throwing cleanups. For funclet EH personalities, the cleanupendpad models
975 // program termination when cleanups throw.
976 bool PushedTerminate = false;
977 SaveAndRestore<llvm::Instruction *> RestoreCurrentFuncletPad(
979 llvm::CleanupPadInst *CPI = nullptr;
981 const EHPersonality &Personality = EHPersonality::get(*this);
982 if (Personality.usesFuncletPads()) {
983 llvm::Value *ParentPad = CurrentFuncletPad;
985 ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
986 CurrentFuncletPad = CPI = Builder.CreateCleanupPad(ParentPad);
989 // Non-MSVC personalities need to terminate when an EH cleanup throws.
990 if (!Personality.isMSVCPersonality()) {
991 EHStack.pushTerminate();
992 PushedTerminate = true;
995 // We only actually emit the cleanup code if the cleanup is either
996 // active or was used before it was deactivated.
997 if (EHActiveFlag.isValid() || IsActive) {
998 cleanupFlags.setIsForEHCleanup();
999 EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
1003 Builder.CreateCleanupRet(CPI, NextAction);
1005 Builder.CreateBr(NextAction);
1007 // Leave the terminate scope.
1008 if (PushedTerminate)
1009 EHStack.popTerminate();
1011 Builder.restoreIP(SavedIP);
1013 SimplifyCleanupEntry(*this, EHEntry);
1017 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
1018 /// specified destination obviously has no cleanups to run. 'false' is always
1019 /// a conservatively correct answer for this method.
1020 bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const {
1021 assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
1022 && "stale jump destination");
1024 // Calculate the innermost active normal cleanup.
1025 EHScopeStack::stable_iterator TopCleanup =
1026 EHStack.getInnermostActiveNormalCleanup();
1028 // If we're not in an active normal cleanup scope, or if the
1029 // destination scope is within the innermost active normal cleanup
1030 // scope, we don't need to worry about fixups.
1031 if (TopCleanup == EHStack.stable_end() ||
1032 TopCleanup.encloses(Dest.getScopeDepth())) // works for invalid
1035 // Otherwise, we might need some cleanups.
1040 /// Terminate the current block by emitting a branch which might leave
1041 /// the current cleanup-protected scope. The target scope may not yet
1042 /// be known, in which case this will require a fixup.
1044 /// As a side-effect, this method clears the insertion point.
1045 void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
1046 assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
1047 && "stale jump destination");
1049 if (!HaveInsertPoint())
1052 // Create the branch.
1053 llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
1055 // Calculate the innermost active normal cleanup.
1056 EHScopeStack::stable_iterator
1057 TopCleanup = EHStack.getInnermostActiveNormalCleanup();
1059 // If we're not in an active normal cleanup scope, or if the
1060 // destination scope is within the innermost active normal cleanup
1061 // scope, we don't need to worry about fixups.
1062 if (TopCleanup == EHStack.stable_end() ||
1063 TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
1064 Builder.ClearInsertionPoint();
1068 // If we can't resolve the destination cleanup scope, just add this
1069 // to the current cleanup scope as a branch fixup.
1070 if (!Dest.getScopeDepth().isValid()) {
1071 BranchFixup &Fixup = EHStack.addBranchFixup();
1072 Fixup.Destination = Dest.getBlock();
1073 Fixup.DestinationIndex = Dest.getDestIndex();
1074 Fixup.InitialBranch = BI;
1075 Fixup.OptimisticBranchBlock = nullptr;
1077 Builder.ClearInsertionPoint();
1081 // Otherwise, thread through all the normal cleanups in scope.
1083 // Store the index at the start.
1084 llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1085 createStoreInstBefore(Index, getNormalCleanupDestSlot(), BI);
1087 // Adjust BI to point to the first cleanup block.
1089 EHCleanupScope &Scope =
1090 cast<EHCleanupScope>(*EHStack.find(TopCleanup));
1091 BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
1094 // Add this destination to all the scopes involved.
1095 EHScopeStack::stable_iterator I = TopCleanup;
1096 EHScopeStack::stable_iterator E = Dest.getScopeDepth();
1097 if (E.strictlyEncloses(I)) {
1099 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1100 assert(Scope.isNormalCleanup());
1101 I = Scope.getEnclosingNormalCleanup();
1103 // If this is the last cleanup we're propagating through, tell it
1104 // that there's a resolved jump moving through it.
1105 if (!E.strictlyEncloses(I)) {
1106 Scope.addBranchAfter(Index, Dest.getBlock());
1110 // Otherwise, tell the scope that there's a jump propagating
1111 // through it. If this isn't new information, all the rest of
1112 // the work has been done before.
1113 if (!Scope.addBranchThrough(Dest.getBlock()))
1118 Builder.ClearInsertionPoint();
1121 static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
1122 EHScopeStack::stable_iterator C) {
1123 // If we needed a normal block for any reason, that counts.
1124 if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
1127 // Check whether any enclosed cleanups were needed.
1128 for (EHScopeStack::stable_iterator
1129 I = EHStack.getInnermostNormalCleanup();
1131 assert(C.strictlyEncloses(I));
1132 EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1133 if (S.getNormalBlock()) return true;
1134 I = S.getEnclosingNormalCleanup();
1140 static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
1141 EHScopeStack::stable_iterator cleanup) {
1142 // If we needed an EH block for any reason, that counts.
1143 if (EHStack.find(cleanup)->hasEHBranches())
1146 // Check whether any enclosed cleanups were needed.
1147 for (EHScopeStack::stable_iterator
1148 i = EHStack.getInnermostEHScope(); i != cleanup; ) {
1149 assert(cleanup.strictlyEncloses(i));
1151 EHScope &scope = *EHStack.find(i);
1152 if (scope.hasEHBranches())
1155 i = scope.getEnclosingEHScope();
1161 enum ForActivation_t {
1166 /// The given cleanup block is changing activation state. Configure a
1167 /// cleanup variable if necessary.
1169 /// It would be good if we had some way of determining if there were
1170 /// extra uses *after* the change-over point.
1171 static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
1172 EHScopeStack::stable_iterator C,
1173 ForActivation_t kind,
1174 llvm::Instruction *dominatingIP) {
1175 EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
1177 // We always need the flag if we're activating the cleanup in a
1178 // conditional context, because we have to assume that the current
1179 // location doesn't necessarily dominate the cleanup's code.
1180 bool isActivatedInConditional =
1181 (kind == ForActivation && CGF.isInConditionalBranch());
1183 bool needFlag = false;
1185 // Calculate whether the cleanup was used:
1187 // - as a normal cleanup
1188 if (Scope.isNormalCleanup() &&
1189 (isActivatedInConditional || IsUsedAsNormalCleanup(CGF.EHStack, C))) {
1190 Scope.setTestFlagInNormalCleanup();
1194 // - as an EH cleanup
1195 if (Scope.isEHCleanup() &&
1196 (isActivatedInConditional || IsUsedAsEHCleanup(CGF.EHStack, C))) {
1197 Scope.setTestFlagInEHCleanup();
1201 // If it hasn't yet been used as either, we're done.
1202 if (!needFlag) return;
1204 Address var = Scope.getActiveFlag();
1205 if (!var.isValid()) {
1206 var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), CharUnits::One(),
1207 "cleanup.isactive");
1208 Scope.setActiveFlag(var);
1210 assert(dominatingIP && "no existing variable and no dominating IP!");
1212 // Initialize to true or false depending on whether it was
1213 // active up to this point.
1214 llvm::Constant *value = CGF.Builder.getInt1(kind == ForDeactivation);
1216 // If we're in a conditional block, ignore the dominating IP and
1217 // use the outermost conditional branch.
1218 if (CGF.isInConditionalBranch()) {
1219 CGF.setBeforeOutermostConditional(value, var);
1221 createStoreInstBefore(value, var, dominatingIP);
1225 CGF.Builder.CreateStore(CGF.Builder.getInt1(kind == ForActivation), var);
1228 /// Activate a cleanup that was created in an inactivated state.
1229 void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C,
1230 llvm::Instruction *dominatingIP) {
1231 assert(C != EHStack.stable_end() && "activating bottom of stack?");
1232 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1233 assert(!Scope.isActive() && "double activation");
1235 SetupCleanupBlockActivation(*this, C, ForActivation, dominatingIP);
1237 Scope.setActive(true);
1240 /// Deactive a cleanup that was created in an active state.
1241 void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C,
1242 llvm::Instruction *dominatingIP) {
1243 assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
1244 EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1245 assert(Scope.isActive() && "double deactivation");
1247 // If it's the top of the stack, just pop it, but do so only if it belongs
1248 // to the current RunCleanupsScope.
1249 if (C == EHStack.stable_begin() &&
1250 CurrentCleanupScopeDepth.strictlyEncloses(C)) {
1251 // If it's a normal cleanup, we need to pretend that the
1252 // fallthrough is unreachable.
1253 CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1255 Builder.restoreIP(SavedIP);
1259 // Otherwise, follow the general case.
1260 SetupCleanupBlockActivation(*this, C, ForDeactivation, dominatingIP);
1262 Scope.setActive(false);
1265 Address CodeGenFunction::getNormalCleanupDestSlot() {
1266 if (!NormalCleanupDest.isValid())
1268 CreateDefaultAlignTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
1269 return NormalCleanupDest;
1272 /// Emits all the code to cause the given temporary to be cleaned up.
1273 void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary,
1276 pushDestroy(NormalAndEHCleanup, Ptr, TempType, destroyCXXObject,
1277 /*useEHCleanup*/ true);