r207241 - CodeGen: Cleanup variable linkage calculation

David Majnemer david.majnemer at gmail.com
Fri Apr 25 10:08:41 PDT 2014


Author: majnemer
Date: Fri Apr 25 12:08:41 2014
New Revision: 207241

URL: http://llvm.org/viewvc/llvm-project?rev=207241&view=rev
Log:
CodeGen: Cleanup variable linkage calculation

Almost all linkage calculation for VarDecls occured inside of
GetLLVMLinkageVarDefinition except for static data members.  Centralize
the logic so that it can be more readily reused.

No functionality change.

Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=207241&r1=207240&r2=207241&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Fri Apr 25 12:08:41 2014
@@ -1565,6 +1565,18 @@ bool CodeGenModule::isTypeConstant(QualT
   return true;
 }
 
+static bool isVarDeclInlineInitializedStaticDataMember(const VarDecl *VD) {
+  if (!VD->isStaticDataMember())
+    return false;
+  const VarDecl *InitDecl;
+  const Expr *InitExpr = VD->getAnyInitializer(InitDecl);
+  if (!InitExpr)
+    return false;
+  if (InitDecl->isThisDeclarationADefinition())
+    return false;
+  return true;
+}
+
 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
 /// create and return an llvm GlobalVariable with the specified type.  If there
 /// is something in the module with the specified name, return it potentially
@@ -1633,8 +1645,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(Str
     // If required by the ABI, treat declarations of static data members with
     // inline initializers as definitions.
     if (getCXXABI().isInlineInitializedStaticDataMemberLinkOnce() &&
-        D->isStaticDataMember() && D->hasInit() &&
-        !D->isThisDeclarationADefinition())
+        isVarDeclInlineInitializedStaticDataMember(D))
       EmitGlobalVarDefinition(D);
   }
 
@@ -1900,13 +1911,6 @@ void CodeGenModule::EmitGlobalVarDefinit
   else if (D->hasAttr<DLLExportAttr>())
     GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
 
-  // If required by the ABI, give definitions of static data members with inline
-  // initializers linkonce_odr linkage.
-  if (getCXXABI().isInlineInitializedStaticDataMemberLinkOnce() &&
-      D->isStaticDataMember() && InitExpr &&
-      !InitDecl->isThisDeclarationADefinition())
-    GV->setLinkage(llvm::GlobalVariable::LinkOnceODRLinkage);
-
   if (Linkage == llvm::GlobalVariable::CommonLinkage)
     // common vars aren't constant even if declared const.
     GV->setConstant(false);
@@ -1992,6 +1996,11 @@ CodeGenModule::GetLLVMLinkageVarDefiniti
     // Itanium-specified entry point, which has the normal linkage of the
     // variable.
     return llvm::GlobalValue::InternalLinkage;
+  else if (getCXXABI().isInlineInitializedStaticDataMemberLinkOnce() &&
+           isVarDeclInlineInitializedStaticDataMember(D))
+    // If required by the ABI, give definitions of static data members with inline
+    // initializers linkonce_odr linkage.
+    return llvm::GlobalVariable::LinkOnceODRLinkage;
   // C++ doesn't have tentative definitions and thus cannot have common linkage.
   else if (!getLangOpts().CPlusPlus &&
            !isVarDeclStrongDefinition(D, CodeGenOpts.NoCommon))





More information about the cfe-commits mailing list