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