summaryrefslogtreecommitdiffstats
path: root/clang/lib/CodeGen/CGExprConstant.cpp
diff options
context:
space:
mode:
authorMatt Davis <Matthew.Davis@sony.com>2018-02-09 22:10:09 +0000
committerMatt Davis <Matthew.Davis@sony.com>2018-02-09 22:10:09 +0000
commit2930d7662e9c7a90976d41be9086298c5305d7a6 (patch)
tree8b889241b26e394baa5e9ce16e8bfc13a2f89dd5 /clang/lib/CodeGen/CGExprConstant.cpp
parent95a0f39e35040e7385cfde680721c1ea578a1d0f (diff)
downloadbcm5719-llvm-2930d7662e9c7a90976d41be9086298c5305d7a6.tar.gz
bcm5719-llvm-2930d7662e9c7a90976d41be9086298c5305d7a6.zip
[CodeGen] Use the zero initializer instead of storing an all zero representation.
Summary: This change avoids the overhead of storing, and later crawling, an initializer list of all zeros for arrays. When LLVM visits this (llvm/IR/Constants.cpp) ConstantArray::getImpl() it will scan the list looking for an array of all zero. We can avoid the store, and short-cut the scan, by detecting all zeros when clang builds-up the initialization representation. This was brought to my attention when investigating PR36030 Reviewers: majnemer, rjmccall Reviewed By: rjmccall Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42549 llvm-svn: 324776
Diffstat (limited to 'clang/lib/CodeGen/CGExprConstant.cpp')
-rw-r--r--clang/lib/CodeGen/CGExprConstant.cpp12
1 files changed, 10 insertions, 2 deletions
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index d1b9e13a6f9..7b076ea3e65 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -859,9 +859,10 @@ public:
// Copy initializer elements.
SmallVector<llvm::Constant*, 16> Elts;
- Elts.reserve(NumInitableElts + NumElements);
+ Elts.reserve(std::max(NumInitableElts, NumElements));
bool RewriteType = false;
+ bool AllNullValues = true;
for (unsigned i = 0; i < NumInitableElts; ++i) {
Expr *Init = ILE->getInit(i);
llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType);
@@ -869,15 +870,22 @@ public:
return nullptr;
RewriteType |= (C->getType() != ElemTy);
Elts.push_back(C);
+ if (AllNullValues && !C->isNullValue())
+ AllNullValues = false;
}
+ // If all initializer elements are "zero," then avoid storing NumElements
+ // instances of the zero representation.
+ if (AllNullValues)
+ return llvm::ConstantAggregateZero::get(AType);
+
RewriteType |= (fillC->getType() != ElemTy);
Elts.resize(NumElements, fillC);
if (RewriteType) {
// FIXME: Try to avoid packing the array
std::vector<llvm::Type*> Types;
- Types.reserve(NumInitableElts + NumElements);
+ Types.reserve(Elts.size());
for (unsigned i = 0, e = Elts.size(); i < e; ++i)
Types.push_back(Elts[i]->getType());
llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
OpenPOWER on IntegriCloud