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"
21 using namespace clang;
22 using namespace CodeGen;
24 static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
25 llvm::Constant *DeclPtr) {
26 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
27 assert(!D.getType()->isReferenceType() &&
28 "Should not call EmitDeclInit on a reference!");
30 ASTContext &Context = CGF.getContext();
32 CharUnits alignment = Context.getDeclAlign(&D);
33 QualType type = D.getType();
34 LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
36 const Expr *Init = D.getInit();
37 switch (CGF.getEvaluationKind(type)) {
39 CodeGenModule &CGM = CGF.CGM;
40 if (lv.isObjCStrong())
41 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
42 DeclPtr, D.getTLSKind());
43 else if (lv.isObjCWeak())
44 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
47 CGF.EmitScalarInit(Init, &D, lv, false);
51 CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
54 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
55 AggValueSlot::DoesNotNeedGCBarriers,
56 AggValueSlot::IsNotAliased));
59 llvm_unreachable("bad evaluation kind");
62 /// Emit code to cause the destruction of the given variable with
63 /// static storage duration.
64 static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
65 llvm::Constant *addr) {
66 CodeGenModule &CGM = CGF.CGM;
68 // FIXME: __attribute__((cleanup)) ?
70 QualType type = D.getType();
71 QualType::DestructionKind dtorKind = type.isDestructedType();
74 case QualType::DK_none:
77 case QualType::DK_cxx_destructor:
80 case QualType::DK_objc_strong_lifetime:
81 case QualType::DK_objc_weak_lifetime:
82 // We don't care about releasing objects during process teardown.
83 assert(!D.getTLSKind() && "should have rejected this");
87 llvm::Constant *function;
88 llvm::Constant *argument;
90 // Special-case non-array C++ destructors, where there's a function
91 // with the right signature that we can just call.
92 const CXXRecordDecl *record = 0;
93 if (dtorKind == QualType::DK_cxx_destructor &&
94 (record = type->getAsCXXRecordDecl())) {
95 assert(!record->hasTrivialDestructor());
96 CXXDestructorDecl *dtor = record->getDestructor();
98 function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
99 argument = llvm::ConstantExpr::getBitCast(
100 addr, CGF.getTypes().ConvertType(type)->getPointerTo());
102 // Otherwise, the standard logic requires a helper function.
104 function = CodeGenFunction(CGM)
105 .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind),
106 CGF.needsEHCleanup(dtorKind), &D);
107 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
110 CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument);
113 /// Emit code to cause the variable at the given address to be considered as
114 /// constant from this point onwards.
115 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
116 llvm::Constant *Addr) {
117 // Don't emit the intrinsic if we're not optimizing.
118 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
121 // Grab the llvm.invariant.start intrinsic.
122 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
123 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
125 // Emit a call with the size in bytes of the object.
126 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
127 uint64_t Width = WidthChars.getQuantity();
128 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
129 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
130 CGF.Builder.CreateCall(InvariantStart, Args);
133 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
134 llvm::Constant *DeclPtr,
137 const Expr *Init = D.getInit();
138 QualType T = D.getType();
140 if (!T->isReferenceType()) {
142 EmitDeclInit(*this, D, DeclPtr);
143 if (CGM.isTypeConstant(D.getType(), true))
144 EmitDeclInvariant(*this, D, DeclPtr);
146 EmitDeclDestroy(*this, D, DeclPtr);
150 assert(PerformInit && "cannot have constant initializer which needs "
151 "destruction for reference");
152 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
153 RValue RV = EmitReferenceBindingToExpr(Init);
154 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
157 static llvm::Function *
158 CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
159 llvm::FunctionType *ty,
163 /// Create a stub function, suitable for being passed to atexit,
164 /// which passes the given address to the given destructor function.
165 static llvm::Constant *createAtExitStub(CodeGenModule &CGM, const VarDecl &VD,
166 llvm::Constant *dtor,
167 llvm::Constant *addr) {
168 // Get the destructor function type, void(*)(void).
169 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
170 SmallString<256> FnName;
172 llvm::raw_svector_ostream Out(FnName);
173 CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
176 CreateGlobalInitOrDestructFunction(CGM, ty, FnName.str());
178 CodeGenFunction CGF(CGM);
180 CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn,
181 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.getSanOpts().Address)
249 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
250 if (CGM.getSanOpts().Thread)
251 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
252 if (CGM.getSanOpts().Memory)
253 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
259 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
260 llvm::GlobalVariable *Addr,
262 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
263 SmallString<256> FnName;
265 llvm::raw_svector_ostream Out(FnName);
266 getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
269 // Create a variable initialization function.
271 CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str());
273 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
276 if (D->getTLSKind()) {
277 // FIXME: Should we support init_priority for thread_local?
278 // FIXME: Ideally, initialization of instantiated thread_local static data
279 // members of class templates should not trigger initialization of other
280 // entities in the TU.
281 // FIXME: We only need to register one __cxa_thread_atexit function for the
283 CXXThreadLocalInits.push_back(Fn);
284 } else if (D->hasAttr<InitPriorityAttr>()) {
285 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
286 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
287 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
288 DelayedCXXInitPosition.erase(D);
289 } else if (D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
290 D->getTemplateSpecializationKind() != TSK_Undeclared) {
291 // C++ [basic.start.init]p2:
292 // Definitions of explicitly specialized class template static data
293 // members have ordered initialization. Other class template static data
294 // members (i.e., implicitly or explicitly instantiated specializations)
295 // have unordered initialization.
297 // As a consequence, we can put them into their own llvm.global_ctors entry.
298 // This should allow GlobalOpt to fire more often, and allow us to implement
299 // the Microsoft C++ ABI, which uses COMDAT elimination to avoid double
302 DelayedCXXInitPosition.erase(D);
304 llvm::DenseMap<const Decl *, unsigned>::iterator I =
305 DelayedCXXInitPosition.find(D);
306 if (I == DelayedCXXInitPosition.end()) {
307 CXXGlobalInits.push_back(Fn);
309 assert(CXXGlobalInits[I->second] == 0);
310 CXXGlobalInits[I->second] = Fn;
311 DelayedCXXInitPosition.erase(I);
316 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
317 llvm::Function *InitFn = 0;
318 if (!CXXThreadLocalInits.empty()) {
319 // Generate a guarded initialization function.
320 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
321 InitFn = CreateGlobalInitOrDestructFunction(*this, FTy, "__tls_init",
323 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
324 getModule(), Int8Ty, false, llvm::GlobalVariable::InternalLinkage,
325 llvm::ConstantInt::get(Int8Ty, 0), "__tls_guard");
326 Guard->setThreadLocal(true);
327 CodeGenFunction(*this)
328 .GenerateCXXGlobalInitFunc(InitFn, CXXThreadLocalInits, Guard);
331 getCXXABI().EmitThreadLocalInitFuncs(CXXThreadLocals, InitFn);
333 CXXThreadLocalInits.clear();
334 CXXThreadLocals.clear();
338 CodeGenModule::EmitCXXGlobalInitFunc() {
339 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
340 CXXGlobalInits.pop_back();
342 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
345 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
348 // Create our global initialization function.
349 if (!PrioritizedCXXGlobalInits.empty()) {
350 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
351 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
352 PrioritizedCXXGlobalInits.end());
353 // Iterate over "chunks" of ctors with same priority and emit each chunk
354 // into separate function. Note - everything is sorted first by priority,
355 // second - by lex order, so we emit ctor functions in proper order.
356 for (SmallVectorImpl<GlobalInitData >::iterator
357 I = PrioritizedCXXGlobalInits.begin(),
358 E = PrioritizedCXXGlobalInits.end(); I != E; ) {
359 SmallVectorImpl<GlobalInitData >::iterator
360 PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
362 LocalCXXGlobalInits.clear();
363 unsigned Priority = I->first.priority;
364 // Compute the function suffix from priority. Prepend with zeroes to make
365 // sure the function names are also ordered as priorities.
366 std::string PrioritySuffix = llvm::utostr(Priority);
367 // Priority is always <= 65535 (enforced by sema)..
368 PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
370 CreateGlobalInitOrDestructFunction(*this, FTy,
371 "_GLOBAL__I_" + PrioritySuffix);
373 for (; I < PrioE; ++I)
374 LocalCXXGlobalInits.push_back(I->second);
376 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
377 AddGlobalCtor(Fn, Priority);
382 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
384 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
387 CXXGlobalInits.clear();
388 PrioritizedCXXGlobalInits.clear();
391 void CodeGenModule::EmitCXXGlobalDtorFunc() {
392 if (CXXGlobalDtors.empty())
395 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
397 // Create our global destructor function.
399 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
401 CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
405 /// Emit the code necessary to initialize the given global variable.
406 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
408 llvm::GlobalVariable *Addr,
410 // Check if we need to emit debug info for variable initializer.
411 if (D->hasAttr<NoDebugAttr>())
412 DebugInfo = NULL; // disable debug info indefinitely for this function
414 StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
415 getTypes().arrangeNullaryFunction(),
416 FunctionArgList(), D->getInit()->getExprLoc());
418 // Use guarded initialization if the global variable is weak. This
419 // occurs for, e.g., instantiated static data members and
420 // definitions explicitly marked weak.
421 if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
422 Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
423 EmitCXXGuardedInit(*D, Addr, PerformInit);
425 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
432 CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
433 ArrayRef<llvm::Constant *> Decls,
434 llvm::GlobalVariable *Guard) {
435 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
436 getTypes().arrangeNullaryFunction(),
437 FunctionArgList(), SourceLocation());
439 llvm::BasicBlock *ExitBlock = 0;
441 // If we have a guard variable, check whether we've already performed these
442 // initializations. This happens for TLS initialization functions.
443 llvm::Value *GuardVal = Builder.CreateLoad(Guard);
444 llvm::Value *Uninit = Builder.CreateIsNull(GuardVal, "guard.uninitialized");
445 // Mark as initialized before initializing anything else. If the
446 // initializers use previously-initialized thread_local vars, that's
447 // probably supposed to be OK, but the standard doesn't say.
448 Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(), 1), Guard);
449 llvm::BasicBlock *InitBlock = createBasicBlock("init");
450 ExitBlock = createBasicBlock("exit");
451 Builder.CreateCondBr(Uninit, InitBlock, ExitBlock);
452 EmitBlock(InitBlock);
455 RunCleanupsScope Scope(*this);
457 // When building in Objective-C++ ARC mode, create an autorelease pool
458 // around the global initializers.
459 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
460 llvm::Value *token = EmitObjCAutoreleasePoolPush();
461 EmitObjCAutoreleasePoolCleanup(token);
464 for (unsigned i = 0, e = Decls.size(); i != e; ++i)
466 EmitRuntimeCall(Decls[i]);
468 Scope.ForceCleanup();
471 Builder.CreateBr(ExitBlock);
472 EmitBlock(ExitBlock);
478 void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
479 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
481 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
482 getTypes().arrangeNullaryFunction(),
483 FunctionArgList(), SourceLocation());
485 // Emit the dtors, in reverse order from construction.
486 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
487 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
488 llvm::CallInst *CI = Builder.CreateCall(Callee,
489 DtorsAndObjects[e - i - 1].second);
490 // Make sure the call and the callee agree on calling convention.
491 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
492 CI->setCallingConv(F->getCallingConv());
498 /// generateDestroyHelper - Generates a helper function which, when
499 /// invoked, destroys the given object.
500 llvm::Function *CodeGenFunction::generateDestroyHelper(
501 llvm::Constant *addr, QualType type, Destroyer *destroyer,
502 bool useEHCleanupForArray, const VarDecl *VD) {
503 FunctionArgList args;
504 ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
505 args.push_back(&dst);
507 const CGFunctionInfo &FI =
508 CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args,
509 FunctionType::ExtInfo(),
511 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
513 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
515 StartFunction(VD, getContext().VoidTy, fn, FI, args, SourceLocation());
517 emitDestroy(addr, type, destroyer, useEHCleanupForArray);