Prune unreferenced variables with a constructor initializer
Treat aggregate constructors as having no side effects, which enables
pruning declarators that have a constructor initializer.
Some logic in RemoveUnreferencedVariables is fixed to make this work
correctly for structs. The bugs were previously not exposed since
constructors were treated as having side effects, but now that those
can be pruned the logic needs to be correct.
BUG=angleproject:2298
TEST=angle_unittests
Change-Id: I6fbe61a9e82065196baa29c200bf556fc21d8962
Reviewed-on: https://chromium-review.googlesource.com/856499
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/RemoveUnreferencedVariables.cpp b/src/compiler/translator/RemoveUnreferencedVariables.cpp
index e281256..fac73f1 100644
--- a/src/compiler/translator/RemoveUnreferencedVariables.cpp
+++ b/src/compiler/translator/RemoveUnreferencedVariables.cpp
@@ -177,7 +177,17 @@
if (declarator->getType().isStructSpecifier() && !declarator->getType().isNamelessStruct())
{
unsigned int structId = declarator->getType().getStruct()->uniqueId().get();
- if ((*mStructIdRefCounts)[structId] > 1u)
+ unsigned int structRefCountInThisDeclarator = 1u;
+ if (declarator->getAsBinaryNode() &&
+ declarator->getAsBinaryNode()->getRight()->getAsAggregate())
+ {
+ ASSERT(declarator->getAsBinaryNode()->getLeft()->getType().getStruct() ==
+ declarator->getType().getStruct());
+ ASSERT(declarator->getAsBinaryNode()->getRight()->getType().getStruct() ==
+ declarator->getType().getStruct());
+ structRefCountInThisDeclarator = 2u;
+ }
+ if ((*mStructIdRefCounts)[structId] > structRefCountInThisDeclarator)
{
// If this declaration declares a named struct type that is used elsewhere, we need to
// keep it. We can still change the declarator though so that it doesn't declare an
@@ -272,7 +282,7 @@
bool RemoveUnreferencedVariablesTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
- if (mRemoveReferences)
+ if (visit == PreVisit && mRemoveReferences)
{
decrementStructTypeRefCount(node->getType());
}