Hash :
a07b4213
Author :
Date :
2018-03-22T16:13:13
Move AST transformations to a subdirectory Move AST transformations to compiler/translator/tree_ops. BUG=angleproject:2409 TEST=angle_unittests Change-Id: I9c620e98707d22d005da6192fe7d1b4e8030aadd Reviewed-on: https://chromium-review.googlesource.com/975550 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
//
// Copyright (c) 2002-2015 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.
//
// RemoveSwitchFallThrough.cpp: Remove fall-through from switch statements.
// Note that it is unsafe to do further AST transformations on the AST generated
// by this function. It leaves duplicate nodes in the AST making replacements
// unreliable.
#include "compiler/translator/tree_ops/RemoveSwitchFallThrough.h"
#include "compiler/translator/Diagnostics.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
class RemoveSwitchFallThroughTraverser : public TIntermTraverser
{
public:
static TIntermBlock *removeFallThrough(TIntermBlock *statementList,
PerformanceDiagnostics *perfDiagnostics);
private:
RemoveSwitchFallThroughTraverser(TIntermBlock *statementList,
PerformanceDiagnostics *perfDiagnostics);
void visitSymbol(TIntermSymbol *node) override;
void visitConstantUnion(TIntermConstantUnion *node) override;
bool visitDeclaration(Visit, TIntermDeclaration *node) override;
bool visitBinary(Visit, TIntermBinary *node) override;
bool visitUnary(Visit, TIntermUnary *node) override;
bool visitTernary(Visit visit, TIntermTernary *node) override;
bool visitSwizzle(Visit, TIntermSwizzle *node) override;
bool visitIfElse(Visit visit, TIntermIfElse *node) override;
bool visitSwitch(Visit, TIntermSwitch *node) override;
bool visitCase(Visit, TIntermCase *node) override;
bool visitAggregate(Visit, TIntermAggregate *node) override;
bool visitBlock(Visit, TIntermBlock *node) override;
bool visitLoop(Visit, TIntermLoop *node) override;
bool visitBranch(Visit, TIntermBranch *node) override;
void outputSequence(TIntermSequence *sequence, size_t startIndex);
void handlePreviousCase();
TIntermBlock *mStatementList;
TIntermBlock *mStatementListOut;
bool mLastStatementWasBreak;
TIntermBlock *mPreviousCase;
std::vector<TIntermBlock *> mCasesSharingBreak;
PerformanceDiagnostics *mPerfDiagnostics;
};
TIntermBlock *RemoveSwitchFallThroughTraverser::removeFallThrough(
TIntermBlock *statementList,
PerformanceDiagnostics *perfDiagnostics)
{
RemoveSwitchFallThroughTraverser rm(statementList, perfDiagnostics);
ASSERT(statementList);
statementList->traverse(&rm);
ASSERT(rm.mPreviousCase || statementList->getSequence()->empty());
if (!rm.mLastStatementWasBreak && rm.mPreviousCase)
{
// Make sure that there's a branch at the end of the final case inside the switch statement.
// This also ensures that any cases that fall through to the final case will get the break.
TIntermBranch *finalBreak = new TIntermBranch(EOpBreak, nullptr);
rm.mPreviousCase->getSequence()->push_back(finalBreak);
rm.mLastStatementWasBreak = true;
}
rm.handlePreviousCase();
return rm.mStatementListOut;
}
RemoveSwitchFallThroughTraverser::RemoveSwitchFallThroughTraverser(
TIntermBlock *statementList,
PerformanceDiagnostics *perfDiagnostics)
: TIntermTraverser(true, false, false),
mStatementList(statementList),
mLastStatementWasBreak(false),
mPreviousCase(nullptr),
mPerfDiagnostics(perfDiagnostics)
{
mStatementListOut = new TIntermBlock();
}
void RemoveSwitchFallThroughTraverser::visitSymbol(TIntermSymbol *node)
{
// Note that this assumes that switch statements which don't begin by a case statement
// have already been weeded out in validation.
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
}
void RemoveSwitchFallThroughTraverser::visitConstantUnion(TIntermConstantUnion *node)
{
// Conditions of case labels are not traversed, so this is a constant statement like "0;".
// These are no-ops so there's no need to add them back to the statement list. Should have
// already been pruned out of the AST, in fact.
UNREACHABLE();
}
bool RemoveSwitchFallThroughTraverser::visitDeclaration(Visit, TIntermDeclaration *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitBinary(Visit, TIntermBinary *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitUnary(Visit, TIntermUnary *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitTernary(Visit, TIntermTernary *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitSwizzle(Visit, TIntermSwizzle *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitIfElse(Visit, TIntermIfElse *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitSwitch(Visit, TIntermSwitch *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
// Don't go into nested switch statements
return false;
}
void RemoveSwitchFallThroughTraverser::outputSequence(TIntermSequence *sequence, size_t startIndex)
{
for (size_t i = startIndex; i < sequence->size(); ++i)
{
mStatementListOut->getSequence()->push_back(sequence->at(i));
}
}
void RemoveSwitchFallThroughTraverser::handlePreviousCase()
{
if (mPreviousCase)
mCasesSharingBreak.push_back(mPreviousCase);
if (mLastStatementWasBreak)
{
for (size_t i = 0; i < mCasesSharingBreak.size(); ++i)
{
ASSERT(!mCasesSharingBreak.at(i)->getSequence()->empty());
if (mCasesSharingBreak.at(i)->getSequence()->size() == 1)
{
// Fall-through is allowed in case the label has no statements.
outputSequence(mCasesSharingBreak.at(i)->getSequence(), 0);
}
else
{
// Include all the statements that this case can fall through under the same label.
if (mCasesSharingBreak.size() > i + 1u)
{
mPerfDiagnostics->warning(mCasesSharingBreak.at(i)->getLine(),
"Performance: non-empty fall-through cases in "
"switch statements generate extra code.",
"switch");
}
for (size_t j = i; j < mCasesSharingBreak.size(); ++j)
{
size_t startIndex =
j > i ? 1 : 0; // Add the label only from the first sequence.
outputSequence(mCasesSharingBreak.at(j)->getSequence(), startIndex);
}
}
}
mCasesSharingBreak.clear();
}
mLastStatementWasBreak = false;
mPreviousCase = nullptr;
}
bool RemoveSwitchFallThroughTraverser::visitCase(Visit, TIntermCase *node)
{
handlePreviousCase();
mPreviousCase = new TIntermBlock();
mPreviousCase->getSequence()->push_back(node);
mPreviousCase->setLine(node->getLine());
// Don't traverse the condition of the case statement
return false;
}
bool RemoveSwitchFallThroughTraverser::visitAggregate(Visit, TIntermAggregate *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool DoesBlockAlwaysBreak(TIntermBlock *node)
{
if (node->getSequence()->empty())
{
return false;
}
TIntermBlock *lastStatementAsBlock = node->getSequence()->back()->getAsBlock();
if (lastStatementAsBlock)
{
return DoesBlockAlwaysBreak(lastStatementAsBlock);
}
TIntermBranch *lastStatementAsBranch = node->getSequence()->back()->getAsBranchNode();
return lastStatementAsBranch != nullptr;
}
bool RemoveSwitchFallThroughTraverser::visitBlock(Visit, TIntermBlock *node)
{
if (node != mStatementList)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = DoesBlockAlwaysBreak(node);
return false;
}
return true;
}
bool RemoveSwitchFallThroughTraverser::visitLoop(Visit, TIntermLoop *node)
{
mPreviousCase->getSequence()->push_back(node);
mLastStatementWasBreak = false;
return false;
}
bool RemoveSwitchFallThroughTraverser::visitBranch(Visit, TIntermBranch *node)
{
mPreviousCase->getSequence()->push_back(node);
// TODO: Verify that accepting return or continue statements here doesn't cause problems.
mLastStatementWasBreak = true;
return false;
}
} // anonymous namespace
TIntermBlock *RemoveSwitchFallThrough(TIntermBlock *statementList,
PerformanceDiagnostics *perfDiagnostics)
{
return RemoveSwitchFallThroughTraverser::removeFallThrough(statementList, perfDiagnostics);
}
} // namespace sh