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