Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2016 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 | // SimplifyLoopConditions is an AST traverser that converts loop conditions and loop expressions |
| 7 | // to regular statements inside the loop. This way further transformations that generate statements |
| 8 | // from loop conditions and loop expressions work correctly. |
| 9 | // |
| 10 | |
| 11 | #include "compiler/translator/SimplifyLoopConditions.h" |
| 12 | |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 13 | #include "compiler/translator/IntermNodePatternMatcher.h" |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 14 | #include "compiler/translator/IntermNode_util.h" |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 15 | #include "compiler/translator/IntermTraverse.h" |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 16 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 17 | namespace sh |
| 18 | { |
| 19 | |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 20 | namespace |
| 21 | { |
| 22 | |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 23 | class SimplifyLoopConditionsTraverser : public TLValueTrackingTraverser |
| 24 | { |
| 25 | public: |
| 26 | SimplifyLoopConditionsTraverser(unsigned int conditionsToSimplifyMask, |
| 27 | const TSymbolTable &symbolTable, |
| 28 | int shaderVersion); |
| 29 | |
| 30 | void traverseLoop(TIntermLoop *node) override; |
| 31 | |
| 32 | bool visitBinary(Visit visit, TIntermBinary *node) override; |
| 33 | bool visitAggregate(Visit visit, TIntermAggregate *node) override; |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 34 | bool visitTernary(Visit visit, TIntermTernary *node) override; |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 35 | bool visitDeclaration(Visit visit, TIntermDeclaration *node) override; |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 36 | |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 37 | bool foundLoopToChange() const { return mFoundLoopToChange; } |
| 38 | |
| 39 | protected: |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 40 | // Marked to true once an operation that needs to be hoisted out of a loop expression has been |
| 41 | // found. |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 42 | bool mFoundLoopToChange; |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 43 | bool mInsideLoopInitConditionOrExpression; |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 44 | IntermNodePatternMatcher mConditionsToSimplify; |
| 45 | }; |
| 46 | |
| 47 | SimplifyLoopConditionsTraverser::SimplifyLoopConditionsTraverser( |
| 48 | unsigned int conditionsToSimplifyMask, |
| 49 | const TSymbolTable &symbolTable, |
| 50 | int shaderVersion) |
| 51 | : TLValueTrackingTraverser(true, false, false, symbolTable, shaderVersion), |
| 52 | mFoundLoopToChange(false), |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 53 | mInsideLoopInitConditionOrExpression(false), |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 54 | mConditionsToSimplify(conditionsToSimplifyMask) |
| 55 | { |
| 56 | } |
| 57 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 58 | // If we're inside a loop initialization, condition, or expression, we check for expressions that |
| 59 | // should be moved out of the loop condition or expression. If one is found, the loop is |
| 60 | // transformed. |
| 61 | // If we're not inside loop initialization, condition, or expression, we only need to traverse nodes |
| 62 | // that may contain loops. |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 63 | |
| 64 | bool SimplifyLoopConditionsTraverser::visitBinary(Visit visit, TIntermBinary *node) |
| 65 | { |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 66 | if (!mInsideLoopInitConditionOrExpression) |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 67 | return false; |
| 68 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 69 | if (mFoundLoopToChange) |
| 70 | return false; // Already decided to change this loop. |
| 71 | |
Jamie Madill | 666f65a | 2016-08-26 01:34:37 +0000 | [diff] [blame] | 72 | mFoundLoopToChange = mConditionsToSimplify.match(node, getParentNode(), isLValueRequiredHere()); |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 73 | return !mFoundLoopToChange; |
| 74 | } |
| 75 | |
| 76 | bool SimplifyLoopConditionsTraverser::visitAggregate(Visit visit, TIntermAggregate *node) |
| 77 | { |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 78 | if (!mInsideLoopInitConditionOrExpression) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 79 | return false; |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 80 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 81 | if (mFoundLoopToChange) |
| 82 | return false; // Already decided to change this loop. |
| 83 | |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 84 | mFoundLoopToChange = mConditionsToSimplify.match(node, getParentNode()); |
| 85 | return !mFoundLoopToChange; |
| 86 | } |
| 87 | |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 88 | bool SimplifyLoopConditionsTraverser::visitTernary(Visit visit, TIntermTernary *node) |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 89 | { |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 90 | if (!mInsideLoopInitConditionOrExpression) |
| 91 | return false; |
| 92 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 93 | if (mFoundLoopToChange) |
| 94 | return false; // Already decided to change this loop. |
| 95 | |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 96 | mFoundLoopToChange = mConditionsToSimplify.match(node); |
| 97 | return !mFoundLoopToChange; |
| 98 | } |
| 99 | |
| 100 | bool SimplifyLoopConditionsTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node) |
| 101 | { |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 102 | if (!mInsideLoopInitConditionOrExpression) |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 103 | return false; |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 104 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 105 | if (mFoundLoopToChange) |
| 106 | return false; // Already decided to change this loop. |
| 107 | |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 108 | mFoundLoopToChange = mConditionsToSimplify.match(node); |
| 109 | return !mFoundLoopToChange; |
| 110 | } |
| 111 | |
| 112 | void SimplifyLoopConditionsTraverser::traverseLoop(TIntermLoop *node) |
| 113 | { |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 114 | // Mark that we're inside a loop condition or expression, and determine if the loop needs to be |
| 115 | // transformed. |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 116 | |
Olli Etuaho | 1d9dcc2 | 2017-01-19 11:25:32 +0000 | [diff] [blame] | 117 | ScopedNodeInTraversalPath addToPath(this, node); |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 118 | |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 119 | mInsideLoopInitConditionOrExpression = true; |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 120 | mFoundLoopToChange = false; |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 121 | |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 122 | if (!mFoundLoopToChange && node->getInit()) |
| 123 | { |
| 124 | node->getInit()->traverse(this); |
| 125 | } |
| 126 | |
| 127 | if (!mFoundLoopToChange && node->getCondition()) |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 128 | { |
| 129 | node->getCondition()->traverse(this); |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (!mFoundLoopToChange && node->getExpression()) |
| 133 | { |
| 134 | node->getExpression()->traverse(this); |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 135 | } |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 136 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 137 | mInsideLoopInitConditionOrExpression = false; |
| 138 | |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 139 | if (mFoundLoopToChange) |
| 140 | { |
Olli Etuaho | 4dd06d5 | 2017-07-05 12:41:06 +0300 | [diff] [blame] | 141 | nextTemporaryId(); |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 142 | |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 143 | // Replace the loop condition with a boolean variable that's updated on each iteration. |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 144 | TLoopType loopType = node->getType(); |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 145 | if (loopType == ELoopWhile) |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 146 | { |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 147 | // Transform: |
| 148 | // while (expr) { body; } |
| 149 | // into |
| 150 | // bool s0 = expr; |
| 151 | // while (s0) { { body; } s0 = expr; } |
| 152 | TIntermSequence tempInitSeq; |
| 153 | tempInitSeq.push_back(createTempInitDeclaration(node->getCondition()->deepCopy())); |
| 154 | insertStatementsInParentBlock(tempInitSeq); |
| 155 | |
| 156 | TIntermBlock *newBody = new TIntermBlock(); |
| 157 | if (node->getBody()) |
| 158 | { |
| 159 | newBody->getSequence()->push_back(node->getBody()); |
| 160 | } |
| 161 | newBody->getSequence()->push_back( |
| 162 | createTempAssignment(node->getCondition()->deepCopy())); |
| 163 | |
| 164 | // Can't use queueReplacement to replace old body, since it may have been nullptr. |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 165 | // It's safe to do the replacements in place here - the new body will still be |
| 166 | // traversed, but that won't create any problems. |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 167 | node->setBody(newBody); |
| 168 | node->setCondition(createTempSymbol(node->getCondition()->getType())); |
| 169 | } |
| 170 | else if (loopType == ELoopDoWhile) |
| 171 | { |
| 172 | // Transform: |
| 173 | // do { |
| 174 | // body; |
| 175 | // } while (expr); |
| 176 | // into |
| 177 | // bool s0 = true; |
| 178 | // do { |
| 179 | // { body; } |
| 180 | // s0 = expr; |
| 181 | // } while (s0); |
| 182 | TIntermSequence tempInitSeq; |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 183 | tempInitSeq.push_back(createTempInitDeclaration(CreateBoolNode(true))); |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 184 | insertStatementsInParentBlock(tempInitSeq); |
| 185 | |
| 186 | TIntermBlock *newBody = new TIntermBlock(); |
| 187 | if (node->getBody()) |
| 188 | { |
| 189 | newBody->getSequence()->push_back(node->getBody()); |
| 190 | } |
| 191 | newBody->getSequence()->push_back( |
| 192 | createTempAssignment(node->getCondition()->deepCopy())); |
| 193 | |
| 194 | // Can't use queueReplacement to replace old body, since it may have been nullptr. |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 195 | // It's safe to do the replacements in place here - the new body will still be |
| 196 | // traversed, but that won't create any problems. |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 197 | node->setBody(newBody); |
| 198 | node->setCondition(createTempSymbol(node->getCondition()->getType())); |
| 199 | } |
| 200 | else if (loopType == ELoopFor) |
| 201 | { |
| 202 | // Move the loop condition inside the loop. |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 203 | // Transform: |
| 204 | // for (init; expr; exprB) { body; } |
| 205 | // into |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 206 | // { |
| 207 | // init; |
| 208 | // bool s0 = expr; |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 209 | // while (s0) { |
| 210 | // { body; } |
| 211 | // exprB; |
| 212 | // s0 = expr; |
| 213 | // } |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 214 | // } |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 215 | TIntermBlock *loopScope = new TIntermBlock(); |
| 216 | TIntermSequence *loopScopeSequence = loopScope->getSequence(); |
| 217 | |
| 218 | // Insert "init;" |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 219 | if (node->getInit()) |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 220 | { |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 221 | loopScopeSequence->push_back(node->getInit()); |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 222 | } |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 223 | |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 224 | // Insert "bool s0 = expr;" if applicable, "bool s0 = true;" otherwise |
| 225 | TIntermTyped *conditionInitializer = nullptr; |
| 226 | if (node->getCondition()) |
| 227 | { |
| 228 | conditionInitializer = node->getCondition()->deepCopy(); |
| 229 | } |
| 230 | else |
| 231 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 232 | conditionInitializer = CreateBoolNode(true); |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 233 | } |
| 234 | loopScopeSequence->push_back(createTempInitDeclaration(conditionInitializer)); |
| 235 | |
| 236 | // Insert "{ body; }" in the while loop |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 237 | TIntermBlock *whileLoopBody = new TIntermBlock(); |
| 238 | if (node->getBody()) |
| 239 | { |
| 240 | whileLoopBody->getSequence()->push_back(node->getBody()); |
| 241 | } |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 242 | // Insert "exprB;" in the while loop |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 243 | if (node->getExpression()) |
| 244 | { |
| 245 | whileLoopBody->getSequence()->push_back(node->getExpression()); |
| 246 | } |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 247 | // Insert "s0 = expr;" in the while loop |
| 248 | if (node->getCondition()) |
| 249 | { |
| 250 | whileLoopBody->getSequence()->push_back( |
| 251 | createTempAssignment(node->getCondition()->deepCopy())); |
| 252 | } |
| 253 | |
| 254 | // Create "while(s0) { whileLoopBody }" |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 255 | TIntermLoop *whileLoop = new TIntermLoop( |
Corentin Wallez | 36fd100 | 2016-12-08 11:30:44 -0500 | [diff] [blame] | 256 | ELoopWhile, nullptr, createTempSymbol(conditionInitializer->getType()), nullptr, |
Corentin Wallez | 1212bca | 2016-11-23 13:44:05 -0500 | [diff] [blame] | 257 | whileLoopBody); |
| 258 | loopScope->getSequence()->push_back(whileLoop); |
Olli Etuaho | ea39a22 | 2017-07-06 12:47:59 +0300 | [diff] [blame] | 259 | queueReplacement(loopScope, OriginalNode::IS_DROPPED); |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 260 | |
| 261 | // After this the old body node will be traversed and loops inside it may be |
| 262 | // transformed. This is fine, since the old body node will still be in the AST after the |
| 263 | // transformation that's queued here, and transforming loops inside it doesn't need to |
| 264 | // know the exact post-transform path to it. |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 268 | mFoundLoopToChange = false; |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 269 | |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 270 | // We traverse the body of the loop even if the loop is transformed. |
| 271 | if (node->getBody()) |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 272 | node->getBody()->traverse(this); |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | } // namespace |
| 276 | |
| 277 | void SimplifyLoopConditions(TIntermNode *root, |
| 278 | unsigned int conditionsToSimplifyMask, |
Olli Etuaho | 4dd06d5 | 2017-07-05 12:41:06 +0300 | [diff] [blame] | 279 | TSymbolUniqueId *temporaryId, |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 280 | const TSymbolTable &symbolTable, |
| 281 | int shaderVersion) |
| 282 | { |
| 283 | SimplifyLoopConditionsTraverser traverser(conditionsToSimplifyMask, symbolTable, shaderVersion); |
Olli Etuaho | 4dd06d5 | 2017-07-05 12:41:06 +0300 | [diff] [blame] | 284 | ASSERT(temporaryId != nullptr); |
| 285 | traverser.useTemporaryId(temporaryId); |
Olli Etuaho | 9676d1a | 2017-05-16 11:29:24 +0300 | [diff] [blame] | 286 | root->traverse(&traverser); |
| 287 | traverser.updateTree(); |
Olli Etuaho | 3cbb27a | 2016-07-14 11:55:48 +0300 | [diff] [blame] | 288 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 289 | |
| 290 | } // namespace sh |