1 //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This contains code dealing with code generation of C++ declarations
11 //===----------------------------------------------------------------------===//
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"
20 #include "clang/Basic/LangOptions.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"
26 using namespace clang
;
27 using namespace CodeGen
;
29 static void EmitDeclInit(CodeGenFunction
&CGF
, const VarDecl
&D
,
30 ConstantAddress DeclPtr
) {
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!");
38 QualType type
= D
.getType();
39 LValue lv
= CGF
.MakeAddrLValue(DeclPtr
, type
);
41 const Expr
*Init
= D
.getInit();
42 switch (CGF
.getEvaluationKind(type
)) {
44 CodeGenModule
&CGM
= CGF
.CGM
;
45 if (lv
.isObjCStrong())
46 CGM
.getObjCRuntime().EmitObjCGlobalAssign(CGF
, CGF
.EmitScalarExpr(Init
),
47 DeclPtr
, D
.getTLSKind());
48 else if (lv
.isObjCWeak())
49 CGM
.getObjCRuntime().EmitObjCWeakAssign(CGF
, CGF
.EmitScalarExpr(Init
),
52 CGF
.EmitScalarInit(Init
, &D
, lv
, false);
56 CGF
.EmitComplexExprIntoLValue(Init
, lv
, /*isInit*/ true);
60 AggValueSlot::forLValue(lv
, CGF
, AggValueSlot::IsDestructed
,
61 AggValueSlot::DoesNotNeedGCBarriers
,
62 AggValueSlot::IsNotAliased
,
63 AggValueSlot::DoesNotOverlap
));
66 llvm_unreachable("bad evaluation kind");
69 /// Emit code to cause the destruction of the given variable with
70 /// static storage duration.
71 static 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.
79 QualType::DestructionKind DtorKind
= D
.needsDestruction(CGF
.getContext());
81 // FIXME: __attribute__((cleanup)) ?
84 case QualType::DK_none
:
87 case QualType::DK_cxx_destructor
:
90 case QualType::DK_objc_strong_lifetime
:
91 case QualType::DK_objc_weak_lifetime
:
92 case QualType::DK_nontrivial_c_struct
:
93 // We don't care about releasing objects during process teardown.
94 assert(!D
.getTLSKind() && "should have rejected this");
98 llvm::FunctionCallee Func
;
99 llvm::Constant
*Argument
;
101 CodeGenModule
&CGM
= CGF
.CGM
;
102 QualType Type
= D
.getType();
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
108 const CXXRecordDecl
*Record
= Type
->getAsCXXRecordDecl();
109 bool CanRegisterDestructor
=
110 Record
&& (!CGM
.getCXXABI().HasThisReturn(
111 GlobalDecl(Record
->getDestructor(), Dtor_Complete
)) ||
112 CGM
.getCXXABI().canCallMismatchedFunctionType());
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
116 bool UsingExternalHelper
= !CGM
.getCodeGenOpts().CXAAtExit
;
117 if (Record
&& (CanRegisterDestructor
|| UsingExternalHelper
)) {
118 assert(!Record
->hasTrivialDestructor());
119 CXXDestructorDecl
*Dtor
= Record
->getDestructor();
121 Func
= CGM
.getAddrAndTypeOfCXXStructor(GlobalDecl(Dtor
, Dtor_Complete
));
122 if (CGF
.getContext().getLangOpts().OpenCL
) {
124 CGM
.getTargetCodeGenInfo().getAddrSpaceOfCxaAtexitPtrParam();
125 auto DestTy
= llvm::PointerType::get(
126 CGM
.getLLVMContext(), CGM
.getContext().getTargetAddressSpace(DestAS
));
127 auto SrcAS
= D
.getType().getQualifiers().getAddressSpace();
129 Argument
= Addr
.getPointer();
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
);
135 Argument
= Addr
.getPointer();
137 // Otherwise, the standard logic requires a helper function.
139 Addr
= Addr
.withElementType(CGF
.ConvertTypeForMem(Type
));
140 Func
= CodeGenFunction(CGM
)
141 .generateDestroyHelper(Addr
, Type
, CGF
.getDestroyer(DtorKind
),
142 CGF
.needsEHCleanup(DtorKind
), &D
);
143 Argument
= llvm::Constant::getNullValue(CGF
.Int8PtrTy
);
146 CGM
.getCXXABI().registerGlobalDtor(CGF
, D
, Func
, Argument
);
149 /// Emit code to cause the variable at the given address to be considered as
150 /// constant from this point onwards.
151 static void EmitDeclInvariant(CodeGenFunction
&CGF
, const VarDecl
&D
,
152 llvm::Constant
*Addr
) {
153 return CGF
.EmitInvariantStart(
154 Addr
, CGF
.getContext().getTypeSizeInChars(D
.getType()));
157 void CodeGenFunction::EmitInvariantStart(llvm::Constant
*Addr
, CharUnits Size
) {
158 // Do not emit the intrinsic if we're not optimizing.
159 if (!CGM
.getCodeGenOpts().OptimizationLevel
)
162 // Grab the llvm.invariant.start intrinsic.
163 llvm::Intrinsic::ID InvStartID
= llvm::Intrinsic::invariant_start
;
164 // Overloaded address space type.
165 llvm::Type
*ObjectPtr
[1] = {Int8PtrTy
};
166 llvm::Function
*InvariantStart
= CGM
.getIntrinsic(InvStartID
, ObjectPtr
);
168 // Emit a call with the size in bytes of the object.
169 uint64_t Width
= Size
.getQuantity();
170 llvm::Value
*Args
[2] = {llvm::ConstantInt::getSigned(Int64Ty
, Width
), Addr
};
171 Builder
.CreateCall(InvariantStart
, Args
);
174 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl
&D
,
175 llvm::GlobalVariable
*GV
,
178 const Expr
*Init
= D
.getInit();
179 QualType T
= D
.getType();
181 // The address space of a static local variable (DeclPtr) may be different
182 // from the address space of the "this" argument of the constructor. In that
183 // case, we need an addrspacecast before calling the constructor.
185 // struct StructWithCtor {
186 // __device__ StructWithCtor() {...}
188 // __device__ void foo() {
189 // __shared__ StructWithCtor s;
193 // For example, in the above CUDA code, the static local variable s has a
194 // "shared" address space qualifier, but the constructor of StructWithCtor
195 // expects "this" in the "generic" address space.
196 unsigned ExpectedAddrSpace
= getTypes().getTargetAddressSpace(T
);
197 unsigned ActualAddrSpace
= GV
->getAddressSpace();
198 llvm::Constant
*DeclPtr
= GV
;
199 if (ActualAddrSpace
!= ExpectedAddrSpace
) {
200 llvm::PointerType
*PTy
=
201 llvm::PointerType::get(getLLVMContext(), ExpectedAddrSpace
);
202 DeclPtr
= llvm::ConstantExpr::getAddrSpaceCast(DeclPtr
, PTy
);
205 ConstantAddress
DeclAddr(
206 DeclPtr
, GV
->getValueType(), getContext().getDeclAlign(&D
));
208 if (!T
->isReferenceType()) {
209 if (getLangOpts().OpenMP
&& !getLangOpts().OpenMPSimd
&&
210 D
.hasAttr
<OMPThreadPrivateDeclAttr
>()) {
211 (void)CGM
.getOpenMPRuntime().emitThreadPrivateVarDefinition(
212 &D
, DeclAddr
, D
.getAttr
<OMPThreadPrivateDeclAttr
>()->getLocation(),
216 D
.needsDestruction(getContext()) == QualType::DK_cxx_destructor
;
218 EmitDeclInit(*this, D
, DeclAddr
);
219 if (D
.getType().isConstantStorage(getContext(), true, !NeedsDtor
))
220 EmitDeclInvariant(*this, D
, DeclPtr
);
222 EmitDeclDestroy(*this, D
, DeclAddr
);
226 assert(PerformInit
&& "cannot have constant initializer which needs "
227 "destruction for reference");
228 RValue RV
= EmitReferenceBindingToExpr(Init
);
229 EmitStoreOfScalar(RV
.getScalarVal(), DeclAddr
, false, T
);
232 /// Create a stub function, suitable for being passed to atexit,
233 /// which passes the given address to the given destructor function.
234 llvm::Function
*CodeGenFunction::createAtExitStub(const VarDecl
&VD
,
235 llvm::FunctionCallee dtor
,
236 llvm::Constant
*addr
) {
237 // Get the destructor function type, void(*)(void).
238 llvm::FunctionType
*ty
= llvm::FunctionType::get(CGM
.VoidTy
, false);
239 SmallString
<256> FnName
;
241 llvm::raw_svector_ostream
Out(FnName
);
242 CGM
.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD
, Out
);
245 const CGFunctionInfo
&FI
= CGM
.getTypes().arrangeNullaryFunction();
246 llvm::Function
*fn
= CGM
.CreateGlobalInitOrCleanUpFunction(
247 ty
, FnName
.str(), FI
, VD
.getLocation());
249 CodeGenFunction
CGF(CGM
);
251 CGF
.StartFunction(GlobalDecl(&VD
, DynamicInitKind::AtExit
),
252 CGM
.getContext().VoidTy
, fn
, FI
, FunctionArgList(),
253 VD
.getLocation(), VD
.getInit()->getExprLoc());
254 // Emit an artificial location for this function.
255 auto AL
= ApplyDebugLocation::CreateArtificial(CGF
);
257 llvm::CallInst
*call
= CGF
.Builder
.CreateCall(dtor
, addr
);
259 // Make sure the call and the callee agree on calling convention.
260 if (auto *dtorFn
= dyn_cast
<llvm::Function
>(
261 dtor
.getCallee()->stripPointerCastsAndAliases()))
262 call
->setCallingConv(dtorFn
->getCallingConv());
264 CGF
.FinishFunction();
269 /// Create a stub function, suitable for being passed to __pt_atexit_np,
270 /// which passes the given address to the given destructor function.
271 llvm::Function
*CodeGenFunction::createTLSAtExitStub(
272 const VarDecl
&D
, llvm::FunctionCallee Dtor
, llvm::Constant
*Addr
,
273 llvm::FunctionCallee
&AtExit
) {
274 SmallString
<256> FnName
;
276 llvm::raw_svector_ostream
Out(FnName
);
277 CGM
.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&D
, Out
);
280 const CGFunctionInfo
&FI
= CGM
.getTypes().arrangeLLVMFunctionInfo(
281 getContext().IntTy
, FnInfoOpts::None
, {getContext().IntTy
},
282 FunctionType::ExtInfo(), {}, RequiredArgs::All
);
284 // Get the stub function type, int(*)(int,...).
285 llvm::FunctionType
*StubTy
=
286 llvm::FunctionType::get(CGM
.IntTy
, {CGM
.IntTy
}, true);
288 llvm::Function
*DtorStub
= CGM
.CreateGlobalInitOrCleanUpFunction(
289 StubTy
, FnName
.str(), FI
, D
.getLocation());
291 CodeGenFunction
CGF(CGM
);
293 FunctionArgList Args
;
294 ImplicitParamDecl
IPD(CGM
.getContext(), CGM
.getContext().IntTy
,
295 ImplicitParamDecl::Other
);
296 Args
.push_back(&IPD
);
297 QualType ResTy
= CGM
.getContext().IntTy
;
299 CGF
.StartFunction(GlobalDecl(&D
, DynamicInitKind::AtExit
), ResTy
, DtorStub
,
300 FI
, Args
, D
.getLocation(), D
.getInit()->getExprLoc());
302 // Emit an artificial location for this function.
303 auto AL
= ApplyDebugLocation::CreateArtificial(CGF
);
305 llvm::CallInst
*call
= CGF
.Builder
.CreateCall(Dtor
, Addr
);
307 // Make sure the call and the callee agree on calling convention.
308 if (auto *DtorFn
= dyn_cast
<llvm::Function
>(
309 Dtor
.getCallee()->stripPointerCastsAndAliases()))
310 call
->setCallingConv(DtorFn
->getCallingConv());
312 // Return 0 from function
313 CGF
.Builder
.CreateStore(llvm::Constant::getNullValue(CGM
.IntTy
),
316 CGF
.FinishFunction();
321 /// Register a global destructor using the C atexit runtime function.
322 void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl
&VD
,
323 llvm::FunctionCallee dtor
,
324 llvm::Constant
*addr
) {
325 // Create a function which calls the destructor.
326 llvm::Constant
*dtorStub
= createAtExitStub(VD
, dtor
, addr
);
327 registerGlobalDtorWithAtExit(dtorStub
);
330 void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant
*dtorStub
) {
331 // extern "C" int atexit(void (*f)(void));
332 assert(dtorStub
->getType() ==
333 llvm::PointerType::get(
334 llvm::FunctionType::get(CGM
.VoidTy
, false),
335 dtorStub
->getType()->getPointerAddressSpace()) &&
336 "Argument to atexit has a wrong type.");
338 llvm::FunctionType
*atexitTy
=
339 llvm::FunctionType::get(IntTy
, dtorStub
->getType(), false);
341 llvm::FunctionCallee atexit
=
342 CGM
.CreateRuntimeFunction(atexitTy
, "atexit", llvm::AttributeList(),
344 if (llvm::Function
*atexitFn
= dyn_cast
<llvm::Function
>(atexit
.getCallee()))
345 atexitFn
->setDoesNotThrow();
347 EmitNounwindRuntimeCall(atexit
, dtorStub
);
351 CodeGenFunction::unregisterGlobalDtorWithUnAtExit(llvm::Constant
*dtorStub
) {
352 // The unatexit subroutine unregisters __dtor functions that were previously
353 // registered by the atexit subroutine. If the referenced function is found,
354 // it is removed from the list of functions that are called at normal program
355 // termination and the unatexit returns a value of 0, otherwise a non-zero
356 // value is returned.
358 // extern "C" int unatexit(void (*f)(void));
359 assert(dtorStub
->getType() ==
360 llvm::PointerType::get(
361 llvm::FunctionType::get(CGM
.VoidTy
, false),
362 dtorStub
->getType()->getPointerAddressSpace()) &&
363 "Argument to unatexit has a wrong type.");
365 llvm::FunctionType
*unatexitTy
=
366 llvm::FunctionType::get(IntTy
, {dtorStub
->getType()}, /*isVarArg=*/false);
368 llvm::FunctionCallee unatexit
=
369 CGM
.CreateRuntimeFunction(unatexitTy
, "unatexit", llvm::AttributeList());
371 cast
<llvm::Function
>(unatexit
.getCallee())->setDoesNotThrow();
373 return EmitNounwindRuntimeCall(unatexit
, dtorStub
);
376 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl
&D
,
377 llvm::GlobalVariable
*DeclPtr
,
379 // If we've been asked to forbid guard variables, emit an error now.
380 // This diagnostic is hard-coded for Darwin's use case; we can find
381 // better phrasing if someone else needs it.
382 if (CGM
.getCodeGenOpts().ForbidGuardVariables
)
383 CGM
.Error(D
.getLocation(),
384 "this initialization requires a guard variable, which "
385 "the kernel does not support");
387 CGM
.getCXXABI().EmitGuardedInit(*this, D
, DeclPtr
, PerformInit
);
390 void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value
*NeedsInit
,
391 llvm::BasicBlock
*InitBlock
,
392 llvm::BasicBlock
*NoInitBlock
,
395 assert((Kind
== GuardKind::TlsGuard
|| D
) && "no guarded variable");
397 // A guess at how many times we will enter the initialization of a
398 // variable, depending on the kind of variable.
399 static const uint64_t InitsPerTLSVar
= 1024;
400 static const uint64_t InitsPerLocalVar
= 1024 * 1024;
402 llvm::MDNode
*Weights
;
403 if (Kind
== GuardKind::VariableGuard
&& !D
->isLocalVarDecl()) {
404 // For non-local variables, don't apply any weighting for now. Due to our
405 // use of COMDATs, we expect there to be at most one initialization of the
406 // variable per DSO, but we have no way to know how many DSOs will try to
407 // initialize the variable.
411 // FIXME: For the TLS case, collect and use profiling information to
412 // determine a more accurate brach weight.
413 if (Kind
== GuardKind::TlsGuard
|| D
->getTLSKind())
414 NumInits
= InitsPerTLSVar
;
416 NumInits
= InitsPerLocalVar
;
418 // The probability of us entering the initializer is
419 // 1 / (total number of times we attempt to initialize the variable).
420 llvm::MDBuilder
MDHelper(CGM
.getLLVMContext());
421 Weights
= MDHelper
.createBranchWeights(1, NumInits
- 1);
424 Builder
.CreateCondBr(NeedsInit
, InitBlock
, NoInitBlock
, Weights
);
427 llvm::Function
*CodeGenModule::CreateGlobalInitOrCleanUpFunction(
428 llvm::FunctionType
*FTy
, const Twine
&Name
, const CGFunctionInfo
&FI
,
429 SourceLocation Loc
, bool TLS
, llvm::GlobalVariable::LinkageTypes Linkage
) {
430 llvm::Function
*Fn
= llvm::Function::Create(FTy
, Linkage
, Name
, &getModule());
432 if (!getLangOpts().AppleKext
&& !TLS
) {
433 // Set the section if needed.
434 if (const char *Section
= getTarget().getStaticInitSectionSpecifier())
435 Fn
->setSection(Section
);
438 if (Linkage
== llvm::GlobalVariable::InternalLinkage
)
439 SetInternalFunctionAttributes(GlobalDecl(), Fn
, FI
);
441 Fn
->setCallingConv(getRuntimeCC());
443 if (!getLangOpts().Exceptions
)
444 Fn
->setDoesNotThrow();
446 if (getLangOpts().Sanitize
.has(SanitizerKind::Address
) &&
447 !isInNoSanitizeList(SanitizerKind::Address
, Fn
, Loc
))
448 Fn
->addFnAttr(llvm::Attribute::SanitizeAddress
);
450 if (getLangOpts().Sanitize
.has(SanitizerKind::KernelAddress
) &&
451 !isInNoSanitizeList(SanitizerKind::KernelAddress
, Fn
, Loc
))
452 Fn
->addFnAttr(llvm::Attribute::SanitizeAddress
);
454 if (getLangOpts().Sanitize
.has(SanitizerKind::HWAddress
) &&
455 !isInNoSanitizeList(SanitizerKind::HWAddress
, Fn
, Loc
))
456 Fn
->addFnAttr(llvm::Attribute::SanitizeHWAddress
);
458 if (getLangOpts().Sanitize
.has(SanitizerKind::KernelHWAddress
) &&
459 !isInNoSanitizeList(SanitizerKind::KernelHWAddress
, Fn
, Loc
))
460 Fn
->addFnAttr(llvm::Attribute::SanitizeHWAddress
);
462 if (getLangOpts().Sanitize
.has(SanitizerKind::MemtagStack
) &&
463 !isInNoSanitizeList(SanitizerKind::MemtagStack
, Fn
, Loc
))
464 Fn
->addFnAttr(llvm::Attribute::SanitizeMemTag
);
466 if (getLangOpts().Sanitize
.has(SanitizerKind::Thread
) &&
467 !isInNoSanitizeList(SanitizerKind::Thread
, Fn
, Loc
))
468 Fn
->addFnAttr(llvm::Attribute::SanitizeThread
);
470 if (getLangOpts().Sanitize
.has(SanitizerKind::Memory
) &&
471 !isInNoSanitizeList(SanitizerKind::Memory
, Fn
, Loc
))
472 Fn
->addFnAttr(llvm::Attribute::SanitizeMemory
);
474 if (getLangOpts().Sanitize
.has(SanitizerKind::KernelMemory
) &&
475 !isInNoSanitizeList(SanitizerKind::KernelMemory
, Fn
, Loc
))
476 Fn
->addFnAttr(llvm::Attribute::SanitizeMemory
);
478 if (getLangOpts().Sanitize
.has(SanitizerKind::SafeStack
) &&
479 !isInNoSanitizeList(SanitizerKind::SafeStack
, Fn
, Loc
))
480 Fn
->addFnAttr(llvm::Attribute::SafeStack
);
482 if (getLangOpts().Sanitize
.has(SanitizerKind::ShadowCallStack
) &&
483 !isInNoSanitizeList(SanitizerKind::ShadowCallStack
, Fn
, Loc
))
484 Fn
->addFnAttr(llvm::Attribute::ShadowCallStack
);
489 /// Create a global pointer to a function that will initialize a global
490 /// variable. The user has requested that this pointer be emitted in a specific
492 void CodeGenModule::EmitPointerToInitFunc(const VarDecl
*D
,
493 llvm::GlobalVariable
*GV
,
494 llvm::Function
*InitFunc
,
496 llvm::GlobalVariable
*PtrArray
= new llvm::GlobalVariable(
497 TheModule
, InitFunc
->getType(), /*isConstant=*/true,
498 llvm::GlobalValue::PrivateLinkage
, InitFunc
, "__cxx_init_fn_ptr");
499 PtrArray
->setSection(ISA
->getSection());
500 addUsedGlobal(PtrArray
);
502 // If the GV is already in a comdat group, then we have to join it.
503 if (llvm::Comdat
*C
= GV
->getComdat())
504 PtrArray
->setComdat(C
);
508 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl
*D
,
509 llvm::GlobalVariable
*Addr
,
512 // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
513 // __constant__ and __shared__ variables defined in namespace scope,
514 // that are of class type, cannot have a non-empty constructor. All
515 // the checks have been done in Sema by now. Whatever initializers
516 // are allowed are empty and we just need to ignore them here.
517 if (getLangOpts().CUDAIsDevice
&& !getLangOpts().GPUAllowDeviceInit
&&
518 (D
->hasAttr
<CUDADeviceAttr
>() || D
->hasAttr
<CUDAConstantAttr
>() ||
519 D
->hasAttr
<CUDASharedAttr
>()))
522 if (getLangOpts().OpenMP
&&
523 getOpenMPRuntime().emitDeclareTargetVarDefinition(D
, Addr
, PerformInit
))
526 // Check if we've already initialized this decl.
527 auto I
= DelayedCXXInitPosition
.find(D
);
528 if (I
!= DelayedCXXInitPosition
.end() && I
->second
== ~0U)
531 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
532 SmallString
<256> FnName
;
534 llvm::raw_svector_ostream
Out(FnName
);
535 getCXXABI().getMangleContext().mangleDynamicInitializer(D
, Out
);
538 // Create a variable initialization function.
539 llvm::Function
*Fn
= CreateGlobalInitOrCleanUpFunction(
540 FTy
, FnName
.str(), getTypes().arrangeNullaryFunction(), D
->getLocation());
542 auto *ISA
= D
->getAttr
<InitSegAttr
>();
543 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn
, D
, Addr
,
546 llvm::GlobalVariable
*COMDATKey
=
547 supportsCOMDAT() && D
->isExternallyVisible() ? Addr
: nullptr;
549 if (D
->getTLSKind()) {
550 // FIXME: Should we support init_priority for thread_local?
551 // FIXME: We only need to register one __cxa_thread_atexit function for the
553 CXXThreadLocalInits
.push_back(Fn
);
554 CXXThreadLocalInitVars
.push_back(D
);
555 } else if (PerformInit
&& ISA
) {
556 // Contract with backend that "init_seg(compiler)" corresponds to priority
557 // 200 and "init_seg(lib)" corresponds to priority 400.
559 if (ISA
->getSection() == ".CRT$XCC")
561 else if (ISA
->getSection() == ".CRT$XCL")
565 AddGlobalCtor(Fn
, Priority
, ~0U, COMDATKey
);
567 EmitPointerToInitFunc(D
, Addr
, Fn
, ISA
);
568 } else if (auto *IPA
= D
->getAttr
<InitPriorityAttr
>()) {
569 OrderGlobalInitsOrStermFinalizers
Key(IPA
->getPriority(),
570 PrioritizedCXXGlobalInits
.size());
571 PrioritizedCXXGlobalInits
.push_back(std::make_pair(Key
, Fn
));
572 } else if (isTemplateInstantiation(D
->getTemplateSpecializationKind()) ||
573 getContext().GetGVALinkageForVariable(D
) == GVA_DiscardableODR
||
574 D
->hasAttr
<SelectAnyAttr
>()) {
575 // C++ [basic.start.init]p2:
576 // Definitions of explicitly specialized class template static data
577 // members have ordered initialization. Other class template static data
578 // members (i.e., implicitly or explicitly instantiated specializations)
579 // have unordered initialization.
581 // As a consequence, we can put them into their own llvm.global_ctors entry.
583 // If the global is externally visible, put the initializer into a COMDAT
584 // group with the global being initialized. On most platforms, this is a
585 // minor startup time optimization. In the MS C++ ABI, there are no guard
586 // variables, so this COMDAT key is required for correctness.
588 // SelectAny globals will be comdat-folded. Put the initializer into a
589 // COMDAT group associated with the global, so the initializers get folded
591 I
= DelayedCXXInitPosition
.find(D
);
592 // CXXGlobalInits.size() is the lex order number for the next deferred
593 // VarDecl. Use it when the current VarDecl is non-deferred. Although this
594 // lex order number is shared between current VarDecl and some following
595 // VarDecls, their order of insertion into `llvm.global_ctors` is the same
596 // as the lexing order and the following stable sort would preserve such
599 I
== DelayedCXXInitPosition
.end() ? CXXGlobalInits
.size() : I
->second
;
600 AddGlobalCtor(Fn
, 65535, LexOrder
, COMDATKey
);
601 if (COMDATKey
&& (getTriple().isOSBinFormatELF() ||
602 getTarget().getCXXABI().isMicrosoft())) {
603 // When COMDAT is used on ELF or in the MS C++ ABI, the key must be in
604 // llvm.used to prevent linker GC.
605 addUsedGlobal(COMDATKey
);
608 // If we used a COMDAT key for the global ctor, the init function can be
609 // discarded if the global ctor entry is discarded.
610 // FIXME: Do we need to restrict this to ELF and Wasm?
611 llvm::Comdat
*C
= Addr
->getComdat();
612 if (COMDATKey
&& C
&&
613 (getTarget().getTriple().isOSBinFormatELF() ||
614 getTarget().getTriple().isOSBinFormatWasm())) {
618 I
= DelayedCXXInitPosition
.find(D
); // Re-do lookup in case of re-hash.
619 if (I
== DelayedCXXInitPosition
.end()) {
620 CXXGlobalInits
.push_back(Fn
);
621 } else if (I
->second
!= ~0U) {
622 assert(I
->second
< CXXGlobalInits
.size() &&
623 CXXGlobalInits
[I
->second
] == nullptr);
624 CXXGlobalInits
[I
->second
] = Fn
;
628 // Remember that we already emitted the initializer for this global.
629 DelayedCXXInitPosition
[D
] = ~0U;
632 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
633 getCXXABI().EmitThreadLocalInitFuncs(
634 *this, CXXThreadLocals
, CXXThreadLocalInits
, CXXThreadLocalInitVars
);
636 CXXThreadLocalInits
.clear();
637 CXXThreadLocalInitVars
.clear();
638 CXXThreadLocals
.clear();
641 /* Build the initializer for a C++20 module:
642 This is arranged to be run only once regardless of how many times the module
643 might be included transitively. This arranged by using a guard variable.
645 If there are no initializers at all (and also no imported modules) we reduce
646 this to an empty function (since the Itanium ABI requires that this function
647 be available to a caller, which might be produced by a different
650 First we call any initializers for imported modules.
651 We then call initializers for the Global Module Fragment (if present)
652 We then call initializers for the current module.
653 We then call initializers for the Private Module Fragment (if present)
656 void CodeGenModule::EmitCXXModuleInitFunc(Module
*Primary
) {
657 assert(Primary
->isInterfaceOrPartition() &&
658 "The function should only be called for C++20 named module interface"
661 while (!CXXGlobalInits
.empty() && !CXXGlobalInits
.back())
662 CXXGlobalInits
.pop_back();
664 // As noted above, we create the function, even if it is empty.
665 // Module initializers for imported modules are emitted first.
667 // Collect all the modules that we import
668 llvm::SmallSetVector
<Module
*, 8> AllImports
;
669 // Ones that we export
670 for (auto I
: Primary
->Exports
)
671 AllImports
.insert(I
.getPointer());
672 // Ones that we only import.
673 for (Module
*M
: Primary
->Imports
)
674 AllImports
.insert(M
);
675 // Ones that we import in the global module fragment or the private module
677 for (Module
*SubM
: Primary
->submodules()) {
678 assert((SubM
->isGlobalModule() || SubM
->isPrivateModule()) &&
679 "The sub modules of C++20 module unit should only be global module "
680 "fragments or private module framents.");
681 assert(SubM
->Exports
.empty() &&
682 "The global mdoule fragments and the private module fragments are "
683 "not allowed to export import modules.");
684 for (Module
*M
: SubM
->Imports
)
685 AllImports
.insert(M
);
688 SmallVector
<llvm::Function
*, 8> ModuleInits
;
689 for (Module
*M
: AllImports
) {
690 // No Itanium initializer in header like modules.
691 if (M
->isHeaderLikeModule())
692 continue; // TODO: warn of mixed use of module map modules and C++20?
693 // We're allowed to skip the initialization if we are sure it doesn't
695 if (!M
->isNamedModuleInterfaceHasInit())
697 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
698 SmallString
<256> FnName
;
700 llvm::raw_svector_ostream
Out(FnName
);
701 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
702 .mangleModuleInitializer(M
, Out
);
704 assert(!GetGlobalValue(FnName
.str()) &&
705 "We should only have one use of the initializer call");
706 llvm::Function
*Fn
= llvm::Function::Create(
707 FTy
, llvm::Function::ExternalLinkage
, FnName
.str(), &getModule());
708 ModuleInits
.push_back(Fn
);
711 // Add any initializers with specified priority; this uses the same approach
712 // as EmitCXXGlobalInitFunc().
713 if (!PrioritizedCXXGlobalInits
.empty()) {
714 SmallVector
<llvm::Function
*, 8> LocalCXXGlobalInits
;
715 llvm::array_pod_sort(PrioritizedCXXGlobalInits
.begin(),
716 PrioritizedCXXGlobalInits
.end());
717 for (SmallVectorImpl
<GlobalInitData
>::iterator
718 I
= PrioritizedCXXGlobalInits
.begin(),
719 E
= PrioritizedCXXGlobalInits
.end();
721 SmallVectorImpl
<GlobalInitData
>::iterator PrioE
=
722 std::upper_bound(I
+ 1, E
, *I
, GlobalInitPriorityCmp());
724 for (; I
< PrioE
; ++I
)
725 ModuleInits
.push_back(I
->second
);
729 // Now append the ones without specified priority.
730 for (auto *F
: CXXGlobalInits
)
731 ModuleInits
.push_back(F
);
733 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
734 const CGFunctionInfo
&FI
= getTypes().arrangeNullaryFunction();
736 // We now build the initializer for this module, which has a mangled name
737 // as per the Itanium ABI . The action of the initializer is guarded so that
738 // each init is run just once (even though a module might be imported
739 // multiple times via nested use).
742 SmallString
<256> InitFnName
;
743 llvm::raw_svector_ostream
Out(InitFnName
);
744 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
745 .mangleModuleInitializer(Primary
, Out
);
746 Fn
= CreateGlobalInitOrCleanUpFunction(
747 FTy
, llvm::Twine(InitFnName
), FI
, SourceLocation(), false,
748 llvm::GlobalVariable::ExternalLinkage
);
750 // If we have a completely empty initializer then we do not want to create
751 // the guard variable.
752 ConstantAddress GuardAddr
= ConstantAddress::invalid();
753 if (!ModuleInits
.empty()) {
754 // Create the guard var.
755 llvm::GlobalVariable
*Guard
= new llvm::GlobalVariable(
756 getModule(), Int8Ty
, /*isConstant=*/false,
757 llvm::GlobalVariable::InternalLinkage
,
758 llvm::ConstantInt::get(Int8Ty
, 0), InitFnName
.str() + "__in_chrg");
759 CharUnits GuardAlign
= CharUnits::One();
760 Guard
->setAlignment(GuardAlign
.getAsAlign());
761 GuardAddr
= ConstantAddress(Guard
, Int8Ty
, GuardAlign
);
763 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn
, ModuleInits
,
767 // We allow for the case that a module object is added to a linked binary
768 // without a specific call to the the initializer. This also ensures that
769 // implementation partition initializers are called when the partition
770 // is not imported as an interface.
773 // See the comment in EmitCXXGlobalInitFunc about OpenCL global init
775 if (getLangOpts().OpenCL
) {
776 GenKernelArgMetadata(Fn
);
777 Fn
->setCallingConv(llvm::CallingConv::SPIR_KERNEL
);
780 assert(!getLangOpts().CUDA
|| !getLangOpts().CUDAIsDevice
||
781 getLangOpts().GPUAllowDeviceInit
);
782 if (getLangOpts().HIP
&& getLangOpts().CUDAIsDevice
) {
783 Fn
->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL
);
784 Fn
->addFnAttr("device-init");
787 // We are done with the inits.
789 PrioritizedCXXGlobalInits
.clear();
790 CXXGlobalInits
.clear();
794 static SmallString
<128> getTransformedFileName(llvm::Module
&M
) {
795 SmallString
<128> FileName
= llvm::sys::path::filename(M
.getName());
797 if (FileName
.empty())
800 for (size_t i
= 0; i
< FileName
.size(); ++i
) {
801 // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
802 // to be the set of C preprocessing numbers.
803 if (!isPreprocessingNumberBody(FileName
[i
]))
810 static std::string
getPrioritySuffix(unsigned int Priority
) {
811 assert(Priority
<= 65535 && "Priority should always be <= 65535.");
813 // Compute the function suffix from priority. Prepend with zeroes to make
814 // sure the function names are also ordered as priorities.
815 std::string PrioritySuffix
= llvm::utostr(Priority
);
816 PrioritySuffix
= std::string(6 - PrioritySuffix
.size(), '0') + PrioritySuffix
;
818 return PrioritySuffix
;
822 CodeGenModule::EmitCXXGlobalInitFunc() {
823 while (!CXXGlobalInits
.empty() && !CXXGlobalInits
.back())
824 CXXGlobalInits
.pop_back();
826 // When we import C++20 modules, we must run their initializers first.
827 SmallVector
<llvm::Function
*, 8> ModuleInits
;
828 if (CXX20ModuleInits
)
829 for (Module
*M
: ImportedModules
) {
830 // No Itanium initializer in header like modules.
831 if (M
->isHeaderLikeModule())
833 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
834 SmallString
<256> FnName
;
836 llvm::raw_svector_ostream
Out(FnName
);
837 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
838 .mangleModuleInitializer(M
, Out
);
840 assert(!GetGlobalValue(FnName
.str()) &&
841 "We should only have one use of the initializer call");
842 llvm::Function
*Fn
= llvm::Function::Create(
843 FTy
, llvm::Function::ExternalLinkage
, FnName
.str(), &getModule());
844 ModuleInits
.push_back(Fn
);
847 if (ModuleInits
.empty() && CXXGlobalInits
.empty() &&
848 PrioritizedCXXGlobalInits
.empty())
851 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
852 const CGFunctionInfo
&FI
= getTypes().arrangeNullaryFunction();
854 // Create our global prioritized initialization function.
855 if (!PrioritizedCXXGlobalInits
.empty()) {
856 SmallVector
<llvm::Function
*, 8> LocalCXXGlobalInits
;
857 llvm::array_pod_sort(PrioritizedCXXGlobalInits
.begin(),
858 PrioritizedCXXGlobalInits
.end());
859 // Iterate over "chunks" of ctors with same priority and emit each chunk
860 // into separate function. Note - everything is sorted first by priority,
861 // second - by lex order, so we emit ctor functions in proper order.
862 for (SmallVectorImpl
<GlobalInitData
>::iterator
863 I
= PrioritizedCXXGlobalInits
.begin(),
864 E
= PrioritizedCXXGlobalInits
.end(); I
!= E
; ) {
865 SmallVectorImpl
<GlobalInitData
>::iterator
866 PrioE
= std::upper_bound(I
+ 1, E
, *I
, GlobalInitPriorityCmp());
868 LocalCXXGlobalInits
.clear();
870 unsigned int Priority
= I
->first
.priority
;
871 llvm::Function
*Fn
= CreateGlobalInitOrCleanUpFunction(
872 FTy
, "_GLOBAL__I_" + getPrioritySuffix(Priority
), FI
);
874 // Prepend the module inits to the highest priority set.
875 if (!ModuleInits
.empty()) {
876 for (auto *F
: ModuleInits
)
877 LocalCXXGlobalInits
.push_back(F
);
881 for (; I
< PrioE
; ++I
)
882 LocalCXXGlobalInits
.push_back(I
->second
);
884 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn
, LocalCXXGlobalInits
);
885 AddGlobalCtor(Fn
, Priority
);
887 PrioritizedCXXGlobalInits
.clear();
890 if (getCXXABI().useSinitAndSterm() && ModuleInits
.empty() &&
891 CXXGlobalInits
.empty())
894 for (auto *F
: CXXGlobalInits
)
895 ModuleInits
.push_back(F
);
896 CXXGlobalInits
.clear();
898 // Include the filename in the symbol name. Including "sub_" matches gcc
899 // and makes sure these symbols appear lexicographically behind the symbols
900 // with priority emitted above. Module implementation units behave the same
901 // way as a non-modular TU with imports.
903 if (CXX20ModuleInits
&& getContext().getCurrentNamedModule() &&
904 !getContext().getCurrentNamedModule()->isModuleImplementation()) {
905 SmallString
<256> InitFnName
;
906 llvm::raw_svector_ostream
Out(InitFnName
);
907 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
908 .mangleModuleInitializer(getContext().getCurrentNamedModule(), Out
);
909 Fn
= CreateGlobalInitOrCleanUpFunction(
910 FTy
, llvm::Twine(InitFnName
), FI
, SourceLocation(), false,
911 llvm::GlobalVariable::ExternalLinkage
);
913 Fn
= CreateGlobalInitOrCleanUpFunction(
915 llvm::Twine("_GLOBAL__sub_I_", getTransformedFileName(getModule())),
918 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn
, ModuleInits
);
921 // In OpenCL global init functions must be converted to kernels in order to
922 // be able to launch them from the host.
923 // FIXME: Some more work might be needed to handle destructors correctly.
924 // Current initialization function makes use of function pointers callbacks.
925 // We can't support function pointers especially between host and device.
926 // However it seems global destruction has little meaning without any
927 // dynamic resource allocation on the device and program scope variables are
928 // destroyed by the runtime when program is released.
929 if (getLangOpts().OpenCL
) {
930 GenKernelArgMetadata(Fn
);
931 Fn
->setCallingConv(llvm::CallingConv::SPIR_KERNEL
);
934 assert(!getLangOpts().CUDA
|| !getLangOpts().CUDAIsDevice
||
935 getLangOpts().GPUAllowDeviceInit
);
936 if (getLangOpts().HIP
&& getLangOpts().CUDAIsDevice
) {
937 Fn
->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL
);
938 Fn
->addFnAttr("device-init");
944 void CodeGenModule::EmitCXXGlobalCleanUpFunc() {
945 if (CXXGlobalDtorsOrStermFinalizers
.empty() &&
946 PrioritizedCXXStermFinalizers
.empty())
949 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
950 const CGFunctionInfo
&FI
= getTypes().arrangeNullaryFunction();
952 // Create our global prioritized cleanup function.
953 if (!PrioritizedCXXStermFinalizers
.empty()) {
954 SmallVector
<CXXGlobalDtorsOrStermFinalizer_t
, 8> LocalCXXStermFinalizers
;
955 llvm::array_pod_sort(PrioritizedCXXStermFinalizers
.begin(),
956 PrioritizedCXXStermFinalizers
.end());
957 // Iterate over "chunks" of dtors with same priority and emit each chunk
958 // into separate function. Note - everything is sorted first by priority,
959 // second - by lex order, so we emit dtor functions in proper order.
960 for (SmallVectorImpl
<StermFinalizerData
>::iterator
961 I
= PrioritizedCXXStermFinalizers
.begin(),
962 E
= PrioritizedCXXStermFinalizers
.end();
964 SmallVectorImpl
<StermFinalizerData
>::iterator PrioE
=
965 std::upper_bound(I
+ 1, E
, *I
, StermFinalizerPriorityCmp());
967 LocalCXXStermFinalizers
.clear();
969 unsigned int Priority
= I
->first
.priority
;
970 llvm::Function
*Fn
= CreateGlobalInitOrCleanUpFunction(
971 FTy
, "_GLOBAL__a_" + getPrioritySuffix(Priority
), FI
);
973 for (; I
< PrioE
; ++I
) {
974 llvm::FunctionCallee DtorFn
= I
->second
;
975 LocalCXXStermFinalizers
.emplace_back(DtorFn
.getFunctionType(),
976 DtorFn
.getCallee(), nullptr);
979 CodeGenFunction(*this).GenerateCXXGlobalCleanUpFunc(
980 Fn
, LocalCXXStermFinalizers
);
981 AddGlobalDtor(Fn
, Priority
);
983 PrioritizedCXXStermFinalizers
.clear();
986 if (CXXGlobalDtorsOrStermFinalizers
.empty())
989 // Create our global cleanup function.
991 CreateGlobalInitOrCleanUpFunction(FTy
, "_GLOBAL__D_a", FI
);
993 CodeGenFunction(*this).GenerateCXXGlobalCleanUpFunc(
994 Fn
, CXXGlobalDtorsOrStermFinalizers
);
996 CXXGlobalDtorsOrStermFinalizers
.clear();
999 /// Emit the code necessary to initialize the given global variable.
1000 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function
*Fn
,
1002 llvm::GlobalVariable
*Addr
,
1004 // Check if we need to emit debug info for variable initializer.
1005 if (D
->hasAttr
<NoDebugAttr
>())
1006 DebugInfo
= nullptr; // disable debug info indefinitely for this function
1008 CurEHLocation
= D
->getBeginLoc();
1010 StartFunction(GlobalDecl(D
, DynamicInitKind::Initializer
),
1011 getContext().VoidTy
, Fn
, getTypes().arrangeNullaryFunction(),
1013 // Emit an artificial location for this function.
1014 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1016 // Use guarded initialization if the global variable is weak. This
1017 // occurs for, e.g., instantiated static data members and
1018 // definitions explicitly marked weak.
1020 // Also use guarded initialization for a variable with dynamic TLS and
1021 // unordered initialization. (If the initialization is ordered, the ABI
1022 // layer will guard the whole-TU initialization for us.)
1023 if (Addr
->hasWeakLinkage() || Addr
->hasLinkOnceLinkage() ||
1024 (D
->getTLSKind() == VarDecl::TLS_Dynamic
&&
1025 isTemplateInstantiation(D
->getTemplateSpecializationKind()))) {
1026 EmitCXXGuardedInit(*D
, Addr
, PerformInit
);
1028 EmitCXXGlobalVarDeclInit(*D
, Addr
, PerformInit
);
1031 if (getLangOpts().HLSL
)
1032 CGM
.getHLSLRuntime().annotateHLSLResource(D
, Addr
);
1038 CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function
*Fn
,
1039 ArrayRef
<llvm::Function
*> Decls
,
1040 ConstantAddress Guard
) {
1042 auto NL
= ApplyDebugLocation::CreateEmpty(*this);
1043 StartFunction(GlobalDecl(), getContext().VoidTy
, Fn
,
1044 getTypes().arrangeNullaryFunction(), FunctionArgList());
1045 // Emit an artificial location for this function.
1046 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1048 llvm::BasicBlock
*ExitBlock
= nullptr;
1049 if (Guard
.isValid()) {
1050 // If we have a guard variable, check whether we've already performed
1051 // these initializations. This happens for TLS initialization functions.
1052 llvm::Value
*GuardVal
= Builder
.CreateLoad(Guard
);
1053 llvm::Value
*Uninit
= Builder
.CreateIsNull(GuardVal
,
1054 "guard.uninitialized");
1055 llvm::BasicBlock
*InitBlock
= createBasicBlock("init");
1056 ExitBlock
= createBasicBlock("exit");
1057 EmitCXXGuardedInitBranch(Uninit
, InitBlock
, ExitBlock
,
1058 GuardKind::TlsGuard
, nullptr);
1059 EmitBlock(InitBlock
);
1060 // Mark as initialized before initializing anything else. If the
1061 // initializers use previously-initialized thread_local vars, that's
1062 // probably supposed to be OK, but the standard doesn't say.
1063 Builder
.CreateStore(llvm::ConstantInt::get(GuardVal
->getType(),1), Guard
);
1065 // The guard variable can't ever change again.
1068 CharUnits::fromQuantity(
1069 CGM
.getDataLayout().getTypeAllocSize(GuardVal
->getType())));
1072 RunCleanupsScope
Scope(*this);
1074 // When building in Objective-C++ ARC mode, create an autorelease pool
1075 // around the global initializers.
1076 if (getLangOpts().ObjCAutoRefCount
&& getLangOpts().CPlusPlus
) {
1077 llvm::Value
*token
= EmitObjCAutoreleasePoolPush();
1078 EmitObjCAutoreleasePoolCleanup(token
);
1081 for (unsigned i
= 0, e
= Decls
.size(); i
!= e
; ++i
)
1083 EmitRuntimeCall(Decls
[i
]);
1085 Scope
.ForceCleanup();
1088 Builder
.CreateBr(ExitBlock
);
1089 EmitBlock(ExitBlock
);
1096 void CodeGenFunction::GenerateCXXGlobalCleanUpFunc(
1098 ArrayRef
<std::tuple
<llvm::FunctionType
*, llvm::WeakTrackingVH
,
1100 DtorsOrStermFinalizers
) {
1102 auto NL
= ApplyDebugLocation::CreateEmpty(*this);
1103 StartFunction(GlobalDecl(), getContext().VoidTy
, Fn
,
1104 getTypes().arrangeNullaryFunction(), FunctionArgList());
1105 // Emit an artificial location for this function.
1106 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1108 // Emit the cleanups, in reverse order from construction.
1109 for (unsigned i
= 0, e
= DtorsOrStermFinalizers
.size(); i
!= e
; ++i
) {
1110 llvm::FunctionType
*CalleeTy
;
1111 llvm::Value
*Callee
;
1112 llvm::Constant
*Arg
;
1113 std::tie(CalleeTy
, Callee
, Arg
) = DtorsOrStermFinalizers
[e
- i
- 1];
1115 llvm::CallInst
*CI
= nullptr;
1116 if (Arg
== nullptr) {
1118 CGM
.getCXXABI().useSinitAndSterm() &&
1119 "Arg could not be nullptr unless using sinit and sterm functions.");
1120 CI
= Builder
.CreateCall(CalleeTy
, Callee
);
1122 CI
= Builder
.CreateCall(CalleeTy
, Callee
, Arg
);
1124 // Make sure the call and the callee agree on calling convention.
1125 if (llvm::Function
*F
= dyn_cast
<llvm::Function
>(Callee
))
1126 CI
->setCallingConv(F
->getCallingConv());
1133 /// generateDestroyHelper - Generates a helper function which, when
1134 /// invoked, destroys the given object. The address of the object
1135 /// should be in global memory.
1136 llvm::Function
*CodeGenFunction::generateDestroyHelper(
1137 Address addr
, QualType type
, Destroyer
*destroyer
,
1138 bool useEHCleanupForArray
, const VarDecl
*VD
) {
1139 FunctionArgList args
;
1140 ImplicitParamDecl
Dst(getContext(), getContext().VoidPtrTy
,
1141 ImplicitParamDecl::Other
);
1142 args
.push_back(&Dst
);
1144 const CGFunctionInfo
&FI
=
1145 CGM
.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy
, args
);
1146 llvm::FunctionType
*FTy
= CGM
.getTypes().GetFunctionType(FI
);
1147 llvm::Function
*fn
= CGM
.CreateGlobalInitOrCleanUpFunction(
1148 FTy
, "__cxx_global_array_dtor", FI
, VD
->getLocation());
1150 CurEHLocation
= VD
->getBeginLoc();
1152 StartFunction(GlobalDecl(VD
, DynamicInitKind::GlobalArrayDestructor
),
1153 getContext().VoidTy
, fn
, FI
, args
);
1154 // Emit an artificial location for this function.
1155 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1157 emitDestroy(addr
, type
, destroyer
, useEHCleanupForArray
);