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