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
41using namespace clang;
42using namespace CodeGen;
43
44static_assert(clang::Sema::MaximumAlignment <= llvm::Value::MaximumAlignment,
45 "Clang max alignment greater than what LLVM supports?");
46
47void 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.
181void 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
211static 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
234llvm::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.
328llvm::GlobalVariable *
329CodeGenFunction::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
393void 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
470namespace {
471 struct DestroyObject final : EHScopeStack::Cleanup {
472 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
483 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 {
494 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
501 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> {
526 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
533 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> {
542 DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
543 : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {}
544
545 void emitDestructorCall(CodeGenFunction &CGF) {
546 CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
547 }
548 };
549
550 struct CallStackRestore final : EHScopeStack::Cleanup {
551 Address Stack;
552 CallStackRestore(Address Stack) : Stack(Stack) {}
553 bool isRedundantBeforeReturn() override { return true; }
554 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;
563 ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
564
565 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
581 CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
582 const VarDecl *Var)
583 : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
584
585 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.
613static 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
649static 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
674static 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
681static 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
731static void drillIntoBlockVariable(CodeGenFunction &CGF,
732 LValue &lvalue,
733 const VarDecl *var) {
734 lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(CGF), var));
735}
736
737void 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
758void 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.
877static 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.
915static 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.
962static 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.
983static 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.
995static 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
1006enum class IsPattern { No, Yes };
1007
1008/// Generate a constant filled with either a pattern or zeroes.
1009static 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
1017static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1018 llvm::Constant *constant);
1019
1020/// Helper function for constWithPadding() to deal with padding in structures.
1021static 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.
1061static 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
1097Address 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
1147static 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
1160static 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
1254static 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
1264static 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
1275static 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
1286static 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.
1311void 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
1320llvm::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
1336void 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
1346void 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.
1408CodeGenFunction::AutoVarEmission
1409CodeGenFunction::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
1624static bool isCapturedBy(const VarDecl &, const Expr *);
1625
1626/// Determines whether the given __block variable is potentially
1627/// captured by the given statement.
1628static 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.
1639static 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.
1688bool 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
1702void 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
1795void 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
1918void 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.
1961void 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
2028void 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
2075CodeGenFunction::Destroyer *
2076CodeGenFunction::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.
2093void 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.
2103void 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
2112void 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
2119void CodeGenFunction::pushStackRestore(CleanupKind Kind, Address SPMem) {
2120 EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
2121}
2122
2123void 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.