1 //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit Decl nodes as LLVM code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGBlocks.h"
14 #include "CGCXXABI.h"
15 #include "CGCleanup.h"
16 #include "CGDebugInfo.h"
17 #include "CGOpenCLRuntime.h"
18 #include "CGOpenMPRuntime.h"
19 #include "CodeGenFunction.h"
20 #include "CodeGenModule.h"
21 #include "ConstantEmitter.h"
22 #include "PatternInit.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/CodeGenOptions.h"
30 #include "clang/Basic/SourceManager.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/CodeGen/CGFunctionInfo.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/IR/Type.h"
38 
39 using namespace clang;
40 using namespace CodeGen;
41 
42 void CodeGenFunction::EmitDecl(const Decl &D) {
43   switch (D.getKind()) {
44   case Decl::BuiltinTemplate:
45   case Decl::TranslationUnit:
46   case Decl::ExternCContext:
47   case Decl::Namespace:
48   case Decl::UnresolvedUsingTypename:
49   case Decl::ClassTemplateSpecialization:
50   case Decl::ClassTemplatePartialSpecialization:
51   case Decl::VarTemplateSpecialization:
52   case Decl::VarTemplatePartialSpecialization:
53   case Decl::TemplateTypeParm:
54   case Decl::UnresolvedUsingValue:
55   case Decl::NonTypeTemplateParm:
56   case Decl::CXXDeductionGuide:
57   case Decl::CXXMethod:
58   case Decl::CXXConstructor:
59   case Decl::CXXDestructor:
60   case Decl::CXXConversion:
61   case Decl::Field:
62   case Decl::MSProperty:
63   case Decl::IndirectField:
64   case Decl::ObjCIvar:
65   case Decl::ObjCAtDefsField:
66   case Decl::ParmVar:
67   case Decl::ImplicitParam:
68   case Decl::ClassTemplate:
69   case Decl::VarTemplate:
70   case Decl::FunctionTemplate:
71   case Decl::TypeAliasTemplate:
72   case Decl::TemplateTemplateParm:
73   case Decl::ObjCMethod:
74   case Decl::ObjCCategory:
75   case Decl::ObjCProtocol:
76   case Decl::ObjCInterface:
77   case Decl::ObjCCategoryImpl:
78   case Decl::ObjCImplementation:
79   case Decl::ObjCProperty:
80   case Decl::ObjCCompatibleAlias:
81   case Decl::PragmaComment:
82   case Decl::PragmaDetectMismatch:
83   case Decl::AccessSpec:
84   case Decl::LinkageSpec:
85   case Decl::Export:
86   case Decl::ObjCPropertyImpl:
87   case Decl::FileScopeAsm:
88   case Decl::Friend:
89   case Decl::FriendTemplate:
90   case Decl::Block:
91   case Decl::Captured:
92   case Decl::ClassScopeFunctionSpecialization:
93   case Decl::UsingShadow:
94   case Decl::ConstructorUsingShadow:
95   case Decl::ObjCTypeParam:
96   case Decl::Binding:
97     llvm_unreachable("Declaration should not be in declstmts!");
98   case Decl::Function:  // void X();
99   case Decl::Record:    // struct/union/class X;
100   case Decl::Enum:      // enum X;
101   case Decl::EnumConstant: // enum ? { X = ? }
102   case Decl::CXXRecord: // struct/union/class X; [C++]
103   case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
104   case Decl::Label:        // __label__ x;
105   case Decl::Import:
106   case Decl::OMPThreadPrivate:
107   case Decl::OMPAllocate:
108   case Decl::OMPCapturedExpr:
109   case Decl::OMPRequires:
110   case Decl::Empty:
111   case Decl::Concept:
112     // None of these decls require codegen support.
113     return;
114 
115   case Decl::NamespaceAlias:
116     if (CGDebugInfo *DI = getDebugInfo())
117         DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
118     return;
119   case Decl::Using:          // using X; [C++]
120     if (CGDebugInfo *DI = getDebugInfo())
121         DI->EmitUsingDecl(cast<UsingDecl>(D));
122     return;
123   case Decl::UsingPack:
124     for (auto *Using : cast<UsingPackDecl>(D).expansions())
125       EmitDecl(*Using);
126     return;
127   case Decl::UsingDirective: // using namespace X; [C++]
128     if (CGDebugInfo *DI = getDebugInfo())
129       DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
130     return;
131   case Decl::Var:
132   case Decl::Decomposition: {
133     const VarDecl &VD = cast<VarDecl>(D);
134     assert(VD.isLocalVarDecl() &&
135            "Should not see file-scope variables inside a function!");
136     EmitVarDecl(VD);
137     if (auto *DD = dyn_cast<DecompositionDecl>(&VD))
138       for (auto *B : DD->bindings())
139         if (auto *HD = B->getHoldingVar())
140           EmitVarDecl(*HD);
141     return;
142   }
143 
144   case Decl::OMPDeclareReduction:
145     return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this);
146 
147   case Decl::OMPDeclareMapper:
148     return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this);
149 
150   case Decl::Typedef:      // typedef int X;
151   case Decl::TypeAlias: {  // using X = int; [C++0x]
152     const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
153     QualType Ty = TD.getUnderlyingType();
154 
155     if (Ty->isVariablyModifiedType())
156       EmitVariablyModifiedType(Ty);
157 
158     return;
159   }
160   }
161 }
162 
163 /// EmitVarDecl - This method handles emission of any variable declaration
164 /// inside a function, including static vars etc.
165 void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
166   if (D.hasExternalStorage())
167     // Don't emit it now, allow it to be emitted lazily on its first use.
168     return;
169 
170   // Some function-scope variable does not have static storage but still
171   // needs to be emitted like a static variable, e.g. a function-scope
172   // variable in constant address space in OpenCL.
173   if (D.getStorageDuration() != SD_Automatic) {
174     // Static sampler variables translated to function calls.
175     if (D.getType()->isSamplerT())
176       return;
177 
178     llvm::GlobalValue::LinkageTypes Linkage =
179         CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false);
180 
181     // FIXME: We need to force the emission/use of a guard variable for
182     // some variables even if we can constant-evaluate them because
183     // we can't guarantee every translation unit will constant-evaluate them.
184 
185     return EmitStaticVarDecl(D, Linkage);
186   }
187 
188   if (D.getType().getAddressSpace() == LangAS::opencl_local)
189     return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D);
190 
191   assert(D.hasLocalStorage());
192   return EmitAutoVarDecl(D);
193 }
194 
195 static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) {
196   if (CGM.getLangOpts().CPlusPlus)
197     return CGM.getMangledName(&D).str();
198 
199   // If this isn't C++, we don't need a mangled name, just a pretty one.
200   assert(!D.isExternallyVisible() && "name shouldn't matter");
201   std::string ContextName;
202   const DeclContext *DC = D.getDeclContext();
203   if (auto *CD = dyn_cast<CapturedDecl>(DC))
204     DC = cast<DeclContext>(CD->getNonClosureContext());
205   if (const auto *FD = dyn_cast<FunctionDecl>(DC))
206     ContextName = CGM.getMangledName(FD);
207   else if (const auto *BD = dyn_cast<BlockDecl>(DC))
208     ContextName = CGM.getBlockMangledName(GlobalDecl(), BD);
209   else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
210     ContextName = OMD->getSelector().getAsString();
211   else
212     llvm_unreachable("Unknown context for static var decl");
213 
214   ContextName += "." + D.getNameAsString();
215   return ContextName;
216 }
217 
218 llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
219     const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
220   // In general, we don't always emit static var decls once before we reference
221   // them. It is possible to reference them before emitting the function that
222   // contains them, and it is possible to emit the containing function multiple
223   // times.
224   if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
225     return ExistingGV;
226 
227   QualType Ty = D.getType();
228   assert(Ty->isConstantSizeType() && "VLAs can't be static");
229 
230   // Use the label if the variable is renamed with the asm-label extension.
231   std::string Name;
232   if (D.hasAttr<AsmLabelAttr>())
233     Name = getMangledName(&D);
234   else
235     Name = getStaticDeclName(*this, D);
236 
237   llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty);
238   LangAS AS = GetGlobalVarAddressSpace(&D);
239   unsigned TargetAS = getContext().getTargetAddressSpace(AS);
240 
241   // OpenCL variables in local address space and CUDA shared
242   // variables cannot have an initializer.
243   llvm::Constant *Init = nullptr;
244   if (Ty.getAddressSpace() == LangAS::opencl_local ||
245       D.hasAttr<CUDASharedAttr>())
246     Init = llvm::UndefValue::get(LTy);
247   else
248     Init = EmitNullConstant(Ty);
249 
250   llvm::GlobalVariable *GV = new llvm::GlobalVariable(
251       getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name,
252       nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
253   GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
254 
255   if (supportsCOMDAT() && GV->isWeakForLinker())
256     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
257 
258   if (D.getTLSKind())
259     setTLSMode(GV, D);
260 
261   setGVProperties(GV, &D);
262 
263   // Make sure the result is of the correct type.
264   LangAS ExpectedAS = Ty.getAddressSpace();
265   llvm::Constant *Addr = GV;
266   if (AS != ExpectedAS) {
267     Addr = getTargetCodeGenInfo().performAddrSpaceCast(
268         *this, GV, AS, ExpectedAS,
269         LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS)));
270   }
271 
272   setStaticLocalDeclAddress(&D, Addr);
273 
274   // Ensure that the static local gets initialized by making sure the parent
275   // function gets emitted eventually.
276   const Decl *DC = cast<Decl>(D.getDeclContext());
277 
278   // We can't name blocks or captured statements directly, so try to emit their
279   // parents.
280   if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) {
281     DC = DC->getNonClosureContext();
282     // FIXME: Ensure that global blocks get emitted.
283     if (!DC)
284       return Addr;
285   }
286 
287   GlobalDecl GD;
288   if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
289     GD = GlobalDecl(CD, Ctor_Base);
290   else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
291     GD = GlobalDecl(DD, Dtor_Base);
292   else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
293     GD = GlobalDecl(FD);
294   else {
295     // Don't do anything for Obj-C method decls or global closures. We should
296     // never defer them.
297     assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
298   }
299   if (GD.getDecl()) {
300     // Disable emission of the parent function for the OpenMP device codegen.
301     CGOpenMPRuntime::DisableAutoDeclareTargetRAII NoDeclTarget(*this);
302     (void)GetAddrOfGlobal(GD);
303   }
304 
305   return Addr;
306 }
307 
308 /// hasNontrivialDestruction - Determine whether a type's destruction is
309 /// non-trivial. If so, and the variable uses static initialization, we must
310 /// register its destructor to run on exit.
311 static bool hasNontrivialDestruction(QualType T) {
312   CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
313   return RD && !RD->hasTrivialDestructor();
314 }
315 
316 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
317 /// global variable that has already been created for it.  If the initializer
318 /// has a different type than GV does, this may free GV and return a different
319 /// one.  Otherwise it just returns GV.
320 llvm::GlobalVariable *
321 CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
322                                                llvm::GlobalVariable *GV) {
323   ConstantEmitter emitter(*this);
324   llvm::Constant *Init = emitter.tryEmitForInitializer(D);
325 
326   // If constant emission failed, then this should be a C++ static
327   // initializer.
328   if (!Init) {
329     if (!getLangOpts().CPlusPlus)
330       CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
331     else if (HaveInsertPoint()) {
332       // Since we have a static initializer, this global variable can't
333       // be constant.
334       GV->setConstant(false);
335 
336       EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
337     }
338     return GV;
339   }
340 
341   // The initializer may differ in type from the global. Rewrite
342   // the global to match the initializer.  (We have to do this
343   // because some types, like unions, can't be completely represented
344   // in the LLVM type system.)
345   if (GV->getType()->getElementType() != Init->getType()) {
346     llvm::GlobalVariable *OldGV = GV;
347 
348     GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
349                                   OldGV->isConstant(),
350                                   OldGV->getLinkage(), Init, "",
351                                   /*InsertBefore*/ OldGV,
352                                   OldGV->getThreadLocalMode(),
353                            CGM.getContext().getTargetAddressSpace(D.getType()));
354     GV->setVisibility(OldGV->getVisibility());
355     GV->setDSOLocal(OldGV->isDSOLocal());
356     GV->setComdat(OldGV->getComdat());
357 
358     // Steal the name of the old global
359     GV->takeName(OldGV);
360 
361     // Replace all uses of the old global with the new global
362     llvm::Constant *NewPtrForOldDecl =
363     llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
364     OldGV->replaceAllUsesWith(NewPtrForOldDecl);
365 
366     // Erase the old global, since it is no longer used.
367     OldGV->eraseFromParent();
368   }
369 
370   GV->setConstant(CGM.isTypeConstant(D.getType(), true));
371   GV->setInitializer(Init);
372 
373   emitter.finalize(GV);
374 
375   if (hasNontrivialDestruction(D.getType()) && HaveInsertPoint()) {
376     // We have a constant initializer, but a nontrivial destructor. We still
377     // need to perform a guarded "initialization" in order to register the
378     // destructor.
379     EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
380   }
381 
382   return GV;
383 }
384 
385 void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
386                                       llvm::GlobalValue::LinkageTypes Linkage) {
387   // Check to see if we already have a global variable for this
388   // declaration.  This can happen when double-emitting function
389   // bodies, e.g. with complete and base constructors.
390   llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
391   CharUnits alignment = getContext().getDeclAlign(&D);
392 
393   // Store into LocalDeclMap before generating initializer to handle
394   // circular references.
395   setAddrOfLocalVar(&D, Address(addr, alignment));
396 
397   // We can't have a VLA here, but we can have a pointer to a VLA,
398   // even though that doesn't really make any sense.
399   // Make sure to evaluate VLA bounds now so that we have them for later.
400   if (D.getType()->isVariablyModifiedType())
401     EmitVariablyModifiedType(D.getType());
402 
403   // Save the type in case adding the initializer forces a type change.
404   llvm::Type *expectedType = addr->getType();
405 
406   llvm::GlobalVariable *var =
407     cast<llvm::GlobalVariable>(addr->stripPointerCasts());
408 
409   // CUDA's local and local static __shared__ variables should not
410   // have any non-empty initializers. This is ensured by Sema.
411   // Whatever initializer such variable may have when it gets here is
412   // a no-op and should not be emitted.
413   bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
414                          D.hasAttr<CUDASharedAttr>();
415   // If this value has an initializer, emit it.
416   if (D.getInit() && !isCudaSharedVar)
417     var = AddInitializerToStaticVarDecl(D, var);
418 
419   var->setAlignment(alignment.getQuantity());
420 
421   if (D.hasAttr<AnnotateAttr>())
422     CGM.AddGlobalAnnotations(&D, var);
423 
424   if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
425     var->addAttribute("bss-section", SA->getName());
426   if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
427     var->addAttribute("data-section", SA->getName());
428   if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
429     var->addAttribute("rodata-section", SA->getName());
430 
431   if (const SectionAttr *SA = D.getAttr<SectionAttr>())
432     var->setSection(SA->getName());
433 
434   if (D.hasAttr<UsedAttr>())
435     CGM.addUsedGlobal(var);
436 
437   // We may have to cast the constant because of the initializer
438   // mismatch above.
439   //
440   // FIXME: It is really dangerous to store this in the map; if anyone
441   // RAUW's the GV uses of this constant will be invalid.
442   llvm::Constant *castedAddr =
443     llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
444   if (var != castedAddr)
445     LocalDeclMap.find(&D)->second = Address(castedAddr, alignment);
446   CGM.setStaticLocalDeclAddress(&D, castedAddr);
447 
448   CGM.getSanitizerMetadata()->reportGlobalToASan(var, D);
449 
450   // Emit global variable debug descriptor for static vars.
451   CGDebugInfo *DI = getDebugInfo();
452   if (DI &&
453       CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) {
454     DI->setLocation(D.getLocation());
455     DI->EmitGlobalVariable(var, &D);
456   }
457 }
458 
459 namespace {
460   struct DestroyObject final : EHScopeStack::Cleanup {
461     DestroyObject(Address addr, QualType type,
462                   CodeGenFunction::Destroyer *destroyer,
463                   bool useEHCleanupForArray)
464       : addr(addr), type(type), destroyer(destroyer),
465         useEHCleanupForArray(useEHCleanupForArray) {}
466 
467     Address addr;
468     QualType type;
469     CodeGenFunction::Destroyer *destroyer;
470     bool useEHCleanupForArray;
471 
472     void Emit(CodeGenFunction &CGF, Flags flags) override {
473       // Don't use an EH cleanup recursively from an EH cleanup.
474       bool useEHCleanupForArray =
475         flags.isForNormalCleanup() && this->useEHCleanupForArray;
476 
477       CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
478     }
479   };
480 
481   template <class Derived>
482   struct DestroyNRVOVariable : EHScopeStack::Cleanup {
483     DestroyNRVOVariable(Address addr, llvm::Value *NRVOFlag)
484         : NRVOFlag(NRVOFlag), Loc(addr) {}
485 
486     llvm::Value *NRVOFlag;
487     Address Loc;
488 
489     void Emit(CodeGenFunction &CGF, Flags flags) override {
490       // Along the exceptions path we always execute the dtor.
491       bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
492 
493       llvm::BasicBlock *SkipDtorBB = nullptr;
494       if (NRVO) {
495         // If we exited via NRVO, we skip the destructor call.
496         llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
497         SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
498         llvm::Value *DidNRVO =
499           CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
500         CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
501         CGF.EmitBlock(RunDtorBB);
502       }
503 
504       static_cast<Derived *>(this)->emitDestructorCall(CGF);
505 
506       if (NRVO) CGF.EmitBlock(SkipDtorBB);
507     }
508 
509     virtual ~DestroyNRVOVariable() = default;
510   };
511 
512   struct DestroyNRVOVariableCXX final
513       : DestroyNRVOVariable<DestroyNRVOVariableCXX> {
514     DestroyNRVOVariableCXX(Address addr, const CXXDestructorDecl *Dtor,
515                            llvm::Value *NRVOFlag)
516       : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, NRVOFlag),
517         Dtor(Dtor) {}
518 
519     const CXXDestructorDecl *Dtor;
520 
521     void emitDestructorCall(CodeGenFunction &CGF) {
522       CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
523                                 /*ForVirtualBase=*/false,
524                                 /*Delegating=*/false, Loc);
525     }
526   };
527 
528   struct DestroyNRVOVariableC final
529       : DestroyNRVOVariable<DestroyNRVOVariableC> {
530     DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
531         : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, NRVOFlag), Ty(Ty) {}
532 
533     QualType Ty;
534 
535     void emitDestructorCall(CodeGenFunction &CGF) {
536       CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
537     }
538   };
539 
540   struct CallStackRestore final : EHScopeStack::Cleanup {
541     Address Stack;
542     CallStackRestore(Address Stack) : Stack(Stack) {}
543     void Emit(CodeGenFunction &CGF, Flags flags) override {
544       llvm::Value *V = CGF.Builder.CreateLoad(Stack);
545       llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
546       CGF.Builder.CreateCall(F, V);
547     }
548   };
549 
550   struct ExtendGCLifetime final : EHScopeStack::Cleanup {
551     const VarDecl &Var;
552     ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
553 
554     void Emit(CodeGenFunction &CGF, Flags flags) override {
555       // Compute the address of the local variable, in case it's a
556       // byref or something.
557       DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
558                       Var.getType(), VK_LValue, SourceLocation());
559       llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
560                                                 SourceLocation());
561       CGF.EmitExtendGCLifetime(value);
562     }
563   };
564 
565   struct CallCleanupFunction final : EHScopeStack::Cleanup {
566     llvm::Constant *CleanupFn;
567     const CGFunctionInfo &FnInfo;
568     const VarDecl &Var;
569 
570     CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
571                         const VarDecl *Var)
572       : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
573 
574     void Emit(CodeGenFunction &CGF, Flags flags) override {
575       DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
576                       Var.getType(), VK_LValue, SourceLocation());
577       // Compute the address of the local variable, in case it's a byref
578       // or something.
579       llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer();
580 
581       // In some cases, the type of the function argument will be different from
582       // the type of the pointer. An example of this is
583       // void f(void* arg);
584       // __attribute__((cleanup(f))) void *g;
585       //
586       // To fix this we insert a bitcast here.
587       QualType ArgTy = FnInfo.arg_begin()->type;
588       llvm::Value *Arg =
589         CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
590 
591       CallArgList Args;
592       Args.add(RValue::get(Arg),
593                CGF.getContext().getPointerType(Var.getType()));
594       auto Callee = CGCallee::forDirect(CleanupFn);
595       CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
596     }
597   };
598 } // end anonymous namespace
599 
600 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
601 /// variable with lifetime.
602 static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
603                                     Address addr,
604                                     Qualifiers::ObjCLifetime lifetime) {
605   switch (lifetime) {
606   case Qualifiers::OCL_None:
607     llvm_unreachable("present but none");
608 
609   case Qualifiers::OCL_ExplicitNone:
610     // nothing to do
611     break;
612 
613   case Qualifiers::OCL_Strong: {
614     CodeGenFunction::Destroyer *destroyer =
615       (var.hasAttr<ObjCPreciseLifetimeAttr>()
616        ? CodeGenFunction::destroyARCStrongPrecise
617        : CodeGenFunction::destroyARCStrongImprecise);
618 
619     CleanupKind cleanupKind = CGF.getARCCleanupKind();
620     CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
621                     cleanupKind & EHCleanup);
622     break;
623   }
624   case Qualifiers::OCL_Autoreleasing:
625     // nothing to do
626     break;
627 
628   case Qualifiers::OCL_Weak:
629     // __weak objects always get EH cleanups; otherwise, exceptions
630     // could cause really nasty crashes instead of mere leaks.
631     CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
632                     CodeGenFunction::destroyARCWeak,
633                     /*useEHCleanup*/ true);
634     break;
635   }
636 }
637 
638 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
639   if (const Expr *e = dyn_cast<Expr>(s)) {
640     // Skip the most common kinds of expressions that make
641     // hierarchy-walking expensive.
642     s = e = e->IgnoreParenCasts();
643 
644     if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
645       return (ref->getDecl() == &var);
646     if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
647       const BlockDecl *block = be->getBlockDecl();
648       for (const auto &I : block->captures()) {
649         if (I.getVariable() == &var)
650           return true;
651       }
652     }
653   }
654 
655   for (const Stmt *SubStmt : s->children())
656     // SubStmt might be null; as in missing decl or conditional of an if-stmt.
657     if (SubStmt && isAccessedBy(var, SubStmt))
658       return true;
659 
660   return false;
661 }
662 
663 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
664   if (!decl) return false;
665   if (!isa<VarDecl>(decl)) return false;
666   const VarDecl *var = cast<VarDecl>(decl);
667   return isAccessedBy(*var, e);
668 }
669 
670 static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF,
671                                    const LValue &destLV, const Expr *init) {
672   bool needsCast = false;
673 
674   while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
675     switch (castExpr->getCastKind()) {
676     // Look through casts that don't require representation changes.
677     case CK_NoOp:
678     case CK_BitCast:
679     case CK_BlockPointerToObjCPointerCast:
680       needsCast = true;
681       break;
682 
683     // If we find an l-value to r-value cast from a __weak variable,
684     // emit this operation as a copy or move.
685     case CK_LValueToRValue: {
686       const Expr *srcExpr = castExpr->getSubExpr();
687       if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
688         return false;
689 
690       // Emit the source l-value.
691       LValue srcLV = CGF.EmitLValue(srcExpr);
692 
693       // Handle a formal type change to avoid asserting.
694       auto srcAddr = srcLV.getAddress();
695       if (needsCast) {
696         srcAddr = CGF.Builder.CreateElementBitCast(srcAddr,
697                                          destLV.getAddress().getElementType());
698       }
699 
700       // If it was an l-value, use objc_copyWeak.
701       if (srcExpr->getValueKind() == VK_LValue) {
702         CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
703       } else {
704         assert(srcExpr->getValueKind() == VK_XValue);
705         CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr);
706       }
707       return true;
708     }
709 
710     // Stop at anything else.
711     default:
712       return false;
713     }
714 
715     init = castExpr->getSubExpr();
716   }
717   return false;
718 }
719 
720 static void drillIntoBlockVariable(CodeGenFunction &CGF,
721                                    LValue &lvalue,
722                                    const VarDecl *var) {
723   lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
724 }
725 
726 void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS,
727                                            SourceLocation Loc) {
728   if (!SanOpts.has(SanitizerKind::NullabilityAssign))
729     return;
730 
731   auto Nullability = LHS.getType()->getNullability(getContext());
732   if (!Nullability || *Nullability != NullabilityKind::NonNull)
733     return;
734 
735   // Check if the right hand side of the assignment is nonnull, if the left
736   // hand side must be nonnull.
737   SanitizerScope SanScope(this);
738   llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
739   llvm::Constant *StaticData[] = {
740       EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()),
741       llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
742       llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
743   EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
744             SanitizerHandler::TypeMismatch, StaticData, RHS);
745 }
746 
747 void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
748                                      LValue lvalue, bool capturedByInit) {
749   Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
750   if (!lifetime) {
751     llvm::Value *value = EmitScalarExpr(init);
752     if (capturedByInit)
753       drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
754     EmitNullabilityCheck(lvalue, value, init->getExprLoc());
755     EmitStoreThroughLValue(RValue::get(value), lvalue, true);
756     return;
757   }
758 
759   if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
760     init = DIE->getExpr();
761 
762   // If we're emitting a value with lifetime, we have to do the
763   // initialization *before* we leave the cleanup scopes.
764   if (const FullExpr *fe = dyn_cast<FullExpr>(init)) {
765     enterFullExpression(fe);
766     init = fe->getSubExpr();
767   }
768   CodeGenFunction::RunCleanupsScope Scope(*this);
769 
770   // We have to maintain the illusion that the variable is
771   // zero-initialized.  If the variable might be accessed in its
772   // initializer, zero-initialize before running the initializer, then
773   // actually perform the initialization with an assign.
774   bool accessedByInit = false;
775   if (lifetime != Qualifiers::OCL_ExplicitNone)
776     accessedByInit = (capturedByInit || isAccessedBy(D, init));
777   if (accessedByInit) {
778     LValue tempLV = lvalue;
779     // Drill down to the __block object if necessary.
780     if (capturedByInit) {
781       // We can use a simple GEP for this because it can't have been
782       // moved yet.
783       tempLV.setAddress(emitBlockByrefAddress(tempLV.getAddress(),
784                                               cast<VarDecl>(D),
785                                               /*follow*/ false));
786     }
787 
788     auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType());
789     llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
790 
791     // If __weak, we want to use a barrier under certain conditions.
792     if (lifetime == Qualifiers::OCL_Weak)
793       EmitARCInitWeak(tempLV.getAddress(), zero);
794 
795     // Otherwise just do a simple store.
796     else
797       EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
798   }
799 
800   // Emit the initializer.
801   llvm::Value *value = nullptr;
802 
803   switch (lifetime) {
804   case Qualifiers::OCL_None:
805     llvm_unreachable("present but none");
806 
807   case Qualifiers::OCL_Strong: {
808     if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) {
809       value = EmitARCRetainScalarExpr(init);
810       break;
811     }
812     // If D is pseudo-strong, treat it like __unsafe_unretained here. This means
813     // that we omit the retain, and causes non-autoreleased return values to be
814     // immediately released.
815     LLVM_FALLTHROUGH;
816   }
817 
818   case Qualifiers::OCL_ExplicitNone:
819     value = EmitARCUnsafeUnretainedScalarExpr(init);
820     break;
821 
822   case Qualifiers::OCL_Weak: {
823     // If it's not accessed by the initializer, try to emit the
824     // initialization with a copy or move.
825     if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) {
826       return;
827     }
828 
829     // No way to optimize a producing initializer into this.  It's not
830     // worth optimizing for, because the value will immediately
831     // disappear in the common case.
832     value = EmitScalarExpr(init);
833 
834     if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
835     if (accessedByInit)
836       EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
837     else
838       EmitARCInitWeak(lvalue.getAddress(), value);
839     return;
840   }
841 
842   case Qualifiers::OCL_Autoreleasing:
843     value = EmitARCRetainAutoreleaseScalarExpr(init);
844     break;
845   }
846 
847   if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
848 
849   EmitNullabilityCheck(lvalue, value, init->getExprLoc());
850 
851   // If the variable might have been accessed by its initializer, we
852   // might have to initialize with a barrier.  We have to do this for
853   // both __weak and __strong, but __weak got filtered out above.
854   if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
855     llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
856     EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
857     EmitARCRelease(oldValue, ARCImpreciseLifetime);
858     return;
859   }
860 
861   EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
862 }
863 
864 /// Decide whether we can emit the non-zero parts of the specified initializer
865 /// with equal or fewer than NumStores scalar stores.
866 static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init,
867                                                unsigned &NumStores) {
868   // Zero and Undef never requires any extra stores.
869   if (isa<llvm::ConstantAggregateZero>(Init) ||
870       isa<llvm::ConstantPointerNull>(Init) ||
871       isa<llvm::UndefValue>(Init))
872     return true;
873   if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
874       isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
875       isa<llvm::ConstantExpr>(Init))
876     return Init->isNullValue() || NumStores--;
877 
878   // See if we can emit each element.
879   if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
880     for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
881       llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
882       if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
883         return false;
884     }
885     return true;
886   }
887 
888   if (llvm::ConstantDataSequential *CDS =
889         dyn_cast<llvm::ConstantDataSequential>(Init)) {
890     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
891       llvm::Constant *Elt = CDS->getElementAsConstant(i);
892       if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
893         return false;
894     }
895     return true;
896   }
897 
898   // Anything else is hard and scary.
899   return false;
900 }
901 
902 /// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit
903 /// the scalar stores that would be required.
904 static void emitStoresForInitAfterBZero(CodeGenModule &CGM,
905                                         llvm::Constant *Init, Address Loc,
906                                         bool isVolatile, CGBuilderTy &Builder) {
907   assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
908          "called emitStoresForInitAfterBZero for zero or undef value.");
909 
910   if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
911       isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
912       isa<llvm::ConstantExpr>(Init)) {
913     Builder.CreateStore(Init, Loc, isVolatile);
914     return;
915   }
916 
917   if (llvm::ConstantDataSequential *CDS =
918           dyn_cast<llvm::ConstantDataSequential>(Init)) {
919     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
920       llvm::Constant *Elt = CDS->getElementAsConstant(i);
921 
922       // If necessary, get a pointer to the element and emit it.
923       if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
924         emitStoresForInitAfterBZero(
925             CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile,
926             Builder);
927     }
928     return;
929   }
930 
931   assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
932          "Unknown value type!");
933 
934   for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
935     llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
936 
937     // If necessary, get a pointer to the element and emit it.
938     if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
939       emitStoresForInitAfterBZero(CGM, Elt,
940                                   Builder.CreateConstInBoundsGEP2_32(Loc, 0, i),
941                                   isVolatile, Builder);
942   }
943 }
944 
945 /// Decide whether we should use bzero plus some stores to initialize a local
946 /// variable instead of using a memcpy from a constant global.  It is beneficial
947 /// to use bzero if the global is all zeros, or mostly zeros and large.
948 static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init,
949                                                  uint64_t GlobalSize) {
950   // If a global is all zeros, always use a bzero.
951   if (isa<llvm::ConstantAggregateZero>(Init)) return true;
952 
953   // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
954   // do it if it will require 6 or fewer scalar stores.
955   // TODO: Should budget depends on the size?  Avoiding a large global warrants
956   // plopping in more stores.
957   unsigned StoreBudget = 6;
958   uint64_t SizeLimit = 32;
959 
960   return GlobalSize > SizeLimit &&
961          canEmitInitWithFewStoresAfterBZero(Init, StoreBudget);
962 }
963 
964 /// Decide whether we should use memset to initialize a local variable instead
965 /// of using a memcpy from a constant global. Assumes we've already decided to
966 /// not user bzero.
967 /// FIXME We could be more clever, as we are for bzero above, and generate
968 ///       memset followed by stores. It's unclear that's worth the effort.
969 static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init,
970                                                 uint64_t GlobalSize,
971                                                 const llvm::DataLayout &DL) {
972   uint64_t SizeLimit = 32;
973   if (GlobalSize <= SizeLimit)
974     return nullptr;
975   return llvm::isBytewiseValue(Init, DL);
976 }
977 
978 /// Decide whether we want to split a constant structure or array store into a
979 /// sequence of its fields' stores. This may cost us code size and compilation
980 /// speed, but plays better with store optimizations.
981 static bool shouldSplitConstantStore(CodeGenModule &CGM,
982                                      uint64_t GlobalByteSize) {
983   // Don't break things that occupy more than one cacheline.
984   uint64_t ByteSizeLimit = 64;
985   if (CGM.getCodeGenOpts().OptimizationLevel == 0)
986     return false;
987   if (GlobalByteSize <= ByteSizeLimit)
988     return true;
989   return false;
990 }
991 
992 enum class IsPattern { No, Yes };
993 
994 /// Generate a constant filled with either a pattern or zeroes.
995 static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern,
996                                         llvm::Type *Ty) {
997   if (isPattern == IsPattern::Yes)
998     return initializationPatternFor(CGM, Ty);
999   else
1000     return llvm::Constant::getNullValue(Ty);
1001 }
1002 
1003 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1004                                         llvm::Constant *constant);
1005 
1006 /// Helper function for constWithPadding() to deal with padding in structures.
1007 static llvm::Constant *constStructWithPadding(CodeGenModule &CGM,
1008                                               IsPattern isPattern,
1009                                               llvm::StructType *STy,
1010                                               llvm::Constant *constant) {
1011   const llvm::DataLayout &DL = CGM.getDataLayout();
1012   const llvm::StructLayout *Layout = DL.getStructLayout(STy);
1013   llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1014   unsigned SizeSoFar = 0;
1015   SmallVector<llvm::Constant *, 8> Values;
1016   bool NestedIntact = true;
1017   for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1018     unsigned CurOff = Layout->getElementOffset(i);
1019     if (SizeSoFar < CurOff) {
1020       assert(!STy->isPacked());
1021       auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar);
1022       Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1023     }
1024     llvm::Constant *CurOp;
1025     if (constant->isZeroValue())
1026       CurOp = llvm::Constant::getNullValue(STy->getElementType(i));
1027     else
1028       CurOp = cast<llvm::Constant>(constant->getAggregateElement(i));
1029     auto *NewOp = constWithPadding(CGM, isPattern, CurOp);
1030     if (CurOp != NewOp)
1031       NestedIntact = false;
1032     Values.push_back(NewOp);
1033     SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType());
1034   }
1035   unsigned TotalSize = Layout->getSizeInBytes();
1036   if (SizeSoFar < TotalSize) {
1037     auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar);
1038     Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1039   }
1040   if (NestedIntact && Values.size() == STy->getNumElements())
1041     return constant;
1042   return llvm::ConstantStruct::getAnon(Values, STy->isPacked());
1043 }
1044 
1045 /// Replace all padding bytes in a given constant with either a pattern byte or
1046 /// 0x00.
1047 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1048                                         llvm::Constant *constant) {
1049   llvm::Type *OrigTy = constant->getType();
1050   if (const auto STy = dyn_cast<llvm::StructType>(OrigTy))
1051     return constStructWithPadding(CGM, isPattern, STy, constant);
1052   if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) {
1053     llvm::SmallVector<llvm::Constant *, 8> Values;
1054     unsigned Size = STy->getNumElements();
1055     if (!Size)
1056       return constant;
1057     llvm::Type *ElemTy = STy->getElementType();
1058     bool ZeroInitializer = constant->isZeroValue();
1059     llvm::Constant *OpValue, *PaddedOp;
1060     if (ZeroInitializer) {
1061       OpValue = llvm::Constant::getNullValue(ElemTy);
1062       PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1063     }
1064     for (unsigned Op = 0; Op != Size; ++Op) {
1065       if (!ZeroInitializer) {
1066         OpValue = constant->getAggregateElement(Op);
1067         PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1068       }
1069       Values.push_back(PaddedOp);
1070     }
1071     auto *NewElemTy = Values[0]->getType();
1072     if (NewElemTy == ElemTy)
1073       return constant;
1074     if (OrigTy->isArrayTy()) {
1075       auto *ArrayTy = llvm::ArrayType::get(NewElemTy, Size);
1076       return llvm::ConstantArray::get(ArrayTy, Values);
1077     } else {
1078       return llvm::ConstantVector::get(Values);
1079     }
1080   }
1081   return constant;
1082 }
1083 
1084 Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D,
1085                                                llvm::Constant *Constant,
1086                                                CharUnits Align) {
1087   auto FunctionName = [&](const DeclContext *DC) -> std::string {
1088     if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
1089       if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD))
1090         return CC->getNameAsString();
1091       if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD))
1092         return CD->getNameAsString();
1093       return getMangledName(FD);
1094     } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) {
1095       return OM->getNameAsString();
1096     } else if (isa<BlockDecl>(DC)) {
1097       return "<block>";
1098     } else if (isa<CapturedDecl>(DC)) {
1099       return "<captured>";
1100     } else {
1101       llvm_unreachable("expected a function or method");
1102     }
1103   };
1104 
1105   // Form a simple per-variable cache of these values in case we find we
1106   // want to reuse them.
1107   llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D];
1108   if (!CacheEntry || CacheEntry->getInitializer() != Constant) {
1109     auto *Ty = Constant->getType();
1110     bool isConstant = true;
1111     llvm::GlobalVariable *InsertBefore = nullptr;
1112     unsigned AS =
1113         getContext().getTargetAddressSpace(getStringLiteralAddressSpace());
1114     std::string Name;
1115     if (D.hasGlobalStorage())
1116       Name = getMangledName(&D).str() + ".const";
1117     else if (const DeclContext *DC = D.getParentFunctionOrMethod())
1118       Name = ("__const." + FunctionName(DC) + "." + D.getName()).str();
1119     else
1120       llvm_unreachable("local variable has no parent function or method");
1121     llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1122         getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage,
1123         Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS);
1124     GV->setAlignment(Align.getQuantity());
1125     GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1126     CacheEntry = GV;
1127   } else if (CacheEntry->getAlignment() < Align.getQuantity()) {
1128     CacheEntry->setAlignment(Align.getQuantity());
1129   }
1130 
1131   return Address(CacheEntry, Align);
1132 }
1133 
1134 static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM,
1135                                                 const VarDecl &D,
1136                                                 CGBuilderTy &Builder,
1137                                                 llvm::Constant *Constant,
1138                                                 CharUnits Align) {
1139   Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align);
1140   llvm::Type *BP = llvm::PointerType::getInt8PtrTy(CGM.getLLVMContext(),
1141                                                    SrcPtr.getAddressSpace());
1142   if (SrcPtr.getType() != BP)
1143     SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1144   return SrcPtr;
1145 }
1146 
1147 static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
1148                                   Address Loc, bool isVolatile,
1149                                   CGBuilderTy &Builder,
1150                                   llvm::Constant *constant) {
1151   auto *Ty = constant->getType();
1152   uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty);
1153   if (!ConstantSize)
1154     return;
1155 
1156   bool canDoSingleStore = Ty->isIntOrIntVectorTy() ||
1157                           Ty->isPtrOrPtrVectorTy() || Ty->isFPOrFPVectorTy();
1158   if (canDoSingleStore) {
1159     Builder.CreateStore(constant, Loc, isVolatile);
1160     return;
1161   }
1162 
1163   auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, ConstantSize);
1164 
1165   // If the initializer is all or mostly the same, codegen with bzero / memset
1166   // then do a few stores afterward.
1167   if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) {
1168     Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0), SizeVal,
1169                          isVolatile);
1170 
1171     bool valueAlreadyCorrect =
1172         constant->isNullValue() || isa<llvm::UndefValue>(constant);
1173     if (!valueAlreadyCorrect) {
1174       Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo(Loc.getAddressSpace()));
1175       emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder);
1176     }
1177     return;
1178   }
1179 
1180   // If the initializer is a repeated byte pattern, use memset.
1181   llvm::Value *Pattern =
1182       shouldUseMemSetToInitialize(constant, ConstantSize, CGM.getDataLayout());
1183   if (Pattern) {
1184     uint64_t Value = 0x00;
1185     if (!isa<llvm::UndefValue>(Pattern)) {
1186       const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();
1187       assert(AP.getBitWidth() <= 8);
1188       Value = AP.getLimitedValue();
1189     }
1190     Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal,
1191                          isVolatile);
1192     return;
1193   }
1194 
1195   // If the initializer is small, use a handful of stores.
1196   if (shouldSplitConstantStore(CGM, ConstantSize)) {
1197     if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1198       // FIXME: handle the case when STy != Loc.getElementType().
1199       if (STy == Loc.getElementType()) {
1200         for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1201           Address EltPtr = Builder.CreateStructGEP(Loc, i);
1202           emitStoresForConstant(
1203               CGM, D, EltPtr, isVolatile, Builder,
1204               cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1205         }
1206         return;
1207       }
1208     } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1209       // FIXME: handle the case when ATy != Loc.getElementType().
1210       if (ATy == Loc.getElementType()) {
1211         for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1212           Address EltPtr = Builder.CreateConstArrayGEP(Loc, i);
1213           emitStoresForConstant(
1214               CGM, D, EltPtr, isVolatile, Builder,
1215               cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1216         }
1217         return;
1218       }
1219     }
1220   }
1221 
1222   // Copy from a global.
1223   Builder.CreateMemCpy(Loc,
1224                        createUnnamedGlobalForMemcpyFrom(
1225                            CGM, D, Builder, constant, Loc.getAlignment()),
1226                        SizeVal, isVolatile);
1227 }
1228 
1229 static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D,
1230                                   Address Loc, bool isVolatile,
1231                                   CGBuilderTy &Builder) {
1232   llvm::Type *ElTy = Loc.getElementType();
1233   llvm::Constant *constant =
1234       constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy));
1235   emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1236 }
1237 
1238 static void emitStoresForPatternInit(CodeGenModule &CGM, const VarDecl &D,
1239                                      Address Loc, bool isVolatile,
1240                                      CGBuilderTy &Builder) {
1241   llvm::Type *ElTy = Loc.getElementType();
1242   llvm::Constant *constant = constWithPadding(
1243       CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1244   assert(!isa<llvm::UndefValue>(constant));
1245   emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1246 }
1247 
1248 static bool containsUndef(llvm::Constant *constant) {
1249   auto *Ty = constant->getType();
1250   if (isa<llvm::UndefValue>(constant))
1251     return true;
1252   if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())
1253     for (llvm::Use &Op : constant->operands())
1254       if (containsUndef(cast<llvm::Constant>(Op)))
1255         return true;
1256   return false;
1257 }
1258 
1259 static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern,
1260                                     llvm::Constant *constant) {
1261   auto *Ty = constant->getType();
1262   if (isa<llvm::UndefValue>(constant))
1263     return patternOrZeroFor(CGM, isPattern, Ty);
1264   if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()))
1265     return constant;
1266   if (!containsUndef(constant))
1267     return constant;
1268   llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands());
1269   for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) {
1270     auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op));
1271     Values[Op] = replaceUndef(CGM, isPattern, OpValue);
1272   }
1273   if (Ty->isStructTy())
1274     return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values);
1275   if (Ty->isArrayTy())
1276     return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values);
1277   assert(Ty->isVectorTy());
1278   return llvm::ConstantVector::get(Values);
1279 }
1280 
1281 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
1282 /// variable declaration with auto, register, or no storage class specifier.
1283 /// These turn into simple stack objects, or GlobalValues depending on target.
1284 void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
1285   AutoVarEmission emission = EmitAutoVarAlloca(D);
1286   EmitAutoVarInit(emission);
1287   EmitAutoVarCleanups(emission);
1288 }
1289 
1290 /// Emit a lifetime.begin marker if some criteria are satisfied.
1291 /// \return a pointer to the temporary size Value if a marker was emitted, null
1292 /// otherwise
1293 llvm::Value *CodeGenFunction::EmitLifetimeStart(uint64_t Size,
1294                                                 llvm::Value *Addr) {
1295   if (!ShouldEmitLifetimeMarkers)
1296     return nullptr;
1297 
1298   assert(Addr->getType()->getPointerAddressSpace() ==
1299              CGM.getDataLayout().getAllocaAddrSpace() &&
1300          "Pointer should be in alloca address space");
1301   llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size);
1302   Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1303   llvm::CallInst *C =
1304       Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
1305   C->setDoesNotThrow();
1306   return SizeV;
1307 }
1308 
1309 void CodeGenFunction::EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr) {
1310   assert(Addr->getType()->getPointerAddressSpace() ==
1311              CGM.getDataLayout().getAllocaAddrSpace() &&
1312          "Pointer should be in alloca address space");
1313   Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1314   llvm::CallInst *C =
1315       Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
1316   C->setDoesNotThrow();
1317 }
1318 
1319 void CodeGenFunction::EmitAndRegisterVariableArrayDimensions(
1320     CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) {
1321   // For each dimension stores its QualType and corresponding
1322   // size-expression Value.
1323   SmallVector<CodeGenFunction::VlaSizePair, 4> Dimensions;
1324   SmallVector<IdentifierInfo *, 4> VLAExprNames;
1325 
1326   // Break down the array into individual dimensions.
1327   QualType Type1D = D.getType();
1328   while (getContext().getAsVariableArrayType(Type1D)) {
1329     auto VlaSize = getVLAElements1D(Type1D);
1330     if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1331       Dimensions.emplace_back(C, Type1D.getUnqualifiedType());
1332     else {
1333       // Generate a locally unique name for the size expression.
1334       Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++);
1335       SmallString<12> Buffer;
1336       StringRef NameRef = Name.toStringRef(Buffer);
1337       auto &Ident = getContext().Idents.getOwn(NameRef);
1338       VLAExprNames.push_back(&Ident);
1339       auto SizeExprAddr =
1340           CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef);
1341       Builder.CreateStore(VlaSize.NumElts, SizeExprAddr);
1342       Dimensions.emplace_back(SizeExprAddr.getPointer(),
1343                               Type1D.getUnqualifiedType());
1344     }
1345     Type1D = VlaSize.Type;
1346   }
1347 
1348   if (!EmitDebugInfo)
1349     return;
1350 
1351   // Register each dimension's size-expression with a DILocalVariable,
1352   // so that it can be used by CGDebugInfo when instantiating a DISubrange
1353   // to describe this array.
1354   unsigned NameIdx = 0;
1355   for (auto &VlaSize : Dimensions) {
1356     llvm::Metadata *MD;
1357     if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1358       MD = llvm::ConstantAsMetadata::get(C);
1359     else {
1360       // Create an artificial VarDecl to generate debug info for.
1361       IdentifierInfo *NameIdent = VLAExprNames[NameIdx++];
1362       auto VlaExprTy = VlaSize.NumElts->getType()->getPointerElementType();
1363       auto QT = getContext().getIntTypeForBitwidth(
1364           VlaExprTy->getScalarSizeInBits(), false);
1365       auto *ArtificialDecl = VarDecl::Create(
1366           getContext(), const_cast<DeclContext *>(D.getDeclContext()),
1367           D.getLocation(), D.getLocation(), NameIdent, QT,
1368           getContext().CreateTypeSourceInfo(QT), SC_Auto);
1369       ArtificialDecl->setImplicit();
1370 
1371       MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts,
1372                                          Builder);
1373     }
1374     assert(MD && "No Size expression debug node created");
1375     DI->registerVLASizeExpression(VlaSize.Type, MD);
1376   }
1377 }
1378 
1379 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
1380 /// local variable.  Does not emit initialization or destruction.
1381 CodeGenFunction::AutoVarEmission
1382 CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
1383   QualType Ty = D.getType();
1384   assert(
1385       Ty.getAddressSpace() == LangAS::Default ||
1386       (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL));
1387 
1388   AutoVarEmission emission(D);
1389 
1390   bool isEscapingByRef = D.isEscapingByref();
1391   emission.IsEscapingByRef = isEscapingByRef;
1392 
1393   CharUnits alignment = getContext().getDeclAlign(&D);
1394 
1395   // If the type is variably-modified, emit all the VLA sizes for it.
1396   if (Ty->isVariablyModifiedType())
1397     EmitVariablyModifiedType(Ty);
1398 
1399   auto *DI = getDebugInfo();
1400   bool EmitDebugInfo = DI && CGM.getCodeGenOpts().getDebugInfo() >=
1401                                  codegenoptions::LimitedDebugInfo;
1402 
1403   Address address = Address::invalid();
1404   Address AllocaAddr = Address::invalid();
1405   Address OpenMPLocalAddr =
1406       getLangOpts().OpenMP
1407           ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
1408           : Address::invalid();
1409   bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable();
1410 
1411   if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
1412     address = OpenMPLocalAddr;
1413   } else if (Ty->isConstantSizeType()) {
1414     // If this value is an array or struct with a statically determinable
1415     // constant initializer, there are optimizations we can do.
1416     //
1417     // TODO: We should constant-evaluate the initializer of any variable,
1418     // as long as it is initialized by a constant expression. Currently,
1419     // isConstantInitializer produces wrong answers for structs with
1420     // reference or bitfield members, and a few other cases, and checking
1421     // for POD-ness protects us from some of these.
1422     if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
1423         (D.isConstexpr() ||
1424          ((Ty.isPODType(getContext()) ||
1425            getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
1426           D.getInit()->isConstantInitializer(getContext(), false)))) {
1427 
1428       // If the variable's a const type, and it's neither an NRVO
1429       // candidate nor a __block variable and has no mutable members,
1430       // emit it as a global instead.
1431       // Exception is if a variable is located in non-constant address space
1432       // in OpenCL.
1433       if ((!getLangOpts().OpenCL ||
1434            Ty.getAddressSpace() == LangAS::opencl_constant) &&
1435           (CGM.getCodeGenOpts().MergeAllConstants && !NRVO &&
1436            !isEscapingByRef && CGM.isTypeConstant(Ty, true))) {
1437         EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
1438 
1439         // Signal this condition to later callbacks.
1440         emission.Addr = Address::invalid();
1441         assert(emission.wasEmittedAsGlobal());
1442         return emission;
1443       }
1444 
1445       // Otherwise, tell the initialization code that we're in this case.
1446       emission.IsConstantAggregate = true;
1447     }
1448 
1449     // A normal fixed sized variable becomes an alloca in the entry block,
1450     // unless:
1451     // - it's an NRVO variable.
1452     // - we are compiling OpenMP and it's an OpenMP local variable.
1453     if (NRVO) {
1454       // The named return value optimization: allocate this variable in the
1455       // return slot, so that we can elide the copy when returning this
1456       // variable (C++0x [class.copy]p34).
1457       address = ReturnValue;
1458 
1459       if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1460         const auto *RD = RecordTy->getDecl();
1461         const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1462         if ((CXXRD && !CXXRD->hasTrivialDestructor()) ||
1463             RD->isNonTrivialToPrimitiveDestroy()) {
1464           // Create a flag that is used to indicate when the NRVO was applied
1465           // to this variable. Set it to zero to indicate that NRVO was not
1466           // applied.
1467           llvm::Value *Zero = Builder.getFalse();
1468           Address NRVOFlag =
1469             CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1470           EnsureInsertPoint();
1471           Builder.CreateStore(Zero, NRVOFlag);
1472 
1473           // Record the NRVO flag for this variable.
1474           NRVOFlags[&D] = NRVOFlag.getPointer();
1475           emission.NRVOFlag = NRVOFlag.getPointer();
1476         }
1477       }
1478     } else {
1479       CharUnits allocaAlignment;
1480       llvm::Type *allocaTy;
1481       if (isEscapingByRef) {
1482         auto &byrefInfo = getBlockByrefInfo(&D);
1483         allocaTy = byrefInfo.Type;
1484         allocaAlignment = byrefInfo.ByrefAlignment;
1485       } else {
1486         allocaTy = ConvertTypeForMem(Ty);
1487         allocaAlignment = alignment;
1488       }
1489 
1490       // Create the alloca.  Note that we set the name separately from
1491       // building the instruction so that it's there even in no-asserts
1492       // builds.
1493       address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(),
1494                                  /*ArraySize=*/nullptr, &AllocaAddr);
1495 
1496       // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1497       // the catch parameter starts in the catchpad instruction, and we can't
1498       // insert code in those basic blocks.
1499       bool IsMSCatchParam =
1500           D.isExceptionVariable() && getTarget().getCXXABI().isMicrosoft();
1501 
1502       // Emit a lifetime intrinsic if meaningful. There's no point in doing this
1503       // if we don't have a valid insertion point (?).
1504       if (HaveInsertPoint() && !IsMSCatchParam) {
1505         // If there's a jump into the lifetime of this variable, its lifetime
1506         // gets broken up into several regions in IR, which requires more work
1507         // to handle correctly. For now, just omit the intrinsics; this is a
1508         // rare case, and it's better to just be conservatively correct.
1509         // PR28267.
1510         //
1511         // We have to do this in all language modes if there's a jump past the
1512         // declaration. We also have to do it in C if there's a jump to an
1513         // earlier point in the current block because non-VLA lifetimes begin as
1514         // soon as the containing block is entered, not when its variables
1515         // actually come into scope; suppressing the lifetime annotations
1516         // completely in this case is unnecessarily pessimistic, but again, this
1517         // is rare.
1518         if (!Bypasses.IsBypassed(&D) &&
1519             !(!getLangOpts().CPlusPlus && hasLabelBeenSeenInCurrentScope())) {
1520           uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1521           emission.SizeForLifetimeMarkers =
1522               EmitLifetimeStart(size, AllocaAddr.getPointer());
1523         }
1524       } else {
1525         assert(!emission.useLifetimeMarkers());
1526       }
1527     }
1528   } else {
1529     EnsureInsertPoint();
1530 
1531     if (!DidCallStackSave) {
1532       // Save the stack.
1533       Address Stack =
1534         CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1535 
1536       llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1537       llvm::Value *V = Builder.CreateCall(F);
1538       Builder.CreateStore(V, Stack);
1539 
1540       DidCallStackSave = true;
1541 
1542       // Push a cleanup block and restore the stack there.
1543       // FIXME: in general circumstances, this should be an EH cleanup.
1544       pushStackRestore(NormalCleanup, Stack);
1545     }
1546 
1547     auto VlaSize = getVLASize(Ty);
1548     llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
1549 
1550     // Allocate memory for the array.
1551     address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
1552                                &AllocaAddr);
1553 
1554     // If we have debug info enabled, properly describe the VLA dimensions for
1555     // this type by registering the vla size expression for each of the
1556     // dimensions.
1557     EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo);
1558   }
1559 
1560   setAddrOfLocalVar(&D, address);
1561   emission.Addr = address;
1562   emission.AllocaAddr = AllocaAddr;
1563 
1564   // Emit debug info for local var declaration.
1565   if (EmitDebugInfo && HaveInsertPoint()) {
1566     Address DebugAddr = address;
1567     bool UsePointerValue = NRVO && ReturnValuePointer.isValid();
1568     DI->setLocation(D.getLocation());
1569 
1570     // If NRVO, use a pointer to the return address.
1571     if (UsePointerValue)
1572       DebugAddr = ReturnValuePointer;
1573 
1574     (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder,
1575                                         UsePointerValue);
1576   }
1577 
1578   if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
1579     EmitVarAnnotations(&D, address.getPointer());
1580 
1581   // Make sure we call @llvm.lifetime.end.
1582   if (emission.useLifetimeMarkers())
1583     EHStack.pushCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker,
1584                                          emission.getOriginalAllocatedAddress(),
1585                                          emission.getSizeForLifetimeMarkers());
1586 
1587   return emission;
1588 }
1589 
1590 static bool isCapturedBy(const VarDecl &, const Expr *);
1591 
1592 /// Determines whether the given __block variable is potentially
1593 /// captured by the given statement.
1594 static bool isCapturedBy(const VarDecl &Var, const Stmt *S) {
1595   if (const Expr *E = dyn_cast<Expr>(S))
1596     return isCapturedBy(Var, E);
1597   for (const Stmt *SubStmt : S->children())
1598     if (isCapturedBy(Var, SubStmt))
1599       return true;
1600   return false;
1601 }
1602 
1603 /// Determines whether the given __block variable is potentially
1604 /// captured by the given expression.
1605 static bool isCapturedBy(const VarDecl &Var, const Expr *E) {
1606   // Skip the most common kinds of expressions that make
1607   // hierarchy-walking expensive.
1608   E = E->IgnoreParenCasts();
1609 
1610   if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) {
1611     const BlockDecl *Block = BE->getBlockDecl();
1612     for (const auto &I : Block->captures()) {
1613       if (I.getVariable() == &Var)
1614         return true;
1615     }
1616 
1617     // No need to walk into the subexpressions.
1618     return false;
1619   }
1620 
1621   if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
1622     const CompoundStmt *CS = SE->getSubStmt();
1623     for (const auto *BI : CS->body())
1624       if (const auto *BIE = dyn_cast<Expr>(BI)) {
1625         if (isCapturedBy(Var, BIE))
1626           return true;
1627       }
1628       else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1629           // special case declarations
1630           for (const auto *I : DS->decls()) {
1631               if (const auto *VD = dyn_cast<VarDecl>((I))) {
1632                 const Expr *Init = VD->getInit();
1633                 if (Init && isCapturedBy(Var, Init))
1634                   return true;
1635               }
1636           }
1637       }
1638       else
1639         // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1640         // Later, provide code to poke into statements for capture analysis.
1641         return true;
1642     return false;
1643   }
1644 
1645   for (const Stmt *SubStmt : E->children())
1646     if (isCapturedBy(Var, SubStmt))
1647       return true;
1648 
1649   return false;
1650 }
1651 
1652 /// Determine whether the given initializer is trivial in the sense
1653 /// that it requires no code to be generated.
1654 bool CodeGenFunction::isTrivialInitializer(const Expr *Init) {
1655   if (!Init)
1656     return true;
1657 
1658   if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1659     if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1660       if (Constructor->isTrivial() &&
1661           Constructor->isDefaultConstructor() &&
1662           !Construct->requiresZeroInitialization())
1663         return true;
1664 
1665   return false;
1666 }
1667 
1668 void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type,
1669                                                       const VarDecl &D,
1670                                                       Address Loc) {
1671   auto trivialAutoVarInit = getContext().getLangOpts().getTrivialAutoVarInit();
1672   CharUnits Size = getContext().getTypeSizeInChars(type);
1673   bool isVolatile = type.isVolatileQualified();
1674   if (!Size.isZero()) {
1675     switch (trivialAutoVarInit) {
1676     case LangOptions::TrivialAutoVarInitKind::Uninitialized:
1677       llvm_unreachable("Uninitialized handled by caller");
1678     case LangOptions::TrivialAutoVarInitKind::Zero:
1679       emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder);
1680       break;
1681     case LangOptions::TrivialAutoVarInitKind::Pattern:
1682       emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder);
1683       break;
1684     }
1685     return;
1686   }
1687 
1688   // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to
1689   // them, so emit a memcpy with the VLA size to initialize each element.
1690   // Technically zero-sized or negative-sized VLAs are undefined, and UBSan
1691   // will catch that code, but there exists code which generates zero-sized
1692   // VLAs. Be nice and initialize whatever they requested.
1693   const auto *VlaType = getContext().getAsVariableArrayType(type);
1694   if (!VlaType)
1695     return;
1696   auto VlaSize = getVLASize(VlaType);
1697   auto SizeVal = VlaSize.NumElts;
1698   CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1699   switch (trivialAutoVarInit) {
1700   case LangOptions::TrivialAutoVarInitKind::Uninitialized:
1701     llvm_unreachable("Uninitialized handled by caller");
1702 
1703   case LangOptions::TrivialAutoVarInitKind::Zero:
1704     if (!EltSize.isOne())
1705       SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1706     Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1707                          isVolatile);
1708     break;
1709 
1710   case LangOptions::TrivialAutoVarInitKind::Pattern: {
1711     llvm::Type *ElTy = Loc.getElementType();
1712     llvm::Constant *Constant = constWithPadding(
1713         CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1714     CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type);
1715     llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop");
1716     llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop");
1717     llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont");
1718     llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ(
1719         SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0),
1720         "vla.iszerosized");
1721     Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB);
1722     EmitBlock(SetupBB);
1723     if (!EltSize.isOne())
1724       SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1725     llvm::Value *BaseSizeInChars =
1726         llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity());
1727     Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin");
1728     llvm::Value *End =
1729         Builder.CreateInBoundsGEP(Begin.getPointer(), SizeVal, "vla.end");
1730     llvm::BasicBlock *OriginBB = Builder.GetInsertBlock();
1731     EmitBlock(LoopBB);
1732     llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur");
1733     Cur->addIncoming(Begin.getPointer(), OriginBB);
1734     CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize);
1735     Builder.CreateMemCpy(Address(Cur, CurAlign),
1736                          createUnnamedGlobalForMemcpyFrom(
1737                              CGM, D, Builder, Constant, ConstantAlign),
1738                          BaseSizeInChars, isVolatile);
1739     llvm::Value *Next =
1740         Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next");
1741     llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone");
1742     Builder.CreateCondBr(Done, ContBB, LoopBB);
1743     Cur->addIncoming(Next, LoopBB);
1744     EmitBlock(ContBB);
1745   } break;
1746   }
1747 }
1748 
1749 void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
1750   assert(emission.Variable && "emission was not valid!");
1751 
1752   // If this was emitted as a global constant, we're done.
1753   if (emission.wasEmittedAsGlobal()) return;
1754 
1755   const VarDecl &D = *emission.Variable;
1756   auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, D.getLocation());
1757   QualType type = D.getType();
1758 
1759   // If this local has an initializer, emit it now.
1760   const Expr *Init = D.getInit();
1761 
1762   // If we are at an unreachable point, we don't need to emit the initializer
1763   // unless it contains a label.
1764   if (!HaveInsertPoint()) {
1765     if (!Init || !ContainsLabel(Init)) return;
1766     EnsureInsertPoint();
1767   }
1768 
1769   // Initialize the structure of a __block variable.
1770   if (emission.IsEscapingByRef)
1771     emitByrefStructureInit(emission);
1772 
1773   // Initialize the variable here if it doesn't have a initializer and it is a
1774   // C struct that is non-trivial to initialize or an array containing such a
1775   // struct.
1776   if (!Init &&
1777       type.isNonTrivialToPrimitiveDefaultInitialize() ==
1778           QualType::PDIK_Struct) {
1779     LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type);
1780     if (emission.IsEscapingByRef)
1781       drillIntoBlockVariable(*this, Dst, &D);
1782     defaultInitNonTrivialCStructVar(Dst);
1783     return;
1784   }
1785 
1786   // Check whether this is a byref variable that's potentially
1787   // captured and moved by its own initializer.  If so, we'll need to
1788   // emit the initializer first, then copy into the variable.
1789   bool capturedByInit =
1790       Init && emission.IsEscapingByRef && isCapturedBy(D, Init);
1791 
1792   bool locIsByrefHeader = !capturedByInit;
1793   const Address Loc =
1794       locIsByrefHeader ? emission.getObjectAddress(*this) : emission.Addr;
1795 
1796   // Note: constexpr already initializes everything correctly.
1797   LangOptions::TrivialAutoVarInitKind trivialAutoVarInit =
1798       (D.isConstexpr()
1799            ? LangOptions::TrivialAutoVarInitKind::Uninitialized
1800            : (D.getAttr<UninitializedAttr>()
1801                   ? LangOptions::TrivialAutoVarInitKind::Uninitialized
1802                   : getContext().getLangOpts().getTrivialAutoVarInit()));
1803 
1804   auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) {
1805     if (trivialAutoVarInit ==
1806         LangOptions::TrivialAutoVarInitKind::Uninitialized)
1807       return;
1808 
1809     // Only initialize a __block's storage: we always initialize the header.
1810     if (emission.IsEscapingByRef && !locIsByrefHeader)
1811       Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false);
1812 
1813     return emitZeroOrPatternForAutoVarInit(type, D, Loc);
1814   };
1815 
1816   if (isTrivialInitializer(Init))
1817     return initializeWhatIsTechnicallyUninitialized(Loc);
1818 
1819   llvm::Constant *constant = nullptr;
1820   if (emission.IsConstantAggregate ||
1821       D.mightBeUsableInConstantExpressions(getContext())) {
1822     assert(!capturedByInit && "constant init contains a capturing block?");
1823     constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D);
1824     if (constant && !constant->isZeroValue() &&
1825         (trivialAutoVarInit !=
1826          LangOptions::TrivialAutoVarInitKind::Uninitialized)) {
1827       IsPattern isPattern =
1828           (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern)
1829               ? IsPattern::Yes
1830               : IsPattern::No;
1831       // C guarantees that brace-init with fewer initializers than members in
1832       // the aggregate will initialize the rest of the aggregate as-if it were
1833       // static initialization. In turn static initialization guarantees that
1834       // padding is initialized to zero bits. We could instead pattern-init if D
1835       // has any ImplicitValueInitExpr, but that seems to be unintuitive
1836       // behavior.
1837       constant = constWithPadding(CGM, IsPattern::No,
1838                                   replaceUndef(CGM, isPattern, constant));
1839     }
1840   }
1841 
1842   if (!constant) {
1843     initializeWhatIsTechnicallyUninitialized(Loc);
1844     LValue lv = MakeAddrLValue(Loc, type);
1845     lv.setNonGC(true);
1846     return EmitExprAsInit(Init, &D, lv, capturedByInit);
1847   }
1848 
1849   if (!emission.IsConstantAggregate) {
1850     // For simple scalar/complex initialization, store the value directly.
1851     LValue lv = MakeAddrLValue(Loc, type);
1852     lv.setNonGC(true);
1853     return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1854   }
1855 
1856   llvm::Type *BP = CGM.Int8Ty->getPointerTo(Loc.getAddressSpace());
1857   emitStoresForConstant(
1858       CGM, D, (Loc.getType() == BP) ? Loc : Builder.CreateBitCast(Loc, BP),
1859       type.isVolatileQualified(), Builder, constant);
1860 }
1861 
1862 /// Emit an expression as an initializer for an object (variable, field, etc.)
1863 /// at the given location.  The expression is not necessarily the normal
1864 /// initializer for the object, and the address is not necessarily
1865 /// its normal location.
1866 ///
1867 /// \param init the initializing expression
1868 /// \param D the object to act as if we're initializing
1869 /// \param loc the address to initialize; its type is a pointer
1870 ///   to the LLVM mapping of the object's type
1871 /// \param alignment the alignment of the address
1872 /// \param capturedByInit true if \p D is a __block variable
1873 ///   whose address is potentially changed by the initializer
1874 void CodeGenFunction::EmitExprAsInit(const Expr *init, const ValueDecl *D,
1875                                      LValue lvalue, bool capturedByInit) {
1876   QualType type = D->getType();
1877 
1878   if (type->isReferenceType()) {
1879     RValue rvalue = EmitReferenceBindingToExpr(init);
1880     if (capturedByInit)
1881       drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1882     EmitStoreThroughLValue(rvalue, lvalue, true);
1883     return;
1884   }
1885   switch (getEvaluationKind(type)) {
1886   case TEK_Scalar:
1887     EmitScalarInit(init, D, lvalue, capturedByInit);
1888     return;
1889   case TEK_Complex: {
1890     ComplexPairTy complex = EmitComplexExpr(init);
1891     if (capturedByInit)
1892       drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1893     EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1894     return;
1895   }
1896   case TEK_Aggregate:
1897     if (type->isAtomicType()) {
1898       EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1899     } else {
1900       AggValueSlot::Overlap_t Overlap = AggValueSlot::MayOverlap;
1901       if (isa<VarDecl>(D))
1902         Overlap = AggValueSlot::DoesNotOverlap;
1903       else if (auto *FD = dyn_cast<FieldDecl>(D))
1904         Overlap = getOverlapForFieldInit(FD);
1905       // TODO: how can we delay here if D is captured by its initializer?
1906       EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1907                                               AggValueSlot::IsDestructed,
1908                                          AggValueSlot::DoesNotNeedGCBarriers,
1909                                               AggValueSlot::IsNotAliased,
1910                                               Overlap));
1911     }
1912     return;
1913   }
1914   llvm_unreachable("bad evaluation kind");
1915 }
1916 
1917 /// Enter a destroy cleanup for the given local variable.
1918 void CodeGenFunction::emitAutoVarTypeCleanup(
1919                             const CodeGenFunction::AutoVarEmission &emission,
1920                             QualType::DestructionKind dtorKind) {
1921   assert(dtorKind != QualType::DK_none);
1922 
1923   // Note that for __block variables, we want to destroy the
1924   // original stack object, not the possibly forwarded object.
1925   Address addr = emission.getObjectAddress(*this);
1926 
1927   const VarDecl *var = emission.Variable;
1928   QualType type = var->getType();
1929 
1930   CleanupKind cleanupKind = NormalAndEHCleanup;
1931   CodeGenFunction::Destroyer *destroyer = nullptr;
1932 
1933   switch (dtorKind) {
1934   case QualType::DK_none:
1935     llvm_unreachable("no cleanup for trivially-destructible variable");
1936 
1937   case QualType::DK_cxx_destructor:
1938     // If there's an NRVO flag on the emission, we need a different
1939     // cleanup.
1940     if (emission.NRVOFlag) {
1941       assert(!type->isArrayType());
1942       CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1943       EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, dtor,
1944                                                   emission.NRVOFlag);
1945       return;
1946     }
1947     break;
1948 
1949   case QualType::DK_objc_strong_lifetime:
1950     // Suppress cleanups for pseudo-strong variables.
1951     if (var->isARCPseudoStrong()) return;
1952 
1953     // Otherwise, consider whether to use an EH cleanup or not.
1954     cleanupKind = getARCCleanupKind();
1955 
1956     // Use the imprecise destroyer by default.
1957     if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1958       destroyer = CodeGenFunction::destroyARCStrongImprecise;
1959     break;
1960 
1961   case QualType::DK_objc_weak_lifetime:
1962     break;
1963 
1964   case QualType::DK_nontrivial_c_struct:
1965     destroyer = CodeGenFunction::destroyNonTrivialCStruct;
1966     if (emission.NRVOFlag) {
1967       assert(!type->isArrayType());
1968       EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr,
1969                                                 emission.NRVOFlag, type);
1970       return;
1971     }
1972     break;
1973   }
1974 
1975   // If we haven't chosen a more specific destroyer, use the default.
1976   if (!destroyer) destroyer = getDestroyer(dtorKind);
1977 
1978   // Use an EH cleanup in array destructors iff the destructor itself
1979   // is being pushed as an EH cleanup.
1980   bool useEHCleanup = (cleanupKind & EHCleanup);
1981   EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1982                                      useEHCleanup);
1983 }
1984 
1985 void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
1986   assert(emission.Variable && "emission was not valid!");
1987 
1988   // If this was emitted as a global constant, we're done.
1989   if (emission.wasEmittedAsGlobal()) return;
1990 
1991   // If we don't have an insertion point, we're done.  Sema prevents
1992   // us from jumping into any of these scopes anyway.
1993   if (!HaveInsertPoint()) return;
1994 
1995   const VarDecl &D = *emission.Variable;
1996 
1997   // Check the type for a cleanup.
1998   if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1999     emitAutoVarTypeCleanup(emission, dtorKind);
2000 
2001   // In GC mode, honor objc_precise_lifetime.
2002   if (getLangOpts().getGC() != LangOptions::NonGC &&
2003       D.hasAttr<ObjCPreciseLifetimeAttr>()) {
2004     EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
2005   }
2006 
2007   // Handle the cleanup attribute.
2008   if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
2009     const FunctionDecl *FD = CA->getFunctionDecl();
2010 
2011     llvm::Constant *F = CGM.GetAddrOfFunction(FD);
2012     assert(F && "Could not find function!");
2013 
2014     const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
2015     EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
2016   }
2017 
2018   // If this is a block variable, call _Block_object_destroy
2019   // (on the unforwarded address). Don't enter this cleanup if we're in pure-GC
2020   // mode.
2021   if (emission.IsEscapingByRef &&
2022       CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
2023     BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF;
2024     if (emission.Variable->getType().isObjCGCWeak())
2025       Flags |= BLOCK_FIELD_IS_WEAK;
2026     enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags,
2027                       /*LoadBlockVarAddr*/ false,
2028                       cxxDestructorCanThrow(emission.Variable->getType()));
2029   }
2030 }
2031 
2032 CodeGenFunction::Destroyer *
2033 CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
2034   switch (kind) {
2035   case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
2036   case QualType::DK_cxx_destructor:
2037     return destroyCXXObject;
2038   case QualType::DK_objc_strong_lifetime:
2039     return destroyARCStrongPrecise;
2040   case QualType::DK_objc_weak_lifetime:
2041     return destroyARCWeak;
2042   case QualType::DK_nontrivial_c_struct:
2043     return destroyNonTrivialCStruct;
2044   }
2045   llvm_unreachable("Unknown DestructionKind");
2046 }
2047 
2048 /// pushEHDestroy - Push the standard destructor for the given type as
2049 /// an EH-only cleanup.
2050 void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind,
2051                                     Address addr, QualType type) {
2052   assert(dtorKind && "cannot push destructor for trivial type");
2053   assert(needsEHCleanup(dtorKind));
2054 
2055   pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
2056 }
2057 
2058 /// pushDestroy - Push the standard destructor for the given type as
2059 /// at least a normal cleanup.
2060 void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
2061                                   Address addr, QualType type) {
2062   assert(dtorKind && "cannot push destructor for trivial type");
2063 
2064   CleanupKind cleanupKind = getCleanupKind(dtorKind);
2065   pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
2066               cleanupKind & EHCleanup);
2067 }
2068 
2069 void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, Address addr,
2070                                   QualType type, Destroyer *destroyer,
2071                                   bool useEHCleanupForArray) {
2072   pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
2073                                      destroyer, useEHCleanupForArray);
2074 }
2075 
2076 void CodeGenFunction::pushStackRestore(CleanupKind Kind, Address SPMem) {
2077   EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
2078 }
2079 
2080 void CodeGenFunction::pushLifetimeExtendedDestroy(
2081     CleanupKind cleanupKind, Address addr, QualType type,
2082     Destroyer *destroyer, bool useEHCleanupForArray) {
2083   // Push an EH-only cleanup for the object now.
2084   // FIXME: When popping normal cleanups, we need to keep this EH cleanup
2085   // around in case a temporary's destructor throws an exception.
2086   if (cleanupKind & EHCleanup)
2087     EHStack.pushCleanup<DestroyObject>(
2088         static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
2089         destroyer, useEHCleanupForArray);
2090 
2091   // Remember that we need to push a full cleanup for the object at the
2092   // end of the full-expression.
2093   pushCleanupAfterFullExpr<DestroyObject>(
2094       cleanupKind, addr, type, destroyer, useEHCleanupForArray);
2095 }
2096 
2097 /// emitDestroy - Immediately perform the destruction of the given
2098 /// object.
2099 ///
2100 /// \param addr - the address of the object; a type*
2101 /// \param type - the type of the object; if an array type, all
2102 ///   objects are destroyed in reverse order
2103 /// \param destroyer - the function to call to destroy individual
2104 ///   elements
2105 /// \param useEHCleanupForArray - whether an EH cleanup should be
2106 ///   used when destroying array elements, in case one of the
2107 ///   destructions throws an exception
2108 void CodeGenFunction::emitDestroy(Address addr, QualType type,
2109                                   Destroyer *destroyer,
2110                                   bool useEHCleanupForArray) {
2111   const ArrayType *arrayType = getContext().getAsArrayType(type);
2112   if (!arrayType)
2113     return destroyer(*this, addr, type);
2114 
2115   llvm::Value *length = emitArrayLength(arrayType, type, addr);
2116 
2117   CharUnits elementAlign =
2118     addr.getAlignment()
2119         .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
2120 
2121   // Normally we have to check whether the array is zero-length.
2122   bool checkZeroLength = true;
2123 
2124   // But if the array length is constant, we can suppress that.
2125   if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
2126     // ...and if it's constant zero, we can just skip the entire thing.
2127     if (constLength->isZero()) return;
2128     checkZeroLength = false;
2129   }
2130 
2131   llvm::Value *begin = addr.getPointer();
2132   llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
2133   emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2134                    checkZeroLength, useEHCleanupForArray);
2135 }
2136 
2137 /// emitArrayDestroy - Destroys all the elements of the given array,
2138 /// beginning from last to first.  The array cannot be zero-length.
2139 ///
2140 /// \param begin - a type* denoting the first element of the array
2141 /// \param end - a type* denoting one past the end of the array
2142 /// \param elementType - the element type of the array
2143 /// \param destroyer - the function to call to destroy elements
2144 /// \param useEHCleanup - whether to push an EH cleanup to destroy
2145 ///   the remaining elements in case the destruction of a single
2146 ///   element throws
2147 void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
2148                                        llvm::Value *end,
2149                                        QualType elementType,
2150                                        CharUnits elementAlign,
2151                                        Destroyer *destroyer,
2152                                        bool checkZeroLength,
2153                                        bool useEHCleanup) {
2154   assert(!elementType->isArrayType());
2155 
2156   // The basic structure here is a do-while loop, because we don't
2157   // need to check for the zero-element case.
2158   llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
2159   llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
2160 
2161   if (checkZeroLength) {
2162     llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
2163                                                 "arraydestroy.isempty");
2164     Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
2165   }
2166 
2167   // Enter the loop body, making that address the current address.
2168   llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2169   EmitBlock(bodyBB);
2170   llvm::PHINode *elementPast =
2171     Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
2172   elementPast->addIncoming(end, entryBB);
2173 
2174   // Shift the address back by one element.
2175   llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
2176   llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
2177                                                    "arraydestroy.element");
2178 
2179   if (useEHCleanup)
2180     pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
2181                                    destroyer);
2182 
2183   // Perform the actual destruction there.
2184   destroyer(*this, Address(element, elementAlign), elementType);
2185 
2186   if (useEHCleanup)
2187     PopCleanupBlock();
2188 
2189   // Check whether we've reached the end.
2190   llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
2191   Builder.CreateCondBr(done, doneBB, bodyBB);
2192   elementPast->addIncoming(element, Builder.GetInsertBlock());
2193 
2194   // Done.
2195   EmitBlock(doneBB);
2196 }
2197 
2198 /// Perform partial array destruction as if in an EH cleanup.  Unlike
2199 /// emitArrayDestroy, the element type here may still be an array type.
2200 static void emitPartialArrayDestroy(CodeGenFunction &CGF,
2201                                     llvm::Value *begin, llvm::Value *end,
2202                                     QualType type, CharUnits elementAlign,
2203                                     CodeGenFunction::Destroyer *destroyer) {
2204   // If the element type is itself an array, drill down.
2205   unsigned arrayDepth = 0;
2206   while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
2207     // VLAs don't require a GEP index to walk into.
2208     if (!isa<VariableArrayType>(arrayType))
2209       arrayDepth++;
2210     type = arrayType->getElementType();
2211   }
2212 
2213   if (arrayDepth) {
2214     llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
2215 
2216     SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
2217     begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
2218     end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
2219   }
2220 
2221   // Destroy the array.  We don't ever need an EH cleanup because we
2222   // assume that we're in an EH cleanup ourselves, so a throwing
2223   // destructor causes an immediate terminate.
2224   CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2225                        /*checkZeroLength*/ true, /*useEHCleanup*/ false);
2226 }
2227 
2228 namespace {
2229   /// RegularPartialArrayDestroy - a cleanup which performs a partial
2230   /// array destroy where the end pointer is regularly determined and
2231   /// does not need to be loaded from a local.
2232   class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2233     llvm::Value *ArrayBegin;
2234     llvm::Value *ArrayEnd;
2235     QualType ElementType;
2236     CodeGenFunction::Destroyer *Destroyer;
2237     CharUnits ElementAlign;
2238   public:
2239     RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
2240                                QualType elementType, CharUnits elementAlign,
2241                                CodeGenFunction::Destroyer *destroyer)
2242       : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
2243         ElementType(elementType), Destroyer(destroyer),
2244         ElementAlign(elementAlign) {}
2245 
2246     void Emit(CodeGenFunction &CGF, Flags flags) override {
2247       emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
2248                               ElementType, ElementAlign, Destroyer);
2249     }
2250   };
2251 
2252   /// IrregularPartialArrayDestroy - a cleanup which performs a
2253   /// partial array destroy where the end pointer is irregularly
2254   /// determined and must be loaded from a local.
2255   class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2256     llvm::Value *ArrayBegin;
2257     Address ArrayEndPointer;
2258     QualType ElementType;
2259     CodeGenFunction::Destroyer *Destroyer;
2260     CharUnits ElementAlign;
2261   public:
2262     IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
2263                                  Address arrayEndPointer,
2264                                  QualType elementType,
2265                                  CharUnits elementAlign,
2266                                  CodeGenFunction::Destroyer *destroyer)
2267       : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
2268         ElementType(elementType), Destroyer(destroyer),
2269         ElementAlign(elementAlign) {}
2270 
2271     void Emit(CodeGenFunction &CGF, Flags flags) override {
2272       llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
2273       emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
2274                               ElementType, ElementAlign, Destroyer);
2275     }
2276   };
2277 } // end anonymous namespace
2278 
2279 /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
2280 /// already-constructed elements of the given array.  The cleanup
2281 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2282 ///
2283 /// \param elementType - the immediate element type of the array;
2284 ///   possibly still an array type
2285 void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
2286                                                        Address arrayEndPointer,
2287                                                        QualType elementType,
2288                                                        CharUnits elementAlign,
2289                                                        Destroyer *destroyer) {
2290   pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
2291                                                     arrayBegin, arrayEndPointer,
2292                                                     elementType, elementAlign,
2293                                                     destroyer);
2294 }
2295 
2296 /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
2297 /// already-constructed elements of the given array.  The cleanup
2298 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2299 ///
2300 /// \param elementType - the immediate element type of the array;
2301 ///   possibly still an array type
2302 void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
2303                                                      llvm::Value *arrayEnd,
2304                                                      QualType elementType,
2305                                                      CharUnits elementAlign,
2306                                                      Destroyer *destroyer) {
2307   pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
2308                                                   arrayBegin, arrayEnd,
2309                                                   elementType, elementAlign,
2310                                                   destroyer);
2311 }
2312 
2313 /// Lazily declare the @llvm.lifetime.start intrinsic.
2314 llvm::Function *CodeGenModule::getLLVMLifetimeStartFn() {
2315   if (LifetimeStartFn)
2316     return LifetimeStartFn;
2317   LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
2318     llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy);
2319   return LifetimeStartFn;
2320 }
2321 
2322 /// Lazily declare the @llvm.lifetime.end intrinsic.
2323 llvm::Function *CodeGenModule::getLLVMLifetimeEndFn() {
2324   if (LifetimeEndFn)
2325     return LifetimeEndFn;
2326   LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
2327     llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy);
2328   return LifetimeEndFn;
2329 }
2330 
2331 namespace {
2332   /// A cleanup to perform a release of an object at the end of a
2333   /// function.  This is used to balance out the incoming +1 of a
2334   /// ns_consumed argument when we can't reasonably do that just by
2335   /// not doing the initial retain for a __block argument.
2336   struct ConsumeARCParameter final : EHScopeStack::Cleanup {
2337     ConsumeARCParameter(llvm::Value *param,
2338                         ARCPreciseLifetime_t precise)
2339       : Param(param), Precise(precise) {}
2340 
2341     llvm::Value *Param;
2342     ARCPreciseLifetime_t Precise;
2343 
2344     void Emit(CodeGenFunction &CGF, Flags flags) override {
2345       CGF.EmitARCRelease(Param, Precise);
2346     }
2347   };
2348 } // end anonymous namespace
2349 
2350 /// Emit an alloca (or GlobalValue depending on target)
2351 /// for the specified parameter and set up LocalDeclMap.
2352 void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
2353                                    unsigned ArgNo) {
2354   // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
2355   assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
2356          "Invalid argument to EmitParmDecl");
2357 
2358   Arg.getAnyValue()->setName(D.getName());
2359 
2360   QualType Ty = D.getType();
2361 
2362   // Use better IR generation for certain implicit parameters.
2363   if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) {
2364     // The only implicit argument a block has is its literal.
2365     // This may be passed as an inalloca'ed value on Windows x86.
2366     if (BlockInfo) {
2367       llvm::Value *V = Arg.isIndirect()
2368                            ? Builder.CreateLoad(Arg.getIndirectAddress())
2369                            : Arg.getDirectValue();
2370       setBlockContextParameter(IPD, ArgNo, V);
2371       return;
2372     }
2373   }
2374 
2375   Address DeclPtr = Address::invalid();
2376   bool DoStore = false;
2377   bool IsScalar = hasScalarEvaluationKind(Ty);
2378   // If we already have a pointer to the argument, reuse the input pointer.
2379   if (Arg.isIndirect()) {
2380     DeclPtr = Arg.getIndirectAddress();
2381     // If we have a prettier pointer type at this point, bitcast to that.
2382     unsigned AS = DeclPtr.getType()->getAddressSpace();
2383     llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS);
2384     if (DeclPtr.getType() != IRTy)
2385       DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName());
2386     // Indirect argument is in alloca address space, which may be different
2387     // from the default address space.
2388     auto AllocaAS = CGM.getASTAllocaAddressSpace();
2389     auto *V = DeclPtr.getPointer();
2390     auto SrcLangAS = getLangOpts().OpenCL ? LangAS::opencl_private : AllocaAS;
2391     auto DestLangAS =
2392         getLangOpts().OpenCL ? LangAS::opencl_private : LangAS::Default;
2393     if (SrcLangAS != DestLangAS) {
2394       assert(getContext().getTargetAddressSpace(SrcLangAS) ==
2395              CGM.getDataLayout().getAllocaAddrSpace());
2396       auto DestAS = getContext().getTargetAddressSpace(DestLangAS);
2397       auto *T = V->getType()->getPointerElementType()->getPointerTo(DestAS);
2398       DeclPtr = Address(getTargetHooks().performAddrSpaceCast(
2399                             *this, V, SrcLangAS, DestLangAS, T, true),
2400                         DeclPtr.getAlignment());
2401     }
2402 
2403     // Push a destructor cleanup for this parameter if the ABI requires it.
2404     // Don't push a cleanup in a thunk for a method that will also emit a
2405     // cleanup.
2406     if (hasAggregateEvaluationKind(Ty) && !CurFuncIsThunk &&
2407         Ty->getAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
2408       if (QualType::DestructionKind DtorKind = Ty.isDestructedType()) {
2409         assert((DtorKind == QualType::DK_cxx_destructor ||
2410                 DtorKind == QualType::DK_nontrivial_c_struct) &&
2411                "unexpected destructor type");
2412         pushDestroy(DtorKind, DeclPtr, Ty);
2413         CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] =
2414             EHStack.stable_begin();
2415       }
2416     }
2417   } else {
2418     // Check if the parameter address is controlled by OpenMP runtime.
2419     Address OpenMPLocalAddr =
2420         getLangOpts().OpenMP
2421             ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
2422             : Address::invalid();
2423     if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
2424       DeclPtr = OpenMPLocalAddr;
2425     } else {
2426       // Otherwise, create a temporary to hold the value.
2427       DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2428                               D.getName() + ".addr");
2429     }
2430     DoStore = true;
2431   }
2432 
2433   llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
2434 
2435   LValue lv = MakeAddrLValue(DeclPtr, Ty);
2436   if (IsScalar) {
2437     Qualifiers qs = Ty.getQualifiers();
2438     if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
2439       // We honor __attribute__((ns_consumed)) for types with lifetime.
2440       // For __strong, it's handled by just skipping the initial retain;
2441       // otherwise we have to balance out the initial +1 with an extra
2442       // cleanup to do the release at the end of the function.
2443       bool isConsumed = D.hasAttr<NSConsumedAttr>();
2444 
2445       // If a parameter is pseudo-strong then we can omit the implicit retain.
2446       if (D.isARCPseudoStrong()) {
2447         assert(lt == Qualifiers::OCL_Strong &&
2448                "pseudo-strong variable isn't strong?");
2449         assert(qs.hasConst() && "pseudo-strong variable should be const!");
2450         lt = Qualifiers::OCL_ExplicitNone;
2451       }
2452 
2453       // Load objects passed indirectly.
2454       if (Arg.isIndirect() && !ArgVal)
2455         ArgVal = Builder.CreateLoad(DeclPtr);
2456 
2457       if (lt == Qualifiers::OCL_Strong) {
2458         if (!isConsumed) {
2459           if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2460             // use objc_storeStrong(&dest, value) for retaining the
2461             // object. But first, store a null into 'dest' because
2462             // objc_storeStrong attempts to release its old value.
2463             llvm::Value *Null = CGM.EmitNullConstant(D.getType());
2464             EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
2465             EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
2466             DoStore = false;
2467           }
2468           else
2469           // Don't use objc_retainBlock for block pointers, because we
2470           // don't want to Block_copy something just because we got it
2471           // as a parameter.
2472             ArgVal = EmitARCRetainNonBlock(ArgVal);
2473         }
2474       } else {
2475         // Push the cleanup for a consumed parameter.
2476         if (isConsumed) {
2477           ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
2478                                 ? ARCPreciseLifetime : ARCImpreciseLifetime);
2479           EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
2480                                                    precise);
2481         }
2482 
2483         if (lt == Qualifiers::OCL_Weak) {
2484           EmitARCInitWeak(DeclPtr, ArgVal);
2485           DoStore = false; // The weak init is a store, no need to do two.
2486         }
2487       }
2488 
2489       // Enter the cleanup scope.
2490       EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
2491     }
2492   }
2493 
2494   // Store the initial value into the alloca.
2495   if (DoStore)
2496     EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
2497 
2498   setAddrOfLocalVar(&D, DeclPtr);
2499 
2500   // Emit debug info for param declaration.
2501   if (CGDebugInfo *DI = getDebugInfo()) {
2502     if (CGM.getCodeGenOpts().getDebugInfo() >=
2503         codegenoptions::LimitedDebugInfo) {
2504       DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder);
2505     }
2506   }
2507 
2508   if (D.hasAttr<AnnotateAttr>())
2509     EmitVarAnnotations(&D, DeclPtr.getPointer());
2510 
2511   // We can only check return value nullability if all arguments to the
2512   // function satisfy their nullability preconditions. This makes it necessary
2513   // to emit null checks for args in the function body itself.
2514   if (requiresReturnValueNullabilityCheck()) {
2515     auto Nullability = Ty->getNullability(getContext());
2516     if (Nullability && *Nullability == NullabilityKind::NonNull) {
2517       SanitizerScope SanScope(this);
2518       RetValNullabilityPrecondition =
2519           Builder.CreateAnd(RetValNullabilityPrecondition,
2520                             Builder.CreateIsNotNull(Arg.getAnyValue()));
2521     }
2522   }
2523 }
2524 
2525 void CodeGenModule::EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
2526                                             CodeGenFunction *CGF) {
2527   if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2528     return;
2529   getOpenMPRuntime().emitUserDefinedReduction(CGF, D);
2530 }
2531 
2532 void CodeGenModule::EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
2533                                             CodeGenFunction *CGF) {
2534   if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2535     return;
2536   // FIXME: need to implement mapper code generation
2537 }
2538 
2539 void CodeGenModule::EmitOMPRequiresDecl(const OMPRequiresDecl *D) {
2540   getOpenMPRuntime().checkArchForUnifiedAddressing(D);
2541 }
2542