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