aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CGExprAgg.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-12-02 18:29:00 +0000
committerChris Lattner <sabre@nondot.org>2010-12-02 18:29:00 +0000
commitd1d56df188e25c633f9bc65d229897b42442b0f7 (patch)
tree6a99b850fabd93d5cfc0dc62fcdee12715fabbcf /lib/CodeGen/CGExprAgg.cpp
parent2575585b79d3380ae5175437da160e5b56385fa8 (diff)
fix PR8726 by teaching the aggregate init optimization code to handle
structs with references in them correctly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@120722 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGExprAgg.cpp')
-rw-r--r--lib/CodeGen/CGExprAgg.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp
index ab694e3112..97aabd8bf4 100644
--- a/lib/CodeGen/CGExprAgg.cpp
+++ b/lib/CodeGen/CGExprAgg.cpp
@@ -738,6 +738,39 @@ static uint64_t GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
return CGF.getContext().getTypeSize(E->getType())/8;
+ // InitListExprs for structs have to be handled carefully. If there are
+ // reference members, we need to consider the size of the reference, not the
+ // referencee. InitListExprs for unions and arrays can't have references.
+ if (!E->getType()->isUnionType() && !E->getType()->isArrayType()) {
+ RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
+ uint64_t NumNonZeroBytes = 0;
+
+ unsigned ILEElement = 0;
+ for (RecordDecl::field_iterator Field = SD->field_begin(),
+ FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
+ // We're done once we hit the flexible array member or run out of
+ // InitListExpr elements.
+ if (Field->getType()->isIncompleteArrayType() ||
+ ILEElement == ILE->getNumInits())
+ break;
+ if (Field->isUnnamedBitfield())
+ continue;
+
+ const Expr *E = ILE->getInit(ILEElement++);
+
+ // Reference values are always non-null and have the width of a pointer.
+ if (Field->getType()->isReferenceType()) {
+ NumNonZeroBytes += CGF.getContext().Target.getPointerWidth(0);
+ continue;
+ }
+
+ NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
+ }
+
+ return NumNonZeroBytes;
+ }
+
+
uint64_t NumNonZeroBytes = 0;
for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);