Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2014 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 | // RewriteElseBlocks.cpp: Implementation for tree transform to change |
| 7 | // all if-else blocks to if-if blocks. |
| 8 | // |
| 9 | |
| 10 | #include "compiler/translator/RewriteElseBlocks.h" |
| 11 | #include "compiler/translator/NodeSearch.h" |
| 12 | #include "compiler/translator/SymbolTable.h" |
| 13 | |
| 14 | namespace sh |
| 15 | { |
| 16 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | class ElseBlockRewriter : public TIntermTraverser |
| 21 | { |
| 22 | public: |
| 23 | ElseBlockRewriter(); |
| 24 | |
| 25 | protected: |
| 26 | bool visitAggregate(Visit visit, TIntermAggregate *aggregate); |
| 27 | |
| 28 | private: |
| 29 | int mTemporaryIndex; |
| 30 | const TType *mFunctionType; |
| 31 | |
| 32 | TIntermNode *rewriteSelection(TIntermSelection *selection); |
| 33 | }; |
| 34 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 35 | TIntermSymbol *MakeNewTemporary(const TString &name, TBasicType type) |
| 36 | { |
Olli Etuaho | 78174db | 2015-04-21 16:14:00 +0300 | [diff] [blame] | 37 | TType variableType(type, EbpHigh, EvqTemporary); |
| 38 | TIntermSymbol *node = new TIntermSymbol(-1, name, variableType); |
| 39 | node->setInternal(true); |
| 40 | return node; |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TIntermBinary *MakeNewBinary(TOperator op, TIntermTyped *left, TIntermTyped *right, const TType &resultType) |
| 44 | { |
| 45 | TIntermBinary *binary = new TIntermBinary(op); |
| 46 | binary->setLeft(left); |
| 47 | binary->setRight(right); |
| 48 | binary->setType(resultType); |
| 49 | return binary; |
| 50 | } |
| 51 | |
| 52 | TIntermUnary *MakeNewUnary(TOperator op, TIntermTyped *operand) |
| 53 | { |
| 54 | TIntermUnary *unary = new TIntermUnary(op, operand->getType()); |
| 55 | unary->setOperand(operand); |
| 56 | return unary; |
| 57 | } |
| 58 | |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 59 | ElseBlockRewriter::ElseBlockRewriter() |
| 60 | : TIntermTraverser(true, false, true, false), |
| 61 | mTemporaryIndex(0), |
| 62 | mFunctionType(NULL) |
| 63 | {} |
| 64 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 65 | bool ElseBlockRewriter::visitAggregate(Visit visit, TIntermAggregate *node) |
| 66 | { |
| 67 | switch (node->getOp()) |
| 68 | { |
| 69 | case EOpSequence: |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 70 | if (visit == PostVisit) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 71 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 72 | for (size_t statementIndex = 0; statementIndex != node->getSequence()->size(); statementIndex++) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 73 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 74 | TIntermNode *statement = (*node->getSequence())[statementIndex]; |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 75 | TIntermSelection *selection = statement->getAsSelectionNode(); |
| 76 | if (selection && selection->getFalseBlock() != NULL) |
| 77 | { |
Jamie Madill | ed3eef1 | 2014-02-26 09:47:11 -0500 | [diff] [blame] | 78 | // Check for if / else if |
| 79 | TIntermSelection *elseIfBranch = selection->getFalseBlock()->getAsSelectionNode(); |
| 80 | if (elseIfBranch) |
| 81 | { |
| 82 | selection->replaceChildNode(elseIfBranch, rewriteSelection(elseIfBranch)); |
| 83 | delete elseIfBranch; |
| 84 | } |
| 85 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 86 | (*node->getSequence())[statementIndex] = rewriteSelection(selection); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 87 | delete selection; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | break; |
| 92 | |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 93 | case EOpFunction: |
| 94 | // Store the current function context (see comment below) |
| 95 | mFunctionType = ((visit == PreVisit) ? &node->getType() : NULL); |
| 96 | break; |
| 97 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 98 | default: break; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | TIntermNode *ElseBlockRewriter::rewriteSelection(TIntermSelection *selection) |
| 105 | { |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 106 | ASSERT(selection != NULL); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 107 | |
| 108 | TString temporaryName = "cond_" + str(mTemporaryIndex++); |
| 109 | TIntermTyped *typedCondition = selection->getCondition()->getAsTyped(); |
| 110 | TType resultType(EbtBool, EbpUndefined); |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 111 | TIntermSymbol *conditionSymbolInit = MakeNewTemporary(temporaryName, EbtBool); |
| 112 | TIntermBinary *storeCondition = MakeNewBinary(EOpInitialize, conditionSymbolInit, |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 113 | typedCondition, resultType); |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 114 | TIntermNode *negatedElse = NULL; |
| 115 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 116 | TIntermSelection *falseBlock = NULL; |
| 117 | |
| 118 | if (selection->getFalseBlock()) |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 119 | { |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 120 | // crbug.com/346463 |
| 121 | // D3D generates error messages claiming a function has no return value, when rewriting |
| 122 | // an if-else clause that returns something non-void in a function. By appending dummy |
| 123 | // returns (that are unreachable) we can silence this compile error. |
| 124 | if (mFunctionType && mFunctionType->getBasicType() != EbtVoid) |
| 125 | { |
| 126 | TString typeString = mFunctionType->getStruct() ? mFunctionType->getStruct()->name() : |
| 127 | mFunctionType->getBasicString(); |
| 128 | TString rawText = "return (" + typeString + ")0"; |
| 129 | negatedElse = new TIntermRaw(*mFunctionType, rawText); |
| 130 | } |
| 131 | |
| 132 | TIntermSymbol *conditionSymbolElse = MakeNewTemporary(temporaryName, EbtBool); |
| 133 | TIntermUnary *negatedCondition = MakeNewUnary(EOpLogicalNot, conditionSymbolElse); |
| 134 | falseBlock = new TIntermSelection(negatedCondition, |
| 135 | selection->getFalseBlock(), negatedElse); |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 138 | TIntermSymbol *conditionSymbolSel = MakeNewTemporary(temporaryName, EbtBool); |
| 139 | TIntermSelection *newSelection = new TIntermSelection(conditionSymbolSel, |
| 140 | selection->getTrueBlock(), falseBlock); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 141 | |
| 142 | TIntermAggregate *declaration = new TIntermAggregate(EOpDeclaration); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 143 | declaration->getSequence()->push_back(storeCondition); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 144 | |
| 145 | TIntermAggregate *block = new TIntermAggregate(EOpSequence); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 146 | block->getSequence()->push_back(declaration); |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 147 | block->getSequence()->push_back(newSelection); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 148 | |
| 149 | return block; |
| 150 | } |
| 151 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 154 | void RewriteElseBlocks(TIntermNode *node) |
| 155 | { |
| 156 | ElseBlockRewriter rewriter; |
| 157 | node->traverse(&rewriter); |
| 158 | } |
| 159 | |
| 160 | } |