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