Hash :
472c74c6
Author :
Date :
2019-08-19T16:32:13
Translator: Allow tree validation in children of TCompiler This is to be able to perform validation inside TranslatorVulkan, even if it's through ASSERTs. Additionally, every transformation is changed such that they do their validation themselves. TIntermTraverser::updateTree() performs the validation, which indirectly validates many of three tree transformations. Some of the more ancient transformations that don't use this function directly call TCompiler::validateAST. Bug: angleproject:2733 Change-Id: Ie4af029d34e053c5ad1dc8c2c2568eecd625d344 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1761149 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// PruneEmptyCases.cpp: The PruneEmptyCases function prunes cases that are followed by nothing from
// the AST.
#include "compiler/translator/tree_ops/PruneEmptyCases.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
bool AreEmptyBlocks(TIntermSequence *statements);
bool IsEmptyBlock(TIntermNode *node)
{
TIntermBlock *asBlock = node->getAsBlock();
if (asBlock)
{
return AreEmptyBlocks(asBlock->getSequence());
}
// Empty declarations should have already been pruned, otherwise they would need to be handled
// here. Note that declarations for struct types do contain a nameless child node.
ASSERT(node->getAsDeclarationNode() == nullptr ||
!node->getAsDeclarationNode()->getSequence()->empty());
// Pure literal statements should also already be pruned.
ASSERT(node->getAsConstantUnion() == nullptr);
return false;
}
// Return true if all statements in "statements" consist only of empty blocks and no-op statements.
// Returns true also if there are no statements.
bool AreEmptyBlocks(TIntermSequence *statements)
{
for (size_t i = 0u; i < statements->size(); ++i)
{
if (!IsEmptyBlock(statements->at(i)))
{
return false;
}
}
return true;
}
class PruneEmptyCasesTraverser : private TIntermTraverser
{
public:
ANGLE_NO_DISCARD static bool apply(TCompiler *compiler, TIntermBlock *root);
private:
PruneEmptyCasesTraverser();
bool visitSwitch(Visit visit, TIntermSwitch *node) override;
};
bool PruneEmptyCasesTraverser::apply(TCompiler *compiler, TIntermBlock *root)
{
PruneEmptyCasesTraverser prune;
root->traverse(&prune);
return prune.updateTree(compiler, root);
}
PruneEmptyCasesTraverser::PruneEmptyCasesTraverser() : TIntermTraverser(true, false, false) {}
bool PruneEmptyCasesTraverser::visitSwitch(Visit visit, TIntermSwitch *node)
{
// This may mutate the statementList, but that's okay, since traversal has not yet reached
// there.
TIntermBlock *statementList = node->getStatementList();
TIntermSequence *statements = statementList->getSequence();
// Iterate block children in reverse order. Cases that are only followed by other cases or empty
// blocks are marked for pruning.
size_t i = statements->size();
size_t lastNoOpInStatementList = i;
while (i > 0)
{
--i;
TIntermNode *statement = statements->at(i);
if (statement->getAsCaseNode() || IsEmptyBlock(statement))
{
lastNoOpInStatementList = i;
}
else
{
break;
}
}
if (lastNoOpInStatementList == 0)
{
// Remove the entire switch statement, extracting the init expression if needed.
TIntermTyped *init = node->getInit();
if (init->hasSideEffects())
{
queueReplacement(init, OriginalNode::IS_DROPPED);
}
else
{
TIntermSequence emptyReplacement;
ASSERT(getParentNode()->getAsBlock());
mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(getParentNode()->getAsBlock(),
node, emptyReplacement));
}
return false;
}
if (lastNoOpInStatementList < statements->size())
{
statements->erase(statements->begin() + lastNoOpInStatementList, statements->end());
}
return true;
}
} // namespace
bool PruneEmptyCases(TCompiler *compiler, TIntermBlock *root)
{
return PruneEmptyCasesTraverser::apply(compiler, root);
}
} // namespace sh