1 //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with code generation of C++ declarations
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenFunction.h"
16 #include "CGObjCRuntime.h"
17 #include "clang/Frontend/CodeGenOptions.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Support/Path.h"
22 using namespace clang;
23 using namespace CodeGen;
25 static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
26 llvm::Constant *DeclPtr) {
27 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
28 assert(!D.getType()->isReferenceType() &&
29 "Should not call EmitDeclInit on a reference!");
31 ASTContext &Context = CGF.getContext();
33 CharUnits alignment = Context.getDeclAlign(&D);
34 QualType type = D.getType();
35 LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
37 const Expr *Init = D.getInit();
38 switch (CGF.getEvaluationKind(type)) {
40 CodeGenModule &CGM = CGF.CGM;
41 if (lv.isObjCStrong())
42 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
43 DeclPtr, D.getTLSKind());
44 else if (lv.isObjCWeak())
45 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
48 CGF.EmitScalarInit(Init, &D, lv, false);
52 CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
55 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
56 AggValueSlot::DoesNotNeedGCBarriers,
57 AggValueSlot::IsNotAliased));
60 llvm_unreachable("bad evaluation kind");
63 /// Emit code to cause the destruction of the given variable with
64 /// static storage duration.
65 static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
66 llvm::Constant *addr) {
67 CodeGenModule &CGM = CGF.CGM;
69 // FIXME: __attribute__((cleanup)) ?
71 QualType type = D.getType();
72 QualType::DestructionKind dtorKind = type.isDestructedType();
75 case QualType::DK_none:
78 case QualType::DK_cxx_destructor:
81 case QualType::DK_objc_strong_lifetime:
82 case QualType::DK_objc_weak_lifetime:
83 // We don't care about releasing objects during process teardown.
84 assert(!D.getTLSKind() && "should have rejected this");
88 llvm::Constant *function;
89 llvm::Constant *argument;
91 // Special-case non-array C++ destructors, where there's a function
92 // with the right signature that we can just call.
93 const CXXRecordDecl *record = nullptr;
94 if (dtorKind == QualType::DK_cxx_destructor &&
95 (record = type->getAsCXXRecordDecl())) {
96 assert(!record->hasTrivialDestructor());
97 CXXDestructorDecl *dtor = record->getDestructor();
99 function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
100 argument = llvm::ConstantExpr::getBitCast(
101 addr, CGF.getTypes().ConvertType(type)->getPointerTo());
103 // Otherwise, the standard logic requires a helper function.
105 function = CodeGenFunction(CGM)
106 .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind),
107 CGF.needsEHCleanup(dtorKind), &D);
108 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
111 CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument);
114 /// Emit code to cause the variable at the given address to be considered as
115 /// constant from this point onwards.
116 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
117 llvm::Constant *Addr) {
118 // Don't emit the intrinsic if we're not optimizing.
119 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
122 // Grab the llvm.invariant.start intrinsic.
123 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
124 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
126 // Emit a call with the size in bytes of the object.
127 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
128 uint64_t Width = WidthChars.getQuantity();
129 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
130 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
131 CGF.Builder.CreateCall(InvariantStart, Args);
134 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
135 llvm::Constant *DeclPtr,
138 const Expr *Init = D.getInit();
139 QualType T = D.getType();
141 if (!T->isReferenceType()) {
143 EmitDeclInit(*this, D, DeclPtr);
144 if (CGM.isTypeConstant(D.getType(), true))
145 EmitDeclInvariant(*this, D, DeclPtr);
147 EmitDeclDestroy(*this, D, DeclPtr);
151 assert(PerformInit && "cannot have constant initializer which needs "
152 "destruction for reference");
153 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
154 RValue RV = EmitReferenceBindingToExpr(Init);
155 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
158 static llvm::Function *
159 CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
160 llvm::FunctionType *ty,
164 /// Create a stub function, suitable for being passed to atexit,
165 /// which passes the given address to the given destructor function.
166 static llvm::Constant *createAtExitStub(CodeGenModule &CGM, const VarDecl &VD,
167 llvm::Constant *dtor,
168 llvm::Constant *addr) {
169 // Get the destructor function type, void(*)(void).
170 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
171 SmallString<256> FnName;
173 llvm::raw_svector_ostream Out(FnName);
174 CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
177 CreateGlobalInitOrDestructFunction(CGM, ty, FnName.str());
179 CodeGenFunction CGF(CGM);
181 CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn,
182 CGM.getTypes().arrangeNullaryFunction(), FunctionArgList());
184 llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
186 // Make sure the call and the callee agree on calling convention.
187 if (llvm::Function *dtorFn =
188 dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
189 call->setCallingConv(dtorFn->getCallingConv());
191 CGF.FinishFunction();
196 /// Register a global destructor using the C atexit runtime function.
197 void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD,
198 llvm::Constant *dtor,
199 llvm::Constant *addr) {
200 // Create a function which calls the destructor.
201 llvm::Constant *dtorStub = createAtExitStub(CGM, VD, dtor, addr);
203 // extern "C" int atexit(void (*f)(void));
204 llvm::FunctionType *atexitTy =
205 llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
207 llvm::Constant *atexit =
208 CGM.CreateRuntimeFunction(atexitTy, "atexit");
209 if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
210 atexitFn->setDoesNotThrow();
212 EmitNounwindRuntimeCall(atexit, dtorStub);
215 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
216 llvm::GlobalVariable *DeclPtr,
218 // If we've been asked to forbid guard variables, emit an error now.
219 // This diagnostic is hard-coded for Darwin's use case; we can find
220 // better phrasing if someone else needs it.
221 if (CGM.getCodeGenOpts().ForbidGuardVariables)
222 CGM.Error(D.getLocation(),
223 "this initialization requires a guard variable, which "
224 "the kernel does not support");
226 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
229 static llvm::Function *
230 CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
231 llvm::FunctionType *FTy,
232 const Twine &Name, bool TLS) {
234 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
235 Name, &CGM.getModule());
236 if (!CGM.getLangOpts().AppleKext && !TLS) {
237 // Set the section if needed.
238 if (const char *Section =
239 CGM.getTarget().getStaticInitSectionSpecifier())
240 Fn->setSection(Section);
243 Fn->setCallingConv(CGM.getRuntimeCC());
245 if (!CGM.getLangOpts().Exceptions)
246 Fn->setDoesNotThrow();
248 if (!CGM.getSanitizerBlacklist().isIn(*Fn)) {
249 if (CGM.getLangOpts().Sanitize.Address)
250 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
251 if (CGM.getLangOpts().Sanitize.Thread)
252 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
253 if (CGM.getLangOpts().Sanitize.Memory)
254 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
260 /// Create a global pointer to a function that will initialize a global
261 /// variable. The user has requested that this pointer be emitted in a specific
263 void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
264 llvm::GlobalVariable *GV,
265 llvm::Function *InitFunc,
267 llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
268 TheModule, InitFunc->getType(), /*isConstant=*/true,
269 llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
270 PtrArray->setSection(ISA->getSection());
271 addUsedGlobal(PtrArray);
273 // If the GV is already in a comdat group, then we have to join it.
274 llvm::Comdat *C = GV->getComdat();
276 // LinkOnce and Weak linkage are lowered down to a single-member comdat group.
277 // Make an explicit group so we can join it.
278 if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) {
279 C = TheModule.getOrInsertComdat(GV->getName());
283 PtrArray->setComdat(C);
287 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
288 llvm::GlobalVariable *Addr,
290 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
291 SmallString<256> FnName;
293 llvm::raw_svector_ostream Out(FnName);
294 getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
297 // Create a variable initialization function.
299 CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str());
301 auto *ISA = D->getAttr<InitSegAttr>();
302 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
304 if (D->getTLSKind()) {
305 // FIXME: Should we support init_priority for thread_local?
306 // FIXME: Ideally, initialization of instantiated thread_local static data
307 // members of class templates should not trigger initialization of other
308 // entities in the TU.
309 // FIXME: We only need to register one __cxa_thread_atexit function for the
311 CXXThreadLocalInits.push_back(Fn);
312 } else if (PerformInit && ISA) {
313 EmitPointerToInitFunc(D, Addr, Fn, ISA);
314 DelayedCXXInitPosition.erase(D);
315 } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
316 OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size());
317 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
318 DelayedCXXInitPosition.erase(D);
319 } else if (D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
320 D->getTemplateSpecializationKind() != TSK_Undeclared) {
321 // C++ [basic.start.init]p2:
322 // Definitions of explicitly specialized class template static data
323 // members have ordered initialization. Other class template static data
324 // members (i.e., implicitly or explicitly instantiated specializations)
325 // have unordered initialization.
327 // As a consequence, we can put them into their own llvm.global_ctors entry.
329 // In addition, put the initializer into a COMDAT group with the global
330 // being initialized. On most platforms, this is a minor startup time
331 // optimization. In the MS C++ ABI, there are no guard variables, so this
332 // COMDAT key is required for correctness.
333 AddGlobalCtor(Fn, 65535, Addr);
334 DelayedCXXInitPosition.erase(D);
336 llvm::DenseMap<const Decl *, unsigned>::iterator I =
337 DelayedCXXInitPosition.find(D);
338 if (I == DelayedCXXInitPosition.end()) {
339 CXXGlobalInits.push_back(Fn);
341 assert(CXXGlobalInits[I->second] == nullptr);
342 CXXGlobalInits[I->second] = Fn;
343 DelayedCXXInitPosition.erase(I);
348 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
349 llvm::Function *InitFn = nullptr;
350 if (!CXXThreadLocalInits.empty()) {
351 // Generate a guarded initialization function.
352 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
353 InitFn = CreateGlobalInitOrDestructFunction(*this, FTy, "__tls_init",
355 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
356 getModule(), Int8Ty, false, llvm::GlobalVariable::InternalLinkage,
357 llvm::ConstantInt::get(Int8Ty, 0), "__tls_guard");
358 Guard->setThreadLocal(true);
359 CodeGenFunction(*this)
360 .GenerateCXXGlobalInitFunc(InitFn, CXXThreadLocalInits, Guard);
363 getCXXABI().EmitThreadLocalInitFuncs(CXXThreadLocals, InitFn);
365 CXXThreadLocalInits.clear();
366 CXXThreadLocals.clear();
370 CodeGenModule::EmitCXXGlobalInitFunc() {
371 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
372 CXXGlobalInits.pop_back();
374 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
377 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
380 // Create our global initialization function.
381 if (!PrioritizedCXXGlobalInits.empty()) {
382 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
383 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
384 PrioritizedCXXGlobalInits.end());
385 // Iterate over "chunks" of ctors with same priority and emit each chunk
386 // into separate function. Note - everything is sorted first by priority,
387 // second - by lex order, so we emit ctor functions in proper order.
388 for (SmallVectorImpl<GlobalInitData >::iterator
389 I = PrioritizedCXXGlobalInits.begin(),
390 E = PrioritizedCXXGlobalInits.end(); I != E; ) {
391 SmallVectorImpl<GlobalInitData >::iterator
392 PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
394 LocalCXXGlobalInits.clear();
395 unsigned Priority = I->first.priority;
396 // Compute the function suffix from priority. Prepend with zeroes to make
397 // sure the function names are also ordered as priorities.
398 std::string PrioritySuffix = llvm::utostr(Priority);
399 // Priority is always <= 65535 (enforced by sema).
400 PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
402 CreateGlobalInitOrDestructFunction(*this, FTy,
403 "_GLOBAL__I_" + PrioritySuffix);
405 for (; I < PrioE; ++I)
406 LocalCXXGlobalInits.push_back(I->second);
408 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
409 AddGlobalCtor(Fn, Priority);
413 // Include the filename in the symbol name. Including "sub_" matches gcc and
414 // makes sure these symbols appear lexicographically behind the symbols with
415 // priority emitted above.
416 SourceManager &SM = Context.getSourceManager();
417 SmallString<128> FileName(llvm::sys::path::filename(
418 SM.getFileEntryForID(SM.getMainFileID())->getName()));
419 for (size_t i = 0; i < FileName.size(); ++i) {
420 // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
421 // to be the set of C preprocessing numbers.
422 if (!isPreprocessingNumberBody(FileName[i]))
425 llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
426 *this, FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
428 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
431 CXXGlobalInits.clear();
432 PrioritizedCXXGlobalInits.clear();
435 void CodeGenModule::EmitCXXGlobalDtorFunc() {
436 if (CXXGlobalDtors.empty())
439 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
441 // Create our global destructor function.
443 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
445 CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
449 /// Emit the code necessary to initialize the given global variable.
450 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
452 llvm::GlobalVariable *Addr,
454 // Check if we need to emit debug info for variable initializer.
455 if (D->hasAttr<NoDebugAttr>())
456 DebugInfo = nullptr; // disable debug info indefinitely for this function
458 StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
459 getTypes().arrangeNullaryFunction(),
460 FunctionArgList(), D->getLocation(),
461 D->getInit()->getExprLoc());
463 // Use guarded initialization if the global variable is weak. This
464 // occurs for, e.g., instantiated static data members and
465 // definitions explicitly marked weak.
466 if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage()) {
467 EmitCXXGuardedInit(*D, Addr, PerformInit);
469 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
476 CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
477 ArrayRef<llvm::Constant *> Decls,
478 llvm::GlobalVariable *Guard) {
480 ArtificialLocation AL(*this, Builder);
481 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
482 getTypes().arrangeNullaryFunction(), FunctionArgList());
483 // Emit an artificial location for this function.
486 llvm::BasicBlock *ExitBlock = nullptr;
488 // If we have a guard variable, check whether we've already performed
489 // these initializations. This happens for TLS initialization functions.
490 llvm::Value *GuardVal = Builder.CreateLoad(Guard);
491 llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
492 "guard.uninitialized");
493 // Mark as initialized before initializing anything else. If the
494 // initializers use previously-initialized thread_local vars, that's
495 // probably supposed to be OK, but the standard doesn't say.
496 Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
497 llvm::BasicBlock *InitBlock = createBasicBlock("init");
498 ExitBlock = createBasicBlock("exit");
499 Builder.CreateCondBr(Uninit, InitBlock, ExitBlock);
500 EmitBlock(InitBlock);
503 RunCleanupsScope Scope(*this);
505 // When building in Objective-C++ ARC mode, create an autorelease pool
506 // around the global initializers.
507 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
508 llvm::Value *token = EmitObjCAutoreleasePoolPush();
509 EmitObjCAutoreleasePoolCleanup(token);
512 for (unsigned i = 0, e = Decls.size(); i != e; ++i)
514 EmitRuntimeCall(Decls[i]);
516 Scope.ForceCleanup();
519 Builder.CreateBr(ExitBlock);
520 EmitBlock(ExitBlock);
527 void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
528 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
531 ArtificialLocation AL(*this, Builder);
532 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
533 getTypes().arrangeNullaryFunction(), FunctionArgList());
534 // Emit an artificial location for this function.
537 // Emit the dtors, in reverse order from construction.
538 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
539 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
540 llvm::CallInst *CI = Builder.CreateCall(Callee,
541 DtorsAndObjects[e - i - 1].second);
542 // Make sure the call and the callee agree on calling convention.
543 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
544 CI->setCallingConv(F->getCallingConv());
551 /// generateDestroyHelper - Generates a helper function which, when
552 /// invoked, destroys the given object.
553 llvm::Function *CodeGenFunction::generateDestroyHelper(
554 llvm::Constant *addr, QualType type, Destroyer *destroyer,
555 bool useEHCleanupForArray, const VarDecl *VD) {
556 FunctionArgList args;
557 ImplicitParamDecl dst(getContext(), nullptr, SourceLocation(), nullptr,
558 getContext().VoidPtrTy);
559 args.push_back(&dst);
561 const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
562 getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
563 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
565 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
567 StartFunction(VD, getContext().VoidTy, fn, FI, args);
569 emitDestroy(addr, type, destroyer, useEHCleanupForArray);