blob: e281256329d96c88b7da62a10bd3184d82b21b8a [file] [log] [blame]
Olli Etuaho3d70ca92017-11-10 16:53:26 +02001//
2// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// RemoveUnreferencedVariables.cpp:
7// Drop variables that are declared but never referenced in the AST. This avoids adding unnecessary
Olli Etuaho39f74df2017-11-20 16:09:57 +02008// initialization code for them. Also removes unreferenced struct types.
Olli Etuaho3d70ca92017-11-10 16:53:26 +02009//
10
11#include "compiler/translator/RemoveUnreferencedVariables.h"
12
13#include "compiler/translator/IntermTraverse.h"
14#include "compiler/translator/SymbolTable.h"
15
16namespace sh
17{
18
19namespace
20{
21
22class CollectVariableRefCountsTraverser : public TIntermTraverser
23{
24 public:
25 CollectVariableRefCountsTraverser();
26
27 using RefCountMap = std::unordered_map<int, unsigned int>;
28 RefCountMap &getSymbolIdRefCounts() { return mSymbolIdRefCounts; }
Olli Etuaho39f74df2017-11-20 16:09:57 +020029 RefCountMap &getStructIdRefCounts() { return mStructIdRefCounts; }
Olli Etuaho3d70ca92017-11-10 16:53:26 +020030
31 void visitSymbol(TIntermSymbol *node) override;
Olli Etuaho39f74df2017-11-20 16:09:57 +020032 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
33 bool visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node) override;
Olli Etuaho3d70ca92017-11-10 16:53:26 +020034
35 private:
Olli Etuaho39f74df2017-11-20 16:09:57 +020036 void incrementStructTypeRefCount(const TType &type);
37
Olli Etuaho3d70ca92017-11-10 16:53:26 +020038 RefCountMap mSymbolIdRefCounts;
Olli Etuaho39f74df2017-11-20 16:09:57 +020039
40 // Structure reference counts are counted from symbols, constructors, function calls, function
41 // return values and from interface block and structure fields. We need to track both function
42 // calls and function return values since there's a compiler option not to prune unused
43 // functions. The type of a constant union may also be a struct, but statements that are just a
44 // constant union are always pruned, and if the constant union is used somehow it will get
45 // counted by something else.
46 RefCountMap mStructIdRefCounts;
Olli Etuaho3d70ca92017-11-10 16:53:26 +020047};
48
49CollectVariableRefCountsTraverser::CollectVariableRefCountsTraverser()
50 : TIntermTraverser(true, false, false)
51{
52}
53
Olli Etuaho39f74df2017-11-20 16:09:57 +020054void CollectVariableRefCountsTraverser::incrementStructTypeRefCount(const TType &type)
55{
56 if (type.isInterfaceBlock())
57 {
58 const auto *block = type.getInterfaceBlock();
59 ASSERT(block);
60
61 // We can end up incrementing ref counts of struct types referenced from an interface block
62 // multiple times for the same block. This doesn't matter, because interface blocks can't be
63 // pruned so we'll never do the reverse operation.
64 for (const auto &field : block->fields())
65 {
66 ASSERT(!field->type()->isInterfaceBlock());
67 incrementStructTypeRefCount(*field->type());
68 }
69 return;
70 }
71
72 const auto *structure = type.getStruct();
73 if (structure != nullptr)
74 {
Olli Etuaho97fa8552017-11-28 16:28:42 +020075 auto structIter = mStructIdRefCounts.find(structure->uniqueId().get());
Olli Etuaho39f74df2017-11-20 16:09:57 +020076 if (structIter == mStructIdRefCounts.end())
77 {
Olli Etuaho97fa8552017-11-28 16:28:42 +020078 mStructIdRefCounts[structure->uniqueId().get()] = 1u;
Olli Etuaho39f74df2017-11-20 16:09:57 +020079
80 for (const auto &field : structure->fields())
81 {
82 incrementStructTypeRefCount(*field->type());
83 }
84
85 return;
86 }
87 ++(structIter->second);
88 }
89}
90
Olli Etuaho3d70ca92017-11-10 16:53:26 +020091void CollectVariableRefCountsTraverser::visitSymbol(TIntermSymbol *node)
92{
Olli Etuaho39f74df2017-11-20 16:09:57 +020093 incrementStructTypeRefCount(node->getType());
94
Olli Etuahob6af22b2017-12-15 14:05:44 +020095 auto iter = mSymbolIdRefCounts.find(node->uniqueId().get());
Olli Etuaho3d70ca92017-11-10 16:53:26 +020096 if (iter == mSymbolIdRefCounts.end())
97 {
Olli Etuahob6af22b2017-12-15 14:05:44 +020098 mSymbolIdRefCounts[node->uniqueId().get()] = 1u;
Olli Etuaho3d70ca92017-11-10 16:53:26 +020099 return;
100 }
101 ++(iter->second);
102}
103
Olli Etuaho39f74df2017-11-20 16:09:57 +0200104bool CollectVariableRefCountsTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
105{
106 // This tracks struct references in both function calls and constructors.
107 incrementStructTypeRefCount(node->getType());
108 return true;
109}
110
111bool CollectVariableRefCountsTraverser::visitFunctionPrototype(Visit visit,
112 TIntermFunctionPrototype *node)
113{
114 incrementStructTypeRefCount(node->getType());
115 return true;
116}
117
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200118// Traverser that removes all unreferenced variables on one traversal.
119class RemoveUnreferencedVariablesTraverser : public TIntermTraverser
120{
121 public:
122 RemoveUnreferencedVariablesTraverser(
123 CollectVariableRefCountsTraverser::RefCountMap *symbolIdRefCounts,
Olli Etuaho39f74df2017-11-20 16:09:57 +0200124 CollectVariableRefCountsTraverser::RefCountMap *structIdRefCounts,
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200125 TSymbolTable *symbolTable);
126
127 bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
128 void visitSymbol(TIntermSymbol *node) override;
Olli Etuaho39f74df2017-11-20 16:09:57 +0200129 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200130
131 // Traverse loop and block nodes in reverse order. Note that this traverser does not track
132 // parent block positions, so insertStatementInParentBlock is unusable!
133 void traverseBlock(TIntermBlock *block) override;
134 void traverseLoop(TIntermLoop *loop) override;
135
136 private:
Olli Etuaho39f74df2017-11-20 16:09:57 +0200137 void removeVariableDeclaration(TIntermDeclaration *node, TIntermTyped *declarator);
138 void decrementStructTypeRefCount(const TType &type);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200139
140 CollectVariableRefCountsTraverser::RefCountMap *mSymbolIdRefCounts;
Olli Etuaho39f74df2017-11-20 16:09:57 +0200141 CollectVariableRefCountsTraverser::RefCountMap *mStructIdRefCounts;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200142 bool mRemoveReferences;
143};
144
145RemoveUnreferencedVariablesTraverser::RemoveUnreferencedVariablesTraverser(
146 CollectVariableRefCountsTraverser::RefCountMap *symbolIdRefCounts,
Olli Etuaho39f74df2017-11-20 16:09:57 +0200147 CollectVariableRefCountsTraverser::RefCountMap *structIdRefCounts,
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200148 TSymbolTable *symbolTable)
149 : TIntermTraverser(true, false, true, symbolTable),
150 mSymbolIdRefCounts(symbolIdRefCounts),
Olli Etuaho39f74df2017-11-20 16:09:57 +0200151 mStructIdRefCounts(structIdRefCounts),
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200152 mRemoveReferences(false)
153{
154}
155
Olli Etuaho39f74df2017-11-20 16:09:57 +0200156void RemoveUnreferencedVariablesTraverser::decrementStructTypeRefCount(const TType &type)
157{
158 auto *structure = type.getStruct();
159 if (structure != nullptr)
160 {
Olli Etuaho97fa8552017-11-28 16:28:42 +0200161 ASSERT(mStructIdRefCounts->find(structure->uniqueId().get()) != mStructIdRefCounts->end());
162 unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId().get()];
Olli Etuaho39f74df2017-11-20 16:09:57 +0200163
164 if (structRefCount == 0)
165 {
166 for (const auto &field : structure->fields())
167 {
168 decrementStructTypeRefCount(*field->type());
169 }
170 }
171 }
172}
173
174void RemoveUnreferencedVariablesTraverser::removeVariableDeclaration(TIntermDeclaration *node,
175 TIntermTyped *declarator)
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200176{
177 if (declarator->getType().isStructSpecifier() && !declarator->getType().isNamelessStruct())
178 {
Olli Etuaho97fa8552017-11-28 16:28:42 +0200179 unsigned int structId = declarator->getType().getStruct()->uniqueId().get();
Olli Etuaho39f74df2017-11-20 16:09:57 +0200180 if ((*mStructIdRefCounts)[structId] > 1u)
181 {
182 // If this declaration declares a named struct type that is used elsewhere, we need to
183 // keep it. We can still change the declarator though so that it doesn't declare an
184 // unreferenced variable.
185
186 // Note that since we're not removing the entire declaration, the struct's reference
187 // count will end up being one less than the correct refcount. But since the struct
188 // declaration is kept, the incorrect refcount can't cause any other problems.
189
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200190 if (declarator->getAsSymbolNode() &&
191 declarator->getAsSymbolNode()->variable().symbolType() == SymbolType::Empty)
Olli Etuaho39f74df2017-11-20 16:09:57 +0200192 {
193 // Already an empty declaration - nothing to do.
194 return;
195 }
Olli Etuahoae4dbf32017-12-08 20:49:00 +0100196 TVariable *emptyVariable =
197 new TVariable(mSymbolTable, nullptr, declarator->getType(), SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +0200198 queueReplacementWithParent(node, declarator, new TIntermSymbol(emptyVariable),
Olli Etuaho39f74df2017-11-20 16:09:57 +0200199 OriginalNode::IS_DROPPED);
200 return;
201 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200202 }
203
204 if (getParentNode()->getAsBlock())
205 {
206 TIntermSequence emptyReplacement;
207 mMultiReplacements.push_back(
208 NodeReplaceWithMultipleEntry(getParentNode()->getAsBlock(), node, emptyReplacement));
209 }
210 else
211 {
212 ASSERT(getParentNode()->getAsLoopNode());
213 queueReplacement(nullptr, OriginalNode::IS_DROPPED);
214 }
215}
216
217bool RemoveUnreferencedVariablesTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
218{
219 if (visit == PreVisit)
220 {
221 // SeparateDeclarations should have already been run.
222 ASSERT(node->getSequence()->size() == 1u);
223
224 TIntermTyped *declarator = node->getSequence()->back()->getAsTyped();
225 ASSERT(declarator);
226
227 // We can only remove variables that are not a part of the shader interface.
228 TQualifier qualifier = declarator->getQualifier();
229 if (qualifier != EvqTemporary && qualifier != EvqGlobal)
230 {
231 return true;
232 }
233
Olli Etuaho39f74df2017-11-20 16:09:57 +0200234 bool canRemoveVariable = false;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200235 TIntermSymbol *symbolNode = declarator->getAsSymbolNode();
236 if (symbolNode != nullptr)
237 {
Olli Etuahob6af22b2017-12-15 14:05:44 +0200238 canRemoveVariable = (*mSymbolIdRefCounts)[symbolNode->uniqueId().get()] == 1u ||
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200239 symbolNode->variable().symbolType() == SymbolType::Empty;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200240 }
241 TIntermBinary *initNode = declarator->getAsBinaryNode();
242 if (initNode != nullptr)
243 {
244 ASSERT(initNode->getLeft()->getAsSymbolNode());
Olli Etuahob6af22b2017-12-15 14:05:44 +0200245 int symbolId = initNode->getLeft()->getAsSymbolNode()->uniqueId().get();
Olli Etuaho39f74df2017-11-20 16:09:57 +0200246 canRemoveVariable =
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200247 (*mSymbolIdRefCounts)[symbolId] == 1u && !initNode->getRight()->hasSideEffects();
248 }
249
Olli Etuaho39f74df2017-11-20 16:09:57 +0200250 if (canRemoveVariable)
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200251 {
Olli Etuaho39f74df2017-11-20 16:09:57 +0200252 removeVariableDeclaration(node, declarator);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200253 mRemoveReferences = true;
254 }
255 return true;
256 }
257 ASSERT(visit == PostVisit);
258 mRemoveReferences = false;
259 return true;
260}
261
262void RemoveUnreferencedVariablesTraverser::visitSymbol(TIntermSymbol *node)
263{
264 if (mRemoveReferences)
265 {
Olli Etuahob6af22b2017-12-15 14:05:44 +0200266 ASSERT(mSymbolIdRefCounts->find(node->uniqueId().get()) != mSymbolIdRefCounts->end());
267 --(*mSymbolIdRefCounts)[node->uniqueId().get()];
Olli Etuaho39f74df2017-11-20 16:09:57 +0200268
269 decrementStructTypeRefCount(node->getType());
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200270 }
271}
272
Olli Etuaho39f74df2017-11-20 16:09:57 +0200273bool RemoveUnreferencedVariablesTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
274{
275 if (mRemoveReferences)
276 {
277 decrementStructTypeRefCount(node->getType());
278 }
279 return true;
280}
281
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200282void RemoveUnreferencedVariablesTraverser::traverseBlock(TIntermBlock *node)
283{
284 // We traverse blocks in reverse order. This way reference counts can be decremented when
285 // removing initializers, and variables that become unused when initializers are removed can be
286 // removed on the same traversal.
287
288 ScopedNodeInTraversalPath addToPath(this, node);
289
290 bool visit = true;
291
292 TIntermSequence *sequence = node->getSequence();
293
294 if (preVisit)
295 visit = visitBlock(PreVisit, node);
296
297 if (visit)
298 {
299 for (auto iter = sequence->rbegin(); iter != sequence->rend(); ++iter)
300 {
301 (*iter)->traverse(this);
302 if (visit && inVisit)
303 {
304 if ((iter + 1) != sequence->rend())
305 visit = visitBlock(InVisit, node);
306 }
307 }
308 }
309
310 if (visit && postVisit)
311 visitBlock(PostVisit, node);
312}
313
314void RemoveUnreferencedVariablesTraverser::traverseLoop(TIntermLoop *node)
315{
316 // We traverse loops in reverse order as well. The loop body gets traversed before the init
317 // node.
318
319 ScopedNodeInTraversalPath addToPath(this, node);
320
321 bool visit = true;
322
323 if (preVisit)
324 visit = visitLoop(PreVisit, node);
325
326 if (visit)
327 {
328 // We don't need to traverse loop expressions or conditions since they can't be declarations
329 // in the AST (loops which have a declaration in their condition get transformed in the
330 // parsing stage).
331 ASSERT(node->getExpression() == nullptr ||
332 node->getExpression()->getAsDeclarationNode() == nullptr);
333 ASSERT(node->getCondition() == nullptr ||
334 node->getCondition()->getAsDeclarationNode() == nullptr);
335
336 if (node->getBody())
337 node->getBody()->traverse(this);
338
339 if (node->getInit())
340 node->getInit()->traverse(this);
341 }
342
343 if (visit && postVisit)
344 visitLoop(PostVisit, node);
345}
346
347} // namespace
348
349void RemoveUnreferencedVariables(TIntermBlock *root, TSymbolTable *symbolTable)
350{
351 CollectVariableRefCountsTraverser collector;
352 root->traverse(&collector);
Olli Etuaho39f74df2017-11-20 16:09:57 +0200353 RemoveUnreferencedVariablesTraverser traverser(&collector.getSymbolIdRefCounts(),
354 &collector.getStructIdRefCounts(), symbolTable);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200355 root->traverse(&traverser);
356 traverser.updateTree();
357}
358
359} // namespace sh