Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 1 | //===--- 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 McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 17 | #include "CGOpenMPRuntime.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/CodeGenOptions.h" |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Intrinsics.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 22 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 26 | static 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 Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 32 | ASTContext &Context = CGF.getContext(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 33 | |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 34 | CharUnits alignment = Context.getDeclAlign(&D); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 35 | QualType type = D.getType(); |
| 36 | LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); |
| 37 | |
| 38 | const Expr *Init = D.getInit(); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 39 | switch (CGF.getEvaluationKind(type)) { |
| 40 | case TEK_Scalar: { |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 41 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 42 | if (lv.isObjCStrong()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 43 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 44 | DeclPtr, D.getTLSKind()); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 45 | else if (lv.isObjCWeak()) |
| 46 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), |
| 47 | DeclPtr); |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 48 | else |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 49 | CGF.EmitScalarInit(Init, &D, lv, false); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | case TEK_Complex: |
| 53 | CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true); |
| 54 | return; |
| 55 | case TEK_Aggregate: |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 56 | CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed, |
| 57 | AggValueSlot::DoesNotNeedGCBarriers, |
| 58 | AggValueSlot::IsNotAliased)); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 59 | return; |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 60 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 61 | llvm_unreachable("bad evaluation kind"); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 64 | /// Emit code to cause the destruction of the given variable with |
| 65 | /// static storage duration. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 66 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 67 | llvm::Constant *addr) { |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 68 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 69 | |
| 70 | // FIXME: __attribute__((cleanup)) ? |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 71 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 72 | QualType type = D.getType(); |
| 73 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 74 | |
| 75 | switch (dtorKind) { |
| 76 | case QualType::DK_none: |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 77 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 78 | |
| 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 Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 85 | assert(!D.getTLSKind() && "should have rejected this"); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 86 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 87 | } |
| 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 Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 94 | const CXXRecordDecl *record = nullptr; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 95 | if (dtorKind == QualType::DK_cxx_destructor && |
| 96 | (record = type->getAsCXXRecordDecl())) { |
| 97 | assert(!record->hasTrivialDestructor()); |
| 98 | CXXDestructorDecl *dtor = record->getDestructor(); |
| 99 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 100 | function = CGM.getAddrOfCXXStructor(dtor, StructorType::Complete); |
Timur Iskhodzhanov | 9a3be4c | 2013-10-02 16:03:16 +0000 | [diff] [blame] | 101 | argument = llvm::ConstantExpr::getBitCast( |
| 102 | addr, CGF.getTypes().ConvertType(type)->getPointerTo()); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 103 | |
| 104 | // Otherwise, the standard logic requires a helper function. |
| 105 | } else { |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 106 | function = CodeGenFunction(CGM) |
| 107 | .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind), |
| 108 | CGF.needsEHCleanup(dtorKind), &D); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 109 | argument = llvm::Constant::getNullValue(CGF.Int8PtrTy); |
| 110 | } |
| 111 | |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 112 | CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 115 | /// Emit code to cause the variable at the given address to be considered as |
| 116 | /// constant from this point onwards. |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 117 | static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, |
| 118 | llvm::Constant *Addr) { |
Richard Smith | 00a8c3f | 2012-02-17 20:12:52 +0000 | [diff] [blame] | 119 | // Don't emit the intrinsic if we're not optimizing. |
| 120 | if (!CGF.CGM.getCodeGenOpts().OptimizationLevel) |
| 121 | return; |
| 122 | |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 123 | // 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 Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 127 | // 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 Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 131 | llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)}; |
| 132 | CGF.Builder.CreateCall(InvariantStart, Args); |
| 133 | } |
| 134 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 135 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 136 | llvm::Constant *DeclPtr, |
| 137 | bool PerformInit) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 138 | |
| 139 | const Expr *Init = D.getInit(); |
| 140 | QualType T = D.getType(); |
| 141 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame^] | 142 | // 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 Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 165 | if (!T->isReferenceType()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 166 | if (getLangOpts().OpenMP && D.hasAttr<OMPThreadPrivateDeclAttr>()) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 167 | (void)CGM.getOpenMPRuntime().emitThreadPrivateVarDefinition( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 168 | &D, DeclPtr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(), |
| 169 | PerformInit, this); |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 170 | if (PerformInit) |
| 171 | EmitDeclInit(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 172 | if (CGM.isTypeConstant(D.getType(), true)) |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 173 | EmitDeclInvariant(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 174 | else |
| 175 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 176 | return; |
| 177 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 178 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 179 | assert(PerformInit && "cannot have constant initializer which needs " |
| 180 | "destruction for reference"); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 181 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
Richard Smith | d4ec562 | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 182 | RValue RV = EmitReferenceBindingToExpr(Init); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 183 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 184 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 185 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 186 | /// Create a stub function, suitable for being passed to atexit, |
| 187 | /// which passes the given address to the given destructor function. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 188 | llvm::Constant *CodeGenFunction::createAtExitStub(const VarDecl &VD, |
| 189 | llvm::Constant *dtor, |
| 190 | llvm::Constant *addr) { |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 191 | // Get the destructor function type, void(*)(void). |
| 192 | llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 193 | SmallString<256> FnName; |
| 194 | { |
| 195 | llvm::raw_svector_ostream Out(FnName); |
| 196 | CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out); |
| 197 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 198 | llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(ty, FnName.str(), |
| 199 | VD.getLocation()); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 200 | |
| 201 | CodeGenFunction CGF(CGM); |
| 202 | |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 203 | CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 204 | CGM.getTypes().arrangeNullaryFunction(), FunctionArgList()); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 205 | |
| 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 McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 218 | /// Register a global destructor using the C atexit runtime function. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 219 | void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD, |
| 220 | llvm::Constant *dtor, |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 221 | llvm::Constant *addr) { |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 222 | // Create a function which calls the destructor. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 223 | llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 224 | |
| 225 | // extern "C" int atexit(void (*f)(void)); |
| 226 | llvm::FunctionType *atexitTy = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 227 | llvm::FunctionType::get(IntTy, dtorStub->getType(), false); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 228 | |
| 229 | llvm::Constant *atexit = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 230 | CGM.CreateRuntimeFunction(atexitTy, "atexit"); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 231 | if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit)) |
| 232 | atexitFn->setDoesNotThrow(); |
| 233 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 234 | EmitNounwindRuntimeCall(atexit, dtorStub); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 235 | } |
| 236 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 237 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 238 | llvm::GlobalVariable *DeclPtr, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 239 | bool PerformInit) { |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 240 | // 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 Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 248 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 251 | llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction( |
| 252 | llvm::FunctionType *FTy, const Twine &Name, SourceLocation Loc, bool TLS) { |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 253 | llvm::Function *Fn = |
| 254 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 255 | Name, &getModule()); |
| 256 | if (!getLangOpts().AppleKext && !TLS) { |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 257 | // Set the section if needed. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 258 | if (const char *Section = getTarget().getStaticInitSectionSpecifier()) |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 259 | Fn->setSection(Section); |
| 260 | } |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 261 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 262 | Fn->setCallingConv(getRuntimeCC()); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 263 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 264 | if (!getLangOpts().Exceptions) |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 265 | Fn->setDoesNotThrow(); |
| 266 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 267 | if (!isInSanitizerBlacklist(Fn, Loc)) { |
| 268 | if (getLangOpts().Sanitize.has(SanitizerKind::Address)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 269 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 270 | if (getLangOpts().Sanitize.has(SanitizerKind::Thread)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 271 | Fn->addFnAttr(llvm::Attribute::SanitizeThread); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 272 | if (getLangOpts().Sanitize.has(SanitizerKind::Memory)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 273 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
| 274 | } |
Kostya Serebryany | b9d2b3b | 2012-06-26 08:56:33 +0000 | [diff] [blame] | 275 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 276 | return Fn; |
| 277 | } |
| 278 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 279 | /// 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. |
| 282 | void 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 Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 293 | if (llvm::Comdat *C = GV->getComdat()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 294 | PtrArray->setComdat(C); |
| 295 | } |
| 296 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 297 | void |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 298 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 299 | llvm::GlobalVariable *Addr, |
| 300 | bool PerformInit) { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 301 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Reid Kleckner | c5c6fa7 | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 302 | SmallString<256> FnName; |
| 303 | { |
| 304 | llvm::raw_svector_ostream Out(FnName); |
| 305 | getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out); |
| 306 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 307 | |
| 308 | // Create a variable initialization function. |
| 309 | llvm::Function *Fn = |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 310 | CreateGlobalInitOrDestructFunction(FTy, FnName.str(), D->getLocation()); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 311 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 312 | auto *ISA = D->getAttr<InitSegAttr>(); |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 313 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, |
| 314 | PerformInit); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 315 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 316 | llvm::GlobalVariable *COMDATKey = |
| 317 | supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr; |
| 318 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 319 | 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 Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 327 | 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 Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 332 | OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 333 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 334 | DelayedCXXInitPosition.erase(D); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 335 | } else if (isTemplateInstantiation(D->getTemplateSpecializationKind())) { |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 336 | // C++ [basic.start.init]p2: |
Reid Kleckner | c47063e | 2013-09-04 00:54:24 +0000 | [diff] [blame] | 337 | // 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 Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 341 | // |
| 342 | // As a consequence, we can put them into their own llvm.global_ctors entry. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 343 | // |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 344 | // 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 Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 351 | // 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 Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 354 | AddGlobalCtor(Fn, 65535, COMDATKey); |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 355 | DelayedCXXInitPosition.erase(D); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 356 | } else { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 357 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 358 | DelayedCXXInitPosition.find(D); |
| 359 | if (I == DelayedCXXInitPosition.end()) { |
| 360 | CXXGlobalInits.push_back(Fn); |
| 361 | } else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 362 | assert(CXXGlobalInits[I->second] == nullptr); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 363 | CXXGlobalInits[I->second] = Fn; |
| 364 | DelayedCXXInitPosition.erase(I); |
| 365 | } |
| 366 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 369 | void CodeGenModule::EmitCXXThreadLocalInitFunc() { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 370 | getCXXABI().EmitThreadLocalInitFuncs( |
| 371 | *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 372 | |
| 373 | CXXThreadLocalInits.clear(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 374 | CXXThreadLocalInitVars.clear(); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 375 | CXXThreadLocals.clear(); |
| 376 | } |
| 377 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 378 | void |
| 379 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 380 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 381 | CXXGlobalInits.pop_back(); |
| 382 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 383 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 384 | return; |
| 385 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 386 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 387 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 388 | |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 389 | // Create our global initialization function. |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 390 | if (!PrioritizedCXXGlobalInits.empty()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 391 | SmallVector<llvm::Function *, 8> LocalCXXGlobalInits; |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 392 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 393 | 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 Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 408 | // Priority is always <= 65535 (enforced by sema). |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 409 | PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 410 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction( |
| 411 | FTy, "_GLOBAL__I_" + PrioritySuffix); |
| 412 | |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 413 | for (; I < PrioE; ++I) |
| 414 | LocalCXXGlobalInits.push_back(I->second); |
| 415 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 416 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 417 | AddGlobalCtor(Fn, Priority); |
| 418 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 419 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 420 | |
| 421 | SmallString<128> FileName; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 422 | SourceManager &SM = Context.getSourceManager(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 423 | 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 Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 432 | 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 Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 438 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 439 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 440 | FTy, llvm::Twine("_GLOBAL__sub_I_", FileName)); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 441 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 442 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 443 | AddGlobalCtor(Fn); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 444 | |
Axel Naumann | 54ec6c5 | 2011-05-06 15:24:04 +0000 | [diff] [blame] | 445 | CXXGlobalInits.clear(); |
| 446 | PrioritizedCXXGlobalInits.clear(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 449 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 450 | if (CXXGlobalDtors.empty()) |
| 451 | return; |
| 452 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 453 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 454 | |
| 455 | // Create our global destructor function. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 456 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 457 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 458 | CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 459 | AddGlobalDtor(Fn); |
| 460 | } |
| 461 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 462 | /// Emit the code necessary to initialize the given global variable. |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 463 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 464 | const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 465 | llvm::GlobalVariable *Addr, |
| 466 | bool PerformInit) { |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 467 | // Check if we need to emit debug info for variable initializer. |
David Blaikie | c3030bc | 2013-08-26 20:33:21 +0000 | [diff] [blame] | 468 | if (D->hasAttr<NoDebugAttr>()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 469 | DebugInfo = nullptr; // disable debug info indefinitely for this function |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 470 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 471 | CurEHLocation = D->getLocStart(); |
| 472 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 473 | StartFunction(GlobalDecl(D), getContext().VoidTy, Fn, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 474 | getTypes().arrangeNullaryFunction(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 475 | FunctionArgList(), D->getLocation(), |
| 476 | D->getInit()->getExprLoc()); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 478 | // 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 Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 481 | if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage()) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 482 | EmitCXXGuardedInit(*D, Addr, PerformInit); |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 483 | } else { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 484 | EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); |
Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 485 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 486 | |
| 487 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 488 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 489 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 490 | void |
| 491 | CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 492 | ArrayRef<llvm::Function *> Decls, |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 493 | llvm::GlobalVariable *Guard) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 494 | { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 495 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 496 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 497 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
| 498 | // Emit an artificial location for this function. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 499 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 500 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 501 | 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 Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 517 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 518 | RunCleanupsScope Scope(*this); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 519 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 520 | // 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 Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 526 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 527 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) |
| 528 | if (Decls[i]) |
| 529 | EmitRuntimeCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 530 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 531 | Scope.ForceCleanup(); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 532 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 533 | if (ExitBlock) { |
| 534 | Builder.CreateBr(ExitBlock); |
| 535 | EmitBlock(ExitBlock); |
| 536 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 539 | FinishFunction(); |
| 540 | } |
| 541 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 542 | void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 543 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 544 | &DtorsAndObjects) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 545 | { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 546 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 547 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 548 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
| 549 | // Emit an artificial location for this function. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 550 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 551 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 552 | // 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 Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 561 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 562 | |
| 563 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 564 | } |
| 565 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 566 | /// generateDestroyHelper - Generates a helper function which, when |
| 567 | /// invoked, destroys the given object. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 568 | llvm::Function *CodeGenFunction::generateDestroyHelper( |
| 569 | llvm::Constant *addr, QualType type, Destroyer *destroyer, |
| 570 | bool useEHCleanupForArray, const VarDecl *VD) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 571 | FunctionArgList args; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 572 | ImplicitParamDecl dst(getContext(), nullptr, SourceLocation(), nullptr, |
| 573 | getContext().VoidPtrTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 574 | args.push_back(&dst); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 575 | |
| 576 | const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 577 | getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 578 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 579 | llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction( |
| 580 | FTy, "__cxx_global_array_dtor", VD->getLocation()); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 581 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 582 | CurEHLocation = VD->getLocStart(); |
| 583 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 584 | StartFunction(VD, getContext().VoidTy, fn, FI, args); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 585 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 586 | emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 587 | |
| 588 | FinishFunction(); |
| 589 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 590 | return fn; |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 591 | } |