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