Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 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 | // |
| 6 | |
| 7 | // RewriteDoWhile.cpp: rewrites do-while loops using another equivalent |
| 8 | // construct. |
| 9 | |
| 10 | #include "compiler/translator/RewriteDoWhile.h" |
| 11 | |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame^] | 12 | #include "compiler/translator/IntermNode_util.h" |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 13 | #include "compiler/translator/IntermTraverse.h" |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 14 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 15 | namespace sh |
| 16 | { |
| 17 | |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | |
| 21 | // An AST traverser that rewrites loops of the form |
| 22 | // do { |
| 23 | // CODE; |
| 24 | // } while (CONDITION) |
| 25 | // |
| 26 | // to loops of the form |
| 27 | // bool temp = false; |
| 28 | // while (true) { |
| 29 | // if (temp) { |
| 30 | // if (!CONDITION) { |
| 31 | // break; |
| 32 | // } |
| 33 | // } |
| 34 | // temp = true; |
| 35 | // CODE; |
| 36 | // } |
| 37 | // |
| 38 | // The reason we don't use a simpler form, with for example just (temp && !CONDITION) in the |
| 39 | // while condition, is that short-circuit is often badly supported by driver shader compiler. |
| 40 | // The double if has the same effect, but forces shader compilers to behave. |
| 41 | // |
| 42 | // TODO(cwallez) when UnfoldShortCircuitIntoIf handles loops correctly, revisit this as we might |
| 43 | // be able to use while (temp || CONDITION) with temp initially set to true then run |
| 44 | // UnfoldShortCircuitIntoIf |
| 45 | class DoWhileRewriter : public TIntermTraverser |
| 46 | { |
| 47 | public: |
Olli Etuaho | 590f623 | 2017-07-21 11:10:26 +0300 | [diff] [blame] | 48 | DoWhileRewriter(TSymbolTable *symbolTable) : TIntermTraverser(true, false, false, symbolTable) |
| 49 | { |
| 50 | } |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 51 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 52 | bool visitBlock(Visit, TIntermBlock *node) override |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 53 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 54 | // A well-formed AST can only have do-while inside TIntermBlock. By doing a prefix traversal |
| 55 | // we are able to replace the do-while in the sequence directly as the content of the |
| 56 | // do-while will be traversed later. |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 57 | |
| 58 | TIntermSequence *statements = node->getSequence(); |
| 59 | |
| 60 | // The statements vector will have new statements inserted when we encounter a do-while, |
| 61 | // which prevents us from using a range-based for loop. Using the usual i++ works, as |
| 62 | // the (two) new statements inserted replace the statement at the current position. |
| 63 | for (size_t i = 0; i < statements->size(); i++) |
| 64 | { |
| 65 | TIntermNode *statement = (*statements)[i]; |
| 66 | TIntermLoop *loop = statement->getAsLoopNode(); |
| 67 | |
| 68 | if (loop == nullptr || loop->getType() != ELoopDoWhile) |
| 69 | { |
| 70 | continue; |
| 71 | } |
| 72 | |
Olli Etuaho | 590f623 | 2017-07-21 11:10:26 +0300 | [diff] [blame] | 73 | // Found a loop to change. |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame^] | 74 | TType boolType(EbtBool); |
| 75 | TVariable *conditionVariable = CreateTempVariable(mSymbolTable, boolType); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 76 | |
| 77 | // bool temp = false; |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame^] | 78 | TIntermDeclaration *tempDeclaration = |
| 79 | CreateTempInitDeclarationNode(conditionVariable, CreateBoolNode(false)); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 80 | |
| 81 | // temp = true; |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame^] | 82 | TIntermBinary *assignTrue = |
| 83 | CreateTempAssignmentNode(conditionVariable, CreateBoolNode(true)); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 84 | |
| 85 | // if (temp) { |
| 86 | // if (!CONDITION) { |
| 87 | // break; |
| 88 | // } |
| 89 | // } |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 90 | TIntermIfElse *breakIf = nullptr; |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 91 | { |
| 92 | TIntermBranch *breakStatement = new TIntermBranch(EOpBreak, nullptr); |
| 93 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 94 | TIntermBlock *breakBlock = new TIntermBlock(); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 95 | breakBlock->getSequence()->push_back(breakStatement); |
| 96 | |
Olli Etuaho | a223430 | 2016-08-31 12:05:39 +0300 | [diff] [blame] | 97 | TIntermUnary *negatedCondition = |
| 98 | new TIntermUnary(EOpLogicalNot, loop->getCondition()); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 99 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 100 | TIntermIfElse *innerIf = new TIntermIfElse(negatedCondition, breakBlock, nullptr); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 101 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 102 | TIntermBlock *innerIfBlock = new TIntermBlock(); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 103 | innerIfBlock->getSequence()->push_back(innerIf); |
| 104 | |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame^] | 105 | breakIf = new TIntermIfElse(CreateTempSymbolNode(conditionVariable), innerIfBlock, |
| 106 | nullptr); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Assemble the replacement loops, reusing the do-while loop's body and inserting our |
| 110 | // statements at the front. |
| 111 | TIntermLoop *newLoop = nullptr; |
| 112 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 113 | TIntermBlock *body = loop->getBody(); |
| 114 | if (body == nullptr) |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 115 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 116 | body = new TIntermBlock(); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 117 | } |
| 118 | auto sequence = body->getSequence(); |
| 119 | sequence->insert(sequence->begin(), assignTrue); |
| 120 | sequence->insert(sequence->begin(), breakIf); |
| 121 | |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame^] | 122 | newLoop = new TIntermLoop(ELoopWhile, nullptr, CreateBoolNode(true), nullptr, body); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TIntermSequence replacement; |
| 126 | replacement.push_back(tempDeclaration); |
| 127 | replacement.push_back(newLoop); |
| 128 | |
| 129 | node->replaceChildNodeWithMultiple(loop, replacement); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | } // anonymous namespace |
| 136 | |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 137 | void RewriteDoWhile(TIntermNode *root, TSymbolTable *symbolTable) |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 138 | { |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 139 | DoWhileRewriter rewriter(symbolTable); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 140 | |
| 141 | root->traverse(&rewriter); |
| 142 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 143 | |
| 144 | } // namespace sh |