Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2015 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 | // |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 6 | // RemoveSwitchFallThrough.cpp: Remove fall-through from switch statements. |
| 7 | // Note that it is unsafe to do further AST transformations on the AST generated |
| 8 | // by this function. It leaves duplicate nodes in the AST making replacements |
| 9 | // unreliable. |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 10 | |
| 11 | #include "compiler/translator/RemoveSwitchFallThrough.h" |
| 12 | |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 13 | #include "compiler/translator/Diagnostics.h" |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 14 | #include "compiler/translator/IntermTraverse.h" |
| 15 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 16 | namespace sh |
| 17 | { |
| 18 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 19 | namespace |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 20 | { |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 21 | |
| 22 | class RemoveSwitchFallThroughTraverser : public TIntermTraverser |
| 23 | { |
| 24 | public: |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 25 | static TIntermBlock *removeFallThrough(TIntermBlock *statementList, |
| 26 | PerformanceDiagnostics *perfDiagnostics); |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 27 | |
| 28 | private: |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 29 | RemoveSwitchFallThroughTraverser(TIntermBlock *statementList, |
| 30 | PerformanceDiagnostics *perfDiagnostics); |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 31 | |
| 32 | void visitSymbol(TIntermSymbol *node) override; |
| 33 | void visitConstantUnion(TIntermConstantUnion *node) override; |
Olli Etuaho | 78507c6 | 2017-10-10 15:06:45 +0300 | [diff] [blame] | 34 | bool visitDeclaration(Visit, TIntermDeclaration *node) override; |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 35 | bool visitBinary(Visit, TIntermBinary *node) override; |
| 36 | bool visitUnary(Visit, TIntermUnary *node) override; |
| 37 | bool visitTernary(Visit visit, TIntermTernary *node) override; |
Olli Etuaho | 78507c6 | 2017-10-10 15:06:45 +0300 | [diff] [blame] | 38 | bool visitSwizzle(Visit, TIntermSwizzle *node) override; |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 39 | bool visitIfElse(Visit visit, TIntermIfElse *node) override; |
| 40 | bool visitSwitch(Visit, TIntermSwitch *node) override; |
| 41 | bool visitCase(Visit, TIntermCase *node) override; |
| 42 | bool visitAggregate(Visit, TIntermAggregate *node) override; |
| 43 | bool visitBlock(Visit, TIntermBlock *node) override; |
| 44 | bool visitLoop(Visit, TIntermLoop *node) override; |
| 45 | bool visitBranch(Visit, TIntermBranch *node) override; |
| 46 | |
| 47 | void outputSequence(TIntermSequence *sequence, size_t startIndex); |
| 48 | void handlePreviousCase(); |
| 49 | |
| 50 | TIntermBlock *mStatementList; |
| 51 | TIntermBlock *mStatementListOut; |
| 52 | bool mLastStatementWasBreak; |
| 53 | TIntermBlock *mPreviousCase; |
| 54 | std::vector<TIntermBlock *> mCasesSharingBreak; |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 55 | PerformanceDiagnostics *mPerfDiagnostics; |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 56 | }; |
| 57 | |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 58 | TIntermBlock *RemoveSwitchFallThroughTraverser::removeFallThrough( |
| 59 | TIntermBlock *statementList, |
| 60 | PerformanceDiagnostics *perfDiagnostics) |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 61 | { |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 62 | RemoveSwitchFallThroughTraverser rm(statementList, perfDiagnostics); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 63 | ASSERT(statementList); |
| 64 | statementList->traverse(&rm); |
Olli Etuaho | 852fe87 | 2017-10-10 15:13:59 +0300 | [diff] [blame] | 65 | ASSERT(rm.mPreviousCase || statementList->getSequence()->empty()); |
| 66 | if (!rm.mLastStatementWasBreak && rm.mPreviousCase) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 67 | { |
Olli Etuaho | 852fe87 | 2017-10-10 15:13:59 +0300 | [diff] [blame] | 68 | // Make sure that there's a branch at the end of the final case inside the switch statement. |
| 69 | // This also ensures that any cases that fall through to the final case will get the break. |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 70 | TIntermBranch *finalBreak = new TIntermBranch(EOpBreak, nullptr); |
Olli Etuaho | 852fe87 | 2017-10-10 15:13:59 +0300 | [diff] [blame] | 71 | rm.mPreviousCase->getSequence()->push_back(finalBreak); |
| 72 | rm.mLastStatementWasBreak = true; |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 73 | } |
Olli Etuaho | 852fe87 | 2017-10-10 15:13:59 +0300 | [diff] [blame] | 74 | rm.handlePreviousCase(); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 75 | return rm.mStatementListOut; |
| 76 | } |
| 77 | |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 78 | RemoveSwitchFallThroughTraverser::RemoveSwitchFallThroughTraverser( |
| 79 | TIntermBlock *statementList, |
| 80 | PerformanceDiagnostics *perfDiagnostics) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 81 | : TIntermTraverser(true, false, false), |
| 82 | mStatementList(statementList), |
| 83 | mLastStatementWasBreak(false), |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 84 | mPreviousCase(nullptr), |
| 85 | mPerfDiagnostics(perfDiagnostics) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 86 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 87 | mStatementListOut = new TIntermBlock(); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 90 | void RemoveSwitchFallThroughTraverser::visitSymbol(TIntermSymbol *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 91 | { |
| 92 | // Note that this assumes that switch statements which don't begin by a case statement |
| 93 | // have already been weeded out in validation. |
| 94 | mPreviousCase->getSequence()->push_back(node); |
| 95 | mLastStatementWasBreak = false; |
| 96 | } |
| 97 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 98 | void RemoveSwitchFallThroughTraverser::visitConstantUnion(TIntermConstantUnion *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 99 | { |
Olli Etuaho | 87c3588 | 2017-10-19 15:19:38 +0300 | [diff] [blame^] | 100 | // Conditions of case labels are not traversed, so this is a constant statement like "0;". |
| 101 | // These are no-ops so there's no need to add them back to the statement list. Should have |
| 102 | // already been pruned out of the AST, in fact. |
| 103 | UNREACHABLE(); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Olli Etuaho | 78507c6 | 2017-10-10 15:06:45 +0300 | [diff] [blame] | 106 | bool RemoveSwitchFallThroughTraverser::visitDeclaration(Visit, TIntermDeclaration *node) |
| 107 | { |
| 108 | mPreviousCase->getSequence()->push_back(node); |
| 109 | mLastStatementWasBreak = false; |
| 110 | return false; |
| 111 | } |
| 112 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 113 | bool RemoveSwitchFallThroughTraverser::visitBinary(Visit, TIntermBinary *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 114 | { |
| 115 | mPreviousCase->getSequence()->push_back(node); |
| 116 | mLastStatementWasBreak = false; |
| 117 | return false; |
| 118 | } |
| 119 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 120 | bool RemoveSwitchFallThroughTraverser::visitUnary(Visit, TIntermUnary *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 121 | { |
| 122 | mPreviousCase->getSequence()->push_back(node); |
| 123 | mLastStatementWasBreak = false; |
| 124 | return false; |
| 125 | } |
| 126 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 127 | bool RemoveSwitchFallThroughTraverser::visitTernary(Visit, TIntermTernary *node) |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 128 | { |
| 129 | mPreviousCase->getSequence()->push_back(node); |
| 130 | mLastStatementWasBreak = false; |
| 131 | return false; |
| 132 | } |
| 133 | |
Olli Etuaho | 78507c6 | 2017-10-10 15:06:45 +0300 | [diff] [blame] | 134 | bool RemoveSwitchFallThroughTraverser::visitSwizzle(Visit, TIntermSwizzle *node) |
| 135 | { |
| 136 | mPreviousCase->getSequence()->push_back(node); |
| 137 | mLastStatementWasBreak = false; |
| 138 | return false; |
| 139 | } |
| 140 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 141 | bool RemoveSwitchFallThroughTraverser::visitIfElse(Visit, TIntermIfElse *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 142 | { |
| 143 | mPreviousCase->getSequence()->push_back(node); |
| 144 | mLastStatementWasBreak = false; |
| 145 | return false; |
| 146 | } |
| 147 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 148 | bool RemoveSwitchFallThroughTraverser::visitSwitch(Visit, TIntermSwitch *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 149 | { |
| 150 | mPreviousCase->getSequence()->push_back(node); |
| 151 | mLastStatementWasBreak = false; |
| 152 | // Don't go into nested switch statements |
| 153 | return false; |
| 154 | } |
| 155 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 156 | void RemoveSwitchFallThroughTraverser::outputSequence(TIntermSequence *sequence, size_t startIndex) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 157 | { |
| 158 | for (size_t i = startIndex; i < sequence->size(); ++i) |
| 159 | { |
| 160 | mStatementListOut->getSequence()->push_back(sequence->at(i)); |
| 161 | } |
| 162 | } |
| 163 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 164 | void RemoveSwitchFallThroughTraverser::handlePreviousCase() |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 165 | { |
| 166 | if (mPreviousCase) |
| 167 | mCasesSharingBreak.push_back(mPreviousCase); |
| 168 | if (mLastStatementWasBreak) |
| 169 | { |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 170 | for (size_t i = 0; i < mCasesSharingBreak.size(); ++i) |
| 171 | { |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 172 | ASSERT(!mCasesSharingBreak.at(i)->getSequence()->empty()); |
| 173 | if (mCasesSharingBreak.at(i)->getSequence()->size() == 1) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 174 | { |
| 175 | // Fall-through is allowed in case the label has no statements. |
| 176 | outputSequence(mCasesSharingBreak.at(i)->getSequence(), 0); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | // Include all the statements that this case can fall through under the same label. |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 181 | if (mCasesSharingBreak.size() > i + 1u) |
| 182 | { |
| 183 | mPerfDiagnostics->warning(mCasesSharingBreak.at(i)->getLine(), |
| 184 | "Performance: non-empty fall-through cases in " |
| 185 | "switch statements generate extra code.", |
| 186 | "switch"); |
| 187 | } |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 188 | for (size_t j = i; j < mCasesSharingBreak.size(); ++j) |
| 189 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 190 | size_t startIndex = |
| 191 | j > i ? 1 : 0; // Add the label only from the first sequence. |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 192 | outputSequence(mCasesSharingBreak.at(j)->getSequence(), startIndex); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | mCasesSharingBreak.clear(); |
| 197 | } |
| 198 | mLastStatementWasBreak = false; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 199 | mPreviousCase = nullptr; |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 202 | bool RemoveSwitchFallThroughTraverser::visitCase(Visit, TIntermCase *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 203 | { |
| 204 | handlePreviousCase(); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 205 | mPreviousCase = new TIntermBlock(); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 206 | mPreviousCase->getSequence()->push_back(node); |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 207 | mPreviousCase->setLine(node->getLine()); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 208 | // Don't traverse the condition of the case statement |
| 209 | return false; |
| 210 | } |
| 211 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 212 | bool RemoveSwitchFallThroughTraverser::visitAggregate(Visit, TIntermAggregate *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 213 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 214 | mPreviousCase->getSequence()->push_back(node); |
| 215 | mLastStatementWasBreak = false; |
| 216 | return false; |
| 217 | } |
| 218 | |
Olli Etuaho | 4bd730c | 2017-10-10 14:14:19 +0300 | [diff] [blame] | 219 | bool DoesBlockAlwaysBreak(TIntermBlock *node) |
| 220 | { |
| 221 | if (node->getSequence()->empty()) |
| 222 | { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | TIntermBlock *lastStatementAsBlock = node->getSequence()->back()->getAsBlock(); |
| 227 | if (lastStatementAsBlock) |
| 228 | { |
| 229 | return DoesBlockAlwaysBreak(lastStatementAsBlock); |
| 230 | } |
| 231 | |
| 232 | TIntermBranch *lastStatementAsBranch = node->getSequence()->back()->getAsBranchNode(); |
| 233 | return lastStatementAsBranch != nullptr; |
| 234 | } |
| 235 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 236 | bool RemoveSwitchFallThroughTraverser::visitBlock(Visit, TIntermBlock *node) |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 237 | { |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 238 | if (node != mStatementList) |
| 239 | { |
| 240 | mPreviousCase->getSequence()->push_back(node); |
Olli Etuaho | 4bd730c | 2017-10-10 14:14:19 +0300 | [diff] [blame] | 241 | mLastStatementWasBreak = DoesBlockAlwaysBreak(node); |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | return true; |
| 245 | } |
| 246 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 247 | bool RemoveSwitchFallThroughTraverser::visitLoop(Visit, TIntermLoop *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 248 | { |
| 249 | mPreviousCase->getSequence()->push_back(node); |
| 250 | mLastStatementWasBreak = false; |
| 251 | return false; |
| 252 | } |
| 253 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 254 | bool RemoveSwitchFallThroughTraverser::visitBranch(Visit, TIntermBranch *node) |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 255 | { |
| 256 | mPreviousCase->getSequence()->push_back(node); |
| 257 | // TODO: Verify that accepting return or continue statements here doesn't cause problems. |
| 258 | mLastStatementWasBreak = true; |
| 259 | return false; |
| 260 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 261 | |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 262 | } // anonymous namespace |
| 263 | |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 264 | TIntermBlock *RemoveSwitchFallThrough(TIntermBlock *statementList, |
| 265 | PerformanceDiagnostics *perfDiagnostics) |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 266 | { |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 267 | return RemoveSwitchFallThroughTraverser::removeFallThrough(statementList, perfDiagnostics); |
Olli Etuaho | af5070f | 2017-10-10 13:53:25 +0300 | [diff] [blame] | 268 | } |
| 269 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 270 | } // namespace sh |