blob: 236337b4034d82d7d8ca0ea04cc83339e8a01d91 [file] [log] [blame]
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +00001//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with code generation of C++ declarations
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
John McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CGObjCRuntime.h"
Stephen Hines176edba2014-12-01 14:53:08 -080017#include "CGOpenMPRuntime.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000018#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000020#include "llvm/IR/Intrinsics.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070021#include "llvm/Support/Path.h"
Douglas Gregor86a3a032010-05-16 01:24:12 +000022
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000023using namespace clang;
24using namespace CodeGen;
25
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000026static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
27 llvm::Constant *DeclPtr) {
28 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
29 assert(!D.getType()->isReferenceType() &&
30 "Should not call EmitDeclInit on a reference!");
31
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000032 ASTContext &Context = CGF.getContext();
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000033
Eli Friedman6da2c712011-12-03 04:14:32 +000034 CharUnits alignment = Context.getDeclAlign(&D);
John McCalla07398e2011-06-16 04:16:24 +000035 QualType type = D.getType();
36 LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
37
38 const Expr *Init = D.getInit();
John McCall9d232c82013-03-07 21:37:08 +000039 switch (CGF.getEvaluationKind(type)) {
40 case TEK_Scalar: {
Fariborz Jahanianec805122011-01-13 20:00:54 +000041 CodeGenModule &CGM = CGF.CGM;
John McCalla07398e2011-06-16 04:16:24 +000042 if (lv.isObjCStrong())
John McCallf85e1932011-06-15 23:02:42 +000043 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
Richard Smith38afbc72013-04-13 02:43:54 +000044 DeclPtr, D.getTLSKind());
John McCalla07398e2011-06-16 04:16:24 +000045 else if (lv.isObjCWeak())
46 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
47 DeclPtr);
Fariborz Jahanianec805122011-01-13 20:00:54 +000048 else
John McCalla07398e2011-06-16 04:16:24 +000049 CGF.EmitScalarInit(Init, &D, lv, false);
John McCall9d232c82013-03-07 21:37:08 +000050 return;
51 }
52 case TEK_Complex:
53 CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
54 return;
55 case TEK_Aggregate:
Chad Rosier649b4a12012-03-29 17:37:10 +000056 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
57 AggValueSlot::DoesNotNeedGCBarriers,
58 AggValueSlot::IsNotAliased));
John McCall9d232c82013-03-07 21:37:08 +000059 return;
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000060 }
John McCall9d232c82013-03-07 21:37:08 +000061 llvm_unreachable("bad evaluation kind");
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000062}
63
John McCall5cd91b52010-09-08 01:44:27 +000064/// Emit code to cause the destruction of the given variable with
65/// static storage duration.
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000066static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
John McCalla91f6662011-07-13 03:01:35 +000067 llvm::Constant *addr) {
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000068 CodeGenModule &CGM = CGF.CGM;
John McCalla91f6662011-07-13 03:01:35 +000069
70 // FIXME: __attribute__((cleanup)) ?
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000071
John McCalla91f6662011-07-13 03:01:35 +000072 QualType type = D.getType();
73 QualType::DestructionKind dtorKind = type.isDestructedType();
74
75 switch (dtorKind) {
76 case QualType::DK_none:
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000077 return;
John McCalla91f6662011-07-13 03:01:35 +000078
79 case QualType::DK_cxx_destructor:
80 break;
81
82 case QualType::DK_objc_strong_lifetime:
83 case QualType::DK_objc_weak_lifetime:
84 // We don't care about releasing objects during process teardown.
Richard Smith04e51762013-04-14 23:01:42 +000085 assert(!D.getTLSKind() && "should have rejected this");
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000086 return;
John McCalla91f6662011-07-13 03:01:35 +000087 }
88
89 llvm::Constant *function;
90 llvm::Constant *argument;
91
92 // Special-case non-array C++ destructors, where there's a function
93 // with the right signature that we can just call.
Stephen Hines6bcf27b2014-05-29 04:14:42 -070094 const CXXRecordDecl *record = nullptr;
John McCalla91f6662011-07-13 03:01:35 +000095 if (dtorKind == QualType::DK_cxx_destructor &&
96 (record = type->getAsCXXRecordDecl())) {
97 assert(!record->hasTrivialDestructor());
98 CXXDestructorDecl *dtor = record->getDestructor();
99
Stephen Hines176edba2014-12-01 14:53:08 -0800100 function = CGM.getAddrOfCXXStructor(dtor, StructorType::Complete);
Timur Iskhodzhanov9a3be4c2013-10-02 16:03:16 +0000101 argument = llvm::ConstantExpr::getBitCast(
102 addr, CGF.getTypes().ConvertType(type)->getPointerTo());
John McCalla91f6662011-07-13 03:01:35 +0000103
104 // Otherwise, the standard logic requires a helper function.
105 } else {
David Blaikiec7971a92013-08-27 23:57:18 +0000106 function = CodeGenFunction(CGM)
107 .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind),
108 CGF.needsEHCleanup(dtorKind), &D);
John McCalla91f6662011-07-13 03:01:35 +0000109 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
110 }
111
Richard Smith04e51762013-04-14 23:01:42 +0000112 CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000113}
114
Richard Smithabb94322012-02-17 07:31:37 +0000115/// Emit code to cause the variable at the given address to be considered as
116/// constant from this point onwards.
Nick Lewyckyef784462012-02-21 00:26:58 +0000117static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
118 llvm::Constant *Addr) {
Richard Smith00a8c3f2012-02-17 20:12:52 +0000119 // Don't emit the intrinsic if we're not optimizing.
120 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
121 return;
122
Richard Smithabb94322012-02-17 07:31:37 +0000123 // Grab the llvm.invariant.start intrinsic.
124 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
125 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
126
Nick Lewyckyef784462012-02-21 00:26:58 +0000127 // Emit a call with the size in bytes of the object.
128 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
129 uint64_t Width = WidthChars.getQuantity();
130 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
Richard Smithabb94322012-02-17 07:31:37 +0000131 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
132 CGF.Builder.CreateCall(InvariantStart, Args);
133}
134
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000135void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Richard Smith7ca48502012-02-13 22:16:19 +0000136 llvm::Constant *DeclPtr,
137 bool PerformInit) {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000138
139 const Expr *Init = D.getInit();
140 QualType T = D.getType();
141
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700142 // The address space of a static local variable (DeclPtr) may be different
143 // from the address space of the "this" argument of the constructor. In that
144 // case, we need an addrspacecast before calling the constructor.
145 //
146 // struct StructWithCtor {
147 // __device__ StructWithCtor() {...}
148 // };
149 // __device__ void foo() {
150 // __shared__ StructWithCtor s;
151 // ...
152 // }
153 //
154 // For example, in the above CUDA code, the static local variable s has a
155 // "shared" address space qualifier, but the constructor of StructWithCtor
156 // expects "this" in the "generic" address space.
157 unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(T);
158 unsigned ActualAddrSpace = DeclPtr->getType()->getPointerAddressSpace();
159 if (ActualAddrSpace != ExpectedAddrSpace) {
160 llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(T);
161 llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace);
162 DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy);
163 }
164
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000165 if (!T->isReferenceType()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800166 if (getLangOpts().OpenMP && D.hasAttr<OMPThreadPrivateDeclAttr>())
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700167 (void)CGM.getOpenMPRuntime().emitThreadPrivateVarDefinition(
Stephen Hines176edba2014-12-01 14:53:08 -0800168 &D, DeclPtr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
169 PerformInit, this);
Richard Smith7ca48502012-02-13 22:16:19 +0000170 if (PerformInit)
171 EmitDeclInit(*this, D, DeclPtr);
Richard Smithabb94322012-02-17 07:31:37 +0000172 if (CGM.isTypeConstant(D.getType(), true))
Nick Lewyckyef784462012-02-21 00:26:58 +0000173 EmitDeclInvariant(*this, D, DeclPtr);
Richard Smithabb94322012-02-17 07:31:37 +0000174 else
175 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000176 return;
177 }
Anders Carlsson045a6d82010-06-27 17:52:15 +0000178
Richard Smith7ca48502012-02-13 22:16:19 +0000179 assert(PerformInit && "cannot have constant initializer which needs "
180 "destruction for reference");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000181 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
Richard Smithd4ec5622013-06-12 23:38:09 +0000182 RValue RV = EmitReferenceBindingToExpr(Init);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000183 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000184}
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000185
John McCall30fa3702012-04-06 18:21:06 +0000186/// Create a stub function, suitable for being passed to atexit,
187/// which passes the given address to the given destructor function.
Stephen Hines176edba2014-12-01 14:53:08 -0800188llvm::Constant *CodeGenFunction::createAtExitStub(const VarDecl &VD,
189 llvm::Constant *dtor,
190 llvm::Constant *addr) {
John McCall30fa3702012-04-06 18:21:06 +0000191 // Get the destructor function type, void(*)(void).
192 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000193 SmallString<256> FnName;
194 {
195 llvm::raw_svector_ostream Out(FnName);
196 CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
197 }
Stephen Hines176edba2014-12-01 14:53:08 -0800198 llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(ty, FnName.str(),
199 VD.getLocation());
John McCall30fa3702012-04-06 18:21:06 +0000200
201 CodeGenFunction CGF(CGM);
202
David Blaikiec7971a92013-08-27 23:57:18 +0000203 CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700204 CGM.getTypes().arrangeNullaryFunction(), FunctionArgList());
John McCall30fa3702012-04-06 18:21:06 +0000205
206 llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
207
208 // Make sure the call and the callee agree on calling convention.
209 if (llvm::Function *dtorFn =
210 dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
211 call->setCallingConv(dtorFn->getCallingConv());
212
213 CGF.FinishFunction();
214
215 return fn;
216}
217
John McCall20bb1752012-05-01 06:13:13 +0000218/// Register a global destructor using the C atexit runtime function.
David Blaikiec7971a92013-08-27 23:57:18 +0000219void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD,
220 llvm::Constant *dtor,
John McCall20bb1752012-05-01 06:13:13 +0000221 llvm::Constant *addr) {
John McCall30fa3702012-04-06 18:21:06 +0000222 // Create a function which calls the destructor.
Stephen Hines176edba2014-12-01 14:53:08 -0800223 llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
John McCall30fa3702012-04-06 18:21:06 +0000224
225 // extern "C" int atexit(void (*f)(void));
226 llvm::FunctionType *atexitTy =
John McCall20bb1752012-05-01 06:13:13 +0000227 llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
John McCall30fa3702012-04-06 18:21:06 +0000228
229 llvm::Constant *atexit =
John McCall20bb1752012-05-01 06:13:13 +0000230 CGM.CreateRuntimeFunction(atexitTy, "atexit");
John McCall30fa3702012-04-06 18:21:06 +0000231 if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
232 atexitFn->setDoesNotThrow();
233
John McCallbd7370a2013-02-28 19:01:20 +0000234 EmitNounwindRuntimeCall(atexit, dtorStub);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000235}
236
John McCall3030eb82010-11-06 09:44:32 +0000237void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000238 llvm::GlobalVariable *DeclPtr,
Richard Smith7ca48502012-02-13 22:16:19 +0000239 bool PerformInit) {
John McCall32096692011-03-18 02:56:14 +0000240 // If we've been asked to forbid guard variables, emit an error now.
241 // This diagnostic is hard-coded for Darwin's use case; we can find
242 // better phrasing if someone else needs it.
243 if (CGM.getCodeGenOpts().ForbidGuardVariables)
244 CGM.Error(D.getLocation(),
245 "this initialization requires a guard variable, which "
246 "the kernel does not support");
247
Chandler Carruth0f30a122012-03-30 19:44:53 +0000248 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000249}
250
Stephen Hines176edba2014-12-01 14:53:08 -0800251llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
252 llvm::FunctionType *FTy, const Twine &Name, SourceLocation Loc, bool TLS) {
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000253 llvm::Function *Fn =
254 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
Stephen Hines176edba2014-12-01 14:53:08 -0800255 Name, &getModule());
256 if (!getLangOpts().AppleKext && !TLS) {
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000257 // Set the section if needed.
Stephen Hines176edba2014-12-01 14:53:08 -0800258 if (const char *Section = getTarget().getStaticInitSectionSpecifier())
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000259 Fn->setSection(Section);
260 }
Anders Carlsson18af3682010-06-08 22:47:50 +0000261
Stephen Hines176edba2014-12-01 14:53:08 -0800262 Fn->setCallingConv(getRuntimeCC());
John McCallbd7370a2013-02-28 19:01:20 +0000263
Stephen Hines176edba2014-12-01 14:53:08 -0800264 if (!getLangOpts().Exceptions)
John McCall044cc542010-07-06 04:38:10 +0000265 Fn->setDoesNotThrow();
266
Stephen Hines176edba2014-12-01 14:53:08 -0800267 if (!isInSanitizerBlacklist(Fn, Loc)) {
268 if (getLangOpts().Sanitize.has(SanitizerKind::Address))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700269 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
Stephen Hines176edba2014-12-01 14:53:08 -0800270 if (getLangOpts().Sanitize.has(SanitizerKind::Thread))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700271 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
Stephen Hines176edba2014-12-01 14:53:08 -0800272 if (getLangOpts().Sanitize.has(SanitizerKind::Memory))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700273 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
274 }
Kostya Serebryanyb9d2b3b2012-06-26 08:56:33 +0000275
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000276 return Fn;
277}
278
Stephen Hines176edba2014-12-01 14:53:08 -0800279/// Create a global pointer to a function that will initialize a global
280/// variable. The user has requested that this pointer be emitted in a specific
281/// section.
282void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
283 llvm::GlobalVariable *GV,
284 llvm::Function *InitFunc,
285 InitSegAttr *ISA) {
286 llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
287 TheModule, InitFunc->getType(), /*isConstant=*/true,
288 llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
289 PtrArray->setSection(ISA->getSection());
290 addUsedGlobal(PtrArray);
291
292 // If the GV is already in a comdat group, then we have to join it.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700293 if (llvm::Comdat *C = GV->getComdat())
Stephen Hines176edba2014-12-01 14:53:08 -0800294 PtrArray->setComdat(C);
295}
296
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000297void
John McCall3030eb82010-11-06 09:44:32 +0000298CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000299 llvm::GlobalVariable *Addr,
300 bool PerformInit) {
Chris Lattner8b418682012-02-07 00:39:47 +0000301 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Reid Klecknerc5c6fa72013-09-10 20:43:12 +0000302 SmallString<256> FnName;
303 {
304 llvm::raw_svector_ostream Out(FnName);
305 getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
306 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000307
308 // Create a variable initialization function.
309 llvm::Function *Fn =
Stephen Hines176edba2014-12-01 14:53:08 -0800310 CreateGlobalInitOrDestructFunction(FTy, FnName.str(), D->getLocation());
Eli Friedman6c6bda32010-01-08 00:50:11 +0000311
Stephen Hines176edba2014-12-01 14:53:08 -0800312 auto *ISA = D->getAttr<InitSegAttr>();
Richard Smith7ca48502012-02-13 22:16:19 +0000313 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
314 PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000315
Stephen Hines176edba2014-12-01 14:53:08 -0800316 llvm::GlobalVariable *COMDATKey =
317 supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr;
318
Richard Smithb80a16e2013-04-19 16:42:07 +0000319 if (D->getTLSKind()) {
320 // FIXME: Should we support init_priority for thread_local?
321 // FIXME: Ideally, initialization of instantiated thread_local static data
322 // members of class templates should not trigger initialization of other
323 // entities in the TU.
324 // FIXME: We only need to register one __cxa_thread_atexit function for the
325 // entire TU.
326 CXXThreadLocalInits.push_back(Fn);
Stephen Hines176edba2014-12-01 14:53:08 -0800327 CXXThreadLocalInitVars.push_back(Addr);
328 } else if (PerformInit && ISA) {
329 EmitPointerToInitFunc(D, Addr, Fn, ISA);
330 DelayedCXXInitPosition.erase(D);
331 } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700332 OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size());
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000333 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCallbf40cb52010-07-15 23:40:35 +0000334 DelayedCXXInitPosition.erase(D);
Stephen Hines176edba2014-12-01 14:53:08 -0800335 } else if (isTemplateInstantiation(D->getTemplateSpecializationKind())) {
Reid Klecknerb969e842013-08-22 20:07:45 +0000336 // C++ [basic.start.init]p2:
Reid Klecknerc47063e2013-09-04 00:54:24 +0000337 // Definitions of explicitly specialized class template static data
338 // members have ordered initialization. Other class template static data
339 // members (i.e., implicitly or explicitly instantiated specializations)
340 // have unordered initialization.
Reid Klecknerb969e842013-08-22 20:07:45 +0000341 //
342 // As a consequence, we can put them into their own llvm.global_ctors entry.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700343 //
Stephen Hines176edba2014-12-01 14:53:08 -0800344 // If the global is externally visible, put the initializer into a COMDAT
345 // group with the global being initialized. On most platforms, this is a
346 // minor startup time optimization. In the MS C++ ABI, there are no guard
347 // variables, so this COMDAT key is required for correctness.
348 AddGlobalCtor(Fn, 65535, COMDATKey);
349 DelayedCXXInitPosition.erase(D);
350 } else if (D->hasAttr<SelectAnyAttr>()) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700351 // SelectAny globals will be comdat-folded. Put the initializer into a
352 // COMDAT group associated with the global, so the initializers get folded
353 // too.
Stephen Hines176edba2014-12-01 14:53:08 -0800354 AddGlobalCtor(Fn, 65535, COMDATKey);
Reid Klecknerb969e842013-08-22 20:07:45 +0000355 DelayedCXXInitPosition.erase(D);
Richard Smithb80a16e2013-04-19 16:42:07 +0000356 } else {
John McCallbf40cb52010-07-15 23:40:35 +0000357 llvm::DenseMap<const Decl *, unsigned>::iterator I =
358 DelayedCXXInitPosition.find(D);
359 if (I == DelayedCXXInitPosition.end()) {
360 CXXGlobalInits.push_back(Fn);
361 } else {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700362 assert(CXXGlobalInits[I->second] == nullptr);
John McCallbf40cb52010-07-15 23:40:35 +0000363 CXXGlobalInits[I->second] = Fn;
364 DelayedCXXInitPosition.erase(I);
365 }
366 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000367}
368
Richard Smithb80a16e2013-04-19 16:42:07 +0000369void CodeGenModule::EmitCXXThreadLocalInitFunc() {
Stephen Hines176edba2014-12-01 14:53:08 -0800370 getCXXABI().EmitThreadLocalInitFuncs(
371 *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
Richard Smithb80a16e2013-04-19 16:42:07 +0000372
373 CXXThreadLocalInits.clear();
Stephen Hines176edba2014-12-01 14:53:08 -0800374 CXXThreadLocalInitVars.clear();
Richard Smithb80a16e2013-04-19 16:42:07 +0000375 CXXThreadLocals.clear();
376}
377
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000378void
379CodeGenModule::EmitCXXGlobalInitFunc() {
John McCallbf40cb52010-07-15 23:40:35 +0000380 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
381 CXXGlobalInits.pop_back();
382
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000383 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000384 return;
385
Chris Lattner8b418682012-02-07 00:39:47 +0000386 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000387
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000388
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000389 // Create our global initialization function.
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000390 if (!PrioritizedCXXGlobalInits.empty()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800391 SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000392 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000393 PrioritizedCXXGlobalInits.end());
394 // Iterate over "chunks" of ctors with same priority and emit each chunk
395 // into separate function. Note - everything is sorted first by priority,
396 // second - by lex order, so we emit ctor functions in proper order.
397 for (SmallVectorImpl<GlobalInitData >::iterator
398 I = PrioritizedCXXGlobalInits.begin(),
399 E = PrioritizedCXXGlobalInits.end(); I != E; ) {
400 SmallVectorImpl<GlobalInitData >::iterator
401 PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
402
403 LocalCXXGlobalInits.clear();
404 unsigned Priority = I->first.priority;
405 // Compute the function suffix from priority. Prepend with zeroes to make
406 // sure the function names are also ordered as priorities.
407 std::string PrioritySuffix = llvm::utostr(Priority);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700408 // Priority is always <= 65535 (enforced by sema).
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000409 PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
Stephen Hines176edba2014-12-01 14:53:08 -0800410 llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
411 FTy, "_GLOBAL__I_" + PrioritySuffix);
412
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000413 for (; I < PrioE; ++I)
414 LocalCXXGlobalInits.push_back(I->second);
415
Benjamin Kramerc7b5f382013-04-26 21:32:52 +0000416 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000417 AddGlobalCtor(Fn, Priority);
418 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000419 }
Stephen Hines176edba2014-12-01 14:53:08 -0800420
421 SmallString<128> FileName;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700422 SourceManager &SM = Context.getSourceManager();
Stephen Hines176edba2014-12-01 14:53:08 -0800423 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
424 // Include the filename in the symbol name. Including "sub_" matches gcc and
425 // makes sure these symbols appear lexicographically behind the symbols with
426 // priority emitted above.
427 FileName = llvm::sys::path::filename(MainFile->getName());
428 } else {
429 FileName = SmallString<128>("<null>");
430 }
431
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700432 for (size_t i = 0; i < FileName.size(); ++i) {
433 // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
434 // to be the set of C preprocessing numbers.
435 if (!isPreprocessingNumberBody(FileName[i]))
436 FileName[i] = '_';
437 }
Stephen Hines176edba2014-12-01 14:53:08 -0800438
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700439 llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
Stephen Hines176edba2014-12-01 14:53:08 -0800440 FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000441
Benjamin Kramerc7b5f382013-04-26 21:32:52 +0000442 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000443 AddGlobalCtor(Fn);
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000444
Axel Naumann54ec6c52011-05-06 15:24:04 +0000445 CXXGlobalInits.clear();
446 PrioritizedCXXGlobalInits.clear();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000447}
448
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000449void CodeGenModule::EmitCXXGlobalDtorFunc() {
450 if (CXXGlobalDtors.empty())
451 return;
452
Chris Lattner8b418682012-02-07 00:39:47 +0000453 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000454
455 // Create our global destructor function.
Stephen Hines176edba2014-12-01 14:53:08 -0800456 llvm::Function *Fn = CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000457
John McCall3f88f682012-04-06 18:21:03 +0000458 CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000459 AddGlobalDtor(Fn);
460}
461
John McCall3030eb82010-11-06 09:44:32 +0000462/// Emit the code necessary to initialize the given global variable.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000463void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
John McCall3030eb82010-11-06 09:44:32 +0000464 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000465 llvm::GlobalVariable *Addr,
466 bool PerformInit) {
Alexey Samsonova240df22012-10-16 07:22:28 +0000467 // Check if we need to emit debug info for variable initializer.
David Blaikiec3030bc2013-08-26 20:33:21 +0000468 if (D->hasAttr<NoDebugAttr>())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700469 DebugInfo = nullptr; // disable debug info indefinitely for this function
Nick Lewycky78d1a102012-07-24 01:40:49 +0000470
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700471 CurEHLocation = D->getLocStart();
472
Nick Lewycky78d1a102012-07-24 01:40:49 +0000473 StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000474 getTypes().arrangeNullaryFunction(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700475 FunctionArgList(), D->getLocation(),
476 D->getInit()->getExprLoc());
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000477
Douglas Gregore67d1512011-07-01 21:54:36 +0000478 // Use guarded initialization if the global variable is weak. This
479 // occurs for, e.g., instantiated static data members and
480 // definitions explicitly marked weak.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700481 if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage()) {
Richard Smith7ca48502012-02-13 22:16:19 +0000482 EmitCXXGuardedInit(*D, Addr, PerformInit);
John McCall3030eb82010-11-06 09:44:32 +0000483 } else {
Richard Smith7ca48502012-02-13 22:16:19 +0000484 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
Fariborz Jahanian92d835a2010-10-26 22:47:47 +0000485 }
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000486
487 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000488}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000489
Benjamin Kramerc7b5f382013-04-26 21:32:52 +0000490void
491CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
Stephen Hines176edba2014-12-01 14:53:08 -0800492 ArrayRef<llvm::Function *> Decls,
Benjamin Kramerc7b5f382013-04-26 21:32:52 +0000493 llvm::GlobalVariable *Guard) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700494 {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700495 auto NL = ApplyDebugLocation::CreateEmpty(*this);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700496 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
497 getTypes().arrangeNullaryFunction(), FunctionArgList());
498 // Emit an artificial location for this function.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700499 auto AL = ApplyDebugLocation::CreateArtificial(*this);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000500
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700501 llvm::BasicBlock *ExitBlock = nullptr;
502 if (Guard) {
503 // If we have a guard variable, check whether we've already performed
504 // these initializations. This happens for TLS initialization functions.
505 llvm::Value *GuardVal = Builder.CreateLoad(Guard);
506 llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
507 "guard.uninitialized");
508 // Mark as initialized before initializing anything else. If the
509 // initializers use previously-initialized thread_local vars, that's
510 // probably supposed to be OK, but the standard doesn't say.
511 Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
512 llvm::BasicBlock *InitBlock = createBasicBlock("init");
513 ExitBlock = createBasicBlock("exit");
514 Builder.CreateCondBr(Uninit, InitBlock, ExitBlock);
515 EmitBlock(InitBlock);
516 }
Richard Smithb80a16e2013-04-19 16:42:07 +0000517
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700518 RunCleanupsScope Scope(*this);
John McCallf85e1932011-06-15 23:02:42 +0000519
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700520 // When building in Objective-C++ ARC mode, create an autorelease pool
521 // around the global initializers.
522 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
523 llvm::Value *token = EmitObjCAutoreleasePoolPush();
524 EmitObjCAutoreleasePoolCleanup(token);
525 }
Benjamin Kramerc7b5f382013-04-26 21:32:52 +0000526
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700527 for (unsigned i = 0, e = Decls.size(); i != e; ++i)
528 if (Decls[i])
529 EmitRuntimeCall(Decls[i]);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000530
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700531 Scope.ForceCleanup();
Richard Smithb80a16e2013-04-19 16:42:07 +0000532
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700533 if (ExitBlock) {
534 Builder.CreateBr(ExitBlock);
535 EmitBlock(ExitBlock);
536 }
Richard Smithb80a16e2013-04-19 16:42:07 +0000537 }
538
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000539 FinishFunction();
540}
541
John McCall3f88f682012-04-06 18:21:03 +0000542void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000543 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000544 &DtorsAndObjects) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700545 {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700546 auto NL = ApplyDebugLocation::CreateEmpty(*this);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700547 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
548 getTypes().arrangeNullaryFunction(), FunctionArgList());
549 // Emit an artificial location for this function.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700550 auto AL = ApplyDebugLocation::CreateArtificial(*this);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000551
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700552 // Emit the dtors, in reverse order from construction.
553 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
554 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
555 llvm::CallInst *CI = Builder.CreateCall(Callee,
556 DtorsAndObjects[e - i - 1].second);
557 // Make sure the call and the callee agree on calling convention.
558 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
559 CI->setCallingConv(F->getCallingConv());
560 }
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000561 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000562
563 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000564}
565
John McCalla91f6662011-07-13 03:01:35 +0000566/// generateDestroyHelper - Generates a helper function which, when
567/// invoked, destroys the given object.
David Blaikiec7971a92013-08-27 23:57:18 +0000568llvm::Function *CodeGenFunction::generateDestroyHelper(
569 llvm::Constant *addr, QualType type, Destroyer *destroyer,
570 bool useEHCleanupForArray, const VarDecl *VD) {
John McCalld26bc762011-03-09 04:27:21 +0000571 FunctionArgList args;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700572 ImplicitParamDecl dst(getContext(), nullptr, SourceLocation(), nullptr,
573 getContext().VoidPtrTy);
John McCalld26bc762011-03-09 04:27:21 +0000574 args.push_back(&dst);
Stephen Hines651f13c2014-04-23 16:59:28 -0700575
576 const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
577 getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
John McCallde5d3c72012-02-17 03:33:10 +0000578 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
Stephen Hines176edba2014-12-01 14:53:08 -0800579 llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
580 FTy, "__cxx_global_array_dtor", VD->getLocation());
Anders Carlsson77291362010-06-08 22:17:27 +0000581
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700582 CurEHLocation = VD->getLocStart();
583
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700584 StartFunction(VD, getContext().VoidTy, fn, FI, args);
Anders Carlsson77291362010-06-08 22:17:27 +0000585
John McCalla91f6662011-07-13 03:01:35 +0000586 emitDestroy(addr, type, destroyer, useEHCleanupForArray);
Anders Carlsson77291362010-06-08 22:17:27 +0000587
588 FinishFunction();
589
John McCalla91f6662011-07-13 03:01:35 +0000590 return fn;
Anders Carlsson77291362010-06-08 22:17:27 +0000591}