Corentin Wallez | e839078 | 2017-04-06 14:34:43 -0400 | [diff] [blame] | 1 | // |
| 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 | // PrunePureLiteralStatements.cpp: Removes statements that are literals and nothing else. |
| 7 | |
| 8 | #include "compiler/translator/PrunePureLiteralStatements.h" |
| 9 | |
| 10 | #include "compiler/translator/IntermNode.h" |
| 11 | |
| 12 | namespace sh |
| 13 | { |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | class PrunePureLiteralStatementsTraverser : public TIntermTraverser |
| 19 | { |
| 20 | public: |
| 21 | PrunePureLiteralStatementsTraverser() : TIntermTraverser(true, false, false) {} |
| 22 | |
| 23 | bool visitBlock(Visit visit, TIntermBlock *node) override |
| 24 | { |
| 25 | TIntermSequence *statements = node->getSequence(); |
| 26 | if (statements == nullptr) |
| 27 | { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | // Empty case statements at the end of a switch are invalid: if the last statements |
| 32 | // of a block was a pure literal, also delete all the case statements directly preceding it. |
| 33 | bool deleteCaseStatements = false; |
| 34 | for (int i = static_cast<int>(statements->size()); i-- > 0;) |
| 35 | { |
| 36 | TIntermNode *statement = (*statements)[i]; |
| 37 | |
| 38 | if (statement->getAsConstantUnion() != nullptr) |
| 39 | { |
| 40 | TIntermSequence emptyReplacement; |
| 41 | mMultiReplacements.push_back( |
| 42 | NodeReplaceWithMultipleEntry(node, statement, emptyReplacement)); |
| 43 | |
| 44 | if (i == static_cast<int>(statements->size()) - 1) |
| 45 | { |
| 46 | deleteCaseStatements = true; |
| 47 | } |
| 48 | |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | if (deleteCaseStatements) |
| 53 | { |
| 54 | if (statement->getAsCaseNode() != nullptr) |
| 55 | { |
| 56 | TIntermSequence emptyReplacement; |
| 57 | mMultiReplacements.push_back( |
| 58 | NodeReplaceWithMultipleEntry(node, statement, emptyReplacement)); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | deleteCaseStatements = false; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | bool visitLoop(Visit visit, TIntermLoop *loop) override |
| 71 | { |
| 72 | TIntermTyped *expr = loop->getExpression(); |
| 73 | if (expr != nullptr && expr->getAsConstantUnion() != nullptr) |
| 74 | { |
| 75 | loop->setExpression(nullptr); |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | void PrunePureLiteralStatements(TIntermNode *root) |
| 85 | { |
| 86 | PrunePureLiteralStatementsTraverser prune; |
| 87 | root->traverse(&prune); |
| 88 | prune.updateTree(); |
| 89 | } |
| 90 | |
| 91 | } // namespace sh |