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