clang API Documentation

CGCleanup.cpp
Go to the documentation of this file.
00001 //===--- CGCleanup.cpp - Bookkeeping and code emission for cleanups -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains code dealing with the IR generation for cleanups
00011 // and related information.
00012 //
00013 // A "cleanup" is a piece of code which needs to be executed whenever
00014 // control transfers out of a particular scope.  This can be
00015 // conditionalized to occur only on exceptional control flow, only on
00016 // normal control flow, or both.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #include "CodeGenFunction.h"
00021 #include "CGCleanup.h"
00022 
00023 using namespace clang;
00024 using namespace CodeGen;
00025 
00026 bool DominatingValue<RValue>::saved_type::needsSaving(RValue rv) {
00027   if (rv.isScalar())
00028     return DominatingLLVMValue::needsSaving(rv.getScalarVal());
00029   if (rv.isAggregate())
00030     return DominatingLLVMValue::needsSaving(rv.getAggregateAddr());
00031   return true;
00032 }
00033 
00034 DominatingValue<RValue>::saved_type
00035 DominatingValue<RValue>::saved_type::save(CodeGenFunction &CGF, RValue rv) {
00036   if (rv.isScalar()) {
00037     llvm::Value *V = rv.getScalarVal();
00038 
00039     // These automatically dominate and don't need to be saved.
00040     if (!DominatingLLVMValue::needsSaving(V))
00041       return saved_type(V, ScalarLiteral);
00042 
00043     // Everything else needs an alloca.
00044     llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
00045     CGF.Builder.CreateStore(V, addr);
00046     return saved_type(addr, ScalarAddress);
00047   }
00048 
00049   if (rv.isComplex()) {
00050     CodeGenFunction::ComplexPairTy V = rv.getComplexVal();
00051     llvm::Type *ComplexTy =
00052       llvm::StructType::get(V.first->getType(), V.second->getType(),
00053                             (void*) 0);
00054     llvm::Value *addr = CGF.CreateTempAlloca(ComplexTy, "saved-complex");
00055     CGF.StoreComplexToAddr(V, addr, /*volatile*/ false);
00056     return saved_type(addr, ComplexAddress);
00057   }
00058 
00059   assert(rv.isAggregate());
00060   llvm::Value *V = rv.getAggregateAddr(); // TODO: volatile?
00061   if (!DominatingLLVMValue::needsSaving(V))
00062     return saved_type(V, AggregateLiteral);
00063 
00064   llvm::Value *addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
00065   CGF.Builder.CreateStore(V, addr);
00066   return saved_type(addr, AggregateAddress);  
00067 }
00068 
00069 /// Given a saved r-value produced by SaveRValue, perform the code
00070 /// necessary to restore it to usability at the current insertion
00071 /// point.
00072 RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
00073   switch (K) {
00074   case ScalarLiteral:
00075     return RValue::get(Value);
00076   case ScalarAddress:
00077     return RValue::get(CGF.Builder.CreateLoad(Value));
00078   case AggregateLiteral:
00079     return RValue::getAggregate(Value);
00080   case AggregateAddress:
00081     return RValue::getAggregate(CGF.Builder.CreateLoad(Value));
00082   case ComplexAddress:
00083     return RValue::getComplex(CGF.LoadComplexFromAddr(Value, false));
00084   }
00085 
00086   llvm_unreachable("bad saved r-value kind");
00087 }
00088 
00089 /// Push an entry of the given size onto this protected-scope stack.
00090 char *EHScopeStack::allocate(size_t Size) {
00091   if (!StartOfBuffer) {
00092     unsigned Capacity = 1024;
00093     while (Capacity < Size) Capacity *= 2;
00094     StartOfBuffer = new char[Capacity];
00095     StartOfData = EndOfBuffer = StartOfBuffer + Capacity;
00096   } else if (static_cast<size_t>(StartOfData - StartOfBuffer) < Size) {
00097     unsigned CurrentCapacity = EndOfBuffer - StartOfBuffer;
00098     unsigned UsedCapacity = CurrentCapacity - (StartOfData - StartOfBuffer);
00099 
00100     unsigned NewCapacity = CurrentCapacity;
00101     do {
00102       NewCapacity *= 2;
00103     } while (NewCapacity < UsedCapacity + Size);
00104 
00105     char *NewStartOfBuffer = new char[NewCapacity];
00106     char *NewEndOfBuffer = NewStartOfBuffer + NewCapacity;
00107     char *NewStartOfData = NewEndOfBuffer - UsedCapacity;
00108     memcpy(NewStartOfData, StartOfData, UsedCapacity);
00109     delete [] StartOfBuffer;
00110     StartOfBuffer = NewStartOfBuffer;
00111     EndOfBuffer = NewEndOfBuffer;
00112     StartOfData = NewStartOfData;
00113   }
00114 
00115   assert(StartOfBuffer + Size <= StartOfData);
00116   StartOfData -= Size;
00117   return StartOfData;
00118 }
00119 
00120 EHScopeStack::stable_iterator
00121 EHScopeStack::getInnermostActiveNormalCleanup() const {
00122   for (stable_iterator si = getInnermostNormalCleanup(), se = stable_end();
00123          si != se; ) {
00124     EHCleanupScope &cleanup = cast<EHCleanupScope>(*find(si));
00125     if (cleanup.isActive()) return si;
00126     si = cleanup.getEnclosingNormalCleanup();
00127   }
00128   return stable_end();
00129 }
00130 
00131 EHScopeStack::stable_iterator EHScopeStack::getInnermostActiveEHScope() const {
00132   for (stable_iterator si = getInnermostEHScope(), se = stable_end();
00133          si != se; ) {
00134     // Skip over inactive cleanups.
00135     EHCleanupScope *cleanup = dyn_cast<EHCleanupScope>(&*find(si));
00136     if (cleanup && !cleanup->isActive()) {
00137       si = cleanup->getEnclosingEHScope();
00138       continue;
00139     }
00140 
00141     // All other scopes are always active.
00142     return si;
00143   }
00144 
00145   return stable_end();
00146 }
00147 
00148 
00149 void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
00150   assert(((Size % sizeof(void*)) == 0) && "cleanup type is misaligned");
00151   char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
00152   bool IsNormalCleanup = Kind & NormalCleanup;
00153   bool IsEHCleanup = Kind & EHCleanup;
00154   bool IsActive = !(Kind & InactiveCleanup);
00155   EHCleanupScope *Scope =
00156     new (Buffer) EHCleanupScope(IsNormalCleanup,
00157                                 IsEHCleanup,
00158                                 IsActive,
00159                                 Size,
00160                                 BranchFixups.size(),
00161                                 InnermostNormalCleanup,
00162                                 InnermostEHScope);
00163   if (IsNormalCleanup)
00164     InnermostNormalCleanup = stable_begin();
00165   if (IsEHCleanup)
00166     InnermostEHScope = stable_begin();
00167 
00168   return Scope->getCleanupBuffer();
00169 }
00170 
00171 void EHScopeStack::popCleanup() {
00172   assert(!empty() && "popping exception stack when not empty");
00173 
00174   assert(isa<EHCleanupScope>(*begin()));
00175   EHCleanupScope &Cleanup = cast<EHCleanupScope>(*begin());
00176   InnermostNormalCleanup = Cleanup.getEnclosingNormalCleanup();
00177   InnermostEHScope = Cleanup.getEnclosingEHScope();
00178   StartOfData += Cleanup.getAllocatedSize();
00179 
00180   // Destroy the cleanup.
00181   Cleanup.~EHCleanupScope();
00182 
00183   // Check whether we can shrink the branch-fixups stack.
00184   if (!BranchFixups.empty()) {
00185     // If we no longer have any normal cleanups, all the fixups are
00186     // complete.
00187     if (!hasNormalCleanups())
00188       BranchFixups.clear();
00189 
00190     // Otherwise we can still trim out unnecessary nulls.
00191     else
00192       popNullFixups();
00193   }
00194 }
00195 
00196 EHFilterScope *EHScopeStack::pushFilter(unsigned numFilters) {
00197   assert(getInnermostEHScope() == stable_end());
00198   char *buffer = allocate(EHFilterScope::getSizeForNumFilters(numFilters));
00199   EHFilterScope *filter = new (buffer) EHFilterScope(numFilters);
00200   InnermostEHScope = stable_begin();
00201   return filter;
00202 }
00203 
00204 void EHScopeStack::popFilter() {
00205   assert(!empty() && "popping exception stack when not empty");
00206 
00207   EHFilterScope &filter = cast<EHFilterScope>(*begin());
00208   StartOfData += EHFilterScope::getSizeForNumFilters(filter.getNumFilters());
00209 
00210   InnermostEHScope = filter.getEnclosingEHScope();
00211 }
00212 
00213 EHCatchScope *EHScopeStack::pushCatch(unsigned numHandlers) {
00214   char *buffer = allocate(EHCatchScope::getSizeForNumHandlers(numHandlers));
00215   EHCatchScope *scope =
00216     new (buffer) EHCatchScope(numHandlers, InnermostEHScope);
00217   InnermostEHScope = stable_begin();
00218   return scope;
00219 }
00220 
00221 void EHScopeStack::pushTerminate() {
00222   char *Buffer = allocate(EHTerminateScope::getSize());
00223   new (Buffer) EHTerminateScope(InnermostEHScope);
00224   InnermostEHScope = stable_begin();
00225 }
00226 
00227 /// Remove any 'null' fixups on the stack.  However, we can't pop more
00228 /// fixups than the fixup depth on the innermost normal cleanup, or
00229 /// else fixups that we try to add to that cleanup will end up in the
00230 /// wrong place.  We *could* try to shrink fixup depths, but that's
00231 /// actually a lot of work for little benefit.
00232 void EHScopeStack::popNullFixups() {
00233   // We expect this to only be called when there's still an innermost
00234   // normal cleanup;  otherwise there really shouldn't be any fixups.
00235   assert(hasNormalCleanups());
00236 
00237   EHScopeStack::iterator it = find(InnermostNormalCleanup);
00238   unsigned MinSize = cast<EHCleanupScope>(*it).getFixupDepth();
00239   assert(BranchFixups.size() >= MinSize && "fixup stack out of order");
00240 
00241   while (BranchFixups.size() > MinSize &&
00242          BranchFixups.back().Destination == 0)
00243     BranchFixups.pop_back();
00244 }
00245 
00246 void CodeGenFunction::initFullExprCleanup() {
00247   // Create a variable to decide whether the cleanup needs to be run.
00248   llvm::AllocaInst *active
00249     = CreateTempAlloca(Builder.getInt1Ty(), "cleanup.cond");
00250 
00251   // Initialize it to false at a site that's guaranteed to be run
00252   // before each evaluation.
00253   setBeforeOutermostConditional(Builder.getFalse(), active);
00254 
00255   // Initialize it to true at the current location.
00256   Builder.CreateStore(Builder.getTrue(), active);
00257 
00258   // Set that as the active flag in the cleanup.
00259   EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
00260   assert(cleanup.getActiveFlag() == 0 && "cleanup already has active flag?");
00261   cleanup.setActiveFlag(active);
00262 
00263   if (cleanup.isNormalCleanup()) cleanup.setTestFlagInNormalCleanup();
00264   if (cleanup.isEHCleanup()) cleanup.setTestFlagInEHCleanup();
00265 }
00266 
00267 void EHScopeStack::Cleanup::anchor() {}
00268 
00269 /// All the branch fixups on the EH stack have propagated out past the
00270 /// outermost normal cleanup; resolve them all by adding cases to the
00271 /// given switch instruction.
00272 static void ResolveAllBranchFixups(CodeGenFunction &CGF,
00273                                    llvm::SwitchInst *Switch,
00274                                    llvm::BasicBlock *CleanupEntry) {
00275   llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
00276 
00277   for (unsigned I = 0, E = CGF.EHStack.getNumBranchFixups(); I != E; ++I) {
00278     // Skip this fixup if its destination isn't set.
00279     BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
00280     if (Fixup.Destination == 0) continue;
00281 
00282     // If there isn't an OptimisticBranchBlock, then InitialBranch is
00283     // still pointing directly to its destination; forward it to the
00284     // appropriate cleanup entry.  This is required in the specific
00285     // case of
00286     //   { std::string s; goto lbl; }
00287     //   lbl:
00288     // i.e. where there's an unresolved fixup inside a single cleanup
00289     // entry which we're currently popping.
00290     if (Fixup.OptimisticBranchBlock == 0) {
00291       new llvm::StoreInst(CGF.Builder.getInt32(Fixup.DestinationIndex),
00292                           CGF.getNormalCleanupDestSlot(),
00293                           Fixup.InitialBranch);
00294       Fixup.InitialBranch->setSuccessor(0, CleanupEntry);
00295     }
00296 
00297     // Don't add this case to the switch statement twice.
00298     if (!CasesAdded.insert(Fixup.Destination)) continue;
00299 
00300     Switch->addCase(CGF.Builder.getInt32(Fixup.DestinationIndex),
00301                     Fixup.Destination);
00302   }
00303 
00304   CGF.EHStack.clearFixups();
00305 }
00306 
00307 /// Transitions the terminator of the given exit-block of a cleanup to
00308 /// be a cleanup switch.
00309 static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
00310                                                    llvm::BasicBlock *Block) {
00311   // If it's a branch, turn it into a switch whose default
00312   // destination is its original target.
00313   llvm::TerminatorInst *Term = Block->getTerminator();
00314   assert(Term && "can't transition block without terminator");
00315 
00316   if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
00317     assert(Br->isUnconditional());
00318     llvm::LoadInst *Load =
00319       new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term);
00320     llvm::SwitchInst *Switch =
00321       llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
00322     Br->eraseFromParent();
00323     return Switch;
00324   } else {
00325     return cast<llvm::SwitchInst>(Term);
00326   }
00327 }
00328 
00329 void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
00330   assert(Block && "resolving a null target block");
00331   if (!EHStack.getNumBranchFixups()) return;
00332 
00333   assert(EHStack.hasNormalCleanups() &&
00334          "branch fixups exist with no normal cleanups on stack");
00335 
00336   llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
00337   bool ResolvedAny = false;
00338 
00339   for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
00340     // Skip this fixup if its destination doesn't match.
00341     BranchFixup &Fixup = EHStack.getBranchFixup(I);
00342     if (Fixup.Destination != Block) continue;
00343 
00344     Fixup.Destination = 0;
00345     ResolvedAny = true;
00346 
00347     // If it doesn't have an optimistic branch block, LatestBranch is
00348     // already pointing to the right place.
00349     llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
00350     if (!BranchBB)
00351       continue;
00352 
00353     // Don't process the same optimistic branch block twice.
00354     if (!ModifiedOptimisticBlocks.insert(BranchBB))
00355       continue;
00356 
00357     llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
00358 
00359     // Add a case to the switch.
00360     Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
00361   }
00362 
00363   if (ResolvedAny)
00364     EHStack.popNullFixups();
00365 }
00366 
00367 /// Pops cleanup blocks until the given savepoint is reached.
00368 void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) {
00369   assert(Old.isValid());
00370 
00371   while (EHStack.stable_begin() != Old) {
00372     EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
00373 
00374     // As long as Old strictly encloses the scope's enclosing normal
00375     // cleanup, we're going to emit another normal cleanup which
00376     // fallthrough can propagate through.
00377     bool FallThroughIsBranchThrough =
00378       Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
00379 
00380     PopCleanupBlock(FallThroughIsBranchThrough);
00381   }
00382 }
00383 
00384 static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
00385                                            EHCleanupScope &Scope) {
00386   assert(Scope.isNormalCleanup());
00387   llvm::BasicBlock *Entry = Scope.getNormalBlock();
00388   if (!Entry) {
00389     Entry = CGF.createBasicBlock("cleanup");
00390     Scope.setNormalBlock(Entry);
00391   }
00392   return Entry;
00393 }
00394 
00395 /// Attempts to reduce a cleanup's entry block to a fallthrough.  This
00396 /// is basically llvm::MergeBlockIntoPredecessor, except
00397 /// simplified/optimized for the tighter constraints on cleanup blocks.
00398 ///
00399 /// Returns the new block, whatever it is.
00400 static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
00401                                               llvm::BasicBlock *Entry) {
00402   llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
00403   if (!Pred) return Entry;
00404 
00405   llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
00406   if (!Br || Br->isConditional()) return Entry;
00407   assert(Br->getSuccessor(0) == Entry);
00408 
00409   // If we were previously inserting at the end of the cleanup entry
00410   // block, we'll need to continue inserting at the end of the
00411   // predecessor.
00412   bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
00413   assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
00414 
00415   // Kill the branch.
00416   Br->eraseFromParent();
00417 
00418   // Replace all uses of the entry with the predecessor, in case there
00419   // are phis in the cleanup.
00420   Entry->replaceAllUsesWith(Pred);
00421 
00422   // Merge the blocks.
00423   Pred->getInstList().splice(Pred->end(), Entry->getInstList());
00424 
00425   // Kill the entry block.
00426   Entry->eraseFromParent();
00427 
00428   if (WasInsertBlock)
00429     CGF.Builder.SetInsertPoint(Pred);
00430 
00431   return Pred;
00432 }
00433 
00434 static void EmitCleanup(CodeGenFunction &CGF,
00435                         EHScopeStack::Cleanup *Fn,
00436                         EHScopeStack::Cleanup::Flags flags,
00437                         llvm::Value *ActiveFlag) {
00438   // EH cleanups always occur within a terminate scope.
00439   if (flags.isForEHCleanup()) CGF.EHStack.pushTerminate();
00440 
00441   // If there's an active flag, load it and skip the cleanup if it's
00442   // false.
00443   llvm::BasicBlock *ContBB = 0;
00444   if (ActiveFlag) {
00445     ContBB = CGF.createBasicBlock("cleanup.done");
00446     llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
00447     llvm::Value *IsActive
00448       = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
00449     CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
00450     CGF.EmitBlock(CleanupBB);
00451   }
00452 
00453   // Ask the cleanup to emit itself.
00454   Fn->Emit(CGF, flags);
00455   assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
00456 
00457   // Emit the continuation block if there was an active flag.
00458   if (ActiveFlag)
00459     CGF.EmitBlock(ContBB);
00460 
00461   // Leave the terminate scope.
00462   if (flags.isForEHCleanup()) CGF.EHStack.popTerminate();
00463 }
00464 
00465 static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
00466                                           llvm::BasicBlock *From,
00467                                           llvm::BasicBlock *To) {
00468   // Exit is the exit block of a cleanup, so it always terminates in
00469   // an unconditional branch or a switch.
00470   llvm::TerminatorInst *Term = Exit->getTerminator();
00471 
00472   if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
00473     assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
00474     Br->setSuccessor(0, To);
00475   } else {
00476     llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
00477     for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
00478       if (Switch->getSuccessor(I) == From)
00479         Switch->setSuccessor(I, To);
00480   }
00481 }
00482 
00483 /// We don't need a normal entry block for the given cleanup.
00484 /// Optimistic fixup branches can cause these blocks to come into
00485 /// existence anyway;  if so, destroy it.
00486 ///
00487 /// The validity of this transformation is very much specific to the
00488 /// exact ways in which we form branches to cleanup entries.
00489 static void destroyOptimisticNormalEntry(CodeGenFunction &CGF,
00490                                          EHCleanupScope &scope) {
00491   llvm::BasicBlock *entry = scope.getNormalBlock();
00492   if (!entry) return;
00493 
00494   // Replace all the uses with unreachable.
00495   llvm::BasicBlock *unreachableBB = CGF.getUnreachableBlock();
00496   for (llvm::BasicBlock::use_iterator
00497          i = entry->use_begin(), e = entry->use_end(); i != e; ) {
00498     llvm::Use &use = i.getUse();
00499     ++i;
00500 
00501     use.set(unreachableBB);
00502     
00503     // The only uses should be fixup switches.
00504     llvm::SwitchInst *si = cast<llvm::SwitchInst>(use.getUser());
00505     if (si->getNumCases() == 1 && si->getDefaultDest() == unreachableBB) {
00506       // Replace the switch with a branch.
00507       llvm::BranchInst::Create(si->case_begin().getCaseSuccessor(), si);
00508 
00509       // The switch operand is a load from the cleanup-dest alloca.
00510       llvm::LoadInst *condition = cast<llvm::LoadInst>(si->getCondition());
00511 
00512       // Destroy the switch.
00513       si->eraseFromParent();
00514 
00515       // Destroy the load.
00516       assert(condition->getOperand(0) == CGF.NormalCleanupDest);
00517       assert(condition->use_empty());
00518       condition->eraseFromParent();
00519     }
00520   }
00521   
00522   assert(entry->use_empty());
00523   delete entry;
00524 }
00525 
00526 /// Pops a cleanup block.  If the block includes a normal cleanup, the
00527 /// current insertion point is threaded through the cleanup, as are
00528 /// any branch fixups on the cleanup.
00529 void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
00530   assert(!EHStack.empty() && "cleanup stack is empty!");
00531   assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
00532   EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
00533   assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
00534 
00535   // Remember activation information.
00536   bool IsActive = Scope.isActive();
00537   llvm::Value *NormalActiveFlag =
00538     Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag() : 0;
00539   llvm::Value *EHActiveFlag = 
00540     Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag() : 0;
00541 
00542   // Check whether we need an EH cleanup.  This is only true if we've
00543   // generated a lazy EH cleanup block.
00544   llvm::BasicBlock *EHEntry = Scope.getCachedEHDispatchBlock();
00545   assert(Scope.hasEHBranches() == (EHEntry != 0));
00546   bool RequiresEHCleanup = (EHEntry != 0);
00547   EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope();
00548 
00549   // Check the three conditions which might require a normal cleanup:
00550 
00551   // - whether there are branch fix-ups through this cleanup
00552   unsigned FixupDepth = Scope.getFixupDepth();
00553   bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
00554 
00555   // - whether there are branch-throughs or branch-afters
00556   bool HasExistingBranches = Scope.hasBranches();
00557 
00558   // - whether there's a fallthrough
00559   llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
00560   bool HasFallthrough = (FallthroughSource != 0 && IsActive);
00561 
00562   // Branch-through fall-throughs leave the insertion point set to the
00563   // end of the last cleanup, which points to the current scope.  The
00564   // rest of IR gen doesn't need to worry about this; it only happens
00565   // during the execution of PopCleanupBlocks().
00566   bool HasPrebranchedFallthrough =
00567     (FallthroughSource && FallthroughSource->getTerminator());
00568 
00569   // If this is a normal cleanup, then having a prebranched
00570   // fallthrough implies that the fallthrough source unconditionally
00571   // jumps here.
00572   assert(!Scope.isNormalCleanup() || !HasPrebranchedFallthrough ||
00573          (Scope.getNormalBlock() &&
00574           FallthroughSource->getTerminator()->getSuccessor(0)
00575             == Scope.getNormalBlock()));
00576 
00577   bool RequiresNormalCleanup = false;
00578   if (Scope.isNormalCleanup() &&
00579       (HasFixups || HasExistingBranches || HasFallthrough)) {
00580     RequiresNormalCleanup = true;
00581   }
00582 
00583   // If we have a prebranched fallthrough into an inactive normal
00584   // cleanup, rewrite it so that it leads to the appropriate place.
00585   if (Scope.isNormalCleanup() && HasPrebranchedFallthrough && !IsActive) {
00586     llvm::BasicBlock *prebranchDest;
00587     
00588     // If the prebranch is semantically branching through the next
00589     // cleanup, just forward it to the next block, leaving the
00590     // insertion point in the prebranched block.
00591     if (FallthroughIsBranchThrough) {
00592       EHScope &enclosing = *EHStack.find(Scope.getEnclosingNormalCleanup());
00593       prebranchDest = CreateNormalEntry(*this, cast<EHCleanupScope>(enclosing));
00594 
00595     // Otherwise, we need to make a new block.  If the normal cleanup
00596     // isn't being used at all, we could actually reuse the normal
00597     // entry block, but this is simpler, and it avoids conflicts with
00598     // dead optimistic fixup branches.
00599     } else {
00600       prebranchDest = createBasicBlock("forwarded-prebranch");
00601       EmitBlock(prebranchDest);
00602     }
00603 
00604     llvm::BasicBlock *normalEntry = Scope.getNormalBlock();
00605     assert(normalEntry && !normalEntry->use_empty());
00606 
00607     ForwardPrebranchedFallthrough(FallthroughSource,
00608                                   normalEntry, prebranchDest);
00609   }
00610 
00611   // If we don't need the cleanup at all, we're done.
00612   if (!RequiresNormalCleanup && !RequiresEHCleanup) {
00613     destroyOptimisticNormalEntry(*this, Scope);
00614     EHStack.popCleanup(); // safe because there are no fixups
00615     assert(EHStack.getNumBranchFixups() == 0 ||
00616            EHStack.hasNormalCleanups());
00617     return;
00618   }
00619 
00620   // Copy the cleanup emission data out.  Note that SmallVector
00621   // guarantees maximal alignment for its buffer regardless of its
00622   // type parameter.
00623   SmallVector<char, 8*sizeof(void*)> CleanupBuffer;
00624   CleanupBuffer.reserve(Scope.getCleanupSize());
00625   memcpy(CleanupBuffer.data(),
00626          Scope.getCleanupBuffer(), Scope.getCleanupSize());
00627   CleanupBuffer.set_size(Scope.getCleanupSize());
00628   EHScopeStack::Cleanup *Fn =
00629     reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data());
00630 
00631   EHScopeStack::Cleanup::Flags cleanupFlags;
00632   if (Scope.isNormalCleanup())
00633     cleanupFlags.setIsNormalCleanupKind();
00634   if (Scope.isEHCleanup())
00635     cleanupFlags.setIsEHCleanupKind();
00636 
00637   if (!RequiresNormalCleanup) {
00638     destroyOptimisticNormalEntry(*this, Scope);
00639     EHStack.popCleanup();
00640   } else {
00641     // If we have a fallthrough and no other need for the cleanup,
00642     // emit it directly.
00643     if (HasFallthrough && !HasPrebranchedFallthrough &&
00644         !HasFixups && !HasExistingBranches) {
00645 
00646       destroyOptimisticNormalEntry(*this, Scope);
00647       EHStack.popCleanup();
00648 
00649       EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
00650 
00651     // Otherwise, the best approach is to thread everything through
00652     // the cleanup block and then try to clean up after ourselves.
00653     } else {
00654       // Force the entry block to exist.
00655       llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
00656 
00657       // I.  Set up the fallthrough edge in.
00658 
00659       CGBuilderTy::InsertPoint savedInactiveFallthroughIP;
00660 
00661       // If there's a fallthrough, we need to store the cleanup
00662       // destination index.  For fall-throughs this is always zero.
00663       if (HasFallthrough) {
00664         if (!HasPrebranchedFallthrough)
00665           Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
00666 
00667       // Otherwise, save and clear the IP if we don't have fallthrough
00668       // because the cleanup is inactive.
00669       } else if (FallthroughSource) {
00670         assert(!IsActive && "source without fallthrough for active cleanup");
00671         savedInactiveFallthroughIP = Builder.saveAndClearIP();
00672       }
00673 
00674       // II.  Emit the entry block.  This implicitly branches to it if
00675       // we have fallthrough.  All the fixups and existing branches
00676       // should already be branched to it.
00677       EmitBlock(NormalEntry);
00678 
00679       // III.  Figure out where we're going and build the cleanup
00680       // epilogue.
00681 
00682       bool HasEnclosingCleanups =
00683         (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
00684 
00685       // Compute the branch-through dest if we need it:
00686       //   - if there are branch-throughs threaded through the scope
00687       //   - if fall-through is a branch-through
00688       //   - if there are fixups that will be optimistically forwarded
00689       //     to the enclosing cleanup
00690       llvm::BasicBlock *BranchThroughDest = 0;
00691       if (Scope.hasBranchThroughs() ||
00692           (FallthroughSource && FallthroughIsBranchThrough) ||
00693           (HasFixups && HasEnclosingCleanups)) {
00694         assert(HasEnclosingCleanups);
00695         EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
00696         BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
00697       }
00698 
00699       llvm::BasicBlock *FallthroughDest = 0;
00700       SmallVector<llvm::Instruction*, 2> InstsToAppend;
00701 
00702       // If there's exactly one branch-after and no other threads,
00703       // we can route it without a switch.
00704       if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
00705           Scope.getNumBranchAfters() == 1) {
00706         assert(!BranchThroughDest || !IsActive);
00707 
00708         // TODO: clean up the possibly dead stores to the cleanup dest slot.
00709         llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
00710         InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
00711 
00712       // Build a switch-out if we need it:
00713       //   - if there are branch-afters threaded through the scope
00714       //   - if fall-through is a branch-after
00715       //   - if there are fixups that have nowhere left to go and
00716       //     so must be immediately resolved
00717       } else if (Scope.getNumBranchAfters() ||
00718                  (HasFallthrough && !FallthroughIsBranchThrough) ||
00719                  (HasFixups && !HasEnclosingCleanups)) {
00720 
00721         llvm::BasicBlock *Default =
00722           (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
00723 
00724         // TODO: base this on the number of branch-afters and fixups
00725         const unsigned SwitchCapacity = 10;
00726 
00727         llvm::LoadInst *Load =
00728           new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest");
00729         llvm::SwitchInst *Switch =
00730           llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
00731 
00732         InstsToAppend.push_back(Load);
00733         InstsToAppend.push_back(Switch);
00734 
00735         // Branch-after fallthrough.
00736         if (FallthroughSource && !FallthroughIsBranchThrough) {
00737           FallthroughDest = createBasicBlock("cleanup.cont");
00738           if (HasFallthrough)
00739             Switch->addCase(Builder.getInt32(0), FallthroughDest);
00740         }
00741 
00742         for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
00743           Switch->addCase(Scope.getBranchAfterIndex(I),
00744                           Scope.getBranchAfterBlock(I));
00745         }
00746 
00747         // If there aren't any enclosing cleanups, we can resolve all
00748         // the fixups now.
00749         if (HasFixups && !HasEnclosingCleanups)
00750           ResolveAllBranchFixups(*this, Switch, NormalEntry);
00751       } else {
00752         // We should always have a branch-through destination in this case.
00753         assert(BranchThroughDest);
00754         InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
00755       }
00756 
00757       // IV.  Pop the cleanup and emit it.
00758       EHStack.popCleanup();
00759       assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
00760 
00761       EmitCleanup(*this, Fn, cleanupFlags, NormalActiveFlag);
00762 
00763       // Append the prepared cleanup prologue from above.
00764       llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
00765       for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
00766         NormalExit->getInstList().push_back(InstsToAppend[I]);
00767 
00768       // Optimistically hope that any fixups will continue falling through.
00769       for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
00770            I < E; ++I) {
00771         BranchFixup &Fixup = EHStack.getBranchFixup(I);
00772         if (!Fixup.Destination) continue;
00773         if (!Fixup.OptimisticBranchBlock) {
00774           new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex),
00775                               getNormalCleanupDestSlot(),
00776                               Fixup.InitialBranch);
00777           Fixup.InitialBranch->setSuccessor(0, NormalEntry);
00778         }
00779         Fixup.OptimisticBranchBlock = NormalExit;
00780       }
00781 
00782       // V.  Set up the fallthrough edge out.
00783       
00784       // Case 1: a fallthrough source exists but doesn't branch to the
00785       // cleanup because the cleanup is inactive.
00786       if (!HasFallthrough && FallthroughSource) {
00787         // Prebranched fallthrough was forwarded earlier.
00788         // Non-prebranched fallthrough doesn't need to be forwarded.
00789         // Either way, all we need to do is restore the IP we cleared before.
00790         assert(!IsActive);
00791         Builder.restoreIP(savedInactiveFallthroughIP);
00792 
00793       // Case 2: a fallthrough source exists and should branch to the
00794       // cleanup, but we're not supposed to branch through to the next
00795       // cleanup.
00796       } else if (HasFallthrough && FallthroughDest) {
00797         assert(!FallthroughIsBranchThrough);
00798         EmitBlock(FallthroughDest);
00799 
00800       // Case 3: a fallthrough source exists and should branch to the
00801       // cleanup and then through to the next.
00802       } else if (HasFallthrough) {
00803         // Everything is already set up for this.
00804 
00805       // Case 4: no fallthrough source exists.
00806       } else {
00807         Builder.ClearInsertionPoint();
00808       }
00809 
00810       // VI.  Assorted cleaning.
00811 
00812       // Check whether we can merge NormalEntry into a single predecessor.
00813       // This might invalidate (non-IR) pointers to NormalEntry.
00814       llvm::BasicBlock *NewNormalEntry =
00815         SimplifyCleanupEntry(*this, NormalEntry);
00816 
00817       // If it did invalidate those pointers, and NormalEntry was the same
00818       // as NormalExit, go back and patch up the fixups.
00819       if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
00820         for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
00821                I < E; ++I)
00822           EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
00823     }
00824   }
00825 
00826   assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
00827 
00828   // Emit the EH cleanup if required.
00829   if (RequiresEHCleanup) {
00830     CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
00831 
00832     EmitBlock(EHEntry);
00833 
00834     cleanupFlags.setIsForEHCleanup();
00835     EmitCleanup(*this, Fn, cleanupFlags, EHActiveFlag);
00836 
00837     Builder.CreateBr(getEHDispatchBlock(EHParent));
00838 
00839     Builder.restoreIP(SavedIP);
00840 
00841     SimplifyCleanupEntry(*this, EHEntry);
00842   }
00843 }
00844 
00845 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
00846 /// specified destination obviously has no cleanups to run.  'false' is always
00847 /// a conservatively correct answer for this method.
00848 bool CodeGenFunction::isObviouslyBranchWithoutCleanups(JumpDest Dest) const {
00849   assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
00850          && "stale jump destination");
00851   
00852   // Calculate the innermost active normal cleanup.
00853   EHScopeStack::stable_iterator TopCleanup =
00854     EHStack.getInnermostActiveNormalCleanup();
00855   
00856   // If we're not in an active normal cleanup scope, or if the
00857   // destination scope is within the innermost active normal cleanup
00858   // scope, we don't need to worry about fixups.
00859   if (TopCleanup == EHStack.stable_end() ||
00860       TopCleanup.encloses(Dest.getScopeDepth())) // works for invalid
00861     return true;
00862 
00863   // Otherwise, we might need some cleanups.
00864   return false;
00865 }
00866 
00867 
00868 /// Terminate the current block by emitting a branch which might leave
00869 /// the current cleanup-protected scope.  The target scope may not yet
00870 /// be known, in which case this will require a fixup.
00871 ///
00872 /// As a side-effect, this method clears the insertion point.
00873 void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
00874   assert(Dest.getScopeDepth().encloses(EHStack.stable_begin())
00875          && "stale jump destination");
00876 
00877   if (!HaveInsertPoint())
00878     return;
00879 
00880   // Create the branch.
00881   llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
00882 
00883   // Calculate the innermost active normal cleanup.
00884   EHScopeStack::stable_iterator
00885     TopCleanup = EHStack.getInnermostActiveNormalCleanup();
00886 
00887   // If we're not in an active normal cleanup scope, or if the
00888   // destination scope is within the innermost active normal cleanup
00889   // scope, we don't need to worry about fixups.
00890   if (TopCleanup == EHStack.stable_end() ||
00891       TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
00892     Builder.ClearInsertionPoint();
00893     return;
00894   }
00895 
00896   // If we can't resolve the destination cleanup scope, just add this
00897   // to the current cleanup scope as a branch fixup.
00898   if (!Dest.getScopeDepth().isValid()) {
00899     BranchFixup &Fixup = EHStack.addBranchFixup();
00900     Fixup.Destination = Dest.getBlock();
00901     Fixup.DestinationIndex = Dest.getDestIndex();
00902     Fixup.InitialBranch = BI;
00903     Fixup.OptimisticBranchBlock = 0;
00904 
00905     Builder.ClearInsertionPoint();
00906     return;
00907   }
00908 
00909   // Otherwise, thread through all the normal cleanups in scope.
00910 
00911   // Store the index at the start.
00912   llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
00913   new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI);
00914 
00915   // Adjust BI to point to the first cleanup block.
00916   {
00917     EHCleanupScope &Scope =
00918       cast<EHCleanupScope>(*EHStack.find(TopCleanup));
00919     BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
00920   }
00921 
00922   // Add this destination to all the scopes involved.
00923   EHScopeStack::stable_iterator I = TopCleanup;
00924   EHScopeStack::stable_iterator E = Dest.getScopeDepth();
00925   if (E.strictlyEncloses(I)) {
00926     while (true) {
00927       EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
00928       assert(Scope.isNormalCleanup());
00929       I = Scope.getEnclosingNormalCleanup();
00930 
00931       // If this is the last cleanup we're propagating through, tell it
00932       // that there's a resolved jump moving through it.
00933       if (!E.strictlyEncloses(I)) {
00934         Scope.addBranchAfter(Index, Dest.getBlock());
00935         break;
00936       }
00937 
00938       // Otherwise, tell the scope that there's a jump propoagating
00939       // through it.  If this isn't new information, all the rest of
00940       // the work has been done before.
00941       if (!Scope.addBranchThrough(Dest.getBlock()))
00942         break;
00943     }
00944   }
00945   
00946   Builder.ClearInsertionPoint();
00947 }
00948 
00949 static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
00950                                   EHScopeStack::stable_iterator C) {
00951   // If we needed a normal block for any reason, that counts.
00952   if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
00953     return true;
00954 
00955   // Check whether any enclosed cleanups were needed.
00956   for (EHScopeStack::stable_iterator
00957          I = EHStack.getInnermostNormalCleanup();
00958          I != C; ) {
00959     assert(C.strictlyEncloses(I));
00960     EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
00961     if (S.getNormalBlock()) return true;
00962     I = S.getEnclosingNormalCleanup();
00963   }
00964 
00965   return false;
00966 }
00967 
00968 static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
00969                               EHScopeStack::stable_iterator cleanup) {
00970   // If we needed an EH block for any reason, that counts.
00971   if (EHStack.find(cleanup)->hasEHBranches())
00972     return true;
00973 
00974   // Check whether any enclosed cleanups were needed.
00975   for (EHScopeStack::stable_iterator
00976          i = EHStack.getInnermostEHScope(); i != cleanup; ) {
00977     assert(cleanup.strictlyEncloses(i));
00978 
00979     EHScope &scope = *EHStack.find(i);
00980     if (scope.hasEHBranches())
00981       return true;
00982 
00983     i = scope.getEnclosingEHScope();
00984   }
00985 
00986   return false;
00987 }
00988 
00989 enum ForActivation_t {
00990   ForActivation,
00991   ForDeactivation
00992 };
00993 
00994 /// The given cleanup block is changing activation state.  Configure a
00995 /// cleanup variable if necessary.
00996 ///
00997 /// It would be good if we had some way of determining if there were
00998 /// extra uses *after* the change-over point.
00999 static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
01000                                         EHScopeStack::stable_iterator C,
01001                                         ForActivation_t kind,
01002                                         llvm::Instruction *dominatingIP) {
01003   EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
01004 
01005   // We always need the flag if we're activating the cleanup in a
01006   // conditional context, because we have to assume that the current
01007   // location doesn't necessarily dominate the cleanup's code.
01008   bool isActivatedInConditional =
01009     (kind == ForActivation && CGF.isInConditionalBranch());
01010 
01011   bool needFlag = false;
01012 
01013   // Calculate whether the cleanup was used:
01014 
01015   //   - as a normal cleanup
01016   if (Scope.isNormalCleanup() &&
01017       (isActivatedInConditional || IsUsedAsNormalCleanup(CGF.EHStack, C))) {
01018     Scope.setTestFlagInNormalCleanup();
01019     needFlag = true;
01020   }
01021 
01022   //  - as an EH cleanup
01023   if (Scope.isEHCleanup() &&
01024       (isActivatedInConditional || IsUsedAsEHCleanup(CGF.EHStack, C))) {
01025     Scope.setTestFlagInEHCleanup();
01026     needFlag = true;
01027   }
01028 
01029   // If it hasn't yet been used as either, we're done.
01030   if (!needFlag) return;
01031 
01032   llvm::AllocaInst *var = Scope.getActiveFlag();
01033   if (!var) {
01034     var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "cleanup.isactive");
01035     Scope.setActiveFlag(var);
01036 
01037     assert(dominatingIP && "no existing variable and no dominating IP!");
01038 
01039     // Initialize to true or false depending on whether it was
01040     // active up to this point.
01041     llvm::Value *value = CGF.Builder.getInt1(kind == ForDeactivation);
01042 
01043     // If we're in a conditional block, ignore the dominating IP and
01044     // use the outermost conditional branch.
01045     if (CGF.isInConditionalBranch()) {
01046       CGF.setBeforeOutermostConditional(value, var);
01047     } else {
01048       new llvm::StoreInst(value, var, dominatingIP);
01049     }
01050   }
01051 
01052   CGF.Builder.CreateStore(CGF.Builder.getInt1(kind == ForActivation), var);
01053 }
01054 
01055 /// Activate a cleanup that was created in an inactivated state.
01056 void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C,
01057                                            llvm::Instruction *dominatingIP) {
01058   assert(C != EHStack.stable_end() && "activating bottom of stack?");
01059   EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
01060   assert(!Scope.isActive() && "double activation");
01061 
01062   SetupCleanupBlockActivation(*this, C, ForActivation, dominatingIP);
01063 
01064   Scope.setActive(true);
01065 }
01066 
01067 /// Deactive a cleanup that was created in an active state.
01068 void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C,
01069                                              llvm::Instruction *dominatingIP) {
01070   assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
01071   EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
01072   assert(Scope.isActive() && "double deactivation");
01073 
01074   // If it's the top of the stack, just pop it.
01075   if (C == EHStack.stable_begin()) {
01076     // If it's a normal cleanup, we need to pretend that the
01077     // fallthrough is unreachable.
01078     CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
01079     PopCleanupBlock();
01080     Builder.restoreIP(SavedIP);
01081     return;
01082   }
01083 
01084   // Otherwise, follow the general case.
01085   SetupCleanupBlockActivation(*this, C, ForDeactivation, dominatingIP);
01086 
01087   Scope.setActive(false);
01088 }
01089 
01090 llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() {
01091   if (!NormalCleanupDest)
01092     NormalCleanupDest =
01093       CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
01094   return NormalCleanupDest;
01095 }
01096 
01097 /// Emits all the code to cause the given temporary to be cleaned up.
01098 void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary,
01099                                        QualType TempType,
01100                                        llvm::Value *Ptr) {
01101   pushDestroy(NormalAndEHCleanup, Ptr, TempType, destroyCXXObject,
01102               /*useEHCleanup*/ true);
01103 }