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