CGDecl.cpp revision 71cba34b6e2d3fb81860bd2176ab7003eaf2e918
1//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
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 contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
17#include "CGOpenCLRuntime.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/CharUnits.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Frontend/CodeGenOptions.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Type.h"
29using namespace clang;
30using namespace CodeGen;
31
32
33void CodeGenFunction::EmitDecl(const Decl &D) {
34  switch (D.getKind()) {
35  case Decl::TranslationUnit:
36  case Decl::Namespace:
37  case Decl::UnresolvedUsingTypename:
38  case Decl::ClassTemplateSpecialization:
39  case Decl::ClassTemplatePartialSpecialization:
40  case Decl::TemplateTypeParm:
41  case Decl::UnresolvedUsingValue:
42  case Decl::NonTypeTemplateParm:
43  case Decl::CXXMethod:
44  case Decl::CXXConstructor:
45  case Decl::CXXDestructor:
46  case Decl::CXXConversion:
47  case Decl::Field:
48  case Decl::IndirectField:
49  case Decl::ObjCIvar:
50  case Decl::ObjCAtDefsField:
51  case Decl::ParmVar:
52  case Decl::ImplicitParam:
53  case Decl::ClassTemplate:
54  case Decl::FunctionTemplate:
55  case Decl::TypeAliasTemplate:
56  case Decl::TemplateTemplateParm:
57  case Decl::ObjCMethod:
58  case Decl::ObjCCategory:
59  case Decl::ObjCProtocol:
60  case Decl::ObjCInterface:
61  case Decl::ObjCCategoryImpl:
62  case Decl::ObjCImplementation:
63  case Decl::ObjCProperty:
64  case Decl::ObjCCompatibleAlias:
65  case Decl::AccessSpec:
66  case Decl::LinkageSpec:
67  case Decl::ObjCPropertyImpl:
68  case Decl::FileScopeAsm:
69  case Decl::Friend:
70  case Decl::FriendTemplate:
71  case Decl::Block:
72  case Decl::ClassScopeFunctionSpecialization:
73    llvm_unreachable("Declaration should not be in declstmts!");
74  case Decl::Function:  // void X();
75  case Decl::Record:    // struct/union/class X;
76  case Decl::Enum:      // enum X;
77  case Decl::EnumConstant: // enum ? { X = ? }
78  case Decl::CXXRecord: // struct/union/class X; [C++]
79  case Decl::Using:          // using X; [C++]
80  case Decl::UsingShadow:
81  case Decl::UsingDirective: // using namespace X; [C++]
82  case Decl::NamespaceAlias:
83  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
84  case Decl::Label:        // __label__ x;
85  case Decl::Import:
86    // None of these decls require codegen support.
87    return;
88
89  case Decl::Var: {
90    const VarDecl &VD = cast<VarDecl>(D);
91    assert(VD.isLocalVarDecl() &&
92           "Should not see file-scope variables inside a function!");
93    return EmitVarDecl(VD);
94  }
95
96  case Decl::Typedef:      // typedef int X;
97  case Decl::TypeAlias: {  // using X = int; [C++0x]
98    const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
99    QualType Ty = TD.getUnderlyingType();
100
101    if (Ty->isVariablyModifiedType())
102      EmitVariablyModifiedType(Ty);
103  }
104  }
105}
106
107/// EmitVarDecl - This method handles emission of any variable declaration
108/// inside a function, including static vars etc.
109void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
110  switch (D.getStorageClass()) {
111  case SC_None:
112  case SC_Auto:
113  case SC_Register:
114    return EmitAutoVarDecl(D);
115  case SC_Static: {
116    llvm::GlobalValue::LinkageTypes Linkage =
117      llvm::GlobalValue::InternalLinkage;
118
119    // If the function definition has some sort of weak linkage, its
120    // static variables should also be weak so that they get properly
121    // uniqued.  We can't do this in C, though, because there's no
122    // standard way to agree on which variables are the same (i.e.
123    // there's no mangling).
124    if (getContext().getLangOptions().CPlusPlus)
125      if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
126        Linkage = CurFn->getLinkage();
127
128    return EmitStaticVarDecl(D, Linkage);
129  }
130  case SC_Extern:
131  case SC_PrivateExtern:
132    // Don't emit it now, allow it to be emitted lazily on its first use.
133    return;
134  case SC_OpenCLWorkGroupLocal:
135    return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D);
136  }
137
138  llvm_unreachable("Unknown storage class");
139}
140
141static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
142                                     const char *Separator) {
143  CodeGenModule &CGM = CGF.CGM;
144  if (CGF.getContext().getLangOptions().CPlusPlus) {
145    StringRef Name = CGM.getMangledName(&D);
146    return Name.str();
147  }
148
149  std::string ContextName;
150  if (!CGF.CurFuncDecl) {
151    // Better be in a block declared in global scope.
152    const NamedDecl *ND = cast<NamedDecl>(&D);
153    const DeclContext *DC = ND->getDeclContext();
154    if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
155      MangleBuffer Name;
156      CGM.getBlockMangledName(GlobalDecl(), Name, BD);
157      ContextName = Name.getString();
158    }
159    else
160      llvm_unreachable("Unknown context for block static var decl");
161  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
162    StringRef Name = CGM.getMangledName(FD);
163    ContextName = Name.str();
164  } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
165    ContextName = CGF.CurFn->getName();
166  else
167    llvm_unreachable("Unknown context for static var decl");
168
169  return ContextName + Separator + D.getNameAsString();
170}
171
172llvm::GlobalVariable *
173CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
174                                     const char *Separator,
175                                     llvm::GlobalValue::LinkageTypes Linkage) {
176  QualType Ty = D.getType();
177  assert(Ty->isConstantSizeType() && "VLAs can't be static");
178
179  // Use the label if the variable is renamed with the asm-label extension.
180  std::string Name;
181  if (D.hasAttr<AsmLabelAttr>())
182    Name = CGM.getMangledName(&D);
183  else
184    Name = GetStaticDeclName(*this, D, Separator);
185
186  llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
187  llvm::GlobalVariable *GV =
188    new llvm::GlobalVariable(CGM.getModule(), LTy,
189                             Ty.isConstant(getContext()), Linkage,
190                             CGM.EmitNullConstant(D.getType()), Name, 0,
191                             D.isThreadSpecified(),
192                             CGM.getContext().getTargetAddressSpace(Ty));
193  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
194  if (Linkage != llvm::GlobalValue::InternalLinkage)
195    GV->setVisibility(CurFn->getVisibility());
196  return GV;
197}
198
199/// hasNontrivialDestruction - Determine whether a type's destruction is
200/// non-trivial. If so, and the variable uses static initialization, we must
201/// register its destructor to run on exit.
202static bool hasNontrivialDestruction(QualType T) {
203  CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
204  return RD && !RD->hasTrivialDestructor();
205}
206
207/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
208/// global variable that has already been created for it.  If the initializer
209/// has a different type than GV does, this may free GV and return a different
210/// one.  Otherwise it just returns GV.
211llvm::GlobalVariable *
212CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
213                                               llvm::GlobalVariable *GV) {
214  llvm::Constant *Init = CGM.EmitConstantInit(D, this);
215
216  // If constant emission failed, then this should be a C++ static
217  // initializer.
218  if (!Init) {
219    if (!getContext().getLangOptions().CPlusPlus)
220      CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
221    else if (Builder.GetInsertBlock()) {
222      // Since we have a static initializer, this global variable can't
223      // be constant.
224      GV->setConstant(false);
225
226      EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
227    }
228    return GV;
229  }
230
231  // The initializer may differ in type from the global. Rewrite
232  // the global to match the initializer.  (We have to do this
233  // because some types, like unions, can't be completely represented
234  // in the LLVM type system.)
235  if (GV->getType()->getElementType() != Init->getType()) {
236    llvm::GlobalVariable *OldGV = GV;
237
238    GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
239                                  OldGV->isConstant(),
240                                  OldGV->getLinkage(), Init, "",
241                                  /*InsertBefore*/ OldGV,
242                                  D.isThreadSpecified(),
243                           CGM.getContext().getTargetAddressSpace(D.getType()));
244    GV->setVisibility(OldGV->getVisibility());
245
246    // Steal the name of the old global
247    GV->takeName(OldGV);
248
249    // Replace all uses of the old global with the new global
250    llvm::Constant *NewPtrForOldDecl =
251    llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
252    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
253
254    // Erase the old global, since it is no longer used.
255    OldGV->eraseFromParent();
256  }
257
258  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
259  GV->setInitializer(Init);
260
261  if (hasNontrivialDestruction(D.getType())) {
262    // We have a constant initializer, but a nontrivial destructor. We still
263    // need to perform a guarded "initialization" in order to register the
264    // destructor.
265    EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
266  }
267
268  return GV;
269}
270
271void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
272                                      llvm::GlobalValue::LinkageTypes Linkage) {
273  llvm::Value *&DMEntry = LocalDeclMap[&D];
274  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
275
276  llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage);
277
278  // Store into LocalDeclMap before generating initializer to handle
279  // circular references.
280  DMEntry = GV;
281
282  // We can't have a VLA here, but we can have a pointer to a VLA,
283  // even though that doesn't really make any sense.
284  // Make sure to evaluate VLA bounds now so that we have them for later.
285  if (D.getType()->isVariablyModifiedType())
286    EmitVariablyModifiedType(D.getType());
287
288  // Local static block variables must be treated as globals as they may be
289  // referenced in their RHS initializer block-literal expresion.
290  CGM.setStaticLocalDeclAddress(&D, GV);
291
292  // If this value has an initializer, emit it.
293  if (D.getInit())
294    GV = AddInitializerToStaticVarDecl(D, GV);
295
296  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
297
298  if (D.hasAttr<AnnotateAttr>())
299    CGM.AddGlobalAnnotations(&D, GV);
300
301  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
302    GV->setSection(SA->getName());
303
304  if (D.hasAttr<UsedAttr>())
305    CGM.AddUsedGlobal(GV);
306
307  // We may have to cast the constant because of the initializer
308  // mismatch above.
309  //
310  // FIXME: It is really dangerous to store this in the map; if anyone
311  // RAUW's the GV uses of this constant will be invalid.
312  llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
313  llvm::Type *LPtrTy =
314    LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType()));
315  llvm::Constant *CastedVal = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
316  DMEntry = CastedVal;
317  CGM.setStaticLocalDeclAddress(&D, CastedVal);
318
319  // Emit global variable debug descriptor for static vars.
320  CGDebugInfo *DI = getDebugInfo();
321  if (DI) {
322    DI->setLocation(D.getLocation());
323    DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
324  }
325}
326
327namespace {
328  struct DestroyObject : EHScopeStack::Cleanup {
329    DestroyObject(llvm::Value *addr, QualType type,
330                  CodeGenFunction::Destroyer *destroyer,
331                  bool useEHCleanupForArray)
332      : addr(addr), type(type), destroyer(destroyer),
333        useEHCleanupForArray(useEHCleanupForArray) {}
334
335    llvm::Value *addr;
336    QualType type;
337    CodeGenFunction::Destroyer *destroyer;
338    bool useEHCleanupForArray;
339
340    void Emit(CodeGenFunction &CGF, Flags flags) {
341      // Don't use an EH cleanup recursively from an EH cleanup.
342      bool useEHCleanupForArray =
343        flags.isForNormalCleanup() && this->useEHCleanupForArray;
344
345      CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
346    }
347  };
348
349  struct DestroyNRVOVariable : EHScopeStack::Cleanup {
350    DestroyNRVOVariable(llvm::Value *addr,
351                        const CXXDestructorDecl *Dtor,
352                        llvm::Value *NRVOFlag)
353      : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
354
355    const CXXDestructorDecl *Dtor;
356    llvm::Value *NRVOFlag;
357    llvm::Value *Loc;
358
359    void Emit(CodeGenFunction &CGF, Flags flags) {
360      // Along the exceptions path we always execute the dtor.
361      bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
362
363      llvm::BasicBlock *SkipDtorBB = 0;
364      if (NRVO) {
365        // If we exited via NRVO, we skip the destructor call.
366        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
367        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
368        llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
369        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
370        CGF.EmitBlock(RunDtorBB);
371      }
372
373      CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
374                                /*ForVirtualBase=*/false, Loc);
375
376      if (NRVO) CGF.EmitBlock(SkipDtorBB);
377    }
378  };
379
380  struct CallStackRestore : EHScopeStack::Cleanup {
381    llvm::Value *Stack;
382    CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
383    void Emit(CodeGenFunction &CGF, Flags flags) {
384      llvm::Value *V = CGF.Builder.CreateLoad(Stack);
385      llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
386      CGF.Builder.CreateCall(F, V);
387    }
388  };
389
390  struct ExtendGCLifetime : EHScopeStack::Cleanup {
391    const VarDecl &Var;
392    ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
393
394    void Emit(CodeGenFunction &CGF, Flags flags) {
395      // Compute the address of the local variable, in case it's a
396      // byref or something.
397      DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
398                      SourceLocation());
399      llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE));
400      CGF.EmitExtendGCLifetime(value);
401    }
402  };
403
404  struct CallCleanupFunction : EHScopeStack::Cleanup {
405    llvm::Constant *CleanupFn;
406    const CGFunctionInfo &FnInfo;
407    const VarDecl &Var;
408
409    CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
410                        const VarDecl *Var)
411      : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
412
413    void Emit(CodeGenFunction &CGF, Flags flags) {
414      DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
415                      SourceLocation());
416      // Compute the address of the local variable, in case it's a byref
417      // or something.
418      llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
419
420      // In some cases, the type of the function argument will be different from
421      // the type of the pointer. An example of this is
422      // void f(void* arg);
423      // __attribute__((cleanup(f))) void *g;
424      //
425      // To fix this we insert a bitcast here.
426      QualType ArgTy = FnInfo.arg_begin()->type;
427      llvm::Value *Arg =
428        CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
429
430      CallArgList Args;
431      Args.add(RValue::get(Arg),
432               CGF.getContext().getPointerType(Var.getType()));
433      CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
434    }
435  };
436}
437
438/// EmitAutoVarWithLifetime - Does the setup required for an automatic
439/// variable with lifetime.
440static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
441                                    llvm::Value *addr,
442                                    Qualifiers::ObjCLifetime lifetime) {
443  switch (lifetime) {
444  case Qualifiers::OCL_None:
445    llvm_unreachable("present but none");
446
447  case Qualifiers::OCL_ExplicitNone:
448    // nothing to do
449    break;
450
451  case Qualifiers::OCL_Strong: {
452    CodeGenFunction::Destroyer *destroyer =
453      (var.hasAttr<ObjCPreciseLifetimeAttr>()
454       ? CodeGenFunction::destroyARCStrongPrecise
455       : CodeGenFunction::destroyARCStrongImprecise);
456
457    CleanupKind cleanupKind = CGF.getARCCleanupKind();
458    CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
459                    cleanupKind & EHCleanup);
460    break;
461  }
462  case Qualifiers::OCL_Autoreleasing:
463    // nothing to do
464    break;
465
466  case Qualifiers::OCL_Weak:
467    // __weak objects always get EH cleanups; otherwise, exceptions
468    // could cause really nasty crashes instead of mere leaks.
469    CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
470                    CodeGenFunction::destroyARCWeak,
471                    /*useEHCleanup*/ true);
472    break;
473  }
474}
475
476static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
477  if (const Expr *e = dyn_cast<Expr>(s)) {
478    // Skip the most common kinds of expressions that make
479    // hierarchy-walking expensive.
480    s = e = e->IgnoreParenCasts();
481
482    if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
483      return (ref->getDecl() == &var);
484  }
485
486  for (Stmt::const_child_range children = s->children(); children; ++children)
487    // children might be null; as in missing decl or conditional of an if-stmt.
488    if ((*children) && isAccessedBy(var, *children))
489      return true;
490
491  return false;
492}
493
494static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
495  if (!decl) return false;
496  if (!isa<VarDecl>(decl)) return false;
497  const VarDecl *var = cast<VarDecl>(decl);
498  return isAccessedBy(*var, e);
499}
500
501static void drillIntoBlockVariable(CodeGenFunction &CGF,
502                                   LValue &lvalue,
503                                   const VarDecl *var) {
504  lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
505}
506
507void CodeGenFunction::EmitScalarInit(const Expr *init,
508                                     const ValueDecl *D,
509                                     LValue lvalue,
510                                     bool capturedByInit) {
511  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
512  if (!lifetime) {
513    llvm::Value *value = EmitScalarExpr(init);
514    if (capturedByInit)
515      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
516    EmitStoreThroughLValue(RValue::get(value), lvalue, true);
517    return;
518  }
519
520  // If we're emitting a value with lifetime, we have to do the
521  // initialization *before* we leave the cleanup scopes.
522  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) {
523    enterFullExpression(ewc);
524    init = ewc->getSubExpr();
525  }
526  CodeGenFunction::RunCleanupsScope Scope(*this);
527
528  // We have to maintain the illusion that the variable is
529  // zero-initialized.  If the variable might be accessed in its
530  // initializer, zero-initialize before running the initializer, then
531  // actually perform the initialization with an assign.
532  bool accessedByInit = false;
533  if (lifetime != Qualifiers::OCL_ExplicitNone)
534    accessedByInit = (capturedByInit || isAccessedBy(D, init));
535  if (accessedByInit) {
536    LValue tempLV = lvalue;
537    // Drill down to the __block object if necessary.
538    if (capturedByInit) {
539      // We can use a simple GEP for this because it can't have been
540      // moved yet.
541      tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
542                                   getByRefValueLLVMField(cast<VarDecl>(D))));
543    }
544
545    llvm::PointerType *ty
546      = cast<llvm::PointerType>(tempLV.getAddress()->getType());
547    ty = cast<llvm::PointerType>(ty->getElementType());
548
549    llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
550
551    // If __weak, we want to use a barrier under certain conditions.
552    if (lifetime == Qualifiers::OCL_Weak)
553      EmitARCInitWeak(tempLV.getAddress(), zero);
554
555    // Otherwise just do a simple store.
556    else
557      EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
558  }
559
560  // Emit the initializer.
561  llvm::Value *value = 0;
562
563  switch (lifetime) {
564  case Qualifiers::OCL_None:
565    llvm_unreachable("present but none");
566
567  case Qualifiers::OCL_ExplicitNone:
568    // nothing to do
569    value = EmitScalarExpr(init);
570    break;
571
572  case Qualifiers::OCL_Strong: {
573    value = EmitARCRetainScalarExpr(init);
574    break;
575  }
576
577  case Qualifiers::OCL_Weak: {
578    // No way to optimize a producing initializer into this.  It's not
579    // worth optimizing for, because the value will immediately
580    // disappear in the common case.
581    value = EmitScalarExpr(init);
582
583    if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
584    if (accessedByInit)
585      EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
586    else
587      EmitARCInitWeak(lvalue.getAddress(), value);
588    return;
589  }
590
591  case Qualifiers::OCL_Autoreleasing:
592    value = EmitARCRetainAutoreleaseScalarExpr(init);
593    break;
594  }
595
596  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
597
598  // If the variable might have been accessed by its initializer, we
599  // might have to initialize with a barrier.  We have to do this for
600  // both __weak and __strong, but __weak got filtered out above.
601  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
602    llvm::Value *oldValue = EmitLoadOfScalar(lvalue);
603    EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
604    EmitARCRelease(oldValue, /*precise*/ false);
605    return;
606  }
607
608  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
609}
610
611/// EmitScalarInit - Initialize the given lvalue with the given object.
612void CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
613  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
614  if (!lifetime)
615    return EmitStoreThroughLValue(RValue::get(init), lvalue, true);
616
617  switch (lifetime) {
618  case Qualifiers::OCL_None:
619    llvm_unreachable("present but none");
620
621  case Qualifiers::OCL_ExplicitNone:
622    // nothing to do
623    break;
624
625  case Qualifiers::OCL_Strong:
626    init = EmitARCRetain(lvalue.getType(), init);
627    break;
628
629  case Qualifiers::OCL_Weak:
630    // Initialize and then skip the primitive store.
631    EmitARCInitWeak(lvalue.getAddress(), init);
632    return;
633
634  case Qualifiers::OCL_Autoreleasing:
635    init = EmitARCRetainAutorelease(lvalue.getType(), init);
636    break;
637  }
638
639  EmitStoreOfScalar(init, lvalue, /* isInitialization */ true);
640}
641
642/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
643/// non-zero parts of the specified initializer with equal or fewer than
644/// NumStores scalar stores.
645static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
646                                                unsigned &NumStores) {
647  // Zero and Undef never requires any extra stores.
648  if (isa<llvm::ConstantAggregateZero>(Init) ||
649      isa<llvm::ConstantPointerNull>(Init) ||
650      isa<llvm::UndefValue>(Init))
651    return true;
652  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
653      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
654      isa<llvm::ConstantExpr>(Init))
655    return Init->isNullValue() || NumStores--;
656
657  // See if we can emit each element.
658  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
659    for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
660      llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
661      if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
662        return false;
663    }
664    return true;
665  }
666
667  if (llvm::ConstantDataSequential *CDS =
668        dyn_cast<llvm::ConstantDataSequential>(Init)) {
669    for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
670      llvm::Constant *Elt = CDS->getElementAsConstant(i);
671      if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
672        return false;
673    }
674    return true;
675  }
676
677  // Anything else is hard and scary.
678  return false;
679}
680
681/// emitStoresForInitAfterMemset - For inits that
682/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
683/// stores that would be required.
684static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
685                                         bool isVolatile, CGBuilderTy &Builder) {
686  // Zero doesn't require a store.
687  if (Init->isNullValue() || isa<llvm::UndefValue>(Init))
688    return;
689
690  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
691      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
692      isa<llvm::ConstantExpr>(Init)) {
693    Builder.CreateStore(Init, Loc, isVolatile);
694    return;
695  }
696
697  if (llvm::ConstantDataSequential *CDS =
698        dyn_cast<llvm::ConstantDataSequential>(Init)) {
699    for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
700      llvm::Constant *Elt = CDS->getElementAsConstant(i);
701
702      // Get a pointer to the element and emit it.
703      emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
704                                   isVolatile, Builder);
705    }
706    return;
707  }
708
709  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
710         "Unknown value type!");
711
712  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
713    llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
714    // Get a pointer to the element and emit it.
715    emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
716                                 isVolatile, Builder);
717  }
718}
719
720
721/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
722/// plus some stores to initialize a local variable instead of using a memcpy
723/// from a constant global.  It is beneficial to use memset if the global is all
724/// zeros, or mostly zeros and large.
725static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
726                                                  uint64_t GlobalSize) {
727  // If a global is all zeros, always use a memset.
728  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
729
730
731  // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
732  // do it if it will require 6 or fewer scalar stores.
733  // TODO: Should budget depends on the size?  Avoiding a large global warrants
734  // plopping in more stores.
735  unsigned StoreBudget = 6;
736  uint64_t SizeLimit = 32;
737
738  return GlobalSize > SizeLimit &&
739         canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
740}
741
742
743/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
744/// variable declaration with auto, register, or no storage class specifier.
745/// These turn into simple stack objects, or GlobalValues depending on target.
746void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
747  AutoVarEmission emission = EmitAutoVarAlloca(D);
748  EmitAutoVarInit(emission);
749  EmitAutoVarCleanups(emission);
750}
751
752/// EmitAutoVarAlloca - Emit the alloca and debug information for a
753/// local variable.  Does not emit initalization or destruction.
754CodeGenFunction::AutoVarEmission
755CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
756  QualType Ty = D.getType();
757
758  AutoVarEmission emission(D);
759
760  bool isByRef = D.hasAttr<BlocksAttr>();
761  emission.IsByRef = isByRef;
762
763  CharUnits alignment = getContext().getDeclAlign(&D);
764  emission.Alignment = alignment;
765
766  // If the type is variably-modified, emit all the VLA sizes for it.
767  if (Ty->isVariablyModifiedType())
768    EmitVariablyModifiedType(Ty);
769
770  llvm::Value *DeclPtr;
771  if (Ty->isConstantSizeType()) {
772    if (!Target.useGlobalsForAutomaticVariables()) {
773      bool NRVO = getContext().getLangOptions().ElideConstructors &&
774                  D.isNRVOVariable();
775
776      // If this value is a POD array or struct with a statically
777      // determinable constant initializer, there are optimizations we can do.
778      //
779      // TODO: We should constant-evaluate the initializer of any variable,
780      // as long as it is initialized by a constant expression. Currently,
781      // isConstantInitializer produces wrong answers for structs with
782      // reference or bitfield members, and a few other cases, and checking
783      // for POD-ness protects us from some of these.
784      if (D.getInit() &&
785          (Ty->isArrayType() || Ty->isRecordType()) &&
786          (Ty.isPODType(getContext()) ||
787           getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
788          D.getInit()->isConstantInitializer(getContext(), false)) {
789
790        // If the variable's a const type, and it's neither an NRVO
791        // candidate nor a __block variable and has no mutable members,
792        // emit it as a global instead.
793        if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef &&
794            CGM.isTypeConstant(Ty, true)) {
795          EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
796
797          emission.Address = 0; // signal this condition to later callbacks
798          assert(emission.wasEmittedAsGlobal());
799          return emission;
800        }
801
802        // Otherwise, tell the initialization code that we're in this case.
803        emission.IsConstantAggregate = true;
804      }
805
806      // A normal fixed sized variable becomes an alloca in the entry block,
807      // unless it's an NRVO variable.
808      llvm::Type *LTy = ConvertTypeForMem(Ty);
809
810      if (NRVO) {
811        // The named return value optimization: allocate this variable in the
812        // return slot, so that we can elide the copy when returning this
813        // variable (C++0x [class.copy]p34).
814        DeclPtr = ReturnValue;
815
816        if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
817          if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
818            // Create a flag that is used to indicate when the NRVO was applied
819            // to this variable. Set it to zero to indicate that NRVO was not
820            // applied.
821            llvm::Value *Zero = Builder.getFalse();
822            llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
823            EnsureInsertPoint();
824            Builder.CreateStore(Zero, NRVOFlag);
825
826            // Record the NRVO flag for this variable.
827            NRVOFlags[&D] = NRVOFlag;
828            emission.NRVOFlag = NRVOFlag;
829          }
830        }
831      } else {
832        if (isByRef)
833          LTy = BuildByRefType(&D);
834
835        llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
836        Alloc->setName(D.getName());
837
838        CharUnits allocaAlignment = alignment;
839        if (isByRef)
840          allocaAlignment = std::max(allocaAlignment,
841              getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
842        Alloc->setAlignment(allocaAlignment.getQuantity());
843        DeclPtr = Alloc;
844      }
845    } else {
846      // Targets that don't support recursion emit locals as globals.
847      const char *Class =
848        D.getStorageClass() == SC_Register ? ".reg." : ".auto.";
849      DeclPtr = CreateStaticVarDecl(D, Class,
850                                    llvm::GlobalValue::InternalLinkage);
851    }
852  } else {
853    EnsureInsertPoint();
854
855    if (!DidCallStackSave) {
856      // Save the stack.
857      llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
858
859      llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
860      llvm::Value *V = Builder.CreateCall(F);
861
862      Builder.CreateStore(V, Stack);
863
864      DidCallStackSave = true;
865
866      // Push a cleanup block and restore the stack there.
867      // FIXME: in general circumstances, this should be an EH cleanup.
868      EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
869    }
870
871    llvm::Value *elementCount;
872    QualType elementType;
873    llvm::tie(elementCount, elementType) = getVLASize(Ty);
874
875    llvm::Type *llvmTy = ConvertTypeForMem(elementType);
876
877    // Allocate memory for the array.
878    llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
879    vla->setAlignment(alignment.getQuantity());
880
881    DeclPtr = vla;
882  }
883
884  llvm::Value *&DMEntry = LocalDeclMap[&D];
885  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
886  DMEntry = DeclPtr;
887  emission.Address = DeclPtr;
888
889  // Emit debug info for local var declaration.
890  if (HaveInsertPoint())
891    if (CGDebugInfo *DI = getDebugInfo()) {
892      DI->setLocation(D.getLocation());
893      if (Target.useGlobalsForAutomaticVariables()) {
894        DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
895      } else
896        DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
897    }
898
899  if (D.hasAttr<AnnotateAttr>())
900      EmitVarAnnotations(&D, emission.Address);
901
902  return emission;
903}
904
905/// Determines whether the given __block variable is potentially
906/// captured by the given expression.
907static bool isCapturedBy(const VarDecl &var, const Expr *e) {
908  // Skip the most common kinds of expressions that make
909  // hierarchy-walking expensive.
910  e = e->IgnoreParenCasts();
911
912  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
913    const BlockDecl *block = be->getBlockDecl();
914    for (BlockDecl::capture_const_iterator i = block->capture_begin(),
915           e = block->capture_end(); i != e; ++i) {
916      if (i->getVariable() == &var)
917        return true;
918    }
919
920    // No need to walk into the subexpressions.
921    return false;
922  }
923
924  if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
925    const CompoundStmt *CS = SE->getSubStmt();
926    for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
927	   BE = CS->body_end(); BI != BE; ++BI)
928      if (Expr *E = dyn_cast<Expr>((*BI))) {
929        if (isCapturedBy(var, E))
930            return true;
931      }
932      else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) {
933          // special case declarations
934          for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
935               I != E; ++I) {
936              if (VarDecl *VD = dyn_cast<VarDecl>((*I))) {
937                Expr *Init = VD->getInit();
938                if (Init && isCapturedBy(var, Init))
939                  return true;
940              }
941          }
942      }
943      else
944        // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
945        // Later, provide code to poke into statements for capture analysis.
946        return true;
947    return false;
948  }
949
950  for (Stmt::const_child_range children = e->children(); children; ++children)
951    if (isCapturedBy(var, cast<Expr>(*children)))
952      return true;
953
954  return false;
955}
956
957/// \brief Determine whether the given initializer is trivial in the sense
958/// that it requires no code to be generated.
959static bool isTrivialInitializer(const Expr *Init) {
960  if (!Init)
961    return true;
962
963  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
964    if (CXXConstructorDecl *Constructor = Construct->getConstructor())
965      if (Constructor->isTrivial() &&
966          Constructor->isDefaultConstructor() &&
967          !Construct->requiresZeroInitialization())
968        return true;
969
970  return false;
971}
972void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
973  assert(emission.Variable && "emission was not valid!");
974
975  // If this was emitted as a global constant, we're done.
976  if (emission.wasEmittedAsGlobal()) return;
977
978  const VarDecl &D = *emission.Variable;
979  QualType type = D.getType();
980
981  // If this local has an initializer, emit it now.
982  const Expr *Init = D.getInit();
983
984  // If we are at an unreachable point, we don't need to emit the initializer
985  // unless it contains a label.
986  if (!HaveInsertPoint()) {
987    if (!Init || !ContainsLabel(Init)) return;
988    EnsureInsertPoint();
989  }
990
991  // Initialize the structure of a __block variable.
992  if (emission.IsByRef)
993    emitByrefStructureInit(emission);
994
995  if (isTrivialInitializer(Init))
996    return;
997
998  CharUnits alignment = emission.Alignment;
999
1000  // Check whether this is a byref variable that's potentially
1001  // captured and moved by its own initializer.  If so, we'll need to
1002  // emit the initializer first, then copy into the variable.
1003  bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
1004
1005  llvm::Value *Loc =
1006    capturedByInit ? emission.Address : emission.getObjectAddress(*this);
1007
1008  llvm::Constant *constant = 0;
1009  if (emission.IsConstantAggregate) {
1010    assert(!capturedByInit && "constant init contains a capturing block?");
1011    constant = CGM.EmitConstantInit(D, this);
1012  }
1013
1014  if (!constant) {
1015    LValue lv = MakeAddrLValue(Loc, type, alignment);
1016    lv.setNonGC(true);
1017    return EmitExprAsInit(Init, &D, lv, capturedByInit);
1018  }
1019
1020  // If this is a simple aggregate initialization, we can optimize it
1021  // in various ways.
1022  bool isVolatile = type.isVolatileQualified();
1023
1024  llvm::Value *SizeVal =
1025    llvm::ConstantInt::get(IntPtrTy,
1026                           getContext().getTypeSizeInChars(type).getQuantity());
1027
1028  llvm::Type *BP = Int8PtrTy;
1029  if (Loc->getType() != BP)
1030    Loc = Builder.CreateBitCast(Loc, BP);
1031
1032  // If the initializer is all or mostly zeros, codegen with memset then do
1033  // a few stores afterward.
1034  if (shouldUseMemSetPlusStoresToInitialize(constant,
1035                CGM.getTargetData().getTypeAllocSize(constant->getType()))) {
1036    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1037                         alignment.getQuantity(), isVolatile);
1038    if (!constant->isNullValue()) {
1039      Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
1040      emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
1041    }
1042  } else {
1043    // Otherwise, create a temporary global with the initializer then
1044    // memcpy from the global to the alloca.
1045    std::string Name = GetStaticDeclName(*this, D, ".");
1046    llvm::GlobalVariable *GV =
1047      new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
1048                               llvm::GlobalValue::PrivateLinkage,
1049                               constant, Name, 0, false, 0);
1050    GV->setAlignment(alignment.getQuantity());
1051    GV->setUnnamedAddr(true);
1052
1053    llvm::Value *SrcPtr = GV;
1054    if (SrcPtr->getType() != BP)
1055      SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1056
1057    Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
1058                         isVolatile);
1059  }
1060}
1061
1062/// Emit an expression as an initializer for a variable at the given
1063/// location.  The expression is not necessarily the normal
1064/// initializer for the variable, and the address is not necessarily
1065/// its normal location.
1066///
1067/// \param init the initializing expression
1068/// \param var the variable to act as if we're initializing
1069/// \param loc the address to initialize; its type is a pointer
1070///   to the LLVM mapping of the variable's type
1071/// \param alignment the alignment of the address
1072/// \param capturedByInit true if the variable is a __block variable
1073///   whose address is potentially changed by the initializer
1074void CodeGenFunction::EmitExprAsInit(const Expr *init,
1075                                     const ValueDecl *D,
1076                                     LValue lvalue,
1077                                     bool capturedByInit) {
1078  QualType type = D->getType();
1079
1080  if (type->isReferenceType()) {
1081    RValue rvalue = EmitReferenceBindingToExpr(init, D);
1082    if (capturedByInit)
1083      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1084    EmitStoreThroughLValue(rvalue, lvalue, true);
1085  } else if (!hasAggregateLLVMType(type)) {
1086    EmitScalarInit(init, D, lvalue, capturedByInit);
1087  } else if (type->isAnyComplexType()) {
1088    ComplexPairTy complex = EmitComplexExpr(init);
1089    if (capturedByInit)
1090      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1091    StoreComplexToAddr(complex, lvalue.getAddress(), lvalue.isVolatile());
1092  } else {
1093    // TODO: how can we delay here if D is captured by its initializer?
1094    EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1095                                              AggValueSlot::IsDestructed,
1096                                         AggValueSlot::DoesNotNeedGCBarriers,
1097                                              AggValueSlot::IsNotAliased));
1098    MaybeEmitStdInitializerListCleanup(lvalue.getAddress(), init);
1099  }
1100}
1101
1102/// Enter a destroy cleanup for the given local variable.
1103void CodeGenFunction::emitAutoVarTypeCleanup(
1104                            const CodeGenFunction::AutoVarEmission &emission,
1105                            QualType::DestructionKind dtorKind) {
1106  assert(dtorKind != QualType::DK_none);
1107
1108  // Note that for __block variables, we want to destroy the
1109  // original stack object, not the possibly forwarded object.
1110  llvm::Value *addr = emission.getObjectAddress(*this);
1111
1112  const VarDecl *var = emission.Variable;
1113  QualType type = var->getType();
1114
1115  CleanupKind cleanupKind = NormalAndEHCleanup;
1116  CodeGenFunction::Destroyer *destroyer = 0;
1117
1118  switch (dtorKind) {
1119  case QualType::DK_none:
1120    llvm_unreachable("no cleanup for trivially-destructible variable");
1121
1122  case QualType::DK_cxx_destructor:
1123    // If there's an NRVO flag on the emission, we need a different
1124    // cleanup.
1125    if (emission.NRVOFlag) {
1126      assert(!type->isArrayType());
1127      CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1128      EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor,
1129                                               emission.NRVOFlag);
1130      return;
1131    }
1132    break;
1133
1134  case QualType::DK_objc_strong_lifetime:
1135    // Suppress cleanups for pseudo-strong variables.
1136    if (var->isARCPseudoStrong()) return;
1137
1138    // Otherwise, consider whether to use an EH cleanup or not.
1139    cleanupKind = getARCCleanupKind();
1140
1141    // Use the imprecise destroyer by default.
1142    if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1143      destroyer = CodeGenFunction::destroyARCStrongImprecise;
1144    break;
1145
1146  case QualType::DK_objc_weak_lifetime:
1147    break;
1148  }
1149
1150  // If we haven't chosen a more specific destroyer, use the default.
1151  if (!destroyer) destroyer = getDestroyer(dtorKind);
1152
1153  // Use an EH cleanup in array destructors iff the destructor itself
1154  // is being pushed as an EH cleanup.
1155  bool useEHCleanup = (cleanupKind & EHCleanup);
1156  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1157                                     useEHCleanup);
1158}
1159
1160void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
1161  assert(emission.Variable && "emission was not valid!");
1162
1163  // If this was emitted as a global constant, we're done.
1164  if (emission.wasEmittedAsGlobal()) return;
1165
1166  const VarDecl &D = *emission.Variable;
1167
1168  // Check the type for a cleanup.
1169  if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1170    emitAutoVarTypeCleanup(emission, dtorKind);
1171
1172  // In GC mode, honor objc_precise_lifetime.
1173  if (getLangOptions().getGC() != LangOptions::NonGC &&
1174      D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1175    EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1176  }
1177
1178  // Handle the cleanup attribute.
1179  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1180    const FunctionDecl *FD = CA->getFunctionDecl();
1181
1182    llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1183    assert(F && "Could not find function!");
1184
1185    const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
1186    EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1187  }
1188
1189  // If this is a block variable, call _Block_object_destroy
1190  // (on the unforwarded address).
1191  if (emission.IsByRef)
1192    enterByrefCleanup(emission);
1193}
1194
1195CodeGenFunction::Destroyer *
1196CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
1197  switch (kind) {
1198  case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
1199  case QualType::DK_cxx_destructor:
1200    return destroyCXXObject;
1201  case QualType::DK_objc_strong_lifetime:
1202    return destroyARCStrongPrecise;
1203  case QualType::DK_objc_weak_lifetime:
1204    return destroyARCWeak;
1205  }
1206  llvm_unreachable("Unknown DestructionKind");
1207}
1208
1209/// pushDestroy - Push the standard destructor for the given type.
1210void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
1211                                  llvm::Value *addr, QualType type) {
1212  assert(dtorKind && "cannot push destructor for trivial type");
1213
1214  CleanupKind cleanupKind = getCleanupKind(dtorKind);
1215  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
1216              cleanupKind & EHCleanup);
1217}
1218
1219void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr,
1220                                  QualType type, Destroyer *destroyer,
1221                                  bool useEHCleanupForArray) {
1222  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
1223                                     destroyer, useEHCleanupForArray);
1224}
1225
1226/// emitDestroy - Immediately perform the destruction of the given
1227/// object.
1228///
1229/// \param addr - the address of the object; a type*
1230/// \param type - the type of the object; if an array type, all
1231///   objects are destroyed in reverse order
1232/// \param destroyer - the function to call to destroy individual
1233///   elements
1234/// \param useEHCleanupForArray - whether an EH cleanup should be
1235///   used when destroying array elements, in case one of the
1236///   destructions throws an exception
1237void CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type,
1238                                  Destroyer *destroyer,
1239                                  bool useEHCleanupForArray) {
1240  const ArrayType *arrayType = getContext().getAsArrayType(type);
1241  if (!arrayType)
1242    return destroyer(*this, addr, type);
1243
1244  llvm::Value *begin = addr;
1245  llvm::Value *length = emitArrayLength(arrayType, type, begin);
1246
1247  // Normally we have to check whether the array is zero-length.
1248  bool checkZeroLength = true;
1249
1250  // But if the array length is constant, we can suppress that.
1251  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
1252    // ...and if it's constant zero, we can just skip the entire thing.
1253    if (constLength->isZero()) return;
1254    checkZeroLength = false;
1255  }
1256
1257  llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
1258  emitArrayDestroy(begin, end, type, destroyer,
1259                   checkZeroLength, useEHCleanupForArray);
1260}
1261
1262/// emitArrayDestroy - Destroys all the elements of the given array,
1263/// beginning from last to first.  The array cannot be zero-length.
1264///
1265/// \param begin - a type* denoting the first element of the array
1266/// \param end - a type* denoting one past the end of the array
1267/// \param type - the element type of the array
1268/// \param destroyer - the function to call to destroy elements
1269/// \param useEHCleanup - whether to push an EH cleanup to destroy
1270///   the remaining elements in case the destruction of a single
1271///   element throws
1272void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
1273                                       llvm::Value *end,
1274                                       QualType type,
1275                                       Destroyer *destroyer,
1276                                       bool checkZeroLength,
1277                                       bool useEHCleanup) {
1278  assert(!type->isArrayType());
1279
1280  // The basic structure here is a do-while loop, because we don't
1281  // need to check for the zero-element case.
1282  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
1283  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
1284
1285  if (checkZeroLength) {
1286    llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
1287                                                "arraydestroy.isempty");
1288    Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
1289  }
1290
1291  // Enter the loop body, making that address the current address.
1292  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1293  EmitBlock(bodyBB);
1294  llvm::PHINode *elementPast =
1295    Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
1296  elementPast->addIncoming(end, entryBB);
1297
1298  // Shift the address back by one element.
1299  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
1300  llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
1301                                                   "arraydestroy.element");
1302
1303  if (useEHCleanup)
1304    pushRegularPartialArrayCleanup(begin, element, type, destroyer);
1305
1306  // Perform the actual destruction there.
1307  destroyer(*this, element, type);
1308
1309  if (useEHCleanup)
1310    PopCleanupBlock();
1311
1312  // Check whether we've reached the end.
1313  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
1314  Builder.CreateCondBr(done, doneBB, bodyBB);
1315  elementPast->addIncoming(element, Builder.GetInsertBlock());
1316
1317  // Done.
1318  EmitBlock(doneBB);
1319}
1320
1321/// Perform partial array destruction as if in an EH cleanup.  Unlike
1322/// emitArrayDestroy, the element type here may still be an array type.
1323static void emitPartialArrayDestroy(CodeGenFunction &CGF,
1324                                    llvm::Value *begin, llvm::Value *end,
1325                                    QualType type,
1326                                    CodeGenFunction::Destroyer *destroyer) {
1327  // If the element type is itself an array, drill down.
1328  unsigned arrayDepth = 0;
1329  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
1330    // VLAs don't require a GEP index to walk into.
1331    if (!isa<VariableArrayType>(arrayType))
1332      arrayDepth++;
1333    type = arrayType->getElementType();
1334  }
1335
1336  if (arrayDepth) {
1337    llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, arrayDepth+1);
1338
1339    SmallVector<llvm::Value*,4> gepIndices(arrayDepth, zero);
1340    begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
1341    end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
1342  }
1343
1344  // Destroy the array.  We don't ever need an EH cleanup because we
1345  // assume that we're in an EH cleanup ourselves, so a throwing
1346  // destructor causes an immediate terminate.
1347  CGF.emitArrayDestroy(begin, end, type, destroyer,
1348                       /*checkZeroLength*/ true, /*useEHCleanup*/ false);
1349}
1350
1351namespace {
1352  /// RegularPartialArrayDestroy - a cleanup which performs a partial
1353  /// array destroy where the end pointer is regularly determined and
1354  /// does not need to be loaded from a local.
1355  class RegularPartialArrayDestroy : public EHScopeStack::Cleanup {
1356    llvm::Value *ArrayBegin;
1357    llvm::Value *ArrayEnd;
1358    QualType ElementType;
1359    CodeGenFunction::Destroyer *Destroyer;
1360  public:
1361    RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
1362                               QualType elementType,
1363                               CodeGenFunction::Destroyer *destroyer)
1364      : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
1365        ElementType(elementType), Destroyer(destroyer) {}
1366
1367    void Emit(CodeGenFunction &CGF, Flags flags) {
1368      emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
1369                              ElementType, Destroyer);
1370    }
1371  };
1372
1373  /// IrregularPartialArrayDestroy - a cleanup which performs a
1374  /// partial array destroy where the end pointer is irregularly
1375  /// determined and must be loaded from a local.
1376  class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup {
1377    llvm::Value *ArrayBegin;
1378    llvm::Value *ArrayEndPointer;
1379    QualType ElementType;
1380    CodeGenFunction::Destroyer *Destroyer;
1381  public:
1382    IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
1383                                 llvm::Value *arrayEndPointer,
1384                                 QualType elementType,
1385                                 CodeGenFunction::Destroyer *destroyer)
1386      : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
1387        ElementType(elementType), Destroyer(destroyer) {}
1388
1389    void Emit(CodeGenFunction &CGF, Flags flags) {
1390      llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
1391      emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
1392                              ElementType, Destroyer);
1393    }
1394  };
1395}
1396
1397/// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
1398/// already-constructed elements of the given array.  The cleanup
1399/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1400///
1401/// \param elementType - the immediate element type of the array;
1402///   possibly still an array type
1403/// \param array - a value of type elementType*
1404/// \param destructionKind - the kind of destruction required
1405/// \param initializedElementCount - a value of type size_t* holding
1406///   the number of successfully-constructed elements
1407void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1408                                                 llvm::Value *arrayEndPointer,
1409                                                       QualType elementType,
1410                                                       Destroyer *destroyer) {
1411  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
1412                                                    arrayBegin, arrayEndPointer,
1413                                                    elementType, destroyer);
1414}
1415
1416/// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
1417/// already-constructed elements of the given array.  The cleanup
1418/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1419///
1420/// \param elementType - the immediate element type of the array;
1421///   possibly still an array type
1422/// \param array - a value of type elementType*
1423/// \param destructionKind - the kind of destruction required
1424/// \param initializedElementCount - a value of type size_t* holding
1425///   the number of successfully-constructed elements
1426void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1427                                                     llvm::Value *arrayEnd,
1428                                                     QualType elementType,
1429                                                     Destroyer *destroyer) {
1430  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
1431                                                  arrayBegin, arrayEnd,
1432                                                  elementType, destroyer);
1433}
1434
1435namespace {
1436  /// A cleanup to perform a release of an object at the end of a
1437  /// function.  This is used to balance out the incoming +1 of a
1438  /// ns_consumed argument when we can't reasonably do that just by
1439  /// not doing the initial retain for a __block argument.
1440  struct ConsumeARCParameter : EHScopeStack::Cleanup {
1441    ConsumeARCParameter(llvm::Value *param) : Param(param) {}
1442
1443    llvm::Value *Param;
1444
1445    void Emit(CodeGenFunction &CGF, Flags flags) {
1446      CGF.EmitARCRelease(Param, /*precise*/ false);
1447    }
1448  };
1449}
1450
1451/// Emit an alloca (or GlobalValue depending on target)
1452/// for the specified parameter and set up LocalDeclMap.
1453void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
1454                                   unsigned ArgNo) {
1455  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1456  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1457         "Invalid argument to EmitParmDecl");
1458
1459  Arg->setName(D.getName());
1460
1461  // Use better IR generation for certain implicit parameters.
1462  if (isa<ImplicitParamDecl>(D)) {
1463    // The only implicit argument a block has is its literal.
1464    if (BlockInfo) {
1465      LocalDeclMap[&D] = Arg;
1466
1467      if (CGDebugInfo *DI = getDebugInfo()) {
1468        DI->setLocation(D.getLocation());
1469        DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder);
1470      }
1471
1472      return;
1473    }
1474  }
1475
1476  QualType Ty = D.getType();
1477
1478  llvm::Value *DeclPtr;
1479  // If this is an aggregate or variable sized value, reuse the input pointer.
1480  if (!Ty->isConstantSizeType() ||
1481      CodeGenFunction::hasAggregateLLVMType(Ty)) {
1482    DeclPtr = Arg;
1483  } else {
1484    // Otherwise, create a temporary to hold the value.
1485    llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1486                                               D.getName() + ".addr");
1487    Alloc->setAlignment(getContext().getDeclAlign(&D).getQuantity());
1488    DeclPtr = Alloc;
1489
1490    bool doStore = true;
1491
1492    Qualifiers qs = Ty.getQualifiers();
1493
1494    if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1495      // We honor __attribute__((ns_consumed)) for types with lifetime.
1496      // For __strong, it's handled by just skipping the initial retain;
1497      // otherwise we have to balance out the initial +1 with an extra
1498      // cleanup to do the release at the end of the function.
1499      bool isConsumed = D.hasAttr<NSConsumedAttr>();
1500
1501      // 'self' is always formally __strong, but if this is not an
1502      // init method then we don't want to retain it.
1503      if (D.isARCPseudoStrong()) {
1504        const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1505        assert(&D == method->getSelfDecl());
1506        assert(lt == Qualifiers::OCL_Strong);
1507        assert(qs.hasConst());
1508        assert(method->getMethodFamily() != OMF_init);
1509        (void) method;
1510        lt = Qualifiers::OCL_ExplicitNone;
1511      }
1512
1513      if (lt == Qualifiers::OCL_Strong) {
1514        if (!isConsumed)
1515          // Don't use objc_retainBlock for block pointers, because we
1516          // don't want to Block_copy something just because we got it
1517          // as a parameter.
1518          Arg = EmitARCRetainNonBlock(Arg);
1519      } else {
1520        // Push the cleanup for a consumed parameter.
1521        if (isConsumed)
1522          EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg);
1523
1524        if (lt == Qualifiers::OCL_Weak) {
1525          EmitARCInitWeak(DeclPtr, Arg);
1526          doStore = false; // The weak init is a store, no need to do two.
1527        }
1528      }
1529
1530      // Enter the cleanup scope.
1531      EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1532    }
1533
1534    // Store the initial value into the alloca.
1535    if (doStore) {
1536      LValue lv = MakeAddrLValue(DeclPtr, Ty,
1537                                 getContext().getDeclAlign(&D));
1538      EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1539    }
1540  }
1541
1542  llvm::Value *&DMEntry = LocalDeclMap[&D];
1543  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
1544  DMEntry = DeclPtr;
1545
1546  // Emit debug info for param declaration.
1547  if (CGDebugInfo *DI = getDebugInfo())
1548    DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
1549
1550  if (D.hasAttr<AnnotateAttr>())
1551      EmitVarAnnotations(&D, DeclPtr);
1552}
1553