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