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