xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGDeclCXX.cpp (revision e40139ff33b48b56a24c808b166b04b8ee6f5b21)
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 "CodeGenFunction.h"
14 #include "CGCXXABI.h"
15 #include "CGObjCRuntime.h"
16 #include "CGOpenMPRuntime.h"
17 #include "TargetInfo.h"
18 #include "clang/Basic/CodeGenOptions.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/Support/Path.h"
23 
24 using namespace clang;
25 using namespace CodeGen;
26 
27 static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
28                          ConstantAddress DeclPtr) {
29   assert(
30       (D.hasGlobalStorage() ||
31        (D.hasLocalStorage() && CGF.getContext().getLangOpts().OpenCLCPlusPlus)) &&
32       "VarDecl must have global or local (in the case of OpenCL) storage!");
33   assert(!D.getType()->isReferenceType() &&
34          "Should not call EmitDeclInit on a reference!");
35 
36   QualType type = D.getType();
37   LValue lv = CGF.MakeAddrLValue(DeclPtr, type);
38 
39   const Expr *Init = D.getInit();
40   switch (CGF.getEvaluationKind(type)) {
41   case TEK_Scalar: {
42     CodeGenModule &CGM = CGF.CGM;
43     if (lv.isObjCStrong())
44       CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
45                                                 DeclPtr, D.getTLSKind());
46     else if (lv.isObjCWeak())
47       CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
48                                               DeclPtr);
49     else
50       CGF.EmitScalarInit(Init, &D, lv, false);
51     return;
52   }
53   case TEK_Complex:
54     CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
55     return;
56   case TEK_Aggregate:
57     CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
58                                           AggValueSlot::DoesNotNeedGCBarriers,
59                                                   AggValueSlot::IsNotAliased,
60                                                   AggValueSlot::DoesNotOverlap));
61     return;
62   }
63   llvm_unreachable("bad evaluation kind");
64 }
65 
66 /// Emit code to cause the destruction of the given variable with
67 /// static storage duration.
68 static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
69                             ConstantAddress Addr) {
70   // Honor __attribute__((no_destroy)) and bail instead of attempting
71   // to emit a reference to a possibly nonexistent destructor, which
72   // in turn can cause a crash. This will result in a global constructor
73   // that isn't balanced out by a destructor call as intended by the
74   // attribute. This also checks for -fno-c++-static-destructors and
75   // bails even if the attribute is not present.
76   QualType::DestructionKind DtorKind = D.needsDestruction(CGF.getContext());
77 
78   // FIXME:  __attribute__((cleanup)) ?
79 
80   switch (DtorKind) {
81   case QualType::DK_none:
82     return;
83 
84   case QualType::DK_cxx_destructor:
85     break;
86 
87   case QualType::DK_objc_strong_lifetime:
88   case QualType::DK_objc_weak_lifetime:
89   case QualType::DK_nontrivial_c_struct:
90     // We don't care about releasing objects during process teardown.
91     assert(!D.getTLSKind() && "should have rejected this");
92     return;
93   }
94 
95   llvm::FunctionCallee Func;
96   llvm::Constant *Argument;
97 
98   CodeGenModule &CGM = CGF.CGM;
99   QualType Type = D.getType();
100 
101   // Special-case non-array C++ destructors, if they have the right signature.
102   // Under some ABIs, destructors return this instead of void, and cannot be
103   // passed directly to __cxa_atexit if the target does not allow this
104   // mismatch.
105   const CXXRecordDecl *Record = Type->getAsCXXRecordDecl();
106   bool CanRegisterDestructor =
107       Record && (!CGM.getCXXABI().HasThisReturn(
108                      GlobalDecl(Record->getDestructor(), Dtor_Complete)) ||
109                  CGM.getCXXABI().canCallMismatchedFunctionType());
110   // If __cxa_atexit is disabled via a flag, a different helper function is
111   // generated elsewhere which uses atexit instead, and it takes the destructor
112   // directly.
113   bool UsingExternalHelper = !CGM.getCodeGenOpts().CXAAtExit;
114   if (Record && (CanRegisterDestructor || UsingExternalHelper)) {
115     assert(!Record->hasTrivialDestructor());
116     CXXDestructorDecl *Dtor = Record->getDestructor();
117 
118     Func = CGM.getAddrAndTypeOfCXXStructor(GlobalDecl(Dtor, Dtor_Complete));
119     if (CGF.getContext().getLangOpts().OpenCL) {
120       auto DestAS =
121           CGM.getTargetCodeGenInfo().getAddrSpaceOfCxaAtexitPtrParam();
122       auto DestTy = CGF.getTypes().ConvertType(Type)->getPointerTo(
123           CGM.getContext().getTargetAddressSpace(DestAS));
124       auto SrcAS = D.getType().getQualifiers().getAddressSpace();
125       if (DestAS == SrcAS)
126         Argument = llvm::ConstantExpr::getBitCast(Addr.getPointer(), DestTy);
127       else
128         // FIXME: On addr space mismatch we are passing NULL. The generation
129         // of the global destructor function should be adjusted accordingly.
130         Argument = llvm::ConstantPointerNull::get(DestTy);
131     } else {
132       Argument = llvm::ConstantExpr::getBitCast(
133           Addr.getPointer(), CGF.getTypes().ConvertType(Type)->getPointerTo());
134     }
135   // Otherwise, the standard logic requires a helper function.
136   } else {
137     Func = CodeGenFunction(CGM)
138            .generateDestroyHelper(Addr, Type, CGF.getDestroyer(DtorKind),
139                                   CGF.needsEHCleanup(DtorKind), &D);
140     Argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
141   }
142 
143   CGM.getCXXABI().registerGlobalDtor(CGF, D, Func, Argument);
144 }
145 
146 /// Emit code to cause the variable at the given address to be considered as
147 /// constant from this point onwards.
148 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
149                               llvm::Constant *Addr) {
150   return CGF.EmitInvariantStart(
151       Addr, CGF.getContext().getTypeSizeInChars(D.getType()));
152 }
153 
154 void CodeGenFunction::EmitInvariantStart(llvm::Constant *Addr, CharUnits Size) {
155   // Do not emit the intrinsic if we're not optimizing.
156   if (!CGM.getCodeGenOpts().OptimizationLevel)
157     return;
158 
159   // Grab the llvm.invariant.start intrinsic.
160   llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
161   // Overloaded address space type.
162   llvm::Type *ObjectPtr[1] = {Int8PtrTy};
163   llvm::Function *InvariantStart = CGM.getIntrinsic(InvStartID, ObjectPtr);
164 
165   // Emit a call with the size in bytes of the object.
166   uint64_t Width = Size.getQuantity();
167   llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(Int64Ty, Width),
168                            llvm::ConstantExpr::getBitCast(Addr, Int8PtrTy)};
169   Builder.CreateCall(InvariantStart, Args);
170 }
171 
172 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
173                                                llvm::Constant *DeclPtr,
174                                                bool PerformInit) {
175 
176   const Expr *Init = D.getInit();
177   QualType T = D.getType();
178 
179   // The address space of a static local variable (DeclPtr) may be different
180   // from the address space of the "this" argument of the constructor. In that
181   // case, we need an addrspacecast before calling the constructor.
182   //
183   // struct StructWithCtor {
184   //   __device__ StructWithCtor() {...}
185   // };
186   // __device__ void foo() {
187   //   __shared__ StructWithCtor s;
188   //   ...
189   // }
190   //
191   // For example, in the above CUDA code, the static local variable s has a
192   // "shared" address space qualifier, but the constructor of StructWithCtor
193   // expects "this" in the "generic" address space.
194   unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(T);
195   unsigned ActualAddrSpace = DeclPtr->getType()->getPointerAddressSpace();
196   if (ActualAddrSpace != ExpectedAddrSpace) {
197     llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(T);
198     llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace);
199     DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy);
200   }
201 
202   ConstantAddress DeclAddr(DeclPtr, getContext().getDeclAlign(&D));
203 
204   if (!T->isReferenceType()) {
205     if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
206         D.hasAttr<OMPThreadPrivateDeclAttr>()) {
207       (void)CGM.getOpenMPRuntime().emitThreadPrivateVarDefinition(
208           &D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
209           PerformInit, this);
210     }
211     if (PerformInit)
212       EmitDeclInit(*this, D, DeclAddr);
213     if (CGM.isTypeConstant(D.getType(), true))
214       EmitDeclInvariant(*this, D, DeclPtr);
215     else
216       EmitDeclDestroy(*this, D, DeclAddr);
217     return;
218   }
219 
220   assert(PerformInit && "cannot have constant initializer which needs "
221          "destruction for reference");
222   RValue RV = EmitReferenceBindingToExpr(Init);
223   EmitStoreOfScalar(RV.getScalarVal(), DeclAddr, false, T);
224 }
225 
226 /// Create a stub function, suitable for being passed to atexit,
227 /// which passes the given address to the given destructor function.
228 llvm::Function *CodeGenFunction::createAtExitStub(const VarDecl &VD,
229                                                   llvm::FunctionCallee dtor,
230                                                   llvm::Constant *addr) {
231   // Get the destructor function type, void(*)(void).
232   llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
233   SmallString<256> FnName;
234   {
235     llvm::raw_svector_ostream Out(FnName);
236     CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
237   }
238 
239   const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
240   llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
241       ty, FnName.str(), FI, VD.getLocation());
242 
243   CodeGenFunction CGF(CGM);
244 
245   CGF.StartFunction(GlobalDecl(&VD, DynamicInitKind::AtExit),
246                     CGM.getContext().VoidTy, fn, FI, FunctionArgList());
247 
248   llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
249 
250  // Make sure the call and the callee agree on calling convention.
251   if (auto *dtorFn = dyn_cast<llvm::Function>(
252           dtor.getCallee()->stripPointerCastsAndAliases()))
253     call->setCallingConv(dtorFn->getCallingConv());
254 
255   CGF.FinishFunction();
256 
257   return fn;
258 }
259 
260 /// Register a global destructor using the C atexit runtime function.
261 void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD,
262                                                    llvm::FunctionCallee dtor,
263                                                    llvm::Constant *addr) {
264   // Create a function which calls the destructor.
265   llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
266   registerGlobalDtorWithAtExit(dtorStub);
267 }
268 
269 void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) {
270   // extern "C" int atexit(void (*f)(void));
271   llvm::FunctionType *atexitTy =
272     llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
273 
274   llvm::FunctionCallee atexit =
275       CGM.CreateRuntimeFunction(atexitTy, "atexit", llvm::AttributeList(),
276                                 /*Local=*/true);
277   if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit.getCallee()))
278     atexitFn->setDoesNotThrow();
279 
280   EmitNounwindRuntimeCall(atexit, dtorStub);
281 }
282 
283 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
284                                          llvm::GlobalVariable *DeclPtr,
285                                          bool PerformInit) {
286   // If we've been asked to forbid guard variables, emit an error now.
287   // This diagnostic is hard-coded for Darwin's use case;  we can find
288   // better phrasing if someone else needs it.
289   if (CGM.getCodeGenOpts().ForbidGuardVariables)
290     CGM.Error(D.getLocation(),
291               "this initialization requires a guard variable, which "
292               "the kernel does not support");
293 
294   CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
295 }
296 
297 void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value *NeedsInit,
298                                                llvm::BasicBlock *InitBlock,
299                                                llvm::BasicBlock *NoInitBlock,
300                                                GuardKind Kind,
301                                                const VarDecl *D) {
302   assert((Kind == GuardKind::TlsGuard || D) && "no guarded variable");
303 
304   // A guess at how many times we will enter the initialization of a
305   // variable, depending on the kind of variable.
306   static const uint64_t InitsPerTLSVar = 1024;
307   static const uint64_t InitsPerLocalVar = 1024 * 1024;
308 
309   llvm::MDNode *Weights;
310   if (Kind == GuardKind::VariableGuard && !D->isLocalVarDecl()) {
311     // For non-local variables, don't apply any weighting for now. Due to our
312     // use of COMDATs, we expect there to be at most one initialization of the
313     // variable per DSO, but we have no way to know how many DSOs will try to
314     // initialize the variable.
315     Weights = nullptr;
316   } else {
317     uint64_t NumInits;
318     // FIXME: For the TLS case, collect and use profiling information to
319     // determine a more accurate brach weight.
320     if (Kind == GuardKind::TlsGuard || D->getTLSKind())
321       NumInits = InitsPerTLSVar;
322     else
323       NumInits = InitsPerLocalVar;
324 
325     // The probability of us entering the initializer is
326     //   1 / (total number of times we attempt to initialize the variable).
327     llvm::MDBuilder MDHelper(CGM.getLLVMContext());
328     Weights = MDHelper.createBranchWeights(1, NumInits - 1);
329   }
330 
331   Builder.CreateCondBr(NeedsInit, InitBlock, NoInitBlock, Weights);
332 }
333 
334 llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
335     llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI,
336     SourceLocation Loc, bool TLS) {
337   llvm::Function *Fn =
338     llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
339                            Name, &getModule());
340   if (!getLangOpts().AppleKext && !TLS) {
341     // Set the section if needed.
342     if (const char *Section = getTarget().getStaticInitSectionSpecifier())
343       Fn->setSection(Section);
344   }
345 
346   SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
347 
348   Fn->setCallingConv(getRuntimeCC());
349 
350   if (!getLangOpts().Exceptions)
351     Fn->setDoesNotThrow();
352 
353   if (getLangOpts().Sanitize.has(SanitizerKind::Address) &&
354       !isInSanitizerBlacklist(SanitizerKind::Address, Fn, Loc))
355     Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
356 
357   if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) &&
358       !isInSanitizerBlacklist(SanitizerKind::KernelAddress, Fn, Loc))
359     Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
360 
361   if (getLangOpts().Sanitize.has(SanitizerKind::HWAddress) &&
362       !isInSanitizerBlacklist(SanitizerKind::HWAddress, Fn, Loc))
363     Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
364 
365   if (getLangOpts().Sanitize.has(SanitizerKind::KernelHWAddress) &&
366       !isInSanitizerBlacklist(SanitizerKind::KernelHWAddress, Fn, Loc))
367     Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
368 
369   if (getLangOpts().Sanitize.has(SanitizerKind::MemTag) &&
370       !isInSanitizerBlacklist(SanitizerKind::MemTag, Fn, Loc))
371     Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
372 
373   if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
374       !isInSanitizerBlacklist(SanitizerKind::Thread, Fn, Loc))
375     Fn->addFnAttr(llvm::Attribute::SanitizeThread);
376 
377   if (getLangOpts().Sanitize.has(SanitizerKind::Memory) &&
378       !isInSanitizerBlacklist(SanitizerKind::Memory, Fn, Loc))
379     Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
380 
381   if (getLangOpts().Sanitize.has(SanitizerKind::KernelMemory) &&
382       !isInSanitizerBlacklist(SanitizerKind::KernelMemory, Fn, Loc))
383     Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
384 
385   if (getLangOpts().Sanitize.has(SanitizerKind::SafeStack) &&
386       !isInSanitizerBlacklist(SanitizerKind::SafeStack, Fn, Loc))
387     Fn->addFnAttr(llvm::Attribute::SafeStack);
388 
389   if (getLangOpts().Sanitize.has(SanitizerKind::ShadowCallStack) &&
390       !isInSanitizerBlacklist(SanitizerKind::ShadowCallStack, Fn, Loc))
391     Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
392 
393   auto RASignKind = getCodeGenOpts().getSignReturnAddress();
394   if (RASignKind != CodeGenOptions::SignReturnAddressScope::None) {
395     Fn->addFnAttr("sign-return-address",
396                   RASignKind == CodeGenOptions::SignReturnAddressScope::All
397                       ? "all"
398                       : "non-leaf");
399     auto RASignKey = getCodeGenOpts().getSignReturnAddressKey();
400     Fn->addFnAttr("sign-return-address-key",
401                   RASignKey == CodeGenOptions::SignReturnAddressKeyValue::AKey
402                       ? "a_key"
403                       : "b_key");
404   }
405 
406   if (getCodeGenOpts().BranchTargetEnforcement)
407     Fn->addFnAttr("branch-target-enforcement");
408 
409   return Fn;
410 }
411 
412 /// Create a global pointer to a function that will initialize a global
413 /// variable.  The user has requested that this pointer be emitted in a specific
414 /// section.
415 void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
416                                           llvm::GlobalVariable *GV,
417                                           llvm::Function *InitFunc,
418                                           InitSegAttr *ISA) {
419   llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
420       TheModule, InitFunc->getType(), /*isConstant=*/true,
421       llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
422   PtrArray->setSection(ISA->getSection());
423   addUsedGlobal(PtrArray);
424 
425   // If the GV is already in a comdat group, then we have to join it.
426   if (llvm::Comdat *C = GV->getComdat())
427     PtrArray->setComdat(C);
428 }
429 
430 void
431 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
432                                             llvm::GlobalVariable *Addr,
433                                             bool PerformInit) {
434 
435   // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
436   // __constant__ and __shared__ variables defined in namespace scope,
437   // that are of class type, cannot have a non-empty constructor. All
438   // the checks have been done in Sema by now. Whatever initializers
439   // are allowed are empty and we just need to ignore them here.
440   if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
441       (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
442        D->hasAttr<CUDASharedAttr>()))
443     return;
444 
445   if (getLangOpts().OpenMP &&
446       getOpenMPRuntime().emitDeclareTargetVarDefinition(D, Addr, PerformInit))
447     return;
448 
449   // Check if we've already initialized this decl.
450   auto I = DelayedCXXInitPosition.find(D);
451   if (I != DelayedCXXInitPosition.end() && I->second == ~0U)
452     return;
453 
454   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
455   SmallString<256> FnName;
456   {
457     llvm::raw_svector_ostream Out(FnName);
458     getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
459   }
460 
461   // Create a variable initialization function.
462   llvm::Function *Fn =
463       CreateGlobalInitOrDestructFunction(FTy, FnName.str(),
464                                          getTypes().arrangeNullaryFunction(),
465                                          D->getLocation());
466 
467   auto *ISA = D->getAttr<InitSegAttr>();
468   CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
469                                                           PerformInit);
470 
471   llvm::GlobalVariable *COMDATKey =
472       supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr;
473 
474   if (D->getTLSKind()) {
475     // FIXME: Should we support init_priority for thread_local?
476     // FIXME: We only need to register one __cxa_thread_atexit function for the
477     // entire TU.
478     CXXThreadLocalInits.push_back(Fn);
479     CXXThreadLocalInitVars.push_back(D);
480   } else if (PerformInit && ISA) {
481     EmitPointerToInitFunc(D, Addr, Fn, ISA);
482   } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
483     OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size());
484     PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
485   } else if (isTemplateInstantiation(D->getTemplateSpecializationKind()) ||
486              getContext().GetGVALinkageForVariable(D) == GVA_DiscardableODR) {
487     // C++ [basic.start.init]p2:
488     //   Definitions of explicitly specialized class template static data
489     //   members have ordered initialization. Other class template static data
490     //   members (i.e., implicitly or explicitly instantiated specializations)
491     //   have unordered initialization.
492     //
493     // As a consequence, we can put them into their own llvm.global_ctors entry.
494     //
495     // If the global is externally visible, put the initializer into a COMDAT
496     // group with the global being initialized.  On most platforms, this is a
497     // minor startup time optimization.  In the MS C++ ABI, there are no guard
498     // variables, so this COMDAT key is required for correctness.
499     AddGlobalCtor(Fn, 65535, COMDATKey);
500     if (getTarget().getCXXABI().isMicrosoft() && COMDATKey) {
501       // In The MS C++, MS add template static data member in the linker
502       // drective.
503       addUsedGlobal(COMDATKey);
504     }
505   } else if (D->hasAttr<SelectAnyAttr>()) {
506     // SelectAny globals will be comdat-folded. Put the initializer into a
507     // COMDAT group associated with the global, so the initializers get folded
508     // too.
509     AddGlobalCtor(Fn, 65535, COMDATKey);
510   } else {
511     I = DelayedCXXInitPosition.find(D); // Re-do lookup in case of re-hash.
512     if (I == DelayedCXXInitPosition.end()) {
513       CXXGlobalInits.push_back(Fn);
514     } else if (I->second != ~0U) {
515       assert(I->second < CXXGlobalInits.size() &&
516              CXXGlobalInits[I->second] == nullptr);
517       CXXGlobalInits[I->second] = Fn;
518     }
519   }
520 
521   // Remember that we already emitted the initializer for this global.
522   DelayedCXXInitPosition[D] = ~0U;
523 }
524 
525 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
526   getCXXABI().EmitThreadLocalInitFuncs(
527       *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
528 
529   CXXThreadLocalInits.clear();
530   CXXThreadLocalInitVars.clear();
531   CXXThreadLocals.clear();
532 }
533 
534 void
535 CodeGenModule::EmitCXXGlobalInitFunc() {
536   while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
537     CXXGlobalInits.pop_back();
538 
539   if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
540     return;
541 
542   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
543   const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction();
544 
545   // Create our global initialization function.
546   if (!PrioritizedCXXGlobalInits.empty()) {
547     SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
548     llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
549                          PrioritizedCXXGlobalInits.end());
550     // Iterate over "chunks" of ctors with same priority and emit each chunk
551     // into separate function. Note - everything is sorted first by priority,
552     // second - by lex order, so we emit ctor functions in proper order.
553     for (SmallVectorImpl<GlobalInitData >::iterator
554            I = PrioritizedCXXGlobalInits.begin(),
555            E = PrioritizedCXXGlobalInits.end(); I != E; ) {
556       SmallVectorImpl<GlobalInitData >::iterator
557         PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
558 
559       LocalCXXGlobalInits.clear();
560       unsigned Priority = I->first.priority;
561       // Compute the function suffix from priority. Prepend with zeroes to make
562       // sure the function names are also ordered as priorities.
563       std::string PrioritySuffix = llvm::utostr(Priority);
564       // Priority is always <= 65535 (enforced by sema).
565       PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
566       llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
567           FTy, "_GLOBAL__I_" + PrioritySuffix, FI);
568 
569       for (; I < PrioE; ++I)
570         LocalCXXGlobalInits.push_back(I->second);
571 
572       CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
573       AddGlobalCtor(Fn, Priority);
574     }
575     PrioritizedCXXGlobalInits.clear();
576   }
577 
578   // Include the filename in the symbol name. Including "sub_" matches gcc and
579   // makes sure these symbols appear lexicographically behind the symbols with
580   // priority emitted above.
581   SmallString<128> FileName = llvm::sys::path::filename(getModule().getName());
582   if (FileName.empty())
583     FileName = "<null>";
584 
585   for (size_t i = 0; i < FileName.size(); ++i) {
586     // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
587     // to be the set of C preprocessing numbers.
588     if (!isPreprocessingNumberBody(FileName[i]))
589       FileName[i] = '_';
590   }
591 
592   llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
593       FTy, llvm::Twine("_GLOBAL__sub_I_", FileName), FI);
594 
595   CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
596   AddGlobalCtor(Fn);
597 
598   // In OpenCL global init functions must be converted to kernels in order to
599   // be able to launch them from the host.
600   // FIXME: Some more work might be needed to handle destructors correctly.
601   // Current initialization function makes use of function pointers callbacks.
602   // We can't support function pointers especially between host and device.
603   // However it seems global destruction has little meaning without any
604   // dynamic resource allocation on the device and program scope variables are
605   // destroyed by the runtime when program is released.
606   if (getLangOpts().OpenCL) {
607     GenOpenCLArgMetadata(Fn);
608     Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
609   }
610 
611   CXXGlobalInits.clear();
612 }
613 
614 void CodeGenModule::EmitCXXGlobalDtorFunc() {
615   if (CXXGlobalDtors.empty())
616     return;
617 
618   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
619 
620   // Create our global destructor function.
621   const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction();
622   llvm::Function *Fn =
623       CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a", FI);
624 
625   CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
626   AddGlobalDtor(Fn);
627 }
628 
629 /// Emit the code necessary to initialize the given global variable.
630 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
631                                                        const VarDecl *D,
632                                                  llvm::GlobalVariable *Addr,
633                                                        bool PerformInit) {
634   // Check if we need to emit debug info for variable initializer.
635   if (D->hasAttr<NoDebugAttr>())
636     DebugInfo = nullptr; // disable debug info indefinitely for this function
637 
638   CurEHLocation = D->getBeginLoc();
639 
640   StartFunction(GlobalDecl(D, DynamicInitKind::Initializer),
641                 getContext().VoidTy, Fn, getTypes().arrangeNullaryFunction(),
642                 FunctionArgList(), D->getLocation(),
643                 D->getInit()->getExprLoc());
644 
645   // Use guarded initialization if the global variable is weak. This
646   // occurs for, e.g., instantiated static data members and
647   // definitions explicitly marked weak.
648   //
649   // Also use guarded initialization for a variable with dynamic TLS and
650   // unordered initialization. (If the initialization is ordered, the ABI
651   // layer will guard the whole-TU initialization for us.)
652   if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage() ||
653       (D->getTLSKind() == VarDecl::TLS_Dynamic &&
654        isTemplateInstantiation(D->getTemplateSpecializationKind()))) {
655     EmitCXXGuardedInit(*D, Addr, PerformInit);
656   } else {
657     EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
658   }
659 
660   FinishFunction();
661 }
662 
663 void
664 CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
665                                            ArrayRef<llvm::Function *> Decls,
666                                            ConstantAddress Guard) {
667   {
668     auto NL = ApplyDebugLocation::CreateEmpty(*this);
669     StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
670                   getTypes().arrangeNullaryFunction(), FunctionArgList());
671     // Emit an artificial location for this function.
672     auto AL = ApplyDebugLocation::CreateArtificial(*this);
673 
674     llvm::BasicBlock *ExitBlock = nullptr;
675     if (Guard.isValid()) {
676       // If we have a guard variable, check whether we've already performed
677       // these initializations. This happens for TLS initialization functions.
678       llvm::Value *GuardVal = Builder.CreateLoad(Guard);
679       llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
680                                                  "guard.uninitialized");
681       llvm::BasicBlock *InitBlock = createBasicBlock("init");
682       ExitBlock = createBasicBlock("exit");
683       EmitCXXGuardedInitBranch(Uninit, InitBlock, ExitBlock,
684                                GuardKind::TlsGuard, nullptr);
685       EmitBlock(InitBlock);
686       // Mark as initialized before initializing anything else. If the
687       // initializers use previously-initialized thread_local vars, that's
688       // probably supposed to be OK, but the standard doesn't say.
689       Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
690 
691       // The guard variable can't ever change again.
692       EmitInvariantStart(
693           Guard.getPointer(),
694           CharUnits::fromQuantity(
695               CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
696     }
697 
698     RunCleanupsScope Scope(*this);
699 
700     // When building in Objective-C++ ARC mode, create an autorelease pool
701     // around the global initializers.
702     if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
703       llvm::Value *token = EmitObjCAutoreleasePoolPush();
704       EmitObjCAutoreleasePoolCleanup(token);
705     }
706 
707     for (unsigned i = 0, e = Decls.size(); i != e; ++i)
708       if (Decls[i])
709         EmitRuntimeCall(Decls[i]);
710 
711     Scope.ForceCleanup();
712 
713     if (ExitBlock) {
714       Builder.CreateBr(ExitBlock);
715       EmitBlock(ExitBlock);
716     }
717   }
718 
719   FinishFunction();
720 }
721 
722 void CodeGenFunction::GenerateCXXGlobalDtorsFunc(
723     llvm::Function *Fn,
724     const std::vector<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
725                                  llvm::Constant *>> &DtorsAndObjects) {
726   {
727     auto NL = ApplyDebugLocation::CreateEmpty(*this);
728     StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
729                   getTypes().arrangeNullaryFunction(), FunctionArgList());
730     // Emit an artificial location for this function.
731     auto AL = ApplyDebugLocation::CreateArtificial(*this);
732 
733     // Emit the dtors, in reverse order from construction.
734     for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
735       llvm::FunctionType *CalleeTy;
736       llvm::Value *Callee;
737       llvm::Constant *Arg;
738       std::tie(CalleeTy, Callee, Arg) = DtorsAndObjects[e - i - 1];
739       llvm::CallInst *CI = Builder.CreateCall(CalleeTy, Callee, Arg);
740       // Make sure the call and the callee agree on calling convention.
741       if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
742         CI->setCallingConv(F->getCallingConv());
743     }
744   }
745 
746   FinishFunction();
747 }
748 
749 /// generateDestroyHelper - Generates a helper function which, when
750 /// invoked, destroys the given object.  The address of the object
751 /// should be in global memory.
752 llvm::Function *CodeGenFunction::generateDestroyHelper(
753     Address addr, QualType type, Destroyer *destroyer,
754     bool useEHCleanupForArray, const VarDecl *VD) {
755   FunctionArgList args;
756   ImplicitParamDecl Dst(getContext(), getContext().VoidPtrTy,
757                         ImplicitParamDecl::Other);
758   args.push_back(&Dst);
759 
760   const CGFunctionInfo &FI =
761     CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, args);
762   llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
763   llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
764       FTy, "__cxx_global_array_dtor", FI, VD->getLocation());
765 
766   CurEHLocation = VD->getBeginLoc();
767 
768   StartFunction(VD, getContext().VoidTy, fn, FI, args);
769 
770   emitDestroy(addr, type, destroyer, useEHCleanupForArray);
771 
772   FinishFunction();
773 
774   return fn;
775 }
776