clang 17.0.0git
CGDeclCXX.cpp
Go to the documentation of this file.
1//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ 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 dealing with code generation of C++ declarations
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGCXXABI.h"
14#include "CGHLSLRuntime.h"
15#include "CGObjCRuntime.h"
16#include "CGOpenMPRuntime.h"
17#include "CodeGenFunction.h"
18#include "TargetInfo.h"
19#include "clang/AST/Attr.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/IR/MDBuilder.h"
24#include "llvm/Support/Path.h"
25
26using namespace clang;
27using namespace CodeGen;
28
29static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
30 ConstantAddress DeclPtr) {
31 assert(
32 (D.hasGlobalStorage() ||
33 (D.hasLocalStorage() && CGF.getContext().getLangOpts().OpenCLCPlusPlus)) &&
34 "VarDecl must have global or local (in the case of OpenCL) storage!");
35 assert(!D.getType()->isReferenceType() &&
36 "Should not call EmitDeclInit on a reference!");
37
38 QualType type = D.getType();
39 LValue lv = CGF.MakeAddrLValue(DeclPtr, type);
40
41 const Expr *Init = D.getInit();
42 switch (CGF.getEvaluationKind(type)) {
43 case TEK_Scalar: {
44 CodeGenModule &CGM = CGF.CGM;
45 if (lv.isObjCStrong())
47 DeclPtr, D.getTLSKind());
48 else if (lv.isObjCWeak())
50 DeclPtr);
51 else
52 CGF.EmitScalarInit(Init, &D, lv, false);
53 return;
54 }
55 case TEK_Complex:
56 CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
57 return;
58 case TEK_Aggregate:
59 CGF.EmitAggExpr(Init,
64 return;
65 }
66 llvm_unreachable("bad evaluation kind");
67}
68
69/// Emit code to cause the destruction of the given variable with
70/// static storage duration.
71static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
72 ConstantAddress Addr) {
73 // Honor __attribute__((no_destroy)) and bail instead of attempting
74 // to emit a reference to a possibly nonexistent destructor, which
75 // in turn can cause a crash. This will result in a global constructor
76 // that isn't balanced out by a destructor call as intended by the
77 // attribute. This also checks for -fno-c++-static-destructors and
78 // bails even if the attribute is not present.
80
81 // FIXME: __attribute__((cleanup)) ?
82
83 switch (DtorKind) {
85 return;
86
88 break;
89
93 // We don't care about releasing objects during process teardown.
94 assert(!D.getTLSKind() && "should have rejected this");
95 return;
96 }
97
98 llvm::FunctionCallee Func;
99 llvm::Constant *Argument;
100
101 CodeGenModule &CGM = CGF.CGM;
102 QualType Type = D.getType();
103
104 // Special-case non-array C++ destructors, if they have the right signature.
105 // Under some ABIs, destructors return this instead of void, and cannot be
106 // passed directly to __cxa_atexit if the target does not allow this
107 // mismatch.
108 const CXXRecordDecl *Record = Type->getAsCXXRecordDecl();
109 bool CanRegisterDestructor =
110 Record && (!CGM.getCXXABI().HasThisReturn(
111 GlobalDecl(Record->getDestructor(), Dtor_Complete)) ||
113 // If __cxa_atexit is disabled via a flag, a different helper function is
114 // generated elsewhere which uses atexit instead, and it takes the destructor
115 // directly.
116 bool UsingExternalHelper = !CGM.getCodeGenOpts().CXAAtExit;
117 if (Record && (CanRegisterDestructor || UsingExternalHelper)) {
118 assert(!Record->hasTrivialDestructor());
119 CXXDestructorDecl *Dtor = Record->getDestructor();
120
122 if (CGF.getContext().getLangOpts().OpenCL) {
123 auto DestAS =
125 auto DestTy = CGF.getTypes().ConvertType(Type)->getPointerTo(
126 CGM.getContext().getTargetAddressSpace(DestAS));
127 auto SrcAS = D.getType().getQualifiers().getAddressSpace();
128 if (DestAS == SrcAS)
129 Argument = llvm::ConstantExpr::getBitCast(Addr.getPointer(), DestTy);
130 else
131 // FIXME: On addr space mismatch we are passing NULL. The generation
132 // of the global destructor function should be adjusted accordingly.
133 Argument = llvm::ConstantPointerNull::get(DestTy);
134 } else {
135 Argument = llvm::ConstantExpr::getBitCast(
136 Addr.getPointer(), CGF.getTypes().ConvertType(Type)->getPointerTo());
137 }
138 // Otherwise, the standard logic requires a helper function.
139 } else {
140 Addr = Addr.getElementBitCast(CGF.ConvertTypeForMem(Type));
141 Func = CodeGenFunction(CGM)
142 .generateDestroyHelper(Addr, Type, CGF.getDestroyer(DtorKind),
143 CGF.needsEHCleanup(DtorKind), &D);
144 Argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
145 }
146
147 CGM.getCXXABI().registerGlobalDtor(CGF, D, Func, Argument);
148}
149
150/// Emit code to cause the variable at the given address to be considered as
151/// constant from this point onwards.
152static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
153 llvm::Constant *Addr) {
154 return CGF.EmitInvariantStart(
155 Addr, CGF.getContext().getTypeSizeInChars(D.getType()));
156}
157
158void CodeGenFunction::EmitInvariantStart(llvm::Constant *Addr, CharUnits Size) {
159 // Do not emit the intrinsic if we're not optimizing.
160 if (!CGM.getCodeGenOpts().OptimizationLevel)
161 return;
162
163 // Grab the llvm.invariant.start intrinsic.
164 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
165 // Overloaded address space type.
166 llvm::Type *ObjectPtr[1] = {Int8PtrTy};
167 llvm::Function *InvariantStart = CGM.getIntrinsic(InvStartID, ObjectPtr);
168
169 // Emit a call with the size in bytes of the object.
170 uint64_t Width = Size.getQuantity();
171 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(Int64Ty, Width),
172 llvm::ConstantExpr::getBitCast(Addr, Int8PtrTy)};
173 Builder.CreateCall(InvariantStart, Args);
174}
175
177 llvm::GlobalVariable *GV,
178 bool PerformInit) {
179
180 const Expr *Init = D.getInit();
181 QualType T = D.getType();
182
183 // The address space of a static local variable (DeclPtr) may be different
184 // from the address space of the "this" argument of the constructor. In that
185 // case, we need an addrspacecast before calling the constructor.
186 //
187 // struct StructWithCtor {
188 // __device__ StructWithCtor() {...}
189 // };
190 // __device__ void foo() {
191 // __shared__ StructWithCtor s;
192 // ...
193 // }
194 //
195 // For example, in the above CUDA code, the static local variable s has a
196 // "shared" address space qualifier, but the constructor of StructWithCtor
197 // expects "this" in the "generic" address space.
198 unsigned ExpectedAddrSpace = getTypes().getTargetAddressSpace(T);
199 unsigned ActualAddrSpace = GV->getAddressSpace();
200 llvm::Constant *DeclPtr = GV;
201 if (ActualAddrSpace != ExpectedAddrSpace) {
202 llvm::PointerType *PTy = llvm::PointerType::getWithSamePointeeType(
203 GV->getType(), ExpectedAddrSpace);
204 DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy);
205 }
206
207 ConstantAddress DeclAddr(
208 DeclPtr, GV->getValueType(), getContext().getDeclAlign(&D));
209
210 if (!T->isReferenceType()) {
211 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
212 D.hasAttr<OMPThreadPrivateDeclAttr>()) {
214 &D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
215 PerformInit, this);
216 }
217 bool NeedsDtor =
219 if (PerformInit)
220 EmitDeclInit(*this, D, DeclAddr);
221 if (CGM.isTypeConstant(D.getType(), true, !NeedsDtor))
222 EmitDeclInvariant(*this, D, DeclPtr);
223 else
224 EmitDeclDestroy(*this, D, DeclAddr);
225 return;
226 }
227
228 assert(PerformInit && "cannot have constant initializer which needs "
229 "destruction for reference");
231 EmitStoreOfScalar(RV.getScalarVal(), DeclAddr, false, T);
232}
233
234/// Create a stub function, suitable for being passed to atexit,
235/// which passes the given address to the given destructor function.
236llvm::Function *CodeGenFunction::createAtExitStub(const VarDecl &VD,
237 llvm::FunctionCallee dtor,
238 llvm::Constant *addr) {
239 // Get the destructor function type, void(*)(void).
240 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
241 SmallString<256> FnName;
242 {
243 llvm::raw_svector_ostream Out(FnName);
245 }
246
248 llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction(
249 ty, FnName.str(), FI, VD.getLocation());
250
251 CodeGenFunction CGF(CGM);
252
253 CGF.StartFunction(GlobalDecl(&VD, DynamicInitKind::AtExit),
255 VD.getLocation(), VD.getInit()->getExprLoc());
256 // Emit an artificial location for this function.
258
259 llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
260
261 // Make sure the call and the callee agree on calling convention.
262 if (auto *dtorFn = dyn_cast<llvm::Function>(
263 dtor.getCallee()->stripPointerCastsAndAliases()))
264 call->setCallingConv(dtorFn->getCallingConv());
265
266 CGF.FinishFunction();
267
268 return fn;
269}
270
271/// Create a stub function, suitable for being passed to __pt_atexit_np,
272/// which passes the given address to the given destructor function.
274 const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr,
275 llvm::FunctionCallee &AtExit) {
276 SmallString<256> FnName;
277 {
278 llvm::raw_svector_ostream Out(FnName);
280 }
281
283 getContext().IntTy, /*instanceMethod=*/false, /*chainCall=*/false,
285
286 // Get the stub function type, int(*)(int,...).
287 llvm::FunctionType *StubTy =
288 llvm::FunctionType::get(CGM.IntTy, {CGM.IntTy}, true);
289
290 llvm::Function *DtorStub = CGM.CreateGlobalInitOrCleanUpFunction(
291 StubTy, FnName.str(), FI, D.getLocation());
292
293 CodeGenFunction CGF(CGM);
294
295 FunctionArgList Args;
298 Args.push_back(&IPD);
299 QualType ResTy = CGM.getContext().IntTy;
300
301 CGF.StartFunction(GlobalDecl(&D, DynamicInitKind::AtExit), ResTy, DtorStub,
302 FI, Args, D.getLocation(), D.getInit()->getExprLoc());
303
304 // Emit an artificial location for this function.
306
307 llvm::CallInst *call = CGF.Builder.CreateCall(Dtor, Addr);
308
309 // Make sure the call and the callee agree on calling convention.
310 if (auto *DtorFn = dyn_cast<llvm::Function>(
311 Dtor.getCallee()->stripPointerCastsAndAliases()))
312 call->setCallingConv(DtorFn->getCallingConv());
313
314 // Return 0 from function
315 CGF.Builder.CreateStore(llvm::Constant::getNullValue(CGM.IntTy),
316 CGF.ReturnValue);
317
318 CGF.FinishFunction();
319
320 return DtorStub;
321}
322
323/// Register a global destructor using the C atexit runtime function.
325 llvm::FunctionCallee dtor,
326 llvm::Constant *addr) {
327 // Create a function which calls the destructor.
328 llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
330}
331
332void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) {
333 // extern "C" int atexit(void (*f)(void));
334 assert(dtorStub->getType() ==
335 llvm::PointerType::get(
336 llvm::FunctionType::get(CGM.VoidTy, false),
337 dtorStub->getType()->getPointerAddressSpace()) &&
338 "Argument to atexit has a wrong type.");
339
340 llvm::FunctionType *atexitTy =
341 llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
342
343 llvm::FunctionCallee atexit =
344 CGM.CreateRuntimeFunction(atexitTy, "atexit", llvm::AttributeList(),
345 /*Local=*/true);
346 if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit.getCallee()))
347 atexitFn->setDoesNotThrow();
348
349 EmitNounwindRuntimeCall(atexit, dtorStub);
350}
351
352llvm::Value *
354 // The unatexit subroutine unregisters __dtor functions that were previously
355 // registered by the atexit subroutine. If the referenced function is found,
356 // it is removed from the list of functions that are called at normal program
357 // termination and the unatexit returns a value of 0, otherwise a non-zero
358 // value is returned.
359 //
360 // extern "C" int unatexit(void (*f)(void));
361 assert(dtorStub->getType() ==
362 llvm::PointerType::get(
363 llvm::FunctionType::get(CGM.VoidTy, false),
364 dtorStub->getType()->getPointerAddressSpace()) &&
365 "Argument to unatexit has a wrong type.");
366
367 llvm::FunctionType *unatexitTy =
368 llvm::FunctionType::get(IntTy, {dtorStub->getType()}, /*isVarArg=*/false);
369
370 llvm::FunctionCallee unatexit =
371 CGM.CreateRuntimeFunction(unatexitTy, "unatexit", llvm::AttributeList());
372
373 cast<llvm::Function>(unatexit.getCallee())->setDoesNotThrow();
374
375 return EmitNounwindRuntimeCall(unatexit, dtorStub);
376}
377
379 llvm::GlobalVariable *DeclPtr,
380 bool PerformInit) {
381 // If we've been asked to forbid guard variables, emit an error now.
382 // This diagnostic is hard-coded for Darwin's use case; we can find
383 // better phrasing if someone else needs it.
384 if (CGM.getCodeGenOpts().ForbidGuardVariables)
386 "this initialization requires a guard variable, which "
387 "the kernel does not support");
388
389 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
390}
391
392void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value *NeedsInit,
393 llvm::BasicBlock *InitBlock,
394 llvm::BasicBlock *NoInitBlock,
395 GuardKind Kind,
396 const VarDecl *D) {
397 assert((Kind == GuardKind::TlsGuard || D) && "no guarded variable");
398
399 // A guess at how many times we will enter the initialization of a
400 // variable, depending on the kind of variable.
401 static const uint64_t InitsPerTLSVar = 1024;
402 static const uint64_t InitsPerLocalVar = 1024 * 1024;
403
404 llvm::MDNode *Weights;
405 if (Kind == GuardKind::VariableGuard && !D->isLocalVarDecl()) {
406 // For non-local variables, don't apply any weighting for now. Due to our
407 // use of COMDATs, we expect there to be at most one initialization of the
408 // variable per DSO, but we have no way to know how many DSOs will try to
409 // initialize the variable.
410 Weights = nullptr;
411 } else {
412 uint64_t NumInits;
413 // FIXME: For the TLS case, collect and use profiling information to
414 // determine a more accurate brach weight.
415 if (Kind == GuardKind::TlsGuard || D->getTLSKind())
416 NumInits = InitsPerTLSVar;
417 else
418 NumInits = InitsPerLocalVar;
419
420 // The probability of us entering the initializer is
421 // 1 / (total number of times we attempt to initialize the variable).
422 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
423 Weights = MDHelper.createBranchWeights(1, NumInits - 1);
424 }
425
426 Builder.CreateCondBr(NeedsInit, InitBlock, NoInitBlock, Weights);
427}
428
430 llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI,
431 SourceLocation Loc, bool TLS, llvm::GlobalVariable::LinkageTypes Linkage) {
432 llvm::Function *Fn = llvm::Function::Create(FTy, Linkage, Name, &getModule());
433
434 if (!getLangOpts().AppleKext && !TLS) {
435 // Set the section if needed.
436 if (const char *Section = getTarget().getStaticInitSectionSpecifier())
437 Fn->setSection(Section);
438 }
439
440 if (Linkage == llvm::GlobalVariable::InternalLinkage)
442
443 Fn->setCallingConv(getRuntimeCC());
444
445 if (!getLangOpts().Exceptions)
446 Fn->setDoesNotThrow();
447
448 if (getLangOpts().Sanitize.has(SanitizerKind::Address) &&
449 !isInNoSanitizeList(SanitizerKind::Address, Fn, Loc))
450 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
451
452 if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) &&
453 !isInNoSanitizeList(SanitizerKind::KernelAddress, Fn, Loc))
454 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
455
456 if (getLangOpts().Sanitize.has(SanitizerKind::HWAddress) &&
457 !isInNoSanitizeList(SanitizerKind::HWAddress, Fn, Loc))
458 Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
459
460 if (getLangOpts().Sanitize.has(SanitizerKind::KernelHWAddress) &&
461 !isInNoSanitizeList(SanitizerKind::KernelHWAddress, Fn, Loc))
462 Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
463
464 if (getLangOpts().Sanitize.has(SanitizerKind::MemtagStack) &&
465 !isInNoSanitizeList(SanitizerKind::MemtagStack, Fn, Loc))
466 Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
467
468 if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
469 !isInNoSanitizeList(SanitizerKind::Thread, Fn, Loc))
470 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
471
472 if (getLangOpts().Sanitize.has(SanitizerKind::Memory) &&
473 !isInNoSanitizeList(SanitizerKind::Memory, Fn, Loc))
474 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
475
476 if (getLangOpts().Sanitize.has(SanitizerKind::KernelMemory) &&
477 !isInNoSanitizeList(SanitizerKind::KernelMemory, Fn, Loc))
478 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
479
480 if (getLangOpts().Sanitize.has(SanitizerKind::SafeStack) &&
481 !isInNoSanitizeList(SanitizerKind::SafeStack, Fn, Loc))
482 Fn->addFnAttr(llvm::Attribute::SafeStack);
483
484 if (getLangOpts().Sanitize.has(SanitizerKind::ShadowCallStack) &&
485 !isInNoSanitizeList(SanitizerKind::ShadowCallStack, Fn, Loc))
486 Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
487
488 return Fn;
489}
490
491/// Create a global pointer to a function that will initialize a global
492/// variable. The user has requested that this pointer be emitted in a specific
493/// section.
494void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
495 llvm::GlobalVariable *GV,
496 llvm::Function *InitFunc,
497 InitSegAttr *ISA) {
498 llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
499 TheModule, InitFunc->getType(), /*isConstant=*/true,
500 llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
501 PtrArray->setSection(ISA->getSection());
502 addUsedGlobal(PtrArray);
503
504 // If the GV is already in a comdat group, then we have to join it.
505 if (llvm::Comdat *C = GV->getComdat())
506 PtrArray->setComdat(C);
507}
508
509void
510CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
511 llvm::GlobalVariable *Addr,
512 bool PerformInit) {
513
514 // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
515 // __constant__ and __shared__ variables defined in namespace scope,
516 // that are of class type, cannot have a non-empty constructor. All
517 // the checks have been done in Sema by now. Whatever initializers
518 // are allowed are empty and we just need to ignore them here.
519 if (getLangOpts().CUDAIsDevice && !getLangOpts().GPUAllowDeviceInit &&
520 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
521 D->hasAttr<CUDASharedAttr>()))
522 return;
523
524 if (getLangOpts().OpenMP &&
525 getOpenMPRuntime().emitDeclareTargetVarDefinition(D, Addr, PerformInit))
526 return;
527
528 // Check if we've already initialized this decl.
529 auto I = DelayedCXXInitPosition.find(D);
530 if (I != DelayedCXXInitPosition.end() && I->second == ~0U)
531 return;
532
533 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
534 SmallString<256> FnName;
535 {
536 llvm::raw_svector_ostream Out(FnName);
538 }
539
540 // Create a variable initialization function.
541 llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
542 FTy, FnName.str(), getTypes().arrangeNullaryFunction(), D->getLocation());
543
544 auto *ISA = D->getAttr<InitSegAttr>();
546 PerformInit);
547
548 llvm::GlobalVariable *COMDATKey =
549 supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr;
550
551 if (D->getTLSKind()) {
552 // FIXME: Should we support init_priority for thread_local?
553 // FIXME: We only need to register one __cxa_thread_atexit function for the
554 // entire TU.
555 CXXThreadLocalInits.push_back(Fn);
556 CXXThreadLocalInitVars.push_back(D);
557 } else if (PerformInit && ISA) {
558 // Contract with backend that "init_seg(compiler)" corresponds to priority
559 // 200 and "init_seg(lib)" corresponds to priority 400.
560 int Priority = -1;
561 if (ISA->getSection() == ".CRT$XCC")
562 Priority = 200;
563 else if (ISA->getSection() == ".CRT$XCL")
564 Priority = 400;
565
566 if (Priority != -1)
567 AddGlobalCtor(Fn, Priority, ~0U, COMDATKey);
568 else
569 EmitPointerToInitFunc(D, Addr, Fn, ISA);
570 } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
571 OrderGlobalInitsOrStermFinalizers Key(IPA->getPriority(),
572 PrioritizedCXXGlobalInits.size());
573 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
575 getContext().GetGVALinkageForVariable(D) == GVA_DiscardableODR ||
576 D->hasAttr<SelectAnyAttr>()) {
577 // C++ [basic.start.init]p2:
578 // Definitions of explicitly specialized class template static data
579 // members have ordered initialization. Other class template static data
580 // members (i.e., implicitly or explicitly instantiated specializations)
581 // have unordered initialization.
582 //
583 // As a consequence, we can put them into their own llvm.global_ctors entry.
584 //
585 // If the global is externally visible, put the initializer into a COMDAT
586 // group with the global being initialized. On most platforms, this is a
587 // minor startup time optimization. In the MS C++ ABI, there are no guard
588 // variables, so this COMDAT key is required for correctness.
589 //
590 // SelectAny globals will be comdat-folded. Put the initializer into a
591 // COMDAT group associated with the global, so the initializers get folded
592 // too.
593 I = DelayedCXXInitPosition.find(D);
594 // CXXGlobalInits.size() is the lex order number for the next deferred
595 // VarDecl. Use it when the current VarDecl is non-deferred. Although this
596 // lex order number is shared between current VarDecl and some following
597 // VarDecls, their order of insertion into `llvm.global_ctors` is the same
598 // as the lexing order and the following stable sort would preserve such
599 // order.
600 unsigned LexOrder =
601 I == DelayedCXXInitPosition.end() ? CXXGlobalInits.size() : I->second;
602 AddGlobalCtor(Fn, 65535, LexOrder, COMDATKey);
603 if (COMDATKey && (getTriple().isOSBinFormatELF() ||
604 getTarget().getCXXABI().isMicrosoft())) {
605 // When COMDAT is used on ELF or in the MS C++ ABI, the key must be in
606 // llvm.used to prevent linker GC.
607 addUsedGlobal(COMDATKey);
608 }
609
610 // If we used a COMDAT key for the global ctor, the init function can be
611 // discarded if the global ctor entry is discarded.
612 // FIXME: Do we need to restrict this to ELF and Wasm?
613 llvm::Comdat *C = Addr->getComdat();
614 if (COMDATKey && C &&
615 (getTarget().getTriple().isOSBinFormatELF() ||
616 getTarget().getTriple().isOSBinFormatWasm())) {
617 Fn->setComdat(C);
618 }
619 } else {
620 I = DelayedCXXInitPosition.find(D); // Re-do lookup in case of re-hash.
621 if (I == DelayedCXXInitPosition.end()) {
622 CXXGlobalInits.push_back(Fn);
623 } else if (I->second != ~0U) {
624 assert(I->second < CXXGlobalInits.size() &&
625 CXXGlobalInits[I->second] == nullptr);
626 CXXGlobalInits[I->second] = Fn;
627 }
628 }
629
630 // Remember that we already emitted the initializer for this global.
631 DelayedCXXInitPosition[D] = ~0U;
632}
633
634void CodeGenModule::EmitCXXThreadLocalInitFunc() {
636 *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
637
638 CXXThreadLocalInits.clear();
639 CXXThreadLocalInitVars.clear();
640 CXXThreadLocals.clear();
641}
642
643/* Build the initializer for a C++20 module:
644 This is arranged to be run only once regardless of how many times the module
645 might be included transitively. This arranged by using a guard variable.
646
647 If there are no initalizers at all (and also no imported modules) we reduce
648 this to an empty function (since the Itanium ABI requires that this function
649 be available to a caller, which might be produced by a different
650 implementation).
651
652 First we call any initializers for imported modules.
653 We then call initializers for the Global Module Fragment (if present)
654 We then call initializers for the current module.
655 We then call initializers for the Private Module Fragment (if present)
656*/
657
658void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
659 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
660 CXXGlobalInits.pop_back();
661
662 // As noted above, we create the function, even if it is empty.
663 // Module initializers for imported modules are emitted first.
664
665 // Collect all the modules that we import
666 SmallVector<Module *> AllImports;
667 // Ones that we export
668 for (auto I : Primary->Exports)
669 AllImports.push_back(I.getPointer());
670 // Ones that we only import.
671 for (Module *M : Primary->Imports)
672 AllImports.push_back(M);
673
675 for (Module *M : AllImports) {
676 // No Itanium initializer in header like modules.
677 if (M->isHeaderLikeModule())
678 continue; // TODO: warn of mixed use of module map modules and C++20?
679 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
680 SmallString<256> FnName;
681 {
682 llvm::raw_svector_ostream Out(FnName);
683 cast<ItaniumMangleContext>(getCXXABI().getMangleContext())
684 .mangleModuleInitializer(M, Out);
685 }
686 assert(!GetGlobalValue(FnName.str()) &&
687 "We should only have one use of the initializer call");
688 llvm::Function *Fn = llvm::Function::Create(
689 FTy, llvm::Function::ExternalLinkage, FnName.str(), &getModule());
690 ModuleInits.push_back(Fn);
691 }
692
693 // Add any initializers with specified priority; this uses the same approach
694 // as EmitCXXGlobalInitFunc().
695 if (!PrioritizedCXXGlobalInits.empty()) {
696 SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
697 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
698 PrioritizedCXXGlobalInits.end());
700 I = PrioritizedCXXGlobalInits.begin(),
701 E = PrioritizedCXXGlobalInits.end();
702 I != E;) {
704 std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
705
706 for (; I < PrioE; ++I)
707 ModuleInits.push_back(I->second);
708 }
709 }
710
711 // Now append the ones without specified priority.
712 for (auto *F : CXXGlobalInits)
713 ModuleInits.push_back(F);
714
715 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
717
718 // We now build the initializer for this module, which has a mangled name
719 // as per the Itanium ABI . The action of the initializer is guarded so that
720 // each init is run just once (even though a module might be imported
721 // multiple times via nested use).
722 llvm::Function *Fn;
723 {
724 SmallString<256> InitFnName;
725 llvm::raw_svector_ostream Out(InitFnName);
726 cast<ItaniumMangleContext>(getCXXABI().getMangleContext())
727 .mangleModuleInitializer(Primary, Out);
729 FTy, llvm::Twine(InitFnName), FI, SourceLocation(), false,
730 llvm::GlobalVariable::ExternalLinkage);
731
732 // If we have a completely empty initializer then we do not want to create
733 // the guard variable.
735 if (!AllImports.empty() || !PrioritizedCXXGlobalInits.empty() ||
736 !CXXGlobalInits.empty()) {
737 // Create the guard var.
738 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
739 getModule(), Int8Ty, /*isConstant=*/false,
740 llvm::GlobalVariable::InternalLinkage,
741 llvm::ConstantInt::get(Int8Ty, 0), InitFnName.str() + "__in_chrg");
742 CharUnits GuardAlign = CharUnits::One();
743 Guard->setAlignment(GuardAlign.getAsAlign());
744 GuardAddr = ConstantAddress(Guard, Int8Ty, GuardAlign);
745 }
746 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, ModuleInits,
747 GuardAddr);
748 }
749
750 // We allow for the case that a module object is added to a linked binary
751 // without a specific call to the the initializer. This also ensures that
752 // implementation partition initializers are called when the partition
753 // is not imported as an interface.
754 AddGlobalCtor(Fn);
755
756 // See the comment in EmitCXXGlobalInitFunc about OpenCL global init
757 // functions.
758 if (getLangOpts().OpenCL) {
760 Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
761 }
762
763 assert(!getLangOpts().CUDA || !getLangOpts().CUDAIsDevice ||
764 getLangOpts().GPUAllowDeviceInit);
765 if (getLangOpts().HIP && getLangOpts().CUDAIsDevice) {
766 Fn->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
767 Fn->addFnAttr("device-init");
768 }
769
770 // We are done with the inits.
771 AllImports.clear();
772 PrioritizedCXXGlobalInits.clear();
773 CXXGlobalInits.clear();
774 ModuleInits.clear();
775}
776
778 SmallString<128> FileName = llvm::sys::path::filename(M.getName());
779
780 if (FileName.empty())
781 FileName = "<null>";
782
783 for (size_t i = 0; i < FileName.size(); ++i) {
784 // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
785 // to be the set of C preprocessing numbers.
786 if (!isPreprocessingNumberBody(FileName[i]))
787 FileName[i] = '_';
788 }
789
790 return FileName;
791}
792
793static std::string getPrioritySuffix(unsigned int Priority) {
794 assert(Priority <= 65535 && "Priority should always be <= 65535.");
795
796 // Compute the function suffix from priority. Prepend with zeroes to make
797 // sure the function names are also ordered as priorities.
798 std::string PrioritySuffix = llvm::utostr(Priority);
799 PrioritySuffix = std::string(6 - PrioritySuffix.size(), '0') + PrioritySuffix;
800
801 return PrioritySuffix;
802}
803
804void
805CodeGenModule::EmitCXXGlobalInitFunc() {
806 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
807 CXXGlobalInits.pop_back();
808
809 // When we import C++20 modules, we must run their initializers first.
811 if (CXX20ModuleInits)
812 for (Module *M : ImportedModules) {
813 // No Itanium initializer in header like modules.
814 if (M->isHeaderLikeModule())
815 continue;
816 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
817 SmallString<256> FnName;
818 {
819 llvm::raw_svector_ostream Out(FnName);
820 cast<ItaniumMangleContext>(getCXXABI().getMangleContext())
821 .mangleModuleInitializer(M, Out);
822 }
823 assert(!GetGlobalValue(FnName.str()) &&
824 "We should only have one use of the initializer call");
825 llvm::Function *Fn = llvm::Function::Create(
826 FTy, llvm::Function::ExternalLinkage, FnName.str(), &getModule());
827 ModuleInits.push_back(Fn);
828 }
829
830 if (ModuleInits.empty() && CXXGlobalInits.empty() &&
831 PrioritizedCXXGlobalInits.empty())
832 return;
833
834 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
836
837 // Create our global prioritized initialization function.
838 if (!PrioritizedCXXGlobalInits.empty()) {
839 SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
840 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
841 PrioritizedCXXGlobalInits.end());
842 // Iterate over "chunks" of ctors with same priority and emit each chunk
843 // into separate function. Note - everything is sorted first by priority,
844 // second - by lex order, so we emit ctor functions in proper order.
846 I = PrioritizedCXXGlobalInits.begin(),
847 E = PrioritizedCXXGlobalInits.end(); I != E; ) {
849 PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
850
851 LocalCXXGlobalInits.clear();
852
853 unsigned int Priority = I->first.priority;
854 llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
855 FTy, "_GLOBAL__I_" + getPrioritySuffix(Priority), FI);
856
857 // Prepend the module inits to the highest priority set.
858 if (!ModuleInits.empty()) {
859 for (auto *F : ModuleInits)
860 LocalCXXGlobalInits.push_back(F);
861 ModuleInits.clear();
862 }
863
864 for (; I < PrioE; ++I)
865 LocalCXXGlobalInits.push_back(I->second);
866
867 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
868 AddGlobalCtor(Fn, Priority);
869 }
870 PrioritizedCXXGlobalInits.clear();
871 }
872
873 if (getCXXABI().useSinitAndSterm() && ModuleInits.empty() &&
874 CXXGlobalInits.empty())
875 return;
876
877 for (auto *F : CXXGlobalInits)
878 ModuleInits.push_back(F);
879 CXXGlobalInits.clear();
880
881 // Include the filename in the symbol name. Including "sub_" matches gcc
882 // and makes sure these symbols appear lexicographically behind the symbols
883 // with priority emitted above. Module implementation units behave the same
884 // way as a non-modular TU with imports.
885 llvm::Function *Fn;
886 if (CXX20ModuleInits && getContext().getNamedModuleForCodeGen() &&
887 !getContext().getNamedModuleForCodeGen()->isModuleImplementation()) {
888 SmallString<256> InitFnName;
889 llvm::raw_svector_ostream Out(InitFnName);
890 cast<ItaniumMangleContext>(getCXXABI().getMangleContext())
891 .mangleModuleInitializer(getContext().getNamedModuleForCodeGen(), Out);
893 FTy, llvm::Twine(InitFnName), FI, SourceLocation(), false,
894 llvm::GlobalVariable::ExternalLinkage);
895 } else
897 FTy,
898 llvm::Twine("_GLOBAL__sub_I_", getTransformedFileName(getModule())),
899 FI);
900
901 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, ModuleInits);
902 AddGlobalCtor(Fn);
903
904 // In OpenCL global init functions must be converted to kernels in order to
905 // be able to launch them from the host.
906 // FIXME: Some more work might be needed to handle destructors correctly.
907 // Current initialization function makes use of function pointers callbacks.
908 // We can't support function pointers especially between host and device.
909 // However it seems global destruction has little meaning without any
910 // dynamic resource allocation on the device and program scope variables are
911 // destroyed by the runtime when program is released.
912 if (getLangOpts().OpenCL) {
914 Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
915 }
916
917 assert(!getLangOpts().CUDA || !getLangOpts().CUDAIsDevice ||
918 getLangOpts().GPUAllowDeviceInit);
919 if (getLangOpts().HIP && getLangOpts().CUDAIsDevice) {
920 Fn->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
921 Fn->addFnAttr("device-init");
922 }
923
924 ModuleInits.clear();
925}
926
927void CodeGenModule::EmitCXXGlobalCleanUpFunc() {
928 if (CXXGlobalDtorsOrStermFinalizers.empty() &&
929 PrioritizedCXXStermFinalizers.empty())
930 return;
931
932 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
934
935 // Create our global prioritized cleanup function.
936 if (!PrioritizedCXXStermFinalizers.empty()) {
938 llvm::array_pod_sort(PrioritizedCXXStermFinalizers.begin(),
939 PrioritizedCXXStermFinalizers.end());
940 // Iterate over "chunks" of dtors with same priority and emit each chunk
941 // into separate function. Note - everything is sorted first by priority,
942 // second - by lex order, so we emit dtor functions in proper order.
944 I = PrioritizedCXXStermFinalizers.begin(),
945 E = PrioritizedCXXStermFinalizers.end();
946 I != E;) {
948 std::upper_bound(I + 1, E, *I, StermFinalizerPriorityCmp());
949
950 LocalCXXStermFinalizers.clear();
951
952 unsigned int Priority = I->first.priority;
953 llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
954 FTy, "_GLOBAL__a_" + getPrioritySuffix(Priority), FI);
955
956 for (; I < PrioE; ++I) {
957 llvm::FunctionCallee DtorFn = I->second;
958 LocalCXXStermFinalizers.emplace_back(DtorFn.getFunctionType(),
959 DtorFn.getCallee(), nullptr);
960 }
961
963 Fn, LocalCXXStermFinalizers);
964 AddGlobalDtor(Fn, Priority);
965 }
966 PrioritizedCXXStermFinalizers.clear();
967 }
968
969 if (CXXGlobalDtorsOrStermFinalizers.empty())
970 return;
971
972 // Create our global cleanup function.
973 llvm::Function *Fn =
974 CreateGlobalInitOrCleanUpFunction(FTy, "_GLOBAL__D_a", FI);
975
977 Fn, CXXGlobalDtorsOrStermFinalizers);
978 AddGlobalDtor(Fn);
979 CXXGlobalDtorsOrStermFinalizers.clear();
980}
981
982/// Emit the code necessary to initialize the given global variable.
984 const VarDecl *D,
985 llvm::GlobalVariable *Addr,
986 bool PerformInit) {
987 // Check if we need to emit debug info for variable initializer.
988 if (D->hasAttr<NoDebugAttr>())
989 DebugInfo = nullptr; // disable debug info indefinitely for this function
990
991 CurEHLocation = D->getBeginLoc();
992
994 getContext().VoidTy, Fn, getTypes().arrangeNullaryFunction(),
996 // Emit an artificial location for this function.
998
999 // Use guarded initialization if the global variable is weak. This
1000 // occurs for, e.g., instantiated static data members and
1001 // definitions explicitly marked weak.
1002 //
1003 // Also use guarded initialization for a variable with dynamic TLS and
1004 // unordered initialization. (If the initialization is ordered, the ABI
1005 // layer will guard the whole-TU initialization for us.)
1006 if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage() ||
1009 EmitCXXGuardedInit(*D, Addr, PerformInit);
1010 } else {
1011 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
1012 }
1013
1014 if (getLangOpts().HLSL)
1016
1018}
1019
1020void
1023 ConstantAddress Guard) {
1024 {
1025 auto NL = ApplyDebugLocation::CreateEmpty(*this);
1027 getTypes().arrangeNullaryFunction(), FunctionArgList());
1028 // Emit an artificial location for this function.
1029 auto AL = ApplyDebugLocation::CreateArtificial(*this);
1030
1031 llvm::BasicBlock *ExitBlock = nullptr;
1032 if (Guard.isValid()) {
1033 // If we have a guard variable, check whether we've already performed
1034 // these initializations. This happens for TLS initialization functions.
1035 llvm::Value *GuardVal = Builder.CreateLoad(Guard);
1036 llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
1037 "guard.uninitialized");
1038 llvm::BasicBlock *InitBlock = createBasicBlock("init");
1039 ExitBlock = createBasicBlock("exit");
1040 EmitCXXGuardedInitBranch(Uninit, InitBlock, ExitBlock,
1041 GuardKind::TlsGuard, nullptr);
1042 EmitBlock(InitBlock);
1043 // Mark as initialized before initializing anything else. If the
1044 // initializers use previously-initialized thread_local vars, that's
1045 // probably supposed to be OK, but the standard doesn't say.
1046 Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
1047
1048 // The guard variable can't ever change again.
1050 Guard.getPointer(),
1052 CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
1053 }
1054
1055 RunCleanupsScope Scope(*this);
1056
1057 // When building in Objective-C++ ARC mode, create an autorelease pool
1058 // around the global initializers.
1059 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
1060 llvm::Value *token = EmitObjCAutoreleasePoolPush();
1062 }
1063
1064 for (unsigned i = 0, e = Decls.size(); i != e; ++i)
1065 if (Decls[i])
1066 EmitRuntimeCall(Decls[i]);
1067
1068 Scope.ForceCleanup();
1069
1070 if (ExitBlock) {
1071 Builder.CreateBr(ExitBlock);
1072 EmitBlock(ExitBlock);
1073 }
1074 }
1075
1077}
1078
1080 llvm::Function *Fn,
1081 ArrayRef<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
1082 llvm::Constant *>>
1083 DtorsOrStermFinalizers) {
1084 {
1085 auto NL = ApplyDebugLocation::CreateEmpty(*this);
1087 getTypes().arrangeNullaryFunction(), FunctionArgList());
1088 // Emit an artificial location for this function.
1089 auto AL = ApplyDebugLocation::CreateArtificial(*this);
1090
1091 // Emit the cleanups, in reverse order from construction.
1092 for (unsigned i = 0, e = DtorsOrStermFinalizers.size(); i != e; ++i) {
1093 llvm::FunctionType *CalleeTy;
1094 llvm::Value *Callee;
1095 llvm::Constant *Arg;
1096 std::tie(CalleeTy, Callee, Arg) = DtorsOrStermFinalizers[e - i - 1];
1097
1098 llvm::CallInst *CI = nullptr;
1099 if (Arg == nullptr) {
1100 assert(
1102 "Arg could not be nullptr unless using sinit and sterm functions.");
1103 CI = Builder.CreateCall(CalleeTy, Callee);
1104 } else
1105 CI = Builder.CreateCall(CalleeTy, Callee, Arg);
1106
1107 // Make sure the call and the callee agree on calling convention.
1108 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1109 CI->setCallingConv(F->getCallingConv());
1110 }
1111 }
1112
1114}
1115
1116/// generateDestroyHelper - Generates a helper function which, when
1117/// invoked, destroys the given object. The address of the object
1118/// should be in global memory.
1120 Address addr, QualType type, Destroyer *destroyer,
1121 bool useEHCleanupForArray, const VarDecl *VD) {
1122 FunctionArgList args;
1125 args.push_back(&Dst);
1126
1127 const CGFunctionInfo &FI =
1129 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
1130 llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction(
1131 FTy, "__cxx_global_array_dtor", FI, VD->getLocation());
1132
1133 CurEHLocation = VD->getBeginLoc();
1134
1136 getContext().VoidTy, fn, FI, args);
1137 // Emit an artificial location for this function.
1138 auto AL = ApplyDebugLocation::CreateArtificial(*this);
1139
1140 emitDestroy(addr, type, destroyer, useEHCleanupForArray);
1141
1143
1144 return fn;
1145}
static std::string getPrioritySuffix(unsigned int Priority)
Definition: CGDeclCXX.cpp:793
static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, ConstantAddress DeclPtr)
Definition: CGDeclCXX.cpp:29
static SmallString< 128 > getTransformedFileName(llvm::Module &M)
Definition: CGDeclCXX.cpp:777
static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, ConstantAddress Addr)
Emit code to cause the destruction of the given variable with static storage duration.
Definition: CGDeclCXX.cpp:71
static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, llvm::Constant *Addr)
Emit code to cause the variable at the given address to be considered as constant from this point onw...
Definition: CGDeclCXX.cpp:152
int Priority
Definition: Format.cpp:2778
Defines the clang::LangOptions interface.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:762
CanQualType IntTy
Definition: ASTContext.h:1087
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
Definition: ASTContext.h:1078
unsigned getTargetAddressSpace(LangAS AS) const
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2738
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
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
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
An aligned address.
Definition: Address.h:29
bool isValid() const
Definition: Address.h:50
static AggValueSlot forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition: CGValue.h:612
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:830
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
Definition: CGDebugInfo.h:847
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:99
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:71
virtual bool HasThisReturn(GlobalDecl GD) const
Returns true if the given constructor or destructor is one of the kinds that the ABI says returns 'th...
Definition: CGCXXABI.h:127
virtual bool useSinitAndSterm() const
Definition: CGCXXABI.h:137
virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)=0
Emits the guarded initializer and destructor setup for the given variable, given that it couldn't be ...
virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr)=0
Emit code to force the execution of a destructor during global teardown.
virtual bool canCallMismatchedFunctionType() const
Returns true if the target allows calling a function through a pointer with a different signature tha...
Definition: CGCXXABI.h:147
virtual void EmitThreadLocalInitFuncs(CodeGenModule &CGM, ArrayRef< const VarDecl * > CXXThreadLocals, ArrayRef< llvm::Function * > CXXThreadLocalInits, ArrayRef< const VarDecl * > CXXThreadLocalInitVars)=0
Emits ABI-required functions necessary to initialize thread_local variables in this translation unit.
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:117
CGFunctionInfo - Class to encapsulate the information about a function definition.
void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV)
virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest)=0
virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest, bool threadlocal=false)=0
virtual llvm::Function * emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, CodeGenFunction *CGF=nullptr)
Emit a code for initialization of threadprivate variable.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void GenerateCXXGlobalInitFunc(llvm::Function *Fn, ArrayRef< llvm::Function * > CXXThreadLocals, ConstantAddress Guard=ConstantAddress::invalid())
GenerateCXXGlobalInitFunc - Generates code for initializing global variables.
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
llvm::Value * EmitObjCAutoreleasePoolPush()
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
const LangOptions & getLangOpts() const
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
llvm::Type * ConvertTypeForMem(QualType T)
llvm::Function * createTLSAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr, llvm::FunctionCallee &AtExit)
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
llvm::Function * generateDestroyHelper(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray, const VarDecl *VD)
void GenerateCXXGlobalCleanUpFunc(llvm::Function *Fn, ArrayRef< std::tuple< llvm::FunctionType *, llvm::WeakTrackingVH, llvm::Constant * > > DtorsOrStermFinalizers)
GenerateCXXGlobalCleanUpFunc - Generates code for cleaning up global variables.
void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)
Emit code in this function to perform a guarded variable initialization.
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::GlobalVariable *GV, bool PerformInit)
EmitCXXGlobalVarDeclInit - Create the initializer for a C++ variable with global storage.
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
llvm::Function * createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr)
void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn, llvm::Constant *addr)
Call atexit() with a function that passes the given argument to the given function.
llvm::Value * unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub)
Call unatexit() with function dtorStub.
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind.
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
void EmitInvariantStart(llvm::Constant *Addr, CharUnits Size)
CodeGenTypes & getTypes() const
void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, const VarDecl *D, llvm::GlobalVariable *Addr, bool PerformInit)
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr)
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
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...
void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, llvm::BasicBlock *InitBlock, llvm::BasicBlock *NoInitBlock, GuardKind Kind, const VarDecl *D)
Emit a branch to select whether or not to perform guarded initialization.
This class organizes the cross-function state that is used while generating LLVM code.
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
CGHLSLRuntime & getHLSLRuntime()
Return a reference to the configured HLSL runtime.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
llvm::FunctionCallee getAddrAndTypeOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Definition: CGCXX.cpp:221
bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, SourceLocation Loc) const
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
const llvm::DataLayout & getDataLayout() const
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
CGCXXABI & getCXXABI() const
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
const llvm::Triple & getTriple() const
ASTContext & getContext() const
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
llvm::LLVMContext & getLLVMContext()
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
void GenKernelArgMetadata(llvm::Function *FN, const FunctionDecl *FD=nullptr, CodeGenFunction *CGF=nullptr)
OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument information in the program executab...
bool isTypeConstant(QualType QTy, bool ExcludeCtor, bool ExcludeDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=std::nullopt)
llvm::Function * CreateGlobalInitOrCleanUpFunction(llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, SourceLocation Loc=SourceLocation(), bool TLS=false, llvm::GlobalVariable::LinkageTypes Linkage=llvm::GlobalVariable::InternalLinkage)
Definition: CGDeclCXX.cpp:429
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, bool instanceMethod, bool chainCall, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
Definition: CGCall.cpp:755
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1618
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:671
unsigned getTargetAddressSpace(QualType T) const
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition: CGCall.cpp:713
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:117
ConstantAddress getElementBitCast(llvm::Type *ElemTy) const
Definition: Address.h:133
static ConstantAddress invalid()
Definition: Address.h:125
llvm::Constant * getPointer() const
Definition: Address.h:129
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:353
LValue - This represents an lvalue references.
Definition: CGValue.h:171
bool isObjCStrong() const
Definition: CGValue.h:314
bool isObjCWeak() const
Definition: CGValue.h:311
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:61
virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const
Get address space of pointer parameter for __cxa_atexit.
Definition: TargetInfo.h:306
T * getAttr() const
Definition: DeclBase.h:556
SourceLocation getLocation() const
Definition: DeclBase.h:432
bool hasAttr() const
Definition: DeclBase.h:560
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:817
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:330
A class which abstracts out some details necessary for making a call.
Definition: Type.h:3805
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
@ Other
Other implicit parameter.
Definition: Decl.h:1682
virtual void mangleDynamicAtExitDestructor(const VarDecl *D, raw_ostream &)=0
virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &)=0
Describes a module or submodule.
Definition: Module.h:98
SmallVector< ExportDecl, 2 > Exports
The set of export declarations.
Definition: Module.h:390
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Definition: Module.h:377
bool isHeaderLikeModule() const
Is this module have similar semantics as headers.
Definition: Module.h:567
bool isExternallyVisible() const
Definition: Decl.h:405
A (possibly-)qualified type.
Definition: Type.h:736
@ DK_cxx_destructor
Definition: Type.h:1278
@ DK_nontrivial_c_struct
Definition: Type.h:1281
@ DK_objc_weak_lifetime
Definition: Type.h:1280
@ DK_objc_strong_lifetime
Definition: Type.h:1279
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6689
LangAS getAddressSpace() const
Definition: Type.h:377
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Encodes a location in the source.
The base class of the type hierarchy.
Definition: Type.h:1566
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1783
bool isReferenceType() const
Definition: Type.h:6922
QualType getType() const
Definition: Decl.h:712
Represents a variable declaration or definition.
Definition: Decl.h:913
TLSKind getTLSKind() const
Definition: Decl.cpp:2113
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1183
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition: Decl.cpp:2756
const Expr * getInit() const
Definition: Decl.h:1325
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1141
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition: Decl.h:939
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1210
TemplateSpecializationKind getTemplateSpecializationKind() const
If this variable is an instantiation of a variable template or a static data member of a class templa...
Definition: Decl.cpp:2692
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
Definition: Specifiers.h:200
@ OpenCL
Definition: LangStandard.h:62
@ CPlusPlus
Definition: LangStandard.h:53
@ GVA_DiscardableODR
Definition: Linkage.h:71
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:23
@ C
Languages that the frontend can parse and compile.
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
Return true if this is the body character of a C preprocessing number, which is [a-zA-Z0-9_.
Definition: CharInfo.h:153
unsigned long uint64_t
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::CallingConv::ID getRuntimeCC() const
llvm::IntegerType * IntTy
int