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 | |
| 142 | if (!T->isReferenceType()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 143 | if (getLangOpts().OpenMP && D.hasAttr<OMPThreadPrivateDeclAttr>()) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 144 | (void)CGM.getOpenMPRuntime().emitThreadPrivateVarDefinition( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 145 | &D, DeclPtr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(), |
| 146 | PerformInit, this); |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 147 | if (PerformInit) |
| 148 | EmitDeclInit(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 149 | if (CGM.isTypeConstant(D.getType(), true)) |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 150 | EmitDeclInvariant(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 151 | else |
| 152 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 155 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 156 | assert(PerformInit && "cannot have constant initializer which needs " |
| 157 | "destruction for reference"); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 158 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
Richard Smith | d4ec562 | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 159 | RValue RV = EmitReferenceBindingToExpr(Init); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 160 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 161 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 162 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 163 | /// Create a stub function, suitable for being passed to atexit, |
| 164 | /// which passes the given address to the given destructor function. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 165 | llvm::Constant *CodeGenFunction::createAtExitStub(const VarDecl &VD, |
| 166 | llvm::Constant *dtor, |
| 167 | llvm::Constant *addr) { |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 168 | // Get the destructor function type, void(*)(void). |
| 169 | llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 170 | SmallString<256> FnName; |
| 171 | { |
| 172 | llvm::raw_svector_ostream Out(FnName); |
| 173 | CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out); |
| 174 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 175 | llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(ty, FnName.str(), |
| 176 | VD.getLocation()); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 177 | |
| 178 | CodeGenFunction CGF(CGM); |
| 179 | |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 180 | CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 181 | CGM.getTypes().arrangeNullaryFunction(), FunctionArgList()); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 182 | |
| 183 | llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr); |
| 184 | |
| 185 | // Make sure the call and the callee agree on calling convention. |
| 186 | if (llvm::Function *dtorFn = |
| 187 | dyn_cast<llvm::Function>(dtor->stripPointerCasts())) |
| 188 | call->setCallingConv(dtorFn->getCallingConv()); |
| 189 | |
| 190 | CGF.FinishFunction(); |
| 191 | |
| 192 | return fn; |
| 193 | } |
| 194 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 195 | /// Register a global destructor using the C atexit runtime function. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 196 | void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD, |
| 197 | llvm::Constant *dtor, |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 198 | llvm::Constant *addr) { |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 199 | // Create a function which calls the destructor. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 200 | llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 201 | |
| 202 | // extern "C" int atexit(void (*f)(void)); |
| 203 | llvm::FunctionType *atexitTy = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 204 | llvm::FunctionType::get(IntTy, dtorStub->getType(), false); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 205 | |
| 206 | llvm::Constant *atexit = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 207 | CGM.CreateRuntimeFunction(atexitTy, "atexit"); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 208 | if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit)) |
| 209 | atexitFn->setDoesNotThrow(); |
| 210 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 211 | EmitNounwindRuntimeCall(atexit, dtorStub); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 212 | } |
| 213 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 214 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 215 | llvm::GlobalVariable *DeclPtr, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 216 | bool PerformInit) { |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 217 | // If we've been asked to forbid guard variables, emit an error now. |
| 218 | // This diagnostic is hard-coded for Darwin's use case; we can find |
| 219 | // better phrasing if someone else needs it. |
| 220 | if (CGM.getCodeGenOpts().ForbidGuardVariables) |
| 221 | CGM.Error(D.getLocation(), |
| 222 | "this initialization requires a guard variable, which " |
| 223 | "the kernel does not support"); |
| 224 | |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 225 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 228 | llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction( |
| 229 | llvm::FunctionType *FTy, const Twine &Name, SourceLocation Loc, bool TLS) { |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 230 | llvm::Function *Fn = |
| 231 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 232 | Name, &getModule()); |
| 233 | if (!getLangOpts().AppleKext && !TLS) { |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 234 | // Set the section if needed. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 235 | if (const char *Section = getTarget().getStaticInitSectionSpecifier()) |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 236 | Fn->setSection(Section); |
| 237 | } |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 238 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 239 | Fn->setCallingConv(getRuntimeCC()); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 240 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 241 | if (!getLangOpts().Exceptions) |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 242 | Fn->setDoesNotThrow(); |
| 243 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 244 | if (!isInSanitizerBlacklist(Fn, Loc)) { |
| 245 | if (getLangOpts().Sanitize.has(SanitizerKind::Address)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 246 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 247 | if (getLangOpts().Sanitize.has(SanitizerKind::Thread)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 248 | Fn->addFnAttr(llvm::Attribute::SanitizeThread); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 249 | if (getLangOpts().Sanitize.has(SanitizerKind::Memory)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 250 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
| 251 | } |
Kostya Serebryany | b9d2b3b | 2012-06-26 08:56:33 +0000 | [diff] [blame] | 252 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 253 | return Fn; |
| 254 | } |
| 255 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 256 | /// Create a global pointer to a function that will initialize a global |
| 257 | /// variable. The user has requested that this pointer be emitted in a specific |
| 258 | /// section. |
| 259 | void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D, |
| 260 | llvm::GlobalVariable *GV, |
| 261 | llvm::Function *InitFunc, |
| 262 | InitSegAttr *ISA) { |
| 263 | llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable( |
| 264 | TheModule, InitFunc->getType(), /*isConstant=*/true, |
| 265 | llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr"); |
| 266 | PtrArray->setSection(ISA->getSection()); |
| 267 | addUsedGlobal(PtrArray); |
| 268 | |
| 269 | // 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^] | 270 | if (llvm::Comdat *C = GV->getComdat()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 271 | PtrArray->setComdat(C); |
| 272 | } |
| 273 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 274 | void |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 275 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 276 | llvm::GlobalVariable *Addr, |
| 277 | bool PerformInit) { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 278 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Reid Kleckner | c5c6fa7 | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 279 | SmallString<256> FnName; |
| 280 | { |
| 281 | llvm::raw_svector_ostream Out(FnName); |
| 282 | getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out); |
| 283 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 284 | |
| 285 | // Create a variable initialization function. |
| 286 | llvm::Function *Fn = |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 287 | CreateGlobalInitOrDestructFunction(FTy, FnName.str(), D->getLocation()); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 288 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 289 | auto *ISA = D->getAttr<InitSegAttr>(); |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 290 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, |
| 291 | PerformInit); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 292 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 293 | llvm::GlobalVariable *COMDATKey = |
| 294 | supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr; |
| 295 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 296 | if (D->getTLSKind()) { |
| 297 | // FIXME: Should we support init_priority for thread_local? |
| 298 | // FIXME: Ideally, initialization of instantiated thread_local static data |
| 299 | // members of class templates should not trigger initialization of other |
| 300 | // entities in the TU. |
| 301 | // FIXME: We only need to register one __cxa_thread_atexit function for the |
| 302 | // entire TU. |
| 303 | CXXThreadLocalInits.push_back(Fn); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 304 | CXXThreadLocalInitVars.push_back(Addr); |
| 305 | } else if (PerformInit && ISA) { |
| 306 | EmitPointerToInitFunc(D, Addr, Fn, ISA); |
| 307 | DelayedCXXInitPosition.erase(D); |
| 308 | } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 309 | OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 310 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 311 | DelayedCXXInitPosition.erase(D); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 312 | } else if (isTemplateInstantiation(D->getTemplateSpecializationKind())) { |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 313 | // C++ [basic.start.init]p2: |
Reid Kleckner | c47063e | 2013-09-04 00:54:24 +0000 | [diff] [blame] | 314 | // Definitions of explicitly specialized class template static data |
| 315 | // members have ordered initialization. Other class template static data |
| 316 | // members (i.e., implicitly or explicitly instantiated specializations) |
| 317 | // have unordered initialization. |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 318 | // |
| 319 | // 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] | 320 | // |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 321 | // If the global is externally visible, put the initializer into a COMDAT |
| 322 | // group with the global being initialized. On most platforms, this is a |
| 323 | // minor startup time optimization. In the MS C++ ABI, there are no guard |
| 324 | // variables, so this COMDAT key is required for correctness. |
| 325 | AddGlobalCtor(Fn, 65535, COMDATKey); |
| 326 | DelayedCXXInitPosition.erase(D); |
| 327 | } else if (D->hasAttr<SelectAnyAttr>()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 328 | // SelectAny globals will be comdat-folded. Put the initializer into a |
| 329 | // COMDAT group associated with the global, so the initializers get folded |
| 330 | // too. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 331 | AddGlobalCtor(Fn, 65535, COMDATKey); |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 332 | DelayedCXXInitPosition.erase(D); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 333 | } else { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 334 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 335 | DelayedCXXInitPosition.find(D); |
| 336 | if (I == DelayedCXXInitPosition.end()) { |
| 337 | CXXGlobalInits.push_back(Fn); |
| 338 | } else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 339 | assert(CXXGlobalInits[I->second] == nullptr); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 340 | CXXGlobalInits[I->second] = Fn; |
| 341 | DelayedCXXInitPosition.erase(I); |
| 342 | } |
| 343 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 346 | void CodeGenModule::EmitCXXThreadLocalInitFunc() { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 347 | getCXXABI().EmitThreadLocalInitFuncs( |
| 348 | *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 349 | |
| 350 | CXXThreadLocalInits.clear(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 351 | CXXThreadLocalInitVars.clear(); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 352 | CXXThreadLocals.clear(); |
| 353 | } |
| 354 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 355 | void |
| 356 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 357 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 358 | CXXGlobalInits.pop_back(); |
| 359 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 360 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 361 | return; |
| 362 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 363 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 364 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 365 | |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 366 | // Create our global initialization function. |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 367 | if (!PrioritizedCXXGlobalInits.empty()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 368 | SmallVector<llvm::Function *, 8> LocalCXXGlobalInits; |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 369 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 370 | PrioritizedCXXGlobalInits.end()); |
| 371 | // Iterate over "chunks" of ctors with same priority and emit each chunk |
| 372 | // into separate function. Note - everything is sorted first by priority, |
| 373 | // second - by lex order, so we emit ctor functions in proper order. |
| 374 | for (SmallVectorImpl<GlobalInitData >::iterator |
| 375 | I = PrioritizedCXXGlobalInits.begin(), |
| 376 | E = PrioritizedCXXGlobalInits.end(); I != E; ) { |
| 377 | SmallVectorImpl<GlobalInitData >::iterator |
| 378 | PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp()); |
| 379 | |
| 380 | LocalCXXGlobalInits.clear(); |
| 381 | unsigned Priority = I->first.priority; |
| 382 | // Compute the function suffix from priority. Prepend with zeroes to make |
| 383 | // sure the function names are also ordered as priorities. |
| 384 | std::string PrioritySuffix = llvm::utostr(Priority); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 385 | // Priority is always <= 65535 (enforced by sema). |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 386 | PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 387 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction( |
| 388 | FTy, "_GLOBAL__I_" + PrioritySuffix); |
| 389 | |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 390 | for (; I < PrioE; ++I) |
| 391 | LocalCXXGlobalInits.push_back(I->second); |
| 392 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 393 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 394 | AddGlobalCtor(Fn, Priority); |
| 395 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 396 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 397 | |
| 398 | SmallString<128> FileName; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 399 | SourceManager &SM = Context.getSourceManager(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 400 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
| 401 | // Include the filename in the symbol name. Including "sub_" matches gcc and |
| 402 | // makes sure these symbols appear lexicographically behind the symbols with |
| 403 | // priority emitted above. |
| 404 | FileName = llvm::sys::path::filename(MainFile->getName()); |
| 405 | } else { |
| 406 | FileName = SmallString<128>("<null>"); |
| 407 | } |
| 408 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 409 | for (size_t i = 0; i < FileName.size(); ++i) { |
| 410 | // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens |
| 411 | // to be the set of C preprocessing numbers. |
| 412 | if (!isPreprocessingNumberBody(FileName[i])) |
| 413 | FileName[i] = '_'; |
| 414 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 415 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 416 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 417 | FTy, llvm::Twine("_GLOBAL__sub_I_", FileName)); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 418 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 419 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 420 | AddGlobalCtor(Fn); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 421 | |
Axel Naumann | 54ec6c5 | 2011-05-06 15:24:04 +0000 | [diff] [blame] | 422 | CXXGlobalInits.clear(); |
| 423 | PrioritizedCXXGlobalInits.clear(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 426 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 427 | if (CXXGlobalDtors.empty()) |
| 428 | return; |
| 429 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 430 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 431 | |
| 432 | // Create our global destructor function. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 433 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 434 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 435 | CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 436 | AddGlobalDtor(Fn); |
| 437 | } |
| 438 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 439 | /// Emit the code necessary to initialize the given global variable. |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 440 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 441 | const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 442 | llvm::GlobalVariable *Addr, |
| 443 | bool PerformInit) { |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 444 | // Check if we need to emit debug info for variable initializer. |
David Blaikie | c3030bc | 2013-08-26 20:33:21 +0000 | [diff] [blame] | 445 | if (D->hasAttr<NoDebugAttr>()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 446 | DebugInfo = nullptr; // disable debug info indefinitely for this function |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 447 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 448 | CurEHLocation = D->getLocStart(); |
| 449 | |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 450 | StartFunction(GlobalDecl(D), getContext().VoidTy, Fn, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 451 | getTypes().arrangeNullaryFunction(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 452 | FunctionArgList(), D->getLocation(), |
| 453 | D->getInit()->getExprLoc()); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 455 | // Use guarded initialization if the global variable is weak. This |
| 456 | // occurs for, e.g., instantiated static data members and |
| 457 | // definitions explicitly marked weak. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 458 | if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage()) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 459 | EmitCXXGuardedInit(*D, Addr, PerformInit); |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 460 | } else { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 461 | EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); |
Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 462 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 463 | |
| 464 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 465 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 466 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 467 | void |
| 468 | CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 469 | ArrayRef<llvm::Function *> Decls, |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 470 | llvm::GlobalVariable *Guard) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 471 | { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 472 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 473 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 474 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
| 475 | // Emit an artificial location for this function. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 476 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 477 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 478 | llvm::BasicBlock *ExitBlock = nullptr; |
| 479 | if (Guard) { |
| 480 | // If we have a guard variable, check whether we've already performed |
| 481 | // these initializations. This happens for TLS initialization functions. |
| 482 | llvm::Value *GuardVal = Builder.CreateLoad(Guard); |
| 483 | llvm::Value *Uninit = Builder.CreateIsNull(GuardVal, |
| 484 | "guard.uninitialized"); |
| 485 | // Mark as initialized before initializing anything else. If the |
| 486 | // initializers use previously-initialized thread_local vars, that's |
| 487 | // probably supposed to be OK, but the standard doesn't say. |
| 488 | Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard); |
| 489 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 490 | ExitBlock = createBasicBlock("exit"); |
| 491 | Builder.CreateCondBr(Uninit, InitBlock, ExitBlock); |
| 492 | EmitBlock(InitBlock); |
| 493 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 494 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 495 | RunCleanupsScope Scope(*this); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 496 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 497 | // When building in Objective-C++ ARC mode, create an autorelease pool |
| 498 | // around the global initializers. |
| 499 | if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) { |
| 500 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
| 501 | EmitObjCAutoreleasePoolCleanup(token); |
| 502 | } |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 503 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 504 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) |
| 505 | if (Decls[i]) |
| 506 | EmitRuntimeCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 507 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 508 | Scope.ForceCleanup(); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 509 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 510 | if (ExitBlock) { |
| 511 | Builder.CreateBr(ExitBlock); |
| 512 | EmitBlock(ExitBlock); |
| 513 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 516 | FinishFunction(); |
| 517 | } |
| 518 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 519 | void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 520 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 521 | &DtorsAndObjects) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 522 | { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 523 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 524 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 525 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
| 526 | // Emit an artificial location for this function. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 527 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 528 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 529 | // Emit the dtors, in reverse order from construction. |
| 530 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
| 531 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
| 532 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 533 | DtorsAndObjects[e - i - 1].second); |
| 534 | // Make sure the call and the callee agree on calling convention. |
| 535 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 536 | CI->setCallingConv(F->getCallingConv()); |
| 537 | } |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 538 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 539 | |
| 540 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 541 | } |
| 542 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 543 | /// generateDestroyHelper - Generates a helper function which, when |
| 544 | /// invoked, destroys the given object. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 545 | llvm::Function *CodeGenFunction::generateDestroyHelper( |
| 546 | llvm::Constant *addr, QualType type, Destroyer *destroyer, |
| 547 | bool useEHCleanupForArray, const VarDecl *VD) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 548 | FunctionArgList args; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 549 | ImplicitParamDecl dst(getContext(), nullptr, SourceLocation(), nullptr, |
| 550 | getContext().VoidPtrTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 551 | args.push_back(&dst); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 552 | |
| 553 | const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 554 | getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 555 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 556 | llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction( |
| 557 | FTy, "__cxx_global_array_dtor", VD->getLocation()); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 558 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 559 | CurEHLocation = VD->getLocStart(); |
| 560 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 561 | StartFunction(VD, getContext().VoidTy, fn, FI, args); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 562 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 563 | emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 564 | |
| 565 | FinishFunction(); |
| 566 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 567 | return fn; |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 568 | } |