blob: ad146483e9ffe039d3305c5de8ff8170e72b3798 [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 Etuahoea78d2b2018-01-09 12:55:27 +0200180 unsigned int structRefCountInThisDeclarator = 1u;
181 if (declarator->getAsBinaryNode() &&
182 declarator->getAsBinaryNode()->getRight()->getAsAggregate())
183 {
184 ASSERT(declarator->getAsBinaryNode()->getLeft()->getType().getStruct() ==
185 declarator->getType().getStruct());
186 ASSERT(declarator->getAsBinaryNode()->getRight()->getType().getStruct() ==
187 declarator->getType().getStruct());
188 structRefCountInThisDeclarator = 2u;
189 }
190 if ((*mStructIdRefCounts)[structId] > structRefCountInThisDeclarator)
Olli Etuaho39f74df2017-11-20 16:09:57 +0200191 {
192 // If this declaration declares a named struct type that is used elsewhere, we need to
193 // keep it. We can still change the declarator though so that it doesn't declare an
194 // unreferenced variable.
195
196 // Note that since we're not removing the entire declaration, the struct's reference
197 // count will end up being one less than the correct refcount. But since the struct
198 // declaration is kept, the incorrect refcount can't cause any other problems.
199
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200200 if (declarator->getAsSymbolNode() &&
201 declarator->getAsSymbolNode()->variable().symbolType() == SymbolType::Empty)
Olli Etuaho39f74df2017-11-20 16:09:57 +0200202 {
203 // Already an empty declaration - nothing to do.
204 return;
205 }
Olli Etuahob60d30f2018-01-16 12:31:06 +0200206 TVariable *emptyVariable = new TVariable(
207 mSymbolTable, nullptr, new TType(declarator->getType()), SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +0200208 queueReplacementWithParent(node, declarator, new TIntermSymbol(emptyVariable),
Olli Etuaho39f74df2017-11-20 16:09:57 +0200209 OriginalNode::IS_DROPPED);
210 return;
211 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200212 }
213
214 if (getParentNode()->getAsBlock())
215 {
216 TIntermSequence emptyReplacement;
217 mMultiReplacements.push_back(
218 NodeReplaceWithMultipleEntry(getParentNode()->getAsBlock(), node, emptyReplacement));
219 }
220 else
221 {
222 ASSERT(getParentNode()->getAsLoopNode());
223 queueReplacement(nullptr, OriginalNode::IS_DROPPED);
224 }
225}
226
227bool RemoveUnreferencedVariablesTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
228{
229 if (visit == PreVisit)
230 {
231 // SeparateDeclarations should have already been run.
232 ASSERT(node->getSequence()->size() == 1u);
233
234 TIntermTyped *declarator = node->getSequence()->back()->getAsTyped();
235 ASSERT(declarator);
236
237 // We can only remove variables that are not a part of the shader interface.
238 TQualifier qualifier = declarator->getQualifier();
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200239 if (qualifier != EvqTemporary && qualifier != EvqGlobal && qualifier != EvqConst)
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200240 {
241 return true;
242 }
243
Olli Etuaho39f74df2017-11-20 16:09:57 +0200244 bool canRemoveVariable = false;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200245 TIntermSymbol *symbolNode = declarator->getAsSymbolNode();
246 if (symbolNode != nullptr)
247 {
Olli Etuahob6af22b2017-12-15 14:05:44 +0200248 canRemoveVariable = (*mSymbolIdRefCounts)[symbolNode->uniqueId().get()] == 1u ||
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200249 symbolNode->variable().symbolType() == SymbolType::Empty;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200250 }
251 TIntermBinary *initNode = declarator->getAsBinaryNode();
252 if (initNode != nullptr)
253 {
254 ASSERT(initNode->getLeft()->getAsSymbolNode());
Olli Etuahob6af22b2017-12-15 14:05:44 +0200255 int symbolId = initNode->getLeft()->getAsSymbolNode()->uniqueId().get();
Olli Etuaho39f74df2017-11-20 16:09:57 +0200256 canRemoveVariable =
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200257 (*mSymbolIdRefCounts)[symbolId] == 1u && !initNode->getRight()->hasSideEffects();
258 }
259
Olli Etuaho39f74df2017-11-20 16:09:57 +0200260 if (canRemoveVariable)
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200261 {
Olli Etuaho39f74df2017-11-20 16:09:57 +0200262 removeVariableDeclaration(node, declarator);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200263 mRemoveReferences = true;
264 }
265 return true;
266 }
267 ASSERT(visit == PostVisit);
268 mRemoveReferences = false;
269 return true;
270}
271
272void RemoveUnreferencedVariablesTraverser::visitSymbol(TIntermSymbol *node)
273{
274 if (mRemoveReferences)
275 {
Olli Etuahob6af22b2017-12-15 14:05:44 +0200276 ASSERT(mSymbolIdRefCounts->find(node->uniqueId().get()) != mSymbolIdRefCounts->end());
277 --(*mSymbolIdRefCounts)[node->uniqueId().get()];
Olli Etuaho39f74df2017-11-20 16:09:57 +0200278
279 decrementStructTypeRefCount(node->getType());
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200280 }
281}
282
Olli Etuaho39f74df2017-11-20 16:09:57 +0200283bool RemoveUnreferencedVariablesTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
284{
Olli Etuahoea78d2b2018-01-09 12:55:27 +0200285 if (visit == PreVisit && mRemoveReferences)
Olli Etuaho39f74df2017-11-20 16:09:57 +0200286 {
287 decrementStructTypeRefCount(node->getType());
288 }
289 return true;
290}
291
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200292void RemoveUnreferencedVariablesTraverser::traverseBlock(TIntermBlock *node)
293{
294 // We traverse blocks in reverse order. This way reference counts can be decremented when
295 // removing initializers, and variables that become unused when initializers are removed can be
296 // removed on the same traversal.
297
298 ScopedNodeInTraversalPath addToPath(this, node);
299
300 bool visit = true;
301
302 TIntermSequence *sequence = node->getSequence();
303
304 if (preVisit)
305 visit = visitBlock(PreVisit, node);
306
307 if (visit)
308 {
309 for (auto iter = sequence->rbegin(); iter != sequence->rend(); ++iter)
310 {
311 (*iter)->traverse(this);
312 if (visit && inVisit)
313 {
314 if ((iter + 1) != sequence->rend())
315 visit = visitBlock(InVisit, node);
316 }
317 }
318 }
319
320 if (visit && postVisit)
321 visitBlock(PostVisit, node);
322}
323
324void RemoveUnreferencedVariablesTraverser::traverseLoop(TIntermLoop *node)
325{
326 // We traverse loops in reverse order as well. The loop body gets traversed before the init
327 // node.
328
329 ScopedNodeInTraversalPath addToPath(this, node);
330
331 bool visit = true;
332
333 if (preVisit)
334 visit = visitLoop(PreVisit, node);
335
336 if (visit)
337 {
338 // We don't need to traverse loop expressions or conditions since they can't be declarations
339 // in the AST (loops which have a declaration in their condition get transformed in the
340 // parsing stage).
341 ASSERT(node->getExpression() == nullptr ||
342 node->getExpression()->getAsDeclarationNode() == nullptr);
343 ASSERT(node->getCondition() == nullptr ||
344 node->getCondition()->getAsDeclarationNode() == nullptr);
345
346 if (node->getBody())
347 node->getBody()->traverse(this);
348
349 if (node->getInit())
350 node->getInit()->traverse(this);
351 }
352
353 if (visit && postVisit)
354 visitLoop(PostVisit, node);
355}
356
357} // namespace
358
359void RemoveUnreferencedVariables(TIntermBlock *root, TSymbolTable *symbolTable)
360{
361 CollectVariableRefCountsTraverser collector;
362 root->traverse(&collector);
Olli Etuaho39f74df2017-11-20 16:09:57 +0200363 RemoveUnreferencedVariablesTraverser traverser(&collector.getSymbolIdRefCounts(),
364 &collector.getStructIdRefCounts(), symbolTable);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200365 root->traverse(&traverser);
366 traverser.updateTree();
367}
368
369} // namespace sh