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