CGDecl.cpp revision 19bd77c64937d6818c61f2aa9e6a9d51c65d0356
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 "clang/AST/ASTContext.h"
18#include "clang/AST/CharUnits.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Frontend/CodeGenOptions.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Type.h"
28using namespace clang;
29using namespace CodeGen;
30
31
32void CodeGenFunction::EmitDecl(const Decl &D) {
33  switch (D.getKind()) {
34  case Decl::TranslationUnit:
35  case Decl::Namespace:
36  case Decl::UnresolvedUsingTypename:
37  case Decl::ClassTemplateSpecialization:
38  case Decl::ClassTemplatePartialSpecialization:
39  case Decl::TemplateTypeParm:
40  case Decl::UnresolvedUsingValue:
41  case Decl::NonTypeTemplateParm:
42  case Decl::CXXMethod:
43  case Decl::CXXConstructor:
44  case Decl::CXXDestructor:
45  case Decl::CXXConversion:
46  case Decl::Field:
47  case Decl::IndirectField:
48  case Decl::ObjCIvar:
49  case Decl::ObjCAtDefsField:
50  case Decl::ParmVar:
51  case Decl::ImplicitParam:
52  case Decl::ClassTemplate:
53  case Decl::FunctionTemplate:
54  case Decl::TypeAliasTemplate:
55  case Decl::TemplateTemplateParm:
56  case Decl::ObjCMethod:
57  case Decl::ObjCCategory:
58  case Decl::ObjCProtocol:
59  case Decl::ObjCInterface:
60  case Decl::ObjCCategoryImpl:
61  case Decl::ObjCImplementation:
62  case Decl::ObjCProperty:
63  case Decl::ObjCCompatibleAlias:
64  case Decl::AccessSpec:
65  case Decl::LinkageSpec:
66  case Decl::ObjCPropertyImpl:
67  case Decl::ObjCClass:
68  case Decl::ObjCForwardProtocol:
69  case Decl::FileScopeAsm:
70  case Decl::Friend:
71  case Decl::FriendTemplate:
72  case Decl::Block:
73    assert(0 && "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    // None of these decls require codegen support.
86    return;
87
88  case Decl::Var: {
89    const VarDecl &VD = cast<VarDecl>(D);
90    assert(VD.isLocalVarDecl() &&
91           "Should not see file-scope variables inside a function!");
92    return EmitVarDecl(VD);
93  }
94
95  case Decl::Typedef:      // typedef int X;
96  case Decl::TypeAlias: {  // using X = int; [C++0x]
97    const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
98    QualType Ty = TD.getUnderlyingType();
99
100    if (Ty->isVariablyModifiedType())
101      EmitVLASize(Ty);
102  }
103  }
104}
105
106/// EmitVarDecl - This method handles emission of any variable declaration
107/// inside a function, including static vars etc.
108void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
109  switch (D.getStorageClass()) {
110  case SC_None:
111  case SC_Auto:
112  case SC_Register:
113    return EmitAutoVarDecl(D);
114  case SC_Static: {
115    llvm::GlobalValue::LinkageTypes Linkage =
116      llvm::GlobalValue::InternalLinkage;
117
118    // If the function definition has some sort of weak linkage, its
119    // static variables should also be weak so that they get properly
120    // uniqued.  We can't do this in C, though, because there's no
121    // standard way to agree on which variables are the same (i.e.
122    // there's no mangling).
123    if (getContext().getLangOptions().CPlusPlus)
124      if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
125        Linkage = CurFn->getLinkage();
126
127    return EmitStaticVarDecl(D, Linkage);
128  }
129  case SC_Extern:
130  case SC_PrivateExtern:
131    // Don't emit it now, allow it to be emitted lazily on its first use.
132    return;
133  }
134
135  assert(0 && "Unknown storage class");
136}
137
138static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
139                                     const char *Separator) {
140  CodeGenModule &CGM = CGF.CGM;
141  if (CGF.getContext().getLangOptions().CPlusPlus) {
142    llvm::StringRef Name = CGM.getMangledName(&D);
143    return Name.str();
144  }
145
146  std::string ContextName;
147  if (!CGF.CurFuncDecl) {
148    // Better be in a block declared in global scope.
149    const NamedDecl *ND = cast<NamedDecl>(&D);
150    const DeclContext *DC = ND->getDeclContext();
151    if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
152      MangleBuffer Name;
153      CGM.getBlockMangledName(GlobalDecl(), Name, BD);
154      ContextName = Name.getString();
155    }
156    else
157      assert(0 && "Unknown context for block static var decl");
158  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
159    llvm::StringRef Name = CGM.getMangledName(FD);
160    ContextName = Name.str();
161  } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
162    ContextName = CGF.CurFn->getName();
163  else
164    assert(0 && "Unknown context for static var decl");
165
166  return ContextName + Separator + D.getNameAsString();
167}
168
169llvm::GlobalVariable *
170CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
171                                     const char *Separator,
172                                     llvm::GlobalValue::LinkageTypes Linkage) {
173  QualType Ty = D.getType();
174  assert(Ty->isConstantSizeType() && "VLAs can't be static");
175
176  std::string Name = GetStaticDeclName(*this, D, Separator);
177
178  const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
179  llvm::GlobalVariable *GV =
180    new llvm::GlobalVariable(CGM.getModule(), LTy,
181                             Ty.isConstant(getContext()), Linkage,
182                             CGM.EmitNullConstant(D.getType()), Name, 0,
183                             D.isThreadSpecified(),
184                             CGM.getContext().getTargetAddressSpace(Ty));
185  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
186  if (Linkage != llvm::GlobalValue::InternalLinkage)
187    GV->setVisibility(CurFn->getVisibility());
188  return GV;
189}
190
191/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
192/// global variable that has already been created for it.  If the initializer
193/// has a different type than GV does, this may free GV and return a different
194/// one.  Otherwise it just returns GV.
195llvm::GlobalVariable *
196CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
197                                               llvm::GlobalVariable *GV) {
198  llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
199
200  // If constant emission failed, then this should be a C++ static
201  // initializer.
202  if (!Init) {
203    if (!getContext().getLangOptions().CPlusPlus)
204      CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
205    else if (Builder.GetInsertBlock()) {
206      // Since we have a static initializer, this global variable can't
207      // be constant.
208      GV->setConstant(false);
209
210      EmitCXXGuardedInit(D, GV);
211    }
212    return GV;
213  }
214
215  // The initializer may differ in type from the global. Rewrite
216  // the global to match the initializer.  (We have to do this
217  // because some types, like unions, can't be completely represented
218  // in the LLVM type system.)
219  if (GV->getType()->getElementType() != Init->getType()) {
220    llvm::GlobalVariable *OldGV = GV;
221
222    GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
223                                  OldGV->isConstant(),
224                                  OldGV->getLinkage(), Init, "",
225                                  /*InsertBefore*/ OldGV,
226                                  D.isThreadSpecified(),
227                           CGM.getContext().getTargetAddressSpace(D.getType()));
228    GV->setVisibility(OldGV->getVisibility());
229
230    // Steal the name of the old global
231    GV->takeName(OldGV);
232
233    // Replace all uses of the old global with the new global
234    llvm::Constant *NewPtrForOldDecl =
235    llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
236    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
237
238    // Erase the old global, since it is no longer used.
239    OldGV->eraseFromParent();
240  }
241
242  GV->setInitializer(Init);
243  return GV;
244}
245
246void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
247                                      llvm::GlobalValue::LinkageTypes Linkage) {
248  llvm::Value *&DMEntry = LocalDeclMap[&D];
249  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
250
251  llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage);
252
253  // Store into LocalDeclMap before generating initializer to handle
254  // circular references.
255  DMEntry = GV;
256
257  // We can't have a VLA here, but we can have a pointer to a VLA,
258  // even though that doesn't really make any sense.
259  // Make sure to evaluate VLA bounds now so that we have them for later.
260  if (D.getType()->isVariablyModifiedType())
261    EmitVLASize(D.getType());
262
263  // Local static block variables must be treated as globals as they may be
264  // referenced in their RHS initializer block-literal expresion.
265  CGM.setStaticLocalDeclAddress(&D, GV);
266
267  // If this value has an initializer, emit it.
268  if (D.getInit())
269    GV = AddInitializerToStaticVarDecl(D, GV);
270
271  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
272
273  // FIXME: Merge attribute handling.
274  if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
275    SourceManager &SM = CGM.getContext().getSourceManager();
276    llvm::Constant *Ann =
277      CGM.EmitAnnotateAttr(GV, AA,
278                           SM.getInstantiationLineNumber(D.getLocation()));
279    CGM.AddAnnotation(Ann);
280  }
281
282  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
283    GV->setSection(SA->getName());
284
285  if (D.hasAttr<UsedAttr>())
286    CGM.AddUsedGlobal(GV);
287
288  // We may have to cast the constant because of the initializer
289  // mismatch above.
290  //
291  // FIXME: It is really dangerous to store this in the map; if anyone
292  // RAUW's the GV uses of this constant will be invalid.
293  const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
294  const llvm::Type *LPtrTy =
295    LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType()));
296  DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
297
298  // Emit global variable debug descriptor for static vars.
299  CGDebugInfo *DI = getDebugInfo();
300  if (DI) {
301    DI->setLocation(D.getLocation());
302    DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
303  }
304}
305
306namespace {
307  struct CallArrayDtor : EHScopeStack::Cleanup {
308    CallArrayDtor(const CXXDestructorDecl *Dtor,
309                  const ConstantArrayType *Type,
310                  llvm::Value *Loc)
311      : Dtor(Dtor), Type(Type), Loc(Loc) {}
312
313    const CXXDestructorDecl *Dtor;
314    const ConstantArrayType *Type;
315    llvm::Value *Loc;
316
317    void Emit(CodeGenFunction &CGF, bool IsForEH) {
318      QualType BaseElementTy = CGF.getContext().getBaseElementType(Type);
319      const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy);
320      BasePtr = llvm::PointerType::getUnqual(BasePtr);
321      llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr);
322      CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr);
323    }
324  };
325
326  struct CallVarDtor : EHScopeStack::Cleanup {
327    CallVarDtor(const CXXDestructorDecl *Dtor,
328                llvm::Value *NRVOFlag,
329                llvm::Value *Loc)
330      : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {}
331
332    const CXXDestructorDecl *Dtor;
333    llvm::Value *NRVOFlag;
334    llvm::Value *Loc;
335
336    void Emit(CodeGenFunction &CGF, bool IsForEH) {
337      // Along the exceptions path we always execute the dtor.
338      bool NRVO = !IsForEH && NRVOFlag;
339
340      llvm::BasicBlock *SkipDtorBB = 0;
341      if (NRVO) {
342        // If we exited via NRVO, we skip the destructor call.
343        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
344        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
345        llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
346        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
347        CGF.EmitBlock(RunDtorBB);
348      }
349
350      CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
351                                /*ForVirtualBase=*/false, Loc);
352
353      if (NRVO) CGF.EmitBlock(SkipDtorBB);
354    }
355  };
356
357  struct CallStackRestore : EHScopeStack::Cleanup {
358    llvm::Value *Stack;
359    CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
360    void Emit(CodeGenFunction &CGF, bool IsForEH) {
361      llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp");
362      llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
363      CGF.Builder.CreateCall(F, V);
364    }
365  };
366
367  struct CallCleanupFunction : EHScopeStack::Cleanup {
368    llvm::Constant *CleanupFn;
369    const CGFunctionInfo &FnInfo;
370    const VarDecl &Var;
371
372    CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
373                        const VarDecl *Var)
374      : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
375
376    void Emit(CodeGenFunction &CGF, bool IsForEH) {
377      DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
378                      SourceLocation());
379      // Compute the address of the local variable, in case it's a byref
380      // or something.
381      llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
382
383      // In some cases, the type of the function argument will be different from
384      // the type of the pointer. An example of this is
385      // void f(void* arg);
386      // __attribute__((cleanup(f))) void *g;
387      //
388      // To fix this we insert a bitcast here.
389      QualType ArgTy = FnInfo.arg_begin()->type;
390      llvm::Value *Arg =
391        CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
392
393      CallArgList Args;
394      Args.add(RValue::get(Arg),
395               CGF.getContext().getPointerType(Var.getType()));
396      CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
397    }
398  };
399}
400
401/// EmitAutoVarWithLifetime - Does the setup required for an automatic
402/// variable with lifetime.
403static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
404                                    llvm::Value *addr,
405                                    Qualifiers::ObjCLifetime lifetime) {
406  switch (lifetime) {
407  case Qualifiers::OCL_None:
408    llvm_unreachable("present but none");
409
410  case Qualifiers::OCL_ExplicitNone:
411    // nothing to do
412    break;
413
414  case Qualifiers::OCL_Strong: {
415    CGF.PushARCReleaseCleanup(CGF.getARCCleanupKind(),
416                              var.getType(), addr,
417                              var.hasAttr<ObjCPreciseLifetimeAttr>());
418    break;
419  }
420  case Qualifiers::OCL_Autoreleasing:
421    // nothing to do
422    break;
423
424  case Qualifiers::OCL_Weak:
425    // __weak objects always get EH cleanups; otherwise, exceptions
426    // could cause really nasty crashes instead of mere leaks.
427    CGF.PushARCWeakReleaseCleanup(NormalAndEHCleanup, var.getType(), addr);
428    break;
429  }
430}
431
432static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
433  if (const Expr *e = dyn_cast<Expr>(s)) {
434    // Skip the most common kinds of expressions that make
435    // hierarchy-walking expensive.
436    s = e = e->IgnoreParenCasts();
437
438    if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
439      return (ref->getDecl() == &var);
440  }
441
442  for (Stmt::const_child_range children = s->children(); children; ++children)
443    if (isAccessedBy(var, *children))
444      return true;
445
446  return false;
447}
448
449static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
450  if (!decl) return false;
451  if (!isa<VarDecl>(decl)) return false;
452  const VarDecl *var = cast<VarDecl>(decl);
453  return isAccessedBy(*var, e);
454}
455
456static void drillIntoBlockVariable(CodeGenFunction &CGF,
457                                   LValue &lvalue,
458                                   const VarDecl *var) {
459  lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
460}
461
462void CodeGenFunction::EmitScalarInit(const Expr *init,
463                                     const ValueDecl *D,
464                                     LValue lvalue,
465                                     bool capturedByInit) {
466  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
467  if (!lifetime) {
468    llvm::Value *value = EmitScalarExpr(init);
469    if (capturedByInit)
470      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
471    EmitStoreThroughLValue(RValue::get(value), lvalue, lvalue.getType());
472    return;
473  }
474
475  // If we're emitting a value with lifetime, we have to do the
476  // initialization *before* we leave the cleanup scopes.
477  CodeGenFunction::RunCleanupsScope Scope(*this);
478  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init))
479    init = ewc->getSubExpr();
480
481  // We have to maintain the illusion that the variable is
482  // zero-initialized.  If the variable might be accessed in its
483  // initializer, zero-initialize before running the initializer, then
484  // actually perform the initialization with an assign.
485  bool accessedByInit = false;
486  if (lifetime != Qualifiers::OCL_ExplicitNone)
487    accessedByInit = isAccessedBy(D, init);
488  if (accessedByInit) {
489    LValue tempLV = lvalue;
490    // Drill down to the __block object if necessary.
491    if (capturedByInit) {
492      // We can use a simple GEP for this because it can't have been
493      // moved yet.
494      tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
495                                   getByRefValueLLVMField(cast<VarDecl>(D))));
496    }
497
498    const llvm::PointerType *ty
499      = cast<llvm::PointerType>(tempLV.getAddress()->getType());
500    ty = cast<llvm::PointerType>(ty->getElementType());
501
502    llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
503
504    // If __weak, we want to use a barrier under certain conditions.
505    if (lifetime == Qualifiers::OCL_Weak)
506      EmitARCInitWeak(tempLV.getAddress(), zero);
507
508    // Otherwise just do a simple store.
509    else
510      EmitStoreOfScalar(zero, tempLV);
511  }
512
513  // Emit the initializer.
514  llvm::Value *value = 0;
515
516  switch (lifetime) {
517  case Qualifiers::OCL_None:
518    llvm_unreachable("present but none");
519
520  case Qualifiers::OCL_ExplicitNone:
521    // nothing to do
522    value = EmitScalarExpr(init);
523    break;
524
525  case Qualifiers::OCL_Strong: {
526    value = EmitARCRetainScalarExpr(init);
527    break;
528  }
529
530  case Qualifiers::OCL_Weak: {
531    // No way to optimize a producing initializer into this.  It's not
532    // worth optimizing for, because the value will immediately
533    // disappear in the common case.
534    value = EmitScalarExpr(init);
535
536    if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
537    if (accessedByInit)
538      EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
539    else
540      EmitARCInitWeak(lvalue.getAddress(), value);
541    return;
542  }
543
544  case Qualifiers::OCL_Autoreleasing:
545    value = EmitARCRetainAutoreleaseScalarExpr(init);
546    break;
547  }
548
549  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
550
551  // If the variable might have been accessed by its initializer, we
552  // might have to initialize with a barrier.  We have to do this for
553  // both __weak and __strong, but __weak got filtered out above.
554  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
555    llvm::Value *oldValue = EmitLoadOfScalar(lvalue);
556    EmitStoreOfScalar(value, lvalue);
557    EmitARCRelease(oldValue, /*precise*/ false);
558    return;
559  }
560
561  EmitStoreOfScalar(value, lvalue);
562}
563
564/// EmitScalarInit - Initialize the given lvalue with the given object.
565void CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
566  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
567  if (!lifetime)
568    return EmitStoreThroughLValue(RValue::get(init), lvalue, lvalue.getType());
569
570  switch (lifetime) {
571  case Qualifiers::OCL_None:
572    llvm_unreachable("present but none");
573
574  case Qualifiers::OCL_ExplicitNone:
575    // nothing to do
576    break;
577
578  case Qualifiers::OCL_Strong:
579    init = EmitARCRetain(lvalue.getType(), init);
580    break;
581
582  case Qualifiers::OCL_Weak:
583    // Initialize and then skip the primitive store.
584    EmitARCInitWeak(lvalue.getAddress(), init);
585    return;
586
587  case Qualifiers::OCL_Autoreleasing:
588    init = EmitARCRetainAutorelease(lvalue.getType(), init);
589    break;
590  }
591
592  EmitStoreOfScalar(init, lvalue);
593}
594
595/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
596/// non-zero parts of the specified initializer with equal or fewer than
597/// NumStores scalar stores.
598static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
599                                                unsigned &NumStores) {
600  // Zero and Undef never requires any extra stores.
601  if (isa<llvm::ConstantAggregateZero>(Init) ||
602      isa<llvm::ConstantPointerNull>(Init) ||
603      isa<llvm::UndefValue>(Init))
604    return true;
605  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
606      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
607      isa<llvm::ConstantExpr>(Init))
608    return Init->isNullValue() || NumStores--;
609
610  // See if we can emit each element.
611  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
612    for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
613      llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
614      if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
615        return false;
616    }
617    return true;
618  }
619
620  // Anything else is hard and scary.
621  return false;
622}
623
624/// emitStoresForInitAfterMemset - For inits that
625/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
626/// stores that would be required.
627static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
628                                         bool isVolatile, CGBuilderTy &Builder) {
629  // Zero doesn't require any stores.
630  if (isa<llvm::ConstantAggregateZero>(Init) ||
631      isa<llvm::ConstantPointerNull>(Init) ||
632      isa<llvm::UndefValue>(Init))
633    return;
634
635  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
636      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
637      isa<llvm::ConstantExpr>(Init)) {
638    if (!Init->isNullValue())
639      Builder.CreateStore(Init, Loc, isVolatile);
640    return;
641  }
642
643  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
644         "Unknown value type!");
645
646  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
647    llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
648    if (Elt->isNullValue()) continue;
649
650    // Otherwise, get a pointer to the element and emit it.
651    emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
652                                 isVolatile, Builder);
653  }
654}
655
656
657/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
658/// plus some stores to initialize a local variable instead of using a memcpy
659/// from a constant global.  It is beneficial to use memset if the global is all
660/// zeros, or mostly zeros and large.
661static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
662                                                  uint64_t GlobalSize) {
663  // If a global is all zeros, always use a memset.
664  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
665
666
667  // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
668  // do it if it will require 6 or fewer scalar stores.
669  // TODO: Should budget depends on the size?  Avoiding a large global warrants
670  // plopping in more stores.
671  unsigned StoreBudget = 6;
672  uint64_t SizeLimit = 32;
673
674  return GlobalSize > SizeLimit &&
675         canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
676}
677
678
679/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
680/// variable declaration with auto, register, or no storage class specifier.
681/// These turn into simple stack objects, or GlobalValues depending on target.
682void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
683  AutoVarEmission emission = EmitAutoVarAlloca(D);
684  EmitAutoVarInit(emission);
685  EmitAutoVarCleanups(emission);
686}
687
688/// EmitAutoVarAlloca - Emit the alloca and debug information for a
689/// local variable.  Does not emit initalization or destruction.
690CodeGenFunction::AutoVarEmission
691CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
692  QualType Ty = D.getType();
693
694  AutoVarEmission emission(D);
695
696  bool isByRef = D.hasAttr<BlocksAttr>();
697  emission.IsByRef = isByRef;
698
699  CharUnits alignment = getContext().getDeclAlign(&D);
700  emission.Alignment = alignment;
701
702  llvm::Value *DeclPtr;
703  if (Ty->isConstantSizeType()) {
704    if (!Target.useGlobalsForAutomaticVariables()) {
705      bool NRVO = getContext().getLangOptions().ElideConstructors &&
706                  D.isNRVOVariable();
707
708      // If this value is a POD array or struct with a statically
709      // determinable constant initializer, there are optimizations we
710      // can do.
711      // TODO: we can potentially constant-evaluate non-POD structs and
712      // arrays as long as the initialization is trivial (e.g. if they
713      // have a non-trivial destructor, but not a non-trivial constructor).
714      if (D.getInit() &&
715          (Ty->isArrayType() || Ty->isRecordType()) &&
716          (Ty.isPODType(getContext()) ||
717           getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
718          D.getInit()->isConstantInitializer(getContext(), false)) {
719
720        // If the variable's a const type, and it's neither an NRVO
721        // candidate nor a __block variable, emit it as a global instead.
722        if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() &&
723            !NRVO && !isByRef) {
724          EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
725
726          emission.Address = 0; // signal this condition to later callbacks
727          assert(emission.wasEmittedAsGlobal());
728          return emission;
729        }
730
731        // Otherwise, tell the initialization code that we're in this case.
732        emission.IsConstantAggregate = true;
733      }
734
735      // A normal fixed sized variable becomes an alloca in the entry block,
736      // unless it's an NRVO variable.
737      const llvm::Type *LTy = ConvertTypeForMem(Ty);
738
739      if (NRVO) {
740        // The named return value optimization: allocate this variable in the
741        // return slot, so that we can elide the copy when returning this
742        // variable (C++0x [class.copy]p34).
743        DeclPtr = ReturnValue;
744
745        if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
746          if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
747            // Create a flag that is used to indicate when the NRVO was applied
748            // to this variable. Set it to zero to indicate that NRVO was not
749            // applied.
750            llvm::Value *Zero = Builder.getFalse();
751            llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
752            EnsureInsertPoint();
753            Builder.CreateStore(Zero, NRVOFlag);
754
755            // Record the NRVO flag for this variable.
756            NRVOFlags[&D] = NRVOFlag;
757            emission.NRVOFlag = NRVOFlag;
758          }
759        }
760      } else {
761        if (isByRef)
762          LTy = BuildByRefType(&D);
763
764        llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
765        Alloc->setName(D.getNameAsString());
766
767        CharUnits allocaAlignment = alignment;
768        if (isByRef)
769          allocaAlignment = std::max(allocaAlignment,
770              getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
771        Alloc->setAlignment(allocaAlignment.getQuantity());
772        DeclPtr = Alloc;
773      }
774    } else {
775      // Targets that don't support recursion emit locals as globals.
776      const char *Class =
777        D.getStorageClass() == SC_Register ? ".reg." : ".auto.";
778      DeclPtr = CreateStaticVarDecl(D, Class,
779                                    llvm::GlobalValue::InternalLinkage);
780    }
781
782    // FIXME: Can this happen?
783    if (Ty->isVariablyModifiedType())
784      EmitVLASize(Ty);
785  } else {
786    EnsureInsertPoint();
787
788    if (!DidCallStackSave) {
789      // Save the stack.
790      llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
791
792      llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
793      llvm::Value *V = Builder.CreateCall(F);
794
795      Builder.CreateStore(V, Stack);
796
797      DidCallStackSave = true;
798
799      // Push a cleanup block and restore the stack there.
800      // FIXME: in general circumstances, this should be an EH cleanup.
801      EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
802    }
803
804    // Get the element type.
805    const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
806    const llvm::Type *LElemPtrTy =
807      LElemTy->getPointerTo(CGM.getContext().getTargetAddressSpace(Ty));
808
809    llvm::Value *VLASize = EmitVLASize(Ty);
810
811    // Allocate memory for the array.
812    llvm::AllocaInst *VLA =
813      Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla");
814    VLA->setAlignment(alignment.getQuantity());
815
816    DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
817  }
818
819  llvm::Value *&DMEntry = LocalDeclMap[&D];
820  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
821  DMEntry = DeclPtr;
822  emission.Address = DeclPtr;
823
824  // Emit debug info for local var declaration.
825  if (HaveInsertPoint())
826    if (CGDebugInfo *DI = getDebugInfo()) {
827      DI->setLocation(D.getLocation());
828      if (Target.useGlobalsForAutomaticVariables()) {
829        DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
830      } else
831        DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
832    }
833
834  return emission;
835}
836
837/// Determines whether the given __block variable is potentially
838/// captured by the given expression.
839static bool isCapturedBy(const VarDecl &var, const Expr *e) {
840  // Skip the most common kinds of expressions that make
841  // hierarchy-walking expensive.
842  e = e->IgnoreParenCasts();
843
844  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
845    const BlockDecl *block = be->getBlockDecl();
846    for (BlockDecl::capture_const_iterator i = block->capture_begin(),
847           e = block->capture_end(); i != e; ++i) {
848      if (i->getVariable() == &var)
849        return true;
850    }
851
852    // No need to walk into the subexpressions.
853    return false;
854  }
855
856  for (Stmt::const_child_range children = e->children(); children; ++children)
857    if (isCapturedBy(var, cast<Expr>(*children)))
858      return true;
859
860  return false;
861}
862
863void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
864  assert(emission.Variable && "emission was not valid!");
865
866  // If this was emitted as a global constant, we're done.
867  if (emission.wasEmittedAsGlobal()) return;
868
869  const VarDecl &D = *emission.Variable;
870  QualType type = D.getType();
871
872  // If this local has an initializer, emit it now.
873  const Expr *Init = D.getInit();
874
875  // If we are at an unreachable point, we don't need to emit the initializer
876  // unless it contains a label.
877  if (!HaveInsertPoint()) {
878    if (!Init || !ContainsLabel(Init)) return;
879    EnsureInsertPoint();
880  }
881
882  // Initialize the structure of a __block variable.
883  if (emission.IsByRef)
884    emitByrefStructureInit(emission);
885
886  if (!Init) return;
887
888  CharUnits alignment = emission.Alignment;
889
890  // Check whether this is a byref variable that's potentially
891  // captured and moved by its own initializer.  If so, we'll need to
892  // emit the initializer first, then copy into the variable.
893  bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
894
895  llvm::Value *Loc =
896    capturedByInit ? emission.Address : emission.getObjectAddress(*this);
897
898  if (!emission.IsConstantAggregate) {
899    LValue lv = MakeAddrLValue(Loc, type, alignment.getQuantity());
900    lv.setNonGC(true);
901    return EmitExprAsInit(Init, &D, lv, capturedByInit);
902  }
903
904  // If this is a simple aggregate initialization, we can optimize it
905  // in various ways.
906  assert(!capturedByInit && "constant init contains a capturing block?");
907
908  bool isVolatile = type.isVolatileQualified();
909
910  llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this);
911  assert(constant != 0 && "Wasn't a simple constant init?");
912
913  llvm::Value *SizeVal =
914    llvm::ConstantInt::get(IntPtrTy,
915                           getContext().getTypeSizeInChars(type).getQuantity());
916
917  const llvm::Type *BP = Int8PtrTy;
918  if (Loc->getType() != BP)
919    Loc = Builder.CreateBitCast(Loc, BP, "tmp");
920
921  // If the initializer is all or mostly zeros, codegen with memset then do
922  // a few stores afterward.
923  if (shouldUseMemSetPlusStoresToInitialize(constant,
924                CGM.getTargetData().getTypeAllocSize(constant->getType()))) {
925    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
926                         alignment.getQuantity(), isVolatile);
927    if (!constant->isNullValue()) {
928      Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
929      emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
930    }
931  } else {
932    // Otherwise, create a temporary global with the initializer then
933    // memcpy from the global to the alloca.
934    std::string Name = GetStaticDeclName(*this, D, ".");
935    llvm::GlobalVariable *GV =
936      new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
937                               llvm::GlobalValue::InternalLinkage,
938                               constant, Name, 0, false, 0);
939    GV->setAlignment(alignment.getQuantity());
940    GV->setUnnamedAddr(true);
941
942    llvm::Value *SrcPtr = GV;
943    if (SrcPtr->getType() != BP)
944      SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
945
946    Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
947                         isVolatile);
948  }
949}
950
951/// Emit an expression as an initializer for a variable at the given
952/// location.  The expression is not necessarily the normal
953/// initializer for the variable, and the address is not necessarily
954/// its normal location.
955///
956/// \param init the initializing expression
957/// \param var the variable to act as if we're initializing
958/// \param loc the address to initialize; its type is a pointer
959///   to the LLVM mapping of the variable's type
960/// \param alignment the alignment of the address
961/// \param capturedByInit true if the variable is a __block variable
962///   whose address is potentially changed by the initializer
963void CodeGenFunction::EmitExprAsInit(const Expr *init,
964                                     const ValueDecl *D,
965                                     LValue lvalue,
966                                     bool capturedByInit) {
967  QualType type = D->getType();
968
969  if (type->isReferenceType()) {
970    RValue rvalue = EmitReferenceBindingToExpr(init, D);
971    if (capturedByInit)
972      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
973    EmitStoreThroughLValue(rvalue, lvalue, type);
974  } else if (!hasAggregateLLVMType(type)) {
975    EmitScalarInit(init, D, lvalue, capturedByInit);
976  } else if (type->isAnyComplexType()) {
977    ComplexPairTy complex = EmitComplexExpr(init);
978    if (capturedByInit)
979      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
980    StoreComplexToAddr(complex, lvalue.getAddress(), lvalue.isVolatile());
981  } else {
982    // TODO: how can we delay here if D is captured by its initializer?
983    EmitAggExpr(init, AggValueSlot::forLValue(lvalue, true, false));
984  }
985}
986
987void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
988  assert(emission.Variable && "emission was not valid!");
989
990  // If this was emitted as a global constant, we're done.
991  if (emission.wasEmittedAsGlobal()) return;
992
993  const VarDecl &D = *emission.Variable;
994
995  // Handle C++ or ARC destruction of variables.
996  if (getLangOptions().CPlusPlus) {
997    QualType type = D.getType();
998    QualType baseType = getContext().getBaseElementType(type);
999    if (const RecordType *RT = baseType->getAs<RecordType>()) {
1000      CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1001      if (!ClassDecl->hasTrivialDestructor()) {
1002        // Note: We suppress the destructor call when the corresponding NRVO
1003        // flag has been set.
1004
1005        // Note that for __block variables, we want to destroy the
1006        // original stack object, not the possible forwarded object.
1007        llvm::Value *Loc = emission.getObjectAddress(*this);
1008
1009        const CXXDestructorDecl *D = ClassDecl->getDestructor();
1010        assert(D && "EmitLocalBlockVarDecl - destructor is nul");
1011
1012        if (type != baseType) {
1013          const ConstantArrayType *Array =
1014            getContext().getAsConstantArrayType(type);
1015          assert(Array && "types changed without array?");
1016          EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup,
1017                                             D, Array, Loc);
1018        } else {
1019          EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup,
1020                                           D, emission.NRVOFlag, Loc);
1021        }
1022      }
1023    }
1024  }
1025
1026  if (Qualifiers::ObjCLifetime lifetime
1027        = D.getType().getQualifiers().getObjCLifetime()) {
1028    if (!D.isARCPseudoStrong()) {
1029      llvm::Value *loc = emission.getObjectAddress(*this);
1030      EmitAutoVarWithLifetime(*this, D, loc, lifetime);
1031    }
1032  }
1033
1034  // Handle the cleanup attribute.
1035  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1036    const FunctionDecl *FD = CA->getFunctionDecl();
1037
1038    llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1039    assert(F && "Could not find function!");
1040
1041    const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
1042    EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1043  }
1044
1045  // If this is a block variable, call _Block_object_destroy
1046  // (on the unforwarded address).
1047  if (emission.IsByRef)
1048    enterByrefCleanup(emission);
1049}
1050
1051namespace {
1052  /// A cleanup to perform a release of an object at the end of a
1053  /// function.  This is used to balance out the incoming +1 of a
1054  /// ns_consumed argument when we can't reasonably do that just by
1055  /// not doing the initial retain for a __block argument.
1056  struct ConsumeARCParameter : EHScopeStack::Cleanup {
1057    ConsumeARCParameter(llvm::Value *param) : Param(param) {}
1058
1059    llvm::Value *Param;
1060
1061    void Emit(CodeGenFunction &CGF, bool IsForEH) {
1062      CGF.EmitARCRelease(Param, /*precise*/ false);
1063    }
1064  };
1065}
1066
1067/// Emit an alloca (or GlobalValue depending on target)
1068/// for the specified parameter and set up LocalDeclMap.
1069void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
1070                                   unsigned ArgNo) {
1071  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1072  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1073         "Invalid argument to EmitParmDecl");
1074
1075  Arg->setName(D.getName());
1076
1077  // Use better IR generation for certain implicit parameters.
1078  if (isa<ImplicitParamDecl>(D)) {
1079    // The only implicit argument a block has is its literal.
1080    if (BlockInfo) {
1081      LocalDeclMap[&D] = Arg;
1082
1083      if (CGDebugInfo *DI = getDebugInfo()) {
1084        DI->setLocation(D.getLocation());
1085        DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder);
1086      }
1087
1088      return;
1089    }
1090  }
1091
1092  QualType Ty = D.getType();
1093
1094  llvm::Value *DeclPtr;
1095  // If this is an aggregate or variable sized value, reuse the input pointer.
1096  if (!Ty->isConstantSizeType() ||
1097      CodeGenFunction::hasAggregateLLVMType(Ty)) {
1098    DeclPtr = Arg;
1099  } else {
1100    // Otherwise, create a temporary to hold the value.
1101    DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr");
1102
1103    bool doStore = true;
1104
1105    Qualifiers qs = Ty.getQualifiers();
1106
1107    if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1108      // We honor __attribute__((ns_consumed)) for types with lifetime.
1109      // For __strong, it's handled by just skipping the initial retain;
1110      // otherwise we have to balance out the initial +1 with an extra
1111      // cleanup to do the release at the end of the function.
1112      bool isConsumed = D.hasAttr<NSConsumedAttr>();
1113
1114      // 'self' is always formally __strong, but if this is not an
1115      // init method then we don't want to retain it.
1116      if (D.isARCPseudoStrong()) {
1117        const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1118        assert(&D == method->getSelfDecl());
1119        assert(lt == Qualifiers::OCL_Strong);
1120        assert(qs.hasConst());
1121        assert(method->getMethodFamily() != OMF_init);
1122        (void) method;
1123        lt = Qualifiers::OCL_ExplicitNone;
1124      }
1125
1126      if (lt == Qualifiers::OCL_Strong) {
1127        if (!isConsumed)
1128          // Don't use objc_retainBlock for block pointers, because we
1129          // don't want to Block_copy something just because we got it
1130          // as a parameter.
1131          Arg = EmitARCRetainNonBlock(Arg);
1132      } else {
1133        // Push the cleanup for a consumed parameter.
1134        if (isConsumed)
1135          EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg);
1136
1137        if (lt == Qualifiers::OCL_Weak) {
1138          EmitARCInitWeak(DeclPtr, Arg);
1139          doStore = false; // The weak init is a store, no need to do two
1140        }
1141      }
1142
1143      // Enter the cleanup scope.
1144      EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1145    }
1146
1147    // Store the initial value into the alloca.
1148    if (doStore)
1149      EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(),
1150                        getContext().getDeclAlign(&D).getQuantity(), Ty,
1151                        CGM.getTBAAInfo(Ty));
1152  }
1153
1154  llvm::Value *&DMEntry = LocalDeclMap[&D];
1155  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
1156  DMEntry = DeclPtr;
1157
1158  // Emit debug info for param declaration.
1159  if (CGDebugInfo *DI = getDebugInfo())
1160    DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
1161}
1162