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