xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGCleanup.cpp (revision 0ad011ececb978e22a9bff2acf76633b094f1ff6)
1 //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains code dealing with the IR generation for cleanups
10 // and related information.
11 //
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.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "CGCleanup.h"
20 #include "CodeGenFunction.h"
21 #include "llvm/Support/SaveAndRestore.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.getAggregatePointer());
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, nullptr, ScalarLiteral);
42 
43     // Everything else needs an alloca.
44     Address addr =
45       CGF.CreateDefaultAlignTempAlloca(V->getType(), "saved-rvalue");
46     CGF.Builder.CreateStore(V, addr);
47     return saved_type(addr.getPointer(), nullptr, ScalarAddress);
48   }
49 
50   if (rv.isComplex()) {
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(), nullptr, ComplexAddress);
58   }
59 
60   assert(rv.isAggregate());
61   Address V = rv.getAggregateAddress(); // TODO: volatile?
62   if (!DominatingLLVMValue::needsSaving(V.getPointer()))
63     return saved_type(V.getPointer(), V.getElementType(), AggregateLiteral,
64                       V.getAlignment().getQuantity());
65 
66   Address addr =
67     CGF.CreateTempAlloca(V.getType(), CGF.getPointerAlign(), "saved-rvalue");
68   CGF.Builder.CreateStore(V.getPointer(), addr);
69   return saved_type(addr.getPointer(), V.getElementType(), AggregateAddress,
70                     V.getAlignment().getQuantity());
71 }
72 
73 /// Given a saved r-value produced by SaveRValue, perform the code
74 /// necessary to restore it to usability at the current insertion
75 /// point.
76 RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
77   auto getSavingAddress = [&](llvm::Value *value) {
78     auto *AI = cast<llvm::AllocaInst>(value);
79     return Address(value, AI->getAllocatedType(),
80                    CharUnits::fromQuantity(AI->getAlign().value()));
81   };
82   switch (K) {
83   case ScalarLiteral:
84     return RValue::get(Value);
85   case ScalarAddress:
86     return RValue::get(CGF.Builder.CreateLoad(getSavingAddress(Value)));
87   case AggregateLiteral:
88     return RValue::getAggregate(
89         Address(Value, ElementType, CharUnits::fromQuantity(Align)));
90   case AggregateAddress: {
91     auto addr = CGF.Builder.CreateLoad(getSavingAddress(Value));
92     return RValue::getAggregate(
93         Address(addr, ElementType, CharUnits::fromQuantity(Align)));
94   }
95   case ComplexAddress: {
96     Address address = getSavingAddress(Value);
97     llvm::Value *real =
98         CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 0));
99     llvm::Value *imag =
100         CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(address, 1));
101     return RValue::getComplex(real, imag);
102   }
103   }
104 
105   llvm_unreachable("bad saved r-value kind");
106 }
107 
108 /// Push an entry of the given size onto this protected-scope stack.
109 char *EHScopeStack::allocate(size_t Size) {
110   Size = llvm::alignTo(Size, ScopeStackAlignment);
111   if (!StartOfBuffer) {
112     unsigned Capacity = 1024;
113     while (Capacity < Size) Capacity *= 2;
114     StartOfBuffer = new char[Capacity];
115     StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
116   } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) {
117     unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
118     unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
119 
120     unsigned NewCapacity = CurrentCapacity;
121     do {
122       NewCapacity *= 2;
123     } while (NewCapacity < UsedCapacity + Size);
124 
125     char *NewStartOfBuffer = new char[NewCapacity];
126     char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
127     char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
128     memcpy(NewStartOfData, StartOfData, UsedCapacity);
129     delete [] StartOfBuffer;
130     StartOfBuffer = NewStartOfBuffer;
131     EndOfBuffer = NewEndOfBuffer;
132     StartOfData = NewStartOfData;
133   }
134 
135   assert(StartOfBuffer + Size <= StartOfData);
136   StartOfData -= Size;
137   return StartOfData;
138 }
139 
140 void EHScopeStack::deallocate(size_t Size) {
141   StartOfData += llvm::alignTo(Size, ScopeStackAlignment);
142 }
143 
144 bool EHScopeStack::containsOnlyLifetimeMarkers(
145     EHScopeStack::stable_iterator Old) const {
146   for (EHScopeStack::iterator it = begin(); stabilize(it) != Old; it++) {
147     EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*it);
148     if (!cleanup || !cleanup->isLifetimeMarker())
149       return false;
150   }
151 
152   return true;
153 }
154 
155 bool EHScopeStack::requiresLandingPad() const {
156   for (stable_iterator si = getInnermostEHScope(); si != stable_end(); ) {
157     // Skip lifetime markers.
158     if (auto *cleanup = dyn_cast<EHCleanupScope>(&*find(si)))
159       if (cleanup->isLifetimeMarker()) {
160         si = cleanup->getEnclosingEHScope();
161         continue;
162       }
163     return true;
164   }
165 
166   return false;
167 }
168 
169 EHScopeStack::stable_iterator
170 EHScopeStack::getInnermostActiveNormalCleanup() const {
171   for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
172          si != se; ) {
173     EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
174     if (cleanup.isActive()) return si;
175     si = cleanup.getEnclosingNormalCleanup();
176   }
177   return stable_end();
178 }
179 
180 
181 void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
182   char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
183   bool IsNormalCleanup = Kind & NormalCleanup;
184   bool IsEHCleanup = Kind & EHCleanup;
185   bool IsLifetimeMarker = Kind & LifetimeMarker;
186 
187   // Per C++ [except.terminate], it is implementation-defined whether none,
188   // some, or all cleanups are called before std::terminate. Thus, when
189   // terminate is the current EH scope, we may skip adding any EH cleanup
190   // scopes.
191   if (InnermostEHScope != stable_end() &&
192       find(InnermostEHScope)->getKind() == EHScope::Terminate)
193     IsEHCleanup = false;
194 
195   EHCleanupScope *Scope =
196     new (Buffer) EHCleanupScope(IsNormalCleanup,
197                                 IsEHCleanup,
198                                 Size,
199                                 BranchFixups.size(),
200                                 InnermostNormalCleanup,
201                                 InnermostEHScope);
202   if (IsNormalCleanup)
203     InnermostNormalCleanup = stable_begin();
204   if (IsEHCleanup)
205     InnermostEHScope = stable_begin();
206   if (IsLifetimeMarker)
207     Scope->setLifetimeMarker();
208 
209   // With Windows -EHa, Invoke llvm.seh.scope.begin() for EHCleanup
210   if (CGF->getLangOpts().EHAsynch && IsEHCleanup && !IsLifetimeMarker &&
211       CGF->getTarget().getCXXABI().isMicrosoft())
212     CGF->EmitSehCppScopeBegin();
213 
214   return Scope->getCleanupBuffer();
215 }
216 
217 void EHScopeStack::popCleanup() {
218   assert(!empty() && "popping exception stack when not empty");
219 
220   assert(isa<EHCleanupScope>(*begin()));
221   EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
222   InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
223   InnermostEHScope = Cleanup.getEnclosingEHScope();
224   deallocate(Cleanup.getAllocatedSize());
225 
226   // Destroy the cleanup.
227   Cleanup.Destroy();
228 
229   // Check whether we can shrink the branch-fixups stack.
230   if (!BranchFixups.empty()) {
231     // If we no longer have any normal cleanups, all the fixups are
232     // complete.
233     if (!hasNormalCleanups())
234       BranchFixups.clear();
235 
236     // Otherwise we can still trim out unnecessary nulls.
237     else
238       popNullFixups();
239   }
240 }
241 
242 EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) {
243   assert(getInnermostEHScope() == stable_end());
244   char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters));
245   EHFilterScope *filter = new (buffer) EHFilterScope(numFilters);
246   InnermostEHScope = stable_begin();
247   return filter;
248 }
249 
250 void EHScopeStack::popFilter() {
251   assert(!empty() && "popping exception stack when not empty");
252 
253   EHFilterScope &filter = cast<EHFilterScope>(*begin());
254   deallocate(EHFilterScope::getSizeForNumFilters(filter.getNumFilters()));
255 
256   InnermostEHScope = filter.getEnclosingEHScope();
257 }
258 
259 EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) {
260   char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers));
261   EHCatchScope *scope =
262     new (buffer) EHCatchScope(numHandlers, InnermostEHScope);
263   InnermostEHScope = stable_begin();
264   return scope;
265 }
266 
267 void EHScopeStack::pushTerminate() {
268   char *Buffer = allocate(EHTerminateScope::getSize());
269   new (Buffer) EHTerminateScope(InnermostEHScope);
270   InnermostEHScope = stable_begin();
271 }
272 
273 /// Remove any 'null' fixups on the stack.  However, we can't pop more
274 /// fixups than the fixup depth on the innermost normal cleanup, or
275 /// else fixups that we try to add to that cleanup will end up in the
276 /// wrong place.  We *could* try to shrink fixup depths, but that's
277 /// actually a lot of work for little benefit.
278 void EHScopeStack::popNullFixups() {
279   // We expect this to only be called when there's still an innermost
280   // normal cleanup;  otherwise there really shouldn't be any fixups.
281   assert(hasNormalCleanups());
282 
283   EHScopeStack::iterator it = find(InnermostNormalCleanup);
284   unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
285   assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
286 
287   while (BranchFixups.size() > MinSize &&
288          BranchFixups.back().Destination == nullptr)
289     BranchFixups.pop_back();
290 }
291 
292 Address CodeGenFunction::createCleanupActiveFlag() {
293   // Create a variable to decide whether the cleanup needs to be run.
294   Address active = CreateTempAllocaWithoutCast(
295       Builder.getInt1Ty(), CharUnits::One(), "cleanup.cond");
296 
297   // Initialize it to false at a site that's guaranteed to be run
298   // before each evaluation.
299   setBeforeOutermostConditional(Builder.getFalse(), active);
300 
301   // Initialize it to true at the current location.
302   Builder.CreateStore(Builder.getTrue(), active);
303 
304   return active;
305 }
306 
307 void CodeGenFunction::initFullExprCleanupWithFlag(Address ActiveFlag) {
308   // Set that as the active flag in the cleanup.
309   EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
310   assert(!cleanup.hasActiveFlag() && "cleanup already has active flag?");
311   cleanup.setActiveFlag(ActiveFlag);
312 
313   if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup();
314   if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
315 }
316 
317 void EHScopeStack::Cleanup::anchor() {}
318 
319 static void createStoreInstBefore(llvm::Value *value, Address addr,
320                                   llvm::Instruction *beforeInst) {
321   auto store = new llvm::StoreInst(value, addr.getPointer(), beforeInst);
322   store->setAlignment(addr.getAlignment().getAsAlign());
323 }
324 
325 static llvm::LoadInst *createLoadInstBefore(Address addr, const Twine &name,
326                                             llvm::Instruction *beforeInst) {
327   return new llvm::LoadInst(addr.getElementType(), addr.getPointer(), name,
328                             false, addr.getAlignment().getAsAlign(),
329                             beforeInst);
330 }
331 
332 /// All the branch fixups on the EH stack have propagated out past the
333 /// outermost normal cleanup; resolve them all by adding cases to the
334 /// given switch instruction.
335 static void ResolveAllBranchFixups(CodeGenFunction &CGF,
336                                    llvm::SwitchInst *Switch,
337                                    llvm::BasicBlock *CleanupEntry) {
338   llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
339 
340   for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) {
341     // Skip this fixup if its destination isn't set.
342     BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
343     if (Fixup.Destination == nullptr) continue;
344 
345     // If there isn't an OptimisticBranchBlock, then InitialBranch is
346     // still pointing directly to its destination; forward it to the
347     // appropriate cleanup entry.  This is required in the specific
348     // case of
349     //   { std::string s; goto lbl; }
350     //   lbl:
351     // i.e. where there's an unresolved fixup inside a single cleanup
352     // entry which we're currently popping.
353     if (Fixup.OptimisticBranchBlock == nullptr) {
354       createStoreInstBefore(CGF.Builder.getInt32(Fixup.DestinationIndex),
355                             CGF.getNormalCleanupDestSlot(),
356                             Fixup.InitialBranch);
357       Fixup.InitialBranch->setSuccessor(0, CleanupEntry);
358     }
359 
360     // Don't add this case to the switch statement twice.
361     if (!CasesAdded.insert(Fixup.Destination).second)
362       continue;
363 
364     Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
365                     Fixup.Destination);
366   }
367 
368   CGF.EHStack.clearFixups();
369 }
370 
371 /// Transitions the terminator of the given exit-block of a cleanup to
372 /// be a cleanup switch.
373 static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
374                                                    llvm::BasicBlock *Block) {
375   // If it's a branch, turn it into a switch whose default
376   // destination is its original target.
377   llvm::Instruction *Term = Block->getTerminator();
378   assert(Term && "can't transition block without terminator");
379 
380   if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
381     assert(Br->isUnconditional());
382     auto Load = createLoadInstBefore(CGF.getNormalCleanupDestSlot(),
383                                      "cleanup.dest", Term);
384     llvm::SwitchInst *Switch =
385       llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
386     Br->eraseFromParent();
387     return Switch;
388   } else {
389     return cast<llvm::SwitchInst>(Term);
390   }
391 }
392 
393 void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
394   assert(Block && "resolving a null target block");
395   if (!EHStack.getNumBranchFixups()) return;
396 
397   assert(EHStack.hasNormalCleanups() &&
398          "branch fixups exist with no normal cleanups on stack");
399 
400   llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
401   bool ResolvedAny = false;
402 
403   for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
404     // Skip this fixup if its destination doesn't match.
405     BranchFixup &Fixup = EHStack.getBranchFixup(I);
406     if (Fixup.Destination != Block) continue;
407 
408     Fixup.Destination = nullptr;
409     ResolvedAny = true;
410 
411     // If it doesn't have an optimistic branch block, LatestBranch is
412     // already pointing to the right place.
413     llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
414     if (!BranchBB)
415       continue;
416 
417     // Don't process the same optimistic branch block twice.
418     if (!ModifiedOptimisticBlocks.insert(BranchBB).second)
419       continue;
420 
421     llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
422 
423     // Add a case to the switch.
424     Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
425   }
426 
427   if (ResolvedAny)
428     EHStack.popNullFixups();
429 }
430 
431 /// Pops cleanup blocks until the given savepoint is reached.
432 void CodeGenFunction::PopCleanupBlocks(
433     EHScopeStack::stable_iterator Old,
434     std::initializer_list<llvm::Value **> ValuesToReload) {
435   assert(Old.isValid());
436 
437   bool HadBranches = false;
438   while (EHStack.stable_begin() != Old) {
439     EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
440     HadBranches |= Scope.hasBranches();
441 
442     // As long as Old strictly encloses the scope's enclosing normal
443     // cleanup, we're going to emit another normal cleanup which
444     // fallthrough can propagate through.
445     bool FallThroughIsBranchThrough =
446       Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
447 
448     PopCleanupBlock(FallThroughIsBranchThrough);
449   }
450 
451   // If we didn't have any branches, the insertion point before cleanups must
452   // dominate the current insertion point and we don't need to reload any
453   // values.
454   if (!HadBranches)
455     return;
456 
457   // Spill and reload all values that the caller wants to be live at the current
458   // insertion point.
459   for (llvm::Value **ReloadedValue : ValuesToReload) {
460     auto *Inst = dyn_cast_or_null<llvm::Instruction>(*ReloadedValue);
461     if (!Inst)
462       continue;
463 
464     // Don't spill static allocas, they dominate all cleanups. These are created
465     // by binding a reference to a local variable or temporary.
466     auto *AI = dyn_cast<llvm::AllocaInst>(Inst);
467     if (AI && AI->isStaticAlloca())
468       continue;
469 
470     Address Tmp =
471         CreateDefaultAlignTempAlloca(Inst->getType(), "tmp.exprcleanup");
472 
473     // Find an insertion point after Inst and spill it to the temporary.
474     llvm::BasicBlock::iterator InsertBefore;
475     if (auto *Invoke = dyn_cast<llvm::InvokeInst>(Inst))
476       InsertBefore = Invoke->getNormalDest()->getFirstInsertionPt();
477     else
478       InsertBefore = std::next(Inst->getIterator());
479     CGBuilderTy(CGM, &*InsertBefore).CreateStore(Inst, Tmp);
480 
481     // Reload the value at the current insertion point.
482     *ReloadedValue = Builder.CreateLoad(Tmp);
483   }
484 }
485 
486 /// Pops cleanup blocks until the given savepoint is reached, then add the
487 /// cleanups from the given savepoint in the lifetime-extended cleanups stack.
488 void CodeGenFunction::PopCleanupBlocks(
489     EHScopeStack::stable_iterator Old, size_t OldLifetimeExtendedSize,
490     std::initializer_list<llvm::Value **> ValuesToReload) {
491   PopCleanupBlocks(Old, ValuesToReload);
492 
493   // Move our deferred cleanups onto the EH stack.
494   for (size_t I = OldLifetimeExtendedSize,
495               E = LifetimeExtendedCleanupStack.size(); I != E; /**/) {
496     // Alignment should be guaranteed by the vptrs in the individual cleanups.
497     assert((I % alignof(LifetimeExtendedCleanupHeader) == 0) &&
498            "misaligned cleanup stack entry");
499 
500     LifetimeExtendedCleanupHeader &Header =
501         reinterpret_cast<LifetimeExtendedCleanupHeader&>(
502             LifetimeExtendedCleanupStack[I]);
503     I += sizeof(Header);
504 
505     EHStack.pushCopyOfCleanup(Header.getKind(),
506                               &LifetimeExtendedCleanupStack[I],
507                               Header.getSize());
508     I += Header.getSize();
509 
510     if (Header.isConditional()) {
511       Address ActiveFlag =
512           reinterpret_cast<Address &>(LifetimeExtendedCleanupStack[I]);
513       initFullExprCleanupWithFlag(ActiveFlag);
514       I += sizeof(ActiveFlag);
515     }
516   }
517   LifetimeExtendedCleanupStack.resize(OldLifetimeExtendedSize);
518 }
519 
520 static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
521                                            EHCleanupScope &Scope) {
522   assert(Scope.isNormalCleanup());
523   llvm::BasicBlock *Entry = Scope.getNormalBlock();
524   if (!Entry) {
525     Entry = CGF.createBasicBlock("cleanup");
526     Scope.setNormalBlock(Entry);
527   }
528   return Entry;
529 }
530 
531 /// Attempts to reduce a cleanup's entry block to a fallthrough.  This
532 /// is basically llvm::MergeBlockIntoPredecessor, except
533 /// simplified/optimized for the tighter constraints on cleanup blocks.
534 ///
535 /// Returns the new block, whatever it is.
536 static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
537                                               llvm::BasicBlock *Entry) {
538   llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
539   if (!Pred) return Entry;
540 
541   llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
542   if (!Br || Br->isConditional()) return Entry;
543   assert(Br->getSuccessor(0) == Entry);
544 
545   // If we were previously inserting at the end of the cleanup entry
546   // block, we'll need to continue inserting at the end of the
547   // predecessor.
548   bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
549   assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
550 
551   // Kill the branch.
552   Br->eraseFromParent();
553 
554   // Replace all uses of the entry with the predecessor, in case there
555   // are phis in the cleanup.
556   Entry->replaceAllUsesWith(Pred);
557 
558   // Merge the blocks.
559   Pred->splice(Pred->end(), Entry);
560 
561   // Kill the entry block.
562   Entry->eraseFromParent();
563 
564   if (WasInsertBlock)
565     CGF.Builder.SetInsertPoint(Pred);
566 
567   return Pred;
568 }
569 
570 static void EmitCleanup(CodeGenFunction &CGF,
571                         EHScopeStack::Cleanup *Fn,
572                         EHScopeStack::Cleanup::Flags flags,
573                         Address ActiveFlag) {
574   // If there's an active flag, load it and skip the cleanup if it's
575   // false.
576   llvm::BasicBlock *ContBB = nullptr;
577   if (ActiveFlag.isValid()) {
578     ContBB = CGF.createBasicBlock("cleanup.done");
579     llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
580     llvm::Value *IsActive
581       = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
582     CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
583     CGF.EmitBlock(CleanupBB);
584   }
585 
586   // Ask the cleanup to emit itself.
587   Fn->Emit(CGF, flags);
588   assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
589 
590   // Emit the continuation block if there was an active flag.
591   if (ActiveFlag.isValid())
592     CGF.EmitBlock(ContBB);
593 }
594 
595 static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
596                                           llvm::BasicBlock *From,
597                                           llvm::BasicBlock *To) {
598   // Exit is the exit block of a cleanup, so it always terminates in
599   // an unconditional branch or a switch.
600   llvm::Instruction *Term = Exit->getTerminator();
601 
602   if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
603     assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
604     Br->setSuccessor(0, To);
605   } else {
606     llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
607     for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
608       if (Switch->getSuccessor(I) == From)
609         Switch->setSuccessor(I, To);
610   }
611 }
612 
613 /// We don't need a normal entry block for the given cleanup.
614 /// Optimistic fixup branches can cause these blocks to come into
615 /// existence anyway;  if so, destroy it.
616 ///
617 /// The validity of this transformation is very much specific to the
618 /// exact ways in which we form branches to cleanup entries.
619 static void destroyOptimisticNormalEntry(CodeGenFunction &CGF,
620                                          EHCleanupScope &scope) {
621   llvm::BasicBlock *entry = scope.getNormalBlock();
622   if (!entry) return;
623 
624   // Replace all the uses with unreachable.
625   llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock();
626   for (llvm::BasicBlock::use_iterator
627          i = entry->use_begin(), e = entry->use_end(); i != e; ) {
628     llvm::Use &use = *i;
629     ++i;
630 
631     use.set(unreachableBB);
632 
633     // The only uses should be fixup switches.
634     llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
635     if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
636       // Replace the switch with a branch.
637       llvm::BranchInst::Create(si->case_begin()->getCaseSuccessor(), si);
638 
639       // The switch operand is a load from the cleanup-dest alloca.
640       llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
641 
642       // Destroy the switch.
643       si->eraseFromParent();
644 
645       // Destroy the load.
646       assert(condition->getOperand(0) == CGF.NormalCleanupDest.getPointer());
647       assert(condition->use_empty());
648       condition->eraseFromParent();
649     }
650   }
651 
652   assert(entry->use_empty());
653   delete entry;
654 }
655 
656 /// Pops a cleanup block.  If the block includes a normal cleanup, the
657 /// current insertion point is threaded through the cleanup, as are
658 /// any branch fixups on the cleanup.
659 void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
660   assert(!EHStack.empty() && "cleanup stack is empty!");
661   assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
662   EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
663   assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
664 
665   // Remember activation information.
666   bool IsActive = Scope.isActive();
667   Address NormalActiveFlag =
668     Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag()
669                                           : Address::invalid();
670   Address EHActiveFlag =
671     Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag()
672                                       : Address::invalid();
673 
674   // Check whether we need an EH cleanup.  This is only true if we've
675   // generated a lazy EH cleanup block.
676   llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock();
677   assert(Scope.hasEHBranches() == (EHEntry != nullptr));
678   bool RequiresEHCleanup = (EHEntry != nullptr);
679   EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope();
680 
681   // Check the three conditions which might require a normal cleanup:
682 
683   // - whether there are branch fix-ups through this cleanup
684   unsigned FixupDepth = Scope.getFixupDepth();
685   bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
686 
687   // - whether there are branch-throughs or branch-afters
688   bool HasExistingBranches = Scope.hasBranches();
689 
690   // - whether there's a fallthrough
691   llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
692   bool HasFallthrough = (FallthroughSource != nullptr && IsActive);
693 
694   // Branch-through fall-throughs leave the insertion point set to the
695   // end of the last cleanup, which points to the current scope.  The
696   // rest of IR gen doesn't need to worry about this; it only happens
697   // during the execution of PopCleanupBlocks().
698   bool HasPrebranchedFallthrough =
699     (FallthroughSource && FallthroughSource->getTerminator());
700 
701   // If this is a normal cleanup, then having a prebranched
702   // fallthrough implies that the fallthrough source unconditionally
703   // jumps here.
704   assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
705          (Scope.getNormalBlock() &&
706           FallthroughSource->getTerminator()->getSuccessor(0)
707             == Scope.getNormalBlock()));
708 
709   bool RequiresNormalCleanup = false;
710   if (Scope.isNormalCleanup() &&
711       (HasFixups || HasExistingBranches || HasFallthrough)) {
712     RequiresNormalCleanup = true;
713   }
714 
715   // If we have a prebranched fallthrough into an inactive normal
716   // cleanup, rewrite it so that it leads to the appropriate place.
717   if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) {
718     llvm::BasicBlock *prebranchDest;
719 
720     // If the prebranch is semantically branching through the next
721     // cleanup, just forward it to the next block, leaving the
722     // insertion point in the prebranched block.
723     if (FallthroughIsBranchThrough) {
724       EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup());
725       prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing));
726 
727     // Otherwise, we need to make a new block.  If the normal cleanup
728     // isn't being used at all, we could actually reuse the normal
729     // entry block, but this is simpler, and it avoids conflicts with
730     // dead optimistic fixup branches.
731     } else {
732       prebranchDest = createBasicBlock("forwarded-prebranch");
733       EmitBlock(prebranchDest);
734     }
735 
736     llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
737     assert(normalEntry && !normalEntry->use_empty());
738 
739     ForwardPrebranchedFallthrough(FallthroughSource,
740                                   normalEntry, prebranchDest);
741   }
742 
743   // If we don't need the cleanup at all, we're done.
744   if (!RequiresNormalCleanup && !RequiresEHCleanup) {
745     destroyOptimisticNormalEntry(*this, Scope);
746     EHStack.popCleanup(); // safe because there are no fixups
747     assert(EHStack.getNumBranchFixups() == 0 ||
748            EHStack.hasNormalCleanups());
749     return;
750   }
751 
752   // Copy the cleanup emission data out.  This uses either a stack
753   // array or malloc'd memory, depending on the size, which is
754   // behavior that SmallVector would provide, if we could use it
755   // here. Unfortunately, if you ask for a SmallVector<char>, the
756   // alignment isn't sufficient.
757   auto *CleanupSource = reinterpret_cast<char *>(Scope.getCleanupBuffer());
758   alignas(EHScopeStack::ScopeStackAlignment) char
759       CleanupBufferStack[8 * sizeof(void *)];
760   std::unique_ptr<char[]> CleanupBufferHeap;
761   size_t CleanupSize = Scope.getCleanupSize();
762   EHScopeStack::Cleanup *Fn;
763 
764   if (CleanupSize <= sizeof(CleanupBufferStack)) {
765     memcpy(CleanupBufferStack, CleanupSource, CleanupSize);
766     Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferStack);
767   } else {
768     CleanupBufferHeap.reset(new char[CleanupSize]);
769     memcpy(CleanupBufferHeap.get(), CleanupSource, CleanupSize);
770     Fn = reinterpret_cast<EHScopeStack::Cleanup *>(CleanupBufferHeap.get());
771   }
772 
773   EHScopeStack::Cleanup::Flags cleanupFlags;
774   if (Scope.isNormalCleanup())
775     cleanupFlags.setIsNormalCleanupKind();
776   if (Scope.isEHCleanup())
777     cleanupFlags.setIsEHCleanupKind();
778 
779   // Under -EHa, invoke seh.scope.end() to mark scope end before dtor
780   bool IsEHa = getLangOpts().EHAsynch && !Scope.isLifetimeMarker();
781   const EHPersonality &Personality = EHPersonality::get(*this);
782   if (!RequiresNormalCleanup) {
783     // Mark CPP scope end for passed-by-value Arg temp
784     //   per Windows ABI which is "normally" Cleanup in callee
785     if (IsEHa && getInvokeDest() && Builder.GetInsertBlock()) {
786       if (Personality.isMSVCXXPersonality())
787         EmitSehCppScopeEnd();
788     }
789     destroyOptimisticNormalEntry(*this, Scope);
790     EHStack.popCleanup();
791   } else {
792     // If we have a fallthrough and no other need for the cleanup,
793     // emit it directly.
794     if (HasFallthrough && !HasPrebranchedFallthrough && !HasFixups &&
795         !HasExistingBranches) {
796 
797       // mark SEH scope end for fall-through flow
798       if (IsEHa && getInvokeDest()) {
799         if (Personality.isMSVCXXPersonality())
800           EmitSehCppScopeEnd();
801         else
802           EmitSehTryScopeEnd();
803       }
804 
805       destroyOptimisticNormalEntry(*this, Scope);
806       EHStack.popCleanup();
807 
808       EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
809 
810     // Otherwise, the best approach is to thread everything through
811     // the cleanup block and then try to clean up after ourselves.
812     } else {
813       // Force the entry block to exist.
814       llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
815 
816       // I.  Set up the fallthrough edge in.
817 
818       CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
819 
820       // If there's a fallthrough, we need to store the cleanup
821       // destination index.  For fall-throughs this is always zero.
822       if (HasFallthrough) {
823         if (!HasPrebranchedFallthrough)
824           Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
825 
826       // Otherwise, save and clear the IP if we don't have fallthrough
827       // because the cleanup is inactive.
828       } else if (FallthroughSource) {
829         assert(!IsActive && "source without fallthrough for active cleanup");
830         savedInactiveFallthroughIP = Builder.saveAndClearIP();
831       }
832 
833       // II.  Emit the entry block.  This implicitly branches to it if
834       // we have fallthrough.  All the fixups and existing branches
835       // should already be branched to it.
836       EmitBlock(NormalEntry);
837 
838       // intercept normal cleanup to mark SEH scope end
839       if (IsEHa && getInvokeDest()) {
840         if (Personality.isMSVCXXPersonality())
841           EmitSehCppScopeEnd();
842         else
843           EmitSehTryScopeEnd();
844       }
845 
846       // III.  Figure out where we're going and build the cleanup
847       // epilogue.
848 
849       bool HasEnclosingCleanups =
850         (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
851 
852       // Compute the branch-through dest if we need it:
853       //   - if there are branch-throughs threaded through the scope
854       //   - if fall-through is a branch-through
855       //   - if there are fixups that will be optimistically forwarded
856       //     to the enclosing cleanup
857       llvm::BasicBlock *BranchThroughDest = nullptr;
858       if (Scope.hasBranchThroughs() ||
859           (FallthroughSource && FallthroughIsBranchThrough) ||
860           (HasFixups && HasEnclosingCleanups)) {
861         assert(HasEnclosingCleanups);
862         EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
863         BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
864       }
865 
866       llvm::BasicBlock *FallthroughDest = nullptr;
867       SmallVector<llvm::Instruction*, 2> InstsToAppend;
868 
869       // If there's exactly one branch-after and no other threads,
870       // we can route it without a switch.
871       if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
872           Scope.getNumBranchAfters() == 1) {
873         assert(!BranchThroughDest || !IsActive);
874 
875         // Clean up the possibly dead store to the cleanup dest slot.
876         llvm::Instruction *NormalCleanupDestSlot =
877             cast<llvm::Instruction>(getNormalCleanupDestSlot().getPointer());
878         if (NormalCleanupDestSlot->hasOneUse()) {
879           NormalCleanupDestSlot->user_back()->eraseFromParent();
880           NormalCleanupDestSlot->eraseFromParent();
881           NormalCleanupDest = Address::invalid();
882         }
883 
884         llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
885         InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
886 
887       // Build a switch-out if we need it:
888       //   - if there are branch-afters threaded through the scope
889       //   - if fall-through is a branch-after
890       //   - if there are fixups that have nowhere left to go and
891       //     so must be immediately resolved
892       } else if (Scope.getNumBranchAfters() ||
893                  (HasFallthrough && !FallthroughIsBranchThrough) ||
894                  (HasFixups && !HasEnclosingCleanups)) {
895 
896         llvm::BasicBlock *Default =
897           (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
898 
899         // TODO: base this on the number of branch-afters and fixups
900         const unsigned SwitchCapacity = 10;
901 
902         // pass the abnormal exit flag to Fn (SEH cleanup)
903         cleanupFlags.setHasExitSwitch();
904 
905         llvm::LoadInst *Load =
906           createLoadInstBefore(getNormalCleanupDestSlot(), "cleanup.dest",
907                                nullptr);
908         llvm::SwitchInst *Switch =
909           llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
910 
911         InstsToAppend.push_back(Load);
912         InstsToAppend.push_back(Switch);
913 
914         // Branch-after fallthrough.
915         if (FallthroughSource && !FallthroughIsBranchThrough) {
916           FallthroughDest = createBasicBlock("cleanup.cont");
917           if (HasFallthrough)
918             Switch->addCase(Builder.getInt32(0), FallthroughDest);
919         }
920 
921         for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
922           Switch->addCase(Scope.getBranchAfterIndex(I),
923                           Scope.getBranchAfterBlock(I));
924         }
925 
926         // If there aren't any enclosing cleanups, we can resolve all
927         // the fixups now.
928         if (HasFixups && !HasEnclosingCleanups)
929           ResolveAllBranchFixups(*this, Switch, NormalEntry);
930       } else {
931         // We should always have a branch-through destination in this case.
932         assert(BranchThroughDest);
933         InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
934       }
935 
936       // IV.  Pop the cleanup and emit it.
937       EHStack.popCleanup();
938       assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
939 
940       EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
941 
942       // Append the prepared cleanup prologue from above.
943       llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
944       for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
945         InstsToAppend[I]->insertInto(NormalExit, NormalExit->end());
946 
947       // Optimistically hope that any fixups will continue falling through.
948       for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
949            I < E; ++I) {
950         BranchFixup &Fixup = EHStack.getBranchFixup(I);
951         if (!Fixup.Destination) continue;
952         if (!Fixup.OptimisticBranchBlock) {
953           createStoreInstBefore(Builder.getInt32(Fixup.DestinationIndex),
954                                 getNormalCleanupDestSlot(),
955                                 Fixup.InitialBranch);
956           Fixup.InitialBranch->setSuccessor(0, NormalEntry);
957         }
958         Fixup.OptimisticBranchBlock = NormalExit;
959       }
960 
961       // V.  Set up the fallthrough edge out.
962 
963       // Case 1: a fallthrough source exists but doesn't branch to the
964       // cleanup because the cleanup is inactive.
965       if (!HasFallthrough && FallthroughSource) {
966         // Prebranched fallthrough was forwarded earlier.
967         // Non-prebranched fallthrough doesn't need to be forwarded.
968         // Either way, all we need to do is restore the IP we cleared before.
969         assert(!IsActive);
970         Builder.restoreIP(savedInactiveFallthroughIP);
971 
972       // Case 2: a fallthrough source exists and should branch to the
973       // cleanup, but we're not supposed to branch through to the next
974       // cleanup.
975       } else if (HasFallthrough && FallthroughDest) {
976         assert(!FallthroughIsBranchThrough);
977         EmitBlock(FallthroughDest);
978 
979       // Case 3: a fallthrough source exists and should branch to the
980       // cleanup and then through to the next.
981       } else if (HasFallthrough) {
982         // Everything is already set up for this.
983 
984       // Case 4: no fallthrough source exists.
985       } else {
986         Builder.ClearInsertionPoint();
987       }
988 
989       // VI.  Assorted cleaning.
990 
991       // Check whether we can merge NormalEntry into a single predecessor.
992       // This might invalidate (non-IR) pointers to NormalEntry.
993       llvm::BasicBlock *NewNormalEntry =
994         SimplifyCleanupEntry(*this, NormalEntry);
995 
996       // If it did invalidate those pointers, and NormalEntry was the same
997       // as NormalExit, go back and patch up the fixups.
998       if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
999         for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
1000                I < E; ++I)
1001           EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
1002     }
1003   }
1004 
1005   assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
1006 
1007   // Emit the EH cleanup if required.
1008   if (RequiresEHCleanup) {
1009     CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1010 
1011     EmitBlock(EHEntry);
1012 
1013     llvm::BasicBlock *NextAction = getEHDispatchBlock(EHParent);
1014 
1015     // Push a terminate scope or cleanupendpad scope around the potentially
1016     // throwing cleanups. For funclet EH personalities, the cleanupendpad models
1017     // program termination when cleanups throw.
1018     bool PushedTerminate = false;
1019     SaveAndRestore RestoreCurrentFuncletPad(CurrentFuncletPad);
1020     llvm::CleanupPadInst *CPI = nullptr;
1021 
1022     const EHPersonality &Personality = EHPersonality::get(*this);
1023     if (Personality.usesFuncletPads()) {
1024       llvm::Value *ParentPad = CurrentFuncletPad;
1025       if (!ParentPad)
1026         ParentPad = llvm::ConstantTokenNone::get(CGM.getLLVMContext());
1027       CurrentFuncletPad = CPI = Builder.CreateCleanupPad(ParentPad);
1028     }
1029 
1030     // Non-MSVC personalities need to terminate when an EH cleanup throws.
1031     if (!Personality.isMSVCPersonality()) {
1032       EHStack.pushTerminate();
1033       PushedTerminate = true;
1034     } else if (IsEHa && getInvokeDest()) {
1035       EmitSehCppScopeEnd();
1036     }
1037 
1038     // We only actually emit the cleanup code if the cleanup is either
1039     // active or was used before it was deactivated.
1040     if (EHActiveFlag.isValid() || IsActive) {
1041       cleanupFlags.setIsForEHCleanup();
1042       EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
1043     }
1044 
1045     if (CPI)
1046       Builder.CreateCleanupRet(CPI, NextAction);
1047     else
1048       Builder.CreateBr(NextAction);
1049 
1050     // Leave the terminate scope.
1051     if (PushedTerminate)
1052       EHStack.popTerminate();
1053 
1054     Builder.restoreIP(SavedIP);
1055 
1056     SimplifyCleanupEntry(*this, EHEntry);
1057   }
1058 }
1059 
1060 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
1061 /// specified destination obviously has no cleanups to run.  'false' is always
1062 /// a conservatively correct answer for this method.
1063 bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const {
1064   assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
1065          && "stale jump destination");
1066 
1067   // Calculate the innermost active normal cleanup.
1068   EHScopeStack::stable_iterator TopCleanup =
1069     EHStack.getInnermostActiveNormalCleanup();
1070 
1071   // If we're not in an active normal cleanup scope, or if the
1072   // destination scope is within the innermost active normal cleanup
1073   // scope, we don't need to worry about fixups.
1074   if (TopCleanup == EHStack.stable_end() ||
1075       TopCleanup.encloses(Dest.getScopeDepth())) // works for invalid
1076     return true;
1077 
1078   // Otherwise, we might need some cleanups.
1079   return false;
1080 }
1081 
1082 
1083 /// Terminate the current block by emitting a branch which might leave
1084 /// the current cleanup-protected scope.  The target scope may not yet
1085 /// be known, in which case this will require a fixup.
1086 ///
1087 /// As a side-effect, this method clears the insertion point.
1088 void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
1089   assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
1090          && "stale jump destination");
1091 
1092   if (!HaveInsertPoint())
1093     return;
1094 
1095   // Create the branch.
1096   llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
1097 
1098   // Calculate the innermost active normal cleanup.
1099   EHScopeStack::stable_iterator
1100     TopCleanup = EHStack.getInnermostActiveNormalCleanup();
1101 
1102   // If we're not in an active normal cleanup scope, or if the
1103   // destination scope is within the innermost active normal cleanup
1104   // scope, we don't need to worry about fixups.
1105   if (TopCleanup == EHStack.stable_end() ||
1106       TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
1107     Builder.ClearInsertionPoint();
1108     return;
1109   }
1110 
1111   // If we can't resolve the destination cleanup scope, just add this
1112   // to the current cleanup scope as a branch fixup.
1113   if (!Dest.getScopeDepth().isValid()) {
1114     BranchFixup &Fixup = EHStack.addBranchFixup();
1115     Fixup.Destination = Dest.getBlock();
1116     Fixup.DestinationIndex = Dest.getDestIndex();
1117     Fixup.InitialBranch = BI;
1118     Fixup.OptimisticBranchBlock = nullptr;
1119 
1120     Builder.ClearInsertionPoint();
1121     return;
1122   }
1123 
1124   // Otherwise, thread through all the normal cleanups in scope.
1125 
1126   // Store the index at the start.
1127   llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1128   createStoreInstBefore(Index, getNormalCleanupDestSlot(), BI);
1129 
1130   // Adjust BI to point to the first cleanup block.
1131   {
1132     EHCleanupScope &Scope =
1133       cast<EHCleanupScope>(*EHStack.find(TopCleanup));
1134     BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
1135   }
1136 
1137   // Add this destination to all the scopes involved.
1138   EHScopeStack::stable_iterator I = TopCleanup;
1139   EHScopeStack::stable_iterator E = Dest.getScopeDepth();
1140   if (E.strictlyEncloses(I)) {
1141     while (true) {
1142       EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1143       assert(Scope.isNormalCleanup());
1144       I = Scope.getEnclosingNormalCleanup();
1145 
1146       // If this is the last cleanup we're propagating through, tell it
1147       // that there's a resolved jump moving through it.
1148       if (!E.strictlyEncloses(I)) {
1149         Scope.addBranchAfter(Index, Dest.getBlock());
1150         break;
1151       }
1152 
1153       // Otherwise, tell the scope that there's a jump propagating
1154       // through it.  If this isn't new information, all the rest of
1155       // the work has been done before.
1156       if (!Scope.addBranchThrough(Dest.getBlock()))
1157         break;
1158     }
1159   }
1160 
1161   Builder.ClearInsertionPoint();
1162 }
1163 
1164 static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
1165                                   EHScopeStack::stable_iterator C) {
1166   // If we needed a normal block for any reason, that counts.
1167   if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
1168     return true;
1169 
1170   // Check whether any enclosed cleanups were needed.
1171   for (EHScopeStack::stable_iterator
1172          I = EHStack.getInnermostNormalCleanup();
1173          I != C; ) {
1174     assert(C.strictlyEncloses(I));
1175     EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1176     if (S.getNormalBlock()) return true;
1177     I = S.getEnclosingNormalCleanup();
1178   }
1179 
1180   return false;
1181 }
1182 
1183 static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
1184                               EHScopeStack::stable_iterator cleanup) {
1185   // If we needed an EH block for any reason, that counts.
1186   if (EHStack.find(cleanup)->hasEHBranches())
1187     return true;
1188 
1189   // Check whether any enclosed cleanups were needed.
1190   for (EHScopeStack::stable_iterator
1191          i = EHStack.getInnermostEHScope(); i != cleanup; ) {
1192     assert(cleanup.strictlyEncloses(i));
1193 
1194     EHScope &scope = *EHStack.find(i);
1195     if (scope.hasEHBranches())
1196       return true;
1197 
1198     i = scope.getEnclosingEHScope();
1199   }
1200 
1201   return false;
1202 }
1203 
1204 enum ForActivation_t {
1205   ForActivation,
1206   ForDeactivation
1207 };
1208 
1209 /// The given cleanup block is changing activation state.  Configure a
1210 /// cleanup variable if necessary.
1211 ///
1212 /// It would be good if we had some way of determining if there were
1213 /// extra uses *after* the change-over point.
1214 static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
1215                                         EHScopeStack::stable_iterator C,
1216                                         ForActivation_t kind,
1217                                         llvm::Instruction *dominatingIP) {
1218   EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
1219 
1220   // We always need the flag if we're activating the cleanup in a
1221   // conditional context, because we have to assume that the current
1222   // location doesn't necessarily dominate the cleanup's code.
1223   bool isActivatedInConditional =
1224     (kind == ForActivation && CGF.isInConditionalBranch());
1225 
1226   bool needFlag = false;
1227 
1228   // Calculate whether the cleanup was used:
1229 
1230   //   - as a normal cleanup
1231   if (Scope.isNormalCleanup() &&
1232       (isActivatedInConditional || IsUsedAsNormalCleanup(CGF.EHStack, C))) {
1233     Scope.setTestFlagInNormalCleanup();
1234     needFlag = true;
1235   }
1236 
1237   //  - as an EH cleanup
1238   if (Scope.isEHCleanup() &&
1239       (isActivatedInConditional || IsUsedAsEHCleanup(CGF.EHStack, C))) {
1240     Scope.setTestFlagInEHCleanup();
1241     needFlag = true;
1242   }
1243 
1244   // If it hasn't yet been used as either, we're done.
1245   if (!needFlag) return;
1246 
1247   Address var = Scope.getActiveFlag();
1248   if (!var.isValid()) {
1249     var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), CharUnits::One(),
1250                                "cleanup.isactive");
1251     Scope.setActiveFlag(var);
1252 
1253     assert(dominatingIP && "no existing variable and no dominating IP!");
1254 
1255     // Initialize to true or false depending on whether it was
1256     // active up to this point.
1257     llvm::Constant *value = CGF.Builder.getInt1(kind == ForDeactivation);
1258 
1259     // If we're in a conditional block, ignore the dominating IP and
1260     // use the outermost conditional branch.
1261     if (CGF.isInConditionalBranch()) {
1262       CGF.setBeforeOutermostConditional(value, var);
1263     } else {
1264       createStoreInstBefore(value, var, dominatingIP);
1265     }
1266   }
1267 
1268   CGF.Builder.CreateStore(CGF.Builder.getInt1(kind == ForActivation), var);
1269 }
1270 
1271 /// Activate a cleanup that was created in an inactivated state.
1272 void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C,
1273                                            llvm::Instruction *dominatingIP) {
1274   assert(C != EHStack.stable_end() && "activating bottom of stack?");
1275   EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1276   assert(!Scope.isActive() && "double activation");
1277 
1278   SetupCleanupBlockActivation(*this, C, ForActivation, dominatingIP);
1279 
1280   Scope.setActive(true);
1281 }
1282 
1283 /// Deactive a cleanup that was created in an active state.
1284 void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C,
1285                                              llvm::Instruction *dominatingIP) {
1286   assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
1287   EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1288   assert(Scope.isActive() && "double deactivation");
1289 
1290   // If it's the top of the stack, just pop it, but do so only if it belongs
1291   // to the current RunCleanupsScope.
1292   if (C == EHStack.stable_begin() &&
1293       CurrentCleanupScopeDepth.strictlyEncloses(C)) {
1294     // Per comment below, checking EHAsynch is not really necessary
1295     // it's there to assure zero-impact w/o EHAsynch option
1296     if (!Scope.isNormalCleanup() && getLangOpts().EHAsynch) {
1297       PopCleanupBlock();
1298     } else {
1299       // If it's a normal cleanup, we need to pretend that the
1300       // fallthrough is unreachable.
1301       CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1302       PopCleanupBlock();
1303       Builder.restoreIP(SavedIP);
1304     }
1305     return;
1306   }
1307 
1308   // Otherwise, follow the general case.
1309   SetupCleanupBlockActivation(*this, C, ForDeactivation, dominatingIP);
1310 
1311   Scope.setActive(false);
1312 }
1313 
1314 Address CodeGenFunction::getNormalCleanupDestSlot() {
1315   if (!NormalCleanupDest.isValid())
1316     NormalCleanupDest =
1317       CreateDefaultAlignTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
1318   return NormalCleanupDest;
1319 }
1320 
1321 /// Emits all the code to cause the given temporary to be cleaned up.
1322 void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary,
1323                                        QualType TempType,
1324                                        Address Ptr) {
1325   pushDestroy(NormalAndEHCleanup, Ptr, TempType, destroyCXXObject,
1326               /*useEHCleanup*/ true);
1327 }
1328 
1329 // Need to set "funclet" in OperandBundle properly for noThrow
1330 //       intrinsic (see CGCall.cpp)
1331 static void EmitSehScope(CodeGenFunction &CGF,
1332                          llvm::FunctionCallee &SehCppScope) {
1333   llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
1334   assert(CGF.Builder.GetInsertBlock() && InvokeDest);
1335   llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
1336   SmallVector<llvm::OperandBundleDef, 1> BundleList =
1337       CGF.getBundlesForFunclet(SehCppScope.getCallee());
1338   if (CGF.CurrentFuncletPad)
1339     BundleList.emplace_back("funclet", CGF.CurrentFuncletPad);
1340   CGF.Builder.CreateInvoke(SehCppScope, Cont, InvokeDest, std::nullopt,
1341                            BundleList);
1342   CGF.EmitBlock(Cont);
1343 }
1344 
1345 // Invoke a llvm.seh.scope.begin at the beginning of a CPP scope for -EHa
1346 void CodeGenFunction::EmitSehCppScopeBegin() {
1347   assert(getLangOpts().EHAsynch);
1348   llvm::FunctionType *FTy =
1349       llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1350   llvm::FunctionCallee SehCppScope =
1351       CGM.CreateRuntimeFunction(FTy, "llvm.seh.scope.begin");
1352   EmitSehScope(*this, SehCppScope);
1353 }
1354 
1355 // Invoke a llvm.seh.scope.end at the end of a CPP scope for -EHa
1356 //   llvm.seh.scope.end is emitted before popCleanup, so it's "invoked"
1357 void CodeGenFunction::EmitSehCppScopeEnd() {
1358   assert(getLangOpts().EHAsynch);
1359   llvm::FunctionType *FTy =
1360       llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1361   llvm::FunctionCallee SehCppScope =
1362       CGM.CreateRuntimeFunction(FTy, "llvm.seh.scope.end");
1363   EmitSehScope(*this, SehCppScope);
1364 }
1365 
1366 // Invoke a llvm.seh.try.begin at the beginning of a SEH scope for -EHa
1367 void CodeGenFunction::EmitSehTryScopeBegin() {
1368   assert(getLangOpts().EHAsynch);
1369   llvm::FunctionType *FTy =
1370       llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1371   llvm::FunctionCallee SehCppScope =
1372       CGM.CreateRuntimeFunction(FTy, "llvm.seh.try.begin");
1373   EmitSehScope(*this, SehCppScope);
1374 }
1375 
1376 // Invoke a llvm.seh.try.end at the end of a SEH scope for -EHa
1377 void CodeGenFunction::EmitSehTryScopeEnd() {
1378   assert(getLangOpts().EHAsynch);
1379   llvm::FunctionType *FTy =
1380       llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1381   llvm::FunctionCallee SehCppScope =
1382       CGM.CreateRuntimeFunction(FTy, "llvm.seh.try.end");
1383   EmitSehScope(*this, SehCppScope);
1384 }
1385