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