CGDecl.cpp revision 272461
175115Sfenner//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
298524Sfenner//
3127668Sbms//                     The LLVM Compiler Infrastructure
498524Sfenner//
598524Sfenner// This file is distributed under the University of Illinois Open Source
698524Sfenner// License. See LICENSE.TXT for details.
7127668Sbms//
898524Sfenner//===----------------------------------------------------------------------===//
998524Sfenner//
1098524Sfenner// This contains code to emit Decl nodes as LLVM code.
1198524Sfenner//
1298524Sfenner//===----------------------------------------------------------------------===//
1398524Sfenner
1498524Sfenner#include "CodeGenFunction.h"
1598524Sfenner#include "CGDebugInfo.h"
1698524Sfenner#include "CGOpenCLRuntime.h"
17127668Sbms#include "CodeGenModule.h"
1898524Sfenner#include "clang/AST/ASTContext.h"
1998524Sfenner#include "clang/AST/CharUnits.h"
2098524Sfenner#include "clang/AST/Decl.h"
2198524Sfenner#include "clang/AST/DeclObjC.h"
2298524Sfenner#include "clang/Basic/SourceManager.h"
2375115Sfenner#include "clang/Basic/TargetInfo.h"
2475115Sfenner#include "clang/CodeGen/CGFunctionInfo.h"
2575115Sfenner#include "clang/Frontend/CodeGenOptions.h"
2675115Sfenner#include "llvm/IR/DataLayout.h"
2775115Sfenner#include "llvm/IR/GlobalVariable.h"
2875115Sfenner#include "llvm/IR/Intrinsics.h"
2975115Sfenner#include "llvm/IR/Type.h"
3075115Sfennerusing namespace clang;
3175115Sfennerusing namespace CodeGen;
3275115Sfenner
3375115Sfenner
3475115Sfennervoid CodeGenFunction::EmitDecl(const Decl &D) {
3575115Sfenner  switch (D.getKind()) {
3675115Sfenner  case Decl::TranslationUnit:
3775115Sfenner  case Decl::Namespace:
3875115Sfenner  case Decl::UnresolvedUsingTypename:
3975115Sfenner  case Decl::ClassTemplateSpecialization:
4075115Sfenner  case Decl::ClassTemplatePartialSpecialization:
4175115Sfenner  case Decl::VarTemplateSpecialization:
42127668Sbms  case Decl::VarTemplatePartialSpecialization:
4375115Sfenner  case Decl::TemplateTypeParm:
4475115Sfenner  case Decl::UnresolvedUsingValue:
4575115Sfenner  case Decl::NonTypeTemplateParm:
46127668Sbms  case Decl::CXXMethod:
47190207Srpaulo  case Decl::CXXConstructor:
4875115Sfenner  case Decl::CXXDestructor:
4975115Sfenner  case Decl::CXXConversion:
5075115Sfenner  case Decl::Field:
5175115Sfenner  case Decl::MSProperty:
5275115Sfenner  case Decl::IndirectField:
5375115Sfenner  case Decl::ObjCIvar:
54127668Sbms  case Decl::ObjCAtDefsField:
55127668Sbms  case Decl::ParmVar:
5698524Sfenner  case Decl::ImplicitParam:
5798524Sfenner  case Decl::ClassTemplate:
5875115Sfenner  case Decl::VarTemplate:
5975115Sfenner  case Decl::FunctionTemplate:
6075115Sfenner  case Decl::TypeAliasTemplate:
6175115Sfenner  case Decl::TemplateTemplateParm:
6275115Sfenner  case Decl::ObjCMethod:
63127668Sbms  case Decl::ObjCCategory:
6475115Sfenner  case Decl::ObjCProtocol:
6575115Sfenner  case Decl::ObjCInterface:
6675115Sfenner  case Decl::ObjCCategoryImpl:
6775115Sfenner  case Decl::ObjCImplementation:
6875115Sfenner  case Decl::ObjCProperty:
6975115Sfenner  case Decl::ObjCCompatibleAlias:
7075115Sfenner  case Decl::AccessSpec:
7175115Sfenner  case Decl::LinkageSpec:
7275115Sfenner  case Decl::ObjCPropertyImpl:
7375115Sfenner  case Decl::FileScopeAsm:
7475115Sfenner  case Decl::Friend:
7575115Sfenner  case Decl::FriendTemplate:
7675115Sfenner  case Decl::Block:
7775115Sfenner  case Decl::Captured:
7875115Sfenner  case Decl::ClassScopeFunctionSpecialization:
7975115Sfenner  case Decl::UsingShadow:
8075115Sfenner    llvm_unreachable("Declaration should not be in declstmts!");
8175115Sfenner  case Decl::Function:  // void X();
8275115Sfenner  case Decl::Record:    // struct/union/class X;
8375115Sfenner  case Decl::Enum:      // enum X;
8475115Sfenner  case Decl::EnumConstant: // enum ? { X = ? }
8575115Sfenner  case Decl::CXXRecord: // struct/union/class X; [C++]
8675115Sfenner  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
87127668Sbms  case Decl::Label:        // __label__ x;
88127668Sbms  case Decl::Import:
89127668Sbms  case Decl::OMPThreadPrivate:
90127668Sbms  case Decl::Empty:
91127668Sbms    // None of these decls require codegen support.
92127668Sbms    return;
93127668Sbms
94127668Sbms  case Decl::NamespaceAlias:
95127668Sbms    if (CGDebugInfo *DI = getDebugInfo())
96127668Sbms        DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
97127668Sbms    return;
98127668Sbms  case Decl::Using:          // using X; [C++]
9975115Sfenner    if (CGDebugInfo *DI = getDebugInfo())
10075115Sfenner        DI->EmitUsingDecl(cast<UsingDecl>(D));
10175115Sfenner    return;
10275115Sfenner  case Decl::UsingDirective: // using namespace X; [C++]
10375115Sfenner    if (CGDebugInfo *DI = getDebugInfo())
10475115Sfenner      DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
10575115Sfenner    return;
10675115Sfenner  case Decl::Var: {
10775115Sfenner    const VarDecl &VD = cast<VarDecl>(D);
10875115Sfenner    assert(VD.isLocalVarDecl() &&
10975115Sfenner           "Should not see file-scope variables inside a function!");
11075115Sfenner    return EmitVarDecl(VD);
11175115Sfenner  }
11275115Sfenner
11375115Sfenner  case Decl::Typedef:      // typedef int X;
11475115Sfenner  case Decl::TypeAlias: {  // using X = int; [C++0x]
11575115Sfenner    const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
11675115Sfenner    QualType Ty = TD.getUnderlyingType();
11775115Sfenner
11875115Sfenner    if (Ty->isVariablyModifiedType())
11975115Sfenner      EmitVariablyModifiedType(Ty);
12075115Sfenner  }
12175115Sfenner  }
12275115Sfenner}
12375115Sfenner
12475115Sfenner/// EmitVarDecl - This method handles emission of any variable declaration
12575115Sfenner/// inside a function, including static vars etc.
12675115Sfennervoid CodeGenFunction::EmitVarDecl(const VarDecl &D) {
12775115Sfenner  if (D.isStaticLocal()) {
12875115Sfenner    llvm::GlobalValue::LinkageTypes Linkage =
12975115Sfenner      llvm::GlobalValue::InternalLinkage;
13075115Sfenner
13175115Sfenner    // If the variable is externally visible, it must have weak linkage so it
13275115Sfenner    // can be uniqued.
13375115Sfenner    if (D.isExternallyVisible()) {
13475115Sfenner      Linkage = llvm::GlobalValue::LinkOnceODRLinkage;
13575115Sfenner
13675115Sfenner      // FIXME: We need to force the emission/use of a guard variable for
13775115Sfenner      // some variables even if we can constant-evaluate them because
13875115Sfenner      // we can't guarantee every translation unit will constant-evaluate them.
13975115Sfenner    }
14075115Sfenner
141127668Sbms    return EmitStaticVarDecl(D, Linkage);
14275115Sfenner  }
14375115Sfenner
14475115Sfenner  if (D.hasExternalStorage())
14575115Sfenner    // Don't emit it now, allow it to be emitted lazily on its first use.
14675115Sfenner    return;
14775115Sfenner
14875115Sfenner  if (D.getStorageClass() == SC_OpenCLWorkGroupLocal)
14975115Sfenner    return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D);
15075115Sfenner
15175115Sfenner  assert(D.hasLocalStorage());
15275115Sfenner  return EmitAutoVarDecl(D);
15398524Sfenner}
15475115Sfenner
15575115Sfennerstatic std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
15675115Sfenner                                     const char *Separator) {
15775115Sfenner  CodeGenModule &CGM = CGF.CGM;
15875115Sfenner  if (CGF.getLangOpts().CPlusPlus) {
15975115Sfenner    StringRef Name = CGM.getMangledName(&D);
160127668Sbms    return Name.str();
16175115Sfenner  }
16275115Sfenner
163127668Sbms  std::string ContextName;
16475115Sfenner  if (!CGF.CurFuncDecl) {
16575115Sfenner    // Better be in a block declared in global scope.
16675115Sfenner    const NamedDecl *ND = cast<NamedDecl>(&D);
16775115Sfenner    const DeclContext *DC = ND->getDeclContext();
168127668Sbms    if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
16975115Sfenner      MangleBuffer Name;
17075115Sfenner      CGM.getBlockMangledName(GlobalDecl(), Name, BD);
17175115Sfenner      ContextName = Name.getString();
17275115Sfenner    }
173127668Sbms    else
17475115Sfenner      llvm_unreachable("Unknown context for block static var decl");
17575115Sfenner  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
17675115Sfenner    StringRef Name = CGM.getMangledName(FD);
17775115Sfenner    ContextName = Name.str();
17875115Sfenner  } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
17975115Sfenner    ContextName = CGF.CurFn->getName();
18075115Sfenner  else
18175115Sfenner    llvm_unreachable("Unknown context for static var decl");
18275115Sfenner
183127668Sbms  return ContextName + Separator + D.getNameAsString();
18475115Sfenner}
18575115Sfenner
18675115Sfennerllvm::GlobalVariable *
18775115SfennerCodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
18875115Sfenner                                     const char *Separator,
18975115Sfenner                                     llvm::GlobalValue::LinkageTypes Linkage) {
190127668Sbms  QualType Ty = D.getType();
19175115Sfenner  assert(Ty->isConstantSizeType() && "VLAs can't be static");
19275115Sfenner
19375115Sfenner  // Use the label if the variable is renamed with the asm-label extension.
19475115Sfenner  std::string Name;
19575115Sfenner  if (D.hasAttr<AsmLabelAttr>())
19675115Sfenner    Name = CGM.getMangledName(&D);
19775115Sfenner  else
19875115Sfenner    Name = GetStaticDeclName(*this, D, Separator);
19975115Sfenner
20075115Sfenner  llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
20175115Sfenner  unsigned AddrSpace =
20275115Sfenner   CGM.GetGlobalVarAddressSpace(&D, CGM.getContext().getTargetAddressSpace(Ty));
20375115Sfenner  llvm::GlobalVariable *GV =
20475115Sfenner    new llvm::GlobalVariable(CGM.getModule(), LTy,
20575115Sfenner                             Ty.isConstant(getContext()), Linkage,
20675115Sfenner                             CGM.EmitNullConstant(D.getType()), Name, 0,
20775115Sfenner                             llvm::GlobalVariable::NotThreadLocal,
20875115Sfenner                             AddrSpace);
20975115Sfenner  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
21075115Sfenner  CGM.setGlobalVisibility(GV, &D);
21175115Sfenner
21275115Sfenner  if (D.getTLSKind())
21375115Sfenner    CGM.setTLSMode(GV, D);
21475115Sfenner
21575115Sfenner  return GV;
21675115Sfenner}
21775115Sfenner
21875115Sfenner/// hasNontrivialDestruction - Determine whether a type's destruction is
21975115Sfenner/// non-trivial. If so, and the variable uses static initialization, we must
22075115Sfenner/// register its destructor to run on exit.
22175115Sfennerstatic bool hasNontrivialDestruction(QualType T) {
22275115Sfenner  CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
22375115Sfenner  return RD && !RD->hasTrivialDestructor();
22475115Sfenner}
22575115Sfenner
22675115Sfenner/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
22775115Sfenner/// global variable that has already been created for it.  If the initializer
22875115Sfenner/// has a different type than GV does, this may free GV and return a different
22975115Sfenner/// one.  Otherwise it just returns GV.
23075115Sfennerllvm::GlobalVariable *
23175115SfennerCodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
23275115Sfenner                                               llvm::GlobalVariable *GV) {
23375115Sfenner  llvm::Constant *Init = CGM.EmitConstantInit(D, this);
23475115Sfenner
23575115Sfenner  // If constant emission failed, then this should be a C++ static
23675115Sfenner  // initializer.
23775115Sfenner  if (!Init) {
238127668Sbms    if (!getLangOpts().CPlusPlus)
23975115Sfenner      CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
24075115Sfenner    else if (Builder.GetInsertBlock()) {
24175115Sfenner      // Since we have a static initializer, this global variable can't
24275115Sfenner      // be constant.
24375115Sfenner      GV->setConstant(false);
24475115Sfenner
24575115Sfenner      EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
24675115Sfenner    }
24775115Sfenner    return GV;
24875115Sfenner  }
24975115Sfenner
25075115Sfenner  // The initializer may differ in type from the global. Rewrite
25175115Sfenner  // the global to match the initializer.  (We have to do this
25275115Sfenner  // because some types, like unions, can't be completely represented
25375115Sfenner  // in the LLVM type system.)
25475115Sfenner  if (GV->getType()->getElementType() != Init->getType()) {
25575115Sfenner    llvm::GlobalVariable *OldGV = GV;
25675115Sfenner
25775115Sfenner    GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
25875115Sfenner                                  OldGV->isConstant(),
25975115Sfenner                                  OldGV->getLinkage(), Init, "",
26075115Sfenner                                  /*InsertBefore*/ OldGV,
26175115Sfenner                                  OldGV->getThreadLocalMode(),
26275115Sfenner                           CGM.getContext().getTargetAddressSpace(D.getType()));
26375115Sfenner    GV->setVisibility(OldGV->getVisibility());
26475115Sfenner
26575115Sfenner    // Steal the name of the old global
26675115Sfenner    GV->takeName(OldGV);
26775115Sfenner
26875115Sfenner    // Replace all uses of the old global with the new global
26975115Sfenner    llvm::Constant *NewPtrForOldDecl =
27075115Sfenner    llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
27175115Sfenner    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
27275115Sfenner
27375115Sfenner    // Erase the old global, since it is no longer used.
27475115Sfenner    OldGV->eraseFromParent();
27575115Sfenner  }
27675115Sfenner
27775115Sfenner  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
27875115Sfenner  GV->setInitializer(Init);
27975115Sfenner
28075115Sfenner  if (hasNontrivialDestruction(D.getType())) {
28175115Sfenner    // We have a constant initializer, but a nontrivial destructor. We still
28275115Sfenner    // need to perform a guarded "initialization" in order to register the
28375115Sfenner    // destructor.
28475115Sfenner    EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
28575115Sfenner  }
28675115Sfenner
28775115Sfenner  return GV;
28875115Sfenner}
28975115Sfenner
29075115Sfennervoid CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
29175115Sfenner                                      llvm::GlobalValue::LinkageTypes Linkage) {
29275115Sfenner  llvm::Value *&DMEntry = LocalDeclMap[&D];
29375115Sfenner  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
29475115Sfenner
29575115Sfenner  // Check to see if we already have a global variable for this
29675115Sfenner  // declaration.  This can happen when double-emitting function
29775115Sfenner  // bodies, e.g. with complete and base constructors.
29875115Sfenner  llvm::Constant *addr =
29975115Sfenner    CGM.getStaticLocalDeclAddress(&D);
30075115Sfenner
30175115Sfenner  llvm::GlobalVariable *var;
30275115Sfenner  if (addr) {
303127668Sbms    var = cast<llvm::GlobalVariable>(addr->stripPointerCasts());
30475115Sfenner  } else {
30575115Sfenner    addr = var = CreateStaticVarDecl(D, ".", Linkage);
30675115Sfenner  }
30775115Sfenner
30875115Sfenner  // Store into LocalDeclMap before generating initializer to handle
30975115Sfenner  // circular references.
31075115Sfenner  DMEntry = addr;
31175115Sfenner  CGM.setStaticLocalDeclAddress(&D, addr);
31275115Sfenner
31375115Sfenner  // We can't have a VLA here, but we can have a pointer to a VLA,
31475115Sfenner  // even though that doesn't really make any sense.
31575115Sfenner  // Make sure to evaluate VLA bounds now so that we have them for later.
31675115Sfenner  if (D.getType()->isVariablyModifiedType())
31775115Sfenner    EmitVariablyModifiedType(D.getType());
31875115Sfenner
31975115Sfenner  // Save the type in case adding the initializer forces a type change.
32075115Sfenner  llvm::Type *expectedType = addr->getType();
32175115Sfenner
32275115Sfenner  // If this value has an initializer, emit it.
32375115Sfenner  if (D.getInit())
32475115Sfenner    var = AddInitializerToStaticVarDecl(D, var);
32575115Sfenner
32675115Sfenner  var->setAlignment(getContext().getDeclAlign(&D).getQuantity());
32775115Sfenner
32875115Sfenner  if (D.hasAttr<AnnotateAttr>())
32975115Sfenner    CGM.AddGlobalAnnotations(&D, var);
33075115Sfenner
33175115Sfenner  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
33275115Sfenner    var->setSection(SA->getName());
33375115Sfenner
334127668Sbms  if (D.hasAttr<UsedAttr>())
335127668Sbms    CGM.AddUsedGlobal(var);
336127668Sbms
33775115Sfenner  // We may have to cast the constant because of the initializer
33875115Sfenner  // mismatch above.
33975115Sfenner  //
34075115Sfenner  // FIXME: It is really dangerous to store this in the map; if anyone
34175115Sfenner  // RAUW's the GV uses of this constant will be invalid.
34275115Sfenner  llvm::Constant *castedAddr = llvm::ConstantExpr::getBitCast(var, expectedType);
343127668Sbms  DMEntry = castedAddr;
344127668Sbms  CGM.setStaticLocalDeclAddress(&D, castedAddr);
345127668Sbms
346127668Sbms  // Emit global variable debug descriptor for static vars.
347127668Sbms  CGDebugInfo *DI = getDebugInfo();
348127668Sbms  if (DI &&
349127668Sbms      CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
350127668Sbms    DI->setLocation(D.getLocation());
351127668Sbms    DI->EmitGlobalVariable(var, &D);
352127668Sbms  }
353127668Sbms}
354127668Sbms
355127668Sbmsnamespace {
356127668Sbms  struct DestroyObject : EHScopeStack::Cleanup {
357127668Sbms    DestroyObject(llvm::Value *addr, QualType type,
358127668Sbms                  CodeGenFunction::Destroyer *destroyer,
359127668Sbms                  bool useEHCleanupForArray)
360127668Sbms      : addr(addr), type(type), destroyer(destroyer),
361127668Sbms        useEHCleanupForArray(useEHCleanupForArray) {}
362127668Sbms
363127668Sbms    llvm::Value *addr;
364127668Sbms    QualType type;
365127668Sbms    CodeGenFunction::Destroyer *destroyer;
366127668Sbms    bool useEHCleanupForArray;
367127668Sbms
368127668Sbms    void Emit(CodeGenFunction &CGF, Flags flags) {
369127668Sbms      // Don't use an EH cleanup recursively from an EH cleanup.
370127668Sbms      bool useEHCleanupForArray =
371127668Sbms        flags.isForNormalCleanup() && this->useEHCleanupForArray;
372127668Sbms
373127668Sbms      CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
374127668Sbms    }
375127668Sbms  };
376127668Sbms
377127668Sbms  struct DestroyNRVOVariable : EHScopeStack::Cleanup {
378127668Sbms    DestroyNRVOVariable(llvm::Value *addr,
379127668Sbms                        const CXXDestructorDecl *Dtor,
380127668Sbms                        llvm::Value *NRVOFlag)
381127668Sbms      : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
382127668Sbms
383127668Sbms    const CXXDestructorDecl *Dtor;
384127668Sbms    llvm::Value *NRVOFlag;
385127668Sbms    llvm::Value *Loc;
386127668Sbms
387127668Sbms    void Emit(CodeGenFunction &CGF, Flags flags) {
388127668Sbms      // Along the exceptions path we always execute the dtor.
389127668Sbms      bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
390127668Sbms
391127668Sbms      llvm::BasicBlock *SkipDtorBB = 0;
392127668Sbms      if (NRVO) {
393127668Sbms        // If we exited via NRVO, we skip the destructor call.
394127668Sbms        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
395127668Sbms        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
396127668Sbms        llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
397127668Sbms        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
398127668Sbms        CGF.EmitBlock(RunDtorBB);
399127668Sbms      }
400127668Sbms
401127668Sbms      CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
402127668Sbms                                /*ForVirtualBase=*/false,
403127668Sbms                                /*Delegating=*/false,
404127668Sbms                                Loc);
405127668Sbms
406127668Sbms      if (NRVO) CGF.EmitBlock(SkipDtorBB);
407127668Sbms    }
408127668Sbms  };
409127668Sbms
410127668Sbms  struct CallStackRestore : EHScopeStack::Cleanup {
411127668Sbms    llvm::Value *Stack;
412127668Sbms    CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
413127668Sbms    void Emit(CodeGenFunction &CGF, Flags flags) {
414127668Sbms      llvm::Value *V = CGF.Builder.CreateLoad(Stack);
415127668Sbms      llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
416127668Sbms      CGF.Builder.CreateCall(F, V);
417127668Sbms    }
418127668Sbms  };
419127668Sbms
420127668Sbms  struct ExtendGCLifetime : EHScopeStack::Cleanup {
421127668Sbms    const VarDecl &Var;
422127668Sbms    ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
423127668Sbms
424127668Sbms    void Emit(CodeGenFunction &CGF, Flags flags) {
425127668Sbms      // Compute the address of the local variable, in case it's a
426127668Sbms      // byref or something.
427127668Sbms      DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
428127668Sbms                      Var.getType(), VK_LValue, SourceLocation());
429127668Sbms      llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
430127668Sbms                                                SourceLocation());
431127668Sbms      CGF.EmitExtendGCLifetime(value);
432127668Sbms    }
433127668Sbms  };
434127668Sbms
435127668Sbms  struct CallCleanupFunction : EHScopeStack::Cleanup {
436127668Sbms    llvm::Constant *CleanupFn;
437127668Sbms    const CGFunctionInfo &FnInfo;
43875115Sfenner    const VarDecl &Var;
43975115Sfenner
44075115Sfenner    CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
44175115Sfenner                        const VarDecl *Var)
44275115Sfenner      : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
44375115Sfenner
44475115Sfenner    void Emit(CodeGenFunction &CGF, Flags flags) {
44575115Sfenner      DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
44675115Sfenner                      Var.getType(), VK_LValue, SourceLocation());
44775115Sfenner      // Compute the address of the local variable, in case it's a byref
44875115Sfenner      // or something.
44975115Sfenner      llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
45075115Sfenner
451127668Sbms      // In some cases, the type of the function argument will be different from
45275115Sfenner      // the type of the pointer. An example of this is
453127668Sbms      // void f(void* arg);
45475115Sfenner      // __attribute__((cleanup(f))) void *g;
45575115Sfenner      //
45675115Sfenner      // To fix this we insert a bitcast here.
457162017Ssam      QualType ArgTy = FnInfo.arg_begin()->type;
458162017Ssam      llvm::Value *Arg =
459162017Ssam        CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
460162017Ssam
461162017Ssam      CallArgList Args;
46275115Sfenner      Args.add(RValue::get(Arg),
463127668Sbms               CGF.getContext().getPointerType(Var.getType()));
46475115Sfenner      CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
465162017Ssam    }
466127668Sbms  };
46775115Sfenner
46875115Sfenner  /// A cleanup to call @llvm.lifetime.end.
46975115Sfenner  class CallLifetimeEnd : public EHScopeStack::Cleanup {
47075115Sfenner    llvm::Value *Addr;
47175115Sfenner    llvm::Value *Size;
47275115Sfenner  public:
47375115Sfenner    CallLifetimeEnd(llvm::Value *addr, llvm::Value *size)
47475115Sfenner      : Addr(addr), Size(size) {}
47575115Sfenner
47675115Sfenner    void Emit(CodeGenFunction &CGF, Flags flags) {
47775115Sfenner      llvm::Value *castAddr = CGF.Builder.CreateBitCast(Addr, CGF.Int8PtrTy);
478162017Ssam      CGF.Builder.CreateCall2(CGF.CGM.getLLVMLifetimeEndFn(),
479162017Ssam                              Size, castAddr)
480162017Ssam        ->setDoesNotThrow();
481162017Ssam    }
482162017Ssam  };
483127668Sbms}
48475115Sfenner
48575115Sfenner/// EmitAutoVarWithLifetime - Does the setup required for an automatic
48675115Sfenner/// variable with lifetime.
48775115Sfennerstatic void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
48875115Sfenner                                    llvm::Value *addr,
48975115Sfenner                                    Qualifiers::ObjCLifetime lifetime) {
490124488Sfenner  switch (lifetime) {
49175115Sfenner  case Qualifiers::OCL_None:
49275115Sfenner    llvm_unreachable("present but none");
49375115Sfenner
494127668Sbms  case Qualifiers::OCL_ExplicitNone:
49575115Sfenner    // nothing to do
496162017Ssam    break;
497127668Sbms
49875115Sfenner  case Qualifiers::OCL_Strong: {
499127668Sbms    CodeGenFunction::Destroyer *destroyer =
500127668Sbms      (var.hasAttr<ObjCPreciseLifetimeAttr>()
501127668Sbms       ? CodeGenFunction::destroyARCStrongPrecise
50275115Sfenner       : CodeGenFunction::destroyARCStrongImprecise);
503127668Sbms
504127668Sbms    CleanupKind cleanupKind = CGF.getARCCleanupKind();
505127668Sbms    CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
506127668Sbms                    cleanupKind & EHCleanup);
507127668Sbms    break;
508127668Sbms  }
509127668Sbms  case Qualifiers::OCL_Autoreleasing:
510127668Sbms    // nothing to do
511162017Ssam    break;
512162017Ssam
513162017Ssam  case Qualifiers::OCL_Weak:
514127668Sbms    // __weak objects always get EH cleanups; otherwise, exceptions
515127668Sbms    // could cause really nasty crashes instead of mere leaks.
516127668Sbms    CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
517127668Sbms                    CodeGenFunction::destroyARCWeak,
518127668Sbms                    /*useEHCleanup*/ true);
519127668Sbms    break;
520127668Sbms  }
521127668Sbms}
522127668Sbms
523162017Ssamstatic bool isAccessedBy(const VarDecl &var, const Stmt *s) {
524127668Sbms  if (const Expr *e = dyn_cast<Expr>(s)) {
525127668Sbms    // Skip the most common kinds of expressions that make
526127668Sbms    // hierarchy-walking expensive.
527127668Sbms    s = e = e->IgnoreParenCasts();
528162017Ssam
529162017Ssam    if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
530162017Ssam      return (ref->getDecl() == &var);
531162017Ssam    if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
532162017Ssam      const BlockDecl *block = be->getBlockDecl();
533162017Ssam      for (BlockDecl::capture_const_iterator i = block->capture_begin(),
534162017Ssam           e = block->capture_end(); i != e; ++i) {
535162017Ssam        if (i->getVariable() == &var)
536162017Ssam          return true;
537162017Ssam      }
538162017Ssam    }
539162017Ssam  }
540162017Ssam
541162017Ssam  for (Stmt::const_child_range children = s->children(); children; ++children)
542127668Sbms    // children might be null; as in missing decl or conditional of an if-stmt.
543162017Ssam    if ((*children) && isAccessedBy(var, *children))
544162017Ssam      return true;
545162017Ssam
546127668Sbms  return false;
547127668Sbms}
548127668Sbms
549127668Sbmsstatic bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
550127668Sbms  if (!decl) return false;
551127668Sbms  if (!isa<VarDecl>(decl)) return false;
552127668Sbms  const VarDecl *var = cast<VarDecl>(decl);
553127668Sbms  return isAccessedBy(*var, e);
554162017Ssam}
555162017Ssam
556162017Ssamstatic void drillIntoBlockVariable(CodeGenFunction &CGF,
557162017Ssam                                   LValue &lvalue,
558127668Sbms                                   const VarDecl *var) {
559127668Sbms  lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
560127668Sbms}
561127668Sbms
56275115Sfennervoid CodeGenFunction::EmitScalarInit(const Expr *init,
56375115Sfenner                                     const ValueDecl *D,
56475115Sfenner                                     LValue lvalue,
56575115Sfenner                                     bool capturedByInit) {
56675115Sfenner  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
56775115Sfenner  if (!lifetime) {
56875115Sfenner    llvm::Value *value = EmitScalarExpr(init);
56975115Sfenner    if (capturedByInit)
57075115Sfenner      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
57175115Sfenner    EmitStoreThroughLValue(RValue::get(value), lvalue, true);
57275115Sfenner    return;
57375115Sfenner  }
574127668Sbms
57575115Sfenner  // If we're emitting a value with lifetime, we have to do the
57675115Sfenner  // initialization *before* we leave the cleanup scopes.
577127668Sbms  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) {
57875115Sfenner    enterFullExpression(ewc);
57975115Sfenner    init = ewc->getSubExpr();
58075115Sfenner  }
58175115Sfenner  CodeGenFunction::RunCleanupsScope Scope(*this);
58275115Sfenner
583127668Sbms  // We have to maintain the illusion that the variable is
58475115Sfenner  // zero-initialized.  If the variable might be accessed in its
58575115Sfenner  // initializer, zero-initialize before running the initializer, then
58675115Sfenner  // actually perform the initialization with an assign.
58775115Sfenner  bool accessedByInit = false;
588127668Sbms  if (lifetime != Qualifiers::OCL_ExplicitNone)
58975115Sfenner    accessedByInit = (capturedByInit || isAccessedBy(D, init));
59075115Sfenner  if (accessedByInit) {
59175115Sfenner    LValue tempLV = lvalue;
592127668Sbms    // Drill down to the __block object if necessary.
59375115Sfenner    if (capturedByInit) {
594127668Sbms      // We can use a simple GEP for this because it can't have been
59575115Sfenner      // moved yet.
59675115Sfenner      tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
59775115Sfenner                                   getByRefValueLLVMField(cast<VarDecl>(D))));
59875115Sfenner    }
59975115Sfenner
60075115Sfenner    llvm::PointerType *ty
60175115Sfenner      = cast<llvm::PointerType>(tempLV.getAddress()->getType());
602127668Sbms    ty = cast<llvm::PointerType>(ty->getElementType());
603111726Sfenner
604111726Sfenner    llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
605127668Sbms
60675115Sfenner    // If __weak, we want to use a barrier under certain conditions.
607127668Sbms    if (lifetime == Qualifiers::OCL_Weak)
60875115Sfenner      EmitARCInitWeak(tempLV.getAddress(), zero);
60975115Sfenner
61075115Sfenner    // Otherwise just do a simple store.
61175115Sfenner    else
61275115Sfenner      EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
61375115Sfenner  }
61475115Sfenner
615127668Sbms  // Emit the initializer.
61675115Sfenner  llvm::Value *value = 0;
617127668Sbms
61875115Sfenner  switch (lifetime) {
61975115Sfenner  case Qualifiers::OCL_None:
62075115Sfenner    llvm_unreachable("present but none");
62175115Sfenner
62275115Sfenner  case Qualifiers::OCL_ExplicitNone:
62375115Sfenner    // nothing to do
62475115Sfenner    value = EmitScalarExpr(init);
62575115Sfenner    break;
62675115Sfenner
627127668Sbms  case Qualifiers::OCL_Strong: {
62875115Sfenner    value = EmitARCRetainScalarExpr(init);
62975115Sfenner    break;
63075115Sfenner  }
631127668Sbms
63275115Sfenner  case Qualifiers::OCL_Weak: {
63375115Sfenner    // No way to optimize a producing initializer into this.  It's not
634127668Sbms    // worth optimizing for, because the value will immediately
635127668Sbms    // disappear in the common case.
63675115Sfenner    value = EmitScalarExpr(init);
63775115Sfenner
63875115Sfenner    if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
63975115Sfenner    if (accessedByInit)
64075115Sfenner      EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
641127668Sbms    else
642127668Sbms      EmitARCInitWeak(lvalue.getAddress(), value);
64375115Sfenner    return;
644127668Sbms  }
64575115Sfenner
646127668Sbms  case Qualifiers::OCL_Autoreleasing:
64775115Sfenner    value = EmitARCRetainAutoreleaseScalarExpr(init);
648127668Sbms    break;
649127668Sbms  }
65075115Sfenner
651127668Sbms  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
65275115Sfenner
65375115Sfenner  // If the variable might have been accessed by its initializer, we
65475115Sfenner  // might have to initialize with a barrier.  We have to do this for
65575115Sfenner  // both __weak and __strong, but __weak got filtered out above.
65675115Sfenner  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
65775115Sfenner    llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
658127668Sbms    EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
65975115Sfenner    EmitARCRelease(oldValue, ARCImpreciseLifetime);
660127668Sbms    return;
66175115Sfenner  }
66275115Sfenner
66375115Sfenner  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
664127668Sbms}
66575115Sfenner
666127668Sbms/// EmitScalarInit - Initialize the given lvalue with the given object.
66775115Sfennervoid CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
668127668Sbms  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
66975115Sfenner  if (!lifetime)
67075115Sfenner    return EmitStoreThroughLValue(RValue::get(init), lvalue, true);
67175115Sfenner
672127668Sbms  switch (lifetime) {
67375115Sfenner  case Qualifiers::OCL_None:
674162017Ssam    llvm_unreachable("present but none");
67575115Sfenner
67675115Sfenner  case Qualifiers::OCL_ExplicitNone:
67775115Sfenner    // nothing to do
67875115Sfenner    break;
67975115Sfenner
68075115Sfenner  case Qualifiers::OCL_Strong:
68175115Sfenner    init = EmitARCRetain(lvalue.getType(), init);
68275115Sfenner    break;
68375115Sfenner
68475115Sfenner  case Qualifiers::OCL_Weak:
68575115Sfenner    // Initialize and then skip the primitive store.
68675115Sfenner    EmitARCInitWeak(lvalue.getAddress(), init);
68775115Sfenner    return;
68875115Sfenner
68975115Sfenner  case Qualifiers::OCL_Autoreleasing:
690127668Sbms    init = EmitARCRetainAutorelease(lvalue.getType(), init);
69175115Sfenner    break;
69275115Sfenner  }
69375115Sfenner
69475115Sfenner  EmitStoreOfScalar(init, lvalue, /* isInitialization */ true);
695127668Sbms}
69675115Sfenner
69775115Sfenner/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
69875115Sfenner/// non-zero parts of the specified initializer with equal or fewer than
69975115Sfenner/// NumStores scalar stores.
70075115Sfennerstatic bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
701127668Sbms                                                unsigned &NumStores) {
70275115Sfenner  // Zero and Undef never requires any extra stores.
70375115Sfenner  if (isa<llvm::ConstantAggregateZero>(Init) ||
704127668Sbms      isa<llvm::ConstantPointerNull>(Init) ||
70575115Sfenner      isa<llvm::UndefValue>(Init))
706127668Sbms    return true;
70775115Sfenner  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
708127668Sbms      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
70975115Sfenner      isa<llvm::ConstantExpr>(Init))
710127668Sbms    return Init->isNullValue() || NumStores--;
71175115Sfenner
71275115Sfenner  // See if we can emit each element.
713127668Sbms  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
71475115Sfenner    for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
715127668Sbms      llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
71675115Sfenner      if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
717162017Ssam        return false;
71875115Sfenner    }
71975115Sfenner    return true;
72075115Sfenner  }
72175115Sfenner
72275115Sfenner  if (llvm::ConstantDataSequential *CDS =
72375115Sfenner        dyn_cast<llvm::ConstantDataSequential>(Init)) {
72475115Sfenner    for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
72575115Sfenner      llvm::Constant *Elt = CDS->getElementAsConstant(i);
72675115Sfenner      if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
72775115Sfenner        return false;
72875115Sfenner    }
729127668Sbms    return true;
73075115Sfenner  }
73175115Sfenner
73275115Sfenner  // Anything else is hard and scary.
73375115Sfenner  return false;
73475115Sfenner}
73575115Sfenner
736127668Sbms/// emitStoresForInitAfterMemset - For inits that
73775115Sfenner/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
73875115Sfenner/// stores that would be required.
73975115Sfennerstatic void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
74075115Sfenner                                         bool isVolatile, CGBuilderTy &Builder) {
741127668Sbms  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
74275115Sfenner         "called emitStoresForInitAfterMemset for zero or undef value.");
74398524Sfenner
74475115Sfenner  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
74575115Sfenner      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
746127668Sbms      isa<llvm::ConstantExpr>(Init)) {
74775115Sfenner    Builder.CreateStore(Init, Loc, isVolatile);
748127668Sbms    return;
74975115Sfenner  }
750162017Ssam
75175115Sfenner  if (llvm::ConstantDataSequential *CDS =
75275115Sfenner        dyn_cast<llvm::ConstantDataSequential>(Init)) {
753127668Sbms    for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
75475115Sfenner      llvm::Constant *Elt = CDS->getElementAsConstant(i);
75575115Sfenner
75675115Sfenner      // If necessary, get a pointer to the element and emit it.
75775115Sfenner      if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
75875115Sfenner        emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
75975115Sfenner                                     isVolatile, Builder);
76075115Sfenner    }
76175115Sfenner    return;
76275115Sfenner  }
76375115Sfenner
764127668Sbms  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
76575115Sfenner         "Unknown value type!");
76675115Sfenner
76775115Sfenner  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
76875115Sfenner    llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
76975115Sfenner
770127668Sbms    // If necessary, get a pointer to the element and emit it.
77175115Sfenner    if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
77275115Sfenner      emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
773127668Sbms                                   isVolatile, Builder);
77475115Sfenner  }
77575115Sfenner}
77675115Sfenner
777127668Sbms
77875115Sfenner/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
77975115Sfenner/// plus some stores to initialize a local variable instead of using a memcpy
78075115Sfenner/// from a constant global.  It is beneficial to use memset if the global is all
781127668Sbms/// zeros, or mostly zeros and large.
78275115Sfennerstatic bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
783127668Sbms                                                  uint64_t GlobalSize) {
78475115Sfenner  // If a global is all zeros, always use a memset.
78575115Sfenner  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
78675115Sfenner
787127668Sbms  // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
78875115Sfenner  // do it if it will require 6 or fewer scalar stores.
78975115Sfenner  // TODO: Should budget depends on the size?  Avoiding a large global warrants
79075115Sfenner  // plopping in more stores.
79175115Sfenner  unsigned StoreBudget = 6;
792127668Sbms  uint64_t SizeLimit = 32;
79375115Sfenner
794127668Sbms  return GlobalSize > SizeLimit &&
79575115Sfenner         canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
79675115Sfenner}
797127668Sbms
79875115Sfenner/// Should we use the LLVM lifetime intrinsics for the given local variable?
799127668Sbmsstatic bool shouldUseLifetimeMarkers(CodeGenFunction &CGF, const VarDecl &D,
80075115Sfenner                                     unsigned Size) {
80175115Sfenner  // Always emit lifetime markers in -fsanitize=use-after-scope mode.
80275115Sfenner  if (CGF.getLangOpts().Sanitize.UseAfterScope)
803127668Sbms    return true;
80475115Sfenner  // For now, only in optimized builds.
80575115Sfenner  if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0)
80675115Sfenner    return false;
807127668Sbms
808162017Ssam  // Limit the size of marked objects to 32 bytes. We don't want to increase
80975115Sfenner  // compile time by marking tiny objects.
81075115Sfenner  unsigned SizeThreshold = 32;
81175115Sfenner
81275115Sfenner  return Size > SizeThreshold;
81375115Sfenner}
81475115Sfenner
81575115Sfenner
816127668Sbms/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
81775115Sfenner/// variable declaration with auto, register, or no storage class specifier.
81875115Sfenner/// These turn into simple stack objects, or GlobalValues depending on target.
81975115Sfennervoid CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
82075115Sfenner  AutoVarEmission emission = EmitAutoVarAlloca(D);
82175115Sfenner  EmitAutoVarInit(emission);
82275115Sfenner  EmitAutoVarCleanups(emission);
82375115Sfenner}
824162017Ssam
825127668Sbms/// EmitAutoVarAlloca - Emit the alloca and debug information for a
82675115Sfenner/// local variable.  Does not emit initalization or destruction.
827162017SsamCodeGenFunction::AutoVarEmission
82875115SfennerCodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
82975115Sfenner  QualType Ty = D.getType();
83075115Sfenner
83175115Sfenner  AutoVarEmission emission(D);
83275115Sfenner
833162017Ssam  bool isByRef = D.hasAttr<BlocksAttr>();
83475115Sfenner  emission.IsByRef = isByRef;
83575115Sfenner
836162017Ssam  CharUnits alignment = getContext().getDeclAlign(&D);
837127668Sbms  emission.Alignment = alignment;
83875115Sfenner
83975115Sfenner  // If the type is variably-modified, emit all the VLA sizes for it.
840162017Ssam  if (Ty->isVariablyModifiedType())
841162017Ssam    EmitVariablyModifiedType(Ty);
842162017Ssam
843162017Ssam  llvm::Value *DeclPtr;
844162017Ssam  if (Ty->isConstantSizeType()) {
845162017Ssam    bool NRVO = getLangOpts().ElideConstructors &&
846162017Ssam      D.isNRVOVariable();
847162017Ssam
848162017Ssam    // If this value is an array or struct with a statically determinable
84998524Sfenner    // constant initializer, there are optimizations we can do.
850162017Ssam    //
851162017Ssam    // TODO: We should constant-evaluate the initializer of any variable,
852162017Ssam    // as long as it is initialized by a constant expression. Currently,
853162017Ssam    // isConstantInitializer produces wrong answers for structs with
854127668Sbms    // reference or bitfield members, and a few other cases, and checking
85598524Sfenner    // for POD-ness protects us from some of these.
856162017Ssam    if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
85775115Sfenner        (D.isConstexpr() ||
858162017Ssam         ((Ty.isPODType(getContext()) ||
859162017Ssam           getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
860162017Ssam          D.getInit()->isConstantInitializer(getContext(), false)))) {
861162017Ssam
862162017Ssam      // If the variable's a const type, and it's neither an NRVO
863162017Ssam      // candidate nor a __block variable and has no mutable members,
864162017Ssam      // emit it as a global instead.
865162017Ssam      if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef &&
866162017Ssam          CGM.isTypeConstant(Ty, true)) {
867162017Ssam        EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
86875115Sfenner
869162017Ssam        emission.Address = 0; // signal this condition to later callbacks
870162017Ssam        assert(emission.wasEmittedAsGlobal());
871162017Ssam        return emission;
872162017Ssam      }
873162017Ssam
874162017Ssam      // Otherwise, tell the initialization code that we're in this case.
875162017Ssam      emission.IsConstantAggregate = true;
876162017Ssam    }
877127668Sbms
87875115Sfenner    // A normal fixed sized variable becomes an alloca in the entry block,
879146773Ssam    // unless it's an NRVO variable.
880162017Ssam    llvm::Type *LTy = ConvertTypeForMem(Ty);
881146773Ssam
882127668Sbms    if (NRVO) {
88375115Sfenner      // The named return value optimization: allocate this variable in the
88475115Sfenner      // return slot, so that we can elide the copy when returning this
88575115Sfenner      // variable (C++0x [class.copy]p34).
886162017Ssam      DeclPtr = ReturnValue;
887162017Ssam
888162017Ssam      if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
889162017Ssam        if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
89075115Sfenner          // Create a flag that is used to indicate when the NRVO was applied
89175115Sfenner          // to this variable. Set it to zero to indicate that NRVO was not
89275115Sfenner          // applied.
89375115Sfenner          llvm::Value *Zero = Builder.getFalse();
89475115Sfenner          llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
89575115Sfenner          EnsureInsertPoint();
89675115Sfenner          Builder.CreateStore(Zero, NRVOFlag);
897127668Sbms
89875115Sfenner          // Record the NRVO flag for this variable.
899162017Ssam          NRVOFlags[&D] = NRVOFlag;
90075115Sfenner          emission.NRVOFlag = NRVOFlag;
901127668Sbms        }
90298524Sfenner      }
90398524Sfenner    } else {
90498524Sfenner      if (isByRef)
90598524Sfenner        LTy = BuildByRefType(&D);
90698524Sfenner
90798524Sfenner      llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
90898524Sfenner      Alloc->setName(D.getName());
909162017Ssam
910162017Ssam      CharUnits allocaAlignment = alignment;
911127668Sbms      if (isByRef)
912127668Sbms        allocaAlignment = std::max(allocaAlignment,
913127668Sbms            getContext().toCharUnitsFromBits(getTarget().getPointerAlign(0)));
914127668Sbms      Alloc->setAlignment(allocaAlignment.getQuantity());
915127668Sbms      DeclPtr = Alloc;
916127668Sbms
917162017Ssam      // Emit a lifetime intrinsic if meaningful.  There's no point
918127668Sbms      // in doing this if we don't have a valid insertion point (?).
919127668Sbms      uint64_t size = CGM.getDataLayout().getTypeAllocSize(LTy);
920127668Sbms      if (HaveInsertPoint() && shouldUseLifetimeMarkers(*this, D, size)) {
921127668Sbms        llvm::Value *sizeV = llvm::ConstantInt::get(Int64Ty, size);
922162017Ssam
923127668Sbms        emission.SizeForLifetimeMarkers = sizeV;
924127668Sbms        llvm::Value *castAddr = Builder.CreateBitCast(Alloc, Int8PtrTy);
925127668Sbms        Builder.CreateCall2(CGM.getLLVMLifetimeStartFn(), sizeV, castAddr)
92675115Sfenner          ->setDoesNotThrow();
927127668Sbms      } else {
928127668Sbms        assert(!emission.useLifetimeMarkers());
929127668Sbms      }
93075115Sfenner    }
931162017Ssam  } else {
932162017Ssam    EnsureInsertPoint();
933162017Ssam
934162017Ssam    if (!DidCallStackSave) {
935162017Ssam      // Save the stack.
936162017Ssam      llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
93775115Sfenner
938      llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
939      llvm::Value *V = Builder.CreateCall(F);
940
941      Builder.CreateStore(V, Stack);
942
943      DidCallStackSave = true;
944
945      // Push a cleanup block and restore the stack there.
946      // FIXME: in general circumstances, this should be an EH cleanup.
947      EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
948    }
949
950    llvm::Value *elementCount;
951    QualType elementType;
952    llvm::tie(elementCount, elementType) = getVLASize(Ty);
953
954    llvm::Type *llvmTy = ConvertTypeForMem(elementType);
955
956    // Allocate memory for the array.
957    llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
958    vla->setAlignment(alignment.getQuantity());
959
960    DeclPtr = vla;
961  }
962
963  llvm::Value *&DMEntry = LocalDeclMap[&D];
964  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
965  DMEntry = DeclPtr;
966  emission.Address = DeclPtr;
967
968  // Emit debug info for local var declaration.
969  if (HaveInsertPoint())
970    if (CGDebugInfo *DI = getDebugInfo()) {
971      if (CGM.getCodeGenOpts().getDebugInfo()
972            >= CodeGenOptions::LimitedDebugInfo) {
973        DI->setLocation(D.getLocation());
974        DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
975      }
976    }
977
978  if (D.hasAttr<AnnotateAttr>())
979      EmitVarAnnotations(&D, emission.Address);
980
981  return emission;
982}
983
984/// Determines whether the given __block variable is potentially
985/// captured by the given expression.
986static bool isCapturedBy(const VarDecl &var, const Expr *e) {
987  // Skip the most common kinds of expressions that make
988  // hierarchy-walking expensive.
989  e = e->IgnoreParenCasts();
990
991  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
992    const BlockDecl *block = be->getBlockDecl();
993    for (BlockDecl::capture_const_iterator i = block->capture_begin(),
994           e = block->capture_end(); i != e; ++i) {
995      if (i->getVariable() == &var)
996        return true;
997    }
998
999    // No need to walk into the subexpressions.
1000    return false;
1001  }
1002
1003  if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
1004    const CompoundStmt *CS = SE->getSubStmt();
1005    for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
1006	   BE = CS->body_end(); BI != BE; ++BI)
1007      if (Expr *E = dyn_cast<Expr>((*BI))) {
1008        if (isCapturedBy(var, E))
1009            return true;
1010      }
1011      else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) {
1012          // special case declarations
1013          for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1014               I != E; ++I) {
1015              if (VarDecl *VD = dyn_cast<VarDecl>((*I))) {
1016                Expr *Init = VD->getInit();
1017                if (Init && isCapturedBy(var, Init))
1018                  return true;
1019              }
1020          }
1021      }
1022      else
1023        // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1024        // Later, provide code to poke into statements for capture analysis.
1025        return true;
1026    return false;
1027  }
1028
1029  for (Stmt::const_child_range children = e->children(); children; ++children)
1030    if (isCapturedBy(var, cast<Expr>(*children)))
1031      return true;
1032
1033  return false;
1034}
1035
1036/// \brief Determine whether the given initializer is trivial in the sense
1037/// that it requires no code to be generated.
1038static bool isTrivialInitializer(const Expr *Init) {
1039  if (!Init)
1040    return true;
1041
1042  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1043    if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1044      if (Constructor->isTrivial() &&
1045          Constructor->isDefaultConstructor() &&
1046          !Construct->requiresZeroInitialization())
1047        return true;
1048
1049  return false;
1050}
1051void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
1052  assert(emission.Variable && "emission was not valid!");
1053
1054  // If this was emitted as a global constant, we're done.
1055  if (emission.wasEmittedAsGlobal()) return;
1056
1057  const VarDecl &D = *emission.Variable;
1058  QualType type = D.getType();
1059
1060  // If this local has an initializer, emit it now.
1061  const Expr *Init = D.getInit();
1062
1063  // If we are at an unreachable point, we don't need to emit the initializer
1064  // unless it contains a label.
1065  if (!HaveInsertPoint()) {
1066    if (!Init || !ContainsLabel(Init)) return;
1067    EnsureInsertPoint();
1068  }
1069
1070  // Initialize the structure of a __block variable.
1071  if (emission.IsByRef)
1072    emitByrefStructureInit(emission);
1073
1074  if (isTrivialInitializer(Init))
1075    return;
1076
1077  CharUnits alignment = emission.Alignment;
1078
1079  // Check whether this is a byref variable that's potentially
1080  // captured and moved by its own initializer.  If so, we'll need to
1081  // emit the initializer first, then copy into the variable.
1082  bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
1083
1084  llvm::Value *Loc =
1085    capturedByInit ? emission.Address : emission.getObjectAddress(*this);
1086
1087  llvm::Constant *constant = 0;
1088  if (emission.IsConstantAggregate || D.isConstexpr()) {
1089    assert(!capturedByInit && "constant init contains a capturing block?");
1090    constant = CGM.EmitConstantInit(D, this);
1091  }
1092
1093  if (!constant) {
1094    LValue lv = MakeAddrLValue(Loc, type, alignment);
1095    lv.setNonGC(true);
1096    return EmitExprAsInit(Init, &D, lv, capturedByInit);
1097  }
1098
1099  if (!emission.IsConstantAggregate) {
1100    // For simple scalar/complex initialization, store the value directly.
1101    LValue lv = MakeAddrLValue(Loc, type, alignment);
1102    lv.setNonGC(true);
1103    return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1104  }
1105
1106  // If this is a simple aggregate initialization, we can optimize it
1107  // in various ways.
1108  bool isVolatile = type.isVolatileQualified();
1109
1110  llvm::Value *SizeVal =
1111    llvm::ConstantInt::get(IntPtrTy,
1112                           getContext().getTypeSizeInChars(type).getQuantity());
1113
1114  llvm::Type *BP = Int8PtrTy;
1115  if (Loc->getType() != BP)
1116    Loc = Builder.CreateBitCast(Loc, BP);
1117
1118  // If the initializer is all or mostly zeros, codegen with memset then do
1119  // a few stores afterward.
1120  if (shouldUseMemSetPlusStoresToInitialize(constant,
1121                CGM.getDataLayout().getTypeAllocSize(constant->getType()))) {
1122    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1123                         alignment.getQuantity(), isVolatile);
1124    // Zero and undef don't require a stores.
1125    if (!constant->isNullValue() && !isa<llvm::UndefValue>(constant)) {
1126      Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
1127      emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
1128    }
1129  } else {
1130    // Otherwise, create a temporary global with the initializer then
1131    // memcpy from the global to the alloca.
1132    std::string Name = GetStaticDeclName(*this, D, ".");
1133    llvm::GlobalVariable *GV =
1134      new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
1135                               llvm::GlobalValue::PrivateLinkage,
1136                               constant, Name);
1137    GV->setAlignment(alignment.getQuantity());
1138    GV->setUnnamedAddr(true);
1139
1140    llvm::Value *SrcPtr = GV;
1141    if (SrcPtr->getType() != BP)
1142      SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1143
1144    Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
1145                         isVolatile);
1146  }
1147}
1148
1149/// Emit an expression as an initializer for a variable at the given
1150/// location.  The expression is not necessarily the normal
1151/// initializer for the variable, and the address is not necessarily
1152/// its normal location.
1153///
1154/// \param init the initializing expression
1155/// \param var the variable to act as if we're initializing
1156/// \param loc the address to initialize; its type is a pointer
1157///   to the LLVM mapping of the variable's type
1158/// \param alignment the alignment of the address
1159/// \param capturedByInit true if the variable is a __block variable
1160///   whose address is potentially changed by the initializer
1161void CodeGenFunction::EmitExprAsInit(const Expr *init,
1162                                     const ValueDecl *D,
1163                                     LValue lvalue,
1164                                     bool capturedByInit) {
1165  QualType type = D->getType();
1166
1167  if (type->isReferenceType()) {
1168    RValue rvalue = EmitReferenceBindingToExpr(init);
1169    if (capturedByInit)
1170      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1171    EmitStoreThroughLValue(rvalue, lvalue, true);
1172    return;
1173  }
1174  switch (getEvaluationKind(type)) {
1175  case TEK_Scalar:
1176    EmitScalarInit(init, D, lvalue, capturedByInit);
1177    return;
1178  case TEK_Complex: {
1179    ComplexPairTy complex = EmitComplexExpr(init);
1180    if (capturedByInit)
1181      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1182    EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1183    return;
1184  }
1185  case TEK_Aggregate:
1186    if (type->isAtomicType()) {
1187      EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1188    } else {
1189      // TODO: how can we delay here if D is captured by its initializer?
1190      EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1191                                              AggValueSlot::IsDestructed,
1192                                         AggValueSlot::DoesNotNeedGCBarriers,
1193                                              AggValueSlot::IsNotAliased));
1194    }
1195    return;
1196  }
1197  llvm_unreachable("bad evaluation kind");
1198}
1199
1200/// Enter a destroy cleanup for the given local variable.
1201void CodeGenFunction::emitAutoVarTypeCleanup(
1202                            const CodeGenFunction::AutoVarEmission &emission,
1203                            QualType::DestructionKind dtorKind) {
1204  assert(dtorKind != QualType::DK_none);
1205
1206  // Note that for __block variables, we want to destroy the
1207  // original stack object, not the possibly forwarded object.
1208  llvm::Value *addr = emission.getObjectAddress(*this);
1209
1210  const VarDecl *var = emission.Variable;
1211  QualType type = var->getType();
1212
1213  CleanupKind cleanupKind = NormalAndEHCleanup;
1214  CodeGenFunction::Destroyer *destroyer = 0;
1215
1216  switch (dtorKind) {
1217  case QualType::DK_none:
1218    llvm_unreachable("no cleanup for trivially-destructible variable");
1219
1220  case QualType::DK_cxx_destructor:
1221    // If there's an NRVO flag on the emission, we need a different
1222    // cleanup.
1223    if (emission.NRVOFlag) {
1224      assert(!type->isArrayType());
1225      CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1226      EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor,
1227                                               emission.NRVOFlag);
1228      return;
1229    }
1230    break;
1231
1232  case QualType::DK_objc_strong_lifetime:
1233    // Suppress cleanups for pseudo-strong variables.
1234    if (var->isARCPseudoStrong()) return;
1235
1236    // Otherwise, consider whether to use an EH cleanup or not.
1237    cleanupKind = getARCCleanupKind();
1238
1239    // Use the imprecise destroyer by default.
1240    if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1241      destroyer = CodeGenFunction::destroyARCStrongImprecise;
1242    break;
1243
1244  case QualType::DK_objc_weak_lifetime:
1245    break;
1246  }
1247
1248  // If we haven't chosen a more specific destroyer, use the default.
1249  if (!destroyer) destroyer = getDestroyer(dtorKind);
1250
1251  // Use an EH cleanup in array destructors iff the destructor itself
1252  // is being pushed as an EH cleanup.
1253  bool useEHCleanup = (cleanupKind & EHCleanup);
1254  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1255                                     useEHCleanup);
1256}
1257
1258void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
1259  assert(emission.Variable && "emission was not valid!");
1260
1261  // If this was emitted as a global constant, we're done.
1262  if (emission.wasEmittedAsGlobal()) return;
1263
1264  // If we don't have an insertion point, we're done.  Sema prevents
1265  // us from jumping into any of these scopes anyway.
1266  if (!HaveInsertPoint()) return;
1267
1268  const VarDecl &D = *emission.Variable;
1269
1270  // Make sure we call @llvm.lifetime.end.  This needs to happen
1271  // *last*, so the cleanup needs to be pushed *first*.
1272  if (emission.useLifetimeMarkers()) {
1273    EHStack.pushCleanup<CallLifetimeEnd>(NormalCleanup,
1274                                         emission.getAllocatedAddress(),
1275                                         emission.getSizeForLifetimeMarkers());
1276  }
1277
1278  // Check the type for a cleanup.
1279  if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1280    emitAutoVarTypeCleanup(emission, dtorKind);
1281
1282  // In GC mode, honor objc_precise_lifetime.
1283  if (getLangOpts().getGC() != LangOptions::NonGC &&
1284      D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1285    EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1286  }
1287
1288  // Handle the cleanup attribute.
1289  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1290    const FunctionDecl *FD = CA->getFunctionDecl();
1291
1292    llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1293    assert(F && "Could not find function!");
1294
1295    const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
1296    EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1297  }
1298
1299  // If this is a block variable, call _Block_object_destroy
1300  // (on the unforwarded address).
1301  if (emission.IsByRef)
1302    enterByrefCleanup(emission);
1303}
1304
1305CodeGenFunction::Destroyer *
1306CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
1307  switch (kind) {
1308  case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
1309  case QualType::DK_cxx_destructor:
1310    return destroyCXXObject;
1311  case QualType::DK_objc_strong_lifetime:
1312    return destroyARCStrongPrecise;
1313  case QualType::DK_objc_weak_lifetime:
1314    return destroyARCWeak;
1315  }
1316  llvm_unreachable("Unknown DestructionKind");
1317}
1318
1319/// pushEHDestroy - Push the standard destructor for the given type as
1320/// an EH-only cleanup.
1321void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind,
1322                                  llvm::Value *addr, QualType type) {
1323  assert(dtorKind && "cannot push destructor for trivial type");
1324  assert(needsEHCleanup(dtorKind));
1325
1326  pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
1327}
1328
1329/// pushDestroy - Push the standard destructor for the given type as
1330/// at least a normal cleanup.
1331void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
1332                                  llvm::Value *addr, QualType type) {
1333  assert(dtorKind && "cannot push destructor for trivial type");
1334
1335  CleanupKind cleanupKind = getCleanupKind(dtorKind);
1336  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
1337              cleanupKind & EHCleanup);
1338}
1339
1340void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr,
1341                                  QualType type, Destroyer *destroyer,
1342                                  bool useEHCleanupForArray) {
1343  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
1344                                     destroyer, useEHCleanupForArray);
1345}
1346
1347void CodeGenFunction::pushLifetimeExtendedDestroy(
1348    CleanupKind cleanupKind, llvm::Value *addr, QualType type,
1349    Destroyer *destroyer, bool useEHCleanupForArray) {
1350  assert(!isInConditionalBranch() &&
1351         "performing lifetime extension from within conditional");
1352
1353  // Push an EH-only cleanup for the object now.
1354  // FIXME: When popping normal cleanups, we need to keep this EH cleanup
1355  // around in case a temporary's destructor throws an exception.
1356  if (cleanupKind & EHCleanup)
1357    EHStack.pushCleanup<DestroyObject>(
1358        static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
1359        destroyer, useEHCleanupForArray);
1360
1361  // Remember that we need to push a full cleanup for the object at the
1362  // end of the full-expression.
1363  pushCleanupAfterFullExpr<DestroyObject>(
1364      cleanupKind, addr, type, destroyer, useEHCleanupForArray);
1365}
1366
1367/// emitDestroy - Immediately perform the destruction of the given
1368/// object.
1369///
1370/// \param addr - the address of the object; a type*
1371/// \param type - the type of the object; if an array type, all
1372///   objects are destroyed in reverse order
1373/// \param destroyer - the function to call to destroy individual
1374///   elements
1375/// \param useEHCleanupForArray - whether an EH cleanup should be
1376///   used when destroying array elements, in case one of the
1377///   destructions throws an exception
1378void CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type,
1379                                  Destroyer *destroyer,
1380                                  bool useEHCleanupForArray) {
1381  const ArrayType *arrayType = getContext().getAsArrayType(type);
1382  if (!arrayType)
1383    return destroyer(*this, addr, type);
1384
1385  llvm::Value *begin = addr;
1386  llvm::Value *length = emitArrayLength(arrayType, type, begin);
1387
1388  // Normally we have to check whether the array is zero-length.
1389  bool checkZeroLength = true;
1390
1391  // But if the array length is constant, we can suppress that.
1392  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
1393    // ...and if it's constant zero, we can just skip the entire thing.
1394    if (constLength->isZero()) return;
1395    checkZeroLength = false;
1396  }
1397
1398  llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
1399  emitArrayDestroy(begin, end, type, destroyer,
1400                   checkZeroLength, useEHCleanupForArray);
1401}
1402
1403/// emitArrayDestroy - Destroys all the elements of the given array,
1404/// beginning from last to first.  The array cannot be zero-length.
1405///
1406/// \param begin - a type* denoting the first element of the array
1407/// \param end - a type* denoting one past the end of the array
1408/// \param type - the element type of the array
1409/// \param destroyer - the function to call to destroy elements
1410/// \param useEHCleanup - whether to push an EH cleanup to destroy
1411///   the remaining elements in case the destruction of a single
1412///   element throws
1413void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
1414                                       llvm::Value *end,
1415                                       QualType type,
1416                                       Destroyer *destroyer,
1417                                       bool checkZeroLength,
1418                                       bool useEHCleanup) {
1419  assert(!type->isArrayType());
1420
1421  // The basic structure here is a do-while loop, because we don't
1422  // need to check for the zero-element case.
1423  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
1424  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
1425
1426  if (checkZeroLength) {
1427    llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
1428                                                "arraydestroy.isempty");
1429    Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
1430  }
1431
1432  // Enter the loop body, making that address the current address.
1433  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1434  EmitBlock(bodyBB);
1435  llvm::PHINode *elementPast =
1436    Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
1437  elementPast->addIncoming(end, entryBB);
1438
1439  // Shift the address back by one element.
1440  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
1441  llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
1442                                                   "arraydestroy.element");
1443
1444  if (useEHCleanup)
1445    pushRegularPartialArrayCleanup(begin, element, type, destroyer);
1446
1447  // Perform the actual destruction there.
1448  destroyer(*this, element, type);
1449
1450  if (useEHCleanup)
1451    PopCleanupBlock();
1452
1453  // Check whether we've reached the end.
1454  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
1455  Builder.CreateCondBr(done, doneBB, bodyBB);
1456  elementPast->addIncoming(element, Builder.GetInsertBlock());
1457
1458  // Done.
1459  EmitBlock(doneBB);
1460}
1461
1462/// Perform partial array destruction as if in an EH cleanup.  Unlike
1463/// emitArrayDestroy, the element type here may still be an array type.
1464static void emitPartialArrayDestroy(CodeGenFunction &CGF,
1465                                    llvm::Value *begin, llvm::Value *end,
1466                                    QualType type,
1467                                    CodeGenFunction::Destroyer *destroyer) {
1468  // If the element type is itself an array, drill down.
1469  unsigned arrayDepth = 0;
1470  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
1471    // VLAs don't require a GEP index to walk into.
1472    if (!isa<VariableArrayType>(arrayType))
1473      arrayDepth++;
1474    type = arrayType->getElementType();
1475  }
1476
1477  if (arrayDepth) {
1478    llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, arrayDepth+1);
1479
1480    SmallVector<llvm::Value*,4> gepIndices(arrayDepth, zero);
1481    begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
1482    end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
1483  }
1484
1485  // Destroy the array.  We don't ever need an EH cleanup because we
1486  // assume that we're in an EH cleanup ourselves, so a throwing
1487  // destructor causes an immediate terminate.
1488  CGF.emitArrayDestroy(begin, end, type, destroyer,
1489                       /*checkZeroLength*/ true, /*useEHCleanup*/ false);
1490}
1491
1492namespace {
1493  /// RegularPartialArrayDestroy - a cleanup which performs a partial
1494  /// array destroy where the end pointer is regularly determined and
1495  /// does not need to be loaded from a local.
1496  class RegularPartialArrayDestroy : public EHScopeStack::Cleanup {
1497    llvm::Value *ArrayBegin;
1498    llvm::Value *ArrayEnd;
1499    QualType ElementType;
1500    CodeGenFunction::Destroyer *Destroyer;
1501  public:
1502    RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
1503                               QualType elementType,
1504                               CodeGenFunction::Destroyer *destroyer)
1505      : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
1506        ElementType(elementType), Destroyer(destroyer) {}
1507
1508    void Emit(CodeGenFunction &CGF, Flags flags) {
1509      emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
1510                              ElementType, Destroyer);
1511    }
1512  };
1513
1514  /// IrregularPartialArrayDestroy - a cleanup which performs a
1515  /// partial array destroy where the end pointer is irregularly
1516  /// determined and must be loaded from a local.
1517  class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup {
1518    llvm::Value *ArrayBegin;
1519    llvm::Value *ArrayEndPointer;
1520    QualType ElementType;
1521    CodeGenFunction::Destroyer *Destroyer;
1522  public:
1523    IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
1524                                 llvm::Value *arrayEndPointer,
1525                                 QualType elementType,
1526                                 CodeGenFunction::Destroyer *destroyer)
1527      : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
1528        ElementType(elementType), Destroyer(destroyer) {}
1529
1530    void Emit(CodeGenFunction &CGF, Flags flags) {
1531      llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
1532      emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
1533                              ElementType, Destroyer);
1534    }
1535  };
1536}
1537
1538/// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
1539/// already-constructed elements of the given array.  The cleanup
1540/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1541///
1542/// \param elementType - the immediate element type of the array;
1543///   possibly still an array type
1544void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1545                                                 llvm::Value *arrayEndPointer,
1546                                                       QualType elementType,
1547                                                       Destroyer *destroyer) {
1548  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
1549                                                    arrayBegin, arrayEndPointer,
1550                                                    elementType, destroyer);
1551}
1552
1553/// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
1554/// already-constructed elements of the given array.  The cleanup
1555/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1556///
1557/// \param elementType - the immediate element type of the array;
1558///   possibly still an array type
1559void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1560                                                     llvm::Value *arrayEnd,
1561                                                     QualType elementType,
1562                                                     Destroyer *destroyer) {
1563  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
1564                                                  arrayBegin, arrayEnd,
1565                                                  elementType, destroyer);
1566}
1567
1568/// Lazily declare the @llvm.lifetime.start intrinsic.
1569llvm::Constant *CodeGenModule::getLLVMLifetimeStartFn() {
1570  if (LifetimeStartFn) return LifetimeStartFn;
1571  LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
1572                                            llvm::Intrinsic::lifetime_start);
1573  return LifetimeStartFn;
1574}
1575
1576/// Lazily declare the @llvm.lifetime.end intrinsic.
1577llvm::Constant *CodeGenModule::getLLVMLifetimeEndFn() {
1578  if (LifetimeEndFn) return LifetimeEndFn;
1579  LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
1580                                              llvm::Intrinsic::lifetime_end);
1581  return LifetimeEndFn;
1582}
1583
1584namespace {
1585  /// A cleanup to perform a release of an object at the end of a
1586  /// function.  This is used to balance out the incoming +1 of a
1587  /// ns_consumed argument when we can't reasonably do that just by
1588  /// not doing the initial retain for a __block argument.
1589  struct ConsumeARCParameter : EHScopeStack::Cleanup {
1590    ConsumeARCParameter(llvm::Value *param,
1591                        ARCPreciseLifetime_t precise)
1592      : Param(param), Precise(precise) {}
1593
1594    llvm::Value *Param;
1595    ARCPreciseLifetime_t Precise;
1596
1597    void Emit(CodeGenFunction &CGF, Flags flags) {
1598      CGF.EmitARCRelease(Param, Precise);
1599    }
1600  };
1601}
1602
1603/// Emit an alloca (or GlobalValue depending on target)
1604/// for the specified parameter and set up LocalDeclMap.
1605void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
1606                                   unsigned ArgNo) {
1607  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1608  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1609         "Invalid argument to EmitParmDecl");
1610
1611  Arg->setName(D.getName());
1612
1613  QualType Ty = D.getType();
1614
1615  // Use better IR generation for certain implicit parameters.
1616  if (isa<ImplicitParamDecl>(D)) {
1617    // The only implicit argument a block has is its literal.
1618    if (BlockInfo) {
1619      LocalDeclMap[&D] = Arg;
1620      llvm::Value *LocalAddr = 0;
1621      if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1622        // Allocate a stack slot to let the debug info survive the RA.
1623        llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1624                                                   D.getName() + ".addr");
1625        Alloc->setAlignment(getContext().getDeclAlign(&D).getQuantity());
1626        LValue lv = MakeAddrLValue(Alloc, Ty, getContext().getDeclAlign(&D));
1627        EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1628        LocalAddr = Builder.CreateLoad(Alloc);
1629      }
1630
1631      if (CGDebugInfo *DI = getDebugInfo()) {
1632        if (CGM.getCodeGenOpts().getDebugInfo()
1633              >= CodeGenOptions::LimitedDebugInfo) {
1634          DI->setLocation(D.getLocation());
1635          DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, LocalAddr, Builder);
1636        }
1637      }
1638
1639      return;
1640    }
1641  }
1642
1643  llvm::Value *DeclPtr;
1644  bool HasNonScalarEvalKind = !CodeGenFunction::hasScalarEvaluationKind(Ty);
1645  // If this is an aggregate or variable sized value, reuse the input pointer.
1646  if (HasNonScalarEvalKind || !Ty->isConstantSizeType()) {
1647    DeclPtr = Arg;
1648    // Push a destructor cleanup for this parameter if the ABI requires it.
1649    if (HasNonScalarEvalKind &&
1650        getTarget().getCXXABI().isArgumentDestroyedByCallee()) {
1651      if (const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl()) {
1652        if (RD->hasNonTrivialDestructor())
1653          pushDestroy(QualType::DK_cxx_destructor, DeclPtr, Ty);
1654      }
1655    }
1656  } else {
1657    // Otherwise, create a temporary to hold the value.
1658    llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1659                                               D.getName() + ".addr");
1660    CharUnits Align = getContext().getDeclAlign(&D);
1661    Alloc->setAlignment(Align.getQuantity());
1662    DeclPtr = Alloc;
1663
1664    bool doStore = true;
1665
1666    Qualifiers qs = Ty.getQualifiers();
1667    LValue lv = MakeAddrLValue(DeclPtr, Ty, Align);
1668    if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1669      // We honor __attribute__((ns_consumed)) for types with lifetime.
1670      // For __strong, it's handled by just skipping the initial retain;
1671      // otherwise we have to balance out the initial +1 with an extra
1672      // cleanup to do the release at the end of the function.
1673      bool isConsumed = D.hasAttr<NSConsumedAttr>();
1674
1675      // 'self' is always formally __strong, but if this is not an
1676      // init method then we don't want to retain it.
1677      if (D.isARCPseudoStrong()) {
1678        const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1679        assert(&D == method->getSelfDecl());
1680        assert(lt == Qualifiers::OCL_Strong);
1681        assert(qs.hasConst());
1682        assert(method->getMethodFamily() != OMF_init);
1683        (void) method;
1684        lt = Qualifiers::OCL_ExplicitNone;
1685      }
1686
1687      if (lt == Qualifiers::OCL_Strong) {
1688        if (!isConsumed) {
1689          if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1690            // use objc_storeStrong(&dest, value) for retaining the
1691            // object. But first, store a null into 'dest' because
1692            // objc_storeStrong attempts to release its old value.
1693            llvm::Value *Null = CGM.EmitNullConstant(D.getType());
1694            EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
1695            EmitARCStoreStrongCall(lv.getAddress(), Arg, true);
1696            doStore = false;
1697          }
1698          else
1699          // Don't use objc_retainBlock for block pointers, because we
1700          // don't want to Block_copy something just because we got it
1701          // as a parameter.
1702            Arg = EmitARCRetainNonBlock(Arg);
1703        }
1704      } else {
1705        // Push the cleanup for a consumed parameter.
1706        if (isConsumed) {
1707          ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
1708                                ? ARCPreciseLifetime : ARCImpreciseLifetime);
1709          EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg,
1710                                                   precise);
1711        }
1712
1713        if (lt == Qualifiers::OCL_Weak) {
1714          EmitARCInitWeak(DeclPtr, Arg);
1715          doStore = false; // The weak init is a store, no need to do two.
1716        }
1717      }
1718
1719      // Enter the cleanup scope.
1720      EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1721    }
1722
1723    // Store the initial value into the alloca.
1724    if (doStore)
1725      EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1726  }
1727
1728  llvm::Value *&DMEntry = LocalDeclMap[&D];
1729  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
1730  DMEntry = DeclPtr;
1731
1732  // Emit debug info for param declaration.
1733  if (CGDebugInfo *DI = getDebugInfo()) {
1734    if (CGM.getCodeGenOpts().getDebugInfo()
1735          >= CodeGenOptions::LimitedDebugInfo) {
1736      DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
1737    }
1738  }
1739
1740  if (D.hasAttr<AnnotateAttr>())
1741      EmitVarAnnotations(&D, DeclPtr);
1742}
1743