CodeGenFunction.cpp revision 3019c444c672938c57f5573840071ecd73425ee7
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 "CGCXXABI.h"
17#include "CGDebugInfo.h"
18#include "CGException.h"
19#include "clang/Basic/TargetInfo.h"
20#include "clang/AST/APValue.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclCXX.h"
24#include "clang/AST/StmtCXX.h"
25#include "clang/Frontend/CodeGenOptions.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Intrinsics.h"
28using namespace clang;
29using namespace CodeGen;
30
31CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
32  : BlockFunction(cgm, *this, Builder), CGM(cgm),
33    Target(CGM.getContext().Target),
34    Builder(cgm.getModule().getContext()),
35    NormalCleanupDest(0), EHCleanupDest(0), NextCleanupDestIndex(1),
36    ExceptionSlot(0), DebugInfo(0), IndirectBranch(0),
37    SwitchInsn(0), CaseRangeBlock(0),
38    DidCallStackSave(false), UnreachableBlock(0),
39    CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0),
40    ConditionalBranchLevel(0), TerminateLandingPad(0), TerminateHandler(0),
41    TrapBB(0) {
42
43  // Get some frequently used types.
44  LLVMPointerWidth = Target.getPointerWidth(0);
45  llvm::LLVMContext &LLVMContext = CGM.getLLVMContext();
46  IntPtrTy = llvm::IntegerType::get(LLVMContext, LLVMPointerWidth);
47  Int32Ty  = llvm::Type::getInt32Ty(LLVMContext);
48  Int64Ty  = llvm::Type::getInt64Ty(LLVMContext);
49
50  Exceptions = getContext().getLangOptions().Exceptions;
51  CatchUndefined = getContext().getLangOptions().CatchUndefined;
52  CGM.getCXXABI().getMangleContext().startNewFunction();
53}
54
55ASTContext &CodeGenFunction::getContext() const {
56  return CGM.getContext();
57}
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->isObjCObjectType();
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.getBlock()->use_empty()) {
84      ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
85      delete ReturnBlock.getBlock();
86    } else
87      EmitBlock(ReturnBlock.getBlock());
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.getBlock()->hasOneUse()) {
95    llvm::BranchInst *BI =
96      dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
97    if (BI && BI->isUnconditional() &&
98        BI->getSuccessor(0) == ReturnBlock.getBlock()) {
99      // Reset insertion point and delete the branch.
100      Builder.SetInsertPoint(BI->getParent());
101      BI->eraseFromParent();
102      delete ReturnBlock.getBlock();
103      return;
104    }
105  }
106
107  // FIXME: We are at an unreachable point, there is no reason to emit the block
108  // unless it has uses. However, we still need a place to put the debug
109  // region.end for now.
110
111  EmitBlock(ReturnBlock.getBlock());
112}
113
114static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
115  if (!BB) return;
116  if (!BB->use_empty())
117    return CGF.CurFn->getBasicBlockList().push_back(BB);
118  delete BB;
119}
120
121void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
122  assert(BreakContinueStack.empty() &&
123         "mismatched push/pop in break/continue stack!");
124
125  // Emit function epilog (to return).
126  EmitReturnBlock();
127
128  EmitFunctionInstrumentation("__cyg_profile_func_exit");
129
130  // Emit debug descriptor for function end.
131  if (CGDebugInfo *DI = getDebugInfo()) {
132    DI->setLocation(EndLoc);
133    DI->EmitFunctionEnd(Builder);
134  }
135
136  EmitFunctionEpilog(*CurFnInfo);
137  EmitEndEHSpec(CurCodeDecl);
138
139  assert(EHStack.empty() &&
140         "did not remove all scopes from cleanup stack!");
141
142  // If someone did an indirect goto, emit the indirect goto block at the end of
143  // the function.
144  if (IndirectBranch) {
145    EmitBlock(IndirectBranch->getParent());
146    Builder.ClearInsertionPoint();
147  }
148
149  // Remove the AllocaInsertPt instruction, which is just a convenience for us.
150  llvm::Instruction *Ptr = AllocaInsertPt;
151  AllocaInsertPt = 0;
152  Ptr->eraseFromParent();
153
154  // If someone took the address of a label but never did an indirect goto, we
155  // made a zero entry PHI node, which is illegal, zap it now.
156  if (IndirectBranch) {
157    llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
158    if (PN->getNumIncomingValues() == 0) {
159      PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
160      PN->eraseFromParent();
161    }
162  }
163
164  EmitIfUsed(*this, RethrowBlock.getBlock());
165  EmitIfUsed(*this, TerminateLandingPad);
166  EmitIfUsed(*this, TerminateHandler);
167  EmitIfUsed(*this, UnreachableBlock);
168
169  if (CGM.getCodeGenOpts().EmitDeclMetadata)
170    EmitDeclMetadata();
171}
172
173/// ShouldInstrumentFunction - Return true if the current function should be
174/// instrumented with __cyg_profile_func_* calls
175bool CodeGenFunction::ShouldInstrumentFunction() {
176  if (!CGM.getCodeGenOpts().InstrumentFunctions)
177    return false;
178  if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
179    return false;
180  return true;
181}
182
183/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
184/// instrumentation function with the current function and the call site, if
185/// function instrumentation is enabled.
186void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
187  if (!ShouldInstrumentFunction())
188    return;
189
190  const llvm::PointerType *PointerTy;
191  const llvm::FunctionType *FunctionTy;
192  std::vector<const llvm::Type*> ProfileFuncArgs;
193
194  // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
195  PointerTy = llvm::Type::getInt8PtrTy(VMContext);
196  ProfileFuncArgs.push_back(PointerTy);
197  ProfileFuncArgs.push_back(PointerTy);
198  FunctionTy = llvm::FunctionType::get(
199    llvm::Type::getVoidTy(VMContext),
200    ProfileFuncArgs, false);
201
202  llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
203  llvm::CallInst *CallSite = Builder.CreateCall(
204    CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0),
205    llvm::ConstantInt::get(Int32Ty, 0),
206    "callsite");
207
208  Builder.CreateCall2(F,
209                      llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
210                      CallSite);
211}
212
213void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
214                                    llvm::Function *Fn,
215                                    const FunctionArgList &Args,
216                                    SourceLocation StartLoc) {
217  const Decl *D = GD.getDecl();
218
219  DidCallStackSave = false;
220  CurCodeDecl = CurFuncDecl = D;
221  FnRetTy = RetTy;
222  CurFn = Fn;
223  assert(CurFn->isDeclaration() && "Function already has body?");
224
225  // Pass inline keyword to optimizer if it appears explicitly on any
226  // declaration.
227  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
228    for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
229           RE = FD->redecls_end(); RI != RE; ++RI)
230      if (RI->isInlineSpecified()) {
231        Fn->addFnAttr(llvm::Attribute::InlineHint);
232        break;
233      }
234
235  llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
236
237  // Create a marker to make it easy to insert allocas into the entryblock
238  // later.  Don't create this with the builder, because we don't want it
239  // folded.
240  llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
241  AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
242  if (Builder.isNamePreserving())
243    AllocaInsertPt->setName("allocapt");
244
245  ReturnBlock = getJumpDestInCurrentScope("return");
246
247  Builder.SetInsertPoint(EntryBB);
248
249  QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0,
250                                                 false, false, 0, 0,
251                                                 /*FIXME?*/
252                                                 FunctionType::ExtInfo());
253
254  // Emit subprogram debug descriptor.
255  if (CGDebugInfo *DI = getDebugInfo()) {
256    DI->setLocation(StartLoc);
257    DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
258  }
259
260  EmitFunctionInstrumentation("__cyg_profile_func_enter");
261
262  // FIXME: Leaked.
263  // CC info is ignored, hopefully?
264  CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args,
265                                              FunctionType::ExtInfo());
266
267  if (RetTy->isVoidType()) {
268    // Void type; nothing to return.
269    ReturnValue = 0;
270  } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
271             hasAggregateLLVMType(CurFnInfo->getReturnType())) {
272    // Indirect aggregate return; emit returned value directly into sret slot.
273    // This reduces code size, and affects correctness in C++.
274    ReturnValue = CurFn->arg_begin();
275  } else {
276    ReturnValue = CreateIRTemp(RetTy, "retval");
277  }
278
279  EmitStartEHSpec(CurCodeDecl);
280  EmitFunctionProlog(*CurFnInfo, CurFn, Args);
281
282  if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
283    CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
284
285  // If any of the arguments have a variably modified type, make sure to
286  // emit the type size.
287  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
288       i != e; ++i) {
289    QualType Ty = i->second;
290
291    if (Ty->isVariablyModifiedType())
292      EmitVLASize(Ty);
293  }
294}
295
296void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
297  const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
298  assert(FD->getBody());
299  EmitStmt(FD->getBody());
300}
301
302/// Tries to mark the given function nounwind based on the
303/// non-existence of any throwing calls within it.  We believe this is
304/// lightweight enough to do at -O0.
305static void TryMarkNoThrow(llvm::Function *F) {
306  // LLVM treats 'nounwind' on a function as part of the type, so we
307  // can't do this on functions that can be overwritten.
308  if (F->mayBeOverridden()) return;
309
310  for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
311    for (llvm::BasicBlock::iterator
312           BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
313      if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI))
314        if (!Call->doesNotThrow())
315          return;
316  F->setDoesNotThrow(true);
317}
318
319void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) {
320  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
321
322  // Check if we should generate debug info for this function.
323  if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
324    DebugInfo = CGM.getDebugInfo();
325
326  FunctionArgList Args;
327  QualType ResTy = FD->getResultType();
328
329  CurGD = GD;
330  if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance())
331    CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args);
332
333  if (FD->getNumParams()) {
334    const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
335    assert(FProto && "Function def must have prototype!");
336
337    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
338      Args.push_back(std::make_pair(FD->getParamDecl(i),
339                                    FProto->getArgType(i)));
340  }
341
342  SourceRange BodyRange;
343  if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
344
345  // Emit the standard function prologue.
346  StartFunction(GD, ResTy, Fn, Args, BodyRange.getBegin());
347
348  // Generate the body of the function.
349  if (isa<CXXDestructorDecl>(FD))
350    EmitDestructorBody(Args);
351  else if (isa<CXXConstructorDecl>(FD))
352    EmitConstructorBody(Args);
353  else
354    EmitFunctionBody(Args);
355
356  // Emit the standard function epilogue.
357  FinishFunction(BodyRange.getEnd());
358
359  // If we haven't marked the function nothrow through other means, do
360  // a quick pass now to see if we can.
361  if (!CurFn->doesNotThrow())
362    TryMarkNoThrow(CurFn);
363}
364
365/// ContainsLabel - Return true if the statement contains a label in it.  If
366/// this statement is not executed normally, it not containing a label means
367/// that we can just remove the code.
368bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
369  // Null statement, not a label!
370  if (S == 0) return false;
371
372  // If this is a label, we have to emit the code, consider something like:
373  // if (0) {  ...  foo:  bar(); }  goto foo;
374  if (isa<LabelStmt>(S))
375    return true;
376
377  // If this is a case/default statement, and we haven't seen a switch, we have
378  // to emit the code.
379  if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
380    return true;
381
382  // If this is a switch statement, we want to ignore cases below it.
383  if (isa<SwitchStmt>(S))
384    IgnoreCaseStmts = true;
385
386  // Scan subexpressions for verboten labels.
387  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
388       I != E; ++I)
389    if (ContainsLabel(*I, IgnoreCaseStmts))
390      return true;
391
392  return false;
393}
394
395
396/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
397/// a constant, or if it does but contains a label, return 0.  If it constant
398/// folds to 'true' and does not contain a label, return 1, if it constant folds
399/// to 'false' and does not contain a label, return -1.
400int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
401  // FIXME: Rename and handle conversion of other evaluatable things
402  // to bool.
403  Expr::EvalResult Result;
404  if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
405      Result.HasSideEffects)
406    return 0;  // Not foldable, not integer or not fully evaluatable.
407
408  if (CodeGenFunction::ContainsLabel(Cond))
409    return 0;  // Contains a label.
410
411  return Result.Val.getInt().getBoolValue() ? 1 : -1;
412}
413
414
415/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
416/// statement) to the specified blocks.  Based on the condition, this might try
417/// to simplify the codegen of the conditional based on the branch.
418///
419void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
420                                           llvm::BasicBlock *TrueBlock,
421                                           llvm::BasicBlock *FalseBlock) {
422  if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
423    return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
424
425  if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
426    // Handle X && Y in a condition.
427    if (CondBOp->getOpcode() == BO_LAnd) {
428      // If we have "1 && X", simplify the code.  "0 && X" would have constant
429      // folded if the case was simple enough.
430      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
431        // br(1 && X) -> br(X).
432        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
433      }
434
435      // If we have "X && 1", simplify the code to use an uncond branch.
436      // "X && 0" would have been constant folded to 0.
437      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
438        // br(X && 1) -> br(X).
439        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
440      }
441
442      // Emit the LHS as a conditional.  If the LHS conditional is false, we
443      // want to jump to the FalseBlock.
444      llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
445      EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
446      EmitBlock(LHSTrue);
447
448      // Any temporaries created here are conditional.
449      BeginConditionalBranch();
450      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
451      EndConditionalBranch();
452
453      return;
454    } else if (CondBOp->getOpcode() == BO_LOr) {
455      // If we have "0 || X", simplify the code.  "1 || X" would have constant
456      // folded if the case was simple enough.
457      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
458        // br(0 || X) -> br(X).
459        return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
460      }
461
462      // If we have "X || 0", simplify the code to use an uncond branch.
463      // "X || 1" would have been constant folded to 1.
464      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
465        // br(X || 0) -> br(X).
466        return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
467      }
468
469      // Emit the LHS as a conditional.  If the LHS conditional is true, we
470      // want to jump to the TrueBlock.
471      llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
472      EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
473      EmitBlock(LHSFalse);
474
475      // Any temporaries created here are conditional.
476      BeginConditionalBranch();
477      EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
478      EndConditionalBranch();
479
480      return;
481    }
482  }
483
484  if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
485    // br(!x, t, f) -> br(x, f, t)
486    if (CondUOp->getOpcode() == UO_LNot)
487      return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
488  }
489
490  if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
491    // Handle ?: operator.
492
493    // Just ignore GNU ?: extension.
494    if (CondOp->getLHS()) {
495      // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
496      llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
497      llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
498      EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
499      EmitBlock(LHSBlock);
500      EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
501      EmitBlock(RHSBlock);
502      EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
503      return;
504    }
505  }
506
507  // Emit the code with the fully general case.
508  llvm::Value *CondV = EvaluateExprAsBool(Cond);
509  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
510}
511
512/// ErrorUnsupported - Print out an error that codegen doesn't support the
513/// specified stmt yet.
514void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
515                                       bool OmitOnError) {
516  CGM.ErrorUnsupported(S, Type, OmitOnError);
517}
518
519void
520CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
521  // Ignore empty classes in C++.
522  if (getContext().getLangOptions().CPlusPlus) {
523    if (const RecordType *RT = Ty->getAs<RecordType>()) {
524      if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
525        return;
526    }
527  }
528
529  // Cast the dest ptr to the appropriate i8 pointer type.
530  unsigned DestAS =
531    cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
532  const llvm::Type *BP =
533    llvm::Type::getInt8PtrTy(VMContext, DestAS);
534  if (DestPtr->getType() != BP)
535    DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
536
537  // Get size and alignment info for this aggregate.
538  std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
539  uint64_t Size = TypeInfo.first;
540  unsigned Align = TypeInfo.second;
541
542  // Don't bother emitting a zero-byte memset.
543  if (Size == 0)
544    return;
545
546  llvm::ConstantInt *SizeVal = llvm::ConstantInt::get(IntPtrTy, Size / 8);
547  llvm::ConstantInt *AlignVal = Builder.getInt32(Align / 8);
548
549  // If the type contains a pointer to data member we can't memset it to zero.
550  // Instead, create a null constant and copy it to the destination.
551  if (!CGM.getTypes().isZeroInitializable(Ty)) {
552    llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
553
554    llvm::GlobalVariable *NullVariable =
555      new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
556                               /*isConstant=*/true,
557                               llvm::GlobalVariable::PrivateLinkage,
558                               NullConstant, llvm::Twine());
559    llvm::Value *SrcPtr =
560      Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
561
562    // FIXME: variable-size types?
563
564    // Get and call the appropriate llvm.memcpy overload.
565    llvm::Constant *Memcpy =
566      CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(), IntPtrTy);
567    Builder.CreateCall5(Memcpy, DestPtr, SrcPtr, SizeVal, AlignVal,
568                        /*volatile*/ Builder.getFalse());
569    return;
570  }
571
572  // Otherwise, just memset the whole thing to zero.  This is legal
573  // because in LLVM, all default initializers (other than the ones we just
574  // handled above) are guaranteed to have a bit pattern of all zeros.
575
576  // FIXME: Handle variable sized types.
577  Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtrTy), DestPtr,
578                      Builder.getInt8(0),
579                      SizeVal, AlignVal, /*volatile*/ Builder.getFalse());
580}
581
582llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) {
583  // Make sure that there is a block for the indirect goto.
584  if (IndirectBranch == 0)
585    GetIndirectGotoBlock();
586
587  llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
588
589  // Make sure the indirect branch includes all of the address-taken blocks.
590  IndirectBranch->addDestination(BB);
591  return llvm::BlockAddress::get(CurFn, BB);
592}
593
594llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
595  // If we already made the indirect branch for indirect goto, return its block.
596  if (IndirectBranch) return IndirectBranch->getParent();
597
598  CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
599
600  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
601
602  // Create the PHI node that indirect gotos will add entries to.
603  llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest");
604
605  // Create the indirect branch instruction.
606  IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
607  return IndirectBranch->getParent();
608}
609
610llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
611  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
612
613  assert(SizeEntry && "Did not emit size for type");
614  return SizeEntry;
615}
616
617llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
618  assert(Ty->isVariablyModifiedType() &&
619         "Must pass variably modified type to EmitVLASizes!");
620
621  EnsureInsertPoint();
622
623  if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
624    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
625
626    if (!SizeEntry) {
627      const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
628
629      // Get the element size;
630      QualType ElemTy = VAT->getElementType();
631      llvm::Value *ElemSize;
632      if (ElemTy->isVariableArrayType())
633        ElemSize = EmitVLASize(ElemTy);
634      else
635        ElemSize = llvm::ConstantInt::get(SizeTy,
636            getContext().getTypeSizeInChars(ElemTy).getQuantity());
637
638      llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
639      NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
640
641      SizeEntry = Builder.CreateMul(ElemSize, NumElements);
642    }
643
644    return SizeEntry;
645  }
646
647  if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
648    EmitVLASize(AT->getElementType());
649    return 0;
650  }
651
652  const PointerType *PT = Ty->getAs<PointerType>();
653  assert(PT && "unknown VM type!");
654  EmitVLASize(PT->getPointeeType());
655  return 0;
656}
657
658llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
659  if (CGM.getContext().getBuiltinVaListType()->isArrayType())
660    return EmitScalarExpr(E);
661  return EmitLValue(E).getAddress();
662}
663
664/// Pops cleanup blocks until the given savepoint is reached.
665void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) {
666  assert(Old.isValid());
667
668  while (EHStack.stable_begin() != Old) {
669    EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
670
671    // As long as Old strictly encloses the scope's enclosing normal
672    // cleanup, we're going to emit another normal cleanup which
673    // fallthrough can propagate through.
674    bool FallThroughIsBranchThrough =
675      Old.strictlyEncloses(Scope.getEnclosingNormalCleanup());
676
677    PopCleanupBlock(FallThroughIsBranchThrough);
678  }
679}
680
681static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF,
682                                           EHCleanupScope &Scope) {
683  assert(Scope.isNormalCleanup());
684  llvm::BasicBlock *Entry = Scope.getNormalBlock();
685  if (!Entry) {
686    Entry = CGF.createBasicBlock("cleanup");
687    Scope.setNormalBlock(Entry);
688  }
689  return Entry;
690}
691
692static llvm::BasicBlock *CreateEHEntry(CodeGenFunction &CGF,
693                                       EHCleanupScope &Scope) {
694  assert(Scope.isEHCleanup());
695  llvm::BasicBlock *Entry = Scope.getEHBlock();
696  if (!Entry) {
697    Entry = CGF.createBasicBlock("eh.cleanup");
698    Scope.setEHBlock(Entry);
699  }
700  return Entry;
701}
702
703/// Transitions the terminator of the given exit-block of a cleanup to
704/// be a cleanup switch.
705static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF,
706                                                   llvm::BasicBlock *Block) {
707  // If it's a branch, turn it into a switch whose default
708  // destination is its original target.
709  llvm::TerminatorInst *Term = Block->getTerminator();
710  assert(Term && "can't transition block without terminator");
711
712  if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
713    assert(Br->isUnconditional());
714    llvm::LoadInst *Load =
715      new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term);
716    llvm::SwitchInst *Switch =
717      llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block);
718    Br->eraseFromParent();
719    return Switch;
720  } else {
721    return cast<llvm::SwitchInst>(Term);
722  }
723}
724
725/// Attempts to reduce a cleanup's entry block to a fallthrough.  This
726/// is basically llvm::MergeBlockIntoPredecessor, except
727/// simplified/optimized for the tighter constraints on cleanup blocks.
728///
729/// Returns the new block, whatever it is.
730static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF,
731                                              llvm::BasicBlock *Entry) {
732  llvm::BasicBlock *Pred = Entry->getSinglePredecessor();
733  if (!Pred) return Entry;
734
735  llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator());
736  if (!Br || Br->isConditional()) return Entry;
737  assert(Br->getSuccessor(0) == Entry);
738
739  // If we were previously inserting at the end of the cleanup entry
740  // block, we'll need to continue inserting at the end of the
741  // predecessor.
742  bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry;
743  assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end());
744
745  // Kill the branch.
746  Br->eraseFromParent();
747
748  // Merge the blocks.
749  Pred->getInstList().splice(Pred->end(), Entry->getInstList());
750
751  // Kill the entry block.
752  Entry->eraseFromParent();
753
754  if (WasInsertBlock)
755    CGF.Builder.SetInsertPoint(Pred);
756
757  return Pred;
758}
759
760static void EmitCleanup(CodeGenFunction &CGF,
761                        EHScopeStack::Cleanup *Fn,
762                        bool ForEH,
763                        llvm::Value *ActiveFlag) {
764  // EH cleanups always occur within a terminate scope.
765  if (ForEH) CGF.EHStack.pushTerminate();
766
767  // If there's an active flag, load it and skip the cleanup if it's
768  // false.
769  llvm::BasicBlock *ContBB = 0;
770  if (ActiveFlag) {
771    ContBB = CGF.createBasicBlock("cleanup.done");
772    llvm::BasicBlock *CleanupBB = CGF.createBasicBlock("cleanup.action");
773    llvm::Value *IsActive
774      = CGF.Builder.CreateLoad(ActiveFlag, "cleanup.is_active");
775    CGF.Builder.CreateCondBr(IsActive, CleanupBB, ContBB);
776    CGF.EmitBlock(CleanupBB);
777  }
778
779  // Ask the cleanup to emit itself.
780  Fn->Emit(CGF, ForEH);
781  assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?");
782
783  // Emit the continuation block if there was an active flag.
784  if (ActiveFlag)
785    CGF.EmitBlock(ContBB);
786
787  // Leave the terminate scope.
788  if (ForEH) CGF.EHStack.popTerminate();
789}
790
791static void ForwardPrebranchedFallthrough(llvm::BasicBlock *Exit,
792                                          llvm::BasicBlock *From,
793                                          llvm::BasicBlock *To) {
794  // Exit is the exit block of a cleanup, so it always terminates in
795  // an unconditional branch or a switch.
796  llvm::TerminatorInst *Term = Exit->getTerminator();
797
798  if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) {
799    assert(Br->isUnconditional() && Br->getSuccessor(0) == From);
800    Br->setSuccessor(0, To);
801  } else {
802    llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Term);
803    for (unsigned I = 0, E = Switch->getNumSuccessors(); I != E; ++I)
804      if (Switch->getSuccessor(I) == From)
805        Switch->setSuccessor(I, To);
806  }
807}
808
809/// Pops a cleanup block.  If the block includes a normal cleanup, the
810/// current insertion point is threaded through the cleanup, as are
811/// any branch fixups on the cleanup.
812void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
813  assert(!EHStack.empty() && "cleanup stack is empty!");
814  assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!");
815  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin());
816  assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups());
817
818  // Remember activation information.
819  bool IsActive = Scope.isActive();
820  llvm::Value *NormalActiveFlag =
821    Scope.shouldTestFlagInNormalCleanup() ? Scope.getActiveFlag() : 0;
822  llvm::Value *EHActiveFlag =
823    Scope.shouldTestFlagInEHCleanup() ? Scope.getActiveFlag() : 0;
824
825  // Check whether we need an EH cleanup.  This is only true if we've
826  // generated a lazy EH cleanup block.
827  bool RequiresEHCleanup = Scope.hasEHBranches();
828
829  // Check the three conditions which might require a normal cleanup:
830
831  // - whether there are branch fix-ups through this cleanup
832  unsigned FixupDepth = Scope.getFixupDepth();
833  bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth;
834
835  // - whether there are branch-throughs or branch-afters
836  bool HasExistingBranches = Scope.hasBranches();
837
838  // - whether there's a fallthrough
839  llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock();
840  bool HasFallthrough = (FallthroughSource != 0 && IsActive);
841
842  // As a kindof crazy internal case, branch-through fall-throughs
843  // leave the insertion point set to the end of the last cleanup.
844  bool HasPrebranchedFallthrough =
845    (FallthroughSource && FallthroughSource->getTerminator());
846
847  bool RequiresNormalCleanup = false;
848  if (Scope.isNormalCleanup() &&
849      (HasFixups || HasExistingBranches || HasFallthrough)) {
850    RequiresNormalCleanup = true;
851  }
852
853  assert(!HasPrebranchedFallthrough || RequiresNormalCleanup || !IsActive);
854  assert(!HasPrebranchedFallthrough ||
855         (Scope.isNormalCleanup() && Scope.getNormalBlock() &&
856          FallthroughSource->getTerminator()->getSuccessor(0)
857            == Scope.getNormalBlock()));
858
859  // Even if we don't need the normal cleanup, we might still have
860  // prebranched fallthrough to worry about.
861  if (!RequiresNormalCleanup && HasPrebranchedFallthrough) {
862    assert(!IsActive);
863
864    llvm::BasicBlock *NormalEntry = Scope.getNormalBlock();
865
866    // If we're branching through this cleanup, just forward the
867    // prebranched fallthrough to the next cleanup, leaving the insert
868    // point in the old block.
869    if (FallthroughIsBranchThrough) {
870      EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
871      llvm::BasicBlock *EnclosingEntry =
872        CreateNormalEntry(*this, cast<EHCleanupScope>(S));
873
874      ForwardPrebranchedFallthrough(FallthroughSource,
875                                    NormalEntry, EnclosingEntry);
876      assert(NormalEntry->use_empty() &&
877             "uses of entry remain after forwarding?");
878      delete NormalEntry;
879
880    // Otherwise, we're branching out;  just emit the next block.
881    } else {
882      EmitBlock(NormalEntry);
883      SimplifyCleanupEntry(*this, NormalEntry);
884    }
885  }
886
887  // If we don't need the cleanup at all, we're done.
888  if (!RequiresNormalCleanup && !RequiresEHCleanup) {
889    EHStack.popCleanup(); // safe because there are no fixups
890    assert(EHStack.getNumBranchFixups() == 0 ||
891           EHStack.hasNormalCleanups());
892    return;
893  }
894
895  // Copy the cleanup emission data out.  Note that SmallVector
896  // guarantees maximal alignment for its buffer regardless of its
897  // type parameter.
898  llvm::SmallVector<char, 8*sizeof(void*)> CleanupBuffer;
899  CleanupBuffer.reserve(Scope.getCleanupSize());
900  memcpy(CleanupBuffer.data(),
901         Scope.getCleanupBuffer(), Scope.getCleanupSize());
902  CleanupBuffer.set_size(Scope.getCleanupSize());
903  EHScopeStack::Cleanup *Fn =
904    reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data());
905
906  // We want to emit the EH cleanup after the normal cleanup, but go
907  // ahead and do the setup for the EH cleanup while the scope is still
908  // alive.
909  llvm::BasicBlock *EHEntry = 0;
910  llvm::SmallVector<llvm::Instruction*, 2> EHInstsToAppend;
911  if (RequiresEHCleanup) {
912    EHEntry = CreateEHEntry(*this, Scope);
913
914    // Figure out the branch-through dest if necessary.
915    llvm::BasicBlock *EHBranchThroughDest = 0;
916    if (Scope.hasEHBranchThroughs()) {
917      assert(Scope.getEnclosingEHCleanup() != EHStack.stable_end());
918      EHScope &S = *EHStack.find(Scope.getEnclosingEHCleanup());
919      EHBranchThroughDest = CreateEHEntry(*this, cast<EHCleanupScope>(S));
920    }
921
922    // If we have exactly one branch-after and no branch-throughs, we
923    // can dispatch it without a switch.
924    if (!Scope.hasEHBranchThroughs() &&
925        Scope.getNumEHBranchAfters() == 1) {
926      assert(!EHBranchThroughDest);
927
928      // TODO: remove the spurious eh.cleanup.dest stores if this edge
929      // never went through any switches.
930      llvm::BasicBlock *BranchAfterDest = Scope.getEHBranchAfterBlock(0);
931      EHInstsToAppend.push_back(llvm::BranchInst::Create(BranchAfterDest));
932
933    // Otherwise, if we have any branch-afters, we need a switch.
934    } else if (Scope.getNumEHBranchAfters()) {
935      // The default of the switch belongs to the branch-throughs if
936      // they exist.
937      llvm::BasicBlock *Default =
938        (EHBranchThroughDest ? EHBranchThroughDest : getUnreachableBlock());
939
940      const unsigned SwitchCapacity = Scope.getNumEHBranchAfters();
941
942      llvm::LoadInst *Load =
943        new llvm::LoadInst(getEHCleanupDestSlot(), "cleanup.dest");
944      llvm::SwitchInst *Switch =
945        llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
946
947      EHInstsToAppend.push_back(Load);
948      EHInstsToAppend.push_back(Switch);
949
950      for (unsigned I = 0, E = Scope.getNumEHBranchAfters(); I != E; ++I)
951        Switch->addCase(Scope.getEHBranchAfterIndex(I),
952                        Scope.getEHBranchAfterBlock(I));
953
954    // Otherwise, we have only branch-throughs; jump to the next EH
955    // cleanup.
956    } else {
957      assert(EHBranchThroughDest);
958      EHInstsToAppend.push_back(llvm::BranchInst::Create(EHBranchThroughDest));
959    }
960  }
961
962  if (!RequiresNormalCleanup) {
963    EHStack.popCleanup();
964  } else {
965    // If we have a fallthrough and no other need for the cleanup,
966    // emit it directly.
967    if (HasFallthrough && !HasPrebranchedFallthrough &&
968        !HasFixups && !HasExistingBranches) {
969
970      // Fixups can cause us to optimistically create a normal block,
971      // only to later have no real uses for it.  Just delete it in
972      // this case.
973      // TODO: we can potentially simplify all the uses after this.
974      if (Scope.getNormalBlock()) {
975        Scope.getNormalBlock()->replaceAllUsesWith(getUnreachableBlock());
976        delete Scope.getNormalBlock();
977      }
978
979      EHStack.popCleanup();
980
981      EmitCleanup(*this, Fn, /*ForEH*/ false, NormalActiveFlag);
982
983    // Otherwise, the best approach is to thread everything through
984    // the cleanup block and then try to clean up after ourselves.
985    } else {
986      // Force the entry block to exist.
987      llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope);
988
989      // I.  Set up the fallthrough edge in.
990
991      // If there's a fallthrough, we need to store the cleanup
992      // destination index.  For fall-throughs this is always zero.
993      if (HasFallthrough) {
994        if (!HasPrebranchedFallthrough)
995          Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot());
996
997      // Otherwise, clear the IP if we don't have fallthrough because
998      // the cleanup is inactive.  We don't need to save it because
999      // it's still just FallthroughSource.
1000      } else if (FallthroughSource) {
1001        assert(!IsActive && "source without fallthrough for active cleanup");
1002        Builder.ClearInsertionPoint();
1003      }
1004
1005      // II.  Emit the entry block.  This implicitly branches to it if
1006      // we have fallthrough.  All the fixups and existing branches
1007      // should already be branched to it.
1008      EmitBlock(NormalEntry);
1009
1010      // III.  Figure out where we're going and build the cleanup
1011      // epilogue.
1012
1013      bool HasEnclosingCleanups =
1014        (Scope.getEnclosingNormalCleanup() != EHStack.stable_end());
1015
1016      // Compute the branch-through dest if we need it:
1017      //   - if there are branch-throughs threaded through the scope
1018      //   - if fall-through is a branch-through
1019      //   - if there are fixups that will be optimistically forwarded
1020      //     to the enclosing cleanup
1021      llvm::BasicBlock *BranchThroughDest = 0;
1022      if (Scope.hasBranchThroughs() ||
1023          (FallthroughSource && FallthroughIsBranchThrough) ||
1024          (HasFixups && HasEnclosingCleanups)) {
1025        assert(HasEnclosingCleanups);
1026        EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup());
1027        BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S));
1028      }
1029
1030      llvm::BasicBlock *FallthroughDest = 0;
1031      llvm::SmallVector<llvm::Instruction*, 2> InstsToAppend;
1032
1033      // If there's exactly one branch-after and no other threads,
1034      // we can route it without a switch.
1035      if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough &&
1036          Scope.getNumBranchAfters() == 1) {
1037        assert(!BranchThroughDest || !IsActive);
1038
1039        // TODO: clean up the possibly dead stores to the cleanup dest slot.
1040        llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0);
1041        InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter));
1042
1043      // Build a switch-out if we need it:
1044      //   - if there are branch-afters threaded through the scope
1045      //   - if fall-through is a branch-after
1046      //   - if there are fixups that have nowhere left to go and
1047      //     so must be immediately resolved
1048      } else if (Scope.getNumBranchAfters() ||
1049                 (HasFallthrough && !FallthroughIsBranchThrough) ||
1050                 (HasFixups && !HasEnclosingCleanups)) {
1051
1052        llvm::BasicBlock *Default =
1053          (BranchThroughDest ? BranchThroughDest : getUnreachableBlock());
1054
1055        // TODO: base this on the number of branch-afters and fixups
1056        const unsigned SwitchCapacity = 10;
1057
1058        llvm::LoadInst *Load =
1059          new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest");
1060        llvm::SwitchInst *Switch =
1061          llvm::SwitchInst::Create(Load, Default, SwitchCapacity);
1062
1063        InstsToAppend.push_back(Load);
1064        InstsToAppend.push_back(Switch);
1065
1066        // Branch-after fallthrough.
1067        if (FallthroughSource && !FallthroughIsBranchThrough) {
1068          FallthroughDest = createBasicBlock("cleanup.cont");
1069          if (HasFallthrough)
1070            Switch->addCase(Builder.getInt32(0), FallthroughDest);
1071        }
1072
1073        for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) {
1074          Switch->addCase(Scope.getBranchAfterIndex(I),
1075                          Scope.getBranchAfterBlock(I));
1076        }
1077
1078        if (HasFixups && !HasEnclosingCleanups)
1079          ResolveAllBranchFixups(Switch);
1080      } else {
1081        // We should always have a branch-through destination in this case.
1082        assert(BranchThroughDest);
1083        InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest));
1084      }
1085
1086      // IV.  Pop the cleanup and emit it.
1087      EHStack.popCleanup();
1088      assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups);
1089
1090      EmitCleanup(*this, Fn, /*ForEH*/ false, NormalActiveFlag);
1091
1092      // Append the prepared cleanup prologue from above.
1093      llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
1094      for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
1095        NormalExit->getInstList().push_back(InstsToAppend[I]);
1096
1097      // Optimistically hope that any fixups will continue falling through.
1098      for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
1099           I < E; ++I) {
1100        BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I);
1101        if (!Fixup.Destination) continue;
1102        if (!Fixup.OptimisticBranchBlock) {
1103          new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex),
1104                              getNormalCleanupDestSlot(),
1105                              Fixup.InitialBranch);
1106          Fixup.InitialBranch->setSuccessor(0, NormalEntry);
1107        }
1108        Fixup.OptimisticBranchBlock = NormalExit;
1109      }
1110
1111      // V.  Set up the fallthrough edge out.
1112
1113      // Case 1: a fallthrough source exists but shouldn't branch to
1114      // the cleanup because the cleanup is inactive.
1115      if (!HasFallthrough && FallthroughSource) {
1116        assert(!IsActive);
1117
1118        // If we have a prebranched fallthrough, that needs to be
1119        // forwarded to the right block.
1120        if (HasPrebranchedFallthrough) {
1121          llvm::BasicBlock *Next;
1122          if (FallthroughIsBranchThrough) {
1123            Next = BranchThroughDest;
1124            assert(!FallthroughDest);
1125          } else {
1126            Next = FallthroughDest;
1127          }
1128
1129          ForwardPrebranchedFallthrough(FallthroughSource, NormalEntry, Next);
1130        }
1131        Builder.SetInsertPoint(FallthroughSource);
1132
1133      // Case 2: a fallthrough source exists and should branch to the
1134      // cleanup, but we're not supposed to branch through to the next
1135      // cleanup.
1136      } else if (HasFallthrough && FallthroughDest) {
1137        assert(!FallthroughIsBranchThrough);
1138        EmitBlock(FallthroughDest);
1139
1140      // Case 3: a fallthrough source exists and should branch to the
1141      // cleanup and then through to the next.
1142      } else if (HasFallthrough) {
1143        // Everything is already set up for this.
1144
1145      // Case 4: no fallthrough source exists.
1146      } else {
1147        Builder.ClearInsertionPoint();
1148      }
1149
1150      // VI.  Assorted cleaning.
1151
1152      // Check whether we can merge NormalEntry into a single predecessor.
1153      // This might invalidate (non-IR) pointers to NormalEntry.
1154      llvm::BasicBlock *NewNormalEntry =
1155        SimplifyCleanupEntry(*this, NormalEntry);
1156
1157      // If it did invalidate those pointers, and NormalEntry was the same
1158      // as NormalExit, go back and patch up the fixups.
1159      if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit)
1160        for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
1161               I < E; ++I)
1162          CGF.EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry;
1163    }
1164  }
1165
1166  assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0);
1167
1168  // Emit the EH cleanup if required.
1169  if (RequiresEHCleanup) {
1170    CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1171
1172    EmitBlock(EHEntry);
1173    EmitCleanup(*this, Fn, /*ForEH*/ true, EHActiveFlag);
1174
1175    // Append the prepared cleanup prologue from above.
1176    llvm::BasicBlock *EHExit = Builder.GetInsertBlock();
1177    for (unsigned I = 0, E = EHInstsToAppend.size(); I != E; ++I)
1178      EHExit->getInstList().push_back(EHInstsToAppend[I]);
1179
1180    Builder.restoreIP(SavedIP);
1181
1182    SimplifyCleanupEntry(*this, EHEntry);
1183  }
1184}
1185
1186/// Terminate the current block by emitting a branch which might leave
1187/// the current cleanup-protected scope.  The target scope may not yet
1188/// be known, in which case this will require a fixup.
1189///
1190/// As a side-effect, this method clears the insertion point.
1191void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
1192  assert(Dest.getScopeDepth().encloses(EHStack.getInnermostNormalCleanup())
1193         && "stale jump destination");
1194
1195  if (!HaveInsertPoint())
1196    return;
1197
1198  // Create the branch.
1199  llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
1200
1201  // Calculate the innermost active normal cleanup.
1202  EHScopeStack::stable_iterator
1203    TopCleanup = EHStack.getInnermostActiveNormalCleanup();
1204
1205  // If we're not in an active normal cleanup scope, or if the
1206  // destination scope is within the innermost active normal cleanup
1207  // scope, we don't need to worry about fixups.
1208  if (TopCleanup == EHStack.stable_end() ||
1209      TopCleanup.encloses(Dest.getScopeDepth())) { // works for invalid
1210    Builder.ClearInsertionPoint();
1211    return;
1212  }
1213
1214  // If we can't resolve the destination cleanup scope, just add this
1215  // to the current cleanup scope as a branch fixup.
1216  if (!Dest.getScopeDepth().isValid()) {
1217    BranchFixup &Fixup = EHStack.addBranchFixup();
1218    Fixup.Destination = Dest.getBlock();
1219    Fixup.DestinationIndex = Dest.getDestIndex();
1220    Fixup.InitialBranch = BI;
1221    Fixup.OptimisticBranchBlock = 0;
1222
1223    Builder.ClearInsertionPoint();
1224    return;
1225  }
1226
1227  // Otherwise, thread through all the normal cleanups in scope.
1228
1229  // Store the index at the start.
1230  llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1231  new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI);
1232
1233  // Adjust BI to point to the first cleanup block.
1234  {
1235    EHCleanupScope &Scope =
1236      cast<EHCleanupScope>(*EHStack.find(TopCleanup));
1237    BI->setSuccessor(0, CreateNormalEntry(*this, Scope));
1238  }
1239
1240  // Add this destination to all the scopes involved.
1241  EHScopeStack::stable_iterator I = TopCleanup;
1242  EHScopeStack::stable_iterator E = Dest.getScopeDepth();
1243  if (E.strictlyEncloses(I)) {
1244    while (true) {
1245      EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1246      assert(Scope.isNormalCleanup());
1247      I = Scope.getEnclosingNormalCleanup();
1248
1249      // If this is the last cleanup we're propagating through, tell it
1250      // that there's a resolved jump moving through it.
1251      if (!E.strictlyEncloses(I)) {
1252        Scope.addBranchAfter(Index, Dest.getBlock());
1253        break;
1254      }
1255
1256      // Otherwise, tell the scope that there's a jump propoagating
1257      // through it.  If this isn't new information, all the rest of
1258      // the work has been done before.
1259      if (!Scope.addBranchThrough(Dest.getBlock()))
1260        break;
1261    }
1262  }
1263
1264  Builder.ClearInsertionPoint();
1265}
1266
1267void CodeGenFunction::EmitBranchThroughEHCleanup(UnwindDest Dest) {
1268  // We should never get invalid scope depths for an UnwindDest; that
1269  // implies that the destination wasn't set up correctly.
1270  assert(Dest.getScopeDepth().isValid() && "invalid scope depth on EH dest?");
1271
1272  if (!HaveInsertPoint())
1273    return;
1274
1275  // Create the branch.
1276  llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
1277
1278  // Calculate the innermost active cleanup.
1279  EHScopeStack::stable_iterator
1280    InnermostCleanup = EHStack.getInnermostActiveEHCleanup();
1281
1282  // If the destination is in the same EH cleanup scope as us, we
1283  // don't need to thread through anything.
1284  if (InnermostCleanup.encloses(Dest.getScopeDepth())) {
1285    Builder.ClearInsertionPoint();
1286    return;
1287  }
1288  assert(InnermostCleanup != EHStack.stable_end());
1289
1290  // Store the index at the start.
1291  llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex());
1292  new llvm::StoreInst(Index, getEHCleanupDestSlot(), BI);
1293
1294  // Adjust BI to point to the first cleanup block.
1295  {
1296    EHCleanupScope &Scope =
1297      cast<EHCleanupScope>(*EHStack.find(InnermostCleanup));
1298    BI->setSuccessor(0, CreateEHEntry(*this, Scope));
1299  }
1300
1301  // Add this destination to all the scopes involved.
1302  for (EHScopeStack::stable_iterator
1303         I = InnermostCleanup, E = Dest.getScopeDepth(); ; ) {
1304    assert(E.strictlyEncloses(I));
1305    EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I));
1306    assert(Scope.isEHCleanup());
1307    I = Scope.getEnclosingEHCleanup();
1308
1309    // If this is the last cleanup we're propagating through, add this
1310    // as a branch-after.
1311    if (I == E) {
1312      Scope.addEHBranchAfter(Index, Dest.getBlock());
1313      break;
1314    }
1315
1316    // Otherwise, add it as a branch-through.  If this isn't new
1317    // information, all the rest of the work has been done before.
1318    if (!Scope.addEHBranchThrough(Dest.getBlock()))
1319      break;
1320  }
1321
1322  Builder.ClearInsertionPoint();
1323}
1324
1325/// All the branch fixups on the EH stack have propagated out past the
1326/// outermost normal cleanup; resolve them all by adding cases to the
1327/// given switch instruction.
1328void CodeGenFunction::ResolveAllBranchFixups(llvm::SwitchInst *Switch) {
1329  llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded;
1330
1331  for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
1332    // Skip this fixup if its destination isn't set or if we've
1333    // already treated it.
1334    BranchFixup &Fixup = EHStack.getBranchFixup(I);
1335    if (Fixup.Destination == 0) continue;
1336    if (!CasesAdded.insert(Fixup.Destination)) continue;
1337
1338    Switch->addCase(Builder.getInt32(Fixup.DestinationIndex),
1339                    Fixup.Destination);
1340  }
1341
1342  EHStack.clearFixups();
1343}
1344
1345void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) {
1346  assert(Block && "resolving a null target block");
1347  if (!EHStack.getNumBranchFixups()) return;
1348
1349  assert(EHStack.hasNormalCleanups() &&
1350         "branch fixups exist with no normal cleanups on stack");
1351
1352  llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks;
1353  bool ResolvedAny = false;
1354
1355  for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) {
1356    // Skip this fixup if its destination doesn't match.
1357    BranchFixup &Fixup = EHStack.getBranchFixup(I);
1358    if (Fixup.Destination != Block) continue;
1359
1360    Fixup.Destination = 0;
1361    ResolvedAny = true;
1362
1363    // If it doesn't have an optimistic branch block, LatestBranch is
1364    // already pointing to the right place.
1365    llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock;
1366    if (!BranchBB)
1367      continue;
1368
1369    // Don't process the same optimistic branch block twice.
1370    if (!ModifiedOptimisticBlocks.insert(BranchBB))
1371      continue;
1372
1373    llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB);
1374
1375    // Add a case to the switch.
1376    Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block);
1377  }
1378
1379  if (ResolvedAny)
1380    EHStack.popNullFixups();
1381}
1382
1383static bool IsUsedAsNormalCleanup(EHScopeStack &EHStack,
1384                                  EHScopeStack::stable_iterator C) {
1385  // If we needed a normal block for any reason, that counts.
1386  if (cast<EHCleanupScope>(*EHStack.find(C)).getNormalBlock())
1387    return true;
1388
1389  // Check whether any enclosed cleanups were needed.
1390  for (EHScopeStack::stable_iterator
1391         I = EHStack.getInnermostNormalCleanup();
1392         I != C; ) {
1393    assert(C.strictlyEncloses(I));
1394    EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1395    if (S.getNormalBlock()) return true;
1396    I = S.getEnclosingNormalCleanup();
1397  }
1398
1399  return false;
1400}
1401
1402static bool IsUsedAsEHCleanup(EHScopeStack &EHStack,
1403                              EHScopeStack::stable_iterator C) {
1404  // If we needed an EH block for any reason, that counts.
1405  if (cast<EHCleanupScope>(*EHStack.find(C)).getEHBlock())
1406    return true;
1407
1408  // Check whether any enclosed cleanups were needed.
1409  for (EHScopeStack::stable_iterator
1410         I = EHStack.getInnermostEHCleanup(); I != C; ) {
1411    assert(C.strictlyEncloses(I));
1412    EHCleanupScope &S = cast<EHCleanupScope>(*EHStack.find(I));
1413    if (S.getEHBlock()) return true;
1414    I = S.getEnclosingEHCleanup();
1415  }
1416
1417  return false;
1418}
1419
1420enum ForActivation_t {
1421  ForActivation,
1422  ForDeactivation
1423};
1424
1425/// The given cleanup block is changing activation state.  Configure a
1426/// cleanup variable if necessary.
1427///
1428/// It would be good if we had some way of determining if there were
1429/// extra uses *after* the change-over point.
1430static void SetupCleanupBlockActivation(CodeGenFunction &CGF,
1431                                        EHScopeStack::stable_iterator C,
1432                                        ForActivation_t Kind) {
1433  EHCleanupScope &Scope = cast<EHCleanupScope>(*CGF.EHStack.find(C));
1434
1435  // We always need the flag if we're activating the cleanup, because
1436  // we have to assume that the current location doesn't necessarily
1437  // dominate all future uses of the cleanup.
1438  bool NeedFlag = (Kind == ForActivation);
1439
1440  // Calculate whether the cleanup was used:
1441
1442  //   - as a normal cleanup
1443  if (Scope.isNormalCleanup() && IsUsedAsNormalCleanup(CGF.EHStack, C)) {
1444    Scope.setTestFlagInNormalCleanup();
1445    NeedFlag = true;
1446  }
1447
1448  //  - as an EH cleanup
1449  if (Scope.isEHCleanup() && IsUsedAsEHCleanup(CGF.EHStack, C)) {
1450    Scope.setTestFlagInEHCleanup();
1451    NeedFlag = true;
1452  }
1453
1454  // If it hasn't yet been used as either, we're done.
1455  if (!NeedFlag) return;
1456
1457  llvm::AllocaInst *Var = Scope.getActiveFlag();
1458  if (!Var) {
1459    Var = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "cleanup.isactive");
1460    Scope.setActiveFlag(Var);
1461
1462    // Initialize to true or false depending on whether it was
1463    // active up to this point.
1464    CGF.InitTempAlloca(Var, CGF.Builder.getInt1(Kind == ForDeactivation));
1465  }
1466
1467  CGF.Builder.CreateStore(CGF.Builder.getInt1(Kind == ForActivation), Var);
1468}
1469
1470/// Activate a cleanup that was created in an inactivated state.
1471void CodeGenFunction::ActivateCleanupBlock(EHScopeStack::stable_iterator C) {
1472  assert(C != EHStack.stable_end() && "activating bottom of stack?");
1473  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1474  assert(!Scope.isActive() && "double activation");
1475
1476  SetupCleanupBlockActivation(*this, C, ForActivation);
1477
1478  Scope.setActive(true);
1479}
1480
1481/// Deactive a cleanup that was created in an active state.
1482void CodeGenFunction::DeactivateCleanupBlock(EHScopeStack::stable_iterator C) {
1483  assert(C != EHStack.stable_end() && "deactivating bottom of stack?");
1484  EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(C));
1485  assert(Scope.isActive() && "double deactivation");
1486
1487  // If it's the top of the stack, just pop it.
1488  if (C == EHStack.stable_begin()) {
1489    // If it's a normal cleanup, we need to pretend that the
1490    // fallthrough is unreachable.
1491    CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
1492    PopCleanupBlock();
1493    Builder.restoreIP(SavedIP);
1494    return;
1495  }
1496
1497  // Otherwise, follow the general case.
1498  SetupCleanupBlockActivation(*this, C, ForDeactivation);
1499
1500  Scope.setActive(false);
1501}
1502
1503llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() {
1504  if (!NormalCleanupDest)
1505    NormalCleanupDest =
1506      CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot");
1507  return NormalCleanupDest;
1508}
1509
1510llvm::Value *CodeGenFunction::getEHCleanupDestSlot() {
1511  if (!EHCleanupDest)
1512    EHCleanupDest =
1513      CreateTempAlloca(Builder.getInt32Ty(), "eh.cleanup.dest.slot");
1514  return EHCleanupDest;
1515}
1516
1517void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
1518                                              llvm::ConstantInt *Init) {
1519  assert (Init && "Invalid DeclRefExpr initializer!");
1520  if (CGDebugInfo *Dbg = getDebugInfo())
1521    Dbg->EmitGlobalVariable(E->getDecl(), Init, Builder);
1522}
1523