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