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