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 | f23847604b2db5a88f0b0b88a2380407b3e7d03f (patch) | |
tree | e5ad6030e5b791db281a3c9822ff9c0eafa45e3b /clang/lib/CodeGen/CodeGenModule.cpp | |
parent | 80075a5fb782989f2b14213e44b4e689f9929789 (diff) | |
download | bcm5719-llvm-f23847604b2db5a88f0b0b88a2380407b3e7d03f.tar.gz bcm5719-llvm-f23847604b2db5a88f0b0b88a2380407b3e7d03f.zip |
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
llvm-svn: 311857
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index c726d90f2e3..5feb6b4d768 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/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 = |