diff options
author | Mehdi Amini <joker.eph@gmail.com> | 2017-08-27 20:24:09 +0000 |
---|---|---|
committer | Mehdi Amini <joker.eph@gmail.com> | 2017-08-27 20:24:09 +0000 |
commit | 0682709a5e5c9df240e0fd7bccd8f7115b25506b (patch) | |
tree | 43fe7b4ca86bcf3157762a69490778fda14a88ff /lib/CodeGen/CodeGenModule.cpp | |
parent | dd5ef8f2a0cf7b27c33854a3017a15ce05c6b19a (diff) |
Emit static constexpr member as available_externally definition
By exposing the constant initializer, the optimizer can fold many
of these constructs.
Differential Revision: https://reviews.llvm.org/D34992
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311857 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CodeGenModule.cpp')
-rw-r--r-- | lib/CodeGen/CodeGenModule.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index c726d90f2e..5feb6b4d76 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -2437,6 +2437,28 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, D->getType().isConstant(Context) && isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) GV->setSection(".cp.rodata"); + + // Check if we a have a const declaration with an initializer, we may be + // able to emit it as available_externally to expose it's value to the + // optimizer. + if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() && + D->getType().isConstQualified() && !GV->hasInitializer() && + !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) { + const auto *Record = + Context.getBaseElementType(D->getType())->getAsCXXRecordDecl(); + bool HasMutableFields = Record && Record->hasMutableFields(); + if (!HasMutableFields) { + const VarDecl *InitDecl; + const Expr *InitExpr = D->getAnyInitializer(InitDecl); + if (InitExpr) { + GV->setConstant(true); + GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); + ConstantEmitter emitter(*this); + GV->setInitializer(emitter.tryEmitForInitializer(*InitDecl)); + emitter.finalize(GV); + } + } + } } auto ExpectedAS = |