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