clang 23.0.0git
CGDecl.cpp
Go to the documentation of this file.
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"
27#include "clang/AST/Attr.h"
28#include "clang/AST/CharUnits.h"
29#include "clang/AST/Decl.h"
30#include "clang/AST/DeclObjC.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())
116 DI->EmitAndRetainType(
117 getContext().getCanonicalTagType(cast<RecordDecl>(&D)));
118 return;
119 case Decl::Enum: // enum X;
120 if (CGDebugInfo *DI = getDebugInfo())
122 DI->EmitAndRetainType(
123 getContext().getCanonicalTagType(cast<EnumDecl>(&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(cast<NamespaceAliasDecl>(D));
150 return;
151 case Decl::Using: // using X; [C++]
152 if (CGDebugInfo *DI = getDebugInfo())
153 DI->EmitUsingDecl(cast<UsingDecl>(D));
154 return;
155 case Decl::UsingEnum: // using enum X; [C++]
156 if (CGDebugInfo *DI = getDebugInfo())
157 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(D));
158 return;
159 case Decl::UsingPack:
160 for (auto *Using : cast<UsingPackDecl>(D).expansions())
161 EmitDecl(*Using, /*EvaluateConditionDecl=*/EvaluateConditionDecl);
162 return;
163 case Decl::UsingDirective: // using namespace X; [C++]
164 if (CGDebugInfo *DI = getDebugInfo())
165 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
166 return;
167 case Decl::Var:
168 case Decl::Decomposition: {
169 const VarDecl &VD = cast<VarDecl>(D);
170 assert(VD.isLocalVarDecl() &&
171 "Should not see file-scope variables inside a function!");
172 EmitVarDecl(VD);
173 if (EvaluateConditionDecl)
175
176 return;
177 }
178
179 case Decl::OMPDeclareReduction:
180 return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this);
181
182 case Decl::OMPDeclareMapper:
183 return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this);
184
185 case Decl::OpenACCDeclare:
186 return CGM.EmitOpenACCDeclare(cast<OpenACCDeclareDecl>(&D), this);
187 case Decl::OpenACCRoutine:
188 return CGM.EmitOpenACCRoutine(cast<OpenACCRoutineDecl>(&D), this);
189
190 case Decl::Typedef: // typedef int X;
191 case Decl::TypeAlias: { // using X = int; [C++0x]
192 QualType Ty = cast<TypedefNameDecl>(D).getUnderlyingType();
193 if (CGDebugInfo *DI = getDebugInfo())
194 DI->EmitAndRetainType(Ty);
195 if (Ty->isVariablyModifiedType())
197 return;
198 }
199 }
200}
201
202/// EmitVarDecl - This method handles emission of any variable declaration
203/// inside a function, including static vars etc.
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(&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
228 return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*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(&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>(DC))
243 DC = cast<DeclContext>(CD->getNonClosureContext());
244 if (const auto *FD = dyn_cast<FunctionDecl>(DC))
245 ContextName = std::string(CGM.getMangledName(FD));
246 else if (const auto *BD = dyn_cast<BlockDecl>(DC))
247 ContextName = std::string(CGM.getBlockMangledName(GlobalDecl(), BD));
248 else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(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
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(&D));
273 else
274 Name = getStaticDeclName(*this, D);
275
276 llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty);
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;
284 D.hasAttr<CUDASharedAttr>() || D.hasAttr<LoaderUninitializedAttr>())
285 Init = llvm::UndefValue::get(LTy);
286 else
288
289 llvm::GlobalVariable *GV = new llvm::GlobalVariable(
290 getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name,
291 nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
292 GV->setAlignment(getContext().getDeclAlign(&D).getAsAlign());
293
294 if (supportsCOMDAT() && GV->isWeakForLinker())
295 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
296
297 if (D.getTLSKind())
298 setTLSMode(GV, D);
299
300 setGVProperties(GV, &D);
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) {
308 GV,
309 llvm::PointerType::get(getLLVMContext(),
310 getContext().getTargetAddressSpace(ExpectedAS)));
311 }
312
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>(D.getDeclContext());
318
319 // We can't name blocks or captured statements directly, so try to emit their
320 // parents.
321 if (isa<BlockDecl>(DC) || isa<CapturedDecl>(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>(DC))
330 GD = GlobalDecl(CD, Ctor_Base);
331 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
332 GD = GlobalDecl(DD, Dtor_Base);
333 else if (const auto *FD = dyn_cast<FunctionDecl>(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.
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 *
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(D.getInit(), "constant l-value expression");
364 else if (D.hasFlexibleArrayInit(getContext()))
365 CGM.ErrorUnsupported(D.getInit(), "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, GV, /*PerformInit*/true);
372 }
373 return GV;
374 }
375
376 PGO->markStmtMaybeUsed(D.getInit()); // FIXME: Too lazy
377
378#ifndef NDEBUG
379 CharUnits VarSize = CGM.getContext().getTypeSizeInChars(D.getType()) +
382 CGM.getDataLayout().getTypeAllocSize(Init->getType()));
383 assert(VarSize == CstSize && "Emitted constant has unexpected size");
384#endif
385
386 bool NeedsDtor =
388
389 GV->setConstant(
390 D.getType().isConstantStorage(getContext(), true, !NeedsDtor));
391 GV->replaceInitializer(Init);
392
393 emitter.finalize(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, GV, /*PerformInit*/false);
400 }
401
402 return GV;
403}
404
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);
412
413 // Store into LocalDeclMap before generating initializer to handle
414 // circular references.
415 llvm::Type *elemTy = ConvertTypeForMem(D.getType());
416 setAddrOfLocalVar(&D, 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.
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>(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) {
439 var = AddInitializerToStaticVarDecl(D, var);
440 }
441
442 var->setAlignment(alignment.getAsAlign());
443
444 if (D.hasAttr<AnnotateAttr>())
445 CGM.AddGlobalAnnotations(&D, var);
446
447 if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
448 var->addAttribute("bss-section", SA->getName());
449 if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
450 var->addAttribute("data-section", SA->getName());
451 if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
452 var->addAttribute("rodata-section", SA->getName());
453 if (auto *SA = D.getAttr<PragmaClangRelroSectionAttr>())
454 var->addAttribute("relro-section", 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(var);
461 else if (D.hasAttr<UsedAttr>())
462 CGM.addUsedOrCompilerUsedGlobal(var);
463
464 if (CGM.getCodeGenOpts().KeepPersistentStorageVariables)
465 CGM.addUsedOrCompilerUsedGlobal(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(var, expectedType);
474 LocalDeclMap.find(&D)->second = Address(castedAddr, elemTy, alignment);
475 CGM.setStaticLocalDeclAddress(&D, castedAddr);
476
477 CGM.getSanitizerMetadata()->reportGlobal(var, D);
478
479 // Emit global variable debug descriptor for static vars.
481 if (DI && CGM.getCodeGenOpts().hasReducedDebugInfo()) {
482 DI->setLocation(D.getLocation());
483 DI->EmitGlobalVariable(var, &D);
484 }
485}
486
487namespace {
488 struct DestroyObject final : EHScopeStack::Cleanup {
489 DestroyObject(Address addr, QualType type,
491 bool useEHCleanupForArray)
492 : addr(addr), type(type), destroyer(destroyer),
493 useEHCleanupForArray(useEHCleanupForArray) {}
494
495 Address addr;
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("nrvo.unused");
526 SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
527 llvm::Value *DidNRVO =
528 CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
529 CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
530 CGF.EmitBlock(RunDtorBB);
531 }
532
533 static_cast<Derived *>(this)->emitDestructorCall(CGF);
534
535 if (NRVO) CGF.EmitBlock(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) {
552 /*ForVirtualBase=*/false,
553 /*Delegating=*/false, Loc, 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(Stack);
573 CGF.Builder.CreateStackRestore(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(CGF.EmitDeclRefLValue(&DRE),
597 SourceLocation());
598 CGF.EmitExtendGCLifetime(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(&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(Addr, CGF.ConvertType(ArgTy));
628
629 CallArgList Args;
630 Args.add(RValue::get(Arg),
631 CGF.getContext().getPointerType(Var.getType()));
632 GlobalDecl GD = GlobalDecl(Attribute->getFunctionDecl());
633 auto Callee = CGCallee::forDirect(CleanupFn, CGCalleeInfo(GD));
634 CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args,
635 /*callOrInvoke*/ nullptr, /*IsMustTail*/ false,
636 Attribute->getLoc());
637 }
638 };
639} // end anonymous namespace
640
641/// EmitAutoVarWithLifetime - Does the setup required for an automatic
642/// variable with lifetime.
644 Address addr,
645 Qualifiers::ObjCLifetime lifetime) {
646 switch (lifetime) {
648 llvm_unreachable("present but none");
649
651 // nothing to do
652 break;
653
655 CodeGenFunction::Destroyer *destroyer =
656 (var.hasAttr<ObjCPreciseLifetimeAttr>()
659
660 CleanupKind cleanupKind = CGF.getARCCleanupKind();
661 CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
662 cleanupKind & EHCleanup);
663 break;
664 }
666 // nothing to do
667 break;
668
670 // __weak objects always get EH cleanups; otherwise, exceptions
671 // could cause really nasty crashes instead of mere leaks.
672 CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
674 /*useEHCleanup*/ true);
675 break;
676 }
677}
678
679static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
680 if (const Expr *e = dyn_cast<Expr>(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>(e))
686 return (ref->getDecl() == &var);
687 if (const BlockExpr *be = dyn_cast<BlockExpr>(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, 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>(decl)) return false;
707 const VarDecl *var = cast<VarDecl>(decl);
708 return isAccessedBy(*var, e);
709}
710
712 const LValue &destLV, const Expr *init) {
713 bool needsCast = false;
714
715 while (auto castExpr = dyn_cast<CastExpr>(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(srcExpr);
733
734 // Handle a formal type change to avoid asserting.
735 auto srcAddr = srcLV.getAddress();
736 if (needsCast) {
737 srcAddr = srcAddr.withElementType(destLV.getAddress().getElementType());
738 }
739
740 // If it was an l-value, use objc_copyWeak.
741 if (srcExpr->isLValue()) {
742 CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
743 } else {
744 assert(srcExpr->isXValue());
745 CGF.EmitARCMoveWeak(destLV.getAddress(), 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
761 LValue &lvalue,
762 const VarDecl *var) {
763 lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
764}
765
767 SourceLocation Loc) {
768 if (!SanOpts.has(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(RHS);
781 llvm::Constant *StaticData[] = {
783 llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
784 llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
785 EmitCheck({{IsNotNull, CheckOrdinal}}, CheckHandler, StaticData, RHS);
786}
787
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(PtrAuth, init, lvalue.getAddress());
795 lvalue.getQuals().removePointerAuth();
796 } else {
797 Value = EmitScalarExpr(init);
798 }
799 if (capturedByInit)
800 drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
801 EmitNullabilityCheck(lvalue, Value, init->getExprLoc());
803 return;
804 }
805
806 if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(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>(init)) {
813 return EmitScalarInit(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(D, 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.
830 cast<VarDecl>(D),
831 /*follow*/ false));
832 }
833
835 llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
836
837 // If __weak, we want to use a barrier under certain conditions.
838 if (lifetime == Qualifiers::OCL_Weak)
839 EmitARCInitWeak(tempLV.getAddress(), zero);
840
841 // Otherwise just do a simple store.
842 else
843 EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
844 }
845
846 // Emit the initializer.
847 llvm::Value *value = nullptr;
848
849 switch (lifetime) {
851 llvm_unreachable("present but none");
852
854 if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) {
855 value = EmitARCRetainScalarExpr(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
866 break;
867
869 // If it's not accessed by the initializer, try to emit the
870 // initialization with a copy or move.
871 if (!accessedByInit && tryEmitARCCopyWeakInit(*this, 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(init);
879
880 if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
881 if (accessedByInit)
882 EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
883 else
884 EmitARCInitWeak(lvalue.getAddress(), value);
885 return;
886 }
887
890 break;
891 }
892
893 if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
894
895 EmitNullabilityCheck(lvalue, value, 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, init->getExprLoc());
902 EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
904 return;
905 }
906
907 EmitStoreOfScalar(value, lvalue, /* isInitialization */ 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.
918 return true;
922 return Init->isNullValue() || NumStores--;
923
924 // See if we can emit each element.
926 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
927 llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
928 if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
929 return false;
930 }
931 return true;
932 }
933
934 if (llvm::ConstantDataSequential *CDS =
935 dyn_cast<llvm::ConstantDataSequential>(Init)) {
936 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
937 llvm::Constant *Elt = CDS->getElementAsConstant(i);
938 if (!canEmitInitWithFewStoresAfterBZero(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
959 auto *I = Builder.CreateStore(Init, Loc, isVolatile);
960 addInstToCurrentSourceAtom(I, nullptr);
961 if (IsAutoInit)
962 I->addAnnotationMetadata("auto-init");
963 return;
964 }
965
966 if (llvm::ConstantDataSequential *CDS =
967 dyn_cast<llvm::ConstantDataSequential>(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>(Elt))
973 emitStoresForInitAfterBZero(
974 Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile,
975 IsAutoInit);
976 }
977 return;
978 }
979
981 "Unknown value type!");
982
983 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
984 llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
985
986 // If necessary, get a pointer to the element and emit it.
987 if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
988 emitStoresForInitAfterBZero(Elt,
989 Builder.CreateConstInBoundsGEP2_32(Loc, 0, 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>(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 &&
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(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.
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(STy);
1062 llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1063 unsigned SizeSoFar = 0;
1065 bool NestedIntact = true;
1066 for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1067 unsigned CurOff = Layout->getElementOffset(i);
1068 if (SizeSoFar < CurOff) {
1069 assert(!STy->isPacked());
1070 auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar);
1071 Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1072 }
1073 llvm::Constant *CurOp;
1074 if (constant->isNullValue())
1075 CurOp = llvm::Constant::getNullValue(STy->getElementType(i));
1076 else
1077 CurOp = cast<llvm::Constant>(constant->getAggregateElement(i));
1078 auto *NewOp = constWithPadding(CGM, isPattern, CurOp);
1079 if (CurOp != NewOp)
1080 NestedIntact = false;
1081 Values.push_back(NewOp);
1082 SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType());
1083 }
1084 unsigned TotalSize = Layout->getSizeInBytes();
1085 if (SizeSoFar < TotalSize) {
1086 auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar);
1087 Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1088 }
1089 if (NestedIntact && Values.size() == STy->getNumElements())
1090 return constant;
1091 return llvm::ConstantStruct::getAnon(Values, 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>(OrigTy))
1100 return constStructWithPadding(CGM, isPattern, STy, constant);
1101 if (auto *ArrayTy = dyn_cast<llvm::ArrayType>(OrigTy)) {
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(ElemTy);
1111 PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1112 }
1113 for (unsigned Op = 0; Op != Size; ++Op) {
1114 if (!ZeroInitializer) {
1115 OpValue = constant->getAggregateElement(Op);
1116 PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1117 }
1118 Values.push_back(PaddedOp);
1119 }
1120 auto *NewElemTy = Values[0]->getType();
1121 if (NewElemTy == ElemTy)
1122 return constant;
1123 auto *NewArrayTy = llvm::ArrayType::get(NewElemTy, Size);
1124 return llvm::ConstantArray::get(NewArrayTy, 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
1133 llvm::Constant *Constant,
1134 CharUnits Align) {
1135 auto FunctionName = [&](const DeclContext *DC) -> std::string {
1136 if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
1137 if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD))
1138 return CC->getNameAsString();
1139 if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD))
1140 return CD->getNameAsString();
1141 return std::string(getMangledName(FD));
1142 } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) {
1143 return OM->getNameAsString();
1144 } else if (isa<BlockDecl>(DC)) {
1145 return "<block>";
1146 } else if (isa<CapturedDecl>(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 =
1162 std::string Name;
1163 if (D.hasGlobalStorage())
1164 Name = getMangledName(&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
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(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(constant, Loc, isVolatile);
1204 addInstToCurrentSourceAtom(I, nullptr);
1205 if (IsAutoInit)
1206 I->addAnnotationMetadata("auto-init");
1207 return;
1208 }
1209
1210 auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, 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(constant, ConstantSize)) {
1215 auto *I = Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0),
1216 SizeVal, isVolatile);
1217 addInstToCurrentSourceAtom(I, nullptr);
1218
1219 if (IsAutoInit)
1220 I->addAnnotationMetadata("auto-init");
1221
1222 bool valueAlreadyCorrect =
1223 constant->isNullValue() || isa<llvm::UndefValue>(constant);
1224 if (!valueAlreadyCorrect) {
1225 Loc = Loc.withElementType(Ty);
1226 emitStoresForInitAfterBZero(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(constant, ConstantSize, CGM.getDataLayout());
1234 if (Pattern) {
1235 uint64_t Value = 0x00;
1236 if (!isa<llvm::UndefValue>(Pattern)) {
1237 const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();
1238 assert(AP.getBitWidth() <= 8);
1239 Value = AP.getLimitedValue();
1240 }
1241 auto *I = Builder.CreateMemSet(
1242 Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal, isVolatile);
1243 addInstToCurrentSourceAtom(I, nullptr);
1244 if (IsAutoInit)
1245 I->addAnnotationMetadata("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() ==
1254 if (shouldSplitConstantStore(CGM, ConstantSize)) {
1255 if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1256 if (STy == Loc.getElementType() || IsTrivialAutoVarInitPattern) {
1257 const llvm::StructLayout *Layout =
1258 CGM.getDataLayout().getStructLayout(STy);
1259 for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1260 CharUnits CurOff =
1261 CharUnits::fromQuantity(Layout->getElementOffset(i));
1262 Address EltPtr = Builder.CreateConstInBoundsByteGEP(
1263 Loc.withElementType(CGM.Int8Ty), CurOff);
1264 emitStoresForConstant(D, EltPtr, isVolatile,
1265 constant->getAggregateElement(i), IsAutoInit);
1266 }
1267 return;
1268 }
1269 } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1270 if (ATy == Loc.getElementType() || IsTrivialAutoVarInitPattern) {
1271 for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1272 Address EltPtr = Builder.CreateConstGEP(
1273 Loc.withElementType(ATy->getElementType()), i);
1274 emitStoresForConstant(D, EltPtr, isVolatile,
1275 constant->getAggregateElement(i), IsAutoInit);
1276 }
1277 return;
1278 }
1279 }
1280 }
1281
1282 // Copy from a global.
1283 auto *I =
1284 Builder.CreateMemCpy(Loc,
1286 CGM, D, Builder, constant, Loc.getAlignment()),
1287 SizeVal, isVolatile);
1288 addInstToCurrentSourceAtom(I, nullptr);
1289
1290 if (IsAutoInit)
1291 I->addAnnotationMetadata("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::No, llvm::Constant::getNullValue(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(
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>(constant))
1316 return true;
1317 if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())
1318 for (llvm::Use &Op : constant->operands())
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>(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>(constant->getOperand(Op));
1336 Values[Op] = replaceUndef(CGM, isPattern, OpValue);
1337 }
1338 if (Ty->isStructTy())
1339 return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values);
1340 if (Ty->isArrayTy())
1341 return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values);
1342 assert(Ty->isVectorTy());
1343 return llvm::ConstantVector::get(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.
1350 AutoVarEmission emission = EmitAutoVarAlloca(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.
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(CGM.getLLVMLifetimeStartFn(), {Addr});
1365 C->setDoesNotThrow();
1366 return true;
1367}
1368
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(CGM.getLLVMLifetimeEndFn(), {Addr});
1377 C->setDoesNotThrow();
1378}
1379
1381 auto NL = ApplyDebugLocation::CreateEmpty(*this);
1382 llvm::Value *V = Builder.CreateLoad(Addr, "fake.use");
1383 llvm::CallInst *C = Builder.CreateCall(CGM.getLLVMFakeUseFn(), {V});
1384 C->setDoesNotThrow();
1385 C->setTailCallKind(llvm::CallInst::TCK_NoTail);
1386}
1387
1389 CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) {
1390 // For each dimension stores its QualType and corresponding
1391 // size-expression Value.
1394
1395 // Break down the array into individual dimensions.
1396 QualType Type1D = D.getType();
1397 while (getContext().getAsVariableArrayType(Type1D)) {
1398 auto VlaSize = getVLAElements1D(Type1D);
1399 if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1400 Dimensions.emplace_back(C, 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(Buffer);
1406 auto &Ident = getContext().Idents.getOwn(NameRef);
1407 VLAExprNames.push_back(&Ident);
1408 auto SizeExprAddr =
1409 CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef);
1410 Builder.CreateStore(VlaSize.NumElts, SizeExprAddr);
1411 Dimensions.emplace_back(SizeExprAddr.getPointer(),
1412 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>(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++];
1432 SizeTy->getScalarSizeInBits(), false);
1433 auto *ArtificialDecl = VarDecl::Create(
1434 getContext(), const_cast<DeclContext *>(D.getDeclContext()),
1435 D.getLocation(), D.getLocation(), NameIdent, QT,
1436 getContext().CreateTypeSourceInfo(QT), SC_Auto);
1437 ArtificialDecl->setImplicit();
1438
1439 MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts,
1440 Builder);
1441 }
1442 assert(MD && "No Size expression debug node created");
1443 DI->registerVLASizeExpression(VlaSize.Type, 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(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(Ty) > maxFakeUseAggregateSize(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.
1484 QualType Ty = D.getType();
1485 assert(
1488
1489 AutoVarEmission emission(D);
1490
1491 bool isEscapingByRef = D.isEscapingByref();
1492 emission.IsEscapingByRef = isEscapingByRef;
1493
1494 CharUnits alignment = getContext().getDeclAlign(&D);
1495
1496 // If the type is variably-modified, emit all the VLA sizes for it.
1497 if (Ty->isVariablyModifiedType())
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(*this, &D);
1508 else
1509 OpenMPLocalAddr =
1510 getLangOpts().OpenMP
1511 ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &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(getContext()) ||
1531 getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
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 =
1541 if ((!getLangOpts().OpenCL ||
1543 (CGM.getCodeGenOpts().MergeAllConstants && !NRVO &&
1544 !isEscapingByRef &&
1545 Ty.isConstantStorage(getContext(), true, !NeedsDtor))) {
1546 EmitStaticVarDecl(D, 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(*this),
1568 ReturnValue.getElementType(), ReturnValue.getAlignment());
1569 address = MaybeCastStackAddressSpace(AllocaAddr, Ty.getAddressSpace());
1570
1571 if (const auto *RD = Ty->getAsRecordDecl()) {
1572 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(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(Zero->getType(), CharUnits::One(), "nrvo");
1582 Builder.CreateStore(Zero, 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(&D);
1594 allocaTy = byrefInfo.Type;
1595 allocaAlignment = byrefInfo.ByrefAlignment;
1596 } else {
1597 allocaTy = ConvertTypeForMem(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(allocaTy, Ty.getAddressSpace(),
1605 allocaAlignment, D.getName(),
1606 /*ArraySize=*/nullptr, &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 =
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) &&
1632 emission.UseLifetimeMarkers =
1633 EmitLifetimeStart(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>(address.getBasePointer())) {
1642 llvm::LLVMContext &Ctx = Builder.getContext();
1643 auto *Operand = llvm::ConstantAsMetadata::get(Builder.getInt32(0));
1644 AI->setMetadata("stack-protector", llvm::MDNode::get(Ctx, {Operand}));
1645 }
1646
1647 std::optional<llvm::Attribute::AttrKind> Attr =
1648 CGM.StackProtectorAttribute(&D);
1649 if (Attr && (*Attr == llvm::Attribute::StackProtectReq)) {
1650 CGM.getDiags().Report(D.getLocation(),
1651 diag::warn_stack_protection_ignore_attribute);
1652 }
1653 }
1654 } else {
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(*this, &D)) {
1666 // Emit call to __kmpc_alloc_shared() instead of the alloca.
1667 std::pair<llvm::Value *, llvm::Value *> AddrSizePair =
1668 RT.getKmpcAllocShared(*this, &D);
1669
1670 // Save the address of the allocation:
1671 LValue Base = MakeAddrLValue(AddrSizePair.first, D.getType(),
1672 CGM.getContext().getDeclAlign(&D),
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(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 =
1691
1692 llvm::Value *V = Builder.CreateStackSave();
1693 assert(V->getType() == AllocaInt8PtrTy);
1694 Builder.CreateStore(V, 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.
1701 }
1702
1703 auto VlaSize = getVLASize(Ty);
1704 llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
1705
1706 // Allocate memory for the array.
1707 address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
1708 &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(&D, 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(&D, AllocaAddr.getPointer(), Builder,
1733 UsePointerValue);
1734 }
1735
1736 if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
1737 EmitVarAnnotations(&D, address.emitRawPointer(*this));
1738
1739 // Make sure we call @llvm.lifetime.end.
1740 if (emission.useLifetimeMarkers())
1741 EHStack.pushCleanup<CallLifetimeEnd>(
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() ==
1751 if (shouldExtendLifetime(getContext(), CurCodeDecl, D, CXXABIThisDecl))
1752 EHStack.pushCleanup<FakeUse>(NormalFakeUse,
1753 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>(S))
1765 return isCapturedBy(Var, E);
1766 for (const Stmt *SubStmt : S->children())
1767 if (isCapturedBy(Var, 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>(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>(E)) {
1791 const CompoundStmt *CS = SE->getSubStmt();
1792 for (const auto *BI : CS->body())
1793 if (const auto *BIE = dyn_cast<Expr>(BI)) {
1794 if (isCapturedBy(Var, BIE))
1795 return true;
1796 }
1797 else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1798 // special case declarations
1799 for (const auto *I : DS->decls()) {
1800 if (const auto *VD = dyn_cast<VarDecl>((I))) {
1801 const Expr *Init = VD->getInit();
1802 if (Init && isCapturedBy(Var, 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, 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.
1824 if (!Init)
1825 return true;
1826
1827 if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(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;
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(Loc.getElementType());
1852 switch (trivialAutoVarInit) {
1854 llvm_unreachable("Uninitialized handled by caller");
1856 if (CGM.stopAutoInit())
1857 return;
1858 if (trivialAutoVarInitMaxSize > 0 &&
1859 allocSize > trivialAutoVarInitMaxSize)
1860 return;
1861 emitStoresForZeroInit(D, Loc, isVolatile);
1862 break;
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(type);
1881 if (!VlaType)
1882 return;
1883 auto VlaSize = getVLASize(VlaType);
1884 auto SizeVal = VlaSize.NumElts;
1885 CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1886 switch (trivialAutoVarInit) {
1888 llvm_unreachable("Uninitialized handled by caller");
1889
1891 if (CGM.stopAutoInit())
1892 return;
1893 if (!EltSize.isOne())
1894 SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1895 auto *I = Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0),
1896 SizeVal, isVolatile);
1897 I->addAnnotationMetadata("auto-init");
1898 break;
1899 }
1900
1902 if (CGM.stopAutoInit())
1903 return;
1904 llvm::Type *ElTy = Loc.getElementType();
1905 llvm::Constant *Constant = constWithPadding(
1907 CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type);
1908 llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop");
1909 llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop");
1910 llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont");
1911 llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ(
1912 SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0),
1913 "vla.iszerosized");
1914 Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB);
1915 EmitBlock(SetupBB);
1916 if (!EltSize.isOne())
1917 SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1918 llvm::Value *BaseSizeInChars =
1919 llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity());
1920 Address Begin = Loc.withElementType(Int8Ty);
1921 llvm::Value *End = Builder.CreateInBoundsGEP(Begin.getElementType(),
1922 Begin.emitRawPointer(*this),
1923 SizeVal, "vla.end");
1924 llvm::BasicBlock *OriginBB = Builder.GetInsertBlock();
1925 EmitBlock(LoopBB);
1926 llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur");
1927 Cur->addIncoming(Begin.emitRawPointer(*this), OriginBB);
1928 CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize);
1929 auto *I =
1930 Builder.CreateMemCpy(Address(Cur, Int8Ty, CurAlign),
1932 CGM, D, Builder, Constant, ConstantAlign),
1933 BaseSizeInChars, isVolatile);
1934 I->addAnnotationMetadata("auto-init");
1935 llvm::Value *Next =
1936 Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next");
1937 llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone");
1938 Builder.CreateCondBr(Done, ContBB, LoopBB);
1939 Cur->addIncoming(Next, LoopBB);
1940 EmitBlock(ContBB);
1941 } break;
1942 }
1943}
1944
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;
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(Init)) {
1963 PGO->markStmtMaybeUsed(Init);
1964 return;
1965 }
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() ==
1979 LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type);
1980 if (emission.IsEscapingByRef)
1981 drillIntoBlockVariable(*this, Dst, &D);
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(D, Init);
1991
1992 bool locIsByrefHeader = !capturedByInit;
1993 const Address Loc =
1994 locIsByrefHeader ? emission.getObjectAddress(*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))
2005 : getContext().getLangOpts().getTrivialAutoVarInit());
2006
2007 auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) {
2008 if (trivialAutoVarInit ==
2010 return;
2011
2012 // Only initialize a __block's storage: we always initialize the header.
2013 if (emission.IsEscapingByRef && !locIsByrefHeader)
2014 Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false);
2015
2016 return emitZeroOrPatternForAutoVarInit(type, D, Loc);
2017 };
2018
2020 return initializeWhatIsTechnicallyUninitialized(Loc);
2021
2022 llvm::Constant *constant = nullptr;
2023 if (emission.IsConstantAggregate ||
2025 assert(!capturedByInit && "constant init contains a capturing block?");
2027 if (constant && !constant->isNullValue() &&
2028 (trivialAutoVarInit !=
2030 IsPattern isPattern =
2031 (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern)
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.
2041 replaceUndef(CGM, isPattern, constant));
2042 }
2043
2044 if (constant && type->isBitIntType() &&
2045 CGM.getTypes().typeRequiresSplitIntoByteArray(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(type, constant->getType());
2051 constant = llvm::ConstantFoldLoadFromConst(
2052 constant, LoadType, llvm::APInt::getZero(32), CGM.getDataLayout());
2053 }
2054 }
2055
2056 if (!constant) {
2057 if (trivialAutoVarInit !=
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(D, Init)) {
2068 initializeWhatIsTechnicallyUninitialized(Loc);
2069 }
2070 }
2071 LValue lv = MakeAddrLValue(Loc, type);
2072 lv.setNonGC(true);
2073 return EmitExprAsInit(Init, &D, lv, capturedByInit);
2074 }
2075
2076 PGO->markStmtMaybeUsed(Init);
2077
2078 if (!emission.IsConstantAggregate) {
2079 // For simple scalar/complex initialization, store the value directly.
2080 LValue lv = MakeAddrLValue(Loc, type);
2081 lv.setNonGC(true);
2082 return EmitStoreThroughLValue(RValue::get(constant), lv, true);
2083 }
2084
2085 emitStoresForConstant(D, Loc.withElementType(CGM.Int8Ty),
2086 type.isVolatileQualified(), constant,
2087 /*IsAutoInit=*/false);
2088}
2089
2091 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
2092 for (auto *B : DD->flat_bindings())
2093 if (auto *HD = B->getHoldingVar())
2094 EmitVarDecl(*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
2109 LValue lvalue, bool capturedByInit) {
2110 QualType type = D->getType();
2111
2112 if (type->isReferenceType()) {
2113 RValue rvalue = EmitReferenceBindingToExpr(init);
2114 if (capturedByInit)
2115 drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
2116 EmitStoreThroughLValue(rvalue, lvalue, true);
2117 return;
2118 }
2119 switch (getEvaluationKind(type)) {
2120 case TEK_Scalar:
2121 EmitScalarInit(init, D, lvalue, capturedByInit);
2122 return;
2123 case TEK_Complex: {
2124 ComplexPairTy complex = EmitComplexExpr(init);
2125 if (capturedByInit)
2126 drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
2127 EmitStoreOfComplex(complex, lvalue, /*init*/ true);
2128 return;
2129 }
2130 case TEK_Aggregate:
2131 if (type->isAtomicType()) {
2132 EmitAtomicInit(const_cast<Expr*>(init), lvalue);
2133 } else {
2135 if (isa<VarDecl>(D))
2137 else if (auto *FD = dyn_cast<FieldDecl>(D))
2138 Overlap = getOverlapForFieldInit(FD);
2139 // TODO: how can we delay here if D is captured by its initializer?
2140 EmitAggExpr(init,
2143 AggValueSlot::IsNotAliased, Overlap));
2144 }
2145 return;
2146 }
2147 llvm_unreachable("bad evaluation kind");
2148}
2149
2150/// Enter a destroy cleanup for the given local variable.
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(*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
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>(cleanupKind, addr, type, dtor,
2177 emission.NRVOFlag);
2178 return;
2179 }
2180 break;
2181
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>())
2192 break;
2193
2195 break;
2196
2199 if (emission.NRVOFlag) {
2200 assert(!type->isArrayType());
2201 EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr,
2202 emission.NRVOFlag, 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(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>(cleanupKind, addr, type, destroyer,
2215 useEHCleanup);
2216}
2217
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.
2232 // Check if we're in a SEH block with /EH, prevent it
2233 if (getLangOpts().CXXExceptions && currentFunctionUsesSEHTry())
2235 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>(NormalCleanup, &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(FD);
2250 assert(F && "Could not find function!");
2251
2252 const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
2253 EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D,
2254 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) {
2263 if (emission.Variable->getType().isObjCGCWeak())
2264 Flags |= BLOCK_FIELD_IS_WEAK;
2265 enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags,
2266 /*LoadBlockVarAddr*/ false,
2267 cxxDestructorCanThrow(emission.Variable->getType()));
2268 }
2269}
2270
2273 switch (kind) {
2274 case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
2276 return destroyCXXObject;
2280 return destroyARCWeak;
2283 }
2284 llvm_unreachable("Unknown DestructionKind");
2285}
2286
2287/// pushEHDestroy - Push the standard destructor for the given type as
2288/// an EH-only cleanup.
2290 Address addr, QualType type) {
2291 assert(dtorKind && "cannot push destructor for trivial type");
2292 assert(needsEHCleanup(dtorKind));
2293
2294 pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
2295}
2296
2297/// pushDestroy - Push the standard destructor for the given type as
2298/// at least a normal cleanup.
2300 Address addr, QualType type) {
2301 assert(dtorKind && "cannot push destructor for trivial type");
2302
2303 CleanupKind cleanupKind = getCleanupKind(dtorKind);
2304 pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
2305 cleanupKind & EHCleanup);
2306}
2307
2310 CleanupKind cleanupKind = getCleanupKind(dtorKind);
2311 pushLifetimeExtendedDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
2312 cleanupKind & EHCleanup);
2313}
2314
2316 QualType type, Destroyer *destroyer,
2317 bool useEHCleanupForArray) {
2318 pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
2319 useEHCleanupForArray);
2320}
2321
2322// Pushes a destroy and defers its deactivation until its
2323// CleanupDeactivationScope is exited.
2326 assert(dtorKind && "cannot push destructor for trivial type");
2327
2328 CleanupKind cleanupKind = getCleanupKind(dtorKind);
2330 cleanupKind, addr, type, getDestroyer(dtorKind), cleanupKind & EHCleanup);
2331}
2332
2334 CleanupKind cleanupKind, Address addr, QualType type, Destroyer *destroyer,
2335 bool useEHCleanupForArray) {
2336 llvm::Instruction *DominatingIP =
2337 Builder.CreateFlagLoad(llvm::Constant::getNullValue(Int8PtrTy));
2338 pushDestroy(cleanupKind, addr, type, destroyer, useEHCleanupForArray);
2340 {EHStack.stable_begin(), DominatingIP});
2341}
2342
2344 EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
2345}
2346
2348 CleanupKind Kind, std::pair<llvm::Value *, llvm::Value *> AddrSizePair) {
2349 EHStack.pushCleanup<KmpcAllocFree>(Kind, AddrSizePair);
2350}
2351
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.
2370 cleanupKind, Address::invalid(), addr, type, destroyer,
2371 useEHCleanupForArray);
2372 }
2373
2374 // Otherwise, we should only destroy the object if it's been initialized.
2375
2376 using ConditionalCleanupType =
2378 Destroyer *, bool>;
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
2387 cleanupKind, SavedAddr, type, destroyer, useEHCleanupForArray);
2388 initFullExprCleanupWithFlag(ActiveFlagForDeactivation);
2390 // Erase the active flag if the cleanup was not emitted.
2391 cleanup.AddAuxAllocas(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();
2400 cleanupKind, ActiveFlagForLifetimeExt, SavedAddr, type, destroyer,
2401 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
2416 Destroyer *destroyer,
2417 bool useEHCleanupForArray) {
2419 if (!arrayType)
2420 return destroyer(*this, addr, type);
2421
2422 llvm::Value *length = emitArrayLength(arrayType, type, addr);
2423
2424 CharUnits elementAlign =
2425 addr.getAlignment()
2426 .alignmentOfArrayElement(getContext().getTypeSizeInChars(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>(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(*this);
2439 llvm::Value *end =
2440 Builder.CreateInBoundsGEP(addr.getElementType(), begin, length);
2441 emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2442 checkZeroLength, 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
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("arraydestroy.body");
2467 llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
2468
2469 if (checkZeroLength) {
2470 llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
2471 "arraydestroy.isempty");
2472 Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
2473 }
2474
2475 // Enter the loop body, making that address the current address.
2476 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2477 EmitBlock(bodyBB);
2478 llvm::PHINode *elementPast =
2479 Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
2480 elementPast->addIncoming(end, entryBB);
2481
2482 // Shift the address back by one element.
2483 llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
2484 llvm::Type *llvmElementType = ConvertTypeForMem(elementType);
2485 llvm::Value *element = Builder.CreateInBoundsGEP(
2486 llvmElementType, elementPast, negativeOne, "arraydestroy.element");
2487
2488 if (useEHCleanup)
2489 pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
2490 destroyer);
2491
2492 // Perform the actual destruction there.
2493 destroyer(*this, Address(element, llvmElementType, elementAlign),
2494 elementType);
2495
2496 if (useEHCleanup)
2498
2499 // Check whether we've reached the end.
2500 llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
2501 Builder.CreateCondBr(done, doneBB, bodyBB);
2502 elementPast->addIncoming(element, Builder.GetInsertBlock());
2503
2504 // Done.
2505 EmitBlock(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.
2511 llvm::Value *begin, llvm::Value *end,
2512 QualType type, CharUnits elementAlign,
2513 CodeGenFunction::Destroyer *destroyer) {
2514 llvm::Type *elemTy = CGF.ConvertTypeForMem(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(type)) {
2519 // VLAs don't require a GEP index to walk into.
2521 arrayDepth++;
2522 type = arrayType->getElementType();
2523 }
2524
2525 if (arrayDepth) {
2526 llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
2527
2528 SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
2529 begin = CGF.Builder.CreateInBoundsGEP(
2530 elemTy, begin, gepIndices, "pad.arraybegin");
2531 end = CGF.Builder.CreateInBoundsGEP(
2532 elemTy, end, gepIndices, "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, 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, ArrayBegin, ArrayEnd,
2562 ElementType, ElementAlign, 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(ArrayEndPointer);
2587 emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
2588 ElementType, ElementAlign, 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
2600 Address arrayEndPointer,
2601 QualType elementType,
2602 CharUnits elementAlign,
2603 Destroyer *destroyer) {
2605 NormalAndEHCleanup, arrayBegin, arrayEndPointer, elementType,
2606 elementAlign, 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
2616 llvm::Value *arrayEnd,
2617 QualType elementType,
2618 CharUnits elementAlign,
2619 Destroyer *destroyer) {
2621 arrayBegin, arrayEnd,
2622 elementType, elementAlign,
2623 destroyer);
2624}
2625
2626/// Lazily declare the @llvm.lifetime.start intrinsic.
2628 if (LifetimeStartFn)
2629 return LifetimeStartFn;
2630 LifetimeStartFn = llvm::Intrinsic::getOrInsertDeclaration(
2631 &getModule(), llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy);
2632 return LifetimeStartFn;
2633}
2634
2635/// Lazily declare the @llvm.lifetime.end intrinsic.
2637 if (LifetimeEndFn)
2638 return LifetimeEndFn;
2639 LifetimeEndFn = llvm::Intrinsic::getOrInsertDeclaration(
2640 &getModule(), llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy);
2641 return LifetimeEndFn;
2642}
2643
2644/// Lazily declare the @llvm.fake.use intrinsic.
2646 if (FakeUseFn)
2647 return FakeUseFn;
2648 FakeUseFn = llvm::Intrinsic::getOrInsertDeclaration(
2649 &getModule(), 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(Param, 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.
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.
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>(&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(Arg.getIndirectAddress())
2697 : Arg.getDirectValue();
2698 setBlockContextParameter(IPD, ArgNo, 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(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(ConvertTypeForMem(Ty));
2717 auto *V = DeclPtr.emitRawPointer(*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(Ty);
2727 AllocaPtr = CreateMemTemp(PtrTy, getContext().getTypeAlignInChars(PtrTy),
2728 D.getName() + ".indirect_addr");
2729 EmitStoreOfScalar(V, AllocaPtr, /* Volatile */ false, PtrTy);
2730 }
2731
2732 LangAS DestLangAS = Ty.getAddressSpace();
2733 unsigned DestAS = getContext().getTargetAddressSpace(DestLangAS);
2734 if (DeclPtr.getAddressSpace() != DestAS) {
2735 auto *T = llvm::PointerType::get(getLLVMContext(), DestAS);
2736 DeclPtr = DeclPtr.withPointer(performAddrSpaceCast(V, T),
2737 DeclPtr.isKnownNonNull());
2738 }
2739
2740 // Push a destructor cleanup for this parameter if the ABI requires it.
2741 // Don't push a cleanup in a thunk for a method that will also emit a
2742 // cleanup.
2743 if (Ty->isRecordType() && !CurFuncIsThunk &&
2745 if (QualType::DestructionKind DtorKind =
2747 assert((DtorKind == QualType::DK_cxx_destructor ||
2748 DtorKind == QualType::DK_nontrivial_c_struct) &&
2749 "unexpected destructor type");
2750 pushDestroy(DtorKind, DeclPtr, Ty);
2751 CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] =
2752 EHStack.stable_begin();
2753 }
2754 }
2755 } else {
2756 // Check if the parameter address is controlled by OpenMP runtime.
2757 Address OpenMPLocalAddr =
2758 getLangOpts().OpenMP
2759 ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
2760 : Address::invalid();
2761 if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
2762 DeclPtr = OpenMPLocalAddr;
2763 AllocaPtr = DeclPtr;
2764 } else {
2765 // Otherwise, create a temporary to hold the value.
2766 DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2767 D.getName() + ".addr", &AllocaPtr);
2768 }
2769 DoStore = true;
2770 }
2771
2772 llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
2773
2774 LValue lv = MakeAddrLValue(DeclPtr, Ty);
2775 // If this is a thunk, don't bother with ARC lifetime management.
2776 // The true implementation will take care of that.
2777 if (IsScalar && !CurFuncIsThunk) {
2778 Qualifiers qs = Ty.getQualifiers();
2780 // We honor __attribute__((ns_consumed)) for types with lifetime.
2781 // For __strong, it's handled by just skipping the initial retain;
2782 // otherwise we have to balance out the initial +1 with an extra
2783 // cleanup to do the release at the end of the function.
2784 bool isConsumed = D.hasAttr<NSConsumedAttr>();
2785
2786 // If a parameter is pseudo-strong then we can omit the implicit retain.
2787 if (D.isARCPseudoStrong()) {
2788 assert(lt == Qualifiers::OCL_Strong &&
2789 "pseudo-strong variable isn't strong?");
2790 assert(qs.hasConst() && "pseudo-strong variable should be const!");
2792 }
2793
2794 // Load objects passed indirectly.
2795 if (Arg.isIndirect() && !ArgVal)
2796 ArgVal = Builder.CreateLoad(DeclPtr);
2797
2798 if (lt == Qualifiers::OCL_Strong) {
2799 if (!isConsumed) {
2800 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2801 // use objc_storeStrong(&dest, value) for retaining the
2802 // object. But first, store a null into 'dest' because
2803 // objc_storeStrong attempts to release its old value.
2804 llvm::Value *Null = CGM.EmitNullConstant(D.getType());
2805 EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
2806 EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
2807 DoStore = false;
2808 }
2809 else
2810 // Don't use objc_retainBlock for block pointers, because we
2811 // don't want to Block_copy something just because we got it
2812 // as a parameter.
2813 ArgVal = EmitARCRetainNonBlock(ArgVal);
2814 }
2815 } else {
2816 // Push the cleanup for a consumed parameter.
2817 if (isConsumed) {
2818 ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
2820 EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
2821 precise);
2822 }
2823
2824 if (lt == Qualifiers::OCL_Weak) {
2825 EmitARCInitWeak(DeclPtr, ArgVal);
2826 DoStore = false; // The weak init is a store, no need to do two.
2827 }
2828 }
2829
2830 // Enter the cleanup scope.
2831 EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
2832 }
2833 }
2834
2835 // Store the initial value into the alloca.
2836 if (DoStore)
2837 EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
2838
2839 setAddrOfLocalVar(&D, DeclPtr);
2840
2841 // Push a FakeUse 'cleanup' object onto the EHStack for the parameter,
2842 // which may be the 'this' pointer. This causes the emission of a fake.use
2843 // call with the parameter as argument at the end of the function.
2844 if (CGM.getCodeGenOpts().getExtendVariableLiveness() ==
2846 (CGM.getCodeGenOpts().getExtendVariableLiveness() ==
2848 &D == CXXABIThisDecl)) {
2849 // We don't emit fake uses for coroutine parameters, other than `this`.
2850 if (auto *FnDecl = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
2851 &D == CXXABIThisDecl || !FnDecl ||
2852 FnDecl->getBody()->getStmtClass() != Stmt::CoroutineBodyStmtClass) {
2853 if (shouldExtendLifetime(getContext(), CurCodeDecl, D, CXXABIThisDecl))
2854 EHStack.pushCleanup<FakeUse>(NormalFakeUse, DeclPtr);
2855 }
2856 }
2857
2858 // Emit debug info for param declarations in non-thunk functions.
2859 if (CGDebugInfo *DI = getDebugInfo()) {
2860 if (CGM.getCodeGenOpts().hasReducedDebugInfo() && !CurFuncIsThunk &&
2861 !NoDebugInfo) {
2862 llvm::DILocalVariable *DILocalVar = DI->EmitDeclareOfArgVariable(
2863 &D, AllocaPtr.getPointer(), ArgNo, Builder, UseIndirectDebugAddress);
2864 if (const auto *Var = dyn_cast_or_null<ParmVarDecl>(&D))
2865 DI->getParamDbgMappings().insert({Var, DILocalVar});
2866 }
2867 }
2868
2869 if (D.hasAttr<AnnotateAttr>())
2870 EmitVarAnnotations(&D, DeclPtr.emitRawPointer(*this));
2871
2872 // We can only check return value nullability if all arguments to the
2873 // function satisfy their nullability preconditions. This makes it necessary
2874 // to emit null checks for args in the function body itself.
2875 if (requiresReturnValueNullabilityCheck()) {
2876 auto Nullability = Ty->getNullability();
2877 if (Nullability && *Nullability == NullabilityKind::NonNull) {
2878 SanitizerScope SanScope(this);
2879 RetValNullabilityPrecondition =
2880 Builder.CreateAnd(RetValNullabilityPrecondition,
2881 Builder.CreateIsNotNull(Arg.getAnyValue()));
2882 }
2883 }
2884}
2885
2887 CodeGenFunction *CGF) {
2888 if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2889 return;
2891}
2892
2894 CodeGenFunction *CGF) {
2895 if (!LangOpts.OpenMP || LangOpts.OpenMPSimd ||
2896 (!LangOpts.EmitAllDecls && !D->isUsed()))
2897 return;
2899}
2900
2902 CodeGenFunction *CGF) {
2903 // This is a no-op, we cna just ignore these declarations.
2904}
2905
2907 CodeGenFunction *CGF) {
2908 // This is a no-op, we cna just ignore these declarations.
2909}
2910
2914
2916 for (const Expr *E : D->varlist()) {
2917 const auto *DE = cast<DeclRefExpr>(E);
2918 const auto *VD = cast<VarDecl>(DE->getDecl());
2919
2920 // Skip all but globals.
2921 if (!VD->hasGlobalStorage())
2922 continue;
2923
2924 // Check if the global has been materialized yet or not. If not, we are done
2925 // as any later generation will utilize the OMPAllocateDeclAttr. However, if
2926 // we already emitted the global we might have done so before the
2927 // OMPAllocateDeclAttr was attached, leading to the wrong address space
2928 // (potentially). While not pretty, common practise is to remove the old IR
2929 // global and generate a new one, so we do that here too. Uses are replaced
2930 // properly.
2931 StringRef MangledName = getMangledName(VD);
2932 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
2933 if (!Entry)
2934 continue;
2935
2936 // We can also keep the existing global if the address space is what we
2937 // expect it to be, if not, it is replaced.
2939 auto TargetAS = getContext().getTargetAddressSpace(GVAS);
2940 if (Entry->getType()->getAddressSpace() == TargetAS)
2941 continue;
2942
2943 llvm::PointerType *PTy = llvm::PointerType::get(getLLVMContext(), TargetAS);
2944
2945 // Replace all uses of the old global with a cast. Since we mutate the type
2946 // in place we neeed an intermediate that takes the spot of the old entry
2947 // until we can create the cast.
2948 llvm::GlobalVariable *DummyGV = new llvm::GlobalVariable(
2949 getModule(), Entry->getValueType(), false,
2950 llvm::GlobalValue::CommonLinkage, nullptr, "dummy", nullptr,
2951 llvm::GlobalVariable::NotThreadLocal, Entry->getAddressSpace());
2952 Entry->replaceAllUsesWith(DummyGV);
2953
2954 Entry->mutateType(PTy);
2955 llvm::Constant *NewPtrForOldDecl =
2956 llvm::ConstantExpr::getAddrSpaceCast(Entry, DummyGV->getType());
2957
2958 // Now we have a casted version of the changed global, the dummy can be
2959 // replaced and deleted.
2960 DummyGV->replaceAllUsesWith(NewPtrForOldDecl);
2961 DummyGV->eraseFromParent();
2962 }
2963}
2964
2965std::optional<CharUnits>
2967 if (const auto *AA = VD->getAttr<OMPAllocateDeclAttr>()) {
2968 if (Expr *Alignment = AA->getAlignment()) {
2969 unsigned UserAlign =
2970 Alignment->EvaluateKnownConstInt(getContext()).getExtValue();
2971 CharUnits NaturalAlign =
2973
2974 // OpenMP5.1 pg 185 lines 7-10
2975 // Each item in the align modifier list must be aligned to the maximum
2976 // of the specified alignment and the type's natural alignment.
2978 std::max<unsigned>(UserAlign, NaturalAlign.getQuantity()));
2979 }
2980 }
2981 return std::nullopt;
2982}
Defines the clang::ASTContext interface.
#define V(N, I)
static bool isCapturedBy(const VarDecl &, const Expr *)
Determines whether the given __block variable is potentially captured by the given expression.
Definition CGDecl.cpp:1774
static void emitPartialArrayDestroy(CodeGenFunction &CGF, llvm::Value *begin, llvm::Value *end, QualType type, CharUnits elementAlign, CodeGenFunction::Destroyer *destroyer)
Perform partial array destruction as if in an EH cleanup.
Definition CGDecl.cpp:2510
static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init, unsigned &NumStores)
Decide whether we can emit the non-zero parts of the specified initializer with equal or fewer than N...
Definition CGDecl.cpp:912
static llvm::Constant * patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern, llvm::Type *Ty)
Generate a constant filled with either a pattern or zeroes.
Definition CGDecl.cpp:1044
static llvm::Constant * constWithPadding(CodeGenModule &CGM, IsPattern isPattern, llvm::Constant *constant)
Replace all padding bytes in a given constant with either a pattern byte or 0x00.
Definition CGDecl.cpp:1096
static llvm::Value * shouldUseMemSetToInitialize(llvm::Constant *Init, uint64_t GlobalSize, const llvm::DataLayout &DL)
Decide whether we should use memset to initialize a local variable instead of using a memcpy from a c...
Definition CGDecl.cpp:1018
IsPattern
Definition CGDecl.cpp:1041
static bool shouldSplitConstantStore(CodeGenModule &CGM, uint64_t GlobalByteSize)
Decide whether we want to split a constant structure or array store into a sequence of its fields' st...
Definition CGDecl.cpp:1030
static llvm::Constant * replaceUndef(CodeGenModule &CGM, IsPattern isPattern, llvm::Constant *constant)
Definition CGDecl.cpp:1324
static bool shouldExtendLifetime(const ASTContext &Context, const Decl *FuncDecl, const VarDecl &D, ImplicitParamDecl *CXXABIThisDecl)
Definition CGDecl.cpp:1455
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, const LValue &destLV, const Expr *init)
Definition CGDecl.cpp:711
static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init, uint64_t GlobalSize)
Decide whether we should use bzero plus some stores to initialize a local variable instead of using a...
Definition CGDecl.cpp:997
static llvm::Constant * constStructWithPadding(CodeGenModule &CGM, IsPattern isPattern, llvm::StructType *STy, llvm::Constant *constant)
Helper function for constWithPadding() to deal with padding in structures.
Definition CGDecl.cpp:1056
static bool containsUndef(llvm::Constant *constant)
Definition CGDecl.cpp:1313
static uint64_t maxFakeUseAggregateSize(const ASTContext &C)
Return the maximum size of an aggregate for which we generate a fake use intrinsic when -fextend-vari...
Definition CGDecl.cpp:1449
static bool isAccessedBy(const VarDecl &var, const Stmt *s)
Definition CGDecl.cpp:679
static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var, Address addr, Qualifiers::ObjCLifetime lifetime)
EmitAutoVarWithLifetime - Does the setup required for an automatic variable with lifetime.
Definition CGDecl.cpp:643
static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM, const VarDecl &D, CGBuilderTy &Builder, llvm::Constant *Constant, CharUnits Align)
Definition CGDecl.cpp:1182
static void drillIntoBlockVariable(CodeGenFunction &CGF, LValue &lvalue, const VarDecl *var)
Definition CGDecl.cpp:760
static std::string getStaticDeclName(CIRGenModule &cgm, const VarDecl &d)
This file defines OpenACC nodes for declarative directives.
This file defines OpenMP nodes for declarative directives.
FormatToken * Next
The next token in the unwrapped line.
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
static const NamedDecl * getDefinition(const Decl *D)
__device__ __2f16 float __ockl_bool s
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:805
const LangOptions & getLangOpts() const
Definition ASTContext.h:959
QualType getIntTypeForBitwidth(unsigned DestWidth, unsigned Signed) const
getIntTypeForBitwidth - sets integer QualTy according to specified details: bitwidth,...
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
const VariableArrayType * getAsVariableArrayType(QualType T) const
DiagnosticsEngine & getDiagnostics() const
unsigned getTargetAddressSpace(LangAS AS) const
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3777
Attr - This represents one attribute.
Definition Attr.h:46
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4690
ArrayRef< Capture > captures() const
Definition Decl.h:4817
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition Expr.h:6671
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
Represents a C++ constructor within a class.
Definition DeclCXX.h:2620
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1381
Represents a C++ destructor within a class.
Definition DeclCXX.h:2882
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition CharUnits.h:214
bool isOne() const
isOne - Test whether the quantity equals one.
Definition CharUnits.h:125
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
llvm::Value * getBasePointer() const
Definition Address.h:198
static Address invalid()
Definition Address.h:176
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
CharUnits getAlignment() const
Definition Address.h:194
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition Address.h:209
Address withPointer(llvm::Value *NewPointer, KnownNonNull_t IsKnownNonNull) const
Return address with different pointer, but same element type and alignment.
Definition Address.h:261
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition Address.h:276
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition Address.h:215
KnownNonNull_t isKnownNonNull() const
Whether the pointer is known not to be null.
Definition Address.h:233
bool isValid() const
Definition Address.h:177
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition Address.h:204
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition CGValue.h:649
A scoped helper to set the current source atom group for CGDebugInfo::addInstToCurrentSourceAtom.
static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF, SourceLocation TemporaryLocation)
Apply TemporaryLocation if it is valid.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:118
llvm::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition CGBuilder.h:168
Address CreateInBoundsGEP(Address Addr, ArrayRef< llvm::Value * > IdxList, llvm::Type *ElementType, CharUnits Align, const Twine &Name="")
Definition CGBuilder.h:356
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:137
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition CGDebugInfo.h:59
void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl)
Emit information about a global variable.
llvm::DILocalVariable * EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, CGBuilderTy &Builder, const bool UsePointerValue=false)
Emit call to llvm.dbg.declare for an automatic variable declaration.
void setLocation(SourceLocation Loc)
Update the current source location.
void registerVLASizeExpression(QualType Ty, llvm::Metadata *SizeExpr)
Register VLA size expression debug node with the qualified type.
CGFunctionInfo - Class to encapsulate the information about a function definition.
const_arg_iterator arg_begin() const
Allows to disable automatic handling of functions used in target regions as those marked as omp decla...
virtual void getKmpcFreeShared(CodeGenFunction &CGF, const std::pair< llvm::Value *, llvm::Value * > &AddrSizePair)
Get call to __kmpc_free_shared.
void emitUserDefinedMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit the function for the user defined mapper construct.
virtual void processRequiresDirective(const OMPRequiresDecl *D)
Perform check on requires decl to ensure that target architecture supports unified addressing.
virtual void emitUserDefinedReduction(CodeGenFunction *CGF, const OMPDeclareReductionDecl *D)
Emit code for the specified user defined reduction construct.
void add(RValue rvalue, QualType type)
Definition CGCall.h:302
Address getAllocatedAddress() const
Returns the raw, allocated address, which is not necessarily the address of the object itself.
RawAddress getOriginalAllocatedAddress() const
Returns the address for the original alloca instruction.
Address getObjectAddress(CodeGenFunction &CGF) const
Returns the address of the object within this declaration.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, QualType elementType, CharUnits elementAlign, Destroyer *destroyer, bool checkZeroLength, bool useEHCleanup)
emitArrayDestroy - Destroys all the elements of the given array, beginning from last to first.
Definition CGDecl.cpp:2455
void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)
Emit code in this function to perform a guarded variable initialization.
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition CGObjC.cpp:2724
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition CGDecl.cpp:2415
AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD)
Determine whether a field initialization may overlap some other object.
void emitByrefStructureInit(const AutoVarEmission &emission)
Initialize the structural components of a __block variable, i.e.
llvm::Value * performAddrSpaceCast(llvm::Value *Src, llvm::Type *DestTy)
llvm::Value * EmitARCUnsafeUnretainedScalarExpr(const Expr *expr)
EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to immediately releasing the resut of Emi...
Definition CGObjC.cpp:3650
SanitizerSet SanOpts
Sanitizers enabled for this function.
void pushStackRestore(CleanupKind kind, Address SPMem)
Definition CGDecl.cpp:2343
llvm::DenseMap< const VarDecl *, llvm::Value * > NRVOFlags
A mapping from NRVO variables to the flags used to indicate when the NRVO has been applied to this va...
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2695
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
static bool hasScalarEvaluationKind(QualType T)
llvm::Type * ConvertType(QualType T)
void EmitFakeUse(Address Addr)
Definition CGDecl.cpp:1380
void pushEHDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushEHDestroy - Push the standard destructor for the given type as an EH-only cleanup.
Definition CGDecl.cpp:2289
llvm::Value * EmitPointerAuthQualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType ValueType, Address StorageAddress, bool IsKnownNonNull)
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
llvm::Value * EmitARCRetainAutoreleaseScalarExpr(const Expr *expr)
Definition CGObjC.cpp:3540
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
void EmitAtomicInit(Expr *E, LValue lvalue)
void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, llvm::Value *arrayEnd, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushRegularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the gi...
Definition CGDecl.cpp:2615
void EmitAutoVarDecl(const VarDecl &D)
EmitAutoVarDecl - Emit an auto variable declaration.
Definition CGDecl.cpp:1349
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition CGExpr.cpp:4035
void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, bool LoadBlockVarAddr, bool CanThrow)
Enter a cleanup to destroy a __block variable.
void EmitAutoVarInit(const AutoVarEmission &emission)
Definition CGDecl.cpp:1945
llvm::SmallVector< DeferredDeactivateCleanup > DeferredDeactivationCleanupStack
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
void addInstToCurrentSourceAtom(llvm::Instruction *KeyInstruction, llvm::Value *Backup)
See CGDebugInfo::addInstToCurrentSourceAtom.
const LangOptions & getLangOpts() const
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition CGExpr.cpp:697
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition CGDecl.cpp:1483
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushDestroy - Push the standard destructor for the given type as at least a normal cleanup.
Definition CGDecl.cpp:2299
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition CGDecl.cpp:788
void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc)
Given an assignment *LHS = RHS, emit a test that checks if RHS is nonnull, if LHS is marked _Nonnull.
Definition CGDecl.cpp:766
const CodeGen::CGBlockInfo * BlockInfo
@ TCK_NonnullAssign
Checking the value assigned to a _Nonnull pointer. Must not be null.
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
Definition CGClass.cpp:2635
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, Address arrayEndPointer, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushIrregularPartialArrayCleanup - Push a NormalAndEHCleanup to destroy already-constructed elements ...
Definition CGDecl.cpp:2599
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition CGDecl.cpp:2272
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition CGObjC.cpp:2513
DominatingValue< T >::saved_type saveValueInCond(T value)
static bool cxxDestructorCanThrow(QualType T)
Check if T is a C++ class that has a destructor that can throw.
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
Definition CGExpr.cpp:3925
void initFullExprCleanupWithFlag(RawAddress ActiveFlag)
void pushCleanupAndDeferDeactivation(CleanupKind Kind, As... A)
RawAddress CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
Definition CGExpr.cpp:181
const TargetInfo & getTarget() const
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition CGDecl.cpp:405
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we're currently emitting one branch or the other of a conditio...
void pushKmpcAllocFree(CleanupKind Kind, std::pair< llvm::Value *, llvm::Value * > AddrSizePair)
Definition CGDecl.cpp:2347
void pushDestroyAndDeferDeactivation(QualType::DestructionKind dtorKind, Address addr, QualType type)
Definition CGDecl.cpp:2324
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
void pushFullExprCleanup(CleanupKind kind, As... A)
pushFullExprCleanup - Push a cleanup to be run at the end of the current full-expression.
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs, const TrapReason *TR=nullptr)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition CGExpr.cpp:4183
void EmitExtendGCLifetime(llvm::Value *object)
EmitExtendGCLifetime - Given a pointer to an Objective-C object, make sure it survives garbage collec...
Definition CGObjC.cpp:3748
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Definition CGExpr.cpp:3592
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it's a VLA, and drill down to the base elem...
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(),...
Definition CGObjC.cpp:3525
bool EmitLifetimeStart(llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition CGDecl.cpp:1357
Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, bool followForward=true)
BuildBlockByrefAddress - Computes the location of the data in a variable which is declared as __block...
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
Definition CGExpr.cpp:158
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5359
void EmitLifetimeEnd(llvm::Value *Addr)
Definition CGDecl.cpp:1369
void pushCleanupAfterFullExprWithActiveFlag(CleanupKind Kind, RawAddress ActiveFlag, As... A)
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements,...
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
void emitAutoVarTypeCleanup(const AutoVarEmission &emission, QualType::DestructionKind dtorKind)
Enter a destroy cleanup for the given local variable.
Definition CGDecl.cpp:2151
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition CGDecl.cpp:2218
void EmitAndRegisterVariableArrayDimensions(CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo)
Emits the alloca and debug information for the size expressions for each dimension of an array.
Definition CGDecl.cpp:1388
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
Definition CGExpr.cpp:2772
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition CGDecl.cpp:2352
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Definition CGDecl.cpp:2674
Address ReturnValuePointer
ReturnValuePointer - The temporary alloca to hold a pointer to sret.
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind.
llvm::GlobalVariable * AddInitializerToStaticVarDecl(const VarDecl &D, llvm::GlobalVariable *GV)
AddInitializerToStaticVarDecl - Add the initializer for 'D' to the global variable that has already b...
Definition CGDecl.cpp:354
CleanupKind getCleanupKind(QualType::DestructionKind kind)
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition CGObjC.cpp:2369
llvm::Type * ConvertTypeForMem(QualType T)
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
RawAddress CreateMemTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition CGExpr.cpp:194
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
void EmitVarDecl(const VarDecl &D)
EmitVarDecl - Emit a local variable declaration.
Definition CGDecl.cpp:204
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition CGObjC.cpp:2556
const CGFunctionInfo * CurFnInfo
void EmitDecl(const Decl &D, bool EvaluateConditionDecl=false)
EmitDecl - Emit a declaration.
Definition CGDecl.cpp:52
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
Definition CGExpr.cpp:1712
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2683
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go.
llvm::LLVMContext & getLLVMContext()
void EmitARCCopyWeak(Address dst, Address src)
void @objc_copyWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition CGObjC.cpp:2733
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
void MaybeEmitDeferredVarDeclInit(const VarDecl *var)
Definition CGDecl.cpp:2090
bool isTrivialInitializer(const Expr *Init)
Determine whether the given initializer is trivial in the sense that it requires no code to be genera...
Definition CGDecl.cpp:1823
void PopCleanupBlock(bool FallThroughIsBranchThrough=false, bool ForDeactivation=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
bool hasLabelBeenSeenInCurrentScope() const
Return true if a label was seen in the current scope.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:643
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
EmitExprAsInit - Emits the code necessary to initialize a location in memory with the given initializ...
Definition CGDecl.cpp:2108
This class organizes the cross-function state that is used while generating LLVM code.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
llvm::Module & getModule() const
llvm::Constant * performAddrSpaceCast(llvm::Constant *Src, llvm::Type *DestTy)
void setStaticLocalDeclAddress(const VarDecl *D, llvm::Constant *C)
llvm::Function * getLLVMLifetimeStartFn()
Lazily declare the @llvm.lifetime.start intrinsic.
Definition CGDecl.cpp:2627
Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant, CharUnits Align)
Definition CGDecl.cpp:1132
void EmitOpenACCDeclare(const OpenACCDeclareDecl *D, CodeGenFunction *CGF=nullptr)
Definition CGDecl.cpp:2901
const LangOptions & getLangOpts() const
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
llvm::Function * getLLVMFakeUseFn()
Lazily declare the @llvm.fake.use intrinsic.
Definition CGDecl.cpp:2645
void EmitOMPAllocateDecl(const OMPAllocateDecl *D)
Emit a code for the allocate directive.
Definition CGDecl.cpp:2915
const llvm::DataLayout & getDataLayout() const
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition CGDecl.cpp:257
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition=NotForDefinition)
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D.
ASTContext & getContext() const
void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare mapper construct.
Definition CGDecl.cpp:2893
llvm::Function * getLLVMLifetimeEndFn()
Lazily declare the @llvm.lifetime.end intrinsic.
Definition CGDecl.cpp:2636
void EmitOMPRequiresDecl(const OMPRequiresDecl *D)
Emit a code for requires directive.
Definition CGDecl.cpp:2911
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
std::optional< CharUnits > getOMPAllocateAlignment(const VarDecl *VD)
Return the alignment specified in an allocate directive, if present.
Definition CGDecl.cpp:2966
llvm::LLVMContext & getLLVMContext()
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
Definition CGDecl.cpp:2886
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
LangAS GetGlobalVarAddressSpace(const VarDecl *D)
Return the AST address space of the underlying global variable for D, as determined by its declaratio...
void EmitOpenACCRoutine(const OpenACCRoutineDecl *D, CodeGenFunction *CGF=nullptr)
Definition CGDecl.cpp:2906
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
void finalize(llvm::GlobalVariable *global)
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
A cleanup scope which generates the cleanup blocks lazily.
Definition CGCleanup.h:250
ConditionalCleanup stores the saved form of its parameters, then restores them and performs the clean...
LValue - This represents an lvalue references.
Definition CGValue.h:183
llvm::Value * getPointer(CodeGenFunction &CGF) const
const Qualifiers & getQuals() const
Definition CGValue.h:350
Address getAddress() const
Definition CGValue.h:373
QualType getType() const
Definition CGValue.h:303
void setNonGC(bool Value)
Definition CGValue.h:316
void setAddress(Address address)
Definition CGValue.h:375
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition CGValue.h:305
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
static RValue get(llvm::Value *V)
Definition CGValue.h:99
An abstract representation of an aligned address.
Definition Address.h:42
llvm::Value * getPointer() const
Definition Address.h:66
static RawAddress invalid()
Definition Address.h:61
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
Definition TargetInfo.h:83
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1746
body_range body()
Definition Stmt.h:1809
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1273
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Definition DeclBase.cpp:341
T * getAttr() const
Definition DeclBase.h:581
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:601
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition DeclBase.h:1100
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks,...
SourceLocation getLocation() const
Definition DeclBase.h:447
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition DeclBase.cpp:576
DeclContext * getDeclContext()
Definition DeclBase.h:456
bool hasAttr() const
Definition DeclBase.h:585
Kind getKind() const
Definition DeclBase.h:450
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
This represents one expression.
Definition Expr.h:112
bool isXValue() const
Definition Expr.h:286
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3095
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3086
bool isConstantInitializer(ASTContext &Ctx, bool ForRef=false, const Expr **Culprit=nullptr) const
Returns true if this expression can be emitted to IR as a constant, and thus can be used as a constan...
Definition Expr.cpp:3347
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:284
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:277
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2018
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
const Decl * getDecl() const
Definition GlobalDecl.h:106
One of these records is kept for each identifier that is lexed.
IdentifierInfo & getOwn(StringRef Name)
Gets an IdentifierInfo for the given name without consulting external sources.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition Decl.h:317
bool isExternallyVisible() const
Definition Decl.h:433
This represents 'pragma omp allocate ...' directive.
Definition DeclOpenMP.h:536
varlist_range varlist()
Definition DeclOpenMP.h:578
This represents 'pragma omp declare mapper ...' directive.
Definition DeclOpenMP.h:349
This represents 'pragma omp declare reduction ...' directive.
Definition DeclOpenMP.h:239
This represents 'pragma omp requires...' directive.
Definition DeclOpenMP.h:479
Pointer-authentication qualifiers.
Definition TypeBase.h:152
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8520
@ PDIK_Struct
The type is a struct containing a field whose type is not PCK_Trivial.
Definition TypeBase.h:1489
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8562
bool isConstant(const ASTContext &Ctx) const
Definition TypeBase.h:1097
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8476
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition TypeBase.h:1449
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8621
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8530
bool isObjCGCWeak() const
true when Type is objc's weak.
Definition TypeBase.h:1439
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition TypeBase.h:1036
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition Type.cpp:2788
The collection of all-type qualifiers we support.
Definition TypeBase.h:331
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition TypeBase.h:361
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition TypeBase.h:354
@ OCL_None
There is no lifetime qualification on this type.
Definition TypeBase.h:350
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition TypeBase.h:364
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition TypeBase.h:367
bool hasConst() const
Definition TypeBase.h:457
void removePointerAuth()
Definition TypeBase.h:610
PointerAuthQualifier getPointerAuth() const
Definition TypeBase.h:603
ObjCLifetime getObjCLifetime() const
Definition TypeBase.h:545
bool isParamDestroyedInCallee() const
Definition Decl.h:4493
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
static const uint64_t MaximumAlignment
Definition Sema.h:1235
Encodes a location in the source.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition Expr.h:4598
Stmt - This represents one statement.
Definition Stmt.h:86
child_range children()
Definition Stmt.cpp:304
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition Type.cpp:2517
bool isArrayType() const
Definition TypeBase.h:8772
RecordDecl * castAsRecordDecl() const
Definition Type.h:48
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2855
bool isSamplerT() const
Definition TypeBase.h:8917
bool isRecordType() const
Definition TypeBase.h:8800
NullabilityKindOrNone getNullability() const
Determine the nullability of the given type.
Definition Type.cpp:5147
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition Decl.cpp:2130
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1582
TLSKind getTLSKind() const
Definition Decl.cpp:2147
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition Decl.cpp:2841
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1239
CharUnits getFlexibleArrayInitChars(const ASTContext &Ctx) const
If hasFlexibleArrayInit is true, compute the number of additional bytes necessary to store those elem...
Definition Decl.cpp:2856
bool mightBeUsableInConstantExpressions(const ASTContext &C) const
Determine whether this variable's value might be usable in a constant expression, according to the re...
Definition Decl.cpp:2465
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO).
Definition Decl.h:1525
bool isExceptionVariable() const
Determine whether this variable is the exception variable in a C++ catch statememt or an Objective-C ...
Definition Decl.h:1507
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition Decl.cpp:2830
const Expr * getInit() const
Definition Decl.h:1381
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1230
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition Decl.h:1560
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition Decl.h:1182
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1266
StorageDuration getStorageDuration() const
Get the storage duration of this variable, per C++ [basic.stc].
Definition Decl.h:1242
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
Definition Decl.cpp:2677
Defines the clang::TargetInfo interface.
@ BLOCK_FIELD_IS_BYREF
Definition CGBlocks.h:92
@ BLOCK_FIELD_IS_WEAK
Definition CGBlocks.h:94
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
llvm::Constant * initializationPatternFor(CodeGenModule &, llvm::Type *)
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
ARCPreciseLifetime_t
Does an ARC strong l-value have precise lifetime?
Definition CGValue.h:136
@ ARCImpreciseLifetime
Definition CGValue.h:137
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang's AST.
constexpr Variable var(Literal L)
Returns the variable of L.
Definition CNFFormula.h:64
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
The JSON file list parser is used to communicate input to InstallAPI.
@ Ctor_Base
Base object ctor.
Definition ABI.h:26
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
@ NonNull
Values of this type can never be null.
Definition Specifiers.h:351
@ SC_Auto
Definition Specifiers.h:257
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24
@ SD_Automatic
Automatic storage duration (most local variables).
Definition Specifiers.h:342
@ Dtor_Base
Base object dtor.
Definition ABI.h:37
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
LangAS
Defines the address space values used by the address space qualifier of QualType.
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:140
U cast(CodeGen::Address addr)
Definition Address.h:327
@ ThreadPrivateVar
Parameter for Thread private variable.
Definition Decl.h:1760
unsigned long uint64_t
float __ovld __cnfn length(float)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
static Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD)
Gets the OpenMP-specific address of the local variable /p VD.
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64