1 //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
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 coordinates the per-function state used while generating code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGDebugInfo.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/AST/APValue.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Decl.h"
21 #include "llvm/Support/CFG.h"
22 #include "llvm/Target/TargetData.h"
23 using namespace clang;
24 using namespace CodeGen;
25 
26 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
27   : CGM(cgm), Target(CGM.getContext().Target), DebugInfo(0), SwitchInsn(0),
28     CaseRangeBlock(0), InvokeDest(0) {
29   LLVMIntTy = ConvertType(getContext().IntTy);
30   LLVMPointerWidth = Target.getPointerWidth(0);
31 
32   // FIXME: We need to rearrange the code for copy/dispose so we have this
33   // sooner, so we can calculate offsets correctly.
34   BlockHasCopyDispose = false;
35   if (!BlockHasCopyDispose)
36     BlockOffset = CGM.getTargetData()
37       .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
38   else
39     BlockOffset = CGM.getTargetData()
40       .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
41 }
42 
43 ASTContext &CodeGenFunction::getContext() const {
44   return CGM.getContext();
45 }
46 
47 
48 llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
49   llvm::BasicBlock *&BB = LabelMap[S];
50   if (BB) return BB;
51 
52   // Create, but don't insert, the new block.
53   return BB = createBasicBlock(S->getName());
54 }
55 
56 llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
57   llvm::Value *Res = LocalDeclMap[VD];
58   assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
59   return Res;
60 }
61 
62 llvm::Constant *
63 CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
64   return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
65 }
66 
67 const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
68   return CGM.getTypes().ConvertTypeForMem(T);
69 }
70 
71 const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
72   return CGM.getTypes().ConvertType(T);
73 }
74 
75 bool CodeGenFunction::isObjCPointerType(QualType T) {
76   // All Objective-C types are pointers.
77   return T->isObjCInterfaceType() ||
78     T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
79 }
80 
81 bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
82   // FIXME: Use positive checks instead of negative ones to be more
83   // robust in the face of extension.
84   return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
85     !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
86     !T->isBlockPointerType();
87 }
88 
89 void CodeGenFunction::EmitReturnBlock() {
90   // For cleanliness, we try to avoid emitting the return block for
91   // simple cases.
92   llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
93 
94   if (CurBB) {
95     assert(!CurBB->getTerminator() && "Unexpected terminated block.");
96 
97     // We have a valid insert point, reuse it if there are no explicit
98     // jumps to the return block.
99     if (ReturnBlock->use_empty())
100       delete ReturnBlock;
101     else
102       EmitBlock(ReturnBlock);
103     return;
104   }
105 
106   // Otherwise, if the return block is the target of a single direct
107   // branch then we can just put the code in that block instead. This
108   // cleans up functions which started with a unified return block.
109   if (ReturnBlock->hasOneUse()) {
110     llvm::BranchInst *BI =
111       dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
112     if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
113       // Reset insertion point and delete the branch.
114       Builder.SetInsertPoint(BI->getParent());
115       BI->eraseFromParent();
116       delete ReturnBlock;
117       return;
118     }
119   }
120 
121   // FIXME: We are at an unreachable point, there is no reason to emit
122   // the block unless it has uses. However, we still need a place to
123   // put the debug region.end for now.
124 
125   EmitBlock(ReturnBlock);
126 }
127 
128 void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
129   // Finish emission of indirect switches.
130   EmitIndirectSwitches();
131 
132   assert(BreakContinueStack.empty() &&
133          "mismatched push/pop in break/continue stack!");
134   assert(BlockScopes.empty() &&
135          "did not remove all blocks from block scope map!");
136   assert(CleanupEntries.empty() &&
137          "mismatched push/pop in cleanup stack!");
138 
139   // Emit function epilog (to return).
140   EmitReturnBlock();
141 
142   // Emit debug descriptor for function end.
143   if (CGDebugInfo *DI = getDebugInfo()) {
144     DI->setLocation(EndLoc);
145     DI->EmitRegionEnd(CurFn, Builder);
146   }
147 
148   EmitFunctionEpilog(*CurFnInfo, ReturnValue);
149 
150   // Remove the AllocaInsertPt instruction, which is just a convenience for us.
151   AllocaInsertPt->eraseFromParent();
152   AllocaInsertPt = 0;
153 }
154 
155 void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
156                                     llvm::Function *Fn,
157                                     const FunctionArgList &Args,
158                                     SourceLocation StartLoc) {
159   DidCallStackSave = false;
160   CurFuncDecl = D;
161   FnRetTy = RetTy;
162   CurFn = Fn;
163   assert(CurFn->isDeclaration() && "Function already has body?");
164 
165   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
166 
167   // Create a marker to make it easy to insert allocas into the entryblock
168   // later.  Don't create this with the builder, because we don't want it
169   // folded.
170   llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
171   AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
172                                          EntryBB);
173 
174   ReturnBlock = createBasicBlock("return");
175   ReturnValue = 0;
176   if (!RetTy->isVoidType())
177     ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
178 
179   Builder.SetInsertPoint(EntryBB);
180 
181   // Emit subprogram debug descriptor.
182   // FIXME: The cast here is a huge hack.
183   if (CGDebugInfo *DI = getDebugInfo()) {
184     DI->setLocation(StartLoc);
185     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
186       DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
187     } else {
188       // Just use LLVM function name.
189       DI->EmitFunctionStart(Fn->getName().c_str(),
190                             RetTy, CurFn, Builder);
191     }
192   }
193 
194   // FIXME: Leaked.
195   CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
196   EmitFunctionProlog(*CurFnInfo, CurFn, Args);
197 
198   // If any of the arguments have a variably modified type, make sure to
199   // emit the type size.
200   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
201        i != e; ++i) {
202     QualType Ty = i->second;
203 
204     if (Ty->isVariablyModifiedType())
205       EmitVLASize(Ty);
206   }
207 }
208 
209 void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
210                                    llvm::Function *Fn) {
211   // Check if we should generate debug info for this function.
212   if (CGM.getDebugInfo() && !FD->getAttr<NodebugAttr>())
213     DebugInfo = CGM.getDebugInfo();
214 
215   FunctionArgList Args;
216   if (FD->getNumParams()) {
217     const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
218     assert(FProto && "Function def must have prototype!");
219 
220     for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
221       Args.push_back(std::make_pair(FD->getParamDecl(i),
222                                     FProto->getArgType(i)));
223   }
224 
225   StartFunction(FD, FD->getResultType(), Fn, Args,
226                 cast<CompoundStmt>(FD->getBody())->getLBracLoc());
227 
228   EmitStmt(FD->getBody());
229 
230   const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody());
231   if (S) {
232     FinishFunction(S->getRBracLoc());
233   } else {
234     FinishFunction();
235   }
236 }
237 
238 /// ContainsLabel - Return true if the statement contains a label in it.  If
239 /// this statement is not executed normally, it not containing a label means
240 /// that we can just remove the code.
241 bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
242   // Null statement, not a label!
243   if (S == 0) return false;
244 
245   // If this is a label, we have to emit the code, consider something like:
246   // if (0) {  ...  foo:  bar(); }  goto foo;
247   if (isa<LabelStmt>(S))
248     return true;
249 
250   // If this is a case/default statement, and we haven't seen a switch, we have
251   // to emit the code.
252   if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
253     return true;
254 
255   // If this is a switch statement, we want to ignore cases below it.
256   if (isa<SwitchStmt>(S))
257     IgnoreCaseStmts = true;
258 
259   // Scan subexpressions for verboten labels.
260   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
261        I != E; ++I)
262     if (ContainsLabel(*I, IgnoreCaseStmts))
263       return true;
264 
265   return false;
266 }
267 
268 
269 /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
270 /// a constant, or if it does but contains a label, return 0.  If it constant
271 /// folds to 'true' and does not contain a label, return 1, if it constant folds
272 /// to 'false' and does not contain a label, return -1.
273 int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
274   // FIXME: Rename and handle conversion of other evaluatable things
275   // to bool.
276   Expr::EvalResult Result;
277   if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
278       Result.HasSideEffects)
279     return 0;  // Not foldable, not integer or not fully evaluatable.
280 
281   if (CodeGenFunction::ContainsLabel(Cond))
282     return 0;  // Contains a label.
283 
284   return Result.Val.getInt().getBoolValue() ? 1 : -1;
285 }
286 
287 
288 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
289 /// statement) to the specified blocks.  Based on the condition, this might try
290 /// to simplify the codegen of the conditional based on the branch.
291 ///
292 void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
293                                            llvm::BasicBlock *TrueBlock,
294                                            llvm::BasicBlock *FalseBlock) {
295   if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
296     return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
297 
298   if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
299     // Handle X && Y in a condition.
300     if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
301       // If we have "1 && X", simplify the code.  "0 && X" would have constant
302       // folded if the case was simple enough.
303       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
304         // br(1 && X) -> br(X).
305         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
306       }
307 
308       // If we have "X && 1", simplify the code to use an uncond branch.
309       // "X && 0" would have been constant folded to 0.
310       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
311         // br(X && 1) -> br(X).
312         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
313       }
314 
315       // Emit the LHS as a conditional.  If the LHS conditional is false, we
316       // want to jump to the FalseBlock.
317       llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
318       EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
319       EmitBlock(LHSTrue);
320 
321       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
322       return;
323     } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
324       // If we have "0 || X", simplify the code.  "1 || X" would have constant
325       // folded if the case was simple enough.
326       if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
327         // br(0 || X) -> br(X).
328         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
329       }
330 
331       // If we have "X || 0", simplify the code to use an uncond branch.
332       // "X || 1" would have been constant folded to 1.
333       if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
334         // br(X || 0) -> br(X).
335         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
336       }
337 
338       // Emit the LHS as a conditional.  If the LHS conditional is true, we
339       // want to jump to the TrueBlock.
340       llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
341       EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
342       EmitBlock(LHSFalse);
343 
344       EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
345       return;
346     }
347   }
348 
349   if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
350     // br(!x, t, f) -> br(x, f, t)
351     if (CondUOp->getOpcode() == UnaryOperator::LNot)
352       return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
353   }
354 
355   if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
356     // Handle ?: operator.
357 
358     // Just ignore GNU ?: extension.
359     if (CondOp->getLHS()) {
360       // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
361       llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
362       llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
363       EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
364       EmitBlock(LHSBlock);
365       EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
366       EmitBlock(RHSBlock);
367       EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
368       return;
369     }
370   }
371 
372   // Emit the code with the fully general case.
373   llvm::Value *CondV = EvaluateExprAsBool(Cond);
374   Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
375 }
376 
377 /// getCGRecordLayout - Return record layout info.
378 const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
379                                                          QualType Ty) {
380   const RecordType *RTy = Ty->getAsRecordType();
381   assert (RTy && "Unexpected type. RecordType expected here.");
382 
383   return CGT.getCGRecordLayout(RTy->getDecl());
384 }
385 
386 /// ErrorUnsupported - Print out an error that codegen doesn't support the
387 /// specified stmt yet.
388 void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
389                                        bool OmitOnError) {
390   CGM.ErrorUnsupported(S, Type, OmitOnError);
391 }
392 
393 unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
394   // Use LabelIDs.size() as the new ID if one hasn't been assigned.
395   return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
396 }
397 
398 void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty)
399 {
400   const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
401   if (DestPtr->getType() != BP)
402     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
403 
404   // Get size and alignment info for this aggregate.
405   std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
406 
407   // FIXME: Handle variable sized types.
408   const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
409 
410   Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
411                       llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
412                       // TypeInfo.first describes size in bits.
413                       llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
414                       llvm::ConstantInt::get(llvm::Type::Int32Ty,
415                                              TypeInfo.second/8));
416 }
417 
418 void CodeGenFunction::EmitIndirectSwitches() {
419   llvm::BasicBlock *Default;
420 
421   if (IndirectSwitches.empty())
422     return;
423 
424   if (!LabelIDs.empty()) {
425     Default = getBasicBlockForLabel(LabelIDs.begin()->first);
426   } else {
427     // No possible targets for indirect goto, just emit an infinite
428     // loop.
429     Default = createBasicBlock("indirectgoto.loop", CurFn);
430     llvm::BranchInst::Create(Default, Default);
431   }
432 
433   for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
434          e = IndirectSwitches.end(); i != e; ++i) {
435     llvm::SwitchInst *I = *i;
436 
437     I->setSuccessor(0, Default);
438     for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
439            LE = LabelIDs.end(); LI != LE; ++LI) {
440       I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
441                                         LI->second),
442                  getBasicBlockForLabel(LI->first));
443     }
444   }
445 }
446 
447 llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
448 {
449   llvm::Value *&SizeEntry = VLASizeMap[VAT];
450 
451   assert(SizeEntry && "Did not emit size for type");
452   return SizeEntry;
453 }
454 
455 llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty)
456 {
457   assert(Ty->isVariablyModifiedType() &&
458          "Must pass variably modified type to EmitVLASizes!");
459 
460   if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
461     llvm::Value *&SizeEntry = VLASizeMap[VAT];
462 
463     if (!SizeEntry) {
464       // Get the element size;
465       llvm::Value *ElemSize;
466 
467       QualType ElemTy = VAT->getElementType();
468 
469       const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
470 
471       if (ElemTy->isVariableArrayType())
472         ElemSize = EmitVLASize(ElemTy);
473       else {
474         ElemSize = llvm::ConstantInt::get(SizeTy,
475                                           getContext().getTypeSize(ElemTy) / 8);
476       }
477 
478       llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
479       NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
480 
481       SizeEntry = Builder.CreateMul(ElemSize, NumElements);
482     }
483 
484     return SizeEntry;
485   } else if (const PointerType *PT = Ty->getAsPointerType())
486     EmitVLASize(PT->getPointeeType());
487   else {
488     assert(0 && "unknown VM type!");
489   }
490 
491   return 0;
492 }
493 
494 llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
495   if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
496     return EmitScalarExpr(E);
497   }
498   return EmitLValue(E).getAddress();
499 }
500 
501 void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
502 {
503   CleanupEntries.push_back(CleanupEntry(CleanupBlock));
504 }
505 
506 void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
507 {
508   assert(CleanupEntries.size() >= OldCleanupStackSize &&
509          "Cleanup stack mismatch!");
510 
511   while (CleanupEntries.size() > OldCleanupStackSize)
512     EmitCleanupBlock();
513 }
514 
515 CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
516 {
517   CleanupEntry &CE = CleanupEntries.back();
518 
519   llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
520 
521   std::vector<llvm::BasicBlock *> Blocks;
522   std::swap(Blocks, CE.Blocks);
523 
524   std::vector<llvm::BranchInst *> BranchFixups;
525   std::swap(BranchFixups, CE.BranchFixups);
526 
527   CleanupEntries.pop_back();
528 
529   // Check if any branch fixups pointed to the scope we just popped. If so,
530   // we can remove them.
531   for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
532     llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
533     BlockScopeMap::iterator I = BlockScopes.find(Dest);
534 
535     if (I == BlockScopes.end())
536       continue;
537 
538     assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
539 
540     if (I->second == CleanupEntries.size()) {
541       // We don't need to do this branch fixup.
542       BranchFixups[i] = BranchFixups.back();
543       BranchFixups.pop_back();
544       i--;
545       e--;
546       continue;
547     }
548   }
549 
550   llvm::BasicBlock *SwitchBlock = 0;
551   llvm::BasicBlock *EndBlock = 0;
552   if (!BranchFixups.empty()) {
553     SwitchBlock = createBasicBlock("cleanup.switch");
554     EndBlock = createBasicBlock("cleanup.end");
555 
556     llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
557 
558     Builder.SetInsertPoint(SwitchBlock);
559 
560     llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty,
561                                                 "cleanup.dst");
562     llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
563 
564     // Create a switch instruction to determine where to jump next.
565     llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
566                                                 BranchFixups.size());
567 
568     // Restore the current basic block (if any)
569     if (CurBB)
570       Builder.SetInsertPoint(CurBB);
571     else
572       Builder.ClearInsertionPoint();
573 
574     for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
575       llvm::BranchInst *BI = BranchFixups[i];
576       llvm::BasicBlock *Dest = BI->getSuccessor(0);
577 
578       // Fixup the branch instruction to point to the cleanup block.
579       BI->setSuccessor(0, CleanupBlock);
580 
581       if (CleanupEntries.empty()) {
582         llvm::ConstantInt *ID;
583 
584         // Check if we already have a destination for this block.
585         if (Dest == SI->getDefaultDest())
586           ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
587         else {
588           ID = SI->findCaseDest(Dest);
589           if (!ID) {
590             // No code found, get a new unique one by using the number of
591             // switch successors.
592             ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
593                                         SI->getNumSuccessors());
594             SI->addCase(ID, Dest);
595           }
596         }
597 
598         // Store the jump destination before the branch instruction.
599         new llvm::StoreInst(ID, DestCodePtr, BI);
600       } else {
601         // We need to jump through another cleanup block. Create a pad block
602         // with a branch instruction that jumps to the final destination and
603         // add it as a branch fixup to the current cleanup scope.
604 
605         // Create the pad block.
606         llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
607 
608         // Create a unique case ID.
609         llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty,
610                                                        SI->getNumSuccessors());
611 
612         // Store the jump destination before the branch instruction.
613         new llvm::StoreInst(ID, DestCodePtr, BI);
614 
615         // Add it as the destination.
616         SI->addCase(ID, CleanupPad);
617 
618         // Create the branch to the final destination.
619         llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
620         CleanupPad->getInstList().push_back(BI);
621 
622         // And add it as a branch fixup.
623         CleanupEntries.back().BranchFixups.push_back(BI);
624       }
625     }
626   }
627 
628   // Remove all blocks from the block scope map.
629   for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
630     assert(BlockScopes.count(Blocks[i]) &&
631            "Did not find block in scope map!");
632 
633     BlockScopes.erase(Blocks[i]);
634   }
635 
636   return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
637 }
638 
639 void CodeGenFunction::EmitCleanupBlock()
640 {
641   CleanupBlockInfo Info = PopCleanupBlock();
642 
643   EmitBlock(Info.CleanupBlock);
644 
645   if (Info.SwitchBlock)
646     EmitBlock(Info.SwitchBlock);
647   if (Info.EndBlock)
648     EmitBlock(Info.EndBlock);
649 }
650 
651 void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
652 {
653   assert(!CleanupEntries.empty() &&
654          "Trying to add branch fixup without cleanup block!");
655 
656   // FIXME: We could be more clever here and check if there's already a
657   // branch fixup for this destination and recycle it.
658   CleanupEntries.back().BranchFixups.push_back(BI);
659 }
660 
661 void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
662 {
663   if (!HaveInsertPoint())
664     return;
665 
666   llvm::BranchInst* BI = Builder.CreateBr(Dest);
667 
668   Builder.ClearInsertionPoint();
669 
670   // The stack is empty, no need to do any cleanup.
671   if (CleanupEntries.empty())
672     return;
673 
674   if (!Dest->getParent()) {
675     // We are trying to branch to a block that hasn't been inserted yet.
676     AddBranchFixup(BI);
677     return;
678   }
679 
680   BlockScopeMap::iterator I = BlockScopes.find(Dest);
681   if (I == BlockScopes.end()) {
682     // We are trying to jump to a block that is outside of any cleanup scope.
683     AddBranchFixup(BI);
684     return;
685   }
686 
687   assert(I->second < CleanupEntries.size() &&
688          "Trying to branch into cleanup region");
689 
690   if (I->second == CleanupEntries.size() - 1) {
691     // We have a branch to a block in the same scope.
692     return;
693   }
694 
695   AddBranchFixup(BI);
696 }
697