Simplify the logic for emitting guard variables for template static
data members by delaying the emission of the initializer until after
linkage and visibility have been set on the global. Also, don't
emit a guard unless the variable actually ends up with vague linkage,
and don't use thread-safe statics in any case.
llvm-svn: 118336
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 94c1f60..839e047 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -120,8 +120,8 @@
QualType ElementType, llvm::Value *&NumElements,
llvm::Value *&AllocPtr, CharUnits &CookieSize);
- void EmitStaticLocalInit(CodeGenFunction &CGF, const VarDecl &D,
- llvm::GlobalVariable *DeclPtr);
+ void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
+ llvm::GlobalVariable *DeclPtr);
};
class ARMCXXABI : public ItaniumCXXABI {
@@ -1078,11 +1078,15 @@
/// The ARM code here follows the Itanium code closely enough that we
/// just special-case it at particular places.
-void ItaniumCXXABI::EmitStaticLocalInit(CodeGenFunction &CGF,
- const VarDecl &D,
- llvm::GlobalVariable *GV) {
+void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
+ const VarDecl &D,
+ llvm::GlobalVariable *GV) {
CGBuilderTy &Builder = CGF.Builder;
- bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics;
+
+ // We only need to use thread-safe statics for local variables;
+ // global initialization is always single-threaded.
+ bool ThreadsafeStatics = (getContext().getLangOptions().ThreadsafeStatics &&
+ D.isLocalVarDecl());
// Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
const llvm::IntegerType *GuardTy
@@ -1093,16 +1097,10 @@
llvm::SmallString<256> GuardVName;
getMangleContext().mangleItaniumGuardVariable(&D, GuardVName);
- // FIXME: we should just absorb linkage and visibility from the
- // variable, but that's not always set up properly just yet.
- llvm::GlobalValue::LinkageTypes Linkage = GV->getLinkage();
- if (D.isStaticDataMember() &&
- D.getInstantiatedFromStaticDataMember())
- Linkage = llvm::GlobalVariable::WeakAnyLinkage;
-
+ // Just absorb linkage and visibility from the variable.
llvm::GlobalVariable *GuardVariable =
new llvm::GlobalVariable(CGM.getModule(), GuardTy,
- false, Linkage,
+ false, GV->getLinkage(),
llvm::ConstantInt::get(GuardTy, 0),
GuardVName.str());
GuardVariable->setVisibility(GV->getVisibility());